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" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 18 | #include "mirror/class-inl.h" |
| 19 | #include "mirror/object-inl.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame^] | 20 | #include "mirror/object_array-inl.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 21 | #include "runtime_support.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | // Assignable test for code, won't throw. Null and equality tests already performed |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | extern "C" uint32_t artIsAssignableFromCode(const mirror::Class* klass, |
| 27 | const mirror::Class* ref_class) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 28 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 29 | DCHECK(klass != NULL); |
| 30 | DCHECK(ref_class != NULL); |
| 31 | return klass->IsAssignableFrom(ref_class) ? 1 : 0; |
| 32 | } |
| 33 | |
| 34 | // Check whether it is safe to cast one class to the other, throw exception and return -1 on failure |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 35 | extern "C" int artCheckCastFromCode(const mirror::Class* a, const mirror::Class* b, Thread* self, |
| 36 | mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 37 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 38 | DCHECK(a->IsClass()) << PrettyClass(a); |
| 39 | DCHECK(b->IsClass()) << PrettyClass(b); |
| 40 | if (LIKELY(b->IsAssignableFrom(a))) { |
| 41 | return 0; // Success |
| 42 | } else { |
| 43 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 44 | self->ThrowNewExceptionF("Ljava/lang/ClassCastException;", |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 45 | "%s cannot be cast to %s", |
| 46 | PrettyDescriptor(a).c_str(), |
| 47 | PrettyDescriptor(b).c_str()); |
| 48 | return -1; // Failure |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Tests whether 'element' can be assigned into an array of type 'array_class'. |
| 53 | // Returns 0 on success and -1 if an exception is pending. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | extern "C" int artCanPutArrayElementFromCode(const mirror::Object* element, |
| 55 | const mirror::Class* array_class, |
| 56 | Thread* self, mirror::AbstractMethod** sp) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 57 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 58 | DCHECK(array_class != NULL); |
| 59 | // element can't be NULL as we catch this is screened in runtime_support |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | mirror::Class* element_class = element->GetClass(); |
| 61 | mirror::Class* component_type = array_class->GetComponentType(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 62 | if (LIKELY(component_type->IsAssignableFrom(element_class))) { |
| 63 | return 0; // Success |
| 64 | } else { |
| 65 | FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly); |
Ian Rogers | 2fa6b2e | 2012-10-17 00:10:17 -0700 | [diff] [blame] | 66 | self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 67 | "%s cannot be stored in an array of type %s", |
| 68 | PrettyDescriptor(element_class).c_str(), |
| 69 | PrettyDescriptor(array_class).c_str()); |
| 70 | return -1; // Failure |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } // namespace art |