xref: /aosp_15_r20/art/libdexfile/dex/signature.cc (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 #include "signature-inl.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include <string.h>
20*795d594fSAndroid Build Coastguard Worker 
21*795d594fSAndroid Build Coastguard Worker #include <ostream>
22*795d594fSAndroid Build Coastguard Worker #include <type_traits>
23*795d594fSAndroid Build Coastguard Worker 
24*795d594fSAndroid Build Coastguard Worker namespace art {
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker using dex::TypeList;
27*795d594fSAndroid Build Coastguard Worker 
ToString() const28*795d594fSAndroid Build Coastguard Worker std::string Signature::ToString() const {
29*795d594fSAndroid Build Coastguard Worker   if (dex_file_ == nullptr) {
30*795d594fSAndroid Build Coastguard Worker     CHECK(proto_id_ == nullptr);
31*795d594fSAndroid Build Coastguard Worker     return "<no signature>";
32*795d594fSAndroid Build Coastguard Worker   }
33*795d594fSAndroid Build Coastguard Worker   const TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
34*795d594fSAndroid Build Coastguard Worker   std::string result;
35*795d594fSAndroid Build Coastguard Worker   if (params == nullptr) {
36*795d594fSAndroid Build Coastguard Worker     result += "()";
37*795d594fSAndroid Build Coastguard Worker   } else {
38*795d594fSAndroid Build Coastguard Worker     result += "(";
39*795d594fSAndroid Build Coastguard Worker     for (uint32_t i = 0; i < params->Size(); ++i) {
40*795d594fSAndroid Build Coastguard Worker       result += dex_file_->GetTypeDescriptorView(params->GetTypeItem(i).type_idx_);
41*795d594fSAndroid Build Coastguard Worker     }
42*795d594fSAndroid Build Coastguard Worker     result += ")";
43*795d594fSAndroid Build Coastguard Worker   }
44*795d594fSAndroid Build Coastguard Worker   result += dex_file_->GetTypeDescriptorView(proto_id_->return_type_idx_);
45*795d594fSAndroid Build Coastguard Worker   return result;
46*795d594fSAndroid Build Coastguard Worker }
47*795d594fSAndroid Build Coastguard Worker 
GetNumberOfParameters() const48*795d594fSAndroid Build Coastguard Worker uint32_t Signature::GetNumberOfParameters() const {
49*795d594fSAndroid Build Coastguard Worker   const TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
50*795d594fSAndroid Build Coastguard Worker   return (params != nullptr) ? params->Size() : 0;
51*795d594fSAndroid Build Coastguard Worker }
52*795d594fSAndroid Build Coastguard Worker 
IsVoid() const53*795d594fSAndroid Build Coastguard Worker bool Signature::IsVoid() const {
54*795d594fSAndroid Build Coastguard Worker   const char* return_type = dex_file_->GetReturnTypeDescriptor(*proto_id_);
55*795d594fSAndroid Build Coastguard Worker   return strcmp(return_type, "V") == 0;
56*795d594fSAndroid Build Coastguard Worker }
57*795d594fSAndroid Build Coastguard Worker 
operator ==(std::string_view rhs) const58*795d594fSAndroid Build Coastguard Worker bool Signature::operator==(std::string_view rhs) const {
59*795d594fSAndroid Build Coastguard Worker   if (dex_file_ == nullptr) {
60*795d594fSAndroid Build Coastguard Worker     return false;
61*795d594fSAndroid Build Coastguard Worker   }
62*795d594fSAndroid Build Coastguard Worker   std::string_view tail(rhs);
63*795d594fSAndroid Build Coastguard Worker   if (!tail.starts_with("(")) {
64*795d594fSAndroid Build Coastguard Worker     return false;  // Invalid signature
65*795d594fSAndroid Build Coastguard Worker   }
66*795d594fSAndroid Build Coastguard Worker   tail.remove_prefix(1);  // "(";
67*795d594fSAndroid Build Coastguard Worker   const TypeList* params = dex_file_->GetProtoParameters(*proto_id_);
68*795d594fSAndroid Build Coastguard Worker   if (params != nullptr) {
69*795d594fSAndroid Build Coastguard Worker     for (uint32_t i = 0; i < params->Size(); ++i) {
70*795d594fSAndroid Build Coastguard Worker       std::string_view param = dex_file_->GetTypeDescriptorView(params->GetTypeItem(i).type_idx_);
71*795d594fSAndroid Build Coastguard Worker       if (!tail.starts_with(param)) {
72*795d594fSAndroid Build Coastguard Worker         return false;
73*795d594fSAndroid Build Coastguard Worker       }
74*795d594fSAndroid Build Coastguard Worker       tail.remove_prefix(param.length());
75*795d594fSAndroid Build Coastguard Worker     }
76*795d594fSAndroid Build Coastguard Worker   }
77*795d594fSAndroid Build Coastguard Worker   if (!tail.starts_with(")")) {
78*795d594fSAndroid Build Coastguard Worker     return false;
79*795d594fSAndroid Build Coastguard Worker   }
80*795d594fSAndroid Build Coastguard Worker   tail.remove_prefix(1);  // ")";
81*795d594fSAndroid Build Coastguard Worker   return tail == dex_file_->GetTypeDescriptorView(proto_id_->return_type_idx_);
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker 
operator <<(std::ostream & os,const Signature & sig)84*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const Signature& sig) {
85*795d594fSAndroid Build Coastguard Worker   return os << sig.ToString();
86*795d594fSAndroid Build Coastguard Worker }
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker }  // namespace art
89