blob: 5ae90c965503e2994d1e99730ad795b5e5931d68 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Brian Carlstrom1f870082011-08-23 16:02:11 -07006#include <vector>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070010#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "globals.h"
12#include "logging.h"
13#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070014#include "monitor.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070016#include "offsets.h"
17#include "stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070018#include "thread.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070019#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070020
21namespace art {
22
23class Array;
24class Class;
Brian Carlstrom1f870082011-08-23 16:02:11 -070025class ClassLoader;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070026class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070027class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040028class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070029class InterfaceEntry;
30class Monitor;
31class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070032class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070033class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040034class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070035template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070036template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070037typedef PrimitiveArray<uint8_t> BooleanArray;
38typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070039typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070040typedef PrimitiveArray<double> DoubleArray;
41typedef PrimitiveArray<float> FloatArray;
42typedef PrimitiveArray<int32_t> IntArray;
43typedef PrimitiveArray<int64_t> LongArray;
44typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070045
Carl Shapiro3ee755d2011-06-28 12:11:04 -070046union JValue {
47 uint8_t z;
48 int8_t b;
49 uint16_t c;
50 int16_t s;
51 int32_t i;
52 int64_t j;
53 float f;
54 double d;
55 Object* l;
56};
57
Brian Carlstrombe977852011-07-19 14:54:54 -070058static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
59static const uint32_t kAccPrivate = 0x0002; // field, method, ic
60static const uint32_t kAccProtected = 0x0004; // field, method, ic
61static const uint32_t kAccStatic = 0x0008; // field, method, ic
62static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
63static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
64static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
65static const uint32_t kAccVolatile = 0x0040; // field
66static const uint32_t kAccBridge = 0x0040; // method (1.5)
67static const uint32_t kAccTransient = 0x0080; // field
68static const uint32_t kAccVarargs = 0x0080; // method (1.5)
69static const uint32_t kAccNative = 0x0100; // method
70static const uint32_t kAccInterface = 0x0200; // class, ic
71static const uint32_t kAccAbstract = 0x0400; // class, method, ic
72static const uint32_t kAccStrict = 0x0800; // method
73static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
74static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
75static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070077static const uint32_t kAccMiranda = 0x8000; // method
78
Brian Carlstroma331b3c2011-07-18 17:47:56 -070079static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
80
Brian Carlstrombe977852011-07-19 14:54:54 -070081static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
82static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070083
Brian Carlstrom1f870082011-08-23 16:02:11 -070084static const uint32_t kAccClassFlagsMask = (kAccPublic
85 | kAccFinal
86 | kAccInterface
87 | kAccAbstract
88 | kAccSynthetic
89 | kAccAnnotation
90 | kAccEnum);
91static const uint32_t kAccInnerClassFlagsMask = (kAccClassFlagsMask
92 | kAccPrivate
93 | kAccProtected
94 | kAccStatic);
95static const uint32_t kAccFieldFlagsMask = (kAccPublic
96 | kAccPrivate
97 | kAccProtected
98 | kAccStatic
99 | kAccFinal
100 | kAccVolatile
101 | kAccTransient
102 | kAccSynthetic
103 | kAccEnum);
104static const uint32_t kAccMethodFlagsMask = (kAccPublic
105 | kAccPrivate
106 | kAccProtected
107 | kAccStatic
108 | kAccFinal
109 | kAccSynchronized
110 | kAccBridge
111 | kAccVarargs
112 | kAccNative
113 | kAccAbstract
114 | kAccStrict
115 | kAccSynthetic
116 | kAccConstructor
117 | kAccDeclaredSynchronized);
118
119// if only kAccClassIsReference is set, we have a soft reference
120static const uint32_t kAccClassIsReference = 0x8000000; // class is a soft/weak/phantom ref
121static const uint32_t kAccClassIsWeakReference = 0x4000000; // class is a weak reference
122static const uint32_t kAccClassIsFinalizerReference = 0x2000000; // class is a finalizer reference
123static const uint32_t kAccClassIsPhantomReference = 0x1000000; // class is a phantom reference
124
125static const uint32_t kAccReferenceFlagsMask = (kAccClassIsReference
126 | kAccClassIsWeakReference
127 | kAccClassIsFinalizerReference
128 | kAccClassIsPhantomReference);
129
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700130/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700131 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700132 */
133/*
134 * A magic value for refOffsets. Ignore the bits and walk the super
135 * chain when this is the value.
136 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
137 * fields followed by 2 ref instance fields.]
138 */
139#define CLASS_WALK_SUPER ((unsigned int)(3))
140#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
141#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
142#define CLASS_OFFSET_ALIGNMENT 4
143#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
144/*
145 * Given an offset, return the bit number which would encode that offset.
146 * Local use only.
147 */
148#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
149 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
150 CLASS_OFFSET_ALIGNMENT)
151/*
152 * Is the given offset too large to be encoded?
153 */
154#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
155 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
156/*
157 * Return a single bit, encoding the offset.
158 * Undefined if the offset is too large, as defined above.
159 */
160#define CLASS_BIT_FROM_OFFSET(byteOffset) \
161 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
162/*
163 * Return an offset, given a bit number as returned from CLZ.
164 */
165#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700166 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700167
168
Carl Shapiro1fb86202011-06-27 17:43:13 -0700169class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700170 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700171 static bool InstanceOf(const Object* object, const Class* klass) {
172 if (object == NULL) {
173 return false;
174 }
175 return object->InstanceOf(klass);
176 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700177
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700178 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700179 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700180 return klass_;
181 }
182
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700183 bool InstanceOf(const Class* klass) const;
184
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700185 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186
Elliott Hughesbf86d042011-08-31 17:53:14 -0700187 Object* Clone() {
188 UNIMPLEMENTED(FATAL);
189 return NULL;
190 }
191
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192 void MonitorEnter() {
193 monitor_->Enter();
194 }
195
196 void MonitorExit() {
197 monitor_->Exit();
198 }
199
200 void Notify() {
201 monitor_->Notify();
202 }
203
204 void NotifyAll() {
205 monitor_->NotifyAll();
206 }
207
208 void Wait() {
209 monitor_->Wait();
210 }
211
212 void Wait(int64_t timeout) {
213 monitor_->Wait(timeout);
214 }
215
216 void Wait(int64_t timeout, int32_t nanos) {
217 monitor_->Wait(timeout, nanos);
218 }
219
Brian Carlstrom4873d462011-08-21 15:23:39 -0700220 Object* GetFieldObject(size_t field_offset) const {
221 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
222 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223 }
224
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700225 void SetFieldObject(size_t offset, const Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700227 *reinterpret_cast<const Object**>(raw_addr) = new_value;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228 // TODO: write barrier
229 }
230
Brian Carlstrom4873d462011-08-21 15:23:39 -0700231 uint32_t GetField32(size_t field_offset) const {
232 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
233 return *reinterpret_cast<const uint32_t*>(raw_addr);
234 }
235
236 void SetField32(size_t offset, uint32_t new_value) {
237 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
238 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
239 }
240
241 uint64_t GetField64(size_t field_offset) const {
242 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
243 return *reinterpret_cast<const uint64_t*>(raw_addr);
244 }
245
246 void SetField64(size_t offset, uint64_t new_value) {
247 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
248 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
249 }
250
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700251 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700252
253 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700254 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700255 return down_cast<Class*>(this);
256 }
257
258 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700259 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700260 return down_cast<const Class*>(this);
261 }
262
Brian Carlstrom4873d462011-08-21 15:23:39 -0700263 bool IsClassClass() const;
264
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700265 bool IsObjectArray() const;
266
267 template<class T>
268 ObjectArray<T>* AsObjectArray() {
269 DCHECK(IsObjectArray());
270 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700271 }
272
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700273 template<class T>
274 const ObjectArray<T>* AsObjectArray() const {
275 DCHECK(IsObjectArray());
276 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700277 }
278
Brian Carlstromb63ec392011-08-27 17:38:27 -0700279 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700280
281 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700282 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700283 return down_cast<Array*>(this);
284 }
285
286 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700287 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700288 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700289 }
290
Brian Carlstroma663ea52011-08-19 23:33:41 -0700291 bool IsString() const;
292
293 String* AsString() {
294 DCHECK(IsString());
295 return down_cast<String*>(this);
296 }
297
298 bool IsMethod() const;
299
300 Method* AsMethod() {
301 DCHECK(IsMethod());
302 return down_cast<Method*>(this);
303 }
304
Brian Carlstrom4873d462011-08-21 15:23:39 -0700305 const Method* AsMethod() const {
306 DCHECK(IsMethod());
307 return down_cast<const Method*>(this);
308 }
309
Brian Carlstroma663ea52011-08-19 23:33:41 -0700310 bool IsField() const;
311
312 Field* AsField() {
313 DCHECK(IsField());
314 return down_cast<Field*>(this);
315 }
316
Brian Carlstrom4873d462011-08-21 15:23:39 -0700317 const Field* AsField() const {
318 DCHECK(IsField());
319 return down_cast<const Field*>(this);
320 }
321
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700322 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700323 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700324
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700325 Monitor* monitor_;
326
327 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700328 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700329};
330
331class ObjectLock {
332 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700333 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700334 CHECK(object != NULL);
335 obj_->MonitorEnter();
336 }
337
338 ~ObjectLock() {
339 obj_->MonitorExit();
340 }
341
342 void Wait(int64_t millis = 0) {
343 return obj_->Wait(millis);
344 }
345
346 void Notify() {
347 obj_->Notify();
348 }
349
350 void NotifyAll() {
351 obj_->NotifyAll();
352 }
353
354 private:
355 Object* obj_;
356 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700357};
358
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400359class AccessibleObject : public Object {
360 private:
361 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
362 uint32_t java_flag_;
363};
364
365class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700366 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700367 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700368 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700369 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700370 }
371
Jesse Wilson14150742011-07-29 19:04:44 -0400372 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700373 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700374 return name_;
375 }
376
377 bool IsStatic() const {
378 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379 }
380
381 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700382 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700383 }
384
Brian Carlstromae3ac012011-07-27 01:30:28 -0700385 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700386 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700388 }
389
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 uint32_t GetOffset() const {
391 return offset_;
392 }
393
394 void SetOffset(size_t num_bytes) {
395 offset_ = num_bytes;
396 }
397
Brian Carlstrom4873d462011-08-21 15:23:39 -0700398 // field access, null object for static fields
399 bool GetBoolean(const Object* object) const;
400 void SetBoolean(Object* object, bool z) const;
401 int8_t GetByte(const Object* object) const;
402 void SetByte(Object* object, int8_t b) const;
403 uint16_t GetChar(const Object* object) const;
404 void SetChar(Object* object, uint16_t c) const;
405 uint16_t GetShort(const Object* object) const;
406 void SetShort(Object* object, uint16_t s) const;
407 int32_t GetInt(const Object* object) const;
408 void SetInt(Object* object, int32_t i) const;
409 int64_t GetLong(const Object* object) const;
410 void SetLong(Object* object, int64_t j) const;
411 float GetFloat(const Object* object) const;
412 void SetFloat(Object* object, float f) const;
413 double GetDouble(const Object* object) const;
414 void SetDouble(Object* object, double d) const;
415 Object* GetObject(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700416 void SetObject(Object* object, const Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700417
Brian Carlstromb9edb842011-08-28 16:31:06 -0700418 // slow path routines for static field access when field was unresolved at compile time
419 static uint32_t Get32StaticFromCode(uint32_t field_idx, const Method* referrer);
420 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value);
421 static uint64_t Get64StaticFromCode(uint32_t field_idx, const Method* referrer);
422 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value);
423 static Object* GetObjStaticFromCode(uint32_t field_idx, const Method* referrer);
424 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value);
425
Jesse Wilson35baaab2011-08-10 16:18:03 -0400426 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700427
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700428 // private implementation of field access using raw data
Brian Carlstrom4873d462011-08-21 15:23:39 -0700429 uint32_t Get32(const Object* object) const;
430 void Set32(Object* object, uint32_t new_value) const;
431 uint64_t Get64(const Object* object) const;
432 void Set64(Object* object, uint64_t new_value) const;
433 Object* GetObj(const Object* object) const;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700434 void SetObj(Object* object, const Object* new_value) const;
Brian Carlstrom4873d462011-08-21 15:23:39 -0700435
Jesse Wilson35baaab2011-08-10 16:18:03 -0400436 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
437 // The class in which this field is declared.
438 Class* declaring_class_;
439 Object* generic_type_;
440 uint32_t generic_types_are_initialized_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700441 const String* name_;
Jesse Wilson35baaab2011-08-10 16:18:03 -0400442 uint32_t offset_;
443 Class* type_;
444
445 // e.g. "I", "[C", "Landroid/os/Debug;"
446 StringPiece descriptor_;
447
448 uint32_t access_flags_;
449
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700450 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400451 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700452};
453
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400454class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700456 // An function that invokes a method with an array of its arguments.
457 typedef void InvokeStub(Method* method,
458 Object* obj,
459 Thread* thread,
460 byte* args,
461 JValue* result);
462
Brian Carlstromae3ac012011-07-27 01:30:28 -0700463 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700464 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700465 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700466 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700467 }
468
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700469 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700470 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700471 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700472 }
473
Brian Carlstroma0808032011-07-18 00:39:23 -0700474 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700475 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700476 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700477 }
478
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700479 static MemberOffset DeclaringClassOffset() {
480 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700481 }
482
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700483 // Returns true if the method is declared public.
484 bool IsPublic() const {
485 return (access_flags_ & kAccPublic) != 0;
486 }
487
488 // Returns true if the method is declared private.
489 bool IsPrivate() const {
490 return (access_flags_ & kAccPrivate) != 0;
491 }
492
493 // Returns true if the method is declared static.
494 bool IsStatic() const {
495 return (access_flags_ & kAccStatic) != 0;
496 }
497
Brian Carlstrom1f870082011-08-23 16:02:11 -0700498 // Returns true if the method is a constructor.
499 bool IsConstructor() const {
500 return (access_flags_ & kAccConstructor) != 0;
501 }
502
503 // Returns true if the method is static, private, or a constructor.
504 bool IsDirect() const {
505 return IsStatic() || IsPrivate() || IsConstructor();
506 }
507
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700508 // Returns true if the method is declared synchronized.
509 bool IsSynchronized() const {
510 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
511 return (access_flags_ & synchonized) != 0;
512 }
513
514 // Returns true if the method is declared final.
515 bool IsFinal() const {
516 return (access_flags_ & kAccFinal) != 0;
517 }
518
519 // Returns true if the method is declared native.
520 bool IsNative() const {
521 return (access_flags_ & kAccNative) != 0;
522 }
523
524 // Returns true if the method is declared abstract.
525 bool IsAbstract() const {
526 return (access_flags_ & kAccAbstract) != 0;
527 }
528
529 bool IsSynthetic() const {
530 return (access_flags_ & kAccSynthetic) != 0;
531 }
532
533 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700534 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700536 // Number of argument bytes required for densely packing the
537 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700538 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700539
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700540 // Converts a native PC to a virtual PC. TODO: this is a no-op
541 // until we associate a PC mapping table with each method.
542 uintptr_t ToDexPC(const uintptr_t pc) const {
543 return pc;
544 }
545
546 // Converts a virtual PC to a native PC. TODO: this is a no-op
547 // until we associate a PC mapping table with each method.
548 uintptr_t ToNativePC(const uintptr_t pc) const {
549 return pc;
550 }
551
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700552 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400553 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400554 // the class we are a part of
555 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400556 ObjectArray<Class>* java_exception_types_;
557 Object* java_formal_type_parameters_;
558 Object* java_generic_exception_types_;
559 Object* java_generic_parameter_types_;
560 Object* java_generic_return_type_;
561 Class* java_return_type_;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700562 const String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400563 ObjectArray<Class>* java_parameter_types_;
564 uint32_t java_generic_types_are_initialized_;
565 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700566
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700567 const StringPiece& GetShorty() const {
568 return shorty_;
569 }
570
Ian Rogersb033c752011-07-20 12:22:35 -0700571 bool IsReturnAReference() const {
572 return (shorty_[0] == 'L') || (shorty_[0] == '[');
573 }
574
575 bool IsReturnAFloatOrDouble() const {
576 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
577 }
578
579 bool IsReturnAFloat() const {
580 return shorty_[0] == 'F';
581 }
582
583 bool IsReturnADouble() const {
584 return shorty_[0] == 'D';
585 }
586
587 bool IsReturnALong() const {
588 return shorty_[0] == 'J';
589 }
590
Ian Rogers45a76cb2011-07-21 22:00:15 -0700591 bool IsReturnVoid() const {
592 return shorty_[0] == 'V';
593 }
594
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700595 // "Args" may refer to any of the 3 levels of "Args."
596 // To avoid confusion, our code will denote which "Args" clearly:
597 // 1. UserArgs: Args that a user see.
598 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
599 // receiver.
600 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
601 // E.g., the first in Args is Method* for both static and non-static
602 // methods. And CConvArgs doesn't deal with the receiver because
603 // receiver is hardwired in an implicit register, so CConvArgs doesn't
604 // need to deal with it.
605 //
606 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700607 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700608 // "1 +" because the first in Args is the receiver.
609 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700610 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
611 }
612
613 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700614 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700615 size_t NumReferenceArgs() const;
616
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700617 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700618 size_t NumLongOrDoubleArgs() const;
619
620 // The number of reference arguments to this method before the given
621 // parameter index
622 size_t NumReferenceArgsBefore(unsigned int param) const;
623
624 // Is the given method parameter a reference?
625 bool IsParamAReference(unsigned int param) const;
626
627 // Is the given method parameter a long or double?
628 bool IsParamALongOrDouble(unsigned int param) const;
629
Ian Rogersdf20fe02011-07-20 20:34:16 -0700630 // Size in bytes of the given parameter
631 size_t ParamSize(unsigned int param) const;
632
633 // Size in bytes of the return value
634 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700635
buzbeec143c552011-08-20 17:38:58 -0700636 bool HasCode() {
637 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700638 }
639
Brian Carlstrom83db7722011-08-26 17:32:56 -0700640 const void* GetCode() {
641 return code_;
642 }
643
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700644 void SetCode(const byte* compiled_code,
645 size_t byte_count,
646 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700647 // Copy the code into an executable region.
648 code_instruction_set_ = set;
649 code_area_.reset(MemMap::Map(byte_count,
650 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700651 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700652 byte* code = code_area_->GetAddress();
653 memcpy(code, compiled_code, byte_count);
654 __builtin___clear_cache(code, code + byte_count);
655
656 uintptr_t address = reinterpret_cast<uintptr_t>(code);
657 if (code_instruction_set_ == kThumb2) {
658 // Set the low-order bit so a BLX will switch to Thumb mode
659 address |= 0x1;
660 }
661 code_ = reinterpret_cast<void*>(address);
662 }
663
Shih-wei Liaod11af152011-08-23 16:02:11 -0700664 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
665 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700666 }
667
Shih-wei Liaod11af152011-08-23 16:02:11 -0700668 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
669 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700670 }
671
Shih-wei Liaod11af152011-08-23 16:02:11 -0700672 size_t GetFrameSizeInBytes() const {
673 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700674 }
675
Shih-wei Liaod11af152011-08-23 16:02:11 -0700676 size_t GetReturnPcOffsetInBytes() const {
677 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700678 }
679
buzbeec143c552011-08-20 17:38:58 -0700680 void SetCoreSpillMask(uint32_t core_spill_mask) {
681 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700682 }
683
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700684 static size_t GetCodeOffset() {
685 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700686 }
687
buzbeec143c552011-08-20 17:38:58 -0700688 void SetFpSpillMask(uint32_t fp_spill_mask) {
689 fp_spill_mask_ = fp_spill_mask;
690 }
691
Ian Rogersb033c752011-07-20 12:22:35 -0700692 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700693 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700694 native_method_ = native_method;
695 }
696
Elliott Hughes5174fe62011-08-23 15:12:35 -0700697 void UnregisterNative() {
698 native_method_ = NULL;
699 }
700
Ian Rogersb033c752011-07-20 12:22:35 -0700701 static MemberOffset NativeMethodOffset() {
702 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
703 }
704
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700705 InvokeStub* GetInvokeStub() const {
706 return invoke_stub_;
707 }
708
709 void SetInvokeStub(const InvokeStub* invoke_stub) {
710 invoke_stub_ = invoke_stub;
711 }
712
713 static size_t GetInvokeStubOffset() {
714 return OFFSETOF_MEMBER(Method, invoke_stub_);
715 }
716
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700717 bool HasSameNameAndDescriptor(const Method* that) const;
718
Ian Rogersb033c752011-07-20 12:22:35 -0700719 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 // access flags; low 16 bits are defined by spec (could be uint16_t?)
721 uint32_t access_flags_;
722
723 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700724 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700725 //
726 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700727 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728 uint16_t method_index_;
729
730 // Method bounds; not needed for an abstract method.
731 //
732 // For a native method, we compute the size of the argument list, and
733 // set "insSize" and "registerSize" equal to it.
734 uint16_t num_registers_; // ins + locals
735 uint16_t num_outs_;
736 uint16_t num_ins_;
737
buzbeec143c552011-08-20 17:38:58 -0700738 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700739 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700740
741 // Architecture-dependent register spill masks
742 uint32_t core_spill_mask_;
743 uint32_t fp_spill_mask_;
744
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700745 // The method descriptor. This represents the parameters a method
746 // takes and value it returns. This string is a list of the type
747 // descriptors for the parameters enclosed in parenthesis followed
748 // by the return type descriptor. For example, for the method
749 //
750 // Object mymethod(int i, double d, Thread t)
751 //
752 // the method descriptor would be
753 //
754 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700755 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700756
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700757 // Method prototype descriptor string (return and argument types).
758 uint32_t proto_idx_;
759
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700760 // Offset to the CodeItem.
761 uint32_t code_off_;
762
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763 // The short-form method descriptor string.
764 StringPiece shorty_;
765
Ian Rogers762400c2011-08-23 12:14:16 -0700766 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
767 // access
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700768 ObjectArray<const String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700769 ObjectArray<Class>* dex_cache_resolved_types_;
770 ObjectArray<Method>* dex_cache_resolved_methods_;
771 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700772 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700773 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700774
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700775 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700776 // Compiled code associated with this method
Elliott Hughes90a33692011-08-30 13:27:07 -0700777 UniquePtr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700778 const void* code_;
779 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700780 InstructionSet code_instruction_set_;
781
782 // Size in bytes of compiled code associated with this method
783 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700784
Ian Rogers762400c2011-08-23 12:14:16 -0700785 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700786 // Offset of PC within compiled code (in bytes)
787 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700788
Ian Rogersb033c752011-07-20 12:22:35 -0700789 // Any native method registered with this method
790 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700791
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700792 // Native invocation stub entry point.
793 const InvokeStub* invoke_stub_;
794
Carl Shapirof88c9522011-08-06 15:47:38 -0700795 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700796};
797
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700798class Array : public Object {
799 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700800 static size_t SizeOf(size_t component_count,
801 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700802 return sizeof(Array) + component_count * component_size;
803 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700804
Brian Carlstromb63ec392011-08-27 17:38:27 -0700805 // Given the context of a calling Method, use its DexCache to
806 // resolve a type to an array Class. If it cannot be resolved, throw
807 // an error. If it can, use it to create an array.
808 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
809
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700810 // A convenience for code that doesn't know the component size,
811 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700812 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700813
Brian Carlstromb63ec392011-08-27 17:38:27 -0700814 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700815
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700816 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700817
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700818 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700819 return length_;
820 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700821
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700822 void SetLength(uint32_t length) {
823 length_ = length;
824 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700825
buzbeec143c552011-08-20 17:38:58 -0700826 static MemberOffset LengthOffset() {
827 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
828 }
829
830 static MemberOffset DataOffset() {
831 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
832 }
833
Elliott Hughesbf86d042011-08-31 17:53:14 -0700834 void* GetRawData() {
835 return reinterpret_cast<void*>(first_element_);
836 }
837
Elliott Hughes289da822011-08-16 10:11:20 -0700838 protected:
839 bool IsValidIndex(int32_t index) const {
840 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700841 Thread* self = Thread::Current();
842 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
843 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700844 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700845 }
846 return true;
847 }
848
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700849 private:
850 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700851 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400852 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
853 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700854 // Marker for the data (used by generated code)
855 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700856
Carl Shapirof88c9522011-08-06 15:47:38 -0700857 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700858};
859
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700860template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700861class ObjectArray : public Array {
862 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700863 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700864 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700865 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700866 }
867
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700868 T* const * GetData() const {
869 return reinterpret_cast<T* const *>(&elements_);
870 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400871
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700872 T** GetData() {
873 return reinterpret_cast<T**>(&elements_);
874 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400875
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700876 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700877 if (!IsValidIndex(i)) {
878 return NULL;
879 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700880 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700881 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700882
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700883 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700884 if (IsValidIndex(i)) {
885 // TODO: ArrayStoreException
886 GetData()[i] = object; // TODO: write barrier
887 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700888 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700889
890 static void Copy(ObjectArray<T>* src, int src_pos,
891 ObjectArray<T>* dst, int dst_pos,
892 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700893 for (size_t i = 0; i < length; i++) {
894 dst->Set(dst_pos + i, src->Get(src_pos + i));
895 }
896 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700897
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700898 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700899 ObjectArray<T>* new_array = Alloc(klass_, new_length);
900 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
901 return new_array;
902 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700903
904 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700905 // Location of first element.
906 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700907
908 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700909};
910
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700911// Type for the InitializedStaticStorage table. Currently the Class
912// provides the static storage. However, this might change to improve
913// image sharing, so we use this type to avoid assumptions on the
914// current storage.
915class StaticStorageBase {};
916
Carl Shapiro1fb86202011-06-27 17:43:13 -0700917// Class objects.
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700918class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700919 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700920
921 // Class Status
922 //
923 // kStatusNotReady: If a Class cannot be found in the class table by
924 // FindClass, it allocates an new one with AllocClass in the
925 // kStatusNotReady and calls LoadClass. Note if it does find a
926 // class, it may not be kStatusResolved and it will try to push it
927 // forward toward kStatusResolved.
928 //
929 // kStatusIdx: LoadClass populates with Class with information from
930 // the DexFile, moving the status to kStatusIdx, indicating that the
931 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700932 // populated based on super_class_type_idx_ and
933 // interfaces_type_idx_. The new Class can then be inserted into the
934 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700935 //
936 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
937 // attempt to move a kStatusIdx class forward to kStatusLoaded by
938 // using ResolveClass to initialize the super_class_ and interfaces_.
939 //
940 // kStatusResolved: Still holding the lock on Class, the ClassLinker
941 // will use LinkClass to link all members, creating Field and Method
942 // objects, setting up the vtable, etc. On success, the class is
943 // marked kStatusResolved.
944
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700945 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700946 kStatusError = -1,
947 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700948 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700949 kStatusLoaded = 2, // DEX idx values resolved
950 kStatusResolved = 3, // part of linking
951 kStatusVerifying = 4, // in the process of being verified
952 kStatusVerified = 5, // logically part of linking; done pre-init
953 kStatusInitializing = 6, // class init in progress
954 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700955 };
956
957 enum PrimitiveType {
958 kPrimNot = -1
959 };
960
Brian Carlstromb63ec392011-08-27 17:38:27 -0700961 // Given the context of a calling Method, use its DexCache to
962 // resolve a type to a Class. If it cannot be resolved, throw an
963 // error. If it can, use it to create an instance.
Brian Carlstrom1f870082011-08-23 16:02:11 -0700964 static Object* AllocObjectFromCode(uint32_t type_idx, Method* method);
Brian Carlstromb63ec392011-08-27 17:38:27 -0700965
Brian Carlstrom1f870082011-08-23 16:02:11 -0700966 // Creates a raw object instance but does not invoke the default constructor.
967 Object* AllocObject();
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700968
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700969 Class* GetSuperClass() const {
970 return super_class_;
971 }
972
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700973 uint32_t GetSuperClassTypeIdx() const {
974 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700975 }
976
977 bool HasSuperClass() const {
978 return super_class_ != NULL;
979 }
980
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700981 bool IsAssignableFrom(const Class* klass) const {
982 DCHECK(klass != NULL);
983 if (this == klass) {
984 return true;
985 }
986 if (IsInterface()) {
987 return klass->Implements(this);
988 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700989 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700990 return IsAssignableFromArray(klass);
991 }
992 return klass->IsSubClass(this);
993 }
994
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700995 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700996 return class_loader_;
997 }
998
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700999 DexCache* GetDexCache() const {
1000 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001001 }
1002
1003 Class* GetComponentType() const {
1004 return component_type_;
1005 }
1006
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001007 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001008
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001009 size_t GetComponentSize() const {
1010 return GetTypeSize(component_type_->descriptor_);
1011 }
1012
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001013 const String* GetDescriptor() const {
1014 DCHECK(descriptor_ != NULL);
1015 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001016 return descriptor_;
1017 }
1018
Brian Carlstrom4873d462011-08-21 15:23:39 -07001019 size_t SizeOf() const {
1020 return class_size_;
1021 }
1022
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001023 Status GetStatus() const {
1024 return status_;
1025 }
1026
1027 void SetStatus(Status new_status) {
1028 // TODO: validate transition
1029 status_ = new_status;
1030 }
1031
Carl Shapiro69759ea2011-07-21 18:13:35 -07001032 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 bool IsErroneous() const {
1034 return GetStatus() == kStatusError;
1035 }
1036
Carl Shapiro69759ea2011-07-21 18:13:35 -07001037 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 bool IsVerified() const {
1039 return GetStatus() >= kStatusVerified;
1040 }
1041
Carl Shapiro69759ea2011-07-21 18:13:35 -07001042 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001043 bool IsLinked() const {
1044 return GetStatus() >= kStatusResolved;
1045 }
1046
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001047 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001048 bool IsLoaded() const {
1049 return GetStatus() >= kStatusLoaded;
1050 }
1051
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001052 // Returns true if the class is initialized.
1053 bool IsInitialized() const {
1054 return GetStatus() == kStatusInitialized;
1055 }
1056
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001057 // Returns true if this class is in the same packages as that class.
1058 bool IsInSamePackage(const Class* that) const;
1059
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001060 static bool IsInSamePackage(const String* descriptor1,
1061 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001062
1063 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001064 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001065
1066 // Returns true if the class is an interface.
1067 bool IsInterface() const {
1068 return (access_flags_ & kAccInterface) != 0;
1069 }
1070
1071 // Returns true if the class is declared public.
1072 bool IsPublic() const {
1073 return (access_flags_ & kAccPublic) != 0;
1074 }
1075
1076 // Returns true if the class is declared final.
1077 bool IsFinal() const {
1078 return (access_flags_ & kAccFinal) != 0;
1079 }
1080
1081 // Returns true if the class is abstract.
1082 bool IsAbstract() const {
1083 return (access_flags_ & kAccAbstract) != 0;
1084 }
1085
1086 // Returns true if the class is an annotation.
1087 bool IsAnnotation() const {
1088 return (access_flags_ & kAccAnnotation) != 0;
1089 }
1090
1091 // Returns true if the class is a primitive type.
1092 bool IsPrimitive() const {
1093 return primitive_type_ != kPrimNot;
1094 }
1095
Brian Carlstromae3ac012011-07-27 01:30:28 -07001096 // Returns true if the class is synthetic.
1097 bool IsSynthetic() const {
1098 return (access_flags_ & kAccSynthetic) != 0;
1099 }
1100
Brian Carlstrom1f870082011-08-23 16:02:11 -07001101 bool IsReference() const {
1102 return (access_flags_ & kAccClassIsReference) != 0;
1103 }
1104
1105 bool IsWeakReference() const {
1106 return (access_flags_ & kAccClassIsWeakReference) != 0;
1107 }
1108
1109 bool IsSoftReference() const {
1110 return (access_flags_ & ~kAccReferenceFlagsMask) == kAccClassIsReference;
1111 }
1112
1113 bool IsFinalizerReference() const {
1114 return (access_flags_ & kAccClassIsFinalizerReference) != 0;
1115 }
1116
1117 bool IsPhantomReference() const {
1118 return (access_flags_ & kAccClassIsPhantomReference) != 0;
1119 }
1120
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001121 // Returns true if this class can access that class.
1122 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001123 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001124 }
1125
Elliott Hughesbf86d042011-08-31 17:53:14 -07001126 static bool CanPutArrayElementNoThrow(const Class* elementClass, const Class* arrayClass);
1127
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001128 // Returns the number of static, private, and constructor methods.
1129 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001130 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001131 }
1132
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001133 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001134 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001135 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001136 }
1137
1138 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001139 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001140 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001141 }
1142
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001143 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001144 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001145
1146 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001147 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001148
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001149 // Returns the number of non-inherited virtual methods.
1150 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001151 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001152 }
1153
1154 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001155 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001156 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001157 }
1158
1159 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001160 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001161 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001162 }
1163
Brian Carlstrom30b94452011-08-25 21:35:26 -07001164 // Given a method implemented by this class but potentially from a
1165 // super class, return the specific implementation
1166 // method for this class.
1167 Method* FindVirtualMethodForVirtual(Method* method) {
1168 DCHECK(!method->GetDeclaringClass()->IsInterface());
1169 // The argument method may from a super class.
1170 // Use the index to a potentially overriden one for this instance's class.
1171 return vtable_->Get(method->method_index_);
1172 }
1173
1174 // Given a method implemented by this class, but potentially from a
1175 // super class or interface, return the specific implementation
1176 // method for this class.
1177 Method* FindVirtualMethodForInterface(Method* method);
1178
1179 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1180 if (method->GetDeclaringClass()->IsInterface()) {
1181 return FindVirtualMethodForInterface(method);
1182 }
1183 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001184 }
1185
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001186 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1187 const StringPiece& descriptor);
1188
1189 Method* FindVirtualMethod(const StringPiece& name,
1190 const StringPiece& descriptor);
1191
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001192 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001193 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001194 }
1195
Carl Shapiro69759ea2011-07-21 18:13:35 -07001196 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001197 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001198 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001199 }
1200
Brian Carlstrom4873d462011-08-21 15:23:39 -07001201 // Returns the number of static fields containing reference types.
1202 size_t NumReferenceStaticFields() const {
1203 return num_reference_static_fields_;
1204 }
1205
Elliott Hughescdf53122011-08-19 15:46:09 -07001206 // Finds the given instance field in this class or a superclass.
1207 Field* FindInstanceField(const StringPiece& name,
1208 const StringPiece& descriptor);
1209
Brian Carlstrom1f870082011-08-23 16:02:11 -07001210 // Finds the given instance field in this class.
Elliott Hughescdf53122011-08-19 15:46:09 -07001211 Field* FindDeclaredInstanceField(const StringPiece& name,
1212 const StringPiece& descriptor);
1213
1214 // Finds the given static field in this class or a superclass.
1215 Field* FindStaticField(const StringPiece& name,
1216 const StringPiece& descriptor);
1217
Brian Carlstrom1f870082011-08-23 16:02:11 -07001218 // Finds the given static field in this class.
Elliott Hughescdf53122011-08-19 15:46:09 -07001219 Field* FindDeclaredStaticField(const StringPiece& name,
1220 const StringPiece& descriptor);
1221
Jesse Wilson35baaab2011-08-10 16:18:03 -04001222 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001223 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001224 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001225 }
1226
Jesse Wilson35baaab2011-08-10 16:18:03 -04001227 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001228 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001229 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001230 }
1231
1232 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001233 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001234 }
1235
Jesse Wilson35baaab2011-08-10 16:18:03 -04001236 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001237 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001238 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001239 }
1240
Jesse Wilson35baaab2011-08-10 16:18:03 -04001241 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001242 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001243 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001244 }
1245
Brian Carlstrom4873d462011-08-21 15:23:39 -07001246 uint32_t GetReferenceInstanceOffsets() const {
1247 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001248 }
1249
Brian Carlstrom4873d462011-08-21 15:23:39 -07001250 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1251 reference_instance_offsets_ = new_reference_offsets;
1252 }
1253
1254 uint32_t GetReferenceStaticOffsets() const {
1255 return reference_static_offsets_;
1256 }
1257
1258 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1259 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001260 }
1261
Carl Shapiro69759ea2011-07-21 18:13:35 -07001262 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001263 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001264 }
1265
1266 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001267 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001268 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001269 }
1270
1271 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001272 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001273 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001274 }
1275
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001276 void SetVerifyErrorClass(Class* klass) {
1277 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1278 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1279 klass->SetFieldObject(field_offset, klass);
1280 }
1281
1282 private:
1283 bool Implements(const Class* klass) const;
1284 bool IsArrayAssignableFromArray(const Class* klass) const;
1285 bool IsAssignableFromArray(const Class* klass) const;
1286 bool IsSubClass(const Class* klass) const;
1287
Ian Rogersb033c752011-07-20 12:22:35 -07001288 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001289 // descriptor for the class such as "java.lang.Class" or "[C"
1290 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001291
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001292 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1293 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001294
1295 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001296 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001297
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001298 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001299 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001300 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001301
1302 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001303 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001304
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001305 // If class verify fails, we must return same error on subsequent tries.
1306 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1307 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001308
1309 // threadId, used to check for recursive <clinit> invocation
1310 uint32_t clinit_thread_id_;
1311
1312 // Total object size; used when allocating storage on gc heap. (For
1313 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001314 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001315
1316 // For array classes, the class object for base element, for
1317 // instanceof/checkcast (for String[][][], this will be String).
1318 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001319 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001320
1321 // For array classes, the number of array dimensions, e.g. int[][]
1322 // is 2. Otherwise 0.
1323 int32_t array_rank_;
1324
1325 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1326 PrimitiveType primitive_type_;
1327
1328 // The superclass, or NULL if this is java.lang.Object or a
1329 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001330 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001331 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001332
1333 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001334 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001335
1336 // initiating class loader list
1337 // NOTE: for classes with low serialNumber, these are unused, and the
1338 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001339 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001340
1341 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001342 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001343 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001344
1345 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001346 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001347
1348 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001349 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001350
1351 // Virtual method table (vtable), for use by "invoke-virtual". The
1352 // vtable from the superclass is copied in, and virtual methods from
1353 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001354 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001355
Brian Carlstrom30b94452011-08-25 21:35:26 -07001356 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001357 // this class. That means one entry for each interface we support
1358 // directly, indirectly via superclass, or indirectly via
1359 // superinterface. This will be null if neither we nor our
1360 // superclass implement any interfaces.
1361 //
1362 // Why we need this: given "class Foo implements Face", declare
1363 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1364 // is part of the Face interface. We can't easily use a single
1365 // vtable.
1366 //
1367 // For every interface a concrete class implements, we create a list
1368 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001369 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001370 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001371 InterfaceEntry* iftable_;
1372
1373 // The interface vtable indices for iftable get stored here. By
1374 // placing them all in a single pool for each class that implements
1375 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001376 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001377 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001378 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001379
1380 // instance fields
1381 //
1382 // These describe the layout of the contents of a
1383 // DataObject-compatible Object. Note that only the fields directly
1384 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001385 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001386 //
1387 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001388 // the beginning of the field list. num_reference_instance_fields_
1389 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001390 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001391
Brian Carlstrom4873d462011-08-21 15:23:39 -07001392 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001393 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001394
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001396 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001397
1398 // source file name, if known. Otherwise, NULL.
1399 const char* source_file_;
1400
Jesse Wilson7833bd22011-08-09 18:31:44 -04001401 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001402 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001403
Brian Carlstrom4873d462011-08-21 15:23:39 -07001404 // number of static fields that are object refs
1405 size_t num_reference_static_fields_;
1406
1407 // Bitmap of offsets of sfields.
1408 uint32_t reference_static_offsets_;
1409
1410 // Total class size; used when allocating storage on gc heap.
1411 size_t class_size_;
1412
1413 // Location of first static field.
1414 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001415
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001416 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001417 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001418};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001419std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001420
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001421inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001422 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001423 DCHECK(klass_ != NULL);
1424 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001425}
1426
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001427inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001428 Class* java_lang_Class = klass_->klass_;
1429 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001430}
1431
Brian Carlstrom4873d462011-08-21 15:23:39 -07001432inline bool Object::IsClassClass() const {
1433 Class* java_lang_Class = klass_->klass_;
1434 return this == java_lang_Class;
1435}
1436
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001437inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001438 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001439}
1440
Brian Carlstromb63ec392011-08-27 17:38:27 -07001441inline bool Object::IsArrayInstance() const {
1442 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001443}
1444
Brian Carlstroma663ea52011-08-19 23:33:41 -07001445inline bool Object::IsField() const {
1446 Class* java_lang_Class = klass_->klass_;
1447 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1448 return klass_ == java_lang_reflect_Field;
1449}
1450
1451inline bool Object::IsMethod() const {
1452 Class* java_lang_Class = klass_->klass_;
1453 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1454 return klass_ == java_lang_reflect_Method;
1455}
1456
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001457inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001458 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001459 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001460 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001461 if (IsClass()) {
1462 return AsClass()->SizeOf();
1463 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001464 return klass_->object_size_;
1465}
1466
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001467inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001468 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001469}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001470
Brian Carlstrom4873d462011-08-21 15:23:39 -07001471class ClassClass : public Class {
1472 private:
1473 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1474 int32_t padding_;
1475 int64_t serialVersionUID_;
1476 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1477};
1478
1479class StringClass : public Class {
1480 private:
1481 CharArray* ASCII_;
1482 Object* CASE_INSENSITIVE_ORDER_;
1483 uint32_t REPLACEMENT_CHAR_;
1484 int64_t serialVersionUID;
1485 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1486};
1487
1488class FieldClass : public Class {
1489 private:
1490 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1491 uint32_t TYPE_BOOLEAN_;
1492 uint32_t TYPE_BYTE_;
1493 uint32_t TYPE_CHAR_;
1494 uint32_t TYPE_DOUBLE_;
1495 uint32_t TYPE_FLOAT_;
1496 uint32_t TYPE_INTEGER_;
1497 uint32_t TYPE_LONG_;
1498 uint32_t TYPE_SHORT_;
1499 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1500};
1501
1502class MethodClass : public Class {
1503 private:
1504 int32_t DECLARED_;
1505 int32_t PUBLIC_;
1506 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1507};
1508
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001509class DataObject : public Object {
1510 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001511 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001512 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001513 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001514 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001515};
1516
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001517template<class T>
1518class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001519 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001520 typedef T ElementType;
1521
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001522 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001523
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001524 const T* GetData() const {
1525 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001526 }
1527
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001528 T* GetData() {
1529 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001530 }
1531
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001532 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001533 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001534 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001535 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001536 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001537 }
1538
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001539 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001540 // TODO: ArrayStoreException
1541 if (IsValidIndex(i)) {
1542 GetData()[i] = value;
1543 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001544 }
1545
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001546 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001547 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001548 CHECK(array_class != NULL);
1549 array_class_ = array_class;
1550 }
1551
Brian Carlstroma663ea52011-08-19 23:33:41 -07001552 static void ResetArrayClass() {
1553 CHECK(array_class_ != NULL);
1554 array_class_ = NULL;
1555 }
1556
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001557 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001558 // Location of first element.
1559 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001560
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001561 static Class* array_class_;
1562
Carl Shapirof88c9522011-08-06 15:47:38 -07001563 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001564};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001565
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001566class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001567 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001568 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001569 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001570 return array_;
1571 }
1572
Carl Shapirof88c9522011-08-06 15:47:38 -07001573 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001574 return hash_code_;
1575 }
1576
Elliott Hughes814e4032011-08-23 12:07:56 -07001577 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001578 return offset_;
1579 }
1580
Elliott Hughes814e4032011-08-23 12:07:56 -07001581 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001582 return count_;
1583 }
1584
Elliott Hughes814e4032011-08-23 12:07:56 -07001585 int32_t GetUtfLength() const {
1586 return CountUtf8Bytes(array_->GetData(), count_);
1587 }
1588
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001589 // TODO: do we need this? Equals is the only caller, and could
1590 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001591 uint16_t CharAt(int32_t index) const {
1592 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001593 Thread* self = Thread::Current();
1594 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1595 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001596 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001597 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001598 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001599 }
1600
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001601 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001602 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001603 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001604 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001605 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001606 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001607 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001608 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001609 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001610 if (hash_code != 0) {
1611 string->hash_code_ = hash_code;
1612 } else {
1613 string->ComputeHashCode();
1614 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001615 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001616 }
1617
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001618 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001619 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001620 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001621 }
1622
Jesse Wilson8989d992011-08-02 13:39:42 -07001623 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1624 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001625 String* string = Alloc(GetJavaLangString(), utf16_length);
1626 uint16_t* utf16_data_out = string->array_->GetData();
1627 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1628 string->ComputeHashCode();
1629 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001630 }
1631
Brian Carlstroma663ea52011-08-19 23:33:41 -07001632 static void SetClass(Class* java_lang_String);
1633 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001634
Elliott Hughes814e4032011-08-23 12:07:56 -07001635 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001636 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1637 }
1638
Elliott Hughes814e4032011-08-23 12:07:56 -07001639 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstrom1f870082011-08-23 16:02:11 -07001640 String* string = down_cast<String*>(java_lang_String->AllocObject());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001641 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001642 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001643 return string;
1644 }
1645
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001646 void ComputeHashCode() {
1647 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1648 }
1649
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001650 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001651 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001652 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001653 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1654 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001655 return false;
1656 }
1657 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001658 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001659 }
1660
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001661 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001662 bool Equals(const StringPiece& modified_utf8) const {
1663 // TODO: do not assume C-string representation.
1664 return Equals(modified_utf8.data());
1665 }
1666
1667 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001668 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001669 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001670 return false;
1671 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001672 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001673 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001674 return false;
1675 }
1676 }
1677 return true;
1678 }
1679
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001680 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001681 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001682 if (this->GetLength() != that_length) {
1683 return false;
1684 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001685 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001686 if (this->CharAt(i) != that_chars[that_offset + i]) {
1687 return false;
1688 }
1689 }
1690 return true;
1691 }
1692
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001693 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1694 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001695 uint16_t* chars = array_->GetData() + offset_;
1696 size_t byte_count(CountUtf8Bytes(chars, count_));
1697 std::string result(byte_count, char(0));
1698 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001699 return result;
1700 }
1701
Elliott Hughescf4c6c42011-09-01 15:16:42 -07001702 const String* Intern() const;
Elliott Hughesbf86d042011-08-31 17:53:14 -07001703
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001704 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001705 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1706 CharArray* array_;
1707
Carl Shapirof88c9522011-08-06 15:47:38 -07001708 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001709
Elliott Hughes289da822011-08-16 10:11:20 -07001710 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001711
Elliott Hughes289da822011-08-16 10:11:20 -07001712 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001713
1714 static Class* GetJavaLangString() {
1715 DCHECK(java_lang_String_ != NULL);
1716 return java_lang_String_;
1717 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001718
1719 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001720
1721 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001722};
1723
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001724class Throwable : public Object {
1725 private:
1726 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1727 Throwable* cause_;
1728 String* detail_message_;
1729 Object* stack_state_; // Note this is Java volatile:
1730 Object* stack_trace_;
1731 Object* suppressed_exceptions_;
1732
1733 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1734};
1735
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001736class StackTraceElement : public Object {
1737 public:
1738 const String* GetDeclaringClass() const {
1739 return declaring_class_;
1740 }
1741
1742 const String* GetMethodName() const {
1743 return method_name_;
1744 }
1745
1746 const String* GetFileName() const {
1747 return file_name_;
1748 }
1749
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001750 int32_t GetLineNumber() const {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001751 return line_number_;
1752 }
1753
1754 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001755 const String* file_name, int32_t line_number) {
Brian Carlstrom1f870082011-08-23 16:02:11 -07001756 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->AllocObject());
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001757 trace->declaring_class_ = declaring_class;
1758 trace->method_name_ = method_name;
1759 trace->file_name_ = file_name;
1760 trace->line_number_ = line_number;
1761 return trace;
1762 }
1763
1764 static void SetClass(Class* java_lang_StackTraceElement);
1765
1766 static void ResetClass();
1767
1768 private:
1769 const String* declaring_class_;
1770 const String* method_name_;
1771 const String* file_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001772 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001773
1774 static Class* GetStackTraceElement() {
1775 DCHECK(java_lang_StackTraceElement_ != NULL);
1776 return java_lang_StackTraceElement_;
1777 }
1778
1779 static Class* java_lang_StackTraceElement_;
1780 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1781};
1782
Brian Carlstroma663ea52011-08-19 23:33:41 -07001783inline bool Object::IsString() const {
1784 // TODO use "klass_ == String::GetJavaLangString()" instead?
1785 return klass_ == klass_->descriptor_->klass_;
1786}
1787
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001788inline size_t Class::GetTypeSize(String* descriptor) {
1789 switch (descriptor->CharAt(0)) {
1790 case 'B': return 1; // byte
1791 case 'C': return 2; // char
1792 case 'D': return 8; // double
1793 case 'F': return 4; // float
1794 case 'I': return 4; // int
1795 case 'J': return 8; // long
1796 case 'S': return 2; // short
1797 case 'Z': return 1; // boolean
1798 case 'L': return sizeof(Object*);
1799 case '[': return sizeof(Array*);
1800 default:
1801 LOG(ERROR) << "Unknown type " << descriptor;
1802 return 0;
1803 }
1804}
1805
Brian Carlstromb63ec392011-08-27 17:38:27 -07001806inline bool Class::IsArrayClass() const {
1807 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001808}
1809
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001810class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001811 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001812 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001813 }
1814
Brian Carlstrom30b94452011-08-25 21:35:26 -07001815 Class* GetInterface() const {
1816 DCHECK(interface_ != NULL);
1817 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001818 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001819
Brian Carlstrom30b94452011-08-25 21:35:26 -07001820 void SetInterface(Class* interface) {
1821 DCHECK(interface->IsInterface());
1822 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001823 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001824
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001825 private:
1826 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001827 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001828
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001829 public: // TODO: private
1830 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001831 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001832 // this class.
1833 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001834
1835 private:
1836 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001837};
1838
1839} // namespace art
1840
1841#endif // ART_SRC_OBJECT_H_