blob: 2cdfae20ce6b69ed245b3615b8b319664dedc0a9 [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 Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -07007#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
11#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070012#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "offsets.h"
14#include "stringpiece.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070015#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17namespace art {
18
19class Array;
20class Class;
Brian Carlstrom83db7722011-08-26 17:32:56 -070021class CodeAndMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070022class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040023class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070024class InterfaceEntry;
25class Monitor;
26class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070027class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040028class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070029template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070030template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070031typedef PrimitiveArray<uint8_t> BooleanArray;
32typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070033typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070034typedef PrimitiveArray<double> DoubleArray;
35typedef PrimitiveArray<float> FloatArray;
36typedef PrimitiveArray<int32_t> IntArray;
37typedef PrimitiveArray<int64_t> LongArray;
38typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070039
Carl Shapiro3ee755d2011-06-28 12:11:04 -070040union JValue {
41 uint8_t z;
42 int8_t b;
43 uint16_t c;
44 int16_t s;
45 int32_t i;
46 int64_t j;
47 float f;
48 double d;
49 Object* l;
50};
51
Brian Carlstrombe977852011-07-19 14:54:54 -070052static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
53static const uint32_t kAccPrivate = 0x0002; // field, method, ic
54static const uint32_t kAccProtected = 0x0004; // field, method, ic
55static const uint32_t kAccStatic = 0x0008; // field, method, ic
56static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
57static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
58static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
59static const uint32_t kAccVolatile = 0x0040; // field
60static const uint32_t kAccBridge = 0x0040; // method (1.5)
61static const uint32_t kAccTransient = 0x0080; // field
62static const uint32_t kAccVarargs = 0x0080; // method (1.5)
63static const uint32_t kAccNative = 0x0100; // method
64static const uint32_t kAccInterface = 0x0200; // class, ic
65static const uint32_t kAccAbstract = 0x0400; // class, method, ic
66static const uint32_t kAccStrict = 0x0800; // method
67static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
68static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
69static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070070
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070071static const uint32_t kAccMiranda = 0x8000; // method
72
Brian Carlstroma331b3c2011-07-18 17:47:56 -070073static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
74
Brian Carlstrombe977852011-07-19 14:54:54 -070075static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
76static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070077
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070078/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070079 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070080 */
81/*
82 * A magic value for refOffsets. Ignore the bits and walk the super
83 * chain when this is the value.
84 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
85 * fields followed by 2 ref instance fields.]
86 */
87#define CLASS_WALK_SUPER ((unsigned int)(3))
88#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
89#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
90#define CLASS_OFFSET_ALIGNMENT 4
91#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
92/*
93 * Given an offset, return the bit number which would encode that offset.
94 * Local use only.
95 */
96#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
97 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
98 CLASS_OFFSET_ALIGNMENT)
99/*
100 * Is the given offset too large to be encoded?
101 */
102#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
103 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
104/*
105 * Return a single bit, encoding the offset.
106 * Undefined if the offset is too large, as defined above.
107 */
108#define CLASS_BIT_FROM_OFFSET(byteOffset) \
109 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
110/*
111 * Return an offset, given a bit number as returned from CLZ.
112 */
113#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700114 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700115
116
Carl Shapiro1fb86202011-06-27 17:43:13 -0700117class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700118 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700119 static bool InstanceOf(const Object* object, const Class* klass) {
120 if (object == NULL) {
121 return false;
122 }
123 return object->InstanceOf(klass);
124 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700125
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700127 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700128 return klass_;
129 }
130
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700131 bool InstanceOf(const Class* klass) const;
132
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700133 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700134
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700135 void MonitorEnter() {
136 monitor_->Enter();
137 }
138
139 void MonitorExit() {
140 monitor_->Exit();
141 }
142
143 void Notify() {
144 monitor_->Notify();
145 }
146
147 void NotifyAll() {
148 monitor_->NotifyAll();
149 }
150
151 void Wait() {
152 monitor_->Wait();
153 }
154
155 void Wait(int64_t timeout) {
156 monitor_->Wait(timeout);
157 }
158
159 void Wait(int64_t timeout, int32_t nanos) {
160 monitor_->Wait(timeout, nanos);
161 }
162
Brian Carlstrom4873d462011-08-21 15:23:39 -0700163 Object* GetFieldObject(size_t field_offset) const {
164 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
165 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 }
167
168 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700169 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
170 *reinterpret_cast<Object**>(raw_addr) = new_value;
171 // TODO: write barrier
172 }
173
Brian Carlstrom4873d462011-08-21 15:23:39 -0700174 uint32_t GetField32(size_t field_offset) const {
175 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
176 return *reinterpret_cast<const uint32_t*>(raw_addr);
177 }
178
179 void SetField32(size_t offset, uint32_t new_value) {
180 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
181 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
182 }
183
184 uint64_t GetField64(size_t field_offset) const {
185 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
186 return *reinterpret_cast<const uint64_t*>(raw_addr);
187 }
188
189 void SetField64(size_t offset, uint64_t new_value) {
190 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
191 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
192 }
193
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700194 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700195
196 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700197 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 return down_cast<Class*>(this);
199 }
200
201 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700202 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203 return down_cast<const Class*>(this);
204 }
205
Brian Carlstrom4873d462011-08-21 15:23:39 -0700206 bool IsClassClass() const;
207
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700208 bool IsObjectArray() const;
209
210 template<class T>
211 ObjectArray<T>* AsObjectArray() {
212 DCHECK(IsObjectArray());
213 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700214 }
215
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700216 template<class T>
217 const ObjectArray<T>* AsObjectArray() const {
218 DCHECK(IsObjectArray());
219 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700220 }
221
222 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700223 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700224 return true;
225 }
226
227 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700228 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700229 return true;
230 }
231
232 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700233 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700234 return true;
235 }
236
237 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700238 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 return true;
240 }
241
242 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700243 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244 return true;
245 }
246
Brian Carlstromb63ec392011-08-27 17:38:27 -0700247 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700248
249 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700250 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 return down_cast<Array*>(this);
252 }
253
254 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700255 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700256 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700257 }
258
Brian Carlstroma663ea52011-08-19 23:33:41 -0700259 bool IsString() const;
260
261 String* AsString() {
262 DCHECK(IsString());
263 return down_cast<String*>(this);
264 }
265
266 bool IsMethod() const;
267
268 Method* AsMethod() {
269 DCHECK(IsMethod());
270 return down_cast<Method*>(this);
271 }
272
Brian Carlstrom4873d462011-08-21 15:23:39 -0700273 const Method* AsMethod() const {
274 DCHECK(IsMethod());
275 return down_cast<const Method*>(this);
276 }
277
Brian Carlstroma663ea52011-08-19 23:33:41 -0700278 bool IsField() const;
279
280 Field* AsField() {
281 DCHECK(IsField());
282 return down_cast<Field*>(this);
283 }
284
Brian Carlstrom4873d462011-08-21 15:23:39 -0700285 const Field* AsField() const {
286 DCHECK(IsField());
287 return down_cast<const Field*>(this);
288 }
289
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700290 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700291 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700292
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700293 Monitor* monitor_;
294
295 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700296 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700297};
298
299class ObjectLock {
300 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700301 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302 CHECK(object != NULL);
303 obj_->MonitorEnter();
304 }
305
306 ~ObjectLock() {
307 obj_->MonitorExit();
308 }
309
310 void Wait(int64_t millis = 0) {
311 return obj_->Wait(millis);
312 }
313
314 void Notify() {
315 obj_->Notify();
316 }
317
318 void NotifyAll() {
319 obj_->NotifyAll();
320 }
321
322 private:
323 Object* obj_;
324 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700325};
326
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400327class AccessibleObject : public Object {
328 private:
329 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
330 uint32_t java_flag_;
331};
332
333class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700334 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700335 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700336 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700337 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700338 }
339
Jesse Wilson14150742011-07-29 19:04:44 -0400340 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700341 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700342 return name_;
343 }
344
345 bool IsStatic() const {
346 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347 }
348
349 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700350 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700351 }
352
Brian Carlstromae3ac012011-07-27 01:30:28 -0700353 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700354 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700355 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700356 }
357
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700358 uint32_t GetOffset() const {
359 return offset_;
360 }
361
362 void SetOffset(size_t num_bytes) {
363 offset_ = num_bytes;
364 }
365
Brian Carlstrom4873d462011-08-21 15:23:39 -0700366 // field access, null object for static fields
367 bool GetBoolean(const Object* object) const;
368 void SetBoolean(Object* object, bool z) const;
369 int8_t GetByte(const Object* object) const;
370 void SetByte(Object* object, int8_t b) const;
371 uint16_t GetChar(const Object* object) const;
372 void SetChar(Object* object, uint16_t c) const;
373 uint16_t GetShort(const Object* object) const;
374 void SetShort(Object* object, uint16_t s) const;
375 int32_t GetInt(const Object* object) const;
376 void SetInt(Object* object, int32_t i) const;
377 int64_t GetLong(const Object* object) const;
378 void SetLong(Object* object, int64_t j) const;
379 float GetFloat(const Object* object) const;
380 void SetFloat(Object* object, float f) const;
381 double GetDouble(const Object* object) const;
382 void SetDouble(Object* object, double d) const;
383 Object* GetObject(const Object* object) const;
384 void SetObject(Object* object, Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700385
Jesse Wilson35baaab2011-08-10 16:18:03 -0400386 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700387
388 // private implemention of field access using raw data
389 uint32_t Get32(const Object* object) const;
390 void Set32(Object* object, uint32_t new_value) const;
391 uint64_t Get64(const Object* object) const;
392 void Set64(Object* object, uint64_t new_value) const;
393 Object* GetObj(const Object* object) const;
394 void SetObj(Object* object, Object* new_value) const;
395
Jesse Wilson35baaab2011-08-10 16:18:03 -0400396 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
397 // The class in which this field is declared.
398 Class* declaring_class_;
399 Object* generic_type_;
400 uint32_t generic_types_are_initialized_;
401 String* name_;
402 uint32_t offset_;
403 Class* type_;
404
405 // e.g. "I", "[C", "Landroid/os/Debug;"
406 StringPiece descriptor_;
407
408 uint32_t access_flags_;
409
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700410 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400411 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700412};
413
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400414class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700415 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700416 // An function that invokes a method with an array of its arguments.
417 typedef void InvokeStub(Method* method,
418 Object* obj,
419 Thread* thread,
420 byte* args,
421 JValue* result);
422
Brian Carlstromae3ac012011-07-27 01:30:28 -0700423 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700424 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700425 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700426 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700427 }
428
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700429 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700430 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700431 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700432 }
433
Brian Carlstroma0808032011-07-18 00:39:23 -0700434 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700435 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700436 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700437 }
438
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700439 static MemberOffset DeclaringClassOffset() {
440 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700441 }
442
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700443 // Returns true if the method is declared public.
444 bool IsPublic() const {
445 return (access_flags_ & kAccPublic) != 0;
446 }
447
448 // Returns true if the method is declared private.
449 bool IsPrivate() const {
450 return (access_flags_ & kAccPrivate) != 0;
451 }
452
453 // Returns true if the method is declared static.
454 bool IsStatic() const {
455 return (access_flags_ & kAccStatic) != 0;
456 }
457
458 // Returns true if the method is declared synchronized.
459 bool IsSynchronized() const {
460 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
461 return (access_flags_ & synchonized) != 0;
462 }
463
464 // Returns true if the method is declared final.
465 bool IsFinal() const {
466 return (access_flags_ & kAccFinal) != 0;
467 }
468
469 // Returns true if the method is declared native.
470 bool IsNative() const {
471 return (access_flags_ & kAccNative) != 0;
472 }
473
474 // Returns true if the method is declared abstract.
475 bool IsAbstract() const {
476 return (access_flags_ & kAccAbstract) != 0;
477 }
478
479 bool IsSynthetic() const {
480 return (access_flags_ & kAccSynthetic) != 0;
481 }
482
483 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700484 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700485
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700486 // Number of argument bytes required for densely packing the
487 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700488 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700489
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700490 // Converts a native PC to a virtual PC. TODO: this is a no-op
491 // until we associate a PC mapping table with each method.
492 uintptr_t ToDexPC(const uintptr_t pc) const {
493 return pc;
494 }
495
496 // Converts a virtual PC to a native PC. TODO: this is a no-op
497 // until we associate a PC mapping table with each method.
498 uintptr_t ToNativePC(const uintptr_t pc) const {
499 return pc;
500 }
501
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700502 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400503 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400504 // the class we are a part of
505 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400506 ObjectArray<Class>* java_exception_types_;
507 Object* java_formal_type_parameters_;
508 Object* java_generic_exception_types_;
509 Object* java_generic_parameter_types_;
510 Object* java_generic_return_type_;
511 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700512 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400513 ObjectArray<Class>* java_parameter_types_;
514 uint32_t java_generic_types_are_initialized_;
515 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700516
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700517 const StringPiece& GetShorty() const {
518 return shorty_;
519 }
520
Ian Rogersb033c752011-07-20 12:22:35 -0700521 bool IsReturnAReference() const {
522 return (shorty_[0] == 'L') || (shorty_[0] == '[');
523 }
524
525 bool IsReturnAFloatOrDouble() const {
526 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
527 }
528
529 bool IsReturnAFloat() const {
530 return shorty_[0] == 'F';
531 }
532
533 bool IsReturnADouble() const {
534 return shorty_[0] == 'D';
535 }
536
537 bool IsReturnALong() const {
538 return shorty_[0] == 'J';
539 }
540
Ian Rogers45a76cb2011-07-21 22:00:15 -0700541 bool IsReturnVoid() const {
542 return shorty_[0] == 'V';
543 }
544
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700545 // "Args" may refer to any of the 3 levels of "Args."
546 // To avoid confusion, our code will denote which "Args" clearly:
547 // 1. UserArgs: Args that a user see.
548 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
549 // receiver.
550 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
551 // E.g., the first in Args is Method* for both static and non-static
552 // methods. And CConvArgs doesn't deal with the receiver because
553 // receiver is hardwired in an implicit register, so CConvArgs doesn't
554 // need to deal with it.
555 //
556 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700557 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700558 // "1 +" because the first in Args is the receiver.
559 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700560 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
561 }
562
563 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700564 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700565 size_t NumReferenceArgs() const;
566
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700567 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700568 size_t NumLongOrDoubleArgs() const;
569
570 // The number of reference arguments to this method before the given
571 // parameter index
572 size_t NumReferenceArgsBefore(unsigned int param) const;
573
574 // Is the given method parameter a reference?
575 bool IsParamAReference(unsigned int param) const;
576
577 // Is the given method parameter a long or double?
578 bool IsParamALongOrDouble(unsigned int param) const;
579
Ian Rogersdf20fe02011-07-20 20:34:16 -0700580 // Size in bytes of the given parameter
581 size_t ParamSize(unsigned int param) const;
582
583 // Size in bytes of the return value
584 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700585
buzbeec143c552011-08-20 17:38:58 -0700586 bool HasCode() {
587 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700588 }
589
Brian Carlstrom83db7722011-08-26 17:32:56 -0700590 const void* GetCode() {
591 return code_;
592 }
593
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700594 void SetCode(const byte* compiled_code,
595 size_t byte_count,
596 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700597 // Copy the code into an executable region.
598 code_instruction_set_ = set;
599 code_area_.reset(MemMap::Map(byte_count,
600 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700601 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700602 byte* code = code_area_->GetAddress();
603 memcpy(code, compiled_code, byte_count);
604 __builtin___clear_cache(code, code + byte_count);
605
606 uintptr_t address = reinterpret_cast<uintptr_t>(code);
607 if (code_instruction_set_ == kThumb2) {
608 // Set the low-order bit so a BLX will switch to Thumb mode
609 address |= 0x1;
610 }
611 code_ = reinterpret_cast<void*>(address);
612 }
613
Shih-wei Liaod11af152011-08-23 16:02:11 -0700614 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
615 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700616 }
617
Shih-wei Liaod11af152011-08-23 16:02:11 -0700618 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
619 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700620 }
621
Shih-wei Liaod11af152011-08-23 16:02:11 -0700622 size_t GetFrameSizeInBytes() const {
623 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700624 }
625
Shih-wei Liaod11af152011-08-23 16:02:11 -0700626 size_t GetReturnPcOffsetInBytes() const {
627 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700628 }
629
buzbeec143c552011-08-20 17:38:58 -0700630 void SetCoreSpillMask(uint32_t core_spill_mask) {
631 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700632 }
633
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700634 static size_t GetCodeOffset() {
635 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700636 }
637
buzbeec143c552011-08-20 17:38:58 -0700638 void SetFpSpillMask(uint32_t fp_spill_mask) {
639 fp_spill_mask_ = fp_spill_mask;
640 }
641
Ian Rogersb033c752011-07-20 12:22:35 -0700642 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700643 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700644 native_method_ = native_method;
645 }
646
Elliott Hughes5174fe62011-08-23 15:12:35 -0700647 void UnregisterNative() {
648 native_method_ = NULL;
649 }
650
Ian Rogersb033c752011-07-20 12:22:35 -0700651 static MemberOffset NativeMethodOffset() {
652 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
653 }
654
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700655 InvokeStub* GetInvokeStub() const {
656 return invoke_stub_;
657 }
658
659 void SetInvokeStub(const InvokeStub* invoke_stub) {
660 invoke_stub_ = invoke_stub;
661 }
662
663 static size_t GetInvokeStubOffset() {
664 return OFFSETOF_MEMBER(Method, invoke_stub_);
665 }
666
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700667 bool HasSameNameAndDescriptor(const Method* that) const;
668
Ian Rogersb033c752011-07-20 12:22:35 -0700669 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700670 // access flags; low 16 bits are defined by spec (could be uint16_t?)
671 uint32_t access_flags_;
672
673 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700674 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700675 //
676 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700677 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700678 uint16_t method_index_;
679
680 // Method bounds; not needed for an abstract method.
681 //
682 // For a native method, we compute the size of the argument list, and
683 // set "insSize" and "registerSize" equal to it.
684 uint16_t num_registers_; // ins + locals
685 uint16_t num_outs_;
686 uint16_t num_ins_;
687
buzbeec143c552011-08-20 17:38:58 -0700688 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700689 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700690
691 // Architecture-dependent register spill masks
692 uint32_t core_spill_mask_;
693 uint32_t fp_spill_mask_;
694
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700695 // The method descriptor. This represents the parameters a method
696 // takes and value it returns. This string is a list of the type
697 // descriptors for the parameters enclosed in parenthesis followed
698 // by the return type descriptor. For example, for the method
699 //
700 // Object mymethod(int i, double d, Thread t)
701 //
702 // the method descriptor would be
703 //
704 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700705 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700706
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700707 // Method prototype descriptor string (return and argument types).
708 uint32_t proto_idx_;
709
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700710 // Offset to the CodeItem.
711 uint32_t code_off_;
712
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700713 // The short-form method descriptor string.
714 StringPiece shorty_;
715
Ian Rogers762400c2011-08-23 12:14:16 -0700716 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
717 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700718 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700719 ObjectArray<Class>* dex_cache_types_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700720 ObjectArray<Method>* dex_cache_methods_;
721 ObjectArray<Field>* dex_cache_fields_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700722 CodeAndMethods* dex_cache_code_and_methods_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700723
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700724 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700725 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700726 scoped_ptr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700727 const void* code_;
728 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700729 InstructionSet code_instruction_set_;
730
731 // Size in bytes of compiled code associated with this method
732 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700733
Ian Rogers762400c2011-08-23 12:14:16 -0700734 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700735 // Offset of PC within compiled code (in bytes)
736 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700737
Ian Rogersb033c752011-07-20 12:22:35 -0700738 // Any native method registered with this method
739 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700740
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700741 // Native invocation stub entry point.
742 const InvokeStub* invoke_stub_;
743
Carl Shapirof88c9522011-08-06 15:47:38 -0700744 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745};
746
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700747class Array : public Object {
748 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700749 static size_t SizeOf(size_t component_count,
750 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700751 return sizeof(Array) + component_count * component_size;
752 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700753
Brian Carlstromb63ec392011-08-27 17:38:27 -0700754 // Given the context of a calling Method, use its DexCache to
755 // resolve a type to an array Class. If it cannot be resolved, throw
756 // an error. If it can, use it to create an array.
757 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
758
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700759 // A convenience for code that doesn't know the component size,
760 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700761 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700762
Brian Carlstromb63ec392011-08-27 17:38:27 -0700763 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700764
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700765 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700766
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700767 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700768 return length_;
769 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700770
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700771 void SetLength(uint32_t length) {
772 length_ = length;
773 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700774
buzbeec143c552011-08-20 17:38:58 -0700775 static MemberOffset LengthOffset() {
776 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
777 }
778
779 static MemberOffset DataOffset() {
780 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
781 }
782
Elliott Hughes289da822011-08-16 10:11:20 -0700783 protected:
784 bool IsValidIndex(int32_t index) const {
785 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700786 Thread* self = Thread::Current();
787 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
788 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700789 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700790 }
791 return true;
792 }
793
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700794 private:
795 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700796 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400797 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
798 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700799 // Marker for the data (used by generated code)
800 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700801
Carl Shapirof88c9522011-08-06 15:47:38 -0700802 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700803};
804
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700805template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700806class ObjectArray : public Array {
807 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700808 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700809 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700810 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700811 }
812
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700813 T* const * GetData() const {
814 return reinterpret_cast<T* const *>(&elements_);
815 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400816
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700817 T** GetData() {
818 return reinterpret_cast<T**>(&elements_);
819 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400820
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700821 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700822 if (!IsValidIndex(i)) {
823 return NULL;
824 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700825 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700826 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700827
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700828 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700829 if (IsValidIndex(i)) {
830 // TODO: ArrayStoreException
831 GetData()[i] = object; // TODO: write barrier
832 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700833 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700834
835 static void Copy(ObjectArray<T>* src, int src_pos,
836 ObjectArray<T>* dst, int dst_pos,
837 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700838 for (size_t i = 0; i < length; i++) {
839 dst->Set(dst_pos + i, src->Get(src_pos + i));
840 }
841 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700842
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700843 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700844 ObjectArray<T>* new_array = Alloc(klass_, new_length);
845 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
846 return new_array;
847 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700848
849 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700850 // Location of first element.
851 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700852
853 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700854};
855
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700856// ClassLoader objects.
857class ClassLoader : public Object {
858 public:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700859 const std::vector<const DexFile*>& GetClassPath() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700860 return class_path_;
861 }
862 void SetClassPath(std::vector<const DexFile*>& class_path) {
863 DCHECK_EQ(0U, class_path_.size());
864 class_path_ = class_path;
865 }
866
867 private:
868 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
869 Object* packages_;
870 ClassLoader* parent_;
871
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700872 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700873 std::vector<const DexFile*> class_path_;
874
Carl Shapirof88c9522011-08-06 15:47:38 -0700875 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700876};
877
878class BaseDexClassLoader : public ClassLoader {
879 private:
880 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
881 String* original_path_;
882 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700883 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700884};
885
886class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700887 public:
888 static PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
889 static void SetClass(Class* dalvik_system_PathClassLoader);
890 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700891 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700892 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700893 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700894};
895
Carl Shapiro1fb86202011-06-27 17:43:13 -0700896// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700897class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700898 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700899
900 // Class Status
901 //
902 // kStatusNotReady: If a Class cannot be found in the class table by
903 // FindClass, it allocates an new one with AllocClass in the
904 // kStatusNotReady and calls LoadClass. Note if it does find a
905 // class, it may not be kStatusResolved and it will try to push it
906 // forward toward kStatusResolved.
907 //
908 // kStatusIdx: LoadClass populates with Class with information from
909 // the DexFile, moving the status to kStatusIdx, indicating that the
910 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700911 // populated based on super_class_type_idx_ and
912 // interfaces_type_idx_. The new Class can then be inserted into the
913 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700914 //
915 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
916 // attempt to move a kStatusIdx class forward to kStatusLoaded by
917 // using ResolveClass to initialize the super_class_ and interfaces_.
918 //
919 // kStatusResolved: Still holding the lock on Class, the ClassLinker
920 // will use LinkClass to link all members, creating Field and Method
921 // objects, setting up the vtable, etc. On success, the class is
922 // marked kStatusResolved.
923
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700924 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700925 kStatusError = -1,
926 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700927 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700928 kStatusLoaded = 2, // DEX idx values resolved
929 kStatusResolved = 3, // part of linking
930 kStatusVerifying = 4, // in the process of being verified
931 kStatusVerified = 5, // logically part of linking; done pre-init
932 kStatusInitializing = 6, // class init in progress
933 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700934 };
935
936 enum PrimitiveType {
937 kPrimNot = -1
938 };
939
Brian Carlstromb63ec392011-08-27 17:38:27 -0700940 // Given the context of a calling Method, use its DexCache to
941 // resolve a type to a Class. If it cannot be resolved, throw an
942 // error. If it can, use it to create an instance.
943 static Object* NewInstanceFromCode(uint32_t type_idx, Method* method);
944
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700945 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700946 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700947 return Heap::AllocObject(this, this->object_size_);
948 }
949
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700950 Class* GetSuperClass() const {
951 return super_class_;
952 }
953
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700954 uint32_t GetSuperClassTypeIdx() const {
955 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700956 }
957
958 bool HasSuperClass() const {
959 return super_class_ != NULL;
960 }
961
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700962 bool IsAssignableFrom(const Class* klass) const {
963 DCHECK(klass != NULL);
964 if (this == klass) {
965 return true;
966 }
967 if (IsInterface()) {
968 return klass->Implements(this);
969 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700970 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700971 return IsAssignableFromArray(klass);
972 }
973 return klass->IsSubClass(this);
974 }
975
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700976 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700977 return class_loader_;
978 }
979
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700980 DexCache* GetDexCache() const {
981 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700982 }
983
984 Class* GetComponentType() const {
985 return component_type_;
986 }
987
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700988 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700989
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700990 size_t GetComponentSize() const {
991 return GetTypeSize(component_type_->descriptor_);
992 }
993
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700994 const String* GetDescriptor() const {
995 DCHECK(descriptor_ != NULL);
996 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 return descriptor_;
998 }
999
Brian Carlstrom4873d462011-08-21 15:23:39 -07001000 size_t SizeOf() const {
1001 return class_size_;
1002 }
1003
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001004 Status GetStatus() const {
1005 return status_;
1006 }
1007
1008 void SetStatus(Status new_status) {
1009 // TODO: validate transition
1010 status_ = new_status;
1011 }
1012
Carl Shapiro69759ea2011-07-21 18:13:35 -07001013 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001014 bool IsErroneous() const {
1015 return GetStatus() == kStatusError;
1016 }
1017
Carl Shapiro69759ea2011-07-21 18:13:35 -07001018 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 bool IsVerified() const {
1020 return GetStatus() >= kStatusVerified;
1021 }
1022
Carl Shapiro69759ea2011-07-21 18:13:35 -07001023 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001024 bool IsLinked() const {
1025 return GetStatus() >= kStatusResolved;
1026 }
1027
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001028 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001029 bool IsLoaded() const {
1030 return GetStatus() >= kStatusLoaded;
1031 }
1032
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001033 // Returns true if the class is initialized.
1034 bool IsInitialized() const {
1035 return GetStatus() == kStatusInitialized;
1036 }
1037
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001038 // Returns true if this class is in the same packages as that class.
1039 bool IsInSamePackage(const Class* that) const;
1040
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001041 static bool IsInSamePackage(const String* descriptor1,
1042 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001043
1044 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001045 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001046
1047 // Returns true if the class is an interface.
1048 bool IsInterface() const {
1049 return (access_flags_ & kAccInterface) != 0;
1050 }
1051
1052 // Returns true if the class is declared public.
1053 bool IsPublic() const {
1054 return (access_flags_ & kAccPublic) != 0;
1055 }
1056
1057 // Returns true if the class is declared final.
1058 bool IsFinal() const {
1059 return (access_flags_ & kAccFinal) != 0;
1060 }
1061
1062 // Returns true if the class is abstract.
1063 bool IsAbstract() const {
1064 return (access_flags_ & kAccAbstract) != 0;
1065 }
1066
1067 // Returns true if the class is an annotation.
1068 bool IsAnnotation() const {
1069 return (access_flags_ & kAccAnnotation) != 0;
1070 }
1071
1072 // Returns true if the class is a primitive type.
1073 bool IsPrimitive() const {
1074 return primitive_type_ != kPrimNot;
1075 }
1076
Brian Carlstromae3ac012011-07-27 01:30:28 -07001077 // Returns true if the class is synthetic.
1078 bool IsSynthetic() const {
1079 return (access_flags_ & kAccSynthetic) != 0;
1080 }
1081
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001082 // Returns true if this class can access that class.
1083 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001084 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001085 }
1086
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 // Returns the number of static, private, and constructor methods.
1088 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001089 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001090 }
1091
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001092 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001093 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001094 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001095 }
1096
1097 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001098 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001099 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001100 }
1101
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001102 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001103 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001104
1105 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001106 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001107
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001108 // Returns the number of non-inherited virtual methods.
1109 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001110 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001111 }
1112
1113 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001114 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001115 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001116 }
1117
1118 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001119 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001120 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001121 }
1122
Brian Carlstrom30b94452011-08-25 21:35:26 -07001123 // Given a method implemented by this class but potentially from a
1124 // super class, return the specific implementation
1125 // method for this class.
1126 Method* FindVirtualMethodForVirtual(Method* method) {
1127 DCHECK(!method->GetDeclaringClass()->IsInterface());
1128 // The argument method may from a super class.
1129 // Use the index to a potentially overriden one for this instance's class.
1130 return vtable_->Get(method->method_index_);
1131 }
1132
1133 // Given a method implemented by this class, but potentially from a
1134 // super class or interface, return the specific implementation
1135 // method for this class.
1136 Method* FindVirtualMethodForInterface(Method* method);
1137
1138 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1139 if (method->GetDeclaringClass()->IsInterface()) {
1140 return FindVirtualMethodForInterface(method);
1141 }
1142 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001143 }
1144
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001145 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1146 const StringPiece& descriptor);
1147
1148 Method* FindVirtualMethod(const StringPiece& name,
1149 const StringPiece& descriptor);
1150
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001151 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001152 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001153 }
1154
Carl Shapiro69759ea2011-07-21 18:13:35 -07001155 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001156 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001157 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001158 }
1159
Brian Carlstrom4873d462011-08-21 15:23:39 -07001160 // Returns the number of static fields containing reference types.
1161 size_t NumReferenceStaticFields() const {
1162 return num_reference_static_fields_;
1163 }
1164
Elliott Hughescdf53122011-08-19 15:46:09 -07001165 // Finds the given instance field in this class or a superclass.
1166 Field* FindInstanceField(const StringPiece& name,
1167 const StringPiece& descriptor);
1168
1169 Field* FindDeclaredInstanceField(const StringPiece& name,
1170 const StringPiece& descriptor);
1171
1172 // Finds the given static field in this class or a superclass.
1173 Field* FindStaticField(const StringPiece& name,
1174 const StringPiece& descriptor);
1175
1176 Field* FindDeclaredStaticField(const StringPiece& name,
1177 const StringPiece& descriptor);
1178
Jesse Wilson35baaab2011-08-10 16:18:03 -04001179 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001180 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001181 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001182 }
1183
Jesse Wilson35baaab2011-08-10 16:18:03 -04001184 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001185 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001186 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001187 }
1188
1189 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001190 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001191 }
1192
Jesse Wilson35baaab2011-08-10 16:18:03 -04001193 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001194 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001195 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001196 }
1197
Jesse Wilson35baaab2011-08-10 16:18:03 -04001198 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001199 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001200 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 }
1202
Brian Carlstrom4873d462011-08-21 15:23:39 -07001203 uint32_t GetReferenceInstanceOffsets() const {
1204 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 }
1206
Brian Carlstrom4873d462011-08-21 15:23:39 -07001207 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1208 reference_instance_offsets_ = new_reference_offsets;
1209 }
1210
1211 uint32_t GetReferenceStaticOffsets() const {
1212 return reference_static_offsets_;
1213 }
1214
1215 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1216 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001217 }
1218
Carl Shapiro69759ea2011-07-21 18:13:35 -07001219 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001220 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001221 }
1222
1223 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001224 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001225 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001226 }
1227
1228 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001229 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001230 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001231 }
1232
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001233 void SetVerifyErrorClass(Class* klass) {
1234 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1235 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1236 klass->SetFieldObject(field_offset, klass);
1237 }
1238
1239 private:
1240 bool Implements(const Class* klass) const;
1241 bool IsArrayAssignableFromArray(const Class* klass) const;
1242 bool IsAssignableFromArray(const Class* klass) const;
1243 bool IsSubClass(const Class* klass) const;
1244
Ian Rogersb033c752011-07-20 12:22:35 -07001245 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001246 // descriptor for the class such as "java.lang.Class" or "[C"
1247 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001248
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001249 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1250 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001251
1252 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001253 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001254
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001255 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001256 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001257 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001258
1259 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001260 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001261
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001262 // If class verify fails, we must return same error on subsequent tries.
1263 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1264 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001265
1266 // threadId, used to check for recursive <clinit> invocation
1267 uint32_t clinit_thread_id_;
1268
1269 // Total object size; used when allocating storage on gc heap. (For
1270 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001271 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001272
1273 // For array classes, the class object for base element, for
1274 // instanceof/checkcast (for String[][][], this will be String).
1275 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001276 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001277
1278 // For array classes, the number of array dimensions, e.g. int[][]
1279 // is 2. Otherwise 0.
1280 int32_t array_rank_;
1281
1282 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1283 PrimitiveType primitive_type_;
1284
1285 // The superclass, or NULL if this is java.lang.Object or a
1286 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001287 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001288 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001289
1290 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001291 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001292
1293 // initiating class loader list
1294 // NOTE: for classes with low serialNumber, these are unused, and the
1295 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001296 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001297
1298 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001299 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001300 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001301
1302 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001303 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001304
1305 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001306 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001307
1308 // Virtual method table (vtable), for use by "invoke-virtual". The
1309 // vtable from the superclass is copied in, and virtual methods from
1310 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001311 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001312
Brian Carlstrom30b94452011-08-25 21:35:26 -07001313 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001314 // this class. That means one entry for each interface we support
1315 // directly, indirectly via superclass, or indirectly via
1316 // superinterface. This will be null if neither we nor our
1317 // superclass implement any interfaces.
1318 //
1319 // Why we need this: given "class Foo implements Face", declare
1320 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1321 // is part of the Face interface. We can't easily use a single
1322 // vtable.
1323 //
1324 // For every interface a concrete class implements, we create a list
1325 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001326 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001327 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001328 InterfaceEntry* iftable_;
1329
1330 // The interface vtable indices for iftable get stored here. By
1331 // placing them all in a single pool for each class that implements
1332 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001333 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001334 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001335 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001336
1337 // instance fields
1338 //
1339 // These describe the layout of the contents of a
1340 // DataObject-compatible Object. Note that only the fields directly
1341 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001342 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001343 //
1344 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001345 // the beginning of the field list. num_reference_instance_fields_
1346 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001347 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001348
Brian Carlstrom4873d462011-08-21 15:23:39 -07001349 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001350 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001351
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001352 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001353 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001354
1355 // source file name, if known. Otherwise, NULL.
1356 const char* source_file_;
1357
Jesse Wilson7833bd22011-08-09 18:31:44 -04001358 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001359 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001360
Brian Carlstrom4873d462011-08-21 15:23:39 -07001361 // number of static fields that are object refs
1362 size_t num_reference_static_fields_;
1363
1364 // Bitmap of offsets of sfields.
1365 uint32_t reference_static_offsets_;
1366
1367 // Total class size; used when allocating storage on gc heap.
1368 size_t class_size_;
1369
1370 // Location of first static field.
1371 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001372
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001373 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001374 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001375};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001376std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001377
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001378inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001379 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001380 DCHECK(klass_ != NULL);
1381 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001382}
1383
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001384inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001385 Class* java_lang_Class = klass_->klass_;
1386 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001387}
1388
Brian Carlstrom4873d462011-08-21 15:23:39 -07001389inline bool Object::IsClassClass() const {
1390 Class* java_lang_Class = klass_->klass_;
1391 return this == java_lang_Class;
1392}
1393
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001394inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001395 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001396}
1397
Brian Carlstromb63ec392011-08-27 17:38:27 -07001398inline bool Object::IsArrayInstance() const {
1399 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001400}
1401
Brian Carlstroma663ea52011-08-19 23:33:41 -07001402inline bool Object::IsField() const {
1403 Class* java_lang_Class = klass_->klass_;
1404 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1405 return klass_ == java_lang_reflect_Field;
1406}
1407
1408inline bool Object::IsMethod() const {
1409 Class* java_lang_Class = klass_->klass_;
1410 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1411 return klass_ == java_lang_reflect_Method;
1412}
1413
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001414inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001415 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001416 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001417 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001418 if (IsClass()) {
1419 return AsClass()->SizeOf();
1420 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001421 return klass_->object_size_;
1422}
1423
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001424inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001425 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001426}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001427
Brian Carlstrom4873d462011-08-21 15:23:39 -07001428class ClassClass : public Class {
1429 private:
1430 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1431 int32_t padding_;
1432 int64_t serialVersionUID_;
1433 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1434};
1435
1436class StringClass : public Class {
1437 private:
1438 CharArray* ASCII_;
1439 Object* CASE_INSENSITIVE_ORDER_;
1440 uint32_t REPLACEMENT_CHAR_;
1441 int64_t serialVersionUID;
1442 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1443};
1444
1445class FieldClass : public Class {
1446 private:
1447 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1448 uint32_t TYPE_BOOLEAN_;
1449 uint32_t TYPE_BYTE_;
1450 uint32_t TYPE_CHAR_;
1451 uint32_t TYPE_DOUBLE_;
1452 uint32_t TYPE_FLOAT_;
1453 uint32_t TYPE_INTEGER_;
1454 uint32_t TYPE_LONG_;
1455 uint32_t TYPE_SHORT_;
1456 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1457};
1458
1459class MethodClass : public Class {
1460 private:
1461 int32_t DECLARED_;
1462 int32_t PUBLIC_;
1463 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1464};
1465
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001466class DataObject : public Object {
1467 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001468 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001469 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001470 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001471 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472};
1473
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001474template<class T>
1475class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001476 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001477 typedef T ElementType;
1478
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001479 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001480
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001481 const T* GetData() const {
1482 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001483 }
1484
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001485 T* GetData() {
1486 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001487 }
1488
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001489 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001490 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001491 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001492 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001493 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001494 }
1495
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001496 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001497 // TODO: ArrayStoreException
1498 if (IsValidIndex(i)) {
1499 GetData()[i] = value;
1500 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001501 }
1502
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001503 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001504 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001505 CHECK(array_class != NULL);
1506 array_class_ = array_class;
1507 }
1508
Brian Carlstroma663ea52011-08-19 23:33:41 -07001509 static void ResetArrayClass() {
1510 CHECK(array_class_ != NULL);
1511 array_class_ = NULL;
1512 }
1513
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001514 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001515 // Location of first element.
1516 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001517
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001518 static Class* array_class_;
1519
Carl Shapirof88c9522011-08-06 15:47:38 -07001520 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001521};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001522
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001524 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001525 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001526 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001527 return array_;
1528 }
1529
Carl Shapirof88c9522011-08-06 15:47:38 -07001530 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001531 return hash_code_;
1532 }
1533
Elliott Hughes814e4032011-08-23 12:07:56 -07001534 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001535 return offset_;
1536 }
1537
Elliott Hughes814e4032011-08-23 12:07:56 -07001538 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001539 return count_;
1540 }
1541
Elliott Hughes814e4032011-08-23 12:07:56 -07001542 int32_t GetUtfLength() const {
1543 return CountUtf8Bytes(array_->GetData(), count_);
1544 }
1545
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001546 // TODO: do we need this? Equals is the only caller, and could
1547 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001548 uint16_t CharAt(int32_t index) const {
1549 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001550 Thread* self = Thread::Current();
1551 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1552 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001553 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001554 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001555 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001556 }
1557
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001558 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001559 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001560 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001561 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001562 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001563 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001564 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001565 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001566 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001567 if (hash_code != 0) {
1568 string->hash_code_ = hash_code;
1569 } else {
1570 string->ComputeHashCode();
1571 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001572 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001573 }
1574
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001575 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001576 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001577 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001578 }
1579
Jesse Wilson8989d992011-08-02 13:39:42 -07001580 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1581 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001582 String* string = Alloc(GetJavaLangString(), utf16_length);
1583 uint16_t* utf16_data_out = string->array_->GetData();
1584 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1585 string->ComputeHashCode();
1586 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001587 }
1588
Brian Carlstroma663ea52011-08-19 23:33:41 -07001589 static void SetClass(Class* java_lang_String);
1590 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001591
Elliott Hughes814e4032011-08-23 12:07:56 -07001592 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001593 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1594 }
1595
Elliott Hughes814e4032011-08-23 12:07:56 -07001596 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001597 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001598 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001599 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001600 return string;
1601 }
1602
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001603 void ComputeHashCode() {
1604 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1605 }
1606
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001607 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001608 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001609 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001610 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1611 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001612 return false;
1613 }
1614 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001615 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001616 }
1617
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001618 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001619 bool Equals(const StringPiece& modified_utf8) const {
1620 // TODO: do not assume C-string representation.
1621 return Equals(modified_utf8.data());
1622 }
1623
1624 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001625 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001626 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001627 return false;
1628 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001629 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001630 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001631 return false;
1632 }
1633 }
1634 return true;
1635 }
1636
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001637 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001638 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001639 if (this->GetLength() != that_length) {
1640 return false;
1641 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001642 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001643 if (this->CharAt(i) != that_chars[that_offset + i]) {
1644 return false;
1645 }
1646 }
1647 return true;
1648 }
1649
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001650 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1651 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001652 uint16_t* chars = array_->GetData() + offset_;
1653 size_t byte_count(CountUtf8Bytes(chars, count_));
1654 std::string result(byte_count, char(0));
1655 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001656 return result;
1657 }
1658
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001659 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001660 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1661 CharArray* array_;
1662
Carl Shapirof88c9522011-08-06 15:47:38 -07001663 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001664
Elliott Hughes289da822011-08-16 10:11:20 -07001665 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001666
Elliott Hughes289da822011-08-16 10:11:20 -07001667 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001668
1669 static Class* GetJavaLangString() {
1670 DCHECK(java_lang_String_ != NULL);
1671 return java_lang_String_;
1672 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001673
1674 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001675
1676 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001677};
1678
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001679class Throwable : public Object {
1680 private:
1681 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1682 Throwable* cause_;
1683 String* detail_message_;
1684 Object* stack_state_; // Note this is Java volatile:
1685 Object* stack_trace_;
1686 Object* suppressed_exceptions_;
1687
1688 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1689};
1690
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001691class StackTraceElement : public Object {
1692 public:
1693 const String* GetDeclaringClass() const {
1694 return declaring_class_;
1695 }
1696
1697 const String* GetMethodName() const {
1698 return method_name_;
1699 }
1700
1701 const String* GetFileName() const {
1702 return file_name_;
1703 }
1704
1705 uint32_t GetLineNumber() const {
1706 return line_number_;
1707 }
1708
1709 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
1710 const String* file_name, uint32_t line_number) {
1711 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1712 trace->declaring_class_ = declaring_class;
1713 trace->method_name_ = method_name;
1714 trace->file_name_ = file_name;
1715 trace->line_number_ = line_number;
1716 return trace;
1717 }
1718
1719 static void SetClass(Class* java_lang_StackTraceElement);
1720
1721 static void ResetClass();
1722
1723 private:
1724 const String* declaring_class_;
1725 const String* method_name_;
1726 const String* file_name_;
1727 uint32_t line_number_;
1728
1729 static Class* GetStackTraceElement() {
1730 DCHECK(java_lang_StackTraceElement_ != NULL);
1731 return java_lang_StackTraceElement_;
1732 }
1733
1734 static Class* java_lang_StackTraceElement_;
1735 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1736};
1737
Brian Carlstroma663ea52011-08-19 23:33:41 -07001738inline bool Object::IsString() const {
1739 // TODO use "klass_ == String::GetJavaLangString()" instead?
1740 return klass_ == klass_->descriptor_->klass_;
1741}
1742
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001743inline size_t Class::GetTypeSize(String* descriptor) {
1744 switch (descriptor->CharAt(0)) {
1745 case 'B': return 1; // byte
1746 case 'C': return 2; // char
1747 case 'D': return 8; // double
1748 case 'F': return 4; // float
1749 case 'I': return 4; // int
1750 case 'J': return 8; // long
1751 case 'S': return 2; // short
1752 case 'Z': return 1; // boolean
1753 case 'L': return sizeof(Object*);
1754 case '[': return sizeof(Array*);
1755 default:
1756 LOG(ERROR) << "Unknown type " << descriptor;
1757 return 0;
1758 }
1759}
1760
Brian Carlstromb63ec392011-08-27 17:38:27 -07001761inline bool Class::IsArrayClass() const {
1762 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001763}
1764
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001765class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001766 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001767 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001768 }
1769
Brian Carlstrom30b94452011-08-25 21:35:26 -07001770 Class* GetInterface() const {
1771 DCHECK(interface_ != NULL);
1772 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001773 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001774
Brian Carlstrom30b94452011-08-25 21:35:26 -07001775 void SetInterface(Class* interface) {
1776 DCHECK(interface->IsInterface());
1777 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001778 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001779
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001780 private:
1781 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001782 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001783
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001784 public: // TODO: private
1785 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001786 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001787 // this class.
1788 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001789
1790 private:
1791 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001792};
1793
1794} // namespace art
1795
1796#endif // ART_SRC_OBJECT_H_