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_H_
18 #define ART_RUNTIME_MIRROR_METHOD_TYPE_H_
19 
20 #include "object_array.h"
21 #include "object.h"
22 #include "string.h"
23 
24 namespace art {
25 
26 struct MethodTypeOffsets;
27 
28 namespace mirror {
29 
30 // C++ mirror of java.lang.invoke.MethodType
31 class MANAGED MethodType : public Object {
32  public:
33   static ObjPtr<MethodType> Create(Thread* const self,
34                                    Handle<Class> return_type,
35                                    Handle<ObjectArray<Class>> param_types)
36       REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
37 
38   static ObjPtr<MethodType> CloneWithoutLeadingParameter(Thread* const self,
39                                                          ObjPtr<MethodType> method_type)
40       REQUIRES_SHARED(Locks::mutator_lock_);
41 
42   // Collects trailing parameter types into an array. Assumes caller
43   // has checked trailing arguments are all of the same type.
44   static ObjPtr<MethodType> CollectTrailingArguments(Thread* const self,
45                                                      ObjPtr<MethodType> method_type,
46                                                      ObjPtr<Class> collector_array_class,
47                                                      int32_t start_index)
48       REQUIRES_SHARED(Locks::mutator_lock_);
49 
50   ObjPtr<ObjectArray<Class>> GetPTypes() REQUIRES_SHARED(Locks::mutator_lock_);
51 
52   int GetNumberOfPTypes() REQUIRES_SHARED(Locks::mutator_lock_);
53 
54   // Number of virtual registers required to hold the parameters for
55   // this method type.
56   size_t NumberOfVRegs() REQUIRES_SHARED(Locks::mutator_lock_);
57 
58   ObjPtr<Class> GetRType() REQUIRES_SHARED(Locks::mutator_lock_);
59 
60   // Returns true iff. |this| is an exact match for method type |target|, i.e
61   // iff. they have the same return types and parameter types.
62   bool IsExactMatch(ObjPtr<MethodType> target) REQUIRES_SHARED(Locks::mutator_lock_);
63 
64   // Returns true iff. |this| can be converted to match |target| method type, i.e
65   // iff. they have convertible return types and parameter types.
66   bool IsConvertible(ObjPtr<MethodType> target) REQUIRES_SHARED(Locks::mutator_lock_);
67 
68   // Returns the pretty descriptor for this method type, suitable for display in
69   // exception messages and the like.
70   std::string PrettyDescriptor() REQUIRES_SHARED(Locks::mutator_lock_);
71 
72  private:
FormOffset()73   static MemberOffset FormOffset() {
74     return MemberOffset(OFFSETOF_MEMBER(MethodType, form_));
75   }
76 
MethodDescriptorOffset()77   static MemberOffset MethodDescriptorOffset() {
78     return MemberOffset(OFFSETOF_MEMBER(MethodType, method_descriptor_));
79   }
80 
PTypesOffset()81   static MemberOffset PTypesOffset() {
82     return MemberOffset(OFFSETOF_MEMBER(MethodType, p_types_));
83   }
84 
RTypeOffset()85   static MemberOffset RTypeOffset() {
86     return MemberOffset(OFFSETOF_MEMBER(MethodType, r_type_));
87   }
88 
WrapAltOffset()89   static MemberOffset WrapAltOffset() {
90     return MemberOffset(OFFSETOF_MEMBER(MethodType, wrap_alt_));
91   }
92 
93   HeapReference<Object> form_;  // Unused in the runtime
94   HeapReference<String> method_descriptor_;  // Unused in the runtime
95   HeapReference<ObjectArray<Class>> p_types_;
96   HeapReference<Class> r_type_;
97   HeapReference<Object> wrap_alt_;  // Unused in the runtime
98 
99   friend struct art::MethodTypeOffsets;  // for verifying offset information
100   DISALLOW_IMPLICIT_CONSTRUCTORS(MethodType);
101 };
102 
103 }  // namespace mirror
104 }  // namespace art
105 
106 #endif  // ART_RUNTIME_MIRROR_METHOD_TYPE_H_
107