Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: runtime/vm/parser.cc

Issue 1980193002: Fix capturing variables in optimized compilations (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/flow_graph_builder.cc ('k') | tests/language/regress_26453_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/parser.h" 5 #include "vm/parser.h"
6 #include "vm/flags.h" 6 #include "vm/flags.h"
7 7
8 #ifndef DART_PRECOMPILED_RUNTIME 8 #ifndef DART_PRECOMPILED_RUNTIME
9 9
10 #include "lib/invocation_mirror.h" 10 #include "lib/invocation_mirror.h"
(...skipping 7594 matching lines...) Expand 10 before | Expand all | Expand 10 after
7605 } 7605 }
7606 } 7606 }
7607 CheckToken(Token::kLPAREN); 7607 CheckToken(Token::kLPAREN);
7608 7608
7609 // Check whether we have parsed this closure function before, in a previous 7609 // Check whether we have parsed this closure function before, in a previous
7610 // compilation. If so, reuse the function object, else create a new one 7610 // compilation. If so, reuse the function object, else create a new one
7611 // and register it in the current class. 7611 // and register it in the current class.
7612 // Note that we cannot share the same closure function between the closurized 7612 // Note that we cannot share the same closure function between the closurized
7613 // and non-closurized versions of the same parent function. 7613 // and non-closurized versions of the same parent function.
7614 Function& function = Function::ZoneHandle(Z); 7614 Function& function = Function::ZoneHandle(Z);
7615 bool found_func = true;
7615 // TODO(hausner): There could be two different closures at the given 7616 // TODO(hausner): There could be two different closures at the given
7616 // function_pos, one enclosed in a closurized function and one enclosed in the 7617 // function_pos, one enclosed in a closurized function and one enclosed in the
7617 // non-closurized version of this same function. 7618 // non-closurized version of this same function.
7618 function = I->LookupClosureFunction(innermost_function(), function_pos); 7619 function = I->LookupClosureFunction(innermost_function(), function_pos);
7619 if (function.IsNull()) { 7620 if (function.IsNull()) {
7620 // The function will be registered in the lookup table by the 7621 // The function will be registered in the lookup table by the
7621 // EffectGraphVisitor::VisitClosureNode when the newly allocated closure 7622 // EffectGraphVisitor::VisitClosureNode when the newly allocated closure
7622 // function has been properly setup. 7623 // function has been properly setup.
7624 found_func = false;
7623 function = Function::NewClosureFunction(*function_name, 7625 function = Function::NewClosureFunction(*function_name,
7624 innermost_function(), 7626 innermost_function(),
7625 function_pos); 7627 function_pos);
7626 function.set_result_type(result_type); 7628 function.set_result_type(result_type);
7627 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { 7629 if (FLAG_enable_mirrors && metadata_pos.IsReal()) {
7628 library_.AddFunctionMetadata(function, metadata_pos); 7630 library_.AddFunctionMetadata(function, metadata_pos);
7629 } 7631 }
7630 } 7632 }
7631 7633
7632 // The function type needs to be finalized at compile time, since the closure 7634 // The function type needs to be finalized at compile time, since the closure
(...skipping 29 matching lines...) Expand all
7662 current_block_->scope->LookupVariable(function_variable->name(), 7664 current_block_->scope->LookupVariable(function_variable->name(),
7663 true); 7665 true);
7664 ASSERT(existing_var != NULL); 7666 ASSERT(existing_var != NULL);
7665 // Use before define cases have already been detected and reported above. 7667 // Use before define cases have already been detected and reported above.
7666 ASSERT(existing_var->owner() == current_block_->scope); 7668 ASSERT(existing_var->owner() == current_block_->scope);
7667 ReportError(function_pos, "identifier '%s' already defined", 7669 ReportError(function_pos, "identifier '%s' already defined",
7668 function_variable->name().ToCString()); 7670 function_variable->name().ToCString());
7669 } 7671 }
7670 } 7672 }
7671 7673
7672 // Parse the local function.
7673 SequenceNode* statements = Parser::ParseFunc(function, !is_literal);
7674 INC_STAT(thread(), num_functions_parsed, 1);
7675 7674
7676 // Now that the local function has formal parameters, lookup the signature 7675 Type& signature_type = Type::ZoneHandle(Z);
7677 Type& signature_type = Type::ZoneHandle(Z, function.SignatureType()); 7676 SequenceNode* statements = NULL;
7678 signature_type ^= ClassFinalizer::FinalizeType( 7677 if (!found_func) {
7679 current_class(), signature_type, ClassFinalizer::kCanonicalize); 7678 // Parse the local function. As a side effect of the parsing, the
7680 function.SetSignatureType(signature_type); 7679 // variables of this function's scope that are referenced by the local
7680 // function (and its inner nested functions) will be marked as captured.
7681 statements = Parser::ParseFunc(function, !is_literal);
7682 INC_STAT(thread(), num_functions_parsed, 1);
7683
7684 // Now that the local function has formal parameters, lookup the signature
7685 signature_type = function.SignatureType();
7686 signature_type ^= ClassFinalizer::FinalizeType(
7687 current_class(), signature_type, ClassFinalizer::kCanonicalize);
7688 function.SetSignatureType(signature_type);
7689 } else {
7690 // The local function was parsed before. The captured variables are
7691 // saved in the function's context scope. Iterate over the context scope
7692 // and mark its variables as captured.
7693 const ContextScope& context_scope =
7694 ContextScope::Handle(Z, function.context_scope());
7695 ASSERT(!context_scope.IsNull());
7696 String& var_name = String::Handle(Z);
7697 for (int i = 0; i < context_scope.num_variables(); i++) {
7698 var_name = context_scope.NameAt(i);
7699 LocalVariable* v = LookupLocalScope(var_name);
7700 ASSERT(v != NULL);
7701 current_block_->scope->CaptureVariable(v);
7702 }
7703 SkipFunctionLiteral();
7704 signature_type = function.SignatureType();
7705 }
7681 7706
7682 // Local functions are registered in the enclosing class, but 7707 // Local functions are registered in the enclosing class, but
7683 // ignored during class finalization. The enclosing class has 7708 // ignored during class finalization. The enclosing class has
7684 // already been finalized. 7709 // already been finalized.
7685 ASSERT(current_class().is_finalized()); 7710 ASSERT(current_class().is_finalized());
7686 ASSERT(signature_type.IsFinalized()); 7711 ASSERT(signature_type.IsFinalized());
7687 7712
7688 // Make sure that the instantiator is captured. 7713 // Make sure that the instantiator is captured.
7689 if ((current_block_->scope->function_level() > 0) && 7714 if ((current_block_->scope->function_level() > 0) &&
7690 Class::Handle(signature_type.type_class()).IsGeneric()) { 7715 Class::Handle(signature_type.type_class()).IsGeneric()) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
7725 // captured. The captured variables will be recorded along with their 7750 // captured. The captured variables will be recorded along with their
7726 // allocation information in a Scope object stored in the function object. 7751 // allocation information in a Scope object stored in the function object.
7727 // This Scope object is then provided to the compiler when compiling the local 7752 // This Scope object is then provided to the compiler when compiling the local
7728 // function. It would be too early to record the captured variables here, 7753 // function. It would be too early to record the captured variables here,
7729 // since further closure functions may capture more variables. 7754 // since further closure functions may capture more variables.
7730 // This Scope object is constructed after all variables have been allocated. 7755 // This Scope object is constructed after all variables have been allocated.
7731 // The local scope of the parsed function can be pruned, since contained 7756 // The local scope of the parsed function can be pruned, since contained
7732 // variables are not relevant for the compilation of the enclosing function. 7757 // variables are not relevant for the compilation of the enclosing function.
7733 // This pruning is done by omitting to hook the local scope in its parent 7758 // This pruning is done by omitting to hook the local scope in its parent
7734 // scope in the constructor of LocalScope. 7759 // scope in the constructor of LocalScope.
7735 AstNode* closure = new(Z) ClosureNode( 7760 AstNode* closure =
7736 function_pos, function, NULL, statements->scope()); 7761 new(Z) ClosureNode(function_pos, function, NULL,
7762 statements != NULL ? statements->scope() : NULL);
7737 7763
7738 if (function_variable == NULL) { 7764 if (function_variable == NULL) {
7739 ASSERT(is_literal); 7765 ASSERT(is_literal);
7740 return closure; 7766 return closure;
7741 } else { 7767 } else {
7742 AstNode* initialization = new(Z) StoreLocalNode( 7768 AstNode* initialization = new(Z) StoreLocalNode(
7743 function_pos, function_variable, closure); 7769 function_pos, function_variable, closure);
7744 return initialization; 7770 return initialization;
7745 } 7771 }
7746 } 7772 }
(...skipping 6793 matching lines...) Expand 10 before | Expand all | Expand 10 after
14540 const ArgumentListNode& function_args, 14566 const ArgumentListNode& function_args,
14541 const LocalVariable* temp_for_last_arg, 14567 const LocalVariable* temp_for_last_arg,
14542 bool is_super_invocation) { 14568 bool is_super_invocation) {
14543 UNREACHABLE(); 14569 UNREACHABLE();
14544 return NULL; 14570 return NULL;
14545 } 14571 }
14546 14572
14547 } // namespace dart 14573 } // namespace dart
14548 14574
14549 #endif // DART_PRECOMPILED_RUNTIME 14575 #endif // DART_PRECOMPILED_RUNTIME
OLDNEW
« no previous file with comments | « runtime/vm/flow_graph_builder.cc ('k') | tests/language/regress_26453_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698