blob: e246deac0812b2a7f002fde85c384e3f6910130d [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
Brian Carlstroma663ea52011-08-19 23:33:41 -0700242 bool IsString() const;
243
244 String* AsString() {
245 DCHECK(IsString());
246 return down_cast<String*>(this);
247 }
248
249 bool IsMethod() const;
250
251 Method* AsMethod() {
252 DCHECK(IsMethod());
253 return down_cast<Method*>(this);
254 }
255
256 bool IsField() const;
257
258 Field* AsField() {
259 DCHECK(IsField());
260 return down_cast<Field*>(this);
261 }
262
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700263 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -0700264 Class* klass_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700265
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700266 Monitor* monitor_;
267
268 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700269 DISALLOW_IMPLICIT_CONSTRUCTORS(Object);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700270};
271
272class ObjectLock {
273 public:
Ian Rogersb033c752011-07-20 12:22:35 -0700274 explicit ObjectLock(Object* object) : obj_(object) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700275 CHECK(object != NULL);
276 obj_->MonitorEnter();
277 }
278
279 ~ObjectLock() {
280 obj_->MonitorExit();
281 }
282
283 void Wait(int64_t millis = 0) {
284 return obj_->Wait(millis);
285 }
286
287 void Notify() {
288 obj_->Notify();
289 }
290
291 void NotifyAll() {
292 obj_->NotifyAll();
293 }
294
295 private:
296 Object* obj_;
297 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700298};
299
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400300class AccessibleObject : public Object {
301 private:
302 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
303 uint32_t java_flag_;
304};
305
306class Field : public AccessibleObject {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700307 public:
Brian Carlstroma0808032011-07-18 00:39:23 -0700308 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700309 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700310 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700311 }
312
Jesse Wilson14150742011-07-29 19:04:44 -0400313 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700314 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700315 return name_;
316 }
317
318 bool IsStatic() const {
319 return (access_flags_ & kAccStatic) != 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700320 }
321
322 char GetType() const { // TODO: return type
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700323 return GetDescriptor()[0];
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700324 }
325
Brian Carlstromae3ac012011-07-27 01:30:28 -0700326 const StringPiece& GetDescriptor() const {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700327 DCHECK_NE(0, descriptor_.size());
Brian Carlstromae3ac012011-07-27 01:30:28 -0700328 return descriptor_;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700329 }
330
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700331 uint32_t GetOffset() const {
332 return offset_;
333 }
334
335 void SetOffset(size_t num_bytes) {
336 offset_ = num_bytes;
337 }
338
Jesse Wilson35baaab2011-08-10 16:18:03 -0400339 // static field access
Jesse Wilson7833bd22011-08-09 18:31:44 -0400340 bool GetBoolean();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400341 void SetBoolean(bool z);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400342 int8_t GetByte();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400343 void SetByte(int8_t b);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400344 uint16_t GetChar();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400345 void SetChar(uint16_t c);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400346 uint16_t GetShort();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400347 void SetShort(uint16_t s);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400348 int32_t GetInt();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400349 void SetInt(int32_t i);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400350 int64_t GetLong();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400351 void SetLong(int64_t j);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400352 float GetFloat();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400353 void SetFloat(float f);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400354 double GetDouble();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400355 void SetDouble(double d);
Jesse Wilson7833bd22011-08-09 18:31:44 -0400356 Object* GetObject();
Jesse Wilson7833bd22011-08-09 18:31:44 -0400357 const Object* GetObject() const;
Jesse Wilson7833bd22011-08-09 18:31:44 -0400358 void SetObject(Object* l);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -0700359
Jesse Wilson35baaab2011-08-10 16:18:03 -0400360 public: // TODO: private
361 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
362 // The class in which this field is declared.
363 Class* declaring_class_;
364 Object* generic_type_;
365 uint32_t generic_types_are_initialized_;
366 String* name_;
367 uint32_t offset_;
368 Class* type_;
369
370 // e.g. "I", "[C", "Landroid/os/Debug;"
371 StringPiece descriptor_;
372
373 uint32_t access_flags_;
374
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700375 private:
Jesse Wilson35baaab2011-08-10 16:18:03 -0400376 DISALLOW_IMPLICIT_CONSTRUCTORS(Field);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700377};
378
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400379class Method : public AccessibleObject {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700380 public:
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700381 // An function that invokes a method with an array of its arguments.
382 typedef void InvokeStub(Method* method,
383 Object* obj,
384 Thread* thread,
385 byte* args,
386 JValue* result);
387
Brian Carlstromae3ac012011-07-27 01:30:28 -0700388 // Returns the method name, e.g. "<init>" or "eatLunch"
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700389 const String* GetName() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700390 DCHECK(name_ != NULL);
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700391 return name_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700392 }
393
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700394 const String* GetSignature() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700395 DCHECK(signature_ != NULL);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700396 return signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700397 }
398
Brian Carlstroma0808032011-07-18 00:39:23 -0700399 Class* GetDeclaringClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700400 DCHECK(declaring_class_ != NULL);
Brian Carlstroma0808032011-07-18 00:39:23 -0700401 return declaring_class_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700402 }
403
Ian Rogerscdd1d2d2011-08-18 09:58:17 -0700404 static MemberOffset DeclaringClassOffset() {
405 return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_));
Ian Rogersb033c752011-07-20 12:22:35 -0700406 }
407
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700408 // Returns true if the method is declared public.
409 bool IsPublic() const {
410 return (access_flags_ & kAccPublic) != 0;
411 }
412
413 // Returns true if the method is declared private.
414 bool IsPrivate() const {
415 return (access_flags_ & kAccPrivate) != 0;
416 }
417
418 // Returns true if the method is declared static.
419 bool IsStatic() const {
420 return (access_flags_ & kAccStatic) != 0;
421 }
422
423 // Returns true if the method is declared synchronized.
424 bool IsSynchronized() const {
425 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
426 return (access_flags_ & synchonized) != 0;
427 }
428
429 // Returns true if the method is declared final.
430 bool IsFinal() const {
431 return (access_flags_ & kAccFinal) != 0;
432 }
433
434 // Returns true if the method is declared native.
435 bool IsNative() const {
436 return (access_flags_ & kAccNative) != 0;
437 }
438
439 // Returns true if the method is declared abstract.
440 bool IsAbstract() const {
441 return (access_flags_ & kAccAbstract) != 0;
442 }
443
444 bool IsSynthetic() const {
445 return (access_flags_ & kAccSynthetic) != 0;
446 }
447
448 // Number of argument registers required by the prototype.
449 uint32_t NumArgRegisters();
450
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700451 // Number of argument bytes required for densely packing the
452 // arguments into an array of arguments.
453 size_t NumArgArrayBytes();
454
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700455 public: // TODO: private
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400456 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
Jesse Wilson35baaab2011-08-10 16:18:03 -0400457 // the class we are a part of
458 Class* declaring_class_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400459 ObjectArray<Class>* java_exception_types_;
460 Object* java_formal_type_parameters_;
461 Object* java_generic_exception_types_;
462 Object* java_generic_parameter_types_;
463 Object* java_generic_return_type_;
464 Class* java_return_type_;
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700465 String* name_;
Jesse Wilson46cdd4b2011-07-28 17:40:48 -0400466 ObjectArray<Class>* java_parameter_types_;
467 uint32_t java_generic_types_are_initialized_;
468 uint32_t java_slot_;
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700469
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700470 const StringPiece& GetShorty() const {
471 return shorty_;
472 }
473
Ian Rogersb033c752011-07-20 12:22:35 -0700474 bool IsReturnAReference() const {
475 return (shorty_[0] == 'L') || (shorty_[0] == '[');
476 }
477
478 bool IsReturnAFloatOrDouble() const {
479 return (shorty_[0] == 'F') || (shorty_[0] == 'D');
480 }
481
482 bool IsReturnAFloat() const {
483 return shorty_[0] == 'F';
484 }
485
486 bool IsReturnADouble() const {
487 return shorty_[0] == 'D';
488 }
489
490 bool IsReturnALong() const {
491 return shorty_[0] == 'J';
492 }
493
Ian Rogers45a76cb2011-07-21 22:00:15 -0700494 bool IsReturnVoid() const {
495 return shorty_[0] == 'V';
496 }
497
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700498 // "Args" may refer to any of the 3 levels of "Args."
499 // To avoid confusion, our code will denote which "Args" clearly:
500 // 1. UserArgs: Args that a user see.
501 // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the
502 // receiver.
503 // 3. CConvArgs: Calling Convention Args, which is physical-level Args.
504 // E.g., the first in Args is Method* for both static and non-static
505 // methods. And CConvArgs doesn't deal with the receiver because
506 // receiver is hardwired in an implicit register, so CConvArgs doesn't
507 // need to deal with it.
508 //
509 // The number of Args that should be supplied to this method
Ian Rogersb033c752011-07-20 12:22:35 -0700510 size_t NumArgs() const {
Shih-wei Liao5381cf92011-07-27 00:28:04 -0700511 // "1 +" because the first in Args is the receiver.
512 // "- 1" because we don't count the return type.
Ian Rogersb033c752011-07-20 12:22:35 -0700513 return (IsStatic() ? 0 : 1) + shorty_.length() - 1;
514 }
515
516 // The number of reference arguments to this method including implicit this
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700517 // pointer.
Ian Rogersb033c752011-07-20 12:22:35 -0700518 size_t NumReferenceArgs() const;
519
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700520 // The number of long or double arguments.
Ian Rogersb033c752011-07-20 12:22:35 -0700521 size_t NumLongOrDoubleArgs() const;
522
523 // The number of reference arguments to this method before the given
524 // parameter index
525 size_t NumReferenceArgsBefore(unsigned int param) const;
526
527 // Is the given method parameter a reference?
528 bool IsParamAReference(unsigned int param) const;
529
530 // Is the given method parameter a long or double?
531 bool IsParamALongOrDouble(unsigned int param) const;
532
Ian Rogersdf20fe02011-07-20 20:34:16 -0700533 // Size in bytes of the given parameter
534 size_t ParamSize(unsigned int param) const;
535
536 // Size in bytes of the return value
537 size_t ReturnSize() const;
Ian Rogersb033c752011-07-20 12:22:35 -0700538
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700539 const void* GetCode() const {
540 return code_;
541 }
542
Ian Rogersb033c752011-07-20 12:22:35 -0700543 void SetCode(const void* code) {
544 code_ = code;
545 }
546
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700547 static size_t GetCodeOffset() {
548 return OFFSETOF_MEMBER(Method, code_);
Ian Rogersb033c752011-07-20 12:22:35 -0700549 }
550
551 void RegisterNative(const void* native_method) {
552 native_method_ = native_method;
553 }
554
555 static MemberOffset NativeMethodOffset() {
556 return MemberOffset(OFFSETOF_MEMBER(Method, native_method_));
557 }
558
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700559 InvokeStub* GetInvokeStub() const {
560 return invoke_stub_;
561 }
562
563 void SetInvokeStub(const InvokeStub* invoke_stub) {
564 invoke_stub_ = invoke_stub;
565 }
566
567 static size_t GetInvokeStubOffset() {
568 return OFFSETOF_MEMBER(Method, invoke_stub_);
569 }
570
Carl Shapiro8860c0e2011-08-04 17:36:16 -0700571 bool HasSameNameAndDescriptor(const Method* that) const;
572
Ian Rogersb033c752011-07-20 12:22:35 -0700573 public: // TODO: private/const
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700574 // access flags; low 16 bits are defined by spec (could be uint16_t?)
575 uint32_t access_flags_;
576
577 // For concrete virtual methods, this is the offset of the method
578 // in "vtable".
579 //
580 // For abstract methods in an interface class, this is the offset
581 // of the method in "iftable[n]->methodIndexArray".
582 uint16_t method_index_;
583
584 // Method bounds; not needed for an abstract method.
585 //
586 // For a native method, we compute the size of the argument list, and
587 // set "insSize" and "registerSize" equal to it.
588 uint16_t num_registers_; // ins + locals
589 uint16_t num_outs_;
590 uint16_t num_ins_;
591
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700592 // The method descriptor. This represents the parameters a method
593 // takes and value it returns. This string is a list of the type
594 // descriptors for the parameters enclosed in parenthesis followed
595 // by the return type descriptor. For example, for the method
596 //
597 // Object mymethod(int i, double d, Thread t)
598 //
599 // the method descriptor would be
600 //
601 // (IDLjava/lang/Thread;)Ljava/lang/Object;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700602 String* signature_;
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700603
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700604 // Method prototype descriptor string (return and argument types).
605 uint32_t proto_idx_;
606
Shih-wei Liao2fb97532011-08-11 16:17:23 -0700607 // Offset to the CodeItem.
608 uint32_t code_off_;
609
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700610 // The short-form method descriptor string.
611 StringPiece shorty_;
612
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700613 private:
Ian Rogersb033c752011-07-20 12:22:35 -0700614 // Compiled code associated with this method
615 const void* code_;
616
617 // Any native method registered with this method
618 const void* native_method_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700619
Carl Shapiro9b9ba282011-08-14 15:30:39 -0700620 // Native invocation stub entry point.
621 const InvokeStub* invoke_stub_;
622
Carl Shapirof88c9522011-08-06 15:47:38 -0700623 DISALLOW_IMPLICIT_CONSTRUCTORS(Method);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700624};
625
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700626class Array : public Object {
627 public:
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700628 static size_t SizeOf(size_t component_count,
629 size_t component_size) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700630 return sizeof(Array) + component_count * component_size;
631 }
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700632
Elliott Hughes68f4fa02011-08-21 10:46:59 -0700633 // A convenience for code that doesn't know the component size,
634 // and doesn't want to have to work it out itself.
635 static Array* Alloc(Class* array_class, size_t component_count);
636
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700637 static Array* Alloc(Class* array_class,
638 size_t component_count,
639 size_t component_size) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700640 size_t size = SizeOf(component_count, component_size);
641 Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size));
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700642 if (array != NULL) {
643 array->SetLength(component_count);
644 }
645 return array;
646 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700647
Elliott Hughes04b63fd2011-08-16 09:40:10 -0700648 size_t SizeOf() const;
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700649
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700650 int32_t GetLength() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700651 return length_;
652 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700653
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700654 void SetLength(uint32_t length) {
655 length_ = length;
656 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700657
Elliott Hughes289da822011-08-16 10:11:20 -0700658 protected:
659 bool IsValidIndex(int32_t index) const {
660 if (index < 0 || index >= length_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700661 Thread* self = Thread::Current();
662 self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;",
663 "length=%i; index=%i", length_, index);
Elliott Hughes710a0cb2011-08-16 14:32:37 -0700664 return false;
Elliott Hughes289da822011-08-16 10:11:20 -0700665 }
666 return true;
667 }
668
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700669 private:
670 // The number of array elements.
Elliott Hughes289da822011-08-16 10:11:20 -0700671 int32_t length_;
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400672 // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary
673 int32_t padding_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700674
Carl Shapirof88c9522011-08-06 15:47:38 -0700675 DISALLOW_IMPLICIT_CONSTRUCTORS(Array);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700676};
677
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700678template<class T>
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700679class ObjectArray : public Array {
680 public:
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700681 static ObjectArray<T>* Alloc(Class* object_array_class,
682 size_t length) {
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700683 return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>();
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700684 }
685
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700686 T* const * GetData() const {
687 return reinterpret_cast<T* const *>(&elements_);
688 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400689
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700690 T** GetData() {
691 return reinterpret_cast<T**>(&elements_);
692 }
Jesse Wilsondf4189c2011-08-09 17:10:28 -0400693
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700694 T* Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -0700695 if (!IsValidIndex(i)) {
696 return NULL;
697 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700698 return GetData()[i];
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700699 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700700
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700701 void Set(int32_t i, T* object) {
Elliott Hughes289da822011-08-16 10:11:20 -0700702 if (IsValidIndex(i)) {
703 // TODO: ArrayStoreException
704 GetData()[i] = object; // TODO: write barrier
705 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700706 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700707
708 static void Copy(ObjectArray<T>* src, int src_pos,
709 ObjectArray<T>* dst, int dst_pos,
710 size_t length) {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700711 for (size_t i = 0; i < length; i++) {
712 dst->Set(dst_pos + i, src->Get(src_pos + i));
713 }
714 }
Carl Shapirof88c9522011-08-06 15:47:38 -0700715
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700716 ObjectArray<T>* CopyOf(int32_t new_length) {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700717 ObjectArray<T>* new_array = Alloc(klass_, new_length);
718 Copy(this, 0, new_array, 0, std::min(GetLength(), new_length));
719 return new_array;
720 }
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700721
722 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -0700723 // Location of first element.
724 T* elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -0700725
726 DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700727};
728
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700729// ClassLoader objects.
730class ClassLoader : public Object {
731 public:
732 std::vector<const DexFile*>& GetClassPath() {
733 return class_path_;
734 }
735 void SetClassPath(std::vector<const DexFile*>& class_path) {
736 DCHECK_EQ(0U, class_path_.size());
737 class_path_ = class_path;
738 }
739
740 private:
741 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
742 Object* packages_;
743 ClassLoader* parent_;
744
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700745 // TODO: remove once we can create a real PathClassLoader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700746 std::vector<const DexFile*> class_path_;
747
Carl Shapirof88c9522011-08-06 15:47:38 -0700748 DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700749};
750
751class BaseDexClassLoader : public ClassLoader {
752 private:
753 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
754 String* original_path_;
755 Object* path_list_;
Carl Shapirof88c9522011-08-06 15:47:38 -0700756 DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700757};
758
759class PathClassLoader : public BaseDexClassLoader {
760 private:
Carl Shapirof88c9522011-08-06 15:47:38 -0700761 DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700762};
763
Carl Shapiro1fb86202011-06-27 17:43:13 -0700764// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700765class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700766 public:
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700767
768 // Class Status
769 //
770 // kStatusNotReady: If a Class cannot be found in the class table by
771 // FindClass, it allocates an new one with AllocClass in the
772 // kStatusNotReady and calls LoadClass. Note if it does find a
773 // class, it may not be kStatusResolved and it will try to push it
774 // forward toward kStatusResolved.
775 //
776 // kStatusIdx: LoadClass populates with Class with information from
777 // the DexFile, moving the status to kStatusIdx, indicating that the
778 // Class values in super_class_ and interfaces_ have not been
779 // populated based on super_class_idx_ and interfaces_idx_. The new
780 // Class can then be inserted into the classes table.
781 //
782 // kStatusLoaded: After taking a lock on Class, the ClassLinker will
783 // attempt to move a kStatusIdx class forward to kStatusLoaded by
784 // using ResolveClass to initialize the super_class_ and interfaces_.
785 //
786 // kStatusResolved: Still holding the lock on Class, the ClassLinker
787 // will use LinkClass to link all members, creating Field and Method
788 // objects, setting up the vtable, etc. On success, the class is
789 // marked kStatusResolved.
790
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700791 enum Status {
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700792 kStatusError = -1,
793 kStatusNotReady = 0,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700794 kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700795 kStatusLoaded = 2, // DEX idx values resolved
796 kStatusResolved = 3, // part of linking
797 kStatusVerifying = 4, // in the process of being verified
798 kStatusVerified = 5, // logically part of linking; done pre-init
799 kStatusInitializing = 6, // class init in progress
800 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -0700801 };
802
803 enum PrimitiveType {
804 kPrimNot = -1
805 };
806
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700807 Object* NewInstance() {
808 return Heap::AllocObject(this, this->object_size_);
809 }
810
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700811 Class* GetSuperClass() const {
812 return super_class_;
813 }
814
815 uint32_t GetSuperClassIdx() const {
816 return super_class_idx_;
817 }
818
819 bool HasSuperClass() const {
820 return super_class_ != NULL;
821 }
822
Brian Carlstromf7ed11a2011-08-09 17:55:51 -0700823 bool IsAssignableFrom(const Class* klass) const {
824 DCHECK(klass != NULL);
825 if (this == klass) {
826 return true;
827 }
828 if (IsInterface()) {
829 return klass->Implements(this);
830 }
831 if (klass->IsArray()) {
832 return IsAssignableFromArray(klass);
833 }
834 return klass->IsSubClass(this);
835 }
836
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700837 ClassLoader* GetClassLoader() const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700838 return class_loader_;
839 }
840
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700841 DexCache* GetDexCache() const {
842 return dex_cache_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700843 }
844
845 Class* GetComponentType() const {
846 return component_type_;
847 }
848
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700849 static size_t GetTypeSize(String* descriptor);
Brian Carlstromdb4d5402011-08-09 12:18:28 -0700850
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700851 size_t GetComponentSize() const {
852 return GetTypeSize(component_type_->descriptor_);
853 }
854
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700855 const String* GetDescriptor() const {
856 DCHECK(descriptor_ != NULL);
857 // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep?
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700858 return descriptor_;
859 }
860
861 Status GetStatus() const {
862 return status_;
863 }
864
865 void SetStatus(Status new_status) {
866 // TODO: validate transition
867 status_ = new_status;
868 }
869
Carl Shapiro69759ea2011-07-21 18:13:35 -0700870 // Returns true if the class has failed to link.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700871 bool IsErroneous() const {
872 return GetStatus() == kStatusError;
873 }
874
Carl Shapiro69759ea2011-07-21 18:13:35 -0700875 // Returns true if the class has been verified.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700876 bool IsVerified() const {
877 return GetStatus() >= kStatusVerified;
878 }
879
Carl Shapiro69759ea2011-07-21 18:13:35 -0700880 // Returns true if the class has been linked.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700881 bool IsLinked() const {
882 return GetStatus() >= kStatusResolved;
883 }
884
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700885 // Returns true if the class has been loaded.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700886 bool IsLoaded() const {
887 return GetStatus() >= kStatusLoaded;
888 }
889
Carl Shapiro83ab4f32011-08-15 20:21:39 -0700890 // Returns true if the class is initialized.
891 bool IsInitialized() const {
892 return GetStatus() == kStatusInitialized;
893 }
894
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700895 // Returns true if this class is in the same packages as that class.
896 bool IsInSamePackage(const Class* that) const;
897
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700898 static bool IsInSamePackage(const String* descriptor1,
899 const String* descriptor2);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700900
901 // Returns true if this class represents an array class.
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700902 bool IsArray() const;
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700903
904 // Returns true if the class is an interface.
905 bool IsInterface() const {
906 return (access_flags_ & kAccInterface) != 0;
907 }
908
909 // Returns true if the class is declared public.
910 bool IsPublic() const {
911 return (access_flags_ & kAccPublic) != 0;
912 }
913
914 // Returns true if the class is declared final.
915 bool IsFinal() const {
916 return (access_flags_ & kAccFinal) != 0;
917 }
918
919 // Returns true if the class is abstract.
920 bool IsAbstract() const {
921 return (access_flags_ & kAccAbstract) != 0;
922 }
923
924 // Returns true if the class is an annotation.
925 bool IsAnnotation() const {
926 return (access_flags_ & kAccAnnotation) != 0;
927 }
928
929 // Returns true if the class is a primitive type.
930 bool IsPrimitive() const {
931 return primitive_type_ != kPrimNot;
932 }
933
Brian Carlstromae3ac012011-07-27 01:30:28 -0700934 // Returns true if the class is synthetic.
935 bool IsSynthetic() const {
936 return (access_flags_ & kAccSynthetic) != 0;
937 }
938
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700939 // Returns true if this class can access that class.
940 bool CanAccess(const Class* that) const {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700941 return that->IsPublic() || this->IsInSamePackage(that);
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700942 }
943
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700944 // Returns the number of static, private, and constructor methods.
945 size_t NumDirectMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700946 return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700947 }
948
Elliott Hughesd8ddfd52011-08-15 14:32:53 -0700949 Method* GetDirectMethod(int32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700950 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700951 return direct_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700952 }
953
954 void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700955 DCHECK_NE(NumDirectMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700956 direct_methods_->Set(i, f);
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700957 }
958
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700959 Method* FindDeclaredDirectMethod(const StringPiece& name,
960 const StringPiece& descriptor);
961
962 Method* FindDirectMethod(const StringPiece& name,
963 const StringPiece& descriptor);
964
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700965 // Returns the number of non-inherited virtual methods.
966 size_t NumVirtualMethods() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700967 return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700968 }
969
970 Method* GetVirtualMethod(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700971 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700972 return virtual_methods_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700973 }
974
975 void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700976 DCHECK_NE(NumVirtualMethods(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700977 virtual_methods_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700978 }
979
Carl Shapiro419ec7b2011-08-03 14:48:33 -0700980 Method* FindDeclaredVirtualMethod(const StringPiece& name,
981 const StringPiece& descriptor);
982
983 Method* FindVirtualMethod(const StringPiece& name,
984 const StringPiece& descriptor);
985
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700986 size_t NumInstanceFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700987 return (ifields_ != NULL) ? ifields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700988 }
989
Carl Shapiro69759ea2011-07-21 18:13:35 -0700990 // Returns the number of instance fields containing reference types.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700991 size_t NumReferenceInstanceFields() const {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700992 return num_reference_instance_fields_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700993 }
994
Elliott Hughescdf53122011-08-19 15:46:09 -0700995 // Finds the given instance field in this class or a superclass.
996 Field* FindInstanceField(const StringPiece& name,
997 const StringPiece& descriptor);
998
999 Field* FindDeclaredInstanceField(const StringPiece& name,
1000 const StringPiece& descriptor);
1001
1002 // Finds the given static field in this class or a superclass.
1003 Field* FindStaticField(const StringPiece& name,
1004 const StringPiece& descriptor);
1005
1006 Field* FindDeclaredStaticField(const StringPiece& name,
1007 const StringPiece& descriptor);
1008
Jesse Wilson35baaab2011-08-10 16:18:03 -04001009 Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001010 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001011 return ifields_->Get(i);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001012 }
1013
Jesse Wilson35baaab2011-08-10 16:18:03 -04001014 void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001015 DCHECK_NE(NumInstanceFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001016 ifields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001017 }
1018
1019 size_t NumStaticFields() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001020 return (sfields_ != NULL) ? sfields_->GetLength() : 0;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001021 }
1022
Jesse Wilson35baaab2011-08-10 16:18:03 -04001023 Field* GetStaticField(uint32_t i) const { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001024 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001025 return sfields_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001026 }
1027
Jesse Wilson35baaab2011-08-10 16:18:03 -04001028 void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001029 DCHECK_NE(NumStaticFields(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001030 sfields_->Set(i, f);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001031 }
1032
1033 uint32_t GetReferenceOffsets() const {
1034 return reference_offsets_;
1035 }
1036
1037 void SetReferenceOffsets(uint32_t new_reference_offsets) {
1038 reference_offsets_ = new_reference_offsets;
1039 }
1040
Carl Shapiro69759ea2011-07-21 18:13:35 -07001041 size_t NumInterfaces() const {
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001042 return (interfaces_ != NULL) ? interfaces_->GetLength() : 0;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001043 }
1044
1045 Class* GetInterface(uint32_t i) const {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001046 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001047 return interfaces_->Get(i);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001048 }
1049
1050 void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t
Brian Carlstrom75cb3b42011-07-28 02:13:36 -07001051 DCHECK_NE(NumInterfaces(), 0U);
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001052 interfaces_->Set(i, f);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001053 }
1054
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001055 void SetVerifyErrorClass(Class* klass) {
1056 // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier
1057 size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_);
1058 klass->SetFieldObject(field_offset, klass);
1059 }
1060
1061 private:
1062 bool Implements(const Class* klass) const;
1063 bool IsArrayAssignableFromArray(const Class* klass) const;
1064 bool IsAssignableFromArray(const Class* klass) const;
1065 bool IsSubClass(const Class* klass) const;
1066
Ian Rogersb033c752011-07-20 12:22:35 -07001067 public: // TODO: private
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001068 // descriptor for the class such as "java.lang.Class" or "[C"
1069 String* name_; // TODO initialize
Carl Shapiro1fb86202011-06-27 17:43:13 -07001070
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001071 // descriptor for the class such as "Ljava/lang/Class;" or "[C"
1072 String* descriptor_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001073
1074 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001075 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -07001076
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001077 // DexCache of resolved constant pool entries
Carl Shapiro1fb86202011-06-27 17:43:13 -07001078 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
Brian Carlstrom7e49dca2011-07-22 18:07:34 -07001079 DexCache* dex_cache_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001080
1081 // state of class initialization
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001082 Status status_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001083
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001084 // If class verify fails, we must return same error on subsequent tries.
1085 // Update with SetVerifyErrorClass to ensure a write barrier is used.
1086 const Class* verify_error_class_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001087
1088 // threadId, used to check for recursive <clinit> invocation
1089 uint32_t clinit_thread_id_;
1090
1091 // Total object size; used when allocating storage on gc heap. (For
1092 // interfaces and abstract classes this will be zero.)
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001093 size_t object_size_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001094
1095 // For array classes, the class object for base element, for
1096 // instanceof/checkcast (for String[][][], this will be String).
1097 // Otherwise, NULL.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001098 Class* component_type_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001099
1100 // For array classes, the number of array dimensions, e.g. int[][]
1101 // is 2. Otherwise 0.
1102 int32_t array_rank_;
1103
1104 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
1105 PrimitiveType primitive_type_;
1106
1107 // The superclass, or NULL if this is java.lang.Object or a
1108 // primitive type.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001109 Class* super_class_; // TODO: make an instance field
1110 uint32_t super_class_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001111
1112 // defining class loader, or NULL for the "bootstrap" system loader
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001113 ClassLoader* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -07001114
1115 // initiating class loader list
1116 // NOTE: for classes with low serialNumber, these are unused, and the
1117 // values are kept in a table in gDvm.
Ian Rogersb033c752011-07-20 12:22:35 -07001118 // InitiatingLoaderList initiating_loader_list_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001119
1120 // array of interfaces this class implements directly
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001121 ObjectArray<Class>* interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001122 uint32_t* interfaces_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001123
1124 // static, private, and <init> methods
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001125 ObjectArray<Method>* direct_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001126
1127 // virtual methods defined in this class; invoked through vtable
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001128 ObjectArray<Method>* virtual_methods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001129
1130 // Virtual method table (vtable), for use by "invoke-virtual". The
1131 // vtable from the superclass is copied in, and virtual methods from
1132 // our class either replace those from the super or are appended.
Brian Carlstrom4a96b602011-07-26 16:40:23 -07001133 ObjectArray<Method>* vtable_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001134
1135 // Interface table (iftable), one entry per interface supported by
1136 // this class. That means one entry for each interface we support
1137 // directly, indirectly via superclass, or indirectly via
1138 // superinterface. This will be null if neither we nor our
1139 // superclass implement any interfaces.
1140 //
1141 // Why we need this: given "class Foo implements Face", declare
1142 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
1143 // is part of the Face interface. We can't easily use a single
1144 // vtable.
1145 //
1146 // For every interface a concrete class implements, we create a list
1147 // of virtualMethod indices for the methods in the interface.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001148 size_t iftable_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001149 InterfaceEntry* iftable_;
1150
1151 // The interface vtable indices for iftable get stored here. By
1152 // placing them all in a single pool for each class that implements
1153 // interfaces, we decrease the number of allocations.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001154 size_t ifvi_pool_count_;
1155 uint32_t* ifvi_pool_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001156
1157 // instance fields
1158 //
1159 // These describe the layout of the contents of a
1160 // DataObject-compatible Object. Note that only the fields directly
1161 // declared by this class are listed in ifields; fields declared by
Brian Carlstroma331b3c2011-07-18 17:47:56 -07001162 // a superclass are listed in the superclass's Class.ifields.
Carl Shapiro1fb86202011-06-27 17:43:13 -07001163 //
1164 // All instance fields that refer to objects are guaranteed to be at
Brian Carlstrom913af1b2011-07-23 21:41:13 -07001165 // the beginning of the field list. num_reference_instance_fields_
1166 // specifies the number of reference fields.
Jesse Wilson35baaab2011-08-10 16:18:03 -04001167 ObjectArray<Field>* ifields_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001168
1169 // number of fields that are object refs
Carl Shapiro69759ea2011-07-21 18:13:35 -07001170 size_t num_reference_instance_fields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001171
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001172 // Bitmap of offsets of ifields.
1173 uint32_t reference_offsets_;
Carl Shapiro1fb86202011-06-27 17:43:13 -07001174
1175 // source file name, if known. Otherwise, NULL.
1176 const char* source_file_;
1177
Jesse Wilson7833bd22011-08-09 18:31:44 -04001178 // Static fields
Jesse Wilson35baaab2011-08-10 16:18:03 -04001179 ObjectArray<Field>* sfields_;
Jesse Wilson7833bd22011-08-09 18:31:44 -04001180
1181 // static field storage
1182 //
1183 // Each static field is stored in one of three arrays:
1184 // o references are stored in static_references_
1185 // o doubles and longs are stored in static_64bit_primitives_
1186 // o everything else is in static_32bit_primitives_
1187 // Static fields select their array using their type and their index using the
1188 // Field->slot_ member. Storing static fields in arrays avoids the need for a
1189 // special case in the GC.
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001190 ObjectArray<Object>* static_references_;
1191 IntArray* static_32bit_primitives_;
1192 LongArray* static_64bit_primitives_;
1193
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001194 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001195 DISALLOW_IMPLICIT_CONSTRUCTORS(Class);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001196};
Elliott Hughes1f359b02011-07-17 14:27:17 -07001197std::ostream& operator<<(std::ostream& os, const Class::Status& rhs);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001198
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001199inline bool Object::InstanceOf(const Class* klass) const {
Jesse Wilson14150742011-07-29 19:04:44 -04001200 DCHECK(klass != NULL);
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001201 DCHECK(klass_ != NULL);
1202 return klass->IsAssignableFrom(klass_);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001203}
1204
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001205inline bool Object::IsClass() const {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001206 Class* java_lang_Class = klass_->klass_;
1207 return klass_ == java_lang_Class;
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001208}
1209
1210inline bool Object::IsObjectArray() const {
1211 return IsArray() && !klass_->component_type_->IsPrimitive();
1212}
1213
1214inline bool Object::IsArray() const {
1215 return klass_->IsArray();
1216}
1217
Brian Carlstroma663ea52011-08-19 23:33:41 -07001218inline bool Object::IsField() const {
1219 Class* java_lang_Class = klass_->klass_;
1220 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_;
1221 return klass_ == java_lang_reflect_Field;
1222}
1223
1224inline bool Object::IsMethod() const {
1225 Class* java_lang_Class = klass_->klass_;
1226 Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_;
1227 return klass_ == java_lang_reflect_Method;
1228}
1229
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001230inline size_t Object::SizeOf() const {
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001231 if (IsArray()) {
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001232 return AsArray()->SizeOf();
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001233 }
1234 return klass_->object_size_;
1235}
1236
Elliott Hughes04b63fd2011-08-16 09:40:10 -07001237inline size_t Array::SizeOf() const {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001238 return SizeOf(GetLength(), klass_->GetComponentSize());
Brian Carlstromdb4d5402011-08-09 12:18:28 -07001239}
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001240
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001241class DataObject : public Object {
1242 public:
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07001243 uint32_t fields_[0];
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001244 private:
Carl Shapirof88c9522011-08-06 15:47:38 -07001245 DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001246};
1247
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001248template<class T>
1249class PrimitiveArray : public Array {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001250 public:
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001251 typedef T ElementType;
1252
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001253 static PrimitiveArray<T>* Alloc(size_t length);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001254
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001255 const T* GetData() const {
1256 return reinterpret_cast<const T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001257 }
1258
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001259 T* GetData() {
1260 return reinterpret_cast<T*>(&elements_);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001261 }
1262
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001263 T Get(int32_t i) const {
Elliott Hughes289da822011-08-16 10:11:20 -07001264 if (!IsValidIndex(i)) {
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001265 return T(0);
Elliott Hughes289da822011-08-16 10:11:20 -07001266 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001267 return GetData()[i];
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001268 }
1269
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001270 void Set(int32_t i, T value) {
Elliott Hughes289da822011-08-16 10:11:20 -07001271 // TODO: ArrayStoreException
1272 if (IsValidIndex(i)) {
1273 GetData()[i] = value;
1274 }
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001275 }
1276
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001277 static void SetArrayClass(Class* array_class) {
Brian Carlstroma663ea52011-08-19 23:33:41 -07001278 CHECK(array_class_ == NULL);
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001279 CHECK(array_class != NULL);
1280 array_class_ = array_class;
1281 }
1282
Brian Carlstroma663ea52011-08-19 23:33:41 -07001283 static void ResetArrayClass() {
1284 CHECK(array_class_ != NULL);
1285 array_class_ = NULL;
1286 }
1287
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001288 private:
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001289 // Location of first element.
1290 T elements_[0];
Carl Shapirof88c9522011-08-06 15:47:38 -07001291
Elliott Hughesd8ddfd52011-08-15 14:32:53 -07001292 static Class* array_class_;
1293
Carl Shapirof88c9522011-08-06 15:47:38 -07001294 DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray);
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001295};
Carl Shapiro5fafe2b2011-07-09 15:34:41 -07001296
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001297class String : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001298 public:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001299 const CharArray* GetCharArray() const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001300 DCHECK(array_ != NULL);
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001301 return array_;
1302 }
1303
Carl Shapirof88c9522011-08-06 15:47:38 -07001304 uint32_t GetHashCode() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001305 return hash_code_;
1306 }
1307
Carl Shapirof88c9522011-08-06 15:47:38 -07001308 uint32_t GetOffset() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001309 return offset_;
1310 }
1311
Carl Shapirof88c9522011-08-06 15:47:38 -07001312 uint32_t GetLength() const {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001313 return count_;
1314 }
1315
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001316 // TODO: do we need this? Equals is the only caller, and could
1317 // bounds check itself.
Elliott Hughes289da822011-08-16 10:11:20 -07001318 uint16_t CharAt(int32_t index) const {
1319 if (index < 0 || index >= count_) {
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001320 Thread* self = Thread::Current();
1321 self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;",
1322 "length=%i; index=%i", count_, index);
Elliott Hughes289da822011-08-16 10:11:20 -07001323 return 0;
Elliott Hughes289da822011-08-16 10:11:20 -07001324 }
Elliott Hughes710a0cb2011-08-16 14:32:37 -07001325 return GetCharArray()->Get(index + GetOffset());
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001326 }
1327
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001328 static String* AllocFromUtf16(int32_t utf16_length,
Brian Carlstroma663ea52011-08-19 23:33:41 -07001329 const uint16_t* utf16_data_in,
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001330 int32_t hash_code) {
Carl Shapirof88c9522011-08-06 15:47:38 -07001331 String* string = Alloc(GetJavaLangString(),
Carl Shapirof88c9522011-08-06 15:47:38 -07001332 utf16_length);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001333 // TODO: use 16-bit wide memset variant
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001334 for (int i = 0; i < utf16_length; i++ ) {
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001335 string->array_->Set(i, utf16_data_in[i]);
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001336 }
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001337 string->ComputeHashCode();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001338 return string;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -07001339 }
1340
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001341 static String* AllocFromModifiedUtf8(const char* utf) {
1342 size_t char_count = ModifiedUtf8Len(utf);
1343 return AllocFromModifiedUtf8(char_count, utf);
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001344 }
1345
Jesse Wilson8989d992011-08-02 13:39:42 -07001346 static String* AllocFromModifiedUtf8(int32_t utf16_length,
1347 const char* utf8_data_in) {
Elliott Hughesbfaadc82011-08-18 17:36:58 -07001348 String* string = Alloc(GetJavaLangString(), utf16_length);
1349 uint16_t* utf16_data_out = string->array_->GetData();
1350 ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in);
1351 string->ComputeHashCode();
1352 return string;
Jesse Wilson8989d992011-08-02 13:39:42 -07001353 }
1354
Brian Carlstroma663ea52011-08-19 23:33:41 -07001355 static void SetClass(Class* java_lang_String);
1356 static void ResetClass();
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001357
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001358 static String* Alloc(Class* java_lang_String,
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001359 int32_t utf16_length) {
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001360 return Alloc(java_lang_String, CharArray::Alloc(utf16_length));
1361 }
1362
1363 static String* Alloc(Class* java_lang_String,
1364 CharArray* array) {
Brian Carlstromf7ed11a2011-08-09 17:55:51 -07001365 String* string = down_cast<String*>(java_lang_String->NewInstance());
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001366 string->array_ = array;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001367 string->count_ = array->GetLength();
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001368 return string;
1369 }
1370
1371 // Convert Modified UTF-8 to UTF-16
1372 // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8
1373 static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) {
1374 while (*utf8_data_in != '\0') {
1375 *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in);
1376 }
1377 }
1378
1379 // Retrieve the next UTF-16 character from a UTF-8 string.
1380 //
1381 // Advances "*pUtf8Ptr" to the start of the next character.
1382 //
1383 // WARNING: If a string is corrupted by dropping a '\0' in the middle
1384 // of a 3-byte sequence, you can end up overrunning the buffer with
1385 // reads (and possibly with the writes if the length was computed and
1386 // cached before the damage). For performance reasons, this function
1387 // assumes that the string being parsed is known to be valid (e.g., by
1388 // already being verified). Most strings we process here are coming
1389 // out of dex files or other internal translations, so the only real
1390 // risk comes from the JNI NewStringUTF call.
1391 static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) {
1392 uint8_t one = *(*utf8_data_in)++;
1393 if ((one & 0x80) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001394 // one-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001395 return one;
1396 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001397 // two- or three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001398 uint8_t two = *(*utf8_data_in)++;
1399 if ((one & 0x20) == 0) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001400 // two-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001401 return ((one & 0x1f) << 6) |
1402 (two & 0x3f);
1403 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001404 // three-byte encoding
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001405 uint8_t three = *(*utf8_data_in)++;
1406 return ((one & 0x0f) << 12) |
1407 ((two & 0x3f) << 6) |
1408 (three & 0x3f);
1409 }
1410
Brian Carlstrom74eb46a2011-08-02 20:10:14 -07001411 // Like "strlen", but for strings encoded with "modified" UTF-8.
1412 //
1413 // The value returned is the number of characters, which may or may not
1414 // be the same as the number of bytes.
1415 //
1416 // (If this needs optimizing, try: mask against 0xa0, shift right 5,
1417 // get increment {1-3} from table of 8 values.)
1418 static size_t ModifiedUtf8Len(const char* utf8) {
1419 size_t len = 0;
1420 int ic;
1421 while ((ic = *utf8++) != '\0') {
1422 len++;
1423 if ((ic & 0x80) == 0) {
1424 // one-byte encoding
1425 continue;
1426 }
1427 // two- or three-byte encoding
1428 utf8++;
1429 if ((ic & 0x20) == 0) {
1430 // two-byte encoding
1431 continue;
1432 }
1433 // three-byte encoding
1434 utf8++;
1435 }
1436 return len;
1437 }
1438
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001439 // The java/lang/String.computeHashCode() algorithm
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001440 static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) {
1441 int32_t hash = 0;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001442 while (string_length--) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001443 hash = hash * 31 + *string_data++;
Brian Carlstrom0b138b22011-07-27 15:19:17 -07001444 }
1445 return hash;
1446 }
1447
Jesse Wilsonfd687c52011-08-04 19:27:35 -07001448 void ComputeHashCode() {
1449 hash_code_ = ComputeUtf16Hash(array_->GetData(), count_);
1450 }
1451
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001452 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001453 bool Equals(const char* modified_utf8) const {
Carl Shapirof88c9522011-08-06 15:47:38 -07001454 for (uint32_t i = 0; i < GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001455 uint16_t ch = GetUtf16FromUtf8(&modified_utf8);
1456 if (ch == '\0' || ch != CharAt(i)) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001457 return false;
1458 }
1459 }
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001460 return *modified_utf8 == '\0';
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -04001461 }
1462
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001463 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001464 bool Equals(const StringPiece& modified_utf8) const {
1465 // TODO: do not assume C-string representation.
1466 return Equals(modified_utf8.data());
1467 }
1468
1469 bool Equals(const String* that) const {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -07001470 // TODO: short circuit on hash_code_
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001471 if (this->GetLength() != that->GetLength()) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001472 return false;
1473 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001474 for (uint32_t i = 0; i < that->GetLength(); ++i) {
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001475 if (this->CharAt(i) != that->CharAt(i)) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001476 return false;
1477 }
1478 }
1479 return true;
1480 }
1481
Elliott Hughesa5b897e2011-08-16 11:33:06 -07001482 // TODO: do we need this overload? give it a more intention-revealing name.
Carl Shapirof88c9522011-08-06 15:47:38 -07001483 bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001484 if (this->GetLength() != that_length) {
1485 return false;
1486 }
Carl Shapirof88c9522011-08-06 15:47:38 -07001487 for (uint32_t i = 0; i < that_length; ++i) {
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001488 if (this->CharAt(i) != that_chars[that_offset + i]) {
1489 return false;
1490 }
1491 }
1492 return true;
1493 }
1494
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001495 // Create a modified UTF-8 encoded std::string from a java/lang/String object.
1496 std::string ToModifiedUtf8() const {
1497 std::string result;
1498 for (uint32_t i = 0; i < GetLength(); i++) {
1499 uint16_t ch = CharAt(i);
1500 // The most common case is (ch > 0 && ch <= 0x7f).
1501 if (ch == 0 || ch > 0x7f) {
1502 if (ch > 0x07ff) {
1503 result.push_back((ch >> 12) | 0xe0);
1504 result.push_back(((ch >> 6) & 0x3f) | 0x80);
1505 result.push_back((ch & 0x3f) | 0x80);
1506 } else { // (ch > 0x7f || ch == 0)
1507 result.push_back((ch >> 6) | 0xc0);
1508 result.push_back((ch & 0x3f) | 0x80);
1509 }
1510 } else {
1511 result.push_back(ch);
1512 }
1513 }
1514 return result;
1515 }
1516
1517
Brian Carlstroma7f4f482011-07-17 17:01:34 -07001518 private:
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001519 // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses".
1520 CharArray* array_;
1521
Carl Shapirof88c9522011-08-06 15:47:38 -07001522 uint32_t hash_code_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001523
Elliott Hughes289da822011-08-16 10:11:20 -07001524 int32_t offset_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001525
Elliott Hughes289da822011-08-16 10:11:20 -07001526 int32_t count_;
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001527
1528 static Class* GetJavaLangString() {
1529 DCHECK(java_lang_String_ != NULL);
1530 return java_lang_String_;
1531 }
Jesse Wilsonf7e85a52011-08-01 18:45:58 -07001532
1533 static Class* java_lang_String_;
Carl Shapiro8860c0e2011-08-04 17:36:16 -07001534
1535 DISALLOW_IMPLICIT_CONSTRUCTORS(String);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001536};
1537
Brian Carlstroma663ea52011-08-19 23:33:41 -07001538inline bool Object::IsString() const {
1539 // TODO use "klass_ == String::GetJavaLangString()" instead?
1540 return klass_ == klass_->descriptor_->klass_;
1541}
1542
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07001543inline size_t Class::GetTypeSize(String* descriptor) {
1544 switch (descriptor->CharAt(0)) {
1545 case 'B': return 1; // byte
1546 case 'C': return 2; // char
1547 case 'D': return 8; // double
1548 case 'F': return 4; // float
1549 case 'I': return 4; // int
1550 case 'J': return 8; // long
1551 case 'S': return 2; // short
1552 case 'Z': return 1; // boolean
1553 case 'L': return sizeof(Object*);
1554 case '[': return sizeof(Array*);
1555 default:
1556 LOG(ERROR) << "Unknown type " << descriptor;
1557 return 0;
1558 }
1559}
1560
1561inline bool Class::IsArray() const {
1562 return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor
1563}
1564
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001565class InterfaceEntry {
Carl Shapiro1fb86202011-06-27 17:43:13 -07001566 public:
Carl Shapirof88c9522011-08-06 15:47:38 -07001567 InterfaceEntry() : klass_(NULL), method_index_array_(NULL) {
1568 }
1569
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001570 Class* GetClass() const {
1571 return klass_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001572 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001573
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001574 void SetClass(Class* klass) {
1575 klass_ = klass;
Carl Shapirof88c9522011-08-06 15:47:38 -07001576 }
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001577
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001578 private:
1579 // Points to the interface class.
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001580 Class* klass_;
1581
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001582 public: // TODO: private
1583 // Index into array of vtable offsets. This points into the
1584 // ifviPool, which holds the vtables for all interfaces declared by
1585 // this class.
1586 uint32_t* method_index_array_;
Carl Shapirof88c9522011-08-06 15:47:38 -07001587
1588 private:
1589 DISALLOW_COPY_AND_ASSIGN(InterfaceEntry);
Carl Shapiro1fb86202011-06-27 17:43:13 -07001590};
1591
1592} // namespace art
1593
1594#endif // ART_SRC_OBJECT_H_