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 1909 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1920 // In top-level and mixin functions, the source may be in a different | 1920 // In top-level and mixin functions, the source may be in a different |
1921 // script than the script of the current class. However, we never reparse | 1921 // script than the script of the current class. However, we never reparse |
1922 // signature functions (except typedef signature functions), therefore | 1922 // signature functions (except typedef signature functions), therefore |
1923 // we do not need to keep the correct script via a patch class. Use the | 1923 // we do not need to keep the correct script via a patch class. Use the |
1924 // actual current class as owner of the signature function. | 1924 // actual current class as owner of the signature function. |
1925 const Function& signature_function = Function::Handle(Z, | 1925 const Function& signature_function = Function::Handle(Z, |
1926 Function::NewSignatureFunction(current_class(), | 1926 Function::NewSignatureFunction(current_class(), |
1927 TokenPosition::kNoSource)); | 1927 TokenPosition::kNoSource)); |
1928 signature_function.set_result_type(result_type); | 1928 signature_function.set_result_type(result_type); |
1929 AddFormalParamsToFunction(&func_params, signature_function); | 1929 AddFormalParamsToFunction(&func_params, signature_function); |
1930 FunctionType& signature_type = | 1930 Type& signature_type = |
1931 FunctionType::ZoneHandle(Z, signature_function.SignatureType()); | 1931 Type::ZoneHandle(Z, signature_function.SignatureType()); |
1932 if (!is_top_level_) { | 1932 if (!is_top_level_) { |
1933 signature_type ^= ClassFinalizer::FinalizeType( | 1933 signature_type ^= ClassFinalizer::FinalizeType( |
1934 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 1934 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
1935 signature_function.SetSignatureType(signature_type); | 1935 signature_function.SetSignatureType(signature_type); |
1936 } | 1936 } |
1937 ASSERT(is_top_level_ || signature_type.IsFinalized()); | 1937 ASSERT(is_top_level_ || signature_type.IsFinalized()); |
1938 // A signature type itself cannot be malformed or malbounded, only its | 1938 // A signature type itself cannot be malformed or malbounded, only its |
1939 // signature function's result type or parameter types may be. | 1939 // signature function's result type or parameter types may be. |
1940 ASSERT(!signature_type.IsMalformed()); | 1940 ASSERT(!signature_type.IsMalformed()); |
1941 ASSERT(!signature_type.IsMalbounded()); | 1941 ASSERT(!signature_type.IsMalbounded()); |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2311 ClosureNode* Parser::CreateImplicitClosureNode(const Function& func, | 2311 ClosureNode* Parser::CreateImplicitClosureNode(const Function& func, |
2312 TokenPosition token_pos, | 2312 TokenPosition token_pos, |
2313 AstNode* receiver) { | 2313 AstNode* receiver) { |
2314 Function& implicit_closure_function = | 2314 Function& implicit_closure_function = |
2315 Function::ZoneHandle(Z, func.ImplicitClosureFunction()); | 2315 Function::ZoneHandle(Z, func.ImplicitClosureFunction()); |
2316 if (receiver != NULL) { | 2316 if (receiver != NULL) { |
2317 // If we create an implicit instance closure from inside a closure of a | 2317 // If we create an implicit instance closure from inside a closure of a |
2318 // parameterized class, make sure that the receiver is captured as | 2318 // parameterized class, make sure that the receiver is captured as |
2319 // instantiator. | 2319 // instantiator. |
2320 if (current_block_->scope->function_level() > 0) { | 2320 if (current_block_->scope->function_level() > 0) { |
2321 const FunctionType& signature_type = FunctionType::Handle(Z, | 2321 const Type& signature_type = Type::Handle(Z, |
2322 implicit_closure_function.SignatureType()); | 2322 implicit_closure_function.SignatureType()); |
2323 const Class& scope_class = Class::Handle(Z, signature_type.type_class()); | 2323 const Class& scope_class = Class::Handle(Z, signature_type.type_class()); |
2324 if (scope_class.IsGeneric()) { | 2324 if (scope_class.IsGeneric()) { |
2325 CaptureInstantiator(); | 2325 CaptureInstantiator(); |
2326 } | 2326 } |
2327 } | 2327 } |
2328 } | 2328 } |
2329 return new ClosureNode(token_pos, implicit_closure_function, receiver, NULL); | 2329 return new ClosureNode(token_pos, implicit_closure_function, receiver, NULL); |
2330 } | 2330 } |
2331 | 2331 |
(...skipping 4241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6573 } | 6573 } |
6574 | 6574 |
6575 ParamList closure_params; | 6575 ParamList closure_params; |
6576 AddSyncGenClosureParameters(&closure_params); | 6576 AddSyncGenClosureParameters(&closure_params); |
6577 | 6577 |
6578 if (is_new_closure) { | 6578 if (is_new_closure) { |
6579 // Add the parameters to the newly created closure. | 6579 // Add the parameters to the newly created closure. |
6580 AddFormalParamsToFunction(&closure_params, body); | 6580 AddFormalParamsToFunction(&closure_params, body); |
6581 | 6581 |
6582 // Finalize function type. | 6582 // Finalize function type. |
6583 FunctionType& signature_type = | 6583 Type& signature_type = Type::Handle(Z, body.SignatureType()); |
6584 FunctionType::Handle(Z, body.SignatureType()); | |
6585 signature_type ^= ClassFinalizer::FinalizeType( | 6584 signature_type ^= ClassFinalizer::FinalizeType( |
6586 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 6585 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6587 body.SetSignatureType(signature_type); | 6586 body.SetSignatureType(signature_type); |
6588 ASSERT(AbstractType::Handle(Z, body.result_type()).IsResolved()); | 6587 ASSERT(AbstractType::Handle(Z, body.result_type()).IsResolved()); |
6589 ASSERT(body.NumParameters() == closure_params.parameters->length()); | 6588 ASSERT(body.NumParameters() == closure_params.parameters->length()); |
6590 } | 6589 } |
6591 | 6590 |
6592 OpenFunctionBlock(body); | 6591 OpenFunctionBlock(body); |
6593 AddFormalParamsToScope(&closure_params, current_block_->scope); | 6592 AddFormalParamsToScope(&closure_params, current_block_->scope); |
6594 async_temp_scope_ = current_block_->scope; | 6593 async_temp_scope_ = current_block_->scope; |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6706 is_new_closure = true; | 6705 is_new_closure = true; |
6707 } | 6706 } |
6708 // Create the parameter list for the async body closure. | 6707 // Create the parameter list for the async body closure. |
6709 ParamList closure_params; | 6708 ParamList closure_params; |
6710 AddAsyncClosureParameters(&closure_params); | 6709 AddAsyncClosureParameters(&closure_params); |
6711 if (is_new_closure) { | 6710 if (is_new_closure) { |
6712 // Add the parameters to the newly created closure. | 6711 // Add the parameters to the newly created closure. |
6713 AddFormalParamsToFunction(&closure_params, closure); | 6712 AddFormalParamsToFunction(&closure_params, closure); |
6714 | 6713 |
6715 // Finalize function type. | 6714 // Finalize function type. |
6716 FunctionType& signature_type = | 6715 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
6717 FunctionType::Handle(Z, closure.SignatureType()); | |
6718 signature_type ^= ClassFinalizer::FinalizeType( | 6716 signature_type ^= ClassFinalizer::FinalizeType( |
6719 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 6717 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6720 closure.SetSignatureType(signature_type); | 6718 closure.SetSignatureType(signature_type); |
6721 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); | 6719 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); |
6722 ASSERT(closure.NumParameters() == closure_params.parameters->length()); | 6720 ASSERT(closure.NumParameters() == closure_params.parameters->length()); |
6723 } | 6721 } |
6724 OpenFunctionBlock(closure); | 6722 OpenFunctionBlock(closure); |
6725 AddFormalParamsToScope(&closure_params, current_block_->scope); | 6723 AddFormalParamsToScope(&closure_params, current_block_->scope); |
6726 async_temp_scope_ = current_block_->scope; | 6724 async_temp_scope_ = current_block_->scope; |
6727 return closure.raw(); | 6725 return closure.raw(); |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6842 } | 6840 } |
6843 | 6841 |
6844 ParamList closure_params; | 6842 ParamList closure_params; |
6845 AddAsyncGenClosureParameters(&closure_params); | 6843 AddAsyncGenClosureParameters(&closure_params); |
6846 | 6844 |
6847 if (is_new_closure) { | 6845 if (is_new_closure) { |
6848 // Add the parameters to the newly created closure. | 6846 // Add the parameters to the newly created closure. |
6849 AddFormalParamsToFunction(&closure_params, closure); | 6847 AddFormalParamsToFunction(&closure_params, closure); |
6850 | 6848 |
6851 // Finalize function type. | 6849 // Finalize function type. |
6852 FunctionType& signature_type = | 6850 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
6853 FunctionType::Handle(Z, closure.SignatureType()); | |
6854 signature_type ^= ClassFinalizer::FinalizeType( | 6851 signature_type ^= ClassFinalizer::FinalizeType( |
6855 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 6852 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
6856 closure.SetSignatureType(signature_type); | 6853 closure.SetSignatureType(signature_type); |
6857 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); | 6854 ASSERT(AbstractType::Handle(Z, closure.result_type()).IsResolved()); |
6858 ASSERT(closure.NumParameters() == closure_params.parameters->length()); | 6855 ASSERT(closure.NumParameters() == closure_params.parameters->length()); |
6859 } | 6856 } |
6860 | 6857 |
6861 OpenFunctionBlock(closure); | 6858 OpenFunctionBlock(closure); |
6862 AddFormalParamsToScope(&closure_params, current_block_->scope); | 6859 AddFormalParamsToScope(&closure_params, current_block_->scope); |
6863 async_temp_scope_ = current_block_->scope; | 6860 async_temp_scope_ = current_block_->scope; |
(...skipping 727 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7591 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { | 7588 if (FLAG_enable_mirrors && metadata_pos.IsReal()) { |
7592 library_.AddFunctionMetadata(function, metadata_pos); | 7589 library_.AddFunctionMetadata(function, metadata_pos); |
7593 } | 7590 } |
7594 } | 7591 } |
7595 | 7592 |
7596 // The function type needs to be finalized at compile time, since the closure | 7593 // The function type needs to be finalized at compile time, since the closure |
7597 // may be type checked at run time when assigned to a function variable, | 7594 // may be type checked at run time when assigned to a function variable, |
7598 // passed as a function argument, or returned as a function result. | 7595 // passed as a function argument, or returned as a function result. |
7599 | 7596 |
7600 LocalVariable* function_variable = NULL; | 7597 LocalVariable* function_variable = NULL; |
7601 FunctionType& function_type = FunctionType::ZoneHandle(Z); | 7598 Type& function_type = Type::ZoneHandle(Z); |
7602 if (variable_name != NULL) { | 7599 if (variable_name != NULL) { |
7603 // Since the function type depends on the signature of the closure function, | 7600 // Since the function type depends on the signature of the closure function, |
7604 // it cannot be determined before the formal parameter list of the closure | 7601 // it cannot be determined before the formal parameter list of the closure |
7605 // function is parsed. Therefore, we set the function type to a new | 7602 // function is parsed. Therefore, we set the function type to a new |
7606 // function type to be patched after the actual type is known. | 7603 // function type to be patched after the actual type is known. |
7607 // We temporarily use the Closure class as scope class. | 7604 // We temporarily use the Closure class as scope class. |
7608 const Class& unknown_scope_class = Class::Handle(Z, | 7605 const Class& unknown_scope_class = Class::Handle(Z, |
7609 I->object_store()->closure_class()); | 7606 I->object_store()->closure_class()); |
7610 function_type = FunctionType::New(unknown_scope_class, | 7607 function_type = Type::New(unknown_scope_class, |
7611 TypeArguments::Handle(Z), | 7608 TypeArguments::Handle(Z), |
7612 function, | 7609 function_pos); |
7613 function_pos); | 7610 function_type.set_signature(function); |
7614 function_type.SetIsFinalized(); // No finalization needed. | 7611 function_type.SetIsFinalized(); // No finalization needed. |
7615 | 7612 |
7616 // Add the function variable to the scope before parsing the function in | 7613 // Add the function variable to the scope before parsing the function in |
7617 // order to allow self reference from inside the function. | 7614 // order to allow self reference from inside the function. |
7618 function_variable = new(Z) LocalVariable(function_pos, | 7615 function_variable = new(Z) LocalVariable(function_pos, |
7619 *variable_name, | 7616 *variable_name, |
7620 function_type); | 7617 function_type); |
7621 function_variable->set_is_final(); | 7618 function_variable->set_is_final(); |
7622 ASSERT(current_block_ != NULL); | 7619 ASSERT(current_block_ != NULL); |
7623 ASSERT(current_block_->scope != NULL); | 7620 ASSERT(current_block_->scope != NULL); |
7624 if (!current_block_->scope->AddVariable(function_variable)) { | 7621 if (!current_block_->scope->AddVariable(function_variable)) { |
7625 LocalVariable* existing_var = | 7622 LocalVariable* existing_var = |
7626 current_block_->scope->LookupVariable(function_variable->name(), | 7623 current_block_->scope->LookupVariable(function_variable->name(), |
7627 true); | 7624 true); |
7628 ASSERT(existing_var != NULL); | 7625 ASSERT(existing_var != NULL); |
7629 // Use before define cases have already been detected and reported above. | 7626 // Use before define cases have already been detected and reported above. |
7630 ASSERT(existing_var->owner() == current_block_->scope); | 7627 ASSERT(existing_var->owner() == current_block_->scope); |
7631 ReportError(function_pos, "identifier '%s' already defined", | 7628 ReportError(function_pos, "identifier '%s' already defined", |
7632 function_variable->name().ToCString()); | 7629 function_variable->name().ToCString()); |
7633 } | 7630 } |
7634 } | 7631 } |
7635 | 7632 |
7636 // Parse the local function. | 7633 // Parse the local function. |
7637 SequenceNode* statements = Parser::ParseFunc(function, !is_literal); | 7634 SequenceNode* statements = Parser::ParseFunc(function, !is_literal); |
7638 INC_STAT(thread(), num_functions_parsed, 1); | 7635 INC_STAT(thread(), num_functions_parsed, 1); |
7639 | 7636 |
7640 // Now that the local function has formal parameters, lookup the signature | 7637 // Now that the local function has formal parameters, lookup the signature |
7641 FunctionType& signature_type = | 7638 Type& signature_type = Type::ZoneHandle(Z, function.SignatureType()); |
7642 FunctionType::ZoneHandle(Z, function.SignatureType()); | |
7643 signature_type ^= ClassFinalizer::FinalizeType( | 7639 signature_type ^= ClassFinalizer::FinalizeType( |
7644 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 7640 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
7645 function.SetSignatureType(signature_type); | 7641 function.SetSignatureType(signature_type); |
7646 | 7642 |
7647 // Local functions are registered in the enclosing class, but | 7643 // Local functions are registered in the enclosing class, but |
7648 // ignored during class finalization. The enclosing class has | 7644 // ignored during class finalization. The enclosing class has |
7649 // already been finalized. | 7645 // already been finalized. |
7650 ASSERT(current_class().is_finalized()); | 7646 ASSERT(current_class().is_finalized()); |
7651 ASSERT(signature_type.IsFinalized()); | 7647 ASSERT(signature_type.IsFinalized()); |
7652 | 7648 |
7653 // Make sure that the instantiator is captured. | 7649 // Make sure that the instantiator is captured. |
7654 if ((current_block_->scope->function_level() > 0) && | 7650 if ((current_block_->scope->function_level() > 0) && |
7655 Class::Handle(signature_type.type_class()).IsGeneric()) { | 7651 Class::Handle(signature_type.type_class()).IsGeneric()) { |
7656 CaptureInstantiator(); | 7652 CaptureInstantiator(); |
7657 } | 7653 } |
7658 | 7654 |
7659 // A signature type itself cannot be malformed or malbounded, only its | 7655 // A local signature type itself cannot be malformed or malbounded, only its |
7660 // signature function's result type or parameter types may be. | 7656 // signature function's result type or parameter types may be. |
7661 ASSERT(!signature_type.IsMalformed()); | 7657 ASSERT(!signature_type.IsMalformed()); |
7662 ASSERT(!signature_type.IsMalbounded()); | 7658 ASSERT(!signature_type.IsMalbounded()); |
7663 | 7659 |
7664 if (variable_name != NULL) { | 7660 if (variable_name != NULL) { |
7665 // Patch the function type of the variable now that the signature is known. | 7661 // Patch the function type of the variable now that the signature is known. |
7666 function_type.set_scope_class( | 7662 function_type.set_type_class( |
7667 Class::Handle(Z, signature_type.scope_class())); | 7663 Class::Handle(Z, signature_type.type_class())); |
7668 function_type.set_arguments( | 7664 function_type.set_arguments( |
7669 TypeArguments::Handle(Z, signature_type.arguments())); | 7665 TypeArguments::Handle(Z, signature_type.arguments())); |
7670 ASSERT(function_type.signature() == function.raw()); | 7666 ASSERT(function_type.signature() == function.raw()); |
7671 | 7667 |
7672 // The function type was initially marked as instantiated, but it may | 7668 // The function type was initially marked as instantiated, but it may |
7673 // actually be uninstantiated. | 7669 // actually be uninstantiated. |
7674 function_type.ResetIsFinalized(); | 7670 function_type.ResetIsFinalized(); |
7675 | 7671 |
7676 // The function variable type should have been patched above. | 7672 // The function variable type should have been patched above. |
7677 ASSERT((function_variable == NULL) || | 7673 ASSERT((function_variable == NULL) || |
(...skipping 5499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13177 closure = Function::NewClosureFunction(closure_name, | 13173 closure = Function::NewClosureFunction(closure_name, |
13178 innermost_function(), | 13174 innermost_function(), |
13179 token_pos); | 13175 token_pos); |
13180 closure.set_is_generated_body(true); | 13176 closure.set_is_generated_body(true); |
13181 closure.set_is_debuggable(false); | 13177 closure.set_is_debuggable(false); |
13182 closure.set_is_visible(false); | 13178 closure.set_is_visible(false); |
13183 closure.set_result_type(Object::dynamic_type()); | 13179 closure.set_result_type(Object::dynamic_type()); |
13184 AddFormalParamsToFunction(¶ms, closure); | 13180 AddFormalParamsToFunction(¶ms, closure); |
13185 | 13181 |
13186 // Finalize function type. | 13182 // Finalize function type. |
13187 FunctionType& signature_type = | 13183 Type& signature_type = Type::Handle(Z, closure.SignatureType()); |
13188 FunctionType::Handle(Z, closure.SignatureType()); | |
13189 signature_type ^= ClassFinalizer::FinalizeType( | 13184 signature_type ^= ClassFinalizer::FinalizeType( |
13190 current_class(), signature_type, ClassFinalizer::kCanonicalize); | 13185 current_class(), signature_type, ClassFinalizer::kCanonicalize); |
13191 closure.SetSignatureType(signature_type); | 13186 closure.SetSignatureType(signature_type); |
13192 // Finalization would be premature when top-level parsing. | 13187 // Finalization would be premature when top-level parsing. |
13193 ASSERT(!is_top_level_); | 13188 ASSERT(!is_top_level_); |
13194 return closure.raw(); | 13189 return closure.raw(); |
13195 } | 13190 } |
13196 | 13191 |
13197 | 13192 |
13198 static String& BuildConstructorName(const String& type_class_name, | 13193 static String& BuildConstructorName(const String& type_class_name, |
(...skipping 1246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14445 const ArgumentListNode& function_args, | 14440 const ArgumentListNode& function_args, |
14446 const LocalVariable* temp_for_last_arg, | 14441 const LocalVariable* temp_for_last_arg, |
14447 bool is_super_invocation) { | 14442 bool is_super_invocation) { |
14448 UNREACHABLE(); | 14443 UNREACHABLE(); |
14449 return NULL; | 14444 return NULL; |
14450 } | 14445 } |
14451 | 14446 |
14452 } // namespace dart | 14447 } // namespace dart |
14453 | 14448 |
14454 #endif // DART_PRECOMPILED_RUNTIME | 14449 #endif // DART_PRECOMPILED_RUNTIME |
OLD | NEW |