| 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 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 if (!IsFilteredIdentifier(name)) { | 281 if (!IsFilteredIdentifier(name)) { |
| 282 kind = RawLocalVarDescriptors::kContextVar; | 282 kind = RawLocalVarDescriptors::kContextVar; |
| 283 } else { | 283 } else { |
| 284 continue; | 284 continue; |
| 285 } | 285 } |
| 286 | 286 |
| 287 VarDesc desc; | 287 VarDesc desc; |
| 288 desc.name = &name; | 288 desc.name = &name; |
| 289 desc.info.set_kind(kind); | 289 desc.info.set_kind(kind); |
| 290 desc.info.scope_id = context_scope.ContextLevelAt(i); | 290 desc.info.scope_id = context_scope.ContextLevelAt(i); |
| 291 desc.info.declaration_pos = context_scope.DeclarationTokenIndexAt(i); |
| 291 desc.info.begin_pos = begin_token_pos(); | 292 desc.info.begin_pos = begin_token_pos(); |
| 292 desc.info.end_pos = end_token_pos(); | 293 desc.info.end_pos = end_token_pos(); |
| 293 ASSERT(desc.info.begin_pos <= desc.info.end_pos); | 294 ASSERT(desc.info.begin_pos <= desc.info.end_pos); |
| 294 desc.info.set_index(context_scope.ContextIndexAt(i)); | 295 desc.info.set_index(context_scope.ContextIndexAt(i)); |
| 295 vars.Add(desc); | 296 vars.Add(desc); |
| 296 } | 297 } |
| 297 } | 298 } |
| 298 // Now collect all variables from local scopes. | 299 // Now collect all variables from local scopes. |
| 299 int16_t scope_id = 0; | 300 int16_t scope_id = 0; |
| 300 CollectLocalVariables(&vars, &scope_id); | 301 CollectLocalVariables(&vars, &scope_id); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 329 for (int i = 0; i < this->variables_.length(); i++) { | 330 for (int i = 0; i < this->variables_.length(); i++) { |
| 330 LocalVariable* var = variables_[i]; | 331 LocalVariable* var = variables_[i]; |
| 331 if ((var->owner() == this) && !var->is_invisible()) { | 332 if ((var->owner() == this) && !var->is_invisible()) { |
| 332 if (var->name().raw() == Symbols::CurrentContextVar().raw()) { | 333 if (var->name().raw() == Symbols::CurrentContextVar().raw()) { |
| 333 // This is the local variable in which the function saves its | 334 // This is the local variable in which the function saves its |
| 334 // own context before calling a closure function. | 335 // own context before calling a closure function. |
| 335 VarDesc desc; | 336 VarDesc desc; |
| 336 desc.name = &var->name(); | 337 desc.name = &var->name(); |
| 337 desc.info.set_kind(RawLocalVarDescriptors::kSavedCurrentContext); | 338 desc.info.set_kind(RawLocalVarDescriptors::kSavedCurrentContext); |
| 338 desc.info.scope_id = 0; | 339 desc.info.scope_id = 0; |
| 340 desc.info.declaration_pos = TokenPosition::kMinSource; |
| 339 desc.info.begin_pos = TokenPosition::kMinSource; | 341 desc.info.begin_pos = TokenPosition::kMinSource; |
| 340 desc.info.end_pos = TokenPosition::kMinSource; | 342 desc.info.end_pos = TokenPosition::kMinSource; |
| 341 desc.info.set_index(var->index()); | 343 desc.info.set_index(var->index()); |
| 342 vars->Add(desc); | 344 vars->Add(desc); |
| 343 } else if (!IsFilteredIdentifier(var->name())) { | 345 } else if (!IsFilteredIdentifier(var->name())) { |
| 344 // This is a regular Dart variable, either stack-based or captured. | 346 // This is a regular Dart variable, either stack-based or captured. |
| 345 VarDesc desc; | 347 VarDesc desc; |
| 346 desc.name = &var->name(); | 348 desc.name = &var->name(); |
| 347 if (var->is_captured()) { | 349 if (var->is_captured()) { |
| 348 desc.info.set_kind(RawLocalVarDescriptors::kContextVar); | 350 desc.info.set_kind(RawLocalVarDescriptors::kContextVar); |
| 349 ASSERT(var->owner() != NULL); | 351 ASSERT(var->owner() != NULL); |
| 350 ASSERT(var->owner()->context_level() >= 0); | 352 ASSERT(var->owner()->context_level() >= 0); |
| 351 desc.info.scope_id = var->owner()->context_level(); | 353 desc.info.scope_id = var->owner()->context_level(); |
| 352 } else { | 354 } else { |
| 353 desc.info.set_kind(RawLocalVarDescriptors::kStackVar); | 355 desc.info.set_kind(RawLocalVarDescriptors::kStackVar); |
| 354 desc.info.scope_id = *scope_id; | 356 desc.info.scope_id = *scope_id; |
| 355 } | 357 } |
| 358 desc.info.declaration_pos = var->declaration_token_pos(); |
| 356 desc.info.begin_pos = var->token_pos(); | 359 desc.info.begin_pos = var->token_pos(); |
| 357 desc.info.end_pos = var->owner()->end_token_pos(); | 360 desc.info.end_pos = var->owner()->end_token_pos(); |
| 358 desc.info.set_index(var->index()); | 361 desc.info.set_index(var->index()); |
| 359 vars->Add(desc); | 362 vars->Add(desc); |
| 360 } | 363 } |
| 361 } | 364 } |
| 362 } | 365 } |
| 363 LocalScope* child = this->child(); | 366 LocalScope* child = this->child(); |
| 364 while (child != NULL) { | 367 while (child != NULL) { |
| 365 child->CollectLocalVariables(vars, scope_id); | 368 child->CollectLocalVariables(vars, scope_id); |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 544 ContextScope::Handle(ContextScope::New(num_captured_vars, false)); | 547 ContextScope::Handle(ContextScope::New(num_captured_vars, false)); |
| 545 | 548 |
| 546 // Create a descriptor for each referenced captured variable of enclosing | 549 // Create a descriptor for each referenced captured variable of enclosing |
| 547 // functions to preserve its name and its context allocation information. | 550 // functions to preserve its name and its context allocation information. |
| 548 int captured_idx = 0; | 551 int captured_idx = 0; |
| 549 for (int i = 0; i < num_variables(); i++) { | 552 for (int i = 0; i < num_variables(); i++) { |
| 550 LocalVariable* variable = VariableAt(i); | 553 LocalVariable* variable = VariableAt(i); |
| 551 // Preserve the aliases of captured variables belonging to outer scopes. | 554 // Preserve the aliases of captured variables belonging to outer scopes. |
| 552 if (variable->owner()->function_level() != 1) { | 555 if (variable->owner()->function_level() != 1) { |
| 553 context_scope.SetTokenIndexAt(captured_idx, variable->token_pos()); | 556 context_scope.SetTokenIndexAt(captured_idx, variable->token_pos()); |
| 557 context_scope.SetDeclarationTokenIndexAt( |
| 558 captured_idx, variable->declaration_token_pos()); |
| 554 context_scope.SetNameAt(captured_idx, variable->name()); | 559 context_scope.SetNameAt(captured_idx, variable->name()); |
| 555 context_scope.SetIsFinalAt(captured_idx, variable->is_final()); | 560 context_scope.SetIsFinalAt(captured_idx, variable->is_final()); |
| 556 context_scope.SetIsConstAt(captured_idx, variable->IsConst()); | 561 context_scope.SetIsConstAt(captured_idx, variable->IsConst()); |
| 557 if (variable->IsConst()) { | 562 if (variable->IsConst()) { |
| 558 context_scope.SetConstValueAt(captured_idx, *variable->ConstValue()); | 563 context_scope.SetConstValueAt(captured_idx, *variable->ConstValue()); |
| 559 } else { | 564 } else { |
| 560 context_scope.SetTypeAt(captured_idx, variable->type()); | 565 context_scope.SetTypeAt(captured_idx, variable->type()); |
| 561 } | 566 } |
| 562 context_scope.SetContextIndexAt(captured_idx, variable->index()); | 567 context_scope.SetContextIndexAt(captured_idx, variable->index()); |
| 563 // Adjust the context level relative to the current context level, | 568 // Adjust the context level relative to the current context level, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 575 | 580 |
| 576 | 581 |
| 577 LocalScope* LocalScope::RestoreOuterScope(const ContextScope& context_scope) { | 582 LocalScope* LocalScope::RestoreOuterScope(const ContextScope& context_scope) { |
| 578 // The function level of the outer scope is one less than the function level | 583 // The function level of the outer scope is one less than the function level |
| 579 // of the current function, which is 0. | 584 // of the current function, which is 0. |
| 580 LocalScope* outer_scope = new LocalScope(NULL, -1, 0); | 585 LocalScope* outer_scope = new LocalScope(NULL, -1, 0); |
| 581 // Add all variables as aliases to the outer scope. | 586 // Add all variables as aliases to the outer scope. |
| 582 for (int i = 0; i < context_scope.num_variables(); i++) { | 587 for (int i = 0; i < context_scope.num_variables(); i++) { |
| 583 LocalVariable* variable; | 588 LocalVariable* variable; |
| 584 if (context_scope.IsConstAt(i)) { | 589 if (context_scope.IsConstAt(i)) { |
| 585 variable = new LocalVariable(context_scope.TokenIndexAt(i), | 590 variable = new LocalVariable( |
| 591 context_scope.DeclarationTokenIndexAt(i), |
| 592 context_scope.TokenIndexAt(i), |
| 586 String::ZoneHandle(context_scope.NameAt(i)), | 593 String::ZoneHandle(context_scope.NameAt(i)), |
| 587 Object::dynamic_type()); | 594 Object::dynamic_type()); |
| 588 variable->SetConstValue( | 595 variable->SetConstValue( |
| 589 Instance::ZoneHandle(context_scope.ConstValueAt(i))); | 596 Instance::ZoneHandle(context_scope.ConstValueAt(i))); |
| 590 } else { | 597 } else { |
| 591 variable = new LocalVariable(context_scope.TokenIndexAt(i), | 598 variable = new LocalVariable( |
| 599 context_scope.DeclarationTokenIndexAt(i), |
| 600 context_scope.TokenIndexAt(i), |
| 592 String::ZoneHandle(context_scope.NameAt(i)), | 601 String::ZoneHandle(context_scope.NameAt(i)), |
| 593 AbstractType::ZoneHandle(context_scope.TypeAt(i))); | 602 AbstractType::ZoneHandle(context_scope.TypeAt(i))); |
| 594 } | 603 } |
| 595 variable->set_is_captured(); | 604 variable->set_is_captured(); |
| 596 variable->set_index(context_scope.ContextIndexAt(i)); | 605 variable->set_index(context_scope.ContextIndexAt(i)); |
| 597 if (context_scope.IsFinalAt(i)) { | 606 if (context_scope.IsFinalAt(i)) { |
| 598 variable->set_is_final(); | 607 variable->set_is_final(); |
| 599 } | 608 } |
| 600 // Create a fake owner scope describing the index and context level of the | 609 // Create a fake owner scope describing the index and context level of the |
| 601 // variable. Function level and loop level are unused (set to 0), since | 610 // variable. Function level and loop level are unused (set to 0), since |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 633 | 642 |
| 634 RawContextScope* LocalScope::CreateImplicitClosureScope(const Function& func) { | 643 RawContextScope* LocalScope::CreateImplicitClosureScope(const Function& func) { |
| 635 static const intptr_t kNumCapturedVars = 1; | 644 static const intptr_t kNumCapturedVars = 1; |
| 636 | 645 |
| 637 // Create a ContextScope with space for kNumCapturedVars descriptors. | 646 // Create a ContextScope with space for kNumCapturedVars descriptors. |
| 638 const ContextScope& context_scope = | 647 const ContextScope& context_scope = |
| 639 ContextScope::Handle(ContextScope::New(kNumCapturedVars, true)); | 648 ContextScope::Handle(ContextScope::New(kNumCapturedVars, true)); |
| 640 | 649 |
| 641 // Create a descriptor for 'this' variable. | 650 // Create a descriptor for 'this' variable. |
| 642 context_scope.SetTokenIndexAt(0, func.token_pos()); | 651 context_scope.SetTokenIndexAt(0, func.token_pos()); |
| 652 context_scope.SetDeclarationTokenIndexAt(0, func.token_pos()); |
| 643 context_scope.SetNameAt(0, Symbols::This()); | 653 context_scope.SetNameAt(0, Symbols::This()); |
| 644 context_scope.SetIsFinalAt(0, true); | 654 context_scope.SetIsFinalAt(0, true); |
| 645 context_scope.SetIsConstAt(0, false); | 655 context_scope.SetIsConstAt(0, false); |
| 646 const AbstractType& type = AbstractType::Handle(func.ParameterTypeAt(0)); | 656 const AbstractType& type = AbstractType::Handle(func.ParameterTypeAt(0)); |
| 647 context_scope.SetTypeAt(0, type); | 657 context_scope.SetTypeAt(0, type); |
| 648 context_scope.SetContextIndexAt(0, 0); | 658 context_scope.SetContextIndexAt(0, 0); |
| 649 context_scope.SetContextLevelAt(0, 0); | 659 context_scope.SetContextLevelAt(0, 0); |
| 650 ASSERT(context_scope.num_variables() == kNumCapturedVars); // Verify count. | 660 ASSERT(context_scope.num_variables() == kNumCapturedVars); // Verify count. |
| 651 return context_scope.raw(); | 661 return context_scope.raw(); |
| 652 } | 662 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 677 return fixed_parameter_count - (index() - kParamEndSlotFromFp); | 687 return fixed_parameter_count - (index() - kParamEndSlotFromFp); |
| 678 } else { | 688 } else { |
| 679 // Shift negative indexes so that the lowest one is 0 (they are still | 689 // Shift negative indexes so that the lowest one is 0 (they are still |
| 680 // non-positive). | 690 // non-positive). |
| 681 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); | 691 return fixed_parameter_count - (index() - kFirstLocalSlotFromFp); |
| 682 } | 692 } |
| 683 } | 693 } |
| 684 | 694 |
| 685 | 695 |
| 686 } // namespace dart | 696 } // namespace dart |
| OLD | NEW |