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