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 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 11 | namespace art { |
| 12 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 13 | bool Class::IsInSamePackage(const char* descriptor1, const char* descriptor2) { |
| 14 | size_t i = 0; |
| 15 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 16 | ++i; |
| 17 | } |
| 18 | if (strrchr(descriptor1 + i, '/') != NULL || |
| 19 | strrchr(descriptor2 + i, '/') != NULL ) { |
| 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 |
| 27 | bool Class::IsInSamePackage(const char* descriptor1, const char* descriptor2) { |
| 28 | size_t size = std::min(descriptor1.size(), descriptor2.size()); |
| 29 | std::pair<const char*, const char*> pos; |
| 30 | pos = std::mismatch(descriptor1.data(), size, descriptor2.data()); |
| 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. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 53 | return IsInSamePackage(klass1->descriptor_.data(), |
| 54 | klass2->descriptor_.data()); |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 57 | uint32_t Method::NumArgRegisters() { |
| 58 | CHECK(shorty_ != NULL); |
| 59 | uint32_t num_registers = 0; |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame^] | 60 | for (int i = 1; i < shorty_.length(); ++i) { |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 61 | char ch = shorty_[i]; |
| 62 | if (ch == 'D' || ch == 'J') { |
| 63 | num_registers += 2; |
| 64 | } else { |
| 65 | num_registers += 1; |
| 66 | } |
| 67 | } |
| 68 | return num_registers; |
| 69 | } |
| 70 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 71 | bool Method::HasSameArgumentTypes(const Method* that) const { |
| 72 | const RawDexFile* raw1 = this->dex_file_->GetRaw(); |
| 73 | const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_); |
| 74 | const RawDexFile* raw2 = that->dex_file_->GetRaw(); |
| 75 | const RawDexFile::ProtoId& proto2 = raw2->GetProtoId(that->proto_idx_); |
| 76 | |
| 77 | // TODO: compare ProtoId objects for equality and exit early |
| 78 | |
| 79 | const RawDexFile::TypeList* type_list1 = raw1->GetProtoParameters(proto1); |
| 80 | size_t arity1 = (type_list1 == NULL) ? 0 : type_list1->Size(); |
| 81 | const RawDexFile::TypeList* type_list2 = raw2->GetProtoParameters(proto2); |
| 82 | size_t arity2 = (type_list2 == NULL) ? 0 : type_list2->Size(); |
| 83 | |
| 84 | if (arity1 != arity2) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | for (size_t i = 0; i < arity1; ++i) { |
| 89 | uint32_t type_idx1 = type_list1->GetTypeItem(i).type_idx_; |
| 90 | const char* type1 = raw1->dexStringByTypeIdx(type_idx1); |
| 91 | |
| 92 | uint32_t type_idx2 = type_list2->GetTypeItem(i).type_idx_; |
| 93 | const char* type2 = raw2->dexStringByTypeIdx(type_idx2); |
| 94 | |
| 95 | if (strcmp(type1, type2) != 0) { |
| 96 | return false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool Method::HasSameReturnType(const Method* that) const { |
| 104 | const RawDexFile* raw1 = this->dex_file_->GetRaw(); |
| 105 | const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_); |
| 106 | const char* type1 = raw1->dexStringByTypeIdx(proto1.return_type_idx_); |
| 107 | |
| 108 | const RawDexFile* raw2 = that->dex_file_->GetRaw(); |
| 109 | const RawDexFile::ProtoId& proto2 = raw2->GetProtoId(that->proto_idx_); |
| 110 | const char* type2 = raw2->dexStringByTypeIdx(proto2.return_type_idx_); |
| 111 | |
| 112 | return (strcmp(type1, type2) == 0); |
| 113 | } |
| 114 | |
| 115 | Method* Class::FindDirectMethodLocally(const StringPiece& name, |
| 116 | const StringPiece& descriptor) const { |
| 117 | return NULL; // TODO |
| 118 | } |
| 119 | |
| 120 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 121 | } // namespace art |