1 /*
2 * Copyright (c) 2021 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 PANDA_ASSEMBLER_MANGLING_H_
17 #define PANDA_ASSEMBLER_MANGLING_H_
18
19 #include "assembly-function.h"
20
21 #include <string>
22 #include <vector>
23
24 namespace panda::pandasm {
25 static std::string MANGLE_BEGIN = ":";
26 static std::string MANGLE_SEPARATOR = ";";
27
MangleFunctionName(const std::string & name,const std::vector<pandasm::Function::Parameter> & params,const pandasm::Type & return_type)28 inline std::string MangleFunctionName(const std::string &name, const std::vector<pandasm::Function::Parameter> ¶ms,
29 const pandasm::Type &return_type)
30 {
31 std::string mangle_name {name};
32 mangle_name += MANGLE_BEGIN;
33 for (const auto &p : params) {
34 mangle_name += p.type.GetName() + MANGLE_SEPARATOR;
35 }
36 mangle_name += return_type.GetName() + MANGLE_SEPARATOR;
37
38 return mangle_name;
39 }
40
DeMangleName(const std::string & name)41 inline std::string DeMangleName(const std::string &name)
42 {
43 auto iter = name.find_first_of(MANGLE_BEGIN);
44 if (iter != std::string::npos) {
45 return name.substr(0, name.find_first_of(MANGLE_BEGIN));
46 }
47 return name;
48 }
49
MangleFieldName(const std::string & name,const pandasm::Type & type)50 inline std::string MangleFieldName(const std::string &name, const pandasm::Type &type)
51 {
52 std::string mangle_name {name};
53 mangle_name += MANGLE_BEGIN;
54 mangle_name += type.GetName() + MANGLE_SEPARATOR;
55 return mangle_name;
56 }
57
58 } // namespace panda::pandasm
59
60 #endif // PANDA_ASSEMBLER_MANGLING_H_
61