blob: 8252de1e1177a59eb9ff0c5d57b5814fed3f5f78 [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
36static 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)
54
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070055static const uint32_t kAccMiranda = 0x8000; // method
56
Carl Shapiro3ee755d2011-06-28 12:11:04 -070057static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
58static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
59
60
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061/*
62 * Definitions for packing refOffsets in ClassObject.
63 */
64/*
65 * A magic value for refOffsets. Ignore the bits and walk the super
66 * chain when this is the value.
67 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
68 * fields followed by 2 ref instance fields.]
69 */
70#define CLASS_WALK_SUPER ((unsigned int)(3))
71#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
72#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
73#define CLASS_OFFSET_ALIGNMENT 4
74#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
75/*
76 * Given an offset, return the bit number which would encode that offset.
77 * Local use only.
78 */
79#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
80 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
81 CLASS_OFFSET_ALIGNMENT)
82/*
83 * Is the given offset too large to be encoded?
84 */
85#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
86 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
87/*
88 * Return a single bit, encoding the offset.
89 * Undefined if the offset is too large, as defined above.
90 */
91#define CLASS_BIT_FROM_OFFSET(byteOffset) \
92 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
93/*
94 * Return an offset, given a bit number as returned from CLZ.
95 */
96#define CLASS_OFFSET_FROM_CLZ(rshift) \
97 (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
98
99
Carl Shapiro1fb86202011-06-27 17:43:13 -0700100class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700101 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700102 Class* GetClass() const {
103 return klass_;
104 }
105
106 void MonitorEnter() {
107 monitor_->Enter();
108 }
109
110 void MonitorExit() {
111 monitor_->Exit();
112 }
113
114 void Notify() {
115 monitor_->Notify();
116 }
117
118 void NotifyAll() {
119 monitor_->NotifyAll();
120 }
121
122 void Wait() {
123 monitor_->Wait();
124 }
125
126 void Wait(int64_t timeout) {
127 monitor_->Wait(timeout);
128 }
129
130 void Wait(int64_t timeout, int32_t nanos) {
131 monitor_->Wait(timeout, nanos);
132 }
133
134 void SetObjectAt(size_t offset, Object* new_value) {
135 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
136 *reinterpret_cast<Object**>(raw_addr) = new_value;
137 // TODO: write barrier
138 }
139
140 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700141 Class* klass_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700142 Monitor* monitor_;
143
144 private:
145 DISALLOW_COPY_AND_ASSIGN(Object);
146};
147
148class ObjectLock {
149 public:
150 ObjectLock(Object* object) : obj_(object) {
151 CHECK(object != NULL);
152 obj_->MonitorEnter();
153 }
154
155 ~ObjectLock() {
156 obj_->MonitorExit();
157 }
158
159 void Wait(int64_t millis = 0) {
160 return obj_->Wait(millis);
161 }
162
163 void Notify() {
164 obj_->Notify();
165 }
166
167 void NotifyAll() {
168 obj_->NotifyAll();
169 }
170
171 private:
172 Object* obj_;
173 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700174};
175
176class Field {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700177 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700178 Class* GetClass() const {
179 return klass_;
180 }
181
182 const char* GetName() const {
183 return name_;
184 }
185
186 char GetType() const { // TODO: return type
187 return signature_[0];
188 }
189
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700190 const char* GetSignature() const {
191 return signature_;
192 }
193
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700194 public: // TODO: private
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700195 // The class in which this field is declared.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700196 Class* klass_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700197
198 const char* name_;
199
200 // e.g. "I", "[C", "Landroid/os/Debug;"
201 const char* signature_;
202
203 uint32_t access_flags_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700204};
205
206// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700207class InstanceField : public Field {
208 public:
209 uint32_t GetOffset() const {
210 return offset_;
211 }
212
213 void SetOffset(size_t num_bytes) {
214 offset_ = num_bytes;
215 }
216
217 // TODO: stl::swap
218 void Swap(InstanceField* that) {
219 InstanceField tmp;
220 memcpy(&tmp, this, sizeof(InstanceField));
221 memcpy(this, that, sizeof(InstanceField));
222 memcpy(that, &tmp, sizeof(InstanceField));
223 }
224
225 private:
226 size_t offset_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700227};
228
229// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700230class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700231 public:
232 void SetBoolean(bool z) {
233 CHECK_EQ(GetType(), 'Z');
234 value_.z = z;
235 }
236
237 void SetByte(int8_t b) {
238 CHECK_EQ(GetType(), 'B');
239 value_.b = b;
240 }
241
242 void SetChar(uint16_t c) {
243 CHECK_EQ(GetType(), 'C');
244 value_.c = c;
245 }
246
247 void SetShort(uint16_t s) {
248 CHECK_EQ(GetType(), 'S');
249 value_.s = s;
250 }
251
252 void SetInt(int32_t i) {
253 CHECK_EQ(GetType(), 'I');
254 value_.i = i;
255 }
256
257 int64_t GetLong() {
258 CHECK_EQ(GetType(), 'J');
259 return value_.j;
260 }
261
262 void SetLong(int64_t j) {
263 CHECK_EQ(GetType(), 'J');
264 value_.j = j;
265 }
266
267 void SetFloat(float f) {
268 CHECK_EQ(GetType(), 'F');
269 value_.f = f;
270 }
271
272 void SetDouble(double d) {
273 CHECK_EQ(GetType(), 'D');
274 value_.d = d;
275 }
276
277 void SetObject(Object* l) {
278 CHECK_EQ(GetType(), 'L');
279 value_.l = l;
280 // TODO: write barrier
281 }
282
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700283 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700284 JValue value_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700285};
286
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700287class Method {
288 public:
289 // Returns the method name.
290 // TODO: example
291 const StringPiece& GetName() const {
292 return name_;
293 }
294
295 Class* GetClass() const {
296 return klass_;
297 }
298
299 // const char* GetReturnTypeDescriptor() const {
300 // return dex_file_->GetRaw()->dexStringByTypeIdx(proto_id_.return_type_id_);
301 // }
302
303 // Returns true if the method is declared public.
304 bool IsPublic() const {
305 return (access_flags_ & kAccPublic) != 0;
306 }
307
308 // Returns true if the method is declared private.
309 bool IsPrivate() const {
310 return (access_flags_ & kAccPrivate) != 0;
311 }
312
313 // Returns true if the method is declared static.
314 bool IsStatic() const {
315 return (access_flags_ & kAccStatic) != 0;
316 }
317
318 // Returns true if the method is declared synchronized.
319 bool IsSynchronized() const {
320 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
321 return (access_flags_ & synchonized) != 0;
322 }
323
324 // Returns true if the method is declared final.
325 bool IsFinal() const {
326 return (access_flags_ & kAccFinal) != 0;
327 }
328
329 // Returns true if the method is declared native.
330 bool IsNative() const {
331 return (access_flags_ & kAccNative) != 0;
332 }
333
334 // Returns true if the method is declared abstract.
335 bool IsAbstract() const {
336 return (access_flags_ & kAccAbstract) != 0;
337 }
338
339 bool IsSynthetic() const {
340 return (access_flags_ & kAccSynthetic) != 0;
341 }
342
343 // Number of argument registers required by the prototype.
344 uint32_t NumArgRegisters();
345
346 bool HasSameNameAndPrototype(const Method* that) const {
347 return HasSameName(that) && HasSamePrototype(that);
348 }
349
350 bool HasSameName(const Method* that) const {
351 return this->GetName() == that->GetName();
352 }
353
354 bool HasSamePrototype(const Method* that) const {
355 return HasSameReturnType(that) && HasSameArgumentTypes(that);
356 }
357
358 bool HasSameReturnType(const Method* that) const;
359
360 bool HasSameArgumentTypes(const Method* that) const;
361
362 public: // TODO: private
363 // the class we are a part of
364 Class* klass_;
365
366 // access flags; low 16 bits are defined by spec (could be uint16_t?)
367 uint32_t access_flags_;
368
369 // For concrete virtual methods, this is the offset of the method
370 // in "vtable".
371 //
372 // For abstract methods in an interface class, this is the offset
373 // of the method in "iftable[n]->methodIndexArray".
374 uint16_t method_index_;
375
376 // Method bounds; not needed for an abstract method.
377 //
378 // For a native method, we compute the size of the argument list, and
379 // set "insSize" and "registerSize" equal to it.
380 uint16_t num_registers_; // ins + locals
381 uint16_t num_outs_;
382 uint16_t num_ins_;
383
384 // method name, e.g. "<init>" or "eatLunch"
385 StringPiece name_;
386
387 // A pointer to the DEX file this class was loaded from or NULL for
388 // proxy objects.
389 DexFile* dex_file_;
390
391 // Method prototype descriptor string (return and argument types).
392 uint32_t proto_idx_;
393
394 // The short-form method descriptor string.
395 StringPiece shorty_;
396
397 // A pointer to the memory-mapped DEX code.
398 const uint16_t* insns_;
399};
400
Carl Shapiro1fb86202011-06-27 17:43:13 -0700401// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700402class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700403 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700404 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700405 kStatusError = -1,
406 kStatusNotReady = 0,
407 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
408 kStatusLoaded = 2, // DEX idx values resolved
409 kStatusResolved = 3, // part of linking
410 kStatusVerifying = 4, // in the process of being verified
411 kStatusVerified = 5, // logically part of linking; done pre-init
412 kStatusInitializing = 6, // class init in progress
413 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700414 };
415
416 enum PrimitiveType {
417 kPrimNot = -1
418 };
419
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700420 Class* GetSuperClass() const {
421 return super_class_;
422 }
423
424 uint32_t GetSuperClassIdx() const {
425 return super_class_idx_;
426 }
427
428 bool HasSuperClass() const {
429 return super_class_ != NULL;
430 }
431
432 Object* GetClassLoader() const {
433 return class_loader_;
434 }
435
436 DexFile* GetDexFile() const {
437 return dex_file_;
438 }
439
440 Class* GetComponentType() const {
441 return component_type_;
442 }
443
444 const StringPiece& GetDescriptor() const {
445 return descriptor_;
446 }
447
448 Status GetStatus() const {
449 return status_;
450 }
451
452 void SetStatus(Status new_status) {
453 // TODO: validate transition
454 status_ = new_status;
455 }
456
457 bool IsErroneous() const {
458 return GetStatus() == kStatusError;
459 }
460
461 bool IsVerified() const {
462 return GetStatus() >= kStatusVerified;
463 }
464
465 bool IsLinked() const {
466 return GetStatus() >= kStatusResolved;
467 }
468
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700469 // Returns true if this class is in the same packages as that class.
470 bool IsInSamePackage(const Class* that) const;
471
472 static bool IsInSamePackage(const char* descriptor1, const char* descriptor2);
473
474 // Returns true if this class represents an array class.
475 bool IsArray() const {
476 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
477 }
478
479 // Returns true if the class is an interface.
480 bool IsInterface() const {
481 return (access_flags_ & kAccInterface) != 0;
482 }
483
484 // Returns true if the class is declared public.
485 bool IsPublic() const {
486 return (access_flags_ & kAccPublic) != 0;
487 }
488
489 // Returns true if the class is declared final.
490 bool IsFinal() const {
491 return (access_flags_ & kAccFinal) != 0;
492 }
493
494 // Returns true if the class is abstract.
495 bool IsAbstract() const {
496 return (access_flags_ & kAccAbstract) != 0;
497 }
498
499 // Returns true if the class is an annotation.
500 bool IsAnnotation() const {
501 return (access_flags_ & kAccAnnotation) != 0;
502 }
503
504 // Returns true if the class is a primitive type.
505 bool IsPrimitive() const {
506 return primitive_type_ != kPrimNot;
507 }
508
509 // Returns true if this class can access that class.
510 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700511 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700512 }
513
Carl Shapiro1fb86202011-06-27 17:43:13 -0700514 // Returns the size in bytes of a class object instance with the
515 // given number of static fields.
516 static size_t Size(size_t num_sfields) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700517 return OFFSETOF_MEMBER(Class, sfields_) + sizeof(StaticField) * num_sfields;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700518 }
519
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700520 // Returns the number of static, private, and constructor methods.
521 size_t NumDirectMethods() const {
522 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700523 }
524
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700525 Method* GetDirectMethod(uint32_t i) const {
526 return &direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700527 }
528
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700529 // Returns the number of non-inherited virtual methods.
530 size_t NumVirtualMethods() const {
531 return num_virtual_methods_;
532 }
533
534 Method* GetVirtualMethod(uint32_t i) const {
535 return &virtual_methods_[i];
536 }
537
538 size_t NumInstanceFields() const {
539 return num_ifields_;
540 }
541
542 size_t NumReferenceInstanceFields() const {
543 return num_reference_ifields_;
544 }
545
546 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
547 return &ifields_[i];
548 }
549
550 size_t NumStaticFields() const {
551 return num_sfields_;
552 }
553
554 StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t
555 return &sfields_[i];
556 }
557
558 uint32_t GetReferenceOffsets() const {
559 return reference_offsets_;
560 }
561
562 void SetReferenceOffsets(uint32_t new_reference_offsets) {
563 reference_offsets_ = new_reference_offsets;
564 }
565
566 Method* FindDirectMethodLocally(const StringPiece& name,
567 const StringPiece& descriptor) const;
568
Carl Shapiro1fb86202011-06-27 17:43:13 -0700569 public: // TODO: private
570 // leave space for instance data; we could access fields directly if
571 // we freeze the definition of java/lang/Class
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700572#define CLASS_FIELD_SLOTS 4
Carl Shapiro1fb86202011-06-27 17:43:13 -0700573 uint32_t instance_data_[CLASS_FIELD_SLOTS];
574#undef CLASS_FIELD_SLOTS
575
576 // UTF-8 descriptor for the class from constant pool
577 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700579
580 // Proxy classes have their descriptor allocated on the native heap.
581 // When this field is non-NULL it must be explicitly freed.
582 char* descriptor_alloc_;
583
584 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700585 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700586
Carl Shapiro1fb86202011-06-27 17:43:13 -0700587 // DexFile from which we came; needed to resolve constant pool entries
588 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
589 DexFile* dex_file_;
590
591 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700592 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700593
594 // if class verify fails, we must return same error on subsequent tries
595 Class* verify_error_class_;
596
597 // threadId, used to check for recursive <clinit> invocation
598 uint32_t clinit_thread_id_;
599
600 // Total object size; used when allocating storage on gc heap. (For
601 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700602 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700603
604 // For array classes, the class object for base element, for
605 // instanceof/checkcast (for String[][][], this will be String).
606 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700607 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700608
609 // For array classes, the number of array dimensions, e.g. int[][]
610 // is 2. Otherwise 0.
611 int32_t array_rank_;
612
613 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
614 PrimitiveType primitive_type_;
615
616 // The superclass, or NULL if this is java.lang.Object or a
617 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700618 Class* super_class_; // TODO: make an instance field
619 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700620
621 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700622 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700623
624 // initiating class loader list
625 // NOTE: for classes with low serialNumber, these are unused, and the
626 // values are kept in a table in gDvm.
627 //InitiatingLoaderList initiating_loader_list_;
628
629 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700630 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700631 Class** interfaces_;
632
633 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700634 size_t num_direct_methods_;
635 Method* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700636
637 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700638 size_t num_virtual_methods_;
639 Method* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700640
641 // Virtual method table (vtable), for use by "invoke-virtual". The
642 // vtable from the superclass is copied in, and virtual methods from
643 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700645 Method** vtable_;
646
647 // Interface table (iftable), one entry per interface supported by
648 // this class. That means one entry for each interface we support
649 // directly, indirectly via superclass, or indirectly via
650 // superinterface. This will be null if neither we nor our
651 // superclass implement any interfaces.
652 //
653 // Why we need this: given "class Foo implements Face", declare
654 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
655 // is part of the Face interface. We can't easily use a single
656 // vtable.
657 //
658 // For every interface a concrete class implements, we create a list
659 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700660 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700661 InterfaceEntry* iftable_;
662
663 // The interface vtable indices for iftable get stored here. By
664 // placing them all in a single pool for each class that implements
665 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700666 size_t ifvi_pool_count_;
667 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700668
669 // instance fields
670 //
671 // These describe the layout of the contents of a
672 // DataObject-compatible Object. Note that only the fields directly
673 // declared by this class are listed in ifields; fields declared by
674 // a superclass are listed in the superclass's ClassObject.ifields.
675 //
676 // All instance fields that refer to objects are guaranteed to be at
677 // the beginning of the field list. ifieldRefCount specifies the
678 // number of reference fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700679 size_t num_ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700680
681 // number of fields that are object refs
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700682 size_t num_reference_ifields_;
683 InstanceField* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700684
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700685 // Bitmap of offsets of ifields.
686 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700687
688 // source file name, if known. Otherwise, NULL.
689 const char* source_file_;
690
691 // Static fields
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700692 size_t num_sfields_;
693 StaticField sfields_[]; // MUST be last item
Carl Shapiro1fb86202011-06-27 17:43:13 -0700694};
695
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696class DataObject : public Object {
697 public:
698 uint32_t fields_[1];
699};
700
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700701class Array : public Object {
702 public:
703 void SetLength(uint32_t length) {
704 length_ = length;
705 }
706
707 private:
708 // The number of array elements.
709 uint32_t length_;
710};
711
712class CharArray : public Array {};
713
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700714class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700715 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700716 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700717
718 uint32_t hash_code_;
719
720 uint32_t offset_;
721
722 uint32_t count_;
723};
724
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700725class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700726 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700727 Class* GetClass() const {
728 return klass_;
729 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700730
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700731 void SetClass(Class* klass) {
732 klass_ = klass;
733 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700734
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735 private:
736 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700737 Class* klass_;
738
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739 public: // TODO: private
740 // Index into array of vtable offsets. This points into the
741 // ifviPool, which holds the vtables for all interfaces declared by
742 // this class.
743 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700744};
745
746} // namespace art
747
748#endif // ART_SRC_OBJECT_H_