Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/GenType.cc

    r615a096 re39241b  
    2828        class GenType : public Visitor {
    2929          public:
    30                 GenType( const std::string &typeString, bool pretty = false );
     30                GenType( const std::string &typeString, bool pretty = false, bool genC = false );
    3131                std::string get_typeString() const { return typeString; }
    3232                void set_typeString( const std::string &newValue ) { typeString = newValue; }
     
    4848          private:
    4949                void handleQualifiers( Type *type );
     50                std::string handleGeneric( ReferenceToType * refType );
    5051                void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic );
    5152
    5253                std::string typeString;
    5354                bool pretty = false; // pretty print
     55                bool genC = false;   // generating C code?
    5456        };
    5557
    56         std::string genType( Type *type, const std::string &baseString, bool pretty ) {
    57                 GenType gt( baseString, pretty );
     58        std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC ) {
     59                GenType gt( baseString, pretty, genC );
    5860                std::ostringstream os;
    5961
    6062                if ( ! type->get_attributes().empty() ) {
    61                         CodeGenerator cg( os, pretty );
     63                        CodeGenerator cg( os, pretty, genC );
    6264                        cg.genAttributes( type->get_attributes() );
    6365                } // if
     
    6870
    6971  std::string genPrettyType( Type * type, const std::string & baseString ) {
    70         return genType( type, baseString, true );
     72        return genType( type, baseString, true, false );
    7173  }
    7274
    73         GenType::GenType( const std::string &typeString, bool pretty ) : typeString( typeString ), pretty( pretty ) {}
     75        GenType::GenType( const std::string &typeString, bool pretty, bool genC ) : typeString( typeString ), pretty( pretty ), genC( genC ) {}
    7476
    7577        void GenType::visit( VoidType *voidType ) {
     
    112114                } // if
    113115                if ( dimension != 0 ) {
    114                         CodeGenerator cg( os, pretty );
     116                        CodeGenerator cg( os, pretty, genC );
    115117                        dimension->accept( cg );
    116118                } else if ( isVarLen ) {
     
    166168                        } // if
    167169                } else {
    168                         CodeGenerator cg( os, pretty );
     170                        CodeGenerator cg( os, pretty, genC );
    169171                        os << "(" ;
    170172
     
    186188        }
    187189
     190        std::string GenType::handleGeneric( ReferenceToType * refType ) {
     191                if ( ! refType->get_parameters().empty() ) {
     192                        std::ostringstream os;
     193                        CodeGenerator cg( os, pretty, genC );
     194                        os << "(";
     195                        cg.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() );
     196                        os << ") ";
     197                        return os.str();
     198                }
     199                return "";
     200        }
     201
    188202        void GenType::visit( StructInstType *structInst )  {
    189                 typeString = "struct " + structInst->get_name() + " " + typeString;
     203                typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString;
     204                if ( genC ) typeString = "struct " + typeString;
    190205                handleQualifiers( structInst );
    191206        }
    192207
    193208        void GenType::visit( UnionInstType *unionInst ) {
    194                 typeString = "union " + unionInst->get_name() + " " + typeString;
     209                typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString;
     210                if ( genC ) typeString = "union " + typeString;
    195211                handleQualifiers( unionInst );
    196212        }
    197213
    198214        void GenType::visit( EnumInstType *enumInst ) {
    199                 typeString = "enum " + enumInst->get_name() + " " + typeString;
     215                typeString = enumInst->get_name() + " " + typeString;
     216                if ( genC ) typeString = "enum " + typeString;
    200217                handleQualifiers( enumInst );
    201218        }
     
    207224
    208225        void GenType::visit( TupleType * tupleType ) {
    209                 assertf( pretty, "Tuple types should not make it to Code Gen." );
     226                assertf( ! genC, "Tuple types should not reach code generation." );
    210227                Visitor::visit( tupleType );
    211228                unsigned int i = 0;
     
    214231                for ( Type * t : *tupleType ) {
    215232                        i++;
    216                         os << genType( t, "", pretty ) << (i == tupleType->size() ? "" : ", ");
     233                        os << genType( t, "", pretty, genC ) << (i == tupleType->size() ? "" : ", ");
    217234                }
    218235                os << "]";
Note: See TracChangeset for help on using the changeset viewer.