blob: b3a480564f0bc19555661eb5c48f6750d72247cf [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
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 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070032#include "primitive.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070033#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070036#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070037
38namespace art {
39
40class Array;
41class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070042class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070043class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040045class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070046class InterfaceEntry;
47class Monitor;
48class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070049class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070050class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040051class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070052template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070053template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070054typedef PrimitiveArray<uint8_t> BooleanArray;
55typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070056typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070057typedef PrimitiveArray<double> DoubleArray;
58typedef PrimitiveArray<float> FloatArray;
59typedef PrimitiveArray<int32_t> IntArray;
60typedef PrimitiveArray<int64_t> LongArray;
61typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070062
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063union JValue {
64 uint8_t z;
65 int8_t b;
66 uint16_t c;
67 int16_t s;
68 int32_t i;
69 int64_t j;
70 float f;
71 double d;
72 Object* l;
73};
74
Logan Chienfca7e872011-12-20 20:08:22 +080075#if defined(ART_USE_LLVM_COMPILER)
76namespace compiler_llvm {
77 class InferredRegCategoryMap;
78}
79#endif
80
Brian Carlstrombe977852011-07-19 14:54:54 -070081static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
82static const uint32_t kAccPrivate = 0x0002; // field, method, ic
83static const uint32_t kAccProtected = 0x0004; // field, method, ic
84static const uint32_t kAccStatic = 0x0008; // field, method, ic
85static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
86static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070087static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070088static const uint32_t kAccVolatile = 0x0040; // field
89static const uint32_t kAccBridge = 0x0040; // method (1.5)
90static const uint32_t kAccTransient = 0x0080; // field
91static const uint32_t kAccVarargs = 0x0080; // method (1.5)
92static const uint32_t kAccNative = 0x0100; // method
93static const uint32_t kAccInterface = 0x0200; // class, ic
94static const uint32_t kAccAbstract = 0x0400; // class, method, ic
95static const uint32_t kAccStrict = 0x0800; // method
96static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
97static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
98static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070099
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700100static const uint32_t kAccMiranda = 0x8000; // method
101
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700102static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
103
Elliott Hughes582a7d12011-10-10 18:38:42 -0700104static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
105static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700107static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700108
Elliott Hughes20cde902011-10-04 17:37:27 -0700109// Special runtime-only flags.
110// Note: if only kAccClassIsReference is set, we have a soft reference.
111static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
112static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
113static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
114static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
115static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700116
117static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
118 | kAccClassIsWeakReference
119 | kAccClassIsFinalizerReference
120 | kAccClassIsPhantomReference);
121
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700122/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700123 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124 */
125/*
126 * A magic value for refOffsets. Ignore the bits and walk the super
127 * chain when this is the value.
128 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
129 * fields followed by 2 ref instance fields.]
130 */
131#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
133#define CLASS_OFFSET_ALIGNMENT 4
134#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
135/*
136 * Given an offset, return the bit number which would encode that offset.
137 * Local use only.
138 */
139#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700140 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700141 CLASS_OFFSET_ALIGNMENT)
142/*
143 * Is the given offset too large to be encoded?
144 */
145#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
146 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
147/*
148 * Return a single bit, encoding the offset.
149 * Undefined if the offset is too large, as defined above.
150 */
151#define CLASS_BIT_FROM_OFFSET(byteOffset) \
152 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
153/*
154 * Return an offset, given a bit number as returned from CLZ.
155 */
156#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700157 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159#define OFFSET_OF_OBJECT_MEMBER(type, field) \
160 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700162// Classes shared with the managed side of the world need to be packed
163// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700164#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700167class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700168 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700169 static MemberOffset ClassOffset() {
170 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 }
172
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700173 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700174 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175 }
176
177 void SetClass(Class* new_klass);
178
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700179 bool InstanceOf(const Class* klass) const;
180
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700181 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700182
Elliott Hughes081be7f2011-09-18 16:50:26 -0700183 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700184
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 static MemberOffset MonitorOffset() {
186 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
187 }
188
Elliott Hughes5f791332011-09-15 17:45:30 -0700189 volatile int32_t* GetRawLockWordAddress() {
190 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
191 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
192 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193 }
194
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700195 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198
Ian Rogersff1ed472011-09-20 13:46:24 -0700199 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202
Elliott Hughes5f791332011-09-15 17:45:30 -0700203 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206
Elliott Hughes5f791332011-09-15 17:45:30 -0700207 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700208
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210
211 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700212 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213 return down_cast<Class*>(this);
214 }
215
216 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 return down_cast<const Class*>(this);
219 }
220
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700221 bool IsObjectArray() const;
222
223 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700224 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700226 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700227 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700228
Brian Carlstromb63ec392011-08-27 17:38:27 -0700229 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700230
231 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700232 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700233 return down_cast<Array*>(this);
234 }
235
236 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700237 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700238 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 }
240
Elliott Hughesdbb40792011-11-18 17:05:22 -0800241 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700242
243 bool IsMethod() const;
244
245 Method* AsMethod() {
246 DCHECK(IsMethod());
247 return down_cast<Method*>(this);
248 }
249
Brian Carlstrom4873d462011-08-21 15:23:39 -0700250 const Method* AsMethod() const {
251 DCHECK(IsMethod());
252 return down_cast<const Method*>(this);
253 }
254
Brian Carlstroma663ea52011-08-19 23:33:41 -0700255 bool IsField() const;
256
257 Field* AsField() {
258 DCHECK(IsField());
259 return down_cast<Field*>(this);
260 }
261
Brian Carlstrom4873d462011-08-21 15:23:39 -0700262 const Field* AsField() const {
263 DCHECK(IsField());
264 return down_cast<const Field*>(this);
265 }
266
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 bool IsReferenceInstance() const;
268
269 bool IsWeakReferenceInstance() const;
270
271 bool IsSoftReferenceInstance() const;
272
273 bool IsFinalizerReferenceInstance() const;
274
275 bool IsPhantomReferenceInstance() const;
276
277 // Accessors for Java type fields
278 template<class T>
279 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700280 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
281 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800282 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 return result;
284 }
285
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700286 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800287 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700288 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
289 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 }
293
294 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800295 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700296 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
297 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700298 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700299 return android_atomic_acquire_load(word_addr);
300 } else {
301 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 }
304
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700305 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
306 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800307 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700308 }
309 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700310 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700311 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700312 /*
313 * TODO: add an android_atomic_synchronization_store() function and
314 * use it in the 32-bit volatile set handlers. On some platforms we
315 * can use a fast atomic instruction and avoid the barriers.
316 */
317 ANDROID_MEMBAR_STORE();
318 *word_addr = new_value;
319 ANDROID_MEMBAR_FULL();
320 } else {
321 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 }
324
325 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800326 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700327 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700328 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700329 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700330 uint64_t result = QuasiAtomicRead64(addr);
331 ANDROID_MEMBAR_FULL();
332 return result;
333 } else {
334 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
337
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700338 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800339 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700340 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
341 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700342 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700343 ANDROID_MEMBAR_STORE();
344 QuasiAtomicSwap64(new_value, addr);
345 // Post-store barrier not required due to use of atomic op or mutex.
346 } else {
347 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
350
351 protected:
352 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 template<class T>
354 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700355 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
357
358 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800359 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
360 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 }
362
363 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700364 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Elliott Hughes5f791332011-09-15 17:45:30 -0700366 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367
Elliott Hughes5f791332011-09-15 17:45:30 -0700368 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700369 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700370 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700371};
372
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800374 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700375#ifdef MOVING_GARBAGE_COLLECTOR
376 // TODO: we'll need to use the Object's internal concept of identity
377 UNIMPLEMENTED(FATAL);
378#endif
379 return reinterpret_cast<size_t>(obj);
380 }
381};
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500384class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700385 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700388 void SetDeclaringClass(Class *new_declaring_class);
389
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 uint32_t GetAccessFlags() const;
391
392 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700393 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700394 }
395
Elliott Hughes80609252011-09-23 17:24:51 -0700396 bool IsPublic() const {
397 return (GetAccessFlags() & kAccPublic) != 0;
398 }
399
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700400 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700401 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402 }
403
jeffhaobdb76512011-09-07 11:43:16 -0700404 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700405 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700406 }
407
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800408 uint32_t GetDexFieldIndex() const {
409 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
410 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 void SetDexFieldIndex(uint32_t new_idx) {
413 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
414 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415
416 // Offset to field within an Object
417 MemberOffset GetOffset() const;
418
buzbee34cd9e52011-09-08 14:31:52 -0700419 static MemberOffset OffsetOffset() {
420 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
421 }
422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 MemberOffset GetOffsetDuringLinking() const;
424
425 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700426
Brian Carlstrom4873d462011-08-21 15:23:39 -0700427 // field access, null object for static fields
428 bool GetBoolean(const Object* object) const;
429 void SetBoolean(Object* object, bool z) const;
430 int8_t GetByte(const Object* object) const;
431 void SetByte(Object* object, int8_t b) const;
432 uint16_t GetChar(const Object* object) const;
433 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700434 int16_t GetShort(const Object* object) const;
435 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700436 int32_t GetInt(const Object* object) const;
437 void SetInt(Object* object, int32_t i) const;
438 int64_t GetLong(const Object* object) const;
439 void SetLong(Object* object, int64_t j) const;
440 float GetFloat(const Object* object) const;
441 void SetFloat(Object* object, float f) const;
442 double GetDouble(const Object* object) const;
443 void SetDouble(Object* object, double d) const;
444 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700445 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700446
Ian Rogersce9eca62011-10-07 17:11:03 -0700447 // raw field accesses
448 uint32_t Get32(const Object* object) const;
449 void Set32(Object* object, uint32_t new_value) const;
450 uint64_t Get64(const Object* object) const;
451 void Set64(Object* object, uint64_t new_value) const;
452 Object* GetObj(const Object* object) const;
453 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700454
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 static Class* GetJavaLangReflectField() {
456 DCHECK(java_lang_reflect_Field_ != NULL);
457 return java_lang_reflect_Field_;
458 }
459
460 static void SetClass(Class* java_lang_reflect_Field);
461 static void ResetClass();
462
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 bool IsVolatile() const {
464 return (GetAccessFlags() & kAccVolatile) != 0;
465 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700466
buzbee1da522d2011-09-04 11:22:20 -0700467 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400468 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400470 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471
Jesse Wilson35baaab2011-08-10 16:18:03 -0400472 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700473
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800474 // Dex cache index of field id
475 uint32_t field_dex_idx_;
476
Brian Carlstrom693267a2011-09-06 09:25:34 -0700477 // Offset of field within an instance or in the Class' static fields
478 uint32_t offset_;
479
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700480 static Class* java_lang_reflect_Field_;
481
Brian Carlstrom693267a2011-09-06 09:25:34 -0700482 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400483 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700484};
485
Jesse Wilson6bf19152011-09-29 13:12:33 -0400486// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500487class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700488 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700489 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700490 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700491 Object* obj,
492 Thread* thread,
493 byte* args,
494 JValue* result);
495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700497
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700498 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700499
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700500 static MemberOffset DeclaringClassOffset() {
501 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700502 }
503
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700504 uint32_t GetAccessFlags() const;
505
506 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700507 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700508 }
509
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700510 // Returns true if the method is declared public.
511 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 }
514
515 // Returns true if the method is declared private.
516 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700517 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700518 }
519
520 // Returns true if the method is declared static.
521 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700522 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700523 }
524
Brian Carlstrom1f870082011-08-23 16:02:11 -0700525 // Returns true if the method is a constructor.
526 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700527 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700528 }
529
530 // Returns true if the method is static, private, or a constructor.
531 bool IsDirect() const {
532 return IsStatic() || IsPrivate() || IsConstructor();
533 }
534
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535 // Returns true if the method is declared synchronized.
536 bool IsSynchronized() const {
537 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700538 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700539 }
540
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700541 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 }
544
Elliott Hughes80609252011-09-23 17:24:51 -0700545 bool IsMiranda() const {
546 return (GetAccessFlags() & kAccMiranda) != 0;
547 }
548
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700549 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700551 }
552
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700553 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555 }
556
557 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700558 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700559 }
560
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800561 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700563 uint16_t GetMethodIndex() const;
564
565 size_t GetVtableIndex() const {
566 return GetMethodIndex();
567 }
568
569 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700570 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700571 }
572
573 static MemberOffset MethodIndexOffset() {
574 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
575 }
576
577 uint32_t GetCodeItemOffset() const {
578 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
579 }
580
581 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700582 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 }
584
585 // Number of 32bit registers that would be required to hold all the arguments
586 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700587
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800588 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700589
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800590 void SetDexMethodIndex(uint32_t new_idx) {
591 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700592 }
593
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700594 ObjectArray<String>* GetDexCacheStrings() const;
595 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
596
597 static MemberOffset DexCacheStringsOffset() {
598 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
599 }
600
Ian Rogers19846512012-02-24 11:42:47 -0800601 static MemberOffset DexCacheResolvedMethodsOffset() {
602 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
603 }
604
buzbee2a475e72011-09-07 17:19:17 -0700605 static MemberOffset DexCacheResolvedTypesOffset() {
606 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
607 }
608
buzbee1da522d2011-09-04 11:22:20 -0700609 static MemberOffset DexCacheInitializedStaticStorageOffset() {
610 return OFFSET_OF_OBJECT_MEMBER(Method,
611 dex_cache_initialized_static_storage_);
612 }
613
Ian Rogers19846512012-02-24 11:42:47 -0800614 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
615 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
616
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700617 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
618 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
619
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700620 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
621 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
622
Ian Rogers466bb252011-10-14 03:29:56 -0700623 // Find the method that this method overrides
624 Method* FindOverriddenMethod() const;
625
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700626 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
627
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700628 const void* GetCode() const {
629 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700630 }
631
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700632 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700633 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
634 }
635
636 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700637 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700638 return reinterpret_cast<uint32_t>(GetCode());
639 }
640
641 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700642 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700643 SetCode(reinterpret_cast<void*>(code_offset));
644 }
645
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700646 static MemberOffset GetCodeOffset() {
647 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700648 }
649
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700650 const uint32_t* GetMappingTable() const {
651 const uint32_t* map = GetMappingTableRaw();
652 if (map == NULL) {
653 return map;
654 }
655 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700656 }
657
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700658 uint32_t GetMappingTableLength() const {
659 const uint32_t* map = GetMappingTableRaw();
660 if (map == NULL) {
661 return 0;
662 }
663 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700664 }
665
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700666 const uint32_t* GetMappingTableRaw() const {
667 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700668 }
669
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700670 void SetMappingTable(const uint32_t* mapping_table) {
671 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
672 mapping_table, false);
673 }
674
675 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700676 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700677 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
678 }
679
680 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700681 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700682 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
683 }
684
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800685 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700686 const uint16_t* GetVmapTableRaw() const {
687 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
688 }
689
690 void SetVmapTable(const uint16_t* vmap_table) {
691 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
692 }
693
694 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700695 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700696 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
697 }
698
699 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700700 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700701 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702 }
703
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800704 const uint8_t* GetGcMap() const {
705 const uint8_t* gc_map_raw = GetGcMapRaw();
706 if (gc_map_raw == NULL) {
707 return gc_map_raw;
708 }
709 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 }
711
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800712 uint32_t GetGcMapLength() const {
713 const uint8_t* gc_map_raw = GetGcMapRaw();
714 if (gc_map_raw == NULL) {
715 return 0;
716 }
717 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
718 (gc_map_raw[1] << 16) |
719 (gc_map_raw[2] << 8) |
720 (gc_map_raw[3] << 0));
721 }
722
723 const uint8_t* GetGcMapRaw() const {
724 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
725 }
726 void SetGcMap(const uint8_t* data) {
727 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
728 }
729
730 uint32_t GetOatGcMapOffset() const {
731 DCHECK(!Runtime::Current()->IsStarted());
732 return reinterpret_cast<uint32_t>(GetGcMapRaw());
733 }
734 void SetOatGcMapOffset(uint32_t gc_map_offset) {
735 DCHECK(!Runtime::Current()->IsStarted());
736 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 }
738
Logan Chienfca7e872011-12-20 20:08:22 +0800739#if defined(ART_USE_LLVM_COMPILER)
740 // NOTE: In order not to change the Oat file format, we are reusing the
741 // gc_map_ field, so be careful while altering the GC map related code.
742
743 const compiler_llvm::InferredRegCategoryMap* GetInferredRegCategoryMap() const;
744
745 void SetInferredRegCategoryMap(const compiler_llvm::InferredRegCategoryMap* map);
746 void ResetInferredRegCategoryMap();
747#endif
748
Shih-wei Liaod11af152011-08-23 16:02:11 -0700749 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700750 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
751 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700752 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
753 return result;
754 }
755
756 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700757 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800758#if !defined(ART_USE_LLVM_COMPILER) // LLVM uses shadow stack instead.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700759 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
Shih-wei Liao5b8b1ed2012-02-23 23:48:21 -0800760#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700761 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
762 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700763 }
764
Shih-wei Liaod11af152011-08-23 16:02:11 -0700765 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700767 }
768
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700769 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700770
Ian Rogers60db5ab2012-02-20 17:02:00 -0800771 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700772
Ian Rogers19846512012-02-24 11:42:47 -0800773 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700774
Ian Rogersb033c752011-07-20 12:22:35 -0700775 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700776 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700777 }
778
Brian Carlstrom78128a62011-09-15 17:21:19 -0700779 const void* GetNativeMethod() const {
780 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
781 }
782
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700783 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700784 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700785 InvokeStub* result = GetFieldPtr<InvokeStub*>(
786 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
787 // TODO: DCHECK(result != NULL); should be ahead of time compiled
788 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700789 }
790
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700791 void SetInvokeStub(InvokeStub* invoke_stub) {
792 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
793 invoke_stub, false);
794 }
795
796 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700797 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700798 return reinterpret_cast<uint32_t>(GetInvokeStub());
799 }
800
801 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700802 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700803 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
804 }
805
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700806 static MemberOffset GetInvokeStubOffset() {
807 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700808 }
809
buzbee561227c2011-09-02 15:28:19 -0700810 static MemberOffset GetMethodIndexOffset() {
811 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
812 }
813
Ian Rogers90865722011-09-19 11:11:44 -0700814 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700815 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700816 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700817
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700818 void SetCoreSpillMask(uint32_t core_spill_mask) {
819 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700820 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700821 }
822
Ian Rogers90865722011-09-19 11:11:44 -0700823 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700824 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
825 }
826
827 void SetFpSpillMask(uint32_t fp_spill_mask) {
828 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700829 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700830 }
831
Ian Rogers90865722011-09-19 11:11:44 -0700832 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700833 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700834 Runtime* runtime = Runtime::Current();
835 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700836 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700837 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
838 result = true;
839 break;
840 }
841 }
Ian Rogers90865722011-09-19 11:11:44 -0700842 // Check that if we do think it is phony it looks like the callee save method
Ian Rogers19846512012-02-24 11:42:47 -0800843 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
Ian Rogers90865722011-09-19 11:11:44 -0700844 return result;
845 }
846
Ian Rogers19846512012-02-24 11:42:47 -0800847 bool IsResolutionMethod() const {
848 bool result = this == Runtime::Current()->GetResolutionMethod();
849 // Check that if we do think it is phony it looks like the resolution method
850 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
851 return result;
852 }
853
854 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
855 // conventions for a method of managed code.
856 bool IsRuntimeMethod() const {
857 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
858 }
859
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 // Converts a native PC to a dex PC. TODO: this is a no-op
861 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700862 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700863
864 // Converts a dex PC to a native PC. TODO: this is a no-op
865 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700866 uintptr_t ToNativePC(const uint32_t dex_pc) const;
867
868 // Find the catch block for the given exception type and dex_pc
869 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700870
Elliott Hughes80609252011-09-23 17:24:51 -0700871 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
872
873 static Class* GetConstructorClass() {
874 return java_lang_reflect_Constructor_;
875 }
876
877 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700878 return java_lang_reflect_Method_;
879 }
880
Elliott Hughes80609252011-09-23 17:24:51 -0700881 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700882
883 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700884 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800885 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700886 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700887
Brian Carlstrom693267a2011-09-06 09:25:34 -0700888 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800889 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700890
891 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800892 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700893
894 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700895 ObjectArray<Class>* dex_cache_resolved_types_;
896
897 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
898 ObjectArray<String>* dex_cache_strings_;
899
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700900 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700901 uint32_t access_flags_;
902
903 // Compiled code associated with this method for callers from managed code.
904 // May be compiled managed code or a bridge for invoking a native method.
905 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700907 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700908 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700909
Brian Carlstrom693267a2011-09-06 09:25:34 -0700910 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700911 uint32_t core_spill_mask_;
912
913 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700914 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700915
Brian Carlstrom693267a2011-09-06 09:25:34 -0700916 // Total size in bytes of the frame
917 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700918
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800919 // Garbage collection map
920 const uint8_t* gc_map_;
921
Brian Carlstrom693267a2011-09-06 09:25:34 -0700922 // Native invocation stub entry point for calling from native to managed code.
923 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700924
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700926 const uint32_t* mapping_table_;
927
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800928 // Index into method_ids of the dex file associated with this method
929 uint32_t method_dex_index_;
930
Ian Rogers466bb252011-10-14 03:29:56 -0700931 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700932 //
Ian Rogers466bb252011-10-14 03:29:56 -0700933 // For abstract methods in an interface class, this is the offset of the method in
934 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800935 //
936 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700937 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700938
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700939 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700940 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700941
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 // When a register is promoted into a register, the spill mask holds which registers hold dex
943 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
944 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700945 const uint16_t* vmap_table_;
946
Elliott Hughes80609252011-09-23 17:24:51 -0700947 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700948 static Class* java_lang_reflect_Method_;
949
Brian Carlstrom693267a2011-09-06 09:25:34 -0700950 friend class ImageWriter; // for relocating code_ and invoke_stub_
951 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700952 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700953};
954
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700955class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700956 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700957 // A convenience for code that doesn't know the component size,
958 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700959 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700960
Brian Carlstromb63ec392011-08-27 17:38:27 -0700961 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700962
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700963 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700964
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700965 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700966 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700967 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700968
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700969 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700970 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700971 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700972 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700973
buzbeec143c552011-08-20 17:38:58 -0700974 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700975 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -0700976 }
977
Ian Rogersa15e67d2012-02-28 13:51:55 -0800978 static MemberOffset DataOffset(size_t component_size) {
979 if (component_size != sizeof(int64_t)) {
980 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
981 } else {
982 // Align longs and doubles.
983 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
984 }
buzbeec143c552011-08-20 17:38:58 -0700985 }
986
Ian Rogersa15e67d2012-02-28 13:51:55 -0800987 void* GetRawData(size_t component_size) {
988 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
989 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700990 }
991
Elliott Hughes289da822011-08-16 10:11:20 -0700992 protected:
993 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700994 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -0700995 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -0700996 }
997 return true;
998 }
999
Elliott Hughes80609252011-09-23 17:24:51 -07001000 protected:
1001 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1002 bool ThrowArrayStoreException(Object* object) const;
1003
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001004 private:
1005 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001006 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001007 // Marker for the data (used by generated code)
1008 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001009
Carl Shapirof88c9522011-08-06 15:47:38 -07001010 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001011};
1012
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001013template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001014class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001015 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001017
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001018 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001019
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001020 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001021
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001022 // Set element without bound and element type checks, to be used in limited
1023 // circumstances, such as during boot image writing
1024 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001025
Ian Rogers5d76c432011-10-31 21:42:49 -07001026 T* GetWithoutChecks(int32_t i) const;
1027
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001028 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001029 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001030 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001031
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001032 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001033
1034 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001035 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001036};
1037
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001038template<class T>
1039ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001040 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001041 if (UNLIKELY(array == NULL)) {
1042 return NULL;
1043 } else {
1044 return array->AsObjectArray<T>();
1045 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001046}
1047
1048template<class T>
1049T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001050 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001051 return NULL;
1052 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001053 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001054 return GetFieldObject<T*>(data_offset, false);
1055}
1056
1057template<class T>
1058ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1059 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1060 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1061 return new_array;
1062}
1063
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001064// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001065// provides the static storage. However, this might change to an Array
1066// to improve image sharing, so we use this type to avoid assumptions
1067// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001068class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001069
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001070// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001071class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001072 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001073
1074 // Class Status
1075 //
1076 // kStatusNotReady: If a Class cannot be found in the class table by
1077 // FindClass, it allocates an new one with AllocClass in the
1078 // kStatusNotReady and calls LoadClass. Note if it does find a
1079 // class, it may not be kStatusResolved and it will try to push it
1080 // forward toward kStatusResolved.
1081 //
1082 // kStatusIdx: LoadClass populates with Class with information from
1083 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001084 // Class value in super_class_ has not been populated. The new Class
1085 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001086 //
1087 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1088 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001089 // using ResolveClass to initialize the super_class_ and ensuring the
1090 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001091 //
1092 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001093 // shows linking is complete and fields of the Class populated by making
1094 // it kStatusResolved. Java allows circularities of the form where a super
1095 // class has a field that is of the type of the sub class. We need to be able
1096 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001097
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001099 kStatusError = -1,
1100 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001101 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001102 kStatusLoaded = 2, // DEX idx values resolved
1103 kStatusResolved = 3, // part of linking
1104 kStatusVerifying = 4, // in the process of being verified
1105 kStatusVerified = 5, // logically part of linking; done pre-init
1106 kStatusInitializing = 6, // class init in progress
1107 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001108 };
1109
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001110 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001111 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1112 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001113 }
1114
1115 void SetStatus(Status new_status);
1116
1117 // Returns true if the class has failed to link.
1118 bool IsErroneous() const {
1119 return GetStatus() == kStatusError;
1120 }
1121
1122 // Returns true if the class has been loaded.
1123 bool IsIdxLoaded() const {
1124 return GetStatus() >= kStatusIdx;
1125 }
1126
1127 // Returns true if the class has been loaded.
1128 bool IsLoaded() const {
1129 return GetStatus() >= kStatusLoaded;
1130 }
1131
1132 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001133 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001134 return GetStatus() >= kStatusResolved;
1135 }
1136
1137 // Returns true if the class has been verified.
1138 bool IsVerified() const {
1139 return GetStatus() >= kStatusVerified;
1140 }
1141
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001142 // Returns true if the class is initializing.
1143 bool IsInitializing() const {
1144 return GetStatus() >= kStatusInitializing;
1145 }
1146
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147 // Returns true if the class is initialized.
1148 bool IsInitialized() const {
1149 return GetStatus() == kStatusInitialized;
1150 }
1151
1152 uint32_t GetAccessFlags() const;
1153
1154 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001155 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 }
1157
1158 // Returns true if the class is an interface.
1159 bool IsInterface() const {
1160 return (GetAccessFlags() & kAccInterface) != 0;
1161 }
1162
1163 // Returns true if the class is declared public.
1164 bool IsPublic() const {
1165 return (GetAccessFlags() & kAccPublic) != 0;
1166 }
1167
1168 // Returns true if the class is declared final.
1169 bool IsFinal() const {
1170 return (GetAccessFlags() & kAccFinal) != 0;
1171 }
1172
Elliott Hughes20cde902011-10-04 17:37:27 -07001173 bool IsFinalizable() const {
1174 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1175 }
1176
1177 void SetFinalizable() {
1178 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1179 SetAccessFlags(flags | kAccClassIsFinalizable);
1180 }
1181
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001182 // Returns true if the class is abstract.
1183 bool IsAbstract() const {
1184 return (GetAccessFlags() & kAccAbstract) != 0;
1185 }
1186
1187 // Returns true if the class is an annotation.
1188 bool IsAnnotation() const {
1189 return (GetAccessFlags() & kAccAnnotation) != 0;
1190 }
1191
1192 // Returns true if the class is synthetic.
1193 bool IsSynthetic() const {
1194 return (GetAccessFlags() & kAccSynthetic) != 0;
1195 }
1196
1197 bool IsReferenceClass() const {
1198 return (GetAccessFlags() & kAccClassIsReference) != 0;
1199 }
1200
1201 bool IsWeakReferenceClass() const {
1202 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1203 }
1204
1205 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001206 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001207 }
1208
1209 bool IsFinalizerReferenceClass() const {
1210 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1211 }
1212
1213 bool IsPhantomReferenceClass() const {
1214 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1215 }
1216
Ian Rogersd418eda2012-01-30 12:14:28 -08001217
1218 String* GetName() const ; // Returns the cached name
1219 void SetName(String* name); // Sets the cached name
1220 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001221
1222 bool IsProxyClass() const {
1223 // Read access flags without using getter as whether something is a proxy can be check in
1224 // any loaded state
1225 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1226 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1227 return (access_flags & kAccClassIsProxy) != 0;
1228 }
1229
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001230 Primitive::Type GetPrimitiveType() const {
1231 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1232 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001233 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1234 }
1235
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001236 void SetPrimitiveType(Primitive::Type new_type) {
1237 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001238 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001239 }
1240
1241 // Returns true if the class is a primitive type.
1242 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001243 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001244 }
1245
1246 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001247 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001248 }
1249
1250 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001251 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001252 }
1253
1254 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001255 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001256 }
1257
1258 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001259 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001260 }
1261
1262 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001263 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001264 }
1265
1266 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001267 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001268 }
1269
1270 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001271 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001272 }
1273
1274 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001275 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001276 }
1277
1278 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001279 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001280 }
1281
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 // Depth of class from java.lang.Object
1283 size_t Depth() {
1284 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001285 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 depth++;
1287 }
1288 return depth;
1289 }
1290
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001291 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001292 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293 }
1294
Elliott Hughesdbb40792011-11-18 17:05:22 -08001295 bool IsClassClass() const;
1296
1297 bool IsStringClass() const;
1298
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001299 bool IsThrowableClass() const;
1300
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001301 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001302 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001303 }
1304
1305 void SetComponentType(Class* new_component_type) {
1306 DCHECK(GetComponentType() == NULL);
1307 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001308 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001309 }
1310
1311 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001312 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001313 }
1314
1315 bool IsObjectClass() const {
1316 return !IsPrimitive() && GetSuperClass() == NULL;
1317 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001318 bool IsInstantiable() const {
1319 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1320 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001321
Brian Carlstrom1f870082011-08-23 16:02:11 -07001322 // Creates a raw object instance but does not invoke the default constructor.
1323 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001324
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 bool IsVariableSize() const {
1326 // Classes and arrays vary in size, and so the object_size_ field cannot
1327 // be used to get their instance size
1328 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001329 }
1330
Brian Carlstrom4873d462011-08-21 15:23:39 -07001331 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001332 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001334 }
1335
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001336 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001337 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 }
1340
Ian Rogers0571d352011-11-03 19:51:38 -07001341 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001342
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001343 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001344 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001345 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001346 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001347 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001348 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 }
1350
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 void SetObjectSize(size_t new_object_size) {
1352 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001353 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001354 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001355 }
1356
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001357 // Returns true if this class is in the same packages as that class.
1358 bool IsInSamePackage(const Class* that) const;
1359
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001360 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001361
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001362 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001363 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001364 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001365 }
1366
Ian Rogersf2391652011-12-14 12:50:52 -08001367 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001368 // Note that access to the class isn't checked in case the declaring class is protected and the
1369 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001370 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001371 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001372 if (this == access_to) {
1373 return true;
1374 }
Ian Rogersf2391652011-12-14 12:50:52 -08001375 // Public members are trivially accessible
1376 if (member_flags & kAccPublic) {
1377 return true;
1378 }
1379 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001380 if (member_flags & kAccPrivate) {
1381 return false;
1382 }
Ian Rogersf2391652011-12-14 12:50:52 -08001383 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001384 if (member_flags & kAccProtected) {
1385 if (this->IsSubClass(access_to)) {
1386 return true;
1387 }
1388 }
Ian Rogersf2391652011-12-14 12:50:52 -08001389 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001390 return this->IsInSamePackage(access_to);
1391 }
1392
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001393 bool IsSubClass(const Class* klass) const;
1394
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1396 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1397 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1398 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1399 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001400 bool IsAssignableFrom(const Class* src) const {
1401 DCHECK(src != NULL);
1402 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001403 // Can always assign to things of the same type
1404 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001405 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001406 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001407 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001409 return src->Implements(this);
1410 } else if (src->IsArrayClass()) {
1411 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001414 }
1415 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 Class* GetSuperClass() const {
1418 // Can only get super class for loaded classes (hack for when runtime is
1419 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001420 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001421 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422 }
1423
1424 void SetSuperClass(Class *new_super_class) {
1425 // super class is assigned once, except during class linker initialization
1426 Class* old_super_class = GetFieldObject<Class*>(
1427 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1428 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1429 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001430 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 }
1432
1433 bool HasSuperClass() const {
1434 return GetSuperClass() != NULL;
1435 }
1436
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001437 static MemberOffset SuperClassOffset() {
1438 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001439 }
1440
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001441 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001442
1443 void SetClassLoader(const ClassLoader* new_cl);
1444
1445 static MemberOffset DexCacheOffset() {
1446 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1447 }
1448
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001449 enum {
1450 kDumpClassFullDetail = 1,
1451 kDumpClassClassLoader = (1 << 1),
1452 kDumpClassInitialized = (1 << 2),
1453 };
1454
Elliott Hughes4681c802011-09-25 18:04:37 -07001455 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 DexCache* GetDexCache() const;
1458
1459 void SetDexCache(DexCache* new_dex_cache);
1460
1461 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001462 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001463 return GetFieldObject<ObjectArray<Method>*>(
1464 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1465 }
1466
1467 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1468 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1469 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1470 DCHECK_NE(0, new_direct_methods->GetLength());
1471 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1472 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001473 }
1474
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001475 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001477 }
1478
1479 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 ObjectArray<Method>* direct_methods =
1481 GetFieldObject<ObjectArray<Method>*>(
1482 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1483 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001484 }
1485
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001486 // Returns the number of static, private, and constructor methods.
1487 size_t NumDirectMethods() const {
1488 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1489 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001490
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001491 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001492 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001493 return GetFieldObject<ObjectArray<Method>*>(
1494 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1495 }
1496
1497 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1498 // TODO: we reassign virtual methods to grow the table for miranda
1499 // methods.. they should really just be assigned once
1500 DCHECK_NE(0, new_virtual_methods->GetLength());
1501 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1502 new_virtual_methods, false);
1503 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001504
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001505 // Returns the number of non-inherited virtual methods.
1506 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001507 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001508 }
1509
1510 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001511 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001512 return GetVirtualMethods()->Get(i);
1513 }
1514
1515 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001516 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001517 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001518 }
1519
1520 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001521 ObjectArray<Method>* virtual_methods =
1522 GetFieldObject<ObjectArray<Method>*>(
1523 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1524 virtual_methods->Set(i, f);
1525 }
1526
1527 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001528 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001529 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001530 }
1531
1532 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001533 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001534 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001535 }
1536
1537 void SetVTable(ObjectArray<Method>* new_vtable) {
1538 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1539 }
1540
1541 static MemberOffset VTableOffset() {
1542 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001543 }
1544
Brian Carlstrom30b94452011-08-25 21:35:26 -07001545 // Given a method implemented by this class but potentially from a
1546 // super class, return the specific implementation
1547 // method for this class.
1548 Method* FindVirtualMethodForVirtual(Method* method) {
1549 DCHECK(!method->GetDeclaringClass()->IsInterface());
1550 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551 // Use the index to a potentially overridden one for this instance's class.
1552 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001553 }
1554
1555 // Given a method implemented by this class, but potentially from a
1556 // super class or interface, return the specific implementation
1557 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001558 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001559
Ian Rogers466bb252011-10-14 03:29:56 -07001560 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001561
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001562 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1563
Brian Carlstrom30b94452011-08-25 21:35:26 -07001564 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001565 if (method->IsDirect()) {
1566 return method;
1567 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001568 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001569 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001570 }
1571 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001572 }
1573
Ian Rogers466bb252011-10-14 03:29:56 -07001574 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001575
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001576 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1577
Ian Rogers466bb252011-10-14 03:29:56 -07001578 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001579
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001580 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001582 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1583
1584 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1585
1586 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1587
1588 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001589
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001590 int32_t GetIfTableCount() const {
1591 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1592 if (iftable == NULL) {
1593 return 0;
1594 }
1595 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001596 }
1597
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001598 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001599 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001600 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001601 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1602 }
1603
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001604 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001605 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001606 }
1607
1608 // Get instance fields
1609 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001610 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001611 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001612 }
1613
1614 void SetIFields(ObjectArray<Field>* new_ifields) {
1615 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1616 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001617 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 }
1619
1620 size_t NumInstanceFields() const {
1621 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1622 }
1623
1624 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1625 DCHECK_NE(NumInstanceFields(), 0U);
1626 return GetIFields()->Get(i);
1627 }
1628
1629 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1630 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1631 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1632 ifields->Set(i, f);
1633 }
1634
1635 // Returns the number of instance fields containing reference types.
1636 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001637 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001638 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001639 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001640 }
1641
1642 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001643 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001644 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001645 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 }
1647
1648 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001649 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001650 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001651 }
1652
1653 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001654 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001655 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001656 }
1657
1658 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1659
1660 // Beginning of static field data
1661 static MemberOffset FieldsOffset() {
1662 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1663 }
1664
1665 // Returns the number of static fields containing reference types.
1666 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001667 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001668 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001669 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001670 }
1671
1672 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001673 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001674 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001675 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001676 }
1677
1678 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001679 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001680 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681 }
1682
1683 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001684 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001685 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001686 }
1687
1688 void SetSFields(ObjectArray<Field>* new_sfields) {
1689 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1690 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001691 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 }
1693
1694 size_t NumStaticFields() const {
1695 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1696 }
1697
1698 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1699 return GetSFields()->Get(i);
1700 }
1701
1702 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1703 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1704 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1705 sfields->Set(i, f);
1706 }
1707
1708 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001709 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001710 }
1711
1712 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1713
Ian Rogersb067ac22011-12-13 18:05:09 -08001714 // Find a static or instance field using the JLS resolution order
1715 Field* FindField(const StringPiece& name, const StringPiece& type);
1716
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001717 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001718 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001719
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001720 // Finds the given instance field in this class or a superclass, only searches classes that
1721 // have the same dex cache.
1722 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1723
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001724 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001726 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1727
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001728 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001729 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001730
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001731 // Finds the given static field in this class or superclass, only searches classes that
1732 // have the same dex cache.
1733 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1734
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001735 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001736
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001737 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1738
Elliott Hughesdcc24742011-09-07 14:02:44 -07001739 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001740 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001741 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1742 }
1743
Elliott Hughesdcc24742011-09-07 14:02:44 -07001744 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001745 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001746 }
1747
1748 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001749 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001750 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001751 }
1752
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001753 uint16_t GetDexTypeIndex() const {
1754 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001755 }
1756
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001757 void SetDexTypeIndex(uint16_t type_idx) {
1758 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001759 }
1760
jeffhaobdb76512011-09-07 11:43:16 -07001761 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001762 void SetVerifyErrorClass(Class* klass) {
1763 CHECK(klass != NULL) << PrettyClass(this);
1764 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1765 }
1766
jeffhao5dbddee2011-09-07 16:38:26 -07001767 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001768 bool IsArrayAssignableFromArray(const Class* klass) const;
1769 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001770
Brian Carlstrom693267a2011-09-06 09:25:34 -07001771 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001772 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001773
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001774 // For array classes, the component class object for instanceof/checkcast
1775 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001776 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001777
Brian Carlstrom693267a2011-09-06 09:25:34 -07001778 // DexCache of resolved constant pool entries
1779 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1780 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001781
1782 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001783 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001784
Brian Carlstrom693267a2011-09-06 09:25:34 -07001785 // instance fields
1786 //
1787 // These describe the layout of the contents of an Object.
1788 // Note that only the fields directly declared by this class are
1789 // listed in ifields; fields declared by a superclass are listed in
1790 // the superclass's Class.ifields.
1791 //
1792 // All instance fields that refer to objects are guaranteed to be at
1793 // the beginning of the field list. num_reference_instance_fields_
1794 // specifies the number of reference fields.
1795 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001796
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001797 // Interface table (iftable_), one entry per interface supported by
1798 // this class. That means one entry for each interface we support
1799 // directly, indirectly via superclass, or indirectly via
1800 // superinterface. This will be null if neither we nor our
1801 // superclass implement any interfaces.
1802 //
1803 // Why we need this: given "class Foo implements Face", declare
1804 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1805 // is part of the Face interface. We can't easily use a single
1806 // vtable.
1807 //
1808 // For every interface a concrete class implements, we create an array
1809 // of the concrete vtable_ methods for the methods in the interface.
1810 ObjectArray<InterfaceEntry>* iftable_;
1811
Ian Rogersd418eda2012-01-30 12:14:28 -08001812 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1813 String* name_;
1814
Brian Carlstromdbc05252011-09-09 01:59:59 -07001815 // Static fields
1816 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001817
Ian Rogersd418eda2012-01-30 12:14:28 -08001818 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001819 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001820
Brian Carlstromdbc05252011-09-09 01:59:59 -07001821 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001822 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001823
Brian Carlstromdbc05252011-09-09 01:59:59 -07001824 // virtual methods defined in this class; invoked through vtable
1825 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001826
Ian Rogers9074b992011-10-26 17:41:55 -07001827 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1828 // copied in, and virtual methods from our class either replace those from the super or are
1829 // appended. For abstract classes, methods may be created in the vtable that aren't in
1830 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001831 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001832
Brian Carlstromdbc05252011-09-09 01:59:59 -07001833 // access flags; low 16 bits are defined by VM spec
1834 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001835
Jesse Wilson6bf19152011-09-29 13:12:33 -04001836 // Total size of the Class instance; used when allocating storage on gc heap.
1837 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001838 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001839
Elliott Hughes5f791332011-09-15 17:45:30 -07001840 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001841 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001842
Ian Rogersd418eda2012-01-30 12:14:28 -08001843 // type index from dex file
1844 // TODO: really 16bits
1845 uint32_t dex_type_idx_;
1846
Brian Carlstromdbc05252011-09-09 01:59:59 -07001847 // number of instance fields that are object refs
1848 size_t num_reference_instance_fields_;
1849
1850 // number of static fields that are object refs
1851 size_t num_reference_static_fields_;
1852
1853 // Total object size; used when allocating storage on gc heap.
1854 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001855 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001856 size_t object_size_;
1857
Ian Rogersd418eda2012-01-30 12:14:28 -08001858 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001859 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001860
1861 // Bitmap of offsets of ifields.
1862 uint32_t reference_instance_offsets_;
1863
1864 // Bitmap of offsets of sfields.
1865 uint32_t reference_static_offsets_;
1866
Brian Carlstrom693267a2011-09-06 09:25:34 -07001867 // state of class initialization
1868 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001869
Brian Carlstrom693267a2011-09-06 09:25:34 -07001870 // TODO: ?
1871 // initiating class loader list
1872 // NOTE: for classes with low serialNumber, these are unused, and the
1873 // values are kept in a table in gDvm.
1874 // InitiatingLoaderList initiating_loader_list_;
1875
Brian Carlstrom4873d462011-08-21 15:23:39 -07001876 // Location of first static field.
1877 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001878
Brian Carlstrom693267a2011-09-06 09:25:34 -07001879 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001880 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001881};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001882
Elliott Hughes1f359b02011-07-17 14:27:17 -07001883std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001884
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001885inline void Object::SetClass(Class* new_klass) {
1886 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001887 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001888}
1889
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001890inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001891 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001892 DCHECK(GetClass() != NULL);
1893 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001894}
1895
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001896inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001897 Class* java_lang_Class = GetClass()->GetClass();
1898 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001899}
1900
1901inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001903}
1904
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001905template<class T>
1906inline ObjectArray<T>* Object::AsObjectArray() {
1907 DCHECK(IsObjectArray());
1908 return down_cast<ObjectArray<T>*>(this);
1909}
1910
1911template<class T>
1912inline const ObjectArray<T>* Object::AsObjectArray() const {
1913 DCHECK(IsObjectArray());
1914 return down_cast<const ObjectArray<T>*>(this);
1915}
1916
Brian Carlstromb63ec392011-08-27 17:38:27 -07001917inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001918 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001919}
1920
Brian Carlstroma663ea52011-08-19 23:33:41 -07001921inline bool Object::IsField() const {
1922 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001923 Class* java_lang_reflect_Field =
1924 java_lang_Class->GetInstanceField(0)->GetClass();
1925 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001926}
1927
1928inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07001929 Class* c = GetClass();
1930 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001931}
1932
1933inline bool Object::IsReferenceInstance() const {
1934 return GetClass()->IsReferenceClass();
1935}
1936
1937inline bool Object::IsWeakReferenceInstance() const {
1938 return GetClass()->IsWeakReferenceClass();
1939}
1940
1941inline bool Object::IsSoftReferenceInstance() const {
1942 return GetClass()->IsSoftReferenceClass();
1943}
1944
1945inline bool Object::IsFinalizerReferenceInstance() const {
1946 return GetClass()->IsFinalizerReferenceClass();
1947}
1948
1949inline bool Object::IsPhantomReferenceInstance() const {
1950 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001951}
1952
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001953inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001954 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07001955 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001956 result = AsArray()->SizeOf();
1957 } else if (IsClass()) {
1958 result = AsClass()->SizeOf();
1959 } else {
1960 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001961 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001962 DCHECK(!IsField() || result == sizeof(Field));
1963 DCHECK(!IsMethod() || result == sizeof(Method));
1964 return result;
1965}
1966
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001967inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001968 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001969 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001970 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001971 return result;
1972}
1973
1974inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001975 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001976}
1977
1978inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001979 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001980 DCHECK(result != NULL) << this;
1981 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001982 return result;
1983}
1984
1985inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001986 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001987}
1988
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001989inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07001990 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08001991 size_t component_size = GetClass()->GetComponentSize();
1992 int32_t component_count = GetLength();
1993 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
1994 size_t data_size = component_count * component_size;
1995 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001996}
1997
1998template<class T>
1999void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002000 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002001 if (object != NULL) {
2002 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002003 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002004 ThrowArrayStoreException(object);
2005 return;
2006 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002007 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002008 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002009 SetFieldObject(data_offset, object, false);
2010 }
2011}
2012
2013template<class T>
2014void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2015 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002016 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002017 SetFieldObject(data_offset, object, false);
2018}
2019
2020template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002021T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2022 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002023 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002024 return GetFieldObject<T*>(data_offset, false);
2025}
2026
2027template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002028void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2029 ObjectArray<T>* dst, int dst_pos,
2030 size_t length) {
2031 if (src->IsValidIndex(src_pos) &&
2032 src->IsValidIndex(src_pos+length-1) &&
2033 dst->IsValidIndex(dst_pos) &&
2034 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002035 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2036 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002037 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002038 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002039 if (array_class == src->GetClass()) {
2040 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002041 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002042 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002043 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002044 // directly set field, we do a bulk write barrier at the end
2045 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002046 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2047 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2048 }
2049 } else {
2050 Class* element_class = array_class->GetComponentType();
2051 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002052 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002053 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002054 if (object != NULL && !object->InstanceOf(element_class)) {
2055 dst->ThrowArrayStoreException(object);
2056 return;
2057 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002058 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002059 // directly set field, we do a bulk write barrier at the end
2060 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002061 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2062 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2063 }
2064 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002065 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002066 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002067}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002068
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002069class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002070 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002071 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002072 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002073 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002074 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2075};
2076
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002077class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002078 private:
2079 CharArray* ASCII_;
2080 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002081 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002082 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002083 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002084 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2085};
2086
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002087class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002088 private:
2089 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002090 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002091 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2092};
2093
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002094class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002095 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002096 Object* ORDER_BY_SIGNATURE_;
2097 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002098 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2099};
2100
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002101template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002102class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002103 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002104 typedef T ElementType;
2105
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002106 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002107
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002108 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002109 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2110 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002111 }
2112
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002113 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002114 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2115 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002116 }
2117
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002118 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002119 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002120 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002121 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002122 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002123 }
2124
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002125 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002126 // TODO: ArrayStoreException
2127 if (IsValidIndex(i)) {
2128 GetData()[i] = value;
2129 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002130 }
2131
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002132 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002133 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002134 CHECK(array_class != NULL);
2135 array_class_ = array_class;
2136 }
2137
Brian Carlstroma663ea52011-08-19 23:33:41 -07002138 static void ResetArrayClass() {
2139 CHECK(array_class_ != NULL);
2140 array_class_ = NULL;
2141 }
2142
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002143 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002144 static Class* array_class_;
2145
Carl Shapirof88c9522011-08-06 15:47:38 -07002146 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002147};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002148
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002149// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002150class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002151 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002152 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002153 const CharArray* result = GetFieldObject<const CharArray*>(
2154 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2155 DCHECK(result != NULL);
2156 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002157 }
2158
Elliott Hughes814e4032011-08-23 12:07:56 -07002159 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002160 int32_t result = GetField32(
2161 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2162 DCHECK_LE(0, result);
2163 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002164 }
2165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002166 int32_t GetLength() const;
2167
Brian Carlstrom395520e2011-09-25 19:35:00 -07002168 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002169
2170 void ComputeHashCode() {
2171 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002172 }
2173
Elliott Hughes814e4032011-08-23 12:07:56 -07002174 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002175 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002176 }
2177
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002178 uint16_t CharAt(int32_t index) const;
2179
Brian Carlstromc74255f2011-09-11 22:47:39 -07002180 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002181
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002182 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002183 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002184 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002185
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002186 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002187
Jesse Wilson8989d992011-08-02 13:39:42 -07002188 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002189 const char* utf8_data_in);
2190
2191 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2192
2193 static String* Alloc(Class* java_lang_String, CharArray* array);
2194
2195 bool Equals(const char* modified_utf8) const;
2196
2197 // TODO: do we need this overload? give it a more intention-revealing name.
2198 bool Equals(const StringPiece& modified_utf8) const;
2199
2200 bool Equals(const String* that) const;
2201
Ian Rogers0571d352011-11-03 19:51:38 -07002202 // Compare UTF-16 code point values not in a locale-sensitive manner
2203 int Compare(int32_t utf16_length, const char* utf8_data_in);
2204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002205 // TODO: do we need this overload? give it a more intention-revealing name.
2206 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2207 int32_t that_length) const;
2208
2209 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2210 std::string ToModifiedUtf8() const;
2211
2212 static Class* GetJavaLangString() {
2213 DCHECK(java_lang_String_ != NULL);
2214 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002215 }
2216
Brian Carlstroma663ea52011-08-19 23:33:41 -07002217 static void SetClass(Class* java_lang_String);
2218 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002219
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002220 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002221 void SetHashCode(int32_t new_hash_code) {
2222 DCHECK_EQ(0u,
2223 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2224 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2225 new_hash_code, false);
2226 }
2227
2228 void SetCount(int32_t new_count) {
2229 DCHECK_LE(0, new_count);
2230 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2231 }
2232
2233 void SetOffset(int32_t new_offset) {
2234 DCHECK_LE(0, new_offset);
2235 DCHECK_GE(GetLength(), new_offset);
2236 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2237 }
2238
2239 void SetArray(CharArray* new_array) {
2240 DCHECK(new_array != NULL);
2241 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2242 }
2243
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002244 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2245 CharArray* array_;
2246
Brian Carlstromdbc05252011-09-09 01:59:59 -07002247 int32_t count_;
2248
Carl Shapirof88c9522011-08-06 15:47:38 -07002249 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002250
Elliott Hughes289da822011-08-16 10:11:20 -07002251 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002252
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002253 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002254
Brian Carlstrom693267a2011-09-06 09:25:34 -07002255 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002256 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002257};
2258
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002259struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002260 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002261 return string->GetHashCode();
2262 }
2263};
2264
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002265inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002266 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002267 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2268}
2269
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002270inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002271 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002272 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002273}
2274
2275inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002276 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002277 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002278}
2279
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002280inline uint32_t Class::GetAccessFlags() const {
2281 // Check class is loaded or this is java.lang.String that has a
2282 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002283 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002284 this == String::GetJavaLangString() ||
2285 this == Field::GetJavaLangReflectField() ||
2286 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002287 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002288 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2289}
2290
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002291inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002292 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002293 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2294}
2295
2296inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002297 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002298 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002299}
2300
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002301inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002302 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002303 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002304}
2305
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002306inline String* Class::GetName() const {
2307 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002308}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002309inline void Class::SetName(String* name) {
2310 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002311}
2312
2313// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002314class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002315 public:
2316 void SetDetailMessage(String* new_detail_message) {
2317 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2318 new_detail_message, false);
2319 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002320 String* GetDetailMessage() const {
2321 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2322 }
Ian Rogers9074b992011-10-26 17:41:55 -07002323 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002324
Ian Rogers1c5eb702012-02-01 09:18:34 -08002325 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2326 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2327 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2328 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002329 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002330
2331 static Class* GetJavaLangThrowable() {
2332 DCHECK(java_lang_Throwable_ != NULL);
2333 return java_lang_Throwable_;
2334 }
2335
2336 static void SetClass(Class* java_lang_Throwable);
2337 static void ResetClass();
2338
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002339 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002340 Object* GetStackState() const {
2341 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2342 }
2343
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002344 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2345 Throwable* cause_;
2346 String* detail_message_;
2347 Object* stack_state_; // Note this is Java volatile:
2348 Object* stack_trace_;
2349 Object* suppressed_exceptions_;
2350
Ian Rogers5167c972012-02-03 10:41:20 -08002351 static Class* java_lang_Throwable_;
2352
Brian Carlstrom693267a2011-09-06 09:25:34 -07002353 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002354 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2355};
2356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002357// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002358class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002359 public:
2360 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002361 return GetFieldObject<const String*>(
2362 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002363 }
2364
2365 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002366 return GetFieldObject<const String*>(
2367 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002368 }
2369
2370 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002371 return GetFieldObject<const String*>(
2372 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002373 }
2374
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002375 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002376 return GetField32(
2377 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002378 }
2379
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002380 static StackTraceElement* Alloc(String* declaring_class,
2381 String* method_name,
2382 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002383 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002384
2385 static void SetClass(Class* java_lang_StackTraceElement);
2386
2387 static void ResetClass();
2388
2389 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002390 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002391 String* declaring_class_;
2392 String* file_name_;
2393 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002394 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002395
2396 static Class* GetStackTraceElement() {
2397 DCHECK(java_lang_StackTraceElement_ != NULL);
2398 return java_lang_StackTraceElement_;
2399 }
2400
2401 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002402
2403 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002404 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2405};
2406
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002407class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002408 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002409 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002410 Class* interface = Get(kInterface)->AsClass();
2411 DCHECK(interface != NULL);
2412 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002413 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002414
Brian Carlstrom30b94452011-08-25 21:35:26 -07002415 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002416 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002417 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002418 DCHECK(Get(kInterface) == NULL);
2419 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002420 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002421
Brian Carlstrom86927212011-09-15 11:31:11 -07002422 size_t GetMethodArrayCount() const {
2423 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002424 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002425 return 0;
2426 }
2427 return method_array->GetLength();
2428 }
2429
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002430 ObjectArray<Method>* GetMethodArray() const {
2431 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2432 DCHECK(method_array != NULL);
2433 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002434 }
2435
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002436 void SetMethodArray(ObjectArray<Method>* new_ma) {
2437 DCHECK(new_ma != NULL);
2438 DCHECK(Get(kMethodArray) == NULL);
2439 Set(kMethodArray, new_ma);
2440 }
2441
2442 static size_t LengthAsArray() {
2443 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002444 }
2445
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002446 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002447
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002448 enum ArrayIndex {
2449 // Points to the interface class.
2450 kInterface = 0,
2451 // Method pointers into the vtable, allow fast map from interface
2452 // method index to concrete instance method.
2453 kMethodArray = 1,
2454 kMax = 2,
2455 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002456
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002457 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002458};
2459
Ian Rogersc2b44472011-12-14 21:17:17 -08002460class MANAGED SynthesizedProxyClass : public Class {
2461 public:
2462 ObjectArray<ObjectArray<Class> >* GetThrows() {
2463 return throws_;
2464 }
Jesse Wilson95caa792011-10-12 18:14:17 -04002465 private:
Ian Rogersc2b44472011-12-14 21:17:17 -08002466 ObjectArray<ObjectArray<Class> >* throws_;
2467 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002468};
2469
2470class MANAGED Proxy : public Object {
2471 private:
2472 Object* h_;
2473
2474 friend struct ProxyOffsets; // for verifying offset information
2475 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2476};
2477
Carl Shapiro1fb86202011-06-27 17:43:13 -07002478} // namespace art
2479
2480#endif // ART_SRC_OBJECT_H_