blob: 2b75ff779f6b0deb61fabcd605e38f0d13b64fc6 [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
Carl Shapiro565f5072011-07-10 13:39:43 -070036enum JType {
37 kTypeByte = 'B',
38 kTypeChar = 'C',
39 kTypeDouble = 'D',
40 kTypeFloat = 'F',
41 kTypeInt = 'I',
42 kTypeLong = 'J',
43 kTypeShort = 'S',
44 kTypeBoolean = 'Z',
45 kTypeClass = 'L',
46 kTypeArray= '[',
47 kTypeVoid = 'V',
48};
49
Carl Shapiro3ee755d2011-06-28 12:11:04 -070050static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
51static const uint32_t kAccPrivate = 0x0002; // field, method, ic
52static const uint32_t kAccProtected = 0x0004; // field, method, ic
53static const uint32_t kAccStatic = 0x0008; // field, method, ic
54static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
55static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
56static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
57static const uint32_t kAccVolatile = 0x0040; // field
58static const uint32_t kAccBridge = 0x0040; // method (1.5)
59static const uint32_t kAccTransient = 0x0080; // field
60static const uint32_t kAccVarargs = 0x0080; // method (1.5)
61static const uint32_t kAccNative = 0x0100; // method
62static const uint32_t kAccInterface = 0x0200; // class, ic
63static const uint32_t kAccAbstract = 0x0400; // class, method, ic
64static const uint32_t kAccStrict = 0x0800; // method
65static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
66static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
67static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
68
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069static const uint32_t kAccMiranda = 0x8000; // method
70
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
72static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
73
74
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070075/*
76 * Definitions for packing refOffsets in ClassObject.
77 */
78/*
79 * A magic value for refOffsets. Ignore the bits and walk the super
80 * chain when this is the value.
81 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
82 * fields followed by 2 ref instance fields.]
83 */
84#define CLASS_WALK_SUPER ((unsigned int)(3))
85#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
86#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
87#define CLASS_OFFSET_ALIGNMENT 4
88#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
89/*
90 * Given an offset, return the bit number which would encode that offset.
91 * Local use only.
92 */
93#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
94 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
95 CLASS_OFFSET_ALIGNMENT)
96/*
97 * Is the given offset too large to be encoded?
98 */
99#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
100 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
101/*
102 * Return a single bit, encoding the offset.
103 * Undefined if the offset is too large, as defined above.
104 */
105#define CLASS_BIT_FROM_OFFSET(byteOffset) \
106 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
107/*
108 * Return an offset, given a bit number as returned from CLZ.
109 */
110#define CLASS_OFFSET_FROM_CLZ(rshift) \
111 (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
112
113
Carl Shapiro1fb86202011-06-27 17:43:13 -0700114class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700115 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700116 Class* GetClass() const {
117 return klass_;
118 }
119
120 void MonitorEnter() {
121 monitor_->Enter();
122 }
123
124 void MonitorExit() {
125 monitor_->Exit();
126 }
127
128 void Notify() {
129 monitor_->Notify();
130 }
131
132 void NotifyAll() {
133 monitor_->NotifyAll();
134 }
135
136 void Wait() {
137 monitor_->Wait();
138 }
139
140 void Wait(int64_t timeout) {
141 monitor_->Wait(timeout);
142 }
143
144 void Wait(int64_t timeout, int32_t nanos) {
145 monitor_->Wait(timeout, nanos);
146 }
147
148 void SetObjectAt(size_t offset, Object* new_value) {
149 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
150 *reinterpret_cast<Object**>(raw_addr) = new_value;
151 // TODO: write barrier
152 }
153
154 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700155 Class* klass_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156 Monitor* monitor_;
157
158 private:
159 DISALLOW_COPY_AND_ASSIGN(Object);
160};
161
162class ObjectLock {
163 public:
164 ObjectLock(Object* object) : obj_(object) {
165 CHECK(object != NULL);
166 obj_->MonitorEnter();
167 }
168
169 ~ObjectLock() {
170 obj_->MonitorExit();
171 }
172
173 void Wait(int64_t millis = 0) {
174 return obj_->Wait(millis);
175 }
176
177 void Notify() {
178 obj_->Notify();
179 }
180
181 void NotifyAll() {
182 obj_->NotifyAll();
183 }
184
185 private:
186 Object* obj_;
187 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700188};
189
190class Field {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700191 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700192 Class* GetClass() const {
193 return klass_;
194 }
195
196 const char* GetName() const {
197 return name_;
198 }
199
200 char GetType() const { // TODO: return type
201 return signature_[0];
202 }
203
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700204 const char* GetSignature() const {
205 return signature_;
206 }
207
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700208 public: // TODO: private
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700209 // The class in which this field is declared.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700210 Class* klass_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700211
212 const char* name_;
213
214 // e.g. "I", "[C", "Landroid/os/Debug;"
215 const char* signature_;
216
217 uint32_t access_flags_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700218};
219
220// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221class InstanceField : public Field {
222 public:
223 uint32_t GetOffset() const {
224 return offset_;
225 }
226
227 void SetOffset(size_t num_bytes) {
228 offset_ = num_bytes;
229 }
230
231 // TODO: stl::swap
232 void Swap(InstanceField* that) {
233 InstanceField tmp;
234 memcpy(&tmp, this, sizeof(InstanceField));
235 memcpy(this, that, sizeof(InstanceField));
236 memcpy(that, &tmp, sizeof(InstanceField));
237 }
238
239 private:
240 size_t offset_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700241};
242
243// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700245 public:
246 void SetBoolean(bool z) {
247 CHECK_EQ(GetType(), 'Z');
248 value_.z = z;
249 }
250
251 void SetByte(int8_t b) {
252 CHECK_EQ(GetType(), 'B');
253 value_.b = b;
254 }
255
256 void SetChar(uint16_t c) {
257 CHECK_EQ(GetType(), 'C');
258 value_.c = c;
259 }
260
261 void SetShort(uint16_t s) {
262 CHECK_EQ(GetType(), 'S');
263 value_.s = s;
264 }
265
266 void SetInt(int32_t i) {
267 CHECK_EQ(GetType(), 'I');
268 value_.i = i;
269 }
270
271 int64_t GetLong() {
272 CHECK_EQ(GetType(), 'J');
273 return value_.j;
274 }
275
276 void SetLong(int64_t j) {
277 CHECK_EQ(GetType(), 'J');
278 value_.j = j;
279 }
280
281 void SetFloat(float f) {
282 CHECK_EQ(GetType(), 'F');
283 value_.f = f;
284 }
285
286 void SetDouble(double d) {
287 CHECK_EQ(GetType(), 'D');
288 value_.d = d;
289 }
290
291 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700292 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700293 value_.l = l;
294 // TODO: write barrier
295 }
296
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700297 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700298 JValue value_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700299};
300
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301class Method {
302 public:
303 // Returns the method name.
304 // TODO: example
305 const StringPiece& GetName() const {
306 return name_;
307 }
308
309 Class* GetClass() const {
310 return klass_;
311 }
312
313 // const char* GetReturnTypeDescriptor() const {
314 // return dex_file_->GetRaw()->dexStringByTypeIdx(proto_id_.return_type_id_);
315 // }
316
317 // Returns true if the method is declared public.
318 bool IsPublic() const {
319 return (access_flags_ & kAccPublic) != 0;
320 }
321
322 // Returns true if the method is declared private.
323 bool IsPrivate() const {
324 return (access_flags_ & kAccPrivate) != 0;
325 }
326
327 // Returns true if the method is declared static.
328 bool IsStatic() const {
329 return (access_flags_ & kAccStatic) != 0;
330 }
331
332 // Returns true if the method is declared synchronized.
333 bool IsSynchronized() const {
334 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
335 return (access_flags_ & synchonized) != 0;
336 }
337
338 // Returns true if the method is declared final.
339 bool IsFinal() const {
340 return (access_flags_ & kAccFinal) != 0;
341 }
342
343 // Returns true if the method is declared native.
344 bool IsNative() const {
345 return (access_flags_ & kAccNative) != 0;
346 }
347
348 // Returns true if the method is declared abstract.
349 bool IsAbstract() const {
350 return (access_flags_ & kAccAbstract) != 0;
351 }
352
353 bool IsSynthetic() const {
354 return (access_flags_ & kAccSynthetic) != 0;
355 }
356
357 // Number of argument registers required by the prototype.
358 uint32_t NumArgRegisters();
359
360 bool HasSameNameAndPrototype(const Method* that) const {
361 return HasSameName(that) && HasSamePrototype(that);
362 }
363
364 bool HasSameName(const Method* that) const {
365 return this->GetName() == that->GetName();
366 }
367
368 bool HasSamePrototype(const Method* that) const {
369 return HasSameReturnType(that) && HasSameArgumentTypes(that);
370 }
371
372 bool HasSameReturnType(const Method* that) const;
373
374 bool HasSameArgumentTypes(const Method* that) const;
375
376 public: // TODO: private
377 // the class we are a part of
378 Class* klass_;
379
380 // access flags; low 16 bits are defined by spec (could be uint16_t?)
381 uint32_t access_flags_;
382
383 // For concrete virtual methods, this is the offset of the method
384 // in "vtable".
385 //
386 // For abstract methods in an interface class, this is the offset
387 // of the method in "iftable[n]->methodIndexArray".
388 uint16_t method_index_;
389
390 // Method bounds; not needed for an abstract method.
391 //
392 // For a native method, we compute the size of the argument list, and
393 // set "insSize" and "registerSize" equal to it.
394 uint16_t num_registers_; // ins + locals
395 uint16_t num_outs_;
396 uint16_t num_ins_;
397
398 // method name, e.g. "<init>" or "eatLunch"
399 StringPiece name_;
400
401 // A pointer to the DEX file this class was loaded from or NULL for
402 // proxy objects.
403 DexFile* dex_file_;
404
405 // Method prototype descriptor string (return and argument types).
406 uint32_t proto_idx_;
407
408 // The short-form method descriptor string.
409 StringPiece shorty_;
410
411 // A pointer to the memory-mapped DEX code.
412 const uint16_t* insns_;
413};
414
Carl Shapiro1fb86202011-06-27 17:43:13 -0700415// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700416class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700417 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700418 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700419 kStatusError = -1,
420 kStatusNotReady = 0,
421 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
422 kStatusLoaded = 2, // DEX idx values resolved
423 kStatusResolved = 3, // part of linking
424 kStatusVerifying = 4, // in the process of being verified
425 kStatusVerified = 5, // logically part of linking; done pre-init
426 kStatusInitializing = 6, // class init in progress
427 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700428 };
429
430 enum PrimitiveType {
431 kPrimNot = -1
432 };
433
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700434 Class* GetSuperClass() const {
435 return super_class_;
436 }
437
438 uint32_t GetSuperClassIdx() const {
439 return super_class_idx_;
440 }
441
442 bool HasSuperClass() const {
443 return super_class_ != NULL;
444 }
445
446 Object* GetClassLoader() const {
447 return class_loader_;
448 }
449
450 DexFile* GetDexFile() const {
451 return dex_file_;
452 }
453
454 Class* GetComponentType() const {
455 return component_type_;
456 }
457
458 const StringPiece& GetDescriptor() const {
459 return descriptor_;
460 }
461
462 Status GetStatus() const {
463 return status_;
464 }
465
466 void SetStatus(Status new_status) {
467 // TODO: validate transition
468 status_ = new_status;
469 }
470
471 bool IsErroneous() const {
472 return GetStatus() == kStatusError;
473 }
474
475 bool IsVerified() const {
476 return GetStatus() >= kStatusVerified;
477 }
478
479 bool IsLinked() const {
480 return GetStatus() >= kStatusResolved;
481 }
482
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700483 // Returns true if this class is in the same packages as that class.
484 bool IsInSamePackage(const Class* that) const;
485
486 static bool IsInSamePackage(const char* descriptor1, const char* descriptor2);
487
488 // Returns true if this class represents an array class.
489 bool IsArray() const {
490 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
491 }
492
493 // Returns true if the class is an interface.
494 bool IsInterface() const {
495 return (access_flags_ & kAccInterface) != 0;
496 }
497
498 // Returns true if the class is declared public.
499 bool IsPublic() const {
500 return (access_flags_ & kAccPublic) != 0;
501 }
502
503 // Returns true if the class is declared final.
504 bool IsFinal() const {
505 return (access_flags_ & kAccFinal) != 0;
506 }
507
508 // Returns true if the class is abstract.
509 bool IsAbstract() const {
510 return (access_flags_ & kAccAbstract) != 0;
511 }
512
513 // Returns true if the class is an annotation.
514 bool IsAnnotation() const {
515 return (access_flags_ & kAccAnnotation) != 0;
516 }
517
518 // Returns true if the class is a primitive type.
519 bool IsPrimitive() const {
520 return primitive_type_ != kPrimNot;
521 }
522
523 // Returns true if this class can access that class.
524 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700525 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700526 }
527
Carl Shapiro1fb86202011-06-27 17:43:13 -0700528 // Returns the size in bytes of a class object instance with the
529 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700530 // static size_t Size(size_t num_sfields) {
531 // return OFFSETOF_MEMBER(Class, sfields_) + sizeof(StaticField) * num_sfields;
532 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700533
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700534 // Returns the number of static, private, and constructor methods.
535 size_t NumDirectMethods() const {
536 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700537 }
538
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700539 Method* GetDirectMethod(uint32_t i) const {
540 return &direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700541 }
542
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 // Returns the number of non-inherited virtual methods.
544 size_t NumVirtualMethods() const {
545 return num_virtual_methods_;
546 }
547
548 Method* GetVirtualMethod(uint32_t i) const {
549 return &virtual_methods_[i];
550 }
551
552 size_t NumInstanceFields() const {
553 return num_ifields_;
554 }
555
556 size_t NumReferenceInstanceFields() const {
557 return num_reference_ifields_;
558 }
559
560 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
561 return &ifields_[i];
562 }
563
564 size_t NumStaticFields() const {
565 return num_sfields_;
566 }
567
568 StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t
569 return &sfields_[i];
570 }
571
572 uint32_t GetReferenceOffsets() const {
573 return reference_offsets_;
574 }
575
576 void SetReferenceOffsets(uint32_t new_reference_offsets) {
577 reference_offsets_ = new_reference_offsets;
578 }
579
580 Method* FindDirectMethodLocally(const StringPiece& name,
581 const StringPiece& descriptor) const;
582
Carl Shapiro1fb86202011-06-27 17:43:13 -0700583 public: // TODO: private
584 // leave space for instance data; we could access fields directly if
585 // we freeze the definition of java/lang/Class
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700586#define CLASS_FIELD_SLOTS 4
Carl Shapiro1fb86202011-06-27 17:43:13 -0700587 uint32_t instance_data_[CLASS_FIELD_SLOTS];
588#undef CLASS_FIELD_SLOTS
589
590 // UTF-8 descriptor for the class from constant pool
591 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700592 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700593
594 // Proxy classes have their descriptor allocated on the native heap.
595 // When this field is non-NULL it must be explicitly freed.
596 char* descriptor_alloc_;
597
598 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700599 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700600
Carl Shapiro1fb86202011-06-27 17:43:13 -0700601 // DexFile from which we came; needed to resolve constant pool entries
602 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
603 DexFile* dex_file_;
604
605 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700606 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700607
608 // if class verify fails, we must return same error on subsequent tries
609 Class* verify_error_class_;
610
611 // threadId, used to check for recursive <clinit> invocation
612 uint32_t clinit_thread_id_;
613
614 // Total object size; used when allocating storage on gc heap. (For
615 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700616 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700617
618 // For array classes, the class object for base element, for
619 // instanceof/checkcast (for String[][][], this will be String).
620 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700621 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700622
623 // For array classes, the number of array dimensions, e.g. int[][]
624 // is 2. Otherwise 0.
625 int32_t array_rank_;
626
627 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
628 PrimitiveType primitive_type_;
629
630 // The superclass, or NULL if this is java.lang.Object or a
631 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632 Class* super_class_; // TODO: make an instance field
633 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700634
635 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700636 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700637
638 // initiating class loader list
639 // NOTE: for classes with low serialNumber, these are unused, and the
640 // values are kept in a table in gDvm.
641 //InitiatingLoaderList initiating_loader_list_;
642
643 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700645 Class** interfaces_;
646
647 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700648 size_t num_direct_methods_;
649 Method* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700650
651 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700652 size_t num_virtual_methods_;
653 Method* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700654
655 // Virtual method table (vtable), for use by "invoke-virtual". The
656 // vtable from the superclass is copied in, and virtual methods from
657 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700659 Method** vtable_;
660
661 // Interface table (iftable), one entry per interface supported by
662 // this class. That means one entry for each interface we support
663 // directly, indirectly via superclass, or indirectly via
664 // superinterface. This will be null if neither we nor our
665 // superclass implement any interfaces.
666 //
667 // Why we need this: given "class Foo implements Face", declare
668 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
669 // is part of the Face interface. We can't easily use a single
670 // vtable.
671 //
672 // For every interface a concrete class implements, we create a list
673 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700674 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700675 InterfaceEntry* iftable_;
676
677 // The interface vtable indices for iftable get stored here. By
678 // placing them all in a single pool for each class that implements
679 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700680 size_t ifvi_pool_count_;
681 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700682
683 // instance fields
684 //
685 // These describe the layout of the contents of a
686 // DataObject-compatible Object. Note that only the fields directly
687 // declared by this class are listed in ifields; fields declared by
688 // a superclass are listed in the superclass's ClassObject.ifields.
689 //
690 // All instance fields that refer to objects are guaranteed to be at
691 // the beginning of the field list. ifieldRefCount specifies the
692 // number of reference fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700693 size_t num_ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700694
695 // number of fields that are object refs
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700696 size_t num_reference_ifields_;
697 InstanceField* ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700698
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700699 // Bitmap of offsets of ifields.
700 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700701
702 // source file name, if known. Otherwise, NULL.
703 const char* source_file_;
704
705 // Static fields
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700706 size_t num_sfields_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700707 StaticField* sfields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700708};
709
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700710class DataObject : public Object {
711 public:
712 uint32_t fields_[1];
713};
714
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700715class Array : public Object {
716 public:
717 void SetLength(uint32_t length) {
718 length_ = length;
719 }
720
721 private:
722 // The number of array elements.
723 uint32_t length_;
724};
725
726class CharArray : public Array {};
727
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700729 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700730 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700731
732 uint32_t hash_code_;
733
734 uint32_t offset_;
735
736 uint32_t count_;
737};
738
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700739class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700740 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700741 Class* GetClass() const {
742 return klass_;
743 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700744
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 void SetClass(Class* klass) {
746 klass_ = klass;
747 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700748
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700749 private:
750 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700751 Class* klass_;
752
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700753 public: // TODO: private
754 // Index into array of vtable offsets. This points into the
755 // ifviPool, which holds the vtables for all interfaces declared by
756 // this class.
757 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700758};
759
760} // namespace art
761
762#endif // ART_SRC_OBJECT_H_