Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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/object.h" | 8 #include "vm/object.h" |
| 9 | 9 |
| 10 namespace dart { | 10 namespace dart { |
| 11 | 11 |
| 12 const char* SourceLabel::kDefaultLabelName = ":L"; | 12 const char* SourceLabel::kDefaultLabelName = ":L"; |
| 13 | 13 |
| 14 | 14 |
| 15 int SourceLabel::FunctionLevel() const { | 15 int SourceLabel::FunctionLevel() const { |
| 16 ASSERT(owner() != NULL); | 16 ASSERT(owner() != NULL); |
| 17 return owner()->function_level(); | 17 return owner()->function_level(); |
| 18 } | 18 } |
| 19 | 19 |
| 20 | 20 |
| 21 LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level) | 21 LocalScope::LocalScope(LocalScope* parent, int function_level, int loop_level) |
| 22 : parent_(parent), | 22 : parent_(parent), |
| 23 child_(NULL), | 23 child_(NULL), |
| 24 sibling_(NULL), | 24 sibling_(NULL), |
| 25 function_level_(function_level), | 25 function_level_(function_level), |
| 26 loop_level_(loop_level), | 26 loop_level_(loop_level), |
| 27 context_level_(LocalScope::kUnitializedContextLevel_), | 27 context_level_(LocalScope::kUnitializedContextLevel_), |
| 28 num_context_variables_(0), | 28 num_context_variables_(0), |
| 29 end_token_index_(0), | |
| 29 variables_(), | 30 variables_(), |
| 30 labels_() { | 31 labels_() { |
| 31 // Hook this node into the children of the parent, unless the parent has a | 32 // Hook this node into the children of the parent, unless the parent has a |
| 32 // different function_level, since the local scope of a nested function can | 33 // different function_level, since the local scope of a nested function can |
| 33 // be discarded after it has been parsed. | 34 // be discarded after it has been parsed. |
| 34 if ((parent != NULL) && (parent->function_level() == function_level)) { | 35 if ((parent != NULL) && (parent->function_level() == function_level)) { |
| 35 sibling_ = parent->child_; | 36 sibling_ = parent->child_; |
| 36 parent->child_ = this; | 37 parent->child_ = this; |
| 37 } | 38 } |
| 38 } | 39 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 if ((child_context_owner != *context_owner) && | 166 if ((child_context_owner != *context_owner) && |
| 166 (child_context_owner->loop_level() <= loop_owner->loop_level())) { | 167 (child_context_owner->loop_level() <= loop_owner->loop_level())) { |
| 167 *context_owner = child_context_owner; | 168 *context_owner = child_context_owner; |
| 168 } | 169 } |
| 169 child = child->sibling(); | 170 child = child->sibling(); |
| 170 } | 171 } |
| 171 return min_frame_index; | 172 return min_frame_index; |
| 172 } | 173 } |
| 173 | 174 |
| 174 | 175 |
| 176 static int CompareVariableRanges( | |
| 177 LocalVariable* const* a, LocalVariable* const* b) { | |
| 178 if ((*a)->token_index() < (*b)->token_index()) return -1; | |
| 179 if ((*a)->token_index() > (*b)->token_index()) return 1; | |
| 180 return 0; | |
| 181 } | |
| 182 | |
| 183 | |
| 184 RawLocalVarDescriptors* LocalScope::GetVarDescriptors() { | |
| 185 GrowableArray<LocalVariable*> vars(8); | |
| 186 CollectLocalVariables(&vars); | |
| 187 vars.Sort(&CompareVariableRanges); | |
| 188 const LocalVarDescriptors& var_desc = | |
| 189 LocalVarDescriptors::Handle(LocalVarDescriptors::New(vars.length())); | |
| 190 for (int i = 0; i < vars.length(); i++) { | |
| 191 LocalVariable *var = vars[i]; | |
|
siva
2011/12/20 19:11:26
LocalVariable* var = vars[i];
hausner
2011/12/20 21:43:55
Done.
| |
| 192 var_desc.SetVar(i, var->name(), var->index(), | |
| 193 var->token_index(), var->owner()->end_token_index()); | |
| 194 } | |
| 195 return var_desc.raw(); | |
| 196 } | |
| 197 | |
| 198 | |
| 199 void LocalScope::CollectLocalVariables(GrowableArray<LocalVariable*>* vars) { | |
| 200 for (int i = 0; i < this->variables_.length(); i++) { | |
| 201 LocalVariable* var = variables_[i]; | |
| 202 if ((var->owner() == this) && Scanner::IsIdent(var->name())) { | |
| 203 vars->Add(this->variables_[i]); | |
| 204 } | |
| 205 } | |
| 206 if (child() != NULL) { | |
| 207 child()->CollectLocalVariables(vars); | |
| 208 } | |
| 209 if (sibling() != NULL) { | |
| 210 sibling()->CollectLocalVariables(vars); | |
| 211 } | |
| 212 } | |
| 213 | |
| 214 | |
| 175 SourceLabel* LocalScope::LocalLookupLabel(const String& name) const { | 215 SourceLabel* LocalScope::LocalLookupLabel(const String& name) const { |
| 176 for (intptr_t i = 0; i < labels_.length(); i++) { | 216 for (intptr_t i = 0; i < labels_.length(); i++) { |
| 177 SourceLabel* label = labels_[i]; | 217 SourceLabel* label = labels_[i]; |
| 178 if (label->name().Equals(name)) { | 218 if (label->name().Equals(name)) { |
| 179 return label; | 219 return label; |
| 180 } | 220 } |
| 181 } | 221 } |
| 182 return NULL; | 222 return NULL; |
| 183 } | 223 } |
| 184 | 224 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 } | 459 } |
| 420 if (owner()->context_level() == other.owner()->context_level()) { | 460 if (owner()->context_level() == other.owner()->context_level()) { |
| 421 return true; | 461 return true; |
| 422 } | 462 } |
| 423 } | 463 } |
| 424 } | 464 } |
| 425 return false; | 465 return false; |
| 426 } | 466 } |
| 427 | 467 |
| 428 } // namespace dart | 468 } // namespace dart |
| OLD | NEW |