blob: ce1d1443b7c1c06fb9b44665ba0738b5f324305e [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
Elliott Hughes20cde902011-10-04 17:37:27 -0700136// Special runtime-only flags.
137// Note: if only kAccClassIsReference is set, we have a soft reference.
138static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
139static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
140static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
141static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
142static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700143
144static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
145 | kAccClassIsWeakReference
146 | kAccClassIsFinalizerReference
147 | kAccClassIsPhantomReference);
148
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700150 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151 */
152/*
153 * A magic value for refOffsets. Ignore the bits and walk the super
154 * chain when this is the value.
155 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
156 * fields followed by 2 ref instance fields.]
157 */
158#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700159#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
160#define CLASS_OFFSET_ALIGNMENT 4
161#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
162/*
163 * Given an offset, return the bit number which would encode that offset.
164 * Local use only.
165 */
166#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700167 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700168 CLASS_OFFSET_ALIGNMENT)
169/*
170 * Is the given offset too large to be encoded?
171 */
172#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
173 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
174/*
175 * Return a single bit, encoding the offset.
176 * Undefined if the offset is too large, as defined above.
177 */
178#define CLASS_BIT_FROM_OFFSET(byteOffset) \
179 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
180/*
181 * Return an offset, given a bit number as returned from CLZ.
182 */
183#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700184 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186#define OFFSET_OF_OBJECT_MEMBER(type, field) \
187 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700188
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700189// Classes shared with the managed side of the world need to be packed
190// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700191#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700194class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700195 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 static MemberOffset ClassOffset() {
197 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198 }
199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 }
203
204 void SetClass(Class* new_klass);
205
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700206 bool InstanceOf(const Class* klass) const;
207
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700208 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209
Elliott Hughes081be7f2011-09-18 16:50:26 -0700210 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700211
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 static MemberOffset MonitorOffset() {
213 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
214 }
215
Elliott Hughes5f791332011-09-15 17:45:30 -0700216 volatile int32_t* GetRawLockWordAddress() {
217 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
218 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
219 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700220 }
221
Elliott Hughes5f791332011-09-15 17:45:30 -0700222 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223
Elliott Hughes5f791332011-09-15 17:45:30 -0700224 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700225
Ian Rogersff1ed472011-09-20 13:46:24 -0700226 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227
Elliott Hughes5f791332011-09-15 17:45:30 -0700228 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Elliott Hughes5f791332011-09-15 17:45:30 -0700232 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233
Elliott Hughes5f791332011-09-15 17:45:30 -0700234 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700235
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700236 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700237
238 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700239 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 return down_cast<Class*>(this);
241 }
242
243 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 return down_cast<const Class*>(this);
246 }
247
Brian Carlstrom4873d462011-08-21 15:23:39 -0700248 bool IsClassClass() const;
249
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700250 bool IsObjectArray() const;
251
252 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700253 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700256 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257
Brian Carlstromb63ec392011-08-27 17:38:27 -0700258 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700259
260 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700261 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700262 return down_cast<Array*>(this);
263 }
264
265 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700266 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700267 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268 }
269
Brian Carlstroma663ea52011-08-19 23:33:41 -0700270 bool IsString() const;
271
272 String* AsString() {
273 DCHECK(IsString());
274 return down_cast<String*>(this);
275 }
276
277 bool IsMethod() const;
278
279 Method* AsMethod() {
280 DCHECK(IsMethod());
281 return down_cast<Method*>(this);
282 }
283
Brian Carlstrom4873d462011-08-21 15:23:39 -0700284 const Method* AsMethod() const {
285 DCHECK(IsMethod());
286 return down_cast<const Method*>(this);
287 }
288
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289 bool IsField() const;
290
291 Field* AsField() {
292 DCHECK(IsField());
293 return down_cast<Field*>(this);
294 }
295
Brian Carlstrom4873d462011-08-21 15:23:39 -0700296 const Field* AsField() const {
297 DCHECK(IsField());
298 return down_cast<const Field*>(this);
299 }
300
Elliott Hughes20cde902011-10-04 17:37:27 -0700301 // If you're looking for SetFinalizable, this is the moral equivalent.
302 void AddFinalizerReference();
303
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700304 bool IsReferenceInstance() const;
305
306 bool IsWeakReferenceInstance() const;
307
308 bool IsSoftReferenceInstance() const;
309
310 bool IsFinalizerReferenceInstance() const;
311
312 bool IsPhantomReferenceInstance() const;
313
314 // Accessors for Java type fields
315 template<class T>
316 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700317 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
318 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700319 Heap::VerifyObject(result);
320 return result;
321 }
322
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700323 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700325 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
326 if (new_value != NULL) {
327 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
330
331 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
332 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700333 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
334 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700336 return android_atomic_acquire_load(word_addr);
337 } else {
338 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700340 }
341
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700342 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
343 if (this_is_valid) {
344 Heap::VerifyObject(this);
345 }
346 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700347 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700349 /*
350 * TODO: add an android_atomic_synchronization_store() function and
351 * use it in the 32-bit volatile set handlers. On some platforms we
352 * can use a fast atomic instruction and avoid the barriers.
353 */
354 ANDROID_MEMBAR_STORE();
355 *word_addr = new_value;
356 ANDROID_MEMBAR_FULL();
357 } else {
358 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700359 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 }
361
362 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
363 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700364 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700365 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700366 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700367 uint64_t result = QuasiAtomicRead64(addr);
368 ANDROID_MEMBAR_FULL();
369 return result;
370 } else {
371 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700372 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700373 }
374
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700375 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700377 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
378 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700380 ANDROID_MEMBAR_STORE();
381 QuasiAtomicSwap64(new_value, addr);
382 // Post-store barrier not required due to use of atomic op or mutex.
383 } else {
384 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700385 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 }
387
388 protected:
389 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 template<class T>
391 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700392 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 }
394
395 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700396 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
397 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 }
399
400 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700401 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700402
Elliott Hughes5f791332011-09-15 17:45:30 -0700403 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404
Elliott Hughes5f791332011-09-15 17:45:30 -0700405 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700406 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700407 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408};
409
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700410// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700411class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400412 private:
413 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700415 friend struct AccessibleObjectOffsets; // for verifying offset information
416 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400417};
418
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700420class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700421 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 void SetDeclaringClass(Class *new_declaring_class);
425
426 const String* GetName() const;
427
428 void SetName(String* new_name);
429
430 uint32_t GetAccessFlags() const;
431
432 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700433 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700434 }
435
Elliott Hughes80609252011-09-23 17:24:51 -0700436 bool IsPublic() const {
437 return (GetAccessFlags() & kAccPublic) != 0;
438 }
439
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700440 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700441 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700442 }
443
jeffhaobdb76512011-09-07 11:43:16 -0700444 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700445 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700446 }
447
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700449
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700450 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700451
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700452 // Gets type using type index and resolved types in the dex cache, may be null
453 // if type isn't yet resolved
454 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700456 // Performs full resolution, may return null and set exceptions if type cannot
457 // be resolved
458 Class* GetType() const;
459
460 // Offset to field within an Object
461 MemberOffset GetOffset() const;
462
buzbee34cd9e52011-09-08 14:31:52 -0700463 static MemberOffset OffsetOffset() {
464 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
465 }
466
Brian Carlstrom845490b2011-09-19 15:56:53 -0700467 static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer);
468 static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer);
469 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static);
buzbee34cd9e52011-09-08 14:31:52 -0700470
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700471 MemberOffset GetOffsetDuringLinking() const;
472
473 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700474
Brian Carlstrom4873d462011-08-21 15:23:39 -0700475 // field access, null object for static fields
476 bool GetBoolean(const Object* object) const;
477 void SetBoolean(Object* object, bool z) const;
478 int8_t GetByte(const Object* object) const;
479 void SetByte(Object* object, int8_t b) const;
480 uint16_t GetChar(const Object* object) const;
481 void SetChar(Object* object, uint16_t c) const;
482 uint16_t GetShort(const Object* object) const;
483 void SetShort(Object* object, uint16_t s) const;
484 int32_t GetInt(const Object* object) const;
485 void SetInt(Object* object, int32_t i) const;
486 int64_t GetLong(const Object* object) const;
487 void SetLong(Object* object, int64_t j) const;
488 float GetFloat(const Object* object) const;
489 void SetFloat(Object* object, float f) const;
490 double GetDouble(const Object* object) const;
491 void SetDouble(Object* object, double d) const;
492 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700493 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700494
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700495 // Slow path routines for static field access when field was unresolved at
496 // compile time
497 static uint32_t Get32StaticFromCode(uint32_t field_idx,
498 const Method* referrer);
499 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
500 uint32_t new_value);
501 static uint64_t Get64StaticFromCode(uint32_t field_idx,
502 const Method* referrer);
503 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
504 uint64_t new_value);
505 static Object* GetObjStaticFromCode(uint32_t field_idx,
506 const Method* referrer);
507 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
508 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700509
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700510 static Class* GetJavaLangReflectField() {
511 DCHECK(java_lang_reflect_Field_ != NULL);
512 return java_lang_reflect_Field_;
513 }
514
515 static void SetClass(Class* java_lang_reflect_Field);
516 static void ResetClass();
517
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700518 bool IsVolatile() const {
519 return (GetAccessFlags() & kAccVolatile) != 0;
520 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700521
Elliott Hughes80609252011-09-23 17:24:51 -0700522 void InitJavaFields();
523
buzbee1da522d2011-09-04 11:22:20 -0700524 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700525 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700526 uint32_t Get32(const Object* object) const;
527 void Set32(Object* object, uint32_t new_value) const;
528 uint64_t Get64(const Object* object) const;
529 void Set64(Object* object, uint64_t new_value) const;
530 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700531 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700532
Elliott Hughes80609252011-09-23 17:24:51 -0700533 void InitJavaFieldsLocked();
534
Jesse Wilson35baaab2011-08-10 16:18:03 -0400535 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700536
Jesse Wilson35baaab2011-08-10 16:18:03 -0400537 // The class in which this field is declared.
538 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700539
Jesse Wilson35baaab2011-08-10 16:18:03 -0400540 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700541
Brian Carlstromdbc05252011-09-09 01:59:59 -0700542 const String* name_;
543
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700544 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700545 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400546
Brian Carlstromdbc05252011-09-09 01:59:59 -0700547 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700548
Jesse Wilson35baaab2011-08-10 16:18:03 -0400549 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700550
551 // Offset of field within an instance or in the Class' static fields
552 uint32_t offset_;
553
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 // Dex cache index of resolved type
555 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400556
Brian Carlstrom693267a2011-09-06 09:25:34 -0700557 int32_t slot_;
558
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700559 static Class* java_lang_reflect_Field_;
560
Brian Carlstrom693267a2011-09-06 09:25:34 -0700561 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400562 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700563};
564
Jesse Wilson6bf19152011-09-29 13:12:33 -0400565// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700566class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700567 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700568 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700569 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700570 Object* obj,
571 Thread* thread,
572 byte* args,
573 JValue* result);
574
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700579 static MemberOffset DeclaringClassOffset() {
580 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700581 }
582
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 // Returns the method name, e.g. "<init>" or "eatLunch"
584 const String* GetName() const;
585
586 void SetName(String* new_name);
587
jeffhaoe23d93c2011-09-15 14:48:43 -0700588 ByteArray* GetRegisterMapData() const;
589
jeffhaoa0a764a2011-09-16 10:43:38 -0700590 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700591
592 ByteArray* GetRegisterMapHeader() const;
593
jeffhaoa0a764a2011-09-16 10:43:38 -0700594 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700595
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700596 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700597
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700598 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700599
600 const String* GetSignature() const;
601
602 void SetSignature(String* new_signature);
603
604 bool HasSameNameAndDescriptor(const Method* that) const;
605
606 uint32_t GetAccessFlags() const;
607
608 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700609 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700610 }
611
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612 // Returns true if the method is declared public.
613 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700615 }
616
617 // Returns true if the method is declared private.
618 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700620 }
621
622 // Returns true if the method is declared static.
623 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700624 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700625 }
626
Brian Carlstrom1f870082011-08-23 16:02:11 -0700627 // Returns true if the method is a constructor.
628 bool IsConstructor() const {
629 return (access_flags_ & kAccConstructor) != 0;
630 }
631
632 // Returns true if the method is static, private, or a constructor.
633 bool IsDirect() const {
634 return IsStatic() || IsPrivate() || IsConstructor();
635 }
636
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700637 // Returns true if the method is declared synchronized.
638 bool IsSynchronized() const {
639 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700640 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700641 }
642
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700643 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700644 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645 }
646
Elliott Hughes80609252011-09-23 17:24:51 -0700647 bool IsMiranda() const {
648 return (GetAccessFlags() & kAccMiranda) != 0;
649 }
650
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700652 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700653 }
654
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700655 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700656 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700657 }
658
659 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700660 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661 }
662
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663 uint16_t GetMethodIndex() const;
664
665 size_t GetVtableIndex() const {
666 return GetMethodIndex();
667 }
668
669 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700670 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700671 new_method_index, false);
672 }
673
674 static MemberOffset MethodIndexOffset() {
675 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
676 }
677
678 uint32_t GetCodeItemOffset() const {
679 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
680 }
681
682 void SetCodeItemOffset(uint32_t new_code_off) {
683 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
684 new_code_off, false);
685 }
686
687 // Number of 32bit registers that would be required to hold all the arguments
688 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700689
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700690 // Number of argument bytes required for densely packing the
691 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700692 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700693
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700694 uint16_t NumRegisters() const;
695
696 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700697 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700698 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700699 }
700
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700701 uint16_t NumIns() const;
702
703 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700704 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700705 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700706 }
707
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700708 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700709
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700710 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700711 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700712 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700713 }
714
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700715 uint32_t GetProtoIdx() const;
716
717 void SetProtoIdx(uint32_t new_proto_idx) {
718 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700719 }
720
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700721 ObjectArray<String>* GetDexCacheStrings() const;
722 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
723
724 static MemberOffset DexCacheStringsOffset() {
725 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
726 }
727
buzbee2a475e72011-09-07 17:19:17 -0700728 static MemberOffset DexCacheResolvedTypesOffset() {
729 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
730 }
731
buzbee34cd9e52011-09-08 14:31:52 -0700732 static MemberOffset DexCacheResolvedFieldsOffset() {
733 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
734 }
735
buzbee1da522d2011-09-04 11:22:20 -0700736 static MemberOffset DexCacheInitializedStaticStorageOffset() {
737 return OFFSET_OF_OBJECT_MEMBER(Method,
738 dex_cache_initialized_static_storage_);
739 }
740
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700741 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
742 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
743
744 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
745 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
746
747 ObjectArray<Field>* GetDexCacheResolvedFields() const;
748 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
749
750 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
751 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
752
753 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
754 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
755
756 void SetReturnTypeIdx(uint32_t new_return_type_idx);
757
758 Class* GetReturnType() const;
759
760 bool IsReturnAReference() const;
761
762 bool IsReturnAFloat() const;
763
764 bool IsReturnADouble() const;
765
Ian Rogersb033c752011-07-20 12:22:35 -0700766 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700767 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700768 }
769
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700770 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700771
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700773
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700774 // "Args" may refer to any of the 3 levels of "Args."
775 // To avoid confusion, our code will denote which "Args" clearly:
776 // 1. UserArgs: Args that a user see.
777 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
778 // receiver.
779 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
780 // E.g., the first in Args is Method* for both static and non-static
781 // methods. And CConvArgs doesn't deal with the receiver because
782 // receiver is hardwired in an implicit register, so CConvArgs doesn't
783 // need to deal with it.
784 //
785 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700786 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700787
788 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700789 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700790 size_t NumReferenceArgs() const;
791
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700792 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700793 size_t NumLongOrDoubleArgs() const;
794
Ian Rogersb033c752011-07-20 12:22:35 -0700795 // Is the given method parameter a reference?
796 bool IsParamAReference(unsigned int param) const;
797
798 // Is the given method parameter a long or double?
799 bool IsParamALongOrDouble(unsigned int param) const;
800
Ian Rogersdf20fe02011-07-20 20:34:16 -0700801 // Size in bytes of the given parameter
802 size_t ParamSize(unsigned int param) const;
803
804 // Size in bytes of the return value
805 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700806
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700807 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
808
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700809 const ByteArray* GetCodeArray() const {
810 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
811 }
812
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700813 const void* GetCode() const {
814 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700815 }
816
Brian Carlstrome24fa612011-09-29 00:53:55 -0700817 void SetCode(void* code) {
818 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
819 }
820
821 uint32_t GetOatCodeOffset() const {
822 CHECK(!Runtime::Current()->IsStarted());
823 return reinterpret_cast<uint32_t>(GetCode());
824 }
825
826 void SetOatCodeOffset(uint32_t code_offset) {
827 CHECK(!Runtime::Current()->IsStarted());
828 SetCode(reinterpret_cast<void*>(code_offset));
829 }
830
831 void SetCodeArray(ByteArray* code_array, InstructionSet instruction_set);
buzbeec143c552011-08-20 17:38:58 -0700832
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700833 static MemberOffset GetCodeOffset() {
834 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700835 }
836
Ian Rogersbdb03912011-09-14 00:55:44 -0700837 // Is the given PC within the code array?
838 bool IsWithinCode(uintptr_t pc) const;
839
840 IntArray* GetMappingTable() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700841 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
842 }
843
844 void SetMappingTable(IntArray* mapping_table) {
845 SetFieldPtr<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), mapping_table, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700846 }
847
buzbeec41e5b52011-09-23 12:46:19 -0700848 ShortArray* GetVMapTable() const {
Ian Rogersd6b1f612011-09-27 13:38:14 -0700849 return GetFieldObject<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700850 }
851
Brian Carlstrome24fa612011-09-29 00:53:55 -0700852 void SetVMapTable(ShortArray* vmap_table) {
853 SetFieldPtr<ShortArray*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
854 }
855
Shih-wei Liaod11af152011-08-23 16:02:11 -0700856 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700857 DCHECK(sizeof(size_t) == sizeof(uint32_t));
858 size_t result = GetField32(
859 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
860 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
861 return result;
862 }
863
864 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
865 DCHECK(sizeof(size_t) == sizeof(uint32_t));
866 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
867 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
868 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700869 }
870
Shih-wei Liaod11af152011-08-23 16:02:11 -0700871 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700872 DCHECK(sizeof(size_t) == sizeof(uint32_t));
873 return GetField32(
874 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700875 }
876
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700877 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
878 DCHECK(sizeof(size_t) == sizeof(uint32_t));
879 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
880 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
881 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700882 }
883
Brian Carlstrom16192862011-09-12 17:50:06 -0700884 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700885
Brian Carlstrom16192862011-09-12 17:50:06 -0700886 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700887
Brian Carlstrom16192862011-09-12 17:50:06 -0700888 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700889
Ian Rogersb033c752011-07-20 12:22:35 -0700890 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700891 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700892 }
893
Brian Carlstrom78128a62011-09-15 17:21:19 -0700894 const void* GetNativeMethod() const {
895 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
896 }
897
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700898 ByteArray* GetInvokeStubArray() const {
899 ByteArray* result = GetFieldPtr<ByteArray*>(
900 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
901 // TODO: DCHECK(result != NULL); should be ahead of time compiled
902 return result;
903 }
904
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700905 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700906 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700907 InvokeStub* result = GetFieldPtr<InvokeStub*>(
908 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
909 // TODO: DCHECK(result != NULL); should be ahead of time compiled
910 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700911 }
912
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700913 static MemberOffset GetInvokeStubOffset() {
914 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700915 }
916
buzbee561227c2011-09-02 15:28:19 -0700917 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
918 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
919 }
920
921 static MemberOffset GetDexCacheResolvedMethodsOffset() {
922 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
923 }
924
925 static MemberOffset GetMethodIndexOffset() {
926 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
927 }
928
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700929 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700930
Ian Rogers90865722011-09-19 11:11:44 -0700931 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700932 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700933 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700934
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700935 void SetCoreSpillMask(uint32_t core_spill_mask) {
936 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700937 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938 }
939
Ian Rogers90865722011-09-19 11:11:44 -0700940 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700941 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
942 }
943
944 void SetFpSpillMask(uint32_t fp_spill_mask) {
945 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700946 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700947 }
948
Ian Rogers90865722011-09-19 11:11:44 -0700949 // Is this a hand crafted method used for something like describing callee saves?
950 bool IsPhony() const {
Ian Rogersff1ed472011-09-20 13:46:24 -0700951 bool result = this == Runtime::Current()->GetCalleeSaveMethod();
Ian Rogers90865722011-09-19 11:11:44 -0700952 // Check that if we do think it is phony it looks like the callee save method
953 DCHECK(!result || GetCoreSpillMask() != 0);
954 return result;
955 }
956
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957 // Converts a native PC to a dex PC. TODO: this is a no-op
958 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700959 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700960
961 // Converts a dex PC to a native PC. TODO: this is a no-op
962 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700963 uintptr_t ToNativePC(const uint32_t dex_pc) const;
964
965 // Find the catch block for the given exception type and dex_pc
966 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700967
Elliott Hughes80609252011-09-23 17:24:51 -0700968 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
969
970 static Class* GetConstructorClass() {
971 return java_lang_reflect_Constructor_;
972 }
973
974 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700975 return java_lang_reflect_Method_;
976 }
977
Elliott Hughes80609252011-09-23 17:24:51 -0700978 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700979
Elliott Hughes418d20f2011-09-22 14:00:39 -0700980 void InitJavaFields();
981
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700982 private:
983 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700984 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700985
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700986 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
987 // the class we are a part of
988 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700989 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700990 Object* java_formal_type_parameters_;
991 Object* java_generic_exception_types_;
992 Object* java_generic_parameter_types_;
993 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700994
Brian Carlstrom693267a2011-09-06 09:25:34 -0700995 String* name_;
996
Elliott Hughes418d20f2011-09-22 14:00:39 -0700997 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700998 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700999 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001000
Brian Carlstrom693267a2011-09-06 09:25:34 -07001001 // Storage for code_
1002 const ByteArray* code_array_;
1003
1004 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001005 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1006
1007 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1008 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1009
1010 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1011 ObjectArray<Field>* dex_cache_resolved_fields_;
1012
1013 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1014 ObjectArray<Method>* dex_cache_resolved_methods_;
1015
Brian Carlstromdbc05252011-09-09 01:59:59 -07001016 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1017 ObjectArray<Class>* dex_cache_resolved_types_;
1018
1019 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1020 ObjectArray<String>* dex_cache_strings_;
1021
1022 // Storage for invoke_stub_
1023 const ByteArray* invoke_stub_array_;
1024
1025 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -07001026 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001027
jeffhaoe23d93c2011-09-15 14:48:43 -07001028 // Byte arrays that hold data for the register maps
1029 const ByteArray* register_map_data_;
1030 const ByteArray* register_map_header_;
1031
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001032 // The short-form method descriptor string.
1033 String* shorty_;
1034
Brian Carlstromdbc05252011-09-09 01:59:59 -07001035 // The method descriptor. This represents the parameters a method
1036 // takes and value it returns. This string is a list of the type
1037 // descriptors for the parameters enclosed in parenthesis followed
1038 // by the return type descriptor. For example, for the method
1039 //
1040 // Object mymethod(int i, double d, Thread t)
1041 //
1042 // the method descriptor would be
1043 //
1044 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1045 String* signature_;
1046
buzbeec41e5b52011-09-23 12:46:19 -07001047 // Storage for Dalvik virtual register mapping_table_
1048 ShortArray* vmap_table_;
1049
Brian Carlstromdbc05252011-09-09 01:59:59 -07001050 uint32_t java_generic_types_are_initialized_;
1051
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001052 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001053 uint32_t access_flags_;
1054
1055 // Compiled code associated with this method for callers from managed code.
1056 // May be compiled managed code or a bridge for invoking a native method.
1057 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001058
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001059 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001060 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001061
Brian Carlstrom693267a2011-09-06 09:25:34 -07001062 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001063 uint32_t core_spill_mask_;
1064
1065 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001066 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001067
Brian Carlstrom693267a2011-09-06 09:25:34 -07001068 // Total size in bytes of the frame
1069 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001070
Brian Carlstrom693267a2011-09-06 09:25:34 -07001071 // Native invocation stub entry point for calling from native to managed code.
1072 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001073
Brian Carlstrom693267a2011-09-06 09:25:34 -07001074 // Index of the return type
1075 uint32_t java_return_type_idx_;
1076
Brian Carlstrom693267a2011-09-06 09:25:34 -07001077 // For concrete virtual methods, this is the offset of the method
1078 // in Class::vtable_.
1079 //
1080 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001081 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001082 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001083
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001084 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001085 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001086
Brian Carlstrom693267a2011-09-06 09:25:34 -07001087 // Method bounds; not needed for an abstract method.
1088 //
1089 // For a native method, we compute the size of the argument list, and
1090 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001091 uint32_t num_ins_;
1092 uint32_t num_outs_;
1093 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001094
Brian Carlstrom693267a2011-09-06 09:25:34 -07001095 // Method prototype descriptor string (return and argument types).
1096 uint32_t proto_idx_;
1097
1098 // Offset of return PC within frame for compiled code (in bytes)
1099 size_t return_pc_offset_in_bytes_;
1100
Brian Carlstrom693267a2011-09-06 09:25:34 -07001101 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001102
Elliott Hughes80609252011-09-23 17:24:51 -07001103 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001104 static Class* java_lang_reflect_Method_;
1105
Brian Carlstrom693267a2011-09-06 09:25:34 -07001106 friend class ImageWriter; // for relocating code_ and invoke_stub_
1107 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001108 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109};
1110
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001111class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001112 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001113 // A convenience for code that doesn't know the component size,
1114 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001115 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001116
Brian Carlstromb63ec392011-08-27 17:38:27 -07001117 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001118
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001119 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001120
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001121 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001123 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001124
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001125 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001126 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001127 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001128 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001129
buzbeec143c552011-08-20 17:38:58 -07001130 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001131 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001132 }
1133
1134 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001135 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001136 }
1137
Elliott Hughesbf86d042011-08-31 17:53:14 -07001138 void* GetRawData() {
1139 return reinterpret_cast<void*>(first_element_);
1140 }
1141
Elliott Hughes289da822011-08-16 10:11:20 -07001142 protected:
1143 bool IsValidIndex(int32_t index) const {
1144 if (index < 0 || index >= length_) {
Elliott Hughes80609252011-09-23 17:24:51 -07001145 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001146 }
1147 return true;
1148 }
1149
Elliott Hughes80609252011-09-23 17:24:51 -07001150 protected:
1151 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1152 bool ThrowArrayStoreException(Object* object) const;
1153
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001154 private:
1155 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001156 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001157 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1158 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001159 // Marker for the data (used by generated code)
1160 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001161
Carl Shapirof88c9522011-08-06 15:47:38 -07001162 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001163};
1164
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001165template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001166class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001167 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001168 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001169
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001170 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001171
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001172 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001173
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001174 // Set element without bound and element type checks, to be used in limited
1175 // circumstances, such as during boot image writing
1176 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001177
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001179 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001180 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001181
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001182 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001183
1184 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001185 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001186};
1187
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001188template<class T>
1189ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1190 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1191}
1192
1193template<class T>
1194T* ObjectArray<T>::Get(int32_t i) const {
1195 if (!IsValidIndex(i)) {
1196 return NULL;
1197 }
1198 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1199 return GetFieldObject<T*>(data_offset, false);
1200}
1201
1202template<class T>
1203ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1204 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1205 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1206 return new_array;
1207}
1208
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001209// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001210// provides the static storage. However, this might change to an Array
1211// to improve image sharing, so we use this type to avoid assumptions
1212// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001213class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001214
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001215// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001216class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001217 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001218
1219 // Class Status
1220 //
1221 // kStatusNotReady: If a Class cannot be found in the class table by
1222 // FindClass, it allocates an new one with AllocClass in the
1223 // kStatusNotReady and calls LoadClass. Note if it does find a
1224 // class, it may not be kStatusResolved and it will try to push it
1225 // forward toward kStatusResolved.
1226 //
1227 // kStatusIdx: LoadClass populates with Class with information from
1228 // the DexFile, moving the status to kStatusIdx, indicating that the
1229 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001230 // populated based on super_class_type_idx_ and
1231 // interfaces_type_idx_. The new Class can then be inserted into the
1232 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001233 //
1234 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1235 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1236 // using ResolveClass to initialize the super_class_ and interfaces_.
1237 //
1238 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001239 // shows linking is complete and fields of the Class populated by making
1240 // it kStatusResolved. Java allows circularities of the form where a super
1241 // class has a field that is of the type of the sub class. We need to be able
1242 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001243
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001244 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001245 kStatusError = -1,
1246 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001247 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001248 kStatusLoaded = 2, // DEX idx values resolved
1249 kStatusResolved = 3, // part of linking
1250 kStatusVerifying = 4, // in the process of being verified
1251 kStatusVerified = 5, // logically part of linking; done pre-init
1252 kStatusInitializing = 6, // class init in progress
1253 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001254 };
1255
1256 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001257 kPrimNot = 0,
1258 kPrimBoolean,
1259 kPrimByte,
1260 kPrimChar,
1261 kPrimShort,
1262 kPrimInt,
1263 kPrimLong,
1264 kPrimFloat,
1265 kPrimDouble,
1266 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001267 };
1268
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001269 Status GetStatus() const {
1270 CHECK(sizeof(Status) == sizeof(uint32_t));
1271 return static_cast<Status>(
1272 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1273 }
1274
1275 void SetStatus(Status new_status);
1276
1277 // Returns true if the class has failed to link.
1278 bool IsErroneous() const {
1279 return GetStatus() == kStatusError;
1280 }
1281
1282 // Returns true if the class has been loaded.
1283 bool IsIdxLoaded() const {
1284 return GetStatus() >= kStatusIdx;
1285 }
1286
1287 // Returns true if the class has been loaded.
1288 bool IsLoaded() const {
1289 return GetStatus() >= kStatusLoaded;
1290 }
1291
1292 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001293 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001294 return GetStatus() >= kStatusResolved;
1295 }
1296
1297 // Returns true if the class has been verified.
1298 bool IsVerified() const {
1299 return GetStatus() >= kStatusVerified;
1300 }
1301
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001302 // Returns true if the class is initializing.
1303 bool IsInitializing() const {
1304 return GetStatus() >= kStatusInitializing;
1305 }
1306
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001307 // Returns true if the class is initialized.
1308 bool IsInitialized() const {
1309 return GetStatus() == kStatusInitialized;
1310 }
1311
1312 uint32_t GetAccessFlags() const;
1313
1314 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001315 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001316 }
1317
1318 // Returns true if the class is an interface.
1319 bool IsInterface() const {
1320 return (GetAccessFlags() & kAccInterface) != 0;
1321 }
1322
1323 // Returns true if the class is declared public.
1324 bool IsPublic() const {
1325 return (GetAccessFlags() & kAccPublic) != 0;
1326 }
1327
1328 // Returns true if the class is declared final.
1329 bool IsFinal() const {
1330 return (GetAccessFlags() & kAccFinal) != 0;
1331 }
1332
Elliott Hughes20cde902011-10-04 17:37:27 -07001333 bool IsFinalizable() const {
1334 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1335 }
1336
1337 void SetFinalizable() {
1338 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1339 SetAccessFlags(flags | kAccClassIsFinalizable);
1340 }
1341
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 // Returns true if the class is abstract.
1343 bool IsAbstract() const {
1344 return (GetAccessFlags() & kAccAbstract) != 0;
1345 }
1346
1347 // Returns true if the class is an annotation.
1348 bool IsAnnotation() const {
1349 return (GetAccessFlags() & kAccAnnotation) != 0;
1350 }
1351
1352 // Returns true if the class is synthetic.
1353 bool IsSynthetic() const {
1354 return (GetAccessFlags() & kAccSynthetic) != 0;
1355 }
1356
1357 bool IsReferenceClass() const {
1358 return (GetAccessFlags() & kAccClassIsReference) != 0;
1359 }
1360
1361 bool IsWeakReferenceClass() const {
1362 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1363 }
1364
1365 bool IsSoftReferenceClass() const {
1366 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1367 }
1368
1369 bool IsFinalizerReferenceClass() const {
1370 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1371 }
1372
1373 bool IsPhantomReferenceClass() const {
1374 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1375 }
1376
1377 PrimitiveType GetPrimitiveType() const {
1378 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1379 return static_cast<PrimitiveType>(
1380 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1381 }
1382
1383 void SetPrimitiveType(PrimitiveType new_type) {
1384 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001385 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001386 }
1387
1388 // Returns true if the class is a primitive type.
1389 bool IsPrimitive() const {
1390 return GetPrimitiveType() != kPrimNot;
1391 }
1392
1393 bool IsPrimitiveBoolean() const {
1394 return GetPrimitiveType() == kPrimBoolean;
1395 }
1396
1397 bool IsPrimitiveByte() const {
1398 return GetPrimitiveType() == kPrimByte;
1399 }
1400
1401 bool IsPrimitiveChar() const {
1402 return GetPrimitiveType() == kPrimChar;
1403 }
1404
1405 bool IsPrimitiveShort() const {
1406 return GetPrimitiveType() == kPrimShort;
1407 }
1408
1409 bool IsPrimitiveInt() const {
1410 return GetPrimitiveType() == kPrimInt;
1411 }
1412
1413 bool IsPrimitiveLong() const {
1414 return GetPrimitiveType() == kPrimLong;
1415 }
1416
1417 bool IsPrimitiveFloat() const {
1418 return GetPrimitiveType() == kPrimFloat;
1419 }
1420
1421 bool IsPrimitiveDouble() const {
1422 return GetPrimitiveType() == kPrimDouble;
1423 }
1424
1425 bool IsPrimitiveVoid() const {
1426 return GetPrimitiveType() == kPrimVoid;
1427 }
1428
1429 size_t PrimitiveSize() const;
1430
1431 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001432 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001433 }
1434
1435 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 return GetFieldObject<Class*>(
1437 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1438 }
1439
1440 void SetComponentType(Class* new_component_type) {
1441 DCHECK(GetComponentType() == NULL);
1442 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001443 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001444 }
1445
1446 size_t GetComponentSize() const {
1447 return GetTypeSize(GetComponentType()->GetDescriptor());
1448 }
1449
1450 bool IsObjectClass() const {
1451 return !IsPrimitive() && GetSuperClass() == NULL;
1452 }
1453
Brian Carlstrom1f870082011-08-23 16:02:11 -07001454 // Creates a raw object instance but does not invoke the default constructor.
1455 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001458
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001459 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460 const String* result = GetFieldObject<const String*>(
1461 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1462 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1463 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1464 return result;
1465 }
1466
1467 void SetDescriptor(String* new_descriptor);
1468
1469 bool IsVariableSize() const {
1470 // Classes and arrays vary in size, and so the object_size_ field cannot
1471 // be used to get their instance size
1472 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 }
1474
Brian Carlstrom4873d462011-08-21 15:23:39 -07001475 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 CHECK(sizeof(size_t) == sizeof(int32_t));
1477 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001478 }
1479
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 size_t GetClassSize() const {
1481 CHECK(sizeof(size_t) == sizeof(uint32_t));
1482 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001483 }
1484
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001485 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001486 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001487 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001488 << " new_class_size=" << new_class_size
1489 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001490 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001491 }
1492
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001493 size_t GetObjectSize() const {
1494 CHECK(!IsVariableSize());
1495 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001496 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001497 CHECK_GE(result, sizeof(Object));
1498 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 }
1500
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501 void SetObjectSize(size_t new_object_size) {
1502 DCHECK(!IsVariableSize());
1503 CHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001504 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001505 }
1506
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001507 // Returns true if this class is in the same packages as that class.
1508 bool IsInSamePackage(const Class* that) const;
1509
Brian Carlstrome24fa612011-09-29 00:53:55 -07001510 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001511
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001512 // Returns true if this class can access that class.
1513 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001515 }
1516
jeffhao4a801a42011-09-23 13:53:40 -07001517 // Validate method/field access.
1518 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1519 // quick accept for public access
1520 if (member_flags & kAccPublic) {
1521 return true;
1522 }
1523
1524 // quick accept for access from same class
1525 if (this == access_to) {
1526 return true;
1527 }
1528
1529 // quick reject for private access from another class
1530 if (member_flags & kAccPrivate) {
1531 return false;
1532 }
1533
1534 // Semi-quick test for protected access from a sub-class, which may or
1535 // may not be in the same package.
1536 if (member_flags & kAccProtected) {
1537 if (this->IsSubClass(access_to)) {
1538 return true;
1539 }
1540 }
1541
1542 // Allow protected and private access from other classes in the same package.
1543 return this->IsInSamePackage(access_to);
1544 }
1545
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001546 bool IsSubClass(const Class* klass) const;
1547
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001548 bool IsAssignableFrom(const Class* src) const {
1549 DCHECK(src != NULL);
1550 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001551 // Can always assign to things of the same type
1552 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001553 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001554 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001555 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001556 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001557 return src->Implements(this);
1558 } else if (src->IsArrayClass()) {
1559 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001561 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001562 }
1563 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001564
buzbee991e3ac2011-09-29 15:44:22 -07001565 // Assignable test for code, won't throw. Null and equality tests already performed
1566 static uint32_t IsAssignableFromCode(const Class* klass, const Class* ref_class)
1567 {
1568 DCHECK(klass != NULL);
1569 DCHECK(ref_class != NULL);
1570 return klass->IsAssignableFrom(ref_class) ? 1 : 0;
1571 }
1572
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573 Class* GetSuperClass() const {
1574 // Can only get super class for loaded classes (hack for when runtime is
1575 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001576 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001577 return GetFieldObject<Class*>(
1578 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1579 }
1580
buzbee4a3164f2011-09-03 11:25:10 -07001581 static MemberOffset SuperClassOffset() {
1582 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1583 }
1584
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001585 void SetSuperClass(Class *new_super_class) {
1586 // super class is assigned once, except during class linker initialization
1587 Class* old_super_class = GetFieldObject<Class*>(
1588 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1589 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1590 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001591 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001592 }
1593
1594 bool HasSuperClass() const {
1595 return GetSuperClass() != NULL;
1596 }
1597
1598 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001599 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001600 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1601 false);
1602 }
1603
1604 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001605 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001606 }
1607
1608 const ClassLoader* GetClassLoader() const;
1609
1610 void SetClassLoader(const ClassLoader* new_cl);
1611
1612 static MemberOffset DexCacheOffset() {
1613 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1614 }
1615
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001616 enum {
1617 kDumpClassFullDetail = 1,
1618 kDumpClassClassLoader = (1 << 1),
1619 kDumpClassInitialized = (1 << 2),
1620 };
1621
Elliott Hughes4681c802011-09-25 18:04:37 -07001622 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001623
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001624 DexCache* GetDexCache() const;
1625
1626 void SetDexCache(DexCache* new_dex_cache);
1627
1628 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001629 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001630 return GetFieldObject<ObjectArray<Method>*>(
1631 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1632 }
1633
1634 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1635 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1636 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1637 DCHECK_NE(0, new_direct_methods->GetLength());
1638 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1639 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001640 }
1641
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001642 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001643 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001644 }
1645
1646 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001647 ObjectArray<Method>* direct_methods =
1648 GetFieldObject<ObjectArray<Method>*>(
1649 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1650 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001651 }
1652
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001653 // Returns the number of static, private, and constructor methods.
1654 size_t NumDirectMethods() const {
1655 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1656 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001657
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001658 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001659 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001660 return GetFieldObject<ObjectArray<Method>*>(
1661 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1662 }
1663
1664 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1665 // TODO: we reassign virtual methods to grow the table for miranda
1666 // methods.. they should really just be assigned once
1667 DCHECK_NE(0, new_virtual_methods->GetLength());
1668 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1669 new_virtual_methods, false);
1670 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001671
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001672 // Returns the number of non-inherited virtual methods.
1673 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001674 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001675 }
1676
1677 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001678 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679 return GetVirtualMethods()->Get(i);
1680 }
1681
1682 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001683 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001685 }
1686
1687 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001688 ObjectArray<Method>* virtual_methods =
1689 GetFieldObject<ObjectArray<Method>*>(
1690 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1691 virtual_methods->Set(i, f);
1692 }
1693
1694 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001695 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001696 return GetFieldObject<ObjectArray<Method>*>(
1697 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1698 }
1699
1700 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001701 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001702 return GetFieldObject<ObjectArray<Method>*>(
1703 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1704 }
1705
1706 void SetVTable(ObjectArray<Method>* new_vtable) {
1707 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1708 }
1709
1710 static MemberOffset VTableOffset() {
1711 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001712 }
1713
Brian Carlstrom30b94452011-08-25 21:35:26 -07001714 // Given a method implemented by this class but potentially from a
1715 // super class, return the specific implementation
1716 // method for this class.
1717 Method* FindVirtualMethodForVirtual(Method* method) {
1718 DCHECK(!method->GetDeclaringClass()->IsInterface());
1719 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001720 // Use the index to a potentially overridden one for this instance's class.
1721 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001722 }
1723
1724 // Given a method implemented by this class, but potentially from a
1725 // super class or interface, return the specific implementation
1726 // method for this class.
1727 Method* FindVirtualMethodForInterface(Method* method);
1728
jeffhaobdb76512011-09-07 11:43:16 -07001729 Method* FindInterfaceMethod(const StringPiece& name,
1730 const StringPiece& descriptor);
1731
Brian Carlstrom30b94452011-08-25 21:35:26 -07001732 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001733 if (method->IsDirect()) {
1734 return method;
1735 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001736 if (method->GetDeclaringClass()->IsInterface()) {
1737 return FindVirtualMethodForInterface(method);
1738 }
1739 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001740 }
1741
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001742 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001743 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001744
1745 Method* FindVirtualMethod(const StringPiece& name,
1746 const StringPiece& descriptor);
1747
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001748 Method* FindDeclaredDirectMethod(const StringPiece& name,
1749 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001750
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001751 Method* FindDirectMethod(const StringPiece& name,
1752 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001753
Carl Shapiro69759ea2011-07-21 18:13:35 -07001754 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001755 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001756 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1757 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1758 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001759 }
1760
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001761 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001762 CHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 return GetFieldObject<IntArray*>(
1764 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1765 }
1766
1767 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1768
1769 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001770 CHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001771 return GetFieldObject<ObjectArray<Class>*>(
1772 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1773 }
1774
1775 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1776 DCHECK(NULL == GetFieldObject<Object*>(
1777 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001778 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001779 }
1780
1781 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001782 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001783 ObjectArray<Class>* interfaces =
1784 GetFieldObject<ObjectArray<Class>*>(
1785 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1786 interfaces->Set(i, f);
1787 }
1788
1789 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001790 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001791 return GetInterfaces()->Get(i);
1792 }
1793
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001794 int32_t GetIfTableCount() const {
1795 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1796 if (iftable == NULL) {
1797 return 0;
1798 }
1799 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001800 }
1801
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001802 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001803 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001804 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001805 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1806 }
1807
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001808 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001809 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001810 }
1811
1812 // Get instance fields
1813 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001814 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001815 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 }
1817
1818 void SetIFields(ObjectArray<Field>* new_ifields) {
1819 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1820 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001821 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001822 }
1823
1824 size_t NumInstanceFields() const {
1825 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1826 }
1827
1828 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1829 DCHECK_NE(NumInstanceFields(), 0U);
1830 return GetIFields()->Get(i);
1831 }
1832
1833 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1834 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1835 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1836 ifields->Set(i, f);
1837 }
1838
1839 // Returns the number of instance fields containing reference types.
1840 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001841 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001842 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001843 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001844 }
1845
1846 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001847 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001848 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001849 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001850 }
1851
1852 void SetNumReferenceInstanceFields(size_t new_num) {
1853 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001854 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001855 }
1856
1857 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001858 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001859 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001860 }
1861
1862 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1863
1864 // Beginning of static field data
1865 static MemberOffset FieldsOffset() {
1866 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1867 }
1868
1869 // Returns the number of static fields containing reference types.
1870 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001871 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001872 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001873 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001874 }
1875
1876 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001877 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001878 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001879 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001880 }
1881
1882 void SetNumReferenceStaticFields(size_t new_num) {
1883 DCHECK(sizeof(size_t) == sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001884 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001885 }
1886
1887 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001888 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001889 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001890 }
1891
1892 void SetSFields(ObjectArray<Field>* new_sfields) {
1893 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1894 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001895 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001896 }
1897
1898 size_t NumStaticFields() const {
1899 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1900 }
1901
1902 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1903 return GetSFields()->Get(i);
1904 }
1905
1906 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1907 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1908 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1909 sfields->Set(i, f);
1910 }
1911
1912 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001913 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001914 }
1915
1916 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1917
1918 // Finds the given instance field in this class or a superclass.
1919 Field* FindInstanceField(const StringPiece& name, Class* type);
1920
1921 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1922
1923 // Finds the given static field in this class or a superclass.
1924 Field* FindStaticField(const StringPiece& name, Class* type);
1925
1926 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1927
Elliott Hughesdcc24742011-09-07 14:02:44 -07001928 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001929 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001930 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1931 }
1932
Elliott Hughesdcc24742011-09-07 14:02:44 -07001933 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001934 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001935 }
1936
1937 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001938 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001939 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001940 }
1941
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001942 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001943 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001944 }
1945
Brian Carlstromc74255f2011-09-11 22:47:39 -07001946 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001947
Brian Carlstromc74255f2011-09-11 22:47:39 -07001948 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001949
jeffhaobdb76512011-09-07 11:43:16 -07001950 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001951 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001952 bool IsArrayAssignableFromArray(const Class* klass) const;
1953 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001954
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001955 // descriptor for the class such as "java.lang.Class" or "[C"
1956 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001957
Brian Carlstrom693267a2011-09-06 09:25:34 -07001958 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001959 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001960
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001961 // For array classes, the component class object for instanceof/checkcast
1962 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001963 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001964
Brian Carlstrom693267a2011-09-06 09:25:34 -07001965 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1966 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001967
Brian Carlstrom693267a2011-09-06 09:25:34 -07001968 // DexCache of resolved constant pool entries
1969 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1970 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001971
1972 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001973 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001974
Brian Carlstrom693267a2011-09-06 09:25:34 -07001975 // instance fields
1976 //
1977 // These describe the layout of the contents of an Object.
1978 // Note that only the fields directly declared by this class are
1979 // listed in ifields; fields declared by a superclass are listed in
1980 // the superclass's Class.ifields.
1981 //
1982 // All instance fields that refer to objects are guaranteed to be at
1983 // the beginning of the field list. num_reference_instance_fields_
1984 // specifies the number of reference fields.
1985 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001986
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001987 // Interface table (iftable_), one entry per interface supported by
1988 // this class. That means one entry for each interface we support
1989 // directly, indirectly via superclass, or indirectly via
1990 // superinterface. This will be null if neither we nor our
1991 // superclass implement any interfaces.
1992 //
1993 // Why we need this: given "class Foo implements Face", declare
1994 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1995 // is part of the Face interface. We can't easily use a single
1996 // vtable.
1997 //
1998 // For every interface a concrete class implements, we create an array
1999 // of the concrete vtable_ methods for the methods in the interface.
2000 ObjectArray<InterfaceEntry>* iftable_;
2001
Brian Carlstromdbc05252011-09-09 01:59:59 -07002002 // array of interfaces this class implements directly
2003 // see also interfaces_type_idx_
2004 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002005
2006 // array of type_idx's for interfaces this class implements directly
2007 // see also interfaces_
2008 IntArray* interfaces_type_idx_;
2009
Brian Carlstromdbc05252011-09-09 01:59:59 -07002010 // Static fields
2011 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002012
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002013 // source file name, if known. Otherwise, NULL.
2014 String* source_file_;
2015
Brian Carlstromdbc05252011-09-09 01:59:59 -07002016 // The superclass, or NULL if this is java.lang.Object or a
2017 // primitive type.
2018 // see also super_class_type_idx_;
2019 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002020
Brian Carlstromdbc05252011-09-09 01:59:59 -07002021 // If class verify fails, we must return same error on subsequent tries.
2022 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2023 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002024
Brian Carlstromdbc05252011-09-09 01:59:59 -07002025 // virtual methods defined in this class; invoked through vtable
2026 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002027
Brian Carlstromdbc05252011-09-09 01:59:59 -07002028 // Virtual method table (vtable), for use by "invoke-virtual". The
2029 // vtable from the superclass is copied in, and virtual methods from
2030 // our class either replace those from the super or are appended.
2031 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002032
Brian Carlstromdbc05252011-09-09 01:59:59 -07002033 // access flags; low 16 bits are defined by VM spec
2034 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002035
Jesse Wilson6bf19152011-09-29 13:12:33 -04002036 // Total size of the Class instance; used when allocating storage on gc heap.
2037 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002038 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002039
Elliott Hughes5f791332011-09-15 17:45:30 -07002040 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002041 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002042
Brian Carlstromdbc05252011-09-09 01:59:59 -07002043 // number of instance fields that are object refs
2044 size_t num_reference_instance_fields_;
2045
2046 // number of static fields that are object refs
2047 size_t num_reference_static_fields_;
2048
2049 // Total object size; used when allocating storage on gc heap.
2050 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002051 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002052 size_t object_size_;
2053
2054 // primitive type index, or kPrimNot (0); set for generated prim classes
2055 PrimitiveType primitive_type_;
2056
2057 // Bitmap of offsets of ifields.
2058 uint32_t reference_instance_offsets_;
2059
2060 // Bitmap of offsets of sfields.
2061 uint32_t reference_static_offsets_;
2062
Brian Carlstrom693267a2011-09-06 09:25:34 -07002063 // state of class initialization
2064 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002065
Brian Carlstrom693267a2011-09-06 09:25:34 -07002066 // Set in LoadClass, used to LinkClass
2067 // see also super_class_
2068 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002069
Brian Carlstrom693267a2011-09-06 09:25:34 -07002070 // TODO: ?
2071 // initiating class loader list
2072 // NOTE: for classes with low serialNumber, these are unused, and the
2073 // values are kept in a table in gDvm.
2074 // InitiatingLoaderList initiating_loader_list_;
2075
Brian Carlstrom4873d462011-08-21 15:23:39 -07002076 // Location of first static field.
2077 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002078
Brian Carlstrom693267a2011-09-06 09:25:34 -07002079 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002080 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002081};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002082
Elliott Hughes1f359b02011-07-17 14:27:17 -07002083std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002084
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002085inline void Object::SetClass(Class* new_klass) {
2086 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002087 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002088}
2089
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002090inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002091 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002092 DCHECK(GetClass() != NULL);
2093 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002094}
2095
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002096inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002097 Class* java_lang_Class = GetClass()->GetClass();
2098 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002099}
2100
Brian Carlstrom4873d462011-08-21 15:23:39 -07002101inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002102 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002103 return this == java_lang_Class;
2104}
2105
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002106inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002107 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002108}
2109
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002110template<class T>
2111inline ObjectArray<T>* Object::AsObjectArray() {
2112 DCHECK(IsObjectArray());
2113 return down_cast<ObjectArray<T>*>(this);
2114}
2115
2116template<class T>
2117inline const ObjectArray<T>* Object::AsObjectArray() const {
2118 DCHECK(IsObjectArray());
2119 return down_cast<const ObjectArray<T>*>(this);
2120}
2121
Brian Carlstromb63ec392011-08-27 17:38:27 -07002122inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002123 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002124}
2125
Brian Carlstroma663ea52011-08-19 23:33:41 -07002126inline bool Object::IsField() const {
2127 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002128 Class* java_lang_reflect_Field =
2129 java_lang_Class->GetInstanceField(0)->GetClass();
2130 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002131}
2132
2133inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002134 Class* c = GetClass();
2135 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002136}
2137
2138inline bool Object::IsReferenceInstance() const {
2139 return GetClass()->IsReferenceClass();
2140}
2141
2142inline bool Object::IsWeakReferenceInstance() const {
2143 return GetClass()->IsWeakReferenceClass();
2144}
2145
2146inline bool Object::IsSoftReferenceInstance() const {
2147 return GetClass()->IsSoftReferenceClass();
2148}
2149
2150inline bool Object::IsFinalizerReferenceInstance() const {
2151 return GetClass()->IsFinalizerReferenceClass();
2152}
2153
2154inline bool Object::IsPhantomReferenceInstance() const {
2155 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002156}
2157
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002158inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002159 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002160 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002161 result = AsArray()->SizeOf();
2162 } else if (IsClass()) {
2163 result = AsClass()->SizeOf();
2164 } else {
2165 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002166 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002167 DCHECK(!IsField() || result == sizeof(Field));
2168 DCHECK(!IsMethod() || result == sizeof(Method));
2169 return result;
2170}
2171
2172inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002173 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002174 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002175 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2176 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002177 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002178 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2179 num_bytes.Uint32Value(), false);
2180}
2181
2182inline Class* Field::GetDeclaringClass() const {
2183 Class* result = GetFieldObject<Class*>(
2184 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2185 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002186 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002187 return result;
2188}
2189
2190inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2191 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2192 new_declaring_class, false);
2193}
2194
2195inline Class* Method::GetDeclaringClass() const {
2196 Class* result =
2197 GetFieldObject<Class*>(
2198 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2199 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002200 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002201 return result;
2202}
2203
2204inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2205 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2206 new_declaring_class, false);
2207}
2208
2209inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002210 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002211 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2212 false);
2213}
2214
2215inline bool Method::IsReturnAReference() const {
2216 return !GetReturnType()->IsPrimitive();
2217}
2218
2219inline bool Method::IsReturnAFloat() const {
2220 return GetReturnType()->IsPrimitiveFloat();
2221}
2222
2223inline bool Method::IsReturnADouble() const {
2224 return GetReturnType()->IsPrimitiveDouble();
2225}
2226
2227inline bool Method::IsReturnALong() const {
2228 return GetReturnType()->IsPrimitiveLong();
2229}
2230
2231inline bool Method::IsReturnVoid() const {
2232 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002233}
2234
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002235inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002236 // This is safe from overflow because the array was already allocated, so we know it's sane.
2237 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002238}
2239
2240template<class T>
2241void ObjectArray<T>::Set(int32_t i, T* object) {
2242 if (IsValidIndex(i)) {
2243 if (object != NULL) {
2244 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002245 if (!object->InstanceOf(element_class)) {
2246 ThrowArrayStoreException(object);
2247 return;
2248 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002249 }
2250 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2251 SetFieldObject(data_offset, object, false);
2252 }
2253}
2254
2255template<class T>
2256void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2257 DCHECK(IsValidIndex(i));
2258 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2259 SetFieldObject(data_offset, object, false);
2260}
2261
2262template<class T>
2263void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2264 ObjectArray<T>* dst, int dst_pos,
2265 size_t length) {
2266 if (src->IsValidIndex(src_pos) &&
2267 src->IsValidIndex(src_pos+length-1) &&
2268 dst->IsValidIndex(dst_pos) &&
2269 dst->IsValidIndex(dst_pos+length-1)) {
2270 MemberOffset src_offset(DataOffset().Int32Value() +
2271 src_pos * sizeof(Object*));
2272 MemberOffset dst_offset(DataOffset().Int32Value() +
2273 dst_pos * sizeof(Object*));
2274 Class* array_class = dst->GetClass();
2275 if (array_class == src->GetClass()) {
2276 // No need for array store checks if arrays are of the same type
2277 for (size_t i=0; i < length; i++) {
2278 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2279 dst->SetFieldObject(dst_offset, object, false);
2280 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2281 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2282 }
2283 } else {
2284 Class* element_class = array_class->GetComponentType();
2285 CHECK(!element_class->IsPrimitive());
2286 for (size_t i=0; i < length; i++) {
2287 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002288 if (object != NULL && !object->InstanceOf(element_class)) {
2289 dst->ThrowArrayStoreException(object);
2290 return;
2291 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002292 dst->SetFieldObject(dst_offset, object, false);
2293 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2294 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2295 }
2296 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002297 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002298 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002299}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002300
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002301class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002302 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002303 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002304 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002305 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002306 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2307};
2308
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002309class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002310 private:
2311 CharArray* ASCII_;
2312 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002313 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002314 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002315 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002316 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2317};
2318
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002319class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002320 private:
2321 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2322 uint32_t TYPE_BOOLEAN_;
2323 uint32_t TYPE_BYTE_;
2324 uint32_t TYPE_CHAR_;
2325 uint32_t TYPE_DOUBLE_;
2326 uint32_t TYPE_FLOAT_;
2327 uint32_t TYPE_INTEGER_;
2328 uint32_t TYPE_LONG_;
2329 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002330 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002331 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2332};
2333
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002334class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002335 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002336 Object* ORDER_BY_SIGNATURE_;
2337 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002338 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2339};
2340
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002341template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002342class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002343 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002344 typedef T ElementType;
2345
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002346 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002347
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002348 const T* GetData() const {
2349 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002350 }
2351
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002352 T* GetData() {
2353 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002354 }
2355
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002356 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002357 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002358 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002359 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002360 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002361 }
2362
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002363 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002364 // TODO: ArrayStoreException
2365 if (IsValidIndex(i)) {
2366 GetData()[i] = value;
2367 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002368 }
2369
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002370 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002371 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002372 CHECK(array_class != NULL);
2373 array_class_ = array_class;
2374 }
2375
Brian Carlstroma663ea52011-08-19 23:33:41 -07002376 static void ResetArrayClass() {
2377 CHECK(array_class_ != NULL);
2378 array_class_ = NULL;
2379 }
2380
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002381 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002382 // Location of first element.
2383 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002384
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002385 static Class* array_class_;
2386
Carl Shapirof88c9522011-08-06 15:47:38 -07002387 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002388};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002389
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002390inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2391 DCHECK(NULL == GetFieldObject<IntArray*>(
2392 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2393 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2394 new_interfaces_idx, false);
2395}
2396
2397// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002398class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002399 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002400 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002401 const CharArray* result = GetFieldObject<const CharArray*>(
2402 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2403 DCHECK(result != NULL);
2404 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002405 }
2406
Elliott Hughes814e4032011-08-23 12:07:56 -07002407 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002408 int32_t result = GetField32(
2409 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2410 DCHECK_LE(0, result);
2411 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002412 }
2413
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002414 int32_t GetLength() const;
2415
Brian Carlstrom395520e2011-09-25 19:35:00 -07002416 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002417
2418 void ComputeHashCode() {
2419 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002420 }
2421
Elliott Hughes814e4032011-08-23 12:07:56 -07002422 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002423 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002424 }
2425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002426 uint16_t CharAt(int32_t index) const;
2427
Brian Carlstromc74255f2011-09-11 22:47:39 -07002428 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002429
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002430 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002431 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002432 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002433
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002434 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002435
Jesse Wilson8989d992011-08-02 13:39:42 -07002436 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002437 const char* utf8_data_in);
2438
2439 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2440
2441 static String* Alloc(Class* java_lang_String, CharArray* array);
2442
2443 bool Equals(const char* modified_utf8) const;
2444
2445 // TODO: do we need this overload? give it a more intention-revealing name.
2446 bool Equals(const StringPiece& modified_utf8) const;
2447
2448 bool Equals(const String* that) const;
2449
2450 // TODO: do we need this overload? give it a more intention-revealing name.
2451 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2452 int32_t that_length) const;
2453
2454 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2455 std::string ToModifiedUtf8() const;
2456
2457 static Class* GetJavaLangString() {
2458 DCHECK(java_lang_String_ != NULL);
2459 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002460 }
2461
Brian Carlstroma663ea52011-08-19 23:33:41 -07002462 static void SetClass(Class* java_lang_String);
2463 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002464
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002465 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002466 void SetHashCode(int32_t new_hash_code) {
2467 DCHECK_EQ(0u,
2468 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2469 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2470 new_hash_code, false);
2471 }
2472
2473 void SetCount(int32_t new_count) {
2474 DCHECK_LE(0, new_count);
2475 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2476 }
2477
2478 void SetOffset(int32_t new_offset) {
2479 DCHECK_LE(0, new_offset);
2480 DCHECK_GE(GetLength(), new_offset);
2481 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2482 }
2483
2484 void SetArray(CharArray* new_array) {
2485 DCHECK(new_array != NULL);
2486 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2487 }
2488
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002489 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2490 CharArray* array_;
2491
Brian Carlstromdbc05252011-09-09 01:59:59 -07002492 int32_t count_;
2493
Carl Shapirof88c9522011-08-06 15:47:38 -07002494 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002495
Elliott Hughes289da822011-08-16 10:11:20 -07002496 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002497
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002498 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002499
Brian Carlstrom693267a2011-09-06 09:25:34 -07002500 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002501 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002502};
2503
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002504inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002505 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002506 String* result =
2507 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2508 DCHECK(result != NULL);
2509 return result;
2510}
2511
2512inline void Field::SetName(String* new_name) {
2513 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2514 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002515}
2516
2517inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002518 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002519 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2520}
2521
2522inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002523 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002524 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2525}
2526
2527inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002528 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002529 return MemberOffset(
2530 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2531}
2532
2533inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002534 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002535 return MemberOffset(
2536 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2537}
2538
2539inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002540 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002541 const String* result =
2542 GetFieldObject<const String*>(
2543 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2544 DCHECK(result != NULL);
2545 return result;
2546}
2547
2548inline void Method::SetName(String* new_name) {
2549 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2550 new_name, false);
2551
2552}
2553
jeffhaoe23d93c2011-09-15 14:48:43 -07002554inline ByteArray* Method::GetRegisterMapData() const {
2555 return GetFieldObject<ByteArray*>(
2556 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2557}
2558
jeffhaoa0a764a2011-09-16 10:43:38 -07002559inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002560 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002561 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002562}
2563
2564inline ByteArray* Method::GetRegisterMapHeader() const {
2565 return GetFieldObject<ByteArray*>(
2566 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2567}
2568
jeffhaoa0a764a2011-09-16 10:43:38 -07002569inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002570 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002571 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002572}
2573
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002574inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002575 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002576 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002577 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2578}
2579
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002580inline void Method::SetShorty(String* new_shorty) {
2581 DCHECK(NULL == GetFieldObject<String*>(
2582 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2583 DCHECK_LE(1, new_shorty->GetLength());
2584 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2585}
2586
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002587inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002588 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002589 const String* result =
2590 GetFieldObject<const String*>(
2591 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2592 DCHECK(result != NULL);
2593 return result;
2594}
2595
2596inline void Method::SetSignature(String* new_signature) {
2597 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2598 new_signature, false);
2599}
2600
2601inline uint32_t Class::GetAccessFlags() const {
2602 // Check class is loaded or this is java.lang.String that has a
2603 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002604 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002605 this == String::GetJavaLangString() ||
2606 this == Field::GetJavaLangReflectField() ||
2607 this == Method::GetConstructorClass() ||
2608 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002609 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2610}
2611
2612inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002613 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002614 DCHECK(new_descriptor != NULL);
2615 DCHECK_NE(0, new_descriptor->GetLength());
2616 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2617 new_descriptor, false);
2618}
2619
Brian Carlstromc74255f2011-09-11 22:47:39 -07002620inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002621 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002622 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2623}
2624
2625inline void Class::SetSourceFile(String* new_source_file) {
2626 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2627}
2628
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002629inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002630 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002631 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2632}
2633
2634inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002635 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002636 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002637}
2638
2639inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002640 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002641 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002642}
2643
2644inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002645 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002646 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002647}
2648
2649inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002650 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002651 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002652}
2653
2654inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002655 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002656 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2657}
2658
2659// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002660class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002661 public:
2662 void SetDetailMessage(String* new_detail_message) {
2663 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2664 new_detail_message, false);
2665 }
2666
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002667 private:
2668 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2669 Throwable* cause_;
2670 String* detail_message_;
2671 Object* stack_state_; // Note this is Java volatile:
2672 Object* stack_trace_;
2673 Object* suppressed_exceptions_;
2674
Brian Carlstrom693267a2011-09-06 09:25:34 -07002675 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002676 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2677};
2678
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002679// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002680class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002681 public:
2682 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002683 return GetFieldObject<const String*>(
2684 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002685 }
2686
2687 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002688 return GetFieldObject<const String*>(
2689 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002690 }
2691
2692 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002693 return GetFieldObject<const String*>(
2694 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002695 }
2696
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002697 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002698 return GetField32(
2699 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002700 }
2701
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002702 static StackTraceElement* Alloc(const String* declaring_class,
2703 const String* method_name,
2704 const String* file_name,
2705 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002706
2707 static void SetClass(Class* java_lang_StackTraceElement);
2708
2709 static void ResetClass();
2710
2711 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002712 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002713 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002714 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002715 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002716 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002717
2718 static Class* GetStackTraceElement() {
2719 DCHECK(java_lang_StackTraceElement_ != NULL);
2720 return java_lang_StackTraceElement_;
2721 }
2722
2723 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002724
2725 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002726 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2727};
2728
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002729class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002730 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002731 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002732 Class* interface = Get(kInterface)->AsClass();
2733 DCHECK(interface != NULL);
2734 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002735 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002736
Brian Carlstrom30b94452011-08-25 21:35:26 -07002737 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002738 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002739 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002740 DCHECK(Get(kInterface) == NULL);
2741 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002742 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002743
Brian Carlstrom86927212011-09-15 11:31:11 -07002744 size_t GetMethodArrayCount() const {
2745 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2746 if (method_array == 0) {
2747 return 0;
2748 }
2749 return method_array->GetLength();
2750 }
2751
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002752 ObjectArray<Method>* GetMethodArray() const {
2753 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2754 DCHECK(method_array != NULL);
2755 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002756 }
2757
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002758 void SetMethodArray(ObjectArray<Method>* new_ma) {
2759 DCHECK(new_ma != NULL);
2760 DCHECK(Get(kMethodArray) == NULL);
2761 Set(kMethodArray, new_ma);
2762 }
2763
2764 static size_t LengthAsArray() {
2765 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002766 }
2767
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002768 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002769
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002770 enum ArrayIndex {
2771 // Points to the interface class.
2772 kInterface = 0,
2773 // Method pointers into the vtable, allow fast map from interface
2774 // method index to concrete instance method.
2775 kMethodArray = 1,
2776 kMax = 2,
2777 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002778
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002779 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002780};
2781
2782} // namespace art
2783
2784#endif // ART_SRC_OBJECT_H_