blob: 49940e0450ca5bf8d4017f5260f08ccf6f9bd3af [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Ian Rogersb033c752011-07-20 12:22:35 -07006#include "src/assembler.h"
7#include "src/constants.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07008#include "src/dex_file.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07009#include "src/globals.h"
Ian Rogersb033c752011-07-20 12:22:35 -070010#include "src/logging.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -070011#include "src/macros.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012#include "src/stringpiece.h"
13#include "src/monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070014
15namespace art {
16
17class Array;
18class Class;
19class DexFile;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070020class InstanceField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070021class InterfaceEntry;
22class Monitor;
23class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070024class Object;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070025class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070026
Carl Shapiro3ee755d2011-06-28 12:11:04 -070027union JValue {
28 uint8_t z;
29 int8_t b;
30 uint16_t c;
31 int16_t s;
32 int32_t i;
33 int64_t j;
34 float f;
35 double d;
36 Object* l;
37};
38
Brian Carlstrombe977852011-07-19 14:54:54 -070039static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
40static const uint32_t kAccPrivate = 0x0002; // field, method, ic
41static const uint32_t kAccProtected = 0x0004; // field, method, ic
42static const uint32_t kAccStatic = 0x0008; // field, method, ic
43static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
44static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
45static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
46static const uint32_t kAccVolatile = 0x0040; // field
47static const uint32_t kAccBridge = 0x0040; // method (1.5)
48static const uint32_t kAccTransient = 0x0080; // field
49static const uint32_t kAccVarargs = 0x0080; // method (1.5)
50static const uint32_t kAccNative = 0x0100; // method
51static const uint32_t kAccInterface = 0x0200; // class, ic
52static const uint32_t kAccAbstract = 0x0400; // class, method, ic
53static const uint32_t kAccStrict = 0x0800; // method
54static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
55static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
56static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070057
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070058static const uint32_t kAccMiranda = 0x8000; // method
59
Brian Carlstroma331b3c2011-07-18 17:47:56 -070060static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
61
Brian Carlstrombe977852011-07-19 14:54:54 -070062static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
63static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070064
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070066 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067 */
68/*
69 * A magic value for refOffsets. Ignore the bits and walk the super
70 * chain when this is the value.
71 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
72 * fields followed by 2 ref instance fields.]
73 */
74#define CLASS_WALK_SUPER ((unsigned int)(3))
75#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
76#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
77#define CLASS_OFFSET_ALIGNMENT 4
78#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
79/*
80 * Given an offset, return the bit number which would encode that offset.
81 * Local use only.
82 */
83#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
84 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
85 CLASS_OFFSET_ALIGNMENT)
86/*
87 * Is the given offset too large to be encoded?
88 */
89#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
90 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
91/*
92 * Return a single bit, encoding the offset.
93 * Undefined if the offset is too large, as defined above.
94 */
95#define CLASS_BIT_FROM_OFFSET(byteOffset) \
96 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
97/*
98 * Return an offset, given a bit number as returned from CLZ.
99 */
100#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700101 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700102
103
Carl Shapiro1fb86202011-06-27 17:43:13 -0700104class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700105 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700106 Class* GetClass() const {
107 return klass_;
108 }
109
110 void MonitorEnter() {
111 monitor_->Enter();
112 }
113
114 void MonitorExit() {
115 monitor_->Exit();
116 }
117
118 void Notify() {
119 monitor_->Notify();
120 }
121
122 void NotifyAll() {
123 monitor_->NotifyAll();
124 }
125
126 void Wait() {
127 monitor_->Wait();
128 }
129
130 void Wait(int64_t timeout) {
131 monitor_->Wait(timeout);
132 }
133
134 void Wait(int64_t timeout, int32_t nanos) {
135 monitor_->Wait(timeout, nanos);
136 }
137
138 void SetObjectAt(size_t offset, Object* new_value) {
139 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
140 *reinterpret_cast<Object**>(raw_addr) = new_value;
141 // TODO: write barrier
142 }
143
144 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700145 Class* klass_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700146 Monitor* monitor_;
147
148 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700149 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 DISALLOW_COPY_AND_ASSIGN(Object);
151};
152
153class ObjectLock {
154 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700155 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156 CHECK(object != NULL);
157 obj_->MonitorEnter();
158 }
159
160 ~ObjectLock() {
161 obj_->MonitorExit();
162 }
163
164 void Wait(int64_t millis = 0) {
165 return obj_->Wait(millis);
166 }
167
168 void Notify() {
169 obj_->Notify();
170 }
171
172 void NotifyAll() {
173 obj_->NotifyAll();
174 }
175
176 private:
177 Object* obj_;
178 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700179};
180
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700181class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700182 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700183 Class* GetDeclaringClass() const {
184 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185 }
186
187 const char* GetName() const {
188 return name_;
189 }
190
191 char GetType() const { // TODO: return type
192 return signature_[0];
193 }
194
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700195 const char* GetSignature() const {
196 return signature_;
197 }
198
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700199 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700200#define FIELD_FIELD_SLOTS 1+6
201 // AccessibleObject #0 flag
202 // Field #0 declaringClass
203 // Field #1 genericType
204 // Field #2 genericTypesAreInitialized
205 // Field #3 name
206 // Field #4 slot
207 // Field #5 type
208 uint32_t instance_data_[FIELD_FIELD_SLOTS];
209#undef FIELD_FIELD_SLOTS
210
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700211 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700212 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700213
214 const char* name_;
215
216 // e.g. "I", "[C", "Landroid/os/Debug;"
217 const char* signature_;
218
219 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700220
221 private:
222 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700223};
224
225// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700226class InstanceField : public Field {
227 public:
228 uint32_t GetOffset() const {
229 return offset_;
230 }
231
232 void SetOffset(size_t num_bytes) {
233 offset_ = num_bytes;
234 }
235
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700236 private:
237 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700238 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700239};
240
241// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700243 public:
244 void SetBoolean(bool z) {
245 CHECK_EQ(GetType(), 'Z');
246 value_.z = z;
247 }
248
249 void SetByte(int8_t b) {
250 CHECK_EQ(GetType(), 'B');
251 value_.b = b;
252 }
253
254 void SetChar(uint16_t c) {
255 CHECK_EQ(GetType(), 'C');
256 value_.c = c;
257 }
258
259 void SetShort(uint16_t s) {
260 CHECK_EQ(GetType(), 'S');
261 value_.s = s;
262 }
263
264 void SetInt(int32_t i) {
265 CHECK_EQ(GetType(), 'I');
266 value_.i = i;
267 }
268
269 int64_t GetLong() {
270 CHECK_EQ(GetType(), 'J');
271 return value_.j;
272 }
273
274 void SetLong(int64_t j) {
275 CHECK_EQ(GetType(), 'J');
276 value_.j = j;
277 }
278
279 void SetFloat(float f) {
280 CHECK_EQ(GetType(), 'F');
281 value_.f = f;
282 }
283
284 void SetDouble(double d) {
285 CHECK_EQ(GetType(), 'D');
286 value_.d = d;
287 }
288
289 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700290 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700291 value_.l = l;
292 // TODO: write barrier
293 }
294
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700295 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700296 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700297 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700298};
299
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700300class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301 public:
302 // Returns the method name.
303 // TODO: example
304 const StringPiece& GetName() const {
305 return name_;
306 }
307
Brian Carlstroma0808032011-07-18 00:39:23 -0700308 Class* GetDeclaringClass() const {
309 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700310 }
311
Ian Rogersb033c752011-07-20 12:22:35 -0700312 static MemberOffset ClassOffset() {
313 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
314 }
315
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700316 // const char* GetReturnTypeDescriptor() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700317 // return declaring_class_->GetDexFile_->GetRaw()
318 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700319 // }
320
321 // Returns true if the method is declared public.
322 bool IsPublic() const {
323 return (access_flags_ & kAccPublic) != 0;
324 }
325
326 // Returns true if the method is declared private.
327 bool IsPrivate() const {
328 return (access_flags_ & kAccPrivate) != 0;
329 }
330
331 // Returns true if the method is declared static.
332 bool IsStatic() const {
333 return (access_flags_ & kAccStatic) != 0;
334 }
335
336 // Returns true if the method is declared synchronized.
337 bool IsSynchronized() const {
338 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
339 return (access_flags_ & synchonized) != 0;
340 }
341
342 // Returns true if the method is declared final.
343 bool IsFinal() const {
344 return (access_flags_ & kAccFinal) != 0;
345 }
346
347 // Returns true if the method is declared native.
348 bool IsNative() const {
349 return (access_flags_ & kAccNative) != 0;
350 }
351
352 // Returns true if the method is declared abstract.
353 bool IsAbstract() const {
354 return (access_flags_ & kAccAbstract) != 0;
355 }
356
357 bool IsSynthetic() const {
358 return (access_flags_ & kAccSynthetic) != 0;
359 }
360
361 // Number of argument registers required by the prototype.
362 uint32_t NumArgRegisters();
363
364 bool HasSameNameAndPrototype(const Method* that) const {
365 return HasSameName(that) && HasSamePrototype(that);
366 }
367
368 bool HasSameName(const Method* that) const {
369 return this->GetName() == that->GetName();
370 }
371
372 bool HasSamePrototype(const Method* that) const {
373 return HasSameReturnType(that) && HasSameArgumentTypes(that);
374 }
375
376 bool HasSameReturnType(const Method* that) const;
377
378 bool HasSameArgumentTypes(const Method* that) const;
379
380 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700381#define METHOD_FIELD_SLOTS 1+11
382 // AccessibleObject #0 flag
383 // Method #0 declaringClass
384 // Method #1 exceptionTypes
385 // Method #2 formalTypeParameters
386 // Method #3 genericExceptionTypes
387 // Method #4 genericParameterTypes
388 // Method #5 genericReturnType
389 // Method #6 genericTypesAreInitialized
390 // Method #7 name
391 // Method #8 parameterTypes
392 // Method #9 returnType
393 // Method #10 slot
394 uint32_t instance_data_[METHOD_FIELD_SLOTS];
395#undef METHOD_FIELD_SLOTS
396
Ian Rogersb033c752011-07-20 12:22:35 -0700397 bool IsReturnAReference() const {
398 return (shorty_[0] == 'L') || (shorty_[0] == '[');
399 }
400
401 bool IsReturnAFloatOrDouble() const {
402 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
403 }
404
405 bool IsReturnAFloat() const {
406 return shorty_[0] == 'F';
407 }
408
409 bool IsReturnADouble() const {
410 return shorty_[0] == 'D';
411 }
412
413 bool IsReturnALong() const {
414 return shorty_[0] == 'J';
415 }
416
417 // The number of arguments that should be supplied to this method
418 size_t NumArgs() const {
419 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
420 }
421
422 // The number of reference arguments to this method including implicit this
423 // pointer
424 size_t NumReferenceArgs() const;
425
426 // The number of long or double arguments
427 size_t NumLongOrDoubleArgs() const;
428
429 // The number of reference arguments to this method before the given
430 // parameter index
431 size_t NumReferenceArgsBefore(unsigned int param) const;
432
433 // Is the given method parameter a reference?
434 bool IsParamAReference(unsigned int param) const;
435
436 // Is the given method parameter a long or double?
437 bool IsParamALongOrDouble(unsigned int param) const;
438
Ian Rogersdf20fe02011-07-20 20:34:16 -0700439 // Size in bytes of the given parameter
440 size_t ParamSize(unsigned int param) const;
441
442 // Size in bytes of the return value
443 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700444
445 void SetCode(const void* code) {
446 code_ = code;
447 }
448
449 const void* GetCode() const {
450 return code_;
451 }
452
453 void RegisterNative(const void* native_method) {
454 native_method_ = native_method;
455 }
456
457 static MemberOffset NativeMethodOffset() {
458 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
459 }
460
461 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700462 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700463 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700464
465 // access flags; low 16 bits are defined by spec (could be uint16_t?)
466 uint32_t access_flags_;
467
468 // For concrete virtual methods, this is the offset of the method
469 // in "vtable".
470 //
471 // For abstract methods in an interface class, this is the offset
472 // of the method in "iftable[n]->methodIndexArray".
473 uint16_t method_index_;
474
475 // Method bounds; not needed for an abstract method.
476 //
477 // For a native method, we compute the size of the argument list, and
478 // set "insSize" and "registerSize" equal to it.
479 uint16_t num_registers_; // ins + locals
480 uint16_t num_outs_;
481 uint16_t num_ins_;
482
483 // method name, e.g. "<init>" or "eatLunch"
484 StringPiece name_;
485
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700486 // Method prototype descriptor string (return and argument types).
487 uint32_t proto_idx_;
488
489 // The short-form method descriptor string.
490 StringPiece shorty_;
491
492 // A pointer to the memory-mapped DEX code.
493 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700494
495 private:
496 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700497
498 // Compiled code associated with this method
499 const void* code_;
500
501 // Any native method registered with this method
502 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700503};
504
Carl Shapiro1fb86202011-06-27 17:43:13 -0700505// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700506class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700507 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700508 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700509 kStatusError = -1,
510 kStatusNotReady = 0,
511 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
512 kStatusLoaded = 2, // DEX idx values resolved
513 kStatusResolved = 3, // part of linking
514 kStatusVerifying = 4, // in the process of being verified
515 kStatusVerified = 5, // logically part of linking; done pre-init
516 kStatusInitializing = 6, // class init in progress
517 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700518 };
519
520 enum PrimitiveType {
521 kPrimNot = -1
522 };
523
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700524 Class* GetSuperClass() const {
525 return super_class_;
526 }
527
528 uint32_t GetSuperClassIdx() const {
529 return super_class_idx_;
530 }
531
532 bool HasSuperClass() const {
533 return super_class_ != NULL;
534 }
535
536 Object* GetClassLoader() const {
537 return class_loader_;
538 }
539
540 DexFile* GetDexFile() const {
541 return dex_file_;
542 }
543
544 Class* GetComponentType() const {
545 return component_type_;
546 }
547
548 const StringPiece& GetDescriptor() const {
549 return descriptor_;
550 }
551
552 Status GetStatus() const {
553 return status_;
554 }
555
556 void SetStatus(Status new_status) {
557 // TODO: validate transition
558 status_ = new_status;
559 }
560
561 bool IsErroneous() const {
562 return GetStatus() == kStatusError;
563 }
564
565 bool IsVerified() const {
566 return GetStatus() >= kStatusVerified;
567 }
568
569 bool IsLinked() const {
570 return GetStatus() >= kStatusResolved;
571 }
572
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700573 // Returns true if this class is in the same packages as that class.
574 bool IsInSamePackage(const Class* that) const;
575
Ian Rogersb033c752011-07-20 12:22:35 -0700576 static bool IsInSamePackage(const StringPiece& descriptor1,
577 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700578
579 // Returns true if this class represents an array class.
580 bool IsArray() const {
581 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
582 }
583
584 // Returns true if the class is an interface.
585 bool IsInterface() const {
586 return (access_flags_ & kAccInterface) != 0;
587 }
588
589 // Returns true if the class is declared public.
590 bool IsPublic() const {
591 return (access_flags_ & kAccPublic) != 0;
592 }
593
594 // Returns true if the class is declared final.
595 bool IsFinal() const {
596 return (access_flags_ & kAccFinal) != 0;
597 }
598
599 // Returns true if the class is abstract.
600 bool IsAbstract() const {
601 return (access_flags_ & kAccAbstract) != 0;
602 }
603
604 // Returns true if the class is an annotation.
605 bool IsAnnotation() const {
606 return (access_flags_ & kAccAnnotation) != 0;
607 }
608
609 // Returns true if the class is a primitive type.
610 bool IsPrimitive() const {
611 return primitive_type_ != kPrimNot;
612 }
613
614 // Returns true if this class can access that class.
615 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700616 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700617 }
618
Carl Shapiro1fb86202011-06-27 17:43:13 -0700619 // Returns the size in bytes of a class object instance with the
620 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700621 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700622 // return OFFSETOF_MEMBER(Class, sfields_) +
623 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700624 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700625
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700626 // Returns the number of static, private, and constructor methods.
627 size_t NumDirectMethods() const {
628 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700629 }
630
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700631 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700632 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700633 }
634
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700635 // Returns the number of non-inherited virtual methods.
636 size_t NumVirtualMethods() const {
637 return num_virtual_methods_;
638 }
639
640 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700641 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700642 }
643
644 size_t NumInstanceFields() const {
645 return num_ifields_;
646 }
647
648 size_t NumReferenceInstanceFields() const {
649 return num_reference_ifields_;
650 }
651
652 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700653 return ifields_[i];
654 }
655
656 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
657 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700658 }
659
660 size_t NumStaticFields() const {
661 return num_sfields_;
662 }
663
664 StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700665 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700666 }
667
668 uint32_t GetReferenceOffsets() const {
669 return reference_offsets_;
670 }
671
672 void SetReferenceOffsets(uint32_t new_reference_offsets) {
673 reference_offsets_ = new_reference_offsets;
674 }
675
Ian Rogersb033c752011-07-20 12:22:35 -0700676 Method* FindDirectMethod(const StringPiece& name) const;
677
678 Method* FindVirtualMethod(const StringPiece& name) const;
679
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700680 Method* FindDirectMethodLocally(const StringPiece& name,
681 const StringPiece& descriptor) const;
682
Ian Rogersb033c752011-07-20 12:22:35 -0700683 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700684 // leave space for instance data; we could access fields directly if
685 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700686#define CLASS_FIELD_SLOTS 1
687 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700688 uint32_t instance_data_[CLASS_FIELD_SLOTS];
689#undef CLASS_FIELD_SLOTS
690
691 // UTF-8 descriptor for the class from constant pool
692 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700693 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700694
695 // Proxy classes have their descriptor allocated on the native heap.
696 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700697 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700698
699 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700700 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700701
Carl Shapiro1fb86202011-06-27 17:43:13 -0700702 // DexFile from which we came; needed to resolve constant pool entries
703 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
704 DexFile* dex_file_;
705
706 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700707 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700708
709 // if class verify fails, we must return same error on subsequent tries
710 Class* verify_error_class_;
711
712 // threadId, used to check for recursive <clinit> invocation
713 uint32_t clinit_thread_id_;
714
715 // Total object size; used when allocating storage on gc heap. (For
716 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700717 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700718
719 // For array classes, the class object for base element, for
720 // instanceof/checkcast (for String[][][], this will be String).
721 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700722 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700723
724 // For array classes, the number of array dimensions, e.g. int[][]
725 // is 2. Otherwise 0.
726 int32_t array_rank_;
727
728 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
729 PrimitiveType primitive_type_;
730
731 // The superclass, or NULL if this is java.lang.Object or a
732 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700733 Class* super_class_; // TODO: make an instance field
734 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700735
736 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700737 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700738
739 // initiating class loader list
740 // NOTE: for classes with low serialNumber, these are unused, and the
741 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700742 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700743
744 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700746 Class** interfaces_;
747
748 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700749 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700750 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700751
752 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700753 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700754 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700755
756 // Virtual method table (vtable), for use by "invoke-virtual". The
757 // vtable from the superclass is copied in, and virtual methods from
758 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700759 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700760 Method** vtable_;
761
762 // Interface table (iftable), one entry per interface supported by
763 // this class. That means one entry for each interface we support
764 // directly, indirectly via superclass, or indirectly via
765 // superinterface. This will be null if neither we nor our
766 // superclass implement any interfaces.
767 //
768 // Why we need this: given "class Foo implements Face", declare
769 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
770 // is part of the Face interface. We can't easily use a single
771 // vtable.
772 //
773 // For every interface a concrete class implements, we create a list
774 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700775 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700776 InterfaceEntry* iftable_;
777
778 // The interface vtable indices for iftable get stored here. By
779 // placing them all in a single pool for each class that implements
780 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700781 size_t ifvi_pool_count_;
782 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700783
784 // instance fields
785 //
786 // These describe the layout of the contents of a
787 // DataObject-compatible Object. Note that only the fields directly
788 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700789 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700790 //
791 // All instance fields that refer to objects are guaranteed to be at
792 // the beginning of the field list. ifieldRefCount specifies the
793 // number of reference fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700794 size_t num_ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700795
796 // number of fields that are object refs
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 size_t num_reference_ifields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700798 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700799
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700800 // Bitmap of offsets of ifields.
801 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700802
803 // source file name, if known. Otherwise, NULL.
804 const char* source_file_;
805
806 // Static fields
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700807 size_t num_sfields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700808 StaticField** sfields_;
809
810 private:
811 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700812};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700813std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700814
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700815class DataObject : public Object {
816 public:
817 uint32_t fields_[1];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700818 private:
819 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700820};
821
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700822class Array : public Object {
823 public:
824 void SetLength(uint32_t length) {
825 length_ = length;
826 }
827
828 private:
829 // The number of array elements.
830 uint32_t length_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700831 Array();
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700832};
833
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700834class CharArray : public Array {
835 private:
836 CharArray();
837};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700838
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700839class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700840 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700841 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700842
843 uint32_t hash_code_;
844
845 uint32_t offset_;
846
847 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700848
849 private:
850 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700851};
852
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700853class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700854 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 Class* GetClass() const {
856 return klass_;
857 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700858
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700859 void SetClass(Class* klass) {
860 klass_ = klass;
861 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700862
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700863 private:
864 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700865 Class* klass_;
866
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700867 public: // TODO: private
868 // Index into array of vtable offsets. This points into the
869 // ifviPool, which holds the vtables for all interfaces declared by
870 // this class.
871 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700872};
873
874} // namespace art
875
876#endif // ART_SRC_OBJECT_H_