OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 }; | 402 }; |
403 | 403 |
404 // ---------------------------------------------------------------------------- | 404 // ---------------------------------------------------------------------------- |
405 // JAVASCRIPT PARSING | 405 // JAVASCRIPT PARSING |
406 | 406 |
407 class Parser; | 407 class Parser; |
408 class SingletonLogger; | 408 class SingletonLogger; |
409 | 409 |
410 class ParserTraits { | 410 class ParserTraits { |
411 public: | 411 public: |
412 typedef Parser* ParserType; | 412 struct Type { |
413 // Return types for traversing functions. | 413 typedef v8::internal::Parser* Parser; |
414 typedef Handle<String> IdentifierType; | 414 |
415 typedef Expression* ExpressionType; | 415 // Types used by FunctionState and BlockState. |
| 416 typedef v8::internal::Scope Scope; |
| 417 typedef AstNodeFactory<AstConstructionVisitor> Factory; |
| 418 typedef Variable GeneratorVariable; |
| 419 typedef v8::internal::Zone Zone; |
| 420 |
| 421 // Return types for traversing functions. |
| 422 typedef Handle<String> Identifier; |
| 423 typedef v8::internal::Expression* Expression; |
| 424 }; |
416 | 425 |
417 explicit ParserTraits(Parser* parser) : parser_(parser) {} | 426 explicit ParserTraits(Parser* parser) : parser_(parser) {} |
418 | 427 |
| 428 // Custom operations executed when FunctionStates are created and destructed. |
| 429 template<typename FS> |
| 430 static void SetUpFunctionState(FS* function_state, Zone* zone) { |
| 431 Isolate* isolate = zone->isolate(); |
| 432 function_state->isolate_ = isolate; |
| 433 function_state->saved_ast_node_id_ = isolate->ast_node_id(); |
| 434 isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); |
| 435 } |
| 436 |
| 437 template<typename FS> |
| 438 static void TearDownFunctionState(FS* function_state) { |
| 439 if (function_state->outer_function_state_ != NULL) { |
| 440 function_state->isolate_->set_ast_node_id( |
| 441 function_state->saved_ast_node_id_); |
| 442 } |
| 443 } |
| 444 |
419 // Helper functions for recursive descent. | 445 // Helper functions for recursive descent. |
420 bool is_classic_mode() const; | |
421 bool is_generator() const; | |
422 bool IsEvalOrArguments(Handle<String> identifier) const; | 446 bool IsEvalOrArguments(Handle<String> identifier) const; |
423 int NextMaterializedLiteralIndex(); | |
424 | 447 |
425 // Reporting errors. | 448 // Reporting errors. |
426 void ReportMessageAt(Scanner::Location source_location, | 449 void ReportMessageAt(Scanner::Location source_location, |
427 const char* message, | 450 const char* message, |
428 Vector<const char*> args); | 451 Vector<const char*> args); |
429 void ReportMessage(const char* message, Vector<Handle<String> > args); | 452 void ReportMessage(const char* message, Vector<Handle<String> > args); |
430 void ReportMessageAt(Scanner::Location source_location, | 453 void ReportMessageAt(Scanner::Location source_location, |
431 const char* message, | 454 const char* message, |
432 Vector<Handle<String> > args); | 455 Vector<Handle<String> > args); |
433 | 456 |
434 // "null" return type creators. | 457 // "null" return type creators. |
435 static IdentifierType EmptyIdentifier() { | 458 static Handle<String> EmptyIdentifier() { |
436 return Handle<String>(); | 459 return Handle<String>(); |
437 } | 460 } |
438 static ExpressionType EmptyExpression() { | 461 static Expression* EmptyExpression() { |
439 return NULL; | 462 return NULL; |
440 } | 463 } |
441 | 464 |
442 // Producing data during the recursive descent. | 465 // Producing data during the recursive descent. |
443 IdentifierType GetSymbol(); | 466 Handle<String> GetSymbol(); |
444 IdentifierType NextLiteralString(PretenureFlag tenured); | 467 Handle<String> NextLiteralString(PretenureFlag tenured); |
445 ExpressionType NewRegExpLiteral(IdentifierType js_pattern, | |
446 IdentifierType js_flags, | |
447 int literal_index, | |
448 int pos); | |
449 | 468 |
450 private: | 469 private: |
451 Parser* parser_; | 470 Parser* parser_; |
452 }; | 471 }; |
453 | 472 |
454 | 473 |
455 class Parser : public ParserBase<ParserTraits> { | 474 class Parser : public ParserBase<ParserTraits> { |
456 public: | 475 public: |
457 explicit Parser(CompilationInfo* info); | 476 explicit Parser(CompilationInfo* info); |
458 ~Parser() { | 477 ~Parser() { |
(...skipping 28 matching lines...) Expand all Loading... |
487 kStatement, | 506 kStatement, |
488 kForStatement | 507 kForStatement |
489 }; | 508 }; |
490 | 509 |
491 // If a list of variable declarations includes any initializers. | 510 // If a list of variable declarations includes any initializers. |
492 enum VariableDeclarationProperties { | 511 enum VariableDeclarationProperties { |
493 kHasInitializers, | 512 kHasInitializers, |
494 kHasNoInitializers | 513 kHasNoInitializers |
495 }; | 514 }; |
496 | 515 |
497 class BlockState; | |
498 | |
499 class FunctionState BASE_EMBEDDED { | |
500 public: | |
501 FunctionState(FunctionState** function_state_stack, | |
502 Scope** scope_stack, Scope* scope, | |
503 Zone* zone); | |
504 ~FunctionState(); | |
505 | |
506 int NextMaterializedLiteralIndex() { | |
507 return next_materialized_literal_index_++; | |
508 } | |
509 int materialized_literal_count() { | |
510 return next_materialized_literal_index_ - JSFunction::kLiteralsPrefixSize; | |
511 } | |
512 | |
513 int NextHandlerIndex() { return next_handler_index_++; } | |
514 int handler_count() { return next_handler_index_; } | |
515 | |
516 void AddProperty() { expected_property_count_++; } | |
517 int expected_property_count() { return expected_property_count_; } | |
518 | |
519 void set_generator_object_variable(Variable *variable) { | |
520 ASSERT(variable != NULL); | |
521 ASSERT(!is_generator()); | |
522 generator_object_variable_ = variable; | |
523 } | |
524 Variable* generator_object_variable() const { | |
525 return generator_object_variable_; | |
526 } | |
527 bool is_generator() const { | |
528 return generator_object_variable_ != NULL; | |
529 } | |
530 | |
531 AstNodeFactory<AstConstructionVisitor>* factory() { return &factory_; } | |
532 | |
533 private: | |
534 // Used to assign an index to each literal that needs materialization in | |
535 // the function. Includes regexp literals, and boilerplate for object and | |
536 // array literals. | |
537 int next_materialized_literal_index_; | |
538 | |
539 // Used to assign a per-function index to try and catch handlers. | |
540 int next_handler_index_; | |
541 | |
542 // Properties count estimation. | |
543 int expected_property_count_; | |
544 | |
545 // For generators, the variable that holds the generator object. This | |
546 // variable is used by yield expressions and return statements. NULL | |
547 // indicates that this function is not a generator. | |
548 Variable* generator_object_variable_; | |
549 | |
550 FunctionState** function_state_stack_; | |
551 FunctionState* outer_function_state_; | |
552 Scope** scope_stack_; | |
553 Scope* outer_scope_; | |
554 Isolate* isolate_; | |
555 int saved_ast_node_id_; | |
556 AstNodeFactory<AstConstructionVisitor> factory_; | |
557 }; | |
558 | |
559 class ParsingModeScope BASE_EMBEDDED { | 516 class ParsingModeScope BASE_EMBEDDED { |
560 public: | 517 public: |
561 ParsingModeScope(Parser* parser, Mode mode) | 518 ParsingModeScope(Parser* parser, Mode mode) |
562 : parser_(parser), | 519 : parser_(parser), |
563 old_mode_(parser->mode()) { | 520 old_mode_(parser->mode()) { |
564 parser_->mode_ = mode; | 521 parser_->mode_ = mode; |
565 } | 522 } |
566 ~ParsingModeScope() { | 523 ~ParsingModeScope() { |
567 parser_->mode_ = old_mode_; | 524 parser_->mode_ = old_mode_; |
568 } | 525 } |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 Handle<Object> second); | 720 Handle<Object> second); |
764 | 721 |
765 // Generic AST generator for throwing errors from compiled code. | 722 // Generic AST generator for throwing errors from compiled code. |
766 Expression* NewThrowError(Handle<String> constructor, | 723 Expression* NewThrowError(Handle<String> constructor, |
767 Handle<String> type, | 724 Handle<String> type, |
768 Vector< Handle<Object> > arguments); | 725 Vector< Handle<Object> > arguments); |
769 | 726 |
770 PreParser::PreParseResult LazyParseFunctionLiteral( | 727 PreParser::PreParseResult LazyParseFunctionLiteral( |
771 SingletonLogger* logger); | 728 SingletonLogger* logger); |
772 | 729 |
773 AstNodeFactory<AstConstructionVisitor>* factory() { | |
774 return function_state_->factory(); | |
775 } | |
776 | |
777 Isolate* isolate_; | 730 Isolate* isolate_; |
778 ZoneList<Handle<String> > symbol_cache_; | 731 ZoneList<Handle<String> > symbol_cache_; |
779 | 732 |
780 Handle<Script> script_; | 733 Handle<Script> script_; |
781 Scanner scanner_; | 734 Scanner scanner_; |
782 PreParser* reusable_preparser_; | 735 PreParser* reusable_preparser_; |
783 Scope* scope_; // Scope stack. | |
784 Scope* original_scope_; // for ES5 function declarations in sloppy eval | 736 Scope* original_scope_; // for ES5 function declarations in sloppy eval |
785 FunctionState* function_state_; // Function state stack. | |
786 Target* target_stack_; // for break, continue statements | 737 Target* target_stack_; // for break, continue statements |
787 v8::Extension* extension_; | 738 v8::Extension* extension_; |
788 ScriptDataImpl* pre_parse_data_; | 739 ScriptDataImpl* pre_parse_data_; |
789 FuncNameInferrer* fni_; | 740 FuncNameInferrer* fni_; |
790 | 741 |
791 Mode mode_; | 742 Mode mode_; |
792 | 743 |
793 Zone* zone_; | 744 Zone* zone_; |
794 CompilationInfo* info_; | 745 CompilationInfo* info_; |
795 }; | 746 }; |
(...skipping 23 matching lines...) Expand all Loading... |
819 private: | 770 private: |
820 static const int kLiteralTypeSlot = 0; | 771 static const int kLiteralTypeSlot = 0; |
821 static const int kElementsSlot = 1; | 772 static const int kElementsSlot = 1; |
822 | 773 |
823 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); | 774 DISALLOW_IMPLICIT_CONSTRUCTORS(CompileTimeValue); |
824 }; | 775 }; |
825 | 776 |
826 } } // namespace v8::internal | 777 } } // namespace v8::internal |
827 | 778 |
828 #endif // V8_PARSER_H_ | 779 #endif // V8_PARSER_H_ |
OLD | NEW |