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 | 6 |
7 #include "lib/invocation_mirror.h" | 7 #include "lib/invocation_mirror.h" |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/ast_transformer.h" | 9 #include "vm/ast_transformer.h" |
10 #include "vm/bootstrap.h" | 10 #include "vm/bootstrap.h" |
(...skipping 834 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
845 SequenceNode* node_sequence = NULL; | 845 SequenceNode* node_sequence = NULL; |
846 Array& default_parameter_values = Array::ZoneHandle(isolate, Array::null()); | 846 Array& default_parameter_values = Array::ZoneHandle(isolate, Array::null()); |
847 switch (func.kind()) { | 847 switch (func.kind()) { |
848 case RawFunction::kRegularFunction: | 848 case RawFunction::kRegularFunction: |
849 case RawFunction::kClosureFunction: | 849 case RawFunction::kClosureFunction: |
850 case RawFunction::kGetterFunction: | 850 case RawFunction::kGetterFunction: |
851 case RawFunction::kSetterFunction: | 851 case RawFunction::kSetterFunction: |
852 case RawFunction::kConstructor: | 852 case RawFunction::kConstructor: |
853 // The call to a redirecting factory is redirected. | 853 // The call to a redirecting factory is redirected. |
854 ASSERT(!func.IsRedirectingFactory()); | 854 ASSERT(!func.IsRedirectingFactory()); |
855 if (!func.IsImplicitConstructor() && !func.is_async_closure()) { | 855 if (!func.IsImplicitConstructor()) { |
856 parser.SkipFunctionPreamble(); | 856 parser.SkipFunctionPreamble(); |
857 } | 857 } |
858 node_sequence = parser.ParseFunc(func, &default_parameter_values); | 858 node_sequence = parser.ParseFunc(func, &default_parameter_values); |
859 break; | 859 break; |
860 case RawFunction::kImplicitGetter: | 860 case RawFunction::kImplicitGetter: |
861 ASSERT(!func.is_static()); | 861 ASSERT(!func.is_static()); |
862 node_sequence = parser.ParseInstanceGetter(func); | 862 node_sequence = parser.ParseInstanceGetter(func); |
863 break; | 863 break; |
864 case RawFunction::kImplicitSetter: | 864 case RawFunction::kImplicitSetter: |
865 ASSERT(!func.is_static()); | 865 ASSERT(!func.is_static()); |
(...skipping 2128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2994 Array* default_parameter_values) { | 2994 Array* default_parameter_values) { |
2995 TRACE_PARSER("ParseFunc"); | 2995 TRACE_PARSER("ParseFunc"); |
2996 Function& saved_innermost_function = | 2996 Function& saved_innermost_function = |
2997 Function::Handle(I, innermost_function().raw()); | 2997 Function::Handle(I, innermost_function().raw()); |
2998 innermost_function_ = func.raw(); | 2998 innermost_function_ = func.raw(); |
2999 | 2999 |
3000 // Save current try index. Try index starts at zero for each function. | 3000 // Save current try index. Try index starts at zero for each function. |
3001 intptr_t saved_try_index = last_used_try_index_; | 3001 intptr_t saved_try_index = last_used_try_index_; |
3002 last_used_try_index_ = 0; | 3002 last_used_try_index_ = 0; |
3003 | 3003 |
3004 intptr_t formal_params_pos = TokenPos(); | |
3005 // TODO(12455) : Need better validation mechanism. | |
3006 | |
3007 // In case of nested async functions we also need to save the currently saved | 3004 // In case of nested async functions we also need to save the currently saved |
3008 // try context, the corresponding stack variable, and the scope where | 3005 // try context, the corresponding stack variable, and the scope where |
3009 // temporaries are added. | 3006 // temporaries are added. |
3010 LocalVariable* saved_saved_try_ctx = parsed_function()->saved_try_ctx(); | 3007 LocalVariable* saved_saved_try_ctx = parsed_function()->saved_try_ctx(); |
3011 const String& saved_async_saved_try_ctx_name = | 3008 const String& saved_async_saved_try_ctx_name = |
3012 String::Handle(I, parsed_function()->async_saved_try_ctx_name()); | 3009 String::Handle(I, parsed_function()->async_saved_try_ctx_name()); |
3013 parsed_function()->reset_saved_try_ctx_vars(); | 3010 parsed_function()->reset_saved_try_ctx_vars(); |
3014 LocalScope* saved_async_temp_scope = async_temp_scope_; | 3011 LocalScope* saved_async_temp_scope = async_temp_scope_; |
3015 | 3012 |
3016 if (func.IsConstructor()) { | 3013 if (func.IsConstructor()) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3070 error_param.type = &dynamic_type; | 3067 error_param.type = &dynamic_type; |
3071 params.parameters->Add(result_param); | 3068 params.parameters->Add(result_param); |
3072 params.parameters->Add(error_param); | 3069 params.parameters->Add(error_param); |
3073 params.num_optional_parameters += 2; | 3070 params.num_optional_parameters += 2; |
3074 params.has_optional_positional_parameters = true; | 3071 params.has_optional_positional_parameters = true; |
3075 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); | 3072 SetupDefaultsForOptionalParams(¶ms, default_parameter_values); |
3076 AddFormalParamsToScope(¶ms, current_block_->scope); | 3073 AddFormalParamsToScope(¶ms, current_block_->scope); |
3077 ASSERT(AbstractType::Handle(I, func.result_type()).IsResolved()); | 3074 ASSERT(AbstractType::Handle(I, func.result_type()).IsResolved()); |
3078 ASSERT(func.NumParameters() == params.parameters->length()); | 3075 ASSERT(func.NumParameters() == params.parameters->length()); |
3079 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { | 3076 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { |
3080 // Parse away any formal parameters, as they are accessed as as context | 3077 // Parse and discard any formal parameters. They are accessed as |
3081 // variables. | 3078 // context variables. |
3082 ParamList parse_away; | 3079 ParamList parse_away; |
3083 ParseFormalParameterList(allow_explicit_default_values, | 3080 ParseFormalParameterList(allow_explicit_default_values, |
3084 false, | 3081 false, |
3085 &parse_away); | 3082 &parse_away); |
3086 } | 3083 } |
3087 } else { | 3084 } else { |
3088 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); | 3085 ParseFormalParameterList(allow_explicit_default_values, false, ¶ms); |
3089 | 3086 |
3090 // The number of parameters and their type are not yet set in local | 3087 // The number of parameters and their type are not yet set in local |
3091 // functions, since they are not 'top-level' parsed. | 3088 // functions, since they are not 'top-level' parsed. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3124 } | 3121 } |
3125 } | 3122 } |
3126 | 3123 |
3127 RawFunction::AsyncModifier func_modifier = ParseFunctionModifier(); | 3124 RawFunction::AsyncModifier func_modifier = ParseFunctionModifier(); |
3128 func.set_modifier(func_modifier); | 3125 func.set_modifier(func_modifier); |
3129 | 3126 |
3130 OpenBlock(); // Open a nested scope for the outermost function block. | 3127 OpenBlock(); // Open a nested scope for the outermost function block. |
3131 | 3128 |
3132 Function& async_closure = Function::ZoneHandle(I); | 3129 Function& async_closure = Function::ZoneHandle(I); |
3133 if (func.IsAsyncFunction() && !func.is_async_closure()) { | 3130 if (func.IsAsyncFunction() && !func.is_async_closure()) { |
3134 async_closure = OpenAsyncFunction(formal_params_pos); | 3131 async_closure = OpenAsyncFunction(func.token_pos()); |
3135 } else if (func.is_async_closure()) { | 3132 } else if (func.is_async_closure()) { |
3136 OpenAsyncClosure(); | 3133 OpenAsyncClosure(); |
3137 } | 3134 } |
3138 | 3135 |
3139 BoolScope allow_await(&this->await_is_keyword_, | 3136 BoolScope allow_await(&this->await_is_keyword_, |
3140 func.IsAsyncFunction() || func.is_async_closure()); | 3137 func.IsAsyncFunction() || func.is_async_closure()); |
3141 intptr_t end_token_pos = 0; | 3138 intptr_t end_token_pos = 0; |
3142 if (CurrentToken() == Token::kLBRACE) { | 3139 if (CurrentToken() == Token::kLBRACE) { |
3143 ConsumeToken(); | 3140 ConsumeToken(); |
3144 if (String::Handle(I, func.name()).Equals(Symbols::EqualOperator())) { | 3141 if (String::Handle(I, func.name()).Equals(Symbols::EqualOperator())) { |
(...skipping 2841 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5986 OpenBlock(); | 5983 OpenBlock(); |
5987 PushTryBlock(current_block_); | 5984 PushTryBlock(current_block_); |
5988 | 5985 |
5989 if (innermost_function().is_async_closure() || | 5986 if (innermost_function().is_async_closure() || |
5990 innermost_function().IsAsyncFunction()) { | 5987 innermost_function().IsAsyncFunction()) { |
5991 SetupSavedTryContext(context_var); | 5988 SetupSavedTryContext(context_var); |
5992 } | 5989 } |
5993 } | 5990 } |
5994 | 5991 |
5995 | 5992 |
5996 RawFunction* Parser::OpenAsyncFunction(intptr_t formal_param_pos) { | 5993 RawFunction* Parser::OpenAsyncFunction(intptr_t async_func_pos) { |
5997 TRACE_PARSER("OpenAsyncFunction"); | 5994 TRACE_PARSER("OpenAsyncFunction"); |
5998 AddAsyncClosureVariables(); | 5995 AddAsyncClosureVariables(); |
5999 Function& closure = Function::Handle(I); | 5996 Function& closure = Function::Handle(I); |
6000 bool is_new_closure = false; | 5997 bool is_new_closure = false; |
6001 | 5998 |
6002 // Check whether a function for the asynchronous function body of | 5999 // Check whether a function for the asynchronous function body of |
6003 // this async function has already been created by a previous | 6000 // this async function has already been created by a previous |
6004 // compilation of this function. | 6001 // compilation of this function. |
6005 const Function& found_func = Function::Handle( | 6002 const Function& found_func = Function::Handle( |
6006 I, current_class().LookupClosureFunction(formal_param_pos)); | 6003 I, current_class().LookupClosureFunction(async_func_pos)); |
6007 if (!found_func.IsNull() && | 6004 if (!found_func.IsNull() && |
6008 (found_func.token_pos() == formal_param_pos) && | 6005 (found_func.token_pos() == async_func_pos) && |
6009 (found_func.script() == innermost_function().script()) && | 6006 (found_func.script() == innermost_function().script()) && |
6010 (found_func.parent_function() == innermost_function().raw())) { | 6007 (found_func.parent_function() == innermost_function().raw())) { |
6011 ASSERT(found_func.is_async_closure()); | 6008 ASSERT(found_func.is_async_closure()); |
6012 closure = found_func.raw(); | 6009 closure = found_func.raw(); |
6013 } else { | 6010 } else { |
6014 // Create the closure containing the body of this async function. | 6011 // Create the closure containing the body of this async function. |
6015 const String& async_func_name = | 6012 const String& async_func_name = |
6016 String::Handle(I, innermost_function().name()); | 6013 String::Handle(I, innermost_function().name()); |
6017 String& closure_name = String::Handle(I, | 6014 String& closure_name = String::Handle(I, |
6018 String::NewFormatted("<%s_async_body>", async_func_name.ToCString())); | 6015 String::NewFormatted("<%s_async_body>", async_func_name.ToCString())); |
6019 closure = Function::NewClosureFunction( | 6016 closure = Function::NewClosureFunction( |
6020 String::Handle(I, Symbols::New(closure_name)), | 6017 String::Handle(I, Symbols::New(closure_name)), |
6021 innermost_function(), | 6018 innermost_function(), |
6022 formal_param_pos); | 6019 async_func_pos); |
6023 closure.set_is_async_closure(true); | 6020 closure.set_is_async_closure(true); |
6024 closure.set_result_type(AbstractType::Handle(Type::DynamicType())); | 6021 closure.set_result_type(AbstractType::Handle(Type::DynamicType())); |
6025 is_new_closure = true; | 6022 is_new_closure = true; |
6026 } | 6023 } |
6027 // Create the parameter list for the async body closure. | 6024 // Create the parameter list for the async body closure. |
6028 ParamList closure_params; | 6025 ParamList closure_params; |
6029 const Type& dynamic_type = Type::ZoneHandle(I, Type::DynamicType()); | 6026 const Type& dynamic_type = Type::ZoneHandle(I, Type::DynamicType()); |
6030 closure_params.AddFinalParameter( | 6027 closure_params.AddFinalParameter( |
6031 formal_param_pos, &Symbols::ClosureParameter(), &dynamic_type); | 6028 async_func_pos, &Symbols::ClosureParameter(), &dynamic_type); |
6032 ParamDesc result_param; | 6029 ParamDesc result_param; |
6033 result_param.name = &Symbols::AsyncOperationParam(); | 6030 result_param.name = &Symbols::AsyncOperationParam(); |
6034 result_param.default_value = &Object::null_instance(); | 6031 result_param.default_value = &Object::null_instance(); |
6035 result_param.type = &dynamic_type; | 6032 result_param.type = &dynamic_type; |
6036 closure_params.parameters->Add(result_param); | 6033 closure_params.parameters->Add(result_param); |
6037 ParamDesc error_param; | 6034 ParamDesc error_param; |
6038 error_param.name = &Symbols::AsyncOperationErrorParam(); | 6035 error_param.name = &Symbols::AsyncOperationErrorParam(); |
6039 error_param.default_value = &Object::null_instance(); | 6036 error_param.default_value = &Object::null_instance(); |
6040 error_param.type = &dynamic_type; | 6037 error_param.type = &dynamic_type; |
6041 closure_params.parameters->Add(error_param); | 6038 closure_params.parameters->Add(error_param); |
6042 closure_params.has_optional_positional_parameters = true; | 6039 closure_params.has_optional_positional_parameters = true; |
6043 closure_params.num_optional_parameters += 2; | 6040 closure_params.num_optional_parameters += 2; |
6044 | 6041 |
6045 if (is_new_closure) { | 6042 if (is_new_closure) { |
6046 // Add the parameters to the newly created closure. | 6043 // Add the parameters to the newly created closure. |
6047 AddFormalParamsToFunction(&closure_params, closure); | 6044 AddFormalParamsToFunction(&closure_params, closure); |
6048 | 6045 |
6049 // Create and set the signature class of the closure. | 6046 // Create and set the signature class of the closure. |
6050 const String& sig = String::Handle(I, closure.Signature()); | 6047 const String& sig = String::Handle(I, closure.Signature()); |
6051 Class& sig_cls = Class::Handle(I, library_.LookupLocalClass(sig)); | 6048 Class& sig_cls = Class::Handle(I, library_.LookupLocalClass(sig)); |
6052 if (sig_cls.IsNull()) { | 6049 if (sig_cls.IsNull()) { |
6053 sig_cls = | 6050 sig_cls = |
6054 Class::NewSignatureClass(sig, closure, script_, formal_param_pos); | 6051 Class::NewSignatureClass(sig, closure, script_, closure.token_pos()); |
6055 library_.AddClass(sig_cls); | 6052 library_.AddClass(sig_cls); |
6056 } | 6053 } |
6057 closure.set_signature_class(sig_cls); | 6054 closure.set_signature_class(sig_cls); |
6058 const Type& sig_type = Type::Handle(I, sig_cls.SignatureType()); | 6055 const Type& sig_type = Type::Handle(I, sig_cls.SignatureType()); |
6059 if (!sig_type.IsFinalized()) { | 6056 if (!sig_type.IsFinalized()) { |
6060 ClassFinalizer::FinalizeType( | 6057 ClassFinalizer::FinalizeType( |
6061 sig_cls, sig_type, ClassFinalizer::kCanonicalize); | 6058 sig_cls, sig_type, ClassFinalizer::kCanonicalize); |
6062 } | 6059 } |
6063 ASSERT(AbstractType::Handle(I, closure.result_type()).IsResolved()); | 6060 ASSERT(AbstractType::Handle(I, closure.result_type()).IsResolved()); |
6064 ASSERT(closure.NumParameters() == closure_params.parameters->length()); | 6061 ASSERT(closure.NumParameters() == closure_params.parameters->length()); |
(...skipping 6138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12203 void Parser::SkipQualIdent() { | 12200 void Parser::SkipQualIdent() { |
12204 ASSERT(IsIdentifier()); | 12201 ASSERT(IsIdentifier()); |
12205 ConsumeToken(); | 12202 ConsumeToken(); |
12206 if (CurrentToken() == Token::kPERIOD) { | 12203 if (CurrentToken() == Token::kPERIOD) { |
12207 ConsumeToken(); // Consume the kPERIOD token. | 12204 ConsumeToken(); // Consume the kPERIOD token. |
12208 ExpectIdentifier("identifier expected after '.'"); | 12205 ExpectIdentifier("identifier expected after '.'"); |
12209 } | 12206 } |
12210 } | 12207 } |
12211 | 12208 |
12212 } // namespace dart | 12209 } // namespace dart |
OLD | NEW |