[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 | // |
---|
[486341f] | 7 | // GenType.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1db21619] | 11 | // Last Modified By : Peter A. Buhr |
---|
[615a096] | 12 | // Last Modified On : Fri Mar 17 09:02:28 2017 |
---|
| 13 | // Update Count : 22 |
---|
[51587aa] | 14 | // |
---|
[3268a58] | 15 | #include "GenType.h" |
---|
[51587aa] | 16 | |
---|
[bf2438c] | 17 | #include <cassert> // for assert, assertf |
---|
| 18 | #include <list> // for _List_iterator, _List_const_iterator |
---|
| 19 | #include <sstream> // for operator<<, ostringstream, basic_os... |
---|
[51b7345] | 20 | |
---|
[bf2438c] | 21 | #include "CodeGenerator.h" // for CodeGenerator |
---|
| 22 | #include "SynTree/Declaration.h" // for DeclarationWithType |
---|
| 23 | #include "SynTree/Expression.h" // for Expression |
---|
| 24 | #include "SynTree/Type.h" // for PointerType, Type, FunctionType |
---|
| 25 | #include "SynTree/Visitor.h" // for Visitor |
---|
[51b7345] | 26 | |
---|
| 27 | namespace CodeGen { |
---|
[51587aa] | 28 | class GenType : public Visitor { |
---|
| 29 | public: |
---|
[c850687] | 30 | GenType( const std::string &typeString, bool pretty = false, bool genC = false, bool lineMarks = false ); |
---|
[51587aa] | 31 | std::string get_typeString() const { return typeString; } |
---|
| 32 | void set_typeString( const std::string &newValue ) { typeString = newValue; } |
---|
[486341f] | 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 ); |
---|
[ce8c12f] | 39 | virtual void visit( ReferenceType *refType ); |
---|
[51587aa] | 40 | virtual void visit( StructInstType *structInst ); |
---|
| 41 | virtual void visit( UnionInstType *unionInst ); |
---|
| 42 | virtual void visit( EnumInstType *enumInst ); |
---|
| 43 | virtual void visit( TypeInstType *typeInst ); |
---|
[6d4d1a6] | 44 | virtual void visit( TupleType * tupleType ); |
---|
[44b7088] | 45 | virtual void visit( VarArgsType *varArgsType ); |
---|
[89e6ffc] | 46 | virtual void visit( ZeroType *zeroType ); |
---|
| 47 | virtual void visit( OneType *oneType ); |
---|
[486341f] | 48 | |
---|
[51587aa] | 49 | private: |
---|
| 50 | void handleQualifiers( Type *type ); |
---|
[e39241b] | 51 | std::string handleGeneric( ReferenceToType * refType ); |
---|
[51587aa] | 52 | void genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ); |
---|
[486341f] | 53 | |
---|
[51587aa] | 54 | std::string typeString; |
---|
[35b1bf4] | 55 | bool pretty = false; // pretty print |
---|
[e39241b] | 56 | bool genC = false; // generating C code? |
---|
[c850687] | 57 | bool lineMarks = false; |
---|
[51587aa] | 58 | }; |
---|
| 59 | |
---|
[c850687] | 60 | std::string genType( Type *type, const std::string &baseString, bool pretty, bool genC , bool lineMarks ) { |
---|
| 61 | GenType gt( baseString, pretty, genC, lineMarks ); |
---|
[c0aa336] | 62 | std::ostringstream os; |
---|
[35b1bf4] | 63 | |
---|
[c0aa336] | 64 | if ( ! type->get_attributes().empty() ) { |
---|
[c850687] | 65 | CodeGenerator cg( os, pretty, genC, lineMarks ); |
---|
[c0aa336] | 66 | cg.genAttributes( type->get_attributes() ); |
---|
| 67 | } // if |
---|
| 68 | |
---|
[51587aa] | 69 | type->accept( gt ); |
---|
[c0aa336] | 70 | return os.str() + gt.get_typeString(); |
---|
[51587aa] | 71 | } |
---|
| 72 | |
---|
[35b1bf4] | 73 | std::string genPrettyType( Type * type, const std::string & baseString ) { |
---|
[e39241b] | 74 | return genType( type, baseString, true, false ); |
---|
[35b1bf4] | 75 | } |
---|
| 76 | |
---|
[c850687] | 77 | GenType::GenType( const std::string &typeString, bool pretty, bool genC, bool lineMarks ) : typeString( typeString ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} |
---|
[51587aa] | 78 | |
---|
| 79 | void GenType::visit( VoidType *voidType ) { |
---|
| 80 | typeString = "void " + typeString; |
---|
| 81 | handleQualifiers( voidType ); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void GenType::visit( BasicType *basicType ) { |
---|
| 85 | BasicType::Kind kind = basicType->get_kind(); |
---|
| 86 | assert( 0 <= kind && kind < BasicType::NUMBER_OF_BASIC_TYPES ); |
---|
| 87 | typeString = std::string( BasicType::typeNames[kind] ) + " " + typeString; |
---|
| 88 | handleQualifiers( basicType ); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void GenType::genArray( const Type::Qualifiers &qualifiers, Type *base, Expression *dimension, bool isVarLen, bool isStatic ) { |
---|
[5f2f2d7] | 92 | std::ostringstream os; |
---|
[51587aa] | 93 | if ( typeString != "" ) { |
---|
| 94 | if ( typeString[ 0 ] == '*' ) { |
---|
| 95 | os << "(" << typeString << ")"; |
---|
| 96 | } else { |
---|
| 97 | os << typeString; |
---|
| 98 | } // if |
---|
| 99 | } // if |
---|
| 100 | os << "["; |
---|
| 101 | |
---|
| 102 | if ( isStatic ) { |
---|
| 103 | os << "static "; |
---|
| 104 | } // if |
---|
[615a096] | 105 | if ( qualifiers.is_const ) { |
---|
[51587aa] | 106 | os << "const "; |
---|
| 107 | } // if |
---|
[615a096] | 108 | if ( qualifiers.is_volatile ) { |
---|
[51587aa] | 109 | os << "volatile "; |
---|
| 110 | } // if |
---|
[615a096] | 111 | if ( qualifiers.is_restrict ) { |
---|
[51587aa] | 112 | os << "__restrict "; |
---|
| 113 | } // if |
---|
[615a096] | 114 | if ( qualifiers.is_atomic ) { |
---|
[51587aa] | 115 | os << "_Atomic "; |
---|
| 116 | } // if |
---|
| 117 | if ( dimension != 0 ) { |
---|
[c850687] | 118 | CodeGenerator cg( os, pretty, genC, lineMarks ); |
---|
[51587aa] | 119 | dimension->accept( cg ); |
---|
[71bd8c6] | 120 | } else if ( isVarLen ) { |
---|
[1db21619] | 121 | // no dimension expression on a VLA means it came in with the * token |
---|
[71bd8c6] | 122 | os << "*"; |
---|
[51587aa] | 123 | } // if |
---|
| 124 | os << "]"; |
---|
| 125 | |
---|
[5f2f2d7] | 126 | typeString = os.str(); |
---|
[486341f] | 127 | |
---|
[51587aa] | 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() ); |
---|
| 135 | } else { |
---|
| 136 | handleQualifiers( pointerType ); |
---|
| 137 | if ( typeString[ 0 ] == '?' ) { |
---|
| 138 | typeString = "* " + typeString; |
---|
| 139 | } else { |
---|
| 140 | typeString = "*" + typeString; |
---|
| 141 | } // if |
---|
| 142 | pointerType->get_base()->accept( *this ); |
---|
| 143 | } // if |
---|
| 144 | } |
---|
| 145 | |
---|
[a08ba92] | 146 | void GenType::visit( ArrayType *arrayType ) { |
---|
[51587aa] | 147 | genArray( arrayType->get_qualifiers(), arrayType->get_base(), arrayType->get_dimension(), arrayType->get_isVarLen(), arrayType->get_isStatic() ); |
---|
| 148 | } |
---|
| 149 | |
---|
[ce8c12f] | 150 | void GenType::visit( ReferenceType *refType ) { |
---|
| 151 | assert( refType->get_base() != 0); |
---|
| 152 | assertf( ! genC, "Reference types should not reach code generation." ); |
---|
| 153 | handleQualifiers( refType ); |
---|
| 154 | typeString = "&" + typeString; |
---|
| 155 | refType->get_base()->accept( *this ); |
---|
| 156 | } |
---|
| 157 | |
---|
[51587aa] | 158 | void GenType::visit( FunctionType *funcType ) { |
---|
[5f2f2d7] | 159 | std::ostringstream os; |
---|
[51587aa] | 160 | |
---|
| 161 | if ( typeString != "" ) { |
---|
| 162 | if ( typeString[ 0 ] == '*' ) { |
---|
| 163 | os << "(" << typeString << ")"; |
---|
| 164 | } else { |
---|
| 165 | os << typeString; |
---|
| 166 | } // if |
---|
| 167 | } // if |
---|
[486341f] | 168 | |
---|
[51587aa] | 169 | /************* parameters ***************/ |
---|
| 170 | |
---|
| 171 | const std::list<DeclarationWithType *> &pars = funcType->get_parameters(); |
---|
| 172 | |
---|
| 173 | if ( pars.empty() ) { |
---|
| 174 | if ( funcType->get_isVarArgs() ) { |
---|
| 175 | os << "()"; |
---|
| 176 | } else { |
---|
| 177 | os << "(void)"; |
---|
| 178 | } // if |
---|
| 179 | } else { |
---|
[c850687] | 180 | CodeGenerator cg( os, pretty, genC, lineMarks ); |
---|
[51587aa] | 181 | os << "(" ; |
---|
| 182 | |
---|
| 183 | cg.genCommaList( pars.begin(), pars.end() ); |
---|
| 184 | |
---|
[a08ba92] | 185 | if ( funcType->get_isVarArgs() ) { |
---|
[51587aa] | 186 | os << ", ..."; |
---|
| 187 | } // if |
---|
| 188 | os << ")"; |
---|
| 189 | } // if |
---|
[486341f] | 190 | |
---|
[5f2f2d7] | 191 | typeString = os.str(); |
---|
[51587aa] | 192 | |
---|
| 193 | if ( funcType->get_returnVals().size() == 0 ) { |
---|
| 194 | typeString = "void " + typeString; |
---|
| 195 | } else { |
---|
| 196 | funcType->get_returnVals().front()->get_type()->accept( *this ); |
---|
| 197 | } // if |
---|
[5f642e38] | 198 | |
---|
| 199 | // add forall |
---|
| 200 | if( ! funcType->get_forall().empty() && ! genC ) { |
---|
| 201 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
| 202 | std::ostringstream os; |
---|
[c850687] | 203 | CodeGenerator cg( os, pretty, genC, lineMarks ); |
---|
[5f642e38] | 204 | os << "forall("; |
---|
| 205 | cg.genCommaList( funcType->get_forall().begin(), funcType->get_forall().end() ); |
---|
| 206 | os << ")" << std::endl; |
---|
| 207 | typeString = os.str() + typeString; |
---|
| 208 | } |
---|
[51587aa] | 209 | } |
---|
| 210 | |
---|
[e39241b] | 211 | std::string GenType::handleGeneric( ReferenceToType * refType ) { |
---|
| 212 | if ( ! refType->get_parameters().empty() ) { |
---|
| 213 | std::ostringstream os; |
---|
[c850687] | 214 | CodeGenerator cg( os, pretty, genC, lineMarks ); |
---|
[e39241b] | 215 | os << "("; |
---|
| 216 | cg.genCommaList( refType->get_parameters().begin(), refType->get_parameters().end() ); |
---|
| 217 | os << ") "; |
---|
| 218 | return os.str(); |
---|
| 219 | } |
---|
| 220 | return ""; |
---|
| 221 | } |
---|
| 222 | |
---|
[51587aa] | 223 | void GenType::visit( StructInstType *structInst ) { |
---|
[e39241b] | 224 | typeString = structInst->get_name() + handleGeneric( structInst ) + " " + typeString; |
---|
| 225 | if ( genC ) typeString = "struct " + typeString; |
---|
[51587aa] | 226 | handleQualifiers( structInst ); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | void GenType::visit( UnionInstType *unionInst ) { |
---|
[e39241b] | 230 | typeString = unionInst->get_name() + handleGeneric( unionInst ) + " " + typeString; |
---|
| 231 | if ( genC ) typeString = "union " + typeString; |
---|
[51587aa] | 232 | handleQualifiers( unionInst ); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | void GenType::visit( EnumInstType *enumInst ) { |
---|
[e39241b] | 236 | typeString = enumInst->get_name() + " " + typeString; |
---|
| 237 | if ( genC ) typeString = "enum " + typeString; |
---|
[51587aa] | 238 | handleQualifiers( enumInst ); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | void GenType::visit( TypeInstType *typeInst ) { |
---|
| 242 | typeString = typeInst->get_name() + " " + typeString; |
---|
| 243 | handleQualifiers( typeInst ); |
---|
| 244 | } |
---|
| 245 | |
---|
[6d4d1a6] | 246 | void GenType::visit( TupleType * tupleType ) { |
---|
[e39241b] | 247 | assertf( ! genC, "Tuple types should not reach code generation." ); |
---|
[35b1bf4] | 248 | unsigned int i = 0; |
---|
| 249 | std::ostringstream os; |
---|
| 250 | os << "["; |
---|
| 251 | for ( Type * t : *tupleType ) { |
---|
| 252 | i++; |
---|
[c850687] | 253 | os << genType( t, "", pretty, genC, lineMarks ) << (i == tupleType->size() ? "" : ", "); |
---|
[35b1bf4] | 254 | } |
---|
[65aca88] | 255 | os << "] "; |
---|
[35b1bf4] | 256 | typeString = os.str() + typeString; |
---|
[6d4d1a6] | 257 | } |
---|
| 258 | |
---|
[44b7088] | 259 | void GenType::visit( VarArgsType *varArgsType ) { |
---|
| 260 | typeString = "__builtin_va_list " + typeString; |
---|
[540ddb7d] | 261 | handleQualifiers( varArgsType ); |
---|
[44b7088] | 262 | } |
---|
| 263 | |
---|
[89e6ffc] | 264 | void GenType::visit( ZeroType *zeroType ) { |
---|
| 265 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
[35b1bf4] | 266 | typeString = (pretty ? "zero_t " : "long int ") + typeString; |
---|
[89e6ffc] | 267 | handleQualifiers( zeroType ); |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | void GenType::visit( OneType *oneType ) { |
---|
| 271 | // ideally these wouldn't hit codegen at all, but should be safe to make them ints |
---|
[35b1bf4] | 272 | typeString = (pretty ? "one_t " : "long int ") + typeString; |
---|
[89e6ffc] | 273 | handleQualifiers( oneType ); |
---|
| 274 | } |
---|
| 275 | |
---|
[51587aa] | 276 | void GenType::handleQualifiers( Type *type ) { |
---|
[615a096] | 277 | if ( type->get_const() ) { |
---|
[51587aa] | 278 | typeString = "const " + typeString; |
---|
| 279 | } // if |
---|
[615a096] | 280 | if ( type->get_volatile() ) { |
---|
[51587aa] | 281 | typeString = "volatile " + typeString; |
---|
| 282 | } // if |
---|
[615a096] | 283 | if ( type->get_restrict() ) { |
---|
[51587aa] | 284 | typeString = "__restrict " + typeString; |
---|
| 285 | } // if |
---|
[615a096] | 286 | if ( type->get_atomic() ) { |
---|
[51587aa] | 287 | typeString = "_Atomic " + typeString; |
---|
| 288 | } // if |
---|
[d104b02] | 289 | if ( type->get_lvalue() && ! genC ) { |
---|
| 290 | // when not generating C code, print lvalue for debugging. |
---|
| 291 | typeString = "lvalue " + typeString; |
---|
| 292 | } |
---|
[51587aa] | 293 | } |
---|
[51b7345] | 294 | } // namespace CodeGen |
---|
[51587aa] | 295 | |
---|
| 296 | // Local Variables: // |
---|
| 297 | // tab-width: 4 // |
---|
| 298 | // mode: c++ // |
---|
| 299 | // compile-command: "make install" // |
---|
| 300 | // End: // |
---|