OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_AST_SCOPES_H_ | 5 #ifndef V8_AST_SCOPES_H_ |
6 #define V8_AST_SCOPES_H_ | 6 #define V8_AST_SCOPES_H_ |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/base/hashmap.h" | 9 #include "src/base/hashmap.h" |
10 #include "src/globals.h" | 10 #include "src/globals.h" |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 111 |
112 // Compute top scope and allocate variables. For lazy compilation the top | 112 // Compute top scope and allocate variables. For lazy compilation the top |
113 // scope only contains the single lazily compiled function, so this | 113 // scope only contains the single lazily compiled function, so this |
114 // doesn't re-allocate variables repeatedly. | 114 // doesn't re-allocate variables repeatedly. |
115 static bool Analyze(ParseInfo* info); | 115 static bool Analyze(ParseInfo* info); |
116 | 116 |
117 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 117 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
118 Context* context, Scope* script_scope, | 118 Context* context, Scope* script_scope, |
119 AstValueFactory* ast_value_factory); | 119 AstValueFactory* ast_value_factory); |
120 | 120 |
121 #ifdef DEBUG | |
121 // The scope name is only used for printing/debugging. | 122 // The scope name is only used for printing/debugging. |
122 void SetScopeName(const AstRawString* scope_name) { | 123 void SetScopeName(const AstRawString* scope_name) { |
123 scope_name_ = scope_name; | 124 scope_name_ = scope_name; |
124 } | 125 } |
126 #endif | |
125 | 127 |
126 void DeclareThis(AstValueFactory* ast_value_factory); | 128 void DeclareThis(AstValueFactory* ast_value_factory); |
127 void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory); | 129 void DeclareDefaultFunctionVariables(AstValueFactory* ast_value_factory); |
128 | 130 |
129 // Checks if the block scope is redundant, i.e. it does not contain any | 131 // Checks if the block scope is redundant, i.e. it does not contain any |
130 // block scoped declarations. In that case it is removed from the scope | 132 // block scoped declarations. In that case it is removed from the scope |
131 // tree and its children are reparented. | 133 // tree and its children are reparented. |
132 Scope* FinalizeBlockScope(); | 134 Scope* FinalizeBlockScope(); |
133 | 135 |
134 // Inserts outer_scope into this scope's scope chain (and removes this | 136 // Inserts outer_scope into this scope's scope chain (and removes this |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
512 Scope* sibling() const { return sibling_; } | 514 Scope* sibling() const { return sibling_; } |
513 | 515 |
514 // The scope immediately surrounding this scope, or NULL. | 516 // The scope immediately surrounding this scope, or NULL. |
515 Scope* outer_scope() const { return outer_scope_; } | 517 Scope* outer_scope() const { return outer_scope_; } |
516 | 518 |
517 // The ModuleDescriptor for this scope; only for module scopes. | 519 // The ModuleDescriptor for this scope; only for module scopes. |
518 ModuleDescriptor* module() const { return module_descriptor_; } | 520 ModuleDescriptor* module() const { return module_descriptor_; } |
519 | 521 |
520 const AstRawString* catch_variable_name() const { | 522 const AstRawString* catch_variable_name() const { |
521 DCHECK(is_catch_scope()); | 523 DCHECK(is_catch_scope()); |
522 DCHECK(num_var() == 1); | 524 DCHECK_EQ(1, num_var()); |
523 return static_cast<AstRawString*>(variables_.Start()->key); | 525 return static_cast<AstRawString*>(variables_.Start()->key); |
524 } | 526 } |
525 | 527 |
526 // --------------------------------------------------------------------------- | 528 // --------------------------------------------------------------------------- |
527 // Variable allocation. | 529 // Variable allocation. |
528 | 530 |
529 // Collect stack and context allocated local variables in this scope. Note | 531 // Collect stack and context allocated local variables in this scope. Note |
530 // that the function variable - if present - is not collected and should be | 532 // that the function variable - if present - is not collected and should be |
531 // handled separately. | 533 // handled separately. |
532 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, | 534 void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
533 ZoneList<Variable*>* context_locals, | 535 ZoneList<Variable*>* context_locals, |
534 ZoneList<Variable*>* context_globals); | 536 ZoneList<Variable*>* context_globals); |
535 | 537 |
536 // Current number of var locals. | |
537 int num_var() const { return num_var_; } | |
538 | |
539 // Result of variable allocation. | 538 // Result of variable allocation. |
540 int num_stack_slots() const { return num_stack_slots_; } | 539 int num_stack_slots() const { return num_stack_slots_; } |
541 int num_heap_slots() const { return num_heap_slots_; } | 540 int num_heap_slots() const { return num_heap_slots_; } |
542 int num_global_slots() const { return num_global_slots_; } | 541 int num_global_slots() const { return num_global_slots_; } |
543 | 542 |
544 int StackLocalCount() const; | 543 int StackLocalCount() const; |
545 int ContextLocalCount() const; | 544 int ContextLocalCount() const; |
546 int ContextGlobalCount() const; | 545 int ContextGlobalCount() const; |
547 | 546 |
548 // Make sure this scope and all outer scopes are eagerly compiled. | 547 // Make sure this scope and all outer scopes are eagerly compiled. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
597 return variables_.Lookup(name) != NULL; | 596 return variables_.Lookup(name) != NULL; |
598 } | 597 } |
599 | 598 |
600 bool IsDeclaredParameter(const AstRawString* name) { | 599 bool IsDeclaredParameter(const AstRawString* name) { |
601 // If IsSimpleParameterList is false, duplicate parameters are not allowed, | 600 // If IsSimpleParameterList is false, duplicate parameters are not allowed, |
602 // however `arguments` may be allowed if function is not strict code. Thus, | 601 // however `arguments` may be allowed if function is not strict code. Thus, |
603 // the assumptions explained above do not hold. | 602 // the assumptions explained above do not hold. |
604 return params_.Contains(variables_.Lookup(name)); | 603 return params_.Contains(variables_.Lookup(name)); |
605 } | 604 } |
606 | 605 |
606 int num_var() const { return variables_.occupancy(); } | |
adamk
2016/08/01 22:47:07
You might have noticed that this slightly changes
| |
607 | |
607 SloppyBlockFunctionMap* sloppy_block_function_map() { | 608 SloppyBlockFunctionMap* sloppy_block_function_map() { |
608 return &sloppy_block_function_map_; | 609 return &sloppy_block_function_map_; |
609 } | 610 } |
610 | 611 |
611 // --------------------------------------------------------------------------- | 612 // --------------------------------------------------------------------------- |
612 // Debugging. | 613 // Debugging. |
613 | 614 |
614 #ifdef DEBUG | 615 #ifdef DEBUG |
615 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 616 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
616 | 617 |
617 // Check that the scope has positions assigned. | 618 // Check that the scope has positions assigned. |
618 void CheckScopePositions(); | 619 void CheckScopePositions(); |
619 #endif | 620 #endif |
620 | 621 |
621 // --------------------------------------------------------------------------- | 622 // --------------------------------------------------------------------------- |
622 // Implementation. | 623 // Implementation. |
623 private: | 624 private: |
624 // Scope tree. | 625 // Scope tree. |
625 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 626 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
626 Scope* inner_scope_; // an inner scope of this scope | 627 Scope* inner_scope_; // an inner scope of this scope |
627 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 628 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
628 | 629 |
629 // The scope type. | |
630 const ScopeType scope_type_; | |
631 // If the scope is a function scope, this is the function kind. | |
632 const FunctionKind function_kind_; | |
633 | |
634 // Debugging support. | 630 // Debugging support. |
631 #ifdef DEBUG | |
635 const AstRawString* scope_name_; | 632 const AstRawString* scope_name_; |
633 #endif | |
636 | 634 |
637 // The variables declared in this scope: | 635 // The variables declared in this scope: |
638 // | 636 // |
639 // All user-declared variables (incl. parameters). For script scopes | 637 // All user-declared variables (incl. parameters). For script scopes |
640 // variables may be implicitly 'declared' by being used (possibly in | 638 // variables may be implicitly 'declared' by being used (possibly in |
641 // an inner scope) with no intervening with statements or eval calls. | 639 // an inner scope) with no intervening with statements or eval calls. |
642 VariableMap variables_; | 640 VariableMap variables_; |
643 // Compiler-allocated (user-invisible) temporaries. Due to the implementation | 641 // Compiler-allocated (user-invisible) temporaries. Due to the implementation |
644 // of RemoveTemporary(), may contain nulls, which must be skipped-over during | 642 // of RemoveTemporary(), may contain nulls, which must be skipped-over during |
645 // allocation and printing. | 643 // allocation and printing. |
(...skipping 16 matching lines...) Expand all Loading... | |
662 // Convenience variable; function scopes only. | 660 // Convenience variable; function scopes only. |
663 Variable* arguments_; | 661 Variable* arguments_; |
664 // Convenience variable; Subclass constructor only | 662 // Convenience variable; Subclass constructor only |
665 Variable* this_function_; | 663 Variable* this_function_; |
666 // Module descriptor; module scopes only. | 664 // Module descriptor; module scopes only. |
667 ModuleDescriptor* module_descriptor_; | 665 ModuleDescriptor* module_descriptor_; |
668 | 666 |
669 // Map of function names to lists of functions defined in sloppy blocks | 667 // Map of function names to lists of functions defined in sloppy blocks |
670 SloppyBlockFunctionMap sloppy_block_function_map_; | 668 SloppyBlockFunctionMap sloppy_block_function_map_; |
671 | 669 |
670 // The scope type. | |
671 const ScopeType scope_type_; | |
672 // If the scope is a function scope, this is the function kind. | |
673 const FunctionKind function_kind_; | |
674 | |
672 // Scope-specific information computed during parsing. | 675 // Scope-specific information computed during parsing. |
673 // | 676 // |
677 // The language mode of this scope. | |
678 STATIC_ASSERT(LANGUAGE_END == 3); | |
679 LanguageMode language_mode_ : 2; | |
674 // This scope is inside a 'with' of some outer scope. | 680 // This scope is inside a 'with' of some outer scope. |
675 bool scope_inside_with_; | 681 bool scope_inside_with_ : 1; |
676 // This scope or a nested catch scope or with scope contain an 'eval' call. At | 682 // This scope or a nested catch scope or with scope contain an 'eval' call. At |
677 // the 'eval' call site this scope is the declaration scope. | 683 // the 'eval' call site this scope is the declaration scope. |
678 bool scope_calls_eval_; | 684 bool scope_calls_eval_ : 1; |
679 // This scope uses "super" property ('super.foo'). | 685 // This scope uses "super" property ('super.foo'). |
680 bool scope_uses_super_property_; | 686 bool scope_uses_super_property_ : 1; |
681 // This scope has a parameter called "arguments". | 687 // This scope has a parameter called "arguments". |
682 bool has_arguments_parameter_; | 688 bool has_arguments_parameter_ : 1; |
683 // This scope contains an "use asm" annotation. | 689 // This scope contains an "use asm" annotation. |
684 bool asm_module_; | 690 bool asm_module_ : 1; |
685 // This scope's outer context is an asm module. | 691 // This scope's outer context is an asm module. |
686 bool asm_function_; | 692 bool asm_function_ : 1; |
687 // This scope's declarations might not be executed in order (e.g., switch). | 693 // This scope's declarations might not be executed in order (e.g., switch). |
688 bool scope_nonlinear_; | 694 bool scope_nonlinear_ : 1; |
689 // The language mode of this scope. | 695 bool is_hidden_ : 1; |
690 LanguageMode language_mode_; | 696 bool has_simple_parameters_ : 1; |
697 | |
698 // Computed via PropagateScopeInfo. | |
699 bool outer_scope_calls_sloppy_eval_ : 1; | |
700 bool inner_scope_calls_eval_ : 1; | |
701 bool force_eager_compilation_ : 1; | |
702 bool force_context_allocation_ : 1; | |
703 | |
704 // True if it doesn't need scope resolution (e.g., if the scope was | |
705 // constructed based on a serialized scope info or a catch context). | |
706 bool already_resolved_ : 1; | |
707 bool already_resolved() { return already_resolved_; } | |
708 | |
709 // True if it holds 'var' declarations. | |
710 bool is_declaration_scope_ : 1; | |
711 | |
691 // Source positions. | 712 // Source positions. |
692 int start_position_; | 713 int start_position_; |
693 int end_position_; | 714 int end_position_; |
694 bool is_hidden_; | |
695 | |
696 // Computed via PropagateScopeInfo. | |
697 bool outer_scope_calls_sloppy_eval_; | |
698 bool inner_scope_calls_eval_; | |
699 bool force_eager_compilation_; | |
700 bool force_context_allocation_; | |
701 | |
702 // True if it doesn't need scope resolution (e.g., if the scope was | |
703 // constructed based on a serialized scope info or a catch context). | |
704 bool already_resolved_; | |
705 | |
706 // True if it holds 'var' declarations. | |
707 bool is_declaration_scope_; | |
708 | |
709 // Computed as variables are declared. | |
710 int num_var_; | |
711 | 715 |
712 // Computed via AllocateVariables; function, block and catch scopes only. | 716 // Computed via AllocateVariables; function, block and catch scopes only. |
713 int num_stack_slots_; | 717 int num_stack_slots_; |
714 int num_heap_slots_; | 718 int num_heap_slots_; |
715 int num_global_slots_; | 719 int num_global_slots_; |
716 | 720 |
717 // Info about the parameter list of a function. | 721 // Info about the parameter list of a function. |
718 int arity_; | 722 int arity_; |
719 bool has_simple_parameters_; | 723 int rest_index_; |
720 Variable* rest_parameter_; | 724 Variable* rest_parameter_; |
721 int rest_index_; | |
722 | 725 |
723 // Serialized scope info support. | 726 // Serialized scope info support. |
724 Handle<ScopeInfo> scope_info_; | 727 Handle<ScopeInfo> scope_info_; |
725 bool already_resolved() { return already_resolved_; } | |
726 | 728 |
727 // Create a non-local variable with a given name. | 729 // Create a non-local variable with a given name. |
728 // These variables are looked up dynamically at runtime. | 730 // These variables are looked up dynamically at runtime. |
729 Variable* NonLocal(const AstRawString* name, VariableMode mode); | 731 Variable* NonLocal(const AstRawString* name, VariableMode mode); |
730 | 732 |
731 // Variable resolution. | 733 // Variable resolution. |
732 // Possible results of a recursive variable lookup telling if and how a | 734 // Possible results of a recursive variable lookup telling if and how a |
733 // variable is bound. These are returned in the output parameter *binding_kind | 735 // variable is bound. These are returned in the output parameter *binding_kind |
734 // of the LookupRecursive function. | 736 // of the LookupRecursive function. |
735 enum BindingKind { | 737 enum BindingKind { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
851 | 853 |
852 void SetDefaults(); | 854 void SetDefaults(); |
853 | 855 |
854 PendingCompilationErrorHandler pending_error_handler_; | 856 PendingCompilationErrorHandler pending_error_handler_; |
855 }; | 857 }; |
856 | 858 |
857 } // namespace internal | 859 } // namespace internal |
858 } // namespace v8 | 860 } // namespace v8 |
859 | 861 |
860 #endif // V8_AST_SCOPES_H_ | 862 #endif // V8_AST_SCOPES_H_ |
OLD | NEW |