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

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

Issue 8776020: Ongoing renaming of type classes: (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years 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 | Annotate | Revision Log
OLDNEW
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 template<typename T> 77 template<typename T>
78 static RawArray* NewArray(const GrowableArray<T*>& objs) { 78 static RawArray* NewArray(const GrowableArray<T*>& objs) {
79 Array& a = Array::Handle(Array::New(objs.length(), Heap::kOld)); 79 Array& a = Array::Handle(Array::New(objs.length(), Heap::kOld));
80 for (int i = 0; i < objs.length(); i++) { 80 for (int i = 0; i < objs.length(); i++) {
81 a.SetAt(i, *objs[i]); 81 a.SetAt(i, *objs[i]);
82 } 82 }
83 return a.raw(); 83 return a.raw();
84 } 84 }
85 85
86 86
87 static RawTypeArray* NewTypeArray(const GrowableArray<AbstractType*>& objs) { 87 static RawTypeArguments* NewTypeArguments(
88 TypeArray& a = TypeArray::Handle(TypeArray::New(objs.length())); 88 const GrowableArray<AbstractType*>& objs) {
89 TypeArguments& a = TypeArguments::Handle(TypeArguments::New(objs.length()));
89 for (int i = 0; i < objs.length(); i++) { 90 for (int i = 0; i < objs.length(); i++) {
90 a.SetTypeAt(i, *objs[i]); 91 a.SetTypeAt(i, *objs[i]);
91 } 92 }
92 return a.raw(); 93 return a.raw();
93 } 94 }
94 95
95 96
96 static ThrowNode* CreateEvalConstConstructorThrow(intptr_t token_pos, 97 static ThrowNode* CreateEvalConstConstructorThrow(intptr_t token_pos,
97 const Instance& instance) { 98 const Instance& instance) {
98 UnhandledException& excp = UnhandledException::Handle(); 99 UnhandledException& excp = UnhandledException::Handle();
(...skipping 1676 matching lines...) Expand 10 before | Expand all | Expand 10 after
1775 return ParseConstructor(func, default_parameter_values); 1776 return ParseConstructor(func, default_parameter_values);
1776 } 1777 }
1777 1778
1778 ASSERT(!func.IsConstructor()); 1779 ASSERT(!func.IsConstructor());
1779 OpenFunctionBlock(func); // Build local scope for function. 1780 OpenFunctionBlock(func); // Build local scope for function.
1780 1781
1781 ParamList params; 1782 ParamList params;
1782 // Static functions do not have a receiver. 1783 // Static functions do not have a receiver.
1783 // An instance closure may capture and access the receiver, but via the 1784 // An instance closure may capture and access the receiver, but via the
1784 // context and not via the first formal parameter. 1785 // context and not via the first formal parameter.
1785 // The first parameter of a factory is the TypeArguments vector of the type 1786 // The first parameter of a factory is the AbstractTypeArguments vector of the
1786 // of the instance to be allocated. We name this hidden parameter 'this'. 1787 // type of the instance to be allocated. We name this hidden parameter 'this'.
1787 const bool has_receiver = !func.IsClosureFunction() && 1788 const bool has_receiver = !func.IsClosureFunction() &&
1788 (!func.is_static() || func.IsFactory()); 1789 (!func.is_static() || func.IsFactory());
1789 const bool allow_explicit_default_values = true; 1790 const bool allow_explicit_default_values = true;
1790 if (has_receiver) { 1791 if (has_receiver) {
1791 params.AddReceiver(token_index_); 1792 params.AddReceiver(token_index_);
1792 } 1793 }
1793 ASSERT(CurrentToken() == Token::kLPAREN); 1794 ASSERT(CurrentToken() == Token::kLPAREN);
1794 ParseFormalParameterList(allow_explicit_default_values, &params); 1795 ParseFormalParameterList(allow_explicit_default_values, &params);
1795 1796
1796 // The number of parameters and their type are not yet set in local functions, 1797 // The number of parameters and their type are not yet set in local functions,
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 "'abstract' method only allowed in class definition"); 1974 "'abstract' method only allowed in class definition");
1974 } 1975 }
1975 1976
1976 if (members->FunctionNameExists(*method->name, method->kind)) { 1977 if (members->FunctionNameExists(*method->name, method->kind)) {
1977 ErrorMsg(method->name_pos, 1978 ErrorMsg(method->name_pos,
1978 "field or method '%s' already defined", method->name->ToCString()); 1979 "field or method '%s' already defined", method->name->ToCString());
1979 } 1980 }
1980 1981
1981 // Parse the formal parameters. 1982 // Parse the formal parameters.
1982 // The first parameter of factory methods is an implicit parameter called 1983 // The first parameter of factory methods is an implicit parameter called
1983 // 'this' of type TypeArguments. 1984 // 'this' of type AbstractTypeArguments.
1984 const bool has_this_param = 1985 const bool has_this_param =
1985 !method->has_static || method->IsConstructor() || method->has_factory; 1986 !method->has_static || method->IsConstructor() || method->has_factory;
1986 const bool are_implicitly_final = method->has_const; 1987 const bool are_implicitly_final = method->has_const;
1987 const bool allow_explicit_default_values = 1988 const bool allow_explicit_default_values =
1988 (!method->has_abstract && !members->is_interface()); 1989 (!method->has_abstract && !members->is_interface());
1989 const intptr_t formal_param_pos = token_index_; 1990 const intptr_t formal_param_pos = token_index_;
1990 method->params.Clear(); 1991 method->params.Clear();
1991 if (has_this_param) { 1992 if (has_this_param) {
1992 method->params.AddReceiver(formal_param_pos); 1993 method->params.AddReceiver(formal_param_pos);
1993 } 1994 }
(...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after
2844 } while (CurrentToken() == Token::kCOMMA); 2845 } while (CurrentToken() == Token::kCOMMA);
2845 Token::Kind token = CurrentToken(); 2846 Token::Kind token = CurrentToken();
2846 if ((token == Token::kGT) || 2847 if ((token == Token::kGT) ||
2847 (token == Token::kSAR) || 2848 (token == Token::kSAR) ||
2848 (token == Token::kSHR)) { 2849 (token == Token::kSHR)) {
2849 ConsumeRightAngleBracket(); 2850 ConsumeRightAngleBracket();
2850 } else { 2851 } else {
2851 ErrorMsg("right angle bracket expected"); 2852 ErrorMsg("right angle bracket expected");
2852 } 2853 }
2853 cls.set_type_parameters(Array::Handle(NewArray<String>(type_parameters))); 2854 cls.set_type_parameters(Array::Handle(NewArray<String>(type_parameters)));
2854 const TypeArray& extends_array = 2855 const TypeArguments& extends_array =
2855 TypeArray::Handle(NewTypeArray(type_parameter_extends)); 2856 TypeArguments::Handle(NewTypeArguments(type_parameter_extends));
2856 cls.set_type_parameter_extends(extends_array); 2857 cls.set_type_parameter_extends(extends_array);
2857 // Try to resolve the upper bounds, which will at least resolve the 2858 // Try to resolve the upper bounds, which will at least resolve the
2858 // referenced type parameters. 2859 // referenced type parameters.
2859 AbstractType& type_extends = AbstractType::Handle(); 2860 AbstractType& type_extends = AbstractType::Handle();
2860 const intptr_t num_types = extends_array.Length(); 2861 const intptr_t num_types = extends_array.Length();
2861 for (intptr_t i = 0; i < num_types; i++) { 2862 for (intptr_t i = 0; i < num_types; i++) {
2862 type_extends = extends_array.TypeAt(i); 2863 type_extends = extends_array.TypeAt(i);
2863 TryResolveTypeFromClass(type_pos, cls, &type_extends); 2864 TryResolveTypeFromClass(type_pos, cls, &type_extends);
2864 extends_array.SetTypeAt(i, type_extends); 2865 extends_array.SetTypeAt(i, type_extends);
2865 } 2866 }
2866 } 2867 }
2867 } 2868 }
2868 2869
2869 2870
2870 RawTypeArguments* Parser::ParseTypeArguments(TypeResolution type_resolution) { 2871 RawAbstractTypeArguments* Parser::ParseTypeArguments(
2872 TypeResolution type_resolution) {
2871 if (CurrentToken() == Token::kLT) { 2873 if (CurrentToken() == Token::kLT) {
2872 GrowableArray<AbstractType*> types; 2874 GrowableArray<AbstractType*> types;
2873 do { 2875 do {
2874 ConsumeToken(); 2876 ConsumeToken();
2875 AbstractType& type = AbstractType::ZoneHandle(ParseType(type_resolution)); 2877 AbstractType& type = AbstractType::ZoneHandle(ParseType(type_resolution));
2876 types.Add(&type); 2878 types.Add(&type);
2877 } while (CurrentToken() == Token::kCOMMA); 2879 } while (CurrentToken() == Token::kCOMMA);
2878 Token::Kind token = CurrentToken(); 2880 Token::Kind token = CurrentToken();
2879 if ((token == Token::kGT) || 2881 if ((token == Token::kGT) ||
2880 (token == Token::kSAR) || 2882 (token == Token::kSAR) ||
2881 (token == Token::kSHR)) { 2883 (token == Token::kSHR)) {
2882 ConsumeRightAngleBracket(); 2884 ConsumeRightAngleBracket();
2883 } else { 2885 } else {
2884 ErrorMsg("right angle bracket expected"); 2886 ErrorMsg("right angle bracket expected");
2885 } 2887 }
2886 return NewTypeArray(types); 2888 return NewTypeArguments(types);
2887 } 2889 }
2888 return TypeArray::null(); 2890 return TypeArguments::null();
2889 } 2891 }
2890 2892
2891 2893
2892 // Parse and return an array of interface types. 2894 // Parse and return an array of interface types.
2893 RawArray* Parser::ParseInterfaceList() { 2895 RawArray* Parser::ParseInterfaceList() {
2894 ASSERT((CurrentToken() == Token::kIMPLEMENTS) || 2896 ASSERT((CurrentToken() == Token::kIMPLEMENTS) ||
2895 (CurrentToken() == Token::kEXTENDS)); 2897 (CurrentToken() == Token::kEXTENDS));
2896 GrowableArray<AbstractType*> interfaces; 2898 GrowableArray<AbstractType*> interfaces;
2897 do { 2899 do {
2898 ConsumeToken(); 2900 ConsumeToken();
(...skipping 823 matching lines...) Expand 10 before | Expand all | Expand 10 after
3722 if ((signature_class.NumTypeParameters() > 0) && 3724 if ((signature_class.NumTypeParameters() > 0) &&
3723 (current_block_->scope->function_level() > 0)) { 3725 (current_block_->scope->function_level() > 0)) {
3724 CaptureReceiver(); 3726 CaptureReceiver();
3725 } 3727 }
3726 3728
3727 if (variable_name != NULL) { 3729 if (variable_name != NULL) {
3728 // Patch the function type now that the signature is known. 3730 // Patch the function type now that the signature is known.
3729 // We need to create a new type for proper finalization, since the existing 3731 // We need to create a new type for proper finalization, since the existing
3730 // type is already marked as finalized. 3732 // type is already marked as finalized.
3731 Type& signature_type = Type::Handle(signature_class.SignatureType()); 3733 Type& signature_type = Type::Handle(signature_class.SignatureType());
3732 const TypeArguments& signature_type_arguments = TypeArguments::Handle( 3734 const AbstractTypeArguments& signature_type_arguments =
3733 signature_type.arguments()); 3735 AbstractTypeArguments::Handle(signature_type.arguments());
3734 3736
3735 // Since the signature type is cached by the signature class, it may have 3737 // Since the signature type is cached by the signature class, it may have
3736 // been finalized already. 3738 // been finalized already.
3737 if (!signature_type.IsFinalized()) { 3739 if (!signature_type.IsFinalized()) {
3738 String& errmsg = String::Handle(); 3740 String& errmsg = String::Handle();
3739 signature_type = 3741 signature_type =
3740 ClassFinalizer::FinalizeAndCanonicalizeType(signature_type, &errmsg); 3742 ClassFinalizer::FinalizeAndCanonicalizeType(signature_type, &errmsg);
3741 if (!errmsg.IsNull()) { 3743 if (!errmsg.IsNull()) {
3742 ErrorMsg(errmsg.ToCString()); 3744 ErrorMsg(errmsg.ToCString());
3743 } 3745 }
(...skipping 2297 matching lines...) Expand 10 before | Expand all | Expand 10 after
6041 const UnresolvedClass& unresolved_class = 6043 const UnresolvedClass& unresolved_class =
6042 UnresolvedClass::Handle(type->unresolved_class()); 6044 UnresolvedClass::Handle(type->unresolved_class());
6043 const String& unresolved_class_name = 6045 const String& unresolved_class_name =
6044 String::Handle(unresolved_class.ident()); 6046 String::Handle(unresolved_class.ident());
6045 // First check if the type is a type parameter of the given class. 6047 // First check if the type is a type parameter of the given class.
6046 const TypeParameter& type_parameter = TypeParameter::Handle( 6048 const TypeParameter& type_parameter = TypeParameter::Handle(
6047 cls.LookupTypeParameter(unresolved_class_name)); 6049 cls.LookupTypeParameter(unresolved_class_name));
6048 if (!type_parameter.IsNull()) { 6050 if (!type_parameter.IsNull()) {
6049 // A type parameter cannot be parameterized, so report an error if type 6051 // A type parameter cannot be parameterized, so report an error if type
6050 // arguments have previously been parsed. 6052 // arguments have previously been parsed.
6051 if (!TypeArguments::Handle(type->arguments()).IsNull()) { 6053 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) {
6052 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized", 6054 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized",
6053 type_parameter.ToCString()); 6055 type_parameter.ToCString());
6054 } 6056 }
6055 *type = type_parameter.raw(); 6057 *type = type_parameter.raw();
6056 return; 6058 return;
6057 } 6059 }
6058 const Class& resolved_type_class = 6060 const Class& resolved_type_class =
6059 Class::Handle(LookupClass(unresolved_class_name)); 6061 Class::Handle(LookupClass(unresolved_class_name));
6060 if (!resolved_type_class.IsNull()) { 6062 if (!resolved_type_class.IsNull()) {
6061 Object& type_class = Object::Handle(resolved_type_class.raw()); 6063 Object& type_class = Object::Handle(resolved_type_class.raw());
6062 ASSERT(type->IsType()); 6064 ASSERT(type->IsType());
6063 // Replace unresolved class with resolved type class. 6065 // Replace unresolved class with resolved type class.
6064 Type& parameterized_type = Type::Handle(); 6066 Type& parameterized_type = Type::Handle();
6065 parameterized_type ^= type->raw(); 6067 parameterized_type ^= type->raw();
6066 parameterized_type.set_type_class(type_class); 6068 parameterized_type.set_type_class(type_class);
6067 } 6069 }
6068 } 6070 }
6069 // Resolve type arguments, if any. 6071 // Resolve type arguments, if any.
6070 const TypeArguments& arguments = TypeArguments::Handle(type->arguments()); 6072 const AbstractTypeArguments& arguments =
6073 AbstractTypeArguments::Handle(type->arguments());
6071 if (!arguments.IsNull()) { 6074 if (!arguments.IsNull()) {
6072 const intptr_t num_arguments = arguments.Length(); 6075 const intptr_t num_arguments = arguments.Length();
6073 for (intptr_t i = 0; i < num_arguments; i++) { 6076 for (intptr_t i = 0; i < num_arguments; i++) {
6074 AbstractType& type_argument = AbstractType::Handle(arguments.TypeAt(i)); 6077 AbstractType& type_argument = AbstractType::Handle(arguments.TypeAt(i));
6075 TryResolveTypeFromClass(type_pos, cls, &type_argument); 6078 TryResolveTypeFromClass(type_pos, cls, &type_argument);
6076 arguments.SetTypeAt(i, type_argument); 6079 arguments.SetTypeAt(i, type_argument);
6077 } 6080 }
6078 } 6081 }
6079 } 6082 }
6080 6083
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
6236 if (!const_value.IsNull()) { 6239 if (!const_value.IsNull()) {
6237 const_value ^= const_value.Canonicalize(); 6240 const_value ^= const_value.Canonicalize();
6238 } 6241 }
6239 field.set_value(const_value); 6242 field.set_value(const_value);
6240 } 6243 }
6241 } 6244 }
6242 6245
6243 6246
6244 RawInstance* Parser::EvaluateConstConstructorCall( 6247 RawInstance* Parser::EvaluateConstConstructorCall(
6245 const Class& type_class, 6248 const Class& type_class,
6246 const TypeArguments& type_arguments, 6249 const AbstractTypeArguments& type_arguments,
6247 const Function& constructor, 6250 const Function& constructor,
6248 ArgumentListNode* arguments) { 6251 ArgumentListNode* arguments) {
6249 // +2 for implicit receiver and construction phase arguments. 6252 // +2 for implicit receiver and construction phase arguments.
6250 GrowableArray<const Object*> arg_values(arguments->length() + 2); 6253 GrowableArray<const Object*> arg_values(arguments->length() + 2);
6251 Instance& instance = Instance::Handle(); 6254 Instance& instance = Instance::Handle();
6252 if (!constructor.IsFactory()) { 6255 if (!constructor.IsFactory()) {
6253 instance = Instance::New(type_class); 6256 instance = Instance::New(type_class);
6254 if (!type_arguments.IsNull()) { 6257 if (!type_arguments.IsNull()) {
6255 // TODO(regis): Where should we check the constraints on type parameters? 6258 // TODO(regis): Where should we check the constraints on type parameters?
6256 if (!type_arguments.IsInstantiated()) { 6259 if (!type_arguments.IsInstantiated()) {
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
6563 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized", 6566 ErrorMsg(type_pos, "type parameter '%s' cannot be parameterized",
6564 String::Handle(type_parameter.Name()).ToCString()); 6567 String::Handle(type_parameter.Name()).ToCString());
6565 } 6568 }
6566 return type_parameter.raw(); 6569 return type_parameter.raw();
6567 } 6570 }
6568 } 6571 }
6569 } 6572 }
6570 // Try to resolve the type class. 6573 // Try to resolve the type class.
6571 type_class = LookupTypeClass(type_name, type_resolution); 6574 type_class = LookupTypeClass(type_name, type_resolution);
6572 } 6575 }
6573 TypeArguments& type_arguments = 6576 AbstractTypeArguments& type_arguments =
6574 TypeArguments::Handle(ParseTypeArguments(type_resolution)); 6577 AbstractTypeArguments::Handle(ParseTypeArguments(type_resolution));
6575 Type& type = Type::Handle( 6578 Type& type = Type::Handle(
6576 Type::NewParameterizedType(type_class, type_arguments)); 6579 Type::NewParameterizedType(type_class, type_arguments));
6577 if (type_resolution == kMustResolve) { 6580 if (type_resolution == kMustResolve) {
6578 ASSERT(type_class.IsClass()); // Must be resolved. 6581 ASSERT(type_class.IsClass()); // Must be resolved.
6579 String& errmsg = String::Handle(); 6582 String& errmsg = String::Handle();
6580 type = ClassFinalizer::FinalizeAndCanonicalizeType(type, &errmsg); 6583 type = ClassFinalizer::FinalizeAndCanonicalizeType(type, &errmsg);
6581 if (!errmsg.IsNull()) { 6584 if (!errmsg.IsNull()) {
6582 ErrorMsg(errmsg.ToCString()); 6585 ErrorMsg(errmsg.ToCString());
6583 } 6586 }
6584 } 6587 }
6585 return type.raw(); 6588 return type.raw();
6586 } 6589 }
6587 6590
6588 6591
6589 void Parser::CheckConstructorCallTypeArguments( 6592 void Parser::CheckConstructorCallTypeArguments(
6590 intptr_t pos, Function& constructor, const TypeArguments& type_arguments) { 6593 intptr_t pos, Function& constructor,
6594 const AbstractTypeArguments& type_arguments) {
6591 if (!type_arguments.IsNull()) { 6595 if (!type_arguments.IsNull()) {
6592 Class& signature_class = Class::Handle(); 6596 Class& signature_class = Class::Handle();
6593 if (constructor.IsFactory()) { 6597 if (constructor.IsFactory()) {
6594 signature_class = constructor.signature_class(); 6598 signature_class = constructor.signature_class();
6595 } else { 6599 } else {
6596 signature_class = constructor.owner(); 6600 signature_class = constructor.owner();
6597 } 6601 }
6598 ASSERT(!signature_class.IsNull()); 6602 ASSERT(!signature_class.IsNull());
6599 ASSERT(signature_class.is_finalized()); 6603 ASSERT(signature_class.is_finalized());
6600 // Do not report the expected vs. actual number of type arguments, because 6604 // Do not report the expected vs. actual number of type arguments, because
6601 // the type argument vector is flattened and raw types are allowed. 6605 // the type argument vector is flattened and raw types are allowed.
6602 if (type_arguments.Length() != signature_class.NumTypeArguments()) { 6606 if (type_arguments.Length() != signature_class.NumTypeArguments()) {
6603 ErrorMsg(pos, "wrong number of type arguments passed to constructor"); 6607 ErrorMsg(pos, "wrong number of type arguments passed to constructor");
6604 } 6608 }
6605 } 6609 }
6606 } 6610 }
6607 6611
6608 6612
6609 // Parse "[" [ expr { "," expr } ["," ] "]". 6613 // Parse "[" [ expr { "," expr } ["," ] "]".
6610 // Note: if the list literal is empty and the brackets have no whitespace 6614 // Note: if the list literal is empty and the brackets have no whitespace
6611 // between them, the scanner recognizes the opening and closing bracket 6615 // between them, the scanner recognizes the opening and closing bracket
6612 // as one token of type Token::kINDEX. 6616 // as one token of type Token::kINDEX.
6613 AstNode* Parser::ParseListLiteral(intptr_t type_pos, 6617 AstNode* Parser::ParseListLiteral(intptr_t type_pos,
6614 bool is_const, 6618 bool is_const,
6615 const TypeArguments& type_arguments) { 6619 const AbstractTypeArguments& type_arguments) {
6616 TRACE_PARSER("ParseListLiteral"); 6620 TRACE_PARSER("ParseListLiteral");
6617 ASSERT(type_pos >= 0); 6621 ASSERT(type_pos >= 0);
6618 ASSERT(CurrentToken() == Token::kLBRACK || CurrentToken() == Token::kINDEX); 6622 ASSERT(CurrentToken() == Token::kLBRACK || CurrentToken() == Token::kINDEX);
6619 const intptr_t literal_pos = token_index_; 6623 const intptr_t literal_pos = token_index_;
6620 bool is_empty_literal = CurrentToken() == Token::kINDEX; 6624 bool is_empty_literal = CurrentToken() == Token::kINDEX;
6621 ConsumeToken(); 6625 ConsumeToken();
6622 6626
6623 AbstractType& element_type = Type::ZoneHandle(Type::DynamicType()); 6627 AbstractType& element_type = Type::ZoneHandle(Type::DynamicType());
6624 // If no type argument vector is provided, leave it as null, which is 6628 // If no type argument vector is provided, leave it as null, which is
6625 // equivalent to using Dynamic as the type argument for the element type. 6629 // equivalent to using Dynamic as the type argument for the element type.
6626 if (!type_arguments.IsNull()) { 6630 if (!type_arguments.IsNull()) {
6627 ASSERT(type_arguments.Length() > 0); 6631 ASSERT(type_arguments.Length() > 0);
6628 // List literals take a single type argument. 6632 // List literals take a single type argument.
6629 element_type = type_arguments.TypeAt(0); 6633 element_type = type_arguments.TypeAt(0);
6630 if (type_arguments.Length() != 1) { 6634 if (type_arguments.Length() != 1) {
6631 ErrorMsg(type_pos, 6635 ErrorMsg(type_pos,
6632 "a list literal takes one type argument specifying " 6636 "a list literal takes one type argument specifying "
6633 "the element type"); 6637 "the element type");
6634 } 6638 }
6635 if (is_const && !element_type.IsInstantiated()) { 6639 if (is_const && !element_type.IsInstantiated()) {
6636 ErrorMsg(type_pos, 6640 ErrorMsg(type_pos,
6637 "the type argument of a constant list literal cannot include " 6641 "the type argument of a constant list literal cannot include "
6638 "a type variable"); 6642 "a type variable");
6639 } 6643 }
6640 } 6644 }
6641 ASSERT(type_arguments.IsNull() || (type_arguments.Length() == 1)); 6645 ASSERT(type_arguments.IsNull() || (type_arguments.Length() == 1));
6642 6646
6643 // Parse the list elements. Note: there may be an optional extra 6647 // Parse the list elements. Note: there may be an optional extra
6644 // comma after the last element. 6648 // comma after the last element.
6645 ArrayNode* list = new ArrayNode(token_index_, TypeArguments::ZoneHandle()); 6649 ArrayNode* list =
6650 new ArrayNode(token_index_, TypeArguments::ZoneHandle());
6646 if (!is_empty_literal) { 6651 if (!is_empty_literal) {
6647 const bool saved_mode = SetAllowFunctionLiterals(true); 6652 const bool saved_mode = SetAllowFunctionLiterals(true);
6648 const String& dst_name = String::ZoneHandle( 6653 const String& dst_name = String::ZoneHandle(
6649 String::NewSymbol("list literal element")); 6654 String::NewSymbol("list literal element"));
6650 while (CurrentToken() != Token::kRBRACK) { 6655 while (CurrentToken() != Token::kRBRACK) {
6651 const intptr_t element_pos = token_index_; 6656 const intptr_t element_pos = token_index_;
6652 AstNode* element = ParseExpr(is_const); 6657 AstNode* element = ParseExpr(is_const);
6653 if (FLAG_enable_type_checks && 6658 if (FLAG_enable_type_checks &&
6654 !is_const && 6659 !is_const &&
6655 !element_type.IsDynamicType()) { 6660 !element_type.IsDynamicType()) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
6748 } 6753 }
6749 } 6754 }
6750 } 6755 }
6751 pairs->AddElement(key); 6756 pairs->AddElement(key);
6752 pairs->AddElement(value); 6757 pairs->AddElement(value);
6753 } 6758 }
6754 6759
6755 6760
6756 AstNode* Parser::ParseMapLiteral(intptr_t type_pos, 6761 AstNode* Parser::ParseMapLiteral(intptr_t type_pos,
6757 bool is_const, 6762 bool is_const,
6758 const TypeArguments& type_arguments) { 6763 const AbstractTypeArguments& type_arguments) {
6759 TRACE_PARSER("ParseMapLiteral"); 6764 TRACE_PARSER("ParseMapLiteral");
6760 ASSERT(type_pos >= 0); 6765 ASSERT(type_pos >= 0);
6761 ASSERT(CurrentToken() == Token::kLBRACE); 6766 ASSERT(CurrentToken() == Token::kLBRACE);
6762 const intptr_t literal_pos = token_index_; 6767 const intptr_t literal_pos = token_index_;
6763 ConsumeToken(); 6768 ConsumeToken();
6764 6769
6765 AbstractType& value_type = Type::ZoneHandle(Type::DynamicType()); 6770 AbstractType& value_type = Type::ZoneHandle(Type::DynamicType());
6766 TypeArguments& map_type_arguments = 6771 AbstractTypeArguments& map_type_arguments =
6767 TypeArguments::ZoneHandle(type_arguments.raw()); 6772 AbstractTypeArguments::ZoneHandle(type_arguments.raw());
6768 // If no type argument vector is provided, leave it as null, which is 6773 // If no type argument vector is provided, leave it as null, which is
6769 // equivalent to using Dynamic as the type argument for the value type. 6774 // equivalent to using Dynamic as the type argument for the value type.
6770 if (!map_type_arguments.IsNull()) { 6775 if (!map_type_arguments.IsNull()) {
6771 ASSERT(map_type_arguments.Length() > 0); 6776 ASSERT(map_type_arguments.Length() > 0);
6772 // Map literals take a single type argument. 6777 // Map literals take a single type argument.
6773 value_type = map_type_arguments.TypeAt(0); 6778 value_type = map_type_arguments.TypeAt(0);
6774 if (map_type_arguments.Length() > 1) { 6779 if (map_type_arguments.Length() > 1) {
6775 // We temporarily accept two type arguments, as long as the first one is 6780 // We temporarily accept two type arguments, as long as the first one is
6776 // type String. 6781 // type String.
6777 if (map_type_arguments.Length() != 2) { 6782 if (map_type_arguments.Length() != 2) {
6778 ErrorMsg(type_pos, 6783 ErrorMsg(type_pos,
6779 "a map literal takes one type argument specifying " 6784 "a map literal takes one type argument specifying "
6780 "the value type"); 6785 "the value type");
6781 } 6786 }
6782 if (!value_type.IsStringInterface()) { 6787 if (!value_type.IsStringInterface()) {
6783 ErrorMsg(type_pos, 6788 ErrorMsg(type_pos,
6784 "the key type of a map literal is implicitly 'String'"); 6789 "the key type of a map literal is implicitly 'String'");
6785 } 6790 }
6786 Warning(type_pos, 6791 Warning(type_pos,
6787 "a map literal takes one type argument specifying " 6792 "a map literal takes one type argument specifying "
6788 "the value type"); 6793 "the value type");
6789 value_type = map_type_arguments.TypeAt(1); 6794 value_type = map_type_arguments.TypeAt(1);
6790 } else { 6795 } else {
6791 TypeArray& type_array = TypeArray::Handle(TypeArray::New(2)); 6796 TypeArguments& type_array = TypeArguments::Handle(TypeArguments::New(2));
6792 type_array.SetTypeAt(0, Type::Handle(Type::StringInterface())); 6797 type_array.SetTypeAt(0, Type::Handle(Type::StringInterface()));
6793 type_array.SetTypeAt(1, value_type); 6798 type_array.SetTypeAt(1, value_type);
6794 map_type_arguments = type_array.raw(); 6799 map_type_arguments = type_array.raw();
6795 } 6800 }
6796 if (is_const && !value_type.IsInstantiated()) { 6801 if (is_const && !value_type.IsInstantiated()) {
6797 ErrorMsg(type_pos, 6802 ErrorMsg(type_pos,
6798 "the type argument of a constant map literal cannot include " 6803 "the type argument of a constant map literal cannot include "
6799 "a type variable"); 6804 "a type variable");
6800 } 6805 }
6801 } 6806 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
6926 } 6931 }
6927 6932
6928 6933
6929 AstNode* Parser::ParseCompoundLiteral() { 6934 AstNode* Parser::ParseCompoundLiteral() {
6930 bool is_const = false; 6935 bool is_const = false;
6931 if (CurrentToken() == Token::kCONST) { 6936 if (CurrentToken() == Token::kCONST) {
6932 is_const = true; 6937 is_const = true;
6933 ConsumeToken(); 6938 ConsumeToken();
6934 } 6939 }
6935 const intptr_t type_pos = token_index_; 6940 const intptr_t type_pos = token_index_;
6936 TypeArguments& type_arguments = 6941 AbstractTypeArguments& type_arguments =
6937 TypeArguments::ZoneHandle(ParseTypeArguments(kMustResolve)); 6942 AbstractTypeArguments::ZoneHandle(ParseTypeArguments(kMustResolve));
6938 AstNode* primary = NULL; 6943 AstNode* primary = NULL;
6939 if ((CurrentToken() == Token::kLBRACK) || 6944 if ((CurrentToken() == Token::kLBRACK) ||
6940 (CurrentToken() == Token::kINDEX)) { 6945 (CurrentToken() == Token::kINDEX)) {
6941 primary = ParseListLiteral(type_pos, is_const, type_arguments); 6946 primary = ParseListLiteral(type_pos, is_const, type_arguments);
6942 } else if (CurrentToken() == Token::kLBRACE) { 6947 } else if (CurrentToken() == Token::kLBRACE) {
6943 primary = ParseMapLiteral(type_pos, is_const, type_arguments); 6948 primary = ParseMapLiteral(type_pos, is_const, type_arguments);
6944 } else { 6949 } else {
6945 ErrorMsg("unexpected token %s", Token::Str(CurrentToken())); 6950 ErrorMsg("unexpected token %s", Token::Str(CurrentToken()));
6946 } 6951 }
6947 return primary; 6952 return primary;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
7007 type_parameter = scope_class.LookupTypeParameter(*type_name.ident); 7012 type_parameter = scope_class.LookupTypeParameter(*type_name.ident);
7008 if (!type_parameter.IsNull()) { 7013 if (!type_parameter.IsNull()) {
7009 ErrorMsg(type_pos, "type parameter '%s' cannot be instantiated", 7014 ErrorMsg(type_pos, "type parameter '%s' cannot be instantiated",
7010 String::Handle(type_parameter.Name()).ToCString()); 7015 String::Handle(type_parameter.Name()).ToCString());
7011 } 7016 }
7012 } 7017 }
7013 Class& type_class = Class::ZoneHandle(); 7018 Class& type_class = Class::ZoneHandle();
7014 type_class ^= LookupTypeClass(type_name, kMustResolve); 7019 type_class ^= LookupTypeClass(type_name, kMustResolve);
7015 String& type_class_name = String::Handle(); 7020 String& type_class_name = String::Handle();
7016 type_class_name = type_class.Name(); 7021 type_class_name = type_class.Name();
7017 TypeArguments& type_arguments = TypeArguments::ZoneHandle(); 7022 AbstractTypeArguments& type_arguments = AbstractTypeArguments::ZoneHandle();
7018 // Type arguments are not allowed after the optional constructor name. 7023 // Type arguments are not allowed after the optional constructor name.
7019 if (named_constructor == NULL) { 7024 if (named_constructor == NULL) {
7020 type_arguments = ParseTypeArguments(kMustResolve); 7025 type_arguments = ParseTypeArguments(kMustResolve);
7021 } 7026 }
7022 if ((named_constructor == NULL) && (CurrentToken() == Token::kPERIOD)) { 7027 if ((named_constructor == NULL) && (CurrentToken() == Token::kPERIOD)) {
7023 ConsumeToken(); 7028 ConsumeToken();
7024 named_constructor = ExpectIdentifier("name of constructor expected"); 7029 named_constructor = ExpectIdentifier("name of constructor expected");
7025 } 7030 }
7026 7031
7027 // Parse constructor parameters. 7032 // Parse constructor parameters.
(...skipping 563 matching lines...) Expand 10 before | Expand all | Expand 10 after
7591 } 7596 }
7592 7597
7593 7598
7594 void Parser::SkipNestedExpr() { 7599 void Parser::SkipNestedExpr() {
7595 const bool saved_mode = SetAllowFunctionLiterals(true); 7600 const bool saved_mode = SetAllowFunctionLiterals(true);
7596 SkipExpr(); 7601 SkipExpr();
7597 SetAllowFunctionLiterals(saved_mode); 7602 SetAllowFunctionLiterals(saved_mode);
7598 } 7603 }
7599 7604
7600 } // namespace dart 7605 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698