• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace panda::pandasm {
25 constexpr const std::string_view MANGLE_BEGIN = ":";
26 constexpr const std::string_view MANGLE_SEPARATOR = ";";
27 
28 constexpr const std::string_view SIGNATURE_BEGIN = MANGLE_BEGIN;
29 constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR = ",";
30 constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR_L = "(";
31 constexpr const std::string_view SIGNATURE_TYPES_SEPARATOR_R = ")";
32 
DeMangleName(const std::string & name)33 inline 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 
MangleFunctionName(const std::string & name,const std::vector<pandasm::Function::Parameter> & params,const pandasm::Type & return_type)42 inline std::string MangleFunctionName(const std::string &name, const std::vector<pandasm::Function::Parameter> &params,
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 
MangleFieldName(const std::string & name,const pandasm::Type & type)55 inline 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 
GetFunctionSignatureFromName(std::string name,const std::vector<pandasm::Function::Parameter> & params)63 inline std::string GetFunctionSignatureFromName(std::string name,
64                                                 const std::vector<pandasm::Function::Parameter> &params)
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 
GetFunctionNameFromSignature(const std::string & sig)81 inline std::string GetFunctionNameFromSignature(const std::string &sig)
82 {
83     return DeMangleName(sig);
84 }
85 
IsSignatureOrMangled(const std::string & str)86 inline 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