blob: 0d5ce65c307e6bf3521a07bace0dc3a93050273e [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
Carl Shapirof88c9522011-08-06 15:47:38 -0700606 const void* GetData() const {
607 return &elements_;
608 }
609
610 void* GetData() {
611 return &elements_;
612 }
613
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700614 private:
615 // The number of array elements.
616 uint32_t length_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700617
618 // Location of first element.
619 uint32_t elements_[0];
620
621 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700622};
623
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700624template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700625class ObjectArray : public Array {
626 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700627 static ObjectArray<T>* Alloc(Class* object_array_class,
628 size_t length) {
629 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
630 length,
631 sizeof(uint32_t)));
632 }
633
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700634 T* const * GetData() const {
635 return reinterpret_cast<T* const *>(&elements_);
636 }
637 T** GetData() {
638 return reinterpret_cast<T**>(&elements_);
639 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700640 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700641 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700642 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700643 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700644
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700645 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700646 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700647 GetData()[i] = object;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700648 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700649
650 static void Copy(ObjectArray<T>* src, int src_pos,
651 ObjectArray<T>* dst, int dst_pos,
652 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700653 for (size_t i = 0; i < length; i++) {
654 dst->Set(dst_pos + i, src->Get(src_pos + i));
655 }
656 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700657
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700658 ObjectArray<T>* CopyOf(size_t new_length) {
659 ObjectArray<T>* new_array = Alloc(klass_, new_length);
660 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
661 return new_array;
662 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700663
664 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700665 // Location of first element.
666 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700667
668 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700669};
670
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700671// ClassLoader objects.
672class ClassLoader : public Object {
673 public:
674 std::vector<const DexFile*>& GetClassPath() {
675 return class_path_;
676 }
677 void SetClassPath(std::vector<const DexFile*>& class_path) {
678 DCHECK_EQ(0U, class_path_.size());
679 class_path_ = class_path;
680 }
681
682 private:
683 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
684 Object* packages_;
685 ClassLoader* parent_;
686
687 // TODO remove once we can create a real PathClassLoader
688 std::vector<const DexFile*> class_path_;
689
Carl Shapirof88c9522011-08-06 15:47:38 -0700690 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700691};
692
693class BaseDexClassLoader : public ClassLoader {
694 private:
695 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
696 String* original_path_;
697 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700698 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700699};
700
701class PathClassLoader : public BaseDexClassLoader {
702 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700703 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700704};
705
Carl Shapiro1fb86202011-06-27 17:43:13 -0700706// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700707class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700708 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700709
710 // Class Status
711 //
712 // kStatusNotReady: If a Class cannot be found in the class table by
713 // FindClass, it allocates an new one with AllocClass in the
714 // kStatusNotReady and calls LoadClass. Note if it does find a
715 // class, it may not be kStatusResolved and it will try to push it
716 // forward toward kStatusResolved.
717 //
718 // kStatusIdx: LoadClass populates with Class with information from
719 // the DexFile, moving the status to kStatusIdx, indicating that the
720 // Class values in super_class_ and interfaces_ have not been
721 // populated based on super_class_idx_ and interfaces_idx_. The new
722 // Class can then be inserted into the classes table.
723 //
724 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
725 // attempt to move a kStatusIdx class forward to kStatusLoaded by
726 // using ResolveClass to initialize the super_class_ and interfaces_.
727 //
728 // kStatusResolved: Still holding the lock on Class, the ClassLinker
729 // will use LinkClass to link all members, creating Field and Method
730 // objects, setting up the vtable, etc. On success, the class is
731 // marked kStatusResolved.
732
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700733 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700734 kStatusError = -1,
735 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700736 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700737 kStatusLoaded = 2, // DEX idx values resolved
738 kStatusResolved = 3, // part of linking
739 kStatusVerifying = 4, // in the process of being verified
740 kStatusVerified = 5, // logically part of linking; done pre-init
741 kStatusInitializing = 6, // class init in progress
742 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700743 };
744
745 enum PrimitiveType {
746 kPrimNot = -1
747 };
748
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700749 Class* GetSuperClass() const {
750 return super_class_;
751 }
752
753 uint32_t GetSuperClassIdx() const {
754 return super_class_idx_;
755 }
756
757 bool HasSuperClass() const {
758 return super_class_ != NULL;
759 }
760
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700761 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700762 return class_loader_;
763 }
764
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700765 DexCache* GetDexCache() const {
766 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 }
768
769 Class* GetComponentType() const {
770 return component_type_;
771 }
772
773 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700774 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700775 return descriptor_;
776 }
777
778 Status GetStatus() const {
779 return status_;
780 }
781
782 void SetStatus(Status new_status) {
783 // TODO: validate transition
784 status_ = new_status;
785 }
786
Carl Shapiro69759ea2011-07-21 18:13:35 -0700787 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700788 bool IsErroneous() const {
789 return GetStatus() == kStatusError;
790 }
791
Carl Shapiro69759ea2011-07-21 18:13:35 -0700792 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700793 bool IsVerified() const {
794 return GetStatus() >= kStatusVerified;
795 }
796
Carl Shapiro69759ea2011-07-21 18:13:35 -0700797 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700798 bool IsLinked() const {
799 return GetStatus() >= kStatusResolved;
800 }
801
Carl Shapiro69759ea2011-07-21 18:13:35 -0700802 bool IsLoaded() const {
803 return GetStatus() >= kStatusLoaded;
804 }
805
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700806 // Returns true if this class is in the same packages as that class.
807 bool IsInSamePackage(const Class* that) const;
808
Ian Rogersb033c752011-07-20 12:22:35 -0700809 static bool IsInSamePackage(const StringPiece& descriptor1,
810 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700811
812 // Returns true if this class represents an array class.
813 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700814 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700815 }
816
817 // Returns true if the class is an interface.
818 bool IsInterface() const {
819 return (access_flags_ & kAccInterface) != 0;
820 }
821
822 // Returns true if the class is declared public.
823 bool IsPublic() const {
824 return (access_flags_ & kAccPublic) != 0;
825 }
826
827 // Returns true if the class is declared final.
828 bool IsFinal() const {
829 return (access_flags_ & kAccFinal) != 0;
830 }
831
832 // Returns true if the class is abstract.
833 bool IsAbstract() const {
834 return (access_flags_ & kAccAbstract) != 0;
835 }
836
837 // Returns true if the class is an annotation.
838 bool IsAnnotation() const {
839 return (access_flags_ & kAccAnnotation) != 0;
840 }
841
842 // Returns true if the class is a primitive type.
843 bool IsPrimitive() const {
844 return primitive_type_ != kPrimNot;
845 }
846
Brian Carlstromae3ac012011-07-27 01:30:28 -0700847 // Returns true if the class is synthetic.
848 bool IsSynthetic() const {
849 return (access_flags_ & kAccSynthetic) != 0;
850 }
851
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700852 // Returns true if this class can access that class.
853 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700854 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700855 }
856
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700857 // Returns the number of static, private, and constructor methods.
858 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700859 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700860 }
861
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700862 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700863 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700864 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700865 }
866
867 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700868 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700869 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700870 }
871
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700872 Method* FindDeclaredDirectMethod(const StringPiece& name,
873 const StringPiece& descriptor);
874
875 Method* FindDirectMethod(const StringPiece& name,
876 const StringPiece& descriptor);
877
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700878 // Returns the number of non-inherited virtual methods.
879 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700880 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700881 }
882
883 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700884 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700885 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700886 }
887
888 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700889 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700890 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700891 }
892
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700893 Method* FindDeclaredVirtualMethod(const StringPiece& name,
894 const StringPiece& descriptor);
895
896 Method* FindVirtualMethod(const StringPiece& name,
897 const StringPiece& descriptor);
898
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700899 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700900 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700901 }
902
Carl Shapiro69759ea2011-07-21 18:13:35 -0700903 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700905 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 }
907
908 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700909 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700910 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700911 }
912
913 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700914 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700915 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 }
917
918 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700919 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700920 }
921
Carl Shapiro69759ea2011-07-21 18:13:35 -0700922 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700923 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700924 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700925 }
926
927 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700928 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700929 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700930 }
931
932 uint32_t GetReferenceOffsets() const {
933 return reference_offsets_;
934 }
935
936 void SetReferenceOffsets(uint32_t new_reference_offsets) {
937 reference_offsets_ = new_reference_offsets;
938 }
939
Carl Shapiro69759ea2011-07-21 18:13:35 -0700940 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700941 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700942 }
943
944 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700945 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700946 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700947 }
948
949 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700950 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700951 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700952 }
953
Ian Rogersb033c752011-07-20 12:22:35 -0700954 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700955 // leave space for instance data; we could access fields directly if
956 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700957#define CLASS_FIELD_SLOTS 1
958 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700959 uint32_t instance_data_[CLASS_FIELD_SLOTS];
960#undef CLASS_FIELD_SLOTS
961
962 // UTF-8 descriptor for the class from constant pool
963 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700964 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700965
966 // Proxy classes have their descriptor allocated on the native heap.
967 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700968 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700969
970 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700971 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700973 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700974 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700975 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700976
977 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700979
980 // if class verify fails, we must return same error on subsequent tries
981 Class* verify_error_class_;
982
983 // threadId, used to check for recursive <clinit> invocation
984 uint32_t clinit_thread_id_;
985
986 // Total object size; used when allocating storage on gc heap. (For
987 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700989
990 // For array classes, the class object for base element, for
991 // instanceof/checkcast (for String[][][], this will be String).
992 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700993 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700994
995 // For array classes, the number of array dimensions, e.g. int[][]
996 // is 2. Otherwise 0.
997 int32_t array_rank_;
998
999 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1000 PrimitiveType primitive_type_;
1001
1002 // The superclass, or NULL if this is java.lang.Object or a
1003 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001004 Class* super_class_; // TODO: make an instance field
1005 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001006
1007 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001008 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001009
1010 // initiating class loader list
1011 // NOTE: for classes with low serialNumber, these are unused, and the
1012 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001013 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014
1015 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001016 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001017 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001018
1019 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001020 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001021
1022 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001023 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001024
1025 // Virtual method table (vtable), for use by "invoke-virtual". The
1026 // vtable from the superclass is copied in, and virtual methods from
1027 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001028 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001029
1030 // Interface table (iftable), one entry per interface supported by
1031 // this class. That means one entry for each interface we support
1032 // directly, indirectly via superclass, or indirectly via
1033 // superinterface. This will be null if neither we nor our
1034 // superclass implement any interfaces.
1035 //
1036 // Why we need this: given "class Foo implements Face", declare
1037 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1038 // is part of the Face interface. We can't easily use a single
1039 // vtable.
1040 //
1041 // For every interface a concrete class implements, we create a list
1042 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001043 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001044 InterfaceEntry* iftable_;
1045
1046 // The interface vtable indices for iftable get stored here. By
1047 // placing them all in a single pool for each class that implements
1048 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049 size_t ifvi_pool_count_;
1050 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001051
1052 // instance fields
1053 //
1054 // These describe the layout of the contents of a
1055 // DataObject-compatible Object. Note that only the fields directly
1056 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001057 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001058 //
1059 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001060 // the beginning of the field list. num_reference_instance_fields_
1061 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001062 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001063
1064 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001065 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001066
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067 // Bitmap of offsets of ifields.
1068 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001069
1070 // source file name, if known. Otherwise, NULL.
1071 const char* source_file_;
1072
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001073 ObjectArray<Object>* static_references_;
1074 IntArray* static_32bit_primitives_;
1075 LongArray* static_64bit_primitives_;
1076
Carl Shapiro1fb86202011-06-27 17:43:13 -07001077 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001078 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001079
1080 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001081 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001082};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001083std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001084
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001085inline Object* Object::Alloc(Class* klass) {
Jesse Wilson14150742011-07-29 19:04:44 -04001086 DCHECK(klass != NULL);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001087 return Heap::AllocObject(klass, klass->object_size_);
1088}
1089
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001090class DataObject : public Object {
1091 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001092 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001093 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001094 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001095};
1096
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001097template<class T>
1098class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001099 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001100 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1101 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1102 length,
1103 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001104 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001105
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001106 const T* GetData() const {
1107 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001108 }
1109
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001110 T* GetData() {
1111 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001112 }
1113
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001114 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001115 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001116 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001117 }
1118
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001119 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001120 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001121 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001122 }
1123
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001124 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001125 // Location of first element.
1126 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001127
1128 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001129};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001130
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001131class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001132 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001133 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001134 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001135 return array_;
1136 }
1137
Carl Shapirof88c9522011-08-06 15:47:38 -07001138 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001139 return hash_code_;
1140 }
1141
Carl Shapirof88c9522011-08-06 15:47:38 -07001142 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001143 return offset_;
1144 }
1145
Carl Shapirof88c9522011-08-06 15:47:38 -07001146 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001147 return count_;
1148 }
1149
Carl Shapirof88c9522011-08-06 15:47:38 -07001150 uint16_t CharAt(uint32_t index) const {
1151 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001152 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001153 }
1154
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001155 static String* AllocFromUtf16(int32_t utf16_length,
1156 uint16_t* utf16_data_in,
1157 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001158 String* string = Alloc(GetJavaLangString(),
1159 GetCharArrayClass(),
1160 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001161 // TODO use 16-bit wide memset variant
1162 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001163 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001164 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001165 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001166 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001167 }
1168
1169 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1170 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001171 int32_t utf16_length,
1172 const char* utf8_data_in) {
1173 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001174 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001175 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001176 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001177 return string;
1178 }
1179
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001180 // Creates a String of the given ASCII characters. It is an error to call this
1181 // using non-ASCII characters as this function assumes one char per byte.
1182 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001183 return AllocFromModifiedUtf8(GetJavaLangString(),
1184 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001185 strlen(ascii_data_in),
1186 ascii_data_in);
1187 }
1188
Jesse Wilson8989d992011-08-02 13:39:42 -07001189 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1190 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001191 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1192 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001193 }
1194
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001195 static void InitClasses(Class* java_lang_String, Class* char_array);
1196
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001197 static String* Alloc(Class* java_lang_String,
1198 Class* char_array,
1199 int32_t utf16_length) {
1200 String* string = down_cast<String*>(Object::Alloc(java_lang_String));
1201 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1202 string->array_ = array;
1203 string->count_ = utf16_length;
1204 return string;
1205 }
1206
1207 // Convert Modified UTF-8 to UTF-16
1208 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1209 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1210 while (*utf8_data_in != '\0') {
1211 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1212 }
1213 }
1214
1215 // Retrieve the next UTF-16 character from a UTF-8 string.
1216 //
1217 // Advances "*pUtf8Ptr" to the start of the next character.
1218 //
1219 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1220 // of a 3-byte sequence, you can end up overrunning the buffer with
1221 // reads (and possibly with the writes if the length was computed and
1222 // cached before the damage). For performance reasons, this function
1223 // assumes that the string being parsed is known to be valid (e.g., by
1224 // already being verified). Most strings we process here are coming
1225 // out of dex files or other internal translations, so the only real
1226 // risk comes from the JNI NewStringUTF call.
1227 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1228 uint8_t one = *(*utf8_data_in)++;
1229 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001230 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001231 return one;
1232 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001233 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001234 uint8_t two = *(*utf8_data_in)++;
1235 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001236 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001237 return ((one & 0x1f) << 6) |
1238 (two & 0x3f);
1239 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001240 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001241 uint8_t three = *(*utf8_data_in)++;
1242 return ((one & 0x0f) << 12) |
1243 ((two & 0x3f) << 6) |
1244 (three & 0x3f);
1245 }
1246
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001247 // Like "strlen", but for strings encoded with "modified" UTF-8.
1248 //
1249 // The value returned is the number of characters, which may or may not
1250 // be the same as the number of bytes.
1251 //
1252 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1253 // get increment {1-3} from table of 8 values.)
1254 static size_t ModifiedUtf8Len(const char* utf8) {
1255 size_t len = 0;
1256 int ic;
1257 while ((ic = *utf8++) != '\0') {
1258 len++;
1259 if ((ic & 0x80) == 0) {
1260 // one-byte encoding
1261 continue;
1262 }
1263 // two- or three-byte encoding
1264 utf8++;
1265 if ((ic & 0x20) == 0) {
1266 // two-byte encoding
1267 continue;
1268 }
1269 // three-byte encoding
1270 utf8++;
1271 }
1272 return len;
1273 }
1274
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001275 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001276 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1277 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001278 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001279 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001280 }
1281 return hash;
1282 }
1283
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001284 void ComputeHashCode() {
1285 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1286 }
1287
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001288 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001289 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001290 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1291 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001292 return false;
1293 }
1294 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001295 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001296 }
1297
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001298 bool Equals(const StringPiece& modified_utf8) const {
1299 // TODO: do not assume C-string representation.
1300 return Equals(modified_utf8.data());
1301 }
1302
1303 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001304 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001305 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001306 return false;
1307 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001308 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001309 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001310 return false;
1311 }
1312 }
1313 return true;
1314 }
1315
Carl Shapirof88c9522011-08-06 15:47:38 -07001316 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001317 if (this->GetLength() != that_length) {
1318 return false;
1319 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001320 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001321 if (this->CharAt(i) != that_chars[that_offset + i]) {
1322 return false;
1323 }
1324 }
1325 return true;
1326 }
1327
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001328 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001329 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1330 CharArray* array_;
1331
Carl Shapirof88c9522011-08-06 15:47:38 -07001332 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001333
Carl Shapirof88c9522011-08-06 15:47:38 -07001334 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001335
Carl Shapirof88c9522011-08-06 15:47:38 -07001336 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001337
1338 static Class* GetJavaLangString() {
1339 DCHECK(java_lang_String_ != NULL);
1340 return java_lang_String_;
1341 }
1342 static Class* GetCharArrayClass() {
1343 DCHECK(char_array_ != NULL);
1344 return char_array_;
1345 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001346
1347 static Class* java_lang_String_;
1348 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001349
1350 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001351};
1352
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001353class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001354 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001355 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1356 }
1357
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001358 Class* GetClass() const {
1359 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001360 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001361
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001362 void SetClass(Class* klass) {
1363 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001364 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001365
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001366 private:
1367 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001368 Class* klass_;
1369
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001370 public: // TODO: private
1371 // Index into array of vtable offsets. This points into the
1372 // ifviPool, which holds the vtables for all interfaces declared by
1373 // this class.
1374 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001375
1376 private:
1377 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001378};
1379
1380} // namespace art
1381
1382#endif // ART_SRC_OBJECT_H_