[51587aa] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // GenType.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1db21619] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Jul 9 16:43:52 2015 |
---|
| 13 | // Update Count : 13 |
---|
[51587aa] | 14 | // |
---|
| 15 | |
---|
[5f2f2d7] | 16 | #include <sstream> |
---|
[51b7345] | 17 | #include <cassert> |
---|
| 18 | |
---|
| 19 | #include "GenType.h" |
---|
[a61fea9a] | 20 | #include "CodeGenerator.h" |
---|
[e8032b0] | 21 | |
---|
| 22 | #include "SynTree/Declaration.h" |
---|
[51b7345] | 23 | #include "SynTree/Expression.h" |
---|
[e8032b0] | 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/Visitor.h" |
---|
[51b7345] | 26 | |
---|
| 27 | namespace CodeGen { |
---|
[51587aa] | 28 | class GenType : public Visitor { |
---|
| 29 | public: |
---|
| 30 | GenType( const std::string &typeString ); |
---|
| 31 | std::string get_typeString() const { return typeString; } |
---|
| 32 | void set_typeString( const std::string &newValue ) { typeString = newValue; } |
---|
[51b7345] | 33 | |
---|
[51587aa] | 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( StructInstType *structInst ); |
---|
| 40 | virtual void visit( UnionInstType *unionInst ); |
---|
| 41 | virtual void visit( EnumInstType *enumInst ); |
---|
| 42 | virtual void visit( TypeInstType *typeInst ); |
---|
[44b7088] | 43 | virtual void visit( VarArgsType *varArgsType ); |
---|
[51b7345] | 44 | |
---|
[51587aa] | 45 | private: |
---|
| 46 | void handleQualifiers( Type *type ); |
---|
| 47 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
[51b7345] | 48 | |
---|
[51587aa] | 49 | std::string typeString; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | std::string genType( Type *type, const std::string &baseString ) { |
---|
| 53 | GenType gt( baseString ); |
---|
| 54 | type->accept( gt ); |
---|
| 55 | return gt.get_typeString(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | GenType::GenType( const std::string &typeString ) : typeString( typeString ) {} |
---|
| 59 | |
---|
| 60 | void GenType::visit( VoidType *voidType ) { |
---|
| 61 | typeString = "void " + typeString; |
---|
| 62 | handleQualifiers( voidType ); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void GenType::visit( BasicType *basicType ) { |
---|
| 66 | BasicType::Kind kind = basicType->get_kind(); |
---|
| 67 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
| 68 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
| 69 | handleQualifiers( basicType ); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
[5f2f2d7] | 73 | std::ostringstream os; |
---|
[51587aa] | 74 | if ( typeString != "" ) { |
---|
| 75 | if ( typeString[ 0 ] == '*' ) { |
---|
| 76 | os << "(" << typeString << ")"; |
---|
| 77 | } else { |
---|
| 78 | os << typeString; |
---|
| 79 | } // if |
---|
| 80 | } // if |
---|
| 81 | os << "["; |
---|
| 82 | |
---|
| 83 | if ( isStatic ) { |
---|
| 84 | os << "static "; |
---|
| 85 | } // if |
---|
| 86 | if ( qualifiers.isConst ) { |
---|
| 87 | os << "const "; |
---|
| 88 | } // if |
---|
| 89 | if ( qualifiers.isVolatile ) { |
---|
| 90 | os << "volatile "; |
---|
| 91 | } // if |
---|
| 92 | if ( qualifiers.isRestrict ) { |
---|
| 93 | os << "__restrict "; |
---|
| 94 | } // if |
---|
| 95 | if ( qualifiers.isAtomic ) { |
---|
| 96 | os << "_Atomic "; |
---|
| 97 | } // if |
---|
[1db21619] | 98 | if ( qualifiers.isAttribute ) { |
---|
| 99 | os << "__attribute(( )) "; |
---|
| 100 | } // if |
---|
[51587aa] | 101 | if ( dimension != 0 ) { |
---|
[6c4ff37] | 102 | CodeGenerator cg( os ); |
---|
[51587aa] | 103 | dimension->accept( cg ); |
---|
[71bd8c6] | 104 | } else if ( isVarLen ) { |
---|
[1db21619] | 105 | // no dimension expression on a VLA means it came in with the * token |
---|
[71bd8c6] | 106 | os << "*"; |
---|
[51587aa] | 107 | } // if |
---|
| 108 | os << "]"; |
---|
| 109 | |
---|
[5f2f2d7] | 110 | typeString = os.str(); |
---|
[51b7345] | 111 | |
---|
[51587aa] | 112 | base->accept( *this ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void GenType::visit( PointerType *pointerType ) { |
---|
| 116 | assert( pointerType->get_base() != 0); |
---|
| 117 | if ( pointerType->get_isStatic() || pointerType->get_isVarLen() || pointerType->get_dimension() ) { |
---|
| 118 | genArray( pointerType->get_qualifiers(), pointerType->get_base(), pointerType->get_dimension(), pointerType->get_isVarLen(), pointerType->get_isStatic() ); |
---|
| 119 | } else { |
---|
| 120 | handleQualifiers( pointerType ); |
---|
| 121 | if ( typeString[ 0 ] == '?' ) { |
---|
| 122 | typeString = "* " + typeString; |
---|
| 123 | } else { |
---|
| 124 | typeString = "*" + typeString; |
---|
| 125 | } // if |
---|
| 126 | pointerType->get_base()->accept( *this ); |
---|
| 127 | } // if |
---|
| 128 | } |
---|
| 129 | |
---|
[a08ba92] | 130 | void GenType::visit( ArrayType *arrayType ) { |
---|
[51587aa] | 131 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void GenType::visit( FunctionType *funcType ) { |
---|
[5f2f2d7] | 135 | std::ostringstream os; |
---|
[51587aa] | 136 | |
---|
| 137 | if ( typeString != "" ) { |
---|
| 138 | if ( typeString[ 0 ] == '*' ) { |
---|
| 139 | os << "(" << typeString << ")"; |
---|
| 140 | } else { |
---|
| 141 | os << typeString; |
---|
| 142 | } // if |
---|
| 143 | } // if |
---|
[51b7345] | 144 | |
---|
[51587aa] | 145 | /************* parameters ***************/ |
---|
| 146 | |
---|
| 147 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); |
---|
| 148 | |
---|
| 149 | if ( pars.empty() ) { |
---|
| 150 | if ( funcType->get_isVarArgs() ) { |
---|
| 151 | os << "()"; |
---|
| 152 | } else { |
---|
| 153 | os << "(void)"; |
---|
| 154 | } // if |
---|
| 155 | } else { |
---|
[6c4ff37] | 156 | CodeGenerator cg( os ); |
---|
[51587aa] | 157 | os << "(" ; |
---|
| 158 | |
---|
| 159 | cg.genCommaList( pars.begin(), pars.end() ); |
---|
| 160 | |
---|
[a08ba92] | 161 | if ( funcType->get_isVarArgs() ) { |
---|
[51587aa] | 162 | os << ", ..."; |
---|
| 163 | } // if |
---|
| 164 | os << ")"; |
---|
| 165 | } // if |
---|
[17cd4eb] | 166 | |
---|
[5f2f2d7] | 167 | typeString = os.str(); |
---|
[51587aa] | 168 | |
---|
| 169 | if ( funcType->get_returnVals().size() == 0 ) { |
---|
| 170 | typeString = "void " + typeString; |
---|
| 171 | } else { |
---|
| 172 | funcType->get_returnVals().front()->get_type()->accept( *this ); |
---|
| 173 | } // if |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | void GenType::visit( StructInstType *structInst ) { |
---|
| 177 | typeString = "struct " + structInst->get_name() + " " + typeString; |
---|
| 178 | handleQualifiers( structInst ); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | void GenType::visit( UnionInstType *unionInst ) { |
---|
| 182 | typeString = "union " + unionInst->get_name() + " " + typeString; |
---|
| 183 | handleQualifiers( unionInst ); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void GenType::visit( EnumInstType *enumInst ) { |
---|
| 187 | typeString = "enum " + enumInst->get_name() + " " + typeString; |
---|
| 188 | handleQualifiers( enumInst ); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | void GenType::visit( TypeInstType *typeInst ) { |
---|
| 192 | typeString = typeInst->get_name() + " " + typeString; |
---|
| 193 | handleQualifiers( typeInst ); |
---|
| 194 | } |
---|
| 195 | |
---|
[44b7088] | 196 | void GenType::visit( VarArgsType *varArgsType ) { |
---|
| 197 | typeString = "__builtin_va_list " + typeString; |
---|
[540ddb7d] | 198 | handleQualifiers( varArgsType ); |
---|
[44b7088] | 199 | } |
---|
| 200 | |
---|
[51587aa] | 201 | void GenType::handleQualifiers( Type *type ) { |
---|
| 202 | if ( type->get_isConst() ) { |
---|
| 203 | typeString = "const " + typeString; |
---|
| 204 | } // if |
---|
| 205 | if ( type->get_isVolatile() ) { |
---|
| 206 | typeString = "volatile " + typeString; |
---|
| 207 | } // if |
---|
| 208 | if ( type->get_isRestrict() ) { |
---|
| 209 | typeString = "__restrict " + typeString; |
---|
| 210 | } // if |
---|
| 211 | if ( type->get_isAtomic() ) { |
---|
| 212 | typeString = "_Atomic " + typeString; |
---|
| 213 | } // if |
---|
[1db21619] | 214 | if ( type->get_isAttribute() ) { |
---|
| 215 | typeString = "__attribute(( )) " + typeString; |
---|
| 216 | } // if |
---|
[51587aa] | 217 | } |
---|
[51b7345] | 218 | } // namespace CodeGen |
---|
[51587aa] | 219 | |
---|
| 220 | // Local Variables: // |
---|
| 221 | // tab-width: 4 // |
---|
| 222 | // mode: c++ // |
---|
| 223 | // compile-command: "make install" // |
---|
| 224 | // End: // |
---|