blob: d8eb179ce19e6a85cbf9ef90b1d0297e88c111fc [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
Brian Carlstrom845490b2011-09-19 15:56:53 -0700474 static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer);
475 static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer);
476 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static);
buzbee34cd9e52011-09-08 14:31:52 -0700477
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700478 MemberOffset GetOffsetDuringLinking() const;
479
480 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700481
Brian Carlstrom4873d462011-08-21 15:23:39 -0700482 // field access, null object for static fields
483 bool GetBoolean(const Object* object) const;
484 void SetBoolean(Object* object, bool z) const;
485 int8_t GetByte(const Object* object) const;
486 void SetByte(Object* object, int8_t b) const;
487 uint16_t GetChar(const Object* object) const;
488 void SetChar(Object* object, uint16_t c) const;
489 uint16_t GetShort(const Object* object) const;
490 void SetShort(Object* object, uint16_t s) const;
491 int32_t GetInt(const Object* object) const;
492 void SetInt(Object* object, int32_t i) const;
493 int64_t GetLong(const Object* object) const;
494 void SetLong(Object* object, int64_t j) const;
495 float GetFloat(const Object* object) const;
496 void SetFloat(Object* object, float f) const;
497 double GetDouble(const Object* object) const;
498 void SetDouble(Object* object, double d) const;
499 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700500 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700501
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700502 // Slow path routines for static field access when field was unresolved at
503 // compile time
504 static uint32_t Get32StaticFromCode(uint32_t field_idx,
505 const Method* referrer);
506 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
507 uint32_t new_value);
508 static uint64_t Get64StaticFromCode(uint32_t field_idx,
509 const Method* referrer);
510 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
511 uint64_t new_value);
512 static Object* GetObjStaticFromCode(uint32_t field_idx,
513 const Method* referrer);
514 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
515 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700516
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700517 static Class* GetJavaLangReflectField() {
518 DCHECK(java_lang_reflect_Field_ != NULL);
519 return java_lang_reflect_Field_;
520 }
521
522 static void SetClass(Class* java_lang_reflect_Field);
523 static void ResetClass();
524
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700525 bool IsVolatile() const {
526 return (GetAccessFlags() & kAccVolatile) != 0;
527 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700528
Elliott Hughes80609252011-09-23 17:24:51 -0700529 void InitJavaFields();
530
buzbee1da522d2011-09-04 11:22:20 -0700531 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700532 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700533 uint32_t Get32(const Object* object) const;
534 void Set32(Object* object, uint32_t new_value) const;
535 uint64_t Get64(const Object* object) const;
536 void Set64(Object* object, uint64_t new_value) const;
537 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700538 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700539
Elliott Hughes80609252011-09-23 17:24:51 -0700540 void InitJavaFieldsLocked();
541
Jesse Wilson35baaab2011-08-10 16:18:03 -0400542 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700543
Jesse Wilson35baaab2011-08-10 16:18:03 -0400544 // The class in which this field is declared.
545 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700546
Jesse Wilson35baaab2011-08-10 16:18:03 -0400547 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548
Brian Carlstromdbc05252011-09-09 01:59:59 -0700549 const String* name_;
550
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700551 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700552 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400553
Brian Carlstromdbc05252011-09-09 01:59:59 -0700554 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700555
Jesse Wilson35baaab2011-08-10 16:18:03 -0400556 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700557
558 // Offset of field within an instance or in the Class' static fields
559 uint32_t offset_;
560
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561 // Dex cache index of resolved type
562 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400563
Brian Carlstrom693267a2011-09-06 09:25:34 -0700564 int32_t slot_;
565
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566 static Class* java_lang_reflect_Field_;
567
Brian Carlstrom693267a2011-09-06 09:25:34 -0700568 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400569 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700570};
571
Jesse Wilson6bf19152011-09-29 13:12:33 -0400572// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700573class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700575 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700576 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700577 Object* obj,
578 Thread* thread,
579 byte* args,
580 JValue* result);
581
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700582 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700583
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700584 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700585
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700586 static MemberOffset DeclaringClassOffset() {
587 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700588 }
589
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700590 // Returns the method name, e.g. "<init>" or "eatLunch"
591 const String* GetName() const;
592
593 void SetName(String* new_name);
594
jeffhaoe23d93c2011-09-15 14:48:43 -0700595 ByteArray* GetRegisterMapData() const;
596
jeffhaoa0a764a2011-09-16 10:43:38 -0700597 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700598
599 ByteArray* GetRegisterMapHeader() const;
600
jeffhaoa0a764a2011-09-16 10:43:38 -0700601 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700602
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700603 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700604
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700605 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700606
607 const String* GetSignature() const;
608
609 void SetSignature(String* new_signature);
610
611 bool HasSameNameAndDescriptor(const Method* that) const;
612
613 uint32_t GetAccessFlags() const;
614
615 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700616 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700617 }
618
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700619 // Returns true if the method is declared public.
620 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 }
623
624 // Returns true if the method is declared private.
625 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700626 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700627 }
628
629 // Returns true if the method is declared static.
630 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632 }
633
Brian Carlstrom1f870082011-08-23 16:02:11 -0700634 // Returns true if the method is a constructor.
635 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700636 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700637 }
638
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700639 // Is this method <clinit>
640 bool IsClassInitializer() const;
641
Brian Carlstrom1f870082011-08-23 16:02:11 -0700642 // Returns true if the method is static, private, or a constructor.
643 bool IsDirect() const {
644 return IsStatic() || IsPrivate() || IsConstructor();
645 }
646
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700647 // Returns true if the method is declared synchronized.
648 bool IsSynchronized() const {
649 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700650 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651 }
652
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700653 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700655 }
656
Elliott Hughes80609252011-09-23 17:24:51 -0700657 bool IsMiranda() const {
658 return (GetAccessFlags() & kAccMiranda) != 0;
659 }
660
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700663 }
664
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700665 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700667 }
668
669 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700670 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700671 }
672
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700673 uint16_t GetMethodIndex() const;
674
675 size_t GetVtableIndex() const {
676 return GetMethodIndex();
677 }
678
679 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700680 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700681 }
682
683 static MemberOffset MethodIndexOffset() {
684 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
685 }
686
687 uint32_t GetCodeItemOffset() const {
688 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
689 }
690
691 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700692 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700693 }
694
695 // Number of 32bit registers that would be required to hold all the arguments
696 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700697
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700698 // Number of argument bytes required for densely packing the
699 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700700 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700701
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700702 uint16_t NumRegisters() const;
703
704 void SetNumRegisters(uint16_t new_num_registers) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700705 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700706 }
707
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700708 uint16_t NumIns() const;
709
710 void SetNumIns(uint16_t new_num_ins) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700711 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700712 }
713
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700714 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700715
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700716 void SetNumOuts(uint16_t new_num_outs) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700717 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700718 }
719
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700720 uint32_t GetProtoIdx() const;
721
722 void SetProtoIdx(uint32_t new_proto_idx) {
723 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700724 }
725
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700726 ObjectArray<String>* GetDexCacheStrings() const;
727 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
728
729 static MemberOffset DexCacheStringsOffset() {
730 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
731 }
732
buzbee2a475e72011-09-07 17:19:17 -0700733 static MemberOffset DexCacheResolvedTypesOffset() {
734 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
735 }
736
buzbee34cd9e52011-09-08 14:31:52 -0700737 static MemberOffset DexCacheResolvedFieldsOffset() {
738 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
739 }
740
buzbee1da522d2011-09-04 11:22:20 -0700741 static MemberOffset DexCacheInitializedStaticStorageOffset() {
742 return OFFSET_OF_OBJECT_MEMBER(Method,
743 dex_cache_initialized_static_storage_);
744 }
745
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700746 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
747 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
748
749 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
750 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
751
752 ObjectArray<Field>* GetDexCacheResolvedFields() const;
753 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
754
755 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
756 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
757
758 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
759 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
760
761 void SetReturnTypeIdx(uint32_t new_return_type_idx);
762
763 Class* GetReturnType() const;
764
765 bool IsReturnAReference() const;
766
767 bool IsReturnAFloat() const;
768
769 bool IsReturnADouble() const;
770
Ian Rogersb033c752011-07-20 12:22:35 -0700771 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700773 }
774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700776
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700777 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700778
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700779 // "Args" may refer to any of the 3 levels of "Args."
780 // To avoid confusion, our code will denote which "Args" clearly:
781 // 1. UserArgs: Args that a user see.
782 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
783 // receiver.
784 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
785 // E.g., the first in Args is Method* for both static and non-static
786 // methods. And CConvArgs doesn't deal with the receiver because
787 // receiver is hardwired in an implicit register, so CConvArgs doesn't
788 // need to deal with it.
789 //
790 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700791 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700792
793 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700794 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700795 size_t NumReferenceArgs() const;
796
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700797 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700798 size_t NumLongOrDoubleArgs() const;
799
Ian Rogersb033c752011-07-20 12:22:35 -0700800 // Is the given method parameter a reference?
801 bool IsParamAReference(unsigned int param) const;
802
803 // Is the given method parameter a long or double?
804 bool IsParamALongOrDouble(unsigned int param) const;
805
Ian Rogersdf20fe02011-07-20 20:34:16 -0700806 // Size in bytes of the given parameter
807 size_t ParamSize(unsigned int param) const;
808
809 // Size in bytes of the return value
810 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700811
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700812 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
813
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700814 const void* GetCode() const {
815 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700816 }
817
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700818 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700819 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
820 }
821
822 uint32_t GetOatCodeOffset() const {
823 CHECK(!Runtime::Current()->IsStarted());
824 return reinterpret_cast<uint32_t>(GetCode());
825 }
826
827 void SetOatCodeOffset(uint32_t code_offset) {
828 CHECK(!Runtime::Current()->IsStarted());
829 SetCode(reinterpret_cast<void*>(code_offset));
830 }
831
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700832 static MemberOffset GetCodeOffset() {
833 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700834 }
835
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700836 const uint32_t* GetMappingTable() const {
837 const uint32_t* map = GetMappingTableRaw();
838 if (map == NULL) {
839 return map;
840 }
841 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700842 }
843
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700844 uint32_t GetMappingTableLength() const {
845 const uint32_t* map = GetMappingTableRaw();
846 if (map == NULL) {
847 return 0;
848 }
849 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700850 }
851
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700852 const uint32_t* GetMappingTableRaw() const {
853 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700854 }
855
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700856 void SetMappingTable(const uint32_t* mapping_table) {
857 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
858 mapping_table, false);
859 }
860
861 uint32_t GetOatMappingTableOffset() const {
862 CHECK(!Runtime::Current()->IsStarted());
863 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
864 }
865
866 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
867 CHECK(!Runtime::Current()->IsStarted());
868 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
869 }
870
871 const uint16_t* GetVmapTable() const {
872 const uint16_t* vmap = GetVmapTableRaw();
873 if (vmap == NULL) {
874 return vmap;
875 }
876 return vmap + 1;
877 }
878
879 uint16_t GetVmapTableLength() const {
880 const uint16_t* vmap = GetVmapTableRaw();
881 if (vmap == NULL) {
882 return 0;
883 }
884 return *vmap;
885 }
886
887 const uint16_t* GetVmapTableRaw() const {
888 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
889 }
890
891 void SetVmapTable(const uint16_t* vmap_table) {
892 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
893 }
894
895 uint32_t GetOatVmapTableOffset() const {
896 CHECK(!Runtime::Current()->IsStarted());
897 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
898 }
899
900 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
901 CHECK(!Runtime::Current()->IsStarted());
902 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700903 }
904
Shih-wei Liaod11af152011-08-23 16:02:11 -0700905 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700906 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
907 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700908 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
909 return result;
910 }
911
912 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700913 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700914 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
915 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
916 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700917 }
918
Shih-wei Liaod11af152011-08-23 16:02:11 -0700919 size_t GetReturnPcOffsetInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700920 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
921 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700922 }
923
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700924 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700925 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700926 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
927 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
928 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700929 }
930
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700931 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700932
Brian Carlstrom16192862011-09-12 17:50:06 -0700933 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700934
Brian Carlstrom16192862011-09-12 17:50:06 -0700935 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700936
Ian Rogersb033c752011-07-20 12:22:35 -0700937 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700939 }
940
Brian Carlstrom78128a62011-09-15 17:21:19 -0700941 const void* GetNativeMethod() const {
942 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
943 }
944
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700945 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700946 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700947 InvokeStub* result = GetFieldPtr<InvokeStub*>(
948 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
949 // TODO: DCHECK(result != NULL); should be ahead of time compiled
950 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700951 }
952
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700953 void SetInvokeStub(InvokeStub* invoke_stub) {
954 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
955 invoke_stub, false);
956 }
957
958 uint32_t GetOatInvokeStubOffset() const {
959 CHECK(!Runtime::Current()->IsStarted());
960 return reinterpret_cast<uint32_t>(GetInvokeStub());
961 }
962
963 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
964 CHECK(!Runtime::Current()->IsStarted());
965 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
966 }
967
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700968 static MemberOffset GetInvokeStubOffset() {
969 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700970 }
971
buzbee561227c2011-09-02 15:28:19 -0700972 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
973 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
974 }
975
976 static MemberOffset GetDexCacheResolvedMethodsOffset() {
977 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
978 }
979
980 static MemberOffset GetMethodIndexOffset() {
981 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
982 }
983
Ian Rogers90865722011-09-19 11:11:44 -0700984 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700985 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700986 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700987
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700988 void SetCoreSpillMask(uint32_t core_spill_mask) {
989 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700990 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991 }
992
Ian Rogers90865722011-09-19 11:11:44 -0700993 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700994 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
995 }
996
997 void SetFpSpillMask(uint32_t fp_spill_mask) {
998 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700999 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -07001000 }
1001
Ian Rogers90865722011-09-19 11:11:44 -07001002 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001003 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001004 Runtime* runtime = Runtime::Current();
1005 bool result = false;
1006 for (int i=0; i < Runtime::kLastCalleeSaveType; i++) {
1007 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
1008 result = true;
1009 break;
1010 }
1011 }
Ian Rogers90865722011-09-19 11:11:44 -07001012 // Check that if we do think it is phony it looks like the callee save method
1013 DCHECK(!result || GetCoreSpillMask() != 0);
1014 return result;
1015 }
1016
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001017 // Converts a native PC to a dex PC. TODO: this is a no-op
1018 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -07001019 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001020
1021 // Converts a dex PC to a native PC. TODO: this is a no-op
1022 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -07001023 uintptr_t ToNativePC(const uint32_t dex_pc) const;
1024
1025 // Find the catch block for the given exception type and dex_pc
1026 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001027
Elliott Hughes80609252011-09-23 17:24:51 -07001028 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
1029
1030 static Class* GetConstructorClass() {
1031 return java_lang_reflect_Constructor_;
1032 }
1033
1034 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001035 return java_lang_reflect_Method_;
1036 }
1037
Elliott Hughes80609252011-09-23 17:24:51 -07001038 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001039
Elliott Hughes418d20f2011-09-22 14:00:39 -07001040 void InitJavaFields();
1041
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001042 private:
1043 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001044 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001045
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001046 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1047 // the class we are a part of
1048 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001049 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001050 Object* java_formal_type_parameters_;
1051 Object* java_generic_exception_types_;
1052 Object* java_generic_parameter_types_;
1053 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -07001054
Brian Carlstrom693267a2011-09-06 09:25:34 -07001055 String* name_;
1056
Elliott Hughes418d20f2011-09-22 14:00:39 -07001057 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001058 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001059 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001060
Brian Carlstrom693267a2011-09-06 09:25:34 -07001061 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001062 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1063
1064 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1065 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1066
1067 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1068 ObjectArray<Field>* dex_cache_resolved_fields_;
1069
1070 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1071 ObjectArray<Method>* dex_cache_resolved_methods_;
1072
Brian Carlstromdbc05252011-09-09 01:59:59 -07001073 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1074 ObjectArray<Class>* dex_cache_resolved_types_;
1075
1076 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1077 ObjectArray<String>* dex_cache_strings_;
1078
jeffhaoe23d93c2011-09-15 14:48:43 -07001079 // Byte arrays that hold data for the register maps
1080 const ByteArray* register_map_data_;
1081 const ByteArray* register_map_header_;
1082
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001083 // The short-form method descriptor string.
1084 String* shorty_;
1085
Brian Carlstromdbc05252011-09-09 01:59:59 -07001086 // The method descriptor. This represents the parameters a method
1087 // takes and value it returns. This string is a list of the type
1088 // descriptors for the parameters enclosed in parenthesis followed
1089 // by the return type descriptor. For example, for the method
1090 //
1091 // Object mymethod(int i, double d, Thread t)
1092 //
1093 // the method descriptor would be
1094 //
1095 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1096 String* signature_;
1097
1098 uint32_t java_generic_types_are_initialized_;
1099
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001100 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001101 uint32_t access_flags_;
1102
1103 // Compiled code associated with this method for callers from managed code.
1104 // May be compiled managed code or a bridge for invoking a native method.
1105 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001107 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001108 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001109
Brian Carlstrom693267a2011-09-06 09:25:34 -07001110 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001111 uint32_t core_spill_mask_;
1112
1113 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001114 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115
Brian Carlstrom693267a2011-09-06 09:25:34 -07001116 // Total size in bytes of the frame
1117 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001118
Brian Carlstrom693267a2011-09-06 09:25:34 -07001119 // Native invocation stub entry point for calling from native to managed code.
1120 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001121
Brian Carlstrom693267a2011-09-06 09:25:34 -07001122 // Index of the return type
1123 uint32_t java_return_type_idx_;
1124
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001125 const uint32_t* mapping_table_;
1126
Brian Carlstrom693267a2011-09-06 09:25:34 -07001127 // For concrete virtual methods, this is the offset of the method
1128 // in Class::vtable_.
1129 //
1130 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001131 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001132 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001133
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001134 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001135 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001136
Brian Carlstrom693267a2011-09-06 09:25:34 -07001137 // Method bounds; not needed for an abstract method.
1138 //
1139 // For a native method, we compute the size of the argument list, and
1140 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001141 uint32_t num_ins_;
1142 uint32_t num_outs_;
1143 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001144
Brian Carlstrom693267a2011-09-06 09:25:34 -07001145 // Method prototype descriptor string (return and argument types).
1146 uint32_t proto_idx_;
1147
1148 // Offset of return PC within frame for compiled code (in bytes)
1149 size_t return_pc_offset_in_bytes_;
1150
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001151 const uint16_t* vmap_table_;
1152
Brian Carlstrom693267a2011-09-06 09:25:34 -07001153 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001154
Elliott Hughes80609252011-09-23 17:24:51 -07001155 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 static Class* java_lang_reflect_Method_;
1157
Brian Carlstrom693267a2011-09-06 09:25:34 -07001158 friend class ImageWriter; // for relocating code_ and invoke_stub_
1159 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001160 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001161};
1162
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001163class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001164 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001165 // A convenience for code that doesn't know the component size,
1166 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001167 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001168
Brian Carlstromb63ec392011-08-27 17:38:27 -07001169 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001170
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001171 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001172
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001173 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001174 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001175 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001176
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001177 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001178 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001180 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001181
buzbeec143c552011-08-20 17:38:58 -07001182 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001183 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001184 }
1185
1186 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001187 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001188 }
1189
Elliott Hughesbf86d042011-08-31 17:53:14 -07001190 void* GetRawData() {
1191 return reinterpret_cast<void*>(first_element_);
1192 }
1193
Elliott Hughes289da822011-08-16 10:11:20 -07001194 protected:
1195 bool IsValidIndex(int32_t index) const {
1196 if (index < 0 || index >= length_) {
Elliott Hughes80609252011-09-23 17:24:51 -07001197 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001198 }
1199 return true;
1200 }
1201
Elliott Hughes80609252011-09-23 17:24:51 -07001202 protected:
1203 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1204 bool ThrowArrayStoreException(Object* object) const;
1205
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001206 private:
1207 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001208 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001209 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1210 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001211 // Marker for the data (used by generated code)
1212 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001213
Carl Shapirof88c9522011-08-06 15:47:38 -07001214 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001215};
1216
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001217template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001218class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001219 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001220 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001221
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001222 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001223
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001224 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001225
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226 // Set element without bound and element type checks, to be used in limited
1227 // circumstances, such as during boot image writing
1228 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001229
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001230 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001231 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001232 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001233
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001234 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001235
1236 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001237 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001238};
1239
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001240template<class T>
1241ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1242 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1243}
1244
1245template<class T>
1246T* ObjectArray<T>::Get(int32_t i) const {
1247 if (!IsValidIndex(i)) {
1248 return NULL;
1249 }
1250 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1251 return GetFieldObject<T*>(data_offset, false);
1252}
1253
1254template<class T>
1255ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1256 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1257 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1258 return new_array;
1259}
1260
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001261// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001262// provides the static storage. However, this might change to an Array
1263// to improve image sharing, so we use this type to avoid assumptions
1264// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001265class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001266
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001267// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001268class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001269 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001270
1271 // Class Status
1272 //
1273 // kStatusNotReady: If a Class cannot be found in the class table by
1274 // FindClass, it allocates an new one with AllocClass in the
1275 // kStatusNotReady and calls LoadClass. Note if it does find a
1276 // class, it may not be kStatusResolved and it will try to push it
1277 // forward toward kStatusResolved.
1278 //
1279 // kStatusIdx: LoadClass populates with Class with information from
1280 // the DexFile, moving the status to kStatusIdx, indicating that the
1281 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001282 // populated based on super_class_type_idx_ and
1283 // interfaces_type_idx_. The new Class can then be inserted into the
1284 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001285 //
1286 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1287 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1288 // using ResolveClass to initialize the super_class_ and interfaces_.
1289 //
1290 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001291 // shows linking is complete and fields of the Class populated by making
1292 // it kStatusResolved. Java allows circularities of the form where a super
1293 // class has a field that is of the type of the sub class. We need to be able
1294 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001295
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001296 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001297 kStatusError = -1,
1298 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001299 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001300 kStatusLoaded = 2, // DEX idx values resolved
1301 kStatusResolved = 3, // part of linking
1302 kStatusVerifying = 4, // in the process of being verified
1303 kStatusVerified = 5, // logically part of linking; done pre-init
1304 kStatusInitializing = 6, // class init in progress
1305 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001306 };
1307
1308 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001309 kPrimNot = 0,
1310 kPrimBoolean,
1311 kPrimByte,
1312 kPrimChar,
1313 kPrimShort,
1314 kPrimInt,
1315 kPrimLong,
1316 kPrimFloat,
1317 kPrimDouble,
1318 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001319 };
1320
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001321 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001322 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1323 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 }
1325
1326 void SetStatus(Status new_status);
1327
1328 // Returns true if the class has failed to link.
1329 bool IsErroneous() const {
1330 return GetStatus() == kStatusError;
1331 }
1332
1333 // Returns true if the class has been loaded.
1334 bool IsIdxLoaded() const {
1335 return GetStatus() >= kStatusIdx;
1336 }
1337
1338 // Returns true if the class has been loaded.
1339 bool IsLoaded() const {
1340 return GetStatus() >= kStatusLoaded;
1341 }
1342
1343 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001344 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001345 return GetStatus() >= kStatusResolved;
1346 }
1347
1348 // Returns true if the class has been verified.
1349 bool IsVerified() const {
1350 return GetStatus() >= kStatusVerified;
1351 }
1352
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001353 // Returns true if the class is initializing.
1354 bool IsInitializing() const {
1355 return GetStatus() >= kStatusInitializing;
1356 }
1357
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001358 // Returns true if the class is initialized.
1359 bool IsInitialized() const {
1360 return GetStatus() == kStatusInitialized;
1361 }
1362
1363 uint32_t GetAccessFlags() const;
1364
1365 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001366 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 }
1368
1369 // Returns true if the class is an interface.
1370 bool IsInterface() const {
1371 return (GetAccessFlags() & kAccInterface) != 0;
1372 }
1373
1374 // Returns true if the class is declared public.
1375 bool IsPublic() const {
1376 return (GetAccessFlags() & kAccPublic) != 0;
1377 }
1378
1379 // Returns true if the class is declared final.
1380 bool IsFinal() const {
1381 return (GetAccessFlags() & kAccFinal) != 0;
1382 }
1383
Elliott Hughes20cde902011-10-04 17:37:27 -07001384 bool IsFinalizable() const {
1385 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1386 }
1387
1388 void SetFinalizable() {
1389 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1390 SetAccessFlags(flags | kAccClassIsFinalizable);
1391 }
1392
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001393 // Returns true if the class is abstract.
1394 bool IsAbstract() const {
1395 return (GetAccessFlags() & kAccAbstract) != 0;
1396 }
1397
1398 // Returns true if the class is an annotation.
1399 bool IsAnnotation() const {
1400 return (GetAccessFlags() & kAccAnnotation) != 0;
1401 }
1402
1403 // Returns true if the class is synthetic.
1404 bool IsSynthetic() const {
1405 return (GetAccessFlags() & kAccSynthetic) != 0;
1406 }
1407
1408 bool IsReferenceClass() const {
1409 return (GetAccessFlags() & kAccClassIsReference) != 0;
1410 }
1411
1412 bool IsWeakReferenceClass() const {
1413 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1414 }
1415
1416 bool IsSoftReferenceClass() const {
1417 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1418 }
1419
1420 bool IsFinalizerReferenceClass() const {
1421 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1422 }
1423
1424 bool IsPhantomReferenceClass() const {
1425 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1426 }
1427
1428 PrimitiveType GetPrimitiveType() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001429 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001430 return static_cast<PrimitiveType>(
1431 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1432 }
1433
1434 void SetPrimitiveType(PrimitiveType new_type) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001435 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001436 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001437 }
1438
1439 // Returns true if the class is a primitive type.
1440 bool IsPrimitive() const {
1441 return GetPrimitiveType() != kPrimNot;
1442 }
1443
1444 bool IsPrimitiveBoolean() const {
1445 return GetPrimitiveType() == kPrimBoolean;
1446 }
1447
1448 bool IsPrimitiveByte() const {
1449 return GetPrimitiveType() == kPrimByte;
1450 }
1451
1452 bool IsPrimitiveChar() const {
1453 return GetPrimitiveType() == kPrimChar;
1454 }
1455
1456 bool IsPrimitiveShort() const {
1457 return GetPrimitiveType() == kPrimShort;
1458 }
1459
1460 bool IsPrimitiveInt() const {
1461 return GetPrimitiveType() == kPrimInt;
1462 }
1463
1464 bool IsPrimitiveLong() const {
1465 return GetPrimitiveType() == kPrimLong;
1466 }
1467
1468 bool IsPrimitiveFloat() const {
1469 return GetPrimitiveType() == kPrimFloat;
1470 }
1471
1472 bool IsPrimitiveDouble() const {
1473 return GetPrimitiveType() == kPrimDouble;
1474 }
1475
1476 bool IsPrimitiveVoid() const {
1477 return GetPrimitiveType() == kPrimVoid;
1478 }
1479
1480 size_t PrimitiveSize() const;
1481
1482 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001483 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001484 }
1485
1486 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001487 return GetFieldObject<Class*>(
1488 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1489 }
1490
1491 void SetComponentType(Class* new_component_type) {
1492 DCHECK(GetComponentType() == NULL);
1493 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001494 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001495 }
1496
1497 size_t GetComponentSize() const {
1498 return GetTypeSize(GetComponentType()->GetDescriptor());
1499 }
1500
1501 bool IsObjectClass() const {
1502 return !IsPrimitive() && GetSuperClass() == NULL;
1503 }
1504
Brian Carlstrom1f870082011-08-23 16:02:11 -07001505 // Creates a raw object instance but does not invoke the default constructor.
1506 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001507
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001508 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001509
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001510 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001511 const String* result = GetFieldObject<const String*>(
1512 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1513 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1514 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1515 return result;
1516 }
1517
1518 void SetDescriptor(String* new_descriptor);
1519
1520 bool IsVariableSize() const {
1521 // Classes and arrays vary in size, and so the object_size_ field cannot
1522 // be used to get their instance size
1523 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001524 }
1525
Brian Carlstrom4873d462011-08-21 15:23:39 -07001526 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001527 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001528 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001529 }
1530
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001531 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001532 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001533 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001534 }
1535
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001536 void SetClassSize(size_t new_class_size) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001537 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughes54e7df12011-09-16 11:47:04 -07001538 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001539 }
1540
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001541 size_t GetObjectSize() const {
1542 CHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001543 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001544 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 CHECK_GE(result, sizeof(Object));
1546 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001547 }
1548
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001549 void SetObjectSize(size_t new_object_size) {
1550 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001551 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001552 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001553 }
1554
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001555 // Returns true if this class is in the same packages as that class.
1556 bool IsInSamePackage(const Class* that) const;
1557
Brian Carlstrome24fa612011-09-29 00:53:55 -07001558 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001559
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001560 // Returns true if this class can access that class.
1561 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001562 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001563 }
1564
jeffhao4a801a42011-09-23 13:53:40 -07001565 // Validate method/field access.
1566 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1567 // quick accept for public access
1568 if (member_flags & kAccPublic) {
1569 return true;
1570 }
1571
1572 // quick accept for access from same class
1573 if (this == access_to) {
1574 return true;
1575 }
1576
1577 // quick reject for private access from another class
1578 if (member_flags & kAccPrivate) {
1579 return false;
1580 }
1581
1582 // Semi-quick test for protected access from a sub-class, which may or
1583 // may not be in the same package.
1584 if (member_flags & kAccProtected) {
1585 if (this->IsSubClass(access_to)) {
1586 return true;
1587 }
1588 }
1589
1590 // Allow protected and private access from other classes in the same package.
1591 return this->IsInSamePackage(access_to);
1592 }
1593
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001594 bool IsSubClass(const Class* klass) const;
1595
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001596 bool IsAssignableFrom(const Class* src) const {
1597 DCHECK(src != NULL);
1598 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001599 // Can always assign to things of the same type
1600 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001601 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001602 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001603 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001604 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001605 return src->Implements(this);
1606 } else if (src->IsArrayClass()) {
1607 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001608 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001609 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001610 }
1611 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001612
buzbee991e3ac2011-09-29 15:44:22 -07001613 // Assignable test for code, won't throw. Null and equality tests already performed
1614 static uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class)
1615 {
1616 DCHECK(klass != NULL);
1617 DCHECK(ref_class != NULL);
1618 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1619 }
1620
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001621 Class* GetSuperClass() const {
1622 // Can only get super class for loaded classes (hack for when runtime is
1623 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001624 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001625 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001626 }
1627
buzbee4a3164f2011-09-03 11:25:10 -07001628 static MemberOffset SuperClassOffset() {
1629 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1630 }
1631
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001632 void SetSuperClass(Class *new_super_class) {
1633 // super class is assigned once, except during class linker initialization
1634 Class* old_super_class = GetFieldObject<Class*>(
1635 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1636 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1637 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001638 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001639 }
1640
1641 bool HasSuperClass() const {
1642 return GetSuperClass() != NULL;
1643 }
1644
1645 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001646 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001647 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1648 false);
1649 }
1650
1651 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001652 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001653 }
1654
1655 const ClassLoader* GetClassLoader() const;
1656
1657 void SetClassLoader(const ClassLoader* new_cl);
1658
1659 static MemberOffset DexCacheOffset() {
1660 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1661 }
1662
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001663 enum {
1664 kDumpClassFullDetail = 1,
1665 kDumpClassClassLoader = (1 << 1),
1666 kDumpClassInitialized = (1 << 2),
1667 };
1668
Elliott Hughes4681c802011-09-25 18:04:37 -07001669 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001670
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001671 DexCache* GetDexCache() const;
1672
1673 void SetDexCache(DexCache* new_dex_cache);
1674
1675 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001676 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001677 return GetFieldObject<ObjectArray<Method>*>(
1678 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1679 }
1680
1681 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1682 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1683 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1684 DCHECK_NE(0, new_direct_methods->GetLength());
1685 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1686 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001687 }
1688
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001689 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001690 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001691 }
1692
1693 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001694 ObjectArray<Method>* direct_methods =
1695 GetFieldObject<ObjectArray<Method>*>(
1696 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1697 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001698 }
1699
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001700 // Returns the number of static, private, and constructor methods.
1701 size_t NumDirectMethods() const {
1702 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1703 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001704
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001705 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001706 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001707 return GetFieldObject<ObjectArray<Method>*>(
1708 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1709 }
1710
1711 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1712 // TODO: we reassign virtual methods to grow the table for miranda
1713 // methods.. they should really just be assigned once
1714 DCHECK_NE(0, new_virtual_methods->GetLength());
1715 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1716 new_virtual_methods, false);
1717 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001718
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001719 // Returns the number of non-inherited virtual methods.
1720 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001721 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001722 }
1723
1724 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001725 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001726 return GetVirtualMethods()->Get(i);
1727 }
1728
1729 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001730 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001731 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001732 }
1733
1734 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001735 ObjectArray<Method>* virtual_methods =
1736 GetFieldObject<ObjectArray<Method>*>(
1737 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1738 virtual_methods->Set(i, f);
1739 }
1740
1741 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001742 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001743 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001744 }
1745
1746 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001747 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001748 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001749 }
1750
1751 void SetVTable(ObjectArray<Method>* new_vtable) {
1752 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1753 }
1754
1755 static MemberOffset VTableOffset() {
1756 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001757 }
1758
Brian Carlstrom30b94452011-08-25 21:35:26 -07001759 // Given a method implemented by this class but potentially from a
1760 // super class, return the specific implementation
1761 // method for this class.
1762 Method* FindVirtualMethodForVirtual(Method* method) {
1763 DCHECK(!method->GetDeclaringClass()->IsInterface());
1764 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001765 // Use the index to a potentially overridden one for this instance's class.
1766 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001767 }
1768
1769 // Given a method implemented by this class, but potentially from a
1770 // super class or interface, return the specific implementation
1771 // method for this class.
1772 Method* FindVirtualMethodForInterface(Method* method);
1773
jeffhaobdb76512011-09-07 11:43:16 -07001774 Method* FindInterfaceMethod(const StringPiece& name,
1775 const StringPiece& descriptor);
1776
Brian Carlstrom30b94452011-08-25 21:35:26 -07001777 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001778 if (method->IsDirect()) {
1779 return method;
1780 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001781 if (method->GetDeclaringClass()->IsInterface()) {
1782 return FindVirtualMethodForInterface(method);
1783 }
1784 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001785 }
1786
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001787 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001788 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001789
1790 Method* FindVirtualMethod(const StringPiece& name,
1791 const StringPiece& descriptor);
1792
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001793 Method* FindDeclaredDirectMethod(const StringPiece& name,
1794 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001795
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001796 Method* FindDirectMethod(const StringPiece& name,
1797 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001798
Carl Shapiro69759ea2011-07-21 18:13:35 -07001799 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001800 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001801 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1802 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1803 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001804 }
1805
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001806 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001807 CHECK(IsIdxLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001808 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001809 }
1810
1811 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1812
1813 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001814 CHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001815 return GetFieldObject<ObjectArray<Class>*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 }
1817
1818 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001819 DCHECK(NULL == GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001820 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001821 }
1822
1823 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001824 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001825 ObjectArray<Class>* interfaces =
1826 GetFieldObject<ObjectArray<Class>*>(
1827 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1828 interfaces->Set(i, f);
1829 }
1830
1831 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001832 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001833 return GetInterfaces()->Get(i);
1834 }
1835
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001836 int32_t GetIfTableCount() const {
1837 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1838 if (iftable == NULL) {
1839 return 0;
1840 }
1841 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001842 }
1843
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001844 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001845 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001846 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001847 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1848 }
1849
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001850 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001851 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001852 }
1853
1854 // Get instance fields
1855 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001856 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001857 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001858 }
1859
1860 void SetIFields(ObjectArray<Field>* new_ifields) {
1861 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1862 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001863 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001864 }
1865
1866 size_t NumInstanceFields() const {
1867 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1868 }
1869
1870 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1871 DCHECK_NE(NumInstanceFields(), 0U);
1872 return GetIFields()->Get(i);
1873 }
1874
1875 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1876 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1877 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1878 ifields->Set(i, f);
1879 }
1880
1881 // Returns the number of instance fields containing reference types.
1882 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001883 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001884 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001885 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001886 }
1887
1888 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001889 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001890 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001891 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001892 }
1893
1894 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001895 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001896 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001897 }
1898
1899 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001900 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001901 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 }
1903
1904 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1905
1906 // Beginning of static field data
1907 static MemberOffset FieldsOffset() {
1908 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1909 }
1910
1911 // Returns the number of static fields containing reference types.
1912 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001913 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001914 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001915 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001916 }
1917
1918 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001919 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001920 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001921 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001922 }
1923
1924 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001925 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001926 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001927 }
1928
1929 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001930 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001931 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001932 }
1933
1934 void SetSFields(ObjectArray<Field>* new_sfields) {
1935 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1936 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001937 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001938 }
1939
1940 size_t NumStaticFields() const {
1941 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1942 }
1943
1944 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1945 return GetSFields()->Get(i);
1946 }
1947
1948 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1949 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1950 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1951 sfields->Set(i, f);
1952 }
1953
1954 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001955 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001956 }
1957
1958 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1959
1960 // Finds the given instance field in this class or a superclass.
1961 Field* FindInstanceField(const StringPiece& name, Class* type);
1962
1963 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1964
1965 // Finds the given static field in this class or a superclass.
1966 Field* FindStaticField(const StringPiece& name, Class* type);
1967
1968 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1969
Elliott Hughesdcc24742011-09-07 14:02:44 -07001970 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001971 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001972 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1973 }
1974
Elliott Hughesdcc24742011-09-07 14:02:44 -07001975 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001976 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001977 }
1978
1979 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001980 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001981 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001982 }
1983
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001984 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001985 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001986 }
1987
Brian Carlstromc74255f2011-09-11 22:47:39 -07001988 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001989
Brian Carlstromc74255f2011-09-11 22:47:39 -07001990 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001991
jeffhaobdb76512011-09-07 11:43:16 -07001992 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001993 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001994 bool IsArrayAssignableFromArray(const Class* klass) const;
1995 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001996
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001997 // descriptor for the class such as "java.lang.Class" or "[C"
1998 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001999
Brian Carlstrom693267a2011-09-06 09:25:34 -07002000 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07002001 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002002
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002003 // For array classes, the component class object for instanceof/checkcast
2004 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002005 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002006
Brian Carlstrom693267a2011-09-06 09:25:34 -07002007 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
2008 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002009
Brian Carlstrom693267a2011-09-06 09:25:34 -07002010 // DexCache of resolved constant pool entries
2011 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
2012 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002013
2014 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07002015 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002016
Brian Carlstrom693267a2011-09-06 09:25:34 -07002017 // instance fields
2018 //
2019 // These describe the layout of the contents of an Object.
2020 // Note that only the fields directly declared by this class are
2021 // listed in ifields; fields declared by a superclass are listed in
2022 // the superclass's Class.ifields.
2023 //
2024 // All instance fields that refer to objects are guaranteed to be at
2025 // the beginning of the field list. num_reference_instance_fields_
2026 // specifies the number of reference fields.
2027 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002028
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002029 // Interface table (iftable_), one entry per interface supported by
2030 // this class. That means one entry for each interface we support
2031 // directly, indirectly via superclass, or indirectly via
2032 // superinterface. This will be null if neither we nor our
2033 // superclass implement any interfaces.
2034 //
2035 // Why we need this: given "class Foo implements Face", declare
2036 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
2037 // is part of the Face interface. We can't easily use a single
2038 // vtable.
2039 //
2040 // For every interface a concrete class implements, we create an array
2041 // of the concrete vtable_ methods for the methods in the interface.
2042 ObjectArray<InterfaceEntry>* iftable_;
2043
Brian Carlstromdbc05252011-09-09 01:59:59 -07002044 // array of interfaces this class implements directly
2045 // see also interfaces_type_idx_
2046 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002047
2048 // array of type_idx's for interfaces this class implements directly
2049 // see also interfaces_
2050 IntArray* interfaces_type_idx_;
2051
Brian Carlstromdbc05252011-09-09 01:59:59 -07002052 // Static fields
2053 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002054
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002055 // source file name, if known. Otherwise, NULL.
2056 String* source_file_;
2057
Brian Carlstromdbc05252011-09-09 01:59:59 -07002058 // The superclass, or NULL if this is java.lang.Object or a
2059 // primitive type.
2060 // see also super_class_type_idx_;
2061 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002062
Brian Carlstromdbc05252011-09-09 01:59:59 -07002063 // If class verify fails, we must return same error on subsequent tries.
2064 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2065 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002066
Brian Carlstromdbc05252011-09-09 01:59:59 -07002067 // virtual methods defined in this class; invoked through vtable
2068 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002069
Brian Carlstromdbc05252011-09-09 01:59:59 -07002070 // Virtual method table (vtable), for use by "invoke-virtual". The
2071 // vtable from the superclass is copied in, and virtual methods from
2072 // our class either replace those from the super or are appended.
2073 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002074
Brian Carlstromdbc05252011-09-09 01:59:59 -07002075 // access flags; low 16 bits are defined by VM spec
2076 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002077
Jesse Wilson6bf19152011-09-29 13:12:33 -04002078 // Total size of the Class instance; used when allocating storage on gc heap.
2079 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002080 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002081
Elliott Hughes5f791332011-09-15 17:45:30 -07002082 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002083 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002084
Brian Carlstromdbc05252011-09-09 01:59:59 -07002085 // number of instance fields that are object refs
2086 size_t num_reference_instance_fields_;
2087
2088 // number of static fields that are object refs
2089 size_t num_reference_static_fields_;
2090
2091 // Total object size; used when allocating storage on gc heap.
2092 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002093 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002094 size_t object_size_;
2095
2096 // primitive type index, or kPrimNot (0); set for generated prim classes
2097 PrimitiveType primitive_type_;
2098
2099 // Bitmap of offsets of ifields.
2100 uint32_t reference_instance_offsets_;
2101
2102 // Bitmap of offsets of sfields.
2103 uint32_t reference_static_offsets_;
2104
Brian Carlstrom693267a2011-09-06 09:25:34 -07002105 // state of class initialization
2106 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002107
Brian Carlstrom693267a2011-09-06 09:25:34 -07002108 // Set in LoadClass, used to LinkClass
2109 // see also super_class_
2110 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002111
Brian Carlstrom693267a2011-09-06 09:25:34 -07002112 // TODO: ?
2113 // initiating class loader list
2114 // NOTE: for classes with low serialNumber, these are unused, and the
2115 // values are kept in a table in gDvm.
2116 // InitiatingLoaderList initiating_loader_list_;
2117
Brian Carlstrom4873d462011-08-21 15:23:39 -07002118 // Location of first static field.
2119 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002120
Brian Carlstrom693267a2011-09-06 09:25:34 -07002121 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002122 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002123};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002124
Elliott Hughes1f359b02011-07-17 14:27:17 -07002125std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002126
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002127inline void Object::SetClass(Class* new_klass) {
2128 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002129 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002130}
2131
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002132inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002133 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002134 DCHECK(GetClass() != NULL);
2135 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002136}
2137
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002138inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002139 Class* java_lang_Class = GetClass()->GetClass();
2140 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002141}
2142
Brian Carlstrom4873d462011-08-21 15:23:39 -07002143inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002144 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002145 return this == java_lang_Class;
2146}
2147
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002148inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002149 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002150}
2151
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002152template<class T>
2153inline ObjectArray<T>* Object::AsObjectArray() {
2154 DCHECK(IsObjectArray());
2155 return down_cast<ObjectArray<T>*>(this);
2156}
2157
2158template<class T>
2159inline const ObjectArray<T>* Object::AsObjectArray() const {
2160 DCHECK(IsObjectArray());
2161 return down_cast<const ObjectArray<T>*>(this);
2162}
2163
Brian Carlstromb63ec392011-08-27 17:38:27 -07002164inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002165 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002166}
2167
Brian Carlstroma663ea52011-08-19 23:33:41 -07002168inline bool Object::IsField() const {
2169 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002170 Class* java_lang_reflect_Field =
2171 java_lang_Class->GetInstanceField(0)->GetClass();
2172 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002173}
2174
2175inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002176 Class* c = GetClass();
2177 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002178}
2179
2180inline bool Object::IsReferenceInstance() const {
2181 return GetClass()->IsReferenceClass();
2182}
2183
2184inline bool Object::IsWeakReferenceInstance() const {
2185 return GetClass()->IsWeakReferenceClass();
2186}
2187
2188inline bool Object::IsSoftReferenceInstance() const {
2189 return GetClass()->IsSoftReferenceClass();
2190}
2191
2192inline bool Object::IsFinalizerReferenceInstance() const {
2193 return GetClass()->IsFinalizerReferenceClass();
2194}
2195
2196inline bool Object::IsPhantomReferenceInstance() const {
2197 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002198}
2199
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002200inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002201 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002202 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002203 result = AsArray()->SizeOf();
2204 } else if (IsClass()) {
2205 result = AsClass()->SizeOf();
2206 } else {
2207 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002208 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002209 DCHECK(!IsField() || result == sizeof(Field));
2210 DCHECK(!IsMethod() || result == sizeof(Method));
2211 return result;
2212}
2213
2214inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002215 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002216 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002217 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2218 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002219 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002220 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2221 num_bytes.Uint32Value(), false);
2222}
2223
2224inline Class* Field::GetDeclaringClass() const {
2225 Class* result = GetFieldObject<Class*>(
2226 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2227 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002228 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002229 return result;
2230}
2231
2232inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2233 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2234 new_declaring_class, false);
2235}
2236
2237inline Class* Method::GetDeclaringClass() const {
2238 Class* result =
2239 GetFieldObject<Class*>(
2240 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2241 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002242 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002243 return result;
2244}
2245
2246inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2247 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2248 new_declaring_class, false);
2249}
2250
2251inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002252 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002253 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2254 false);
2255}
2256
2257inline bool Method::IsReturnAReference() const {
2258 return !GetReturnType()->IsPrimitive();
2259}
2260
2261inline bool Method::IsReturnAFloat() const {
2262 return GetReturnType()->IsPrimitiveFloat();
2263}
2264
2265inline bool Method::IsReturnADouble() const {
2266 return GetReturnType()->IsPrimitiveDouble();
2267}
2268
2269inline bool Method::IsReturnALong() const {
2270 return GetReturnType()->IsPrimitiveLong();
2271}
2272
2273inline bool Method::IsReturnVoid() const {
2274 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002275}
2276
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002277inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002278 // This is safe from overflow because the array was already allocated, so we know it's sane.
2279 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002280}
2281
2282template<class T>
2283void ObjectArray<T>::Set(int32_t i, T* object) {
2284 if (IsValidIndex(i)) {
2285 if (object != NULL) {
2286 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002287 if (!object->InstanceOf(element_class)) {
2288 ThrowArrayStoreException(object);
2289 return;
2290 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002291 }
2292 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2293 SetFieldObject(data_offset, object, false);
2294 }
2295}
2296
2297template<class T>
2298void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2299 DCHECK(IsValidIndex(i));
2300 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2301 SetFieldObject(data_offset, object, false);
2302}
2303
2304template<class T>
2305void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2306 ObjectArray<T>* dst, int dst_pos,
2307 size_t length) {
2308 if (src->IsValidIndex(src_pos) &&
2309 src->IsValidIndex(src_pos+length-1) &&
2310 dst->IsValidIndex(dst_pos) &&
2311 dst->IsValidIndex(dst_pos+length-1)) {
2312 MemberOffset src_offset(DataOffset().Int32Value() +
2313 src_pos * sizeof(Object*));
2314 MemberOffset dst_offset(DataOffset().Int32Value() +
2315 dst_pos * sizeof(Object*));
2316 Class* array_class = dst->GetClass();
2317 if (array_class == src->GetClass()) {
2318 // No need for array store checks if arrays are of the same type
2319 for (size_t i=0; i < length; i++) {
2320 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2321 dst->SetFieldObject(dst_offset, object, false);
2322 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2323 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2324 }
2325 } else {
2326 Class* element_class = array_class->GetComponentType();
2327 CHECK(!element_class->IsPrimitive());
2328 for (size_t i=0; i < length; i++) {
2329 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002330 if (object != NULL && !object->InstanceOf(element_class)) {
2331 dst->ThrowArrayStoreException(object);
2332 return;
2333 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002334 dst->SetFieldObject(dst_offset, object, false);
2335 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2336 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2337 }
2338 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002339 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002340 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002341}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002342
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002343class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002344 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002345 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002346 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002347 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002348 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2349};
2350
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002351class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002352 private:
2353 CharArray* ASCII_;
2354 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002355 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002356 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002357 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002358 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2359};
2360
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002361class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002362 private:
2363 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2364 uint32_t TYPE_BOOLEAN_;
2365 uint32_t TYPE_BYTE_;
2366 uint32_t TYPE_CHAR_;
2367 uint32_t TYPE_DOUBLE_;
2368 uint32_t TYPE_FLOAT_;
2369 uint32_t TYPE_INTEGER_;
2370 uint32_t TYPE_LONG_;
2371 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002372 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002373 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2374};
2375
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002376class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002377 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002378 Object* ORDER_BY_SIGNATURE_;
2379 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002380 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2381};
2382
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002383template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002384class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002385 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002386 typedef T ElementType;
2387
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002388 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002389
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002390 const T* GetData() const {
2391 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002392 }
2393
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002394 T* GetData() {
2395 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002396 }
2397
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002398 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002399 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002400 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002401 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002402 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002403 }
2404
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002405 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002406 // TODO: ArrayStoreException
2407 if (IsValidIndex(i)) {
2408 GetData()[i] = value;
2409 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002410 }
2411
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002412 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002413 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002414 CHECK(array_class != NULL);
2415 array_class_ = array_class;
2416 }
2417
Brian Carlstroma663ea52011-08-19 23:33:41 -07002418 static void ResetArrayClass() {
2419 CHECK(array_class_ != NULL);
2420 array_class_ = NULL;
2421 }
2422
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002423 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002424 // Location of first element.
2425 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002426
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002427 static Class* array_class_;
2428
Carl Shapirof88c9522011-08-06 15:47:38 -07002429 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002430};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002432inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2433 DCHECK(NULL == GetFieldObject<IntArray*>(
2434 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2435 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2436 new_interfaces_idx, false);
2437}
2438
2439// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002440class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002441 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002442 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002443 const CharArray* result = GetFieldObject<const CharArray*>(
2444 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2445 DCHECK(result != NULL);
2446 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002447 }
2448
Elliott Hughes814e4032011-08-23 12:07:56 -07002449 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002450 int32_t result = GetField32(
2451 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2452 DCHECK_LE(0, result);
2453 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002454 }
2455
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002456 int32_t GetLength() const;
2457
Brian Carlstrom395520e2011-09-25 19:35:00 -07002458 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002459
2460 void ComputeHashCode() {
2461 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002462 }
2463
Elliott Hughes814e4032011-08-23 12:07:56 -07002464 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002465 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002466 }
2467
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002468 uint16_t CharAt(int32_t index) const;
2469
Brian Carlstromc74255f2011-09-11 22:47:39 -07002470 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002471
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002472 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002473 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002474 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002475
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002476 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002477
Jesse Wilson8989d992011-08-02 13:39:42 -07002478 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002479 const char* utf8_data_in);
2480
2481 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2482
2483 static String* Alloc(Class* java_lang_String, CharArray* array);
2484
2485 bool Equals(const char* modified_utf8) const;
2486
2487 // TODO: do we need this overload? give it a more intention-revealing name.
2488 bool Equals(const StringPiece& modified_utf8) const;
2489
2490 bool Equals(const String* that) const;
2491
2492 // TODO: do we need this overload? give it a more intention-revealing name.
2493 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2494 int32_t that_length) const;
2495
2496 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2497 std::string ToModifiedUtf8() const;
2498
2499 static Class* GetJavaLangString() {
2500 DCHECK(java_lang_String_ != NULL);
2501 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002502 }
2503
Brian Carlstroma663ea52011-08-19 23:33:41 -07002504 static void SetClass(Class* java_lang_String);
2505 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002506
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002507 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002508 void SetHashCode(int32_t new_hash_code) {
2509 DCHECK_EQ(0u,
2510 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2511 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2512 new_hash_code, false);
2513 }
2514
2515 void SetCount(int32_t new_count) {
2516 DCHECK_LE(0, new_count);
2517 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2518 }
2519
2520 void SetOffset(int32_t new_offset) {
2521 DCHECK_LE(0, new_offset);
2522 DCHECK_GE(GetLength(), new_offset);
2523 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2524 }
2525
2526 void SetArray(CharArray* new_array) {
2527 DCHECK(new_array != NULL);
2528 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2529 }
2530
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002531 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2532 CharArray* array_;
2533
Brian Carlstromdbc05252011-09-09 01:59:59 -07002534 int32_t count_;
2535
Carl Shapirof88c9522011-08-06 15:47:38 -07002536 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002537
Elliott Hughes289da822011-08-16 10:11:20 -07002538 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002539
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002540 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002541
Brian Carlstrom693267a2011-09-06 09:25:34 -07002542 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002543 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002544};
2545
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002546inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002547 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002548 String* result =
2549 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2550 DCHECK(result != NULL);
2551 return result;
2552}
2553
2554inline void Field::SetName(String* new_name) {
2555 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2556 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002557}
2558
2559inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002560 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002561 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2562}
2563
2564inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002565 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002566 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2567}
2568
2569inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002570 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002571 return MemberOffset(
2572 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2573}
2574
2575inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002576 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002577 return MemberOffset(
2578 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2579}
2580
2581inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002582 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002583 const String* result =
2584 GetFieldObject<const String*>(
2585 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2586 DCHECK(result != NULL);
2587 return result;
2588}
2589
2590inline void Method::SetName(String* new_name) {
2591 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2592 new_name, false);
2593
2594}
2595
jeffhaoe23d93c2011-09-15 14:48:43 -07002596inline ByteArray* Method::GetRegisterMapData() const {
2597 return GetFieldObject<ByteArray*>(
2598 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2599}
2600
jeffhaoa0a764a2011-09-16 10:43:38 -07002601inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002602 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002603 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002604}
2605
2606inline ByteArray* Method::GetRegisterMapHeader() const {
2607 return GetFieldObject<ByteArray*>(
2608 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2609}
2610
jeffhaoa0a764a2011-09-16 10:43:38 -07002611inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002612 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002613 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002614}
2615
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002616inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002617 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002618 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002619 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2620}
2621
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002622inline void Method::SetShorty(String* new_shorty) {
2623 DCHECK(NULL == GetFieldObject<String*>(
2624 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2625 DCHECK_LE(1, new_shorty->GetLength());
2626 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2627}
2628
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002629inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002630 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002631 const String* result =
Elliott Hughesf5a7a472011-10-07 14:31:02 -07002632 GetFieldObject<const String*>(OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002633 DCHECK(result != NULL);
2634 return result;
2635}
2636
2637inline void Method::SetSignature(String* new_signature) {
2638 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2639 new_signature, false);
2640}
2641
2642inline uint32_t Class::GetAccessFlags() const {
2643 // Check class is loaded or this is java.lang.String that has a
2644 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002645 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002646 this == String::GetJavaLangString() ||
2647 this == Field::GetJavaLangReflectField() ||
2648 this == Method::GetConstructorClass() ||
2649 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002650 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2651}
2652
2653inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002654 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002655 DCHECK(new_descriptor != NULL);
2656 DCHECK_NE(0, new_descriptor->GetLength());
2657 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2658 new_descriptor, false);
2659}
2660
Brian Carlstromc74255f2011-09-11 22:47:39 -07002661inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002662 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002663 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2664}
2665
2666inline void Class::SetSourceFile(String* new_source_file) {
2667 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2668}
2669
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002670inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002671 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002672 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2673}
2674
2675inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002676 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002677 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002678}
2679
2680inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002681 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002682 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002683}
2684
2685inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002686 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002687 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002688}
2689
2690inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002691 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002692 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002693}
2694
2695inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002696 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002697 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2698}
2699
2700// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002701class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002702 public:
2703 void SetDetailMessage(String* new_detail_message) {
2704 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2705 new_detail_message, false);
2706 }
2707
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002708 private:
2709 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2710 Throwable* cause_;
2711 String* detail_message_;
2712 Object* stack_state_; // Note this is Java volatile:
2713 Object* stack_trace_;
2714 Object* suppressed_exceptions_;
2715
Brian Carlstrom693267a2011-09-06 09:25:34 -07002716 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002717 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2718};
2719
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002720// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002721class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002722 public:
2723 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002724 return GetFieldObject<const String*>(
2725 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002726 }
2727
2728 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002729 return GetFieldObject<const String*>(
2730 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002731 }
2732
2733 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002734 return GetFieldObject<const String*>(
2735 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002736 }
2737
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002738 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002739 return GetField32(
2740 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002741 }
2742
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002743 static StackTraceElement* Alloc(const String* declaring_class,
2744 const String* method_name,
2745 const String* file_name,
2746 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002747
2748 static void SetClass(Class* java_lang_StackTraceElement);
2749
2750 static void ResetClass();
2751
2752 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002753 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002754 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002755 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002756 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002757 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002758
2759 static Class* GetStackTraceElement() {
2760 DCHECK(java_lang_StackTraceElement_ != NULL);
2761 return java_lang_StackTraceElement_;
2762 }
2763
2764 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002765
2766 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002767 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2768};
2769
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002770class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002771 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002772 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002773 Class* interface = Get(kInterface)->AsClass();
2774 DCHECK(interface != NULL);
2775 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002776 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002777
Brian Carlstrom30b94452011-08-25 21:35:26 -07002778 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002779 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002780 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002781 DCHECK(Get(kInterface) == NULL);
2782 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002783 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002784
Brian Carlstrom86927212011-09-15 11:31:11 -07002785 size_t GetMethodArrayCount() const {
2786 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2787 if (method_array == 0) {
2788 return 0;
2789 }
2790 return method_array->GetLength();
2791 }
2792
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002793 ObjectArray<Method>* GetMethodArray() const {
2794 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2795 DCHECK(method_array != NULL);
2796 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002797 }
2798
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002799 void SetMethodArray(ObjectArray<Method>* new_ma) {
2800 DCHECK(new_ma != NULL);
2801 DCHECK(Get(kMethodArray) == NULL);
2802 Set(kMethodArray, new_ma);
2803 }
2804
2805 static size_t LengthAsArray() {
2806 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002807 }
2808
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002809 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002810
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002811 enum ArrayIndex {
2812 // Points to the interface class.
2813 kInterface = 0,
2814 // Method pointers into the vtable, allow fast map from interface
2815 // method index to concrete instance method.
2816 kMethodArray = 1,
2817 kMax = 2,
2818 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002819
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002820 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002821};
2822
2823} // namespace art
2824
2825#endif // ART_SRC_OBJECT_H_