blob: 1df10ad97defa28153d69db53fbb22466d8e1473 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "casts.h"
Elliott Hughes814e4032011-08-23 12:07:56 -07007#include "constants.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07008#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
11#include "macros.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070012#include "monitor.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "offsets.h"
14#include "stringpiece.h"
Elliott Hughes814e4032011-08-23 12:07:56 -070015#include "utf.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17namespace art {
18
19class Array;
20class Class;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -070021class CodeAndDirectMethods;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070022class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040023class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070024class InterfaceEntry;
25class Monitor;
26class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070027class Object;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -070028class StaticStorageBase;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040029class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070030template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070031template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070032typedef PrimitiveArray<uint8_t> BooleanArray;
33typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070034typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070035typedef PrimitiveArray<double> DoubleArray;
36typedef PrimitiveArray<float> FloatArray;
37typedef PrimitiveArray<int32_t> IntArray;
38typedef PrimitiveArray<int64_t> LongArray;
39typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070040
Carl Shapiro3ee755d2011-06-28 12:11:04 -070041union JValue {
42 uint8_t z;
43 int8_t b;
44 uint16_t c;
45 int16_t s;
46 int32_t i;
47 int64_t j;
48 float f;
49 double d;
50 Object* l;
51};
52
Brian Carlstrombe977852011-07-19 14:54:54 -070053static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
54static const uint32_t kAccPrivate = 0x0002; // field, method, ic
55static const uint32_t kAccProtected = 0x0004; // field, method, ic
56static const uint32_t kAccStatic = 0x0008; // field, method, ic
57static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
58static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
59static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
60static const uint32_t kAccVolatile = 0x0040; // field
61static const uint32_t kAccBridge = 0x0040; // method (1.5)
62static const uint32_t kAccTransient = 0x0080; // field
63static const uint32_t kAccVarargs = 0x0080; // method (1.5)
64static const uint32_t kAccNative = 0x0100; // method
65static const uint32_t kAccInterface = 0x0200; // class, ic
66static const uint32_t kAccAbstract = 0x0400; // class, method, ic
67static const uint32_t kAccStrict = 0x0800; // method
68static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
69static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
70static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072static const uint32_t kAccMiranda = 0x8000; // method
73
Brian Carlstroma331b3c2011-07-18 17:47:56 -070074static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
75
Brian Carlstrombe977852011-07-19 14:54:54 -070076static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
77static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070078
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070079/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070080 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070081 */
82/*
83 * A magic value for refOffsets. Ignore the bits and walk the super
84 * chain when this is the value.
85 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
86 * fields followed by 2 ref instance fields.]
87 */
88#define CLASS_WALK_SUPER ((unsigned int)(3))
89#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
90#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
91#define CLASS_OFFSET_ALIGNMENT 4
92#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
93/*
94 * Given an offset, return the bit number which would encode that offset.
95 * Local use only.
96 */
97#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
98 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
99 CLASS_OFFSET_ALIGNMENT)
100/*
101 * Is the given offset too large to be encoded?
102 */
103#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
104 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
105/*
106 * Return a single bit, encoding the offset.
107 * Undefined if the offset is too large, as defined above.
108 */
109#define CLASS_BIT_FROM_OFFSET(byteOffset) \
110 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
111/*
112 * Return an offset, given a bit number as returned from CLZ.
113 */
114#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700115 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116
117
Carl Shapiro1fb86202011-06-27 17:43:13 -0700118class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700119 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700120 static bool InstanceOf(const Object* object, const Class* klass) {
121 if (object == NULL) {
122 return false;
123 }
124 return object->InstanceOf(klass);
125 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700126
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700128 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700129 return klass_;
130 }
131
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700132 bool InstanceOf(const Class* klass) const;
133
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700134 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700135
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700136 void MonitorEnter() {
137 monitor_->Enter();
138 }
139
140 void MonitorExit() {
141 monitor_->Exit();
142 }
143
144 void Notify() {
145 monitor_->Notify();
146 }
147
148 void NotifyAll() {
149 monitor_->NotifyAll();
150 }
151
152 void Wait() {
153 monitor_->Wait();
154 }
155
156 void Wait(int64_t timeout) {
157 monitor_->Wait(timeout);
158 }
159
160 void Wait(int64_t timeout, int32_t nanos) {
161 monitor_->Wait(timeout, nanos);
162 }
163
Brian Carlstrom4873d462011-08-21 15:23:39 -0700164 Object* GetFieldObject(size_t field_offset) const {
165 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
166 return *reinterpret_cast<Object* const *>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167 }
168
169 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700170 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
171 *reinterpret_cast<Object**>(raw_addr) = new_value;
172 // TODO: write barrier
173 }
174
Brian Carlstrom4873d462011-08-21 15:23:39 -0700175 uint32_t GetField32(size_t field_offset) const {
176 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
177 return *reinterpret_cast<const uint32_t*>(raw_addr);
178 }
179
180 void SetField32(size_t offset, uint32_t new_value) {
181 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
182 *reinterpret_cast<uint32_t*>(raw_addr) = new_value;
183 }
184
185 uint64_t GetField64(size_t field_offset) const {
186 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
187 return *reinterpret_cast<const uint64_t*>(raw_addr);
188 }
189
190 void SetField64(size_t offset, uint64_t new_value) {
191 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
192 *reinterpret_cast<uint64_t*>(raw_addr) = new_value;
193 }
194
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700195 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196
197 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700198 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 return down_cast<Class*>(this);
200 }
201
202 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700203 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700204 return down_cast<const Class*>(this);
205 }
206
Brian Carlstrom4873d462011-08-21 15:23:39 -0700207 bool IsClassClass() const;
208
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700209 bool IsObjectArray() const;
210
211 template<class T>
212 ObjectArray<T>* AsObjectArray() {
213 DCHECK(IsObjectArray());
214 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215 }
216
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700217 template<class T>
218 const ObjectArray<T>* AsObjectArray() const {
219 DCHECK(IsObjectArray());
220 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221 }
222
223 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700224 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 return true;
226 }
227
228 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700229 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700230 return true;
231 }
232
233 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700234 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 return true;
236 }
237
238 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700239 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 return true;
241 }
242
243 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700244 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700245 return true;
246 }
247
Brian Carlstromb63ec392011-08-27 17:38:27 -0700248 bool IsArrayInstance() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700249
250 Array* AsArray() {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700251 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700252 return down_cast<Array*>(this);
253 }
254
255 const Array* AsArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -0700256 DCHECK(IsArrayInstance());
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700257 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258 }
259
Brian Carlstroma663ea52011-08-19 23:33:41 -0700260 bool IsString() const;
261
262 String* AsString() {
263 DCHECK(IsString());
264 return down_cast<String*>(this);
265 }
266
267 bool IsMethod() const;
268
269 Method* AsMethod() {
270 DCHECK(IsMethod());
271 return down_cast<Method*>(this);
272 }
273
Brian Carlstrom4873d462011-08-21 15:23:39 -0700274 const Method* AsMethod() const {
275 DCHECK(IsMethod());
276 return down_cast<const Method*>(this);
277 }
278
Brian Carlstroma663ea52011-08-19 23:33:41 -0700279 bool IsField() const;
280
281 Field* AsField() {
282 DCHECK(IsField());
283 return down_cast<Field*>(this);
284 }
285
Brian Carlstrom4873d462011-08-21 15:23:39 -0700286 const Field* AsField() const {
287 DCHECK(IsField());
288 return down_cast<const Field*>(this);
289 }
290
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700291 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700292 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700293
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700294 Monitor* monitor_;
295
296 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700297 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700298};
299
300class ObjectLock {
301 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700302 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700303 CHECK(object != NULL);
304 obj_->MonitorEnter();
305 }
306
307 ~ObjectLock() {
308 obj_->MonitorExit();
309 }
310
311 void Wait(int64_t millis = 0) {
312 return obj_->Wait(millis);
313 }
314
315 void Notify() {
316 obj_->Notify();
317 }
318
319 void NotifyAll() {
320 obj_->NotifyAll();
321 }
322
323 private:
324 Object* obj_;
325 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700326};
327
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400328class AccessibleObject : public Object {
329 private:
330 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
331 uint32_t java_flag_;
332};
333
334class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700335 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700336 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700337 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700338 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700339 }
340
Jesse Wilson14150742011-07-29 19:04:44 -0400341 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700342 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700343 return name_;
344 }
345
346 bool IsStatic() const {
347 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700348 }
349
350 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700351 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700352 }
353
Brian Carlstromae3ac012011-07-27 01:30:28 -0700354 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700355 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700356 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700357 }
358
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700359 uint32_t GetOffset() const {
360 return offset_;
361 }
362
363 void SetOffset(size_t num_bytes) {
364 offset_ = num_bytes;
365 }
366
Brian Carlstrom4873d462011-08-21 15:23:39 -0700367 // field access, null object for static fields
368 bool GetBoolean(const Object* object) const;
369 void SetBoolean(Object* object, bool z) const;
370 int8_t GetByte(const Object* object) const;
371 void SetByte(Object* object, int8_t b) const;
372 uint16_t GetChar(const Object* object) const;
373 void SetChar(Object* object, uint16_t c) const;
374 uint16_t GetShort(const Object* object) const;
375 void SetShort(Object* object, uint16_t s) const;
376 int32_t GetInt(const Object* object) const;
377 void SetInt(Object* object, int32_t i) const;
378 int64_t GetLong(const Object* object) const;
379 void SetLong(Object* object, int64_t j) const;
380 float GetFloat(const Object* object) const;
381 void SetFloat(Object* object, float f) const;
382 double GetDouble(const Object* object) const;
383 void SetDouble(Object* object, double d) const;
384 Object* GetObject(const Object* object) const;
385 void SetObject(Object* object, Object* l) const;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700386
Brian Carlstromb9edb842011-08-28 16:31:06 -0700387 // slow path routines for static field access when field was unresolved at compile time
388 static uint32_t Get32StaticFromCode(uint32_t field_idx, const Method* referrer);
389 static void Set32StaticFromCode(uint32_t field_idx, const Method* referrer, uint32_t new_value);
390 static uint64_t Get64StaticFromCode(uint32_t field_idx, const Method* referrer);
391 static void Set64StaticFromCode(uint32_t field_idx, const Method* referrer, uint64_t new_value);
392 static Object* GetObjStaticFromCode(uint32_t field_idx, const Method* referrer);
393 static void SetObjStaticFromCode(uint32_t field_idx, const Method* referrer, Object* new_value);
394
Jesse Wilson35baaab2011-08-10 16:18:03 -0400395 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700396
397 // private implemention of field access using raw data
398 uint32_t Get32(const Object* object) const;
399 void Set32(Object* object, uint32_t new_value) const;
400 uint64_t Get64(const Object* object) const;
401 void Set64(Object* object, uint64_t new_value) const;
402 Object* GetObj(const Object* object) const;
403 void SetObj(Object* object, Object* new_value) const;
404
Jesse Wilson35baaab2011-08-10 16:18:03 -0400405 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
406 // The class in which this field is declared.
407 Class* declaring_class_;
408 Object* generic_type_;
409 uint32_t generic_types_are_initialized_;
410 String* name_;
411 uint32_t offset_;
412 Class* type_;
413
414 // e.g. "I", "[C", "Landroid/os/Debug;"
415 StringPiece descriptor_;
416
417 uint32_t access_flags_;
418
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700419 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400420 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700421};
422
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400423class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700424 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700425 // An function that invokes a method with an array of its arguments.
426 typedef void InvokeStub(Method* method,
427 Object* obj,
428 Thread* thread,
429 byte* args,
430 JValue* result);
431
Brian Carlstromae3ac012011-07-27 01:30:28 -0700432 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700433 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700434 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700435 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700436 }
437
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700438 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700439 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700440 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700441 }
442
Brian Carlstroma0808032011-07-18 00:39:23 -0700443 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700444 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700445 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700446 }
447
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700448 static MemberOffset DeclaringClassOffset() {
449 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700450 }
451
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700452 // Returns true if the method is declared public.
453 bool IsPublic() const {
454 return (access_flags_ & kAccPublic) != 0;
455 }
456
457 // Returns true if the method is declared private.
458 bool IsPrivate() const {
459 return (access_flags_ & kAccPrivate) != 0;
460 }
461
462 // Returns true if the method is declared static.
463 bool IsStatic() const {
464 return (access_flags_ & kAccStatic) != 0;
465 }
466
467 // Returns true if the method is declared synchronized.
468 bool IsSynchronized() const {
469 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
470 return (access_flags_ & synchonized) != 0;
471 }
472
473 // Returns true if the method is declared final.
474 bool IsFinal() const {
475 return (access_flags_ & kAccFinal) != 0;
476 }
477
478 // Returns true if the method is declared native.
479 bool IsNative() const {
480 return (access_flags_ & kAccNative) != 0;
481 }
482
483 // Returns true if the method is declared abstract.
484 bool IsAbstract() const {
485 return (access_flags_ & kAccAbstract) != 0;
486 }
487
488 bool IsSynthetic() const {
489 return (access_flags_ & kAccSynthetic) != 0;
490 }
491
492 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700493 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700494
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700495 // Number of argument bytes required for densely packing the
496 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700497 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700498
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700499 // Converts a native PC to a virtual PC. TODO: this is a no-op
500 // until we associate a PC mapping table with each method.
501 uintptr_t ToDexPC(const uintptr_t pc) const {
502 return pc;
503 }
504
505 // Converts a virtual PC to a native PC. TODO: this is a no-op
506 // until we associate a PC mapping table with each method.
507 uintptr_t ToNativePC(const uintptr_t pc) const {
508 return pc;
509 }
510
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700511 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400512 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400513 // the class we are a part of
514 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400515 ObjectArray<Class>* java_exception_types_;
516 Object* java_formal_type_parameters_;
517 Object* java_generic_exception_types_;
518 Object* java_generic_parameter_types_;
519 Object* java_generic_return_type_;
520 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700521 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400522 ObjectArray<Class>* java_parameter_types_;
523 uint32_t java_generic_types_are_initialized_;
524 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700525
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700526 const StringPiece& GetShorty() const {
527 return shorty_;
528 }
529
Ian Rogersb033c752011-07-20 12:22:35 -0700530 bool IsReturnAReference() const {
531 return (shorty_[0] == 'L') || (shorty_[0] == '[');
532 }
533
534 bool IsReturnAFloatOrDouble() const {
535 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
536 }
537
538 bool IsReturnAFloat() const {
539 return shorty_[0] == 'F';
540 }
541
542 bool IsReturnADouble() const {
543 return shorty_[0] == 'D';
544 }
545
546 bool IsReturnALong() const {
547 return shorty_[0] == 'J';
548 }
549
Ian Rogers45a76cb2011-07-21 22:00:15 -0700550 bool IsReturnVoid() const {
551 return shorty_[0] == 'V';
552 }
553
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700554 bool IsDirect() const {
555 return is_direct_;
556 }
557
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700558 // "Args" may refer to any of the 3 levels of "Args."
559 // To avoid confusion, our code will denote which "Args" clearly:
560 // 1. UserArgs: Args that a user see.
561 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
562 // receiver.
563 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
564 // E.g., the first in Args is Method* for both static and non-static
565 // methods. And CConvArgs doesn't deal with the receiver because
566 // receiver is hardwired in an implicit register, so CConvArgs doesn't
567 // need to deal with it.
568 //
569 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700570 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700571 // "1 +" because the first in Args is the receiver.
572 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700573 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
574 }
575
576 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700577 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700578 size_t NumReferenceArgs() const;
579
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700580 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700581 size_t NumLongOrDoubleArgs() const;
582
583 // The number of reference arguments to this method before the given
584 // parameter index
585 size_t NumReferenceArgsBefore(unsigned int param) const;
586
587 // Is the given method parameter a reference?
588 bool IsParamAReference(unsigned int param) const;
589
590 // Is the given method parameter a long or double?
591 bool IsParamALongOrDouble(unsigned int param) const;
592
Ian Rogersdf20fe02011-07-20 20:34:16 -0700593 // Size in bytes of the given parameter
594 size_t ParamSize(unsigned int param) const;
595
596 // Size in bytes of the return value
597 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700598
buzbeec143c552011-08-20 17:38:58 -0700599 bool HasCode() {
600 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700601 }
602
Brian Carlstrom83db7722011-08-26 17:32:56 -0700603 const void* GetCode() {
604 return code_;
605 }
606
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700607 void SetCode(const byte* compiled_code,
608 size_t byte_count,
609 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700610 // Copy the code into an executable region.
611 code_instruction_set_ = set;
612 code_area_.reset(MemMap::Map(byte_count,
613 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700614 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700615 byte* code = code_area_->GetAddress();
616 memcpy(code, compiled_code, byte_count);
617 __builtin___clear_cache(code, code + byte_count);
618
619 uintptr_t address = reinterpret_cast<uintptr_t>(code);
620 if (code_instruction_set_ == kThumb2) {
621 // Set the low-order bit so a BLX will switch to Thumb mode
622 address |= 0x1;
623 }
624 code_ = reinterpret_cast<void*>(address);
625 }
626
Shih-wei Liaod11af152011-08-23 16:02:11 -0700627 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
628 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700629 }
630
Shih-wei Liaod11af152011-08-23 16:02:11 -0700631 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
632 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700633 }
634
Shih-wei Liaod11af152011-08-23 16:02:11 -0700635 size_t GetFrameSizeInBytes() const {
636 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700637 }
638
Shih-wei Liaod11af152011-08-23 16:02:11 -0700639 size_t GetReturnPcOffsetInBytes() const {
640 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700641 }
642
buzbeec143c552011-08-20 17:38:58 -0700643 void SetCoreSpillMask(uint32_t core_spill_mask) {
644 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700645 }
646
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700647 static size_t GetCodeOffset() {
648 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700649 }
650
buzbeec143c552011-08-20 17:38:58 -0700651 void SetFpSpillMask(uint32_t fp_spill_mask) {
652 fp_spill_mask_ = fp_spill_mask;
653 }
654
Ian Rogersb033c752011-07-20 12:22:35 -0700655 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700656 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700657 native_method_ = native_method;
658 }
659
Elliott Hughes5174fe62011-08-23 15:12:35 -0700660 void UnregisterNative() {
661 native_method_ = NULL;
662 }
663
Ian Rogersb033c752011-07-20 12:22:35 -0700664 static MemberOffset NativeMethodOffset() {
665 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
666 }
667
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700668 InvokeStub* GetInvokeStub() const {
669 return invoke_stub_;
670 }
671
672 void SetInvokeStub(const InvokeStub* invoke_stub) {
673 invoke_stub_ = invoke_stub;
674 }
675
676 static size_t GetInvokeStubOffset() {
677 return OFFSETOF_MEMBER(Method, invoke_stub_);
678 }
679
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700680 bool HasSameNameAndDescriptor(const Method* that) const;
681
Ian Rogersb033c752011-07-20 12:22:35 -0700682 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700683 // access flags; low 16 bits are defined by spec (could be uint16_t?)
684 uint32_t access_flags_;
685
686 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700687 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700688 //
689 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700690 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700691 uint16_t method_index_;
692
693 // Method bounds; not needed for an abstract method.
694 //
695 // For a native method, we compute the size of the argument list, and
696 // set "insSize" and "registerSize" equal to it.
697 uint16_t num_registers_; // ins + locals
698 uint16_t num_outs_;
699 uint16_t num_ins_;
700
buzbeec143c552011-08-20 17:38:58 -0700701 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700702 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700703
704 // Architecture-dependent register spill masks
705 uint32_t core_spill_mask_;
706 uint32_t fp_spill_mask_;
707
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700708 // The method descriptor. This represents the parameters a method
709 // takes and value it returns. This string is a list of the type
710 // descriptors for the parameters enclosed in parenthesis followed
711 // by the return type descriptor. For example, for the method
712 //
713 // Object mymethod(int i, double d, Thread t)
714 //
715 // the method descriptor would be
716 //
717 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700718 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700719
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 // Method prototype descriptor string (return and argument types).
721 uint32_t proto_idx_;
722
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700723 // Offset to the CodeItem.
724 uint32_t code_off_;
725
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 // The short-form method descriptor string.
727 StringPiece shorty_;
728
Ian Rogers762400c2011-08-23 12:14:16 -0700729 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
730 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700731 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700732 ObjectArray<Class>* dex_cache_resolved_types_;
733 ObjectArray<Method>* dex_cache_resolved_methods_;
734 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700735 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700736 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700737
738 bool is_direct_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700739
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700740 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700741 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700742 scoped_ptr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700743 const void* code_;
744 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700745 InstructionSet code_instruction_set_;
746
747 // Size in bytes of compiled code associated with this method
748 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700749
Ian Rogers762400c2011-08-23 12:14:16 -0700750 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700751 // Offset of PC within compiled code (in bytes)
752 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700753
Ian Rogersb033c752011-07-20 12:22:35 -0700754 // Any native method registered with this method
755 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700756
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700757 // Native invocation stub entry point.
758 const InvokeStub* invoke_stub_;
759
Carl Shapirof88c9522011-08-06 15:47:38 -0700760 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700761};
762
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700763class Array : public Object {
764 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700765 static size_t SizeOf(size_t component_count,
766 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700767 return sizeof(Array) + component_count * component_size;
768 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700769
Brian Carlstromb63ec392011-08-27 17:38:27 -0700770 // Given the context of a calling Method, use its DexCache to
771 // resolve a type to an array Class. If it cannot be resolved, throw
772 // an error. If it can, use it to create an array.
773 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
774
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700775 // A convenience for code that doesn't know the component size,
776 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700777 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700778
Brian Carlstromb63ec392011-08-27 17:38:27 -0700779 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700780
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700781 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700782
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700783 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700784 return length_;
785 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700786
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700787 void SetLength(uint32_t length) {
788 length_ = length;
789 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700790
buzbeec143c552011-08-20 17:38:58 -0700791 static MemberOffset LengthOffset() {
792 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
793 }
794
795 static MemberOffset DataOffset() {
796 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
797 }
798
Elliott Hughes289da822011-08-16 10:11:20 -0700799 protected:
800 bool IsValidIndex(int32_t index) const {
801 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700802 Thread* self = Thread::Current();
803 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
804 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700805 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700806 }
807 return true;
808 }
809
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700810 private:
811 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700812 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400813 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
814 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700815 // Marker for the data (used by generated code)
816 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700817
Carl Shapirof88c9522011-08-06 15:47:38 -0700818 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700819};
820
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700821template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700822class ObjectArray : public Array {
823 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700824 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700825 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700826 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700827 }
828
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700829 T* const * GetData() const {
830 return reinterpret_cast<T* const *>(&elements_);
831 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400832
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700833 T** GetData() {
834 return reinterpret_cast<T**>(&elements_);
835 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400836
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700837 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700838 if (!IsValidIndex(i)) {
839 return NULL;
840 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700841 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700842 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700843
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700844 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700845 if (IsValidIndex(i)) {
846 // TODO: ArrayStoreException
847 GetData()[i] = object; // TODO: write barrier
848 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700849 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700850
851 static void Copy(ObjectArray<T>* src, int src_pos,
852 ObjectArray<T>* dst, int dst_pos,
853 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700854 for (size_t i = 0; i < length; i++) {
855 dst->Set(dst_pos + i, src->Get(src_pos + i));
856 }
857 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700858
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700859 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700860 ObjectArray<T>* new_array = Alloc(klass_, new_length);
861 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
862 return new_array;
863 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700864
865 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700866 // Location of first element.
867 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700868
869 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700870};
871
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700872// ClassLoader objects.
873class ClassLoader : public Object {
874 public:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700875 const std::vector<const DexFile*>& GetClassPath() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700876 return class_path_;
877 }
878 void SetClassPath(std::vector<const DexFile*>& class_path) {
879 DCHECK_EQ(0U, class_path_.size());
880 class_path_ = class_path;
881 }
882
883 private:
884 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
885 Object* packages_;
886 ClassLoader* parent_;
887
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700888 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700889 std::vector<const DexFile*> class_path_;
890
Carl Shapirof88c9522011-08-06 15:47:38 -0700891 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700892};
893
894class BaseDexClassLoader : public ClassLoader {
895 private:
896 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
897 String* original_path_;
898 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700899 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700900};
901
902class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700903 public:
904 static PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
905 static void SetClass(Class* dalvik_system_PathClassLoader);
906 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700907 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700908 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700909 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700910};
911
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700912// Type for the InitializedStaticStorage table. Currently the Class
913// provides the static storage. However, this might change to improve
914// image sharing, so we use this type to avoid assumptions on the
915// current storage.
916class StaticStorageBase {};
917
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918// Class objects.
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700919class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700920 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700921
922 // Class Status
923 //
924 // kStatusNotReady: If a Class cannot be found in the class table by
925 // FindClass, it allocates an new one with AllocClass in the
926 // kStatusNotReady and calls LoadClass. Note if it does find a
927 // class, it may not be kStatusResolved and it will try to push it
928 // forward toward kStatusResolved.
929 //
930 // kStatusIdx: LoadClass populates with Class with information from
931 // the DexFile, moving the status to kStatusIdx, indicating that the
932 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700933 // populated based on super_class_type_idx_ and
934 // interfaces_type_idx_. The new Class can then be inserted into the
935 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700936 //
937 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
938 // attempt to move a kStatusIdx class forward to kStatusLoaded by
939 // using ResolveClass to initialize the super_class_ and interfaces_.
940 //
941 // kStatusResolved: Still holding the lock on Class, the ClassLinker
942 // will use LinkClass to link all members, creating Field and Method
943 // objects, setting up the vtable, etc. On success, the class is
944 // marked kStatusResolved.
945
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700947 kStatusError = -1,
948 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700949 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700950 kStatusLoaded = 2, // DEX idx values resolved
951 kStatusResolved = 3, // part of linking
952 kStatusVerifying = 4, // in the process of being verified
953 kStatusVerified = 5, // logically part of linking; done pre-init
954 kStatusInitializing = 6, // class init in progress
955 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700956 };
957
958 enum PrimitiveType {
959 kPrimNot = -1
960 };
961
Brian Carlstromb63ec392011-08-27 17:38:27 -0700962 // Given the context of a calling Method, use its DexCache to
963 // resolve a type to a Class. If it cannot be resolved, throw an
964 // error. If it can, use it to create an instance.
965 static Object* NewInstanceFromCode(uint32_t type_idx, Method* method);
966
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700967 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700968 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700969 return Heap::AllocObject(this, this->object_size_);
970 }
971
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 Class* GetSuperClass() const {
973 return super_class_;
974 }
975
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700976 uint32_t GetSuperClassTypeIdx() const {
977 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978 }
979
980 bool HasSuperClass() const {
981 return super_class_ != NULL;
982 }
983
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700984 bool IsAssignableFrom(const Class* klass) const {
985 DCHECK(klass != NULL);
986 if (this == klass) {
987 return true;
988 }
989 if (IsInterface()) {
990 return klass->Implements(this);
991 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700992 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700993 return IsAssignableFromArray(klass);
994 }
995 return klass->IsSubClass(this);
996 }
997
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700998 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700999 return class_loader_;
1000 }
1001
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001002 DexCache* GetDexCache() const {
1003 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001004 }
1005
1006 Class* GetComponentType() const {
1007 return component_type_;
1008 }
1009
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001010 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001011
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001012 size_t GetComponentSize() const {
1013 return GetTypeSize(component_type_->descriptor_);
1014 }
1015
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001016 const String* GetDescriptor() const {
1017 DCHECK(descriptor_ != NULL);
1018 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 return descriptor_;
1020 }
1021
Brian Carlstrom4873d462011-08-21 15:23:39 -07001022 size_t SizeOf() const {
1023 return class_size_;
1024 }
1025
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 Status GetStatus() const {
1027 return status_;
1028 }
1029
1030 void SetStatus(Status new_status) {
1031 // TODO: validate transition
1032 status_ = new_status;
1033 }
1034
Carl Shapiro69759ea2011-07-21 18:13:35 -07001035 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 bool IsErroneous() const {
1037 return GetStatus() == kStatusError;
1038 }
1039
Carl Shapiro69759ea2011-07-21 18:13:35 -07001040 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001041 bool IsVerified() const {
1042 return GetStatus() >= kStatusVerified;
1043 }
1044
Carl Shapiro69759ea2011-07-21 18:13:35 -07001045 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001046 bool IsLinked() const {
1047 return GetStatus() >= kStatusResolved;
1048 }
1049
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001050 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001051 bool IsLoaded() const {
1052 return GetStatus() >= kStatusLoaded;
1053 }
1054
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001055 // Returns true if the class is initialized.
1056 bool IsInitialized() const {
1057 return GetStatus() == kStatusInitialized;
1058 }
1059
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001060 // Returns true if this class is in the same packages as that class.
1061 bool IsInSamePackage(const Class* that) const;
1062
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001063 static bool IsInSamePackage(const String* descriptor1,
1064 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001065
1066 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001067 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001068
1069 // Returns true if the class is an interface.
1070 bool IsInterface() const {
1071 return (access_flags_ & kAccInterface) != 0;
1072 }
1073
1074 // Returns true if the class is declared public.
1075 bool IsPublic() const {
1076 return (access_flags_ & kAccPublic) != 0;
1077 }
1078
1079 // Returns true if the class is declared final.
1080 bool IsFinal() const {
1081 return (access_flags_ & kAccFinal) != 0;
1082 }
1083
1084 // Returns true if the class is abstract.
1085 bool IsAbstract() const {
1086 return (access_flags_ & kAccAbstract) != 0;
1087 }
1088
1089 // Returns true if the class is an annotation.
1090 bool IsAnnotation() const {
1091 return (access_flags_ & kAccAnnotation) != 0;
1092 }
1093
1094 // Returns true if the class is a primitive type.
1095 bool IsPrimitive() const {
1096 return primitive_type_ != kPrimNot;
1097 }
1098
Brian Carlstromae3ac012011-07-27 01:30:28 -07001099 // Returns true if the class is synthetic.
1100 bool IsSynthetic() const {
1101 return (access_flags_ & kAccSynthetic) != 0;
1102 }
1103
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001104 // Returns true if this class can access that class.
1105 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001106 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001107 }
1108
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 // Returns the number of static, private, and constructor methods.
1110 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001111 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001112 }
1113
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001114 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001115 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001116 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001117 }
1118
1119 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001120 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001121 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001122 }
1123
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001124 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001125 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001126
1127 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001128 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001129
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001130 // Returns the number of non-inherited virtual methods.
1131 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001132 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001133 }
1134
1135 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001136 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001137 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001138 }
1139
1140 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001141 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001142 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001143 }
1144
Brian Carlstrom30b94452011-08-25 21:35:26 -07001145 // Given a method implemented by this class but potentially from a
1146 // super class, return the specific implementation
1147 // method for this class.
1148 Method* FindVirtualMethodForVirtual(Method* method) {
1149 DCHECK(!method->GetDeclaringClass()->IsInterface());
1150 // The argument method may from a super class.
1151 // Use the index to a potentially overriden one for this instance's class.
1152 return vtable_->Get(method->method_index_);
1153 }
1154
1155 // Given a method implemented by this class, but potentially from a
1156 // super class or interface, return the specific implementation
1157 // method for this class.
1158 Method* FindVirtualMethodForInterface(Method* method);
1159
1160 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1161 if (method->GetDeclaringClass()->IsInterface()) {
1162 return FindVirtualMethodForInterface(method);
1163 }
1164 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001165 }
1166
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001167 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1168 const StringPiece& descriptor);
1169
1170 Method* FindVirtualMethod(const StringPiece& name,
1171 const StringPiece& descriptor);
1172
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001173 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001174 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001175 }
1176
Carl Shapiro69759ea2011-07-21 18:13:35 -07001177 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001178 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001179 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001180 }
1181
Brian Carlstrom4873d462011-08-21 15:23:39 -07001182 // Returns the number of static fields containing reference types.
1183 size_t NumReferenceStaticFields() const {
1184 return num_reference_static_fields_;
1185 }
1186
Elliott Hughescdf53122011-08-19 15:46:09 -07001187 // Finds the given instance field in this class or a superclass.
1188 Field* FindInstanceField(const StringPiece& name,
1189 const StringPiece& descriptor);
1190
1191 Field* FindDeclaredInstanceField(const StringPiece& name,
1192 const StringPiece& descriptor);
1193
1194 // Finds the given static field in this class or a superclass.
1195 Field* FindStaticField(const StringPiece& name,
1196 const StringPiece& descriptor);
1197
1198 Field* FindDeclaredStaticField(const StringPiece& name,
1199 const StringPiece& descriptor);
1200
Jesse Wilson35baaab2011-08-10 16:18:03 -04001201 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001202 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001203 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001204 }
1205
Jesse Wilson35baaab2011-08-10 16:18:03 -04001206 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001207 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001208 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001209 }
1210
1211 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001212 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001213 }
1214
Jesse Wilson35baaab2011-08-10 16:18:03 -04001215 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001216 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001217 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001218 }
1219
Jesse Wilson35baaab2011-08-10 16:18:03 -04001220 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001221 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001222 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001223 }
1224
Brian Carlstrom4873d462011-08-21 15:23:39 -07001225 uint32_t GetReferenceInstanceOffsets() const {
1226 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001227 }
1228
Brian Carlstrom4873d462011-08-21 15:23:39 -07001229 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1230 reference_instance_offsets_ = new_reference_offsets;
1231 }
1232
1233 uint32_t GetReferenceStaticOffsets() const {
1234 return reference_static_offsets_;
1235 }
1236
1237 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1238 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001239 }
1240
Carl Shapiro69759ea2011-07-21 18:13:35 -07001241 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001242 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001243 }
1244
1245 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001246 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001247 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001248 }
1249
1250 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001251 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001252 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001253 }
1254
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001255 void SetVerifyErrorClass(Class* klass) {
1256 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1257 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1258 klass->SetFieldObject(field_offset, klass);
1259 }
1260
1261 private:
1262 bool Implements(const Class* klass) const;
1263 bool IsArrayAssignableFromArray(const Class* klass) const;
1264 bool IsAssignableFromArray(const Class* klass) const;
1265 bool IsSubClass(const Class* klass) const;
1266
Ian Rogersb033c752011-07-20 12:22:35 -07001267 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001268 // descriptor for the class such as "java.lang.Class" or "[C"
1269 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001270
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001271 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1272 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001273
1274 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001275 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001276
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001277 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001278 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001279 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001280
1281 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001282 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001283
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001284 // If class verify fails, we must return same error on subsequent tries.
1285 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1286 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001287
1288 // threadId, used to check for recursive <clinit> invocation
1289 uint32_t clinit_thread_id_;
1290
1291 // Total object size; used when allocating storage on gc heap. (For
1292 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001293 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001294
1295 // For array classes, the class object for base element, for
1296 // instanceof/checkcast (for String[][][], this will be String).
1297 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001298 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001299
1300 // For array classes, the number of array dimensions, e.g. int[][]
1301 // is 2. Otherwise 0.
1302 int32_t array_rank_;
1303
1304 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1305 PrimitiveType primitive_type_;
1306
1307 // The superclass, or NULL if this is java.lang.Object or a
1308 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001309 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001310 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001311
1312 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001313 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001314
1315 // initiating class loader list
1316 // NOTE: for classes with low serialNumber, these are unused, and the
1317 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001318 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001319
1320 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001321 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001322 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001323
1324 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001325 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001326
1327 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001328 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001329
1330 // Virtual method table (vtable), for use by "invoke-virtual". The
1331 // vtable from the superclass is copied in, and virtual methods from
1332 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001333 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001334
Brian Carlstrom30b94452011-08-25 21:35:26 -07001335 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001336 // this class. That means one entry for each interface we support
1337 // directly, indirectly via superclass, or indirectly via
1338 // superinterface. This will be null if neither we nor our
1339 // superclass implement any interfaces.
1340 //
1341 // Why we need this: given "class Foo implements Face", declare
1342 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1343 // is part of the Face interface. We can't easily use a single
1344 // vtable.
1345 //
1346 // For every interface a concrete class implements, we create a list
1347 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001348 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001349 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001350 InterfaceEntry* iftable_;
1351
1352 // The interface vtable indices for iftable get stored here. By
1353 // placing them all in a single pool for each class that implements
1354 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001356 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001357 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001358
1359 // instance fields
1360 //
1361 // These describe the layout of the contents of a
1362 // DataObject-compatible Object. Note that only the fields directly
1363 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001364 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001365 //
1366 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001367 // the beginning of the field list. num_reference_instance_fields_
1368 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001369 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001370
Brian Carlstrom4873d462011-08-21 15:23:39 -07001371 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001372 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001373
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001374 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001375 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001376
1377 // source file name, if known. Otherwise, NULL.
1378 const char* source_file_;
1379
Jesse Wilson7833bd22011-08-09 18:31:44 -04001380 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001381 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001382
Brian Carlstrom4873d462011-08-21 15:23:39 -07001383 // number of static fields that are object refs
1384 size_t num_reference_static_fields_;
1385
1386 // Bitmap of offsets of sfields.
1387 uint32_t reference_static_offsets_;
1388
1389 // Total class size; used when allocating storage on gc heap.
1390 size_t class_size_;
1391
1392 // Location of first static field.
1393 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001394
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001395 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001396 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001397};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001398std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001399
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001400inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001401 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001402 DCHECK(klass_ != NULL);
1403 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001404}
1405
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001406inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001407 Class* java_lang_Class = klass_->klass_;
1408 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001409}
1410
Brian Carlstrom4873d462011-08-21 15:23:39 -07001411inline bool Object::IsClassClass() const {
1412 Class* java_lang_Class = klass_->klass_;
1413 return this == java_lang_Class;
1414}
1415
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001416inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001417 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001418}
1419
Brian Carlstromb63ec392011-08-27 17:38:27 -07001420inline bool Object::IsArrayInstance() const {
1421 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001422}
1423
Brian Carlstroma663ea52011-08-19 23:33:41 -07001424inline bool Object::IsField() const {
1425 Class* java_lang_Class = klass_->klass_;
1426 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1427 return klass_ == java_lang_reflect_Field;
1428}
1429
1430inline bool Object::IsMethod() const {
1431 Class* java_lang_Class = klass_->klass_;
1432 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1433 return klass_ == java_lang_reflect_Method;
1434}
1435
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001436inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001437 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001438 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001439 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001440 if (IsClass()) {
1441 return AsClass()->SizeOf();
1442 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001443 return klass_->object_size_;
1444}
1445
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001446inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001447 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001448}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001449
Brian Carlstrom4873d462011-08-21 15:23:39 -07001450class ClassClass : public Class {
1451 private:
1452 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1453 int32_t padding_;
1454 int64_t serialVersionUID_;
1455 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1456};
1457
1458class StringClass : public Class {
1459 private:
1460 CharArray* ASCII_;
1461 Object* CASE_INSENSITIVE_ORDER_;
1462 uint32_t REPLACEMENT_CHAR_;
1463 int64_t serialVersionUID;
1464 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1465};
1466
1467class FieldClass : public Class {
1468 private:
1469 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1470 uint32_t TYPE_BOOLEAN_;
1471 uint32_t TYPE_BYTE_;
1472 uint32_t TYPE_CHAR_;
1473 uint32_t TYPE_DOUBLE_;
1474 uint32_t TYPE_FLOAT_;
1475 uint32_t TYPE_INTEGER_;
1476 uint32_t TYPE_LONG_;
1477 uint32_t TYPE_SHORT_;
1478 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1479};
1480
1481class MethodClass : public Class {
1482 private:
1483 int32_t DECLARED_;
1484 int32_t PUBLIC_;
1485 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1486};
1487
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001488class DataObject : public Object {
1489 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001490 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001491 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001492 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001493 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001494};
1495
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001496template<class T>
1497class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001498 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001499 typedef T ElementType;
1500
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001501 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001502
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001503 const T* GetData() const {
1504 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001505 }
1506
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001507 T* GetData() {
1508 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001509 }
1510
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001511 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001512 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001513 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001514 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001515 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001516 }
1517
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001518 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001519 // TODO: ArrayStoreException
1520 if (IsValidIndex(i)) {
1521 GetData()[i] = value;
1522 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001523 }
1524
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001525 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001526 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001527 CHECK(array_class != NULL);
1528 array_class_ = array_class;
1529 }
1530
Brian Carlstroma663ea52011-08-19 23:33:41 -07001531 static void ResetArrayClass() {
1532 CHECK(array_class_ != NULL);
1533 array_class_ = NULL;
1534 }
1535
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001536 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001537 // Location of first element.
1538 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001539
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001540 static Class* array_class_;
1541
Carl Shapirof88c9522011-08-06 15:47:38 -07001542 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001543};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001545class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001546 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001547 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001548 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001549 return array_;
1550 }
1551
Carl Shapirof88c9522011-08-06 15:47:38 -07001552 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001553 return hash_code_;
1554 }
1555
Elliott Hughes814e4032011-08-23 12:07:56 -07001556 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001557 return offset_;
1558 }
1559
Elliott Hughes814e4032011-08-23 12:07:56 -07001560 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001561 return count_;
1562 }
1563
Elliott Hughes814e4032011-08-23 12:07:56 -07001564 int32_t GetUtfLength() const {
1565 return CountUtf8Bytes(array_->GetData(), count_);
1566 }
1567
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001568 // TODO: do we need this? Equals is the only caller, and could
1569 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001570 uint16_t CharAt(int32_t index) const {
1571 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001572 Thread* self = Thread::Current();
1573 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1574 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001575 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001576 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001577 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001578 }
1579
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001580 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001581 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001582 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001583 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001584 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001585 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001586 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001587 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001588 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001589 if (hash_code != 0) {
1590 string->hash_code_ = hash_code;
1591 } else {
1592 string->ComputeHashCode();
1593 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001594 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001595 }
1596
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001597 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001598 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001599 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001600 }
1601
Jesse Wilson8989d992011-08-02 13:39:42 -07001602 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1603 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001604 String* string = Alloc(GetJavaLangString(), utf16_length);
1605 uint16_t* utf16_data_out = string->array_->GetData();
1606 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1607 string->ComputeHashCode();
1608 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001609 }
1610
Brian Carlstroma663ea52011-08-19 23:33:41 -07001611 static void SetClass(Class* java_lang_String);
1612 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001613
Elliott Hughes814e4032011-08-23 12:07:56 -07001614 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001615 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1616 }
1617
Elliott Hughes814e4032011-08-23 12:07:56 -07001618 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001619 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001620 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001621 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001622 return string;
1623 }
1624
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001625 void ComputeHashCode() {
1626 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1627 }
1628
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001629 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001630 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001631 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001632 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1633 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001634 return false;
1635 }
1636 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001637 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001638 }
1639
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001640 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001641 bool Equals(const StringPiece& modified_utf8) const {
1642 // TODO: do not assume C-string representation.
1643 return Equals(modified_utf8.data());
1644 }
1645
1646 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001647 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001648 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001649 return false;
1650 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001651 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001652 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001653 return false;
1654 }
1655 }
1656 return true;
1657 }
1658
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001659 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001660 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001661 if (this->GetLength() != that_length) {
1662 return false;
1663 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001664 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001665 if (this->CharAt(i) != that_chars[that_offset + i]) {
1666 return false;
1667 }
1668 }
1669 return true;
1670 }
1671
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001672 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1673 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001674 uint16_t* chars = array_->GetData() + offset_;
1675 size_t byte_count(CountUtf8Bytes(chars, count_));
1676 std::string result(byte_count, char(0));
1677 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001678 return result;
1679 }
1680
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001681 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001682 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1683 CharArray* array_;
1684
Carl Shapirof88c9522011-08-06 15:47:38 -07001685 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001686
Elliott Hughes289da822011-08-16 10:11:20 -07001687 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001688
Elliott Hughes289da822011-08-16 10:11:20 -07001689 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001690
1691 static Class* GetJavaLangString() {
1692 DCHECK(java_lang_String_ != NULL);
1693 return java_lang_String_;
1694 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001695
1696 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001697
1698 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001699};
1700
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001701class Throwable : public Object {
1702 private:
1703 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1704 Throwable* cause_;
1705 String* detail_message_;
1706 Object* stack_state_; // Note this is Java volatile:
1707 Object* stack_trace_;
1708 Object* suppressed_exceptions_;
1709
1710 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1711};
1712
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001713class StackTraceElement : public Object {
1714 public:
1715 const String* GetDeclaringClass() const {
1716 return declaring_class_;
1717 }
1718
1719 const String* GetMethodName() const {
1720 return method_name_;
1721 }
1722
1723 const String* GetFileName() const {
1724 return file_name_;
1725 }
1726
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001727 int32_t GetLineNumber() const {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001728 return line_number_;
1729 }
1730
1731 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001732 const String* file_name, int32_t line_number) {
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001733 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1734 trace->declaring_class_ = declaring_class;
1735 trace->method_name_ = method_name;
1736 trace->file_name_ = file_name;
1737 trace->line_number_ = line_number;
1738 return trace;
1739 }
1740
1741 static void SetClass(Class* java_lang_StackTraceElement);
1742
1743 static void ResetClass();
1744
1745 private:
1746 const String* declaring_class_;
1747 const String* method_name_;
1748 const String* file_name_;
Shih-wei Liaoff0f9be2011-08-29 15:43:53 -07001749 int32_t line_number_;
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001750
1751 static Class* GetStackTraceElement() {
1752 DCHECK(java_lang_StackTraceElement_ != NULL);
1753 return java_lang_StackTraceElement_;
1754 }
1755
1756 static Class* java_lang_StackTraceElement_;
1757 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1758};
1759
Brian Carlstroma663ea52011-08-19 23:33:41 -07001760inline bool Object::IsString() const {
1761 // TODO use "klass_ == String::GetJavaLangString()" instead?
1762 return klass_ == klass_->descriptor_->klass_;
1763}
1764
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001765inline size_t Class::GetTypeSize(String* descriptor) {
1766 switch (descriptor->CharAt(0)) {
1767 case 'B': return 1; // byte
1768 case 'C': return 2; // char
1769 case 'D': return 8; // double
1770 case 'F': return 4; // float
1771 case 'I': return 4; // int
1772 case 'J': return 8; // long
1773 case 'S': return 2; // short
1774 case 'Z': return 1; // boolean
1775 case 'L': return sizeof(Object*);
1776 case '[': return sizeof(Array*);
1777 default:
1778 LOG(ERROR) << "Unknown type " << descriptor;
1779 return 0;
1780 }
1781}
1782
Brian Carlstromb63ec392011-08-27 17:38:27 -07001783inline bool Class::IsArrayClass() const {
1784 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001785}
1786
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001787class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001788 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001789 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001790 }
1791
Brian Carlstrom30b94452011-08-25 21:35:26 -07001792 Class* GetInterface() const {
1793 DCHECK(interface_ != NULL);
1794 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001795 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001796
Brian Carlstrom30b94452011-08-25 21:35:26 -07001797 void SetInterface(Class* interface) {
1798 DCHECK(interface->IsInterface());
1799 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001800 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001801
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001802 private:
1803 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001804 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001805
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001806 public: // TODO: private
1807 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001808 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001809 // this class.
1810 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001811
1812 private:
1813 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001814};
1815
1816} // namespace art
1817
1818#endif // ART_SRC_OBJECT_H_