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