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