blob: 882c025336ee71197f260bffa3a35f87145c30c3 [file] [log] [blame]
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_OBJECT_H_
18#define ART_SRC_OBJECT_H_
19
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070020#include <iosfwd>
Brian Carlstrom1f870082011-08-23 16:02:11 -070021#include <vector>
22
Elliott Hughes90a33692011-08-30 13:27:07 -070023#include "UniquePtr.h"
Elliott Hughes5ea047b2011-09-13 14:38:18 -070024#include "atomic.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070026#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070027#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070028#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070029#include "logging.h"
30#include "macros.h"
31#include "offsets.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070032#include "runtime.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070034#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070035#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
38
39class Array;
40class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070041class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070042class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070043class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040044class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045class InterfaceEntry;
46class Monitor;
47class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070048class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070049class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040050class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070051template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070052template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070053typedef PrimitiveArray<uint8_t> BooleanArray;
54typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070055typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070056typedef PrimitiveArray<double> DoubleArray;
57typedef PrimitiveArray<float> FloatArray;
58typedef PrimitiveArray<int32_t> IntArray;
59typedef PrimitiveArray<int64_t> LongArray;
60typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070061
Carl Shapiro3ee755d2011-06-28 12:11:04 -070062union JValue {
63 uint8_t z;
64 int8_t b;
65 uint16_t c;
66 int16_t s;
67 int32_t i;
68 int64_t j;
69 float f;
70 double d;
71 Object* l;
72};
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
75static const uint32_t kAccPrivate = 0x0002; // field, method, ic
76static const uint32_t kAccProtected = 0x0004; // field, method, ic
77static const uint32_t kAccStatic = 0x0008; // field, method, ic
78static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
79static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -070080static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -070081static const uint32_t kAccVolatile = 0x0040; // field
82static const uint32_t kAccBridge = 0x0040; // method (1.5)
83static const uint32_t kAccTransient = 0x0080; // field
84static const uint32_t kAccVarargs = 0x0080; // method (1.5)
85static const uint32_t kAccNative = 0x0100; // method
86static const uint32_t kAccInterface = 0x0200; // class, ic
87static const uint32_t kAccAbstract = 0x0400; // class, method, ic
88static const uint32_t kAccStrict = 0x0800; // method
89static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
90static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
91static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070093static const uint32_t kAccMiranda = 0x8000; // method
94
Brian Carlstroma331b3c2011-07-18 17:47:56 -070095static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
96
Elliott Hughes582a7d12011-10-10 18:38:42 -070097static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
98static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
99static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700100
Elliott Hughes20cde902011-10-04 17:37:27 -0700101// Special runtime-only flags.
102// Note: if only kAccClassIsReference is set, we have a soft reference.
103static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
104static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
105static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
106static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
107static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700108
109static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
110 | kAccClassIsWeakReference
111 | kAccClassIsFinalizerReference
112 | kAccClassIsPhantomReference);
113
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700114/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700115 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116 */
117/*
118 * A magic value for refOffsets. Ignore the bits and walk the super
119 * chain when this is the value.
120 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
121 * fields followed by 2 ref instance fields.]
122 */
123#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
125#define CLASS_OFFSET_ALIGNMENT 4
126#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
127/*
128 * Given an offset, return the bit number which would encode that offset.
129 * Local use only.
130 */
131#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700132 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700133 CLASS_OFFSET_ALIGNMENT)
134/*
135 * Is the given offset too large to be encoded?
136 */
137#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
138 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
139/*
140 * Return a single bit, encoding the offset.
141 * Undefined if the offset is too large, as defined above.
142 */
143#define CLASS_BIT_FROM_OFFSET(byteOffset) \
144 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
145/*
146 * Return an offset, given a bit number as returned from CLZ.
147 */
148#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700149 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700151#define OFFSET_OF_OBJECT_MEMBER(type, field) \
152 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700153
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700154// Classes shared with the managed side of the world need to be packed
155// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700156#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700157
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700158// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700159class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700160 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 static MemberOffset ClassOffset() {
162 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700163 }
164
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700165 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700166 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700167 }
168
169 void SetClass(Class* new_klass);
170
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700171 bool InstanceOf(const Class* klass) const;
172
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700173 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700174
Elliott Hughes081be7f2011-09-18 16:50:26 -0700175 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700176
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700177 static MemberOffset MonitorOffset() {
178 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
179 }
180
Elliott Hughes5f791332011-09-15 17:45:30 -0700181 volatile int32_t* GetRawLockWordAddress() {
182 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
183 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
184 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 }
186
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700187 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700188
Elliott Hughes5f791332011-09-15 17:45:30 -0700189 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700190
Ian Rogersff1ed472011-09-20 13:46:24 -0700191 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192
Elliott Hughes5f791332011-09-15 17:45:30 -0700193 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
Elliott Hughes5f791332011-09-15 17:45:30 -0700195 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196
Elliott Hughes5f791332011-09-15 17:45:30 -0700197 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700198
Elliott Hughes5f791332011-09-15 17:45:30 -0700199 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700200
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700201 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202
203 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700204 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700205 return down_cast<Class*>(this);
206 }
207
208 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210 return down_cast<const Class*>(this);
211 }
212
Brian Carlstrom4873d462011-08-21 15:23:39 -0700213 bool IsClassClass() const;
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
Brian Carlstroma663ea52011-08-19 23:33:41 -0700235 bool IsString() const;
236
237 String* AsString() {
238 DCHECK(IsString());
239 return down_cast<String*>(this);
240 }
241
242 bool IsMethod() const;
243
244 Method* AsMethod() {
245 DCHECK(IsMethod());
246 return down_cast<Method*>(this);
247 }
248
Brian Carlstrom4873d462011-08-21 15:23:39 -0700249 const Method* AsMethod() const {
250 DCHECK(IsMethod());
251 return down_cast<const Method*>(this);
252 }
253
Brian Carlstroma663ea52011-08-19 23:33:41 -0700254 bool IsField() const;
255
256 Field* AsField() {
257 DCHECK(IsField());
258 return down_cast<Field*>(this);
259 }
260
Brian Carlstrom4873d462011-08-21 15:23:39 -0700261 const Field* AsField() const {
262 DCHECK(IsField());
263 return down_cast<const Field*>(this);
264 }
265
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700266 bool IsReferenceInstance() const;
267
268 bool IsWeakReferenceInstance() const;
269
270 bool IsSoftReferenceInstance() const;
271
272 bool IsFinalizerReferenceInstance() const;
273
274 bool IsPhantomReferenceInstance() const;
275
276 // Accessors for Java type fields
277 template<class T>
278 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700279 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
280 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 Heap::VerifyObject(result);
282 return result;
283 }
284
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700285 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 Heap::VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700287 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
288 if (new_value != NULL) {
289 Heap::WriteBarrier(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700290 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700291 }
292
293 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
294 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700295 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
296 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700297 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700298 return android_atomic_acquire_load(word_addr);
299 } else {
300 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700302 }
303
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700304 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
305 if (this_is_valid) {
306 Heap::VerifyObject(this);
307 }
308 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700309 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700310 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700311 /*
312 * TODO: add an android_atomic_synchronization_store() function and
313 * use it in the 32-bit volatile set handlers. On some platforms we
314 * can use a fast atomic instruction and avoid the barriers.
315 */
316 ANDROID_MEMBAR_STORE();
317 *word_addr = new_value;
318 ANDROID_MEMBAR_FULL();
319 } else {
320 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700322 }
323
324 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
325 Heap::VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700326 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700327 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700328 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700329 uint64_t result = QuasiAtomicRead64(addr);
330 ANDROID_MEMBAR_FULL();
331 return result;
332 } else {
333 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700334 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
336
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700337 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700338 Heap::VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700339 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
340 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700341 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700342 ANDROID_MEMBAR_STORE();
343 QuasiAtomicSwap64(new_value, addr);
344 // Post-store barrier not required due to use of atomic op or mutex.
345 } else {
346 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700347 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700348 }
349
350 protected:
351 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700352 template<class T>
353 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700354 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
356
357 template<typename T>
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700358 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile) {
359 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700360 }
361
362 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700363 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364
Elliott Hughes5f791332011-09-15 17:45:30 -0700365 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366
Elliott Hughes5f791332011-09-15 17:45:30 -0700367 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700368 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700369 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370};
371
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700372struct ObjectIdentityHash {
373 size_t operator()(const art::Object* const& obj) const {
374#ifdef MOVING_GARBAGE_COLLECTOR
375 // TODO: we'll need to use the Object's internal concept of identity
376 UNIMPLEMENTED(FATAL);
377#endif
378 return reinterpret_cast<size_t>(obj);
379 }
380};
381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382// C++ mirror of java.lang.reflect.AccessibleObject
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700383class MANAGED AccessibleObject : public Object {
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400384 private:
385 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 uint32_t java_flag_; // can accessibility checks be bypassed
Brian Carlstrom693267a2011-09-06 09:25:34 -0700387 friend struct AccessibleObjectOffsets; // for verifying offset information
388 DISALLOW_IMPLICIT_CONSTRUCTORS(AccessibleObject);
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400389};
390
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700391// C++ mirror of java.lang.reflect.Field
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700392class MANAGED Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700393 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700395
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 void SetDeclaringClass(Class *new_declaring_class);
397
398 const String* GetName() const;
399
400 void SetName(String* new_name);
401
402 uint32_t GetAccessFlags() const;
403
404 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700405 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700406 }
407
Elliott Hughes80609252011-09-23 17:24:51 -0700408 bool IsPublic() const {
409 return (GetAccessFlags() & kAccPublic) != 0;
410 }
411
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700412 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700413 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700414 }
415
jeffhaobdb76512011-09-07 11:43:16 -0700416 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700417 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700418 }
419
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420 uint32_t GetTypeIdx() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 void SetTypeIdx(uint32_t type_idx);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 // Gets type using type index and resolved types in the dex cache, may be null
425 // if type isn't yet resolved
426 Class* GetTypeDuringLinking() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700427
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700428 // Performs full resolution, may return null and set exceptions if type cannot
429 // be resolved
430 Class* GetType() const;
431
432 // Offset to field within an Object
433 MemberOffset GetOffset() const;
434
buzbee34cd9e52011-09-08 14:31:52 -0700435 static MemberOffset OffsetOffset() {
436 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
437 }
438
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700439 MemberOffset GetOffsetDuringLinking() const;
440
441 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700442
Brian Carlstrom4873d462011-08-21 15:23:39 -0700443 // field access, null object for static fields
444 bool GetBoolean(const Object* object) const;
445 void SetBoolean(Object* object, bool z) const;
446 int8_t GetByte(const Object* object) const;
447 void SetByte(Object* object, int8_t b) const;
448 uint16_t GetChar(const Object* object) const;
449 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700450 int16_t GetShort(const Object* object) const;
451 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700452 int32_t GetInt(const Object* object) const;
453 void SetInt(Object* object, int32_t i) const;
454 int64_t GetLong(const Object* object) const;
455 void SetLong(Object* object, int64_t j) const;
456 float GetFloat(const Object* object) const;
457 void SetFloat(Object* object, float f) const;
458 double GetDouble(const Object* object) const;
459 void SetDouble(Object* object, double d) const;
460 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700461 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700462
Ian Rogersce9eca62011-10-07 17:11:03 -0700463 // raw field accesses
464 uint32_t Get32(const Object* object) const;
465 void Set32(Object* object, uint32_t new_value) const;
466 uint64_t Get64(const Object* object) const;
467 void Set64(Object* object, uint64_t new_value) const;
468 Object* GetObj(const Object* object) const;
469 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700470
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700471 static Class* GetJavaLangReflectField() {
472 DCHECK(java_lang_reflect_Field_ != NULL);
473 return java_lang_reflect_Field_;
474 }
475
476 static void SetClass(Class* java_lang_reflect_Field);
477 static void ResetClass();
478
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700479 bool IsVolatile() const {
480 return (GetAccessFlags() & kAccVolatile) != 0;
481 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700482
Elliott Hughes80609252011-09-23 17:24:51 -0700483 void InitJavaFields();
484
buzbee1da522d2011-09-04 11:22:20 -0700485 private:
Elliott Hughes80609252011-09-23 17:24:51 -0700486 void InitJavaFieldsLocked();
487
Jesse Wilson35baaab2011-08-10 16:18:03 -0400488 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Brian Carlstrom693267a2011-09-06 09:25:34 -0700489
Jesse Wilson35baaab2011-08-10 16:18:03 -0400490 // The class in which this field is declared.
491 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700492
Jesse Wilson35baaab2011-08-10 16:18:03 -0400493 Object* generic_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700494
Brian Carlstromdbc05252011-09-09 01:59:59 -0700495 const String* name_;
496
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 // Type of the field
Elliott Hughes80609252011-09-23 17:24:51 -0700498 mutable Class* type_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400499
Brian Carlstromdbc05252011-09-09 01:59:59 -0700500 uint32_t generic_types_are_initialized_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700501
Jesse Wilson35baaab2011-08-10 16:18:03 -0400502 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700503
504 // Offset of field within an instance or in the Class' static fields
505 uint32_t offset_;
506
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700507 // Dex cache index of resolved type
508 uint32_t type_idx_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400509
Brian Carlstrom693267a2011-09-06 09:25:34 -0700510 int32_t slot_;
511
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700512 static Class* java_lang_reflect_Field_;
513
Brian Carlstrom693267a2011-09-06 09:25:34 -0700514 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400515 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700516};
517
Jesse Wilson6bf19152011-09-29 13:12:33 -0400518// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700519class MANAGED Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700520 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700521 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700522 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700523 Object* obj,
524 Thread* thread,
525 byte* args,
526 JValue* result);
527
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700528 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700530 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700531
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700532 static MemberOffset DeclaringClassOffset() {
533 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700534 }
535
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700536 // Returns the method name, e.g. "<init>" or "eatLunch"
Ian Rogers466bb252011-10-14 03:29:56 -0700537 String* GetName() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700538
539 void SetName(String* new_name);
540
jeffhaoe23d93c2011-09-15 14:48:43 -0700541 ByteArray* GetRegisterMapData() const;
542
jeffhaoa0a764a2011-09-16 10:43:38 -0700543 void SetRegisterMapData(ByteArray* data);
jeffhaoe23d93c2011-09-15 14:48:43 -0700544
545 ByteArray* GetRegisterMapHeader() const;
546
jeffhaoa0a764a2011-09-16 10:43:38 -0700547 void SetRegisterMapHeader(ByteArray* header);
jeffhaoe23d93c2011-09-15 14:48:43 -0700548
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700549 String* GetShorty() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700551 void SetShorty(String* new_shorty);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700552
Ian Rogers466bb252011-10-14 03:29:56 -0700553 String* GetSignature() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554
555 void SetSignature(String* new_signature);
556
Ian Rogers466bb252011-10-14 03:29:56 -0700557 bool HasSameNameAndSignature(const Method* that) const {
558 return GetName() == that->GetName() && GetSignature() == that->GetSignature();
559 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700560
561 uint32_t GetAccessFlags() const;
562
563 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700564 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700565 }
566
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700567 // Returns true if the method is declared public.
568 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700569 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700570 }
571
572 // Returns true if the method is declared private.
573 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700574 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700575 }
576
577 // Returns true if the method is declared static.
578 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580 }
581
Brian Carlstrom1f870082011-08-23 16:02:11 -0700582 // Returns true if the method is a constructor.
583 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700584 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700585 }
586
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700587 // Is this method <clinit>
588 bool IsClassInitializer() const;
589
Brian Carlstrom1f870082011-08-23 16:02:11 -0700590 // Returns true if the method is static, private, or a constructor.
591 bool IsDirect() const {
592 return IsStatic() || IsPrivate() || IsConstructor();
593 }
594
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700595 // Returns true if the method is declared synchronized.
596 bool IsSynchronized() const {
597 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700598 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700599 }
600
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700601 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700602 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700603 }
604
Elliott Hughes80609252011-09-23 17:24:51 -0700605 bool IsMiranda() const {
606 return (GetAccessFlags() & kAccMiranda) != 0;
607 }
608
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700609 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700610 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700611 }
612
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700613 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700614 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700615 }
616
617 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700618 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700619 }
620
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 uint16_t GetMethodIndex() const;
622
623 size_t GetVtableIndex() const {
624 return GetMethodIndex();
625 }
626
627 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700628 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700629 }
630
631 static MemberOffset MethodIndexOffset() {
632 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
633 }
634
635 uint32_t GetCodeItemOffset() const {
636 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
637 }
638
639 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700640 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700641 }
642
643 // Number of 32bit registers that would be required to hold all the arguments
644 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700646 // Number of argument bytes required for densely packing the
647 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700648 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700649
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700650 uint16_t NumRegisters() const;
651
652 void SetNumRegisters(uint16_t new_num_registers) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700653 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), new_num_registers, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700654 }
655
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700656 uint16_t NumIns() const;
657
658 void SetNumIns(uint16_t new_num_ins) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700659 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), new_num_ins, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700660 }
661
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700662 uint16_t NumOuts() const;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700663
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700664 void SetNumOuts(uint16_t new_num_outs) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700665 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), new_num_outs, false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700666 }
667
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700668 uint32_t GetProtoIdx() const;
669
670 void SetProtoIdx(uint32_t new_proto_idx) {
671 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), new_proto_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700672 }
673
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700674 ObjectArray<String>* GetDexCacheStrings() const;
675 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
676
677 static MemberOffset DexCacheStringsOffset() {
678 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
679 }
680
buzbee2a475e72011-09-07 17:19:17 -0700681 static MemberOffset DexCacheResolvedTypesOffset() {
682 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
683 }
684
buzbee34cd9e52011-09-08 14:31:52 -0700685 static MemberOffset DexCacheResolvedFieldsOffset() {
686 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_fields_);
687 }
688
buzbee1da522d2011-09-04 11:22:20 -0700689 static MemberOffset DexCacheInitializedStaticStorageOffset() {
690 return OFFSET_OF_OBJECT_MEMBER(Method,
691 dex_cache_initialized_static_storage_);
692 }
693
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700694 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
695 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
696
697 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
698 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
699
700 ObjectArray<Field>* GetDexCacheResolvedFields() const;
701 void SetDexCacheResolvedFields(ObjectArray<Field>* new_dex_cache_fields);
702
703 CodeAndDirectMethods* GetDexCacheCodeAndDirectMethods() const;
704 void SetDexCacheCodeAndDirectMethods(CodeAndDirectMethods* new_value);
705
706 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
707 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
708
Ian Rogers466bb252011-10-14 03:29:56 -0700709 // Find the method that this method overrides
710 Method* FindOverriddenMethod() const;
711
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700712 void SetReturnTypeIdx(uint32_t new_return_type_idx);
713
714 Class* GetReturnType() const;
715
716 bool IsReturnAReference() const;
717
718 bool IsReturnAFloat() const;
719
720 bool IsReturnADouble() const;
721
Ian Rogersb033c752011-07-20 12:22:35 -0700722 bool IsReturnAFloatOrDouble() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700723 return IsReturnAFloat() || IsReturnADouble();
Ian Rogersb033c752011-07-20 12:22:35 -0700724 }
725
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700726 bool IsReturnALong() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700727
Ian Rogersae675992011-10-09 17:10:22 -0700728 bool IsReturnALongOrDouble() const {
729 return IsReturnALong() || IsReturnADouble();
730 }
731
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700732 bool IsReturnVoid() const;
Ian Rogers45a76cb2011-07-21 22:00:15 -0700733
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700734 // "Args" may refer to any of the 3 levels of "Args."
735 // To avoid confusion, our code will denote which "Args" clearly:
736 // 1. UserArgs: Args that a user see.
737 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
738 // receiver.
739 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
740 // E.g., the first in Args is Method* for both static and non-static
741 // methods. And CConvArgs doesn't deal with the receiver because
742 // receiver is hardwired in an implicit register, so CConvArgs doesn't
743 // need to deal with it.
744 //
745 // The number of Args that should be supplied to this method
Brian Carlstrom2ed67392011-09-09 14:53:28 -0700746 size_t NumArgs() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700747
748 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700749 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700750 size_t NumReferenceArgs() const;
751
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700752 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700753 size_t NumLongOrDoubleArgs() const;
754
Ian Rogersb033c752011-07-20 12:22:35 -0700755 // Is the given method parameter a reference?
756 bool IsParamAReference(unsigned int param) const;
757
758 // Is the given method parameter a long or double?
759 bool IsParamALongOrDouble(unsigned int param) const;
760
Ian Rogersdf20fe02011-07-20 20:34:16 -0700761 // Size in bytes of the given parameter
762 size_t ParamSize(unsigned int param) const;
763
764 // Size in bytes of the return value
765 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700766
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700767 void Invoke(Thread* self, Object* receiver, byte* args, JValue* result) const;
768
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700769 const void* GetCode() const {
770 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700771 }
772
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700773 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700774 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
775 }
776
777 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700778 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700779 return reinterpret_cast<uint32_t>(GetCode());
780 }
781
782 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700783 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700784 SetCode(reinterpret_cast<void*>(code_offset));
785 }
786
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700787 static MemberOffset GetCodeOffset() {
788 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700789 }
790
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700791 const uint32_t* GetMappingTable() const {
792 const uint32_t* map = GetMappingTableRaw();
793 if (map == NULL) {
794 return map;
795 }
796 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700797 }
798
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700799 uint32_t GetMappingTableLength() const {
800 const uint32_t* map = GetMappingTableRaw();
801 if (map == NULL) {
802 return 0;
803 }
804 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700805 }
806
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700807 const uint32_t* GetMappingTableRaw() const {
808 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700809 }
810
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700811 void SetMappingTable(const uint32_t* mapping_table) {
812 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
813 mapping_table, false);
814 }
815
816 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700817 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700818 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
819 }
820
821 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700822 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700823 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
824 }
825
826 const uint16_t* GetVmapTable() const {
827 const uint16_t* vmap = GetVmapTableRaw();
828 if (vmap == NULL) {
829 return vmap;
830 }
831 return vmap + 1;
832 }
833
834 uint16_t GetVmapTableLength() const {
835 const uint16_t* vmap = GetVmapTableRaw();
836 if (vmap == NULL) {
837 return 0;
838 }
839 return *vmap;
840 }
841
842 const uint16_t* GetVmapTableRaw() const {
843 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
844 }
845
846 void SetVmapTable(const uint16_t* vmap_table) {
847 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
848 }
849
850 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700851 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700852 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
853 }
854
855 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700856 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700857 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700858 }
859
Shih-wei Liaod11af152011-08-23 16:02:11 -0700860 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700861 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
862 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700863 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
864 return result;
865 }
866
867 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700868 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700869 DCHECK_LE(static_cast<size_t>(kStackAlignment), new_frame_size_in_bytes);
870 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
871 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700872 }
873
Shih-wei Liaod11af152011-08-23 16:02:11 -0700874 size_t GetReturnPcOffsetInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700875 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
876 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_), false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700877 }
878
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700879 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700880 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700881 DCHECK_LT(return_pc_offset_in_bytes, GetFrameSizeInBytes());
882 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, return_pc_offset_in_bytes_),
883 return_pc_offset_in_bytes, false);
buzbeec143c552011-08-20 17:38:58 -0700884 }
885
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700886 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700887
Brian Carlstrom16192862011-09-12 17:50:06 -0700888 void RegisterNative(const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700889
Brian Carlstrom16192862011-09-12 17:50:06 -0700890 void UnregisterNative();
Elliott Hughes5174fe62011-08-23 15:12:35 -0700891
Ian Rogersb033c752011-07-20 12:22:35 -0700892 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700893 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700894 }
895
Brian Carlstrom78128a62011-09-15 17:21:19 -0700896 const void* GetNativeMethod() const {
897 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
898 }
899
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700900 // Native to managed invocation stub entry point
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700901 InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700902 InvokeStub* result = GetFieldPtr<InvokeStub*>(
903 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
904 // TODO: DCHECK(result != NULL); should be ahead of time compiled
905 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700906 }
907
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700908 void SetInvokeStub(InvokeStub* invoke_stub) {
909 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
910 invoke_stub, false);
911 }
912
913 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700914 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700915 return reinterpret_cast<uint32_t>(GetInvokeStub());
916 }
917
918 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700919 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700920 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
921 }
922
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700923 static MemberOffset GetInvokeStubOffset() {
924 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700925 }
926
buzbee561227c2011-09-02 15:28:19 -0700927 static MemberOffset GetDexCacheCodeAndDirectMethodsOffset() {
928 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_code_and_direct_methods_);
929 }
930
931 static MemberOffset GetDexCacheResolvedMethodsOffset() {
932 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
933 }
934
935 static MemberOffset GetMethodIndexOffset() {
936 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
937 }
938
Ian Rogers90865722011-09-19 11:11:44 -0700939 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700940 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700941 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700942
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700943 void SetCoreSpillMask(uint32_t core_spill_mask) {
944 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700945 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700946 }
947
Ian Rogers90865722011-09-19 11:11:44 -0700948 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700949 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
950 }
951
952 void SetFpSpillMask(uint32_t fp_spill_mask) {
953 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700954 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700955 }
956
Ian Rogers466bb252011-10-14 03:29:56 -0700957 ObjectArray<Class>* GetExceptionTypes() const {
958 return GetFieldObject<ObjectArray<Class>*>(
959 OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), false);
Jesse Wilson95caa792011-10-12 18:14:17 -0400960 }
961
Ian Rogers466bb252011-10-14 03:29:56 -0700962 void SetExceptionTypes(ObjectArray<Class>* exception_types);
963
Ian Rogersdfcdf1a2011-10-10 17:50:35 -0700964 ObjectArray<Class>* GetJavaParameterTypes() const {
965 return GetFieldObject<ObjectArray<Class>*>(
966 OFFSET_OF_OBJECT_MEMBER(Method, java_parameter_types_), false);
967 }
968
Ian Rogers90865722011-09-19 11:11:44 -0700969 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700970 bool IsCalleeSaveMethod() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700971 Runtime* runtime = Runtime::Current();
972 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700973 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700974 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
975 result = true;
976 break;
977 }
978 }
Ian Rogers90865722011-09-19 11:11:44 -0700979 // Check that if we do think it is phony it looks like the callee save method
980 DCHECK(!result || GetCoreSpillMask() != 0);
981 return result;
982 }
983
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700984 // Converts a native PC to a dex PC. TODO: this is a no-op
985 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700986 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700987
988 // Converts a dex PC to a native PC. TODO: this is a no-op
989 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700990 uintptr_t ToNativePC(const uint32_t dex_pc) const;
991
992 // Find the catch block for the given exception type and dex_pc
993 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700994
Elliott Hughes80609252011-09-23 17:24:51 -0700995 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
996
997 static Class* GetConstructorClass() {
998 return java_lang_reflect_Constructor_;
999 }
1000
1001 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001002 return java_lang_reflect_Method_;
1003 }
1004
Elliott Hughes80609252011-09-23 17:24:51 -07001005 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001006
Elliott Hughes418d20f2011-09-22 14:00:39 -07001007 void InitJavaFields();
1008
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009 private:
1010 uint32_t GetReturnTypeIdx() const;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001011 void InitJavaFieldsLocked();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001012
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001013 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1014 // the class we are a part of
1015 Class* declaring_class_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001016 ObjectArray<Class>* java_exception_types_; // TODO
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001017 Object* java_formal_type_parameters_;
1018 Object* java_generic_exception_types_;
1019 Object* java_generic_parameter_types_;
1020 Object* java_generic_return_type_;
buzbeec143c552011-08-20 17:38:58 -07001021
Brian Carlstrom693267a2011-09-06 09:25:34 -07001022 String* name_;
1023
Elliott Hughes418d20f2011-09-22 14:00:39 -07001024 // Initialized by InitJavaFields.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001025 ObjectArray<Class>* java_parameter_types_;
Elliott Hughes418d20f2011-09-22 14:00:39 -07001026 Class* java_return_type_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001027
Brian Carlstrom693267a2011-09-06 09:25:34 -07001028 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstrom693267a2011-09-06 09:25:34 -07001029 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
1030
1031 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1032 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
1033
1034 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1035 ObjectArray<Field>* dex_cache_resolved_fields_;
1036
1037 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1038 ObjectArray<Method>* dex_cache_resolved_methods_;
1039
Brian Carlstromdbc05252011-09-09 01:59:59 -07001040 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1041 ObjectArray<Class>* dex_cache_resolved_types_;
1042
1043 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
1044 ObjectArray<String>* dex_cache_strings_;
1045
jeffhaoe23d93c2011-09-15 14:48:43 -07001046 // Byte arrays that hold data for the register maps
1047 const ByteArray* register_map_data_;
1048 const ByteArray* register_map_header_;
1049
Brian Carlstrom2ed67392011-09-09 14:53:28 -07001050 // The short-form method descriptor string.
1051 String* shorty_;
1052
Brian Carlstromdbc05252011-09-09 01:59:59 -07001053 // The method descriptor. This represents the parameters a method
1054 // takes and value it returns. This string is a list of the type
1055 // descriptors for the parameters enclosed in parenthesis followed
1056 // by the return type descriptor. For example, for the method
1057 //
1058 // Object mymethod(int i, double d, Thread t)
1059 //
1060 // the method descriptor would be
1061 //
1062 // (IDLjava/lang/Thread;)Ljava/lang/Object;
1063 String* signature_;
1064
1065 uint32_t java_generic_types_are_initialized_;
1066
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001067 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001068 uint32_t access_flags_;
1069
1070 // Compiled code associated with this method for callers from managed code.
1071 // May be compiled managed code or a bridge for invoking a native method.
1072 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001073
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001074 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001075 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -07001076
Brian Carlstrom693267a2011-09-06 09:25:34 -07001077 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -07001078 uint32_t core_spill_mask_;
1079
1080 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -07001081 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001082
Brian Carlstrom693267a2011-09-06 09:25:34 -07001083 // Total size in bytes of the frame
1084 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085
Brian Carlstrom693267a2011-09-06 09:25:34 -07001086 // Native invocation stub entry point for calling from native to managed code.
1087 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -07001088
Brian Carlstrom693267a2011-09-06 09:25:34 -07001089 // Index of the return type
1090 uint32_t java_return_type_idx_;
1091
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001092 const uint32_t* mapping_table_;
1093
Ian Rogers466bb252011-10-14 03:29:56 -07001094 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -07001095 //
Ian Rogers466bb252011-10-14 03:29:56 -07001096 // For abstract methods in an interface class, this is the offset of the method in
1097 // "iftable_->Get(n)->GetMethodArray()".
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001098 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -07001099
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001100 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -07001101 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001102
Brian Carlstrom693267a2011-09-06 09:25:34 -07001103 // Method bounds; not needed for an abstract method.
1104 //
1105 // For a native method, we compute the size of the argument list, and
1106 // set "insSize" and "registerSize" equal to it.
Elliott Hughes1d3f1142011-09-13 12:00:00 -07001107 uint32_t num_ins_;
1108 uint32_t num_outs_;
1109 uint32_t num_registers_; // ins + locals
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -07001110
Brian Carlstrom693267a2011-09-06 09:25:34 -07001111 // Method prototype descriptor string (return and argument types).
1112 uint32_t proto_idx_;
1113
1114 // Offset of return PC within frame for compiled code (in bytes)
1115 size_t return_pc_offset_in_bytes_;
1116
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001117 const uint16_t* vmap_table_;
1118
Brian Carlstrom693267a2011-09-06 09:25:34 -07001119 uint32_t java_slot_;
Carl Shapiro9b9ba282011-08-14 15:30:39 -07001120
Elliott Hughes80609252011-09-23 17:24:51 -07001121 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001122 static Class* java_lang_reflect_Method_;
1123
Brian Carlstrom693267a2011-09-06 09:25:34 -07001124 friend class ImageWriter; // for relocating code_ and invoke_stub_
1125 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001126 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001127};
1128
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001129class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001130 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001131 // A convenience for code that doesn't know the component size,
1132 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001133 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001134
Brian Carlstromb63ec392011-08-27 17:38:27 -07001135 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001136
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001137 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001138
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001139 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001140 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001141 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001142
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001143 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001144 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001145 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001146 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001147
buzbeec143c552011-08-20 17:38:58 -07001148 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001149 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001150 }
1151
1152 static MemberOffset DataOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001153 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
buzbeec143c552011-08-20 17:38:58 -07001154 }
1155
Elliott Hughesbf86d042011-08-31 17:53:14 -07001156 void* GetRawData() {
1157 return reinterpret_cast<void*>(first_element_);
1158 }
1159
Elliott Hughes289da822011-08-16 10:11:20 -07001160 protected:
1161 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001162 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001163 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001164 }
1165 return true;
1166 }
1167
Elliott Hughes80609252011-09-23 17:24:51 -07001168 protected:
1169 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1170 bool ThrowArrayStoreException(Object* object) const;
1171
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001172 private:
1173 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001174 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001175 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
1176 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -07001177 // Marker for the data (used by generated code)
1178 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001179
Carl Shapirof88c9522011-08-06 15:47:38 -07001180 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001181};
1182
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001183template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001184class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001185 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001186 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001187
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001188 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001189
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001190 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001191
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001192 // Set element without bound and element type checks, to be used in limited
1193 // circumstances, such as during boot image writing
1194 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001195
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001196 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001197 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001198 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001200 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001201
1202 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001203 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001204};
1205
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001206template<class T>
1207ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
1208 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
1209}
1210
1211template<class T>
1212T* ObjectArray<T>::Get(int32_t i) const {
1213 if (!IsValidIndex(i)) {
1214 return NULL;
1215 }
1216 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
1217 return GetFieldObject<T*>(data_offset, false);
1218}
1219
1220template<class T>
1221ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1222 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1223 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1224 return new_array;
1225}
1226
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001227// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001228// provides the static storage. However, this might change to an Array
1229// to improve image sharing, so we use this type to avoid assumptions
1230// on the current storage.
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001231class MANAGED StaticStorageBase : public Object {};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001232
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001233// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001234class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001235 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001236
1237 // Class Status
1238 //
1239 // kStatusNotReady: If a Class cannot be found in the class table by
1240 // FindClass, it allocates an new one with AllocClass in the
1241 // kStatusNotReady and calls LoadClass. Note if it does find a
1242 // class, it may not be kStatusResolved and it will try to push it
1243 // forward toward kStatusResolved.
1244 //
1245 // kStatusIdx: LoadClass populates with Class with information from
1246 // the DexFile, moving the status to kStatusIdx, indicating that the
1247 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001248 // populated based on super_class_type_idx_ and
1249 // interfaces_type_idx_. The new Class can then be inserted into the
1250 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001251 //
1252 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1253 // attempt to move a kStatusIdx class forward to kStatusLoaded by
1254 // using ResolveClass to initialize the super_class_ and interfaces_.
1255 //
1256 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001257 // shows linking is complete and fields of the Class populated by making
1258 // it kStatusResolved. Java allows circularities of the form where a super
1259 // class has a field that is of the type of the sub class. We need to be able
1260 // to fully resolve super classes while resolving types for fields.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001261
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001262 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001263 kStatusError = -1,
1264 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001265 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001266 kStatusLoaded = 2, // DEX idx values resolved
1267 kStatusResolved = 3, // part of linking
1268 kStatusVerifying = 4, // in the process of being verified
1269 kStatusVerified = 5, // logically part of linking; done pre-init
1270 kStatusInitializing = 6, // class init in progress
1271 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001272 };
1273
1274 enum PrimitiveType {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001275 kPrimNot = 0,
1276 kPrimBoolean,
1277 kPrimByte,
1278 kPrimChar,
1279 kPrimShort,
1280 kPrimInt,
1281 kPrimLong,
1282 kPrimFloat,
1283 kPrimDouble,
1284 kPrimVoid,
Carl Shapiro1fb86202011-06-27 17:43:13 -07001285 };
1286
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001287 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001288 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1289 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001290 }
1291
1292 void SetStatus(Status new_status);
1293
1294 // Returns true if the class has failed to link.
1295 bool IsErroneous() const {
1296 return GetStatus() == kStatusError;
1297 }
1298
1299 // Returns true if the class has been loaded.
1300 bool IsIdxLoaded() const {
1301 return GetStatus() >= kStatusIdx;
1302 }
1303
1304 // Returns true if the class has been loaded.
1305 bool IsLoaded() const {
1306 return GetStatus() >= kStatusLoaded;
1307 }
1308
1309 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001310 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001311 return GetStatus() >= kStatusResolved;
1312 }
1313
1314 // Returns true if the class has been verified.
1315 bool IsVerified() const {
1316 return GetStatus() >= kStatusVerified;
1317 }
1318
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001319 // Returns true if the class is initializing.
1320 bool IsInitializing() const {
1321 return GetStatus() >= kStatusInitializing;
1322 }
1323
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001324 // Returns true if the class is initialized.
1325 bool IsInitialized() const {
1326 return GetStatus() == kStatusInitialized;
1327 }
1328
1329 uint32_t GetAccessFlags() const;
1330
1331 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001332 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 }
1334
1335 // Returns true if the class is an interface.
1336 bool IsInterface() const {
1337 return (GetAccessFlags() & kAccInterface) != 0;
1338 }
1339
1340 // Returns true if the class is declared public.
1341 bool IsPublic() const {
1342 return (GetAccessFlags() & kAccPublic) != 0;
1343 }
1344
1345 // Returns true if the class is declared final.
1346 bool IsFinal() const {
1347 return (GetAccessFlags() & kAccFinal) != 0;
1348 }
1349
Elliott Hughes20cde902011-10-04 17:37:27 -07001350 bool IsFinalizable() const {
1351 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1352 }
1353
1354 void SetFinalizable() {
1355 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1356 SetAccessFlags(flags | kAccClassIsFinalizable);
1357 }
1358
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001359 // Returns true if the class is abstract.
1360 bool IsAbstract() const {
1361 return (GetAccessFlags() & kAccAbstract) != 0;
1362 }
1363
1364 // Returns true if the class is an annotation.
1365 bool IsAnnotation() const {
1366 return (GetAccessFlags() & kAccAnnotation) != 0;
1367 }
1368
1369 // Returns true if the class is synthetic.
1370 bool IsSynthetic() const {
1371 return (GetAccessFlags() & kAccSynthetic) != 0;
1372 }
1373
1374 bool IsReferenceClass() const {
1375 return (GetAccessFlags() & kAccClassIsReference) != 0;
1376 }
1377
1378 bool IsWeakReferenceClass() const {
1379 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1380 }
1381
1382 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001383 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001384 }
1385
1386 bool IsFinalizerReferenceClass() const {
1387 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1388 }
1389
1390 bool IsPhantomReferenceClass() const {
1391 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1392 }
1393
1394 PrimitiveType GetPrimitiveType() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001395 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001396 return static_cast<PrimitiveType>(
1397 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1398 }
1399
1400 void SetPrimitiveType(PrimitiveType new_type) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001401 DCHECK_EQ(sizeof(PrimitiveType), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001402 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001403 }
1404
1405 // Returns true if the class is a primitive type.
1406 bool IsPrimitive() const {
1407 return GetPrimitiveType() != kPrimNot;
1408 }
1409
1410 bool IsPrimitiveBoolean() const {
1411 return GetPrimitiveType() == kPrimBoolean;
1412 }
1413
1414 bool IsPrimitiveByte() const {
1415 return GetPrimitiveType() == kPrimByte;
1416 }
1417
1418 bool IsPrimitiveChar() const {
1419 return GetPrimitiveType() == kPrimChar;
1420 }
1421
1422 bool IsPrimitiveShort() const {
1423 return GetPrimitiveType() == kPrimShort;
1424 }
1425
1426 bool IsPrimitiveInt() const {
1427 return GetPrimitiveType() == kPrimInt;
1428 }
1429
1430 bool IsPrimitiveLong() const {
1431 return GetPrimitiveType() == kPrimLong;
1432 }
1433
1434 bool IsPrimitiveFloat() const {
1435 return GetPrimitiveType() == kPrimFloat;
1436 }
1437
1438 bool IsPrimitiveDouble() const {
1439 return GetPrimitiveType() == kPrimDouble;
1440 }
1441
1442 bool IsPrimitiveVoid() const {
1443 return GetPrimitiveType() == kPrimVoid;
1444 }
1445
1446 size_t PrimitiveSize() const;
1447
1448 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001449 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001450 }
1451
1452 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001453 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001454 }
1455
1456 void SetComponentType(Class* new_component_type) {
1457 DCHECK(GetComponentType() == NULL);
1458 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001459 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001460 }
1461
1462 size_t GetComponentSize() const {
1463 return GetTypeSize(GetComponentType()->GetDescriptor());
1464 }
1465
1466 bool IsObjectClass() const {
1467 return !IsPrimitive() && GetSuperClass() == NULL;
1468 }
1469
Brian Carlstrom1f870082011-08-23 16:02:11 -07001470 // Creates a raw object instance but does not invoke the default constructor.
1471 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001472
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001473 static size_t GetTypeSize(const String* descriptor);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001474
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001475 const String* GetDescriptor() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 const String* result = GetFieldObject<const String*>(
1477 OFFSET_OF_OBJECT_MEMBER(Class, descriptor_), false);
1478 // DCHECK(result != NULL); // may be NULL prior to class linker initialization
1479 // DCHECK_NE(0, result->GetLength()); // TODO: keep?
1480 return result;
1481 }
1482
1483 void SetDescriptor(String* new_descriptor);
1484
1485 bool IsVariableSize() const {
1486 // Classes and arrays vary in size, and so the object_size_ field cannot
1487 // be used to get their instance size
1488 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001489 }
1490
Brian Carlstrom4873d462011-08-21 15:23:39 -07001491 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001492 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001493 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001494 }
1495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001497 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001498 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001499 }
1500
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001501 void SetClassSize(size_t new_class_size) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001502 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughes54e7df12011-09-16 11:47:04 -07001503 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504 }
1505
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001506 size_t GetObjectSize() const {
1507 CHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001508 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001509 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001510 CHECK_GE(result, sizeof(Object));
1511 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001512 }
1513
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001514 void SetObjectSize(size_t new_object_size) {
1515 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001516 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001517 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001518 }
1519
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001520 // Returns true if this class is in the same packages as that class.
1521 bool IsInSamePackage(const Class* that) const;
1522
Brian Carlstrome24fa612011-09-29 00:53:55 -07001523 static bool IsInSamePackage(const String* descriptor1, const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001524
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001525 // Returns true if this class can access that class.
1526 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001528 }
1529
jeffhao4a801a42011-09-23 13:53:40 -07001530 // Validate method/field access.
1531 bool CanAccessMember(const Class* access_to, uint32_t member_flags) const {
1532 // quick accept for public access
1533 if (member_flags & kAccPublic) {
1534 return true;
1535 }
1536
1537 // quick accept for access from same class
1538 if (this == access_to) {
1539 return true;
1540 }
1541
1542 // quick reject for private access from another class
1543 if (member_flags & kAccPrivate) {
1544 return false;
1545 }
1546
1547 // Semi-quick test for protected access from a sub-class, which may or
1548 // may not be in the same package.
1549 if (member_flags & kAccProtected) {
1550 if (this->IsSubClass(access_to)) {
1551 return true;
1552 }
1553 }
1554
1555 // Allow protected and private access from other classes in the same package.
1556 return this->IsInSamePackage(access_to);
1557 }
1558
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001559 bool IsSubClass(const Class* klass) const;
1560
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001561 bool IsAssignableFrom(const Class* src) const {
1562 DCHECK(src != NULL);
1563 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001564 // Can always assign to things of the same type
1565 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001566 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001568 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001569 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001570 return src->Implements(this);
1571 } else if (src->IsArrayClass()) {
1572 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001573 } else {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001574 return src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001575 }
1576 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001577
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001578 Class* GetSuperClass() const {
1579 // Can only get super class for loaded classes (hack for when runtime is
1580 // initializing)
Elliott Hughesdcc24742011-09-07 14:02:44 -07001581 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001582 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001583 }
1584
buzbee4a3164f2011-09-03 11:25:10 -07001585 static MemberOffset SuperClassOffset() {
1586 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
1587 }
1588
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001589 void SetSuperClass(Class *new_super_class) {
1590 // super class is assigned once, except during class linker initialization
1591 Class* old_super_class = GetFieldObject<Class*>(
1592 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1593 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1594 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001595 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001596 }
1597
1598 bool HasSuperClass() const {
1599 return GetSuperClass() != NULL;
1600 }
1601
1602 uint32_t GetSuperClassTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001603 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001604 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_),
1605 false);
1606 }
1607
1608 void SetSuperClassTypeIdx(int32_t new_super_class_idx) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001609 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, super_class_type_idx_), new_super_class_idx, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001610 }
1611
1612 const ClassLoader* GetClassLoader() const;
1613
1614 void SetClassLoader(const ClassLoader* new_cl);
1615
1616 static MemberOffset DexCacheOffset() {
1617 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1618 }
1619
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001620 enum {
1621 kDumpClassFullDetail = 1,
1622 kDumpClassClassLoader = (1 << 1),
1623 kDumpClassInitialized = (1 << 2),
1624 };
1625
Elliott Hughes4681c802011-09-25 18:04:37 -07001626 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001627
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001628 DexCache* GetDexCache() const;
1629
1630 void SetDexCache(DexCache* new_dex_cache);
1631
1632 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001633 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001634 return GetFieldObject<ObjectArray<Method>*>(
1635 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1636 }
1637
1638 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1639 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1640 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1641 DCHECK_NE(0, new_direct_methods->GetLength());
1642 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1643 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001644 }
1645
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001646 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001647 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001648 }
1649
1650 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001651 ObjectArray<Method>* direct_methods =
1652 GetFieldObject<ObjectArray<Method>*>(
1653 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1654 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001655 }
1656
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001657 // Returns the number of static, private, and constructor methods.
1658 size_t NumDirectMethods() const {
1659 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1660 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001661
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001662 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001663 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001664 return GetFieldObject<ObjectArray<Method>*>(
1665 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1666 }
1667
1668 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1669 // TODO: we reassign virtual methods to grow the table for miranda
1670 // methods.. they should really just be assigned once
1671 DCHECK_NE(0, new_virtual_methods->GetLength());
1672 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1673 new_virtual_methods, false);
1674 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001675
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001676 // Returns the number of non-inherited virtual methods.
1677 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001678 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001679 }
1680
1681 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001682 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001683 return GetVirtualMethods()->Get(i);
1684 }
1685
1686 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001687 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001688 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001689 }
1690
1691 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 ObjectArray<Method>* virtual_methods =
1693 GetFieldObject<ObjectArray<Method>*>(
1694 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1695 virtual_methods->Set(i, f);
1696 }
1697
1698 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001699 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001700 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001701 }
1702
1703 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001704 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001705 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001706 }
1707
1708 void SetVTable(ObjectArray<Method>* new_vtable) {
1709 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1710 }
1711
1712 static MemberOffset VTableOffset() {
1713 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001714 }
1715
Brian Carlstrom30b94452011-08-25 21:35:26 -07001716 // Given a method implemented by this class but potentially from a
1717 // super class, return the specific implementation
1718 // method for this class.
1719 Method* FindVirtualMethodForVirtual(Method* method) {
1720 DCHECK(!method->GetDeclaringClass()->IsInterface());
1721 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001722 // Use the index to a potentially overridden one for this instance's class.
1723 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001724 }
1725
1726 // Given a method implemented by this class, but potentially from a
1727 // super class or interface, return the specific implementation
1728 // method for this class.
Ian Rogersb04f69f2011-10-17 00:40:54 -07001729 Method* FindVirtualMethodForInterface(Method* method, bool can_throw);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001730
Ian Rogers466bb252011-10-14 03:29:56 -07001731 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
1732 Method* FindInterfaceMethod(String* name, String* descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001733
Brian Carlstrom30b94452011-08-25 21:35:26 -07001734 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001735 if (method->IsDirect()) {
1736 return method;
1737 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001738 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersb04f69f2011-10-17 00:40:54 -07001739 return FindVirtualMethodForInterface(method, true);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001740 }
1741 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001742 }
1743
Ian Rogers466bb252011-10-14 03:29:56 -07001744 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
1745 Method* FindDeclaredVirtualMethod(String* name, String* signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001746
Ian Rogers466bb252011-10-14 03:29:56 -07001747 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
1748 Method* FindVirtualMethod(String* name, String* descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001749
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750 Method* FindDeclaredDirectMethod(const StringPiece& name,
1751 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001752
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001753 Method* FindDirectMethod(const StringPiece& name,
1754 const StringPiece& signature);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001755
Carl Shapiro69759ea2011-07-21 18:13:35 -07001756 size_t NumInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001757 CHECK(IsIdxLoaded() || IsErroneous()); // used during loading
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 ObjectArray<Class>* interfaces = GetFieldObject<ObjectArray<Class>*>(
1759 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1760 return (interfaces != NULL) ? interfaces->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001761 }
1762
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001763 IntArray* GetInterfacesTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001764 CHECK(IsIdxLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001765 return GetFieldObject<IntArray*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001766 }
1767
1768 void SetInterfacesTypeIdx(IntArray* new_interfaces_idx);
1769
1770 ObjectArray<Class>* GetInterfaces() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001771 CHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001772 return GetFieldObject<ObjectArray<Class>*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001773 }
1774
1775 void SetInterfaces(ObjectArray<Class>* new_interfaces) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001776 DCHECK(NULL == GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001777 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), new_interfaces, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001778 }
1779
1780 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Elliott Hughes418d20f2011-09-22 14:00:39 -07001781 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001782 ObjectArray<Class>* interfaces =
1783 GetFieldObject<ObjectArray<Class>*>(
1784 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_), false);
1785 interfaces->Set(i, f);
1786 }
1787
1788 Class* GetInterface(uint32_t i) const {
Elliott Hughes418d20f2011-09-22 14:00:39 -07001789 DCHECK_LT(i, NumInterfaces());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001790 return GetInterfaces()->Get(i);
1791 }
1792
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001793 int32_t GetIfTableCount() const {
1794 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1795 if (iftable == NULL) {
1796 return 0;
1797 }
1798 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001799 }
1800
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001801 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001802 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001803 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1805 }
1806
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001807 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001808 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001809 }
1810
1811 // Get instance fields
1812 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001813 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001814 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001815 }
1816
1817 void SetIFields(ObjectArray<Field>* new_ifields) {
1818 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1819 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001820 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001821 }
1822
1823 size_t NumInstanceFields() const {
1824 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1825 }
1826
1827 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1828 DCHECK_NE(NumInstanceFields(), 0U);
1829 return GetIFields()->Get(i);
1830 }
1831
1832 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1833 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1834 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1835 ifields->Set(i, f);
1836 }
1837
1838 // Returns the number of instance fields containing reference types.
1839 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001840 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001841 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001842 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001843 }
1844
1845 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001846 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001847 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001848 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001849 }
1850
1851 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001852 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001853 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001854 }
1855
1856 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001857 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001858 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001859 }
1860
1861 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1862
1863 // Beginning of static field data
1864 static MemberOffset FieldsOffset() {
1865 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1866 }
1867
1868 // Returns the number of static fields containing reference types.
1869 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001870 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001871 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001872 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001873 }
1874
1875 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001876 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001877 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001878 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001879 }
1880
1881 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001882 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001883 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001884 }
1885
1886 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001887 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001888 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001889 }
1890
1891 void SetSFields(ObjectArray<Field>* new_sfields) {
1892 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1893 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001894 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001895 }
1896
1897 size_t NumStaticFields() const {
1898 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1899 }
1900
1901 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1902 return GetSFields()->Get(i);
1903 }
1904
1905 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1906 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1907 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1908 sfields->Set(i, f);
1909 }
1910
1911 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001912 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001913 }
1914
1915 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1916
1917 // Finds the given instance field in this class or a superclass.
1918 Field* FindInstanceField(const StringPiece& name, Class* type);
1919
1920 Field* FindDeclaredInstanceField(const StringPiece& name, Class* type);
1921
1922 // Finds the given static field in this class or a superclass.
1923 Field* FindStaticField(const StringPiece& name, Class* type);
1924
1925 Field* FindDeclaredStaticField(const StringPiece& name, Class* type);
1926
Elliott Hughesdcc24742011-09-07 14:02:44 -07001927 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001928 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001929 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1930 }
1931
Elliott Hughesdcc24742011-09-07 14:02:44 -07001932 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001933 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001934 }
1935
1936 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001937 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001938 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001939 }
1940
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001941 void SetVerifyErrorClass(Class* klass) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001942 klass->SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001943 }
1944
Brian Carlstromc74255f2011-09-11 22:47:39 -07001945 String* GetSourceFile() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001946
Brian Carlstromc74255f2011-09-11 22:47:39 -07001947 void SetSourceFile(String* new_source_file);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001948
jeffhaobdb76512011-09-07 11:43:16 -07001949 private:
jeffhao5dbddee2011-09-07 16:38:26 -07001950 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001951 bool IsArrayAssignableFromArray(const Class* klass) const;
1952 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001953
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001954 // descriptor for the class such as "java.lang.Class" or "[C"
1955 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001956
Brian Carlstrom693267a2011-09-06 09:25:34 -07001957 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstromdbc05252011-09-09 01:59:59 -07001958 const ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001959
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001960 // For array classes, the component class object for instanceof/checkcast
1961 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001962 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001963
Brian Carlstrom693267a2011-09-06 09:25:34 -07001964 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1965 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001966
Brian Carlstrom693267a2011-09-06 09:25:34 -07001967 // DexCache of resolved constant pool entries
1968 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
1969 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001970
1971 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001972 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001973
Brian Carlstrom693267a2011-09-06 09:25:34 -07001974 // instance fields
1975 //
1976 // These describe the layout of the contents of an Object.
1977 // Note that only the fields directly declared by this class are
1978 // listed in ifields; fields declared by a superclass are listed in
1979 // the superclass's Class.ifields.
1980 //
1981 // All instance fields that refer to objects are guaranteed to be at
1982 // the beginning of the field list. num_reference_instance_fields_
1983 // specifies the number of reference fields.
1984 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001985
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001986 // Interface table (iftable_), one entry per interface supported by
1987 // this class. That means one entry for each interface we support
1988 // directly, indirectly via superclass, or indirectly via
1989 // superinterface. This will be null if neither we nor our
1990 // superclass implement any interfaces.
1991 //
1992 // Why we need this: given "class Foo implements Face", declare
1993 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1994 // is part of the Face interface. We can't easily use a single
1995 // vtable.
1996 //
1997 // For every interface a concrete class implements, we create an array
1998 // of the concrete vtable_ methods for the methods in the interface.
1999 ObjectArray<InterfaceEntry>* iftable_;
2000
Brian Carlstromdbc05252011-09-09 01:59:59 -07002001 // array of interfaces this class implements directly
2002 // see also interfaces_type_idx_
2003 ObjectArray<Class>* interfaces_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002004
2005 // array of type_idx's for interfaces this class implements directly
2006 // see also interfaces_
2007 IntArray* interfaces_type_idx_;
2008
Brian Carlstromdbc05252011-09-09 01:59:59 -07002009 // Static fields
2010 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002011
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002012 // source file name, if known. Otherwise, NULL.
2013 String* source_file_;
2014
Brian Carlstromdbc05252011-09-09 01:59:59 -07002015 // The superclass, or NULL if this is java.lang.Object or a
2016 // primitive type.
2017 // see also super_class_type_idx_;
2018 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002019
Brian Carlstromdbc05252011-09-09 01:59:59 -07002020 // If class verify fails, we must return same error on subsequent tries.
2021 // Update with SetVerifyErrorClass to ensure a write barrier is used.
2022 const Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002023
Brian Carlstromdbc05252011-09-09 01:59:59 -07002024 // virtual methods defined in this class; invoked through vtable
2025 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002026
Brian Carlstromdbc05252011-09-09 01:59:59 -07002027 // Virtual method table (vtable), for use by "invoke-virtual". The
2028 // vtable from the superclass is copied in, and virtual methods from
2029 // our class either replace those from the super or are appended.
2030 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002031
Brian Carlstromdbc05252011-09-09 01:59:59 -07002032 // access flags; low 16 bits are defined by VM spec
2033 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002034
Jesse Wilson6bf19152011-09-29 13:12:33 -04002035 // Total size of the Class instance; used when allocating storage on gc heap.
2036 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002037 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002038
Elliott Hughes5f791332011-09-15 17:45:30 -07002039 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07002040 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07002041
Brian Carlstromdbc05252011-09-09 01:59:59 -07002042 // number of instance fields that are object refs
2043 size_t num_reference_instance_fields_;
2044
2045 // number of static fields that are object refs
2046 size_t num_reference_static_fields_;
2047
2048 // Total object size; used when allocating storage on gc heap.
2049 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04002050 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07002051 size_t object_size_;
2052
2053 // primitive type index, or kPrimNot (0); set for generated prim classes
2054 PrimitiveType primitive_type_;
2055
2056 // Bitmap of offsets of ifields.
2057 uint32_t reference_instance_offsets_;
2058
2059 // Bitmap of offsets of sfields.
2060 uint32_t reference_static_offsets_;
2061
Brian Carlstrom693267a2011-09-06 09:25:34 -07002062 // state of class initialization
2063 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04002064
Brian Carlstrom693267a2011-09-06 09:25:34 -07002065 // Set in LoadClass, used to LinkClass
2066 // see also super_class_
2067 uint32_t super_class_type_idx_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002068
Brian Carlstrom693267a2011-09-06 09:25:34 -07002069 // TODO: ?
2070 // initiating class loader list
2071 // NOTE: for classes with low serialNumber, these are unused, and the
2072 // values are kept in a table in gDvm.
2073 // InitiatingLoaderList initiating_loader_list_;
2074
Brian Carlstrom4873d462011-08-21 15:23:39 -07002075 // Location of first static field.
2076 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002077
Brian Carlstrom693267a2011-09-06 09:25:34 -07002078 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07002079 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002080};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002081
Elliott Hughes1f359b02011-07-17 14:27:17 -07002082std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002083
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002084inline void Object::SetClass(Class* new_klass) {
2085 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07002086 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002087}
2088
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002089inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04002090 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 DCHECK(GetClass() != NULL);
2092 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002093}
2094
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002095inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002096 Class* java_lang_Class = GetClass()->GetClass();
2097 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002098}
2099
Brian Carlstrom4873d462011-08-21 15:23:39 -07002100inline bool Object::IsClassClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002101 Class* java_lang_Class = GetClass()->GetClass();
Brian Carlstrom4873d462011-08-21 15:23:39 -07002102 return this == java_lang_Class;
2103}
2104
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002105inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002106 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002107}
2108
Brian Carlstrom34f426c2011-10-04 12:58:02 -07002109template<class T>
2110inline ObjectArray<T>* Object::AsObjectArray() {
2111 DCHECK(IsObjectArray());
2112 return down_cast<ObjectArray<T>*>(this);
2113}
2114
2115template<class T>
2116inline const ObjectArray<T>* Object::AsObjectArray() const {
2117 DCHECK(IsObjectArray());
2118 return down_cast<const ObjectArray<T>*>(this);
2119}
2120
Brian Carlstromb63ec392011-08-27 17:38:27 -07002121inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002122 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002123}
2124
Brian Carlstroma663ea52011-08-19 23:33:41 -07002125inline bool Object::IsField() const {
2126 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002127 Class* java_lang_reflect_Field =
2128 java_lang_Class->GetInstanceField(0)->GetClass();
2129 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002130}
2131
2132inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002133 Class* c = GetClass();
2134 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002135}
2136
2137inline bool Object::IsReferenceInstance() const {
2138 return GetClass()->IsReferenceClass();
2139}
2140
2141inline bool Object::IsWeakReferenceInstance() const {
2142 return GetClass()->IsWeakReferenceClass();
2143}
2144
2145inline bool Object::IsSoftReferenceInstance() const {
2146 return GetClass()->IsSoftReferenceClass();
2147}
2148
2149inline bool Object::IsFinalizerReferenceInstance() const {
2150 return GetClass()->IsFinalizerReferenceClass();
2151}
2152
2153inline bool Object::IsPhantomReferenceInstance() const {
2154 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002155}
2156
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002157inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002158 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002159 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002160 result = AsArray()->SizeOf();
2161 } else if (IsClass()) {
2162 result = AsClass()->SizeOf();
2163 } else {
2164 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002165 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002166 DCHECK(!IsField() || result == sizeof(Field));
2167 DCHECK(!IsMethod() || result == sizeof(Method));
2168 return result;
2169}
2170
2171inline void Field::SetOffset(MemberOffset num_bytes) {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002172 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Brian Carlstrom693267a2011-09-06 09:25:34 -07002173 Class* type = GetTypeDuringLinking();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002174 if (type != NULL && (type->IsPrimitiveDouble() || type->IsPrimitiveLong())) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002175 DCHECK_ALIGNED(num_bytes.Uint32Value(), 8);
Brian Carlstrom4873d462011-08-21 15:23:39 -07002176 }
Elliott Hughes06b37d92011-10-16 11:51:29 -07002177 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), num_bytes.Uint32Value(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002178}
2179
2180inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002181 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002182 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002183 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002184 return result;
2185}
2186
2187inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002188 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002189}
2190
2191inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002192 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002193 DCHECK(result != NULL);
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002194 DCHECK(result->IsIdxLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002195 return result;
2196}
2197
2198inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002199 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002200}
2201
2202inline uint32_t Method::GetReturnTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002203 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes06b37d92011-10-16 11:51:29 -07002204 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, java_return_type_idx_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002205}
2206
2207inline bool Method::IsReturnAReference() const {
2208 return !GetReturnType()->IsPrimitive();
2209}
2210
2211inline bool Method::IsReturnAFloat() const {
2212 return GetReturnType()->IsPrimitiveFloat();
2213}
2214
2215inline bool Method::IsReturnADouble() const {
2216 return GetReturnType()->IsPrimitiveDouble();
2217}
2218
2219inline bool Method::IsReturnALong() const {
2220 return GetReturnType()->IsPrimitiveLong();
2221}
2222
2223inline bool Method::IsReturnVoid() const {
2224 return GetReturnType()->IsPrimitiveVoid();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002225}
2226
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002227inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002228 // This is safe from overflow because the array was already allocated, so we know it's sane.
2229 return sizeof(Array) + GetLength() * GetClass()->GetComponentSize();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002230}
2231
2232template<class T>
2233void ObjectArray<T>::Set(int32_t i, T* object) {
2234 if (IsValidIndex(i)) {
2235 if (object != NULL) {
2236 Class* element_class = GetClass()->GetComponentType();
Elliott Hughes80609252011-09-23 17:24:51 -07002237 if (!object->InstanceOf(element_class)) {
2238 ThrowArrayStoreException(object);
2239 return;
2240 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002241 }
2242 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2243 SetFieldObject(data_offset, object, false);
2244 }
2245}
2246
2247template<class T>
2248void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2249 DCHECK(IsValidIndex(i));
2250 MemberOffset data_offset(DataOffset().Int32Value() + i * sizeof(Object*));
2251 SetFieldObject(data_offset, object, false);
2252}
2253
2254template<class T>
2255void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2256 ObjectArray<T>* dst, int dst_pos,
2257 size_t length) {
2258 if (src->IsValidIndex(src_pos) &&
2259 src->IsValidIndex(src_pos+length-1) &&
2260 dst->IsValidIndex(dst_pos) &&
2261 dst->IsValidIndex(dst_pos+length-1)) {
2262 MemberOffset src_offset(DataOffset().Int32Value() +
2263 src_pos * sizeof(Object*));
2264 MemberOffset dst_offset(DataOffset().Int32Value() +
2265 dst_pos * sizeof(Object*));
2266 Class* array_class = dst->GetClass();
2267 if (array_class == src->GetClass()) {
2268 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002269 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002270 Object* object = src->GetFieldObject<Object*>(src_offset, false);
2271 dst->SetFieldObject(dst_offset, object, false);
2272 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2273 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2274 }
2275 } else {
2276 Class* element_class = array_class->GetComponentType();
2277 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002278 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002279 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002280 if (object != NULL && !object->InstanceOf(element_class)) {
2281 dst->ThrowArrayStoreException(object);
2282 return;
2283 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002284 dst->SetFieldObject(dst_offset, object, false);
2285 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2286 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2287 }
2288 }
Elliott Hughes3a4f8df2011-09-13 15:22:36 -07002289 Heap::WriteBarrier(dst);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002290 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002291}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002292
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002293class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002294 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002295 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002296 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002297 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002298 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2299};
2300
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002301class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002302 private:
2303 CharArray* ASCII_;
2304 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002305 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002306 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002307 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002308 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2309};
2310
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002311class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002312 private:
2313 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
2314 uint32_t TYPE_BOOLEAN_;
2315 uint32_t TYPE_BYTE_;
2316 uint32_t TYPE_CHAR_;
2317 uint32_t TYPE_DOUBLE_;
2318 uint32_t TYPE_FLOAT_;
2319 uint32_t TYPE_INTEGER_;
2320 uint32_t TYPE_LONG_;
2321 uint32_t TYPE_SHORT_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002322 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002323 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2324};
2325
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002326class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002327 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002328 Object* ORDER_BY_SIGNATURE_;
2329 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002330 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2331};
2332
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002333template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002334class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002335 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002336 typedef T ElementType;
2337
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002338 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002339
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002340 const T* GetData() const {
2341 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002342 }
2343
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002344 T* GetData() {
2345 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002346 }
2347
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002348 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002349 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002350 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002351 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002352 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002353 }
2354
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002355 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002356 // TODO: ArrayStoreException
2357 if (IsValidIndex(i)) {
2358 GetData()[i] = value;
2359 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002360 }
2361
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002362 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002363 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002364 CHECK(array_class != NULL);
2365 array_class_ = array_class;
2366 }
2367
Brian Carlstroma663ea52011-08-19 23:33:41 -07002368 static void ResetArrayClass() {
2369 CHECK(array_class_ != NULL);
2370 array_class_ = NULL;
2371 }
2372
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002373 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002374 // Location of first element.
2375 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07002376
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002377 static Class* array_class_;
2378
Carl Shapirof88c9522011-08-06 15:47:38 -07002379 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002380};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002381
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002382inline void Class::SetInterfacesTypeIdx(IntArray* new_interfaces_idx) {
2383 DCHECK(NULL == GetFieldObject<IntArray*>(
2384 OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_), false));
2385 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, interfaces_type_idx_),
2386 new_interfaces_idx, false);
2387}
2388
2389// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002390class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002391 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002392 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002393 const CharArray* result = GetFieldObject<const CharArray*>(
2394 OFFSET_OF_OBJECT_MEMBER(String, array_), false);
2395 DCHECK(result != NULL);
2396 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002397 }
2398
Elliott Hughes814e4032011-08-23 12:07:56 -07002399 int32_t GetOffset() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002400 int32_t result = GetField32(
2401 OFFSET_OF_OBJECT_MEMBER(String, offset_), false);
2402 DCHECK_LE(0, result);
2403 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002404 }
2405
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002406 int32_t GetLength() const;
2407
Brian Carlstrom395520e2011-09-25 19:35:00 -07002408 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002409
2410 void ComputeHashCode() {
2411 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002412 }
2413
Elliott Hughes814e4032011-08-23 12:07:56 -07002414 int32_t GetUtfLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002415 return CountUtf8Bytes(GetCharArray()->GetData(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002416 }
2417
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002418 uint16_t CharAt(int32_t index) const;
2419
Brian Carlstromc74255f2011-09-11 22:47:39 -07002420 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002421
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002422 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002423 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002424 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002425
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002426 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002427
Jesse Wilson8989d992011-08-02 13:39:42 -07002428 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002429 const char* utf8_data_in);
2430
2431 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2432
2433 static String* Alloc(Class* java_lang_String, CharArray* array);
2434
2435 bool Equals(const char* modified_utf8) const;
2436
2437 // TODO: do we need this overload? give it a more intention-revealing name.
2438 bool Equals(const StringPiece& modified_utf8) const;
2439
2440 bool Equals(const String* that) const;
2441
2442 // TODO: do we need this overload? give it a more intention-revealing name.
2443 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2444 int32_t that_length) const;
2445
2446 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2447 std::string ToModifiedUtf8() const;
2448
2449 static Class* GetJavaLangString() {
2450 DCHECK(java_lang_String_ != NULL);
2451 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002452 }
2453
Brian Carlstroma663ea52011-08-19 23:33:41 -07002454 static void SetClass(Class* java_lang_String);
2455 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002456
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002457 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002458 void SetHashCode(int32_t new_hash_code) {
2459 DCHECK_EQ(0u,
2460 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2461 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2462 new_hash_code, false);
2463 }
2464
2465 void SetCount(int32_t new_count) {
2466 DCHECK_LE(0, new_count);
2467 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2468 }
2469
2470 void SetOffset(int32_t new_offset) {
2471 DCHECK_LE(0, new_offset);
2472 DCHECK_GE(GetLength(), new_offset);
2473 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2474 }
2475
2476 void SetArray(CharArray* new_array) {
2477 DCHECK(new_array != NULL);
2478 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2479 }
2480
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002481 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2482 CharArray* array_;
2483
Brian Carlstromdbc05252011-09-09 01:59:59 -07002484 int32_t count_;
2485
Carl Shapirof88c9522011-08-06 15:47:38 -07002486 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002487
Elliott Hughes289da822011-08-16 10:11:20 -07002488 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002489
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002490 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002491
Brian Carlstrom693267a2011-09-06 09:25:34 -07002492 friend struct StringOffsets; // for verifying offset information
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002493 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002494};
2495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002496inline const String* Field::GetName() const {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002497 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
2498 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Field, name_), false);
2499 DCHECK(result != NULL);
2500 return result;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002501}
2502
2503inline void Field::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002504 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002505}
2506
2507inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002508 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002509 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2510}
2511
2512inline uint32_t Field::GetTypeIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002513 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002514 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, type_idx_), false);
2515}
2516
2517inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002518 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002519 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002520}
2521
2522inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002523 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002524 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002525}
2526
Ian Rogers466bb252011-10-14 03:29:56 -07002527inline String* Method::GetName() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002528 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002529 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002530 DCHECK(result != NULL);
2531 return result;
2532}
2533
2534inline void Method::SetName(String* new_name) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002535 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, name_), new_name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002536}
2537
jeffhaoe23d93c2011-09-15 14:48:43 -07002538inline ByteArray* Method::GetRegisterMapData() const {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002539 return GetFieldObject<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002540}
2541
jeffhaoa0a764a2011-09-16 10:43:38 -07002542inline void Method::SetRegisterMapData(ByteArray* data) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002543 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_data_), data, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002544}
2545
2546inline ByteArray* Method::GetRegisterMapHeader() const {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002547 return GetFieldObject<ByteArray*>(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002548}
2549
jeffhaoa0a764a2011-09-16 10:43:38 -07002550inline void Method::SetRegisterMapHeader(ByteArray* header) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002551 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, register_map_header_), header, false);
jeffhaoe23d93c2011-09-15 14:48:43 -07002552}
2553
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002554inline String* Method::GetShorty() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002555 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002556 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002557}
2558
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002559inline void Method::SetShorty(String* new_shorty) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002560 DCHECK(NULL == GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), false));
Brian Carlstrom2ed67392011-09-09 14:53:28 -07002561 DCHECK_LE(1, new_shorty->GetLength());
2562 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, shorty_), new_shorty, false);
2563}
2564
Ian Rogers466bb252011-10-14 03:29:56 -07002565inline String* Method::GetSignature() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002566 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers466bb252011-10-14 03:29:56 -07002567 String* result = GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Method, signature_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002568 DCHECK(result != NULL);
2569 return result;
2570}
2571
2572inline void Method::SetSignature(String* new_signature) {
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002573 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, signature_), new_signature, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002574}
2575
Ian Rogers466bb252011-10-14 03:29:56 -07002576inline void Method::SetExceptionTypes(ObjectArray<Class>* exception_types) {
2577 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, java_exception_types_), exception_types, false);
2578}
2579
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002580inline uint32_t Class::GetAccessFlags() const {
2581 // Check class is loaded or this is java.lang.String that has a
2582 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002583 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002584 this == String::GetJavaLangString() ||
2585 this == Field::GetJavaLangReflectField() ||
2586 this == Method::GetConstructorClass() ||
2587 this == Method::GetMethodClass()) << PrettyClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002588 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2589}
2590
2591inline void Class::SetDescriptor(String* new_descriptor) {
Brian Carlstrom693267a2011-09-06 09:25:34 -07002592 DCHECK(GetDescriptor() == NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002593 DCHECK(new_descriptor != NULL);
2594 DCHECK_NE(0, new_descriptor->GetLength());
2595 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, descriptor_),
2596 new_descriptor, false);
2597}
2598
Brian Carlstromc74255f2011-09-11 22:47:39 -07002599inline String* Class::GetSourceFile() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002600 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstromc74255f2011-09-11 22:47:39 -07002601 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), false);
2602}
2603
2604inline void Class::SetSourceFile(String* new_source_file) {
2605 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, source_file_), new_source_file, false);
2606}
2607
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002608inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002609 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002610 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2611}
2612
2613inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002614 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002615 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002616}
2617
2618inline uint16_t Method::NumRegisters() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002619 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002620 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_registers_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002621}
2622
2623inline uint16_t Method::NumIns() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002624 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002625 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_ins_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002626}
2627
2628inline uint16_t Method::NumOuts() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002629 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002630 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, num_outs_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002631}
2632
2633inline uint32_t Method::GetProtoIdx() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002634 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002635 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, proto_idx_), false);
2636}
2637
2638// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002639class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002640 public:
2641 void SetDetailMessage(String* new_detail_message) {
2642 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2643 new_detail_message, false);
2644 }
2645
Ian Rogers466bb252011-10-14 03:29:56 -07002646 bool IsCheckedException() const;
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002647 private:
2648 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2649 Throwable* cause_;
2650 String* detail_message_;
2651 Object* stack_state_; // Note this is Java volatile:
2652 Object* stack_trace_;
2653 Object* suppressed_exceptions_;
2654
Brian Carlstrom693267a2011-09-06 09:25:34 -07002655 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002656 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2657};
2658
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002659// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002660class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002661 public:
2662 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002663 return GetFieldObject<const String*>(
2664 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002665 }
2666
2667 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002668 return GetFieldObject<const String*>(
2669 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002670 }
2671
2672 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002673 return GetFieldObject<const String*>(
2674 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002675 }
2676
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002677 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002678 return GetField32(
2679 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002680 }
2681
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002682 static StackTraceElement* Alloc(const String* declaring_class,
2683 const String* method_name,
2684 const String* file_name,
2685 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002686
2687 static void SetClass(Class* java_lang_StackTraceElement);
2688
2689 static void ResetClass();
2690
2691 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002692 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002693 const String* declaring_class_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002694 const String* file_name_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002695 const String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002696 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002697
2698 static Class* GetStackTraceElement() {
2699 DCHECK(java_lang_StackTraceElement_ != NULL);
2700 return java_lang_StackTraceElement_;
2701 }
2702
2703 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002704
2705 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002706 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2707};
2708
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002709class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002710 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002711 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002712 Class* interface = Get(kInterface)->AsClass();
2713 DCHECK(interface != NULL);
2714 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002715 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002716
Brian Carlstrom30b94452011-08-25 21:35:26 -07002717 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002718 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002719 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002720 DCHECK(Get(kInterface) == NULL);
2721 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002722 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002723
Brian Carlstrom86927212011-09-15 11:31:11 -07002724 size_t GetMethodArrayCount() const {
2725 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2726 if (method_array == 0) {
2727 return 0;
2728 }
2729 return method_array->GetLength();
2730 }
2731
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002732 ObjectArray<Method>* GetMethodArray() const {
2733 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2734 DCHECK(method_array != NULL);
2735 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002736 }
2737
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002738 void SetMethodArray(ObjectArray<Method>* new_ma) {
2739 DCHECK(new_ma != NULL);
2740 DCHECK(Get(kMethodArray) == NULL);
2741 Set(kMethodArray, new_ma);
2742 }
2743
2744 static size_t LengthAsArray() {
2745 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002746 }
2747
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002748 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002749
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002750 enum ArrayIndex {
2751 // Points to the interface class.
2752 kInterface = 0,
2753 // Method pointers into the vtable, allow fast map from interface
2754 // method index to concrete instance method.
2755 kMethodArray = 1,
2756 kMax = 2,
2757 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002758
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002759 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002760};
2761
Jesse Wilson95caa792011-10-12 18:14:17 -04002762class MANAGED ProxyClass : public Class {
2763 private:
2764 int32_t NextClassNameIndex_;
2765 int64_t serialVersionUID_;
2766 friend struct ProxyClassOffsets; // for verifying offset information
2767 DISALLOW_IMPLICIT_CONSTRUCTORS(ProxyClass);
2768};
2769
2770class MANAGED Proxy : public Object {
2771 private:
2772 Object* h_;
2773
2774 friend struct ProxyOffsets; // for verifying offset information
2775 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2776};
2777
Carl Shapiro1fb86202011-06-27 17:43:13 -07002778} // namespace art
2779
2780#endif // ART_SRC_OBJECT_H_