Ignore:
Timestamp:
Aug 27, 2018, 4:40:34 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
b7c89aa
Parents:
f9feab8 (diff), 305581d (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' into cleanup-dtors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/GenType.cc

    rf9feab8 r90152a4  
    2626
    2727namespace CodeGen {
    28         class GenType : public Visitor {
    29           public:
    30                 GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false );
    31                 std::string get_typeString() const { return typeString; }
    32                 void set_typeString( const std::string &newValue ) { typeString = newValue; }
    33 
    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 );
     28        struct GenType : public WithVisitorRef<GenType>, public WithShortCircuiting {
     29                std::string typeString;
     30                GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks );
     31
     32                void previsit( BaseSyntaxNode * );
     33                void postvisit( BaseSyntaxNode * );
     34
     35                void postvisit( FunctionType * funcType );
     36                void postvisit( VoidType * voidType );
     37                void postvisit( BasicType * basicType );
     38                void postvisit( PointerType * pointerType );
     39                void postvisit( ArrayType * arrayType );
     40                void postvisit( ReferenceType * refType );
     41                void postvisit( StructInstType * structInst );
     42                void postvisit( UnionInstType * unionInst );
     43                void postvisit( EnumInstType * enumInst );
     44                void postvisit( TypeInstType * typeInst );
     45                void postvisit( TupleType  * tupleType );
     46                void postvisit( VarArgsType * varArgsType );
     47                void postvisit( ZeroType * zeroType );
     48                void postvisit( OneType * oneType );
     49                void postvisit( GlobalScopeType * globalType );
     50                void postvisit( TraitInstType * inst );
     51                void postvisit( TypeofType * typeof );
     52                void postvisit( QualifiedType * qualType );
    4853
    4954          private:
     
    5257                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
    5358
    54                 std::string typeString;
    55                 bool pretty = false; // pretty print
    56                 bool genC = false;   // generating C code?
    57                 bool lineMarks = false;
     59                bool pretty = false;    // pretty print
     60                bool genC = false;      // generating C code?
     61                bool lineMarks = false; // lineMarks on for CodeGenerator?
    5862        };
    5963
    6064        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) {
    61                 GenType gt( baseString, pretty, genC, lineMarks );
     65                PassVisitor<GenType> gt( baseString, pretty, genC, lineMarks );
    6266                std::ostringstream os;
    6367
     
    6872
    6973                type->accept( gt );
    70                 return os.str() + gt.get_typeString();
     74                return os.str() + gt.pass.typeString;
    7175        }
    7276
     
    7781        GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    7882
    79         void GenType::visit( VoidType *voidType ) {
     83        // *** BaseSyntaxNode
     84        void GenType::previsit( BaseSyntaxNode * ) {
     85                // turn off automatic recursion for all nodes, to allow each visitor to
     86                // precisely control the order in which its children are visited.
     87                visit_children = false;
     88        }
     89
     90        void GenType::postvisit( BaseSyntaxNode * node ) {
     91                std::stringstream ss;
     92                node->print( ss );
     93                assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() );
     94        }
     95
     96        void GenType::postvisit( VoidType * voidType ) {
    8097                typeString = "void " + typeString;
    8198                handleQualifiers( voidType );
    8299        }
    83100
    84         void GenType::visit( BasicType *basicType ) {
    85                 BasicType::Kind kind = basicType->get_kind();
     101        void GenType::postvisit( BasicType * basicType ) {
     102                BasicType::Kind kind = basicType->kind;
    86103                assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES );
    87104                typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString;
     
    89106        }
    90107
    91         void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) {
     108        void GenType::genArray( const Type::Qualifiers & qualifiers, Type * base, Expression *dimension, bool isVarLen, bool isStatic ) {
    92109                std::ostringstream os;
    93110                if ( typeString != "" ) {
     
    126143                typeString = os.str();
    127144
    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() );
     145                base->accept( *visitor );
     146        }
     147
     148        void GenType::postvisit( PointerType * pointerType ) {
     149                assert( pointerType->base != 0);
     150                if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->dimension ) {
     151                        genArray( pointerType->get_qualifiers(), pointerType->base, pointerType->dimension, pointerType->get_isVarLen(), pointerType->get_isStatic() );
    135152                } else {
    136153                        handleQualifiers( pointerType );
     
    140157                                typeString = "*" + typeString;
    141158                        } // 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);
     159                        pointerType->base->accept( *visitor );
     160                } // if
     161        }
     162
     163        void GenType::postvisit( ArrayType * arrayType ) {
     164                genArray( arrayType->get_qualifiers(), arrayType->base, arrayType->dimension, arrayType->get_isVarLen(), arrayType->get_isStatic() );
     165        }
     166
     167        void GenType::postvisit( ReferenceType * refType ) {
     168                assert( refType->base != 0);
    152169                assertf( ! genC, "Reference types should not reach code generation." );
    153170                handleQualifiers( refType );
    154171                typeString = "&" + typeString;
    155                 refType->get_base()->accept( *this );
    156         }
    157 
    158         void GenType::visit( FunctionType *funcType ) {
     172                refType->base->accept( *visitor );
     173        }
     174
     175        void GenType::postvisit( FunctionType * funcType ) {
    159176                std::ostringstream os;
    160177
     
    169186                /************* parameters ***************/
    170187
    171                 const std::list<DeclarationWithType *> &pars = funcType->get_parameters();
     188                const std::list<DeclarationWithType *> &pars = funcType->parameters;
    172189
    173190                if ( pars.empty() ) {
     
    191208                typeString = os.str();
    192209
    193                 if ( funcType->get_returnVals().size() == 0 ) {
     210                if ( funcType->returnVals.size() == 0 ) {
    194211                        typeString = "void " + typeString;
    195212                } else {
    196                         funcType->get_returnVals().front()->get_type()->accept( *this );
     213                        funcType->returnVals.front()->get_type()->accept( *visitor );
    197214                } // if
    198215
    199216                // add forall
    200                 if( ! funcType->get_forall().empty() && ! genC ) {
     217                if( ! funcType->forall.empty() && ! genC ) {
    201218                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
    202219                        std::ostringstream os;
    203220                        PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
    204221                        os << "forall(";
    205                         cg.pass.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() );
     222                        cg.pass.genCommaList( funcType->forall.begin(), funcType->forall.end() );
    206223                        os << ")" << std::endl;
    207224                        typeString = os.str() + typeString;
     
    221238        }
    222239
    223         void GenType::visit( StructInstType *structInst )  {
    224                 typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
     240        void GenType::postvisit( StructInstType * structInst )  {
     241                typeString = structInst->name + handleGeneric( structInst ) + " " + typeString;
    225242                if ( genC ) typeString = "struct " + typeString;
    226243                handleQualifiers( structInst );
    227244        }
    228245
    229         void GenType::visit( UnionInstType *unionInst ) {
    230                 typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
     246        void GenType::postvisit( UnionInstType * unionInst ) {
     247                typeString = unionInst->name + handleGeneric( unionInst ) + " " + typeString;
    231248                if ( genC ) typeString = "union " + typeString;
    232249                handleQualifiers( unionInst );
    233250        }
    234251
    235         void GenType::visit( EnumInstType *enumInst ) {
    236                 typeString = enumInst->get_name() + " " + typeString;
     252        void GenType::postvisit( EnumInstType * enumInst ) {
     253                typeString = enumInst->name + " " + typeString;
    237254                if ( genC ) typeString = "enum " + typeString;
    238255                handleQualifiers( enumInst );
    239256        }
    240257
    241         void GenType::visit( TypeInstType *typeInst ) {
    242                 typeString = typeInst->get_name() + " " + typeString;
     258        void GenType::postvisit( TypeInstType * typeInst ) {
     259                typeString = typeInst->name + " " + typeString;
    243260                handleQualifiers( typeInst );
    244261        }
    245262
    246         void GenType::visit( TupleType * tupleType ) {
     263        void GenType::postvisit( TupleType * tupleType ) {
    247264                assertf( ! genC, "Tuple types should not reach code generation." );
    248265                unsigned int i = 0;
     
    257274        }
    258275
    259         void GenType::visit( VarArgsType *varArgsType ) {
     276        void GenType::postvisit( VarArgsType * varArgsType ) {
    260277                typeString = "__builtin_va_list " + typeString;
    261278                handleQualifiers( varArgsType );
    262279        }
    263280
    264         void GenType::visit( ZeroType *zeroType ) {
     281        void GenType::postvisit( ZeroType * zeroType ) {
    265282                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    266283                typeString = (pretty ? "zero_t " : "long int ") + typeString;
     
    268285        }
    269286
    270         void GenType::visit( OneType *oneType ) {
     287        void GenType::postvisit( OneType * oneType ) {
    271288                // ideally these wouldn't hit codegen at all, but should be safe to make them ints
    272289                typeString = (pretty ? "one_t " : "long int ") + typeString;
     
    274291        }
    275292
    276         void GenType::handleQualifiers( Type *type ) {
     293        void GenType::postvisit( GlobalScopeType * globalType ) {
     294                assertf( ! genC, "Global scope type should not reach code generation." );
     295                handleQualifiers( globalType );
     296        }
     297
     298        void GenType::postvisit( TraitInstType * inst ) {
     299                assertf( ! genC, "Trait types should not reach code generation." );
     300                typeString = inst->name + " " + typeString;
     301                handleQualifiers( inst );
     302        }
     303
     304        void GenType::postvisit( TypeofType * typeof ) {
     305                std::ostringstream os;
     306                PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
     307                os << "typeof(";
     308                typeof->expr->accept( cg );
     309                os << ") " << typeString;
     310                typeString = os.str();
     311                handleQualifiers( typeof );
     312        }
     313
     314        void GenType::postvisit( QualifiedType * qualType ) {
     315                assertf( ! genC, "Qualified types should not reach code generation." );
     316                std::ostringstream os;
     317                os << genType( qualType->parent, "", pretty, genC, lineMarks ) << "." << genType( qualType->child, "", pretty, genC, lineMarks ) << typeString;
     318                typeString = os.str();
     319                handleQualifiers( qualType );
     320        }
     321
     322        void GenType::handleQualifiers( Type * type ) {
    277323                if ( type->get_const() ) {
    278324                        typeString = "const " + typeString;
Note: See TracChangeset for help on using the changeset viewer.