Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "src/globals.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 4 | #include "src/logging.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5 | #include "src/object.h" |
| 6 | #include "src/dex_file.h" |
| 7 | #include "src/raw_dex_file.h" |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 8 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 9 | #include <algorithm> |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 10 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 11 | namespace art { |
| 12 | |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 13 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 14 | size_t i = 0; |
| 15 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 16 | ++i; |
| 17 | } |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 18 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 19 | descriptor2.find('/', i) != StringPiece::npos) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 20 | return false; |
| 21 | } else { |
| 22 | return true; |
| 23 | } |
| 24 | } |
| 25 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 26 | #if 0 |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 27 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 28 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 29 | std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos; |
| 30 | pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, descriptor2.begin()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos); |
| 32 | } |
| 33 | #endif |
| 34 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 35 | bool Class::IsInSamePackage(const Class* that) const { |
| 36 | const Class* klass1 = this; |
| 37 | const Class* klass2 = that; |
| 38 | if (klass1 == klass2) { |
| 39 | return true; |
| 40 | } |
| 41 | // Class loaders must match. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 42 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | // Arrays are in the same package when their element classes are. |
| 46 | if (klass1->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 47 | klass1 = klass1->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 48 | } |
| 49 | if (klass2->IsArray()) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 50 | klass2 = klass2->GetComponentType(); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 51 | } |
| 52 | // Compare the package part of the descriptor string. |
Brian Carlstrom | 6cc1845 | 2011-07-18 15:10:33 -0700 | [diff] [blame] | 53 | return IsInSamePackage(klass1->descriptor_, klass2->descriptor_); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 56 | uint32_t Method::NumArgRegisters() { |
| 57 | CHECK(shorty_ != NULL); |
| 58 | uint32_t num_registers = 0; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 59 | for (int i = 1; i < shorty_.length(); ++i) { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 60 | char ch = shorty_[i]; |
| 61 | if (ch == 'D' || ch == 'J') { |
| 62 | num_registers += 2; |
| 63 | } else { |
| 64 | num_registers += 1; |
| 65 | } |
| 66 | } |
| 67 | return num_registers; |
| 68 | } |
| 69 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 70 | bool Method::HasSameArgumentTypes(const Method* that) const { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 71 | const RawDexFile* raw1 = this->GetClass()->GetDexFile()->GetRaw(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 72 | const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 73 | const RawDexFile* raw2 = that->GetClass()->GetDexFile()->GetRaw(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 74 | const RawDexFile::ProtoId& proto2 = raw2->GetProtoId(that->proto_idx_); |
| 75 | |
| 76 | // TODO: compare ProtoId objects for equality and exit early |
| 77 | |
| 78 | const RawDexFile::TypeList* type_list1 = raw1->GetProtoParameters(proto1); |
| 79 | size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size(); |
| 80 | const RawDexFile::TypeList* type_list2 = raw2->GetProtoParameters(proto2); |
| 81 | size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size(); |
| 82 | |
| 83 | if (arity1 != arity2) { |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | for (size_t i = 0; i < arity1; ++i) { |
| 88 | uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_; |
| 89 | const char* type1 = raw1->dexStringByTypeIdx(type_idx1); |
| 90 | |
| 91 | uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_; |
| 92 | const char* type2 = raw2->dexStringByTypeIdx(type_idx2); |
| 93 | |
| 94 | if (strcmp(type1, type2) != 0) { |
| 95 | return false; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool Method::HasSameReturnType(const Method* that) const { |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 103 | const RawDexFile* raw1 = this->GetClass()->GetDexFile()->GetRaw(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 104 | const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_); |
| 105 | const char* type1 = raw1->dexStringByTypeIdx(proto1.return_type_idx_); |
| 106 | |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 107 | const RawDexFile* raw2 = that->GetClass()->GetDexFile()->GetRaw(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 108 | const RawDexFile::ProtoId& proto2 = raw2->GetProtoId(that->proto_idx_); |
| 109 | const char* type2 = raw2->dexStringByTypeIdx(proto2.return_type_idx_); |
| 110 | |
| 111 | return (strcmp(type1, type2) == 0); |
| 112 | } |
| 113 | |
| 114 | Method* Class::FindDirectMethodLocally(const StringPiece& name, |
| 115 | const StringPiece& descriptor) const { |
| 116 | return NULL; // TODO |
| 117 | } |
| 118 | |
Elliott Hughes | 1f359b0 | 2011-07-17 14:27:17 -0700 | [diff] [blame] | 119 | static const char* kClassStatusNames[] = { |
| 120 | "Error", |
| 121 | "NotReady", |
| 122 | "Idx", |
| 123 | "Loaded", |
| 124 | "Resolved", |
| 125 | "Verifying", |
| 126 | "Verified", |
| 127 | "Initializing", |
| 128 | "Initialized" |
| 129 | }; |
| 130 | std::ostream& operator<<(std::ostream& os, const Class::Status& rhs) { |
| 131 | if (rhs >= Class::kStatusError && rhs <= Class::kStatusInitialized) { |
| 132 | os << kClassStatusNames[rhs - 1]; |
| 133 | } else { |
| 134 | os << "Class::Status[" << int(rhs) << "]"; |
| 135 | } |
| 136 | return os; |
| 137 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 138 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 139 | } // namespace art |