Ignore:
Timestamp:
Nov 13, 2023, 3:43:43 AM (23 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
25f2798
Parents:
0030b508 (diff), 2174191 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Demangle.cc

    r0030b508 rfc12f05  
    99// Author           : Rob Schluntz
    1010// Created On       : Thu Jul 19 12:52:41 2018
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jan 11 21:28:27 2021
    13 // Update Count     : 11
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Mon Nov  6 15:59:00 2023
     13// Update Count     : 12
    1414//
    1515
     
    1717#include <sstream>
    1818
     19#include "AST/Pass.hpp"
     20#include "AST/Type.hpp"
    1921#include "CodeGen/GenType.h"
    20 #include "Common/PassVisitor.h"
     22#include "CodeGen/OperatorTable.h"
    2123#include "Common/utility.h"                                                             // isPrefix
    2224#include "Mangler.h"
    23 #include "SynTree/Type.h"
    24 #include "SynTree/Declaration.h"
    2525
    2626#define DEBUG
     
    3131#endif
    3232
     33namespace Mangle {
     34
    3335namespace {
    34         struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
    35                 std::string typeString;
    36                 GenType( const std::string &typeString );
    37 
    38                 void previsit( BaseSyntaxNode * );
    39                 void postvisit( BaseSyntaxNode * );
    40 
    41                 void postvisit( FunctionType * funcType );
    42                 void postvisit( VoidType * voidType );
    43                 void postvisit( BasicType * basicType );
    44                 void postvisit( PointerType * pointerType );
    45                 void postvisit( ArrayType * arrayType );
    46                 void postvisit( ReferenceType * refType );
    47                 void postvisit( StructInstType * structInst );
    48                 void postvisit( UnionInstType * unionInst );
    49                 void postvisit( EnumInstType * enumInst );
    50                 void postvisit( TypeInstType * typeInst );
    51                 void postvisit( TupleType  * tupleType );
    52                 void postvisit( VarArgsType * varArgsType );
    53                 void postvisit( ZeroType * zeroType );
    54                 void postvisit( OneType * oneType );
    55                 void postvisit( GlobalScopeType * globalType );
    56                 void postvisit( QualifiedType * qualType );
    57 
    58           private:
    59                 void handleQualifiers( Type *type );
    60                 std::string handleGeneric( ReferenceToType * refType );
    61                 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
    62         };
    63 
    64   std::string genDemangleType( Type * type, const std::string & baseString ) {
    65                 PassVisitor<GenType> gt( baseString );
    66                 assert( type );
    67                 type->accept( gt );
    68                 return gt.pass.typeString;
    69   }
    70 
    71         GenType::GenType( const std::string &typeString ) : typeString( typeString ) {}
    72 
    73         // *** BaseSyntaxNode
    74         void GenType::previsit( BaseSyntaxNode * ) {
    75                 // turn off automatic recursion for all nodes, to allow each visitor to
    76                 // precisely control the order in which its children are visited.
    77                 visit_children = false;
    78         }
    79 
    80         void GenType::postvisit( BaseSyntaxNode * node ) {
    81                 std::stringstream ss;
    82                 node->print( ss );
    83                 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
    84         }
    85 
    86         void GenType::postvisit( VoidType * voidType ) {
    87                 typeString = "void " + typeString;
    88                 handleQualifiers( voidType );
    89         }
    90 
    91         void GenType::postvisit( BasicType * basicType ) {
    92                 BasicType::Kind kind = basicType->kind;
    93                 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    94                 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
    95                 handleQualifiers( basicType );
    96         }
    97 
    98         void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool ) {
    99                 std::ostringstream os;
    100                 if ( typeString != "" ) {
    101                         if ( typeString[ 0 ] == '*' ) {
    102                                 os << "(" << typeString << ")";
    103                         } else {
    104                                 os << typeString;
    105                         } // if
    106                 } // if
    107                 os << "[";
    108 
    109                 if ( qualifiers.is_const ) {
    110                         os << "const ";
    111                 } // if
    112                 if ( qualifiers.is_volatile ) {
    113                         os << "volatile ";
    114                 } // if
    115                 if ( qualifiers.is_restrict ) {
    116                         os << "__restrict ";
    117                 } // if
    118                 if ( qualifiers.is_atomic ) {
    119                         os << "_Atomic ";
    120                 } // if
    121                 if ( dimension != 0 ) {
    122                         // TODO: ???
    123                         // PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    124                         // dimension->accept( cg );
    125                 } else if ( isVarLen ) {
    126                         // no dimension expression on a VLA means it came in with the * token
    127                         os << "*";
    128                 } // if
    129                 os << "]";
    130 
    131                 typeString = os.str();
    132 
    133                 base->accept( *visitor );
    134         }
    135 
    136         void GenType::postvisit( PointerType * pointerType ) {
    137                 assert( pointerType->base != 0);
    138                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
    139                         assert(false);
    140                         genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
    141                 } else {
    142                         handleQualifiers( pointerType );
    143                         if ( typeString[ 0 ] == '?' ) {
    144                                 typeString = "* " + typeString;
    145                         } else {
    146                                 typeString = "*" + typeString;
    147                         } // if
    148                         pointerType->base->accept( *visitor );
    149                 } // if
    150         }
    151 
    152         void GenType::postvisit( ArrayType * arrayType ) {
    153                 genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
    154         }
    155 
    156         void GenType::postvisit( ReferenceType * refType ) {
    157                 assert( false );
    158                 assert( refType->base != 0);
    159                 handleQualifiers( refType );
    160                 typeString = "&" + typeString;
    161                 refType->base->accept( *visitor );
    162         }
    163 
    164         void GenType::postvisit( FunctionType * funcType ) {
    165                 std::ostringstream os;
    166 
    167                 if ( typeString != "" ) {
    168                         if ( typeString[0] == '*' ) {
    169                                 os << "(" << typeString << ")";
    170                         } else {
    171                                 os << typeString;
    172                         } // if
    173                 } // if
    174 
    175                 /************* parameters ***************/
    176                 const std::list<DeclarationWithType *> &pars = funcType->parameters;
    177 
    178                 if ( pars.empty() ) {
    179                         if ( funcType->get_isVarArgs() ) {
    180                                 os << "()";
    181                         } else {
    182                                 os << "(void)";
    183                         } // if
    184                 } else {
    185                         os << "(" ;
    186 
    187                         unsigned int i = 0;
    188                         for (DeclarationWithType * p : pars) {
    189                                 os << genDemangleType( p->get_type(), "" );
    190                                 if (++i != pars.size()) os << ", ";
    191                         }
    192 
    193                         if ( funcType->get_isVarArgs() ) {
    194                                 os << ", ...";
    195                         } // if
    196                         os << ")";
    197                 } // if
    198 
    199                 typeString = os.str();
    200 
    201                 if ( funcType->returnVals.size() == 0 ) {
    202                         typeString += ": void";
    203                 } else {
    204                         typeString += ": " + genDemangleType(funcType->returnVals.front()->get_type(), "");
    205                 } // if
    206 
    207                 // add forall
    208                 if( ! funcType->forall.empty() ) {
    209                         std::ostringstream os;
    210                         os << "forall(";
    211                         unsigned int i = 0;
    212                         for ( auto td : funcType->forall ) {
    213                                 os << td->typeString() << " " << td->name;
    214                                 if (! td->assertions.empty()) {
    215                                         os << " | { ";
    216                                         unsigned int j = 0;
    217                                         for (DeclarationWithType * assert : td->assertions) {
    218                                                 os << genDemangleType(assert->get_type(), assert->name);
    219                                                 if (++j != td->assertions.size()) os << ", ";
    220                                         }
    221                                         os << "}";
    222                                 }
    223                                 if (++i != funcType->forall.size()) os << ", ";
    224                         }
    225                         os << ")";
    226                         typeString = typeString + " -> " + os.str();
     36
     37struct Demangler {
     38private:
     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;
     43public:
     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
     70Demangler::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
     108bool 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
     121bool 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
     131bool 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
     141bool 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
     163ast::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
     184ast::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
     200ast::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
     207ast::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
     221ast::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
     228ast::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
     235ast::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
     242ast::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
     250ast::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;
    227272                }
    228         }
    229 
    230         std::string GenType::handleGeneric( ReferenceToType * refType ) {
    231                 if ( ! refType->parameters.empty() ) {
    232                         std::ostringstream os;
    233                         // TODO: ???
    234                         // PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    235                         os << "(";
    236                         // cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
    237                         os << ") ";
    238                         return os.str();
    239                 }
    240                 return "";
    241         }
    242 
    243         void GenType::postvisit( StructInstType * structInst )  {
    244                 typeString = "struct " + structInst->name + handleGeneric( structInst ) + " " + typeString;
    245                 handleQualifiers( structInst );
    246         }
    247 
    248         void GenType::postvisit( UnionInstType * unionInst ) {
    249                 typeString = "union " + unionInst->name + handleGeneric( unionInst ) + " " + typeString;
    250                 handleQualifiers( unionInst );
    251         }
    252 
    253         void GenType::postvisit( EnumInstType * enumInst ) {
    254                 typeString = "enum " + enumInst->name + " " + typeString;
    255                 handleQualifiers( enumInst );
    256         }
    257 
    258         void GenType::postvisit( TypeInstType * typeInst ) {
    259                 typeString = typeInst->name + " " + typeString;
    260                 handleQualifiers( typeInst );
    261         }
    262 
    263         void GenType::postvisit( TupleType * tupleType ) {
    264                 unsigned int i = 0;
    265                 std::ostringstream os;
    266                 os << "[";
    267                 for ( Type * t : *tupleType ) {
    268                         i++;
    269                         os << genDemangleType( t, "" ) << (i == tupleType->size() ? "" : ", ");
    270                 }
    271                 os << "] ";
    272                 typeString = os.str() + typeString;
    273         }
    274 
    275         void GenType::postvisit( VarArgsType * varArgsType ) {
    276                 typeString = "__builtin_va_list " + typeString;
    277                 handleQualifiers( varArgsType );
    278         }
    279 
    280         void GenType::postvisit( ZeroType * zeroType ) {
    281                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    282                 typeString = "zero_t " + typeString;
    283                 handleQualifiers( zeroType );
    284         }
    285 
    286         void GenType::postvisit( OneType * oneType ) {
    287                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    288                 typeString = "one_t " + typeString;
    289                 handleQualifiers( oneType );
    290         }
    291 
    292         void GenType::postvisit( GlobalScopeType * globalType ) {
    293                 handleQualifiers( globalType );
    294         }
    295 
    296         void GenType::postvisit( QualifiedType * qualType ) {
    297                 std::ostringstream os;
    298                 os << genDemangleType( qualType->parent, "" ) << "." << genDemangleType( qualType->child, "" ) << typeString;
    299                 typeString = os.str();
    300                 handleQualifiers( qualType );
    301         }
    302 
    303         void GenType::handleQualifiers( Type * type ) {
    304                 if ( type->get_const() ) {
    305                         typeString = "const " + typeString;
    306                 } // if
    307                 if ( type->get_volatile() ) {
    308                         typeString = "volatile " + typeString;
    309                 } // if
    310                 if ( type->get_restrict() ) {
    311                         typeString = "__restrict " + typeString;
    312                 } // if
    313                 if ( type->get_atomic() ) {
    314                         typeString = "_Atomic " + typeString;
    315                 } // if
    316         }
    317 }
    318 
    319 
    320 namespace SymTab {
    321         namespace Mangler {
    322                 namespace {
    323                         struct StringView {
    324                         private:
    325                                 std::string str;
    326                                 size_t idx = 0;
    327                                 // typedef Type * (StringView::*parser)(Type::Qualifiers);
    328                                 typedef std::function<Type * (Type::Qualifiers)> parser;
    329                                 std::vector<std::pair<std::string, parser>> parsers;
    330                         public:
    331                                 StringView(const std::string & str);
    332 
    333                                 bool done() const { return idx >= str.size(); }
    334                                 char cur() const { assert(! done()); return str[idx]; }
    335 
    336                                 bool expect(char ch) { return str[idx++] == ch; }
    337                                 void next(size_t inc = 1) { idx += inc; }
    338 
    339                                 /// determines if `pref` is a prefix of `str`
    340                                 bool isPrefix(const std::string & pref);
    341                                 bool extractNumber(size_t & out);
    342                                 bool extractName(std::string & out);
    343                                 bool stripMangleName(std::string & name);
    344 
    345                                 Type * parseFunction(Type::Qualifiers tq);
    346                                 Type * parseTuple(Type::Qualifiers tq);
    347                                 Type * parseVoid(Type::Qualifiers tq);
    348                                 Type * parsePointer(Type::Qualifiers tq);
    349                                 Type * parseArray(Type::Qualifiers tq);
    350                                 Type * parseStruct(Type::Qualifiers tq);
    351                                 Type * parseUnion(Type::Qualifiers tq);
    352                                 Type * parseEnum(Type::Qualifiers tq);
    353                                 Type * parseType(Type::Qualifiers tq);
    354 
    355                                 Type * parseType();
    356                                 bool parse(std::string & name, Type *& type);
    357                         };
    358 
    359                         StringView::StringView(const std::string & str) : str(str) {
    360                                 // basic types
    361                                 for (size_t k = 0; k < BasicType::NUMBER_OF_BASIC_TYPES; ++k) {
    362                                         parsers.emplace_back(Encoding::basicTypes[k], [k](Type::Qualifiers tq) {
    363                                                 PRINT( std::cerr << "basic type: " << k << std::endl; )
    364                                                 return new BasicType(tq, (BasicType::Kind)k);
    365                                         });
    366                                 }
    367                                 // type variable types
    368                                 for (size_t k = 0; k < TypeDecl::NUMBER_OF_KINDS; ++k) {
    369                                         static const std::string typeVariableNames[] = { "DT", "DST", "OT", "FT", "TT", "ALT", };
    370                                         static_assert(
    371                                                 sizeof(typeVariableNames)/sizeof(typeVariableNames[0]) == TypeDecl::NUMBER_OF_KINDS,
    372                                                 "Each type variable kind should have a demangle name prefix"
    373                                         );
    374                                         parsers.emplace_back(Encoding::typeVariables[k], [k, this](Type::Qualifiers tq) -> TypeInstType * {
    375                                                 PRINT( std::cerr << "type variable type: " << k << std::endl; )
    376                                                 size_t N;
    377                                                 if (! extractNumber(N)) return nullptr;
    378                                                 return new TypeInstType(tq, toString(typeVariableNames[k], N), (TypeDecl::Kind)k != TypeDecl::Ftype);
    379                                         });
    380                                 }
    381                                 // everything else
    382                                 parsers.emplace_back(Encoding::void_t, [this](Type::Qualifiers tq) { return parseVoid(tq); });
    383                                 parsers.emplace_back(Encoding::function, [this](Type::Qualifiers tq) { return parseFunction(tq); });
    384                                 parsers.emplace_back(Encoding::pointer, [this](Type::Qualifiers tq) { return parsePointer(tq); });
    385                                 parsers.emplace_back(Encoding::array, [this](Type::Qualifiers tq) { return parseArray(tq); });
    386                                 parsers.emplace_back(Encoding::tuple, [this](Type::Qualifiers tq) { return parseTuple(tq); });
    387                                 parsers.emplace_back(Encoding::struct_t, [this](Type::Qualifiers tq) { return parseStruct(tq); });
    388                                 parsers.emplace_back(Encoding::union_t, [this](Type::Qualifiers tq) { return parseUnion(tq); });
    389                                 parsers.emplace_back(Encoding::enum_t, [this](Type::Qualifiers tq) { return parseEnum(tq); });
    390                                 parsers.emplace_back(Encoding::type, [this](Type::Qualifiers tq) { return parseType(tq); });
    391                                 parsers.emplace_back(Encoding::zero, [](Type::Qualifiers tq) { return new ZeroType(tq); });
    392                                 parsers.emplace_back(Encoding::one, [](Type::Qualifiers tq) { return new OneType(tq); });
    393                         }
    394 
    395                         bool StringView::extractNumber(size_t & out) {
    396                                 std::stringstream numss;
    397                                 if (idx >= str.size()) return false;
    398                                 while (isdigit(str[idx])) {
    399                                         numss << str[idx];
    400                                         ++idx;
    401                                         if (idx == str.size()) break;
    402                                 }
    403                                 if (! (numss >> out)) return false;
    404                                 PRINT( std::cerr << "extractNumber success: " << out << std::endl; )
    405                                 return true;
    406                         }
    407 
    408                         bool StringView::extractName(std::string & out) {
    409                                 size_t len;
    410                                 if (! extractNumber(len)) return false;
    411                                 if (idx+len > str.size()) return false;
    412                                 out = str.substr(idx, len);
    413                                 idx += len;
    414                                 PRINT( std::cerr << "extractName success: " << out << std::endl; )
    415                                 return true;
    416                         }
    417 
    418                         bool StringView::isPrefix(const std::string & pref) {
    419                                 // if ( pref.size() > str.size()-idx ) return false;
    420                                 // auto its = std::mismatch( pref.begin(), pref.end(), std::next(str.begin(), idx) );
    421                                 // if (its.first == pref.end()) {
    422                                 //      idx += pref.size();
    423                                 //      return true;
    424                                 // }
    425 
    426                                 // This update is untested because there are no tests for this code.
    427                                 if ( ::isPrefix( str, pref, idx ) ) {
    428                                         idx += pref.size();
    429                                         return true;
    430                                 }
    431                                 return false;
    432                         }
    433 
    434                         // strips __NAME__cfa__TYPE_N, where N is [0-9]+: returns str is a match is found, returns empty string otherwise
    435                         bool StringView::stripMangleName(std::string & name) {
    436                                 PRINT( std::cerr << "====== " << str.size() << " " << str << std::endl; )
    437                                 if (str.size() < 2+Encoding::manglePrefix.size()) return false; // +2 for at least _1 suffix
    438                                 if ( ! isPrefix(Encoding::manglePrefix) || ! isdigit(str.back() ) ) return false;
    439 
    440                                 // get name
    441                                 if (! extractName(name)) return false;
    442 
    443                                 // find bounds for type
    444                                 PRINT( std::cerr << idx << " " << str.size() << std::endl; )
    445                                 PRINT( std::cerr << "[");
    446                                 while (isdigit(str.back())) {
    447                                         PRINT(std::cerr << ".");
    448                                         str.pop_back();
    449                                         if (str.size() <= idx) return false;
    450                                 }
    451                                 PRINT( std::cerr << "]" << std::endl );
    452                                 if (str.back() != '_') return false;
    453                                 str.pop_back();
    454                                 PRINT( std::cerr << str.size() << " " << name << " " << str.substr(idx) << std::endl; )
    455                                 return str.size() > idx;
    456                         }
    457 
    458                         Type * StringView::parseFunction(Type::Qualifiers tq) {
    459                                 PRINT( std::cerr << "function..." << std::endl; )
    460                                 if (done()) return nullptr;
    461                                 FunctionType * ftype = new FunctionType( tq, false );
    462                                 std::unique_ptr<Type> manager(ftype);
    463                                 Type * retVal = parseType();
    464                                 if (! retVal) return nullptr;
    465                                 PRINT( std::cerr << "with return type: " << retVal << std::endl; )
    466                                 ftype->returnVals.push_back(ObjectDecl::newObject("", retVal, nullptr));
    467                                 if (done() || ! expect('_')) return nullptr;
    468                                 while (! done()) {
    469                                         PRINT( std::cerr << "got ch: " << cur() << std::endl; )
    470                                         if (cur() == '_') return manager.release();
    471                                         Type * param = parseType();
    472                                         if (! param) return nullptr;
    473                                         PRINT( std::cerr << "with parameter : " << param << std::endl; )
    474                                         ftype->parameters.push_back(ObjectDecl::newObject("", param, nullptr));
    475                                 }
    476                                 return nullptr;
    477                         }
    478 
    479                         Type * StringView::parseTuple(Type::Qualifiers tq) {
    480                                 PRINT( std::cerr << "tuple..." << std::endl; )
    481                                 std::list< Type * > types;
    482                                 size_t ncomponents;
    483                                 if (! extractNumber(ncomponents)) return nullptr;
    484                                 for (size_t i = 0; i < ncomponents; ++i) {
    485                                         // TODO: delete all on return
    486                                         if (done()) return nullptr;
    487                                         PRINT( std::cerr << "got ch: " << cur() << std::endl; )
    488                                         Type * t = parseType();
    489                                         if (! t) return nullptr;
    490                                         PRINT( std::cerr << "with type : " << t << std::endl; )
    491                                         types.push_back(t);
    492                                 }
    493                                 return new TupleType( tq, types );
    494                         }
    495 
    496                         Type * StringView::parseVoid(Type::Qualifiers tq) {
    497                                 return new VoidType( tq );
    498                         }
    499 
    500                         Type * StringView::parsePointer(Type::Qualifiers tq) {
    501                                 PRINT( std::cerr << "pointer..." << std::endl; )
    502                                 Type * t = parseType();
    503                                 if (! t) return nullptr;
    504                                 return new PointerType( tq, t );
    505                         }
    506 
    507                         Type * StringView::parseArray(Type::Qualifiers tq) {
    508                                 PRINT( std::cerr << "array..." << std::endl; )
    509                                 size_t length;
    510                                 if (! extractNumber(length)) return nullptr;
    511                                 Type * t = parseType();
    512                                 if (! t) return nullptr;
    513                                 return new ArrayType( tq, t, new ConstantExpr( Constant::from_ulong(length) ), false, false );
    514                         }
    515 
    516                         Type * StringView::parseStruct(Type::Qualifiers tq) {
    517                                 PRINT( std::cerr << "struct..." << std::endl; )
    518                                 std::string name;
    519                                 if (! extractName(name)) return nullptr;
    520                                 return new StructInstType(tq, name);
    521                         }
    522 
    523                         Type * StringView::parseUnion(Type::Qualifiers tq) {
    524                                 PRINT( std::cerr << "union..." << std::endl; )
    525                                 std::string name;
    526                                 if (! extractName(name)) return nullptr;
    527                                 return new UnionInstType(tq, name);
    528                         }
    529 
    530                         Type * StringView::parseEnum(Type::Qualifiers tq) {
    531                                 PRINT( std::cerr << "enum..." << std::endl; )
    532                                 std::string name;
    533                                 if (! extractName(name)) return nullptr;
    534                                 return new EnumInstType(tq, name);
    535                         }
    536 
    537                         Type * StringView::parseType(Type::Qualifiers tq) {
    538                                 PRINT( std::cerr << "type..." << std::endl; )
    539                                 std::string name;
    540                                 if (! extractName(name)) return nullptr;
    541                                 PRINT( std::cerr << "typename..." << name << std::endl; )
    542                                 return new TypeInstType(tq, name, false);
    543                         }
    544 
    545                         Type * StringView::parseType() {
    546                                 if (done()) return nullptr;
    547 
    548                                 std::list<TypeDecl *> forall;
    549                                 if (isPrefix(Encoding::forall)) {
    550                                         PRINT( std::cerr << "polymorphic with..." << std::endl; )
    551                                         size_t dcount, fcount, vcount, acount;
    552                                         if (! extractNumber(dcount)) return nullptr;
    553                                         PRINT( std::cerr << dcount << " dtypes" << std::endl; )
    554                                         if (! expect('_')) return nullptr;
    555                                         if (! extractNumber(fcount)) return nullptr;
    556                                         PRINT( std::cerr << fcount << " ftypes" << std::endl; )
    557                                         if (! expect('_')) return nullptr;
    558                                         if (! extractNumber(vcount)) return nullptr;
    559                                         PRINT( std::cerr << vcount << " ttypes" << std::endl; )
    560                                         if (! expect('_')) return nullptr;
    561                                         if (! extractNumber(acount)) return nullptr;
    562                                         PRINT( std::cerr << acount << " assertions" << std::endl; )
    563                                         if (! expect('_')) return nullptr;
    564                                         for (size_t i = 0; i < acount; ++i) {
    565                                                 // TODO: need to recursively parse assertions, but for now just return nullptr so that
    566                                                 // demangler does not crash if there are assertions
    567                                                 return nullptr;
    568                                         }
    569                                         if (! expect('_')) return nullptr;
    570                                 }
    571 
    572                                 // qualifiers
    573                                 Type::Qualifiers tq;
    574                                 while (true) {
    575                                         auto qual = std::find_if(Encoding::qualifiers.begin(), Encoding::qualifiers.end(), [this](decltype(Encoding::qualifiers)::value_type val) {
    576                                                 return isPrefix(val.second);
    577                                         });
    578                                         if (qual == Encoding::qualifiers.end()) break;
    579                                         tq |= qual->first;
    580                                 }
    581 
    582                                 // find the correct type parser and use it
    583                                 auto iter = std::find_if(parsers.begin(), parsers.end(), [this](std::pair<std::string, parser> & p) {
    584                                         return isPrefix(p.first);
    585                                 });
    586                                 assertf(iter != parsers.end(), "Unhandled type letter: %c at index: %zd", cur(), idx);
    587                                 Type * ret = iter->second(tq);
    588                                 if (! ret) return nullptr;
    589                                 ret->forall = std::move(forall);
    590                                 return ret;
    591                         }
    592 
    593                         bool StringView::parse(std::string & name, Type *& type) {
    594                                 if (! stripMangleName(name)) return false;
    595                                 PRINT( std::cerr << "stripped name: " << name << std::endl; )
    596                                 Type * t = parseType();
    597                                 if (! t) return false;
    598                                 type = t;
    599                                 return true;
    600                         }
    601 
    602                         std::string demangle(const std::string & mangleName) {
    603                                 SymTab::Mangler::StringView view(mangleName);
    604                                 std::string name;
    605                                 Type * type = nullptr;
    606                                 if (! view.parse(name, type)) return mangleName;
    607                                 std::unique_ptr<Type> manager(type);
    608                                 return genDemangleType(type, name);
    609                         }
    610                 } // namespace
    611         } // namespace Mangler
    612 } // namespace SymTab
     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
     295bool 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
     304std::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
    613318
    614319extern "C" {
    615320        char * cforall_demangle(const char * mangleName, int option __attribute__((unused))) {
    616                 const std::string & demangleName = SymTab::Mangler::demangle(mangleName);
     321                const std::string & demangleName = Mangle::demangle(mangleName);
    617322                return strdup(demangleName.c_str());
    618323        }
Note: See TracChangeset for help on using the changeset viewer.