blob: 030a49ed1ee9f11fd81a52fc263effd9dc07cce8 [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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "object.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010025
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,
Orion Hodsonb8b93872018-01-30 07:51:10 +000050 kInvokeVarHandle,
51 kInvokeVarHandleExact,
Orion Hodson811bd5f2016-12-07 11:35:37 +000052 kInstanceGet,
53 kInstancePut,
54 kStaticGet,
55 kStaticPut,
56 kLastValidKind = kStaticPut,
57 kFirstAccessorKind = kInstanceGet,
58 kLastAccessorKind = kStaticPut,
Orion Hodsonb8b93872018-01-30 07:51:10 +000059 kLastInvokeKind = kInvokeVarHandleExact
Orion Hodson811bd5f2016-12-07 11:35:37 +000060 };
61
62 Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) {
63 const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_));
64 DCHECK(handle_kind >= 0 &&
65 handle_kind <= static_cast<int32_t>(Kind::kLastValidKind));
66 return static_cast<Kind>(handle_kind);
67 }
68
Andreas Gampec6ea7d02017-02-01 16:46:28 -080069 ALWAYS_INLINE mirror::MethodType* GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathafa48272016-08-03 12:46:58 +010070
Andreas Gampec6ea7d02017-02-01 16:46:28 -080071 ALWAYS_INLINE mirror::MethodType* GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath0a8485e2016-11-02 18:47:11 +000072
Orion Hodson3d617ac2016-10-19 14:00:46 +010073 ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) {
74 return reinterpret_cast<ArtField*>(
75 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
76 }
77
Narayan Kamathafa48272016-08-03 12:46:58 +010078 ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) {
79 return reinterpret_cast<ArtMethod*>(
80 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
81 }
82
Andreas Gampec6ea7d02017-02-01 16:46:28 -080083 ALWAYS_INLINE ObjPtr<mirror::Class> GetTargetClass() REQUIRES_SHARED(Locks::mutator_lock_);
Orion Hodsonc069a302017-01-18 09:23:12 +000084
Orion Hodsonfe92d122018-01-02 10:45:17 +000085 // Gets the return type for a named invoke method, or nullptr if the invoke method is not
86 // supported.
87 static const char* GetReturnTypeDescriptor(const char* invoke_method_name);
88
Orion Hodsonc069a302017-01-18 09:23:12 +000089 protected:
90 void Initialize(uintptr_t art_field_or_method, Kind kind, Handle<MethodType> method_type)
91 REQUIRES_SHARED(Locks::mutator_lock_);
92
Narayan Kamathafa48272016-08-03 12:46:58 +010093 private:
Narayan Kamathc5889ce2017-01-19 20:42:23 +000094 HeapReference<mirror::MethodHandle> cached_spread_invoker_;
Narayan Kamath0a8485e2016-11-02 18:47:11 +000095 HeapReference<mirror::MethodType> nominal_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010096 HeapReference<mirror::MethodType> method_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +010097 uint32_t handle_kind_;
Narayan Kamathc5889ce2017-01-19 20:42:23 +000098 uint64_t art_field_or_method_;
Narayan Kamathafa48272016-08-03 12:46:58 +010099
100 private:
Orion Hodsonc069a302017-01-18 09:23:12 +0000101 static MemberOffset CachedSpreadInvokerOffset() {
102 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, cached_spread_invoker_));
103 }
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000104 static MemberOffset NominalTypeOffset() {
105 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_));
Narayan Kamathafa48272016-08-03 12:46:58 +0100106 }
107 static MemberOffset MethodTypeOffset() {
108 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_));
109 }
110 static MemberOffset ArtFieldOrMethodOffset() {
111 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_));
112 }
113 static MemberOffset HandleKindOffset() {
114 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_));
115 }
116
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000117 friend struct art::MethodHandleOffsets; // for verifying offset information
Narayan Kamathafa48272016-08-03 12:46:58 +0100118 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle);
119};
120
121// C++ mirror of java.lang.invoke.MethodHandleImpl
122class MANAGED MethodHandleImpl : public MethodHandle {
123 public:
Orion Hodsonc069a302017-01-18 09:23:12 +0000124 static mirror::MethodHandleImpl* Create(Thread* const self,
125 uintptr_t art_field_or_method,
126 MethodHandle::Kind kind,
127 Handle<MethodType> method_type)
128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
129
Narayan Kamathafa48272016-08-03 12:46:58 +0100130 private:
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000131 static MemberOffset InfoOffset() {
132 return MemberOffset(OFFSETOF_MEMBER(MethodHandleImpl, info_));
133 }
134
135 HeapReference<mirror::Object> info_; // Unused by the runtime.
Narayan Kamathafa48272016-08-03 12:46:58 +0100136
137 friend struct art::MethodHandleImplOffsets; // for verifying offset information
138 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl);
139};
140
141} // namespace mirror
142} // namespace art
143
144#endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_