blob: d299c5b28f6b31ac3590f032d36229a30ee1094c [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
Jesse Wilson35baaab2011-08-10 16:18:03 -0400387 public: // TODO: private
Brian Carlstrom4873d462011-08-21 15:23:39 -0700388
389 // private implemention of field access using raw data
390 uint32_t Get32(const Object* object) const;
391 void Set32(Object* object, uint32_t new_value) const;
392 uint64_t Get64(const Object* object) const;
393 void Set64(Object* object, uint64_t new_value) const;
394 Object* GetObj(const Object* object) const;
395 void SetObj(Object* object, Object* new_value) const;
396
Jesse Wilson35baaab2011-08-10 16:18:03 -0400397 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
398 // The class in which this field is declared.
399 Class* declaring_class_;
400 Object* generic_type_;
401 uint32_t generic_types_are_initialized_;
402 String* name_;
403 uint32_t offset_;
404 Class* type_;
405
406 // e.g. "I", "[C", "Landroid/os/Debug;"
407 StringPiece descriptor_;
408
409 uint32_t access_flags_;
410
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700411 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400412 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700413};
414
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400415class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700416 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700417 // An function that invokes a method with an array of its arguments.
418 typedef void InvokeStub(Method* method,
419 Object* obj,
420 Thread* thread,
421 byte* args,
422 JValue* result);
423
Brian Carlstromae3ac012011-07-27 01:30:28 -0700424 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700425 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700426 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700427 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700428 }
429
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700430 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700431 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700432 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700433 }
434
Brian Carlstroma0808032011-07-18 00:39:23 -0700435 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700436 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700437 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700438 }
439
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700440 static MemberOffset DeclaringClassOffset() {
441 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700442 }
443
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700444 // Returns true if the method is declared public.
445 bool IsPublic() const {
446 return (access_flags_ & kAccPublic) != 0;
447 }
448
449 // Returns true if the method is declared private.
450 bool IsPrivate() const {
451 return (access_flags_ & kAccPrivate) != 0;
452 }
453
454 // Returns true if the method is declared static.
455 bool IsStatic() const {
456 return (access_flags_ & kAccStatic) != 0;
457 }
458
459 // Returns true if the method is declared synchronized.
460 bool IsSynchronized() const {
461 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
462 return (access_flags_ & synchonized) != 0;
463 }
464
465 // Returns true if the method is declared final.
466 bool IsFinal() const {
467 return (access_flags_ & kAccFinal) != 0;
468 }
469
470 // Returns true if the method is declared native.
471 bool IsNative() const {
472 return (access_flags_ & kAccNative) != 0;
473 }
474
475 // Returns true if the method is declared abstract.
476 bool IsAbstract() const {
477 return (access_flags_ & kAccAbstract) != 0;
478 }
479
480 bool IsSynthetic() const {
481 return (access_flags_ & kAccSynthetic) != 0;
482 }
483
484 // Number of argument registers required by the prototype.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700485 uint32_t NumArgRegisters() const;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700487 // Number of argument bytes required for densely packing the
488 // arguments into an array of arguments.
Brian Carlstrom4873d462011-08-21 15:23:39 -0700489 size_t NumArgArrayBytes() const;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700490
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700491 // Converts a native PC to a virtual PC. TODO: this is a no-op
492 // until we associate a PC mapping table with each method.
493 uintptr_t ToDexPC(const uintptr_t pc) const {
494 return pc;
495 }
496
497 // Converts a virtual PC to a native PC. TODO: this is a no-op
498 // until we associate a PC mapping table with each method.
499 uintptr_t ToNativePC(const uintptr_t pc) const {
500 return pc;
501 }
502
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700503 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400504 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400505 // the class we are a part of
506 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400507 ObjectArray<Class>* java_exception_types_;
508 Object* java_formal_type_parameters_;
509 Object* java_generic_exception_types_;
510 Object* java_generic_parameter_types_;
511 Object* java_generic_return_type_;
512 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700513 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400514 ObjectArray<Class>* java_parameter_types_;
515 uint32_t java_generic_types_are_initialized_;
516 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700517
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700518 const StringPiece& GetShorty() const {
519 return shorty_;
520 }
521
Ian Rogersb033c752011-07-20 12:22:35 -0700522 bool IsReturnAReference() const {
523 return (shorty_[0] == 'L') || (shorty_[0] == '[');
524 }
525
526 bool IsReturnAFloatOrDouble() const {
527 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
528 }
529
530 bool IsReturnAFloat() const {
531 return shorty_[0] == 'F';
532 }
533
534 bool IsReturnADouble() const {
535 return shorty_[0] == 'D';
536 }
537
538 bool IsReturnALong() const {
539 return shorty_[0] == 'J';
540 }
541
Ian Rogers45a76cb2011-07-21 22:00:15 -0700542 bool IsReturnVoid() const {
543 return shorty_[0] == 'V';
544 }
545
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700546 bool IsDirect() const {
547 return is_direct_;
548 }
549
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700550 // "Args" may refer to any of the 3 levels of "Args."
551 // To avoid confusion, our code will denote which "Args" clearly:
552 // 1. UserArgs: Args that a user see.
553 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
554 // receiver.
555 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
556 // E.g., the first in Args is Method* for both static and non-static
557 // methods. And CConvArgs doesn't deal with the receiver because
558 // receiver is hardwired in an implicit register, so CConvArgs doesn't
559 // need to deal with it.
560 //
561 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700562 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700563 // "1 +" because the first in Args is the receiver.
564 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700565 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
566 }
567
568 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700569 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700570 size_t NumReferenceArgs() const;
571
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700572 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700573 size_t NumLongOrDoubleArgs() const;
574
575 // The number of reference arguments to this method before the given
576 // parameter index
577 size_t NumReferenceArgsBefore(unsigned int param) const;
578
579 // Is the given method parameter a reference?
580 bool IsParamAReference(unsigned int param) const;
581
582 // Is the given method parameter a long or double?
583 bool IsParamALongOrDouble(unsigned int param) const;
584
Ian Rogersdf20fe02011-07-20 20:34:16 -0700585 // Size in bytes of the given parameter
586 size_t ParamSize(unsigned int param) const;
587
588 // Size in bytes of the return value
589 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700590
buzbeec143c552011-08-20 17:38:58 -0700591 bool HasCode() {
592 return code_ != NULL;
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700593 }
594
Brian Carlstrom83db7722011-08-26 17:32:56 -0700595 const void* GetCode() {
596 return code_;
597 }
598
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700599 void SetCode(const byte* compiled_code,
600 size_t byte_count,
601 InstructionSet set) {
buzbeec143c552011-08-20 17:38:58 -0700602 // Copy the code into an executable region.
603 code_instruction_set_ = set;
604 code_area_.reset(MemMap::Map(byte_count,
605 PROT_READ | PROT_WRITE | PROT_EXEC));
Elliott Hughesedcc09c2011-08-21 18:47:05 -0700606 CHECK(code_area_.get());
buzbeec143c552011-08-20 17:38:58 -0700607 byte* code = code_area_->GetAddress();
608 memcpy(code, compiled_code, byte_count);
609 __builtin___clear_cache(code, code + byte_count);
610
611 uintptr_t address = reinterpret_cast<uintptr_t>(code);
612 if (code_instruction_set_ == kThumb2) {
613 // Set the low-order bit so a BLX will switch to Thumb mode
614 address |= 0x1;
615 }
616 code_ = reinterpret_cast<void*>(address);
617 }
618
Shih-wei Liaod11af152011-08-23 16:02:11 -0700619 void SetFrameSizeInBytes(size_t frame_size_in_bytes) {
620 frame_size_in_bytes_ = frame_size_in_bytes;
buzbeec143c552011-08-20 17:38:58 -0700621 }
622
Shih-wei Liaod11af152011-08-23 16:02:11 -0700623 void SetReturnPcOffsetInBytes(size_t return_pc_offset_in_bytes) {
624 return_pc_offset_in_bytes_ = return_pc_offset_in_bytes;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700625 }
626
Shih-wei Liaod11af152011-08-23 16:02:11 -0700627 size_t GetFrameSizeInBytes() const {
628 return frame_size_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700629 }
630
Shih-wei Liaod11af152011-08-23 16:02:11 -0700631 size_t GetReturnPcOffsetInBytes() const {
632 return return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700633 }
634
buzbeec143c552011-08-20 17:38:58 -0700635 void SetCoreSpillMask(uint32_t core_spill_mask) {
636 core_spill_mask_ = core_spill_mask;
Ian Rogersb033c752011-07-20 12:22:35 -0700637 }
638
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700639 static size_t GetCodeOffset() {
640 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700641 }
642
buzbeec143c552011-08-20 17:38:58 -0700643 void SetFpSpillMask(uint32_t fp_spill_mask) {
644 fp_spill_mask_ = fp_spill_mask;
645 }
646
Ian Rogersb033c752011-07-20 12:22:35 -0700647 void RegisterNative(const void* native_method) {
Elliott Hughes5174fe62011-08-23 15:12:35 -0700648 CHECK(native_method != NULL);
Ian Rogersb033c752011-07-20 12:22:35 -0700649 native_method_ = native_method;
650 }
651
Elliott Hughes5174fe62011-08-23 15:12:35 -0700652 void UnregisterNative() {
653 native_method_ = NULL;
654 }
655
Ian Rogersb033c752011-07-20 12:22:35 -0700656 static MemberOffset NativeMethodOffset() {
657 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
658 }
659
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700660 InvokeStub* GetInvokeStub() const {
661 return invoke_stub_;
662 }
663
664 void SetInvokeStub(const InvokeStub* invoke_stub) {
665 invoke_stub_ = invoke_stub;
666 }
667
668 static size_t GetInvokeStubOffset() {
669 return OFFSETOF_MEMBER(Method, invoke_stub_);
670 }
671
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700672 bool HasSameNameAndDescriptor(const Method* that) const;
673
Ian Rogersb033c752011-07-20 12:22:35 -0700674 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700675 // access flags; low 16 bits are defined by spec (could be uint16_t?)
676 uint32_t access_flags_;
677
678 // For concrete virtual methods, this is the offset of the method
Brian Carlstrom30b94452011-08-25 21:35:26 -0700679 // in Class::vtable_.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700680 //
681 // For abstract methods in an interface class, this is the offset
Brian Carlstrom30b94452011-08-25 21:35:26 -0700682 // of the method in "iftable_[n]->method_index_array_".
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700683 uint16_t method_index_;
684
685 // Method bounds; not needed for an abstract method.
686 //
687 // For a native method, we compute the size of the argument list, and
688 // set "insSize" and "registerSize" equal to it.
689 uint16_t num_registers_; // ins + locals
690 uint16_t num_outs_;
691 uint16_t num_ins_;
692
buzbeec143c552011-08-20 17:38:58 -0700693 // Total size in bytes of the frame
Shih-wei Liaod11af152011-08-23 16:02:11 -0700694 size_t frame_size_in_bytes_;
buzbeec143c552011-08-20 17:38:58 -0700695
696 // Architecture-dependent register spill masks
697 uint32_t core_spill_mask_;
698 uint32_t fp_spill_mask_;
699
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700700 // The method descriptor. This represents the parameters a method
701 // takes and value it returns. This string is a list of the type
702 // descriptors for the parameters enclosed in parenthesis followed
703 // by the return type descriptor. For example, for the method
704 //
705 // Object mymethod(int i, double d, Thread t)
706 //
707 // the method descriptor would be
708 //
709 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700710 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700711
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700712 // Method prototype descriptor string (return and argument types).
713 uint32_t proto_idx_;
714
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700715 // Offset to the CodeItem.
716 uint32_t code_off_;
717
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700718 // The short-form method descriptor string.
719 StringPiece shorty_;
720
Ian Rogers762400c2011-08-23 12:14:16 -0700721 // short cuts to declaring_class_->dex_cache_ members for fast compiled code
722 // access
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700723 ObjectArray<String>* dex_cache_strings_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700724 ObjectArray<Class>* dex_cache_resolved_types_;
725 ObjectArray<Method>* dex_cache_resolved_methods_;
726 ObjectArray<Field>* dex_cache_resolved_fields_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700727 CodeAndDirectMethods* dex_cache_code_and_direct_methods_;
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700728 ObjectArray<StaticStorageBase>* dex_cache_initialized_static_storage_;
Brian Carlstrom9cc262e2011-08-28 12:45:30 -0700729
730 bool is_direct_;
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700731
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700732 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700733 // Compiled code associated with this method
buzbeec143c552011-08-20 17:38:58 -0700734 scoped_ptr<MemMap> code_area_;
Brian Carlstrom83db7722011-08-26 17:32:56 -0700735 const void* code_;
736 // Instruction set of the compiled code
buzbeec143c552011-08-20 17:38:58 -0700737 InstructionSet code_instruction_set_;
738
739 // Size in bytes of compiled code associated with this method
740 const uint32_t code_size_;
Ian Rogersb033c752011-07-20 12:22:35 -0700741
Ian Rogers762400c2011-08-23 12:14:16 -0700742 // Offset of return PC within frame for compiled code (in bytes)
Shih-wei Liaod11af152011-08-23 16:02:11 -0700743 // Offset of PC within compiled code (in bytes)
744 size_t return_pc_offset_in_bytes_;
Shih-wei Liao1a18c8c2011-08-14 17:47:36 -0700745
Ian Rogersb033c752011-07-20 12:22:35 -0700746 // Any native method registered with this method
747 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700748
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700749 // Native invocation stub entry point.
750 const InvokeStub* invoke_stub_;
751
Carl Shapirof88c9522011-08-06 15:47:38 -0700752 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700753};
754
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700755class Array : public Object {
756 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700757 static size_t SizeOf(size_t component_count,
758 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700759 return sizeof(Array) + component_count * component_size;
760 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700761
Brian Carlstromb63ec392011-08-27 17:38:27 -0700762 // Given the context of a calling Method, use its DexCache to
763 // resolve a type to an array Class. If it cannot be resolved, throw
764 // an error. If it can, use it to create an array.
765 static Array* AllocFromCode(uint32_t type_idx, Method* method, int32_t component_count);
766
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700767 // A convenience for code that doesn't know the component size,
768 // and doesn't want to have to work it out itself.
Brian Carlstromb63ec392011-08-27 17:38:27 -0700769 static Array* Alloc(Class* array_class, int32_t component_count);
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700770
Brian Carlstromb63ec392011-08-27 17:38:27 -0700771 static Array* Alloc(Class* array_class, int32_t component_count, size_t component_size);
Carl Shapirof88c9522011-08-06 15:47:38 -0700772
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700773 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700774
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700775 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700776 return length_;
777 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700778
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700779 void SetLength(uint32_t length) {
780 length_ = length;
781 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700782
buzbeec143c552011-08-20 17:38:58 -0700783 static MemberOffset LengthOffset() {
784 return MemberOffset(OFFSETOF_MEMBER(Array, length_));
785 }
786
787 static MemberOffset DataOffset() {
788 return MemberOffset(OFFSETOF_MEMBER(Array, first_element_));
789 }
790
Elliott Hughes289da822011-08-16 10:11:20 -0700791 protected:
792 bool IsValidIndex(int32_t index) const {
793 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700794 Thread* self = Thread::Current();
795 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
796 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700797 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700798 }
799 return true;
800 }
801
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700802 private:
803 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700804 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400805 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
806 int32_t padding_;
buzbeec143c552011-08-20 17:38:58 -0700807 // Marker for the data (used by generated code)
808 uint32_t first_element_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700809
Carl Shapirof88c9522011-08-06 15:47:38 -0700810 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700811};
812
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700813template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700814class ObjectArray : public Array {
815 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700816 static ObjectArray<T>* Alloc(Class* object_array_class,
Brian Carlstromb63ec392011-08-27 17:38:27 -0700817 int32_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700818 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700819 }
820
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700821 T* const * GetData() const {
822 return reinterpret_cast<T* const *>(&elements_);
823 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400824
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700825 T** GetData() {
826 return reinterpret_cast<T**>(&elements_);
827 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400828
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700829 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700830 if (!IsValidIndex(i)) {
831 return NULL;
832 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700833 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700834 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700835
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700836 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700837 if (IsValidIndex(i)) {
838 // TODO: ArrayStoreException
839 GetData()[i] = object; // TODO: write barrier
840 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700841 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700842
843 static void Copy(ObjectArray<T>* src, int src_pos,
844 ObjectArray<T>* dst, int dst_pos,
845 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700846 for (size_t i = 0; i < length; i++) {
847 dst->Set(dst_pos + i, src->Get(src_pos + i));
848 }
849 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700850
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700851 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700852 ObjectArray<T>* new_array = Alloc(klass_, new_length);
853 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
854 return new_array;
855 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700856
857 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700858 // Location of first element.
859 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700860
861 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700862};
863
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700864// ClassLoader objects.
865class ClassLoader : public Object {
866 public:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700867 const std::vector<const DexFile*>& GetClassPath() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700868 return class_path_;
869 }
870 void SetClassPath(std::vector<const DexFile*>& class_path) {
871 DCHECK_EQ(0U, class_path_.size());
872 class_path_ = class_path;
873 }
874
875 private:
876 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
877 Object* packages_;
878 ClassLoader* parent_;
879
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700880 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700881 std::vector<const DexFile*> class_path_;
882
Carl Shapirof88c9522011-08-06 15:47:38 -0700883 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700884};
885
886class BaseDexClassLoader : public ClassLoader {
887 private:
888 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
889 String* original_path_;
890 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700891 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700892};
893
894class PathClassLoader : public BaseDexClassLoader {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700895 public:
896 static PathClassLoader* Alloc(std::vector<const DexFile*> dex_files);
897 static void SetClass(Class* dalvik_system_PathClassLoader);
898 static void ResetClass();
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700899 private:
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700900 static Class* dalvik_system_PathClassLoader_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700901 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700902};
903
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700904// Type for the InitializedStaticStorage table. Currently the Class
905// provides the static storage. However, this might change to improve
906// image sharing, so we use this type to avoid assumptions on the
907// current storage.
908class StaticStorageBase {};
909
Carl Shapiro1fb86202011-06-27 17:43:13 -0700910// Class objects.
Brian Carlstrom1caa2c22011-08-28 13:02:33 -0700911class Class : public Object, public StaticStorageBase {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700912 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700913
914 // Class Status
915 //
916 // kStatusNotReady: If a Class cannot be found in the class table by
917 // FindClass, it allocates an new one with AllocClass in the
918 // kStatusNotReady and calls LoadClass. Note if it does find a
919 // class, it may not be kStatusResolved and it will try to push it
920 // forward toward kStatusResolved.
921 //
922 // kStatusIdx: LoadClass populates with Class with information from
923 // the DexFile, moving the status to kStatusIdx, indicating that the
924 // Class values in super_class_ and interfaces_ have not been
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700925 // populated based on super_class_type_idx_ and
926 // interfaces_type_idx_. The new Class can then be inserted into the
927 // classes table.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700928 //
929 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
930 // attempt to move a kStatusIdx class forward to kStatusLoaded by
931 // using ResolveClass to initialize the super_class_ and interfaces_.
932 //
933 // kStatusResolved: Still holding the lock on Class, the ClassLinker
934 // will use LinkClass to link all members, creating Field and Method
935 // objects, setting up the vtable, etc. On success, the class is
936 // marked kStatusResolved.
937
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700938 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700939 kStatusError = -1,
940 kStatusNotReady = 0,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700941 kStatusIdx = 1, // loaded, DEX idx in super_class_type_idx_ and interfaces_type_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700942 kStatusLoaded = 2, // DEX idx values resolved
943 kStatusResolved = 3, // part of linking
944 kStatusVerifying = 4, // in the process of being verified
945 kStatusVerified = 5, // logically part of linking; done pre-init
946 kStatusInitializing = 6, // class init in progress
947 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700948 };
949
950 enum PrimitiveType {
951 kPrimNot = -1
952 };
953
Brian Carlstromb63ec392011-08-27 17:38:27 -0700954 // Given the context of a calling Method, use its DexCache to
955 // resolve a type to a Class. If it cannot be resolved, throw an
956 // error. If it can, use it to create an instance.
957 static Object* NewInstanceFromCode(uint32_t type_idx, Method* method);
958
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700959 Object* NewInstance() {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700960 DCHECK(!IsAbstract());
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700961 return Heap::AllocObject(this, this->object_size_);
962 }
963
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700964 Class* GetSuperClass() const {
965 return super_class_;
966 }
967
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700968 uint32_t GetSuperClassTypeIdx() const {
969 return super_class_type_idx_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970 }
971
972 bool HasSuperClass() const {
973 return super_class_ != NULL;
974 }
975
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700976 bool IsAssignableFrom(const Class* klass) const {
977 DCHECK(klass != NULL);
978 if (this == klass) {
979 return true;
980 }
981 if (IsInterface()) {
982 return klass->Implements(this);
983 }
Brian Carlstromb63ec392011-08-27 17:38:27 -0700984 if (klass->IsArrayClass()) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700985 return IsAssignableFromArray(klass);
986 }
987 return klass->IsSubClass(this);
988 }
989
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700990 const ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700991 return class_loader_;
992 }
993
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700994 DexCache* GetDexCache() const {
995 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700996 }
997
998 Class* GetComponentType() const {
999 return component_type_;
1000 }
1001
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001002 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001003
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001004 size_t GetComponentSize() const {
1005 return GetTypeSize(component_type_->descriptor_);
1006 }
1007
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001008 const String* GetDescriptor() const {
1009 DCHECK(descriptor_ != NULL);
1010 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001011 return descriptor_;
1012 }
1013
Brian Carlstrom4873d462011-08-21 15:23:39 -07001014 size_t SizeOf() const {
1015 return class_size_;
1016 }
1017
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001018 Status GetStatus() const {
1019 return status_;
1020 }
1021
1022 void SetStatus(Status new_status) {
1023 // TODO: validate transition
1024 status_ = new_status;
1025 }
1026
Carl Shapiro69759ea2011-07-21 18:13:35 -07001027 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001028 bool IsErroneous() const {
1029 return GetStatus() == kStatusError;
1030 }
1031
Carl Shapiro69759ea2011-07-21 18:13:35 -07001032 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 bool IsVerified() const {
1034 return GetStatus() >= kStatusVerified;
1035 }
1036
Carl Shapiro69759ea2011-07-21 18:13:35 -07001037 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001038 bool IsLinked() const {
1039 return GetStatus() >= kStatusResolved;
1040 }
1041
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001042 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -07001043 bool IsLoaded() const {
1044 return GetStatus() >= kStatusLoaded;
1045 }
1046
Carl Shapiro83ab4f32011-08-15 20:21:39 -07001047 // Returns true if the class is initialized.
1048 bool IsInitialized() const {
1049 return GetStatus() == kStatusInitialized;
1050 }
1051
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001052 // Returns true if this class is in the same packages as that class.
1053 bool IsInSamePackage(const Class* that) const;
1054
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001055 static bool IsInSamePackage(const String* descriptor1,
1056 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001057
1058 // Returns true if this class represents an array class.
Brian Carlstromb63ec392011-08-27 17:38:27 -07001059 bool IsArrayClass() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001060
1061 // Returns true if the class is an interface.
1062 bool IsInterface() const {
1063 return (access_flags_ & kAccInterface) != 0;
1064 }
1065
1066 // Returns true if the class is declared public.
1067 bool IsPublic() const {
1068 return (access_flags_ & kAccPublic) != 0;
1069 }
1070
1071 // Returns true if the class is declared final.
1072 bool IsFinal() const {
1073 return (access_flags_ & kAccFinal) != 0;
1074 }
1075
1076 // Returns true if the class is abstract.
1077 bool IsAbstract() const {
1078 return (access_flags_ & kAccAbstract) != 0;
1079 }
1080
1081 // Returns true if the class is an annotation.
1082 bool IsAnnotation() const {
1083 return (access_flags_ & kAccAnnotation) != 0;
1084 }
1085
1086 // Returns true if the class is a primitive type.
1087 bool IsPrimitive() const {
1088 return primitive_type_ != kPrimNot;
1089 }
1090
Brian Carlstromae3ac012011-07-27 01:30:28 -07001091 // Returns true if the class is synthetic.
1092 bool IsSynthetic() const {
1093 return (access_flags_ & kAccSynthetic) != 0;
1094 }
1095
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001096 // Returns true if this class can access that class.
1097 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001099 }
1100
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001101 // Returns the number of static, private, and constructor methods.
1102 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001103 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001104 }
1105
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001106 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001107 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001108 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001109 }
1110
1111 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001112 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001113 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001114 }
1115
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001116 Method* FindDeclaredDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001117 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001118
1119 Method* FindDirectMethod(const StringPiece& name,
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001120 const StringPiece& signature);
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001121
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001122 // Returns the number of non-inherited virtual methods.
1123 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001124 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001125 }
1126
1127 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001128 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001129 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001130 }
1131
1132 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001133 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001134 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001135 }
1136
Brian Carlstrom30b94452011-08-25 21:35:26 -07001137 // Given a method implemented by this class but potentially from a
1138 // super class, return the specific implementation
1139 // method for this class.
1140 Method* FindVirtualMethodForVirtual(Method* method) {
1141 DCHECK(!method->GetDeclaringClass()->IsInterface());
1142 // The argument method may from a super class.
1143 // Use the index to a potentially overriden one for this instance's class.
1144 return vtable_->Get(method->method_index_);
1145 }
1146
1147 // Given a method implemented by this class, but potentially from a
1148 // super class or interface, return the specific implementation
1149 // method for this class.
1150 Method* FindVirtualMethodForInterface(Method* method);
1151
1152 Method* FindVirtualMethodForVirtualOrInterface(Method* method) {
1153 if (method->GetDeclaringClass()->IsInterface()) {
1154 return FindVirtualMethodForInterface(method);
1155 }
1156 return FindVirtualMethodForVirtual(method);
Elliott Hughes72025e52011-08-23 17:50:30 -07001157 }
1158
Carl Shapiro419ec7b2011-08-03 14:48:33 -07001159 Method* FindDeclaredVirtualMethod(const StringPiece& name,
1160 const StringPiece& descriptor);
1161
1162 Method* FindVirtualMethod(const StringPiece& name,
1163 const StringPiece& descriptor);
1164
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001165 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001166 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001167 }
1168
Carl Shapiro69759ea2011-07-21 18:13:35 -07001169 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001170 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -07001171 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 }
1173
Brian Carlstrom4873d462011-08-21 15:23:39 -07001174 // Returns the number of static fields containing reference types.
1175 size_t NumReferenceStaticFields() const {
1176 return num_reference_static_fields_;
1177 }
1178
Elliott Hughescdf53122011-08-19 15:46:09 -07001179 // Finds the given instance field in this class or a superclass.
1180 Field* FindInstanceField(const StringPiece& name,
1181 const StringPiece& descriptor);
1182
1183 Field* FindDeclaredInstanceField(const StringPiece& name,
1184 const StringPiece& descriptor);
1185
1186 // Finds the given static field in this class or a superclass.
1187 Field* FindStaticField(const StringPiece& name,
1188 const StringPiece& descriptor);
1189
1190 Field* FindDeclaredStaticField(const StringPiece& name,
1191 const StringPiece& descriptor);
1192
Jesse Wilson35baaab2011-08-10 16:18:03 -04001193 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001194 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001195 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001196 }
1197
Jesse Wilson35baaab2011-08-10 16:18:03 -04001198 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001199 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001200 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001201 }
1202
1203 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001204 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001205 }
1206
Jesse Wilson35baaab2011-08-10 16:18:03 -04001207 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001208 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001209 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001210 }
1211
Jesse Wilson35baaab2011-08-10 16:18:03 -04001212 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001213 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001214 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001215 }
1216
Brian Carlstrom4873d462011-08-21 15:23:39 -07001217 uint32_t GetReferenceInstanceOffsets() const {
1218 return reference_instance_offsets_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001219 }
1220
Brian Carlstrom4873d462011-08-21 15:23:39 -07001221 void SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
1222 reference_instance_offsets_ = new_reference_offsets;
1223 }
1224
1225 uint32_t GetReferenceStaticOffsets() const {
1226 return reference_static_offsets_;
1227 }
1228
1229 void SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
1230 reference_static_offsets_ = new_reference_offsets;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001231 }
1232
Carl Shapiro69759ea2011-07-21 18:13:35 -07001233 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001234 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001235 }
1236
1237 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001238 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001239 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001240 }
1241
1242 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001243 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001244 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001245 }
1246
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001247 void SetVerifyErrorClass(Class* klass) {
1248 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1249 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1250 klass->SetFieldObject(field_offset, klass);
1251 }
1252
1253 private:
1254 bool Implements(const Class* klass) const;
1255 bool IsArrayAssignableFromArray(const Class* klass) const;
1256 bool IsAssignableFromArray(const Class* klass) const;
1257 bool IsSubClass(const Class* klass) const;
1258
Ian Rogersb033c752011-07-20 12:22:35 -07001259 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001260 // descriptor for the class such as "java.lang.Class" or "[C"
1261 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001262
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001263 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1264 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001265
1266 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001267 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001268
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001269 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001270 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001271 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001272
1273 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001274 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001275
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001276 // If class verify fails, we must return same error on subsequent tries.
1277 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1278 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001279
1280 // threadId, used to check for recursive <clinit> invocation
1281 uint32_t clinit_thread_id_;
1282
1283 // Total object size; used when allocating storage on gc heap. (For
1284 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001285 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001286
1287 // For array classes, the class object for base element, for
1288 // instanceof/checkcast (for String[][][], this will be String).
1289 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001290 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001291
1292 // For array classes, the number of array dimensions, e.g. int[][]
1293 // is 2. Otherwise 0.
1294 int32_t array_rank_;
1295
1296 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1297 PrimitiveType primitive_type_;
1298
1299 // The superclass, or NULL if this is java.lang.Object or a
1300 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001301 Class* super_class_; // TODO: make an instance field
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001302 uint32_t super_class_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001303
1304 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -07001305 const ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001306
1307 // initiating class loader list
1308 // NOTE: for classes with low serialNumber, these are unused, and the
1309 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001310 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001311
1312 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001313 ObjectArray<Class>* interfaces_;
Elliott Hughesc1674ed2011-08-25 18:09:09 -07001314 IntArray* interfaces_type_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001315
1316 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001317 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001318
1319 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001320 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001321
1322 // Virtual method table (vtable), for use by "invoke-virtual". The
1323 // vtable from the superclass is copied in, and virtual methods from
1324 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001325 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001326
Brian Carlstrom30b94452011-08-25 21:35:26 -07001327 // Interface table (iftable_), one entry per interface supported by
Carl Shapiro1fb86202011-06-27 17:43:13 -07001328 // this class. That means one entry for each interface we support
1329 // directly, indirectly via superclass, or indirectly via
1330 // superinterface. This will be null if neither we nor our
1331 // superclass implement any interfaces.
1332 //
1333 // Why we need this: given "class Foo implements Face", declare
1334 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1335 // is part of the Face interface. We can't easily use a single
1336 // vtable.
1337 //
1338 // For every interface a concrete class implements, we create a list
1339 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001340 size_t iftable_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001341 // TODO convert to ObjectArray<?>
Carl Shapiro1fb86202011-06-27 17:43:13 -07001342 InterfaceEntry* iftable_;
1343
1344 // The interface vtable indices for iftable get stored here. By
1345 // placing them all in a single pool for each class that implements
1346 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 size_t ifvi_pool_count_;
Brian Carlstrom30b94452011-08-25 21:35:26 -07001348 // TODO convert to IntArray
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001349 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001350
1351 // instance fields
1352 //
1353 // These describe the layout of the contents of a
1354 // DataObject-compatible Object. Note that only the fields directly
1355 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001356 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001357 //
1358 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001359 // the beginning of the field list. num_reference_instance_fields_
1360 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001361 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001362
Brian Carlstrom4873d462011-08-21 15:23:39 -07001363 // number of instance fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001364 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001365
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001366 // Bitmap of offsets of ifields.
Brian Carlstrom4873d462011-08-21 15:23:39 -07001367 uint32_t reference_instance_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001368
1369 // source file name, if known. Otherwise, NULL.
1370 const char* source_file_;
1371
Jesse Wilson7833bd22011-08-09 18:31:44 -04001372 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001373 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001374
Brian Carlstrom4873d462011-08-21 15:23:39 -07001375 // number of static fields that are object refs
1376 size_t num_reference_static_fields_;
1377
1378 // Bitmap of offsets of sfields.
1379 uint32_t reference_static_offsets_;
1380
1381 // Total class size; used when allocating storage on gc heap.
1382 size_t class_size_;
1383
1384 // Location of first static field.
1385 uint32_t fields_[0];
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001386
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001387 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001388 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001389};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001390std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001391
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001392inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001393 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001394 DCHECK(klass_ != NULL);
1395 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001396}
1397
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001398inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001399 Class* java_lang_Class = klass_->klass_;
1400 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001401}
1402
Brian Carlstrom4873d462011-08-21 15:23:39 -07001403inline bool Object::IsClassClass() const {
1404 Class* java_lang_Class = klass_->klass_;
1405 return this == java_lang_Class;
1406}
1407
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001408inline bool Object::IsObjectArray() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001409 return IsArrayInstance() && !klass_->component_type_->IsPrimitive();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001410}
1411
Brian Carlstromb63ec392011-08-27 17:38:27 -07001412inline bool Object::IsArrayInstance() const {
1413 return klass_->IsArrayClass();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001414}
1415
Brian Carlstroma663ea52011-08-19 23:33:41 -07001416inline bool Object::IsField() const {
1417 Class* java_lang_Class = klass_->klass_;
1418 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1419 return klass_ == java_lang_reflect_Field;
1420}
1421
1422inline bool Object::IsMethod() const {
1423 Class* java_lang_Class = klass_->klass_;
1424 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1425 return klass_ == java_lang_reflect_Method;
1426}
1427
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001428inline size_t Object::SizeOf() const {
Brian Carlstromb63ec392011-08-27 17:38:27 -07001429 if (IsArrayInstance()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001430 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001431 }
Brian Carlstrom4873d462011-08-21 15:23:39 -07001432 if (IsClass()) {
1433 return AsClass()->SizeOf();
1434 }
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001435 return klass_->object_size_;
1436}
1437
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001438inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001439 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001440}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001441
Brian Carlstrom4873d462011-08-21 15:23:39 -07001442class ClassClass : public Class {
1443 private:
1444 // Padding to ensure the 64-bit serialVersionUID_ begins on a 8-byte boundary
1445 int32_t padding_;
1446 int64_t serialVersionUID_;
1447 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassClass);
1448};
1449
1450class StringClass : public Class {
1451 private:
1452 CharArray* ASCII_;
1453 Object* CASE_INSENSITIVE_ORDER_;
1454 uint32_t REPLACEMENT_CHAR_;
1455 int64_t serialVersionUID;
1456 DISALLOW_IMPLICIT_CONSTRUCTORS(StringClass);
1457};
1458
1459class FieldClass : public Class {
1460 private:
1461 Object* ORDER_BY_NAME_AND_DECLARING_CLASS_;
1462 uint32_t TYPE_BOOLEAN_;
1463 uint32_t TYPE_BYTE_;
1464 uint32_t TYPE_CHAR_;
1465 uint32_t TYPE_DOUBLE_;
1466 uint32_t TYPE_FLOAT_;
1467 uint32_t TYPE_INTEGER_;
1468 uint32_t TYPE_LONG_;
1469 uint32_t TYPE_SHORT_;
1470 DISALLOW_IMPLICIT_CONSTRUCTORS(FieldClass);
1471};
1472
1473class MethodClass : public Class {
1474 private:
1475 int32_t DECLARED_;
1476 int32_t PUBLIC_;
1477 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodClass);
1478};
1479
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001480class DataObject : public Object {
1481 public:
Brian Carlstrom4873d462011-08-21 15:23:39 -07001482 // Location of first instance field.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001483 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001484 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001485 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001486};
1487
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001488template<class T>
1489class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001490 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001491 typedef T ElementType;
1492
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001493 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001494
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001495 const T* GetData() const {
1496 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001497 }
1498
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001499 T* GetData() {
1500 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001501 }
1502
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001503 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001504 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001505 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001506 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001507 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001508 }
1509
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001510 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001511 // TODO: ArrayStoreException
1512 if (IsValidIndex(i)) {
1513 GetData()[i] = value;
1514 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001515 }
1516
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001517 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001518 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001519 CHECK(array_class != NULL);
1520 array_class_ = array_class;
1521 }
1522
Brian Carlstroma663ea52011-08-19 23:33:41 -07001523 static void ResetArrayClass() {
1524 CHECK(array_class_ != NULL);
1525 array_class_ = NULL;
1526 }
1527
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001528 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001529 // Location of first element.
1530 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001531
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001532 static Class* array_class_;
1533
Carl Shapirof88c9522011-08-06 15:47:38 -07001534 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001535};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001536
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001537class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001538 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001539 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001540 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001541 return array_;
1542 }
1543
Carl Shapirof88c9522011-08-06 15:47:38 -07001544 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001545 return hash_code_;
1546 }
1547
Elliott Hughes814e4032011-08-23 12:07:56 -07001548 int32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001549 return offset_;
1550 }
1551
Elliott Hughes814e4032011-08-23 12:07:56 -07001552 int32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001553 return count_;
1554 }
1555
Elliott Hughes814e4032011-08-23 12:07:56 -07001556 int32_t GetUtfLength() const {
1557 return CountUtf8Bytes(array_->GetData(), count_);
1558 }
1559
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001560 // TODO: do we need this? Equals is the only caller, and could
1561 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001562 uint16_t CharAt(int32_t index) const {
1563 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001564 Thread* self = Thread::Current();
1565 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1566 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001567 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001568 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001569 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001570 }
1571
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001572 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001573 const uint16_t* utf16_data_in,
Elliott Hughes814e4032011-08-23 12:07:56 -07001574 int32_t hash_code = 0) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001575 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001576 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001577 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001578 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001579 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001580 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001581 if (hash_code != 0) {
1582 string->hash_code_ = hash_code;
1583 } else {
1584 string->ComputeHashCode();
1585 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001586 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001587 }
1588
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001589 static String* AllocFromModifiedUtf8(const char* utf) {
Elliott Hughes814e4032011-08-23 12:07:56 -07001590 size_t char_count = CountModifiedUtf8Chars(utf);
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001591 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001592 }
1593
Jesse Wilson8989d992011-08-02 13:39:42 -07001594 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1595 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001596 String* string = Alloc(GetJavaLangString(), utf16_length);
1597 uint16_t* utf16_data_out = string->array_->GetData();
1598 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1599 string->ComputeHashCode();
1600 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001601 }
1602
Brian Carlstroma663ea52011-08-19 23:33:41 -07001603 static void SetClass(Class* java_lang_String);
1604 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001605
Elliott Hughes814e4032011-08-23 12:07:56 -07001606 static String* Alloc(Class* java_lang_String, int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001607 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1608 }
1609
Elliott Hughes814e4032011-08-23 12:07:56 -07001610 static String* Alloc(Class* java_lang_String, CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001611 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001612 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001613 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001614 return string;
1615 }
1616
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001617 void ComputeHashCode() {
1618 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1619 }
1620
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001621 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001622 bool Equals(const char* modified_utf8) const {
Elliott Hughes814e4032011-08-23 12:07:56 -07001623 for (int32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001624 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1625 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001626 return false;
1627 }
1628 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001629 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001630 }
1631
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001632 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001633 bool Equals(const StringPiece& modified_utf8) const {
1634 // TODO: do not assume C-string representation.
1635 return Equals(modified_utf8.data());
1636 }
1637
1638 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001639 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001640 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001641 return false;
1642 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001643 for (int32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001644 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001645 return false;
1646 }
1647 }
1648 return true;
1649 }
1650
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001651 // TODO: do we need this overload? give it a more intention-revealing name.
Elliott Hughes814e4032011-08-23 12:07:56 -07001652 bool Equals(const uint16_t* that_chars, int32_t that_offset, int32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001653 if (this->GetLength() != that_length) {
1654 return false;
1655 }
Elliott Hughes814e4032011-08-23 12:07:56 -07001656 for (int32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001657 if (this->CharAt(i) != that_chars[that_offset + i]) {
1658 return false;
1659 }
1660 }
1661 return true;
1662 }
1663
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001664 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1665 std::string ToModifiedUtf8() const {
Elliott Hughesb465ab02011-08-24 11:21:21 -07001666 uint16_t* chars = array_->GetData() + offset_;
1667 size_t byte_count(CountUtf8Bytes(chars, count_));
1668 std::string result(byte_count, char(0));
1669 ConvertUtf16ToModifiedUtf8(&result[0], chars, count_);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001670 return result;
1671 }
1672
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001673 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001674 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1675 CharArray* array_;
1676
Carl Shapirof88c9522011-08-06 15:47:38 -07001677 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001678
Elliott Hughes289da822011-08-16 10:11:20 -07001679 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001680
Elliott Hughes289da822011-08-16 10:11:20 -07001681 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001682
1683 static Class* GetJavaLangString() {
1684 DCHECK(java_lang_String_ != NULL);
1685 return java_lang_String_;
1686 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001687
1688 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001689
1690 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001691};
1692
Elliott Hughese5b0dc82011-08-23 09:59:02 -07001693class Throwable : public Object {
1694 private:
1695 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1696 Throwable* cause_;
1697 String* detail_message_;
1698 Object* stack_state_; // Note this is Java volatile:
1699 Object* stack_trace_;
1700 Object* suppressed_exceptions_;
1701
1702 DISALLOW_IMPLICIT_CONSTRUCTORS(Throwable);
1703};
1704
Shih-wei Liao55df06b2011-08-26 14:39:27 -07001705class StackTraceElement : public Object {
1706 public:
1707 const String* GetDeclaringClass() const {
1708 return declaring_class_;
1709 }
1710
1711 const String* GetMethodName() const {
1712 return method_name_;
1713 }
1714
1715 const String* GetFileName() const {
1716 return file_name_;
1717 }
1718
1719 uint32_t GetLineNumber() const {
1720 return line_number_;
1721 }
1722
1723 static StackTraceElement* Alloc(const String* declaring_class, const String* method_name,
1724 const String* file_name, uint32_t line_number) {
1725 StackTraceElement* trace = down_cast<StackTraceElement*>(GetStackTraceElement()->NewInstance());
1726 trace->declaring_class_ = declaring_class;
1727 trace->method_name_ = method_name;
1728 trace->file_name_ = file_name;
1729 trace->line_number_ = line_number;
1730 return trace;
1731 }
1732
1733 static void SetClass(Class* java_lang_StackTraceElement);
1734
1735 static void ResetClass();
1736
1737 private:
1738 const String* declaring_class_;
1739 const String* method_name_;
1740 const String* file_name_;
1741 uint32_t line_number_;
1742
1743 static Class* GetStackTraceElement() {
1744 DCHECK(java_lang_StackTraceElement_ != NULL);
1745 return java_lang_StackTraceElement_;
1746 }
1747
1748 static Class* java_lang_StackTraceElement_;
1749 DISALLOW_IMPLICIT_CONSTRUCTORS(StackTraceElement);
1750};
1751
Brian Carlstroma663ea52011-08-19 23:33:41 -07001752inline bool Object::IsString() const {
1753 // TODO use "klass_ == String::GetJavaLangString()" instead?
1754 return klass_ == klass_->descriptor_->klass_;
1755}
1756
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001757inline size_t Class::GetTypeSize(String* descriptor) {
1758 switch (descriptor->CharAt(0)) {
1759 case 'B': return 1; // byte
1760 case 'C': return 2; // char
1761 case 'D': return 8; // double
1762 case 'F': return 4; // float
1763 case 'I': return 4; // int
1764 case 'J': return 8; // long
1765 case 'S': return 2; // short
1766 case 'Z': return 1; // boolean
1767 case 'L': return sizeof(Object*);
1768 case '[': return sizeof(Array*);
1769 default:
1770 LOG(ERROR) << "Unknown type " << descriptor;
1771 return 0;
1772 }
1773}
1774
Brian Carlstromb63ec392011-08-27 17:38:27 -07001775inline bool Class::IsArrayClass() const {
1776 return array_rank_ != 0;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001777}
1778
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001779class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001780 public:
Brian Carlstrom30b94452011-08-25 21:35:26 -07001781 InterfaceEntry() : interface_(NULL), method_index_array_(NULL) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001782 }
1783
Brian Carlstrom30b94452011-08-25 21:35:26 -07001784 Class* GetInterface() const {
1785 DCHECK(interface_ != NULL);
1786 return interface_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001787 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001788
Brian Carlstrom30b94452011-08-25 21:35:26 -07001789 void SetInterface(Class* interface) {
1790 DCHECK(interface->IsInterface());
1791 interface_ = interface;
Carl Shapirof88c9522011-08-06 15:47:38 -07001792 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001793
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001794 private:
1795 // Points to the interface class.
Brian Carlstrom30b94452011-08-25 21:35:26 -07001796 Class* interface_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001797
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001798 public: // TODO: private
1799 // Index into array of vtable offsets. This points into the
Brian Carlstrom30b94452011-08-25 21:35:26 -07001800 // ifvi_pool_, which holds the vtables for all interfaces declared by
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001801 // this class.
1802 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001803
1804 private:
1805 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001806};
1807
1808} // namespace art
1809
1810#endif // ART_SRC_OBJECT_H_