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