1 /* 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 26 class 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* ParseT(const char* name); 43 const char* AppendOperatorString(const char* name); 44 void Save(const std::string& str, bool is_name); 45 46 private: Clear()47 void Clear() { 48 parse_funcs_.clear(); 49 function_name_.clear(); 50 function_suffix_.clear(); 51 first_save_.clear(); 52 cur_state_.Clear(); 53 saves_.clear(); 54 template_saves_.clear(); 55 while (!state_stack_.empty()) { 56 state_stack_.pop(); 57 } 58 last_save_name_ = false; 59 template_found_ = false; 60 } 61 62 using parse_func_type = const char* (Demangler::*)(const char*); 63 parse_func_type parse_func_; 64 std::vector<parse_func_type> parse_funcs_; 65 std::vector<std::string> saves_; 66 std::vector<std::string> template_saves_; 67 bool last_save_name_; 68 bool template_found_; 69 70 std::string function_name_; 71 std::string function_suffix_; 72 73 struct StateData { ClearStateData74 void Clear() { 75 str.clear(); 76 args.clear(); 77 prefix.clear(); 78 suffixes.clear(); 79 last_save.clear(); 80 } 81 82 std::string str; 83 std::vector<std::string> args; 84 std::string prefix; 85 std::vector<std::string> suffixes; 86 std::string last_save; 87 }; 88 std::stack<StateData> state_stack_; 89 std::string first_save_; 90 StateData cur_state_; 91 92 static const char* GetStringFromLength(const char* name, std::string* str); 93 94 // Parsing functions. 95 const char* ParseComplexString(const char* name); 96 const char* ParseComplexArgument(const char* name); 97 const char* ParseArgumentsAtTopLevel(const char* name); 98 const char* ParseArguments(const char* name); 99 const char* ParseTemplateArguments(const char* name); 100 const char* ParseTemplateArgumentsComplex(const char* name); 101 const char* ParseTemplateLiteral(const char* name); 102 const char* ParseFunctionArgument(const char* name); 103 const char* ParseFunctionName(const char* name); 104 const char* ParseFunctionNameTemplate(const char* name); 105 const char* ParseFunctionTemplateArguments(const char* name); 106 const char* FindFunctionName(const char* name); Fail(const char *)107 const char* Fail(const char*) { return nullptr; } 108 109 // The default maximum string length string to process. 110 static constexpr size_t kMaxDefaultLength = 2048; 111 112 static constexpr const char* kTypes[] = { 113 "signed char", // a 114 "bool", // b 115 "char", // c 116 "double", // d 117 "long double", // e 118 "float", // f 119 "__float128", // g 120 "unsigned char", // h 121 "int", // i 122 "unsigned int", // j 123 nullptr, // k 124 "long", // l 125 "unsigned long", // m 126 "__int128", // n 127 "unsigned __int128", // o 128 nullptr, // p 129 nullptr, // q 130 nullptr, // r 131 "short", // s 132 "unsigned short", // t 133 nullptr, // u 134 "void", // v 135 "wchar_t", // w 136 "long long", // x 137 "unsigned long long", // y 138 "...", // z 139 }; 140 141 static constexpr const char* kDTypes[] = { 142 "auto", // a 143 nullptr, // b 144 nullptr, // c 145 "decimal64", // d 146 "decimal128", // e 147 "decimal32", // f 148 nullptr, // g 149 "half", // h 150 "char32_t", // i 151 nullptr, // j 152 nullptr, // k 153 nullptr, // l 154 nullptr, // m 155 "decltype(nullptr)", // n 156 nullptr, // o 157 nullptr, // p 158 nullptr, // q 159 nullptr, // r 160 "char16_t", // s 161 nullptr, // t 162 nullptr, // u 163 nullptr, // v 164 nullptr, // w 165 nullptr, // x 166 nullptr, // y 167 nullptr, // z 168 }; 169 170 static constexpr const char* kSTypes[] = { 171 "std::allocator", // a 172 "std::basic_string", // b 173 nullptr, // c 174 "std::iostream", // d 175 nullptr, // e 176 nullptr, // f 177 nullptr, // g 178 nullptr, // h 179 "std::istream", // i 180 nullptr, // j 181 nullptr, // k 182 nullptr, // l 183 nullptr, // m 184 nullptr, // n 185 "std::ostream", // o 186 nullptr, // p 187 nullptr, // q 188 nullptr, // r 189 "std::string", // s 190 nullptr, // t 191 nullptr, // u 192 nullptr, // v 193 nullptr, // w 194 nullptr, // x 195 nullptr, // y 196 nullptr, // z 197 }; 198 }; 199 200 #endif // __LIB_DEMANGLE_DEMANGLER_H 201