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 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6 | #include "src/dex_file.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 7 | #include "src/globals.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 8 | #include "src/macros.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9 | #include "src/stringpiece.h" |
| 10 | #include "src/monitor.h" |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 11 | |
| 12 | namespace art { |
| 13 | |
| 14 | class Array; |
| 15 | class Class; |
| 16 | class DexFile; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 17 | class InstanceField; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 18 | class InterfaceEntry; |
| 19 | class Monitor; |
| 20 | class Method; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 21 | class Object; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 22 | class StaticField; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 23 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 24 | union JValue { |
| 25 | uint8_t z; |
| 26 | int8_t b; |
| 27 | uint16_t c; |
| 28 | int16_t s; |
| 29 | int32_t i; |
| 30 | int64_t j; |
| 31 | float f; |
| 32 | double d; |
| 33 | Object* l; |
| 34 | }; |
| 35 | |
| 36 | static const uint32_t kAccPublic = 0x0001; // class, field, method, ic |
| 37 | static const uint32_t kAccPrivate = 0x0002; // field, method, ic |
| 38 | static const uint32_t kAccProtected = 0x0004; // field, method, ic |
| 39 | static const uint32_t kAccStatic = 0x0008; // field, method, ic |
| 40 | static const uint32_t kAccFinal = 0x0010; // class, field, method, ic |
| 41 | static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives) |
| 42 | static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik) |
| 43 | static const uint32_t kAccVolatile = 0x0040; // field |
| 44 | static const uint32_t kAccBridge = 0x0040; // method (1.5) |
| 45 | static const uint32_t kAccTransient = 0x0080; // field |
| 46 | static const uint32_t kAccVarargs = 0x0080; // method (1.5) |
| 47 | static const uint32_t kAccNative = 0x0100; // method |
| 48 | static const uint32_t kAccInterface = 0x0200; // class, ic |
| 49 | static const uint32_t kAccAbstract = 0x0400; // class, method, ic |
| 50 | static const uint32_t kAccStrict = 0x0800; // method |
| 51 | static const uint32_t kAccSynthetic = 0x1000; // field, method, ic |
| 52 | static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5) |
| 53 | static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5) |
| 54 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 55 | static const uint32_t kAccMiranda = 0x8000; // method |
| 56 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 57 | static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only) |
| 58 | static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only) |
| 59 | |
| 60 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 61 | /* |
| 62 | * Definitions for packing refOffsets in ClassObject. |
| 63 | */ |
| 64 | /* |
| 65 | * A magic value for refOffsets. Ignore the bits and walk the super |
| 66 | * chain when this is the value. |
| 67 | * [This is an unlikely "natural" value, since it would be 30 non-ref instance |
| 68 | * fields followed by 2 ref instance fields.] |
| 69 | */ |
| 70 | #define CLASS_WALK_SUPER ((unsigned int)(3)) |
| 71 | #define CLASS_SMALLEST_OFFSET (sizeof(struct Object)) |
| 72 | #define CLASS_BITS_PER_WORD (sizeof(unsigned long int) * 8) |
| 73 | #define CLASS_OFFSET_ALIGNMENT 4 |
| 74 | #define CLASS_HIGH_BIT ((unsigned int)1 << (CLASS_BITS_PER_WORD - 1)) |
| 75 | /* |
| 76 | * Given an offset, return the bit number which would encode that offset. |
| 77 | * Local use only. |
| 78 | */ |
| 79 | #define _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) \ |
| 80 | (((unsigned int)(byteOffset) - CLASS_SMALLEST_OFFSET) / \ |
| 81 | CLASS_OFFSET_ALIGNMENT) |
| 82 | /* |
| 83 | * Is the given offset too large to be encoded? |
| 84 | */ |
| 85 | #define CLASS_CAN_ENCODE_OFFSET(byteOffset) \ |
| 86 | (_CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset) < CLASS_BITS_PER_WORD) |
| 87 | /* |
| 88 | * Return a single bit, encoding the offset. |
| 89 | * Undefined if the offset is too large, as defined above. |
| 90 | */ |
| 91 | #define CLASS_BIT_FROM_OFFSET(byteOffset) \ |
| 92 | (CLASS_HIGH_BIT >> _CLASS_BIT_NUMBER_FROM_OFFSET(byteOffset)) |
| 93 | /* |
| 94 | * Return an offset, given a bit number as returned from CLZ. |
| 95 | */ |
| 96 | #define CLASS_OFFSET_FROM_CLZ(rshift) \ |
| 97 | (((int)(rshift) * CLASS_OFFSET_ALIGNMENT) + CLASS_SMALLEST_OFFSET) |
| 98 | |
| 99 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 100 | class Object { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 101 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 102 | Class* GetClass() const { |
| 103 | return klass_; |
| 104 | } |
| 105 | |
| 106 | void MonitorEnter() { |
| 107 | monitor_->Enter(); |
| 108 | } |
| 109 | |
| 110 | void MonitorExit() { |
| 111 | monitor_->Exit(); |
| 112 | } |
| 113 | |
| 114 | void Notify() { |
| 115 | monitor_->Notify(); |
| 116 | } |
| 117 | |
| 118 | void NotifyAll() { |
| 119 | monitor_->NotifyAll(); |
| 120 | } |
| 121 | |
| 122 | void Wait() { |
| 123 | monitor_->Wait(); |
| 124 | } |
| 125 | |
| 126 | void Wait(int64_t timeout) { |
| 127 | monitor_->Wait(timeout); |
| 128 | } |
| 129 | |
| 130 | void Wait(int64_t timeout, int32_t nanos) { |
| 131 | monitor_->Wait(timeout, nanos); |
| 132 | } |
| 133 | |
| 134 | void SetObjectAt(size_t offset, Object* new_value) { |
| 135 | byte* raw_addr = reinterpret_cast<byte*>(this) + offset; |
| 136 | *reinterpret_cast<Object**>(raw_addr) = new_value; |
| 137 | // TODO: write barrier |
| 138 | } |
| 139 | |
| 140 | public: |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 141 | Class* klass_; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 142 | Monitor* monitor_; |
| 143 | |
| 144 | private: |
| 145 | DISALLOW_COPY_AND_ASSIGN(Object); |
| 146 | }; |
| 147 | |
| 148 | class ObjectLock { |
| 149 | public: |
| 150 | ObjectLock(Object* object) : obj_(object) { |
| 151 | CHECK(object != NULL); |
| 152 | obj_->MonitorEnter(); |
| 153 | } |
| 154 | |
| 155 | ~ObjectLock() { |
| 156 | obj_->MonitorExit(); |
| 157 | } |
| 158 | |
| 159 | void Wait(int64_t millis = 0) { |
| 160 | return obj_->Wait(millis); |
| 161 | } |
| 162 | |
| 163 | void Notify() { |
| 164 | obj_->Notify(); |
| 165 | } |
| 166 | |
| 167 | void NotifyAll() { |
| 168 | obj_->NotifyAll(); |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | Object* obj_; |
| 173 | DISALLOW_COPY_AND_ASSIGN(ObjectLock); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | class Field { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 177 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 178 | Class* GetClass() const { |
| 179 | return klass_; |
| 180 | } |
| 181 | |
| 182 | const char* GetName() const { |
| 183 | return name_; |
| 184 | } |
| 185 | |
| 186 | char GetType() const { // TODO: return type |
| 187 | return signature_[0]; |
| 188 | } |
| 189 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 190 | const char* GetSignature() const { |
| 191 | return signature_; |
| 192 | } |
| 193 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 194 | public: // TODO: private |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 195 | // The class in which this field is declared. |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 196 | Class* klass_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 197 | |
| 198 | const char* name_; |
| 199 | |
| 200 | // e.g. "I", "[C", "Landroid/os/Debug;" |
| 201 | const char* signature_; |
| 202 | |
| 203 | uint32_t access_flags_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | // Instance fields. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 207 | class InstanceField : public Field { |
| 208 | public: |
| 209 | uint32_t GetOffset() const { |
| 210 | return offset_; |
| 211 | } |
| 212 | |
| 213 | void SetOffset(size_t num_bytes) { |
| 214 | offset_ = num_bytes; |
| 215 | } |
| 216 | |
| 217 | // TODO: stl::swap |
| 218 | void Swap(InstanceField* that) { |
| 219 | InstanceField tmp; |
| 220 | memcpy(&tmp, this, sizeof(InstanceField)); |
| 221 | memcpy(this, that, sizeof(InstanceField)); |
| 222 | memcpy(that, &tmp, sizeof(InstanceField)); |
| 223 | } |
| 224 | |
| 225 | private: |
| 226 | size_t offset_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | // Static fields. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 230 | class StaticField : public Field { |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 231 | public: |
| 232 | void SetBoolean(bool z) { |
| 233 | CHECK_EQ(GetType(), 'Z'); |
| 234 | value_.z = z; |
| 235 | } |
| 236 | |
| 237 | void SetByte(int8_t b) { |
| 238 | CHECK_EQ(GetType(), 'B'); |
| 239 | value_.b = b; |
| 240 | } |
| 241 | |
| 242 | void SetChar(uint16_t c) { |
| 243 | CHECK_EQ(GetType(), 'C'); |
| 244 | value_.c = c; |
| 245 | } |
| 246 | |
| 247 | void SetShort(uint16_t s) { |
| 248 | CHECK_EQ(GetType(), 'S'); |
| 249 | value_.s = s; |
| 250 | } |
| 251 | |
| 252 | void SetInt(int32_t i) { |
| 253 | CHECK_EQ(GetType(), 'I'); |
| 254 | value_.i = i; |
| 255 | } |
| 256 | |
| 257 | int64_t GetLong() { |
| 258 | CHECK_EQ(GetType(), 'J'); |
| 259 | return value_.j; |
| 260 | } |
| 261 | |
| 262 | void SetLong(int64_t j) { |
| 263 | CHECK_EQ(GetType(), 'J'); |
| 264 | value_.j = j; |
| 265 | } |
| 266 | |
| 267 | void SetFloat(float f) { |
| 268 | CHECK_EQ(GetType(), 'F'); |
| 269 | value_.f = f; |
| 270 | } |
| 271 | |
| 272 | void SetDouble(double d) { |
| 273 | CHECK_EQ(GetType(), 'D'); |
| 274 | value_.d = d; |
| 275 | } |
| 276 | |
| 277 | void SetObject(Object* l) { |
| 278 | CHECK_EQ(GetType(), 'L'); |
| 279 | value_.l = l; |
| 280 | // TODO: write barrier |
| 281 | } |
| 282 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 283 | private: |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 284 | JValue value_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 285 | }; |
| 286 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 287 | class Method { |
| 288 | public: |
| 289 | // Returns the method name. |
| 290 | // TODO: example |
| 291 | const StringPiece& GetName() const { |
| 292 | return name_; |
| 293 | } |
| 294 | |
| 295 | Class* GetClass() const { |
| 296 | return klass_; |
| 297 | } |
| 298 | |
| 299 | // const char* GetReturnTypeDescriptor() const { |
| 300 | // return dex_file_->GetRaw()->dexStringByTypeIdx(proto_id_.return_type_id_); |
| 301 | // } |
| 302 | |
| 303 | // Returns true if the method is declared public. |
| 304 | bool IsPublic() const { |
| 305 | return (access_flags_ & kAccPublic) != 0; |
| 306 | } |
| 307 | |
| 308 | // Returns true if the method is declared private. |
| 309 | bool IsPrivate() const { |
| 310 | return (access_flags_ & kAccPrivate) != 0; |
| 311 | } |
| 312 | |
| 313 | // Returns true if the method is declared static. |
| 314 | bool IsStatic() const { |
| 315 | return (access_flags_ & kAccStatic) != 0; |
| 316 | } |
| 317 | |
| 318 | // Returns true if the method is declared synchronized. |
| 319 | bool IsSynchronized() const { |
| 320 | uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized; |
| 321 | return (access_flags_ & synchonized) != 0; |
| 322 | } |
| 323 | |
| 324 | // Returns true if the method is declared final. |
| 325 | bool IsFinal() const { |
| 326 | return (access_flags_ & kAccFinal) != 0; |
| 327 | } |
| 328 | |
| 329 | // Returns true if the method is declared native. |
| 330 | bool IsNative() const { |
| 331 | return (access_flags_ & kAccNative) != 0; |
| 332 | } |
| 333 | |
| 334 | // Returns true if the method is declared abstract. |
| 335 | bool IsAbstract() const { |
| 336 | return (access_flags_ & kAccAbstract) != 0; |
| 337 | } |
| 338 | |
| 339 | bool IsSynthetic() const { |
| 340 | return (access_flags_ & kAccSynthetic) != 0; |
| 341 | } |
| 342 | |
| 343 | // Number of argument registers required by the prototype. |
| 344 | uint32_t NumArgRegisters(); |
| 345 | |
| 346 | bool HasSameNameAndPrototype(const Method* that) const { |
| 347 | return HasSameName(that) && HasSamePrototype(that); |
| 348 | } |
| 349 | |
| 350 | bool HasSameName(const Method* that) const { |
| 351 | return this->GetName() == that->GetName(); |
| 352 | } |
| 353 | |
| 354 | bool HasSamePrototype(const Method* that) const { |
| 355 | return HasSameReturnType(that) && HasSameArgumentTypes(that); |
| 356 | } |
| 357 | |
| 358 | bool HasSameReturnType(const Method* that) const; |
| 359 | |
| 360 | bool HasSameArgumentTypes(const Method* that) const; |
| 361 | |
| 362 | public: // TODO: private |
| 363 | // the class we are a part of |
| 364 | Class* klass_; |
| 365 | |
| 366 | // access flags; low 16 bits are defined by spec (could be uint16_t?) |
| 367 | uint32_t access_flags_; |
| 368 | |
| 369 | // For concrete virtual methods, this is the offset of the method |
| 370 | // in "vtable". |
| 371 | // |
| 372 | // For abstract methods in an interface class, this is the offset |
| 373 | // of the method in "iftable[n]->methodIndexArray". |
| 374 | uint16_t method_index_; |
| 375 | |
| 376 | // Method bounds; not needed for an abstract method. |
| 377 | // |
| 378 | // For a native method, we compute the size of the argument list, and |
| 379 | // set "insSize" and "registerSize" equal to it. |
| 380 | uint16_t num_registers_; // ins + locals |
| 381 | uint16_t num_outs_; |
| 382 | uint16_t num_ins_; |
| 383 | |
| 384 | // method name, e.g. "<init>" or "eatLunch" |
| 385 | StringPiece name_; |
| 386 | |
| 387 | // A pointer to the DEX file this class was loaded from or NULL for |
| 388 | // proxy objects. |
| 389 | DexFile* dex_file_; |
| 390 | |
| 391 | // Method prototype descriptor string (return and argument types). |
| 392 | uint32_t proto_idx_; |
| 393 | |
| 394 | // The short-form method descriptor string. |
| 395 | StringPiece shorty_; |
| 396 | |
| 397 | // A pointer to the memory-mapped DEX code. |
| 398 | const uint16_t* insns_; |
| 399 | }; |
| 400 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 401 | // Class objects. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 402 | class Class : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 403 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 404 | enum Status { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 405 | kStatusError = -1, |
| 406 | kStatusNotReady = 0, |
| 407 | kStatusIdx = 1, // loaded, DEX idx in super or ifaces |
| 408 | kStatusLoaded = 2, // DEX idx values resolved |
| 409 | kStatusResolved = 3, // part of linking |
| 410 | kStatusVerifying = 4, // in the process of being verified |
| 411 | kStatusVerified = 5, // logically part of linking; done pre-init |
| 412 | kStatusInitializing = 6, // class init in progress |
| 413 | kStatusInitialized = 7, // ready to go |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | enum PrimitiveType { |
| 417 | kPrimNot = -1 |
| 418 | }; |
| 419 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 420 | Class* GetSuperClass() const { |
| 421 | return super_class_; |
| 422 | } |
| 423 | |
| 424 | uint32_t GetSuperClassIdx() const { |
| 425 | return super_class_idx_; |
| 426 | } |
| 427 | |
| 428 | bool HasSuperClass() const { |
| 429 | return super_class_ != NULL; |
| 430 | } |
| 431 | |
| 432 | Object* GetClassLoader() const { |
| 433 | return class_loader_; |
| 434 | } |
| 435 | |
| 436 | DexFile* GetDexFile() const { |
| 437 | return dex_file_; |
| 438 | } |
| 439 | |
| 440 | Class* GetComponentType() const { |
| 441 | return component_type_; |
| 442 | } |
| 443 | |
| 444 | const StringPiece& GetDescriptor() const { |
| 445 | return descriptor_; |
| 446 | } |
| 447 | |
| 448 | Status GetStatus() const { |
| 449 | return status_; |
| 450 | } |
| 451 | |
| 452 | void SetStatus(Status new_status) { |
| 453 | // TODO: validate transition |
| 454 | status_ = new_status; |
| 455 | } |
| 456 | |
| 457 | bool IsErroneous() const { |
| 458 | return GetStatus() == kStatusError; |
| 459 | } |
| 460 | |
| 461 | bool IsVerified() const { |
| 462 | return GetStatus() >= kStatusVerified; |
| 463 | } |
| 464 | |
| 465 | bool IsLinked() const { |
| 466 | return GetStatus() >= kStatusResolved; |
| 467 | } |
| 468 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 469 | // Returns true if this class is in the same packages as that class. |
| 470 | bool IsInSamePackage(const Class* that) const; |
| 471 | |
| 472 | static bool IsInSamePackage(const char* descriptor1, const char* descriptor2); |
| 473 | |
| 474 | // Returns true if this class represents an array class. |
| 475 | bool IsArray() const { |
| 476 | return descriptor_[0] == '['; // TODO: avoid parsing the descriptor |
| 477 | } |
| 478 | |
| 479 | // Returns true if the class is an interface. |
| 480 | bool IsInterface() const { |
| 481 | return (access_flags_ & kAccInterface) != 0; |
| 482 | } |
| 483 | |
| 484 | // Returns true if the class is declared public. |
| 485 | bool IsPublic() const { |
| 486 | return (access_flags_ & kAccPublic) != 0; |
| 487 | } |
| 488 | |
| 489 | // Returns true if the class is declared final. |
| 490 | bool IsFinal() const { |
| 491 | return (access_flags_ & kAccFinal) != 0; |
| 492 | } |
| 493 | |
| 494 | // Returns true if the class is abstract. |
| 495 | bool IsAbstract() const { |
| 496 | return (access_flags_ & kAccAbstract) != 0; |
| 497 | } |
| 498 | |
| 499 | // Returns true if the class is an annotation. |
| 500 | bool IsAnnotation() const { |
| 501 | return (access_flags_ & kAccAnnotation) != 0; |
| 502 | } |
| 503 | |
| 504 | // Returns true if the class is a primitive type. |
| 505 | bool IsPrimitive() const { |
| 506 | return primitive_type_ != kPrimNot; |
| 507 | } |
| 508 | |
| 509 | // Returns true if this class can access that class. |
| 510 | bool CanAccess(const Class* that) const { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 511 | return that->IsPublic() || this->IsInSamePackage(that); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 514 | // Returns the size in bytes of a class object instance with the |
| 515 | // given number of static fields. |
| 516 | static size_t Size(size_t num_sfields) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 517 | return OFFSETOF_MEMBER(Class, sfields_) + sizeof(StaticField) * num_sfields; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 520 | // Returns the number of static, private, and constructor methods. |
| 521 | size_t NumDirectMethods() const { |
| 522 | return num_direct_methods_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 525 | Method* GetDirectMethod(uint32_t i) const { |
| 526 | return &direct_methods_[i]; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 529 | // Returns the number of non-inherited virtual methods. |
| 530 | size_t NumVirtualMethods() const { |
| 531 | return num_virtual_methods_; |
| 532 | } |
| 533 | |
| 534 | Method* GetVirtualMethod(uint32_t i) const { |
| 535 | return &virtual_methods_[i]; |
| 536 | } |
| 537 | |
| 538 | size_t NumInstanceFields() const { |
| 539 | return num_ifields_; |
| 540 | } |
| 541 | |
| 542 | size_t NumReferenceInstanceFields() const { |
| 543 | return num_reference_ifields_; |
| 544 | } |
| 545 | |
| 546 | InstanceField* GetInstanceField(uint32_t i) { // TODO: uint16_t |
| 547 | return &ifields_[i]; |
| 548 | } |
| 549 | |
| 550 | size_t NumStaticFields() const { |
| 551 | return num_sfields_; |
| 552 | } |
| 553 | |
| 554 | StaticField* GetStaticField(uint32_t i) { // TODO: uint16_t |
| 555 | return &sfields_[i]; |
| 556 | } |
| 557 | |
| 558 | uint32_t GetReferenceOffsets() const { |
| 559 | return reference_offsets_; |
| 560 | } |
| 561 | |
| 562 | void SetReferenceOffsets(uint32_t new_reference_offsets) { |
| 563 | reference_offsets_ = new_reference_offsets; |
| 564 | } |
| 565 | |
| 566 | Method* FindDirectMethodLocally(const StringPiece& name, |
| 567 | const StringPiece& descriptor) const; |
| 568 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 569 | public: // TODO: private |
| 570 | // leave space for instance data; we could access fields directly if |
| 571 | // we freeze the definition of java/lang/Class |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 572 | #define CLASS_FIELD_SLOTS 4 |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 573 | uint32_t instance_data_[CLASS_FIELD_SLOTS]; |
| 574 | #undef CLASS_FIELD_SLOTS |
| 575 | |
| 576 | // UTF-8 descriptor for the class from constant pool |
| 577 | // ("Ljava/lang/Class;"), or on heap if generated ("[C") |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 578 | StringPiece descriptor_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 579 | |
| 580 | // Proxy classes have their descriptor allocated on the native heap. |
| 581 | // When this field is non-NULL it must be explicitly freed. |
| 582 | char* descriptor_alloc_; |
| 583 | |
| 584 | // access flags; low 16 bits are defined by VM spec |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 585 | uint32_t access_flags_; // TODO: make an instance field? |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 586 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 587 | // DexFile from which we came; needed to resolve constant pool entries |
| 588 | // (will be NULL for VM-generated, e.g. arrays and primitive classes) |
| 589 | DexFile* dex_file_; |
| 590 | |
| 591 | // state of class initialization |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 592 | Status status_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 593 | |
| 594 | // if class verify fails, we must return same error on subsequent tries |
| 595 | Class* verify_error_class_; |
| 596 | |
| 597 | // threadId, used to check for recursive <clinit> invocation |
| 598 | uint32_t clinit_thread_id_; |
| 599 | |
| 600 | // Total object size; used when allocating storage on gc heap. (For |
| 601 | // interfaces and abstract classes this will be zero.) |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 602 | size_t object_size_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 603 | |
| 604 | // For array classes, the class object for base element, for |
| 605 | // instanceof/checkcast (for String[][][], this will be String). |
| 606 | // Otherwise, NULL. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 607 | Class* component_type_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 608 | |
| 609 | // For array classes, the number of array dimensions, e.g. int[][] |
| 610 | // is 2. Otherwise 0. |
| 611 | int32_t array_rank_; |
| 612 | |
| 613 | // primitive type index, or PRIM_NOT (-1); set for generated prim classes |
| 614 | PrimitiveType primitive_type_; |
| 615 | |
| 616 | // The superclass, or NULL if this is java.lang.Object or a |
| 617 | // primitive type. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 618 | Class* super_class_; // TODO: make an instance field |
| 619 | uint32_t super_class_idx_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 620 | |
| 621 | // defining class loader, or NULL for the "bootstrap" system loader |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 622 | Object* class_loader_; // TODO: make an instance field |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 623 | |
| 624 | // initiating class loader list |
| 625 | // NOTE: for classes with low serialNumber, these are unused, and the |
| 626 | // values are kept in a table in gDvm. |
| 627 | //InitiatingLoaderList initiating_loader_list_; |
| 628 | |
| 629 | // array of interfaces this class implements directly |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 630 | size_t interface_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 631 | Class** interfaces_; |
| 632 | |
| 633 | // static, private, and <init> methods |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 634 | size_t num_direct_methods_; |
| 635 | Method* direct_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 636 | |
| 637 | // virtual methods defined in this class; invoked through vtable |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 638 | size_t num_virtual_methods_; |
| 639 | Method* virtual_methods_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 640 | |
| 641 | // Virtual method table (vtable), for use by "invoke-virtual". The |
| 642 | // vtable from the superclass is copied in, and virtual methods from |
| 643 | // our class either replace those from the super or are appended. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 644 | size_t vtable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 645 | Method** vtable_; |
| 646 | |
| 647 | // Interface table (iftable), one entry per interface supported by |
| 648 | // this class. That means one entry for each interface we support |
| 649 | // directly, indirectly via superclass, or indirectly via |
| 650 | // superinterface. This will be null if neither we nor our |
| 651 | // superclass implement any interfaces. |
| 652 | // |
| 653 | // Why we need this: given "class Foo implements Face", declare |
| 654 | // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah" |
| 655 | // is part of the Face interface. We can't easily use a single |
| 656 | // vtable. |
| 657 | // |
| 658 | // For every interface a concrete class implements, we create a list |
| 659 | // of virtualMethod indices for the methods in the interface. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 660 | size_t iftable_count_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 661 | InterfaceEntry* iftable_; |
| 662 | |
| 663 | // The interface vtable indices for iftable get stored here. By |
| 664 | // placing them all in a single pool for each class that implements |
| 665 | // interfaces, we decrease the number of allocations. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 666 | size_t ifvi_pool_count_; |
| 667 | uint32_t* ifvi_pool_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 668 | |
| 669 | // instance fields |
| 670 | // |
| 671 | // These describe the layout of the contents of a |
| 672 | // DataObject-compatible Object. Note that only the fields directly |
| 673 | // declared by this class are listed in ifields; fields declared by |
| 674 | // a superclass are listed in the superclass's ClassObject.ifields. |
| 675 | // |
| 676 | // All instance fields that refer to objects are guaranteed to be at |
| 677 | // the beginning of the field list. ifieldRefCount specifies the |
| 678 | // number of reference fields. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 679 | size_t num_ifields_; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 680 | |
| 681 | // number of fields that are object refs |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 682 | size_t num_reference_ifields_; |
| 683 | InstanceField* ifields_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 684 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 685 | // Bitmap of offsets of ifields. |
| 686 | uint32_t reference_offsets_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 687 | |
| 688 | // source file name, if known. Otherwise, NULL. |
| 689 | const char* source_file_; |
| 690 | |
| 691 | // Static fields |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 692 | size_t num_sfields_; |
| 693 | StaticField sfields_[]; // MUST be last item |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 694 | }; |
| 695 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 696 | class DataObject : public Object { |
| 697 | public: |
| 698 | uint32_t fields_[1]; |
| 699 | }; |
| 700 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 701 | class Array : public Object { |
| 702 | public: |
| 703 | void SetLength(uint32_t length) { |
| 704 | length_ = length; |
| 705 | } |
| 706 | |
| 707 | private: |
| 708 | // The number of array elements. |
| 709 | uint32_t length_; |
| 710 | }; |
| 711 | |
| 712 | class CharArray : public Array {}; |
| 713 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 714 | class String : public Object { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 715 | public: |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 716 | CharArray* array_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 717 | |
| 718 | uint32_t hash_code_; |
| 719 | |
| 720 | uint32_t offset_; |
| 721 | |
| 722 | uint32_t count_; |
| 723 | }; |
| 724 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 725 | class InterfaceEntry { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 726 | public: |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 727 | Class* GetClass() const { |
| 728 | return klass_; |
| 729 | }; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 730 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 731 | void SetClass(Class* klass) { |
| 732 | klass_ = klass; |
| 733 | }; |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 734 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 735 | private: |
| 736 | // Points to the interface class. |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 737 | Class* klass_; |
| 738 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 739 | public: // TODO: private |
| 740 | // Index into array of vtable offsets. This points into the |
| 741 | // ifviPool, which holds the vtables for all interfaces declared by |
| 742 | // this class. |
| 743 | uint32_t* method_index_array_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | } // namespace art |
| 747 | |
| 748 | #endif // ART_SRC_OBJECT_H_ |