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

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

Issue 23465004: More cleanup related to malformed and malbounded types. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 3 months 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
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/raw_object_snapshot.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 10 matching lines...) Expand all
21 #include "vm/resolver.h" 21 #include "vm/resolver.h"
22 #include "vm/scopes.h" 22 #include "vm/scopes.h"
23 #include "vm/stack_frame.h" 23 #include "vm/stack_frame.h"
24 #include "vm/symbols.h" 24 #include "vm/symbols.h"
25 25
26 namespace dart { 26 namespace dart {
27 27
28 DEFINE_FLAG(bool, enable_asserts, false, "Enable assert statements."); 28 DEFINE_FLAG(bool, enable_asserts, false, "Enable assert statements.");
29 DEFINE_FLAG(bool, enable_type_checks, false, "Enable type checks."); 29 DEFINE_FLAG(bool, enable_type_checks, false, "Enable type checks.");
30 DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations."); 30 DEFINE_FLAG(bool, trace_parser, false, "Trace parser operations.");
31 DEFINE_FLAG(bool, warning_as_error, false, "Treat warnings as errors."); 31 DECLARE_FLAG(bool, error_on_bad_type);
32 DEFINE_FLAG(bool, silent_warnings, false, "Silence warnings.");
33 DECLARE_FLAG(bool, error_on_malformed_type);
34 DECLARE_FLAG(bool, throw_on_javascript_int_overflow); 32 DECLARE_FLAG(bool, throw_on_javascript_int_overflow);
35 33
36 static void CheckedModeHandler(bool value) { 34 static void CheckedModeHandler(bool value) {
37 FLAG_enable_asserts = value; 35 FLAG_enable_asserts = value;
38 FLAG_enable_type_checks = value; 36 FLAG_enable_type_checks = value;
39 } 37 }
40 38
41 // --enable-checked-mode and --checked both enable checked mode which is 39 // --enable-checked-mode and --checked both enable checked mode which is
42 // equivalent to setting --enable-asserts and --enable-type-checks. 40 // equivalent to setting --enable-asserts and --enable-type-checks.
43 DEFINE_FLAG_HANDLER(CheckedModeHandler, 41 DEFINE_FLAG_HANDLER(CheckedModeHandler,
(...skipping 6961 matching lines...) Expand 10 before | Expand all | Expand 10 after
7005 va_list args; 7003 va_list args;
7006 va_start(args, format); 7004 va_start(args, format);
7007 const Error& error = Error::Handle(FormatErrorWithAppend( 7005 const Error& error = Error::Handle(FormatErrorWithAppend(
7008 prev_error, script_, token_pos, "Error", format, args)); 7006 prev_error, script_, token_pos, "Error", format, args));
7009 va_end(args); 7007 va_end(args);
7010 isolate()->long_jump_base()->Jump(1, error); 7008 isolate()->long_jump_base()->Jump(1, error);
7011 UNREACHABLE(); 7009 UNREACHABLE();
7012 } 7010 }
7013 7011
7014 7012
7015 void Parser::Warning(intptr_t token_pos, const char* format, ...) {
hausner 2013/08/26 22:17:33 Can you please leave Warning() and friends in the
regis 2013/08/26 23:23:38 Done.
7016 if (FLAG_silent_warnings) return;
7017 va_list args;
7018 va_start(args, format);
7019 const Error& error = Error::Handle(
7020 FormatError(script_, token_pos, "Warning", format, args));
7021 va_end(args);
7022 if (FLAG_warning_as_error) {
7023 isolate()->long_jump_base()->Jump(1, error);
7024 UNREACHABLE();
7025 } else {
7026 OS::Print("%s", error.ToErrorCString());
7027 }
7028 }
7029
7030
7031 void Parser::Warning(const char* format, ...) {
7032 if (FLAG_silent_warnings) return;
7033 va_list args;
7034 va_start(args, format);
7035 const Error& error = Error::Handle(
7036 FormatError(script_, TokenPos(), "Warning", format, args));
7037 va_end(args);
7038 if (FLAG_warning_as_error) {
7039 isolate()->long_jump_base()->Jump(1, error);
7040 UNREACHABLE();
7041 } else {
7042 OS::Print("%s", error.ToErrorCString());
7043 }
7044 }
7045
7046
7047 void Parser::Unimplemented(const char* msg) { 7013 void Parser::Unimplemented(const char* msg) {
7048 ErrorMsg(TokenPos(), "%s", msg); 7014 ErrorMsg(TokenPos(), "%s", msg);
7049 } 7015 }
7050 7016
7051 7017
7052 void Parser::ExpectToken(Token::Kind token_expected) { 7018 void Parser::ExpectToken(Token::Kind token_expected) {
7053 if (CurrentToken() != token_expected) { 7019 if (CurrentToken() != token_expected) {
7054 ErrorMsg("'%s' expected", Token::Str(token_expected)); 7020 ErrorMsg("'%s' expected", Token::Str(token_expected));
7055 } 7021 }
7056 ConsumeToken(); 7022 ConsumeToken();
(...skipping 1219 matching lines...) Expand 10 before | Expand all | Expand 10 after
8276 if (!scope_class.IsNull()) { 8242 if (!scope_class.IsNull()) {
8277 // First check if the type is a type parameter of the given scope class. 8243 // First check if the type is a type parameter of the given scope class.
8278 const TypeParameter& type_parameter = TypeParameter::Handle( 8244 const TypeParameter& type_parameter = TypeParameter::Handle(
8279 scope_class.LookupTypeParameter(unresolved_class_name)); 8245 scope_class.LookupTypeParameter(unresolved_class_name));
8280 if (!type_parameter.IsNull()) { 8246 if (!type_parameter.IsNull()) {
8281 // A type parameter is considered to be a malformed type when 8247 // A type parameter is considered to be a malformed type when
8282 // referenced by a static member. 8248 // referenced by a static member.
8283 if (ParsingStaticMember()) { 8249 if (ParsingStaticMember()) {
8284 ASSERT(scope_class.raw() == current_class().raw()); 8250 ASSERT(scope_class.raw() == current_class().raw());
8285 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || 8251 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) ||
8286 FLAG_error_on_malformed_type) { 8252 FLAG_error_on_bad_type) {
8287 *type = ClassFinalizer::NewFinalizedMalformedType( 8253 *type = ClassFinalizer::NewFinalizedMalformedType(
8288 Error::Handle(), // No previous error. 8254 Error::Handle(), // No previous error.
8289 scope_class, 8255 scope_class,
8290 type->token_pos(), 8256 type->token_pos(),
8291 "type parameter '%s' cannot be referenced " 8257 "type parameter '%s' cannot be referenced "
8292 "from static member", 8258 "from static member",
8293 String::Handle(type_parameter.name()).ToCString()); 8259 String::Handle(type_parameter.name()).ToCString());
8294 } else { 8260 } else {
8295 // Map the malformed type to dynamic and ignore type arguments. 8261 // Map the malformed type to dynamic and ignore type arguments.
8296 *type = Type::DynamicType(); 8262 *type = Type::DynamicType();
8297 } 8263 }
8298 return; 8264 return;
8299 } 8265 }
8300 // A type parameter cannot be parameterized, so make the type 8266 // A type parameter cannot be parameterized, so make the type
8301 // malformed if type arguments have previously been parsed. 8267 // malformed if type arguments have previously been parsed.
8302 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) { 8268 if (!AbstractTypeArguments::Handle(type->arguments()).IsNull()) {
8303 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || 8269 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) ||
8304 FLAG_error_on_malformed_type) { 8270 FLAG_error_on_bad_type) {
8305 *type = ClassFinalizer::NewFinalizedMalformedType( 8271 *type = ClassFinalizer::NewFinalizedMalformedType(
8306 Error::Handle(), // No previous error. 8272 Error::Handle(), // No previous error.
8307 scope_class, 8273 scope_class,
8308 type_parameter.token_pos(), 8274 type_parameter.token_pos(),
8309 "type parameter '%s' cannot be parameterized", 8275 "type parameter '%s' cannot be parameterized",
8310 String::Handle(type_parameter.name()).ToCString()); 8276 String::Handle(type_parameter.name()).ToCString());
8311 } else { 8277 } else {
8312 // Map the malformed type to dynamic and ignore type arguments. 8278 // Map the malformed type to dynamic and ignore type arguments.
8313 *type = Type::DynamicType(); 8279 *type = Type::DynamicType();
8314 } 8280 }
8315 return; 8281 return;
8316 } 8282 }
8317 *type = type_parameter.raw(); 8283 *type = type_parameter.raw();
8318 return; 8284 return;
8319 } 8285 }
8320 } 8286 }
8321 // The referenced class may not have been parsed yet. It would be wrong 8287 // The referenced class may not have been parsed yet. It would be wrong
8322 // to resolve it too early to an imported class of the same name. 8288 // to resolve it too early to an imported class of the same name.
8323 if (finalization > ClassFinalizer::kResolveTypeParameters) { 8289 if (finalization > ClassFinalizer::kResolveTypeParameters) {
8324 // Resolve classname in the scope of the current library. 8290 // Resolve classname in the scope of the current library.
8325 Error& error = Error::Handle(); 8291 Error& error = Error::Handle();
8326 resolved_type_class = ResolveClassInCurrentLibraryScope( 8292 resolved_type_class = ResolveClassInCurrentLibraryScope(
8327 unresolved_class.token_pos(), 8293 unresolved_class.token_pos(),
8328 unresolved_class_name, 8294 unresolved_class_name,
8329 &error); 8295 &error);
8330 if (!error.IsNull()) { 8296 if (!error.IsNull()) {
8331 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || 8297 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) ||
8332 FLAG_error_on_malformed_type) { 8298 FLAG_error_on_bad_type) {
8333 *type = ClassFinalizer::NewFinalizedMalformedType( 8299 *type = ClassFinalizer::NewFinalizedMalformedType(
8334 error, 8300 error,
8335 scope_class, 8301 scope_class,
8336 unresolved_class.token_pos(), 8302 unresolved_class.token_pos(),
8337 "cannot resolve class '%s'", 8303 "cannot resolve class '%s'",
8338 unresolved_class_name.ToCString()); 8304 unresolved_class_name.ToCString());
8339 } else { 8305 } else {
8340 // Map the malformed type to dynamic and ignore type arguments. 8306 // Map the malformed type to dynamic and ignore type arguments.
8341 *type = Type::DynamicType(); 8307 *type = Type::DynamicType();
8342 } 8308 }
8343 return; 8309 return;
8344 } 8310 }
8345 } 8311 }
8346 } else { 8312 } else {
8347 LibraryPrefix& lib_prefix = 8313 LibraryPrefix& lib_prefix =
8348 LibraryPrefix::Handle(unresolved_class.library_prefix()); 8314 LibraryPrefix::Handle(unresolved_class.library_prefix());
8349 // Resolve class name in the scope of the library prefix. 8315 // Resolve class name in the scope of the library prefix.
8350 Error& error = Error::Handle(); 8316 Error& error = Error::Handle();
8351 resolved_type_class = ResolveClassInPrefixScope( 8317 resolved_type_class = ResolveClassInPrefixScope(
8352 unresolved_class.token_pos(), 8318 unresolved_class.token_pos(),
8353 lib_prefix, 8319 lib_prefix,
8354 unresolved_class_name, 8320 unresolved_class_name,
8355 &error); 8321 &error);
8356 if (!error.IsNull()) { 8322 if (!error.IsNull()) {
8357 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || 8323 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) ||
8358 FLAG_error_on_malformed_type) { 8324 FLAG_error_on_bad_type) {
8359 *type = ClassFinalizer::NewFinalizedMalformedType( 8325 *type = ClassFinalizer::NewFinalizedMalformedType(
8360 error, 8326 error,
8361 scope_class, 8327 scope_class,
8362 unresolved_class.token_pos(), 8328 unresolved_class.token_pos(),
8363 "cannot resolve class '%s'", 8329 "cannot resolve class '%s'",
8364 unresolved_class_name.ToCString()); 8330 unresolved_class_name.ToCString());
8365 } else { 8331 } else {
8366 // Map the malformed type to dynamic and ignore type arguments. 8332 // Map the malformed type to dynamic and ignore type arguments.
8367 *type = Type::DynamicType(); 8333 *type = Type::DynamicType();
8368 } 8334 }
8369 return; 8335 return;
8370 } 8336 }
8371 } 8337 }
8372 // At this point, we can only have a parameterized_type. 8338 // At this point, we can only have a parameterized_type.
8373 const Type& parameterized_type = Type::Cast(*type); 8339 const Type& parameterized_type = Type::Cast(*type);
8374 if (!resolved_type_class.IsNull()) { 8340 if (!resolved_type_class.IsNull()) {
8375 // Replace unresolved class with resolved type class. 8341 // Replace unresolved class with resolved type class.
8376 parameterized_type.set_type_class(resolved_type_class); 8342 parameterized_type.set_type_class(resolved_type_class);
8377 } else if (finalization >= ClassFinalizer::kCanonicalize) { 8343 } else if (finalization >= ClassFinalizer::kCanonicalize) {
8378 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) || 8344 if ((finalization == ClassFinalizer::kCanonicalizeWellFormed) ||
8379 FLAG_error_on_malformed_type) { 8345 FLAG_error_on_bad_type) {
8380 ClassFinalizer::FinalizeMalformedType( 8346 ClassFinalizer::FinalizeMalformedType(
8381 Error::Handle(), // No previous error. 8347 Error::Handle(), // No previous error.
8382 scope_class, 8348 scope_class,
8383 parameterized_type, 8349 parameterized_type,
8384 "type '%s' is not loaded", 8350 "type '%s' is not loaded",
8385 String::Handle(parameterized_type.UserVisibleName()).ToCString()); 8351 String::Handle(parameterized_type.UserVisibleName()).ToCString());
8386 } else { 8352 } else {
8387 // Map the malformed type to dynamic and ignore type arguments. 8353 // Map the malformed type to dynamic and ignore type arguments.
8388 *type = Type::DynamicType(); 8354 *type = Type::DynamicType();
8389 } 8355 }
(...skipping 780 matching lines...) Expand 10 before | Expand all | Expand 10 after
9170 AbstractTypeArguments& list_type_arguments = 9136 AbstractTypeArguments& list_type_arguments =
9171 AbstractTypeArguments::ZoneHandle(type_arguments.raw()); 9137 AbstractTypeArguments::ZoneHandle(type_arguments.raw());
9172 // If no type argument vector is provided, leave it as null, which is 9138 // If no type argument vector is provided, leave it as null, which is
9173 // equivalent to using dynamic as the type argument for the element type. 9139 // equivalent to using dynamic as the type argument for the element type.
9174 if (!list_type_arguments.IsNull()) { 9140 if (!list_type_arguments.IsNull()) {
9175 ASSERT(list_type_arguments.Length() > 0); 9141 ASSERT(list_type_arguments.Length() > 0);
9176 // List literals take a single type argument. 9142 // List literals take a single type argument.
9177 if (list_type_arguments.Length() == 1) { 9143 if (list_type_arguments.Length() == 1) {
9178 element_type = list_type_arguments.TypeAt(0); 9144 element_type = list_type_arguments.TypeAt(0);
9179 } else { 9145 } else {
9180 if (FLAG_error_on_malformed_type) { 9146 if (FLAG_error_on_bad_type) {
9181 ErrorMsg(type_pos, 9147 ErrorMsg(type_pos,
9182 "a list literal takes one type argument specifying " 9148 "a list literal takes one type argument specifying "
9183 "the element type"); 9149 "the element type");
9184 } 9150 }
9185 // Ignore type arguments. 9151 // Ignore type arguments.
9186 list_type_arguments = AbstractTypeArguments::null(); 9152 list_type_arguments = AbstractTypeArguments::null();
9187 } 9153 }
9188 if (is_const && !element_type.IsInstantiated()) { 9154 if (is_const && !element_type.IsInstantiated()) {
9189 ErrorMsg(type_pos, 9155 ErrorMsg(type_pos,
9190 "the type argument of a constant list literal cannot include " 9156 "the type argument of a constant list literal cannot include "
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
9368 // Map literals take two type arguments. 9334 // Map literals take two type arguments.
9369 if (map_type_arguments.Length() == 2) { 9335 if (map_type_arguments.Length() == 2) {
9370 key_type = map_type_arguments.TypeAt(0); 9336 key_type = map_type_arguments.TypeAt(0);
9371 value_type = map_type_arguments.TypeAt(1); 9337 value_type = map_type_arguments.TypeAt(1);
9372 if (is_const && !type_arguments.IsInstantiated()) { 9338 if (is_const && !type_arguments.IsInstantiated()) {
9373 ErrorMsg(type_pos, 9339 ErrorMsg(type_pos,
9374 "the type arguments of a constant map literal cannot include " 9340 "the type arguments of a constant map literal cannot include "
9375 "a type variable"); 9341 "a type variable");
9376 } 9342 }
9377 if (key_type.IsMalformed()) { 9343 if (key_type.IsMalformed()) {
9378 if (FLAG_error_on_malformed_type) { 9344 if (FLAG_error_on_bad_type) {
9379 ErrorMsg(Error::Handle(key_type.malformed_error())); 9345 ErrorMsg(Error::Handle(key_type.malformed_error()));
9380 } 9346 }
9381 // Map malformed key type to dynamic. 9347 // Map malformed key type to dynamic.
9382 key_type = Type::DynamicType(); 9348 key_type = Type::DynamicType();
9383 map_type_arguments.SetTypeAt(0, key_type); 9349 map_type_arguments.SetTypeAt(0, key_type);
9384 } 9350 }
9385 if (value_type.IsMalformed()) { 9351 if (value_type.IsMalformed()) {
9386 if (FLAG_error_on_malformed_type) { 9352 if (FLAG_error_on_bad_type) {
9387 ErrorMsg(Error::Handle(value_type.malformed_error())); 9353 ErrorMsg(Error::Handle(value_type.malformed_error()));
9388 } 9354 }
9389 // Map malformed value type to dynamic. 9355 // Map malformed value type to dynamic.
9390 value_type = Type::DynamicType(); 9356 value_type = Type::DynamicType();
9391 map_type_arguments.SetTypeAt(1, value_type); 9357 map_type_arguments.SetTypeAt(1, value_type);
9392 } 9358 }
9393 } else { 9359 } else {
9394 if (FLAG_error_on_malformed_type) { 9360 if (FLAG_error_on_bad_type) {
9395 ErrorMsg(type_pos, 9361 ErrorMsg(type_pos,
9396 "a map literal takes two type arguments specifying " 9362 "a map literal takes two type arguments specifying "
9397 "the key type and the value type"); 9363 "the key type and the value type");
9398 } 9364 }
9399 // Ignore type arguments. 9365 // Ignore type arguments.
9400 map_type_arguments = AbstractTypeArguments::null(); 9366 map_type_arguments = AbstractTypeArguments::null();
9401 } 9367 }
9402 } 9368 }
9403 ASSERT((map_type_arguments.IsNull() && 9369 ASSERT((map_type_arguments.IsNull() &&
9404 key_type.IsDynamicType() && value_type.IsDynamicType()) || 9370 key_type.IsDynamicType() && value_type.IsDynamicType()) ||
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after
9618 if (type.IsTypeParameter() || type.IsDynamicType()) { 9584 if (type.IsTypeParameter() || type.IsDynamicType()) {
9619 // Replace the type with a malformed type. 9585 // Replace the type with a malformed type.
9620 type = ClassFinalizer::NewFinalizedMalformedType( 9586 type = ClassFinalizer::NewFinalizedMalformedType(
9621 Error::Handle(), // No previous error. 9587 Error::Handle(), // No previous error.
9622 current_class(), 9588 current_class(),
9623 type_pos, 9589 type_pos,
9624 "%s'%s' cannot be instantiated", 9590 "%s'%s' cannot be instantiated",
9625 type.IsTypeParameter() ? "type parameter " : "", 9591 type.IsTypeParameter() ? "type parameter " : "",
9626 type.IsTypeParameter() ? 9592 type.IsTypeParameter() ?
9627 String::Handle(type.UserVisibleName()).ToCString() : "dynamic"); 9593 String::Handle(type.UserVisibleName()).ToCString() : "dynamic");
9628 } else if (FLAG_enable_type_checks || FLAG_error_on_malformed_type) { 9594 } else if (FLAG_enable_type_checks || FLAG_error_on_bad_type) {
9629 Error& bound_error = Error::Handle(); 9595 Error& bound_error = Error::Handle();
9630 if (type.IsMalboundedWithError(&bound_error)) { 9596 if (type.IsMalboundedWithError(&bound_error)) {
9631 // Replace the type with a malformed type. 9597 // Replace the type with a malformed type.
9632 type = ClassFinalizer::NewFinalizedMalformedType( 9598 type = ClassFinalizer::NewFinalizedMalformedType(
9633 bound_error, 9599 bound_error,
9634 current_class(), 9600 current_class(),
9635 type_pos, 9601 type_pos,
9636 "malbounded type '%s' cannot be instantiated", 9602 "malbounded type '%s' cannot be instantiated",
9637 String::Handle(type.UserVisibleName()).ToCString()); 9603 String::Handle(type.UserVisibleName()).ToCString());
9638 } 9604 }
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
10420 void Parser::SkipQualIdent() { 10386 void Parser::SkipQualIdent() {
10421 ASSERT(IsIdentifier()); 10387 ASSERT(IsIdentifier());
10422 ConsumeToken(); 10388 ConsumeToken();
10423 if (CurrentToken() == Token::kPERIOD) { 10389 if (CurrentToken() == Token::kPERIOD) {
10424 ConsumeToken(); // Consume the kPERIOD token. 10390 ConsumeToken(); // Consume the kPERIOD token.
10425 ExpectIdentifier("identifier expected after '.'"); 10391 ExpectIdentifier("identifier expected after '.'");
10426 } 10392 }
10427 } 10393 }
10428 10394
10429 } // namespace dart 10395 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | runtime/vm/raw_object_snapshot.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698