Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3 | #include "object.h" |
| 4 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 5 | #include <string.h> |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 6 | #include <algorithm> |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 7 | |
| 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" |
Brian Carlstrom | 7e49dca | 2011-07-22 18:07:34 -0700 | [diff] [blame] | 11 | #include "dex_cache.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 12 | #include "dex_file.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 13 | |
| 14 | namespace art { |
| 15 | |
Brian Carlstrom | f7ed11a | 2011-08-09 17:55:51 -0700 | [diff] [blame^] | 16 | bool Class::Implements(const Class* klass) const { |
| 17 | DCHECK(klass != NULL); |
| 18 | DCHECK(klass->IsInterface()); |
| 19 | // All interfaces implemented directly and by our superclass, and |
| 20 | // recursively all super-interfaces of those interfaces, are listed |
| 21 | // in iftable_, so we can just do a linear scan through that. |
| 22 | for (size_t i = 0; i < iftable_count_; i++) { |
| 23 | if (iftable_[i].GetClass() == klass) { |
| 24 | return true; |
| 25 | } |
| 26 | } |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | // Determine whether "this" is assignable from "klazz", where both of these |
| 31 | // are array classes. |
| 32 | // |
| 33 | // Consider an array class, e.g. Y[][], where Y is a subclass of X. |
| 34 | // Y[][] = Y[][] --> true (identity) |
| 35 | // X[][] = Y[][] --> true (element superclass) |
| 36 | // Y = Y[][] --> false |
| 37 | // Y[] = Y[][] --> false |
| 38 | // Object = Y[][] --> true (everything is an object) |
| 39 | // Object[] = Y[][] --> true |
| 40 | // Object[][] = Y[][] --> true |
| 41 | // Object[][][] = Y[][] --> false (too many []s) |
| 42 | // Serializable = Y[][] --> true (all arrays are Serializable) |
| 43 | // Serializable[] = Y[][] --> true |
| 44 | // Serializable[][] = Y[][] --> false (unless Y is Serializable) |
| 45 | // |
| 46 | // Don't forget about primitive types. |
| 47 | // int[] instanceof Object[] --> false |
| 48 | // |
| 49 | bool Class::IsArrayAssignableFromArray(const Class* klass) const { |
| 50 | DCHECK(IsArray()); |
| 51 | DCHECK(klass->IsArray()); |
| 52 | DCHECK_GT(array_rank_, 0); |
| 53 | DCHECK_GT(klass->array_rank_, 0); |
| 54 | DCHECK(component_type_ != NULL); |
| 55 | DCHECK(klass->component_type_ != NULL); |
| 56 | if (array_rank_ > klass->array_rank_) { |
| 57 | // Too many []s. |
| 58 | return false; |
| 59 | } |
| 60 | if (array_rank_ == klass->array_rank_) { |
| 61 | return component_type_->IsAssignableFrom(klass->component_type_); |
| 62 | } |
| 63 | DCHECK_LT(array_rank_, klass->array_rank_); |
| 64 | // The thing we might be assignable from has more dimensions. We |
| 65 | // must be an Object or array of Object, or a standard array |
| 66 | // interface or array of standard array interfaces (the standard |
| 67 | // interfaces being java/lang/Cloneable and java/io/Serializable). |
| 68 | if (component_type_->IsInterface()) { |
| 69 | // See if we implement our component type. We know the |
| 70 | // base element is an interface; if the array class implements |
| 71 | // it, we know it's a standard array interface. |
| 72 | return Implements(component_type_); |
| 73 | } |
| 74 | // See if this is an array of Object, Object[], etc. We know |
| 75 | // that the superclass of an array is always Object, so we |
| 76 | // just compare the element type to that. |
| 77 | Class* java_lang_Object = GetSuperClass(); |
| 78 | DCHECK(java_lang_Object != NULL); |
| 79 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 80 | return (component_type_ == java_lang_Object); |
| 81 | } |
| 82 | |
| 83 | bool Class::IsAssignableFromArray(const Class* klass) const { |
| 84 | DCHECK(!IsInterface()); // handled first in IsAssignableFrom |
| 85 | DCHECK(klass->IsArray()); |
| 86 | if (!IsArray()) { |
| 87 | // If "this" is not also an array, it must be Object. |
| 88 | // klass's super should be java_lang_Object, since it is an array. |
| 89 | Class* java_lang_Object = klass->GetSuperClass(); |
| 90 | DCHECK(java_lang_Object != NULL); |
| 91 | DCHECK(java_lang_Object->GetSuperClass() == NULL); |
| 92 | return this == java_lang_Object; |
| 93 | } |
| 94 | return IsArrayAssignableFromArray(klass); |
| 95 | } |
| 96 | |
| 97 | bool Class::IsSubClass(const Class* klass) const { |
| 98 | DCHECK(!IsInterface()); |
| 99 | DCHECK(!klass->IsArray()); |
| 100 | const Class* current = this; |
| 101 | do { |
| 102 | if (current == klass) { |
| 103 | return true; |
| 104 | } |
| 105 | current = current->GetSuperClass(); |
| 106 | } while (current != NULL); |
| 107 | return false; |
| 108 | } |
| 109 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 110 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 111 | const StringPiece& descriptor2) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 112 | size_t i = 0; |
| 113 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 114 | ++i; |
| 115 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 116 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 117 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 118 | return false; |
| 119 | } else { |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 124 | #if 0 |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 125 | bool Class::IsInSamePackage(const StringPiece& descriptor1, |
| 126 | const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 127 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 128 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 129 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, |
| 130 | descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 131 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 132 | } |
| 133 | #endif |
| 134 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 135 | bool Class::IsInSamePackage(const Class* that) const { |
| 136 | const Class* klass1 = this; |
| 137 | const Class* klass2 = that; |
| 138 | if (klass1 == klass2) { |
| 139 | return true; |
| 140 | } |
| 141 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 142 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | // Arrays are in the same package when their element classes are. |
| 146 | if (klass1->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 147 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 148 | } |
| 149 | if (klass2->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 150 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 151 | } |
| 152 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 153 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 156 | uint32_t Method::NumArgRegisters() { |
| 157 | CHECK(shorty_ != NULL); |
| 158 | uint32_t num_registers = 0; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 159 | for (int i = 1; i < shorty_.length(); ++i) { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 160 | char ch = shorty_[i]; |
| 161 | if (ch == 'D' || ch == 'J') { |
| 162 | num_registers += 2; |
| 163 | } else { |
| 164 | num_registers += 1; |
| 165 | } |
| 166 | } |
| 167 | return num_registers; |
| 168 | } |
| 169 | |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 170 | // The number of reference arguments to this method including implicit this |
| 171 | // pointer |
| 172 | size_t Method::NumReferenceArgs() const { |
| 173 | size_t result = IsStatic() ? 0 : 1; |
| 174 | for (int i = 1; i < shorty_.length(); i++) { |
| 175 | if ((shorty_[i] == 'L') || (shorty_[i] == '[')) { |
| 176 | result++; |
| 177 | } |
| 178 | } |
| 179 | return result; |
| 180 | } |
| 181 | |
| 182 | // The number of long or double arguments |
| 183 | size_t Method::NumLongOrDoubleArgs() const { |
| 184 | size_t result = 0; |
| 185 | for (int i = 1; i < shorty_.length(); i++) { |
| 186 | if ((shorty_[i] == 'D') || (shorty_[i] == 'J')) { |
| 187 | result++; |
| 188 | } |
| 189 | } |
| 190 | return result; |
| 191 | } |
| 192 | |
| 193 | // The number of reference arguments to this method before the given parameter |
| 194 | // index |
| 195 | size_t Method::NumReferenceArgsBefore(unsigned int param) const { |
| 196 | CHECK_LT(param, NumArgs()); |
| 197 | unsigned int result = IsStatic() ? 0 : 1; |
| 198 | for (unsigned int i = 1; (i < (unsigned int)shorty_.length()) && |
| 199 | (i < (param + 1)); i++) { |
| 200 | if ((shorty_[i] == 'L') || (shorty_[i] == '[')) { |
| 201 | result++; |
| 202 | } |
| 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | |
| 207 | // Is the given method parameter a reference? |
| 208 | bool Method::IsParamAReference(unsigned int param) const { |
| 209 | CHECK_LT(param, NumArgs()); |
| 210 | if (IsStatic()) { |
| 211 | param++; // 0th argument must skip return value at start of the shorty |
| 212 | } else if (param == 0) { |
| 213 | return true; // this argument |
| 214 | } |
| 215 | return ((shorty_[param] == 'L') || (shorty_[param] == '[')); |
| 216 | } |
| 217 | |
| 218 | // Is the given method parameter a long or double? |
| 219 | bool Method::IsParamALongOrDouble(unsigned int param) const { |
| 220 | CHECK_LT(param, NumArgs()); |
| 221 | if (IsStatic()) { |
| 222 | param++; // 0th argument must skip return value at start of the shorty |
Carl Shapiro | e2d373e | 2011-07-25 15:20:06 -0700 | [diff] [blame] | 223 | } else if (param == 0) { |
| 224 | return false; // this argument |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 225 | } |
| 226 | return (shorty_[param] == 'J') || (shorty_[param] == 'D'); |
| 227 | } |
| 228 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 229 | static size_t ShortyCharToSize(char x) { |
| 230 | switch (x) { |
Ian Rogers | 45a76cb | 2011-07-21 22:00:15 -0700 | [diff] [blame] | 231 | case 'V': return 0; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 232 | case '[': return kPointerSize; |
| 233 | case 'L': return kPointerSize; |
| 234 | case 'D': return 8; |
| 235 | case 'J': return 8; |
| 236 | default: return 4; |
| 237 | } |
| 238 | } |
| 239 | |
Ian Rogers | df20fe0 | 2011-07-20 20:34:16 -0700 | [diff] [blame] | 240 | size_t Method::ParamSize(unsigned int param) const { |
| 241 | CHECK_LT(param, NumArgs()); |
| 242 | if (IsStatic()) { |
| 243 | param++; // 0th argument must skip return value at start of the shorty |
| 244 | } else if (param == 0) { |
| 245 | return kPointerSize; // this argument |
| 246 | } |
| 247 | return ShortyCharToSize(shorty_[param]); |
| 248 | } |
| 249 | |
| 250 | size_t Method::ReturnSize() const { |
| 251 | return ShortyCharToSize(shorty_[0]); |
| 252 | } |
| 253 | |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 254 | bool Method::HasSameNameAndDescriptor(const Method* that) const { |
| 255 | return (this->GetName()->Equals(that->GetName()) && |
| 256 | this->GetDescriptor()->Equals(that->GetDescriptor())); |
| 257 | } |
| 258 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 259 | Method* Class::FindDeclaredDirectMethod(const StringPiece& name, |
| 260 | const StringPiece& descriptor) { |
| 261 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 262 | Method* method = GetDirectMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 263 | if (method->GetName()->Equals(name) && |
| 264 | method->GetDescriptor()->Equals(descriptor)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 265 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 268 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 271 | Method* Class::FindDirectMethod(const StringPiece& name, |
| 272 | const StringPiece& descriptor) { |
| 273 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 274 | Method* method = klass->FindDeclaredDirectMethod(name, descriptor); |
| 275 | if (method != NULL) { |
| 276 | return method; |
| 277 | } |
| 278 | } |
| 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | Method* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
| 283 | const StringPiece& descriptor) { |
| 284 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 285 | Method* method = GetVirtualMethod(i); |
Carl Shapiro | 8860c0e | 2011-08-04 17:36:16 -0700 | [diff] [blame] | 286 | if (method->GetName()->Equals(name) && |
| 287 | method->GetDescriptor()->Equals(descriptor)) { |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 288 | return method; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 291 | return NULL; |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Carl Shapiro | 419ec7b | 2011-08-03 14:48:33 -0700 | [diff] [blame] | 294 | Method* Class::FindVirtualMethod(const StringPiece& name, |
| 295 | const StringPiece& descriptor) { |
| 296 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 297 | Method* method = klass->FindDeclaredVirtualMethod(name, descriptor); |
| 298 | if (method != NULL) { |
| 299 | return method; |
| 300 | } |
| 301 | } |
| 302 | return NULL; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 305 | // TODO: get global references for these |
| 306 | Class* String::java_lang_String_ = NULL; |
| 307 | Class* String::char_array_ = NULL; |
| 308 | |
| 309 | void String::InitClasses(Class* java_lang_String, Class* char_array) { |
| 310 | java_lang_String_ = java_lang_String; |
| 311 | char_array_ = char_array; |
| 312 | } |
| 313 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 314 | static const char* kClassStatusNames[] = { |
| 315 | "Error", |
| 316 | "NotReady", |
| 317 | "Idx", |
| 318 | "Loaded", |
| 319 | "Resolved", |
| 320 | "Verifying", |
| 321 | "Verified", |
| 322 | "Initializing", |
| 323 | "Initialized" |
| 324 | }; |
| 325 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 326 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
Brian Carlstrom | ae3ac01 | 2011-07-27 01:30:28 -0700 | [diff] [blame] | 327 | os << kClassStatusNames[rhs + 1]; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 328 | } else { |
Ian Rogers | b033c75 | 2011-07-20 12:22:35 -0700 | [diff] [blame] | 329 | os << "Class::Status[" << static_cast<int>(rhs) << "]"; |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 330 | } |
| 331 | return os; |
| 332 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 333 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 334 | } // namespace art |