blob: 1db3e8316702d8592c77bfa877fbc2e117346b00 [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
Brian Carlstrom1f870082011-08-23 16:02:11 -07006#include <vector>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070010#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "globals.h"
12#include "logging.h"
13#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070014#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "offsets.h"
17#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070019#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070020
21namespace art {
22
23class Array;
24class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070025class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070026class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040028class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070029class InterfaceEntry;
30class Monitor;
31class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070032class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070033class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040034class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070035template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070036template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070037typedef PrimitiveArray<uint8_t> BooleanArray;
38typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070039typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070040typedef PrimitiveArray<double> DoubleArray;
41typedef PrimitiveArray<float> FloatArray;
42typedef PrimitiveArray<int32_t> IntArray;
43typedef PrimitiveArray<int64_t> LongArray;
44typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045
Carl Shapiro3ee755d2011-06-28 12:11:04 -070046union JValue {
47 uint8_t z;
48 int8_t b;
49 uint16_t c;
50 int16_t s;
51 int32_t i;
52 int64_t j;
53 float f;
54 double d;
55 Object* l;
56};
57
Brian Carlstrombe977852011-07-19 14:54:54 -070058static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
59static const uint32_t kAccPrivate = 0x0002; // field, method, ic
60static const uint32_t kAccProtected = 0x0004; // field, method, ic
61static const uint32_t kAccStatic = 0x0008; // field, method, ic
62static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
63static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
64static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
65static const uint32_t kAccVolatile = 0x0040; // field
66static const uint32_t kAccBridge = 0x0040; // method (1.5)
67static const uint32_t kAccTransient = 0x0080; // field
68static const uint32_t kAccVarargs = 0x0080; // method (1.5)
69static const uint32_t kAccNative = 0x0100; // method
70static const uint32_t kAccInterface = 0x0200; // class, ic
71static const uint32_t kAccAbstract = 0x0400; // class, method, ic
72static const uint32_t kAccStrict = 0x0800; // method
73static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
74static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
75static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070077static const uint32_t kAccMiranda = 0x8000; // method
78
Brian Carlstroma331b3c2011-07-18 17:47:56 -070079static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
80
Brian Carlstrombe977852011-07-19 14:54:54 -070081static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
82static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070083
Brian Carlstrom1f870082011-08-23 16:02:11 -070084static const uint32_t kAccClassFlagsMask = (kAccPublic
85 | kAccFinal
86 | kAccInterface
87 | kAccAbstract
88 | kAccSynthetic
89 | kAccAnnotation
90 | kAccEnum);
91static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
92 | kAccPrivate
93 | kAccProtected
94 | kAccStatic);
95static const uint32_t kAccFieldFlagsMask = (kAccPublic
96 | kAccPrivate
97 | kAccProtected
98 | kAccStatic
99 | kAccFinal
100 | kAccVolatile
101 | kAccTransient
102 | kAccSynthetic
103 | kAccEnum);
104static const uint32_t kAccMethodFlagsMask = (kAccPublic
105 | kAccPrivate
106 | kAccProtected
107 | kAccStatic
108 | kAccFinal
109 | kAccSynchronized
110 | kAccBridge
111 | kAccVarargs
112 | kAccNative
113 | kAccAbstract
114 | kAccStrict
115 | kAccSynthetic
116 | kAccConstructor
117 | kAccDeclaredSynchronized);
118
119// if only kAccClassIsReference is set, we have a soft reference
120static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
121static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
122static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
123static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
124
125static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
126 | kAccClassIsWeakReference
127 | kAccClassIsFinalizerReference
128 | kAccClassIsPhantomReference);
129
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700130/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700131 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132 */
133/*
134 * A magic value for refOffsets. Ignore the bits and walk the super
135 * chain when this is the value.
136 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
137 * fields followed by 2 ref instance fields.]
138 */
139#define CLASS_WALK_SUPER ((unsigned int)(3))
140#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
141#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
142#define CLASS_OFFSET_ALIGNMENT 4
143#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
144/*
145 * Given an offset, return the bit number which would encode that offset.
146 * Local use only.
147 */
148#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
149 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
150 CLASS_OFFSET_ALIGNMENT)
151/*
152 * Is the given offset too large to be encoded?
153 */
154#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
155 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
156/*
157 * Return a single bit, encoding the offset.
158 * Undefined if the offset is too large, as defined above.
159 */
160#define CLASS_BIT_FROM_OFFSET(byteOffset) \
161 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
162/*
163 * Return an offset, given a bit number as returned from CLZ.
164 */
165#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700166 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167
168
Carl Shapiro1fb86202011-06-27 17:43:13 -0700169class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700170 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700171 static bool InstanceOf(const Object* object, const Class* klass) {
172 if (object == NULL) {
173 return false;
174 }
175 return object->InstanceOf(klass);
176 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700177
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700178 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700179 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700180 return klass_;
181 }
182
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700183 bool InstanceOf(const Class* klass) const;
184
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700185 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700187 void MonitorEnter() {
188 monitor_->Enter();
189 }
190
191 void MonitorExit() {
192 monitor_->Exit();
193 }
194
195 void Notify() {
196 monitor_->Notify();
197 }
198
199 void NotifyAll() {
200 monitor_->NotifyAll();
201 }
202
203 void Wait() {
204 monitor_->Wait();
205 }
206
207 void Wait(int64_t timeout) {
208 monitor_->Wait(timeout);
209 }
210
211 void Wait(int64_t timeout, int32_t nanos) {
212 monitor_->Wait(timeout, nanos);
213 }
214
Brian Carlstrom4873d462011-08-21 15:23:39 -0700215 Object* GetFieldObject(size_t field_offset) const {
216 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
217 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 }
219
220 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
222 *reinterpret_cast<Object**>(raw_addr) = new_value;
223 // TODO: write barrier
224 }
225
Brian Carlstrom4873d462011-08-21 15:23:39 -0700226 uint32_t GetField32(size_t field_offset) const {
227 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
228 return *reinterpret_cast<const uint32_t*>(raw_addr);
229 }
230
231 void SetField32(size_t offset, uint32_t new_value) {
232 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
233 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
234 }
235
236 uint64_t GetField64(size_t field_offset) const {
237 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
238 return *reinterpret_cast<const uint64_t*>(raw_addr);
239 }
240
241 void SetField64(size_t offset, uint64_t new_value) {
242 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
243 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
244 }
245
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700246 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700247
248 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700250 return down_cast<Class*>(this);
251 }
252
253 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700254 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255 return down_cast<const Class*>(this);
256 }
257
Brian Carlstrom4873d462011-08-21 15:23:39 -0700258 bool IsClassClass() const;
259
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700260 bool IsObjectArray() const;
261
262 template<class T>
263 ObjectArray<T>* AsObjectArray() {
264 DCHECK(IsObjectArray());
265 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700266 }
267
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700268 template<class T>
269 const ObjectArray<T>* AsObjectArray() const {
270 DCHECK(IsObjectArray());
271 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700272 }
273
Brian Carlstromb63ec392011-08-27 17:38:27 -0700274 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700275
276 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700277 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700278 return down_cast<Array*>(this);
279 }
280
281 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700282 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700283 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700284 }
285
Brian Carlstroma663ea52011-08-19 23:33:41 -0700286 bool IsString() const;
287
288 String* AsString() {
289 DCHECK(IsString());
290 return down_cast<String*>(this);
291 }
292
293 bool IsMethod() const;
294
295 Method* AsMethod() {
296 DCHECK(IsMethod());
297 return down_cast<Method*>(this);
298 }
299
Brian Carlstrom4873d462011-08-21 15:23:39 -0700300 const Method* AsMethod() const {
301 DCHECK(IsMethod());
302 return down_cast<const Method*>(this);
303 }
304
Brian Carlstroma663ea52011-08-19 23:33:41 -0700305 bool IsField() const;
306
307 Field* AsField() {
308 DCHECK(IsField());
309 return down_cast<Field*>(this);
310 }
311
Brian Carlstrom4873d462011-08-21 15:23:39 -0700312 const Field* AsField() const {
313 DCHECK(IsField());
314 return down_cast<const Field*>(this);
315 }
316
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700317 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700318 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700319
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700320 Monitor* monitor_;
321
322 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700323 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700324};
325
326class ObjectLock {
327 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700328 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700329 CHECK(object != NULL);
330 obj_->MonitorEnter();
331 }
332
333 ~ObjectLock() {
334 obj_->MonitorExit();
335 }
336
337 void Wait(int64_t millis = 0) {
338 return obj_->Wait(millis);
339 }
340
341 void Notify() {
342 obj_->Notify();
343 }
344
345 void NotifyAll() {
346 obj_->NotifyAll();
347 }
348
349 private:
350 Object* obj_;
351 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700352};
353
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400354class AccessibleObject : public Object {
355 private:
356 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
357 uint32_t java_flag_;
358};
359
360class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700361 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700362 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700363 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700364 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700365 }
366
Jesse Wilson14150742011-07-29 19:04:44 -0400367 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700368 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700369 return name_;
370 }
371
372 bool IsStatic() const {
373 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700374 }
375
376 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700377 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700378 }
379
Brian Carlstromae3ac012011-07-27 01:30:28 -0700380 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700381 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700382 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700383 }
384
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700385 uint32_t GetOffset() const {
386 return offset_;
387 }
388
389 void SetOffset(size_t num_bytes) {
390 offset_ = num_bytes;
391 }
392
Brian Carlstrom4873d462011-08-21 15:23:39 -0700393 // field access, null object for static fields
394 bool GetBoolean(const Object* object) const;
395 void SetBoolean(Object* object, bool z) const;
396 int8_t GetByte(const Object* object) const;
397 void SetByte(Object* object, int8_t b) const;
398 uint16_t GetChar(const Object* object) const;
399 void SetChar(Object* object, uint16_t c) const;
400 uint16_t GetShort(const Object* object) const;
401 void SetShort(Object* object, uint16_t s) const;
402 int32_t GetInt(const Object* object) const;
403 void SetInt(Object* object, int32_t i) const;
404 int64_t GetLong(const Object* object) const;
405 void SetLong(Object* object, int64_t j) const;
406 float GetFloat(const Object* object) const;
407 void SetFloat(Object* object, float f) const;
408 double GetDouble(const Object* object) const;
409 void SetDouble(Object* object, double d) const;
410 Object* GetObject(const Object* object) const;
411 void SetObject(Object* object, Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700412
Brian Carlstromb9edb842011-08-28 16:31:06 -0700413 // slow path routines for static field access when field was unresolved at compile time
414 static uint32_t Get32StaticFromCode(uint32_t field_idx, const Method* referrer);
415 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value);
416 static uint64_t Get64StaticFromCode(uint32_t field_idx, const Method* referrer);
417 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value);
418 static Object* GetObjStaticFromCode(uint32_t field_idx, const Method* referrer);
419 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value);
420
Jesse Wilson35baaab2011-08-10 16:18:03 -0400421 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700422
423 // private implemention of field access using raw data
424 uint32_t Get32(const Object* object) const;
425 void Set32(Object* object, uint32_t new_value) const;
426 uint64_t Get64(const Object* object) const;
427 void Set64(Object* object, uint64_t new_value) const;
428 Object* GetObj(const Object* object) const;
429 void SetObj(Object* object, Object* new_value) const;
430
Jesse Wilson35baaab2011-08-10 16:18:03 -0400431 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
432 // The class in which this field is declared.
433 Class* declaring_class_;
434 Object* generic_type_;
435 uint32_t generic_types_are_initialized_;
436 String* name_;
437 uint32_t offset_;
438 Class* type_;
439
440 // e.g. "I", "[C", "Landroid/os/Debug;"
441 StringPiece descriptor_;
442
443 uint32_t access_flags_;
444
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400446 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700447};
448
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400449class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700450 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700451 // An function that invokes a method with an array of its arguments.
452 typedef void InvokeStub(Method* method,
453 Object* obj,
454 Thread* thread,
455 byte* args,
456 JValue* result);
457
Brian Carlstromae3ac012011-07-27 01:30:28 -0700458 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700459 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700460 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700461 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462 }
463
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700464 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700465 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700466 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700467 }
468
Brian Carlstroma0808032011-07-18 00:39:23 -0700469 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700470 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700471 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700472 }
473
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700474 static MemberOffset DeclaringClassOffset() {
475 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700476 }
477
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700478 // Returns true if the method is declared public.
479 bool IsPublic() const {
480 return (access_flags_ & kAccPublic) != 0;
481 }
482
483 // Returns true if the method is declared private.
484 bool IsPrivate() const {
485 return (access_flags_ & kAccPrivate) != 0;
486 }
487
488 // Returns true if the method is declared static.
489 bool IsStatic() const {
490 return (access_flags_ & kAccStatic) != 0;
491 }
492
Brian Carlstrom1f870082011-08-23 16:02:11 -0700493 // Returns true if the method is a constructor.
494 bool IsConstructor() const {
495 return (access_flags_ & kAccConstructor) != 0;
496 }
497
498 // Returns true if the method is static, private, or a constructor.
499 bool IsDirect() const {
500 return IsStatic() || IsPrivate() || IsConstructor();
501 }
502
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700503 // Returns true if the method is declared synchronized.
504 bool IsSynchronized() const {
505 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
506 return (access_flags_ & synchonized) != 0;
507 }
508
509 // Returns true if the method is declared final.
510 bool IsFinal() const {
511 return (access_flags_ & kAccFinal) != 0;
512 }
513
514 // Returns true if the method is declared native.
515 bool IsNative() const {
516 return (access_flags_ & kAccNative) != 0;
517 }
518
519 // Returns true if the method is declared abstract.
520 bool IsAbstract() const {
521 return (access_flags_ & kAccAbstract) != 0;
522 }
523
524 bool IsSynthetic() const {
525 return (access_flags_ & kAccSynthetic) != 0;
526 }
527
528 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700529 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700530
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700531 // Number of argument bytes required for densely packing the
532 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700533 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700534
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700535 // Converts a native PC to a virtual PC. TODO: this is a no-op
536 // until we associate a PC mapping table with each method.
537 uintptr_t ToDexPC(const uintptr_t pc) const {
538 return pc;
539 }
540
541 // Converts a virtual PC to a native PC. TODO: this is a no-op
542 // until we associate a PC mapping table with each method.
543 uintptr_t ToNativePC(const uintptr_t pc) const {
544 return pc;
545 }
546
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700547 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400548 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400549 // the class we are a part of
550 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400551 ObjectArray<Class>* java_exception_types_;
552 Object* java_formal_type_parameters_;
553 Object* java_generic_exception_types_;
554 Object* java_generic_parameter_types_;
555 Object* java_generic_return_type_;
556 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700557 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400558 ObjectArray<Class>* java_parameter_types_;
559 uint32_t java_generic_types_are_initialized_;
560 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700561
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700562 const StringPiece& GetShorty() const {
563 return shorty_;
564 }
565
Ian Rogersb033c752011-07-20 12:22:35 -0700566 bool IsReturnAReference() const {
567 return (shorty_[0] == 'L') || (shorty_[0] == '[');
568 }
569
570 bool IsReturnAFloatOrDouble() const {
571 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
572 }
573
574 bool IsReturnAFloat() const {
575 return shorty_[0] == 'F';
576 }
577
578 bool IsReturnADouble() const {
579 return shorty_[0] == 'D';
580 }
581
582 bool IsReturnALong() const {
583 return shorty_[0] == 'J';
584 }
585
Ian Rogers45a76cb2011-07-21 22:00:15 -0700586 bool IsReturnVoid() const {
587 return shorty_[0] == 'V';
588 }
589
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700590 // "Args" may refer to any of the 3 levels of "Args."
591 // To avoid confusion, our code will denote which "Args" clearly:
592 // 1. UserArgs: Args that a user see.
593 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
594 // receiver.
595 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
596 // E.g., the first in Args is Method* for both static and non-static
597 // methods. And CConvArgs doesn't deal with the receiver because
598 // receiver is hardwired in an implicit register, so CConvArgs doesn't
599 // need to deal with it.
600 //
601 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700602 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700603 // "1 +" because the first in Args is the receiver.
604 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700605 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
606 }
607
608 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700609 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700610 size_t NumReferenceArgs() const;
611
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700612 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700613 size_t NumLongOrDoubleArgs() const;
614
615 // The number of reference arguments to this method before the given
616 // parameter index
617 size_t NumReferenceArgsBefore(unsigned int param) const;
618
619 // Is the given method parameter a reference?
620 bool IsParamAReference(unsigned int param) const;
621
622 // Is the given method parameter a long or double?
623 bool IsParamALongOrDouble(unsigned int param) const;
624
Ian Rogersdf20fe02011-07-20 20:34:16 -0700625 // Size in bytes of the given parameter
626 size_t ParamSize(unsigned int param) const;
627
628 // Size in bytes of the return value
629 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700630
buzbeec143c552011-08-20 17:38:58 -0700631 bool HasCode() {
632 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700633 }
634
Brian Carlstrom83db7722011-08-26 17:32:56 -0700635 const void* GetCode() {
636 return code_;
637 }
638
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700639 void SetCode(const byte* compiled_code,
640 size_t byte_count,
641 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700642 // Copy the code into an executable region.
643 code_instruction_set_ = set;
644 code_area_.reset(MemMap::Map(byte_count,
645 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700646 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700647 byte* code = code_area_->GetAddress();
648 memcpy(code, compiled_code, byte_count);
649 __builtin___clear_cache(code, code + byte_count);
650
651 uintptr_t address = reinterpret_cast<uintptr_t>(code);
652 if (code_instruction_set_ == kThumb2) {
653 // Set the low-order bit so a BLX will switch to Thumb mode
654 address |= 0x1;
655 }
656 code_ = reinterpret_cast<void*>(address);
657 }
658
Shih-wei Liaod11af152011-08-23 16:02:11 -0700659 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
660 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700661 }
662
Shih-wei Liaod11af152011-08-23 16:02:11 -0700663 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
664 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700665 }
666
Shih-wei Liaod11af152011-08-23 16:02:11 -0700667 size_t GetFrameSizeInBytes() const {
668 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700669 }
670
Shih-wei Liaod11af152011-08-23 16:02:11 -0700671 size_t GetReturnPcOffsetInBytes() const {
672 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700673 }
674
buzbeec143c552011-08-20 17:38:58 -0700675 void SetCoreSpillMask(uint32_t core_spill_mask) {
676 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700677 }
678
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700679 static size_t GetCodeOffset() {
680 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700681 }
682
buzbeec143c552011-08-20 17:38:58 -0700683 void SetFpSpillMask(uint32_t fp_spill_mask) {
684 fp_spill_mask_ = fp_spill_mask;
685 }
686
Ian Rogersb033c752011-07-20 12:22:35 -0700687 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700688 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700689 native_method_ = native_method;
690 }
691
Elliott Hughes5174fe62011-08-23 15:12:35 -0700692 void UnregisterNative() {
693 native_method_ = NULL;
694 }
695
Ian Rogersb033c752011-07-20 12:22:35 -0700696 static MemberOffset NativeMethodOffset() {
697 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
698 }
699
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700700 InvokeStub* GetInvokeStub() const {
701 return invoke_stub_;
702 }
703
704 void SetInvokeStub(const InvokeStub* invoke_stub) {
705 invoke_stub_ = invoke_stub;
706 }
707
708 static size_t GetInvokeStubOffset() {
709 return OFFSETOF_MEMBER(Method, invoke_stub_);
710 }
711
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700712 bool HasSameNameAndDescriptor(const Method* that) const;
713
Ian Rogersb033c752011-07-20 12:22:35 -0700714 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715 // access flags; low 16 bits are defined by spec (could be uint16_t?)
716 uint32_t access_flags_;
717
718 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700719 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 //
721 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700722 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700723 uint16_t method_index_;
724
725 // Method bounds; not needed for an abstract method.
726 //
727 // For a native method, we compute the size of the argument list, and
728 // set "insSize" and "registerSize" equal to it.
729 uint16_t num_registers_; // ins + locals
730 uint16_t num_outs_;
731 uint16_t num_ins_;
732
buzbeec143c552011-08-20 17:38:58 -0700733 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700734 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700735
736 // Architecture-dependent register spill masks
737 uint32_t core_spill_mask_;
738 uint32_t fp_spill_mask_;
739
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700740 // The method descriptor. This represents the parameters a method
741 // takes and value it returns. This string is a list of the type
742 // descriptors for the parameters enclosed in parenthesis followed
743 // by the return type descriptor. For example, for the method
744 //
745 // Object mymethod(int i, double d, Thread t)
746 //
747 // the method descriptor would be
748 //
749 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700750 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700751
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700752 // Method prototype descriptor string (return and argument types).
753 uint32_t proto_idx_;
754
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700755 // Offset to the CodeItem.
756 uint32_t code_off_;
757
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700758 // The short-form method descriptor string.
759 StringPiece shorty_;
760
Ian Rogers762400c2011-08-23 12:14:16 -0700761 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
762 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700763 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700764 ObjectArray<Class>* dex_cache_resolved_types_;
765 ObjectArray<Method>* dex_cache_resolved_methods_;
766 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700767 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700768 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700769
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700770 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700771 // Compiled code associated with this method
Elliott Hughes90a33692011-08-30 13:27:07 -0700772 UniquePtr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700773 const void* code_;
774 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700775 InstructionSet code_instruction_set_;
776
777 // Size in bytes of compiled code associated with this method
778 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700779
Ian Rogers762400c2011-08-23 12:14:16 -0700780 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700781 // Offset of PC within compiled code (in bytes)
782 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700783
Ian Rogersb033c752011-07-20 12:22:35 -0700784 // Any native method registered with this method
785 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700786
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700787 // Native invocation stub entry point.
788 const InvokeStub* invoke_stub_;
789
Carl Shapirof88c9522011-08-06 15:47:38 -0700790 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791};
792
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700793class Array : public Object {
794 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700795 static size_t SizeOf(size_t component_count,
796 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700797 return sizeof(Array) + component_count * component_size;
798 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700799
Brian Carlstromb63ec392011-08-27 17:38:27 -0700800 // Given the context of a calling Method, use its DexCache to
801 // resolve a type to an array Class. If it cannot be resolved, throw
802 // an error. If it can, use it to create an array.
803 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
804
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700805 // A convenience for code that doesn't know the component size,
806 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700807 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700808
Brian Carlstromb63ec392011-08-27 17:38:27 -0700809 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700810
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700811 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700812
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700813 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700814 return length_;
815 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700816
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700817 void SetLength(uint32_t length) {
818 length_ = length;
819 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700820
buzbeec143c552011-08-20 17:38:58 -0700821 static MemberOffset LengthOffset() {
822 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
823 }
824
825 static MemberOffset DataOffset() {
826 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
827 }
828
Elliott Hughes289da822011-08-16 10:11:20 -0700829 protected:
830 bool IsValidIndex(int32_t index) const {
831 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700832 Thread* self = Thread::Current();
833 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
834 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700835 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700836 }
837 return true;
838 }
839
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700840 private:
841 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700842 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400843 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
844 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700845 // Marker for the data (used by generated code)
846 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700847
Carl Shapirof88c9522011-08-06 15:47:38 -0700848 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700849};
850
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700851template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700852class ObjectArray : public Array {
853 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700854 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700855 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700856 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700857 }
858
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700859 T* const * GetData() const {
860 return reinterpret_cast<T* const *>(&elements_);
861 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400862
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700863 T** GetData() {
864 return reinterpret_cast<T**>(&elements_);
865 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400866
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700867 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700868 if (!IsValidIndex(i)) {
869 return NULL;
870 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700871 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700872 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700873
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700874 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700875 if (IsValidIndex(i)) {
876 // TODO: ArrayStoreException
877 GetData()[i] = object; // TODO: write barrier
878 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700879 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700880
881 static void Copy(ObjectArray<T>* src, int src_pos,
882 ObjectArray<T>* dst, int dst_pos,
883 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700884 for (size_t i = 0; i < length; i++) {
885 dst->Set(dst_pos + i, src->Get(src_pos + i));
886 }
887 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700888
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700889 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700890 ObjectArray<T>* new_array = Alloc(klass_, new_length);
891 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
892 return new_array;
893 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700894
895 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700896 // Location of first element.
897 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700898
899 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700900};
901
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700902// Type for the InitializedStaticStorage table. Currently the Class
903// provides the static storage. However, this might change to improve
904// image sharing, so we use this type to avoid assumptions on the
905// current storage.
906class StaticStorageBase {};
907
Carl Shapiro1fb86202011-06-27 17:43:13 -0700908// Class objects.
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700909class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700910 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700911
912 // Class Status
913 //
914 // kStatusNotReady: If a Class cannot be found in the class table by
915 // FindClass, it allocates an new one with AllocClass in the
916 // kStatusNotReady and calls LoadClass. Note if it does find a
917 // class, it may not be kStatusResolved and it will try to push it
918 // forward toward kStatusResolved.
919 //
920 // kStatusIdx: LoadClass populates with Class with information from
921 // the DexFile, moving the status to kStatusIdx, indicating that the
922 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700923 // populated based on super_class_type_idx_ and
924 // interfaces_type_idx_. The new Class can then be inserted into the
925 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700926 //
927 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
928 // attempt to move a kStatusIdx class forward to kStatusLoaded by
929 // using ResolveClass to initialize the super_class_ and interfaces_.
930 //
931 // kStatusResolved: Still holding the lock on Class, the ClassLinker
932 // will use LinkClass to link all members, creating Field and Method
933 // objects, setting up the vtable, etc. On success, the class is
934 // marked kStatusResolved.
935
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700936 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700937 kStatusError = -1,
938 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700939 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700940 kStatusLoaded = 2, // DEX idx values resolved
941 kStatusResolved = 3, // part of linking
942 kStatusVerifying = 4, // in the process of being verified
943 kStatusVerified = 5, // logically part of linking; done pre-init
944 kStatusInitializing = 6, // class init in progress
945 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700946 };
947
948 enum PrimitiveType {
949 kPrimNot = -1
950 };
951
Brian Carlstromb63ec392011-08-27 17:38:27 -0700952 // Given the context of a calling Method, use its DexCache to
953 // resolve a type to a Class. If it cannot be resolved, throw an
954 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -0700955 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700956
Brian Carlstrom1f870082011-08-23 16:02:11 -0700957 // Creates a raw object instance but does not invoke the default constructor.
958 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700959
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700960 Class* GetSuperClass() const {
961 return super_class_;
962 }
963
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700964 uint32_t GetSuperClassTypeIdx() const {
965 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966 }
967
968 bool HasSuperClass() const {
969 return super_class_ != NULL;
970 }
971
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700972 bool IsAssignableFrom(const Class* klass) const {
973 DCHECK(klass != NULL);
974 if (this == klass) {
975 return true;
976 }
977 if (IsInterface()) {
978 return klass->Implements(this);
979 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700980 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700981 return IsAssignableFromArray(klass);
982 }
983 return klass->IsSubClass(this);
984 }
985
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700986 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700987 return class_loader_;
988 }
989
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700990 DexCache* GetDexCache() const {
991 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700992 }
993
994 Class* GetComponentType() const {
995 return component_type_;
996 }
997
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700998 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700999
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001000 size_t GetComponentSize() const {
1001 return GetTypeSize(component_type_->descriptor_);
1002 }
1003
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001004 const String* GetDescriptor() const {
1005 DCHECK(descriptor_ != NULL);
1006 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001007 return descriptor_;
1008 }
1009
Brian Carlstrom4873d462011-08-21 15:23:39 -07001010 size_t SizeOf() const {
1011 return class_size_;
1012 }
1013
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001014 Status GetStatus() const {
1015 return status_;
1016 }
1017
1018 void SetStatus(Status new_status) {
1019 // TODO: validate transition
1020 status_ = new_status;
1021 }
1022
Carl Shapiro69759ea2011-07-21 18:13:35 -07001023 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001024 bool IsErroneous() const {
1025 return GetStatus() == kStatusError;
1026 }
1027
Carl Shapiro69759ea2011-07-21 18:13:35 -07001028 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001029 bool IsVerified() const {
1030 return GetStatus() >= kStatusVerified;
1031 }
1032
Carl Shapiro69759ea2011-07-21 18:13:35 -07001033 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001034 bool IsLinked() const {
1035 return GetStatus() >= kStatusResolved;
1036 }
1037
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001038 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001039 bool IsLoaded() const {
1040 return GetStatus() >= kStatusLoaded;
1041 }
1042
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001043 // Returns true if the class is initialized.
1044 bool IsInitialized() const {
1045 return GetStatus() == kStatusInitialized;
1046 }
1047
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001048 // Returns true if this class is in the same packages as that class.
1049 bool IsInSamePackage(const Class* that) const;
1050
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001051 static bool IsInSamePackage(const String* descriptor1,
1052 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001053
1054 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001055 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001056
1057 // Returns true if the class is an interface.
1058 bool IsInterface() const {
1059 return (access_flags_ & kAccInterface) != 0;
1060 }
1061
1062 // Returns true if the class is declared public.
1063 bool IsPublic() const {
1064 return (access_flags_ & kAccPublic) != 0;
1065 }
1066
1067 // Returns true if the class is declared final.
1068 bool IsFinal() const {
1069 return (access_flags_ & kAccFinal) != 0;
1070 }
1071
1072 // Returns true if the class is abstract.
1073 bool IsAbstract() const {
1074 return (access_flags_ & kAccAbstract) != 0;
1075 }
1076
1077 // Returns true if the class is an annotation.
1078 bool IsAnnotation() const {
1079 return (access_flags_ & kAccAnnotation) != 0;
1080 }
1081
1082 // Returns true if the class is a primitive type.
1083 bool IsPrimitive() const {
1084 return primitive_type_ != kPrimNot;
1085 }
1086
Brian Carlstromae3ac012011-07-27 01:30:28 -07001087 // Returns true if the class is synthetic.
1088 bool IsSynthetic() const {
1089 return (access_flags_ & kAccSynthetic) != 0;
1090 }
1091
Brian Carlstrom1f870082011-08-23 16:02:11 -07001092 bool IsReference() const {
1093 return (access_flags_ & kAccClassIsReference) != 0;
1094 }
1095
1096 bool IsWeakReference() const {
1097 return (access_flags_ & kAccClassIsWeakReference) != 0;
1098 }
1099
1100 bool IsSoftReference() const {
1101 return (access_flags_ & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1102 }
1103
1104 bool IsFinalizerReference() const {
1105 return (access_flags_ & kAccClassIsFinalizerReference) != 0;
1106 }
1107
1108 bool IsPhantomReference() const {
1109 return (access_flags_ & kAccClassIsPhantomReference) != 0;
1110 }
1111
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001112 // Returns true if this class can access that class.
1113 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001115 }
1116
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117 // Returns the number of static, private, and constructor methods.
1118 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001119 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001120 }
1121
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001122 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001123 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001124 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001125 }
1126
1127 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001128 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001129 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001130 }
1131
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001132 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001133 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001134
1135 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001136 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001137
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001138 // Returns the number of non-inherited virtual methods.
1139 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001140 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001141 }
1142
1143 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001144 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001145 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001146 }
1147
1148 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001149 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001150 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 }
1152
Brian Carlstrom30b94452011-08-25 21:35:26 -07001153 // Given a method implemented by this class but potentially from a
1154 // super class, return the specific implementation
1155 // method for this class.
1156 Method* FindVirtualMethodForVirtual(Method* method) {
1157 DCHECK(!method->GetDeclaringClass()->IsInterface());
1158 // The argument method may from a super class.
1159 // Use the index to a potentially overriden one for this instance's class.
1160 return vtable_->Get(method->method_index_);
1161 }
1162
1163 // Given a method implemented by this class, but potentially from a
1164 // super class or interface, return the specific implementation
1165 // method for this class.
1166 Method* FindVirtualMethodForInterface(Method* method);
1167
1168 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1169 if (method->GetDeclaringClass()->IsInterface()) {
1170 return FindVirtualMethodForInterface(method);
1171 }
1172 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001173 }
1174
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001175 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1176 const StringPiece& descriptor);
1177
1178 Method* FindVirtualMethod(const StringPiece& name,
1179 const StringPiece& descriptor);
1180
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001181 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001182 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001183 }
1184
Carl Shapiro69759ea2011-07-21 18:13:35 -07001185 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001187 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001188 }
1189
Brian Carlstrom4873d462011-08-21 15:23:39 -07001190 // Returns the number of static fields containing reference types.
1191 size_t NumReferenceStaticFields() const {
1192 return num_reference_static_fields_;
1193 }
1194
Elliott Hughescdf53122011-08-19 15:46:09 -07001195 // Finds the given instance field in this class or a superclass.
1196 Field* FindInstanceField(const StringPiece& name,
1197 const StringPiece& descriptor);
1198
Brian Carlstrom1f870082011-08-23 16:02:11 -07001199 // Finds the given instance field in this class.
Elliott Hughescdf53122011-08-19 15:46:09 -07001200 Field* FindDeclaredInstanceField(const StringPiece& name,
1201 const StringPiece& descriptor);
1202
1203 // Finds the given static field in this class or a superclass.
1204 Field* FindStaticField(const StringPiece& name,
1205 const StringPiece& descriptor);
1206
Brian Carlstrom1f870082011-08-23 16:02:11 -07001207 // Finds the given static field in this class.
Elliott Hughescdf53122011-08-19 15:46:09 -07001208 Field* FindDeclaredStaticField(const StringPiece& name,
1209 const StringPiece& descriptor);
1210
Jesse Wilson35baaab2011-08-10 16:18:03 -04001211 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001212 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001213 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001214 }
1215
Jesse Wilson35baaab2011-08-10 16:18:03 -04001216 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001217 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001218 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 }
1220
1221 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001222 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 }
1224
Jesse Wilson35baaab2011-08-10 16:18:03 -04001225 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001226 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001227 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001228 }
1229
Jesse Wilson35baaab2011-08-10 16:18:03 -04001230 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001231 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001232 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001233 }
1234
Brian Carlstrom4873d462011-08-21 15:23:39 -07001235 uint32_t GetReferenceInstanceOffsets() const {
1236 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001237 }
1238
Brian Carlstrom4873d462011-08-21 15:23:39 -07001239 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1240 reference_instance_offsets_ = new_reference_offsets;
1241 }
1242
1243 uint32_t GetReferenceStaticOffsets() const {
1244 return reference_static_offsets_;
1245 }
1246
1247 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1248 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001249 }
1250
Carl Shapiro69759ea2011-07-21 18:13:35 -07001251 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001252 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001253 }
1254
1255 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001256 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001257 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001258 }
1259
1260 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001261 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001262 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001263 }
1264
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001265 void SetVerifyErrorClass(Class* klass) {
1266 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1267 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1268 klass->SetFieldObject(field_offset, klass);
1269 }
1270
1271 private:
1272 bool Implements(const Class* klass) const;
1273 bool IsArrayAssignableFromArray(const Class* klass) const;
1274 bool IsAssignableFromArray(const Class* klass) const;
1275 bool IsSubClass(const Class* klass) const;
1276
Ian Rogersb033c752011-07-20 12:22:35 -07001277 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001278 // descriptor for the class such as "java.lang.Class" or "[C"
1279 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001280
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001281 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1282 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001283
1284 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001285 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001286
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001287 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001288 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001289 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001290
1291 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001292 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001293
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001294 // If class verify fails, we must return same error on subsequent tries.
1295 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1296 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001297
1298 // threadId, used to check for recursive <clinit> invocation
1299 uint32_t clinit_thread_id_;
1300
1301 // Total object size; used when allocating storage on gc heap. (For
1302 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001304
1305 // For array classes, the class object for base element, for
1306 // instanceof/checkcast (for String[][][], this will be String).
1307 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001308 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001309
1310 // For array classes, the number of array dimensions, e.g. int[][]
1311 // is 2. Otherwise 0.
1312 int32_t array_rank_;
1313
1314 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1315 PrimitiveType primitive_type_;
1316
1317 // The superclass, or NULL if this is java.lang.Object or a
1318 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001319 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001320 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001321
1322 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001323 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001324
1325 // initiating class loader list
1326 // NOTE: for classes with low serialNumber, these are unused, and the
1327 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001328 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001329
1330 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001331 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001332 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001333
1334 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001335 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001336
1337 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001338 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001339
1340 // Virtual method table (vtable), for use by "invoke-virtual". The
1341 // vtable from the superclass is copied in, and virtual methods from
1342 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001343 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001344
Brian Carlstrom30b94452011-08-25 21:35:26 -07001345 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001346 // this class. That means one entry for each interface we support
1347 // directly, indirectly via superclass, or indirectly via
1348 // superinterface. This will be null if neither we nor our
1349 // superclass implement any interfaces.
1350 //
1351 // Why we need this: given "class Foo implements Face", declare
1352 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1353 // is part of the Face interface. We can't easily use a single
1354 // vtable.
1355 //
1356 // For every interface a concrete class implements, we create a list
1357 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001359 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001360 InterfaceEntry* iftable_;
1361
1362 // The interface vtable indices for iftable get stored here. By
1363 // placing them all in a single pool for each class that implements
1364 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001365 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001366 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001367 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001368
1369 // instance fields
1370 //
1371 // These describe the layout of the contents of a
1372 // DataObject-compatible Object. Note that only the fields directly
1373 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001374 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001375 //
1376 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001377 // the beginning of the field list. num_reference_instance_fields_
1378 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001379 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001380
Brian Carlstrom4873d462011-08-21 15:23:39 -07001381 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001382 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001383
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001384 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001385 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001386
1387 // source file name, if known. Otherwise, NULL.
1388 const char* source_file_;
1389
Jesse Wilson7833bd22011-08-09 18:31:44 -04001390 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001391 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001392
Brian Carlstrom4873d462011-08-21 15:23:39 -07001393 // number of static fields that are object refs
1394 size_t num_reference_static_fields_;
1395
1396 // Bitmap of offsets of sfields.
1397 uint32_t reference_static_offsets_;
1398
1399 // Total class size; used when allocating storage on gc heap.
1400 size_t class_size_;
1401
1402 // Location of first static field.
1403 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001404
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001405 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001406 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001407};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001408std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001409
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001410inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001411 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001412 DCHECK(klass_ != NULL);
1413 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001414}
1415
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001416inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001417 Class* java_lang_Class = klass_->klass_;
1418 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001419}
1420
Brian Carlstrom4873d462011-08-21 15:23:39 -07001421inline bool Object::IsClassClass() const {
1422 Class* java_lang_Class = klass_->klass_;
1423 return this == java_lang_Class;
1424}
1425
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001426inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001427 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001428}
1429
Brian Carlstromb63ec392011-08-27 17:38:27 -07001430inline bool Object::IsArrayInstance() const {
1431 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001432}
1433
Brian Carlstroma663ea52011-08-19 23:33:41 -07001434inline bool Object::IsField() const {
1435 Class* java_lang_Class = klass_->klass_;
1436 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1437 return klass_ == java_lang_reflect_Field;
1438}
1439
1440inline bool Object::IsMethod() const {
1441 Class* java_lang_Class = klass_->klass_;
1442 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1443 return klass_ == java_lang_reflect_Method;
1444}
1445
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001446inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001447 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001448 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001449 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001450 if (IsClass()) {
1451 return AsClass()->SizeOf();
1452 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001453 return klass_->object_size_;
1454}
1455
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001456inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001457 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001458}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001459
Brian Carlstrom4873d462011-08-21 15:23:39 -07001460class ClassClass : public Class {
1461 private:
1462 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1463 int32_t padding_;
1464 int64_t serialVersionUID_;
1465 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1466};
1467
1468class StringClass : public Class {
1469 private:
1470 CharArray* ASCII_;
1471 Object* CASE_INSENSITIVE_ORDER_;
1472 uint32_t REPLACEMENT_CHAR_;
1473 int64_t serialVersionUID;
1474 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1475};
1476
1477class FieldClass : public Class {
1478 private:
1479 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1480 uint32_t TYPE_BOOLEAN_;
1481 uint32_t TYPE_BYTE_;
1482 uint32_t TYPE_CHAR_;
1483 uint32_t TYPE_DOUBLE_;
1484 uint32_t TYPE_FLOAT_;
1485 uint32_t TYPE_INTEGER_;
1486 uint32_t TYPE_LONG_;
1487 uint32_t TYPE_SHORT_;
1488 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1489};
1490
1491class MethodClass : public Class {
1492 private:
1493 int32_t DECLARED_;
1494 int32_t PUBLIC_;
1495 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1496};
1497
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001498class DataObject : public Object {
1499 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001500 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001501 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001502 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001503 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001504};
1505
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001506template<class T>
1507class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001508 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001509 typedef T ElementType;
1510
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001511 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001512
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001513 const T* GetData() const {
1514 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001515 }
1516
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001517 T* GetData() {
1518 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001519 }
1520
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001521 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001522 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001523 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001524 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001525 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001526 }
1527
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001528 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001529 // TODO: ArrayStoreException
1530 if (IsValidIndex(i)) {
1531 GetData()[i] = value;
1532 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001533 }
1534
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001535 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001536 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001537 CHECK(array_class != NULL);
1538 array_class_ = array_class;
1539 }
1540
Brian Carlstroma663ea52011-08-19 23:33:41 -07001541 static void ResetArrayClass() {
1542 CHECK(array_class_ != NULL);
1543 array_class_ = NULL;
1544 }
1545
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001546 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001547 // Location of first element.
1548 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001549
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001550 static Class* array_class_;
1551
Carl Shapirof88c9522011-08-06 15:47:38 -07001552 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001553};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001554
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001555class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001556 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001557 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001558 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001559 return array_;
1560 }
1561
Carl Shapirof88c9522011-08-06 15:47:38 -07001562 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001563 return hash_code_;
1564 }
1565
Elliott Hughes814e4032011-08-23 12:07:56 -07001566 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001567 return offset_;
1568 }
1569
Elliott Hughes814e4032011-08-23 12:07:56 -07001570 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001571 return count_;
1572 }
1573
Elliott Hughes814e4032011-08-23 12:07:56 -07001574 int32_t GetUtfLength() const {
1575 return CountUtf8Bytes(array_->GetData(), count_);
1576 }
1577
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001578 // TODO: do we need this? Equals is the only caller, and could
1579 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001580 uint16_t CharAt(int32_t index) const {
1581 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001582 Thread* self = Thread::Current();
1583 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1584 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001585 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001586 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001587 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001588 }
1589
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001590 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001591 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001592 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001593 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001594 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001595 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001596 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001597 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001598 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001599 if (hash_code != 0) {
1600 string->hash_code_ = hash_code;
1601 } else {
1602 string->ComputeHashCode();
1603 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001604 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001605 }
1606
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001607 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001608 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001609 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001610 }
1611
Jesse Wilson8989d992011-08-02 13:39:42 -07001612 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1613 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001614 String* string = Alloc(GetJavaLangString(), utf16_length);
1615 uint16_t* utf16_data_out = string->array_->GetData();
1616 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1617 string->ComputeHashCode();
1618 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001619 }
1620
Brian Carlstroma663ea52011-08-19 23:33:41 -07001621 static void SetClass(Class* java_lang_String);
1622 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001623
Elliott Hughes814e4032011-08-23 12:07:56 -07001624 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001625 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1626 }
1627
Elliott Hughes814e4032011-08-23 12:07:56 -07001628 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom1f870082011-08-23 16:02:11 -07001629 String* string = down_cast<String*>(java_lang_String->AllocObject());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001630 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001631 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001632 return string;
1633 }
1634
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001635 void ComputeHashCode() {
1636 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1637 }
1638
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001639 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001640 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001641 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001642 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1643 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001644 return false;
1645 }
1646 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001647 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001648 }
1649
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001650 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001651 bool Equals(const StringPiece& modified_utf8) const {
1652 // TODO: do not assume C-string representation.
1653 return Equals(modified_utf8.data());
1654 }
1655
1656 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001657 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001658 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001659 return false;
1660 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001661 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001662 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001663 return false;
1664 }
1665 }
1666 return true;
1667 }
1668
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001669 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001670 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001671 if (this->GetLength() != that_length) {
1672 return false;
1673 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001674 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001675 if (this->CharAt(i) != that_chars[that_offset + i]) {
1676 return false;
1677 }
1678 }
1679 return true;
1680 }
1681
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001682 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1683 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001684 uint16_t* chars = array_->GetData() + offset_;
1685 size_t byte_count(CountUtf8Bytes(chars, count_));
1686 std::string result(byte_count, char(0));
1687 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001688 return result;
1689 }
1690
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001691 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001692 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1693 CharArray* array_;
1694
Carl Shapirof88c9522011-08-06 15:47:38 -07001695 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001696
Elliott Hughes289da822011-08-16 10:11:20 -07001697 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001698
Elliott Hughes289da822011-08-16 10:11:20 -07001699 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001700
1701 static Class* GetJavaLangString() {
1702 DCHECK(java_lang_String_ != NULL);
1703 return java_lang_String_;
1704 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001705
1706 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001707
1708 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001709};
1710
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001711class Throwable : public Object {
1712 private:
1713 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1714 Throwable* cause_;
1715 String* detail_message_;
1716 Object* stack_state_; // Note this is Java volatile:
1717 Object* stack_trace_;
1718 Object* suppressed_exceptions_;
1719
1720 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1721};
1722
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001723class StackTraceElement : public Object {
1724 public:
1725 const String* GetDeclaringClass() const {
1726 return declaring_class_;
1727 }
1728
1729 const String* GetMethodName() const {
1730 return method_name_;
1731 }
1732
1733 const String* GetFileName() const {
1734 return file_name_;
1735 }
1736
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001737 int32_t GetLineNumber() const {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001738 return line_number_;
1739 }
1740
1741 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001742 const String* file_name, int32_t line_number) {
Brian Carlstrom1f870082011-08-23 16:02:11 -07001743 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001744 trace->declaring_class_ = declaring_class;
1745 trace->method_name_ = method_name;
1746 trace->file_name_ = file_name;
1747 trace->line_number_ = line_number;
1748 return trace;
1749 }
1750
1751 static void SetClass(Class* java_lang_StackTraceElement);
1752
1753 static void ResetClass();
1754
1755 private:
1756 const String* declaring_class_;
1757 const String* method_name_;
1758 const String* file_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001759 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001760
1761 static Class* GetStackTraceElement() {
1762 DCHECK(java_lang_StackTraceElement_ != NULL);
1763 return java_lang_StackTraceElement_;
1764 }
1765
1766 static Class* java_lang_StackTraceElement_;
1767 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1768};
1769
Brian Carlstroma663ea52011-08-19 23:33:41 -07001770inline bool Object::IsString() const {
1771 // TODO use "klass_ == String::GetJavaLangString()" instead?
1772 return klass_ == klass_->descriptor_->klass_;
1773}
1774
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001775inline size_t Class::GetTypeSize(String* descriptor) {
1776 switch (descriptor->CharAt(0)) {
1777 case 'B': return 1; // byte
1778 case 'C': return 2; // char
1779 case 'D': return 8; // double
1780 case 'F': return 4; // float
1781 case 'I': return 4; // int
1782 case 'J': return 8; // long
1783 case 'S': return 2; // short
1784 case 'Z': return 1; // boolean
1785 case 'L': return sizeof(Object*);
1786 case '[': return sizeof(Array*);
1787 default:
1788 LOG(ERROR) << "Unknown type " << descriptor;
1789 return 0;
1790 }
1791}
1792
Brian Carlstromb63ec392011-08-27 17:38:27 -07001793inline bool Class::IsArrayClass() const {
1794 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001795}
1796
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001797class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001798 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001799 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001800 }
1801
Brian Carlstrom30b94452011-08-25 21:35:26 -07001802 Class* GetInterface() const {
1803 DCHECK(interface_ != NULL);
1804 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001805 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001806
Brian Carlstrom30b94452011-08-25 21:35:26 -07001807 void SetInterface(Class* interface) {
1808 DCHECK(interface->IsInterface());
1809 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001810 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001811
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001812 private:
1813 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001814 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001815
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001816 public: // TODO: private
1817 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001818 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001819 // this class.
1820 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001821
1822 private:
1823 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001824};
1825
1826} // namespace art
1827
1828#endif // ART_SRC_OBJECT_H_