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