| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/scopes.h" | 5 #include "vm/scopes.h" |
| 6 | 6 |
| 7 #include "vm/object.h" | 7 #include "vm/object.h" |
| 8 #include "vm/stack_frame.h" | 8 #include "vm/stack_frame.h" |
| 9 #include "vm/symbols.h" | 9 #include "vm/symbols.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 | 23 |
| 24 LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level) | 24 LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level) |
| 25 : parent_(parent), | 25 : parent_(parent), |
| 26 child_(NULL), | 26 child_(NULL), |
| 27 sibling_(NULL), | 27 sibling_(NULL), |
| 28 function_level_(function_level), | 28 function_level_(function_level), |
| 29 loop_level_(loop_level), | 29 loop_level_(loop_level), |
| 30 context_level_(LocalScope::kUnitializedContextLevel), | 30 context_level_(LocalScope::kUnitializedContextLevel), |
| 31 num_context_variables_(0), | 31 num_context_variables_(0), |
| 32 begin_token_pos_(Token::kNoSourcePos), | 32 begin_token_pos_(TokenDescriptor::kNoSourcePos), |
| 33 end_token_pos_(Token::kNoSourcePos), | 33 end_token_pos_(TokenDescriptor::kNoSourcePos), |
| 34 variables_(), | 34 variables_(), |
| 35 labels_(), | 35 labels_(), |
| 36 referenced_() { | 36 referenced_() { |
| 37 // Hook this node into the children of the parent, unless the parent has a | 37 // Hook this node into the children of the parent, unless the parent has a |
| 38 // different function_level, since the local scope of a nested function can | 38 // different function_level, since the local scope of a nested function can |
| 39 // be discarded after it has been parsed. | 39 // be discarded after it has been parsed. |
| 40 if ((parent != NULL) && (parent->function_level() == function_level)) { | 40 if ((parent != NULL) && (parent->function_level() == function_level)) { |
| 41 sibling_ = parent->child_; | 41 sibling_ = parent->child_; |
| 42 parent->child_ = this; | 42 parent->child_ = this; |
| 43 } | 43 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 intptr_t num_references = referenced_.length(); | 103 intptr_t num_references = referenced_.length(); |
| 104 for (intptr_t i = 0; i < num_references; i++) { | 104 for (intptr_t i = 0; i < num_references; i++) { |
| 105 if (name.raw() == referenced_[i]->name().raw()) { | 105 if (name.raw() == referenced_[i]->name().raw()) { |
| 106 return referenced_[i]; | 106 return referenced_[i]; |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 return NULL; | 109 return NULL; |
| 110 } | 110 } |
| 111 | 111 |
| 112 | 112 |
| 113 void LocalScope::AddReferencedName(intptr_t token_pos, | 113 void LocalScope::AddReferencedName(TokenDescriptor token_pos, |
| 114 const String& name) { | 114 const String& name) { |
| 115 if (LocalLookupVariable(name) != NULL) { | 115 if (LocalLookupVariable(name) != NULL) { |
| 116 return; | 116 return; |
| 117 } | 117 } |
| 118 NameReference* ref = FindReference(name); | 118 NameReference* ref = FindReference(name); |
| 119 if (ref != NULL) { | 119 if (ref != NULL) { |
| 120 ref->set_token_pos(token_pos); | 120 ref->set_token_pos(token_pos); |
| 121 return; | 121 return; |
| 122 } | 122 } |
| 123 ref = new NameReference(token_pos, name); | 123 ref = new NameReference(token_pos, name); |
| 124 referenced_.Add(ref); | 124 referenced_.Add(ref); |
| 125 // Add name reference in innermost enclosing scopes that do not | 125 // Add name reference in innermost enclosing scopes that do not |
| 126 // define a local variable with this name. | 126 // define a local variable with this name. |
| 127 LocalScope* scope = this->parent(); | 127 LocalScope* scope = this->parent(); |
| 128 while (scope != NULL && (scope->LocalLookupVariable(name) == NULL)) { | 128 while (scope != NULL && (scope->LocalLookupVariable(name) == NULL)) { |
| 129 scope->referenced_.Add(ref); | 129 scope->referenced_.Add(ref); |
| 130 scope = scope->parent(); | 130 scope = scope->parent(); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 | 134 |
| 135 intptr_t LocalScope::PreviousReferencePos(const String& name) const { | 135 TokenDescriptor LocalScope::PreviousReferencePos(const String& name) const { |
| 136 NameReference* ref = FindReference(name); | 136 NameReference* ref = FindReference(name); |
| 137 if (ref != NULL) { | 137 if (ref != NULL) { |
| 138 return ref->token_pos(); | 138 return ref->token_pos(); |
| 139 } | 139 } |
| 140 return -1; | 140 return TokenDescriptor::kNoSource; |
| 141 } | 141 } |
| 142 | 142 |
| 143 | 143 |
| 144 void LocalScope::AllocateContextVariable(LocalVariable* variable, | 144 void LocalScope::AllocateContextVariable(LocalVariable* variable, |
| 145 LocalScope** context_owner) { | 145 LocalScope** context_owner) { |
| 146 ASSERT(variable->is_captured()); | 146 ASSERT(variable->is_captured()); |
| 147 ASSERT(variable->owner() == this); | 147 ASSERT(variable->owner() == this); |
| 148 // The context level in the owner scope of a captured variable indicates at | 148 // The context level in the owner scope of a captured variable indicates at |
| 149 // code generation time how far to walk up the context chain in order to | 149 // code generation time how far to walk up the context chain in order to |
| 150 // access the variable from the current context level. | 150 // access the variable from the current context level. |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 for (int i = 0; i < this->variables_.length(); i++) { | 321 for (int i = 0; i < this->variables_.length(); i++) { |
| 322 LocalVariable* var = variables_[i]; | 322 LocalVariable* var = variables_[i]; |
| 323 if ((var->owner() == this) && !var->is_invisible()) { | 323 if ((var->owner() == this) && !var->is_invisible()) { |
| 324 if (var->name().raw() == Symbols::CurrentContextVar().raw()) { | 324 if (var->name().raw() == Symbols::CurrentContextVar().raw()) { |
| 325 // This is the local variable in which the function saves its | 325 // This is the local variable in which the function saves its |
| 326 // own context before calling a closure function. | 326 // own context before calling a closure function. |
| 327 VarDesc desc; | 327 VarDesc desc; |
| 328 desc.name = &var->name(); | 328 desc.name = &var->name(); |
| 329 desc.info.set_kind(RawLocalVarDescriptors::kSavedCurrentContext); | 329 desc.info.set_kind(RawLocalVarDescriptors::kSavedCurrentContext); |
| 330 desc.info.scope_id = 0; | 330 desc.info.scope_id = 0; |
| 331 desc.info.begin_pos = 0; | 331 desc.info.begin_pos = TokenDescriptor::kMinSource; |
| 332 desc.info.end_pos = 0; | 332 desc.info.end_pos = TokenDescriptor::kMinSource; |
| 333 desc.info.set_index(var->index()); | 333 desc.info.set_index(var->index()); |
| 334 vars->Add(desc); | 334 vars->Add(desc); |
| 335 } else if (!IsFilteredIdentifier(var->name())) { | 335 } else if (!IsFilteredIdentifier(var->name())) { |
| 336 // This is a regular Dart variable, either stack-based or captured. | 336 // This is a regular Dart variable, either stack-based or captured. |
| 337 VarDesc desc; | 337 VarDesc desc; |
| 338 desc.name = &var->name(); | 338 desc.name = &var->name(); |
| 339 if (var->is_captured()) { | 339 if (var->is_captured()) { |
| 340 desc.info.set_kind(RawLocalVarDescriptors::kContextVar); | 340 desc.info.set_kind(RawLocalVarDescriptors::kContextVar); |
| 341 ASSERT(var->owner() != NULL); | 341 ASSERT(var->owner() != NULL); |
| 342 ASSERT(var->owner()->context_level() >= 0); | 342 ASSERT(var->owner()->context_level() >= 0); |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 668 return fixed_parameter_count - (index() - kParamEndSlotFromFp); | 668 return fixed_parameter_count - (index() - kParamEndSlotFromFp); |
| 669 } else { | 669 } else { |
| 670 // Shift negative indexes so that the lowest one is 0 (they are still | 670 // Shift negative indexes so that the lowest one is 0 (they are still |
| 671 // non-positive). | 671 // non-positive). |
| 672 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); | 672 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); |
| 673 } | 673 } |
| 674 } | 674 } |
| 675 | 675 |
| 676 | 676 |
| 677 } // namespace dart | 677 } // namespace dart |
| OLD | NEW |