| 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 "lib/error.h" | 5 #include "lib/error.h" |
| 6 | 6 |
| 7 #include "vm/bootstrap_natives.h" | 7 #include "vm/bootstrap_natives.h" |
| 8 #include "vm/exceptions.h" | 8 #include "vm/exceptions.h" |
| 9 #include "vm/object_store.h" | 9 #include "vm/object_store.h" |
| 10 #include "vm/runtime_entry.h" | 10 #include "vm/runtime_entry.h" |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 // Arg0: index of the token of the assignment (source location). | 207 // Arg0: index of the token of the assignment (source location). |
| 208 // Arg1: instance being assigned. | 208 // Arg1: instance being assigned. |
| 209 // Arg2: type being assigned to. | 209 // Arg2: type being assigned to. |
| 210 // Arg3: type arguments of the instantiator of the type being assigned to. | 210 // Arg3: type arguments of the instantiator of the type being assigned to. |
| 211 // Arg4: name of instance being assigned to. | 211 // Arg4: name of instance being assigned to. |
| 212 // Return value: instance if assignable, otherwise throw a TypeError. | 212 // Return value: instance if assignable, otherwise throw a TypeError. |
| 213 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) { | 213 DEFINE_RUNTIME_ENTRY(TypeCheck, 5) { |
| 214 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count()); | 214 ASSERT(arguments.Count() == kTypeCheckRuntimeEntry.argument_count()); |
| 215 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | 215 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); |
| 216 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); | 216 const Instance& src_instance = Instance::CheckedHandle(arguments.At(1)); |
| 217 const Type& dst_type = Type::CheckedHandle(arguments.At(2)); | 217 const AbstractType& dst_type = AbstractType::CheckedHandle(arguments.At(2)); |
| 218 const TypeArguments& dst_type_instantiator = | 218 const TypeArguments& dst_type_instantiator = |
| 219 TypeArguments::CheckedHandle(arguments.At(3)); | 219 TypeArguments::CheckedHandle(arguments.At(3)); |
| 220 const String& dst_name = String::CheckedHandle(arguments.At(4)); | 220 const String& dst_name = String::CheckedHandle(arguments.At(4)); |
| 221 ASSERT(!dst_type.IsDynamicType()); // No need to check assignment. | 221 ASSERT(!dst_type.IsDynamicType()); // No need to check assignment. |
| 222 ASSERT(!src_instance.IsNull()); // Already checked in inlined code. | 222 ASSERT(!src_instance.IsNull()); // Already checked in inlined code. |
| 223 | 223 |
| 224 const bool is_assignable = | 224 const bool is_assignable = |
| 225 src_instance.IsAssignableTo(dst_type, dst_type_instantiator); | 225 src_instance.IsAssignableTo(dst_type, dst_type_instantiator); |
| 226 | 226 |
| 227 if (FLAG_trace_type_checks) { | 227 if (FLAG_trace_type_checks) { |
| 228 const Type& src_type = Type::Handle(src_instance.GetType()); | 228 const Type& src_type = Type::Handle(src_instance.GetType()); |
| 229 Type& instantiated_dst_type = Type::Handle(dst_type.raw()); | 229 AbstractType& instantiated_dst_type = AbstractType::Handle(dst_type.raw()); |
| 230 if (!dst_type.IsInstantiated()) { | 230 if (!dst_type.IsInstantiated()) { |
| 231 // Instantiate dst_type before printing. | 231 // Instantiate dst_type before printing. |
| 232 instantiated_dst_type = | 232 instantiated_dst_type = |
| 233 dst_type.InstantiateFrom(dst_type_instantiator, 0); | 233 dst_type.InstantiateFrom(dst_type_instantiator, 0); |
| 234 } | 234 } |
| 235 OS::Print("TypeCheck: '%s' %s assignable to '%s'.\n", | 235 OS::Print("TypeCheck: '%s' %s assignable to '%s'.\n", |
| 236 String::Handle(src_type.Name()).ToCString(), | 236 String::Handle(src_type.Name()).ToCString(), |
| 237 is_assignable ? "is" : "is not", | 237 is_assignable ? "is" : "is not", |
| 238 String::Handle(dst_type.Name()).ToCString()); | 238 String::Handle(dst_type.Name()).ToCString()); |
| 239 } | 239 } |
| 240 if (!is_assignable) { | 240 if (!is_assignable) { |
| 241 const Type& src_type = Type::Handle(src_instance.GetType()); | 241 const Type& src_type = Type::Handle(src_instance.GetType()); |
| 242 const String& src_type_name = String::Handle(src_type.Name()); | 242 const String& src_type_name = String::Handle(src_type.Name()); |
| 243 String& dst_type_name = String::Handle(); | 243 String& dst_type_name = String::Handle(); |
| 244 if (!dst_type.IsInstantiated()) { | 244 if (!dst_type.IsInstantiated()) { |
| 245 // Instantiate dst_type before reporting the error. | 245 // Instantiate dst_type before reporting the error. |
| 246 const Type& instantiated_dst_type = Type::Handle( | 246 const AbstractType& instantiated_dst_type = AbstractType::Handle( |
| 247 dst_type.InstantiateFrom(dst_type_instantiator, 0)); | 247 dst_type.InstantiateFrom(dst_type_instantiator, 0)); |
| 248 dst_type_name = instantiated_dst_type.Name(); | 248 dst_type_name = instantiated_dst_type.Name(); |
| 249 } else { | 249 } else { |
| 250 dst_type_name = dst_type.Name(); | 250 dst_type_name = dst_type.Name(); |
| 251 } | 251 } |
| 252 ThrowTypeError(location, src_type_name, dst_type_name, dst_name); | 252 ThrowTypeError(location, src_type_name, dst_type_name, dst_name); |
| 253 UNREACHABLE(); | 253 UNREACHABLE(); |
| 254 } | 254 } |
| 255 arguments.SetReturn(src_instance); | 255 arguments.SetReturn(src_instance); |
| 256 } | 256 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 283 // Arg1: rest argument array. | 283 // Arg1: rest argument array. |
| 284 // Arg2: element declaration type. | 284 // Arg2: element declaration type. |
| 285 // Arg3: type arguments of the instantiator of the element declaration type. | 285 // Arg3: type arguments of the instantiator of the element declaration type. |
| 286 // Arg4: name of object being assigned to, i.e. name of rest argument. | 286 // Arg4: name of object being assigned to, i.e. name of rest argument. |
| 287 // Return value: null if assignable, otherwise allocate and throw a TypeError. | 287 // Return value: null if assignable, otherwise allocate and throw a TypeError. |
| 288 DEFINE_RUNTIME_ENTRY(RestArgumentTypeCheck, 5) { | 288 DEFINE_RUNTIME_ENTRY(RestArgumentTypeCheck, 5) { |
| 289 ASSERT(arguments.Count() == | 289 ASSERT(arguments.Count() == |
| 290 kRestArgumentTypeCheckRuntimeEntry.argument_count()); | 290 kRestArgumentTypeCheckRuntimeEntry.argument_count()); |
| 291 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); | 291 intptr_t location = Smi::CheckedHandle(arguments.At(0)).Value(); |
| 292 const Array& rest_array = Array::CheckedHandle(arguments.At(1)); | 292 const Array& rest_array = Array::CheckedHandle(arguments.At(1)); |
| 293 const Type& element_type = Type::CheckedHandle(arguments.At(2)); | 293 const AbstractType& element_type = |
| 294 AbstractType::CheckedHandle(arguments.At(2)); |
| 294 const TypeArguments& element_type_instantiator = | 295 const TypeArguments& element_type_instantiator = |
| 295 TypeArguments::CheckedHandle(arguments.At(3)); | 296 TypeArguments::CheckedHandle(arguments.At(3)); |
| 296 const String& rest_name = String::CheckedHandle(arguments.At(4)); | 297 const String& rest_name = String::CheckedHandle(arguments.At(4)); |
| 297 ASSERT(!element_type.IsDynamicType()); // No need to check assignment. | 298 ASSERT(!element_type.IsDynamicType()); // No need to check assignment. |
| 298 ASSERT(!rest_array.IsNull()); | 299 ASSERT(!rest_array.IsNull()); |
| 299 | 300 |
| 300 Instance& elem = Instance::Handle(); | 301 Instance& elem = Instance::Handle(); |
| 301 for (intptr_t i = 0; i < rest_array.Length(); i++) { | 302 for (intptr_t i = 0; i < rest_array.Length(); i++) { |
| 302 elem ^= rest_array.At(i); | 303 elem ^= rest_array.At(i); |
| 303 if (!elem.IsNull() && | 304 if (!elem.IsNull() && |
| 304 !elem.IsAssignableTo(element_type, element_type_instantiator)) { | 305 !elem.IsAssignableTo(element_type, element_type_instantiator)) { |
| 305 // Allocate and throw a new instance of TypeError. | 306 // Allocate and throw a new instance of TypeError. |
| 306 char buf[256]; | 307 char buf[256]; |
| 307 OS::SNPrint(buf, sizeof(buf), "%s[%d]", | 308 OS::SNPrint(buf, sizeof(buf), "%s[%d]", |
| 308 rest_name.ToCString(), static_cast<int>(i)); | 309 rest_name.ToCString(), static_cast<int>(i)); |
| 309 const String& src_type_name = | 310 const String& src_type_name = |
| 310 String::Handle(Type::Handle(elem.GetType()).Name()); | 311 String::Handle(Type::Handle(elem.GetType()).Name()); |
| 311 String& dst_type_name = String::Handle(); | 312 String& dst_type_name = String::Handle(); |
| 312 if (!element_type.IsInstantiated()) { | 313 if (!element_type.IsInstantiated()) { |
| 313 // Instantiate element_type before reporting the error. | 314 // Instantiate element_type before reporting the error. |
| 314 const Type& instantiated_element_type = Type::Handle( | 315 const AbstractType& instantiated_element_type = AbstractType::Handle( |
| 315 element_type.InstantiateFrom(element_type_instantiator, 0)); | 316 element_type.InstantiateFrom(element_type_instantiator, 0)); |
| 316 dst_type_name = instantiated_element_type.Name(); | 317 dst_type_name = instantiated_element_type.Name(); |
| 317 } else { | 318 } else { |
| 318 dst_type_name = element_type.Name(); | 319 dst_type_name = element_type.Name(); |
| 319 } | 320 } |
| 320 const String& dst_name = String::Handle(String::New(buf)); | 321 const String& dst_name = String::Handle(String::New(buf)); |
| 321 ThrowTypeError(location, src_type_name, dst_type_name, dst_name); | 322 ThrowTypeError(location, src_type_name, dst_type_name, dst_name); |
| 322 UNREACHABLE(); | 323 UNREACHABLE(); |
| 323 } | 324 } |
| 324 } | 325 } |
| 325 } | 326 } |
| 326 | 327 |
| 327 } // namespace dart | 328 } // namespace dart |
| OLD | NEW |