blob: 438b58723b42a5fbd674a5b94159a2687a680b71 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Elliott Hughes90a33692011-08-30 13:27:07 -07006#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -07008#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070010#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "logging.h"
12#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070013#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070014#include "offsets.h"
15#include "stringpiece.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070016#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070017
18namespace art {
19
20class Array;
21class Class;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070022class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070023class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040024class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070025class InterfaceEntry;
26class Monitor;
27class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070028class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070029class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040030class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070031template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070032template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070033typedef PrimitiveArray<uint8_t> BooleanArray;
34typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070035typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070036typedef PrimitiveArray<double> DoubleArray;
37typedef PrimitiveArray<float> FloatArray;
38typedef PrimitiveArray<int32_t> IntArray;
39typedef PrimitiveArray<int64_t> LongArray;
40typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070041
Carl Shapiro3ee755d2011-06-28 12:11:04 -070042union JValue {
43 uint8_t z;
44 int8_t b;
45 uint16_t c;
46 int16_t s;
47 int32_t i;
48 int64_t j;
49 float f;
50 double d;
51 Object* l;
52};
53
Brian Carlstrombe977852011-07-19 14:54:54 -070054static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
55static const uint32_t kAccPrivate = 0x0002; // field, method, ic
56static const uint32_t kAccProtected = 0x0004; // field, method, ic
57static const uint32_t kAccStatic = 0x0008; // field, method, ic
58static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
59static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
60static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
61static const uint32_t kAccVolatile = 0x0040; // field
62static const uint32_t kAccBridge = 0x0040; // method (1.5)
63static const uint32_t kAccTransient = 0x0080; // field
64static const uint32_t kAccVarargs = 0x0080; // method (1.5)
65static const uint32_t kAccNative = 0x0100; // method
66static const uint32_t kAccInterface = 0x0200; // class, ic
67static const uint32_t kAccAbstract = 0x0400; // class, method, ic
68static const uint32_t kAccStrict = 0x0800; // method
69static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
70static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
71static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070072
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070073static const uint32_t kAccMiranda = 0x8000; // method
74
Brian Carlstroma331b3c2011-07-18 17:47:56 -070075static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
76
Brian Carlstrombe977852011-07-19 14:54:54 -070077static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
78static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070079
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070080/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070081 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070082 */
83/*
84 * A magic value for refOffsets. Ignore the bits and walk the super
85 * chain when this is the value.
86 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
87 * fields followed by 2 ref instance fields.]
88 */
89#define CLASS_WALK_SUPER ((unsigned int)(3))
90#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
91#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
92#define CLASS_OFFSET_ALIGNMENT 4
93#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
94/*
95 * Given an offset, return the bit number which would encode that offset.
96 * Local use only.
97 */
98#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
99 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
100 CLASS_OFFSET_ALIGNMENT)
101/*
102 * Is the given offset too large to be encoded?
103 */
104#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
105 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
106/*
107 * Return a single bit, encoding the offset.
108 * Undefined if the offset is too large, as defined above.
109 */
110#define CLASS_BIT_FROM_OFFSET(byteOffset) \
111 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
112/*
113 * Return an offset, given a bit number as returned from CLZ.
114 */
115#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700116 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700117
118
Carl Shapiro1fb86202011-06-27 17:43:13 -0700119class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700120 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700121 static bool InstanceOf(const Object* object, const Class* klass) {
122 if (object == NULL) {
123 return false;
124 }
125 return object->InstanceOf(klass);
126 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700127
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700128 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700129 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700130 return klass_;
131 }
132
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700133 bool InstanceOf(const Class* klass) const;
134
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700135 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700136
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700137 void MonitorEnter() {
138 monitor_->Enter();
139 }
140
141 void MonitorExit() {
142 monitor_->Exit();
143 }
144
145 void Notify() {
146 monitor_->Notify();
147 }
148
149 void NotifyAll() {
150 monitor_->NotifyAll();
151 }
152
153 void Wait() {
154 monitor_->Wait();
155 }
156
157 void Wait(int64_t timeout) {
158 monitor_->Wait(timeout);
159 }
160
161 void Wait(int64_t timeout, int32_t nanos) {
162 monitor_->Wait(timeout, nanos);
163 }
164
Brian Carlstrom4873d462011-08-21 15:23:39 -0700165 Object* GetFieldObject(size_t field_offset) const {
166 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
167 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168 }
169
170 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700171 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
172 *reinterpret_cast<Object**>(raw_addr) = new_value;
173 // TODO: write barrier
174 }
175
Brian Carlstrom4873d462011-08-21 15:23:39 -0700176 uint32_t GetField32(size_t field_offset) const {
177 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
178 return *reinterpret_cast<const uint32_t*>(raw_addr);
179 }
180
181 void SetField32(size_t offset, uint32_t new_value) {
182 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
183 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
184 }
185
186 uint64_t GetField64(size_t field_offset) const {
187 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
188 return *reinterpret_cast<const uint64_t*>(raw_addr);
189 }
190
191 void SetField64(size_t offset, uint64_t new_value) {
192 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
193 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
194 }
195
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700196 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197
198 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700199 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700200 return down_cast<Class*>(this);
201 }
202
203 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700204 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700205 return down_cast<const Class*>(this);
206 }
207
Brian Carlstrom4873d462011-08-21 15:23:39 -0700208 bool IsClassClass() const;
209
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700210 bool IsObjectArray() const;
211
212 template<class T>
213 ObjectArray<T>* AsObjectArray() {
214 DCHECK(IsObjectArray());
215 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216 }
217
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700218 template<class T>
219 const ObjectArray<T>* AsObjectArray() const {
220 DCHECK(IsObjectArray());
221 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222 }
223
224 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700225 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 return true;
227 }
228
229 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700230 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700231 return true;
232 }
233
234 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700235 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700236 return true;
237 }
238
239 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700240 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700241 return true;
242 }
243
244 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700245 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700246 return true;
247 }
248
Brian Carlstromb63ec392011-08-27 17:38:27 -0700249 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700250
251 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700252 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700253 return down_cast<Array*>(this);
254 }
255
256 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700257 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700258 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700259 }
260
Brian Carlstroma663ea52011-08-19 23:33:41 -0700261 bool IsString() const;
262
263 String* AsString() {
264 DCHECK(IsString());
265 return down_cast<String*>(this);
266 }
267
268 bool IsMethod() const;
269
270 Method* AsMethod() {
271 DCHECK(IsMethod());
272 return down_cast<Method*>(this);
273 }
274
Brian Carlstrom4873d462011-08-21 15:23:39 -0700275 const Method* AsMethod() const {
276 DCHECK(IsMethod());
277 return down_cast<const Method*>(this);
278 }
279
Brian Carlstroma663ea52011-08-19 23:33:41 -0700280 bool IsField() const;
281
282 Field* AsField() {
283 DCHECK(IsField());
284 return down_cast<Field*>(this);
285 }
286
Brian Carlstrom4873d462011-08-21 15:23:39 -0700287 const Field* AsField() const {
288 DCHECK(IsField());
289 return down_cast<const Field*>(this);
290 }
291
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700292 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700293 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 Monitor* monitor_;
296
297 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700298 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299};
300
301class ObjectLock {
302 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700303 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700304 CHECK(object != NULL);
305 obj_->MonitorEnter();
306 }
307
308 ~ObjectLock() {
309 obj_->MonitorExit();
310 }
311
312 void Wait(int64_t millis = 0) {
313 return obj_->Wait(millis);
314 }
315
316 void Notify() {
317 obj_->Notify();
318 }
319
320 void NotifyAll() {
321 obj_->NotifyAll();
322 }
323
324 private:
325 Object* obj_;
326 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700327};
328
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400329class AccessibleObject : public Object {
330 private:
331 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
332 uint32_t java_flag_;
333};
334
335class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700336 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700337 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700338 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700339 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700340 }
341
Jesse Wilson14150742011-07-29 19:04:44 -0400342 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700343 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700344 return name_;
345 }
346
347 bool IsStatic() const {
348 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700349 }
350
351 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700352 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700353 }
354
Brian Carlstromae3ac012011-07-27 01:30:28 -0700355 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700356 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700357 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700358 }
359
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700360 uint32_t GetOffset() const {
361 return offset_;
362 }
363
364 void SetOffset(size_t num_bytes) {
365 offset_ = num_bytes;
366 }
367
Brian Carlstrom4873d462011-08-21 15:23:39 -0700368 // field access, null object for static fields
369 bool GetBoolean(const Object* object) const;
370 void SetBoolean(Object* object, bool z) const;
371 int8_t GetByte(const Object* object) const;
372 void SetByte(Object* object, int8_t b) const;
373 uint16_t GetChar(const Object* object) const;
374 void SetChar(Object* object, uint16_t c) const;
375 uint16_t GetShort(const Object* object) const;
376 void SetShort(Object* object, uint16_t s) const;
377 int32_t GetInt(const Object* object) const;
378 void SetInt(Object* object, int32_t i) const;
379 int64_t GetLong(const Object* object) const;
380 void SetLong(Object* object, int64_t j) const;
381 float GetFloat(const Object* object) const;
382 void SetFloat(Object* object, float f) const;
383 double GetDouble(const Object* object) const;
384 void SetDouble(Object* object, double d) const;
385 Object* GetObject(const Object* object) const;
386 void SetObject(Object* object, Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700387
Brian Carlstromb9edb842011-08-28 16:31:06 -0700388 // slow path routines for static field access when field was unresolved at compile time
389 static uint32_t Get32StaticFromCode(uint32_t field_idx, const Method* referrer);
390 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value);
391 static uint64_t Get64StaticFromCode(uint32_t field_idx, const Method* referrer);
392 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value);
393 static Object* GetObjStaticFromCode(uint32_t field_idx, const Method* referrer);
394 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value);
395
Jesse Wilson35baaab2011-08-10 16:18:03 -0400396 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700397
398 // private implemention of field access using raw data
399 uint32_t Get32(const Object* object) const;
400 void Set32(Object* object, uint32_t new_value) const;
401 uint64_t Get64(const Object* object) const;
402 void Set64(Object* object, uint64_t new_value) const;
403 Object* GetObj(const Object* object) const;
404 void SetObj(Object* object, Object* new_value) const;
405
Jesse Wilson35baaab2011-08-10 16:18:03 -0400406 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
407 // The class in which this field is declared.
408 Class* declaring_class_;
409 Object* generic_type_;
410 uint32_t generic_types_are_initialized_;
411 String* name_;
412 uint32_t offset_;
413 Class* type_;
414
415 // e.g. "I", "[C", "Landroid/os/Debug;"
416 StringPiece descriptor_;
417
418 uint32_t access_flags_;
419
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700420 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400421 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700422};
423
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400424class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700425 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700426 // An function that invokes a method with an array of its arguments.
427 typedef void InvokeStub(Method* method,
428 Object* obj,
429 Thread* thread,
430 byte* args,
431 JValue* result);
432
Brian Carlstromae3ac012011-07-27 01:30:28 -0700433 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700434 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700435 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700436 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700437 }
438
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700439 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700440 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700441 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700442 }
443
Brian Carlstroma0808032011-07-18 00:39:23 -0700444 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700445 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700446 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700447 }
448
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700449 static MemberOffset DeclaringClassOffset() {
450 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700451 }
452
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700453 // Returns true if the method is declared public.
454 bool IsPublic() const {
455 return (access_flags_ & kAccPublic) != 0;
456 }
457
458 // Returns true if the method is declared private.
459 bool IsPrivate() const {
460 return (access_flags_ & kAccPrivate) != 0;
461 }
462
463 // Returns true if the method is declared static.
464 bool IsStatic() const {
465 return (access_flags_ & kAccStatic) != 0;
466 }
467
468 // Returns true if the method is declared synchronized.
469 bool IsSynchronized() const {
470 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
471 return (access_flags_ & synchonized) != 0;
472 }
473
474 // Returns true if the method is declared final.
475 bool IsFinal() const {
476 return (access_flags_ & kAccFinal) != 0;
477 }
478
479 // Returns true if the method is declared native.
480 bool IsNative() const {
481 return (access_flags_ & kAccNative) != 0;
482 }
483
484 // Returns true if the method is declared abstract.
485 bool IsAbstract() const {
486 return (access_flags_ & kAccAbstract) != 0;
487 }
488
489 bool IsSynthetic() const {
490 return (access_flags_ & kAccSynthetic) != 0;
491 }
492
493 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700494 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700495
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700496 // Number of argument bytes required for densely packing the
497 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700498 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700499
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700500 // Converts a native PC to a virtual PC. TODO: this is a no-op
501 // until we associate a PC mapping table with each method.
502 uintptr_t ToDexPC(const uintptr_t pc) const {
503 return pc;
504 }
505
506 // Converts a virtual PC to a native PC. TODO: this is a no-op
507 // until we associate a PC mapping table with each method.
508 uintptr_t ToNativePC(const uintptr_t pc) const {
509 return pc;
510 }
511
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400513 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400514 // the class we are a part of
515 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400516 ObjectArray<Class>* java_exception_types_;
517 Object* java_formal_type_parameters_;
518 Object* java_generic_exception_types_;
519 Object* java_generic_parameter_types_;
520 Object* java_generic_return_type_;
521 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700522 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400523 ObjectArray<Class>* java_parameter_types_;
524 uint32_t java_generic_types_are_initialized_;
525 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700526
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700527 const StringPiece& GetShorty() const {
528 return shorty_;
529 }
530
Ian Rogersb033c752011-07-20 12:22:35 -0700531 bool IsReturnAReference() const {
532 return (shorty_[0] == 'L') || (shorty_[0] == '[');
533 }
534
535 bool IsReturnAFloatOrDouble() const {
536 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
537 }
538
539 bool IsReturnAFloat() const {
540 return shorty_[0] == 'F';
541 }
542
543 bool IsReturnADouble() const {
544 return shorty_[0] == 'D';
545 }
546
547 bool IsReturnALong() const {
548 return shorty_[0] == 'J';
549 }
550
Ian Rogers45a76cb2011-07-21 22:00:15 -0700551 bool IsReturnVoid() const {
552 return shorty_[0] == 'V';
553 }
554
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700555 bool IsDirect() const {
556 return is_direct_;
557 }
558
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700559 // "Args" may refer to any of the 3 levels of "Args."
560 // To avoid confusion, our code will denote which "Args" clearly:
561 // 1. UserArgs: Args that a user see.
562 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
563 // receiver.
564 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
565 // E.g., the first in Args is Method* for both static and non-static
566 // methods. And CConvArgs doesn't deal with the receiver because
567 // receiver is hardwired in an implicit register, so CConvArgs doesn't
568 // need to deal with it.
569 //
570 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700571 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700572 // "1 +" because the first in Args is the receiver.
573 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700574 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
575 }
576
577 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700578 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700579 size_t NumReferenceArgs() const;
580
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700581 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700582 size_t NumLongOrDoubleArgs() const;
583
584 // The number of reference arguments to this method before the given
585 // parameter index
586 size_t NumReferenceArgsBefore(unsigned int param) const;
587
588 // Is the given method parameter a reference?
589 bool IsParamAReference(unsigned int param) const;
590
591 // Is the given method parameter a long or double?
592 bool IsParamALongOrDouble(unsigned int param) const;
593
Ian Rogersdf20fe02011-07-20 20:34:16 -0700594 // Size in bytes of the given parameter
595 size_t ParamSize(unsigned int param) const;
596
597 // Size in bytes of the return value
598 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700599
buzbeec143c552011-08-20 17:38:58 -0700600 bool HasCode() {
601 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700602 }
603
Brian Carlstrom83db7722011-08-26 17:32:56 -0700604 const void* GetCode() {
605 return code_;
606 }
607
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700608 void SetCode(const byte* compiled_code,
609 size_t byte_count,
610 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700611 // Copy the code into an executable region.
612 code_instruction_set_ = set;
613 code_area_.reset(MemMap::Map(byte_count,
614 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700615 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700616 byte* code = code_area_->GetAddress();
617 memcpy(code, compiled_code, byte_count);
618 __builtin___clear_cache(code, code + byte_count);
619
620 uintptr_t address = reinterpret_cast<uintptr_t>(code);
621 if (code_instruction_set_ == kThumb2) {
622 // Set the low-order bit so a BLX will switch to Thumb mode
623 address |= 0x1;
624 }
625 code_ = reinterpret_cast<void*>(address);
626 }
627
Shih-wei Liaod11af152011-08-23 16:02:11 -0700628 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
629 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700630 }
631
Shih-wei Liaod11af152011-08-23 16:02:11 -0700632 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
633 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700634 }
635
Shih-wei Liaod11af152011-08-23 16:02:11 -0700636 size_t GetFrameSizeInBytes() const {
637 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700638 }
639
Shih-wei Liaod11af152011-08-23 16:02:11 -0700640 size_t GetReturnPcOffsetInBytes() const {
641 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700642 }
643
buzbeec143c552011-08-20 17:38:58 -0700644 void SetCoreSpillMask(uint32_t core_spill_mask) {
645 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700646 }
647
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700648 static size_t GetCodeOffset() {
649 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700650 }
651
buzbeec143c552011-08-20 17:38:58 -0700652 void SetFpSpillMask(uint32_t fp_spill_mask) {
653 fp_spill_mask_ = fp_spill_mask;
654 }
655
Ian Rogersb033c752011-07-20 12:22:35 -0700656 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700657 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700658 native_method_ = native_method;
659 }
660
Elliott Hughes5174fe62011-08-23 15:12:35 -0700661 void UnregisterNative() {
662 native_method_ = NULL;
663 }
664
Ian Rogersb033c752011-07-20 12:22:35 -0700665 static MemberOffset NativeMethodOffset() {
666 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
667 }
668
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700669 InvokeStub* GetInvokeStub() const {
670 return invoke_stub_;
671 }
672
673 void SetInvokeStub(const InvokeStub* invoke_stub) {
674 invoke_stub_ = invoke_stub;
675 }
676
677 static size_t GetInvokeStubOffset() {
678 return OFFSETOF_MEMBER(Method, invoke_stub_);
679 }
680
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700681 bool HasSameNameAndDescriptor(const Method* that) const;
682
Ian Rogersb033c752011-07-20 12:22:35 -0700683 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700684 // access flags; low 16 bits are defined by spec (could be uint16_t?)
685 uint32_t access_flags_;
686
687 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700688 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700689 //
690 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700691 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700692 uint16_t method_index_;
693
694 // Method bounds; not needed for an abstract method.
695 //
696 // For a native method, we compute the size of the argument list, and
697 // set "insSize" and "registerSize" equal to it.
698 uint16_t num_registers_; // ins + locals
699 uint16_t num_outs_;
700 uint16_t num_ins_;
701
buzbeec143c552011-08-20 17:38:58 -0700702 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700703 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700704
705 // Architecture-dependent register spill masks
706 uint32_t core_spill_mask_;
707 uint32_t fp_spill_mask_;
708
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700709 // The method descriptor. This represents the parameters a method
710 // takes and value it returns. This string is a list of the type
711 // descriptors for the parameters enclosed in parenthesis followed
712 // by the return type descriptor. For example, for the method
713 //
714 // Object mymethod(int i, double d, Thread t)
715 //
716 // the method descriptor would be
717 //
718 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700719 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700720
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700721 // Method prototype descriptor string (return and argument types).
722 uint32_t proto_idx_;
723
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700724 // Offset to the CodeItem.
725 uint32_t code_off_;
726
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700727 // The short-form method descriptor string.
728 StringPiece shorty_;
729
Ian Rogers762400c2011-08-23 12:14:16 -0700730 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
731 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700732 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700733 ObjectArray<Class>* dex_cache_resolved_types_;
734 ObjectArray<Method>* dex_cache_resolved_methods_;
735 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700736 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700737 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700738
739 bool is_direct_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700740
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700741 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700742 // Compiled code associated with this method
Elliott Hughes90a33692011-08-30 13:27:07 -0700743 UniquePtr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700744 const void* code_;
745 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700746 InstructionSet code_instruction_set_;
747
748 // Size in bytes of compiled code associated with this method
749 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700750
Ian Rogers762400c2011-08-23 12:14:16 -0700751 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700752 // Offset of PC within compiled code (in bytes)
753 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700754
Ian Rogersb033c752011-07-20 12:22:35 -0700755 // Any native method registered with this method
756 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700757
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700758 // Native invocation stub entry point.
759 const InvokeStub* invoke_stub_;
760
Carl Shapirof88c9522011-08-06 15:47:38 -0700761 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700762};
763
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700764class Array : public Object {
765 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700766 static size_t SizeOf(size_t component_count,
767 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700768 return sizeof(Array) + component_count * component_size;
769 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700770
Brian Carlstromb63ec392011-08-27 17:38:27 -0700771 // Given the context of a calling Method, use its DexCache to
772 // resolve a type to an array Class. If it cannot be resolved, throw
773 // an error. If it can, use it to create an array.
774 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
775
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700776 // A convenience for code that doesn't know the component size,
777 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700778 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700779
Brian Carlstromb63ec392011-08-27 17:38:27 -0700780 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700781
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700782 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700783
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700784 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700785 return length_;
786 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700787
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700788 void SetLength(uint32_t length) {
789 length_ = length;
790 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700791
buzbeec143c552011-08-20 17:38:58 -0700792 static MemberOffset LengthOffset() {
793 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
794 }
795
796 static MemberOffset DataOffset() {
797 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
798 }
799
Elliott Hughes289da822011-08-16 10:11:20 -0700800 protected:
801 bool IsValidIndex(int32_t index) const {
802 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700803 Thread* self = Thread::Current();
804 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
805 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700806 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700807 }
808 return true;
809 }
810
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700811 private:
812 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700813 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400814 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
815 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700816 // Marker for the data (used by generated code)
817 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700818
Carl Shapirof88c9522011-08-06 15:47:38 -0700819 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700820};
821
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700822template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700823class ObjectArray : public Array {
824 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700825 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700826 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700827 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700828 }
829
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700830 T* const * GetData() const {
831 return reinterpret_cast<T* const *>(&elements_);
832 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400833
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700834 T** GetData() {
835 return reinterpret_cast<T**>(&elements_);
836 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400837
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700838 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700839 if (!IsValidIndex(i)) {
840 return NULL;
841 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700842 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700843 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700844
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700845 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700846 if (IsValidIndex(i)) {
847 // TODO: ArrayStoreException
848 GetData()[i] = object; // TODO: write barrier
849 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700850 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700851
852 static void Copy(ObjectArray<T>* src, int src_pos,
853 ObjectArray<T>* dst, int dst_pos,
854 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700855 for (size_t i = 0; i < length; i++) {
856 dst->Set(dst_pos + i, src->Get(src_pos + i));
857 }
858 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700859
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700860 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700861 ObjectArray<T>* new_array = Alloc(klass_, new_length);
862 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
863 return new_array;
864 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700865
866 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700867 // Location of first element.
868 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700869
870 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700871};
872
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700873// ClassLoader objects.
874class ClassLoader : public Object {
875 public:
Brian Carlstrom8a487412011-08-29 20:08:52 -0700876 static const std::vector<const DexFile*>& GetClassPath(const ClassLoader* class_loader);
877
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700878 void SetClassPath(std::vector<const DexFile*>& class_path) {
879 DCHECK_EQ(0U, class_path_.size());
880 class_path_ = class_path;
881 }
882
883 private:
884 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
885 Object* packages_;
886 ClassLoader* parent_;
887
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700888 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700889 std::vector<const DexFile*> class_path_;
890
Carl Shapirof88c9522011-08-06 15:47:38 -0700891 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700892};
893
894class BaseDexClassLoader : public ClassLoader {
895 private:
896 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
897 String* original_path_;
898 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700899 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700900};
901
902class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700903 public:
Brian Carlstrom8a487412011-08-29 20:08:52 -0700904 static const PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700905 static void SetClass(Class* dalvik_system_PathClassLoader);
906 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700907 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700908 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700909 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700910};
911
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700912// Type for the InitializedStaticStorage table. Currently the Class
913// provides the static storage. However, this might change to improve
914// image sharing, so we use this type to avoid assumptions on the
915// current storage.
916class StaticStorageBase {};
917
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918// Class objects.
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700919class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700920 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700921
922 // Class Status
923 //
924 // kStatusNotReady: If a Class cannot be found in the class table by
925 // FindClass, it allocates an new one with AllocClass in the
926 // kStatusNotReady and calls LoadClass. Note if it does find a
927 // class, it may not be kStatusResolved and it will try to push it
928 // forward toward kStatusResolved.
929 //
930 // kStatusIdx: LoadClass populates with Class with information from
931 // the DexFile, moving the status to kStatusIdx, indicating that the
932 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700933 // populated based on super_class_type_idx_ and
934 // interfaces_type_idx_. The new Class can then be inserted into the
935 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700936 //
937 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
938 // attempt to move a kStatusIdx class forward to kStatusLoaded by
939 // using ResolveClass to initialize the super_class_ and interfaces_.
940 //
941 // kStatusResolved: Still holding the lock on Class, the ClassLinker
942 // will use LinkClass to link all members, creating Field and Method
943 // objects, setting up the vtable, etc. On success, the class is
944 // marked kStatusResolved.
945
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700947 kStatusError = -1,
948 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700949 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700950 kStatusLoaded = 2, // DEX idx values resolved
951 kStatusResolved = 3, // part of linking
952 kStatusVerifying = 4, // in the process of being verified
953 kStatusVerified = 5, // logically part of linking; done pre-init
954 kStatusInitializing = 6, // class init in progress
955 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700956 };
957
958 enum PrimitiveType {
959 kPrimNot = -1
960 };
961
Brian Carlstromb63ec392011-08-27 17:38:27 -0700962 // Given the context of a calling Method, use its DexCache to
963 // resolve a type to a Class. If it cannot be resolved, throw an
964 // error. If it can, use it to create an instance.
965 static Object* NewInstanceFromCode(uint32_t type_idx, Method* method);
966
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700967 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700968 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700969 return Heap::AllocObject(this, this->object_size_);
970 }
971
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 Class* GetSuperClass() const {
973 return super_class_;
974 }
975
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700976 uint32_t GetSuperClassTypeIdx() const {
977 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978 }
979
980 bool HasSuperClass() const {
981 return super_class_ != NULL;
982 }
983
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700984 bool IsAssignableFrom(const Class* klass) const {
985 DCHECK(klass != NULL);
986 if (this == klass) {
987 return true;
988 }
989 if (IsInterface()) {
990 return klass->Implements(this);
991 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700992 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700993 return IsAssignableFromArray(klass);
994 }
995 return klass->IsSubClass(this);
996 }
997
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700998 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700999 return class_loader_;
1000 }
1001
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001002 DexCache* GetDexCache() const {
1003 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001004 }
1005
1006 Class* GetComponentType() const {
1007 return component_type_;
1008 }
1009
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001010 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001011
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001012 size_t GetComponentSize() const {
1013 return GetTypeSize(component_type_->descriptor_);
1014 }
1015
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001016 const String* GetDescriptor() const {
1017 DCHECK(descriptor_ != NULL);
1018 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 return descriptor_;
1020 }
1021
Brian Carlstrom4873d462011-08-21 15:23:39 -07001022 size_t SizeOf() const {
1023 return class_size_;
1024 }
1025
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 Status GetStatus() const {
1027 return status_;
1028 }
1029
1030 void SetStatus(Status new_status) {
1031 // TODO: validate transition
1032 status_ = new_status;
1033 }
1034
Carl Shapiro69759ea2011-07-21 18:13:35 -07001035 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 bool IsErroneous() const {
1037 return GetStatus() == kStatusError;
1038 }
1039
Carl Shapiro69759ea2011-07-21 18:13:35 -07001040 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001041 bool IsVerified() const {
1042 return GetStatus() >= kStatusVerified;
1043 }
1044
Carl Shapiro69759ea2011-07-21 18:13:35 -07001045 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001046 bool IsLinked() const {
1047 return GetStatus() >= kStatusResolved;
1048 }
1049
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001050 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051 bool IsLoaded() const {
1052 return GetStatus() >= kStatusLoaded;
1053 }
1054
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001055 // Returns true if the class is initialized.
1056 bool IsInitialized() const {
1057 return GetStatus() == kStatusInitialized;
1058 }
1059
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001060 // Returns true if this class is in the same packages as that class.
1061 bool IsInSamePackage(const Class* that) const;
1062
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001063 static bool IsInSamePackage(const String* descriptor1,
1064 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001065
1066 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001067 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001068
1069 // Returns true if the class is an interface.
1070 bool IsInterface() const {
1071 return (access_flags_ & kAccInterface) != 0;
1072 }
1073
1074 // Returns true if the class is declared public.
1075 bool IsPublic() const {
1076 return (access_flags_ & kAccPublic) != 0;
1077 }
1078
1079 // Returns true if the class is declared final.
1080 bool IsFinal() const {
1081 return (access_flags_ & kAccFinal) != 0;
1082 }
1083
1084 // Returns true if the class is abstract.
1085 bool IsAbstract() const {
1086 return (access_flags_ & kAccAbstract) != 0;
1087 }
1088
1089 // Returns true if the class is an annotation.
1090 bool IsAnnotation() const {
1091 return (access_flags_ & kAccAnnotation) != 0;
1092 }
1093
1094 // Returns true if the class is a primitive type.
1095 bool IsPrimitive() const {
1096 return primitive_type_ != kPrimNot;
1097 }
1098
Brian Carlstromae3ac012011-07-27 01:30:28 -07001099 // Returns true if the class is synthetic.
1100 bool IsSynthetic() const {
1101 return (access_flags_ & kAccSynthetic) != 0;
1102 }
1103
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001104 // Returns true if this class can access that class.
1105 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001107 }
1108
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 // Returns the number of static, private, and constructor methods.
1110 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001111 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001112 }
1113
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001114 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001115 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001116 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001117 }
1118
1119 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001120 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001121 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001122 }
1123
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001124 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001125 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001126
1127 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001128 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001129
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001130 // Returns the number of non-inherited virtual methods.
1131 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001132 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 }
1134
1135 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001136 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001137 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001138 }
1139
1140 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001141 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001142 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001143 }
1144
Brian Carlstrom30b94452011-08-25 21:35:26 -07001145 // Given a method implemented by this class but potentially from a
1146 // super class, return the specific implementation
1147 // method for this class.
1148 Method* FindVirtualMethodForVirtual(Method* method) {
1149 DCHECK(!method->GetDeclaringClass()->IsInterface());
1150 // The argument method may from a super class.
1151 // Use the index to a potentially overriden one for this instance's class.
1152 return vtable_->Get(method->method_index_);
1153 }
1154
1155 // Given a method implemented by this class, but potentially from a
1156 // super class or interface, return the specific implementation
1157 // method for this class.
1158 Method* FindVirtualMethodForInterface(Method* method);
1159
1160 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1161 if (method->GetDeclaringClass()->IsInterface()) {
1162 return FindVirtualMethodForInterface(method);
1163 }
1164 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 }
1166
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001167 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1168 const StringPiece& descriptor);
1169
1170 Method* FindVirtualMethod(const StringPiece& name,
1171 const StringPiece& descriptor);
1172
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001174 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001175 }
1176
Carl Shapiro69759ea2011-07-21 18:13:35 -07001177 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001179 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001180 }
1181
Brian Carlstrom4873d462011-08-21 15:23:39 -07001182 // Returns the number of static fields containing reference types.
1183 size_t NumReferenceStaticFields() const {
1184 return num_reference_static_fields_;
1185 }
1186
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 // Finds the given instance field in this class or a superclass.
1188 Field* FindInstanceField(const StringPiece& name,
1189 const StringPiece& descriptor);
1190
1191 Field* FindDeclaredInstanceField(const StringPiece& name,
1192 const StringPiece& descriptor);
1193
1194 // Finds the given static field in this class or a superclass.
1195 Field* FindStaticField(const StringPiece& name,
1196 const StringPiece& descriptor);
1197
1198 Field* FindDeclaredStaticField(const StringPiece& name,
1199 const StringPiece& descriptor);
1200
Jesse Wilson35baaab2011-08-10 16:18:03 -04001201 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001202 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001203 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001204 }
1205
Jesse Wilson35baaab2011-08-10 16:18:03 -04001206 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001207 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001208 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001209 }
1210
1211 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001212 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 }
1214
Jesse Wilson35baaab2011-08-10 16:18:03 -04001215 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001216 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001217 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001218 }
1219
Jesse Wilson35baaab2011-08-10 16:18:03 -04001220 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001221 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001222 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 }
1224
Brian Carlstrom4873d462011-08-21 15:23:39 -07001225 uint32_t GetReferenceInstanceOffsets() const {
1226 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 }
1228
Brian Carlstrom4873d462011-08-21 15:23:39 -07001229 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1230 reference_instance_offsets_ = new_reference_offsets;
1231 }
1232
1233 uint32_t GetReferenceStaticOffsets() const {
1234 return reference_static_offsets_;
1235 }
1236
1237 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1238 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 }
1240
Carl Shapiro69759ea2011-07-21 18:13:35 -07001241 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001242 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001243 }
1244
1245 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001246 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001247 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001248 }
1249
1250 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001251 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001252 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001253 }
1254
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001255 void SetVerifyErrorClass(Class* klass) {
1256 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1257 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1258 klass->SetFieldObject(field_offset, klass);
1259 }
1260
1261 private:
1262 bool Implements(const Class* klass) const;
1263 bool IsArrayAssignableFromArray(const Class* klass) const;
1264 bool IsAssignableFromArray(const Class* klass) const;
1265 bool IsSubClass(const Class* klass) const;
1266
Ian Rogersb033c752011-07-20 12:22:35 -07001267 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001268 // descriptor for the class such as "java.lang.Class" or "[C"
1269 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001270
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001271 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1272 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001273
1274 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001275 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001276
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001277 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001278 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001279 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001280
1281 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001283
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001284 // If class verify fails, we must return same error on subsequent tries.
1285 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1286 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001287
1288 // threadId, used to check for recursive <clinit> invocation
1289 uint32_t clinit_thread_id_;
1290
1291 // Total object size; used when allocating storage on gc heap. (For
1292 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001293 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001294
1295 // For array classes, the class object for base element, for
1296 // instanceof/checkcast (for String[][][], this will be String).
1297 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001299
1300 // For array classes, the number of array dimensions, e.g. int[][]
1301 // is 2. Otherwise 0.
1302 int32_t array_rank_;
1303
1304 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1305 PrimitiveType primitive_type_;
1306
1307 // The superclass, or NULL if this is java.lang.Object or a
1308 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001309 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001310 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001311
1312 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001313 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001314
1315 // initiating class loader list
1316 // NOTE: for classes with low serialNumber, these are unused, and the
1317 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001318 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001319
1320 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001321 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001322 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001323
1324 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001325 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001326
1327 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001328 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001329
1330 // Virtual method table (vtable), for use by "invoke-virtual". The
1331 // vtable from the superclass is copied in, and virtual methods from
1332 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001333 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001334
Brian Carlstrom30b94452011-08-25 21:35:26 -07001335 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001336 // this class. That means one entry for each interface we support
1337 // directly, indirectly via superclass, or indirectly via
1338 // superinterface. This will be null if neither we nor our
1339 // superclass implement any interfaces.
1340 //
1341 // Why we need this: given "class Foo implements Face", declare
1342 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1343 // is part of the Face interface. We can't easily use a single
1344 // vtable.
1345 //
1346 // For every interface a concrete class implements, we create a list
1347 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001349 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001350 InterfaceEntry* iftable_;
1351
1352 // The interface vtable indices for iftable get stored here. By
1353 // placing them all in a single pool for each class that implements
1354 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001356 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001358
1359 // instance fields
1360 //
1361 // These describe the layout of the contents of a
1362 // DataObject-compatible Object. Note that only the fields directly
1363 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001364 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001365 //
1366 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001367 // the beginning of the field list. num_reference_instance_fields_
1368 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001369 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001370
Brian Carlstrom4873d462011-08-21 15:23:39 -07001371 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001372 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001373
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001375 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001376
1377 // source file name, if known. Otherwise, NULL.
1378 const char* source_file_;
1379
Jesse Wilson7833bd22011-08-09 18:31:44 -04001380 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001381 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001382
Brian Carlstrom4873d462011-08-21 15:23:39 -07001383 // number of static fields that are object refs
1384 size_t num_reference_static_fields_;
1385
1386 // Bitmap of offsets of sfields.
1387 uint32_t reference_static_offsets_;
1388
1389 // Total class size; used when allocating storage on gc heap.
1390 size_t class_size_;
1391
1392 // Location of first static field.
1393 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001394
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001395 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001396 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001397};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001398std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001399
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001400inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001401 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001402 DCHECK(klass_ != NULL);
1403 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001404}
1405
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001406inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001407 Class* java_lang_Class = klass_->klass_;
1408 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001409}
1410
Brian Carlstrom4873d462011-08-21 15:23:39 -07001411inline bool Object::IsClassClass() const {
1412 Class* java_lang_Class = klass_->klass_;
1413 return this == java_lang_Class;
1414}
1415
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001416inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001417 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001418}
1419
Brian Carlstromb63ec392011-08-27 17:38:27 -07001420inline bool Object::IsArrayInstance() const {
1421 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001422}
1423
Brian Carlstroma663ea52011-08-19 23:33:41 -07001424inline bool Object::IsField() const {
1425 Class* java_lang_Class = klass_->klass_;
1426 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1427 return klass_ == java_lang_reflect_Field;
1428}
1429
1430inline bool Object::IsMethod() const {
1431 Class* java_lang_Class = klass_->klass_;
1432 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1433 return klass_ == java_lang_reflect_Method;
1434}
1435
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001436inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001437 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001438 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001439 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001440 if (IsClass()) {
1441 return AsClass()->SizeOf();
1442 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001443 return klass_->object_size_;
1444}
1445
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001446inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001447 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001448}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001449
Brian Carlstrom4873d462011-08-21 15:23:39 -07001450class ClassClass : public Class {
1451 private:
1452 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1453 int32_t padding_;
1454 int64_t serialVersionUID_;
1455 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1456};
1457
1458class StringClass : public Class {
1459 private:
1460 CharArray* ASCII_;
1461 Object* CASE_INSENSITIVE_ORDER_;
1462 uint32_t REPLACEMENT_CHAR_;
1463 int64_t serialVersionUID;
1464 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1465};
1466
1467class FieldClass : public Class {
1468 private:
1469 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1470 uint32_t TYPE_BOOLEAN_;
1471 uint32_t TYPE_BYTE_;
1472 uint32_t TYPE_CHAR_;
1473 uint32_t TYPE_DOUBLE_;
1474 uint32_t TYPE_FLOAT_;
1475 uint32_t TYPE_INTEGER_;
1476 uint32_t TYPE_LONG_;
1477 uint32_t TYPE_SHORT_;
1478 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1479};
1480
1481class MethodClass : public Class {
1482 private:
1483 int32_t DECLARED_;
1484 int32_t PUBLIC_;
1485 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1486};
1487
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488class DataObject : public Object {
1489 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001490 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001491 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001492 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001493 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494};
1495
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001496template<class T>
1497class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001498 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001499 typedef T ElementType;
1500
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001501 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001502
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001503 const T* GetData() const {
1504 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001505 }
1506
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001507 T* GetData() {
1508 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001509 }
1510
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001511 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001512 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001513 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001514 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001515 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001516 }
1517
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001518 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001519 // TODO: ArrayStoreException
1520 if (IsValidIndex(i)) {
1521 GetData()[i] = value;
1522 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001523 }
1524
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001525 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001526 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001527 CHECK(array_class != NULL);
1528 array_class_ = array_class;
1529 }
1530
Brian Carlstroma663ea52011-08-19 23:33:41 -07001531 static void ResetArrayClass() {
1532 CHECK(array_class_ != NULL);
1533 array_class_ = NULL;
1534 }
1535
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001536 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001537 // Location of first element.
1538 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001539
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001540 static Class* array_class_;
1541
Carl Shapirof88c9522011-08-06 15:47:38 -07001542 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001543};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001546 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001547 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001548 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001549 return array_;
1550 }
1551
Carl Shapirof88c9522011-08-06 15:47:38 -07001552 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001553 return hash_code_;
1554 }
1555
Elliott Hughes814e4032011-08-23 12:07:56 -07001556 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001557 return offset_;
1558 }
1559
Elliott Hughes814e4032011-08-23 12:07:56 -07001560 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001561 return count_;
1562 }
1563
Elliott Hughes814e4032011-08-23 12:07:56 -07001564 int32_t GetUtfLength() const {
1565 return CountUtf8Bytes(array_->GetData(), count_);
1566 }
1567
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001568 // TODO: do we need this? Equals is the only caller, and could
1569 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001570 uint16_t CharAt(int32_t index) const {
1571 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001572 Thread* self = Thread::Current();
1573 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1574 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001575 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001576 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001577 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001578 }
1579
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001580 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001581 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001582 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001583 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001584 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001585 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001586 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001587 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001588 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001589 if (hash_code != 0) {
1590 string->hash_code_ = hash_code;
1591 } else {
1592 string->ComputeHashCode();
1593 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001594 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001595 }
1596
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001597 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001598 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001599 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001600 }
1601
Jesse Wilson8989d992011-08-02 13:39:42 -07001602 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1603 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001604 String* string = Alloc(GetJavaLangString(), utf16_length);
1605 uint16_t* utf16_data_out = string->array_->GetData();
1606 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1607 string->ComputeHashCode();
1608 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001609 }
1610
Brian Carlstroma663ea52011-08-19 23:33:41 -07001611 static void SetClass(Class* java_lang_String);
1612 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001613
Elliott Hughes814e4032011-08-23 12:07:56 -07001614 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001615 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1616 }
1617
Elliott Hughes814e4032011-08-23 12:07:56 -07001618 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001619 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001620 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001621 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001622 return string;
1623 }
1624
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001625 void ComputeHashCode() {
1626 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1627 }
1628
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001629 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001630 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001631 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001632 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1633 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001634 return false;
1635 }
1636 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001637 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001638 }
1639
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001640 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001641 bool Equals(const StringPiece& modified_utf8) const {
1642 // TODO: do not assume C-string representation.
1643 return Equals(modified_utf8.data());
1644 }
1645
1646 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001647 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001648 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001649 return false;
1650 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001651 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001652 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001653 return false;
1654 }
1655 }
1656 return true;
1657 }
1658
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001659 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001660 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001661 if (this->GetLength() != that_length) {
1662 return false;
1663 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001664 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001665 if (this->CharAt(i) != that_chars[that_offset + i]) {
1666 return false;
1667 }
1668 }
1669 return true;
1670 }
1671
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001672 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1673 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 uint16_t* chars = array_->GetData() + offset_;
1675 size_t byte_count(CountUtf8Bytes(chars, count_));
1676 std::string result(byte_count, char(0));
1677 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001678 return result;
1679 }
1680
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001681 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001682 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1683 CharArray* array_;
1684
Carl Shapirof88c9522011-08-06 15:47:38 -07001685 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001686
Elliott Hughes289da822011-08-16 10:11:20 -07001687 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001688
Elliott Hughes289da822011-08-16 10:11:20 -07001689 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001690
1691 static Class* GetJavaLangString() {
1692 DCHECK(java_lang_String_ != NULL);
1693 return java_lang_String_;
1694 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001695
1696 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001697
1698 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001699};
1700
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001701class Throwable : public Object {
1702 private:
1703 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1704 Throwable* cause_;
1705 String* detail_message_;
1706 Object* stack_state_; // Note this is Java volatile:
1707 Object* stack_trace_;
1708 Object* suppressed_exceptions_;
1709
1710 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1711};
1712
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001713class StackTraceElement : public Object {
1714 public:
1715 const String* GetDeclaringClass() const {
1716 return declaring_class_;
1717 }
1718
1719 const String* GetMethodName() const {
1720 return method_name_;
1721 }
1722
1723 const String* GetFileName() const {
1724 return file_name_;
1725 }
1726
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001727 int32_t GetLineNumber() const {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001728 return line_number_;
1729 }
1730
1731 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001732 const String* file_name, int32_t line_number) {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001733 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1734 trace->declaring_class_ = declaring_class;
1735 trace->method_name_ = method_name;
1736 trace->file_name_ = file_name;
1737 trace->line_number_ = line_number;
1738 return trace;
1739 }
1740
1741 static void SetClass(Class* java_lang_StackTraceElement);
1742
1743 static void ResetClass();
1744
1745 private:
1746 const String* declaring_class_;
1747 const String* method_name_;
1748 const String* file_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001749 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001750
1751 static Class* GetStackTraceElement() {
1752 DCHECK(java_lang_StackTraceElement_ != NULL);
1753 return java_lang_StackTraceElement_;
1754 }
1755
1756 static Class* java_lang_StackTraceElement_;
1757 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1758};
1759
Brian Carlstroma663ea52011-08-19 23:33:41 -07001760inline bool Object::IsString() const {
1761 // TODO use "klass_ == String::GetJavaLangString()" instead?
1762 return klass_ == klass_->descriptor_->klass_;
1763}
1764
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001765inline size_t Class::GetTypeSize(String* descriptor) {
1766 switch (descriptor->CharAt(0)) {
1767 case 'B': return 1; // byte
1768 case 'C': return 2; // char
1769 case 'D': return 8; // double
1770 case 'F': return 4; // float
1771 case 'I': return 4; // int
1772 case 'J': return 8; // long
1773 case 'S': return 2; // short
1774 case 'Z': return 1; // boolean
1775 case 'L': return sizeof(Object*);
1776 case '[': return sizeof(Array*);
1777 default:
1778 LOG(ERROR) << "Unknown type " << descriptor;
1779 return 0;
1780 }
1781}
1782
Brian Carlstromb63ec392011-08-27 17:38:27 -07001783inline bool Class::IsArrayClass() const {
1784 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001785}
1786
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001787class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001788 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001789 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001790 }
1791
Brian Carlstrom30b94452011-08-25 21:35:26 -07001792 Class* GetInterface() const {
1793 DCHECK(interface_ != NULL);
1794 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001795 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001796
Brian Carlstrom30b94452011-08-25 21:35:26 -07001797 void SetInterface(Class* interface) {
1798 DCHECK(interface->IsInterface());
1799 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001800 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001801
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001802 private:
1803 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001804 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001805
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001806 public: // TODO: private
1807 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001808 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001809 // this class.
1810 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001811
1812 private:
1813 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001814};
1815
1816} // namespace art
1817
1818#endif // ART_SRC_OBJECT_H_