blob: 0fc6acc78c80513db8d94299d31c16f0ae1d1460 [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"
26#include "globals.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070027#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070028#include "logging.h"
29#include "macros.h"
30#include "offsets.h"
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070031#include "primitive.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 {
Elliott Hughes1d878f32012-04-11 15:17:54 -070063 // We default initialize JValue instances to all-zeros.
64 JValue() : j(0) {}
65
Elliott Hughesf24d3ce2012-04-11 17:43:37 -070066 int8_t GetB() const { return b; }
67 void SetB(int8_t new_b) {
68 i = ((static_cast<int32_t>(new_b) << 24) >> 24); // Sign-extend.
69 }
70
71 uint16_t GetC() const { return c; }
72 void SetC(uint16_t new_c) { c = new_c; }
73
74 double GetD() const { return d; }
75 void SetD(double new_d) { d = new_d; }
76
77 float GetF() const { return f; }
78 void SetF(float new_f) { f = new_f; }
79
80 int32_t GetI() const { return i; }
81 void SetI(int32_t new_i) { i = new_i; }
82
83 int64_t GetJ() const { return j; }
84 void SetJ(int64_t new_j) { j = new_j; }
85
86 Object* GetL() const { return l; }
87 void SetL(Object* new_l) { l = new_l; }
88
89 int16_t GetS() const { return s; }
90 void SetS(int16_t new_s) {
91 i = ((static_cast<int32_t>(new_s) << 16) >> 16); // Sign-extend.
92 }
93
94 uint8_t GetZ() const { return z; }
95 void SetZ(uint8_t new_z) { z = new_z; }
96
97 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -070098 uint8_t z;
99 int8_t b;
100 uint16_t c;
101 int16_t s;
102 int32_t i;
103 int64_t j;
104 float f;
105 double d;
106 Object* l;
107};
108
Logan Chienfca7e872011-12-20 20:08:22 +0800109#if defined(ART_USE_LLVM_COMPILER)
110namespace compiler_llvm {
111 class InferredRegCategoryMap;
Elliott Hughes48257562012-06-06 17:42:44 -0700112} // namespace compiler_llvm
Logan Chienfca7e872011-12-20 20:08:22 +0800113#endif
114
Brian Carlstrombe977852011-07-19 14:54:54 -0700115static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
116static const uint32_t kAccPrivate = 0x0002; // field, method, ic
117static const uint32_t kAccProtected = 0x0004; // field, method, ic
118static const uint32_t kAccStatic = 0x0008; // field, method, ic
119static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
120static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700121static const uint32_t kAccSuper = 0x0020; // class (not used in dex)
Brian Carlstrombe977852011-07-19 14:54:54 -0700122static const uint32_t kAccVolatile = 0x0040; // field
123static const uint32_t kAccBridge = 0x0040; // method (1.5)
124static const uint32_t kAccTransient = 0x0080; // field
125static const uint32_t kAccVarargs = 0x0080; // method (1.5)
126static const uint32_t kAccNative = 0x0100; // method
127static const uint32_t kAccInterface = 0x0200; // class, ic
128static const uint32_t kAccAbstract = 0x0400; // class, method, ic
129static const uint32_t kAccStrict = 0x0800; // method
130static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
131static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
132static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700133
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134static const uint32_t kAccMiranda = 0x8000; // method
135
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700136static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
137
Elliott Hughes582a7d12011-10-10 18:38:42 -0700138static const uint32_t kAccConstructor = 0x00010000; // method (dex only)
139static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (dex only)
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140static const uint32_t kAccClassIsProxy = 0x00040000; // class (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700141
Elliott Hughes20cde902011-10-04 17:37:27 -0700142// Special runtime-only flags.
143// Note: if only kAccClassIsReference is set, we have a soft reference.
144static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
145static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
146static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
147static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
148static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700149
150static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
151 | kAccClassIsWeakReference
152 | kAccClassIsFinalizerReference
153 | kAccClassIsPhantomReference);
154
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700156 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 */
158/*
159 * A magic value for refOffsets. Ignore the bits and walk the super
160 * chain when this is the value.
161 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
162 * fields followed by 2 ref instance fields.]
163 */
164#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700165#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
166#define CLASS_OFFSET_ALIGNMENT 4
167#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
168/*
169 * Given an offset, return the bit number which would encode that offset.
170 * Local use only.
171 */
172#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700173 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700174 CLASS_OFFSET_ALIGNMENT)
175/*
176 * Is the given offset too large to be encoded?
177 */
178#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
179 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
180/*
181 * Return a single bit, encoding the offset.
182 * Undefined if the offset is too large, as defined above.
183 */
184#define CLASS_BIT_FROM_OFFSET(byteOffset) \
185 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
186/*
187 * Return an offset, given a bit number as returned from CLZ.
188 */
189#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700190 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700191
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192#define OFFSET_OF_OBJECT_MEMBER(type, field) \
193 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700195// Classes shared with the managed side of the world need to be packed
196// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700197#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700198
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700200class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700201 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 static MemberOffset ClassOffset() {
203 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700204 }
205
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700207 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700208 }
209
210 void SetClass(Class* new_klass);
211
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700212 bool InstanceOf(const Class* klass) const;
213
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700214 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700215
Elliott Hughes081be7f2011-09-18 16:50:26 -0700216 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700217
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 static MemberOffset MonitorOffset() {
219 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
220 }
221
Elliott Hughes5f791332011-09-15 17:45:30 -0700222 volatile int32_t* GetRawLockWordAddress() {
223 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
224 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
225 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700226 }
227
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700228 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700231
Ian Rogersff1ed472011-09-20 13:46:24 -0700232 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233
Elliott Hughes5f791332011-09-15 17:45:30 -0700234 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700235
Elliott Hughes5f791332011-09-15 17:45:30 -0700236 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237
Elliott Hughes5f791332011-09-15 17:45:30 -0700238 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239
Elliott Hughes5f791332011-09-15 17:45:30 -0700240 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700241
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700242 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700243
244 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700245 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 return down_cast<Class*>(this);
247 }
248
249 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700250 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700251 return down_cast<const Class*>(this);
252 }
253
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700254 bool IsObjectArray() const;
255
256 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700257 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700259 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700260 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700261
Brian Carlstromb63ec392011-08-27 17:38:27 -0700262 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700263
264 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700265 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700266 return down_cast<Array*>(this);
267 }
268
269 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700270 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700271 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 }
273
Elliott Hughesdbb40792011-11-18 17:05:22 -0800274 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700275
276 bool IsMethod() const;
277
278 Method* AsMethod() {
279 DCHECK(IsMethod());
280 return down_cast<Method*>(this);
281 }
282
Brian Carlstrom4873d462011-08-21 15:23:39 -0700283 const Method* AsMethod() const {
284 DCHECK(IsMethod());
285 return down_cast<const Method*>(this);
286 }
287
Brian Carlstroma663ea52011-08-19 23:33:41 -0700288 bool IsField() const;
289
290 Field* AsField() {
291 DCHECK(IsField());
292 return down_cast<Field*>(this);
293 }
294
Brian Carlstrom4873d462011-08-21 15:23:39 -0700295 const Field* AsField() const {
296 DCHECK(IsField());
297 return down_cast<const Field*>(this);
298 }
299
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 bool IsReferenceInstance() const;
301
302 bool IsWeakReferenceInstance() const;
303
304 bool IsSoftReferenceInstance() const;
305
306 bool IsFinalizerReferenceInstance() const;
307
308 bool IsPhantomReferenceInstance() const;
309
310 // Accessors for Java type fields
311 template<class T>
312 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700313 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
314 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800315 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 return result;
317 }
318
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700319 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800320 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700321 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
322 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800323 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700324 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 }
326
327 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800328 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700329 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
330 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700331 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700332 return android_atomic_acquire_load(word_addr);
333 } else {
334 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700335 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
337
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700338 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
339 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800340 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700341 }
342 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700343 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700344 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700345 /*
346 * TODO: add an android_atomic_synchronization_store() function and
347 * use it in the 32-bit volatile set handlers. On some platforms we
348 * can use a fast atomic instruction and avoid the barriers.
349 */
350 ANDROID_MEMBAR_STORE();
351 *word_addr = new_value;
352 ANDROID_MEMBAR_FULL();
353 } else {
354 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700355 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
357
358 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800359 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700360 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700361 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700362 if (UNLIKELY(is_volatile)) {
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700363 uint64_t result = QuasiAtomic::Read64(addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700364 ANDROID_MEMBAR_FULL();
365 return result;
366 } else {
367 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700368 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
370
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700371 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800372 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700373 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
374 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700375 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700376 ANDROID_MEMBAR_STORE();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700377 QuasiAtomic::Swap64(new_value, addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700378 // Post-store barrier not required due to use of atomic op or mutex.
379 } else {
380 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700381 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 }
383
384 protected:
385 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700386 template<class T>
387 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700388 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700389 }
390
391 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800392 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
393 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700394 }
395
396 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700397 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700398
Elliott Hughes5f791332011-09-15 17:45:30 -0700399 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700400
Elliott Hughes5f791332011-09-15 17:45:30 -0700401 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700402 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700403 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404};
405
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700406struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800407 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700408#ifdef MOVING_GARBAGE_COLLECTOR
409 // TODO: we'll need to use the Object's internal concept of identity
410 UNIMPLEMENTED(FATAL);
411#endif
412 return reinterpret_cast<size_t>(obj);
413 }
414};
415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700416// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500417class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700418 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700419 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700420
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421 void SetDeclaringClass(Class *new_declaring_class);
422
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700423 uint32_t GetAccessFlags() const;
424
425 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700426 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700427 }
428
Elliott Hughes80609252011-09-23 17:24:51 -0700429 bool IsPublic() const {
430 return (GetAccessFlags() & kAccPublic) != 0;
431 }
432
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700433 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435 }
436
jeffhaobdb76512011-09-07 11:43:16 -0700437 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700438 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700439 }
440
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800441 uint32_t GetDexFieldIndex() const {
442 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
443 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700444
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800445 void SetDexFieldIndex(uint32_t new_idx) {
446 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
447 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700448
449 // Offset to field within an Object
450 MemberOffset GetOffset() const;
451
buzbee34cd9e52011-09-08 14:31:52 -0700452 static MemberOffset OffsetOffset() {
453 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
454 }
455
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700456 MemberOffset GetOffsetDuringLinking() const;
457
458 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700459
Brian Carlstrom4873d462011-08-21 15:23:39 -0700460 // field access, null object for static fields
461 bool GetBoolean(const Object* object) const;
462 void SetBoolean(Object* object, bool z) const;
463 int8_t GetByte(const Object* object) const;
464 void SetByte(Object* object, int8_t b) const;
465 uint16_t GetChar(const Object* object) const;
466 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700467 int16_t GetShort(const Object* object) const;
468 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700469 int32_t GetInt(const Object* object) const;
470 void SetInt(Object* object, int32_t i) const;
471 int64_t GetLong(const Object* object) const;
472 void SetLong(Object* object, int64_t j) const;
473 float GetFloat(const Object* object) const;
474 void SetFloat(Object* object, float f) const;
475 double GetDouble(const Object* object) const;
476 void SetDouble(Object* object, double d) const;
477 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700478 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700479
Ian Rogersce9eca62011-10-07 17:11:03 -0700480 // raw field accesses
481 uint32_t Get32(const Object* object) const;
482 void Set32(Object* object, uint32_t new_value) const;
483 uint64_t Get64(const Object* object) const;
484 void Set64(Object* object, uint64_t new_value) const;
485 Object* GetObj(const Object* object) const;
486 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700487
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700488 static Class* GetJavaLangReflectField() {
489 DCHECK(java_lang_reflect_Field_ != NULL);
490 return java_lang_reflect_Field_;
491 }
492
493 static void SetClass(Class* java_lang_reflect_Field);
494 static void ResetClass();
495
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700496 bool IsVolatile() const {
497 return (GetAccessFlags() & kAccVolatile) != 0;
498 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700499
buzbee1da522d2011-09-04 11:22:20 -0700500 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400501 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800502 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400503 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700504
Jesse Wilson35baaab2011-08-10 16:18:03 -0400505 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700506
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800507 // Dex cache index of field id
508 uint32_t field_dex_idx_;
509
Brian Carlstrom693267a2011-09-06 09:25:34 -0700510 // Offset of field within an instance or in the Class' static fields
511 uint32_t offset_;
512
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700513 static Class* java_lang_reflect_Field_;
514
Brian Carlstrom693267a2011-09-06 09:25:34 -0700515 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400516 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700517};
518
Jesse Wilson6bf19152011-09-29 13:12:33 -0400519// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500520class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700521 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700522 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700523 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700524 Object* obj,
525 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700526 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700527 JValue* result);
528
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700529 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700530
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700531 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700532
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700533 static MemberOffset DeclaringClassOffset() {
534 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700535 }
536
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700537 uint32_t GetAccessFlags() const;
538
539 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700540 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700541 }
542
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 // Returns true if the method is declared public.
544 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700545 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700546 }
547
548 // Returns true if the method is declared private.
549 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700550 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700551 }
552
553 // Returns true if the method is declared static.
554 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700556 }
557
Brian Carlstrom1f870082011-08-23 16:02:11 -0700558 // Returns true if the method is a constructor.
559 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700560 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700561 }
562
563 // Returns true if the method is static, private, or a constructor.
564 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700565 return IsDirect(GetAccessFlags());
566 }
567
568 static bool IsDirect(uint32_t access_flags) {
569 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700570 }
571
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700572 // Returns true if the method is declared synchronized.
573 bool IsSynchronized() const {
574 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700575 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700576 }
577
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700579 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580 }
581
Elliott Hughes80609252011-09-23 17:24:51 -0700582 bool IsMiranda() const {
583 return (GetAccessFlags() & kAccMiranda) != 0;
584 }
585
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700586 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700587 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700588 }
589
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700590 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700591 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700592 }
593
594 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700595 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700596 }
597
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800598 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700599
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700600 uint16_t GetMethodIndex() const;
601
602 size_t GetVtableIndex() const {
603 return GetMethodIndex();
604 }
605
606 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700607 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700608 }
609
610 static MemberOffset MethodIndexOffset() {
611 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
612 }
613
614 uint32_t GetCodeItemOffset() const {
615 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
616 }
617
618 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700619 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700620 }
621
622 // Number of 32bit registers that would be required to hold all the arguments
623 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700624
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800625 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700626
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800627 void SetDexMethodIndex(uint32_t new_idx) {
628 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700629 }
630
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700631 ObjectArray<String>* GetDexCacheStrings() const;
632 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
633
634 static MemberOffset DexCacheStringsOffset() {
635 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
636 }
637
Ian Rogers19846512012-02-24 11:42:47 -0800638 static MemberOffset DexCacheResolvedMethodsOffset() {
639 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
640 }
641
buzbee2a475e72011-09-07 17:19:17 -0700642 static MemberOffset DexCacheResolvedTypesOffset() {
643 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
644 }
645
buzbee1da522d2011-09-04 11:22:20 -0700646 static MemberOffset DexCacheInitializedStaticStorageOffset() {
647 return OFFSET_OF_OBJECT_MEMBER(Method,
648 dex_cache_initialized_static_storage_);
649 }
650
Ian Rogers19846512012-02-24 11:42:47 -0800651 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
652 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
653
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700654 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
655 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
656
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700657 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
658 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
659
Ian Rogers466bb252011-10-14 03:29:56 -0700660 // Find the method that this method overrides
661 Method* FindOverriddenMethod() const;
662
Elliott Hughes77405792012-03-15 15:22:12 -0700663 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700664
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700665 const void* GetCode() const {
666 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700667 }
668
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700669 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700670 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
671 }
672
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700673 uint32_t GetCodeSize() const {
674 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
675 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
676 if (code == 0) {
677 return 0;
678 }
679 // TODO: make this Thumb2 specific
680 code &= ~0x1;
681 return reinterpret_cast<uint32_t*>(code)[-1];
682 }
683
684 bool IsWithinCode(uintptr_t pc) const {
685 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
686 if (code == 0) {
687 return pc == 0;
688 }
689 return (code <= pc && pc < code + GetCodeSize());
690 }
691
692 void AssertPcIsWithinCode(uintptr_t pc) const;
693
Brian Carlstrome24fa612011-09-29 00:53:55 -0700694 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700695 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700696 return reinterpret_cast<uint32_t>(GetCode());
697 }
698
699 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700700 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700701 SetCode(reinterpret_cast<void*>(code_offset));
702 }
703
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700704 static MemberOffset GetCodeOffset() {
705 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700706 }
707
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700708 const uint32_t* GetMappingTable() const {
709 const uint32_t* map = GetMappingTableRaw();
710 if (map == NULL) {
711 return map;
712 }
713 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700714 }
715
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700716 uint32_t GetMappingTableLength() const {
717 const uint32_t* map = GetMappingTableRaw();
718 if (map == NULL) {
719 return 0;
720 }
721 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700722 }
723
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700724 const uint32_t* GetMappingTableRaw() const {
725 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700726 }
727
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700728 void SetMappingTable(const uint32_t* mapping_table) {
729 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
730 mapping_table, false);
731 }
732
733 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700734 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700735 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
736 }
737
738 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700739 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700740 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
741 }
742
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800743 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700744 const uint16_t* GetVmapTableRaw() const {
745 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
746 }
747
748 void SetVmapTable(const uint16_t* vmap_table) {
749 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
750 }
751
752 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700753 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700754 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
755 }
756
757 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700758 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700759 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700760 }
761
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800762 const uint8_t* GetGcMap() const {
763 const uint8_t* gc_map_raw = GetGcMapRaw();
764 if (gc_map_raw == NULL) {
765 return gc_map_raw;
766 }
767 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 }
769
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800770 uint32_t GetGcMapLength() const {
771 const uint8_t* gc_map_raw = GetGcMapRaw();
772 if (gc_map_raw == NULL) {
773 return 0;
774 }
775 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
776 (gc_map_raw[1] << 16) |
777 (gc_map_raw[2] << 8) |
778 (gc_map_raw[3] << 0));
779 }
780
781 const uint8_t* GetGcMapRaw() const {
782 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
783 }
784 void SetGcMap(const uint8_t* data) {
785 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
786 }
787
788 uint32_t GetOatGcMapOffset() const {
789 DCHECK(!Runtime::Current()->IsStarted());
790 return reinterpret_cast<uint32_t>(GetGcMapRaw());
791 }
792 void SetOatGcMapOffset(uint32_t gc_map_offset) {
793 DCHECK(!Runtime::Current()->IsStarted());
794 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 }
796
Shih-wei Liaod11af152011-08-23 16:02:11 -0700797 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700798 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
799 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700800 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
801 return result;
802 }
803
804 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700805 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700806 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
807 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700808 }
809
Shih-wei Liaod11af152011-08-23 16:02:11 -0700810 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700811 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700812 }
813
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700814 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700815
Ian Rogers60db5ab2012-02-20 17:02:00 -0800816 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700817
Ian Rogers19846512012-02-24 11:42:47 -0800818 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700819
Ian Rogersb033c752011-07-20 12:22:35 -0700820 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700821 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700822 }
823
Brian Carlstrom78128a62011-09-15 17:21:19 -0700824 const void* GetNativeMethod() const {
825 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
826 }
827
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700828 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700829 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700830 InvokeStub* result = GetFieldPtr<InvokeStub*>(
831 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
832 // TODO: DCHECK(result != NULL); should be ahead of time compiled
833 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700834 }
835
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700836 void SetInvokeStub(InvokeStub* invoke_stub) {
837 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
838 invoke_stub, false);
839 }
840
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700841 uint32_t GetInvokeStubSize() const {
Logan Chien4284bb92012-06-06 15:30:44 +0800842 uintptr_t invoke_stub = reinterpret_cast<uintptr_t>(GetInvokeStub());
843 if (invoke_stub == 0) {
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700844 return 0;
845 }
Logan Chien4284bb92012-06-06 15:30:44 +0800846 // TODO: make this Thumb2 specific
847 invoke_stub &= ~0x1;
848 return reinterpret_cast<const uint32_t*>(invoke_stub)[-1];
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700849 }
850
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700851 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700852 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700853 return reinterpret_cast<uint32_t>(GetInvokeStub());
854 }
855
856 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700857 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700858 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
859 }
860
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700861 static MemberOffset GetInvokeStubOffset() {
862 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700863 }
864
buzbee561227c2011-09-02 15:28:19 -0700865 static MemberOffset GetMethodIndexOffset() {
866 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
867 }
868
Ian Rogers90865722011-09-19 11:11:44 -0700869 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700870 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700871 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700872
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700873 void SetCoreSpillMask(uint32_t core_spill_mask) {
874 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700875 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700876 }
877
Ian Rogers90865722011-09-19 11:11:44 -0700878 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700879 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
880 }
881
882 void SetFpSpillMask(uint32_t fp_spill_mask) {
883 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700884 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700885 }
886
Ian Rogers57b86d42012-03-27 16:05:41 -0700887 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
Ian Rogers640495b2012-06-22 15:15:47 -0700888 // conventions for a method of managed code. Returns false for Proxy methods.
Ian Rogers57b86d42012-03-27 16:05:41 -0700889 bool IsRuntimeMethod() const {
890 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
891 }
892
Ian Rogers90865722011-09-19 11:11:44 -0700893 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700894 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700895 if (!IsRuntimeMethod()) {
896 return false;
897 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700898 Runtime* runtime = Runtime::Current();
899 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700900 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700901 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
902 result = true;
903 break;
904 }
905 }
Ian Rogers90865722011-09-19 11:11:44 -0700906 return result;
907 }
908
Ian Rogers19846512012-02-24 11:42:47 -0800909 bool IsResolutionMethod() const {
910 bool result = this == Runtime::Current()->GetResolutionMethod();
911 // Check that if we do think it is phony it looks like the resolution method
912 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
913 return result;
914 }
915
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700916 // Converts a native PC to a dex PC. TODO: this is a no-op
917 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700918 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700919
920 // Converts a dex PC to a native PC. TODO: this is a no-op
921 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700922 uintptr_t ToNativePC(const uint32_t dex_pc) const;
923
924 // Find the catch block for the given exception type and dex_pc
925 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700926
Elliott Hughes80609252011-09-23 17:24:51 -0700927 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
928
929 static Class* GetConstructorClass() {
930 return java_lang_reflect_Constructor_;
931 }
932
933 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700934 return java_lang_reflect_Method_;
935 }
936
Elliott Hughes80609252011-09-23 17:24:51 -0700937 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700938
939 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700940 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800941 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700942 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700943
Brian Carlstrom693267a2011-09-06 09:25:34 -0700944 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800945 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700946
947 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800948 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700949
950 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700951 ObjectArray<Class>* dex_cache_resolved_types_;
952
953 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
954 ObjectArray<String>* dex_cache_strings_;
955
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700956 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700957 uint32_t access_flags_;
958
959 // Compiled code associated with this method for callers from managed code.
960 // May be compiled managed code or a bridge for invoking a native method.
961 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700962
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700963 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700964 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700965
Brian Carlstrom693267a2011-09-06 09:25:34 -0700966 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700967 uint32_t core_spill_mask_;
968
969 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700970 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700971
Brian Carlstrom693267a2011-09-06 09:25:34 -0700972 // Total size in bytes of the frame
973 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800975 // Garbage collection map
976 const uint8_t* gc_map_;
977
Brian Carlstrom693267a2011-09-06 09:25:34 -0700978 // Native invocation stub entry point for calling from native to managed code.
979 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700980
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700982 const uint32_t* mapping_table_;
983
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800984 // Index into method_ids of the dex file associated with this method
985 uint32_t method_dex_index_;
986
Ian Rogers466bb252011-10-14 03:29:56 -0700987 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700988 //
Ian Rogers466bb252011-10-14 03:29:56 -0700989 // For abstract methods in an interface class, this is the offset of the method in
990 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800991 //
992 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700993 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700994
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700995 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700996 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700997
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 // When a register is promoted into a register, the spill mask holds which registers hold dex
999 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
1000 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001001 const uint16_t* vmap_table_;
1002
Elliott Hughes80609252011-09-23 17:24:51 -07001003 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001004 static Class* java_lang_reflect_Method_;
1005
Brian Carlstrom693267a2011-09-06 09:25:34 -07001006 friend class ImageWriter; // for relocating code_ and invoke_stub_
1007 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001008 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001009};
1010
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001011class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001012 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001013 // A convenience for code that doesn't know the component size,
1014 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001015 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001016
Brian Carlstromb63ec392011-08-27 17:38:27 -07001017 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001018
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001019 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001020
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001021 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001022 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001023 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001024
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001025 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001026 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001027 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001028 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001029
buzbeec143c552011-08-20 17:38:58 -07001030 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001031 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001032 }
1033
Ian Rogersa15e67d2012-02-28 13:51:55 -08001034 static MemberOffset DataOffset(size_t component_size) {
1035 if (component_size != sizeof(int64_t)) {
1036 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1037 } else {
1038 // Align longs and doubles.
1039 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1040 }
buzbeec143c552011-08-20 17:38:58 -07001041 }
1042
Ian Rogersa15e67d2012-02-28 13:51:55 -08001043 void* GetRawData(size_t component_size) {
1044 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1045 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001046 }
1047
Elliott Hughesa21039c2012-06-21 12:09:25 -07001048 const void* GetRawData(size_t component_size) const {
1049 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1050 return reinterpret_cast<const void*>(data);
1051 }
1052
Elliott Hughes289da822011-08-16 10:11:20 -07001053 protected:
1054 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001055 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001056 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001057 }
1058 return true;
1059 }
1060
Elliott Hughes80609252011-09-23 17:24:51 -07001061 protected:
1062 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1063 bool ThrowArrayStoreException(Object* object) const;
1064
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001065 private:
1066 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001067 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001068 // Marker for the data (used by generated code)
1069 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001070
Carl Shapirof88c9522011-08-06 15:47:38 -07001071 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001072};
1073
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001074template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001075class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001076 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001077 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001078
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001079 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001080
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001081 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001082
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001083 // Set element without bound and element type checks, to be used in limited
1084 // circumstances, such as during boot image writing
1085 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001086
Ian Rogers5d76c432011-10-31 21:42:49 -07001087 T* GetWithoutChecks(int32_t i) const;
1088
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001089 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001090 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001091 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001092
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001093 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001094
1095 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001096 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001097};
1098
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001099template<class T>
1100ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001101 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001102 if (UNLIKELY(array == NULL)) {
1103 return NULL;
1104 } else {
1105 return array->AsObjectArray<T>();
1106 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001107}
1108
1109template<class T>
1110T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001111 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001112 return NULL;
1113 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001114 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001115 return GetFieldObject<T*>(data_offset, false);
1116}
1117
1118template<class T>
1119ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1120 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1121 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1122 return new_array;
1123}
1124
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001125// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001126// provides the static storage. However, this might change to an Array
1127// to improve image sharing, so we use this type to avoid assumptions
1128// on the current storage.
Elliott Hughes48257562012-06-06 17:42:44 -07001129class MANAGED StaticStorageBase : public Object {
1130};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001131
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001132// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001133class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001134 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001135 // Class Status
1136 //
1137 // kStatusNotReady: If a Class cannot be found in the class table by
1138 // FindClass, it allocates an new one with AllocClass in the
1139 // kStatusNotReady and calls LoadClass. Note if it does find a
1140 // class, it may not be kStatusResolved and it will try to push it
1141 // forward toward kStatusResolved.
1142 //
1143 // kStatusIdx: LoadClass populates with Class with information from
1144 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001145 // Class value in super_class_ has not been populated. The new Class
1146 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001147 //
1148 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1149 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001150 // using ResolveClass to initialize the super_class_ and ensuring the
1151 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001152 //
1153 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001154 // shows linking is complete and fields of the Class populated by making
1155 // it kStatusResolved. Java allows circularities of the form where a super
1156 // class has a field that is of the type of the sub class. We need to be able
1157 // to fully resolve super classes while resolving types for fields.
jeffhaof1e6b7c2012-06-05 18:33:30 -07001158 //
1159 // kStatusRetryVerificationAtRuntime: The verifier sets a class to
1160 // this state if it encounters a soft failure at compile time. This
1161 // often happens when there are unresolved classes in other dex
1162 // files, and this status marks a class as needing to be verified
1163 // again at runtime.
1164 //
1165 // TODO: Explain the other states
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001166 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001167 kStatusError = -1,
1168 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001169 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001170 kStatusLoaded = 2, // DEX idx values resolved
1171 kStatusResolved = 3, // part of linking
jeffhaoa9b3bf42012-06-06 17:18:39 -07001172 kStatusVerifying = 4, // in the process of being verified
1173 kStatusRetryVerificationAtRuntime = 5, // compile time verification failed, retry at runtime
jeffhaof1e6b7c2012-06-05 18:33:30 -07001174 kStatusVerified = 6, // logically part of linking; done pre-init
1175 kStatusInitializing = 7, // class init in progress
1176 kStatusInitialized = 8, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001177 };
1178
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001179 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001180 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1181 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001182 }
1183
1184 void SetStatus(Status new_status);
1185
1186 // Returns true if the class has failed to link.
1187 bool IsErroneous() const {
1188 return GetStatus() == kStatusError;
1189 }
1190
1191 // Returns true if the class has been loaded.
1192 bool IsIdxLoaded() const {
1193 return GetStatus() >= kStatusIdx;
1194 }
1195
1196 // Returns true if the class has been loaded.
1197 bool IsLoaded() const {
1198 return GetStatus() >= kStatusLoaded;
1199 }
1200
1201 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001202 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001203 return GetStatus() >= kStatusResolved;
1204 }
1205
jeffhaof1e6b7c2012-06-05 18:33:30 -07001206 // Returns true if the class was compile-time verified.
1207 bool IsCompileTimeVerified() const {
1208 return GetStatus() >= kStatusRetryVerificationAtRuntime;
1209 }
1210
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001211 // Returns true if the class has been verified.
1212 bool IsVerified() const {
1213 return GetStatus() >= kStatusVerified;
1214 }
1215
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001216 // Returns true if the class is initializing.
1217 bool IsInitializing() const {
1218 return GetStatus() >= kStatusInitializing;
1219 }
1220
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001221 // Returns true if the class is initialized.
1222 bool IsInitialized() const {
1223 return GetStatus() == kStatusInitialized;
1224 }
1225
1226 uint32_t GetAccessFlags() const;
1227
1228 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001229 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001230 }
1231
1232 // Returns true if the class is an interface.
1233 bool IsInterface() const {
1234 return (GetAccessFlags() & kAccInterface) != 0;
1235 }
1236
1237 // Returns true if the class is declared public.
1238 bool IsPublic() const {
1239 return (GetAccessFlags() & kAccPublic) != 0;
1240 }
1241
1242 // Returns true if the class is declared final.
1243 bool IsFinal() const {
1244 return (GetAccessFlags() & kAccFinal) != 0;
1245 }
1246
Elliott Hughes20cde902011-10-04 17:37:27 -07001247 bool IsFinalizable() const {
1248 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1249 }
1250
1251 void SetFinalizable() {
1252 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1253 SetAccessFlags(flags | kAccClassIsFinalizable);
1254 }
1255
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001256 // Returns true if the class is abstract.
1257 bool IsAbstract() const {
1258 return (GetAccessFlags() & kAccAbstract) != 0;
1259 }
1260
1261 // Returns true if the class is an annotation.
1262 bool IsAnnotation() const {
1263 return (GetAccessFlags() & kAccAnnotation) != 0;
1264 }
1265
1266 // Returns true if the class is synthetic.
1267 bool IsSynthetic() const {
1268 return (GetAccessFlags() & kAccSynthetic) != 0;
1269 }
1270
1271 bool IsReferenceClass() const {
1272 return (GetAccessFlags() & kAccClassIsReference) != 0;
1273 }
1274
1275 bool IsWeakReferenceClass() const {
1276 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1277 }
1278
1279 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001280 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001281 }
1282
1283 bool IsFinalizerReferenceClass() const {
1284 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1285 }
1286
1287 bool IsPhantomReferenceClass() const {
1288 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1289 }
1290
Ian Rogersd418eda2012-01-30 12:14:28 -08001291
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001292 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001293 void SetName(String* name); // Sets the cached name
1294 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001295
1296 bool IsProxyClass() const {
1297 // Read access flags without using getter as whether something is a proxy can be check in
1298 // any loaded state
1299 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1300 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1301 return (access_flags & kAccClassIsProxy) != 0;
1302 }
1303
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001304 Primitive::Type GetPrimitiveType() const {
1305 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1306 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001307 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1308 }
1309
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001310 void SetPrimitiveType(Primitive::Type new_type) {
1311 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001312 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001313 }
1314
1315 // Returns true if the class is a primitive type.
1316 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001317 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001318 }
1319
1320 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001321 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001322 }
1323
1324 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001325 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001326 }
1327
1328 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001329 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001330 }
1331
1332 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001333 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001334 }
1335
1336 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001337 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001338 }
1339
1340 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001341 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001342 }
1343
1344 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001345 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001346 }
1347
1348 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001349 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001350 }
1351
1352 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001353 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001354 }
1355
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 // Depth of class from java.lang.Object
1357 size_t Depth() {
1358 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001359 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 depth++;
1361 }
1362 return depth;
1363 }
1364
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001365 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001366 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001367 }
1368
Elliott Hughesdbb40792011-11-18 17:05:22 -08001369 bool IsClassClass() const;
1370
1371 bool IsStringClass() const;
1372
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001373 bool IsThrowableClass() const;
1374
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001375 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001376 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001377 }
1378
1379 void SetComponentType(Class* new_component_type) {
1380 DCHECK(GetComponentType() == NULL);
1381 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001382 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001383 }
1384
1385 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001386 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001387 }
1388
1389 bool IsObjectClass() const {
1390 return !IsPrimitive() && GetSuperClass() == NULL;
1391 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001392 bool IsInstantiable() const {
1393 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1394 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001395
Brian Carlstrom1f870082011-08-23 16:02:11 -07001396 // Creates a raw object instance but does not invoke the default constructor.
1397 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001398
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001399 bool IsVariableSize() const {
1400 // Classes and arrays vary in size, and so the object_size_ field cannot
1401 // be used to get their instance size
1402 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001403 }
1404
Brian Carlstrom4873d462011-08-21 15:23:39 -07001405 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001406 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001407 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001408 }
1409
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001410 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001411 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001412 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001413 }
1414
Ian Rogers0571d352011-11-03 19:51:38 -07001415 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001417 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001418 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001419 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001420 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001421 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001422 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001423 }
1424
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001425 void SetObjectSize(size_t new_object_size) {
1426 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001427 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001428 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001429 }
1430
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001431 // Returns true if this class is in the same packages as that class.
1432 bool IsInSamePackage(const Class* that) const;
1433
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001434 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001435
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001436 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001437 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001438 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001439 }
1440
Ian Rogersf2391652011-12-14 12:50:52 -08001441 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001442 // Note that access to the class isn't checked in case the declaring class is protected and the
1443 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001444 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001445 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001446 if (this == access_to) {
1447 return true;
1448 }
Ian Rogersf2391652011-12-14 12:50:52 -08001449 // Public members are trivially accessible
1450 if (member_flags & kAccPublic) {
1451 return true;
1452 }
1453 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001454 if (member_flags & kAccPrivate) {
1455 return false;
1456 }
Ian Rogersf2391652011-12-14 12:50:52 -08001457 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001458 if (member_flags & kAccProtected) {
1459 if (this->IsSubClass(access_to)) {
1460 return true;
1461 }
1462 }
Ian Rogersf2391652011-12-14 12:50:52 -08001463 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001464 return this->IsInSamePackage(access_to);
1465 }
1466
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001467 bool IsSubClass(const Class* klass) const;
1468
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1470 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1471 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1472 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1473 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001474 bool IsAssignableFrom(const Class* src) const {
1475 DCHECK(src != NULL);
1476 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001477 // Can always assign to things of the same type
1478 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001479 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001480 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001481 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001482 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001483 return src->Implements(this);
1484 } else if (src->IsArrayClass()) {
1485 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001486 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001488 }
1489 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001490
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001491 Class* GetSuperClass() const {
1492 // Can only get super class for loaded classes (hack for when runtime is
1493 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001494 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001495 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001496 }
1497
1498 void SetSuperClass(Class *new_super_class) {
1499 // super class is assigned once, except during class linker initialization
1500 Class* old_super_class = GetFieldObject<Class*>(
1501 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1502 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1503 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001504 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001505 }
1506
1507 bool HasSuperClass() const {
1508 return GetSuperClass() != NULL;
1509 }
1510
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001511 static MemberOffset SuperClassOffset() {
1512 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001513 }
1514
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001515 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001516
Ian Rogers365c1022012-06-22 15:05:28 -07001517 void SetClassLoader(ClassLoader* new_cl);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001518
1519 static MemberOffset DexCacheOffset() {
1520 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1521 }
1522
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001523 enum {
1524 kDumpClassFullDetail = 1,
1525 kDumpClassClassLoader = (1 << 1),
1526 kDumpClassInitialized = (1 << 2),
1527 };
1528
Elliott Hughes4681c802011-09-25 18:04:37 -07001529 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001530
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001531 DexCache* GetDexCache() const;
1532
1533 void SetDexCache(DexCache* new_dex_cache);
1534
1535 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001536 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001537 return GetFieldObject<ObjectArray<Method>*>(
1538 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1539 }
1540
1541 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1542 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1543 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1544 DCHECK_NE(0, new_direct_methods->GetLength());
1545 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1546 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001547 }
1548
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001549 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001550 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001551 }
1552
1553 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001554 ObjectArray<Method>* direct_methods =
1555 GetFieldObject<ObjectArray<Method>*>(
1556 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1557 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001558 }
1559
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001560 // Returns the number of static, private, and constructor methods.
1561 size_t NumDirectMethods() const {
1562 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1563 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001564
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001565 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001566 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001567 return GetFieldObject<ObjectArray<Method>*>(
1568 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1569 }
1570
1571 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1572 // TODO: we reassign virtual methods to grow the table for miranda
1573 // methods.. they should really just be assigned once
1574 DCHECK_NE(0, new_virtual_methods->GetLength());
1575 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1576 new_virtual_methods, false);
1577 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001578
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001579 // Returns the number of non-inherited virtual methods.
1580 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001581 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001582 }
1583
1584 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001585 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001586 return GetVirtualMethods()->Get(i);
1587 }
1588
1589 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001590 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001591 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001592 }
1593
1594 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001595 ObjectArray<Method>* virtual_methods =
1596 GetFieldObject<ObjectArray<Method>*>(
1597 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1598 virtual_methods->Set(i, f);
1599 }
1600
1601 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001602 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001603 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001604 }
1605
1606 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001607 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001608 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001609 }
1610
1611 void SetVTable(ObjectArray<Method>* new_vtable) {
1612 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1613 }
1614
1615 static MemberOffset VTableOffset() {
1616 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001617 }
1618
Brian Carlstrom30b94452011-08-25 21:35:26 -07001619 // Given a method implemented by this class but potentially from a
1620 // super class, return the specific implementation
1621 // method for this class.
1622 Method* FindVirtualMethodForVirtual(Method* method) {
1623 DCHECK(!method->GetDeclaringClass()->IsInterface());
1624 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001625 // Use the index to a potentially overridden one for this instance's class.
1626 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001627 }
1628
1629 // Given a method implemented by this class, but potentially from a
1630 // super class or interface, return the specific implementation
1631 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001632 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001633
Ian Rogers466bb252011-10-14 03:29:56 -07001634 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001635
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001636 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1637
Brian Carlstrom30b94452011-08-25 21:35:26 -07001638 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001639 if (method->IsDirect()) {
1640 return method;
1641 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001642 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001643 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001644 }
1645 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001646 }
1647
Ian Rogers466bb252011-10-14 03:29:56 -07001648 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001649
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001650 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1651
Ian Rogers466bb252011-10-14 03:29:56 -07001652 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001653
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001654 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001655
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001656 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1657
1658 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1659
1660 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1661
1662 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001663
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001664 int32_t GetIfTableCount() const {
1665 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1666 if (iftable == NULL) {
1667 return 0;
1668 }
1669 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001670 }
1671
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001672 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001673 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001674 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001675 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1676 }
1677
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001678 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001679 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001680 }
1681
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001682 // Get instance fields of the class (See also GetSFields).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001683 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001684 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001685 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001686 }
1687
1688 void SetIFields(ObjectArray<Field>* new_ifields) {
1689 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1690 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001691 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001692 }
1693
1694 size_t NumInstanceFields() const {
1695 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1696 }
1697
1698 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1699 DCHECK_NE(NumInstanceFields(), 0U);
1700 return GetIFields()->Get(i);
1701 }
1702
1703 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1704 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1705 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1706 ifields->Set(i, f);
1707 }
1708
1709 // Returns the number of instance fields containing reference types.
1710 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001711 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001712 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001713 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001714 }
1715
1716 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001717 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001718 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001719 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001720 }
1721
1722 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001723 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001724 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001725 }
1726
1727 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001728 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001729 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001730 }
1731
1732 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1733
1734 // Beginning of static field data
1735 static MemberOffset FieldsOffset() {
1736 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1737 }
1738
1739 // Returns the number of static fields containing reference types.
1740 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001741 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001742 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001743 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001744 }
1745
1746 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001747 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001748 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001749 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001750 }
1751
1752 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001753 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001754 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001755 }
1756
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001757 // Gets the static fields of the class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001758 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001759 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001760 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001761 }
1762
1763 void SetSFields(ObjectArray<Field>* new_sfields) {
1764 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1765 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001766 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001767 }
1768
1769 size_t NumStaticFields() const {
1770 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1771 }
1772
1773 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1774 return GetSFields()->Get(i);
1775 }
1776
1777 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1778 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1779 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1780 sfields->Set(i, f);
1781 }
1782
1783 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001784 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001785 }
1786
1787 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1788
Ian Rogersb067ac22011-12-13 18:05:09 -08001789 // Find a static or instance field using the JLS resolution order
1790 Field* FindField(const StringPiece& name, const StringPiece& type);
1791
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001792 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001793 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001794
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001795 // Finds the given instance field in this class or a superclass, only searches classes that
1796 // have the same dex cache.
1797 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1798
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001799 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001800
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001801 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1802
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001803 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001804 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001805
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001806 // Finds the given static field in this class or superclass, only searches classes that
1807 // have the same dex cache.
1808 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1809
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001810 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001811
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001812 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1813
Elliott Hughesdcc24742011-09-07 14:02:44 -07001814 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001815 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001816 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1817 }
1818
Elliott Hughesdcc24742011-09-07 14:02:44 -07001819 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001820 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001821 }
1822
1823 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001824 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001825 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001826 }
1827
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001828 uint16_t GetDexTypeIndex() const {
1829 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001830 }
1831
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001832 void SetDexTypeIndex(uint16_t type_idx) {
1833 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001834 }
1835
jeffhaobdb76512011-09-07 11:43:16 -07001836 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001837 void SetVerifyErrorClass(Class* klass) {
1838 CHECK(klass != NULL) << PrettyClass(this);
1839 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1840 }
1841
jeffhao5dbddee2011-09-07 16:38:26 -07001842 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001843 bool IsArrayAssignableFromArray(const Class* klass) const;
1844 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001845
Brian Carlstrom693267a2011-09-06 09:25:34 -07001846 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001847 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001848
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001849 // For array classes, the component class object for instanceof/checkcast
1850 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001851 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001852
Elliott Hughes81ff3182012-03-23 20:35:56 -07001853 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1854 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001855 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001856
1857 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001858 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001859
Brian Carlstrom693267a2011-09-06 09:25:34 -07001860 // instance fields
1861 //
1862 // These describe the layout of the contents of an Object.
1863 // Note that only the fields directly declared by this class are
1864 // listed in ifields; fields declared by a superclass are listed in
1865 // the superclass's Class.ifields.
1866 //
1867 // All instance fields that refer to objects are guaranteed to be at
1868 // the beginning of the field list. num_reference_instance_fields_
1869 // specifies the number of reference fields.
1870 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001871
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001872 // Interface table (iftable_), one entry per interface supported by
1873 // this class. That means one entry for each interface we support
1874 // directly, indirectly via superclass, or indirectly via
1875 // superinterface. This will be null if neither we nor our
1876 // superclass implement any interfaces.
1877 //
1878 // Why we need this: given "class Foo implements Face", declare
1879 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1880 // is part of the Face interface. We can't easily use a single
1881 // vtable.
1882 //
1883 // For every interface a concrete class implements, we create an array
1884 // of the concrete vtable_ methods for the methods in the interface.
1885 ObjectArray<InterfaceEntry>* iftable_;
1886
Ian Rogersd418eda2012-01-30 12:14:28 -08001887 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1888 String* name_;
1889
Brian Carlstromdbc05252011-09-09 01:59:59 -07001890 // Static fields
1891 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001892
Ian Rogersd418eda2012-01-30 12:14:28 -08001893 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001894 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001895
Brian Carlstromdbc05252011-09-09 01:59:59 -07001896 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001897 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001898
Brian Carlstromdbc05252011-09-09 01:59:59 -07001899 // virtual methods defined in this class; invoked through vtable
1900 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001901
Ian Rogers9074b992011-10-26 17:41:55 -07001902 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1903 // copied in, and virtual methods from our class either replace those from the super or are
1904 // appended. For abstract classes, methods may be created in the vtable that aren't in
1905 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001906 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001907
Brian Carlstromdbc05252011-09-09 01:59:59 -07001908 // access flags; low 16 bits are defined by VM spec
1909 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001910
Jesse Wilson6bf19152011-09-29 13:12:33 -04001911 // Total size of the Class instance; used when allocating storage on gc heap.
1912 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001913 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001914
Elliott Hughes5f791332011-09-15 17:45:30 -07001915 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001916 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001917
Ian Rogersd418eda2012-01-30 12:14:28 -08001918 // type index from dex file
1919 // TODO: really 16bits
1920 uint32_t dex_type_idx_;
1921
Brian Carlstromdbc05252011-09-09 01:59:59 -07001922 // number of instance fields that are object refs
1923 size_t num_reference_instance_fields_;
1924
1925 // number of static fields that are object refs
1926 size_t num_reference_static_fields_;
1927
1928 // Total object size; used when allocating storage on gc heap.
1929 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001930 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001931 size_t object_size_;
1932
Ian Rogersd418eda2012-01-30 12:14:28 -08001933 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001934 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001935
1936 // Bitmap of offsets of ifields.
1937 uint32_t reference_instance_offsets_;
1938
1939 // Bitmap of offsets of sfields.
1940 uint32_t reference_static_offsets_;
1941
Brian Carlstrom693267a2011-09-06 09:25:34 -07001942 // state of class initialization
1943 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001944
Brian Carlstrom693267a2011-09-06 09:25:34 -07001945 // TODO: ?
1946 // initiating class loader list
1947 // NOTE: for classes with low serialNumber, these are unused, and the
1948 // values are kept in a table in gDvm.
1949 // InitiatingLoaderList initiating_loader_list_;
1950
Brian Carlstrom4873d462011-08-21 15:23:39 -07001951 // Location of first static field.
1952 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001953
Brian Carlstrom693267a2011-09-06 09:25:34 -07001954 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001955 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001956};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001957
Elliott Hughes1f359b02011-07-17 14:27:17 -07001958std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001959
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001960inline void Object::SetClass(Class* new_klass) {
1961 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001962 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001963}
1964
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001965inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001966 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001967 DCHECK(GetClass() != NULL);
1968 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001969}
1970
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001971inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001972 Class* java_lang_Class = GetClass()->GetClass();
1973 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001974}
1975
1976inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001977 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001978}
1979
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001980template<class T>
1981inline ObjectArray<T>* Object::AsObjectArray() {
1982 DCHECK(IsObjectArray());
1983 return down_cast<ObjectArray<T>*>(this);
1984}
1985
1986template<class T>
1987inline const ObjectArray<T>* Object::AsObjectArray() const {
1988 DCHECK(IsObjectArray());
1989 return down_cast<const ObjectArray<T>*>(this);
1990}
1991
Brian Carlstromb63ec392011-08-27 17:38:27 -07001992inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001993 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001994}
1995
Brian Carlstroma663ea52011-08-19 23:33:41 -07001996inline bool Object::IsField() const {
1997 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001998 Class* java_lang_reflect_Field =
1999 java_lang_Class->GetInstanceField(0)->GetClass();
2000 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002001}
2002
2003inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002004 Class* c = GetClass();
2005 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002006}
2007
2008inline bool Object::IsReferenceInstance() const {
2009 return GetClass()->IsReferenceClass();
2010}
2011
2012inline bool Object::IsWeakReferenceInstance() const {
2013 return GetClass()->IsWeakReferenceClass();
2014}
2015
2016inline bool Object::IsSoftReferenceInstance() const {
2017 return GetClass()->IsSoftReferenceClass();
2018}
2019
2020inline bool Object::IsFinalizerReferenceInstance() const {
2021 return GetClass()->IsFinalizerReferenceClass();
2022}
2023
2024inline bool Object::IsPhantomReferenceInstance() const {
2025 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002026}
2027
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002028inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002029 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002030 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002031 result = AsArray()->SizeOf();
2032 } else if (IsClass()) {
2033 result = AsClass()->SizeOf();
2034 } else {
2035 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002036 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002037 DCHECK(!IsField() || result == sizeof(Field));
2038 DCHECK(!IsMethod() || result == sizeof(Method));
2039 return result;
2040}
2041
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002042inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002043 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002044 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002045 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002046 return result;
2047}
2048
2049inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002050 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002051}
2052
2053inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002054 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002055 DCHECK(result != NULL) << this;
2056 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002057 return result;
2058}
2059
2060inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002061 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002062}
2063
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002064inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002065 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002066 size_t component_size = GetClass()->GetComponentSize();
2067 int32_t component_count = GetLength();
2068 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2069 size_t data_size = component_count * component_size;
2070 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002071}
2072
2073template<class T>
2074void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002075 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002076 if (object != NULL) {
2077 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002078 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002079 ThrowArrayStoreException(object);
2080 return;
2081 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002082 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002083 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002084 SetFieldObject(data_offset, object, false);
2085 }
2086}
2087
2088template<class T>
2089void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2090 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002091 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002092 SetFieldObject(data_offset, object, false);
2093}
2094
2095template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002096T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2097 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002098 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002099 return GetFieldObject<T*>(data_offset, false);
2100}
2101
2102template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002103void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2104 ObjectArray<T>* dst, int dst_pos,
2105 size_t length) {
2106 if (src->IsValidIndex(src_pos) &&
2107 src->IsValidIndex(src_pos+length-1) &&
2108 dst->IsValidIndex(dst_pos) &&
2109 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002110 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2111 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002112 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002113 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002114 if (array_class == src->GetClass()) {
2115 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002116 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002117 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002118 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002119 // directly set field, we do a bulk write barrier at the end
2120 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002121 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2122 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2123 }
2124 } else {
2125 Class* element_class = array_class->GetComponentType();
2126 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002127 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002128 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002129 if (object != NULL && !object->InstanceOf(element_class)) {
2130 dst->ThrowArrayStoreException(object);
2131 return;
2132 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002133 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002134 // directly set field, we do a bulk write barrier at the end
2135 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002136 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2137 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2138 }
2139 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002140 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002141 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002142}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002143
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002144class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002145 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002146 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002147 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002148 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002149 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2150};
2151
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002152class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002153 private:
2154 CharArray* ASCII_;
2155 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002156 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002157 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002158 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002159 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2160};
2161
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002162class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002163 private:
2164 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002165 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002166 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2167};
2168
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002169class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002170 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002171 Object* ORDER_BY_SIGNATURE_;
2172 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002173 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2174};
2175
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002176template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002177class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002178 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002179 typedef T ElementType;
2180
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002181 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002182
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002183 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002184 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2185 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002186 }
2187
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002188 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002189 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2190 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002191 }
2192
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002193 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002194 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002195 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002196 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002197 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002198 }
2199
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002200 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002201 // TODO: ArrayStoreException
2202 if (IsValidIndex(i)) {
2203 GetData()[i] = value;
2204 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002205 }
2206
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002207 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002208 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002209 CHECK(array_class != NULL);
2210 array_class_ = array_class;
2211 }
2212
Brian Carlstroma663ea52011-08-19 23:33:41 -07002213 static void ResetArrayClass() {
2214 CHECK(array_class_ != NULL);
2215 array_class_ = NULL;
2216 }
2217
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002218 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002219 static Class* array_class_;
2220
Carl Shapirof88c9522011-08-06 15:47:38 -07002221 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002222};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002223
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002224// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002225class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002226 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002227 static MemberOffset CountOffset() {
2228 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2229 }
2230
2231 static MemberOffset ValueOffset() {
2232 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2233 }
2234
2235 static MemberOffset OffsetOffset() {
2236 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2237 }
2238
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002239 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002240 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002241 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002242 DCHECK(result != NULL);
2243 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002244 }
2245
Elliott Hughes814e4032011-08-23 12:07:56 -07002246 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002247 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002248 DCHECK_LE(0, result);
2249 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002250 }
2251
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002252 int32_t GetLength() const;
2253
Brian Carlstrom395520e2011-09-25 19:35:00 -07002254 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002255
2256 void ComputeHashCode() {
2257 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002258 }
2259
Elliott Hughes814e4032011-08-23 12:07:56 -07002260 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002261 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002262 }
2263
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002264 uint16_t CharAt(int32_t index) const;
2265
Brian Carlstromc74255f2011-09-11 22:47:39 -07002266 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002267
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002268 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002269 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002270 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002271
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002272 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002273
Jesse Wilson8989d992011-08-02 13:39:42 -07002274 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002275 const char* utf8_data_in);
2276
2277 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2278
2279 static String* Alloc(Class* java_lang_String, CharArray* array);
2280
2281 bool Equals(const char* modified_utf8) const;
2282
2283 // TODO: do we need this overload? give it a more intention-revealing name.
2284 bool Equals(const StringPiece& modified_utf8) const;
2285
2286 bool Equals(const String* that) const;
2287
Ian Rogers0571d352011-11-03 19:51:38 -07002288 // Compare UTF-16 code point values not in a locale-sensitive manner
2289 int Compare(int32_t utf16_length, const char* utf8_data_in);
2290
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002291 // TODO: do we need this overload? give it a more intention-revealing name.
2292 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2293 int32_t that_length) const;
2294
2295 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2296 std::string ToModifiedUtf8() const;
2297
2298 static Class* GetJavaLangString() {
2299 DCHECK(java_lang_String_ != NULL);
2300 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002301 }
2302
Brian Carlstroma663ea52011-08-19 23:33:41 -07002303 static void SetClass(Class* java_lang_String);
2304 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002305
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002306 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002307 void SetHashCode(int32_t new_hash_code) {
2308 DCHECK_EQ(0u,
2309 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2310 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2311 new_hash_code, false);
2312 }
2313
2314 void SetCount(int32_t new_count) {
2315 DCHECK_LE(0, new_count);
2316 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2317 }
2318
2319 void SetOffset(int32_t new_offset) {
2320 DCHECK_LE(0, new_offset);
2321 DCHECK_GE(GetLength(), new_offset);
2322 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2323 }
2324
2325 void SetArray(CharArray* new_array) {
2326 DCHECK(new_array != NULL);
2327 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2328 }
2329
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002330 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2331 CharArray* array_;
2332
Brian Carlstromdbc05252011-09-09 01:59:59 -07002333 int32_t count_;
2334
Carl Shapirof88c9522011-08-06 15:47:38 -07002335 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002336
Elliott Hughes289da822011-08-16 10:11:20 -07002337 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002338
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002339 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002340
Brian Carlstrom693267a2011-09-06 09:25:34 -07002341 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002342 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002343 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002344};
2345
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002346struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002347 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002348 return string->GetHashCode();
2349 }
2350};
2351
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002352inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002353 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002354 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2355}
2356
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002357inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002358 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002359 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002360}
2361
2362inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002363 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002364 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002365}
2366
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002367inline uint32_t Class::GetAccessFlags() const {
2368 // Check class is loaded or this is java.lang.String that has a
2369 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002370 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002371 this == String::GetJavaLangString() ||
2372 this == Field::GetJavaLangReflectField() ||
2373 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002374 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002375 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2376}
2377
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002378inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002379 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002380 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2381}
2382
2383inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002384 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002385 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002386}
2387
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002388inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002389 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002390 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002391}
2392
Elliott Hughes76e36942012-03-16 13:44:56 -07002393inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002394 if (!kIsDebugBuild) {
2395 return;
2396 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002397 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2398 return;
2399 }
2400 Runtime* runtime = Runtime::Current();
2401 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2402 return;
2403 }
2404 DCHECK(IsWithinCode(pc))
2405 << PrettyMethod(this)
2406 << " pc=" << std::hex << pc
2407 << " code=" << GetCode()
2408 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002409}
2410
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002411inline String* Class::GetName() const {
2412 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002413}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002414inline void Class::SetName(String* name) {
2415 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002416}
2417
2418// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002419class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002420 public:
2421 void SetDetailMessage(String* new_detail_message) {
2422 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2423 new_detail_message, false);
2424 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002425 String* GetDetailMessage() const {
2426 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2427 }
Ian Rogers9074b992011-10-26 17:41:55 -07002428 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002429
Ian Rogers1c5eb702012-02-01 09:18:34 -08002430 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2431 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2432 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2433 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002434 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002435
2436 static Class* GetJavaLangThrowable() {
2437 DCHECK(java_lang_Throwable_ != NULL);
2438 return java_lang_Throwable_;
2439 }
2440
2441 static void SetClass(Class* java_lang_Throwable);
2442 static void ResetClass();
2443
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002444 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002445 Object* GetStackState() const {
2446 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2447 }
2448
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002449 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2450 Throwable* cause_;
2451 String* detail_message_;
2452 Object* stack_state_; // Note this is Java volatile:
2453 Object* stack_trace_;
2454 Object* suppressed_exceptions_;
2455
Ian Rogers5167c972012-02-03 10:41:20 -08002456 static Class* java_lang_Throwable_;
2457
Brian Carlstrom693267a2011-09-06 09:25:34 -07002458 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002459 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2460};
2461
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002462// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002463class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002464 public:
2465 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002466 return GetFieldObject<const String*>(
2467 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002468 }
2469
2470 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002471 return GetFieldObject<const String*>(
2472 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002473 }
2474
2475 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002476 return GetFieldObject<const String*>(
2477 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002478 }
2479
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002480 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002481 return GetField32(
2482 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002483 }
2484
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002485 static StackTraceElement* Alloc(String* declaring_class,
2486 String* method_name,
2487 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002488 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002489
2490 static void SetClass(Class* java_lang_StackTraceElement);
2491
2492 static void ResetClass();
2493
2494 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002495 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002496 String* declaring_class_;
2497 String* file_name_;
2498 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002499 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002500
2501 static Class* GetStackTraceElement() {
2502 DCHECK(java_lang_StackTraceElement_ != NULL);
2503 return java_lang_StackTraceElement_;
2504 }
2505
2506 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002507
2508 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002509 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2510};
2511
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002512class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002513 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002514 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002515 Class* interface = Get(kInterface)->AsClass();
2516 DCHECK(interface != NULL);
2517 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002518 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002519
Brian Carlstrom30b94452011-08-25 21:35:26 -07002520 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002521 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002522 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002523 DCHECK(Get(kInterface) == NULL);
2524 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002525 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002526
Brian Carlstrom86927212011-09-15 11:31:11 -07002527 size_t GetMethodArrayCount() const {
2528 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002529 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002530 return 0;
2531 }
2532 return method_array->GetLength();
2533 }
2534
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002535 ObjectArray<Method>* GetMethodArray() const {
2536 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2537 DCHECK(method_array != NULL);
2538 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002539 }
2540
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002541 void SetMethodArray(ObjectArray<Method>* new_ma) {
2542 DCHECK(new_ma != NULL);
2543 DCHECK(Get(kMethodArray) == NULL);
2544 Set(kMethodArray, new_ma);
2545 }
2546
2547 static size_t LengthAsArray() {
2548 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002549 }
2550
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002551 private:
Elliott Hughes48257562012-06-06 17:42:44 -07002552 enum {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002553 // Points to the interface class.
2554 kInterface = 0,
2555 // Method pointers into the vtable, allow fast map from interface
2556 // method index to concrete instance method.
2557 kMethodArray = 1,
2558 kMax = 2,
2559 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002560
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002561 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002562};
2563
Ian Rogersc2b44472011-12-14 21:17:17 -08002564class MANAGED SynthesizedProxyClass : public Class {
2565 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002566 ObjectArray<Class>* GetInterfaces() {
2567 return interfaces_;
2568 }
2569
Ian Rogersc2b44472011-12-14 21:17:17 -08002570 ObjectArray<ObjectArray<Class> >* GetThrows() {
2571 return throws_;
2572 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002573
Jesse Wilson95caa792011-10-12 18:14:17 -04002574 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002575 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002576 ObjectArray<ObjectArray<Class> >* throws_;
2577 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002578};
2579
2580class MANAGED Proxy : public Object {
2581 private:
2582 Object* h_;
2583
2584 friend struct ProxyOffsets; // for verifying offset information
2585 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2586};
2587
Carl Shapiro1fb86202011-06-27 17:43:13 -07002588} // namespace art
2589
2590#endif // ART_SRC_OBJECT_H_