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