blob: eeac3ea0f1606a5a36c0f5505e1a04a4a3ad33e3 [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)
Elliott Hughes582a7d12011-10-10 18:38:42 -0700141static const uint32_t kAccWritable = 0x80000000; // method (dex only)
Brian Carlstrom1f870082011-08-23 16:02:11 -0700142
Elliott Hughes20cde902011-10-04 17:37:27 -0700143// Special runtime-only flags.
144// Note: if only kAccClassIsReference is set, we have a soft reference.
145static const uint32_t kAccClassIsFinalizable = 0x80000000; // class/ancestor overrides finalize()
146static const uint32_t kAccClassIsReference = 0x08000000; // class is a soft/weak/phantom ref
147static const uint32_t kAccClassIsWeakReference = 0x04000000; // class is a weak reference
148static const uint32_t kAccClassIsFinalizerReference = 0x02000000; // class is a finalizer reference
149static const uint32_t kAccClassIsPhantomReference = 0x01000000; // class is a phantom reference
Brian Carlstrom1f870082011-08-23 16:02:11 -0700150
151static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
152 | kAccClassIsWeakReference
153 | kAccClassIsFinalizerReference
154 | kAccClassIsPhantomReference);
155
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700157 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158 */
159/*
160 * A magic value for refOffsets. Ignore the bits and walk the super
161 * chain when this is the value.
162 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
163 * fields followed by 2 ref instance fields.]
164 */
165#define CLASS_WALK_SUPER ((unsigned int)(3))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700166#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
167#define CLASS_OFFSET_ALIGNMENT 4
168#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
169/*
170 * Given an offset, return the bit number which would encode that offset.
171 * Local use only.
172 */
173#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700174 ((unsigned int)(byteOffset) / \
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700175 CLASS_OFFSET_ALIGNMENT)
176/*
177 * Is the given offset too large to be encoded?
178 */
179#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
180 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
181/*
182 * Return a single bit, encoding the offset.
183 * Undefined if the offset is too large, as defined above.
184 */
185#define CLASS_BIT_FROM_OFFSET(byteOffset) \
186 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
187/*
188 * Return an offset, given a bit number as returned from CLZ.
189 */
190#define CLASS_OFFSET_FROM_CLZ(rshift) \
Brian Carlstrom693267a2011-09-06 09:25:34 -0700191 MemberOffset((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700193#define OFFSET_OF_OBJECT_MEMBER(type, field) \
194 MemberOffset(OFFSETOF_MEMBER(type, field))
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700196// Classes shared with the managed side of the world need to be packed
197// so that they don't have extra platform specific padding.
Elliott Hughes85d15452011-09-16 17:33:01 -0700198#define MANAGED PACKED
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700199
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200// C++ mirror of java.lang.Object
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -0700201class MANAGED Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700202 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 static MemberOffset ClassOffset() {
204 return OFFSET_OF_OBJECT_MEMBER(Object, klass_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700205 }
206
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 Class* GetClass() const {
Elliott Hughes5f791332011-09-15 17:45:30 -0700208 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Object, klass_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700209 }
210
211 void SetClass(Class* new_klass);
212
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700213 bool InstanceOf(const Class* klass) const;
214
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700215 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216
Elliott Hughes081be7f2011-09-18 16:50:26 -0700217 Object* Clone();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700218
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 static MemberOffset MonitorOffset() {
220 return OFFSET_OF_OBJECT_MEMBER(Object, monitor_);
221 }
222
Elliott Hughes5f791332011-09-15 17:45:30 -0700223 volatile int32_t* GetRawLockWordAddress() {
224 byte* raw_addr = reinterpret_cast<byte*>(this) + OFFSET_OF_OBJECT_MEMBER(Object, monitor_).Int32Value();
225 int32_t* word_addr = reinterpret_cast<int32_t*>(raw_addr);
226 return const_cast<volatile int32_t*>(word_addr);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 }
228
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700229 uint32_t GetThinLockId();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230
Elliott Hughes5f791332011-09-15 17:45:30 -0700231 void MonitorEnter(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232
Ian Rogersff1ed472011-09-20 13:46:24 -0700233 bool MonitorExit(Thread* thread);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700234
Elliott Hughes5f791332011-09-15 17:45:30 -0700235 void Notify();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236
Elliott Hughes5f791332011-09-15 17:45:30 -0700237 void NotifyAll();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700238
Elliott Hughes5f791332011-09-15 17:45:30 -0700239 void Wait(int64_t timeout);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240
Elliott Hughes5f791332011-09-15 17:45:30 -0700241 void Wait(int64_t timeout, int32_t nanos);
Brian Carlstrom4873d462011-08-21 15:23:39 -0700242
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700243 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
245 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700246 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247 return down_cast<Class*>(this);
248 }
249
250 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252 return down_cast<const Class*>(this);
253 }
254
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700255 bool IsObjectArray() const;
256
257 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700258 ObjectArray<T>* AsObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700260 template<class T>
Brian Carlstrom34f426c2011-10-04 12:58:02 -0700261 const ObjectArray<T>* AsObjectArray() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700262
Brian Carlstromb63ec392011-08-27 17:38:27 -0700263 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700264
265 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700266 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700267 return down_cast<Array*>(this);
268 }
269
270 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700271 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700272 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700273 }
274
Elliott Hughesdbb40792011-11-18 17:05:22 -0800275 String* AsString();
Brian Carlstroma663ea52011-08-19 23:33:41 -0700276
277 bool IsMethod() const;
278
279 Method* AsMethod() {
280 DCHECK(IsMethod());
281 return down_cast<Method*>(this);
282 }
283
Brian Carlstrom4873d462011-08-21 15:23:39 -0700284 const Method* AsMethod() const {
285 DCHECK(IsMethod());
286 return down_cast<const Method*>(this);
287 }
288
Brian Carlstroma663ea52011-08-19 23:33:41 -0700289 bool IsField() const;
290
291 Field* AsField() {
292 DCHECK(IsField());
293 return down_cast<Field*>(this);
294 }
295
Brian Carlstrom4873d462011-08-21 15:23:39 -0700296 const Field* AsField() const {
297 DCHECK(IsField());
298 return down_cast<const Field*>(this);
299 }
300
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 bool IsReferenceInstance() const;
302
303 bool IsWeakReferenceInstance() const;
304
305 bool IsSoftReferenceInstance() const;
306
307 bool IsFinalizerReferenceInstance() const;
308
309 bool IsPhantomReferenceInstance() const;
310
311 // Accessors for Java type fields
312 template<class T>
313 T GetFieldObject(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700314 DCHECK(Thread::Current() == NULL || Thread::Current()->CanAccessDirectReferences());
315 T result = reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800316 Runtime::Current()->GetHeap()->VerifyObject(result);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700317 return result;
318 }
319
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700320 void SetFieldObject(MemberOffset field_offset, const Object* new_value, bool is_volatile, bool this_is_valid = true) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800321 Runtime::Current()->GetHeap()->VerifyObject(new_value);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700322 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
323 if (new_value != NULL) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800324 Runtime::Current()->GetHeap()->WriteBarrierField(this, field_offset, new_value);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700325 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700326 }
327
328 uint32_t GetField32(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800329 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700330 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
331 const int32_t* word_addr = reinterpret_cast<const int32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700332 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700333 return android_atomic_acquire_load(word_addr);
334 } else {
335 return *word_addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700336 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700337 }
338
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700339 void SetField32(MemberOffset field_offset, uint32_t new_value, bool is_volatile, bool this_is_valid = true) {
340 if (this_is_valid) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800341 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700342 }
343 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700344 uint32_t* word_addr = reinterpret_cast<uint32_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700345 if (UNLIKELY(is_volatile)) {
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700346 /*
347 * TODO: add an android_atomic_synchronization_store() function and
348 * use it in the 32-bit volatile set handlers. On some platforms we
349 * can use a fast atomic instruction and avoid the barriers.
350 */
351 ANDROID_MEMBAR_STORE();
352 *word_addr = new_value;
353 ANDROID_MEMBAR_FULL();
354 } else {
355 *word_addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700356 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700357 }
358
359 uint64_t GetField64(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800360 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700361 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset.Int32Value();
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700362 const int64_t* addr = reinterpret_cast<const int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700363 if (UNLIKELY(is_volatile)) {
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700364 uint64_t result = QuasiAtomic::Read64(addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700365 ANDROID_MEMBAR_FULL();
366 return result;
367 } else {
368 return *addr;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700369 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700370 }
371
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700372 void SetField64(MemberOffset field_offset, uint64_t new_value, bool is_volatile) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800373 Runtime::Current()->GetHeap()->VerifyObject(this);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700374 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset.Int32Value();
375 int64_t* addr = reinterpret_cast<int64_t*>(raw_addr);
Ian Rogerscaab8c42011-10-12 12:11:18 -0700376 if (UNLIKELY(is_volatile)) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700377 ANDROID_MEMBAR_STORE();
Elliott Hughes7c6169d2012-05-02 16:11:48 -0700378 QuasiAtomic::Swap64(new_value, addr);
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700379 // Post-store barrier not required due to use of atomic op or mutex.
380 } else {
381 *addr = new_value;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700382 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700383 }
384
385 protected:
386 // Accessors for non-Java type fields
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700387 template<class T>
388 T GetFieldPtr(MemberOffset field_offset, bool is_volatile) const {
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700389 return reinterpret_cast<T>(GetField32(field_offset, is_volatile));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700390 }
391
392 template<typename T>
Ian Rogers30fab402012-01-23 15:43:46 -0800393 void SetFieldPtr(MemberOffset field_offset, T new_value, bool is_volatile, bool this_is_valid = true) {
394 SetField32(field_offset, reinterpret_cast<uint32_t>(new_value), is_volatile, this_is_valid);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700395 }
396
397 private:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700398 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700399
Elliott Hughes5f791332011-09-15 17:45:30 -0700400 uint32_t monitor_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401
Elliott Hughes5f791332011-09-15 17:45:30 -0700402 friend class ImageWriter; // for abusing monitor_ directly
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700403 friend struct ObjectOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -0700404 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405};
406
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700407struct ObjectIdentityHash {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800408 size_t operator()(const Object* const& obj) const {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700409#ifdef MOVING_GARBAGE_COLLECTOR
410 // TODO: we'll need to use the Object's internal concept of identity
411 UNIMPLEMENTED(FATAL);
412#endif
413 return reinterpret_cast<size_t>(obj);
414 }
415};
416
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700417// C++ mirror of java.lang.reflect.Field
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500418class MANAGED Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700419 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700420 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700421
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700422 void SetDeclaringClass(Class *new_declaring_class);
423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700424 uint32_t GetAccessFlags() const;
425
426 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700427 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), new_access_flags, false);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700428 }
429
Elliott Hughes80609252011-09-23 17:24:51 -0700430 bool IsPublic() const {
431 return (GetAccessFlags() & kAccPublic) != 0;
432 }
433
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700434 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700435 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700436 }
437
jeffhaobdb76512011-09-07 11:43:16 -0700438 bool IsFinal() const {
Elliott Hughes80609252011-09-23 17:24:51 -0700439 return (GetAccessFlags() & kAccFinal) != 0;
jeffhaobdb76512011-09-07 11:43:16 -0700440 }
441
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 uint32_t GetDexFieldIndex() const {
443 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), false);
444 }
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800446 void SetDexFieldIndex(uint32_t new_idx) {
447 SetField32(OFFSET_OF_OBJECT_MEMBER(Field, field_dex_idx_), new_idx, false);
448 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700449
450 // Offset to field within an Object
451 MemberOffset GetOffset() const;
452
buzbee34cd9e52011-09-08 14:31:52 -0700453 static MemberOffset OffsetOffset() {
454 return MemberOffset(OFFSETOF_MEMBER(Field, offset_));
455 }
456
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700457 MemberOffset GetOffsetDuringLinking() const;
458
459 void SetOffset(MemberOffset num_bytes);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700460
Brian Carlstrom4873d462011-08-21 15:23:39 -0700461 // field access, null object for static fields
462 bool GetBoolean(const Object* object) const;
463 void SetBoolean(Object* object, bool z) const;
464 int8_t GetByte(const Object* object) const;
465 void SetByte(Object* object, int8_t b) const;
466 uint16_t GetChar(const Object* object) const;
467 void SetChar(Object* object, uint16_t c) const;
Ian Rogers466bb252011-10-14 03:29:56 -0700468 int16_t GetShort(const Object* object) const;
469 void SetShort(Object* object, int16_t s) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700470 int32_t GetInt(const Object* object) const;
471 void SetInt(Object* object, int32_t i) const;
472 int64_t GetLong(const Object* object) const;
473 void SetLong(Object* object, int64_t j) const;
474 float GetFloat(const Object* object) const;
475 void SetFloat(Object* object, float f) const;
476 double GetDouble(const Object* object) const;
477 void SetDouble(Object* object, double d) const;
478 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700479 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700480
Ian Rogersce9eca62011-10-07 17:11:03 -0700481 // raw field accesses
482 uint32_t Get32(const Object* object) const;
483 void Set32(Object* object, uint32_t new_value) const;
484 uint64_t Get64(const Object* object) const;
485 void Set64(Object* object, uint64_t new_value) const;
486 Object* GetObj(const Object* object) const;
487 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstromb9edb842011-08-28 16:31:06 -0700488
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700489 static Class* GetJavaLangReflectField() {
490 DCHECK(java_lang_reflect_Field_ != NULL);
491 return java_lang_reflect_Field_;
492 }
493
494 static void SetClass(Class* java_lang_reflect_Field);
495 static void ResetClass();
496
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700497 bool IsVolatile() const {
498 return (GetAccessFlags() & kAccVolatile) != 0;
499 }
Brian Carlstrom4873d462011-08-21 15:23:39 -0700500
buzbee1da522d2011-09-04 11:22:20 -0700501 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400502 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800503 // The class we are a part of
Jesse Wilson35baaab2011-08-10 16:18:03 -0400504 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700505
Jesse Wilson35baaab2011-08-10 16:18:03 -0400506 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700507
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 // Dex cache index of field id
509 uint32_t field_dex_idx_;
510
Brian Carlstrom693267a2011-09-06 09:25:34 -0700511 // Offset of field within an instance or in the Class' static fields
512 uint32_t offset_;
513
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700514 static Class* java_lang_reflect_Field_;
515
Brian Carlstrom693267a2011-09-06 09:25:34 -0700516 friend struct FieldOffsets; // for verifying offset information
Jesse Wilson35baaab2011-08-10 16:18:03 -0400517 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700518};
519
Jesse Wilson6bf19152011-09-29 13:12:33 -0400520// C++ mirror of java.lang.reflect.Method and java.lang.reflect.Constructor
Jesse Wilsonc129a6b2011-11-24 14:47:46 -0500521class MANAGED Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700522 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700523 // An function that invokes a method with an array of its arguments.
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700524 typedef void InvokeStub(const Method* method,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700525 Object* obj,
526 Thread* thread,
Elliott Hughes77405792012-03-15 15:22:12 -0700527 JValue* args,
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700528 JValue* result);
529
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700530 Class* GetDeclaringClass() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700531
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700532 void SetDeclaringClass(Class *new_declaring_class);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700534 static MemberOffset DeclaringClassOffset() {
535 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700536 }
537
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700538 uint32_t GetAccessFlags() const;
539
540 void SetAccessFlags(uint32_t new_access_flags) {
Elliott Hughes20cde902011-10-04 17:37:27 -0700541 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700542 }
543
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700544 // Returns true if the method is declared public.
545 bool IsPublic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700546 return (GetAccessFlags() & kAccPublic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547 }
548
549 // Returns true if the method is declared private.
550 bool IsPrivate() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700551 return (GetAccessFlags() & kAccPrivate) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552 }
553
554 // Returns true if the method is declared static.
555 bool IsStatic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700556 return (GetAccessFlags() & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 }
558
Brian Carlstrom1f870082011-08-23 16:02:11 -0700559 // Returns true if the method is a constructor.
560 bool IsConstructor() const {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700561 return (GetAccessFlags() & kAccConstructor) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700562 }
563
564 // Returns true if the method is static, private, or a constructor.
565 bool IsDirect() const {
Brian Carlstromf5822582012-03-19 22:34:31 -0700566 return IsDirect(GetAccessFlags());
567 }
568
569 static bool IsDirect(uint32_t access_flags) {
570 return (access_flags & (kAccStatic | kAccPrivate | kAccConstructor)) != 0;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700571 }
572
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573 // Returns true if the method is declared synchronized.
574 bool IsSynchronized() const {
575 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700576 return (GetAccessFlags() & synchonized) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577 }
578
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579 bool IsFinal() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700580 return (GetAccessFlags() & kAccFinal) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700581 }
582
Elliott Hughes80609252011-09-23 17:24:51 -0700583 bool IsMiranda() const {
584 return (GetAccessFlags() & kAccMiranda) != 0;
585 }
586
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700587 bool IsNative() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700588 return (GetAccessFlags() & kAccNative) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700589 }
590
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700591 bool IsAbstract() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700592 return (GetAccessFlags() & kAccAbstract) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700593 }
594
595 bool IsSynthetic() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700596 return (GetAccessFlags() & kAccSynthetic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700597 }
598
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800599 bool IsProxyMethod() const;
Ian Rogers0571d352011-11-03 19:51:38 -0700600
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700601 uint16_t GetMethodIndex() const;
602
603 size_t GetVtableIndex() const {
604 return GetMethodIndex();
605 }
606
607 void SetMethodIndex(uint16_t new_method_index) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700608 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), new_method_index, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700609 }
610
611 static MemberOffset MethodIndexOffset() {
612 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
613 }
614
615 uint32_t GetCodeItemOffset() const {
616 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), false);
617 }
618
619 void SetCodeItemOffset(uint32_t new_code_off) {
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700620 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, code_item_offset_), new_code_off, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700621 }
622
623 // Number of 32bit registers that would be required to hold all the arguments
624 static size_t NumArgRegisters(const StringPiece& shorty);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700625
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800626 uint32_t GetDexMethodIndex() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700627
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800628 void SetDexMethodIndex(uint32_t new_idx) {
629 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), new_idx, false);
Ian Rogersb033c752011-07-20 12:22:35 -0700630 }
631
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700632 ObjectArray<String>* GetDexCacheStrings() const;
633 void SetDexCacheStrings(ObjectArray<String>* new_dex_cache_strings);
634
635 static MemberOffset DexCacheStringsOffset() {
636 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_strings_);
637 }
638
Ian Rogers19846512012-02-24 11:42:47 -0800639 static MemberOffset DexCacheResolvedMethodsOffset() {
640 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_methods_);
641 }
642
buzbee2a475e72011-09-07 17:19:17 -0700643 static MemberOffset DexCacheResolvedTypesOffset() {
644 return OFFSET_OF_OBJECT_MEMBER(Method, dex_cache_resolved_types_);
645 }
646
buzbee1da522d2011-09-04 11:22:20 -0700647 static MemberOffset DexCacheInitializedStaticStorageOffset() {
648 return OFFSET_OF_OBJECT_MEMBER(Method,
649 dex_cache_initialized_static_storage_);
650 }
651
Ian Rogers19846512012-02-24 11:42:47 -0800652 ObjectArray<Method>* GetDexCacheResolvedMethods() const;
653 void SetDexCacheResolvedMethods(ObjectArray<Method>* new_dex_cache_methods);
654
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700655 ObjectArray<Class>* GetDexCacheResolvedTypes() const;
656 void SetDexCacheResolvedTypes(ObjectArray<Class>* new_dex_cache_types);
657
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700658 ObjectArray<StaticStorageBase>* GetDexCacheInitializedStaticStorage() const;
659 void SetDexCacheInitializedStaticStorage(ObjectArray<StaticStorageBase>* new_value);
660
Ian Rogers466bb252011-10-14 03:29:56 -0700661 // Find the method that this method overrides
662 Method* FindOverriddenMethod() const;
663
Elliott Hughes77405792012-03-15 15:22:12 -0700664 void Invoke(Thread* self, Object* receiver, JValue* args, JValue* result) const;
Elliott Hughesf5ecf062011-09-06 17:37:59 -0700665
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700666 const void* GetCode() const {
667 return GetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), false);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700668 }
669
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700670 void SetCode(const void* code) {
Brian Carlstrome24fa612011-09-29 00:53:55 -0700671 SetFieldPtr<const void*>(OFFSET_OF_OBJECT_MEMBER(Method, code_), code, false);
672 }
673
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700674 uint32_t GetCodeSize() const {
675 DCHECK(!IsRuntimeMethod() && !IsProxyMethod()) << PrettyMethod(this);
676 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
677 if (code == 0) {
678 return 0;
679 }
680 // TODO: make this Thumb2 specific
681 code &= ~0x1;
682 return reinterpret_cast<uint32_t*>(code)[-1];
683 }
684
685 bool IsWithinCode(uintptr_t pc) const {
686 uintptr_t code = reinterpret_cast<uintptr_t>(GetCode());
687 if (code == 0) {
688 return pc == 0;
689 }
690 return (code <= pc && pc < code + GetCodeSize());
691 }
692
693 void AssertPcIsWithinCode(uintptr_t pc) const;
694
Brian Carlstrome24fa612011-09-29 00:53:55 -0700695 uint32_t GetOatCodeOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700696 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700697 return reinterpret_cast<uint32_t>(GetCode());
698 }
699
700 void SetOatCodeOffset(uint32_t code_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700701 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrome24fa612011-09-29 00:53:55 -0700702 SetCode(reinterpret_cast<void*>(code_offset));
703 }
704
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700705 static MemberOffset GetCodeOffset() {
706 return OFFSET_OF_OBJECT_MEMBER(Method, code_);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700707 }
708
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700709 const uint32_t* GetMappingTable() const {
710 const uint32_t* map = GetMappingTableRaw();
711 if (map == NULL) {
712 return map;
713 }
714 return map + 1;
Brian Carlstrome24fa612011-09-29 00:53:55 -0700715 }
716
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700717 uint32_t GetMappingTableLength() const {
718 const uint32_t* map = GetMappingTableRaw();
719 if (map == NULL) {
720 return 0;
721 }
722 return *map;
Ian Rogersbdb03912011-09-14 00:55:44 -0700723 }
724
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700725 const uint32_t* GetMappingTableRaw() const {
726 return GetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_), false);
buzbeec41e5b52011-09-23 12:46:19 -0700727 }
728
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700729 void SetMappingTable(const uint32_t* mapping_table) {
730 SetFieldPtr<const uint32_t*>(OFFSET_OF_OBJECT_MEMBER(Method, mapping_table_),
731 mapping_table, false);
732 }
733
734 uint32_t GetOatMappingTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700735 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700736 return reinterpret_cast<uint32_t>(GetMappingTableRaw());
737 }
738
739 void SetOatMappingTableOffset(uint32_t mapping_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700740 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700741 SetMappingTable(reinterpret_cast<const uint32_t*>(mapping_table_offset));
742 }
743
Elliott Hughes68fdbd02011-11-29 19:22:47 -0800744 // Callers should wrap the uint16_t* in a VmapTable instance for convenient access.
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700745 const uint16_t* GetVmapTableRaw() const {
746 return GetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), false);
747 }
748
749 void SetVmapTable(const uint16_t* vmap_table) {
750 SetFieldPtr<const uint16_t*>(OFFSET_OF_OBJECT_MEMBER(Method, vmap_table_), vmap_table, false);
751 }
752
753 uint32_t GetOatVmapTableOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700754 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700755 return reinterpret_cast<uint32_t>(GetVmapTableRaw());
756 }
757
758 void SetOatVmapTableOffset(uint32_t vmap_table_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700759 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700760 SetVmapTable(reinterpret_cast<uint16_t*>(vmap_table_offset));
Brian Carlstrome24fa612011-09-29 00:53:55 -0700761 }
762
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800763 const uint8_t* GetGcMap() const {
764 const uint8_t* gc_map_raw = GetGcMapRaw();
765 if (gc_map_raw == NULL) {
766 return gc_map_raw;
767 }
768 return gc_map_raw + sizeof(uint32_t);
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 }
770
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800771 uint32_t GetGcMapLength() const {
772 const uint8_t* gc_map_raw = GetGcMapRaw();
773 if (gc_map_raw == NULL) {
774 return 0;
775 }
776 return static_cast<uint32_t>((gc_map_raw[0] << 24) |
777 (gc_map_raw[1] << 16) |
778 (gc_map_raw[2] << 8) |
779 (gc_map_raw[3] << 0));
780 }
781
782 const uint8_t* GetGcMapRaw() const {
783 return GetFieldPtr<uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), false);
784 }
785 void SetGcMap(const uint8_t* data) {
786 SetFieldPtr<const uint8_t*>(OFFSET_OF_OBJECT_MEMBER(Method, gc_map_), data, false);
787 }
788
789 uint32_t GetOatGcMapOffset() const {
790 DCHECK(!Runtime::Current()->IsStarted());
791 return reinterpret_cast<uint32_t>(GetGcMapRaw());
792 }
793 void SetOatGcMapOffset(uint32_t gc_map_offset) {
794 DCHECK(!Runtime::Current()->IsStarted());
795 SetGcMap(reinterpret_cast<uint8_t*>(gc_map_offset));
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 }
797
Shih-wei Liaod11af152011-08-23 16:02:11 -0700798 size_t GetFrameSizeInBytes() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700799 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
800 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700801 DCHECK_LE(static_cast<size_t>(kStackAlignment), result);
802 return result;
803 }
804
805 void SetFrameSizeInBytes(size_t new_frame_size_in_bytes) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700806 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700807 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, frame_size_in_bytes_),
808 new_frame_size_in_bytes, false);
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700809 }
810
Shih-wei Liaod11af152011-08-23 16:02:11 -0700811 size_t GetReturnPcOffsetInBytes() const {
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 return GetFrameSizeInBytes() - kPointerSize;
buzbeec143c552011-08-20 17:38:58 -0700813 }
814
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700815 bool IsRegistered() const;
Elliott Hughesd369bb72011-09-12 14:41:14 -0700816
Ian Rogers60db5ab2012-02-20 17:02:00 -0800817 void RegisterNative(Thread* self, const void* native_method);
Ian Rogersb033c752011-07-20 12:22:35 -0700818
Ian Rogers19846512012-02-24 11:42:47 -0800819 void UnregisterNative(Thread* self);
Elliott Hughes5174fe62011-08-23 15:12:35 -0700820
Ian Rogersb033c752011-07-20 12:22:35 -0700821 static MemberOffset NativeMethodOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700822 return OFFSET_OF_OBJECT_MEMBER(Method, native_method_);
Ian Rogersb033c752011-07-20 12:22:35 -0700823 }
824
Brian Carlstrom78128a62011-09-15 17:21:19 -0700825 const void* GetNativeMethod() const {
826 return reinterpret_cast<const void*>(GetField32(NativeMethodOffset(), false));
827 }
828
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700829 // Native to managed invocation stub entry point
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700830 const InvokeStub* GetInvokeStub() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700831 InvokeStub* result = GetFieldPtr<InvokeStub*>(
832 OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_), false);
833 // TODO: DCHECK(result != NULL); should be ahead of time compiled
834 return result;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700835 }
836
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700837 void SetInvokeStub(InvokeStub* invoke_stub) {
838 SetFieldPtr<const InvokeStub*>(OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_),
839 invoke_stub, false);
840 }
841
Brian Carlstromf8bbb842012-03-14 03:01:42 -0700842 uint32_t GetInvokeStubSize() const {
843 const uint32_t* invoke_stub = reinterpret_cast<const uint32_t*>(GetInvokeStub());
844 if (invoke_stub == NULL) {
845 return 0;
846 }
847 return invoke_stub[-1];
848 }
849
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700850 uint32_t GetOatInvokeStubOffset() const {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700851 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700852 return reinterpret_cast<uint32_t>(GetInvokeStub());
853 }
854
855 void SetOatInvokeStubOffset(uint32_t invoke_stub_offset) {
Elliott Hughes307f75d2011-10-12 18:04:40 -0700856 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700857 SetInvokeStub(reinterpret_cast<InvokeStub*>(invoke_stub_offset));
858 }
859
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700860 static MemberOffset GetInvokeStubOffset() {
861 return OFFSET_OF_OBJECT_MEMBER(Method, invoke_stub_);
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700862 }
863
buzbee561227c2011-09-02 15:28:19 -0700864 static MemberOffset GetMethodIndexOffset() {
865 return OFFSET_OF_OBJECT_MEMBER(Method, method_index_);
866 }
867
Ian Rogers90865722011-09-19 11:11:44 -0700868 uint32_t GetCoreSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700869 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700870 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700871
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700872 void SetCoreSpillMask(uint32_t core_spill_mask) {
873 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700874 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, core_spill_mask_), core_spill_mask, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700875 }
876
Ian Rogers90865722011-09-19 11:11:44 -0700877 uint32_t GetFpSpillMask() const {
Ian Rogersbdb03912011-09-14 00:55:44 -0700878 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), false);
879 }
880
881 void SetFpSpillMask(uint32_t fp_spill_mask) {
882 // Computed during compilation
Elliott Hughes418d20f2011-09-22 14:00:39 -0700883 SetField32(OFFSET_OF_OBJECT_MEMBER(Method, fp_spill_mask_), fp_spill_mask, false);
Ian Rogersbdb03912011-09-14 00:55:44 -0700884 }
885
Ian Rogers57b86d42012-03-27 16:05:41 -0700886 // Is this a CalleSaveMethod or ResolutionMethod and therefore doesn't adhere to normal
Ian Rogers640495b2012-06-22 15:15:47 -0700887 // conventions for a method of managed code. Returns false for Proxy methods.
Ian Rogers57b86d42012-03-27 16:05:41 -0700888 bool IsRuntimeMethod() const {
889 return GetDexMethodIndex() == DexFile::kDexNoIndex16;
890 }
891
Ian Rogers90865722011-09-19 11:11:44 -0700892 // Is this a hand crafted method used for something like describing callee saves?
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700893 bool IsCalleeSaveMethod() const {
Ian Rogers57b86d42012-03-27 16:05:41 -0700894 if (!IsRuntimeMethod()) {
895 return false;
896 }
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700897 Runtime* runtime = Runtime::Current();
898 bool result = false;
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700899 for (int i = 0; i < Runtime::kLastCalleeSaveType; i++) {
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700900 if (this == runtime->GetCalleeSaveMethod(Runtime::CalleeSaveType(i))) {
901 result = true;
902 break;
903 }
904 }
Ian Rogers90865722011-09-19 11:11:44 -0700905 return result;
906 }
907
Ian Rogers19846512012-02-24 11:42:47 -0800908 bool IsResolutionMethod() const {
909 bool result = this == Runtime::Current()->GetResolutionMethod();
910 // Check that if we do think it is phony it looks like the resolution method
911 DCHECK(!result || GetDexMethodIndex() == DexFile::kDexNoIndex16);
912 return result;
913 }
914
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700915 // Converts a native PC to a dex PC. TODO: this is a no-op
916 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700917 uint32_t ToDexPC(const uintptr_t pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700918
919 // Converts a dex PC to a native PC. TODO: this is a no-op
920 // until we associate a PC mapping table with each method.
Ian Rogersbdb03912011-09-14 00:55:44 -0700921 uintptr_t ToNativePC(const uint32_t dex_pc) const;
922
923 // Find the catch block for the given exception type and dex_pc
924 uint32_t FindCatchBlock(Class* exception_type, uint32_t dex_pc) const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700925
Elliott Hughes80609252011-09-23 17:24:51 -0700926 static void SetClasses(Class* java_lang_reflect_Constructor, Class* java_lang_reflect_Method);
927
928 static Class* GetConstructorClass() {
929 return java_lang_reflect_Constructor_;
930 }
931
932 static Class* GetMethodClass() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700933 return java_lang_reflect_Method_;
934 }
935
Elliott Hughes80609252011-09-23 17:24:51 -0700936 static void ResetClasses();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700937
938 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700939 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800940 // The class we are a part of
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700941 Class* declaring_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700942
Brian Carlstrom693267a2011-09-06 09:25:34 -0700943 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800944 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700945
946 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Ian Rogers19846512012-02-24 11:42:47 -0800947 ObjectArray<Class>* dex_cache_resolved_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -0700948
949 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
Brian Carlstromdbc05252011-09-09 01:59:59 -0700950 ObjectArray<Class>* dex_cache_resolved_types_;
951
952 // short cuts to declaring_class_->dex_cache_ member for fast compiled code access
953 ObjectArray<String>* dex_cache_strings_;
954
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700955 // Access flags; low 16 bits are defined by spec.
Brian Carlstromdbc05252011-09-09 01:59:59 -0700956 uint32_t access_flags_;
957
958 // Compiled code associated with this method for callers from managed code.
959 // May be compiled managed code or a bridge for invoking a native method.
960 const void* code_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700961
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700962 // Offset to the CodeItem.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700963 uint32_t code_item_offset_;
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700964
Brian Carlstrom693267a2011-09-06 09:25:34 -0700965 // Architecture-dependent register spill mask
Brian Carlstromdbc05252011-09-09 01:59:59 -0700966 uint32_t core_spill_mask_;
967
968 // Architecture-dependent register spill mask
Brian Carlstrom693267a2011-09-06 09:25:34 -0700969 uint32_t fp_spill_mask_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700970
Brian Carlstrom693267a2011-09-06 09:25:34 -0700971 // Total size in bytes of the frame
972 size_t frame_size_in_bytes_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800974 // Garbage collection map
975 const uint8_t* gc_map_;
976
Brian Carlstrom693267a2011-09-06 09:25:34 -0700977 // Native invocation stub entry point for calling from native to managed code.
978 const InvokeStub* invoke_stub_;
buzbee4ef76522011-09-08 10:00:32 -0700979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 // Mapping from native pc to dex pc
Brian Carlstrom3320cf42011-10-04 14:58:28 -0700981 const uint32_t* mapping_table_;
982
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800983 // Index into method_ids of the dex file associated with this method
984 uint32_t method_dex_index_;
985
Ian Rogers466bb252011-10-14 03:29:56 -0700986 // For concrete virtual methods, this is the offset of the method in Class::vtable_.
Brian Carlstrom693267a2011-09-06 09:25:34 -0700987 //
Ian Rogers466bb252011-10-14 03:29:56 -0700988 // For abstract methods in an interface class, this is the offset of the method in
989 // "iftable_->Get(n)->GetMethodArray()".
Ian Rogers19846512012-02-24 11:42:47 -0800990 //
991 // For static and direct methods this is the index in the direct methods table.
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700992 uint32_t method_index_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700993
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700994 // The target native method registered with this method
Ian Rogersb033c752011-07-20 12:22:35 -0700995 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700996
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 // When a register is promoted into a register, the spill mask holds which registers hold dex
998 // registers. The first promoted register's corresponding dex register is vmap_table_[1], the Nth
999 // is vmap_table_[N]. vmap_table_[0] holds the length of the table.
Brian Carlstrom3320cf42011-10-04 14:58:28 -07001000 const uint16_t* vmap_table_;
1001
Elliott Hughes80609252011-09-23 17:24:51 -07001002 static Class* java_lang_reflect_Constructor_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001003 static Class* java_lang_reflect_Method_;
1004
Brian Carlstrom693267a2011-09-06 09:25:34 -07001005 friend class ImageWriter; // for relocating code_ and invoke_stub_
1006 friend struct MethodOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001007 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001008};
1009
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001010class MANAGED Array : public Object {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001011 public:
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001012 // A convenience for code that doesn't know the component size,
1013 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001014 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -07001015
Brian Carlstromb63ec392011-08-27 17:38:27 -07001016 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -07001017
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001018 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001019
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001020 int32_t GetLength() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001021 return GetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001022 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001023
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001024 void SetLength(int32_t length) {
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001025 CHECK_GE(length, 0);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001026 SetField32(OFFSET_OF_OBJECT_MEMBER(Array, length_), length, false);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001027 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001028
buzbeec143c552011-08-20 17:38:58 -07001029 static MemberOffset LengthOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001030 return OFFSET_OF_OBJECT_MEMBER(Array, length_);
buzbeec143c552011-08-20 17:38:58 -07001031 }
1032
Ian Rogersa15e67d2012-02-28 13:51:55 -08001033 static MemberOffset DataOffset(size_t component_size) {
1034 if (component_size != sizeof(int64_t)) {
1035 return OFFSET_OF_OBJECT_MEMBER(Array, first_element_);
1036 } else {
1037 // Align longs and doubles.
1038 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_) + 4);
1039 }
buzbeec143c552011-08-20 17:38:58 -07001040 }
1041
Ian Rogersa15e67d2012-02-28 13:51:55 -08001042 void* GetRawData(size_t component_size) {
1043 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1044 return reinterpret_cast<void*>(data);
Elliott Hughesbf86d042011-08-31 17:53:14 -07001045 }
1046
Elliott Hughesa21039c2012-06-21 12:09:25 -07001047 const void* GetRawData(size_t component_size) const {
1048 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(component_size).Int32Value();
1049 return reinterpret_cast<const void*>(data);
1050 }
1051
Elliott Hughes289da822011-08-16 10:11:20 -07001052 protected:
1053 bool IsValidIndex(int32_t index) const {
Ian Rogerscaab8c42011-10-12 12:11:18 -07001054 if (UNLIKELY(index < 0 || index >= length_)) {
Elliott Hughes80609252011-09-23 17:24:51 -07001055 return ThrowArrayIndexOutOfBoundsException(index);
Elliott Hughes289da822011-08-16 10:11:20 -07001056 }
1057 return true;
1058 }
1059
Elliott Hughes80609252011-09-23 17:24:51 -07001060 protected:
1061 bool ThrowArrayIndexOutOfBoundsException(int32_t index) const;
1062 bool ThrowArrayStoreException(Object* object) const;
1063
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001064 private:
1065 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -07001066 int32_t length_;
buzbeec143c552011-08-20 17:38:58 -07001067 // Marker for the data (used by generated code)
1068 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001069
Carl Shapirof88c9522011-08-06 15:47:38 -07001070 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001071};
1072
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001073template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001074class MANAGED ObjectArray : public Array {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001075 public:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001076 static ObjectArray<T>* Alloc(Class* object_array_class, int32_t length);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001077
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001078 T* Get(int32_t i) const;
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001079
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001080 void Set(int32_t i, T* object);
Jesse Wilsondf4189c2011-08-09 17:10:28 -04001081
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001082 // Set element without bound and element type checks, to be used in limited
1083 // circumstances, such as during boot image writing
1084 void SetWithoutChecks(int32_t i, T* object);
Carl Shapirof88c9522011-08-06 15:47:38 -07001085
Ian Rogers5d76c432011-10-31 21:42:49 -07001086 T* GetWithoutChecks(int32_t i) const;
1087
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001088 static void Copy(const ObjectArray<T>* src, int src_pos,
Carl Shapirof88c9522011-08-06 15:47:38 -07001089 ObjectArray<T>* dst, int dst_pos,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001090 size_t length);
Carl Shapirof88c9522011-08-06 15:47:38 -07001091
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001092 ObjectArray<T>* CopyOf(int32_t new_length);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001093
1094 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001095 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001096};
1097
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001098template<class T>
1099ObjectArray<T>* ObjectArray<T>::Alloc(Class* object_array_class, int32_t length) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08001100 Array* array = Array::Alloc(object_array_class, length, sizeof(Object*));
Ian Rogersc8b306f2012-02-17 21:34:44 -08001101 if (UNLIKELY(array == NULL)) {
1102 return NULL;
1103 } else {
1104 return array->AsObjectArray<T>();
1105 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001106}
1107
1108template<class T>
1109T* ObjectArray<T>::Get(int32_t i) const {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001110 if (UNLIKELY(!IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001111 return NULL;
1112 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08001113 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001114 return GetFieldObject<T*>(data_offset, false);
1115}
1116
1117template<class T>
1118ObjectArray<T>* ObjectArray<T>::CopyOf(int32_t new_length) {
1119 ObjectArray<T>* new_array = Alloc(GetClass(), new_length);
1120 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
1121 return new_array;
1122}
1123
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001124// Type for the InitializedStaticStorage table. Currently the Class
Brian Carlstrom848a4b32011-09-04 11:29:27 -07001125// provides the static storage. However, this might change to an Array
1126// to improve image sharing, so we use this type to avoid assumptions
1127// on the current storage.
Elliott Hughes48257562012-06-06 17:42:44 -07001128class MANAGED StaticStorageBase : public Object {
1129};
Brian Carlstrom1caa2c22011-08-28 13:02:33 -07001130
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001131// C++ mirror of java.lang.Class
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07001132class MANAGED Class : public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001133 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001134 // Class Status
1135 //
1136 // kStatusNotReady: If a Class cannot be found in the class table by
1137 // FindClass, it allocates an new one with AllocClass in the
1138 // kStatusNotReady and calls LoadClass. Note if it does find a
1139 // class, it may not be kStatusResolved and it will try to push it
1140 // forward toward kStatusResolved.
1141 //
1142 // kStatusIdx: LoadClass populates with Class with information from
1143 // the DexFile, moving the status to kStatusIdx, indicating that the
Ian Rogersd418eda2012-01-30 12:14:28 -08001144 // Class value in super_class_ has not been populated. The new Class
1145 // can then be inserted into the classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001146 //
1147 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
1148 // attempt to move a kStatusIdx class forward to kStatusLoaded by
Ian Rogersd418eda2012-01-30 12:14:28 -08001149 // using ResolveClass to initialize the super_class_ and ensuring the
1150 // interfaces are resolved.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001151 //
1152 // kStatusResolved: Still holding the lock on Class, the ClassLinker
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001153 // shows linking is complete and fields of the Class populated by making
1154 // it kStatusResolved. Java allows circularities of the form where a super
1155 // class has a field that is of the type of the sub class. We need to be able
1156 // to fully resolve super classes while resolving types for fields.
jeffhaof1e6b7c2012-06-05 18:33:30 -07001157 //
1158 // kStatusRetryVerificationAtRuntime: The verifier sets a class to
1159 // this state if it encounters a soft failure at compile time. This
1160 // often happens when there are unresolved classes in other dex
1161 // files, and this status marks a class as needing to be verified
1162 // again at runtime.
1163 //
1164 // TODO: Explain the other states
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001165 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001166 kStatusError = -1,
1167 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001168 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001169 kStatusLoaded = 2, // DEX idx values resolved
1170 kStatusResolved = 3, // part of linking
jeffhaoa9b3bf42012-06-06 17:18:39 -07001171 kStatusVerifying = 4, // in the process of being verified
1172 kStatusRetryVerificationAtRuntime = 5, // compile time verification failed, retry at runtime
jeffhaof1e6b7c2012-06-05 18:33:30 -07001173 kStatusVerified = 6, // logically part of linking; done pre-init
1174 kStatusInitializing = 7, // class init in progress
1175 kStatusInitialized = 8, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -07001176 };
1177
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001178 Status GetStatus() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001179 DCHECK_EQ(sizeof(Status), sizeof(uint32_t));
1180 return static_cast<Status>(GetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001181 }
1182
1183 void SetStatus(Status new_status);
1184
1185 // Returns true if the class has failed to link.
1186 bool IsErroneous() const {
1187 return GetStatus() == kStatusError;
1188 }
1189
1190 // Returns true if the class has been loaded.
1191 bool IsIdxLoaded() const {
1192 return GetStatus() >= kStatusIdx;
1193 }
1194
1195 // Returns true if the class has been loaded.
1196 bool IsLoaded() const {
1197 return GetStatus() >= kStatusLoaded;
1198 }
1199
1200 // Returns true if the class has been linked.
Elliott Hughes5fe594f2011-09-08 12:33:17 -07001201 bool IsResolved() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001202 return GetStatus() >= kStatusResolved;
1203 }
1204
jeffhaof1e6b7c2012-06-05 18:33:30 -07001205 // Returns true if the class was compile-time verified.
1206 bool IsCompileTimeVerified() const {
1207 return GetStatus() >= kStatusRetryVerificationAtRuntime;
1208 }
1209
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001210 // Returns true if the class has been verified.
1211 bool IsVerified() const {
1212 return GetStatus() >= kStatusVerified;
1213 }
1214
Brian Carlstrom5d40f182011-09-26 22:29:18 -07001215 // Returns true if the class is initializing.
1216 bool IsInitializing() const {
1217 return GetStatus() >= kStatusInitializing;
1218 }
1219
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001220 // Returns true if the class is initialized.
1221 bool IsInitialized() const {
1222 return GetStatus() == kStatusInitialized;
1223 }
1224
1225 uint32_t GetAccessFlags() const;
1226
1227 void SetAccessFlags(uint32_t new_access_flags) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001228 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), new_access_flags, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001229 }
1230
1231 // Returns true if the class is an interface.
1232 bool IsInterface() const {
1233 return (GetAccessFlags() & kAccInterface) != 0;
1234 }
1235
1236 // Returns true if the class is declared public.
1237 bool IsPublic() const {
1238 return (GetAccessFlags() & kAccPublic) != 0;
1239 }
1240
1241 // Returns true if the class is declared final.
1242 bool IsFinal() const {
1243 return (GetAccessFlags() & kAccFinal) != 0;
1244 }
1245
Elliott Hughes20cde902011-10-04 17:37:27 -07001246 bool IsFinalizable() const {
1247 return (GetAccessFlags() & kAccClassIsFinalizable) != 0;
1248 }
1249
1250 void SetFinalizable() {
1251 uint32_t flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1252 SetAccessFlags(flags | kAccClassIsFinalizable);
1253 }
1254
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001255 // Returns true if the class is abstract.
1256 bool IsAbstract() const {
1257 return (GetAccessFlags() & kAccAbstract) != 0;
1258 }
1259
1260 // Returns true if the class is an annotation.
1261 bool IsAnnotation() const {
1262 return (GetAccessFlags() & kAccAnnotation) != 0;
1263 }
1264
1265 // Returns true if the class is synthetic.
1266 bool IsSynthetic() const {
1267 return (GetAccessFlags() & kAccSynthetic) != 0;
1268 }
1269
1270 bool IsReferenceClass() const {
1271 return (GetAccessFlags() & kAccClassIsReference) != 0;
1272 }
1273
1274 bool IsWeakReferenceClass() const {
1275 return (GetAccessFlags() & kAccClassIsWeakReference) != 0;
1276 }
1277
1278 bool IsSoftReferenceClass() const {
Brian Carlstrom0796af02011-10-12 14:31:45 -07001279 return (GetAccessFlags() & kAccReferenceFlagsMask) == kAccClassIsReference;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001280 }
1281
1282 bool IsFinalizerReferenceClass() const {
1283 return (GetAccessFlags() & kAccClassIsFinalizerReference) != 0;
1284 }
1285
1286 bool IsPhantomReferenceClass() const {
1287 return (GetAccessFlags() & kAccClassIsPhantomReference) != 0;
1288 }
1289
Ian Rogersd418eda2012-01-30 12:14:28 -08001290
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001291 String* GetName() const; // Returns the cached name
Ian Rogersd418eda2012-01-30 12:14:28 -08001292 void SetName(String* name); // Sets the cached name
1293 String* ComputeName(); // Computes the name, then sets the cached value
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001294
1295 bool IsProxyClass() const {
1296 // Read access flags without using getter as whether something is a proxy can be check in
1297 // any loaded state
1298 // TODO: switch to a check if the super class is java.lang.reflect.Proxy?
1299 uint32_t access_flags = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
1300 return (access_flags & kAccClassIsProxy) != 0;
1301 }
1302
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001303 Primitive::Type GetPrimitiveType() const {
1304 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
1305 return static_cast<Primitive::Type>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001306 GetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), false));
1307 }
1308
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001309 void SetPrimitiveType(Primitive::Type new_type) {
1310 DCHECK_EQ(sizeof(Primitive::Type), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001311 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, primitive_type_), new_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001312 }
1313
1314 // Returns true if the class is a primitive type.
1315 bool IsPrimitive() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001316 return GetPrimitiveType() != Primitive::kPrimNot;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001317 }
1318
1319 bool IsPrimitiveBoolean() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001320 return GetPrimitiveType() == Primitive::kPrimBoolean;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001321 }
1322
1323 bool IsPrimitiveByte() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001324 return GetPrimitiveType() == Primitive::kPrimByte;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 }
1326
1327 bool IsPrimitiveChar() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001328 return GetPrimitiveType() == Primitive::kPrimChar;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001329 }
1330
1331 bool IsPrimitiveShort() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001332 return GetPrimitiveType() == Primitive::kPrimShort;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001333 }
1334
1335 bool IsPrimitiveInt() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001336 return GetPrimitiveType() == Primitive::kPrimInt;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001337 }
1338
1339 bool IsPrimitiveLong() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001340 return GetPrimitiveType() == Primitive::kPrimLong;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001341 }
1342
1343 bool IsPrimitiveFloat() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001344 return GetPrimitiveType() == Primitive::kPrimFloat;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001345 }
1346
1347 bool IsPrimitiveDouble() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001348 return GetPrimitiveType() == Primitive::kPrimDouble;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001349 }
1350
1351 bool IsPrimitiveVoid() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001352 return GetPrimitiveType() == Primitive::kPrimVoid;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001353 }
1354
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 // Depth of class from java.lang.Object
1356 size_t Depth() {
1357 size_t depth = 0;
Elliott Hughesff17f1f2012-01-24 18:12:29 -08001358 for (Class* klass = this; klass->GetSuperClass() != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 depth++;
1360 }
1361 return depth;
1362 }
1363
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001364 bool IsArrayClass() const {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001365 return GetComponentType() != NULL;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001366 }
1367
Elliott Hughesdbb40792011-11-18 17:05:22 -08001368 bool IsClassClass() const;
1369
1370 bool IsStringClass() const;
1371
Ian Rogers6f1dfe42011-12-08 17:28:34 -08001372 bool IsThrowableClass() const;
1373
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001374 Class* GetComponentType() const {
Elliott Hughesb0663112011-10-19 18:16:37 -07001375 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001376 }
1377
1378 void SetComponentType(Class* new_component_type) {
1379 DCHECK(GetComponentType() == NULL);
1380 DCHECK(new_component_type != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001381 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, component_type_), new_component_type, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001382 }
1383
1384 size_t GetComponentSize() const {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001385 return Primitive::ComponentSize(GetComponentType()->GetPrimitiveType());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001386 }
1387
1388 bool IsObjectClass() const {
1389 return !IsPrimitive() && GetSuperClass() == NULL;
1390 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001391 bool IsInstantiable() const {
1392 return !IsPrimitive() && !IsInterface() && !IsAbstract();
1393 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001394
Brian Carlstrom1f870082011-08-23 16:02:11 -07001395 // Creates a raw object instance but does not invoke the default constructor.
1396 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001397
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001398 bool IsVariableSize() const {
1399 // Classes and arrays vary in size, and so the object_size_ field cannot
1400 // be used to get their instance size
1401 return IsClassClass() || IsArrayClass();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001402 }
1403
Brian Carlstrom4873d462011-08-21 15:23:39 -07001404 size_t SizeOf() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001405 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001406 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Brian Carlstrom4873d462011-08-21 15:23:39 -07001407 }
1408
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001409 size_t GetClassSize() const {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001410 DCHECK_EQ(sizeof(size_t), sizeof(uint32_t));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001411 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), false);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001412 }
1413
Ian Rogers0571d352011-11-03 19:51:38 -07001414 void SetClassSize(size_t new_class_size);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001415
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001416 size_t GetObjectSize() const {
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001417 CHECK(!IsVariableSize()) << " class=" << PrettyTypeOf(this);
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001418 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Elliott Hughes54e7df12011-09-16 11:47:04 -07001419 size_t result = GetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), false);
Jesse Wilson1121e0b2011-11-07 15:37:42 -05001420 CHECK_GE(result, sizeof(Object)) << " class=" << PrettyTypeOf(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001421 return result;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001422 }
1423
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001424 void SetObjectSize(size_t new_object_size) {
1425 DCHECK(!IsVariableSize());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001426 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001427 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, object_size_), new_object_size, false);
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001428 }
1429
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001430 // Returns true if this class is in the same packages as that class.
1431 bool IsInSamePackage(const Class* that) const;
1432
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001433 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001434
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001435 // Returns true if this class can access that class.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001436 bool CanAccess(Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001437 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001438 }
1439
Ian Rogersf2391652011-12-14 12:50:52 -08001440 // Can this class access a member in the provided class with the provided member access flags?
Ian Rogersc2b44472011-12-14 21:17:17 -08001441 // Note that access to the class isn't checked in case the declaring class is protected and the
1442 // method has been exposed by a public sub-class
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001443 bool CanAccessMember(Class* access_to, uint32_t member_flags) const {
Ian Rogersf2391652011-12-14 12:50:52 -08001444 // Classes can access all of their own members
jeffhao4a801a42011-09-23 13:53:40 -07001445 if (this == access_to) {
1446 return true;
1447 }
Ian Rogersf2391652011-12-14 12:50:52 -08001448 // Public members are trivially accessible
1449 if (member_flags & kAccPublic) {
1450 return true;
1451 }
1452 // Private members are trivially not accessible
jeffhao4a801a42011-09-23 13:53:40 -07001453 if (member_flags & kAccPrivate) {
1454 return false;
1455 }
Ian Rogersf2391652011-12-14 12:50:52 -08001456 // Check for protected access from a sub-class, which may or may not be in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001457 if (member_flags & kAccProtected) {
1458 if (this->IsSubClass(access_to)) {
1459 return true;
1460 }
1461 }
Ian Rogersf2391652011-12-14 12:50:52 -08001462 // Allow protected access from other classes in the same package.
jeffhao4a801a42011-09-23 13:53:40 -07001463 return this->IsInSamePackage(access_to);
1464 }
1465
Brian Carlstromf91c8c32011-09-21 17:30:34 -07001466 bool IsSubClass(const Class* klass) const;
1467
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 // Can src be assigned to this class? For example, String can be assigned to Object (by an
1469 // upcast), however, an Object cannot be assigned to a String as a potentially exception throwing
1470 // downcast would be necessary. Similarly for interfaces, a class that implements (or an interface
1471 // that extends) another can be assigned to its parent, but not vice-versa. All Classes may assign
1472 // to themselves. Classes for primitive types may not assign to each other.
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001473 bool IsAssignableFrom(const Class* src) const {
1474 DCHECK(src != NULL);
1475 if (this == src) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001476 // Can always assign to things of the same type
1477 return true;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001478 } else if (IsObjectClass()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001479 // Can assign any reference to java.lang.Object
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001480 return !src->IsPrimitive();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001481 } else if (IsInterface()) {
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001482 return src->Implements(this);
1483 } else if (src->IsArrayClass()) {
1484 return IsAssignableFromArray(src);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001485 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 return !src->IsInterface() && src->IsSubClass(this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001487 }
1488 }
Elliott Hughesbf86d042011-08-31 17:53:14 -07001489
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001490 Class* GetSuperClass() const {
1491 // Can only get super class for loaded classes (hack for when runtime is
1492 // initializing)
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08001493 DCHECK(IsLoaded() || !Runtime::Current()->IsStarted()) << IsLoaded();
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001494 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001495 }
1496
1497 void SetSuperClass(Class *new_super_class) {
1498 // super class is assigned once, except during class linker initialization
1499 Class* old_super_class = GetFieldObject<Class*>(
1500 OFFSET_OF_OBJECT_MEMBER(Class, super_class_), false);
1501 DCHECK(old_super_class == NULL || old_super_class == new_super_class);
1502 DCHECK(new_super_class != NULL);
Brian Carlstrome24fa612011-09-29 00:53:55 -07001503 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, super_class_), new_super_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001504 }
1505
1506 bool HasSuperClass() const {
1507 return GetSuperClass() != NULL;
1508 }
1509
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001510 static MemberOffset SuperClassOffset() {
1511 return MemberOffset(OFFSETOF_MEMBER(Class, super_class_));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001512 }
1513
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001514 ClassLoader* GetClassLoader() const;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001515
1516 void SetClassLoader(const ClassLoader* new_cl);
1517
1518 static MemberOffset DexCacheOffset() {
1519 return MemberOffset(OFFSETOF_MEMBER(Class, dex_cache_));
1520 }
1521
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001522 enum {
1523 kDumpClassFullDetail = 1,
1524 kDumpClassClassLoader = (1 << 1),
1525 kDumpClassInitialized = (1 << 2),
1526 };
1527
Elliott Hughes4681c802011-09-25 18:04:37 -07001528 void DumpClass(std::ostream& os, int flags) const;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001529
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001530 DexCache* GetDexCache() const;
1531
1532 void SetDexCache(DexCache* new_dex_cache);
1533
1534 ObjectArray<Method>* GetDirectMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001535 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001536 return GetFieldObject<ObjectArray<Method>*>(
1537 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1538 }
1539
1540 void SetDirectMethods(ObjectArray<Method>* new_direct_methods) {
1541 DCHECK(NULL == GetFieldObject<ObjectArray<Method>*>(
1542 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false));
1543 DCHECK_NE(0, new_direct_methods->GetLength());
1544 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_),
1545 new_direct_methods, false);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001546 }
1547
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001548 Method* GetDirectMethod(int32_t i) const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001549 return GetDirectMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001550 }
1551
1552 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001553 ObjectArray<Method>* direct_methods =
1554 GetFieldObject<ObjectArray<Method>*>(
1555 OFFSET_OF_OBJECT_MEMBER(Class, direct_methods_), false);
1556 direct_methods->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001557 }
1558
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001559 // Returns the number of static, private, and constructor methods.
1560 size_t NumDirectMethods() const {
1561 return (GetDirectMethods() != NULL) ? GetDirectMethods()->GetLength() : 0;
1562 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001563
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001564 ObjectArray<Method>* GetVirtualMethods() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001565 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001566 return GetFieldObject<ObjectArray<Method>*>(
1567 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1568 }
1569
1570 void SetVirtualMethods(ObjectArray<Method>* new_virtual_methods) {
1571 // TODO: we reassign virtual methods to grow the table for miranda
1572 // methods.. they should really just be assigned once
1573 DCHECK_NE(0, new_virtual_methods->GetLength());
1574 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_),
1575 new_virtual_methods, false);
1576 }
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001577
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001578 // Returns the number of non-inherited virtual methods.
1579 size_t NumVirtualMethods() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001580 return (GetVirtualMethods() != NULL) ? GetVirtualMethods()->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001581 }
1582
1583 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001584 DCHECK(IsResolved() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001585 return GetVirtualMethods()->Get(i);
1586 }
1587
1588 Method* GetVirtualMethodDuringLinking(uint32_t i) const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001589 DCHECK(IsLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001590 return GetVirtualMethods()->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001591 }
1592
1593 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001594 ObjectArray<Method>* virtual_methods =
1595 GetFieldObject<ObjectArray<Method>*>(
1596 OFFSET_OF_OBJECT_MEMBER(Class, virtual_methods_), false);
1597 virtual_methods->Set(i, f);
1598 }
1599
1600 ObjectArray<Method>* GetVTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001601 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001602 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001603 }
1604
1605 ObjectArray<Method>* GetVTableDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001606 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001607 return GetFieldObject<ObjectArray<Method>*>(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001608 }
1609
1610 void SetVTable(ObjectArray<Method>* new_vtable) {
1611 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, vtable_), new_vtable, false);
1612 }
1613
1614 static MemberOffset VTableOffset() {
1615 return OFFSET_OF_OBJECT_MEMBER(Class, vtable_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001616 }
1617
Brian Carlstrom30b94452011-08-25 21:35:26 -07001618 // Given a method implemented by this class but potentially from a
1619 // super class, return the specific implementation
1620 // method for this class.
1621 Method* FindVirtualMethodForVirtual(Method* method) {
1622 DCHECK(!method->GetDeclaringClass()->IsInterface());
1623 // The argument method may from a super class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001624 // Use the index to a potentially overridden one for this instance's class.
1625 return GetVTable()->Get(method->GetMethodIndex());
Brian Carlstrom30b94452011-08-25 21:35:26 -07001626 }
1627
1628 // Given a method implemented by this class, but potentially from a
1629 // super class or interface, return the specific implementation
1630 // method for this class.
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001631 Method* FindVirtualMethodForInterface(Method* method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001632
Ian Rogers466bb252011-10-14 03:29:56 -07001633 Method* FindInterfaceMethod(const StringPiece& name, const StringPiece& descriptor) const;
jeffhaobdb76512011-09-07 11:43:16 -07001634
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001635 Method* FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1636
Brian Carlstrom30b94452011-08-25 21:35:26 -07001637 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
Brian Carlstrom395520e2011-09-25 19:35:00 -07001638 if (method->IsDirect()) {
1639 return method;
1640 }
Brian Carlstrom30b94452011-08-25 21:35:26 -07001641 if (method->GetDeclaringClass()->IsInterface()) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08001642 return FindVirtualMethodForInterface(method);
Brian Carlstrom30b94452011-08-25 21:35:26 -07001643 }
1644 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001645 }
1646
Ian Rogers466bb252011-10-14 03:29:56 -07001647 Method* FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001648
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001649 Method* FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1650
Ian Rogers466bb252011-10-14 03:29:56 -07001651 Method* FindVirtualMethod(const StringPiece& name, const StringPiece& descriptor) const;
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001652
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001653 Method* FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001654
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001655 Method* FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1656
1657 Method* FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
1658
1659 Method* FindDirectMethod(const StringPiece& name, const StringPiece& signature) const;
1660
1661 Method* FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001662
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001663 int32_t GetIfTableCount() const {
1664 ObjectArray<InterfaceEntry>* iftable = GetIfTable();
1665 if (iftable == NULL) {
1666 return 0;
1667 }
1668 return iftable->GetLength();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001669 }
1670
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001671 ObjectArray<InterfaceEntry>* GetIfTable() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001672 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001673 return GetFieldObject<ObjectArray<InterfaceEntry>*>(
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001674 OFFSET_OF_OBJECT_MEMBER(Class, iftable_), false);
1675 }
1676
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001677 void SetIfTable(ObjectArray<InterfaceEntry>* new_iftable) {
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001678 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, iftable_), new_iftable, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001679 }
1680
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001681 // Get instance fields of the class (See also GetSFields).
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001682 ObjectArray<Field>* GetIFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001683 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001684 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001685 }
1686
1687 void SetIFields(ObjectArray<Field>* new_ifields) {
1688 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1689 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001690 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, ifields_), new_ifields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001691 }
1692
1693 size_t NumInstanceFields() const {
1694 return (GetIFields() != NULL) ? GetIFields()->GetLength() : 0;
1695 }
1696
1697 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
1698 DCHECK_NE(NumInstanceFields(), 0U);
1699 return GetIFields()->Get(i);
1700 }
1701
1702 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
1703 ObjectArray<Field>* ifields= GetFieldObject<ObjectArray<Field>*>(
1704 OFFSET_OF_OBJECT_MEMBER(Class, ifields_), false);
1705 ifields->Set(i, f);
1706 }
1707
1708 // Returns the number of instance fields containing reference types.
1709 size_t NumReferenceInstanceFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001710 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001711 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001712 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001713 }
1714
1715 size_t NumReferenceInstanceFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001716 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001717 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001718 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001719 }
1720
1721 void SetNumReferenceInstanceFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001722 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001723 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_instance_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001724 }
1725
1726 uint32_t GetReferenceInstanceOffsets() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001727 DCHECK(IsResolved() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001728 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001729 }
1730
1731 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets);
1732
1733 // Beginning of static field data
1734 static MemberOffset FieldsOffset() {
1735 return OFFSET_OF_OBJECT_MEMBER(Class, fields_);
1736 }
1737
1738 // Returns the number of static fields containing reference types.
1739 size_t NumReferenceStaticFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001740 DCHECK(IsResolved() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001741 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001742 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001743 }
1744
1745 size_t NumReferenceStaticFieldsDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001746 DCHECK(IsLoaded() || IsErroneous());
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001747 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001748 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001749 }
1750
1751 void SetNumReferenceStaticFields(size_t new_num) {
Elliott Hughesf5a7a472011-10-07 14:31:02 -07001752 DCHECK_EQ(sizeof(size_t), sizeof(int32_t));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001753 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, num_reference_static_fields_), new_num, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001754 }
1755
Mathieu Chartier262e5ff2012-06-01 17:35:38 -07001756 // Gets the static fields of the class.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001757 ObjectArray<Field>* GetSFields() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001758 DCHECK(IsLoaded() || IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001759 return GetFieldObject<ObjectArray<Field>*>(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001760 }
1761
1762 void SetSFields(ObjectArray<Field>* new_sfields) {
1763 DCHECK(NULL == GetFieldObject<ObjectArray<Field>*>(
1764 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false));
Brian Carlstrome24fa612011-09-29 00:53:55 -07001765 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, sfields_), new_sfields, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001766 }
1767
1768 size_t NumStaticFields() const {
1769 return (GetSFields() != NULL) ? GetSFields()->GetLength() : 0;
1770 }
1771
1772 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
1773 return GetSFields()->Get(i);
1774 }
1775
1776 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
1777 ObjectArray<Field>* sfields= GetFieldObject<ObjectArray<Field>*>(
1778 OFFSET_OF_OBJECT_MEMBER(Class, sfields_), false);
1779 sfields->Set(i, f);
1780 }
1781
1782 uint32_t GetReferenceStaticOffsets() const {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001783 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001784 }
1785
1786 void SetReferenceStaticOffsets(uint32_t new_reference_offsets);
1787
Ian Rogersb067ac22011-12-13 18:05:09 -08001788 // Find a static or instance field using the JLS resolution order
1789 Field* FindField(const StringPiece& name, const StringPiece& type);
1790
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001791 // Finds the given instance field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001792 Field* FindInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001793
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001794 // Finds the given instance field in this class or a superclass, only searches classes that
1795 // have the same dex cache.
1796 Field* FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1797
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001798 Field* FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001799
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001800 Field* FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx);
1801
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001802 // Finds the given static field in this class or a superclass.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001803 Field* FindStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001804
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001805 // Finds the given static field in this class or superclass, only searches classes that
1806 // have the same dex cache.
1807 Field* FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1808
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001809 Field* FindDeclaredStaticField(const StringPiece& name, const StringPiece& type);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001810
Ian Rogers7b0c5b42012-02-16 15:29:07 -08001811 Field* FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx);
1812
Elliott Hughesdcc24742011-09-07 14:02:44 -07001813 pid_t GetClinitThreadId() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07001814 DCHECK(IsIdxLoaded() || IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001815 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), false);
1816 }
1817
Elliott Hughesdcc24742011-09-07 14:02:44 -07001818 void SetClinitThreadId(pid_t new_clinit_thread_id) {
Brian Carlstrome24fa612011-09-29 00:53:55 -07001819 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, clinit_thread_id_), new_clinit_thread_id, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001820 }
1821
1822 Class* GetVerifyErrorClass() const {
Brian Carlstrom693267a2011-09-06 09:25:34 -07001823 // DCHECK(IsErroneous());
Brian Carlstrome24fa612011-09-29 00:53:55 -07001824 return GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), false);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001825 }
1826
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001827 uint16_t GetDexTypeIndex() const {
1828 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), false);
jeffhao64155032011-11-03 17:56:34 -07001829 }
1830
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001831 void SetDexTypeIndex(uint16_t type_idx) {
1832 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, dex_type_idx_), type_idx, false);
jeffhao64155032011-11-03 17:56:34 -07001833 }
1834
jeffhaobdb76512011-09-07 11:43:16 -07001835 private:
Brian Carlstrom4d9716c2012-01-30 01:49:33 -08001836 void SetVerifyErrorClass(Class* klass) {
1837 CHECK(klass != NULL) << PrettyClass(this);
1838 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_class_), klass, false);
1839 }
1840
jeffhao5dbddee2011-09-07 16:38:26 -07001841 bool Implements(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001842 bool IsArrayAssignableFromArray(const Class* klass) const;
1843 bool IsAssignableFromArray(const Class* klass) const;
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001844
Brian Carlstrom693267a2011-09-06 09:25:34 -07001845 // defining class loader, or NULL for the "bootstrap" system loader
Elliott Hughes1bba14f2011-12-01 18:00:36 -08001846 ClassLoader* class_loader_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001847
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001848 // For array classes, the component class object for instanceof/checkcast
1849 // (for String[][][], this will be String[][]). NULL for non-array classes.
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001850 Class* component_type_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001851
Elliott Hughes81ff3182012-03-23 20:35:56 -07001852 // DexCache of resolved constant pool entries (will be NULL for classes generated by the
1853 // runtime such as arrays and primitive classes).
Brian Carlstrom693267a2011-09-06 09:25:34 -07001854 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001855
1856 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001857 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001858
Brian Carlstrom693267a2011-09-06 09:25:34 -07001859 // instance fields
1860 //
1861 // These describe the layout of the contents of an Object.
1862 // Note that only the fields directly declared by this class are
1863 // listed in ifields; fields declared by a superclass are listed in
1864 // the superclass's Class.ifields.
1865 //
1866 // All instance fields that refer to objects are guaranteed to be at
1867 // the beginning of the field list. num_reference_instance_fields_
1868 // specifies the number of reference fields.
1869 ObjectArray<Field>* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001870
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07001871 // Interface table (iftable_), one entry per interface supported by
1872 // this class. That means one entry for each interface we support
1873 // directly, indirectly via superclass, or indirectly via
1874 // superinterface. This will be null if neither we nor our
1875 // superclass implement any interfaces.
1876 //
1877 // Why we need this: given "class Foo implements Face", declare
1878 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1879 // is part of the Face interface. We can't easily use a single
1880 // vtable.
1881 //
1882 // For every interface a concrete class implements, we create an array
1883 // of the concrete vtable_ methods for the methods in the interface.
1884 ObjectArray<InterfaceEntry>* iftable_;
1885
Ian Rogersd418eda2012-01-30 12:14:28 -08001886 // descriptor for the class such as "java.lang.Class" or "[C". Lazily initialized by ComputeName
1887 String* name_;
1888
Brian Carlstromdbc05252011-09-09 01:59:59 -07001889 // Static fields
1890 ObjectArray<Field>* sfields_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001891
Ian Rogersd418eda2012-01-30 12:14:28 -08001892 // The superclass, or NULL if this is java.lang.Object, an interface or primitive type.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001893 Class* super_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001894
Brian Carlstromdbc05252011-09-09 01:59:59 -07001895 // If class verify fails, we must return same error on subsequent tries.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001896 Class* verify_error_class_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001897
Brian Carlstromdbc05252011-09-09 01:59:59 -07001898 // virtual methods defined in this class; invoked through vtable
1899 ObjectArray<Method>* virtual_methods_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001900
Ian Rogers9074b992011-10-26 17:41:55 -07001901 // Virtual method table (vtable), for use by "invoke-virtual". The vtable from the superclass is
1902 // copied in, and virtual methods from our class either replace those from the super or are
1903 // appended. For abstract classes, methods may be created in the vtable that aren't in
1904 // virtual_ methods_ for miranda methods.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001905 ObjectArray<Method>* vtable_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001906
Brian Carlstromdbc05252011-09-09 01:59:59 -07001907 // access flags; low 16 bits are defined by VM spec
1908 uint32_t access_flags_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001909
Jesse Wilson6bf19152011-09-29 13:12:33 -04001910 // Total size of the Class instance; used when allocating storage on gc heap.
1911 // See also object_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001912 size_t class_size_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07001913
Elliott Hughes5f791332011-09-15 17:45:30 -07001914 // tid used to check for recursive <clinit> invocation
Brian Carlstromdbc05252011-09-09 01:59:59 -07001915 pid_t clinit_thread_id_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001916
Ian Rogersd418eda2012-01-30 12:14:28 -08001917 // type index from dex file
1918 // TODO: really 16bits
1919 uint32_t dex_type_idx_;
1920
Brian Carlstromdbc05252011-09-09 01:59:59 -07001921 // number of instance fields that are object refs
1922 size_t num_reference_instance_fields_;
1923
1924 // number of static fields that are object refs
1925 size_t num_reference_static_fields_;
1926
1927 // Total object size; used when allocating storage on gc heap.
1928 // (For interfaces and abstract classes this will be zero.)
Jesse Wilson6bf19152011-09-29 13:12:33 -04001929 // See also class_size_.
Brian Carlstromdbc05252011-09-09 01:59:59 -07001930 size_t object_size_;
1931
Ian Rogersd418eda2012-01-30 12:14:28 -08001932 // primitive type value, or Primitive::kPrimNot (0); set for generated prim classes
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001933 Primitive::Type primitive_type_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07001934
1935 // Bitmap of offsets of ifields.
1936 uint32_t reference_instance_offsets_;
1937
1938 // Bitmap of offsets of sfields.
1939 uint32_t reference_static_offsets_;
1940
Brian Carlstrom693267a2011-09-06 09:25:34 -07001941 // state of class initialization
1942 Status status_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001943
Brian Carlstrom693267a2011-09-06 09:25:34 -07001944 // TODO: ?
1945 // initiating class loader list
1946 // NOTE: for classes with low serialNumber, these are unused, and the
1947 // values are kept in a table in gDvm.
1948 // InitiatingLoaderList initiating_loader_list_;
1949
Brian Carlstrom4873d462011-08-21 15:23:39 -07001950 // Location of first static field.
1951 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001952
Brian Carlstrom693267a2011-09-06 09:25:34 -07001953 friend struct ClassOffsets; // for verifying offset information
Carl Shapirof88c9522011-08-06 15:47:38 -07001954 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001955};
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001956
Elliott Hughes1f359b02011-07-17 14:27:17 -07001957std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001958
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001959inline void Object::SetClass(Class* new_klass) {
1960 // new_klass may be NULL prior to class linker initialization
Elliott Hughes5ea047b2011-09-13 14:38:18 -07001961 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Object, klass_), new_klass, false, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001962}
1963
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001964inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001965 DCHECK(klass != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001966 DCHECK(GetClass() != NULL);
1967 return klass->IsAssignableFrom(GetClass());
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001968}
1969
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001970inline bool Object::IsClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001971 Class* java_lang_Class = GetClass()->GetClass();
1972 return GetClass() == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001973}
1974
1975inline bool Object::IsObjectArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001976 return IsArrayInstance() && !GetClass()->GetComponentType()->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001977}
1978
Brian Carlstrom34f426c2011-10-04 12:58:02 -07001979template<class T>
1980inline ObjectArray<T>* Object::AsObjectArray() {
1981 DCHECK(IsObjectArray());
1982 return down_cast<ObjectArray<T>*>(this);
1983}
1984
1985template<class T>
1986inline const ObjectArray<T>* Object::AsObjectArray() const {
1987 DCHECK(IsObjectArray());
1988 return down_cast<const ObjectArray<T>*>(this);
1989}
1990
Brian Carlstromb63ec392011-08-27 17:38:27 -07001991inline bool Object::IsArrayInstance() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001992 return GetClass()->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001993}
1994
Brian Carlstroma663ea52011-08-19 23:33:41 -07001995inline bool Object::IsField() const {
1996 Class* java_lang_Class = klass_->klass_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001997 Class* java_lang_reflect_Field =
1998 java_lang_Class->GetInstanceField(0)->GetClass();
1999 return GetClass() == java_lang_reflect_Field;
Brian Carlstroma663ea52011-08-19 23:33:41 -07002000}
2001
2002inline bool Object::IsMethod() const {
Elliott Hughes80609252011-09-23 17:24:51 -07002003 Class* c = GetClass();
2004 return c == Method::GetMethodClass() || c == Method::GetConstructorClass();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002005}
2006
2007inline bool Object::IsReferenceInstance() const {
2008 return GetClass()->IsReferenceClass();
2009}
2010
2011inline bool Object::IsWeakReferenceInstance() const {
2012 return GetClass()->IsWeakReferenceClass();
2013}
2014
2015inline bool Object::IsSoftReferenceInstance() const {
2016 return GetClass()->IsSoftReferenceClass();
2017}
2018
2019inline bool Object::IsFinalizerReferenceInstance() const {
2020 return GetClass()->IsFinalizerReferenceClass();
2021}
2022
2023inline bool Object::IsPhantomReferenceInstance() const {
2024 return GetClass()->IsPhantomReferenceClass();
Brian Carlstroma663ea52011-08-19 23:33:41 -07002025}
2026
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002027inline size_t Object::SizeOf() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002028 size_t result;
Brian Carlstromb63ec392011-08-27 17:38:27 -07002029 if (IsArrayInstance()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002030 result = AsArray()->SizeOf();
2031 } else if (IsClass()) {
2032 result = AsClass()->SizeOf();
2033 } else {
2034 result = GetClass()->GetObjectSize();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002035 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002036 DCHECK(!IsField() || result == sizeof(Field));
2037 DCHECK(!IsMethod() || result == sizeof(Method));
2038 return result;
2039}
2040
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002041inline Class* Field::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002042 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002043 DCHECK(result != NULL);
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002044 DCHECK(result->IsLoaded() || result->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002045 return result;
2046}
2047
2048inline void Field::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002049 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Field, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002050}
2051
2052inline Class* Method::GetDeclaringClass() const {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002053 Class* result = GetFieldObject<Class*>(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), false);
Elliott Hughes7b3cdfc2011-12-08 21:28:17 -08002054 DCHECK(result != NULL) << this;
2055 DCHECK(result->IsIdxLoaded() || result->IsErroneous()) << this;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002056 return result;
2057}
2058
2059inline void Method::SetDeclaringClass(Class *new_declaring_class) {
Elliott Hughes06b37d92011-10-16 11:51:29 -07002060 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Method, declaring_class_), new_declaring_class, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002061}
2062
Elliott Hughes04b63fd2011-08-16 09:40:10 -07002063inline size_t Array::SizeOf() const {
Elliott Hughesb408de72011-10-04 14:35:05 -07002064 // This is safe from overflow because the array was already allocated, so we know it's sane.
Ian Rogersa15e67d2012-02-28 13:51:55 -08002065 size_t component_size = GetClass()->GetComponentSize();
2066 int32_t component_count = GetLength();
2067 size_t header_size = sizeof(Object) + (component_size == sizeof(int64_t) ? 8 : 4);
2068 size_t data_size = component_count * component_size;
2069 return header_size + data_size;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002070}
2071
2072template<class T>
2073void ObjectArray<T>::Set(int32_t i, T* object) {
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002074 if (LIKELY(IsValidIndex(i))) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002075 if (object != NULL) {
2076 Class* element_class = GetClass()->GetComponentType();
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002077 if (UNLIKELY(!object->InstanceOf(element_class))) {
Elliott Hughes80609252011-09-23 17:24:51 -07002078 ThrowArrayStoreException(object);
2079 return;
2080 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002081 }
Ian Rogersa15e67d2012-02-28 13:51:55 -08002082 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002083 SetFieldObject(data_offset, object, false);
2084 }
2085}
2086
2087template<class T>
2088void ObjectArray<T>::SetWithoutChecks(int32_t i, T* object) {
2089 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002090 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002091 SetFieldObject(data_offset, object, false);
2092}
2093
2094template<class T>
Ian Rogers5d76c432011-10-31 21:42:49 -07002095T* ObjectArray<T>::GetWithoutChecks(int32_t i) const {
2096 DCHECK(IsValidIndex(i));
Ian Rogersa15e67d2012-02-28 13:51:55 -08002097 MemberOffset data_offset(DataOffset(sizeof(Object*)).Int32Value() + i * sizeof(Object*));
Ian Rogers5d76c432011-10-31 21:42:49 -07002098 return GetFieldObject<T*>(data_offset, false);
2099}
2100
2101template<class T>
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002102void ObjectArray<T>::Copy(const ObjectArray<T>* src, int src_pos,
2103 ObjectArray<T>* dst, int dst_pos,
2104 size_t length) {
2105 if (src->IsValidIndex(src_pos) &&
2106 src->IsValidIndex(src_pos+length-1) &&
2107 dst->IsValidIndex(dst_pos) &&
2108 dst->IsValidIndex(dst_pos+length-1)) {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002109 MemberOffset src_offset(DataOffset(sizeof(Object*)).Int32Value() + src_pos * sizeof(Object*));
2110 MemberOffset dst_offset(DataOffset(sizeof(Object*)).Int32Value() + dst_pos * sizeof(Object*));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002111 Class* array_class = dst->GetClass();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002112 Heap* heap = Runtime::Current()->GetHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002113 if (array_class == src->GetClass()) {
2114 // No need for array store checks if arrays are of the same type
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002115 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002116 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002117 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002118 // directly set field, we do a bulk write barrier at the end
2119 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002120 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2121 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2122 }
2123 } else {
2124 Class* element_class = array_class->GetComponentType();
2125 CHECK(!element_class->IsPrimitive());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002126 for (size_t i = 0; i < length; i++) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002127 Object* object = src->GetFieldObject<Object*>(src_offset, false);
Elliott Hughes80609252011-09-23 17:24:51 -07002128 if (object != NULL && !object->InstanceOf(element_class)) {
2129 dst->ThrowArrayStoreException(object);
2130 return;
2131 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002132 heap->VerifyObject(object);
Ian Rogers5d76c432011-10-31 21:42:49 -07002133 // directly set field, we do a bulk write barrier at the end
2134 dst->SetField32(dst_offset, reinterpret_cast<uint32_t>(object), false, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002135 src_offset = MemberOffset(src_offset.Uint32Value() + sizeof(Object*));
2136 dst_offset = MemberOffset(dst_offset.Uint32Value() + sizeof(Object*));
2137 }
2138 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08002139 heap->WriteBarrierArray(dst, dst_pos, length);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002140 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07002141}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07002142
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002143class MANAGED ClassClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002144 private:
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002145 int32_t padding_;
Brian Carlstrom4873d462011-08-21 15:23:39 -07002146 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002147 friend struct ClassClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002148 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
2149};
2150
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002151class MANAGED StringClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002152 private:
2153 CharArray* ASCII_;
2154 Object* CASE_INSENSITIVE_ORDER_;
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002155 uint32_t REPLACEMENT_CHAR_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002156 int64_t serialVersionUID_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002157 friend struct StringClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002158 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
2159};
2160
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002161class MANAGED FieldClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002162 private:
2163 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
Brian Carlstromdbc05252011-09-09 01:59:59 -07002164 friend struct FieldClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002165 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
2166};
2167
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002168class MANAGED MethodClass : public Class {
Brian Carlstrom4873d462011-08-21 15:23:39 -07002169 private:
Brian Carlstromdbc05252011-09-09 01:59:59 -07002170 Object* ORDER_BY_SIGNATURE_;
2171 friend struct MethodClassOffsets; // for verifying offset information
Brian Carlstrom4873d462011-08-21 15:23:39 -07002172 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
2173};
2174
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002175template<class T>
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002176class MANAGED PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002177 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002178 typedef T ElementType;
2179
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002180 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002181
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002182 const T* GetData() const {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002183 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2184 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002185 }
2186
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002187 T* GetData() {
Ian Rogersa15e67d2012-02-28 13:51:55 -08002188 intptr_t data = reinterpret_cast<intptr_t>(this) + DataOffset(sizeof(T)).Int32Value();
2189 return reinterpret_cast<T*>(data);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002190 }
2191
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002192 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07002193 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07002194 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07002195 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07002196 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002197 }
2198
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002199 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07002200 // TODO: ArrayStoreException
2201 if (IsValidIndex(i)) {
2202 GetData()[i] = value;
2203 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07002204 }
2205
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002206 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07002207 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002208 CHECK(array_class != NULL);
2209 array_class_ = array_class;
2210 }
2211
Brian Carlstroma663ea52011-08-19 23:33:41 -07002212 static void ResetArrayClass() {
2213 CHECK(array_class_ != NULL);
2214 array_class_ = NULL;
2215 }
2216
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002217 private:
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07002218 static Class* array_class_;
2219
Carl Shapirof88c9522011-08-06 15:47:38 -07002220 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002221};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07002222
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002223// C++ mirror of java.lang.String
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002224class MANAGED String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002225 public:
buzbeefc9e6fa2012-03-23 15:14:29 -07002226 static MemberOffset CountOffset() {
2227 return OFFSET_OF_OBJECT_MEMBER(String, count_);
2228 }
2229
2230 static MemberOffset ValueOffset() {
2231 return OFFSET_OF_OBJECT_MEMBER(String, array_);
2232 }
2233
2234 static MemberOffset OffsetOffset() {
2235 return OFFSET_OF_OBJECT_MEMBER(String, offset_);
2236 }
2237
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002238 const CharArray* GetCharArray() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002239 const CharArray* result = GetFieldObject<const CharArray*>(
buzbeefc9e6fa2012-03-23 15:14:29 -07002240 ValueOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002241 DCHECK(result != NULL);
2242 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002243 }
2244
Elliott Hughes814e4032011-08-23 12:07:56 -07002245 int32_t GetOffset() const {
buzbeefc9e6fa2012-03-23 15:14:29 -07002246 int32_t result = GetField32(OffsetOffset(), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002247 DCHECK_LE(0, result);
2248 return result;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002249 }
2250
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002251 int32_t GetLength() const;
2252
Brian Carlstrom395520e2011-09-25 19:35:00 -07002253 int32_t GetHashCode();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002254
2255 void ComputeHashCode() {
2256 SetHashCode(ComputeUtf16Hash(GetCharArray(), GetOffset(), GetLength()));
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002257 }
2258
Elliott Hughes814e4032011-08-23 12:07:56 -07002259 int32_t GetUtfLength() const {
jeffhao0ce13152012-03-27 19:45:50 -07002260 return CountUtf8Bytes(GetCharArray()->GetData() + GetOffset(), GetLength());
Elliott Hughes814e4032011-08-23 12:07:56 -07002261 }
2262
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002263 uint16_t CharAt(int32_t index) const;
2264
Brian Carlstromc74255f2011-09-11 22:47:39 -07002265 String* Intern();
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002266
Brian Carlstrom7e93b502011-08-04 14:16:22 -07002267 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07002268 const uint16_t* utf16_data_in,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002269 int32_t hash_code = 0);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07002270
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002271 static String* AllocFromModifiedUtf8(const char* utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002272
Jesse Wilson8989d992011-08-02 13:39:42 -07002273 static String* AllocFromModifiedUtf8(int32_t utf16_length,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002274 const char* utf8_data_in);
2275
2276 static String* Alloc(Class* java_lang_String, int32_t utf16_length);
2277
2278 static String* Alloc(Class* java_lang_String, CharArray* array);
2279
2280 bool Equals(const char* modified_utf8) const;
2281
2282 // TODO: do we need this overload? give it a more intention-revealing name.
2283 bool Equals(const StringPiece& modified_utf8) const;
2284
2285 bool Equals(const String* that) const;
2286
Ian Rogers0571d352011-11-03 19:51:38 -07002287 // Compare UTF-16 code point values not in a locale-sensitive manner
2288 int Compare(int32_t utf16_length, const char* utf8_data_in);
2289
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002290 // TODO: do we need this overload? give it a more intention-revealing name.
2291 bool Equals(const uint16_t* that_chars, int32_t that_offset,
2292 int32_t that_length) const;
2293
2294 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
2295 std::string ToModifiedUtf8() const;
2296
2297 static Class* GetJavaLangString() {
2298 DCHECK(java_lang_String_ != NULL);
2299 return java_lang_String_;
Jesse Wilson8989d992011-08-02 13:39:42 -07002300 }
2301
Brian Carlstroma663ea52011-08-19 23:33:41 -07002302 static void SetClass(Class* java_lang_String);
2303 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002304
Brian Carlstroma7f4f482011-07-17 17:01:34 -07002305 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002306 void SetHashCode(int32_t new_hash_code) {
2307 DCHECK_EQ(0u,
2308 GetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_), false));
2309 SetField32(OFFSET_OF_OBJECT_MEMBER(String, hash_code_),
2310 new_hash_code, false);
2311 }
2312
2313 void SetCount(int32_t new_count) {
2314 DCHECK_LE(0, new_count);
2315 SetField32(OFFSET_OF_OBJECT_MEMBER(String, count_), new_count, false);
2316 }
2317
2318 void SetOffset(int32_t new_offset) {
2319 DCHECK_LE(0, new_offset);
2320 DCHECK_GE(GetLength(), new_offset);
2321 SetField32(OFFSET_OF_OBJECT_MEMBER(String, offset_), new_offset, false);
2322 }
2323
2324 void SetArray(CharArray* new_array) {
2325 DCHECK(new_array != NULL);
2326 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(String, array_), new_array, false);
2327 }
2328
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002329 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2330 CharArray* array_;
2331
Brian Carlstromdbc05252011-09-09 01:59:59 -07002332 int32_t count_;
2333
Carl Shapirof88c9522011-08-06 15:47:38 -07002334 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002335
Elliott Hughes289da822011-08-16 10:11:20 -07002336 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002337
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07002338 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002339
Brian Carlstrom693267a2011-09-06 09:25:34 -07002340 friend struct StringOffsets; // for verifying offset information
jeffhao0ce13152012-03-27 19:45:50 -07002341 FRIEND_TEST(ObjectTest, StringLength); // for SetOffset and SetCount
Carl Shapiro8860c0e2011-08-04 17:36:16 -07002342 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002343};
2344
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002345struct StringHashCode {
Elliott Hughes11d1b0c2012-01-23 16:57:47 -08002346 int32_t operator()(String* string) const {
Jesse Wilsonc4824e62011-11-01 14:39:04 -04002347 return string->GetHashCode();
2348 }
2349};
2350
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002351inline uint32_t Field::GetAccessFlags() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002352 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002353 return GetField32(OFFSET_OF_OBJECT_MEMBER(Field, access_flags_), false);
2354}
2355
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002356inline MemberOffset Field::GetOffset() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002357 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002358 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002359}
2360
2361inline MemberOffset Field::GetOffsetDuringLinking() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002362 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Elliott Hughes362f9bc2011-10-17 18:56:41 -07002363 return MemberOffset(GetField32(OFFSET_OF_OBJECT_MEMBER(Field, offset_), false));
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002364}
2365
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002366inline uint32_t Class::GetAccessFlags() const {
2367 // Check class is loaded or this is java.lang.String that has a
2368 // circularity issue during loading the names of its members
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002369 DCHECK(IsLoaded() || IsErroneous() ||
Elliott Hughes80609252011-09-23 17:24:51 -07002370 this == String::GetJavaLangString() ||
2371 this == Field::GetJavaLangReflectField() ||
2372 this == Method::GetConstructorClass() ||
Ian Rogers0571d352011-11-03 19:51:38 -07002373 this == Method::GetMethodClass());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002374 return GetField32(OFFSET_OF_OBJECT_MEMBER(Class, access_flags_), false);
2375}
2376
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002377inline uint32_t Method::GetAccessFlags() const {
Brian Carlstrom65ca0772011-09-24 16:03:08 -07002378 DCHECK(GetDeclaringClass()->IsIdxLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002379 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, access_flags_), false);
2380}
2381
2382inline uint16_t Method::GetMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002383 DCHECK(GetDeclaringClass()->IsResolved() || GetDeclaringClass()->IsErroneous());
Elliott Hughes1d3f1142011-09-13 12:00:00 -07002384 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002385}
2386
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002387inline uint32_t Method::GetDexMethodIndex() const {
Brian Carlstrom27ec9612011-09-19 20:20:38 -07002388 DCHECK(GetDeclaringClass()->IsLoaded() || GetDeclaringClass()->IsErroneous());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002389 return GetField32(OFFSET_OF_OBJECT_MEMBER(Method, method_dex_index_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002390}
2391
Elliott Hughes76e36942012-03-16 13:44:56 -07002392inline void Method::AssertPcIsWithinCode(uintptr_t pc) const {
Elliott Hughes67d92002012-03-26 15:08:51 -07002393 if (!kIsDebugBuild) {
2394 return;
2395 }
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002396 if (IsNative() || IsRuntimeMethod() || IsProxyMethod()) {
2397 return;
2398 }
2399 Runtime* runtime = Runtime::Current();
2400 if (GetCode() == runtime->GetResolutionStubArray(Runtime::kStaticMethod)->GetData()) {
2401 return;
2402 }
2403 DCHECK(IsWithinCode(pc))
2404 << PrettyMethod(this)
2405 << " pc=" << std::hex << pc
2406 << " code=" << GetCode()
2407 << " size=" << GetCodeSize();
Brian Carlstromf8bbb842012-03-14 03:01:42 -07002408}
2409
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002410inline String* Class::GetName() const {
2411 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Class, name_), false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002412}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002413inline void Class::SetName(String* name) {
2414 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, name_), name, false);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002415}
2416
2417// C++ mirror of java.lang.Throwable
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002418class MANAGED Throwable : public Object {
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002419 public:
2420 void SetDetailMessage(String* new_detail_message) {
2421 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_),
2422 new_detail_message, false);
2423 }
Ian Rogersa32a6fd2012-02-06 20:18:44 -08002424 String* GetDetailMessage() const {
2425 return GetFieldObject<String*>(OFFSET_OF_OBJECT_MEMBER(Throwable, detail_message_), false);
2426 }
Ian Rogers9074b992011-10-26 17:41:55 -07002427 std::string Dump() const;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -07002428
Ian Rogers1c5eb702012-02-01 09:18:34 -08002429 // This is a runtime version of initCause, you shouldn't use it if initCause may have been
2430 // overridden. Also it asserts rather than throwing exceptions. Currently this is only used
2431 // in cases like the verifier where the checks cannot fail and initCause isn't overridden.
2432 void SetCause(Throwable* cause);
Ian Rogers466bb252011-10-14 03:29:56 -07002433 bool IsCheckedException() const;
Ian Rogers5167c972012-02-03 10:41:20 -08002434
2435 static Class* GetJavaLangThrowable() {
2436 DCHECK(java_lang_Throwable_ != NULL);
2437 return java_lang_Throwable_;
2438 }
2439
2440 static void SetClass(Class* java_lang_Throwable);
2441 static void ResetClass();
2442
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002443 private:
Ian Rogers9074b992011-10-26 17:41:55 -07002444 Object* GetStackState() const {
2445 return GetFieldObject<Object*>(OFFSET_OF_OBJECT_MEMBER(Throwable, stack_state_), true);
2446 }
2447
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002448 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
2449 Throwable* cause_;
2450 String* detail_message_;
2451 Object* stack_state_; // Note this is Java volatile:
2452 Object* stack_trace_;
2453 Object* suppressed_exceptions_;
2454
Ian Rogers5167c972012-02-03 10:41:20 -08002455 static Class* java_lang_Throwable_;
2456
Brian Carlstrom693267a2011-09-06 09:25:34 -07002457 friend struct ThrowableOffsets; // for verifying offset information
Elliott Hughese5b0dc82011-08-23 09:59:02 -07002458 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
2459};
2460
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002461// C++ mirror of java.lang.StackTraceElement
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002462class MANAGED StackTraceElement : public Object {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002463 public:
2464 const String* GetDeclaringClass() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002465 return GetFieldObject<const String*>(
2466 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, declaring_class_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002467 }
2468
2469 const String* GetMethodName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002470 return GetFieldObject<const String*>(
2471 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, method_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002472 }
2473
2474 const String* GetFileName() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002475 return GetFieldObject<const String*>(
2476 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, file_name_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002477 }
2478
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002479 int32_t GetLineNumber() const {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002480 return GetField32(
2481 OFFSET_OF_OBJECT_MEMBER(StackTraceElement, line_number_), false);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002482 }
2483
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002484 static StackTraceElement* Alloc(String* declaring_class,
2485 String* method_name,
2486 String* file_name,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002487 int32_t line_number);
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002488
2489 static void SetClass(Class* java_lang_StackTraceElement);
2490
2491 static void ResetClass();
2492
2493 private:
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002494 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002495 String* declaring_class_;
2496 String* file_name_;
2497 String* method_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07002498 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002499
2500 static Class* GetStackTraceElement() {
2501 DCHECK(java_lang_StackTraceElement_ != NULL);
2502 return java_lang_StackTraceElement_;
2503 }
2504
2505 static Class* java_lang_StackTraceElement_;
Brian Carlstrom693267a2011-09-06 09:25:34 -07002506
2507 friend struct StackTraceElementOffsets; // for verifying offset information
Shih-wei Liao55df06b2011-08-26 14:39:27 -07002508 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
2509};
2510
Brian Carlstrome4f7e1d2011-09-11 13:21:12 -07002511class MANAGED InterfaceEntry : public ObjectArray<Object> {
Carl Shapiro1fb86202011-06-27 17:43:13 -07002512 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07002513 Class* GetInterface() const {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002514 Class* interface = Get(kInterface)->AsClass();
2515 DCHECK(interface != NULL);
2516 return interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07002517 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002518
Brian Carlstrom30b94452011-08-25 21:35:26 -07002519 void SetInterface(Class* interface) {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002520 DCHECK(interface != NULL);
Brian Carlstrom30b94452011-08-25 21:35:26 -07002521 DCHECK(interface->IsInterface());
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002522 DCHECK(Get(kInterface) == NULL);
2523 Set(kInterface, interface);
Carl Shapirof88c9522011-08-06 15:47:38 -07002524 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07002525
Brian Carlstrom86927212011-09-15 11:31:11 -07002526 size_t GetMethodArrayCount() const {
2527 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002528 if (method_array == NULL) {
Brian Carlstrom86927212011-09-15 11:31:11 -07002529 return 0;
2530 }
2531 return method_array->GetLength();
2532 }
2533
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002534 ObjectArray<Method>* GetMethodArray() const {
2535 ObjectArray<Method>* method_array = down_cast<ObjectArray<Method>*>(Get(kMethodArray));
2536 DCHECK(method_array != NULL);
2537 return method_array;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002538 }
2539
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002540 void SetMethodArray(ObjectArray<Method>* new_ma) {
2541 DCHECK(new_ma != NULL);
2542 DCHECK(Get(kMethodArray) == NULL);
2543 Set(kMethodArray, new_ma);
2544 }
2545
2546 static size_t LengthAsArray() {
2547 return kMax;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07002548 }
2549
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07002550 private:
Elliott Hughes48257562012-06-06 17:42:44 -07002551 enum {
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002552 // Points to the interface class.
2553 kInterface = 0,
2554 // Method pointers into the vtable, allow fast map from interface
2555 // method index to concrete instance method.
2556 kMethodArray = 1,
2557 kMax = 2,
2558 };
Carl Shapirof88c9522011-08-06 15:47:38 -07002559
Brian Carlstrom4b620ff2011-09-11 01:11:01 -07002560 DISALLOW_IMPLICIT_CONSTRUCTORS(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07002561};
2562
Ian Rogersc2b44472011-12-14 21:17:17 -08002563class MANAGED SynthesizedProxyClass : public Class {
2564 public:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002565 ObjectArray<Class>* GetInterfaces() {
2566 return interfaces_;
2567 }
2568
Ian Rogersc2b44472011-12-14 21:17:17 -08002569 ObjectArray<ObjectArray<Class> >* GetThrows() {
2570 return throws_;
2571 }
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002572
Jesse Wilson95caa792011-10-12 18:14:17 -04002573 private:
Elliott Hughes2ed52c42012-03-21 16:56:56 -07002574 ObjectArray<Class>* interfaces_;
Ian Rogersc2b44472011-12-14 21:17:17 -08002575 ObjectArray<ObjectArray<Class> >* throws_;
2576 DISALLOW_IMPLICIT_CONSTRUCTORS(SynthesizedProxyClass);
Jesse Wilson95caa792011-10-12 18:14:17 -04002577};
2578
2579class MANAGED Proxy : public Object {
2580 private:
2581 Object* h_;
2582
2583 friend struct ProxyOffsets; // for verifying offset information
2584 DISALLOW_IMPLICIT_CONSTRUCTORS(Proxy);
2585};
2586
Carl Shapiro1fb86202011-06-27 17:43:13 -07002587} // namespace art
2588
2589#endif // ART_SRC_OBJECT_H_