blob: e071b97e6b308330b5cf2b31e7ac68e40c2861b1 [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 "constants.h"
7#include "casts.h"
8#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"
12#include "offsets.h"
13#include "stringpiece.h"
14#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070015
16namespace art {
17
18class Array;
19class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070020class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040021class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class InterfaceEntry;
23class Monitor;
24class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040026class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070027template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070028template<class T> class PrimitiveArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070029typedef PrimitiveArray<uint16_t> CharArray;
30typedef PrimitiveArray<uint32_t> IntArray;
31typedef PrimitiveArray<uint64_t> LongArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070032
Carl Shapiro3ee755d2011-06-28 12:11:04 -070033union JValue {
34 uint8_t z;
35 int8_t b;
36 uint16_t c;
37 int16_t s;
38 int32_t i;
39 int64_t j;
40 float f;
41 double d;
42 Object* l;
43};
44
Brian Carlstrombe977852011-07-19 14:54:54 -070045static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
46static const uint32_t kAccPrivate = 0x0002; // field, method, ic
47static const uint32_t kAccProtected = 0x0004; // field, method, ic
48static const uint32_t kAccStatic = 0x0008; // field, method, ic
49static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
50static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
51static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
52static const uint32_t kAccVolatile = 0x0040; // field
53static const uint32_t kAccBridge = 0x0040; // method (1.5)
54static const uint32_t kAccTransient = 0x0080; // field
55static const uint32_t kAccVarargs = 0x0080; // method (1.5)
56static const uint32_t kAccNative = 0x0100; // method
57static const uint32_t kAccInterface = 0x0200; // class, ic
58static const uint32_t kAccAbstract = 0x0400; // class, method, ic
59static const uint32_t kAccStrict = 0x0800; // method
60static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
61static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
62static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070064static const uint32_t kAccMiranda = 0x8000; // method
65
Brian Carlstroma331b3c2011-07-18 17:47:56 -070066static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
67
Brian Carlstrombe977852011-07-19 14:54:54 -070068static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
69static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070070
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070071/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070072 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070073 */
74/*
75 * A magic value for refOffsets. Ignore the bits and walk the super
76 * chain when this is the value.
77 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
78 * fields followed by 2 ref instance fields.]
79 */
80#define CLASS_WALK_SUPER ((unsigned int)(3))
81#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
82#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
83#define CLASS_OFFSET_ALIGNMENT 4
84#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
85/*
86 * Given an offset, return the bit number which would encode that offset.
87 * Local use only.
88 */
89#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
90 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
91 CLASS_OFFSET_ALIGNMENT)
92/*
93 * Is the given offset too large to be encoded?
94 */
95#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
96 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
97/*
98 * Return a single bit, encoding the offset.
99 * Undefined if the offset is too large, as defined above.
100 */
101#define CLASS_BIT_FROM_OFFSET(byteOffset) \
102 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
103/*
104 * Return an offset, given a bit number as returned from CLZ.
105 */
106#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700107 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700108
109
Carl Shapiro1fb86202011-06-27 17:43:13 -0700110class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700111 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700112 static bool InstanceOf(const Object* object, const Class* klass) {
113 if (object == NULL) {
114 return false;
115 }
116 return object->InstanceOf(klass);
117 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700118
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700119 Class* GetClass() const {
120 return klass_;
121 }
122
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700123 bool InstanceOf(const Class* klass) const;
124
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700125 void MonitorEnter() {
126 monitor_->Enter();
127 }
128
129 void MonitorExit() {
130 monitor_->Exit();
131 }
132
133 void Notify() {
134 monitor_->Notify();
135 }
136
137 void NotifyAll() {
138 monitor_->NotifyAll();
139 }
140
141 void Wait() {
142 monitor_->Wait();
143 }
144
145 void Wait(int64_t timeout) {
146 monitor_->Wait(timeout);
147 }
148
149 void Wait(int64_t timeout, int32_t nanos) {
150 monitor_->Wait(timeout, nanos);
151 }
152
Carl Shapiro69759ea2011-07-21 18:13:35 -0700153 const Object* GetFieldObject(size_t field_offset) const {
154 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
155 return *reinterpret_cast<Object* const*>(raw_addr);
156 }
157
158 Object* GetFieldObject(size_t field_offset) {
159 return const_cast<Object*>(GetFieldObject(field_offset));
160 }
161
162 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700163 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
164 *reinterpret_cast<Object**>(raw_addr) = new_value;
165 // TODO: write barrier
166 }
167
Carl Shapiro69759ea2011-07-21 18:13:35 -0700168 bool IsClass() const {
169 LOG(FATAL) << "Unimplemented";
170 return true;
171 }
172
173 Class* AsClass() {
174 return down_cast<Class*>(this);
175 }
176
177 const Class* AsClass() const {
178 return down_cast<const Class*>(this);
179 }
180
181 bool IsObjectArray() const {
182 LOG(FATAL) << "Unimplemented";
183 return true;
184 }
185
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700186 const ObjectArray<Object>* AsObjectArray() const {
187 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 }
189
190 bool IsReference() const {
191 LOG(FATAL) << "Unimplemented";
192 return true;
193 }
194
195 bool IsWeakReference() const {
196 LOG(FATAL) << "Unimplemented";
197 return true;
198 }
199
200 bool IsSoftReference() const {
201 LOG(FATAL) << "Unimplemented";
202 return true;
203 }
204
205 bool IsFinalizerReference() const {
206 LOG(FATAL) << "Unimplemented";
207 return true;
208 }
209
210 bool IsPhantomReference() const {
211 LOG(FATAL) << "Unimplemented";
212 return true;
213 }
214
215 bool IsArray() const {
216 LOG(FATAL) << "Unimplemented";
217 return true;
218 }
219
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700221 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223 Monitor* monitor_;
224
225 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700226 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700227};
228
229class ObjectLock {
230 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700231 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700232 CHECK(object != NULL);
233 obj_->MonitorEnter();
234 }
235
236 ~ObjectLock() {
237 obj_->MonitorExit();
238 }
239
240 void Wait(int64_t millis = 0) {
241 return obj_->Wait(millis);
242 }
243
244 void Notify() {
245 obj_->Notify();
246 }
247
248 void NotifyAll() {
249 obj_->NotifyAll();
250 }
251
252 private:
253 Object* obj_;
254 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700255};
256
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400257class AccessibleObject : public Object {
258 private:
259 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
260 uint32_t java_flag_;
261};
262
263class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700264 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700265 Class* GetDeclaringClass() const {
266 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700267 }
268
Jesse Wilson14150742011-07-29 19:04:44 -0400269 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700270 return name_;
271 }
272
273 bool IsStatic() const {
274 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 }
276
277 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700278 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700279 }
280
Brian Carlstromae3ac012011-07-27 01:30:28 -0700281 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700282 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700283 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700284 }
285
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700286 uint32_t GetOffset() const {
287 return offset_;
288 }
289
290 void SetOffset(size_t num_bytes) {
291 offset_ = num_bytes;
292 }
293
Jesse Wilson35baaab2011-08-10 16:18:03 -0400294 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400295 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400296 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400297 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400298 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400299 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400300 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400301 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400302 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400303 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400304 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400305 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400306 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400307 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400308 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400309 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400310 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400311 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400312 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400313 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700314
Jesse Wilson35baaab2011-08-10 16:18:03 -0400315 public: // TODO: private
316 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
317 // The class in which this field is declared.
318 Class* declaring_class_;
319 Object* generic_type_;
320 uint32_t generic_types_are_initialized_;
321 String* name_;
322 uint32_t offset_;
323 Class* type_;
324
325 // e.g. "I", "[C", "Landroid/os/Debug;"
326 StringPiece descriptor_;
327
328 uint32_t access_flags_;
329
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700330 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400331 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700332};
333
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400334class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700335 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700336 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700337 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700338 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700339 }
340
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700341 const String* GetDescriptor() const {
342 return descriptor_;
343 }
344
Brian Carlstroma0808032011-07-18 00:39:23 -0700345 Class* GetDeclaringClass() const {
346 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347 }
348
Ian Rogersb033c752011-07-20 12:22:35 -0700349 static MemberOffset ClassOffset() {
350 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
351 }
352
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700353 // Returns true if the method is declared public.
354 bool IsPublic() const {
355 return (access_flags_ & kAccPublic) != 0;
356 }
357
358 // Returns true if the method is declared private.
359 bool IsPrivate() const {
360 return (access_flags_ & kAccPrivate) != 0;
361 }
362
363 // Returns true if the method is declared static.
364 bool IsStatic() const {
365 return (access_flags_ & kAccStatic) != 0;
366 }
367
368 // Returns true if the method is declared synchronized.
369 bool IsSynchronized() const {
370 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
371 return (access_flags_ & synchonized) != 0;
372 }
373
374 // Returns true if the method is declared final.
375 bool IsFinal() const {
376 return (access_flags_ & kAccFinal) != 0;
377 }
378
379 // Returns true if the method is declared native.
380 bool IsNative() const {
381 return (access_flags_ & kAccNative) != 0;
382 }
383
384 // Returns true if the method is declared abstract.
385 bool IsAbstract() const {
386 return (access_flags_ & kAccAbstract) != 0;
387 }
388
389 bool IsSynthetic() const {
390 return (access_flags_ & kAccSynthetic) != 0;
391 }
392
393 // Number of argument registers required by the prototype.
394 uint32_t NumArgRegisters();
395
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700396 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400397 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400398 // the class we are a part of
399 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400400 ObjectArray<Class>* java_exception_types_;
401 Object* java_formal_type_parameters_;
402 Object* java_generic_exception_types_;
403 Object* java_generic_parameter_types_;
404 Object* java_generic_return_type_;
405 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700406 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400407 ObjectArray<Class>* java_parameter_types_;
408 uint32_t java_generic_types_are_initialized_;
409 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700410
Ian Rogersb033c752011-07-20 12:22:35 -0700411 bool IsReturnAReference() const {
412 return (shorty_[0] == 'L') || (shorty_[0] == '[');
413 }
414
415 bool IsReturnAFloatOrDouble() const {
416 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
417 }
418
419 bool IsReturnAFloat() const {
420 return shorty_[0] == 'F';
421 }
422
423 bool IsReturnADouble() const {
424 return shorty_[0] == 'D';
425 }
426
427 bool IsReturnALong() const {
428 return shorty_[0] == 'J';
429 }
430
Ian Rogers45a76cb2011-07-21 22:00:15 -0700431 bool IsReturnVoid() const {
432 return shorty_[0] == 'V';
433 }
434
Ian Rogersb033c752011-07-20 12:22:35 -0700435 // The number of arguments that should be supplied to this method
436 size_t NumArgs() const {
437 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
438 }
439
440 // The number of reference arguments to this method including implicit this
441 // pointer
442 size_t NumReferenceArgs() const;
443
444 // The number of long or double arguments
445 size_t NumLongOrDoubleArgs() const;
446
447 // The number of reference arguments to this method before the given
448 // parameter index
449 size_t NumReferenceArgsBefore(unsigned int param) const;
450
451 // Is the given method parameter a reference?
452 bool IsParamAReference(unsigned int param) const;
453
454 // Is the given method parameter a long or double?
455 bool IsParamALongOrDouble(unsigned int param) const;
456
Ian Rogersdf20fe02011-07-20 20:34:16 -0700457 // Size in bytes of the given parameter
458 size_t ParamSize(unsigned int param) const;
459
460 // Size in bytes of the return value
461 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700462
463 void SetCode(const void* code) {
464 code_ = code;
465 }
466
467 const void* GetCode() const {
468 return code_;
469 }
470
471 void RegisterNative(const void* native_method) {
472 native_method_ = native_method;
473 }
474
475 static MemberOffset NativeMethodOffset() {
476 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
477 }
478
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700479 bool HasSameNameAndDescriptor(const Method* that) const;
480
Ian Rogersb033c752011-07-20 12:22:35 -0700481 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700482 // access flags; low 16 bits are defined by spec (could be uint16_t?)
483 uint32_t access_flags_;
484
485 // For concrete virtual methods, this is the offset of the method
486 // in "vtable".
487 //
488 // For abstract methods in an interface class, this is the offset
489 // of the method in "iftable[n]->methodIndexArray".
490 uint16_t method_index_;
491
492 // Method bounds; not needed for an abstract method.
493 //
494 // For a native method, we compute the size of the argument list, and
495 // set "insSize" and "registerSize" equal to it.
496 uint16_t num_registers_; // ins + locals
497 uint16_t num_outs_;
498 uint16_t num_ins_;
499
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700500 // The method descriptor. This represents the parameters a method
501 // takes and value it returns. This string is a list of the type
502 // descriptors for the parameters enclosed in parenthesis followed
503 // by the return type descriptor. For example, for the method
504 //
505 // Object mymethod(int i, double d, Thread t)
506 //
507 // the method descriptor would be
508 //
509 // (IDLjava/lang/Thread;)Ljava/lang/Object;
510 String* descriptor_;
511
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700512 // Method prototype descriptor string (return and argument types).
513 uint32_t proto_idx_;
514
515 // The short-form method descriptor string.
516 StringPiece shorty_;
517
518 // A pointer to the memory-mapped DEX code.
519 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700520
521 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700522 // Compiled code associated with this method
523 const void* code_;
524
525 // Any native method registered with this method
526 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700527
528 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529};
530
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700531class Array : public Object {
532 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700533 static Array* Alloc(Class* array_class,
534 size_t component_count,
535 size_t component_size) {
536 size_t size = sizeof(Array) + component_count * component_size;
537 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
538 if (array != NULL) {
539 array->SetLength(component_count);
540 }
541 return array;
542 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700543
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700544 uint32_t GetLength() const {
545 return length_;
546 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700547
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700548 void SetLength(uint32_t length) {
549 length_ = length;
550 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700551
552 private:
553 // The number of array elements.
554 uint32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400555 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
556 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700557
Carl Shapirof88c9522011-08-06 15:47:38 -0700558 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700559};
560
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700561template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700562class ObjectArray : public Array {
563 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700564 static ObjectArray<T>* Alloc(Class* object_array_class,
565 size_t length) {
566 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
567 length,
568 sizeof(uint32_t)));
569 }
570
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700571 T* const * GetData() const {
572 return reinterpret_cast<T* const *>(&elements_);
573 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400574
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700575 T** GetData() {
576 return reinterpret_cast<T**>(&elements_);
577 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400578
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700579 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700580 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700581 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700582 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700583
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700584 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700585 CHECK_LT(i, GetLength());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400586 GetData()[i] = object; // TODO: write barrier
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700587 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700588
589 static void Copy(ObjectArray<T>* src, int src_pos,
590 ObjectArray<T>* dst, int dst_pos,
591 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700592 for (size_t i = 0; i < length; i++) {
593 dst->Set(dst_pos + i, src->Get(src_pos + i));
594 }
595 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700596
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700597 ObjectArray<T>* CopyOf(size_t new_length) {
598 ObjectArray<T>* new_array = Alloc(klass_, new_length);
599 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
600 return new_array;
601 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700602
603 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700604 // Location of first element.
605 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700606
607 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700608};
609
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700610// ClassLoader objects.
611class ClassLoader : public Object {
612 public:
613 std::vector<const DexFile*>& GetClassPath() {
614 return class_path_;
615 }
616 void SetClassPath(std::vector<const DexFile*>& class_path) {
617 DCHECK_EQ(0U, class_path_.size());
618 class_path_ = class_path;
619 }
620
621 private:
622 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
623 Object* packages_;
624 ClassLoader* parent_;
625
626 // TODO remove once we can create a real PathClassLoader
627 std::vector<const DexFile*> class_path_;
628
Carl Shapirof88c9522011-08-06 15:47:38 -0700629 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700630};
631
632class BaseDexClassLoader : public ClassLoader {
633 private:
634 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
635 String* original_path_;
636 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700637 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700638};
639
640class PathClassLoader : public BaseDexClassLoader {
641 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700642 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700643};
644
Carl Shapiro1fb86202011-06-27 17:43:13 -0700645// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700646class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700647 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700648
649 // Class Status
650 //
651 // kStatusNotReady: If a Class cannot be found in the class table by
652 // FindClass, it allocates an new one with AllocClass in the
653 // kStatusNotReady and calls LoadClass. Note if it does find a
654 // class, it may not be kStatusResolved and it will try to push it
655 // forward toward kStatusResolved.
656 //
657 // kStatusIdx: LoadClass populates with Class with information from
658 // the DexFile, moving the status to kStatusIdx, indicating that the
659 // Class values in super_class_ and interfaces_ have not been
660 // populated based on super_class_idx_ and interfaces_idx_. The new
661 // Class can then be inserted into the classes table.
662 //
663 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
664 // attempt to move a kStatusIdx class forward to kStatusLoaded by
665 // using ResolveClass to initialize the super_class_ and interfaces_.
666 //
667 // kStatusResolved: Still holding the lock on Class, the ClassLinker
668 // will use LinkClass to link all members, creating Field and Method
669 // objects, setting up the vtable, etc. On success, the class is
670 // marked kStatusResolved.
671
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700672 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700673 kStatusError = -1,
674 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700675 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700676 kStatusLoaded = 2, // DEX idx values resolved
677 kStatusResolved = 3, // part of linking
678 kStatusVerifying = 4, // in the process of being verified
679 kStatusVerified = 5, // logically part of linking; done pre-init
680 kStatusInitializing = 6, // class init in progress
681 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700682 };
683
684 enum PrimitiveType {
685 kPrimNot = -1
686 };
687
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700688 Object* NewInstance() {
689 return Heap::AllocObject(this, this->object_size_);
690 }
691
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700692 Class* GetSuperClass() const {
693 return super_class_;
694 }
695
696 uint32_t GetSuperClassIdx() const {
697 return super_class_idx_;
698 }
699
700 bool HasSuperClass() const {
701 return super_class_ != NULL;
702 }
703
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700704 bool IsAssignableFrom(const Class* klass) const {
705 DCHECK(klass != NULL);
706 if (this == klass) {
707 return true;
708 }
709 if (IsInterface()) {
710 return klass->Implements(this);
711 }
712 if (klass->IsArray()) {
713 return IsAssignableFromArray(klass);
714 }
715 return klass->IsSubClass(this);
716 }
717
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700718 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700719 return class_loader_;
720 }
721
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700722 DexCache* GetDexCache() const {
723 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700724 }
725
726 Class* GetComponentType() const {
727 return component_type_;
728 }
729
730 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700731 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700732 return descriptor_;
733 }
734
735 Status GetStatus() const {
736 return status_;
737 }
738
739 void SetStatus(Status new_status) {
740 // TODO: validate transition
741 status_ = new_status;
742 }
743
Carl Shapiro69759ea2011-07-21 18:13:35 -0700744 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 bool IsErroneous() const {
746 return GetStatus() == kStatusError;
747 }
748
Carl Shapiro69759ea2011-07-21 18:13:35 -0700749 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700750 bool IsVerified() const {
751 return GetStatus() >= kStatusVerified;
752 }
753
Carl Shapiro69759ea2011-07-21 18:13:35 -0700754 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700755 bool IsLinked() const {
756 return GetStatus() >= kStatusResolved;
757 }
758
Carl Shapiro69759ea2011-07-21 18:13:35 -0700759 bool IsLoaded() const {
760 return GetStatus() >= kStatusLoaded;
761 }
762
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700763 // Returns true if this class is in the same packages as that class.
764 bool IsInSamePackage(const Class* that) const;
765
Ian Rogersb033c752011-07-20 12:22:35 -0700766 static bool IsInSamePackage(const StringPiece& descriptor1,
767 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700768
769 // Returns true if this class represents an array class.
770 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700771 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700772 }
773
774 // Returns true if the class is an interface.
775 bool IsInterface() const {
776 return (access_flags_ & kAccInterface) != 0;
777 }
778
779 // Returns true if the class is declared public.
780 bool IsPublic() const {
781 return (access_flags_ & kAccPublic) != 0;
782 }
783
784 // Returns true if the class is declared final.
785 bool IsFinal() const {
786 return (access_flags_ & kAccFinal) != 0;
787 }
788
789 // Returns true if the class is abstract.
790 bool IsAbstract() const {
791 return (access_flags_ & kAccAbstract) != 0;
792 }
793
794 // Returns true if the class is an annotation.
795 bool IsAnnotation() const {
796 return (access_flags_ & kAccAnnotation) != 0;
797 }
798
799 // Returns true if the class is a primitive type.
800 bool IsPrimitive() const {
801 return primitive_type_ != kPrimNot;
802 }
803
Brian Carlstromae3ac012011-07-27 01:30:28 -0700804 // Returns true if the class is synthetic.
805 bool IsSynthetic() const {
806 return (access_flags_ & kAccSynthetic) != 0;
807 }
808
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700809 // Returns true if this class can access that class.
810 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700811 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700812 }
813
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700814 // Returns the number of static, private, and constructor methods.
815 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700816 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700817 }
818
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700819 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700820 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700821 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700822 }
823
824 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700825 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700826 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700827 }
828
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700829 Method* FindDeclaredDirectMethod(const StringPiece& name,
830 const StringPiece& descriptor);
831
832 Method* FindDirectMethod(const StringPiece& name,
833 const StringPiece& descriptor);
834
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700835 // Returns the number of non-inherited virtual methods.
836 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700837 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700838 }
839
840 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700841 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700842 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700843 }
844
845 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700846 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700847 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700848 }
849
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700850 Method* FindDeclaredVirtualMethod(const StringPiece& name,
851 const StringPiece& descriptor);
852
853 Method* FindVirtualMethod(const StringPiece& name,
854 const StringPiece& descriptor);
855
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700857 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700858 }
859
Carl Shapiro69759ea2011-07-21 18:13:35 -0700860 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700861 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700862 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700863 }
864
Jesse Wilson35baaab2011-08-10 16:18:03 -0400865 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700866 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700867 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700868 }
869
Jesse Wilson35baaab2011-08-10 16:18:03 -0400870 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700871 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700872 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700873 }
874
875 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700876 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700877 }
878
Jesse Wilson35baaab2011-08-10 16:18:03 -0400879 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700880 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700881 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700882 }
883
Jesse Wilson35baaab2011-08-10 16:18:03 -0400884 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700885 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700886 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700887 }
888
889 uint32_t GetReferenceOffsets() const {
890 return reference_offsets_;
891 }
892
893 void SetReferenceOffsets(uint32_t new_reference_offsets) {
894 reference_offsets_ = new_reference_offsets;
895 }
896
Carl Shapiro69759ea2011-07-21 18:13:35 -0700897 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700898 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700899 }
900
901 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700902 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700903 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700904 }
905
906 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700907 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700908 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700909 }
910
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700911 void SetVerifyErrorClass(Class* klass) {
912 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
913 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
914 klass->SetFieldObject(field_offset, klass);
915 }
916
917 private:
918 bool Implements(const Class* klass) const;
919 bool IsArrayAssignableFromArray(const Class* klass) const;
920 bool IsAssignableFromArray(const Class* klass) const;
921 bool IsSubClass(const Class* klass) const;
922
Ian Rogersb033c752011-07-20 12:22:35 -0700923 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700924 // leave space for instance data; we could access fields directly if
925 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700926#define CLASS_FIELD_SLOTS 1
927 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700928 uint32_t instance_data_[CLASS_FIELD_SLOTS];
929#undef CLASS_FIELD_SLOTS
930
931 // UTF-8 descriptor for the class from constant pool
932 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700933 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700934
935 // Proxy classes have their descriptor allocated on the native heap.
936 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700937 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700938
939 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700940 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700941
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700942 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700943 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700944 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700945
946 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700948
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700949 // If class verify fails, we must return same error on subsequent tries.
950 // Update with SetVerifyErrorClass to ensure a write barrier is used.
951 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700952
953 // threadId, used to check for recursive <clinit> invocation
954 uint32_t clinit_thread_id_;
955
956 // Total object size; used when allocating storage on gc heap. (For
957 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700958 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700959
960 // For array classes, the class object for base element, for
961 // instanceof/checkcast (for String[][][], this will be String).
962 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700964
965 // For array classes, the number of array dimensions, e.g. int[][]
966 // is 2. Otherwise 0.
967 int32_t array_rank_;
968
969 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
970 PrimitiveType primitive_type_;
971
972 // The superclass, or NULL if this is java.lang.Object or a
973 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974 Class* super_class_; // TODO: make an instance field
975 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700976
977 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700978 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700979
980 // initiating class loader list
981 // NOTE: for classes with low serialNumber, these are unused, and the
982 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700983 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700984
985 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700986 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700987 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700988
989 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700990 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700991
992 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700993 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994
995 // Virtual method table (vtable), for use by "invoke-virtual". The
996 // vtable from the superclass is copied in, and virtual methods from
997 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700998 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700999
1000 // Interface table (iftable), one entry per interface supported by
1001 // this class. That means one entry for each interface we support
1002 // directly, indirectly via superclass, or indirectly via
1003 // superinterface. This will be null if neither we nor our
1004 // superclass implement any interfaces.
1005 //
1006 // Why we need this: given "class Foo implements Face", declare
1007 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1008 // is part of the Face interface. We can't easily use a single
1009 // vtable.
1010 //
1011 // For every interface a concrete class implements, we create a list
1012 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001013 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014 InterfaceEntry* iftable_;
1015
1016 // The interface vtable indices for iftable get stored here. By
1017 // placing them all in a single pool for each class that implements
1018 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 size_t ifvi_pool_count_;
1020 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001021
1022 // instance fields
1023 //
1024 // These describe the layout of the contents of a
1025 // DataObject-compatible Object. Note that only the fields directly
1026 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001027 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001028 //
1029 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001030 // the beginning of the field list. num_reference_instance_fields_
1031 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001032 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001033
1034 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001035 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001036
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001037 // Bitmap of offsets of ifields.
1038 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001039
1040 // source file name, if known. Otherwise, NULL.
1041 const char* source_file_;
1042
Jesse Wilson7833bd22011-08-09 18:31:44 -04001043 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001044 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001045
1046 // static field storage
1047 //
1048 // Each static field is stored in one of three arrays:
1049 // o references are stored in static_references_
1050 // o doubles and longs are stored in static_64bit_primitives_
1051 // o everything else is in static_32bit_primitives_
1052 // Static fields select their array using their type and their index using the
1053 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1054 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001055 ObjectArray<Object>* static_references_;
1056 IntArray* static_32bit_primitives_;
1057 LongArray* static_64bit_primitives_;
1058
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001059 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001060 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001061};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001062std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001063
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001064inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001065 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001066 DCHECK(klass_ != NULL);
1067 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001068}
1069
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001070
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001071class DataObject : public Object {
1072 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001073 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001074 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001075 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001076};
1077
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001078template<class T>
1079class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001080 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001081 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1082 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1083 length,
1084 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001085 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001086
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001087 const T* GetData() const {
1088 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001089 }
1090
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001091 T* GetData() {
1092 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001093 }
1094
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001095 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001096 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001097 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001098 }
1099
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001100 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001101 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001102 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001103 }
1104
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001105 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001106 // Location of first element.
1107 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001108
1109 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001110};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001111
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001112class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001113 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001114 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001115 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001116 return array_;
1117 }
1118
Carl Shapirof88c9522011-08-06 15:47:38 -07001119 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001120 return hash_code_;
1121 }
1122
Carl Shapirof88c9522011-08-06 15:47:38 -07001123 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001124 return offset_;
1125 }
1126
Carl Shapirof88c9522011-08-06 15:47:38 -07001127 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001128 return count_;
1129 }
1130
Carl Shapirof88c9522011-08-06 15:47:38 -07001131 uint16_t CharAt(uint32_t index) const {
1132 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001133 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001134 }
1135
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001136 static String* AllocFromUtf16(int32_t utf16_length,
1137 uint16_t* utf16_data_in,
1138 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001139 String* string = Alloc(GetJavaLangString(),
1140 GetCharArrayClass(),
1141 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001142 // TODO use 16-bit wide memset variant
1143 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001144 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001145 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001146 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001147 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001148 }
1149
1150 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1151 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001152 int32_t utf16_length,
1153 const char* utf8_data_in) {
1154 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001155 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001156 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001157 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001158 return string;
1159 }
1160
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001161 // Creates a String of the given ASCII characters. It is an error to call this
1162 // using non-ASCII characters as this function assumes one char per byte.
1163 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001164 return AllocFromModifiedUtf8(GetJavaLangString(),
1165 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001166 strlen(ascii_data_in),
1167 ascii_data_in);
1168 }
1169
Jesse Wilson8989d992011-08-02 13:39:42 -07001170 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1171 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001172 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1173 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001174 }
1175
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001176 static void InitClasses(Class* java_lang_String, Class* char_array);
1177
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001178 static String* Alloc(Class* java_lang_String,
1179 Class* char_array,
1180 int32_t utf16_length) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001181 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001182 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1183 string->array_ = array;
1184 string->count_ = utf16_length;
1185 return string;
1186 }
1187
1188 // Convert Modified UTF-8 to UTF-16
1189 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1190 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1191 while (*utf8_data_in != '\0') {
1192 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1193 }
1194 }
1195
1196 // Retrieve the next UTF-16 character from a UTF-8 string.
1197 //
1198 // Advances "*pUtf8Ptr" to the start of the next character.
1199 //
1200 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1201 // of a 3-byte sequence, you can end up overrunning the buffer with
1202 // reads (and possibly with the writes if the length was computed and
1203 // cached before the damage). For performance reasons, this function
1204 // assumes that the string being parsed is known to be valid (e.g., by
1205 // already being verified). Most strings we process here are coming
1206 // out of dex files or other internal translations, so the only real
1207 // risk comes from the JNI NewStringUTF call.
1208 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1209 uint8_t one = *(*utf8_data_in)++;
1210 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001211 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001212 return one;
1213 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001214 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001215 uint8_t two = *(*utf8_data_in)++;
1216 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001217 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001218 return ((one & 0x1f) << 6) |
1219 (two & 0x3f);
1220 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001221 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001222 uint8_t three = *(*utf8_data_in)++;
1223 return ((one & 0x0f) << 12) |
1224 ((two & 0x3f) << 6) |
1225 (three & 0x3f);
1226 }
1227
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001228 // Like "strlen", but for strings encoded with "modified" UTF-8.
1229 //
1230 // The value returned is the number of characters, which may or may not
1231 // be the same as the number of bytes.
1232 //
1233 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1234 // get increment {1-3} from table of 8 values.)
1235 static size_t ModifiedUtf8Len(const char* utf8) {
1236 size_t len = 0;
1237 int ic;
1238 while ((ic = *utf8++) != '\0') {
1239 len++;
1240 if ((ic & 0x80) == 0) {
1241 // one-byte encoding
1242 continue;
1243 }
1244 // two- or three-byte encoding
1245 utf8++;
1246 if ((ic & 0x20) == 0) {
1247 // two-byte encoding
1248 continue;
1249 }
1250 // three-byte encoding
1251 utf8++;
1252 }
1253 return len;
1254 }
1255
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001256 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001257 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1258 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001259 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001260 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001261 }
1262 return hash;
1263 }
1264
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001265 void ComputeHashCode() {
1266 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1267 }
1268
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001269 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001270 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001271 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1272 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001273 return false;
1274 }
1275 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001276 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001277 }
1278
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001279 bool Equals(const StringPiece& modified_utf8) const {
1280 // TODO: do not assume C-string representation.
1281 return Equals(modified_utf8.data());
1282 }
1283
1284 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001285 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001286 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001287 return false;
1288 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001289 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001290 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001291 return false;
1292 }
1293 }
1294 return true;
1295 }
1296
Carl Shapirof88c9522011-08-06 15:47:38 -07001297 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001298 if (this->GetLength() != that_length) {
1299 return false;
1300 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001301 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001302 if (this->CharAt(i) != that_chars[that_offset + i]) {
1303 return false;
1304 }
1305 }
1306 return true;
1307 }
1308
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001309 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001310 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1311 CharArray* array_;
1312
Carl Shapirof88c9522011-08-06 15:47:38 -07001313 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001314
Carl Shapirof88c9522011-08-06 15:47:38 -07001315 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001316
Carl Shapirof88c9522011-08-06 15:47:38 -07001317 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001318
1319 static Class* GetJavaLangString() {
1320 DCHECK(java_lang_String_ != NULL);
1321 return java_lang_String_;
1322 }
1323 static Class* GetCharArrayClass() {
1324 DCHECK(char_array_ != NULL);
1325 return char_array_;
1326 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001327
1328 static Class* java_lang_String_;
1329 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001330
1331 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001332};
1333
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001334class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001335 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001336 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1337 }
1338
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001339 Class* GetClass() const {
1340 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001341 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001342
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001343 void SetClass(Class* klass) {
1344 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001345 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001346
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001347 private:
1348 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001349 Class* klass_;
1350
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 public: // TODO: private
1352 // Index into array of vtable offsets. This points into the
1353 // ifviPool, which holds the vtables for all interfaces declared by
1354 // this class.
1355 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001356
1357 private:
1358 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001359};
1360
1361} // namespace art
1362
1363#endif // ART_SRC_OBJECT_H_