blob: 266822da027d25b40d59c1a15f5cc12375be0676 [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
Ian Rogersb033c752011-07-20 12:22:35 -07006#include "src/constants.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07007#include "src/casts.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07008#include "src/dex_file.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07009#include "src/globals.h"
Ian Rogersb033c752011-07-20 12:22:35 -070010#include "src/logging.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070011#include "src/macros.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070012#include "src/offsets.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070013#include "src/stringpiece.h"
14#include "src/monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070015
16namespace art {
17
18class Array;
19class Class;
20class DexFile;
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;
Carl Shapiro69759ea2011-07-21 18:13:35 -070026class ObjectArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
Carl Shapiro3ee755d2011-06-28 12:11:04 -070029union JValue {
30 uint8_t z;
31 int8_t b;
32 uint16_t c;
33 int16_t s;
34 int32_t i;
35 int64_t j;
36 float f;
37 double d;
38 Object* l;
39};
40
Brian Carlstrombe977852011-07-19 14:54:54 -070041static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
42static const uint32_t kAccPrivate = 0x0002; // field, method, ic
43static const uint32_t kAccProtected = 0x0004; // field, method, ic
44static const uint32_t kAccStatic = 0x0008; // field, method, ic
45static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
46static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
47static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
48static const uint32_t kAccVolatile = 0x0040; // field
49static const uint32_t kAccBridge = 0x0040; // method (1.5)
50static const uint32_t kAccTransient = 0x0080; // field
51static const uint32_t kAccVarargs = 0x0080; // method (1.5)
52static const uint32_t kAccNative = 0x0100; // method
53static const uint32_t kAccInterface = 0x0200; // class, ic
54static const uint32_t kAccAbstract = 0x0400; // class, method, ic
55static const uint32_t kAccStrict = 0x0800; // method
56static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
57static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
58static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070059
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060static const uint32_t kAccMiranda = 0x8000; // method
61
Brian Carlstroma331b3c2011-07-18 17:47:56 -070062static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
63
Brian Carlstrombe977852011-07-19 14:54:54 -070064static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
65static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069 */
70/*
71 * A magic value for refOffsets. Ignore the bits and walk the super
72 * chain when this is the value.
73 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
74 * fields followed by 2 ref instance fields.]
75 */
76#define CLASS_WALK_SUPER ((unsigned int)(3))
77#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
78#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
79#define CLASS_OFFSET_ALIGNMENT 4
80#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
81/*
82 * Given an offset, return the bit number which would encode that offset.
83 * Local use only.
84 */
85#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
86 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
87 CLASS_OFFSET_ALIGNMENT)
88/*
89 * Is the given offset too large to be encoded?
90 */
91#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
92 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
93/*
94 * Return a single bit, encoding the offset.
95 * Undefined if the offset is too large, as defined above.
96 */
97#define CLASS_BIT_FROM_OFFSET(byteOffset) \
98 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
99/*
100 * Return an offset, given a bit number as returned from CLZ.
101 */
102#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700103 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700104
105
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700107 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700108 Class* GetClass() const {
109 return klass_;
110 }
111
112 void MonitorEnter() {
113 monitor_->Enter();
114 }
115
116 void MonitorExit() {
117 monitor_->Exit();
118 }
119
120 void Notify() {
121 monitor_->Notify();
122 }
123
124 void NotifyAll() {
125 monitor_->NotifyAll();
126 }
127
128 void Wait() {
129 monitor_->Wait();
130 }
131
132 void Wait(int64_t timeout) {
133 monitor_->Wait(timeout);
134 }
135
136 void Wait(int64_t timeout, int32_t nanos) {
137 monitor_->Wait(timeout, nanos);
138 }
139
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140 const Object* GetFieldObject(size_t field_offset) const {
141 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
142 return *reinterpret_cast<Object* const*>(raw_addr);
143 }
144
145 Object* GetFieldObject(size_t field_offset) {
146 return const_cast<Object*>(GetFieldObject(field_offset));
147 }
148
149 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
151 *reinterpret_cast<Object**>(raw_addr) = new_value;
152 // TODO: write barrier
153 }
154
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 bool IsClass() const {
156 LOG(FATAL) << "Unimplemented";
157 return true;
158 }
159
160 Class* AsClass() {
161 return down_cast<Class*>(this);
162 }
163
164 const Class* AsClass() const {
165 return down_cast<const Class*>(this);
166 }
167
168 bool IsObjectArray() const {
169 LOG(FATAL) << "Unimplemented";
170 return true;
171 }
172
173 const ObjectArray* AsObjectArray() const {
174 return down_cast<const ObjectArray*>(this);
175 }
176
177 bool IsReference() const {
178 LOG(FATAL) << "Unimplemented";
179 return true;
180 }
181
182 bool IsWeakReference() const {
183 LOG(FATAL) << "Unimplemented";
184 return true;
185 }
186
187 bool IsSoftReference() const {
188 LOG(FATAL) << "Unimplemented";
189 return true;
190 }
191
192 bool IsFinalizerReference() const {
193 LOG(FATAL) << "Unimplemented";
194 return true;
195 }
196
197 bool IsPhantomReference() const {
198 LOG(FATAL) << "Unimplemented";
199 return true;
200 }
201
202 bool IsArray() const {
203 LOG(FATAL) << "Unimplemented";
204 return true;
205 }
206
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700207 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700208 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700209
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 Monitor* monitor_;
211
212 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700213 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 DISALLOW_COPY_AND_ASSIGN(Object);
215};
216
217class ObjectLock {
218 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700219 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 CHECK(object != NULL);
221 obj_->MonitorEnter();
222 }
223
224 ~ObjectLock() {
225 obj_->MonitorExit();
226 }
227
228 void Wait(int64_t millis = 0) {
229 return obj_->Wait(millis);
230 }
231
232 void Notify() {
233 obj_->Notify();
234 }
235
236 void NotifyAll() {
237 obj_->NotifyAll();
238 }
239
240 private:
241 Object* obj_;
242 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700243};
244
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700245class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700246 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700247 Class* GetDeclaringClass() const {
248 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 }
250
251 const char* GetName() const {
252 return name_;
253 }
254
255 char GetType() const { // TODO: return type
256 return signature_[0];
257 }
258
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700259 const char* GetSignature() const {
260 return signature_;
261 }
262
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700263 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700264#define FIELD_FIELD_SLOTS 1+6
265 // AccessibleObject #0 flag
266 // Field #0 declaringClass
267 // Field #1 genericType
268 // Field #2 genericTypesAreInitialized
269 // Field #3 name
270 // Field #4 slot
271 // Field #5 type
272 uint32_t instance_data_[FIELD_FIELD_SLOTS];
273#undef FIELD_FIELD_SLOTS
274
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700275 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700276 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700277
278 const char* name_;
279
280 // e.g. "I", "[C", "Landroid/os/Debug;"
281 const char* signature_;
282
283 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700284
285 private:
286 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700287};
288
289// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700290class InstanceField : public Field {
291 public:
292 uint32_t GetOffset() const {
293 return offset_;
294 }
295
296 void SetOffset(size_t num_bytes) {
297 offset_ = num_bytes;
298 }
299
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700300 private:
301 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700302 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700303};
304
305// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700306class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700307 public:
308 void SetBoolean(bool z) {
309 CHECK_EQ(GetType(), 'Z');
310 value_.z = z;
311 }
312
313 void SetByte(int8_t b) {
314 CHECK_EQ(GetType(), 'B');
315 value_.b = b;
316 }
317
318 void SetChar(uint16_t c) {
319 CHECK_EQ(GetType(), 'C');
320 value_.c = c;
321 }
322
323 void SetShort(uint16_t s) {
324 CHECK_EQ(GetType(), 'S');
325 value_.s = s;
326 }
327
328 void SetInt(int32_t i) {
329 CHECK_EQ(GetType(), 'I');
330 value_.i = i;
331 }
332
333 int64_t GetLong() {
334 CHECK_EQ(GetType(), 'J');
335 return value_.j;
336 }
337
338 void SetLong(int64_t j) {
339 CHECK_EQ(GetType(), 'J');
340 value_.j = j;
341 }
342
343 void SetFloat(float f) {
344 CHECK_EQ(GetType(), 'F');
345 value_.f = f;
346 }
347
348 void SetDouble(double d) {
349 CHECK_EQ(GetType(), 'D');
350 value_.d = d;
351 }
352
Carl Shapiro69759ea2011-07-21 18:13:35 -0700353 Object* GetObject() {
354 return value_.l;
355 }
356
357 const Object* GetObject() const {
358 return value_.l;
359 }
360
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700361 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700362 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700363 value_.l = l;
364 // TODO: write barrier
365 }
366
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700368 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700369 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700370};
371
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700372class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700373 public:
374 // Returns the method name.
375 // TODO: example
376 const StringPiece& GetName() const {
377 return name_;
378 }
379
Brian Carlstroma0808032011-07-18 00:39:23 -0700380 Class* GetDeclaringClass() const {
381 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382 }
383
Ian Rogersb033c752011-07-20 12:22:35 -0700384 static MemberOffset ClassOffset() {
385 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
386 }
387
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700388 // const char* GetReturnTypeDescriptor() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700389 // return declaring_class_->GetDexFile_->GetRaw()
390 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 // }
392
393 // Returns true if the method is declared public.
394 bool IsPublic() const {
395 return (access_flags_ & kAccPublic) != 0;
396 }
397
398 // Returns true if the method is declared private.
399 bool IsPrivate() const {
400 return (access_flags_ & kAccPrivate) != 0;
401 }
402
403 // Returns true if the method is declared static.
404 bool IsStatic() const {
405 return (access_flags_ & kAccStatic) != 0;
406 }
407
408 // Returns true if the method is declared synchronized.
409 bool IsSynchronized() const {
410 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
411 return (access_flags_ & synchonized) != 0;
412 }
413
414 // Returns true if the method is declared final.
415 bool IsFinal() const {
416 return (access_flags_ & kAccFinal) != 0;
417 }
418
419 // Returns true if the method is declared native.
420 bool IsNative() const {
421 return (access_flags_ & kAccNative) != 0;
422 }
423
424 // Returns true if the method is declared abstract.
425 bool IsAbstract() const {
426 return (access_flags_ & kAccAbstract) != 0;
427 }
428
429 bool IsSynthetic() const {
430 return (access_flags_ & kAccSynthetic) != 0;
431 }
432
433 // Number of argument registers required by the prototype.
434 uint32_t NumArgRegisters();
435
436 bool HasSameNameAndPrototype(const Method* that) const {
437 return HasSameName(that) && HasSamePrototype(that);
438 }
439
440 bool HasSameName(const Method* that) const {
441 return this->GetName() == that->GetName();
442 }
443
444 bool HasSamePrototype(const Method* that) const {
445 return HasSameReturnType(that) && HasSameArgumentTypes(that);
446 }
447
448 bool HasSameReturnType(const Method* that) const;
449
450 bool HasSameArgumentTypes(const Method* that) const;
451
452 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700453#define METHOD_FIELD_SLOTS 1+11
454 // AccessibleObject #0 flag
455 // Method #0 declaringClass
456 // Method #1 exceptionTypes
457 // Method #2 formalTypeParameters
458 // Method #3 genericExceptionTypes
459 // Method #4 genericParameterTypes
460 // Method #5 genericReturnType
461 // Method #6 genericTypesAreInitialized
462 // Method #7 name
463 // Method #8 parameterTypes
464 // Method #9 returnType
465 // Method #10 slot
466 uint32_t instance_data_[METHOD_FIELD_SLOTS];
467#undef METHOD_FIELD_SLOTS
468
Ian Rogersb033c752011-07-20 12:22:35 -0700469 bool IsReturnAReference() const {
470 return (shorty_[0] == 'L') || (shorty_[0] == '[');
471 }
472
473 bool IsReturnAFloatOrDouble() const {
474 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
475 }
476
477 bool IsReturnAFloat() const {
478 return shorty_[0] == 'F';
479 }
480
481 bool IsReturnADouble() const {
482 return shorty_[0] == 'D';
483 }
484
485 bool IsReturnALong() const {
486 return shorty_[0] == 'J';
487 }
488
489 // The number of arguments that should be supplied to this method
490 size_t NumArgs() const {
491 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
492 }
493
494 // The number of reference arguments to this method including implicit this
495 // pointer
496 size_t NumReferenceArgs() const;
497
498 // The number of long or double arguments
499 size_t NumLongOrDoubleArgs() const;
500
501 // The number of reference arguments to this method before the given
502 // parameter index
503 size_t NumReferenceArgsBefore(unsigned int param) const;
504
505 // Is the given method parameter a reference?
506 bool IsParamAReference(unsigned int param) const;
507
508 // Is the given method parameter a long or double?
509 bool IsParamALongOrDouble(unsigned int param) const;
510
Ian Rogersdf20fe02011-07-20 20:34:16 -0700511 // Size in bytes of the given parameter
512 size_t ParamSize(unsigned int param) const;
513
514 // Size in bytes of the return value
515 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700516
517 void SetCode(const void* code) {
518 code_ = code;
519 }
520
521 const void* GetCode() const {
522 return code_;
523 }
524
525 void RegisterNative(const void* native_method) {
526 native_method_ = native_method;
527 }
528
529 static MemberOffset NativeMethodOffset() {
530 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
531 }
532
533 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700534 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700535 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700536
537 // access flags; low 16 bits are defined by spec (could be uint16_t?)
538 uint32_t access_flags_;
539
540 // For concrete virtual methods, this is the offset of the method
541 // in "vtable".
542 //
543 // For abstract methods in an interface class, this is the offset
544 // of the method in "iftable[n]->methodIndexArray".
545 uint16_t method_index_;
546
547 // Method bounds; not needed for an abstract method.
548 //
549 // For a native method, we compute the size of the argument list, and
550 // set "insSize" and "registerSize" equal to it.
551 uint16_t num_registers_; // ins + locals
552 uint16_t num_outs_;
553 uint16_t num_ins_;
554
555 // method name, e.g. "<init>" or "eatLunch"
556 StringPiece name_;
557
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700558 // Method prototype descriptor string (return and argument types).
559 uint32_t proto_idx_;
560
561 // The short-form method descriptor string.
562 StringPiece shorty_;
563
564 // A pointer to the memory-mapped DEX code.
565 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700566
567 private:
568 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700569
570 // Compiled code associated with this method
571 const void* code_;
572
573 // Any native method registered with this method
574 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700575};
576
Carl Shapiro1fb86202011-06-27 17:43:13 -0700577// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700578class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700579 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700580 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700581 kStatusError = -1,
582 kStatusNotReady = 0,
583 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
584 kStatusLoaded = 2, // DEX idx values resolved
585 kStatusResolved = 3, // part of linking
586 kStatusVerifying = 4, // in the process of being verified
587 kStatusVerified = 5, // logically part of linking; done pre-init
588 kStatusInitializing = 6, // class init in progress
589 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700590 };
591
592 enum PrimitiveType {
593 kPrimNot = -1
594 };
595
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700596 Class* GetSuperClass() const {
597 return super_class_;
598 }
599
600 uint32_t GetSuperClassIdx() const {
601 return super_class_idx_;
602 }
603
604 bool HasSuperClass() const {
605 return super_class_ != NULL;
606 }
607
608 Object* GetClassLoader() const {
609 return class_loader_;
610 }
611
612 DexFile* GetDexFile() const {
613 return dex_file_;
614 }
615
616 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700617 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700618 return component_type_;
619 }
620
621 const StringPiece& GetDescriptor() const {
622 return descriptor_;
623 }
624
625 Status GetStatus() const {
626 return status_;
627 }
628
629 void SetStatus(Status new_status) {
630 // TODO: validate transition
631 status_ = new_status;
632 }
633
Carl Shapiro69759ea2011-07-21 18:13:35 -0700634 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700635 bool IsErroneous() const {
636 return GetStatus() == kStatusError;
637 }
638
Carl Shapiro69759ea2011-07-21 18:13:35 -0700639 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700640 bool IsVerified() const {
641 return GetStatus() >= kStatusVerified;
642 }
643
Carl Shapiro69759ea2011-07-21 18:13:35 -0700644 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645 bool IsLinked() const {
646 return GetStatus() >= kStatusResolved;
647 }
648
Carl Shapiro69759ea2011-07-21 18:13:35 -0700649 bool IsLoaded() const {
650 return GetStatus() >= kStatusLoaded;
651 }
652
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700653 // Returns true if this class is in the same packages as that class.
654 bool IsInSamePackage(const Class* that) const;
655
Ian Rogersb033c752011-07-20 12:22:35 -0700656 static bool IsInSamePackage(const StringPiece& descriptor1,
657 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700658
659 // Returns true if this class represents an array class.
660 bool IsArray() const {
661 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
662 }
663
664 // Returns true if the class is an interface.
665 bool IsInterface() const {
666 return (access_flags_ & kAccInterface) != 0;
667 }
668
669 // Returns true if the class is declared public.
670 bool IsPublic() const {
671 return (access_flags_ & kAccPublic) != 0;
672 }
673
674 // Returns true if the class is declared final.
675 bool IsFinal() const {
676 return (access_flags_ & kAccFinal) != 0;
677 }
678
679 // Returns true if the class is abstract.
680 bool IsAbstract() const {
681 return (access_flags_ & kAccAbstract) != 0;
682 }
683
684 // Returns true if the class is an annotation.
685 bool IsAnnotation() const {
686 return (access_flags_ & kAccAnnotation) != 0;
687 }
688
689 // Returns true if the class is a primitive type.
690 bool IsPrimitive() const {
691 return primitive_type_ != kPrimNot;
692 }
693
694 // Returns true if this class can access that class.
695 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700697 }
698
Carl Shapiro1fb86202011-06-27 17:43:13 -0700699 // Returns the size in bytes of a class object instance with the
700 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700701 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700702 // return OFFSETOF_MEMBER(Class, sfields_) +
703 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700704 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700705
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700706 // Returns the number of static, private, and constructor methods.
707 size_t NumDirectMethods() const {
708 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700709 }
710
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700711 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700712 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700713 }
714
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715 // Returns the number of non-inherited virtual methods.
716 size_t NumVirtualMethods() const {
717 return num_virtual_methods_;
718 }
719
720 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700721 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 }
723
724 size_t NumInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700725 return num_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 }
727
Carl Shapiro69759ea2011-07-21 18:13:35 -0700728 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700729 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700730 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700731 }
732
733 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700734 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700735 return ifields_[i];
736 }
737
738 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700739 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700740 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700741 }
742
743 size_t NumStaticFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700744 return num_static_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 }
746
Carl Shapiro69759ea2011-07-21 18:13:35 -0700747 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
748 DCHECK_LT(i, num_static_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700749 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700750 }
751
752 uint32_t GetReferenceOffsets() const {
753 return reference_offsets_;
754 }
755
756 void SetReferenceOffsets(uint32_t new_reference_offsets) {
757 reference_offsets_ = new_reference_offsets;
758 }
759
Ian Rogersb033c752011-07-20 12:22:35 -0700760 Method* FindDirectMethod(const StringPiece& name) const;
761
762 Method* FindVirtualMethod(const StringPiece& name) const;
763
Carl Shapiro69759ea2011-07-21 18:13:35 -0700764 size_t NumInterfaces() const {
765 return interface_count_;
766 }
767
768 Class* GetInterface(uint32_t i) const {
769 DCHECK_LT(i, interface_count_);
770 return interfaces_[i];
771 }
772
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773 Method* FindDirectMethodLocally(const StringPiece& name,
774 const StringPiece& descriptor) const;
775
Ian Rogersb033c752011-07-20 12:22:35 -0700776 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700777 // leave space for instance data; we could access fields directly if
778 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700779#define CLASS_FIELD_SLOTS 1
780 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700781 uint32_t instance_data_[CLASS_FIELD_SLOTS];
782#undef CLASS_FIELD_SLOTS
783
784 // UTF-8 descriptor for the class from constant pool
785 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700786 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700787
788 // Proxy classes have their descriptor allocated on the native heap.
789 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700790 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700791
792 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700793 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700794
Carl Shapiro1fb86202011-06-27 17:43:13 -0700795 // DexFile from which we came; needed to resolve constant pool entries
796 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
797 DexFile* dex_file_;
798
799 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700800 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700801
802 // if class verify fails, we must return same error on subsequent tries
803 Class* verify_error_class_;
804
805 // threadId, used to check for recursive <clinit> invocation
806 uint32_t clinit_thread_id_;
807
808 // Total object size; used when allocating storage on gc heap. (For
809 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700810 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700811
812 // For array classes, the class object for base element, for
813 // instanceof/checkcast (for String[][][], this will be String).
814 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700815 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700816
817 // For array classes, the number of array dimensions, e.g. int[][]
818 // is 2. Otherwise 0.
819 int32_t array_rank_;
820
821 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
822 PrimitiveType primitive_type_;
823
824 // The superclass, or NULL if this is java.lang.Object or a
825 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700826 Class* super_class_; // TODO: make an instance field
827 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700828
829 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700830 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700831
832 // initiating class loader list
833 // NOTE: for classes with low serialNumber, these are unused, and the
834 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700835 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700836
837 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700838 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700839 Class** interfaces_;
840
841 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700842 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700843 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700844
845 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700846 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700847 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700848
849 // Virtual method table (vtable), for use by "invoke-virtual". The
850 // vtable from the superclass is copied in, and virtual methods from
851 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700852 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700853 Method** vtable_;
854
855 // Interface table (iftable), one entry per interface supported by
856 // this class. That means one entry for each interface we support
857 // directly, indirectly via superclass, or indirectly via
858 // superinterface. This will be null if neither we nor our
859 // superclass implement any interfaces.
860 //
861 // Why we need this: given "class Foo implements Face", declare
862 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
863 // is part of the Face interface. We can't easily use a single
864 // vtable.
865 //
866 // For every interface a concrete class implements, we create a list
867 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700868 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700869 InterfaceEntry* iftable_;
870
871 // The interface vtable indices for iftable get stored here. By
872 // placing them all in a single pool for each class that implements
873 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700874 size_t ifvi_pool_count_;
875 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700876
877 // instance fields
878 //
879 // These describe the layout of the contents of a
880 // DataObject-compatible Object. Note that only the fields directly
881 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700882 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700883 //
884 // All instance fields that refer to objects are guaranteed to be at
885 // the beginning of the field list. ifieldRefCount specifies the
886 // number of reference fields.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700887 size_t num_instance_fields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700888
889 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700890 size_t num_reference_instance_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700891 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700892
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700893 // Bitmap of offsets of ifields.
894 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700895
896 // source file name, if known. Otherwise, NULL.
897 const char* source_file_;
898
899 // Static fields
Carl Shapiro69759ea2011-07-21 18:13:35 -0700900 size_t num_static_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700901 StaticField** sfields_;
902
903 private:
904 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700905};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700906std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700907
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700908class DataObject : public Object {
909 public:
910 uint32_t fields_[1];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700911 private:
912 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700913};
914
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700915class Array : public Object {
916 public:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700917 uint32_t GetLength() const{
918 return length_;
919 }
920
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700921 void SetLength(uint32_t length) {
922 length_ = length;
923 }
924
925 private:
926 // The number of array elements.
927 uint32_t length_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700928
929 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700930};
931
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700932class CharArray : public Array {
933 private:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700934 DISALLOW_IMPLICIT_CONSTRUCTORS(CharArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700935};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700936
Carl Shapiro69759ea2011-07-21 18:13:35 -0700937class ObjectArray : public Array {
938 public:
939 Object* Get(int32_t i) {
940 return NULL;
941 }
942
943 const Object* Get(int32_t i) const {
944 return NULL;
945 }
946
947 private:
948 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
949};
950
951
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700952class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700953 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700954 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700955
956 uint32_t hash_code_;
957
958 uint32_t offset_;
959
960 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700961
962 private:
963 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700964};
965
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700967 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968 Class* GetClass() const {
969 return klass_;
970 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700971
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 void SetClass(Class* klass) {
973 klass_ = klass;
974 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700975
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976 private:
977 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700978 Class* klass_;
979
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700980 public: // TODO: private
981 // Index into array of vtable offsets. This points into the
982 // ifviPool, which holds the vtables for all interfaces declared by
983 // this class.
984 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700985};
986
987} // namespace art
988
989#endif // ART_SRC_OBJECT_H_