Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_SCOPES_H_ |
| 6 | #define V8_SCOPES_H_ |
| 7 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 8 | #include "src/ast.h" |
| 9 | #include "src/zone.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 14 | class CompilationInfo; |
| 15 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 16 | |
| 17 | // A hash map to support fast variable declaration and lookup. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 18 | class VariableMap: public ZoneHashMap { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 19 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 20 | explicit VariableMap(Zone* zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 21 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 22 | virtual ~VariableMap(); |
| 23 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 24 | Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, |
| 25 | bool is_valid_lhs, Variable::Kind kind, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 26 | InitializationFlag initialization_flag, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 27 | MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 28 | Interface* interface = Interface::NewValue()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | Variable* Lookup(const AstRawString* name); |
| 31 | |
| 32 | Zone* zone() const { return zone_; } |
| 33 | |
| 34 | private: |
| 35 | Zone* zone_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | |
| 39 | // The dynamic scope part holds hash maps for the variables that will |
| 40 | // be looked up dynamically from within eval and with scopes. The objects |
| 41 | // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
| 42 | // and setup time for scopes that don't need them. |
| 43 | class DynamicScopePart : public ZoneObject { |
| 44 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 45 | explicit DynamicScopePart(Zone* zone) { |
| 46 | for (int i = 0; i < 3; i++) |
| 47 | maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); |
| 48 | } |
| 49 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 50 | VariableMap* GetMap(VariableMode mode) { |
| 51 | int index = mode - DYNAMIC; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 52 | DCHECK(index >= 0 && index < 3); |
| 53 | return maps_[index]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | VariableMap *maps_[3]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | |
| 61 | // Global invariants after AST construction: Each reference (i.e. identifier) |
| 62 | // to a JavaScript variable (including global properties) is represented by a |
| 63 | // VariableProxy node. Immediately after AST construction and before variable |
| 64 | // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 65 | // corresponding variable (though some are bound during parse time). Variable |
| 66 | // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 67 | // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 68 | // Script variable. |
| 69 | |
| 70 | class Scope: public ZoneObject { |
| 71 | public: |
| 72 | // --------------------------------------------------------------------------- |
| 73 | // Construction |
| 74 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | Scope(Scope* outer_scope, ScopeType scope_type, |
| 76 | AstValueFactory* value_factory, Zone* zone); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 78 | // Compute top scope and allocate variables. For lazy compilation the top |
| 79 | // scope only contains the single lazily compiled function, so this |
| 80 | // doesn't re-allocate variables repeatedly. |
| 81 | static bool Analyze(CompilationInfo* info); |
| 82 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 83 | static Scope* DeserializeScopeChain(Context* context, Scope* global_scope, |
| 84 | Zone* zone); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 85 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 86 | // The scope name is only used for printing/debugging. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | void SetScopeName(const AstRawString* scope_name) { |
| 88 | scope_name_ = scope_name; |
| 89 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 90 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 91 | void Initialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 92 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 93 | // Checks if the block scope is redundant, i.e. it does not contain any |
| 94 | // block scoped declarations. In that case it is removed from the scope |
| 95 | // tree and its children are reparented. |
| 96 | Scope* FinalizeBlockScope(); |
| 97 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 98 | Zone* zone() const { return zone_; } |
| 99 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | // --------------------------------------------------------------------------- |
| 101 | // Declarations |
| 102 | |
| 103 | // Lookup a variable in this scope. Returns the variable or NULL if not found. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | Variable* LookupLocal(const AstRawString* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 106 | // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 107 | // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 108 | // the name of named function literal is kept in an intermediate scope |
| 109 | // in between this scope and the next outer scope.) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 110 | Variable* LookupFunctionVar(const AstRawString* name, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 111 | AstNodeFactory<AstNullVisitor>* factory); |
| 112 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 113 | // Lookup a variable in this scope or outer scopes. |
| 114 | // Returns the variable or NULL if not found. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | Variable* Lookup(const AstRawString* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 116 | |
| 117 | // Declare the function variable for a function literal. This variable |
| 118 | // is in an intermediate scope between this function scope and the the |
| 119 | // outer scope. Only possible for function scopes; at most one variable. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 120 | void DeclareFunctionVar(VariableDeclaration* declaration) { |
| 121 | DCHECK(is_function_scope()); |
| 122 | function_ = declaration; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 123 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 125 | // Declare a parameter in this scope. When there are duplicated |
| 126 | // parameters the rightmost one 'wins'. However, the implementation |
| 127 | // expects all parameters to be declared and from left to right. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | Variable* DeclareParameter(const AstRawString* name, VariableMode mode); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 129 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 130 | // Declare a local variable in this scope. If the variable has been |
| 131 | // declared before, the previously declared variable is returned. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | Variable* DeclareLocal(const AstRawString* name, VariableMode mode, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 133 | InitializationFlag init_flag, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 134 | MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 135 | Interface* interface = Interface::NewValue()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 136 | |
| 137 | // Declare an implicit global variable in this scope which must be a |
| 138 | // global scope. The variable was introduced (possibly from an inner |
| 139 | // scope) by a reference to an unresolved variable with no intervening |
| 140 | // with statements or eval calls. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 141 | Variable* DeclareDynamicGlobal(const AstRawString* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 142 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | // Create a new unresolved variable. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 144 | template<class Visitor> |
| 145 | VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 146 | const AstRawString* name, |
| 147 | Interface* interface = Interface::NewValue(), |
| 148 | int position = RelocInfo::kNoPosition) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 149 | // Note that we must not share the unresolved variables with |
| 150 | // the same name because they may be removed selectively via |
| 151 | // RemoveUnresolved(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 152 | DCHECK(!already_resolved()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 153 | VariableProxy* proxy = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | factory->NewVariableProxy(name, false, interface, position); |
| 155 | unresolved_.Add(proxy, zone_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 156 | return proxy; |
| 157 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 158 | |
| 159 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 160 | // may have been added optimistically, but then only the variable name |
| 161 | // was used (typically for labels). If the variable was not declared, the |
| 162 | // addition introduced a new unresolved variable which may end up being |
| 163 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 164 | // such a variable again if it was added; otherwise this is a no-op. |
| 165 | void RemoveUnresolved(VariableProxy* var); |
| 166 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 167 | // Creates a new internal variable in this scope. The name is only used |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 168 | // for printing and cannot be used to find the variable. In particular, |
| 169 | // the only way to get hold of the temporary is by keeping the Variable* |
| 170 | // around. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 171 | Variable* NewInternal(const AstRawString* name); |
| 172 | |
| 173 | // Creates a new temporary variable in this scope. The name is only used |
| 174 | // for printing and cannot be used to find the variable. In particular, |
| 175 | // the only way to get hold of the temporary is by keeping the Variable* |
| 176 | // around. The name should not clash with a legitimate variable names. |
| 177 | Variable* NewTemporary(const AstRawString* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 178 | |
| 179 | // Adds the specific declaration node to the list of declarations in |
| 180 | // this scope. The declarations are processed as part of entering |
| 181 | // the scope; see codegen.cc:ProcessDeclarations. |
| 182 | void AddDeclaration(Declaration* declaration); |
| 183 | |
| 184 | // --------------------------------------------------------------------------- |
| 185 | // Illegal redeclaration support. |
| 186 | |
| 187 | // Set an expression node that will be executed when the scope is |
| 188 | // entered. We only keep track of one illegal redeclaration node per |
| 189 | // scope - the first one - so if you try to set it multiple times |
| 190 | // the additional requests will be silently ignored. |
| 191 | void SetIllegalRedeclaration(Expression* expression); |
| 192 | |
| 193 | // Visit the illegal redeclaration expression. Do not call if the |
| 194 | // scope doesn't have an illegal redeclaration node. |
| 195 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
| 196 | |
| 197 | // Check if the scope has (at least) one illegal redeclaration. |
| 198 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 199 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 200 | // For harmony block scoping mode: Check if the scope has conflicting var |
| 201 | // declarations, i.e. a var declaration that has been hoisted from a nested |
| 202 | // scope over a let binding of the same name. |
| 203 | Declaration* CheckConflictingVarDeclarations(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | |
| 205 | // --------------------------------------------------------------------------- |
| 206 | // Scope-specific info. |
| 207 | |
| 208 | // Inform the scope that the corresponding code contains a with statement. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 209 | void RecordWithStatement() { scope_contains_with_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | |
| 211 | // Inform the scope that the corresponding code contains an eval call. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 212 | void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 213 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 214 | // Set the strict mode flag (unless disabled by a global flag). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } |
| 216 | |
| 217 | // Set the ASM module flag. |
| 218 | void SetAsmModule() { asm_module_ = true; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 219 | |
| 220 | // Position in the source where this scope begins and ends. |
| 221 | // |
| 222 | // * For the scope of a with statement |
| 223 | // with (obj) stmt |
| 224 | // start position: start position of first token of 'stmt' |
| 225 | // end position: end position of last token of 'stmt' |
| 226 | // * For the scope of a block |
| 227 | // { stmts } |
| 228 | // start position: start position of '{' |
| 229 | // end position: end position of '}' |
| 230 | // * For the scope of a function literal or decalaration |
| 231 | // function fun(a,b) { stmts } |
| 232 | // start position: start position of '(' |
| 233 | // end position: end position of '}' |
| 234 | // * For the scope of a catch block |
| 235 | // try { stms } catch(e) { stmts } |
| 236 | // start position: start position of '(' |
| 237 | // end position: end position of ')' |
| 238 | // * For the scope of a for-statement |
| 239 | // for (let x ...) stmt |
| 240 | // start position: start position of '(' |
| 241 | // end position: end position of last token of 'stmt' |
| 242 | int start_position() const { return start_position_; } |
| 243 | void set_start_position(int statement_pos) { |
| 244 | start_position_ = statement_pos; |
| 245 | } |
| 246 | int end_position() const { return end_position_; } |
| 247 | void set_end_position(int statement_pos) { |
| 248 | end_position_ = statement_pos; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 249 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | // In some cases we want to force context allocation for a whole scope. |
| 252 | void ForceContextAllocation() { |
| 253 | DCHECK(!already_resolved()); |
| 254 | force_context_allocation_ = true; |
| 255 | } |
| 256 | bool has_forced_context_allocation() const { |
| 257 | return force_context_allocation_; |
| 258 | } |
| 259 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | // --------------------------------------------------------------------------- |
| 261 | // Predicates. |
| 262 | |
| 263 | // Specific scope types. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } |
| 265 | bool is_function_scope() const { return scope_type_ == FUNCTION_SCOPE; } |
| 266 | bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } |
| 267 | bool is_global_scope() const { return scope_type_ == GLOBAL_SCOPE; } |
| 268 | bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } |
| 269 | bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } |
| 270 | bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 271 | bool is_declaration_scope() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | return is_eval_scope() || is_function_scope() || |
| 273 | is_module_scope() || is_global_scope(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 274 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 275 | bool is_strict_eval_scope() const { |
| 276 | return is_eval_scope() && strict_mode_ == STRICT; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 277 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | |
| 279 | // Information about which scopes calls eval. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 280 | bool calls_eval() const { return scope_calls_eval_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 281 | bool calls_sloppy_eval() { |
| 282 | return scope_calls_eval_ && strict_mode_ == SLOPPY; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 283 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 284 | bool outer_scope_calls_sloppy_eval() const { |
| 285 | return outer_scope_calls_sloppy_eval_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 286 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 287 | bool asm_module() const { return asm_module_; } |
| 288 | bool asm_function() const { return asm_function_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 289 | |
| 290 | // Is this scope inside a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 291 | bool inside_with() const { return scope_inside_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 292 | // Does this scope contain a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 293 | bool contains_with() const { return scope_contains_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | // --------------------------------------------------------------------------- |
| 296 | // Accessors. |
| 297 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 298 | // The type of this scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 299 | ScopeType scope_type() const { return scope_type_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 300 | |
| 301 | // The language mode of this scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 302 | StrictMode strict_mode() const { return strict_mode_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 303 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 304 | // The variable corresponding the 'this' value. |
| 305 | Variable* receiver() { return receiver_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 306 | |
| 307 | // The variable holding the function literal for named function |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 308 | // literals, or NULL. Only valid for function scopes. |
| 309 | VariableDeclaration* function() const { |
| 310 | DCHECK(is_function_scope()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 311 | return function_; |
| 312 | } |
| 313 | |
| 314 | // Parameters. The left-most parameter has index 0. |
| 315 | // Only valid for function scopes. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 316 | Variable* parameter(int index) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 317 | DCHECK(is_function_scope()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 318 | return params_[index]; |
| 319 | } |
| 320 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 321 | int num_parameters() const { return params_.length(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | |
| 323 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 324 | Variable* arguments() const { return arguments_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 325 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 326 | // Declarations list. |
| 327 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 328 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 329 | // Inner scope list. |
| 330 | ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } |
| 331 | |
| 332 | // The scope immediately surrounding this scope, or NULL. |
| 333 | Scope* outer_scope() const { return outer_scope_; } |
| 334 | |
| 335 | // The interface as inferred so far; only for module scopes. |
| 336 | Interface* interface() const { return interface_; } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 337 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 338 | // --------------------------------------------------------------------------- |
| 339 | // Variable allocation. |
| 340 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 341 | // Collect stack and context allocated local variables in this scope. Note |
| 342 | // that the function variable - if present - is not collected and should be |
| 343 | // handled separately. |
| 344 | void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
| 345 | ZoneList<Variable*>* context_locals); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 346 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 347 | // Current number of var or const locals. |
| 348 | int num_var_or_const() { return num_var_or_const_; } |
| 349 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | // Result of variable allocation. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 351 | int num_stack_slots() const { return num_stack_slots_; } |
| 352 | int num_heap_slots() const { return num_heap_slots_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 353 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 354 | int StackLocalCount() const; |
| 355 | int ContextLocalCount() const; |
| 356 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 357 | // For global scopes, the number of module literals (including nested ones). |
| 358 | int num_modules() const { return num_modules_; } |
| 359 | |
| 360 | // For module scopes, the host scope's internal variable binding this module. |
| 361 | Variable* module_var() const { return module_var_; } |
| 362 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 363 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 364 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 365 | |
| 366 | // Determine if we can use lazy compilation for this scope. |
| 367 | bool AllowsLazyCompilation() const; |
| 368 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 369 | // Determine if we can use lazy compilation for this scope without a context. |
| 370 | bool AllowsLazyCompilationWithoutContext() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 371 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 372 | // True if the outer context of this scope is always the native context. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 373 | bool HasTrivialOuterContext() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 375 | // True if the outer context allows lazy compilation of this scope. |
| 376 | bool HasLazyCompilableOuterContext() const; |
Ben Murdoch | 4768e9d | 2012-05-24 11:17:13 +0100 | [diff] [blame] | 377 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | // The number of contexts between this and scope; zero if this == scope. |
| 379 | int ContextChainLength(Scope* scope); |
| 380 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 381 | // Find the innermost global scope. |
| 382 | Scope* GlobalScope(); |
| 383 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 384 | // Find the first function, global, or eval scope. This is the scope |
| 385 | // where var declarations will be hoisted to in the implementation. |
| 386 | Scope* DeclarationScope(); |
| 387 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 388 | Handle<ScopeInfo> GetScopeInfo(); |
| 389 | |
| 390 | // Get the chain of nested scopes within this scope for the source statement |
| 391 | // position. The scopes will be added to the list from the outermost scope to |
| 392 | // the innermost scope. Only nested block, catch or with scopes are tracked |
| 393 | // and will be returned, but no inner function scopes. |
| 394 | void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, |
| 395 | int statement_position); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 396 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 397 | // --------------------------------------------------------------------------- |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 398 | // Strict mode support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 399 | bool IsDeclared(const AstRawString* name) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 400 | // During formal parameter list parsing the scope only contains |
| 401 | // two variables inserted at initialization: "this" and "arguments". |
| 402 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 403 | // name in strict mode. Therefore looking up with the map which includes |
| 404 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 405 | return variables_.Lookup(name) != NULL; |
| 406 | } |
| 407 | |
| 408 | // --------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 409 | // Debugging. |
| 410 | |
| 411 | #ifdef DEBUG |
| 412 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 413 | #endif |
| 414 | |
| 415 | // --------------------------------------------------------------------------- |
| 416 | // Implementation. |
| 417 | protected: |
| 418 | friend class ParserFactory; |
| 419 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 420 | Isolate* const isolate_; |
| 421 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 422 | // Scope tree. |
| 423 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 424 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 425 | |
| 426 | // The scope type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 427 | ScopeType scope_type_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | |
| 429 | // Debugging support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 430 | const AstRawString* scope_name_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 431 | |
| 432 | // The variables declared in this scope: |
| 433 | // |
| 434 | // All user-declared variables (incl. parameters). For global scopes |
| 435 | // variables may be implicitly 'declared' by being used (possibly in |
| 436 | // an inner scope) with no intervening with statements or eval calls. |
| 437 | VariableMap variables_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 438 | // Compiler-allocated (user-invisible) internals. |
| 439 | ZoneList<Variable*> internals_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | // Compiler-allocated (user-invisible) temporaries. |
| 441 | ZoneList<Variable*> temps_; |
| 442 | // Parameter list in source order. |
| 443 | ZoneList<Variable*> params_; |
| 444 | // Variables that must be looked up dynamically. |
| 445 | DynamicScopePart* dynamics_; |
| 446 | // Unresolved variables referred to from this scope. |
| 447 | ZoneList<VariableProxy*> unresolved_; |
| 448 | // Declarations. |
| 449 | ZoneList<Declaration*> decls_; |
| 450 | // Convenience variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 451 | Variable* receiver_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 452 | // Function variable, if any; function scopes only. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 453 | VariableDeclaration* function_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | // Convenience variable; function scopes only. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 455 | Variable* arguments_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 456 | // Interface; module scopes only. |
| 457 | Interface* interface_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 458 | |
| 459 | // Illegal redeclaration. |
| 460 | Expression* illegal_redecl_; |
| 461 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 462 | // Scope-specific information computed during parsing. |
| 463 | // |
| 464 | // This scope is inside a 'with' of some outer scope. |
| 465 | bool scope_inside_with_; |
| 466 | // This scope contains a 'with' statement. |
| 467 | bool scope_contains_with_; |
| 468 | // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 469 | // the 'eval' call site this scope is the declaration scope. |
| 470 | bool scope_calls_eval_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | // This scope contains an "use asm" annotation. |
| 472 | bool asm_module_; |
| 473 | // This scope's outer context is an asm module. |
| 474 | bool asm_function_; |
| 475 | // The strict mode of this scope. |
| 476 | StrictMode strict_mode_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 477 | // Source positions. |
| 478 | int start_position_; |
| 479 | int end_position_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 480 | |
| 481 | // Computed via PropagateScopeInfo. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 482 | bool outer_scope_calls_sloppy_eval_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 483 | bool inner_scope_calls_eval_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 484 | bool force_eager_compilation_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 485 | bool force_context_allocation_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 487 | // True if it doesn't need scope resolution (e.g., if the scope was |
| 488 | // constructed based on a serialized scope info or a catch context). |
| 489 | bool already_resolved_; |
| 490 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 491 | // Computed as variables are declared. |
| 492 | int num_var_or_const_; |
| 493 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 494 | // Computed via AllocateVariables; function, block and catch scopes only. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | int num_stack_slots_; |
| 496 | int num_heap_slots_; |
| 497 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 498 | // The number of modules (including nested ones). |
| 499 | int num_modules_; |
| 500 | |
| 501 | // For module scopes, the host scope's internal variable binding this module. |
| 502 | Variable* module_var_; |
| 503 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 504 | // Serialized scope info support. |
| 505 | Handle<ScopeInfo> scope_info_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 506 | bool already_resolved() { return already_resolved_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 507 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 508 | // Create a non-local variable with a given name. |
| 509 | // These variables are looked up dynamically at runtime. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 510 | Variable* NonLocal(const AstRawString* name, VariableMode mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 511 | |
| 512 | // Variable resolution. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 513 | // Possible results of a recursive variable lookup telling if and how a |
| 514 | // variable is bound. These are returned in the output parameter *binding_kind |
| 515 | // of the LookupRecursive function. |
| 516 | enum BindingKind { |
| 517 | // The variable reference could be statically resolved to a variable binding |
| 518 | // which is returned. There is no 'with' statement between the reference and |
| 519 | // the binding and no scope between the reference scope (inclusive) and |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 520 | // binding scope (exclusive) makes a sloppy 'eval' call. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 521 | BOUND, |
| 522 | |
| 523 | // The variable reference could be statically resolved to a variable binding |
| 524 | // which is returned. There is no 'with' statement between the reference and |
| 525 | // the binding, but some scope between the reference scope (inclusive) and |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 526 | // binding scope (exclusive) makes a sloppy 'eval' call, that might |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 527 | // possibly introduce variable bindings shadowing the found one. Thus the |
| 528 | // found variable binding is just a guess. |
| 529 | BOUND_EVAL_SHADOWED, |
| 530 | |
| 531 | // The variable reference could not be statically resolved to any binding |
| 532 | // and thus should be considered referencing a global variable. NULL is |
| 533 | // returned. The variable reference is not inside any 'with' statement and |
| 534 | // no scope between the reference scope (inclusive) and global scope |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 535 | // (exclusive) makes a sloppy 'eval' call. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 536 | UNBOUND, |
| 537 | |
| 538 | // The variable reference could not be statically resolved to any binding |
| 539 | // NULL is returned. The variable reference is not inside any 'with' |
| 540 | // statement, but some scope between the reference scope (inclusive) and |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 541 | // global scope (exclusive) makes a sloppy 'eval' call, that might |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 542 | // possibly introduce a variable binding. Thus the reference should be |
| 543 | // considered referencing a global variable unless it is shadowed by an |
| 544 | // 'eval' introduced binding. |
| 545 | UNBOUND_EVAL_SHADOWED, |
| 546 | |
| 547 | // The variable could not be statically resolved and needs to be looked up |
| 548 | // dynamically. NULL is returned. There are two possible reasons: |
| 549 | // * A 'with' statement has been encountered and there is no variable |
| 550 | // binding for the name between the variable reference and the 'with'. |
| 551 | // The variable potentially references a property of the 'with' object. |
| 552 | // * The code is being executed as part of a call to 'eval' and the calling |
| 553 | // context chain contains either a variable binding for the name or it |
| 554 | // contains a 'with' context. |
| 555 | DYNAMIC_LOOKUP |
| 556 | }; |
| 557 | |
| 558 | // Lookup a variable reference given by name recursively starting with this |
| 559 | // scope. If the code is executed because of a call to 'eval', the context |
| 560 | // parameter should be set to the calling context of 'eval'. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 561 | Variable* LookupRecursive(VariableProxy* proxy, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 562 | BindingKind* binding_kind, |
| 563 | AstNodeFactory<AstNullVisitor>* factory); |
| 564 | MUST_USE_RESULT |
| 565 | bool ResolveVariable(CompilationInfo* info, |
| 566 | VariableProxy* proxy, |
| 567 | AstNodeFactory<AstNullVisitor>* factory); |
| 568 | MUST_USE_RESULT |
| 569 | bool ResolveVariablesRecursively(CompilationInfo* info, |
| 570 | AstNodeFactory<AstNullVisitor>* factory); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 571 | |
| 572 | // Scope analysis. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 573 | void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 574 | bool HasTrivialContext() const; |
| 575 | |
| 576 | // Predicates. |
| 577 | bool MustAllocate(Variable* var); |
| 578 | bool MustAllocateInContext(Variable* var); |
| 579 | bool HasArgumentsParameter(); |
| 580 | |
| 581 | // Variable allocation. |
| 582 | void AllocateStackSlot(Variable* var); |
| 583 | void AllocateHeapSlot(Variable* var); |
| 584 | void AllocateParameterLocals(); |
| 585 | void AllocateNonParameterLocal(Variable* var); |
| 586 | void AllocateNonParameterLocals(); |
| 587 | void AllocateVariablesRecursively(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 588 | void AllocateModulesRecursively(Scope* host_scope); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 589 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 590 | // Resolve and fill in the allocation information for all variables |
| 591 | // in this scopes. Must be called *after* all scopes have been |
| 592 | // processed (parsed) to ensure that unresolved variables can be |
| 593 | // resolved properly. |
| 594 | // |
| 595 | // In the case of code compiled and run using 'eval', the context |
| 596 | // parameter is the context in which eval was called. In all other |
| 597 | // cases the context parameter is an empty handle. |
| 598 | MUST_USE_RESULT |
| 599 | bool AllocateVariables(CompilationInfo* info, |
| 600 | AstNodeFactory<AstNullVisitor>* factory); |
| 601 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 602 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 603 | // Construct a scope based on the scope info. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 604 | Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
| 605 | AstValueFactory* value_factory, Zone* zone); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 606 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 607 | // Construct a catch scope with a binding for the name. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 608 | Scope(Scope* inner_scope, |
| 609 | const AstRawString* catch_variable_name, |
| 610 | AstValueFactory* value_factory, Zone* zone); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 611 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 612 | void AddInnerScope(Scope* inner_scope) { |
| 613 | if (inner_scope != NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 614 | inner_scopes_.Add(inner_scope, zone_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 615 | inner_scope->outer_scope_ = this; |
| 616 | } |
| 617 | } |
| 618 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 619 | void SetDefaults(ScopeType type, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 620 | Scope* outer_scope, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 621 | Handle<ScopeInfo> scope_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 622 | |
| 623 | AstValueFactory* ast_value_factory_; |
| 624 | Zone* zone_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | }; |
| 626 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 627 | } } // namespace v8::internal |
| 628 | |
| 629 | #endif // V8_SCOPES_H_ |