svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2012 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" |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 32 | #include "zone.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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. |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 41 | class VariableMap: public ZoneHashMap { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 43 | explicit VariableMap(Zone* zone); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 45 | virtual ~VariableMap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 47 | Variable* Declare(Scope* scope, |
| 48 | Handle<String> name, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 49 | VariableMode mode, |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 50 | bool is_valid_lhs, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 51 | Variable::Kind kind, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 52 | InitializationFlag initialization_flag, |
| 53 | Interface* interface = Interface::NewValue()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 54 | |
| 55 | Variable* Lookup(Handle<String> name); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 56 | |
| 57 | Zone* zone() const { return zone_; } |
| 58 | |
| 59 | private: |
| 60 | Zone* zone_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 64 | // The dynamic scope part holds hash maps for the variables that will |
| 65 | // be looked up dynamically from within eval and with scopes. The objects |
| 66 | // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
| 67 | // and setup time for scopes that don't need them. |
| 68 | class DynamicScopePart : public ZoneObject { |
| 69 | public: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 70 | explicit DynamicScopePart(Zone* zone) { |
| 71 | for (int i = 0; i < 3; i++) |
| 72 | maps_[i] = new(zone->New(sizeof(VariableMap))) VariableMap(zone); |
| 73 | } |
| 74 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 75 | VariableMap* GetMap(VariableMode mode) { |
| 76 | int index = mode - DYNAMIC; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 77 | ASSERT(index >= 0 && index < 3); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 78 | return maps_[index]; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 82 | VariableMap *maps_[3]; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 86 | // Global invariants after AST construction: Each reference (i.e. identifier) |
| 87 | // to a JavaScript variable (including global properties) is represented by a |
| 88 | // VariableProxy node. Immediately after AST construction and before variable |
| 89 | // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 90 | // corresponding variable (though some are bound during parse time). Variable |
| 91 | // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 92 | // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 93 | // Script variable. |
| 94 | |
| 95 | class Scope: public ZoneObject { |
| 96 | public: |
| 97 | // --------------------------------------------------------------------------- |
| 98 | // Construction |
| 99 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 100 | Scope(Scope* outer_scope, ScopeType type, Zone* zone); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 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 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 107 | static Scope* DeserializeScopeChain(Context* context, Scope* global_scope, |
| 108 | Zone* zone); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 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 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 113 | void Initialize(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 114 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +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 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 120 | Zone* zone() const { return zone_; } |
| 121 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | // --------------------------------------------------------------------------- |
| 123 | // Declarations |
| 124 | |
| 125 | // 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] | 126 | Variable* LocalLookup(Handle<String> name); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 127 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 128 | // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 129 | // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| 130 | // the name of named function literal is kept in an intermediate scope |
| 131 | // in between this scope and the next outer scope.) |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 132 | Variable* LookupFunctionVar(Handle<String> name, |
| 133 | AstNodeFactory<AstNullVisitor>* factory); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 134 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 135 | // Lookup a variable in this scope or outer scopes. |
| 136 | // Returns the variable or NULL if not found. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 137 | Variable* Lookup(Handle<String> name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 138 | |
| 139 | // Declare the function variable for a function literal. This variable |
| 140 | // is in an intermediate scope between this function scope and the the |
| 141 | // outer scope. Only possible for function scopes; at most one variable. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 142 | void DeclareFunctionVar(VariableDeclaration* declaration) { |
| 143 | ASSERT(is_function_scope()); |
| 144 | function_ = declaration; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 145 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 146 | |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 147 | // Declare a parameter in this scope. When there are duplicated |
| 148 | // parameters the rightmost one 'wins'. However, the implementation |
| 149 | // expects all parameters to be declared and from left to right. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 150 | void DeclareParameter(Handle<String> name, VariableMode mode); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 151 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 152 | // 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] | 153 | // declared before, the previously declared variable is returned. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 154 | Variable* DeclareLocal(Handle<String> name, |
| 155 | VariableMode mode, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 156 | InitializationFlag init_flag, |
| 157 | Interface* interface = Interface::NewValue()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 158 | |
| 159 | // Declare an implicit global variable in this scope which must be a |
| 160 | // global scope. The variable was introduced (possibly from an inner |
| 161 | // scope) by a reference to an unresolved variable with no intervening |
| 162 | // with statements or eval calls. |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 163 | Variable* DeclareDynamicGlobal(Handle<String> name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 165 | // Create a new unresolved variable. |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 166 | template<class Visitor> |
| 167 | VariableProxy* NewUnresolved(AstNodeFactory<Visitor>* factory, |
| 168 | Handle<String> name, |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 169 | Interface* interface = Interface::NewValue(), |
| 170 | int position = RelocInfo::kNoPosition) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 171 | // Note that we must not share the unresolved variables with |
| 172 | // the same name because they may be removed selectively via |
| 173 | // RemoveUnresolved(). |
| 174 | ASSERT(!already_resolved()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 175 | VariableProxy* proxy = |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 176 | factory->NewVariableProxy(name, false, interface, position); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 177 | unresolved_.Add(proxy, zone_); |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 178 | return proxy; |
| 179 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 180 | |
| 181 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 182 | // may have been added optimistically, but then only the variable name |
| 183 | // was used (typically for labels). If the variable was not declared, the |
| 184 | // addition introduced a new unresolved variable which may end up being |
| 185 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 186 | // such a variable again if it was added; otherwise this is a no-op. |
| 187 | void RemoveUnresolved(VariableProxy* var); |
| 188 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 189 | // Creates a new internal variable in this scope. The name is only used |
| 190 | // for printing and cannot be used to find the variable. In particular, |
| 191 | // the only way to get hold of the temporary is by keeping the Variable* |
| 192 | // around. |
| 193 | Variable* NewInternal(Handle<String> name); |
| 194 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 195 | // Creates a new temporary variable in this scope. The name is only used |
| 196 | // for printing and cannot be used to find the variable. In particular, |
| 197 | // the only way to get hold of the temporary is by keeping the Variable* |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 198 | // around. The name should not clash with a legitimate variable names. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 199 | Variable* NewTemporary(Handle<String> name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 200 | |
| 201 | // Adds the specific declaration node to the list of declarations in |
| 202 | // this scope. The declarations are processed as part of entering |
| 203 | // the scope; see codegen.cc:ProcessDeclarations. |
| 204 | void AddDeclaration(Declaration* declaration); |
| 205 | |
| 206 | // --------------------------------------------------------------------------- |
| 207 | // Illegal redeclaration support. |
| 208 | |
| 209 | // Set an expression node that will be executed when the scope is |
| 210 | // entered. We only keep track of one illegal redeclaration node per |
| 211 | // scope - the first one - so if you try to set it multiple times |
| 212 | // the additional requests will be silently ignored. |
| 213 | void SetIllegalRedeclaration(Expression* expression); |
| 214 | |
| 215 | // Visit the illegal redeclaration expression. Do not call if the |
| 216 | // scope doesn't have an illegal redeclaration node. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 217 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | |
| 219 | // Check if the scope has (at least) one illegal redeclaration. |
| 220 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 221 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 222 | // For harmony block scoping mode: Check if the scope has conflicting var |
| 223 | // declarations, i.e. a var declaration that has been hoisted from a nested |
| 224 | // scope over a let binding of the same name. |
| 225 | Declaration* CheckConflictingVarDeclarations(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | |
| 227 | // --------------------------------------------------------------------------- |
| 228 | // Scope-specific info. |
| 229 | |
| 230 | // Inform the scope that the corresponding code contains a with statement. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 231 | void RecordWithStatement() { scope_contains_with_ = true; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | |
| 233 | // Inform the scope that the corresponding code contains an eval call. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 234 | void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 235 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 236 | // Set the strict mode flag (unless disabled by a global flag). |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 237 | void SetLanguageMode(LanguageMode language_mode) { |
| 238 | language_mode_ = language_mode; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Position in the source where this scope begins and ends. |
| 242 | // |
| 243 | // * For the scope of a with statement |
| 244 | // with (obj) stmt |
| 245 | // start position: start position of first token of 'stmt' |
| 246 | // end position: end position of last token of 'stmt' |
| 247 | // * For the scope of a block |
| 248 | // { stmts } |
| 249 | // start position: start position of '{' |
| 250 | // end position: end position of '}' |
| 251 | // * For the scope of a function literal or decalaration |
| 252 | // function fun(a,b) { stmts } |
| 253 | // start position: start position of '(' |
| 254 | // end position: end position of '}' |
| 255 | // * For the scope of a catch block |
| 256 | // try { stms } catch(e) { stmts } |
| 257 | // start position: start position of '(' |
| 258 | // end position: end position of ')' |
| 259 | // * For the scope of a for-statement |
| 260 | // for (let x ...) stmt |
| 261 | // start position: start position of '(' |
| 262 | // end position: end position of last token of 'stmt' |
| 263 | int start_position() const { return start_position_; } |
| 264 | void set_start_position(int statement_pos) { |
| 265 | start_position_ = statement_pos; |
| 266 | } |
| 267 | int end_position() const { return end_position_; } |
| 268 | void set_end_position(int statement_pos) { |
| 269 | end_position_ = statement_pos; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 270 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 271 | |
| 272 | // --------------------------------------------------------------------------- |
| 273 | // Predicates. |
| 274 | |
| 275 | // Specific scope types. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 276 | bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 277 | bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 278 | bool is_module_scope() const { return type_ == MODULE_SCOPE; } |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 279 | bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 280 | bool is_catch_scope() const { return type_ == CATCH_SCOPE; } |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 281 | bool is_block_scope() const { return type_ == BLOCK_SCOPE; } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 282 | bool is_with_scope() const { return type_ == WITH_SCOPE; } |
| 283 | bool is_declaration_scope() const { |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 284 | return is_eval_scope() || is_function_scope() || |
| 285 | is_module_scope() || is_global_scope(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 286 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 287 | bool is_classic_mode() const { |
| 288 | return language_mode() == CLASSIC_MODE; |
| 289 | } |
| 290 | bool is_extended_mode() const { |
| 291 | return language_mode() == EXTENDED_MODE; |
| 292 | } |
| 293 | bool is_strict_or_extended_eval_scope() const { |
| 294 | return is_eval_scope() && !is_classic_mode(); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 295 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 296 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 297 | // Information about which scopes calls eval. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 298 | bool calls_eval() const { return scope_calls_eval_; } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 299 | bool calls_non_strict_eval() { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 300 | return scope_calls_eval_ && is_classic_mode(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 301 | } |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 302 | bool outer_scope_calls_non_strict_eval() const { |
| 303 | return outer_scope_calls_non_strict_eval_; |
| 304 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 305 | |
| 306 | // Is this scope inside a with statement. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 307 | bool inside_with() const { return scope_inside_with_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 308 | // Does this scope contain a with statement. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 309 | bool contains_with() const { return scope_contains_with_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 310 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 311 | // --------------------------------------------------------------------------- |
| 312 | // Accessors. |
| 313 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 314 | // The type of this scope. |
| 315 | ScopeType type() const { return type_; } |
| 316 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 317 | // The language mode of this scope. |
| 318 | LanguageMode language_mode() const { return language_mode_; } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 319 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 320 | // The variable corresponding the 'this' value. |
| 321 | Variable* receiver() { return receiver_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 322 | |
| 323 | // The variable holding the function literal for named function |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 324 | // literals, or NULL. Only valid for function scopes. |
| 325 | VariableDeclaration* function() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 326 | ASSERT(is_function_scope()); |
| 327 | return function_; |
| 328 | } |
| 329 | |
| 330 | // Parameters. The left-most parameter has index 0. |
| 331 | // Only valid for function scopes. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 332 | Variable* parameter(int index) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 333 | ASSERT(is_function_scope()); |
| 334 | return params_[index]; |
| 335 | } |
| 336 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 337 | int num_parameters() const { return params_.length(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 338 | |
| 339 | // 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] | 340 | Variable* arguments() const { return arguments_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 342 | // Declarations list. |
| 343 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 344 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 345 | // Inner scope list. |
| 346 | ZoneList<Scope*>* inner_scopes() { return &inner_scopes_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 348 | // The scope immediately surrounding this scope, or NULL. |
| 349 | Scope* outer_scope() const { return outer_scope_; } |
| 350 | |
| 351 | // The interface as inferred so far; only for module scopes. |
| 352 | Interface* interface() const { return interface_; } |
| 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | // --------------------------------------------------------------------------- |
| 355 | // Variable allocation. |
| 356 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 357 | // Collect stack and context allocated local variables in this scope. Note |
| 358 | // that the function variable - if present - is not collected and should be |
| 359 | // handled separately. |
| 360 | void CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
| 361 | ZoneList<Variable*>* context_locals); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 362 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 363 | // Current number of var or const locals. |
| 364 | int num_var_or_const() { return num_var_or_const_; } |
| 365 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 366 | // Result of variable allocation. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 367 | int num_stack_slots() const { return num_stack_slots_; } |
| 368 | int num_heap_slots() const { return num_heap_slots_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 369 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 370 | int StackLocalCount() const; |
| 371 | int ContextLocalCount() const; |
| 372 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 373 | // For global scopes, the number of module literals (including nested ones). |
| 374 | int num_modules() const { return num_modules_; } |
| 375 | |
| 376 | // For module scopes, the host scope's internal variable binding this module. |
| 377 | Variable* module_var() const { return module_var_; } |
| 378 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 379 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 380 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 381 | |
| 382 | // Determine if we can use lazy compilation for this scope. |
| 383 | bool AllowsLazyCompilation() const; |
| 384 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 385 | // Determine if we can use lazy compilation for this scope without a context. |
| 386 | bool AllowsLazyCompilationWithoutContext() const; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 387 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 388 | // True if the outer context of this scope is always the native context. |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 389 | bool HasTrivialOuterContext() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 390 | |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 391 | // True if the outer context allows lazy compilation of this scope. |
| 392 | bool HasLazyCompilableOuterContext() const; |
ulan@chromium.org | d6899c3 | 2012-05-18 14:12:25 +0000 | [diff] [blame] | 393 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | // The number of contexts between this and scope; zero if this == scope. |
| 395 | int ContextChainLength(Scope* scope); |
| 396 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 397 | // Find the innermost global scope. |
| 398 | Scope* GlobalScope(); |
| 399 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 400 | // Find the first function, global, or eval scope. This is the scope |
| 401 | // where var declarations will be hoisted to in the implementation. |
| 402 | Scope* DeclarationScope(); |
| 403 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 404 | Handle<ScopeInfo> GetScopeInfo(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 405 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 406 | // Get the chain of nested scopes within this scope for the source statement |
| 407 | // position. The scopes will be added to the list from the outermost scope to |
| 408 | // the innermost scope. Only nested block, catch or with scopes are tracked |
| 409 | // and will be returned, but no inner function scopes. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 410 | void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 411 | int statement_position); |
| 412 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 413 | // --------------------------------------------------------------------------- |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 414 | // Strict mode support. |
| 415 | bool IsDeclared(Handle<String> name) { |
| 416 | // During formal parameter list parsing the scope only contains |
| 417 | // two variables inserted at initialization: "this" and "arguments". |
| 418 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 419 | // name in strict mode. Therefore looking up with the map which includes |
| 420 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 421 | return variables_.Lookup(name) != NULL; |
| 422 | } |
| 423 | |
| 424 | // --------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 425 | // Debugging. |
| 426 | |
| 427 | #ifdef DEBUG |
| 428 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 429 | #endif |
| 430 | |
| 431 | // --------------------------------------------------------------------------- |
| 432 | // Implementation. |
| 433 | protected: |
| 434 | friend class ParserFactory; |
| 435 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 436 | Isolate* const isolate_; |
| 437 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 438 | // Scope tree. |
| 439 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 440 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 441 | |
| 442 | // The scope type. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 443 | ScopeType type_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 444 | |
| 445 | // Debugging support. |
| 446 | Handle<String> scope_name_; |
| 447 | |
| 448 | // The variables declared in this scope: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 449 | // |
| 450 | // All user-declared variables (incl. parameters). For global scopes |
| 451 | // variables may be implicitly 'declared' by being used (possibly in |
| 452 | // an inner scope) with no intervening with statements or eval calls. |
| 453 | VariableMap variables_; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 454 | // Compiler-allocated (user-invisible) internals. |
| 455 | ZoneList<Variable*> internals_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 456 | // Compiler-allocated (user-invisible) temporaries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 457 | ZoneList<Variable*> temps_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 458 | // Parameter list in source order. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | ZoneList<Variable*> params_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 460 | // Variables that must be looked up dynamically. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 461 | DynamicScopePart* dynamics_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 462 | // Unresolved variables referred to from this scope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | ZoneList<VariableProxy*> unresolved_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 464 | // Declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 465 | ZoneList<Declaration*> decls_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 466 | // Convenience variable. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 467 | Variable* receiver_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 468 | // Function variable, if any; function scopes only. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 469 | VariableDeclaration* function_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 470 | // Convenience variable; function scopes only. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 471 | Variable* arguments_; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 472 | // Interface; module scopes only. |
| 473 | Interface* interface_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 474 | |
| 475 | // Illegal redeclaration. |
| 476 | Expression* illegal_redecl_; |
| 477 | |
vegorov@chromium.org | 7943d46 | 2011-08-01 11:41:52 +0000 | [diff] [blame] | 478 | // Scope-specific information computed during parsing. |
| 479 | // |
| 480 | // This scope is inside a 'with' of some outer scope. |
| 481 | bool scope_inside_with_; |
| 482 | // This scope contains a 'with' statement. |
| 483 | bool scope_contains_with_; |
| 484 | // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 485 | // the 'eval' call site this scope is the declaration scope. |
| 486 | bool scope_calls_eval_; |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 487 | // The language mode of this scope. |
| 488 | LanguageMode language_mode_; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 489 | // Source positions. |
| 490 | int start_position_; |
| 491 | int end_position_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 492 | |
| 493 | // Computed via PropagateScopeInfo. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 494 | bool outer_scope_calls_non_strict_eval_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 495 | bool inner_scope_calls_eval_; |
| 496 | bool force_eager_compilation_; |
| 497 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 498 | // True if it doesn't need scope resolution (e.g., if the scope was |
| 499 | // constructed based on a serialized scope info or a catch context). |
| 500 | bool already_resolved_; |
| 501 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 502 | // Computed as variables are declared. |
| 503 | int num_var_or_const_; |
| 504 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 505 | // Computed via AllocateVariables; function, block and catch scopes only. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 506 | int num_stack_slots_; |
| 507 | int num_heap_slots_; |
| 508 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 509 | // The number of modules (including nested ones). |
| 510 | int num_modules_; |
| 511 | |
| 512 | // For module scopes, the host scope's internal variable binding this module. |
| 513 | Variable* module_var_; |
| 514 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 515 | // Serialized scope info support. |
| 516 | Handle<ScopeInfo> scope_info_; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 517 | bool already_resolved() { return already_resolved_; } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 518 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 519 | // Create a non-local variable with a given name. |
| 520 | // These variables are looked up dynamically at runtime. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 521 | Variable* NonLocal(Handle<String> name, VariableMode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 522 | |
| 523 | // Variable resolution. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 524 | // Possible results of a recursive variable lookup telling if and how a |
| 525 | // variable is bound. These are returned in the output parameter *binding_kind |
| 526 | // of the LookupRecursive function. |
| 527 | enum BindingKind { |
| 528 | // The variable reference could be statically resolved to a variable binding |
| 529 | // which is returned. There is no 'with' statement between the reference and |
| 530 | // the binding and no scope between the reference scope (inclusive) and |
| 531 | // binding scope (exclusive) makes a non-strict 'eval' call. |
| 532 | BOUND, |
| 533 | |
| 534 | // The variable reference could be statically resolved to a variable binding |
| 535 | // which is returned. There is no 'with' statement between the reference and |
| 536 | // the binding, but some scope between the reference scope (inclusive) and |
| 537 | // binding scope (exclusive) makes a non-strict 'eval' call, that might |
| 538 | // possibly introduce variable bindings shadowing the found one. Thus the |
| 539 | // found variable binding is just a guess. |
| 540 | BOUND_EVAL_SHADOWED, |
| 541 | |
| 542 | // The variable reference could not be statically resolved to any binding |
| 543 | // and thus should be considered referencing a global variable. NULL is |
| 544 | // returned. The variable reference is not inside any 'with' statement and |
| 545 | // no scope between the reference scope (inclusive) and global scope |
| 546 | // (exclusive) makes a non-strict 'eval' call. |
| 547 | UNBOUND, |
| 548 | |
| 549 | // The variable reference could not be statically resolved to any binding |
| 550 | // NULL is returned. The variable reference is not inside any 'with' |
| 551 | // statement, but some scope between the reference scope (inclusive) and |
| 552 | // global scope (exclusive) makes a non-strict 'eval' call, that might |
| 553 | // possibly introduce a variable binding. Thus the reference should be |
| 554 | // considered referencing a global variable unless it is shadowed by an |
| 555 | // 'eval' introduced binding. |
| 556 | UNBOUND_EVAL_SHADOWED, |
| 557 | |
| 558 | // The variable could not be statically resolved and needs to be looked up |
| 559 | // dynamically. NULL is returned. There are two possible reasons: |
| 560 | // * A 'with' statement has been encountered and there is no variable |
| 561 | // binding for the name between the variable reference and the 'with'. |
| 562 | // The variable potentially references a property of the 'with' object. |
| 563 | // * The code is being executed as part of a call to 'eval' and the calling |
| 564 | // context chain contains either a variable binding for the name or it |
| 565 | // contains a 'with' context. |
| 566 | DYNAMIC_LOOKUP |
| 567 | }; |
| 568 | |
| 569 | // Lookup a variable reference given by name recursively starting with this |
| 570 | // scope. If the code is executed because of a call to 'eval', the context |
| 571 | // parameter should be set to the calling context of 'eval'. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 572 | Variable* LookupRecursive(Handle<String> name, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 573 | BindingKind* binding_kind, |
| 574 | AstNodeFactory<AstNullVisitor>* factory); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 575 | MUST_USE_RESULT |
| 576 | bool ResolveVariable(CompilationInfo* info, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 577 | VariableProxy* proxy, |
| 578 | AstNodeFactory<AstNullVisitor>* factory); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 579 | MUST_USE_RESULT |
| 580 | bool ResolveVariablesRecursively(CompilationInfo* info, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 581 | AstNodeFactory<AstNullVisitor>* factory); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 582 | |
| 583 | // Scope analysis. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 584 | bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 585 | bool HasTrivialContext() const; |
| 586 | |
| 587 | // Predicates. |
| 588 | bool MustAllocate(Variable* var); |
| 589 | bool MustAllocateInContext(Variable* var); |
| 590 | bool HasArgumentsParameter(); |
| 591 | |
| 592 | // Variable allocation. |
| 593 | void AllocateStackSlot(Variable* var); |
| 594 | void AllocateHeapSlot(Variable* var); |
| 595 | void AllocateParameterLocals(); |
| 596 | void AllocateNonParameterLocal(Variable* var); |
| 597 | void AllocateNonParameterLocals(); |
| 598 | void AllocateVariablesRecursively(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 599 | void AllocateModulesRecursively(Scope* host_scope); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 600 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 601 | // Resolve and fill in the allocation information for all variables |
| 602 | // in this scopes. Must be called *after* all scopes have been |
| 603 | // processed (parsed) to ensure that unresolved variables can be |
| 604 | // resolved properly. |
| 605 | // |
| 606 | // In the case of code compiled and run using 'eval', the context |
| 607 | // parameter is the context in which eval was called. In all other |
| 608 | // cases the context parameter is an empty handle. |
| 609 | MUST_USE_RESULT |
| 610 | bool AllocateVariables(CompilationInfo* info, |
| 611 | AstNodeFactory<AstNullVisitor>* factory); |
| 612 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 613 | private: |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 614 | // Construct a scope based on the scope info. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 615 | Scope(Scope* inner_scope, ScopeType type, Handle<ScopeInfo> scope_info, |
| 616 | Zone* zone); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 617 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 618 | // Construct a catch scope with a binding for the name. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 619 | Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 620 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 621 | void AddInnerScope(Scope* inner_scope) { |
| 622 | if (inner_scope != NULL) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 623 | inner_scopes_.Add(inner_scope, zone_); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 624 | inner_scope->outer_scope_ = this; |
| 625 | } |
| 626 | } |
| 627 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 628 | void SetDefaults(ScopeType type, |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 629 | Scope* outer_scope, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 630 | Handle<ScopeInfo> scope_info); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 631 | |
| 632 | Zone* zone_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 633 | }; |
| 634 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 635 | } } // namespace v8::internal |
| 636 | |
| 637 | #endif // V8_SCOPES_H_ |