blob: 661087160146a7059832f9279ce3d8528c02fc52 [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 Carlstrom4873d462011-08-21 15:23:39 -0700214 bool IsClassClass() const;
215
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 bool IsObjectArray() const;
217
218 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700219 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700221 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700222 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Brian Carlstromb63ec392011-08-27 17:38:27 -0700224 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225
226 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700227 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700228 return down_cast<Array*>(this);
229 }
230
231 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700232 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700233 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234 }
235
Brian Carlstroma663ea52011-08-19 23:33:41 -0700236 bool IsString() const;
237
238 String* AsString() {
239 DCHECK(IsString());
240 return down_cast<String*>(this);
241 }
242
243 bool IsMethod() const;
244
245 Method* AsMethod() {
246 DCHECK(IsMethod());
247 return down_cast<Method*>(this);
248 }
249
Brian Carlstrom4873d462011-08-21 15:23:39 -0700250 const Method* AsMethod() const {
251 DCHECK(IsMethod());
252 return down_cast<const Method*>(this);
253 }
254
Brian Carlstroma663ea52011-08-19 23:33:41 -0700255 bool IsField() const;
256
257 Field* AsField() {
258 DCHECK(IsField());
259 return down_cast<Field*>(this);
260 }
261
Brian Carlstrom4873d462011-08-21 15:23:39 -0700262 const Field* AsField() const {
263 DCHECK(IsField());
264 return down_cast<const Field*>(this);
265 }
266
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700267 bool IsReferenceInstance() const;
268
269 bool IsWeakReferenceInstance() const;
270
271 bool IsSoftReferenceInstance() const;
272
273 bool IsFinalizerReferenceInstance() const;
274
275 bool IsPhantomReferenceInstance() const;
276
277 // Accessors for Java type fields
278 template<class T>
279 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700280 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
281 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700282 Heap::VerifyObject(result);
283 return result;
284 }
285
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700286 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700287 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700288 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
289 if (new_value != NULL) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700290 Heap::WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700292 }
293
294 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
295 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700296 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
297 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700298 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700299 return android_atomic_acquire_load(word_addr);
300 } else {
301 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700303 }
304
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700305 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
306 if (this_is_valid) {
307 Heap::VerifyObject(this);
308 }
309 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700310 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700311 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700312 /*
313 * TODO: add an android_atomic_synchronization_store() function and
314 * use it in the 32-bit volatile set handlers. On some platforms we
315 * can use a fast atomic instruction and avoid the barriers.
316 */
317 ANDROID_MEMBAR_STORE();
318 *word_addr = new_value;
319 ANDROID_MEMBAR_FULL();
320 } else {
321 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700323 }
324
325 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
326 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700327 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700328 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700329 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700330 uint64_t result = QuasiAtomicRead64(addr);
331 ANDROID_MEMBAR_FULL();
332 return result;
333 } else {
334 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
337
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700338 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700339 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700340 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
341 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700342 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700343 ANDROID_MEMBAR_STORE();
344 QuasiAtomicSwap64(new_value, addr);
345 // Post-store barrier not required due to use of atomic op or mutex.
346 } else {
347 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700349 }
350
351 protected:
352 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700353 template<class T>
354 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700355 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
357
358 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700359 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
360 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700361 }
362
363 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700364 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Elliott Hughes5f791332011-09-15 17:45:30 -0700366 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367
Elliott Hughes5f791332011-09-15 17:45:30 -0700368 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700369 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700370 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700371};
372
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700373struct ObjectIdentityHash {
374 size_t operator()(const art::Object* const& obj) const {
375#ifdef MOVING_GARBAGE_COLLECTOR
376 // TODO: we'll need to use the Object's internal concept of identity
377 UNIMPLEMENTED(FATAL);
378#endif
379 return reinterpret_cast<size_t>(obj);
380 }
381};
382
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700384class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400385 private:
386 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700388 friend struct AccessibleObjectOffsets; // for verifying offset information
389 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400390};
391
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700392// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700393class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700394 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700396
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700397 void SetDeclaringClass(Class *new_declaring_class);
398
Jesse Wilsonc4824e62011-11-01 14:39:04 -0400399 String* GetName() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400
401 void SetName(String* new_name);
402
403 uint32_t GetAccessFlags() const;
404
405 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700406 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700407 }
408
Elliott Hughes80609252011-09-23 17:24:51 -0700409 bool IsPublic() const {
410 return (GetAccessFlags() & kAccPublic) != 0;
411 }
412
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700413 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700415 }
416
jeffhaobdb76512011-09-07 11:43:16 -0700417 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700418 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700419 }
420
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700424
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 // Gets type using type index and resolved types in the dex cache, may be null
426 // if type isn't yet resolved
427 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700428
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700429 bool IsPrimitiveType() const;
430
431 Primitive::Type GetPrimitiveType() const;
432
433 size_t PrimitiveSize() const;
434
435 const char* GetTypeDescriptor() const;
436
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700437 // Performs full resolution, may return null and set exceptions if type cannot
438 // be resolved
Ian Rogers5d76c432011-10-31 21:42:49 -0700439 Class* GetType();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700440
441 // Offset to field within an Object
442 MemberOffset GetOffset() const;
443
buzbee34cd9e52011-09-08 14:31:52 -0700444 static MemberOffset OffsetOffset() {
445 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
446 }
447
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448 MemberOffset GetOffsetDuringLinking() const;
449
450 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700451
Brian Carlstrom4873d462011-08-21 15:23:39 -0700452 // field access, null object for static fields
453 bool GetBoolean(const Object* object) const;
454 void SetBoolean(Object* object, bool z) const;
455 int8_t GetByte(const Object* object) const;
456 void SetByte(Object* object, int8_t b) const;
457 uint16_t GetChar(const Object* object) const;
458 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700459 int16_t GetShort(const Object* object) const;
460 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700461 int32_t GetInt(const Object* object) const;
462 void SetInt(Object* object, int32_t i) const;
463 int64_t GetLong(const Object* object) const;
464 void SetLong(Object* object, int64_t j) const;
465 float GetFloat(const Object* object) const;
466 void SetFloat(Object* object, float f) const;
467 double GetDouble(const Object* object) const;
468 void SetDouble(Object* object, double d) const;
469 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700470 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700471
Ian Rogersce9eca62011-10-07 17:11:03 -0700472 // raw field accesses
473 uint32_t Get32(const Object* object) const;
474 void Set32(Object* object, uint32_t new_value) const;
475 uint64_t Get64(const Object* object) const;
476 void Set64(Object* object, uint64_t new_value) const;
477 Object* GetObj(const Object* object) const;
478 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700479
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700480 static Class* GetJavaLangReflectField() {
481 DCHECK(java_lang_reflect_Field_ != NULL);
482 return java_lang_reflect_Field_;
483 }
484
485 static void SetClass(Class* java_lang_reflect_Field);
486 static void ResetClass();
487
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488 bool IsVolatile() const {
489 return (GetAccessFlags() & kAccVolatile) != 0;
490 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700491
Elliott Hughes80609252011-09-23 17:24:51 -0700492 void InitJavaFields();
493
buzbee1da522d2011-09-04 11:22:20 -0700494 private:
Elliott Hughes80609252011-09-23 17:24:51 -0700495 void InitJavaFieldsLocked();
496
Jesse Wilson35baaab2011-08-10 16:18:03 -0400497 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700498
Jesse Wilson35baaab2011-08-10 16:18:03 -0400499 // The class in which this field is declared.
500 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700501
Jesse Wilson35baaab2011-08-10 16:18:03 -0400502 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700503
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700504 String* name_;
Brian Carlstromdbc05252011-09-09 01:59:59 -0700505
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700506 // The possibly null type of the field
Ian Rogers5d76c432011-10-31 21:42:49 -0700507 Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400508
Brian Carlstromdbc05252011-09-09 01:59:59 -0700509 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700510
Jesse Wilson35baaab2011-08-10 16:18:03 -0400511 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700512
513 // Offset of field within an instance or in the Class' static fields
514 uint32_t offset_;
515
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700516 // Dex cache index of resolved type
517 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400518
Brian Carlstrom693267a2011-09-06 09:25:34 -0700519 int32_t slot_;
520
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700521 static Class* java_lang_reflect_Field_;
522
Brian Carlstrom693267a2011-09-06 09:25:34 -0700523 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400524 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700525};
526
Jesse Wilson6bf19152011-09-29 13:12:33 -0400527// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700528class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700530 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700531 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700532 Object* obj,
533 Thread* thread,
534 byte* args,
535 JValue* result);
536
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700537 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700538
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700539 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700540
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700541 static MemberOffset DeclaringClassOffset() {
542 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700543 }
544
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545 // Returns the method name, e.g. "<init>" or "eatLunch"
Ian Rogers466bb252011-10-14 03:29:56 -0700546 String* GetName() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700547
548 void SetName(String* new_name);
549
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700550 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700551
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700552 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700553
Ian Rogers466bb252011-10-14 03:29:56 -0700554 String* GetSignature() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555
556 void SetSignature(String* new_signature);
557
Ian Rogers466bb252011-10-14 03:29:56 -0700558 bool HasSameNameAndSignature(const Method* that) const {
559 return GetName() == that->GetName() && GetSignature() == that->GetSignature();
560 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561
562 uint32_t GetAccessFlags() const;
563
564 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700565 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700566 }
567
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700568 // Returns true if the method is declared public.
569 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700570 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700571 }
572
573 // Returns true if the method is declared private.
574 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576 }
577
578 // Returns true if the method is declared static.
579 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700580 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581 }
582
Brian Carlstrom1f870082011-08-23 16:02:11 -0700583 // Returns true if the method is a constructor.
584 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700585 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700586 }
587
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700588 // Is this method <clinit>
589 bool IsClassInitializer() const;
590
Brian Carlstrom1f870082011-08-23 16:02:11 -0700591 // Returns true if the method is static, private, or a constructor.
592 bool IsDirect() const {
593 return IsStatic() || IsPrivate() || IsConstructor();
594 }
595
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700596 // Returns true if the method is declared synchronized.
597 bool IsSynchronized() const {
598 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700599 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700600 }
601
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700602 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700603 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700604 }
605
Elliott Hughes80609252011-09-23 17:24:51 -0700606 bool IsMiranda() const {
607 return (GetAccessFlags() & kAccMiranda) != 0;
608 }
609
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700610 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700612 }
613
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700614 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700615 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700616 }
617
618 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700619 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700620 }
621
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700622 uint16_t GetMethodIndex() const;
623
624 size_t GetVtableIndex() const {
625 return GetMethodIndex();
626 }
627
628 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700629 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700630 }
631
632 static MemberOffset MethodIndexOffset() {
633 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
634 }
635
636 uint32_t GetCodeItemOffset() const {
637 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
638 }
639
640 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700641 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700642 }
643
644 // Number of 32bit registers that would be required to hold all the arguments
645 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700646
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700647 // Number of argument bytes required for densely packing the
648 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700649 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700650
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700651 uint16_t NumRegisters() const;
652
653 void SetNumRegisters(uint16_t new_num_registers) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700654 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700655 }
656
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 uint16_t NumIns() const;
658
659 void SetNumIns(uint16_t new_num_ins) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700660 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700661 }
662
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700663 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700664
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700665 void SetNumOuts(uint16_t new_num_outs) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700666 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700667 }
668
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700669 uint32_t GetProtoIdx() const;
670
671 void SetProtoIdx(uint32_t new_proto_idx) {
672 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700673 }
674
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700675 ObjectArray<String>* GetDexCacheStrings() const;
676 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
677
678 static MemberOffset DexCacheStringsOffset() {
679 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
680 }
681
buzbee2a475e72011-09-07 17:19:17 -0700682 static MemberOffset DexCacheResolvedTypesOffset() {
683 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
684 }
685
buzbee34cd9e52011-09-08 14:31:52 -0700686 static MemberOffset DexCacheResolvedFieldsOffset() {
687 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
688 }
689
buzbee1da522d2011-09-04 11:22:20 -0700690 static MemberOffset DexCacheInitializedStaticStorageOffset() {
691 return OFFSET_OF_OBJECT_MEMBER(Method,
692 dex_cache_initialized_static_storage_);
693 }
694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700695 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
696 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
697
698 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
699 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
700
701 ObjectArray<Field>* GetDexCacheResolvedFields() const;
702 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
703
704 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
705 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
706
707 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
708 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
709
Ian Rogers466bb252011-10-14 03:29:56 -0700710 // Find the method that this method overrides
711 Method* FindOverriddenMethod() const;
712
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700713 void SetReturnTypeIdx(uint32_t new_return_type_idx);
714
Ian Rogers9074b992011-10-26 17:41:55 -0700715 const char* GetReturnTypeDescriptor() const;
716
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700717 Class* GetReturnType() const;
718
719 bool IsReturnAReference() const;
720
721 bool IsReturnAFloat() const;
722
723 bool IsReturnADouble() const;
724
Ian Rogersb033c752011-07-20 12:22:35 -0700725 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700726 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700727 }
728
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700729 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700730
Ian Rogersae675992011-10-09 17:10:22 -0700731 bool IsReturnALongOrDouble() const {
732 return IsReturnALong() || IsReturnADouble();
733 }
734
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700735 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700736
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700737 // "Args" may refer to any of the 3 levels of "Args."
738 // To avoid confusion, our code will denote which "Args" clearly:
739 // 1. UserArgs: Args that a user see.
740 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
741 // receiver.
742 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
743 // E.g., the first in Args is Method* for both static and non-static
744 // methods. And CConvArgs doesn't deal with the receiver because
745 // receiver is hardwired in an implicit register, so CConvArgs doesn't
746 // need to deal with it.
747 //
748 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700749 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700750
751 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700752 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700753 size_t NumReferenceArgs() const;
754
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700755 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700756 size_t NumLongOrDoubleArgs() const;
757
Ian Rogersb033c752011-07-20 12:22:35 -0700758 // Is the given method parameter a reference?
759 bool IsParamAReference(unsigned int param) const;
760
761 // Is the given method parameter a long or double?
762 bool IsParamALongOrDouble(unsigned int param) const;
763
Ian Rogersdf20fe02011-07-20 20:34:16 -0700764 // Size in bytes of the given parameter
765 size_t ParamSize(unsigned int param) const;
766
767 // Size in bytes of the return value
768 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700769
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700770 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
771
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700772 const void* GetCode() const {
773 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700774 }
775
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700776 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700777 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
778 }
779
780 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700781 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700782 return reinterpret_cast<uint32_t>(GetCode());
783 }
784
785 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700786 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700787 SetCode(reinterpret_cast<void*>(code_offset));
788 }
789
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700790 static MemberOffset GetCodeOffset() {
791 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700792 }
793
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700794 const uint32_t* GetMappingTable() const {
795 const uint32_t* map = GetMappingTableRaw();
796 if (map == NULL) {
797 return map;
798 }
799 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700800 }
801
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700802 uint32_t GetMappingTableLength() const {
803 const uint32_t* map = GetMappingTableRaw();
804 if (map == NULL) {
805 return 0;
806 }
807 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700808 }
809
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700810 const uint32_t* GetMappingTableRaw() const {
811 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700812 }
813
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700814 void SetMappingTable(const uint32_t* mapping_table) {
815 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
816 mapping_table, false);
817 }
818
819 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700820 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700821 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
822 }
823
824 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700825 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700826 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
827 }
828
829 const uint16_t* GetVmapTable() const {
830 const uint16_t* vmap = GetVmapTableRaw();
831 if (vmap == NULL) {
832 return vmap;
833 }
834 return vmap + 1;
835 }
836
837 uint16_t GetVmapTableLength() const {
838 const uint16_t* vmap = GetVmapTableRaw();
839 if (vmap == NULL) {
840 return 0;
841 }
842 return *vmap;
843 }
844
845 const uint16_t* GetVmapTableRaw() const {
846 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
847 }
848
849 void SetVmapTable(const uint16_t* vmap_table) {
850 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
851 }
852
853 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700854 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700855 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
856 }
857
858 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700859 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700860 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700861 }
862
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 Object* GetGcMap() const {
864 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
865 }
866
867 void SetGcMap(Object* data) {
868 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
869 }
870
Shih-wei Liaod11af152011-08-23 16:02:11 -0700871 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700872 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
873 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700874 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
875 return result;
876 }
877
878 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700879 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700880 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
881 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
882 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700883 }
884
Shih-wei Liaod11af152011-08-23 16:02:11 -0700885 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700887 }
888
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700889 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700890
Brian Carlstrom16192862011-09-12 17:50:06 -0700891 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700892
Brian Carlstrom16192862011-09-12 17:50:06 -0700893 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700894
Ian Rogersb033c752011-07-20 12:22:35 -0700895 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700896 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700897 }
898
Brian Carlstrom78128a62011-09-15 17:21:19 -0700899 const void* GetNativeMethod() const {
900 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
901 }
902
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700903 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700904 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700905 InvokeStub* result = GetFieldPtr<InvokeStub*>(
906 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
907 // TODO: DCHECK(result != NULL); should be ahead of time compiled
908 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700909 }
910
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700911 void SetInvokeStub(InvokeStub* invoke_stub) {
912 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
913 invoke_stub, false);
914 }
915
916 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700917 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700918 return reinterpret_cast<uint32_t>(GetInvokeStub());
919 }
920
921 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700922 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700923 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
924 }
925
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700926 static MemberOffset GetInvokeStubOffset() {
927 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700928 }
929
buzbee561227c2011-09-02 15:28:19 -0700930 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
931 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
932 }
933
934 static MemberOffset GetDexCacheResolvedMethodsOffset() {
935 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
936 }
937
938 static MemberOffset GetMethodIndexOffset() {
939 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
940 }
941
Ian Rogers90865722011-09-19 11:11:44 -0700942 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700943 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700944 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700945
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700946 void SetCoreSpillMask(uint32_t core_spill_mask) {
947 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700948 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700949 }
950
Ian Rogers90865722011-09-19 11:11:44 -0700951 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700952 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
953 }
954
955 void SetFpSpillMask(uint32_t fp_spill_mask) {
956 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700957 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700958 }
959
Ian Rogers466bb252011-10-14 03:29:56 -0700960 ObjectArray<Class>* GetExceptionTypes() const {
961 return GetFieldObject<ObjectArray<Class>*>(
962 OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), false);
Jesse Wilson95caa792011-10-12 18:14:17 -0400963 }
964
Ian Rogers466bb252011-10-14 03:29:56 -0700965 void SetExceptionTypes(ObjectArray<Class>* exception_types);
966
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700967 ObjectArray<Class>* GetJavaParameterTypes() const {
968 return GetFieldObject<ObjectArray<Class>*>(
969 OFFSET_OF_OBJECT_MEMBER(Method, java_parameter_types_), false);
970 }
971
Ian Rogers90865722011-09-19 11:11:44 -0700972 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700973 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700974 Runtime* runtime = Runtime::Current();
975 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700976 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700977 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
978 result = true;
979 break;
980 }
981 }
Ian Rogers90865722011-09-19 11:11:44 -0700982 // Check that if we do think it is phony it looks like the callee save method
983 DCHECK(!result || GetCoreSpillMask() != 0);
984 return result;
985 }
986
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700987 // Converts a native PC to a dex PC. TODO: this is a no-op
988 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700989 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700990
991 // Converts a dex PC to a native PC. TODO: this is a no-op
992 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700993 uintptr_t ToNativePC(const uint32_t dex_pc) const;
994
995 // Find the catch block for the given exception type and dex_pc
996 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700997
Elliott Hughes80609252011-09-23 17:24:51 -0700998 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
999
1000 static Class* GetConstructorClass() {
1001 return java_lang_reflect_Constructor_;
1002 }
1003
1004 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001005 return java_lang_reflect_Method_;
1006 }
1007
Elliott Hughes80609252011-09-23 17:24:51 -07001008 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009
Elliott Hughes418d20f2011-09-22 14:00:39 -07001010 void InitJavaFields();
1011
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001012 private:
1013 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001014 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001015
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001016 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1017 // the class we are a part of
1018 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001019 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001020 Object* java_formal_type_parameters_;
1021 Object* java_generic_exception_types_;
1022 Object* java_generic_parameter_types_;
1023 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -07001024
Brian Carlstrom693267a2011-09-06 09:25:34 -07001025 String* name_;
1026
Elliott Hughes418d20f2011-09-22 14:00:39 -07001027 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001028 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001029 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001030
Brian Carlstrom693267a2011-09-06 09:25:34 -07001031 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001032 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1033
1034 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1035 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1036
1037 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1038 ObjectArray<Field>* dex_cache_resolved_fields_;
1039
1040 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1041 ObjectArray<Method>* dex_cache_resolved_methods_;
1042
Brian Carlstromdbc05252011-09-09 01:59:59 -07001043 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1044 ObjectArray<Class>* dex_cache_resolved_types_;
1045
1046 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1047 ObjectArray<String>* dex_cache_strings_;
1048
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 // Garbage collection map
1050 Object* gc_map_;
jeffhaoe23d93c2011-09-15 14:48:43 -07001051
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001052 // The short-form method descriptor string.
1053 String* shorty_;
1054
Brian Carlstromdbc05252011-09-09 01:59:59 -07001055 // The method descriptor. This represents the parameters a method
1056 // takes and value it returns. This string is a list of the type
1057 // descriptors for the parameters enclosed in parenthesis followed
1058 // by the return type descriptor. For example, for the method
1059 //
1060 // Object mymethod(int i, double d, Thread t)
1061 //
1062 // the method descriptor would be
1063 //
1064 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1065 String* signature_;
1066
1067 uint32_t java_generic_types_are_initialized_;
1068
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001069 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001070 uint32_t access_flags_;
1071
1072 // Compiled code associated with this method for callers from managed code.
1073 // May be compiled managed code or a bridge for invoking a native method.
1074 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001075
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001076 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001077 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001078
Brian Carlstrom693267a2011-09-06 09:25:34 -07001079 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001080 uint32_t core_spill_mask_;
1081
1082 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001083 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001084
Brian Carlstrom693267a2011-09-06 09:25:34 -07001085 // Total size in bytes of the frame
1086 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087
Brian Carlstrom693267a2011-09-06 09:25:34 -07001088 // Native invocation stub entry point for calling from native to managed code.
1089 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001090
Brian Carlstrom693267a2011-09-06 09:25:34 -07001091 // Index of the return type
1092 uint32_t java_return_type_idx_;
1093
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001095 const uint32_t* mapping_table_;
1096
Ian Rogers466bb252011-10-14 03:29:56 -07001097 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001098 //
Ian Rogers466bb252011-10-14 03:29:56 -07001099 // For abstract methods in an interface class, this is the offset of the method in
1100 // "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001101 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001102
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001103 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001104 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001105
Brian Carlstrom693267a2011-09-06 09:25:34 -07001106 // Method bounds; not needed for an abstract method.
1107 //
1108 // For a native method, we compute the size of the argument list, and
1109 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001110 uint32_t num_ins_;
1111 uint32_t num_outs_;
1112 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001113
Brian Carlstrom693267a2011-09-06 09:25:34 -07001114 // Method prototype descriptor string (return and argument types).
1115 uint32_t proto_idx_;
1116
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 // When a register is promoted into a register, the spill mask holds which registers hold dex
1118 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
1119 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001120 const uint16_t* vmap_table_;
1121
Brian Carlstrom693267a2011-09-06 09:25:34 -07001122 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001123
Elliott Hughes80609252011-09-23 17:24:51 -07001124 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001125 static Class* java_lang_reflect_Method_;
1126
Brian Carlstrom693267a2011-09-06 09:25:34 -07001127 friend class ImageWriter; // for relocating code_ and invoke_stub_
1128 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001129 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001130};
1131
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001132class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001133 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001134 // A convenience for code that doesn't know the component size,
1135 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001136 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001137
Brian Carlstromb63ec392011-08-27 17:38:27 -07001138 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001139
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001140 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001141
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001142 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001144 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001145
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001146 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001147 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001148 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001149 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001150
buzbeec143c552011-08-20 17:38:58 -07001151 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001152 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001153 }
1154
1155 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001156 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001157 }
1158
Elliott Hughesbf86d042011-08-31 17:53:14 -07001159 void* GetRawData() {
1160 return reinterpret_cast<void*>(first_element_);
1161 }
1162
Elliott Hughes289da822011-08-16 10:11:20 -07001163 protected:
1164 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001165 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001166 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001167 }
1168 return true;
1169 }
1170
Elliott Hughes80609252011-09-23 17:24:51 -07001171 protected:
1172 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1173 bool ThrowArrayStoreException(Object* object) const;
1174
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001175 private:
1176 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001177 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001178 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1179 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001180 // Marker for the data (used by generated code)
1181 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001182
Carl Shapirof88c9522011-08-06 15:47:38 -07001183 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001184};
1185
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001186template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001187class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001188 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001189 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001190
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001191 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001192
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001193 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001194
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001195 // Set element without bound and element type checks, to be used in limited
1196 // circumstances, such as during boot image writing
1197 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001198
Ian Rogers5d76c432011-10-31 21:42:49 -07001199 T* GetWithoutChecks(int32_t i) const;
1200
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001201 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001202 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001203 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001204
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001205 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001206
1207 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001208 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001209};
1210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001211template<class T>
1212ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1213 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1214}
1215
1216template<class T>
1217T* ObjectArray<T>::Get(int32_t i) const {
1218 if (!IsValidIndex(i)) {
1219 return NULL;
1220 }
1221 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1222 return GetFieldObject<T*>(data_offset, false);
1223}
1224
1225template<class T>
1226ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1227 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1228 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1229 return new_array;
1230}
1231
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001232// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001233// provides the static storage. However, this might change to an Array
1234// to improve image sharing, so we use this type to avoid assumptions
1235// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001236class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001237
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001238// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001239class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001240 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001241
1242 // Class Status
1243 //
1244 // kStatusNotReady: If a Class cannot be found in the class table by
1245 // FindClass, it allocates an new one with AllocClass in the
1246 // kStatusNotReady and calls LoadClass. Note if it does find a
1247 // class, it may not be kStatusResolved and it will try to push it
1248 // forward toward kStatusResolved.
1249 //
1250 // kStatusIdx: LoadClass populates with Class with information from
1251 // the DexFile, moving the status to kStatusIdx, indicating that the
1252 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001253 // populated based on super_class_type_idx_ and
1254 // interfaces_type_idx_. The new Class can then be inserted into the
1255 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001256 //
1257 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1258 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1259 // using ResolveClass to initialize the super_class_ and interfaces_.
1260 //
1261 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001262 // shows linking is complete and fields of the Class populated by making
1263 // it kStatusResolved. Java allows circularities of the form where a super
1264 // class has a field that is of the type of the sub class. We need to be able
1265 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001266
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001267 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001268 kStatusError = -1,
1269 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001270 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001271 kStatusLoaded = 2, // DEX idx values resolved
1272 kStatusResolved = 3, // part of linking
1273 kStatusVerifying = 4, // in the process of being verified
1274 kStatusVerified = 5, // logically part of linking; done pre-init
1275 kStatusInitializing = 6, // class init in progress
1276 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001277 };
1278
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001279 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001280 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1281 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001282 }
1283
1284 void SetStatus(Status new_status);
1285
1286 // Returns true if the class has failed to link.
1287 bool IsErroneous() const {
1288 return GetStatus() == kStatusError;
1289 }
1290
1291 // Returns true if the class has been loaded.
1292 bool IsIdxLoaded() const {
1293 return GetStatus() >= kStatusIdx;
1294 }
1295
1296 // Returns true if the class has been loaded.
1297 bool IsLoaded() const {
1298 return GetStatus() >= kStatusLoaded;
1299 }
1300
1301 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001302 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001303 return GetStatus() >= kStatusResolved;
1304 }
1305
1306 // Returns true if the class has been verified.
1307 bool IsVerified() const {
1308 return GetStatus() >= kStatusVerified;
1309 }
1310
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001311 // Returns true if the class is initializing.
1312 bool IsInitializing() const {
1313 return GetStatus() >= kStatusInitializing;
1314 }
1315
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001316 // Returns true if the class is initialized.
1317 bool IsInitialized() const {
1318 return GetStatus() == kStatusInitialized;
1319 }
1320
1321 uint32_t GetAccessFlags() const;
1322
1323 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001324 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 }
1326
1327 // Returns true if the class is an interface.
1328 bool IsInterface() const {
1329 return (GetAccessFlags() & kAccInterface) != 0;
1330 }
1331
1332 // Returns true if the class is declared public.
1333 bool IsPublic() const {
1334 return (GetAccessFlags() & kAccPublic) != 0;
1335 }
1336
1337 // Returns true if the class is declared final.
1338 bool IsFinal() const {
1339 return (GetAccessFlags() & kAccFinal) != 0;
1340 }
1341
Elliott Hughes20cde902011-10-04 17:37:27 -07001342 bool IsFinalizable() const {
1343 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1344 }
1345
1346 void SetFinalizable() {
1347 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1348 SetAccessFlags(flags | kAccClassIsFinalizable);
1349 }
1350
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001351 // Returns true if the class is abstract.
1352 bool IsAbstract() const {
1353 return (GetAccessFlags() & kAccAbstract) != 0;
1354 }
1355
1356 // Returns true if the class is an annotation.
1357 bool IsAnnotation() const {
1358 return (GetAccessFlags() & kAccAnnotation) != 0;
1359 }
1360
1361 // Returns true if the class is synthetic.
1362 bool IsSynthetic() const {
1363 return (GetAccessFlags() & kAccSynthetic) != 0;
1364 }
1365
1366 bool IsReferenceClass() const {
1367 return (GetAccessFlags() & kAccClassIsReference) != 0;
1368 }
1369
1370 bool IsWeakReferenceClass() const {
1371 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1372 }
1373
1374 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001375 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 }
1377
1378 bool IsFinalizerReferenceClass() const {
1379 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1380 }
1381
1382 bool IsPhantomReferenceClass() const {
1383 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1384 }
1385
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001386 Primitive::Type GetPrimitiveType() const {
1387 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1388 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001389 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1390 }
1391
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001392 void SetPrimitiveType(Primitive::Type new_type) {
1393 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001394 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001395 }
1396
1397 // Returns true if the class is a primitive type.
1398 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001399 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001400 }
1401
1402 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001403 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001404 }
1405
1406 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001407 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001408 }
1409
1410 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001411 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 }
1413
1414 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001415 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001416 }
1417
1418 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001419 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001420 }
1421
1422 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001423 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 }
1425
1426 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001427 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001428 }
1429
1430 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001431 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001432 }
1433
1434 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001435 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001436 }
1437
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 // Depth of class from java.lang.Object
1439 size_t Depth() {
1440 size_t depth = 0;
1441 for(Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
1442 depth++;
1443 }
1444 return depth;
1445 }
1446
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001447 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001448 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001449 }
1450
1451 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001452 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001453 }
1454
1455 void SetComponentType(Class* new_component_type) {
1456 DCHECK(GetComponentType() == NULL);
1457 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001458 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 }
1460
1461 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001462 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001463 }
1464
1465 bool IsObjectClass() const {
1466 return !IsPrimitive() && GetSuperClass() == NULL;
1467 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001468 bool IsInstantiable() const {
1469 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1470 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001471
Brian Carlstrom1f870082011-08-23 16:02:11 -07001472 // Creates a raw object instance but does not invoke the default constructor.
1473 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001474
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001475 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 const String* result = GetFieldObject<const String*>(
1477 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1478 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1479 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1480 return result;
1481 }
1482
1483 void SetDescriptor(String* new_descriptor);
1484
1485 bool IsVariableSize() const {
1486 // Classes and arrays vary in size, and so the object_size_ field cannot
1487 // be used to get their instance size
1488 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 }
1490
Brian Carlstrom4873d462011-08-21 15:23:39 -07001491 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001492 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001493 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001494 }
1495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001497 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001498 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 }
1500
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501 void SetClassSize(size_t new_class_size) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001502 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughes54e7df12011-09-16 11:47:04 -07001503 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 }
1505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001506 size_t GetObjectSize() const {
1507 CHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001508 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001509 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001510 CHECK_GE(result, sizeof(Object));
1511 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512 }
1513
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001514 void SetObjectSize(size_t new_object_size) {
1515 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001516 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001517 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001518 }
1519
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001520 // Returns true if this class is in the same packages as that class.
1521 bool IsInSamePackage(const Class* that) const;
1522
Brian Carlstrome24fa612011-09-29 00:53:55 -07001523 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001524
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001525 // Returns true if this class can access that class.
1526 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001528 }
1529
jeffhao4a801a42011-09-23 13:53:40 -07001530 // Validate method/field access.
1531 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1532 // quick accept for public access
1533 if (member_flags & kAccPublic) {
1534 return true;
1535 }
1536
1537 // quick accept for access from same class
1538 if (this == access_to) {
1539 return true;
1540 }
1541
1542 // quick reject for private access from another class
1543 if (member_flags & kAccPrivate) {
1544 return false;
1545 }
1546
1547 // Semi-quick test for protected access from a sub-class, which may or
1548 // may not be in the same package.
1549 if (member_flags & kAccProtected) {
1550 if (this->IsSubClass(access_to)) {
1551 return true;
1552 }
1553 }
1554
1555 // Allow protected and private access from other classes in the same package.
1556 return this->IsInSamePackage(access_to);
1557 }
1558
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001559 bool IsSubClass(const Class* klass) const;
1560
Ian Rogersd81871c2011-10-03 13:57:23 -07001561 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1562 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1563 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1564 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1565 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001566 bool IsAssignableFrom(const Class* src) const {
1567 DCHECK(src != NULL);
1568 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001569 // Can always assign to things of the same type
1570 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001571 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001572 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001573 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001574 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001575 return src->Implements(this);
1576 } else if (src->IsArrayClass()) {
1577 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001580 }
1581 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001582
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 Class* GetSuperClass() const {
1584 // Can only get super class for loaded classes (hack for when runtime is
1585 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001586 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001587 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001588 }
1589
buzbee4a3164f2011-09-03 11:25:10 -07001590 static MemberOffset SuperClassOffset() {
1591 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1592 }
1593
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001594 void SetSuperClass(Class *new_super_class) {
1595 // super class is assigned once, except during class linker initialization
1596 Class* old_super_class = GetFieldObject<Class*>(
1597 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1598 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1599 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001600 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001601 }
1602
1603 bool HasSuperClass() const {
1604 return GetSuperClass() != NULL;
1605 }
1606
1607 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001608 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001609 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1610 false);
1611 }
1612
1613 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001614 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001615 }
1616
1617 const ClassLoader* GetClassLoader() const;
1618
1619 void SetClassLoader(const ClassLoader* new_cl);
1620
1621 static MemberOffset DexCacheOffset() {
1622 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1623 }
1624
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001625 enum {
1626 kDumpClassFullDetail = 1,
1627 kDumpClassClassLoader = (1 << 1),
1628 kDumpClassInitialized = (1 << 2),
1629 };
1630
Elliott Hughes4681c802011-09-25 18:04:37 -07001631 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001632
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001633 DexCache* GetDexCache() const;
1634
1635 void SetDexCache(DexCache* new_dex_cache);
1636
1637 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001638 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001639 return GetFieldObject<ObjectArray<Method>*>(
1640 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1641 }
1642
1643 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1644 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1645 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1646 DCHECK_NE(0, new_direct_methods->GetLength());
1647 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1648 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001649 }
1650
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001651 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001653 }
1654
1655 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001656 ObjectArray<Method>* direct_methods =
1657 GetFieldObject<ObjectArray<Method>*>(
1658 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1659 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001660 }
1661
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 // Returns the number of static, private, and constructor methods.
1663 size_t NumDirectMethods() const {
1664 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1665 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001666
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001667 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001668 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001669 return GetFieldObject<ObjectArray<Method>*>(
1670 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1671 }
1672
1673 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1674 // TODO: we reassign virtual methods to grow the table for miranda
1675 // methods.. they should really just be assigned once
1676 DCHECK_NE(0, new_virtual_methods->GetLength());
1677 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1678 new_virtual_methods, false);
1679 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001680
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001681 // Returns the number of non-inherited virtual methods.
1682 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001683 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001684 }
1685
1686 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001688 return GetVirtualMethods()->Get(i);
1689 }
1690
1691 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001692 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001693 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001694 }
1695
1696 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001697 ObjectArray<Method>* virtual_methods =
1698 GetFieldObject<ObjectArray<Method>*>(
1699 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1700 virtual_methods->Set(i, f);
1701 }
1702
1703 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001704 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001705 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001706 }
1707
1708 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001709 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001710 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001711 }
1712
1713 void SetVTable(ObjectArray<Method>* new_vtable) {
1714 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1715 }
1716
1717 static MemberOffset VTableOffset() {
1718 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001719 }
1720
Brian Carlstrom30b94452011-08-25 21:35:26 -07001721 // Given a method implemented by this class but potentially from a
1722 // super class, return the specific implementation
1723 // method for this class.
1724 Method* FindVirtualMethodForVirtual(Method* method) {
1725 DCHECK(!method->GetDeclaringClass()->IsInterface());
1726 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001727 // Use the index to a potentially overridden one for this instance's class.
1728 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001729 }
1730
1731 // Given a method implemented by this class, but potentially from a
1732 // super class or interface, return the specific implementation
1733 // method for this class.
Ian Rogersb04f69f2011-10-17 00:40:54 -07001734 Method* FindVirtualMethodForInterface(Method* method, bool can_throw);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001735
Ian Rogers466bb252011-10-14 03:29:56 -07001736 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
1737 Method* FindInterfaceMethod(String* name, String* descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001738
Brian Carlstrom30b94452011-08-25 21:35:26 -07001739 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001740 if (method->IsDirect()) {
1741 return method;
1742 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001743 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersb04f69f2011-10-17 00:40:54 -07001744 return FindVirtualMethodForInterface(method, true);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001745 }
1746 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001747 }
1748
Ian Rogers466bb252011-10-14 03:29:56 -07001749 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
1750 Method* FindDeclaredVirtualMethod(String* name, String* signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001751
Ian Rogers466bb252011-10-14 03:29:56 -07001752 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
1753 Method* FindVirtualMethod(String* name, String* descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001754
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001755 Method* FindDeclaredDirectMethod(const StringPiece& name,
1756 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001757
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 Method* FindDirectMethod(const StringPiece& name,
1759 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001760
Carl Shapiro69759ea2011-07-21 18:13:35 -07001761 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001762 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1764 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1765 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001766 }
1767
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001768 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001769 CHECK(IsIdxLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001770 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001771 }
1772
1773 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1774
1775 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001776 CHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001777 return GetFieldObject<ObjectArray<Class>*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001778 }
1779
1780 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001781 DCHECK(NULL == GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001782 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001783 }
1784
1785 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001786 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001787 ObjectArray<Class>* interfaces =
1788 GetFieldObject<ObjectArray<Class>*>(
1789 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1790 interfaces->Set(i, f);
1791 }
1792
1793 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001794 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001795 return GetInterfaces()->Get(i);
1796 }
1797
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001798 int32_t GetIfTableCount() const {
1799 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1800 if (iftable == NULL) {
1801 return 0;
1802 }
1803 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 }
1805
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001806 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001807 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001808 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001809 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1810 }
1811
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001812 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001813 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001814 }
1815
1816 // Get instance fields
1817 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001818 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001819 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001820 }
1821
1822 void SetIFields(ObjectArray<Field>* new_ifields) {
1823 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1824 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001825 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001826 }
1827
1828 size_t NumInstanceFields() const {
1829 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1830 }
1831
1832 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1833 DCHECK_NE(NumInstanceFields(), 0U);
1834 return GetIFields()->Get(i);
1835 }
1836
1837 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1838 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1839 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1840 ifields->Set(i, f);
1841 }
1842
1843 // Returns the number of instance fields containing reference types.
1844 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001845 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001846 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001847 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001848 }
1849
1850 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001851 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001852 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001853 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001854 }
1855
1856 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001857 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001858 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001859 }
1860
1861 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001862 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001863 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001864 }
1865
1866 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1867
1868 // Beginning of static field data
1869 static MemberOffset FieldsOffset() {
1870 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1871 }
1872
1873 // Returns the number of static fields containing reference types.
1874 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001875 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001876 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001877 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001878 }
1879
1880 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001881 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001882 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001883 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884 }
1885
1886 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001887 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001888 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 }
1890
1891 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001892 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001893 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001894 }
1895
1896 void SetSFields(ObjectArray<Field>* new_sfields) {
1897 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1898 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001899 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001900 }
1901
1902 size_t NumStaticFields() const {
1903 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1904 }
1905
1906 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1907 return GetSFields()->Get(i);
1908 }
1909
1910 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1911 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1912 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1913 sfields->Set(i, f);
1914 }
1915
1916 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001917 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001918 }
1919
1920 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1921
1922 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001923 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
1924 Field* FindInstanceField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001925
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001926 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
1927 Field* FindDeclaredInstanceField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001928
1929 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001930 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
1931 Field* FindStaticField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001932
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001933 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
1934 Field* FindDeclaredStaticField(String* name, String* type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001935
Elliott Hughesdcc24742011-09-07 14:02:44 -07001936 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001937 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001938 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1939 }
1940
Elliott Hughesdcc24742011-09-07 14:02:44 -07001941 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001942 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001943 }
1944
1945 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001946 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001947 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001948 }
1949
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001950 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001951 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001952 }
1953
jeffhao64155032011-11-03 17:56:34 -07001954 uint32_t GetAnnotationsOffset() {
1955 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, annotations_offset_), false);
1956 }
1957
1958 void SetAnnotationsOffset(uint32_t annotations_offset) {
1959 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, annotations_offset_), annotations_offset, false);
1960 }
1961
1962 uint32_t GetTypeIdx() {
1963 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, type_idx_), false);
1964 }
1965
1966 void SetTypeIdx(uint32_t type_idx) {
1967 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, type_idx_), type_idx, false);
1968 }
1969
Brian Carlstromc74255f2011-09-11 22:47:39 -07001970 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001971
Brian Carlstromc74255f2011-09-11 22:47:39 -07001972 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001973
jeffhaobdb76512011-09-07 11:43:16 -07001974 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001975 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001976 bool IsArrayAssignableFromArray(const Class* klass) const;
1977 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001978
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001979 // descriptor for the class such as "java.lang.Class" or "[C"
1980 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001981
Brian Carlstrom693267a2011-09-06 09:25:34 -07001982 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001983 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001984
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001985 // For array classes, the component class object for instanceof/checkcast
1986 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001987 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001988
Brian Carlstrom693267a2011-09-06 09:25:34 -07001989 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1990 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001991
Brian Carlstrom693267a2011-09-06 09:25:34 -07001992 // DexCache of resolved constant pool entries
1993 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1994 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001995
1996 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001997 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001998
Brian Carlstrom693267a2011-09-06 09:25:34 -07001999 // instance fields
2000 //
2001 // These describe the layout of the contents of an Object.
2002 // Note that only the fields directly declared by this class are
2003 // listed in ifields; fields declared by a superclass are listed in
2004 // the superclass's Class.ifields.
2005 //
2006 // All instance fields that refer to objects are guaranteed to be at
2007 // the beginning of the field list. num_reference_instance_fields_
2008 // specifies the number of reference fields.
2009 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002010
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002011 // Interface table (iftable_), one entry per interface supported by
2012 // this class. That means one entry for each interface we support
2013 // directly, indirectly via superclass, or indirectly via
2014 // superinterface. This will be null if neither we nor our
2015 // superclass implement any interfaces.
2016 //
2017 // Why we need this: given "class Foo implements Face", declare
2018 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
2019 // is part of the Face interface. We can't easily use a single
2020 // vtable.
2021 //
2022 // For every interface a concrete class implements, we create an array
2023 // of the concrete vtable_ methods for the methods in the interface.
2024 ObjectArray<InterfaceEntry>* iftable_;
2025
Brian Carlstromdbc05252011-09-09 01:59:59 -07002026 // array of interfaces this class implements directly
2027 // see also interfaces_type_idx_
2028 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002029
2030 // array of type_idx's for interfaces this class implements directly
2031 // see also interfaces_
2032 IntArray* interfaces_type_idx_;
2033
Brian Carlstromdbc05252011-09-09 01:59:59 -07002034 // Static fields
2035 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002036
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002037 // source file name, if known. Otherwise, NULL.
2038 String* source_file_;
2039
Brian Carlstromdbc05252011-09-09 01:59:59 -07002040 // The superclass, or NULL if this is java.lang.Object or a
2041 // primitive type.
2042 // see also super_class_type_idx_;
2043 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002044
Brian Carlstromdbc05252011-09-09 01:59:59 -07002045 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002046 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002047
Brian Carlstromdbc05252011-09-09 01:59:59 -07002048 // virtual methods defined in this class; invoked through vtable
2049 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002050
Ian Rogers9074b992011-10-26 17:41:55 -07002051 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
2052 // copied in, and virtual methods from our class either replace those from the super or are
2053 // appended. For abstract classes, methods may be created in the vtable that aren't in
2054 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002055 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002056
Brian Carlstromdbc05252011-09-09 01:59:59 -07002057 // access flags; low 16 bits are defined by VM spec
2058 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002059
jeffhao64155032011-11-03 17:56:34 -07002060 // annotation directory offset from dex file
2061 uint32_t annotations_offset_;
2062
Jesse Wilson6bf19152011-09-29 13:12:33 -04002063 // Total size of the Class instance; used when allocating storage on gc heap.
2064 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002065 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002066
Elliott Hughes5f791332011-09-15 17:45:30 -07002067 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002068 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002069
Brian Carlstromdbc05252011-09-09 01:59:59 -07002070 // number of instance fields that are object refs
2071 size_t num_reference_instance_fields_;
2072
2073 // number of static fields that are object refs
2074 size_t num_reference_static_fields_;
2075
2076 // Total object size; used when allocating storage on gc heap.
2077 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002078 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002079 size_t object_size_;
2080
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002081 // primitive type index, or Primitive::kPrimNot (0); set for generated prim classes
2082 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002083
2084 // Bitmap of offsets of ifields.
2085 uint32_t reference_instance_offsets_;
2086
2087 // Bitmap of offsets of sfields.
2088 uint32_t reference_static_offsets_;
2089
Brian Carlstrom693267a2011-09-06 09:25:34 -07002090 // state of class initialization
2091 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002092
Brian Carlstrom693267a2011-09-06 09:25:34 -07002093 // Set in LoadClass, used to LinkClass
2094 // see also super_class_
2095 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002096
jeffhao64155032011-11-03 17:56:34 -07002097 // type index from dex file
2098 uint32_t type_idx_;
2099
Brian Carlstrom693267a2011-09-06 09:25:34 -07002100 // TODO: ?
2101 // initiating class loader list
2102 // NOTE: for classes with low serialNumber, these are unused, and the
2103 // values are kept in a table in gDvm.
2104 // InitiatingLoaderList initiating_loader_list_;
2105
Brian Carlstrom4873d462011-08-21 15:23:39 -07002106 // Location of first static field.
2107 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002108
Brian Carlstrom693267a2011-09-06 09:25:34 -07002109 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002110 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002111};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002112
Elliott Hughes1f359b02011-07-17 14:27:17 -07002113std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002114
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002115inline void Object::SetClass(Class* new_klass) {
2116 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002117 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002118}
2119
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002120inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002121 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002122 DCHECK(GetClass() != NULL);
2123 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002124}
2125
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002126inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002127 Class* java_lang_Class = GetClass()->GetClass();
2128 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002129}
2130
Brian Carlstrom4873d462011-08-21 15:23:39 -07002131inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002132 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002133 return this == java_lang_Class;
2134}
2135
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002136inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002137 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002138}
2139
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002140template<class T>
2141inline ObjectArray<T>* Object::AsObjectArray() {
2142 DCHECK(IsObjectArray());
2143 return down_cast<ObjectArray<T>*>(this);
2144}
2145
2146template<class T>
2147inline const ObjectArray<T>* Object::AsObjectArray() const {
2148 DCHECK(IsObjectArray());
2149 return down_cast<const ObjectArray<T>*>(this);
2150}
2151
Brian Carlstromb63ec392011-08-27 17:38:27 -07002152inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002153 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002154}
2155
Brian Carlstroma663ea52011-08-19 23:33:41 -07002156inline bool Object::IsField() const {
2157 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002158 Class* java_lang_reflect_Field =
2159 java_lang_Class->GetInstanceField(0)->GetClass();
2160 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002161}
2162
2163inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002164 Class* c = GetClass();
2165 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002166}
2167
2168inline bool Object::IsReferenceInstance() const {
2169 return GetClass()->IsReferenceClass();
2170}
2171
2172inline bool Object::IsWeakReferenceInstance() const {
2173 return GetClass()->IsWeakReferenceClass();
2174}
2175
2176inline bool Object::IsSoftReferenceInstance() const {
2177 return GetClass()->IsSoftReferenceClass();
2178}
2179
2180inline bool Object::IsFinalizerReferenceInstance() const {
2181 return GetClass()->IsFinalizerReferenceClass();
2182}
2183
2184inline bool Object::IsPhantomReferenceInstance() const {
2185 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002186}
2187
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002188inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002189 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002190 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002191 result = AsArray()->SizeOf();
2192 } else if (IsClass()) {
2193 result = AsClass()->SizeOf();
2194 } else {
2195 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002196 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002197 DCHECK(!IsField() || result == sizeof(Field));
2198 DCHECK(!IsMethod() || result == sizeof(Method));
2199 return result;
2200}
2201
2202inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002203 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002204 Primitive::Type type = GetPrimitiveType();
2205 if (type == Primitive::kPrimDouble || type == Primitive::kPrimLong) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002206 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002207 }
Elliott Hughes06b37d92011-10-16 11:51:29 -07002208 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002209}
2210
2211inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002212 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002213 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002214 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002215 return result;
2216}
2217
2218inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002219 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002220}
2221
2222inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002223 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002224 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002225 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002226 return result;
2227}
2228
2229inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002230 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002231}
2232
2233inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002234 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes06b37d92011-10-16 11:51:29 -07002235 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002236}
2237
2238inline bool Method::IsReturnAReference() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002239 char d = GetReturnTypeDescriptor()[0];
2240 return d == 'L' || d == '[';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002241}
2242
2243inline bool Method::IsReturnAFloat() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002244 return GetReturnTypeDescriptor()[0] == 'F';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002245}
2246
2247inline bool Method::IsReturnADouble() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002248 return GetReturnTypeDescriptor()[0] == 'D';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002249}
2250
2251inline bool Method::IsReturnALong() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002252 return GetReturnTypeDescriptor()[0] == 'J';
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002253}
2254
2255inline bool Method::IsReturnVoid() const {
Ian Rogers9074b992011-10-26 17:41:55 -07002256 return GetReturnTypeDescriptor()[0] == 'V';
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002257}
2258
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002259inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002260 // This is safe from overflow because the array was already allocated, so we know it's sane.
2261 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002262}
2263
2264template<class T>
2265void ObjectArray<T>::Set(int32_t i, T* object) {
2266 if (IsValidIndex(i)) {
2267 if (object != NULL) {
2268 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002269 if (!object->InstanceOf(element_class)) {
2270 ThrowArrayStoreException(object);
2271 return;
2272 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002273 }
2274 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2275 SetFieldObject(data_offset, object, false);
2276 }
2277}
2278
2279template<class T>
2280void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2281 DCHECK(IsValidIndex(i));
2282 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2283 SetFieldObject(data_offset, object, false);
2284}
2285
2286template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002287T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2288 DCHECK(IsValidIndex(i));
2289 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2290 return GetFieldObject<T*>(data_offset, false);
2291}
2292
2293template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002294void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2295 ObjectArray<T>* dst, int dst_pos,
2296 size_t length) {
2297 if (src->IsValidIndex(src_pos) &&
2298 src->IsValidIndex(src_pos+length-1) &&
2299 dst->IsValidIndex(dst_pos) &&
2300 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogers5d76c432011-10-31 21:42:49 -07002301 MemberOffset src_offset(DataOffset().Int32Value() + src_pos * sizeof(Object*));
2302 MemberOffset dst_offset(DataOffset().Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002303 Class* array_class = dst->GetClass();
2304 if (array_class == src->GetClass()) {
2305 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002306 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002307 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Ian Rogers5d76c432011-10-31 21:42:49 -07002308 Heap::VerifyObject(object);
2309 // directly set field, we do a bulk write barrier at the end
2310 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002311 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2312 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2313 }
2314 } else {
2315 Class* element_class = array_class->GetComponentType();
2316 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002317 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002318 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002319 if (object != NULL && !object->InstanceOf(element_class)) {
2320 dst->ThrowArrayStoreException(object);
2321 return;
2322 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002323 Heap::VerifyObject(object);
2324 // directly set field, we do a bulk write barrier at the end
2325 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002326 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2327 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2328 }
2329 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002330 Heap::WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002331 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002332}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002333
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002334class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002335 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002336 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002337 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002338 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002339 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2340};
2341
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002342class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002343 private:
2344 CharArray* ASCII_;
2345 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002346 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002347 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002348 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002349 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2350};
2351
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002352class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002353 private:
2354 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2355 uint32_t TYPE_BOOLEAN_;
2356 uint32_t TYPE_BYTE_;
2357 uint32_t TYPE_CHAR_;
2358 uint32_t TYPE_DOUBLE_;
2359 uint32_t TYPE_FLOAT_;
2360 uint32_t TYPE_INTEGER_;
2361 uint32_t TYPE_LONG_;
2362 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002363 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002364 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2365};
2366
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002367class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002368 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002369 Object* ORDER_BY_SIGNATURE_;
2370 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002371 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2372};
2373
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002374template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002375class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002376 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002377 typedef T ElementType;
2378
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002379 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002380
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002381 const T* GetData() const {
2382 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002383 }
2384
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002385 T* GetData() {
2386 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002387 }
2388
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002389 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002390 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002391 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002392 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002393 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002394 }
2395
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002396 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002397 // TODO: ArrayStoreException
2398 if (IsValidIndex(i)) {
2399 GetData()[i] = value;
2400 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002401 }
2402
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002403 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002404 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002405 CHECK(array_class != NULL);
2406 array_class_ = array_class;
2407 }
2408
Brian Carlstroma663ea52011-08-19 23:33:41 -07002409 static void ResetArrayClass() {
2410 CHECK(array_class_ != NULL);
2411 array_class_ = NULL;
2412 }
2413
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002414 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002415 // Location of first element.
2416 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002417
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002418 static Class* array_class_;
2419
Carl Shapirof88c9522011-08-06 15:47:38 -07002420 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002421};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002423inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2424 DCHECK(NULL == GetFieldObject<IntArray*>(
2425 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2426 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2427 new_interfaces_idx, false);
2428}
2429
2430// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002431class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002432 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002433 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002434 const CharArray* result = GetFieldObject<const CharArray*>(
2435 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2436 DCHECK(result != NULL);
2437 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002438 }
2439
Elliott Hughes814e4032011-08-23 12:07:56 -07002440 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002441 int32_t result = GetField32(
2442 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2443 DCHECK_LE(0, result);
2444 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002445 }
2446
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002447 int32_t GetLength() const;
2448
Brian Carlstrom395520e2011-09-25 19:35:00 -07002449 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002450
2451 void ComputeHashCode() {
2452 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002453 }
2454
Elliott Hughes814e4032011-08-23 12:07:56 -07002455 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002456 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002457 }
2458
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002459 uint16_t CharAt(int32_t index) const;
2460
Brian Carlstromc74255f2011-09-11 22:47:39 -07002461 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002462
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002463 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002464 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002465 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002466
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002467 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002468
Jesse Wilson8989d992011-08-02 13:39:42 -07002469 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002470 const char* utf8_data_in);
2471
2472 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2473
2474 static String* Alloc(Class* java_lang_String, CharArray* array);
2475
2476 bool Equals(const char* modified_utf8) const;
2477
2478 // TODO: do we need this overload? give it a more intention-revealing name.
2479 bool Equals(const StringPiece& modified_utf8) const;
2480
2481 bool Equals(const String* that) const;
2482
2483 // TODO: do we need this overload? give it a more intention-revealing name.
2484 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2485 int32_t that_length) const;
2486
2487 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2488 std::string ToModifiedUtf8() const;
2489
2490 static Class* GetJavaLangString() {
2491 DCHECK(java_lang_String_ != NULL);
2492 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002493 }
2494
Brian Carlstroma663ea52011-08-19 23:33:41 -07002495 static void SetClass(Class* java_lang_String);
2496 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002497
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002498 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002499 void SetHashCode(int32_t new_hash_code) {
2500 DCHECK_EQ(0u,
2501 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2502 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2503 new_hash_code, false);
2504 }
2505
2506 void SetCount(int32_t new_count) {
2507 DCHECK_LE(0, new_count);
2508 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2509 }
2510
2511 void SetOffset(int32_t new_offset) {
2512 DCHECK_LE(0, new_offset);
2513 DCHECK_GE(GetLength(), new_offset);
2514 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2515 }
2516
2517 void SetArray(CharArray* new_array) {
2518 DCHECK(new_array != NULL);
2519 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2520 }
2521
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002522 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2523 CharArray* array_;
2524
Brian Carlstromdbc05252011-09-09 01:59:59 -07002525 int32_t count_;
2526
Carl Shapirof88c9522011-08-06 15:47:38 -07002527 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002528
Elliott Hughes289da822011-08-16 10:11:20 -07002529 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002530
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002531 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002532
Brian Carlstrom693267a2011-09-06 09:25:34 -07002533 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002534 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002535};
2536
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002537struct StringHashCode {
2538 int32_t operator()(art::String* string) const {
2539 return string->GetHashCode();
2540 }
2541};
2542
2543inline String* Field::GetName() const {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002544 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
2545 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2546 DCHECK(result != NULL);
2547 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002548}
2549
2550inline void Field::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002551 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002552}
2553
2554inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002555 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002556 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2557}
2558
2559inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002560 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002561 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2562}
2563
2564inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002565 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002566 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002567}
2568
2569inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002570 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002571 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002572}
2573
Ian Rogers466bb252011-10-14 03:29:56 -07002574inline String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002575 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002576 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002577 DCHECK(result != NULL);
2578 return result;
2579}
2580
2581inline void Method::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002582 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002583}
2584
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002585inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002586 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002587 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588}
2589
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002590inline void Method::SetShorty(String* new_shorty) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002591 DCHECK(NULL == GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002592 DCHECK_LE(1, new_shorty->GetLength());
2593 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2594}
2595
Ian Rogers466bb252011-10-14 03:29:56 -07002596inline String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002597 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002598 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002599 DCHECK(result != NULL);
2600 return result;
2601}
2602
2603inline void Method::SetSignature(String* new_signature) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002604 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_), new_signature, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002605}
2606
Ian Rogers466bb252011-10-14 03:29:56 -07002607inline void Method::SetExceptionTypes(ObjectArray<Class>* exception_types) {
2608 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), exception_types, false);
2609}
2610
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002611inline uint32_t Class::GetAccessFlags() const {
2612 // Check class is loaded or this is java.lang.String that has a
2613 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002614 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002615 this == String::GetJavaLangString() ||
2616 this == Field::GetJavaLangReflectField() ||
2617 this == Method::GetConstructorClass() ||
2618 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002619 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2620}
2621
2622inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002623 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002624 DCHECK(new_descriptor != NULL);
2625 DCHECK_NE(0, new_descriptor->GetLength());
2626 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2627 new_descriptor, false);
2628}
2629
Brian Carlstromc74255f2011-09-11 22:47:39 -07002630inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002631 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002632 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2633}
2634
2635inline void Class::SetSourceFile(String* new_source_file) {
2636 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2637}
2638
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002639inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002640 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002641 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2642}
2643
2644inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002645 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002646 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002647}
2648
2649inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002650 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002651 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002652}
2653
2654inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002655 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002656 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002657}
2658
2659inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002660 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002661 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002662}
2663
2664inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002665 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002666 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2667}
2668
2669// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002670class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002671 public:
2672 void SetDetailMessage(String* new_detail_message) {
2673 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2674 new_detail_message, false);
2675 }
Ian Rogers9074b992011-10-26 17:41:55 -07002676 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002677
Ian Rogers466bb252011-10-14 03:29:56 -07002678 bool IsCheckedException() const;
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002679 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002680 Object* GetStackState() const {
2681 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2682 }
2683
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002684 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2685 Throwable* cause_;
2686 String* detail_message_;
2687 Object* stack_state_; // Note this is Java volatile:
2688 Object* stack_trace_;
2689 Object* suppressed_exceptions_;
2690
Brian Carlstrom693267a2011-09-06 09:25:34 -07002691 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002692 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2693};
2694
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002695// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002696class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002697 public:
2698 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002699 return GetFieldObject<const String*>(
2700 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002701 }
2702
2703 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002704 return GetFieldObject<const String*>(
2705 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002706 }
2707
2708 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002709 return GetFieldObject<const String*>(
2710 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002711 }
2712
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002713 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002714 return GetField32(
2715 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002716 }
2717
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002718 static StackTraceElement* Alloc(const String* declaring_class,
2719 const String* method_name,
2720 const String* file_name,
2721 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002722
2723 static void SetClass(Class* java_lang_StackTraceElement);
2724
2725 static void ResetClass();
2726
2727 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002728 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002729 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002730 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002731 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002732 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002733
2734 static Class* GetStackTraceElement() {
2735 DCHECK(java_lang_StackTraceElement_ != NULL);
2736 return java_lang_StackTraceElement_;
2737 }
2738
2739 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002740
2741 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002742 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2743};
2744
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002745class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002746 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002747 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002748 Class* interface = Get(kInterface)->AsClass();
2749 DCHECK(interface != NULL);
2750 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002751 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002752
Brian Carlstrom30b94452011-08-25 21:35:26 -07002753 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002754 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002755 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002756 DCHECK(Get(kInterface) == NULL);
2757 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002758 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002759
Brian Carlstrom86927212011-09-15 11:31:11 -07002760 size_t GetMethodArrayCount() const {
2761 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2762 if (method_array == 0) {
2763 return 0;
2764 }
2765 return method_array->GetLength();
2766 }
2767
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002768 ObjectArray<Method>* GetMethodArray() const {
2769 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2770 DCHECK(method_array != NULL);
2771 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002772 }
2773
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002774 void SetMethodArray(ObjectArray<Method>* new_ma) {
2775 DCHECK(new_ma != NULL);
2776 DCHECK(Get(kMethodArray) == NULL);
2777 Set(kMethodArray, new_ma);
2778 }
2779
2780 static size_t LengthAsArray() {
2781 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002782 }
2783
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002784 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002785
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002786 enum ArrayIndex {
2787 // Points to the interface class.
2788 kInterface = 0,
2789 // Method pointers into the vtable, allow fast map from interface
2790 // method index to concrete instance method.
2791 kMethodArray = 1,
2792 kMax = 2,
2793 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002794
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002795 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002796};
2797
Jesse Wilson95caa792011-10-12 18:14:17 -04002798class MANAGED ProxyClass : public Class {
2799 private:
2800 int32_t NextClassNameIndex_;
2801 int64_t serialVersionUID_;
2802 friend struct ProxyClassOffsets; // for verifying offset information
2803 DISALLOW_IMPLICIT_CONSTRUCTORS(ProxyClass);
2804};
2805
2806class MANAGED Proxy : public Object {
2807 private:
2808 Object* h_;
2809
2810 friend struct ProxyOffsets; // for verifying offset information
2811 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2812};
2813
Carl Shapiro1fb86202011-06-27 17:43:13 -07002814} // namespace art
2815
2816#endif // ART_SRC_OBJECT_H_