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