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