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* AppendOperatorString(const char* name); 43 void Save(const std::string& str, bool is_name); 44 45 private: Clear()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 { ClearStateData69 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); 95 const char* ParseFunctionArgument(const char* name); 96 const char* ParseFunctionName(const char* name); 97 const char* FindFunctionName(const char* name); Fail(const char *)98 const char* Fail(const char*) { return nullptr; } 99 100 // The default maximum string length string to process. 101 static constexpr size_t kMaxDefaultLength = 2048; 102 103 static constexpr const char* kTypes[] = { 104 "signed char", // a 105 "bool", // b 106 "char", // c 107 "double", // d 108 "long double", // e 109 "float", // f 110 "__float128", // g 111 "unsigned char", // h 112 "int", // i 113 "unsigned int", // j 114 nullptr, // k 115 "long", // l 116 "unsigned long", // m 117 "__int128", // n 118 "unsigned __int128", // o 119 nullptr, // p 120 nullptr, // q 121 nullptr, // r 122 "short", // s 123 "unsigned short", // t 124 nullptr, // u 125 "void", // v 126 "wchar_t", // w 127 "long long", // x 128 "unsigned long long", // y 129 "...", // z 130 }; 131 132 static constexpr const char* kDTypes[] = { 133 "auto", // a 134 nullptr, // b 135 nullptr, // c 136 "decimal64", // d 137 "decimal128", // e 138 "decimal32", // f 139 nullptr, // g 140 "half", // h 141 "char32_t", // i 142 nullptr, // j 143 nullptr, // k 144 nullptr, // l 145 nullptr, // m 146 "decltype(nullptr)", // n 147 nullptr, // o 148 nullptr, // p 149 nullptr, // q 150 nullptr, // r 151 "char16_t", // s 152 nullptr, // t 153 nullptr, // u 154 nullptr, // v 155 nullptr, // w 156 nullptr, // x 157 nullptr, // y 158 nullptr, // z 159 }; 160 161 static constexpr const char* kSTypes[] = { 162 "std::allocator", // a 163 "std::basic_string", // b 164 nullptr, // c 165 "std::iostream", // d 166 nullptr, // e 167 nullptr, // f 168 nullptr, // g 169 nullptr, // h 170 "std::istream", // i 171 nullptr, // j 172 nullptr, // k 173 nullptr, // l 174 nullptr, // m 175 nullptr, // n 176 "std::ostream", // o 177 nullptr, // p 178 nullptr, // q 179 nullptr, // r 180 "std::string", // s 181 nullptr, // t 182 nullptr, // u 183 nullptr, // v 184 nullptr, // w 185 nullptr, // x 186 nullptr, // y 187 nullptr, // z 188 }; 189 }; 190 191 #endif // __LIB_DEMANGLE_DEMANGLER_H 192