Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1 | // Copyright 2011 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" |
| 32 | #include "hashmap.h" |
| 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. |
| 41 | class VariableMap: public HashMap { |
| 42 | public: |
| 43 | VariableMap(); |
| 44 | |
| 45 | // Dummy constructor. This constructor doesn't set up the map |
| 46 | // properly so don't use it unless you have a good reason. |
| 47 | explicit VariableMap(bool gotta_love_static_overloading); |
| 48 | |
| 49 | virtual ~VariableMap(); |
| 50 | |
| 51 | Variable* Declare(Scope* scope, |
| 52 | Handle<String> name, |
| 53 | Variable::Mode mode, |
| 54 | bool is_valid_lhs, |
| 55 | Variable::Kind kind); |
| 56 | |
| 57 | Variable* Lookup(Handle<String> name); |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // The dynamic scope part holds hash maps for the variables that will |
| 62 | // be looked up dynamically from within eval and with scopes. The objects |
| 63 | // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
| 64 | // and setup time for scopes that don't need them. |
| 65 | class DynamicScopePart : public ZoneObject { |
| 66 | public: |
| 67 | VariableMap* GetMap(Variable::Mode mode) { |
| 68 | int index = mode - Variable::DYNAMIC; |
| 69 | ASSERT(index >= 0 && index < 3); |
| 70 | return &maps_[index]; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | VariableMap maps_[3]; |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | // Global invariants after AST construction: Each reference (i.e. identifier) |
| 79 | // to a JavaScript variable (including global properties) is represented by a |
| 80 | // VariableProxy node. Immediately after AST construction and before variable |
| 81 | // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 82 | // corresponding variable (though some are bound during parse time). Variable |
| 83 | // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 84 | // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 85 | // Script variable. |
| 86 | |
| 87 | class Scope: public ZoneObject { |
| 88 | public: |
| 89 | // --------------------------------------------------------------------------- |
| 90 | // Construction |
| 91 | |
| 92 | enum Type { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 93 | EVAL_SCOPE, // The top-level scope for an eval source. |
| 94 | FUNCTION_SCOPE, // The top-level scope for a function. |
| 95 | GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval. |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 96 | CATCH_SCOPE, // The scope introduced by catch. |
| 97 | BLOCK_SCOPE // The scope introduced by a new block. |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 100 | Scope(Scope* outer_scope, Type type); |
| 101 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 102 | // Compute top scope and allocate variables. For lazy compilation the top |
| 103 | // scope only contains the single lazily compiled function, so this |
| 104 | // doesn't re-allocate variables repeatedly. |
| 105 | static bool Analyze(CompilationInfo* info); |
| 106 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 107 | static Scope* DeserializeScopeChain(CompilationInfo* info, |
| 108 | Scope* innermost_scope); |
| 109 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 110 | // The scope name is only used for printing/debugging. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 111 | void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 112 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 113 | void Initialize(bool inside_with); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 114 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 115 | // Checks if the block scope is redundant, i.e. it does not contain any |
| 116 | // block scoped declarations. In that case it is removed from the scope |
| 117 | // tree and its children are reparented. |
| 118 | Scope* FinalizeBlockScope(); |
| 119 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | // --------------------------------------------------------------------------- |
| 121 | // Declarations |
| 122 | |
| 123 | // 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] | 124 | Variable* LocalLookup(Handle<String> name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | |
| 126 | // Lookup a variable in this scope or outer scopes. |
| 127 | // Returns the variable or NULL if not found. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 128 | Variable* Lookup(Handle<String> name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | |
| 130 | // Declare the function variable for a function literal. This variable |
| 131 | // is in an intermediate scope between this function scope and the the |
| 132 | // outer scope. Only possible for function scopes; at most one variable. |
| 133 | Variable* DeclareFunctionVar(Handle<String> name); |
| 134 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 135 | // Declare a parameter in this scope. When there are duplicated |
| 136 | // parameters the rightmost one 'wins'. However, the implementation |
| 137 | // expects all parameters to be declared and from left to right. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 138 | void DeclareParameter(Handle<String> name, Variable::Mode mode); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 139 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 140 | // Declare a local variable in this scope. If the variable has been |
| 141 | // declared before, the previously declared variable is returned. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 142 | Variable* DeclareLocal(Handle<String> name, Variable::Mode mode); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 143 | |
| 144 | // Declare an implicit global variable in this scope which must be a |
| 145 | // global scope. The variable was introduced (possibly from an inner |
| 146 | // scope) by a reference to an unresolved variable with no intervening |
| 147 | // with statements or eval calls. |
| 148 | Variable* DeclareGlobal(Handle<String> name); |
| 149 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 150 | // Create a new unresolved variable. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 151 | VariableProxy* NewUnresolved(Handle<String> name, |
| 152 | bool inside_with, |
| 153 | int position = RelocInfo::kNoPosition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | |
| 155 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 156 | // may have been added optimistically, but then only the variable name |
| 157 | // was used (typically for labels). If the variable was not declared, the |
| 158 | // addition introduced a new unresolved variable which may end up being |
| 159 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 160 | // such a variable again if it was added; otherwise this is a no-op. |
| 161 | void RemoveUnresolved(VariableProxy* var); |
| 162 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 163 | // Creates a new temporary variable in this scope. The name is only used |
| 164 | // for printing and cannot be used to find the variable. In particular, |
| 165 | // the only way to get hold of the temporary is by keeping the Variable* |
| 166 | // around. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 167 | Variable* NewTemporary(Handle<String> name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | |
| 169 | // Adds the specific declaration node to the list of declarations in |
| 170 | // this scope. The declarations are processed as part of entering |
| 171 | // the scope; see codegen.cc:ProcessDeclarations. |
| 172 | void AddDeclaration(Declaration* declaration); |
| 173 | |
| 174 | // --------------------------------------------------------------------------- |
| 175 | // Illegal redeclaration support. |
| 176 | |
| 177 | // Set an expression node that will be executed when the scope is |
| 178 | // entered. We only keep track of one illegal redeclaration node per |
| 179 | // scope - the first one - so if you try to set it multiple times |
| 180 | // the additional requests will be silently ignored. |
| 181 | void SetIllegalRedeclaration(Expression* expression); |
| 182 | |
| 183 | // Visit the illegal redeclaration expression. Do not call if the |
| 184 | // scope doesn't have an illegal redeclaration node. |
| 185 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
| 186 | |
| 187 | // Check if the scope has (at least) one illegal redeclaration. |
| 188 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 189 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 190 | // For harmony block scoping mode: Check if the scope has conflicting var |
| 191 | // declarations, i.e. a var declaration that has been hoisted from a nested |
| 192 | // scope over a let binding of the same name. |
| 193 | Declaration* CheckConflictingVarDeclarations(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 194 | |
| 195 | // --------------------------------------------------------------------------- |
| 196 | // Scope-specific info. |
| 197 | |
| 198 | // Inform the scope that the corresponding code contains a with statement. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 199 | void RecordWithStatement() { scope_contains_with_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 200 | |
| 201 | // Inform the scope that the corresponding code contains an eval call. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 202 | void RecordEvalCall() { scope_calls_eval_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 204 | // Enable strict mode for the scope (unless disabled by a global flag). |
| 205 | void EnableStrictMode() { |
| 206 | strict_mode_ = FLAG_strict_mode; |
| 207 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 208 | |
| 209 | // --------------------------------------------------------------------------- |
| 210 | // Predicates. |
| 211 | |
| 212 | // Specific scope types. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 213 | bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 214 | bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
| 215 | bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 216 | bool is_catch_scope() const { return type_ == CATCH_SCOPE; } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 217 | bool is_block_scope() const { return type_ == BLOCK_SCOPE; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 218 | bool is_strict_mode() const { return strict_mode_; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 219 | bool is_strict_mode_eval_scope() const { |
| 220 | return is_eval_scope() && is_strict_mode(); |
| 221 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 222 | |
| 223 | // Information about which scopes calls eval. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 224 | bool calls_eval() const { return scope_calls_eval_; } |
| 225 | bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 226 | bool outer_scope_calls_non_strict_eval() const { |
| 227 | return outer_scope_calls_non_strict_eval_; |
| 228 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 229 | |
| 230 | // Is this scope inside a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 231 | bool inside_with() const { return scope_inside_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 232 | // Does this scope contain a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 233 | bool contains_with() const { return scope_contains_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | |
| 235 | // The scope immediately surrounding this scope, or NULL. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 236 | Scope* outer_scope() const { return outer_scope_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 237 | |
| 238 | // --------------------------------------------------------------------------- |
| 239 | // Accessors. |
| 240 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 241 | // The variable corresponding the 'this' value. |
| 242 | Variable* receiver() { return receiver_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | |
| 244 | // The variable holding the function literal for named function |
| 245 | // literals, or NULL. |
| 246 | // Only valid for function scopes. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 247 | VariableProxy* function() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 248 | ASSERT(is_function_scope()); |
| 249 | return function_; |
| 250 | } |
| 251 | |
| 252 | // Parameters. The left-most parameter has index 0. |
| 253 | // Only valid for function scopes. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 254 | Variable* parameter(int index) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | ASSERT(is_function_scope()); |
| 256 | return params_[index]; |
| 257 | } |
| 258 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 259 | int num_parameters() const { return params_.length(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 260 | |
| 261 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 262 | Variable* arguments() const { return arguments_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | // Declarations list. |
| 265 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 266 | |
| 267 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | // --------------------------------------------------------------------------- |
| 269 | // Variable allocation. |
| 270 | |
| 271 | // Collect all used locals in this scope. |
| 272 | template<class Allocator> |
| 273 | void CollectUsedVariables(List<Variable*, Allocator>* locals); |
| 274 | |
| 275 | // Resolve and fill in the allocation information for all variables |
| 276 | // in this scopes. Must be called *after* all scopes have been |
| 277 | // processed (parsed) to ensure that unresolved variables can be |
| 278 | // resolved properly. |
| 279 | // |
| 280 | // In the case of code compiled and run using 'eval', the context |
| 281 | // parameter is the context in which eval was called. In all other |
| 282 | // cases the context parameter is an empty handle. |
| 283 | void AllocateVariables(Handle<Context> context); |
| 284 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 285 | // Current number of var or const locals. |
| 286 | int num_var_or_const() { return num_var_or_const_; } |
| 287 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 288 | // Result of variable allocation. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 289 | int num_stack_slots() const { return num_stack_slots_; } |
| 290 | int num_heap_slots() const { return num_heap_slots_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 291 | |
| 292 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 293 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 294 | |
| 295 | // Determine if we can use lazy compilation for this scope. |
| 296 | bool AllowsLazyCompilation() const; |
| 297 | |
| 298 | // 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] | 299 | bool HasTrivialOuterContext() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | |
| 301 | // The number of contexts between this and scope; zero if this == scope. |
| 302 | int ContextChainLength(Scope* scope); |
| 303 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 304 | // Find the first function, global, or eval scope. This is the scope |
| 305 | // where var declarations will be hoisted to in the implementation. |
| 306 | Scope* DeclarationScope(); |
| 307 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 308 | Handle<SerializedScopeInfo> GetSerializedScopeInfo(); |
| 309 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 310 | // --------------------------------------------------------------------------- |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 311 | // Strict mode support. |
| 312 | bool IsDeclared(Handle<String> name) { |
| 313 | // During formal parameter list parsing the scope only contains |
| 314 | // two variables inserted at initialization: "this" and "arguments". |
| 315 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 316 | // name in strict mode. Therefore looking up with the map which includes |
| 317 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 318 | return variables_.Lookup(name) != NULL; |
| 319 | } |
| 320 | |
| 321 | // --------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 322 | // Debugging. |
| 323 | |
| 324 | #ifdef DEBUG |
| 325 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 326 | #endif |
| 327 | |
| 328 | // --------------------------------------------------------------------------- |
| 329 | // Implementation. |
| 330 | protected: |
| 331 | friend class ParserFactory; |
| 332 | |
| 333 | explicit Scope(Type type); |
| 334 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 335 | Isolate* const isolate_; |
| 336 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | // Scope tree. |
| 338 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 339 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 340 | |
| 341 | // The scope type. |
| 342 | Type type_; |
| 343 | |
| 344 | // Debugging support. |
| 345 | Handle<String> scope_name_; |
| 346 | |
| 347 | // The variables declared in this scope: |
| 348 | // |
| 349 | // All user-declared variables (incl. parameters). For global scopes |
| 350 | // variables may be implicitly 'declared' by being used (possibly in |
| 351 | // an inner scope) with no intervening with statements or eval calls. |
| 352 | VariableMap variables_; |
| 353 | // Compiler-allocated (user-invisible) temporaries. |
| 354 | ZoneList<Variable*> temps_; |
| 355 | // Parameter list in source order. |
| 356 | ZoneList<Variable*> params_; |
| 357 | // Variables that must be looked up dynamically. |
| 358 | DynamicScopePart* dynamics_; |
| 359 | // Unresolved variables referred to from this scope. |
| 360 | ZoneList<VariableProxy*> unresolved_; |
| 361 | // Declarations. |
| 362 | ZoneList<Declaration*> decls_; |
| 363 | // Convenience variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 364 | Variable* receiver_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 365 | // Function variable, if any; function scopes only. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame^] | 366 | VariableProxy* function_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 367 | // Convenience variable; function scopes only. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 368 | Variable* arguments_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 369 | |
| 370 | // Illegal redeclaration. |
| 371 | Expression* illegal_redecl_; |
| 372 | |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 373 | // Scope-specific information computed during parsing. |
| 374 | // |
| 375 | // This scope is inside a 'with' of some outer scope. |
| 376 | bool scope_inside_with_; |
| 377 | // This scope contains a 'with' statement. |
| 378 | bool scope_contains_with_; |
| 379 | // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 380 | // the 'eval' call site this scope is the declaration scope. |
| 381 | bool scope_calls_eval_; |
| 382 | // This scope is a strict mode scope. |
| 383 | bool strict_mode_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 384 | |
| 385 | // Computed via PropagateScopeInfo. |
| 386 | bool outer_scope_calls_eval_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 387 | bool outer_scope_calls_non_strict_eval_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 388 | bool inner_scope_calls_eval_; |
| 389 | bool outer_scope_is_eval_scope_; |
| 390 | bool force_eager_compilation_; |
| 391 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 392 | // True if it doesn't need scope resolution (e.g., if the scope was |
| 393 | // constructed based on a serialized scope info or a catch context). |
| 394 | bool already_resolved_; |
| 395 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 396 | // Computed as variables are declared. |
| 397 | int num_var_or_const_; |
| 398 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | // Computed via AllocateVariables; function scopes only. |
| 400 | int num_stack_slots_; |
| 401 | int num_heap_slots_; |
| 402 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 403 | // Serialized scopes support. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 404 | Handle<SerializedScopeInfo> scope_info_; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 405 | bool already_resolved() { return already_resolved_; } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 406 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 407 | // Create a non-local variable with a given name. |
| 408 | // These variables are looked up dynamically at runtime. |
| 409 | Variable* NonLocal(Handle<String> name, Variable::Mode mode); |
| 410 | |
| 411 | // Variable resolution. |
| 412 | Variable* LookupRecursive(Handle<String> name, |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 413 | bool from_inner_function, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 414 | Variable** invalidated_local); |
| 415 | void ResolveVariable(Scope* global_scope, |
| 416 | Handle<Context> context, |
| 417 | VariableProxy* proxy); |
| 418 | void ResolveVariablesRecursively(Scope* global_scope, |
| 419 | Handle<Context> context); |
| 420 | |
| 421 | // Scope analysis. |
| 422 | bool PropagateScopeInfo(bool outer_scope_calls_eval, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 423 | bool outer_scope_calls_non_strict_eval, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 424 | bool outer_scope_is_eval_scope); |
| 425 | bool HasTrivialContext() const; |
| 426 | |
| 427 | // Predicates. |
| 428 | bool MustAllocate(Variable* var); |
| 429 | bool MustAllocateInContext(Variable* var); |
| 430 | bool HasArgumentsParameter(); |
| 431 | |
| 432 | // Variable allocation. |
| 433 | void AllocateStackSlot(Variable* var); |
| 434 | void AllocateHeapSlot(Variable* var); |
| 435 | void AllocateParameterLocals(); |
| 436 | void AllocateNonParameterLocal(Variable* var); |
| 437 | void AllocateNonParameterLocals(); |
| 438 | void AllocateVariablesRecursively(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 439 | |
| 440 | private: |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 441 | // Construct a function or block scope based on the scope info. |
| 442 | Scope(Scope* inner_scope, Type type, Handle<SerializedScopeInfo> scope_info); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 443 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 444 | // Construct a catch scope with a binding for the name. |
| 445 | Scope(Scope* inner_scope, Handle<String> catch_variable_name); |
| 446 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 447 | void AddInnerScope(Scope* inner_scope) { |
| 448 | if (inner_scope != NULL) { |
| 449 | inner_scopes_.Add(inner_scope); |
| 450 | inner_scope->outer_scope_ = this; |
| 451 | } |
| 452 | } |
| 453 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 454 | void SetDefaults(Type type, |
| 455 | Scope* outer_scope, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 456 | Handle<SerializedScopeInfo> scope_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 457 | }; |
| 458 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 459 | } } // namespace v8::internal |
| 460 | |
| 461 | #endif // V8_SCOPES_H_ |