blob: 9db07048754effbacef8b9884746ad739fc4c38b [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 Carlstromdb4d5402011-08-09 12:18:28 -0700247 bool IsArray() const;
248
249 Array* AsArray() {
250 DCHECK(IsArray());
251 return down_cast<Array*>(this);
252 }
253
254 const Array* AsArray() const {
255 DCHECK(IsArray());
256 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
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700754 // A convenience for code that doesn't know the component size,
755 // and doesn't want to have to work it out itself.
756 static Array* Alloc(Class* array_class, size_t component_count);
757
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700758 static Array* Alloc(Class* array_class,
759 size_t component_count,
760 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700761 size_t size = SizeOf(component_count, component_size);
762 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700763 if (array != NULL) {
764 array->SetLength(component_count);
765 }
766 return array;
767 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700768
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700769 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700770
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700771 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700772 return length_;
773 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700774
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700775 void SetLength(uint32_t length) {
776 length_ = length;
777 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700778
buzbeec143c552011-08-20 17:38:58 -0700779 static MemberOffset LengthOffset() {
780 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
781 }
782
783 static MemberOffset DataOffset() {
784 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
785 }
786
Elliott Hughes289da822011-08-16 10:11:20 -0700787 protected:
788 bool IsValidIndex(int32_t index) const {
789 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700790 Thread* self = Thread::Current();
791 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
792 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700793 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700794 }
795 return true;
796 }
797
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700798 private:
799 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700800 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400801 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
802 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700803 // Marker for the data (used by generated code)
804 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700805
Carl Shapirof88c9522011-08-06 15:47:38 -0700806 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700807};
808
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700809template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700810class ObjectArray : public Array {
811 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700812 static ObjectArray<T>* Alloc(Class* object_array_class,
813 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700814 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700815 }
816
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700817 T* const * GetData() const {
818 return reinterpret_cast<T* const *>(&elements_);
819 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400820
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700821 T** GetData() {
822 return reinterpret_cast<T**>(&elements_);
823 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400824
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700825 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700826 if (!IsValidIndex(i)) {
827 return NULL;
828 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700829 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700830 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700831
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700832 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700833 if (IsValidIndex(i)) {
834 // TODO: ArrayStoreException
835 GetData()[i] = object; // TODO: write barrier
836 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700837 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700838
839 static void Copy(ObjectArray<T>* src, int src_pos,
840 ObjectArray<T>* dst, int dst_pos,
841 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700842 for (size_t i = 0; i < length; i++) {
843 dst->Set(dst_pos + i, src->Get(src_pos + i));
844 }
845 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700846
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700847 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700848 ObjectArray<T>* new_array = Alloc(klass_, new_length);
849 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
850 return new_array;
851 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700852
853 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700854 // Location of first element.
855 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700856
857 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700858};
859
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700860// ClassLoader objects.
861class ClassLoader : public Object {
862 public:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700863 const std::vector<const DexFile*>& GetClassPath() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700864 return class_path_;
865 }
866 void SetClassPath(std::vector<const DexFile*>& class_path) {
867 DCHECK_EQ(0U, class_path_.size());
868 class_path_ = class_path;
869 }
870
871 private:
872 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
873 Object* packages_;
874 ClassLoader* parent_;
875
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700876 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700877 std::vector<const DexFile*> class_path_;
878
Carl Shapirof88c9522011-08-06 15:47:38 -0700879 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700880};
881
882class BaseDexClassLoader : public ClassLoader {
883 private:
884 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
885 String* original_path_;
886 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700887 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700888};
889
890class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700891 public:
892 static PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
893 static void SetClass(Class* dalvik_system_PathClassLoader);
894 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700895 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700896 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700897 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700898};
899
Carl Shapiro1fb86202011-06-27 17:43:13 -0700900// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700901class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700902 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700903
904 // Class Status
905 //
906 // kStatusNotReady: If a Class cannot be found in the class table by
907 // FindClass, it allocates an new one with AllocClass in the
908 // kStatusNotReady and calls LoadClass. Note if it does find a
909 // class, it may not be kStatusResolved and it will try to push it
910 // forward toward kStatusResolved.
911 //
912 // kStatusIdx: LoadClass populates with Class with information from
913 // the DexFile, moving the status to kStatusIdx, indicating that the
914 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700915 // populated based on super_class_type_idx_ and
916 // interfaces_type_idx_. The new Class can then be inserted into the
917 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700918 //
919 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
920 // attempt to move a kStatusIdx class forward to kStatusLoaded by
921 // using ResolveClass to initialize the super_class_ and interfaces_.
922 //
923 // kStatusResolved: Still holding the lock on Class, the ClassLinker
924 // will use LinkClass to link all members, creating Field and Method
925 // objects, setting up the vtable, etc. On success, the class is
926 // marked kStatusResolved.
927
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700928 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700929 kStatusError = -1,
930 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700931 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700932 kStatusLoaded = 2, // DEX idx values resolved
933 kStatusResolved = 3, // part of linking
934 kStatusVerifying = 4, // in the process of being verified
935 kStatusVerified = 5, // logically part of linking; done pre-init
936 kStatusInitializing = 6, // class init in progress
937 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700938 };
939
940 enum PrimitiveType {
941 kPrimNot = -1
942 };
943
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700944 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700945 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700946 return Heap::AllocObject(this, this->object_size_);
947 }
948
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 Class* GetSuperClass() const {
950 return super_class_;
951 }
952
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700953 uint32_t GetSuperClassTypeIdx() const {
954 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700955 }
956
957 bool HasSuperClass() const {
958 return super_class_ != NULL;
959 }
960
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700961 bool IsAssignableFrom(const Class* klass) const {
962 DCHECK(klass != NULL);
963 if (this == klass) {
964 return true;
965 }
966 if (IsInterface()) {
967 return klass->Implements(this);
968 }
969 if (klass->IsArray()) {
970 return IsAssignableFromArray(klass);
971 }
972 return klass->IsSubClass(this);
973 }
974
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700975 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976 return class_loader_;
977 }
978
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700979 DexCache* GetDexCache() const {
980 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981 }
982
983 Class* GetComponentType() const {
984 return component_type_;
985 }
986
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700987 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700988
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700989 size_t GetComponentSize() const {
990 return GetTypeSize(component_type_->descriptor_);
991 }
992
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700993 const String* GetDescriptor() const {
994 DCHECK(descriptor_ != NULL);
995 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700996 return descriptor_;
997 }
998
Brian Carlstrom4873d462011-08-21 15:23:39 -0700999 size_t SizeOf() const {
1000 return class_size_;
1001 }
1002
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001003 Status GetStatus() const {
1004 return status_;
1005 }
1006
1007 void SetStatus(Status new_status) {
1008 // TODO: validate transition
1009 status_ = new_status;
1010 }
1011
Carl Shapiro69759ea2011-07-21 18:13:35 -07001012 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001013 bool IsErroneous() const {
1014 return GetStatus() == kStatusError;
1015 }
1016
Carl Shapiro69759ea2011-07-21 18:13:35 -07001017 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 bool IsVerified() const {
1019 return GetStatus() >= kStatusVerified;
1020 }
1021
Carl Shapiro69759ea2011-07-21 18:13:35 -07001022 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001023 bool IsLinked() const {
1024 return GetStatus() >= kStatusResolved;
1025 }
1026
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001027 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001028 bool IsLoaded() const {
1029 return GetStatus() >= kStatusLoaded;
1030 }
1031
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001032 // Returns true if the class is initialized.
1033 bool IsInitialized() const {
1034 return GetStatus() == kStatusInitialized;
1035 }
1036
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001037 // Returns true if this class is in the same packages as that class.
1038 bool IsInSamePackage(const Class* that) const;
1039
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001040 static bool IsInSamePackage(const String* descriptor1,
1041 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001042
1043 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001044 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001045
1046 // Returns true if the class is an interface.
1047 bool IsInterface() const {
1048 return (access_flags_ & kAccInterface) != 0;
1049 }
1050
1051 // Returns true if the class is declared public.
1052 bool IsPublic() const {
1053 return (access_flags_ & kAccPublic) != 0;
1054 }
1055
1056 // Returns true if the class is declared final.
1057 bool IsFinal() const {
1058 return (access_flags_ & kAccFinal) != 0;
1059 }
1060
1061 // Returns true if the class is abstract.
1062 bool IsAbstract() const {
1063 return (access_flags_ & kAccAbstract) != 0;
1064 }
1065
1066 // Returns true if the class is an annotation.
1067 bool IsAnnotation() const {
1068 return (access_flags_ & kAccAnnotation) != 0;
1069 }
1070
1071 // Returns true if the class is a primitive type.
1072 bool IsPrimitive() const {
1073 return primitive_type_ != kPrimNot;
1074 }
1075
Brian Carlstromae3ac012011-07-27 01:30:28 -07001076 // Returns true if the class is synthetic.
1077 bool IsSynthetic() const {
1078 return (access_flags_ & kAccSynthetic) != 0;
1079 }
1080
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001081 // Returns true if this class can access that class.
1082 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001084 }
1085
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001086 // Returns the number of static, private, and constructor methods.
1087 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001088 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001089 }
1090
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001091 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001092 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001093 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001094 }
1095
1096 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001097 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001098 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001099 }
1100
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001101 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001102 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001103
1104 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001105 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001106
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001107 // Returns the number of non-inherited virtual methods.
1108 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001109 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001110 }
1111
1112 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001113 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001114 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001115 }
1116
1117 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001118 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001119 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001120 }
1121
Brian Carlstrom30b94452011-08-25 21:35:26 -07001122 // Given a method implemented by this class but potentially from a
1123 // super class, return the specific implementation
1124 // method for this class.
1125 Method* FindVirtualMethodForVirtual(Method* method) {
1126 DCHECK(!method->GetDeclaringClass()->IsInterface());
1127 // The argument method may from a super class.
1128 // Use the index to a potentially overriden one for this instance's class.
1129 return vtable_->Get(method->method_index_);
1130 }
1131
1132 // Given a method implemented by this class, but potentially from a
1133 // super class or interface, return the specific implementation
1134 // method for this class.
1135 Method* FindVirtualMethodForInterface(Method* method);
1136
1137 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1138 if (method->GetDeclaringClass()->IsInterface()) {
1139 return FindVirtualMethodForInterface(method);
1140 }
1141 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001142 }
1143
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001144 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1145 const StringPiece& descriptor);
1146
1147 Method* FindVirtualMethod(const StringPiece& name,
1148 const StringPiece& descriptor);
1149
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001150 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001151 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001152 }
1153
Carl Shapiro69759ea2011-07-21 18:13:35 -07001154 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001155 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001156 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001157 }
1158
Brian Carlstrom4873d462011-08-21 15:23:39 -07001159 // Returns the number of static fields containing reference types.
1160 size_t NumReferenceStaticFields() const {
1161 return num_reference_static_fields_;
1162 }
1163
Elliott Hughescdf53122011-08-19 15:46:09 -07001164 // Finds the given instance field in this class or a superclass.
1165 Field* FindInstanceField(const StringPiece& name,
1166 const StringPiece& descriptor);
1167
1168 Field* FindDeclaredInstanceField(const StringPiece& name,
1169 const StringPiece& descriptor);
1170
1171 // Finds the given static field in this class or a superclass.
1172 Field* FindStaticField(const StringPiece& name,
1173 const StringPiece& descriptor);
1174
1175 Field* FindDeclaredStaticField(const StringPiece& name,
1176 const StringPiece& descriptor);
1177
Jesse Wilson35baaab2011-08-10 16:18:03 -04001178 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001179 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001180 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001181 }
1182
Jesse Wilson35baaab2011-08-10 16:18:03 -04001183 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001184 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001185 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001186 }
1187
1188 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001189 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001190 }
1191
Jesse Wilson35baaab2011-08-10 16:18:03 -04001192 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001193 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001194 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001195 }
1196
Jesse Wilson35baaab2011-08-10 16:18:03 -04001197 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001198 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001199 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001200 }
1201
Brian Carlstrom4873d462011-08-21 15:23:39 -07001202 uint32_t GetReferenceInstanceOffsets() const {
1203 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001204 }
1205
Brian Carlstrom4873d462011-08-21 15:23:39 -07001206 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1207 reference_instance_offsets_ = new_reference_offsets;
1208 }
1209
1210 uint32_t GetReferenceStaticOffsets() const {
1211 return reference_static_offsets_;
1212 }
1213
1214 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1215 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001216 }
1217
Carl Shapiro69759ea2011-07-21 18:13:35 -07001218 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001219 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001220 }
1221
1222 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001223 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001224 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001225 }
1226
1227 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001228 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001229 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001230 }
1231
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001232 void SetVerifyErrorClass(Class* klass) {
1233 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1234 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1235 klass->SetFieldObject(field_offset, klass);
1236 }
1237
1238 private:
1239 bool Implements(const Class* klass) const;
1240 bool IsArrayAssignableFromArray(const Class* klass) const;
1241 bool IsAssignableFromArray(const Class* klass) const;
1242 bool IsSubClass(const Class* klass) const;
1243
Ian Rogersb033c752011-07-20 12:22:35 -07001244 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001245 // descriptor for the class such as "java.lang.Class" or "[C"
1246 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001247
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001248 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1249 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001250
1251 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001252 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001253
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001254 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001255 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001256 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001257
1258 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001259 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001260
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001261 // If class verify fails, we must return same error on subsequent tries.
1262 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1263 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001264
1265 // threadId, used to check for recursive <clinit> invocation
1266 uint32_t clinit_thread_id_;
1267
1268 // Total object size; used when allocating storage on gc heap. (For
1269 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001270 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001271
1272 // For array classes, the class object for base element, for
1273 // instanceof/checkcast (for String[][][], this will be String).
1274 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001275 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001276
1277 // For array classes, the number of array dimensions, e.g. int[][]
1278 // is 2. Otherwise 0.
1279 int32_t array_rank_;
1280
1281 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1282 PrimitiveType primitive_type_;
1283
1284 // The superclass, or NULL if this is java.lang.Object or a
1285 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001286 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001287 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001288
1289 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001290 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001291
1292 // initiating class loader list
1293 // NOTE: for classes with low serialNumber, these are unused, and the
1294 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001295 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001296
1297 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001298 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001299 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001300
1301 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001302 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001303
1304 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001305 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001306
1307 // Virtual method table (vtable), for use by "invoke-virtual". The
1308 // vtable from the superclass is copied in, and virtual methods from
1309 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001310 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001311
Brian Carlstrom30b94452011-08-25 21:35:26 -07001312 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001313 // this class. That means one entry for each interface we support
1314 // directly, indirectly via superclass, or indirectly via
1315 // superinterface. This will be null if neither we nor our
1316 // superclass implement any interfaces.
1317 //
1318 // Why we need this: given "class Foo implements Face", declare
1319 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1320 // is part of the Face interface. We can't easily use a single
1321 // vtable.
1322 //
1323 // For every interface a concrete class implements, we create a list
1324 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001325 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001326 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001327 InterfaceEntry* iftable_;
1328
1329 // The interface vtable indices for iftable get stored here. By
1330 // placing them all in a single pool for each class that implements
1331 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001332 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001333 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001334 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001335
1336 // instance fields
1337 //
1338 // These describe the layout of the contents of a
1339 // DataObject-compatible Object. Note that only the fields directly
1340 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001341 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001342 //
1343 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001344 // the beginning of the field list. num_reference_instance_fields_
1345 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001346 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001347
Brian Carlstrom4873d462011-08-21 15:23:39 -07001348 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001349 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001350
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001352 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001353
1354 // source file name, if known. Otherwise, NULL.
1355 const char* source_file_;
1356
Jesse Wilson7833bd22011-08-09 18:31:44 -04001357 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001358 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001359
Brian Carlstrom4873d462011-08-21 15:23:39 -07001360 // number of static fields that are object refs
1361 size_t num_reference_static_fields_;
1362
1363 // Bitmap of offsets of sfields.
1364 uint32_t reference_static_offsets_;
1365
1366 // Total class size; used when allocating storage on gc heap.
1367 size_t class_size_;
1368
1369 // Location of first static field.
1370 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001371
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001372 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001373 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001374};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001375std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001376
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001377inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001378 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001379 DCHECK(klass_ != NULL);
1380 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001381}
1382
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001383inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001384 Class* java_lang_Class = klass_->klass_;
1385 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001386}
1387
Brian Carlstrom4873d462011-08-21 15:23:39 -07001388inline bool Object::IsClassClass() const {
1389 Class* java_lang_Class = klass_->klass_;
1390 return this == java_lang_Class;
1391}
1392
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001393inline bool Object::IsObjectArray() const {
1394 return IsArray() && !klass_->component_type_->IsPrimitive();
1395}
1396
1397inline bool Object::IsArray() const {
1398 return klass_->IsArray();
1399}
1400
Brian Carlstroma663ea52011-08-19 23:33:41 -07001401inline bool Object::IsField() const {
1402 Class* java_lang_Class = klass_->klass_;
1403 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1404 return klass_ == java_lang_reflect_Field;
1405}
1406
1407inline bool Object::IsMethod() const {
1408 Class* java_lang_Class = klass_->klass_;
1409 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1410 return klass_ == java_lang_reflect_Method;
1411}
1412
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001413inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001414 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001415 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001416 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001417 if (IsClass()) {
1418 return AsClass()->SizeOf();
1419 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001420 return klass_->object_size_;
1421}
1422
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001423inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001424 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001425}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001426
Brian Carlstrom4873d462011-08-21 15:23:39 -07001427class ClassClass : public Class {
1428 private:
1429 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1430 int32_t padding_;
1431 int64_t serialVersionUID_;
1432 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1433};
1434
1435class StringClass : public Class {
1436 private:
1437 CharArray* ASCII_;
1438 Object* CASE_INSENSITIVE_ORDER_;
1439 uint32_t REPLACEMENT_CHAR_;
1440 int64_t serialVersionUID;
1441 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1442};
1443
1444class FieldClass : public Class {
1445 private:
1446 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1447 uint32_t TYPE_BOOLEAN_;
1448 uint32_t TYPE_BYTE_;
1449 uint32_t TYPE_CHAR_;
1450 uint32_t TYPE_DOUBLE_;
1451 uint32_t TYPE_FLOAT_;
1452 uint32_t TYPE_INTEGER_;
1453 uint32_t TYPE_LONG_;
1454 uint32_t TYPE_SHORT_;
1455 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1456};
1457
1458class MethodClass : public Class {
1459 private:
1460 int32_t DECLARED_;
1461 int32_t PUBLIC_;
1462 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1463};
1464
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001465class DataObject : public Object {
1466 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001467 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001468 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001469 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001470 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001471};
1472
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001473template<class T>
1474class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001475 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001476 typedef T ElementType;
1477
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001478 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001479
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001480 const T* GetData() const {
1481 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001482 }
1483
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001484 T* GetData() {
1485 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001486 }
1487
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001488 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001489 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001490 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001491 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001492 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001493 }
1494
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001495 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001496 // TODO: ArrayStoreException
1497 if (IsValidIndex(i)) {
1498 GetData()[i] = value;
1499 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001500 }
1501
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001502 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001503 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001504 CHECK(array_class != NULL);
1505 array_class_ = array_class;
1506 }
1507
Brian Carlstroma663ea52011-08-19 23:33:41 -07001508 static void ResetArrayClass() {
1509 CHECK(array_class_ != NULL);
1510 array_class_ = NULL;
1511 }
1512
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001513 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001514 // Location of first element.
1515 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001516
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001517 static Class* array_class_;
1518
Carl Shapirof88c9522011-08-06 15:47:38 -07001519 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001520};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001521
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001522class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001523 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001524 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001525 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001526 return array_;
1527 }
1528
Carl Shapirof88c9522011-08-06 15:47:38 -07001529 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001530 return hash_code_;
1531 }
1532
Elliott Hughes814e4032011-08-23 12:07:56 -07001533 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001534 return offset_;
1535 }
1536
Elliott Hughes814e4032011-08-23 12:07:56 -07001537 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001538 return count_;
1539 }
1540
Elliott Hughes814e4032011-08-23 12:07:56 -07001541 int32_t GetUtfLength() const {
1542 return CountUtf8Bytes(array_->GetData(), count_);
1543 }
1544
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001545 // TODO: do we need this? Equals is the only caller, and could
1546 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001547 uint16_t CharAt(int32_t index) const {
1548 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001549 Thread* self = Thread::Current();
1550 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1551 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001552 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001553 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001554 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001555 }
1556
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001557 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001558 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001559 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001560 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001561 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001562 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001563 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001564 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001565 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001566 if (hash_code != 0) {
1567 string->hash_code_ = hash_code;
1568 } else {
1569 string->ComputeHashCode();
1570 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001571 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001572 }
1573
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001574 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001575 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001576 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001577 }
1578
Jesse Wilson8989d992011-08-02 13:39:42 -07001579 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1580 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001581 String* string = Alloc(GetJavaLangString(), utf16_length);
1582 uint16_t* utf16_data_out = string->array_->GetData();
1583 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1584 string->ComputeHashCode();
1585 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001586 }
1587
Brian Carlstroma663ea52011-08-19 23:33:41 -07001588 static void SetClass(Class* java_lang_String);
1589 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001590
Elliott Hughes814e4032011-08-23 12:07:56 -07001591 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001592 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1593 }
1594
Elliott Hughes814e4032011-08-23 12:07:56 -07001595 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001596 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001597 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001598 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001599 return string;
1600 }
1601
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001602 void ComputeHashCode() {
1603 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1604 }
1605
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001606 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001607 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001608 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001609 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1610 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001611 return false;
1612 }
1613 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001614 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001615 }
1616
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001617 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001618 bool Equals(const StringPiece& modified_utf8) const {
1619 // TODO: do not assume C-string representation.
1620 return Equals(modified_utf8.data());
1621 }
1622
1623 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001624 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001625 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001626 return false;
1627 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001628 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001629 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001630 return false;
1631 }
1632 }
1633 return true;
1634 }
1635
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001636 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001637 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001638 if (this->GetLength() != that_length) {
1639 return false;
1640 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001641 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001642 if (this->CharAt(i) != that_chars[that_offset + i]) {
1643 return false;
1644 }
1645 }
1646 return true;
1647 }
1648
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001649 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1650 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001651 uint16_t* chars = array_->GetData() + offset_;
1652 size_t byte_count(CountUtf8Bytes(chars, count_));
1653 std::string result(byte_count, char(0));
1654 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001655 return result;
1656 }
1657
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001658 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001659 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1660 CharArray* array_;
1661
Carl Shapirof88c9522011-08-06 15:47:38 -07001662 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001663
Elliott Hughes289da822011-08-16 10:11:20 -07001664 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001665
Elliott Hughes289da822011-08-16 10:11:20 -07001666 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001667
1668 static Class* GetJavaLangString() {
1669 DCHECK(java_lang_String_ != NULL);
1670 return java_lang_String_;
1671 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001672
1673 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001674
1675 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001676};
1677
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001678class Throwable : public Object {
1679 private:
1680 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1681 Throwable* cause_;
1682 String* detail_message_;
1683 Object* stack_state_; // Note this is Java volatile:
1684 Object* stack_trace_;
1685 Object* suppressed_exceptions_;
1686
1687 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1688};
1689
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001690class StackTraceElement : public Object {
1691 public:
1692 const String* GetDeclaringClass() const {
1693 return declaring_class_;
1694 }
1695
1696 const String* GetMethodName() const {
1697 return method_name_;
1698 }
1699
1700 const String* GetFileName() const {
1701 return file_name_;
1702 }
1703
1704 uint32_t GetLineNumber() const {
1705 return line_number_;
1706 }
1707
1708 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
1709 const String* file_name, uint32_t line_number) {
1710 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1711 trace->declaring_class_ = declaring_class;
1712 trace->method_name_ = method_name;
1713 trace->file_name_ = file_name;
1714 trace->line_number_ = line_number;
1715 return trace;
1716 }
1717
1718 static void SetClass(Class* java_lang_StackTraceElement);
1719
1720 static void ResetClass();
1721
1722 private:
1723 const String* declaring_class_;
1724 const String* method_name_;
1725 const String* file_name_;
1726 uint32_t line_number_;
1727
1728 static Class* GetStackTraceElement() {
1729 DCHECK(java_lang_StackTraceElement_ != NULL);
1730 return java_lang_StackTraceElement_;
1731 }
1732
1733 static Class* java_lang_StackTraceElement_;
1734 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1735};
1736
Brian Carlstroma663ea52011-08-19 23:33:41 -07001737inline bool Object::IsString() const {
1738 // TODO use "klass_ == String::GetJavaLangString()" instead?
1739 return klass_ == klass_->descriptor_->klass_;
1740}
1741
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001742inline size_t Class::GetTypeSize(String* descriptor) {
1743 switch (descriptor->CharAt(0)) {
1744 case 'B': return 1; // byte
1745 case 'C': return 2; // char
1746 case 'D': return 8; // double
1747 case 'F': return 4; // float
1748 case 'I': return 4; // int
1749 case 'J': return 8; // long
1750 case 'S': return 2; // short
1751 case 'Z': return 1; // boolean
1752 case 'L': return sizeof(Object*);
1753 case '[': return sizeof(Array*);
1754 default:
1755 LOG(ERROR) << "Unknown type " << descriptor;
1756 return 0;
1757 }
1758}
1759
1760inline bool Class::IsArray() const {
1761 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1762}
1763
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001764class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001765 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001766 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001767 }
1768
Brian Carlstrom30b94452011-08-25 21:35:26 -07001769 Class* GetInterface() const {
1770 DCHECK(interface_ != NULL);
1771 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001772 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001773
Brian Carlstrom30b94452011-08-25 21:35:26 -07001774 void SetInterface(Class* interface) {
1775 DCHECK(interface->IsInterface());
1776 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001777 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001778
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001779 private:
1780 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001781 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001782
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001783 public: // TODO: private
1784 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001785 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001786 // this class.
1787 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001788
1789 private:
1790 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001791};
1792
1793} // namespace art
1794
1795#endif // ART_SRC_OBJECT_H_