| 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 { |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 if ((child_context_owner != *context_owner) && | 166 if ((child_context_owner != *context_owner) && |
| 167 (child_context_owner->loop_level() <= loop_owner->loop_level())) { | 167 (child_context_owner->loop_level() <= loop_owner->loop_level())) { |
| 168 *context_owner = child_context_owner; | 168 *context_owner = child_context_owner; |
| 169 } | 169 } |
| 170 child = child->sibling(); | 170 child = child->sibling(); |
| 171 } | 171 } |
| 172 return min_frame_index; | 172 return min_frame_index; |
| 173 } | 173 } |
| 174 | 174 |
| 175 | 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() { | 176 RawLocalVarDescriptors* LocalScope::GetVarDescriptors() { |
| 185 GrowableArray<LocalVariable*> vars(8); | 177 GrowableArray<LocalVariable*> vars(8); |
| 178 // Variables of each scope are guaranteed to be consecutive elements |
| 179 // in array vars. See CollectLocalVariables() below. The outermost |
| 180 // scope (containing the function parameters) has id 0. |
| 186 CollectLocalVariables(&vars); | 181 CollectLocalVariables(&vars); |
| 187 vars.Sort(&CompareVariableRanges); | |
| 188 const LocalVarDescriptors& var_desc = | 182 const LocalVarDescriptors& var_desc = |
| 189 LocalVarDescriptors::Handle(LocalVarDescriptors::New(vars.length())); | 183 LocalVarDescriptors::Handle(LocalVarDescriptors::New(vars.length())); |
| 184 intptr_t scope_id = -1; |
| 185 LocalScope* current_scope = NULL; |
| 190 for (int i = 0; i < vars.length(); i++) { | 186 for (int i = 0; i < vars.length(); i++) { |
| 191 LocalVariable* var = vars[i]; | 187 LocalVariable* var = vars[i]; |
| 188 if (current_scope != var->owner()) { |
| 189 current_scope = var->owner(); |
| 190 scope_id += 1; |
| 191 } |
| 192 var_desc.SetVar(i, var->name(), var->index(), | 192 var_desc.SetVar(i, var->name(), var->index(), |
| 193 var->token_index(), var->owner()->end_token_index()); | 193 scope_id, var->token_index(), var->owner()->end_token_index()); |
| 194 } | 194 } |
| 195 return var_desc.raw(); | 195 return var_desc.raw(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 | 198 |
| 199 // Add variables that are declared in this scope to vars, then collect |
| 200 // variables of children, followed by siblings. |
| 199 void LocalScope::CollectLocalVariables(GrowableArray<LocalVariable*>* vars) { | 201 void LocalScope::CollectLocalVariables(GrowableArray<LocalVariable*>* vars) { |
| 200 for (int i = 0; i < this->variables_.length(); i++) { | 202 for (int i = 0; i < this->variables_.length(); i++) { |
| 201 LocalVariable* var = variables_[i]; | 203 LocalVariable* var = variables_[i]; |
| 202 if ((var->owner() == this) && Scanner::IsIdent(var->name())) { | 204 if ((var->owner() == this) && Scanner::IsIdent(var->name())) { |
| 203 vars->Add(this->variables_[i]); | 205 vars->Add(this->variables_[i]); |
| 204 } | 206 } |
| 205 } | 207 } |
| 206 if (child() != NULL) { | 208 if (child() != NULL) { |
| 207 child()->CollectLocalVariables(vars); | 209 child()->CollectLocalVariables(vars); |
| 208 } | 210 } |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 } | 461 } |
| 460 if (owner()->context_level() == other.owner()->context_level()) { | 462 if (owner()->context_level() == other.owner()->context_level()) { |
| 461 return true; | 463 return true; |
| 462 } | 464 } |
| 463 } | 465 } |
| 464 } | 466 } |
| 465 return false; | 467 return false; |
| 466 } | 468 } |
| 467 | 469 |
| 468 } // namespace dart | 470 } // namespace dart |
| OLD | NEW |