[d1e0979] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[c0af102] | 7 | // Demangle.cc -- Convert a mangled name into a human readable name.
|
---|
[d1e0979] | 8 | //
|
---|
| 9 | // Author : Rob Schluntz
|
---|
| 10 | // Created On : Thu Jul 19 12:52:41 2018
|
---|
[c649d2c] | 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Mon Nov 6 15:59:00 2023
|
---|
| 13 | // Update Count : 12
|
---|
[d1e0979] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <algorithm>
|
---|
| 17 | #include <sstream>
|
---|
| 18 |
|
---|
[934fa0f] | 19 | #include "AST/Pass.hpp"
|
---|
| 20 | #include "AST/Type.hpp"
|
---|
[d1e0979] | 21 | #include "CodeGen/GenType.h"
|
---|
[c649d2c] | 22 | #include "CodeGen/OperatorTable.h"
|
---|
[8abca06] | 23 | #include "Common/utility.h" // isPrefix
|
---|
[d1e0979] | 24 | #include "Mangler.h"
|
---|
| 25 |
|
---|
[04c77791] | 26 | #define DEBUG
|
---|
[d1e0979] | 27 | #ifdef DEBUG
|
---|
| 28 | #define PRINT(x) x
|
---|
| 29 | #else
|
---|
| 30 | #define PRINT(x) {}
|
---|
| 31 | #endif
|
---|
| 32 |
|
---|
[934fa0f] | 33 | namespace Mangle {
|
---|
| 34 |
|
---|
| 35 | namespace {
|
---|
| 36 |
|
---|
| 37 | struct Demangler {
|
---|
| 38 | private:
|
---|
| 39 | std::string str;
|
---|
| 40 | size_t index = 0;
|
---|
| 41 | using Parser = std::function<ast::Type * ( ast::CV::Qualifiers )>;
|
---|
| 42 | std::vector<std::pair<std::string, Parser>> parsers;
|
---|
| 43 | public:
|
---|
| 44 | Demangler( const std::string & str );
|
---|
| 45 |
|
---|
| 46 | bool done() const { return str.size() <= index; }
|
---|
| 47 | char cur() const { assert( !done() ); return str[index]; }
|
---|
| 48 | bool expect( char ch ) { return str[index++] == ch; }
|
---|
| 49 |
|
---|
| 50 | bool isPrefix( const std::string & pref );
|
---|
| 51 | bool extractNumber( size_t & out );
|
---|
| 52 | bool extractName( std::string & out );
|
---|
| 53 | bool stripMangleName( std::string & name );
|
---|
| 54 |
|
---|
| 55 | ast::Type * parseFunction( ast::CV::Qualifiers tq );
|
---|
| 56 | ast::Type * parseTuple( ast::CV::Qualifiers tq );
|
---|
| 57 | ast::Type * parsePointer( ast::CV::Qualifiers tq );
|
---|
| 58 | ast::Type * parseArray( ast::CV::Qualifiers tq );
|
---|
| 59 | ast::Type * parseStruct( ast::CV::Qualifiers tq );
|
---|
| 60 | ast::Type * parseUnion( ast::CV::Qualifiers tq );
|
---|
| 61 | ast::Type * parseEnum( ast::CV::Qualifiers tq );
|
---|
| 62 | ast::Type * parseType( ast::CV::Qualifiers tq );
|
---|
| 63 | ast::Type * parseZero( ast::CV::Qualifiers tq );
|
---|
| 64 | ast::Type * parseOne( ast::CV::Qualifiers tq );
|
---|
| 65 |
|
---|
| 66 | ast::Type * parseType();
|
---|
| 67 | bool parse( std::string & name, ast::Type *& type );
|
---|
| 68 | };
|
---|
| 69 |
|
---|
| 70 | Demangler::Demangler(const std::string & str) : str(str) {
|
---|
| 71 | for (size_t k = 0; k < ast::BasicType::NUMBER_OF_BASIC_TYPES; ++k) {
|
---|
| 72 | parsers.emplace_back(Encoding::basicTypes[k], [k]( ast::CV::Qualifiers tq ) {
|
---|
| 73 | PRINT( std::cerr << "basic type: " << k << std::endl; )
|
---|
| 74 | return new ast::BasicType( (ast::BasicType::Kind)k, tq );
|
---|
| 75 | });
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | for (size_t k = 0; k < ast::TypeDecl::NUMBER_OF_KINDS; ++k) {
|
---|
| 79 | static const std::string typeVariableNames[] = { "DT", "DST", "OT", "FT", "TT", "ALT", };
|
---|
| 80 | static_assert(
|
---|
| 81 | sizeof(typeVariableNames)/sizeof(typeVariableNames[0]) == ast::TypeDecl::NUMBER_OF_KINDS,
|
---|
| 82 | "Each type variable kind should have a demangle name prefix"
|
---|
| 83 | );
|
---|
| 84 | parsers.emplace_back(Encoding::typeVariables[k], [k, this]( ast::CV::Qualifiers tq ) -> ast::TypeInstType * {
|
---|
| 85 | PRINT( std::cerr << "type variable type: " << k << std::endl; )
|
---|
| 86 | size_t N;
|
---|
| 87 | if (!extractNumber(N)) return nullptr;
|
---|
| 88 | return new ast::TypeInstType(
|
---|
| 89 | toString(typeVariableNames[k], N),
|
---|
| 90 | (ast::TypeDecl::Kind)k,
|
---|
| 91 | tq );
|
---|
| 92 | });
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | parsers.emplace_back(Encoding::void_t, [this]( ast::CV::Qualifiers tq ) { return new ast::VoidType(tq); });
|
---|
| 96 | parsers.emplace_back(Encoding::function, [this]( ast::CV::Qualifiers tq ) { return parseFunction(tq); });
|
---|
| 97 | parsers.emplace_back(Encoding::pointer, [this]( ast::CV::Qualifiers tq ) { return parsePointer(tq); });
|
---|
| 98 | parsers.emplace_back(Encoding::array, [this]( ast::CV::Qualifiers tq ) { return parseArray(tq); });
|
---|
| 99 | parsers.emplace_back(Encoding::tuple, [this]( ast::CV::Qualifiers tq ) { return parseTuple(tq); });
|
---|
| 100 | parsers.emplace_back(Encoding::struct_t, [this]( ast::CV::Qualifiers tq ) { return parseStruct(tq); });
|
---|
| 101 | parsers.emplace_back(Encoding::union_t, [this]( ast::CV::Qualifiers tq ) { return parseUnion(tq); });
|
---|
| 102 | parsers.emplace_back(Encoding::enum_t, [this]( ast::CV::Qualifiers tq ) { return parseEnum(tq); });
|
---|
| 103 | parsers.emplace_back(Encoding::type, [this]( ast::CV::Qualifiers tq ) { return parseType(tq); });
|
---|
| 104 | parsers.emplace_back(Encoding::zero, []( ast::CV::Qualifiers tq ) { return new ast::ZeroType(tq); });
|
---|
| 105 | parsers.emplace_back(Encoding::one, []( ast::CV::Qualifiers tq ) { return new ast::OneType(tq); });
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool Demangler::extractNumber( size_t & out ) {
|
---|
| 109 | std::stringstream numss;
|
---|
| 110 | if ( str.size() <= index ) return false;
|
---|
| 111 | while ( isdigit( str[index] ) ) {
|
---|
| 112 | numss << str[index];
|
---|
| 113 | ++index;
|
---|
| 114 | if ( str.size() == index ) break;
|
---|
| 115 | }
|
---|
| 116 | if ( !(numss >> out) ) return false;
|
---|
| 117 | PRINT( std::cerr << "extractNumber success: " << out << std::endl; )
|
---|
| 118 | return true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | bool Demangler::extractName( std::string & out ) {
|
---|
| 122 | size_t len;
|
---|
| 123 | if ( !extractNumber(len) ) return false;
|
---|
| 124 | if ( str.size() < index + len ) return false;
|
---|
| 125 | out = str.substr( index, len );
|
---|
| 126 | index += len;
|
---|
| 127 | PRINT( std::cerr << "extractName success: " << out << std::endl; )
|
---|
| 128 | return true;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | bool Demangler::isPrefix( const std::string & pref ) {
|
---|
| 132 | // Wraps the utility isPrefix function.
|
---|
| 133 | if ( ::isPrefix( str, pref, index ) ) {
|
---|
| 134 | index += pref.size();
|
---|
| 135 | return true;
|
---|
| 136 | }
|
---|
| 137 | return false;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | // strips __NAME__cfa__TYPE_N, where N is [0-9]+: returns str is a match is found, returns empty string otherwise
|
---|
| 141 | bool Demangler::stripMangleName( std::string & name ) {
|
---|
| 142 | PRINT( std::cerr << "====== " << str.size() << " " << str << std::endl; )
|
---|
| 143 | if (str.size() < 2+Encoding::manglePrefix.size()) return false; // +2 for at least _1 suffix
|
---|
| 144 | if ( !isPrefix(Encoding::manglePrefix) || !isdigit(str.back() ) ) return false;
|
---|
| 145 |
|
---|
| 146 | if (!extractName(name)) return false;
|
---|
| 147 |
|
---|
| 148 | // Find bounds for type.
|
---|
| 149 | PRINT( std::cerr << index << " " << str.size() << std::endl; )
|
---|
| 150 | PRINT( std::cerr << "[");
|
---|
| 151 | while (isdigit(str.back())) {
|
---|
| 152 | PRINT(std::cerr << ".");
|
---|
| 153 | str.pop_back();
|
---|
| 154 | if (str.size() <= index) return false;
|
---|
| 155 | }
|
---|
| 156 | PRINT( std::cerr << "]" << std::endl );
|
---|
| 157 | if (str.back() != '_') return false;
|
---|
| 158 | str.pop_back();
|
---|
| 159 | PRINT( std::cerr << str.size() << " " << name << " " << str.substr(index) << std::endl; )
|
---|
| 160 | return index < str.size();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | ast::Type * Demangler::parseFunction( ast::CV::Qualifiers tq ) {
|
---|
| 164 | PRINT( std::cerr << "function..." << std::endl; )
|
---|
| 165 | if ( done() ) return nullptr;
|
---|
| 166 | ast::FunctionType * ftype = new ast::FunctionType( ast::FixedArgs, tq );
|
---|
| 167 | std::unique_ptr<ast::Type> manager( ftype );
|
---|
| 168 | ast::Type * retVal = parseType();
|
---|
| 169 | if ( !retVal ) return nullptr;
|
---|
| 170 | PRINT( std::cerr << "with return type: " << retVal << std::endl; )
|
---|
| 171 | ftype->returns.emplace_back( retVal );
|
---|
| 172 | if ( done() || !expect('_') ) return nullptr;
|
---|
| 173 | while ( !done() ) {
|
---|
| 174 | PRINT( std::cerr << "got ch: " << cur() << std::endl; )
|
---|
| 175 | if ( cur() == '_' ) return manager.release();
|
---|
| 176 | ast::Type * param = parseType();
|
---|
| 177 | if ( !param ) return nullptr;
|
---|
| 178 | PRINT( std::cerr << "with parameter : " << param << std::endl; )
|
---|
| 179 | ftype->params.emplace_back( param );
|
---|
| 180 | }
|
---|
| 181 | return nullptr;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | ast::Type * Demangler::parseTuple( ast::CV::Qualifiers tq ) {
|
---|
| 185 | PRINT( std::cerr << "tuple..." << std::endl; )
|
---|
| 186 | std::vector<ast::ptr<ast::Type>> types;
|
---|
| 187 | size_t ncomponents;
|
---|
| 188 | if ( !extractNumber(ncomponents) ) return nullptr;
|
---|
| 189 | for ( size_t i = 0; i < ncomponents; ++i ) {
|
---|
| 190 | if ( done() ) return nullptr;
|
---|
| 191 | PRINT( std::cerr << "got ch: " << cur() << std::endl; )
|
---|
| 192 | ast::Type * t = parseType();
|
---|
| 193 | if ( !t ) return nullptr;
|
---|
| 194 | PRINT( std::cerr << "with type : " << t << std::endl; )
|
---|
| 195 | types.push_back( t );
|
---|
| 196 | }
|
---|
| 197 | return new ast::TupleType( std::move( types ), tq );
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | ast::Type * Demangler::parsePointer( ast::CV::Qualifiers tq ) {
|
---|
| 201 | PRINT( std::cerr << "pointer..." << std::endl; )
|
---|
| 202 | ast::Type * t = parseType();
|
---|
| 203 | if ( !t ) return nullptr;
|
---|
| 204 | return new ast::PointerType( t, tq );
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | ast::Type * Demangler::parseArray( ast::CV::Qualifiers tq ) {
|
---|
| 208 | PRINT( std::cerr << "array..." << std::endl; )
|
---|
| 209 | size_t length;
|
---|
| 210 | if ( !extractNumber(length) ) return nullptr;
|
---|
| 211 | ast::Type * t = parseType();
|
---|
| 212 | if ( !t ) return nullptr;
|
---|
| 213 | return new ast::ArrayType(
|
---|
| 214 | t,
|
---|
| 215 | ast::ConstantExpr::from_ulong( CodeLocation(), length ),
|
---|
| 216 | ast::FixedLen,
|
---|
| 217 | ast::DynamicDim,
|
---|
| 218 | tq );
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | ast::Type * Demangler::parseStruct( ast::CV::Qualifiers tq ) {
|
---|
| 222 | PRINT( std::cerr << "struct..." << std::endl; )
|
---|
| 223 | std::string name;
|
---|
| 224 | if ( !extractName(name) ) return nullptr;
|
---|
| 225 | return new ast::StructInstType( name, tq );
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | ast::Type * Demangler::parseUnion( ast::CV::Qualifiers tq ) {
|
---|
| 229 | PRINT( std::cerr << "union..." << std::endl; )
|
---|
| 230 | std::string name;
|
---|
| 231 | if ( !extractName(name) ) return nullptr;
|
---|
| 232 | return new ast::UnionInstType( name, tq );
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | ast::Type * Demangler::parseEnum( ast::CV::Qualifiers tq ) {
|
---|
| 236 | PRINT( std::cerr << "enum..." << std::endl; )
|
---|
| 237 | std::string name;
|
---|
| 238 | if ( !extractName(name) ) return nullptr;
|
---|
| 239 | return new ast::EnumInstType( name, tq );
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | ast::Type * Demangler::parseType( ast::CV::Qualifiers tq ) {
|
---|
| 243 | PRINT( std::cerr << "type..." << std::endl; )
|
---|
| 244 | std::string name;
|
---|
| 245 | if ( !extractName(name) ) return nullptr;
|
---|
| 246 | PRINT( std::cerr << "typename..." << name << std::endl; )
|
---|
| 247 | return new ast::TypeInstType( name, ast::TypeDecl::Dtype, tq );
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | ast::Type * Demangler::parseType() {
|
---|
| 251 | if (done()) return nullptr;
|
---|
| 252 |
|
---|
| 253 | if (isPrefix(Encoding::forall)) {
|
---|
| 254 | PRINT( std::cerr << "polymorphic with..." << std::endl; )
|
---|
| 255 | size_t dcount, fcount, vcount, acount;
|
---|
| 256 | if ( !extractNumber(dcount) ) return nullptr;
|
---|
| 257 | PRINT( std::cerr << dcount << " dtypes" << std::endl; )
|
---|
| 258 | if ( !expect('_') ) return nullptr;
|
---|
| 259 | if ( !extractNumber(fcount) ) return nullptr;
|
---|
| 260 | PRINT( std::cerr << fcount << " ftypes" << std::endl; )
|
---|
| 261 | if ( !expect('_')) return nullptr;
|
---|
| 262 | if ( !extractNumber(vcount)) return nullptr;
|
---|
| 263 | PRINT( std::cerr << vcount << " ttypes" << std::endl; )
|
---|
| 264 | if ( !expect('_') ) return nullptr;
|
---|
| 265 | if ( !extractNumber(acount) ) return nullptr;
|
---|
| 266 | PRINT( std::cerr << acount << " assertions" << std::endl; )
|
---|
| 267 | if ( !expect('_') ) return nullptr;
|
---|
| 268 | for ( size_t i = 0 ; i < acount ; ++i ) {
|
---|
| 269 | // TODO: need to recursively parse assertions, but for now just return nullptr so that
|
---|
| 270 | // demangler does not crash if there are assertions
|
---|
| 271 | return nullptr;
|
---|
| 272 | }
|
---|
| 273 | if ( !expect('_') ) return nullptr;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | ast::CV::Qualifiers tq;
|
---|
| 277 | while (true) {
|
---|
| 278 | auto qual = std::find_if(Encoding::qualifiers.begin(), Encoding::qualifiers.end(), [this](decltype(Encoding::qualifiers)::value_type val) {
|
---|
| 279 | return isPrefix(val.second);
|
---|
| 280 | });
|
---|
| 281 | if (qual == Encoding::qualifiers.end()) break;
|
---|
| 282 | tq |= qual->first;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | // Find the correct type parser and then apply it.
|
---|
| 286 | auto iter = std::find_if(parsers.begin(), parsers.end(), [this](std::pair<std::string, Parser> & p) {
|
---|
| 287 | return isPrefix(p.first);
|
---|
| 288 | });
|
---|
| 289 | assertf(iter != parsers.end(), "Unhandled type letter: %c at index: %zd", cur(), index);
|
---|
| 290 | ast::Type * ret = iter->second(tq);
|
---|
| 291 | if ( !ret ) return nullptr;
|
---|
| 292 | return ret;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | bool Demangler::parse( std::string & name, ast::Type *& type) {
|
---|
| 296 | if ( !stripMangleName(name) ) return false;
|
---|
| 297 | PRINT( std::cerr << "stripped name: " << name << std::endl; )
|
---|
| 298 | ast::Type * t = parseType();
|
---|
| 299 | if ( !t ) return false;
|
---|
| 300 | type = t;
|
---|
| 301 | return true;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | std::string demangle( const std::string & mangleName ) {
|
---|
| 305 | using namespace CodeGen;
|
---|
| 306 | Demangler demangler( mangleName );
|
---|
| 307 | std::string name;
|
---|
| 308 | ast::Type * type = nullptr;
|
---|
| 309 | if ( !demangler.parse( name, type ) ) return mangleName;
|
---|
| 310 | ast::readonly<ast::Type> roType = type;
|
---|
| 311 | if ( auto info = operatorLookupByOutput( name ) ) name = info->inputName;
|
---|
| 312 | return genType( type, name, Options( false, false, false, false ) );
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | } // namespace
|
---|
| 316 |
|
---|
| 317 | } // namespace Mangle
|
---|
| 318 |
|
---|
[d1e0979] | 319 | extern "C" {
|
---|
[90cac45] | 320 | char * cforall_demangle(const char * mangleName, int option __attribute__((unused))) {
|
---|
[934fa0f] | 321 | const std::string & demangleName = Mangle::demangle(mangleName);
|
---|
[f47d0ad] | 322 | return strdup(demangleName.c_str());
|
---|
| 323 | }
|
---|
[d1e0979] | 324 | }
|
---|
| 325 |
|
---|
| 326 | // Local Variables: //
|
---|
| 327 | // tab-width: 4 //
|
---|
| 328 | // mode: c++ //
|
---|
| 329 | // compile-command: "make install" //
|
---|
| 330 | // End: //
|
---|