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 | #include "v8.h" |
| 29 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 30 | #include "scopes.h" |
| 31 | |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 32 | #include "accessors.h" |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 33 | #include "bootstrapper.h" |
| 34 | #include "compiler.h" |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 35 | #include "messages.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | #include "scopeinfo.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 37 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 38 | #include "allocation-inl.h" |
| 39 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
| 43 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | // Implementation of LocalsMap |
| 45 | // |
| 46 | // Note: We are storing the handle locations as key values in the hash map. |
| 47 | // When inserting a new variable via Declare(), we rely on the fact that |
| 48 | // the handle location remains alive for the duration of that variable |
| 49 | // use. Because a Variable holding a handle with the same location exists |
| 50 | // this is ensured. |
| 51 | |
| 52 | static bool Match(void* key1, void* key2) { |
| 53 | String* name1 = *reinterpret_cast<String**>(key1); |
| 54 | String* name2 = *reinterpret_cast<String**>(key2); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 55 | ASSERT(name1->IsInternalizedString()); |
| 56 | ASSERT(name2->IsInternalizedString()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | return name1 == name2; |
| 58 | } |
| 59 | |
| 60 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 61 | VariableMap::VariableMap(Zone* zone) |
| 62 | : ZoneHashMap(Match, 8, ZoneAllocationPolicy(zone)), |
| 63 | zone_(zone) {} |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 64 | VariableMap::~VariableMap() {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | |
| 66 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 67 | Variable* VariableMap::Declare( |
| 68 | Scope* scope, |
| 69 | Handle<String> name, |
| 70 | VariableMode mode, |
| 71 | bool is_valid_lhs, |
| 72 | Variable::Kind kind, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 73 | InitializationFlag initialization_flag, |
| 74 | Interface* interface) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 75 | Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), true, |
| 76 | ZoneAllocationPolicy(zone())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | if (p->value == NULL) { |
| 78 | // The variable has not been declared yet -> insert it. |
| 79 | ASSERT(p->key == name.location()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 80 | p->value = new(zone()) Variable(scope, |
| 81 | name, |
| 82 | mode, |
| 83 | is_valid_lhs, |
| 84 | kind, |
| 85 | initialization_flag, |
| 86 | interface); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | } |
| 88 | return reinterpret_cast<Variable*>(p->value); |
| 89 | } |
| 90 | |
| 91 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 92 | Variable* VariableMap::Lookup(Handle<String> name) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 93 | Entry* p = ZoneHashMap::Lookup(name.location(), name->Hash(), false, |
| 94 | ZoneAllocationPolicy(NULL)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | if (p != NULL) { |
| 96 | ASSERT(*reinterpret_cast<String**>(p->key) == *name); |
| 97 | ASSERT(p->value != NULL); |
| 98 | return reinterpret_cast<Variable*>(p->value); |
| 99 | } |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // ---------------------------------------------------------------------------- |
| 105 | // Implementation of Scope |
| 106 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 107 | Scope::Scope(Scope* outer_scope, ScopeType scope_type, Zone* zone) |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 108 | : isolate_(zone->isolate()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 109 | inner_scopes_(4, zone), |
| 110 | variables_(zone), |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 111 | internals_(4, zone), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 112 | temps_(4, zone), |
| 113 | params_(4, zone), |
| 114 | unresolved_(16, zone), |
| 115 | decls_(4, zone), |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 116 | interface_(FLAG_harmony_modules && |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 117 | (scope_type == MODULE_SCOPE || scope_type == GLOBAL_SCOPE) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 118 | ? Interface::NewModule(zone) : NULL), |
| 119 | already_resolved_(false), |
| 120 | zone_(zone) { |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 121 | SetDefaults(scope_type, outer_scope, Handle<ScopeInfo>::null()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 122 | // The outermost scope must be a global scope. |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 123 | ASSERT(scope_type == GLOBAL_SCOPE || outer_scope != NULL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 124 | ASSERT(!HasIllegalRedeclaration()); |
| 125 | } |
| 126 | |
| 127 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 128 | Scope::Scope(Scope* inner_scope, |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 129 | ScopeType scope_type, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 130 | Handle<ScopeInfo> scope_info, |
| 131 | Zone* zone) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 132 | : isolate_(zone->isolate()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 133 | inner_scopes_(4, zone), |
| 134 | variables_(zone), |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 135 | internals_(4, zone), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 136 | temps_(4, zone), |
| 137 | params_(4, zone), |
| 138 | unresolved_(16, zone), |
| 139 | decls_(4, zone), |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 140 | interface_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 141 | already_resolved_(true), |
| 142 | zone_(zone) { |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 143 | SetDefaults(scope_type, NULL, scope_info); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 144 | if (!scope_info.is_null()) { |
| 145 | num_heap_slots_ = scope_info_->ContextLength(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 146 | } |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 147 | // Ensure at least MIN_CONTEXT_SLOTS to indicate a materialized context. |
| 148 | num_heap_slots_ = Max(num_heap_slots_, |
| 149 | static_cast<int>(Context::MIN_CONTEXT_SLOTS)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 150 | AddInnerScope(inner_scope); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 154 | Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name, Zone* zone) |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 155 | : isolate_(zone->isolate()), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 156 | inner_scopes_(1, zone), |
| 157 | variables_(zone), |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 158 | internals_(0, zone), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 159 | temps_(0, zone), |
| 160 | params_(0, zone), |
| 161 | unresolved_(0, zone), |
| 162 | decls_(0, zone), |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 163 | interface_(NULL), |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 164 | already_resolved_(true), |
| 165 | zone_(zone) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 166 | SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 167 | AddInnerScope(inner_scope); |
| 168 | ++num_var_or_const_; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 169 | num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 170 | Variable* variable = variables_.Declare(this, |
| 171 | catch_variable_name, |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 172 | VAR, |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 173 | true, // Valid left-hand side. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 174 | Variable::NORMAL, |
| 175 | kCreatedInitialized); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 176 | AllocateHeapSlot(variable); |
| 177 | } |
| 178 | |
| 179 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 180 | void Scope::SetDefaults(ScopeType scope_type, |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 181 | Scope* outer_scope, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 182 | Handle<ScopeInfo> scope_info) { |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 183 | outer_scope_ = outer_scope; |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 184 | scope_type_ = scope_type; |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 185 | scope_name_ = isolate_->factory()->empty_string(); |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 186 | dynamics_ = NULL; |
| 187 | receiver_ = NULL; |
| 188 | function_ = NULL; |
| 189 | arguments_ = NULL; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 190 | illegal_redecl_ = NULL; |
| 191 | scope_inside_with_ = false; |
| 192 | scope_contains_with_ = false; |
| 193 | scope_calls_eval_ = false; |
| 194 | // Inherit the strict mode from the parent scope. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 195 | language_mode_ = (outer_scope != NULL) |
| 196 | ? outer_scope->language_mode_ : CLASSIC_MODE; |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 197 | outer_scope_calls_non_strict_eval_ = false; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 198 | inner_scope_calls_eval_ = false; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 199 | force_eager_compilation_ = false; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 200 | force_context_allocation_ = (outer_scope != NULL && !is_function_scope()) |
| 201 | ? outer_scope->has_forced_context_allocation() : false; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 202 | num_var_or_const_ = 0; |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 203 | num_stack_slots_ = 0; |
| 204 | num_heap_slots_ = 0; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 205 | num_modules_ = 0; |
| 206 | module_var_ = NULL, |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 207 | scope_info_ = scope_info; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 208 | start_position_ = RelocInfo::kNoPosition; |
| 209 | end_position_ = RelocInfo::kNoPosition; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 210 | if (!scope_info.is_null()) { |
| 211 | scope_calls_eval_ = scope_info->CallsEval(); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 212 | language_mode_ = scope_info->language_mode(); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 213 | } |
sgjesse@chromium.org | 3475509 | 2011-04-07 08:41:03 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 217 | Scope* Scope::DeserializeScopeChain(Context* context, Scope* global_scope, |
| 218 | Zone* zone) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 219 | // Reconstruct the outer scope chain from a closure's context chain. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 220 | Scope* current_scope = NULL; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 221 | Scope* innermost_scope = NULL; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 222 | bool contains_with = false; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 223 | while (!context->IsNativeContext()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 224 | if (context->IsWithContext()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 225 | Scope* with_scope = new(zone) Scope(current_scope, |
| 226 | WITH_SCOPE, |
| 227 | Handle<ScopeInfo>::null(), |
| 228 | zone); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 229 | current_scope = with_scope; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 230 | // All the inner scopes are inside a with. |
| 231 | contains_with = true; |
| 232 | for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { |
| 233 | s->scope_inside_with_ = true; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 234 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 235 | } else if (context->IsGlobalContext()) { |
| 236 | ScopeInfo* scope_info = ScopeInfo::cast(context->extension()); |
| 237 | current_scope = new(zone) Scope(current_scope, |
| 238 | GLOBAL_SCOPE, |
| 239 | Handle<ScopeInfo>(scope_info), |
| 240 | zone); |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 241 | } else if (context->IsModuleContext()) { |
| 242 | ScopeInfo* scope_info = ScopeInfo::cast(context->module()->scope_info()); |
| 243 | current_scope = new(zone) Scope(current_scope, |
| 244 | MODULE_SCOPE, |
| 245 | Handle<ScopeInfo>(scope_info), |
| 246 | zone); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 247 | } else if (context->IsFunctionContext()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 248 | ScopeInfo* scope_info = context->closure()->shared()->scope_info(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 249 | current_scope = new(zone) Scope(current_scope, |
| 250 | FUNCTION_SCOPE, |
| 251 | Handle<ScopeInfo>(scope_info), |
| 252 | zone); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 253 | } else if (context->IsBlockContext()) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 254 | ScopeInfo* scope_info = ScopeInfo::cast(context->extension()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 255 | current_scope = new(zone) Scope(current_scope, |
| 256 | BLOCK_SCOPE, |
| 257 | Handle<ScopeInfo>(scope_info), |
| 258 | zone); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 259 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 260 | ASSERT(context->IsCatchContext()); |
| 261 | String* name = String::cast(context->extension()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 262 | current_scope = new(zone) Scope( |
| 263 | current_scope, Handle<String>(name), zone); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 264 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 265 | if (contains_with) current_scope->RecordWithStatement(); |
| 266 | if (innermost_scope == NULL) innermost_scope = current_scope; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 267 | |
| 268 | // Forget about a with when we move to a context for a different function. |
| 269 | if (context->previous()->closure() != context->closure()) { |
| 270 | contains_with = false; |
| 271 | } |
| 272 | context = context->previous(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 273 | } |
| 274 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 275 | global_scope->AddInnerScope(current_scope); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 276 | global_scope->PropagateScopeInfo(false); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 277 | return (innermost_scope == NULL) ? global_scope : innermost_scope; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 278 | } |
| 279 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 280 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 281 | bool Scope::Analyze(CompilationInfo* info) { |
| 282 | ASSERT(info->function() != NULL); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 283 | Scope* scope = info->function()->scope(); |
| 284 | Scope* top = scope; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 285 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 286 | // Traverse the scope tree up to the first unresolved scope or the global |
| 287 | // scope and start scope resolution and variable allocation from that scope. |
| 288 | while (!top->is_global_scope() && |
| 289 | !top->outer_scope()->already_resolved()) { |
| 290 | top = top->outer_scope(); |
| 291 | } |
| 292 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 293 | // Allocate the variables. |
| 294 | { |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 295 | AstNodeFactory<AstNullVisitor> ast_node_factory(info->isolate(), |
| 296 | info->zone()); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 297 | if (!top->AllocateVariables(info, &ast_node_factory)) return false; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 298 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 299 | |
| 300 | #ifdef DEBUG |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 301 | if (info->isolate()->bootstrapper()->IsActive() |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 302 | ? FLAG_print_builtin_scopes |
| 303 | : FLAG_print_scopes) { |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 304 | scope->Print(); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 305 | } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 306 | |
| 307 | if (FLAG_harmony_modules && FLAG_print_interfaces && top->is_global_scope()) { |
| 308 | PrintF("global : "); |
| 309 | top->interface()->Print(); |
| 310 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 311 | #endif |
| 312 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 313 | info->SetScope(scope); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 314 | return true; |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 318 | void Scope::Initialize() { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 319 | ASSERT(!already_resolved()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 320 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 321 | // Add this scope as a new inner scope of the outer scope. |
| 322 | if (outer_scope_ != NULL) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 323 | outer_scope_->inner_scopes_.Add(this, zone()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 324 | scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 325 | } else { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 326 | scope_inside_with_ = is_with_scope(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Declare convenience variables. |
| 330 | // Declare and allocate receiver (even for the global scope, and even |
| 331 | // if naccesses_ == 0). |
| 332 | // NOTE: When loading parameters in the global scope, we must take |
| 333 | // care not to access them as properties of the global object, but |
| 334 | // instead load them directly from the stack. Currently, the only |
| 335 | // such parameter is 'this' which is passed on the stack when |
| 336 | // invoking scripts |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 337 | if (is_declaration_scope()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 338 | Variable* var = |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 339 | variables_.Declare(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 340 | isolate_->factory()->this_string(), |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 341 | VAR, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 342 | false, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 343 | Variable::THIS, |
| 344 | kCreatedInitialized); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 345 | var->AllocateTo(Variable::PARAMETER, -1); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 346 | receiver_ = var; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 347 | } else { |
| 348 | ASSERT(outer_scope() != NULL); |
| 349 | receiver_ = outer_scope()->receiver(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 350 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 351 | |
| 352 | if (is_function_scope()) { |
| 353 | // Declare 'arguments' variable which exists in all functions. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 354 | // Note that it might never be accessed, in which case it won't be |
| 355 | // allocated during variable allocation. |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 356 | variables_.Declare(this, |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 357 | isolate_->factory()->arguments_string(), |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 358 | VAR, |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 359 | true, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 360 | Variable::ARGUMENTS, |
| 361 | kCreatedInitialized); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 366 | Scope* Scope::FinalizeBlockScope() { |
| 367 | ASSERT(is_block_scope()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 368 | ASSERT(internals_.is_empty()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 369 | ASSERT(temps_.is_empty()); |
| 370 | ASSERT(params_.is_empty()); |
| 371 | |
| 372 | if (num_var_or_const() > 0) return this; |
| 373 | |
| 374 | // Remove this scope from outer scope. |
| 375 | for (int i = 0; i < outer_scope_->inner_scopes_.length(); i++) { |
| 376 | if (outer_scope_->inner_scopes_[i] == this) { |
| 377 | outer_scope_->inner_scopes_.Remove(i); |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // Reparent inner scopes. |
| 383 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 384 | outer_scope()->AddInnerScope(inner_scopes_[i]); |
| 385 | } |
| 386 | |
| 387 | // Move unresolved variables |
| 388 | for (int i = 0; i < unresolved_.length(); i++) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 389 | outer_scope()->unresolved_.Add(unresolved_[i], zone()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 396 | Variable* Scope::LocalLookup(Handle<String> name) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 397 | Variable* result = variables_.Lookup(name); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 398 | if (result != NULL || scope_info_.is_null()) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 399 | return result; |
| 400 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 401 | // If we have a serialized scope info, we might find the variable there. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 402 | // There should be no local slot with the given name. |
erik.corry@gmail.com | 0511e24 | 2011-01-19 11:11:08 +0000 | [diff] [blame] | 403 | ASSERT(scope_info_->StackSlotIndex(*name) < 0); |
| 404 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 405 | // Check context slot lookup. |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 406 | VariableMode mode; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 407 | Variable::Location location = Variable::CONTEXT; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 408 | InitializationFlag init_flag; |
| 409 | int index = scope_info_->ContextSlotIndex(*name, &mode, &init_flag); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 410 | if (index < 0) { |
| 411 | // Check parameters. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 412 | index = scope_info_->ParameterIndex(*name); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 413 | if (index < 0) return NULL; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 414 | |
| 415 | mode = DYNAMIC; |
| 416 | location = Variable::LOOKUP; |
| 417 | init_flag = kCreatedInitialized; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 418 | } |
| 419 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 420 | Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, |
| 421 | init_flag); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 422 | var->AllocateTo(location, index); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 423 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 427 | Variable* Scope::LookupFunctionVar(Handle<String> name, |
| 428 | AstNodeFactory<AstNullVisitor>* factory) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 429 | if (function_ != NULL && function_->proxy()->name().is_identical_to(name)) { |
| 430 | return function_->proxy()->var(); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 431 | } else if (!scope_info_.is_null()) { |
| 432 | // If we are backed by a scope info, try to lookup the variable there. |
| 433 | VariableMode mode; |
| 434 | int index = scope_info_->FunctionContextSlotIndex(*name, &mode); |
| 435 | if (index < 0) return NULL; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 436 | Variable* var = new(zone()) Variable( |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 437 | this, name, mode, true /* is valid LHS */, |
| 438 | Variable::NORMAL, kCreatedInitialized); |
| 439 | VariableProxy* proxy = factory->NewVariableProxy(var); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 440 | VariableDeclaration* declaration = factory->NewVariableDeclaration( |
| 441 | proxy, mode, this, RelocInfo::kNoPosition); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 442 | DeclareFunctionVar(declaration); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 443 | var->AllocateTo(Variable::CONTEXT, index); |
| 444 | return var; |
| 445 | } else { |
| 446 | return NULL; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 451 | Variable* Scope::Lookup(Handle<String> name) { |
| 452 | for (Scope* scope = this; |
| 453 | scope != NULL; |
| 454 | scope = scope->outer_scope()) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 455 | Variable* var = scope->LocalLookup(name); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 456 | if (var != NULL) return var; |
| 457 | } |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 462 | void Scope::DeclareParameter(Handle<String> name, VariableMode mode) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 463 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 464 | ASSERT(is_function_scope()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 465 | Variable* var = variables_.Declare(this, name, mode, true, Variable::NORMAL, |
| 466 | kCreatedInitialized); |
| 467 | params_.Add(var, zone()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 471 | Variable* Scope::DeclareLocal(Handle<String> name, |
| 472 | VariableMode mode, |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 473 | InitializationFlag init_flag, |
| 474 | Interface* interface) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 475 | ASSERT(!already_resolved()); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 476 | // This function handles VAR and CONST modes. DYNAMIC variables are |
| 477 | // introduces during variable allocation, INTERNAL variables are allocated |
| 478 | // explicitly, and TEMPORARY variables are allocated via NewTemporary(). |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 479 | ASSERT(IsDeclaredVariableMode(mode)); |
ricow@chromium.org | c54d365 | 2011-05-30 09:20:16 +0000 | [diff] [blame] | 480 | ++num_var_or_const_; |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 481 | return variables_.Declare( |
| 482 | this, name, mode, true, Variable::NORMAL, init_flag, interface); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 486 | Variable* Scope::DeclareDynamicGlobal(Handle<String> name) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 487 | ASSERT(is_global_scope()); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 488 | return variables_.Declare(this, |
| 489 | name, |
| 490 | DYNAMIC_GLOBAL, |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 491 | true, |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 492 | Variable::NORMAL, |
| 493 | kCreatedInitialized); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | void Scope::RemoveUnresolved(VariableProxy* var) { |
| 498 | // Most likely (always?) any variable we want to remove |
| 499 | // was just added before, so we search backwards. |
| 500 | for (int i = unresolved_.length(); i-- > 0;) { |
| 501 | if (unresolved_[i] == var) { |
| 502 | unresolved_.Remove(i); |
| 503 | return; |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 509 | Variable* Scope::NewInternal(Handle<String> name) { |
| 510 | ASSERT(!already_resolved()); |
| 511 | Variable* var = new(zone()) Variable(this, |
| 512 | name, |
| 513 | INTERNAL, |
| 514 | false, |
| 515 | Variable::NORMAL, |
| 516 | kCreatedInitialized); |
| 517 | internals_.Add(var, zone()); |
| 518 | return var; |
| 519 | } |
| 520 | |
| 521 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 522 | Variable* Scope::NewTemporary(Handle<String> name) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 523 | ASSERT(!already_resolved()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 524 | Variable* var = new(zone()) Variable(this, |
| 525 | name, |
| 526 | TEMPORARY, |
| 527 | true, |
| 528 | Variable::NORMAL, |
| 529 | kCreatedInitialized); |
| 530 | temps_.Add(var, zone()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 531 | return var; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | void Scope::AddDeclaration(Declaration* declaration) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 536 | decls_.Add(declaration, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | |
| 540 | void Scope::SetIllegalRedeclaration(Expression* expression) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 541 | // Record only the first illegal redeclaration. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 542 | if (!HasIllegalRedeclaration()) { |
| 543 | illegal_redecl_ = expression; |
| 544 | } |
| 545 | ASSERT(HasIllegalRedeclaration()); |
| 546 | } |
| 547 | |
| 548 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 549 | void Scope::VisitIllegalRedeclaration(AstVisitor* visitor) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 550 | ASSERT(HasIllegalRedeclaration()); |
| 551 | illegal_redecl_->Accept(visitor); |
| 552 | } |
| 553 | |
| 554 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 555 | Declaration* Scope::CheckConflictingVarDeclarations() { |
| 556 | int length = decls_.length(); |
| 557 | for (int i = 0; i < length; i++) { |
| 558 | Declaration* decl = decls_[i]; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 559 | if (decl->mode() != VAR) continue; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 560 | Handle<String> name = decl->proxy()->name(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 561 | |
| 562 | // Iterate through all scopes until and including the declaration scope. |
| 563 | Scope* previous = NULL; |
| 564 | Scope* current = decl->scope(); |
| 565 | do { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 566 | // There is a conflict if there exists a non-VAR binding. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 567 | Variable* other_var = current->variables_.Lookup(name); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 568 | if (other_var != NULL && other_var->mode() != VAR) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 569 | return decl; |
| 570 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 571 | previous = current; |
| 572 | current = current->outer_scope_; |
| 573 | } while (!previous->is_declaration_scope()); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 574 | } |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 579 | class VarAndOrder { |
| 580 | public: |
| 581 | VarAndOrder(Variable* var, int order) : var_(var), order_(order) { } |
| 582 | Variable* var() const { return var_; } |
| 583 | int order() const { return order_; } |
| 584 | static int Compare(const VarAndOrder* a, const VarAndOrder* b) { |
| 585 | return a->order_ - b->order_; |
| 586 | } |
| 587 | |
| 588 | private: |
| 589 | Variable* var_; |
| 590 | int order_; |
| 591 | }; |
| 592 | |
| 593 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 594 | void Scope::CollectStackAndContextLocals(ZoneList<Variable*>* stack_locals, |
| 595 | ZoneList<Variable*>* context_locals) { |
| 596 | ASSERT(stack_locals != NULL); |
| 597 | ASSERT(context_locals != NULL); |
| 598 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 599 | // Collect internals which are always allocated on the heap. |
| 600 | for (int i = 0; i < internals_.length(); i++) { |
| 601 | Variable* var = internals_[i]; |
| 602 | if (var->is_used()) { |
| 603 | ASSERT(var->IsContextSlot()); |
| 604 | context_locals->Add(var, zone()); |
| 605 | } |
| 606 | } |
| 607 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 608 | // Collect temporaries which are always allocated on the stack, unless the |
| 609 | // context as a whole has forced context allocation. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 610 | for (int i = 0; i < temps_.length(); i++) { |
| 611 | Variable* var = temps_[i]; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 612 | if (var->is_used()) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 613 | if (var->IsContextSlot()) { |
| 614 | ASSERT(has_forced_context_allocation()); |
| 615 | context_locals->Add(var, zone()); |
| 616 | } else { |
| 617 | ASSERT(var->IsStackLocal()); |
| 618 | stack_locals->Add(var, zone()); |
| 619 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 622 | |
| 623 | // Collect declared local variables. |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 624 | ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 625 | for (VariableMap::Entry* p = variables_.Start(); |
| 626 | p != NULL; |
| 627 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 628 | Variable* var = reinterpret_cast<Variable*>(p->value); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 629 | if (var->is_used()) { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 630 | vars.Add(VarAndOrder(var, p->order), zone()); |
| 631 | } |
| 632 | } |
| 633 | vars.Sort(VarAndOrder::Compare); |
| 634 | int var_count = vars.length(); |
| 635 | for (int i = 0; i < var_count; i++) { |
| 636 | Variable* var = vars[i].var(); |
| 637 | if (var->IsStackLocal()) { |
| 638 | stack_locals->Add(var, zone()); |
| 639 | } else if (var->IsContextSlot()) { |
| 640 | context_locals->Add(var, zone()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 646 | bool Scope::AllocateVariables(CompilationInfo* info, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 647 | AstNodeFactory<AstNullVisitor>* factory) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 648 | // 1) Propagate scope information. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 649 | bool outer_scope_calls_non_strict_eval = false; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 650 | if (outer_scope_ != NULL) { |
| 651 | outer_scope_calls_non_strict_eval = |
| 652 | outer_scope_->outer_scope_calls_non_strict_eval() | |
| 653 | outer_scope_->calls_non_strict_eval(); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 654 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 655 | PropagateScopeInfo(outer_scope_calls_non_strict_eval); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 656 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 657 | // 2) Allocate module instances. |
| 658 | if (FLAG_harmony_modules && (is_global_scope() || is_module_scope())) { |
| 659 | ASSERT(num_modules_ == 0); |
| 660 | AllocateModulesRecursively(this); |
| 661 | } |
| 662 | |
| 663 | // 3) Resolve variables. |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 664 | if (!ResolveVariablesRecursively(info, factory)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 665 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 666 | // 4) Allocate variables. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 667 | AllocateVariablesRecursively(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 668 | |
| 669 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 673 | bool Scope::HasTrivialContext() const { |
| 674 | // A function scope has a trivial context if it always is the global |
| 675 | // context. We iteratively scan out the context chain to see if |
| 676 | // there is anything that makes this scope non-trivial; otherwise we |
| 677 | // return true. |
| 678 | for (const Scope* scope = this; scope != NULL; scope = scope->outer_scope_) { |
| 679 | if (scope->is_eval_scope()) return false; |
| 680 | if (scope->scope_inside_with_) return false; |
| 681 | if (scope->num_heap_slots_ > 0) return false; |
| 682 | } |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | |
| 687 | bool Scope::HasTrivialOuterContext() const { |
| 688 | Scope* outer = outer_scope_; |
| 689 | if (outer == NULL) return true; |
| 690 | // Note that the outer context may be trivial in general, but the current |
| 691 | // scope may be inside a 'with' statement in which case the outer context |
| 692 | // for this scope is not trivial. |
| 693 | return !scope_inside_with_ && outer->HasTrivialContext(); |
| 694 | } |
| 695 | |
| 696 | |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 697 | bool Scope::HasLazyCompilableOuterContext() const { |
| 698 | Scope* outer = outer_scope_; |
| 699 | if (outer == NULL) return true; |
rossberg@chromium.org | cddc71f | 2012-12-07 12:40:13 +0000 | [diff] [blame] | 700 | // We have to prevent lazy compilation if this scope is inside a with scope |
| 701 | // and all declaration scopes between them have empty contexts. Such |
| 702 | // declaration scopes may become invisible during scope info deserialization. |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 703 | outer = outer->DeclarationScope(); |
| 704 | bool found_non_trivial_declarations = false; |
| 705 | for (const Scope* scope = outer; scope != NULL; scope = scope->outer_scope_) { |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 706 | if (scope->is_with_scope() && !found_non_trivial_declarations) return false; |
| 707 | if (scope->is_declaration_scope() && scope->num_heap_slots() > 0) { |
| 708 | found_non_trivial_declarations = true; |
| 709 | } |
| 710 | } |
| 711 | return true; |
| 712 | } |
| 713 | |
| 714 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 715 | bool Scope::AllowsLazyCompilation() const { |
mstarzinger@chromium.org | c6d9cee | 2012-07-03 10:03:19 +0000 | [diff] [blame] | 716 | return !force_eager_compilation_ && HasLazyCompilableOuterContext(); |
ulan@chromium.org | d6899c3 | 2012-05-18 14:12:25 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 720 | bool Scope::AllowsLazyCompilationWithoutContext() const { |
| 721 | return !force_eager_compilation_ && HasTrivialOuterContext(); |
| 722 | } |
| 723 | |
| 724 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | int Scope::ContextChainLength(Scope* scope) { |
| 726 | int n = 0; |
| 727 | for (Scope* s = this; s != scope; s = s->outer_scope_) { |
| 728 | ASSERT(s != NULL); // scope must be in the scope chain |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 729 | if (s->is_with_scope() || s->num_heap_slots() > 0) n++; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 730 | // Catch and module scopes always have heap slots. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 731 | ASSERT(!s->is_catch_scope() || s->num_heap_slots() > 0); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 732 | ASSERT(!s->is_module_scope() || s->num_heap_slots() > 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 733 | } |
| 734 | return n; |
| 735 | } |
| 736 | |
| 737 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 738 | Scope* Scope::GlobalScope() { |
| 739 | Scope* scope = this; |
| 740 | while (!scope->is_global_scope()) { |
| 741 | scope = scope->outer_scope(); |
| 742 | } |
| 743 | return scope; |
| 744 | } |
| 745 | |
| 746 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 747 | Scope* Scope::DeclarationScope() { |
| 748 | Scope* scope = this; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 749 | while (!scope->is_declaration_scope()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 750 | scope = scope->outer_scope(); |
| 751 | } |
| 752 | return scope; |
| 753 | } |
| 754 | |
| 755 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 756 | Handle<ScopeInfo> Scope::GetScopeInfo() { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 757 | if (scope_info_.is_null()) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 758 | scope_info_ = ScopeInfo::Create(this, zone()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 759 | } |
| 760 | return scope_info_; |
| 761 | } |
| 762 | |
| 763 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 764 | void Scope::GetNestedScopeChain( |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 765 | List<Handle<ScopeInfo> >* chain, |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 766 | int position) { |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 767 | if (!is_eval_scope()) chain->Add(Handle<ScopeInfo>(GetScopeInfo())); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 768 | |
| 769 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 770 | Scope* scope = inner_scopes_[i]; |
| 771 | int beg_pos = scope->start_position(); |
| 772 | int end_pos = scope->end_position(); |
| 773 | ASSERT(beg_pos >= 0 && end_pos >= 0); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 774 | if (beg_pos <= position && position < end_pos) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 775 | scope->GetNestedScopeChain(chain, position); |
| 776 | return; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 782 | #ifdef DEBUG |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 783 | static const char* Header(ScopeType scope_type) { |
| 784 | switch (scope_type) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 785 | case EVAL_SCOPE: return "eval"; |
| 786 | case FUNCTION_SCOPE: return "function"; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 787 | case MODULE_SCOPE: return "module"; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 788 | case GLOBAL_SCOPE: return "global"; |
| 789 | case CATCH_SCOPE: return "catch"; |
| 790 | case BLOCK_SCOPE: return "block"; |
| 791 | case WITH_SCOPE: return "with"; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 792 | } |
| 793 | UNREACHABLE(); |
| 794 | return NULL; |
| 795 | } |
| 796 | |
| 797 | |
| 798 | static void Indent(int n, const char* str) { |
| 799 | PrintF("%*s%s", n, "", str); |
| 800 | } |
| 801 | |
| 802 | |
| 803 | static void PrintName(Handle<String> name) { |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 804 | SmartArrayPointer<char> s = name->ToCString(DISALLOW_NULLS); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 805 | PrintF("%s", *s); |
| 806 | } |
| 807 | |
| 808 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 809 | static void PrintLocation(Variable* var) { |
| 810 | switch (var->location()) { |
| 811 | case Variable::UNALLOCATED: |
| 812 | break; |
| 813 | case Variable::PARAMETER: |
| 814 | PrintF("parameter[%d]", var->index()); |
| 815 | break; |
| 816 | case Variable::LOCAL: |
| 817 | PrintF("local[%d]", var->index()); |
| 818 | break; |
| 819 | case Variable::CONTEXT: |
| 820 | PrintF("context[%d]", var->index()); |
| 821 | break; |
| 822 | case Variable::LOOKUP: |
| 823 | PrintF("lookup"); |
| 824 | break; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | |
| 829 | static void PrintVar(int indent, Variable* var) { |
| 830 | if (var->is_used() || !var->IsUnallocated()) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 831 | Indent(indent, Variable::Mode2String(var->mode())); |
| 832 | PrintF(" "); |
| 833 | PrintName(var->name()); |
| 834 | PrintF("; // "); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 835 | PrintLocation(var); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 836 | if (var->has_forced_context_allocation()) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 837 | if (!var->IsUnallocated()) PrintF(", "); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 838 | PrintF("forced context allocation"); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 839 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 840 | PrintF("\n"); |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 845 | static void PrintMap(int indent, VariableMap* map) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 846 | for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 847 | Variable* var = reinterpret_cast<Variable*>(p->value); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 848 | PrintVar(indent, var); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 853 | void Scope::Print(int n) { |
| 854 | int n0 = (n > 0 ? n : 0); |
| 855 | int n1 = n0 + 2; // indentation |
| 856 | |
| 857 | // Print header. |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 858 | Indent(n0, Header(scope_type_)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 859 | if (scope_name_->length() > 0) { |
| 860 | PrintF(" "); |
| 861 | PrintName(scope_name_); |
| 862 | } |
| 863 | |
| 864 | // Print parameters, if any. |
| 865 | if (is_function_scope()) { |
| 866 | PrintF(" ("); |
| 867 | for (int i = 0; i < params_.length(); i++) { |
| 868 | if (i > 0) PrintF(", "); |
| 869 | PrintName(params_[i]->name()); |
| 870 | } |
| 871 | PrintF(")"); |
| 872 | } |
| 873 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 874 | PrintF(" { // (%d, %d)\n", start_position(), end_position()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 875 | |
| 876 | // Function name, if any (named function literals, only). |
| 877 | if (function_ != NULL) { |
| 878 | Indent(n1, "// (local) function name: "); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 879 | PrintName(function_->proxy()->name()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 880 | PrintF("\n"); |
| 881 | } |
| 882 | |
| 883 | // Scope info. |
| 884 | if (HasTrivialOuterContext()) { |
| 885 | Indent(n1, "// scope has trivial outer context\n"); |
| 886 | } |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 887 | switch (language_mode()) { |
| 888 | case CLASSIC_MODE: |
| 889 | break; |
| 890 | case STRICT_MODE: |
| 891 | Indent(n1, "// strict mode scope\n"); |
| 892 | break; |
| 893 | case EXTENDED_MODE: |
| 894 | Indent(n1, "// extended mode scope\n"); |
| 895 | break; |
| 896 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 897 | if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
| 898 | if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
| 899 | if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 900 | if (outer_scope_calls_non_strict_eval_) { |
| 901 | Indent(n1, "// outer scope calls 'eval' in non-strict context\n"); |
| 902 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 903 | if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
| 904 | if (num_stack_slots_ > 0) { Indent(n1, "// "); |
| 905 | PrintF("%d stack slots\n", num_stack_slots_); } |
| 906 | if (num_heap_slots_ > 0) { Indent(n1, "// "); |
| 907 | PrintF("%d heap slots\n", num_heap_slots_); } |
| 908 | |
| 909 | // Print locals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 910 | if (function_ != NULL) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 911 | Indent(n1, "// function var:\n"); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 912 | PrintVar(n1, function_->proxy()->var()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 913 | } |
| 914 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 915 | if (temps_.length() > 0) { |
| 916 | Indent(n1, "// temporary vars:\n"); |
| 917 | for (int i = 0; i < temps_.length(); i++) { |
| 918 | PrintVar(n1, temps_[i]); |
| 919 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 920 | } |
| 921 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 922 | if (internals_.length() > 0) { |
| 923 | Indent(n1, "// internal vars:\n"); |
| 924 | for (int i = 0; i < internals_.length(); i++) { |
| 925 | PrintVar(n1, internals_[i]); |
| 926 | } |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 927 | } |
| 928 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 929 | if (variables_.Start() != NULL) { |
| 930 | Indent(n1, "// local vars:\n"); |
| 931 | PrintMap(n1, &variables_); |
| 932 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 933 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 934 | if (dynamics_ != NULL) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 935 | Indent(n1, "// dynamic vars:\n"); |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 936 | PrintMap(n1, dynamics_->GetMap(DYNAMIC)); |
| 937 | PrintMap(n1, dynamics_->GetMap(DYNAMIC_LOCAL)); |
| 938 | PrintMap(n1, dynamics_->GetMap(DYNAMIC_GLOBAL)); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 939 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 940 | |
| 941 | // Print inner scopes (disable by providing negative n). |
| 942 | if (n >= 0) { |
| 943 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 944 | PrintF("\n"); |
| 945 | inner_scopes_[i]->Print(n1); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | Indent(n0, "}\n"); |
| 950 | } |
| 951 | #endif // DEBUG |
| 952 | |
| 953 | |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 954 | Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 955 | if (dynamics_ == NULL) dynamics_ = new(zone()) DynamicScopePart(zone()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 956 | VariableMap* map = dynamics_->GetMap(mode); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 957 | Variable* var = map->Lookup(name); |
| 958 | if (var == NULL) { |
| 959 | // Declare a new non-local. |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 960 | InitializationFlag init_flag = (mode == VAR) |
| 961 | ? kCreatedInitialized : kNeedsInitialization; |
| 962 | var = map->Declare(NULL, |
| 963 | name, |
| 964 | mode, |
| 965 | true, |
| 966 | Variable::NORMAL, |
| 967 | init_flag); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 968 | // Allocate it by giving it a dynamic lookup. |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 969 | var->AllocateTo(Variable::LOOKUP, -1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 970 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 971 | return var; |
| 972 | } |
| 973 | |
| 974 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 975 | Variable* Scope::LookupRecursive(Handle<String> name, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 976 | BindingKind* binding_kind, |
| 977 | AstNodeFactory<AstNullVisitor>* factory) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 978 | ASSERT(binding_kind != NULL); |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 979 | if (already_resolved() && is_with_scope()) { |
| 980 | // Short-cut: if the scope is deserialized from a scope info, variable |
| 981 | // allocation is already fixed. We can simply return with dynamic lookup. |
| 982 | *binding_kind = DYNAMIC_LOOKUP; |
| 983 | return NULL; |
| 984 | } |
| 985 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 986 | // Try to find the variable in this scope. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 987 | Variable* var = LocalLookup(name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 988 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 989 | // We found a variable and we are done. (Even if there is an 'eval' in |
| 990 | // this scope which introduces the same variable again, the resulting |
| 991 | // variable remains the same.) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 992 | if (var != NULL) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 993 | *binding_kind = BOUND; |
| 994 | return var; |
| 995 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 996 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 997 | // We did not find a variable locally. Check against the function variable, |
| 998 | // if any. We can do this for all scopes, since the function variable is |
| 999 | // only present - if at all - for function scopes. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1000 | *binding_kind = UNBOUND; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1001 | var = LookupFunctionVar(name, factory); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1002 | if (var != NULL) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1003 | *binding_kind = BOUND; |
| 1004 | } else if (outer_scope_ != NULL) { |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1005 | var = outer_scope_->LookupRecursive(name, binding_kind, factory); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1006 | if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { |
| 1007 | var->ForceContextAllocation(); |
| 1008 | } |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1009 | } else { |
| 1010 | ASSERT(is_global_scope()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1011 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1012 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1013 | if (is_with_scope()) { |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 1014 | ASSERT(!already_resolved()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1015 | // The current scope is a with scope, so the variable binding can not be |
| 1016 | // statically resolved. However, note that it was necessary to do a lookup |
| 1017 | // in the outer scope anyway, because if a binding exists in an outer scope, |
| 1018 | // the associated variable has to be marked as potentially being accessed |
| 1019 | // from inside of an inner with scope (the property may not be in the 'with' |
| 1020 | // object). |
| 1021 | *binding_kind = DYNAMIC_LOOKUP; |
| 1022 | return NULL; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1023 | } else if (calls_non_strict_eval()) { |
| 1024 | // A variable binding may have been found in an outer scope, but the current |
| 1025 | // scope makes a non-strict 'eval' call, so the found variable may not be |
| 1026 | // the correct one (the 'eval' may introduce a binding with the same name). |
| 1027 | // In that case, change the lookup result to reflect this situation. |
| 1028 | if (*binding_kind == BOUND) { |
| 1029 | *binding_kind = BOUND_EVAL_SHADOWED; |
| 1030 | } else if (*binding_kind == UNBOUND) { |
| 1031 | *binding_kind = UNBOUND_EVAL_SHADOWED; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1032 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1033 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1034 | return var; |
| 1035 | } |
| 1036 | |
| 1037 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1038 | bool Scope::ResolveVariable(CompilationInfo* info, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1039 | VariableProxy* proxy, |
| 1040 | AstNodeFactory<AstNullVisitor>* factory) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1041 | ASSERT(info->global_scope()->is_global_scope()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1042 | |
| 1043 | // If the proxy is already resolved there's nothing to do |
| 1044 | // (functions and consts may be resolved by the parser). |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1045 | if (proxy->var() != NULL) return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Otherwise, try to resolve the variable. |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1048 | BindingKind binding_kind; |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1049 | Variable* var = LookupRecursive(proxy->name(), &binding_kind, factory); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1050 | switch (binding_kind) { |
| 1051 | case BOUND: |
| 1052 | // We found a variable binding. |
| 1053 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1054 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1055 | case BOUND_EVAL_SHADOWED: |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1056 | // We either found a variable binding that might be shadowed by eval or |
| 1057 | // gave up on it (e.g. by encountering a local with the same in the outer |
| 1058 | // scope which was not promoted to a context, this can happen if we use |
| 1059 | // debugger to evaluate arbitrary expressions at a break point). |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1060 | if (var->IsGlobalObjectProperty()) { |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1061 | var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1062 | } else if (var->is_dynamic()) { |
| 1063 | var = NonLocal(proxy->name(), DYNAMIC); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1064 | } else { |
| 1065 | Variable* invalidated = var; |
| 1066 | var = NonLocal(proxy->name(), DYNAMIC_LOCAL); |
| 1067 | var->set_local_if_not_shadowed(invalidated); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1068 | } |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1069 | break; |
| 1070 | |
| 1071 | case UNBOUND: |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1072 | // No binding has been found. Declare a variable on the global object. |
| 1073 | var = info->global_scope()->DeclareDynamicGlobal(proxy->name()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1074 | break; |
| 1075 | |
| 1076 | case UNBOUND_EVAL_SHADOWED: |
| 1077 | // No binding has been found. But some scope makes a |
| 1078 | // non-strict 'eval' call. |
| 1079 | var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); |
| 1080 | break; |
| 1081 | |
| 1082 | case DYNAMIC_LOOKUP: |
| 1083 | // The variable could not be resolved statically. |
| 1084 | var = NonLocal(proxy->name(), DYNAMIC); |
| 1085 | break; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1088 | ASSERT(var != NULL); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1089 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1090 | if (FLAG_harmony_scoping && is_extended_mode() && |
| 1091 | var->is_const_mode() && proxy->IsLValue()) { |
| 1092 | // Assignment to const. Throw a syntax error. |
| 1093 | MessageLocation location( |
| 1094 | info->script(), proxy->position(), proxy->position()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1095 | Isolate* isolate = info->isolate(); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1096 | Factory* factory = isolate->factory(); |
| 1097 | Handle<JSArray> array = factory->NewJSArray(0); |
| 1098 | Handle<Object> result = |
| 1099 | factory->NewSyntaxError("harmony_const_assign", array); |
| 1100 | isolate->Throw(*result, &location); |
| 1101 | return false; |
| 1102 | } |
| 1103 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1104 | if (FLAG_harmony_modules) { |
| 1105 | bool ok; |
| 1106 | #ifdef DEBUG |
| 1107 | if (FLAG_print_interface_details) |
| 1108 | PrintF("# Resolve %s:\n", var->name()->ToAsciiArray()); |
| 1109 | #endif |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 1110 | proxy->interface()->Unify(var->interface(), zone(), &ok); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1111 | if (!ok) { |
| 1112 | #ifdef DEBUG |
| 1113 | if (FLAG_print_interfaces) { |
| 1114 | PrintF("SCOPES TYPE ERROR\n"); |
| 1115 | PrintF("proxy: "); |
| 1116 | proxy->interface()->Print(); |
| 1117 | PrintF("var: "); |
| 1118 | var->interface()->Print(); |
| 1119 | } |
| 1120 | #endif |
| 1121 | |
| 1122 | // Inconsistent use of module. Throw a syntax error. |
| 1123 | // TODO(rossberg): generate more helpful error message. |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1124 | MessageLocation location( |
| 1125 | info->script(), proxy->position(), proxy->position()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 1126 | Isolate* isolate = info->isolate(); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1127 | Factory* factory = isolate->factory(); |
| 1128 | Handle<JSArray> array = factory->NewJSArray(1); |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1129 | USE(JSObject::SetElement(array, 0, var->name(), NONE, kStrictMode)); |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1130 | Handle<Object> result = |
| 1131 | factory->NewSyntaxError("module_type_error", array); |
| 1132 | isolate->Throw(*result, &location); |
| 1133 | return false; |
| 1134 | } |
| 1135 | } |
| 1136 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1137 | proxy->BindTo(var); |
| 1138 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1139 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1143 | bool Scope::ResolveVariablesRecursively( |
| 1144 | CompilationInfo* info, |
svenpanne@chromium.org | b1df11d | 2012-02-08 10:26:21 +0000 | [diff] [blame] | 1145 | AstNodeFactory<AstNullVisitor>* factory) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1146 | ASSERT(info->global_scope()->is_global_scope()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Resolve unresolved variables for this scope. |
| 1149 | for (int i = 0; i < unresolved_.length(); i++) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1150 | if (!ResolveVariable(info, unresolved_[i], factory)) return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // Resolve unresolved variables for inner scopes. |
| 1154 | for (int i = 0; i < inner_scopes_.length(); i++) { |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1155 | if (!inner_scopes_[i]->ResolveVariablesRecursively(info, factory)) |
| 1156 | return false; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1157 | } |
erik.corry@gmail.com | bbceb57 | 2012-03-09 10:52:05 +0000 | [diff] [blame] | 1158 | |
| 1159 | return true; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1163 | bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1164 | if (outer_scope_calls_non_strict_eval) { |
| 1165 | outer_scope_calls_non_strict_eval_ = true; |
| 1166 | } |
| 1167 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1168 | bool calls_non_strict_eval = |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 1169 | this->calls_non_strict_eval() || outer_scope_calls_non_strict_eval_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1170 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1171 | Scope* inner_scope = inner_scopes_[i]; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 1172 | if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1173 | inner_scope_calls_eval_ = true; |
| 1174 | } |
| 1175 | if (inner_scope->force_eager_compilation_) { |
| 1176 | force_eager_compilation_ = true; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return scope_calls_eval_ || inner_scope_calls_eval_; |
| 1181 | } |
| 1182 | |
| 1183 | |
| 1184 | bool Scope::MustAllocate(Variable* var) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1185 | // Give var a read/write use if there is a chance it might be accessed |
| 1186 | // via an eval() call. This is only possible if the variable has a |
| 1187 | // visible name. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | if ((var->is_this() || var->name()->length() > 0) && |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1189 | (var->has_forced_context_allocation() || |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1190 | scope_calls_eval_ || |
| 1191 | inner_scope_calls_eval_ || |
| 1192 | scope_contains_with_ || |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 1193 | is_catch_scope() || |
danno@chromium.org | 81cac2b | 2012-07-10 11:28:27 +0000 | [diff] [blame] | 1194 | is_block_scope() || |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1195 | is_module_scope() || |
| 1196 | is_global_scope())) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 1197 | var->set_is_used(true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1198 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1199 | // Global variables do not need to be allocated. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1200 | return !var->IsGlobalObjectProperty() && var->is_used(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | bool Scope::MustAllocateInContext(Variable* var) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1205 | // If var is accessed from an inner scope, or if there is a possibility |
| 1206 | // that it might be accessed from the current or an inner scope (through |
| 1207 | // an eval() call or a runtime with lookup), it must be allocated in the |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1208 | // context. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1209 | // |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1210 | // Exceptions: If the scope as a whole has forced context allocation, all |
| 1211 | // variables will have context allocation, even temporaries. Otherwise |
| 1212 | // temporary variables are always stack-allocated. Catch-bound variables are |
| 1213 | // always context-allocated. |
| 1214 | if (has_forced_context_allocation()) return true; |
rossberg@chromium.org | b4b2aa6 | 2011-10-13 09:49:59 +0000 | [diff] [blame] | 1215 | if (var->mode() == TEMPORARY) return false; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1216 | if (var->mode() == INTERNAL) return true; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1217 | if (is_catch_scope() || is_block_scope() || is_module_scope()) return true; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1218 | if (is_global_scope() && IsLexicalVariableMode(var->mode())) return true; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1219 | return var->has_forced_context_allocation() || |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1220 | scope_calls_eval_ || |
| 1221 | inner_scope_calls_eval_ || |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 1222 | scope_contains_with_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | bool Scope::HasArgumentsParameter() { |
| 1227 | for (int i = 0; i < params_.length(); i++) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1228 | if (params_[i]->name().is_identical_to( |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1229 | isolate_->factory()->arguments_string())) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | return true; |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1231 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1232 | } |
| 1233 | return false; |
| 1234 | } |
| 1235 | |
| 1236 | |
| 1237 | void Scope::AllocateStackSlot(Variable* var) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1238 | var->AllocateTo(Variable::LOCAL, num_stack_slots_++); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | |
| 1242 | void Scope::AllocateHeapSlot(Variable* var) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1243 | var->AllocateTo(Variable::CONTEXT, num_heap_slots_++); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | void Scope::AllocateParameterLocals() { |
| 1248 | ASSERT(is_function_scope()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1249 | Variable* arguments = LocalLookup(isolate_->factory()->arguments_string()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1250 | ASSERT(arguments != NULL); // functions have 'arguments' declared implicitly |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1251 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1252 | bool uses_nonstrict_arguments = false; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1253 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1254 | if (MustAllocate(arguments) && !HasArgumentsParameter()) { |
| 1255 | // 'arguments' is used. Unless there is also a parameter called |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1256 | // 'arguments', we must be conservative and allocate all parameters to |
| 1257 | // the context assuming they will be captured by the arguments object. |
| 1258 | // If we have a parameter named 'arguments', a (new) value is always |
| 1259 | // assigned to it via the function invocation. Then 'arguments' denotes |
| 1260 | // that specific parameter value and cannot be used to access the |
| 1261 | // parameters, which is why we don't need to allocate an arguments |
| 1262 | // object in that case. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1263 | |
| 1264 | // We are using 'arguments'. Tell the code generator that is needs to |
| 1265 | // allocate the arguments object by setting 'arguments_'. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 1266 | arguments_ = arguments; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1267 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1268 | // In strict mode 'arguments' does not alias formal parameters. |
| 1269 | // Therefore in strict mode we allocate parameters as if 'arguments' |
| 1270 | // were not used. |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 1271 | uses_nonstrict_arguments = is_classic_mode(); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1274 | // The same parameter may occur multiple times in the parameters_ list. |
| 1275 | // If it does, and if it is not copied into the context object, it must |
| 1276 | // receive the highest parameter index for that parameter; thus iteration |
| 1277 | // order is relevant! |
| 1278 | for (int i = params_.length() - 1; i >= 0; --i) { |
| 1279 | Variable* var = params_[i]; |
| 1280 | ASSERT(var->scope() == this); |
| 1281 | if (uses_nonstrict_arguments) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1282 | // Force context allocation of the parameter. |
| 1283 | var->ForceContextAllocation(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1286 | if (MustAllocate(var)) { |
| 1287 | if (MustAllocateInContext(var)) { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1288 | ASSERT(var->IsUnallocated() || var->IsContextSlot()); |
| 1289 | if (var->IsUnallocated()) { |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1290 | AllocateHeapSlot(var); |
| 1291 | } |
| 1292 | } else { |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1293 | ASSERT(var->IsUnallocated() || var->IsParameter()); |
| 1294 | if (var->IsUnallocated()) { |
| 1295 | var->AllocateTo(Variable::PARAMETER, i); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | |
| 1303 | void Scope::AllocateNonParameterLocal(Variable* var) { |
| 1304 | ASSERT(var->scope() == this); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1305 | ASSERT(!var->IsVariable(isolate_->factory()->result_string()) || |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 1306 | !var->IsStackLocal()); |
| 1307 | if (var->IsUnallocated() && MustAllocate(var)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1308 | if (MustAllocateInContext(var)) { |
| 1309 | AllocateHeapSlot(var); |
| 1310 | } else { |
| 1311 | AllocateStackSlot(var); |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1317 | void Scope::AllocateNonParameterLocals() { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1318 | // All variables that have no rewrite yet are non-parameter locals. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1319 | for (int i = 0; i < temps_.length(); i++) { |
| 1320 | AllocateNonParameterLocal(temps_[i]); |
| 1321 | } |
| 1322 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1323 | for (int i = 0; i < internals_.length(); i++) { |
| 1324 | AllocateNonParameterLocal(internals_[i]); |
| 1325 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1326 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1327 | ZoneList<VarAndOrder> vars(variables_.occupancy(), zone()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1328 | for (VariableMap::Entry* p = variables_.Start(); |
| 1329 | p != NULL; |
| 1330 | p = variables_.Next(p)) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1331 | Variable* var = reinterpret_cast<Variable*>(p->value); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1332 | vars.Add(VarAndOrder(var, p->order), zone()); |
| 1333 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 1334 | vars.Sort(VarAndOrder::Compare); |
| 1335 | int var_count = vars.length(); |
| 1336 | for (int i = 0; i < var_count; i++) { |
| 1337 | AllocateNonParameterLocal(vars[i].var()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 1340 | // For now, function_ must be allocated at the very end. If it gets |
| 1341 | // allocated in the context, it must be the last slot in the context, |
| 1342 | // because of the current ScopeInfo implementation (see |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1343 | // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). |
| 1344 | if (function_ != NULL) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1345 | AllocateNonParameterLocal(function_->proxy()->var()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | void Scope::AllocateVariablesRecursively() { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1351 | // Allocate variables for inner scopes. |
| 1352 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1353 | inner_scopes_[i]->AllocateVariablesRecursively(); |
| 1354 | } |
| 1355 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1356 | // If scope is already resolved, we still need to allocate |
| 1357 | // variables in inner scopes which might not had been resolved yet. |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1358 | if (already_resolved()) return; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1359 | // The number of slots required for variables. |
| 1360 | num_stack_slots_ = 0; |
| 1361 | num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
| 1362 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1363 | // Allocate variables for this scope. |
| 1364 | // Parameters must be allocated first, if any. |
| 1365 | if (is_function_scope()) AllocateParameterLocals(); |
| 1366 | AllocateNonParameterLocals(); |
| 1367 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1368 | // Force allocation of a context for this scope if necessary. For a 'with' |
| 1369 | // scope and for a function scope that makes an 'eval' call we need a context, |
| 1370 | // even if no local variables were statically allocated in the scope. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1371 | // Likewise for modules. |
| 1372 | bool must_have_context = is_with_scope() || is_module_scope() || |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1373 | (is_function_scope() && calls_eval()); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1374 | |
| 1375 | // If we didn't allocate any locals in the local context, then we only |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1376 | // need the minimal number of slots if we must have a context. |
| 1377 | if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1378 | num_heap_slots_ = 0; |
| 1379 | } |
| 1380 | |
| 1381 | // Allocation done. |
| 1382 | ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 1383 | } |
| 1384 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1385 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1386 | void Scope::AllocateModulesRecursively(Scope* host_scope) { |
| 1387 | if (already_resolved()) return; |
| 1388 | if (is_module_scope()) { |
| 1389 | ASSERT(interface_->IsFrozen()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 1390 | Handle<String> name = isolate_->factory()->InternalizeOneByteString( |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1391 | STATIC_ASCII_VECTOR(".module")); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 1392 | ASSERT(module_var_ == NULL); |
| 1393 | module_var_ = host_scope->NewInternal(name); |
| 1394 | ++host_scope->num_modules_; |
| 1395 | } |
| 1396 | |
| 1397 | for (int i = 0; i < inner_scopes_.length(); i++) { |
| 1398 | Scope* inner_scope = inner_scopes_.at(i); |
| 1399 | inner_scope->AllocateModulesRecursively(host_scope); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1404 | int Scope::StackLocalCount() const { |
| 1405 | return num_stack_slots() - |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1406 | (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | int Scope::ContextLocalCount() const { |
| 1411 | if (num_heap_slots() == 0) return 0; |
| 1412 | return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1413 | (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1416 | } } // namespace v8::internal |