OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 "vm/bigint_operations.h" | 7 #include "vm/bigint_operations.h" |
8 #include "vm/class_finalizer.h" | 8 #include "vm/class_finalizer.h" |
9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
10 #include "vm/compiler_stats.h" | 10 #include "vm/compiler_stats.h" |
(...skipping 814 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 params->has_field_initializer = true; | 825 params->has_field_initializer = true; |
826 } | 826 } |
827 | 827 |
828 if (CurrentToken() == Token::kLPAREN) { | 828 if (CurrentToken() == Token::kLPAREN) { |
829 // This parameter is probably a closure. If we saw the keyword 'var' | 829 // This parameter is probably a closure. If we saw the keyword 'var' |
830 // or 'final', a closure is not legal here and we ignore the | 830 // or 'final', a closure is not legal here and we ignore the |
831 // opening parens. | 831 // opening parens. |
832 if (!var_seen && !parameter.is_final) { | 832 if (!var_seen && !parameter.is_final) { |
833 // The parsed parameter type is actually the function result type. | 833 // The parsed parameter type is actually the function result type. |
834 const Type& result_type = Type::Handle(parameter.type->raw()); | 834 const Type& result_type = Type::Handle(parameter.type->raw()); |
| 835 |
835 // Finish parsing the function type parameter. | 836 // Finish parsing the function type parameter. |
836 ParamList func_params; | 837 ParamList func_params; |
837 const bool no_explicit_default_values = false; | 838 const bool no_explicit_default_values = false; |
838 ParseFormalParameterList(no_explicit_default_values, &func_params); | 839 ParseFormalParameterList(no_explicit_default_values, &func_params); |
839 // Change the name of the parameter type to be the signature of the | 840 |
840 // function type. | 841 // The field 'is_static' has no meaning for signature functions. |
841 const bool is_static = | |
842 current_function().IsNull() || current_function().is_static(); | |
843 const Function& signature_function = Function::Handle( | 842 const Function& signature_function = Function::Handle( |
844 Function::New(*parameter.name, | 843 Function::New(*parameter.name, |
845 RawFunction::kSignatureFunction, | 844 RawFunction::kSignatureFunction, |
846 is_static, | 845 /* is_static = */ false, |
847 /* is_const = */ false, | 846 /* is_const = */ false, |
848 parameter.name_pos)); | 847 parameter.name_pos)); |
849 signature_function.set_owner(current_class()); | 848 signature_function.set_owner(current_class()); |
850 signature_function.set_result_type(result_type); | 849 signature_function.set_result_type(result_type); |
851 AddFormalParamsToFunction(&func_params, signature_function); | 850 AddFormalParamsToFunction(&func_params, signature_function); |
852 const String& signature = String::Handle(signature_function.Signature()); | 851 const String& signature = String::Handle(signature_function.Signature()); |
853 // Lookup the class named signature and only create a new class if it does | 852 // Lookup the class named signature and only create a new class if it does |
854 // not exist yet. | 853 // not exist yet. |
855 Class& signature_class = Class::ZoneHandle(LookupClass(signature)); | 854 Class& signature_class = Class::ZoneHandle(LookupClass(signature)); |
856 if (signature_class.IsNull()) { | 855 if (signature_class.IsNull()) { |
857 signature_class = Class::NewSignatureClass(signature, | 856 signature_class = Class::NewSignatureClass(signature, |
858 signature_function, | 857 signature_function, |
859 script_, | 858 script_, |
860 parameter.name_pos); | 859 parameter.name_pos); |
861 // Record the function signature class in the library. | 860 // Record the function signature class in the library. |
862 library_.AddClass(signature_class); | 861 library_.AddClass(signature_class); |
863 } | 862 } |
864 ASSERT(!Function::Handle(signature_class.signature_function()).IsNull()); | 863 ASSERT(!Function::Handle(signature_class.signature_function()).IsNull()); |
865 ASSERT(Class::Handle(signature_function.signature_class()).IsNull()); | 864 ASSERT(Class::Handle(signature_function.signature_class()).IsNull()); |
866 signature_function.set_signature_class(signature_class); | 865 signature_function.set_signature_class(signature_class); |
867 // The type of the parameter is now the signature class. | 866 |
868 // TODO(regis): What are the type arguments? | 867 // The type of the parameter is now the signature type. |
869 parameter.type = &Type::ZoneHandle(Type::NewRawType(signature_class)); | 868 parameter.type = &Type::ZoneHandle(signature_class.SignatureType()); |
870 if (!is_top_level_) { | 869 if (!is_top_level_ && !parameter.type->IsFinalized()) { |
871 const String& errmsg = String::Handle( | 870 const String& errmsg = String::Handle( |
872 ClassFinalizer::FinalizeTypeWhileParsing(*parameter.type)); | 871 ClassFinalizer::FinalizeTypeWhileParsing(*parameter.type)); |
873 if (!errmsg.IsNull()) { | 872 if (!errmsg.IsNull()) { |
874 ErrorMsg(errmsg.ToCString()); | 873 ErrorMsg(errmsg.ToCString()); |
875 } | 874 } |
876 } | 875 } |
877 } | 876 } |
878 } | 877 } |
879 | 878 |
880 if (CurrentToken() == Token::kASSIGN) { | 879 if (CurrentToken() == Token::kASSIGN) { |
(...skipping 1502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2383 result_type = ParseType(kDoNotResolve); // No owner class yet. | 2382 result_type = ParseType(kDoNotResolve); // No owner class yet. |
2384 } | 2383 } |
2385 | 2384 |
2386 if (CurrentToken() != Token::kIDENT) { | 2385 if (CurrentToken() != Token::kIDENT) { |
2387 ErrorMsg("function alias name expected"); | 2386 ErrorMsg("function alias name expected"); |
2388 } | 2387 } |
2389 const intptr_t alias_name_pos = token_index_; | 2388 const intptr_t alias_name_pos = token_index_; |
2390 const String* alias_name = CurrentLiteral(); | 2389 const String* alias_name = CurrentLiteral(); |
2391 ConsumeToken(); | 2390 ConsumeToken(); |
2392 | 2391 |
2393 // Allocate a class to hold the type parameters and their 'extends' | 2392 // Allocate an interface to hold the type parameters and their 'extends' |
2394 // constraints. Make it the owner of the function type descriptor. | 2393 // constraints. Make it the owner of the function type descriptor. |
2395 const Class& alias_owner = Class::Handle( | 2394 const Class& alias_owner = Class::Handle( |
2396 Class::New(String::Handle(String::NewSymbol("")), Script::Handle())); | 2395 Class::New(String::Handle(String::NewSymbol("")), Script::Handle())); |
| 2396 alias_owner.set_is_interface(); |
2397 set_current_class(alias_owner); | 2397 set_current_class(alias_owner); |
2398 ParseTypeParameters(alias_owner); | 2398 ParseTypeParameters(alias_owner); |
2399 if (CurrentToken() != Token::kLPAREN) { | 2399 if (CurrentToken() != Token::kLPAREN) { |
2400 ErrorMsg("formal parameter list expected"); | 2400 ErrorMsg("formal parameter list expected"); |
2401 } | 2401 } |
2402 | 2402 |
2403 // At this point, the type parameters have been parsed, so we can resolve the | 2403 // At this point, the type parameters have been parsed, so we can resolve the |
2404 // result type. | 2404 // result type. |
2405 if (!result_type.IsNull() && !result_type.IsResolved()) { | 2405 if (!result_type.IsNull() && !result_type.IsResolved()) { |
2406 ResolveTypeFromClass(result_type_pos, alias_owner, &result_type); | 2406 ResolveTypeFromClass(result_type_pos, alias_owner, &result_type); |
2407 } | 2407 } |
2408 ParamList func_params; | 2408 ParamList func_params; |
2409 const bool no_explicit_default_values = false; | 2409 const bool no_explicit_default_values = false; |
2410 ParseFormalParameterList(no_explicit_default_values, &func_params); | 2410 ParseFormalParameterList(no_explicit_default_values, &func_params); |
2411 // TODO(regis): If no type parameters were specified, the function could be | 2411 // The field 'is_static' has no meaning for signature functions. |
2412 // static. Revisit. | |
2413 Function& signature_function = Function::Handle( | 2412 Function& signature_function = Function::Handle( |
2414 Function::New(*alias_name, | 2413 Function::New(*alias_name, |
2415 RawFunction::kSignatureFunction, | 2414 RawFunction::kSignatureFunction, |
2416 /* is_static = */ false, | 2415 /* is_static = */ false, |
2417 /* is_const = */ false, | 2416 /* is_const = */ false, |
2418 alias_name_pos)); | 2417 alias_name_pos)); |
2419 signature_function.set_owner(alias_owner); | 2418 signature_function.set_owner(alias_owner); |
2420 signature_function.set_result_type(result_type); | 2419 signature_function.set_result_type(result_type); |
2421 AddFormalParamsToFunction(&func_params, signature_function); | 2420 AddFormalParamsToFunction(&func_params, signature_function); |
2422 const String& signature = String::Handle(signature_function.Signature()); | 2421 const String& signature = String::Handle(signature_function.Signature()); |
(...skipping 1020 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3443 // Make sure that the instantiator is captured. | 3442 // Make sure that the instantiator is captured. |
3444 if (signature_class.IsParameterized() && | 3443 if (signature_class.IsParameterized() && |
3445 (current_block_->scope->function_level() > 0)) { | 3444 (current_block_->scope->function_level() > 0)) { |
3446 CaptureReceiver(); | 3445 CaptureReceiver(); |
3447 } | 3446 } |
3448 | 3447 |
3449 if (variable_name != NULL) { | 3448 if (variable_name != NULL) { |
3450 // Patch the function type now that the signature is known. | 3449 // Patch the function type now that the signature is known. |
3451 // We need to create a new type for proper finalization, since the existing | 3450 // We need to create a new type for proper finalization, since the existing |
3452 // type is already marked as finalized. | 3451 // type is already marked as finalized. |
3453 TypeArguments& signature_type_arguments = TypeArguments::Handle(); | 3452 const Type& signature_type = Type::Handle(signature_class.SignatureType()); |
3454 if (signature_class.IsParameterized()) { | 3453 const TypeArguments& signature_type_arguments = TypeArguments::Handle( |
3455 const intptr_t num_type_params = signature_class.NumTypeParameters(); | 3454 signature_type.arguments()); |
3456 const Array& type_params = Array::Handle( | 3455 |
3457 signature_class.type_parameters()); | 3456 // Since the signature type is cached by the signature class, it may have |
3458 signature_type_arguments = TypeArguments::NewTypeArray(num_type_params); | 3457 // been finalized already. |
3459 String& type_param_name = String::Handle(); | 3458 if (!signature_type.IsFinalized()) { |
3460 Type& type_param = Type::Handle(); | 3459 const String& errmsg = String::Handle( |
3461 for (int i = 0; i < num_type_params; i++) { | 3460 ClassFinalizer::FinalizeTypeWhileParsing(signature_type)); |
3462 type_param_name ^= type_params.At(i); | 3461 if (!errmsg.IsNull()) { |
3463 type_param = Type::NewTypeParameter(i, type_param_name); | 3462 ErrorMsg(errmsg.ToCString()); |
3464 signature_type_arguments.SetTypeAt(i, type_param); | |
3465 } | 3463 } |
| 3464 // The call to ClassFinalizer::FinalizeTypeWhileParsing may have extended |
| 3465 // the vector of type arguments. |
| 3466 ASSERT(signature_type_arguments.IsNull() || |
| 3467 (signature_type_arguments.Length() == |
| 3468 signature_class.NumTypeArguments())); |
| 3469 // The signature_class should not have changed. |
| 3470 ASSERT(signature_type.type_class() == signature_class.raw()); |
3466 } | 3471 } |
3467 const ParameterizedType& actual_function_type = ParameterizedType::Handle( | |
3468 ParameterizedType::New(signature_class, signature_type_arguments)); | |
3469 const String& errmsg = String::Handle( | |
3470 ClassFinalizer::FinalizeTypeWhileParsing(actual_function_type)); | |
3471 if (!errmsg.IsNull()) { | |
3472 ErrorMsg(errmsg.ToCString()); | |
3473 } | |
3474 // The call to ClassFinalizer::FinalizeTypeWhileParsing may have extended | |
3475 // the vector of type arguments. | |
3476 signature_type_arguments = actual_function_type.arguments(); | |
3477 ASSERT(signature_type_arguments.IsNull() || | |
3478 (signature_type_arguments.Length() == | |
3479 signature_class.NumTypeArguments())); | |
3480 // The signature_class should not have changed. | |
3481 ASSERT(actual_function_type.type_class() == signature_class.raw()); | |
3482 | 3472 |
3483 // Now patch the function type of the variable. | 3473 // Now patch the function type of the variable. |
3484 function_type.set_type_class(signature_class); | 3474 function_type.set_type_class(signature_class); |
3485 function_type.set_arguments(signature_type_arguments); | 3475 function_type.set_arguments(signature_type_arguments); |
3486 | 3476 |
3487 // The function variable type should have been patched above. | 3477 // The function variable type should have been patched above. |
3488 ASSERT((function_variable == NULL) || | 3478 ASSERT((function_variable == NULL) || |
3489 (function_variable->type().raw() == function_type.raw())); | 3479 (function_variable->type().raw() == function_type.raw())); |
3490 } | 3480 } |
3491 | 3481 |
(...skipping 3555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7047 } | 7037 } |
7048 | 7038 |
7049 | 7039 |
7050 void Parser::SkipNestedExpr() { | 7040 void Parser::SkipNestedExpr() { |
7051 const bool saved_mode = SetAllowFunctionLiterals(true); | 7041 const bool saved_mode = SetAllowFunctionLiterals(true); |
7052 SkipExpr(); | 7042 SkipExpr(); |
7053 SetAllowFunctionLiterals(saved_mode); | 7043 SetAllowFunctionLiterals(saved_mode); |
7054 } | 7044 } |
7055 | 7045 |
7056 } // namespace dart | 7046 } // namespace dart |
OLD | NEW |