Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/scopes.h

Issue 231073002: WIP: Parser: delay string internalization. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: internalizing better Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_SCOPES_H_ 5 #ifndef V8_SCOPES_H_
6 #define V8_SCOPES_H_ 6 #define V8_SCOPES_H_
7 7
8 #include "ast.h" 8 #include "ast.h"
9 #include "zone.h" 9 #include "zone.h"
10 10
11 namespace v8 { 11 namespace v8 {
12 namespace internal { 12 namespace internal {
13 13
14 class CompilationInfo; 14 class CompilationInfo;
15 15
16 16
17 // A hash map to support fast variable declaration and lookup. 17 // A hash map to support fast variable declaration and lookup.
18 class VariableMap: public ZoneHashMap { 18 class VariableMap: public ZoneHashMap {
19 public: 19 public:
20 explicit VariableMap(Zone* zone); 20 explicit VariableMap(Zone* zone);
21 21
22 virtual ~VariableMap(); 22 virtual ~VariableMap();
23 23
24 Variable* Declare(Scope* scope, 24 Variable* Declare(Scope* scope,
25 Handle<String> name, 25 ParserSymbolTable::Symbol* name,
26 VariableMode mode, 26 VariableMode mode,
27 bool is_valid_lhs, 27 bool is_valid_lhs,
28 Variable::Kind kind, 28 Variable::Kind kind,
29 InitializationFlag initialization_flag, 29 InitializationFlag initialization_flag,
30 Interface* interface = Interface::NewValue()); 30 Interface* interface = Interface::NewValue());
31 31
32 Variable* Lookup(Handle<String> name); 32 Variable* Lookup(ParserSymbolTable::Symbol* name);
33
34 void Add(ParserSymbolTable::Symbol* name, Variable* variable);
33 35
34 Zone* zone() const { return zone_; } 36 Zone* zone() const { return zone_; }
35 37
36 private: 38 private:
37 Zone* zone_; 39 Zone* zone_;
38 }; 40 };
39 41
40 42
41 // The dynamic scope part holds hash maps for the variables that will 43 // The dynamic scope part holds hash maps for the variables that will
42 // be looked up dynamically from within eval and with scopes. The objects 44 // be looked up dynamically from within eval and with scopes. The objects
(...skipping 21 matching lines...) Expand all
64 // to a JavaScript variable (including global properties) is represented by a 66 // to a JavaScript variable (including global properties) is represented by a
65 // VariableProxy node. Immediately after AST construction and before variable 67 // VariableProxy node. Immediately after AST construction and before variable
66 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a 68 // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a
67 // corresponding variable (though some are bound during parse time). Variable 69 // corresponding variable (though some are bound during parse time). Variable
68 // allocation binds each unresolved VariableProxy to one Variable and assigns 70 // allocation binds each unresolved VariableProxy to one Variable and assigns
69 // a location. Note that many VariableProxy nodes may refer to the same Java- 71 // a location. Note that many VariableProxy nodes may refer to the same Java-
70 // Script variable. 72 // Script variable.
71 73
72 class Scope: public ZoneObject { 74 class Scope: public ZoneObject {
73 public: 75 public:
76 enum HeapMode {
rossberg 2014/05/08 12:52:08 Where is this used?
marja 2014/06/03 08:48:20 Nowhere :) -> removed
77 DONT_USE_HEAP,
78 USE_HEAP
79 };
80
81
74 // --------------------------------------------------------------------------- 82 // ---------------------------------------------------------------------------
75 // Construction 83 // Construction
76 84
77 Scope(Scope* outer_scope, ScopeType scope_type, Zone* zone); 85 Scope(Scope* outer_scope, ScopeType scope_type,
86 ParserSymbolTable* symbol_table, Zone* zone);
78 87
79 // Compute top scope and allocate variables. For lazy compilation the top 88 // Compute top scope and allocate variables. For lazy compilation the top
80 // scope only contains the single lazily compiled function, so this 89 // scope only contains the single lazily compiled function, so this
81 // doesn't re-allocate variables repeatedly. 90 // doesn't re-allocate variables repeatedly.
82 static bool Analyze(CompilationInfo* info); 91 static bool Analyze(CompilationInfo* info);
83 92
84 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope, 93 static Scope* DeserializeScopeChain(Context* context, Scope* global_scope,
85 Zone* zone); 94 Zone* zone);
86 95
87 // The scope name is only used for printing/debugging. 96 // The scope name is only used for printing/debugging.
88 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } 97 void SetScopeName(ParserSymbolTable::Symbol* scope_name) {
98 scope_name_ = scope_name;
99 }
89 100
90 void Initialize(); 101 void Initialize();
91 102
92 // Checks if the block scope is redundant, i.e. it does not contain any 103 // Checks if the block scope is redundant, i.e. it does not contain any
93 // block scoped declarations. In that case it is removed from the scope 104 // block scoped declarations. In that case it is removed from the scope
94 // tree and its children are reparented. 105 // tree and its children are reparented.
95 Scope* FinalizeBlockScope(); 106 Scope* FinalizeBlockScope();
96 107
97 Zone* zone() const { return zone_; } 108 Zone* zone() const { return zone_; }
98 109
99 // --------------------------------------------------------------------------- 110 // ---------------------------------------------------------------------------
100 // Declarations 111 // Declarations
101 112
102 // Lookup a variable in this scope. Returns the variable or NULL if not found. 113 // Lookup a variable in this scope. Returns the variable or NULL if not found.
103 Variable* LocalLookup(Handle<String> name); 114 Variable* LocalLookup(ParserSymbolTable::Symbol* name);
104 115
105 // This lookup corresponds to a lookup in the "intermediate" scope sitting 116 // This lookup corresponds to a lookup in the "intermediate" scope sitting
106 // between this scope and the outer scope. (ECMA-262, 3rd., requires that 117 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
107 // the name of named function literal is kept in an intermediate scope 118 // the name of named function literal is kept in an intermediate scope
108 // in between this scope and the next outer scope.) 119 // in between this scope and the next outer scope.)
109 Variable* LookupFunctionVar(Handle<String> name, 120 Variable* LookupFunctionVar(ParserSymbolTable::Symbol* name,
110 AstNodeFactory<AstNullVisitor>* factory); 121 AstNodeFactory<AstNullVisitor>* factory);
111 122
112 // Lookup a variable in this scope or outer scopes. 123 // Lookup a variable in this scope or outer scopes.
113 // Returns the variable or NULL if not found. 124 // Returns the variable or NULL if not found.
114 Variable* Lookup(Handle<String> name); 125 Variable* Lookup(ParserSymbolTable::Symbol* name);
115 126
116 // Declare the function variable for a function literal. This variable 127 // Declare the function variable for a function literal. This variable
117 // is in an intermediate scope between this function scope and the the 128 // is in an intermediate scope between this function scope and the the
118 // outer scope. Only possible for function scopes; at most one variable. 129 // outer scope. Only possible for function scopes; at most one variable.
119 void DeclareFunctionVar(VariableDeclaration* declaration) { 130 void DeclareFunctionVar(VariableDeclaration* declaration) {
120 ASSERT(is_function_scope()); 131 ASSERT(is_function_scope());
121 function_ = declaration; 132 function_ = declaration;
122 } 133 }
123 134
124 // Declare a parameter in this scope. When there are duplicated 135 // Declare a parameter in this scope. When there are duplicated
125 // parameters the rightmost one 'wins'. However, the implementation 136 // parameters the rightmost one 'wins'. However, the implementation
126 // expects all parameters to be declared and from left to right. 137 // expects all parameters to be declared and from left to right.
127 void DeclareParameter(Handle<String> name, VariableMode mode); 138 void DeclareParameter(ParserSymbolTable::Symbol* name, VariableMode mode);
128 139
129 // Declare a local variable in this scope. If the variable has been 140 // Declare a local variable in this scope. If the variable has been
130 // declared before, the previously declared variable is returned. 141 // declared before, the previously declared variable is returned.
131 Variable* DeclareLocal(Handle<String> name, 142 Variable* DeclareLocal(Handle<String> name,
132 VariableMode mode, 143 VariableMode mode,
133 InitializationFlag init_flag, 144 InitializationFlag init_flag,
134 Interface* interface = Interface::NewValue()); 145 Interface* interface = Interface::NewValue());
135 146
147 Variable* DeclareLocal(ParserSymbolTable::Symbol* name,
148 VariableMode mode,
149 InitializationFlag init_flag,
150 Interface* interface = Interface::NewValue());
151
136 // Declare an implicit global variable in this scope which must be a 152 // Declare an implicit global variable in this scope which must be a
137 // global scope. The variable was introduced (possibly from an inner 153 // global scope. The variable was introduced (possibly from an inner
138 // scope) by a reference to an unresolved variable with no intervening 154 // scope) by a reference to an unresolved variable with no intervening
139 // with statements or eval calls. 155 // with statements or eval calls.
140 Variable* DeclareDynamicGlobal(Handle<String> name); 156 Variable* DeclareDynamicGlobal(ParserSymbolTable::Symbol* name);
141 157
142 // Create a new unresolved variable. 158 // Create a new unresolved variable.
143 template<class Visitor> 159 template<class Visitor>
144 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, 160 VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory,
145 Handle<String> name, 161 ParserSymbolTable::Symbol* name,
146 Interface* interface = Interface::NewValue(), 162 Interface* interface = Interface::NewValue(),
147 int position = RelocInfo::kNoPosition) { 163 int position = RelocInfo::kNoPosition) {
148 // Note that we must not share the unresolved variables with 164 // Note that we must not share the unresolved variables with
149 // the same name because they may be removed selectively via 165 // the same name because they may be removed selectively via
150 // RemoveUnresolved(). 166 // RemoveUnresolved().
151 ASSERT(!already_resolved()); 167 ASSERT(!already_resolved());
152 VariableProxy* proxy = 168 VariableProxy* proxy =
153 factory->NewVariableProxy(name, false, interface, position); 169 factory->NewVariableProxy(name, false, interface, position);
154 unresolved_.Add(proxy, zone_); 170 unresolved_.Add(proxy, zone_);
155 return proxy; 171 return proxy;
156 } 172 }
157 173
158 // Remove a unresolved variable. During parsing, an unresolved variable 174 // Remove a unresolved variable. During parsing, an unresolved variable
159 // may have been added optimistically, but then only the variable name 175 // may have been added optimistically, but then only the variable name
160 // was used (typically for labels). If the variable was not declared, the 176 // was used (typically for labels). If the variable was not declared, the
161 // addition introduced a new unresolved variable which may end up being 177 // addition introduced a new unresolved variable which may end up being
162 // allocated globally as a "ghost" variable. RemoveUnresolved removes 178 // allocated globally as a "ghost" variable. RemoveUnresolved removes
163 // such a variable again if it was added; otherwise this is a no-op. 179 // such a variable again if it was added; otherwise this is a no-op.
164 void RemoveUnresolved(VariableProxy* var); 180 void RemoveUnresolved(VariableProxy* var);
165 181
166 // Creates a new internal variable in this scope. The name is only used 182 // Creates a new internal variable in this scope. The name is only used
167 // for printing and cannot be used to find the variable. In particular, 183 // for printing and cannot be used to find the variable. In particular,
168 // the only way to get hold of the temporary is by keeping the Variable* 184 // the only way to get hold of the temporary is by keeping the Variable*
169 // around. 185 // around.
170 Variable* NewInternal(Handle<String> name); 186 Variable* NewInternal(ParserSymbolTable::Symbol* name);
171 187
172 // Creates a new temporary variable in this scope. The name is only used 188 // Creates a new temporary variable in this scope. The name is only used
173 // for printing and cannot be used to find the variable. In particular, 189 // for printing and cannot be used to find the variable. In particular,
174 // the only way to get hold of the temporary is by keeping the Variable* 190 // the only way to get hold of the temporary is by keeping the Variable*
175 // around. The name should not clash with a legitimate variable names. 191 // around. The name should not clash with a legitimate variable names.
176 Variable* NewTemporary(Handle<String> name); 192 Variable* NewTemporary(ParserSymbolTable::Symbol* name);
177 193
178 // Adds the specific declaration node to the list of declarations in 194 // Adds the specific declaration node to the list of declarations in
179 // this scope. The declarations are processed as part of entering 195 // this scope. The declarations are processed as part of entering
180 // the scope; see codegen.cc:ProcessDeclarations. 196 // the scope; see codegen.cc:ProcessDeclarations.
181 void AddDeclaration(Declaration* declaration); 197 void AddDeclaration(Declaration* declaration);
182 198
183 // --------------------------------------------------------------------------- 199 // ---------------------------------------------------------------------------
184 // Illegal redeclaration support. 200 // Illegal redeclaration support.
185 201
186 // Set an expression node that will be executed when the scope is 202 // Set an expression node that will be executed when the scope is
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 399
384 // Get the chain of nested scopes within this scope for the source statement 400 // Get the chain of nested scopes within this scope for the source statement
385 // position. The scopes will be added to the list from the outermost scope to 401 // position. The scopes will be added to the list from the outermost scope to
386 // the innermost scope. Only nested block, catch or with scopes are tracked 402 // the innermost scope. Only nested block, catch or with scopes are tracked
387 // and will be returned, but no inner function scopes. 403 // and will be returned, but no inner function scopes.
388 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, 404 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain,
389 int statement_position); 405 int statement_position);
390 406
391 // --------------------------------------------------------------------------- 407 // ---------------------------------------------------------------------------
392 // Strict mode support. 408 // Strict mode support.
393 bool IsDeclared(Handle<String> name) { 409 bool IsDeclared(ParserSymbolTable::Symbol* name) {
394 // During formal parameter list parsing the scope only contains 410 // During formal parameter list parsing the scope only contains
395 // two variables inserted at initialization: "this" and "arguments". 411 // two variables inserted at initialization: "this" and "arguments".
396 // "this" is an invalid parameter name and "arguments" is invalid parameter 412 // "this" is an invalid parameter name and "arguments" is invalid parameter
397 // name in strict mode. Therefore looking up with the map which includes 413 // name in strict mode. Therefore looking up with the map which includes
398 // "this" and "arguments" in addition to all formal parameters is safe. 414 // "this" and "arguments" in addition to all formal parameters is safe.
399 return variables_.Lookup(name) != NULL; 415 return variables_.Lookup(name) != NULL;
400 } 416 }
401 417
418 Isolate* isolate() const { return isolate_; }
419
402 // --------------------------------------------------------------------------- 420 // ---------------------------------------------------------------------------
403 // Debugging. 421 // Debugging.
404 422
405 #ifdef DEBUG 423 #ifdef DEBUG
406 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively 424 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively
407 #endif 425 #endif
408 426
409 // --------------------------------------------------------------------------- 427 // ---------------------------------------------------------------------------
410 // Implementation. 428 // Implementation.
411 protected: 429 protected:
412 friend class ParserFactory; 430 friend class ParserFactory;
413 431
414 Isolate* const isolate_; 432 Isolate* const isolate_;
415 433
416 // Scope tree. 434 // Scope tree.
417 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL 435 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL
418 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes 436 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes
419 437
420 // The scope type. 438 // The scope type.
421 ScopeType scope_type_; 439 ScopeType scope_type_;
422 440
423 // Debugging support. 441 // Debugging support.
424 Handle<String> scope_name_; 442 ParserSymbolTable::Symbol* scope_name_;
425 443
426 // The variables declared in this scope: 444 // The variables declared in this scope:
427 // 445 //
428 // All user-declared variables (incl. parameters). For global scopes 446 // All user-declared variables (incl. parameters). For global scopes
429 // variables may be implicitly 'declared' by being used (possibly in 447 // variables may be implicitly 'declared' by being used (possibly in
430 // an inner scope) with no intervening with statements or eval calls. 448 // an inner scope) with no intervening with statements or eval calls.
431 VariableMap variables_; 449 VariableMap variables_;
432 // Compiler-allocated (user-invisible) internals. 450 // Compiler-allocated (user-invisible) internals.
433 ZoneList<Variable*> internals_; 451 ZoneList<Variable*> internals_;
434 // Compiler-allocated (user-invisible) temporaries. 452 // Compiler-allocated (user-invisible) temporaries.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 508
491 // For module scopes, the host scope's internal variable binding this module. 509 // For module scopes, the host scope's internal variable binding this module.
492 Variable* module_var_; 510 Variable* module_var_;
493 511
494 // Serialized scope info support. 512 // Serialized scope info support.
495 Handle<ScopeInfo> scope_info_; 513 Handle<ScopeInfo> scope_info_;
496 bool already_resolved() { return already_resolved_; } 514 bool already_resolved() { return already_resolved_; }
497 515
498 // Create a non-local variable with a given name. 516 // Create a non-local variable with a given name.
499 // These variables are looked up dynamically at runtime. 517 // These variables are looked up dynamically at runtime.
500 Variable* NonLocal(Handle<String> name, VariableMode mode); 518 Variable* NonLocal(ParserSymbolTable::Symbol* name, VariableMode mode);
501 519
502 // Variable resolution. 520 // Variable resolution.
503 // Possible results of a recursive variable lookup telling if and how a 521 // Possible results of a recursive variable lookup telling if and how a
504 // variable is bound. These are returned in the output parameter *binding_kind 522 // variable is bound. These are returned in the output parameter *binding_kind
505 // of the LookupRecursive function. 523 // of the LookupRecursive function.
506 enum BindingKind { 524 enum BindingKind {
507 // The variable reference could be statically resolved to a variable binding 525 // The variable reference could be statically resolved to a variable binding
508 // which is returned. There is no 'with' statement between the reference and 526 // which is returned. There is no 'with' statement between the reference and
509 // the binding and no scope between the reference scope (inclusive) and 527 // the binding and no scope between the reference scope (inclusive) and
510 // binding scope (exclusive) makes a sloppy 'eval' call. 528 // binding scope (exclusive) makes a sloppy 'eval' call.
(...skipping 30 matching lines...) Expand all
541 // The variable potentially references a property of the 'with' object. 559 // The variable potentially references a property of the 'with' object.
542 // * The code is being executed as part of a call to 'eval' and the calling 560 // * The code is being executed as part of a call to 'eval' and the calling
543 // context chain contains either a variable binding for the name or it 561 // context chain contains either a variable binding for the name or it
544 // contains a 'with' context. 562 // contains a 'with' context.
545 DYNAMIC_LOOKUP 563 DYNAMIC_LOOKUP
546 }; 564 };
547 565
548 // Lookup a variable reference given by name recursively starting with this 566 // Lookup a variable reference given by name recursively starting with this
549 // scope. If the code is executed because of a call to 'eval', the context 567 // scope. If the code is executed because of a call to 'eval', the context
550 // parameter should be set to the calling context of 'eval'. 568 // parameter should be set to the calling context of 'eval'.
551 Variable* LookupRecursive(Handle<String> name, 569 Variable* LookupRecursive(ParserSymbolTable::Symbol* name,
552 BindingKind* binding_kind, 570 BindingKind* binding_kind,
553 AstNodeFactory<AstNullVisitor>* factory); 571 AstNodeFactory<AstNullVisitor>* factory);
554 MUST_USE_RESULT 572 MUST_USE_RESULT
555 bool ResolveVariable(CompilationInfo* info, 573 bool ResolveVariable(CompilationInfo* info,
556 VariableProxy* proxy, 574 VariableProxy* proxy,
557 AstNodeFactory<AstNullVisitor>* factory); 575 AstNodeFactory<AstNullVisitor>* factory);
558 MUST_USE_RESULT 576 MUST_USE_RESULT
559 bool ResolveVariablesRecursively(CompilationInfo* info, 577 bool ResolveVariablesRecursively(CompilationInfo* info,
560 AstNodeFactory<AstNullVisitor>* factory); 578 AstNodeFactory<AstNullVisitor>* factory);
561 579
(...skipping 23 matching lines...) Expand all
585 // In the case of code compiled and run using 'eval', the context 603 // In the case of code compiled and run using 'eval', the context
586 // parameter is the context in which eval was called. In all other 604 // parameter is the context in which eval was called. In all other
587 // cases the context parameter is an empty handle. 605 // cases the context parameter is an empty handle.
588 MUST_USE_RESULT 606 MUST_USE_RESULT
589 bool AllocateVariables(CompilationInfo* info, 607 bool AllocateVariables(CompilationInfo* info,
590 AstNodeFactory<AstNullVisitor>* factory); 608 AstNodeFactory<AstNullVisitor>* factory);
591 609
592 private: 610 private:
593 // Construct a scope based on the scope info. 611 // Construct a scope based on the scope info.
594 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, 612 Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info,
595 Zone* zone); 613 ParserSymbolTable* symbol_table, Zone* zone);
596 614
597 // Construct a catch scope with a binding for the name. 615 // Construct a catch scope with a binding for the name.
598 Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); 616 Scope(Scope* inner_scope, ParserSymbolTable::Symbol* catch_variable_name,
617 ParserSymbolTable* symbol_table, Zone* zone);
599 618
600 void AddInnerScope(Scope* inner_scope) { 619 void AddInnerScope(Scope* inner_scope) {
601 if (inner_scope != NULL) { 620 if (inner_scope != NULL) {
602 inner_scopes_.Add(inner_scope, zone_); 621 inner_scopes_.Add(inner_scope, zone_);
603 inner_scope->outer_scope_ = this; 622 inner_scope->outer_scope_ = this;
604 } 623 }
605 } 624 }
606 625
607 void SetDefaults(ScopeType type, 626 void SetDefaults(ScopeType type,
608 Scope* outer_scope, 627 Scope* outer_scope,
609 Handle<ScopeInfo> scope_info); 628 Handle<ScopeInfo> scope_info);
610 629
630 ParserSymbolTable* symbol_table_;
611 Zone* zone_; 631 Zone* zone_;
612 }; 632 };
613 633
614 } } // namespace v8::internal 634 } } // namespace v8::internal
615 635
616 #endif // V8_SCOPES_H_ 636 #endif // V8_SCOPES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698