blob: 45a3e60d83d25015d1af543d9653eba92f660bf5 [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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070031extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070032 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070033 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.
49extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070050 Thread* self, Method** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070051 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070052 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