Changeset 0589e83 for src/SymTab


Ignore:
Timestamp:
Nov 7, 2023, 11:14:11 AM (8 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
934fa0f
Parents:
c46c999
Message:

The demangler now uses the compiler's genType. The only difference I can find is that the demangler genType wrote functions ': RET_TYPE' instead of the C style. If this or a different change turns out to be imported it will have to be replicated post-translation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Demangle.cc

    rc46c999 r0589e83  
    3131#define PRINT(x) {}
    3232#endif
    33 
    34 namespace {
    35         struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
    36                 std::string typeString;
    37                 GenType( const std::string &typeString );
    38 
    39                 void previsit( BaseSyntaxNode * );
    40                 void postvisit( BaseSyntaxNode * );
    41 
    42                 void postvisit( FunctionType * funcType );
    43                 void postvisit( VoidType * voidType );
    44                 void postvisit( BasicType * basicType );
    45                 void postvisit( PointerType * pointerType );
    46                 void postvisit( ArrayType * arrayType );
    47                 void postvisit( ReferenceType * refType );
    48                 void postvisit( StructInstType * structInst );
    49                 void postvisit( UnionInstType * unionInst );
    50                 void postvisit( EnumInstType * enumInst );
    51                 void postvisit( TypeInstType * typeInst );
    52                 void postvisit( TupleType  * tupleType );
    53                 void postvisit( VarArgsType * varArgsType );
    54                 void postvisit( ZeroType * zeroType );
    55                 void postvisit( OneType * oneType );
    56                 void postvisit( GlobalScopeType * globalType );
    57                 void postvisit( QualifiedType * qualType );
    58 
    59           private:
    60                 void handleQualifiers( Type *type );
    61                 std::string handleGeneric( ReferenceToType * refType );
    62                 void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
    63         };
    64 
    65   std::string genDemangleType( Type * type, const std::string & baseString ) {
    66                 PassVisitor<GenType> gt( baseString );
    67                 assert( type );
    68                 type->accept( gt );
    69                 return gt.pass.typeString;
    70   }
    71 
    72         GenType::GenType( const std::string &typeString ) : typeString( typeString ) {}
    73 
    74         // *** BaseSyntaxNode
    75         void GenType::previsit( BaseSyntaxNode * ) {
    76                 // turn off automatic recursion for all nodes, to allow each visitor to
    77                 // precisely control the order in which its children are visited.
    78                 visit_children = false;
    79         }
    80 
    81         void GenType::postvisit( BaseSyntaxNode * node ) {
    82                 std::stringstream ss;
    83                 node->print( ss );
    84                 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
    85         }
    86 
    87         void GenType::postvisit( VoidType * voidType ) {
    88                 typeString = "void " + typeString;
    89                 handleQualifiers( voidType );
    90         }
    91 
    92         void GenType::postvisit( BasicType * basicType ) {
    93                 BasicType::Kind kind = basicType->kind;
    94                 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    95                 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
    96                 handleQualifiers( basicType );
    97         }
    98 
    99         void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool ) {
    100                 std::ostringstream os;
    101                 if ( typeString != "" ) {
    102                         if ( typeString[ 0 ] == '*' ) {
    103                                 os << "(" << typeString << ")";
    104                         } else {
    105                                 os << typeString;
    106                         } // if
    107                 } // if
    108                 os << "[";
    109 
    110                 if ( qualifiers.is_const ) {
    111                         os << "const ";
    112                 } // if
    113                 if ( qualifiers.is_volatile ) {
    114                         os << "volatile ";
    115                 } // if
    116                 if ( qualifiers.is_restrict ) {
    117                         os << "__restrict ";
    118                 } // if
    119                 if ( qualifiers.is_atomic ) {
    120                         os << "_Atomic ";
    121                 } // if
    122                 if ( dimension != 0 ) {
    123                         // TODO: ???
    124                         // PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    125                         // dimension->accept( cg );
    126                 } else if ( isVarLen ) {
    127                         // no dimension expression on a VLA means it came in with the * token
    128                         os << "*";
    129                 } // if
    130                 os << "]";
    131 
    132                 typeString = os.str();
    133 
    134                 base->accept( *visitor );
    135         }
    136 
    137         void GenType::postvisit( PointerType * pointerType ) {
    138                 assert( pointerType->base != 0);
    139                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
    140                         assert(false);
    141                         genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
    142                 } else {
    143                         handleQualifiers( pointerType );
    144                         if ( typeString[ 0 ] == '?' ) {
    145                                 typeString = "* " + typeString;
    146                         } else {
    147                                 typeString = "*" + typeString;
    148                         } // if
    149                         pointerType->base->accept( *visitor );
    150                 } // if
    151         }
    152 
    153         void GenType::postvisit( ArrayType * arrayType ) {
    154                 genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
    155         }
    156 
    157         void GenType::postvisit( ReferenceType * refType ) {
    158                 assert( false );
    159                 assert( refType->base != 0);
    160                 handleQualifiers( refType );
    161                 typeString = "&" + typeString;
    162                 refType->base->accept( *visitor );
    163         }
    164 
    165         void GenType::postvisit( FunctionType * funcType ) {
    166                 std::ostringstream os;
    167 
    168                 if ( typeString != "" ) {
    169                         if ( typeString[0] == '*' ) {
    170                                 os << "(" << typeString << ")";
    171                         } else {
    172                                 os << typeString;
    173                         } // if
    174                 } // if
    175 
    176                 /************* parameters ***************/
    177                 const std::list<DeclarationWithType *> &pars = funcType->parameters;
    178 
    179                 if ( pars.empty() ) {
    180                         if ( funcType->get_isVarArgs() ) {
    181                                 os << "()";
    182                         } else {
    183                                 os << "(void)";
    184                         } // if
    185                 } else {
    186                         os << "(" ;
    187 
    188                         unsigned int i = 0;
    189                         for (DeclarationWithType * p : pars) {
    190                                 os << genDemangleType( p->get_type(), "" );
    191                                 if (++i != pars.size()) os << ", ";
    192                         }
    193 
    194                         if ( funcType->get_isVarArgs() ) {
    195                                 os << ", ...";
    196                         } // if
    197                         os << ")";
    198                 } // if
    199 
    200                 typeString = os.str();
    201 
    202                 if ( funcType->returnVals.size() == 0 ) {
    203                         typeString += ": void";
    204                 } else {
    205                         typeString += ": " + genDemangleType(funcType->returnVals.front()->get_type(), "");
    206                 } // if
    207 
    208                 // add forall
    209                 if( ! funcType->forall.empty() ) {
    210                         std::ostringstream os;
    211                         os << "forall(";
    212                         unsigned int i = 0;
    213                         for ( auto td : funcType->forall ) {
    214                                 os << td->typeString() << " " << td->name;
    215                                 if (! td->assertions.empty()) {
    216                                         os << " | { ";
    217                                         unsigned int j = 0;
    218                                         for (DeclarationWithType * assert : td->assertions) {
    219                                                 os << genDemangleType(assert->get_type(), assert->name);
    220                                                 if (++j != td->assertions.size()) os << ", ";
    221                                         }
    222                                         os << "}";
    223                                 }
    224                                 if (++i != funcType->forall.size()) os << ", ";
    225                         }
    226                         os << ")";
    227                         typeString = typeString + " -> " + os.str();
    228                 }
    229         }
    230 
    231         std::string GenType::handleGeneric( ReferenceToType * refType ) {
    232                 if ( ! refType->parameters.empty() ) {
    233                         std::ostringstream os;
    234                         // TODO: ???
    235                         // PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    236                         os << "(";
    237                         // cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
    238                         os << ") ";
    239                         return os.str();
    240                 }
    241                 return "";
    242         }
    243 
    244         void GenType::postvisit( StructInstType * structInst )  {
    245                 typeString = "struct " + structInst->name + handleGeneric( structInst ) + " " + typeString;
    246                 handleQualifiers( structInst );
    247         }
    248 
    249         void GenType::postvisit( UnionInstType * unionInst ) {
    250                 typeString = "union " + unionInst->name + handleGeneric( unionInst ) + " " + typeString;
    251                 handleQualifiers( unionInst );
    252         }
    253 
    254         void GenType::postvisit( EnumInstType * enumInst ) {
    255                 typeString = "enum " + enumInst->name + " " + typeString;
    256                 handleQualifiers( enumInst );
    257         }
    258 
    259         void GenType::postvisit( TypeInstType * typeInst ) {
    260                 typeString = typeInst->name + " " + typeString;
    261                 handleQualifiers( typeInst );
    262         }
    263 
    264         void GenType::postvisit( TupleType * tupleType ) {
    265                 unsigned int i = 0;
    266                 std::ostringstream os;
    267                 os << "[";
    268                 for ( Type * t : *tupleType ) {
    269                         i++;
    270                         os << genDemangleType( t, "" ) << (i == tupleType->size() ? "" : ", ");
    271                 }
    272                 os << "] ";
    273                 typeString = os.str() + typeString;
    274         }
    275 
    276         void GenType::postvisit( VarArgsType * varArgsType ) {
    277                 typeString = "__builtin_va_list " + typeString;
    278                 handleQualifiers( varArgsType );
    279         }
    280 
    281         void GenType::postvisit( ZeroType * zeroType ) {
    282                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    283                 typeString = "zero_t " + typeString;
    284                 handleQualifiers( zeroType );
    285         }
    286 
    287         void GenType::postvisit( OneType * oneType ) {
    288                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    289                 typeString = "one_t " + typeString;
    290                 handleQualifiers( oneType );
    291         }
    292 
    293         void GenType::postvisit( GlobalScopeType * globalType ) {
    294                 handleQualifiers( globalType );
    295         }
    296 
    297         void GenType::postvisit( QualifiedType * qualType ) {
    298                 std::ostringstream os;
    299                 os << genDemangleType( qualType->parent, "" ) << "." << genDemangleType( qualType->child, "" ) << typeString;
    300                 typeString = os.str();
    301                 handleQualifiers( qualType );
    302         }
    303 
    304         void GenType::handleQualifiers( Type * type ) {
    305                 if ( type->get_const() ) {
    306                         typeString = "const " + typeString;
    307                 } // if
    308                 if ( type->get_volatile() ) {
    309                         typeString = "volatile " + typeString;
    310                 } // if
    311                 if ( type->get_restrict() ) {
    312                         typeString = "__restrict " + typeString;
    313                 } // if
    314                 if ( type->get_atomic() ) {
    315                         typeString = "_Atomic " + typeString;
    316                 } // if
    317         }
    318 }
    319 
    32033
    32134namespace SymTab {
     
    609322                                if (info) name = info->inputName;
    610323                                std::unique_ptr<Type> manager(type);
    611                                 return genDemangleType(type, name);
     324                                return CodeGen::genType(type, name);
    612325                        }
    613326                } // namespace
Note: See TracChangeset for help on using the changeset viewer.