• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
18 #define ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
19 
20 #include "signature.h"
21 
22 #include "dex_file-inl.h"
23 
24 namespace art {
25 
26 inline bool Signature::operator==(const Signature& rhs) const {
27   if (dex_file_ == nullptr) {
28     return rhs.dex_file_ == nullptr;
29   }
30   if (rhs.dex_file_ == nullptr) {
31     return false;
32   }
33   if (dex_file_ == rhs.dex_file_) {
34     return proto_id_ == rhs.proto_id_;
35   }
36   std::string_view lhs_shorty = dex_file_->GetShortyView(*proto_id_);
37   if (lhs_shorty != rhs.dex_file_->GetShortyView(*rhs.proto_id_)) {
38     return false;  // Shorty mismatch.
39   }
40   if (lhs_shorty[0] == 'L') {
41     const dex::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_);
42     const dex::TypeId& rhs_return_type_id =
43         rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_);
44     if (!DexFile::StringEquals(dex_file_, return_type_id.descriptor_idx_,
45                                rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) {
46       return false;  // Return type mismatch.
47     }
48   }
49   if (lhs_shorty.find('L', 1) != std::string_view::npos) {
50     const dex::TypeList* lhs_params = dex_file_->GetProtoParameters(*proto_id_);
51     const dex::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_);
52     // We found a reference parameter in the matching shorty, so both lists must be non-empty.
53     DCHECK(lhs_params != nullptr);
54     DCHECK(rhs_params != nullptr);
55     uint32_t params_size = lhs_shorty.size() - 1u;
56     DCHECK_EQ(params_size, lhs_params->Size());  // Parameter list sizes must match shorty.
57     DCHECK_EQ(params_size, rhs_params->Size());
58     for (uint32_t i = 0; i < params_size; ++i) {
59       const dex::TypeId& lhs_param_id = dex_file_->GetTypeId(lhs_params->GetTypeItem(i).type_idx_);
60       const dex::TypeId& rhs_param_id =
61           rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_);
62       if (!DexFile::StringEquals(dex_file_, lhs_param_id.descriptor_idx_,
63                                  rhs.dex_file_, rhs_param_id.descriptor_idx_)) {
64         return false;  // Parameter type mismatch.
65       }
66     }
67   }
68   return true;
69 }
70 
Compare(const Signature & rhs)71 inline int Signature::Compare(const Signature& rhs) const {
72   DCHECK(dex_file_ != nullptr);
73   DCHECK(rhs.dex_file_ != nullptr);
74   if (dex_file_ == rhs.dex_file_) {
75     return static_cast<int>(dex_file_->GetIndexForProtoId(*proto_id_).index_) -
76            static_cast<int>(rhs.dex_file_->GetIndexForProtoId(*rhs.proto_id_).index_);
77   }
78   // Use shorty to avoid looking at primitive type descriptors
79   // as long as they are not compared with reference descriptors.
80   std::string_view lhs_shorty = dex_file_->GetShortyView(*proto_id_);
81   std::string_view rhs_shorty = rhs.dex_file_->GetShortyView(*rhs.proto_id_);
82   // Note that 'L' in a shorty can represent an array type starting with '[',
83   // so do the full type descriptor comparison when we see 'L' in either shorty.
84   if (lhs_shorty[0] == 'L' || rhs_shorty[0] == 'L') {
85     std::string_view lhs_return_type = dex_file_->GetTypeDescriptorView(
86         dex_file_->GetTypeId(proto_id_->return_type_idx_));
87     std::string_view rhs_return_type = rhs.dex_file_->GetTypeDescriptorView(
88         rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_));
89     int cmp_result = lhs_return_type.compare(rhs_return_type);
90     if (cmp_result != 0) {
91       return cmp_result;
92     }
93   } else if (lhs_shorty[0] != rhs_shorty[0]) {
94     return static_cast<int>(lhs_shorty[0]) - static_cast<int>(rhs_shorty[0]);
95   }
96   size_t min_shorty_size = std::min(lhs_shorty.size(), rhs_shorty.size());
97   if (min_shorty_size != 1u) {  // If both shortys contain parameters, compare parameters.
98     const dex::TypeList* lhs_params = dex_file_->GetProtoParameters(*proto_id_);
99     const dex::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_);
100     DCHECK(lhs_params != nullptr);
101     DCHECK(rhs_params != nullptr);
102     for (size_t i = 1u; i != min_shorty_size; ++i) {
103       if (lhs_shorty[i] == 'L' || rhs_shorty[i] == 'L') {
104         std::string_view lhs_param_type = dex_file_->GetTypeDescriptorView(
105             dex_file_->GetTypeId(lhs_params->GetTypeItem(i - 1u).type_idx_));
106         std::string_view rhs_param_type = rhs.dex_file_->GetTypeDescriptorView(
107             rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i - 1u).type_idx_));
108         int cmp_result = lhs_param_type.compare(rhs_param_type);
109         if (cmp_result != 0) {
110           return cmp_result;
111         }
112       } else if (lhs_shorty[i] != rhs_shorty[i]) {
113         return static_cast<int>(lhs_shorty[i]) - static_cast<int>(rhs_shorty[i]);
114       }
115     }
116   }
117   if (lhs_shorty.size() == rhs_shorty.size()) {
118     return 0;
119   } else if (lhs_shorty.size() < rhs_shorty.size()) {
120     return -1;
121   } else {
122     return 1;
123   }
124 }
125 
126 }  // namespace art
127 
128 #endif  // ART_LIBDEXFILE_DEX_SIGNATURE_INL_H_
129