blob: dca30626e07664bc8685c35830eaca030f1b472d [file] [log] [blame]
Narayan Kamathafa48272016-08-03 12:46:58 +01001/*
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_HANDLE_IMPL_H_
18#define ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_
19
20#include "class.h"
21#include "gc_root.h"
22#include "object.h"
Narayan Kamath9823e782016-08-03 12:46:58 +010023#include "method_handles.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010024#include "method_type.h"
25
26namespace art {
27
Narayan Kamathbd2fed52017-01-25 10:46:54 +000028struct MethodHandleOffsets;
Narayan Kamathafa48272016-08-03 12:46:58 +010029struct MethodHandleImplOffsets;
30
31namespace mirror {
32
33// C++ mirror of java.lang.invoke.MethodHandle
34class MANAGED MethodHandle : public Object {
35 public:
Orion Hodson811bd5f2016-12-07 11:35:37 +000036 // Defines the behaviour of a given method handle. The behaviour
37 // of a handle of a given kind is identical to the dex bytecode behaviour
38 // of the equivalent instruction.
39 //
40 // NOTE: These must be kept in sync with the constants defined in
41 // java.lang.invoke.MethodHandle.
42 enum Kind {
43 kInvokeVirtual = 0,
44 kInvokeSuper,
45 kInvokeDirect,
46 kInvokeStatic,
47 kInvokeInterface,
48 kInvokeTransform,
49 kInvokeCallSiteTransform,
50 kInstanceGet,
51 kInstancePut,
52 kStaticGet,
53 kStaticPut,
54 kLastValidKind = kStaticPut,
55 kFirstAccessorKind = kInstanceGet,
56 kLastAccessorKind = kStaticPut,
57 kLastInvokeKind = kInvokeCallSiteTransform
58 };
59
60 Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) {
61 const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_));
62 DCHECK(handle_kind >= 0 &&
63 handle_kind <= static_cast<int32_t>(Kind::kLastValidKind));
64 return static_cast<Kind>(handle_kind);
65 }
66
Narayan Kamathafa48272016-08-03 12:46:58 +010067 mirror::MethodType* GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_) {
68 return GetFieldObject<mirror::MethodType>(OFFSET_OF_OBJECT_MEMBER(MethodHandle, method_type_));
69 }
70
Narayan Kamath0a8485e2016-11-02 18:47:11 +000071 mirror::MethodType* GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_) {
72 return GetFieldObject<mirror::MethodType>(OFFSET_OF_OBJECT_MEMBER(MethodHandle, nominal_type_));
73 }
74
Orion Hodson3d617ac2016-10-19 14:00:46 +010075 ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) {
76 return reinterpret_cast<ArtField*>(
77 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
78 }
79
Narayan Kamathafa48272016-08-03 12:46:58 +010080 ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) {
81 return reinterpret_cast<ArtMethod*>(
82 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
83 }
84
Orion Hodsoncfa325e2016-10-13 10:25:54 +010085 static mirror::Class* StaticClass() REQUIRES_SHARED(Locks::mutator_lock_);
86
Narayan Kamathafa48272016-08-03 12:46:58 +010087 private:
Narayan Kamathc5889ce2017-01-19 20:42:23 +000088 // NOTE: cached_spread_invoker_ isn't used by the runtime.
89 HeapReference<mirror::MethodHandle> cached_spread_invoker_;
Narayan Kamath0a8485e2016-11-02 18:47:11 +000090 HeapReference<mirror::MethodType> nominal_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010091 HeapReference<mirror::MethodType> method_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010092 uint32_t handle_kind_;
Narayan Kamathc5889ce2017-01-19 20:42:23 +000093 uint64_t art_field_or_method_;
Narayan Kamathafa48272016-08-03 12:46:58 +010094
95 private:
Narayan Kamath0a8485e2016-11-02 18:47:11 +000096 static MemberOffset NominalTypeOffset() {
97 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_));
Narayan Kamathafa48272016-08-03 12:46:58 +010098 }
99 static MemberOffset MethodTypeOffset() {
100 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_));
101 }
102 static MemberOffset ArtFieldOrMethodOffset() {
103 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_));
104 }
105 static MemberOffset HandleKindOffset() {
106 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_));
107 }
108
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000109 friend struct art::MethodHandleOffsets; // for verifying offset information
Narayan Kamathafa48272016-08-03 12:46:58 +0100110 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle);
111};
112
113// C++ mirror of java.lang.invoke.MethodHandleImpl
114class MANAGED MethodHandleImpl : public MethodHandle {
115 public:
116 static mirror::Class* StaticClass() REQUIRES_SHARED(Locks::mutator_lock_) {
117 return static_class_.Read();
118 }
119
120 static void SetClass(Class* klass) REQUIRES_SHARED(Locks::mutator_lock_);
121 static void ResetClass() REQUIRES_SHARED(Locks::mutator_lock_);
122 static void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_);
123
124 private:
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000125 static MemberOffset InfoOffset() {
126 return MemberOffset(OFFSETOF_MEMBER(MethodHandleImpl, info_));
127 }
128
129 HeapReference<mirror::Object> info_; // Unused by the runtime.
Narayan Kamathafa48272016-08-03 12:46:58 +0100130 static GcRoot<mirror::Class> static_class_; // java.lang.invoke.MethodHandleImpl.class
131
132 friend struct art::MethodHandleImplOffsets; // for verifying offset information
133 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl);
134};
135
136} // namespace mirror
137} // namespace art
138
139#endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_