blob: 02c98d8328d4008837beb5400a74a07550da21d1 [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
Brian Carlstrom1f870082011-08-23 16:02:11 -070020#include <vector>
21
Elliott Hughes90a33692011-08-30 13:27:07 -070022#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070023#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070024#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070025#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070031#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070034#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070035
36namespace art {
37
38class Array;
39class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070040class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070041class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070042class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040043class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070044class InterfaceEntry;
45class Monitor;
46class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070047class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070048class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040049class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070050template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070051template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070052typedef PrimitiveArray<uint8_t> BooleanArray;
53typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070054typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070055typedef PrimitiveArray<double> DoubleArray;
56typedef PrimitiveArray<float> FloatArray;
57typedef PrimitiveArray<int32_t> IntArray;
58typedef PrimitiveArray<int64_t> LongArray;
59typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070060
Carl Shapiro3ee755d2011-06-28 12:11:04 -070061union JValue {
62 uint8_t z;
63 int8_t b;
64 uint16_t c;
65 int16_t s;
66 int32_t i;
67 int64_t j;
68 float f;
69 double d;
70 Object* l;
71};
72
Brian Carlstrombe977852011-07-19 14:54:54 -070073static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
74static const uint32_t kAccPrivate = 0x0002; // field, method, ic
75static const uint32_t kAccProtected = 0x0004; // field, method, ic
76static const uint32_t kAccStatic = 0x0008; // field, method, ic
77static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
78static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
79static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
80static const uint32_t kAccVolatile = 0x0040; // field
81static const uint32_t kAccBridge = 0x0040; // method (1.5)
82static const uint32_t kAccTransient = 0x0080; // field
83static const uint32_t kAccVarargs = 0x0080; // method (1.5)
84static const uint32_t kAccNative = 0x0100; // method
85static const uint32_t kAccInterface = 0x0200; // class, ic
86static const uint32_t kAccAbstract = 0x0400; // class, method, ic
87static const uint32_t kAccStrict = 0x0800; // method
88static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
89static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
90static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070091
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070092static const uint32_t kAccMiranda = 0x8000; // method
93
Brian Carlstroma331b3c2011-07-18 17:47:56 -070094static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
95
Brian Carlstrombe977852011-07-19 14:54:54 -070096static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
97static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070098static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070099
Brian Carlstrom1f870082011-08-23 16:02:11 -0700100static const uint32_t kAccClassFlagsMask = (kAccPublic
101 | kAccFinal
102 | kAccInterface
103 | kAccAbstract
104 | kAccSynthetic
105 | kAccAnnotation
106 | kAccEnum);
107static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
108 | kAccPrivate
109 | kAccProtected
110 | kAccStatic);
111static const uint32_t kAccFieldFlagsMask = (kAccPublic
112 | kAccPrivate
113 | kAccProtected
114 | kAccStatic
115 | kAccFinal
116 | kAccVolatile
117 | kAccTransient
118 | kAccSynthetic
119 | kAccEnum);
120static const uint32_t kAccMethodFlagsMask = (kAccPublic
121 | kAccPrivate
122 | kAccProtected
123 | kAccStatic
124 | kAccFinal
125 | kAccSynchronized
126 | kAccBridge
127 | kAccVarargs
128 | kAccNative
129 | kAccAbstract
130 | kAccStrict
131 | kAccSynthetic
132 | kAccConstructor
133 | kAccDeclaredSynchronized);
134
135// if only kAccClassIsReference is set, we have a soft reference
136static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
137static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
138static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
139static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
140
141static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
142 | kAccClassIsWeakReference
143 | kAccClassIsFinalizerReference
144 | kAccClassIsPhantomReference);
145
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700147 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700148 */
149/*
150 * A magic value for refOffsets. Ignore the bits and walk the super
151 * chain when this is the value.
152 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
153 * fields followed by 2 ref instance fields.]
154 */
155#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
157#define CLASS_OFFSET_ALIGNMENT 4
158#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
159/*
160 * Given an offset, return the bit number which would encode that offset.
161 * Local use only.
162 */
163#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700164 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165 CLASS_OFFSET_ALIGNMENT)
166/*
167 * Is the given offset too large to be encoded?
168 */
169#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
170 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
171/*
172 * Return a single bit, encoding the offset.
173 * Undefined if the offset is too large, as defined above.
174 */
175#define CLASS_BIT_FROM_OFFSET(byteOffset) \
176 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
177/*
178 * Return an offset, given a bit number as returned from CLZ.
179 */
180#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700181 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700182
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700183#define OFFSET_OF_OBJECT_MEMBER(type, field) \
184 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700186// Classes shared with the managed side of the world need to be packed
187// so that they don't have extra platform specific padding.
188#define MANAGED __attribute__ ((__packed__))
189
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700191class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700192 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700193 static bool InstanceOf(const Object* object, const Class* klass) {
194 if (object == NULL) {
195 return false;
196 }
197 return object->InstanceOf(klass);
198 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 static MemberOffset ClassOffset() {
201 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700202 }
203
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700204 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 }
207
208 void SetClass(Class* new_klass);
209
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700210 bool InstanceOf(const Class* klass) const;
211
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700212 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700213
Elliott Hughesbf86d042011-08-31 17:53:14 -0700214 Object* Clone() {
215 UNIMPLEMENTED(FATAL);
216 return NULL;
217 }
218
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 static MemberOffset MonitorOffset() {
220 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
221 }
222
Elliott Hughes5f791332011-09-15 17:45:30 -0700223 volatile int32_t* GetRawLockWordAddress() {
224 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
225 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
226 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 }
228
Elliott Hughes5f791332011-09-15 17:45:30 -0700229 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230
Elliott Hughes5f791332011-09-15 17:45:30 -0700231 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Elliott Hughes5f791332011-09-15 17:45:30 -0700233 void MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Elliott Hughes5f791332011-09-15 17:45:30 -0700235 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236
Elliott Hughes5f791332011-09-15 17:45:30 -0700237 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238
Elliott Hughes5f791332011-09-15 17:45:30 -0700239 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240
Elliott Hughes5f791332011-09-15 17:45:30 -0700241 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700242
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700243 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
245 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700246 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 return down_cast<Class*>(this);
248 }
249
250 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252 return down_cast<const Class*>(this);
253 }
254
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255 bool IsClassClass() const;
256
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700257 bool IsObjectArray() const;
258
259 template<class T>
260 ObjectArray<T>* AsObjectArray() {
261 DCHECK(IsObjectArray());
262 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700263 }
264
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700265 template<class T>
266 const ObjectArray<T>* AsObjectArray() const {
267 DCHECK(IsObjectArray());
268 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700269 }
270
Brian Carlstromb63ec392011-08-27 17:38:27 -0700271 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700272
273 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700274 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700275 return down_cast<Array*>(this);
276 }
277
278 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700279 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700280 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700281 }
282
Brian Carlstroma663ea52011-08-19 23:33:41 -0700283 bool IsString() const;
284
285 String* AsString() {
286 DCHECK(IsString());
287 return down_cast<String*>(this);
288 }
289
290 bool IsMethod() const;
291
292 Method* AsMethod() {
293 DCHECK(IsMethod());
294 return down_cast<Method*>(this);
295 }
296
Brian Carlstrom4873d462011-08-21 15:23:39 -0700297 const Method* AsMethod() const {
298 DCHECK(IsMethod());
299 return down_cast<const Method*>(this);
300 }
301
Brian Carlstroma663ea52011-08-19 23:33:41 -0700302 bool IsField() const;
303
304 Field* AsField() {
305 DCHECK(IsField());
306 return down_cast<Field*>(this);
307 }
308
Brian Carlstrom4873d462011-08-21 15:23:39 -0700309 const Field* AsField() const {
310 DCHECK(IsField());
311 return down_cast<const Field*>(this);
312 }
313
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700314 bool IsReferenceInstance() const;
315
316 bool IsWeakReferenceInstance() const;
317
318 bool IsSoftReferenceInstance() const;
319
320 bool IsFinalizerReferenceInstance() const;
321
322 bool IsPhantomReferenceInstance() const;
323
324 // Accessors for Java type fields
325 template<class T>
326 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700327 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
328 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 Heap::VerifyObject(result);
330 return result;
331 }
332
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700333 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700335 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
336 if (new_value != NULL) {
337 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 }
340
341 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
342 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700343 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
344 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700346 return android_atomic_acquire_load(word_addr);
347 } else {
348 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 }
351
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700352 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
353 if (this_is_valid) {
354 Heap::VerifyObject(this);
355 }
356 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700357 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700358 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700359 /*
360 * TODO: add an android_atomic_synchronization_store() function and
361 * use it in the 32-bit volatile set handlers. On some platforms we
362 * can use a fast atomic instruction and avoid the barriers.
363 */
364 ANDROID_MEMBAR_STORE();
365 *word_addr = new_value;
366 ANDROID_MEMBAR_FULL();
367 } else {
368 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370 }
371
372 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
373 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700374 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700375 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700377 uint64_t result = QuasiAtomicRead64(addr);
378 ANDROID_MEMBAR_FULL();
379 return result;
380 } else {
381 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 }
384
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700385 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700387 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
388 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700390 ANDROID_MEMBAR_STORE();
391 QuasiAtomicSwap64(new_value, addr);
392 // Post-store barrier not required due to use of atomic op or mutex.
393 } else {
394 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 }
397
398 protected:
399 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400 template<class T>
401 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700402 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700403 }
404
405 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700406 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
407 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700408 }
409
410 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700411 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700412
Elliott Hughes5f791332011-09-15 17:45:30 -0700413 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700414
Elliott Hughes5f791332011-09-15 17:45:30 -0700415 friend class ImageWriter; // for abusing monitor_ directly
416 friend class ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700417 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700418};
419
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700421class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400422 private:
423 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700425 friend struct AccessibleObjectOffsets; // for verifying offset information
426 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400427};
428
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700430class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700431 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700433
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434 void SetDeclaringClass(Class *new_declaring_class);
435
436 const String* GetName() const;
437
438 void SetName(String* new_name);
439
440 uint32_t GetAccessFlags() const;
441
442 void SetAccessFlags(uint32_t new_access_flags) {
443 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
444 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700445 }
446
447 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700449 }
450
jeffhaobdb76512011-09-07 11:43:16 -0700451 bool IsFinal() const {
452 return (access_flags_ & kAccFinal) != 0;
453 }
454
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700458
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459 // Gets type using type index and resolved types in the dex cache, may be null
460 // if type isn't yet resolved
461 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700463 // Performs full resolution, may return null and set exceptions if type cannot
464 // be resolved
465 Class* GetType() const;
466
467 // Offset to field within an Object
468 MemberOffset GetOffset() const;
469
buzbee34cd9e52011-09-08 14:31:52 -0700470 static MemberOffset OffsetOffset() {
471 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
472 }
473
474 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer);
475
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700476 MemberOffset GetOffsetDuringLinking() const;
477
478 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700479
Brian Carlstrom4873d462011-08-21 15:23:39 -0700480 // field access, null object for static fields
481 bool GetBoolean(const Object* object) const;
482 void SetBoolean(Object* object, bool z) const;
483 int8_t GetByte(const Object* object) const;
484 void SetByte(Object* object, int8_t b) const;
485 uint16_t GetChar(const Object* object) const;
486 void SetChar(Object* object, uint16_t c) const;
487 uint16_t GetShort(const Object* object) const;
488 void SetShort(Object* object, uint16_t s) const;
489 int32_t GetInt(const Object* object) const;
490 void SetInt(Object* object, int32_t i) const;
491 int64_t GetLong(const Object* object) const;
492 void SetLong(Object* object, int64_t j) const;
493 float GetFloat(const Object* object) const;
494 void SetFloat(Object* object, float f) const;
495 double GetDouble(const Object* object) const;
496 void SetDouble(Object* object, double d) const;
497 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700498 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700499
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700500 // Slow path routines for static field access when field was unresolved at
501 // compile time
502 static uint32_t Get32StaticFromCode(uint32_t field_idx,
503 const Method* referrer);
504 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
505 uint32_t new_value);
506 static uint64_t Get64StaticFromCode(uint32_t field_idx,
507 const Method* referrer);
508 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
509 uint64_t new_value);
510 static Object* GetObjStaticFromCode(uint32_t field_idx,
511 const Method* referrer);
512 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
513 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700514
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515 static Class* GetJavaLangReflectField() {
516 DCHECK(java_lang_reflect_Field_ != NULL);
517 return java_lang_reflect_Field_;
518 }
519
520 static void SetClass(Class* java_lang_reflect_Field);
521 static void ResetClass();
522
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523 bool IsVolatile() const {
524 return (GetAccessFlags() & kAccVolatile) != 0;
525 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700526
buzbee1da522d2011-09-04 11:22:20 -0700527 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700528 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700529 uint32_t Get32(const Object* object) const;
530 void Set32(Object* object, uint32_t new_value) const;
531 uint64_t Get64(const Object* object) const;
532 void Set64(Object* object, uint64_t new_value) const;
533 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700534 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700535
Jesse Wilson35baaab2011-08-10 16:18:03 -0400536 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537
Jesse Wilson35baaab2011-08-10 16:18:03 -0400538 // The class in which this field is declared.
539 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540
Jesse Wilson35baaab2011-08-10 16:18:03 -0400541 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700542
Brian Carlstromdbc05252011-09-09 01:59:59 -0700543 const String* name_;
544
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545 // Type of the field
Jesse Wilson35baaab2011-08-10 16:18:03 -0400546 Class* type_;
547
Brian Carlstromdbc05252011-09-09 01:59:59 -0700548 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549
Jesse Wilson35baaab2011-08-10 16:18:03 -0400550 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551
552 // Offset of field within an instance or in the Class' static fields
553 uint32_t offset_;
554
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 // Dex cache index of resolved type
556 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400557
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558 int32_t slot_;
559
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560 static Class* java_lang_reflect_Field_;
561
Brian Carlstrom693267a2011-09-06 09:25:34 -0700562 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400563 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700564};
565
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566// C++ mirror of java.lang.reflect.Method
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700567class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700569 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700570 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700571 Object* obj,
572 Thread* thread,
573 byte* args,
574 JValue* result);
575
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700576 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700578 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700580 static MemberOffset DeclaringClassOffset() {
581 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700582 }
583
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700584 // Returns the method name, e.g. "<init>" or "eatLunch"
585 const String* GetName() const;
586
587 void SetName(String* new_name);
588
jeffhaoe23d93c2011-09-15 14:48:43 -0700589 ByteArray* GetRegisterMapData() const;
590
jeffhaoa0a764a2011-09-16 10:43:38 -0700591 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700592
593 ByteArray* GetRegisterMapHeader() const;
594
jeffhaoa0a764a2011-09-16 10:43:38 -0700595 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700596
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700597 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700599 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700600
601 const String* GetSignature() const;
602
603 void SetSignature(String* new_signature);
604
605 bool HasSameNameAndDescriptor(const Method* that) const;
606
607 uint32_t GetAccessFlags() const;
608
609 void SetAccessFlags(uint32_t new_access_flags) {
610 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
611 false);
612 }
613
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700614 // Returns true if the method is declared public.
615 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700616 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 }
618
619 // Returns true if the method is declared private.
620 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 }
623
624 // Returns true if the method is declared static.
625 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700626 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700627 }
628
Brian Carlstrom1f870082011-08-23 16:02:11 -0700629 // Returns true if the method is a constructor.
630 bool IsConstructor() const {
631 return (access_flags_ & kAccConstructor) != 0;
632 }
633
634 // Returns true if the method is static, private, or a constructor.
635 bool IsDirect() const {
636 return IsStatic() || IsPrivate() || IsConstructor();
637 }
638
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700639 // Returns true if the method is declared synchronized.
640 bool IsSynchronized() const {
641 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700642 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700643 }
644
645 // Returns true if the method is declared final.
646 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 }
649
650 // Returns true if the method is declared native.
651 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700652 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700653 }
654
655 // Returns true if the method is declared abstract.
656 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 }
659
660 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700661 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700662 }
663
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664 uint16_t GetMethodIndex() const;
665
666 size_t GetVtableIndex() const {
667 return GetMethodIndex();
668 }
669
670 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700671 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700672 new_method_index, false);
673 }
674
675 static MemberOffset MethodIndexOffset() {
676 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
677 }
678
679 uint32_t GetCodeItemOffset() const {
680 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
681 }
682
683 void SetCodeItemOffset(uint32_t new_code_off) {
684 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
685 new_code_off, false);
686 }
687
688 // Number of 32bit registers that would be required to hold all the arguments
689 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700691 // Number of argument bytes required for densely packing the
692 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700693 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 uint16_t NumRegisters() const;
696
697 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700698 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700700 }
701
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700702 uint16_t NumIns() const;
703
704 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700705 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700706 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700707 }
708
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700710
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700711 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700712 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700714 }
715
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700716 uint32_t GetProtoIdx() const;
717
718 void SetProtoIdx(uint32_t new_proto_idx) {
719 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700720 }
721
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700722 ObjectArray<String>* GetDexCacheStrings() const;
723 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
724
725 static MemberOffset DexCacheStringsOffset() {
726 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
727 }
728
buzbee2a475e72011-09-07 17:19:17 -0700729 static MemberOffset DexCacheResolvedTypesOffset() {
730 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
731 }
732
buzbee34cd9e52011-09-08 14:31:52 -0700733 static MemberOffset DexCacheResolvedFieldsOffset() {
734 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
735 }
736
buzbee1da522d2011-09-04 11:22:20 -0700737 static MemberOffset DexCacheInitializedStaticStorageOffset() {
738 return OFFSET_OF_OBJECT_MEMBER(Method,
739 dex_cache_initialized_static_storage_);
740 }
741
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700742 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
743 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
744
745 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
746 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
747
748 ObjectArray<Field>* GetDexCacheResolvedFields() const;
749 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
750
751 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
752 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
753
754 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
755 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
756
757 void SetReturnTypeIdx(uint32_t new_return_type_idx);
758
759 Class* GetReturnType() const;
760
761 bool IsReturnAReference() const;
762
763 bool IsReturnAFloat() const;
764
765 bool IsReturnADouble() const;
766
Ian Rogersb033c752011-07-20 12:22:35 -0700767 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700768 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700769 }
770
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700771 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700772
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700773 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700774
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700775 // "Args" may refer to any of the 3 levels of "Args."
776 // To avoid confusion, our code will denote which "Args" clearly:
777 // 1. UserArgs: Args that a user see.
778 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
779 // receiver.
780 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
781 // E.g., the first in Args is Method* for both static and non-static
782 // methods. And CConvArgs doesn't deal with the receiver because
783 // receiver is hardwired in an implicit register, so CConvArgs doesn't
784 // need to deal with it.
785 //
786 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700787 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700788
789 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700790 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700791 size_t NumReferenceArgs() const;
792
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700793 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700794 size_t NumLongOrDoubleArgs() const;
795
Ian Rogersb033c752011-07-20 12:22:35 -0700796 // Is the given method parameter a reference?
797 bool IsParamAReference(unsigned int param) const;
798
799 // Is the given method parameter a long or double?
800 bool IsParamALongOrDouble(unsigned int param) const;
801
Ian Rogersdf20fe02011-07-20 20:34:16 -0700802 // Size in bytes of the given parameter
803 size_t ParamSize(unsigned int param) const;
804
805 // Size in bytes of the return value
806 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700807
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700808 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
809
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700810 const ByteArray* GetCodeArray() const {
811 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
812 }
813
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700814 const void* GetCode() const {
815 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700816 }
817
buzbee4ef76522011-09-08 10:00:32 -0700818 void SetCode(ByteArray* code_array, InstructionSet instruction_set,
Ian Rogersbdb03912011-09-14 00:55:44 -0700819 IntArray* mapping_table = NULL);
buzbeec143c552011-08-20 17:38:58 -0700820
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700821 static MemberOffset GetCodeOffset() {
822 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700823 }
824
Ian Rogersbdb03912011-09-14 00:55:44 -0700825 // Is the given PC within the code array?
826 bool IsWithinCode(uintptr_t pc) const;
827
828 IntArray* GetMappingTable() const {
829 return GetFieldObject<IntArray*>(
830 OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
831 }
832
Shih-wei Liaod11af152011-08-23 16:02:11 -0700833 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700834 DCHECK(sizeof(size_t) == sizeof(uint32_t));
835 size_t result = GetField32(
836 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
837 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
838 return result;
839 }
840
841 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
842 DCHECK(sizeof(size_t) == sizeof(uint32_t));
843 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
844 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
845 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700846 }
847
Shih-wei Liaod11af152011-08-23 16:02:11 -0700848 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700849 DCHECK(sizeof(size_t) == sizeof(uint32_t));
850 return GetField32(
851 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700852 }
853
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
855 DCHECK(sizeof(size_t) == sizeof(uint32_t));
856 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
857 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
858 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700859 }
860
Brian Carlstrom16192862011-09-12 17:50:06 -0700861 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700862
Brian Carlstrom16192862011-09-12 17:50:06 -0700863 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700864
Brian Carlstrom16192862011-09-12 17:50:06 -0700865 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700866
Ian Rogersb033c752011-07-20 12:22:35 -0700867 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700868 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700869 }
870
Brian Carlstrom78128a62011-09-15 17:21:19 -0700871 const void* GetNativeMethod() const {
872 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
873 }
874
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700875 ByteArray* GetInvokeStubArray() const {
876 ByteArray* result = GetFieldPtr<ByteArray*>(
877 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
878 // TODO: DCHECK(result != NULL); should be ahead of time compiled
879 return result;
880 }
881
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700882 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700883 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700884 InvokeStub* result = GetFieldPtr<InvokeStub*>(
885 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
886 // TODO: DCHECK(result != NULL); should be ahead of time compiled
887 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700888 }
889
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700890 static MemberOffset GetInvokeStubOffset() {
891 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700892 }
893
buzbee561227c2011-09-02 15:28:19 -0700894 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
895 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
896 }
897
898 static MemberOffset GetDexCacheResolvedMethodsOffset() {
899 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
900 }
901
902 static MemberOffset GetMethodIndexOffset() {
903 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
904 }
905
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700906 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700907
Ian Rogersbdb03912011-09-14 00:55:44 -0700908 uint32_t GetCoreSpillMask() {
909 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700910 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700911
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700912 void SetCoreSpillMask(uint32_t core_spill_mask) {
913 // Computed during compilation
914 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_),
915 core_spill_mask, false);
916 }
917
Ian Rogersbdb03912011-09-14 00:55:44 -0700918 uint32_t GetFpSpillMask() {
919 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
920 }
921
922 void SetFpSpillMask(uint32_t fp_spill_mask) {
923 // Computed during compilation
924 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_),
925 fp_spill_mask, false);
926 }
927
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700928 // Converts a native PC to a dex PC. TODO: this is a no-op
929 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700930 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700931
932 // Converts a dex PC to a native PC. TODO: this is a no-op
933 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700934 uintptr_t ToNativePC(const uint32_t dex_pc) const;
935
936 // Find the catch block for the given exception type and dex_pc
937 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938
939 static Class* GetJavaLangReflectMethod() {
940 DCHECK(java_lang_reflect_Method_ != NULL);
941 return java_lang_reflect_Method_;
942 }
943
944 static void SetClass(Class* java_lang_reflect_Method);
945 static void ResetClass();
946
947 private:
948 uint32_t GetReturnTypeIdx() const;
949
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700950 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
951 // the class we are a part of
952 Class* declaring_class_;
953 ObjectArray<Class>* java_exception_types_;
954 Object* java_formal_type_parameters_;
955 Object* java_generic_exception_types_;
956 Object* java_generic_parameter_types_;
957 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700958
Brian Carlstrom693267a2011-09-06 09:25:34 -0700959 String* name_;
960
961 ObjectArray<Class>* java_parameter_types_;
962
963 Class* java_return_type_; // Unused by ART
964
Brian Carlstrom693267a2011-09-06 09:25:34 -0700965 // Storage for code_
966 const ByteArray* code_array_;
967
968 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700969 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
970
971 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
972 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
973
974 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
975 ObjectArray<Field>* dex_cache_resolved_fields_;
976
977 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
978 ObjectArray<Method>* dex_cache_resolved_methods_;
979
Brian Carlstromdbc05252011-09-09 01:59:59 -0700980 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
981 ObjectArray<Class>* dex_cache_resolved_types_;
982
983 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
984 ObjectArray<String>* dex_cache_strings_;
985
986 // Storage for invoke_stub_
987 const ByteArray* invoke_stub_array_;
988
989 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -0700990 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700991
jeffhaoe23d93c2011-09-15 14:48:43 -0700992 // Byte arrays that hold data for the register maps
993 const ByteArray* register_map_data_;
994 const ByteArray* register_map_header_;
995
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700996 // The short-form method descriptor string.
997 String* shorty_;
998
Brian Carlstromdbc05252011-09-09 01:59:59 -0700999 // The method descriptor. This represents the parameters a method
1000 // takes and value it returns. This string is a list of the type
1001 // descriptors for the parameters enclosed in parenthesis followed
1002 // by the return type descriptor. For example, for the method
1003 //
1004 // Object mymethod(int i, double d, Thread t)
1005 //
1006 // the method descriptor would be
1007 //
1008 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1009 String* signature_;
1010
1011 uint32_t java_generic_types_are_initialized_;
1012
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001013 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001014 uint32_t access_flags_;
1015
1016 // Compiled code associated with this method for callers from managed code.
1017 // May be compiled managed code or a bridge for invoking a native method.
1018 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001020 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001021 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001022
Brian Carlstrom693267a2011-09-06 09:25:34 -07001023 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001024 uint32_t core_spill_mask_;
1025
1026 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001027 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001028
Brian Carlstrom693267a2011-09-06 09:25:34 -07001029 // Total size in bytes of the frame
1030 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031
Brian Carlstrom693267a2011-09-06 09:25:34 -07001032 // Native invocation stub entry point for calling from native to managed code.
1033 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001034
Brian Carlstrom693267a2011-09-06 09:25:34 -07001035 // Index of the return type
1036 uint32_t java_return_type_idx_;
1037
Brian Carlstrom693267a2011-09-06 09:25:34 -07001038 // For concrete virtual methods, this is the offset of the method
1039 // in Class::vtable_.
1040 //
1041 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001042 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001043 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001044
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001045 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001046 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001047
Brian Carlstrom693267a2011-09-06 09:25:34 -07001048 // Method bounds; not needed for an abstract method.
1049 //
1050 // For a native method, we compute the size of the argument list, and
1051 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001052 uint32_t num_ins_;
1053 uint32_t num_outs_;
1054 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001055
Brian Carlstrom693267a2011-09-06 09:25:34 -07001056 // Method prototype descriptor string (return and argument types).
1057 uint32_t proto_idx_;
1058
1059 // Offset of return PC within frame for compiled code (in bytes)
1060 size_t return_pc_offset_in_bytes_;
1061
Brian Carlstrom693267a2011-09-06 09:25:34 -07001062 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001063
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001064 static Class* java_lang_reflect_Method_;
1065
Brian Carlstrom693267a2011-09-06 09:25:34 -07001066 friend class ImageWriter; // for relocating code_ and invoke_stub_
1067 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001068 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001069};
1070
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001071class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001072 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001073 static size_t SizeOf(size_t component_count,
1074 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001075 return sizeof(Array) + component_count * component_size;
1076 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001077
Brian Carlstromb63ec392011-08-27 17:38:27 -07001078 // Given the context of a calling Method, use its DexCache to
1079 // resolve a type to an array Class. If it cannot be resolved, throw
1080 // an error. If it can, use it to create an array.
1081 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
1082
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001083 // A convenience for code that doesn't know the component size,
1084 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001085 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001086
Brian Carlstromb63ec392011-08-27 17:38:27 -07001087 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001088
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001089 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001090
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001091 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001092 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001093 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001094
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001095 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001096 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001097 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001098 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001099
buzbeec143c552011-08-20 17:38:58 -07001100 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001101 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001102 }
1103
1104 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001105 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001106 }
1107
Elliott Hughesbf86d042011-08-31 17:53:14 -07001108 void* GetRawData() {
1109 return reinterpret_cast<void*>(first_element_);
1110 }
1111
Elliott Hughes289da822011-08-16 10:11:20 -07001112 protected:
1113 bool IsValidIndex(int32_t index) const {
1114 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001115 Thread* self = Thread::Current();
1116 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1117 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001118 return false;
Elliott Hughes289da822011-08-16 10:11:20 -07001119 }
1120 return true;
1121 }
1122
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001123 private:
1124 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001125 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001126 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1127 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001128 // Marker for the data (used by generated code)
1129 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001130
Carl Shapirof88c9522011-08-06 15:47:38 -07001131 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001132};
1133
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001134template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001135class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001136 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001137 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001138
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001139 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001140
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001141 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001142
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 // Set element without bound and element type checks, to be used in limited
1144 // circumstances, such as during boot image writing
1145 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001146
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001147 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001148 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001149 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001150
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001151 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001152
1153 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001154 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001155};
1156
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157template<class T>
1158ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1159 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1160}
1161
1162template<class T>
1163T* ObjectArray<T>::Get(int32_t i) const {
1164 if (!IsValidIndex(i)) {
1165 return NULL;
1166 }
1167 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1168 return GetFieldObject<T*>(data_offset, false);
1169}
1170
1171template<class T>
1172ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1173 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1174 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1175 return new_array;
1176}
1177
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001178// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001179// provides the static storage. However, this might change to an Array
1180// to improve image sharing, so we use this type to avoid assumptions
1181// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001182class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001184// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001185class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001186 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001187
1188 // Class Status
1189 //
1190 // kStatusNotReady: If a Class cannot be found in the class table by
1191 // FindClass, it allocates an new one with AllocClass in the
1192 // kStatusNotReady and calls LoadClass. Note if it does find a
1193 // class, it may not be kStatusResolved and it will try to push it
1194 // forward toward kStatusResolved.
1195 //
1196 // kStatusIdx: LoadClass populates with Class with information from
1197 // the DexFile, moving the status to kStatusIdx, indicating that the
1198 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001199 // populated based on super_class_type_idx_ and
1200 // interfaces_type_idx_. The new Class can then be inserted into the
1201 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001202 //
1203 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1204 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1205 // using ResolveClass to initialize the super_class_ and interfaces_.
1206 //
1207 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001208 // shows linking is complete and fields of the Class populated by making
1209 // it kStatusResolved. Java allows circularities of the form where a super
1210 // class has a field that is of the type of the sub class. We need to be able
1211 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001212
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001214 kStatusError = -1,
1215 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001216 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001217 kStatusLoaded = 2, // DEX idx values resolved
1218 kStatusResolved = 3, // part of linking
1219 kStatusVerifying = 4, // in the process of being verified
1220 kStatusVerified = 5, // logically part of linking; done pre-init
1221 kStatusInitializing = 6, // class init in progress
1222 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001223 };
1224
1225 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226 kPrimNot = 0,
1227 kPrimBoolean,
1228 kPrimByte,
1229 kPrimChar,
1230 kPrimShort,
1231 kPrimInt,
1232 kPrimLong,
1233 kPrimFloat,
1234 kPrimDouble,
1235 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001236 };
1237
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001238 Status GetStatus() const {
1239 CHECK(sizeof(Status) == sizeof(uint32_t));
1240 return static_cast<Status>(
1241 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1242 }
1243
1244 void SetStatus(Status new_status);
1245
1246 // Returns true if the class has failed to link.
1247 bool IsErroneous() const {
1248 return GetStatus() == kStatusError;
1249 }
1250
1251 // Returns true if the class has been loaded.
1252 bool IsIdxLoaded() const {
1253 return GetStatus() >= kStatusIdx;
1254 }
1255
1256 // Returns true if the class has been loaded.
1257 bool IsLoaded() const {
1258 return GetStatus() >= kStatusLoaded;
1259 }
1260
1261 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001262 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001263 return GetStatus() >= kStatusResolved;
1264 }
1265
1266 // Returns true if the class has been verified.
1267 bool IsVerified() const {
1268 return GetStatus() >= kStatusVerified;
1269 }
1270
1271 // Returns true if the class is initialized.
1272 bool IsInitialized() const {
1273 return GetStatus() == kStatusInitialized;
1274 }
1275
1276 uint32_t GetAccessFlags() const;
1277
1278 void SetAccessFlags(uint32_t new_access_flags) {
1279 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags,
1280 false);
1281 }
1282
1283 // Returns true if the class is an interface.
1284 bool IsInterface() const {
1285 return (GetAccessFlags() & kAccInterface) != 0;
1286 }
1287
1288 // Returns true if the class is declared public.
1289 bool IsPublic() const {
1290 return (GetAccessFlags() & kAccPublic) != 0;
1291 }
1292
1293 // Returns true if the class is declared final.
1294 bool IsFinal() const {
1295 return (GetAccessFlags() & kAccFinal) != 0;
1296 }
1297
1298 // Returns true if the class is abstract.
1299 bool IsAbstract() const {
1300 return (GetAccessFlags() & kAccAbstract) != 0;
1301 }
1302
1303 // Returns true if the class is an annotation.
1304 bool IsAnnotation() const {
1305 return (GetAccessFlags() & kAccAnnotation) != 0;
1306 }
1307
1308 // Returns true if the class is synthetic.
1309 bool IsSynthetic() const {
1310 return (GetAccessFlags() & kAccSynthetic) != 0;
1311 }
1312
1313 bool IsReferenceClass() const {
1314 return (GetAccessFlags() & kAccClassIsReference) != 0;
1315 }
1316
1317 bool IsWeakReferenceClass() const {
1318 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1319 }
1320
1321 bool IsSoftReferenceClass() const {
1322 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1323 }
1324
1325 bool IsFinalizerReferenceClass() const {
1326 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1327 }
1328
1329 bool IsPhantomReferenceClass() const {
1330 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1331 }
1332
1333 PrimitiveType GetPrimitiveType() const {
1334 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1335 return static_cast<PrimitiveType>(
1336 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1337 }
1338
1339 void SetPrimitiveType(PrimitiveType new_type) {
1340 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1341 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type,
1342 false);
1343 }
1344
1345 // Returns true if the class is a primitive type.
1346 bool IsPrimitive() const {
1347 return GetPrimitiveType() != kPrimNot;
1348 }
1349
1350 bool IsPrimitiveBoolean() const {
1351 return GetPrimitiveType() == kPrimBoolean;
1352 }
1353
1354 bool IsPrimitiveByte() const {
1355 return GetPrimitiveType() == kPrimByte;
1356 }
1357
1358 bool IsPrimitiveChar() const {
1359 return GetPrimitiveType() == kPrimChar;
1360 }
1361
1362 bool IsPrimitiveShort() const {
1363 return GetPrimitiveType() == kPrimShort;
1364 }
1365
1366 bool IsPrimitiveInt() const {
1367 return GetPrimitiveType() == kPrimInt;
1368 }
1369
1370 bool IsPrimitiveLong() const {
1371 return GetPrimitiveType() == kPrimLong;
1372 }
1373
1374 bool IsPrimitiveFloat() const {
1375 return GetPrimitiveType() == kPrimFloat;
1376 }
1377
1378 bool IsPrimitiveDouble() const {
1379 return GetPrimitiveType() == kPrimDouble;
1380 }
1381
1382 bool IsPrimitiveVoid() const {
1383 return GetPrimitiveType() == kPrimVoid;
1384 }
1385
1386 size_t PrimitiveSize() const;
1387
1388 bool IsArrayClass() const {
1389 return GetArrayRank() != 0;
1390 }
1391
1392 int32_t GetArrayRank() const {
1393 int32_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, array_rank_),
1394 false);
1395 return result;
1396 }
1397
1398 void SetArrayRank(int32_t new_array_rank) {
1399 DCHECK_EQ(0, GetArrayRank());
1400 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, array_rank_), new_array_rank,
1401 false);
1402 }
1403
1404 Class* GetComponentType() const {
1405 DCHECK(IsArrayClass());
1406 return GetFieldObject<Class*>(
1407 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1408 }
1409
1410 void SetComponentType(Class* new_component_type) {
1411 DCHECK(GetComponentType() == NULL);
1412 DCHECK(new_component_type != NULL);
1413 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_),
1414 new_component_type, false);
1415 }
1416
1417 size_t GetComponentSize() const {
1418 return GetTypeSize(GetComponentType()->GetDescriptor());
1419 }
1420
1421 bool IsObjectClass() const {
1422 return !IsPrimitive() && GetSuperClass() == NULL;
1423 }
1424
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001425 // Tests whether an element of type 'object_class' can be
1426 // assigned into an array of type 'array_class'.
1427 static bool CanPutArrayElement(const Class* object_class, const Class* array_class);
1428 // Like CanPutArrayElement, but throws an exception and
1429 // unwinds the stack instead of returning false.
buzbee9a195c92011-09-16 13:26:02 -07001430 static void CanPutArrayElementFromCode(const Object* element, const Class* array_class);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431
Brian Carlstromb63ec392011-08-27 17:38:27 -07001432 // Given the context of a calling Method, use its DexCache to
1433 // resolve a type to a Class. If it cannot be resolved, throw an
1434 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -07001435 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001436
Brian Carlstrom1f870082011-08-23 16:02:11 -07001437 // Creates a raw object instance but does not invoke the default constructor.
1438 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001439
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001440 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001441
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001442 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 const String* result = GetFieldObject<const String*>(
1444 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1445 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1446 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1447 return result;
1448 }
1449
1450 void SetDescriptor(String* new_descriptor);
1451
1452 bool IsVariableSize() const {
1453 // Classes and arrays vary in size, and so the object_size_ field cannot
1454 // be used to get their instance size
1455 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001456 }
1457
Brian Carlstrom4873d462011-08-21 15:23:39 -07001458 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 CHECK(sizeof(size_t) == sizeof(int32_t));
1460 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001461 }
1462
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001463 size_t GetClassSize() const {
1464 CHECK(sizeof(size_t) == sizeof(uint32_t));
1465 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466 }
1467
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001469 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001470 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001471 << " new_class_size=" << new_class_size
1472 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001473 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001474 }
1475
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 size_t GetObjectSize() const {
1477 CHECK(!IsVariableSize());
1478 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001479 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 CHECK_GE(result, sizeof(Object));
1481 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001482 }
1483
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001484 void SetObjectSize(size_t new_object_size) {
1485 DCHECK(!IsVariableSize());
1486 CHECK(sizeof(size_t) == sizeof(int32_t));
1487 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1488 new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001489 }
1490
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001491 // Returns true if this class is in the same packages as that class.
1492 bool IsInSamePackage(const Class* that) const;
1493
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001494 static bool IsInSamePackage(const String* descriptor1,
1495 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001496
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001497 // Returns true if this class can access that class.
1498 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001500 }
1501
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001502 bool IsAssignableFrom(const Class* klass) const {
1503 DCHECK(klass != NULL);
1504 if (this == klass) {
1505 // Can always assign to things of the same type
1506 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001507 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001508 // Can assign any reference to java.lang.Object
1509 return !klass->IsPrimitive();
1510 } else if (IsInterface()) {
1511 return klass->Implements(this);
1512 } else if (klass->IsArrayClass()) {
1513 return IsAssignableFromArray(klass);
1514 } else {
1515 return klass->IsSubClass(this);
1516 }
1517 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001518
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001519 Class* GetSuperClass() const {
1520 // Can only get super class for loaded classes (hack for when runtime is
1521 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001522 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001523 return GetFieldObject<Class*>(
1524 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1525 }
1526
buzbee4a3164f2011-09-03 11:25:10 -07001527 static MemberOffset SuperClassOffset() {
1528 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1529 }
1530
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001531 void SetSuperClass(Class *new_super_class) {
1532 // super class is assigned once, except during class linker initialization
1533 Class* old_super_class = GetFieldObject<Class*>(
1534 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1535 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1536 DCHECK(new_super_class != NULL);
1537 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_),
1538 new_super_class, false);
1539 }
1540
1541 bool HasSuperClass() const {
1542 return GetSuperClass() != NULL;
1543 }
1544
1545 uint32_t GetSuperClassTypeIdx() const {
1546 DCHECK(IsIdxLoaded());
1547 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1548 false);
1549 }
1550
1551 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
1552 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1553 new_super_class_idx, false);
1554 }
1555
1556 const ClassLoader* GetClassLoader() const;
1557
1558 void SetClassLoader(const ClassLoader* new_cl);
1559
1560 static MemberOffset DexCacheOffset() {
1561 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1562 }
1563
1564 DexCache* GetDexCache() const;
1565
1566 void SetDexCache(DexCache* new_dex_cache);
1567
1568 ObjectArray<Method>* GetDirectMethods() const {
1569 DCHECK(IsLoaded());
1570 return GetFieldObject<ObjectArray<Method>*>(
1571 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1572 }
1573
1574 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1575 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1576 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1577 DCHECK_NE(0, new_direct_methods->GetLength());
1578 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1579 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001580 }
1581
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001582 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001584 }
1585
1586 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001587 ObjectArray<Method>* direct_methods =
1588 GetFieldObject<ObjectArray<Method>*>(
1589 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1590 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001591 }
1592
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001593 // Returns the number of static, private, and constructor methods.
1594 size_t NumDirectMethods() const {
1595 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1596 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001597
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001598 ObjectArray<Method>* GetVirtualMethods() const {
1599 DCHECK(IsLoaded());
1600 return GetFieldObject<ObjectArray<Method>*>(
1601 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1602 }
1603
1604 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1605 // TODO: we reassign virtual methods to grow the table for miranda
1606 // methods.. they should really just be assigned once
1607 DCHECK_NE(0, new_virtual_methods->GetLength());
1608 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1609 new_virtual_methods, false);
1610 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001611
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001612 // Returns the number of non-inherited virtual methods.
1613 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001614 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001615 }
1616
1617 Method* GetVirtualMethod(uint32_t i) const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001618 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001619 return GetVirtualMethods()->Get(i);
1620 }
1621
1622 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
1623 DCHECK(IsLoaded());
1624 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001625 }
1626
1627 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001628 ObjectArray<Method>* virtual_methods =
1629 GetFieldObject<ObjectArray<Method>*>(
1630 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1631 virtual_methods->Set(i, f);
1632 }
1633
1634 ObjectArray<Method>* GetVTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001635 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001636 return GetFieldObject<ObjectArray<Method>*>(
1637 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1638 }
1639
1640 ObjectArray<Method>* GetVTableDuringLinking() const {
1641 DCHECK(IsLoaded());
1642 return GetFieldObject<ObjectArray<Method>*>(
1643 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1644 }
1645
1646 void SetVTable(ObjectArray<Method>* new_vtable) {
1647 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1648 }
1649
1650 static MemberOffset VTableOffset() {
1651 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001652 }
1653
Brian Carlstrom30b94452011-08-25 21:35:26 -07001654 // Given a method implemented by this class but potentially from a
1655 // super class, return the specific implementation
1656 // method for this class.
1657 Method* FindVirtualMethodForVirtual(Method* method) {
1658 DCHECK(!method->GetDeclaringClass()->IsInterface());
1659 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001660 // Use the index to a potentially overridden one for this instance's class.
1661 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001662 }
1663
1664 // Given a method implemented by this class, but potentially from a
1665 // super class or interface, return the specific implementation
1666 // method for this class.
1667 Method* FindVirtualMethodForInterface(Method* method);
1668
jeffhaobdb76512011-09-07 11:43:16 -07001669 Method* FindInterfaceMethod(const StringPiece& name,
1670 const StringPiece& descriptor);
1671
Brian Carlstrom30b94452011-08-25 21:35:26 -07001672 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1673 if (method->GetDeclaringClass()->IsInterface()) {
1674 return FindVirtualMethodForInterface(method);
1675 }
1676 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001677 }
1678
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001679 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1680 const StringPiece& descriptor);
1681
1682 Method* FindVirtualMethod(const StringPiece& name,
1683 const StringPiece& descriptor);
1684
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001685 Method* FindDeclaredDirectMethod(const StringPiece& name,
1686 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001687
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001688 Method* FindDirectMethod(const StringPiece& name,
1689 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001690
Carl Shapiro69759ea2011-07-21 18:13:35 -07001691 size_t NumInterfaces() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 CHECK(IsIdxLoaded()); // used during loading
1693 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1694 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1695 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001696 }
1697
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001698 IntArray* GetInterfacesTypeIdx() const {
1699 CHECK(IsIdxLoaded());
1700 return GetFieldObject<IntArray*>(
1701 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1702 }
1703
1704 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1705
1706 ObjectArray<Class>* GetInterfaces() const {
1707 CHECK(IsLoaded());
1708 return GetFieldObject<ObjectArray<Class>*>(
1709 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1710 }
1711
1712 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1713 DCHECK(NULL == GetFieldObject<Object*>(
1714 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
1715 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_),
1716 new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001717 }
1718
1719 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001720 DCHECK_NE(NumInterfaces(), 0U);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001721 ObjectArray<Class>* interfaces =
1722 GetFieldObject<ObjectArray<Class>*>(
1723 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1724 interfaces->Set(i, f);
1725 }
1726
1727 Class* GetInterface(uint32_t i) const {
1728 DCHECK_NE(NumInterfaces(), 0U);
1729 return GetInterfaces()->Get(i);
1730 }
1731
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001732 int32_t GetIfTableCount() const {
1733 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1734 if (iftable == NULL) {
1735 return 0;
1736 }
1737 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001738 }
1739
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001740 ObjectArray<InterfaceEntry>* GetIfTable() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001741 DCHECK(IsResolved());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001742 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001743 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1744 }
1745
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001746 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001747 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001748 }
1749
1750 // Get instance fields
1751 ObjectArray<Field>* GetIFields() const {
1752 DCHECK(IsLoaded());
1753 return GetFieldObject<ObjectArray<Field>*>(
1754 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1755 }
1756
1757 void SetIFields(ObjectArray<Field>* new_ifields) {
1758 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1759 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
1760 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_),
1761 new_ifields, false);
1762 }
1763
1764 size_t NumInstanceFields() const {
1765 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1766 }
1767
1768 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1769 DCHECK_NE(NumInstanceFields(), 0U);
1770 return GetIFields()->Get(i);
1771 }
1772
1773 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1774 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1775 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1776 ifields->Set(i, f);
1777 }
1778
1779 // Returns the number of instance fields containing reference types.
1780 size_t NumReferenceInstanceFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001781 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 DCHECK(sizeof(size_t) == sizeof(int32_t));
1783 return GetField32(
1784 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1785 }
1786
1787 size_t NumReferenceInstanceFieldsDuringLinking() const {
1788 DCHECK(IsLoaded());
1789 DCHECK(sizeof(size_t) == sizeof(int32_t));
1790 return GetField32(
1791 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1792 }
1793
1794 void SetNumReferenceInstanceFields(size_t new_num) {
1795 DCHECK(sizeof(size_t) == sizeof(int32_t));
1796 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_),
1797 new_num, false);
1798 }
1799
1800 uint32_t GetReferenceInstanceOffsets() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001801 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001802 return GetField32(
1803 OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
1804 }
1805
1806 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1807
1808 // Beginning of static field data
1809 static MemberOffset FieldsOffset() {
1810 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1811 }
1812
1813 // Returns the number of static fields containing reference types.
1814 size_t NumReferenceStaticFields() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001815 DCHECK(IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 DCHECK(sizeof(size_t) == sizeof(int32_t));
1817 return GetField32(
1818 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1819 }
1820
1821 size_t NumReferenceStaticFieldsDuringLinking() const {
1822 DCHECK(IsLoaded());
1823 DCHECK(sizeof(size_t) == sizeof(int32_t));
1824 return GetField32(
1825 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1826 }
1827
1828 void SetNumReferenceStaticFields(size_t new_num) {
1829 DCHECK(sizeof(size_t) == sizeof(int32_t));
1830 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_),
1831 new_num, false);
1832 }
1833
1834 ObjectArray<Field>* GetSFields() const {
1835 DCHECK(IsLoaded());
1836 return GetFieldObject<ObjectArray<Field>*>(
1837 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1838 }
1839
1840 void SetSFields(ObjectArray<Field>* new_sfields) {
1841 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1842 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
1843 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_),
1844 new_sfields, false);
1845 }
1846
1847 size_t NumStaticFields() const {
1848 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1849 }
1850
1851 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1852 return GetSFields()->Get(i);
1853 }
1854
1855 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1856 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1857 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1858 sfields->Set(i, f);
1859 }
1860
1861 uint32_t GetReferenceStaticOffsets() const {
1862 return GetField32(
1863 OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
1864 }
1865
1866 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1867
1868 // Finds the given instance field in this class or a superclass.
1869 Field* FindInstanceField(const StringPiece& name, Class* type);
1870
1871 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1872
1873 // Finds the given static field in this class or a superclass.
1874 Field* FindStaticField(const StringPiece& name, Class* type);
1875
1876 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1877
Elliott Hughesdcc24742011-09-07 14:02:44 -07001878 pid_t GetClinitThreadId() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001879 DCHECK(IsIdxLoaded());
1880 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1881 }
1882
Elliott Hughesdcc24742011-09-07 14:02:44 -07001883 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_),
1885 new_clinit_thread_id, false);
1886 }
1887
1888 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001889 // DCHECK(IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001890 return GetFieldObject<Class*>(
1891 OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001892 }
1893
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001894 void SetVerifyErrorClass(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001895 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_),
1896 klass, false);
1897 }
1898
Brian Carlstromc74255f2011-09-11 22:47:39 -07001899 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001900
Brian Carlstromc74255f2011-09-11 22:47:39 -07001901 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001902
jeffhaobdb76512011-09-07 11:43:16 -07001903 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001904 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001905 bool IsArrayAssignableFromArray(const Class* klass) const;
1906 bool IsAssignableFromArray(const Class* klass) const;
1907 bool IsSubClass(const Class* klass) const;
1908
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001909 // descriptor for the class such as "java.lang.Class" or "[C"
1910 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001911
Brian Carlstrom693267a2011-09-06 09:25:34 -07001912 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001913 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001914
1915 // For array classes, the class object for base element, for
1916 // instanceof/checkcast (for String[][][], this will be String).
1917 // Otherwise, NULL.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001918 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001919
Brian Carlstrom693267a2011-09-06 09:25:34 -07001920 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1921 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001922
Brian Carlstrom693267a2011-09-06 09:25:34 -07001923 // DexCache of resolved constant pool entries
1924 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1925 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001926
1927 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001928 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001929
Brian Carlstrom693267a2011-09-06 09:25:34 -07001930 // instance fields
1931 //
1932 // These describe the layout of the contents of an Object.
1933 // Note that only the fields directly declared by this class are
1934 // listed in ifields; fields declared by a superclass are listed in
1935 // the superclass's Class.ifields.
1936 //
1937 // All instance fields that refer to objects are guaranteed to be at
1938 // the beginning of the field list. num_reference_instance_fields_
1939 // specifies the number of reference fields.
1940 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001941
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001942 // Interface table (iftable_), one entry per interface supported by
1943 // this class. That means one entry for each interface we support
1944 // directly, indirectly via superclass, or indirectly via
1945 // superinterface. This will be null if neither we nor our
1946 // superclass implement any interfaces.
1947 //
1948 // Why we need this: given "class Foo implements Face", declare
1949 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1950 // is part of the Face interface. We can't easily use a single
1951 // vtable.
1952 //
1953 // For every interface a concrete class implements, we create an array
1954 // of the concrete vtable_ methods for the methods in the interface.
1955 ObjectArray<InterfaceEntry>* iftable_;
1956
Brian Carlstromdbc05252011-09-09 01:59:59 -07001957 // array of interfaces this class implements directly
1958 // see also interfaces_type_idx_
1959 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001960
1961 // array of type_idx's for interfaces this class implements directly
1962 // see also interfaces_
1963 IntArray* interfaces_type_idx_;
1964
Brian Carlstromdbc05252011-09-09 01:59:59 -07001965 // Static fields
1966 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001967
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001968 // source file name, if known. Otherwise, NULL.
1969 String* source_file_;
1970
Brian Carlstromdbc05252011-09-09 01:59:59 -07001971 // The superclass, or NULL if this is java.lang.Object or a
1972 // primitive type.
1973 // see also super_class_type_idx_;
1974 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001975
Brian Carlstromdbc05252011-09-09 01:59:59 -07001976 // If class verify fails, we must return same error on subsequent tries.
1977 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1978 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001979
Brian Carlstromdbc05252011-09-09 01:59:59 -07001980 // virtual methods defined in this class; invoked through vtable
1981 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001982
Brian Carlstromdbc05252011-09-09 01:59:59 -07001983 // Virtual method table (vtable), for use by "invoke-virtual". The
1984 // vtable from the superclass is copied in, and virtual methods from
1985 // our class either replace those from the super or are appended.
1986 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001987
Brian Carlstromdbc05252011-09-09 01:59:59 -07001988 // access flags; low 16 bits are defined by VM spec
1989 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001990
Brian Carlstromdbc05252011-09-09 01:59:59 -07001991 // For array classes, the number of array dimensions, e.g. int[][]
1992 // is 2. Otherwise 0.
1993 int32_t array_rank_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001994
Brian Carlstromdbc05252011-09-09 01:59:59 -07001995 // Total class size; used when allocating storage on gc heap.
1996 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001997
Elliott Hughes5f791332011-09-15 17:45:30 -07001998 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001999 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002000
Brian Carlstromdbc05252011-09-09 01:59:59 -07002001 // number of instance fields that are object refs
2002 size_t num_reference_instance_fields_;
2003
2004 // number of static fields that are object refs
2005 size_t num_reference_static_fields_;
2006
2007 // Total object size; used when allocating storage on gc heap.
2008 // (For interfaces and abstract classes this will be zero.)
2009 size_t object_size_;
2010
2011 // primitive type index, or kPrimNot (0); set for generated prim classes
2012 PrimitiveType primitive_type_;
2013
2014 // Bitmap of offsets of ifields.
2015 uint32_t reference_instance_offsets_;
2016
2017 // Bitmap of offsets of sfields.
2018 uint32_t reference_static_offsets_;
2019
Brian Carlstrom693267a2011-09-06 09:25:34 -07002020 // state of class initialization
2021 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002022
Brian Carlstrom693267a2011-09-06 09:25:34 -07002023 // Set in LoadClass, used to LinkClass
2024 // see also super_class_
2025 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002026
Brian Carlstrom693267a2011-09-06 09:25:34 -07002027 // TODO: ?
2028 // initiating class loader list
2029 // NOTE: for classes with low serialNumber, these are unused, and the
2030 // values are kept in a table in gDvm.
2031 // InitiatingLoaderList initiating_loader_list_;
2032
Brian Carlstrom4873d462011-08-21 15:23:39 -07002033 // Location of first static field.
2034 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002035
Brian Carlstrom693267a2011-09-06 09:25:34 -07002036 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002037 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002038};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002039
Elliott Hughes1f359b02011-07-17 14:27:17 -07002040std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002041
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002042inline void Object::SetClass(Class* new_klass) {
2043 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002044 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002045}
2046
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002047inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002048 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002049 DCHECK(GetClass() != NULL);
2050 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002051}
2052
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002053inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002054 Class* java_lang_Class = GetClass()->GetClass();
2055 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002056}
2057
Brian Carlstrom4873d462011-08-21 15:23:39 -07002058inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002059 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002060 return this == java_lang_Class;
2061}
2062
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002063inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002064 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002065}
2066
Brian Carlstromb63ec392011-08-27 17:38:27 -07002067inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002068 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002069}
2070
Brian Carlstroma663ea52011-08-19 23:33:41 -07002071inline bool Object::IsField() const {
2072 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002073 Class* java_lang_reflect_Field =
2074 java_lang_Class->GetInstanceField(0)->GetClass();
2075 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002076}
2077
2078inline bool Object::IsMethod() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002079 Class* java_lang_Class = GetClass()->GetClass();
2080 Class* java_lang_reflect_Method =
2081 java_lang_Class->GetDirectMethod(0)->GetClass();
2082 return GetClass() == java_lang_reflect_Method;
2083}
2084
2085inline bool Object::IsReferenceInstance() const {
2086 return GetClass()->IsReferenceClass();
2087}
2088
2089inline bool Object::IsWeakReferenceInstance() const {
2090 return GetClass()->IsWeakReferenceClass();
2091}
2092
2093inline bool Object::IsSoftReferenceInstance() const {
2094 return GetClass()->IsSoftReferenceClass();
2095}
2096
2097inline bool Object::IsFinalizerReferenceInstance() const {
2098 return GetClass()->IsFinalizerReferenceClass();
2099}
2100
2101inline bool Object::IsPhantomReferenceInstance() const {
2102 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002103}
2104
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002105inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002106 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002107 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002108 result = AsArray()->SizeOf();
2109 } else if (IsClass()) {
2110 result = AsClass()->SizeOf();
2111 } else {
2112 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002113 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002114 DCHECK(!IsField() || result == sizeof(Field));
2115 DCHECK(!IsMethod() || result == sizeof(Method));
2116 return result;
2117}
2118
2119inline void Field::SetOffset(MemberOffset num_bytes) {
2120 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002121 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002122 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2123 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002124 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002125 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2126 num_bytes.Uint32Value(), false);
2127}
2128
2129inline Class* Field::GetDeclaringClass() const {
2130 Class* result = GetFieldObject<Class*>(
2131 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2132 DCHECK(result != NULL);
2133 DCHECK(result->IsLoaded());
2134 return result;
2135}
2136
2137inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2138 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2139 new_declaring_class, false);
2140}
2141
2142inline Class* Method::GetDeclaringClass() const {
2143 Class* result =
2144 GetFieldObject<Class*>(
2145 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2146 DCHECK(result != NULL);
2147 DCHECK(result->IsLoaded());
2148 return result;
2149}
2150
2151inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2152 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2153 new_declaring_class, false);
2154}
2155
2156inline uint32_t Method::GetReturnTypeIdx() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002157 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002158 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2159 false);
2160}
2161
2162inline bool Method::IsReturnAReference() const {
2163 return !GetReturnType()->IsPrimitive();
2164}
2165
2166inline bool Method::IsReturnAFloat() const {
2167 return GetReturnType()->IsPrimitiveFloat();
2168}
2169
2170inline bool Method::IsReturnADouble() const {
2171 return GetReturnType()->IsPrimitiveDouble();
2172}
2173
2174inline bool Method::IsReturnALong() const {
2175 return GetReturnType()->IsPrimitiveLong();
2176}
2177
2178inline bool Method::IsReturnVoid() const {
2179 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002180}
2181
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002182inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002183 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2184}
2185
2186template<class T>
2187void ObjectArray<T>::Set(int32_t i, T* object) {
2188 if (IsValidIndex(i)) {
2189 if (object != NULL) {
2190 Class* element_class = GetClass()->GetComponentType();
2191 DCHECK(!element_class->IsPrimitive());
2192 // TODO: ArrayStoreException
2193 CHECK(object->InstanceOf(element_class));
2194 }
2195 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2196 SetFieldObject(data_offset, object, false);
2197 }
2198}
2199
2200template<class T>
2201void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2202 DCHECK(IsValidIndex(i));
2203 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2204 SetFieldObject(data_offset, object, false);
2205}
2206
2207template<class T>
2208void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2209 ObjectArray<T>* dst, int dst_pos,
2210 size_t length) {
2211 if (src->IsValidIndex(src_pos) &&
2212 src->IsValidIndex(src_pos+length-1) &&
2213 dst->IsValidIndex(dst_pos) &&
2214 dst->IsValidIndex(dst_pos+length-1)) {
2215 MemberOffset src_offset(DataOffset().Int32Value() +
2216 src_pos * sizeof(Object*));
2217 MemberOffset dst_offset(DataOffset().Int32Value() +
2218 dst_pos * sizeof(Object*));
2219 Class* array_class = dst->GetClass();
2220 if (array_class == src->GetClass()) {
2221 // No need for array store checks if arrays are of the same type
2222 for (size_t i=0; i < length; i++) {
2223 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2224 dst->SetFieldObject(dst_offset, object, false);
2225 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2226 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2227 }
2228 } else {
2229 Class* element_class = array_class->GetComponentType();
2230 CHECK(!element_class->IsPrimitive());
2231 for (size_t i=0; i < length; i++) {
2232 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2233 // TODO: ArrayStoreException
2234 CHECK(object == NULL || object->InstanceOf(element_class));
2235 dst->SetFieldObject(dst_offset, object, false);
2236 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2237 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2238 }
2239 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002240 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002241 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002242}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002243
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002244class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002245 private:
Brian Carlstrom4873d462011-08-21 15:23:39 -07002246 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002247 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002248 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2249};
2250
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002251class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002252 private:
2253 CharArray* ASCII_;
2254 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002255 int64_t serialVersionUID_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002256 uint32_t REPLACEMENT_CHAR_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002257 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002258 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2259};
2260
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002261class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002262 private:
2263 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2264 uint32_t TYPE_BOOLEAN_;
2265 uint32_t TYPE_BYTE_;
2266 uint32_t TYPE_CHAR_;
2267 uint32_t TYPE_DOUBLE_;
2268 uint32_t TYPE_FLOAT_;
2269 uint32_t TYPE_INTEGER_;
2270 uint32_t TYPE_LONG_;
2271 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002272 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002273 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2274};
2275
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002276class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002277 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002278 ObjectArray<Object>* NO_ANNOTATIONS_;
2279 Object* ORDER_BY_SIGNATURE_;
2280 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002281 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2282};
2283
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002284template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002285class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002286 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002287 typedef T ElementType;
2288
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002289 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002290
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002291 const T* GetData() const {
2292 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002293 }
2294
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002295 T* GetData() {
2296 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002297 }
2298
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002299 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002300 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002301 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002302 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002303 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002304 }
2305
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002306 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002307 // TODO: ArrayStoreException
2308 if (IsValidIndex(i)) {
2309 GetData()[i] = value;
2310 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002311 }
2312
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002313 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002314 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002315 CHECK(array_class != NULL);
2316 array_class_ = array_class;
2317 }
2318
Brian Carlstroma663ea52011-08-19 23:33:41 -07002319 static void ResetArrayClass() {
2320 CHECK(array_class_ != NULL);
2321 array_class_ = NULL;
2322 }
2323
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002324 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002325 // Location of first element.
2326 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002327
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002328 static Class* array_class_;
2329
Carl Shapirof88c9522011-08-06 15:47:38 -07002330 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002331};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002332
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002333inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2334 DCHECK(NULL == GetFieldObject<IntArray*>(
2335 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2336 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2337 new_interfaces_idx, false);
2338}
2339
2340// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002341class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002342 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002343 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002344 const CharArray* result = GetFieldObject<const CharArray*>(
2345 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2346 DCHECK(result != NULL);
2347 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002348 }
2349
Elliott Hughes814e4032011-08-23 12:07:56 -07002350 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002351 int32_t result = GetField32(
2352 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2353 DCHECK_LE(0, result);
2354 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002355 }
2356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002357 int32_t GetLength() const;
2358
2359 int32_t GetHashCode() const;
2360
2361 void ComputeHashCode() {
2362 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002363 }
2364
Elliott Hughes814e4032011-08-23 12:07:56 -07002365 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002366 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002367 }
2368
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002369 uint16_t CharAt(int32_t index) const;
2370
Brian Carlstromc74255f2011-09-11 22:47:39 -07002371 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002372
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002373 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002374 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002375 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002376
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002377 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002378
Jesse Wilson8989d992011-08-02 13:39:42 -07002379 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002380 const char* utf8_data_in);
2381
2382 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2383
2384 static String* Alloc(Class* java_lang_String, CharArray* array);
2385
2386 bool Equals(const char* modified_utf8) const;
2387
2388 // TODO: do we need this overload? give it a more intention-revealing name.
2389 bool Equals(const StringPiece& modified_utf8) const;
2390
2391 bool Equals(const String* that) const;
2392
2393 // TODO: do we need this overload? give it a more intention-revealing name.
2394 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2395 int32_t that_length) const;
2396
2397 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2398 std::string ToModifiedUtf8() const;
2399
2400 static Class* GetJavaLangString() {
2401 DCHECK(java_lang_String_ != NULL);
2402 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002403 }
2404
Brian Carlstroma663ea52011-08-19 23:33:41 -07002405 static void SetClass(Class* java_lang_String);
2406 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002407
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002408 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002409 void SetHashCode(int32_t new_hash_code) {
2410 DCHECK_EQ(0u,
2411 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2412 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2413 new_hash_code, false);
2414 }
2415
2416 void SetCount(int32_t new_count) {
2417 DCHECK_LE(0, new_count);
2418 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2419 }
2420
2421 void SetOffset(int32_t new_offset) {
2422 DCHECK_LE(0, new_offset);
2423 DCHECK_GE(GetLength(), new_offset);
2424 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2425 }
2426
2427 void SetArray(CharArray* new_array) {
2428 DCHECK(new_array != NULL);
2429 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2430 }
2431
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002432 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2433 CharArray* array_;
2434
Brian Carlstromdbc05252011-09-09 01:59:59 -07002435 int32_t count_;
2436
Carl Shapirof88c9522011-08-06 15:47:38 -07002437 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002438
Elliott Hughes289da822011-08-16 10:11:20 -07002439 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002440
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002441 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002442
Brian Carlstrom693267a2011-09-06 09:25:34 -07002443 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002444 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002445};
2446
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002447inline const String* Field::GetName() const {
2448 DCHECK(GetDeclaringClass()->IsLoaded());
2449 String* result =
2450 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2451 DCHECK(result != NULL);
2452 return result;
2453}
2454
2455inline void Field::SetName(String* new_name) {
2456 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2457 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002458}
2459
2460inline uint32_t Field::GetAccessFlags() const {
2461 DCHECK(GetDeclaringClass()->IsLoaded());
2462 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2463}
2464
2465inline uint32_t Field::GetTypeIdx() const {
2466 DCHECK(GetDeclaringClass()->IsIdxLoaded());
2467 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2468}
2469
2470inline MemberOffset Field::GetOffset() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002471 DCHECK(GetDeclaringClass()->IsResolved());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002472 return MemberOffset(
2473 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2474}
2475
2476inline MemberOffset Field::GetOffsetDuringLinking() const {
2477 DCHECK(GetDeclaringClass()->IsLoaded());
2478 return MemberOffset(
2479 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2480}
2481
2482inline const String* Method::GetName() const {
2483 DCHECK(GetDeclaringClass()->IsLoaded());
2484 const String* result =
2485 GetFieldObject<const String*>(
2486 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2487 DCHECK(result != NULL);
2488 return result;
2489}
2490
2491inline void Method::SetName(String* new_name) {
2492 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2493 new_name, false);
2494
2495}
2496
jeffhaoe23d93c2011-09-15 14:48:43 -07002497inline ByteArray* Method::GetRegisterMapData() const {
2498 return GetFieldObject<ByteArray*>(
2499 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2500}
2501
jeffhaoa0a764a2011-09-16 10:43:38 -07002502inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002503 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002504 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002505}
2506
2507inline ByteArray* Method::GetRegisterMapHeader() const {
2508 return GetFieldObject<ByteArray*>(
2509 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2510}
2511
jeffhaoa0a764a2011-09-16 10:43:38 -07002512inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002513 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002514 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002515}
2516
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002517inline String* Method::GetShorty() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002518 DCHECK(GetDeclaringClass()->IsLoaded());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002519 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002520 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2521}
2522
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002523inline void Method::SetShorty(String* new_shorty) {
2524 DCHECK(NULL == GetFieldObject<String*>(
2525 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2526 DCHECK_LE(1, new_shorty->GetLength());
2527 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2528}
2529
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002530inline const String* Method::GetSignature() const {
2531 DCHECK(GetDeclaringClass()->IsLoaded());
2532 const String* result =
2533 GetFieldObject<const String*>(
2534 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2535 DCHECK(result != NULL);
2536 return result;
2537}
2538
2539inline void Method::SetSignature(String* new_signature) {
2540 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2541 new_signature, false);
2542}
2543
2544inline uint32_t Class::GetAccessFlags() const {
2545 // Check class is loaded or this is java.lang.String that has a
2546 // circularity issue during loading the names of its members
2547 DCHECK(IsLoaded() || this == String::GetJavaLangString() ||
2548 this == Field::GetJavaLangReflectField() ||
2549 this == Method::GetJavaLangReflectMethod());
2550 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2551}
2552
2553inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002554 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002555 DCHECK(new_descriptor != NULL);
2556 DCHECK_NE(0, new_descriptor->GetLength());
2557 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2558 new_descriptor, false);
2559}
2560
Brian Carlstromc74255f2011-09-11 22:47:39 -07002561inline String* Class::GetSourceFile() const {
2562 DCHECK(IsLoaded());
2563 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2564}
2565
2566inline void Class::SetSourceFile(String* new_source_file) {
2567 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2568}
2569
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002570inline uint32_t Method::GetAccessFlags() const {
2571 DCHECK(GetDeclaringClass()->IsLoaded());
2572 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2573}
2574
2575inline uint16_t Method::GetMethodIndex() const {
Elliott Hughes5fe594f2011-09-08 12:33:17 -07002576 DCHECK(GetDeclaringClass()->IsResolved());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002577 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002578}
2579
2580inline uint16_t Method::NumRegisters() const {
2581 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002582 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002583}
2584
2585inline uint16_t Method::NumIns() const {
2586 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002587 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588}
2589
2590inline uint16_t Method::NumOuts() const {
2591 DCHECK(GetDeclaringClass()->IsLoaded());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002592 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002593}
2594
2595inline uint32_t Method::GetProtoIdx() const {
2596 DCHECK(GetDeclaringClass()->IsLoaded());
2597 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2598}
2599
2600// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002601class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002602 public:
2603 void SetDetailMessage(String* new_detail_message) {
2604 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2605 new_detail_message, false);
2606 }
2607
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002608 private:
2609 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2610 Throwable* cause_;
2611 String* detail_message_;
2612 Object* stack_state_; // Note this is Java volatile:
2613 Object* stack_trace_;
2614 Object* suppressed_exceptions_;
2615
Brian Carlstrom693267a2011-09-06 09:25:34 -07002616 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002617 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2618};
2619
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002620// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002621class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002622 public:
2623 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002624 return GetFieldObject<const String*>(
2625 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002626 }
2627
2628 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002629 return GetFieldObject<const String*>(
2630 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002631 }
2632
2633 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002634 return GetFieldObject<const String*>(
2635 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002636 }
2637
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002638 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002639 return GetField32(
2640 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002641 }
2642
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002643 static StackTraceElement* Alloc(const String* declaring_class,
2644 const String* method_name,
2645 const String* file_name,
2646 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002647
2648 static void SetClass(Class* java_lang_StackTraceElement);
2649
2650 static void ResetClass();
2651
2652 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002653 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002654 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002655 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002656 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002657 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002658
2659 static Class* GetStackTraceElement() {
2660 DCHECK(java_lang_StackTraceElement_ != NULL);
2661 return java_lang_StackTraceElement_;
2662 }
2663
2664 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002665
2666 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002667 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2668};
2669
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002670class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002671 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002672 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002673 Class* interface = Get(kInterface)->AsClass();
2674 DCHECK(interface != NULL);
2675 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002676 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002677
Brian Carlstrom30b94452011-08-25 21:35:26 -07002678 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002679 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002680 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002681 DCHECK(Get(kInterface) == NULL);
2682 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002683 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002684
Brian Carlstrom86927212011-09-15 11:31:11 -07002685 size_t GetMethodArrayCount() const {
2686 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2687 if (method_array == 0) {
2688 return 0;
2689 }
2690 return method_array->GetLength();
2691 }
2692
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002693 ObjectArray<Method>* GetMethodArray() const {
2694 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2695 DCHECK(method_array != NULL);
2696 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002697 }
2698
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002699 void SetMethodArray(ObjectArray<Method>* new_ma) {
2700 DCHECK(new_ma != NULL);
2701 DCHECK(Get(kMethodArray) == NULL);
2702 Set(kMethodArray, new_ma);
2703 }
2704
2705 static size_t LengthAsArray() {
2706 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002707 }
2708
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002709 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002710
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002711 enum ArrayIndex {
2712 // Points to the interface class.
2713 kInterface = 0,
2714 // Method pointers into the vtable, allow fast map from interface
2715 // method index to concrete instance method.
2716 kMethodArray = 1,
2717 kMax = 2,
2718 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002719
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002720 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002721};
2722
2723} // namespace art
2724
2725#endif // ART_SRC_OBJECT_H_