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