| 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/code_generator.h" | 5 #include "vm/code_generator.h" |
| 6 | 6 |
| 7 #include "vm/code_index_table.h" | 7 #include "vm/code_index_table.h" |
| 8 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
| 9 #include "vm/compiler.h" | 9 #include "vm/compiler.h" |
| 10 #include "vm/dart_api_impl.h" | 10 #include "vm/dart_api_impl.h" |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 406 String::New(malformed_error.ToErrorCString())); | 406 String::New(malformed_error.ToErrorCString())); |
| 407 const String& no_name = String::Handle(String::NewSymbol("")); | 407 const String& no_name = String::Handle(String::NewSymbol("")); |
| 408 Exceptions::CreateAndThrowTypeError( | 408 Exceptions::CreateAndThrowTypeError( |
| 409 location, no_name, no_name, no_name, malformed_error_message); | 409 location, no_name, no_name, no_name, malformed_error_message); |
| 410 UNREACHABLE(); | 410 UNREACHABLE(); |
| 411 } | 411 } |
| 412 arguments.SetReturn(result); | 412 arguments.SetReturn(result); |
| 413 } | 413 } |
| 414 | 414 |
| 415 | 415 |
| 416 // For error reporting simplify type name, e.g, all integer types (Smi, Mint, |
| 417 // Bigint) a re reported as 'int'. |
| 418 static RawString* GetSimpleTypeName(const Instance& value) { |
| 419 if (value.IsInteger()) { |
| 420 return String::NewSymbol("int"); |
| 421 } else { |
| 422 return Type::Handle(value.GetType()).Name(); |
| 423 } |
| 424 } |
| 425 |
| 426 |
| 416 // Check that the type of the given instance is a subtype of the given type and | 427 // Check that the type of the given instance is a subtype of the given type and |
| 417 // can therefore be assigned. | 428 // can therefore be assigned. |
| 418 // Arg0: index of the token of the assignment (source location). | 429 // Arg0: index of the token of the assignment (source location). |
| 419 // Arg1: instance being assigned. | 430 // Arg1: instance being assigned. |
| 420 // Arg2: type being assigned to. | 431 // Arg2: type being assigned to. |
| 421 // Arg3: type arguments of the instantiator of the type being assigned to. | 432 // Arg3: type arguments of the instantiator of the type being assigned to. |
| 422 // Arg4: name of variable being assigned to. | 433 // Arg4: name of variable being assigned to. |
| 423 // Return value: instance if a subtype, otherwise throw a TypeError. | 434 // Return value: instance if a subtype, otherwise throw a TypeError. |
| 424 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) { | 435 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) { |
| 425 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count()); | 436 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count()); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 String::Handle(dst_type.Name()).ToCString()); | 470 String::Handle(dst_type.Name()).ToCString()); |
| 460 } | 471 } |
| 461 DartFrameIterator iterator; | 472 DartFrameIterator iterator; |
| 462 DartFrame* caller_frame = iterator.NextFrame(); | 473 DartFrame* caller_frame = iterator.NextFrame(); |
| 463 ASSERT(caller_frame != NULL); | 474 ASSERT(caller_frame != NULL); |
| 464 const Function& function = Function::Handle( | 475 const Function& function = Function::Handle( |
| 465 caller_frame->LookupDartFunction()); | 476 caller_frame->LookupDartFunction()); |
| 466 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); | 477 OS::Print(" -> Function %s\n", function.ToFullyQualifiedCString()); |
| 467 } | 478 } |
| 468 if (!is_instance_of) { | 479 if (!is_instance_of) { |
| 469 const Type& src_type = Type::Handle(src_instance.GetType()); | 480 String& src_type_name = String::Handle(GetSimpleTypeName(src_instance)); |
| 470 const String& src_type_name = String::Handle(src_type.Name()); | |
| 471 String& dst_type_name = String::Handle(); | 481 String& dst_type_name = String::Handle(); |
| 472 if (!dst_type.IsInstantiated()) { | 482 if (!dst_type.IsInstantiated()) { |
| 473 // Instantiate dst_type before reporting the error. | 483 // Instantiate dst_type before reporting the error. |
| 474 const AbstractType& instantiated_dst_type = AbstractType::Handle( | 484 const AbstractType& instantiated_dst_type = AbstractType::Handle( |
| 475 dst_type.InstantiateFrom(dst_type_instantiator)); | 485 dst_type.InstantiateFrom(dst_type_instantiator)); |
| 476 dst_type_name = instantiated_dst_type.Name(); | 486 dst_type_name = instantiated_dst_type.Name(); |
| 477 } else { | 487 } else { |
| 478 dst_type_name = dst_type.Name(); | 488 dst_type_name = dst_type.Name(); |
| 479 } | 489 } |
| 480 String& malformed_error_message = String::Handle(); | 490 String& malformed_error_message = String::Handle(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 495 // Arg1: bad object. | 505 // Arg1: bad object. |
| 496 // Return value: none, throws a TypeError. | 506 // Return value: none, throws a TypeError. |
| 497 DEFINE_RUNTIME_ENTRY(ConditionTypeError, 2) { | 507 DEFINE_RUNTIME_ENTRY(ConditionTypeError, 2) { |
| 498 ASSERT(arguments.Count() == | 508 ASSERT(arguments.Count() == |
| 499 kConditionTypeErrorRuntimeEntry.argument_count()); | 509 kConditionTypeErrorRuntimeEntry.argument_count()); |
| 500 // TODO(regis): Get the token index from the PcDesc (via DartFrame). | 510 // TODO(regis): Get the token index from the PcDesc (via DartFrame). |
| 501 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | 511 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); |
| 502 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); | 512 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); |
| 503 ASSERT(src_instance.IsNull() || !src_instance.IsBool()); | 513 ASSERT(src_instance.IsNull() || !src_instance.IsBool()); |
| 504 const Type& bool_interface = Type::Handle(Type::BoolInterface()); | 514 const Type& bool_interface = Type::Handle(Type::BoolInterface()); |
| 505 const Type& src_type = Type::Handle(src_instance.GetType()); | 515 const String& src_type_name = String::Handle(GetSimpleTypeName(src_instance)); |
| 506 const String& src_type_name = String::Handle(src_type.Name()); | |
| 507 const String& bool_type_name = String::Handle(bool_interface.Name()); | 516 const String& bool_type_name = String::Handle(bool_interface.Name()); |
| 508 const String& expr = String::Handle(String::NewSymbol("boolean expression")); | 517 const String& expr = String::Handle(String::NewSymbol("boolean expression")); |
| 509 const String& no_malformed_type_error = String::Handle(); | 518 const String& no_malformed_type_error = String::Handle(); |
| 510 Exceptions::CreateAndThrowTypeError(location, src_type_name, bool_type_name, | 519 Exceptions::CreateAndThrowTypeError(location, src_type_name, bool_type_name, |
| 511 expr, no_malformed_type_error); | 520 expr, no_malformed_type_error); |
| 512 UNREACHABLE(); | 521 UNREACHABLE(); |
| 513 } | 522 } |
| 514 | 523 |
| 515 | 524 |
| 516 // Report that the type of the type check is malformed. | 525 // Report that the type of the type check is malformed. |
| 517 // Arg0: index of the token of the failed type check. | 526 // Arg0: index of the token of the failed type check. |
| 518 // Arg1: src value. | 527 // Arg1: src value. |
| 519 // Arg2: name of instance being assigned to. | 528 // Arg2: name of instance being assigned to. |
| 520 // Arg3: malformed type error message. | 529 // Arg3: malformed type error message. |
| 521 // Return value: none, throws an exception. | 530 // Return value: none, throws an exception. |
| 522 DEFINE_RUNTIME_ENTRY(MalformedTypeError, 4) { | 531 DEFINE_RUNTIME_ENTRY(MalformedTypeError, 4) { |
| 523 ASSERT(arguments.Count() == | 532 ASSERT(arguments.Count() == |
| 524 kMalformedTypeErrorRuntimeEntry.argument_count()); | 533 kMalformedTypeErrorRuntimeEntry.argument_count()); |
| 525 // TODO(regis): Get the token index from the PcDesc (via DartFrame). | 534 // TODO(regis): Get the token index from the PcDesc (via DartFrame). |
| 526 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | 535 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); |
| 527 const Instance& src_value = Instance::CheckedHandle(arguments.At(1)); | 536 const Instance& src_value = Instance::CheckedHandle(arguments.At(1)); |
| 528 const String& dst_name = String::CheckedHandle(arguments.At(2)); | 537 const String& dst_name = String::CheckedHandle(arguments.At(2)); |
| 529 const String& malformed_error = String::CheckedHandle(arguments.At(3)); | 538 const String& malformed_error = String::CheckedHandle(arguments.At(3)); |
| 530 const String& dst_type_name = String::Handle(String::NewSymbol("malformed")); | 539 const String& dst_type_name = String::Handle(String::NewSymbol("malformed")); |
| 531 const String& src_type_name = | 540 const String& src_type_name = String::Handle(GetSimpleTypeName(src_value)); |
| 532 String::Handle(Type::Handle(src_value.GetType()).Name()); | |
| 533 Exceptions::CreateAndThrowTypeError(location, src_type_name, | 541 Exceptions::CreateAndThrowTypeError(location, src_type_name, |
| 534 dst_type_name, dst_name, malformed_error); | 542 dst_type_name, dst_name, malformed_error); |
| 535 UNREACHABLE(); | 543 UNREACHABLE(); |
| 536 } | 544 } |
| 537 | 545 |
| 538 | 546 |
| 539 // TODO(regis): Function rest arguments are not supported anymore, but they may | 547 // TODO(regis): Function rest arguments are not supported anymore, but they may |
| 540 // come back. | 548 // come back. |
| 541 // Check that the type of each element of the given array is assignable to the | 549 // Check that the type of each element of the given array is assignable to the |
| 542 // given type. | 550 // given type. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 568 // The previous successful type check may have set malformed_error. | 576 // The previous successful type check may have set malformed_error. |
| 569 // Note that a returned malformed_error is ignored if a type check succeeds. | 577 // Note that a returned malformed_error is ignored if a type check succeeds. |
| 570 malformed_error = Error::null(); | 578 malformed_error = Error::null(); |
| 571 if (!elem.IsNull() && !elem.IsInstanceOf(element_type, | 579 if (!elem.IsNull() && !elem.IsInstanceOf(element_type, |
| 572 element_type_instantiator, | 580 element_type_instantiator, |
| 573 &malformed_error)) { | 581 &malformed_error)) { |
| 574 // Allocate and throw a new instance of TypeError. | 582 // Allocate and throw a new instance of TypeError. |
| 575 char buf[256]; | 583 char buf[256]; |
| 576 OS::SNPrint(buf, sizeof(buf), "%s[%d]", | 584 OS::SNPrint(buf, sizeof(buf), "%s[%d]", |
| 577 rest_name.ToCString(), static_cast<int>(i)); | 585 rest_name.ToCString(), static_cast<int>(i)); |
| 578 const String& src_type_name = | 586 const String& src_type_name = String::Handle(GetSimpleTypeName(elem)); |
| 579 String::Handle(Type::Handle(elem.GetType()).Name()); | |
| 580 String& dst_type_name = String::Handle(); | 587 String& dst_type_name = String::Handle(); |
| 581 if (!element_type.IsInstantiated()) { | 588 if (!element_type.IsInstantiated()) { |
| 582 // Instantiate element_type before reporting the error. | 589 // Instantiate element_type before reporting the error. |
| 583 const AbstractType& instantiated_element_type = AbstractType::Handle( | 590 const AbstractType& instantiated_element_type = AbstractType::Handle( |
| 584 element_type.InstantiateFrom(element_type_instantiator)); | 591 element_type.InstantiateFrom(element_type_instantiator)); |
| 585 dst_type_name = instantiated_element_type.Name(); | 592 dst_type_name = instantiated_element_type.Name(); |
| 586 } else { | 593 } else { |
| 587 dst_type_name = element_type.Name(); | 594 dst_type_name = element_type.Name(); |
| 588 } | 595 } |
| 589 const String& dst_name = String::Handle(String::New(buf)); | 596 const String& dst_name = String::Handle(String::New(buf)); |
| (...skipping 849 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1439 } | 1446 } |
| 1440 } | 1447 } |
| 1441 } | 1448 } |
| 1442 // The cache is null terminated, therefore the loop above should never | 1449 // The cache is null terminated, therefore the loop above should never |
| 1443 // terminate by itself. | 1450 // terminate by itself. |
| 1444 UNREACHABLE(); | 1451 UNREACHABLE(); |
| 1445 return Code::null(); | 1452 return Code::null(); |
| 1446 } | 1453 } |
| 1447 | 1454 |
| 1448 } // namespace dart | 1455 } // namespace dart |
| OLD | NEW |