1 /*
2  * Copyright (C) 2016 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 #ifndef ART_RUNTIME_MIRROR_METHOD_TYPE_INL_H_
18 #define ART_RUNTIME_MIRROR_METHOD_TYPE_INL_H_
19 
20 #include "method_type.h"
21 
22 #include "base/casts.h"
23 #include "handle_scope-inl.h"
24 #include "mirror/object-inl.h"
25 
26 namespace art HIDDEN {
27 namespace mirror {
28 
RawMethodType(VariableSizedHandleScope * hs)29 inline RawMethodType::RawMethodType(VariableSizedHandleScope* hs)
30     : hs_(hs) {
31   DCHECK(hs != nullptr);
32 }
33 
IsValid()34 inline bool RawMethodType::IsValid() const {
35   return hs_->Size() != 0u;
36 }
37 
SetRType(ObjPtr<mirror::Class> rtype)38 inline void RawMethodType::SetRType(ObjPtr<mirror::Class> rtype) {
39   DCHECK(rtype != nullptr);
40   DCHECK_EQ(hs_->Size(), 0u);
41   hs_->NewHandle(rtype);
42   DCHECK_EQ(rtype, GetRType());
43 }
44 
AddPType(ObjPtr<mirror::Class> ptype)45 inline void RawMethodType::AddPType(ObjPtr<mirror::Class> ptype) {
46   DCHECK(ptype != nullptr);
47   DCHECK_NE(hs_->Size(), 0u);
48   hs_->NewHandle(ptype);
49   DCHECK_NE(GetNumberOfPTypes(), 0);
50   DCHECK_EQ(GetPType(GetNumberOfPTypes() - 1), ptype);
51 }
52 
GetNumberOfPTypes()53 inline int32_t RawMethodType::GetNumberOfPTypes() const {
54   DCHECK_NE(hs_->Size(), 0u);
55   return dchecked_integral_cast<int32_t>(hs_->Size() - 1u);
56 }
57 
GetPType(int32_t i)58 inline ObjPtr<mirror::Class> RawMethodType::GetPType(int32_t i) const {
59   DCHECK_LT(i, GetNumberOfPTypes());
60   return hs_->GetHandle<mirror::Class>(i + 1).Get();
61 }
62 
GetRType()63 inline ObjPtr<mirror::Class> RawMethodType::GetRType() const {
64   return GetRTypeHandle().Get();
65 }
66 
GetRTypeHandle()67 inline Handle<mirror::Class> RawMethodType::GetRTypeHandle() const {
68   DCHECK_NE(hs_->Size(), 0u);
69   return hs_->GetHandle<mirror::Class>(0u);
70 }
71 
GetPTypes()72 inline ObjPtr<ObjectArray<Class>> MethodType::GetPTypes() {
73   return GetFieldObject<ObjectArray<Class>>(OFFSET_OF_OBJECT_MEMBER(MethodType, p_types_));
74 }
75 
GetNumberOfPTypes()76 inline int MethodType::GetNumberOfPTypes() {
77   return GetPTypes()->GetLength();
78 }
79 
GetRType()80 inline ObjPtr<Class> MethodType::GetRType() {
81   return GetFieldObject<Class>(OFFSET_OF_OBJECT_MEMBER(MethodType, r_type_));
82 }
83 
84 template <typename PTypesType>
PTypesAccessor(PTypesType p_types)85 inline MethodType::PTypesAccessor<PTypesType>::PTypesAccessor(PTypesType p_types)
86     : p_types_(p_types) {}
87 
88 template <typename PTypesType>
GetLength()89 inline int32_t MethodType::PTypesAccessor<PTypesType>::GetLength() const {
90   return p_types_->GetLength();
91 }
92 
93 template <typename PTypesType>
Get(int32_t i)94 inline ObjPtr<mirror::Class> MethodType::PTypesAccessor<PTypesType>::Get(int32_t i) const {
95   DCHECK_LT(i, GetLength());
96   return p_types_->GetWithoutChecks(i);
97 }
98 
RawPTypesAccessor(RawMethodType method_type)99 inline MethodType::RawPTypesAccessor::RawPTypesAccessor(RawMethodType method_type)
100     : method_type_(method_type) {
101   DCHECK(method_type.IsValid());
102 }
103 
GetLength()104 inline int32_t MethodType::RawPTypesAccessor::GetLength() const {
105   return method_type_.GetNumberOfPTypes();
106 }
107 
Get(int32_t i)108 inline ObjPtr<mirror::Class> MethodType::RawPTypesAccessor::Get(int32_t i) const {
109   return method_type_.GetPType(i);
110 }
111 
112 template <typename HandleScopeType>
NewHandlePTypes(Handle<MethodType> method_type,HandleScopeType * hs)113 inline MethodType::HandlePTypesAccessor MethodType::NewHandlePTypes(
114     Handle<MethodType> method_type, HandleScopeType* hs) {
115   Handle<ObjectArray<mirror::Class>> p_types = hs->NewHandle(method_type->GetPTypes());
116   return HandlePTypesAccessor(p_types);
117 }
118 
119 template <typename HandleScopeType>
NewHandlePTypes(RawMethodType method_type,HandleScopeType * hs)120 inline MethodType::RawPTypesAccessor MethodType::NewHandlePTypes(
121     RawMethodType method_type, [[maybe_unused]] HandleScopeType* hs) {
122   return RawPTypesAccessor(method_type);
123 }
124 
GetPTypes(ObjPtr<MethodType> method_type)125 inline MethodType::ObjPtrPTypesAccessor MethodType::GetPTypes(ObjPtr<MethodType> method_type) {
126   return ObjPtrPTypesAccessor(method_type->GetPTypes());
127 }
128 
GetPTypes(Handle<MethodType> method_type)129 inline MethodType::ObjPtrPTypesAccessor MethodType::GetPTypes(Handle<MethodType> method_type) {
130   return GetPTypes(method_type.Get());
131 }
132 
GetPTypes(RawMethodType method_type)133 inline MethodType::RawPTypesAccessor MethodType::GetPTypes(RawMethodType method_type) {
134   return RawPTypesAccessor(method_type);
135 }
136 
GetRType(ObjPtr<MethodType> method_type)137 inline ObjPtr<mirror::Class> MethodType::GetRType(ObjPtr<MethodType> method_type) {
138   return method_type->GetRType();
139 }
140 
GetRType(Handle<MethodType> method_type)141 inline ObjPtr<mirror::Class> MethodType::GetRType(Handle<MethodType> method_type) {
142   return GetRType(method_type.Get());
143 }
144 
GetRType(RawMethodType method_type)145 inline ObjPtr<mirror::Class> MethodType::GetRType(RawMethodType method_type) {
146   return method_type.GetRType();
147 }
148 
149 }  // namespace mirror
150 }  // namespace art
151 
152 #endif  // ART_RUNTIME_MIRROR_METHOD_TYPE_INL_H_
153