blob: 927729ce5826ab5e87a434fc2e375fe5f599131f [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"
Shih-wei Liao2fb97532011-08-11 16:17:23 -07008#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070010#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "logging.h"
12#include "macros.h"
13#include "offsets.h"
14#include "stringpiece.h"
15#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070016
17namespace art {
18
19class Array;
20class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070021class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040022class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070023class InterfaceEntry;
24class Monitor;
25class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070026class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040027class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070028template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070029template<class T> class PrimitiveArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070030typedef PrimitiveArray<uint16_t> CharArray;
31typedef PrimitiveArray<uint32_t> IntArray;
32typedef PrimitiveArray<uint64_t> LongArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070033
Carl Shapiro3ee755d2011-06-28 12:11:04 -070034union JValue {
35 uint8_t z;
36 int8_t b;
37 uint16_t c;
38 int16_t s;
39 int32_t i;
40 int64_t j;
41 float f;
42 double d;
43 Object* l;
44};
45
Brian Carlstrombe977852011-07-19 14:54:54 -070046static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
47static const uint32_t kAccPrivate = 0x0002; // field, method, ic
48static const uint32_t kAccProtected = 0x0004; // field, method, ic
49static const uint32_t kAccStatic = 0x0008; // field, method, ic
50static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
51static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
52static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
53static const uint32_t kAccVolatile = 0x0040; // field
54static const uint32_t kAccBridge = 0x0040; // method (1.5)
55static const uint32_t kAccTransient = 0x0080; // field
56static const uint32_t kAccVarargs = 0x0080; // method (1.5)
57static const uint32_t kAccNative = 0x0100; // method
58static const uint32_t kAccInterface = 0x0200; // class, ic
59static const uint32_t kAccAbstract = 0x0400; // class, method, ic
60static const uint32_t kAccStrict = 0x0800; // method
61static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
62static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
63static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070064
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065static const uint32_t kAccMiranda = 0x8000; // method
66
Brian Carlstroma331b3c2011-07-18 17:47:56 -070067static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
68
Brian Carlstrombe977852011-07-19 14:54:54 -070069static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
70static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070073 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070074 */
75/*
76 * A magic value for refOffsets. Ignore the bits and walk the super
77 * chain when this is the value.
78 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
79 * fields followed by 2 ref instance fields.]
80 */
81#define CLASS_WALK_SUPER ((unsigned int)(3))
82#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
83#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
84#define CLASS_OFFSET_ALIGNMENT 4
85#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
86/*
87 * Given an offset, return the bit number which would encode that offset.
88 * Local use only.
89 */
90#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
91 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
92 CLASS_OFFSET_ALIGNMENT)
93/*
94 * Is the given offset too large to be encoded?
95 */
96#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
97 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
98/*
99 * Return a single bit, encoding the offset.
100 * Undefined if the offset is too large, as defined above.
101 */
102#define CLASS_BIT_FROM_OFFSET(byteOffset) \
103 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
104/*
105 * Return an offset, given a bit number as returned from CLZ.
106 */
107#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700108 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109
110
Carl Shapiro1fb86202011-06-27 17:43:13 -0700111class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700112 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700113 static bool InstanceOf(const Object* object, const Class* klass) {
114 if (object == NULL) {
115 return false;
116 }
117 return object->InstanceOf(klass);
118 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700119
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700120 Class* GetClass() const {
121 return klass_;
122 }
123
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700124 bool InstanceOf(const Class* klass) const;
125
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 void MonitorEnter() {
127 monitor_->Enter();
128 }
129
130 void MonitorExit() {
131 monitor_->Exit();
132 }
133
134 void Notify() {
135 monitor_->Notify();
136 }
137
138 void NotifyAll() {
139 monitor_->NotifyAll();
140 }
141
142 void Wait() {
143 monitor_->Wait();
144 }
145
146 void Wait(int64_t timeout) {
147 monitor_->Wait(timeout);
148 }
149
150 void Wait(int64_t timeout, int32_t nanos) {
151 monitor_->Wait(timeout, nanos);
152 }
153
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154 const Object* GetFieldObject(size_t field_offset) const {
155 const byte* raw_addr = reinterpret_cast<const byte*>(this) + field_offset;
156 return *reinterpret_cast<Object* const*>(raw_addr);
157 }
158
159 Object* GetFieldObject(size_t field_offset) {
160 return const_cast<Object*>(GetFieldObject(field_offset));
161 }
162
163 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700164 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
165 *reinterpret_cast<Object**>(raw_addr) = new_value;
166 // TODO: write barrier
167 }
168
Carl Shapiro69759ea2011-07-21 18:13:35 -0700169 bool IsClass() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700170 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 return true;
172 }
173
174 Class* AsClass() {
175 return down_cast<Class*>(this);
176 }
177
178 const Class* AsClass() const {
179 return down_cast<const Class*>(this);
180 }
181
182 bool IsObjectArray() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700183 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700184 return true;
185 }
186
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700187 const ObjectArray<Object>* AsObjectArray() const {
188 return down_cast<const ObjectArray<Object>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700189 }
190
191 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700192 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700193 return true;
194 }
195
196 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700197 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 return true;
199 }
200
201 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700202 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203 return true;
204 }
205
206 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700207 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700208 return true;
209 }
210
211 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700212 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700213 return true;
214 }
215
216 bool IsArray() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700217 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700218 return true;
219 }
220
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700221 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700222 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700223
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700224 Monitor* monitor_;
225
226 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700227 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700228};
229
230class ObjectLock {
231 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700232 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700233 CHECK(object != NULL);
234 obj_->MonitorEnter();
235 }
236
237 ~ObjectLock() {
238 obj_->MonitorExit();
239 }
240
241 void Wait(int64_t millis = 0) {
242 return obj_->Wait(millis);
243 }
244
245 void Notify() {
246 obj_->Notify();
247 }
248
249 void NotifyAll() {
250 obj_->NotifyAll();
251 }
252
253 private:
254 Object* obj_;
255 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700256};
257
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400258class AccessibleObject : public Object {
259 private:
260 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
261 uint32_t java_flag_;
262};
263
264class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700265 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700266 Class* GetDeclaringClass() const {
267 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700268 }
269
Jesse Wilson14150742011-07-29 19:04:44 -0400270 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700271 return name_;
272 }
273
274 bool IsStatic() const {
275 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700276 }
277
278 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700279 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700280 }
281
Brian Carlstromae3ac012011-07-27 01:30:28 -0700282 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700283 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700284 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700285 }
286
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700287 uint32_t GetOffset() const {
288 return offset_;
289 }
290
291 void SetOffset(size_t num_bytes) {
292 offset_ = num_bytes;
293 }
294
Jesse Wilson35baaab2011-08-10 16:18:03 -0400295 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400296 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400297 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400298 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400299 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400300 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400301 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400302 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400303 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400304 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400305 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400306 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400307 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400308 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400309 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400310 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400311 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400312 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400313 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400314 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700315
Jesse Wilson35baaab2011-08-10 16:18:03 -0400316 public: // TODO: private
317 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
318 // The class in which this field is declared.
319 Class* declaring_class_;
320 Object* generic_type_;
321 uint32_t generic_types_are_initialized_;
322 String* name_;
323 uint32_t offset_;
324 Class* type_;
325
326 // e.g. "I", "[C", "Landroid/os/Debug;"
327 StringPiece descriptor_;
328
329 uint32_t access_flags_;
330
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400332 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700333};
334
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400335class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700336 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700337 // An function that invokes a method with an array of its arguments.
338 typedef void InvokeStub(Method* method,
339 Object* obj,
340 Thread* thread,
341 byte* args,
342 JValue* result);
343
Brian Carlstromae3ac012011-07-27 01:30:28 -0700344 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700345 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700346 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347 }
348
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700349 const String* GetDescriptor() const {
350 return descriptor_;
351 }
352
Brian Carlstroma0808032011-07-18 00:39:23 -0700353 Class* GetDeclaringClass() const {
354 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700355 }
356
Ian Rogersb033c752011-07-20 12:22:35 -0700357 static MemberOffset ClassOffset() {
358 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
359 }
360
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700361 // Returns true if the method is declared public.
362 bool IsPublic() const {
363 return (access_flags_ & kAccPublic) != 0;
364 }
365
366 // Returns true if the method is declared private.
367 bool IsPrivate() const {
368 return (access_flags_ & kAccPrivate) != 0;
369 }
370
371 // Returns true if the method is declared static.
372 bool IsStatic() const {
373 return (access_flags_ & kAccStatic) != 0;
374 }
375
376 // Returns true if the method is declared synchronized.
377 bool IsSynchronized() const {
378 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
379 return (access_flags_ & synchonized) != 0;
380 }
381
382 // Returns true if the method is declared final.
383 bool IsFinal() const {
384 return (access_flags_ & kAccFinal) != 0;
385 }
386
387 // Returns true if the method is declared native.
388 bool IsNative() const {
389 return (access_flags_ & kAccNative) != 0;
390 }
391
392 // Returns true if the method is declared abstract.
393 bool IsAbstract() const {
394 return (access_flags_ & kAccAbstract) != 0;
395 }
396
397 bool IsSynthetic() const {
398 return (access_flags_ & kAccSynthetic) != 0;
399 }
400
401 // Number of argument registers required by the prototype.
402 uint32_t NumArgRegisters();
403
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700404 // Number of argument bytes required for densely packing the
405 // arguments into an array of arguments.
406 size_t NumArgArrayBytes();
407
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400409 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400410 // the class we are a part of
411 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400412 ObjectArray<Class>* java_exception_types_;
413 Object* java_formal_type_parameters_;
414 Object* java_generic_exception_types_;
415 Object* java_generic_parameter_types_;
416 Object* java_generic_return_type_;
417 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700418 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400419 ObjectArray<Class>* java_parameter_types_;
420 uint32_t java_generic_types_are_initialized_;
421 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700422
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700423 const StringPiece& GetShorty() const {
424 return shorty_;
425 }
426
Ian Rogersb033c752011-07-20 12:22:35 -0700427 bool IsReturnAReference() const {
428 return (shorty_[0] == 'L') || (shorty_[0] == '[');
429 }
430
431 bool IsReturnAFloatOrDouble() const {
432 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
433 }
434
435 bool IsReturnAFloat() const {
436 return shorty_[0] == 'F';
437 }
438
439 bool IsReturnADouble() const {
440 return shorty_[0] == 'D';
441 }
442
443 bool IsReturnALong() const {
444 return shorty_[0] == 'J';
445 }
446
Ian Rogers45a76cb2011-07-21 22:00:15 -0700447 bool IsReturnVoid() const {
448 return shorty_[0] == 'V';
449 }
450
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700451 // "Args" may refer to any of the 3 levels of "Args."
452 // To avoid confusion, our code will denote which "Args" clearly:
453 // 1. UserArgs: Args that a user see.
454 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
455 // receiver.
456 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
457 // E.g., the first in Args is Method* for both static and non-static
458 // methods. And CConvArgs doesn't deal with the receiver because
459 // receiver is hardwired in an implicit register, so CConvArgs doesn't
460 // need to deal with it.
461 //
462 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700463 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700464 // "1 +" because the first in Args is the receiver.
465 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700466 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
467 }
468
469 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700470 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700471 size_t NumReferenceArgs() const;
472
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700473 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700474 size_t NumLongOrDoubleArgs() const;
475
476 // The number of reference arguments to this method before the given
477 // parameter index
478 size_t NumReferenceArgsBefore(unsigned int param) const;
479
480 // Is the given method parameter a reference?
481 bool IsParamAReference(unsigned int param) const;
482
483 // Is the given method parameter a long or double?
484 bool IsParamALongOrDouble(unsigned int param) const;
485
Ian Rogersdf20fe02011-07-20 20:34:16 -0700486 // Size in bytes of the given parameter
487 size_t ParamSize(unsigned int param) const;
488
489 // Size in bytes of the return value
490 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700491
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700492 const void* GetCode() const {
493 return code_;
494 }
495
Ian Rogersb033c752011-07-20 12:22:35 -0700496 void SetCode(const void* code) {
497 code_ = code;
498 }
499
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700500 static size_t GetCodeOffset() {
501 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700502 }
503
504 void RegisterNative(const void* native_method) {
505 native_method_ = native_method;
506 }
507
508 static MemberOffset NativeMethodOffset() {
509 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
510 }
511
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700512 InvokeStub* GetInvokeStub() const {
513 return invoke_stub_;
514 }
515
516 void SetInvokeStub(const InvokeStub* invoke_stub) {
517 invoke_stub_ = invoke_stub;
518 }
519
520 static size_t GetInvokeStubOffset() {
521 return OFFSETOF_MEMBER(Method, invoke_stub_);
522 }
523
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700524 bool HasSameNameAndDescriptor(const Method* that) const;
525
Ian Rogersb033c752011-07-20 12:22:35 -0700526 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700527 // access flags; low 16 bits are defined by spec (could be uint16_t?)
528 uint32_t access_flags_;
529
530 // For concrete virtual methods, this is the offset of the method
531 // in "vtable".
532 //
533 // For abstract methods in an interface class, this is the offset
534 // of the method in "iftable[n]->methodIndexArray".
535 uint16_t method_index_;
536
537 // Method bounds; not needed for an abstract method.
538 //
539 // For a native method, we compute the size of the argument list, and
540 // set "insSize" and "registerSize" equal to it.
541 uint16_t num_registers_; // ins + locals
542 uint16_t num_outs_;
543 uint16_t num_ins_;
544
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700545 // The method descriptor. This represents the parameters a method
546 // takes and value it returns. This string is a list of the type
547 // descriptors for the parameters enclosed in parenthesis followed
548 // by the return type descriptor. For example, for the method
549 //
550 // Object mymethod(int i, double d, Thread t)
551 //
552 // the method descriptor would be
553 //
554 // (IDLjava/lang/Thread;)Ljava/lang/Object;
555 String* descriptor_;
556
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700557 // Method prototype descriptor string (return and argument types).
558 uint32_t proto_idx_;
559
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700560 // Offset to the CodeItem.
561 uint32_t code_off_;
562
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700563 // The short-form method descriptor string.
564 StringPiece shorty_;
565
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700566 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700567 // Compiled code associated with this method
568 const void* code_;
569
570 // Any native method registered with this method
571 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700572
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700573 // Native invocation stub entry point.
574 const InvokeStub* invoke_stub_;
575
Carl Shapirof88c9522011-08-06 15:47:38 -0700576 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700577};
578
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700579class Array : public Object {
580 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700581 static Array* Alloc(Class* array_class,
582 size_t component_count,
583 size_t component_size) {
584 size_t size = sizeof(Array) + component_count * component_size;
585 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
586 if (array != NULL) {
587 array->SetLength(component_count);
588 }
589 return array;
590 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700591
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700592 uint32_t GetLength() const {
593 return length_;
594 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700595
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700596 void SetLength(uint32_t length) {
597 length_ = length;
598 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700599
600 private:
601 // The number of array elements.
602 uint32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400603 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
604 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700605
Carl Shapirof88c9522011-08-06 15:47:38 -0700606 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700607};
608
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700609template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700610class ObjectArray : public Array {
611 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700612 static ObjectArray<T>* Alloc(Class* object_array_class,
613 size_t length) {
614 return down_cast<ObjectArray<T>*>(Array::Alloc(object_array_class,
615 length,
616 sizeof(uint32_t)));
617 }
618
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700619 T* const * GetData() const {
620 return reinterpret_cast<T* const *>(&elements_);
621 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400622
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700623 T** GetData() {
624 return reinterpret_cast<T**>(&elements_);
625 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400626
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700627 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700628 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700629 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700630 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700631
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700632 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700633 CHECK_LT(i, GetLength());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400634 GetData()[i] = object; // TODO: write barrier
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700635 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700636
637 static void Copy(ObjectArray<T>* src, int src_pos,
638 ObjectArray<T>* dst, int dst_pos,
639 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700640 for (size_t i = 0; i < length; i++) {
641 dst->Set(dst_pos + i, src->Get(src_pos + i));
642 }
643 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700644
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700645 ObjectArray<T>* CopyOf(size_t new_length) {
646 ObjectArray<T>* new_array = Alloc(klass_, new_length);
647 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
648 return new_array;
649 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700650
651 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700652 // Location of first element.
653 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700654
655 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700656};
657
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700658// ClassLoader objects.
659class ClassLoader : public Object {
660 public:
661 std::vector<const DexFile*>& GetClassPath() {
662 return class_path_;
663 }
664 void SetClassPath(std::vector<const DexFile*>& class_path) {
665 DCHECK_EQ(0U, class_path_.size());
666 class_path_ = class_path;
667 }
668
669 private:
670 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
671 Object* packages_;
672 ClassLoader* parent_;
673
674 // TODO remove once we can create a real PathClassLoader
675 std::vector<const DexFile*> class_path_;
676
Carl Shapirof88c9522011-08-06 15:47:38 -0700677 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700678};
679
680class BaseDexClassLoader : public ClassLoader {
681 private:
682 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
683 String* original_path_;
684 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700685 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700686};
687
688class PathClassLoader : public BaseDexClassLoader {
689 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700690 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700691};
692
Carl Shapiro1fb86202011-06-27 17:43:13 -0700693// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700694class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700695 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700696
697 // Class Status
698 //
699 // kStatusNotReady: If a Class cannot be found in the class table by
700 // FindClass, it allocates an new one with AllocClass in the
701 // kStatusNotReady and calls LoadClass. Note if it does find a
702 // class, it may not be kStatusResolved and it will try to push it
703 // forward toward kStatusResolved.
704 //
705 // kStatusIdx: LoadClass populates with Class with information from
706 // the DexFile, moving the status to kStatusIdx, indicating that the
707 // Class values in super_class_ and interfaces_ have not been
708 // populated based on super_class_idx_ and interfaces_idx_. The new
709 // Class can then be inserted into the classes table.
710 //
711 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
712 // attempt to move a kStatusIdx class forward to kStatusLoaded by
713 // using ResolveClass to initialize the super_class_ and interfaces_.
714 //
715 // kStatusResolved: Still holding the lock on Class, the ClassLinker
716 // will use LinkClass to link all members, creating Field and Method
717 // objects, setting up the vtable, etc. On success, the class is
718 // marked kStatusResolved.
719
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700720 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700721 kStatusError = -1,
722 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700723 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700724 kStatusLoaded = 2, // DEX idx values resolved
725 kStatusResolved = 3, // part of linking
726 kStatusVerifying = 4, // in the process of being verified
727 kStatusVerified = 5, // logically part of linking; done pre-init
728 kStatusInitializing = 6, // class init in progress
729 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700730 };
731
732 enum PrimitiveType {
733 kPrimNot = -1
734 };
735
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700736 Object* NewInstance() {
737 return Heap::AllocObject(this, this->object_size_);
738 }
739
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700740 Class* GetSuperClass() const {
741 return super_class_;
742 }
743
744 uint32_t GetSuperClassIdx() const {
745 return super_class_idx_;
746 }
747
748 bool HasSuperClass() const {
749 return super_class_ != NULL;
750 }
751
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700752 bool IsAssignableFrom(const Class* klass) const {
753 DCHECK(klass != NULL);
754 if (this == klass) {
755 return true;
756 }
757 if (IsInterface()) {
758 return klass->Implements(this);
759 }
760 if (klass->IsArray()) {
761 return IsAssignableFromArray(klass);
762 }
763 return klass->IsSubClass(this);
764 }
765
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700766 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700767 return class_loader_;
768 }
769
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700770 DexCache* GetDexCache() const {
771 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700772 }
773
774 Class* GetComponentType() const {
775 return component_type_;
776 }
777
778 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700779 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700780 return descriptor_;
781 }
782
783 Status GetStatus() const {
784 return status_;
785 }
786
787 void SetStatus(Status new_status) {
788 // TODO: validate transition
789 status_ = new_status;
790 }
791
Carl Shapiro69759ea2011-07-21 18:13:35 -0700792 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700793 bool IsErroneous() const {
794 return GetStatus() == kStatusError;
795 }
796
Carl Shapiro69759ea2011-07-21 18:13:35 -0700797 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700798 bool IsVerified() const {
799 return GetStatus() >= kStatusVerified;
800 }
801
Carl Shapiro69759ea2011-07-21 18:13:35 -0700802 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700803 bool IsLinked() const {
804 return GetStatus() >= kStatusResolved;
805 }
806
Carl Shapiro69759ea2011-07-21 18:13:35 -0700807 bool IsLoaded() const {
808 return GetStatus() >= kStatusLoaded;
809 }
810
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700811 // Returns true if this class is in the same packages as that class.
812 bool IsInSamePackage(const Class* that) const;
813
Ian Rogersb033c752011-07-20 12:22:35 -0700814 static bool IsInSamePackage(const StringPiece& descriptor1,
815 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700816
817 // Returns true if this class represents an array class.
818 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700819 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700820 }
821
822 // Returns true if the class is an interface.
823 bool IsInterface() const {
824 return (access_flags_ & kAccInterface) != 0;
825 }
826
827 // Returns true if the class is declared public.
828 bool IsPublic() const {
829 return (access_flags_ & kAccPublic) != 0;
830 }
831
832 // Returns true if the class is declared final.
833 bool IsFinal() const {
834 return (access_flags_ & kAccFinal) != 0;
835 }
836
837 // Returns true if the class is abstract.
838 bool IsAbstract() const {
839 return (access_flags_ & kAccAbstract) != 0;
840 }
841
842 // Returns true if the class is an annotation.
843 bool IsAnnotation() const {
844 return (access_flags_ & kAccAnnotation) != 0;
845 }
846
847 // Returns true if the class is a primitive type.
848 bool IsPrimitive() const {
849 return primitive_type_ != kPrimNot;
850 }
851
Brian Carlstromae3ac012011-07-27 01:30:28 -0700852 // Returns true if the class is synthetic.
853 bool IsSynthetic() const {
854 return (access_flags_ & kAccSynthetic) != 0;
855 }
856
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700857 // Returns true if this class can access that class.
858 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700859 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700860 }
861
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700862 // Returns the number of static, private, and constructor methods.
863 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700864 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700865 }
866
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700867 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700868 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700869 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700870 }
871
872 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700873 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700874 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700875 }
876
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700877 Method* FindDeclaredDirectMethod(const StringPiece& name,
878 const StringPiece& descriptor);
879
880 Method* FindDirectMethod(const StringPiece& name,
881 const StringPiece& descriptor);
882
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700883 // Returns the number of non-inherited virtual methods.
884 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700885 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700886 }
887
888 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700889 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700890 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700891 }
892
893 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700894 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700895 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700896 }
897
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700898 Method* FindDeclaredVirtualMethod(const StringPiece& name,
899 const StringPiece& descriptor);
900
901 Method* FindVirtualMethod(const StringPiece& name,
902 const StringPiece& descriptor);
903
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700904 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700905 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700906 }
907
Carl Shapiro69759ea2011-07-21 18:13:35 -0700908 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700909 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700910 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700911 }
912
Jesse Wilson35baaab2011-08-10 16:18:03 -0400913 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700914 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700915 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700916 }
917
Jesse Wilson35baaab2011-08-10 16:18:03 -0400918 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700919 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700920 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700921 }
922
923 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700924 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700925 }
926
Jesse Wilson35baaab2011-08-10 16:18:03 -0400927 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700928 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700929 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700930 }
931
Jesse Wilson35baaab2011-08-10 16:18:03 -0400932 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700933 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700934 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700935 }
936
937 uint32_t GetReferenceOffsets() const {
938 return reference_offsets_;
939 }
940
941 void SetReferenceOffsets(uint32_t new_reference_offsets) {
942 reference_offsets_ = new_reference_offsets;
943 }
944
Carl Shapiro69759ea2011-07-21 18:13:35 -0700945 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700946 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700947 }
948
949 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700950 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700951 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700952 }
953
954 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700955 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700956 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700957 }
958
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700959 void SetVerifyErrorClass(Class* klass) {
960 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
961 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
962 klass->SetFieldObject(field_offset, klass);
963 }
964
965 private:
966 bool Implements(const Class* klass) const;
967 bool IsArrayAssignableFromArray(const Class* klass) const;
968 bool IsAssignableFromArray(const Class* klass) const;
969 bool IsSubClass(const Class* klass) const;
970
Ian Rogersb033c752011-07-20 12:22:35 -0700971 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -0700972 // leave space for instance data; we could access fields directly if
973 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700974#define CLASS_FIELD_SLOTS 1
975 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -0700976 uint32_t instance_data_[CLASS_FIELD_SLOTS];
977#undef CLASS_FIELD_SLOTS
978
979 // UTF-8 descriptor for the class from constant pool
980 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700981 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700982
983 // Proxy classes have their descriptor allocated on the native heap.
984 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700985 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700986
987 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700988 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700989
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700990 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -0700991 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700992 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700993
994 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700996
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700997 // If class verify fails, we must return same error on subsequent tries.
998 // Update with SetVerifyErrorClass to ensure a write barrier is used.
999 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001000
1001 // threadId, used to check for recursive <clinit> invocation
1002 uint32_t clinit_thread_id_;
1003
1004 // Total object size; used when allocating storage on gc heap. (For
1005 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001006 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001007
1008 // For array classes, the class object for base element, for
1009 // instanceof/checkcast (for String[][][], this will be String).
1010 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001011 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001012
1013 // For array classes, the number of array dimensions, e.g. int[][]
1014 // is 2. Otherwise 0.
1015 int32_t array_rank_;
1016
1017 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1018 PrimitiveType primitive_type_;
1019
1020 // The superclass, or NULL if this is java.lang.Object or a
1021 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001022 Class* super_class_; // TODO: make an instance field
1023 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001024
1025 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001026 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001027
1028 // initiating class loader list
1029 // NOTE: for classes with low serialNumber, these are unused, and the
1030 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001031 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001032
1033 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001034 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001035 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001036
1037 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001038 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001039
1040 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001041 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001042
1043 // Virtual method table (vtable), for use by "invoke-virtual". The
1044 // vtable from the superclass is copied in, and virtual methods from
1045 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001046 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001047
1048 // Interface table (iftable), one entry per interface supported by
1049 // this class. That means one entry for each interface we support
1050 // directly, indirectly via superclass, or indirectly via
1051 // superinterface. This will be null if neither we nor our
1052 // superclass implement any interfaces.
1053 //
1054 // Why we need this: given "class Foo implements Face", declare
1055 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1056 // is part of the Face interface. We can't easily use a single
1057 // vtable.
1058 //
1059 // For every interface a concrete class implements, we create a list
1060 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001061 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001062 InterfaceEntry* iftable_;
1063
1064 // The interface vtable indices for iftable get stored here. By
1065 // placing them all in a single pool for each class that implements
1066 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067 size_t ifvi_pool_count_;
1068 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001069
1070 // instance fields
1071 //
1072 // These describe the layout of the contents of a
1073 // DataObject-compatible Object. Note that only the fields directly
1074 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001075 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001076 //
1077 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001078 // the beginning of the field list. num_reference_instance_fields_
1079 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001080 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001081
1082 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001083 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001084
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001085 // Bitmap of offsets of ifields.
1086 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001087
1088 // source file name, if known. Otherwise, NULL.
1089 const char* source_file_;
1090
Jesse Wilson7833bd22011-08-09 18:31:44 -04001091 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001092 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001093
1094 // static field storage
1095 //
1096 // Each static field is stored in one of three arrays:
1097 // o references are stored in static_references_
1098 // o doubles and longs are stored in static_64bit_primitives_
1099 // o everything else is in static_32bit_primitives_
1100 // Static fields select their array using their type and their index using the
1101 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1102 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001103 ObjectArray<Object>* static_references_;
1104 IntArray* static_32bit_primitives_;
1105 LongArray* static_64bit_primitives_;
1106
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001107 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001108 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001109};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001110std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001111
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001112inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001113 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001114 DCHECK(klass_ != NULL);
1115 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001116}
1117
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001118
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001119class DataObject : public Object {
1120 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001121 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001122 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001123 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001124};
1125
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001126template<class T>
1127class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001128 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001129 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1130 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1131 length,
1132 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001133 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001134
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001135 const T* GetData() const {
1136 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001137 }
1138
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001139 T* GetData() {
1140 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001141 }
1142
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001143 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001144 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001145 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001146 }
1147
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001148 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001149 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001150 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001151 }
1152
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001153 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001154 // Location of first element.
1155 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001156
1157 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001158};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001159
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001160class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001161 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001162 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001163 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001164 return array_;
1165 }
1166
Carl Shapirof88c9522011-08-06 15:47:38 -07001167 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001168 return hash_code_;
1169 }
1170
Carl Shapirof88c9522011-08-06 15:47:38 -07001171 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001172 return offset_;
1173 }
1174
Carl Shapirof88c9522011-08-06 15:47:38 -07001175 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001176 return count_;
1177 }
1178
Carl Shapirof88c9522011-08-06 15:47:38 -07001179 uint16_t CharAt(uint32_t index) const {
1180 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001181 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001182 }
1183
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001184 static String* AllocFromUtf16(int32_t utf16_length,
1185 uint16_t* utf16_data_in,
1186 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001187 String* string = Alloc(GetJavaLangString(),
1188 GetCharArrayClass(),
1189 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001190 // TODO use 16-bit wide memset variant
1191 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001192 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001193 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001194 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001195 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001196 }
1197
1198 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1199 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001200 int32_t utf16_length,
1201 const char* utf8_data_in) {
1202 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001203 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001204 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001205 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001206 return string;
1207 }
1208
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001209 // Creates a String of the given ASCII characters. It is an error to call this
1210 // using non-ASCII characters as this function assumes one char per byte.
1211 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001212 return AllocFromModifiedUtf8(GetJavaLangString(),
1213 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001214 strlen(ascii_data_in),
1215 ascii_data_in);
1216 }
1217
Jesse Wilson8989d992011-08-02 13:39:42 -07001218 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1219 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001220 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1221 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001222 }
1223
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001224 static void InitClasses(Class* java_lang_String, Class* char_array);
1225
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001226 static String* Alloc(Class* java_lang_String,
1227 Class* char_array,
1228 int32_t utf16_length) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001229 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001230 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1231 string->array_ = array;
1232 string->count_ = utf16_length;
1233 return string;
1234 }
1235
1236 // Convert Modified UTF-8 to UTF-16
1237 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1238 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1239 while (*utf8_data_in != '\0') {
1240 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1241 }
1242 }
1243
1244 // Retrieve the next UTF-16 character from a UTF-8 string.
1245 //
1246 // Advances "*pUtf8Ptr" to the start of the next character.
1247 //
1248 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1249 // of a 3-byte sequence, you can end up overrunning the buffer with
1250 // reads (and possibly with the writes if the length was computed and
1251 // cached before the damage). For performance reasons, this function
1252 // assumes that the string being parsed is known to be valid (e.g., by
1253 // already being verified). Most strings we process here are coming
1254 // out of dex files or other internal translations, so the only real
1255 // risk comes from the JNI NewStringUTF call.
1256 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1257 uint8_t one = *(*utf8_data_in)++;
1258 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001259 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001260 return one;
1261 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001262 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001263 uint8_t two = *(*utf8_data_in)++;
1264 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001265 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001266 return ((one & 0x1f) << 6) |
1267 (two & 0x3f);
1268 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001269 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001270 uint8_t three = *(*utf8_data_in)++;
1271 return ((one & 0x0f) << 12) |
1272 ((two & 0x3f) << 6) |
1273 (three & 0x3f);
1274 }
1275
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001276 // Like "strlen", but for strings encoded with "modified" UTF-8.
1277 //
1278 // The value returned is the number of characters, which may or may not
1279 // be the same as the number of bytes.
1280 //
1281 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1282 // get increment {1-3} from table of 8 values.)
1283 static size_t ModifiedUtf8Len(const char* utf8) {
1284 size_t len = 0;
1285 int ic;
1286 while ((ic = *utf8++) != '\0') {
1287 len++;
1288 if ((ic & 0x80) == 0) {
1289 // one-byte encoding
1290 continue;
1291 }
1292 // two- or three-byte encoding
1293 utf8++;
1294 if ((ic & 0x20) == 0) {
1295 // two-byte encoding
1296 continue;
1297 }
1298 // three-byte encoding
1299 utf8++;
1300 }
1301 return len;
1302 }
1303
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001304 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001305 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1306 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001307 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001308 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001309 }
1310 return hash;
1311 }
1312
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001313 void ComputeHashCode() {
1314 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1315 }
1316
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001317 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001318 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001319 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1320 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001321 return false;
1322 }
1323 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001324 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001325 }
1326
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001327 bool Equals(const StringPiece& modified_utf8) const {
1328 // TODO: do not assume C-string representation.
1329 return Equals(modified_utf8.data());
1330 }
1331
1332 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001333 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001334 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001335 return false;
1336 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001337 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001338 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001339 return false;
1340 }
1341 }
1342 return true;
1343 }
1344
Carl Shapirof88c9522011-08-06 15:47:38 -07001345 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001346 if (this->GetLength() != that_length) {
1347 return false;
1348 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001349 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001350 if (this->CharAt(i) != that_chars[that_offset + i]) {
1351 return false;
1352 }
1353 }
1354 return true;
1355 }
1356
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001357 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001358 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1359 CharArray* array_;
1360
Carl Shapirof88c9522011-08-06 15:47:38 -07001361 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001362
Carl Shapirof88c9522011-08-06 15:47:38 -07001363 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001364
Carl Shapirof88c9522011-08-06 15:47:38 -07001365 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001366
1367 static Class* GetJavaLangString() {
1368 DCHECK(java_lang_String_ != NULL);
1369 return java_lang_String_;
1370 }
1371 static Class* GetCharArrayClass() {
1372 DCHECK(char_array_ != NULL);
1373 return char_array_;
1374 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001375
1376 static Class* java_lang_String_;
1377 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001378
1379 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001380};
1381
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001382class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001383 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001384 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1385 }
1386
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001387 Class* GetClass() const {
1388 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001389 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001390
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001391 void SetClass(Class* klass) {
1392 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001393 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001394
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001395 private:
1396 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001397 Class* klass_;
1398
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001399 public: // TODO: private
1400 // Index into array of vtable offsets. This points into the
1401 // ifviPool, which holds the vtables for all interfaces declared by
1402 // this class.
1403 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001404
1405 private:
1406 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001407};
1408
1409} // namespace art
1410
1411#endif // ART_SRC_OBJECT_H_