blob: 763480701c147e812582cefef6870779c3be4c81 [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
136// if only kAccClassIsReference is set, we have a soft reference
137static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
138static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
139static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
140static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
141
142static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
143 | kAccClassIsWeakReference
144 | kAccClassIsFinalizerReference
145 | kAccClassIsPhantomReference);
146
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700148 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 */
150/*
151 * A magic value for refOffsets. Ignore the bits and walk the super
152 * chain when this is the value.
153 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
154 * fields followed by 2 ref instance fields.]
155 */
156#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
158#define CLASS_OFFSET_ALIGNMENT 4
159#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
160/*
161 * Given an offset, return the bit number which would encode that offset.
162 * Local use only.
163 */
164#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700165 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166 CLASS_OFFSET_ALIGNMENT)
167/*
168 * Is the given offset too large to be encoded?
169 */
170#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
171 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
172/*
173 * Return a single bit, encoding the offset.
174 * Undefined if the offset is too large, as defined above.
175 */
176#define CLASS_BIT_FROM_OFFSET(byteOffset) \
177 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
178/*
179 * Return an offset, given a bit number as returned from CLZ.
180 */
181#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700182 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184#define OFFSET_OF_OBJECT_MEMBER(type, field) \
185 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700187// Classes shared with the managed side of the world need to be packed
188// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700189#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700193 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 static MemberOffset ClassOffset() {
195 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196 }
197
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700199 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 }
201
202 void SetClass(Class* new_klass);
203
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700204 bool InstanceOf(const Class* klass) const;
205
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700206 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700207
Elliott Hughes081be7f2011-09-18 16:50:26 -0700208 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700209
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700210 static MemberOffset MonitorOffset() {
211 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
212 }
213
Elliott Hughes5f791332011-09-15 17:45:30 -0700214 volatile int32_t* GetRawLockWordAddress() {
215 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
216 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
217 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 }
219
Elliott Hughes5f791332011-09-15 17:45:30 -0700220 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221
Elliott Hughes5f791332011-09-15 17:45:30 -0700222 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223
Ian Rogersff1ed472011-09-20 13:46:24 -0700224 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Elliott Hughes5f791332011-09-15 17:45:30 -0700226 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Elliott Hughes5f791332011-09-15 17:45:30 -0700228 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Elliott Hughes5f791332011-09-15 17:45:30 -0700232 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700233
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700234 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235
236 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700237 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700238 return down_cast<Class*>(this);
239 }
240
241 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700242 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243 return down_cast<const Class*>(this);
244 }
245
Brian Carlstrom4873d462011-08-21 15:23:39 -0700246 bool IsClassClass() const;
247
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700248 bool IsObjectArray() const;
249
250 template<class T>
251 ObjectArray<T>* AsObjectArray() {
252 DCHECK(IsObjectArray());
253 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254 }
255
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700256 template<class T>
257 const ObjectArray<T>* AsObjectArray() const {
258 DCHECK(IsObjectArray());
259 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260 }
261
Brian Carlstromb63ec392011-08-27 17:38:27 -0700262 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700263
264 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700265 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700266 return down_cast<Array*>(this);
267 }
268
269 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700270 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700271 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 }
273
Brian Carlstroma663ea52011-08-19 23:33:41 -0700274 bool IsString() const;
275
276 String* AsString() {
277 DCHECK(IsString());
278 return down_cast<String*>(this);
279 }
280
281 bool IsMethod() const;
282
283 Method* AsMethod() {
284 DCHECK(IsMethod());
285 return down_cast<Method*>(this);
286 }
287
Brian Carlstrom4873d462011-08-21 15:23:39 -0700288 const Method* AsMethod() const {
289 DCHECK(IsMethod());
290 return down_cast<const Method*>(this);
291 }
292
Brian Carlstroma663ea52011-08-19 23:33:41 -0700293 bool IsField() const;
294
295 Field* AsField() {
296 DCHECK(IsField());
297 return down_cast<Field*>(this);
298 }
299
Brian Carlstrom4873d462011-08-21 15:23:39 -0700300 const Field* AsField() const {
301 DCHECK(IsField());
302 return down_cast<const Field*>(this);
303 }
304
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 bool IsReferenceInstance() const;
306
307 bool IsWeakReferenceInstance() const;
308
309 bool IsSoftReferenceInstance() const;
310
311 bool IsFinalizerReferenceInstance() const;
312
313 bool IsPhantomReferenceInstance() const;
314
315 // Accessors for Java type fields
316 template<class T>
317 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700318 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
319 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700320 Heap::VerifyObject(result);
321 return result;
322 }
323
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700324 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700326 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
327 if (new_value != NULL) {
328 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 }
331
332 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
333 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700334 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
335 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700337 return android_atomic_acquire_load(word_addr);
338 } else {
339 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700340 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700341 }
342
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700343 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
344 if (this_is_valid) {
345 Heap::VerifyObject(this);
346 }
347 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700348 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700350 /*
351 * TODO: add an android_atomic_synchronization_store() function and
352 * use it in the 32-bit volatile set handlers. On some platforms we
353 * can use a fast atomic instruction and avoid the barriers.
354 */
355 ANDROID_MEMBAR_STORE();
356 *word_addr = new_value;
357 ANDROID_MEMBAR_FULL();
358 } else {
359 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 }
362
363 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
364 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700365 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700366 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700368 uint64_t result = QuasiAtomicRead64(addr);
369 ANDROID_MEMBAR_FULL();
370 return result;
371 } else {
372 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 }
375
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700376 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700378 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
379 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700381 ANDROID_MEMBAR_STORE();
382 QuasiAtomicSwap64(new_value, addr);
383 // Post-store barrier not required due to use of atomic op or mutex.
384 } else {
385 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 }
388
389 protected:
390 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391 template<class T>
392 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700393 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
395
396 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700397 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
398 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700399 }
400
401 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700402 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403
Elliott Hughes5f791332011-09-15 17:45:30 -0700404 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405
Elliott Hughes5f791332011-09-15 17:45:30 -0700406 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700407 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700408 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700409};
410
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700411// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700412class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400413 private:
414 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700415 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700416 friend struct AccessibleObjectOffsets; // for verifying offset information
417 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400418};
419
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700421class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700422 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700424
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 void SetDeclaringClass(Class *new_declaring_class);
426
427 const String* GetName() const;
428
429 void SetName(String* new_name);
430
431 uint32_t GetAccessFlags() const;
432
433 void SetAccessFlags(uint32_t new_access_flags) {
434 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
435 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700436 }
437
Elliott Hughes80609252011-09-23 17:24:51 -0700438 bool IsPublic() const {
439 return (GetAccessFlags() & kAccPublic) != 0;
440 }
441
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700442 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700443 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700444 }
445
jeffhaobdb76512011-09-07 11:43:16 -0700446 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700447 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700448 }
449
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700450 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700451
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700454 // Gets type using type index and resolved types in the dex cache, may be null
455 // if type isn't yet resolved
456 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700457
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700458 // Performs full resolution, may return null and set exceptions if type cannot
459 // be resolved
460 Class* GetType() const;
461
462 // Offset to field within an Object
463 MemberOffset GetOffset() const;
464
buzbee34cd9e52011-09-08 14:31:52 -0700465 static MemberOffset OffsetOffset() {
466 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
467 }
468
Brian Carlstrom845490b2011-09-19 15:56:53 -0700469 static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer);
470 static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer);
471 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static);
buzbee34cd9e52011-09-08 14:31:52 -0700472
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700473 MemberOffset GetOffsetDuringLinking() const;
474
475 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700476
Brian Carlstrom4873d462011-08-21 15:23:39 -0700477 // field access, null object for static fields
478 bool GetBoolean(const Object* object) const;
479 void SetBoolean(Object* object, bool z) const;
480 int8_t GetByte(const Object* object) const;
481 void SetByte(Object* object, int8_t b) const;
482 uint16_t GetChar(const Object* object) const;
483 void SetChar(Object* object, uint16_t c) const;
484 uint16_t GetShort(const Object* object) const;
485 void SetShort(Object* object, uint16_t s) const;
486 int32_t GetInt(const Object* object) const;
487 void SetInt(Object* object, int32_t i) const;
488 int64_t GetLong(const Object* object) const;
489 void SetLong(Object* object, int64_t j) const;
490 float GetFloat(const Object* object) const;
491 void SetFloat(Object* object, float f) const;
492 double GetDouble(const Object* object) const;
493 void SetDouble(Object* object, double d) const;
494 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700495 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700496
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 // Slow path routines for static field access when field was unresolved at
498 // compile time
499 static uint32_t Get32StaticFromCode(uint32_t field_idx,
500 const Method* referrer);
501 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
502 uint32_t new_value);
503 static uint64_t Get64StaticFromCode(uint32_t field_idx,
504 const Method* referrer);
505 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
506 uint64_t new_value);
507 static Object* GetObjStaticFromCode(uint32_t field_idx,
508 const Method* referrer);
509 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
510 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700511
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 static Class* GetJavaLangReflectField() {
513 DCHECK(java_lang_reflect_Field_ != NULL);
514 return java_lang_reflect_Field_;
515 }
516
517 static void SetClass(Class* java_lang_reflect_Field);
518 static void ResetClass();
519
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700520 bool IsVolatile() const {
521 return (GetAccessFlags() & kAccVolatile) != 0;
522 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700523
Elliott Hughes80609252011-09-23 17:24:51 -0700524 void InitJavaFields();
525
buzbee1da522d2011-09-04 11:22:20 -0700526 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700527 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700528 uint32_t Get32(const Object* object) const;
529 void Set32(Object* object, uint32_t new_value) const;
530 uint64_t Get64(const Object* object) const;
531 void Set64(Object* object, uint64_t new_value) const;
532 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700533 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700534
Elliott Hughes80609252011-09-23 17:24:51 -0700535 void InitJavaFieldsLocked();
536
Jesse Wilson35baaab2011-08-10 16:18:03 -0400537 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700538
Jesse Wilson35baaab2011-08-10 16:18:03 -0400539 // The class in which this field is declared.
540 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541
Jesse Wilson35baaab2011-08-10 16:18:03 -0400542 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700543
Brian Carlstromdbc05252011-09-09 01:59:59 -0700544 const String* name_;
545
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700547 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400548
Brian Carlstromdbc05252011-09-09 01:59:59 -0700549 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700550
Jesse Wilson35baaab2011-08-10 16:18:03 -0400551 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700552
553 // Offset of field within an instance or in the Class' static fields
554 uint32_t offset_;
555
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700556 // Dex cache index of resolved type
557 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400558
Brian Carlstrom693267a2011-09-06 09:25:34 -0700559 int32_t slot_;
560
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561 static Class* java_lang_reflect_Field_;
562
Brian Carlstrom693267a2011-09-06 09:25:34 -0700563 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400564 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700565};
566
Jesse Wilson6bf19152011-09-29 13:12:33 -0400567// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700568class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700569 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700570 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700571 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700572 Object* obj,
573 Thread* thread,
574 byte* args,
575 JValue* result);
576
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700581 static MemberOffset DeclaringClassOffset() {
582 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700583 }
584
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700585 // Returns the method name, e.g. "<init>" or "eatLunch"
586 const String* GetName() const;
587
588 void SetName(String* new_name);
589
jeffhaoe23d93c2011-09-15 14:48:43 -0700590 ByteArray* GetRegisterMapData() const;
591
jeffhaoa0a764a2011-09-16 10:43:38 -0700592 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700593
594 ByteArray* GetRegisterMapHeader() const;
595
jeffhaoa0a764a2011-09-16 10:43:38 -0700596 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700597
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700598 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700599
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700600 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700601
602 const String* GetSignature() const;
603
604 void SetSignature(String* new_signature);
605
606 bool HasSameNameAndDescriptor(const Method* that) const;
607
608 uint32_t GetAccessFlags() const;
609
610 void SetAccessFlags(uint32_t new_access_flags) {
611 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
612 false);
613 }
614
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700615 // Returns true if the method is declared public.
616 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700617 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700618 }
619
620 // Returns true if the method is declared private.
621 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700622 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700623 }
624
625 // Returns true if the method is declared static.
626 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700627 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700628 }
629
Brian Carlstrom1f870082011-08-23 16:02:11 -0700630 // Returns true if the method is a constructor.
631 bool IsConstructor() const {
632 return (access_flags_ & kAccConstructor) != 0;
633 }
634
635 // Returns true if the method is static, private, or a constructor.
636 bool IsDirect() const {
637 return IsStatic() || IsPrivate() || IsConstructor();
638 }
639
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 // Returns true if the method is declared synchronized.
641 bool IsSynchronized() const {
642 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700643 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 }
645
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 }
649
Elliott Hughes80609252011-09-23 17:24:51 -0700650 bool IsMiranda() const {
651 return (GetAccessFlags() & kAccMiranda) != 0;
652 }
653
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700654 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700655 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700656 }
657
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700659 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700660 }
661
662 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700664 }
665
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666 uint16_t GetMethodIndex() const;
667
668 size_t GetVtableIndex() const {
669 return GetMethodIndex();
670 }
671
672 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700673 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674 new_method_index, false);
675 }
676
677 static MemberOffset MethodIndexOffset() {
678 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
679 }
680
681 uint32_t GetCodeItemOffset() const {
682 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
683 }
684
685 void SetCodeItemOffset(uint32_t new_code_off) {
686 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
687 new_code_off, false);
688 }
689
690 // Number of 32bit registers that would be required to hold all the arguments
691 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700692
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700693 // Number of argument bytes required for densely packing the
694 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700695 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700696
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700697 uint16_t NumRegisters() const;
698
699 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700700 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700701 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700702 }
703
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700704 uint16_t NumIns() const;
705
706 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700707 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700708 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700709 }
710
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700711 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700712
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700714 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700715 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700716 }
717
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700718 uint32_t GetProtoIdx() const;
719
720 void SetProtoIdx(uint32_t new_proto_idx) {
721 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700722 }
723
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700724 ObjectArray<String>* GetDexCacheStrings() const;
725 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
726
727 static MemberOffset DexCacheStringsOffset() {
728 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
729 }
730
buzbee2a475e72011-09-07 17:19:17 -0700731 static MemberOffset DexCacheResolvedTypesOffset() {
732 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
733 }
734
buzbee34cd9e52011-09-08 14:31:52 -0700735 static MemberOffset DexCacheResolvedFieldsOffset() {
736 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
737 }
738
buzbee1da522d2011-09-04 11:22:20 -0700739 static MemberOffset DexCacheInitializedStaticStorageOffset() {
740 return OFFSET_OF_OBJECT_MEMBER(Method,
741 dex_cache_initialized_static_storage_);
742 }
743
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700744 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
745 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
746
747 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
748 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
749
750 ObjectArray<Field>* GetDexCacheResolvedFields() const;
751 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
752
753 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
754 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
755
756 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
757 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
758
759 void SetReturnTypeIdx(uint32_t new_return_type_idx);
760
761 Class* GetReturnType() const;
762
763 bool IsReturnAReference() const;
764
765 bool IsReturnAFloat() const;
766
767 bool IsReturnADouble() const;
768
Ian Rogersb033c752011-07-20 12:22:35 -0700769 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700770 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700771 }
772
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700773 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700774
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700775 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700776
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700777 // "Args" may refer to any of the 3 levels of "Args."
778 // To avoid confusion, our code will denote which "Args" clearly:
779 // 1. UserArgs: Args that a user see.
780 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
781 // receiver.
782 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
783 // E.g., the first in Args is Method* for both static and non-static
784 // methods. And CConvArgs doesn't deal with the receiver because
785 // receiver is hardwired in an implicit register, so CConvArgs doesn't
786 // need to deal with it.
787 //
788 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700789 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700790
791 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700792 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700793 size_t NumReferenceArgs() const;
794
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700795 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700796 size_t NumLongOrDoubleArgs() const;
797
Ian Rogersb033c752011-07-20 12:22:35 -0700798 // Is the given method parameter a reference?
799 bool IsParamAReference(unsigned int param) const;
800
801 // Is the given method parameter a long or double?
802 bool IsParamALongOrDouble(unsigned int param) const;
803
Ian Rogersdf20fe02011-07-20 20:34:16 -0700804 // Size in bytes of the given parameter
805 size_t ParamSize(unsigned int param) const;
806
807 // Size in bytes of the return value
808 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700809
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700810 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
811
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700812 const ByteArray* GetCodeArray() const {
813 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
814 }
815
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700816 const void* GetCode() const {
817 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700818 }
819
Brian Carlstrome24fa612011-09-29 00:53:55 -0700820 void SetCode(void* code) {
821 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
822 }
823
824 uint32_t GetOatCodeOffset() const {
825 CHECK(!Runtime::Current()->IsStarted());
826 return reinterpret_cast<uint32_t>(GetCode());
827 }
828
829 void SetOatCodeOffset(uint32_t code_offset) {
830 CHECK(!Runtime::Current()->IsStarted());
831 SetCode(reinterpret_cast<void*>(code_offset));
832 }
833
834 void SetCodeArray(ByteArray* code_array, InstructionSet instruction_set);
buzbeec143c552011-08-20 17:38:58 -0700835
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700836 static MemberOffset GetCodeOffset() {
837 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700838 }
839
Ian Rogersbdb03912011-09-14 00:55:44 -0700840 // Is the given PC within the code array?
841 bool IsWithinCode(uintptr_t pc) const;
842
843 IntArray* GetMappingTable() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700844 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
845 }
846
847 void SetMappingTable(IntArray* mapping_table) {
848 SetFieldPtr<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), mapping_table, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700849 }
850
buzbeec41e5b52011-09-23 12:46:19 -0700851 ShortArray* GetVMapTable() const {
Ian Rogersd6b1f612011-09-27 13:38:14 -0700852 return GetFieldObject<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700853 }
854
Brian Carlstrome24fa612011-09-29 00:53:55 -0700855 void SetVMapTable(ShortArray* vmap_table) {
856 SetFieldPtr<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
857 }
858
Shih-wei Liaod11af152011-08-23 16:02:11 -0700859 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 DCHECK(sizeof(size_t) == sizeof(uint32_t));
861 size_t result = GetField32(
862 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
863 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
864 return result;
865 }
866
867 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
868 DCHECK(sizeof(size_t) == sizeof(uint32_t));
869 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
870 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
871 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700872 }
873
Shih-wei Liaod11af152011-08-23 16:02:11 -0700874 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700875 DCHECK(sizeof(size_t) == sizeof(uint32_t));
876 return GetField32(
877 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700878 }
879
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700880 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
881 DCHECK(sizeof(size_t) == sizeof(uint32_t));
882 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
883 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
884 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700885 }
886
Brian Carlstrom16192862011-09-12 17:50:06 -0700887 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700888
Brian Carlstrom16192862011-09-12 17:50:06 -0700889 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700890
Brian Carlstrom16192862011-09-12 17:50:06 -0700891 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700892
Ian Rogersb033c752011-07-20 12:22:35 -0700893 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700894 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700895 }
896
Brian Carlstrom78128a62011-09-15 17:21:19 -0700897 const void* GetNativeMethod() const {
898 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
899 }
900
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700901 ByteArray* GetInvokeStubArray() const {
902 ByteArray* result = GetFieldPtr<ByteArray*>(
903 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
904 // TODO: DCHECK(result != NULL); should be ahead of time compiled
905 return result;
906 }
907
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700908 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700909 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700910 InvokeStub* result = GetFieldPtr<InvokeStub*>(
911 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
912 // TODO: DCHECK(result != NULL); should be ahead of time compiled
913 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700914 }
915
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700916 static MemberOffset GetInvokeStubOffset() {
917 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700918 }
919
buzbee561227c2011-09-02 15:28:19 -0700920 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
921 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
922 }
923
924 static MemberOffset GetDexCacheResolvedMethodsOffset() {
925 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
926 }
927
928 static MemberOffset GetMethodIndexOffset() {
929 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
930 }
931
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700932 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700933
Ian Rogers90865722011-09-19 11:11:44 -0700934 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700935 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700936 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700937
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938 void SetCoreSpillMask(uint32_t core_spill_mask) {
939 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700940 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700941 }
942
Ian Rogers90865722011-09-19 11:11:44 -0700943 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700944 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
945 }
946
947 void SetFpSpillMask(uint32_t fp_spill_mask) {
948 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700949 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700950 }
951
Ian Rogers90865722011-09-19 11:11:44 -0700952 // Is this a hand crafted method used for something like describing callee saves?
953 bool IsPhony() const {
Ian Rogersff1ed472011-09-20 13:46:24 -0700954 bool result = this == Runtime::Current()->GetCalleeSaveMethod();
Ian Rogers90865722011-09-19 11:11:44 -0700955 // Check that if we do think it is phony it looks like the callee save method
956 DCHECK(!result || GetCoreSpillMask() != 0);
957 return result;
958 }
959
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700960 // Converts a native PC to a dex PC. TODO: this is a no-op
961 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700962 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700963
964 // Converts a dex PC to a native PC. TODO: this is a no-op
965 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700966 uintptr_t ToNativePC(const uint32_t dex_pc) const;
967
968 // Find the catch block for the given exception type and dex_pc
969 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700970
Elliott Hughes80609252011-09-23 17:24:51 -0700971 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
972
973 static Class* GetConstructorClass() {
974 return java_lang_reflect_Constructor_;
975 }
976
977 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700978 return java_lang_reflect_Method_;
979 }
980
Elliott Hughes80609252011-09-23 17:24:51 -0700981 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700982
Elliott Hughes418d20f2011-09-22 14:00:39 -0700983 void InitJavaFields();
984
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700985 private:
986 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700987 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700988
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700989 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
990 // the class we are a part of
991 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700992 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700993 Object* java_formal_type_parameters_;
994 Object* java_generic_exception_types_;
995 Object* java_generic_parameter_types_;
996 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700997
Brian Carlstrom693267a2011-09-06 09:25:34 -0700998 String* name_;
999
Elliott Hughes418d20f2011-09-22 14:00:39 -07001000 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001001 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001002 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001003
Brian Carlstrom693267a2011-09-06 09:25:34 -07001004 // Storage for code_
1005 const ByteArray* code_array_;
1006
1007 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001008 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1009
1010 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1011 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1012
1013 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1014 ObjectArray<Field>* dex_cache_resolved_fields_;
1015
1016 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1017 ObjectArray<Method>* dex_cache_resolved_methods_;
1018
Brian Carlstromdbc05252011-09-09 01:59:59 -07001019 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1020 ObjectArray<Class>* dex_cache_resolved_types_;
1021
1022 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1023 ObjectArray<String>* dex_cache_strings_;
1024
1025 // Storage for invoke_stub_
1026 const ByteArray* invoke_stub_array_;
1027
1028 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -07001029 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001030
jeffhaoe23d93c2011-09-15 14:48:43 -07001031 // Byte arrays that hold data for the register maps
1032 const ByteArray* register_map_data_;
1033 const ByteArray* register_map_header_;
1034
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001035 // The short-form method descriptor string.
1036 String* shorty_;
1037
Brian Carlstromdbc05252011-09-09 01:59:59 -07001038 // The method descriptor. This represents the parameters a method
1039 // takes and value it returns. This string is a list of the type
1040 // descriptors for the parameters enclosed in parenthesis followed
1041 // by the return type descriptor. For example, for the method
1042 //
1043 // Object mymethod(int i, double d, Thread t)
1044 //
1045 // the method descriptor would be
1046 //
1047 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1048 String* signature_;
1049
buzbeec41e5b52011-09-23 12:46:19 -07001050 // Storage for Dalvik virtual register mapping_table_
1051 ShortArray* vmap_table_;
1052
Brian Carlstromdbc05252011-09-09 01:59:59 -07001053 uint32_t java_generic_types_are_initialized_;
1054
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001055 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001056 uint32_t access_flags_;
1057
1058 // Compiled code associated with this method for callers from managed code.
1059 // May be compiled managed code or a bridge for invoking a native method.
1060 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001061
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001062 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001063 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001064
Brian Carlstrom693267a2011-09-06 09:25:34 -07001065 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001066 uint32_t core_spill_mask_;
1067
1068 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001069 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001070
Brian Carlstrom693267a2011-09-06 09:25:34 -07001071 // Total size in bytes of the frame
1072 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001073
Brian Carlstrom693267a2011-09-06 09:25:34 -07001074 // Native invocation stub entry point for calling from native to managed code.
1075 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001076
Brian Carlstrom693267a2011-09-06 09:25:34 -07001077 // Index of the return type
1078 uint32_t java_return_type_idx_;
1079
Brian Carlstrom693267a2011-09-06 09:25:34 -07001080 // For concrete virtual methods, this is the offset of the method
1081 // in Class::vtable_.
1082 //
1083 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001084 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001085 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001086
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001087 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001088 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001089
Brian Carlstrom693267a2011-09-06 09:25:34 -07001090 // Method bounds; not needed for an abstract method.
1091 //
1092 // For a native method, we compute the size of the argument list, and
1093 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001094 uint32_t num_ins_;
1095 uint32_t num_outs_;
1096 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001097
Brian Carlstrom693267a2011-09-06 09:25:34 -07001098 // Method prototype descriptor string (return and argument types).
1099 uint32_t proto_idx_;
1100
1101 // Offset of return PC within frame for compiled code (in bytes)
1102 size_t return_pc_offset_in_bytes_;
1103
Brian Carlstrom693267a2011-09-06 09:25:34 -07001104 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001105
Elliott Hughes80609252011-09-23 17:24:51 -07001106 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001107 static Class* java_lang_reflect_Method_;
1108
Brian Carlstrom693267a2011-09-06 09:25:34 -07001109 friend class ImageWriter; // for relocating code_ and invoke_stub_
1110 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001111 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001112};
1113
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001114class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001115 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001116 static size_t SizeOf(size_t component_count,
1117 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001118 return sizeof(Array) + component_count * component_size;
1119 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001120
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001121 // A convenience for code that doesn't know the component size,
1122 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001123 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001124
Brian Carlstromb63ec392011-08-27 17:38:27 -07001125 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001126
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001127 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001128
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001129 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001130 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001131 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001132
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001133 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001134 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001136 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001137
buzbeec143c552011-08-20 17:38:58 -07001138 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001139 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001140 }
1141
1142 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001144 }
1145
Elliott Hughesbf86d042011-08-31 17:53:14 -07001146 void* GetRawData() {
1147 return reinterpret_cast<void*>(first_element_);
1148 }
1149
Elliott Hughes289da822011-08-16 10:11:20 -07001150 protected:
1151 bool IsValidIndex(int32_t index) const {
1152 if (index < 0 || index >= length_) {
Elliott Hughes80609252011-09-23 17:24:51 -07001153 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001154 }
1155 return true;
1156 }
1157
Elliott Hughes80609252011-09-23 17:24:51 -07001158 protected:
1159 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1160 bool ThrowArrayStoreException(Object* object) const;
1161
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001162 private:
1163 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001164 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001165 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1166 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001167 // Marker for the data (used by generated code)
1168 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001169
Carl Shapirof88c9522011-08-06 15:47:38 -07001170 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001171};
1172
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001173template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001174class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001175 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001176 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001177
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001179
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001181
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001182 // Set element without bound and element type checks, to be used in limited
1183 // circumstances, such as during boot image writing
1184 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001185
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001186 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001187 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001188 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001189
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001190 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001191
1192 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001193 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001194};
1195
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001196template<class T>
1197ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1198 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1199}
1200
1201template<class T>
1202T* ObjectArray<T>::Get(int32_t i) const {
1203 if (!IsValidIndex(i)) {
1204 return NULL;
1205 }
1206 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1207 return GetFieldObject<T*>(data_offset, false);
1208}
1209
1210template<class T>
1211ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1212 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1213 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1214 return new_array;
1215}
1216
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001217// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001218// provides the static storage. However, this might change to an Array
1219// to improve image sharing, so we use this type to avoid assumptions
1220// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001221class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001222
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001223// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001224class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001225 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001226
1227 // Class Status
1228 //
1229 // kStatusNotReady: If a Class cannot be found in the class table by
1230 // FindClass, it allocates an new one with AllocClass in the
1231 // kStatusNotReady and calls LoadClass. Note if it does find a
1232 // class, it may not be kStatusResolved and it will try to push it
1233 // forward toward kStatusResolved.
1234 //
1235 // kStatusIdx: LoadClass populates with Class with information from
1236 // the DexFile, moving the status to kStatusIdx, indicating that the
1237 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001238 // populated based on super_class_type_idx_ and
1239 // interfaces_type_idx_. The new Class can then be inserted into the
1240 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001241 //
1242 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1243 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1244 // using ResolveClass to initialize the super_class_ and interfaces_.
1245 //
1246 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001247 // shows linking is complete and fields of the Class populated by making
1248 // it kStatusResolved. Java allows circularities of the form where a super
1249 // class has a field that is of the type of the sub class. We need to be able
1250 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001251
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001252 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001253 kStatusError = -1,
1254 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001255 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001256 kStatusLoaded = 2, // DEX idx values resolved
1257 kStatusResolved = 3, // part of linking
1258 kStatusVerifying = 4, // in the process of being verified
1259 kStatusVerified = 5, // logically part of linking; done pre-init
1260 kStatusInitializing = 6, // class init in progress
1261 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001262 };
1263
1264 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001265 kPrimNot = 0,
1266 kPrimBoolean,
1267 kPrimByte,
1268 kPrimChar,
1269 kPrimShort,
1270 kPrimInt,
1271 kPrimLong,
1272 kPrimFloat,
1273 kPrimDouble,
1274 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001275 };
1276
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001277 Status GetStatus() const {
1278 CHECK(sizeof(Status) == sizeof(uint32_t));
1279 return static_cast<Status>(
1280 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1281 }
1282
1283 void SetStatus(Status new_status);
1284
1285 // Returns true if the class has failed to link.
1286 bool IsErroneous() const {
1287 return GetStatus() == kStatusError;
1288 }
1289
1290 // Returns true if the class has been loaded.
1291 bool IsIdxLoaded() const {
1292 return GetStatus() >= kStatusIdx;
1293 }
1294
1295 // Returns true if the class has been loaded.
1296 bool IsLoaded() const {
1297 return GetStatus() >= kStatusLoaded;
1298 }
1299
1300 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001301 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001302 return GetStatus() >= kStatusResolved;
1303 }
1304
1305 // Returns true if the class has been verified.
1306 bool IsVerified() const {
1307 return GetStatus() >= kStatusVerified;
1308 }
1309
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001310 // Returns true if the class is initializing.
1311 bool IsInitializing() const {
1312 return GetStatus() >= kStatusInitializing;
1313 }
1314
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001315 // Returns true if the class is initialized.
1316 bool IsInitialized() const {
1317 return GetStatus() == kStatusInitialized;
1318 }
1319
1320 uint32_t GetAccessFlags() const;
1321
1322 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001323 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 }
1325
1326 // Returns true if the class is an interface.
1327 bool IsInterface() const {
1328 return (GetAccessFlags() & kAccInterface) != 0;
1329 }
1330
1331 // Returns true if the class is declared public.
1332 bool IsPublic() const {
1333 return (GetAccessFlags() & kAccPublic) != 0;
1334 }
1335
1336 // Returns true if the class is declared final.
1337 bool IsFinal() const {
1338 return (GetAccessFlags() & kAccFinal) != 0;
1339 }
1340
1341 // Returns true if the class is abstract.
1342 bool IsAbstract() const {
1343 return (GetAccessFlags() & kAccAbstract) != 0;
1344 }
1345
1346 // Returns true if the class is an annotation.
1347 bool IsAnnotation() const {
1348 return (GetAccessFlags() & kAccAnnotation) != 0;
1349 }
1350
1351 // Returns true if the class is synthetic.
1352 bool IsSynthetic() const {
1353 return (GetAccessFlags() & kAccSynthetic) != 0;
1354 }
1355
1356 bool IsReferenceClass() const {
1357 return (GetAccessFlags() & kAccClassIsReference) != 0;
1358 }
1359
1360 bool IsWeakReferenceClass() const {
1361 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1362 }
1363
1364 bool IsSoftReferenceClass() const {
1365 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1366 }
1367
1368 bool IsFinalizerReferenceClass() const {
1369 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1370 }
1371
1372 bool IsPhantomReferenceClass() const {
1373 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1374 }
1375
1376 PrimitiveType GetPrimitiveType() const {
1377 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1378 return static_cast<PrimitiveType>(
1379 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1380 }
1381
1382 void SetPrimitiveType(PrimitiveType new_type) {
1383 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001384 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001385 }
1386
1387 // Returns true if the class is a primitive type.
1388 bool IsPrimitive() const {
1389 return GetPrimitiveType() != kPrimNot;
1390 }
1391
1392 bool IsPrimitiveBoolean() const {
1393 return GetPrimitiveType() == kPrimBoolean;
1394 }
1395
1396 bool IsPrimitiveByte() const {
1397 return GetPrimitiveType() == kPrimByte;
1398 }
1399
1400 bool IsPrimitiveChar() const {
1401 return GetPrimitiveType() == kPrimChar;
1402 }
1403
1404 bool IsPrimitiveShort() const {
1405 return GetPrimitiveType() == kPrimShort;
1406 }
1407
1408 bool IsPrimitiveInt() const {
1409 return GetPrimitiveType() == kPrimInt;
1410 }
1411
1412 bool IsPrimitiveLong() const {
1413 return GetPrimitiveType() == kPrimLong;
1414 }
1415
1416 bool IsPrimitiveFloat() const {
1417 return GetPrimitiveType() == kPrimFloat;
1418 }
1419
1420 bool IsPrimitiveDouble() const {
1421 return GetPrimitiveType() == kPrimDouble;
1422 }
1423
1424 bool IsPrimitiveVoid() const {
1425 return GetPrimitiveType() == kPrimVoid;
1426 }
1427
1428 size_t PrimitiveSize() const;
1429
1430 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001431 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432 }
1433
1434 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 return GetFieldObject<Class*>(
1436 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1437 }
1438
1439 void SetComponentType(Class* new_component_type) {
1440 DCHECK(GetComponentType() == NULL);
1441 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001442 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443 }
1444
1445 size_t GetComponentSize() const {
1446 return GetTypeSize(GetComponentType()->GetDescriptor());
1447 }
1448
1449 bool IsObjectClass() const {
1450 return !IsPrimitive() && GetSuperClass() == NULL;
1451 }
1452
Brian Carlstrom1f870082011-08-23 16:02:11 -07001453 // Creates a raw object instance but does not invoke the default constructor.
1454 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001455
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001456 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001457
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001458 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 const String* result = GetFieldObject<const String*>(
1460 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1461 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1462 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1463 return result;
1464 }
1465
1466 void SetDescriptor(String* new_descriptor);
1467
1468 bool IsVariableSize() const {
1469 // Classes and arrays vary in size, and so the object_size_ field cannot
1470 // be used to get their instance size
1471 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 }
1473
Brian Carlstrom4873d462011-08-21 15:23:39 -07001474 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475 CHECK(sizeof(size_t) == sizeof(int32_t));
1476 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001477 }
1478
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001479 size_t GetClassSize() const {
1480 CHECK(sizeof(size_t) == sizeof(uint32_t));
1481 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001482 }
1483
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001484 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001485 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001486 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001487 << " new_class_size=" << new_class_size
1488 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001489 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001490 }
1491
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001492 size_t GetObjectSize() const {
1493 CHECK(!IsVariableSize());
1494 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001495 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 CHECK_GE(result, sizeof(Object));
1497 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498 }
1499
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001500 void SetObjectSize(size_t new_object_size) {
1501 DCHECK(!IsVariableSize());
1502 CHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001503 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001504 }
1505
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001506 // Returns true if this class is in the same packages as that class.
1507 bool IsInSamePackage(const Class* that) const;
1508
Brian Carlstrome24fa612011-09-29 00:53:55 -07001509 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001510
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001511 // Returns true if this class can access that class.
1512 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001513 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001514 }
1515
jeffhao4a801a42011-09-23 13:53:40 -07001516 // Validate method/field access.
1517 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1518 // quick accept for public access
1519 if (member_flags & kAccPublic) {
1520 return true;
1521 }
1522
1523 // quick accept for access from same class
1524 if (this == access_to) {
1525 return true;
1526 }
1527
1528 // quick reject for private access from another class
1529 if (member_flags & kAccPrivate) {
1530 return false;
1531 }
1532
1533 // Semi-quick test for protected access from a sub-class, which may or
1534 // may not be in the same package.
1535 if (member_flags & kAccProtected) {
1536 if (this->IsSubClass(access_to)) {
1537 return true;
1538 }
1539 }
1540
1541 // Allow protected and private access from other classes in the same package.
1542 return this->IsInSamePackage(access_to);
1543 }
1544
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001545 bool IsSubClass(const Class* klass) const;
1546
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001547 bool IsAssignableFrom(const Class* src) const {
1548 DCHECK(src != NULL);
1549 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001550 // Can always assign to things of the same type
1551 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001552 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001553 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001554 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001555 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001556 return src->Implements(this);
1557 } else if (src->IsArrayClass()) {
1558 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001559 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001560 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001561 }
1562 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001563
buzbee991e3ac2011-09-29 15:44:22 -07001564 // Assignable test for code, won't throw. Null and equality tests already performed
1565 static uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class)
1566 {
1567 DCHECK(klass != NULL);
1568 DCHECK(ref_class != NULL);
1569 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1570 }
1571
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001572 Class* GetSuperClass() const {
1573 // Can only get super class for loaded classes (hack for when runtime is
1574 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001575 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001576 return GetFieldObject<Class*>(
1577 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1578 }
1579
buzbee4a3164f2011-09-03 11:25:10 -07001580 static MemberOffset SuperClassOffset() {
1581 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1582 }
1583
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001584 void SetSuperClass(Class *new_super_class) {
1585 // super class is assigned once, except during class linker initialization
1586 Class* old_super_class = GetFieldObject<Class*>(
1587 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1588 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1589 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001590 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001591 }
1592
1593 bool HasSuperClass() const {
1594 return GetSuperClass() != NULL;
1595 }
1596
1597 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001598 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001599 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1600 false);
1601 }
1602
1603 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001604 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001605 }
1606
1607 const ClassLoader* GetClassLoader() const;
1608
1609 void SetClassLoader(const ClassLoader* new_cl);
1610
1611 static MemberOffset DexCacheOffset() {
1612 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1613 }
1614
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001615 enum {
1616 kDumpClassFullDetail = 1,
1617 kDumpClassClassLoader = (1 << 1),
1618 kDumpClassInitialized = (1 << 2),
1619 };
1620
Elliott Hughes4681c802011-09-25 18:04:37 -07001621 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001622
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001623 DexCache* GetDexCache() const;
1624
1625 void SetDexCache(DexCache* new_dex_cache);
1626
1627 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001628 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001629 return GetFieldObject<ObjectArray<Method>*>(
1630 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1631 }
1632
1633 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1634 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1635 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1636 DCHECK_NE(0, new_direct_methods->GetLength());
1637 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1638 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001639 }
1640
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001641 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001642 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001643 }
1644
1645 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 ObjectArray<Method>* direct_methods =
1647 GetFieldObject<ObjectArray<Method>*>(
1648 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1649 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001650 }
1651
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 // Returns the number of static, private, and constructor methods.
1653 size_t NumDirectMethods() const {
1654 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1655 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001656
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001657 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001658 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001659 return GetFieldObject<ObjectArray<Method>*>(
1660 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1661 }
1662
1663 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1664 // TODO: we reassign virtual methods to grow the table for miranda
1665 // methods.. they should really just be assigned once
1666 DCHECK_NE(0, new_virtual_methods->GetLength());
1667 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1668 new_virtual_methods, false);
1669 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001670
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001671 // Returns the number of non-inherited virtual methods.
1672 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001673 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001674 }
1675
1676 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001677 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001678 return GetVirtualMethods()->Get(i);
1679 }
1680
1681 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001682 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001683 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001684 }
1685
1686 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001687 ObjectArray<Method>* virtual_methods =
1688 GetFieldObject<ObjectArray<Method>*>(
1689 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1690 virtual_methods->Set(i, f);
1691 }
1692
1693 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001694 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001695 return GetFieldObject<ObjectArray<Method>*>(
1696 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1697 }
1698
1699 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001700 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001701 return GetFieldObject<ObjectArray<Method>*>(
1702 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1703 }
1704
1705 void SetVTable(ObjectArray<Method>* new_vtable) {
1706 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1707 }
1708
1709 static MemberOffset VTableOffset() {
1710 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001711 }
1712
Brian Carlstrom30b94452011-08-25 21:35:26 -07001713 // Given a method implemented by this class but potentially from a
1714 // super class, return the specific implementation
1715 // method for this class.
1716 Method* FindVirtualMethodForVirtual(Method* method) {
1717 DCHECK(!method->GetDeclaringClass()->IsInterface());
1718 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001719 // Use the index to a potentially overridden one for this instance's class.
1720 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001721 }
1722
1723 // Given a method implemented by this class, but potentially from a
1724 // super class or interface, return the specific implementation
1725 // method for this class.
1726 Method* FindVirtualMethodForInterface(Method* method);
1727
jeffhaobdb76512011-09-07 11:43:16 -07001728 Method* FindInterfaceMethod(const StringPiece& name,
1729 const StringPiece& descriptor);
1730
Brian Carlstrom30b94452011-08-25 21:35:26 -07001731 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001732 if (method->IsDirect()) {
1733 return method;
1734 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001735 if (method->GetDeclaringClass()->IsInterface()) {
1736 return FindVirtualMethodForInterface(method);
1737 }
1738 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001739 }
1740
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001741 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001742 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001743
1744 Method* FindVirtualMethod(const StringPiece& name,
1745 const StringPiece& descriptor);
1746
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747 Method* FindDeclaredDirectMethod(const StringPiece& name,
1748 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001749
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750 Method* FindDirectMethod(const StringPiece& name,
1751 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001752
Carl Shapiro69759ea2011-07-21 18:13:35 -07001753 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001754 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001755 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1756 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1757 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001758 }
1759
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001760 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001761 CHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001762 return GetFieldObject<IntArray*>(
1763 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1764 }
1765
1766 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1767
1768 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001769 CHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001770 return GetFieldObject<ObjectArray<Class>*>(
1771 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1772 }
1773
1774 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1775 DCHECK(NULL == GetFieldObject<Object*>(
1776 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001777 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001778 }
1779
1780 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001781 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 ObjectArray<Class>* interfaces =
1783 GetFieldObject<ObjectArray<Class>*>(
1784 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1785 interfaces->Set(i, f);
1786 }
1787
1788 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001789 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 return GetInterfaces()->Get(i);
1791 }
1792
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001793 int32_t GetIfTableCount() const {
1794 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1795 if (iftable == NULL) {
1796 return 0;
1797 }
1798 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001799 }
1800
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001801 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001803 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1805 }
1806
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001807 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001808 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001809 }
1810
1811 // Get instance fields
1812 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001813 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001814 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001815 }
1816
1817 void SetIFields(ObjectArray<Field>* new_ifields) {
1818 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1819 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001820 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001821 }
1822
1823 size_t NumInstanceFields() const {
1824 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1825 }
1826
1827 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1828 DCHECK_NE(NumInstanceFields(), 0U);
1829 return GetIFields()->Get(i);
1830 }
1831
1832 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1833 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1834 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1835 ifields->Set(i, f);
1836 }
1837
1838 // Returns the number of instance fields containing reference types.
1839 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001840 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001841 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001842 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001843 }
1844
1845 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001846 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001847 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001848 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001849 }
1850
1851 void SetNumReferenceInstanceFields(size_t new_num) {
1852 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001853 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001854 }
1855
1856 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001857 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001858 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001859 }
1860
1861 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1862
1863 // Beginning of static field data
1864 static MemberOffset FieldsOffset() {
1865 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1866 }
1867
1868 // Returns the number of static fields containing reference types.
1869 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001870 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001871 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001872 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 }
1874
1875 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001876 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001877 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001878 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001879 }
1880
1881 void SetNumReferenceStaticFields(size_t new_num) {
1882 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001883 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884 }
1885
1886 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001887 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001888 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 }
1890
1891 void SetSFields(ObjectArray<Field>* new_sfields) {
1892 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1893 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001894 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001895 }
1896
1897 size_t NumStaticFields() const {
1898 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1899 }
1900
1901 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1902 return GetSFields()->Get(i);
1903 }
1904
1905 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1906 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1907 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1908 sfields->Set(i, f);
1909 }
1910
1911 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001912 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001913 }
1914
1915 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1916
1917 // Finds the given instance field in this class or a superclass.
1918 Field* FindInstanceField(const StringPiece& name, Class* type);
1919
1920 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1921
1922 // Finds the given static field in this class or a superclass.
1923 Field* FindStaticField(const StringPiece& name, Class* type);
1924
1925 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1926
Elliott Hughesdcc24742011-09-07 14:02:44 -07001927 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001928 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001929 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1930 }
1931
Elliott Hughesdcc24742011-09-07 14:02:44 -07001932 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001933 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001934 }
1935
1936 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001937 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001938 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001939 }
1940
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001941 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001942 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001943 }
1944
Brian Carlstromc74255f2011-09-11 22:47:39 -07001945 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001946
Brian Carlstromc74255f2011-09-11 22:47:39 -07001947 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001948
jeffhaobdb76512011-09-07 11:43:16 -07001949 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001950 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001951 bool IsArrayAssignableFromArray(const Class* klass) const;
1952 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001953
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001954 // descriptor for the class such as "java.lang.Class" or "[C"
1955 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001956
Brian Carlstrom693267a2011-09-06 09:25:34 -07001957 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001958 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001959
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001960 // For array classes, the component class object for instanceof/checkcast
1961 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001962 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001963
Brian Carlstrom693267a2011-09-06 09:25:34 -07001964 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1965 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001966
Brian Carlstrom693267a2011-09-06 09:25:34 -07001967 // DexCache of resolved constant pool entries
1968 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1969 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001970
1971 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001972 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001973
Brian Carlstrom693267a2011-09-06 09:25:34 -07001974 // instance fields
1975 //
1976 // These describe the layout of the contents of an Object.
1977 // Note that only the fields directly declared by this class are
1978 // listed in ifields; fields declared by a superclass are listed in
1979 // the superclass's Class.ifields.
1980 //
1981 // All instance fields that refer to objects are guaranteed to be at
1982 // the beginning of the field list. num_reference_instance_fields_
1983 // specifies the number of reference fields.
1984 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001985
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001986 // Interface table (iftable_), one entry per interface supported by
1987 // this class. That means one entry for each interface we support
1988 // directly, indirectly via superclass, or indirectly via
1989 // superinterface. This will be null if neither we nor our
1990 // superclass implement any interfaces.
1991 //
1992 // Why we need this: given "class Foo implements Face", declare
1993 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1994 // is part of the Face interface. We can't easily use a single
1995 // vtable.
1996 //
1997 // For every interface a concrete class implements, we create an array
1998 // of the concrete vtable_ methods for the methods in the interface.
1999 ObjectArray<InterfaceEntry>* iftable_;
2000
Brian Carlstromdbc05252011-09-09 01:59:59 -07002001 // array of interfaces this class implements directly
2002 // see also interfaces_type_idx_
2003 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002004
2005 // array of type_idx's for interfaces this class implements directly
2006 // see also interfaces_
2007 IntArray* interfaces_type_idx_;
2008
Brian Carlstromdbc05252011-09-09 01:59:59 -07002009 // Static fields
2010 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002011
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002012 // source file name, if known. Otherwise, NULL.
2013 String* source_file_;
2014
Brian Carlstromdbc05252011-09-09 01:59:59 -07002015 // The superclass, or NULL if this is java.lang.Object or a
2016 // primitive type.
2017 // see also super_class_type_idx_;
2018 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002019
Brian Carlstromdbc05252011-09-09 01:59:59 -07002020 // If class verify fails, we must return same error on subsequent tries.
2021 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2022 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002023
Brian Carlstromdbc05252011-09-09 01:59:59 -07002024 // virtual methods defined in this class; invoked through vtable
2025 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002026
Brian Carlstromdbc05252011-09-09 01:59:59 -07002027 // Virtual method table (vtable), for use by "invoke-virtual". The
2028 // vtable from the superclass is copied in, and virtual methods from
2029 // our class either replace those from the super or are appended.
2030 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002031
Brian Carlstromdbc05252011-09-09 01:59:59 -07002032 // access flags; low 16 bits are defined by VM spec
2033 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002034
Jesse Wilson6bf19152011-09-29 13:12:33 -04002035 // Total size of the Class instance; used when allocating storage on gc heap.
2036 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002037 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002038
Elliott Hughes5f791332011-09-15 17:45:30 -07002039 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002040 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002041
Brian Carlstromdbc05252011-09-09 01:59:59 -07002042 // number of instance fields that are object refs
2043 size_t num_reference_instance_fields_;
2044
2045 // number of static fields that are object refs
2046 size_t num_reference_static_fields_;
2047
2048 // Total object size; used when allocating storage on gc heap.
2049 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002050 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002051 size_t object_size_;
2052
2053 // primitive type index, or kPrimNot (0); set for generated prim classes
2054 PrimitiveType primitive_type_;
2055
2056 // Bitmap of offsets of ifields.
2057 uint32_t reference_instance_offsets_;
2058
2059 // Bitmap of offsets of sfields.
2060 uint32_t reference_static_offsets_;
2061
Brian Carlstrom693267a2011-09-06 09:25:34 -07002062 // state of class initialization
2063 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002064
Brian Carlstrom693267a2011-09-06 09:25:34 -07002065 // Set in LoadClass, used to LinkClass
2066 // see also super_class_
2067 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002068
Brian Carlstrom693267a2011-09-06 09:25:34 -07002069 // TODO: ?
2070 // initiating class loader list
2071 // NOTE: for classes with low serialNumber, these are unused, and the
2072 // values are kept in a table in gDvm.
2073 // InitiatingLoaderList initiating_loader_list_;
2074
Brian Carlstrom4873d462011-08-21 15:23:39 -07002075 // Location of first static field.
2076 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002077
Brian Carlstrom693267a2011-09-06 09:25:34 -07002078 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002079 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002080};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002081
Elliott Hughes1f359b02011-07-17 14:27:17 -07002082std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002083
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002084inline void Object::SetClass(Class* new_klass) {
2085 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002086 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002087}
2088
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002089inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002090 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 DCHECK(GetClass() != NULL);
2092 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002093}
2094
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002095inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002096 Class* java_lang_Class = GetClass()->GetClass();
2097 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002098}
2099
Brian Carlstrom4873d462011-08-21 15:23:39 -07002100inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002101 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002102 return this == java_lang_Class;
2103}
2104
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002105inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002106 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002107}
2108
Brian Carlstromb63ec392011-08-27 17:38:27 -07002109inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002110 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002111}
2112
Brian Carlstroma663ea52011-08-19 23:33:41 -07002113inline bool Object::IsField() const {
2114 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002115 Class* java_lang_reflect_Field =
2116 java_lang_Class->GetInstanceField(0)->GetClass();
2117 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002118}
2119
2120inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002121 Class* c = GetClass();
2122 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002123}
2124
2125inline bool Object::IsReferenceInstance() const {
2126 return GetClass()->IsReferenceClass();
2127}
2128
2129inline bool Object::IsWeakReferenceInstance() const {
2130 return GetClass()->IsWeakReferenceClass();
2131}
2132
2133inline bool Object::IsSoftReferenceInstance() const {
2134 return GetClass()->IsSoftReferenceClass();
2135}
2136
2137inline bool Object::IsFinalizerReferenceInstance() const {
2138 return GetClass()->IsFinalizerReferenceClass();
2139}
2140
2141inline bool Object::IsPhantomReferenceInstance() const {
2142 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002143}
2144
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002145inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002146 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002147 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002148 result = AsArray()->SizeOf();
2149 } else if (IsClass()) {
2150 result = AsClass()->SizeOf();
2151 } else {
2152 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002153 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002154 DCHECK(!IsField() || result == sizeof(Field));
2155 DCHECK(!IsMethod() || result == sizeof(Method));
2156 return result;
2157}
2158
2159inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002160 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002161 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002162 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2163 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002164 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002165 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2166 num_bytes.Uint32Value(), false);
2167}
2168
2169inline Class* Field::GetDeclaringClass() const {
2170 Class* result = GetFieldObject<Class*>(
2171 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2172 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002173 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002174 return result;
2175}
2176
2177inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2178 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2179 new_declaring_class, false);
2180}
2181
2182inline Class* Method::GetDeclaringClass() const {
2183 Class* result =
2184 GetFieldObject<Class*>(
2185 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2186 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002187 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002188 return result;
2189}
2190
2191inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2192 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2193 new_declaring_class, false);
2194}
2195
2196inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002197 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002198 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2199 false);
2200}
2201
2202inline bool Method::IsReturnAReference() const {
2203 return !GetReturnType()->IsPrimitive();
2204}
2205
2206inline bool Method::IsReturnAFloat() const {
2207 return GetReturnType()->IsPrimitiveFloat();
2208}
2209
2210inline bool Method::IsReturnADouble() const {
2211 return GetReturnType()->IsPrimitiveDouble();
2212}
2213
2214inline bool Method::IsReturnALong() const {
2215 return GetReturnType()->IsPrimitiveLong();
2216}
2217
2218inline bool Method::IsReturnVoid() const {
2219 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002220}
2221
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002222inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002223 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2224}
2225
2226template<class T>
2227void ObjectArray<T>::Set(int32_t i, T* object) {
2228 if (IsValidIndex(i)) {
2229 if (object != NULL) {
2230 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002231 if (!object->InstanceOf(element_class)) {
2232 ThrowArrayStoreException(object);
2233 return;
2234 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002235 }
2236 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2237 SetFieldObject(data_offset, object, false);
2238 }
2239}
2240
2241template<class T>
2242void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2243 DCHECK(IsValidIndex(i));
2244 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2245 SetFieldObject(data_offset, object, false);
2246}
2247
2248template<class T>
2249void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2250 ObjectArray<T>* dst, int dst_pos,
2251 size_t length) {
2252 if (src->IsValidIndex(src_pos) &&
2253 src->IsValidIndex(src_pos+length-1) &&
2254 dst->IsValidIndex(dst_pos) &&
2255 dst->IsValidIndex(dst_pos+length-1)) {
2256 MemberOffset src_offset(DataOffset().Int32Value() +
2257 src_pos * sizeof(Object*));
2258 MemberOffset dst_offset(DataOffset().Int32Value() +
2259 dst_pos * sizeof(Object*));
2260 Class* array_class = dst->GetClass();
2261 if (array_class == src->GetClass()) {
2262 // No need for array store checks if arrays are of the same type
2263 for (size_t i=0; i < length; i++) {
2264 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2265 dst->SetFieldObject(dst_offset, object, false);
2266 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2267 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2268 }
2269 } else {
2270 Class* element_class = array_class->GetComponentType();
2271 CHECK(!element_class->IsPrimitive());
2272 for (size_t i=0; i < length; i++) {
2273 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002274 if (object != NULL && !object->InstanceOf(element_class)) {
2275 dst->ThrowArrayStoreException(object);
2276 return;
2277 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002278 dst->SetFieldObject(dst_offset, object, false);
2279 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2280 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2281 }
2282 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002283 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002284 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002285}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002286
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002287class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002288 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002289 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002290 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002291 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002292 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2293};
2294
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002295class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002296 private:
2297 CharArray* ASCII_;
2298 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002299 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002300 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002301 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002302 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2303};
2304
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002305class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002306 private:
2307 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2308 uint32_t TYPE_BOOLEAN_;
2309 uint32_t TYPE_BYTE_;
2310 uint32_t TYPE_CHAR_;
2311 uint32_t TYPE_DOUBLE_;
2312 uint32_t TYPE_FLOAT_;
2313 uint32_t TYPE_INTEGER_;
2314 uint32_t TYPE_LONG_;
2315 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002316 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002317 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2318};
2319
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002320class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002321 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002322 Object* ORDER_BY_SIGNATURE_;
2323 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002324 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2325};
2326
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002327template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002328class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002329 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002330 typedef T ElementType;
2331
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002332 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002333
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002334 const T* GetData() const {
2335 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002336 }
2337
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002338 T* GetData() {
2339 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002340 }
2341
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002342 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002343 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002344 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002345 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002346 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002347 }
2348
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002349 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002350 // TODO: ArrayStoreException
2351 if (IsValidIndex(i)) {
2352 GetData()[i] = value;
2353 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002354 }
2355
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002356 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002357 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002358 CHECK(array_class != NULL);
2359 array_class_ = array_class;
2360 }
2361
Brian Carlstroma663ea52011-08-19 23:33:41 -07002362 static void ResetArrayClass() {
2363 CHECK(array_class_ != NULL);
2364 array_class_ = NULL;
2365 }
2366
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002367 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002368 // Location of first element.
2369 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002370
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002371 static Class* array_class_;
2372
Carl Shapirof88c9522011-08-06 15:47:38 -07002373 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002374};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002376inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2377 DCHECK(NULL == GetFieldObject<IntArray*>(
2378 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2379 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2380 new_interfaces_idx, false);
2381}
2382
2383// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002384class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002385 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002386 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002387 const CharArray* result = GetFieldObject<const CharArray*>(
2388 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2389 DCHECK(result != NULL);
2390 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002391 }
2392
Elliott Hughes814e4032011-08-23 12:07:56 -07002393 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002394 int32_t result = GetField32(
2395 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2396 DCHECK_LE(0, result);
2397 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002398 }
2399
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002400 int32_t GetLength() const;
2401
Brian Carlstrom395520e2011-09-25 19:35:00 -07002402 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002403
2404 void ComputeHashCode() {
2405 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002406 }
2407
Elliott Hughes814e4032011-08-23 12:07:56 -07002408 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002409 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002410 }
2411
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002412 uint16_t CharAt(int32_t index) const;
2413
Brian Carlstromc74255f2011-09-11 22:47:39 -07002414 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002415
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002416 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002417 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002418 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002419
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002420 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002421
Jesse Wilson8989d992011-08-02 13:39:42 -07002422 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002423 const char* utf8_data_in);
2424
2425 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2426
2427 static String* Alloc(Class* java_lang_String, CharArray* array);
2428
2429 bool Equals(const char* modified_utf8) const;
2430
2431 // TODO: do we need this overload? give it a more intention-revealing name.
2432 bool Equals(const StringPiece& modified_utf8) const;
2433
2434 bool Equals(const String* that) const;
2435
2436 // TODO: do we need this overload? give it a more intention-revealing name.
2437 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2438 int32_t that_length) const;
2439
2440 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2441 std::string ToModifiedUtf8() const;
2442
2443 static Class* GetJavaLangString() {
2444 DCHECK(java_lang_String_ != NULL);
2445 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002446 }
2447
Brian Carlstroma663ea52011-08-19 23:33:41 -07002448 static void SetClass(Class* java_lang_String);
2449 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002450
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002451 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002452 void SetHashCode(int32_t new_hash_code) {
2453 DCHECK_EQ(0u,
2454 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2455 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2456 new_hash_code, false);
2457 }
2458
2459 void SetCount(int32_t new_count) {
2460 DCHECK_LE(0, new_count);
2461 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2462 }
2463
2464 void SetOffset(int32_t new_offset) {
2465 DCHECK_LE(0, new_offset);
2466 DCHECK_GE(GetLength(), new_offset);
2467 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2468 }
2469
2470 void SetArray(CharArray* new_array) {
2471 DCHECK(new_array != NULL);
2472 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2473 }
2474
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002475 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2476 CharArray* array_;
2477
Brian Carlstromdbc05252011-09-09 01:59:59 -07002478 int32_t count_;
2479
Carl Shapirof88c9522011-08-06 15:47:38 -07002480 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002481
Elliott Hughes289da822011-08-16 10:11:20 -07002482 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002483
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002484 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002485
Brian Carlstrom693267a2011-09-06 09:25:34 -07002486 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002487 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002488};
2489
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002490inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002491 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002492 String* result =
2493 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2494 DCHECK(result != NULL);
2495 return result;
2496}
2497
2498inline void Field::SetName(String* new_name) {
2499 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2500 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002501}
2502
2503inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002504 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002505 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2506}
2507
2508inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002509 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002510 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2511}
2512
2513inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002514 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002515 return MemberOffset(
2516 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2517}
2518
2519inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002520 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002521 return MemberOffset(
2522 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2523}
2524
2525inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002526 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002527 const String* result =
2528 GetFieldObject<const String*>(
2529 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2530 DCHECK(result != NULL);
2531 return result;
2532}
2533
2534inline void Method::SetName(String* new_name) {
2535 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2536 new_name, false);
2537
2538}
2539
jeffhaoe23d93c2011-09-15 14:48:43 -07002540inline ByteArray* Method::GetRegisterMapData() const {
2541 return GetFieldObject<ByteArray*>(
2542 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2543}
2544
jeffhaoa0a764a2011-09-16 10:43:38 -07002545inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002546 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002547 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002548}
2549
2550inline ByteArray* Method::GetRegisterMapHeader() const {
2551 return GetFieldObject<ByteArray*>(
2552 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2553}
2554
jeffhaoa0a764a2011-09-16 10:43:38 -07002555inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002556 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002557 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002558}
2559
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002560inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002561 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002562 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002563 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2564}
2565
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002566inline void Method::SetShorty(String* new_shorty) {
2567 DCHECK(NULL == GetFieldObject<String*>(
2568 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2569 DCHECK_LE(1, new_shorty->GetLength());
2570 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2571}
2572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002573inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002574 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002575 const String* result =
2576 GetFieldObject<const String*>(
2577 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2578 DCHECK(result != NULL);
2579 return result;
2580}
2581
2582inline void Method::SetSignature(String* new_signature) {
2583 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2584 new_signature, false);
2585}
2586
2587inline uint32_t Class::GetAccessFlags() const {
2588 // Check class is loaded or this is java.lang.String that has a
2589 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002590 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002591 this == String::GetJavaLangString() ||
2592 this == Field::GetJavaLangReflectField() ||
2593 this == Method::GetConstructorClass() ||
2594 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002595 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2596}
2597
2598inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002599 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002600 DCHECK(new_descriptor != NULL);
2601 DCHECK_NE(0, new_descriptor->GetLength());
2602 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2603 new_descriptor, false);
2604}
2605
Brian Carlstromc74255f2011-09-11 22:47:39 -07002606inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002607 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002608 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2609}
2610
2611inline void Class::SetSourceFile(String* new_source_file) {
2612 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2613}
2614
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002615inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002616 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002617 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2618}
2619
2620inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002621 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002622 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002623}
2624
2625inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002626 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002627 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002628}
2629
2630inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002631 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002632 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002633}
2634
2635inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002636 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002637 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002638}
2639
2640inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002641 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002642 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2643}
2644
2645// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002646class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002647 public:
2648 void SetDetailMessage(String* new_detail_message) {
2649 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2650 new_detail_message, false);
2651 }
2652
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002653 private:
2654 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2655 Throwable* cause_;
2656 String* detail_message_;
2657 Object* stack_state_; // Note this is Java volatile:
2658 Object* stack_trace_;
2659 Object* suppressed_exceptions_;
2660
Brian Carlstrom693267a2011-09-06 09:25:34 -07002661 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002662 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2663};
2664
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002665// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002666class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002667 public:
2668 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002669 return GetFieldObject<const String*>(
2670 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002671 }
2672
2673 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002674 return GetFieldObject<const String*>(
2675 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002676 }
2677
2678 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002679 return GetFieldObject<const String*>(
2680 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002681 }
2682
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002683 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002684 return GetField32(
2685 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002686 }
2687
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002688 static StackTraceElement* Alloc(const String* declaring_class,
2689 const String* method_name,
2690 const String* file_name,
2691 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002692
2693 static void SetClass(Class* java_lang_StackTraceElement);
2694
2695 static void ResetClass();
2696
2697 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002698 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002699 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002700 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002701 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002702 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002703
2704 static Class* GetStackTraceElement() {
2705 DCHECK(java_lang_StackTraceElement_ != NULL);
2706 return java_lang_StackTraceElement_;
2707 }
2708
2709 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002710
2711 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002712 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2713};
2714
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002715class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002716 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002717 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002718 Class* interface = Get(kInterface)->AsClass();
2719 DCHECK(interface != NULL);
2720 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002721 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002722
Brian Carlstrom30b94452011-08-25 21:35:26 -07002723 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002724 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002725 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002726 DCHECK(Get(kInterface) == NULL);
2727 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002728 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002729
Brian Carlstrom86927212011-09-15 11:31:11 -07002730 size_t GetMethodArrayCount() const {
2731 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2732 if (method_array == 0) {
2733 return 0;
2734 }
2735 return method_array->GetLength();
2736 }
2737
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002738 ObjectArray<Method>* GetMethodArray() const {
2739 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2740 DCHECK(method_array != NULL);
2741 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002742 }
2743
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002744 void SetMethodArray(ObjectArray<Method>* new_ma) {
2745 DCHECK(new_ma != NULL);
2746 DCHECK(Get(kMethodArray) == NULL);
2747 Set(kMethodArray, new_ma);
2748 }
2749
2750 static size_t LengthAsArray() {
2751 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002752 }
2753
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002754 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002755
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002756 enum ArrayIndex {
2757 // Points to the interface class.
2758 kInterface = 0,
2759 // Method pointers into the vtable, allow fast map from interface
2760 // method index to concrete instance method.
2761 kMethodArray = 1,
2762 kMax = 2,
2763 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002764
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002765 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002766};
2767
2768} // namespace art
2769
2770#endif // ART_SRC_OBJECT_H_