blob: 5b4c7ba1d56d3512cbb10328e77b76bd618e8fec [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
80static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
81static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Brian Carlstrombe977852011-07-19 14:54:54 -070097static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
jeffhaobdb76512011-09-07 11:43:16 -070099static const uint32_t kAccWritable = 0x80000000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700100
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101static const uint32_t kAccClassFlagsMask = (kAccPublic
102 | kAccFinal
103 | kAccInterface
104 | kAccAbstract
105 | kAccSynthetic
106 | kAccAnnotation
107 | kAccEnum);
108static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
109 | kAccPrivate
110 | kAccProtected
111 | kAccStatic);
112static const uint32_t kAccFieldFlagsMask = (kAccPublic
113 | kAccPrivate
114 | kAccProtected
115 | kAccStatic
116 | kAccFinal
117 | kAccVolatile
118 | kAccTransient
119 | kAccSynthetic
120 | kAccEnum);
121static const uint32_t kAccMethodFlagsMask = (kAccPublic
122 | kAccPrivate
123 | kAccProtected
124 | kAccStatic
125 | kAccFinal
126 | kAccSynchronized
127 | kAccBridge
128 | kAccVarargs
129 | kAccNative
130 | kAccAbstract
131 | kAccStrict
132 | kAccSynthetic
133 | kAccConstructor
134 | kAccDeclaredSynchronized);
135
136// if only kAccClassIsReference is set, we have a soft reference
137static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
138static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
139static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
140static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
141
142static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
143 | kAccClassIsWeakReference
144 | kAccClassIsFinalizerReference
145 | kAccClassIsPhantomReference);
146
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700148 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 */
150/*
151 * A magic value for refOffsets. Ignore the bits and walk the super
152 * chain when this is the value.
153 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
154 * fields followed by 2 ref instance fields.]
155 */
156#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
158#define CLASS_OFFSET_ALIGNMENT 4
159#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
160/*
161 * Given an offset, return the bit number which would encode that offset.
162 * Local use only.
163 */
164#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700165 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166 CLASS_OFFSET_ALIGNMENT)
167/*
168 * Is the given offset too large to be encoded?
169 */
170#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
171 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
172/*
173 * Return a single bit, encoding the offset.
174 * Undefined if the offset is too large, as defined above.
175 */
176#define CLASS_BIT_FROM_OFFSET(byteOffset) \
177 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
178/*
179 * Return an offset, given a bit number as returned from CLZ.
180 */
181#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700182 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700183
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184#define OFFSET_OF_OBJECT_MEMBER(type, field) \
185 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700186
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700187// Classes shared with the managed side of the world need to be packed
188// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700189#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700191// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700192class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700193 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700194 static bool InstanceOf(const Object* object, const Class* klass) {
195 if (object == NULL) {
196 return false;
197 }
198 return object->InstanceOf(klass);
199 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700200
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700201 static MemberOffset ClassOffset() {
202 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700203 }
204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700206 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 }
208
209 void SetClass(Class* new_klass);
210
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700211 bool InstanceOf(const Class* klass) const;
212
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700213 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214
Elliott Hughes081be7f2011-09-18 16:50:26 -0700215 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700216
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 static MemberOffset MonitorOffset() {
218 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
219 }
220
Elliott Hughes5f791332011-09-15 17:45:30 -0700221 volatile int32_t* GetRawLockWordAddress() {
222 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
223 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
224 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225 }
226
Elliott Hughes5f791332011-09-15 17:45:30 -0700227 uint32_t GetLockOwner();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228
Elliott Hughes5f791332011-09-15 17:45:30 -0700229 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700230
Ian Rogersff1ed472011-09-20 13:46:24 -0700231 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Elliott Hughes5f791332011-09-15 17:45:30 -0700233 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Elliott Hughes5f791332011-09-15 17:45:30 -0700235 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236
Elliott Hughes5f791332011-09-15 17:45:30 -0700237 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238
Elliott Hughes5f791332011-09-15 17:45:30 -0700239 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700240
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700241 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700242
243 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700244 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 return down_cast<Class*>(this);
246 }
247
248 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 return down_cast<const Class*>(this);
251 }
252
Brian Carlstrom4873d462011-08-21 15:23:39 -0700253 bool IsClassClass() const;
254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 bool IsObjectArray() const;
256
257 template<class T>
258 ObjectArray<T>* AsObjectArray() {
259 DCHECK(IsObjectArray());
260 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700261 }
262
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700263 template<class T>
264 const ObjectArray<T>* AsObjectArray() const {
265 DCHECK(IsObjectArray());
266 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700267 }
268
Brian Carlstromb63ec392011-08-27 17:38:27 -0700269 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700270
271 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700272 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700273 return down_cast<Array*>(this);
274 }
275
276 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700277 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700278 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700279 }
280
Brian Carlstroma663ea52011-08-19 23:33:41 -0700281 bool IsString() const;
282
283 String* AsString() {
284 DCHECK(IsString());
285 return down_cast<String*>(this);
286 }
287
288 bool IsMethod() const;
289
290 Method* AsMethod() {
291 DCHECK(IsMethod());
292 return down_cast<Method*>(this);
293 }
294
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 const Method* AsMethod() const {
296 DCHECK(IsMethod());
297 return down_cast<const Method*>(this);
298 }
299
Brian Carlstroma663ea52011-08-19 23:33:41 -0700300 bool IsField() const;
301
302 Field* AsField() {
303 DCHECK(IsField());
304 return down_cast<Field*>(this);
305 }
306
Brian Carlstrom4873d462011-08-21 15:23:39 -0700307 const Field* AsField() const {
308 DCHECK(IsField());
309 return down_cast<const Field*>(this);
310 }
311
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700312 bool IsReferenceInstance() const;
313
314 bool IsWeakReferenceInstance() const;
315
316 bool IsSoftReferenceInstance() const;
317
318 bool IsFinalizerReferenceInstance() const;
319
320 bool IsPhantomReferenceInstance() const;
321
322 // Accessors for Java type fields
323 template<class T>
324 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700325 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
326 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700327 Heap::VerifyObject(result);
328 return result;
329 }
330
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700331 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700333 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
334 if (new_value != NULL) {
335 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
338
339 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
340 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700341 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
342 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700344 return android_atomic_acquire_load(word_addr);
345 } else {
346 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
349
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700350 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
351 if (this_is_valid) {
352 Heap::VerifyObject(this);
353 }
354 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700355 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 if (is_volatile) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700357 /*
358 * TODO: add an android_atomic_synchronization_store() function and
359 * use it in the 32-bit volatile set handlers. On some platforms we
360 * can use a fast atomic instruction and avoid the barriers.
361 */
362 ANDROID_MEMBAR_STORE();
363 *word_addr = new_value;
364 ANDROID_MEMBAR_FULL();
365 } else {
366 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700367 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
369
370 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
371 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700372 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700373 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700374 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700375 uint64_t result = QuasiAtomicRead64(addr);
376 ANDROID_MEMBAR_FULL();
377 return result;
378 } else {
379 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 }
382
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700383 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700385 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
386 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 if (is_volatile) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700388 ANDROID_MEMBAR_STORE();
389 QuasiAtomicSwap64(new_value, addr);
390 // Post-store barrier not required due to use of atomic op or mutex.
391 } else {
392 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700393 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
395
396 protected:
397 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 template<class T>
399 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700400 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700401 }
402
403 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700404 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
405 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700406 }
407
408 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700409 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700410
Elliott Hughes5f791332011-09-15 17:45:30 -0700411 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700412
Elliott Hughes5f791332011-09-15 17:45:30 -0700413 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700414 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700415 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700416};
417
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700418// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700419class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400420 private:
421 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700423 friend struct AccessibleObjectOffsets; // for verifying offset information
424 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400425};
426
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700427// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700428class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700429 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700430 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 void SetDeclaringClass(Class *new_declaring_class);
433
434 const String* GetName() const;
435
436 void SetName(String* new_name);
437
438 uint32_t GetAccessFlags() const;
439
440 void SetAccessFlags(uint32_t new_access_flags) {
441 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags,
442 false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700443 }
444
445 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700446 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447 }
448
jeffhaobdb76512011-09-07 11:43:16 -0700449 bool IsFinal() const {
450 return (access_flags_ & kAccFinal) != 0;
451 }
452
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700453 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700454
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 // Gets type using type index and resolved types in the dex cache, may be null
458 // if type isn't yet resolved
459 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700460
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700461 // Performs full resolution, may return null and set exceptions if type cannot
462 // be resolved
463 Class* GetType() const;
464
465 // Offset to field within an Object
466 MemberOffset GetOffset() const;
467
buzbee34cd9e52011-09-08 14:31:52 -0700468 static MemberOffset OffsetOffset() {
469 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
470 }
471
Brian Carlstrom845490b2011-09-19 15:56:53 -0700472 static Field* FindInstanceFieldFromCode(uint32_t field_idx, const Method* referrer);
473 static Field* FindStaticFieldFromCode(uint32_t field_idx, const Method* referrer);
474 static Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, bool is_static);
buzbee34cd9e52011-09-08 14:31:52 -0700475
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700476 MemberOffset GetOffsetDuringLinking() const;
477
478 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700479
Brian Carlstrom4873d462011-08-21 15:23:39 -0700480 // field access, null object for static fields
481 bool GetBoolean(const Object* object) const;
482 void SetBoolean(Object* object, bool z) const;
483 int8_t GetByte(const Object* object) const;
484 void SetByte(Object* object, int8_t b) const;
485 uint16_t GetChar(const Object* object) const;
486 void SetChar(Object* object, uint16_t c) const;
487 uint16_t GetShort(const Object* object) const;
488 void SetShort(Object* object, uint16_t s) const;
489 int32_t GetInt(const Object* object) const;
490 void SetInt(Object* object, int32_t i) const;
491 int64_t GetLong(const Object* object) const;
492 void SetLong(Object* object, int64_t j) const;
493 float GetFloat(const Object* object) const;
494 void SetFloat(Object* object, float f) const;
495 double GetDouble(const Object* object) const;
496 void SetDouble(Object* object, double d) const;
497 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700498 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700499
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700500 // Slow path routines for static field access when field was unresolved at
501 // compile time
502 static uint32_t Get32StaticFromCode(uint32_t field_idx,
503 const Method* referrer);
504 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer,
505 uint32_t new_value);
506 static uint64_t Get64StaticFromCode(uint32_t field_idx,
507 const Method* referrer);
508 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer,
509 uint64_t new_value);
510 static Object* GetObjStaticFromCode(uint32_t field_idx,
511 const Method* referrer);
512 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer,
513 Object* new_value);
Brian Carlstromb9edb842011-08-28 16:31:06 -0700514
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700515 static Class* GetJavaLangReflectField() {
516 DCHECK(java_lang_reflect_Field_ != NULL);
517 return java_lang_reflect_Field_;
518 }
519
520 static void SetClass(Class* java_lang_reflect_Field);
521 static void ResetClass();
522
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523 bool IsVolatile() const {
524 return (GetAccessFlags() & kAccVolatile) != 0;
525 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700526
buzbee1da522d2011-09-04 11:22:20 -0700527 private:
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700528 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700529 uint32_t Get32(const Object* object) const;
530 void Set32(Object* object, uint32_t new_value) const;
531 uint64_t Get64(const Object* object) const;
532 void Set64(Object* object, uint64_t new_value) const;
533 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700534 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700535
Jesse Wilson35baaab2011-08-10 16:18:03 -0400536 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700537
Jesse Wilson35baaab2011-08-10 16:18:03 -0400538 // The class in which this field is declared.
539 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700540
Jesse Wilson35baaab2011-08-10 16:18:03 -0400541 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700542
Brian Carlstromdbc05252011-09-09 01:59:59 -0700543 const String* name_;
544
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545 // Type of the field
Jesse Wilson35baaab2011-08-10 16:18:03 -0400546 Class* type_;
547
Brian Carlstromdbc05252011-09-09 01:59:59 -0700548 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700549
Jesse Wilson35baaab2011-08-10 16:18:03 -0400550 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700551
552 // Offset of field within an instance or in the Class' static fields
553 uint32_t offset_;
554
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 // Dex cache index of resolved type
556 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400557
Brian Carlstrom693267a2011-09-06 09:25:34 -0700558 int32_t slot_;
559
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560 static Class* java_lang_reflect_Field_;
561
Brian Carlstrom693267a2011-09-06 09:25:34 -0700562 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400563 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700564};
565
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566// C++ mirror of java.lang.reflect.Method
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700567class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700569 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700570 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700571 Object* obj,
572 Thread* thread,
573 byte* args,
574 JValue* result);
575
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700576 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700578 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700580 static MemberOffset DeclaringClassOffset() {
581 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700582 }
583
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700584 // Returns the method name, e.g. "<init>" or "eatLunch"
585 const String* GetName() const;
586
587 void SetName(String* new_name);
588
jeffhaoe23d93c2011-09-15 14:48:43 -0700589 ByteArray* GetRegisterMapData() const;
590
jeffhaoa0a764a2011-09-16 10:43:38 -0700591 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700592
593 ByteArray* GetRegisterMapHeader() const;
594
jeffhaoa0a764a2011-09-16 10:43:38 -0700595 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700596
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700597 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700599 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700600
601 const String* GetSignature() const;
602
603 void SetSignature(String* new_signature);
604
605 bool HasSameNameAndDescriptor(const Method* that) const;
606
607 uint32_t GetAccessFlags() const;
608
609 void SetAccessFlags(uint32_t new_access_flags) {
610 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags,
611 false);
612 }
613
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700614 // Returns true if the method is declared public.
615 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700616 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700617 }
618
619 // Returns true if the method is declared private.
620 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 }
623
624 // Returns true if the method is declared static.
625 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700626 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700627 }
628
Brian Carlstrom1f870082011-08-23 16:02:11 -0700629 // Returns true if the method is a constructor.
630 bool IsConstructor() const {
631 return (access_flags_ & kAccConstructor) != 0;
632 }
633
634 // Returns true if the method is static, private, or a constructor.
635 bool IsDirect() const {
636 return IsStatic() || IsPrivate() || IsConstructor();
637 }
638
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700639 // Returns true if the method is declared synchronized.
640 bool IsSynchronized() const {
641 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700642 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700643 }
644
645 // Returns true if the method is declared final.
646 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 }
649
650 // Returns true if the method is declared native.
651 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700652 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700653 }
654
655 // Returns true if the method is declared abstract.
656 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 }
659
660 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700661 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700662 }
663
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664 uint16_t GetMethodIndex() const;
665
666 size_t GetVtableIndex() const {
667 return GetMethodIndex();
668 }
669
670 void SetMethodIndex(uint16_t new_method_index) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700671 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700672 new_method_index, false);
673 }
674
675 static MemberOffset MethodIndexOffset() {
676 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
677 }
678
679 uint32_t GetCodeItemOffset() const {
680 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
681 }
682
683 void SetCodeItemOffset(uint32_t new_code_off) {
684 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_),
685 new_code_off, false);
686 }
687
688 // Number of 32bit registers that would be required to hold all the arguments
689 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700691 // Number of argument bytes required for densely packing the
692 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700693 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 uint16_t NumRegisters() const;
696
697 void SetNumRegisters(uint16_t new_num_registers) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700698 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699 new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700700 }
701
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700702 uint16_t NumIns() const;
703
704 void SetNumIns(uint16_t new_num_ins) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700705 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700706 new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700707 }
708
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700709 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700710
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700711 void SetNumOuts(uint16_t new_num_outs) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700712 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_),
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713 new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700714 }
715
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700716 uint32_t GetProtoIdx() const;
717
718 void SetProtoIdx(uint32_t new_proto_idx) {
719 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700720 }
721
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700722 ObjectArray<String>* GetDexCacheStrings() const;
723 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
724
725 static MemberOffset DexCacheStringsOffset() {
726 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
727 }
728
buzbee2a475e72011-09-07 17:19:17 -0700729 static MemberOffset DexCacheResolvedTypesOffset() {
730 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
731 }
732
buzbee34cd9e52011-09-08 14:31:52 -0700733 static MemberOffset DexCacheResolvedFieldsOffset() {
734 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
735 }
736
buzbee1da522d2011-09-04 11:22:20 -0700737 static MemberOffset DexCacheInitializedStaticStorageOffset() {
738 return OFFSET_OF_OBJECT_MEMBER(Method,
739 dex_cache_initialized_static_storage_);
740 }
741
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700742 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
743 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
744
745 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
746 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
747
748 ObjectArray<Field>* GetDexCacheResolvedFields() const;
749 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
750
751 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
752 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
753
754 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
755 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
756
757 void SetReturnTypeIdx(uint32_t new_return_type_idx);
758
759 Class* GetReturnType() const;
760
761 bool IsReturnAReference() const;
762
763 bool IsReturnAFloat() const;
764
765 bool IsReturnADouble() const;
766
Ian Rogersb033c752011-07-20 12:22:35 -0700767 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700768 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700769 }
770
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700771 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700772
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700773 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700774
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700775 // "Args" may refer to any of the 3 levels of "Args."
776 // To avoid confusion, our code will denote which "Args" clearly:
777 // 1. UserArgs: Args that a user see.
778 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
779 // receiver.
780 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
781 // E.g., the first in Args is Method* for both static and non-static
782 // methods. And CConvArgs doesn't deal with the receiver because
783 // receiver is hardwired in an implicit register, so CConvArgs doesn't
784 // need to deal with it.
785 //
786 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700787 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700788
789 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700790 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700791 size_t NumReferenceArgs() const;
792
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700793 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700794 size_t NumLongOrDoubleArgs() const;
795
Ian Rogersb033c752011-07-20 12:22:35 -0700796 // Is the given method parameter a reference?
797 bool IsParamAReference(unsigned int param) const;
798
799 // Is the given method parameter a long or double?
800 bool IsParamALongOrDouble(unsigned int param) const;
801
Ian Rogersdf20fe02011-07-20 20:34:16 -0700802 // Size in bytes of the given parameter
803 size_t ParamSize(unsigned int param) const;
804
805 // Size in bytes of the return value
806 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700807
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700808 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
809
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700810 const ByteArray* GetCodeArray() const {
811 return GetFieldPtr<const ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, code_array_), false);
812 }
813
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700814 const void* GetCode() const {
815 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700816 }
817
buzbee4ef76522011-09-08 10:00:32 -0700818 void SetCode(ByteArray* code_array, InstructionSet instruction_set,
buzbeec41e5b52011-09-23 12:46:19 -0700819 IntArray* mapping_table = NULL, ShortArray* vmap_table = NULL);
buzbeec143c552011-08-20 17:38:58 -0700820
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700821 static MemberOffset GetCodeOffset() {
822 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700823 }
824
Ian Rogersbdb03912011-09-14 00:55:44 -0700825 // Is the given PC within the code array?
826 bool IsWithinCode(uintptr_t pc) const;
827
828 IntArray* GetMappingTable() const {
829 return GetFieldObject<IntArray*>(
830 OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
831 }
832
buzbeec41e5b52011-09-23 12:46:19 -0700833 ShortArray* GetVMapTable() const {
834 return GetFieldObject<ShortArray*>(
835 OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
836 }
837
Shih-wei Liaod11af152011-08-23 16:02:11 -0700838 size_t GetFrameSizeInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839 DCHECK(sizeof(size_t) == sizeof(uint32_t));
840 size_t result = GetField32(
841 OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
842 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
843 return result;
844 }
845
846 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
847 DCHECK(sizeof(size_t) == sizeof(uint32_t));
848 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
849 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
850 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700851 }
852
Shih-wei Liaod11af152011-08-23 16:02:11 -0700853 size_t GetReturnPcOffsetInBytes() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 DCHECK(sizeof(size_t) == sizeof(uint32_t));
855 return GetField32(
856 OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700857 }
858
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700859 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
860 DCHECK(sizeof(size_t) == sizeof(uint32_t));
861 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
862 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
863 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700864 }
865
Brian Carlstrom16192862011-09-12 17:50:06 -0700866 bool IsRegistered();
Elliott Hughesd369bb72011-09-12 14:41:14 -0700867
Brian Carlstrom16192862011-09-12 17:50:06 -0700868 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700869
Brian Carlstrom16192862011-09-12 17:50:06 -0700870 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700871
Ian Rogersb033c752011-07-20 12:22:35 -0700872 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700873 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700874 }
875
Brian Carlstrom78128a62011-09-15 17:21:19 -0700876 const void* GetNativeMethod() const {
877 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
878 }
879
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700880 ByteArray* GetInvokeStubArray() const {
881 ByteArray* result = GetFieldPtr<ByteArray*>(
882 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_array_), false);
883 // TODO: DCHECK(result != NULL); should be ahead of time compiled
884 return result;
885 }
886
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700887 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700888 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700889 InvokeStub* result = GetFieldPtr<InvokeStub*>(
890 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
891 // TODO: DCHECK(result != NULL); should be ahead of time compiled
892 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700893 }
894
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700895 static MemberOffset GetInvokeStubOffset() {
896 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700897 }
898
buzbee561227c2011-09-02 15:28:19 -0700899 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
900 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
901 }
902
903 static MemberOffset GetDexCacheResolvedMethodsOffset() {
904 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
905 }
906
907 static MemberOffset GetMethodIndexOffset() {
908 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
909 }
910
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700911 void SetInvokeStub(const ByteArray* invoke_stub_array);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700912
Ian Rogers90865722011-09-19 11:11:44 -0700913 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700914 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700915 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700916
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700917 void SetCoreSpillMask(uint32_t core_spill_mask) {
918 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700919 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700920 }
921
Ian Rogers90865722011-09-19 11:11:44 -0700922 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700923 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
924 }
925
926 void SetFpSpillMask(uint32_t fp_spill_mask) {
927 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700928 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700929 }
930
Ian Rogers90865722011-09-19 11:11:44 -0700931 // Is this a hand crafted method used for something like describing callee saves?
932 bool IsPhony() const {
Ian Rogersff1ed472011-09-20 13:46:24 -0700933 bool result = this == Runtime::Current()->GetCalleeSaveMethod();
Ian Rogers90865722011-09-19 11:11:44 -0700934 // Check that if we do think it is phony it looks like the callee save method
935 DCHECK(!result || GetCoreSpillMask() != 0);
936 return result;
937 }
938
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700939 // Converts a native PC to a dex PC. TODO: this is a no-op
940 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700941 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700942
943 // Converts a dex PC to a native PC. TODO: this is a no-op
944 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700945 uintptr_t ToNativePC(const uint32_t dex_pc) const;
946
947 // Find the catch block for the given exception type and dex_pc
948 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700949
950 static Class* GetJavaLangReflectMethod() {
951 DCHECK(java_lang_reflect_Method_ != NULL);
952 return java_lang_reflect_Method_;
953 }
954
955 static void SetClass(Class* java_lang_reflect_Method);
Ian Rogersff1ed472011-09-20 13:46:24 -0700956 static Class* GetMethodClass() { return java_lang_reflect_Method_; }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957 static void ResetClass();
958
Elliott Hughes418d20f2011-09-22 14:00:39 -0700959 void InitJavaFields();
960
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700961 private:
962 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700963 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700964
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700965 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
966 // the class we are a part of
967 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700968 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700969 Object* java_formal_type_parameters_;
970 Object* java_generic_exception_types_;
971 Object* java_generic_parameter_types_;
972 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700973
Brian Carlstrom693267a2011-09-06 09:25:34 -0700974 String* name_;
975
Elliott Hughes418d20f2011-09-22 14:00:39 -0700976 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700977 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700978 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700979
Brian Carlstrom693267a2011-09-06 09:25:34 -0700980 // Storage for code_
981 const ByteArray* code_array_;
982
983 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700984 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
985
986 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
987 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
988
989 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
990 ObjectArray<Field>* dex_cache_resolved_fields_;
991
992 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
993 ObjectArray<Method>* dex_cache_resolved_methods_;
994
Brian Carlstromdbc05252011-09-09 01:59:59 -0700995 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
996 ObjectArray<Class>* dex_cache_resolved_types_;
997
998 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
999 ObjectArray<String>* dex_cache_strings_;
1000
1001 // Storage for invoke_stub_
1002 const ByteArray* invoke_stub_array_;
1003
1004 // Storage for mapping_table_
Ian Rogersbdb03912011-09-14 00:55:44 -07001005 IntArray* mapping_table_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001006
jeffhaoe23d93c2011-09-15 14:48:43 -07001007 // Byte arrays that hold data for the register maps
1008 const ByteArray* register_map_data_;
1009 const ByteArray* register_map_header_;
1010
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001011 // The short-form method descriptor string.
1012 String* shorty_;
1013
Brian Carlstromdbc05252011-09-09 01:59:59 -07001014 // The method descriptor. This represents the parameters a method
1015 // takes and value it returns. This string is a list of the type
1016 // descriptors for the parameters enclosed in parenthesis followed
1017 // by the return type descriptor. For example, for the method
1018 //
1019 // Object mymethod(int i, double d, Thread t)
1020 //
1021 // the method descriptor would be
1022 //
1023 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1024 String* signature_;
1025
buzbeec41e5b52011-09-23 12:46:19 -07001026 // Storage for Dalvik virtual register mapping_table_
1027 ShortArray* vmap_table_;
1028
Brian Carlstromdbc05252011-09-09 01:59:59 -07001029 uint32_t java_generic_types_are_initialized_;
1030
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001031 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001032 uint32_t access_flags_;
1033
1034 // Compiled code associated with this method for callers from managed code.
1035 // May be compiled managed code or a bridge for invoking a native method.
1036 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001037
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001038 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001039 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001040
Brian Carlstrom693267a2011-09-06 09:25:34 -07001041 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001042 uint32_t core_spill_mask_;
1043
1044 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001045 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001046
Brian Carlstrom693267a2011-09-06 09:25:34 -07001047 // Total size in bytes of the frame
1048 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049
Brian Carlstrom693267a2011-09-06 09:25:34 -07001050 // Native invocation stub entry point for calling from native to managed code.
1051 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001052
Brian Carlstrom693267a2011-09-06 09:25:34 -07001053 // Index of the return type
1054 uint32_t java_return_type_idx_;
1055
Brian Carlstrom693267a2011-09-06 09:25:34 -07001056 // For concrete virtual methods, this is the offset of the method
1057 // in Class::vtable_.
1058 //
1059 // For abstract methods in an interface class, this is the offset
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001060 // of the method in "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001061 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001062
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001063 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001064 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001065
Brian Carlstrom693267a2011-09-06 09:25:34 -07001066 // Method bounds; not needed for an abstract method.
1067 //
1068 // For a native method, we compute the size of the argument list, and
1069 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001070 uint32_t num_ins_;
1071 uint32_t num_outs_;
1072 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001073
Brian Carlstrom693267a2011-09-06 09:25:34 -07001074 // Method prototype descriptor string (return and argument types).
1075 uint32_t proto_idx_;
1076
1077 // Offset of return PC within frame for compiled code (in bytes)
1078 size_t return_pc_offset_in_bytes_;
1079
Brian Carlstrom693267a2011-09-06 09:25:34 -07001080 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001081
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001082 static Class* java_lang_reflect_Method_;
1083
Brian Carlstrom693267a2011-09-06 09:25:34 -07001084 friend class ImageWriter; // for relocating code_ and invoke_stub_
1085 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001086 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087};
1088
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001089class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001090 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001091 static size_t SizeOf(size_t component_count,
1092 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001093 return sizeof(Array) + component_count * component_size;
1094 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001095
Brian Carlstromb63ec392011-08-27 17:38:27 -07001096 // Given the context of a calling Method, use its DexCache to
1097 // resolve a type to an array Class. If it cannot be resolved, throw
1098 // an error. If it can, use it to create an array.
1099 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
1100
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001101 // A convenience for code that doesn't know the component size,
1102 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001103 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001104
Brian Carlstromb63ec392011-08-27 17:38:27 -07001105 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001106
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001107 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001108
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001109 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001110 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001111 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001112
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001113 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001114 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001116 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001117
buzbeec143c552011-08-20 17:38:58 -07001118 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001119 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001120 }
1121
1122 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001123 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001124 }
1125
Elliott Hughesbf86d042011-08-31 17:53:14 -07001126 void* GetRawData() {
1127 return reinterpret_cast<void*>(first_element_);
1128 }
1129
Elliott Hughes289da822011-08-16 10:11:20 -07001130 protected:
1131 bool IsValidIndex(int32_t index) const {
1132 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001133 Thread* self = Thread::Current();
1134 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
1135 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001136 return false;
Elliott Hughes289da822011-08-16 10:11:20 -07001137 }
1138 return true;
1139 }
1140
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001141 private:
1142 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001143 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001144 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1145 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001146 // Marker for the data (used by generated code)
1147 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001148
Carl Shapirof88c9522011-08-06 15:47:38 -07001149 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001150};
1151
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001152template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001153class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001154 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001155 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001156
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001159 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001160
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001161 // Set element without bound and element type checks, to be used in limited
1162 // circumstances, such as during boot image writing
1163 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001164
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001165 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001166 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001167 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001168
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001170
1171 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001172 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001173};
1174
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175template<class T>
1176ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1177 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1178}
1179
1180template<class T>
1181T* ObjectArray<T>::Get(int32_t i) const {
1182 if (!IsValidIndex(i)) {
1183 return NULL;
1184 }
1185 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1186 return GetFieldObject<T*>(data_offset, false);
1187}
1188
1189template<class T>
1190ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1191 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1192 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1193 return new_array;
1194}
1195
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001196// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001197// provides the static storage. However, this might change to an Array
1198// to improve image sharing, so we use this type to avoid assumptions
1199// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001200class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001201
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001202// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001203class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001204 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001205
1206 // Class Status
1207 //
1208 // kStatusNotReady: If a Class cannot be found in the class table by
1209 // FindClass, it allocates an new one with AllocClass in the
1210 // kStatusNotReady and calls LoadClass. Note if it does find a
1211 // class, it may not be kStatusResolved and it will try to push it
1212 // forward toward kStatusResolved.
1213 //
1214 // kStatusIdx: LoadClass populates with Class with information from
1215 // the DexFile, moving the status to kStatusIdx, indicating that the
1216 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001217 // populated based on super_class_type_idx_ and
1218 // interfaces_type_idx_. The new Class can then be inserted into the
1219 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001220 //
1221 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1222 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1223 // using ResolveClass to initialize the super_class_ and interfaces_.
1224 //
1225 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001226 // shows linking is complete and fields of the Class populated by making
1227 // it kStatusResolved. Java allows circularities of the form where a super
1228 // class has a field that is of the type of the sub class. We need to be able
1229 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001230
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001231 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001232 kStatusError = -1,
1233 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001234 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001235 kStatusLoaded = 2, // DEX idx values resolved
1236 kStatusResolved = 3, // part of linking
1237 kStatusVerifying = 4, // in the process of being verified
1238 kStatusVerified = 5, // logically part of linking; done pre-init
1239 kStatusInitializing = 6, // class init in progress
1240 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001241 };
1242
1243 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001244 kPrimNot = 0,
1245 kPrimBoolean,
1246 kPrimByte,
1247 kPrimChar,
1248 kPrimShort,
1249 kPrimInt,
1250 kPrimLong,
1251 kPrimFloat,
1252 kPrimDouble,
1253 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001254 };
1255
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001256 Status GetStatus() const {
1257 CHECK(sizeof(Status) == sizeof(uint32_t));
1258 return static_cast<Status>(
1259 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
1260 }
1261
1262 void SetStatus(Status new_status);
1263
1264 // Returns true if the class has failed to link.
1265 bool IsErroneous() const {
1266 return GetStatus() == kStatusError;
1267 }
1268
1269 // Returns true if the class has been loaded.
1270 bool IsIdxLoaded() const {
1271 return GetStatus() >= kStatusIdx;
1272 }
1273
1274 // Returns true if the class has been loaded.
1275 bool IsLoaded() const {
1276 return GetStatus() >= kStatusLoaded;
1277 }
1278
1279 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001280 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001281 return GetStatus() >= kStatusResolved;
1282 }
1283
1284 // Returns true if the class has been verified.
1285 bool IsVerified() const {
1286 return GetStatus() >= kStatusVerified;
1287 }
1288
1289 // Returns true if the class is initialized.
1290 bool IsInitialized() const {
1291 return GetStatus() == kStatusInitialized;
1292 }
1293
1294 uint32_t GetAccessFlags() const;
1295
1296 void SetAccessFlags(uint32_t new_access_flags) {
1297 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags,
1298 false);
1299 }
1300
1301 // Returns true if the class is an interface.
1302 bool IsInterface() const {
1303 return (GetAccessFlags() & kAccInterface) != 0;
1304 }
1305
1306 // Returns true if the class is declared public.
1307 bool IsPublic() const {
1308 return (GetAccessFlags() & kAccPublic) != 0;
1309 }
1310
1311 // Returns true if the class is declared final.
1312 bool IsFinal() const {
1313 return (GetAccessFlags() & kAccFinal) != 0;
1314 }
1315
1316 // Returns true if the class is abstract.
1317 bool IsAbstract() const {
1318 return (GetAccessFlags() & kAccAbstract) != 0;
1319 }
1320
1321 // Returns true if the class is an annotation.
1322 bool IsAnnotation() const {
1323 return (GetAccessFlags() & kAccAnnotation) != 0;
1324 }
1325
1326 // Returns true if the class is synthetic.
1327 bool IsSynthetic() const {
1328 return (GetAccessFlags() & kAccSynthetic) != 0;
1329 }
1330
1331 bool IsReferenceClass() const {
1332 return (GetAccessFlags() & kAccClassIsReference) != 0;
1333 }
1334
1335 bool IsWeakReferenceClass() const {
1336 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1337 }
1338
1339 bool IsSoftReferenceClass() const {
1340 return (GetAccessFlags() & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1341 }
1342
1343 bool IsFinalizerReferenceClass() const {
1344 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1345 }
1346
1347 bool IsPhantomReferenceClass() const {
1348 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1349 }
1350
1351 PrimitiveType GetPrimitiveType() const {
1352 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1353 return static_cast<PrimitiveType>(
1354 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1355 }
1356
1357 void SetPrimitiveType(PrimitiveType new_type) {
1358 CHECK(sizeof(PrimitiveType) == sizeof(int32_t));
1359 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type,
1360 false);
1361 }
1362
1363 // Returns true if the class is a primitive type.
1364 bool IsPrimitive() const {
1365 return GetPrimitiveType() != kPrimNot;
1366 }
1367
1368 bool IsPrimitiveBoolean() const {
1369 return GetPrimitiveType() == kPrimBoolean;
1370 }
1371
1372 bool IsPrimitiveByte() const {
1373 return GetPrimitiveType() == kPrimByte;
1374 }
1375
1376 bool IsPrimitiveChar() const {
1377 return GetPrimitiveType() == kPrimChar;
1378 }
1379
1380 bool IsPrimitiveShort() const {
1381 return GetPrimitiveType() == kPrimShort;
1382 }
1383
1384 bool IsPrimitiveInt() const {
1385 return GetPrimitiveType() == kPrimInt;
1386 }
1387
1388 bool IsPrimitiveLong() const {
1389 return GetPrimitiveType() == kPrimLong;
1390 }
1391
1392 bool IsPrimitiveFloat() const {
1393 return GetPrimitiveType() == kPrimFloat;
1394 }
1395
1396 bool IsPrimitiveDouble() const {
1397 return GetPrimitiveType() == kPrimDouble;
1398 }
1399
1400 bool IsPrimitiveVoid() const {
1401 return GetPrimitiveType() == kPrimVoid;
1402 }
1403
1404 size_t PrimitiveSize() const;
1405
1406 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001407 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 }
1409
1410 Class* GetComponentType() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411 return GetFieldObject<Class*>(
1412 OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
1413 }
1414
1415 void SetComponentType(Class* new_component_type) {
1416 DCHECK(GetComponentType() == NULL);
1417 DCHECK(new_component_type != NULL);
1418 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_),
1419 new_component_type, false);
1420 }
1421
1422 size_t GetComponentSize() const {
1423 return GetTypeSize(GetComponentType()->GetDescriptor());
1424 }
1425
1426 bool IsObjectClass() const {
1427 return !IsPrimitive() && GetSuperClass() == NULL;
1428 }
1429
Brian Carlstromb63ec392011-08-27 17:38:27 -07001430 // Given the context of a calling Method, use its DexCache to
1431 // resolve a type to a Class. If it cannot be resolved, throw an
1432 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -07001433 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -07001434
Brian Carlstrom1f870082011-08-23 16:02:11 -07001435 // Creates a raw object instance but does not invoke the default constructor.
1436 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001437
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001438 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001439
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001440 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001441 const String* result = GetFieldObject<const String*>(
1442 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1443 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1444 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1445 return result;
1446 }
1447
1448 void SetDescriptor(String* new_descriptor);
1449
1450 bool IsVariableSize() const {
1451 // Classes and arrays vary in size, and so the object_size_ field cannot
1452 // be used to get their instance size
1453 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001454 }
1455
Brian Carlstrom4873d462011-08-21 15:23:39 -07001456 size_t SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001457 CHECK(sizeof(size_t) == sizeof(int32_t));
1458 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001459 }
1460
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001461 size_t GetClassSize() const {
1462 CHECK(sizeof(size_t) == sizeof(uint32_t));
1463 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001464 }
1465
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001466 void SetClassSize(size_t new_class_size) {
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001467 DCHECK(new_class_size >= GetClassSize())
Elliott Hughes54e7df12011-09-16 11:47:04 -07001468 << " class=" << PrettyTypeOf(this)
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001469 << " new_class_size=" << new_class_size
1470 << " GetClassSize=" << GetClassSize();
Elliott Hughes54e7df12011-09-16 11:47:04 -07001471 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472 }
1473
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001474 size_t GetObjectSize() const {
1475 CHECK(!IsVariableSize());
1476 CHECK(sizeof(size_t) == sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001477 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001478 CHECK_GE(result, sizeof(Object));
1479 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001480 }
1481
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001482 void SetObjectSize(size_t new_object_size) {
1483 DCHECK(!IsVariableSize());
1484 CHECK(sizeof(size_t) == sizeof(int32_t));
1485 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_),
1486 new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001487 }
1488
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001489 // Returns true if this class is in the same packages as that class.
1490 bool IsInSamePackage(const Class* that) const;
1491
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001492 static bool IsInSamePackage(const String* descriptor1,
1493 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001494
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001495 // Returns true if this class can access that class.
1496 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001497 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001498 }
1499
jeffhao4a801a42011-09-23 13:53:40 -07001500 // Validate method/field access.
1501 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1502 // quick accept for public access
1503 if (member_flags & kAccPublic) {
1504 return true;
1505 }
1506
1507 // quick accept for access from same class
1508 if (this == access_to) {
1509 return true;
1510 }
1511
1512 // quick reject for private access from another class
1513 if (member_flags & kAccPrivate) {
1514 return false;
1515 }
1516
1517 // Semi-quick test for protected access from a sub-class, which may or
1518 // may not be in the same package.
1519 if (member_flags & kAccProtected) {
1520 if (this->IsSubClass(access_to)) {
1521 return true;
1522 }
1523 }
1524
1525 // Allow protected and private access from other classes in the same package.
1526 return this->IsInSamePackage(access_to);
1527 }
1528
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001529 bool IsSubClass(const Class* klass) const;
1530
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001531 bool IsAssignableFrom(const Class* src) const {
1532 DCHECK(src != NULL);
1533 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001534 // Can always assign to things of the same type
1535 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001536 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001537 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001538 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001539 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001540 return src->Implements(this);
1541 } else if (src->IsArrayClass()) {
1542 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001543 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001544 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001545 }
1546 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001547
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001548 Class* GetSuperClass() const {
1549 // Can only get super class for loaded classes (hack for when runtime is
1550 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001551 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001552 return GetFieldObject<Class*>(
1553 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1554 }
1555
buzbee4a3164f2011-09-03 11:25:10 -07001556 static MemberOffset SuperClassOffset() {
1557 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1558 }
1559
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 void SetSuperClass(Class *new_super_class) {
1561 // super class is assigned once, except during class linker initialization
1562 Class* old_super_class = GetFieldObject<Class*>(
1563 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1564 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1565 DCHECK(new_super_class != NULL);
1566 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_),
1567 new_super_class, false);
1568 }
1569
1570 bool HasSuperClass() const {
1571 return GetSuperClass() != NULL;
1572 }
1573
1574 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001575 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001576 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1577 false);
1578 }
1579
1580 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
1581 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1582 new_super_class_idx, false);
1583 }
1584
1585 const ClassLoader* GetClassLoader() const;
1586
1587 void SetClassLoader(const ClassLoader* new_cl);
1588
1589 static MemberOffset DexCacheOffset() {
1590 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1591 }
1592
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001593 enum {
1594 kDumpClassFullDetail = 1,
1595 kDumpClassClassLoader = (1 << 1),
1596 kDumpClassInitialized = (1 << 2),
1597 };
1598
1599 void DumpClass(std::ostream& os, int flags);
1600
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001601 DexCache* GetDexCache() const;
1602
1603 void SetDexCache(DexCache* new_dex_cache);
1604
1605 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001606 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001607 return GetFieldObject<ObjectArray<Method>*>(
1608 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1609 }
1610
1611 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1612 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1613 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1614 DCHECK_NE(0, new_direct_methods->GetLength());
1615 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1616 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001617 }
1618
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001619 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001620 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001621 }
1622
1623 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001624 ObjectArray<Method>* direct_methods =
1625 GetFieldObject<ObjectArray<Method>*>(
1626 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1627 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001628 }
1629
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001630 // Returns the number of static, private, and constructor methods.
1631 size_t NumDirectMethods() const {
1632 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1633 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001634
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001635 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001636 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001637 return GetFieldObject<ObjectArray<Method>*>(
1638 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1639 }
1640
1641 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1642 // TODO: we reassign virtual methods to grow the table for miranda
1643 // methods.. they should really just be assigned once
1644 DCHECK_NE(0, new_virtual_methods->GetLength());
1645 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1646 new_virtual_methods, false);
1647 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001648
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001649 // Returns the number of non-inherited virtual methods.
1650 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001651 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001652 }
1653
1654 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001655 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001656 return GetVirtualMethods()->Get(i);
1657 }
1658
1659 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001660 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001661 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001662 }
1663
1664 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001665 ObjectArray<Method>* virtual_methods =
1666 GetFieldObject<ObjectArray<Method>*>(
1667 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1668 virtual_methods->Set(i, f);
1669 }
1670
1671 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001672 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001673 return GetFieldObject<ObjectArray<Method>*>(
1674 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1675 }
1676
1677 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001678 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679 return GetFieldObject<ObjectArray<Method>*>(
1680 OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
1681 }
1682
1683 void SetVTable(ObjectArray<Method>* new_vtable) {
1684 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1685 }
1686
1687 static MemberOffset VTableOffset() {
1688 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001689 }
1690
Brian Carlstrom30b94452011-08-25 21:35:26 -07001691 // Given a method implemented by this class but potentially from a
1692 // super class, return the specific implementation
1693 // method for this class.
1694 Method* FindVirtualMethodForVirtual(Method* method) {
1695 DCHECK(!method->GetDeclaringClass()->IsInterface());
1696 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001697 // Use the index to a potentially overridden one for this instance's class.
1698 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001699 }
1700
1701 // Given a method implemented by this class, but potentially from a
1702 // super class or interface, return the specific implementation
1703 // method for this class.
1704 Method* FindVirtualMethodForInterface(Method* method);
1705
jeffhaobdb76512011-09-07 11:43:16 -07001706 Method* FindInterfaceMethod(const StringPiece& name,
1707 const StringPiece& descriptor);
1708
Brian Carlstrom30b94452011-08-25 21:35:26 -07001709 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1710 if (method->GetDeclaringClass()->IsInterface()) {
1711 return FindVirtualMethodForInterface(method);
1712 }
1713 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001714 }
1715
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001716 Method* FindDeclaredVirtualMethod(const StringPiece& name,
Brian Carlstrom3a7b4f22011-09-17 15:01:57 -07001717 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001718
1719 Method* FindVirtualMethod(const StringPiece& name,
1720 const StringPiece& descriptor);
1721
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001722 Method* FindDeclaredDirectMethod(const StringPiece& name,
1723 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001724
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725 Method* FindDirectMethod(const StringPiece& name,
1726 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001727
Carl Shapiro69759ea2011-07-21 18:13:35 -07001728 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001729 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001730 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1731 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1732 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001733 }
1734
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001735 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001736 CHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001737 return GetFieldObject<IntArray*>(
1738 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
1739 }
1740
1741 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1742
1743 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001744 CHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001745 return GetFieldObject<ObjectArray<Class>*>(
1746 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1747 }
1748
1749 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
1750 DCHECK(NULL == GetFieldObject<Object*>(
1751 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
1752 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_),
1753 new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001754 }
1755
1756 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001757 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 ObjectArray<Class>* interfaces =
1759 GetFieldObject<ObjectArray<Class>*>(
1760 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1761 interfaces->Set(i, f);
1762 }
1763
1764 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001765 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001766 return GetInterfaces()->Get(i);
1767 }
1768
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001769 int32_t GetIfTableCount() const {
1770 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1771 if (iftable == NULL) {
1772 return 0;
1773 }
1774 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001775 }
1776
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001777 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001778 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001779 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001780 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1781 }
1782
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001783 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001784 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001785 }
1786
1787 // Get instance fields
1788 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001789 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 return GetFieldObject<ObjectArray<Field>*>(
1791 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1792 }
1793
1794 void SetIFields(ObjectArray<Field>* new_ifields) {
1795 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1796 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
1797 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_),
1798 new_ifields, false);
1799 }
1800
1801 size_t NumInstanceFields() const {
1802 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1803 }
1804
1805 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1806 DCHECK_NE(NumInstanceFields(), 0U);
1807 return GetIFields()->Get(i);
1808 }
1809
1810 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1811 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1812 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1813 ifields->Set(i, f);
1814 }
1815
1816 // Returns the number of instance fields containing reference types.
1817 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001818 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001819 DCHECK(sizeof(size_t) == sizeof(int32_t));
1820 return GetField32(
1821 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1822 }
1823
1824 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001825 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001826 DCHECK(sizeof(size_t) == sizeof(int32_t));
1827 return GetField32(
1828 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
1829 }
1830
1831 void SetNumReferenceInstanceFields(size_t new_num) {
1832 DCHECK(sizeof(size_t) == sizeof(int32_t));
1833 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_),
1834 new_num, false);
1835 }
1836
1837 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001838 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001839 return GetField32(
1840 OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
1841 }
1842
1843 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1844
1845 // Beginning of static field data
1846 static MemberOffset FieldsOffset() {
1847 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1848 }
1849
1850 // Returns the number of static fields containing reference types.
1851 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001852 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001853 DCHECK(sizeof(size_t) == sizeof(int32_t));
1854 return GetField32(
1855 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1856 }
1857
1858 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001859 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001860 DCHECK(sizeof(size_t) == sizeof(int32_t));
1861 return GetField32(
1862 OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
1863 }
1864
1865 void SetNumReferenceStaticFields(size_t new_num) {
1866 DCHECK(sizeof(size_t) == sizeof(int32_t));
1867 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_),
1868 new_num, false);
1869 }
1870
1871 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001872 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 return GetFieldObject<ObjectArray<Field>*>(
1874 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1875 }
1876
1877 void SetSFields(ObjectArray<Field>* new_sfields) {
1878 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1879 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
1880 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_),
1881 new_sfields, false);
1882 }
1883
1884 size_t NumStaticFields() const {
1885 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1886 }
1887
1888 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1889 return GetSFields()->Get(i);
1890 }
1891
1892 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1893 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1894 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1895 sfields->Set(i, f);
1896 }
1897
1898 uint32_t GetReferenceStaticOffsets() const {
1899 return GetField32(
1900 OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
1901 }
1902
1903 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1904
1905 // Finds the given instance field in this class or a superclass.
1906 Field* FindInstanceField(const StringPiece& name, Class* type);
1907
1908 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1909
1910 // Finds the given static field in this class or a superclass.
1911 Field* FindStaticField(const StringPiece& name, Class* type);
1912
1913 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1914
Elliott Hughesdcc24742011-09-07 14:02:44 -07001915 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001916 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001917 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1918 }
1919
Elliott Hughesdcc24742011-09-07 14:02:44 -07001920 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001921 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_),
1922 new_clinit_thread_id, false);
1923 }
1924
1925 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001926 // DCHECK(IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001927 return GetFieldObject<Class*>(
1928 OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001929 }
1930
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001931 void SetVerifyErrorClass(Class* klass) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001932 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_),
1933 klass, false);
1934 }
1935
Brian Carlstromc74255f2011-09-11 22:47:39 -07001936 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001937
Brian Carlstromc74255f2011-09-11 22:47:39 -07001938 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001939
jeffhaobdb76512011-09-07 11:43:16 -07001940 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001941 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001942 bool IsArrayAssignableFromArray(const Class* klass) const;
1943 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001944
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001945 // descriptor for the class such as "java.lang.Class" or "[C"
1946 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001947
Brian Carlstrom693267a2011-09-06 09:25:34 -07001948 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001949 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001950
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001951 // For array classes, the component class object for instanceof/checkcast
1952 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001953 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001954
Brian Carlstrom693267a2011-09-06 09:25:34 -07001955 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1956 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001957
Brian Carlstrom693267a2011-09-06 09:25:34 -07001958 // DexCache of resolved constant pool entries
1959 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1960 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001961
1962 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001963 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001964
Brian Carlstrom693267a2011-09-06 09:25:34 -07001965 // instance fields
1966 //
1967 // These describe the layout of the contents of an Object.
1968 // Note that only the fields directly declared by this class are
1969 // listed in ifields; fields declared by a superclass are listed in
1970 // the superclass's Class.ifields.
1971 //
1972 // All instance fields that refer to objects are guaranteed to be at
1973 // the beginning of the field list. num_reference_instance_fields_
1974 // specifies the number of reference fields.
1975 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001976
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001977 // Interface table (iftable_), one entry per interface supported by
1978 // this class. That means one entry for each interface we support
1979 // directly, indirectly via superclass, or indirectly via
1980 // superinterface. This will be null if neither we nor our
1981 // superclass implement any interfaces.
1982 //
1983 // Why we need this: given "class Foo implements Face", declare
1984 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1985 // is part of the Face interface. We can't easily use a single
1986 // vtable.
1987 //
1988 // For every interface a concrete class implements, we create an array
1989 // of the concrete vtable_ methods for the methods in the interface.
1990 ObjectArray<InterfaceEntry>* iftable_;
1991
Brian Carlstromdbc05252011-09-09 01:59:59 -07001992 // array of interfaces this class implements directly
1993 // see also interfaces_type_idx_
1994 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001995
1996 // array of type_idx's for interfaces this class implements directly
1997 // see also interfaces_
1998 IntArray* interfaces_type_idx_;
1999
Brian Carlstromdbc05252011-09-09 01:59:59 -07002000 // Static fields
2001 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002002
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002003 // source file name, if known. Otherwise, NULL.
2004 String* source_file_;
2005
Brian Carlstromdbc05252011-09-09 01:59:59 -07002006 // The superclass, or NULL if this is java.lang.Object or a
2007 // primitive type.
2008 // see also super_class_type_idx_;
2009 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002010
Brian Carlstromdbc05252011-09-09 01:59:59 -07002011 // If class verify fails, we must return same error on subsequent tries.
2012 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2013 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002014
Brian Carlstromdbc05252011-09-09 01:59:59 -07002015 // virtual methods defined in this class; invoked through vtable
2016 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002017
Brian Carlstromdbc05252011-09-09 01:59:59 -07002018 // Virtual method table (vtable), for use by "invoke-virtual". The
2019 // vtable from the superclass is copied in, and virtual methods from
2020 // our class either replace those from the super or are appended.
2021 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002022
Brian Carlstromdbc05252011-09-09 01:59:59 -07002023 // access flags; low 16 bits are defined by VM spec
2024 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002025
Brian Carlstrom53d6ff42011-09-23 10:45:07 -07002026 // Total class size; used when allocating storage on gc heap.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002027 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002028
Elliott Hughes5f791332011-09-15 17:45:30 -07002029 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002030 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002031
Brian Carlstromdbc05252011-09-09 01:59:59 -07002032 // number of instance fields that are object refs
2033 size_t num_reference_instance_fields_;
2034
2035 // number of static fields that are object refs
2036 size_t num_reference_static_fields_;
2037
2038 // Total object size; used when allocating storage on gc heap.
2039 // (For interfaces and abstract classes this will be zero.)
2040 size_t object_size_;
2041
2042 // primitive type index, or kPrimNot (0); set for generated prim classes
2043 PrimitiveType primitive_type_;
2044
2045 // Bitmap of offsets of ifields.
2046 uint32_t reference_instance_offsets_;
2047
2048 // Bitmap of offsets of sfields.
2049 uint32_t reference_static_offsets_;
2050
Brian Carlstrom693267a2011-09-06 09:25:34 -07002051 // state of class initialization
2052 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002053
Brian Carlstrom693267a2011-09-06 09:25:34 -07002054 // Set in LoadClass, used to LinkClass
2055 // see also super_class_
2056 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002057
Brian Carlstrom693267a2011-09-06 09:25:34 -07002058 // TODO: ?
2059 // initiating class loader list
2060 // NOTE: for classes with low serialNumber, these are unused, and the
2061 // values are kept in a table in gDvm.
2062 // InitiatingLoaderList initiating_loader_list_;
2063
Brian Carlstrom4873d462011-08-21 15:23:39 -07002064 // Location of first static field.
2065 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002066
Brian Carlstrom693267a2011-09-06 09:25:34 -07002067 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002068 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002069};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002070
Elliott Hughes1f359b02011-07-17 14:27:17 -07002071std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002072
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002073inline void Object::SetClass(Class* new_klass) {
2074 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002075 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002076}
2077
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002078inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002079 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002080 DCHECK(GetClass() != NULL);
2081 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002082}
2083
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002084inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002085 Class* java_lang_Class = GetClass()->GetClass();
2086 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002087}
2088
Brian Carlstrom4873d462011-08-21 15:23:39 -07002089inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002090 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002091 return this == java_lang_Class;
2092}
2093
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002094inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002095 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002096}
2097
Brian Carlstromb63ec392011-08-27 17:38:27 -07002098inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002099 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002100}
2101
Brian Carlstroma663ea52011-08-19 23:33:41 -07002102inline bool Object::IsField() const {
2103 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002104 Class* java_lang_reflect_Field =
2105 java_lang_Class->GetInstanceField(0)->GetClass();
2106 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002107}
2108
2109inline bool Object::IsMethod() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002110 Class* java_lang_Class = GetClass()->GetClass();
2111 Class* java_lang_reflect_Method =
2112 java_lang_Class->GetDirectMethod(0)->GetClass();
2113 return GetClass() == java_lang_reflect_Method;
2114}
2115
2116inline bool Object::IsReferenceInstance() const {
2117 return GetClass()->IsReferenceClass();
2118}
2119
2120inline bool Object::IsWeakReferenceInstance() const {
2121 return GetClass()->IsWeakReferenceClass();
2122}
2123
2124inline bool Object::IsSoftReferenceInstance() const {
2125 return GetClass()->IsSoftReferenceClass();
2126}
2127
2128inline bool Object::IsFinalizerReferenceInstance() const {
2129 return GetClass()->IsFinalizerReferenceClass();
2130}
2131
2132inline bool Object::IsPhantomReferenceInstance() const {
2133 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002134}
2135
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002136inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002137 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002138 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002139 result = AsArray()->SizeOf();
2140 } else if (IsClass()) {
2141 result = AsClass()->SizeOf();
2142 } else {
2143 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002144 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002145 DCHECK(!IsField() || result == sizeof(Field));
2146 DCHECK(!IsMethod() || result == sizeof(Method));
2147 return result;
2148}
2149
2150inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002151 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002152 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002153 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
2154 DCHECK(IsAligned(num_bytes.Uint32Value(), 8));
Brian Carlstrom4873d462011-08-21 15:23:39 -07002155 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002156 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_),
2157 num_bytes.Uint32Value(), false);
2158}
2159
2160inline Class* Field::GetDeclaringClass() const {
2161 Class* result = GetFieldObject<Class*>(
2162 OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
2163 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002164 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002165 return result;
2166}
2167
2168inline void Field::SetDeclaringClass(Class *new_declaring_class) {
2169 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_),
2170 new_declaring_class, false);
2171}
2172
2173inline Class* Method::GetDeclaringClass() const {
2174 Class* result =
2175 GetFieldObject<Class*>(
2176 OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
2177 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002178 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002179 return result;
2180}
2181
2182inline void Method::SetDeclaringClass(Class *new_declaring_class) {
2183 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_),
2184 new_declaring_class, false);
2185}
2186
2187inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002188 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002189 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_),
2190 false);
2191}
2192
2193inline bool Method::IsReturnAReference() const {
2194 return !GetReturnType()->IsPrimitive();
2195}
2196
2197inline bool Method::IsReturnAFloat() const {
2198 return GetReturnType()->IsPrimitiveFloat();
2199}
2200
2201inline bool Method::IsReturnADouble() const {
2202 return GetReturnType()->IsPrimitiveDouble();
2203}
2204
2205inline bool Method::IsReturnALong() const {
2206 return GetReturnType()->IsPrimitiveLong();
2207}
2208
2209inline bool Method::IsReturnVoid() const {
2210 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002211}
2212
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002213inline size_t Array::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002214 return SizeOf(GetLength(), GetClass()->GetComponentSize());
2215}
2216
2217template<class T>
2218void ObjectArray<T>::Set(int32_t i, T* object) {
2219 if (IsValidIndex(i)) {
2220 if (object != NULL) {
2221 Class* element_class = GetClass()->GetComponentType();
2222 DCHECK(!element_class->IsPrimitive());
2223 // TODO: ArrayStoreException
2224 CHECK(object->InstanceOf(element_class));
2225 }
2226 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2227 SetFieldObject(data_offset, object, false);
2228 }
2229}
2230
2231template<class T>
2232void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2233 DCHECK(IsValidIndex(i));
2234 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2235 SetFieldObject(data_offset, object, false);
2236}
2237
2238template<class T>
2239void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2240 ObjectArray<T>* dst, int dst_pos,
2241 size_t length) {
2242 if (src->IsValidIndex(src_pos) &&
2243 src->IsValidIndex(src_pos+length-1) &&
2244 dst->IsValidIndex(dst_pos) &&
2245 dst->IsValidIndex(dst_pos+length-1)) {
2246 MemberOffset src_offset(DataOffset().Int32Value() +
2247 src_pos * sizeof(Object*));
2248 MemberOffset dst_offset(DataOffset().Int32Value() +
2249 dst_pos * sizeof(Object*));
2250 Class* array_class = dst->GetClass();
2251 if (array_class == src->GetClass()) {
2252 // No need for array store checks if arrays are of the same type
2253 for (size_t i=0; i < length; i++) {
2254 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2255 dst->SetFieldObject(dst_offset, object, false);
2256 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2257 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2258 }
2259 } else {
2260 Class* element_class = array_class->GetComponentType();
2261 CHECK(!element_class->IsPrimitive());
2262 for (size_t i=0; i < length; i++) {
2263 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2264 // TODO: ArrayStoreException
2265 CHECK(object == NULL || object->InstanceOf(element_class));
2266 dst->SetFieldObject(dst_offset, object, false);
2267 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2268 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2269 }
2270 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002271 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002272 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002273}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002274
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002275class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002276 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002277 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002278 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002279 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002280 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2281};
2282
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002283class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002284 private:
2285 CharArray* ASCII_;
2286 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002287 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002288 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002289 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002290 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2291};
2292
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002293class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002294 private:
2295 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2296 uint32_t TYPE_BOOLEAN_;
2297 uint32_t TYPE_BYTE_;
2298 uint32_t TYPE_CHAR_;
2299 uint32_t TYPE_DOUBLE_;
2300 uint32_t TYPE_FLOAT_;
2301 uint32_t TYPE_INTEGER_;
2302 uint32_t TYPE_LONG_;
2303 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002304 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002305 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2306};
2307
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002308class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002309 private:
Brian Carlstrom53d6ff42011-09-23 10:45:07 -07002310 ObjectArray<Object>* NO_ANNOTATIONS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002311 Object* ORDER_BY_SIGNATURE_;
2312 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002313 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2314};
2315
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002316template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002317class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002318 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002319 typedef T ElementType;
2320
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002321 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002322
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002323 const T* GetData() const {
2324 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002325 }
2326
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002327 T* GetData() {
2328 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002329 }
2330
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002331 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002332 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002333 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002334 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002335 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002336 }
2337
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002338 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002339 // TODO: ArrayStoreException
2340 if (IsValidIndex(i)) {
2341 GetData()[i] = value;
2342 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002343 }
2344
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002345 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002346 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002347 CHECK(array_class != NULL);
2348 array_class_ = array_class;
2349 }
2350
Brian Carlstroma663ea52011-08-19 23:33:41 -07002351 static void ResetArrayClass() {
2352 CHECK(array_class_ != NULL);
2353 array_class_ = NULL;
2354 }
2355
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002356 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002357 // Location of first element.
2358 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002360 static Class* array_class_;
2361
Carl Shapirof88c9522011-08-06 15:47:38 -07002362 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002363};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002364
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002365inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2366 DCHECK(NULL == GetFieldObject<IntArray*>(
2367 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2368 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2369 new_interfaces_idx, false);
2370}
2371
2372// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002373class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002374 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002375 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002376 const CharArray* result = GetFieldObject<const CharArray*>(
2377 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2378 DCHECK(result != NULL);
2379 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002380 }
2381
Elliott Hughes814e4032011-08-23 12:07:56 -07002382 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002383 int32_t result = GetField32(
2384 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2385 DCHECK_LE(0, result);
2386 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002387 }
2388
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002389 int32_t GetLength() const;
2390
2391 int32_t GetHashCode() const;
2392
2393 void ComputeHashCode() {
2394 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002395 }
2396
Elliott Hughes814e4032011-08-23 12:07:56 -07002397 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002398 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002399 }
2400
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002401 uint16_t CharAt(int32_t index) const;
2402
Brian Carlstromc74255f2011-09-11 22:47:39 -07002403 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002404
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002405 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002406 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002407 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002408
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002409 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002410
Jesse Wilson8989d992011-08-02 13:39:42 -07002411 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002412 const char* utf8_data_in);
2413
2414 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2415
2416 static String* Alloc(Class* java_lang_String, CharArray* array);
2417
2418 bool Equals(const char* modified_utf8) const;
2419
2420 // TODO: do we need this overload? give it a more intention-revealing name.
2421 bool Equals(const StringPiece& modified_utf8) const;
2422
2423 bool Equals(const String* that) const;
2424
2425 // TODO: do we need this overload? give it a more intention-revealing name.
2426 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2427 int32_t that_length) const;
2428
2429 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2430 std::string ToModifiedUtf8() const;
2431
2432 static Class* GetJavaLangString() {
2433 DCHECK(java_lang_String_ != NULL);
2434 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002435 }
2436
Brian Carlstroma663ea52011-08-19 23:33:41 -07002437 static void SetClass(Class* java_lang_String);
2438 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002439
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002440 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002441 void SetHashCode(int32_t new_hash_code) {
2442 DCHECK_EQ(0u,
2443 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2444 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2445 new_hash_code, false);
2446 }
2447
2448 void SetCount(int32_t new_count) {
2449 DCHECK_LE(0, new_count);
2450 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2451 }
2452
2453 void SetOffset(int32_t new_offset) {
2454 DCHECK_LE(0, new_offset);
2455 DCHECK_GE(GetLength(), new_offset);
2456 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2457 }
2458
2459 void SetArray(CharArray* new_array) {
2460 DCHECK(new_array != NULL);
2461 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2462 }
2463
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002464 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2465 CharArray* array_;
2466
Brian Carlstromdbc05252011-09-09 01:59:59 -07002467 int32_t count_;
2468
Carl Shapirof88c9522011-08-06 15:47:38 -07002469 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002470
Elliott Hughes289da822011-08-16 10:11:20 -07002471 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002472
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002473 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002474
Brian Carlstrom693267a2011-09-06 09:25:34 -07002475 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002476 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002477};
2478
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002479inline const String* Field::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002480 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002481 String* result =
2482 GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2483 DCHECK(result != NULL);
2484 return result;
2485}
2486
2487inline void Field::SetName(String* new_name) {
2488 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_),
2489 new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002490}
2491
2492inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002493 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002494 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2495}
2496
2497inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002498 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002499 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2500}
2501
2502inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002503 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002504 return MemberOffset(
2505 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2506}
2507
2508inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002509 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002510 return MemberOffset(
2511 GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
2512}
2513
2514inline const String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002515 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002516 const String* result =
2517 GetFieldObject<const String*>(
2518 OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
2519 DCHECK(result != NULL);
2520 return result;
2521}
2522
2523inline void Method::SetName(String* new_name) {
2524 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_),
2525 new_name, false);
2526
2527}
2528
jeffhaoe23d93c2011-09-15 14:48:43 -07002529inline ByteArray* Method::GetRegisterMapData() const {
2530 return GetFieldObject<ByteArray*>(
2531 OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
2532}
2533
jeffhaoa0a764a2011-09-16 10:43:38 -07002534inline void Method::SetRegisterMapData(ByteArray* data) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002535 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002536 data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002537}
2538
2539inline ByteArray* Method::GetRegisterMapHeader() const {
2540 return GetFieldObject<ByteArray*>(
2541 OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
2542}
2543
jeffhaoa0a764a2011-09-16 10:43:38 -07002544inline void Method::SetRegisterMapHeader(ByteArray* header) {
jeffhaoe23d93c2011-09-15 14:48:43 -07002545 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_),
jeffhaoa0a764a2011-09-16 10:43:38 -07002546 header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002547}
2548
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002549inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002550 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002551 return GetFieldObject<String*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002552 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
2553}
2554
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002555inline void Method::SetShorty(String* new_shorty) {
2556 DCHECK(NULL == GetFieldObject<String*>(
2557 OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
2558 DCHECK_LE(1, new_shorty->GetLength());
2559 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2560}
2561
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002562inline const String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002563 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002564 const String* result =
2565 GetFieldObject<const String*>(
2566 OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
2567 DCHECK(result != NULL);
2568 return result;
2569}
2570
2571inline void Method::SetSignature(String* new_signature) {
2572 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_),
2573 new_signature, false);
2574}
2575
2576inline uint32_t Class::GetAccessFlags() const {
2577 // Check class is loaded or this is java.lang.String that has a
2578 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002579 DCHECK(IsLoaded() || IsErroneous() ||
2580 this == String::GetJavaLangString() ||
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002581 this == Field::GetJavaLangReflectField() ||
Brian Carlstrom25c33252011-09-18 15:58:35 -07002582 this == Method::GetJavaLangReflectMethod()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002583 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2584}
2585
2586inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002587 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588 DCHECK(new_descriptor != NULL);
2589 DCHECK_NE(0, new_descriptor->GetLength());
2590 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2591 new_descriptor, false);
2592}
2593
Brian Carlstromc74255f2011-09-11 22:47:39 -07002594inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002595 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002596 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2597}
2598
2599inline void Class::SetSourceFile(String* new_source_file) {
2600 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2601}
2602
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002603inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002604 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002605 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2606}
2607
2608inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002609 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002610 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002611}
2612
2613inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002614 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002615 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002616}
2617
2618inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002619 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002620 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002621}
2622
2623inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002624 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002625 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002626}
2627
2628inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002629 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002630 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2631}
2632
2633// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002634class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002635 public:
2636 void SetDetailMessage(String* new_detail_message) {
2637 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2638 new_detail_message, false);
2639 }
2640
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002641 private:
2642 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2643 Throwable* cause_;
2644 String* detail_message_;
2645 Object* stack_state_; // Note this is Java volatile:
2646 Object* stack_trace_;
2647 Object* suppressed_exceptions_;
2648
Brian Carlstrom693267a2011-09-06 09:25:34 -07002649 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002650 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2651};
2652
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002653// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002654class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002655 public:
2656 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002657 return GetFieldObject<const String*>(
2658 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002659 }
2660
2661 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002662 return GetFieldObject<const String*>(
2663 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002664 }
2665
2666 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002667 return GetFieldObject<const String*>(
2668 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002669 }
2670
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002671 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002672 return GetField32(
2673 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002674 }
2675
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002676 static StackTraceElement* Alloc(const String* declaring_class,
2677 const String* method_name,
2678 const String* file_name,
2679 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002680
2681 static void SetClass(Class* java_lang_StackTraceElement);
2682
2683 static void ResetClass();
2684
2685 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002686 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002687 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002688 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002689 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002690 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002691
2692 static Class* GetStackTraceElement() {
2693 DCHECK(java_lang_StackTraceElement_ != NULL);
2694 return java_lang_StackTraceElement_;
2695 }
2696
2697 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002698
2699 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002700 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2701};
2702
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002703class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002704 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002705 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002706 Class* interface = Get(kInterface)->AsClass();
2707 DCHECK(interface != NULL);
2708 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002709 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002710
Brian Carlstrom30b94452011-08-25 21:35:26 -07002711 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002712 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002713 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002714 DCHECK(Get(kInterface) == NULL);
2715 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002716 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002717
Brian Carlstrom86927212011-09-15 11:31:11 -07002718 size_t GetMethodArrayCount() const {
2719 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2720 if (method_array == 0) {
2721 return 0;
2722 }
2723 return method_array->GetLength();
2724 }
2725
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002726 ObjectArray<Method>* GetMethodArray() const {
2727 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2728 DCHECK(method_array != NULL);
2729 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002730 }
2731
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002732 void SetMethodArray(ObjectArray<Method>* new_ma) {
2733 DCHECK(new_ma != NULL);
2734 DCHECK(Get(kMethodArray) == NULL);
2735 Set(kMethodArray, new_ma);
2736 }
2737
2738 static size_t LengthAsArray() {
2739 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002740 }
2741
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002742 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002743
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002744 enum ArrayIndex {
2745 // Points to the interface class.
2746 kInterface = 0,
2747 // Method pointers into the vtable, allow fast map from interface
2748 // method index to concrete instance method.
2749 kMethodArray = 1,
2750 kMax = 2,
2751 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002752
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002753 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002754};
2755
2756} // namespace art
2757
2758#endif // ART_SRC_OBJECT_H_