[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 |
---|
[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 | // |
---|
[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 | |
---|
[8941b6b] | 21 | #include "AST/Print.hpp" // for print |
---|
| 22 | #include "AST/Vector.hpp" // for vector |
---|
[83fd57d] | 23 | #include "CodeGenerator.hpp" // for CodeGenerator |
---|
[934fa0f] | 24 | #include "Common/UniqueName.h" // for UniqueName |
---|
[51b7345] | 25 | |
---|
| 26 | namespace CodeGen { |
---|
[8941b6b] | 27 | |
---|
| 28 | namespace { |
---|
| 29 | |
---|
[0bd3faf] | 30 | struct GenType final : |
---|
[8941b6b] | 31 | public ast::WithShortCircuiting, |
---|
[0bd3faf] | 32 | public ast::WithVisitorRef<GenType> { |
---|
[8941b6b] | 33 | std::string result; |
---|
[0bd3faf] | 34 | GenType( const std::string &typeString, const Options &options ); |
---|
[8941b6b] | 35 | |
---|
| 36 | void previsit( ast::Node const * ); |
---|
| 37 | void postvisit( ast::Node const * ); |
---|
| 38 | |
---|
| 39 | void postvisit( ast::FunctionType const * type ); |
---|
| 40 | void postvisit( ast::VoidType const * type ); |
---|
| 41 | void postvisit( ast::BasicType const * type ); |
---|
| 42 | void postvisit( ast::PointerType const * type ); |
---|
| 43 | void postvisit( ast::ArrayType const * type ); |
---|
| 44 | void postvisit( ast::ReferenceType const * type ); |
---|
| 45 | void postvisit( ast::StructInstType const * type ); |
---|
| 46 | void postvisit( ast::UnionInstType const * type ); |
---|
| 47 | void postvisit( ast::EnumInstType const * type ); |
---|
[af746cc] | 48 | void postvisit( ast::EnumAttrType const * type ); |
---|
[8941b6b] | 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() ) { |
---|
| 170 | if ( type->isVarArgs ) { |
---|
| 171 | os << "()"; |
---|
| 172 | } else { |
---|
| 173 | os << "(void)"; |
---|
| 174 | } |
---|
| 175 | } else { |
---|
| 176 | os << "(" ; |
---|
| 177 | |
---|
| 178 | os << genParamList( type->params ); |
---|
| 179 | |
---|
| 180 | if ( type->isVarArgs ) { |
---|
| 181 | os << ", ..."; |
---|
| 182 | } |
---|
| 183 | os << ")"; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | result = os.str(); |
---|
| 187 | |
---|
| 188 | if ( type->returns.size() == 0 ) { |
---|
| 189 | result = "void " + result; |
---|
| 190 | } else { |
---|
| 191 | type->returns.front()->accept( *visitor ); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | // Add forall clause. |
---|
| 195 | if( !type->forall.empty() && !options.genC ) { |
---|
| 196 | //assertf( !options.genC, "FunctionDecl type parameters should not reach code generation." ); |
---|
| 197 | std::ostringstream os; |
---|
[0bd3faf] | 198 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 199 | os << "forall("; |
---|
| 200 | cg.core.genCommaList( type->forall ); |
---|
| 201 | os << ")" << std::endl; |
---|
| 202 | result = os.str() + result; |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
[0bd3faf] | 206 | std::string GenType::handleGeneric( ast::BaseInstType const * type ) { |
---|
[8941b6b] | 207 | if ( !type->params.empty() ) { |
---|
| 208 | std::ostringstream os; |
---|
[0bd3faf] | 209 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 210 | os << "("; |
---|
| 211 | cg.core.genCommaList( type->params ); |
---|
| 212 | os << ") "; |
---|
| 213 | return os.str(); |
---|
| 214 | } |
---|
| 215 | return ""; |
---|
| 216 | } |
---|
| 217 | |
---|
[0bd3faf] | 218 | void GenType::postvisit( ast::StructInstType const * type ) { |
---|
[8941b6b] | 219 | result = type->name + handleGeneric( type ) + " " + result; |
---|
| 220 | if ( options.genC ) result = "struct " + result; |
---|
| 221 | handleQualifiers( type ); |
---|
| 222 | } |
---|
| 223 | |
---|
[0bd3faf] | 224 | void GenType::postvisit( ast::UnionInstType const * type ) { |
---|
[8941b6b] | 225 | result = type->name + handleGeneric( type ) + " " + result; |
---|
| 226 | if ( options.genC ) result = "union " + result; |
---|
| 227 | handleQualifiers( type ); |
---|
| 228 | } |
---|
| 229 | |
---|
[0bd3faf] | 230 | void GenType::postvisit( ast::EnumInstType const * type ) { |
---|
[c75b30a] | 231 | // if ( type->base && type->base->base ) { |
---|
| 232 | // result = genType( type->base->base, result, options ); |
---|
| 233 | // } else { |
---|
[8941b6b] | 234 | result = type->name + " " + result; |
---|
| 235 | if ( options.genC ) { |
---|
| 236 | result = "enum " + result; |
---|
| 237 | } |
---|
[c75b30a] | 238 | // } |
---|
[8941b6b] | 239 | handleQualifiers( type ); |
---|
| 240 | } |
---|
| 241 | |
---|
[af746cc] | 242 | void GenType::postvisit( ast::EnumAttrType const * type ) { |
---|
[0522ebe] | 243 | postvisit( type->instance ); |
---|
| 244 | } |
---|
| 245 | |
---|
[0bd3faf] | 246 | void GenType::postvisit( ast::TypeInstType const * type ) { |
---|
[8941b6b] | 247 | assertf( !options.genC, "TypeInstType should not reach code generation." ); |
---|
| 248 | result = type->name + " " + result; |
---|
| 249 | handleQualifiers( type ); |
---|
| 250 | } |
---|
| 251 | |
---|
[0bd3faf] | 252 | void GenType::postvisit( ast::TupleType const * type ) { |
---|
[8941b6b] | 253 | assertf( !options.genC, "TupleType should not reach code generation." ); |
---|
| 254 | unsigned int i = 0; |
---|
| 255 | std::ostringstream os; |
---|
| 256 | os << "["; |
---|
| 257 | for ( ast::ptr<ast::Type> const & t : type->types ) { |
---|
| 258 | i++; |
---|
| 259 | os << genType( t, "", options ) << (i == type->size() ? "" : ", "); |
---|
| 260 | } |
---|
| 261 | os << "] "; |
---|
| 262 | result = os.str() + result; |
---|
| 263 | } |
---|
| 264 | |
---|
[0bd3faf] | 265 | void GenType::postvisit( ast::VarArgsType const * type ) { |
---|
[8941b6b] | 266 | result = "__builtin_va_list " + result; |
---|
| 267 | handleQualifiers( type ); |
---|
| 268 | } |
---|
| 269 | |
---|
[0bd3faf] | 270 | void GenType::postvisit( ast::ZeroType const * type ) { |
---|
[8941b6b] | 271 | // Ideally these wouldn't hit codegen at all, but should be safe to make them ints. |
---|
| 272 | result = (options.pretty ? "zero_t " : "long int ") + result; |
---|
| 273 | handleQualifiers( type ); |
---|
| 274 | } |
---|
| 275 | |
---|
[0bd3faf] | 276 | void GenType::postvisit( ast::OneType const * type ) { |
---|
[8941b6b] | 277 | // Ideally these wouldn't hit codegen at all, but should be safe to make them ints. |
---|
| 278 | result = (options.pretty ? "one_t " : "long int ") + result; |
---|
| 279 | handleQualifiers( type ); |
---|
| 280 | } |
---|
| 281 | |
---|
[0bd3faf] | 282 | void GenType::postvisit( ast::GlobalScopeType const * type ) { |
---|
[8941b6b] | 283 | assertf( !options.genC, "GlobalScopeType should not reach code generation." ); |
---|
| 284 | handleQualifiers( type ); |
---|
| 285 | } |
---|
| 286 | |
---|
[0bd3faf] | 287 | void GenType::postvisit( ast::TraitInstType const * type ) { |
---|
[8941b6b] | 288 | assertf( !options.genC, "TraitInstType should not reach code generation." ); |
---|
| 289 | result = type->name + " " + result; |
---|
| 290 | handleQualifiers( type ); |
---|
| 291 | } |
---|
| 292 | |
---|
[0bd3faf] | 293 | void GenType::postvisit( ast::TypeofType const * type ) { |
---|
[8941b6b] | 294 | std::ostringstream os; |
---|
| 295 | os << "typeof("; |
---|
[116d7e2] | 296 | ast::Pass<CodeGenerator>::read( type->expr.get(), os, options ); |
---|
[8941b6b] | 297 | os << ") " << result; |
---|
| 298 | result = os.str(); |
---|
| 299 | handleQualifiers( type ); |
---|
| 300 | } |
---|
| 301 | |
---|
[0bd3faf] | 302 | void GenType::postvisit( ast::VTableType const * type ) { |
---|
[8941b6b] | 303 | assertf( !options.genC, "Virtual table types should not reach code generation." ); |
---|
| 304 | std::ostringstream os; |
---|
| 305 | os << "vtable(" << genType( type->base, "", options ) << ") " << result; |
---|
| 306 | result = os.str(); |
---|
| 307 | handleQualifiers( type ); |
---|
| 308 | } |
---|
| 309 | |
---|
[0bd3faf] | 310 | void GenType::postvisit( ast::QualifiedType const * type ) { |
---|
[8941b6b] | 311 | assertf( !options.genC, "QualifiedType should not reach code generation." ); |
---|
| 312 | std::ostringstream os; |
---|
| 313 | os << genType( type->parent, "", options ) << "." << genType( type->child, "", options ) << result; |
---|
| 314 | result = os.str(); |
---|
| 315 | handleQualifiers( type ); |
---|
| 316 | } |
---|
| 317 | |
---|
[0bd3faf] | 318 | void GenType::handleQualifiers( ast::Type const * type ) { |
---|
[8941b6b] | 319 | if ( type->is_const() ) { |
---|
| 320 | result = "const " + result; |
---|
| 321 | } |
---|
| 322 | if ( type->is_volatile() ) { |
---|
| 323 | result = "volatile " + result; |
---|
| 324 | } |
---|
| 325 | if ( type->is_restrict() ) { |
---|
| 326 | result = "__restrict " + result; |
---|
| 327 | } |
---|
| 328 | if ( type->is_atomic() ) { |
---|
| 329 | result = "_Atomic " + result; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[0bd3faf] | 333 | std::string GenType::genParamList( const ast::vector<ast::Type> & range ) { |
---|
[8941b6b] | 334 | auto cur = range.begin(); |
---|
| 335 | auto end = range.end(); |
---|
| 336 | if ( cur == end ) return ""; |
---|
| 337 | std::ostringstream oss; |
---|
[934fa0f] | 338 | UniqueName param( "__param_" ); |
---|
| 339 | while ( true ) { |
---|
| 340 | oss << genType( *cur++, options.genC ? param.newName() : "", options ); |
---|
[8941b6b] | 341 | if ( cur == end ) break; |
---|
| 342 | oss << ", "; |
---|
| 343 | } |
---|
| 344 | return oss.str(); |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | } // namespace |
---|
| 348 | |
---|
| 349 | std::string genType( ast::Type const * type, const std::string & base, const Options & options ) { |
---|
| 350 | std::ostringstream os; |
---|
| 351 | if ( !type->attributes.empty() ) { |
---|
[0bd3faf] | 352 | ast::Pass<CodeGenerator> cg( os, options ); |
---|
[8941b6b] | 353 | cg.core.genAttributes( type->attributes ); |
---|
| 354 | } |
---|
| 355 | |
---|
[0bd3faf] | 356 | return os.str() + ast::Pass<GenType>::read( type, base, options ); |
---|
[8941b6b] | 357 | } |
---|
| 358 | |
---|
| 359 | std::string genTypeNoAttr( ast::Type const * type, const std::string & base, const Options & options ) { |
---|
[0bd3faf] | 360 | return ast::Pass<GenType>::read( type, base, options ); |
---|
[8941b6b] | 361 | } |
---|
| 362 | |
---|
[51b7345] | 363 | } // namespace CodeGen |
---|
[51587aa] | 364 | |
---|
| 365 | // Local Variables: // |
---|
| 366 | // tab-width: 4 // |
---|
| 367 | // mode: c++ // |
---|
| 368 | // compile-command: "make install" // |
---|
| 369 | // End: // |
---|