blob: 19ca3258de89520f09c137cd239c534b7a75f964 [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)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800100static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700101static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700102
Elliott Hughes20cde902011-10-04 17:37:27 -0700103// Special runtime-only flags.
104// Note: if only kAccClassIsReference is set, we have a soft reference.
105static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
106static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
107static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
108static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
109static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700110
111static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
112 | kAccClassIsWeakReference
113 | kAccClassIsFinalizerReference
114 | kAccClassIsPhantomReference);
115
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700117 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700118 */
119/*
120 * A magic value for refOffsets. Ignore the bits and walk the super
121 * chain when this is the value.
122 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
123 * fields followed by 2 ref instance fields.]
124 */
125#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
127#define CLASS_OFFSET_ALIGNMENT 4
128#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
129/*
130 * Given an offset, return the bit number which would encode that offset.
131 * Local use only.
132 */
133#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700134 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700135 CLASS_OFFSET_ALIGNMENT)
136/*
137 * Is the given offset too large to be encoded?
138 */
139#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
140 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
141/*
142 * Return a single bit, encoding the offset.
143 * Undefined if the offset is too large, as defined above.
144 */
145#define CLASS_BIT_FROM_OFFSET(byteOffset) \
146 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
147/*
148 * Return an offset, given a bit number as returned from CLZ.
149 */
150#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700151 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700152
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700153#define OFFSET_OF_OBJECT_MEMBER(type, field) \
154 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700156// Classes shared with the managed side of the world need to be packed
157// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700158#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700159
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700160// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700161class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700162 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700163 static MemberOffset ClassOffset() {
164 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165 }
166
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700167 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700168 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700169 }
170
171 void SetClass(Class* new_klass);
172
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700173 bool InstanceOf(const Class* klass) const;
174
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700175 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700176
Elliott Hughes081be7f2011-09-18 16:50:26 -0700177 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700178
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700179 static MemberOffset MonitorOffset() {
180 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
181 }
182
Elliott Hughes5f791332011-09-15 17:45:30 -0700183 volatile int32_t* GetRawLockWordAddress() {
184 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
185 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
186 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 }
188
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700189 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190
Elliott Hughes5f791332011-09-15 17:45:30 -0700191 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192
Ian Rogersff1ed472011-09-20 13:46:24 -0700193 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
Elliott Hughes5f791332011-09-15 17:45:30 -0700195 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198
Elliott Hughes5f791332011-09-15 17:45:30 -0700199 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200
Elliott Hughes5f791332011-09-15 17:45:30 -0700201 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700202
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700203 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204
205 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700206 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700207 return down_cast<Class*>(this);
208 }
209
210 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700211 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 return down_cast<const Class*>(this);
213 }
214
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700215 bool IsObjectArray() const;
216
217 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700218 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700219
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700220 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700221 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Brian Carlstromb63ec392011-08-27 17:38:27 -0700223 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700224
225 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700226 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700227 return down_cast<Array*>(this);
228 }
229
230 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700231 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700232 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700233 }
234
Elliott Hughesdbb40792011-11-18 17:05:22 -0800235 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700236
237 bool IsMethod() const;
238
239 Method* AsMethod() {
240 DCHECK(IsMethod());
241 return down_cast<Method*>(this);
242 }
243
Brian Carlstrom4873d462011-08-21 15:23:39 -0700244 const Method* AsMethod() const {
245 DCHECK(IsMethod());
246 return down_cast<const Method*>(this);
247 }
248
Brian Carlstroma663ea52011-08-19 23:33:41 -0700249 bool IsField() const;
250
251 Field* AsField() {
252 DCHECK(IsField());
253 return down_cast<Field*>(this);
254 }
255
Brian Carlstrom4873d462011-08-21 15:23:39 -0700256 const Field* AsField() const {
257 DCHECK(IsField());
258 return down_cast<const Field*>(this);
259 }
260
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700261 bool IsReferenceInstance() const;
262
263 bool IsWeakReferenceInstance() const;
264
265 bool IsSoftReferenceInstance() const;
266
267 bool IsFinalizerReferenceInstance() const;
268
269 bool IsPhantomReferenceInstance() const;
270
271 // Accessors for Java type fields
272 template<class T>
273 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700274 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
275 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700276 Heap::VerifyObject(result);
277 return result;
278 }
279
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700280 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700282 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
283 if (new_value != NULL) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700284 Heap::WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 }
287
288 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
289 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700290 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
291 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700292 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700293 return android_atomic_acquire_load(word_addr);
294 } else {
295 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700297 }
298
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700299 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
300 if (this_is_valid) {
301 Heap::VerifyObject(this);
302 }
303 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700304 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700305 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700306 /*
307 * TODO: add an android_atomic_synchronization_store() function and
308 * use it in the 32-bit volatile set handlers. On some platforms we
309 * can use a fast atomic instruction and avoid the barriers.
310 */
311 ANDROID_MEMBAR_STORE();
312 *word_addr = new_value;
313 ANDROID_MEMBAR_FULL();
314 } else {
315 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317 }
318
319 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
320 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700321 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700322 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700323 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700324 uint64_t result = QuasiAtomicRead64(addr);
325 ANDROID_MEMBAR_FULL();
326 return result;
327 } else {
328 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700329 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700330 }
331
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700332 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700333 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700334 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
335 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700336 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700337 ANDROID_MEMBAR_STORE();
338 QuasiAtomicSwap64(new_value, addr);
339 // Post-store barrier not required due to use of atomic op or mutex.
340 } else {
341 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700342 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700343 }
344
345 protected:
346 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 template<class T>
348 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700349 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700350 }
351
352 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800353 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
354 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
356
357 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700358 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700359
Elliott Hughes5f791332011-09-15 17:45:30 -0700360 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700361
Elliott Hughes5f791332011-09-15 17:45:30 -0700362 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700363 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700364 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700365};
366
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700367struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800368 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700369#ifdef MOVING_GARBAGE_COLLECTOR
370 // TODO: we'll need to use the Object's internal concept of identity
371 UNIMPLEMENTED(FATAL);
372#endif
373 return reinterpret_cast<size_t>(obj);
374 }
375};
376
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700377// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500378class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700379 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700380 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 void SetDeclaringClass(Class *new_declaring_class);
383
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700384 uint32_t GetAccessFlags() const;
385
386 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700387 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700388 }
389
Elliott Hughes80609252011-09-23 17:24:51 -0700390 bool IsPublic() const {
391 return (GetAccessFlags() & kAccPublic) != 0;
392 }
393
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700394 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700396 }
397
jeffhaobdb76512011-09-07 11:43:16 -0700398 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700399 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700400 }
401
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800402 uint32_t GetDexFieldIndex() const {
403 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
404 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800406 void SetDexFieldIndex(uint32_t new_idx) {
407 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
408 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700409
410 // Offset to field within an Object
411 MemberOffset GetOffset() const;
412
buzbee34cd9e52011-09-08 14:31:52 -0700413 static MemberOffset OffsetOffset() {
414 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
415 }
416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417 MemberOffset GetOffsetDuringLinking() const;
418
419 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700420
Brian Carlstrom4873d462011-08-21 15:23:39 -0700421 // field access, null object for static fields
422 bool GetBoolean(const Object* object) const;
423 void SetBoolean(Object* object, bool z) const;
424 int8_t GetByte(const Object* object) const;
425 void SetByte(Object* object, int8_t b) const;
426 uint16_t GetChar(const Object* object) const;
427 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700428 int16_t GetShort(const Object* object) const;
429 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700430 int32_t GetInt(const Object* object) const;
431 void SetInt(Object* object, int32_t i) const;
432 int64_t GetLong(const Object* object) const;
433 void SetLong(Object* object, int64_t j) const;
434 float GetFloat(const Object* object) const;
435 void SetFloat(Object* object, float f) const;
436 double GetDouble(const Object* object) const;
437 void SetDouble(Object* object, double d) const;
438 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700439 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700440
Ian Rogersce9eca62011-10-07 17:11:03 -0700441 // raw field accesses
442 uint32_t Get32(const Object* object) const;
443 void Set32(Object* object, uint32_t new_value) const;
444 uint64_t Get64(const Object* object) const;
445 void Set64(Object* object, uint64_t new_value) const;
446 Object* GetObj(const Object* object) const;
447 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700448
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700449 static Class* GetJavaLangReflectField() {
450 DCHECK(java_lang_reflect_Field_ != NULL);
451 return java_lang_reflect_Field_;
452 }
453
454 static void SetClass(Class* java_lang_reflect_Field);
455 static void ResetClass();
456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 bool IsVolatile() const {
458 return (GetAccessFlags() & kAccVolatile) != 0;
459 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700460
buzbee1da522d2011-09-04 11:22:20 -0700461 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400462 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800463 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400464 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700465
Jesse Wilson35baaab2011-08-10 16:18:03 -0400466 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700467
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800468 // Dex cache index of field id
469 uint32_t field_dex_idx_;
470
Brian Carlstrom693267a2011-09-06 09:25:34 -0700471 // Offset of field within an instance or in the Class' static fields
472 uint32_t offset_;
473
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700474 static Class* java_lang_reflect_Field_;
475
Brian Carlstrom693267a2011-09-06 09:25:34 -0700476 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400477 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700478};
479
Jesse Wilson6bf19152011-09-29 13:12:33 -0400480// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500481class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700482 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700483 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700484 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700485 Object* obj,
486 Thread* thread,
487 byte* args,
488 JValue* result);
489
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700490 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700491
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700492 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700493
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700494 static MemberOffset DeclaringClassOffset() {
495 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700496 }
497
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700498 uint32_t GetAccessFlags() const;
499
500 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700501 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700502 }
503
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700504 // Returns true if the method is declared public.
505 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700506 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700507 }
508
509 // Returns true if the method is declared private.
510 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 }
513
514 // Returns true if the method is declared static.
515 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700516 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700517 }
518
Brian Carlstrom1f870082011-08-23 16:02:11 -0700519 // Returns true if the method is a constructor.
520 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700521 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700522 }
523
524 // Returns true if the method is static, private, or a constructor.
525 bool IsDirect() const {
526 return IsStatic() || IsPrivate() || IsConstructor();
527 }
528
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529 // Returns true if the method is declared synchronized.
530 bool IsSynchronized() const {
531 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700532 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533 }
534
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700537 }
538
Elliott Hughes80609252011-09-23 17:24:51 -0700539 bool IsMiranda() const {
540 return (GetAccessFlags() & kAccMiranda) != 0;
541 }
542
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700544 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 }
546
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700548 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700549 }
550
551 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700552 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700553 }
554
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700556
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700557 uint16_t GetMethodIndex() const;
558
559 size_t GetVtableIndex() const {
560 return GetMethodIndex();
561 }
562
563 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700564 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700565 }
566
567 static MemberOffset MethodIndexOffset() {
568 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
569 }
570
571 uint32_t GetCodeItemOffset() const {
572 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
573 }
574
575 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700576 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700577 }
578
579 // Number of 32bit registers that would be required to hold all the arguments
580 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800582 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700583
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800584 void SetDexMethodIndex(uint32_t new_idx) {
585 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700586 }
587
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700588 ObjectArray<String>* GetDexCacheStrings() const;
589 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
590
591 static MemberOffset DexCacheStringsOffset() {
592 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
593 }
594
buzbee2a475e72011-09-07 17:19:17 -0700595 static MemberOffset DexCacheResolvedTypesOffset() {
596 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
597 }
598
buzbee1da522d2011-09-04 11:22:20 -0700599 static MemberOffset DexCacheInitializedStaticStorageOffset() {
600 return OFFSET_OF_OBJECT_MEMBER(Method,
601 dex_cache_initialized_static_storage_);
602 }
603
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700604 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
605 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
606
607 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
608 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
609
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700610 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
611 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
612
613 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
614 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
615
Ian Rogers466bb252011-10-14 03:29:56 -0700616 // Find the method that this method overrides
617 Method* FindOverriddenMethod() const;
618
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700619 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
620
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 const void* GetCode() const {
622 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700623 }
624
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700625 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700626 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
627 }
628
629 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700630 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700631 return reinterpret_cast<uint32_t>(GetCode());
632 }
633
634 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700635 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700636 SetCode(reinterpret_cast<void*>(code_offset));
637 }
638
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700639 static MemberOffset GetCodeOffset() {
640 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700641 }
642
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700643 const uint32_t* GetMappingTable() const {
644 const uint32_t* map = GetMappingTableRaw();
645 if (map == NULL) {
646 return map;
647 }
648 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700649 }
650
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700651 uint32_t GetMappingTableLength() const {
652 const uint32_t* map = GetMappingTableRaw();
653 if (map == NULL) {
654 return 0;
655 }
656 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700657 }
658
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700659 const uint32_t* GetMappingTableRaw() const {
660 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700661 }
662
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700663 void SetMappingTable(const uint32_t* mapping_table) {
664 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
665 mapping_table, false);
666 }
667
668 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700669 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700670 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
671 }
672
673 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700674 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700675 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
676 }
677
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800678 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700679 const uint16_t* GetVmapTableRaw() const {
680 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
681 }
682
683 void SetVmapTable(const uint16_t* vmap_table) {
684 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
685 }
686
687 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700688 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700689 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
690 }
691
692 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700693 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700694 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700695 }
696
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800697 const uint8_t* GetGcMap() const {
698 const uint8_t* gc_map_raw = GetGcMapRaw();
699 if (gc_map_raw == NULL) {
700 return gc_map_raw;
701 }
702 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 }
704
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800705 uint32_t GetGcMapLength() const {
706 const uint8_t* gc_map_raw = GetGcMapRaw();
707 if (gc_map_raw == NULL) {
708 return 0;
709 }
710 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
711 (gc_map_raw[1] << 16) |
712 (gc_map_raw[2] << 8) |
713 (gc_map_raw[3] << 0));
714 }
715
716 const uint8_t* GetGcMapRaw() const {
717 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
718 }
719 void SetGcMap(const uint8_t* data) {
720 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
721 }
722
723 uint32_t GetOatGcMapOffset() const {
724 DCHECK(!Runtime::Current()->IsStarted());
725 return reinterpret_cast<uint32_t>(GetGcMapRaw());
726 }
727 void SetOatGcMapOffset(uint32_t gc_map_offset) {
728 DCHECK(!Runtime::Current()->IsStarted());
729 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 }
731
Shih-wei Liaod11af152011-08-23 16:02:11 -0700732 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700733 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
734 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700735 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
736 return result;
737 }
738
739 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700740 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700741 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
742 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
743 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700744 }
745
Shih-wei Liaod11af152011-08-23 16:02:11 -0700746 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700748 }
749
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700750 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700751
Brian Carlstrom16192862011-09-12 17:50:06 -0700752 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700753
Brian Carlstrom16192862011-09-12 17:50:06 -0700754 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700755
Ian Rogersb033c752011-07-20 12:22:35 -0700756 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700757 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700758 }
759
Brian Carlstrom78128a62011-09-15 17:21:19 -0700760 const void* GetNativeMethod() const {
761 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
762 }
763
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700764 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700765 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700766 InvokeStub* result = GetFieldPtr<InvokeStub*>(
767 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
768 // TODO: DCHECK(result != NULL); should be ahead of time compiled
769 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700770 }
771
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700772 void SetInvokeStub(InvokeStub* invoke_stub) {
773 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
774 invoke_stub, false);
775 }
776
777 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700778 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700779 return reinterpret_cast<uint32_t>(GetInvokeStub());
780 }
781
782 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700783 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700784 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
785 }
786
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700787 static MemberOffset GetInvokeStubOffset() {
788 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700789 }
790
buzbee561227c2011-09-02 15:28:19 -0700791 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
792 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
793 }
794
795 static MemberOffset GetDexCacheResolvedMethodsOffset() {
796 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
797 }
798
799 static MemberOffset GetMethodIndexOffset() {
800 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
801 }
802
Ian Rogers90865722011-09-19 11:11:44 -0700803 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700804 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700805 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700806
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700807 void SetCoreSpillMask(uint32_t core_spill_mask) {
808 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700809 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700810 }
811
Ian Rogers90865722011-09-19 11:11:44 -0700812 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700813 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
814 }
815
816 void SetFpSpillMask(uint32_t fp_spill_mask) {
817 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700818 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700819 }
820
Ian Rogers90865722011-09-19 11:11:44 -0700821 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700822 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700823 Runtime* runtime = Runtime::Current();
824 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700825 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700826 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
827 result = true;
828 break;
829 }
830 }
Ian Rogers90865722011-09-19 11:11:44 -0700831 // Check that if we do think it is phony it looks like the callee save method
832 DCHECK(!result || GetCoreSpillMask() != 0);
833 return result;
834 }
835
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700836 // Converts a native PC to a dex PC. TODO: this is a no-op
837 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700838 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700839
840 // Converts a dex PC to a native PC. TODO: this is a no-op
841 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700842 uintptr_t ToNativePC(const uint32_t dex_pc) const;
843
844 // Find the catch block for the given exception type and dex_pc
845 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700846
Elliott Hughes80609252011-09-23 17:24:51 -0700847 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
848
849 static Class* GetConstructorClass() {
850 return java_lang_reflect_Constructor_;
851 }
852
853 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700854 return java_lang_reflect_Method_;
855 }
856
Elliott Hughes80609252011-09-23 17:24:51 -0700857 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700858
859 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800861 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700862 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700863
Brian Carlstrom693267a2011-09-06 09:25:34 -0700864 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700865 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
866
867 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
868 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
869
870 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -0700871 ObjectArray<Method>* dex_cache_resolved_methods_;
872
Brian Carlstromdbc05252011-09-09 01:59:59 -0700873 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
874 ObjectArray<Class>* dex_cache_resolved_types_;
875
876 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
877 ObjectArray<String>* dex_cache_strings_;
878
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700879 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700880 uint32_t access_flags_;
881
882 // Compiled code associated with this method for callers from managed code.
883 // May be compiled managed code or a bridge for invoking a native method.
884 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700885
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700886 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700887 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700888
Brian Carlstrom693267a2011-09-06 09:25:34 -0700889 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700890 uint32_t core_spill_mask_;
891
892 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700893 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700894
Brian Carlstrom693267a2011-09-06 09:25:34 -0700895 // Total size in bytes of the frame
896 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800898 // Garbage collection map
899 const uint8_t* gc_map_;
900
Brian Carlstrom693267a2011-09-06 09:25:34 -0700901 // Native invocation stub entry point for calling from native to managed code.
902 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700903
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700905 const uint32_t* mapping_table_;
906
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800907 // Index into method_ids of the dex file associated with this method
908 uint32_t method_dex_index_;
909
Ian Rogers466bb252011-10-14 03:29:56 -0700910 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700911 //
Ian Rogers466bb252011-10-14 03:29:56 -0700912 // For abstract methods in an interface class, this is the offset of the method in
913 // "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700914 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700915
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700916 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700917 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700918
Ian Rogersd81871c2011-10-03 13:57:23 -0700919 // When a register is promoted into a register, the spill mask holds which registers hold dex
920 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
921 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700922 const uint16_t* vmap_table_;
923
Elliott Hughes80609252011-09-23 17:24:51 -0700924 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700925 static Class* java_lang_reflect_Method_;
926
Brian Carlstrom693267a2011-09-06 09:25:34 -0700927 friend class ImageWriter; // for relocating code_ and invoke_stub_
928 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700929 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930};
931
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700932class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700933 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700934 // A convenience for code that doesn't know the component size,
935 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700936 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700937
Brian Carlstromb63ec392011-08-27 17:38:27 -0700938 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700939
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700940 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700941
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700942 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700943 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700944 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700945
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700946 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700947 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700948 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700949 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700950
buzbeec143c552011-08-20 17:38:58 -0700951 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700952 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -0700953 }
954
955 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700956 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -0700957 }
958
Elliott Hughesbf86d042011-08-31 17:53:14 -0700959 void* GetRawData() {
960 return reinterpret_cast<void*>(first_element_);
961 }
962
Elliott Hughes289da822011-08-16 10:11:20 -0700963 protected:
964 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -0700965 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -0700966 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -0700967 }
968 return true;
969 }
970
Elliott Hughes80609252011-09-23 17:24:51 -0700971 protected:
972 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
973 bool ThrowArrayStoreException(Object* object) const;
974
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700975 private:
976 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700977 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400978 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
979 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700980 // Marker for the data (used by generated code)
981 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700982
Carl Shapirof88c9522011-08-06 15:47:38 -0700983 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700984};
985
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700986template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700987class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700988 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700989 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700990
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700991 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400992
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700993 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400994
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700995 // Set element without bound and element type checks, to be used in limited
996 // circumstances, such as during boot image writing
997 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -0700998
Ian Rogers5d76c432011-10-31 21:42:49 -0700999 T* GetWithoutChecks(int32_t i) const;
1000
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001001 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001002 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001003 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001004
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001005 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001006
1007 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001008 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001009};
1010
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001011template<class T>
1012ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1013 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1014}
1015
1016template<class T>
1017T* ObjectArray<T>::Get(int32_t i) const {
1018 if (!IsValidIndex(i)) {
1019 return NULL;
1020 }
1021 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1022 return GetFieldObject<T*>(data_offset, false);
1023}
1024
1025template<class T>
1026ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1027 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1028 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1029 return new_array;
1030}
1031
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001032// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001033// provides the static storage. However, this might change to an Array
1034// to improve image sharing, so we use this type to avoid assumptions
1035// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001036class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001037
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001038// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001039class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001040 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001041
1042 // Class Status
1043 //
1044 // kStatusNotReady: If a Class cannot be found in the class table by
1045 // FindClass, it allocates an new one with AllocClass in the
1046 // kStatusNotReady and calls LoadClass. Note if it does find a
1047 // class, it may not be kStatusResolved and it will try to push it
1048 // forward toward kStatusResolved.
1049 //
1050 // kStatusIdx: LoadClass populates with Class with information from
1051 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001052 // Class value in super_class_ has not been populated. The new Class
1053 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001054 //
1055 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1056 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001057 // using ResolveClass to initialize the super_class_ and ensuring the
1058 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001059 //
1060 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001061 // shows linking is complete and fields of the Class populated by making
1062 // it kStatusResolved. Java allows circularities of the form where a super
1063 // class has a field that is of the type of the sub class. We need to be able
1064 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001065
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001066 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001067 kStatusError = -1,
1068 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001069 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001070 kStatusLoaded = 2, // DEX idx values resolved
1071 kStatusResolved = 3, // part of linking
1072 kStatusVerifying = 4, // in the process of being verified
1073 kStatusVerified = 5, // logically part of linking; done pre-init
1074 kStatusInitializing = 6, // class init in progress
1075 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001076 };
1077
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001078 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001079 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1080 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001081 }
1082
1083 void SetStatus(Status new_status);
1084
1085 // Returns true if the class has failed to link.
1086 bool IsErroneous() const {
1087 return GetStatus() == kStatusError;
1088 }
1089
1090 // Returns true if the class has been loaded.
1091 bool IsIdxLoaded() const {
1092 return GetStatus() >= kStatusIdx;
1093 }
1094
1095 // Returns true if the class has been loaded.
1096 bool IsLoaded() const {
1097 return GetStatus() >= kStatusLoaded;
1098 }
1099
1100 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001101 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001102 return GetStatus() >= kStatusResolved;
1103 }
1104
1105 // Returns true if the class has been verified.
1106 bool IsVerified() const {
1107 return GetStatus() >= kStatusVerified;
1108 }
1109
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001110 // Returns true if the class is initializing.
1111 bool IsInitializing() const {
1112 return GetStatus() >= kStatusInitializing;
1113 }
1114
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115 // Returns true if the class is initialized.
1116 bool IsInitialized() const {
1117 return GetStatus() == kStatusInitialized;
1118 }
1119
1120 uint32_t GetAccessFlags() const;
1121
1122 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001123 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001124 }
1125
1126 // Returns true if the class is an interface.
1127 bool IsInterface() const {
1128 return (GetAccessFlags() & kAccInterface) != 0;
1129 }
1130
1131 // Returns true if the class is declared public.
1132 bool IsPublic() const {
1133 return (GetAccessFlags() & kAccPublic) != 0;
1134 }
1135
1136 // Returns true if the class is declared final.
1137 bool IsFinal() const {
1138 return (GetAccessFlags() & kAccFinal) != 0;
1139 }
1140
Elliott Hughes20cde902011-10-04 17:37:27 -07001141 bool IsFinalizable() const {
1142 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1143 }
1144
1145 void SetFinalizable() {
1146 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1147 SetAccessFlags(flags | kAccClassIsFinalizable);
1148 }
1149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001150 // Returns true if the class is abstract.
1151 bool IsAbstract() const {
1152 return (GetAccessFlags() & kAccAbstract) != 0;
1153 }
1154
1155 // Returns true if the class is an annotation.
1156 bool IsAnnotation() const {
1157 return (GetAccessFlags() & kAccAnnotation) != 0;
1158 }
1159
1160 // Returns true if the class is synthetic.
1161 bool IsSynthetic() const {
1162 return (GetAccessFlags() & kAccSynthetic) != 0;
1163 }
1164
1165 bool IsReferenceClass() const {
1166 return (GetAccessFlags() & kAccClassIsReference) != 0;
1167 }
1168
1169 bool IsWeakReferenceClass() const {
1170 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1171 }
1172
1173 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001174 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001175 }
1176
1177 bool IsFinalizerReferenceClass() const {
1178 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1179 }
1180
1181 bool IsPhantomReferenceClass() const {
1182 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1183 }
1184
Ian Rogersd418eda2012-01-30 12:14:28 -08001185
1186 String* GetName() const ; // Returns the cached name
1187 void SetName(String* name); // Sets the cached name
1188 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001189
1190 bool IsProxyClass() const {
1191 // Read access flags without using getter as whether something is a proxy can be check in
1192 // any loaded state
1193 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1194 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1195 return (access_flags & kAccClassIsProxy) != 0;
1196 }
1197
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001198 Primitive::Type GetPrimitiveType() const {
1199 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1200 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001201 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1202 }
1203
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001204 void SetPrimitiveType(Primitive::Type new_type) {
1205 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001206 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001207 }
1208
1209 // Returns true if the class is a primitive type.
1210 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001211 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001212 }
1213
1214 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001215 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001216 }
1217
1218 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001219 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001220 }
1221
1222 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001223 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001224 }
1225
1226 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001227 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001228 }
1229
1230 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001231 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001232 }
1233
1234 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001235 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001236 }
1237
1238 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001239 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001240 }
1241
1242 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001243 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001244 }
1245
1246 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001247 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001248 }
1249
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 // Depth of class from java.lang.Object
1251 size_t Depth() {
1252 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001253 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 depth++;
1255 }
1256 return depth;
1257 }
1258
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001259 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001260 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001261 }
1262
Elliott Hughesdbb40792011-11-18 17:05:22 -08001263 bool IsClassClass() const;
1264
1265 bool IsStringClass() const;
1266
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001267 bool IsThrowableClass() const;
1268
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001269 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001270 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001271 }
1272
1273 void SetComponentType(Class* new_component_type) {
1274 DCHECK(GetComponentType() == NULL);
1275 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001276 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001277 }
1278
1279 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001280 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001281 }
1282
1283 bool IsObjectClass() const {
1284 return !IsPrimitive() && GetSuperClass() == NULL;
1285 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001286 bool IsInstantiable() const {
1287 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1288 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001289
Brian Carlstrom1f870082011-08-23 16:02:11 -07001290 // Creates a raw object instance but does not invoke the default constructor.
1291 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001292
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001293 bool IsVariableSize() const {
1294 // Classes and arrays vary in size, and so the object_size_ field cannot
1295 // be used to get their instance size
1296 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001297 }
1298
Brian Carlstrom4873d462011-08-21 15:23:39 -07001299 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001300 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001301 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001302 }
1303
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001304 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001305 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001306 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001307 }
1308
Ian Rogers0571d352011-11-03 19:51:38 -07001309 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001310
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001311 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001312 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001313 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001314 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001315 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001316 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001317 }
1318
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001319 void SetObjectSize(size_t new_object_size) {
1320 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001321 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001322 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001323 }
1324
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001325 // Returns true if this class is in the same packages as that class.
1326 bool IsInSamePackage(const Class* that) const;
1327
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001328 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001329
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001330 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001331 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001332 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001333 }
1334
Ian Rogersf2391652011-12-14 12:50:52 -08001335 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001336 // Note that access to the class isn't checked in case the declaring class is protected and the
1337 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001338 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001339 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001340 if (this == access_to) {
1341 return true;
1342 }
Ian Rogersf2391652011-12-14 12:50:52 -08001343 // Public members are trivially accessible
1344 if (member_flags & kAccPublic) {
1345 return true;
1346 }
1347 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001348 if (member_flags & kAccPrivate) {
1349 return false;
1350 }
Ian Rogersf2391652011-12-14 12:50:52 -08001351 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001352 if (member_flags & kAccProtected) {
1353 if (this->IsSubClass(access_to)) {
1354 return true;
1355 }
1356 }
Ian Rogersf2391652011-12-14 12:50:52 -08001357 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001358 return this->IsInSamePackage(access_to);
1359 }
1360
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001361 bool IsSubClass(const Class* klass) const;
1362
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1364 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1365 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1366 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1367 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001368 bool IsAssignableFrom(const Class* src) const {
1369 DCHECK(src != NULL);
1370 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001371 // Can always assign to things of the same type
1372 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001373 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001375 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001377 return src->Implements(this);
1378 } else if (src->IsArrayClass()) {
1379 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001380 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 }
1383 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001384
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001385 Class* GetSuperClass() const {
1386 // Can only get super class for loaded classes (hack for when runtime is
1387 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001388 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001389 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001390 }
1391
1392 void SetSuperClass(Class *new_super_class) {
1393 // super class is assigned once, except during class linker initialization
1394 Class* old_super_class = GetFieldObject<Class*>(
1395 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1396 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1397 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001398 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399 }
1400
1401 bool HasSuperClass() const {
1402 return GetSuperClass() != NULL;
1403 }
1404
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001405 static MemberOffset SuperClassOffset() {
1406 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001407 }
1408
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001409 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001410
1411 void SetClassLoader(const ClassLoader* new_cl);
1412
1413 static MemberOffset DexCacheOffset() {
1414 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1415 }
1416
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001417 enum {
1418 kDumpClassFullDetail = 1,
1419 kDumpClassClassLoader = (1 << 1),
1420 kDumpClassInitialized = (1 << 2),
1421 };
1422
Elliott Hughes4681c802011-09-25 18:04:37 -07001423 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001424
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 DexCache* GetDexCache() const;
1426
1427 void SetDexCache(DexCache* new_dex_cache);
1428
1429 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001430 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001431 return GetFieldObject<ObjectArray<Method>*>(
1432 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1433 }
1434
1435 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1436 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1437 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1438 DCHECK_NE(0, new_direct_methods->GetLength());
1439 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1440 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001441 }
1442
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001443 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001444 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001445 }
1446
1447 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001448 ObjectArray<Method>* direct_methods =
1449 GetFieldObject<ObjectArray<Method>*>(
1450 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1451 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001452 }
1453
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001454 // Returns the number of static, private, and constructor methods.
1455 size_t NumDirectMethods() const {
1456 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1457 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001458
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001459 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001460 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001461 return GetFieldObject<ObjectArray<Method>*>(
1462 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1463 }
1464
1465 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1466 // TODO: we reassign virtual methods to grow the table for miranda
1467 // methods.. they should really just be assigned once
1468 DCHECK_NE(0, new_virtual_methods->GetLength());
1469 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1470 new_virtual_methods, false);
1471 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001472
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001473 // Returns the number of non-inherited virtual methods.
1474 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001475 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001476 }
1477
1478 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001479 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 return GetVirtualMethods()->Get(i);
1481 }
1482
1483 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001484 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001485 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001486 }
1487
1488 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001489 ObjectArray<Method>* virtual_methods =
1490 GetFieldObject<ObjectArray<Method>*>(
1491 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1492 virtual_methods->Set(i, f);
1493 }
1494
1495 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001496 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001497 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001498 }
1499
1500 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001501 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001502 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001503 }
1504
1505 void SetVTable(ObjectArray<Method>* new_vtable) {
1506 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1507 }
1508
1509 static MemberOffset VTableOffset() {
1510 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001511 }
1512
Brian Carlstrom30b94452011-08-25 21:35:26 -07001513 // Given a method implemented by this class but potentially from a
1514 // super class, return the specific implementation
1515 // method for this class.
1516 Method* FindVirtualMethodForVirtual(Method* method) {
1517 DCHECK(!method->GetDeclaringClass()->IsInterface());
1518 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001519 // Use the index to a potentially overridden one for this instance's class.
1520 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001521 }
1522
1523 // Given a method implemented by this class, but potentially from a
1524 // super class or interface, return the specific implementation
1525 // method for this class.
Ian Rogersb04f69f2011-10-17 00:40:54 -07001526 Method* FindVirtualMethodForInterface(Method* method, bool can_throw);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001527
Ian Rogers466bb252011-10-14 03:29:56 -07001528 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001529
Brian Carlstrom30b94452011-08-25 21:35:26 -07001530 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001531 if (method->IsDirect()) {
1532 return method;
1533 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001534 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersb04f69f2011-10-17 00:40:54 -07001535 return FindVirtualMethodForInterface(method, true);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001536 }
1537 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001538 }
1539
Ian Rogers466bb252011-10-14 03:29:56 -07001540 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001541
Ian Rogers466bb252011-10-14 03:29:56 -07001542 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001543
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001544 Method* FindDeclaredDirectMethod(const StringPiece& name,
1545 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001546
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001547 Method* FindDirectMethod(const StringPiece& name,
1548 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001549
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001550 int32_t GetIfTableCount() const {
1551 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1552 if (iftable == NULL) {
1553 return 0;
1554 }
1555 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001556 }
1557
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001558 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001559 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001560 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001561 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1562 }
1563
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001564 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001565 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001566 }
1567
1568 // Get instance fields
1569 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001570 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001571 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001572 }
1573
1574 void SetIFields(ObjectArray<Field>* new_ifields) {
1575 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1576 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001577 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 }
1579
1580 size_t NumInstanceFields() const {
1581 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1582 }
1583
1584 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1585 DCHECK_NE(NumInstanceFields(), 0U);
1586 return GetIFields()->Get(i);
1587 }
1588
1589 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1590 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1591 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1592 ifields->Set(i, f);
1593 }
1594
1595 // Returns the number of instance fields containing reference types.
1596 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001597 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001598 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001599 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001600 }
1601
1602 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001603 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001604 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001605 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001606 }
1607
1608 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001609 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001610 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001611 }
1612
1613 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001614 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001615 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001616 }
1617
1618 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1619
1620 // Beginning of static field data
1621 static MemberOffset FieldsOffset() {
1622 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1623 }
1624
1625 // Returns the number of static fields containing reference types.
1626 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001627 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001628 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001629 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001630 }
1631
1632 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001633 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001634 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001635 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001636 }
1637
1638 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001639 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001640 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001641 }
1642
1643 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001644 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001645 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001646 }
1647
1648 void SetSFields(ObjectArray<Field>* new_sfields) {
1649 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1650 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001651 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001652 }
1653
1654 size_t NumStaticFields() const {
1655 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1656 }
1657
1658 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1659 return GetSFields()->Get(i);
1660 }
1661
1662 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1663 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1664 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1665 sfields->Set(i, f);
1666 }
1667
1668 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001669 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001670 }
1671
1672 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1673
Ian Rogersb067ac22011-12-13 18:05:09 -08001674 // Find a static or instance field using the JLS resolution order
1675 Field* FindField(const StringPiece& name, const StringPiece& type);
1676
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001677 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001678 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001680 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001681
1682 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001683 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001684
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001685 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001686
Elliott Hughesdcc24742011-09-07 14:02:44 -07001687 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001688 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001689 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1690 }
1691
Elliott Hughesdcc24742011-09-07 14:02:44 -07001692 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001693 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001694 }
1695
1696 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001697 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001698 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001699 }
1700
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001701 uint16_t GetDexTypeIndex() const {
1702 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001703 }
1704
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001705 void SetDexTypeIndex(uint16_t type_idx) {
1706 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001707 }
1708
jeffhaobdb76512011-09-07 11:43:16 -07001709 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001710 void SetVerifyErrorClass(Class* klass) {
1711 CHECK(klass != NULL) << PrettyClass(this);
1712 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1713 }
1714
jeffhao5dbddee2011-09-07 16:38:26 -07001715 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001716 bool IsArrayAssignableFromArray(const Class* klass) const;
1717 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001718
Brian Carlstrom693267a2011-09-06 09:25:34 -07001719 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001720 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001721
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001722 // For array classes, the component class object for instanceof/checkcast
1723 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001724 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001725
Brian Carlstrom693267a2011-09-06 09:25:34 -07001726 // DexCache of resolved constant pool entries
1727 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1728 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001729
1730 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001731 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001732
Brian Carlstrom693267a2011-09-06 09:25:34 -07001733 // instance fields
1734 //
1735 // These describe the layout of the contents of an Object.
1736 // Note that only the fields directly declared by this class are
1737 // listed in ifields; fields declared by a superclass are listed in
1738 // the superclass's Class.ifields.
1739 //
1740 // All instance fields that refer to objects are guaranteed to be at
1741 // the beginning of the field list. num_reference_instance_fields_
1742 // specifies the number of reference fields.
1743 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001744
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001745 // Interface table (iftable_), one entry per interface supported by
1746 // this class. That means one entry for each interface we support
1747 // directly, indirectly via superclass, or indirectly via
1748 // superinterface. This will be null if neither we nor our
1749 // superclass implement any interfaces.
1750 //
1751 // Why we need this: given "class Foo implements Face", declare
1752 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1753 // is part of the Face interface. We can't easily use a single
1754 // vtable.
1755 //
1756 // For every interface a concrete class implements, we create an array
1757 // of the concrete vtable_ methods for the methods in the interface.
1758 ObjectArray<InterfaceEntry>* iftable_;
1759
Ian Rogersd418eda2012-01-30 12:14:28 -08001760 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1761 String* name_;
1762
Brian Carlstromdbc05252011-09-09 01:59:59 -07001763 // Static fields
1764 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001765
Ian Rogersd418eda2012-01-30 12:14:28 -08001766 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001767 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001768
Brian Carlstromdbc05252011-09-09 01:59:59 -07001769 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001770 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001771
Brian Carlstromdbc05252011-09-09 01:59:59 -07001772 // virtual methods defined in this class; invoked through vtable
1773 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001774
Ian Rogers9074b992011-10-26 17:41:55 -07001775 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1776 // copied in, and virtual methods from our class either replace those from the super or are
1777 // appended. For abstract classes, methods may be created in the vtable that aren't in
1778 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001779 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001780
Brian Carlstromdbc05252011-09-09 01:59:59 -07001781 // access flags; low 16 bits are defined by VM spec
1782 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001783
Jesse Wilson6bf19152011-09-29 13:12:33 -04001784 // Total size of the Class instance; used when allocating storage on gc heap.
1785 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001786 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001787
Elliott Hughes5f791332011-09-15 17:45:30 -07001788 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001789 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001790
Ian Rogersd418eda2012-01-30 12:14:28 -08001791 // type index from dex file
1792 // TODO: really 16bits
1793 uint32_t dex_type_idx_;
1794
Brian Carlstromdbc05252011-09-09 01:59:59 -07001795 // number of instance fields that are object refs
1796 size_t num_reference_instance_fields_;
1797
1798 // number of static fields that are object refs
1799 size_t num_reference_static_fields_;
1800
1801 // Total object size; used when allocating storage on gc heap.
1802 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001803 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001804 size_t object_size_;
1805
Ian Rogersd418eda2012-01-30 12:14:28 -08001806 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001807 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001808
1809 // Bitmap of offsets of ifields.
1810 uint32_t reference_instance_offsets_;
1811
1812 // Bitmap of offsets of sfields.
1813 uint32_t reference_static_offsets_;
1814
Brian Carlstrom693267a2011-09-06 09:25:34 -07001815 // state of class initialization
1816 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001817
Brian Carlstrom693267a2011-09-06 09:25:34 -07001818 // TODO: ?
1819 // initiating class loader list
1820 // NOTE: for classes with low serialNumber, these are unused, and the
1821 // values are kept in a table in gDvm.
1822 // InitiatingLoaderList initiating_loader_list_;
1823
Brian Carlstrom4873d462011-08-21 15:23:39 -07001824 // Location of first static field.
1825 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001826
Brian Carlstrom693267a2011-09-06 09:25:34 -07001827 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001828 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001829};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001830
Elliott Hughes1f359b02011-07-17 14:27:17 -07001831std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001832
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001833inline void Object::SetClass(Class* new_klass) {
1834 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001835 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001836}
1837
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001838inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001839 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001840 DCHECK(GetClass() != NULL);
1841 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001842}
1843
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001844inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001845 Class* java_lang_Class = GetClass()->GetClass();
1846 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001847}
1848
1849inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001850 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001851}
1852
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001853template<class T>
1854inline ObjectArray<T>* Object::AsObjectArray() {
1855 DCHECK(IsObjectArray());
1856 return down_cast<ObjectArray<T>*>(this);
1857}
1858
1859template<class T>
1860inline const ObjectArray<T>* Object::AsObjectArray() const {
1861 DCHECK(IsObjectArray());
1862 return down_cast<const ObjectArray<T>*>(this);
1863}
1864
Brian Carlstromb63ec392011-08-27 17:38:27 -07001865inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001866 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001867}
1868
Brian Carlstroma663ea52011-08-19 23:33:41 -07001869inline bool Object::IsField() const {
1870 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001871 Class* java_lang_reflect_Field =
1872 java_lang_Class->GetInstanceField(0)->GetClass();
1873 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07001874}
1875
1876inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07001877 Class* c = GetClass();
1878 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001879}
1880
1881inline bool Object::IsReferenceInstance() const {
1882 return GetClass()->IsReferenceClass();
1883}
1884
1885inline bool Object::IsWeakReferenceInstance() const {
1886 return GetClass()->IsWeakReferenceClass();
1887}
1888
1889inline bool Object::IsSoftReferenceInstance() const {
1890 return GetClass()->IsSoftReferenceClass();
1891}
1892
1893inline bool Object::IsFinalizerReferenceInstance() const {
1894 return GetClass()->IsFinalizerReferenceClass();
1895}
1896
1897inline bool Object::IsPhantomReferenceInstance() const {
1898 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07001899}
1900
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001901inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001902 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07001903 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001904 result = AsArray()->SizeOf();
1905 } else if (IsClass()) {
1906 result = AsClass()->SizeOf();
1907 } else {
1908 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001909 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001910 DCHECK(!IsField() || result == sizeof(Field));
1911 DCHECK(!IsMethod() || result == sizeof(Method));
1912 return result;
1913}
1914
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001915inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001916 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001917 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001918 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001919 return result;
1920}
1921
1922inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001923 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001924}
1925
1926inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001927 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001928 DCHECK(result != NULL) << this;
1929 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001930 return result;
1931}
1932
1933inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07001934 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001935}
1936
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001937inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07001938 // This is safe from overflow because the array was already allocated, so we know it's sane.
1939 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001940}
1941
1942template<class T>
1943void ObjectArray<T>::Set(int32_t i, T* object) {
1944 if (IsValidIndex(i)) {
1945 if (object != NULL) {
1946 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07001947 if (!object->InstanceOf(element_class)) {
1948 ThrowArrayStoreException(object);
1949 return;
1950 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001951 }
1952 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1953 SetFieldObject(data_offset, object, false);
1954 }
1955}
1956
1957template<class T>
1958void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
1959 DCHECK(IsValidIndex(i));
1960 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1961 SetFieldObject(data_offset, object, false);
1962}
1963
1964template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07001965T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
1966 DCHECK(IsValidIndex(i));
1967 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1968 return GetFieldObject<T*>(data_offset, false);
1969}
1970
1971template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001972void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
1973 ObjectArray<T>* dst, int dst_pos,
1974 size_t length) {
1975 if (src->IsValidIndex(src_pos) &&
1976 src->IsValidIndex(src_pos+length-1) &&
1977 dst->IsValidIndex(dst_pos) &&
1978 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogers5d76c432011-10-31 21:42:49 -07001979 MemberOffset src_offset(DataOffset().Int32Value() + src_pos * sizeof(Object*));
1980 MemberOffset dst_offset(DataOffset().Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001981 Class* array_class = dst->GetClass();
1982 if (array_class == src->GetClass()) {
1983 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001984 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001985 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Ian Rogers5d76c432011-10-31 21:42:49 -07001986 Heap::VerifyObject(object);
1987 // directly set field, we do a bulk write barrier at the end
1988 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001989 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
1990 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
1991 }
1992 } else {
1993 Class* element_class = array_class->GetComponentType();
1994 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07001995 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001996 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07001997 if (object != NULL && !object->InstanceOf(element_class)) {
1998 dst->ThrowArrayStoreException(object);
1999 return;
2000 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002001 Heap::VerifyObject(object);
2002 // directly set field, we do a bulk write barrier at the end
2003 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002004 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2005 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2006 }
2007 }
Ian Rogers5d76c432011-10-31 21:42:49 -07002008 Heap::WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002009 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002010}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002011
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002012class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002013 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002014 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002015 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002016 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002017 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2018};
2019
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002020class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002021 private:
2022 CharArray* ASCII_;
2023 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002024 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002025 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002026 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002027 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2028};
2029
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002030class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002031 private:
2032 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002033 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002034 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2035};
2036
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002037class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002038 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002039 Object* ORDER_BY_SIGNATURE_;
2040 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002041 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2042};
2043
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002044template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002045class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002046 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002047 typedef T ElementType;
2048
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002049 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002050
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002051 const T* GetData() const {
Elliott Hugheseaa200d2012-01-05 16:30:31 -08002052 return &elements_[0];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002053 }
2054
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002055 T* GetData() {
Elliott Hugheseaa200d2012-01-05 16:30:31 -08002056 return &elements_[0];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002057 }
2058
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002059 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002060 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002061 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002062 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002063 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002064 }
2065
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002066 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002067 // TODO: ArrayStoreException
2068 if (IsValidIndex(i)) {
2069 GetData()[i] = value;
2070 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002071 }
2072
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002073 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002074 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002075 CHECK(array_class != NULL);
2076 array_class_ = array_class;
2077 }
2078
Brian Carlstroma663ea52011-08-19 23:33:41 -07002079 static void ResetArrayClass() {
2080 CHECK(array_class_ != NULL);
2081 array_class_ = NULL;
2082 }
2083
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002084 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002085 // Location of first element.
2086 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002087
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002088 static Class* array_class_;
2089
Carl Shapirof88c9522011-08-06 15:47:38 -07002090 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002091};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002092
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002093// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002094class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002095 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002096 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002097 const CharArray* result = GetFieldObject<const CharArray*>(
2098 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2099 DCHECK(result != NULL);
2100 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002101 }
2102
Elliott Hughes814e4032011-08-23 12:07:56 -07002103 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002104 int32_t result = GetField32(
2105 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2106 DCHECK_LE(0, result);
2107 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002108 }
2109
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002110 int32_t GetLength() const;
2111
Brian Carlstrom395520e2011-09-25 19:35:00 -07002112 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002113
2114 void ComputeHashCode() {
2115 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002116 }
2117
Elliott Hughes814e4032011-08-23 12:07:56 -07002118 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002119 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002120 }
2121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002122 uint16_t CharAt(int32_t index) const;
2123
Brian Carlstromc74255f2011-09-11 22:47:39 -07002124 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002125
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002126 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002127 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002128 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002130 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002131
Jesse Wilson8989d992011-08-02 13:39:42 -07002132 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002133 const char* utf8_data_in);
2134
2135 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2136
2137 static String* Alloc(Class* java_lang_String, CharArray* array);
2138
2139 bool Equals(const char* modified_utf8) const;
2140
2141 // TODO: do we need this overload? give it a more intention-revealing name.
2142 bool Equals(const StringPiece& modified_utf8) const;
2143
2144 bool Equals(const String* that) const;
2145
Ian Rogers0571d352011-11-03 19:51:38 -07002146 // Compare UTF-16 code point values not in a locale-sensitive manner
2147 int Compare(int32_t utf16_length, const char* utf8_data_in);
2148
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002149 // TODO: do we need this overload? give it a more intention-revealing name.
2150 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2151 int32_t that_length) const;
2152
2153 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2154 std::string ToModifiedUtf8() const;
2155
2156 static Class* GetJavaLangString() {
2157 DCHECK(java_lang_String_ != NULL);
2158 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002159 }
2160
Brian Carlstroma663ea52011-08-19 23:33:41 -07002161 static void SetClass(Class* java_lang_String);
2162 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002163
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002164 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002165 void SetHashCode(int32_t new_hash_code) {
2166 DCHECK_EQ(0u,
2167 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2168 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2169 new_hash_code, false);
2170 }
2171
2172 void SetCount(int32_t new_count) {
2173 DCHECK_LE(0, new_count);
2174 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2175 }
2176
2177 void SetOffset(int32_t new_offset) {
2178 DCHECK_LE(0, new_offset);
2179 DCHECK_GE(GetLength(), new_offset);
2180 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2181 }
2182
2183 void SetArray(CharArray* new_array) {
2184 DCHECK(new_array != NULL);
2185 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2186 }
2187
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002188 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2189 CharArray* array_;
2190
Brian Carlstromdbc05252011-09-09 01:59:59 -07002191 int32_t count_;
2192
Carl Shapirof88c9522011-08-06 15:47:38 -07002193 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002194
Elliott Hughes289da822011-08-16 10:11:20 -07002195 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002196
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002197 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002198
Brian Carlstrom693267a2011-09-06 09:25:34 -07002199 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002200 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002201};
2202
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002203struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002204 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002205 return string->GetHashCode();
2206 }
2207};
2208
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002209inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002210 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002211 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2212}
2213
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002214inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002215 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002216 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002217}
2218
2219inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002220 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002221 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002222}
2223
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002224inline uint32_t Class::GetAccessFlags() const {
2225 // Check class is loaded or this is java.lang.String that has a
2226 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002227 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002228 this == String::GetJavaLangString() ||
2229 this == Field::GetJavaLangReflectField() ||
2230 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002231 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002232 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2233}
2234
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002235inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002236 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002237 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2238}
2239
2240inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002241 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002242 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002243}
2244
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002245inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002246 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002247 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002248}
2249
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002250inline String* Class::GetName() const {
2251 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002252}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002253inline void Class::SetName(String* name) {
2254 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002255}
2256
2257// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002258class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002259 public:
2260 void SetDetailMessage(String* new_detail_message) {
2261 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2262 new_detail_message, false);
2263 }
Ian Rogers9074b992011-10-26 17:41:55 -07002264 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002265
Ian Rogers1c5eb702012-02-01 09:18:34 -08002266 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2267 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2268 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2269 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002270 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002271
2272 static Class* GetJavaLangThrowable() {
2273 DCHECK(java_lang_Throwable_ != NULL);
2274 return java_lang_Throwable_;
2275 }
2276
2277 static void SetClass(Class* java_lang_Throwable);
2278 static void ResetClass();
2279
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002280 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002281 Object* GetStackState() const {
2282 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2283 }
2284
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002285 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2286 Throwable* cause_;
2287 String* detail_message_;
2288 Object* stack_state_; // Note this is Java volatile:
2289 Object* stack_trace_;
2290 Object* suppressed_exceptions_;
2291
Ian Rogers5167c972012-02-03 10:41:20 -08002292 static Class* java_lang_Throwable_;
2293
Brian Carlstrom693267a2011-09-06 09:25:34 -07002294 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002295 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2296};
2297
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002298// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002299class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002300 public:
2301 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002302 return GetFieldObject<const String*>(
2303 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002304 }
2305
2306 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002307 return GetFieldObject<const String*>(
2308 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002309 }
2310
2311 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002312 return GetFieldObject<const String*>(
2313 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002314 }
2315
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002316 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002317 return GetField32(
2318 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002319 }
2320
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002321 static StackTraceElement* Alloc(String* declaring_class,
2322 String* method_name,
2323 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002324 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002325
2326 static void SetClass(Class* java_lang_StackTraceElement);
2327
2328 static void ResetClass();
2329
2330 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002331 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002332 String* declaring_class_;
2333 String* file_name_;
2334 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002335 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002336
2337 static Class* GetStackTraceElement() {
2338 DCHECK(java_lang_StackTraceElement_ != NULL);
2339 return java_lang_StackTraceElement_;
2340 }
2341
2342 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002343
2344 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002345 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2346};
2347
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002348class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002349 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002350 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002351 Class* interface = Get(kInterface)->AsClass();
2352 DCHECK(interface != NULL);
2353 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002354 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002355
Brian Carlstrom30b94452011-08-25 21:35:26 -07002356 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002357 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002358 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002359 DCHECK(Get(kInterface) == NULL);
2360 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002361 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002362
Brian Carlstrom86927212011-09-15 11:31:11 -07002363 size_t GetMethodArrayCount() const {
2364 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002365 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002366 return 0;
2367 }
2368 return method_array->GetLength();
2369 }
2370
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002371 ObjectArray<Method>* GetMethodArray() const {
2372 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2373 DCHECK(method_array != NULL);
2374 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002375 }
2376
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002377 void SetMethodArray(ObjectArray<Method>* new_ma) {
2378 DCHECK(new_ma != NULL);
2379 DCHECK(Get(kMethodArray) == NULL);
2380 Set(kMethodArray, new_ma);
2381 }
2382
2383 static size_t LengthAsArray() {
2384 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002385 }
2386
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002387 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002388
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002389 enum ArrayIndex {
2390 // Points to the interface class.
2391 kInterface = 0,
2392 // Method pointers into the vtable, allow fast map from interface
2393 // method index to concrete instance method.
2394 kMethodArray = 1,
2395 kMax = 2,
2396 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002397
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002398 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002399};
2400
Ian Rogersc2b44472011-12-14 21:17:17 -08002401class MANAGED SynthesizedProxyClass : public Class {
2402 public:
2403 ObjectArray<ObjectArray<Class> >* GetThrows() {
2404 return throws_;
2405 }
Jesse Wilson95caa792011-10-12 18:14:17 -04002406 private:
Ian Rogersc2b44472011-12-14 21:17:17 -08002407 ObjectArray<ObjectArray<Class> >* throws_;
2408 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002409};
2410
2411class MANAGED Proxy : public Object {
2412 private:
2413 Object* h_;
2414
2415 friend struct ProxyOffsets; // for verifying offset information
2416 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2417};
2418
Carl Shapiro1fb86202011-06-27 17:43:13 -07002419} // namespace art
2420
2421#endif // ART_SRC_OBJECT_H_