blob: c973a24111f72398a1784d3f0cd88c1f300ec2d1 [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
Orion Hodsonc069a302017-01-18 09:23:12 +000020#include "art_field.h"
21#include "art_method.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010022#include "class.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010023#include "method_type.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000024#include "obj_ptr.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "object.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010026
27namespace art {
28
Narayan Kamathbd2fed52017-01-25 10:46:54 +000029struct MethodHandleOffsets;
Narayan Kamathafa48272016-08-03 12:46:58 +010030struct MethodHandleImplOffsets;
31
32namespace mirror {
33
34// C++ mirror of java.lang.invoke.MethodHandle
35class MANAGED MethodHandle : public Object {
36 public:
Orion Hodson811bd5f2016-12-07 11:35:37 +000037 // Defines the behaviour of a given method handle. The behaviour
38 // of a handle of a given kind is identical to the dex bytecode behaviour
39 // of the equivalent instruction.
40 //
41 // NOTE: These must be kept in sync with the constants defined in
42 // java.lang.invoke.MethodHandle.
43 enum Kind {
44 kInvokeVirtual = 0,
45 kInvokeSuper,
46 kInvokeDirect,
47 kInvokeStatic,
48 kInvokeInterface,
49 kInvokeTransform,
50 kInvokeCallSiteTransform,
Orion Hodsonb8b93872018-01-30 07:51:10 +000051 kInvokeVarHandle,
52 kInvokeVarHandleExact,
Orion Hodson811bd5f2016-12-07 11:35:37 +000053 kInstanceGet,
54 kInstancePut,
55 kStaticGet,
56 kStaticPut,
57 kLastValidKind = kStaticPut,
58 kFirstAccessorKind = kInstanceGet,
59 kLastAccessorKind = kStaticPut,
Orion Hodsonb8b93872018-01-30 07:51:10 +000060 kLastInvokeKind = kInvokeVarHandleExact
Orion Hodson811bd5f2016-12-07 11:35:37 +000061 };
62
63 Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) {
64 const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_));
65 DCHECK(handle_kind >= 0 &&
66 handle_kind <= static_cast<int32_t>(Kind::kLastValidKind));
67 return static_cast<Kind>(handle_kind);
68 }
69
Vladimir Marko5aead702019-03-27 11:00:36 +000070 ALWAYS_INLINE ObjPtr<mirror::MethodType> GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathafa48272016-08-03 12:46:58 +010071
Vladimir Marko5aead702019-03-27 11:00:36 +000072 ALWAYS_INLINE ObjPtr<mirror::MethodType> GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath0a8485e2016-11-02 18:47:11 +000073
Orion Hodson3d617ac2016-10-19 14:00:46 +010074 ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) {
75 return reinterpret_cast<ArtField*>(
76 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
77 }
78
Narayan Kamathafa48272016-08-03 12:46:58 +010079 ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) {
80 return reinterpret_cast<ArtMethod*>(
81 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
82 }
83
Andreas Gampec6ea7d02017-02-01 16:46:28 -080084 ALWAYS_INLINE ObjPtr<mirror::Class> GetTargetClass() REQUIRES_SHARED(Locks::mutator_lock_);
Orion Hodsonc069a302017-01-18 09:23:12 +000085
Orion Hodsonfe92d122018-01-02 10:45:17 +000086 // Gets the return type for a named invoke method, or nullptr if the invoke method is not
87 // supported.
88 static const char* GetReturnTypeDescriptor(const char* invoke_method_name);
89
Orion Hodsonc069a302017-01-18 09:23:12 +000090 protected:
91 void Initialize(uintptr_t art_field_or_method, Kind kind, Handle<MethodType> method_type)
92 REQUIRES_SHARED(Locks::mutator_lock_);
93
Narayan Kamathafa48272016-08-03 12:46:58 +010094 private:
Narayan Kamathc5889ce2017-01-19 20:42:23 +000095 HeapReference<mirror::MethodHandle> cached_spread_invoker_;
Narayan Kamath0a8485e2016-11-02 18:47:11 +000096 HeapReference<mirror::MethodType> nominal_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010097 HeapReference<mirror::MethodType> method_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010098 uint32_t handle_kind_;
Narayan Kamathc5889ce2017-01-19 20:42:23 +000099 uint64_t art_field_or_method_;
Narayan Kamathafa48272016-08-03 12:46:58 +0100100
101 private:
Orion Hodsonc069a302017-01-18 09:23:12 +0000102 static MemberOffset CachedSpreadInvokerOffset() {
103 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, cached_spread_invoker_));
104 }
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000105 static MemberOffset NominalTypeOffset() {
106 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_));
Narayan Kamathafa48272016-08-03 12:46:58 +0100107 }
108 static MemberOffset MethodTypeOffset() {
109 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_));
110 }
111 static MemberOffset ArtFieldOrMethodOffset() {
112 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_));
113 }
114 static MemberOffset HandleKindOffset() {
115 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_));
116 }
117
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000118 friend struct art::MethodHandleOffsets; // for verifying offset information
Narayan Kamathafa48272016-08-03 12:46:58 +0100119 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle);
120};
121
122// C++ mirror of java.lang.invoke.MethodHandleImpl
123class MANAGED MethodHandleImpl : public MethodHandle {
124 public:
Vladimir Marko5aead702019-03-27 11:00:36 +0000125 static ObjPtr<mirror::MethodHandleImpl> Create(Thread* const self,
126 uintptr_t art_field_or_method,
127 MethodHandle::Kind kind,
128 Handle<MethodType> method_type)
Orion Hodsonc069a302017-01-18 09:23:12 +0000129 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
130
Narayan Kamathafa48272016-08-03 12:46:58 +0100131 private:
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000132 static MemberOffset InfoOffset() {
133 return MemberOffset(OFFSETOF_MEMBER(MethodHandleImpl, info_));
134 }
135
136 HeapReference<mirror::Object> info_; // Unused by the runtime.
Narayan Kamathafa48272016-08-03 12:46:58 +0100137
138 friend struct art::MethodHandleImplOffsets; // for verifying offset information
139 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl);
140};
141
142} // namespace mirror
143} // namespace art
144
145#endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_