blob: 5e6c17565331a3cba717b68b77aee9ece2f03274 [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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021class InstanceField;
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;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070029class StaticField;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070030typedef PrimitiveArray<uint16_t> CharArray;
31typedef PrimitiveArray<uint32_t> IntArray;
32typedef PrimitiveArray<uint64_t> LongArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070033
Carl Shapiro3ee755d2011-06-28 12:11:04 -070034union JValue {
35 uint8_t z;
36 int8_t b;
37 uint16_t c;
38 int16_t s;
39 int32_t i;
40 int64_t j;
41 float f;
42 double d;
43 Object* l;
44};
45
Brian Carlstrombe977852011-07-19 14:54:54 -070046static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
47static const uint32_t kAccPrivate = 0x0002; // field, method, ic
48static const uint32_t kAccProtected = 0x0004; // field, method, ic
49static const uint32_t kAccStatic = 0x0008; // field, method, ic
50static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
51static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
52static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
53static const uint32_t kAccVolatile = 0x0040; // field
54static const uint32_t kAccBridge = 0x0040; // method (1.5)
55static const uint32_t kAccTransient = 0x0080; // field
56static const uint32_t kAccVarargs = 0x0080; // method (1.5)
57static const uint32_t kAccNative = 0x0100; // method
58static const uint32_t kAccInterface = 0x0200; // class, ic
59static const uint32_t kAccAbstract = 0x0400; // class, method, ic
60static const uint32_t kAccStrict = 0x0800; // method
61static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
62static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
63static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070064
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065static const uint32_t kAccMiranda = 0x8000; // method
66
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
68
Brian Carlstrombe977852011-07-19 14:54:54 -070069static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
70static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070073 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070074 */
75/*
76 * A magic value for refOffsets. Ignore the bits and walk the super
77 * chain when this is the value.
78 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
79 * fields followed by 2 ref instance fields.]
80 */
81#define CLASS_WALK_SUPER ((unsigned int)(3))
82#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
83#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
84#define CLASS_OFFSET_ALIGNMENT 4
85#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
86/*
87 * Given an offset, return the bit number which would encode that offset.
88 * Local use only.
89 */
90#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
91 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
92 CLASS_OFFSET_ALIGNMENT)
93/*
94 * Is the given offset too large to be encoded?
95 */
96#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
97 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
98/*
99 * Return a single bit, encoding the offset.
100 * Undefined if the offset is too large, as defined above.
101 */
102#define CLASS_BIT_FROM_OFFSET(byteOffset) \
103 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
104/*
105 * Return an offset, given a bit number as returned from CLZ.
106 */
107#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700108 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109
110
Carl Shapiro1fb86202011-06-27 17:43:13 -0700111class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700112 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700113 static Object* Alloc(Class* klass);
114
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700115 Class* GetClass() const {
116 return klass_;
117 }
118
119 void MonitorEnter() {
120 monitor_->Enter();
121 }
122
123 void MonitorExit() {
124 monitor_->Exit();
125 }
126
127 void Notify() {
128 monitor_->Notify();
129 }
130
131 void NotifyAll() {
132 monitor_->NotifyAll();
133 }
134
135 void Wait() {
136 monitor_->Wait();
137 }
138
139 void Wait(int64_t timeout) {
140 monitor_->Wait(timeout);
141 }
142
143 void Wait(int64_t timeout, int32_t nanos) {
144 monitor_->Wait(timeout, nanos);
145 }
146
Carl Shapiro69759ea2011-07-21 18:13:35 -0700147 const Object* GetFieldObject(size_t field_offset) const {
148 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
149 return *reinterpret_cast<Object* const*>(raw_addr);
150 }
151
152 Object* GetFieldObject(size_t field_offset) {
153 return const_cast<Object*>(GetFieldObject(field_offset));
154 }
155
156 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700157 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
158 *reinterpret_cast<Object**>(raw_addr) = new_value;
159 // TODO: write barrier
160 }
161
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 bool IsClass() const {
163 LOG(FATAL) << "Unimplemented";
164 return true;
165 }
166
167 Class* AsClass() {
168 return down_cast<Class*>(this);
169 }
170
171 const Class* AsClass() const {
172 return down_cast<const Class*>(this);
173 }
174
175 bool IsObjectArray() const {
176 LOG(FATAL) << "Unimplemented";
177 return true;
178 }
179
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700180 const ObjectArray<Object>* AsObjectArray() const {
181 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182 }
183
184 bool IsReference() const {
185 LOG(FATAL) << "Unimplemented";
186 return true;
187 }
188
189 bool IsWeakReference() const {
190 LOG(FATAL) << "Unimplemented";
191 return true;
192 }
193
194 bool IsSoftReference() const {
195 LOG(FATAL) << "Unimplemented";
196 return true;
197 }
198
199 bool IsFinalizerReference() const {
200 LOG(FATAL) << "Unimplemented";
201 return true;
202 }
203
204 bool IsPhantomReference() const {
205 LOG(FATAL) << "Unimplemented";
206 return true;
207 }
208
209 bool IsArray() const {
210 LOG(FATAL) << "Unimplemented";
211 return true;
212 }
213
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700215 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700217 Monitor* monitor_;
218
219 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700220 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221};
222
223class ObjectLock {
224 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700225 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226 CHECK(object != NULL);
227 obj_->MonitorEnter();
228 }
229
230 ~ObjectLock() {
231 obj_->MonitorExit();
232 }
233
234 void Wait(int64_t millis = 0) {
235 return obj_->Wait(millis);
236 }
237
238 void Notify() {
239 obj_->Notify();
240 }
241
242 void NotifyAll() {
243 obj_->NotifyAll();
244 }
245
246 private:
247 Object* obj_;
248 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700249};
250
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400251class AccessibleObject : public Object {
252 private:
253 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
254 uint32_t java_flag_;
255};
256
257class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700258 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700259 Class* GetDeclaringClass() const {
260 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700261 }
262
Jesse Wilson14150742011-07-29 19:04:44 -0400263 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700264 return name_;
265 }
266
267 bool IsStatic() const {
268 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269 }
270
271 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700272 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700273 }
274
Brian Carlstromae3ac012011-07-27 01:30:28 -0700275 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700276 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700277 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700278 }
279
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400281 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
282 Class* java_declaring_class_;
283 Object* java_generic_type_;
284 uint32_t java_generic_types_are_initialized_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700285 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400286 uint32_t java_slot_;
287 Class* java_type_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700288
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700289 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700290 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700291
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700292 // e.g. "I", "[C", "Landroid/os/Debug;"
Brian Carlstromae3ac012011-07-27 01:30:28 -0700293 StringPiece descriptor_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700294
295 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700296
297 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700298 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700299};
300
301// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700302class InstanceField : public Field {
303 public:
304 uint32_t GetOffset() const {
305 return offset_;
306 }
307
308 void SetOffset(size_t num_bytes) {
309 offset_ = num_bytes;
310 }
311
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700312 private:
313 size_t offset_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700314
315 DISALLOW_IMPLICIT_CONSTRUCTORS(InstanceField);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700316};
317
318// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700319class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700320 public:
321 void SetBoolean(bool z) {
322 CHECK_EQ(GetType(), 'Z');
323 value_.z = z;
324 }
325
326 void SetByte(int8_t b) {
327 CHECK_EQ(GetType(), 'B');
328 value_.b = b;
329 }
330
331 void SetChar(uint16_t c) {
332 CHECK_EQ(GetType(), 'C');
333 value_.c = c;
334 }
335
336 void SetShort(uint16_t s) {
337 CHECK_EQ(GetType(), 'S');
338 value_.s = s;
339 }
340
341 void SetInt(int32_t i) {
342 CHECK_EQ(GetType(), 'I');
343 value_.i = i;
344 }
345
346 int64_t GetLong() {
347 CHECK_EQ(GetType(), 'J');
348 return value_.j;
349 }
350
351 void SetLong(int64_t j) {
352 CHECK_EQ(GetType(), 'J');
353 value_.j = j;
354 }
355
356 void SetFloat(float f) {
357 CHECK_EQ(GetType(), 'F');
358 value_.f = f;
359 }
360
361 void SetDouble(double d) {
362 CHECK_EQ(GetType(), 'D');
363 value_.d = d;
364 }
365
Carl Shapiro69759ea2011-07-21 18:13:35 -0700366 Object* GetObject() {
367 return value_.l;
368 }
369
370 const Object* GetObject() const {
371 return value_.l;
372 }
373
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700374 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700375 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700376 value_.l = l;
377 // TODO: write barrier
378 }
379
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700381 JValue value_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700382
383 DISALLOW_IMPLICIT_CONSTRUCTORS(StaticField);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700384};
385
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400386class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387 public:
Brian Carlstromae3ac012011-07-27 01:30:28 -0700388 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700389 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700390 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700393 const String* GetDescriptor() const {
394 return descriptor_;
395 }
396
Brian Carlstroma0808032011-07-18 00:39:23 -0700397 Class* GetDeclaringClass() const {
398 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700399 }
400
Ian Rogersb033c752011-07-20 12:22:35 -0700401 static MemberOffset ClassOffset() {
402 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
403 }
404
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700405 // Returns true if the method is declared public.
406 bool IsPublic() const {
407 return (access_flags_ & kAccPublic) != 0;
408 }
409
410 // Returns true if the method is declared private.
411 bool IsPrivate() const {
412 return (access_flags_ & kAccPrivate) != 0;
413 }
414
415 // Returns true if the method is declared static.
416 bool IsStatic() const {
417 return (access_flags_ & kAccStatic) != 0;
418 }
419
420 // Returns true if the method is declared synchronized.
421 bool IsSynchronized() const {
422 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
423 return (access_flags_ & synchonized) != 0;
424 }
425
426 // Returns true if the method is declared final.
427 bool IsFinal() const {
428 return (access_flags_ & kAccFinal) != 0;
429 }
430
431 // Returns true if the method is declared native.
432 bool IsNative() const {
433 return (access_flags_ & kAccNative) != 0;
434 }
435
436 // Returns true if the method is declared abstract.
437 bool IsAbstract() const {
438 return (access_flags_ & kAccAbstract) != 0;
439 }
440
441 bool IsSynthetic() const {
442 return (access_flags_ & kAccSynthetic) != 0;
443 }
444
445 // Number of argument registers required by the prototype.
446 uint32_t NumArgRegisters();
447
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700448 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400449 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
450 Class* java_declaring_class_;
451 ObjectArray<Class>* java_exception_types_;
452 Object* java_formal_type_parameters_;
453 Object* java_generic_exception_types_;
454 Object* java_generic_parameter_types_;
455 Object* java_generic_return_type_;
456 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700457 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400458 ObjectArray<Class>* java_parameter_types_;
459 uint32_t java_generic_types_are_initialized_;
460 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700461
Ian Rogersb033c752011-07-20 12:22:35 -0700462 bool IsReturnAReference() const {
463 return (shorty_[0] == 'L') || (shorty_[0] == '[');
464 }
465
466 bool IsReturnAFloatOrDouble() const {
467 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
468 }
469
470 bool IsReturnAFloat() const {
471 return shorty_[0] == 'F';
472 }
473
474 bool IsReturnADouble() const {
475 return shorty_[0] == 'D';
476 }
477
478 bool IsReturnALong() const {
479 return shorty_[0] == 'J';
480 }
481
Ian Rogers45a76cb2011-07-21 22:00:15 -0700482 bool IsReturnVoid() const {
483 return shorty_[0] == 'V';
484 }
485
Ian Rogersb033c752011-07-20 12:22:35 -0700486 // The number of arguments that should be supplied to this method
487 size_t NumArgs() const {
488 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
489 }
490
491 // The number of reference arguments to this method including implicit this
492 // pointer
493 size_t NumReferenceArgs() const;
494
495 // The number of long or double arguments
496 size_t NumLongOrDoubleArgs() const;
497
498 // The number of reference arguments to this method before the given
499 // parameter index
500 size_t NumReferenceArgsBefore(unsigned int param) const;
501
502 // Is the given method parameter a reference?
503 bool IsParamAReference(unsigned int param) const;
504
505 // Is the given method parameter a long or double?
506 bool IsParamALongOrDouble(unsigned int param) const;
507
Ian Rogersdf20fe02011-07-20 20:34:16 -0700508 // Size in bytes of the given parameter
509 size_t ParamSize(unsigned int param) const;
510
511 // Size in bytes of the return value
512 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700513
514 void SetCode(const void* code) {
515 code_ = code;
516 }
517
518 const void* GetCode() const {
519 return code_;
520 }
521
522 void RegisterNative(const void* native_method) {
523 native_method_ = native_method;
524 }
525
526 static MemberOffset NativeMethodOffset() {
527 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
528 }
529
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700530 bool HasSameNameAndDescriptor(const Method* that) const;
531
Ian Rogersb033c752011-07-20 12:22:35 -0700532 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700533 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700534 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700535
536 // access flags; low 16 bits are defined by spec (could be uint16_t?)
537 uint32_t access_flags_;
538
539 // For concrete virtual methods, this is the offset of the method
540 // in "vtable".
541 //
542 // For abstract methods in an interface class, this is the offset
543 // of the method in "iftable[n]->methodIndexArray".
544 uint16_t method_index_;
545
546 // Method bounds; not needed for an abstract method.
547 //
548 // For a native method, we compute the size of the argument list, and
549 // set "insSize" and "registerSize" equal to it.
550 uint16_t num_registers_; // ins + locals
551 uint16_t num_outs_;
552 uint16_t num_ins_;
553
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700554 // The method descriptor. This represents the parameters a method
555 // takes and value it returns. This string is a list of the type
556 // descriptors for the parameters enclosed in parenthesis followed
557 // by the return type descriptor. For example, for the method
558 //
559 // Object mymethod(int i, double d, Thread t)
560 //
561 // the method descriptor would be
562 //
563 // (IDLjava/lang/Thread;)Ljava/lang/Object;
564 String* descriptor_;
565
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700566 // Method prototype descriptor string (return and argument types).
567 uint32_t proto_idx_;
568
569 // The short-form method descriptor string.
570 StringPiece shorty_;
571
572 // A pointer to the memory-mapped DEX code.
573 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700574
575 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700576 // Compiled code associated with this method
577 const void* code_;
578
579 // Any native method registered with this method
580 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700581
582 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700583};
584
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700585class Array : public Object {
586 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700587 static Array* Alloc(Class* array_class,
588 size_t component_count,
589 size_t component_size) {
590 size_t size = sizeof(Array) + component_count * component_size;
591 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
592 if (array != NULL) {
593 array->SetLength(component_count);
594 }
595 return array;
596 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700597
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700598 uint32_t GetLength() const {
599 return length_;
600 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700601
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700602 void SetLength(uint32_t length) {
603 length_ = length;
604 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700605
606 private:
607 // The number of array elements.
608 uint32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400609 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
610 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700611
Carl Shapirof88c9522011-08-06 15:47:38 -0700612 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700613};
614
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700615template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700616class ObjectArray : public Array {
617 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700618 static ObjectArray<T>* Alloc(Class* object_array_class,
619 size_t length) {
620 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
621 length,
622 sizeof(uint32_t)));
623 }
624
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700625 T* const * GetData() const {
626 return reinterpret_cast<T* const *>(&elements_);
627 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400628
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700629 T** GetData() {
630 return reinterpret_cast<T**>(&elements_);
631 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400632
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700633 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700634 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700635 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700636 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700637
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700638 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700639 CHECK_LT(i, GetLength());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400640 GetData()[i] = object; // TODO: write barrier
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700641 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700642
643 static void Copy(ObjectArray<T>* src, int src_pos,
644 ObjectArray<T>* dst, int dst_pos,
645 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700646 for (size_t i = 0; i < length; i++) {
647 dst->Set(dst_pos + i, src->Get(src_pos + i));
648 }
649 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700650
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700651 ObjectArray<T>* CopyOf(size_t new_length) {
652 ObjectArray<T>* new_array = Alloc(klass_, new_length);
653 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
654 return new_array;
655 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700656
657 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700658 // Location of first element.
659 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700660
661 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700662};
663
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700664// ClassLoader objects.
665class ClassLoader : public Object {
666 public:
667 std::vector<const DexFile*>& GetClassPath() {
668 return class_path_;
669 }
670 void SetClassPath(std::vector<const DexFile*>& class_path) {
671 DCHECK_EQ(0U, class_path_.size());
672 class_path_ = class_path;
673 }
674
675 private:
676 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
677 Object* packages_;
678 ClassLoader* parent_;
679
680 // TODO remove once we can create a real PathClassLoader
681 std::vector<const DexFile*> class_path_;
682
Carl Shapirof88c9522011-08-06 15:47:38 -0700683 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700684};
685
686class BaseDexClassLoader : public ClassLoader {
687 private:
688 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
689 String* original_path_;
690 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700691 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700692};
693
694class PathClassLoader : public BaseDexClassLoader {
695 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700696 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700697};
698
Carl Shapiro1fb86202011-06-27 17:43:13 -0700699// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700700class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700701 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700702
703 // Class Status
704 //
705 // kStatusNotReady: If a Class cannot be found in the class table by
706 // FindClass, it allocates an new one with AllocClass in the
707 // kStatusNotReady and calls LoadClass. Note if it does find a
708 // class, it may not be kStatusResolved and it will try to push it
709 // forward toward kStatusResolved.
710 //
711 // kStatusIdx: LoadClass populates with Class with information from
712 // the DexFile, moving the status to kStatusIdx, indicating that the
713 // Class values in super_class_ and interfaces_ have not been
714 // populated based on super_class_idx_ and interfaces_idx_. The new
715 // Class can then be inserted into the classes table.
716 //
717 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
718 // attempt to move a kStatusIdx class forward to kStatusLoaded by
719 // using ResolveClass to initialize the super_class_ and interfaces_.
720 //
721 // kStatusResolved: Still holding the lock on Class, the ClassLinker
722 // will use LinkClass to link all members, creating Field and Method
723 // objects, setting up the vtable, etc. On success, the class is
724 // marked kStatusResolved.
725
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700727 kStatusError = -1,
728 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700729 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700730 kStatusLoaded = 2, // DEX idx values resolved
731 kStatusResolved = 3, // part of linking
732 kStatusVerifying = 4, // in the process of being verified
733 kStatusVerified = 5, // logically part of linking; done pre-init
734 kStatusInitializing = 6, // class init in progress
735 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700736 };
737
738 enum PrimitiveType {
739 kPrimNot = -1
740 };
741
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700742 Class* GetSuperClass() const {
743 return super_class_;
744 }
745
746 uint32_t GetSuperClassIdx() const {
747 return super_class_idx_;
748 }
749
750 bool HasSuperClass() const {
751 return super_class_ != NULL;
752 }
753
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700754 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700755 return class_loader_;
756 }
757
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700758 DexCache* GetDexCache() const {
759 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700760 }
761
762 Class* GetComponentType() const {
763 return component_type_;
764 }
765
766 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700767 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700768 return descriptor_;
769 }
770
771 Status GetStatus() const {
772 return status_;
773 }
774
775 void SetStatus(Status new_status) {
776 // TODO: validate transition
777 status_ = new_status;
778 }
779
Carl Shapiro69759ea2011-07-21 18:13:35 -0700780 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700781 bool IsErroneous() const {
782 return GetStatus() == kStatusError;
783 }
784
Carl Shapiro69759ea2011-07-21 18:13:35 -0700785 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700786 bool IsVerified() const {
787 return GetStatus() >= kStatusVerified;
788 }
789
Carl Shapiro69759ea2011-07-21 18:13:35 -0700790 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791 bool IsLinked() const {
792 return GetStatus() >= kStatusResolved;
793 }
794
Carl Shapiro69759ea2011-07-21 18:13:35 -0700795 bool IsLoaded() const {
796 return GetStatus() >= kStatusLoaded;
797 }
798
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700799 // Returns true if this class is in the same packages as that class.
800 bool IsInSamePackage(const Class* that) const;
801
Ian Rogersb033c752011-07-20 12:22:35 -0700802 static bool IsInSamePackage(const StringPiece& descriptor1,
803 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700804
805 // Returns true if this class represents an array class.
806 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700807 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700808 }
809
810 // Returns true if the class is an interface.
811 bool IsInterface() const {
812 return (access_flags_ & kAccInterface) != 0;
813 }
814
815 // Returns true if the class is declared public.
816 bool IsPublic() const {
817 return (access_flags_ & kAccPublic) != 0;
818 }
819
820 // Returns true if the class is declared final.
821 bool IsFinal() const {
822 return (access_flags_ & kAccFinal) != 0;
823 }
824
825 // Returns true if the class is abstract.
826 bool IsAbstract() const {
827 return (access_flags_ & kAccAbstract) != 0;
828 }
829
830 // Returns true if the class is an annotation.
831 bool IsAnnotation() const {
832 return (access_flags_ & kAccAnnotation) != 0;
833 }
834
835 // Returns true if the class is a primitive type.
836 bool IsPrimitive() const {
837 return primitive_type_ != kPrimNot;
838 }
839
Brian Carlstromae3ac012011-07-27 01:30:28 -0700840 // Returns true if the class is synthetic.
841 bool IsSynthetic() const {
842 return (access_flags_ & kAccSynthetic) != 0;
843 }
844
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700845 // Returns true if this class can access that class.
846 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700847 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700848 }
849
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700850 // Returns the number of static, private, and constructor methods.
851 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700852 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700853 }
854
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700856 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700857 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700858 }
859
860 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700861 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700862 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700863 }
864
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700865 Method* FindDeclaredDirectMethod(const StringPiece& name,
866 const StringPiece& descriptor);
867
868 Method* FindDirectMethod(const StringPiece& name,
869 const StringPiece& descriptor);
870
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 // Returns the number of non-inherited virtual methods.
872 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700873 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700874 }
875
876 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700877 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700878 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700879 }
880
881 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700882 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700883 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700884 }
885
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700886 Method* FindDeclaredVirtualMethod(const StringPiece& name,
887 const StringPiece& descriptor);
888
889 Method* FindVirtualMethod(const StringPiece& name,
890 const StringPiece& descriptor);
891
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700892 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700893 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700894 }
895
Carl Shapiro69759ea2011-07-21 18:13:35 -0700896 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700898 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700899 }
900
901 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700902 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700903 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700904 }
905
906 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700907 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700908 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 }
910
911 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700912 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913 }
914
Carl Shapiro69759ea2011-07-21 18:13:35 -0700915 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700916 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700917 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700918 }
919
920 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700921 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700922 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700923 }
924
925 uint32_t GetReferenceOffsets() const {
926 return reference_offsets_;
927 }
928
929 void SetReferenceOffsets(uint32_t new_reference_offsets) {
930 reference_offsets_ = new_reference_offsets;
931 }
932
Carl Shapiro69759ea2011-07-21 18:13:35 -0700933 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700934 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700935 }
936
937 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700938 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700939 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700940 }
941
942 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700943 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700944 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700945 }
946
Ian Rogersb033c752011-07-20 12:22:35 -0700947 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700948 // leave space for instance data; we could access fields directly if
949 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700950#define CLASS_FIELD_SLOTS 1
951 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700952 uint32_t instance_data_[CLASS_FIELD_SLOTS];
953#undef CLASS_FIELD_SLOTS
954
955 // UTF-8 descriptor for the class from constant pool
956 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700957 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700958
959 // Proxy classes have their descriptor allocated on the native heap.
960 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700961 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700962
963 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700964 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700965
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700966 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700967 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700968 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700969
970 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700971 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972
973 // if class verify fails, we must return same error on subsequent tries
974 Class* verify_error_class_;
975
976 // threadId, used to check for recursive <clinit> invocation
977 uint32_t clinit_thread_id_;
978
979 // Total object size; used when allocating storage on gc heap. (For
980 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700982
983 // For array classes, the class object for base element, for
984 // instanceof/checkcast (for String[][][], this will be String).
985 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700986 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700987
988 // For array classes, the number of array dimensions, e.g. int[][]
989 // is 2. Otherwise 0.
990 int32_t array_rank_;
991
992 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
993 PrimitiveType primitive_type_;
994
995 // The superclass, or NULL if this is java.lang.Object or a
996 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700997 Class* super_class_; // TODO: make an instance field
998 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700999
1000 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001001 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001002
1003 // initiating class loader list
1004 // NOTE: for classes with low serialNumber, these are unused, and the
1005 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001006 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001007
1008 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001009 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001010 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001011
1012 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001013 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014
1015 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001016 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001017
1018 // Virtual method table (vtable), for use by "invoke-virtual". The
1019 // vtable from the superclass is copied in, and virtual methods from
1020 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001021 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001022
1023 // Interface table (iftable), one entry per interface supported by
1024 // this class. That means one entry for each interface we support
1025 // directly, indirectly via superclass, or indirectly via
1026 // superinterface. This will be null if neither we nor our
1027 // superclass implement any interfaces.
1028 //
1029 // Why we need this: given "class Foo implements Face", declare
1030 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1031 // is part of the Face interface. We can't easily use a single
1032 // vtable.
1033 //
1034 // For every interface a concrete class implements, we create a list
1035 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001037 InterfaceEntry* iftable_;
1038
1039 // The interface vtable indices for iftable get stored here. By
1040 // placing them all in a single pool for each class that implements
1041 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001042 size_t ifvi_pool_count_;
1043 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001044
1045 // instance fields
1046 //
1047 // These describe the layout of the contents of a
1048 // DataObject-compatible Object. Note that only the fields directly
1049 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001050 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001051 //
1052 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001053 // the beginning of the field list. num_reference_instance_fields_
1054 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001055 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001056
1057 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001058 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001059
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001060 // Bitmap of offsets of ifields.
1061 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001062
1063 // source file name, if known. Otherwise, NULL.
1064 const char* source_file_;
1065
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001066 ObjectArray<Object>* static_references_;
1067 IntArray* static_32bit_primitives_;
1068 LongArray* static_64bit_primitives_;
1069
Carl Shapiro1fb86202011-06-27 17:43:13 -07001070 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001071 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001072
1073 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001074 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001075};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001076std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001077
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001078inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001079 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001080 return Heap::AllocObject(klass, klass->object_size_);
1081}
1082
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083class DataObject : public Object {
1084 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001085 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001086 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001087 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001088};
1089
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001090template<class T>
1091class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001092 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001093 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1094 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1095 length,
1096 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001097 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001098
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001099 const T* GetData() const {
1100 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001101 }
1102
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001103 T* GetData() {
1104 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001105 }
1106
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001107 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001108 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001109 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001110 }
1111
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001112 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001113 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001114 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001115 }
1116
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001117 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001118 // Location of first element.
1119 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001120
1121 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001122};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001123
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001124class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001125 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001126 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001127 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001128 return array_;
1129 }
1130
Carl Shapirof88c9522011-08-06 15:47:38 -07001131 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001132 return hash_code_;
1133 }
1134
Carl Shapirof88c9522011-08-06 15:47:38 -07001135 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001136 return offset_;
1137 }
1138
Carl Shapirof88c9522011-08-06 15:47:38 -07001139 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001140 return count_;
1141 }
1142
Carl Shapirof88c9522011-08-06 15:47:38 -07001143 uint16_t CharAt(uint32_t index) const {
1144 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001145 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001146 }
1147
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001148 static String* AllocFromUtf16(int32_t utf16_length,
1149 uint16_t* utf16_data_in,
1150 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001151 String* string = Alloc(GetJavaLangString(),
1152 GetCharArrayClass(),
1153 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001154 // TODO use 16-bit wide memset variant
1155 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001156 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001157 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001158 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001159 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001160 }
1161
1162 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1163 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001164 int32_t utf16_length,
1165 const char* utf8_data_in) {
1166 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001167 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001168 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001169 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001170 return string;
1171 }
1172
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001173 // Creates a String of the given ASCII characters. It is an error to call this
1174 // using non-ASCII characters as this function assumes one char per byte.
1175 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001176 return AllocFromModifiedUtf8(GetJavaLangString(),
1177 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001178 strlen(ascii_data_in),
1179 ascii_data_in);
1180 }
1181
Jesse Wilson8989d992011-08-02 13:39:42 -07001182 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1183 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001184 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1185 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001186 }
1187
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001188 static void InitClasses(Class* java_lang_String, Class* char_array);
1189
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001190 static String* Alloc(Class* java_lang_String,
1191 Class* char_array,
1192 int32_t utf16_length) {
1193 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1194 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1195 string->array_ = array;
1196 string->count_ = utf16_length;
1197 return string;
1198 }
1199
1200 // Convert Modified UTF-8 to UTF-16
1201 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1202 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1203 while (*utf8_data_in != '\0') {
1204 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1205 }
1206 }
1207
1208 // Retrieve the next UTF-16 character from a UTF-8 string.
1209 //
1210 // Advances "*pUtf8Ptr" to the start of the next character.
1211 //
1212 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1213 // of a 3-byte sequence, you can end up overrunning the buffer with
1214 // reads (and possibly with the writes if the length was computed and
1215 // cached before the damage). For performance reasons, this function
1216 // assumes that the string being parsed is known to be valid (e.g., by
1217 // already being verified). Most strings we process here are coming
1218 // out of dex files or other internal translations, so the only real
1219 // risk comes from the JNI NewStringUTF call.
1220 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1221 uint8_t one = *(*utf8_data_in)++;
1222 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001223 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001224 return one;
1225 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001226 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001227 uint8_t two = *(*utf8_data_in)++;
1228 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001229 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001230 return ((one & 0x1f) << 6) |
1231 (two & 0x3f);
1232 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001233 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001234 uint8_t three = *(*utf8_data_in)++;
1235 return ((one & 0x0f) << 12) |
1236 ((two & 0x3f) << 6) |
1237 (three & 0x3f);
1238 }
1239
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001240 // Like "strlen", but for strings encoded with "modified" UTF-8.
1241 //
1242 // The value returned is the number of characters, which may or may not
1243 // be the same as the number of bytes.
1244 //
1245 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1246 // get increment {1-3} from table of 8 values.)
1247 static size_t ModifiedUtf8Len(const char* utf8) {
1248 size_t len = 0;
1249 int ic;
1250 while ((ic = *utf8++) != '\0') {
1251 len++;
1252 if ((ic & 0x80) == 0) {
1253 // one-byte encoding
1254 continue;
1255 }
1256 // two- or three-byte encoding
1257 utf8++;
1258 if ((ic & 0x20) == 0) {
1259 // two-byte encoding
1260 continue;
1261 }
1262 // three-byte encoding
1263 utf8++;
1264 }
1265 return len;
1266 }
1267
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001268 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001269 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1270 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001271 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001272 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001273 }
1274 return hash;
1275 }
1276
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001277 void ComputeHashCode() {
1278 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1279 }
1280
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001281 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001282 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001283 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1284 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001285 return false;
1286 }
1287 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001288 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001289 }
1290
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001291 bool Equals(const StringPiece& modified_utf8) const {
1292 // TODO: do not assume C-string representation.
1293 return Equals(modified_utf8.data());
1294 }
1295
1296 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001297 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001298 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299 return false;
1300 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001301 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001302 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001303 return false;
1304 }
1305 }
1306 return true;
1307 }
1308
Carl Shapirof88c9522011-08-06 15:47:38 -07001309 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001310 if (this->GetLength() != that_length) {
1311 return false;
1312 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001313 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001314 if (this->CharAt(i) != that_chars[that_offset + i]) {
1315 return false;
1316 }
1317 }
1318 return true;
1319 }
1320
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001321 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001322 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1323 CharArray* array_;
1324
Carl Shapirof88c9522011-08-06 15:47:38 -07001325 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001326
Carl Shapirof88c9522011-08-06 15:47:38 -07001327 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001328
Carl Shapirof88c9522011-08-06 15:47:38 -07001329 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001330
1331 static Class* GetJavaLangString() {
1332 DCHECK(java_lang_String_ != NULL);
1333 return java_lang_String_;
1334 }
1335 static Class* GetCharArrayClass() {
1336 DCHECK(char_array_ != NULL);
1337 return char_array_;
1338 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001339
1340 static Class* java_lang_String_;
1341 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001342
1343 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001344};
1345
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001346class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001347 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001348 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1349 }
1350
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001351 Class* GetClass() const {
1352 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001353 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001354
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001355 void SetClass(Class* klass) {
1356 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001357 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001358
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001359 private:
1360 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001361 Class* klass_;
1362
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001363 public: // TODO: private
1364 // Index into array of vtable offsets. This points into the
1365 // ifviPool, which holds the vtables for all interfaces declared by
1366 // this class.
1367 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001368
1369 private:
1370 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001371};
1372
1373} // namespace art
1374
1375#endif // ART_SRC_OBJECT_H_