blob: 0db743b16cd1f94a81cfc0677653d1b138cd4886 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
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
20namespace art {
21
22// Assignable test for code, won't throw. Null and equality tests already performed
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023extern "C" uint32_t artIsAssignableFromCode(const Class* klass, const Class* ref_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070024 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070025 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
Mathieu Chartier66f19252012-09-18 08:57:04 -070031extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self,
32 AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070033 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070034 DCHECK(a->IsClass()) << PrettyClass(a);
35 DCHECK(b->IsClass()) << PrettyClass(b);
36 if (LIKELY(b->IsAssignableFrom(a))) {
37 return 0; // Success
38 } else {
39 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070040 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Ian Rogers57b86d42012-03-27 16:05:41 -070041 "%s cannot be cast to %s",
42 PrettyDescriptor(a).c_str(),
43 PrettyDescriptor(b).c_str());
44 return -1; // Failure
45 }
46}
47
48// Tests whether 'element' can be assigned into an array of type 'array_class'.
49// Returns 0 on success and -1 if an exception is pending.
50extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
Mathieu Chartier66f19252012-09-18 08:57:04 -070051 Thread* self, AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070052 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070053 DCHECK(array_class != NULL);
54 // element can't be NULL as we catch this is screened in runtime_support
55 Class* element_class = element->GetClass();
56 Class* component_type = array_class->GetComponentType();
57 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
58 return 0; // Success
59 } else {
60 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070061 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogers57b86d42012-03-27 16:05:41 -070062 "%s cannot be stored in an array of type %s",
63 PrettyDescriptor(element_class).c_str(),
64 PrettyDescriptor(array_class).c_str());
65 return -1; // Failure
66 }
67}
68
69} // namespace art