Ignore:
Timestamp:
Nov 8, 2023, 2:01:11 PM (12 months ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
master
Children:
3e4bf0d, f5ec35a
Parents:
790d835
Message:

Remove BaseSyntaxNode? and clean-up.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/GenType.cc

    r790d835 rc6b4432  
    2121#include "AST/Print.hpp"          // for print
    2222#include "AST/Vector.hpp"         // for vector
    23 #include "CodeGenerator.h"        // for CodeGenerator
    2423#include "CodeGeneratorNew.hpp"   // for CodeGenerator_new
    2524#include "Common/UniqueName.h"    // for UniqueName
    26 #include "SynTree/Declaration.h"  // for DeclarationWithType
    27 #include "SynTree/Expression.h"   // for Expression
    28 #include "SynTree/Type.h"         // for PointerType, Type, FunctionType
    29 #include "SynTree/Visitor.h"      // for Visitor
    3025
    3126namespace CodeGen {
    32         struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
    33                 std::string typeString;
    34                 GenType( const std::string &typeString, const Options &options );
    35 
    36                 void previsit( BaseSyntaxNode * );
    37                 void postvisit( BaseSyntaxNode * );
    38 
    39                 void postvisit( FunctionType * funcType );
    40                 void postvisit( VoidType * voidType );
    41                 void postvisit( BasicType * basicType );
    42                 void postvisit( PointerType * pointerType );
    43                 void postvisit( ArrayType * arrayType );
    44                 void postvisit( ReferenceType * refType );
    45                 void postvisit( StructInstType * structInst );
    46                 void postvisit( UnionInstType * unionInst );
    47                 void postvisit( EnumInstType * enumInst );
    48                 void postvisit( TypeInstType * typeInst );
    49                 void postvisit( TupleType  * tupleType );
    50                 void postvisit( VarArgsType * varArgsType );
    51                 void postvisit( ZeroType * zeroType );
    52                 void postvisit( OneType * oneType );
    53                 void postvisit( GlobalScopeType * globalType );
    54                 void postvisit( TraitInstType * inst );
    55                 void postvisit( TypeofType * typeof );
    56                 void postvisit( VTableType * vtable );
    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                 Options options;
    65         };
    66 
    67         std::string genType( Type *type, const std::string &baseString, const Options &options ) {
    68                 PassVisitor<GenType> gt( baseString, options );
    69                 std::ostringstream os;
    70 
    71                 if ( ! type->get_attributes().empty() ) {
    72                         PassVisitor<CodeGenerator> cg( os, options );
    73                         cg.pass.genAttributes( type->get_attributes() );
    74                 } // if
    75 
    76                 type->accept( gt );
    77                 return os.str() + gt.pass.typeString;
    78         }
    79 
    80         std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
    81                 return genType( type, baseString, Options(pretty, genC, lineMarks, false ) );
    82         }
    83 
    84         std::string genPrettyType( Type * type, const std::string & baseString ) {
    85                 return genType( type, baseString, true, false );
    86         }
    87 
    88         GenType::GenType( const std::string &typeString, const Options &options ) : typeString( typeString ), options( options ) {}
    89 
    90         // *** BaseSyntaxNode
    91         void GenType::previsit( BaseSyntaxNode * ) {
    92                 // turn off automatic recursion for all nodes, to allow each visitor to
    93                 // precisely control the order in which its children are visited.
    94                 visit_children = false;
    95         }
    96 
    97         void GenType::postvisit( BaseSyntaxNode * node ) {
    98                 std::stringstream ss;
    99                 node->print( ss );
    100                 assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
    101         }
    102 
    103         void GenType::postvisit( VoidType * voidType ) {
    104                 typeString = "void " + typeString;
    105                 handleQualifiers( voidType );
    106         }
    107 
    108         void GenType::postvisit( BasicType * basicType ) {
    109                 BasicType::Kind kind = basicType->kind;
    110                 assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    111                 typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
    112                 handleQualifiers( basicType );
    113         }
    114 
    115         void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
    116                 std::ostringstream os;
    117                 if ( typeString != "" ) {
    118                         if ( typeString[ 0 ] == '*' ) {
    119                                 os << "(" << typeString << ")";
    120                         } else {
    121                                 os << typeString;
    122                         } // if
    123                 } // if
    124                 os << "[";
    125 
    126                 if ( isStatic ) {
    127                         os << "static ";
    128                 } // if
    129                 if ( qualifiers.is_const ) {
    130                         os << "const ";
    131                 } // if
    132                 if ( qualifiers.is_volatile ) {
    133                         os << "volatile ";
    134                 } // if
    135                 if ( qualifiers.is_restrict ) {
    136                         os << "__restrict ";
    137                 } // if
    138                 if ( qualifiers.is_atomic ) {
    139                         os << "_Atomic ";
    140                 } // if
    141                 if ( dimension != 0 ) {
    142                         PassVisitor<CodeGenerator> cg( os, options );
    143                         dimension->accept( cg );
    144                 } else if ( isVarLen ) {
    145                         // no dimension expression on a VLA means it came in with the * token
    146                         os << "*";
    147                 } // if
    148                 os << "]";
    149 
    150                 typeString = os.str();
    151 
    152                 base->accept( *visitor );
    153         }
    154 
    155         void GenType::postvisit( PointerType * pointerType ) {
    156                 assert( pointerType->base != 0);
    157                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
    158                         genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
    159                 } else {
    160                         handleQualifiers( pointerType );
    161                         if ( typeString[ 0 ] == '?' ) {
    162                                 typeString = "* " + typeString;
    163                         } else {
    164                                 typeString = "*" + typeString;
    165                         } // if
    166                         pointerType->base->accept( *visitor );
    167                 } // if
    168         }
    169 
    170         void GenType::postvisit( ArrayType * arrayType ) {
    171                 genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
    172         }
    173 
    174         void GenType::postvisit( ReferenceType * refType ) {
    175                 assert( 0 != refType->base );
    176                 assertf( ! options.genC, "Reference types should not reach code generation." );
    177                 handleQualifiers( refType );
    178                 typeString = "&" + typeString;
    179                 refType->base->accept( *visitor );
    180         }
    181 
    182         void GenType::postvisit( FunctionType * funcType ) {
    183                 std::ostringstream os;
    184 
    185                 if ( typeString != "" ) {
    186                         if ( typeString[ 0 ] == '*' ) {
    187                                 os << "(" << typeString << ")";
    188                         } else {
    189                                 os << typeString;
    190                         } // if
    191                 } // if
    192 
    193                 /************* parameters ***************/
    194 
    195                 const std::list<DeclarationWithType *> &pars = funcType->parameters;
    196 
    197                 if ( pars.empty() ) {
    198                         if ( funcType->get_isVarArgs() ) {
    199                                 os << "()";
    200                         } else {
    201                                 os << "(void)";
    202                         } // if
    203                 } else {
    204                         PassVisitor<CodeGenerator> cg( os, options );
    205                         os << "(" ;
    206 
    207                         cg.pass.genCommaList( pars.begin(), pars.end() );
    208 
    209                         if ( funcType->get_isVarArgs() ) {
    210                                 os << ", ...";
    211                         } // if
    212                         os << ")";
    213                 } // if
    214 
    215                 typeString = os.str();
    216 
    217                 if ( funcType->returnVals.size() == 0 ) {
    218                         typeString = "void " + typeString;
    219                 } else {
    220                         funcType->returnVals.front()->get_type()->accept( *visitor );
    221                 } // if
    222 
    223                 // add forall
    224                 if( ! funcType->forall.empty() && ! options.genC ) {
    225                         // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
    226                         std::ostringstream os;
    227                         PassVisitor<CodeGenerator> cg( os, options );
    228                         os << "forall(";
    229                         cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
    230                         os << ")" << std::endl;
    231                         typeString = os.str() + typeString;
    232                 }
    233         }
    234 
    235         std::string GenType::handleGeneric( ReferenceToType * refType ) {
    236                 if ( ! refType->parameters.empty() ) {
    237                         std::ostringstream os;
    238                         PassVisitor<CodeGenerator> cg( os, options );
    239                         os << "(";
    240                         cg.pass.genCommaList( refType->parameters.begin(), refType->parameters.end() );
    241                         os << ") ";
    242                         return os.str();
    243                 }
    244                 return "";
    245         }
    246 
    247         void GenType::postvisit( StructInstType * structInst )  {
    248                 typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
    249                 if ( options.genC ) typeString = "struct " + typeString;
    250                 handleQualifiers( structInst );
    251         }
    252 
    253         void GenType::postvisit( UnionInstType * unionInst ) {
    254                 typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
    255                 if ( options.genC ) typeString = "union " + typeString;
    256                 handleQualifiers( unionInst );
    257         }
    258 
    259         void GenType::postvisit( EnumInstType * enumInst ) {
    260                 if ( enumInst->baseEnum && enumInst->baseEnum->base ) {
    261                         typeString = genType(enumInst->baseEnum->base, typeString, options);
    262                 } else {
    263                         typeString = enumInst->name + " " + typeString;
    264                         if ( options.genC ) {
    265                                 typeString = "enum " + typeString;
    266                         }
    267                 }
    268                 handleQualifiers( enumInst );
    269         }
    270 
    271         void GenType::postvisit( TypeInstType * typeInst ) {
    272                 assertf( ! options.genC, "Type instance types should not reach code generation." );
    273                 typeString = typeInst->name + " " + typeString;
    274                 handleQualifiers( typeInst );
    275         }
    276 
    277         void GenType::postvisit( TupleType * tupleType ) {
    278                 assertf( ! options.genC, "Tuple types should not reach code generation." );
    279                 unsigned int i = 0;
    280                 std::ostringstream os;
    281                 os << "[";
    282                 for ( Type * t : *tupleType ) {
    283                         i++;
    284                         os << genType( t, "", options ) << (i == tupleType->size() ? "" : ", ");
    285                 }
    286                 os << "] ";
    287                 typeString = os.str() + typeString;
    288         }
    289 
    290         void GenType::postvisit( VarArgsType * varArgsType ) {
    291                 typeString = "__builtin_va_list " + typeString;
    292                 handleQualifiers( varArgsType );
    293         }
    294 
    295         void GenType::postvisit( ZeroType * zeroType ) {
    296                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    297                 typeString = (options.pretty ? "zero_t " : "long int ") + typeString;
    298                 handleQualifiers( zeroType );
    299         }
    300 
    301         void GenType::postvisit( OneType * oneType ) {
    302                 // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    303                 typeString = (options.pretty ? "one_t " : "long int ") + typeString;
    304                 handleQualifiers( oneType );
    305         }
    306 
    307         void GenType::postvisit( GlobalScopeType * globalType ) {
    308                 assertf( ! options.genC, "Global scope type should not reach code generation." );
    309                 handleQualifiers( globalType );
    310         }
    311 
    312         void GenType::postvisit( TraitInstType * inst ) {
    313                 assertf( ! options.genC, "Trait types should not reach code generation." );
    314                 typeString = inst->name + " " + typeString;
    315                 handleQualifiers( inst );
    316         }
    317 
    318         void GenType::postvisit( TypeofType * typeof ) {
    319                 std::ostringstream os;
    320                 PassVisitor<CodeGenerator> cg( os, options );
    321                 os << "typeof(";
    322                 typeof->expr->accept( cg );
    323                 os << ") " << typeString;
    324                 typeString = os.str();
    325                 handleQualifiers( typeof );
    326         }
    327 
    328         void GenType::postvisit( VTableType * vtable ) {
    329                 assertf( ! options.genC, "Virtual table types should not reach code generation." );
    330                 std::ostringstream os;
    331                 os << "vtable(" << genType( vtable->base, "", options ) << ") " << typeString;
    332                 typeString = os.str();
    333                 handleQualifiers( vtable );
    334         }
    335 
    336         void GenType::postvisit( QualifiedType * qualType ) {
    337                 assertf( ! options.genC, "Qualified types should not reach code generation." );
    338                 std::ostringstream os;
    339                 os << genType( qualType->parent, "", options ) << "." << genType( qualType->child, "", options ) << typeString;
    340                 typeString = os.str();
    341                 handleQualifiers( qualType );
    342         }
    343 
    344         void GenType::handleQualifiers( Type * type ) {
    345                 if ( type->get_const() ) {
    346                         typeString = "const " + typeString;
    347                 } // if
    348                 if ( type->get_volatile() ) {
    349                         typeString = "volatile " + typeString;
    350                 } // if
    351                 if ( type->get_restrict() ) {
    352                         typeString = "__restrict " + typeString;
    353                 } // if
    354                 if ( type->get_atomic() ) {
    355                         typeString = "_Atomic " + typeString;
    356                 } // if
    357         }
    35827
    35928namespace {
Note: See TracChangeset for help on using the changeset viewer.