blob: bf965dff52124430feea622ec91ae63f12210548 [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"
9#include "logging.h"
10#include "macros.h"
11#include "offsets.h"
12#include "stringpiece.h"
13#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070014
15namespace art {
16
17class Array;
18class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070019class DexCache;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020class InstanceField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070021class InterfaceEntry;
22class Monitor;
23class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070024class Object;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070025template<class T> class ObjectArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070027
Carl Shapiro3ee755d2011-06-28 12:11:04 -070028union JValue {
29 uint8_t z;
30 int8_t b;
31 uint16_t c;
32 int16_t s;
33 int32_t i;
34 int64_t j;
35 float f;
36 double d;
37 Object* l;
38};
39
Brian Carlstrombe977852011-07-19 14:54:54 -070040static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
41static const uint32_t kAccPrivate = 0x0002; // field, method, ic
42static const uint32_t kAccProtected = 0x0004; // field, method, ic
43static const uint32_t kAccStatic = 0x0008; // field, method, ic
44static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
45static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
46static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
47static const uint32_t kAccVolatile = 0x0040; // field
48static const uint32_t kAccBridge = 0x0040; // method (1.5)
49static const uint32_t kAccTransient = 0x0080; // field
50static const uint32_t kAccVarargs = 0x0080; // method (1.5)
51static const uint32_t kAccNative = 0x0100; // method
52static const uint32_t kAccInterface = 0x0200; // class, ic
53static const uint32_t kAccAbstract = 0x0400; // class, method, ic
54static const uint32_t kAccStrict = 0x0800; // method
55static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
56static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
57static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070058
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070059static const uint32_t kAccMiranda = 0x8000; // method
60
Brian Carlstroma331b3c2011-07-18 17:47:56 -070061static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
62
Brian Carlstrombe977852011-07-19 14:54:54 -070063static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
64static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070065
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068 */
69/*
70 * A magic value for refOffsets. Ignore the bits and walk the super
71 * chain when this is the value.
72 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
73 * fields followed by 2 ref instance fields.]
74 */
75#define CLASS_WALK_SUPER ((unsigned int)(3))
76#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
77#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
78#define CLASS_OFFSET_ALIGNMENT 4
79#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
80/*
81 * Given an offset, return the bit number which would encode that offset.
82 * Local use only.
83 */
84#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
85 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
86 CLASS_OFFSET_ALIGNMENT)
87/*
88 * Is the given offset too large to be encoded?
89 */
90#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
91 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
92/*
93 * Return a single bit, encoding the offset.
94 * Undefined if the offset is too large, as defined above.
95 */
96#define CLASS_BIT_FROM_OFFSET(byteOffset) \
97 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
98/*
99 * Return an offset, given a bit number as returned from CLZ.
100 */
101#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700102 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700103
104
Carl Shapiro1fb86202011-06-27 17:43:13 -0700105class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700106 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107 Class* GetClass() const {
108 return klass_;
109 }
110
111 void MonitorEnter() {
112 monitor_->Enter();
113 }
114
115 void MonitorExit() {
116 monitor_->Exit();
117 }
118
119 void Notify() {
120 monitor_->Notify();
121 }
122
123 void NotifyAll() {
124 monitor_->NotifyAll();
125 }
126
127 void Wait() {
128 monitor_->Wait();
129 }
130
131 void Wait(int64_t timeout) {
132 monitor_->Wait(timeout);
133 }
134
135 void Wait(int64_t timeout, int32_t nanos) {
136 monitor_->Wait(timeout, nanos);
137 }
138
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139 const Object* GetFieldObject(size_t field_offset) const {
140 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
141 return *reinterpret_cast<Object* const*>(raw_addr);
142 }
143
144 Object* GetFieldObject(size_t field_offset) {
145 return const_cast<Object*>(GetFieldObject(field_offset));
146 }
147
148 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
150 *reinterpret_cast<Object**>(raw_addr) = new_value;
151 // TODO: write barrier
152 }
153
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 bool IsClass() const {
155 LOG(FATAL) << "Unimplemented";
156 return true;
157 }
158
159 Class* AsClass() {
160 return down_cast<Class*>(this);
161 }
162
163 const Class* AsClass() const {
164 return down_cast<const Class*>(this);
165 }
166
167 bool IsObjectArray() const {
168 LOG(FATAL) << "Unimplemented";
169 return true;
170 }
171
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700172 const ObjectArray<Object>* AsObjectArray() const {
173 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700174 }
175
176 bool IsReference() const {
177 LOG(FATAL) << "Unimplemented";
178 return true;
179 }
180
181 bool IsWeakReference() const {
182 LOG(FATAL) << "Unimplemented";
183 return true;
184 }
185
186 bool IsSoftReference() const {
187 LOG(FATAL) << "Unimplemented";
188 return true;
189 }
190
191 bool IsFinalizerReference() const {
192 LOG(FATAL) << "Unimplemented";
193 return true;
194 }
195
196 bool IsPhantomReference() const {
197 LOG(FATAL) << "Unimplemented";
198 return true;
199 }
200
201 bool IsArray() const {
202 LOG(FATAL) << "Unimplemented";
203 return true;
204 }
205
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700207 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 Monitor* monitor_;
210
211 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700212 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 DISALLOW_COPY_AND_ASSIGN(Object);
214};
215
216class ObjectLock {
217 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700218 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219 CHECK(object != NULL);
220 obj_->MonitorEnter();
221 }
222
223 ~ObjectLock() {
224 obj_->MonitorExit();
225 }
226
227 void Wait(int64_t millis = 0) {
228 return obj_->Wait(millis);
229 }
230
231 void Notify() {
232 obj_->Notify();
233 }
234
235 void NotifyAll() {
236 obj_->NotifyAll();
237 }
238
239 private:
240 Object* obj_;
241 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700242};
243
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700244class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700245 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700246 Class* GetDeclaringClass() const {
247 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700248 }
249
250 const char* GetName() const {
251 return name_;
252 }
253
254 char GetType() const { // TODO: return type
255 return signature_[0];
256 }
257
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700258 const char* GetSignature() const {
259 return signature_;
260 }
261
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700263#define FIELD_FIELD_SLOTS 1+6
264 // AccessibleObject #0 flag
265 // Field #0 declaringClass
266 // Field #1 genericType
267 // Field #2 genericTypesAreInitialized
268 // Field #3 name
269 // Field #4 slot
270 // Field #5 type
271 uint32_t instance_data_[FIELD_FIELD_SLOTS];
272#undef FIELD_FIELD_SLOTS
273
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700274 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700275 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700276
277 const char* name_;
278
279 // e.g. "I", "[C", "Landroid/os/Debug;"
280 const char* signature_;
281
282 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700283
284 private:
285 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700286};
287
288// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700289class InstanceField : public Field {
290 public:
291 uint32_t GetOffset() const {
292 return offset_;
293 }
294
295 void SetOffset(size_t num_bytes) {
296 offset_ = num_bytes;
297 }
298
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299 private:
300 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700301 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700302};
303
304// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700305class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700306 public:
307 void SetBoolean(bool z) {
308 CHECK_EQ(GetType(), 'Z');
309 value_.z = z;
310 }
311
312 void SetByte(int8_t b) {
313 CHECK_EQ(GetType(), 'B');
314 value_.b = b;
315 }
316
317 void SetChar(uint16_t c) {
318 CHECK_EQ(GetType(), 'C');
319 value_.c = c;
320 }
321
322 void SetShort(uint16_t s) {
323 CHECK_EQ(GetType(), 'S');
324 value_.s = s;
325 }
326
327 void SetInt(int32_t i) {
328 CHECK_EQ(GetType(), 'I');
329 value_.i = i;
330 }
331
332 int64_t GetLong() {
333 CHECK_EQ(GetType(), 'J');
334 return value_.j;
335 }
336
337 void SetLong(int64_t j) {
338 CHECK_EQ(GetType(), 'J');
339 value_.j = j;
340 }
341
342 void SetFloat(float f) {
343 CHECK_EQ(GetType(), 'F');
344 value_.f = f;
345 }
346
347 void SetDouble(double d) {
348 CHECK_EQ(GetType(), 'D');
349 value_.d = d;
350 }
351
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352 Object* GetObject() {
353 return value_.l;
354 }
355
356 const Object* GetObject() const {
357 return value_.l;
358 }
359
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700360 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700361 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700362 value_.l = l;
363 // TODO: write barrier
364 }
365
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700367 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700368 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700369};
370
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700371class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372 public:
373 // Returns the method name.
374 // TODO: example
375 const StringPiece& GetName() const {
376 return name_;
377 }
378
Brian Carlstroma0808032011-07-18 00:39:23 -0700379 Class* GetDeclaringClass() const {
380 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700381 }
382
Ian Rogersb033c752011-07-20 12:22:35 -0700383 static MemberOffset ClassOffset() {
384 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
385 }
386
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700388 // return FindDexFile(declaring_class_->GetDexCache()
Ian Rogersb033c752011-07-20 12:22:35 -0700389 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 // }
391
392 // Returns true if the method is declared public.
393 bool IsPublic() const {
394 return (access_flags_ & kAccPublic) != 0;
395 }
396
397 // Returns true if the method is declared private.
398 bool IsPrivate() const {
399 return (access_flags_ & kAccPrivate) != 0;
400 }
401
402 // Returns true if the method is declared static.
403 bool IsStatic() const {
404 return (access_flags_ & kAccStatic) != 0;
405 }
406
407 // Returns true if the method is declared synchronized.
408 bool IsSynchronized() const {
409 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
410 return (access_flags_ & synchonized) != 0;
411 }
412
413 // Returns true if the method is declared final.
414 bool IsFinal() const {
415 return (access_flags_ & kAccFinal) != 0;
416 }
417
418 // Returns true if the method is declared native.
419 bool IsNative() const {
420 return (access_flags_ & kAccNative) != 0;
421 }
422
423 // Returns true if the method is declared abstract.
424 bool IsAbstract() const {
425 return (access_flags_ & kAccAbstract) != 0;
426 }
427
428 bool IsSynthetic() const {
429 return (access_flags_ & kAccSynthetic) != 0;
430 }
431
432 // Number of argument registers required by the prototype.
433 uint32_t NumArgRegisters();
434
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700436#define METHOD_FIELD_SLOTS 1+11
437 // AccessibleObject #0 flag
438 // Method #0 declaringClass
439 // Method #1 exceptionTypes
440 // Method #2 formalTypeParameters
441 // Method #3 genericExceptionTypes
442 // Method #4 genericParameterTypes
443 // Method #5 genericReturnType
444 // Method #6 genericTypesAreInitialized
445 // Method #7 name
446 // Method #8 parameterTypes
447 // Method #9 returnType
448 // Method #10 slot
449 uint32_t instance_data_[METHOD_FIELD_SLOTS];
450#undef METHOD_FIELD_SLOTS
451
Ian Rogersb033c752011-07-20 12:22:35 -0700452 bool IsReturnAReference() const {
453 return (shorty_[0] == 'L') || (shorty_[0] == '[');
454 }
455
456 bool IsReturnAFloatOrDouble() const {
457 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
458 }
459
460 bool IsReturnAFloat() const {
461 return shorty_[0] == 'F';
462 }
463
464 bool IsReturnADouble() const {
465 return shorty_[0] == 'D';
466 }
467
468 bool IsReturnALong() const {
469 return shorty_[0] == 'J';
470 }
471
Ian Rogers45a76cb2011-07-21 22:00:15 -0700472 bool IsReturnVoid() const {
473 return shorty_[0] == 'V';
474 }
475
Ian Rogersb033c752011-07-20 12:22:35 -0700476 // The number of arguments that should be supplied to this method
477 size_t NumArgs() const {
478 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
479 }
480
481 // The number of reference arguments to this method including implicit this
482 // pointer
483 size_t NumReferenceArgs() const;
484
485 // The number of long or double arguments
486 size_t NumLongOrDoubleArgs() const;
487
488 // The number of reference arguments to this method before the given
489 // parameter index
490 size_t NumReferenceArgsBefore(unsigned int param) const;
491
492 // Is the given method parameter a reference?
493 bool IsParamAReference(unsigned int param) const;
494
495 // Is the given method parameter a long or double?
496 bool IsParamALongOrDouble(unsigned int param) const;
497
Ian Rogersdf20fe02011-07-20 20:34:16 -0700498 // Size in bytes of the given parameter
499 size_t ParamSize(unsigned int param) const;
500
501 // Size in bytes of the return value
502 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700503
504 void SetCode(const void* code) {
505 code_ = code;
506 }
507
508 const void* GetCode() const {
509 return code_;
510 }
511
512 void RegisterNative(const void* native_method) {
513 native_method_ = native_method;
514 }
515
516 static MemberOffset NativeMethodOffset() {
517 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
518 }
519
520 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700521 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700522 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700523
524 // access flags; low 16 bits are defined by spec (could be uint16_t?)
525 uint32_t access_flags_;
526
527 // For concrete virtual methods, this is the offset of the method
528 // in "vtable".
529 //
530 // For abstract methods in an interface class, this is the offset
531 // of the method in "iftable[n]->methodIndexArray".
532 uint16_t method_index_;
533
534 // Method bounds; not needed for an abstract method.
535 //
536 // For a native method, we compute the size of the argument list, and
537 // set "insSize" and "registerSize" equal to it.
538 uint16_t num_registers_; // ins + locals
539 uint16_t num_outs_;
540 uint16_t num_ins_;
541
542 // method name, e.g. "<init>" or "eatLunch"
543 StringPiece name_;
544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 // Method prototype descriptor string (return and argument types).
546 uint32_t proto_idx_;
547
548 // The short-form method descriptor string.
549 StringPiece shorty_;
550
551 // A pointer to the memory-mapped DEX code.
552 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700553
554 private:
555 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700556
557 // Compiled code associated with this method
558 const void* code_;
559
560 // Any native method registered with this method
561 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562};
563
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700564class Array : public Object {
565 public:
566 uint32_t GetLength() const {
567 return length_;
568 }
569 void SetLength(uint32_t length) {
570 length_ = length;
571 }
572 const void* GetData() const {
573 return &elements_;
574 }
575 void* GetData() {
576 return &elements_;
577 }
578
579 private:
580 // The number of array elements.
581 uint32_t length_;
582 // Location of first element.
583 uint32_t elements_[0];
584 Array();
585};
586
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700587template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700588class ObjectArray : public Array {
589 public:
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700590 C* Get(uint32_t i) const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700591 DCHECK_LT(i, GetLength());
592 Object* const * data = reinterpret_cast<Object* const *>(GetData());
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700593 return down_cast<C*>(data[i]);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700594 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700595 void Set(uint32_t i, C* object) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700596 DCHECK_LT(i, GetLength());
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700597 C** data = reinterpret_cast<C**>(GetData());
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700598 data[i] = object;
599 }
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700600 static void Copy(ObjectArray<C>* src, int src_pos, ObjectArray<C>* dst, int dst_pos, size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700601 for (size_t i = 0; i < length; i++) {
602 dst->Set(dst_pos + i, src->Get(src_pos + i));
603 }
604 }
605
606 private:
607 ObjectArray();
608};
609
Carl Shapiro1fb86202011-06-27 17:43:13 -0700610// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700611class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700612 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700613 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700614 kStatusError = -1,
615 kStatusNotReady = 0,
616 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
617 kStatusLoaded = 2, // DEX idx values resolved
618 kStatusResolved = 3, // part of linking
619 kStatusVerifying = 4, // in the process of being verified
620 kStatusVerified = 5, // logically part of linking; done pre-init
621 kStatusInitializing = 6, // class init in progress
622 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700623 };
624
625 enum PrimitiveType {
626 kPrimNot = -1
627 };
628
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700629 Class* GetSuperClass() const {
630 return super_class_;
631 }
632
633 uint32_t GetSuperClassIdx() const {
634 return super_class_idx_;
635 }
636
637 bool HasSuperClass() const {
638 return super_class_ != NULL;
639 }
640
641 Object* GetClassLoader() const {
642 return class_loader_;
643 }
644
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700645 DexCache* GetDexCache() const {
646 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700647 }
648
649 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700650 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700651 return component_type_;
652 }
653
654 const StringPiece& GetDescriptor() const {
655 return descriptor_;
656 }
657
658 Status GetStatus() const {
659 return status_;
660 }
661
662 void SetStatus(Status new_status) {
663 // TODO: validate transition
664 status_ = new_status;
665 }
666
Carl Shapiro69759ea2011-07-21 18:13:35 -0700667 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700668 bool IsErroneous() const {
669 return GetStatus() == kStatusError;
670 }
671
Carl Shapiro69759ea2011-07-21 18:13:35 -0700672 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700673 bool IsVerified() const {
674 return GetStatus() >= kStatusVerified;
675 }
676
Carl Shapiro69759ea2011-07-21 18:13:35 -0700677 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700678 bool IsLinked() const {
679 return GetStatus() >= kStatusResolved;
680 }
681
Carl Shapiro69759ea2011-07-21 18:13:35 -0700682 bool IsLoaded() const {
683 return GetStatus() >= kStatusLoaded;
684 }
685
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700686 // Returns true if this class is in the same packages as that class.
687 bool IsInSamePackage(const Class* that) const;
688
Ian Rogersb033c752011-07-20 12:22:35 -0700689 static bool IsInSamePackage(const StringPiece& descriptor1,
690 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700691
692 // Returns true if this class represents an array class.
693 bool IsArray() const {
694 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
695 }
696
697 // Returns true if the class is an interface.
698 bool IsInterface() const {
699 return (access_flags_ & kAccInterface) != 0;
700 }
701
702 // Returns true if the class is declared public.
703 bool IsPublic() const {
704 return (access_flags_ & kAccPublic) != 0;
705 }
706
707 // Returns true if the class is declared final.
708 bool IsFinal() const {
709 return (access_flags_ & kAccFinal) != 0;
710 }
711
712 // Returns true if the class is abstract.
713 bool IsAbstract() const {
714 return (access_flags_ & kAccAbstract) != 0;
715 }
716
717 // Returns true if the class is an annotation.
718 bool IsAnnotation() const {
719 return (access_flags_ & kAccAnnotation) != 0;
720 }
721
722 // Returns true if the class is a primitive type.
723 bool IsPrimitive() const {
724 return primitive_type_ != kPrimNot;
725 }
726
727 // Returns true if this class can access that class.
728 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700729 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700730 }
731
Carl Shapiro1fb86202011-06-27 17:43:13 -0700732 // Returns the size in bytes of a class object instance with the
733 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700734 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700735 // return OFFSETOF_MEMBER(Class, sfields_) +
736 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700737 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700738
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739 // Returns the number of static, private, and constructor methods.
740 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700741 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700742 }
743
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700744 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700745 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700746 }
747
748 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
749 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700750 }
751
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700752 // Returns the number of non-inherited virtual methods.
753 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700754 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700755 }
756
757 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700758 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700759 }
760
761 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
762 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700763 }
764
765 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700766 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 }
768
Carl Shapiro69759ea2011-07-21 18:13:35 -0700769 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700770 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700771 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700772 }
773
774 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700775 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700776 }
777
778 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700779 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 }
781
782 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700783 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700784 }
785
Carl Shapiro69759ea2011-07-21 18:13:35 -0700786 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700787 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700788 }
789
790 void SetStaticField(uint32_t i, StaticField* f) { // TODO: uint16_t
791 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700792 }
793
794 uint32_t GetReferenceOffsets() const {
795 return reference_offsets_;
796 }
797
798 void SetReferenceOffsets(uint32_t new_reference_offsets) {
799 reference_offsets_ = new_reference_offsets;
800 }
801
Ian Rogersb033c752011-07-20 12:22:35 -0700802 Method* FindDirectMethod(const StringPiece& name) const;
803
804 Method* FindVirtualMethod(const StringPiece& name) const;
805
Carl Shapiro69759ea2011-07-21 18:13:35 -0700806 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700807 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700808 }
809
810 Class* GetInterface(uint32_t i) const {
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700811 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700812 }
813
814 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
815 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700816 }
817
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700818 Method* FindDirectMethodLocally(const StringPiece& name,
819 const StringPiece& descriptor) const;
820
Ian Rogersb033c752011-07-20 12:22:35 -0700821 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700822 // leave space for instance data; we could access fields directly if
823 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700824#define CLASS_FIELD_SLOTS 1
825 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700826 uint32_t instance_data_[CLASS_FIELD_SLOTS];
827#undef CLASS_FIELD_SLOTS
828
829 // UTF-8 descriptor for the class from constant pool
830 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700831 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700832
833 // Proxy classes have their descriptor allocated on the native heap.
834 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700835 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700836
837 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700838 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700839
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700840 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700841 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700842 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700843
844 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700845 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700846
847 // if class verify fails, we must return same error on subsequent tries
848 Class* verify_error_class_;
849
850 // threadId, used to check for recursive <clinit> invocation
851 uint32_t clinit_thread_id_;
852
853 // Total object size; used when allocating storage on gc heap. (For
854 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700856
857 // For array classes, the class object for base element, for
858 // instanceof/checkcast (for String[][][], this will be String).
859 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700860 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700861
862 // For array classes, the number of array dimensions, e.g. int[][]
863 // is 2. Otherwise 0.
864 int32_t array_rank_;
865
866 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
867 PrimitiveType primitive_type_;
868
869 // The superclass, or NULL if this is java.lang.Object or a
870 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 Class* super_class_; // TODO: make an instance field
872 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700873
874 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700875 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700876
877 // initiating class loader list
878 // NOTE: for classes with low serialNumber, these are unused, and the
879 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700880 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700881
882 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700883 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700884 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700885
886 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700887 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700888
889 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700890 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700891
892 // Virtual method table (vtable), for use by "invoke-virtual". The
893 // vtable from the superclass is copied in, and virtual methods from
894 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700895 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700896
897 // Interface table (iftable), one entry per interface supported by
898 // this class. That means one entry for each interface we support
899 // directly, indirectly via superclass, or indirectly via
900 // superinterface. This will be null if neither we nor our
901 // superclass implement any interfaces.
902 //
903 // Why we need this: given "class Foo implements Face", declare
904 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
905 // is part of the Face interface. We can't easily use a single
906 // vtable.
907 //
908 // For every interface a concrete class implements, we create a list
909 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700910 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700911 InterfaceEntry* iftable_;
912
913 // The interface vtable indices for iftable get stored here. By
914 // placing them all in a single pool for each class that implements
915 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700916 size_t ifvi_pool_count_;
917 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700918
919 // instance fields
920 //
921 // These describe the layout of the contents of a
922 // DataObject-compatible Object. Note that only the fields directly
923 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700924 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700925 //
926 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700927 // the beginning of the field list. num_reference_instance_fields_
928 // specifies the number of reference fields.
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700929 ObjectArray<InstanceField>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700930
931 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700932 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700933
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700934 // Bitmap of offsets of ifields.
935 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700936
937 // source file name, if known. Otherwise, NULL.
938 const char* source_file_;
939
940 // Static fields
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700941 ObjectArray<StaticField>* sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700942
943 private:
944 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700945};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700946std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700947
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700948class DataObject : public Object {
949 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700950 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700951 private:
952 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700953};
954
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700955class CharArray : public Array {
956 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700957 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700958};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700959
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700960class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700961 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700962 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700963
964 uint32_t hash_code_;
965
966 uint32_t offset_;
967
968 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700969
970 private:
971 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972};
973
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700975 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976 Class* GetClass() const {
977 return klass_;
978 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700979
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700980 void SetClass(Class* klass) {
981 klass_ = klass;
982 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700983
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700984 private:
985 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700986 Class* klass_;
987
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988 public: // TODO: private
989 // Index into array of vtable offsets. This points into the
990 // ifviPool, which holds the vtables for all interfaces declared by
991 // this class.
992 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700993};
994
995} // namespace art
996
997#endif // ART_SRC_OBJECT_H_