blob: ca09d078992dcab6005e794010a2caf461bdd657 [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;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070030typedef PrimitiveArray<uint8_t> BooleanArray;
31typedef PrimitiveArray<int8_t> ByteArray;
Jesse Wilsonfd687c52011-08-04 19:27:35 -070032typedef PrimitiveArray<uint16_t> CharArray;
Elliott Hughesd8ddfd52011-08-15 14:32:53 -070033typedef PrimitiveArray<double> DoubleArray;
34typedef PrimitiveArray<float> FloatArray;
35typedef PrimitiveArray<int32_t> IntArray;
36typedef PrimitiveArray<int64_t> LongArray;
37typedef PrimitiveArray<int16_t> ShortArray;
Carl Shapiro1fb86202011-06-27 17:43:13 -070038
Carl Shapiro3ee755d2011-06-28 12:11:04 -070039union JValue {
40 uint8_t z;
41 int8_t b;
42 uint16_t c;
43 int16_t s;
44 int32_t i;
45 int64_t j;
46 float f;
47 double d;
48 Object* l;
49};
50
Brian Carlstrombe977852011-07-19 14:54:54 -070051static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
52static const uint32_t kAccPrivate = 0x0002; // field, method, ic
53static const uint32_t kAccProtected = 0x0004; // field, method, ic
54static const uint32_t kAccStatic = 0x0008; // field, method, ic
55static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
56static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
57static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
58static const uint32_t kAccVolatile = 0x0040; // field
59static const uint32_t kAccBridge = 0x0040; // method (1.5)
60static const uint32_t kAccTransient = 0x0080; // field
61static const uint32_t kAccVarargs = 0x0080; // method (1.5)
62static const uint32_t kAccNative = 0x0100; // method
63static const uint32_t kAccInterface = 0x0200; // class, ic
64static const uint32_t kAccAbstract = 0x0400; // class, method, ic
65static const uint32_t kAccStrict = 0x0800; // method
66static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
67static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
68static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070069
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070070static const uint32_t kAccMiranda = 0x8000; // method
71
Brian Carlstroma331b3c2011-07-18 17:47:56 -070072static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16)
73
Brian Carlstrombe977852011-07-19 14:54:54 -070074static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
75static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070077/*
Brian Carlstroma331b3c2011-07-18 17:47:56 -070078 * Definitions for packing refOffsets in Class.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070079 */
80/*
81 * A magic value for refOffsets. Ignore the bits and walk the super
82 * chain when this is the value.
83 * [This is an unlikely "natural" value, since it would be 30 non-ref instance
84 * fields followed by 2 ref instance fields.]
85 */
86#define CLASS_WALK_SUPER ((unsigned int)(3))
87#define CLASS_SMALLEST_OFFSET (sizeof(struct Object))
88#define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8)
89#define CLASS_OFFSET_ALIGNMENT 4
90#define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1))
91/*
92 * Given an offset, return the bit number which would encode that offset.
93 * Local use only.
94 */
95#define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \
96 (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \
97 CLASS_OFFSET_ALIGNMENT)
98/*
99 * Is the given offset too large to be encoded?
100 */
101#define CLASS_CAN_ENCODE_OFFSET(byteOffset) \
102 (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD)
103/*
104 * Return a single bit, encoding the offset.
105 * Undefined if the offset is too large, as defined above.
106 */
107#define CLASS_BIT_FROM_OFFSET(byteOffset) \
108 (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset))
109/*
110 * Return an offset, given a bit number as returned from CLZ.
111 */
112#define CLASS_OFFSET_FROM_CLZ(rshift) \
Ian Rogersb033c752011-07-20 12:22:35 -0700113 ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700114
115
Carl Shapiro1fb86202011-06-27 17:43:13 -0700116class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700117 public:
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700118 static bool InstanceOf(const Object* object, const Class* klass) {
119 if (object == NULL) {
120 return false;
121 }
122 return object->InstanceOf(klass);
123 }
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700124
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700125 Class* GetClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700126 DCHECK(klass_ != NULL);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700127 return klass_;
128 }
129
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700130 bool InstanceOf(const Class* klass) const;
131
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700132 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700133
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134 void MonitorEnter() {
135 monitor_->Enter();
136 }
137
138 void MonitorExit() {
139 monitor_->Exit();
140 }
141
142 void Notify() {
143 monitor_->Notify();
144 }
145
146 void NotifyAll() {
147 monitor_->NotifyAll();
148 }
149
150 void Wait() {
151 monitor_->Wait();
152 }
153
154 void Wait(int64_t timeout) {
155 monitor_->Wait(timeout);
156 }
157
158 void Wait(int64_t timeout, int32_t nanos) {
159 monitor_->Wait(timeout, nanos);
160 }
161
Carl Shapiro69759ea2011-07-21 18:13:35 -0700162 const Object* GetFieldObject(size_t field_offset) const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700163 Object* that = const_cast<Object*>(this);
164 Object* other = that->GetFieldObject(field_offset);
165 return const_cast<const Object*>(other);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166 }
167
168 Object* GetFieldObject(size_t field_offset) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700169 byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset;
170 return *reinterpret_cast<Object**>(raw_addr);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700171 }
172
173 void SetFieldObject(size_t offset, Object* new_value) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700174 byte* raw_addr = reinterpret_cast<byte*>(this) + offset;
175 *reinterpret_cast<Object**>(raw_addr) = new_value;
176 // TODO: write barrier
177 }
178
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700179 bool IsClass() const;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700180
181 Class* AsClass() {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700182 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700183 return down_cast<Class*>(this);
184 }
185
186 const Class* AsClass() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700187 DCHECK(IsClass());
Carl Shapiro69759ea2011-07-21 18:13:35 -0700188 return down_cast<const Class*>(this);
189 }
190
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700191 bool IsObjectArray() const;
192
193 template<class T>
194 ObjectArray<T>* AsObjectArray() {
195 DCHECK(IsObjectArray());
196 return down_cast<ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700197 }
198
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700199 template<class T>
200 const ObjectArray<T>* AsObjectArray() const {
201 DCHECK(IsObjectArray());
202 return down_cast<const ObjectArray<T>*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700203 }
204
205 bool IsReference() 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 IsWeakReference() 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 IsSoftReference() 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 IsFinalizerReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700221 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222 return true;
223 }
224
225 bool IsPhantomReference() const {
Elliott Hughes53b61312011-08-12 18:28:20 -0700226 UNIMPLEMENTED(FATAL);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700227 return true;
228 }
229
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700230 bool IsArray() const;
231
232 Array* AsArray() {
233 DCHECK(IsArray());
234 return down_cast<Array*>(this);
235 }
236
237 const Array* AsArray() const {
238 DCHECK(IsArray());
239 return down_cast<const Array*>(this);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240 }
241
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700242 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700243 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700244
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700245 Monitor* monitor_;
246
247 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700248 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700249};
250
251class ObjectLock {
252 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700253 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700254 CHECK(object != NULL);
255 obj_->MonitorEnter();
256 }
257
258 ~ObjectLock() {
259 obj_->MonitorExit();
260 }
261
262 void Wait(int64_t millis = 0) {
263 return obj_->Wait(millis);
264 }
265
266 void Notify() {
267 obj_->Notify();
268 }
269
270 void NotifyAll() {
271 obj_->NotifyAll();
272 }
273
274 private:
275 Object* obj_;
276 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700277};
278
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400279class AccessibleObject : public Object {
280 private:
281 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
282 uint32_t java_flag_;
283};
284
285class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700286 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700287 Class* GetDeclaringClass() const {
288 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700289 }
290
Jesse Wilson14150742011-07-29 19:04:44 -0400291 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700292 return name_;
293 }
294
295 bool IsStatic() const {
296 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700297 }
298
299 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700300 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700301 }
302
Brian Carlstromae3ac012011-07-27 01:30:28 -0700303 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700304 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700305 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700306 }
307
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700308 uint32_t GetOffset() const {
309 return offset_;
310 }
311
312 void SetOffset(size_t num_bytes) {
313 offset_ = num_bytes;
314 }
315
Jesse Wilson35baaab2011-08-10 16:18:03 -0400316 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400317 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400318 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400319 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400320 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400321 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400322 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400323 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400324 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400325 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400326 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400327 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400328 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400329 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400330 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400331 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400332 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400333 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400334 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400335 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700336
Jesse Wilson35baaab2011-08-10 16:18:03 -0400337 public: // TODO: private
338 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
339 // The class in which this field is declared.
340 Class* declaring_class_;
341 Object* generic_type_;
342 uint32_t generic_types_are_initialized_;
343 String* name_;
344 uint32_t offset_;
345 Class* type_;
346
347 // e.g. "I", "[C", "Landroid/os/Debug;"
348 StringPiece descriptor_;
349
350 uint32_t access_flags_;
351
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700352 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400353 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700354};
355
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400356class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700357 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700358 // An function that invokes a method with an array of its arguments.
359 typedef void InvokeStub(Method* method,
360 Object* obj,
361 Thread* thread,
362 byte* args,
363 JValue* result);
364
Brian Carlstromae3ac012011-07-27 01:30:28 -0700365 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700366 const String* GetName() const {
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700367 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700368 }
369
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700370 const String* GetSignature() const {
371 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700372 }
373
Brian Carlstroma0808032011-07-18 00:39:23 -0700374 Class* GetDeclaringClass() const {
375 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700376 }
377
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700378 static MemberOffset DeclaringClassOffset() {
379 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700380 }
381
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700382 // Returns true if the method is declared public.
383 bool IsPublic() const {
384 return (access_flags_ & kAccPublic) != 0;
385 }
386
387 // Returns true if the method is declared private.
388 bool IsPrivate() const {
389 return (access_flags_ & kAccPrivate) != 0;
390 }
391
392 // Returns true if the method is declared static.
393 bool IsStatic() const {
394 return (access_flags_ & kAccStatic) != 0;
395 }
396
397 // Returns true if the method is declared synchronized.
398 bool IsSynchronized() const {
399 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
400 return (access_flags_ & synchonized) != 0;
401 }
402
403 // Returns true if the method is declared final.
404 bool IsFinal() const {
405 return (access_flags_ & kAccFinal) != 0;
406 }
407
408 // Returns true if the method is declared native.
409 bool IsNative() const {
410 return (access_flags_ & kAccNative) != 0;
411 }
412
413 // Returns true if the method is declared abstract.
414 bool IsAbstract() const {
415 return (access_flags_ & kAccAbstract) != 0;
416 }
417
418 bool IsSynthetic() const {
419 return (access_flags_ & kAccSynthetic) != 0;
420 }
421
422 // Number of argument registers required by the prototype.
423 uint32_t NumArgRegisters();
424
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700425 // Number of argument bytes required for densely packing the
426 // arguments into an array of arguments.
427 size_t NumArgArrayBytes();
428
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700429 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400430 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400431 // the class we are a part of
432 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400433 ObjectArray<Class>* java_exception_types_;
434 Object* java_formal_type_parameters_;
435 Object* java_generic_exception_types_;
436 Object* java_generic_parameter_types_;
437 Object* java_generic_return_type_;
438 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700439 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400440 ObjectArray<Class>* java_parameter_types_;
441 uint32_t java_generic_types_are_initialized_;
442 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700443
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700444 const StringPiece& GetShorty() const {
445 return shorty_;
446 }
447
Ian Rogersb033c752011-07-20 12:22:35 -0700448 bool IsReturnAReference() const {
449 return (shorty_[0] == 'L') || (shorty_[0] == '[');
450 }
451
452 bool IsReturnAFloatOrDouble() const {
453 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
454 }
455
456 bool IsReturnAFloat() const {
457 return shorty_[0] == 'F';
458 }
459
460 bool IsReturnADouble() const {
461 return shorty_[0] == 'D';
462 }
463
464 bool IsReturnALong() const {
465 return shorty_[0] == 'J';
466 }
467
Ian Rogers45a76cb2011-07-21 22:00:15 -0700468 bool IsReturnVoid() const {
469 return shorty_[0] == 'V';
470 }
471
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700472 // "Args" may refer to any of the 3 levels of "Args."
473 // To avoid confusion, our code will denote which "Args" clearly:
474 // 1. UserArgs: Args that a user see.
475 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
476 // receiver.
477 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
478 // E.g., the first in Args is Method* for both static and non-static
479 // methods. And CConvArgs doesn't deal with the receiver because
480 // receiver is hardwired in an implicit register, so CConvArgs doesn't
481 // need to deal with it.
482 //
483 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700484 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700485 // "1 +" because the first in Args is the receiver.
486 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700487 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
488 }
489
490 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700491 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700492 size_t NumReferenceArgs() const;
493
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700494 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700495 size_t NumLongOrDoubleArgs() const;
496
497 // The number of reference arguments to this method before the given
498 // parameter index
499 size_t NumReferenceArgsBefore(unsigned int param) const;
500
501 // Is the given method parameter a reference?
502 bool IsParamAReference(unsigned int param) const;
503
504 // Is the given method parameter a long or double?
505 bool IsParamALongOrDouble(unsigned int param) const;
506
Ian Rogersdf20fe02011-07-20 20:34:16 -0700507 // Size in bytes of the given parameter
508 size_t ParamSize(unsigned int param) const;
509
510 // Size in bytes of the return value
511 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700512
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700513 const void* GetCode() const {
514 return code_;
515 }
516
Ian Rogersb033c752011-07-20 12:22:35 -0700517 void SetCode(const void* code) {
518 code_ = code;
519 }
520
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700521 static size_t GetCodeOffset() {
522 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700523 }
524
525 void RegisterNative(const void* native_method) {
526 native_method_ = native_method;
527 }
528
529 static MemberOffset NativeMethodOffset() {
530 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
531 }
532
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700533 InvokeStub* GetInvokeStub() const {
534 return invoke_stub_;
535 }
536
537 void SetInvokeStub(const InvokeStub* invoke_stub) {
538 invoke_stub_ = invoke_stub;
539 }
540
541 static size_t GetInvokeStubOffset() {
542 return OFFSETOF_MEMBER(Method, invoke_stub_);
543 }
544
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700545 bool HasSameNameAndDescriptor(const Method* that) const;
546
Ian Rogersb033c752011-07-20 12:22:35 -0700547 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700548 // access flags; low 16 bits are defined by spec (could be uint16_t?)
549 uint32_t access_flags_;
550
551 // For concrete virtual methods, this is the offset of the method
552 // in "vtable".
553 //
554 // For abstract methods in an interface class, this is the offset
555 // of the method in "iftable[n]->methodIndexArray".
556 uint16_t method_index_;
557
558 // Method bounds; not needed for an abstract method.
559 //
560 // For a native method, we compute the size of the argument list, and
561 // set "insSize" and "registerSize" equal to it.
562 uint16_t num_registers_; // ins + locals
563 uint16_t num_outs_;
564 uint16_t num_ins_;
565
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700566 // The method descriptor. This represents the parameters a method
567 // takes and value it returns. This string is a list of the type
568 // descriptors for the parameters enclosed in parenthesis followed
569 // by the return type descriptor. For example, for the method
570 //
571 // Object mymethod(int i, double d, Thread t)
572 //
573 // the method descriptor would be
574 //
575 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700576 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700577
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700578 // Method prototype descriptor string (return and argument types).
579 uint32_t proto_idx_;
580
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700581 // Offset to the CodeItem.
582 uint32_t code_off_;
583
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700584 // The short-form method descriptor string.
585 StringPiece shorty_;
586
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700587 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700588 // Compiled code associated with this method
589 const void* code_;
590
591 // Any native method registered with this method
592 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700593
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700594 // Native invocation stub entry point.
595 const InvokeStub* invoke_stub_;
596
Carl Shapirof88c9522011-08-06 15:47:38 -0700597 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700598};
599
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700600class Array : public Object {
601 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700602 static size_t SizeOf(size_t component_count,
603 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700604 return sizeof(Array) + component_count * component_size;
605 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700606
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700607 // A convenience for code that doesn't know the component size,
608 // and doesn't want to have to work it out itself.
609 static Array* Alloc(Class* array_class, size_t component_count);
610
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700611 static Array* Alloc(Class* array_class,
612 size_t component_count,
613 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700614 size_t size = SizeOf(component_count, component_size);
615 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700616 if (array != NULL) {
617 array->SetLength(component_count);
618 }
619 return array;
620 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700621
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700622 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700623
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700624 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700625 return length_;
626 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700627
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700628 void SetLength(uint32_t length) {
629 length_ = length;
630 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700631
Elliott Hughes289da822011-08-16 10:11:20 -0700632 protected:
633 bool IsValidIndex(int32_t index) const {
634 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700635 Thread* self = Thread::Current();
636 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
637 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700638 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700639 }
640 return true;
641 }
642
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700643 private:
644 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700645 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400646 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
647 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700648
Carl Shapirof88c9522011-08-06 15:47:38 -0700649 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700650};
651
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700652template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700653class ObjectArray : public Array {
654 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700655 static ObjectArray<T>* Alloc(Class* object_array_class,
656 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700657 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700658 }
659
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700660 T* const * GetData() const {
661 return reinterpret_cast<T* const *>(&elements_);
662 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400663
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700664 T** GetData() {
665 return reinterpret_cast<T**>(&elements_);
666 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400667
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700668 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700669 if (!IsValidIndex(i)) {
670 return NULL;
671 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700672 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700673 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700674
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700675 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700676 if (IsValidIndex(i)) {
677 // TODO: ArrayStoreException
678 GetData()[i] = object; // TODO: write barrier
679 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700680 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700681
682 static void Copy(ObjectArray<T>* src, int src_pos,
683 ObjectArray<T>* dst, int dst_pos,
684 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700685 for (size_t i = 0; i < length; i++) {
686 dst->Set(dst_pos + i, src->Get(src_pos + i));
687 }
688 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700689
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700690 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700691 ObjectArray<T>* new_array = Alloc(klass_, new_length);
692 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
693 return new_array;
694 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700695
696 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700697 // Location of first element.
698 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700699
700 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700701};
702
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700703// ClassLoader objects.
704class ClassLoader : public Object {
705 public:
706 std::vector<const DexFile*>& GetClassPath() {
707 return class_path_;
708 }
709 void SetClassPath(std::vector<const DexFile*>& class_path) {
710 DCHECK_EQ(0U, class_path_.size());
711 class_path_ = class_path;
712 }
713
714 private:
715 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
716 Object* packages_;
717 ClassLoader* parent_;
718
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700719 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700720 std::vector<const DexFile*> class_path_;
721
Carl Shapirof88c9522011-08-06 15:47:38 -0700722 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700723};
724
725class BaseDexClassLoader : public ClassLoader {
726 private:
727 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
728 String* original_path_;
729 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700730 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700731};
732
733class PathClassLoader : public BaseDexClassLoader {
734 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700735 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700736};
737
Carl Shapiro1fb86202011-06-27 17:43:13 -0700738// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700739class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700740 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700741
742 // Class Status
743 //
744 // kStatusNotReady: If a Class cannot be found in the class table by
745 // FindClass, it allocates an new one with AllocClass in the
746 // kStatusNotReady and calls LoadClass. Note if it does find a
747 // class, it may not be kStatusResolved and it will try to push it
748 // forward toward kStatusResolved.
749 //
750 // kStatusIdx: LoadClass populates with Class with information from
751 // the DexFile, moving the status to kStatusIdx, indicating that the
752 // Class values in super_class_ and interfaces_ have not been
753 // populated based on super_class_idx_ and interfaces_idx_. The new
754 // Class can then be inserted into the classes table.
755 //
756 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
757 // attempt to move a kStatusIdx class forward to kStatusLoaded by
758 // using ResolveClass to initialize the super_class_ and interfaces_.
759 //
760 // kStatusResolved: Still holding the lock on Class, the ClassLinker
761 // will use LinkClass to link all members, creating Field and Method
762 // objects, setting up the vtable, etc. On success, the class is
763 // marked kStatusResolved.
764
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700765 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700766 kStatusError = -1,
767 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700768 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700769 kStatusLoaded = 2, // DEX idx values resolved
770 kStatusResolved = 3, // part of linking
771 kStatusVerifying = 4, // in the process of being verified
772 kStatusVerified = 5, // logically part of linking; done pre-init
773 kStatusInitializing = 6, // class init in progress
774 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700775 };
776
777 enum PrimitiveType {
778 kPrimNot = -1
779 };
780
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700781 Object* NewInstance() {
782 return Heap::AllocObject(this, this->object_size_);
783 }
784
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700785 Class* GetSuperClass() const {
786 return super_class_;
787 }
788
789 uint32_t GetSuperClassIdx() const {
790 return super_class_idx_;
791 }
792
793 bool HasSuperClass() const {
794 return super_class_ != NULL;
795 }
796
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700797 bool IsAssignableFrom(const Class* klass) const {
798 DCHECK(klass != NULL);
799 if (this == klass) {
800 return true;
801 }
802 if (IsInterface()) {
803 return klass->Implements(this);
804 }
805 if (klass->IsArray()) {
806 return IsAssignableFromArray(klass);
807 }
808 return klass->IsSubClass(this);
809 }
810
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700811 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700812 return class_loader_;
813 }
814
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700815 DexCache* GetDexCache() const {
816 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700817 }
818
819 Class* GetComponentType() const {
820 return component_type_;
821 }
822
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700823 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700824
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700825 size_t GetComponentSize() const {
826 return GetTypeSize(component_type_->descriptor_);
827 }
828
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700829 const String* GetDescriptor() const {
830 DCHECK(descriptor_ != NULL);
831 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700832 return descriptor_;
833 }
834
835 Status GetStatus() const {
836 return status_;
837 }
838
839 void SetStatus(Status new_status) {
840 // TODO: validate transition
841 status_ = new_status;
842 }
843
Carl Shapiro69759ea2011-07-21 18:13:35 -0700844 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700845 bool IsErroneous() const {
846 return GetStatus() == kStatusError;
847 }
848
Carl Shapiro69759ea2011-07-21 18:13:35 -0700849 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700850 bool IsVerified() const {
851 return GetStatus() >= kStatusVerified;
852 }
853
Carl Shapiro69759ea2011-07-21 18:13:35 -0700854 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700855 bool IsLinked() const {
856 return GetStatus() >= kStatusResolved;
857 }
858
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700859 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700860 bool IsLoaded() const {
861 return GetStatus() >= kStatusLoaded;
862 }
863
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700864 // Returns true if the class is initialized.
865 bool IsInitialized() const {
866 return GetStatus() == kStatusInitialized;
867 }
868
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700869 // Returns true if this class is in the same packages as that class.
870 bool IsInSamePackage(const Class* that) const;
871
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700872 static bool IsInSamePackage(const String* descriptor1,
873 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700874
875 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700876 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700877
878 // Returns true if the class is an interface.
879 bool IsInterface() const {
880 return (access_flags_ & kAccInterface) != 0;
881 }
882
883 // Returns true if the class is declared public.
884 bool IsPublic() const {
885 return (access_flags_ & kAccPublic) != 0;
886 }
887
888 // Returns true if the class is declared final.
889 bool IsFinal() const {
890 return (access_flags_ & kAccFinal) != 0;
891 }
892
893 // Returns true if the class is abstract.
894 bool IsAbstract() const {
895 return (access_flags_ & kAccAbstract) != 0;
896 }
897
898 // Returns true if the class is an annotation.
899 bool IsAnnotation() const {
900 return (access_flags_ & kAccAnnotation) != 0;
901 }
902
903 // Returns true if the class is a primitive type.
904 bool IsPrimitive() const {
905 return primitive_type_ != kPrimNot;
906 }
907
Brian Carlstromae3ac012011-07-27 01:30:28 -0700908 // Returns true if the class is synthetic.
909 bool IsSynthetic() const {
910 return (access_flags_ & kAccSynthetic) != 0;
911 }
912
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700913 // Returns true if this class can access that class.
914 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700915 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700916 }
917
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700918 // Returns the number of static, private, and constructor methods.
919 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700920 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700921 }
922
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700923 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700924 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700925 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700926 }
927
928 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700929 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700930 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700931 }
932
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700933 Method* FindDeclaredDirectMethod(const StringPiece& name,
934 const StringPiece& descriptor);
935
936 Method* FindDirectMethod(const StringPiece& name,
937 const StringPiece& descriptor);
938
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700939 // Returns the number of non-inherited virtual methods.
940 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700941 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700942 }
943
944 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700945 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700946 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700947 }
948
949 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700950 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700951 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700952 }
953
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700954 Method* FindDeclaredVirtualMethod(const StringPiece& name,
955 const StringPiece& descriptor);
956
957 Method* FindVirtualMethod(const StringPiece& name,
958 const StringPiece& descriptor);
959
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700960 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700961 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700962 }
963
Carl Shapiro69759ea2011-07-21 18:13:35 -0700964 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700965 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700966 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700967 }
968
Elliott Hughescdf53122011-08-19 15:46:09 -0700969 // Finds the given instance field in this class or a superclass.
970 Field* FindInstanceField(const StringPiece& name,
971 const StringPiece& descriptor);
972
973 Field* FindDeclaredInstanceField(const StringPiece& name,
974 const StringPiece& descriptor);
975
976 // Finds the given static field in this class or a superclass.
977 Field* FindStaticField(const StringPiece& name,
978 const StringPiece& descriptor);
979
980 Field* FindDeclaredStaticField(const StringPiece& name,
981 const StringPiece& descriptor);
982
Jesse Wilson35baaab2011-08-10 16:18:03 -0400983 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700984 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700985 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700986 }
987
Jesse Wilson35baaab2011-08-10 16:18:03 -0400988 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700989 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700990 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700991 }
992
993 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700994 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700995 }
996
Jesse Wilson35baaab2011-08-10 16:18:03 -0400997 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700998 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700999 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001000 }
1001
Jesse Wilson35baaab2011-08-10 16:18:03 -04001002 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001003 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001004 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001005 }
1006
1007 uint32_t GetReferenceOffsets() const {
1008 return reference_offsets_;
1009 }
1010
1011 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1012 reference_offsets_ = new_reference_offsets;
1013 }
1014
Carl Shapiro69759ea2011-07-21 18:13:35 -07001015 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001016 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001017 }
1018
1019 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001020 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001021 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001022 }
1023
1024 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001025 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001026 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001027 }
1028
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001029 void SetVerifyErrorClass(Class* klass) {
1030 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1031 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1032 klass->SetFieldObject(field_offset, klass);
1033 }
1034
1035 private:
1036 bool Implements(const Class* klass) const;
1037 bool IsArrayAssignableFromArray(const Class* klass) const;
1038 bool IsAssignableFromArray(const Class* klass) const;
1039 bool IsSubClass(const Class* klass) const;
1040
Ian Rogersb033c752011-07-20 12:22:35 -07001041 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001042 // descriptor for the class such as "java.lang.Class" or "[C"
1043 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001044
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001045 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1046 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001047
1048 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001049 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001050
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001051 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001052 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001053 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001054
1055 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001056 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001057
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001058 // If class verify fails, we must return same error on subsequent tries.
1059 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1060 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001061
1062 // threadId, used to check for recursive <clinit> invocation
1063 uint32_t clinit_thread_id_;
1064
1065 // Total object size; used when allocating storage on gc heap. (For
1066 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001067 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001068
1069 // For array classes, the class object for base element, for
1070 // instanceof/checkcast (for String[][][], this will be String).
1071 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001072 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001073
1074 // For array classes, the number of array dimensions, e.g. int[][]
1075 // is 2. Otherwise 0.
1076 int32_t array_rank_;
1077
1078 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1079 PrimitiveType primitive_type_;
1080
1081 // The superclass, or NULL if this is java.lang.Object or a
1082 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001083 Class* super_class_; // TODO: make an instance field
1084 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001085
1086 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001087 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001088
1089 // initiating class loader list
1090 // NOTE: for classes with low serialNumber, these are unused, and the
1091 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001092 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001093
1094 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001095 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001096 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001097
1098 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001099 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001100
1101 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001102 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001103
1104 // Virtual method table (vtable), for use by "invoke-virtual". The
1105 // vtable from the superclass is copied in, and virtual methods from
1106 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001107 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001108
1109 // Interface table (iftable), one entry per interface supported by
1110 // this class. That means one entry for each interface we support
1111 // directly, indirectly via superclass, or indirectly via
1112 // superinterface. This will be null if neither we nor our
1113 // superclass implement any interfaces.
1114 //
1115 // Why we need this: given "class Foo implements Face", declare
1116 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1117 // is part of the Face interface. We can't easily use a single
1118 // vtable.
1119 //
1120 // For every interface a concrete class implements, we create a list
1121 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001122 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001123 InterfaceEntry* iftable_;
1124
1125 // The interface vtable indices for iftable get stored here. By
1126 // placing them all in a single pool for each class that implements
1127 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001128 size_t ifvi_pool_count_;
1129 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001130
1131 // instance fields
1132 //
1133 // These describe the layout of the contents of a
1134 // DataObject-compatible Object. Note that only the fields directly
1135 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001136 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001137 //
1138 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001139 // the beginning of the field list. num_reference_instance_fields_
1140 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001141 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001142
1143 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001144 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001145
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001146 // Bitmap of offsets of ifields.
1147 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001148
1149 // source file name, if known. Otherwise, NULL.
1150 const char* source_file_;
1151
Jesse Wilson7833bd22011-08-09 18:31:44 -04001152 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001153 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001154
1155 // static field storage
1156 //
1157 // Each static field is stored in one of three arrays:
1158 // o references are stored in static_references_
1159 // o doubles and longs are stored in static_64bit_primitives_
1160 // o everything else is in static_32bit_primitives_
1161 // Static fields select their array using their type and their index using the
1162 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1163 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001164 ObjectArray<Object>* static_references_;
1165 IntArray* static_32bit_primitives_;
1166 LongArray* static_64bit_primitives_;
1167
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001168 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001169 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001170};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001171std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001172
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001173inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001174 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001175 DCHECK(klass_ != NULL);
1176 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001177}
1178
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001179inline bool Object::IsClass() const {
1180 return klass_ == klass_->klass_;
1181}
1182
1183inline bool Object::IsObjectArray() const {
1184 return IsArray() && !klass_->component_type_->IsPrimitive();
1185}
1186
1187inline bool Object::IsArray() const {
1188 return klass_->IsArray();
1189}
1190
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001191inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001192 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001193 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001194 }
1195 return klass_->object_size_;
1196}
1197
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001198inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001199 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001200}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001201
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001202class DataObject : public Object {
1203 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001204 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001205 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001206 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001207};
1208
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001209template<class T>
1210class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001211 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001212 typedef T ElementType;
1213
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001214 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001215
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001216 const T* GetData() const {
1217 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001218 }
1219
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001220 T* GetData() {
1221 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001222 }
1223
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001224 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001225 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001226 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001227 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001228 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001229 }
1230
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001231 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001232 // TODO: ArrayStoreException
1233 if (IsValidIndex(i)) {
1234 GetData()[i] = value;
1235 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001236 }
1237
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001238 static void SetArrayClass(Class* array_class) {
1239 CHECK(array_class != NULL);
1240 array_class_ = array_class;
1241 }
1242
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001243 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001244 // Location of first element.
1245 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001246
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001247 static Class* array_class_;
1248
Carl Shapirof88c9522011-08-06 15:47:38 -07001249 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001250};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001251
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001252class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001253 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001254 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001255 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001256 return array_;
1257 }
1258
Carl Shapirof88c9522011-08-06 15:47:38 -07001259 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001260 return hash_code_;
1261 }
1262
Carl Shapirof88c9522011-08-06 15:47:38 -07001263 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001264 return offset_;
1265 }
1266
Carl Shapirof88c9522011-08-06 15:47:38 -07001267 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001268 return count_;
1269 }
1270
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001271 // TODO: do we need this? Equals is the only caller, and could
1272 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001273 uint16_t CharAt(int32_t index) const {
1274 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001275 Thread* self = Thread::Current();
1276 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1277 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001278 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001279 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001280 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001281 }
1282
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001283 static String* AllocFromUtf16(int32_t utf16_length,
1284 uint16_t* utf16_data_in,
1285 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001286 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001287 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001288 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001289 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001290 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001291 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001292 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001293 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001294 }
1295
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001296 static String* AllocFromModifiedUtf8(const char* utf) {
1297 size_t char_count = ModifiedUtf8Len(utf);
1298 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001299 }
1300
Jesse Wilson8989d992011-08-02 13:39:42 -07001301 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1302 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001303 String* string = Alloc(GetJavaLangString(), utf16_length);
1304 uint16_t* utf16_data_out = string->array_->GetData();
1305 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1306 string->ComputeHashCode();
1307 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001308 }
1309
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001310 static void InitClass(Class* java_lang_String);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001311
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001312 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001313 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001314 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1315 }
1316
1317 static String* Alloc(Class* java_lang_String,
1318 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001319 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001320 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001321 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001322 return string;
1323 }
1324
1325 // Convert Modified UTF-8 to UTF-16
1326 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1327 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1328 while (*utf8_data_in != '\0') {
1329 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1330 }
1331 }
1332
1333 // Retrieve the next UTF-16 character from a UTF-8 string.
1334 //
1335 // Advances "*pUtf8Ptr" to the start of the next character.
1336 //
1337 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1338 // of a 3-byte sequence, you can end up overrunning the buffer with
1339 // reads (and possibly with the writes if the length was computed and
1340 // cached before the damage). For performance reasons, this function
1341 // assumes that the string being parsed is known to be valid (e.g., by
1342 // already being verified). Most strings we process here are coming
1343 // out of dex files or other internal translations, so the only real
1344 // risk comes from the JNI NewStringUTF call.
1345 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1346 uint8_t one = *(*utf8_data_in)++;
1347 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001348 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001349 return one;
1350 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001351 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001352 uint8_t two = *(*utf8_data_in)++;
1353 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001354 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001355 return ((one & 0x1f) << 6) |
1356 (two & 0x3f);
1357 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001358 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001359 uint8_t three = *(*utf8_data_in)++;
1360 return ((one & 0x0f) << 12) |
1361 ((two & 0x3f) << 6) |
1362 (three & 0x3f);
1363 }
1364
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001365 // Like "strlen", but for strings encoded with "modified" UTF-8.
1366 //
1367 // The value returned is the number of characters, which may or may not
1368 // be the same as the number of bytes.
1369 //
1370 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1371 // get increment {1-3} from table of 8 values.)
1372 static size_t ModifiedUtf8Len(const char* utf8) {
1373 size_t len = 0;
1374 int ic;
1375 while ((ic = *utf8++) != '\0') {
1376 len++;
1377 if ((ic & 0x80) == 0) {
1378 // one-byte encoding
1379 continue;
1380 }
1381 // two- or three-byte encoding
1382 utf8++;
1383 if ((ic & 0x20) == 0) {
1384 // two-byte encoding
1385 continue;
1386 }
1387 // three-byte encoding
1388 utf8++;
1389 }
1390 return len;
1391 }
1392
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001393 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001394 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1395 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001396 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001397 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001398 }
1399 return hash;
1400 }
1401
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001402 void ComputeHashCode() {
1403 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1404 }
1405
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001406 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001407 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001408 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001409 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1410 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001411 return false;
1412 }
1413 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001414 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001415 }
1416
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001417 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001418 bool Equals(const StringPiece& modified_utf8) const {
1419 // TODO: do not assume C-string representation.
1420 return Equals(modified_utf8.data());
1421 }
1422
1423 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001424 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001425 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001426 return false;
1427 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001428 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001429 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001430 return false;
1431 }
1432 }
1433 return true;
1434 }
1435
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001436 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001437 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001438 if (this->GetLength() != that_length) {
1439 return false;
1440 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001441 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001442 if (this->CharAt(i) != that_chars[that_offset + i]) {
1443 return false;
1444 }
1445 }
1446 return true;
1447 }
1448
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001449 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1450 std::string ToModifiedUtf8() const {
1451 std::string result;
1452 for (uint32_t i = 0; i < GetLength(); i++) {
1453 uint16_t ch = CharAt(i);
1454 // The most common case is (ch > 0 && ch <= 0x7f).
1455 if (ch == 0 || ch > 0x7f) {
1456 if (ch > 0x07ff) {
1457 result.push_back((ch >> 12) | 0xe0);
1458 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1459 result.push_back((ch & 0x3f) | 0x80);
1460 } else { // (ch > 0x7f || ch == 0)
1461 result.push_back((ch >> 6) | 0xc0);
1462 result.push_back((ch & 0x3f) | 0x80);
1463 }
1464 } else {
1465 result.push_back(ch);
1466 }
1467 }
1468 return result;
1469 }
1470
1471
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001472 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001473 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1474 CharArray* array_;
1475
Carl Shapirof88c9522011-08-06 15:47:38 -07001476 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001477
Elliott Hughes289da822011-08-16 10:11:20 -07001478 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001479
Elliott Hughes289da822011-08-16 10:11:20 -07001480 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001481
1482 static Class* GetJavaLangString() {
1483 DCHECK(java_lang_String_ != NULL);
1484 return java_lang_String_;
1485 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001486
1487 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001488
1489 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001490};
1491
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001492inline size_t Class::GetTypeSize(String* descriptor) {
1493 switch (descriptor->CharAt(0)) {
1494 case 'B': return 1; // byte
1495 case 'C': return 2; // char
1496 case 'D': return 8; // double
1497 case 'F': return 4; // float
1498 case 'I': return 4; // int
1499 case 'J': return 8; // long
1500 case 'S': return 2; // short
1501 case 'Z': return 1; // boolean
1502 case 'L': return sizeof(Object*);
1503 case '[': return sizeof(Array*);
1504 default:
1505 LOG(ERROR) << "Unknown type " << descriptor;
1506 return 0;
1507 }
1508}
1509
1510inline bool Class::IsArray() const {
1511 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1512}
1513
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001514class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001515 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001516 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1517 }
1518
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001519 Class* GetClass() const {
1520 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001521 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001522
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001523 void SetClass(Class* klass) {
1524 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001525 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001526
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001527 private:
1528 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001529 Class* klass_;
1530
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001531 public: // TODO: private
1532 // Index into array of vtable offsets. This points into the
1533 // ifviPool, which holds the vtables for all interfaces declared by
1534 // this class.
1535 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001536
1537 private:
1538 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001539};
1540
1541} // namespace art
1542
1543#endif // ART_SRC_OBJECT_H_