blob: 6bbccb8c4d564648a0d9714fd8ba21759818c938 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 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
Mathieu Chartiere401d142015-04-22 13:56:20 -070017#ifndef ART_RUNTIME_ART_METHOD_INL_H_
18#define ART_RUNTIME_ART_METHOD_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021
Andreas Gampe58a5af82014-07-31 16:23:49 -070022#include "art_field.h"
Andreas Gampe8228cdf2017-05-30 15:03:54 -070023#include "base/callee_save_type.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070024#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010025#include "common_throws.h"
David Sehr9e734c72018-01-04 17:56:19 -080026#include "dex/code_item_accessors-inl.h"
27#include "dex/dex_file-inl.h"
28#include "dex/dex_file_annotations.h"
29#include "dex/dex_file_types.h"
David Sehr8c0961f2018-01-23 16:11:38 -080030#include "dex/invoke_type.h"
David Sehr67bf42e2018-02-26 16:43:04 -080031#include "dex/primitive.h"
Andreas Gampeb79674c2019-05-14 16:16:46 -070032#include "dex/signature.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "gc_root-inl.h"
David Srbeckye36e7f22018-11-14 14:21:23 +000034#include "imtable-inl.h"
David Brazdil87144352018-01-24 12:50:01 +000035#include "intrinsics_enum.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010036#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070037#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010038#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070039#include "mirror/object-inl.h"
40#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000041#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010042#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070043#include "obj_ptr-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010044#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070045#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010046#include "runtime-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070047#include "thread-current-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
49namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080051template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010052inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070053 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080054 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070055}
56
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080057template <ReadBarrierOption kReadBarrierOption>
Vladimir Markod93e3742018-07-18 10:58:13 +010058inline ObjPtr<mirror::Class> ArtMethod::GetDeclaringClass() {
59 ObjPtr<mirror::Class> result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070060 if (kIsDebugBuild) {
61 if (!IsRuntimeMethod()) {
62 CHECK(result != nullptr) << this;
Mathieu Chartiere401d142015-04-22 13:56:20 -070063 } else {
64 CHECK(result == nullptr) << this;
65 }
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 return result;
68}
69
Mathieu Chartier28357fa2016-10-18 16:27:40 -070070inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070071 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Vladimir Markod93e3742018-07-18 10:58:13 +010074inline bool ArtMethod::CASDeclaringClass(ObjPtr<mirror::Class> expected_class,
75 ObjPtr<mirror::Class> desired_class) {
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070076 GcRoot<mirror::Class> expected_root(expected_class);
77 GcRoot<mirror::Class> desired_root(desired_class);
Orion Hodson4557b382018-01-03 11:47:54 +000078 auto atomic_root_class = reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_);
79 return atomic_root_class->CompareAndSetStrongSequentiallyConsistent(expected_root, desired_root);
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070080}
81
Ian Rogersef7d42f2014-01-06 12:55:46 -080082inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000083 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070084 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085}
86
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070087inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070088 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070089}
90
Vladimir Markob45528c2017-07-27 14:14:28 +010091inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +000092 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko666ee3d2017-12-11 18:37:36 +000093 ObjPtr<mirror::Class> type =
94 Runtime::Current()->GetClassLinker()->LookupResolvedType(type_idx, this);
95 DCHECK(!Thread::Current()->IsExceptionPending());
96 return type;
Vladimir Markob45528c2017-07-27 14:14:28 +010097}
98
99inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000100 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
101 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
102 return type;
Ian Rogersa0485602014-12-02 15:48:04 -0800103}
104
Brian Carlstromea46f952013-07-30 01:26:50 -0700105inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 switch (type) {
107 case kStatic:
108 return !IsStatic();
109 case kDirect:
110 return !IsDirect() || IsStatic();
111 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700112 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
113 // method.
Vladimir Markod93e3742018-07-18 10:58:13 +0100114 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700115 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 }
117 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700118 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700119 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120 case kInterface: {
Vladimir Markod93e3742018-07-18 10:58:13 +0100121 ObjPtr<mirror::Class> methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
123 }
124 default:
125 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700126 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 }
128}
129
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 if (!IsRuntimeMethod()) {
132 return false;
133 }
134 Runtime* runtime = Runtime::Current();
135 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700136 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
137 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 result = true;
139 break;
140 }
141 }
142 return result;
143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 bool result = this == Runtime::Current()->GetResolutionMethod();
147 // Check that if we do think it is phony it looks like the resolution method.
148 DCHECK(!result || IsRuntimeMethod());
149 return result;
150}
Jeff Hao88474b42013-10-23 16:24:40 -0700151
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700152inline bool ArtMethod::IsImtUnimplementedMethod() {
153 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
154 // Check that if we do think it is phony it looks like the imt unimplemented method.
155 DCHECK(!result || IsRuntimeMethod());
156 return result;
157}
158
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700159inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800160 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
161 // from-space dex file pointer it will be equal to the to-space copy.
162 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700163}
164
165inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700166 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700167 if (UNLIKELY(dex_method_idx == dex::kDexNoIndex)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700168 return "<runtime method>";
169 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 DCHECK(!IsProxyMethod());
171 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700172 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
173}
174
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800175inline const char* ArtMethod::GetShorty() {
176 uint32_t unused_length;
177 return GetShorty(&unused_length);
178}
179
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700180inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000181 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 const DexFile* dex_file = GetDexFile();
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100183 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700184}
185
186inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700187 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700188 if (dex_method_idx != dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 DCHECK(!IsProxyMethod());
190 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700191 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
192 }
193 return Signature::NoSignature();
194}
195
Ian Rogers1ff3c982014-08-12 02:30:58 -0700196inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 uint32_t dex_method_idx = GetDexMethodIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700198 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700199 DCHECK(!IsProxyMethod());
200 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700201 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
202 }
Eric Holkabdb4592019-05-16 08:33:12 -0700203 return GetRuntimeMethodName();
204}
205
206inline std::string_view ArtMethod::GetNameView() {
207 uint32_t dex_method_idx = GetDexMethodIndex();
208 if (LIKELY(dex_method_idx != dex::kDexNoIndex)) {
209 DCHECK(!IsProxyMethod());
210 const DexFile* dex_file = GetDexFile();
211 uint32_t length = 0;
212 const char* name = dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx), &length);
213 return StringViewFromUtf16Length(name, length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700214 }
Eric Holkabdb4592019-05-16 08:33:12 -0700215 return GetRuntimeMethodName();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700216}
217
Vladimir Marko18090d12018-06-01 16:53:12 +0100218inline ObjPtr<mirror::String> ArtMethod::ResolveNameString() {
219 DCHECK(!IsProxyMethod());
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800220 const dex::MethodId& method_id = GetDexFile()->GetMethodId(GetDexMethodIndex());
Vladimir Marko18090d12018-06-01 16:53:12 +0100221 return Runtime::Current()->GetClassLinker()->ResolveString(method_id.name_idx_, this);
222}
223
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800224inline const dex::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800225 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700226}
227
Vladimir Marko942fd312017-01-16 20:52:19 +0000228inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100230 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700231}
232
233inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 DCHECK(!IsProxyMethod());
Andreas Gampee2abbc62017-09-15 11:59:26 -0700235 if (dex_pc == dex::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700237 }
David Sehr9323e6e2016-09-13 08:58:35 -0700238 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700239}
240
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800241inline const dex::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 DCHECK(!IsProxyMethod());
243 const DexFile* dex_file = GetDexFile();
244 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700245}
246
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800247inline const dex::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000248 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800250 const dex::ProtoId& proto = dex_file->GetMethodPrototype(
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000251 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252 return dex_file->GetProtoParameters(proto);
253}
254
255inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 DCHECK(!IsProxyMethod());
257 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700258}
259
260inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000262 if (LIKELY(!IsObsolete())) {
263 return GetDeclaringClass()->GetDexClassDefIndex();
264 } else {
265 return FindObsoleteDexClassDefIndex();
266 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700267}
268
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800269inline const dex::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 DCHECK(!IsProxyMethod());
271 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700272}
273
Orion Hodson58143d22018-02-20 08:44:20 +0000274inline size_t ArtMethod::GetNumberOfParameters() {
275 constexpr size_t return_type_count = 1u;
276 return strlen(GetShorty()) - return_type_count;
277}
278
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 DCHECK(!IsProxyMethod());
281 const DexFile* dex_file = GetDexFile();
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000282 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(GetReturnTypeIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283}
284
Alex Lightd7661582017-05-01 13:48:16 -0700285inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
286 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
287}
288
Andreas Gampea5b09a62016-11-17 15:21:22 -0800289inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 DCHECK(!IsProxyMethod());
291 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700292 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
293}
294
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000295inline ObjPtr<mirror::ClassLoader> ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 DCHECK(!IsProxyMethod());
297 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700298}
299
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800300template <ReadBarrierOption kReadBarrierOption>
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000301inline ObjPtr<mirror::DexCache> ArtMethod::GetDexCache() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100302 if (LIKELY(!IsObsolete())) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100303 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800304 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800305 } else {
306 DCHECK(!IsProxyMethod());
307 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800308 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700309}
310
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700311inline bool ArtMethod::IsProxyMethod() {
Roland Levillainfa854e42018-02-07 13:09:55 +0000312 DCHECK(!IsRuntimeMethod()) << "ArtMethod::IsProxyMethod called on a runtime method";
Vladimir Marko415ac852019-03-21 12:49:10 +0000313 // No read barrier needed, we're reading the constant declaring class only to read
314 // the constant proxy flag. See ReadBarrierOption.
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800315 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700316}
317
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000318inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
319 DCHECK(IsProxyMethod());
320 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
321 // which may give false negatives while visiting references for a non-CC moving GC.
322 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
323}
324
Andreas Gampe542451c2016-07-26 09:02:02 -0700325inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700326 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700327 return this;
328 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000329 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
330 // We can check that the proxy class implements the interface only if the proxy class
331 // is resolved, otherwise the interface table is not yet initialized.
332 DCHECK(!GetDeclaringClass()->IsResolved() ||
333 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700334 return interface_method;
335}
336
Vladimir Markob45528c2017-07-27 14:14:28 +0100337inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700338 DCHECK(!IsProxyMethod());
339 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800340 const dex::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
341 const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100342 return proto_id.return_type_idx_;
343}
344
345inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
346 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
347}
348
349inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
350 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700351}
352
Alexey Grebenkinbe4c2bd2018-02-01 19:09:59 +0300353template <ReadBarrierOption kReadBarrierOption>
Mingyao Yang063fc772016-08-02 11:02:54 -0700354inline bool ArtMethod::HasSingleImplementation() {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100355 if (IsFinal() || GetDeclaringClass<kReadBarrierOption>()->IsFinal()) {
Mingyao Yang063fc772016-08-02 11:02:54 -0700356 // We don't set kAccSingleImplementation for these cases since intrinsic
357 // can use the flag also.
358 return true;
359 }
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100360 return (GetAccessFlags() & kAccSingleImplementation) != 0;
Mingyao Yang063fc772016-08-02 11:02:54 -0700361}
362
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700363template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700364void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700365 if (LIKELY(!declaring_class_.IsNull())) {
366 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
Vladimir Markod93e3742018-07-18 10:58:13 +0100367 ObjPtr<mirror::Class> klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000368 if (UNLIKELY(klass->IsProxyClass())) {
369 // For normal methods, dex cache shortcuts will be visited through the declaring class.
370 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000371 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000372 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000373 interface_method->VisitRoots(visitor, pointer_size);
374 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100375 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800376}
377
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800378template <typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700379inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Vladimir Markoc945e0d2018-07-18 17:26:45 +0100380 if (IsNative()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800381 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800382 const void* new_native_code = visitor(old_native_code);
383 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800384 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800385 }
386 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700387 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800388 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800389 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800390 const void* new_code = visitor(old_code);
391 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800392 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800393 }
394}
395
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800396inline CodeItemInstructionAccessor ArtMethod::DexInstructions() {
David Sehr0225f8e2018-01-31 08:52:24 +0000397 return CodeItemInstructionAccessor(*GetDexFile(), GetCodeItem());
398}
399
400inline CodeItemDataAccessor ArtMethod::DexInstructionData() {
401 return CodeItemDataAccessor(*GetDexFile(), GetCodeItem());
402}
403
404inline CodeItemDebugInfoAccessor ArtMethod::DexInstructionDebugInfo() {
405 return CodeItemDebugInfoAccessor(*GetDexFile(), GetCodeItem(), GetDexMethodIndex());
Mathieu Chartier69147f12017-11-06 20:02:24 -0800406}
407
David Srbeckye36e7f22018-11-14 14:21:23 +0000408inline void ArtMethod::SetCounter(int16_t hotness_count) {
409 DCHECK(!IsAbstract()) << PrettyMethod();
410 hotness_count_ = hotness_count;
411}
412
413inline uint16_t ArtMethod::GetCounter() {
414 DCHECK(!IsAbstract()) << PrettyMethod();
415 return hotness_count_;
416}
417
418inline uint32_t ArtMethod::GetImtIndex() {
419 if (LIKELY(IsAbstract() && imt_index_ != 0)) {
420 uint16_t imt_index = ~imt_index_;
421 DCHECK_EQ(imt_index, ImTable::GetImtIndex(this)) << PrettyMethod();
422 return imt_index;
423 } else {
424 return ImTable::GetImtIndex(this);
425 }
426}
427
428inline void ArtMethod::CalculateAndSetImtIndex() {
429 DCHECK(IsAbstract()) << PrettyMethod();
430 imt_index_ = ~ImTable::GetImtIndex(this);
431}
432
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433} // namespace art
434
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435#endif // ART_RUNTIME_ART_METHOD_INL_H_