Home | History | Annotate | Download | only in quick
      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"
     18 #include "entrypoints/entrypoint_utils.h"
     19 #include "mirror/class-inl.h"
     20 #include "mirror/object-inl.h"
     21 #include "mirror/object_array-inl.h"
     22 
     23 namespace art {
     24 
     25 // Assignable test for code, won't throw.  Null and equality tests already performed
     26 extern "C" uint32_t artIsAssignableFromCode(const mirror::Class* klass,
     27                                             const mirror::Class* ref_class)
     28     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     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
     35 extern "C" int artCheckCastFromCode(mirror::Class* src_type, mirror::Class* dest_type,
     36                                     Thread* self, mirror::ArtMethod** sp)
     37     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     38   DCHECK(src_type->IsClass()) << PrettyClass(src_type);
     39   DCHECK(dest_type->IsClass()) << PrettyClass(dest_type);
     40   if (LIKELY(dest_type->IsAssignableFrom(src_type))) {
     41     return 0;  // Success
     42   } else {
     43     FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
     44     ThrowClassCastException(dest_type, src_type);
     45     return -1;  // Failure
     46   }
     47 }
     48 
     49 // Tests whether 'element' can be assigned into an array of type 'array_class'.
     50 // Returns 0 on success and -1 if an exception is pending.
     51 extern "C" int artCanPutArrayElementFromCode(const mirror::Object* element,
     52                                              const mirror::Class* array_class,
     53                                              Thread* self, mirror::ArtMethod** sp)
     54     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
     55   DCHECK(array_class != NULL);
     56   // element can't be NULL as we catch this is screened in runtime_support
     57   mirror::Class* element_class = element->GetClass();
     58   mirror::Class* component_type = array_class->GetComponentType();
     59   if (LIKELY(component_type->IsAssignableFrom(element_class))) {
     60     return 0;  // Success
     61   } else {
     62     FinishCalleeSaveFrameSetup(self, sp, Runtime::kRefsOnly);
     63     ThrowArrayStoreException(element_class, array_class);
     64     return -1;  // Failure
     65   }
     66 }
     67 
     68 }  // namespace art
     69