blob: 218aed94e164bccab87607f10c5270b616ed8416 [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/constants.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07007#include "src/casts.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 Shapiro69759ea2011-07-21 18:13:35 -070012#include "src/offsets.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070013#include "src/stringpiece.h"
14#include "src/monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070015
16namespace art {
17
18class Array;
19class Class;
20class DexFile;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070021class InstanceField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class InterfaceEntry;
23class Monitor;
24class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070026class ObjectArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070027class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070028
Carl Shapiro3ee755d2011-06-28 12:11:04 -070029union JValue {
30 uint8_t z;
31 int8_t b;
32 uint16_t c;
33 int16_t s;
34 int32_t i;
35 int64_t j;
36 float f;
37 double d;
38 Object* l;
39};
40
Brian Carlstrombe977852011-07-19 14:54:54 -070041static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
42static const uint32_t kAccPrivate = 0x0002; // field, method, ic
43static const uint32_t kAccProtected = 0x0004; // field, method, ic
44static const uint32_t kAccStatic = 0x0008; // field, method, ic
45static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
46static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
47static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
48static const uint32_t kAccVolatile = 0x0040; // field
49static const uint32_t kAccBridge = 0x0040; // method (1.5)
50static const uint32_t kAccTransient = 0x0080; // field
51static const uint32_t kAccVarargs = 0x0080; // method (1.5)
52static const uint32_t kAccNative = 0x0100; // method
53static const uint32_t kAccInterface = 0x0200; // class, ic
54static const uint32_t kAccAbstract = 0x0400; // class, method, ic
55static const uint32_t kAccStrict = 0x0800; // method
56static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
57static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
58static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070059
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060static const uint32_t kAccMiranda = 0x8000; // method
61
Brian Carlstroma331b3c2011-07-18 17:47:56 -070062static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
63
Brian Carlstrombe977852011-07-19 14:54:54 -070064static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
65static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069 */
70/*
71 * A magic value for refOffsets. Ignore the bits and walk the super
72 * chain when this is the value.
73 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
74 * fields followed by 2 ref instance fields.]
75 */
76#define CLASS_WALK_SUPER ((unsigned int)(3))
77#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
78#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
79#define CLASS_OFFSET_ALIGNMENT 4
80#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
81/*
82 * Given an offset, return the bit number which would encode that offset.
83 * Local use only.
84 */
85#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
86 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
87 CLASS_OFFSET_ALIGNMENT)
88/*
89 * Is the given offset too large to be encoded?
90 */
91#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
92 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
93/*
94 * Return a single bit, encoding the offset.
95 * Undefined if the offset is too large, as defined above.
96 */
97#define CLASS_BIT_FROM_OFFSET(byteOffset) \
98 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
99/*
100 * Return an offset, given a bit number as returned from CLZ.
101 */
102#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700103 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700104
105
Carl Shapiro1fb86202011-06-27 17:43:13 -0700106class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700107 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700108 Class* GetClass() const {
109 return klass_;
110 }
111
112 void MonitorEnter() {
113 monitor_->Enter();
114 }
115
116 void MonitorExit() {
117 monitor_->Exit();
118 }
119
120 void Notify() {
121 monitor_->Notify();
122 }
123
124 void NotifyAll() {
125 monitor_->NotifyAll();
126 }
127
128 void Wait() {
129 monitor_->Wait();
130 }
131
132 void Wait(int64_t timeout) {
133 monitor_->Wait(timeout);
134 }
135
136 void Wait(int64_t timeout, int32_t nanos) {
137 monitor_->Wait(timeout, nanos);
138 }
139
Carl Shapiro69759ea2011-07-21 18:13:35 -0700140 const Object* GetFieldObject(size_t field_offset) const {
141 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
142 return *reinterpret_cast<Object* const*>(raw_addr);
143 }
144
145 Object* GetFieldObject(size_t field_offset) {
146 return const_cast<Object*>(GetFieldObject(field_offset));
147 }
148
149 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700150 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
151 *reinterpret_cast<Object**>(raw_addr) = new_value;
152 // TODO: write barrier
153 }
154
Carl Shapiro69759ea2011-07-21 18:13:35 -0700155 bool IsClass() const {
156 LOG(FATAL) << "Unimplemented";
157 return true;
158 }
159
160 Class* AsClass() {
161 return down_cast<Class*>(this);
162 }
163
164 const Class* AsClass() const {
165 return down_cast<const Class*>(this);
166 }
167
168 bool IsObjectArray() const {
169 LOG(FATAL) << "Unimplemented";
170 return true;
171 }
172
173 const ObjectArray* AsObjectArray() const {
174 return down_cast<const ObjectArray*>(this);
175 }
176
177 bool IsReference() const {
178 LOG(FATAL) << "Unimplemented";
179 return true;
180 }
181
182 bool IsWeakReference() const {
183 LOG(FATAL) << "Unimplemented";
184 return true;
185 }
186
187 bool IsSoftReference() const {
188 LOG(FATAL) << "Unimplemented";
189 return true;
190 }
191
192 bool IsFinalizerReference() const {
193 LOG(FATAL) << "Unimplemented";
194 return true;
195 }
196
197 bool IsPhantomReference() const {
198 LOG(FATAL) << "Unimplemented";
199 return true;
200 }
201
202 bool IsArray() const {
203 LOG(FATAL) << "Unimplemented";
204 return true;
205 }
206
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700207 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700208 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700209
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700210 Monitor* monitor_;
211
212 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700213 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700214 DISALLOW_COPY_AND_ASSIGN(Object);
215};
216
217class ObjectLock {
218 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700219 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700220 CHECK(object != NULL);
221 obj_->MonitorEnter();
222 }
223
224 ~ObjectLock() {
225 obj_->MonitorExit();
226 }
227
228 void Wait(int64_t millis = 0) {
229 return obj_->Wait(millis);
230 }
231
232 void Notify() {
233 obj_->Notify();
234 }
235
236 void NotifyAll() {
237 obj_->NotifyAll();
238 }
239
240 private:
241 Object* obj_;
242 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700243};
244
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700245class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700246 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700247 Class* GetDeclaringClass() const {
248 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 }
250
251 const char* GetName() const {
252 return name_;
253 }
254
255 char GetType() const { // TODO: return type
256 return signature_[0];
257 }
258
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700259 const char* GetSignature() const {
260 return signature_;
261 }
262
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700263 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700264#define FIELD_FIELD_SLOTS 1+6
265 // AccessibleObject #0 flag
266 // Field #0 declaringClass
267 // Field #1 genericType
268 // Field #2 genericTypesAreInitialized
269 // Field #3 name
270 // Field #4 slot
271 // Field #5 type
272 uint32_t instance_data_[FIELD_FIELD_SLOTS];
273#undef FIELD_FIELD_SLOTS
274
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700275 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700276 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700277
278 const char* name_;
279
280 // e.g. "I", "[C", "Landroid/os/Debug;"
281 const char* signature_;
282
283 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700284
285 private:
286 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700287};
288
289// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700290class InstanceField : public Field {
291 public:
292 uint32_t GetOffset() const {
293 return offset_;
294 }
295
296 void SetOffset(size_t num_bytes) {
297 offset_ = num_bytes;
298 }
299
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700300 private:
301 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700302 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700303};
304
305// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700306class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700307 public:
308 void SetBoolean(bool z) {
309 CHECK_EQ(GetType(), 'Z');
310 value_.z = z;
311 }
312
313 void SetByte(int8_t b) {
314 CHECK_EQ(GetType(), 'B');
315 value_.b = b;
316 }
317
318 void SetChar(uint16_t c) {
319 CHECK_EQ(GetType(), 'C');
320 value_.c = c;
321 }
322
323 void SetShort(uint16_t s) {
324 CHECK_EQ(GetType(), 'S');
325 value_.s = s;
326 }
327
328 void SetInt(int32_t i) {
329 CHECK_EQ(GetType(), 'I');
330 value_.i = i;
331 }
332
333 int64_t GetLong() {
334 CHECK_EQ(GetType(), 'J');
335 return value_.j;
336 }
337
338 void SetLong(int64_t j) {
339 CHECK_EQ(GetType(), 'J');
340 value_.j = j;
341 }
342
343 void SetFloat(float f) {
344 CHECK_EQ(GetType(), 'F');
345 value_.f = f;
346 }
347
348 void SetDouble(double d) {
349 CHECK_EQ(GetType(), 'D');
350 value_.d = d;
351 }
352
Carl Shapiro69759ea2011-07-21 18:13:35 -0700353 Object* GetObject() {
354 return value_.l;
355 }
356
357 const Object* GetObject() const {
358 return value_.l;
359 }
360
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700361 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700362 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700363 value_.l = l;
364 // TODO: write barrier
365 }
366
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700367 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700368 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700369 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700370};
371
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700372class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700373 public:
374 // Returns the method name.
375 // TODO: example
376 const StringPiece& GetName() const {
377 return name_;
378 }
379
Brian Carlstroma0808032011-07-18 00:39:23 -0700380 Class* GetDeclaringClass() const {
381 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382 }
383
Ian Rogersb033c752011-07-20 12:22:35 -0700384 static MemberOffset ClassOffset() {
385 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
386 }
387
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700388 // const char* GetReturnTypeDescriptor() const {
Ian Rogersb033c752011-07-20 12:22:35 -0700389 // return declaring_class_->GetDexFile_->GetRaw()
390 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 // }
392
393 // Returns true if the method is declared public.
394 bool IsPublic() const {
395 return (access_flags_ & kAccPublic) != 0;
396 }
397
398 // Returns true if the method is declared private.
399 bool IsPrivate() const {
400 return (access_flags_ & kAccPrivate) != 0;
401 }
402
403 // Returns true if the method is declared static.
404 bool IsStatic() const {
405 return (access_flags_ & kAccStatic) != 0;
406 }
407
408 // Returns true if the method is declared synchronized.
409 bool IsSynchronized() const {
410 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
411 return (access_flags_ & synchonized) != 0;
412 }
413
414 // Returns true if the method is declared final.
415 bool IsFinal() const {
416 return (access_flags_ & kAccFinal) != 0;
417 }
418
419 // Returns true if the method is declared native.
420 bool IsNative() const {
421 return (access_flags_ & kAccNative) != 0;
422 }
423
424 // Returns true if the method is declared abstract.
425 bool IsAbstract() const {
426 return (access_flags_ & kAccAbstract) != 0;
427 }
428
429 bool IsSynthetic() const {
430 return (access_flags_ & kAccSynthetic) != 0;
431 }
432
433 // Number of argument registers required by the prototype.
434 uint32_t NumArgRegisters();
435
436 bool HasSameNameAndPrototype(const Method* that) const {
437 return HasSameName(that) && HasSamePrototype(that);
438 }
439
440 bool HasSameName(const Method* that) const {
441 return this->GetName() == that->GetName();
442 }
443
444 bool HasSamePrototype(const Method* that) const {
445 return HasSameReturnType(that) && HasSameArgumentTypes(that);
446 }
447
448 bool HasSameReturnType(const Method* that) const;
449
450 bool HasSameArgumentTypes(const Method* that) const;
451
452 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700453#define METHOD_FIELD_SLOTS 1+11
454 // AccessibleObject #0 flag
455 // Method #0 declaringClass
456 // Method #1 exceptionTypes
457 // Method #2 formalTypeParameters
458 // Method #3 genericExceptionTypes
459 // Method #4 genericParameterTypes
460 // Method #5 genericReturnType
461 // Method #6 genericTypesAreInitialized
462 // Method #7 name
463 // Method #8 parameterTypes
464 // Method #9 returnType
465 // Method #10 slot
466 uint32_t instance_data_[METHOD_FIELD_SLOTS];
467#undef METHOD_FIELD_SLOTS
468
Ian Rogersb033c752011-07-20 12:22:35 -0700469 bool IsReturnAReference() const {
470 return (shorty_[0] == 'L') || (shorty_[0] == '[');
471 }
472
473 bool IsReturnAFloatOrDouble() const {
474 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
475 }
476
477 bool IsReturnAFloat() const {
478 return shorty_[0] == 'F';
479 }
480
481 bool IsReturnADouble() const {
482 return shorty_[0] == 'D';
483 }
484
485 bool IsReturnALong() const {
486 return shorty_[0] == 'J';
487 }
488
Ian Rogers45a76cb2011-07-21 22:00:15 -0700489 bool IsReturnVoid() const {
490 return shorty_[0] == 'V';
491 }
492
Ian Rogersb033c752011-07-20 12:22:35 -0700493 // The number of arguments that should be supplied to this method
494 size_t NumArgs() const {
495 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
496 }
497
498 // The number of reference arguments to this method including implicit this
499 // pointer
500 size_t NumReferenceArgs() const;
501
502 // The number of long or double arguments
503 size_t NumLongOrDoubleArgs() const;
504
505 // The number of reference arguments to this method before the given
506 // parameter index
507 size_t NumReferenceArgsBefore(unsigned int param) const;
508
509 // Is the given method parameter a reference?
510 bool IsParamAReference(unsigned int param) const;
511
512 // Is the given method parameter a long or double?
513 bool IsParamALongOrDouble(unsigned int param) const;
514
Ian Rogersdf20fe02011-07-20 20:34:16 -0700515 // Size in bytes of the given parameter
516 size_t ParamSize(unsigned int param) const;
517
518 // Size in bytes of the return value
519 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700520
521 void SetCode(const void* code) {
522 code_ = code;
523 }
524
525 const void* GetCode() const {
526 return code_;
527 }
528
529 void RegisterNative(const void* native_method) {
530 native_method_ = native_method;
531 }
532
533 static MemberOffset NativeMethodOffset() {
534 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
535 }
536
537 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700538 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700539 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700540
541 // access flags; low 16 bits are defined by spec (could be uint16_t?)
542 uint32_t access_flags_;
543
544 // For concrete virtual methods, this is the offset of the method
545 // in "vtable".
546 //
547 // For abstract methods in an interface class, this is the offset
548 // of the method in "iftable[n]->methodIndexArray".
549 uint16_t method_index_;
550
551 // Method bounds; not needed for an abstract method.
552 //
553 // For a native method, we compute the size of the argument list, and
554 // set "insSize" and "registerSize" equal to it.
555 uint16_t num_registers_; // ins + locals
556 uint16_t num_outs_;
557 uint16_t num_ins_;
558
559 // method name, e.g. "<init>" or "eatLunch"
560 StringPiece name_;
561
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562 // Method prototype descriptor string (return and argument types).
563 uint32_t proto_idx_;
564
565 // The short-form method descriptor string.
566 StringPiece shorty_;
567
568 // A pointer to the memory-mapped DEX code.
569 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700570
571 private:
572 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700573
574 // Compiled code associated with this method
575 const void* code_;
576
577 // Any native method registered with this method
578 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579};
580
Carl Shapiro1fb86202011-06-27 17:43:13 -0700581// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700582class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700583 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700584 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700585 kStatusError = -1,
586 kStatusNotReady = 0,
587 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
588 kStatusLoaded = 2, // DEX idx values resolved
589 kStatusResolved = 3, // part of linking
590 kStatusVerifying = 4, // in the process of being verified
591 kStatusVerified = 5, // logically part of linking; done pre-init
592 kStatusInitializing = 6, // class init in progress
593 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700594 };
595
596 enum PrimitiveType {
597 kPrimNot = -1
598 };
599
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700600 Class* GetSuperClass() const {
601 return super_class_;
602 }
603
604 uint32_t GetSuperClassIdx() const {
605 return super_class_idx_;
606 }
607
608 bool HasSuperClass() const {
609 return super_class_ != NULL;
610 }
611
612 Object* GetClassLoader() const {
613 return class_loader_;
614 }
615
616 DexFile* GetDexFile() const {
617 return dex_file_;
618 }
619
620 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700621 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 return component_type_;
623 }
624
625 const StringPiece& GetDescriptor() const {
626 return descriptor_;
627 }
628
629 Status GetStatus() const {
630 return status_;
631 }
632
633 void SetStatus(Status new_status) {
634 // TODO: validate transition
635 status_ = new_status;
636 }
637
Carl Shapiro69759ea2011-07-21 18:13:35 -0700638 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700639 bool IsErroneous() const {
640 return GetStatus() == kStatusError;
641 }
642
Carl Shapiro69759ea2011-07-21 18:13:35 -0700643 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700644 bool IsVerified() const {
645 return GetStatus() >= kStatusVerified;
646 }
647
Carl Shapiro69759ea2011-07-21 18:13:35 -0700648 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700649 bool IsLinked() const {
650 return GetStatus() >= kStatusResolved;
651 }
652
Carl Shapiro69759ea2011-07-21 18:13:35 -0700653 bool IsLoaded() const {
654 return GetStatus() >= kStatusLoaded;
655 }
656
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700657 // Returns true if this class is in the same packages as that class.
658 bool IsInSamePackage(const Class* that) const;
659
Ian Rogersb033c752011-07-20 12:22:35 -0700660 static bool IsInSamePackage(const StringPiece& descriptor1,
661 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700662
663 // Returns true if this class represents an array class.
664 bool IsArray() const {
665 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
666 }
667
668 // Returns true if the class is an interface.
669 bool IsInterface() const {
670 return (access_flags_ & kAccInterface) != 0;
671 }
672
673 // Returns true if the class is declared public.
674 bool IsPublic() const {
675 return (access_flags_ & kAccPublic) != 0;
676 }
677
678 // Returns true if the class is declared final.
679 bool IsFinal() const {
680 return (access_flags_ & kAccFinal) != 0;
681 }
682
683 // Returns true if the class is abstract.
684 bool IsAbstract() const {
685 return (access_flags_ & kAccAbstract) != 0;
686 }
687
688 // Returns true if the class is an annotation.
689 bool IsAnnotation() const {
690 return (access_flags_ & kAccAnnotation) != 0;
691 }
692
693 // Returns true if the class is a primitive type.
694 bool IsPrimitive() const {
695 return primitive_type_ != kPrimNot;
696 }
697
698 // Returns true if this class can access that class.
699 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700700 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700701 }
702
Carl Shapiro1fb86202011-06-27 17:43:13 -0700703 // Returns the size in bytes of a class object instance with the
704 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700705 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700706 // return OFFSETOF_MEMBER(Class, sfields_) +
707 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700708 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700709
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700710 // Returns the number of static, private, and constructor methods.
711 size_t NumDirectMethods() const {
712 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700713 }
714
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700715 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700716 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700717 }
718
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700719 // Returns the number of non-inherited virtual methods.
720 size_t NumVirtualMethods() const {
721 return num_virtual_methods_;
722 }
723
724 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700725 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700726 }
727
728 size_t NumInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700729 return num_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700730 }
731
Carl Shapiro69759ea2011-07-21 18:13:35 -0700732 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700733 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700734 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700735 }
736
737 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700738 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700739 return ifields_[i];
740 }
741
742 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700743 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700744 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700745 }
746
747 size_t NumStaticFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700748 return num_static_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700749 }
750
Carl Shapiro69759ea2011-07-21 18:13:35 -0700751 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
752 DCHECK_LT(i, num_static_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700753 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700754 }
755
756 uint32_t GetReferenceOffsets() const {
757 return reference_offsets_;
758 }
759
760 void SetReferenceOffsets(uint32_t new_reference_offsets) {
761 reference_offsets_ = new_reference_offsets;
762 }
763
Ian Rogersb033c752011-07-20 12:22:35 -0700764 Method* FindDirectMethod(const StringPiece& name) const;
765
766 Method* FindVirtualMethod(const StringPiece& name) const;
767
Carl Shapiro69759ea2011-07-21 18:13:35 -0700768 size_t NumInterfaces() const {
769 return interface_count_;
770 }
771
772 Class* GetInterface(uint32_t i) const {
773 DCHECK_LT(i, interface_count_);
774 return interfaces_[i];
775 }
776
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700777 Method* FindDirectMethodLocally(const StringPiece& name,
778 const StringPiece& descriptor) const;
779
Ian Rogersb033c752011-07-20 12:22:35 -0700780 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700781 // leave space for instance data; we could access fields directly if
782 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700783#define CLASS_FIELD_SLOTS 1
784 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700785 uint32_t instance_data_[CLASS_FIELD_SLOTS];
786#undef CLASS_FIELD_SLOTS
787
788 // UTF-8 descriptor for the class from constant pool
789 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700790 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700791
792 // Proxy classes have their descriptor allocated on the native heap.
793 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700794 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700795
796 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700797 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700798
Carl Shapiro1fb86202011-06-27 17:43:13 -0700799 // DexFile from which we came; needed to resolve constant pool entries
800 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
801 DexFile* dex_file_;
802
803 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700804 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700805
806 // if class verify fails, we must return same error on subsequent tries
807 Class* verify_error_class_;
808
809 // threadId, used to check for recursive <clinit> invocation
810 uint32_t clinit_thread_id_;
811
812 // Total object size; used when allocating storage on gc heap. (For
813 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700814 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700815
816 // For array classes, the class object for base element, for
817 // instanceof/checkcast (for String[][][], this will be String).
818 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700819 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700820
821 // For array classes, the number of array dimensions, e.g. int[][]
822 // is 2. Otherwise 0.
823 int32_t array_rank_;
824
825 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
826 PrimitiveType primitive_type_;
827
828 // The superclass, or NULL if this is java.lang.Object or a
829 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700830 Class* super_class_; // TODO: make an instance field
831 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700832
833 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700834 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700835
836 // initiating class loader list
837 // NOTE: for classes with low serialNumber, these are unused, and the
838 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700839 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700840
841 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700842 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700843 Class** interfaces_;
844
845 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700846 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700847 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700848
849 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700850 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700851 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700852
853 // Virtual method table (vtable), for use by "invoke-virtual". The
854 // vtable from the superclass is copied in, and virtual methods from
855 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700856 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700857 Method** vtable_;
858
859 // Interface table (iftable), one entry per interface supported by
860 // this class. That means one entry for each interface we support
861 // directly, indirectly via superclass, or indirectly via
862 // superinterface. This will be null if neither we nor our
863 // superclass implement any interfaces.
864 //
865 // Why we need this: given "class Foo implements Face", declare
866 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
867 // is part of the Face interface. We can't easily use a single
868 // vtable.
869 //
870 // For every interface a concrete class implements, we create a list
871 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700872 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700873 InterfaceEntry* iftable_;
874
875 // The interface vtable indices for iftable get stored here. By
876 // placing them all in a single pool for each class that implements
877 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700878 size_t ifvi_pool_count_;
879 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700880
881 // instance fields
882 //
883 // These describe the layout of the contents of a
884 // DataObject-compatible Object. Note that only the fields directly
885 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700886 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700887 //
888 // All instance fields that refer to objects are guaranteed to be at
889 // the beginning of the field list. ifieldRefCount specifies the
890 // number of reference fields.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700891 size_t num_instance_fields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700892
893 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700894 size_t num_reference_instance_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700895 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700896
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897 // Bitmap of offsets of ifields.
898 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700899
900 // source file name, if known. Otherwise, NULL.
901 const char* source_file_;
902
903 // Static fields
Carl Shapiro69759ea2011-07-21 18:13:35 -0700904 size_t num_static_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700905 StaticField** sfields_;
906
907 private:
908 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700909};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700910std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700911
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700912class DataObject : public Object {
913 public:
914 uint32_t fields_[1];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700915 private:
916 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700917};
918
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700919class Array : public Object {
920 public:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700921 uint32_t GetLength() const{
922 return length_;
923 }
924
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700925 void SetLength(uint32_t length) {
926 length_ = length;
927 }
928
929 private:
930 // The number of array elements.
931 uint32_t length_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700932
933 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700934};
935
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700936class CharArray : public Array {
937 private:
Carl Shapiro69759ea2011-07-21 18:13:35 -0700938 DISALLOW_IMPLICIT_CONSTRUCTORS(CharArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700939};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700940
Carl Shapiro69759ea2011-07-21 18:13:35 -0700941class ObjectArray : public Array {
942 public:
943 Object* Get(int32_t i) {
944 return NULL;
945 }
946
947 const Object* Get(int32_t i) const {
948 return NULL;
949 }
950
951 private:
952 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
953};
954
955
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700956class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700957 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700958 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700959
960 uint32_t hash_code_;
961
962 uint32_t offset_;
963
964 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700965
966 private:
967 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700968};
969
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700971 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700972 Class* GetClass() const {
973 return klass_;
974 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700975
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700976 void SetClass(Class* klass) {
977 klass_ = klass;
978 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700979
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700980 private:
981 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700982 Class* klass_;
983
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700984 public: // TODO: private
985 // Index into array of vtable offsets. This points into the
986 // ifviPool, which holds the vtables for all interfaces declared by
987 // this class.
988 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700989};
990
991} // namespace art
992
993#endif // ART_SRC_OBJECT_H_