blob: cb7f379ea7f1d1ddad754d12fa1833e2c31524cb [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 {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700121 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700122 return klass_;
123 }
124
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700125 bool InstanceOf(const Class* klass) const;
126
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700127 size_t Size() const;
128
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700129 void MonitorEnter() {
130 monitor_->Enter();
131 }
132
133 void MonitorExit() {
134 monitor_->Exit();
135 }
136
137 void Notify() {
138 monitor_->Notify();
139 }
140
141 void NotifyAll() {
142 monitor_->NotifyAll();
143 }
144
145 void Wait() {
146 monitor_->Wait();
147 }
148
149 void Wait(int64_t timeout) {
150 monitor_->Wait(timeout);
151 }
152
153 void Wait(int64_t timeout, int32_t nanos) {
154 monitor_->Wait(timeout, nanos);
155 }
156
Carl Shapiro69759ea2011-07-21 18:13:35 -0700157 const Object* GetFieldObject(size_t field_offset) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700158 Object* that = const_cast<Object*>(this);
159 Object* other = that->GetFieldObject(field_offset);
160 return const_cast<const Object*>(other);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 }
162
163 Object* GetFieldObject(size_t field_offset) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700164 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset;
165 return *reinterpret_cast<Object**>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 }
167
168 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700169 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
170 *reinterpret_cast<Object**>(raw_addr) = new_value;
171 // TODO: write barrier
172 }
173
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700174 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700175
176 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700177 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700178 return down_cast<Class*>(this);
179 }
180
181 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700182 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 return down_cast<const Class*>(this);
184 }
185
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186 bool IsObjectArray() const;
187
188 template<class T>
189 ObjectArray<T>* AsObjectArray() {
190 DCHECK(IsObjectArray());
191 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192 }
193
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700194 template<class T>
195 const ObjectArray<T>* AsObjectArray() const {
196 DCHECK(IsObjectArray());
197 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700198 }
199
200 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700201 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202 return true;
203 }
204
205 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700206 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700207 return true;
208 }
209
210 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700211 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700212 return true;
213 }
214
215 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700216 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700217 return true;
218 }
219
220 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700221 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222 return true;
223 }
224
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700225 bool IsArray() const;
226
227 Array* AsArray() {
228 DCHECK(IsArray());
229 return down_cast<Array*>(this);
230 }
231
232 const Array* AsArray() const {
233 DCHECK(IsArray());
234 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700235 }
236
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700237 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700238 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700240 Monitor* monitor_;
241
242 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700243 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700244};
245
246class ObjectLock {
247 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700248 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249 CHECK(object != NULL);
250 obj_->MonitorEnter();
251 }
252
253 ~ObjectLock() {
254 obj_->MonitorExit();
255 }
256
257 void Wait(int64_t millis = 0) {
258 return obj_->Wait(millis);
259 }
260
261 void Notify() {
262 obj_->Notify();
263 }
264
265 void NotifyAll() {
266 obj_->NotifyAll();
267 }
268
269 private:
270 Object* obj_;
271 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700272};
273
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400274class AccessibleObject : public Object {
275 private:
276 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
277 uint32_t java_flag_;
278};
279
280class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700281 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700282 Class* GetDeclaringClass() const {
283 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700284 }
285
Jesse Wilson14150742011-07-29 19:04:44 -0400286 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700287 return name_;
288 }
289
290 bool IsStatic() const {
291 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700292 }
293
294 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700295 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700296 }
297
Brian Carlstromae3ac012011-07-27 01:30:28 -0700298 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700299 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700300 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700301 }
302
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700303 uint32_t GetOffset() const {
304 return offset_;
305 }
306
307 void SetOffset(size_t num_bytes) {
308 offset_ = num_bytes;
309 }
310
Jesse Wilson35baaab2011-08-10 16:18:03 -0400311 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400312 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400313 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400314 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400315 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400316 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400317 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400318 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400319 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400320 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400321 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400322 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400323 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400324 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400325 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400326 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400327 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400328 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400329 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400330 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700331
Jesse Wilson35baaab2011-08-10 16:18:03 -0400332 public: // TODO: private
333 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
334 // The class in which this field is declared.
335 Class* declaring_class_;
336 Object* generic_type_;
337 uint32_t generic_types_are_initialized_;
338 String* name_;
339 uint32_t offset_;
340 Class* type_;
341
342 // e.g. "I", "[C", "Landroid/os/Debug;"
343 StringPiece descriptor_;
344
345 uint32_t access_flags_;
346
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700347 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400348 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700349};
350
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400351class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700352 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700353 // An function that invokes a method with an array of its arguments.
354 typedef void InvokeStub(Method* method,
355 Object* obj,
356 Thread* thread,
357 byte* args,
358 JValue* result);
359
Brian Carlstromae3ac012011-07-27 01:30:28 -0700360 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700361 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700362 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700363 }
364
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700365 const String* GetDescriptor() const {
366 return descriptor_;
367 }
368
Brian Carlstroma0808032011-07-18 00:39:23 -0700369 Class* GetDeclaringClass() const {
370 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700371 }
372
Ian Rogersb033c752011-07-20 12:22:35 -0700373 static MemberOffset ClassOffset() {
374 return MemberOffset(OFFSETOF_MEMBER(Method, klass_));
375 }
376
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700377 // Returns true if the method is declared public.
378 bool IsPublic() const {
379 return (access_flags_ & kAccPublic) != 0;
380 }
381
382 // Returns true if the method is declared private.
383 bool IsPrivate() const {
384 return (access_flags_ & kAccPrivate) != 0;
385 }
386
387 // Returns true if the method is declared static.
388 bool IsStatic() const {
389 return (access_flags_ & kAccStatic) != 0;
390 }
391
392 // Returns true if the method is declared synchronized.
393 bool IsSynchronized() const {
394 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
395 return (access_flags_ & synchonized) != 0;
396 }
397
398 // Returns true if the method is declared final.
399 bool IsFinal() const {
400 return (access_flags_ & kAccFinal) != 0;
401 }
402
403 // Returns true if the method is declared native.
404 bool IsNative() const {
405 return (access_flags_ & kAccNative) != 0;
406 }
407
408 // Returns true if the method is declared abstract.
409 bool IsAbstract() const {
410 return (access_flags_ & kAccAbstract) != 0;
411 }
412
413 bool IsSynthetic() const {
414 return (access_flags_ & kAccSynthetic) != 0;
415 }
416
417 // Number of argument registers required by the prototype.
418 uint32_t NumArgRegisters();
419
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700420 // Number of argument bytes required for densely packing the
421 // arguments into an array of arguments.
422 size_t NumArgArrayBytes();
423
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700424 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400425 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400426 // the class we are a part of
427 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400428 ObjectArray<Class>* java_exception_types_;
429 Object* java_formal_type_parameters_;
430 Object* java_generic_exception_types_;
431 Object* java_generic_parameter_types_;
432 Object* java_generic_return_type_;
433 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700434 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400435 ObjectArray<Class>* java_parameter_types_;
436 uint32_t java_generic_types_are_initialized_;
437 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700438
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700439 const StringPiece& GetShorty() const {
440 return shorty_;
441 }
442
Ian Rogersb033c752011-07-20 12:22:35 -0700443 bool IsReturnAReference() const {
444 return (shorty_[0] == 'L') || (shorty_[0] == '[');
445 }
446
447 bool IsReturnAFloatOrDouble() const {
448 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
449 }
450
451 bool IsReturnAFloat() const {
452 return shorty_[0] == 'F';
453 }
454
455 bool IsReturnADouble() const {
456 return shorty_[0] == 'D';
457 }
458
459 bool IsReturnALong() const {
460 return shorty_[0] == 'J';
461 }
462
Ian Rogers45a76cb2011-07-21 22:00:15 -0700463 bool IsReturnVoid() const {
464 return shorty_[0] == 'V';
465 }
466
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700467 // "Args" may refer to any of the 3 levels of "Args."
468 // To avoid confusion, our code will denote which "Args" clearly:
469 // 1. UserArgs: Args that a user see.
470 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
471 // receiver.
472 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
473 // E.g., the first in Args is Method* for both static and non-static
474 // methods. And CConvArgs doesn't deal with the receiver because
475 // receiver is hardwired in an implicit register, so CConvArgs doesn't
476 // need to deal with it.
477 //
478 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700479 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700480 // "1 +" because the first in Args is the receiver.
481 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700482 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
483 }
484
485 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700486 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700487 size_t NumReferenceArgs() const;
488
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700489 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700490 size_t NumLongOrDoubleArgs() const;
491
492 // The number of reference arguments to this method before the given
493 // parameter index
494 size_t NumReferenceArgsBefore(unsigned int param) const;
495
496 // Is the given method parameter a reference?
497 bool IsParamAReference(unsigned int param) const;
498
499 // Is the given method parameter a long or double?
500 bool IsParamALongOrDouble(unsigned int param) const;
501
Ian Rogersdf20fe02011-07-20 20:34:16 -0700502 // Size in bytes of the given parameter
503 size_t ParamSize(unsigned int param) const;
504
505 // Size in bytes of the return value
506 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700507
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700508 const void* GetCode() const {
509 return code_;
510 }
511
Ian Rogersb033c752011-07-20 12:22:35 -0700512 void SetCode(const void* code) {
513 code_ = code;
514 }
515
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700516 static size_t GetCodeOffset() {
517 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700518 }
519
520 void RegisterNative(const void* native_method) {
521 native_method_ = native_method;
522 }
523
524 static MemberOffset NativeMethodOffset() {
525 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
526 }
527
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700528 InvokeStub* GetInvokeStub() const {
529 return invoke_stub_;
530 }
531
532 void SetInvokeStub(const InvokeStub* invoke_stub) {
533 invoke_stub_ = invoke_stub;
534 }
535
536 static size_t GetInvokeStubOffset() {
537 return OFFSETOF_MEMBER(Method, invoke_stub_);
538 }
539
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700540 bool HasSameNameAndDescriptor(const Method* that) const;
541
Ian Rogersb033c752011-07-20 12:22:35 -0700542 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700543 // access flags; low 16 bits are defined by spec (could be uint16_t?)
544 uint32_t access_flags_;
545
546 // For concrete virtual methods, this is the offset of the method
547 // in "vtable".
548 //
549 // For abstract methods in an interface class, this is the offset
550 // of the method in "iftable[n]->methodIndexArray".
551 uint16_t method_index_;
552
553 // Method bounds; not needed for an abstract method.
554 //
555 // For a native method, we compute the size of the argument list, and
556 // set "insSize" and "registerSize" equal to it.
557 uint16_t num_registers_; // ins + locals
558 uint16_t num_outs_;
559 uint16_t num_ins_;
560
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700561 // The method descriptor. This represents the parameters a method
562 // takes and value it returns. This string is a list of the type
563 // descriptors for the parameters enclosed in parenthesis followed
564 // by the return type descriptor. For example, for the method
565 //
566 // Object mymethod(int i, double d, Thread t)
567 //
568 // the method descriptor would be
569 //
570 // (IDLjava/lang/Thread;)Ljava/lang/Object;
571 String* descriptor_;
572
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573 // Method prototype descriptor string (return and argument types).
574 uint32_t proto_idx_;
575
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700576 // Offset to the CodeItem.
577 uint32_t code_off_;
578
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700579 // The short-form method descriptor string.
580 StringPiece shorty_;
581
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700582 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700583 // Compiled code associated with this method
584 const void* code_;
585
586 // Any native method registered with this method
587 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700588
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700589 // Native invocation stub entry point.
590 const InvokeStub* invoke_stub_;
591
Carl Shapirof88c9522011-08-06 15:47:38 -0700592 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700593};
594
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700595class Array : public Object {
596 public:
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700597 static size_t Size(size_t component_count,
598 size_t component_size) {
599 return sizeof(Array) + component_count * component_size;
600 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700601 static Array* Alloc(Class* array_class,
602 size_t component_count,
603 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700604 size_t size = Size(component_count, component_size);
605 Array* array = Heap::AllocObject(array_class, size)->AsArray();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700606 if (array != NULL) {
607 array->SetLength(component_count);
608 }
609 return array;
610 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700611
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700612 size_t Size() const;
613
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700614 uint32_t GetLength() const {
615 return length_;
616 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700617
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700618 void SetLength(uint32_t length) {
619 length_ = length;
620 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700621
622 private:
623 // The number of array elements.
624 uint32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400625 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
626 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700627
Carl Shapirof88c9522011-08-06 15:47:38 -0700628 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700629};
630
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700631template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700632class ObjectArray : public Array {
633 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700634 static ObjectArray<T>* Alloc(Class* object_array_class,
635 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700636 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700637 }
638
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700639 T* const * GetData() const {
640 return reinterpret_cast<T* const *>(&elements_);
641 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400642
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700643 T** GetData() {
644 return reinterpret_cast<T**>(&elements_);
645 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400646
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700647 T* Get(uint32_t i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700648 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700649 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700650 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700651
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700652 void Set(uint32_t i, T* object) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -0700653 CHECK_LT(i, GetLength());
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400654 GetData()[i] = object; // TODO: write barrier
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700655 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700656
657 static void Copy(ObjectArray<T>* src, int src_pos,
658 ObjectArray<T>* dst, int dst_pos,
659 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700660 for (size_t i = 0; i < length; i++) {
661 dst->Set(dst_pos + i, src->Get(src_pos + i));
662 }
663 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700664
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700665 ObjectArray<T>* CopyOf(size_t new_length) {
666 ObjectArray<T>* new_array = Alloc(klass_, new_length);
667 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
668 return new_array;
669 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700670
671 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700672 // Location of first element.
673 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700674
675 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700676};
677
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700678// ClassLoader objects.
679class ClassLoader : public Object {
680 public:
681 std::vector<const DexFile*>& GetClassPath() {
682 return class_path_;
683 }
684 void SetClassPath(std::vector<const DexFile*>& class_path) {
685 DCHECK_EQ(0U, class_path_.size());
686 class_path_ = class_path;
687 }
688
689 private:
690 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
691 Object* packages_;
692 ClassLoader* parent_;
693
694 // TODO remove once we can create a real PathClassLoader
695 std::vector<const DexFile*> class_path_;
696
Carl Shapirof88c9522011-08-06 15:47:38 -0700697 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700698};
699
700class BaseDexClassLoader : public ClassLoader {
701 private:
702 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
703 String* original_path_;
704 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700705 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700706};
707
708class PathClassLoader : public BaseDexClassLoader {
709 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700710 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700711};
712
Carl Shapiro1fb86202011-06-27 17:43:13 -0700713// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700714class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700715 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700716
717 // Class Status
718 //
719 // kStatusNotReady: If a Class cannot be found in the class table by
720 // FindClass, it allocates an new one with AllocClass in the
721 // kStatusNotReady and calls LoadClass. Note if it does find a
722 // class, it may not be kStatusResolved and it will try to push it
723 // forward toward kStatusResolved.
724 //
725 // kStatusIdx: LoadClass populates with Class with information from
726 // the DexFile, moving the status to kStatusIdx, indicating that the
727 // Class values in super_class_ and interfaces_ have not been
728 // populated based on super_class_idx_ and interfaces_idx_. The new
729 // Class can then be inserted into the classes table.
730 //
731 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
732 // attempt to move a kStatusIdx class forward to kStatusLoaded by
733 // using ResolveClass to initialize the super_class_ and interfaces_.
734 //
735 // kStatusResolved: Still holding the lock on Class, the ClassLinker
736 // will use LinkClass to link all members, creating Field and Method
737 // objects, setting up the vtable, etc. On success, the class is
738 // marked kStatusResolved.
739
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700740 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700741 kStatusError = -1,
742 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700743 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700744 kStatusLoaded = 2, // DEX idx values resolved
745 kStatusResolved = 3, // part of linking
746 kStatusVerifying = 4, // in the process of being verified
747 kStatusVerified = 5, // logically part of linking; done pre-init
748 kStatusInitializing = 6, // class init in progress
749 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700750 };
751
752 enum PrimitiveType {
753 kPrimNot = -1
754 };
755
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700756 Object* NewInstance() {
757 return Heap::AllocObject(this, this->object_size_);
758 }
759
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700760 Class* GetSuperClass() const {
761 return super_class_;
762 }
763
764 uint32_t GetSuperClassIdx() const {
765 return super_class_idx_;
766 }
767
768 bool HasSuperClass() const {
769 return super_class_ != NULL;
770 }
771
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700772 bool IsAssignableFrom(const Class* klass) const {
773 DCHECK(klass != NULL);
774 if (this == klass) {
775 return true;
776 }
777 if (IsInterface()) {
778 return klass->Implements(this);
779 }
780 if (klass->IsArray()) {
781 return IsAssignableFromArray(klass);
782 }
783 return klass->IsSubClass(this);
784 }
785
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700786 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700787 return class_loader_;
788 }
789
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700790 DexCache* GetDexCache() const {
791 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700792 }
793
794 Class* GetComponentType() const {
795 return component_type_;
796 }
797
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700798 size_t GetComponentSize() const {
799 switch (component_type_->descriptor_[0]) {
800 case 'B': return 1; // byte
801 case 'C': return 2; // char
802 case 'D': return 8; // double
803 case 'F': return 4; // float
804 case 'I': return 4; // int
805 case 'J': return 8; // long
806 case 'S': return 2; // short
807 case 'Z': return 1; // boolean
808 case 'L': return sizeof(Object*);
809 case '[': return sizeof(Array*);
810 default:
811 LOG(ERROR) << "Unknown component type " << component_type_->descriptor_;
812 return 0;
813 }
814 }
815
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700816 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700817 DCHECK_NE(0, descriptor_.size());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700818 return descriptor_;
819 }
820
821 Status GetStatus() const {
822 return status_;
823 }
824
825 void SetStatus(Status new_status) {
826 // TODO: validate transition
827 status_ = new_status;
828 }
829
Carl Shapiro69759ea2011-07-21 18:13:35 -0700830 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700831 bool IsErroneous() const {
832 return GetStatus() == kStatusError;
833 }
834
Carl Shapiro69759ea2011-07-21 18:13:35 -0700835 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700836 bool IsVerified() const {
837 return GetStatus() >= kStatusVerified;
838 }
839
Carl Shapiro69759ea2011-07-21 18:13:35 -0700840 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700841 bool IsLinked() const {
842 return GetStatus() >= kStatusResolved;
843 }
844
Carl Shapiro69759ea2011-07-21 18:13:35 -0700845 bool IsLoaded() const {
846 return GetStatus() >= kStatusLoaded;
847 }
848
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700849 // Returns true if this class is in the same packages as that class.
850 bool IsInSamePackage(const Class* that) const;
851
Ian Rogersb033c752011-07-20 12:22:35 -0700852 static bool IsInSamePackage(const StringPiece& descriptor1,
853 const StringPiece& descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700854
855 // Returns true if this class represents an array class.
856 bool IsArray() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700857 return GetDescriptor()[0] == '['; // TODO: avoid parsing the descriptor
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700858 }
859
860 // Returns true if the class is an interface.
861 bool IsInterface() const {
862 return (access_flags_ & kAccInterface) != 0;
863 }
864
865 // Returns true if the class is declared public.
866 bool IsPublic() const {
867 return (access_flags_ & kAccPublic) != 0;
868 }
869
870 // Returns true if the class is declared final.
871 bool IsFinal() const {
872 return (access_flags_ & kAccFinal) != 0;
873 }
874
875 // Returns true if the class is abstract.
876 bool IsAbstract() const {
877 return (access_flags_ & kAccAbstract) != 0;
878 }
879
880 // Returns true if the class is an annotation.
881 bool IsAnnotation() const {
882 return (access_flags_ & kAccAnnotation) != 0;
883 }
884
885 // Returns true if the class is a primitive type.
886 bool IsPrimitive() const {
887 return primitive_type_ != kPrimNot;
888 }
889
Brian Carlstromae3ac012011-07-27 01:30:28 -0700890 // Returns true if the class is synthetic.
891 bool IsSynthetic() const {
892 return (access_flags_ & kAccSynthetic) != 0;
893 }
894
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700895 // Returns true if this class can access that class.
896 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700897 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700898 }
899
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700900 // Returns the number of static, private, and constructor methods.
901 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700902 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700903 }
904
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700905 Method* GetDirectMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700906 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700907 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700908 }
909
910 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700911 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700912 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700913 }
914
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700915 Method* FindDeclaredDirectMethod(const StringPiece& name,
916 const StringPiece& descriptor);
917
918 Method* FindDirectMethod(const StringPiece& name,
919 const StringPiece& descriptor);
920
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700921 // Returns the number of non-inherited virtual methods.
922 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700923 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700924 }
925
926 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700927 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700928 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700929 }
930
931 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700932 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700933 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700934 }
935
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700936 Method* FindDeclaredVirtualMethod(const StringPiece& name,
937 const StringPiece& descriptor);
938
939 Method* FindVirtualMethod(const StringPiece& name,
940 const StringPiece& descriptor);
941
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700942 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700943 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700944 }
945
Carl Shapiro69759ea2011-07-21 18:13:35 -0700946 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700947 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700948 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 }
950
Jesse Wilson35baaab2011-08-10 16:18:03 -0400951 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700952 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700953 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700954 }
955
Jesse Wilson35baaab2011-08-10 16:18:03 -0400956 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700957 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700958 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700959 }
960
961 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700962 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700963 }
964
Jesse Wilson35baaab2011-08-10 16:18:03 -0400965 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700966 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700967 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700968 }
969
Jesse Wilson35baaab2011-08-10 16:18:03 -0400970 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700971 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700972 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973 }
974
975 uint32_t GetReferenceOffsets() const {
976 return reference_offsets_;
977 }
978
979 void SetReferenceOffsets(uint32_t new_reference_offsets) {
980 reference_offsets_ = new_reference_offsets;
981 }
982
Carl Shapiro69759ea2011-07-21 18:13:35 -0700983 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700984 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700985 }
986
987 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700988 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700989 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700990 }
991
992 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700993 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700994 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700995 }
996
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700997 void SetVerifyErrorClass(Class* klass) {
998 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
999 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1000 klass->SetFieldObject(field_offset, klass);
1001 }
1002
1003 private:
1004 bool Implements(const Class* klass) const;
1005 bool IsArrayAssignableFromArray(const Class* klass) const;
1006 bool IsAssignableFromArray(const Class* klass) const;
1007 bool IsSubClass(const Class* klass) const;
1008
Ian Rogersb033c752011-07-20 12:22:35 -07001009 public: // TODO: private
Carl Shapiro1fb86202011-06-27 17:43:13 -07001010 // leave space for instance data; we could access fields directly if
1011 // we freeze the definition of java/lang/Class
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001012#define CLASS_FIELD_SLOTS 1
1013 // Class.#0 name
Carl Shapiro1fb86202011-06-27 17:43:13 -07001014 uint32_t instance_data_[CLASS_FIELD_SLOTS];
1015#undef CLASS_FIELD_SLOTS
1016
1017 // UTF-8 descriptor for the class from constant pool
1018 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001019 StringPiece descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001020
1021 // Proxy classes have their descriptor allocated on the native heap.
1022 // When this field is non-NULL it must be explicitly freed.
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001023 std::string* descriptor_alloc_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001024
1025 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001026 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001027
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001028 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001029 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001030 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001031
1032 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001033 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001034
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001035 // If class verify fails, we must return same error on subsequent tries.
1036 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1037 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001038
1039 // threadId, used to check for recursive <clinit> invocation
1040 uint32_t clinit_thread_id_;
1041
1042 // Total object size; used when allocating storage on gc heap. (For
1043 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001044 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001045
1046 // For array classes, the class object for base element, for
1047 // instanceof/checkcast (for String[][][], this will be String).
1048 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001049 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001050
1051 // For array classes, the number of array dimensions, e.g. int[][]
1052 // is 2. Otherwise 0.
1053 int32_t array_rank_;
1054
1055 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1056 PrimitiveType primitive_type_;
1057
1058 // The superclass, or NULL if this is java.lang.Object or a
1059 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001060 Class* super_class_; // TODO: make an instance field
1061 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001062
1063 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001064 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001065
1066 // initiating class loader list
1067 // NOTE: for classes with low serialNumber, these are unused, and the
1068 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001069 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001070
1071 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001072 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001073 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001074
1075 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001076 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001077
1078 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001079 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001080
1081 // Virtual method table (vtable), for use by "invoke-virtual". The
1082 // vtable from the superclass is copied in, and virtual methods from
1083 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001084 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001085
1086 // Interface table (iftable), one entry per interface supported by
1087 // this class. That means one entry for each interface we support
1088 // directly, indirectly via superclass, or indirectly via
1089 // superinterface. This will be null if neither we nor our
1090 // superclass implement any interfaces.
1091 //
1092 // Why we need this: given "class Foo implements Face", declare
1093 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1094 // is part of the Face interface. We can't easily use a single
1095 // vtable.
1096 //
1097 // For every interface a concrete class implements, we create a list
1098 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001099 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001100 InterfaceEntry* iftable_;
1101
1102 // The interface vtable indices for iftable get stored here. By
1103 // placing them all in a single pool for each class that implements
1104 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001105 size_t ifvi_pool_count_;
1106 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001107
1108 // instance fields
1109 //
1110 // These describe the layout of the contents of a
1111 // DataObject-compatible Object. Note that only the fields directly
1112 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001113 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001114 //
1115 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001116 // the beginning of the field list. num_reference_instance_fields_
1117 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001118 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001119
1120 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001121 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001122
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001123 // Bitmap of offsets of ifields.
1124 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001125
1126 // source file name, if known. Otherwise, NULL.
1127 const char* source_file_;
1128
Jesse Wilson7833bd22011-08-09 18:31:44 -04001129 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001130 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001131
1132 // static field storage
1133 //
1134 // Each static field is stored in one of three arrays:
1135 // o references are stored in static_references_
1136 // o doubles and longs are stored in static_64bit_primitives_
1137 // o everything else is in static_32bit_primitives_
1138 // Static fields select their array using their type and their index using the
1139 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1140 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001141 ObjectArray<Object>* static_references_;
1142 IntArray* static_32bit_primitives_;
1143 LongArray* static_64bit_primitives_;
1144
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001145 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001146 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001147};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001148std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001149
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001150inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001151 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001152 DCHECK(klass_ != NULL);
1153 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001154}
1155
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001156inline bool Object::IsClass() const {
1157 return klass_ == klass_->klass_;
1158}
1159
1160inline bool Object::IsObjectArray() const {
1161 return IsArray() && !klass_->component_type_->IsPrimitive();
1162}
1163
1164inline bool Object::IsArray() const {
1165 return klass_->IsArray();
1166}
1167
1168inline size_t Object::Size() const {
1169 if (IsArray()) {
1170 return AsArray()->Size();
1171 }
1172 return klass_->object_size_;
1173}
1174
1175inline size_t Array::Size() const {
1176 return Size(GetLength(), klass_->GetComponentSize());
1177}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001178
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001179class DataObject : public Object {
1180 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001181 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001182 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001183 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001184};
1185
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001186template<class T>
1187class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001188 public:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001189 static PrimitiveArray<T>* Alloc(Class* element_class, size_t length) {
1190 return down_cast<PrimitiveArray<T>*>(Array::Alloc(element_class,
1191 length,
1192 sizeof(T)));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001193 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001194
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001195 const T* GetData() const {
1196 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001197 }
1198
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001199 T* GetData() {
1200 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001201 }
1202
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001203 T Get(T i) const {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001204 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001205 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001206 }
1207
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001208 void Set(uint32_t i, T value) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001209 CHECK_LT(i, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001210 GetData()[i] = value;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001211 }
1212
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001213 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001214 // Location of first element.
1215 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001216
1217 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001218};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001219
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001220class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001221 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001222 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001223 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001224 return array_;
1225 }
1226
Carl Shapirof88c9522011-08-06 15:47:38 -07001227 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001228 return hash_code_;
1229 }
1230
Carl Shapirof88c9522011-08-06 15:47:38 -07001231 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001232 return offset_;
1233 }
1234
Carl Shapirof88c9522011-08-06 15:47:38 -07001235 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001236 return count_;
1237 }
1238
Carl Shapirof88c9522011-08-06 15:47:38 -07001239 uint16_t CharAt(uint32_t index) const {
1240 DCHECK_LE(index, GetLength());
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001241 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001242 }
1243
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001244 static String* AllocFromUtf16(int32_t utf16_length,
1245 uint16_t* utf16_data_in,
1246 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001247 String* string = Alloc(GetJavaLangString(),
1248 GetCharArrayClass(),
1249 utf16_length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001250 // TODO use 16-bit wide memset variant
1251 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001252 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001253 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001254 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001255 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001256 }
1257
1258 static String* AllocFromModifiedUtf8(Class* java_lang_String,
1259 Class* char_array,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001260 int32_t utf16_length,
1261 const char* utf8_data_in) {
1262 String* string = Alloc(java_lang_String, char_array, utf16_length);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001263 uint16_t* utf16_data_out = string->array_->GetData();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001264 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001265 string->ComputeHashCode();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001266 return string;
1267 }
1268
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001269 // Creates a String of the given ASCII characters. It is an error to call this
1270 // using non-ASCII characters as this function assumes one char per byte.
1271 static String* AllocFromAscii(const char* ascii_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001272 return AllocFromModifiedUtf8(GetJavaLangString(),
1273 GetCharArrayClass(),
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001274 strlen(ascii_data_in),
1275 ascii_data_in);
1276 }
1277
Jesse Wilson8989d992011-08-02 13:39:42 -07001278 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1279 const char* utf8_data_in) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001280 return AllocFromModifiedUtf8(GetJavaLangString(), GetCharArrayClass(),
1281 utf16_length, utf8_data_in);
Jesse Wilson8989d992011-08-02 13:39:42 -07001282 }
1283
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001284 static void InitClasses(Class* java_lang_String, Class* char_array);
1285
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001286 static String* Alloc(Class* java_lang_String,
1287 Class* char_array,
1288 int32_t utf16_length) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001289 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001290 CharArray* array = CharArray::Alloc(char_array, utf16_length);
1291 string->array_ = array;
1292 string->count_ = utf16_length;
1293 return string;
1294 }
1295
1296 // Convert Modified UTF-8 to UTF-16
1297 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1298 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1299 while (*utf8_data_in != '\0') {
1300 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1301 }
1302 }
1303
1304 // Retrieve the next UTF-16 character from a UTF-8 string.
1305 //
1306 // Advances "*pUtf8Ptr" to the start of the next character.
1307 //
1308 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1309 // of a 3-byte sequence, you can end up overrunning the buffer with
1310 // reads (and possibly with the writes if the length was computed and
1311 // cached before the damage). For performance reasons, this function
1312 // assumes that the string being parsed is known to be valid (e.g., by
1313 // already being verified). Most strings we process here are coming
1314 // out of dex files or other internal translations, so the only real
1315 // risk comes from the JNI NewStringUTF call.
1316 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1317 uint8_t one = *(*utf8_data_in)++;
1318 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001319 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001320 return one;
1321 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001322 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001323 uint8_t two = *(*utf8_data_in)++;
1324 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001325 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001326 return ((one & 0x1f) << 6) |
1327 (two & 0x3f);
1328 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001329 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001330 uint8_t three = *(*utf8_data_in)++;
1331 return ((one & 0x0f) << 12) |
1332 ((two & 0x3f) << 6) |
1333 (three & 0x3f);
1334 }
1335
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001336 // Like "strlen", but for strings encoded with "modified" UTF-8.
1337 //
1338 // The value returned is the number of characters, which may or may not
1339 // be the same as the number of bytes.
1340 //
1341 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1342 // get increment {1-3} from table of 8 values.)
1343 static size_t ModifiedUtf8Len(const char* utf8) {
1344 size_t len = 0;
1345 int ic;
1346 while ((ic = *utf8++) != '\0') {
1347 len++;
1348 if ((ic & 0x80) == 0) {
1349 // one-byte encoding
1350 continue;
1351 }
1352 // two- or three-byte encoding
1353 utf8++;
1354 if ((ic & 0x20) == 0) {
1355 // two-byte encoding
1356 continue;
1357 }
1358 // three-byte encoding
1359 utf8++;
1360 }
1361 return len;
1362 }
1363
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001364 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001365 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1366 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001367 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001368 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001369 }
1370 return hash;
1371 }
1372
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001373 void ComputeHashCode() {
1374 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1375 }
1376
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001377 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001378 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001379 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1380 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001381 return false;
1382 }
1383 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001384 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001385 }
1386
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001387 bool Equals(const StringPiece& modified_utf8) const {
1388 // TODO: do not assume C-string representation.
1389 return Equals(modified_utf8.data());
1390 }
1391
1392 bool Equals(const String* that) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001393 // TODO short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001394 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001395 return false;
1396 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001397 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001398 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001399 return false;
1400 }
1401 }
1402 return true;
1403 }
1404
Carl Shapirof88c9522011-08-06 15:47:38 -07001405 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001406 if (this->GetLength() != that_length) {
1407 return false;
1408 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001409 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001410 if (this->CharAt(i) != that_chars[that_offset + i]) {
1411 return false;
1412 }
1413 }
1414 return true;
1415 }
1416
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001417 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001418 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1419 CharArray* array_;
1420
Carl Shapirof88c9522011-08-06 15:47:38 -07001421 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001422
Carl Shapirof88c9522011-08-06 15:47:38 -07001423 uint32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001424
Carl Shapirof88c9522011-08-06 15:47:38 -07001425 uint32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001426
1427 static Class* GetJavaLangString() {
1428 DCHECK(java_lang_String_ != NULL);
1429 return java_lang_String_;
1430 }
1431 static Class* GetCharArrayClass() {
1432 DCHECK(char_array_ != NULL);
1433 return char_array_;
1434 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001435
1436 static Class* java_lang_String_;
1437 static Class* char_array_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001438
1439 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001440};
1441
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001442class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001443 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001444 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1445 }
1446
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001447 Class* GetClass() const {
1448 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001449 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001450
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001451 void SetClass(Class* klass) {
1452 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001453 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001454
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001455 private:
1456 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001457 Class* klass_;
1458
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001459 public: // TODO: private
1460 // Index into array of vtable offsets. This points into the
1461 // ifviPool, which holds the vtables for all interfaces declared by
1462 // this class.
1463 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001464
1465 private:
1466 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001467};
1468
1469} // namespace art
1470
1471#endif // ART_SRC_OBJECT_H_