[51b7345] | 1 | #include <strstream> |
---|
| 2 | #include <cassert> |
---|
| 3 | |
---|
| 4 | #include "GenType.h" |
---|
| 5 | #include "CodeGenerator2.h" |
---|
| 6 | #include "SynTree/Visitor.h" |
---|
| 7 | #include "SynTree/Type.h" |
---|
| 8 | #include "SynTree/Expression.h" |
---|
| 9 | |
---|
| 10 | namespace CodeGen { |
---|
[17cd4eb] | 11 | class GenType : public Visitor { |
---|
| 12 | public: |
---|
| 13 | GenType( const std::string &typeString ); |
---|
| 14 | std::string get_typeString() const { return typeString; } |
---|
| 15 | void set_typeString( const std::string &newValue ) { typeString = newValue; } |
---|
[51b7345] | 16 | |
---|
[17cd4eb] | 17 | virtual void visit( FunctionType *funcType ); |
---|
| 18 | virtual void visit( VoidType *voidType ); |
---|
| 19 | virtual void visit( BasicType *basicType ); |
---|
| 20 | virtual void visit( PointerType *pointerType ); |
---|
| 21 | virtual void visit( ArrayType *arrayType ); |
---|
| 22 | virtual void visit( StructInstType *structInst ); |
---|
| 23 | virtual void visit( UnionInstType *unionInst ); |
---|
| 24 | virtual void visit( EnumInstType *enumInst ); |
---|
| 25 | virtual void visit( TypeInstType *typeInst ); |
---|
[51b7345] | 26 | |
---|
[17cd4eb] | 27 | private: |
---|
| 28 | void handleQualifiers( Type *type ); |
---|
| 29 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
[51b7345] | 30 | |
---|
[17cd4eb] | 31 | std::string typeString; |
---|
| 32 | }; |
---|
[51b7345] | 33 | |
---|
[17cd4eb] | 34 | std::string genType( Type *type, const std::string &baseString ) { |
---|
| 35 | GenType gt( baseString ); |
---|
| 36 | type->accept( gt ); |
---|
| 37 | return gt.get_typeString(); |
---|
| 38 | } |
---|
[51b7345] | 39 | |
---|
[17cd4eb] | 40 | GenType::GenType( const std::string &typeString ) : typeString( typeString ) {} |
---|
[51b7345] | 41 | |
---|
[17cd4eb] | 42 | void GenType::visit( VoidType *voidType ) { |
---|
| 43 | typeString = "void " + typeString; |
---|
| 44 | handleQualifiers( voidType ); |
---|
[51b7345] | 45 | } |
---|
| 46 | |
---|
[17cd4eb] | 47 | void GenType::visit( BasicType *basicType ) { |
---|
| 48 | BasicType::Kind kind = basicType->get_kind(); |
---|
| 49 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
| 50 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
| 51 | handleQualifiers( basicType ); |
---|
[51b7345] | 52 | } |
---|
| 53 | |
---|
[17cd4eb] | 54 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
| 55 | std::ostrstream os; |
---|
| 56 | if ( typeString != "" ) { |
---|
| 57 | if ( typeString[ 0 ] == '*' ) { |
---|
| 58 | os << "(" << typeString << ")"; |
---|
| 59 | } else { |
---|
| 60 | os << typeString; |
---|
| 61 | } // if |
---|
| 62 | } // if |
---|
| 63 | os << "["; |
---|
| 64 | |
---|
| 65 | if ( isStatic ) { |
---|
| 66 | os << "static "; |
---|
| 67 | } // if |
---|
| 68 | if ( qualifiers.isConst ) { |
---|
| 69 | os << "const "; |
---|
| 70 | } // if |
---|
| 71 | if ( qualifiers.isVolatile ) { |
---|
| 72 | os << "volatile "; |
---|
| 73 | } // if |
---|
| 74 | if ( qualifiers.isRestrict ) { |
---|
| 75 | os << "__restrict "; |
---|
| 76 | } // if |
---|
[bdd516a] | 77 | if ( qualifiers.isAtomic ) { |
---|
| 78 | os << "_Atomic "; |
---|
| 79 | } // if |
---|
[17cd4eb] | 80 | if ( isVarLen ) { |
---|
| 81 | os << "*"; |
---|
| 82 | } // if |
---|
| 83 | if ( dimension != 0 ) { |
---|
| 84 | CodeGenerator2 cg( os ); |
---|
| 85 | dimension->accept( cg ); |
---|
| 86 | } // if |
---|
| 87 | os << "]"; |
---|
| 88 | |
---|
| 89 | typeString = std::string( os.str(), os.pcount() ); |
---|
[51b7345] | 90 | |
---|
[17cd4eb] | 91 | base->accept( *this ); |
---|
[51b7345] | 92 | } |
---|
| 93 | |
---|
[17cd4eb] | 94 | void GenType::visit( PointerType *pointerType ) { |
---|
| 95 | assert( pointerType->get_base() != 0); |
---|
| 96 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { |
---|
| 97 | genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); |
---|
| 98 | } else { |
---|
| 99 | handleQualifiers( pointerType ); |
---|
| 100 | if ( typeString[ 0 ] == '?' ) { |
---|
| 101 | typeString = "* " + typeString; |
---|
| 102 | } else { |
---|
| 103 | typeString = "*" + typeString; |
---|
| 104 | } // if |
---|
| 105 | pointerType->get_base()->accept( *this ); |
---|
| 106 | } // if |
---|
[51b7345] | 107 | } |
---|
| 108 | |
---|
[17cd4eb] | 109 | void GenType::visit( ArrayType *arrayType ){ |
---|
| 110 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
| 111 | } |
---|
[51b7345] | 112 | |
---|
[17cd4eb] | 113 | void GenType::visit( FunctionType *funcType ) { |
---|
| 114 | std::ostrstream os; |
---|
[51b7345] | 115 | |
---|
[17cd4eb] | 116 | if ( typeString != "" ) { |
---|
| 117 | if ( typeString[ 0 ] == '*' ) { |
---|
| 118 | os << "(" << typeString << ")"; |
---|
| 119 | } else { |
---|
| 120 | os << typeString; |
---|
| 121 | } // if |
---|
| 122 | } // if |
---|
[51b7345] | 123 | |
---|
[17cd4eb] | 124 | /************* parameters ***************/ |
---|
| 125 | |
---|
| 126 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); |
---|
| 127 | |
---|
| 128 | if ( pars.empty() ) { |
---|
| 129 | if ( funcType->get_isVarArgs() ) { |
---|
| 130 | os << "()"; |
---|
| 131 | } else { |
---|
| 132 | os << "(void)"; |
---|
| 133 | } // if |
---|
| 134 | } else { |
---|
| 135 | CodeGenerator2 cg( os ); |
---|
| 136 | os << "(" ; |
---|
| 137 | |
---|
| 138 | cg.genCommaList( pars.begin(), pars.end() ); |
---|
| 139 | |
---|
| 140 | if ( funcType->get_isVarArgs() ){ |
---|
| 141 | os << ", ..."; |
---|
| 142 | } // if |
---|
| 143 | os << ")"; |
---|
| 144 | } // if |
---|
| 145 | |
---|
| 146 | typeString = std::string( os.str(), os.pcount() ); |
---|
[51b7345] | 147 | |
---|
[17cd4eb] | 148 | if ( funcType->get_returnVals().size() == 0 ) { |
---|
| 149 | typeString = "void " + typeString; |
---|
| 150 | } else { |
---|
| 151 | funcType->get_returnVals().front()->get_type()->accept( *this ); |
---|
| 152 | } // if |
---|
| 153 | } |
---|
[51b7345] | 154 | |
---|
[17cd4eb] | 155 | void GenType::visit( StructInstType *structInst ) { |
---|
| 156 | typeString = "struct " + structInst->get_name() + " " + typeString; |
---|
| 157 | handleQualifiers( structInst ); |
---|
| 158 | } |
---|
[51b7345] | 159 | |
---|
[17cd4eb] | 160 | void GenType::visit( UnionInstType *unionInst ) { |
---|
| 161 | typeString = "union " + unionInst->get_name() + " " + typeString; |
---|
| 162 | handleQualifiers( unionInst ); |
---|
| 163 | } |
---|
[51b7345] | 164 | |
---|
[17cd4eb] | 165 | void GenType::visit( EnumInstType *enumInst ) { |
---|
| 166 | typeString = "enum " + enumInst->get_name() + " " + typeString; |
---|
| 167 | handleQualifiers( enumInst ); |
---|
| 168 | } |
---|
[51b7345] | 169 | |
---|
[17cd4eb] | 170 | void GenType::visit( TypeInstType *typeInst ) { |
---|
| 171 | typeString = typeInst->get_name() + " " + typeString; |
---|
| 172 | handleQualifiers( typeInst ); |
---|
| 173 | } |
---|
[51b7345] | 174 | |
---|
[17cd4eb] | 175 | void GenType::handleQualifiers( Type *type ) { |
---|
| 176 | if ( type->get_isConst() ) { |
---|
| 177 | typeString = "const " + typeString; |
---|
| 178 | } // if |
---|
| 179 | if ( type->get_isVolatile() ) { |
---|
| 180 | typeString = "volatile " + typeString; |
---|
| 181 | } // if |
---|
| 182 | if ( type->get_isRestrict() ) { |
---|
| 183 | typeString = "__restrict " + typeString; |
---|
| 184 | } // if |
---|
[bdd516a] | 185 | if ( type->get_isAtomic() ) { |
---|
| 186 | typeString = "_Atomic " + typeString; |
---|
| 187 | } // if |
---|
[17cd4eb] | 188 | } |
---|
[51b7345] | 189 | } // namespace CodeGen |
---|