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 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 83 | static Scope* DeserializeScopeChain(Context* context, Scope* script_scope, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 111 | AstNodeFactory* factory); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 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 |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 138 | // script scope. The variable was introduced (possibly from an inner |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 144 | VariableProxy* NewUnresolved(AstNodeFactory* factory, |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | const AstRawString* name, |
| 146 | Interface* interface = Interface::NewValue(), |
| 147 | int position = RelocInfo::kNoPosition) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 148 | // Note that we must not share the unresolved variables with |
| 149 | // the same name because they may be removed selectively via |
| 150 | // RemoveUnresolved(). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 151 | DCHECK(!already_resolved()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 152 | VariableProxy* proxy = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | factory->NewVariableProxy(name, false, interface, position); |
| 154 | unresolved_.Add(proxy, zone_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 155 | return proxy; |
| 156 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 157 | |
| 158 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 159 | // may have been added optimistically, but then only the variable name |
| 160 | // was used (typically for labels). If the variable was not declared, the |
| 161 | // addition introduced a new unresolved variable which may end up being |
| 162 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 163 | // such a variable again if it was added; otherwise this is a no-op. |
| 164 | void RemoveUnresolved(VariableProxy* var); |
| 165 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 166 | // 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] | 167 | // 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* |
| 169 | // around. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | Variable* NewInternal(const AstRawString* name); |
| 171 | |
| 172 | // 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, |
| 174 | // 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. |
| 176 | Variable* NewTemporary(const AstRawString* name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 177 | |
| 178 | // Adds the specific declaration node to the list of declarations in |
| 179 | // this scope. The declarations are processed as part of entering |
| 180 | // the scope; see codegen.cc:ProcessDeclarations. |
| 181 | void AddDeclaration(Declaration* declaration); |
| 182 | |
| 183 | // --------------------------------------------------------------------------- |
| 184 | // Illegal redeclaration support. |
| 185 | |
| 186 | // Set an expression node that will be executed when the scope is |
| 187 | // entered. We only keep track of one illegal redeclaration node per |
| 188 | // scope - the first one - so if you try to set it multiple times |
| 189 | // the additional requests will be silently ignored. |
| 190 | void SetIllegalRedeclaration(Expression* expression); |
| 191 | |
| 192 | // Visit the illegal redeclaration expression. Do not call if the |
| 193 | // scope doesn't have an illegal redeclaration node. |
| 194 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
| 195 | |
| 196 | // Check if the scope has (at least) one illegal redeclaration. |
| 197 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 198 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 199 | // For harmony block scoping mode: Check if the scope has conflicting var |
| 200 | // declarations, i.e. a var declaration that has been hoisted from a nested |
| 201 | // scope over a let binding of the same name. |
| 202 | Declaration* CheckConflictingVarDeclarations(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
| 204 | // --------------------------------------------------------------------------- |
| 205 | // Scope-specific info. |
| 206 | |
| 207 | // Inform the scope that the corresponding code contains a with statement. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 208 | void RecordWithStatement() { scope_contains_with_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 209 | |
| 210 | // Inform the scope that the corresponding code contains an eval call. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 211 | void RecordEvalCall() { if (!is_script_scope()) scope_calls_eval_ = true; } |
| 212 | |
| 213 | // Inform the scope that the corresponding code uses "arguments". |
| 214 | void RecordArgumentsUsage() { scope_uses_arguments_ = true; } |
| 215 | |
| 216 | // Inform the scope that the corresponding code uses "super". |
| 217 | void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; } |
| 218 | |
| 219 | // Inform the scope that the corresponding code invokes "super" constructor. |
| 220 | void RecordSuperConstructorCallUsage() { |
| 221 | scope_uses_super_constructor_call_ = true; |
| 222 | } |
| 223 | |
| 224 | // Inform the scope that the corresponding code uses "this". |
| 225 | void RecordThisUsage() { scope_uses_this_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 226 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 227 | // Set the strict mode flag (unless disabled by a global flag). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 228 | void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } |
| 229 | |
| 230 | // Set the ASM module flag. |
| 231 | void SetAsmModule() { asm_module_ = true; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 232 | |
| 233 | // Position in the source where this scope begins and ends. |
| 234 | // |
| 235 | // * For the scope of a with statement |
| 236 | // with (obj) stmt |
| 237 | // start position: start position of first token of 'stmt' |
| 238 | // end position: end position of last token of 'stmt' |
| 239 | // * For the scope of a block |
| 240 | // { stmts } |
| 241 | // start position: start position of '{' |
| 242 | // end position: end position of '}' |
| 243 | // * For the scope of a function literal or decalaration |
| 244 | // function fun(a,b) { stmts } |
| 245 | // start position: start position of '(' |
| 246 | // end position: end position of '}' |
| 247 | // * For the scope of a catch block |
| 248 | // try { stms } catch(e) { stmts } |
| 249 | // start position: start position of '(' |
| 250 | // end position: end position of ')' |
| 251 | // * For the scope of a for-statement |
| 252 | // for (let x ...) stmt |
| 253 | // start position: start position of '(' |
| 254 | // end position: end position of last token of 'stmt' |
| 255 | int start_position() const { return start_position_; } |
| 256 | void set_start_position(int statement_pos) { |
| 257 | start_position_ = statement_pos; |
| 258 | } |
| 259 | int end_position() const { return end_position_; } |
| 260 | void set_end_position(int statement_pos) { |
| 261 | end_position_ = statement_pos; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 262 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | // In some cases we want to force context allocation for a whole scope. |
| 265 | void ForceContextAllocation() { |
| 266 | DCHECK(!already_resolved()); |
| 267 | force_context_allocation_ = true; |
| 268 | } |
| 269 | bool has_forced_context_allocation() const { |
| 270 | return force_context_allocation_; |
| 271 | } |
| 272 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 273 | // --------------------------------------------------------------------------- |
| 274 | // Predicates. |
| 275 | |
| 276 | // Specific scope types. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 277 | bool is_eval_scope() const { return scope_type_ == EVAL_SCOPE; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 278 | bool is_function_scope() const { |
| 279 | return scope_type_ == FUNCTION_SCOPE || scope_type_ == ARROW_SCOPE; |
| 280 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 281 | bool is_module_scope() const { return scope_type_ == MODULE_SCOPE; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 282 | bool is_script_scope() const { return scope_type_ == SCRIPT_SCOPE; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 283 | bool is_catch_scope() const { return scope_type_ == CATCH_SCOPE; } |
| 284 | bool is_block_scope() const { return scope_type_ == BLOCK_SCOPE; } |
| 285 | bool is_with_scope() const { return scope_type_ == WITH_SCOPE; } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 286 | bool is_arrow_scope() const { return scope_type_ == ARROW_SCOPE; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 287 | bool is_declaration_scope() const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 288 | return is_eval_scope() || is_function_scope() || |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 289 | is_module_scope() || is_script_scope(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 290 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 291 | bool is_strict_eval_scope() const { |
| 292 | return is_eval_scope() && strict_mode_ == STRICT; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 293 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 294 | |
| 295 | // Information about which scopes calls eval. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 296 | bool calls_eval() const { return scope_calls_eval_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | bool calls_sloppy_eval() { |
| 298 | return scope_calls_eval_ && strict_mode_ == SLOPPY; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 299 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 300 | bool outer_scope_calls_sloppy_eval() const { |
| 301 | return outer_scope_calls_sloppy_eval_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 302 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 303 | bool asm_module() const { return asm_module_; } |
| 304 | bool asm_function() const { return asm_function_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | |
| 306 | // Is this scope inside a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 307 | bool inside_with() const { return scope_inside_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 308 | // Does this scope contain a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 309 | bool contains_with() const { return scope_contains_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 311 | // Does this scope access "arguments". |
| 312 | bool uses_arguments() const { return scope_uses_arguments_; } |
| 313 | // Does any inner scope access "arguments". |
| 314 | bool inner_uses_arguments() const { return inner_scope_uses_arguments_; } |
| 315 | // Does this scope access "super" property (super.foo). |
| 316 | bool uses_super_property() const { return scope_uses_super_property_; } |
| 317 | // Does any inner scope access "super" property. |
| 318 | bool inner_uses_super_property() const { |
| 319 | return inner_scope_uses_super_property_; |
| 320 | } |
| 321 | // Does this scope calls "super" constructor. |
| 322 | bool uses_super_constructor_call() const { |
| 323 | return scope_uses_super_constructor_call_; |
| 324 | } |
| 325 | // Does any inner scope calls "super" constructor. |
| 326 | bool inner_uses_super_constructor_call() const { |
| 327 | return inner_scope_uses_super_constructor_call_; |
| 328 | } |
| 329 | // Does this scope access "this". |
| 330 | bool uses_this() const { return scope_uses_this_; } |
| 331 | // Does any inner scope access "this". |
| 332 | bool inner_uses_this() const { return inner_scope_uses_this_; } |
| 333 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 334 | // --------------------------------------------------------------------------- |
| 335 | // Accessors. |
| 336 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 337 | // The type of this scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 338 | ScopeType scope_type() const { return scope_type_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 339 | |
| 340 | // The language mode of this scope. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 341 | StrictMode strict_mode() const { return strict_mode_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 342 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 343 | // The variable corresponding the 'this' value. |
| 344 | Variable* receiver() { return receiver_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 345 | |
| 346 | // The variable holding the function literal for named function |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 347 | // literals, or NULL. Only valid for function scopes. |
| 348 | VariableDeclaration* function() const { |
| 349 | DCHECK(is_function_scope()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 350 | return function_; |
| 351 | } |
| 352 | |
| 353 | // Parameters. The left-most parameter has index 0. |
| 354 | // Only valid for function scopes. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 355 | Variable* parameter(int index) const { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 356 | DCHECK(is_function_scope()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 357 | return params_[index]; |
| 358 | } |
| 359 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 360 | int num_parameters() const { return params_.length(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | |
| 362 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 363 | Variable* arguments() const { return arguments_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 364 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | // Declarations list. |
| 366 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 367 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 368 | // Inner scope list. |
| 369 | ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } |
| 370 | |
| 371 | // The scope immediately surrounding this scope, or NULL. |
| 372 | Scope* outer_scope() const { return outer_scope_; } |
| 373 | |
| 374 | // The interface as inferred so far; only for module scopes. |
| 375 | Interface* interface() const { return interface_; } |
Ben Murdoch | 5d4cdbf | 2012-04-11 10:23:59 +0100 | [diff] [blame] | 376 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 377 | // --------------------------------------------------------------------------- |
| 378 | // Variable allocation. |
| 379 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 380 | // Collect stack and context allocated local variables in this scope. Note |
| 381 | // that the function variable - if present - is not collected and should be |
| 382 | // handled separately. |
| 383 | void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
| 384 | ZoneList<Variable*>* context_locals); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 386 | // Current number of var or const locals. |
| 387 | int num_var_or_const() { return num_var_or_const_; } |
| 388 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | // Result of variable allocation. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 390 | int num_stack_slots() const { return num_stack_slots_; } |
| 391 | int num_heap_slots() const { return num_heap_slots_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 392 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 393 | int StackLocalCount() const; |
| 394 | int ContextLocalCount() const; |
| 395 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 396 | // For script scopes, the number of module literals (including nested ones). |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 397 | int num_modules() const { return num_modules_; } |
| 398 | |
| 399 | // For module scopes, the host scope's internal variable binding this module. |
| 400 | Variable* module_var() const { return module_var_; } |
| 401 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 403 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 404 | |
| 405 | // Determine if we can use lazy compilation for this scope. |
| 406 | bool AllowsLazyCompilation() const; |
| 407 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | // Determine if we can use lazy compilation for this scope without a context. |
| 409 | bool AllowsLazyCompilationWithoutContext() const; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 410 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 411 | // 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] | 412 | bool HasTrivialOuterContext() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 414 | // True if the outer context allows lazy compilation of this scope. |
| 415 | bool HasLazyCompilableOuterContext() const; |
Ben Murdoch | 4768e9d | 2012-05-24 11:17:13 +0100 | [diff] [blame] | 416 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 417 | // The number of contexts between this and scope; zero if this == scope. |
| 418 | int ContextChainLength(Scope* scope); |
| 419 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 420 | // Find the script scope. |
| 421 | // Used in modules implemenetation to find hosting scope. |
| 422 | // TODO(rossberg): is this needed? |
| 423 | Scope* ScriptScope(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 424 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 425 | // Find the first function, global, or eval scope. This is the scope |
| 426 | // where var declarations will be hoisted to in the implementation. |
| 427 | Scope* DeclarationScope(); |
| 428 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 429 | Handle<ScopeInfo> GetScopeInfo(); |
| 430 | |
| 431 | // Get the chain of nested scopes within this scope for the source statement |
| 432 | // position. The scopes will be added to the list from the outermost scope to |
| 433 | // the innermost scope. Only nested block, catch or with scopes are tracked |
| 434 | // and will be returned, but no inner function scopes. |
| 435 | void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, |
| 436 | int statement_position); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 437 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 438 | // --------------------------------------------------------------------------- |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 439 | // Strict mode support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 440 | bool IsDeclared(const AstRawString* name) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 441 | // During formal parameter list parsing the scope only contains |
| 442 | // two variables inserted at initialization: "this" and "arguments". |
| 443 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 444 | // name in strict mode. Therefore looking up with the map which includes |
| 445 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 446 | return variables_.Lookup(name) != NULL; |
| 447 | } |
| 448 | |
| 449 | // --------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 450 | // Debugging. |
| 451 | |
| 452 | #ifdef DEBUG |
| 453 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 454 | #endif |
| 455 | |
| 456 | // --------------------------------------------------------------------------- |
| 457 | // Implementation. |
| 458 | protected: |
| 459 | friend class ParserFactory; |
| 460 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 461 | Isolate* const isolate_; |
| 462 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 463 | // Scope tree. |
| 464 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 465 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 466 | |
| 467 | // The scope type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 468 | ScopeType scope_type_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 469 | |
| 470 | // Debugging support. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 471 | const AstRawString* scope_name_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | |
| 473 | // The variables declared in this scope: |
| 474 | // |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 475 | // All user-declared variables (incl. parameters). For script scopes |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 476 | // variables may be implicitly 'declared' by being used (possibly in |
| 477 | // an inner scope) with no intervening with statements or eval calls. |
| 478 | VariableMap variables_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 479 | // Compiler-allocated (user-invisible) internals. |
| 480 | ZoneList<Variable*> internals_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 481 | // Compiler-allocated (user-invisible) temporaries. |
| 482 | ZoneList<Variable*> temps_; |
| 483 | // Parameter list in source order. |
| 484 | ZoneList<Variable*> params_; |
| 485 | // Variables that must be looked up dynamically. |
| 486 | DynamicScopePart* dynamics_; |
| 487 | // Unresolved variables referred to from this scope. |
| 488 | ZoneList<VariableProxy*> unresolved_; |
| 489 | // Declarations. |
| 490 | ZoneList<Declaration*> decls_; |
| 491 | // Convenience variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 492 | Variable* receiver_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 493 | // Function variable, if any; function scopes only. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 494 | VariableDeclaration* function_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 495 | // Convenience variable; function scopes only. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 496 | Variable* arguments_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 497 | // Interface; module scopes only. |
| 498 | Interface* interface_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 499 | |
| 500 | // Illegal redeclaration. |
| 501 | Expression* illegal_redecl_; |
| 502 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 503 | // Scope-specific information computed during parsing. |
| 504 | // |
| 505 | // This scope is inside a 'with' of some outer scope. |
| 506 | bool scope_inside_with_; |
| 507 | // This scope contains a 'with' statement. |
| 508 | bool scope_contains_with_; |
| 509 | // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 510 | // the 'eval' call site this scope is the declaration scope. |
| 511 | bool scope_calls_eval_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 512 | // This scope uses "arguments". |
| 513 | bool scope_uses_arguments_; |
| 514 | // This scope uses "super" property ('super.foo'). |
| 515 | bool scope_uses_super_property_; |
| 516 | // This scope uses "super" constructor ('super(..)'). |
| 517 | bool scope_uses_super_constructor_call_; |
| 518 | // This scope uses "this". |
| 519 | bool scope_uses_this_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 520 | // This scope contains an "use asm" annotation. |
| 521 | bool asm_module_; |
| 522 | // This scope's outer context is an asm module. |
| 523 | bool asm_function_; |
| 524 | // The strict mode of this scope. |
| 525 | StrictMode strict_mode_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 526 | // Source positions. |
| 527 | int start_position_; |
| 528 | int end_position_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 529 | |
| 530 | // Computed via PropagateScopeInfo. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 531 | bool outer_scope_calls_sloppy_eval_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 532 | bool inner_scope_calls_eval_; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 533 | bool inner_scope_uses_arguments_; |
| 534 | bool inner_scope_uses_super_property_; |
| 535 | bool inner_scope_uses_super_constructor_call_; |
| 536 | bool inner_scope_uses_this_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 537 | bool force_eager_compilation_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 538 | bool force_context_allocation_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 540 | // True if it doesn't need scope resolution (e.g., if the scope was |
| 541 | // constructed based on a serialized scope info or a catch context). |
| 542 | bool already_resolved_; |
| 543 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 544 | // Computed as variables are declared. |
| 545 | int num_var_or_const_; |
| 546 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 547 | // Computed via AllocateVariables; function, block and catch scopes only. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 548 | int num_stack_slots_; |
| 549 | int num_heap_slots_; |
| 550 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 551 | // The number of modules (including nested ones). |
| 552 | int num_modules_; |
| 553 | |
| 554 | // For module scopes, the host scope's internal variable binding this module. |
| 555 | Variable* module_var_; |
| 556 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 557 | // Serialized scope info support. |
| 558 | Handle<ScopeInfo> scope_info_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 559 | bool already_resolved() { return already_resolved_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 560 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 561 | // Create a non-local variable with a given name. |
| 562 | // These variables are looked up dynamically at runtime. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 563 | Variable* NonLocal(const AstRawString* name, VariableMode mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 564 | |
| 565 | // Variable resolution. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 566 | // Possible results of a recursive variable lookup telling if and how a |
| 567 | // variable is bound. These are returned in the output parameter *binding_kind |
| 568 | // of the LookupRecursive function. |
| 569 | enum BindingKind { |
| 570 | // The variable reference could be statically resolved to a variable binding |
| 571 | // which is returned. There is no 'with' statement between the reference and |
| 572 | // the binding and no scope between the reference scope (inclusive) and |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 573 | // binding scope (exclusive) makes a sloppy 'eval' call. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 574 | BOUND, |
| 575 | |
| 576 | // The variable reference could be statically resolved to a variable binding |
| 577 | // which is returned. There is no 'with' statement between the reference and |
| 578 | // the binding, but some scope between the reference scope (inclusive) and |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 579 | // binding scope (exclusive) makes a sloppy 'eval' call, that might |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 580 | // possibly introduce variable bindings shadowing the found one. Thus the |
| 581 | // found variable binding is just a guess. |
| 582 | BOUND_EVAL_SHADOWED, |
| 583 | |
| 584 | // The variable reference could not be statically resolved to any binding |
| 585 | // and thus should be considered referencing a global variable. NULL is |
| 586 | // returned. The variable reference is not inside any 'with' statement and |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 587 | // no scope between the reference scope (inclusive) and script scope |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 588 | // (exclusive) makes a sloppy 'eval' call. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 589 | UNBOUND, |
| 590 | |
| 591 | // The variable reference could not be statically resolved to any binding |
| 592 | // NULL is returned. The variable reference is not inside any 'with' |
| 593 | // statement, but some scope between the reference scope (inclusive) and |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 594 | // script scope (exclusive) makes a sloppy 'eval' call, that might |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 595 | // possibly introduce a variable binding. Thus the reference should be |
| 596 | // considered referencing a global variable unless it is shadowed by an |
| 597 | // 'eval' introduced binding. |
| 598 | UNBOUND_EVAL_SHADOWED, |
| 599 | |
| 600 | // The variable could not be statically resolved and needs to be looked up |
| 601 | // dynamically. NULL is returned. There are two possible reasons: |
| 602 | // * A 'with' statement has been encountered and there is no variable |
| 603 | // binding for the name between the variable reference and the 'with'. |
| 604 | // The variable potentially references a property of the 'with' object. |
| 605 | // * The code is being executed as part of a call to 'eval' and the calling |
| 606 | // context chain contains either a variable binding for the name or it |
| 607 | // contains a 'with' context. |
| 608 | DYNAMIC_LOOKUP |
| 609 | }; |
| 610 | |
| 611 | // Lookup a variable reference given by name recursively starting with this |
| 612 | // scope. If the code is executed because of a call to 'eval', the context |
| 613 | // parameter should be set to the calling context of 'eval'. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 614 | Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, |
| 615 | AstNodeFactory* factory); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 616 | MUST_USE_RESULT |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 617 | bool ResolveVariable(CompilationInfo* info, VariableProxy* proxy, |
| 618 | AstNodeFactory* factory); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 619 | MUST_USE_RESULT |
| 620 | bool ResolveVariablesRecursively(CompilationInfo* info, |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 621 | AstNodeFactory* factory); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 622 | |
| 623 | // Scope analysis. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 624 | void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 625 | bool HasTrivialContext() const; |
| 626 | |
| 627 | // Predicates. |
| 628 | bool MustAllocate(Variable* var); |
| 629 | bool MustAllocateInContext(Variable* var); |
| 630 | bool HasArgumentsParameter(); |
| 631 | |
| 632 | // Variable allocation. |
| 633 | void AllocateStackSlot(Variable* var); |
| 634 | void AllocateHeapSlot(Variable* var); |
| 635 | void AllocateParameterLocals(); |
| 636 | void AllocateNonParameterLocal(Variable* var); |
| 637 | void AllocateNonParameterLocals(); |
| 638 | void AllocateVariablesRecursively(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 639 | void AllocateModulesRecursively(Scope* host_scope); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 640 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 641 | // Resolve and fill in the allocation information for all variables |
| 642 | // in this scopes. Must be called *after* all scopes have been |
| 643 | // processed (parsed) to ensure that unresolved variables can be |
| 644 | // resolved properly. |
| 645 | // |
| 646 | // In the case of code compiled and run using 'eval', the context |
| 647 | // parameter is the context in which eval was called. In all other |
| 648 | // cases the context parameter is an empty handle. |
| 649 | MUST_USE_RESULT |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 650 | bool AllocateVariables(CompilationInfo* info, AstNodeFactory* factory); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 651 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 652 | private: |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 653 | // Construct a scope based on the scope info. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 654 | Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
| 655 | AstValueFactory* value_factory, Zone* zone); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 656 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 657 | // Construct a catch scope with a binding for the name. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 658 | Scope(Scope* inner_scope, |
| 659 | const AstRawString* catch_variable_name, |
| 660 | AstValueFactory* value_factory, Zone* zone); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 661 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 662 | void AddInnerScope(Scope* inner_scope) { |
| 663 | if (inner_scope != NULL) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 664 | inner_scopes_.Add(inner_scope, zone_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 665 | inner_scope->outer_scope_ = this; |
| 666 | } |
| 667 | } |
| 668 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 669 | void SetDefaults(ScopeType type, |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 670 | Scope* outer_scope, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 671 | Handle<ScopeInfo> scope_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 672 | |
| 673 | AstValueFactory* ast_value_factory_; |
| 674 | Zone* zone_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 677 | } } // namespace v8::internal |
| 678 | |
| 679 | #endif // V8_SCOPES_H_ |