Changeset 7dc0246d for src


Ignore:
Timestamp:
Jan 15, 2018, 1:32:38 PM (6 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
c0b9f5d
Parents:
bee7f04
Message:

Convert GenType? to PassVisitor?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/GenType.cc

    rbee7f04 r7dc0246d  
    2626
    2727namespace CodeGen {
    28         class GenType : public Visitor {
    29           public:
     28        struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
    3029                GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false );
    3130                std::string get_typeString() const { return typeString; }
    3231                void set_typeString( const std::string &newValue ) { typeString = newValue; }
    3332
    34                 virtual void visit( FunctionType *funcType );
    35                 virtual void visit( VoidType *voidType );
    36                 virtual void visit( BasicType *basicType );
    37                 virtual void visit( PointerType *pointerType );
    38                 virtual void visit( ArrayType *arrayType );
    39                 virtual void visit( ReferenceType *refType );
    40                 virtual void visit( StructInstType *structInst );
    41                 virtual void visit( UnionInstType *unionInst );
    42                 virtual void visit( EnumInstType *enumInst );
    43                 virtual void visit( TypeInstType *typeInst );
    44                 virtual void visit( TupleType * tupleType );
    45                 virtual void visit( VarArgsType *varArgsType );
    46                 virtual void visit( ZeroType *zeroType );
    47                 virtual void visit( OneType *oneType );
     33                void previsit( BaseSyntaxNode * );
     34                void postvisit( BaseSyntaxNode * );
     35
     36                void postvisit( FunctionType * funcType );
     37                void postvisit( VoidType * voidType );
     38                void postvisit( BasicType * basicType );
     39                void postvisit( PointerType * pointerType );
     40                void postvisit( ArrayType * arrayType );
     41                void postvisit( ReferenceType * refType );
     42                void postvisit( StructInstType * structInst );
     43                void postvisit( UnionInstType * unionInst );
     44                void postvisit( EnumInstType * enumInst );
     45                void postvisit( TypeInstType * typeInst );
     46                void postvisit( TupleType  * tupleType );
     47                void postvisit( VarArgsType * varArgsType );
     48                void postvisit( ZeroType * zeroType );
     49                void postvisit( OneType * oneType );
    4850
    4951          private:
     
    5961
    6062        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
    61                 GenType gt( baseString, pretty, genC, lineMarks );
     63                PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks );
    6264                std::ostringstream os;
    6365
     
    6870
    6971                type->accept( gt );
    70                 return os.str() + gt.get_typeString();
     72                return os.str() + gt.pass.get_typeString();
    7173        }
    7274
     
    7779        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    7880
    79         void GenType::visit( VoidType *voidType ) {
     81        // *** BaseSyntaxNode
     82        void GenType::previsit( BaseSyntaxNode * ) {
     83                // turn off automatic recursion for all nodes, to allow each visitor to
     84                // precisely control the order in which its children are visited.
     85                visit_children = false;
     86        }
     87
     88        void GenType::postvisit( BaseSyntaxNode * node ) {
     89                std::stringstream ss;
     90                node->print( ss );
     91                assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
     92        }
     93
     94        void GenType::postvisit( VoidType * voidType ) {
    8095                typeString = "void " + typeString;
    8196                handleQualifiers( voidType );
    8297        }
    8398
    84         void GenType::visit( BasicType *basicType ) {
    85                 BasicType::Kind kind = basicType->get_kind();
     99        void GenType::postvisit( BasicType * basicType ) {
     100                BasicType::Kind kind = basicType->kind;
    86101                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    87102                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
     
    89104        }
    90105
    91         void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
     106        void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
    92107                std::ostringstream os;
    93108                if ( typeString != "" ) {
     
    126141                typeString = os.str();
    127142
    128                 base->accept( *this );
    129         }
    130 
    131         void GenType::visit( PointerType *pointerType ) {
    132                 assert( pointerType->get_base() != 0);
    133                 if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) {
    134                         genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() );
     143                base->accept( *visitor );
     144        }
     145
     146        void GenType::postvisit( PointerType * pointerType ) {
     147                assert( pointerType->base != 0);
     148                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
     149                        genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
    135150                } else {
    136151                        handleQualifiers( pointerType );
     
    140155                                typeString = "*" + typeString;
    141156                        } // if
    142                         pointerType->get_base()->accept( *this );
    143                 } // if
    144         }
    145 
    146         void GenType::visit( ArrayType *arrayType ) {
    147                 genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() );
    148         }
    149 
    150         void GenType::visit( ReferenceType *refType ) {
    151                 assert( refType->get_base() != 0);
     157                        pointerType->base->accept( *visitor );
     158                } // if
     159        }
     160
     161        void GenType::postvisit( ArrayType * arrayType ) {
     162                genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
     163        }
     164
     165        void GenType::postvisit( ReferenceType * refType ) {
     166                assert( refType->base != 0);
    152167                assertf( ! genC, "Reference types should not reach code generation." );
    153168                handleQualifiers( refType );
    154169                typeString = "&" + typeString;
    155                 refType->get_base()->accept( *this );
    156         }
    157 
    158         void GenType::visit( FunctionType *funcType ) {
     170                refType->base->accept( *visitor );
     171        }
     172
     173        void GenType::postvisit( FunctionType * funcType ) {
    159174                std::ostringstream os;
    160175
     
    169184                /************* parameters ***************/
    170185
    171                 const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
     186                const std::list<DeclarationWithType *> &pars = funcType->parameters;
    172187
    173188                if ( pars.empty() ) {
     
    191206                typeString = os.str();
    192207
    193                 if ( funcType->get_returnVals().size() == 0 ) {
     208                if ( funcType->returnVals.size() == 0 ) {
    194209                        typeString = "void " + typeString;
    195210                } else {
    196                         funcType->get_returnVals().front()->get_type()->accept( *this );
     211                        funcType->returnVals.front()->get_type()->accept( *visitor );
    197212                } // if
    198213
    199214                // add forall
    200                 if( ! funcType->get_forall().empty() && ! genC ) {
     215                if( ! funcType->forall.empty() && ! genC ) {
    201216                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
    202217                        std::ostringstream os;
    203218                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    204219                        os << "forall(";
    205                         cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
     220                        cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
    206221                        os << ")" << std::endl;
    207222                        typeString = os.str() + typeString;
     
    221236        }
    222237
    223         void GenType::visit( StructInstType *structInst )  {
    224                 typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
     238        void GenType::postvisit( StructInstType * structInst )  {
     239                typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
    225240                if ( genC ) typeString = "struct " + typeString;
    226241                handleQualifiers( structInst );
    227242        }
    228243
    229         void GenType::visit( UnionInstType *unionInst ) {
    230                 typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
     244        void GenType::postvisit( UnionInstType * unionInst ) {
     245                typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
    231246                if ( genC ) typeString = "union " + typeString;
    232247                handleQualifiers( unionInst );
    233248        }
    234249
    235         void GenType::visit( EnumInstType *enumInst ) {
    236                 typeString = enumInst->get_name() + " " + typeString;
     250        void GenType::postvisit( EnumInstType * enumInst ) {
     251                typeString = enumInst->name + " " + typeString;
    237252                if ( genC ) typeString = "enum " + typeString;
    238253                handleQualifiers( enumInst );
    239254        }
    240255
    241         void GenType::visit( TypeInstType *typeInst ) {
    242                 typeString = typeInst->get_name() + " " + typeString;
     256        void GenType::postvisit( TypeInstType * typeInst ) {
     257                typeString = typeInst->name + " " + typeString;
    243258                handleQualifiers( typeInst );
    244259        }
    245260
    246         void GenType::visit( TupleType * tupleType ) {
     261        void GenType::postvisit( TupleType * tupleType ) {
    247262                assertf( ! genC, "Tuple types should not reach code generation." );
    248263                unsigned int i = 0;
     
    257272        }
    258273
    259         void GenType::visit( VarArgsType *varArgsType ) {
     274        void GenType::postvisit( VarArgsType * varArgsType ) {
    260275                typeString = "__builtin_va_list " + typeString;
    261276                handleQualifiers( varArgsType );
    262277        }
    263278
    264         void GenType::visit( ZeroType *zeroType ) {
     279        void GenType::postvisit( ZeroType * zeroType ) {
    265280                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    266281                typeString = (pretty ? "zero_t " : "long int ") + typeString;
     
    268283        }
    269284
    270         void GenType::visit( OneType *oneType ) {
     285        void GenType::postvisit( OneType * oneType ) {
    271286                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    272287                typeString = (pretty ? "one_t " : "long int ") + typeString;
     
    274289        }
    275290
    276         void GenType::handleQualifiers( Type *type ) {
     291        void GenType::handleQualifiers( Type * type ) {
    277292                if ( type->get_const() ) {
    278293                        typeString = "const " + typeString;
Note: See TracChangeset for help on using the changeset viewer.