blob: 11f825353beae21b026fe00a49473307507af7cb [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"
Mathieu Chartier7c0fe5e2015-07-17 19:53:47 -070024#include "base/logging.h"
Hiroshi Yamauchi00370822015-08-18 14:47:25 -070025#include "class_linker-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010026#include "common_throws.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "dex_file.h"
David Sehr9323e6e2016-09-13 08:58:35 -070029#include "dex_file_annotations.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "gc_root-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070031#include "invoke_type.h"
Nicolas Geoffray5550ca82015-08-21 18:38:30 +010032#include "jit/profiling_info.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070033#include "mirror/class-inl.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010034#include "mirror/dex_cache-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070035#include "mirror/object-inl.h"
36#include "mirror/object_array.h"
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000037#include "mirror/string.h"
Vladimir Marko96c6ab92014-04-08 14:00:50 +010038#include "oat.h"
Mathieu Chartier28357fa2016-10-18 16:27:40 -070039#include "obj_ptr-inl.h"
Alex Lightd7661582017-05-01 13:48:16 -070040#include "primitive.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010041#include "quick/quick_method_frame_info.h"
Alex Lightd7661582017-05-01 13:48:16 -070042#include "read_barrier-inl.h"
Vladimir Marko7624d252014-05-02 14:40:15 +010043#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070044#include "scoped_thread_state_change-inl.h"
Andreas Gampeb486a982017-06-01 13:45:54 -070045#include "thread-current-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010046#include "utils.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047
48namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080050template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070051inline mirror::Class* ArtMethod::GetDeclaringClassUnchecked() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070052 GcRootSource gc_root_source(this);
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080053 return declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mingyao Yang98d1cc82014-05-15 17:02:16 -070054}
55
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080056template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartiere401d142015-04-22 13:56:20 -070057inline mirror::Class* ArtMethod::GetDeclaringClass() {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -080058 mirror::Class* result = GetDeclaringClassUnchecked<kReadBarrierOption>();
Mathieu Chartiere401d142015-04-22 13:56:20 -070059 if (kIsDebugBuild) {
60 if (!IsRuntimeMethod()) {
61 CHECK(result != nullptr) << this;
Andreas Gampeb1106e22017-02-23 11:34:48 -080062 if (kCheckDeclaringClassState) {
Nicolas Geoffray4c0b4bc2017-03-17 13:08:26 +000063 if (!(result->IsIdxLoaded() || result->IsErroneous())) {
64 LOG(FATAL_WITHOUT_ABORT) << "Class status: " << result->GetStatus();
65 LOG(FATAL) << result->PrettyClass();
66 }
Andreas Gampeb1106e22017-02-23 11:34:48 -080067 }
Mathieu Chartiere401d142015-04-22 13:56:20 -070068 } else {
69 CHECK(result == nullptr) << this;
70 }
71 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 return result;
73}
74
Mathieu Chartier28357fa2016-10-18 16:27:40 -070075inline void ArtMethod::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartiere401d142015-04-22 13:56:20 -070076 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077}
78
Mathieu Chartier10e5ea92015-08-13 12:56:31 -070079inline bool ArtMethod::CASDeclaringClass(mirror::Class* expected_class,
80 mirror::Class* desired_class) {
81 GcRoot<mirror::Class> expected_root(expected_class);
82 GcRoot<mirror::Class> desired_root(desired_class);
83 return reinterpret_cast<Atomic<GcRoot<mirror::Class>>*>(&declaring_class_)->
84 CompareExchangeStrongSequentiallyConsistent(
85 expected_root, desired_root);
86}
87
Ian Rogersef7d42f2014-01-06 12:55:46 -080088inline uint16_t ArtMethod::GetMethodIndex() {
Vladimir Marko72ab6842017-01-20 19:32:50 +000089 DCHECK(IsRuntimeMethod() || GetDeclaringClass()->IsResolved());
Mathieu Chartiere401d142015-04-22 13:56:20 -070090 return method_index_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091}
92
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070093inline uint16_t ArtMethod::GetMethodIndexDuringLinking() {
Mathieu Chartiere401d142015-04-22 13:56:20 -070094 return method_index_;
Mathieu Chartier9f3629d2014-10-28 18:23:02 -070095}
96
Ian Rogersef7d42f2014-01-06 12:55:46 -080097inline uint32_t ArtMethod::GetDexMethodIndex() {
Andreas Gampeb1106e22017-02-23 11:34:48 -080098 if (kCheckDeclaringClassState) {
99 CHECK(IsRuntimeMethod() || GetDeclaringClass()->IsIdxLoaded() ||
100 GetDeclaringClass()->IsErroneous());
101 }
Nicolas Geoffray5d37c152017-01-12 13:25:19 +0000102 return GetDexMethodIndexUnchecked();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800103}
104
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100105inline mirror::MethodDexCacheType* ArtMethod::GetDexCacheResolvedMethods(PointerSize pointer_size) {
106 return GetNativePointer<mirror::MethodDexCacheType*>(DexCacheResolvedMethodsOffset(pointer_size),
107 pointer_size);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700108}
109
Andreas Gampe542451c2016-07-26 09:02:02 -0700110inline ArtMethod* ArtMethod::GetDexCacheResolvedMethod(uint16_t method_index,
111 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100112 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
113 // without accessing the DexCache and we don't want to do that in release build.
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100114 DCHECK_LT(method_index, GetInterfaceMethodIfProxy(pointer_size)->GetDexFile()->NumMethodIds());
115 uint32_t slot_idx = method_index % mirror::DexCache::kDexCacheMethodCacheSize;
116 DCHECK_LT(slot_idx, GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
117 mirror::MethodDexCachePair pair = mirror::DexCache::GetNativePairPtrSize(
118 GetDexCacheResolvedMethods(pointer_size), slot_idx, pointer_size);
119 ArtMethod* method = pair.GetObjectForIndex(method_index);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700120 if (LIKELY(method != nullptr)) {
121 auto* declaring_class = method->GetDeclaringClass();
122 if (LIKELY(declaring_class == nullptr || !declaring_class->IsErroneous())) {
123 return method;
124 }
Andreas Gampe58a5af82014-07-31 16:23:49 -0700125 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 return nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700127}
128
Andreas Gampe542451c2016-07-26 09:02:02 -0700129inline void ArtMethod::SetDexCacheResolvedMethod(uint16_t method_index,
130 ArtMethod* new_method,
131 PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100132 // NOTE: Unchecked, i.e. not throwing AIOOB. We don't even know the length here
133 // without accessing the DexCache and we don't want to do that in release build.
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100134 DCHECK_LT(method_index, GetInterfaceMethodIfProxy(pointer_size)->GetDexFile()->NumMethodIds());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700135 DCHECK(new_method == nullptr || new_method->GetDeclaringClass() != nullptr);
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100136 uint32_t slot_idx = method_index % mirror::DexCache::kDexCacheMethodCacheSize;
137 DCHECK_LT(slot_idx, GetInterfaceMethodIfProxy(pointer_size)->GetDexCache()->NumResolvedMethods());
138 mirror::MethodDexCachePair pair(new_method, method_index);
139 mirror::DexCache::SetNativePairPtrSize(
140 GetDexCacheResolvedMethods(pointer_size), slot_idx, pair, pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700141}
142
Andreas Gampe542451c2016-07-26 09:02:02 -0700143inline bool ArtMethod::HasDexCacheResolvedMethods(PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100144 return GetDexCacheResolvedMethods(pointer_size) != nullptr;
Andreas Gampe58a5af82014-07-31 16:23:49 -0700145}
146
Andreas Gampe542451c2016-07-26 09:02:02 -0700147inline bool ArtMethod::HasSameDexCacheResolvedMethods(ArtMethod* other, PointerSize pointer_size) {
Vladimir Marko05792b92015-08-03 11:56:49 +0100148 return GetDexCacheResolvedMethods(pointer_size) ==
149 other->GetDexCacheResolvedMethods(pointer_size);
Andreas Gampe58a5af82014-07-31 16:23:49 -0700150}
151
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100152inline bool ArtMethod::HasSameDexCacheResolvedMethods(mirror::MethodDexCacheType* other_cache,
153 PointerSize pointer_size) {
154 return GetDexCacheResolvedMethods(pointer_size) == other_cache;
155}
156
Vladimir Markob45528c2017-07-27 14:14:28 +0100157inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedClassFromTypeIndex(dex::TypeIndex type_idx) {
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 Markob45528c2017-07-27 14:14:28 +0100161 type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
162 *dex_cache->GetDexFile(), type_idx, dex_cache, GetClassLoader());
163 }
164 return type.Ptr();
165}
166
167inline ObjPtr<mirror::Class> ArtMethod::ResolveClassFromTypeIndex(dex::TypeIndex type_idx) {
168 ObjPtr<mirror::DexCache> dex_cache = GetDexCache();
169 ObjPtr<mirror::Class> type = dex_cache->GetResolvedType(type_idx);
170 if (UNLIKELY(type == nullptr)) {
171 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, this);
172 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersa0485602014-12-02 15:48:04 -0800173 }
Vladimir Marko942fd312017-01-16 20:52:19 +0000174 return type.Ptr();
Ian Rogersa0485602014-12-02 15:48:04 -0800175}
176
Brian Carlstromea46f952013-07-30 01:26:50 -0700177inline bool ArtMethod::CheckIncompatibleClassChange(InvokeType type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 switch (type) {
179 case kStatic:
180 return !IsStatic();
181 case kDirect:
182 return !IsDirect() || IsStatic();
183 case kVirtual: {
Alex Lightd6e0fa92016-10-17 13:02:39 -0700184 // We have an error if we are direct or a non-copied (i.e. not part of a real class) interface
185 // method.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 mirror::Class* methods_class = GetDeclaringClass();
Alex Lightd6e0fa92016-10-17 13:02:39 -0700187 return IsDirect() || (methods_class->IsInterface() && !IsCopied());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 }
189 case kSuper:
Andreas Gampe8f252e62014-08-25 20:46:31 -0700190 // Constructors and static methods are called with invoke-direct.
Alex Light705ad492015-09-21 11:36:30 -0700191 return IsConstructor() || IsStatic();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 case kInterface: {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700193 mirror::Class* methods_class = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 return IsDirect() || !(methods_class->IsInterface() || methods_class->IsObjectClass());
195 }
196 default:
197 LOG(FATAL) << "Unreachable - invocation type: " << type;
Ian Rogers2c4257b2014-10-24 14:20:06 -0700198 UNREACHABLE();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 }
200}
201
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202inline bool ArtMethod::IsCalleeSaveMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 if (!IsRuntimeMethod()) {
204 return false;
205 }
206 Runtime* runtime = Runtime::Current();
207 bool result = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700208 for (uint32_t i = 0; i < static_cast<uint32_t>(CalleeSaveType::kLastCalleeSaveType); i++) {
209 if (this == runtime->GetCalleeSaveMethod(CalleeSaveType(i))) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 result = true;
211 break;
212 }
213 }
214 return result;
215}
216
Ian Rogersef7d42f2014-01-06 12:55:46 -0800217inline bool ArtMethod::IsResolutionMethod() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 bool result = this == Runtime::Current()->GetResolutionMethod();
219 // Check that if we do think it is phony it looks like the resolution method.
220 DCHECK(!result || IsRuntimeMethod());
221 return result;
222}
Jeff Hao88474b42013-10-23 16:24:40 -0700223
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700224inline bool ArtMethod::IsImtUnimplementedMethod() {
225 bool result = this == Runtime::Current()->GetImtUnimplementedMethod();
226 // Check that if we do think it is phony it looks like the imt unimplemented method.
227 DCHECK(!result || IsRuntimeMethod());
228 return result;
229}
230
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700231inline const DexFile* ArtMethod::GetDexFile() {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800232 // It is safe to avoid the read barrier here since the dex file is constant, so if we read the
233 // from-space dex file pointer it will be equal to the to-space copy.
234 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700235}
236
237inline const char* ArtMethod::GetDeclaringClassDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700239 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
240 return "<runtime method>";
241 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700242 DCHECK(!IsProxyMethod());
243 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700244 return dex_file->GetMethodDeclaringClassDescriptor(dex_file->GetMethodId(dex_method_idx));
245}
246
Andreas Gampec6ea7d02017-02-01 16:46:28 -0800247inline const char* ArtMethod::GetShorty() {
248 uint32_t unused_length;
249 return GetShorty(&unused_length);
250}
251
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700252inline const char* ArtMethod::GetShorty(uint32_t* out_length) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000253 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 const DexFile* dex_file = GetDexFile();
255 return dex_file->GetMethodShorty(dex_file->GetMethodId(GetDexMethodIndex()), out_length);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700256}
257
258inline const Signature ArtMethod::GetSignature() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700260 if (dex_method_idx != DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 DCHECK(!IsProxyMethod());
262 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700263 return dex_file->GetMethodSignature(dex_file->GetMethodId(dex_method_idx));
264 }
265 return Signature::NoSignature();
266}
267
Ian Rogers1ff3c982014-08-12 02:30:58 -0700268inline const char* ArtMethod::GetName() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 uint32_t dex_method_idx = GetDexMethodIndex();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700270 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 DCHECK(!IsProxyMethod());
272 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700273 return dex_file->GetMethodName(dex_file->GetMethodId(dex_method_idx));
274 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700275 Runtime* const runtime = Runtime::Current();
276 if (this == runtime->GetResolutionMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700277 return "<runtime internal resolution method>";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700278 } else if (this == runtime->GetImtConflictMethod()) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279 return "<runtime internal imt conflict method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700280 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveAllCalleeSaves)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700281 return "<runtime internal callee-save all registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700282 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsOnly)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283 return "<runtime internal callee-save reference registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700284 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs)) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700285 return "<runtime internal callee-save reference and argument registers method>";
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700286 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverything)) {
287 return "<runtime internal save-every-register method>";
Mingyao Yang0a87a652017-04-12 13:43:15 -0700288 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForClinit)) {
289 return "<runtime internal save-every-register method for clinit>";
290 } else if (this == runtime->GetCalleeSaveMethod(CalleeSaveType::kSaveEverythingForSuspendCheck)) {
291 return "<runtime internal save-every-register method for suspend check>";
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700292 } else {
293 return "<unknown runtime internal method>";
294 }
295}
296
297inline const DexFile::CodeItem* ArtMethod::GetCodeItem() {
Alex Lightdba61482016-12-21 08:20:29 -0800298 return GetDexFile()->GetCodeItem(GetCodeItemOffset());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700299}
300
Vladimir Marko942fd312017-01-16 20:52:19 +0000301inline bool ArtMethod::IsResolvedTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 DCHECK(!IsProxyMethod());
Vladimir Markob45528c2017-07-27 14:14:28 +0100303 return LookupResolvedClassFromTypeIndex(type_idx) != nullptr;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700304}
305
306inline int32_t ArtMethod::GetLineNumFromDexPC(uint32_t dex_pc) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 DCHECK(!IsProxyMethod());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700308 if (dex_pc == DexFile::kDexNoIndex) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 return IsNative() ? -2 : -1;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700310 }
David Sehr9323e6e2016-09-13 08:58:35 -0700311 return annotations::GetLineNumFromPC(GetDexFile(), this, dex_pc);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700312}
313
314inline const DexFile::ProtoId& ArtMethod::GetPrototype() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 DCHECK(!IsProxyMethod());
316 const DexFile* dex_file = GetDexFile();
317 return dex_file->GetMethodPrototype(dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700318}
319
320inline const DexFile::TypeList* ArtMethod::GetParameterTypeList() {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000321 DCHECK(!IsProxyMethod());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 const DexFile* dex_file = GetDexFile();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000323 const DexFile::ProtoId& proto = dex_file->GetMethodPrototype(
324 dex_file->GetMethodId(GetDexMethodIndex()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700325 return dex_file->GetProtoParameters(proto);
326}
327
328inline const char* ArtMethod::GetDeclaringClassSourceFile() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 DCHECK(!IsProxyMethod());
330 return GetDeclaringClass()->GetSourceFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700331}
332
333inline uint16_t ArtMethod::GetClassDefIndex() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700334 DCHECK(!IsProxyMethod());
Alex Lightf2f1c9d2017-03-15 15:35:46 +0000335 if (LIKELY(!IsObsolete())) {
336 return GetDeclaringClass()->GetDexClassDefIndex();
337 } else {
338 return FindObsoleteDexClassDefIndex();
339 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340}
341
342inline const DexFile::ClassDef& ArtMethod::GetClassDef() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 DCHECK(!IsProxyMethod());
344 return GetDexFile()->GetClassDef(GetClassDefIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345}
346
347inline const char* ArtMethod::GetReturnTypeDescriptor() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 DCHECK(!IsProxyMethod());
349 const DexFile* dex_file = GetDexFile();
350 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700351 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Andreas Gampea5b09a62016-11-17 15:21:22 -0800352 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(proto_id.return_type_idx_));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700353}
354
Alex Lightd7661582017-05-01 13:48:16 -0700355inline Primitive::Type ArtMethod::GetReturnTypePrimitive() {
356 return Primitive::GetType(GetReturnTypeDescriptor()[0]);
357}
358
Andreas Gampea5b09a62016-11-17 15:21:22 -0800359inline const char* ArtMethod::GetTypeDescriptorFromTypeIdx(dex::TypeIndex type_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 DCHECK(!IsProxyMethod());
361 const DexFile* dex_file = GetDexFile();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700362 return dex_file->GetTypeDescriptor(dex_file->GetTypeId(type_idx));
363}
364
365inline mirror::ClassLoader* ArtMethod::GetClassLoader() {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 DCHECK(!IsProxyMethod());
367 return GetDeclaringClass()->GetClassLoader();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368}
369
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800370template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371inline mirror::DexCache* ArtMethod::GetDexCache() {
Alex Lightdba61482016-12-21 08:20:29 -0800372 if (LIKELY(!IsObsolete())) {
Mathieu Chartier137cdfa2017-01-26 14:03:11 -0800373 mirror::Class* klass = GetDeclaringClass<kReadBarrierOption>();
374 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Alex Lightdba61482016-12-21 08:20:29 -0800375 } else {
376 DCHECK(!IsProxyMethod());
377 return GetObsoleteDexCache();
Alex Lighta01de592016-11-15 10:43:06 -0800378 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700379}
380
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700381inline bool ArtMethod::IsProxyMethod() {
Mathieu Chartier90c5a9b2017-02-01 13:10:06 -0800382 // Avoid read barrier since the from-space version of the class will have the correct proxy class
383 // flags since they are constant for the lifetime of the class.
384 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass();
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700385}
386
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000387inline ArtMethod* ArtMethod::GetInterfaceMethodForProxyUnchecked(PointerSize pointer_size) {
388 DCHECK(IsProxyMethod());
389 // Do not check IsAssignableFrom() here as it relies on raw reference comparison
390 // which may give false negatives while visiting references for a non-CC moving GC.
391 return reinterpret_cast<ArtMethod*>(GetDataPtrSize(pointer_size));
392}
393
Andreas Gampe542451c2016-07-26 09:02:02 -0700394inline ArtMethod* ArtMethod::GetInterfaceMethodIfProxy(PointerSize pointer_size) {
Ian Rogers03b6eaf2014-10-28 09:34:57 -0700395 if (LIKELY(!IsProxyMethod())) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700396 return this;
397 }
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000398 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
399 // We can check that the proxy class implements the interface only if the proxy class
400 // is resolved, otherwise the interface table is not yet initialized.
401 DCHECK(!GetDeclaringClass()->IsResolved() ||
402 interface_method->GetDeclaringClass()->IsAssignableFrom(GetDeclaringClass()));
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700403 return interface_method;
404}
405
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100406inline void ArtMethod::SetDexCacheResolvedMethods(mirror::MethodDexCacheType* new_dex_cache_methods,
Andreas Gampe542451c2016-07-26 09:02:02 -0700407 PointerSize pointer_size) {
408 SetNativePointer(DexCacheResolvedMethodsOffset(pointer_size),
409 new_dex_cache_methods,
410 pointer_size);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700411}
412
Vladimir Markob45528c2017-07-27 14:14:28 +0100413inline dex::TypeIndex ArtMethod::GetReturnTypeIndex() {
Ian Rogersded66a02014-10-28 18:12:55 -0700414 DCHECK(!IsProxyMethod());
415 const DexFile* dex_file = GetDexFile();
416 const DexFile::MethodId& method_id = dex_file->GetMethodId(GetDexMethodIndex());
417 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Vladimir Markob45528c2017-07-27 14:14:28 +0100418 return proto_id.return_type_idx_;
419}
420
421inline ObjPtr<mirror::Class> ArtMethod::LookupResolvedReturnType() {
422 return LookupResolvedClassFromTypeIndex(GetReturnTypeIndex());
423}
424
425inline ObjPtr<mirror::Class> ArtMethod::ResolveReturnType() {
426 return ResolveClassFromTypeIndex(GetReturnTypeIndex());
Ian Rogersded66a02014-10-28 18:12:55 -0700427}
428
Mingyao Yang063fc772016-08-02 11:02:54 -0700429inline bool ArtMethod::HasSingleImplementation() {
430 if (IsFinal() || GetDeclaringClass()->IsFinal()) {
431 // We don't set kAccSingleImplementation for these cases since intrinsic
432 // can use the flag also.
433 return true;
434 }
435 return (GetAccessFlags() & kAccSingleImplementation) != 0;
436}
437
438inline void ArtMethod::SetIntrinsic(uint32_t intrinsic) {
439 DCHECK(IsUint<8>(intrinsic));
440 // Currently we only do intrinsics for static/final methods or methods of final
441 // classes. We don't set kHasSingleImplementation for those methods.
442 DCHECK(IsStatic() || IsFinal() || GetDeclaringClass()->IsFinal()) <<
443 "Potential conflict with kAccSingleImplementation";
444 uint32_t new_value = (GetAccessFlags() & kAccFlagsNotUsedByIntrinsic) |
445 kAccIntrinsic |
446 (intrinsic << POPCOUNT(kAccFlagsNotUsedByIntrinsic));
447 if (kIsDebugBuild) {
448 uint32_t java_flags = (GetAccessFlags() & kAccJavaFlagsMask);
449 bool is_constructor = IsConstructor();
450 bool is_synchronized = IsSynchronized();
451 bool skip_access_checks = SkipAccessChecks();
452 bool is_fast_native = IsFastNative();
453 bool is_copied = IsCopied();
454 bool is_miranda = IsMiranda();
455 bool is_default = IsDefault();
456 bool is_default_conflict = IsDefaultConflicting();
457 bool is_compilable = IsCompilable();
458 bool must_count_locks = MustCountLocks();
459 SetAccessFlags(new_value);
460 DCHECK_EQ(java_flags, (GetAccessFlags() & kAccJavaFlagsMask));
461 DCHECK_EQ(is_constructor, IsConstructor());
462 DCHECK_EQ(is_synchronized, IsSynchronized());
463 DCHECK_EQ(skip_access_checks, SkipAccessChecks());
464 DCHECK_EQ(is_fast_native, IsFastNative());
465 DCHECK_EQ(is_copied, IsCopied());
466 DCHECK_EQ(is_miranda, IsMiranda());
467 DCHECK_EQ(is_default, IsDefault());
468 DCHECK_EQ(is_default_conflict, IsDefaultConflicting());
469 DCHECK_EQ(is_compilable, IsCompilable());
470 DCHECK_EQ(must_count_locks, MustCountLocks());
471 } else {
472 SetAccessFlags(new_value);
473 }
474}
475
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700476template<ReadBarrierOption kReadBarrierOption, typename RootVisitorType>
Andreas Gampe542451c2016-07-26 09:02:02 -0700477void ArtMethod::VisitRoots(RootVisitorType& visitor, PointerSize pointer_size) {
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -0700478 if (LIKELY(!declaring_class_.IsNull())) {
479 visitor.VisitRoot(declaring_class_.AddressWithoutBarrier());
480 mirror::Class* klass = declaring_class_.Read<kReadBarrierOption>();
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000481 if (UNLIKELY(klass->IsProxyClass())) {
482 // For normal methods, dex cache shortcuts will be visited through the declaring class.
483 // However, for proxies we need to keep the interface method alive, so we visit its roots.
Vladimir Markod1ee20f2017-08-17 09:21:16 +0000484 ArtMethod* interface_method = GetInterfaceMethodForProxyUnchecked(pointer_size);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000485 DCHECK(interface_method != nullptr);
Nicolas Geoffraydec3a122016-02-13 12:38:36 +0000486 interface_method->VisitRoots(visitor, pointer_size);
487 }
Nicolas Geoffray5550ca82015-08-21 18:38:30 +0100488 }
Mathieu Chartier2d721012014-11-10 11:08:06 -0800489}
490
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800491template <typename Visitor>
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800492inline void ArtMethod::UpdateObjectsForImageRelocation(const Visitor& visitor,
Andreas Gampe542451c2016-07-26 09:02:02 -0700493 PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800494 mirror::Class* old_class = GetDeclaringClassUnchecked<kWithoutReadBarrier>();
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800495 mirror::Class* new_class = visitor(old_class);
496 if (old_class != new_class) {
497 SetDeclaringClass(new_class);
498 }
Vladimir Marko07bfbac2017-07-06 14:55:02 +0100499 mirror::MethodDexCacheType* old_methods = GetDexCacheResolvedMethods(pointer_size);
500 mirror::MethodDexCacheType* new_methods = visitor(old_methods);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800501 if (old_methods != new_methods) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800502 SetDexCacheResolvedMethods(new_methods, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800503 }
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800504}
505
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800506template <ReadBarrierOption kReadBarrierOption, typename Visitor>
Andreas Gampe542451c2016-07-26 09:02:02 -0700507inline void ArtMethod::UpdateEntrypoints(const Visitor& visitor, PointerSize pointer_size) {
Mathieu Chartiere7f75f32016-02-01 16:08:15 -0800508 if (IsNative<kReadBarrierOption>()) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800509 const void* old_native_code = GetEntryPointFromJniPtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800510 const void* new_native_code = visitor(old_native_code);
511 if (old_native_code != new_native_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800512 SetEntryPointFromJniPtrSize(new_native_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800513 }
514 } else {
Andreas Gampe75f08852016-07-19 08:06:07 -0700515 DCHECK(GetDataPtrSize(pointer_size) == nullptr);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800516 }
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800517 const void* old_code = GetEntryPointFromQuickCompiledCodePtrSize(pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800518 const void* new_code = visitor(old_code);
519 if (old_code != new_code) {
Mathieu Chartier0b4cbd02016-03-08 16:49:58 -0800520 SetEntryPointFromQuickCompiledCodePtrSize(new_code, pointer_size);
Mathieu Chartierfbc31082016-01-24 11:59:56 -0800521 }
522}
523
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524} // namespace art
525
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526#endif // ART_RUNTIME_ART_METHOD_INL_H_