Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "callee_save_frame.h" |
| 18 | #include "runtime_support.h" |
| 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | // Assignable test for code, won't throw. Null and equality tests already performed |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 23 | extern "C" uint32_t artIsAssignableFromCode(const Class* klass, const Class* ref_class) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 24 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | DCHECK(klass != NULL); |
| 26 | DCHECK(ref_class != NULL); |
| 27 | return klass->IsAssignableFrom(ref_class) ? 1 : 0; |
| 28 | } |
| 29 | |
| 30 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 32 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 33 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 34 | DCHECK(b->IsClass()) << PrettyClass(b); |
| 35 | if (LIKELY(b->IsAssignableFrom(a))) { |
| 36 | return 0; // Success |
| 37 | } else { |
| 38 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 39 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
| 40 | "%s cannot be cast to %s", |
| 41 | PrettyDescriptor(a).c_str(), |
| 42 | PrettyDescriptor(b).c_str()); |
| 43 | return -1; // Failure |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 48 | // Returns 0 on success and -1 if an exception is pending. |
| 49 | extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | Thread* self, Method** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame^] | 51 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 52 | DCHECK(array_class != NULL); |
| 53 | // element can't be NULL as we catch this is screened in runtime_support |
| 54 | Class* element_class = element->GetClass(); |
| 55 | Class* component_type = array_class->GetComponentType(); |
| 56 | if (LIKELY(component_type->IsAssignableFrom(element_class))) { |
| 57 | return 0; // Success |
| 58 | } else { |
| 59 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
| 60 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
| 61 | "%s cannot be stored in an array of type %s", |
| 62 | PrettyDescriptor(element_class).c_str(), |
| 63 | PrettyDescriptor(array_class).c_str()); |
| 64 | return -1; // Failure |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace art |