ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 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 | |
| 34 | namespace v8 { namespace internal { |
| 35 | |
| 36 | |
| 37 | // A hash map to support fast local variable declaration and lookup. |
| 38 | |
| 39 | class LocalsMap: public HashMap { |
| 40 | public: |
| 41 | LocalsMap(); |
| 42 | |
| 43 | // Dummy constructor. This constructor doesn't set up the map |
| 44 | // properly so don't use it unless you have a good reason. |
| 45 | explicit LocalsMap(bool gotta_love_static_overloading); |
| 46 | |
| 47 | virtual ~LocalsMap(); |
| 48 | |
| 49 | Variable* Declare(Scope* scope, Handle<String> name, Variable::Mode mode, |
| 50 | bool is_valid_LHS, bool is_this); |
| 51 | |
| 52 | Variable* Lookup(Handle<String> name); |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | // Global invariants after AST construction: Each reference (i.e. identifier) |
| 57 | // to a JavaScript variable (including global properties) is represented by a |
| 58 | // VariableProxy node. Immediately after AST construction and before variable |
| 59 | // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 60 | // corresponding variable (though some are bound during parse time). Variable |
| 61 | // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 62 | // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 63 | // Script variable. |
| 64 | |
| 65 | class Scope: public ZoneObject { |
| 66 | public: |
| 67 | // --------------------------------------------------------------------------- |
| 68 | // Construction |
| 69 | |
| 70 | enum Type { |
| 71 | EVAL_SCOPE, // the top-level scope for an 'eval' source |
| 72 | FUNCTION_SCOPE, // the top-level scope for a function |
| 73 | GLOBAL_SCOPE // the top-level scope for a program or a top-level eval |
| 74 | }; |
| 75 | |
| 76 | Scope(); |
| 77 | Scope(Scope* outer_scope, Type type); |
| 78 | |
| 79 | virtual ~Scope() { } |
| 80 | |
| 81 | // The scope name is only used for printing/debugging. |
| 82 | void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
| 83 | |
| 84 | void Initialize(bool inside_with); |
| 85 | |
| 86 | |
| 87 | // --------------------------------------------------------------------------- |
| 88 | // Declarations |
| 89 | |
| 90 | // Lookup a variable in this scope. Returns the variable or NULL if not found. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 91 | virtual Variable* LookupLocal(Handle<String> name); |
| 92 | |
| 93 | // Lookup a variable in this scope or outer scopes. |
| 94 | // Returns the variable or NULL if not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | virtual Variable* Lookup(Handle<String> name); |
| 96 | |
| 97 | // Declare the function variable for a function literal. This variable |
| 98 | // is in an intermediate scope between this function scope and the the |
| 99 | // outer scope. Only possible for function scopes; at most one variable. |
| 100 | Variable* DeclareFunctionVar(Handle<String> name); |
| 101 | |
| 102 | // Declare a variable in this scope. If the variable has been |
| 103 | // declared before, the previously declared variable is returned. |
| 104 | virtual Variable* Declare(Handle<String> name, Variable::Mode mode); |
| 105 | |
| 106 | // Add a parameter to the parameter list. The parameter must have been |
| 107 | // declared via Declare. The same parameter may occur more then once in |
| 108 | // the parameter list; they must be added in source order, from left to |
| 109 | // right. |
| 110 | void AddParameter(Variable* var); |
| 111 | |
| 112 | // Create a new unresolved variable. |
| 113 | virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with); |
| 114 | |
| 115 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 116 | // may have been added optimistically, but then only the variable name |
| 117 | // was used (typically for labels). If the variable was not declared, the |
| 118 | // addition introduced a new unresolved variable which may end up being |
| 119 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 120 | // such a variable again if it was added; otherwise this is a no-op. |
| 121 | void RemoveUnresolved(VariableProxy* var); |
| 122 | |
| 123 | // Creates a new temporary variable in this scope and binds a proxy to it. |
| 124 | // The name is only used for printing and cannot be used to find the variable. |
| 125 | // In particular, the only way to get hold of the temporary is by keeping the |
| 126 | // VariableProxy* around. |
| 127 | virtual VariableProxy* NewTemporary(Handle<String> name); |
| 128 | |
| 129 | // Adds the specific declaration node to the list of declarations in |
| 130 | // this scope. The declarations are processed as part of entering |
| 131 | // the scope; see codegen.cc:ProcessDeclarations. |
| 132 | void AddDeclaration(Declaration* declaration); |
| 133 | |
| 134 | // --------------------------------------------------------------------------- |
| 135 | // Illegal redeclaration support. |
| 136 | |
| 137 | // Set an expression node that will be executed when the scope is |
| 138 | // entered. We only keep track of one illegal redeclaration node per |
| 139 | // scope - the first one - so if you try to set it multiple times |
| 140 | // the additional requests will be silently ignored. |
| 141 | void SetIllegalRedeclaration(Expression* expression); |
| 142 | |
| 143 | // Visit the illegal redeclaration expression. Do not call if the |
| 144 | // scope doesn't have an illegal redeclaration node. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame^] | 145 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | |
| 147 | // Check if the scope has (at least) one illegal redeclaration. |
| 148 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 149 | |
| 150 | |
| 151 | // --------------------------------------------------------------------------- |
| 152 | // Scope-specific info. |
| 153 | |
| 154 | // Inform the scope that the corresponding code contains a with statement. |
| 155 | void RecordWithStatement() { scope_contains_with_ = true; } |
| 156 | |
| 157 | // Inform the scope that the corresponding code contains an eval call. |
| 158 | void RecordEvalCall() { scope_calls_eval_ = true; } |
| 159 | |
| 160 | |
| 161 | // --------------------------------------------------------------------------- |
| 162 | // Predicates. |
| 163 | |
| 164 | // Specific scope types. |
| 165 | bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 166 | bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
| 167 | bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
| 168 | |
| 169 | // The scope immediately surrounding this scope, or NULL. |
| 170 | Scope* outer_scope() const { return outer_scope_; } |
| 171 | |
| 172 | |
| 173 | // --------------------------------------------------------------------------- |
| 174 | // Accessors. |
| 175 | |
| 176 | // The variable corresponding to the (function) receiver. |
| 177 | VariableProxy* receiver() const { return receiver_; } |
| 178 | |
| 179 | // The variable holding the function literal for named function |
| 180 | // literals, or NULL. |
| 181 | // Only valid for function scopes. |
| 182 | Variable* function() const { |
| 183 | ASSERT(is_function_scope()); |
| 184 | return function_; |
| 185 | } |
| 186 | |
| 187 | // Parameters. The left-most parameter has index 0. |
| 188 | // Only valid for function scopes. |
| 189 | Variable* parameter(int index) const { |
| 190 | ASSERT(is_function_scope()); |
| 191 | return params_[index]; |
| 192 | } |
| 193 | |
| 194 | int num_parameters() const { return params_.length(); } |
| 195 | |
| 196 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
| 197 | // If arguments() exist, arguments_shadow() exists, too. |
| 198 | VariableProxy* arguments() const { return arguments_; } |
| 199 | |
| 200 | // The '.arguments' shadow variable if we need to allocate it; NULL otherwise. |
| 201 | // If arguments_shadow() exist, arguments() exists, too. |
| 202 | VariableProxy* arguments_shadow() const { return arguments_shadow_; } |
| 203 | |
| 204 | // Declarations list. |
| 205 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 206 | |
| 207 | |
| 208 | |
| 209 | // --------------------------------------------------------------------------- |
| 210 | // Variable allocation. |
| 211 | |
| 212 | // Collect all used locals in this scope. |
| 213 | template<class Allocator> |
| 214 | void CollectUsedVariables(List<Variable*, Allocator>* locals); |
| 215 | |
| 216 | // Resolve and fill in the allocation information for all variables in |
| 217 | // this scopes. Must be called *after* all scopes have been processed |
| 218 | // (parsed) to ensure that unresolved variables can be resolved properly. |
| 219 | void AllocateVariables(); |
| 220 | |
| 221 | // Result of variable allocation. |
| 222 | int num_stack_slots() const { return num_stack_slots_; } |
| 223 | int num_heap_slots() const { return num_heap_slots_; } |
| 224 | |
| 225 | // True if this scope supports calling eval (has a properly |
| 226 | // initialized context). |
| 227 | bool SupportsEval() const; |
| 228 | |
| 229 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 230 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 231 | |
| 232 | // Determine if we can use lazy compilation for this scope. |
| 233 | bool AllowsLazyCompilation() const; |
| 234 | |
| 235 | // True if the outer context of this scope is always the global context. |
| 236 | bool HasTrivialOuterContext() const; |
| 237 | |
| 238 | // The number of contexts between this and scope; zero if this == scope. |
| 239 | int ContextChainLength(Scope* scope); |
| 240 | |
| 241 | |
| 242 | // --------------------------------------------------------------------------- |
| 243 | // Debugging. |
| 244 | |
| 245 | #ifdef DEBUG |
| 246 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 247 | #endif |
| 248 | |
| 249 | // --------------------------------------------------------------------------- |
| 250 | // Implementation. |
| 251 | protected: |
| 252 | friend class ParserFactory; |
| 253 | |
| 254 | // Scope tree. |
| 255 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 256 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 257 | |
| 258 | // The scope type. |
| 259 | Type type_; |
| 260 | |
| 261 | // Debugging support. |
| 262 | Handle<String> scope_name_; |
| 263 | |
| 264 | // The variables declared in this scope: |
| 265 | // all user-declared variables (incl. parameters) |
| 266 | LocalsMap locals_; |
| 267 | // compiler-allocated (user-invisible) temporaries |
| 268 | ZoneList<Variable*> temps_; |
| 269 | // parameter list in source order |
| 270 | ZoneList<Variable*> params_; |
| 271 | // variables that must be looked up dynamically |
| 272 | ZoneList<Variable*> nonlocals_; |
| 273 | // unresolved variables referred to from this scope |
| 274 | ZoneList<VariableProxy*> unresolved_; |
| 275 | // declarations |
| 276 | ZoneList<Declaration*> decls_; |
| 277 | // convenience variable |
| 278 | VariableProxy* receiver_; |
| 279 | // function variable, if any; function scopes only |
| 280 | Variable* function_; |
| 281 | // convenience variable; function scopes only |
| 282 | VariableProxy* arguments_; |
| 283 | // convenience variable; function scopes only |
| 284 | VariableProxy* arguments_shadow_; |
| 285 | |
| 286 | // Illegal redeclaration. |
| 287 | Expression* illegal_redecl_; |
| 288 | |
| 289 | // Scope-specific information. |
| 290 | bool scope_inside_with_; // this scope is inside a 'with' of some outer scope |
| 291 | bool scope_contains_with_; // this scope contains a 'with' statement |
| 292 | bool scope_calls_eval_; // this scope contains an 'eval' call |
| 293 | |
| 294 | // Computed via PropagateScopeInfo. |
| 295 | bool outer_scope_calls_eval_; |
| 296 | bool inner_scope_calls_eval_; |
| 297 | bool force_eager_compilation_; |
| 298 | |
| 299 | // Computed via AllocateVariables; function scopes only. |
| 300 | int num_stack_slots_; |
| 301 | int num_heap_slots_; |
| 302 | |
| 303 | // Create a non-local variable with a given name. |
| 304 | // These variables are looked up dynamically at runtime. |
| 305 | Variable* NonLocal(Handle<String> name); |
| 306 | |
| 307 | // Variable resolution. |
| 308 | Variable* LookupRecursive(Handle<String> name, bool inner_lookup); |
| 309 | void ResolveVariable(Scope* global_scope, VariableProxy* proxy); |
| 310 | void ResolveVariablesRecursively(Scope* global_scope); |
| 311 | |
| 312 | // Scope analysis. |
| 313 | bool PropagateScopeInfo(bool outer_scope_calls_eval); |
| 314 | bool HasTrivialContext() const; |
| 315 | |
| 316 | // Predicates. |
| 317 | bool MustAllocate(Variable* var); |
| 318 | bool MustAllocateInContext(Variable* var); |
| 319 | bool HasArgumentsParameter(); |
| 320 | |
| 321 | // Variable allocation. |
| 322 | void AllocateStackSlot(Variable* var); |
| 323 | void AllocateHeapSlot(Variable* var); |
| 324 | void AllocateParameterLocals(); |
| 325 | void AllocateNonParameterLocal(Variable* var); |
| 326 | void AllocateNonParameterLocals(); |
| 327 | void AllocateVariablesRecursively(); |
| 328 | }; |
| 329 | |
| 330 | |
| 331 | class DummyScope : public Scope { |
| 332 | public: |
| 333 | DummyScope() { |
| 334 | outer_scope_ = this; |
| 335 | } |
| 336 | |
| 337 | virtual Variable* Lookup(Handle<String> name) { return NULL; } |
| 338 | virtual Variable* Declare(Handle<String> name, Variable::Mode mode) { |
| 339 | return NULL; |
| 340 | } |
| 341 | virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) { |
| 342 | return NULL; |
| 343 | } |
| 344 | virtual VariableProxy* NewTemporary(Handle<String> name) { return NULL; } |
| 345 | }; |
| 346 | |
| 347 | |
| 348 | } } // namespace v8::internal |
| 349 | |
| 350 | #endif // V8_SCOPES_H_ |