blob: 6f37d5af728afa02a512039474ec2e60ab4c2644 [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"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
136// if only kAccClassIsReference is set, we have a soft reference
137static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
138static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
139static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
140static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
141
142static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
143 | kAccClassIsWeakReference
144 | kAccClassIsFinalizerReference
145 | kAccClassIsPhantomReference);
146
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700148 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 */
150/*
151 * A magic value for refOffsets. Ignore the bits and walk the super
152 * chain when this is the value.
153 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
154 * fields followed by 2 ref instance fields.]
155 */
156#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
158#define CLASS_OFFSET_ALIGNMENT 4
159#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
160/*
161 * Given an offset, return the bit number which would encode that offset.
162 * Local use only.
163 */
164#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700165 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166 CLASS_OFFSET_ALIGNMENT)
167/*
168 * Is the given offset too large to be encoded?
169 */
170#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
171 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
172/*
173 * Return a single bit, encoding the offset.
174 * Undefined if the offset is too large, as defined above.
175 */
176#define CLASS_BIT_FROM_OFFSET(byteOffset) \
177 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
178/*
179 * Return an offset, given a bit number as returned from CLZ.
180 */
181#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700182 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184#define OFFSET_OF_OBJECT_MEMBER(type, field) \
185 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700187// Classes shared with the managed side of the world need to be packed
188// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700189#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700193 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700194 static bool InstanceOf(const Object* object, const Class* klass) {
195 if (object == NULL) {
196 return false;
197 }
198 return object->InstanceOf(klass);
199 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700200
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 static MemberOffset ClassOffset() {
202 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203 }
204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700206 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 }
208
209 void SetClass(Class* new_klass);
210
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700211 bool InstanceOf(const Class* klass) const;
212
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700213 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214
Elliott Hughes081be7f2011-09-18 16:50:26 -0700215 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700216
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 static MemberOffset MonitorOffset() {
218 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
219 }
220
Elliott Hughes5f791332011-09-15 17:45:30 -0700221 volatile int32_t* GetRawLockWordAddress() {
222 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
223 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
224 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 }
226
Elliott Hughes5f791332011-09-15 17:45:30 -0700227 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228
Elliott Hughes5f791332011-09-15 17:45:30 -0700229 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700230
Elliott Hughes5f791332011-09-15 17:45:30 -0700231 void MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Elliott Hughes5f791332011-09-15 17:45:30 -0700233 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Elliott Hughes5f791332011-09-15 17:45:30 -0700235 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236
Elliott Hughes5f791332011-09-15 17:45:30 -0700237 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238
Elliott Hughes5f791332011-09-15 17:45:30 -0700239 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700241 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
243 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 return down_cast<Class*>(this);
246 }
247
248 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 return down_cast<const Class*>(this);
251 }
252
Brian Carlstrom4873d462011-08-21 15:23:39 -0700253 bool IsClassClass() const;
254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 bool IsObjectArray() const;
256
257 template<class T>
258 ObjectArray<T>* AsObjectArray() {
259 DCHECK(IsObjectArray());
260 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700261 }
262
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700263 template<class T>
264 const ObjectArray<T>* AsObjectArray() const {
265 DCHECK(IsObjectArray());
266 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267 }
268
Brian Carlstromb63ec392011-08-27 17:38:27 -0700269 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700270
271 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700272 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700273 return down_cast<Array*>(this);
274 }
275
276 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700277 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700278 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 }
280
Brian Carlstroma663ea52011-08-19 23:33:41 -0700281 bool IsString() const;
282
283 String* AsString() {
284 DCHECK(IsString());
285 return down_cast<String*>(this);
286 }
287
288 bool IsMethod() const;
289
290 Method* AsMethod() {
291 DCHECK(IsMethod());
292 return down_cast<Method*>(this);
293 }
294
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 const Method* AsMethod() const {
296 DCHECK(IsMethod());
297 return down_cast<const Method*>(this);
298 }
299
Brian Carlstroma663ea52011-08-19 23:33:41 -0700300 bool IsField() const;
301
302 Field* AsField() {
303 DCHECK(IsField());
304 return down_cast<Field*>(this);
305 }
306
Brian Carlstrom4873d462011-08-21 15:23:39 -0700307 const Field* AsField() const {
308 DCHECK(IsField());
309 return down_cast<const Field*>(this);
310 }
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 bool IsReferenceInstance() const;
313
314 bool IsWeakReferenceInstance() const;
315
316 bool IsSoftReferenceInstance() const;
317
318 bool IsFinalizerReferenceInstance() const;
319
320 bool IsPhantomReferenceInstance() const;
321
322 // Accessors for Java type fields
323 template<class T>
324 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700325 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
326 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 Heap::VerifyObject(result);
328 return result;
329 }
330
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700331 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700333 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
334 if (new_value != NULL) {
335 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
338
339 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
340 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700341 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
342 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700344 return android_atomic_acquire_load(word_addr);
345 } else {
346 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
349
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700350 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
351 if (this_is_valid) {
352 Heap::VerifyObject(this);
353 }
354 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700355 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700357 /*
358 * TODO: add an android_atomic_synchronization_store() function and
359 * use it in the 32-bit volatile set handlers. On some platforms we
360 * can use a fast atomic instruction and avoid the barriers.
361 */
362 ANDROID_MEMBAR_STORE();
363 *word_addr = new_value;
364 ANDROID_MEMBAR_FULL();
365 } else {
366 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
369
370 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
371 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700372 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700373 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700375 uint64_t result = QuasiAtomicRead64(addr);
376 ANDROID_MEMBAR_FULL();
377 return result;
378 } else {
379 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 }
382
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700383 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700385 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
386 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700388 ANDROID_MEMBAR_STORE();
389 QuasiAtomicSwap64(new_value, addr);
390 // Post-store barrier not required due to use of atomic op or mutex.
391 } else {
392 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
395
396 protected:
397 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 template<class T>
399 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700400 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700401 }
402
403 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700404 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
405 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 }
407
408 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700409 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700410
Elliott Hughes5f791332011-09-15 17:45:30 -0700411 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700412
Elliott Hughes5f791332011-09-15 17:45:30 -0700413 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700414 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700415 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700416};
417
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700419class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400420 private:
421 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700423 friend struct AccessibleObjectOffsets; // for verifying offset information
424 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400425};
426
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700428class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700429 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 void SetDeclaringClass(Class *new_declaring_class);
433
434 const String* GetName() const;
435
436 void SetName(String* new_name);
437
438 uint32_t GetAccessFlags() const;
439
440 void SetAccessFlags(uint32_t new_access_flags) {
441 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
442 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700443 }
444
445 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700446 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447 }
448
jeffhaobdb76512011-09-07 11:43:16 -0700449 bool IsFinal() const {
450 return (access_flags_ & kAccFinal) != 0;
451 }
452
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700453 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700454
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 // Gets type using type index and resolved types in the dex cache, may be null
458 // if type isn't yet resolved
459 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700460
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 // Performs full resolution, may return null and set exceptions if type cannot
462 // be resolved
463 Class* GetType() const;
464
465 // Offset to field within an Object
466 MemberOffset GetOffset() const;
467
buzbee34cd9e52011-09-08 14:31:52 -0700468 static MemberOffset OffsetOffset() {
469 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
470 }
471
472 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer);
473
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 MemberOffset GetOffsetDuringLinking() const;
475
476 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700477
Brian Carlstrom4873d462011-08-21 15:23:39 -0700478 // field access, null object for static fields
479 bool GetBoolean(const Object* object) const;
480 void SetBoolean(Object* object, bool z) const;
481 int8_t GetByte(const Object* object) const;
482 void SetByte(Object* object, int8_t b) const;
483 uint16_t GetChar(const Object* object) const;
484 void SetChar(Object* object, uint16_t c) const;
485 uint16_t GetShort(const Object* object) const;
486 void SetShort(Object* object, uint16_t s) const;
487 int32_t GetInt(const Object* object) const;
488 void SetInt(Object* object, int32_t i) const;
489 int64_t GetLong(const Object* object) const;
490 void SetLong(Object* object, int64_t j) const;
491 float GetFloat(const Object* object) const;
492 void SetFloat(Object* object, float f) const;
493 double GetDouble(const Object* object) const;
494 void SetDouble(Object* object, double d) const;
495 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700496 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700497
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700498 // Slow path routines for static field access when field was unresolved at
499 // compile time
500 static uint32_t Get32StaticFromCode(uint32_t field_idx,
501 const Method* referrer);
502 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
503 uint32_t new_value);
504 static uint64_t Get64StaticFromCode(uint32_t field_idx,
505 const Method* referrer);
506 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
507 uint64_t new_value);
508 static Object* GetObjStaticFromCode(uint32_t field_idx,
509 const Method* referrer);
510 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
511 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700512
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700513 static Class* GetJavaLangReflectField() {
514 DCHECK(java_lang_reflect_Field_ != NULL);
515 return java_lang_reflect_Field_;
516 }
517
518 static void SetClass(Class* java_lang_reflect_Field);
519 static void ResetClass();
520
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700521 bool IsVolatile() const {
522 return (GetAccessFlags() & kAccVolatile) != 0;
523 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700524
buzbee1da522d2011-09-04 11:22:20 -0700525 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700526 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700527 uint32_t Get32(const Object* object) const;
528 void Set32(Object* object, uint32_t new_value) const;
529 uint64_t Get64(const Object* object) const;
530 void Set64(Object* object, uint64_t new_value) const;
531 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700532 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700533
Jesse Wilson35baaab2011-08-10 16:18:03 -0400534 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700535
Jesse Wilson35baaab2011-08-10 16:18:03 -0400536 // The class in which this field is declared.
537 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538
Jesse Wilson35baaab2011-08-10 16:18:03 -0400539 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540
Brian Carlstromdbc05252011-09-09 01:59:59 -0700541 const String* name_;
542
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700543 // Type of the field
Jesse Wilson35baaab2011-08-10 16:18:03 -0400544 Class* type_;
545
Brian Carlstromdbc05252011-09-09 01:59:59 -0700546 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700547
Jesse Wilson35baaab2011-08-10 16:18:03 -0400548 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549
550 // Offset of field within an instance or in the Class' static fields
551 uint32_t offset_;
552
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700553 // Dex cache index of resolved type
554 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400555
Brian Carlstrom693267a2011-09-06 09:25:34 -0700556 int32_t slot_;
557
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700558 static Class* java_lang_reflect_Field_;
559
Brian Carlstrom693267a2011-09-06 09:25:34 -0700560 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400561 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700562};
563
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700564// C++ mirror of java.lang.reflect.Method
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700565class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700566 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700567 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700568 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700569 Object* obj,
570 Thread* thread,
571 byte* args,
572 JValue* result);
573
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700574 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700575
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700576 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700578 static MemberOffset DeclaringClassOffset() {
579 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700580 }
581
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582 // Returns the method name, e.g. "<init>" or "eatLunch"
583 const String* GetName() const;
584
585 void SetName(String* new_name);
586
jeffhaoe23d93c2011-09-15 14:48:43 -0700587 ByteArray* GetRegisterMapData() const;
588
jeffhaoa0a764a2011-09-16 10:43:38 -0700589 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700590
591 ByteArray* GetRegisterMapHeader() const;
592
jeffhaoa0a764a2011-09-16 10:43:38 -0700593 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700594
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700595 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700596
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700597 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598
599 const String* GetSignature() const;
600
601 void SetSignature(String* new_signature);
602
603 bool HasSameNameAndDescriptor(const Method* that) const;
604
605 uint32_t GetAccessFlags() const;
606
607 void SetAccessFlags(uint32_t new_access_flags) {
608 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
609 false);
610 }
611
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612 // Returns true if the method is declared public.
613 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700615 }
616
617 // Returns true if the method is declared private.
618 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700620 }
621
622 // Returns true if the method is declared static.
623 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700624 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700625 }
626
Brian Carlstrom1f870082011-08-23 16:02:11 -0700627 // Returns true if the method is a constructor.
628 bool IsConstructor() const {
629 return (access_flags_ & kAccConstructor) != 0;
630 }
631
632 // Returns true if the method is static, private, or a constructor.
633 bool IsDirect() const {
634 return IsStatic() || IsPrivate() || IsConstructor();
635 }
636
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700637 // Returns true if the method is declared synchronized.
638 bool IsSynchronized() const {
639 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700640 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700641 }
642
643 // Returns true if the method is declared final.
644 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700645 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646 }
647
648 // Returns true if the method is declared native.
649 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700650 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651 }
652
653 // Returns true if the method is declared abstract.
654 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700655 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700656 }
657
658 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700659 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700660 }
661
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662 uint16_t GetMethodIndex() const;
663
664 size_t GetVtableIndex() const {
665 return GetMethodIndex();
666 }
667
668 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700669 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700670 new_method_index, false);
671 }
672
673 static MemberOffset MethodIndexOffset() {
674 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
675 }
676
677 uint32_t GetCodeItemOffset() const {
678 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
679 }
680
681 void SetCodeItemOffset(uint32_t new_code_off) {
682 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
683 new_code_off, false);
684 }
685
686 // Number of 32bit registers that would be required to hold all the arguments
687 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700688
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700689 // Number of argument bytes required for densely packing the
690 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700691 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700692
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700693 uint16_t NumRegisters() const;
694
695 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700696 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700697 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700698 }
699
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700700 uint16_t NumIns() const;
701
702 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700703 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700704 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700705 }
706
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700707 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700708
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700710 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700711 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700712 }
713
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700714 uint32_t GetProtoIdx() const;
715
716 void SetProtoIdx(uint32_t new_proto_idx) {
717 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700718 }
719
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700720 ObjectArray<String>* GetDexCacheStrings() const;
721 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
722
723 static MemberOffset DexCacheStringsOffset() {
724 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
725 }
726
buzbee2a475e72011-09-07 17:19:17 -0700727 static MemberOffset DexCacheResolvedTypesOffset() {
728 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
729 }
730
buzbee34cd9e52011-09-08 14:31:52 -0700731 static MemberOffset DexCacheResolvedFieldsOffset() {
732 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
733 }
734
buzbee1da522d2011-09-04 11:22:20 -0700735 static MemberOffset DexCacheInitializedStaticStorageOffset() {
736 return OFFSET_OF_OBJECT_MEMBER(Method,
737 dex_cache_initialized_static_storage_);
738 }
739
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700740 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
741 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
742
743 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
744 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
745
746 ObjectArray<Field>* GetDexCacheResolvedFields() const;
747 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
748
749 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
750 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
751
752 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
753 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
754
755 void SetReturnTypeIdx(uint32_t new_return_type_idx);
756
757 Class* GetReturnType() const;
758
759 bool IsReturnAReference() const;
760
761 bool IsReturnAFloat() const;
762
763 bool IsReturnADouble() const;
764
Ian Rogersb033c752011-07-20 12:22:35 -0700765 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700766 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700767 }
768
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700770
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700771 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700772
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700773 // "Args" may refer to any of the 3 levels of "Args."
774 // To avoid confusion, our code will denote which "Args" clearly:
775 // 1. UserArgs: Args that a user see.
776 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
777 // receiver.
778 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
779 // E.g., the first in Args is Method* for both static and non-static
780 // methods. And CConvArgs doesn't deal with the receiver because
781 // receiver is hardwired in an implicit register, so CConvArgs doesn't
782 // need to deal with it.
783 //
784 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700785 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700786
787 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700788 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700789 size_t NumReferenceArgs() const;
790
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700791 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700792 size_t NumLongOrDoubleArgs() const;
793
Ian Rogersb033c752011-07-20 12:22:35 -0700794 // Is the given method parameter a reference?
795 bool IsParamAReference(unsigned int param) const;
796
797 // Is the given method parameter a long or double?
798 bool IsParamALongOrDouble(unsigned int param) const;
799
Ian Rogersdf20fe02011-07-20 20:34:16 -0700800 // Size in bytes of the given parameter
801 size_t ParamSize(unsigned int param) const;
802
803 // Size in bytes of the return value
804 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700805
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700806 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
807
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700808 const ByteArray* GetCodeArray() const {
809 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
810 }
811
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700812 const void* GetCode() const {
813 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700814 }
815
buzbee4ef76522011-09-08 10:00:32 -0700816 void SetCode(ByteArray* code_array, InstructionSet instruction_set,
Ian Rogersbdb03912011-09-14 00:55:44 -0700817 IntArray* mapping_table = NULL);
buzbeec143c552011-08-20 17:38:58 -0700818
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700819 static MemberOffset GetCodeOffset() {
820 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700821 }
822
Ian Rogersbdb03912011-09-14 00:55:44 -0700823 // Is the given PC within the code array?
824 bool IsWithinCode(uintptr_t pc) const;
825
826 IntArray* GetMappingTable() const {
827 return GetFieldObject<IntArray*>(
828 OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
829 }
830
Shih-wei Liaod11af152011-08-23 16:02:11 -0700831 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700832 DCHECK(sizeof(size_t) == sizeof(uint32_t));
833 size_t result = GetField32(
834 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
835 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
836 return result;
837 }
838
839 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
840 DCHECK(sizeof(size_t) == sizeof(uint32_t));
841 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
842 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
843 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700844 }
845
Shih-wei Liaod11af152011-08-23 16:02:11 -0700846 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847 DCHECK(sizeof(size_t) == sizeof(uint32_t));
848 return GetField32(
849 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700850 }
851
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700852 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
853 DCHECK(sizeof(size_t) == sizeof(uint32_t));
854 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
855 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
856 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700857 }
858
Brian Carlstrom16192862011-09-12 17:50:06 -0700859 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700860
Brian Carlstrom16192862011-09-12 17:50:06 -0700861 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700862
Brian Carlstrom16192862011-09-12 17:50:06 -0700863 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700864
Ian Rogersb033c752011-07-20 12:22:35 -0700865 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700866 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700867 }
868
Brian Carlstrom78128a62011-09-15 17:21:19 -0700869 const void* GetNativeMethod() const {
870 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
871 }
872
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700873 ByteArray* GetInvokeStubArray() const {
874 ByteArray* result = GetFieldPtr<ByteArray*>(
875 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
876 // TODO: DCHECK(result != NULL); should be ahead of time compiled
877 return result;
878 }
879
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700880 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700881 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700882 InvokeStub* result = GetFieldPtr<InvokeStub*>(
883 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
884 // TODO: DCHECK(result != NULL); should be ahead of time compiled
885 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700886 }
887
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700888 static MemberOffset GetInvokeStubOffset() {
889 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700890 }
891
buzbee561227c2011-09-02 15:28:19 -0700892 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
893 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
894 }
895
896 static MemberOffset GetDexCacheResolvedMethodsOffset() {
897 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
898 }
899
900 static MemberOffset GetMethodIndexOffset() {
901 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
902 }
903
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700904 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700905
Ian Rogers90865722011-09-19 11:11:44 -0700906 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700907 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700908 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700909
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700910 void SetCoreSpillMask(uint32_t core_spill_mask) {
911 // Computed during compilation
912 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_),
913 core_spill_mask, false);
914 }
915
Ian Rogers90865722011-09-19 11:11:44 -0700916 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700917 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
918 }
919
920 void SetFpSpillMask(uint32_t fp_spill_mask) {
921 // Computed during compilation
922 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_),
923 fp_spill_mask, false);
924 }
925
Ian Rogers90865722011-09-19 11:11:44 -0700926 // Is this a hand crafted method used for something like describing callee saves?
927 bool IsPhony() const {
928 bool result =
929 NULL == GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
930 // Check that if we do think it is phony it looks like the callee save method
931 DCHECK(!result || GetCoreSpillMask() != 0);
932 return result;
933 }
934
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700935 // Converts a native PC to a dex PC. TODO: this is a no-op
936 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700937 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938
939 // Converts a dex PC to a native PC. TODO: this is a no-op
940 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700941 uintptr_t ToNativePC(const uint32_t dex_pc) const;
942
943 // Find the catch block for the given exception type and dex_pc
944 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700945
946 static Class* GetJavaLangReflectMethod() {
947 DCHECK(java_lang_reflect_Method_ != NULL);
948 return java_lang_reflect_Method_;
949 }
950
951 static void SetClass(Class* java_lang_reflect_Method);
952 static void ResetClass();
953
954 private:
955 uint32_t GetReturnTypeIdx() const;
956
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
958 // the class we are a part of
959 Class* declaring_class_;
960 ObjectArray<Class>* java_exception_types_;
961 Object* java_formal_type_parameters_;
962 Object* java_generic_exception_types_;
963 Object* java_generic_parameter_types_;
964 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700965
Brian Carlstrom693267a2011-09-06 09:25:34 -0700966 String* name_;
967
968 ObjectArray<Class>* java_parameter_types_;
969
970 Class* java_return_type_; // Unused by ART
971
Brian Carlstrom693267a2011-09-06 09:25:34 -0700972 // Storage for code_
973 const ByteArray* code_array_;
974
975 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700976 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
977
978 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
979 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
980
981 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
982 ObjectArray<Field>* dex_cache_resolved_fields_;
983
984 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
985 ObjectArray<Method>* dex_cache_resolved_methods_;
986
Brian Carlstromdbc05252011-09-09 01:59:59 -0700987 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
988 ObjectArray<Class>* dex_cache_resolved_types_;
989
990 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
991 ObjectArray<String>* dex_cache_strings_;
992
993 // Storage for invoke_stub_
994 const ByteArray* invoke_stub_array_;
995
996 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -0700997 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700998
jeffhaoe23d93c2011-09-15 14:48:43 -0700999 // Byte arrays that hold data for the register maps
1000 const ByteArray* register_map_data_;
1001 const ByteArray* register_map_header_;
1002
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001003 // The short-form method descriptor string.
1004 String* shorty_;
1005
Brian Carlstromdbc05252011-09-09 01:59:59 -07001006 // The method descriptor. This represents the parameters a method
1007 // takes and value it returns. This string is a list of the type
1008 // descriptors for the parameters enclosed in parenthesis followed
1009 // by the return type descriptor. For example, for the method
1010 //
1011 // Object mymethod(int i, double d, Thread t)
1012 //
1013 // the method descriptor would be
1014 //
1015 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1016 String* signature_;
1017
1018 uint32_t java_generic_types_are_initialized_;
1019
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001020 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001021 uint32_t access_flags_;
1022
1023 // Compiled code associated with this method for callers from managed code.
1024 // May be compiled managed code or a bridge for invoking a native method.
1025 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001027 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001028 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001029
Brian Carlstrom693267a2011-09-06 09:25:34 -07001030 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001031 uint32_t core_spill_mask_;
1032
1033 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001034 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001035
Brian Carlstrom693267a2011-09-06 09:25:34 -07001036 // Total size in bytes of the frame
1037 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038
Brian Carlstrom693267a2011-09-06 09:25:34 -07001039 // Native invocation stub entry point for calling from native to managed code.
1040 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001041
Brian Carlstrom693267a2011-09-06 09:25:34 -07001042 // Index of the return type
1043 uint32_t java_return_type_idx_;
1044
Brian Carlstrom693267a2011-09-06 09:25:34 -07001045 // For concrete virtual methods, this is the offset of the method
1046 // in Class::vtable_.
1047 //
1048 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001049 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001050 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001051
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001052 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001053 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001054
Brian Carlstrom693267a2011-09-06 09:25:34 -07001055 // Method bounds; not needed for an abstract method.
1056 //
1057 // For a native method, we compute the size of the argument list, and
1058 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001059 uint32_t num_ins_;
1060 uint32_t num_outs_;
1061 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001062
Brian Carlstrom693267a2011-09-06 09:25:34 -07001063 // Method prototype descriptor string (return and argument types).
1064 uint32_t proto_idx_;
1065
1066 // Offset of return PC within frame for compiled code (in bytes)
1067 size_t return_pc_offset_in_bytes_;
1068
Brian Carlstrom693267a2011-09-06 09:25:34 -07001069 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001070
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001071 static Class* java_lang_reflect_Method_;
1072
Brian Carlstrom693267a2011-09-06 09:25:34 -07001073 friend class ImageWriter; // for relocating code_ and invoke_stub_
1074 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001075 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076};
1077
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001078class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001079 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001080 static size_t SizeOf(size_t component_count,
1081 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001082 return sizeof(Array) + component_count * component_size;
1083 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001084
Brian Carlstromb63ec392011-08-27 17:38:27 -07001085 // Given the context of a calling Method, use its DexCache to
1086 // resolve a type to an array Class. If it cannot be resolved, throw
1087 // an error. If it can, use it to create an array.
1088 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
1089
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001090 // A convenience for code that doesn't know the component size,
1091 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001092 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001093
Brian Carlstromb63ec392011-08-27 17:38:27 -07001094 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001095
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001096 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001097
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001098 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001099 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001100 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001103 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001104 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001105 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001106
buzbeec143c552011-08-20 17:38:58 -07001107 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001108 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001109 }
1110
1111 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001112 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001113 }
1114
Elliott Hughesbf86d042011-08-31 17:53:14 -07001115 void* GetRawData() {
1116 return reinterpret_cast<void*>(first_element_);
1117 }
1118
Elliott Hughes289da822011-08-16 10:11:20 -07001119 protected:
1120 bool IsValidIndex(int32_t index) const {
1121 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001122 Thread* self = Thread::Current();
1123 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1124 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001125 return false;
Elliott Hughes289da822011-08-16 10:11:20 -07001126 }
1127 return true;
1128 }
1129
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001130 private:
1131 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001132 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001133 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1134 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001135 // Marker for the data (used by generated code)
1136 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001137
Carl Shapirof88c9522011-08-06 15:47:38 -07001138 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001139};
1140
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001141template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001142class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001143 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001144 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001145
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001146 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001147
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001148 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001150 // Set element without bound and element type checks, to be used in limited
1151 // circumstances, such as during boot image writing
1152 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001153
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001154 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001155 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001157
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001158 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001159
1160 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001161 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001162};
1163
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001164template<class T>
1165ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1166 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1167}
1168
1169template<class T>
1170T* ObjectArray<T>::Get(int32_t i) const {
1171 if (!IsValidIndex(i)) {
1172 return NULL;
1173 }
1174 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1175 return GetFieldObject<T*>(data_offset, false);
1176}
1177
1178template<class T>
1179ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1180 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1181 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1182 return new_array;
1183}
1184
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001185// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001186// provides the static storage. However, this might change to an Array
1187// to improve image sharing, so we use this type to avoid assumptions
1188// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001189class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001191// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001192class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001193 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001194
1195 // Class Status
1196 //
1197 // kStatusNotReady: If a Class cannot be found in the class table by
1198 // FindClass, it allocates an new one with AllocClass in the
1199 // kStatusNotReady and calls LoadClass. Note if it does find a
1200 // class, it may not be kStatusResolved and it will try to push it
1201 // forward toward kStatusResolved.
1202 //
1203 // kStatusIdx: LoadClass populates with Class with information from
1204 // the DexFile, moving the status to kStatusIdx, indicating that the
1205 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001206 // populated based on super_class_type_idx_ and
1207 // interfaces_type_idx_. The new Class can then be inserted into the
1208 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001209 //
1210 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1211 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1212 // using ResolveClass to initialize the super_class_ and interfaces_.
1213 //
1214 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215 // shows linking is complete and fields of the Class populated by making
1216 // it kStatusResolved. Java allows circularities of the form where a super
1217 // class has a field that is of the type of the sub class. We need to be able
1218 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001219
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001221 kStatusError = -1,
1222 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001223 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001224 kStatusLoaded = 2, // DEX idx values resolved
1225 kStatusResolved = 3, // part of linking
1226 kStatusVerifying = 4, // in the process of being verified
1227 kStatusVerified = 5, // logically part of linking; done pre-init
1228 kStatusInitializing = 6, // class init in progress
1229 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001230 };
1231
1232 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001233 kPrimNot = 0,
1234 kPrimBoolean,
1235 kPrimByte,
1236 kPrimChar,
1237 kPrimShort,
1238 kPrimInt,
1239 kPrimLong,
1240 kPrimFloat,
1241 kPrimDouble,
1242 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001243 };
1244
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001245 Status GetStatus() const {
1246 CHECK(sizeof(Status) == sizeof(uint32_t));
1247 return static_cast<Status>(
1248 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1249 }
1250
1251 void SetStatus(Status new_status);
1252
1253 // Returns true if the class has failed to link.
1254 bool IsErroneous() const {
1255 return GetStatus() == kStatusError;
1256 }
1257
1258 // Returns true if the class has been loaded.
1259 bool IsIdxLoaded() const {
1260 return GetStatus() >= kStatusIdx;
1261 }
1262
1263 // Returns true if the class has been loaded.
1264 bool IsLoaded() const {
1265 return GetStatus() >= kStatusLoaded;
1266 }
1267
1268 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001269 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001270 return GetStatus() >= kStatusResolved;
1271 }
1272
1273 // Returns true if the class has been verified.
1274 bool IsVerified() const {
1275 return GetStatus() >= kStatusVerified;
1276 }
1277
1278 // Returns true if the class is initialized.
1279 bool IsInitialized() const {
1280 return GetStatus() == kStatusInitialized;
1281 }
1282
1283 uint32_t GetAccessFlags() const;
1284
1285 void SetAccessFlags(uint32_t new_access_flags) {
1286 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags,
1287 false);
1288 }
1289
1290 // Returns true if the class is an interface.
1291 bool IsInterface() const {
1292 return (GetAccessFlags() & kAccInterface) != 0;
1293 }
1294
1295 // Returns true if the class is declared public.
1296 bool IsPublic() const {
1297 return (GetAccessFlags() & kAccPublic) != 0;
1298 }
1299
1300 // Returns true if the class is declared final.
1301 bool IsFinal() const {
1302 return (GetAccessFlags() & kAccFinal) != 0;
1303 }
1304
1305 // Returns true if the class is abstract.
1306 bool IsAbstract() const {
1307 return (GetAccessFlags() & kAccAbstract) != 0;
1308 }
1309
1310 // Returns true if the class is an annotation.
1311 bool IsAnnotation() const {
1312 return (GetAccessFlags() & kAccAnnotation) != 0;
1313 }
1314
1315 // Returns true if the class is synthetic.
1316 bool IsSynthetic() const {
1317 return (GetAccessFlags() & kAccSynthetic) != 0;
1318 }
1319
1320 bool IsReferenceClass() const {
1321 return (GetAccessFlags() & kAccClassIsReference) != 0;
1322 }
1323
1324 bool IsWeakReferenceClass() const {
1325 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1326 }
1327
1328 bool IsSoftReferenceClass() const {
1329 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1330 }
1331
1332 bool IsFinalizerReferenceClass() const {
1333 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1334 }
1335
1336 bool IsPhantomReferenceClass() const {
1337 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1338 }
1339
1340 PrimitiveType GetPrimitiveType() const {
1341 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1342 return static_cast<PrimitiveType>(
1343 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1344 }
1345
1346 void SetPrimitiveType(PrimitiveType new_type) {
1347 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1348 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type,
1349 false);
1350 }
1351
1352 // Returns true if the class is a primitive type.
1353 bool IsPrimitive() const {
1354 return GetPrimitiveType() != kPrimNot;
1355 }
1356
1357 bool IsPrimitiveBoolean() const {
1358 return GetPrimitiveType() == kPrimBoolean;
1359 }
1360
1361 bool IsPrimitiveByte() const {
1362 return GetPrimitiveType() == kPrimByte;
1363 }
1364
1365 bool IsPrimitiveChar() const {
1366 return GetPrimitiveType() == kPrimChar;
1367 }
1368
1369 bool IsPrimitiveShort() const {
1370 return GetPrimitiveType() == kPrimShort;
1371 }
1372
1373 bool IsPrimitiveInt() const {
1374 return GetPrimitiveType() == kPrimInt;
1375 }
1376
1377 bool IsPrimitiveLong() const {
1378 return GetPrimitiveType() == kPrimLong;
1379 }
1380
1381 bool IsPrimitiveFloat() const {
1382 return GetPrimitiveType() == kPrimFloat;
1383 }
1384
1385 bool IsPrimitiveDouble() const {
1386 return GetPrimitiveType() == kPrimDouble;
1387 }
1388
1389 bool IsPrimitiveVoid() const {
1390 return GetPrimitiveType() == kPrimVoid;
1391 }
1392
1393 size_t PrimitiveSize() const;
1394
1395 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001396 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001397 }
1398
1399 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 return GetFieldObject<Class*>(
1401 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1402 }
1403
1404 void SetComponentType(Class* new_component_type) {
1405 DCHECK(GetComponentType() == NULL);
1406 DCHECK(new_component_type != NULL);
1407 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_),
1408 new_component_type, false);
1409 }
1410
1411 size_t GetComponentSize() const {
1412 return GetTypeSize(GetComponentType()->GetDescriptor());
1413 }
1414
1415 bool IsObjectClass() const {
1416 return !IsPrimitive() && GetSuperClass() == NULL;
1417 }
1418
Brian Carlstrom25c33252011-09-18 15:58:35 -07001419 // Tests whether a possibly null 'element' can be
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001420 // assigned into an array of type 'array_class'.
buzbee9a195c92011-09-16 13:26:02 -07001421 static void CanPutArrayElementFromCode(const Object* element, const Class* array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422
Brian Carlstromb63ec392011-08-27 17:38:27 -07001423 // Given the context of a calling Method, use its DexCache to
1424 // resolve a type to a Class. If it cannot be resolved, throw an
1425 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -07001426 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001427
Brian Carlstrom1f870082011-08-23 16:02:11 -07001428 // Creates a raw object instance but does not invoke the default constructor.
1429 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001430
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001432
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001433 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001434 const String* result = GetFieldObject<const String*>(
1435 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1436 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1437 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1438 return result;
1439 }
1440
1441 void SetDescriptor(String* new_descriptor);
1442
1443 bool IsVariableSize() const {
1444 // Classes and arrays vary in size, and so the object_size_ field cannot
1445 // be used to get their instance size
1446 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 }
1448
Brian Carlstrom4873d462011-08-21 15:23:39 -07001449 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001450 CHECK(sizeof(size_t) == sizeof(int32_t));
1451 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001452 }
1453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001454 size_t GetClassSize() const {
1455 CHECK(sizeof(size_t) == sizeof(uint32_t));
1456 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001457 }
1458
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001460 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001461 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001462 << " new_class_size=" << new_class_size
1463 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001464 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465 }
1466
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001467 size_t GetObjectSize() const {
1468 CHECK(!IsVariableSize());
1469 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001470 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001471 CHECK_GE(result, sizeof(Object));
1472 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 }
1474
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475 void SetObjectSize(size_t new_object_size) {
1476 DCHECK(!IsVariableSize());
1477 CHECK(sizeof(size_t) == sizeof(int32_t));
1478 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1479 new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001480 }
1481
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001482 // Returns true if this class is in the same packages as that class.
1483 bool IsInSamePackage(const Class* that) const;
1484
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001485 static bool IsInSamePackage(const String* descriptor1,
1486 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001487
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001488 // Returns true if this class can access that class.
1489 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001491 }
1492
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001493 bool IsAssignableFrom(const Class* src) const {
1494 DCHECK(src != NULL);
1495 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 // Can always assign to things of the same type
1497 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001498 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001499 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001500 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001502 return src->Implements(this);
1503 } else if (src->IsArrayClass()) {
1504 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001505 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001506 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001507 }
1508 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001509
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001510 Class* GetSuperClass() const {
1511 // Can only get super class for loaded classes (hack for when runtime is
1512 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001513 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001514 return GetFieldObject<Class*>(
1515 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1516 }
1517
buzbee4a3164f2011-09-03 11:25:10 -07001518 static MemberOffset SuperClassOffset() {
1519 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1520 }
1521
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001522 void SetSuperClass(Class *new_super_class) {
1523 // super class is assigned once, except during class linker initialization
1524 Class* old_super_class = GetFieldObject<Class*>(
1525 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1526 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1527 DCHECK(new_super_class != NULL);
1528 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_),
1529 new_super_class, false);
1530 }
1531
1532 bool HasSuperClass() const {
1533 return GetSuperClass() != NULL;
1534 }
1535
1536 uint32_t GetSuperClassTypeIdx() const {
1537 DCHECK(IsIdxLoaded());
1538 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1539 false);
1540 }
1541
1542 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
1543 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1544 new_super_class_idx, false);
1545 }
1546
1547 const ClassLoader* GetClassLoader() const;
1548
1549 void SetClassLoader(const ClassLoader* new_cl);
1550
1551 static MemberOffset DexCacheOffset() {
1552 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1553 }
1554
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001555 enum {
1556 kDumpClassFullDetail = 1,
1557 kDumpClassClassLoader = (1 << 1),
1558 kDumpClassInitialized = (1 << 2),
1559 };
1560
1561 void DumpClass(std::ostream& os, int flags);
1562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001563 DexCache* GetDexCache() const;
1564
1565 void SetDexCache(DexCache* new_dex_cache);
1566
1567 ObjectArray<Method>* GetDirectMethods() const {
1568 DCHECK(IsLoaded());
1569 return GetFieldObject<ObjectArray<Method>*>(
1570 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1571 }
1572
1573 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1574 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1575 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1576 DCHECK_NE(0, new_direct_methods->GetLength());
1577 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1578 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001579 }
1580
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001581 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001582 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001583 }
1584
1585 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001586 ObjectArray<Method>* direct_methods =
1587 GetFieldObject<ObjectArray<Method>*>(
1588 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1589 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001590 }
1591
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001592 // Returns the number of static, private, and constructor methods.
1593 size_t NumDirectMethods() const {
1594 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1595 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001596
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001597 ObjectArray<Method>* GetVirtualMethods() const {
1598 DCHECK(IsLoaded());
1599 return GetFieldObject<ObjectArray<Method>*>(
1600 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1601 }
1602
1603 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1604 // TODO: we reassign virtual methods to grow the table for miranda
1605 // methods.. they should really just be assigned once
1606 DCHECK_NE(0, new_virtual_methods->GetLength());
1607 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1608 new_virtual_methods, false);
1609 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001610
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001611 // Returns the number of non-inherited virtual methods.
1612 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001613 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001614 }
1615
1616 Method* GetVirtualMethod(uint32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001617 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001618 return GetVirtualMethods()->Get(i);
1619 }
1620
1621 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
1622 DCHECK(IsLoaded());
1623 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001624 }
1625
1626 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001627 ObjectArray<Method>* virtual_methods =
1628 GetFieldObject<ObjectArray<Method>*>(
1629 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1630 virtual_methods->Set(i, f);
1631 }
1632
1633 ObjectArray<Method>* GetVTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001634 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 return GetFieldObject<ObjectArray<Method>*>(
1636 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1637 }
1638
1639 ObjectArray<Method>* GetVTableDuringLinking() const {
1640 DCHECK(IsLoaded());
1641 return GetFieldObject<ObjectArray<Method>*>(
1642 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1643 }
1644
1645 void SetVTable(ObjectArray<Method>* new_vtable) {
1646 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1647 }
1648
1649 static MemberOffset VTableOffset() {
1650 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001651 }
1652
Brian Carlstrom30b94452011-08-25 21:35:26 -07001653 // Given a method implemented by this class but potentially from a
1654 // super class, return the specific implementation
1655 // method for this class.
1656 Method* FindVirtualMethodForVirtual(Method* method) {
1657 DCHECK(!method->GetDeclaringClass()->IsInterface());
1658 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001659 // Use the index to a potentially overridden one for this instance's class.
1660 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001661 }
1662
1663 // Given a method implemented by this class, but potentially from a
1664 // super class or interface, return the specific implementation
1665 // method for this class.
1666 Method* FindVirtualMethodForInterface(Method* method);
1667
jeffhaobdb76512011-09-07 11:43:16 -07001668 Method* FindInterfaceMethod(const StringPiece& name,
1669 const StringPiece& descriptor);
1670
Brian Carlstrom30b94452011-08-25 21:35:26 -07001671 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1672 if (method->GetDeclaringClass()->IsInterface()) {
1673 return FindVirtualMethodForInterface(method);
1674 }
1675 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001676 }
1677
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001678 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001679 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001680
1681 Method* FindVirtualMethod(const StringPiece& name,
1682 const StringPiece& descriptor);
1683
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684 Method* FindDeclaredDirectMethod(const StringPiece& name,
1685 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001686
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001687 Method* FindDirectMethod(const StringPiece& name,
1688 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001689
Carl Shapiro69759ea2011-07-21 18:13:35 -07001690 size_t NumInterfaces() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001691 CHECK(IsIdxLoaded()); // used during loading
1692 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1693 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1694 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001695 }
1696
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001697 IntArray* GetInterfacesTypeIdx() const {
1698 CHECK(IsIdxLoaded());
1699 return GetFieldObject<IntArray*>(
1700 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1701 }
1702
1703 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1704
1705 ObjectArray<Class>* GetInterfaces() const {
1706 CHECK(IsLoaded());
1707 return GetFieldObject<ObjectArray<Class>*>(
1708 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1709 }
1710
1711 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1712 DCHECK(NULL == GetFieldObject<Object*>(
1713 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
1714 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_),
1715 new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001716 }
1717
1718 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001719 DCHECK_NE(NumInterfaces(), 0U);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001720 ObjectArray<Class>* interfaces =
1721 GetFieldObject<ObjectArray<Class>*>(
1722 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1723 interfaces->Set(i, f);
1724 }
1725
1726 Class* GetInterface(uint32_t i) const {
1727 DCHECK_NE(NumInterfaces(), 0U);
1728 return GetInterfaces()->Get(i);
1729 }
1730
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001731 int32_t GetIfTableCount() const {
1732 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1733 if (iftable == NULL) {
1734 return 0;
1735 }
1736 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001737 }
1738
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001739 ObjectArray<InterfaceEntry>* GetIfTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001740 DCHECK(IsResolved());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001741 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001742 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1743 }
1744
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001745 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001746 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747 }
1748
1749 // Get instance fields
1750 ObjectArray<Field>* GetIFields() const {
1751 DCHECK(IsLoaded());
1752 return GetFieldObject<ObjectArray<Field>*>(
1753 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1754 }
1755
1756 void SetIFields(ObjectArray<Field>* new_ifields) {
1757 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1758 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
1759 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_),
1760 new_ifields, false);
1761 }
1762
1763 size_t NumInstanceFields() const {
1764 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1765 }
1766
1767 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1768 DCHECK_NE(NumInstanceFields(), 0U);
1769 return GetIFields()->Get(i);
1770 }
1771
1772 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1773 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1774 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1775 ifields->Set(i, f);
1776 }
1777
1778 // Returns the number of instance fields containing reference types.
1779 size_t NumReferenceInstanceFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001780 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001781 DCHECK(sizeof(size_t) == sizeof(int32_t));
1782 return GetField32(
1783 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1784 }
1785
1786 size_t NumReferenceInstanceFieldsDuringLinking() const {
1787 DCHECK(IsLoaded());
1788 DCHECK(sizeof(size_t) == sizeof(int32_t));
1789 return GetField32(
1790 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1791 }
1792
1793 void SetNumReferenceInstanceFields(size_t new_num) {
1794 DCHECK(sizeof(size_t) == sizeof(int32_t));
1795 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_),
1796 new_num, false);
1797 }
1798
1799 uint32_t GetReferenceInstanceOffsets() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001800 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001801 return GetField32(
1802 OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
1803 }
1804
1805 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1806
1807 // Beginning of static field data
1808 static MemberOffset FieldsOffset() {
1809 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1810 }
1811
1812 // Returns the number of static fields containing reference types.
1813 size_t NumReferenceStaticFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001814 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001815 DCHECK(sizeof(size_t) == sizeof(int32_t));
1816 return GetField32(
1817 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1818 }
1819
1820 size_t NumReferenceStaticFieldsDuringLinking() const {
1821 DCHECK(IsLoaded());
1822 DCHECK(sizeof(size_t) == sizeof(int32_t));
1823 return GetField32(
1824 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1825 }
1826
1827 void SetNumReferenceStaticFields(size_t new_num) {
1828 DCHECK(sizeof(size_t) == sizeof(int32_t));
1829 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_),
1830 new_num, false);
1831 }
1832
1833 ObjectArray<Field>* GetSFields() const {
1834 DCHECK(IsLoaded());
1835 return GetFieldObject<ObjectArray<Field>*>(
1836 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1837 }
1838
1839 void SetSFields(ObjectArray<Field>* new_sfields) {
1840 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1841 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
1842 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_),
1843 new_sfields, false);
1844 }
1845
1846 size_t NumStaticFields() const {
1847 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1848 }
1849
1850 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1851 return GetSFields()->Get(i);
1852 }
1853
1854 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1855 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1856 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1857 sfields->Set(i, f);
1858 }
1859
1860 uint32_t GetReferenceStaticOffsets() const {
1861 return GetField32(
1862 OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
1863 }
1864
1865 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1866
1867 // Finds the given instance field in this class or a superclass.
1868 Field* FindInstanceField(const StringPiece& name, Class* type);
1869
1870 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1871
1872 // Finds the given static field in this class or a superclass.
1873 Field* FindStaticField(const StringPiece& name, Class* type);
1874
1875 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1876
Elliott Hughesdcc24742011-09-07 14:02:44 -07001877 pid_t GetClinitThreadId() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001878 DCHECK(IsIdxLoaded());
1879 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1880 }
1881
Elliott Hughesdcc24742011-09-07 14:02:44 -07001882 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001883 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_),
1884 new_clinit_thread_id, false);
1885 }
1886
1887 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001888 // DCHECK(IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 return GetFieldObject<Class*>(
1890 OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001891 }
1892
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001893 void SetVerifyErrorClass(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001894 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_),
1895 klass, false);
1896 }
1897
Brian Carlstromc74255f2011-09-11 22:47:39 -07001898 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001899
Brian Carlstromc74255f2011-09-11 22:47:39 -07001900 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001901
jeffhaobdb76512011-09-07 11:43:16 -07001902 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001903 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001904 bool IsArrayAssignableFromArray(const Class* klass) const;
1905 bool IsAssignableFromArray(const Class* klass) const;
1906 bool IsSubClass(const Class* klass) const;
1907
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001908 // descriptor for the class such as "java.lang.Class" or "[C"
1909 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001910
Brian Carlstrom693267a2011-09-06 09:25:34 -07001911 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001912 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001913
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001914 // For array classes, the component class object for instanceof/checkcast
1915 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001916 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001917
Brian Carlstrom693267a2011-09-06 09:25:34 -07001918 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1919 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001920
Brian Carlstrom693267a2011-09-06 09:25:34 -07001921 // DexCache of resolved constant pool entries
1922 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1923 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001924
1925 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001926 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001927
Brian Carlstrom693267a2011-09-06 09:25:34 -07001928 // instance fields
1929 //
1930 // These describe the layout of the contents of an Object.
1931 // Note that only the fields directly declared by this class are
1932 // listed in ifields; fields declared by a superclass are listed in
1933 // the superclass's Class.ifields.
1934 //
1935 // All instance fields that refer to objects are guaranteed to be at
1936 // the beginning of the field list. num_reference_instance_fields_
1937 // specifies the number of reference fields.
1938 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001939
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001940 // Interface table (iftable_), one entry per interface supported by
1941 // this class. That means one entry for each interface we support
1942 // directly, indirectly via superclass, or indirectly via
1943 // superinterface. This will be null if neither we nor our
1944 // superclass implement any interfaces.
1945 //
1946 // Why we need this: given "class Foo implements Face", declare
1947 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1948 // is part of the Face interface. We can't easily use a single
1949 // vtable.
1950 //
1951 // For every interface a concrete class implements, we create an array
1952 // of the concrete vtable_ methods for the methods in the interface.
1953 ObjectArray<InterfaceEntry>* iftable_;
1954
Brian Carlstromdbc05252011-09-09 01:59:59 -07001955 // array of interfaces this class implements directly
1956 // see also interfaces_type_idx_
1957 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001958
1959 // array of type_idx's for interfaces this class implements directly
1960 // see also interfaces_
1961 IntArray* interfaces_type_idx_;
1962
Brian Carlstromdbc05252011-09-09 01:59:59 -07001963 // Static fields
1964 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001965
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001966 // source file name, if known. Otherwise, NULL.
1967 String* source_file_;
1968
Brian Carlstromdbc05252011-09-09 01:59:59 -07001969 // The superclass, or NULL if this is java.lang.Object or a
1970 // primitive type.
1971 // see also super_class_type_idx_;
1972 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001973
Brian Carlstromdbc05252011-09-09 01:59:59 -07001974 // If class verify fails, we must return same error on subsequent tries.
1975 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1976 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001977
Brian Carlstromdbc05252011-09-09 01:59:59 -07001978 // virtual methods defined in this class; invoked through vtable
1979 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001980
Brian Carlstromdbc05252011-09-09 01:59:59 -07001981 // Virtual method table (vtable), for use by "invoke-virtual". The
1982 // vtable from the superclass is copied in, and virtual methods from
1983 // our class either replace those from the super or are appended.
1984 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001985
Brian Carlstromdbc05252011-09-09 01:59:59 -07001986 // access flags; low 16 bits are defined by VM spec
1987 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001988
Brian Carlstromdbc05252011-09-09 01:59:59 -07001989 // Total class size; used when allocating storage on gc heap.
1990 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001991
Elliott Hughes5f791332011-09-15 17:45:30 -07001992 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001993 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001994
Brian Carlstromdbc05252011-09-09 01:59:59 -07001995 // number of instance fields that are object refs
1996 size_t num_reference_instance_fields_;
1997
1998 // number of static fields that are object refs
1999 size_t num_reference_static_fields_;
2000
2001 // Total object size; used when allocating storage on gc heap.
2002 // (For interfaces and abstract classes this will be zero.)
2003 size_t object_size_;
2004
2005 // primitive type index, or kPrimNot (0); set for generated prim classes
2006 PrimitiveType primitive_type_;
2007
2008 // Bitmap of offsets of ifields.
2009 uint32_t reference_instance_offsets_;
2010
2011 // Bitmap of offsets of sfields.
2012 uint32_t reference_static_offsets_;
2013
Brian Carlstrom693267a2011-09-06 09:25:34 -07002014 // state of class initialization
2015 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002016
Brian Carlstrom693267a2011-09-06 09:25:34 -07002017 // Set in LoadClass, used to LinkClass
2018 // see also super_class_
2019 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002020
Brian Carlstrom693267a2011-09-06 09:25:34 -07002021 // TODO: ?
2022 // initiating class loader list
2023 // NOTE: for classes with low serialNumber, these are unused, and the
2024 // values are kept in a table in gDvm.
2025 // InitiatingLoaderList initiating_loader_list_;
2026
Brian Carlstrom4873d462011-08-21 15:23:39 -07002027 // Location of first static field.
2028 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002029
Brian Carlstrom693267a2011-09-06 09:25:34 -07002030 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002031 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002032};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002033
Elliott Hughes1f359b02011-07-17 14:27:17 -07002034std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002035
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002036inline void Object::SetClass(Class* new_klass) {
2037 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002038 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002039}
2040
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002041inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002042 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002043 DCHECK(GetClass() != NULL);
2044 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002045}
2046
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002047inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002048 Class* java_lang_Class = GetClass()->GetClass();
2049 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002050}
2051
Brian Carlstrom4873d462011-08-21 15:23:39 -07002052inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002053 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002054 return this == java_lang_Class;
2055}
2056
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002057inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002058 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002059}
2060
Brian Carlstromb63ec392011-08-27 17:38:27 -07002061inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002062 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002063}
2064
Brian Carlstroma663ea52011-08-19 23:33:41 -07002065inline bool Object::IsField() const {
2066 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002067 Class* java_lang_reflect_Field =
2068 java_lang_Class->GetInstanceField(0)->GetClass();
2069 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002070}
2071
2072inline bool Object::IsMethod() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002073 Class* java_lang_Class = GetClass()->GetClass();
2074 Class* java_lang_reflect_Method =
2075 java_lang_Class->GetDirectMethod(0)->GetClass();
2076 return GetClass() == java_lang_reflect_Method;
2077}
2078
2079inline bool Object::IsReferenceInstance() const {
2080 return GetClass()->IsReferenceClass();
2081}
2082
2083inline bool Object::IsWeakReferenceInstance() const {
2084 return GetClass()->IsWeakReferenceClass();
2085}
2086
2087inline bool Object::IsSoftReferenceInstance() const {
2088 return GetClass()->IsSoftReferenceClass();
2089}
2090
2091inline bool Object::IsFinalizerReferenceInstance() const {
2092 return GetClass()->IsFinalizerReferenceClass();
2093}
2094
2095inline bool Object::IsPhantomReferenceInstance() const {
2096 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002097}
2098
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002099inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002100 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002101 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002102 result = AsArray()->SizeOf();
2103 } else if (IsClass()) {
2104 result = AsClass()->SizeOf();
2105 } else {
2106 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002107 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002108 DCHECK(!IsField() || result == sizeof(Field));
2109 DCHECK(!IsMethod() || result == sizeof(Method));
2110 return result;
2111}
2112
2113inline void Field::SetOffset(MemberOffset num_bytes) {
2114 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002115 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002116 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2117 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002118 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002119 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2120 num_bytes.Uint32Value(), false);
2121}
2122
2123inline Class* Field::GetDeclaringClass() const {
2124 Class* result = GetFieldObject<Class*>(
2125 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2126 DCHECK(result != NULL);
2127 DCHECK(result->IsLoaded());
2128 return result;
2129}
2130
2131inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2132 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2133 new_declaring_class, false);
2134}
2135
2136inline Class* Method::GetDeclaringClass() const {
2137 Class* result =
2138 GetFieldObject<Class*>(
2139 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2140 DCHECK(result != NULL);
2141 DCHECK(result->IsLoaded());
2142 return result;
2143}
2144
2145inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2146 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2147 new_declaring_class, false);
2148}
2149
2150inline uint32_t Method::GetReturnTypeIdx() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002151 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002152 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2153 false);
2154}
2155
2156inline bool Method::IsReturnAReference() const {
2157 return !GetReturnType()->IsPrimitive();
2158}
2159
2160inline bool Method::IsReturnAFloat() const {
2161 return GetReturnType()->IsPrimitiveFloat();
2162}
2163
2164inline bool Method::IsReturnADouble() const {
2165 return GetReturnType()->IsPrimitiveDouble();
2166}
2167
2168inline bool Method::IsReturnALong() const {
2169 return GetReturnType()->IsPrimitiveLong();
2170}
2171
2172inline bool Method::IsReturnVoid() const {
2173 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002174}
2175
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002176inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002177 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2178}
2179
2180template<class T>
2181void ObjectArray<T>::Set(int32_t i, T* object) {
2182 if (IsValidIndex(i)) {
2183 if (object != NULL) {
2184 Class* element_class = GetClass()->GetComponentType();
2185 DCHECK(!element_class->IsPrimitive());
2186 // TODO: ArrayStoreException
2187 CHECK(object->InstanceOf(element_class));
2188 }
2189 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2190 SetFieldObject(data_offset, object, false);
2191 }
2192}
2193
2194template<class T>
2195void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2196 DCHECK(IsValidIndex(i));
2197 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2198 SetFieldObject(data_offset, object, false);
2199}
2200
2201template<class T>
2202void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2203 ObjectArray<T>* dst, int dst_pos,
2204 size_t length) {
2205 if (src->IsValidIndex(src_pos) &&
2206 src->IsValidIndex(src_pos+length-1) &&
2207 dst->IsValidIndex(dst_pos) &&
2208 dst->IsValidIndex(dst_pos+length-1)) {
2209 MemberOffset src_offset(DataOffset().Int32Value() +
2210 src_pos * sizeof(Object*));
2211 MemberOffset dst_offset(DataOffset().Int32Value() +
2212 dst_pos * sizeof(Object*));
2213 Class* array_class = dst->GetClass();
2214 if (array_class == src->GetClass()) {
2215 // No need for array store checks if arrays are of the same type
2216 for (size_t i=0; i < length; i++) {
2217 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2218 dst->SetFieldObject(dst_offset, object, false);
2219 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2220 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2221 }
2222 } else {
2223 Class* element_class = array_class->GetComponentType();
2224 CHECK(!element_class->IsPrimitive());
2225 for (size_t i=0; i < length; i++) {
2226 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2227 // TODO: ArrayStoreException
2228 CHECK(object == NULL || object->InstanceOf(element_class));
2229 dst->SetFieldObject(dst_offset, object, false);
2230 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2231 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2232 }
2233 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002234 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002235 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002236}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002237
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002238class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002239 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002240 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002241 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002242 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002243 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2244};
2245
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002246class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002247 private:
2248 CharArray* ASCII_;
2249 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002250 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002251 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002252 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002253 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2254};
2255
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002256class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002257 private:
2258 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2259 uint32_t TYPE_BOOLEAN_;
2260 uint32_t TYPE_BYTE_;
2261 uint32_t TYPE_CHAR_;
2262 uint32_t TYPE_DOUBLE_;
2263 uint32_t TYPE_FLOAT_;
2264 uint32_t TYPE_INTEGER_;
2265 uint32_t TYPE_LONG_;
2266 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002267 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002268 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2269};
2270
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002271class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002272 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002273 ObjectArray<Object>* NO_ANNOTATIONS_;
2274 Object* ORDER_BY_SIGNATURE_;
2275 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002276 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2277};
2278
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002279template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002280class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002281 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002282 typedef T ElementType;
2283
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002284 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002285
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002286 const T* GetData() const {
2287 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002288 }
2289
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002290 T* GetData() {
2291 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002292 }
2293
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002294 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002295 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002296 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002297 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002298 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002299 }
2300
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002301 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002302 // TODO: ArrayStoreException
2303 if (IsValidIndex(i)) {
2304 GetData()[i] = value;
2305 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002306 }
2307
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002308 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002309 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002310 CHECK(array_class != NULL);
2311 array_class_ = array_class;
2312 }
2313
Brian Carlstroma663ea52011-08-19 23:33:41 -07002314 static void ResetArrayClass() {
2315 CHECK(array_class_ != NULL);
2316 array_class_ = NULL;
2317 }
2318
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002319 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002320 // Location of first element.
2321 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002322
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002323 static Class* array_class_;
2324
Carl Shapirof88c9522011-08-06 15:47:38 -07002325 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002326};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002327
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002328inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2329 DCHECK(NULL == GetFieldObject<IntArray*>(
2330 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2331 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2332 new_interfaces_idx, false);
2333}
2334
2335// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002336class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002337 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002338 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002339 const CharArray* result = GetFieldObject<const CharArray*>(
2340 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2341 DCHECK(result != NULL);
2342 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002343 }
2344
Elliott Hughes814e4032011-08-23 12:07:56 -07002345 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002346 int32_t result = GetField32(
2347 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2348 DCHECK_LE(0, result);
2349 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002350 }
2351
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002352 int32_t GetLength() const;
2353
2354 int32_t GetHashCode() const;
2355
2356 void ComputeHashCode() {
2357 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002358 }
2359
Elliott Hughes814e4032011-08-23 12:07:56 -07002360 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002361 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002362 }
2363
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002364 uint16_t CharAt(int32_t index) const;
2365
Brian Carlstromc74255f2011-09-11 22:47:39 -07002366 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002367
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002368 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002369 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002370 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002371
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002372 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002373
Jesse Wilson8989d992011-08-02 13:39:42 -07002374 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002375 const char* utf8_data_in);
2376
2377 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2378
2379 static String* Alloc(Class* java_lang_String, CharArray* array);
2380
2381 bool Equals(const char* modified_utf8) const;
2382
2383 // TODO: do we need this overload? give it a more intention-revealing name.
2384 bool Equals(const StringPiece& modified_utf8) const;
2385
2386 bool Equals(const String* that) const;
2387
2388 // TODO: do we need this overload? give it a more intention-revealing name.
2389 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2390 int32_t that_length) const;
2391
2392 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2393 std::string ToModifiedUtf8() const;
2394
2395 static Class* GetJavaLangString() {
2396 DCHECK(java_lang_String_ != NULL);
2397 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002398 }
2399
Brian Carlstroma663ea52011-08-19 23:33:41 -07002400 static void SetClass(Class* java_lang_String);
2401 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002402
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002403 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002404 void SetHashCode(int32_t new_hash_code) {
2405 DCHECK_EQ(0u,
2406 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2407 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2408 new_hash_code, false);
2409 }
2410
2411 void SetCount(int32_t new_count) {
2412 DCHECK_LE(0, new_count);
2413 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2414 }
2415
2416 void SetOffset(int32_t new_offset) {
2417 DCHECK_LE(0, new_offset);
2418 DCHECK_GE(GetLength(), new_offset);
2419 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2420 }
2421
2422 void SetArray(CharArray* new_array) {
2423 DCHECK(new_array != NULL);
2424 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2425 }
2426
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002427 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2428 CharArray* array_;
2429
Brian Carlstromdbc05252011-09-09 01:59:59 -07002430 int32_t count_;
2431
Carl Shapirof88c9522011-08-06 15:47:38 -07002432 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002433
Elliott Hughes289da822011-08-16 10:11:20 -07002434 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002435
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002436 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002437
Brian Carlstrom693267a2011-09-06 09:25:34 -07002438 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002439 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002440};
2441
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002442inline const String* Field::GetName() const {
2443 DCHECK(GetDeclaringClass()->IsLoaded());
2444 String* result =
2445 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2446 DCHECK(result != NULL);
2447 return result;
2448}
2449
2450inline void Field::SetName(String* new_name) {
2451 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2452 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002453}
2454
2455inline uint32_t Field::GetAccessFlags() const {
2456 DCHECK(GetDeclaringClass()->IsLoaded());
2457 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2458}
2459
2460inline uint32_t Field::GetTypeIdx() const {
2461 DCHECK(GetDeclaringClass()->IsIdxLoaded());
2462 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2463}
2464
2465inline MemberOffset Field::GetOffset() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002466 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002467 return MemberOffset(
2468 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2469}
2470
2471inline MemberOffset Field::GetOffsetDuringLinking() const {
2472 DCHECK(GetDeclaringClass()->IsLoaded());
2473 return MemberOffset(
2474 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2475}
2476
2477inline const String* Method::GetName() const {
2478 DCHECK(GetDeclaringClass()->IsLoaded());
2479 const String* result =
2480 GetFieldObject<const String*>(
2481 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2482 DCHECK(result != NULL);
2483 return result;
2484}
2485
2486inline void Method::SetName(String* new_name) {
2487 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2488 new_name, false);
2489
2490}
2491
jeffhaoe23d93c2011-09-15 14:48:43 -07002492inline ByteArray* Method::GetRegisterMapData() const {
2493 return GetFieldObject<ByteArray*>(
2494 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2495}
2496
jeffhaoa0a764a2011-09-16 10:43:38 -07002497inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002498 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002499 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002500}
2501
2502inline ByteArray* Method::GetRegisterMapHeader() const {
2503 return GetFieldObject<ByteArray*>(
2504 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2505}
2506
jeffhaoa0a764a2011-09-16 10:43:38 -07002507inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002508 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002509 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002510}
2511
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002512inline String* Method::GetShorty() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002513 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002514 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002515 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2516}
2517
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002518inline void Method::SetShorty(String* new_shorty) {
2519 DCHECK(NULL == GetFieldObject<String*>(
2520 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2521 DCHECK_LE(1, new_shorty->GetLength());
2522 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2523}
2524
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002525inline const String* Method::GetSignature() const {
2526 DCHECK(GetDeclaringClass()->IsLoaded());
2527 const String* result =
2528 GetFieldObject<const String*>(
2529 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2530 DCHECK(result != NULL);
2531 return result;
2532}
2533
2534inline void Method::SetSignature(String* new_signature) {
2535 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2536 new_signature, false);
2537}
2538
2539inline uint32_t Class::GetAccessFlags() const {
2540 // Check class is loaded or this is java.lang.String that has a
2541 // circularity issue during loading the names of its members
2542 DCHECK(IsLoaded() || this == String::GetJavaLangString() ||
2543 this == Field::GetJavaLangReflectField() ||
Brian Carlstrom25c33252011-09-18 15:58:35 -07002544 this == Method::GetJavaLangReflectMethod()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002545 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2546}
2547
2548inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002549 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002550 DCHECK(new_descriptor != NULL);
2551 DCHECK_NE(0, new_descriptor->GetLength());
2552 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2553 new_descriptor, false);
2554}
2555
Brian Carlstromc74255f2011-09-11 22:47:39 -07002556inline String* Class::GetSourceFile() const {
2557 DCHECK(IsLoaded());
2558 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2559}
2560
2561inline void Class::SetSourceFile(String* new_source_file) {
2562 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2563}
2564
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002565inline uint32_t Method::GetAccessFlags() const {
2566 DCHECK(GetDeclaringClass()->IsLoaded());
2567 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2568}
2569
2570inline uint16_t Method::GetMethodIndex() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002571 DCHECK(GetDeclaringClass()->IsResolved());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002572 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002573}
2574
2575inline uint16_t Method::NumRegisters() const {
2576 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002577 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002578}
2579
2580inline uint16_t Method::NumIns() const {
2581 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002582 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002583}
2584
2585inline uint16_t Method::NumOuts() const {
2586 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002587 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588}
2589
2590inline uint32_t Method::GetProtoIdx() const {
2591 DCHECK(GetDeclaringClass()->IsLoaded());
2592 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2593}
2594
2595// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002596class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002597 public:
2598 void SetDetailMessage(String* new_detail_message) {
2599 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2600 new_detail_message, false);
2601 }
2602
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002603 private:
2604 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2605 Throwable* cause_;
2606 String* detail_message_;
2607 Object* stack_state_; // Note this is Java volatile:
2608 Object* stack_trace_;
2609 Object* suppressed_exceptions_;
2610
Brian Carlstrom693267a2011-09-06 09:25:34 -07002611 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002612 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2613};
2614
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002615// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002616class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002617 public:
2618 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002619 return GetFieldObject<const String*>(
2620 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002621 }
2622
2623 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002624 return GetFieldObject<const String*>(
2625 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002626 }
2627
2628 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002629 return GetFieldObject<const String*>(
2630 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002631 }
2632
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002633 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002634 return GetField32(
2635 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002636 }
2637
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002638 static StackTraceElement* Alloc(const String* declaring_class,
2639 const String* method_name,
2640 const String* file_name,
2641 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002642
2643 static void SetClass(Class* java_lang_StackTraceElement);
2644
2645 static void ResetClass();
2646
2647 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002648 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002649 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002650 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002651 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002652 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002653
2654 static Class* GetStackTraceElement() {
2655 DCHECK(java_lang_StackTraceElement_ != NULL);
2656 return java_lang_StackTraceElement_;
2657 }
2658
2659 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002660
2661 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002662 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2663};
2664
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002665class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002666 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002667 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002668 Class* interface = Get(kInterface)->AsClass();
2669 DCHECK(interface != NULL);
2670 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002671 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002672
Brian Carlstrom30b94452011-08-25 21:35:26 -07002673 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002674 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002675 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002676 DCHECK(Get(kInterface) == NULL);
2677 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002678 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002679
Brian Carlstrom86927212011-09-15 11:31:11 -07002680 size_t GetMethodArrayCount() const {
2681 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2682 if (method_array == 0) {
2683 return 0;
2684 }
2685 return method_array->GetLength();
2686 }
2687
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002688 ObjectArray<Method>* GetMethodArray() const {
2689 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2690 DCHECK(method_array != NULL);
2691 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002692 }
2693
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002694 void SetMethodArray(ObjectArray<Method>* new_ma) {
2695 DCHECK(new_ma != NULL);
2696 DCHECK(Get(kMethodArray) == NULL);
2697 Set(kMethodArray, new_ma);
2698 }
2699
2700 static size_t LengthAsArray() {
2701 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002702 }
2703
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002704 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002705
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002706 enum ArrayIndex {
2707 // Points to the interface class.
2708 kInterface = 0,
2709 // Method pointers into the vtable, allow fast map from interface
2710 // method index to concrete instance method.
2711 kMethodArray = 1,
2712 kMax = 2,
2713 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002714
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002715 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002716};
2717
2718} // namespace art
2719
2720#endif // ART_SRC_OBJECT_H_