Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #ifndef ART_SRC_OBJECT_H_ |
| 4 | #define ART_SRC_OBJECT_H_ |
| 5 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "constants.h" |
| 7 | #include "casts.h" |
| 8 | #include "globals.h" |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 9 | #include "heap.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 10 | #include "logging.h" |
| 11 | #include "macros.h" |
| 12 | #include "offsets.h" |
| 13 | #include "stringpiece.h" |
| 14 | #include "monitor.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 15 | |
| 16 | namespace art { |
| 17 | |
| 18 | class Array; |
| 19 | class Class; |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 20 | class DexCache; |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 21 | class Field; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 22 | class InterfaceEntry; |
| 23 | class Monitor; |
| 24 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 25 | class Object; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 26 | class String; |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 27 | template<class T> class ObjectArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 28 | template<class T> class PrimitiveArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 29 | typedef PrimitiveArray<uint8_t> BooleanArray; |
| 30 | typedef PrimitiveArray<int8_t> ByteArray; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 31 | typedef PrimitiveArray<uint16_t> CharArray; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 32 | typedef PrimitiveArray<double> DoubleArray; |
| 33 | typedef PrimitiveArray<float> FloatArray; |
| 34 | typedef PrimitiveArray<int32_t> IntArray; |
| 35 | typedef PrimitiveArray<int64_t> LongArray; |
| 36 | typedef PrimitiveArray<int16_t> ShortArray; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 37 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 38 | union JValue { |
| 39 | uint8_t z; |
| 40 | int8_t b; |
| 41 | uint16_t c; |
| 42 | int16_t s; |
| 43 | int32_t i; |
| 44 | int64_t j; |
| 45 | float f; |
| 46 | double d; |
| 47 | Object* l; |
| 48 | }; |
| 49 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 50 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 51 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 52 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 53 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 54 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 55 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 56 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 57 | static const uint32_t kAccVolatile = 0x0040; // field |
| 58 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 59 | static const uint32_t kAccTransient = 0x0080; // field |
| 60 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 61 | static const uint32_t kAccNative = 0x0100; // method |
| 62 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 63 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 64 | static const uint32_t kAccStrict = 0x0800; // method |
| 65 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 66 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 67 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 68 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 69 | static const uint32_t kAccMiranda = 0x8000; // method |
| 70 | |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 71 | static const uint32_t kAccJavaFlagsMask = 0xffff; // bits set from Java sources (low 16) |
| 72 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 73 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 74 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 75 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 76 | /* |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 77 | * Definitions for packing refOffsets in Class. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 78 | */ |
| 79 | /* |
| 80 | * A magic value for refOffsets. Ignore the bits and walk the super |
| 81 | * chain when this is the value. |
| 82 | * [This is an unlikely "natural" value, since it would be 30 non-ref instance |
| 83 | * fields followed by 2 ref instance fields.] |
| 84 | */ |
| 85 | #define CLASS_WALK_SUPER ((unsigned int)(3)) |
| 86 | #define CLASS_SMALLEST_OFFSET (sizeof(struct Object)) |
| 87 | #define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8) |
| 88 | #define CLASS_OFFSET_ALIGNMENT 4 |
| 89 | #define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1)) |
| 90 | /* |
| 91 | * Given an offset, return the bit number which would encode that offset. |
| 92 | * Local use only. |
| 93 | */ |
| 94 | #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ |
| 95 | (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \ |
| 96 | CLASS_OFFSET_ALIGNMENT) |
| 97 | /* |
| 98 | * Is the given offset too large to be encoded? |
| 99 | */ |
| 100 | #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ |
| 101 | (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) |
| 102 | /* |
| 103 | * Return a single bit, encoding the offset. |
| 104 | * Undefined if the offset is too large, as defined above. |
| 105 | */ |
| 106 | #define CLASS_BIT_FROM_OFFSET(byteOffset) \ |
| 107 | (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) |
| 108 | /* |
| 109 | * Return an offset, given a bit number as returned from CLZ. |
| 110 | */ |
| 111 | #define CLASS_OFFSET_FROM_CLZ(rshift) \ |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 112 | ((static_cast<int>(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 113 | |
| 114 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 115 | class Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 116 | public: |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 117 | static bool InstanceOf(const Object* object, const Class* klass) { |
| 118 | if (object == NULL) { |
| 119 | return false; |
| 120 | } |
| 121 | return object->InstanceOf(klass); |
| 122 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 123 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 124 | Class* GetClass() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 125 | DCHECK(klass_ != NULL); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 126 | return klass_; |
| 127 | } |
| 128 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 129 | bool InstanceOf(const Class* klass) const; |
| 130 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 131 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 132 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 133 | void MonitorEnter() { |
| 134 | monitor_->Enter(); |
| 135 | } |
| 136 | |
| 137 | void MonitorExit() { |
| 138 | monitor_->Exit(); |
| 139 | } |
| 140 | |
| 141 | void Notify() { |
| 142 | monitor_->Notify(); |
| 143 | } |
| 144 | |
| 145 | void NotifyAll() { |
| 146 | monitor_->NotifyAll(); |
| 147 | } |
| 148 | |
| 149 | void Wait() { |
| 150 | monitor_->Wait(); |
| 151 | } |
| 152 | |
| 153 | void Wait(int64_t timeout) { |
| 154 | monitor_->Wait(timeout); |
| 155 | } |
| 156 | |
| 157 | void Wait(int64_t timeout, int32_t nanos) { |
| 158 | monitor_->Wait(timeout, nanos); |
| 159 | } |
| 160 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 161 | const Object* GetFieldObject(size_t field_offset) const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 162 | Object* that = const_cast<Object*>(this); |
| 163 | Object* other = that->GetFieldObject(field_offset); |
| 164 | return const_cast<const Object*>(other); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | Object* GetFieldObject(size_t field_offset) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 168 | byte* raw_addr = reinterpret_cast<byte*>(this) + field_offset; |
| 169 | return *reinterpret_cast<Object**>(raw_addr); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void SetFieldObject(size_t offset, Object* new_value) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 173 | byte* raw_addr = reinterpret_cast<byte*>(this) + offset; |
| 174 | *reinterpret_cast<Object**>(raw_addr) = new_value; |
| 175 | // TODO: write barrier |
| 176 | } |
| 177 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 178 | bool IsClass() const; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 179 | |
| 180 | Class* AsClass() { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 181 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 182 | return down_cast<Class*>(this); |
| 183 | } |
| 184 | |
| 185 | const Class* AsClass() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 186 | DCHECK(IsClass()); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 187 | return down_cast<const Class*>(this); |
| 188 | } |
| 189 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 190 | bool IsObjectArray() const; |
| 191 | |
| 192 | template<class T> |
| 193 | ObjectArray<T>* AsObjectArray() { |
| 194 | DCHECK(IsObjectArray()); |
| 195 | return down_cast<ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 198 | template<class T> |
| 199 | const ObjectArray<T>* AsObjectArray() const { |
| 200 | DCHECK(IsObjectArray()); |
| 201 | return down_cast<const ObjectArray<T>*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | bool IsReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 205 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 206 | return true; |
| 207 | } |
| 208 | |
| 209 | bool IsWeakReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 210 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | bool IsSoftReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 215 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 216 | return true; |
| 217 | } |
| 218 | |
| 219 | bool IsFinalizerReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 220 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool IsPhantomReference() const { |
Elliott Hughes | 53b6131 | 2011-08-12 18:28:20 -0700 | [diff] [blame] | 225 | UNIMPLEMENTED(FATAL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 229 | bool IsArray() const; |
| 230 | |
| 231 | Array* AsArray() { |
| 232 | DCHECK(IsArray()); |
| 233 | return down_cast<Array*>(this); |
| 234 | } |
| 235 | |
| 236 | const Array* AsArray() const { |
| 237 | DCHECK(IsArray()); |
| 238 | return down_cast<const Array*>(this); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 241 | bool IsString() const; |
| 242 | |
| 243 | String* AsString() { |
| 244 | DCHECK(IsString()); |
| 245 | return down_cast<String*>(this); |
| 246 | } |
| 247 | |
| 248 | bool IsMethod() const; |
| 249 | |
| 250 | Method* AsMethod() { |
| 251 | DCHECK(IsMethod()); |
| 252 | return down_cast<Method*>(this); |
| 253 | } |
| 254 | |
| 255 | bool IsField() const; |
| 256 | |
| 257 | Field* AsField() { |
| 258 | DCHECK(IsField()); |
| 259 | return down_cast<Field*>(this); |
| 260 | } |
| 261 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 262 | public: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 263 | Class* klass_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 264 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 265 | Monitor* monitor_; |
| 266 | |
| 267 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 268 | DISALLOW_IMPLICIT_CONSTRUCTORS(Object); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | class ObjectLock { |
| 272 | public: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 273 | explicit ObjectLock(Object* object) : obj_(object) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 274 | CHECK(object != NULL); |
| 275 | obj_->MonitorEnter(); |
| 276 | } |
| 277 | |
| 278 | ~ObjectLock() { |
| 279 | obj_->MonitorExit(); |
| 280 | } |
| 281 | |
| 282 | void Wait(int64_t millis = 0) { |
| 283 | return obj_->Wait(millis); |
| 284 | } |
| 285 | |
| 286 | void Notify() { |
| 287 | obj_->Notify(); |
| 288 | } |
| 289 | |
| 290 | void NotifyAll() { |
| 291 | obj_->NotifyAll(); |
| 292 | } |
| 293 | |
| 294 | private: |
| 295 | Object* obj_; |
| 296 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 299 | class AccessibleObject : public Object { |
| 300 | private: |
| 301 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 302 | uint32_t java_flag_; |
| 303 | }; |
| 304 | |
| 305 | class Field : public AccessibleObject { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 306 | public: |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 307 | Class* GetDeclaringClass() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 308 | DCHECK(declaring_class_ != NULL); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 309 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 312 | const String* GetName() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 313 | DCHECK(name_ != NULL); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 314 | return name_; |
| 315 | } |
| 316 | |
| 317 | bool IsStatic() const { |
| 318 | return (access_flags_ & kAccStatic) != 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | char GetType() const { // TODO: return type |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 322 | return GetDescriptor()[0]; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 325 | const StringPiece& GetDescriptor() const { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 326 | DCHECK_NE(0, descriptor_.size()); |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 327 | return descriptor_; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 330 | uint32_t GetOffset() const { |
| 331 | return offset_; |
| 332 | } |
| 333 | |
| 334 | void SetOffset(size_t num_bytes) { |
| 335 | offset_ = num_bytes; |
| 336 | } |
| 337 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 338 | // static field access |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 339 | bool GetBoolean(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 340 | void SetBoolean(bool z); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 341 | int8_t GetByte(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 342 | void SetByte(int8_t b); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 343 | uint16_t GetChar(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 344 | void SetChar(uint16_t c); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 345 | uint16_t GetShort(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 346 | void SetShort(uint16_t s); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 347 | int32_t GetInt(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 348 | void SetInt(int32_t i); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 349 | int64_t GetLong(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 350 | void SetLong(int64_t j); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 351 | float GetFloat(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 352 | void SetFloat(float f); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 353 | double GetDouble(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 354 | void SetDouble(double d); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 355 | Object* GetObject(); |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 356 | const Object* GetObject() const; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 357 | void SetObject(Object* l); |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 358 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 359 | public: // TODO: private |
| 360 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 361 | // The class in which this field is declared. |
| 362 | Class* declaring_class_; |
| 363 | Object* generic_type_; |
| 364 | uint32_t generic_types_are_initialized_; |
| 365 | String* name_; |
| 366 | uint32_t offset_; |
| 367 | Class* type_; |
| 368 | |
| 369 | // e.g. "I", "[C", "Landroid/os/Debug;" |
| 370 | StringPiece descriptor_; |
| 371 | |
| 372 | uint32_t access_flags_; |
| 373 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 374 | private: |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 375 | DISALLOW_IMPLICIT_CONSTRUCTORS(Field); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 376 | }; |
| 377 | |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 378 | class Method : public AccessibleObject { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 379 | public: |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 380 | // An function that invokes a method with an array of its arguments. |
| 381 | typedef void InvokeStub(Method* method, |
| 382 | Object* obj, |
| 383 | Thread* thread, |
| 384 | byte* args, |
| 385 | JValue* result); |
| 386 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 387 | // Returns the method name, e.g. "<init>" or "eatLunch" |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 388 | const String* GetName() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 389 | DCHECK(name_ != NULL); |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 390 | return name_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 393 | const String* GetSignature() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 394 | DCHECK(signature_ != NULL); |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 395 | return signature_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 398 | Class* GetDeclaringClass() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 399 | DCHECK(declaring_class_ != NULL); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 400 | return declaring_class_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Ian Rogers | cdd1d2d | 2011-08-18 09:58:17 -0700 | [diff] [blame] | 403 | static MemberOffset DeclaringClassOffset() { |
| 404 | return MemberOffset(OFFSETOF_MEMBER(Method, declaring_class_)); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 407 | // Returns true if the method is declared public. |
| 408 | bool IsPublic() const { |
| 409 | return (access_flags_ & kAccPublic) != 0; |
| 410 | } |
| 411 | |
| 412 | // Returns true if the method is declared private. |
| 413 | bool IsPrivate() const { |
| 414 | return (access_flags_ & kAccPrivate) != 0; |
| 415 | } |
| 416 | |
| 417 | // Returns true if the method is declared static. |
| 418 | bool IsStatic() const { |
| 419 | return (access_flags_ & kAccStatic) != 0; |
| 420 | } |
| 421 | |
| 422 | // Returns true if the method is declared synchronized. |
| 423 | bool IsSynchronized() const { |
| 424 | uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; |
| 425 | return (access_flags_ & synchonized) != 0; |
| 426 | } |
| 427 | |
| 428 | // Returns true if the method is declared final. |
| 429 | bool IsFinal() const { |
| 430 | return (access_flags_ & kAccFinal) != 0; |
| 431 | } |
| 432 | |
| 433 | // Returns true if the method is declared native. |
| 434 | bool IsNative() const { |
| 435 | return (access_flags_ & kAccNative) != 0; |
| 436 | } |
| 437 | |
| 438 | // Returns true if the method is declared abstract. |
| 439 | bool IsAbstract() const { |
| 440 | return (access_flags_ & kAccAbstract) != 0; |
| 441 | } |
| 442 | |
| 443 | bool IsSynthetic() const { |
| 444 | return (access_flags_ & kAccSynthetic) != 0; |
| 445 | } |
| 446 | |
| 447 | // Number of argument registers required by the prototype. |
| 448 | uint32_t NumArgRegisters(); |
| 449 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 450 | // Number of argument bytes required for densely packing the |
| 451 | // arguments into an array of arguments. |
| 452 | size_t NumArgArrayBytes(); |
| 453 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 454 | public: // TODO: private |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 455 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 456 | // the class we are a part of |
| 457 | Class* declaring_class_; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 458 | ObjectArray<Class>* java_exception_types_; |
| 459 | Object* java_formal_type_parameters_; |
| 460 | Object* java_generic_exception_types_; |
| 461 | Object* java_generic_parameter_types_; |
| 462 | Object* java_generic_return_type_; |
| 463 | Class* java_return_type_; |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 464 | String* name_; |
Jesse Wilson | 46cdd4b | 2011-07-28 17:40:48 -0400 | [diff] [blame] | 465 | ObjectArray<Class>* java_parameter_types_; |
| 466 | uint32_t java_generic_types_are_initialized_; |
| 467 | uint32_t java_slot_; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 468 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 469 | const StringPiece& GetShorty() const { |
| 470 | return shorty_; |
| 471 | } |
| 472 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 473 | bool IsReturnAReference() const { |
| 474 | return (shorty_[0] == 'L') || (shorty_[0] == '['); |
| 475 | } |
| 476 | |
| 477 | bool IsReturnAFloatOrDouble() const { |
| 478 | return (shorty_[0] == 'F') || (shorty_[0] == 'D'); |
| 479 | } |
| 480 | |
| 481 | bool IsReturnAFloat() const { |
| 482 | return shorty_[0] == 'F'; |
| 483 | } |
| 484 | |
| 485 | bool IsReturnADouble() const { |
| 486 | return shorty_[0] == 'D'; |
| 487 | } |
| 488 | |
| 489 | bool IsReturnALong() const { |
| 490 | return shorty_[0] == 'J'; |
| 491 | } |
| 492 | |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 493 | bool IsReturnVoid() const { |
| 494 | return shorty_[0] == 'V'; |
| 495 | } |
| 496 | |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 497 | // "Args" may refer to any of the 3 levels of "Args." |
| 498 | // To avoid confusion, our code will denote which "Args" clearly: |
| 499 | // 1. UserArgs: Args that a user see. |
| 500 | // 2. Args: Logical JVM-level Args. E.g., the first in Args will be the |
| 501 | // receiver. |
| 502 | // 3. CConvArgs: Calling Convention Args, which is physical-level Args. |
| 503 | // E.g., the first in Args is Method* for both static and non-static |
| 504 | // methods. And CConvArgs doesn't deal with the receiver because |
| 505 | // receiver is hardwired in an implicit register, so CConvArgs doesn't |
| 506 | // need to deal with it. |
| 507 | // |
| 508 | // The number of Args that should be supplied to this method |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 509 | size_t NumArgs() const { |
Shih-wei Liao | 5381cf9 | 2011-07-27 00:28:04 -0700 | [diff] [blame] | 510 | // "1 +" because the first in Args is the receiver. |
| 511 | // "- 1" because we don't count the return type. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 512 | return (IsStatic() ? 0 : 1) + shorty_.length() - 1; |
| 513 | } |
| 514 | |
| 515 | // The number of reference arguments to this method including implicit this |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 516 | // pointer. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 517 | size_t NumReferenceArgs() const; |
| 518 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 519 | // The number of long or double arguments. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 520 | size_t NumLongOrDoubleArgs() const; |
| 521 | |
| 522 | // The number of reference arguments to this method before the given |
| 523 | // parameter index |
| 524 | size_t NumReferenceArgsBefore(unsigned int param) const; |
| 525 | |
| 526 | // Is the given method parameter a reference? |
| 527 | bool IsParamAReference(unsigned int param) const; |
| 528 | |
| 529 | // Is the given method parameter a long or double? |
| 530 | bool IsParamALongOrDouble(unsigned int param) const; |
| 531 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 532 | // Size in bytes of the given parameter |
| 533 | size_t ParamSize(unsigned int param) const; |
| 534 | |
| 535 | // Size in bytes of the return value |
| 536 | size_t ReturnSize() const; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 537 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 538 | const void* GetCode() const { |
| 539 | return code_; |
| 540 | } |
| 541 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 542 | void SetCode(const void* code) { |
| 543 | code_ = code; |
| 544 | } |
| 545 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 546 | static size_t GetCodeOffset() { |
| 547 | return OFFSETOF_MEMBER(Method, code_); |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void RegisterNative(const void* native_method) { |
| 551 | native_method_ = native_method; |
| 552 | } |
| 553 | |
| 554 | static MemberOffset NativeMethodOffset() { |
| 555 | return MemberOffset(OFFSETOF_MEMBER(Method, native_method_)); |
| 556 | } |
| 557 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 558 | InvokeStub* GetInvokeStub() const { |
| 559 | return invoke_stub_; |
| 560 | } |
| 561 | |
| 562 | void SetInvokeStub(const InvokeStub* invoke_stub) { |
| 563 | invoke_stub_ = invoke_stub; |
| 564 | } |
| 565 | |
| 566 | static size_t GetInvokeStubOffset() { |
| 567 | return OFFSETOF_MEMBER(Method, invoke_stub_); |
| 568 | } |
| 569 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 570 | bool HasSameNameAndDescriptor(const Method* that) const; |
| 571 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 572 | public: // TODO: private/const |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 573 | // access flags; low 16 bits are defined by spec (could be uint16_t?) |
| 574 | uint32_t access_flags_; |
| 575 | |
| 576 | // For concrete virtual methods, this is the offset of the method |
| 577 | // in "vtable". |
| 578 | // |
| 579 | // For abstract methods in an interface class, this is the offset |
| 580 | // of the method in "iftable[n]->methodIndexArray". |
| 581 | uint16_t method_index_; |
| 582 | |
| 583 | // Method bounds; not needed for an abstract method. |
| 584 | // |
| 585 | // For a native method, we compute the size of the argument list, and |
| 586 | // set "insSize" and "registerSize" equal to it. |
| 587 | uint16_t num_registers_; // ins + locals |
| 588 | uint16_t num_outs_; |
| 589 | uint16_t num_ins_; |
| 590 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 591 | // The method descriptor. This represents the parameters a method |
| 592 | // takes and value it returns. This string is a list of the type |
| 593 | // descriptors for the parameters enclosed in parenthesis followed |
| 594 | // by the return type descriptor. For example, for the method |
| 595 | // |
| 596 | // Object mymethod(int i, double d, Thread t) |
| 597 | // |
| 598 | // the method descriptor would be |
| 599 | // |
| 600 | // (IDLjava/lang/Thread;)Ljava/lang/Object; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 601 | String* signature_; |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 602 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 603 | // Method prototype descriptor string (return and argument types). |
| 604 | uint32_t proto_idx_; |
| 605 | |
Shih-wei Liao | 2fb9753 | 2011-08-11 16:17:23 -0700 | [diff] [blame] | 606 | // Offset to the CodeItem. |
| 607 | uint32_t code_off_; |
| 608 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 609 | // The short-form method descriptor string. |
| 610 | StringPiece shorty_; |
| 611 | |
Brian Carlstrom | c4fa2c0 | 2011-08-21 03:00:12 -0700 | [diff] [blame] | 612 | // short cuts to declaring_class_->dex_cache_ members for fast compiled code access |
| 613 | ObjectArray<String>* dex_cache_strings_; |
| 614 | ObjectArray<Class>* dex_cache_classes_; |
| 615 | ObjectArray<Method>* dex_cache_methods_; |
| 616 | ObjectArray<Field>* dex_cache_fields_; |
| 617 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 618 | private: |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 619 | // Compiled code associated with this method |
| 620 | const void* code_; |
| 621 | |
| 622 | // Any native method registered with this method |
| 623 | const void* native_method_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 624 | |
Carl Shapiro | 9b9ba28 | 2011-08-14 15:30:39 -0700 | [diff] [blame] | 625 | // Native invocation stub entry point. |
| 626 | const InvokeStub* invoke_stub_; |
| 627 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 628 | DISALLOW_IMPLICIT_CONSTRUCTORS(Method); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 629 | }; |
| 630 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 631 | class Array : public Object { |
| 632 | public: |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 633 | static size_t SizeOf(size_t component_count, |
| 634 | size_t component_size) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 635 | return sizeof(Array) + component_count * component_size; |
| 636 | } |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 637 | |
Elliott Hughes | 68f4fa0 | 2011-08-21 10:46:59 -0700 | [diff] [blame] | 638 | // A convenience for code that doesn't know the component size, |
| 639 | // and doesn't want to have to work it out itself. |
| 640 | static Array* Alloc(Class* array_class, size_t component_count); |
| 641 | |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 642 | static Array* Alloc(Class* array_class, |
| 643 | size_t component_count, |
| 644 | size_t component_size) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 645 | size_t size = SizeOf(component_count, component_size); |
| 646 | Array* array = down_cast<Array*>(Heap::AllocObject(array_class, size)); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 647 | if (array != NULL) { |
| 648 | array->SetLength(component_count); |
| 649 | } |
| 650 | return array; |
| 651 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 652 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 653 | size_t SizeOf() const; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 654 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 655 | int32_t GetLength() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 656 | return length_; |
| 657 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 658 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 659 | void SetLength(uint32_t length) { |
| 660 | length_ = length; |
| 661 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 662 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 663 | protected: |
| 664 | bool IsValidIndex(int32_t index) const { |
| 665 | if (index < 0 || index >= length_) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 666 | Thread* self = Thread::Current(); |
| 667 | self->ThrowNewException("Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 668 | "length=%i; index=%i", length_, index); |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 669 | return false; |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 670 | } |
| 671 | return true; |
| 672 | } |
| 673 | |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 674 | private: |
| 675 | // The number of array elements. |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 676 | int32_t length_; |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 677 | // Padding to ensure the first member defined by a subclass begins on a 8-byte boundary |
| 678 | int32_t padding_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 679 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 680 | DISALLOW_IMPLICIT_CONSTRUCTORS(Array); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 681 | }; |
| 682 | |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 683 | template<class T> |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 684 | class ObjectArray : public Array { |
| 685 | public: |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 686 | static ObjectArray<T>* Alloc(Class* object_array_class, |
| 687 | size_t length) { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 688 | return Array::Alloc(object_array_class, length, sizeof(uint32_t))->AsObjectArray<T>(); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 691 | T* const * GetData() const { |
| 692 | return reinterpret_cast<T* const *>(&elements_); |
| 693 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 694 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 695 | T** GetData() { |
| 696 | return reinterpret_cast<T**>(&elements_); |
| 697 | } |
Jesse Wilson | df4189c | 2011-08-09 17:10:28 -0400 | [diff] [blame] | 698 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 699 | T* Get(int32_t i) const { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 700 | if (!IsValidIndex(i)) { |
| 701 | return NULL; |
| 702 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 703 | return GetData()[i]; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 704 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 705 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 706 | void Set(int32_t i, T* object) { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 707 | if (IsValidIndex(i)) { |
| 708 | // TODO: ArrayStoreException |
| 709 | GetData()[i] = object; // TODO: write barrier |
| 710 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 711 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 712 | |
| 713 | static void Copy(ObjectArray<T>* src, int src_pos, |
| 714 | ObjectArray<T>* dst, int dst_pos, |
| 715 | size_t length) { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 716 | for (size_t i = 0; i < length; i++) { |
| 717 | dst->Set(dst_pos + i, src->Get(src_pos + i)); |
| 718 | } |
| 719 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 720 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 721 | ObjectArray<T>* CopyOf(int32_t new_length) { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 722 | ObjectArray<T>* new_array = Alloc(klass_, new_length); |
| 723 | Copy(this, 0, new_array, 0, std::min(GetLength(), new_length)); |
| 724 | return new_array; |
| 725 | } |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 726 | |
| 727 | private: |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 728 | // Location of first element. |
| 729 | T* elements_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 730 | |
| 731 | DISALLOW_IMPLICIT_CONSTRUCTORS(ObjectArray); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 732 | }; |
| 733 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 734 | // ClassLoader objects. |
| 735 | class ClassLoader : public Object { |
| 736 | public: |
| 737 | std::vector<const DexFile*>& GetClassPath() { |
| 738 | return class_path_; |
| 739 | } |
| 740 | void SetClassPath(std::vector<const DexFile*>& class_path) { |
| 741 | DCHECK_EQ(0U, class_path_.size()); |
| 742 | class_path_ = class_path; |
| 743 | } |
| 744 | |
| 745 | private: |
| 746 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 747 | Object* packages_; |
| 748 | ClassLoader* parent_; |
| 749 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 750 | // TODO: remove once we can create a real PathClassLoader |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 751 | std::vector<const DexFile*> class_path_; |
| 752 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 753 | DISALLOW_IMPLICIT_CONSTRUCTORS(ClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 754 | }; |
| 755 | |
| 756 | class BaseDexClassLoader : public ClassLoader { |
| 757 | private: |
| 758 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 759 | String* original_path_; |
| 760 | Object* path_list_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 761 | DISALLOW_IMPLICIT_CONSTRUCTORS(BaseDexClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 762 | }; |
| 763 | |
| 764 | class PathClassLoader : public BaseDexClassLoader { |
| 765 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 766 | DISALLOW_IMPLICIT_CONSTRUCTORS(PathClassLoader); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 767 | }; |
| 768 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 769 | // Class objects. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 770 | class Class : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 771 | public: |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 772 | |
| 773 | // Class Status |
| 774 | // |
| 775 | // kStatusNotReady: If a Class cannot be found in the class table by |
| 776 | // FindClass, it allocates an new one with AllocClass in the |
| 777 | // kStatusNotReady and calls LoadClass. Note if it does find a |
| 778 | // class, it may not be kStatusResolved and it will try to push it |
| 779 | // forward toward kStatusResolved. |
| 780 | // |
| 781 | // kStatusIdx: LoadClass populates with Class with information from |
| 782 | // the DexFile, moving the status to kStatusIdx, indicating that the |
| 783 | // Class values in super_class_ and interfaces_ have not been |
| 784 | // populated based on super_class_idx_ and interfaces_idx_. The new |
| 785 | // Class can then be inserted into the classes table. |
| 786 | // |
| 787 | // kStatusLoaded: After taking a lock on Class, the ClassLinker will |
| 788 | // attempt to move a kStatusIdx class forward to kStatusLoaded by |
| 789 | // using ResolveClass to initialize the super_class_ and interfaces_. |
| 790 | // |
| 791 | // kStatusResolved: Still holding the lock on Class, the ClassLinker |
| 792 | // will use LinkClass to link all members, creating Field and Method |
| 793 | // objects, setting up the vtable, etc. On success, the class is |
| 794 | // marked kStatusResolved. |
| 795 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 796 | enum Status { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 797 | kStatusError = -1, |
| 798 | kStatusNotReady = 0, |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 799 | kStatusIdx = 1, // loaded, DEX idx in super_class_idx_ and interfaces_idx_ |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 800 | kStatusLoaded = 2, // DEX idx values resolved |
| 801 | kStatusResolved = 3, // part of linking |
| 802 | kStatusVerifying = 4, // in the process of being verified |
| 803 | kStatusVerified = 5, // logically part of linking; done pre-init |
| 804 | kStatusInitializing = 6, // class init in progress |
| 805 | kStatusInitialized = 7, // ready to go |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | enum PrimitiveType { |
| 809 | kPrimNot = -1 |
| 810 | }; |
| 811 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 812 | Object* NewInstance() { |
| 813 | return Heap::AllocObject(this, this->object_size_); |
| 814 | } |
| 815 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 816 | Class* GetSuperClass() const { |
| 817 | return super_class_; |
| 818 | } |
| 819 | |
| 820 | uint32_t GetSuperClassIdx() const { |
| 821 | return super_class_idx_; |
| 822 | } |
| 823 | |
| 824 | bool HasSuperClass() const { |
| 825 | return super_class_ != NULL; |
| 826 | } |
| 827 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 828 | bool IsAssignableFrom(const Class* klass) const { |
| 829 | DCHECK(klass != NULL); |
| 830 | if (this == klass) { |
| 831 | return true; |
| 832 | } |
| 833 | if (IsInterface()) { |
| 834 | return klass->Implements(this); |
| 835 | } |
| 836 | if (klass->IsArray()) { |
| 837 | return IsAssignableFromArray(klass); |
| 838 | } |
| 839 | return klass->IsSubClass(this); |
| 840 | } |
| 841 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 842 | ClassLoader* GetClassLoader() const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 843 | return class_loader_; |
| 844 | } |
| 845 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 846 | DexCache* GetDexCache() const { |
| 847 | return dex_cache_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | Class* GetComponentType() const { |
| 851 | return component_type_; |
| 852 | } |
| 853 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 854 | static size_t GetTypeSize(String* descriptor); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 855 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 856 | size_t GetComponentSize() const { |
| 857 | return GetTypeSize(component_type_->descriptor_); |
| 858 | } |
| 859 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 860 | const String* GetDescriptor() const { |
| 861 | DCHECK(descriptor_ != NULL); |
| 862 | // DCHECK_NE(0, descriptor_->GetLength()); // TODO: keep? |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 863 | return descriptor_; |
| 864 | } |
| 865 | |
| 866 | Status GetStatus() const { |
| 867 | return status_; |
| 868 | } |
| 869 | |
| 870 | void SetStatus(Status new_status) { |
| 871 | // TODO: validate transition |
| 872 | status_ = new_status; |
| 873 | } |
| 874 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 875 | // Returns true if the class has failed to link. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 876 | bool IsErroneous() const { |
| 877 | return GetStatus() == kStatusError; |
| 878 | } |
| 879 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 880 | // Returns true if the class has been verified. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 881 | bool IsVerified() const { |
| 882 | return GetStatus() >= kStatusVerified; |
| 883 | } |
| 884 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 885 | // Returns true if the class has been linked. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 886 | bool IsLinked() const { |
| 887 | return GetStatus() >= kStatusResolved; |
| 888 | } |
| 889 | |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 890 | // Returns true if the class has been loaded. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 891 | bool IsLoaded() const { |
| 892 | return GetStatus() >= kStatusLoaded; |
| 893 | } |
| 894 | |
Carl Shapiro | 83ab4f3 | 2011-08-15 20:21:39 -0700 | [diff] [blame] | 895 | // Returns true if the class is initialized. |
| 896 | bool IsInitialized() const { |
| 897 | return GetStatus() == kStatusInitialized; |
| 898 | } |
| 899 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 900 | // Returns true if this class is in the same packages as that class. |
| 901 | bool IsInSamePackage(const Class* that) const; |
| 902 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 903 | static bool IsInSamePackage(const String* descriptor1, |
| 904 | const String* descriptor2); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 905 | |
| 906 | // Returns true if this class represents an array class. |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 907 | bool IsArray() const; |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 908 | |
| 909 | // Returns true if the class is an interface. |
| 910 | bool IsInterface() const { |
| 911 | return (access_flags_ & kAccInterface) != 0; |
| 912 | } |
| 913 | |
| 914 | // Returns true if the class is declared public. |
| 915 | bool IsPublic() const { |
| 916 | return (access_flags_ & kAccPublic) != 0; |
| 917 | } |
| 918 | |
| 919 | // Returns true if the class is declared final. |
| 920 | bool IsFinal() const { |
| 921 | return (access_flags_ & kAccFinal) != 0; |
| 922 | } |
| 923 | |
| 924 | // Returns true if the class is abstract. |
| 925 | bool IsAbstract() const { |
| 926 | return (access_flags_ & kAccAbstract) != 0; |
| 927 | } |
| 928 | |
| 929 | // Returns true if the class is an annotation. |
| 930 | bool IsAnnotation() const { |
| 931 | return (access_flags_ & kAccAnnotation) != 0; |
| 932 | } |
| 933 | |
| 934 | // Returns true if the class is a primitive type. |
| 935 | bool IsPrimitive() const { |
| 936 | return primitive_type_ != kPrimNot; |
| 937 | } |
| 938 | |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 939 | // Returns true if the class is synthetic. |
| 940 | bool IsSynthetic() const { |
| 941 | return (access_flags_ & kAccSynthetic) != 0; |
| 942 | } |
| 943 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 944 | // Returns true if this class can access that class. |
| 945 | bool CanAccess(const Class* that) const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 946 | return that->IsPublic() || this->IsInSamePackage(that); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 947 | } |
| 948 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 949 | // Returns the number of static, private, and constructor methods. |
| 950 | size_t NumDirectMethods() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 951 | return (direct_methods_ != NULL) ? direct_methods_->GetLength() : 0; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 952 | } |
| 953 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 954 | Method* GetDirectMethod(int32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 955 | DCHECK_NE(NumDirectMethods(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 956 | return direct_methods_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | void SetDirectMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 960 | DCHECK_NE(NumDirectMethods(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 961 | direct_methods_->Set(i, f); |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 964 | Method* FindDeclaredDirectMethod(const StringPiece& name, |
| 965 | const StringPiece& descriptor); |
| 966 | |
| 967 | Method* FindDirectMethod(const StringPiece& name, |
| 968 | const StringPiece& descriptor); |
| 969 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 970 | // Returns the number of non-inherited virtual methods. |
| 971 | size_t NumVirtualMethods() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 972 | return (virtual_methods_ != NULL) ? virtual_methods_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | Method* GetVirtualMethod(uint32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 976 | DCHECK_NE(NumVirtualMethods(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 977 | return virtual_methods_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | void SetVirtualMethod(uint32_t i, Method* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 981 | DCHECK_NE(NumVirtualMethods(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 982 | virtual_methods_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 985 | Method* FindDeclaredVirtualMethod(const StringPiece& name, |
| 986 | const StringPiece& descriptor); |
| 987 | |
| 988 | Method* FindVirtualMethod(const StringPiece& name, |
| 989 | const StringPiece& descriptor); |
| 990 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 991 | size_t NumInstanceFields() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 992 | return (ifields_ != NULL) ? ifields_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 993 | } |
| 994 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 995 | // Returns the number of instance fields containing reference types. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 996 | size_t NumReferenceInstanceFields() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 997 | return num_reference_instance_fields_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Elliott Hughes | cdf5312 | 2011-08-19 15:46:09 -0700 | [diff] [blame] | 1000 | // Finds the given instance field in this class or a superclass. |
| 1001 | Field* FindInstanceField(const StringPiece& name, |
| 1002 | const StringPiece& descriptor); |
| 1003 | |
| 1004 | Field* FindDeclaredInstanceField(const StringPiece& name, |
| 1005 | const StringPiece& descriptor); |
| 1006 | |
| 1007 | // Finds the given static field in this class or a superclass. |
| 1008 | Field* FindStaticField(const StringPiece& name, |
| 1009 | const StringPiece& descriptor); |
| 1010 | |
| 1011 | Field* FindDeclaredStaticField(const StringPiece& name, |
| 1012 | const StringPiece& descriptor); |
| 1013 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1014 | Field* GetInstanceField(uint32_t i) const { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1015 | DCHECK_NE(NumInstanceFields(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1016 | return ifields_->Get(i); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1019 | void SetInstanceField(uint32_t i, Field* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1020 | DCHECK_NE(NumInstanceFields(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1021 | ifields_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | size_t NumStaticFields() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1025 | return (sfields_ != NULL) ? sfields_->GetLength() : 0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1028 | Field* GetStaticField(uint32_t i) const { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1029 | DCHECK_NE(NumStaticFields(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1030 | return sfields_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1033 | void SetStaticField(uint32_t i, Field* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1034 | DCHECK_NE(NumStaticFields(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1035 | sfields_->Set(i, f); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | uint32_t GetReferenceOffsets() const { |
| 1039 | return reference_offsets_; |
| 1040 | } |
| 1041 | |
| 1042 | void SetReferenceOffsets(uint32_t new_reference_offsets) { |
| 1043 | reference_offsets_ = new_reference_offsets; |
| 1044 | } |
| 1045 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1046 | size_t NumInterfaces() const { |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1047 | return (interfaces_ != NULL) ? interfaces_->GetLength() : 0; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | Class* GetInterface(uint32_t i) const { |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1051 | DCHECK_NE(NumInterfaces(), 0U); |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1052 | return interfaces_->Get(i); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | void SetInterface(uint32_t i, Class* f) { // TODO: uint16_t |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1056 | DCHECK_NE(NumInterfaces(), 0U); |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1057 | interfaces_->Set(i, f); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1058 | } |
| 1059 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1060 | void SetVerifyErrorClass(Class* klass) { |
| 1061 | // Note SetFieldObject is used rather than verify_error_class_ directly for the barrier |
| 1062 | size_t field_offset = OFFSETOF_MEMBER(Class, verify_error_class_); |
| 1063 | klass->SetFieldObject(field_offset, klass); |
| 1064 | } |
| 1065 | |
| 1066 | private: |
| 1067 | bool Implements(const Class* klass) const; |
| 1068 | bool IsArrayAssignableFromArray(const Class* klass) const; |
| 1069 | bool IsAssignableFromArray(const Class* klass) const; |
| 1070 | bool IsSubClass(const Class* klass) const; |
| 1071 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1072 | public: // TODO: private |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1073 | // descriptor for the class such as "java.lang.Class" or "[C" |
| 1074 | String* name_; // TODO initialize |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1075 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1076 | // descriptor for the class such as "Ljava/lang/Class;" or "[C" |
| 1077 | String* descriptor_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1078 | |
| 1079 | // access flags; low 16 bits are defined by VM spec |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1080 | uint32_t access_flags_; // TODO: make an instance field? |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1081 | |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1082 | // DexCache of resolved constant pool entries |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1083 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 1084 | DexCache* dex_cache_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1085 | |
| 1086 | // state of class initialization |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1087 | Status status_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1088 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1089 | // If class verify fails, we must return same error on subsequent tries. |
| 1090 | // Update with SetVerifyErrorClass to ensure a write barrier is used. |
| 1091 | const Class* verify_error_class_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1092 | |
| 1093 | // threadId, used to check for recursive <clinit> invocation |
| 1094 | uint32_t clinit_thread_id_; |
| 1095 | |
| 1096 | // Total object size; used when allocating storage on gc heap. (For |
| 1097 | // interfaces and abstract classes this will be zero.) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1098 | size_t object_size_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1099 | |
| 1100 | // For array classes, the class object for base element, for |
| 1101 | // instanceof/checkcast (for String[][][], this will be String). |
| 1102 | // Otherwise, NULL. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1103 | Class* component_type_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1104 | |
| 1105 | // For array classes, the number of array dimensions, e.g. int[][] |
| 1106 | // is 2. Otherwise 0. |
| 1107 | int32_t array_rank_; |
| 1108 | |
| 1109 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 1110 | PrimitiveType primitive_type_; |
| 1111 | |
| 1112 | // The superclass, or NULL if this is java.lang.Object or a |
| 1113 | // primitive type. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1114 | Class* super_class_; // TODO: make an instance field |
| 1115 | uint32_t super_class_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1116 | |
| 1117 | // defining class loader, or NULL for the "bootstrap" system loader |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1118 | ClassLoader* class_loader_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1119 | |
| 1120 | // initiating class loader list |
| 1121 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 1122 | // values are kept in a table in gDvm. |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 1123 | // InitiatingLoaderList initiating_loader_list_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1124 | |
| 1125 | // array of interfaces this class implements directly |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1126 | ObjectArray<Class>* interfaces_; |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1127 | uint32_t* interfaces_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1128 | |
| 1129 | // static, private, and <init> methods |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1130 | ObjectArray<Method>* direct_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1131 | |
| 1132 | // virtual methods defined in this class; invoked through vtable |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1133 | ObjectArray<Method>* virtual_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1134 | |
| 1135 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 1136 | // vtable from the superclass is copied in, and virtual methods from |
| 1137 | // our class either replace those from the super or are appended. |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 1138 | ObjectArray<Method>* vtable_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1139 | |
| 1140 | // Interface table (iftable), one entry per interface supported by |
| 1141 | // this class. That means one entry for each interface we support |
| 1142 | // directly, indirectly via superclass, or indirectly via |
| 1143 | // superinterface. This will be null if neither we nor our |
| 1144 | // superclass implement any interfaces. |
| 1145 | // |
| 1146 | // Why we need this: given "class Foo implements Face", declare |
| 1147 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 1148 | // is part of the Face interface. We can't easily use a single |
| 1149 | // vtable. |
| 1150 | // |
| 1151 | // For every interface a concrete class implements, we create a list |
| 1152 | // of virtualMethod indices for the methods in the interface. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1153 | size_t iftable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1154 | InterfaceEntry* iftable_; |
| 1155 | |
| 1156 | // The interface vtable indices for iftable get stored here. By |
| 1157 | // placing them all in a single pool for each class that implements |
| 1158 | // interfaces, we decrease the number of allocations. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1159 | size_t ifvi_pool_count_; |
| 1160 | uint32_t* ifvi_pool_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1161 | |
| 1162 | // instance fields |
| 1163 | // |
| 1164 | // These describe the layout of the contents of a |
| 1165 | // DataObject-compatible Object. Note that only the fields directly |
| 1166 | // declared by this class are listed in ifields; fields declared by |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 1167 | // a superclass are listed in the superclass's Class.ifields. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1168 | // |
| 1169 | // All instance fields that refer to objects are guaranteed to be at |
Brian Carlstrom | 913af1b | 2011-07-23 21:41:13 -0700 | [diff] [blame] | 1170 | // the beginning of the field list. num_reference_instance_fields_ |
| 1171 | // specifies the number of reference fields. |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1172 | ObjectArray<Field>* ifields_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1173 | |
| 1174 | // number of fields that are object refs |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1175 | size_t num_reference_instance_fields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1176 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1177 | // Bitmap of offsets of ifields. |
| 1178 | uint32_t reference_offsets_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1179 | |
| 1180 | // source file name, if known. Otherwise, NULL. |
| 1181 | const char* source_file_; |
| 1182 | |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1183 | // Static fields |
Jesse Wilson | 35baaab | 2011-08-10 16:18:03 -0400 | [diff] [blame] | 1184 | ObjectArray<Field>* sfields_; |
Jesse Wilson | 7833bd2 | 2011-08-09 18:31:44 -0400 | [diff] [blame] | 1185 | |
| 1186 | // static field storage |
| 1187 | // |
| 1188 | // Each static field is stored in one of three arrays: |
| 1189 | // o references are stored in static_references_ |
| 1190 | // o doubles and longs are stored in static_64bit_primitives_ |
| 1191 | // o everything else is in static_32bit_primitives_ |
| 1192 | // Static fields select their array using their type and their index using the |
| 1193 | // Field->slot_ member. Storing static fields in arrays avoids the need for a |
| 1194 | // special case in the GC. |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1195 | ObjectArray<Object>* static_references_; |
| 1196 | IntArray* static_32bit_primitives_; |
| 1197 | LongArray* static_64bit_primitives_; |
| 1198 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1199 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1200 | DISALLOW_IMPLICIT_CONSTRUCTORS(Class); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1201 | }; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 1202 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1203 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1204 | inline bool Object::InstanceOf(const Class* klass) const { |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 1205 | DCHECK(klass != NULL); |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1206 | DCHECK(klass_ != NULL); |
| 1207 | return klass->IsAssignableFrom(klass_); |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1210 | inline bool Object::IsClass() const { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1211 | Class* java_lang_Class = klass_->klass_; |
| 1212 | return klass_ == java_lang_Class; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | inline bool Object::IsObjectArray() const { |
| 1216 | return IsArray() && !klass_->component_type_->IsPrimitive(); |
| 1217 | } |
| 1218 | |
| 1219 | inline bool Object::IsArray() const { |
| 1220 | return klass_->IsArray(); |
| 1221 | } |
| 1222 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1223 | inline bool Object::IsField() const { |
| 1224 | Class* java_lang_Class = klass_->klass_; |
| 1225 | Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->klass_; |
| 1226 | return klass_ == java_lang_reflect_Field; |
| 1227 | } |
| 1228 | |
| 1229 | inline bool Object::IsMethod() const { |
| 1230 | Class* java_lang_Class = klass_->klass_; |
| 1231 | Class* java_lang_reflect_Method = java_lang_Class->GetDirectMethod(0)->klass_; |
| 1232 | return klass_ == java_lang_reflect_Method; |
| 1233 | } |
| 1234 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1235 | inline size_t Object::SizeOf() const { |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1236 | if (IsArray()) { |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1237 | return AsArray()->SizeOf(); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1238 | } |
| 1239 | return klass_->object_size_; |
| 1240 | } |
| 1241 | |
Elliott Hughes | 04b63fd | 2011-08-16 09:40:10 -0700 | [diff] [blame] | 1242 | inline size_t Array::SizeOf() const { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1243 | return SizeOf(GetLength(), klass_->GetComponentSize()); |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 1244 | } |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1245 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1246 | class DataObject : public Object { |
| 1247 | public: |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1248 | uint32_t fields_[0]; |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1249 | private: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1250 | DISALLOW_IMPLICIT_CONSTRUCTORS(DataObject); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1251 | }; |
| 1252 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1253 | template<class T> |
| 1254 | class PrimitiveArray : public Array { |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1255 | public: |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1256 | typedef T ElementType; |
| 1257 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1258 | static PrimitiveArray<T>* Alloc(size_t length); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1259 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1260 | const T* GetData() const { |
| 1261 | return reinterpret_cast<const T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1262 | } |
| 1263 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1264 | T* GetData() { |
| 1265 | return reinterpret_cast<T*>(&elements_); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1266 | } |
| 1267 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1268 | T Get(int32_t i) const { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1269 | if (!IsValidIndex(i)) { |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1270 | return T(0); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1271 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1272 | return GetData()[i]; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1273 | } |
| 1274 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1275 | void Set(int32_t i, T value) { |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1276 | // TODO: ArrayStoreException |
| 1277 | if (IsValidIndex(i)) { |
| 1278 | GetData()[i] = value; |
| 1279 | } |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1282 | static void SetArrayClass(Class* array_class) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1283 | CHECK(array_class_ == NULL); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1284 | CHECK(array_class != NULL); |
| 1285 | array_class_ = array_class; |
| 1286 | } |
| 1287 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1288 | static void ResetArrayClass() { |
| 1289 | CHECK(array_class_ != NULL); |
| 1290 | array_class_ = NULL; |
| 1291 | } |
| 1292 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1293 | private: |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1294 | // Location of first element. |
| 1295 | T elements_[0]; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1296 | |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1297 | static Class* array_class_; |
| 1298 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1299 | DISALLOW_IMPLICIT_CONSTRUCTORS(PrimitiveArray); |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1300 | }; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 1301 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1302 | class String : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1303 | public: |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1304 | const CharArray* GetCharArray() const { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1305 | DCHECK(array_ != NULL); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1306 | return array_; |
| 1307 | } |
| 1308 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1309 | uint32_t GetHashCode() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1310 | return hash_code_; |
| 1311 | } |
| 1312 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1313 | uint32_t GetOffset() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1314 | return offset_; |
| 1315 | } |
| 1316 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1317 | uint32_t GetLength() const { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1318 | return count_; |
| 1319 | } |
| 1320 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1321 | // TODO: do we need this? Equals is the only caller, and could |
| 1322 | // bounds check itself. |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1323 | uint16_t CharAt(int32_t index) const { |
| 1324 | if (index < 0 || index >= count_) { |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1325 | Thread* self = Thread::Current(); |
| 1326 | self->ThrowNewException("Ljava/lang/StringIndexOutOfBoundsException;", |
| 1327 | "length=%i; index=%i", count_, index); |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1328 | return 0; |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1329 | } |
Elliott Hughes | 710a0cb | 2011-08-16 14:32:37 -0700 | [diff] [blame] | 1330 | return GetCharArray()->Get(index + GetOffset()); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1333 | static String* AllocFromUtf16(int32_t utf16_length, |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1334 | const uint16_t* utf16_data_in, |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1335 | int32_t hash_code) { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1336 | String* string = Alloc(GetJavaLangString(), |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1337 | utf16_length); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1338 | // TODO: use 16-bit wide memset variant |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1339 | for (int i = 0; i < utf16_length; i++ ) { |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1340 | string->array_->Set(i, utf16_data_in[i]); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1341 | } |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1342 | string->ComputeHashCode(); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1343 | return string; |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame] | 1346 | static String* AllocFromModifiedUtf8(const char* utf) { |
| 1347 | size_t char_count = ModifiedUtf8Len(utf); |
| 1348 | return AllocFromModifiedUtf8(char_count, utf); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 1351 | static String* AllocFromModifiedUtf8(int32_t utf16_length, |
| 1352 | const char* utf8_data_in) { |
Elliott Hughes | bfaadc8 | 2011-08-18 17:36:58 -0700 | [diff] [blame] | 1353 | String* string = Alloc(GetJavaLangString(), utf16_length); |
| 1354 | uint16_t* utf16_data_out = string->array_->GetData(); |
| 1355 | ConvertModifiedUtf8ToUtf16(utf16_data_out, utf8_data_in); |
| 1356 | string->ComputeHashCode(); |
| 1357 | return string; |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 1358 | } |
| 1359 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1360 | static void SetClass(Class* java_lang_String); |
| 1361 | static void ResetClass(); |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1362 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1363 | static String* Alloc(Class* java_lang_String, |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1364 | int32_t utf16_length) { |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1365 | return Alloc(java_lang_String, CharArray::Alloc(utf16_length)); |
| 1366 | } |
| 1367 | |
| 1368 | static String* Alloc(Class* java_lang_String, |
| 1369 | CharArray* array) { |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame] | 1370 | String* string = down_cast<String*>(java_lang_String->NewInstance()); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1371 | string->array_ = array; |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1372 | string->count_ = array->GetLength(); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1373 | return string; |
| 1374 | } |
| 1375 | |
| 1376 | // Convert Modified UTF-8 to UTF-16 |
| 1377 | // http://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 |
| 1378 | static void ConvertModifiedUtf8ToUtf16(uint16_t* utf16_data_out, const char* utf8_data_in) { |
| 1379 | while (*utf8_data_in != '\0') { |
| 1380 | *utf16_data_out++ = GetUtf16FromUtf8(&utf8_data_in); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | // Retrieve the next UTF-16 character from a UTF-8 string. |
| 1385 | // |
| 1386 | // Advances "*pUtf8Ptr" to the start of the next character. |
| 1387 | // |
| 1388 | // WARNING: If a string is corrupted by dropping a '\0' in the middle |
| 1389 | // of a 3-byte sequence, you can end up overrunning the buffer with |
| 1390 | // reads (and possibly with the writes if the length was computed and |
| 1391 | // cached before the damage). For performance reasons, this function |
| 1392 | // assumes that the string being parsed is known to be valid (e.g., by |
| 1393 | // already being verified). Most strings we process here are coming |
| 1394 | // out of dex files or other internal translations, so the only real |
| 1395 | // risk comes from the JNI NewStringUTF call. |
| 1396 | static uint16_t GetUtf16FromUtf8(const char** utf8_data_in) { |
| 1397 | uint8_t one = *(*utf8_data_in)++; |
| 1398 | if ((one & 0x80) == 0) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1399 | // one-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1400 | return one; |
| 1401 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1402 | // two- or three-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1403 | uint8_t two = *(*utf8_data_in)++; |
| 1404 | if ((one & 0x20) == 0) { |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1405 | // two-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1406 | return ((one & 0x1f) << 6) | |
| 1407 | (two & 0x3f); |
| 1408 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1409 | // three-byte encoding |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1410 | uint8_t three = *(*utf8_data_in)++; |
| 1411 | return ((one & 0x0f) << 12) | |
| 1412 | ((two & 0x3f) << 6) | |
| 1413 | (three & 0x3f); |
| 1414 | } |
| 1415 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 1416 | // Like "strlen", but for strings encoded with "modified" UTF-8. |
| 1417 | // |
| 1418 | // The value returned is the number of characters, which may or may not |
| 1419 | // be the same as the number of bytes. |
| 1420 | // |
| 1421 | // (If this needs optimizing, try: mask against 0xa0, shift right 5, |
| 1422 | // get increment {1-3} from table of 8 values.) |
| 1423 | static size_t ModifiedUtf8Len(const char* utf8) { |
| 1424 | size_t len = 0; |
| 1425 | int ic; |
| 1426 | while ((ic = *utf8++) != '\0') { |
| 1427 | len++; |
| 1428 | if ((ic & 0x80) == 0) { |
| 1429 | // one-byte encoding |
| 1430 | continue; |
| 1431 | } |
| 1432 | // two- or three-byte encoding |
| 1433 | utf8++; |
| 1434 | if ((ic & 0x20) == 0) { |
| 1435 | // two-byte encoding |
| 1436 | continue; |
| 1437 | } |
| 1438 | // three-byte encoding |
| 1439 | utf8++; |
| 1440 | } |
| 1441 | return len; |
| 1442 | } |
| 1443 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1444 | // The java/lang/String.computeHashCode() algorithm |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1445 | static int32_t ComputeUtf16Hash(const uint16_t* string_data, size_t string_length) { |
| 1446 | int32_t hash = 0; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1447 | while (string_length--) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1448 | hash = hash * 31 + *string_data++; |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 1449 | } |
| 1450 | return hash; |
| 1451 | } |
| 1452 | |
Jesse Wilson | fd687c5 | 2011-08-04 19:27:35 -0700 | [diff] [blame] | 1453 | void ComputeHashCode() { |
| 1454 | hash_code_ = ComputeUtf16Hash(array_->GetData(), count_); |
| 1455 | } |
| 1456 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1457 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1458 | bool Equals(const char* modified_utf8) const { |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1459 | for (uint32_t i = 0; i < GetLength(); ++i) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1460 | uint16_t ch = GetUtf16FromUtf8(&modified_utf8); |
| 1461 | if (ch == '\0' || ch != CharAt(i)) { |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1462 | return false; |
| 1463 | } |
| 1464 | } |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1465 | return *modified_utf8 == '\0'; |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 1466 | } |
| 1467 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1468 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1469 | bool Equals(const StringPiece& modified_utf8) const { |
| 1470 | // TODO: do not assume C-string representation. |
| 1471 | return Equals(modified_utf8.data()); |
| 1472 | } |
| 1473 | |
| 1474 | bool Equals(const String* that) const { |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 1475 | // TODO: short circuit on hash_code_ |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1476 | if (this->GetLength() != that->GetLength()) { |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1477 | return false; |
| 1478 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1479 | for (uint32_t i = 0; i < that->GetLength(); ++i) { |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1480 | if (this->CharAt(i) != that->CharAt(i)) { |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1481 | return false; |
| 1482 | } |
| 1483 | } |
| 1484 | return true; |
| 1485 | } |
| 1486 | |
Elliott Hughes | a5b897e | 2011-08-16 11:33:06 -0700 | [diff] [blame] | 1487 | // TODO: do we need this overload? give it a more intention-revealing name. |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1488 | bool Equals(const uint16_t* that_chars, uint32_t that_offset, uint32_t that_length) const { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1489 | if (this->GetLength() != that_length) { |
| 1490 | return false; |
| 1491 | } |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1492 | for (uint32_t i = 0; i < that_length; ++i) { |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1493 | if (this->CharAt(i) != that_chars[that_offset + i]) { |
| 1494 | return false; |
| 1495 | } |
| 1496 | } |
| 1497 | return true; |
| 1498 | } |
| 1499 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1500 | // Create a modified UTF-8 encoded std::string from a java/lang/String object. |
| 1501 | std::string ToModifiedUtf8() const { |
| 1502 | std::string result; |
| 1503 | for (uint32_t i = 0; i < GetLength(); i++) { |
| 1504 | uint16_t ch = CharAt(i); |
| 1505 | // The most common case is (ch > 0 && ch <= 0x7f). |
| 1506 | if (ch == 0 || ch > 0x7f) { |
| 1507 | if (ch > 0x07ff) { |
| 1508 | result.push_back((ch >> 12) | 0xe0); |
| 1509 | result.push_back(((ch >> 6) & 0x3f) | 0x80); |
| 1510 | result.push_back((ch & 0x3f) | 0x80); |
| 1511 | } else { // (ch > 0x7f || ch == 0) |
| 1512 | result.push_back((ch >> 6) | 0xc0); |
| 1513 | result.push_back((ch & 0x3f) | 0x80); |
| 1514 | } |
| 1515 | } else { |
| 1516 | result.push_back(ch); |
| 1517 | } |
| 1518 | } |
| 1519 | return result; |
| 1520 | } |
| 1521 | |
| 1522 | |
Brian Carlstrom | a7f4f48 | 2011-07-17 17:01:34 -0700 | [diff] [blame] | 1523 | private: |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1524 | // Field order required by test "ValidateFieldOrderOfJavaCppUnionClasses". |
| 1525 | CharArray* array_; |
| 1526 | |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1527 | uint32_t hash_code_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1528 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1529 | int32_t offset_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1530 | |
Elliott Hughes | 289da82 | 2011-08-16 10:11:20 -0700 | [diff] [blame] | 1531 | int32_t count_; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 1532 | |
| 1533 | static Class* GetJavaLangString() { |
| 1534 | DCHECK(java_lang_String_ != NULL); |
| 1535 | return java_lang_String_; |
| 1536 | } |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 1537 | |
| 1538 | static Class* java_lang_String_; |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 1539 | |
| 1540 | DISALLOW_IMPLICIT_CONSTRUCTORS(String); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1541 | }; |
| 1542 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1543 | inline bool Object::IsString() const { |
| 1544 | // TODO use "klass_ == String::GetJavaLangString()" instead? |
| 1545 | return klass_ == klass_->descriptor_->klass_; |
| 1546 | } |
| 1547 | |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 1548 | inline size_t Class::GetTypeSize(String* descriptor) { |
| 1549 | switch (descriptor->CharAt(0)) { |
| 1550 | case 'B': return 1; // byte |
| 1551 | case 'C': return 2; // char |
| 1552 | case 'D': return 8; // double |
| 1553 | case 'F': return 4; // float |
| 1554 | case 'I': return 4; // int |
| 1555 | case 'J': return 8; // long |
| 1556 | case 'S': return 2; // short |
| 1557 | case 'Z': return 1; // boolean |
| 1558 | case 'L': return sizeof(Object*); |
| 1559 | case '[': return sizeof(Array*); |
| 1560 | default: |
| 1561 | LOG(ERROR) << "Unknown type " << descriptor; |
| 1562 | return 0; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | inline bool Class::IsArray() const { |
| 1567 | return GetDescriptor()->CharAt(0) == '['; // TODO: avoid parsing the descriptor |
| 1568 | } |
| 1569 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1570 | class InterfaceEntry { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1571 | public: |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1572 | InterfaceEntry() : klass_(NULL), method_index_array_(NULL) { |
| 1573 | } |
| 1574 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1575 | Class* GetClass() const { |
| 1576 | return klass_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1577 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1578 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1579 | void SetClass(Class* klass) { |
| 1580 | klass_ = klass; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1581 | } |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1582 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1583 | private: |
| 1584 | // Points to the interface class. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1585 | Class* klass_; |
| 1586 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 1587 | public: // TODO: private |
| 1588 | // Index into array of vtable offsets. This points into the |
| 1589 | // ifviPool, which holds the vtables for all interfaces declared by |
| 1590 | // this class. |
| 1591 | uint32_t* method_index_array_; |
Carl Shapiro | f88c952 | 2011-08-06 15:47:38 -0700 | [diff] [blame] | 1592 | |
| 1593 | private: |
| 1594 | DISALLOW_COPY_AND_ASSIGN(InterfaceEntry); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1595 | }; |
| 1596 | |
| 1597 | } // namespace art |
| 1598 | |
| 1599 | #endif // ART_SRC_OBJECT_H_ |