blob: 71a37efe2c658197db42a442c4602bcea26ab4dc [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "mirror/class-inl.h"
19#include "mirror/object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070020#include "runtime_support.h"
21
22namespace art {
23
24// Assignable test for code, won't throw. Null and equality tests already performed
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025extern "C" uint32_t artIsAssignableFromCode(const mirror::Class* klass,
26 const mirror::Class* ref_class)
Ian Rogersb726dcb2012-09-05 08:57:23 -070027 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070028 DCHECK(klass != NULL);
29 DCHECK(ref_class != NULL);
30 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
31}
32
33// Check whether it is safe to cast one class to the other, throw exception and return -1 on failure
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034extern "C" int artCheckCastFromCode(const mirror::Class* a, const mirror::Class* b, Thread* self,
35 mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070036 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070037 DCHECK(a->IsClass()) << PrettyClass(a);
38 DCHECK(b->IsClass()) << PrettyClass(b);
39 if (LIKELY(b->IsAssignableFrom(a))) {
40 return 0; // Success
41 } else {
42 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070043 self->ThrowNewExceptionF("Ljava/lang/ClassCastException;",
Ian Rogers57b86d42012-03-27 16:05:41 -070044 "%s cannot be cast to %s",
45 PrettyDescriptor(a).c_str(),
46 PrettyDescriptor(b).c_str());
47 return -1; // Failure
48 }
49}
50
51// Tests whether 'element' can be assigned into an array of type 'array_class'.
52// Returns 0 on success and -1 if an exception is pending.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053extern "C" int artCanPutArrayElementFromCode(const mirror::Object* element,
54 const mirror::Class* array_class,
55 Thread* self, mirror::AbstractMethod** sp)
Ian Rogersb726dcb2012-09-05 08:57:23 -070056 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers57b86d42012-03-27 16:05:41 -070057 DCHECK(array_class != NULL);
58 // element can't be NULL as we catch this is screened in runtime_support
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059 mirror::Class* element_class = element->GetClass();
60 mirror::Class* component_type = array_class->GetComponentType();
Ian Rogers57b86d42012-03-27 16:05:41 -070061 if (LIKELY(component_type->IsAssignableFrom(element_class))) {
62 return 0; // Success
63 } else {
64 FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070065 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogers57b86d42012-03-27 16:05:41 -070066 "%s cannot be stored in an array of type %s",
67 PrettyDescriptor(element_class).c_str(),
68 PrettyDescriptor(array_class).c_str());
69 return -1; // Failure
70 }
71}
72
73} // namespace art