blob: c2c09cac98ffe7e276592d14469773ababee1ad0 [file] [log] [blame]
Carl Shapiro3ee755d2011-06-28 12:11:04 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "src/globals.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -07004#include "src/logging.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005#include "src/object.h"
6#include "src/dex_file.h"
7#include "src/raw_dex_file.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -07008
Brian Carlstrom6cc18452011-07-18 15:10:33 -07009#include <algorithm>
Carl Shapiro894d0fa2011-06-30 14:48:49 -070010
Carl Shapiro3ee755d2011-06-28 12:11:04 -070011namespace art {
12
Brian Carlstrom6cc18452011-07-18 15:10:33 -070013bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070014 size_t i = 0;
15 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
16 ++i;
17 }
Brian Carlstrom6cc18452011-07-18 15:10:33 -070018 if (descriptor1.find('/', i) != StringPiece::npos ||
19 descriptor2.find('/', i) != StringPiece::npos) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070020 return false;
21 } else {
22 return true;
23 }
24}
25
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026#if 0
Brian Carlstrom6cc18452011-07-18 15:10:33 -070027bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028 size_t size = std::min(descriptor1.size(), descriptor2.size());
Brian Carlstrom6cc18452011-07-18 15:10:33 -070029 std::pair<StringPiece::const_iterator, StringPiece::const_iterator> pos;
30 pos = std::mismatch(descriptor1.begin(), descriptor1.begin() + size, descriptor2.begin());
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070031 return !(*(pos.second).rfind('/') != npos && descriptor2.rfind('/') != npos);
32}
33#endif
34
Carl Shapiro894d0fa2011-06-30 14:48:49 -070035bool 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 Shapiro0e5d75d2011-07-06 18:28:37 -070042 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070043 return false;
44 }
45 // Arrays are in the same package when their element classes are.
46 if (klass1->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047 klass1 = klass1->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -070048 }
49 if (klass2->IsArray()) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050 klass2 = klass2->GetComponentType();
Carl Shapiro894d0fa2011-06-30 14:48:49 -070051 }
52 // Compare the package part of the descriptor string.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070053 return IsInSamePackage(klass1->descriptor_, klass2->descriptor_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -070054}
55
Carl Shapiro3ee755d2011-06-28 12:11:04 -070056uint32_t Method::NumArgRegisters() {
57 CHECK(shorty_ != NULL);
58 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -070059 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -070060 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 Shapiro0e5d75d2011-07-06 18:28:37 -070070bool Method::HasSameArgumentTypes(const Method* that) const {
Brian Carlstrom934486c2011-07-12 23:42:50 -070071 const RawDexFile* raw1 = this->GetClass()->GetDexFile()->GetRaw();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072 const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_);
Brian Carlstrom934486c2011-07-12 23:42:50 -070073 const RawDexFile* raw2 = that->GetClass()->GetDexFile()->GetRaw();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070074 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
102bool Method::HasSameReturnType(const Method* that) const {
Brian Carlstrom934486c2011-07-12 23:42:50 -0700103 const RawDexFile* raw1 = this->GetClass()->GetDexFile()->GetRaw();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700104 const RawDexFile::ProtoId& proto1 = raw1->GetProtoId(this->proto_idx_);
105 const char* type1 = raw1->dexStringByTypeIdx(proto1.return_type_idx_);
106
Brian Carlstrom934486c2011-07-12 23:42:50 -0700107 const RawDexFile* raw2 = that->GetClass()->GetDexFile()->GetRaw();
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700108 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
114Method* Class::FindDirectMethodLocally(const StringPiece& name,
115 const StringPiece& descriptor) const {
116 return NULL; // TODO
117}
118
Elliott Hughes1f359b02011-07-17 14:27:17 -0700119static const char* kClassStatusNames[] = {
120 "Error",
121 "NotReady",
122 "Idx",
123 "Loaded",
124 "Resolved",
125 "Verifying",
126 "Verified",
127 "Initializing",
128 "Initialized"
129};
130std::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 Shapiro0e5d75d2011-07-06 18:28:37 -0700138
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700139} // namespace art