blob: d7df9dc0e54933ce0328aed7047b49e43e21d1e6 [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:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700159 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700160 DISALLOW_COPY_AND_ASSIGN(Object);
161};
162
163class ObjectLock {
164 public:
165 ObjectLock(Object* object) : obj_(object) {
166 CHECK(object != NULL);
167 obj_->MonitorEnter();
168 }
169
170 ~ObjectLock() {
171 obj_->MonitorExit();
172 }
173
174 void Wait(int64_t millis = 0) {
175 return obj_->Wait(millis);
176 }
177
178 void Notify() {
179 obj_->Notify();
180 }
181
182 void NotifyAll() {
183 obj_->NotifyAll();
184 }
185
186 private:
187 Object* obj_;
188 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700189};
190
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700191class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700192 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700193 Class* GetClass() const {
194 return klass_;
195 }
196
197 const char* GetName() const {
198 return name_;
199 }
200
201 char GetType() const { // TODO: return type
202 return signature_[0];
203 }
204
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700205 const char* GetSignature() const {
206 return signature_;
207 }
208
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700210#define FIELD_FIELD_SLOTS 1+6
211 // AccessibleObject #0 flag
212 // Field #0 declaringClass
213 // Field #1 genericType
214 // Field #2 genericTypesAreInitialized
215 // Field #3 name
216 // Field #4 slot
217 // Field #5 type
218 uint32_t instance_data_[FIELD_FIELD_SLOTS];
219#undef FIELD_FIELD_SLOTS
220
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700221 // The class in which this field is declared.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700222 Class* klass_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700223
224 const char* name_;
225
226 // e.g. "I", "[C", "Landroid/os/Debug;"
227 const char* signature_;
228
229 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700230
231 private:
232 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700233};
234
235// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236class InstanceField : public Field {
237 public:
238 uint32_t GetOffset() const {
239 return offset_;
240 }
241
242 void SetOffset(size_t num_bytes) {
243 offset_ = num_bytes;
244 }
245
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700246 private:
247 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700248 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700249};
250
251// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700252class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700253 public:
254 void SetBoolean(bool z) {
255 CHECK_EQ(GetType(), 'Z');
256 value_.z = z;
257 }
258
259 void SetByte(int8_t b) {
260 CHECK_EQ(GetType(), 'B');
261 value_.b = b;
262 }
263
264 void SetChar(uint16_t c) {
265 CHECK_EQ(GetType(), 'C');
266 value_.c = c;
267 }
268
269 void SetShort(uint16_t s) {
270 CHECK_EQ(GetType(), 'S');
271 value_.s = s;
272 }
273
274 void SetInt(int32_t i) {
275 CHECK_EQ(GetType(), 'I');
276 value_.i = i;
277 }
278
279 int64_t GetLong() {
280 CHECK_EQ(GetType(), 'J');
281 return value_.j;
282 }
283
284 void SetLong(int64_t j) {
285 CHECK_EQ(GetType(), 'J');
286 value_.j = j;
287 }
288
289 void SetFloat(float f) {
290 CHECK_EQ(GetType(), 'F');
291 value_.f = f;
292 }
293
294 void SetDouble(double d) {
295 CHECK_EQ(GetType(), 'D');
296 value_.d = d;
297 }
298
299 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700300 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700301 value_.l = l;
302 // TODO: write barrier
303 }
304
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700305 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700306 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700307 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700308};
309
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700310class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311 public:
312 // Returns the method name.
313 // TODO: example
314 const StringPiece& GetName() const {
315 return name_;
316 }
317
318 Class* GetClass() const {
319 return klass_;
320 }
321
322 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700323 // return klass_->GetDexFile_->GetRaw()->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700324 // }
325
326 // Returns true if the method is declared public.
327 bool IsPublic() const {
328 return (access_flags_ & kAccPublic) != 0;
329 }
330
331 // Returns true if the method is declared private.
332 bool IsPrivate() const {
333 return (access_flags_ & kAccPrivate) != 0;
334 }
335
336 // Returns true if the method is declared static.
337 bool IsStatic() const {
338 return (access_flags_ & kAccStatic) != 0;
339 }
340
341 // Returns true if the method is declared synchronized.
342 bool IsSynchronized() const {
343 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
344 return (access_flags_ & synchonized) != 0;
345 }
346
347 // Returns true if the method is declared final.
348 bool IsFinal() const {
349 return (access_flags_ & kAccFinal) != 0;
350 }
351
352 // Returns true if the method is declared native.
353 bool IsNative() const {
354 return (access_flags_ & kAccNative) != 0;
355 }
356
357 // Returns true if the method is declared abstract.
358 bool IsAbstract() const {
359 return (access_flags_ & kAccAbstract) != 0;
360 }
361
362 bool IsSynthetic() const {
363 return (access_flags_ & kAccSynthetic) != 0;
364 }
365
366 // Number of argument registers required by the prototype.
367 uint32_t NumArgRegisters();
368
369 bool HasSameNameAndPrototype(const Method* that) const {
370 return HasSameName(that) && HasSamePrototype(that);
371 }
372
373 bool HasSameName(const Method* that) const {
374 return this->GetName() == that->GetName();
375 }
376
377 bool HasSamePrototype(const Method* that) const {
378 return HasSameReturnType(that) && HasSameArgumentTypes(that);
379 }
380
381 bool HasSameReturnType(const Method* that) const;
382
383 bool HasSameArgumentTypes(const Method* that) const;
384
385 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700386#define METHOD_FIELD_SLOTS 1+11
387 // AccessibleObject #0 flag
388 // Method #0 declaringClass
389 // Method #1 exceptionTypes
390 // Method #2 formalTypeParameters
391 // Method #3 genericExceptionTypes
392 // Method #4 genericParameterTypes
393 // Method #5 genericReturnType
394 // Method #6 genericTypesAreInitialized
395 // Method #7 name
396 // Method #8 parameterTypes
397 // Method #9 returnType
398 // Method #10 slot
399 uint32_t instance_data_[METHOD_FIELD_SLOTS];
400#undef METHOD_FIELD_SLOTS
401
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402 // the class we are a part of
403 Class* klass_;
404
405 // access flags; low 16 bits are defined by spec (could be uint16_t?)
406 uint32_t access_flags_;
407
408 // For concrete virtual methods, this is the offset of the method
409 // in "vtable".
410 //
411 // For abstract methods in an interface class, this is the offset
412 // of the method in "iftable[n]->methodIndexArray".
413 uint16_t method_index_;
414
415 // Method bounds; not needed for an abstract method.
416 //
417 // For a native method, we compute the size of the argument list, and
418 // set "insSize" and "registerSize" equal to it.
419 uint16_t num_registers_; // ins + locals
420 uint16_t num_outs_;
421 uint16_t num_ins_;
422
423 // method name, e.g. "<init>" or "eatLunch"
424 StringPiece name_;
425
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700426 // Method prototype descriptor string (return and argument types).
427 uint32_t proto_idx_;
428
429 // The short-form method descriptor string.
430 StringPiece shorty_;
431
432 // A pointer to the memory-mapped DEX code.
433 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700434
435 private:
436 Method();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700437};
438
Carl Shapiro1fb86202011-06-27 17:43:13 -0700439// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700440class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700441 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700442 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700443 kStatusError = -1,
444 kStatusNotReady = 0,
445 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
446 kStatusLoaded = 2, // DEX idx values resolved
447 kStatusResolved = 3, // part of linking
448 kStatusVerifying = 4, // in the process of being verified
449 kStatusVerified = 5, // logically part of linking; done pre-init
450 kStatusInitializing = 6, // class init in progress
451 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700452 };
453
454 enum PrimitiveType {
455 kPrimNot = -1
456 };
457
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700458 Class* GetSuperClass() const {
459 return super_class_;
460 }
461
462 uint32_t GetSuperClassIdx() const {
463 return super_class_idx_;
464 }
465
466 bool HasSuperClass() const {
467 return super_class_ != NULL;
468 }
469
470 Object* GetClassLoader() const {
471 return class_loader_;
472 }
473
474 DexFile* GetDexFile() const {
475 return dex_file_;
476 }
477
478 Class* GetComponentType() const {
479 return component_type_;
480 }
481
482 const StringPiece& GetDescriptor() const {
483 return descriptor_;
484 }
485
486 Status GetStatus() const {
487 return status_;
488 }
489
490 void SetStatus(Status new_status) {
491 // TODO: validate transition
492 status_ = new_status;
493 }
494
495 bool IsErroneous() const {
496 return GetStatus() == kStatusError;
497 }
498
499 bool IsVerified() const {
500 return GetStatus() >= kStatusVerified;
501 }
502
503 bool IsLinked() const {
504 return GetStatus() >= kStatusResolved;
505 }
506
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700507 // Returns true if this class is in the same packages as that class.
508 bool IsInSamePackage(const Class* that) const;
509
510 static bool IsInSamePackage(const char* descriptor1, const char* descriptor2);
511
512 // Returns true if this class represents an array class.
513 bool IsArray() const {
514 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
515 }
516
517 // Returns true if the class is an interface.
518 bool IsInterface() const {
519 return (access_flags_ & kAccInterface) != 0;
520 }
521
522 // Returns true if the class is declared public.
523 bool IsPublic() const {
524 return (access_flags_ & kAccPublic) != 0;
525 }
526
527 // Returns true if the class is declared final.
528 bool IsFinal() const {
529 return (access_flags_ & kAccFinal) != 0;
530 }
531
532 // Returns true if the class is abstract.
533 bool IsAbstract() const {
534 return (access_flags_ & kAccAbstract) != 0;
535 }
536
537 // Returns true if the class is an annotation.
538 bool IsAnnotation() const {
539 return (access_flags_ & kAccAnnotation) != 0;
540 }
541
542 // Returns true if the class is a primitive type.
543 bool IsPrimitive() const {
544 return primitive_type_ != kPrimNot;
545 }
546
547 // Returns true if this class can access that class.
548 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700549 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700550 }
551
Carl Shapiro1fb86202011-06-27 17:43:13 -0700552 // Returns the size in bytes of a class object instance with the
553 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700554 // static size_t Size(size_t num_sfields) {
555 // return OFFSETOF_MEMBER(Class, sfields_) + sizeof(StaticField) * num_sfields;
556 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700557
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700558 // Returns the number of static, private, and constructor methods.
559 size_t NumDirectMethods() const {
560 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700561 }
562
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700564 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700565 }
566
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700567 // Returns the number of non-inherited virtual methods.
568 size_t NumVirtualMethods() const {
569 return num_virtual_methods_;
570 }
571
572 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700573 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574 }
575
576 size_t NumInstanceFields() const {
577 return num_ifields_;
578 }
579
580 size_t NumReferenceInstanceFields() const {
581 return num_reference_ifields_;
582 }
583
584 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700585 return ifields_[i];
586 }
587
588 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
589 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700590 }
591
592 size_t NumStaticFields() const {
593 return num_sfields_;
594 }
595
596 StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700597 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700598 }
599
600 uint32_t GetReferenceOffsets() const {
601 return reference_offsets_;
602 }
603
604 void SetReferenceOffsets(uint32_t new_reference_offsets) {
605 reference_offsets_ = new_reference_offsets;
606 }
607
608 Method* FindDirectMethodLocally(const StringPiece& name,
609 const StringPiece& descriptor) const;
610
Carl Shapiro1fb86202011-06-27 17:43:13 -0700611 public: // TODO: private
612 // leave space for instance data; we could access fields directly if
613 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700614#define CLASS_FIELD_SLOTS 1
615 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700616 uint32_t instance_data_[CLASS_FIELD_SLOTS];
617#undef CLASS_FIELD_SLOTS
618
619 // UTF-8 descriptor for the class from constant pool
620 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700621 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700622
623 // Proxy classes have their descriptor allocated on the native heap.
624 // When this field is non-NULL it must be explicitly freed.
625 char* descriptor_alloc_;
626
627 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700628 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700629
Carl Shapiro1fb86202011-06-27 17:43:13 -0700630 // DexFile from which we came; needed to resolve constant pool entries
631 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
632 DexFile* dex_file_;
633
634 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700635 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700636
637 // if class verify fails, we must return same error on subsequent tries
638 Class* verify_error_class_;
639
640 // threadId, used to check for recursive <clinit> invocation
641 uint32_t clinit_thread_id_;
642
643 // Total object size; used when allocating storage on gc heap. (For
644 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700645 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700646
647 // For array classes, the class object for base element, for
648 // instanceof/checkcast (for String[][][], this will be String).
649 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700650 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700651
652 // For array classes, the number of array dimensions, e.g. int[][]
653 // is 2. Otherwise 0.
654 int32_t array_rank_;
655
656 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
657 PrimitiveType primitive_type_;
658
659 // The superclass, or NULL if this is java.lang.Object or a
660 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700661 Class* super_class_; // TODO: make an instance field
662 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700663
664 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700665 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700666
667 // initiating class loader list
668 // NOTE: for classes with low serialNumber, these are unused, and the
669 // values are kept in a table in gDvm.
670 //InitiatingLoaderList initiating_loader_list_;
671
672 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700673 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700674 Class** interfaces_;
675
676 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700677 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700678 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700679
680 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700681 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700682 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700683
684 // Virtual method table (vtable), for use by "invoke-virtual". The
685 // vtable from the superclass is copied in, and virtual methods from
686 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700687 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700688 Method** vtable_;
689
690 // Interface table (iftable), one entry per interface supported by
691 // this class. That means one entry for each interface we support
692 // directly, indirectly via superclass, or indirectly via
693 // superinterface. This will be null if neither we nor our
694 // superclass implement any interfaces.
695 //
696 // Why we need this: given "class Foo implements Face", declare
697 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
698 // is part of the Face interface. We can't easily use a single
699 // vtable.
700 //
701 // For every interface a concrete class implements, we create a list
702 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700703 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700704 InterfaceEntry* iftable_;
705
706 // The interface vtable indices for iftable get stored here. By
707 // placing them all in a single pool for each class that implements
708 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709 size_t ifvi_pool_count_;
710 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700711
712 // instance fields
713 //
714 // These describe the layout of the contents of a
715 // DataObject-compatible Object. Note that only the fields directly
716 // declared by this class are listed in ifields; fields declared by
717 // a superclass are listed in the superclass's ClassObject.ifields.
718 //
719 // All instance fields that refer to objects are guaranteed to be at
720 // the beginning of the field list. ifieldRefCount specifies the
721 // number of reference fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 size_t num_ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700723
724 // number of fields that are object refs
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700725 size_t num_reference_ifields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700726 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700727
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728 // Bitmap of offsets of ifields.
729 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700730
731 // source file name, if known. Otherwise, NULL.
732 const char* source_file_;
733
734 // Static fields
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735 size_t num_sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700736 StaticField** sfields_;
737
738 private:
739 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700740};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700741std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700742
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700743class DataObject : public Object {
744 public:
745 uint32_t fields_[1];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700746 private:
747 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700748};
749
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700750class Array : public Object {
751 public:
752 void SetLength(uint32_t length) {
753 length_ = length;
754 }
755
756 private:
757 // The number of array elements.
758 uint32_t length_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700759 Array();
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700760};
761
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700762class CharArray : public Array {
763 private:
764 CharArray();
765};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700766
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700768 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700769 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700770
771 uint32_t hash_code_;
772
773 uint32_t offset_;
774
775 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700776
777 private:
778 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700779};
780
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700781class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700782 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700783 Class* GetClass() const {
784 return klass_;
785 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700786
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 void SetClass(Class* klass) {
788 klass_ = klass;
789 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700790
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791 private:
792 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700793 Class* klass_;
794
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700795 public: // TODO: private
796 // Index into array of vtable offsets. This points into the
797 // ifviPool, which holds the vtables for all interfaces declared by
798 // this class.
799 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700800};
801
802} // namespace art
803
804#endif // ART_SRC_OBJECT_H_