blob: 07ca35fb0013eb0770478b4c2bfed1ce2c5d8d87 [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"
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -070023#include "base/logging.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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "dex_file.h"
David Sehr9323e6e2016-09-13 08:58:35 -070027#include "dex_file_annotations.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080028#include "dex_file-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070029#include "gc_root-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070030#include "invoke_type.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010031#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010033#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070034#include "mirror/object-inl.h"
35#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000036#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010037#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070038#include "obj_ptr-inl.h"
Alex Lightd7661582017-05-01 13:48:16 -070039#include "primitive.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010040#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070041#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010042#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070043#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070044#include "thread-current-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046
47namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080049template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070050inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070051 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080052 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070053}
54
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080055template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070056inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080057 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070058 if (kIsDebugBuild) {
59 if (!IsRuntimeMethod()) {
60 CHECK(result != nullptr) << this;
Andreas Gampeb1106e22017-02-23 11:34:48 -080061 if (kCheckDeclaringClassState) {
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000062 if (!(result->IsIdxLoaded() || result->IsErroneous())) {
63 LOG(FATAL_WITHOUT_ABORT) << "Class status: " << result->GetStatus();
64 LOG(FATAL) << result->PrettyClass();
65 }
Andreas Gampeb1106e22017-02-23 11:34:48 -080066 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070067 } else {
68 CHECK(result == nullptr) << this;
69 }
70 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071 return result;
72}
73
Mathieu Chartier28357fa2016-10-18 16:27:40 -070074inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070075 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076}
77
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070078inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
79 mirror::Class* desired_class) {
80 GcRoot<mirror::Class> expected_root(expected_class);
81 GcRoot<mirror::Class> desired_root(desired_class);
82 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
83 CompareExchangeStrongSequentiallyConsistent(
84 expected_root, desired_root);
85}
86
Ian Rogersef7d42f2014-01-06 12:55:46 -080087inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000088 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070089 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090}
91
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070092inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070093 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070094}
95
Ian Rogersef7d42f2014-01-06 12:55:46 -080096inline uint32_t ArtMethod::GetDexMethodIndex() {
Andreas Gampeb1106e22017-02-23 11:34:48 -080097 if (kCheckDeclaringClassState) {
98 CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
99 GetDeclaringClass()->IsErroneous());
100 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000101 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102}
103
Andreas Gampe542451c2016-07-26 09:02:02 -0700104inline ArtMethod** ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100105 return GetNativePointer<ArtMethod**>(DexCacheResolvedMethodsOffset(pointer_size),
106 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700107}
108
Andreas Gampe542451c2016-07-26 09:02:02 -0700109inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
110 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100111 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
112 // without accessing the DexCache and we don't want to do that in release build.
113 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800114 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Andreas Gampe542451c2016-07-26 09:02:02 -0700115 ArtMethod* method = mirror::DexCache::GetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100116 method_index,
Andreas Gampe542451c2016-07-26 09:02:02 -0700117 pointer_size);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700118 if (LIKELY(method != nullptr)) {
119 auto* declaring_class = method->GetDeclaringClass();
120 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
121 return method;
122 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700123 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700125}
126
Andreas Gampe542451c2016-07-26 09:02:02 -0700127inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
128 ArtMethod* new_method,
129 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100130 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
131 // without accessing the DexCache and we don't want to do that in release build.
132 DCHECK_LT(method_index,
Alex Lightdba61482016-12-21 08:20:29 -0800133 GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700134 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -0700135 mirror::DexCache::SetElementPtrSize(GetDexCacheResolvedMethods(pointer_size),
Vladimir Marko05792b92015-08-03 11:56:49 +0100136 method_index,
137 new_method,
Andreas Gampe542451c2016-07-26 09:02:02 -0700138 pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700139}
140
Andreas Gampe542451c2016-07-26 09:02:02 -0700141inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100142 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700143}
144
Vladimir Marko05792b92015-08-03 11:56:49 +0100145inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod** other_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700146 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100147 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700148}
149
Andreas Gampe542451c2016-07-26 09:02:02 -0700150inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100151 return GetDexCacheResolvedMethods(pointer_size) ==
152 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700153}
154
Vladimir Marko942fd312017-01-16 20:52:19 +0000155inline mirror::Class* ArtMethod::GetClassFromTypeIndex(dex::TypeIndex type_idx, bool resolve) {
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000156 // TODO: Refactor this function into two functions, Resolve...() and Lookup...()
157 // so that we can properly annotate it with no-suspension possible / suspension possible.
Vladimir Marko942fd312017-01-16 20:52:19 +0000158 ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
159 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000160 if (UNLIKELY(type == nullptr)) {
Vladimir Marko942fd312017-01-16 20:52:19 +0000161 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000162 if (resolve) {
163 type = class_linker->ResolveType(type_idx, this);
164 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
165 } else {
166 type = class_linker->LookupResolvedType(
167 *dex_cache->GetDexFile(), type_idx, dex_cache, GetClassLoader());
168 }
Ian Rogersa0485602014-12-02 15:48:04 -0800169 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000170 return type.Ptr();
Ian Rogersa0485602014-12-02 15:48:04 -0800171}
172
Brian Carlstromea46f952013-07-30 01:26:50 -0700173inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 switch (type) {
175 case kStatic:
176 return !IsStatic();
177 case kDirect:
178 return !IsDirect() || IsStatic();
179 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700180 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
181 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700183 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 }
185 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700186 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700187 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700189 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
191 }
192 default:
193 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700194 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 }
196}
197
Ian Rogersef7d42f2014-01-06 12:55:46 -0800198inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 if (!IsRuntimeMethod()) {
200 return false;
201 }
202 Runtime* runtime = Runtime::Current();
203 bool result = false;
204 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
205 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
206 result = true;
207 break;
208 }
209 }
210 return result;
211}
212
Ian Rogersef7d42f2014-01-06 12:55:46 -0800213inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 bool result = this == Runtime::Current()->GetResolutionMethod();
215 // Check that if we do think it is phony it looks like the resolution method.
216 DCHECK(!result || IsRuntimeMethod());
217 return result;
218}
Jeff Hao88474b42013-10-23 16:24:40 -0700219
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700220inline bool ArtMethod::IsImtUnimplementedMethod() {
221 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
222 // Check that if we do think it is phony it looks like the imt unimplemented method.
223 DCHECK(!result || IsRuntimeMethod());
224 return result;
225}
226
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700227inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800228 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
229 // from-space dex file pointer it will be equal to the to-space copy.
230 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700231}
232
233inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700235 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
236 return "<runtime method>";
237 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 DCHECK(!IsProxyMethod());
239 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700240 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
241}
242
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800243inline const char* ArtMethod::GetShorty() {
244 uint32_t unused_length;
245 return GetShorty(&unused_length);
246}
247
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700248inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000249 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 const DexFile* dex_file = GetDexFile();
251 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252}
253
254inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700256 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700257 DCHECK(!IsProxyMethod());
258 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700259 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
260 }
261 return Signature::NoSignature();
262}
263
Ian Rogers1ff3c982014-08-12 02:30:58 -0700264inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700266 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 DCHECK(!IsProxyMethod());
268 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700269 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
270 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 Runtime* const runtime = Runtime::Current();
272 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700273 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700274 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700275 return "<runtime internal imt conflict method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100276 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700277 return "<runtime internal callee-save all registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100278 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279 return "<runtime internal callee-save reference registers method>";
Vladimir Markofd36f1f2016-08-03 18:49:58 +0100280 } else if (this == runtime->GetCalleeSaveMethod(Runtime::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700281 return "<runtime internal callee-save reference and argument registers method>";
282 } else {
283 return "<unknown runtime internal method>";
284 }
285}
286
287inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800288 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700289}
290
Vladimir Marko942fd312017-01-16 20:52:19 +0000291inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 DCHECK(!IsProxyMethod());
Vladimir Marko942fd312017-01-16 20:52:19 +0000293 return GetClassFromTypeIndex(type_idx, /* resolve */ false) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700294}
295
296inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700298 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700300 }
David Sehr9323e6e2016-09-13 08:58:35 -0700301 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700302}
303
304inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 DCHECK(!IsProxyMethod());
306 const DexFile* dex_file = GetDexFile();
307 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700308}
309
310inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000311 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000313 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
314 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700315 return dex_file->GetProtoParameters(proto);
316}
317
318inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 DCHECK(!IsProxyMethod());
320 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700321}
322
323inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700324 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000325 if (LIKELY(!IsObsolete())) {
326 return GetDeclaringClass()->GetDexClassDefIndex();
327 } else {
328 return FindObsoleteDexClassDefIndex();
329 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700330}
331
332inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK(!IsProxyMethod());
334 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700335}
336
337inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 DCHECK(!IsProxyMethod());
339 const DexFile* dex_file = GetDexFile();
340 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700341 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800342 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700343}
344
Alex Lightd7661582017-05-01 13:48:16 -0700345inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
346 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
347}
348
Andreas Gampea5b09a62016-11-17 15:21:22 -0800349inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 DCHECK(!IsProxyMethod());
351 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700352 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
353}
354
355inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356 DCHECK(!IsProxyMethod());
357 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700358}
359
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800360template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700361inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800362 if (LIKELY(!IsObsolete())) {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800363 mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
364 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800365 } else {
366 DCHECK(!IsProxyMethod());
367 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800368 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700369}
370
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700371inline bool ArtMethod::IsProxyMethod() {
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800372 // Avoid read barrier since the from-space version of the class will have the correct proxy class
373 // flags since they are constant for the lifetime of the class.
374 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700375}
376
Andreas Gampe542451c2016-07-26 09:02:02 -0700377inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700378 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700379 return this;
380 }
Vladimir Marko05792b92015-08-03 11:56:49 +0100381 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
382 GetDexCacheResolvedMethods(pointer_size),
383 GetDexMethodIndex(),
384 pointer_size);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700385 DCHECK(interface_method != nullptr);
386 DCHECK_EQ(interface_method,
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800387 Runtime::Current()->GetClassLinker()->FindMethodForProxy(GetDeclaringClass(), this));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700388 return interface_method;
389}
390
Vladimir Marko05792b92015-08-03 11:56:49 +0100391inline void ArtMethod::SetDexCacheResolvedMethods(ArtMethod** new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700392 PointerSize pointer_size) {
393 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
394 new_dex_cache_methods,
395 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700396}
397
Vladimir Marko942fd312017-01-16 20:52:19 +0000398inline mirror::Class* ArtMethod::GetReturnType(bool resolve) {
Ian Rogersded66a02014-10-28 18:12:55 -0700399 DCHECK(!IsProxyMethod());
400 const DexFile* dex_file = GetDexFile();
401 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
402 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800403 dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
Vladimir Marko942fd312017-01-16 20:52:19 +0000404 return GetClassFromTypeIndex(return_type_idx, resolve);
Ian Rogersded66a02014-10-28 18:12:55 -0700405}
406
Mingyao Yang063fc772016-08-02 11:02:54 -0700407inline bool ArtMethod::HasSingleImplementation() {
408 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
409 // We don't set kAccSingleImplementation for these cases since intrinsic
410 // can use the flag also.
411 return true;
412 }
413 return (GetAccessFlags() & kAccSingleImplementation) != 0;
414}
415
416inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
417 DCHECK(IsUint<8>(intrinsic));
418 // Currently we only do intrinsics for static/final methods or methods of final
419 // classes. We don't set kHasSingleImplementation for those methods.
420 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
421 "Potential conflict with kAccSingleImplementation";
422 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
423 kAccIntrinsic |
424 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
425 if (kIsDebugBuild) {
426 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
427 bool is_constructor = IsConstructor();
428 bool is_synchronized = IsSynchronized();
429 bool skip_access_checks = SkipAccessChecks();
430 bool is_fast_native = IsFastNative();
431 bool is_copied = IsCopied();
432 bool is_miranda = IsMiranda();
433 bool is_default = IsDefault();
434 bool is_default_conflict = IsDefaultConflicting();
435 bool is_compilable = IsCompilable();
436 bool must_count_locks = MustCountLocks();
437 SetAccessFlags(new_value);
438 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
439 DCHECK_EQ(is_constructor, IsConstructor());
440 DCHECK_EQ(is_synchronized, IsSynchronized());
441 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
442 DCHECK_EQ(is_fast_native, IsFastNative());
443 DCHECK_EQ(is_copied, IsCopied());
444 DCHECK_EQ(is_miranda, IsMiranda());
445 DCHECK_EQ(is_default, IsDefault());
446 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
447 DCHECK_EQ(is_compilable, IsCompilable());
448 DCHECK_EQ(must_count_locks, MustCountLocks());
449 } else {
450 SetAccessFlags(new_value);
451 }
452}
453
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700454template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700455void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700456 if (LIKELY(!declaring_class_.IsNull())) {
457 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
458 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000459 if (UNLIKELY(klass->IsProxyClass())) {
460 // For normal methods, dex cache shortcuts will be visited through the declaring class.
461 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700462 ArtMethod* interface_method = mirror::DexCache::GetElementPtrSize(
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000463 GetDexCacheResolvedMethods(pointer_size),
464 GetDexMethodIndex(),
465 pointer_size);
466 DCHECK(interface_method != nullptr);
467 DCHECK_EQ(interface_method,
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700468 Runtime::Current()->GetClassLinker()->FindMethodForProxy<kReadBarrierOption>(
469 klass, this));
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000470 interface_method->VisitRoots(visitor, pointer_size);
471 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100472 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800473}
474
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800475template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800476inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700477 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800478 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800479 mirror::Class* new_class = visitor(old_class);
480 if (old_class != new_class) {
481 SetDeclaringClass(new_class);
482 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800483 ArtMethod** old_methods = GetDexCacheResolvedMethods(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800484 ArtMethod** new_methods = visitor(old_methods);
485 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800486 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800487 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800488}
489
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800490template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700491inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800492 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800493 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800494 const void* new_native_code = visitor(old_native_code);
495 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800496 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800497 }
498 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700499 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800500 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800501 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800502 const void* new_code = visitor(old_code);
503 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800504 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800505 }
506}
507
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508} // namespace art
509
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510#endif // ART_RUNTIME_ART_METHOD_INL_H_