blob: 7c28d760ab103c73eaedd8c57340177a06664b07 [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
Carl Shapiro894d0fa2011-06-30 14:48:49 -07009#include <string.h>
10
Carl Shapiro3ee755d2011-06-28 12:11:04 -070011namespace art {
12
Carl Shapiro894d0fa2011-06-30 14:48:49 -070013bool 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 Shapiro0e5d75d2011-07-06 18:28:37 -070026#if 0
27bool 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 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.
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053 return IsInSamePackage(klass1->descriptor_.data(),
54 klass2->descriptor_.data());
Carl Shapiro894d0fa2011-06-30 14:48:49 -070055}
56
Carl Shapiro3ee755d2011-06-28 12:11:04 -070057uint32_t Method::NumArgRegisters() {
58 CHECK(shorty_ != NULL);
59 uint32_t num_registers = 0;
Carl Shapiro565f5072011-07-10 13:39:43 -070060 for (int i = 1; i < shorty_.length(); ++i) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -070061 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 Shapiro0e5d75d2011-07-06 18:28:37 -070071bool 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
103bool 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
115Method* Class::FindDirectMethodLocally(const StringPiece& name,
116 const StringPiece& descriptor) const {
117 return NULL; // TODO
118}
119
120
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700121} // namespace art