blob: 82cd0a3ad370c8a7d0f7a7e6e963344779339762 [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"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070032#include "primitive.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070033#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070034#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070035#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070036#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070037
38namespace art {
39
40class Array;
41class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070042class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070043class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070044class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040045class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070046class InterfaceEntry;
47class Monitor;
48class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070049class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070050class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040051class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070052template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070053template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070054typedef PrimitiveArray<uint8_t> BooleanArray;
55typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070056typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070057typedef PrimitiveArray<double> DoubleArray;
58typedef PrimitiveArray<float> FloatArray;
59typedef PrimitiveArray<int32_t> IntArray;
60typedef PrimitiveArray<int64_t> LongArray;
61typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070062
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063union JValue {
64 uint8_t z;
65 int8_t b;
66 uint16_t c;
67 int16_t s;
68 int32_t i;
69 int64_t j;
70 float f;
71 double d;
72 Object* l;
73};
74
Brian Carlstrombe977852011-07-19 14:54:54 -070075static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
76static const uint32_t kAccPrivate = 0x0002; // field, method, ic
77static const uint32_t kAccProtected = 0x0004; // field, method, ic
78static const uint32_t kAccStatic = 0x0008; // field, method, ic
79static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
80static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070081static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070082static const uint32_t kAccVolatile = 0x0040; // field
83static const uint32_t kAccBridge = 0x0040; // method (1.5)
84static const uint32_t kAccTransient = 0x0080; // field
85static const uint32_t kAccVarargs = 0x0080; // method (1.5)
86static const uint32_t kAccNative = 0x0100; // method
87static const uint32_t kAccInterface = 0x0200; // class, ic
88static const uint32_t kAccAbstract = 0x0400; // class, method, ic
89static const uint32_t kAccStrict = 0x0800; // method
90static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
91static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
92static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070093
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070094static const uint32_t kAccMiranda = 0x8000; // method
95
Brian Carlstroma331b3c2011-07-18 17:47:56 -070096static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
97
Elliott Hughes582a7d12011-10-10 18:38:42 -070098static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
99static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
100static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700101
Elliott Hughes20cde902011-10-04 17:37:27 -0700102// Special runtime-only flags.
103// Note: if only kAccClassIsReference is set, we have a soft reference.
104static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
105static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
106static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
107static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
108static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700109
110static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
111 | kAccClassIsWeakReference
112 | kAccClassIsFinalizerReference
113 | kAccClassIsPhantomReference);
114
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700115/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700116 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700117 */
118/*
119 * A magic value for refOffsets. Ignore the bits and walk the super
120 * chain when this is the value.
121 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
122 * fields followed by 2 ref instance fields.]
123 */
124#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700125#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
126#define CLASS_OFFSET_ALIGNMENT 4
127#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
128/*
129 * Given an offset, return the bit number which would encode that offset.
130 * Local use only.
131 */
132#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700133 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 CLASS_OFFSET_ALIGNMENT)
135/*
136 * Is the given offset too large to be encoded?
137 */
138#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
139 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
140/*
141 * Return a single bit, encoding the offset.
142 * Undefined if the offset is too large, as defined above.
143 */
144#define CLASS_BIT_FROM_OFFSET(byteOffset) \
145 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
146/*
147 * Return an offset, given a bit number as returned from CLZ.
148 */
149#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700150 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700151
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700152#define OFFSET_OF_OBJECT_MEMBER(type, field) \
153 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700154
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700155// Classes shared with the managed side of the world need to be packed
156// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700157#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700160class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700161 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700162 static MemberOffset ClassOffset() {
163 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700164 }
165
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700167 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700168 }
169
170 void SetClass(Class* new_klass);
171
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700172 bool InstanceOf(const Class* klass) const;
173
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700174 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700175
Elliott Hughes081be7f2011-09-18 16:50:26 -0700176 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700177
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700178 static MemberOffset MonitorOffset() {
179 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
180 }
181
Elliott Hughes5f791332011-09-15 17:45:30 -0700182 volatile int32_t* GetRawLockWordAddress() {
183 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
184 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
185 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186 }
187
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700188 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189
Elliott Hughes5f791332011-09-15 17:45:30 -0700190 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700191
Ian Rogersff1ed472011-09-20 13:46:24 -0700192 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700193
Elliott Hughes5f791332011-09-15 17:45:30 -0700194 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195
Elliott Hughes5f791332011-09-15 17:45:30 -0700196 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700197
Elliott Hughes5f791332011-09-15 17:45:30 -0700198 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700199
Elliott Hughes5f791332011-09-15 17:45:30 -0700200 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700201
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700202 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203
204 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700205 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206 return down_cast<Class*>(this);
207 }
208
209 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700210 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211 return down_cast<const Class*>(this);
212 }
213
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700214 bool IsObjectArray() const;
215
216 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700217 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700219 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700220 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221
Brian Carlstromb63ec392011-08-27 17:38:27 -0700222 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700223
224 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700225 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700226 return down_cast<Array*>(this);
227 }
228
229 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700230 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700231 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700232 }
233
Elliott Hughesdbb40792011-11-18 17:05:22 -0800234 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700235
236 bool IsMethod() const;
237
238 Method* AsMethod() {
239 DCHECK(IsMethod());
240 return down_cast<Method*>(this);
241 }
242
Brian Carlstrom4873d462011-08-21 15:23:39 -0700243 const Method* AsMethod() const {
244 DCHECK(IsMethod());
245 return down_cast<const Method*>(this);
246 }
247
Brian Carlstroma663ea52011-08-19 23:33:41 -0700248 bool IsField() const;
249
250 Field* AsField() {
251 DCHECK(IsField());
252 return down_cast<Field*>(this);
253 }
254
Brian Carlstrom4873d462011-08-21 15:23:39 -0700255 const Field* AsField() const {
256 DCHECK(IsField());
257 return down_cast<const Field*>(this);
258 }
259
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700260 bool IsReferenceInstance() const;
261
262 bool IsWeakReferenceInstance() const;
263
264 bool IsSoftReferenceInstance() const;
265
266 bool IsFinalizerReferenceInstance() const;
267
268 bool IsPhantomReferenceInstance() const;
269
270 // Accessors for Java type fields
271 template<class T>
272 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700273 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
274 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700275 Heap::VerifyObject(result);
276 return result;
277 }
278
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700279 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700280 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700281 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
282 if (new_value != NULL) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700283 Heap::WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700284 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 }
286
287 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
288 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700289 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
290 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700291 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700292 return android_atomic_acquire_load(word_addr);
293 } else {
294 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700295 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 }
297
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700298 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
299 if (this_is_valid) {
300 Heap::VerifyObject(this);
301 }
302 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700303 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700304 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700305 /*
306 * TODO: add an android_atomic_synchronization_store() function and
307 * use it in the 32-bit volatile set handlers. On some platforms we
308 * can use a fast atomic instruction and avoid the barriers.
309 */
310 ANDROID_MEMBAR_STORE();
311 *word_addr = new_value;
312 ANDROID_MEMBAR_FULL();
313 } else {
314 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700315 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 }
317
318 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
319 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700320 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700321 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700322 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700323 uint64_t result = QuasiAtomicRead64(addr);
324 ANDROID_MEMBAR_FULL();
325 return result;
326 } else {
327 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700328 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
330
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700331 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700332 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700333 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
334 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700335 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700336 ANDROID_MEMBAR_STORE();
337 QuasiAtomicSwap64(new_value, addr);
338 // Post-store barrier not required due to use of atomic op or mutex.
339 } else {
340 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700341 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 }
343
344 protected:
345 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700346 template<class T>
347 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700348 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
350
351 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700352 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
353 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700354 }
355
356 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700357 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358
Elliott Hughes5f791332011-09-15 17:45:30 -0700359 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700360
Elliott Hughes5f791332011-09-15 17:45:30 -0700361 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700362 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700363 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700364};
365
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700366struct ObjectIdentityHash {
367 size_t operator()(const art::Object* const& obj) const {
368#ifdef MOVING_GARBAGE_COLLECTOR
369 // TODO: we'll need to use the Object's internal concept of identity
370 UNIMPLEMENTED(FATAL);
371#endif
372 return reinterpret_cast<size_t>(obj);
373 }
374};
375
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700376// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500377class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700378 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700379 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 void SetDeclaringClass(Class *new_declaring_class);
382
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400383 String* GetName() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384
385 void SetName(String* new_name);
386
387 uint32_t GetAccessFlags() const;
388
389 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700390 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700391 }
392
Elliott Hughes80609252011-09-23 17:24:51 -0700393 bool IsPublic() const {
394 return (GetAccessFlags() & kAccPublic) != 0;
395 }
396
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700397 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700398 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700399 }
400
jeffhaobdb76512011-09-07 11:43:16 -0700401 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700402 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700403 }
404
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700405 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700406
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700407 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700408
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700409 // Gets type using type index and resolved types in the dex cache, may be null
410 // if type isn't yet resolved
411 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700412
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700413 bool IsPrimitiveType() const;
414
415 Primitive::Type GetPrimitiveType() const;
416
417 size_t PrimitiveSize() const;
418
419 const char* GetTypeDescriptor() const;
420
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 // Performs full resolution, may return null and set exceptions if type cannot
422 // be resolved
Ian Rogers5d76c432011-10-31 21:42:49 -0700423 Class* GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424
425 // Offset to field within an Object
426 MemberOffset GetOffset() const;
427
buzbee34cd9e52011-09-08 14:31:52 -0700428 static MemberOffset OffsetOffset() {
429 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
430 }
431
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432 MemberOffset GetOffsetDuringLinking() const;
433
434 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435
Brian Carlstrom4873d462011-08-21 15:23:39 -0700436 // field access, null object for static fields
437 bool GetBoolean(const Object* object) const;
438 void SetBoolean(Object* object, bool z) const;
439 int8_t GetByte(const Object* object) const;
440 void SetByte(Object* object, int8_t b) const;
441 uint16_t GetChar(const Object* object) const;
442 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700443 int16_t GetShort(const Object* object) const;
444 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700445 int32_t GetInt(const Object* object) const;
446 void SetInt(Object* object, int32_t i) const;
447 int64_t GetLong(const Object* object) const;
448 void SetLong(Object* object, int64_t j) const;
449 float GetFloat(const Object* object) const;
450 void SetFloat(Object* object, float f) const;
451 double GetDouble(const Object* object) const;
452 void SetDouble(Object* object, double d) const;
453 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700454 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700455
Ian Rogersce9eca62011-10-07 17:11:03 -0700456 // raw field accesses
457 uint32_t Get32(const Object* object) const;
458 void Set32(Object* object, uint32_t new_value) const;
459 uint64_t Get64(const Object* object) const;
460 void Set64(Object* object, uint64_t new_value) const;
461 Object* GetObj(const Object* object) const;
462 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700463
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700464 static Class* GetJavaLangReflectField() {
465 DCHECK(java_lang_reflect_Field_ != NULL);
466 return java_lang_reflect_Field_;
467 }
468
469 static void SetClass(Class* java_lang_reflect_Field);
470 static void ResetClass();
471
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700472 bool IsVolatile() const {
473 return (GetAccessFlags() & kAccVolatile) != 0;
474 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700475
Elliott Hughes80609252011-09-23 17:24:51 -0700476 void InitJavaFields();
477
buzbee1da522d2011-09-04 11:22:20 -0700478 private:
Elliott Hughes80609252011-09-23 17:24:51 -0700479 void InitJavaFieldsLocked();
480
Jesse Wilson35baaab2011-08-10 16:18:03 -0400481 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700482
Jesse Wilson35baaab2011-08-10 16:18:03 -0400483 // The class in which this field is declared.
484 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700485
Jesse Wilson35baaab2011-08-10 16:18:03 -0400486 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700487
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700488 String* name_;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700489
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700490 // The possibly null type of the field
Ian Rogers5d76c432011-10-31 21:42:49 -0700491 Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400492
Brian Carlstromdbc05252011-09-09 01:59:59 -0700493 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700494
Jesse Wilson35baaab2011-08-10 16:18:03 -0400495 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700496
497 // Offset of field within an instance or in the Class' static fields
498 uint32_t offset_;
499
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700500 // Dex cache index of resolved type
501 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400502
Brian Carlstrom693267a2011-09-06 09:25:34 -0700503 int32_t slot_;
504
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700505 static Class* java_lang_reflect_Field_;
506
Brian Carlstrom693267a2011-09-06 09:25:34 -0700507 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400508 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700509};
510
Jesse Wilson6bf19152011-09-29 13:12:33 -0400511// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500512class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700513 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700514 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700515 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700516 Object* obj,
517 Thread* thread,
518 byte* args,
519 JValue* result);
520
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700521 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700523 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700525 static MemberOffset DeclaringClassOffset() {
526 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700527 }
528
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700529 // Returns the method name, e.g. "<init>" or "eatLunch"
Ian Rogers466bb252011-10-14 03:29:56 -0700530 String* GetName() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700531
532 void SetName(String* new_name);
533
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700534 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700535
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700536 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700537
Ian Rogers466bb252011-10-14 03:29:56 -0700538 String* GetSignature() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700539
540 void SetSignature(String* new_signature);
541
Ian Rogers466bb252011-10-14 03:29:56 -0700542 bool HasSameNameAndSignature(const Method* that) const {
543 return GetName() == that->GetName() && GetSignature() == that->GetSignature();
544 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545
546 uint32_t GetAccessFlags() const;
547
548 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700549 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 }
551
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552 // Returns true if the method is declared public.
553 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700555 }
556
557 // Returns true if the method is declared private.
558 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700559 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700560 }
561
562 // Returns true if the method is declared static.
563 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700564 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700565 }
566
Brian Carlstrom1f870082011-08-23 16:02:11 -0700567 // Returns true if the method is a constructor.
568 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700569 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700570 }
571
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700572 // Is this method <clinit>
573 bool IsClassInitializer() const;
574
Brian Carlstrom1f870082011-08-23 16:02:11 -0700575 // Returns true if the method is static, private, or a constructor.
576 bool IsDirect() const {
577 return IsStatic() || IsPrivate() || IsConstructor();
578 }
579
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580 // Returns true if the method is declared synchronized.
581 bool IsSynchronized() const {
582 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700584 }
585
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700586 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700588 }
589
Elliott Hughes80609252011-09-23 17:24:51 -0700590 bool IsMiranda() const {
591 return (GetAccessFlags() & kAccMiranda) != 0;
592 }
593
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700594 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700596 }
597
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700598 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700599 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700600 }
601
602 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700603 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700604 }
605
Ian Rogers0571d352011-11-03 19:51:38 -0700606 uint32_t GetDexMethodIndex() const;
607
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700608 uint16_t GetMethodIndex() const;
609
610 size_t GetVtableIndex() const {
611 return GetMethodIndex();
612 }
613
614 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700615 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700616 }
617
618 static MemberOffset MethodIndexOffset() {
619 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
620 }
621
622 uint32_t GetCodeItemOffset() const {
623 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
624 }
625
626 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700627 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700628 }
629
630 // Number of 32bit registers that would be required to hold all the arguments
631 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700633 uint16_t NumRegisters() const;
634
635 void SetNumRegisters(uint16_t new_num_registers) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700636 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700637 }
638
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700639 uint16_t NumIns() const;
640
641 void SetNumIns(uint16_t new_num_ins) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700642 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700643 }
644
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700645 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700646
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 void SetNumOuts(uint16_t new_num_outs) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700648 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700649 }
650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 uint32_t GetProtoIdx() const;
652
653 void SetProtoIdx(uint32_t new_proto_idx) {
654 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700655 }
656
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 ObjectArray<String>* GetDexCacheStrings() const;
658 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
659
660 static MemberOffset DexCacheStringsOffset() {
661 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
662 }
663
buzbee2a475e72011-09-07 17:19:17 -0700664 static MemberOffset DexCacheResolvedTypesOffset() {
665 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
666 }
667
buzbee34cd9e52011-09-08 14:31:52 -0700668 static MemberOffset DexCacheResolvedFieldsOffset() {
669 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
670 }
671
buzbee1da522d2011-09-04 11:22:20 -0700672 static MemberOffset DexCacheInitializedStaticStorageOffset() {
673 return OFFSET_OF_OBJECT_MEMBER(Method,
674 dex_cache_initialized_static_storage_);
675 }
676
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700677 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
678 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
679
680 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
681 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
682
683 ObjectArray<Field>* GetDexCacheResolvedFields() const;
684 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
685
686 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
687 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
688
689 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
690 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
691
Ian Rogers466bb252011-10-14 03:29:56 -0700692 // Find the method that this method overrides
693 Method* FindOverriddenMethod() const;
694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 void SetReturnTypeIdx(uint32_t new_return_type_idx);
696
Ian Rogers9074b992011-10-26 17:41:55 -0700697 const char* GetReturnTypeDescriptor() const;
698
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700699 Class* GetReturnType() const;
700
701 bool IsReturnAReference() const;
702
703 bool IsReturnAFloat() const;
704
705 bool IsReturnADouble() const;
706
Ian Rogersb033c752011-07-20 12:22:35 -0700707 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700708 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700709 }
710
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700711 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700712
Ian Rogersae675992011-10-09 17:10:22 -0700713 bool IsReturnALongOrDouble() const {
714 return IsReturnALong() || IsReturnADouble();
715 }
716
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700717 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700718
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700719 // "Args" may refer to any of the 3 levels of "Args."
720 // To avoid confusion, our code will denote which "Args" clearly:
721 // 1. UserArgs: Args that a user see.
722 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
723 // receiver.
724 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
725 // E.g., the first in Args is Method* for both static and non-static
726 // methods. And CConvArgs doesn't deal with the receiver because
727 // receiver is hardwired in an implicit register, so CConvArgs doesn't
728 // need to deal with it.
729 //
730 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700731 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700732
733 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700734 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700735 size_t NumReferenceArgs() const;
736
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700737 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700738 size_t NumLongOrDoubleArgs() const;
739
Ian Rogersb033c752011-07-20 12:22:35 -0700740 // Is the given method parameter a reference?
741 bool IsParamAReference(unsigned int param) const;
742
743 // Is the given method parameter a long or double?
744 bool IsParamALongOrDouble(unsigned int param) const;
745
Ian Rogersdf20fe02011-07-20 20:34:16 -0700746 // Size in bytes of the given parameter
747 size_t ParamSize(unsigned int param) const;
748
749 // Size in bytes of the return value
750 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700751
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700752 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
753
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700754 const void* GetCode() const {
755 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700756 }
757
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700758 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700759 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
760 }
761
762 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700763 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700764 return reinterpret_cast<uint32_t>(GetCode());
765 }
766
767 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700768 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700769 SetCode(reinterpret_cast<void*>(code_offset));
770 }
771
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772 static MemberOffset GetCodeOffset() {
773 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700774 }
775
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700776 const uint32_t* GetMappingTable() const {
777 const uint32_t* map = GetMappingTableRaw();
778 if (map == NULL) {
779 return map;
780 }
781 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700782 }
783
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700784 uint32_t GetMappingTableLength() const {
785 const uint32_t* map = GetMappingTableRaw();
786 if (map == NULL) {
787 return 0;
788 }
789 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700790 }
791
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700792 const uint32_t* GetMappingTableRaw() const {
793 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700794 }
795
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700796 void SetMappingTable(const uint32_t* mapping_table) {
797 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
798 mapping_table, false);
799 }
800
801 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700802 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700803 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
804 }
805
806 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700807 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700808 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
809 }
810
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800811 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700812 const uint16_t* GetVmapTableRaw() const {
813 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
814 }
815
816 void SetVmapTable(const uint16_t* vmap_table) {
817 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
818 }
819
820 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700821 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700822 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
823 }
824
825 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700826 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700827 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700828 }
829
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 Object* GetGcMap() const {
831 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
832 }
833
834 void SetGcMap(Object* data) {
835 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
836 }
837
Shih-wei Liaod11af152011-08-23 16:02:11 -0700838 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700839 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
840 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700841 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
842 return result;
843 }
844
845 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700846 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700847 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
848 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
849 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700850 }
851
Shih-wei Liaod11af152011-08-23 16:02:11 -0700852 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700854 }
855
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700856 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700857
Brian Carlstrom16192862011-09-12 17:50:06 -0700858 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700859
Brian Carlstrom16192862011-09-12 17:50:06 -0700860 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700861
Ian Rogersb033c752011-07-20 12:22:35 -0700862 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700863 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700864 }
865
Brian Carlstrom78128a62011-09-15 17:21:19 -0700866 const void* GetNativeMethod() const {
867 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
868 }
869
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700870 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700871 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700872 InvokeStub* result = GetFieldPtr<InvokeStub*>(
873 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
874 // TODO: DCHECK(result != NULL); should be ahead of time compiled
875 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700876 }
877
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700878 void SetInvokeStub(InvokeStub* invoke_stub) {
879 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
880 invoke_stub, false);
881 }
882
883 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700884 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700885 return reinterpret_cast<uint32_t>(GetInvokeStub());
886 }
887
888 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700889 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700890 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
891 }
892
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700893 static MemberOffset GetInvokeStubOffset() {
894 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700895 }
896
buzbee561227c2011-09-02 15:28:19 -0700897 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
898 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
899 }
900
901 static MemberOffset GetDexCacheResolvedMethodsOffset() {
902 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
903 }
904
905 static MemberOffset GetMethodIndexOffset() {
906 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
907 }
908
Ian Rogers90865722011-09-19 11:11:44 -0700909 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700910 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700911 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700912
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700913 void SetCoreSpillMask(uint32_t core_spill_mask) {
914 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700915 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700916 }
917
Ian Rogers90865722011-09-19 11:11:44 -0700918 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700919 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
920 }
921
922 void SetFpSpillMask(uint32_t fp_spill_mask) {
923 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700924 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700925 }
926
Ian Rogers466bb252011-10-14 03:29:56 -0700927 ObjectArray<Class>* GetExceptionTypes() const {
928 return GetFieldObject<ObjectArray<Class>*>(
929 OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), false);
Jesse Wilson95caa792011-10-12 18:14:17 -0400930 }
931
Ian Rogers466bb252011-10-14 03:29:56 -0700932 void SetExceptionTypes(ObjectArray<Class>* exception_types);
933
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700934 ObjectArray<Class>* GetJavaParameterTypes() const {
935 return GetFieldObject<ObjectArray<Class>*>(
936 OFFSET_OF_OBJECT_MEMBER(Method, java_parameter_types_), false);
937 }
938
Ian Rogers90865722011-09-19 11:11:44 -0700939 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700940 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700941 Runtime* runtime = Runtime::Current();
942 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700943 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700944 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
945 result = true;
946 break;
947 }
948 }
Ian Rogers90865722011-09-19 11:11:44 -0700949 // Check that if we do think it is phony it looks like the callee save method
950 DCHECK(!result || GetCoreSpillMask() != 0);
951 return result;
952 }
953
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700954 // Converts a native PC to a dex PC. TODO: this is a no-op
955 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700956 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700957
958 // Converts a dex PC to a native PC. TODO: this is a no-op
959 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700960 uintptr_t ToNativePC(const uint32_t dex_pc) const;
961
962 // Find the catch block for the given exception type and dex_pc
963 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700964
Elliott Hughes80609252011-09-23 17:24:51 -0700965 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
966
967 static Class* GetConstructorClass() {
968 return java_lang_reflect_Constructor_;
969 }
970
971 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700972 return java_lang_reflect_Method_;
973 }
974
Elliott Hughes80609252011-09-23 17:24:51 -0700975 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700976
Elliott Hughes418d20f2011-09-22 14:00:39 -0700977 void InitJavaFields();
978
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700979 private:
980 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700981 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700982
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700983 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
984 // the class we are a part of
985 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700986 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700987 Object* java_formal_type_parameters_;
988 Object* java_generic_exception_types_;
989 Object* java_generic_parameter_types_;
990 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -0700991
Brian Carlstrom693267a2011-09-06 09:25:34 -0700992 String* name_;
993
Elliott Hughes418d20f2011-09-22 14:00:39 -0700994 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700995 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700996 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700997
Brian Carlstrom693267a2011-09-06 09:25:34 -0700998 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700999 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1000
1001 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1002 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1003
1004 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1005 ObjectArray<Field>* dex_cache_resolved_fields_;
1006
1007 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1008 ObjectArray<Method>* dex_cache_resolved_methods_;
1009
Brian Carlstromdbc05252011-09-09 01:59:59 -07001010 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1011 ObjectArray<Class>* dex_cache_resolved_types_;
1012
1013 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1014 ObjectArray<String>* dex_cache_strings_;
1015
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 // Garbage collection map
1017 Object* gc_map_;
jeffhaoe23d93c2011-09-15 14:48:43 -07001018
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001019 // The short-form method descriptor string.
1020 String* shorty_;
1021
Brian Carlstromdbc05252011-09-09 01:59:59 -07001022 // The method descriptor. This represents the parameters a method
1023 // takes and value it returns. This string is a list of the type
1024 // descriptors for the parameters enclosed in parenthesis followed
1025 // by the return type descriptor. For example, for the method
1026 //
1027 // Object mymethod(int i, double d, Thread t)
1028 //
1029 // the method descriptor would be
1030 //
1031 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1032 String* signature_;
1033
1034 uint32_t java_generic_types_are_initialized_;
1035
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001036 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001037 uint32_t access_flags_;
1038
1039 // Compiled code associated with this method for callers from managed code.
1040 // May be compiled managed code or a bridge for invoking a native method.
1041 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001043 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001044 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001045
Brian Carlstrom693267a2011-09-06 09:25:34 -07001046 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001047 uint32_t core_spill_mask_;
1048
1049 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001050 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001051
Brian Carlstrom693267a2011-09-06 09:25:34 -07001052 // Total size in bytes of the frame
1053 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001054
Brian Carlstrom693267a2011-09-06 09:25:34 -07001055 // Native invocation stub entry point for calling from native to managed code.
1056 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001057
Ian Rogers0571d352011-11-03 19:51:38 -07001058 // Index of the return type in the declaring classes dex cache or dex file's type ids
1059 // TODO: value is really just 16bit
Brian Carlstrom693267a2011-09-06 09:25:34 -07001060 uint32_t java_return_type_idx_;
1061
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001063 const uint32_t* mapping_table_;
1064
Ian Rogers466bb252011-10-14 03:29:56 -07001065 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001066 //
Ian Rogers466bb252011-10-14 03:29:56 -07001067 // For abstract methods in an interface class, this is the offset of the method in
1068 // "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001069 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001070
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001071 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001072 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001073
Brian Carlstrom693267a2011-09-06 09:25:34 -07001074 // Method bounds; not needed for an abstract method.
1075 //
1076 // For a native method, we compute the size of the argument list, and
1077 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001078 uint32_t num_ins_;
1079 uint32_t num_outs_;
1080 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001081
Brian Carlstrom693267a2011-09-06 09:25:34 -07001082 // Method prototype descriptor string (return and argument types).
1083 uint32_t proto_idx_;
1084
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 // When a register is promoted into a register, the spill mask holds which registers hold dex
1086 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
1087 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001088 const uint16_t* vmap_table_;
1089
Brian Carlstrom693267a2011-09-06 09:25:34 -07001090 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001091
Elliott Hughes80609252011-09-23 17:24:51 -07001092 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001093 static Class* java_lang_reflect_Method_;
1094
Brian Carlstrom693267a2011-09-06 09:25:34 -07001095 friend class ImageWriter; // for relocating code_ and invoke_stub_
1096 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001097 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098};
1099
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001100class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001101 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001102 // A convenience for code that doesn't know the component size,
1103 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001104 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001105
Brian Carlstromb63ec392011-08-27 17:38:27 -07001106 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001107
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001108 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001109
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001110 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001112 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001113
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001114 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001115 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001116 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001117 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001118
buzbeec143c552011-08-20 17:38:58 -07001119 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001120 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001121 }
1122
1123 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001124 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001125 }
1126
Elliott Hughesbf86d042011-08-31 17:53:14 -07001127 void* GetRawData() {
1128 return reinterpret_cast<void*>(first_element_);
1129 }
1130
Elliott Hughes289da822011-08-16 10:11:20 -07001131 protected:
1132 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001133 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001134 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001135 }
1136 return true;
1137 }
1138
Elliott Hughes80609252011-09-23 17:24:51 -07001139 protected:
1140 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1141 bool ThrowArrayStoreException(Object* object) const;
1142
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001143 private:
1144 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001145 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001146 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1147 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001148 // Marker for the data (used by generated code)
1149 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001150
Carl Shapirof88c9522011-08-06 15:47:38 -07001151 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001152};
1153
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001154template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001155class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001156 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001159 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001160
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001161 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001162
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001163 // Set element without bound and element type checks, to be used in limited
1164 // circumstances, such as during boot image writing
1165 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001166
Ian Rogers5d76c432011-10-31 21:42:49 -07001167 T* GetWithoutChecks(int32_t i) const;
1168
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001169 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001170 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001171 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001172
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001173 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001174
1175 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001176 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001177};
1178
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179template<class T>
1180ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1181 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1182}
1183
1184template<class T>
1185T* ObjectArray<T>::Get(int32_t i) const {
1186 if (!IsValidIndex(i)) {
1187 return NULL;
1188 }
1189 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1190 return GetFieldObject<T*>(data_offset, false);
1191}
1192
1193template<class T>
1194ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1195 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1196 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1197 return new_array;
1198}
1199
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001200// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001201// provides the static storage. However, this might change to an Array
1202// to improve image sharing, so we use this type to avoid assumptions
1203// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001204class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001205
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001206// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001207class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001208 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001209
1210 // Class Status
1211 //
1212 // kStatusNotReady: If a Class cannot be found in the class table by
1213 // FindClass, it allocates an new one with AllocClass in the
1214 // kStatusNotReady and calls LoadClass. Note if it does find a
1215 // class, it may not be kStatusResolved and it will try to push it
1216 // forward toward kStatusResolved.
1217 //
1218 // kStatusIdx: LoadClass populates with Class with information from
1219 // the DexFile, moving the status to kStatusIdx, indicating that the
1220 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001221 // populated based on super_class_type_idx_ and
1222 // interfaces_type_idx_. The new Class can then be inserted into the
1223 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001224 //
1225 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1226 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1227 // using ResolveClass to initialize the super_class_ and interfaces_.
1228 //
1229 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001230 // shows linking is complete and fields of the Class populated by making
1231 // it kStatusResolved. Java allows circularities of the form where a super
1232 // class has a field that is of the type of the sub class. We need to be able
1233 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001234
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001235 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001236 kStatusError = -1,
1237 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001238 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001239 kStatusLoaded = 2, // DEX idx values resolved
1240 kStatusResolved = 3, // part of linking
1241 kStatusVerifying = 4, // in the process of being verified
1242 kStatusVerified = 5, // logically part of linking; done pre-init
1243 kStatusInitializing = 6, // class init in progress
1244 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001245 };
1246
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001247 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001248 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1249 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001250 }
1251
1252 void SetStatus(Status new_status);
1253
1254 // Returns true if the class has failed to link.
1255 bool IsErroneous() const {
1256 return GetStatus() == kStatusError;
1257 }
1258
1259 // Returns true if the class has been loaded.
1260 bool IsIdxLoaded() const {
1261 return GetStatus() >= kStatusIdx;
1262 }
1263
1264 // Returns true if the class has been loaded.
1265 bool IsLoaded() const {
1266 return GetStatus() >= kStatusLoaded;
1267 }
1268
1269 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001270 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001271 return GetStatus() >= kStatusResolved;
1272 }
1273
1274 // Returns true if the class has been verified.
1275 bool IsVerified() const {
1276 return GetStatus() >= kStatusVerified;
1277 }
1278
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001279 // Returns true if the class is initializing.
1280 bool IsInitializing() const {
1281 return GetStatus() >= kStatusInitializing;
1282 }
1283
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001284 // Returns true if the class is initialized.
1285 bool IsInitialized() const {
1286 return GetStatus() == kStatusInitialized;
1287 }
1288
1289 uint32_t GetAccessFlags() const;
1290
1291 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001292 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293 }
1294
1295 // Returns true if the class is an interface.
1296 bool IsInterface() const {
1297 return (GetAccessFlags() & kAccInterface) != 0;
1298 }
1299
1300 // Returns true if the class is declared public.
1301 bool IsPublic() const {
1302 return (GetAccessFlags() & kAccPublic) != 0;
1303 }
1304
1305 // Returns true if the class is declared final.
1306 bool IsFinal() const {
1307 return (GetAccessFlags() & kAccFinal) != 0;
1308 }
1309
Elliott Hughes20cde902011-10-04 17:37:27 -07001310 bool IsFinalizable() const {
1311 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1312 }
1313
1314 void SetFinalizable() {
1315 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1316 SetAccessFlags(flags | kAccClassIsFinalizable);
1317 }
1318
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001319 // Returns true if the class is abstract.
1320 bool IsAbstract() const {
1321 return (GetAccessFlags() & kAccAbstract) != 0;
1322 }
1323
1324 // Returns true if the class is an annotation.
1325 bool IsAnnotation() const {
1326 return (GetAccessFlags() & kAccAnnotation) != 0;
1327 }
1328
1329 // Returns true if the class is synthetic.
1330 bool IsSynthetic() const {
1331 return (GetAccessFlags() & kAccSynthetic) != 0;
1332 }
1333
1334 bool IsReferenceClass() const {
1335 return (GetAccessFlags() & kAccClassIsReference) != 0;
1336 }
1337
1338 bool IsWeakReferenceClass() const {
1339 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1340 }
1341
1342 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001343 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001344 }
1345
1346 bool IsFinalizerReferenceClass() const {
1347 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1348 }
1349
1350 bool IsPhantomReferenceClass() const {
1351 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1352 }
1353
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001354 Primitive::Type GetPrimitiveType() const {
1355 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1356 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001357 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1358 }
1359
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001360 void SetPrimitiveType(Primitive::Type new_type) {
1361 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001362 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001363 }
1364
1365 // Returns true if the class is a primitive type.
1366 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001367 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001368 }
1369
1370 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001371 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001372 }
1373
1374 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001375 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 }
1377
1378 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001379 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001380 }
1381
1382 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001383 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 }
1385
1386 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001387 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001388 }
1389
1390 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001391 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001392 }
1393
1394 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001395 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001396 }
1397
1398 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001399 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 }
1401
1402 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001403 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404 }
1405
Ian Rogersd81871c2011-10-03 13:57:23 -07001406 // Depth of class from java.lang.Object
1407 size_t Depth() {
1408 size_t depth = 0;
1409 for(Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
1410 depth++;
1411 }
1412 return depth;
1413 }
1414
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001415 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001416 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 }
1418
Elliott Hughesdbb40792011-11-18 17:05:22 -08001419 bool IsClassClass() const;
1420
1421 bool IsStringClass() const;
1422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001423 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001424 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 }
1426
1427 void SetComponentType(Class* new_component_type) {
1428 DCHECK(GetComponentType() == NULL);
1429 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001430 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 }
1432
1433 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001434 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001435 }
1436
1437 bool IsObjectClass() const {
1438 return !IsPrimitive() && GetSuperClass() == NULL;
1439 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001440 bool IsInstantiable() const {
1441 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1442 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001443
Brian Carlstrom1f870082011-08-23 16:02:11 -07001444 // Creates a raw object instance but does not invoke the default constructor.
1445 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001446
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001447 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001448 const String* result = GetFieldObject<const String*>(
1449 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1450 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1451 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1452 return result;
1453 }
1454
1455 void SetDescriptor(String* new_descriptor);
1456
1457 bool IsVariableSize() const {
1458 // Classes and arrays vary in size, and so the object_size_ field cannot
1459 // be used to get their instance size
1460 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001461 }
1462
Brian Carlstrom4873d462011-08-21 15:23:39 -07001463 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001464 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001465 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001466 }
1467
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001468 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001469 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001470 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471 }
1472
Ian Rogers0571d352011-11-03 19:51:38 -07001473 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001474
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001476 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001477 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001478 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001479 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001481 }
1482
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001483 void SetObjectSize(size_t new_object_size) {
1484 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001485 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001486 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), 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 Carlstrome24fa612011-09-29 00:53:55 -07001492 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001493
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001494 // Returns true if this class can access that class.
1495 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001496 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001497 }
1498
jeffhao4a801a42011-09-23 13:53:40 -07001499 // Validate method/field access.
1500 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1501 // quick accept for public access
1502 if (member_flags & kAccPublic) {
1503 return true;
1504 }
1505
1506 // quick accept for access from same class
1507 if (this == access_to) {
1508 return true;
1509 }
1510
1511 // quick reject for private access from another class
1512 if (member_flags & kAccPrivate) {
1513 return false;
1514 }
1515
1516 // Semi-quick test for protected access from a sub-class, which may or
1517 // may not be in the same package.
1518 if (member_flags & kAccProtected) {
1519 if (this->IsSubClass(access_to)) {
1520 return true;
1521 }
1522 }
1523
1524 // Allow protected and private access from other classes in the same package.
1525 return this->IsInSamePackage(access_to);
1526 }
1527
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001528 bool IsSubClass(const Class* klass) const;
1529
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1531 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1532 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1533 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1534 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001535 bool IsAssignableFrom(const Class* src) const {
1536 DCHECK(src != NULL);
1537 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001538 // Can always assign to things of the same type
1539 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001540 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001541 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001542 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001543 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001544 return src->Implements(this);
1545 } else if (src->IsArrayClass()) {
1546 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001547 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001549 }
1550 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001551
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001552 Class* GetSuperClass() const {
1553 // Can only get super class for loaded classes (hack for when runtime is
1554 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001555 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001556 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001557 }
1558
buzbee4a3164f2011-09-03 11:25:10 -07001559 static MemberOffset SuperClassOffset() {
1560 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1561 }
1562
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001563 void SetSuperClass(Class *new_super_class) {
1564 // super class is assigned once, except during class linker initialization
1565 Class* old_super_class = GetFieldObject<Class*>(
1566 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1567 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1568 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001569 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001570 }
1571
1572 bool HasSuperClass() const {
1573 return GetSuperClass() != NULL;
1574 }
1575
1576 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001577 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1579 false);
1580 }
1581
1582 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001583 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001584 }
1585
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001586 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001587
1588 void SetClassLoader(const ClassLoader* new_cl);
1589
1590 static MemberOffset DexCacheOffset() {
1591 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1592 }
1593
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001594 enum {
1595 kDumpClassFullDetail = 1,
1596 kDumpClassClassLoader = (1 << 1),
1597 kDumpClassInitialized = (1 << 2),
1598 };
1599
Elliott Hughes4681c802011-09-25 18:04:37 -07001600 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001601
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001602 DexCache* GetDexCache() const;
1603
1604 void SetDexCache(DexCache* new_dex_cache);
1605
1606 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001607 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001608 return GetFieldObject<ObjectArray<Method>*>(
1609 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1610 }
1611
1612 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1613 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1614 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1615 DCHECK_NE(0, new_direct_methods->GetLength());
1616 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1617 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001618 }
1619
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001620 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001621 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001622 }
1623
1624 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001625 ObjectArray<Method>* direct_methods =
1626 GetFieldObject<ObjectArray<Method>*>(
1627 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1628 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001629 }
1630
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001631 // Returns the number of static, private, and constructor methods.
1632 size_t NumDirectMethods() const {
1633 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1634 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001635
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001636 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001637 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001638 return GetFieldObject<ObjectArray<Method>*>(
1639 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1640 }
1641
1642 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1643 // TODO: we reassign virtual methods to grow the table for miranda
1644 // methods.. they should really just be assigned once
1645 DCHECK_NE(0, new_virtual_methods->GetLength());
1646 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1647 new_virtual_methods, false);
1648 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001649
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001650 // Returns the number of non-inherited virtual methods.
1651 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001653 }
1654
1655 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001656 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001657 return GetVirtualMethods()->Get(i);
1658 }
1659
1660 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001661 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001663 }
1664
1665 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001666 ObjectArray<Method>* virtual_methods =
1667 GetFieldObject<ObjectArray<Method>*>(
1668 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1669 virtual_methods->Set(i, f);
1670 }
1671
1672 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001673 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001674 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001675 }
1676
1677 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001678 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001679 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001680 }
1681
1682 void SetVTable(ObjectArray<Method>* new_vtable) {
1683 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1684 }
1685
1686 static MemberOffset VTableOffset() {
1687 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001688 }
1689
Brian Carlstrom30b94452011-08-25 21:35:26 -07001690 // Given a method implemented by this class but potentially from a
1691 // super class, return the specific implementation
1692 // method for this class.
1693 Method* FindVirtualMethodForVirtual(Method* method) {
1694 DCHECK(!method->GetDeclaringClass()->IsInterface());
1695 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001696 // Use the index to a potentially overridden one for this instance's class.
1697 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001698 }
1699
1700 // Given a method implemented by this class, but potentially from a
1701 // super class or interface, return the specific implementation
1702 // method for this class.
Ian Rogersb04f69f2011-10-17 00:40:54 -07001703 Method* FindVirtualMethodForInterface(Method* method, bool can_throw);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001704
Ian Rogers466bb252011-10-14 03:29:56 -07001705 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
1706 Method* FindInterfaceMethod(String* name, String* descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001707
Brian Carlstrom30b94452011-08-25 21:35:26 -07001708 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001709 if (method->IsDirect()) {
1710 return method;
1711 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001712 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersb04f69f2011-10-17 00:40:54 -07001713 return FindVirtualMethodForInterface(method, true);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001714 }
1715 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001716 }
1717
Ian Rogers466bb252011-10-14 03:29:56 -07001718 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
1719 Method* FindDeclaredVirtualMethod(String* name, String* signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001720
Ian Rogers466bb252011-10-14 03:29:56 -07001721 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
1722 Method* FindVirtualMethod(String* name, String* descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001723
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001724 Method* FindDeclaredDirectMethod(const StringPiece& name,
1725 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001726
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001727 Method* FindDirectMethod(const StringPiece& name,
1728 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001729
Carl Shapiro69759ea2011-07-21 18:13:35 -07001730 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001731 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001732 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1733 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1734 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001735 }
1736
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001737 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001738 CHECK(IsIdxLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001739 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001740 }
1741
1742 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1743
1744 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001745 CHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001746 return GetFieldObject<ObjectArray<Class>*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001747 }
1748
1749 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001750 DCHECK(NULL == GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001751 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001752 }
1753
1754 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001755 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001756 ObjectArray<Class>* interfaces =
1757 GetFieldObject<ObjectArray<Class>*>(
1758 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1759 interfaces->Set(i, f);
1760 }
1761
1762 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001763 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001764 return GetInterfaces()->Get(i);
1765 }
1766
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001767 int32_t GetIfTableCount() const {
1768 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1769 if (iftable == NULL) {
1770 return 0;
1771 }
1772 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001773 }
1774
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001775 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001776 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001777 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001778 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1779 }
1780
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001781 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001782 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001783 }
1784
1785 // Get instance fields
1786 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001787 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001788 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001789 }
1790
1791 void SetIFields(ObjectArray<Field>* new_ifields) {
1792 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1793 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001794 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001795 }
1796
1797 size_t NumInstanceFields() const {
1798 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1799 }
1800
1801 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1802 DCHECK_NE(NumInstanceFields(), 0U);
1803 return GetIFields()->Get(i);
1804 }
1805
1806 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1807 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1808 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1809 ifields->Set(i, f);
1810 }
1811
1812 // Returns the number of instance fields containing reference types.
1813 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001814 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001815 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001816 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001817 }
1818
1819 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001820 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001821 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001822 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001823 }
1824
1825 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001826 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001827 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001828 }
1829
1830 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001831 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001832 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001833 }
1834
1835 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1836
1837 // Beginning of static field data
1838 static MemberOffset FieldsOffset() {
1839 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1840 }
1841
1842 // Returns the number of static fields containing reference types.
1843 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001844 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001845 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001846 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001847 }
1848
1849 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001850 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001851 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001852 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001853 }
1854
1855 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001856 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001857 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001858 }
1859
1860 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001861 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001862 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001863 }
1864
1865 void SetSFields(ObjectArray<Field>* new_sfields) {
1866 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1867 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001868 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001869 }
1870
1871 size_t NumStaticFields() const {
1872 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1873 }
1874
1875 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1876 return GetSFields()->Get(i);
1877 }
1878
1879 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1880 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1881 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1882 sfields->Set(i, f);
1883 }
1884
1885 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001886 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001887 }
1888
1889 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1890
1891 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001892 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
1893 Field* FindInstanceField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001894
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001895 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
1896 Field* FindDeclaredInstanceField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001897
1898 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001899 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
1900 Field* FindStaticField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001901
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001902 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
1903 Field* FindDeclaredStaticField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001904
Elliott Hughesdcc24742011-09-07 14:02:44 -07001905 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001906 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001907 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1908 }
1909
Elliott Hughesdcc24742011-09-07 14:02:44 -07001910 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001911 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001912 }
1913
1914 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001915 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001916 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001917 }
1918
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001919 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001920 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001921 }
1922
jeffhao64155032011-11-03 17:56:34 -07001923 uint32_t GetAnnotationsOffset() {
1924 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, annotations_offset_), false);
1925 }
1926
1927 void SetAnnotationsOffset(uint32_t annotations_offset) {
1928 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, annotations_offset_), annotations_offset, false);
1929 }
1930
1931 uint32_t GetTypeIdx() {
1932 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, type_idx_), false);
1933 }
1934
1935 void SetTypeIdx(uint32_t type_idx) {
1936 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, type_idx_), type_idx, false);
1937 }
1938
Brian Carlstromc74255f2011-09-11 22:47:39 -07001939 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001940
Brian Carlstromc74255f2011-09-11 22:47:39 -07001941 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001942
jeffhaobdb76512011-09-07 11:43:16 -07001943 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001944 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001945 bool IsArrayAssignableFromArray(const Class* klass) const;
1946 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001947
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001948 // descriptor for the class such as "java.lang.Class" or "[C"
1949 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001950
Brian Carlstrom693267a2011-09-06 09:25:34 -07001951 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001952 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001953
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001954 // For array classes, the component class object for instanceof/checkcast
1955 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001956 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001957
Brian Carlstrom693267a2011-09-06 09:25:34 -07001958 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1959 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001960
Brian Carlstrom693267a2011-09-06 09:25:34 -07001961 // DexCache of resolved constant pool entries
1962 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1963 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001964
1965 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001966 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001967
Brian Carlstrom693267a2011-09-06 09:25:34 -07001968 // instance fields
1969 //
1970 // These describe the layout of the contents of an Object.
1971 // Note that only the fields directly declared by this class are
1972 // listed in ifields; fields declared by a superclass are listed in
1973 // the superclass's Class.ifields.
1974 //
1975 // All instance fields that refer to objects are guaranteed to be at
1976 // the beginning of the field list. num_reference_instance_fields_
1977 // specifies the number of reference fields.
1978 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001979
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001980 // Interface table (iftable_), one entry per interface supported by
1981 // this class. That means one entry for each interface we support
1982 // directly, indirectly via superclass, or indirectly via
1983 // superinterface. This will be null if neither we nor our
1984 // superclass implement any interfaces.
1985 //
1986 // Why we need this: given "class Foo implements Face", declare
1987 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1988 // is part of the Face interface. We can't easily use a single
1989 // vtable.
1990 //
1991 // For every interface a concrete class implements, we create an array
1992 // of the concrete vtable_ methods for the methods in the interface.
1993 ObjectArray<InterfaceEntry>* iftable_;
1994
Brian Carlstromdbc05252011-09-09 01:59:59 -07001995 // array of interfaces this class implements directly
1996 // see also interfaces_type_idx_
1997 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001998
1999 // array of type_idx's for interfaces this class implements directly
2000 // see also interfaces_
2001 IntArray* interfaces_type_idx_;
2002
Brian Carlstromdbc05252011-09-09 01:59:59 -07002003 // Static fields
2004 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002005
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002006 // source file name, if known. Otherwise, NULL.
2007 String* source_file_;
2008
Brian Carlstromdbc05252011-09-09 01:59:59 -07002009 // The superclass, or NULL if this is java.lang.Object or a
2010 // primitive type.
2011 // see also super_class_type_idx_;
2012 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002013
Brian Carlstromdbc05252011-09-09 01:59:59 -07002014 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002015 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002016
Brian Carlstromdbc05252011-09-09 01:59:59 -07002017 // virtual methods defined in this class; invoked through vtable
2018 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002019
Ian Rogers9074b992011-10-26 17:41:55 -07002020 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
2021 // copied in, and virtual methods from our class either replace those from the super or are
2022 // appended. For abstract classes, methods may be created in the vtable that aren't in
2023 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002024 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002025
Brian Carlstromdbc05252011-09-09 01:59:59 -07002026 // access flags; low 16 bits are defined by VM spec
2027 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002028
jeffhao64155032011-11-03 17:56:34 -07002029 // annotation directory offset from dex file
2030 uint32_t annotations_offset_;
2031
Jesse Wilson6bf19152011-09-29 13:12:33 -04002032 // Total size of the Class instance; used when allocating storage on gc heap.
2033 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002034 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002035
Elliott Hughes5f791332011-09-15 17:45:30 -07002036 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002037 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002038
Brian Carlstromdbc05252011-09-09 01:59:59 -07002039 // number of instance fields that are object refs
2040 size_t num_reference_instance_fields_;
2041
2042 // number of static fields that are object refs
2043 size_t num_reference_static_fields_;
2044
2045 // Total object size; used when allocating storage on gc heap.
2046 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002047 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002048 size_t object_size_;
2049
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002050 // primitive type index, or Primitive::kPrimNot (0); set for generated prim classes
2051 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002052
2053 // Bitmap of offsets of ifields.
2054 uint32_t reference_instance_offsets_;
2055
2056 // Bitmap of offsets of sfields.
2057 uint32_t reference_static_offsets_;
2058
Brian Carlstrom693267a2011-09-06 09:25:34 -07002059 // state of class initialization
2060 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002061
Brian Carlstrom693267a2011-09-06 09:25:34 -07002062 // Set in LoadClass, used to LinkClass
2063 // see also super_class_
Ian Rogers0571d352011-11-03 19:51:38 -07002064 // TODO: really 16bits
Brian Carlstrom693267a2011-09-06 09:25:34 -07002065 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002066
jeffhao64155032011-11-03 17:56:34 -07002067 // type index from dex file
Ian Rogers0571d352011-11-03 19:51:38 -07002068 // TODO: really 16bits
jeffhao64155032011-11-03 17:56:34 -07002069 uint32_t type_idx_;
2070
Brian Carlstrom693267a2011-09-06 09:25:34 -07002071 // TODO: ?
2072 // initiating class loader list
2073 // NOTE: for classes with low serialNumber, these are unused, and the
2074 // values are kept in a table in gDvm.
2075 // InitiatingLoaderList initiating_loader_list_;
2076
Brian Carlstrom4873d462011-08-21 15:23:39 -07002077 // Location of first static field.
2078 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002079
Brian Carlstrom693267a2011-09-06 09:25:34 -07002080 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002081 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002082};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002083
Elliott Hughes1f359b02011-07-17 14:27:17 -07002084std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002085
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002086inline void Object::SetClass(Class* new_klass) {
2087 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002088 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002089}
2090
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002091inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002092 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002093 DCHECK(GetClass() != NULL);
2094 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002095}
2096
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002097inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002098 Class* java_lang_Class = GetClass()->GetClass();
2099 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002100}
2101
2102inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002103 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002104}
2105
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002106template<class T>
2107inline ObjectArray<T>* Object::AsObjectArray() {
2108 DCHECK(IsObjectArray());
2109 return down_cast<ObjectArray<T>*>(this);
2110}
2111
2112template<class T>
2113inline const ObjectArray<T>* Object::AsObjectArray() const {
2114 DCHECK(IsObjectArray());
2115 return down_cast<const ObjectArray<T>*>(this);
2116}
2117
Brian Carlstromb63ec392011-08-27 17:38:27 -07002118inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002119 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002120}
2121
Brian Carlstroma663ea52011-08-19 23:33:41 -07002122inline bool Object::IsField() const {
2123 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002124 Class* java_lang_reflect_Field =
2125 java_lang_Class->GetInstanceField(0)->GetClass();
2126 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002127}
2128
2129inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002130 Class* c = GetClass();
2131 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002132}
2133
2134inline bool Object::IsReferenceInstance() const {
2135 return GetClass()->IsReferenceClass();
2136}
2137
2138inline bool Object::IsWeakReferenceInstance() const {
2139 return GetClass()->IsWeakReferenceClass();
2140}
2141
2142inline bool Object::IsSoftReferenceInstance() const {
2143 return GetClass()->IsSoftReferenceClass();
2144}
2145
2146inline bool Object::IsFinalizerReferenceInstance() const {
2147 return GetClass()->IsFinalizerReferenceClass();
2148}
2149
2150inline bool Object::IsPhantomReferenceInstance() const {
2151 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002152}
2153
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002154inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002155 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002156 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002157 result = AsArray()->SizeOf();
2158 } else if (IsClass()) {
2159 result = AsClass()->SizeOf();
2160 } else {
2161 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002162 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002163 DCHECK(!IsField() || result == sizeof(Field));
2164 DCHECK(!IsMethod() || result == sizeof(Method));
2165 return result;
2166}
2167
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002168inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002169 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002170 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002171 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002172 return result;
2173}
2174
2175inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002176 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002177}
2178
2179inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002180 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002181 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002182 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002183 return result;
2184}
2185
2186inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002187 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002188}
2189
2190inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002191 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes06b37d92011-10-16 11:51:29 -07002192 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002193}
2194
2195inline bool Method::IsReturnAReference() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002196 char d = GetReturnTypeDescriptor()[0];
2197 return d == 'L' || d == '[';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002198}
2199
2200inline bool Method::IsReturnAFloat() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002201 return GetReturnTypeDescriptor()[0] == 'F';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002202}
2203
2204inline bool Method::IsReturnADouble() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002205 return GetReturnTypeDescriptor()[0] == 'D';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002206}
2207
2208inline bool Method::IsReturnALong() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002209 return GetReturnTypeDescriptor()[0] == 'J';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002210}
2211
2212inline bool Method::IsReturnVoid() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002213 return GetReturnTypeDescriptor()[0] == 'V';
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002214}
2215
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002216inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002217 // This is safe from overflow because the array was already allocated, so we know it's sane.
2218 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002219}
2220
2221template<class T>
2222void ObjectArray<T>::Set(int32_t i, T* object) {
2223 if (IsValidIndex(i)) {
2224 if (object != NULL) {
2225 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002226 if (!object->InstanceOf(element_class)) {
2227 ThrowArrayStoreException(object);
2228 return;
2229 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002230 }
2231 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2232 SetFieldObject(data_offset, object, false);
2233 }
2234}
2235
2236template<class T>
2237void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2238 DCHECK(IsValidIndex(i));
2239 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2240 SetFieldObject(data_offset, object, false);
2241}
2242
2243template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002244T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2245 DCHECK(IsValidIndex(i));
2246 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2247 return GetFieldObject<T*>(data_offset, false);
2248}
2249
2250template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002251void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2252 ObjectArray<T>* dst, int dst_pos,
2253 size_t length) {
2254 if (src->IsValidIndex(src_pos) &&
2255 src->IsValidIndex(src_pos+length-1) &&
2256 dst->IsValidIndex(dst_pos) &&
2257 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogers5d76c432011-10-31 21:42:49 -07002258 MemberOffset src_offset(DataOffset().Int32Value() + src_pos * sizeof(Object*));
2259 MemberOffset dst_offset(DataOffset().Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002260 Class* array_class = dst->GetClass();
2261 if (array_class == src->GetClass()) {
2262 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002263 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002264 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Ian Rogers5d76c432011-10-31 21:42:49 -07002265 Heap::VerifyObject(object);
2266 // directly set field, we do a bulk write barrier at the end
2267 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002268 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2269 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2270 }
2271 } else {
2272 Class* element_class = array_class->GetComponentType();
2273 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002274 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002275 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002276 if (object != NULL && !object->InstanceOf(element_class)) {
2277 dst->ThrowArrayStoreException(object);
2278 return;
2279 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002280 Heap::VerifyObject(object);
2281 // directly set field, we do a bulk write barrier at the end
2282 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002283 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2284 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2285 }
2286 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002287 Heap::WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002288 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002289}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002290
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002291class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002292 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002293 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002294 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002295 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002296 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2297};
2298
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002299class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002300 private:
2301 CharArray* ASCII_;
2302 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002303 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002304 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002305 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002306 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2307};
2308
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002309class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002310 private:
2311 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2312 uint32_t TYPE_BOOLEAN_;
2313 uint32_t TYPE_BYTE_;
2314 uint32_t TYPE_CHAR_;
2315 uint32_t TYPE_DOUBLE_;
2316 uint32_t TYPE_FLOAT_;
2317 uint32_t TYPE_INTEGER_;
2318 uint32_t TYPE_LONG_;
2319 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002320 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002321 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2322};
2323
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002324class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002325 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002326 Object* ORDER_BY_SIGNATURE_;
2327 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002328 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2329};
2330
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002331template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002332class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002333 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002334 typedef T ElementType;
2335
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002336 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002337
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002338 const T* GetData() const {
2339 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002340 }
2341
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002342 T* GetData() {
2343 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002344 }
2345
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002346 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002347 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002348 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002349 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002350 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002351 }
2352
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002353 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002354 // TODO: ArrayStoreException
2355 if (IsValidIndex(i)) {
2356 GetData()[i] = value;
2357 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002358 }
2359
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002360 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002361 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002362 CHECK(array_class != NULL);
2363 array_class_ = array_class;
2364 }
2365
Brian Carlstroma663ea52011-08-19 23:33:41 -07002366 static void ResetArrayClass() {
2367 CHECK(array_class_ != NULL);
2368 array_class_ = NULL;
2369 }
2370
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002371 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002372 // Location of first element.
2373 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002374
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002375 static Class* array_class_;
2376
Carl Shapirof88c9522011-08-06 15:47:38 -07002377 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002378};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002379
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002380inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2381 DCHECK(NULL == GetFieldObject<IntArray*>(
2382 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2383 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2384 new_interfaces_idx, false);
2385}
2386
2387// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002388class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002389 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002390 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002391 const CharArray* result = GetFieldObject<const CharArray*>(
2392 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2393 DCHECK(result != NULL);
2394 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002395 }
2396
Elliott Hughes814e4032011-08-23 12:07:56 -07002397 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002398 int32_t result = GetField32(
2399 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2400 DCHECK_LE(0, result);
2401 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002402 }
2403
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002404 int32_t GetLength() const;
2405
Brian Carlstrom395520e2011-09-25 19:35:00 -07002406 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002407
2408 void ComputeHashCode() {
2409 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002410 }
2411
Elliott Hughes814e4032011-08-23 12:07:56 -07002412 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002413 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002414 }
2415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002416 uint16_t CharAt(int32_t index) const;
2417
Brian Carlstromc74255f2011-09-11 22:47:39 -07002418 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002419
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002420 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002421 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002422 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002424 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002425
Jesse Wilson8989d992011-08-02 13:39:42 -07002426 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002427 const char* utf8_data_in);
2428
2429 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2430
2431 static String* Alloc(Class* java_lang_String, CharArray* array);
2432
2433 bool Equals(const char* modified_utf8) const;
2434
2435 // TODO: do we need this overload? give it a more intention-revealing name.
2436 bool Equals(const StringPiece& modified_utf8) const;
2437
2438 bool Equals(const String* that) const;
2439
Ian Rogers0571d352011-11-03 19:51:38 -07002440 // Compare UTF-16 code point values not in a locale-sensitive manner
2441 int Compare(int32_t utf16_length, const char* utf8_data_in);
2442
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002443 // TODO: do we need this overload? give it a more intention-revealing name.
2444 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2445 int32_t that_length) const;
2446
2447 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2448 std::string ToModifiedUtf8() const;
2449
2450 static Class* GetJavaLangString() {
2451 DCHECK(java_lang_String_ != NULL);
2452 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002453 }
2454
Brian Carlstroma663ea52011-08-19 23:33:41 -07002455 static void SetClass(Class* java_lang_String);
2456 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002457
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002458 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002459 void SetHashCode(int32_t new_hash_code) {
2460 DCHECK_EQ(0u,
2461 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2462 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2463 new_hash_code, false);
2464 }
2465
2466 void SetCount(int32_t new_count) {
2467 DCHECK_LE(0, new_count);
2468 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2469 }
2470
2471 void SetOffset(int32_t new_offset) {
2472 DCHECK_LE(0, new_offset);
2473 DCHECK_GE(GetLength(), new_offset);
2474 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2475 }
2476
2477 void SetArray(CharArray* new_array) {
2478 DCHECK(new_array != NULL);
2479 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2480 }
2481
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002482 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2483 CharArray* array_;
2484
Brian Carlstromdbc05252011-09-09 01:59:59 -07002485 int32_t count_;
2486
Carl Shapirof88c9522011-08-06 15:47:38 -07002487 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002488
Elliott Hughes289da822011-08-16 10:11:20 -07002489 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002490
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002491 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002492
Brian Carlstrom693267a2011-09-06 09:25:34 -07002493 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002494 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002495};
2496
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002497struct StringHashCode {
2498 int32_t operator()(art::String* string) const {
2499 return string->GetHashCode();
2500 }
2501};
2502
2503inline String* Field::GetName() const {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002504 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
2505 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2506 DCHECK(result != NULL);
2507 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002508}
2509
2510inline void Field::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002511 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002512}
2513
2514inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002515 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002516 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2517}
2518
2519inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002520 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002521 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2522}
2523
2524inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002525 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002526 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002527}
2528
2529inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002530 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002531 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002532}
2533
Ian Rogers466bb252011-10-14 03:29:56 -07002534inline String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002535 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002536 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002537 DCHECK(result != NULL);
2538 return result;
2539}
2540
2541inline void Method::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002542 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002543}
2544
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002545inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002546 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002547 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002548}
2549
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002550inline void Method::SetShorty(String* new_shorty) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002551 DCHECK(NULL == GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002552 DCHECK_LE(1, new_shorty->GetLength());
2553 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2554}
2555
Ian Rogers466bb252011-10-14 03:29:56 -07002556inline String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002557 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002558 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002559 DCHECK(result != NULL);
2560 return result;
2561}
2562
2563inline void Method::SetSignature(String* new_signature) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002564 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_), new_signature, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002565}
2566
Ian Rogers466bb252011-10-14 03:29:56 -07002567inline void Method::SetExceptionTypes(ObjectArray<Class>* exception_types) {
2568 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), exception_types, false);
2569}
2570
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002571inline uint32_t Class::GetAccessFlags() const {
2572 // Check class is loaded or this is java.lang.String that has a
2573 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002574 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002575 this == String::GetJavaLangString() ||
2576 this == Field::GetJavaLangReflectField() ||
2577 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002578 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002579 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2580}
2581
2582inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002583 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002584 DCHECK(new_descriptor != NULL);
2585 DCHECK_NE(0, new_descriptor->GetLength());
2586 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2587 new_descriptor, false);
2588}
2589
Brian Carlstromc74255f2011-09-11 22:47:39 -07002590inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002591 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002592 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2593}
2594
2595inline void Class::SetSourceFile(String* new_source_file) {
2596 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2597}
2598
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002599inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002600 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002601 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2602}
2603
2604inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002605 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002606 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002607}
2608
2609inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002610 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002611 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002612}
2613
2614inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002615 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002616 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002617}
2618
2619inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002620 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002621 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002622}
2623
2624inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002625 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002626 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2627}
2628
2629// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002630class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002631 public:
2632 void SetDetailMessage(String* new_detail_message) {
2633 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2634 new_detail_message, false);
2635 }
Ian Rogers9074b992011-10-26 17:41:55 -07002636 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002637
Ian Rogers466bb252011-10-14 03:29:56 -07002638 bool IsCheckedException() const;
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002639 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002640 Object* GetStackState() const {
2641 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2642 }
2643
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002644 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2645 Throwable* cause_;
2646 String* detail_message_;
2647 Object* stack_state_; // Note this is Java volatile:
2648 Object* stack_trace_;
2649 Object* suppressed_exceptions_;
2650
Brian Carlstrom693267a2011-09-06 09:25:34 -07002651 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002652 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2653};
2654
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002655// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002656class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002657 public:
2658 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002659 return GetFieldObject<const String*>(
2660 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002661 }
2662
2663 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002664 return GetFieldObject<const String*>(
2665 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002666 }
2667
2668 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002669 return GetFieldObject<const String*>(
2670 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002671 }
2672
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002673 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002674 return GetField32(
2675 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002676 }
2677
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002678 static StackTraceElement* Alloc(const String* declaring_class,
2679 const String* method_name,
2680 const String* file_name,
2681 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002682
2683 static void SetClass(Class* java_lang_StackTraceElement);
2684
2685 static void ResetClass();
2686
2687 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002688 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002689 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002690 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002691 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002692 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002693
2694 static Class* GetStackTraceElement() {
2695 DCHECK(java_lang_StackTraceElement_ != NULL);
2696 return java_lang_StackTraceElement_;
2697 }
2698
2699 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002700
2701 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002702 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2703};
2704
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002705class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002706 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002707 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002708 Class* interface = Get(kInterface)->AsClass();
2709 DCHECK(interface != NULL);
2710 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002711 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002712
Brian Carlstrom30b94452011-08-25 21:35:26 -07002713 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002714 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002715 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002716 DCHECK(Get(kInterface) == NULL);
2717 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002718 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002719
Brian Carlstrom86927212011-09-15 11:31:11 -07002720 size_t GetMethodArrayCount() const {
2721 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2722 if (method_array == 0) {
2723 return 0;
2724 }
2725 return method_array->GetLength();
2726 }
2727
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002728 ObjectArray<Method>* GetMethodArray() const {
2729 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2730 DCHECK(method_array != NULL);
2731 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002732 }
2733
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002734 void SetMethodArray(ObjectArray<Method>* new_ma) {
2735 DCHECK(new_ma != NULL);
2736 DCHECK(Get(kMethodArray) == NULL);
2737 Set(kMethodArray, new_ma);
2738 }
2739
2740 static size_t LengthAsArray() {
2741 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002742 }
2743
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002744 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002745
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002746 enum ArrayIndex {
2747 // Points to the interface class.
2748 kInterface = 0,
2749 // Method pointers into the vtable, allow fast map from interface
2750 // method index to concrete instance method.
2751 kMethodArray = 1,
2752 kMax = 2,
2753 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002754
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002755 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002756};
2757
Jesse Wilson95caa792011-10-12 18:14:17 -04002758class MANAGED ProxyClass : public Class {
2759 private:
2760 int32_t NextClassNameIndex_;
2761 int64_t serialVersionUID_;
2762 friend struct ProxyClassOffsets; // for verifying offset information
2763 DISALLOW_IMPLICIT_CONSTRUCTORS(ProxyClass);
2764};
2765
2766class MANAGED Proxy : public Object {
2767 private:
2768 Object* h_;
2769
2770 friend struct ProxyOffsets; // for verifying offset information
2771 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2772};
2773
Carl Shapiro1fb86202011-06-27 17:43:13 -07002774} // namespace art
2775
2776#endif // ART_SRC_OBJECT_H_