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/ast.h" | 7 #include "vm/ast.h" |
8 #include "vm/bit_vector.h" | 8 #include "vm/bit_vector.h" |
9 #include "vm/object.h" | 9 #include "vm/object.h" |
10 #include "vm/parser.h" | 10 #include "vm/parser.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 : parent_(parent), | 23 : parent_(parent), |
24 child_(NULL), | 24 child_(NULL), |
25 sibling_(NULL), | 25 sibling_(NULL), |
26 function_level_(function_level), | 26 function_level_(function_level), |
27 loop_level_(loop_level), | 27 loop_level_(loop_level), |
28 context_level_(LocalScope::kUnitializedContextLevel), | 28 context_level_(LocalScope::kUnitializedContextLevel), |
29 num_context_variables_(0), | 29 num_context_variables_(0), |
30 begin_token_pos_(0), | 30 begin_token_pos_(0), |
31 end_token_pos_(0), | 31 end_token_pos_(0), |
32 variables_(), | 32 variables_(), |
33 labels_() { | 33 labels_(), |
| 34 referenced_() { |
34 // Hook this node into the children of the parent, unless the parent has a | 35 // Hook this node into the children of the parent, unless the parent has a |
35 // different function_level, since the local scope of a nested function can | 36 // different function_level, since the local scope of a nested function can |
36 // be discarded after it has been parsed. | 37 // be discarded after it has been parsed. |
37 if ((parent != NULL) && (parent->function_level() == function_level)) { | 38 if ((parent != NULL) && (parent->function_level() == function_level)) { |
38 sibling_ = parent->child_; | 39 sibling_ = parent->child_; |
39 parent->child_ = this; | 40 parent->child_ = this; |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 | 44 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 labels_.Add(label); | 76 labels_.Add(label); |
76 if (label->owner() == NULL) { | 77 if (label->owner() == NULL) { |
77 // Labels must be added to their owner scope first. Subsequent calls | 78 // Labels must be added to their owner scope first. Subsequent calls |
78 // to 'add' treat the label as an alias. | 79 // to 'add' treat the label as an alias. |
79 label->set_owner(this); | 80 label->set_owner(this); |
80 } | 81 } |
81 return true; | 82 return true; |
82 } | 83 } |
83 | 84 |
84 | 85 |
| 86 NameReference* LocalScope::FindReference(const String& name) const { |
| 87 intptr_t num_references = referenced_.length(); |
| 88 for (intptr_t i = 0; i < num_references; i++) { |
| 89 if (name.Equals(referenced_[i]->name())) { |
| 90 return referenced_[i]; |
| 91 } |
| 92 } |
| 93 return NULL; |
| 94 } |
| 95 |
| 96 |
| 97 void LocalScope::AddReferencedName(intptr_t token_pos, |
| 98 const String& name) { |
| 99 if (LocalLookupVariable(name) != NULL) { |
| 100 return; |
| 101 } |
| 102 NameReference* ref = FindReference(name); |
| 103 if (ref != NULL) { |
| 104 ref->set_token_pos(token_pos); |
| 105 return; |
| 106 } |
| 107 ref = new NameReference(token_pos, name); |
| 108 referenced_.Add(ref); |
| 109 // Add name reference in innermost enclosing scopes that do not |
| 110 // define a local variable with this name. |
| 111 LocalScope* scope = this->parent(); |
| 112 while (scope != NULL && (scope->LocalLookupVariable(name) == NULL)) { |
| 113 scope->referenced_.Add(ref); |
| 114 scope = scope->parent(); |
| 115 } |
| 116 } |
| 117 |
| 118 |
| 119 intptr_t LocalScope::PreviousReferencePos(const String& name) const { |
| 120 NameReference* ref = FindReference(name); |
| 121 if (ref != NULL) { |
| 122 return ref->token_pos(); |
| 123 } |
| 124 return -1; |
| 125 } |
| 126 |
| 127 |
85 void LocalScope::AllocateContextVariable(LocalVariable* variable, | 128 void LocalScope::AllocateContextVariable(LocalVariable* variable, |
86 LocalScope** context_owner) { | 129 LocalScope** context_owner) { |
87 ASSERT(variable->is_captured()); | 130 ASSERT(variable->is_captured()); |
88 ASSERT(variable->owner()->loop_level() == loop_level()); | 131 ASSERT(variable->owner()->loop_level() == loop_level()); |
89 if (num_context_variables_ == 0) { | 132 if (num_context_variables_ == 0) { |
90 // This scope will allocate and chain a new context. | 133 // This scope will allocate and chain a new context. |
91 int new_context_level = ((*context_owner) == NULL) ? | 134 int new_context_level = ((*context_owner) == NULL) ? |
92 1 : (*context_owner)->context_level() + 1; | 135 1 : (*context_owner)->context_level() + 1; |
93 // This scope becomes the current context owner. | 136 // This scope becomes the current context owner. |
94 *context_owner = this; | 137 *context_owner = this; |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
600 return fixed_parameter_count - (index() - kParamEndSlotFromFp); | 643 return fixed_parameter_count - (index() - kParamEndSlotFromFp); |
601 } else { | 644 } else { |
602 // Shift negative indexes so that the lowest one is 0 (they are still | 645 // Shift negative indexes so that the lowest one is 0 (they are still |
603 // non-positive). | 646 // non-positive). |
604 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); | 647 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); |
605 } | 648 } |
606 } | 649 } |
607 | 650 |
608 | 651 |
609 } // namespace dart | 652 } // namespace dart |
OLD | NEW |