blob: 987e76417d00521cf66b9f4c545aa504ea1ebcf7 [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
23uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class) {
24 DCHECK(klass != NULL);
25 DCHECK(ref_class != NULL);
26 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
27}
28
29// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
30extern "C" int artCheckCastFromCode(const Class* a, const Class* b, Thread* self, Method** sp) {
31 DCHECK(a->IsClass()) << PrettyClass(a);
32 DCHECK(b->IsClass()) << PrettyClass(b);
33 if (LIKELY(b->IsAssignableFrom(a))) {
34 return 0; // Success
35 } else {
36 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
37 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
38 "%s cannot be cast to %s",
39 PrettyDescriptor(a).c_str(),
40 PrettyDescriptor(b).c_str());
41 return -1; // Failure
42 }
43}
44
45// Tests whether 'element' can be assigned into an array of type 'array_class'.
46// Returns 0 on success and -1 if an exception is pending.
47extern "C" int artCanPutArrayElementFromCode(const Object* element, const Class* array_class,
48 Thread* self, Method** sp) {
49 DCHECK(array_class != NULL);
50 // element can't be NULL as we catch this is screened in runtime_support
51 Class* element_class = element->GetClass();
52 Class* component_type = array_class->GetComponentType();
53 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
54 return 0; // Success
55 } else {
56 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
57 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
58 "%s cannot be stored in an array of type %s",
59 PrettyDescriptor(element_class).c_str(),
60 PrettyDescriptor(array_class).c_str());
61 return -1; // Failure
62 }
63}
64
65} // namespace art