| 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/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 Loading... |
| 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; | |
| 7616 // TODO(hausner): There could be two different closures at the given | 7615 // TODO(hausner): There could be two different closures at the given |
| 7617 // function_pos, one enclosed in a closurized function and one enclosed in the | 7616 // function_pos, one enclosed in a closurized function and one enclosed in the |
| 7618 // non-closurized version of this same function. | 7617 // non-closurized version of this same function. |
| 7619 function = I->LookupClosureFunction(innermost_function(), function_pos); | 7618 function = I->LookupClosureFunction(innermost_function(), function_pos); |
| 7620 if (function.IsNull()) { | 7619 if (function.IsNull()) { |
| 7621 // The function will be registered in the lookup table by the | 7620 // The function will be registered in the lookup table by the |
| 7622 // EffectGraphVisitor::VisitClosureNode when the newly allocated closure | 7621 // EffectGraphVisitor::VisitClosureNode when the newly allocated closure |
| 7623 // function has been properly setup. | 7622 // function has been properly setup. |
| 7624 found_func = false; | |
| 7625 function = Function::NewClosureFunction(*function_name, | 7623 function = Function::NewClosureFunction(*function_name, |
| 7626 innermost_function(), | 7624 innermost_function(), |
| 7627 function_pos); | 7625 function_pos); |
| 7628 function.set_result_type(result_type); | 7626 function.set_result_type(result_type); |
| 7629 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { | 7627 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { |
| 7630 library_.AddFunctionMetadata(function, metadata_pos); | 7628 library_.AddFunctionMetadata(function, metadata_pos); |
| 7631 } | 7629 } |
| 7632 } | 7630 } |
| 7633 | 7631 |
| 7634 // The function type needs to be finalized at compile time, since the closure | 7632 // The function type needs to be finalized at compile time, since the closure |
| (...skipping 29 matching lines...) Expand all Loading... |
| 7664 current_block_->scope->LookupVariable(function_variable->name(), | 7662 current_block_->scope->LookupVariable(function_variable->name(), |
| 7665 true); | 7663 true); |
| 7666 ASSERT(existing_var != NULL); | 7664 ASSERT(existing_var != NULL); |
| 7667 // Use before define cases have already been detected and reported above. | 7665 // Use before define cases have already been detected and reported above. |
| 7668 ASSERT(existing_var->owner() == current_block_->scope); | 7666 ASSERT(existing_var->owner() == current_block_->scope); |
| 7669 ReportError(function_pos, "identifier '%s' already defined", | 7667 ReportError(function_pos, "identifier '%s' already defined", |
| 7670 function_variable->name().ToCString()); | 7668 function_variable->name().ToCString()); |
| 7671 } | 7669 } |
| 7672 } | 7670 } |
| 7673 | 7671 |
| 7672 // Parse the local function. |
| 7673 SequenceNode* statements = Parser::ParseFunc(function, !is_literal); |
| 7674 INC_STAT(thread(), num_functions_parsed, 1); |
| 7674 | 7675 |
| 7675 Type& signature_type = Type::ZoneHandle(Z); | 7676 // Now that the local function has formal parameters, lookup the signature |
| 7676 SequenceNode* statements = NULL; | 7677 Type& signature_type = Type::ZoneHandle(Z, function.SignatureType()); |
| 7677 if (!found_func) { | 7678 signature_type ^= ClassFinalizer::FinalizeType( |
| 7678 // Parse the local function. As a side effect of the parsing, the | 7679 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
| 7679 // variables of this function's scope that are referenced by the local | 7680 function.SetSignatureType(signature_type); |
| 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 } | |
| 7706 | 7681 |
| 7707 // Local functions are registered in the enclosing class, but | 7682 // Local functions are registered in the enclosing class, but |
| 7708 // ignored during class finalization. The enclosing class has | 7683 // ignored during class finalization. The enclosing class has |
| 7709 // already been finalized. | 7684 // already been finalized. |
| 7710 ASSERT(current_class().is_finalized()); | 7685 ASSERT(current_class().is_finalized()); |
| 7711 ASSERT(signature_type.IsFinalized()); | 7686 ASSERT(signature_type.IsFinalized()); |
| 7712 | 7687 |
| 7713 // Make sure that the instantiator is captured. | 7688 // Make sure that the instantiator is captured. |
| 7714 if ((current_block_->scope->function_level() > 0) && | 7689 if ((current_block_->scope->function_level() > 0) && |
| 7715 Class::Handle(signature_type.type_class()).IsGeneric()) { | 7690 Class::Handle(signature_type.type_class()).IsGeneric()) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7750 // captured. The captured variables will be recorded along with their | 7725 // captured. The captured variables will be recorded along with their |
| 7751 // allocation information in a Scope object stored in the function object. | 7726 // allocation information in a Scope object stored in the function object. |
| 7752 // This Scope object is then provided to the compiler when compiling the local | 7727 // This Scope object is then provided to the compiler when compiling the local |
| 7753 // function. It would be too early to record the captured variables here, | 7728 // function. It would be too early to record the captured variables here, |
| 7754 // since further closure functions may capture more variables. | 7729 // since further closure functions may capture more variables. |
| 7755 // This Scope object is constructed after all variables have been allocated. | 7730 // This Scope object is constructed after all variables have been allocated. |
| 7756 // The local scope of the parsed function can be pruned, since contained | 7731 // The local scope of the parsed function can be pruned, since contained |
| 7757 // variables are not relevant for the compilation of the enclosing function. | 7732 // variables are not relevant for the compilation of the enclosing function. |
| 7758 // This pruning is done by omitting to hook the local scope in its parent | 7733 // This pruning is done by omitting to hook the local scope in its parent |
| 7759 // scope in the constructor of LocalScope. | 7734 // scope in the constructor of LocalScope. |
| 7760 AstNode* closure = | 7735 AstNode* closure = new(Z) ClosureNode( |
| 7761 new(Z) ClosureNode(function_pos, function, NULL, | 7736 function_pos, function, NULL, statements->scope()); |
| 7762 statements != NULL ? statements->scope() : NULL); | |
| 7763 | 7737 |
| 7764 if (function_variable == NULL) { | 7738 if (function_variable == NULL) { |
| 7765 ASSERT(is_literal); | 7739 ASSERT(is_literal); |
| 7766 return closure; | 7740 return closure; |
| 7767 } else { | 7741 } else { |
| 7768 AstNode* initialization = new(Z) StoreLocalNode( | 7742 AstNode* initialization = new(Z) StoreLocalNode( |
| 7769 function_pos, function_variable, closure); | 7743 function_pos, function_variable, closure); |
| 7770 return initialization; | 7744 return initialization; |
| 7771 } | 7745 } |
| 7772 } | 7746 } |
| (...skipping 6793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 14566 const ArgumentListNode& function_args, | 14540 const ArgumentListNode& function_args, |
| 14567 const LocalVariable* temp_for_last_arg, | 14541 const LocalVariable* temp_for_last_arg, |
| 14568 bool is_super_invocation) { | 14542 bool is_super_invocation) { |
| 14569 UNREACHABLE(); | 14543 UNREACHABLE(); |
| 14570 return NULL; | 14544 return NULL; |
| 14571 } | 14545 } |
| 14572 | 14546 |
| 14573 } // namespace dart | 14547 } // namespace dart |
| 14574 | 14548 |
| 14575 #endif // DART_PRECOMPILED_RUNTIME | 14549 #endif // DART_PRECOMPILED_RUNTIME |
| OLD | NEW |