blob: 770e8bbd9f0435aa75c780e539a5ec4a3edf14ba [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
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "constants.h"
7#include "casts.h"
8#include "globals.h"
9#include "logging.h"
10#include "macros.h"
11#include "offsets.h"
12#include "stringpiece.h"
13#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070014
15namespace art {
16
17class Array;
18class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070019class DexCache;
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 Shapiro69759ea2011-07-21 18:13:35 -070025class ObjectArray;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026class StaticField;
Carl Shapiro1fb86202011-06-27 17:43:13 -070027
Carl Shapiro3ee755d2011-06-28 12:11:04 -070028union JValue {
29 uint8_t z;
30 int8_t b;
31 uint16_t c;
32 int16_t s;
33 int32_t i;
34 int64_t j;
35 float f;
36 double d;
37 Object* l;
38};
39
Brian Carlstrombe977852011-07-19 14:54:54 -070040static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
41static const uint32_t kAccPrivate = 0x0002; // field, method, ic
42static const uint32_t kAccProtected = 0x0004; // field, method, ic
43static const uint32_t kAccStatic = 0x0008; // field, method, ic
44static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
45static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
46static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
47static const uint32_t kAccVolatile = 0x0040; // field
48static const uint32_t kAccBridge = 0x0040; // method (1.5)
49static const uint32_t kAccTransient = 0x0080; // field
50static const uint32_t kAccVarargs = 0x0080; // method (1.5)
51static const uint32_t kAccNative = 0x0100; // method
52static const uint32_t kAccInterface = 0x0200; // class, ic
53static const uint32_t kAccAbstract = 0x0400; // class, method, ic
54static const uint32_t kAccStrict = 0x0800; // method
55static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
56static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
57static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070058
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070059static const uint32_t kAccMiranda = 0x8000; // method
60
Brian Carlstroma331b3c2011-07-18 17:47:56 -070061static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
62
Brian Carlstrombe977852011-07-19 14:54:54 -070063static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
64static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070065
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068 */
69/*
70 * A magic value for refOffsets. Ignore the bits and walk the super
71 * chain when this is the value.
72 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
73 * fields followed by 2 ref instance fields.]
74 */
75#define CLASS_WALK_SUPER ((unsigned int)(3))
76#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
77#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
78#define CLASS_OFFSET_ALIGNMENT 4
79#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
80/*
81 * Given an offset, return the bit number which would encode that offset.
82 * Local use only.
83 */
84#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
85 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
86 CLASS_OFFSET_ALIGNMENT)
87/*
88 * Is the given offset too large to be encoded?
89 */
90#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
91 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
92/*
93 * Return a single bit, encoding the offset.
94 * Undefined if the offset is too large, as defined above.
95 */
96#define CLASS_BIT_FROM_OFFSET(byteOffset) \
97 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
98/*
99 * Return an offset, given a bit number as returned from CLZ.
100 */
101#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700102 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700103
104
Carl Shapiro1fb86202011-06-27 17:43:13 -0700105class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700106 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107 Class* GetClass() const {
108 return klass_;
109 }
110
111 void MonitorEnter() {
112 monitor_->Enter();
113 }
114
115 void MonitorExit() {
116 monitor_->Exit();
117 }
118
119 void Notify() {
120 monitor_->Notify();
121 }
122
123 void NotifyAll() {
124 monitor_->NotifyAll();
125 }
126
127 void Wait() {
128 monitor_->Wait();
129 }
130
131 void Wait(int64_t timeout) {
132 monitor_->Wait(timeout);
133 }
134
135 void Wait(int64_t timeout, int32_t nanos) {
136 monitor_->Wait(timeout, nanos);
137 }
138
Carl Shapiro69759ea2011-07-21 18:13:35 -0700139 const Object* GetFieldObject(size_t field_offset) const {
140 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
141 return *reinterpret_cast<Object* const*>(raw_addr);
142 }
143
144 Object* GetFieldObject(size_t field_offset) {
145 return const_cast<Object*>(GetFieldObject(field_offset));
146 }
147
148 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700149 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
150 *reinterpret_cast<Object**>(raw_addr) = new_value;
151 // TODO: write barrier
152 }
153
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 bool IsClass() const {
155 LOG(FATAL) << "Unimplemented";
156 return true;
157 }
158
159 Class* AsClass() {
160 return down_cast<Class*>(this);
161 }
162
163 const Class* AsClass() const {
164 return down_cast<const Class*>(this);
165 }
166
167 bool IsObjectArray() const {
168 LOG(FATAL) << "Unimplemented";
169 return true;
170 }
171
172 const ObjectArray* AsObjectArray() const {
173 return down_cast<const ObjectArray*>(this);
174 }
175
176 bool IsReference() const {
177 LOG(FATAL) << "Unimplemented";
178 return true;
179 }
180
181 bool IsWeakReference() const {
182 LOG(FATAL) << "Unimplemented";
183 return true;
184 }
185
186 bool IsSoftReference() const {
187 LOG(FATAL) << "Unimplemented";
188 return true;
189 }
190
191 bool IsFinalizerReference() const {
192 LOG(FATAL) << "Unimplemented";
193 return true;
194 }
195
196 bool IsPhantomReference() const {
197 LOG(FATAL) << "Unimplemented";
198 return true;
199 }
200
201 bool IsArray() const {
202 LOG(FATAL) << "Unimplemented";
203 return true;
204 }
205
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700206 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700207 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700209 Monitor* monitor_;
210
211 private:
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700212 Object();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700213 DISALLOW_COPY_AND_ASSIGN(Object);
214};
215
216class ObjectLock {
217 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700218 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700219 CHECK(object != NULL);
220 obj_->MonitorEnter();
221 }
222
223 ~ObjectLock() {
224 obj_->MonitorExit();
225 }
226
227 void Wait(int64_t millis = 0) {
228 return obj_->Wait(millis);
229 }
230
231 void Notify() {
232 obj_->Notify();
233 }
234
235 void NotifyAll() {
236 obj_->NotifyAll();
237 }
238
239 private:
240 Object* obj_;
241 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700242};
243
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700244class Field : public Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700245 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700246 Class* GetDeclaringClass() const {
247 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700248 }
249
250 const char* GetName() const {
251 return name_;
252 }
253
254 char GetType() const { // TODO: return type
255 return signature_[0];
256 }
257
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700258 const char* GetSignature() const {
259 return signature_;
260 }
261
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700263#define FIELD_FIELD_SLOTS 1+6
264 // AccessibleObject #0 flag
265 // Field #0 declaringClass
266 // Field #1 genericType
267 // Field #2 genericTypesAreInitialized
268 // Field #3 name
269 // Field #4 slot
270 // Field #5 type
271 uint32_t instance_data_[FIELD_FIELD_SLOTS];
272#undef FIELD_FIELD_SLOTS
273
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700274 // The class in which this field is declared.
Brian Carlstroma0808032011-07-18 00:39:23 -0700275 Class* declaring_class_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700276
277 const char* name_;
278
279 // e.g. "I", "[C", "Landroid/os/Debug;"
280 const char* signature_;
281
282 uint32_t access_flags_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700283
284 private:
285 Field();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700286};
287
288// Instance fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700289class InstanceField : public Field {
290 public:
291 uint32_t GetOffset() const {
292 return offset_;
293 }
294
295 void SetOffset(size_t num_bytes) {
296 offset_ = num_bytes;
297 }
298
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700299 private:
300 size_t offset_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700301 InstanceField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700302};
303
304// Static fields.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700305class StaticField : public Field {
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700306 public:
307 void SetBoolean(bool z) {
308 CHECK_EQ(GetType(), 'Z');
309 value_.z = z;
310 }
311
312 void SetByte(int8_t b) {
313 CHECK_EQ(GetType(), 'B');
314 value_.b = b;
315 }
316
317 void SetChar(uint16_t c) {
318 CHECK_EQ(GetType(), 'C');
319 value_.c = c;
320 }
321
322 void SetShort(uint16_t s) {
323 CHECK_EQ(GetType(), 'S');
324 value_.s = s;
325 }
326
327 void SetInt(int32_t i) {
328 CHECK_EQ(GetType(), 'I');
329 value_.i = i;
330 }
331
332 int64_t GetLong() {
333 CHECK_EQ(GetType(), 'J');
334 return value_.j;
335 }
336
337 void SetLong(int64_t j) {
338 CHECK_EQ(GetType(), 'J');
339 value_.j = j;
340 }
341
342 void SetFloat(float f) {
343 CHECK_EQ(GetType(), 'F');
344 value_.f = f;
345 }
346
347 void SetDouble(double d) {
348 CHECK_EQ(GetType(), 'D');
349 value_.d = d;
350 }
351
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352 Object* GetObject() {
353 return value_.l;
354 }
355
356 const Object* GetObject() const {
357 return value_.l;
358 }
359
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700360 void SetObject(Object* l) {
Carl Shapiro565f5072011-07-10 13:39:43 -0700361 CHECK(GetType() == 'L' || GetType() == '[');
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700362 value_.l = l;
363 // TODO: write barrier
364 }
365
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700366 private:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700367 JValue value_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700368 StaticField();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700369};
370
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700371class Method : public Object {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700372 public:
373 // Returns the method name.
374 // TODO: example
375 const StringPiece& GetName() const {
376 return name_;
377 }
378
Brian Carlstroma0808032011-07-18 00:39:23 -0700379 Class* GetDeclaringClass() const {
380 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700381 }
382
Ian Rogersb033c752011-07-20 12:22:35 -0700383 static MemberOffset ClassOffset() {
384 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
385 }
386
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700387 // const char* GetReturnTypeDescriptor() const {
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700388 // return FindDexFile(declaring_class_->GetDexCache()
Ian Rogersb033c752011-07-20 12:22:35 -0700389 // ->dexStringByTypeIdx(proto_id_.return_type_id_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700390 // }
391
392 // Returns true if the method is declared public.
393 bool IsPublic() const {
394 return (access_flags_ & kAccPublic) != 0;
395 }
396
397 // Returns true if the method is declared private.
398 bool IsPrivate() const {
399 return (access_flags_ & kAccPrivate) != 0;
400 }
401
402 // Returns true if the method is declared static.
403 bool IsStatic() const {
404 return (access_flags_ & kAccStatic) != 0;
405 }
406
407 // Returns true if the method is declared synchronized.
408 bool IsSynchronized() const {
409 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
410 return (access_flags_ & synchonized) != 0;
411 }
412
413 // Returns true if the method is declared final.
414 bool IsFinal() const {
415 return (access_flags_ & kAccFinal) != 0;
416 }
417
418 // Returns true if the method is declared native.
419 bool IsNative() const {
420 return (access_flags_ & kAccNative) != 0;
421 }
422
423 // Returns true if the method is declared abstract.
424 bool IsAbstract() const {
425 return (access_flags_ & kAccAbstract) != 0;
426 }
427
428 bool IsSynthetic() const {
429 return (access_flags_ & kAccSynthetic) != 0;
430 }
431
432 // Number of argument registers required by the prototype.
433 uint32_t NumArgRegisters();
434
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700435 public: // TODO: private
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700436#define METHOD_FIELD_SLOTS 1+11
437 // AccessibleObject #0 flag
438 // Method #0 declaringClass
439 // Method #1 exceptionTypes
440 // Method #2 formalTypeParameters
441 // Method #3 genericExceptionTypes
442 // Method #4 genericParameterTypes
443 // Method #5 genericReturnType
444 // Method #6 genericTypesAreInitialized
445 // Method #7 name
446 // Method #8 parameterTypes
447 // Method #9 returnType
448 // Method #10 slot
449 uint32_t instance_data_[METHOD_FIELD_SLOTS];
450#undef METHOD_FIELD_SLOTS
451
Ian Rogersb033c752011-07-20 12:22:35 -0700452 bool IsReturnAReference() const {
453 return (shorty_[0] == 'L') || (shorty_[0] == '[');
454 }
455
456 bool IsReturnAFloatOrDouble() const {
457 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
458 }
459
460 bool IsReturnAFloat() const {
461 return shorty_[0] == 'F';
462 }
463
464 bool IsReturnADouble() const {
465 return shorty_[0] == 'D';
466 }
467
468 bool IsReturnALong() const {
469 return shorty_[0] == 'J';
470 }
471
Ian Rogers45a76cb2011-07-21 22:00:15 -0700472 bool IsReturnVoid() const {
473 return shorty_[0] == 'V';
474 }
475
Ian Rogersb033c752011-07-20 12:22:35 -0700476 // The number of arguments that should be supplied to this method
477 size_t NumArgs() const {
478 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
479 }
480
481 // The number of reference arguments to this method including implicit this
482 // pointer
483 size_t NumReferenceArgs() const;
484
485 // The number of long or double arguments
486 size_t NumLongOrDoubleArgs() const;
487
488 // The number of reference arguments to this method before the given
489 // parameter index
490 size_t NumReferenceArgsBefore(unsigned int param) const;
491
492 // Is the given method parameter a reference?
493 bool IsParamAReference(unsigned int param) const;
494
495 // Is the given method parameter a long or double?
496 bool IsParamALongOrDouble(unsigned int param) const;
497
Ian Rogersdf20fe02011-07-20 20:34:16 -0700498 // Size in bytes of the given parameter
499 size_t ParamSize(unsigned int param) const;
500
501 // Size in bytes of the return value
502 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700503
504 void SetCode(const void* code) {
505 code_ = code;
506 }
507
508 const void* GetCode() const {
509 return code_;
510 }
511
512 void RegisterNative(const void* native_method) {
513 native_method_ = native_method;
514 }
515
516 static MemberOffset NativeMethodOffset() {
517 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
518 }
519
520 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700521 // the class we are a part of
Brian Carlstroma0808032011-07-18 00:39:23 -0700522 Class* declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700523
524 // access flags; low 16 bits are defined by spec (could be uint16_t?)
525 uint32_t access_flags_;
526
527 // For concrete virtual methods, this is the offset of the method
528 // in "vtable".
529 //
530 // For abstract methods in an interface class, this is the offset
531 // of the method in "iftable[n]->methodIndexArray".
532 uint16_t method_index_;
533
534 // Method bounds; not needed for an abstract method.
535 //
536 // For a native method, we compute the size of the argument list, and
537 // set "insSize" and "registerSize" equal to it.
538 uint16_t num_registers_; // ins + locals
539 uint16_t num_outs_;
540 uint16_t num_ins_;
541
542 // method name, e.g. "<init>" or "eatLunch"
543 StringPiece name_;
544
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700545 // Method prototype descriptor string (return and argument types).
546 uint32_t proto_idx_;
547
548 // The short-form method descriptor string.
549 StringPiece shorty_;
550
551 // A pointer to the memory-mapped DEX code.
552 const uint16_t* insns_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700553
554 private:
555 Method();
Ian Rogersb033c752011-07-20 12:22:35 -0700556
557 // Compiled code associated with this method
558 const void* code_;
559
560 // Any native method registered with this method
561 const void* native_method_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700562};
563
Carl Shapiro1fb86202011-06-27 17:43:13 -0700564// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700565class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700566 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700567 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700568 kStatusError = -1,
569 kStatusNotReady = 0,
570 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
571 kStatusLoaded = 2, // DEX idx values resolved
572 kStatusResolved = 3, // part of linking
573 kStatusVerifying = 4, // in the process of being verified
574 kStatusVerified = 5, // logically part of linking; done pre-init
575 kStatusInitializing = 6, // class init in progress
576 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700577 };
578
579 enum PrimitiveType {
580 kPrimNot = -1
581 };
582
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700583 Class* GetSuperClass() const {
584 return super_class_;
585 }
586
587 uint32_t GetSuperClassIdx() const {
588 return super_class_idx_;
589 }
590
591 bool HasSuperClass() const {
592 return super_class_ != NULL;
593 }
594
595 Object* GetClassLoader() const {
596 return class_loader_;
597 }
598
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700599 DexCache* GetDexCache() const {
600 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700601 }
602
603 Class* GetComponentType() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700604 DCHECK(IsArray());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700605 return component_type_;
606 }
607
608 const StringPiece& GetDescriptor() const {
609 return descriptor_;
610 }
611
612 Status GetStatus() const {
613 return status_;
614 }
615
616 void SetStatus(Status new_status) {
617 // TODO: validate transition
618 status_ = new_status;
619 }
620
Carl Shapiro69759ea2011-07-21 18:13:35 -0700621 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700622 bool IsErroneous() const {
623 return GetStatus() == kStatusError;
624 }
625
Carl Shapiro69759ea2011-07-21 18:13:35 -0700626 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700627 bool IsVerified() const {
628 return GetStatus() >= kStatusVerified;
629 }
630
Carl Shapiro69759ea2011-07-21 18:13:35 -0700631 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700632 bool IsLinked() const {
633 return GetStatus() >= kStatusResolved;
634 }
635
Carl Shapiro69759ea2011-07-21 18:13:35 -0700636 bool IsLoaded() const {
637 return GetStatus() >= kStatusLoaded;
638 }
639
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700640 // Returns true if this class is in the same packages as that class.
641 bool IsInSamePackage(const Class* that) const;
642
Ian Rogersb033c752011-07-20 12:22:35 -0700643 static bool IsInSamePackage(const StringPiece& descriptor1,
644 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700645
646 // Returns true if this class represents an array class.
647 bool IsArray() const {
648 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
649 }
650
651 // Returns true if the class is an interface.
652 bool IsInterface() const {
653 return (access_flags_ & kAccInterface) != 0;
654 }
655
656 // Returns true if the class is declared public.
657 bool IsPublic() const {
658 return (access_flags_ & kAccPublic) != 0;
659 }
660
661 // Returns true if the class is declared final.
662 bool IsFinal() const {
663 return (access_flags_ & kAccFinal) != 0;
664 }
665
666 // Returns true if the class is abstract.
667 bool IsAbstract() const {
668 return (access_flags_ & kAccAbstract) != 0;
669 }
670
671 // Returns true if the class is an annotation.
672 bool IsAnnotation() const {
673 return (access_flags_ & kAccAnnotation) != 0;
674 }
675
676 // Returns true if the class is a primitive type.
677 bool IsPrimitive() const {
678 return primitive_type_ != kPrimNot;
679 }
680
681 // Returns true if this class can access that class.
682 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700683 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700684 }
685
Carl Shapiro1fb86202011-06-27 17:43:13 -0700686 // Returns the size in bytes of a class object instance with the
687 // given number of static fields.
Carl Shapiro565f5072011-07-10 13:39:43 -0700688 // static size_t Size(size_t num_sfields) {
Ian Rogersb033c752011-07-20 12:22:35 -0700689 // return OFFSETOF_MEMBER(Class, sfields_) +
690 // sizeof(StaticField) * num_sfields;
Carl Shapiro565f5072011-07-10 13:39:43 -0700691 // }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700692
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700693 // Returns the number of static, private, and constructor methods.
694 size_t NumDirectMethods() const {
695 return num_direct_methods_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700696 }
697
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700698 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700699 return direct_methods_[i];
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700700 }
701
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700702 // Returns the number of non-inherited virtual methods.
703 size_t NumVirtualMethods() const {
704 return num_virtual_methods_;
705 }
706
707 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700708 return virtual_methods_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700709 }
710
711 size_t NumInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700712 return num_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700713 }
714
Carl Shapiro69759ea2011-07-21 18:13:35 -0700715 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700716 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700717 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700718 }
719
720 InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700721 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700722 return ifields_[i];
723 }
724
725 void SetInstanceField(uint32_t i, InstanceField* f) { // TODO: uint16_t
Carl Shapiro69759ea2011-07-21 18:13:35 -0700726 DCHECK_LT(i, num_instance_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700727 ifields_[i] = f;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700728 }
729
730 size_t NumStaticFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700731 return num_static_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700732 }
733
Carl Shapiro69759ea2011-07-21 18:13:35 -0700734 StaticField* GetStaticField(uint32_t i) const { // TODO: uint16_t
735 DCHECK_LT(i, num_static_fields_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700736 return sfields_[i];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700737 }
738
739 uint32_t GetReferenceOffsets() const {
740 return reference_offsets_;
741 }
742
743 void SetReferenceOffsets(uint32_t new_reference_offsets) {
744 reference_offsets_ = new_reference_offsets;
745 }
746
Ian Rogersb033c752011-07-20 12:22:35 -0700747 Method* FindDirectMethod(const StringPiece& name) const;
748
749 Method* FindVirtualMethod(const StringPiece& name) const;
750
Carl Shapiro69759ea2011-07-21 18:13:35 -0700751 size_t NumInterfaces() const {
752 return interface_count_;
753 }
754
755 Class* GetInterface(uint32_t i) const {
756 DCHECK_LT(i, interface_count_);
757 return interfaces_[i];
758 }
759
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700760 Method* FindDirectMethodLocally(const StringPiece& name,
761 const StringPiece& descriptor) const;
762
Ian Rogersb033c752011-07-20 12:22:35 -0700763 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700764 // leave space for instance data; we could access fields directly if
765 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700766#define CLASS_FIELD_SLOTS 1
767 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700768 uint32_t instance_data_[CLASS_FIELD_SLOTS];
769#undef CLASS_FIELD_SLOTS
770
771 // UTF-8 descriptor for the class from constant pool
772 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700773 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700774
775 // Proxy classes have their descriptor allocated on the native heap.
776 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700777 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700778
779 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700780 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700781
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700782 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700783 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700784 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700785
786 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700788
789 // if class verify fails, we must return same error on subsequent tries
790 Class* verify_error_class_;
791
792 // threadId, used to check for recursive <clinit> invocation
793 uint32_t clinit_thread_id_;
794
795 // Total object size; used when allocating storage on gc heap. (For
796 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700797 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700798
799 // For array classes, the class object for base element, for
800 // instanceof/checkcast (for String[][][], this will be String).
801 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700802 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700803
804 // For array classes, the number of array dimensions, e.g. int[][]
805 // is 2. Otherwise 0.
806 int32_t array_rank_;
807
808 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
809 PrimitiveType primitive_type_;
810
811 // The superclass, or NULL if this is java.lang.Object or a
812 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700813 Class* super_class_; // TODO: make an instance field
814 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700815
816 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700817 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700818
819 // initiating class loader list
820 // NOTE: for classes with low serialNumber, these are unused, and the
821 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -0700822 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700823
824 // array of interfaces this class implements directly
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700825 size_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700826 Class** interfaces_;
827
828 // static, private, and <init> methods
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700829 size_t num_direct_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700830 Method** direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700831
832 // virtual methods defined in this class; invoked through vtable
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700833 size_t num_virtual_methods_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700834 Method** virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700835
836 // Virtual method table (vtable), for use by "invoke-virtual". The
837 // vtable from the superclass is copied in, and virtual methods from
838 // our class either replace those from the super or are appended.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700839 size_t vtable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700840 Method** vtable_;
841
842 // Interface table (iftable), one entry per interface supported by
843 // this class. That means one entry for each interface we support
844 // directly, indirectly via superclass, or indirectly via
845 // superinterface. This will be null if neither we nor our
846 // superclass implement any interfaces.
847 //
848 // Why we need this: given "class Foo implements Face", declare
849 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
850 // is part of the Face interface. We can't easily use a single
851 // vtable.
852 //
853 // For every interface a concrete class implements, we create a list
854 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700856 InterfaceEntry* iftable_;
857
858 // The interface vtable indices for iftable get stored here. By
859 // placing them all in a single pool for each class that implements
860 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700861 size_t ifvi_pool_count_;
862 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700863
864 // instance fields
865 //
866 // These describe the layout of the contents of a
867 // DataObject-compatible Object. Note that only the fields directly
868 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700869 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -0700870 //
871 // All instance fields that refer to objects are guaranteed to be at
872 // the beginning of the field list. ifieldRefCount specifies the
873 // number of reference fields.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700874 size_t num_instance_fields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700875
876 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -0700877 size_t num_reference_instance_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700878 InstanceField** ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700879
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700880 // Bitmap of offsets of ifields.
881 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700882
883 // source file name, if known. Otherwise, NULL.
884 const char* source_file_;
885
886 // Static fields
Carl Shapiro69759ea2011-07-21 18:13:35 -0700887 size_t num_static_fields_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700888 StaticField** sfields_;
889
890 private:
891 Class();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700892};
Elliott Hughes1f359b02011-07-17 14:27:17 -0700893std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700894
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700895class DataObject : public Object {
896 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700897 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700898 private:
899 DataObject();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700900};
901
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700902class Array : public Object {
903 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700904 uint32_t GetLength() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700905 return length_;
906 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700907 void SetLength(uint32_t length) {
908 length_ = length;
909 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700910 const void* GetData() const {
911 return &elements_;
912 }
913 void* GetData() {
914 return &elements_;
915 }
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700916
917 private:
918 // The number of array elements.
919 uint32_t length_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700920 // Location of first element.
921 uint32_t elements_[0];
922 Array();
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700923};
924
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700925class CharArray : public Array {
926 private:
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700927 CharArray();
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700928};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700929
Carl Shapiro69759ea2011-07-21 18:13:35 -0700930class ObjectArray : public Array {
931 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700932 Object* Get(uint32_t i) const {
933 DCHECK_LT(i, GetLength());
934 Object* const * data = reinterpret_cast<Object* const *>(GetData());
935 return data[i];
Carl Shapiro69759ea2011-07-21 18:13:35 -0700936 }
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700937 void Set(uint32_t i, Object* object) {
938 DCHECK_LT(i, GetLength());
939 Object** data = reinterpret_cast<Object**>(GetData());
940 data[i] = object;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700941 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700942 private:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700943 ObjectArray();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700944};
945
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700947 public:
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700948 CharArray* array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700949
950 uint32_t hash_code_;
951
952 uint32_t offset_;
953
954 uint32_t count_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700955
956 private:
957 String();
Carl Shapiro1fb86202011-06-27 17:43:13 -0700958};
959
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700960class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700961 public:
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700962 Class* GetClass() const {
963 return klass_;
964 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700965
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700966 void SetClass(Class* klass) {
967 klass_ = klass;
968 };
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700969
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970 private:
971 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700972 Class* klass_;
973
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700974 public: // TODO: private
975 // Index into array of vtable offsets. This points into the
976 // ifviPool, which holds the vtables for all interfaces declared by
977 // this class.
978 uint32_t* method_index_array_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700979};
980
981} // namespace art
982
983#endif // ART_SRC_OBJECT_H_