blob: c8ec9cb88241481e755b21bd946e981ed902bfc3 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "constants.h"
7#include "casts.h"
8#include "globals.h"
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07009#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "logging.h"
11#include "macros.h"
12#include "offsets.h"
13#include "stringpiece.h"
14#include "monitor.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070015
16namespace art {
17
18class Array;
19class Class;
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070020class DexCache;
Jesse Wilson35baaab2011-08-10 16:18:03 -040021class Field;
Carl Shapiro1fb86202011-06-27 17:43:13 -070022class InterfaceEntry;
23class Monitor;
24class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070025class Object;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -040026class String;
Brian Carlstrom4a96b602011-07-26 16:40:23 -070027template<class T> class ObjectArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070028template<class T> class PrimitiveArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070029typedef PrimitiveArray<uint8_t> BooleanArray;
30typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070031typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070032typedef PrimitiveArray<double> DoubleArray;
33typedef PrimitiveArray<float> FloatArray;
34typedef PrimitiveArray<int32_t> IntArray;
35typedef PrimitiveArray<int64_t> LongArray;
36typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070037
Carl Shapiro3ee755d2011-06-28 12:11:04 -070038union JValue {
39 uint8_t z;
40 int8_t b;
41 uint16_t c;
42 int16_t s;
43 int32_t i;
44 int64_t j;
45 float f;
46 double d;
47 Object* l;
48};
49
Brian Carlstrombe977852011-07-19 14:54:54 -070050static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
51static const uint32_t kAccPrivate = 0x0002; // field, method, ic
52static const uint32_t kAccProtected = 0x0004; // field, method, ic
53static const uint32_t kAccStatic = 0x0008; // field, method, ic
54static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
55static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
56static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
57static const uint32_t kAccVolatile = 0x0040; // field
58static const uint32_t kAccBridge = 0x0040; // method (1.5)
59static const uint32_t kAccTransient = 0x0080; // field
60static const uint32_t kAccVarargs = 0x0080; // method (1.5)
61static const uint32_t kAccNative = 0x0100; // method
62static const uint32_t kAccInterface = 0x0200; // class, ic
63static const uint32_t kAccAbstract = 0x0400; // class, method, ic
64static const uint32_t kAccStrict = 0x0800; // method
65static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
66static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
67static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070068
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069static const uint32_t kAccMiranda = 0x8000; // method
70
Brian Carlstroma331b3c2011-07-18 17:47:56 -070071static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
72
Brian Carlstrombe977852011-07-19 14:54:54 -070073static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
74static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070075
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070076/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070077 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070078 */
79/*
80 * A magic value for refOffsets. Ignore the bits and walk the super
81 * chain when this is the value.
82 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
83 * fields followed by 2 ref instance fields.]
84 */
85#define CLASS_WALK_SUPER ((unsigned int)(3))
86#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
87#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
88#define CLASS_OFFSET_ALIGNMENT 4
89#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
90/*
91 * Given an offset, return the bit number which would encode that offset.
92 * Local use only.
93 */
94#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
95 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
96 CLASS_OFFSET_ALIGNMENT)
97/*
98 * Is the given offset too large to be encoded?
99 */
100#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
101 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
102/*
103 * Return a single bit, encoding the offset.
104 * Undefined if the offset is too large, as defined above.
105 */
106#define CLASS_BIT_FROM_OFFSET(byteOffset) \
107 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
108/*
109 * Return an offset, given a bit number as returned from CLZ.
110 */
111#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700112 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700113
114
Carl Shapiro1fb86202011-06-27 17:43:13 -0700115class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700116 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700117 static bool InstanceOf(const Object* object, const Class* klass) {
118 if (object == NULL) {
119 return false;
120 }
121 return object->InstanceOf(klass);
122 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700123
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700124 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700125 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126 return klass_;
127 }
128
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700129 bool InstanceOf(const Class* klass) const;
130
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700131 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700132
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700133 void MonitorEnter() {
134 monitor_->Enter();
135 }
136
137 void MonitorExit() {
138 monitor_->Exit();
139 }
140
141 void Notify() {
142 monitor_->Notify();
143 }
144
145 void NotifyAll() {
146 monitor_->NotifyAll();
147 }
148
149 void Wait() {
150 monitor_->Wait();
151 }
152
153 void Wait(int64_t timeout) {
154 monitor_->Wait(timeout);
155 }
156
157 void Wait(int64_t timeout, int32_t nanos) {
158 monitor_->Wait(timeout, nanos);
159 }
160
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161 const Object* GetFieldObject(size_t field_offset) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700162 Object* that = const_cast<Object*>(this);
163 Object* other = that->GetFieldObject(field_offset);
164 return const_cast<const Object*>(other);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700165 }
166
167 Object* GetFieldObject(size_t field_offset) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700168 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset;
169 return *reinterpret_cast<Object**>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700170 }
171
172 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700173 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
174 *reinterpret_cast<Object**>(raw_addr) = new_value;
175 // TODO: write barrier
176 }
177
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700178 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700179
180 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700181 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700182 return down_cast<Class*>(this);
183 }
184
185 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700186 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700187 return down_cast<const Class*>(this);
188 }
189
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700190 bool IsObjectArray() const;
191
192 template<class T>
193 ObjectArray<T>* AsObjectArray() {
194 DCHECK(IsObjectArray());
195 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700196 }
197
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700198 template<class T>
199 const ObjectArray<T>* AsObjectArray() const {
200 DCHECK(IsObjectArray());
201 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700202 }
203
204 bool IsReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700205 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206 return true;
207 }
208
209 bool IsWeakReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700210 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700211 return true;
212 }
213
214 bool IsSoftReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700215 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700216 return true;
217 }
218
219 bool IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700220 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700221 return true;
222 }
223
224 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700225 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700226 return true;
227 }
228
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700229 bool IsArray() const;
230
231 Array* AsArray() {
232 DCHECK(IsArray());
233 return down_cast<Array*>(this);
234 }
235
236 const Array* AsArray() const {
237 DCHECK(IsArray());
238 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700239 }
240
Brian Carlstroma663ea52011-08-19 23:33:41 -0700241 bool IsString() const;
242
243 String* AsString() {
244 DCHECK(IsString());
245 return down_cast<String*>(this);
246 }
247
248 bool IsMethod() const;
249
250 Method* AsMethod() {
251 DCHECK(IsMethod());
252 return down_cast<Method*>(this);
253 }
254
255 bool IsField() const;
256
257 Field* AsField() {
258 DCHECK(IsField());
259 return down_cast<Field*>(this);
260 }
261
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700262 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700263 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700264
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700265 Monitor* monitor_;
266
267 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700268 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700269};
270
271class ObjectLock {
272 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700273 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700274 CHECK(object != NULL);
275 obj_->MonitorEnter();
276 }
277
278 ~ObjectLock() {
279 obj_->MonitorExit();
280 }
281
282 void Wait(int64_t millis = 0) {
283 return obj_->Wait(millis);
284 }
285
286 void Notify() {
287 obj_->Notify();
288 }
289
290 void NotifyAll() {
291 obj_->NotifyAll();
292 }
293
294 private:
295 Object* obj_;
296 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700297};
298
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400299class AccessibleObject : public Object {
300 private:
301 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
302 uint32_t java_flag_;
303};
304
305class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700306 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700307 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700308 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700309 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700310 }
311
Jesse Wilson14150742011-07-29 19:04:44 -0400312 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700313 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700314 return name_;
315 }
316
317 bool IsStatic() const {
318 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700319 }
320
321 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700322 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700323 }
324
Brian Carlstromae3ac012011-07-27 01:30:28 -0700325 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700326 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700327 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700328 }
329
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700330 uint32_t GetOffset() const {
331 return offset_;
332 }
333
334 void SetOffset(size_t num_bytes) {
335 offset_ = num_bytes;
336 }
337
Jesse Wilson35baaab2011-08-10 16:18:03 -0400338 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400339 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400340 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400341 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400342 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400343 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400344 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400345 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400346 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400347 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400348 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400349 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400350 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400351 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400352 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400353 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400354 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400355 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400356 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400357 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700358
Jesse Wilson35baaab2011-08-10 16:18:03 -0400359 public: // TODO: private
360 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
361 // The class in which this field is declared.
362 Class* declaring_class_;
363 Object* generic_type_;
364 uint32_t generic_types_are_initialized_;
365 String* name_;
366 uint32_t offset_;
367 Class* type_;
368
369 // e.g. "I", "[C", "Landroid/os/Debug;"
370 StringPiece descriptor_;
371
372 uint32_t access_flags_;
373
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700374 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400375 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700376};
377
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400378class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700379 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700380 // An function that invokes a method with an array of its arguments.
381 typedef void InvokeStub(Method* method,
382 Object* obj,
383 Thread* thread,
384 byte* args,
385 JValue* result);
386
Brian Carlstromae3ac012011-07-27 01:30:28 -0700387 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700388 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700389 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700390 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700391 }
392
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700393 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700394 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700395 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700396 }
397
Brian Carlstroma0808032011-07-18 00:39:23 -0700398 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700399 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700400 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700401 }
402
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700403 static MemberOffset DeclaringClassOffset() {
404 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700405 }
406
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700407 // Returns true if the method is declared public.
408 bool IsPublic() const {
409 return (access_flags_ & kAccPublic) != 0;
410 }
411
412 // Returns true if the method is declared private.
413 bool IsPrivate() const {
414 return (access_flags_ & kAccPrivate) != 0;
415 }
416
417 // Returns true if the method is declared static.
418 bool IsStatic() const {
419 return (access_flags_ & kAccStatic) != 0;
420 }
421
422 // Returns true if the method is declared synchronized.
423 bool IsSynchronized() const {
424 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
425 return (access_flags_ & synchonized) != 0;
426 }
427
428 // Returns true if the method is declared final.
429 bool IsFinal() const {
430 return (access_flags_ & kAccFinal) != 0;
431 }
432
433 // Returns true if the method is declared native.
434 bool IsNative() const {
435 return (access_flags_ & kAccNative) != 0;
436 }
437
438 // Returns true if the method is declared abstract.
439 bool IsAbstract() const {
440 return (access_flags_ & kAccAbstract) != 0;
441 }
442
443 bool IsSynthetic() const {
444 return (access_flags_ & kAccSynthetic) != 0;
445 }
446
447 // Number of argument registers required by the prototype.
448 uint32_t NumArgRegisters();
449
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700450 // Number of argument bytes required for densely packing the
451 // arguments into an array of arguments.
452 size_t NumArgArrayBytes();
453
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700454 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400455 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400456 // the class we are a part of
457 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400458 ObjectArray<Class>* java_exception_types_;
459 Object* java_formal_type_parameters_;
460 Object* java_generic_exception_types_;
461 Object* java_generic_parameter_types_;
462 Object* java_generic_return_type_;
463 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700464 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400465 ObjectArray<Class>* java_parameter_types_;
466 uint32_t java_generic_types_are_initialized_;
467 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700468
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700469 const StringPiece& GetShorty() const {
470 return shorty_;
471 }
472
Ian Rogersb033c752011-07-20 12:22:35 -0700473 bool IsReturnAReference() const {
474 return (shorty_[0] == 'L') || (shorty_[0] == '[');
475 }
476
477 bool IsReturnAFloatOrDouble() const {
478 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
479 }
480
481 bool IsReturnAFloat() const {
482 return shorty_[0] == 'F';
483 }
484
485 bool IsReturnADouble() const {
486 return shorty_[0] == 'D';
487 }
488
489 bool IsReturnALong() const {
490 return shorty_[0] == 'J';
491 }
492
Ian Rogers45a76cb2011-07-21 22:00:15 -0700493 bool IsReturnVoid() const {
494 return shorty_[0] == 'V';
495 }
496
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700497 // "Args" may refer to any of the 3 levels of "Args."
498 // To avoid confusion, our code will denote which "Args" clearly:
499 // 1. UserArgs: Args that a user see.
500 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
501 // receiver.
502 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
503 // E.g., the first in Args is Method* for both static and non-static
504 // methods. And CConvArgs doesn't deal with the receiver because
505 // receiver is hardwired in an implicit register, so CConvArgs doesn't
506 // need to deal with it.
507 //
508 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700509 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700510 // "1 +" because the first in Args is the receiver.
511 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700512 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
513 }
514
515 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700516 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700517 size_t NumReferenceArgs() const;
518
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700519 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700520 size_t NumLongOrDoubleArgs() const;
521
522 // The number of reference arguments to this method before the given
523 // parameter index
524 size_t NumReferenceArgsBefore(unsigned int param) const;
525
526 // Is the given method parameter a reference?
527 bool IsParamAReference(unsigned int param) const;
528
529 // Is the given method parameter a long or double?
530 bool IsParamALongOrDouble(unsigned int param) const;
531
Ian Rogersdf20fe02011-07-20 20:34:16 -0700532 // Size in bytes of the given parameter
533 size_t ParamSize(unsigned int param) const;
534
535 // Size in bytes of the return value
536 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700537
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700538 const void* GetCode() const {
539 return code_;
540 }
541
Ian Rogersb033c752011-07-20 12:22:35 -0700542 void SetCode(const void* code) {
543 code_ = code;
544 }
545
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700546 static size_t GetCodeOffset() {
547 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700548 }
549
550 void RegisterNative(const void* native_method) {
551 native_method_ = native_method;
552 }
553
554 static MemberOffset NativeMethodOffset() {
555 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
556 }
557
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700558 InvokeStub* GetInvokeStub() const {
559 return invoke_stub_;
560 }
561
562 void SetInvokeStub(const InvokeStub* invoke_stub) {
563 invoke_stub_ = invoke_stub;
564 }
565
566 static size_t GetInvokeStubOffset() {
567 return OFFSETOF_MEMBER(Method, invoke_stub_);
568 }
569
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700570 bool HasSameNameAndDescriptor(const Method* that) const;
571
Ian Rogersb033c752011-07-20 12:22:35 -0700572 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700573 // access flags; low 16 bits are defined by spec (could be uint16_t?)
574 uint32_t access_flags_;
575
576 // For concrete virtual methods, this is the offset of the method
577 // in "vtable".
578 //
579 // For abstract methods in an interface class, this is the offset
580 // of the method in "iftable[n]->methodIndexArray".
581 uint16_t method_index_;
582
583 // Method bounds; not needed for an abstract method.
584 //
585 // For a native method, we compute the size of the argument list, and
586 // set "insSize" and "registerSize" equal to it.
587 uint16_t num_registers_; // ins + locals
588 uint16_t num_outs_;
589 uint16_t num_ins_;
590
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700591 // The method descriptor. This represents the parameters a method
592 // takes and value it returns. This string is a list of the type
593 // descriptors for the parameters enclosed in parenthesis followed
594 // by the return type descriptor. For example, for the method
595 //
596 // Object mymethod(int i, double d, Thread t)
597 //
598 // the method descriptor would be
599 //
600 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700601 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700602
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700603 // Method prototype descriptor string (return and argument types).
604 uint32_t proto_idx_;
605
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700606 // Offset to the CodeItem.
607 uint32_t code_off_;
608
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700609 // The short-form method descriptor string.
610 StringPiece shorty_;
611
Brian Carlstromc4fa2c02011-08-21 03:00:12 -0700612 // short cuts to declaring_class_->dex_cache_ members for fast compiled code access
613 ObjectArray<String>* dex_cache_strings_;
614 ObjectArray<Class>* dex_cache_classes_;
615 ObjectArray<Method>* dex_cache_methods_;
616 ObjectArray<Field>* dex_cache_fields_;
617
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700618 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700619 // Compiled code associated with this method
620 const void* code_;
621
622 // Any native method registered with this method
623 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700624
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700625 // Native invocation stub entry point.
626 const InvokeStub* invoke_stub_;
627
Carl Shapirof88c9522011-08-06 15:47:38 -0700628 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700629};
630
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700631class Array : public Object {
632 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700633 static size_t SizeOf(size_t component_count,
634 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700635 return sizeof(Array) + component_count * component_size;
636 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700637
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700638 // A convenience for code that doesn't know the component size,
639 // and doesn't want to have to work it out itself.
640 static Array* Alloc(Class* array_class, size_t component_count);
641
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700642 static Array* Alloc(Class* array_class,
643 size_t component_count,
644 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700645 size_t size = SizeOf(component_count, component_size);
646 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700647 if (array != NULL) {
648 array->SetLength(component_count);
649 }
650 return array;
651 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700652
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700653 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700654
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700655 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700656 return length_;
657 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700658
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700659 void SetLength(uint32_t length) {
660 length_ = length;
661 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700662
Elliott Hughes289da822011-08-16 10:11:20 -0700663 protected:
664 bool IsValidIndex(int32_t index) const {
665 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700666 Thread* self = Thread::Current();
667 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
668 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700669 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700670 }
671 return true;
672 }
673
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700674 private:
675 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700676 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400677 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
678 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700679
Carl Shapirof88c9522011-08-06 15:47:38 -0700680 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700681};
682
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700683template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700684class ObjectArray : public Array {
685 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700686 static ObjectArray<T>* Alloc(Class* object_array_class,
687 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700688 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700689 }
690
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700691 T* const * GetData() const {
692 return reinterpret_cast<T* const *>(&elements_);
693 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400694
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700695 T** GetData() {
696 return reinterpret_cast<T**>(&elements_);
697 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400698
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700699 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700700 if (!IsValidIndex(i)) {
701 return NULL;
702 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700703 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700704 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700705
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700706 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700707 if (IsValidIndex(i)) {
708 // TODO: ArrayStoreException
709 GetData()[i] = object; // TODO: write barrier
710 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700711 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700712
713 static void Copy(ObjectArray<T>* src, int src_pos,
714 ObjectArray<T>* dst, int dst_pos,
715 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700716 for (size_t i = 0; i < length; i++) {
717 dst->Set(dst_pos + i, src->Get(src_pos + i));
718 }
719 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700720
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700721 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700722 ObjectArray<T>* new_array = Alloc(klass_, new_length);
723 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
724 return new_array;
725 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700726
727 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700728 // Location of first element.
729 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700730
731 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700732};
733
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700734// ClassLoader objects.
735class ClassLoader : public Object {
736 public:
737 std::vector<const DexFile*>& GetClassPath() {
738 return class_path_;
739 }
740 void SetClassPath(std::vector<const DexFile*>& class_path) {
741 DCHECK_EQ(0U, class_path_.size());
742 class_path_ = class_path;
743 }
744
745 private:
746 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
747 Object* packages_;
748 ClassLoader* parent_;
749
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700750 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700751 std::vector<const DexFile*> class_path_;
752
Carl Shapirof88c9522011-08-06 15:47:38 -0700753 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700754};
755
756class BaseDexClassLoader : public ClassLoader {
757 private:
758 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
759 String* original_path_;
760 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700761 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700762};
763
764class PathClassLoader : public BaseDexClassLoader {
765 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700766 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700767};
768
Carl Shapiro1fb86202011-06-27 17:43:13 -0700769// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700770class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700771 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700772
773 // Class Status
774 //
775 // kStatusNotReady: If a Class cannot be found in the class table by
776 // FindClass, it allocates an new one with AllocClass in the
777 // kStatusNotReady and calls LoadClass. Note if it does find a
778 // class, it may not be kStatusResolved and it will try to push it
779 // forward toward kStatusResolved.
780 //
781 // kStatusIdx: LoadClass populates with Class with information from
782 // the DexFile, moving the status to kStatusIdx, indicating that the
783 // Class values in super_class_ and interfaces_ have not been
784 // populated based on super_class_idx_ and interfaces_idx_. The new
785 // Class can then be inserted into the classes table.
786 //
787 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
788 // attempt to move a kStatusIdx class forward to kStatusLoaded by
789 // using ResolveClass to initialize the super_class_ and interfaces_.
790 //
791 // kStatusResolved: Still holding the lock on Class, the ClassLinker
792 // will use LinkClass to link all members, creating Field and Method
793 // objects, setting up the vtable, etc. On success, the class is
794 // marked kStatusResolved.
795
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700796 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700797 kStatusError = -1,
798 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700799 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700800 kStatusLoaded = 2, // DEX idx values resolved
801 kStatusResolved = 3, // part of linking
802 kStatusVerifying = 4, // in the process of being verified
803 kStatusVerified = 5, // logically part of linking; done pre-init
804 kStatusInitializing = 6, // class init in progress
805 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700806 };
807
808 enum PrimitiveType {
809 kPrimNot = -1
810 };
811
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700812 Object* NewInstance() {
813 return Heap::AllocObject(this, this->object_size_);
814 }
815
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700816 Class* GetSuperClass() const {
817 return super_class_;
818 }
819
820 uint32_t GetSuperClassIdx() const {
821 return super_class_idx_;
822 }
823
824 bool HasSuperClass() const {
825 return super_class_ != NULL;
826 }
827
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700828 bool IsAssignableFrom(const Class* klass) const {
829 DCHECK(klass != NULL);
830 if (this == klass) {
831 return true;
832 }
833 if (IsInterface()) {
834 return klass->Implements(this);
835 }
836 if (klass->IsArray()) {
837 return IsAssignableFromArray(klass);
838 }
839 return klass->IsSubClass(this);
840 }
841
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700842 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 return class_loader_;
844 }
845
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700846 DexCache* GetDexCache() const {
847 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700848 }
849
850 Class* GetComponentType() const {
851 return component_type_;
852 }
853
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700854 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700855
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700856 size_t GetComponentSize() const {
857 return GetTypeSize(component_type_->descriptor_);
858 }
859
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700860 const String* GetDescriptor() const {
861 DCHECK(descriptor_ != NULL);
862 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700863 return descriptor_;
864 }
865
866 Status GetStatus() const {
867 return status_;
868 }
869
870 void SetStatus(Status new_status) {
871 // TODO: validate transition
872 status_ = new_status;
873 }
874
Carl Shapiro69759ea2011-07-21 18:13:35 -0700875 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 bool IsErroneous() const {
877 return GetStatus() == kStatusError;
878 }
879
Carl Shapiro69759ea2011-07-21 18:13:35 -0700880 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700881 bool IsVerified() const {
882 return GetStatus() >= kStatusVerified;
883 }
884
Carl Shapiro69759ea2011-07-21 18:13:35 -0700885 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700886 bool IsLinked() const {
887 return GetStatus() >= kStatusResolved;
888 }
889
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700890 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700891 bool IsLoaded() const {
892 return GetStatus() >= kStatusLoaded;
893 }
894
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700895 // Returns true if the class is initialized.
896 bool IsInitialized() const {
897 return GetStatus() == kStatusInitialized;
898 }
899
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700900 // Returns true if this class is in the same packages as that class.
901 bool IsInSamePackage(const Class* that) const;
902
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700903 static bool IsInSamePackage(const String* descriptor1,
904 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700905
906 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700907 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700908
909 // Returns true if the class is an interface.
910 bool IsInterface() const {
911 return (access_flags_ & kAccInterface) != 0;
912 }
913
914 // Returns true if the class is declared public.
915 bool IsPublic() const {
916 return (access_flags_ & kAccPublic) != 0;
917 }
918
919 // Returns true if the class is declared final.
920 bool IsFinal() const {
921 return (access_flags_ & kAccFinal) != 0;
922 }
923
924 // Returns true if the class is abstract.
925 bool IsAbstract() const {
926 return (access_flags_ & kAccAbstract) != 0;
927 }
928
929 // Returns true if the class is an annotation.
930 bool IsAnnotation() const {
931 return (access_flags_ & kAccAnnotation) != 0;
932 }
933
934 // Returns true if the class is a primitive type.
935 bool IsPrimitive() const {
936 return primitive_type_ != kPrimNot;
937 }
938
Brian Carlstromae3ac012011-07-27 01:30:28 -0700939 // Returns true if the class is synthetic.
940 bool IsSynthetic() const {
941 return (access_flags_ & kAccSynthetic) != 0;
942 }
943
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700944 // Returns true if this class can access that class.
945 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700946 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700947 }
948
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700949 // Returns the number of static, private, and constructor methods.
950 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700951 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700952 }
953
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700954 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700955 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700956 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700957 }
958
959 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700960 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700961 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700962 }
963
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700964 Method* FindDeclaredDirectMethod(const StringPiece& name,
965 const StringPiece& descriptor);
966
967 Method* FindDirectMethod(const StringPiece& name,
968 const StringPiece& descriptor);
969
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700970 // Returns the number of non-inherited virtual methods.
971 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700972 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700973 }
974
975 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700976 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700977 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700978 }
979
980 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700981 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700982 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700983 }
984
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700985 Method* FindDeclaredVirtualMethod(const StringPiece& name,
986 const StringPiece& descriptor);
987
988 Method* FindVirtualMethod(const StringPiece& name,
989 const StringPiece& descriptor);
990
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700991 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700992 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700993 }
994
Carl Shapiro69759ea2011-07-21 18:13:35 -0700995 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700996 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700997 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700998 }
999
Elliott Hughescdf53122011-08-19 15:46:09 -07001000 // Finds the given instance field in this class or a superclass.
1001 Field* FindInstanceField(const StringPiece& name,
1002 const StringPiece& descriptor);
1003
1004 Field* FindDeclaredInstanceField(const StringPiece& name,
1005 const StringPiece& descriptor);
1006
1007 // Finds the given static field in this class or a superclass.
1008 Field* FindStaticField(const StringPiece& name,
1009 const StringPiece& descriptor);
1010
1011 Field* FindDeclaredStaticField(const StringPiece& name,
1012 const StringPiece& descriptor);
1013
Jesse Wilson35baaab2011-08-10 16:18:03 -04001014 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001015 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001016 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001017 }
1018
Jesse Wilson35baaab2011-08-10 16:18:03 -04001019 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001020 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001021 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001022 }
1023
1024 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001025 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001026 }
1027
Jesse Wilson35baaab2011-08-10 16:18:03 -04001028 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001029 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001030 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001031 }
1032
Jesse Wilson35baaab2011-08-10 16:18:03 -04001033 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001034 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001035 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001036 }
1037
1038 uint32_t GetReferenceOffsets() const {
1039 return reference_offsets_;
1040 }
1041
1042 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1043 reference_offsets_ = new_reference_offsets;
1044 }
1045
Carl Shapiro69759ea2011-07-21 18:13:35 -07001046 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001047 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001048 }
1049
1050 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001051 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001052 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001053 }
1054
1055 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001056 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001057 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001058 }
1059
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001060 void SetVerifyErrorClass(Class* klass) {
1061 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1062 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1063 klass->SetFieldObject(field_offset, klass);
1064 }
1065
1066 private:
1067 bool Implements(const Class* klass) const;
1068 bool IsArrayAssignableFromArray(const Class* klass) const;
1069 bool IsAssignableFromArray(const Class* klass) const;
1070 bool IsSubClass(const Class* klass) const;
1071
Ian Rogersb033c752011-07-20 12:22:35 -07001072 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001073 // descriptor for the class such as "java.lang.Class" or "[C"
1074 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001075
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001076 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1077 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001078
1079 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001080 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001081
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001082 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001083 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001084 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001085
1086 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001087 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001088
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001089 // If class verify fails, we must return same error on subsequent tries.
1090 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1091 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001092
1093 // threadId, used to check for recursive <clinit> invocation
1094 uint32_t clinit_thread_id_;
1095
1096 // Total object size; used when allocating storage on gc heap. (For
1097 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001099
1100 // For array classes, the class object for base element, for
1101 // instanceof/checkcast (for String[][][], this will be String).
1102 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001103 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001104
1105 // For array classes, the number of array dimensions, e.g. int[][]
1106 // is 2. Otherwise 0.
1107 int32_t array_rank_;
1108
1109 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1110 PrimitiveType primitive_type_;
1111
1112 // The superclass, or NULL if this is java.lang.Object or a
1113 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001114 Class* super_class_; // TODO: make an instance field
1115 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001116
1117 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001118 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001119
1120 // initiating class loader list
1121 // NOTE: for classes with low serialNumber, these are unused, and the
1122 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001123 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001124
1125 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001126 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001127 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001128
1129 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001130 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001131
1132 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001133 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001134
1135 // Virtual method table (vtable), for use by "invoke-virtual". The
1136 // vtable from the superclass is copied in, and virtual methods from
1137 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001138 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001139
1140 // Interface table (iftable), one entry per interface supported by
1141 // this class. That means one entry for each interface we support
1142 // directly, indirectly via superclass, or indirectly via
1143 // superinterface. This will be null if neither we nor our
1144 // superclass implement any interfaces.
1145 //
1146 // Why we need this: given "class Foo implements Face", declare
1147 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1148 // is part of the Face interface. We can't easily use a single
1149 // vtable.
1150 //
1151 // For every interface a concrete class implements, we create a list
1152 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001153 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001154 InterfaceEntry* iftable_;
1155
1156 // The interface vtable indices for iftable get stored here. By
1157 // placing them all in a single pool for each class that implements
1158 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001159 size_t ifvi_pool_count_;
1160 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001161
1162 // instance fields
1163 //
1164 // These describe the layout of the contents of a
1165 // DataObject-compatible Object. Note that only the fields directly
1166 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001167 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001168 //
1169 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001170 // the beginning of the field list. num_reference_instance_fields_
1171 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001172 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001173
1174 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001175 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001176
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001177 // Bitmap of offsets of ifields.
1178 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001179
1180 // source file name, if known. Otherwise, NULL.
1181 const char* source_file_;
1182
Jesse Wilson7833bd22011-08-09 18:31:44 -04001183 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001184 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001185
1186 // static field storage
1187 //
1188 // Each static field is stored in one of three arrays:
1189 // o references are stored in static_references_
1190 // o doubles and longs are stored in static_64bit_primitives_
1191 // o everything else is in static_32bit_primitives_
1192 // Static fields select their array using their type and their index using the
1193 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1194 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001195 ObjectArray<Object>* static_references_;
1196 IntArray* static_32bit_primitives_;
1197 LongArray* static_64bit_primitives_;
1198
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001199 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001200 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001201};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001202std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001203
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001204inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001205 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001206 DCHECK(klass_ != NULL);
1207 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001208}
1209
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001210inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001211 Class* java_lang_Class = klass_->klass_;
1212 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001213}
1214
1215inline bool Object::IsObjectArray() const {
1216 return IsArray() && !klass_->component_type_->IsPrimitive();
1217}
1218
1219inline bool Object::IsArray() const {
1220 return klass_->IsArray();
1221}
1222
Brian Carlstroma663ea52011-08-19 23:33:41 -07001223inline bool Object::IsField() const {
1224 Class* java_lang_Class = klass_->klass_;
1225 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1226 return klass_ == java_lang_reflect_Field;
1227}
1228
1229inline bool Object::IsMethod() const {
1230 Class* java_lang_Class = klass_->klass_;
1231 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1232 return klass_ == java_lang_reflect_Method;
1233}
1234
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001235inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001236 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001237 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001238 }
1239 return klass_->object_size_;
1240}
1241
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001242inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001243 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001244}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001245
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001246class DataObject : public Object {
1247 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001248 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001249 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001250 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001251};
1252
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001253template<class T>
1254class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001255 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001256 typedef T ElementType;
1257
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001258 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001259
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001260 const T* GetData() const {
1261 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001262 }
1263
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001264 T* GetData() {
1265 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001266 }
1267
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001268 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001269 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001270 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001271 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001272 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001273 }
1274
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001275 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001276 // TODO: ArrayStoreException
1277 if (IsValidIndex(i)) {
1278 GetData()[i] = value;
1279 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001280 }
1281
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001282 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001283 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001284 CHECK(array_class != NULL);
1285 array_class_ = array_class;
1286 }
1287
Brian Carlstroma663ea52011-08-19 23:33:41 -07001288 static void ResetArrayClass() {
1289 CHECK(array_class_ != NULL);
1290 array_class_ = NULL;
1291 }
1292
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001293 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001294 // Location of first element.
1295 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001296
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001297 static Class* array_class_;
1298
Carl Shapirof88c9522011-08-06 15:47:38 -07001299 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001300};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001301
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001302class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001303 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001304 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001305 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001306 return array_;
1307 }
1308
Carl Shapirof88c9522011-08-06 15:47:38 -07001309 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001310 return hash_code_;
1311 }
1312
Carl Shapirof88c9522011-08-06 15:47:38 -07001313 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001314 return offset_;
1315 }
1316
Carl Shapirof88c9522011-08-06 15:47:38 -07001317 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001318 return count_;
1319 }
1320
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001321 // TODO: do we need this? Equals is the only caller, and could
1322 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001323 uint16_t CharAt(int32_t index) const {
1324 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001325 Thread* self = Thread::Current();
1326 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1327 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001328 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001329 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001330 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001331 }
1332
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001333 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001334 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001335 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001336 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001337 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001338 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001339 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001340 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001341 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001342 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001343 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001344 }
1345
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001346 static String* AllocFromModifiedUtf8(const char* utf) {
1347 size_t char_count = ModifiedUtf8Len(utf);
1348 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001349 }
1350
Jesse Wilson8989d992011-08-02 13:39:42 -07001351 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1352 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001353 String* string = Alloc(GetJavaLangString(), utf16_length);
1354 uint16_t* utf16_data_out = string->array_->GetData();
1355 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1356 string->ComputeHashCode();
1357 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001358 }
1359
Brian Carlstroma663ea52011-08-19 23:33:41 -07001360 static void SetClass(Class* java_lang_String);
1361 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001362
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001363 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001364 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001365 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1366 }
1367
1368 static String* Alloc(Class* java_lang_String,
1369 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001370 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001371 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001372 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001373 return string;
1374 }
1375
1376 // Convert Modified UTF-8 to UTF-16
1377 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1378 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1379 while (*utf8_data_in != '\0') {
1380 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1381 }
1382 }
1383
1384 // Retrieve the next UTF-16 character from a UTF-8 string.
1385 //
1386 // Advances "*pUtf8Ptr" to the start of the next character.
1387 //
1388 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1389 // of a 3-byte sequence, you can end up overrunning the buffer with
1390 // reads (and possibly with the writes if the length was computed and
1391 // cached before the damage). For performance reasons, this function
1392 // assumes that the string being parsed is known to be valid (e.g., by
1393 // already being verified). Most strings we process here are coming
1394 // out of dex files or other internal translations, so the only real
1395 // risk comes from the JNI NewStringUTF call.
1396 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1397 uint8_t one = *(*utf8_data_in)++;
1398 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001399 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001400 return one;
1401 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001402 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001403 uint8_t two = *(*utf8_data_in)++;
1404 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001405 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001406 return ((one & 0x1f) << 6) |
1407 (two & 0x3f);
1408 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001409 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001410 uint8_t three = *(*utf8_data_in)++;
1411 return ((one & 0x0f) << 12) |
1412 ((two & 0x3f) << 6) |
1413 (three & 0x3f);
1414 }
1415
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001416 // Like "strlen", but for strings encoded with "modified" UTF-8.
1417 //
1418 // The value returned is the number of characters, which may or may not
1419 // be the same as the number of bytes.
1420 //
1421 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1422 // get increment {1-3} from table of 8 values.)
1423 static size_t ModifiedUtf8Len(const char* utf8) {
1424 size_t len = 0;
1425 int ic;
1426 while ((ic = *utf8++) != '\0') {
1427 len++;
1428 if ((ic & 0x80) == 0) {
1429 // one-byte encoding
1430 continue;
1431 }
1432 // two- or three-byte encoding
1433 utf8++;
1434 if ((ic & 0x20) == 0) {
1435 // two-byte encoding
1436 continue;
1437 }
1438 // three-byte encoding
1439 utf8++;
1440 }
1441 return len;
1442 }
1443
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001444 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001445 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1446 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001447 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001448 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001449 }
1450 return hash;
1451 }
1452
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001453 void ComputeHashCode() {
1454 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1455 }
1456
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001457 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001458 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001459 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001460 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1461 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001462 return false;
1463 }
1464 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001465 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001466 }
1467
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001468 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001469 bool Equals(const StringPiece& modified_utf8) const {
1470 // TODO: do not assume C-string representation.
1471 return Equals(modified_utf8.data());
1472 }
1473
1474 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001475 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001476 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001477 return false;
1478 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001479 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001480 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001481 return false;
1482 }
1483 }
1484 return true;
1485 }
1486
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001487 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001488 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001489 if (this->GetLength() != that_length) {
1490 return false;
1491 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001492 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001493 if (this->CharAt(i) != that_chars[that_offset + i]) {
1494 return false;
1495 }
1496 }
1497 return true;
1498 }
1499
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001500 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1501 std::string ToModifiedUtf8() const {
1502 std::string result;
1503 for (uint32_t i = 0; i < GetLength(); i++) {
1504 uint16_t ch = CharAt(i);
1505 // The most common case is (ch > 0 && ch <= 0x7f).
1506 if (ch == 0 || ch > 0x7f) {
1507 if (ch > 0x07ff) {
1508 result.push_back((ch >> 12) | 0xe0);
1509 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1510 result.push_back((ch & 0x3f) | 0x80);
1511 } else { // (ch > 0x7f || ch == 0)
1512 result.push_back((ch >> 6) | 0xc0);
1513 result.push_back((ch & 0x3f) | 0x80);
1514 }
1515 } else {
1516 result.push_back(ch);
1517 }
1518 }
1519 return result;
1520 }
1521
1522
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001523 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001524 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1525 CharArray* array_;
1526
Carl Shapirof88c9522011-08-06 15:47:38 -07001527 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001528
Elliott Hughes289da822011-08-16 10:11:20 -07001529 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001530
Elliott Hughes289da822011-08-16 10:11:20 -07001531 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001532
1533 static Class* GetJavaLangString() {
1534 DCHECK(java_lang_String_ != NULL);
1535 return java_lang_String_;
1536 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001537
1538 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001539
1540 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001541};
1542
Brian Carlstroma663ea52011-08-19 23:33:41 -07001543inline bool Object::IsString() const {
1544 // TODO use "klass_ == String::GetJavaLangString()" instead?
1545 return klass_ == klass_->descriptor_->klass_;
1546}
1547
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001548inline size_t Class::GetTypeSize(String* descriptor) {
1549 switch (descriptor->CharAt(0)) {
1550 case 'B': return 1; // byte
1551 case 'C': return 2; // char
1552 case 'D': return 8; // double
1553 case 'F': return 4; // float
1554 case 'I': return 4; // int
1555 case 'J': return 8; // long
1556 case 'S': return 2; // short
1557 case 'Z': return 1; // boolean
1558 case 'L': return sizeof(Object*);
1559 case '[': return sizeof(Array*);
1560 default:
1561 LOG(ERROR) << "Unknown type " << descriptor;
1562 return 0;
1563 }
1564}
1565
1566inline bool Class::IsArray() const {
1567 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1568}
1569
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001570class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001571 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001572 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1573 }
1574
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001575 Class* GetClass() const {
1576 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001577 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001578
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001579 void SetClass(Class* klass) {
1580 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001581 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001582
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001583 private:
1584 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001585 Class* klass_;
1586
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001587 public: // TODO: private
1588 // Index into array of vtable offsets. This points into the
1589 // ifviPool, which holds the vtables for all interfaces declared by
1590 // this class.
1591 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001592
1593 private:
1594 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001595};
1596
1597} // namespace art
1598
1599#endif // ART_SRC_OBJECT_H_