blob: f76def64a992bbe2e1d8071ed9b87609deac464c [file] [log] [blame]
Christopher Ferris9323b722017-03-03 17:43:14 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __LIB_DEMANGLE_DEMANGLER_H
18#define __LIB_DEMANGLE_DEMANGLER_H
19
20#include <assert.h>
21
22#include <stack>
23#include <string>
24#include <vector>
25
26class Demangler {
27 public:
28 Demangler() = default;
29
30 // NOTE: The max_length is not guaranteed to be the absolute max length
31 // of a string that will be rejected. Under certain circumstances the
32 // length check will not occur until after the second letter of a pair
33 // is checked.
34 std::string Parse(const char* name, size_t max_length = kMaxDefaultLength);
35
36 void AppendCurrent(const std::string& str);
37 void AppendCurrent(const char* str);
38 void AppendArgument(const std::string& str);
39 std::string GetArgumentsString();
40 void FinalizeTemplate();
41 const char* ParseS(const char* name);
42 const char* AppendOperatorString(const char* name);
43 void Save(const std::string& str, bool is_name);
44
45 private:
46 void Clear() {
47 parse_funcs_.clear();
48 function_name_.clear();
49 function_suffix_.clear();
50 first_save_.clear();
51 cur_state_.Clear();
52 saves_.clear();
53 while (!state_stack_.empty()) {
54 state_stack_.pop();
55 }
56 last_save_name_ = false;
57 }
58
59 using parse_func_type = const char* (Demangler::*)(const char*);
60 parse_func_type parse_func_;
61 std::vector<parse_func_type> parse_funcs_;
62 std::vector<std::string> saves_;
63 bool last_save_name_;
64
65 std::string function_name_;
66 std::string function_suffix_;
67
68 struct StateData {
69 void Clear() {
70 str.clear();
71 args.clear();
72 prefix.clear();
73 suffixes.clear();
74 last_save.clear();
75 }
76
77 std::string str;
78 std::vector<std::string> args;
79 std::string prefix;
80 std::vector<std::string> suffixes;
81 std::string last_save;
82 };
83 std::stack<StateData> state_stack_;
84 std::string first_save_;
85 StateData cur_state_;
86
87 static const char* GetStringFromLength(const char* name, std::string* str);
88
89 // Parsing functions.
90 const char* ParseComplexString(const char* name);
91 const char* ParseComplexArgument(const char* name);
92 const char* ParseArguments(const char* name);
93 const char* ParseTemplateArguments(const char* name);
94 const char* ParseTemplateArgumentsComplex(const char* name);
Christopher Ferris05232752017-10-11 15:22:29 -070095 const char* ParseTemplateLiteral(const char* name);
Christopher Ferris9323b722017-03-03 17:43:14 -080096 const char* ParseFunctionArgument(const char* name);
97 const char* ParseFunctionName(const char* name);
98 const char* FindFunctionName(const char* name);
99 const char* Fail(const char*) { return nullptr; }
100
101 // The default maximum string length string to process.
102 static constexpr size_t kMaxDefaultLength = 2048;
103
104 static constexpr const char* kTypes[] = {
105 "signed char", // a
106 "bool", // b
107 "char", // c
108 "double", // d
109 "long double", // e
110 "float", // f
111 "__float128", // g
112 "unsigned char", // h
113 "int", // i
114 "unsigned int", // j
115 nullptr, // k
116 "long", // l
117 "unsigned long", // m
118 "__int128", // n
119 "unsigned __int128", // o
120 nullptr, // p
121 nullptr, // q
122 nullptr, // r
123 "short", // s
124 "unsigned short", // t
125 nullptr, // u
126 "void", // v
127 "wchar_t", // w
128 "long long", // x
129 "unsigned long long", // y
130 "...", // z
131 };
132
133 static constexpr const char* kDTypes[] = {
134 "auto", // a
135 nullptr, // b
136 nullptr, // c
137 "decimal64", // d
138 "decimal128", // e
139 "decimal32", // f
140 nullptr, // g
141 "half", // h
142 "char32_t", // i
143 nullptr, // j
144 nullptr, // k
145 nullptr, // l
146 nullptr, // m
147 "decltype(nullptr)", // n
148 nullptr, // o
149 nullptr, // p
150 nullptr, // q
151 nullptr, // r
152 "char16_t", // s
153 nullptr, // t
154 nullptr, // u
155 nullptr, // v
156 nullptr, // w
157 nullptr, // x
158 nullptr, // y
159 nullptr, // z
160 };
161
162 static constexpr const char* kSTypes[] = {
163 "std::allocator", // a
164 "std::basic_string", // b
165 nullptr, // c
166 "std::iostream", // d
167 nullptr, // e
168 nullptr, // f
169 nullptr, // g
170 nullptr, // h
171 "std::istream", // i
172 nullptr, // j
173 nullptr, // k
174 nullptr, // l
175 nullptr, // m
176 nullptr, // n
177 "std::ostream", // o
178 nullptr, // p
179 nullptr, // q
180 nullptr, // r
181 "std::string", // s
182 nullptr, // t
183 nullptr, // u
184 nullptr, // v
185 nullptr, // w
186 nullptr, // x
187 nullptr, // y
188 nullptr, // z
189 };
190};
191
192#endif // __LIB_DEMANGLE_DEMANGLER_H