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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 void Reparent(DeclarationScope* new_parent) const; | 83 void Reparent(DeclarationScope* new_parent) const; |
84 | 84 |
85 private: | 85 private: |
86 Scope* outer_scope_; | 86 Scope* outer_scope_; |
87 Scope* top_inner_scope_; | 87 Scope* top_inner_scope_; |
88 VariableProxy* top_unresolved_; | 88 VariableProxy* top_unresolved_; |
89 int top_local_; | 89 int top_local_; |
90 int top_decl_; | 90 int top_decl_; |
91 }; | 91 }; |
92 | 92 |
93 // Compute top scope and allocate variables. For lazy compilation the top | |
94 // scope only contains the single lazily compiled function, so this | |
95 // doesn't re-allocate variables repeatedly. | |
96 static void Analyze(ParseInfo* info); | |
97 | |
98 enum class DeserializationMode { kDeserializeOffHeap, kKeepScopeInfo }; | 93 enum class DeserializationMode { kDeserializeOffHeap, kKeepScopeInfo }; |
99 | 94 |
100 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 95 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
101 Context* context, | 96 Context* context, |
102 DeclarationScope* script_scope, | 97 DeclarationScope* script_scope, |
103 AstValueFactory* ast_value_factory, | 98 AstValueFactory* ast_value_factory, |
104 DeserializationMode deserialization_mode); | 99 DeserializationMode deserialization_mode); |
105 | 100 |
106 // Checks if the block scope is redundant, i.e. it does not contain any | 101 // Checks if the block scope is redundant, i.e. it does not contain any |
107 // block scoped declarations. In that case it is removed from the scope | 102 // block scoped declarations. In that case it is removed from the scope |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 // function, or eval scope. Same as DeclarationScope(), but skips declaration | 367 // function, or eval scope. Same as DeclarationScope(), but skips declaration |
373 // "block" scopes. Used for differentiating associated function objects (i.e., | 368 // "block" scopes. Used for differentiating associated function objects (i.e., |
374 // the scope for which a function prologue allocates a context) or declaring | 369 // the scope for which a function prologue allocates a context) or declaring |
375 // temporaries. | 370 // temporaries. |
376 DeclarationScope* GetClosureScope(); | 371 DeclarationScope* GetClosureScope(); |
377 | 372 |
378 // Find the first (non-arrow) function or script scope. This is where | 373 // Find the first (non-arrow) function or script scope. This is where |
379 // 'this' is bound, and what determines the function kind. | 374 // 'this' is bound, and what determines the function kind. |
380 DeclarationScope* GetReceiverScope(); | 375 DeclarationScope* GetReceiverScope(); |
381 | 376 |
382 // Creates a scope info if it doesn't already exist. | 377 // Analyze() must have been called once to create the ScopeInfo. |
383 Handle<ScopeInfo> GetScopeInfo(Isolate* isolate); | |
384 | |
385 // GetScopeInfo() must have been called once to create the ScopeInfo. | |
386 Handle<ScopeInfo> scope_info() { | 378 Handle<ScopeInfo> scope_info() { |
387 DCHECK(!scope_info_.is_null()); | 379 DCHECK(!scope_info_.is_null()); |
388 return scope_info_; | 380 return scope_info_; |
389 } | 381 } |
390 | 382 |
391 // --------------------------------------------------------------------------- | 383 // --------------------------------------------------------------------------- |
392 // Strict mode support. | 384 // Strict mode support. |
393 bool IsDeclared(const AstRawString* name) { | 385 bool IsDeclared(const AstRawString* name) { |
394 // During formal parameter list parsing the scope only contains | 386 // During formal parameter list parsing the scope only contains |
395 // two variables inserted at initialization: "this" and "arguments". | 387 // two variables inserted at initialization: "this" and "arguments". |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 VariableMode mode, Variable::Kind kind, | 422 VariableMode mode, Variable::Kind kind, |
431 InitializationFlag initialization_flag, | 423 InitializationFlag initialization_flag, |
432 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned) { | 424 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned) { |
433 bool added; | 425 bool added; |
434 Variable* var = | 426 Variable* var = |
435 variables_.Declare(zone, scope, name, mode, kind, initialization_flag, | 427 variables_.Declare(zone, scope, name, mode, kind, initialization_flag, |
436 maybe_assigned_flag, &added); | 428 maybe_assigned_flag, &added); |
437 if (added) locals_.Add(var, zone); | 429 if (added) locals_.Add(var, zone); |
438 return var; | 430 return var; |
439 } | 431 } |
| 432 |
| 433 // This method should only be invoked on scopes created during parsing (i.e., |
| 434 // not deserialized from a context). Also, since NeedsContext() is only |
| 435 // returning a valid result after variables are resolved, NeedsScopeInfo() |
| 436 // should also be invoked after resolution. |
| 437 bool NeedsScopeInfo() const { |
| 438 DCHECK(!already_resolved_); |
| 439 return NeedsContext() || is_script_scope() || is_function_scope() || |
| 440 is_eval_scope() || is_module_scope(); |
| 441 } |
| 442 |
440 Zone* zone_; | 443 Zone* zone_; |
441 | 444 |
442 // Scope tree. | 445 // Scope tree. |
443 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 446 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
444 Scope* inner_scope_; // an inner scope of this scope | 447 Scope* inner_scope_; // an inner scope of this scope |
445 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 448 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
446 | 449 |
447 // The variables declared in this scope: | 450 // The variables declared in this scope: |
448 // | 451 // |
449 // All user-declared variables (incl. parameters). For script scopes | 452 // All user-declared variables (incl. parameters). For script scopes |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 bool MustAllocateInContext(Variable* var); | 538 bool MustAllocateInContext(Variable* var); |
536 | 539 |
537 // Variable allocation. | 540 // Variable allocation. |
538 void AllocateStackSlot(Variable* var); | 541 void AllocateStackSlot(Variable* var); |
539 void AllocateHeapSlot(Variable* var); | 542 void AllocateHeapSlot(Variable* var); |
540 void AllocateNonParameterLocal(Variable* var); | 543 void AllocateNonParameterLocal(Variable* var); |
541 void AllocateDeclaredGlobal(Variable* var); | 544 void AllocateDeclaredGlobal(Variable* var); |
542 void AllocateNonParameterLocalsAndDeclaredGlobals(); | 545 void AllocateNonParameterLocalsAndDeclaredGlobals(); |
543 void AllocateVariablesRecursively(); | 546 void AllocateVariablesRecursively(); |
544 | 547 |
| 548 void AllocateScopeInfosRecursively(Isolate* isolate, bool for_debugger); |
| 549 |
545 // Construct a scope based on the scope info. | 550 // Construct a scope based on the scope info. |
546 Scope(Zone* zone, ScopeType type, Handle<ScopeInfo> scope_info); | 551 Scope(Zone* zone, ScopeType type, Handle<ScopeInfo> scope_info); |
547 | 552 |
548 // Construct a catch scope with a binding for the name. | 553 // Construct a catch scope with a binding for the name. |
549 Scope(Zone* zone, const AstRawString* catch_variable_name); | 554 Scope(Zone* zone, const AstRawString* catch_variable_name); |
550 | 555 |
551 void AddInnerScope(Scope* inner_scope) { | 556 void AddInnerScope(Scope* inner_scope) { |
552 inner_scope->sibling_ = inner_scope_; | 557 inner_scope->sibling_ = inner_scope_; |
553 inner_scope_ = inner_scope; | 558 inner_scope_ = inner_scope; |
554 inner_scope->outer_scope_ = this; | 559 inner_scope->outer_scope_ = this; |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 | 742 |
738 void DeclareSloppyBlockFunction(const AstRawString* name, | 743 void DeclareSloppyBlockFunction(const AstRawString* name, |
739 SloppyBlockFunctionStatement* statement) { | 744 SloppyBlockFunctionStatement* statement) { |
740 sloppy_block_function_map_.Declare(zone(), name, statement); | 745 sloppy_block_function_map_.Declare(zone(), name, statement); |
741 } | 746 } |
742 | 747 |
743 SloppyBlockFunctionMap* sloppy_block_function_map() { | 748 SloppyBlockFunctionMap* sloppy_block_function_map() { |
744 return &sloppy_block_function_map_; | 749 return &sloppy_block_function_map_; |
745 } | 750 } |
746 | 751 |
747 // Resolve and fill in the allocation information for all variables | 752 // Compute top scope and allocate variables. For lazy compilation the top |
748 // in this scopes. Must be called *after* all scopes have been | 753 // scope only contains the single lazily compiled function, so this |
749 // processed (parsed) to ensure that unresolved variables can be | 754 // doesn't re-allocate variables repeatedly. |
750 // resolved properly. | 755 static void Analyze(ParseInfo* info); |
751 // | 756 |
752 // In the case of code compiled and run using 'eval', the context | 757 // Version used by the debugger that creates extra ScopeInfos. |
753 // parameter is the context in which eval was called. In all other | 758 static void AnalyzeForDebugger(ParseInfo* info); |
754 // cases the context parameter is an empty handle. | |
755 void AllocateVariables(ParseInfo* info); | |
756 | 759 |
757 // To be called during parsing. Do just enough scope analysis that we can | 760 // To be called during parsing. Do just enough scope analysis that we can |
758 // discard the Scope for lazily compiled functions. In particular, this | 761 // discard the Scope for lazily compiled functions. In particular, this |
759 // records variables which cannot be resolved inside the Scope (we don't yet | 762 // records variables which cannot be resolved inside the Scope (we don't yet |
760 // know what they will resolve to since the outer Scopes are incomplete) and | 763 // know what they will resolve to since the outer Scopes are incomplete) and |
761 // migrates them into migrate_to. | 764 // migrates them into migrate_to. |
762 void AnalyzePartially(DeclarationScope* migrate_to, | 765 void AnalyzePartially(DeclarationScope* migrate_to, |
763 AstNodeFactory* ast_node_factory); | 766 AstNodeFactory* ast_node_factory); |
764 | 767 |
765 Handle<StringSet> CollectNonLocals(ParseInfo* info, | 768 Handle<StringSet> CollectNonLocals(ParseInfo* info, |
(...skipping 18 matching lines...) Expand all Loading... |
784 void PrintParameters(); | 787 void PrintParameters(); |
785 #endif | 788 #endif |
786 | 789 |
787 void AllocateLocals(); | 790 void AllocateLocals(); |
788 void AllocateParameterLocals(); | 791 void AllocateParameterLocals(); |
789 void AllocateReceiver(); | 792 void AllocateReceiver(); |
790 | 793 |
791 private: | 794 private: |
792 void AllocateParameter(Variable* var, int index); | 795 void AllocateParameter(Variable* var, int index); |
793 | 796 |
| 797 // Resolve and fill in the allocation information for all variables |
| 798 // in this scopes. Must be called *after* all scopes have been |
| 799 // processed (parsed) to ensure that unresolved variables can be |
| 800 // resolved properly. |
| 801 // |
| 802 // In the case of code compiled and run using 'eval', the context |
| 803 // parameter is the context in which eval was called. In all other |
| 804 // cases the context parameter is an empty handle. |
| 805 void AllocateVariables(ParseInfo* info, bool for_debugger); |
| 806 |
794 void SetDefaults(); | 807 void SetDefaults(); |
795 | 808 |
796 // If the scope is a function scope, this is the function kind. | 809 // If the scope is a function scope, this is the function kind. |
797 const FunctionKind function_kind_; | 810 const FunctionKind function_kind_; |
798 | 811 |
799 bool has_simple_parameters_ : 1; | 812 bool has_simple_parameters_ : 1; |
800 // This scope contains an "use asm" annotation. | 813 // This scope contains an "use asm" annotation. |
801 bool asm_module_ : 1; | 814 bool asm_module_ : 1; |
802 // This scope's outer context is an asm module. | 815 // This scope's outer context is an asm module. |
803 bool asm_function_ : 1; | 816 bool asm_function_ : 1; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 void AllocateModuleVariables(); | 855 void AllocateModuleVariables(); |
843 | 856 |
844 private: | 857 private: |
845 ModuleDescriptor* module_descriptor_; | 858 ModuleDescriptor* module_descriptor_; |
846 }; | 859 }; |
847 | 860 |
848 } // namespace internal | 861 } // namespace internal |
849 } // namespace v8 | 862 } // namespace v8 |
850 | 863 |
851 #endif // V8_AST_SCOPES_H_ | 864 #endif // V8_AST_SCOPES_H_ |
OLD | NEW |