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 "vm/bigint_operations.h" | 8 #include "vm/bigint_operations.h" |
9 #include "vm/bootstrap.h" | 9 #include "vm/bootstrap.h" |
10 #include "vm/class_finalizer.h" | 10 #include "vm/class_finalizer.h" |
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 GrowableObjectArray::Handle( | 301 GrowableObjectArray::Handle( |
302 isolate()->object_store()->pending_functions()); | 302 isolate()->object_store()->pending_functions()); |
303 ASSERT(pending_functions.Length() > 0); | 303 ASSERT(pending_functions.Length() > 0); |
304 ASSERT(pending_functions.At(pending_functions.Length()-1) == | 304 ASSERT(pending_functions.At(pending_functions.Length()-1) == |
305 current_function().raw()); | 305 current_function().raw()); |
306 pending_functions.RemoveLast(); | 306 pending_functions.RemoveLast(); |
307 } | 307 } |
308 } | 308 } |
309 | 309 |
310 | 310 |
311 void Parser::SetScript(const Script & script, intptr_t token_pos) { | 311 void Parser::SetScript(const Script& script, intptr_t token_pos) { |
312 script_ = script.raw(); | 312 script_ = script.raw(); |
313 tokens_iterator_.SetStream(TokenStream::Handle(script.tokens()), token_pos); | 313 tokens_iterator_.SetStream(TokenStream::Handle(script.tokens()), token_pos); |
314 token_kind_ = Token::kILLEGAL; | 314 token_kind_ = Token::kILLEGAL; |
315 } | 315 } |
316 | 316 |
317 | 317 |
318 bool Parser::SetAllowFunctionLiterals(bool value) { | 318 bool Parser::SetAllowFunctionLiterals(bool value) { |
319 bool current_value = allow_function_literals_; | 319 bool current_value = allow_function_literals_; |
320 allow_function_literals_ = value; | 320 allow_function_literals_ = value; |
321 return current_value; | 321 return current_value; |
(...skipping 2794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3116 String& redirection_identifier = String::Handle(); | 3116 String& redirection_identifier = String::Handle(); |
3117 if (method->IsFactory() && (CurrentToken() == Token::kASSIGN)) { | 3117 if (method->IsFactory() && (CurrentToken() == Token::kASSIGN)) { |
3118 ConsumeToken(); | 3118 ConsumeToken(); |
3119 const intptr_t type_pos = TokenPos(); | 3119 const intptr_t type_pos = TokenPos(); |
3120 const AbstractType& type = AbstractType::Handle( | 3120 const AbstractType& type = AbstractType::Handle( |
3121 ParseType(ClassFinalizer::kResolveTypeParameters)); | 3121 ParseType(ClassFinalizer::kResolveTypeParameters)); |
3122 if (!type.IsMalformed() && type.IsTypeParameter()) { | 3122 if (!type.IsMalformed() && type.IsTypeParameter()) { |
3123 // Replace the type with a malformed type and compile a throw when called. | 3123 // Replace the type with a malformed type and compile a throw when called. |
3124 redirection_type = ClassFinalizer::NewFinalizedMalformedType( | 3124 redirection_type = ClassFinalizer::NewFinalizedMalformedType( |
3125 Error::Handle(), // No previous error. | 3125 Error::Handle(), // No previous error. |
3126 current_class(), | 3126 script_, |
3127 type_pos, | 3127 type_pos, |
3128 "factory '%s' may not redirect to type parameter '%s'", | 3128 "factory '%s' may not redirect to type parameter '%s'", |
3129 method->name->ToCString(), | 3129 method->name->ToCString(), |
3130 String::Handle(type.UserVisibleName()).ToCString()); | 3130 String::Handle(type.UserVisibleName()).ToCString()); |
3131 } else { | 3131 } else { |
3132 // TODO(regis): What if the redirection type is malbounded? | 3132 // TODO(regis): What if the redirection type is malbounded? |
3133 redirection_type ^= type.raw(); | 3133 redirection_type ^= type.raw(); |
3134 } | 3134 } |
3135 if (CurrentToken() == Token::kPERIOD) { | 3135 if (CurrentToken() == Token::kPERIOD) { |
3136 // Named constructor or factory. | 3136 // Named constructor or factory. |
(...skipping 5392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8529 scope_class.LookupTypeParameter(unresolved_class_name)); | 8529 scope_class.LookupTypeParameter(unresolved_class_name)); |
8530 if (!type_parameter.IsNull()) { | 8530 if (!type_parameter.IsNull()) { |
8531 // A type parameter is considered to be a malformed type when | 8531 // A type parameter is considered to be a malformed type when |
8532 // referenced by a static member. | 8532 // referenced by a static member. |
8533 if (ParsingStaticMember()) { | 8533 if (ParsingStaticMember()) { |
8534 ASSERT(scope_class.raw() == current_class().raw()); | 8534 ASSERT(scope_class.raw() == current_class().raw()); |
8535 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || | 8535 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || |
8536 FLAG_error_on_bad_type) { | 8536 FLAG_error_on_bad_type) { |
8537 *type = ClassFinalizer::NewFinalizedMalformedType( | 8537 *type = ClassFinalizer::NewFinalizedMalformedType( |
8538 Error::Handle(), // No previous error. | 8538 Error::Handle(), // No previous error. |
8539 scope_class, | 8539 script_, |
8540 type->token_pos(), | 8540 type->token_pos(), |
8541 "type parameter '%s' cannot be referenced " | 8541 "type parameter '%s' cannot be referenced " |
8542 "from static member", | 8542 "from static member", |
8543 String::Handle(type_parameter.name()).ToCString()); | 8543 String::Handle(type_parameter.name()).ToCString()); |
8544 } else { | 8544 } else { |
8545 // Map the malformed type to dynamic and ignore type arguments. | 8545 // Map the malformed type to dynamic and ignore type arguments. |
8546 *type = Type::DynamicType(); | 8546 *type = Type::DynamicType(); |
8547 } | 8547 } |
8548 return; | 8548 return; |
8549 } | 8549 } |
8550 // A type parameter cannot be parameterized, so make the type | 8550 // A type parameter cannot be parameterized, so make the type |
8551 // malformed if type arguments have previously been parsed. | 8551 // malformed if type arguments have previously been parsed. |
8552 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { | 8552 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { |
8553 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || | 8553 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || |
8554 FLAG_error_on_bad_type) { | 8554 FLAG_error_on_bad_type) { |
8555 *type = ClassFinalizer::NewFinalizedMalformedType( | 8555 *type = ClassFinalizer::NewFinalizedMalformedType( |
8556 Error::Handle(), // No previous error. | 8556 Error::Handle(), // No previous error. |
8557 scope_class, | 8557 script_, |
8558 type_parameter.token_pos(), | 8558 type_parameter.token_pos(), |
8559 "type parameter '%s' cannot be parameterized", | 8559 "type parameter '%s' cannot be parameterized", |
8560 String::Handle(type_parameter.name()).ToCString()); | 8560 String::Handle(type_parameter.name()).ToCString()); |
8561 } else { | 8561 } else { |
8562 // Map the malformed type to dynamic and ignore type arguments. | 8562 // Map the malformed type to dynamic and ignore type arguments. |
8563 *type = Type::DynamicType(); | 8563 *type = Type::DynamicType(); |
8564 } | 8564 } |
8565 return; | 8565 return; |
8566 } | 8566 } |
8567 *type = type_parameter.raw(); | 8567 *type = type_parameter.raw(); |
(...skipping 17 matching lines...) Expand all Loading... |
8585 // At this point, we can only have a parameterized_type. | 8585 // At this point, we can only have a parameterized_type. |
8586 const Type& parameterized_type = Type::Cast(*type); | 8586 const Type& parameterized_type = Type::Cast(*type); |
8587 if (!resolved_type_class.IsNull()) { | 8587 if (!resolved_type_class.IsNull()) { |
8588 // Replace unresolved class with resolved type class. | 8588 // Replace unresolved class with resolved type class. |
8589 parameterized_type.set_type_class(resolved_type_class); | 8589 parameterized_type.set_type_class(resolved_type_class); |
8590 } else if (finalization >= ClassFinalizer::kCanonicalize) { | 8590 } else if (finalization >= ClassFinalizer::kCanonicalize) { |
8591 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || | 8591 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || |
8592 FLAG_error_on_bad_type) { | 8592 FLAG_error_on_bad_type) { |
8593 ClassFinalizer::FinalizeMalformedType( | 8593 ClassFinalizer::FinalizeMalformedType( |
8594 Error::Handle(), // No previous error. | 8594 Error::Handle(), // No previous error. |
8595 scope_class, | 8595 script_, |
8596 parameterized_type, | 8596 parameterized_type, |
8597 "type '%s' is not loaded", | 8597 "type '%s' is not loaded", |
8598 String::Handle(parameterized_type.UserVisibleName()).ToCString()); | 8598 String::Handle(parameterized_type.UserVisibleName()).ToCString()); |
8599 } else { | 8599 } else { |
8600 // Map the malformed type to dynamic and ignore type arguments. | 8600 // Map the malformed type to dynamic and ignore type arguments. |
8601 *type = Type::DynamicType(); | 8601 *type = Type::DynamicType(); |
8602 } | 8602 } |
8603 return; | 8603 return; |
8604 } | 8604 } |
8605 } | 8605 } |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9180 ParseQualIdent(&type_name); | 9180 ParseQualIdent(&type_name); |
9181 // An identifier cannot be resolved in a local scope when top level parsing. | 9181 // An identifier cannot be resolved in a local scope when top level parsing. |
9182 if (!is_top_level_ && | 9182 if (!is_top_level_ && |
9183 (type_name.lib_prefix == NULL) && | 9183 (type_name.lib_prefix == NULL) && |
9184 ResolveIdentInLocalScope(type_name.ident_pos, *type_name.ident, NULL)) { | 9184 ResolveIdentInLocalScope(type_name.ident_pos, *type_name.ident, NULL)) { |
9185 // The type is malformed. Skip over its type arguments. | 9185 // The type is malformed. Skip over its type arguments. |
9186 ParseTypeArguments(ClassFinalizer::kIgnore); | 9186 ParseTypeArguments(ClassFinalizer::kIgnore); |
9187 if (finalization == ClassFinalizer::kCanonicalizeWellFormed) { | 9187 if (finalization == ClassFinalizer::kCanonicalizeWellFormed) { |
9188 return ClassFinalizer::NewFinalizedMalformedType( | 9188 return ClassFinalizer::NewFinalizedMalformedType( |
9189 Error::Handle(), // No previous error. | 9189 Error::Handle(), // No previous error. |
9190 current_class(), | 9190 script_, |
9191 type_name.ident_pos, | 9191 type_name.ident_pos, |
9192 "using '%s' in this context is invalid", | 9192 "using '%s' in this context is invalid", |
9193 type_name.ident->ToCString()); | 9193 type_name.ident->ToCString()); |
9194 } | 9194 } |
9195 return Type::DynamicType(); | 9195 return Type::DynamicType(); |
9196 } | 9196 } |
9197 } | 9197 } |
9198 Object& type_class = Object::Handle(isolate()); | 9198 Object& type_class = Object::Handle(isolate()); |
9199 // Leave type_class as null if type finalization mode is kIgnore. | 9199 // Leave type_class as null if type finalization mode is kIgnore. |
9200 if (finalization != ClassFinalizer::kIgnore) { | 9200 if (finalization != ClassFinalizer::kIgnore) { |
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9743 intptr_t type_pos = TokenPos(); | 9743 intptr_t type_pos = TokenPos(); |
9744 AbstractType& type = AbstractType::Handle( | 9744 AbstractType& type = AbstractType::Handle( |
9745 ParseType(ClassFinalizer::kCanonicalizeWellFormed)); | 9745 ParseType(ClassFinalizer::kCanonicalizeWellFormed)); |
9746 // In case the type is malformed, throw a dynamic type error after finishing | 9746 // In case the type is malformed, throw a dynamic type error after finishing |
9747 // parsing the instance creation expression. | 9747 // parsing the instance creation expression. |
9748 if (!type.IsMalformed()) { | 9748 if (!type.IsMalformed()) { |
9749 if (type.IsTypeParameter() || type.IsDynamicType()) { | 9749 if (type.IsTypeParameter() || type.IsDynamicType()) { |
9750 // Replace the type with a malformed type. | 9750 // Replace the type with a malformed type. |
9751 type = ClassFinalizer::NewFinalizedMalformedType( | 9751 type = ClassFinalizer::NewFinalizedMalformedType( |
9752 Error::Handle(), // No previous error. | 9752 Error::Handle(), // No previous error. |
9753 current_class(), | 9753 script_, |
9754 type_pos, | 9754 type_pos, |
9755 "%s'%s' cannot be instantiated", | 9755 "%s'%s' cannot be instantiated", |
9756 type.IsTypeParameter() ? "type parameter " : "", | 9756 type.IsTypeParameter() ? "type parameter " : "", |
9757 type.IsTypeParameter() ? | 9757 type.IsTypeParameter() ? |
9758 String::Handle(type.UserVisibleName()).ToCString() : "dynamic"); | 9758 String::Handle(type.UserVisibleName()).ToCString() : "dynamic"); |
9759 } else if (FLAG_enable_type_checks || FLAG_error_on_bad_type) { | 9759 } else if (FLAG_enable_type_checks || FLAG_error_on_bad_type) { |
9760 Error& bound_error = Error::Handle(); | 9760 Error& bound_error = Error::Handle(); |
9761 if (type.IsMalboundedWithError(&bound_error)) { | 9761 if (type.IsMalboundedWithError(&bound_error)) { |
9762 // Replace the type with a malformed type. | 9762 // Replace the type with a malformed type. |
9763 type = ClassFinalizer::NewFinalizedMalformedType( | 9763 type = ClassFinalizer::NewFinalizedMalformedType( |
9764 bound_error, | 9764 bound_error, |
9765 current_class(), | 9765 script_, |
9766 type_pos, | 9766 type_pos, |
9767 "malbounded type '%s' cannot be instantiated", | 9767 "malbounded type '%s' cannot be instantiated", |
9768 String::Handle(type.UserVisibleName()).ToCString()); | 9768 String::Handle(type.UserVisibleName()).ToCString()); |
9769 } | 9769 } |
9770 } | 9770 } |
9771 } | 9771 } |
9772 | 9772 |
9773 // The grammar allows for an optional ('.' identifier)? after the type, which | 9773 // The grammar allows for an optional ('.' identifier)? after the type, which |
9774 // is a named constructor. Note that ParseType() above will not consume it as | 9774 // is a named constructor. Note that ParseType() above will not consume it as |
9775 // part of a misinterpreted qualified identifier, because only a valid library | 9775 // part of a misinterpreted qualified identifier, because only a valid library |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9820 if (constructor.IsNull()) { | 9820 if (constructor.IsNull()) { |
9821 constructor = type_class.LookupFactory(constructor_name); | 9821 constructor = type_class.LookupFactory(constructor_name); |
9822 if (constructor.IsNull()) { | 9822 if (constructor.IsNull()) { |
9823 const String& external_constructor_name = | 9823 const String& external_constructor_name = |
9824 (named_constructor ? constructor_name : type_class_name); | 9824 (named_constructor ? constructor_name : type_class_name); |
9825 // Replace the type with a malformed type and compile a throw or report a | 9825 // Replace the type with a malformed type and compile a throw or report a |
9826 // compile-time error if the constructor is const. | 9826 // compile-time error if the constructor is const. |
9827 if (is_const) { | 9827 if (is_const) { |
9828 type = ClassFinalizer::NewFinalizedMalformedType( | 9828 type = ClassFinalizer::NewFinalizedMalformedType( |
9829 Error::Handle(), // No previous error. | 9829 Error::Handle(), // No previous error. |
9830 current_class(), | 9830 script_, |
9831 call_pos, | 9831 call_pos, |
9832 "class '%s' has no constructor or factory named '%s'", | 9832 "class '%s' has no constructor or factory named '%s'", |
9833 String::Handle(type_class.Name()).ToCString(), | 9833 String::Handle(type_class.Name()).ToCString(), |
9834 external_constructor_name.ToCString()); | 9834 external_constructor_name.ToCString()); |
9835 const Error& error = Error::Handle(type.malformed_error()); | 9835 const Error& error = Error::Handle(type.malformed_error()); |
9836 ErrorMsg(error); | 9836 ErrorMsg(error); |
9837 } | 9837 } |
9838 return ThrowNoSuchMethodError(call_pos, | 9838 return ThrowNoSuchMethodError(call_pos, |
9839 type_class, | 9839 type_class, |
9840 external_constructor_name, | 9840 external_constructor_name, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9946 Instance::ZoneHandle(const_instance.raw())); | 9946 Instance::ZoneHandle(const_instance.raw())); |
9947 if (!type_bound.IsNull()) { | 9947 if (!type_bound.IsNull()) { |
9948 ASSERT(!type_bound.IsMalformed()); | 9948 ASSERT(!type_bound.IsMalformed()); |
9949 Error& malformed_error = Error::Handle(); | 9949 Error& malformed_error = Error::Handle(); |
9950 ASSERT(!is_top_level_); // We cannot check unresolved types. | 9950 ASSERT(!is_top_level_); // We cannot check unresolved types. |
9951 if (!const_instance.IsInstanceOf(type_bound, | 9951 if (!const_instance.IsInstanceOf(type_bound, |
9952 TypeArguments::Handle(), | 9952 TypeArguments::Handle(), |
9953 &malformed_error)) { | 9953 &malformed_error)) { |
9954 type_bound = ClassFinalizer::NewFinalizedMalformedType( | 9954 type_bound = ClassFinalizer::NewFinalizedMalformedType( |
9955 malformed_error, | 9955 malformed_error, |
9956 current_class(), | 9956 script_, |
9957 new_pos, | 9957 new_pos, |
9958 "const factory result is not an instance of '%s'", | 9958 "const factory result is not an instance of '%s'", |
9959 String::Handle(type_bound.UserVisibleName()).ToCString()); | 9959 String::Handle(type_bound.UserVisibleName()).ToCString()); |
9960 new_object = ThrowTypeError(new_pos, type_bound); | 9960 new_object = ThrowTypeError(new_pos, type_bound); |
9961 } | 9961 } |
9962 type_bound = AbstractType::null(); | 9962 type_bound = AbstractType::null(); |
9963 } | 9963 } |
9964 } | 9964 } |
9965 } else { | 9965 } else { |
9966 CheckConstructorCallTypeArguments(new_pos, constructor, type_arguments); | 9966 CheckConstructorCallTypeArguments(new_pos, constructor, type_arguments); |
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
10609 void Parser::SkipQualIdent() { | 10609 void Parser::SkipQualIdent() { |
10610 ASSERT(IsIdentifier()); | 10610 ASSERT(IsIdentifier()); |
10611 ConsumeToken(); | 10611 ConsumeToken(); |
10612 if (CurrentToken() == Token::kPERIOD) { | 10612 if (CurrentToken() == Token::kPERIOD) { |
10613 ConsumeToken(); // Consume the kPERIOD token. | 10613 ConsumeToken(); // Consume the kPERIOD token. |
10614 ExpectIdentifier("identifier expected after '.'"); | 10614 ExpectIdentifier("identifier expected after '.'"); |
10615 } | 10615 } |
10616 } | 10616 } |
10617 | 10617 |
10618 } // namespace dart | 10618 } // namespace dart |
OLD | NEW |