blob: 41808dd137909cff23eac1ae51fc3bbcbd120af0 [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 Chartierc7853442015-03-27 14:35:38 -070017#ifndef ART_RUNTIME_ART_FIELD_INL_H_
18#define ART_RUNTIME_ART_FIELD_INL_H_
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019
Brian Carlstromea46f952013-07-30 01:26:50 -070020#include "art_field.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021
Andreas Gampe57943812017-12-06 21:39:13 -080022#include <android-base/logging.h>
23
Vladimir Marko18090d12018-06-01 16:53:12 +010024#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080025#include "dex/dex_file-inl.h"
David Sehr67bf42e2018-02-26 16:43:04 -080026#include "dex/primitive.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include "gc_root-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080029#include "jvalue.h"
Vladimir Marko05792b92015-08-03 11:56:49 +010030#include "mirror/dex_cache-inl.h"
Mathieu Chartierc7853442015-03-27 14:35:38 -070031#include "mirror/object-inl.h"
Vladimir Markoc524e9e2019-03-26 10:54:50 +000032#include "obj_ptr-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070033#include "thread-current-inl.h"
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080034
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036
Vladimir Marko09c5ca42018-05-31 15:15:31 +010037inline bool ArtField::IsProxyField() {
Vladimir Marko415ac852019-03-21 12:49:10 +000038 // No read barrier needed, we're reading the constant declaring class only to read
39 // the constant proxy flag. See ReadBarrierOption.
Vladimir Marko09c5ca42018-05-31 15:15:31 +010040 return GetDeclaringClass<kWithoutReadBarrier>()->IsProxyClass<kVerifyNone>();
41}
42
Hiroshi Yamauchi7a62e672016-06-10 17:22:48 -070043template<ReadBarrierOption kReadBarrierOption>
Mathieu Chartier3398c782016-09-30 10:27:43 -070044inline ObjPtr<mirror::Class> ArtField::GetDeclaringClass() {
Hiroshi Yamauchi3f64f252015-06-12 18:35:06 -070045 GcRootSource gc_root_source(this);
Mathieu Chartier3398c782016-09-30 10:27:43 -070046 ObjPtr<mirror::Class> result = declaring_class_.Read<kReadBarrierOption>(&gc_root_source);
Mathieu Chartierc7853442015-03-27 14:35:38 -070047 DCHECK(result != nullptr);
Vladimir Marko09c5ca42018-05-31 15:15:31 +010048 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << result->GetStatus();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 return result;
50}
51
Mathieu Chartier3398c782016-09-30 10:27:43 -070052inline void ArtField::SetDeclaringClass(ObjPtr<mirror::Class> new_declaring_class) {
Mathieu Chartierc7853442015-03-27 14:35:38 -070053 declaring_class_ = GcRoot<mirror::Class>(new_declaring_class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
Ian Rogersef7d42f2014-01-06 12:55:46 -080056inline MemberOffset ArtField::GetOffsetDuringLinking() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Mathieu Chartierc7853442015-03-27 14:35:38 -070058 return MemberOffset(offset_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059}
60
Mathieu Chartier3398c782016-09-30 10:27:43 -070061inline uint32_t ArtField::Get32(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -070062 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080063 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070064 if (UNLIKELY(IsVolatile())) {
65 return object->GetField32Volatile(GetOffset());
66 }
67 return object->GetField32(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080068}
69
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010070template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -070071inline void ArtField::Set32(ObjPtr<mirror::Object> object, uint32_t new_value) {
David Sehr709b0702016-10-13 09:12:37 -070072 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080073 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070074 if (UNLIKELY(IsVolatile())) {
75 object->SetField32Volatile<kTransactionActive>(GetOffset(), new_value);
76 } else {
77 object->SetField32<kTransactionActive>(GetOffset(), new_value);
78 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080079}
80
Mathieu Chartier3398c782016-09-30 10:27:43 -070081inline uint64_t ArtField::Get64(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -070082 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080083 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070084 if (UNLIKELY(IsVolatile())) {
85 return object->GetField64Volatile(GetOffset());
86 }
87 return object->GetField64(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080088}
89
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010090template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -070091inline void ArtField::Set64(ObjPtr<mirror::Object> object, uint64_t new_value) {
David Sehr709b0702016-10-13 09:12:37 -070092 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080093 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -070094 if (UNLIKELY(IsVolatile())) {
95 object->SetField64Volatile<kTransactionActive>(GetOffset(), new_value);
96 } else {
97 object->SetField64<kTransactionActive>(GetOffset(), new_value);
98 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -080099}
100
Mathieu Chartier3398c782016-09-30 10:27:43 -0700101template<class MirrorType>
102inline ObjPtr<MirrorType> ArtField::GetObj(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -0700103 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800104 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700105 if (UNLIKELY(IsVolatile())) {
Mathieu Chartier3398c782016-09-30 10:27:43 -0700106 return object->GetFieldObjectVolatile<MirrorType>(GetOffset());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700107 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700108 return object->GetFieldObject<MirrorType>(GetOffset());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800109}
110
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700112inline void ArtField::SetObj(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> new_value) {
David Sehr709b0702016-10-13 09:12:37 -0700113 DCHECK(object != nullptr) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800114 DCHECK(!IsStatic() || (object == GetDeclaringClass()) || !Runtime::Current()->IsStarted());
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700115 if (UNLIKELY(IsVolatile())) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700116 object->SetFieldObjectVolatile<kTransactionActive>(GetOffset(), new_value);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700117 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700118 object->SetFieldObject<kTransactionActive>(GetOffset(), new_value);
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800120}
121
Fred Shih37f05ef2014-07-16 18:38:08 -0700122#define FIELD_GET(object, type) \
David Sehr709b0702016-10-13 09:12:37 -0700123 DCHECK_EQ(Primitive::kPrim ## type, GetTypeAsPrimitiveType()) << PrettyField(); \
124 DCHECK((object) != nullptr) << PrettyField(); \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700125 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
Fred Shih37f05ef2014-07-16 18:38:08 -0700126 if (UNLIKELY(IsVolatile())) { \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700127 return (object)->GetField ## type ## Volatile(GetOffset()); \
Fred Shih37f05ef2014-07-16 18:38:08 -0700128 } \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700129 return (object)->GetField ## type(GetOffset());
Fred Shih37f05ef2014-07-16 18:38:08 -0700130
131#define FIELD_SET(object, type, value) \
David Sehr709b0702016-10-13 09:12:37 -0700132 DCHECK((object) != nullptr) << PrettyField(); \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700133 DCHECK(!IsStatic() || ((object) == GetDeclaringClass()) || !Runtime::Current()->IsStarted()); \
Fred Shih37f05ef2014-07-16 18:38:08 -0700134 if (UNLIKELY(IsVolatile())) { \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700135 (object)->SetField ## type ## Volatile<kTransactionActive>(GetOffset(), value); \
Fred Shih37f05ef2014-07-16 18:38:08 -0700136 } else { \
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700137 (object)->SetField ## type<kTransactionActive>(GetOffset(), value); \
Fred Shih37f05ef2014-07-16 18:38:08 -0700138 }
139
Mathieu Chartier3398c782016-09-30 10:27:43 -0700140inline uint8_t ArtField::GetBoolean(ObjPtr<mirror::Object> object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700141 FIELD_GET(object, Boolean);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800142}
143
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100144template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700145inline void ArtField::SetBoolean(ObjPtr<mirror::Object> object, uint8_t z) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000146 if (kIsDebugBuild) {
147 // For simplicity, this method is being called by the compiler entrypoint for
148 // both boolean and byte fields.
149 Primitive::Type type = GetTypeAsPrimitiveType();
150 DCHECK(type == Primitive::kPrimBoolean || type == Primitive::kPrimByte) << PrettyField();
151 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700152 FIELD_SET(object, Boolean, z);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800153}
154
Mathieu Chartier3398c782016-09-30 10:27:43 -0700155inline int8_t ArtField::GetByte(ObjPtr<mirror::Object> object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700156 FIELD_GET(object, Byte);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800157}
158
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700160inline void ArtField::SetByte(ObjPtr<mirror::Object> object, int8_t b) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000161 DCHECK_EQ(Primitive::kPrimByte, GetTypeAsPrimitiveType()) << PrettyField();
Fred Shih37f05ef2014-07-16 18:38:08 -0700162 FIELD_SET(object, Byte, b);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800163}
164
Mathieu Chartier3398c782016-09-30 10:27:43 -0700165inline uint16_t ArtField::GetChar(ObjPtr<mirror::Object> object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700166 FIELD_GET(object, Char);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800167}
168
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700170inline void ArtField::SetChar(ObjPtr<mirror::Object> object, uint16_t c) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000171 if (kIsDebugBuild) {
172 // For simplicity, this method is being called by the compiler entrypoint for
173 // both char and short fields.
174 Primitive::Type type = GetTypeAsPrimitiveType();
175 DCHECK(type == Primitive::kPrimChar || type == Primitive::kPrimShort) << PrettyField();
176 }
Fred Shih37f05ef2014-07-16 18:38:08 -0700177 FIELD_SET(object, Char, c);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800178}
179
Mathieu Chartier3398c782016-09-30 10:27:43 -0700180inline int16_t ArtField::GetShort(ObjPtr<mirror::Object> object) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700181 FIELD_GET(object, Short);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800182}
183
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100184template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700185inline void ArtField::SetShort(ObjPtr<mirror::Object> object, int16_t s) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000186 DCHECK_EQ(Primitive::kPrimShort, GetTypeAsPrimitiveType()) << PrettyField();
Fred Shih37f05ef2014-07-16 18:38:08 -0700187 FIELD_SET(object, Short, s);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800188}
189
Fred Shih37f05ef2014-07-16 18:38:08 -0700190#undef FIELD_GET
191#undef FIELD_SET
192
Mathieu Chartier3398c782016-09-30 10:27:43 -0700193inline int32_t ArtField::GetInt(ObjPtr<mirror::Object> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100194 if (kIsDebugBuild) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000195 // For simplicity, this method is being called by the compiler entrypoint for
196 // both int and float fields.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700197 Primitive::Type type = GetTypeAsPrimitiveType();
David Sehr709b0702016-10-13 09:12:37 -0700198 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100199 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800200 return Get32(object);
201}
202
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100203template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700204inline void ArtField::SetInt(ObjPtr<mirror::Object> object, int32_t i) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100205 if (kIsDebugBuild) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000206 // For simplicity, this method is being called by the compiler entrypoint for
207 // both int and float fields.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700208 Primitive::Type type = GetTypeAsPrimitiveType();
David Sehr709b0702016-10-13 09:12:37 -0700209 CHECK(type == Primitive::kPrimInt || type == Primitive::kPrimFloat) << PrettyField();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100210 }
211 Set32<kTransactionActive>(object, i);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800212}
213
Mathieu Chartier3398c782016-09-30 10:27:43 -0700214inline int64_t ArtField::GetLong(ObjPtr<mirror::Object> object) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100215 if (kIsDebugBuild) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000216 // For simplicity, this method is being called by the compiler entrypoint for
217 // both long and double fields.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700218 Primitive::Type type = GetTypeAsPrimitiveType();
David Sehr709b0702016-10-13 09:12:37 -0700219 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220 }
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800221 return Get64(object);
222}
223
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100224template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700225inline void ArtField::SetLong(ObjPtr<mirror::Object> object, int64_t j) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100226 if (kIsDebugBuild) {
Nicolas Geoffray5b3c6c02017-01-19 14:22:26 +0000227 // For simplicity, this method is being called by the compiler entrypoint for
228 // both long and double fields.
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700229 Primitive::Type type = GetTypeAsPrimitiveType();
David Sehr709b0702016-10-13 09:12:37 -0700230 CHECK(type == Primitive::kPrimLong || type == Primitive::kPrimDouble) << PrettyField();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100231 }
232 Set64<kTransactionActive>(object, j);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800233}
234
Mathieu Chartier3398c782016-09-30 10:27:43 -0700235inline float ArtField::GetFloat(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -0700236 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800237 JValue bits;
238 bits.SetI(Get32(object));
239 return bits.GetF();
240}
241
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100242template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700243inline void ArtField::SetFloat(ObjPtr<mirror::Object> object, float f) {
David Sehr709b0702016-10-13 09:12:37 -0700244 DCHECK_EQ(Primitive::kPrimFloat, GetTypeAsPrimitiveType()) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800245 JValue bits;
246 bits.SetF(f);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100247 Set32<kTransactionActive>(object, bits.GetI());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800248}
249
Mathieu Chartier3398c782016-09-30 10:27:43 -0700250inline double ArtField::GetDouble(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -0700251 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800252 JValue bits;
253 bits.SetJ(Get64(object));
254 return bits.GetD();
255}
256
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100257template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700258inline void ArtField::SetDouble(ObjPtr<mirror::Object> object, double d) {
David Sehr709b0702016-10-13 09:12:37 -0700259 DCHECK_EQ(Primitive::kPrimDouble, GetTypeAsPrimitiveType()) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800260 JValue bits;
261 bits.SetD(d);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100262 Set64<kTransactionActive>(object, bits.GetJ());
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800263}
264
Mathieu Chartier3398c782016-09-30 10:27:43 -0700265inline ObjPtr<mirror::Object> ArtField::GetObject(ObjPtr<mirror::Object> object) {
David Sehr709b0702016-10-13 09:12:37 -0700266 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800267 return GetObj(object);
268}
269
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100270template<bool kTransactionActive>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700271inline void ArtField::SetObject(ObjPtr<mirror::Object> object, ObjPtr<mirror::Object> l) {
David Sehr709b0702016-10-13 09:12:37 -0700272 DCHECK_EQ(Primitive::kPrimNot, GetTypeAsPrimitiveType()) << PrettyField();
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273 SetObj<kTransactionActive>(object, l);
Ian Rogersc0fa3ad2013-02-05 00:11:55 -0800274}
275
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700276inline const char* ArtField::GetName() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700277 uint32_t field_index = GetDexFieldIndex();
Vladimir Marko415ac852019-03-21 12:49:10 +0000278 if (UNLIKELY(IsProxyField())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700279 DCHECK(IsStatic());
280 DCHECK_LT(field_index, 2U);
281 return field_index == 0 ? "interfaces" : "throws";
282 }
283 const DexFile* dex_file = GetDexFile();
284 return dex_file->GetFieldName(dex_file->GetFieldId(field_index));
285}
286
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700287inline const char* ArtField::GetTypeDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700288 uint32_t field_index = GetDexFieldIndex();
Vladimir Marko415ac852019-03-21 12:49:10 +0000289 if (UNLIKELY(IsProxyField())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700290 DCHECK(IsStatic());
291 DCHECK_LT(field_index, 2U);
292 // 0 == Class[] interfaces; 1 == Class[][] throws;
293 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
294 }
295 const DexFile* dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800296 const dex::FieldId& field_id = dex_file->GetFieldId(field_index);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700297 return dex_file->GetFieldTypeDescriptor(field_id);
298}
299
300inline Primitive::Type ArtField::GetTypeAsPrimitiveType()
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700301 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700302 return Primitive::GetType(GetTypeDescriptor()[0]);
303}
304
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700305inline bool ArtField::IsPrimitiveType() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700306 return GetTypeAsPrimitiveType() != Primitive::kPrimNot;
307}
308
Vladimir Marko208f6702017-12-08 12:00:50 +0000309inline ObjPtr<mirror::Class> ArtField::LookupResolvedType() {
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000310 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100311 if (UNLIKELY(IsProxyField())) {
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000312 return ProxyFindSystemClass(GetTypeDescriptor());
313 }
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000314 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100315 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000316 DCHECK(!Thread::Current()->IsExceptionPending());
317 return type;
Vladimir Marko4098a7a2017-11-06 16:00:51 +0000318}
319
320inline ObjPtr<mirror::Class> ArtField::ResolveType() {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100321 if (UNLIKELY(IsProxyField())) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000322 return ProxyFindSystemClass(GetTypeDescriptor());
Ian Rogers08f1f502014-12-02 15:04:37 -0800323 }
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000324 ObjPtr<mirror::Class> type = Runtime::Current()->GetClassLinker()->ResolveType(
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100325 GetDexFile()->GetFieldId(GetDexFieldIndex()).type_idx_, this);
Vladimir Marko666ee3d2017-12-11 18:37:36 +0000326 DCHECK_EQ(type == nullptr, Thread::Current()->IsExceptionPending());
Ian Rogers08f1f502014-12-02 15:04:37 -0800327 return type;
328}
329
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700330inline size_t ArtField::FieldSize() REQUIRES_SHARED(Locks::mutator_lock_) {
Fred Shih37f05ef2014-07-16 18:38:08 -0700331 return Primitive::ComponentSize(GetTypeAsPrimitiveType());
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700332}
333
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100334template <ReadBarrierOption kReadBarrierOption>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700335inline ObjPtr<mirror::DexCache> ArtField::GetDexCache() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100336 ObjPtr<mirror::Class> klass = GetDeclaringClass<kReadBarrierOption>();
337 return klass->GetDexCache<kDefaultVerifyFlags, kReadBarrierOption>();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700338}
339
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700340inline const DexFile* ArtField::GetDexFile() REQUIRES_SHARED(Locks::mutator_lock_) {
Vladimir Marko09c5ca42018-05-31 15:15:31 +0100341 return GetDexCache<kWithoutReadBarrier>()->GetDexFile();
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700342}
343
Vladimir Marko18090d12018-06-01 16:53:12 +0100344inline ObjPtr<mirror::String> ArtField::ResolveNameString() {
345 uint32_t dex_field_index = GetDexFieldIndex();
Andreas Gampee2abbc62017-09-15 11:59:26 -0700346 CHECK_NE(dex_field_index, dex::kDexNoIndex);
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800347 const dex::FieldId& field_id = GetDexFile()->GetFieldId(dex_field_index);
Vladimir Marko18090d12018-06-01 16:53:12 +0100348 return Runtime::Current()->GetClassLinker()->ResolveString(field_id.name_idx_, this);
Mathieu Chartier76433272014-09-26 14:32:37 -0700349}
350
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700351// If kExactOffset is true then we only find the matching offset, not the field containing the
352// offset.
353template <bool kExactOffset>
354static inline ArtField* FindFieldWithOffset(
355 const IterationRange<StrideIterator<ArtField>>& fields,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700356 uint32_t field_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700357 for (ArtField& field : fields) {
358 if (kExactOffset) {
359 if (field.GetOffset().Uint32Value() == field_offset) {
360 return &field;
361 }
362 } else {
363 const uint32_t offset = field.GetOffset().Uint32Value();
364 Primitive::Type type = field.GetTypeAsPrimitiveType();
365 const size_t field_size = Primitive::ComponentSize(type);
366 DCHECK_GT(field_size, 0u);
367 if (offset <= field_offset && field_offset < offset + field_size) {
368 return &field;
369 }
370 }
371 }
372 return nullptr;
373}
374
375template <bool kExactOffset>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700376inline ArtField* ArtField::FindInstanceFieldWithOffset(ObjPtr<mirror::Class> klass,
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700377 uint32_t field_offset) {
378 DCHECK(klass != nullptr);
379 ArtField* field = FindFieldWithOffset<kExactOffset>(klass->GetIFields(), field_offset);
380 if (field != nullptr) {
381 return field;
382 }
383 // We did not find field in the class: look into superclass.
384 return (klass->GetSuperClass() != nullptr) ?
385 FindInstanceFieldWithOffset<kExactOffset>(klass->GetSuperClass(), field_offset) : nullptr;
386}
387
388template <bool kExactOffset>
Mathieu Chartier3398c782016-09-30 10:27:43 -0700389inline ArtField* ArtField::FindStaticFieldWithOffset(ObjPtr<mirror::Class> klass,
390 uint32_t field_offset) {
Mathieu Chartiercb044bc2016-04-01 13:56:41 -0700391 DCHECK(klass != nullptr);
392 return FindFieldWithOffset<kExactOffset>(klass->GetSFields(), field_offset);
393}
394
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000395inline ObjPtr<mirror::ClassLoader> ArtField::GetClassLoader() {
Andreas Gampe4835d212018-11-21 14:55:10 -0800396 return GetDeclaringClass()->GetClassLoader();
397}
398
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399} // namespace art
400
Mathieu Chartierc7853442015-03-27 14:35:38 -0700401#endif // ART_RUNTIME_ART_FIELD_INL_H_