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