blob: 80a4ce92d1f385877cd105ff7a84f21ff6e0dc9f [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
Elliott Hughes20cde902011-10-04 17:37:27 -0700136// Special runtime-only flags.
137// Note: if only kAccClassIsReference is set, we have a soft reference.
138static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
139static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
140static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
141static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
142static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700143
144static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
145 | kAccClassIsWeakReference
146 | kAccClassIsFinalizerReference
147 | kAccClassIsPhantomReference);
148
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700150 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151 */
152/*
153 * A magic value for refOffsets. Ignore the bits and walk the super
154 * chain when this is the value.
155 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
156 * fields followed by 2 ref instance fields.]
157 */
158#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700159#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
160#define CLASS_OFFSET_ALIGNMENT 4
161#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
162/*
163 * Given an offset, return the bit number which would encode that offset.
164 * Local use only.
165 */
166#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700167 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700168 CLASS_OFFSET_ALIGNMENT)
169/*
170 * Is the given offset too large to be encoded?
171 */
172#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
173 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
174/*
175 * Return a single bit, encoding the offset.
176 * Undefined if the offset is too large, as defined above.
177 */
178#define CLASS_BIT_FROM_OFFSET(byteOffset) \
179 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
180/*
181 * Return an offset, given a bit number as returned from CLZ.
182 */
183#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700184 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186#define OFFSET_OF_OBJECT_MEMBER(type, field) \
187 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700188
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700189// Classes shared with the managed side of the world need to be packed
190// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700191#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700194class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700195 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 static MemberOffset ClassOffset() {
197 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198 }
199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 }
203
204 void SetClass(Class* new_klass);
205
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700206 bool InstanceOf(const Class* klass) const;
207
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700208 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209
Elliott Hughes081be7f2011-09-18 16:50:26 -0700210 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700211
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 static MemberOffset MonitorOffset() {
213 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
214 }
215
Elliott Hughes5f791332011-09-15 17:45:30 -0700216 volatile int32_t* GetRawLockWordAddress() {
217 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
218 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
219 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 }
221
Elliott Hughes5f791332011-09-15 17:45:30 -0700222 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223
Elliott Hughes5f791332011-09-15 17:45:30 -0700224 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Ian Rogersff1ed472011-09-20 13:46:24 -0700226 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Elliott Hughes5f791332011-09-15 17:45:30 -0700228 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Elliott Hughes5f791332011-09-15 17:45:30 -0700232 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233
Elliott Hughes5f791332011-09-15 17:45:30 -0700234 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700236 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237
238 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700239 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 return down_cast<Class*>(this);
241 }
242
243 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 return down_cast<const Class*>(this);
246 }
247
Brian Carlstrom4873d462011-08-21 15:23:39 -0700248 bool IsClassClass() const;
249
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700250 bool IsObjectArray() const;
251
252 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700253 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700256 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257
Brian Carlstromb63ec392011-08-27 17:38:27 -0700258 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700259
260 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700261 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700262 return down_cast<Array*>(this);
263 }
264
265 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700266 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700267 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268 }
269
Brian Carlstroma663ea52011-08-19 23:33:41 -0700270 bool IsString() const;
271
272 String* AsString() {
273 DCHECK(IsString());
274 return down_cast<String*>(this);
275 }
276
277 bool IsMethod() const;
278
279 Method* AsMethod() {
280 DCHECK(IsMethod());
281 return down_cast<Method*>(this);
282 }
283
Brian Carlstrom4873d462011-08-21 15:23:39 -0700284 const Method* AsMethod() const {
285 DCHECK(IsMethod());
286 return down_cast<const Method*>(this);
287 }
288
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289 bool IsField() const;
290
291 Field* AsField() {
292 DCHECK(IsField());
293 return down_cast<Field*>(this);
294 }
295
Brian Carlstrom4873d462011-08-21 15:23:39 -0700296 const Field* AsField() const {
297 DCHECK(IsField());
298 return down_cast<const Field*>(this);
299 }
300
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 bool IsReferenceInstance() const;
302
303 bool IsWeakReferenceInstance() const;
304
305 bool IsSoftReferenceInstance() const;
306
307 bool IsFinalizerReferenceInstance() const;
308
309 bool IsPhantomReferenceInstance() const;
310
311 // Accessors for Java type fields
312 template<class T>
313 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700314 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
315 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 Heap::VerifyObject(result);
317 return result;
318 }
319
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700320 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700322 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
323 if (new_value != NULL) {
324 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700326 }
327
328 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
329 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700330 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
331 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700333 return android_atomic_acquire_load(word_addr);
334 } else {
335 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
338
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700339 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
340 if (this_is_valid) {
341 Heap::VerifyObject(this);
342 }
343 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700344 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700345 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700346 /*
347 * TODO: add an android_atomic_synchronization_store() function and
348 * use it in the 32-bit volatile set handlers. On some platforms we
349 * can use a fast atomic instruction and avoid the barriers.
350 */
351 ANDROID_MEMBAR_STORE();
352 *word_addr = new_value;
353 ANDROID_MEMBAR_FULL();
354 } else {
355 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 }
358
359 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
360 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700361 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700362 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700363 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700364 uint64_t result = QuasiAtomicRead64(addr);
365 ANDROID_MEMBAR_FULL();
366 return result;
367 } else {
368 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370 }
371
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700372 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700374 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
375 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700377 ANDROID_MEMBAR_STORE();
378 QuasiAtomicSwap64(new_value, addr);
379 // Post-store barrier not required due to use of atomic op or mutex.
380 } else {
381 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 }
384
385 protected:
386 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 template<class T>
388 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700389 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 }
391
392 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700393 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
394 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 }
396
397 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700398 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700399
Elliott Hughes5f791332011-09-15 17:45:30 -0700400 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401
Elliott Hughes5f791332011-09-15 17:45:30 -0700402 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700403 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700404 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405};
406
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700407struct ObjectIdentityHash {
408 size_t operator()(const art::Object* const& obj) const {
409#ifdef MOVING_GARBAGE_COLLECTOR
410 // TODO: we'll need to use the Object's internal concept of identity
411 UNIMPLEMENTED(FATAL);
412#endif
413 return reinterpret_cast<size_t>(obj);
414 }
415};
416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700418class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400419 private:
420 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700422 friend struct AccessibleObjectOffsets; // for verifying offset information
423 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400424};
425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700426// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700427class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700428 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700429 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700430
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700431 void SetDeclaringClass(Class *new_declaring_class);
432
433 const String* GetName() const;
434
435 void SetName(String* new_name);
436
437 uint32_t GetAccessFlags() const;
438
439 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700440 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700441 }
442
Elliott Hughes80609252011-09-23 17:24:51 -0700443 bool IsPublic() const {
444 return (GetAccessFlags() & kAccPublic) != 0;
445 }
446
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700447 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 {
Elliott Hughes80609252011-09-23 17:24:51 -0700452 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700453 }
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
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 MemberOffset GetOffsetDuringLinking() const;
475
476 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700477
Brian Carlstrom4873d462011-08-21 15:23:39 -0700478 // field access, null object for static fields
479 bool GetBoolean(const Object* object) const;
480 void SetBoolean(Object* object, bool z) const;
481 int8_t GetByte(const Object* object) const;
482 void SetByte(Object* object, int8_t b) const;
483 uint16_t GetChar(const Object* object) const;
484 void SetChar(Object* object, uint16_t c) const;
485 uint16_t GetShort(const Object* object) const;
486 void SetShort(Object* object, uint16_t s) const;
487 int32_t GetInt(const Object* object) const;
488 void SetInt(Object* object, int32_t i) const;
489 int64_t GetLong(const Object* object) const;
490 void SetLong(Object* object, int64_t j) const;
491 float GetFloat(const Object* object) const;
492 void SetFloat(Object* object, float f) const;
493 double GetDouble(const Object* object) const;
494 void SetDouble(Object* object, double d) const;
495 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700496 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700497
Ian Rogersce9eca62011-10-07 17:11:03 -0700498 // raw field accesses
499 uint32_t Get32(const Object* object) const;
500 void Set32(Object* object, uint32_t new_value) const;
501 uint64_t Get64(const Object* object) const;
502 void Set64(Object* object, uint64_t new_value) const;
503 Object* GetObj(const Object* object) const;
504 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 static Class* GetJavaLangReflectField() {
507 DCHECK(java_lang_reflect_Field_ != NULL);
508 return java_lang_reflect_Field_;
509 }
510
511 static void SetClass(Class* java_lang_reflect_Field);
512 static void ResetClass();
513
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 bool IsVolatile() const {
515 return (GetAccessFlags() & kAccVolatile) != 0;
516 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700517
Elliott Hughes80609252011-09-23 17:24:51 -0700518 void InitJavaFields();
519
buzbee1da522d2011-09-04 11:22:20 -0700520 private:
Elliott Hughes80609252011-09-23 17:24:51 -0700521 void InitJavaFieldsLocked();
522
Jesse Wilson35baaab2011-08-10 16:18:03 -0400523 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700524
Jesse Wilson35baaab2011-08-10 16:18:03 -0400525 // The class in which this field is declared.
526 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700527
Jesse Wilson35baaab2011-08-10 16:18:03 -0400528 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700529
Brian Carlstromdbc05252011-09-09 01:59:59 -0700530 const String* name_;
531
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700532 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700533 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400534
Brian Carlstromdbc05252011-09-09 01:59:59 -0700535 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700536
Jesse Wilson35baaab2011-08-10 16:18:03 -0400537 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538
539 // Offset of field within an instance or in the Class' static fields
540 uint32_t offset_;
541
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 // Dex cache index of resolved type
543 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400544
Brian Carlstrom693267a2011-09-06 09:25:34 -0700545 int32_t slot_;
546
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547 static Class* java_lang_reflect_Field_;
548
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400550 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700551};
552
Jesse Wilson6bf19152011-09-29 13:12:33 -0400553// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700554class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700556 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700557 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700558 Object* obj,
559 Thread* thread,
560 byte* args,
561 JValue* result);
562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700563 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700564
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700565 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700566
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700567 static MemberOffset DeclaringClassOffset() {
568 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700569 }
570
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700571 // Returns the method name, e.g. "<init>" or "eatLunch"
572 const String* GetName() const;
573
574 void SetName(String* new_name);
575
jeffhaoe23d93c2011-09-15 14:48:43 -0700576 ByteArray* GetRegisterMapData() const;
577
jeffhaoa0a764a2011-09-16 10:43:38 -0700578 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700579
580 ByteArray* GetRegisterMapHeader() const;
581
jeffhaoa0a764a2011-09-16 10:43:38 -0700582 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700583
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700584 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700585
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700586 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587
588 const String* GetSignature() const;
589
590 void SetSignature(String* new_signature);
591
592 bool HasSameNameAndDescriptor(const Method* that) const;
593
594 uint32_t GetAccessFlags() const;
595
596 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700597 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598 }
599
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700600 // Returns true if the method is declared public.
601 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700602 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700603 }
604
605 // Returns true if the method is declared private.
606 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700607 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700608 }
609
610 // Returns true if the method is declared static.
611 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700612 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700613 }
614
Brian Carlstrom1f870082011-08-23 16:02:11 -0700615 // Returns true if the method is a constructor.
616 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700617 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700618 }
619
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700620 // Is this method <clinit>
621 bool IsClassInitializer() const;
622
Brian Carlstrom1f870082011-08-23 16:02:11 -0700623 // Returns true if the method is static, private, or a constructor.
624 bool IsDirect() const {
625 return IsStatic() || IsPrivate() || IsConstructor();
626 }
627
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700628 // Returns true if the method is declared synchronized.
629 bool IsSynchronized() const {
630 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632 }
633
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700634 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700635 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700636 }
637
Elliott Hughes80609252011-09-23 17:24:51 -0700638 bool IsMiranda() const {
639 return (GetAccessFlags() & kAccMiranda) != 0;
640 }
641
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700642 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700643 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 }
645
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 }
649
650 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700652 }
653
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 uint16_t GetMethodIndex() const;
655
656 size_t GetVtableIndex() const {
657 return GetMethodIndex();
658 }
659
660 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700661 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662 }
663
664 static MemberOffset MethodIndexOffset() {
665 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
666 }
667
668 uint32_t GetCodeItemOffset() const {
669 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
670 }
671
672 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700673 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674 }
675
676 // Number of 32bit registers that would be required to hold all the arguments
677 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700678
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700679 // Number of argument bytes required for densely packing the
680 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700681 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700682
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700683 uint16_t NumRegisters() const;
684
685 void SetNumRegisters(uint16_t new_num_registers) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700686 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700687 }
688
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700689 uint16_t NumIns() const;
690
691 void SetNumIns(uint16_t new_num_ins) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700692 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700693 }
694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700696
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700697 void SetNumOuts(uint16_t new_num_outs) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700698 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700699 }
700
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700701 uint32_t GetProtoIdx() const;
702
703 void SetProtoIdx(uint32_t new_proto_idx) {
704 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700705 }
706
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700707 ObjectArray<String>* GetDexCacheStrings() const;
708 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
709
710 static MemberOffset DexCacheStringsOffset() {
711 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
712 }
713
buzbee2a475e72011-09-07 17:19:17 -0700714 static MemberOffset DexCacheResolvedTypesOffset() {
715 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
716 }
717
buzbee34cd9e52011-09-08 14:31:52 -0700718 static MemberOffset DexCacheResolvedFieldsOffset() {
719 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
720 }
721
buzbee1da522d2011-09-04 11:22:20 -0700722 static MemberOffset DexCacheInitializedStaticStorageOffset() {
723 return OFFSET_OF_OBJECT_MEMBER(Method,
724 dex_cache_initialized_static_storage_);
725 }
726
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700727 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
728 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
729
730 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
731 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
732
733 ObjectArray<Field>* GetDexCacheResolvedFields() const;
734 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
735
736 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
737 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
738
739 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
740 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
741
742 void SetReturnTypeIdx(uint32_t new_return_type_idx);
743
744 Class* GetReturnType() const;
745
746 bool IsReturnAReference() const;
747
748 bool IsReturnAFloat() const;
749
750 bool IsReturnADouble() const;
751
Ian Rogersb033c752011-07-20 12:22:35 -0700752 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700753 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700754 }
755
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700756 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700757
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700758 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700759
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700760 // "Args" may refer to any of the 3 levels of "Args."
761 // To avoid confusion, our code will denote which "Args" clearly:
762 // 1. UserArgs: Args that a user see.
763 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
764 // receiver.
765 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
766 // E.g., the first in Args is Method* for both static and non-static
767 // methods. And CConvArgs doesn't deal with the receiver because
768 // receiver is hardwired in an implicit register, so CConvArgs doesn't
769 // need to deal with it.
770 //
771 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700772 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700773
774 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700775 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700776 size_t NumReferenceArgs() const;
777
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700778 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700779 size_t NumLongOrDoubleArgs() const;
780
Ian Rogersb033c752011-07-20 12:22:35 -0700781 // Is the given method parameter a reference?
782 bool IsParamAReference(unsigned int param) const;
783
784 // Is the given method parameter a long or double?
785 bool IsParamALongOrDouble(unsigned int param) const;
786
Ian Rogersdf20fe02011-07-20 20:34:16 -0700787 // Size in bytes of the given parameter
788 size_t ParamSize(unsigned int param) const;
789
790 // Size in bytes of the return value
791 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700792
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700793 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
794
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700795 const void* GetCode() const {
796 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700797 }
798
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700799 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700800 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
801 }
802
803 uint32_t GetOatCodeOffset() const {
804 CHECK(!Runtime::Current()->IsStarted());
805 return reinterpret_cast<uint32_t>(GetCode());
806 }
807
808 void SetOatCodeOffset(uint32_t code_offset) {
809 CHECK(!Runtime::Current()->IsStarted());
810 SetCode(reinterpret_cast<void*>(code_offset));
811 }
812
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700813 static MemberOffset GetCodeOffset() {
814 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700815 }
816
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700817 const uint32_t* GetMappingTable() const {
818 const uint32_t* map = GetMappingTableRaw();
819 if (map == NULL) {
820 return map;
821 }
822 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700823 }
824
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700825 uint32_t GetMappingTableLength() const {
826 const uint32_t* map = GetMappingTableRaw();
827 if (map == NULL) {
828 return 0;
829 }
830 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700831 }
832
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700833 const uint32_t* GetMappingTableRaw() const {
834 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700835 }
836
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700837 void SetMappingTable(const uint32_t* mapping_table) {
838 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
839 mapping_table, false);
840 }
841
842 uint32_t GetOatMappingTableOffset() const {
843 CHECK(!Runtime::Current()->IsStarted());
844 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
845 }
846
847 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
848 CHECK(!Runtime::Current()->IsStarted());
849 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
850 }
851
852 const uint16_t* GetVmapTable() const {
853 const uint16_t* vmap = GetVmapTableRaw();
854 if (vmap == NULL) {
855 return vmap;
856 }
857 return vmap + 1;
858 }
859
860 uint16_t GetVmapTableLength() const {
861 const uint16_t* vmap = GetVmapTableRaw();
862 if (vmap == NULL) {
863 return 0;
864 }
865 return *vmap;
866 }
867
868 const uint16_t* GetVmapTableRaw() const {
869 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
870 }
871
872 void SetVmapTable(const uint16_t* vmap_table) {
873 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
874 }
875
876 uint32_t GetOatVmapTableOffset() const {
877 CHECK(!Runtime::Current()->IsStarted());
878 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
879 }
880
881 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
882 CHECK(!Runtime::Current()->IsStarted());
883 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700884 }
885
Shih-wei Liaod11af152011-08-23 16:02:11 -0700886 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700887 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
888 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700889 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
890 return result;
891 }
892
893 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700894 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700895 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
896 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
897 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700898 }
899
Shih-wei Liaod11af152011-08-23 16:02:11 -0700900 size_t GetReturnPcOffsetInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700901 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
902 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700903 }
904
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700905 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700906 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
908 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
909 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700910 }
911
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700912 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700913
Brian Carlstrom16192862011-09-12 17:50:06 -0700914 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700915
Brian Carlstrom16192862011-09-12 17:50:06 -0700916 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700917
Ian Rogersb033c752011-07-20 12:22:35 -0700918 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700919 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700920 }
921
Brian Carlstrom78128a62011-09-15 17:21:19 -0700922 const void* GetNativeMethod() const {
923 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
924 }
925
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700926 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700927 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700928 InvokeStub* result = GetFieldPtr<InvokeStub*>(
929 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
930 // TODO: DCHECK(result != NULL); should be ahead of time compiled
931 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700932 }
933
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700934 void SetInvokeStub(InvokeStub* invoke_stub) {
935 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
936 invoke_stub, false);
937 }
938
939 uint32_t GetOatInvokeStubOffset() const {
940 CHECK(!Runtime::Current()->IsStarted());
941 return reinterpret_cast<uint32_t>(GetInvokeStub());
942 }
943
944 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
945 CHECK(!Runtime::Current()->IsStarted());
946 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
947 }
948
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700949 static MemberOffset GetInvokeStubOffset() {
950 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700951 }
952
buzbee561227c2011-09-02 15:28:19 -0700953 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
954 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
955 }
956
957 static MemberOffset GetDexCacheResolvedMethodsOffset() {
958 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
959 }
960
961 static MemberOffset GetMethodIndexOffset() {
962 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
963 }
964
Ian Rogers90865722011-09-19 11:11:44 -0700965 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700966 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700967 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700968
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700969 void SetCoreSpillMask(uint32_t core_spill_mask) {
970 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700971 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700972 }
973
Ian Rogers90865722011-09-19 11:11:44 -0700974 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700975 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
976 }
977
978 void SetFpSpillMask(uint32_t fp_spill_mask) {
979 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700980 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700981 }
982
Ian Rogers90865722011-09-19 11:11:44 -0700983 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700984 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700985 Runtime* runtime = Runtime::Current();
986 bool result = false;
987 for (int i=0; i < Runtime::kLastCalleeSaveType; i++) {
988 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
989 result = true;
990 break;
991 }
992 }
Ian Rogers90865722011-09-19 11:11:44 -0700993 // Check that if we do think it is phony it looks like the callee save method
994 DCHECK(!result || GetCoreSpillMask() != 0);
995 return result;
996 }
997
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700998 // Converts a native PC to a dex PC. TODO: this is a no-op
999 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -07001000 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001001
1002 // Converts a dex PC to a native PC. TODO: this is a no-op
1003 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -07001004 uintptr_t ToNativePC(const uint32_t dex_pc) const;
1005
1006 // Find the catch block for the given exception type and dex_pc
1007 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001008
Elliott Hughes80609252011-09-23 17:24:51 -07001009 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
1010
1011 static Class* GetConstructorClass() {
1012 return java_lang_reflect_Constructor_;
1013 }
1014
1015 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016 return java_lang_reflect_Method_;
1017 }
1018
Elliott Hughes80609252011-09-23 17:24:51 -07001019 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001020
Elliott Hughes418d20f2011-09-22 14:00:39 -07001021 void InitJavaFields();
1022
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001023 private:
1024 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001025 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001026
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001027 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1028 // the class we are a part of
1029 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001030 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001031 Object* java_formal_type_parameters_;
1032 Object* java_generic_exception_types_;
1033 Object* java_generic_parameter_types_;
1034 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -07001035
Brian Carlstrom693267a2011-09-06 09:25:34 -07001036 String* name_;
1037
Elliott Hughes418d20f2011-09-22 14:00:39 -07001038 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001039 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001040 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001041
Brian Carlstrom693267a2011-09-06 09:25:34 -07001042 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001043 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1044
1045 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1046 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1047
1048 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1049 ObjectArray<Field>* dex_cache_resolved_fields_;
1050
1051 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1052 ObjectArray<Method>* dex_cache_resolved_methods_;
1053
Brian Carlstromdbc05252011-09-09 01:59:59 -07001054 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1055 ObjectArray<Class>* dex_cache_resolved_types_;
1056
1057 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1058 ObjectArray<String>* dex_cache_strings_;
1059
jeffhaoe23d93c2011-09-15 14:48:43 -07001060 // Byte arrays that hold data for the register maps
1061 const ByteArray* register_map_data_;
1062 const ByteArray* register_map_header_;
1063
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001064 // The short-form method descriptor string.
1065 String* shorty_;
1066
Brian Carlstromdbc05252011-09-09 01:59:59 -07001067 // The method descriptor. This represents the parameters a method
1068 // takes and value it returns. This string is a list of the type
1069 // descriptors for the parameters enclosed in parenthesis followed
1070 // by the return type descriptor. For example, for the method
1071 //
1072 // Object mymethod(int i, double d, Thread t)
1073 //
1074 // the method descriptor would be
1075 //
1076 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1077 String* signature_;
1078
1079 uint32_t java_generic_types_are_initialized_;
1080
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001081 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001082 uint32_t access_flags_;
1083
1084 // Compiled code associated with this method for callers from managed code.
1085 // May be compiled managed code or a bridge for invoking a native method.
1086 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001088 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001089 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001090
Brian Carlstrom693267a2011-09-06 09:25:34 -07001091 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001092 uint32_t core_spill_mask_;
1093
1094 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001095 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001096
Brian Carlstrom693267a2011-09-06 09:25:34 -07001097 // Total size in bytes of the frame
1098 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099
Brian Carlstrom693267a2011-09-06 09:25:34 -07001100 // Native invocation stub entry point for calling from native to managed code.
1101 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001102
Brian Carlstrom693267a2011-09-06 09:25:34 -07001103 // Index of the return type
1104 uint32_t java_return_type_idx_;
1105
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001106 const uint32_t* mapping_table_;
1107
Brian Carlstrom693267a2011-09-06 09:25:34 -07001108 // For concrete virtual methods, this is the offset of the method
1109 // in Class::vtable_.
1110 //
1111 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001112 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001113 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001114
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001115 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001116 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001117
Brian Carlstrom693267a2011-09-06 09:25:34 -07001118 // Method bounds; not needed for an abstract method.
1119 //
1120 // For a native method, we compute the size of the argument list, and
1121 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001122 uint32_t num_ins_;
1123 uint32_t num_outs_;
1124 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001125
Brian Carlstrom693267a2011-09-06 09:25:34 -07001126 // Method prototype descriptor string (return and argument types).
1127 uint32_t proto_idx_;
1128
1129 // Offset of return PC within frame for compiled code (in bytes)
1130 size_t return_pc_offset_in_bytes_;
1131
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001132 const uint16_t* vmap_table_;
1133
Brian Carlstrom693267a2011-09-06 09:25:34 -07001134 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001135
Elliott Hughes80609252011-09-23 17:24:51 -07001136 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001137 static Class* java_lang_reflect_Method_;
1138
Brian Carlstrom693267a2011-09-06 09:25:34 -07001139 friend class ImageWriter; // for relocating code_ and invoke_stub_
1140 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001141 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001142};
1143
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001144class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001145 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001146 // A convenience for code that doesn't know the component size,
1147 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001148 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001149
Brian Carlstromb63ec392011-08-27 17:38:27 -07001150 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001151
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001152 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001153
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001154 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001155 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001156 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001157
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001158 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001159 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001160 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001161 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001162
buzbeec143c552011-08-20 17:38:58 -07001163 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001164 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001165 }
1166
1167 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001168 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001169 }
1170
Elliott Hughesbf86d042011-08-31 17:53:14 -07001171 void* GetRawData() {
1172 return reinterpret_cast<void*>(first_element_);
1173 }
1174
Elliott Hughes289da822011-08-16 10:11:20 -07001175 protected:
1176 bool IsValidIndex(int32_t index) const {
1177 if (index < 0 || index >= length_) {
Elliott Hughes80609252011-09-23 17:24:51 -07001178 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001179 }
1180 return true;
1181 }
1182
Elliott Hughes80609252011-09-23 17:24:51 -07001183 protected:
1184 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1185 bool ThrowArrayStoreException(Object* object) const;
1186
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001187 private:
1188 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001189 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001190 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1191 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001192 // Marker for the data (used by generated code)
1193 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001194
Carl Shapirof88c9522011-08-06 15:47:38 -07001195 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001196};
1197
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001198template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001199class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001200 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001201 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001202
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001203 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001205 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001206
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001207 // Set element without bound and element type checks, to be used in limited
1208 // circumstances, such as during boot image writing
1209 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001211 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001212 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001213 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001214
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001216
1217 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001218 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001219};
1220
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001221template<class T>
1222ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1223 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1224}
1225
1226template<class T>
1227T* ObjectArray<T>::Get(int32_t i) const {
1228 if (!IsValidIndex(i)) {
1229 return NULL;
1230 }
1231 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1232 return GetFieldObject<T*>(data_offset, false);
1233}
1234
1235template<class T>
1236ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1237 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1238 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1239 return new_array;
1240}
1241
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001242// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001243// provides the static storage. However, this might change to an Array
1244// to improve image sharing, so we use this type to avoid assumptions
1245// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001246class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001247
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001248// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001249class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001250 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001251
1252 // Class Status
1253 //
1254 // kStatusNotReady: If a Class cannot be found in the class table by
1255 // FindClass, it allocates an new one with AllocClass in the
1256 // kStatusNotReady and calls LoadClass. Note if it does find a
1257 // class, it may not be kStatusResolved and it will try to push it
1258 // forward toward kStatusResolved.
1259 //
1260 // kStatusIdx: LoadClass populates with Class with information from
1261 // the DexFile, moving the status to kStatusIdx, indicating that the
1262 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001263 // populated based on super_class_type_idx_ and
1264 // interfaces_type_idx_. The new Class can then be inserted into the
1265 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001266 //
1267 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1268 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1269 // using ResolveClass to initialize the super_class_ and interfaces_.
1270 //
1271 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001272 // shows linking is complete and fields of the Class populated by making
1273 // it kStatusResolved. Java allows circularities of the form where a super
1274 // class has a field that is of the type of the sub class. We need to be able
1275 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001276
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001277 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001278 kStatusError = -1,
1279 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001280 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001281 kStatusLoaded = 2, // DEX idx values resolved
1282 kStatusResolved = 3, // part of linking
1283 kStatusVerifying = 4, // in the process of being verified
1284 kStatusVerified = 5, // logically part of linking; done pre-init
1285 kStatusInitializing = 6, // class init in progress
1286 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001287 };
1288
1289 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001290 kPrimNot = 0,
1291 kPrimBoolean,
1292 kPrimByte,
1293 kPrimChar,
1294 kPrimShort,
1295 kPrimInt,
1296 kPrimLong,
1297 kPrimFloat,
1298 kPrimDouble,
1299 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001300 };
1301
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001302 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001303 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1304 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001305 }
1306
1307 void SetStatus(Status new_status);
1308
1309 // Returns true if the class has failed to link.
1310 bool IsErroneous() const {
1311 return GetStatus() == kStatusError;
1312 }
1313
1314 // Returns true if the class has been loaded.
1315 bool IsIdxLoaded() const {
1316 return GetStatus() >= kStatusIdx;
1317 }
1318
1319 // Returns true if the class has been loaded.
1320 bool IsLoaded() const {
1321 return GetStatus() >= kStatusLoaded;
1322 }
1323
1324 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001325 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 return GetStatus() >= kStatusResolved;
1327 }
1328
1329 // Returns true if the class has been verified.
1330 bool IsVerified() const {
1331 return GetStatus() >= kStatusVerified;
1332 }
1333
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001334 // Returns true if the class is initializing.
1335 bool IsInitializing() const {
1336 return GetStatus() >= kStatusInitializing;
1337 }
1338
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001339 // Returns true if the class is initialized.
1340 bool IsInitialized() const {
1341 return GetStatus() == kStatusInitialized;
1342 }
1343
1344 uint32_t GetAccessFlags() const;
1345
1346 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001347 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001348 }
1349
1350 // Returns true if the class is an interface.
1351 bool IsInterface() const {
1352 return (GetAccessFlags() & kAccInterface) != 0;
1353 }
1354
1355 // Returns true if the class is declared public.
1356 bool IsPublic() const {
1357 return (GetAccessFlags() & kAccPublic) != 0;
1358 }
1359
1360 // Returns true if the class is declared final.
1361 bool IsFinal() const {
1362 return (GetAccessFlags() & kAccFinal) != 0;
1363 }
1364
Elliott Hughes20cde902011-10-04 17:37:27 -07001365 bool IsFinalizable() const {
1366 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1367 }
1368
1369 void SetFinalizable() {
1370 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1371 SetAccessFlags(flags | kAccClassIsFinalizable);
1372 }
1373
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 // Returns true if the class is abstract.
1375 bool IsAbstract() const {
1376 return (GetAccessFlags() & kAccAbstract) != 0;
1377 }
1378
1379 // Returns true if the class is an annotation.
1380 bool IsAnnotation() const {
1381 return (GetAccessFlags() & kAccAnnotation) != 0;
1382 }
1383
1384 // Returns true if the class is synthetic.
1385 bool IsSynthetic() const {
1386 return (GetAccessFlags() & kAccSynthetic) != 0;
1387 }
1388
1389 bool IsReferenceClass() const {
1390 return (GetAccessFlags() & kAccClassIsReference) != 0;
1391 }
1392
1393 bool IsWeakReferenceClass() const {
1394 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1395 }
1396
1397 bool IsSoftReferenceClass() const {
1398 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1399 }
1400
1401 bool IsFinalizerReferenceClass() const {
1402 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1403 }
1404
1405 bool IsPhantomReferenceClass() const {
1406 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1407 }
1408
1409 PrimitiveType GetPrimitiveType() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001410 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411 return static_cast<PrimitiveType>(
1412 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1413 }
1414
1415 void SetPrimitiveType(PrimitiveType new_type) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001416 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001417 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001418 }
1419
1420 // Returns true if the class is a primitive type.
1421 bool IsPrimitive() const {
1422 return GetPrimitiveType() != kPrimNot;
1423 }
1424
1425 bool IsPrimitiveBoolean() const {
1426 return GetPrimitiveType() == kPrimBoolean;
1427 }
1428
1429 bool IsPrimitiveByte() const {
1430 return GetPrimitiveType() == kPrimByte;
1431 }
1432
1433 bool IsPrimitiveChar() const {
1434 return GetPrimitiveType() == kPrimChar;
1435 }
1436
1437 bool IsPrimitiveShort() const {
1438 return GetPrimitiveType() == kPrimShort;
1439 }
1440
1441 bool IsPrimitiveInt() const {
1442 return GetPrimitiveType() == kPrimInt;
1443 }
1444
1445 bool IsPrimitiveLong() const {
1446 return GetPrimitiveType() == kPrimLong;
1447 }
1448
1449 bool IsPrimitiveFloat() const {
1450 return GetPrimitiveType() == kPrimFloat;
1451 }
1452
1453 bool IsPrimitiveDouble() const {
1454 return GetPrimitiveType() == kPrimDouble;
1455 }
1456
1457 bool IsPrimitiveVoid() const {
1458 return GetPrimitiveType() == kPrimVoid;
1459 }
1460
1461 size_t PrimitiveSize() const;
1462
1463 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001464 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001465 }
1466
1467 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 return GetFieldObject<Class*>(
1469 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1470 }
1471
1472 void SetComponentType(Class* new_component_type) {
1473 DCHECK(GetComponentType() == NULL);
1474 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001475 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 }
1477
1478 size_t GetComponentSize() const {
1479 return GetTypeSize(GetComponentType()->GetDescriptor());
1480 }
1481
1482 bool IsObjectClass() const {
1483 return !IsPrimitive() && GetSuperClass() == NULL;
1484 }
1485
Brian Carlstrom1f870082011-08-23 16:02:11 -07001486 // Creates a raw object instance but does not invoke the default constructor.
1487 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001488
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001489 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001490
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001491 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001492 const String* result = GetFieldObject<const String*>(
1493 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1494 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1495 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1496 return result;
1497 }
1498
1499 void SetDescriptor(String* new_descriptor);
1500
1501 bool IsVariableSize() const {
1502 // Classes and arrays vary in size, and so the object_size_ field cannot
1503 // be used to get their instance size
1504 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001505 }
1506
Brian Carlstrom4873d462011-08-21 15:23:39 -07001507 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001508 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001509 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001510 }
1511
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001512 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001513 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001514 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515 }
1516
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001517 void SetClassSize(size_t new_class_size) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001518 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughes54e7df12011-09-16 11:47:04 -07001519 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001520 }
1521
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001522 size_t GetObjectSize() const {
1523 CHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001524 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001525 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001526 CHECK_GE(result, sizeof(Object));
1527 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001528 }
1529
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001530 void SetObjectSize(size_t new_object_size) {
1531 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001532 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001533 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001534 }
1535
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001536 // Returns true if this class is in the same packages as that class.
1537 bool IsInSamePackage(const Class* that) const;
1538
Brian Carlstrome24fa612011-09-29 00:53:55 -07001539 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001540
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001541 // Returns true if this class can access that class.
1542 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001543 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001544 }
1545
jeffhao4a801a42011-09-23 13:53:40 -07001546 // Validate method/field access.
1547 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1548 // quick accept for public access
1549 if (member_flags & kAccPublic) {
1550 return true;
1551 }
1552
1553 // quick accept for access from same class
1554 if (this == access_to) {
1555 return true;
1556 }
1557
1558 // quick reject for private access from another class
1559 if (member_flags & kAccPrivate) {
1560 return false;
1561 }
1562
1563 // Semi-quick test for protected access from a sub-class, which may or
1564 // may not be in the same package.
1565 if (member_flags & kAccProtected) {
1566 if (this->IsSubClass(access_to)) {
1567 return true;
1568 }
1569 }
1570
1571 // Allow protected and private access from other classes in the same package.
1572 return this->IsInSamePackage(access_to);
1573 }
1574
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001575 bool IsSubClass(const Class* klass) const;
1576
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001577 bool IsAssignableFrom(const Class* src) const {
1578 DCHECK(src != NULL);
1579 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001580 // Can always assign to things of the same type
1581 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001582 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001584 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001585 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001586 return src->Implements(this);
1587 } else if (src->IsArrayClass()) {
1588 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001589 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001590 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001591 }
1592 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001593
buzbee991e3ac2011-09-29 15:44:22 -07001594 // Assignable test for code, won't throw. Null and equality tests already performed
1595 static uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class)
1596 {
1597 DCHECK(klass != NULL);
1598 DCHECK(ref_class != NULL);
1599 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1600 }
1601
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001602 Class* GetSuperClass() const {
1603 // Can only get super class for loaded classes (hack for when runtime is
1604 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001605 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001606 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001607 }
1608
buzbee4a3164f2011-09-03 11:25:10 -07001609 static MemberOffset SuperClassOffset() {
1610 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1611 }
1612
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001613 void SetSuperClass(Class *new_super_class) {
1614 // super class is assigned once, except during class linker initialization
1615 Class* old_super_class = GetFieldObject<Class*>(
1616 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1617 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1618 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001619 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001620 }
1621
1622 bool HasSuperClass() const {
1623 return GetSuperClass() != NULL;
1624 }
1625
1626 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001627 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001628 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1629 false);
1630 }
1631
1632 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001633 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001634 }
1635
1636 const ClassLoader* GetClassLoader() const;
1637
1638 void SetClassLoader(const ClassLoader* new_cl);
1639
1640 static MemberOffset DexCacheOffset() {
1641 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1642 }
1643
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001644 enum {
1645 kDumpClassFullDetail = 1,
1646 kDumpClassClassLoader = (1 << 1),
1647 kDumpClassInitialized = (1 << 2),
1648 };
1649
Elliott Hughes4681c802011-09-25 18:04:37 -07001650 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001651
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 DexCache* GetDexCache() const;
1653
1654 void SetDexCache(DexCache* new_dex_cache);
1655
1656 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001657 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001658 return GetFieldObject<ObjectArray<Method>*>(
1659 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1660 }
1661
1662 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1663 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1664 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1665 DCHECK_NE(0, new_direct_methods->GetLength());
1666 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1667 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001668 }
1669
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001670 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001671 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001672 }
1673
1674 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001675 ObjectArray<Method>* direct_methods =
1676 GetFieldObject<ObjectArray<Method>*>(
1677 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1678 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001679 }
1680
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681 // Returns the number of static, private, and constructor methods.
1682 size_t NumDirectMethods() const {
1683 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1684 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001685
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001686 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001688 return GetFieldObject<ObjectArray<Method>*>(
1689 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1690 }
1691
1692 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1693 // TODO: we reassign virtual methods to grow the table for miranda
1694 // methods.. they should really just be assigned once
1695 DCHECK_NE(0, new_virtual_methods->GetLength());
1696 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1697 new_virtual_methods, false);
1698 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001699
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001700 // Returns the number of non-inherited virtual methods.
1701 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001702 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001703 }
1704
1705 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001706 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001707 return GetVirtualMethods()->Get(i);
1708 }
1709
1710 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001711 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001712 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001713 }
1714
1715 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001716 ObjectArray<Method>* virtual_methods =
1717 GetFieldObject<ObjectArray<Method>*>(
1718 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1719 virtual_methods->Set(i, f);
1720 }
1721
1722 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001723 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001724 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725 }
1726
1727 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001728 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001729 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001730 }
1731
1732 void SetVTable(ObjectArray<Method>* new_vtable) {
1733 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1734 }
1735
1736 static MemberOffset VTableOffset() {
1737 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001738 }
1739
Brian Carlstrom30b94452011-08-25 21:35:26 -07001740 // Given a method implemented by this class but potentially from a
1741 // super class, return the specific implementation
1742 // method for this class.
1743 Method* FindVirtualMethodForVirtual(Method* method) {
1744 DCHECK(!method->GetDeclaringClass()->IsInterface());
1745 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001746 // Use the index to a potentially overridden one for this instance's class.
1747 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001748 }
1749
1750 // Given a method implemented by this class, but potentially from a
1751 // super class or interface, return the specific implementation
1752 // method for this class.
1753 Method* FindVirtualMethodForInterface(Method* method);
1754
jeffhaobdb76512011-09-07 11:43:16 -07001755 Method* FindInterfaceMethod(const StringPiece& name,
1756 const StringPiece& descriptor);
1757
Brian Carlstrom30b94452011-08-25 21:35:26 -07001758 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001759 if (method->IsDirect()) {
1760 return method;
1761 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001762 if (method->GetDeclaringClass()->IsInterface()) {
1763 return FindVirtualMethodForInterface(method);
1764 }
1765 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001766 }
1767
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001768 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001769 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001770
1771 Method* FindVirtualMethod(const StringPiece& name,
1772 const StringPiece& descriptor);
1773
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001774 Method* FindDeclaredDirectMethod(const StringPiece& name,
1775 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001776
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001777 Method* FindDirectMethod(const StringPiece& name,
1778 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001779
Carl Shapiro69759ea2011-07-21 18:13:35 -07001780 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001781 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1783 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1784 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001785 }
1786
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001787 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001788 CHECK(IsIdxLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001789 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 }
1791
1792 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1793
1794 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001795 CHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001796 return GetFieldObject<ObjectArray<Class>*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001797 }
1798
1799 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001800 DCHECK(NULL == GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001801 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001802 }
1803
1804 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001805 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001806 ObjectArray<Class>* interfaces =
1807 GetFieldObject<ObjectArray<Class>*>(
1808 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1809 interfaces->Set(i, f);
1810 }
1811
1812 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001813 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001814 return GetInterfaces()->Get(i);
1815 }
1816
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001817 int32_t GetIfTableCount() const {
1818 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1819 if (iftable == NULL) {
1820 return 0;
1821 }
1822 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001823 }
1824
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001825 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001826 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001827 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001828 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1829 }
1830
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001831 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001832 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001833 }
1834
1835 // Get instance fields
1836 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001837 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001838 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001839 }
1840
1841 void SetIFields(ObjectArray<Field>* new_ifields) {
1842 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1843 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001844 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001845 }
1846
1847 size_t NumInstanceFields() const {
1848 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1849 }
1850
1851 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1852 DCHECK_NE(NumInstanceFields(), 0U);
1853 return GetIFields()->Get(i);
1854 }
1855
1856 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1857 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1858 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1859 ifields->Set(i, f);
1860 }
1861
1862 // Returns the number of instance fields containing reference types.
1863 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001864 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001865 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001866 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001867 }
1868
1869 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001870 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001871 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001872 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 }
1874
1875 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001876 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001877 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001878 }
1879
1880 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001881 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001882 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001883 }
1884
1885 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1886
1887 // Beginning of static field data
1888 static MemberOffset FieldsOffset() {
1889 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1890 }
1891
1892 // Returns the number of static fields containing reference types.
1893 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001894 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001895 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001896 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001897 }
1898
1899 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001900 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001901 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001902 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001903 }
1904
1905 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001906 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001907 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001908 }
1909
1910 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001911 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001912 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001913 }
1914
1915 void SetSFields(ObjectArray<Field>* new_sfields) {
1916 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1917 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001918 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001919 }
1920
1921 size_t NumStaticFields() const {
1922 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1923 }
1924
1925 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1926 return GetSFields()->Get(i);
1927 }
1928
1929 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1930 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1931 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1932 sfields->Set(i, f);
1933 }
1934
1935 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001936 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001937 }
1938
1939 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1940
1941 // Finds the given instance field in this class or a superclass.
1942 Field* FindInstanceField(const StringPiece& name, Class* type);
1943
1944 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1945
1946 // Finds the given static field in this class or a superclass.
1947 Field* FindStaticField(const StringPiece& name, Class* type);
1948
1949 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1950
Elliott Hughesdcc24742011-09-07 14:02:44 -07001951 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001952 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001953 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1954 }
1955
Elliott Hughesdcc24742011-09-07 14:02:44 -07001956 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001957 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001958 }
1959
1960 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001961 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001962 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001963 }
1964
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001965 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001966 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001967 }
1968
Brian Carlstromc74255f2011-09-11 22:47:39 -07001969 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001970
Brian Carlstromc74255f2011-09-11 22:47:39 -07001971 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001972
jeffhaobdb76512011-09-07 11:43:16 -07001973 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001974 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001975 bool IsArrayAssignableFromArray(const Class* klass) const;
1976 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001977
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001978 // descriptor for the class such as "java.lang.Class" or "[C"
1979 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001980
Brian Carlstrom693267a2011-09-06 09:25:34 -07001981 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001982 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001983
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001984 // For array classes, the component class object for instanceof/checkcast
1985 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001986 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001987
Brian Carlstrom693267a2011-09-06 09:25:34 -07001988 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1989 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001990
Brian Carlstrom693267a2011-09-06 09:25:34 -07001991 // DexCache of resolved constant pool entries
1992 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1993 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001994
1995 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001996 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001997
Brian Carlstrom693267a2011-09-06 09:25:34 -07001998 // instance fields
1999 //
2000 // These describe the layout of the contents of an Object.
2001 // Note that only the fields directly declared by this class are
2002 // listed in ifields; fields declared by a superclass are listed in
2003 // the superclass's Class.ifields.
2004 //
2005 // All instance fields that refer to objects are guaranteed to be at
2006 // the beginning of the field list. num_reference_instance_fields_
2007 // specifies the number of reference fields.
2008 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002009
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002010 // Interface table (iftable_), one entry per interface supported by
2011 // this class. That means one entry for each interface we support
2012 // directly, indirectly via superclass, or indirectly via
2013 // superinterface. This will be null if neither we nor our
2014 // superclass implement any interfaces.
2015 //
2016 // Why we need this: given "class Foo implements Face", declare
2017 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
2018 // is part of the Face interface. We can't easily use a single
2019 // vtable.
2020 //
2021 // For every interface a concrete class implements, we create an array
2022 // of the concrete vtable_ methods for the methods in the interface.
2023 ObjectArray<InterfaceEntry>* iftable_;
2024
Brian Carlstromdbc05252011-09-09 01:59:59 -07002025 // array of interfaces this class implements directly
2026 // see also interfaces_type_idx_
2027 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002028
2029 // array of type_idx's for interfaces this class implements directly
2030 // see also interfaces_
2031 IntArray* interfaces_type_idx_;
2032
Brian Carlstromdbc05252011-09-09 01:59:59 -07002033 // Static fields
2034 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002035
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002036 // source file name, if known. Otherwise, NULL.
2037 String* source_file_;
2038
Brian Carlstromdbc05252011-09-09 01:59:59 -07002039 // The superclass, or NULL if this is java.lang.Object or a
2040 // primitive type.
2041 // see also super_class_type_idx_;
2042 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002043
Brian Carlstromdbc05252011-09-09 01:59:59 -07002044 // If class verify fails, we must return same error on subsequent tries.
2045 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2046 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002047
Brian Carlstromdbc05252011-09-09 01:59:59 -07002048 // virtual methods defined in this class; invoked through vtable
2049 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002050
Brian Carlstromdbc05252011-09-09 01:59:59 -07002051 // Virtual method table (vtable), for use by "invoke-virtual". The
2052 // vtable from the superclass is copied in, and virtual methods from
2053 // our class either replace those from the super or are appended.
2054 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002055
Brian Carlstromdbc05252011-09-09 01:59:59 -07002056 // access flags; low 16 bits are defined by VM spec
2057 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002058
Jesse Wilson6bf19152011-09-29 13:12:33 -04002059 // Total size of the Class instance; used when allocating storage on gc heap.
2060 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002061 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002062
Elliott Hughes5f791332011-09-15 17:45:30 -07002063 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002064 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002065
Brian Carlstromdbc05252011-09-09 01:59:59 -07002066 // number of instance fields that are object refs
2067 size_t num_reference_instance_fields_;
2068
2069 // number of static fields that are object refs
2070 size_t num_reference_static_fields_;
2071
2072 // Total object size; used when allocating storage on gc heap.
2073 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002074 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002075 size_t object_size_;
2076
2077 // primitive type index, or kPrimNot (0); set for generated prim classes
2078 PrimitiveType primitive_type_;
2079
2080 // Bitmap of offsets of ifields.
2081 uint32_t reference_instance_offsets_;
2082
2083 // Bitmap of offsets of sfields.
2084 uint32_t reference_static_offsets_;
2085
Brian Carlstrom693267a2011-09-06 09:25:34 -07002086 // state of class initialization
2087 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002088
Brian Carlstrom693267a2011-09-06 09:25:34 -07002089 // Set in LoadClass, used to LinkClass
2090 // see also super_class_
2091 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002092
Brian Carlstrom693267a2011-09-06 09:25:34 -07002093 // TODO: ?
2094 // initiating class loader list
2095 // NOTE: for classes with low serialNumber, these are unused, and the
2096 // values are kept in a table in gDvm.
2097 // InitiatingLoaderList initiating_loader_list_;
2098
Brian Carlstrom4873d462011-08-21 15:23:39 -07002099 // Location of first static field.
2100 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002101
Brian Carlstrom693267a2011-09-06 09:25:34 -07002102 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002103 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002104};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002105
Elliott Hughes1f359b02011-07-17 14:27:17 -07002106std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002107
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002108inline void Object::SetClass(Class* new_klass) {
2109 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002110 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002111}
2112
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002113inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002114 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002115 DCHECK(GetClass() != NULL);
2116 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002117}
2118
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002119inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002120 Class* java_lang_Class = GetClass()->GetClass();
2121 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002122}
2123
Brian Carlstrom4873d462011-08-21 15:23:39 -07002124inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002125 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002126 return this == java_lang_Class;
2127}
2128
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002129inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002130 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002131}
2132
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002133template<class T>
2134inline ObjectArray<T>* Object::AsObjectArray() {
2135 DCHECK(IsObjectArray());
2136 return down_cast<ObjectArray<T>*>(this);
2137}
2138
2139template<class T>
2140inline const ObjectArray<T>* Object::AsObjectArray() const {
2141 DCHECK(IsObjectArray());
2142 return down_cast<const ObjectArray<T>*>(this);
2143}
2144
Brian Carlstromb63ec392011-08-27 17:38:27 -07002145inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002146 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002147}
2148
Brian Carlstroma663ea52011-08-19 23:33:41 -07002149inline bool Object::IsField() const {
2150 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002151 Class* java_lang_reflect_Field =
2152 java_lang_Class->GetInstanceField(0)->GetClass();
2153 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002154}
2155
2156inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002157 Class* c = GetClass();
2158 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002159}
2160
2161inline bool Object::IsReferenceInstance() const {
2162 return GetClass()->IsReferenceClass();
2163}
2164
2165inline bool Object::IsWeakReferenceInstance() const {
2166 return GetClass()->IsWeakReferenceClass();
2167}
2168
2169inline bool Object::IsSoftReferenceInstance() const {
2170 return GetClass()->IsSoftReferenceClass();
2171}
2172
2173inline bool Object::IsFinalizerReferenceInstance() const {
2174 return GetClass()->IsFinalizerReferenceClass();
2175}
2176
2177inline bool Object::IsPhantomReferenceInstance() const {
2178 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002179}
2180
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002181inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002182 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002183 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002184 result = AsArray()->SizeOf();
2185 } else if (IsClass()) {
2186 result = AsClass()->SizeOf();
2187 } else {
2188 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002189 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002190 DCHECK(!IsField() || result == sizeof(Field));
2191 DCHECK(!IsMethod() || result == sizeof(Method));
2192 return result;
2193}
2194
2195inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002196 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002197 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002198 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2199 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002200 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002201 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2202 num_bytes.Uint32Value(), false);
2203}
2204
2205inline Class* Field::GetDeclaringClass() const {
2206 Class* result = GetFieldObject<Class*>(
2207 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2208 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002209 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002210 return result;
2211}
2212
2213inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2214 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2215 new_declaring_class, false);
2216}
2217
2218inline Class* Method::GetDeclaringClass() const {
2219 Class* result =
2220 GetFieldObject<Class*>(
2221 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2222 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002223 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002224 return result;
2225}
2226
2227inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2228 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2229 new_declaring_class, false);
2230}
2231
2232inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002233 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002234 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2235 false);
2236}
2237
2238inline bool Method::IsReturnAReference() const {
2239 return !GetReturnType()->IsPrimitive();
2240}
2241
2242inline bool Method::IsReturnAFloat() const {
2243 return GetReturnType()->IsPrimitiveFloat();
2244}
2245
2246inline bool Method::IsReturnADouble() const {
2247 return GetReturnType()->IsPrimitiveDouble();
2248}
2249
2250inline bool Method::IsReturnALong() const {
2251 return GetReturnType()->IsPrimitiveLong();
2252}
2253
2254inline bool Method::IsReturnVoid() const {
2255 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002256}
2257
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002258inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002259 // This is safe from overflow because the array was already allocated, so we know it's sane.
2260 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002261}
2262
2263template<class T>
2264void ObjectArray<T>::Set(int32_t i, T* object) {
2265 if (IsValidIndex(i)) {
2266 if (object != NULL) {
2267 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002268 if (!object->InstanceOf(element_class)) {
2269 ThrowArrayStoreException(object);
2270 return;
2271 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002272 }
2273 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2274 SetFieldObject(data_offset, object, false);
2275 }
2276}
2277
2278template<class T>
2279void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2280 DCHECK(IsValidIndex(i));
2281 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2282 SetFieldObject(data_offset, object, false);
2283}
2284
2285template<class T>
2286void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2287 ObjectArray<T>* dst, int dst_pos,
2288 size_t length) {
2289 if (src->IsValidIndex(src_pos) &&
2290 src->IsValidIndex(src_pos+length-1) &&
2291 dst->IsValidIndex(dst_pos) &&
2292 dst->IsValidIndex(dst_pos+length-1)) {
2293 MemberOffset src_offset(DataOffset().Int32Value() +
2294 src_pos * sizeof(Object*));
2295 MemberOffset dst_offset(DataOffset().Int32Value() +
2296 dst_pos * sizeof(Object*));
2297 Class* array_class = dst->GetClass();
2298 if (array_class == src->GetClass()) {
2299 // No need for array store checks if arrays are of the same type
2300 for (size_t i=0; i < length; i++) {
2301 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2302 dst->SetFieldObject(dst_offset, object, false);
2303 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2304 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2305 }
2306 } else {
2307 Class* element_class = array_class->GetComponentType();
2308 CHECK(!element_class->IsPrimitive());
2309 for (size_t i=0; i < length; i++) {
2310 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002311 if (object != NULL && !object->InstanceOf(element_class)) {
2312 dst->ThrowArrayStoreException(object);
2313 return;
2314 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002315 dst->SetFieldObject(dst_offset, object, false);
2316 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2317 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2318 }
2319 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002320 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002321 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002322}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002323
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002324class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002325 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002326 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002327 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002328 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002329 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2330};
2331
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002332class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002333 private:
2334 CharArray* ASCII_;
2335 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002336 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002337 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002338 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002339 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2340};
2341
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002342class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002343 private:
2344 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2345 uint32_t TYPE_BOOLEAN_;
2346 uint32_t TYPE_BYTE_;
2347 uint32_t TYPE_CHAR_;
2348 uint32_t TYPE_DOUBLE_;
2349 uint32_t TYPE_FLOAT_;
2350 uint32_t TYPE_INTEGER_;
2351 uint32_t TYPE_LONG_;
2352 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002353 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002354 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2355};
2356
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002357class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002358 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002359 Object* ORDER_BY_SIGNATURE_;
2360 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002361 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2362};
2363
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002364template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002365class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002366 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002367 typedef T ElementType;
2368
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002369 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002370
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002371 const T* GetData() const {
2372 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002373 }
2374
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002375 T* GetData() {
2376 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002377 }
2378
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002379 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002380 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002381 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002382 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002383 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002384 }
2385
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002386 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002387 // TODO: ArrayStoreException
2388 if (IsValidIndex(i)) {
2389 GetData()[i] = value;
2390 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002391 }
2392
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002393 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002394 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002395 CHECK(array_class != NULL);
2396 array_class_ = array_class;
2397 }
2398
Brian Carlstroma663ea52011-08-19 23:33:41 -07002399 static void ResetArrayClass() {
2400 CHECK(array_class_ != NULL);
2401 array_class_ = NULL;
2402 }
2403
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002404 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002405 // Location of first element.
2406 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002407
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002408 static Class* array_class_;
2409
Carl Shapirof88c9522011-08-06 15:47:38 -07002410 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002411};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002412
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002413inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2414 DCHECK(NULL == GetFieldObject<IntArray*>(
2415 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2416 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2417 new_interfaces_idx, false);
2418}
2419
2420// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002421class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002422 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002423 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002424 const CharArray* result = GetFieldObject<const CharArray*>(
2425 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2426 DCHECK(result != NULL);
2427 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002428 }
2429
Elliott Hughes814e4032011-08-23 12:07:56 -07002430 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002431 int32_t result = GetField32(
2432 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2433 DCHECK_LE(0, result);
2434 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002435 }
2436
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002437 int32_t GetLength() const;
2438
Brian Carlstrom395520e2011-09-25 19:35:00 -07002439 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002440
2441 void ComputeHashCode() {
2442 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002443 }
2444
Elliott Hughes814e4032011-08-23 12:07:56 -07002445 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002446 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002447 }
2448
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002449 uint16_t CharAt(int32_t index) const;
2450
Brian Carlstromc74255f2011-09-11 22:47:39 -07002451 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002452
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002453 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002454 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002455 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002457 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002458
Jesse Wilson8989d992011-08-02 13:39:42 -07002459 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002460 const char* utf8_data_in);
2461
2462 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2463
2464 static String* Alloc(Class* java_lang_String, CharArray* array);
2465
2466 bool Equals(const char* modified_utf8) const;
2467
2468 // TODO: do we need this overload? give it a more intention-revealing name.
2469 bool Equals(const StringPiece& modified_utf8) const;
2470
2471 bool Equals(const String* that) const;
2472
2473 // TODO: do we need this overload? give it a more intention-revealing name.
2474 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2475 int32_t that_length) const;
2476
2477 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2478 std::string ToModifiedUtf8() const;
2479
2480 static Class* GetJavaLangString() {
2481 DCHECK(java_lang_String_ != NULL);
2482 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002483 }
2484
Brian Carlstroma663ea52011-08-19 23:33:41 -07002485 static void SetClass(Class* java_lang_String);
2486 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002487
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002488 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002489 void SetHashCode(int32_t new_hash_code) {
2490 DCHECK_EQ(0u,
2491 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2492 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2493 new_hash_code, false);
2494 }
2495
2496 void SetCount(int32_t new_count) {
2497 DCHECK_LE(0, new_count);
2498 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2499 }
2500
2501 void SetOffset(int32_t new_offset) {
2502 DCHECK_LE(0, new_offset);
2503 DCHECK_GE(GetLength(), new_offset);
2504 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2505 }
2506
2507 void SetArray(CharArray* new_array) {
2508 DCHECK(new_array != NULL);
2509 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2510 }
2511
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002512 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2513 CharArray* array_;
2514
Brian Carlstromdbc05252011-09-09 01:59:59 -07002515 int32_t count_;
2516
Carl Shapirof88c9522011-08-06 15:47:38 -07002517 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002518
Elliott Hughes289da822011-08-16 10:11:20 -07002519 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002520
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002521 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002522
Brian Carlstrom693267a2011-09-06 09:25:34 -07002523 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002524 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002525};
2526
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002527inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002528 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002529 String* result =
2530 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2531 DCHECK(result != NULL);
2532 return result;
2533}
2534
2535inline void Field::SetName(String* new_name) {
2536 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2537 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002538}
2539
2540inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002541 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002542 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2543}
2544
2545inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002546 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002547 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2548}
2549
2550inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002551 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002552 return MemberOffset(
2553 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2554}
2555
2556inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002557 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002558 return MemberOffset(
2559 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2560}
2561
2562inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002563 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002564 const String* result =
2565 GetFieldObject<const String*>(
2566 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2567 DCHECK(result != NULL);
2568 return result;
2569}
2570
2571inline void Method::SetName(String* new_name) {
2572 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2573 new_name, false);
2574
2575}
2576
jeffhaoe23d93c2011-09-15 14:48:43 -07002577inline ByteArray* Method::GetRegisterMapData() const {
2578 return GetFieldObject<ByteArray*>(
2579 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2580}
2581
jeffhaoa0a764a2011-09-16 10:43:38 -07002582inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002583 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002584 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002585}
2586
2587inline ByteArray* Method::GetRegisterMapHeader() const {
2588 return GetFieldObject<ByteArray*>(
2589 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2590}
2591
jeffhaoa0a764a2011-09-16 10:43:38 -07002592inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002593 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002594 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002595}
2596
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002597inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002598 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002599 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002600 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2601}
2602
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002603inline void Method::SetShorty(String* new_shorty) {
2604 DCHECK(NULL == GetFieldObject<String*>(
2605 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2606 DCHECK_LE(1, new_shorty->GetLength());
2607 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2608}
2609
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002610inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002611 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002612 const String* result =
Elliott Hughesf5a7a472011-10-07 14:31:02 -07002613 GetFieldObject<const String*>(OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002614 DCHECK(result != NULL);
2615 return result;
2616}
2617
2618inline void Method::SetSignature(String* new_signature) {
2619 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2620 new_signature, false);
2621}
2622
2623inline uint32_t Class::GetAccessFlags() const {
2624 // Check class is loaded or this is java.lang.String that has a
2625 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002626 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002627 this == String::GetJavaLangString() ||
2628 this == Field::GetJavaLangReflectField() ||
2629 this == Method::GetConstructorClass() ||
2630 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002631 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2632}
2633
2634inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002635 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002636 DCHECK(new_descriptor != NULL);
2637 DCHECK_NE(0, new_descriptor->GetLength());
2638 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2639 new_descriptor, false);
2640}
2641
Brian Carlstromc74255f2011-09-11 22:47:39 -07002642inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002643 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002644 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2645}
2646
2647inline void Class::SetSourceFile(String* new_source_file) {
2648 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2649}
2650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002651inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002652 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002653 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2654}
2655
2656inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002657 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002658 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002659}
2660
2661inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002662 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002663 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002664}
2665
2666inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002667 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002668 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002669}
2670
2671inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002672 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002673 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002674}
2675
2676inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002677 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002678 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2679}
2680
2681// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002682class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002683 public:
2684 void SetDetailMessage(String* new_detail_message) {
2685 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2686 new_detail_message, false);
2687 }
2688
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002689 private:
2690 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2691 Throwable* cause_;
2692 String* detail_message_;
2693 Object* stack_state_; // Note this is Java volatile:
2694 Object* stack_trace_;
2695 Object* suppressed_exceptions_;
2696
Brian Carlstrom693267a2011-09-06 09:25:34 -07002697 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002698 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2699};
2700
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002701// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002702class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002703 public:
2704 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002705 return GetFieldObject<const String*>(
2706 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002707 }
2708
2709 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002710 return GetFieldObject<const String*>(
2711 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002712 }
2713
2714 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002715 return GetFieldObject<const String*>(
2716 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002717 }
2718
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002719 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002720 return GetField32(
2721 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002722 }
2723
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002724 static StackTraceElement* Alloc(const String* declaring_class,
2725 const String* method_name,
2726 const String* file_name,
2727 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002728
2729 static void SetClass(Class* java_lang_StackTraceElement);
2730
2731 static void ResetClass();
2732
2733 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002734 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002735 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002736 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002737 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002738 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002739
2740 static Class* GetStackTraceElement() {
2741 DCHECK(java_lang_StackTraceElement_ != NULL);
2742 return java_lang_StackTraceElement_;
2743 }
2744
2745 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002746
2747 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002748 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2749};
2750
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002751class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002752 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002753 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002754 Class* interface = Get(kInterface)->AsClass();
2755 DCHECK(interface != NULL);
2756 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002757 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002758
Brian Carlstrom30b94452011-08-25 21:35:26 -07002759 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002760 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002761 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002762 DCHECK(Get(kInterface) == NULL);
2763 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002764 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002765
Brian Carlstrom86927212011-09-15 11:31:11 -07002766 size_t GetMethodArrayCount() const {
2767 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2768 if (method_array == 0) {
2769 return 0;
2770 }
2771 return method_array->GetLength();
2772 }
2773
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002774 ObjectArray<Method>* GetMethodArray() const {
2775 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2776 DCHECK(method_array != NULL);
2777 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002778 }
2779
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002780 void SetMethodArray(ObjectArray<Method>* new_ma) {
2781 DCHECK(new_ma != NULL);
2782 DCHECK(Get(kMethodArray) == NULL);
2783 Set(kMethodArray, new_ma);
2784 }
2785
2786 static size_t LengthAsArray() {
2787 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002788 }
2789
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002790 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002791
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002792 enum ArrayIndex {
2793 // Points to the interface class.
2794 kInterface = 0,
2795 // Method pointers into the vtable, allow fast map from interface
2796 // method index to concrete instance method.
2797 kMethodArray = 1,
2798 kMax = 2,
2799 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002800
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002801 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002802};
2803
2804} // namespace art
2805
2806#endif // ART_SRC_OBJECT_H_