blob: 51bf9992e33e819804ccc663e5754a432ee717dc [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 Carlstrom9cc262e2011-08-28 12:45:30 -070021class CodeAndDirectMethods;
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
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700545 bool IsDirect() const {
546 return is_direct_;
547 }
548
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700549 // "Args" may refer to any of the 3 levels of "Args."
550 // To avoid confusion, our code will denote which "Args" clearly:
551 // 1. UserArgs: Args that a user see.
552 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
553 // receiver.
554 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
555 // E.g., the first in Args is Method* for both static and non-static
556 // methods. And CConvArgs doesn't deal with the receiver because
557 // receiver is hardwired in an implicit register, so CConvArgs doesn't
558 // need to deal with it.
559 //
560 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700561 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700562 // "1 +" because the first in Args is the receiver.
563 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700564 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
565 }
566
567 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700568 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700569 size_t NumReferenceArgs() const;
570
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700571 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700572 size_t NumLongOrDoubleArgs() const;
573
574 // The number of reference arguments to this method before the given
575 // parameter index
576 size_t NumReferenceArgsBefore(unsigned int param) const;
577
578 // Is the given method parameter a reference?
579 bool IsParamAReference(unsigned int param) const;
580
581 // Is the given method parameter a long or double?
582 bool IsParamALongOrDouble(unsigned int param) const;
583
Ian Rogersdf20fe02011-07-20 20:34:16 -0700584 // Size in bytes of the given parameter
585 size_t ParamSize(unsigned int param) const;
586
587 // Size in bytes of the return value
588 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700589
buzbeec143c552011-08-20 17:38:58 -0700590 bool HasCode() {
591 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700592 }
593
Brian Carlstrom83db7722011-08-26 17:32:56 -0700594 const void* GetCode() {
595 return code_;
596 }
597
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700598 void SetCode(const byte* compiled_code,
599 size_t byte_count,
600 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700601 // Copy the code into an executable region.
602 code_instruction_set_ = set;
603 code_area_.reset(MemMap::Map(byte_count,
604 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700605 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700606 byte* code = code_area_->GetAddress();
607 memcpy(code, compiled_code, byte_count);
608 __builtin___clear_cache(code, code + byte_count);
609
610 uintptr_t address = reinterpret_cast<uintptr_t>(code);
611 if (code_instruction_set_ == kThumb2) {
612 // Set the low-order bit so a BLX will switch to Thumb mode
613 address |= 0x1;
614 }
615 code_ = reinterpret_cast<void*>(address);
616 }
617
Shih-wei Liaod11af152011-08-23 16:02:11 -0700618 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
619 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700620 }
621
Shih-wei Liaod11af152011-08-23 16:02:11 -0700622 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
623 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700624 }
625
Shih-wei Liaod11af152011-08-23 16:02:11 -0700626 size_t GetFrameSizeInBytes() const {
627 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700628 }
629
Shih-wei Liaod11af152011-08-23 16:02:11 -0700630 size_t GetReturnPcOffsetInBytes() const {
631 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700632 }
633
buzbeec143c552011-08-20 17:38:58 -0700634 void SetCoreSpillMask(uint32_t core_spill_mask) {
635 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700636 }
637
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700638 static size_t GetCodeOffset() {
639 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700640 }
641
buzbeec143c552011-08-20 17:38:58 -0700642 void SetFpSpillMask(uint32_t fp_spill_mask) {
643 fp_spill_mask_ = fp_spill_mask;
644 }
645
Ian Rogersb033c752011-07-20 12:22:35 -0700646 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700647 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700648 native_method_ = native_method;
649 }
650
Elliott Hughes5174fe62011-08-23 15:12:35 -0700651 void UnregisterNative() {
652 native_method_ = NULL;
653 }
654
Ian Rogersb033c752011-07-20 12:22:35 -0700655 static MemberOffset NativeMethodOffset() {
656 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
657 }
658
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700659 InvokeStub* GetInvokeStub() const {
660 return invoke_stub_;
661 }
662
663 void SetInvokeStub(const InvokeStub* invoke_stub) {
664 invoke_stub_ = invoke_stub;
665 }
666
667 static size_t GetInvokeStubOffset() {
668 return OFFSETOF_MEMBER(Method, invoke_stub_);
669 }
670
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700671 bool HasSameNameAndDescriptor(const Method* that) const;
672
Ian Rogersb033c752011-07-20 12:22:35 -0700673 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700674 // access flags; low 16 bits are defined by spec (could be uint16_t?)
675 uint32_t access_flags_;
676
677 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700678 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700679 //
680 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700681 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700682 uint16_t method_index_;
683
684 // Method bounds; not needed for an abstract method.
685 //
686 // For a native method, we compute the size of the argument list, and
687 // set "insSize" and "registerSize" equal to it.
688 uint16_t num_registers_; // ins + locals
689 uint16_t num_outs_;
690 uint16_t num_ins_;
691
buzbeec143c552011-08-20 17:38:58 -0700692 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700693 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700694
695 // Architecture-dependent register spill masks
696 uint32_t core_spill_mask_;
697 uint32_t fp_spill_mask_;
698
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700699 // The method descriptor. This represents the parameters a method
700 // takes and value it returns. This string is a list of the type
701 // descriptors for the parameters enclosed in parenthesis followed
702 // by the return type descriptor. For example, for the method
703 //
704 // Object mymethod(int i, double d, Thread t)
705 //
706 // the method descriptor would be
707 //
708 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700709 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700710
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700711 // Method prototype descriptor string (return and argument types).
712 uint32_t proto_idx_;
713
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700714 // Offset to the CodeItem.
715 uint32_t code_off_;
716
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700717 // The short-form method descriptor string.
718 StringPiece shorty_;
719
Ian Rogers762400c2011-08-23 12:14:16 -0700720 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
721 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700722 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700723 ObjectArray<Class>* dex_cache_types_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700724 ObjectArray<Method>* dex_cache_methods_;
725 ObjectArray<Field>* dex_cache_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700726 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
727
728 bool is_direct_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700729
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700730 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700731 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700732 scoped_ptr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700733 const void* code_;
734 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700735 InstructionSet code_instruction_set_;
736
737 // Size in bytes of compiled code associated with this method
738 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700739
Ian Rogers762400c2011-08-23 12:14:16 -0700740 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700741 // Offset of PC within compiled code (in bytes)
742 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700743
Ian Rogersb033c752011-07-20 12:22:35 -0700744 // Any native method registered with this method
745 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700746
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700747 // Native invocation stub entry point.
748 const InvokeStub* invoke_stub_;
749
Carl Shapirof88c9522011-08-06 15:47:38 -0700750 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700751};
752
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700753class Array : public Object {
754 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700755 static size_t SizeOf(size_t component_count,
756 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700757 return sizeof(Array) + component_count * component_size;
758 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700759
Brian Carlstromb63ec392011-08-27 17:38:27 -0700760 // Given the context of a calling Method, use its DexCache to
761 // resolve a type to an array Class. If it cannot be resolved, throw
762 // an error. If it can, use it to create an array.
763 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
764
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700765 // A convenience for code that doesn't know the component size,
766 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700767 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700768
Brian Carlstromb63ec392011-08-27 17:38:27 -0700769 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700770
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700771 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700772
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700773 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700774 return length_;
775 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700776
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700777 void SetLength(uint32_t length) {
778 length_ = length;
779 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700780
buzbeec143c552011-08-20 17:38:58 -0700781 static MemberOffset LengthOffset() {
782 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
783 }
784
785 static MemberOffset DataOffset() {
786 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
787 }
788
Elliott Hughes289da822011-08-16 10:11:20 -0700789 protected:
790 bool IsValidIndex(int32_t index) const {
791 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700792 Thread* self = Thread::Current();
793 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
794 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700795 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700796 }
797 return true;
798 }
799
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700800 private:
801 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700802 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400803 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
804 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700805 // Marker for the data (used by generated code)
806 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700807
Carl Shapirof88c9522011-08-06 15:47:38 -0700808 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700809};
810
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700811template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700812class ObjectArray : public Array {
813 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700814 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700815 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700816 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700817 }
818
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700819 T* const * GetData() const {
820 return reinterpret_cast<T* const *>(&elements_);
821 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400822
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700823 T** GetData() {
824 return reinterpret_cast<T**>(&elements_);
825 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400826
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700827 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700828 if (!IsValidIndex(i)) {
829 return NULL;
830 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700831 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700832 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700833
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700834 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700835 if (IsValidIndex(i)) {
836 // TODO: ArrayStoreException
837 GetData()[i] = object; // TODO: write barrier
838 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700839 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700840
841 static void Copy(ObjectArray<T>* src, int src_pos,
842 ObjectArray<T>* dst, int dst_pos,
843 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700844 for (size_t i = 0; i < length; i++) {
845 dst->Set(dst_pos + i, src->Get(src_pos + i));
846 }
847 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700848
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700849 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700850 ObjectArray<T>* new_array = Alloc(klass_, new_length);
851 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
852 return new_array;
853 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700854
855 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700856 // Location of first element.
857 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700858
859 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700860};
861
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700862// ClassLoader objects.
863class ClassLoader : public Object {
864 public:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700865 const std::vector<const DexFile*>& GetClassPath() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700866 return class_path_;
867 }
868 void SetClassPath(std::vector<const DexFile*>& class_path) {
869 DCHECK_EQ(0U, class_path_.size());
870 class_path_ = class_path;
871 }
872
873 private:
874 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
875 Object* packages_;
876 ClassLoader* parent_;
877
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700878 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700879 std::vector<const DexFile*> class_path_;
880
Carl Shapirof88c9522011-08-06 15:47:38 -0700881 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700882};
883
884class BaseDexClassLoader : public ClassLoader {
885 private:
886 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
887 String* original_path_;
888 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700889 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700890};
891
892class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700893 public:
894 static PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
895 static void SetClass(Class* dalvik_system_PathClassLoader);
896 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700897 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700898 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700899 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700900};
901
Carl Shapiro1fb86202011-06-27 17:43:13 -0700902// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700903class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700904 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700905
906 // Class Status
907 //
908 // kStatusNotReady: If a Class cannot be found in the class table by
909 // FindClass, it allocates an new one with AllocClass in the
910 // kStatusNotReady and calls LoadClass. Note if it does find a
911 // class, it may not be kStatusResolved and it will try to push it
912 // forward toward kStatusResolved.
913 //
914 // kStatusIdx: LoadClass populates with Class with information from
915 // the DexFile, moving the status to kStatusIdx, indicating that the
916 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700917 // populated based on super_class_type_idx_ and
918 // interfaces_type_idx_. The new Class can then be inserted into the
919 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700920 //
921 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
922 // attempt to move a kStatusIdx class forward to kStatusLoaded by
923 // using ResolveClass to initialize the super_class_ and interfaces_.
924 //
925 // kStatusResolved: Still holding the lock on Class, the ClassLinker
926 // will use LinkClass to link all members, creating Field and Method
927 // objects, setting up the vtable, etc. On success, the class is
928 // marked kStatusResolved.
929
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700931 kStatusError = -1,
932 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700933 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700934 kStatusLoaded = 2, // DEX idx values resolved
935 kStatusResolved = 3, // part of linking
936 kStatusVerifying = 4, // in the process of being verified
937 kStatusVerified = 5, // logically part of linking; done pre-init
938 kStatusInitializing = 6, // class init in progress
939 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700940 };
941
942 enum PrimitiveType {
943 kPrimNot = -1
944 };
945
Brian Carlstromb63ec392011-08-27 17:38:27 -0700946 // Given the context of a calling Method, use its DexCache to
947 // resolve a type to a Class. If it cannot be resolved, throw an
948 // error. If it can, use it to create an instance.
949 static Object* NewInstanceFromCode(uint32_t type_idx, Method* method);
950
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700951 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700952 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700953 return Heap::AllocObject(this, this->object_size_);
954 }
955
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700956 Class* GetSuperClass() const {
957 return super_class_;
958 }
959
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700960 uint32_t GetSuperClassTypeIdx() const {
961 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700962 }
963
964 bool HasSuperClass() const {
965 return super_class_ != NULL;
966 }
967
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700968 bool IsAssignableFrom(const Class* klass) const {
969 DCHECK(klass != NULL);
970 if (this == klass) {
971 return true;
972 }
973 if (IsInterface()) {
974 return klass->Implements(this);
975 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700976 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700977 return IsAssignableFromArray(klass);
978 }
979 return klass->IsSubClass(this);
980 }
981
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700982 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700983 return class_loader_;
984 }
985
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700986 DexCache* GetDexCache() const {
987 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988 }
989
990 Class* GetComponentType() const {
991 return component_type_;
992 }
993
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700994 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700995
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700996 size_t GetComponentSize() const {
997 return GetTypeSize(component_type_->descriptor_);
998 }
999
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001000 const String* GetDescriptor() const {
1001 DCHECK(descriptor_ != NULL);
1002 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 return descriptor_;
1004 }
1005
Brian Carlstrom4873d462011-08-21 15:23:39 -07001006 size_t SizeOf() const {
1007 return class_size_;
1008 }
1009
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001010 Status GetStatus() const {
1011 return status_;
1012 }
1013
1014 void SetStatus(Status new_status) {
1015 // TODO: validate transition
1016 status_ = new_status;
1017 }
1018
Carl Shapiro69759ea2011-07-21 18:13:35 -07001019 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001020 bool IsErroneous() const {
1021 return GetStatus() == kStatusError;
1022 }
1023
Carl Shapiro69759ea2011-07-21 18:13:35 -07001024 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001025 bool IsVerified() const {
1026 return GetStatus() >= kStatusVerified;
1027 }
1028
Carl Shapiro69759ea2011-07-21 18:13:35 -07001029 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001030 bool IsLinked() const {
1031 return GetStatus() >= kStatusResolved;
1032 }
1033
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001034 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001035 bool IsLoaded() const {
1036 return GetStatus() >= kStatusLoaded;
1037 }
1038
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001039 // Returns true if the class is initialized.
1040 bool IsInitialized() const {
1041 return GetStatus() == kStatusInitialized;
1042 }
1043
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001044 // Returns true if this class is in the same packages as that class.
1045 bool IsInSamePackage(const Class* that) const;
1046
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001047 static bool IsInSamePackage(const String* descriptor1,
1048 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001049
1050 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001051 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001052
1053 // Returns true if the class is an interface.
1054 bool IsInterface() const {
1055 return (access_flags_ & kAccInterface) != 0;
1056 }
1057
1058 // Returns true if the class is declared public.
1059 bool IsPublic() const {
1060 return (access_flags_ & kAccPublic) != 0;
1061 }
1062
1063 // Returns true if the class is declared final.
1064 bool IsFinal() const {
1065 return (access_flags_ & kAccFinal) != 0;
1066 }
1067
1068 // Returns true if the class is abstract.
1069 bool IsAbstract() const {
1070 return (access_flags_ & kAccAbstract) != 0;
1071 }
1072
1073 // Returns true if the class is an annotation.
1074 bool IsAnnotation() const {
1075 return (access_flags_ & kAccAnnotation) != 0;
1076 }
1077
1078 // Returns true if the class is a primitive type.
1079 bool IsPrimitive() const {
1080 return primitive_type_ != kPrimNot;
1081 }
1082
Brian Carlstromae3ac012011-07-27 01:30:28 -07001083 // Returns true if the class is synthetic.
1084 bool IsSynthetic() const {
1085 return (access_flags_ & kAccSynthetic) != 0;
1086 }
1087
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001088 // Returns true if this class can access that class.
1089 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001090 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001091 }
1092
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001093 // Returns the number of static, private, and constructor methods.
1094 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001095 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001096 }
1097
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001098 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001099 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001100 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001101 }
1102
1103 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001104 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001105 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001106 }
1107
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001108 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001109 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001110
1111 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001112 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001113
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 // Returns the number of non-inherited virtual methods.
1115 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001116 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001117 }
1118
1119 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001120 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001121 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001122 }
1123
1124 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001125 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001126 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001127 }
1128
Brian Carlstrom30b94452011-08-25 21:35:26 -07001129 // Given a method implemented by this class but potentially from a
1130 // super class, return the specific implementation
1131 // method for this class.
1132 Method* FindVirtualMethodForVirtual(Method* method) {
1133 DCHECK(!method->GetDeclaringClass()->IsInterface());
1134 // The argument method may from a super class.
1135 // Use the index to a potentially overriden one for this instance's class.
1136 return vtable_->Get(method->method_index_);
1137 }
1138
1139 // Given a method implemented by this class, but potentially from a
1140 // super class or interface, return the specific implementation
1141 // method for this class.
1142 Method* FindVirtualMethodForInterface(Method* method);
1143
1144 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1145 if (method->GetDeclaringClass()->IsInterface()) {
1146 return FindVirtualMethodForInterface(method);
1147 }
1148 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001149 }
1150
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001151 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1152 const StringPiece& descriptor);
1153
1154 Method* FindVirtualMethod(const StringPiece& name,
1155 const StringPiece& descriptor);
1156
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001158 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 }
1160
Carl Shapiro69759ea2011-07-21 18:13:35 -07001161 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001163 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001164 }
1165
Brian Carlstrom4873d462011-08-21 15:23:39 -07001166 // Returns the number of static fields containing reference types.
1167 size_t NumReferenceStaticFields() const {
1168 return num_reference_static_fields_;
1169 }
1170
Elliott Hughescdf53122011-08-19 15:46:09 -07001171 // Finds the given instance field in this class or a superclass.
1172 Field* FindInstanceField(const StringPiece& name,
1173 const StringPiece& descriptor);
1174
1175 Field* FindDeclaredInstanceField(const StringPiece& name,
1176 const StringPiece& descriptor);
1177
1178 // Finds the given static field in this class or a superclass.
1179 Field* FindStaticField(const StringPiece& name,
1180 const StringPiece& descriptor);
1181
1182 Field* FindDeclaredStaticField(const StringPiece& name,
1183 const StringPiece& descriptor);
1184
Jesse Wilson35baaab2011-08-10 16:18:03 -04001185 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001186 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001187 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001188 }
1189
Jesse Wilson35baaab2011-08-10 16:18:03 -04001190 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001191 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001192 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001193 }
1194
1195 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001196 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001197 }
1198
Jesse Wilson35baaab2011-08-10 16:18:03 -04001199 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001200 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001201 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001202 }
1203
Jesse Wilson35baaab2011-08-10 16:18:03 -04001204 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001205 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001206 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207 }
1208
Brian Carlstrom4873d462011-08-21 15:23:39 -07001209 uint32_t GetReferenceInstanceOffsets() const {
1210 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001211 }
1212
Brian Carlstrom4873d462011-08-21 15:23:39 -07001213 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1214 reference_instance_offsets_ = new_reference_offsets;
1215 }
1216
1217 uint32_t GetReferenceStaticOffsets() const {
1218 return reference_static_offsets_;
1219 }
1220
1221 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1222 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 }
1224
Carl Shapiro69759ea2011-07-21 18:13:35 -07001225 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001226 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001227 }
1228
1229 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001230 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001231 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001232 }
1233
1234 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001235 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001236 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001237 }
1238
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001239 void SetVerifyErrorClass(Class* klass) {
1240 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1241 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1242 klass->SetFieldObject(field_offset, klass);
1243 }
1244
1245 private:
1246 bool Implements(const Class* klass) const;
1247 bool IsArrayAssignableFromArray(const Class* klass) const;
1248 bool IsAssignableFromArray(const Class* klass) const;
1249 bool IsSubClass(const Class* klass) const;
1250
Ian Rogersb033c752011-07-20 12:22:35 -07001251 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001252 // descriptor for the class such as "java.lang.Class" or "[C"
1253 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001254
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001255 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1256 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001257
1258 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001259 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001260
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001261 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001262 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001263 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001264
1265 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001266 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001267
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001268 // If class verify fails, we must return same error on subsequent tries.
1269 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1270 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001271
1272 // threadId, used to check for recursive <clinit> invocation
1273 uint32_t clinit_thread_id_;
1274
1275 // Total object size; used when allocating storage on gc heap. (For
1276 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001277 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001278
1279 // For array classes, the class object for base element, for
1280 // instanceof/checkcast (for String[][][], this will be String).
1281 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001283
1284 // For array classes, the number of array dimensions, e.g. int[][]
1285 // is 2. Otherwise 0.
1286 int32_t array_rank_;
1287
1288 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1289 PrimitiveType primitive_type_;
1290
1291 // The superclass, or NULL if this is java.lang.Object or a
1292 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001293 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001294 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001295
1296 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001297 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001298
1299 // initiating class loader list
1300 // NOTE: for classes with low serialNumber, these are unused, and the
1301 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001302 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001303
1304 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001305 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001306 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001307
1308 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001309 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001310
1311 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001312 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001313
1314 // Virtual method table (vtable), for use by "invoke-virtual". The
1315 // vtable from the superclass is copied in, and virtual methods from
1316 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001317 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001318
Brian Carlstrom30b94452011-08-25 21:35:26 -07001319 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001320 // this class. That means one entry for each interface we support
1321 // directly, indirectly via superclass, or indirectly via
1322 // superinterface. This will be null if neither we nor our
1323 // superclass implement any interfaces.
1324 //
1325 // Why we need this: given "class Foo implements Face", declare
1326 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1327 // is part of the Face interface. We can't easily use a single
1328 // vtable.
1329 //
1330 // For every interface a concrete class implements, we create a list
1331 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001332 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001333 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001334 InterfaceEntry* iftable_;
1335
1336 // The interface vtable indices for iftable get stored here. By
1337 // placing them all in a single pool for each class that implements
1338 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001340 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001341 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001342
1343 // instance fields
1344 //
1345 // These describe the layout of the contents of a
1346 // DataObject-compatible Object. Note that only the fields directly
1347 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001348 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001349 //
1350 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001351 // the beginning of the field list. num_reference_instance_fields_
1352 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001353 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001354
Brian Carlstrom4873d462011-08-21 15:23:39 -07001355 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001356 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001357
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001359 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001360
1361 // source file name, if known. Otherwise, NULL.
1362 const char* source_file_;
1363
Jesse Wilson7833bd22011-08-09 18:31:44 -04001364 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001365 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001366
Brian Carlstrom4873d462011-08-21 15:23:39 -07001367 // number of static fields that are object refs
1368 size_t num_reference_static_fields_;
1369
1370 // Bitmap of offsets of sfields.
1371 uint32_t reference_static_offsets_;
1372
1373 // Total class size; used when allocating storage on gc heap.
1374 size_t class_size_;
1375
1376 // Location of first static field.
1377 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001378
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001379 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001380 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001381};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001382std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001383
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001384inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001385 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001386 DCHECK(klass_ != NULL);
1387 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001388}
1389
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001390inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001391 Class* java_lang_Class = klass_->klass_;
1392 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001393}
1394
Brian Carlstrom4873d462011-08-21 15:23:39 -07001395inline bool Object::IsClassClass() const {
1396 Class* java_lang_Class = klass_->klass_;
1397 return this == java_lang_Class;
1398}
1399
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001400inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001401 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001402}
1403
Brian Carlstromb63ec392011-08-27 17:38:27 -07001404inline bool Object::IsArrayInstance() const {
1405 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001406}
1407
Brian Carlstroma663ea52011-08-19 23:33:41 -07001408inline bool Object::IsField() const {
1409 Class* java_lang_Class = klass_->klass_;
1410 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1411 return klass_ == java_lang_reflect_Field;
1412}
1413
1414inline bool Object::IsMethod() const {
1415 Class* java_lang_Class = klass_->klass_;
1416 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1417 return klass_ == java_lang_reflect_Method;
1418}
1419
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001420inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001421 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001422 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001423 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001424 if (IsClass()) {
1425 return AsClass()->SizeOf();
1426 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001427 return klass_->object_size_;
1428}
1429
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001430inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001431 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001432}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001433
Brian Carlstrom4873d462011-08-21 15:23:39 -07001434class ClassClass : public Class {
1435 private:
1436 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1437 int32_t padding_;
1438 int64_t serialVersionUID_;
1439 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1440};
1441
1442class StringClass : public Class {
1443 private:
1444 CharArray* ASCII_;
1445 Object* CASE_INSENSITIVE_ORDER_;
1446 uint32_t REPLACEMENT_CHAR_;
1447 int64_t serialVersionUID;
1448 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1449};
1450
1451class FieldClass : public Class {
1452 private:
1453 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1454 uint32_t TYPE_BOOLEAN_;
1455 uint32_t TYPE_BYTE_;
1456 uint32_t TYPE_CHAR_;
1457 uint32_t TYPE_DOUBLE_;
1458 uint32_t TYPE_FLOAT_;
1459 uint32_t TYPE_INTEGER_;
1460 uint32_t TYPE_LONG_;
1461 uint32_t TYPE_SHORT_;
1462 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1463};
1464
1465class MethodClass : public Class {
1466 private:
1467 int32_t DECLARED_;
1468 int32_t PUBLIC_;
1469 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1470};
1471
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001472class DataObject : public Object {
1473 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001474 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001475 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001476 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001477 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001478};
1479
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001480template<class T>
1481class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001482 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001483 typedef T ElementType;
1484
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001485 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001486
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001487 const T* GetData() const {
1488 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001489 }
1490
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001491 T* GetData() {
1492 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001493 }
1494
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001495 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001496 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001497 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001498 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001499 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001500 }
1501
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001502 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001503 // TODO: ArrayStoreException
1504 if (IsValidIndex(i)) {
1505 GetData()[i] = value;
1506 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001507 }
1508
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001509 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001510 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001511 CHECK(array_class != NULL);
1512 array_class_ = array_class;
1513 }
1514
Brian Carlstroma663ea52011-08-19 23:33:41 -07001515 static void ResetArrayClass() {
1516 CHECK(array_class_ != NULL);
1517 array_class_ = NULL;
1518 }
1519
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001520 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001521 // Location of first element.
1522 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001523
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001524 static Class* array_class_;
1525
Carl Shapirof88c9522011-08-06 15:47:38 -07001526 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001527};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001528
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001529class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001530 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001531 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001532 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001533 return array_;
1534 }
1535
Carl Shapirof88c9522011-08-06 15:47:38 -07001536 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001537 return hash_code_;
1538 }
1539
Elliott Hughes814e4032011-08-23 12:07:56 -07001540 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001541 return offset_;
1542 }
1543
Elliott Hughes814e4032011-08-23 12:07:56 -07001544 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001545 return count_;
1546 }
1547
Elliott Hughes814e4032011-08-23 12:07:56 -07001548 int32_t GetUtfLength() const {
1549 return CountUtf8Bytes(array_->GetData(), count_);
1550 }
1551
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001552 // TODO: do we need this? Equals is the only caller, and could
1553 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001554 uint16_t CharAt(int32_t index) const {
1555 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001556 Thread* self = Thread::Current();
1557 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1558 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001559 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001560 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001561 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001562 }
1563
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001564 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001565 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001566 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001567 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001568 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001569 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001570 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001571 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001572 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001573 if (hash_code != 0) {
1574 string->hash_code_ = hash_code;
1575 } else {
1576 string->ComputeHashCode();
1577 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001578 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001579 }
1580
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001581 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001582 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001583 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001584 }
1585
Jesse Wilson8989d992011-08-02 13:39:42 -07001586 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1587 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001588 String* string = Alloc(GetJavaLangString(), utf16_length);
1589 uint16_t* utf16_data_out = string->array_->GetData();
1590 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1591 string->ComputeHashCode();
1592 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001593 }
1594
Brian Carlstroma663ea52011-08-19 23:33:41 -07001595 static void SetClass(Class* java_lang_String);
1596 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001597
Elliott Hughes814e4032011-08-23 12:07:56 -07001598 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001599 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1600 }
1601
Elliott Hughes814e4032011-08-23 12:07:56 -07001602 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001603 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001604 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001605 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001606 return string;
1607 }
1608
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001609 void ComputeHashCode() {
1610 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1611 }
1612
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001613 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001614 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001615 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001616 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1617 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001618 return false;
1619 }
1620 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001621 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001622 }
1623
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001624 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001625 bool Equals(const StringPiece& modified_utf8) const {
1626 // TODO: do not assume C-string representation.
1627 return Equals(modified_utf8.data());
1628 }
1629
1630 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001631 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001632 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001633 return false;
1634 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001635 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001636 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001637 return false;
1638 }
1639 }
1640 return true;
1641 }
1642
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001643 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001644 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001645 if (this->GetLength() != that_length) {
1646 return false;
1647 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001648 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001649 if (this->CharAt(i) != that_chars[that_offset + i]) {
1650 return false;
1651 }
1652 }
1653 return true;
1654 }
1655
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001656 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1657 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001658 uint16_t* chars = array_->GetData() + offset_;
1659 size_t byte_count(CountUtf8Bytes(chars, count_));
1660 std::string result(byte_count, char(0));
1661 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001662 return result;
1663 }
1664
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001665 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001666 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1667 CharArray* array_;
1668
Carl Shapirof88c9522011-08-06 15:47:38 -07001669 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001670
Elliott Hughes289da822011-08-16 10:11:20 -07001671 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001672
Elliott Hughes289da822011-08-16 10:11:20 -07001673 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001674
1675 static Class* GetJavaLangString() {
1676 DCHECK(java_lang_String_ != NULL);
1677 return java_lang_String_;
1678 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001679
1680 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001681
1682 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001683};
1684
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001685class Throwable : public Object {
1686 private:
1687 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1688 Throwable* cause_;
1689 String* detail_message_;
1690 Object* stack_state_; // Note this is Java volatile:
1691 Object* stack_trace_;
1692 Object* suppressed_exceptions_;
1693
1694 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1695};
1696
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001697class StackTraceElement : public Object {
1698 public:
1699 const String* GetDeclaringClass() const {
1700 return declaring_class_;
1701 }
1702
1703 const String* GetMethodName() const {
1704 return method_name_;
1705 }
1706
1707 const String* GetFileName() const {
1708 return file_name_;
1709 }
1710
1711 uint32_t GetLineNumber() const {
1712 return line_number_;
1713 }
1714
1715 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
1716 const String* file_name, uint32_t line_number) {
1717 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1718 trace->declaring_class_ = declaring_class;
1719 trace->method_name_ = method_name;
1720 trace->file_name_ = file_name;
1721 trace->line_number_ = line_number;
1722 return trace;
1723 }
1724
1725 static void SetClass(Class* java_lang_StackTraceElement);
1726
1727 static void ResetClass();
1728
1729 private:
1730 const String* declaring_class_;
1731 const String* method_name_;
1732 const String* file_name_;
1733 uint32_t line_number_;
1734
1735 static Class* GetStackTraceElement() {
1736 DCHECK(java_lang_StackTraceElement_ != NULL);
1737 return java_lang_StackTraceElement_;
1738 }
1739
1740 static Class* java_lang_StackTraceElement_;
1741 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1742};
1743
Brian Carlstroma663ea52011-08-19 23:33:41 -07001744inline bool Object::IsString() const {
1745 // TODO use "klass_ == String::GetJavaLangString()" instead?
1746 return klass_ == klass_->descriptor_->klass_;
1747}
1748
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001749inline size_t Class::GetTypeSize(String* descriptor) {
1750 switch (descriptor->CharAt(0)) {
1751 case 'B': return 1; // byte
1752 case 'C': return 2; // char
1753 case 'D': return 8; // double
1754 case 'F': return 4; // float
1755 case 'I': return 4; // int
1756 case 'J': return 8; // long
1757 case 'S': return 2; // short
1758 case 'Z': return 1; // boolean
1759 case 'L': return sizeof(Object*);
1760 case '[': return sizeof(Array*);
1761 default:
1762 LOG(ERROR) << "Unknown type " << descriptor;
1763 return 0;
1764 }
1765}
1766
Brian Carlstromb63ec392011-08-27 17:38:27 -07001767inline bool Class::IsArrayClass() const {
1768 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001769}
1770
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001771class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001772 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001773 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001774 }
1775
Brian Carlstrom30b94452011-08-25 21:35:26 -07001776 Class* GetInterface() const {
1777 DCHECK(interface_ != NULL);
1778 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001779 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001780
Brian Carlstrom30b94452011-08-25 21:35:26 -07001781 void SetInterface(Class* interface) {
1782 DCHECK(interface->IsInterface());
1783 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001784 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001785
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001786 private:
1787 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001788 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001789
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001790 public: // TODO: private
1791 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001792 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001793 // this class.
1794 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001795
1796 private:
1797 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001798};
1799
1800} // namespace art
1801
1802#endif // ART_SRC_OBJECT_H_