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