[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 | // |
---|
[c92bdcc] | 7 | // GenType.cpp -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[42a36d9] | 11 | // Last Modified By : Andrew Beach |
---|
[c715e5f] | 12 | // Last Modified On : Fri May 20 11:18:00 2022 |
---|
| 13 | // Update Count : 24 |
---|
[51587aa] | 14 | // |
---|
[c92bdcc] | 15 | |
---|
| 16 | #include "GenType.hpp" |
---|
[51587aa] | 17 | |
---|
[bf2438c] | 18 | #include <cassert> // for assert, assertf |
---|
| 19 | #include <list> // for _List_iterator, _List_const_iterator |
---|
| 20 | #include <sstream> // for operator<<, ostringstream, basic_os... |
---|
[51b7345] | 21 | |
---|
[8941b6b] | 22 | #include "AST/Print.hpp" // for print |
---|
| 23 | #include "AST/Vector.hpp" // for vector |
---|
[83fd57d] | 24 | #include "CodeGenerator.hpp" // for CodeGenerator |
---|
[c92bdcc] | 25 | #include "Common/UniqueName.hpp" // for UniqueName |
---|
[51b7345] | 26 | |
---|
| 27 | namespace CodeGen { |
---|
[8941b6b] | 28 | |
---|
| 29 | namespace { |
---|
| 30 | |
---|
[0bd3faf] | 31 | struct GenType final : |
---|
[8941b6b] | 32 | public ast::WithShortCircuiting, |
---|
[0bd3faf] | 33 | public ast::WithVisitorRef<GenType> { |
---|
[8941b6b] | 34 | std::string result; |
---|
[0bd3faf] | 35 | GenType( const std::string &typeString, const Options &options ); |
---|
[8941b6b] | 36 | |
---|
| 37 | void previsit( ast::Node const * ); |
---|
| 38 | void postvisit( ast::Node const * ); |
---|
| 39 | |
---|
| 40 | void postvisit( ast::FunctionType const * type ); |
---|
| 41 | void postvisit( ast::VoidType const * type ); |
---|
| 42 | void postvisit( ast::BasicType const * type ); |
---|
| 43 | void postvisit( ast::PointerType const * type ); |
---|
| 44 | void postvisit( ast::ArrayType const * type ); |
---|
| 45 | void postvisit( ast::ReferenceType const * type ); |
---|
| 46 | void postvisit( ast::StructInstType const * type ); |
---|
| 47 | void postvisit( ast::UnionInstType const * type ); |
---|
| 48 | void postvisit( ast::EnumInstType const * type ); |
---|
| 49 | void postvisit( ast::TypeInstType const * type ); |
---|
| 50 | void postvisit( ast::TupleType const * type ); |
---|
| 51 | void postvisit( ast::VarArgsType const * type ); |
---|
| 52 | void postvisit( ast::ZeroType const * type ); |
---|
| 53 | void postvisit( ast::OneType const * type ); |
---|
| 54 | void postvisit( ast::GlobalScopeType const * type ); |
---|
| 55 | void postvisit( ast::TraitInstType const * type ); |
---|
| 56 | void postvisit( ast::TypeofType const * type ); |
---|
| 57 | void postvisit( ast::VTableType const * type ); |
---|
| 58 | void postvisit( ast::QualifiedType const * type ); |
---|
| 59 | |
---|
| 60 | private: |
---|
| 61 | void handleQualifiers( ast::Type const *type ); |
---|
| 62 | std::string handleGeneric( ast::BaseInstType const * type ); |
---|
| 63 | void genArray( const ast::CV::Qualifiers &qualifiers, ast::Type const *base, ast::Expr const *dimension, bool isVarLen, bool isStatic ); |
---|
| 64 | std::string genParamList( const ast::vector<ast::Type> & ); |
---|
| 65 | |
---|
| 66 | Options options; |
---|
| 67 | }; |
---|
| 68 | |
---|
[0bd3faf] | 69 | GenType::GenType( const std::string &typeString, const Options &options ) : result( typeString ), options( options ) {} |
---|
[8941b6b] | 70 | |
---|
[0bd3faf] | 71 | void GenType::previsit( ast::Node const * ) { |
---|
[8941b6b] | 72 | // Turn off automatic recursion for all nodes, to allow each visitor to |
---|
| 73 | // precisely control the order in which its children are visited. |
---|
| 74 | visit_children = false; |
---|
| 75 | } |
---|
| 76 | |
---|
[0bd3faf] | 77 | void GenType::postvisit( ast::Node const * node ) { |
---|
[8941b6b] | 78 | std::stringstream ss; |
---|
| 79 | ast::print( ss, node ); |
---|
| 80 | assertf( false, "Unhandled node reached in GenType: %s", ss.str().c_str() ); |
---|
| 81 | } |
---|
| 82 | |
---|
[0bd3faf] | 83 | void GenType::postvisit( ast::VoidType const * type ) { |
---|
[8941b6b] | 84 | result = "void " + result; |
---|
| 85 | handleQualifiers( type ); |
---|
| 86 | } |
---|
| 87 | |
---|
[0bd3faf] | 88 | void GenType::postvisit( ast::BasicType const * type ) { |
---|
[7a780ad] | 89 | ast::BasicKind kind = type->kind; |
---|
| 90 | assert( 0 <= kind && kind < ast::BasicKind::NUMBER_OF_BASIC_TYPES ); |
---|
[8941b6b] | 91 | result = std::string( ast::BasicType::typeNames[kind] ) + " " + result; |
---|
| 92 | handleQualifiers( type ); |
---|
| 93 | } |
---|
| 94 | |
---|
[0bd3faf] | 95 | void GenType::genArray( const ast::CV::Qualifiers & qualifiers, ast::Type const * base, ast::Expr const *dimension, bool isVarLen, bool isStatic ) { |
---|
[8941b6b] | 96 | std::ostringstream os; |
---|
| 97 | if ( result != "" ) { |
---|
| 98 | if ( result[ 0 ] == '*' ) { |
---|
| 99 | os << "(" << result << ")"; |
---|
| 100 | } else { |
---|
| 101 | os << result; |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | os << "["; |
---|
| 105 | if ( isStatic ) { |
---|
| 106 | os << "static "; |
---|
| 107 | } |
---|
| 108 | if ( qualifiers.is_const ) { |
---|
| 109 | os << "const "; |
---|
| 110 | } |
---|
| 111 | if ( qualifiers.is_volatile ) { |
---|
| 112 | os << "volatile "; |
---|
| 113 | } |
---|
| 114 | if ( qualifiers.is_restrict ) { |
---|
| 115 | os << "__restrict "; |
---|
| 116 | } |
---|
| 117 | if ( qualifiers.is_atomic ) { |
---|
| 118 | os << "_Atomic "; |
---|
| 119 | } |
---|
| 120 | if ( dimension != 0 ) { |
---|
[0bd3faf] | 121 | ast::Pass<CodeGenerator>::read( dimension, os, options ); |
---|
[8941b6b] | 122 | } else if ( isVarLen ) { |
---|
| 123 | // no dimension expression on a VLA means it came in with the * token |
---|
| 124 | os << "*"; |
---|
| 125 | } |
---|
| 126 | os << "]"; |
---|
| 127 | |
---|
| 128 | result = os.str(); |
---|
| 129 | |
---|
| 130 | base->accept( *visitor ); |
---|
| 131 | } |
---|
| 132 | |
---|
[0bd3faf] | 133 | void GenType::postvisit( ast::PointerType const * type ) { |
---|
[8941b6b] | 134 | if ( type->isStatic || type->isVarLen || type->dimension ) { |
---|
| 135 | genArray( type->qualifiers, type->base, type->dimension, type->isVarLen, type->isStatic ); |
---|
| 136 | } else { |
---|
| 137 | handleQualifiers( type ); |
---|
| 138 | if ( result[ 0 ] == '?' ) { |
---|
| 139 | result = "* " + result; |
---|
| 140 | } else { |
---|
| 141 | result = "*" + result; |
---|
| 142 | } |
---|
| 143 | type->base->accept( *visitor ); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
[0bd3faf] | 147 | void GenType::postvisit( ast::ArrayType const * type ) { |
---|
[8941b6b] | 148 | genArray( type->qualifiers, type->base, type->dimension, type->isVarLen, type->isStatic ); |
---|
| 149 | } |
---|
| 150 | |
---|
[0bd3faf] | 151 | void GenType::postvisit( ast::ReferenceType const * type ) { |
---|
[8941b6b] | 152 | assertf( !options.genC, "Reference types should not reach code generation." ); |
---|
| 153 | handleQualifiers( type ); |
---|
| 154 | result = "&" + result; |
---|
| 155 | type->base->accept( *visitor ); |
---|
| 156 | } |
---|
| 157 | |
---|
[0bd3faf] | 158 | void GenType::postvisit( ast::FunctionType const * type ) { |
---|
[8941b6b] | 159 | std::ostringstream os; |
---|
| 160 | |
---|
| 161 | if ( result != "" ) { |
---|
| 162 | if ( result[ 0 ] == '*' ) { |
---|
| 163 | os << "(" << result << ")"; |
---|
| 164 | } else { |
---|
| 165 | os << result; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | if ( type->params.empty() ) { |
---|
[db19e1d] | 170 | if ( !type->isVarArgs ) { |
---|
| 171 | os << "(void)"; |
---|
| 172 | } else if ( options.genC ) { |
---|
[8941b6b] | 173 | os << "()"; |
---|
| 174 | } else { |
---|
[db19e1d] | 175 | os << "(...)"; |
---|
[8941b6b] | 176 | } |
---|
| 177 | } else { |
---|
| 178 | os << "(" ; |
---|
| 179 | |
---|
| 180 | os << genParamList( type->params ); |
---|
| 181 | |
---|
| 182 | if ( type->isVarArgs ) { |
---|
| 183 | os << ", ..."; |
---|
| 184 | } |
---|
| 185 | os << ")"; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | result = os.str(); |
---|
| 189 | |
---|
| 190 | if ( type->returns.size() == 0 ) { |
---|
| 191 | result = "void " + result; |
---|
| 192 | } else { |
---|
| 193 | type->returns.front()->accept( *visitor ); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | // Add forall clause. |
---|
| 197 | if( !type->forall.empty() && !options.genC ) { |
---|
| 198 | //assertf( !options.genC, "FunctionDecl type parameters should not reach code generation." ); |
---|
| 199 | std::ostringstream os; |
---|
[0bd3faf] | 200 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 201 | os << "forall("; |
---|
| 202 | cg.core.genCommaList( type->forall ); |
---|
| 203 | os << ")" << std::endl; |
---|
| 204 | result = os.str() + result; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
[0bd3faf] | 208 | std::string GenType::handleGeneric( ast::BaseInstType const * type ) { |
---|
[8941b6b] | 209 | if ( !type->params.empty() ) { |
---|
| 210 | std::ostringstream os; |
---|
[0bd3faf] | 211 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 212 | os << "("; |
---|
| 213 | cg.core.genCommaList( type->params ); |
---|
| 214 | os << ") "; |
---|
| 215 | return os.str(); |
---|
| 216 | } |
---|
| 217 | return ""; |
---|
| 218 | } |
---|
| 219 | |
---|
[0bd3faf] | 220 | void GenType::postvisit( ast::StructInstType const * type ) { |
---|
[8941b6b] | 221 | result = type->name + handleGeneric( type ) + " " + result; |
---|
| 222 | if ( options.genC ) result = "struct " + result; |
---|
| 223 | handleQualifiers( type ); |
---|
| 224 | } |
---|
| 225 | |
---|
[0bd3faf] | 226 | void GenType::postvisit( ast::UnionInstType const * type ) { |
---|
[8941b6b] | 227 | result = type->name + handleGeneric( type ) + " " + result; |
---|
| 228 | if ( options.genC ) result = "union " + result; |
---|
| 229 | handleQualifiers( type ); |
---|
| 230 | } |
---|
| 231 | |
---|
[0bd3faf] | 232 | void GenType::postvisit( ast::EnumInstType const * type ) { |
---|
[c75b30a] | 233 | // if ( type->base && type->base->base ) { |
---|
| 234 | // result = genType( type->base->base, result, options ); |
---|
| 235 | // } else { |
---|
[8941b6b] | 236 | result = type->name + " " + result; |
---|
| 237 | if ( options.genC ) { |
---|
| 238 | result = "enum " + result; |
---|
| 239 | } |
---|
[c75b30a] | 240 | // } |
---|
[8941b6b] | 241 | handleQualifiers( type ); |
---|
| 242 | } |
---|
| 243 | |
---|
[0bd3faf] | 244 | void GenType::postvisit( ast::TypeInstType const * type ) { |
---|
[8941b6b] | 245 | assertf( !options.genC, "TypeInstType should not reach code generation." ); |
---|
| 246 | result = type->name + " " + result; |
---|
| 247 | handleQualifiers( type ); |
---|
| 248 | } |
---|
| 249 | |
---|
[0bd3faf] | 250 | void GenType::postvisit( ast::TupleType const * type ) { |
---|
[8941b6b] | 251 | assertf( !options.genC, "TupleType should not reach code generation." ); |
---|
| 252 | unsigned int i = 0; |
---|
| 253 | std::ostringstream os; |
---|
| 254 | os << "["; |
---|
| 255 | for ( ast::ptr<ast::Type> const & t : type->types ) { |
---|
| 256 | i++; |
---|
| 257 | os << genType( t, "", options ) << (i == type->size() ? "" : ", "); |
---|
| 258 | } |
---|
| 259 | os << "] "; |
---|
| 260 | result = os.str() + result; |
---|
| 261 | } |
---|
| 262 | |
---|
[0bd3faf] | 263 | void GenType::postvisit( ast::VarArgsType const * type ) { |
---|
[8941b6b] | 264 | result = "__builtin_va_list " + result; |
---|
| 265 | handleQualifiers( type ); |
---|
| 266 | } |
---|
| 267 | |
---|
[0bd3faf] | 268 | void GenType::postvisit( ast::ZeroType const * type ) { |
---|
[8941b6b] | 269 | // Ideally these wouldn't hit codegen at all, but should be safe to make them ints. |
---|
| 270 | result = (options.pretty ? "zero_t " : "long int ") + result; |
---|
| 271 | handleQualifiers( type ); |
---|
| 272 | } |
---|
| 273 | |
---|
[0bd3faf] | 274 | void GenType::postvisit( ast::OneType const * type ) { |
---|
[8941b6b] | 275 | // Ideally these wouldn't hit codegen at all, but should be safe to make them ints. |
---|
| 276 | result = (options.pretty ? "one_t " : "long int ") + result; |
---|
| 277 | handleQualifiers( type ); |
---|
| 278 | } |
---|
| 279 | |
---|
[0bd3faf] | 280 | void GenType::postvisit( ast::GlobalScopeType const * type ) { |
---|
[8941b6b] | 281 | assertf( !options.genC, "GlobalScopeType should not reach code generation." ); |
---|
| 282 | handleQualifiers( type ); |
---|
| 283 | } |
---|
| 284 | |
---|
[0bd3faf] | 285 | void GenType::postvisit( ast::TraitInstType const * type ) { |
---|
[8941b6b] | 286 | assertf( !options.genC, "TraitInstType should not reach code generation." ); |
---|
| 287 | result = type->name + " " + result; |
---|
| 288 | handleQualifiers( type ); |
---|
| 289 | } |
---|
| 290 | |
---|
[0bd3faf] | 291 | void GenType::postvisit( ast::TypeofType const * type ) { |
---|
[8941b6b] | 292 | std::ostringstream os; |
---|
| 293 | os << "typeof("; |
---|
[116d7e2] | 294 | ast::Pass<CodeGenerator>::read( type->expr.get(), os, options ); |
---|
[8941b6b] | 295 | os << ") " << result; |
---|
| 296 | result = os.str(); |
---|
| 297 | handleQualifiers( type ); |
---|
| 298 | } |
---|
| 299 | |
---|
[0bd3faf] | 300 | void GenType::postvisit( ast::VTableType const * type ) { |
---|
[8941b6b] | 301 | assertf( !options.genC, "Virtual table types should not reach code generation." ); |
---|
| 302 | std::ostringstream os; |
---|
| 303 | os << "vtable(" << genType( type->base, "", options ) << ") " << result; |
---|
| 304 | result = os.str(); |
---|
| 305 | handleQualifiers( type ); |
---|
| 306 | } |
---|
| 307 | |
---|
[0bd3faf] | 308 | void GenType::postvisit( ast::QualifiedType const * type ) { |
---|
[8941b6b] | 309 | assertf( !options.genC, "QualifiedType should not reach code generation." ); |
---|
| 310 | std::ostringstream os; |
---|
| 311 | os << genType( type->parent, "", options ) << "." << genType( type->child, "", options ) << result; |
---|
| 312 | result = os.str(); |
---|
| 313 | handleQualifiers( type ); |
---|
| 314 | } |
---|
| 315 | |
---|
[0bd3faf] | 316 | void GenType::handleQualifiers( ast::Type const * type ) { |
---|
[8941b6b] | 317 | if ( type->is_const() ) { |
---|
| 318 | result = "const " + result; |
---|
| 319 | } |
---|
| 320 | if ( type->is_volatile() ) { |
---|
| 321 | result = "volatile " + result; |
---|
| 322 | } |
---|
| 323 | if ( type->is_restrict() ) { |
---|
| 324 | result = "__restrict " + result; |
---|
| 325 | } |
---|
| 326 | if ( type->is_atomic() ) { |
---|
| 327 | result = "_Atomic " + result; |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
[0bd3faf] | 331 | std::string GenType::genParamList( const ast::vector<ast::Type> & range ) { |
---|
[8941b6b] | 332 | auto cur = range.begin(); |
---|
| 333 | auto end = range.end(); |
---|
| 334 | if ( cur == end ) return ""; |
---|
| 335 | std::ostringstream oss; |
---|
[934fa0f] | 336 | UniqueName param( "__param_" ); |
---|
| 337 | while ( true ) { |
---|
| 338 | oss << genType( *cur++, options.genC ? param.newName() : "", options ); |
---|
[8941b6b] | 339 | if ( cur == end ) break; |
---|
| 340 | oss << ", "; |
---|
| 341 | } |
---|
| 342 | return oss.str(); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | } // namespace |
---|
| 346 | |
---|
| 347 | std::string genType( ast::Type const * type, const std::string & base, const Options & options ) { |
---|
| 348 | std::ostringstream os; |
---|
| 349 | if ( !type->attributes.empty() ) { |
---|
[0bd3faf] | 350 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 351 | cg.core.genAttributes( type->attributes ); |
---|
| 352 | } |
---|
| 353 | |
---|
[0bd3faf] | 354 | return os.str() + ast::Pass<GenType>::read( type, base, options ); |
---|
[8941b6b] | 355 | } |
---|
| 356 | |
---|
| 357 | std::string genTypeNoAttr( ast::Type const * type, const std::string & base, const Options & options ) { |
---|
[0bd3faf] | 358 | return ast::Pass<GenType>::read( type, base, options ); |
---|
[8941b6b] | 359 | } |
---|
| 360 | |
---|
[51b7345] | 361 | } // namespace CodeGen |
---|
[51587aa] | 362 | |
---|
| 363 | // Local Variables: // |
---|
| 364 | // tab-width: 4 // |
---|
| 365 | // mode: c++ // |
---|
| 366 | // compile-command: "make install" // |
---|
| 367 | // End: // |
---|