1/** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#ifndef ASSEMBLER_MANGLING_H 17#define ASSEMBLER_MANGLING_H 18 19#include "assembly-function.h" 20 21#include <string> 22#include <vector> 23 24namespace panda::pandasm { 25constexpr const std::string_view MANGLE_BEGIN = ":"; 26constexpr const std::string_view MANGLE_SEPARATOR = ";"; 27 28constexpr const std::string_view SIGNATURE_BEGIN = MANGLE_BEGIN; 29constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR = ","; 30constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR_L = "("; 31constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR_R = ")"; 32 33inline std::string DeMangleName(const std::string &name) 34{ 35 auto iter = name.find_first_of(MANGLE_BEGIN); 36 if (iter != std::string::npos) { 37 return name.substr(0, name.find_first_of(MANGLE_BEGIN)); 38 } 39 return name; 40} 41 42inline std::string MangleFunctionName(const std::string &name, const std::vector<pandasm::Function::Parameter> ¶ms, 43 const pandasm::Type &return_type) 44{ 45 std::string mangle_name {name}; 46 mangle_name += MANGLE_BEGIN; 47 for (const auto &p : params) { 48 mangle_name += p.type.GetName() + std::string(MANGLE_SEPARATOR); 49 } 50 mangle_name += return_type.GetName() + std::string(MANGLE_SEPARATOR); 51 52 return mangle_name; 53} 54 55inline std::string MangleFieldName(const std::string &name, const pandasm::Type &type) 56{ 57 std::string mangle_name {name}; 58 mangle_name += MANGLE_BEGIN; 59 mangle_name += type.GetName() + std::string(MANGLE_SEPARATOR); 60 return mangle_name; 61} 62 63inline std::string GetFunctionSignatureFromName(std::string name, 64 const std::vector<pandasm::Function::Parameter> ¶ms) 65{ 66 name += SIGNATURE_BEGIN; 67 name += SIGNATURE_TYPES_SEPARATOR_L; 68 69 bool first = true; 70 for (const auto &p : params) { 71 name += (first) ? "" : SIGNATURE_TYPES_SEPARATOR; 72 first = false; 73 name += p.type.GetPandasmName(); 74 } 75 76 name += SIGNATURE_TYPES_SEPARATOR_R; 77 78 return name; 79} 80 81inline std::string GetFunctionNameFromSignature(const std::string &sig) 82{ 83 return DeMangleName(sig); 84} 85 86inline bool IsSignatureOrMangled(const std::string &str) 87{ 88 return str.find(SIGNATURE_BEGIN) != std::string::npos; 89} 90 91} // namespace panda::pandasm 92 93#endif // ASSEMBLER_MANGLING_H 94