summaryrefslogtreecommitdiffstats
path: root/demangle/Demangler.h
blob: 3b7d44ef36536ca5fc69776fbd11d59695ecca4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __LIB_DEMANGLE_DEMANGLER_H
#define __LIB_DEMANGLE_DEMANGLER_H

#include <assert.h>

#include <stack>
#include <string>
#include <vector>

class Demangler {
 public:
  Demangler() = default;

  // NOTE: The max_length is not guaranteed to be the absolute max length
  // of a string that will be rejected. Under certain circumstances the
  // length check will not occur until after the second letter of a pair
  // is checked.
  std::string Parse(const char* name, size_t max_length = kMaxDefaultLength);

  void AppendCurrent(const std::string& str);
  void AppendCurrent(const char* str);
  void AppendArgument(const std::string& str);
  std::string GetArgumentsString();
  void FinalizeTemplate();
  const char* ParseS(const char* name);
  const char* ParseT(const char* name);
  const char* AppendOperatorString(const char* name);
  void Save(const std::string& str, bool is_name);

 private:
  void Clear() {
    parse_funcs_.clear();
    function_name_.clear();
    function_suffix_.clear();
    first_save_.clear();
    cur_state_.Clear();
    saves_.clear();
    template_saves_.clear();
    while (!state_stack_.empty()) {
      state_stack_.pop();
    }
    last_save_name_ = false;
    template_found_ = false;
  }

  using parse_func_type = const char* (Demangler::*)(const char*);
  parse_func_type parse_func_;
  std::vector<parse_func_type> parse_funcs_;
  std::vector<std::string> saves_;
  std::vector<std::string> template_saves_;
  bool last_save_name_;
  bool template_found_;

  std::string function_name_;
  std::string function_suffix_;

  struct StateData {
    void Clear() {
      str.clear();
      args.clear();
      prefix.clear();
      suffixes.clear();
      last_save.clear();
    }

    std::string str;
    std::vector<std::string> args;
    std::string prefix;
    std::vector<std::string> suffixes;
    std::string last_save;
  };
  std::stack<StateData> state_stack_;
  std::string first_save_;
  StateData cur_state_;

  static const char* GetStringFromLength(const char* name, std::string* str);

  // Parsing functions.
  const char* ParseComplexString(const char* name);
  const char* ParseComplexArgument(const char* name);
  const char* ParseArgumentsAtTopLevel(const char* name);
  const char* ParseArguments(const char* name);
  const char* ParseTemplateArguments(const char* name);
  const char* ParseTemplateArgumentsComplex(const char* name);
  const char* ParseTemplateLiteral(const char* name);
  const char* ParseFunctionArgument(const char* name);
  const char* ParseFunctionName(const char* name);
  const char* ParseFunctionNameTemplate(const char* name);
  const char* ParseFunctionTemplateArguments(const char* name);
  const char* FindFunctionName(const char* name);
  const char* Fail(const char*) { return nullptr; }

  // The default maximum string length string to process.
  static constexpr size_t kMaxDefaultLength = 2048;

  static constexpr const char* kTypes[] = {
    "signed char",        // a
    "bool",               // b
    "char",               // c
    "double",             // d
    "long double",        // e
    "float",              // f
    "__float128",         // g
    "unsigned char",      // h
    "int",                // i
    "unsigned int",       // j
    nullptr,              // k
    "long",               // l
    "unsigned long",      // m
    "__int128",           // n
    "unsigned __int128",  // o
    nullptr,              // p
    nullptr,              // q
    nullptr,              // r
    "short",              // s
    "unsigned short",     // t
    nullptr,              // u
    "void",               // v
    "wchar_t",            // w
    "long long",          // x
    "unsigned long long", // y
    "...",                // z
  };

  static constexpr const char* kDTypes[] = {
    "auto",               // a
    nullptr,              // b
    nullptr,              // c
    "decimal64",          // d
    "decimal128",         // e
    "decimal32",          // f
    nullptr,              // g
    "half",               // h
    "char32_t",           // i
    nullptr,              // j
    nullptr,              // k
    nullptr,              // l
    nullptr,              // m
    "decltype(nullptr)",  // n
    nullptr,              // o
    nullptr,              // p
    nullptr,              // q
    nullptr,              // r
    "char16_t",           // s
    nullptr,              // t
    nullptr,              // u
    nullptr,              // v
    nullptr,              // w
    nullptr,              // x
    nullptr,              // y
    nullptr,              // z
  };

  static constexpr const char* kSTypes[] = {
    "std::allocator",     // a
    "std::basic_string",  // b
    nullptr,              // c
    "std::iostream",      // d
    nullptr,              // e
    nullptr,              // f
    nullptr,              // g
    nullptr,              // h
    "std::istream",       // i
    nullptr,              // j
    nullptr,              // k
    nullptr,              // l
    nullptr,              // m
    nullptr,              // n
    "std::ostream",       // o
    nullptr,              // p
    nullptr,              // q
    nullptr,              // r
    "std::string",        // s
    nullptr,              // t
    nullptr,              // u
    nullptr,              // v
    nullptr,              // w
    nullptr,              // x
    nullptr,              // y
    nullptr,              // z
  };
};

#endif  // __LIB_DEMANGLE_DEMANGLER_H