blob: da64d1b6be19dcca8ff9b73a9e7925b6b9ba435d [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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07006#include "src/dex_file.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07007#include "src/globals.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -07008#include "src/macros.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07009#include "src/stringpiece.h"
10#include "src/monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070011
12namespace art {
13
14class Array;
15class Class;
16class DexFile;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017class InstanceField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070018class InterfaceEntry;
19class Monitor;
20class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070021class Object;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070022class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070023
Carl Shapiro3ee755d2011-06-28 12:11:04 -070024union JValue {
25 uint8_t z;
26 int8_t b;
27 uint16_t c;
28 int16_t s;
29 int32_t i;
30 int64_t j;
31 float f;
32 double d;
33 Object* l;
34};
35
Brian Carlstrombe977852011-07-19 14:54:54 -070036static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
37static const uint32_t kAccPrivate = 0x0002; // field, method, ic
38static const uint32_t kAccProtected = 0x0004; // field, method, ic
39static const uint32_t kAccStatic = 0x0008; // field, method, ic
40static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
41static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
42static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
43static const uint32_t kAccVolatile = 0x0040; // field
44static const uint32_t kAccBridge = 0x0040; // method (1.5)
45static const uint32_t kAccTransient = 0x0080; // field
46static const uint32_t kAccVarargs = 0x0080; // method (1.5)
47static const uint32_t kAccNative = 0x0100; // method
48static const uint32_t kAccInterface = 0x0200; // class, ic
49static const uint32_t kAccAbstract = 0x0400; // class, method, ic
50static const uint32_t kAccStrict = 0x0800; // method
51static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
52static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
53static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070054
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070055static const uint32_t kAccMiranda = 0x8000; // method
56
Brian Carlstroma331b3c2011-07-18 17:47:56 -070057static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
58
Brian Carlstrombe977852011-07-19 14:54:54 -070059static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
60static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070061
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070062/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070063 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070064 */
65/*
66 * A magic value for refOffsets. Ignore the bits and walk the super
67 * chain when this is the value.
68 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
69 * fields followed by 2 ref instance fields.]
70 */
71#define CLASS_WALK_SUPER ((unsigned int)(3))
72#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
73#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
74#define CLASS_OFFSET_ALIGNMENT 4
75#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
76/*
77 * Given an offset, return the bit number which would encode that offset.
78 * Local use only.
79 */
80#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
81 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
82 CLASS_OFFSET_ALIGNMENT)
83/*
84 * Is the given offset too large to be encoded?
85 */
86#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
87 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
88/*
89 * Return a single bit, encoding the offset.
90 * Undefined if the offset is too large, as defined above.
91 */
92#define CLASS_BIT_FROM_OFFSET(byteOffset) \
93 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
94/*
95 * Return an offset, given a bit number as returned from CLZ.
96 */
97#define CLASS_OFFSET_FROM_CLZ(rshift) \
98 (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
99
100
Carl Shapiro1fb86202011-06-27 17:43:13 -0700101class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700102 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700103 Class* GetClass() const {
104 return klass_;
105 }
106
107 void MonitorEnter() {
108 monitor_->Enter();
109 }
110
111 void MonitorExit() {
112 monitor_->Exit();
113 }
114
115 void Notify() {
116 monitor_->Notify();
117 }
118
119 void NotifyAll() {
120 monitor_->NotifyAll();
121 }
122
123 void Wait() {
124 monitor_->Wait();
125 }
126
127 void Wait(int64_t timeout) {
128 monitor_->Wait(timeout);
129 }
130
131 void Wait(int64_t timeout, int32_t nanos) {
132 monitor_->Wait(timeout, nanos);
133 }
134
135 void SetObjectAt(size_t offset, Object* new_value) {
136 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
137 *reinterpret_cast<Object**>(raw_addr) = new_value;
138 // TODO: write barrier
139 }
140
141 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700142 Class* klass_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143 Monitor* monitor_;
144
145 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700146 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700147 DISALLOW_COPY_AND_ASSIGN(Object);
148};
149
150class ObjectLock {
151 public:
152 ObjectLock(Object* object) : obj_(object) {
153 CHECK(object != NULL);
154 obj_->MonitorEnter();
155 }
156
157 ~ObjectLock() {
158 obj_->MonitorExit();
159 }
160
161 void Wait(int64_t millis = 0) {
162 return obj_->Wait(millis);
163 }
164
165 void Notify() {
166 obj_->Notify();
167 }
168
169 void NotifyAll() {
170 obj_->NotifyAll();
171 }
172
173 private:
174 Object* obj_;
175 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700176};
177
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700178class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700179 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700180 Class* GetDeclaringClass() const {
181 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700182 }
183
184 const char* GetName() const {
185 return name_;
186 }
187
188 char GetType() const { // TODO: return type
189 return signature_[0];
190 }
191
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700192 const char* GetSignature() const {
193 return signature_;
194 }
195
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700196 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700197#define FIELD_FIELD_SLOTS 1+6
198 // AccessibleObject #0 flag
199 // Field #0 declaringClass
200 // Field #1 genericType
201 // Field #2 genericTypesAreInitialized
202 // Field #3 name
203 // Field #4 slot
204 // Field #5 type
205 uint32_t instance_data_[FIELD_FIELD_SLOTS];
206#undef FIELD_FIELD_SLOTS
207
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700208 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700209 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700210
211 const char* name_;
212
213 // e.g. "I", "[C", "Landroid/os/Debug;"
214 const char* signature_;
215
216 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700217
218 private:
219 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700220};
221
222// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700223class InstanceField : public Field {
224 public:
225 uint32_t GetOffset() const {
226 return offset_;
227 }
228
229 void SetOffset(size_t num_bytes) {
230 offset_ = num_bytes;
231 }
232
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233 private:
234 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700235 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700236};
237
238// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700239class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700240 public:
241 void SetBoolean(bool z) {
242 CHECK_EQ(GetType(), 'Z');
243 value_.z = z;
244 }
245
246 void SetByte(int8_t b) {
247 CHECK_EQ(GetType(), 'B');
248 value_.b = b;
249 }
250
251 void SetChar(uint16_t c) {
252 CHECK_EQ(GetType(), 'C');
253 value_.c = c;
254 }
255
256 void SetShort(uint16_t s) {
257 CHECK_EQ(GetType(), 'S');
258 value_.s = s;
259 }
260
261 void SetInt(int32_t i) {
262 CHECK_EQ(GetType(), 'I');
263 value_.i = i;
264 }
265
266 int64_t GetLong() {
267 CHECK_EQ(GetType(), 'J');
268 return value_.j;
269 }
270
271 void SetLong(int64_t j) {
272 CHECK_EQ(GetType(), 'J');
273 value_.j = j;
274 }
275
276 void SetFloat(float f) {
277 CHECK_EQ(GetType(), 'F');
278 value_.f = f;
279 }
280
281 void SetDouble(double d) {
282 CHECK_EQ(GetType(), 'D');
283 value_.d = d;
284 }
285
286 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700287 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700288 value_.l = l;
289 // TODO: write barrier
290 }
291
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700292 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700293 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700294 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700295};
296
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700297class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700298 public:
299 // Returns the method name.
300 // TODO: example
301 const StringPiece& GetName() const {
302 return name_;
303 }
304
Brian Carlstroma0808032011-07-18 00:39:23 -0700305 Class* GetDeclaringClass() const {
306 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700307 }
308
309 // const char* GetReturnTypeDescriptor() const {
Brian Carlstroma0808032011-07-18 00:39:23 -0700310 // return declaring_class_->GetDexFile_->GetRaw()->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311 // }
312
313 // Returns true if the method is declared public.
314 bool IsPublic() const {
315 return (access_flags_ & kAccPublic) != 0;
316 }
317
318 // Returns true if the method is declared private.
319 bool IsPrivate() const {
320 return (access_flags_ & kAccPrivate) != 0;
321 }
322
323 // Returns true if the method is declared static.
324 bool IsStatic() const {
325 return (access_flags_ & kAccStatic) != 0;
326 }
327
328 // Returns true if the method is declared synchronized.
329 bool IsSynchronized() const {
330 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
331 return (access_flags_ & synchonized) != 0;
332 }
333
334 // Returns true if the method is declared final.
335 bool IsFinal() const {
336 return (access_flags_ & kAccFinal) != 0;
337 }
338
339 // Returns true if the method is declared native.
340 bool IsNative() const {
341 return (access_flags_ & kAccNative) != 0;
342 }
343
344 // Returns true if the method is declared abstract.
345 bool IsAbstract() const {
346 return (access_flags_ & kAccAbstract) != 0;
347 }
348
349 bool IsSynthetic() const {
350 return (access_flags_ & kAccSynthetic) != 0;
351 }
352
353 // Number of argument registers required by the prototype.
354 uint32_t NumArgRegisters();
355
356 bool HasSameNameAndPrototype(const Method* that) const {
357 return HasSameName(that) && HasSamePrototype(that);
358 }
359
360 bool HasSameName(const Method* that) const {
361 return this->GetName() == that->GetName();
362 }
363
364 bool HasSamePrototype(const Method* that) const {
365 return HasSameReturnType(that) && HasSameArgumentTypes(that);
366 }
367
368 bool HasSameReturnType(const Method* that) const;
369
370 bool HasSameArgumentTypes(const Method* that) const;
371
372 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700373#define METHOD_FIELD_SLOTS 1+11
374 // AccessibleObject #0 flag
375 // Method #0 declaringClass
376 // Method #1 exceptionTypes
377 // Method #2 formalTypeParameters
378 // Method #3 genericExceptionTypes
379 // Method #4 genericParameterTypes
380 // Method #5 genericReturnType
381 // Method #6 genericTypesAreInitialized
382 // Method #7 name
383 // Method #8 parameterTypes
384 // Method #9 returnType
385 // Method #10 slot
386 uint32_t instance_data_[METHOD_FIELD_SLOTS];
387#undef METHOD_FIELD_SLOTS
388
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700389 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700390 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391
392 // access flags; low 16 bits are defined by spec (could be uint16_t?)
393 uint32_t access_flags_;
394
395 // For concrete virtual methods, this is the offset of the method
396 // in "vtable".
397 //
398 // For abstract methods in an interface class, this is the offset
399 // of the method in "iftable[n]->methodIndexArray".
400 uint16_t method_index_;
401
402 // Method bounds; not needed for an abstract method.
403 //
404 // For a native method, we compute the size of the argument list, and
405 // set "insSize" and "registerSize" equal to it.
406 uint16_t num_registers_; // ins + locals
407 uint16_t num_outs_;
408 uint16_t num_ins_;
409
410 // method name, e.g. "<init>" or "eatLunch"
411 StringPiece name_;
412
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700413 // Method prototype descriptor string (return and argument types).
414 uint32_t proto_idx_;
415
416 // The short-form method descriptor string.
417 StringPiece shorty_;
418
419 // A pointer to the memory-mapped DEX code.
420 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700421
422 private:
423 Method();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700424};
425
Carl Shapiro1fb86202011-06-27 17:43:13 -0700426// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700427class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700428 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700429 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700430 kStatusError = -1,
431 kStatusNotReady = 0,
432 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
433 kStatusLoaded = 2, // DEX idx values resolved
434 kStatusResolved = 3, // part of linking
435 kStatusVerifying = 4, // in the process of being verified
436 kStatusVerified = 5, // logically part of linking; done pre-init
437 kStatusInitializing = 6, // class init in progress
438 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700439 };
440
441 enum PrimitiveType {
442 kPrimNot = -1
443 };
444
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700445 Class* GetSuperClass() const {
446 return super_class_;
447 }
448
449 uint32_t GetSuperClassIdx() const {
450 return super_class_idx_;
451 }
452
453 bool HasSuperClass() const {
454 return super_class_ != NULL;
455 }
456
457 Object* GetClassLoader() const {
458 return class_loader_;
459 }
460
461 DexFile* GetDexFile() const {
462 return dex_file_;
463 }
464
465 Class* GetComponentType() const {
466 return component_type_;
467 }
468
469 const StringPiece& GetDescriptor() const {
470 return descriptor_;
471 }
472
473 Status GetStatus() const {
474 return status_;
475 }
476
477 void SetStatus(Status new_status) {
478 // TODO: validate transition
479 status_ = new_status;
480 }
481
482 bool IsErroneous() const {
483 return GetStatus() == kStatusError;
484 }
485
486 bool IsVerified() const {
487 return GetStatus() >= kStatusVerified;
488 }
489
490 bool IsLinked() const {
491 return GetStatus() >= kStatusResolved;
492 }
493
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700494 // Returns true if this class is in the same packages as that class.
495 bool IsInSamePackage(const Class* that) const;
496
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700497 static bool IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700498
499 // Returns true if this class represents an array class.
500 bool IsArray() const {
501 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
502 }
503
504 // Returns true if the class is an interface.
505 bool IsInterface() const {
506 return (access_flags_ & kAccInterface) != 0;
507 }
508
509 // Returns true if the class is declared public.
510 bool IsPublic() const {
511 return (access_flags_ & kAccPublic) != 0;
512 }
513
514 // Returns true if the class is declared final.
515 bool IsFinal() const {
516 return (access_flags_ & kAccFinal) != 0;
517 }
518
519 // Returns true if the class is abstract.
520 bool IsAbstract() const {
521 return (access_flags_ & kAccAbstract) != 0;
522 }
523
524 // Returns true if the class is an annotation.
525 bool IsAnnotation() const {
526 return (access_flags_ & kAccAnnotation) != 0;
527 }
528
529 // Returns true if the class is a primitive type.
530 bool IsPrimitive() const {
531 return primitive_type_ != kPrimNot;
532 }
533
534 // Returns true if this class can access that class.
535 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700536 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700537 }
538
Carl Shapiro1fb86202011-06-27 17:43:13 -0700539 // Returns the size in bytes of a class object instance with the
540 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700541 // static size_t Size(size_t num_sfields) {
542 // return OFFSETOF_MEMBER(Class, sfields_) + sizeof(StaticField) * num_sfields;
543 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 // Returns the number of static, private, and constructor methods.
546 size_t NumDirectMethods() const {
547 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700548 }
549
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700550 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700551 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700552 }
553
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700554 // Returns the number of non-inherited virtual methods.
555 size_t NumVirtualMethods() const {
556 return num_virtual_methods_;
557 }
558
559 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700560 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700561 }
562
563 size_t NumInstanceFields() const {
564 return num_ifields_;
565 }
566
567 size_t NumReferenceInstanceFields() const {
568 return num_reference_ifields_;
569 }
570
571 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700572 return ifields_[i];
573 }
574
575 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
576 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577 }
578
579 size_t NumStaticFields() const {
580 return num_sfields_;
581 }
582
583 StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700584 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700585 }
586
587 uint32_t GetReferenceOffsets() const {
588 return reference_offsets_;
589 }
590
591 void SetReferenceOffsets(uint32_t new_reference_offsets) {
592 reference_offsets_ = new_reference_offsets;
593 }
594
595 Method* FindDirectMethodLocally(const StringPiece& name,
596 const StringPiece& descriptor) const;
597
Carl Shapiro1fb86202011-06-27 17:43:13 -0700598 public: // TODO: private
599 // leave space for instance data; we could access fields directly if
600 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700601#define CLASS_FIELD_SLOTS 1
602 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700603 uint32_t instance_data_[CLASS_FIELD_SLOTS];
604#undef CLASS_FIELD_SLOTS
605
606 // UTF-8 descriptor for the class from constant pool
607 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700608 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700609
610 // Proxy classes have their descriptor allocated on the native heap.
611 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700612 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700613
614 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700615 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700616
Carl Shapiro1fb86202011-06-27 17:43:13 -0700617 // DexFile from which we came; needed to resolve constant pool entries
618 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
619 DexFile* dex_file_;
620
621 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700623
624 // if class verify fails, we must return same error on subsequent tries
625 Class* verify_error_class_;
626
627 // threadId, used to check for recursive <clinit> invocation
628 uint32_t clinit_thread_id_;
629
630 // Total object size; used when allocating storage on gc heap. (For
631 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700633
634 // For array classes, the class object for base element, for
635 // instanceof/checkcast (for String[][][], this will be String).
636 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700637 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700638
639 // For array classes, the number of array dimensions, e.g. int[][]
640 // is 2. Otherwise 0.
641 int32_t array_rank_;
642
643 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
644 PrimitiveType primitive_type_;
645
646 // The superclass, or NULL if this is java.lang.Object or a
647 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 Class* super_class_; // TODO: make an instance field
649 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700650
651 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700652 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700653
654 // initiating class loader list
655 // NOTE: for classes with low serialNumber, these are unused, and the
656 // values are kept in a table in gDvm.
657 //InitiatingLoaderList initiating_loader_list_;
658
659 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700660 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700661 Class** interfaces_;
662
663 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700664 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700665 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700666
667 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700668 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700669 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700670
671 // Virtual method table (vtable), for use by "invoke-virtual". The
672 // vtable from the superclass is copied in, and virtual methods from
673 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700674 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700675 Method** vtable_;
676
677 // Interface table (iftable), one entry per interface supported by
678 // this class. That means one entry for each interface we support
679 // directly, indirectly via superclass, or indirectly via
680 // superinterface. This will be null if neither we nor our
681 // superclass implement any interfaces.
682 //
683 // Why we need this: given "class Foo implements Face", declare
684 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
685 // is part of the Face interface. We can't easily use a single
686 // vtable.
687 //
688 // For every interface a concrete class implements, we create a list
689 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700690 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700691 InterfaceEntry* iftable_;
692
693 // The interface vtable indices for iftable get stored here. By
694 // placing them all in a single pool for each class that implements
695 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696 size_t ifvi_pool_count_;
697 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700698
699 // instance fields
700 //
701 // These describe the layout of the contents of a
702 // DataObject-compatible Object. Note that only the fields directly
703 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700704 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700705 //
706 // All instance fields that refer to objects are guaranteed to be at
707 // the beginning of the field list. ifieldRefCount specifies the
708 // number of reference fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709 size_t num_ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700710
711 // number of fields that are object refs
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700712 size_t num_reference_ifields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700713 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700714
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715 // Bitmap of offsets of ifields.
716 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700717
718 // source file name, if known. Otherwise, NULL.
719 const char* source_file_;
720
721 // Static fields
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 size_t num_sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700723 StaticField** sfields_;
724
725 private:
726 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700727};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700728std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700729
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700730class DataObject : public Object {
731 public:
732 uint32_t fields_[1];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700733 private:
734 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735};
736
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700737class Array : public Object {
738 public:
739 void SetLength(uint32_t length) {
740 length_ = length;
741 }
742
743 private:
744 // The number of array elements.
745 uint32_t length_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700746 Array();
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700747};
748
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700749class CharArray : public Array {
750 private:
751 CharArray();
752};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700753
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700755 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700756 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700757
758 uint32_t hash_code_;
759
760 uint32_t offset_;
761
762 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700763
764 private:
765 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700766};
767
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700768class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700769 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700770 Class* GetClass() const {
771 return klass_;
772 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700773
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700774 void SetClass(Class* klass) {
775 klass_ = klass;
776 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700777
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700778 private:
779 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700780 Class* klass_;
781
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700782 public: // TODO: private
783 // Index into array of vtable offsets. This points into the
784 // ifviPool, which holds the vtables for all interfaces declared by
785 // this class.
786 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700787};
788
789} // namespace art
790
791#endif // ART_SRC_OBJECT_H_