[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 | // |
---|
[71f4e4f] | 7 | // CodeGenerator.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[1931bb01] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Jun 29 14:34:00 2022 |
---|
| 13 | // Update Count : 542 |
---|
[51587aa] | 14 | // |
---|
[3268a58] | 15 | #include "CodeGenerator.h" |
---|
[51587aa] | 16 | |
---|
[bf2438c] | 17 | #include <cassert> // for assert, assertf |
---|
| 18 | #include <list> // for _List_iterator, list, list<>::it... |
---|
[51b7345] | 19 | |
---|
[1931bb01] | 20 | #include "AST/Decl.hpp" // for DeclWithType |
---|
[bf2438c] | 21 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 22 | #include "Common/utility.h" // for CodeLocation, toString |
---|
| 23 | #include "GenType.h" // for genType |
---|
| 24 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 25 | #include "OperatorTable.h" // for OperatorInfo, operatorLookup |
---|
[07de76b] | 26 | #include "SynTree/LinkageSpec.h" // for Spec, Intrinsic |
---|
[bf2438c] | 27 | #include "SynTree/Attribute.h" // for Attribute |
---|
| 28 | #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode |
---|
| 29 | #include "SynTree/Constant.h" // for Constant |
---|
| 30 | #include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl |
---|
| 31 | #include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica... |
---|
| 32 | #include "SynTree/Initializer.h" // for Initializer, ListInit, Designation |
---|
| 33 | #include "SynTree/Label.h" // for Label, operator<< |
---|
| 34 | #include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt |
---|
| 35 | #include "SynTree/Type.h" // for Type, Type::StorageClasses, Func... |
---|
[10a7775] | 36 | |
---|
[51b7345] | 37 | using namespace std; |
---|
| 38 | |
---|
| 39 | namespace CodeGen { |
---|
[6c4ff37] | 40 | int CodeGenerator::tabsize = 4; |
---|
[51587aa] | 41 | |
---|
[60a8062] | 42 | // The kinds of statements that would ideally be followed by whitespace. |
---|
[2b6c1e0] | 43 | bool wantSpacing( Statement * stmt) { |
---|
| 44 | return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || |
---|
[3b0bc16] | 45 | dynamic_cast< WhileDoStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt ); |
---|
[2b6c1e0] | 46 | } |
---|
| 47 | |
---|
[8688ce1] | 48 | void CodeGenerator::extension( Expression * expr ) { |
---|
[8e9cbb2] | 49 | if ( expr->get_extension() ) { |
---|
| 50 | output << "__extension__ "; |
---|
| 51 | } // if |
---|
| 52 | } // extension |
---|
| 53 | |
---|
[8688ce1] | 54 | void CodeGenerator::extension( Declaration * decl ) { |
---|
[8e9cbb2] | 55 | if ( decl->get_extension() ) { |
---|
| 56 | output << "__extension__ "; |
---|
| 57 | } // if |
---|
| 58 | } // extension |
---|
| 59 | |
---|
[58dd019] | 60 | void CodeGenerator::asmName( DeclarationWithType * decl ) { |
---|
[e612146c] | 61 | if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) { |
---|
[58dd019] | 62 | output << " asm ( " << asmName->get_constant()->get_value() << " )"; |
---|
| 63 | } // if |
---|
| 64 | } // extension |
---|
| 65 | |
---|
[888cbe4] | 66 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { |
---|
| 67 | labels = &l; |
---|
| 68 | return *this; |
---|
| 69 | } |
---|
| 70 | |
---|
[8688ce1] | 71 | ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { |
---|
[888cbe4] | 72 | std::list< Label > & labs = *printLabels.labels; |
---|
| 73 | // l.unique(); // assumes a sorted list. Why not use set? Does order matter? |
---|
| 74 | for ( Label & l : labs ) { |
---|
| 75 | output << l.get_name() + ": "; |
---|
| 76 | printLabels.cg.genAttributes( l.get_attributes() ); |
---|
[8688ce1] | 77 | } // for |
---|
[888cbe4] | 78 | return output; |
---|
| 79 | } |
---|
| 80 | |
---|
[60a8062] | 81 | // Using updateLocation at the beginning of a node and endl within a node should become the method of formating. |
---|
[8bafacc] | 82 | void CodeGenerator::updateLocation( CodeLocation const & to ) { |
---|
[d22e90f] | 83 | // skip if linemarks shouldn't appear or if codelocation is unset |
---|
[42a36d9] | 84 | if ( !options.lineMarks || to.isUnset() ) return; |
---|
[d22e90f] | 85 | |
---|
| 86 | if ( currentLocation.followedBy( to, 0 ) ) { |
---|
[8bafacc] | 87 | return; |
---|
| 88 | } else if ( currentLocation.followedBy( to, 1 ) ) { |
---|
| 89 | output << "\n" << indent; |
---|
[d48e529] | 90 | currentLocation.first_line += 1; |
---|
[8bafacc] | 91 | } else if ( currentLocation.followedBy( to, 2 ) ) { |
---|
| 92 | output << "\n\n" << indent; |
---|
[d48e529] | 93 | currentLocation.first_line += 2; |
---|
[8bafacc] | 94 | } else { |
---|
[d48e529] | 95 | output << "\n# " << to.first_line << " \"" << to.filename |
---|
[60a8062] | 96 | << "\"\n" << indent; |
---|
[8bafacc] | 97 | currentLocation = to; |
---|
| 98 | } |
---|
| 99 | output << std::flush; |
---|
| 100 | } |
---|
[c850687] | 101 | |
---|
[8bafacc] | 102 | void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) { |
---|
| 103 | updateLocation( to->location ); |
---|
[c850687] | 104 | } |
---|
| 105 | |
---|
[d22e90f] | 106 | // replace endl |
---|
| 107 | ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const { |
---|
| 108 | // if ( !cg.lineMarks ) { |
---|
| 109 | // os << "\n" << cg.indent << std::flush; |
---|
| 110 | // } |
---|
| 111 | os << "\n" << std::flush; |
---|
| 112 | cg.currentLocation.first_line++; |
---|
| 113 | // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/"; |
---|
| 114 | return os; |
---|
[c850687] | 115 | } |
---|
| 116 | |
---|
[4e5e6cc] | 117 | CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options( pretty, genC, lineMarks, printExprTypes ), endl( *this ) {} |
---|
| 118 | CodeGenerator::CodeGenerator( std::ostream & os, const Options &options ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options(options), endl( *this ) {} |
---|
[cda48b6] | 119 | |
---|
[486341f] | 120 | string CodeGenerator::mangleName( DeclarationWithType * decl ) { |
---|
[13073be] | 121 | // GCC builtins should always be printed unmangled |
---|
[42a36d9] | 122 | if ( options.pretty || decl->linkage.is_gcc_builtin ) return decl->name; |
---|
[16ba4a6f] | 123 | if ( LinkageSpec::isMangled(decl->linkage) && decl->mangleName != "" ) { |
---|
[f326f99] | 124 | // need to incorporate scope level in order to differentiate names for destructors |
---|
| 125 | return decl->get_scopedMangleName(); |
---|
[51587aa] | 126 | } else { |
---|
[13073be] | 127 | return decl->name; |
---|
[51587aa] | 128 | } // if |
---|
| 129 | } |
---|
[94b4364] | 130 | |
---|
[44a81853] | 131 | void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { |
---|
[60a8062] | 132 | if ( attributes.empty() ) return; |
---|
[44a81853] | 133 | output << "__attribute__ (("; |
---|
| 134 | for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { |
---|
[85b2300] | 135 | output << (*attr)->name; |
---|
| 136 | if ( ! (*attr)->parameters.empty() ) { |
---|
[44a81853] | 137 | output << "("; |
---|
[85b2300] | 138 | genCommaList( (*attr)->parameters.begin(), (*attr)->parameters.end() ); |
---|
[44a81853] | 139 | output << ")"; |
---|
| 140 | } // if |
---|
[60a8062] | 141 | if ( ++attr == attributes.end() ) break; |
---|
[44a81853] | 142 | output << ","; // separator |
---|
| 143 | } // for |
---|
| 144 | output << ")) "; |
---|
| 145 | } // CodeGenerator::genAttributes |
---|
[7baed7d] | 146 | |
---|
[9857e8d] | 147 | // *** BaseSyntaxNode |
---|
[d22e90f] | 148 | void CodeGenerator::previsit( BaseSyntaxNode * node ) { |
---|
[9857e8d] | 149 | // turn off automatic recursion for all nodes, to allow each visitor to |
---|
| 150 | // precisely control the order in which its children are visited. |
---|
| 151 | visit_children = false; |
---|
[d22e90f] | 152 | updateLocation( node ); |
---|
[9857e8d] | 153 | } |
---|
| 154 | |
---|
| 155 | // *** BaseSyntaxNode |
---|
| 156 | void CodeGenerator::postvisit( BaseSyntaxNode * node ) { |
---|
| 157 | std::stringstream ss; |
---|
| 158 | node->print( ss ); |
---|
| 159 | assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() ); |
---|
| 160 | } |
---|
[7baed7d] | 161 | |
---|
[5f08961d] | 162 | // *** Expression |
---|
| 163 | void CodeGenerator::previsit( Expression * node ) { |
---|
| 164 | previsit( (BaseSyntaxNode *)node ); |
---|
| 165 | GuardAction( [this, node](){ |
---|
[60a8062] | 166 | if ( options.printExprTypes && node->result ) { |
---|
| 167 | output << " /* " << genType( node->result, "", options ) << " */ "; |
---|
| 168 | } |
---|
| 169 | } ); |
---|
[5f08961d] | 170 | } |
---|
| 171 | |
---|
[4810867] | 172 | // *** Declarations |
---|
[9857e8d] | 173 | void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { |
---|
[3ed994e] | 174 | // deleted decls should never be used, so don't print them |
---|
[42a36d9] | 175 | if ( functionDecl->isDeleted && options.genC ) return; |
---|
[8e9cbb2] | 176 | extension( functionDecl ); |
---|
[7baed7d] | 177 | genAttributes( functionDecl->get_attributes() ); |
---|
| 178 | |
---|
[51587aa] | 179 | handleStorageClass( functionDecl ); |
---|
[6e8bd43] | 180 | functionDecl->get_funcSpec().print( output ); |
---|
[dd020c0] | 181 | |
---|
[da09ba1] | 182 | Options subOptions = options; |
---|
[76f7fc7] | 183 | subOptions.anonymousUnused = functionDecl->has_body(); |
---|
[da09ba1] | 184 | output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), subOptions ); |
---|
[51587aa] | 185 | |
---|
[58dd019] | 186 | asmName( functionDecl ); |
---|
| 187 | |
---|
[51587aa] | 188 | if ( functionDecl->get_statements() ) { |
---|
[9857e8d] | 189 | functionDecl->get_statements()->accept( *visitor ); |
---|
[51587aa] | 190 | } // if |
---|
[3ed994e] | 191 | if ( functionDecl->isDeleted ) { |
---|
| 192 | output << " = void"; |
---|
| 193 | } |
---|
[51587aa] | 194 | } |
---|
| 195 | |
---|
[9857e8d] | 196 | void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { |
---|
[3ed994e] | 197 | // deleted decls should never be used, so don't print them |
---|
[42a36d9] | 198 | if ( objectDecl->isDeleted && options.genC ) return; |
---|
[de8d7fb1] | 199 | |
---|
| 200 | // gcc allows an empty declarator (no name) for bit-fields and C states: 6.7.2.1 Structure and union specifiers, |
---|
| 201 | // point 4, page 113: If the (bit field) value is zero, the declaration shall have no declarator. For anything |
---|
| 202 | // else, the anonymous name refers to the anonymous object for plan9 inheritance. |
---|
| 203 | if ( objectDecl->get_name().empty() && options.genC && ! objectDecl->get_bitfieldWidth() ) { |
---|
[e39241b] | 204 | // only generate an anonymous name when generating C code, otherwise it clutters the output too much |
---|
[d9c8a59] | 205 | static UniqueName name = { "__anonymous_object" }; |
---|
| 206 | objectDecl->set_name( name.newName() ); |
---|
[de8d7fb1] | 207 | // Stops unused parameter warnings. |
---|
| 208 | if ( options.anonymousUnused ) { |
---|
| 209 | objectDecl->attributes.push_back( new Attribute( "unused" ) ); |
---|
| 210 | } |
---|
[d9c8a59] | 211 | } |
---|
| 212 | |
---|
[8e9cbb2] | 213 | extension( objectDecl ); |
---|
[f9cebb5] | 214 | genAttributes( objectDecl->get_attributes() ); |
---|
| 215 | |
---|
[51587aa] | 216 | handleStorageClass( objectDecl ); |
---|
[42a36d9] | 217 | output << genType( objectDecl->get_type(), mangleName( objectDecl ), options.pretty, options.genC ); |
---|
[71f4e4f] | 218 | |
---|
[58dd019] | 219 | asmName( objectDecl ); |
---|
| 220 | |
---|
[51587aa] | 221 | if ( objectDecl->get_init() ) { |
---|
[6c4ff37] | 222 | output << " = "; |
---|
[9857e8d] | 223 | objectDecl->get_init()->accept( *visitor ); |
---|
[51587aa] | 224 | } // if |
---|
[3ed994e] | 225 | if ( objectDecl->isDeleted ) { |
---|
| 226 | output << " = void"; |
---|
| 227 | } |
---|
[3778cb2] | 228 | |
---|
[51587aa] | 229 | if ( objectDecl->get_bitfieldWidth() ) { |
---|
[6c4ff37] | 230 | output << ":"; |
---|
[9857e8d] | 231 | objectDecl->get_bitfieldWidth()->accept( *visitor ); |
---|
[51587aa] | 232 | } // if |
---|
| 233 | } |
---|
| 234 | |
---|
[5f642e38] | 235 | void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { |
---|
[42a36d9] | 236 | if( ! aggDecl->parameters.empty() && ! options.genC ) { |
---|
[e39241b] | 237 | // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); |
---|
| 238 | output << "forall("; |
---|
[0b3b2ae] | 239 | genCommaList( aggDecl->parameters.begin(), aggDecl->parameters.end() ); |
---|
[e39241b] | 240 | output << ")" << endl; |
---|
[e6cee92] | 241 | output << indent; |
---|
[e39241b] | 242 | } |
---|
| 243 | |
---|
[593370c] | 244 | output << kind; |
---|
[0b3b2ae] | 245 | genAttributes( aggDecl->attributes ); |
---|
| 246 | output << aggDecl->name; |
---|
[71f4e4f] | 247 | |
---|
[2c57025] | 248 | if ( aggDecl->has_body() ) { |
---|
[0b3b2ae] | 249 | std::list< Declaration * > & memb = aggDecl->members; |
---|
[94b4364] | 250 | output << " {" << endl; |
---|
[51587aa] | 251 | |
---|
[f7cb0bc] | 252 | ++indent; |
---|
[3778cb2] | 253 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { |
---|
[87e08e24] | 254 | output << indent; |
---|
[9857e8d] | 255 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 256 | output << ";" << endl; |
---|
[3778cb2] | 257 | } // for |
---|
[51587aa] | 258 | |
---|
[f7cb0bc] | 259 | --indent; |
---|
[51587aa] | 260 | |
---|
[cda48b6] | 261 | output << indent << "}"; |
---|
[17cd4eb] | 262 | } // if |
---|
[51587aa] | 263 | } |
---|
[17cd4eb] | 264 | |
---|
[9857e8d] | 265 | void CodeGenerator::postvisit( StructDecl * structDecl ) { |
---|
[8e9cbb2] | 266 | extension( structDecl ); |
---|
[5f642e38] | 267 | handleAggregate( structDecl, "struct " ); |
---|
[51587aa] | 268 | } |
---|
[17cd4eb] | 269 | |
---|
[9857e8d] | 270 | void CodeGenerator::postvisit( UnionDecl * unionDecl ) { |
---|
[8e9cbb2] | 271 | extension( unionDecl ); |
---|
[5f642e38] | 272 | handleAggregate( unionDecl, "union " ); |
---|
[51587aa] | 273 | } |
---|
[71f4e4f] | 274 | |
---|
[9857e8d] | 275 | void CodeGenerator::postvisit( EnumDecl * enumDecl ) { |
---|
[8e9cbb2] | 276 | extension( enumDecl ); |
---|
| 277 | std::list< Declaration* > &memb = enumDecl->get_members(); |
---|
[d8e2a09] | 278 | if (enumDecl->base && ! memb.empty()) { |
---|
[30d91e4] | 279 | unsigned long long last_val = -1; |
---|
[51587aa] | 280 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
[8688ce1] | 281 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
[51587aa] | 282 | assert( obj ); |
---|
[4390fb6] | 283 | output << "static const "; |
---|
| 284 | output << genType(enumDecl->base, "", options) << " "; |
---|
| 285 | output << mangleName( obj ) << " "; |
---|
| 286 | output << " = "; |
---|
[30d91e4] | 287 | output << "(" << genType(enumDecl->base, "", options) << ")"; |
---|
| 288 | if ( (BasicType *)(enumDecl->base) && ((BasicType *)(enumDecl->base))->isWholeNumber() ) { |
---|
| 289 | if ( obj->get_init() ) { |
---|
| 290 | obj->get_init()->accept( *visitor ); |
---|
| 291 | last_val = ((ConstantExpr *)(((SingleInit *)(obj->init))->value))->constant.get_ival(); |
---|
| 292 | } else { |
---|
| 293 | output << ++last_val; |
---|
| 294 | } // if |
---|
[4390fb6] | 295 | } else { |
---|
[30d91e4] | 296 | if ( obj->get_init() ) { |
---|
[b99fd56] | 297 | obj->get_init()->accept( *visitor ); |
---|
[4390fb6] | 298 | } else { |
---|
[30d91e4] | 299 | // Should not reach here! |
---|
[4390fb6] | 300 | } |
---|
[30d91e4] | 301 | } |
---|
[4390fb6] | 302 | output << ";" << endl; |
---|
[51587aa] | 303 | } // for |
---|
[4390fb6] | 304 | } else { |
---|
| 305 | output << "enum "; |
---|
| 306 | genAttributes( enumDecl->get_attributes() ); |
---|
| 307 | |
---|
| 308 | output << enumDecl->get_name(); |
---|
| 309 | |
---|
| 310 | if ( ! memb.empty() ) { |
---|
| 311 | output << " {" << endl; |
---|
| 312 | |
---|
| 313 | ++indent; |
---|
| 314 | for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { |
---|
| 315 | ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); |
---|
| 316 | assert( obj ); |
---|
| 317 | output << indent << mangleName( obj ); |
---|
| 318 | if ( obj->get_init() ) { |
---|
| 319 | output << " = "; |
---|
| 320 | obj->get_init()->accept( *visitor ); |
---|
| 321 | } // if |
---|
| 322 | output << "," << endl; |
---|
| 323 | } // for |
---|
[f7cb0bc] | 324 | --indent; |
---|
[cda48b6] | 325 | output << indent << "}"; |
---|
[4390fb6] | 326 | } // if |
---|
[51587aa] | 327 | } // if |
---|
| 328 | } |
---|
[71f4e4f] | 329 | |
---|
[9857e8d] | 330 | void CodeGenerator::postvisit( TraitDecl * traitDecl ) { |
---|
[42a36d9] | 331 | assertf( ! options.genC, "TraitDecls should not reach code generation." ); |
---|
[a984e65] | 332 | extension( traitDecl ); |
---|
| 333 | handleAggregate( traitDecl, "trait " ); |
---|
| 334 | } |
---|
[71f4e4f] | 335 | |
---|
[9857e8d] | 336 | void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { |
---|
[42a36d9] | 337 | assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." ); |
---|
[e39241b] | 338 | output << "typedef "; |
---|
[42a36d9] | 339 | output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl; |
---|
[51587aa] | 340 | } |
---|
[71f4e4f] | 341 | |
---|
[9857e8d] | 342 | void CodeGenerator::postvisit( TypeDecl * typeDecl ) { |
---|
[42a36d9] | 343 | assertf( ! options.genC, "TypeDecls should not reach code generation." ); |
---|
[ab4bff5] | 344 | output << typeDecl->genTypeString() << " " << typeDecl->name; |
---|
[f0ecf9b] | 345 | if ( typeDecl->sized ) { |
---|
[ab4bff5] | 346 | output << " | sized(" << typeDecl->name << ")"; |
---|
[a0c7dc36] | 347 | } |
---|
[ab4bff5] | 348 | if ( ! typeDecl->assertions.empty() ) { |
---|
[a0c7dc36] | 349 | output << " | { "; |
---|
[ab4bff5] | 350 | for ( DeclarationWithType * assert : typeDecl->assertions ) { |
---|
| 351 | assert->accept( *visitor ); |
---|
| 352 | output << "; "; |
---|
| 353 | } |
---|
[a0c7dc36] | 354 | output << " }"; |
---|
[e39241b] | 355 | } |
---|
[51587aa] | 356 | } |
---|
| 357 | |
---|
[92fea32] | 358 | void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) { |
---|
| 359 | output << "_Static_assert("; |
---|
| 360 | assertDecl->condition->accept( *visitor ); |
---|
| 361 | output << ", "; |
---|
| 362 | assertDecl->message->accept( *visitor ); |
---|
| 363 | output << ")"; |
---|
| 364 | } |
---|
| 365 | |
---|
[9857e8d] | 366 | void CodeGenerator::postvisit( Designation * designation ) { |
---|
[e4d829b] | 367 | std::list< Expression * > designators = designation->get_designators(); |
---|
[e45215c] | 368 | if ( designators.size() == 0 ) return; |
---|
[e4d829b] | 369 | for ( Expression * des : designators ) { |
---|
[62423350] | 370 | if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) { |
---|
| 371 | // if expression is a NameExpr or VariableExpr, then initializing aggregate member |
---|
| 372 | output << "."; |
---|
[9857e8d] | 373 | des->accept( *visitor ); |
---|
[e4d829b] | 374 | } else { |
---|
[3e54399] | 375 | // otherwise, it has to be a ConstantExpr or CastExpr, initializing array element |
---|
[62423350] | 376 | output << "["; |
---|
[9857e8d] | 377 | des->accept( *visitor ); |
---|
[62423350] | 378 | output << "]"; |
---|
[3778cb2] | 379 | } // if |
---|
| 380 | } // for |
---|
[3eb1653] | 381 | output << " = "; |
---|
[e45215c] | 382 | } |
---|
| 383 | |
---|
[9857e8d] | 384 | void CodeGenerator::postvisit( SingleInit * init ) { |
---|
| 385 | init->get_value()->accept( *visitor ); |
---|
[51587aa] | 386 | } |
---|
| 387 | |
---|
[9857e8d] | 388 | void CodeGenerator::postvisit( ListInit * init ) { |
---|
[e4d829b] | 389 | auto initBegin = init->begin(); |
---|
| 390 | auto initEnd = init->end(); |
---|
| 391 | auto desigBegin = init->get_designations().begin(); |
---|
| 392 | auto desigEnd = init->get_designations().end(); |
---|
| 393 | |
---|
[6c4ff37] | 394 | output << "{ "; |
---|
[e4d829b] | 395 | for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { |
---|
[9857e8d] | 396 | (*desigBegin)->accept( *visitor ); |
---|
| 397 | (*initBegin)->accept( *visitor ); |
---|
[e4d829b] | 398 | ++initBegin, ++desigBegin; |
---|
| 399 | if ( initBegin != initEnd ) { |
---|
| 400 | output << ", "; |
---|
| 401 | } |
---|
| 402 | } |
---|
[6c4ff37] | 403 | output << " }"; |
---|
[e4d829b] | 404 | assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() ); |
---|
[51587aa] | 405 | } |
---|
| 406 | |
---|
[d22e90f] | 407 | void CodeGenerator::postvisit( ConstructorInit * init ){ |
---|
[42a36d9] | 408 | assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." ); |
---|
[e6cee92] | 409 | // pseudo-output for constructor/destructor pairs |
---|
[d22e90f] | 410 | output << "<ctorinit>{" << endl << ++indent << "ctor: "; |
---|
[9857e8d] | 411 | maybeAccept( init->get_ctor(), *visitor ); |
---|
[d22e90f] | 412 | output << ", " << endl << indent << "dtor: "; |
---|
[9857e8d] | 413 | maybeAccept( init->get_dtor(), *visitor ); |
---|
[d22e90f] | 414 | output << endl << --indent << "}"; |
---|
[fc638d2] | 415 | } |
---|
| 416 | |
---|
[9857e8d] | 417 | void CodeGenerator::postvisit( Constant * constant ) { |
---|
[d7312ac] | 418 | output << constant->get_value(); |
---|
[51587aa] | 419 | } |
---|
| 420 | |
---|
[4810867] | 421 | // *** Expressions |
---|
[9857e8d] | 422 | void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { |
---|
[e04ef3a] | 423 | extension( applicationExpr ); |
---|
[8688ce1] | 424 | if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { |
---|
[60a8062] | 425 | const OperatorInfo * opInfo; |
---|
| 426 | if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) { |
---|
[51587aa] | 427 | std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); |
---|
[60a8062] | 428 | switch ( opInfo->type ) { |
---|
[51587aa] | 429 | case OT_INDEX: |
---|
| 430 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[9857e8d] | 431 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 432 | output << "["; |
---|
[9857e8d] | 433 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 434 | output << "]"; |
---|
[51587aa] | 435 | break; |
---|
[71f4e4f] | 436 | |
---|
[51587aa] | 437 | case OT_CALL: |
---|
[356189a] | 438 | // there are no intrinsic definitions of the function call operator |
---|
[51587aa] | 439 | assert( false ); |
---|
| 440 | break; |
---|
[71f4e4f] | 441 | |
---|
[f1e012b] | 442 | case OT_CTOR: |
---|
[c2ce2350] | 443 | case OT_DTOR: |
---|
[356189a] | 444 | if ( applicationExpr->get_args().size() == 1 ) { |
---|
[8e9cbb2] | 445 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 446 | // effects, so must still output this expression |
---|
[64071c2] | 447 | output << "("; |
---|
[9857e8d] | 448 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 449 | output << ") /* " << opInfo->inputName << " */"; |
---|
[356189a] | 450 | } else if ( applicationExpr->get_args().size() == 2 ) { |
---|
[c2ce2350] | 451 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
[356189a] | 452 | output << "("; |
---|
[9857e8d] | 453 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 454 | output << opInfo->symbol; |
---|
[9857e8d] | 455 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 456 | output << ") /* " << opInfo->inputName << " */"; |
---|
[356189a] | 457 | } else { |
---|
[c2ce2350] | 458 | // no constructors with 0 or more than 2 parameters |
---|
[356189a] | 459 | assert( false ); |
---|
[8688ce1] | 460 | } // if |
---|
[356189a] | 461 | break; |
---|
[f1e012b] | 462 | |
---|
[51587aa] | 463 | case OT_PREFIX: |
---|
| 464 | case OT_PREFIXASSIGN: |
---|
| 465 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[6c4ff37] | 466 | output << "("; |
---|
[60a8062] | 467 | output << opInfo->symbol; |
---|
[9857e8d] | 468 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 469 | output << ")"; |
---|
[51587aa] | 470 | break; |
---|
[71f4e4f] | 471 | |
---|
[51587aa] | 472 | case OT_POSTFIX: |
---|
| 473 | case OT_POSTFIXASSIGN: |
---|
| 474 | assert( applicationExpr->get_args().size() == 1 ); |
---|
[9857e8d] | 475 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 476 | output << opInfo->symbol; |
---|
[51587aa] | 477 | break; |
---|
| 478 | |
---|
[f1e012b] | 479 | |
---|
[51587aa] | 480 | case OT_INFIX: |
---|
| 481 | case OT_INFIXASSIGN: |
---|
| 482 | assert( applicationExpr->get_args().size() == 2 ); |
---|
[6c4ff37] | 483 | output << "("; |
---|
[9857e8d] | 484 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 485 | output << opInfo->symbol; |
---|
[9857e8d] | 486 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 487 | output << ")"; |
---|
[51587aa] | 488 | break; |
---|
[71f4e4f] | 489 | |
---|
[51587aa] | 490 | case OT_CONSTANT: |
---|
[721f17a] | 491 | case OT_LABELADDRESS: |
---|
| 492 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
[51587aa] | 493 | assert( false ); |
---|
[3778cb2] | 494 | } // switch |
---|
[17cd4eb] | 495 | } else { |
---|
[9857e8d] | 496 | varExpr->accept( *visitor ); |
---|
[6c4ff37] | 497 | output << "("; |
---|
[51587aa] | 498 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 499 | output << ")"; |
---|
[17cd4eb] | 500 | } // if |
---|
[51587aa] | 501 | } else { |
---|
[9857e8d] | 502 | applicationExpr->get_function()->accept( *visitor ); |
---|
[6c4ff37] | 503 | output << "("; |
---|
[51587aa] | 504 | genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); |
---|
[6c4ff37] | 505 | output << ")"; |
---|
[51587aa] | 506 | } // if |
---|
| 507 | } |
---|
[71f4e4f] | 508 | |
---|
[9857e8d] | 509 | void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { |
---|
[e04ef3a] | 510 | extension( untypedExpr ); |
---|
[22bc276] | 511 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) { |
---|
[60a8062] | 512 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); |
---|
| 513 | if ( opInfo ) { |
---|
[22bc276] | 514 | std::list< Expression* >::iterator arg = untypedExpr->args.begin(); |
---|
[60a8062] | 515 | switch ( opInfo->type ) { |
---|
[51587aa] | 516 | case OT_INDEX: |
---|
[22bc276] | 517 | assert( untypedExpr->args.size() == 2 ); |
---|
[9857e8d] | 518 | (*arg++)->accept( *visitor ); |
---|
[6c4ff37] | 519 | output << "["; |
---|
[9857e8d] | 520 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 521 | output << "]"; |
---|
[51587aa] | 522 | break; |
---|
[71f4e4f] | 523 | |
---|
[51587aa] | 524 | case OT_CALL: |
---|
[f1e012b] | 525 | assert( false ); |
---|
| 526 | |
---|
[c2ce2350] | 527 | case OT_CTOR: |
---|
| 528 | case OT_DTOR: |
---|
[22bc276] | 529 | if ( untypedExpr->args.size() == 1 ) { |
---|
[8e9cbb2] | 530 | // the expression fed into a single parameter constructor or destructor may contain side |
---|
| 531 | // effects, so must still output this expression |
---|
[64071c2] | 532 | output << "("; |
---|
[9857e8d] | 533 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 534 | output << ") /* " << opInfo->inputName << " */"; |
---|
[c2ce2350] | 535 | } else if ( untypedExpr->get_args().size() == 2 ) { |
---|
| 536 | // intrinsic two parameter constructors are essentially bitwise assignment |
---|
| 537 | output << "("; |
---|
[9857e8d] | 538 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 539 | output << opInfo->symbol; |
---|
[9857e8d] | 540 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 541 | output << ") /* " << opInfo->inputName << " */"; |
---|
[c2ce2350] | 542 | } else { |
---|
| 543 | // no constructors with 0 or more than 2 parameters |
---|
[42a36d9] | 544 | assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); |
---|
[4e8949f] | 545 | output << "("; |
---|
| 546 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 547 | output << opInfo->symbol << "{ "; |
---|
[22bc276] | 548 | genCommaList( arg, untypedExpr->args.end() ); |
---|
[60a8062] | 549 | output << "}) /* " << opInfo->inputName << " */"; |
---|
[3778cb2] | 550 | } // if |
---|
[51587aa] | 551 | break; |
---|
[71f4e4f] | 552 | |
---|
[51587aa] | 553 | case OT_PREFIX: |
---|
| 554 | case OT_PREFIXASSIGN: |
---|
[de62360d] | 555 | case OT_LABELADDRESS: |
---|
[22bc276] | 556 | assert( untypedExpr->args.size() == 1 ); |
---|
[6c4ff37] | 557 | output << "("; |
---|
[60a8062] | 558 | output << opInfo->symbol; |
---|
[9857e8d] | 559 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 560 | output << ")"; |
---|
[51587aa] | 561 | break; |
---|
[71f4e4f] | 562 | |
---|
[51587aa] | 563 | case OT_POSTFIX: |
---|
| 564 | case OT_POSTFIXASSIGN: |
---|
[22bc276] | 565 | assert( untypedExpr->args.size() == 1 ); |
---|
[9857e8d] | 566 | (*arg)->accept( *visitor ); |
---|
[60a8062] | 567 | output << opInfo->symbol; |
---|
[51587aa] | 568 | break; |
---|
[71f4e4f] | 569 | |
---|
[51587aa] | 570 | case OT_INFIX: |
---|
| 571 | case OT_INFIXASSIGN: |
---|
[22bc276] | 572 | assert( untypedExpr->args.size() == 2 ); |
---|
[6c4ff37] | 573 | output << "("; |
---|
[9857e8d] | 574 | (*arg++)->accept( *visitor ); |
---|
[60a8062] | 575 | output << opInfo->symbol; |
---|
[9857e8d] | 576 | (*arg)->accept( *visitor ); |
---|
[6c4ff37] | 577 | output << ")"; |
---|
[51587aa] | 578 | break; |
---|
[71f4e4f] | 579 | |
---|
[51587aa] | 580 | case OT_CONSTANT: |
---|
| 581 | // there are no intrinsic definitions of 0 or 1 as functions |
---|
| 582 | assert( false ); |
---|
[3778cb2] | 583 | } // switch |
---|
[51587aa] | 584 | } else { |
---|
[22bc276] | 585 | // builtin routines |
---|
| 586 | nameExpr->accept( *visitor ); |
---|
| 587 | output << "("; |
---|
| 588 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
| 589 | output << ")"; |
---|
[51587aa] | 590 | } // if |
---|
| 591 | } else { |
---|
[22bc276] | 592 | untypedExpr->function->accept( *visitor ); |
---|
[6c4ff37] | 593 | output << "("; |
---|
[22bc276] | 594 | genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() ); |
---|
[6c4ff37] | 595 | output << ")"; |
---|
[51587aa] | 596 | } // if |
---|
| 597 | } |
---|
[71f4e4f] | 598 | |
---|
[9857e8d] | 599 | void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { |
---|
[22bc276] | 600 | rangeExpr->low->accept( *visitor ); |
---|
[064e3ff] | 601 | output << " ... "; |
---|
[22bc276] | 602 | rangeExpr->high->accept( *visitor ); |
---|
[064e3ff] | 603 | } |
---|
| 604 | |
---|
[9857e8d] | 605 | void CodeGenerator::postvisit( NameExpr * nameExpr ) { |
---|
[e04ef3a] | 606 | extension( nameExpr ); |
---|
[60a8062] | 607 | const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); |
---|
| 608 | if ( opInfo ) { |
---|
| 609 | if ( opInfo->type == OT_CONSTANT ) { |
---|
| 610 | output << opInfo->symbol; |
---|
[5ea7a22] | 611 | } else { |
---|
[60a8062] | 612 | output << opInfo->outputName; |
---|
[5ea7a22] | 613 | } |
---|
[51587aa] | 614 | } else { |
---|
[6c4ff37] | 615 | output << nameExpr->get_name(); |
---|
[51587aa] | 616 | } // if |
---|
| 617 | } |
---|
[71f4e4f] | 618 | |
---|
[6e50a6b] | 619 | void CodeGenerator::postvisit( DimensionExpr * dimensionExpr ) { |
---|
| 620 | extension( dimensionExpr ); |
---|
| 621 | output << "/*non-type*/" << dimensionExpr->get_name(); |
---|
| 622 | } |
---|
| 623 | |
---|
[9857e8d] | 624 | void CodeGenerator::postvisit( AddressExpr * addressExpr ) { |
---|
[e04ef3a] | 625 | extension( addressExpr ); |
---|
[6c4ff37] | 626 | output << "(&"; |
---|
[9857e8d] | 627 | addressExpr->arg->accept( *visitor ); |
---|
[6c4ff37] | 628 | output << ")"; |
---|
[51587aa] | 629 | } |
---|
| 630 | |
---|
[9857e8d] | 631 | void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { |
---|
[5809461] | 632 | extension( addressExpr ); |
---|
| 633 | output << "(&&" << addressExpr->arg << ")"; |
---|
| 634 | } |
---|
| 635 | |
---|
[9857e8d] | 636 | void CodeGenerator::postvisit( CastExpr * castExpr ) { |
---|
[e04ef3a] | 637 | extension( castExpr ); |
---|
[803deb1] | 638 | output << "("; |
---|
[906e24d] | 639 | if ( castExpr->get_result()->isVoid() ) { |
---|
[d7312ac] | 640 | output << "(void)"; |
---|
[d104b02] | 641 | } else { |
---|
| 642 | // at least one result type of cast. |
---|
| 643 | // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write |
---|
| 644 | // an lvalue cast, this has been taken out. |
---|
[803deb1] | 645 | output << "("; |
---|
[42a36d9] | 646 | output << genType( castExpr->get_result(), "", options ); |
---|
[71f4e4f] | 647 | output << ")"; |
---|
[3778cb2] | 648 | } // if |
---|
[da9d79b] | 649 | castExpr->arg->accept( *visitor ); |
---|
| 650 | output << ")"; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) { |
---|
[42a36d9] | 654 | assertf( ! options.genC, "KeywordCast should not reach code generation." ); |
---|
[da9d79b] | 655 | extension( castExpr ); |
---|
| 656 | output << "((" << castExpr->targetString() << " &)"; |
---|
| 657 | castExpr->arg->accept( *visitor ); |
---|
[803deb1] | 658 | output << ")"; |
---|
[51587aa] | 659 | } |
---|
[71f4e4f] | 660 | |
---|
[9857e8d] | 661 | void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { |
---|
[42a36d9] | 662 | assertf( ! options.genC, "VirtualCastExpr should not reach code generation." ); |
---|
[a5f0529] | 663 | extension( castExpr ); |
---|
| 664 | output << "(virtual "; |
---|
[9857e8d] | 665 | castExpr->get_arg()->accept( *visitor ); |
---|
[a5f0529] | 666 | output << ")"; |
---|
| 667 | } |
---|
| 668 | |
---|
[9857e8d] | 669 | void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { |
---|
[42a36d9] | 670 | assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." ); |
---|
[e39241b] | 671 | extension( memberExpr ); |
---|
[9857e8d] | 672 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[5f642e38] | 673 | output << "."; |
---|
[9857e8d] | 674 | memberExpr->get_member()->accept( *visitor ); |
---|
[51587aa] | 675 | } |
---|
[71f4e4f] | 676 | |
---|
[9857e8d] | 677 | void CodeGenerator::postvisit( MemberExpr * memberExpr ) { |
---|
[e04ef3a] | 678 | extension( memberExpr ); |
---|
[9857e8d] | 679 | memberExpr->get_aggregate()->accept( *visitor ); |
---|
[6c4ff37] | 680 | output << "." << mangleName( memberExpr->get_member() ); |
---|
[51587aa] | 681 | } |
---|
[71f4e4f] | 682 | |
---|
[9857e8d] | 683 | void CodeGenerator::postvisit( VariableExpr * variableExpr ) { |
---|
[e04ef3a] | 684 | extension( variableExpr ); |
---|
[60a8062] | 685 | const OperatorInfo * opInfo; |
---|
[b99fd56] | 686 | if( dynamic_cast<ZeroType*>( variableExpr->get_var()->get_type() ) ) { |
---|
| 687 | output << "0"; |
---|
| 688 | } else if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) { |
---|
[60a8062] | 689 | output << opInfo->symbol; |
---|
[51587aa] | 690 | } else { |
---|
[b99fd56] | 691 | // if (dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type()) |
---|
[4390fb6] | 692 | // && dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base) { |
---|
| 693 | // output << '(' <<genType(dynamic_cast<EnumInstType *>(variableExpr->get_var()->get_type())->baseEnum->base, "", options) << ')'; |
---|
| 694 | // } |
---|
[6c4ff37] | 695 | output << mangleName( variableExpr->get_var() ); |
---|
[51587aa] | 696 | } // if |
---|
| 697 | } |
---|
[71f4e4f] | 698 | |
---|
[9857e8d] | 699 | void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { |
---|
[51587aa] | 700 | assert( constantExpr->get_constant() ); |
---|
[e04ef3a] | 701 | extension( constantExpr ); |
---|
[9857e8d] | 702 | constantExpr->get_constant()->accept( *visitor ); |
---|
[51587aa] | 703 | } |
---|
[71f4e4f] | 704 | |
---|
[9857e8d] | 705 | void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { |
---|
[e04ef3a] | 706 | extension( sizeofExpr ); |
---|
[6c4ff37] | 707 | output << "sizeof("; |
---|
[51587aa] | 708 | if ( sizeofExpr->get_isType() ) { |
---|
[42a36d9] | 709 | output << genType( sizeofExpr->get_type(), "", options ); |
---|
[51587aa] | 710 | } else { |
---|
[9857e8d] | 711 | sizeofExpr->get_expr()->accept( *visitor ); |
---|
[51587aa] | 712 | } // if |
---|
[6c4ff37] | 713 | output << ")"; |
---|
[51587aa] | 714 | } |
---|
[47534159] | 715 | |
---|
[9857e8d] | 716 | void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { |
---|
[47534159] | 717 | // use GCC extension to avoid bumping std to C11 |
---|
[8e9cbb2] | 718 | extension( alignofExpr ); |
---|
[47534159] | 719 | output << "__alignof__("; |
---|
[25a054f] | 720 | if ( alignofExpr->get_isType() ) { |
---|
[42a36d9] | 721 | output << genType( alignofExpr->get_type(), "", options ); |
---|
[47534159] | 722 | } else { |
---|
[9857e8d] | 723 | alignofExpr->get_expr()->accept( *visitor ); |
---|
[47534159] | 724 | } // if |
---|
| 725 | output << ")"; |
---|
| 726 | } |
---|
[71f4e4f] | 727 | |
---|
[9857e8d] | 728 | void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { |
---|
[42a36d9] | 729 | assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." ); |
---|
[e39241b] | 730 | output << "offsetof("; |
---|
[42a36d9] | 731 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
[e39241b] | 732 | output << ", " << offsetofExpr->get_member(); |
---|
| 733 | output << ")"; |
---|
[2a4b088] | 734 | } |
---|
| 735 | |
---|
[9857e8d] | 736 | void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { |
---|
[25a054f] | 737 | // use GCC builtin |
---|
| 738 | output << "__builtin_offsetof("; |
---|
[42a36d9] | 739 | output << genType( offsetofExpr->get_type(), "", options ); |
---|
[e551c69] | 740 | output << ", " << mangleName( offsetofExpr->get_member() ); |
---|
[25a054f] | 741 | output << ")"; |
---|
| 742 | } |
---|
[d63eeb0] | 743 | |
---|
[9857e8d] | 744 | void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { |
---|
[42a36d9] | 745 | assertf( ! options.genC, "OffsetPackExpr should not reach code generation." ); |
---|
| 746 | output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")"; |
---|
[afc1045] | 747 | } |
---|
[70a06f6] | 748 | |
---|
[9857e8d] | 749 | void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { |
---|
[e04ef3a] | 750 | extension( logicalExpr ); |
---|
[6c4ff37] | 751 | output << "("; |
---|
[9857e8d] | 752 | logicalExpr->get_arg1()->accept( *visitor ); |
---|
[51587aa] | 753 | if ( logicalExpr->get_isAnd() ) { |
---|
[6c4ff37] | 754 | output << " && "; |
---|
[51587aa] | 755 | } else { |
---|
[6c4ff37] | 756 | output << " || "; |
---|
[51587aa] | 757 | } // if |
---|
[9857e8d] | 758 | logicalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 759 | output << ")"; |
---|
[51587aa] | 760 | } |
---|
[71f4e4f] | 761 | |
---|
[9857e8d] | 762 | void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { |
---|
[e04ef3a] | 763 | extension( conditionalExpr ); |
---|
[6c4ff37] | 764 | output << "("; |
---|
[9857e8d] | 765 | conditionalExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 766 | output << " ? "; |
---|
[9857e8d] | 767 | conditionalExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 768 | output << " : "; |
---|
[9857e8d] | 769 | conditionalExpr->get_arg3()->accept( *visitor ); |
---|
[6c4ff37] | 770 | output << ")"; |
---|
[51587aa] | 771 | } |
---|
[71f4e4f] | 772 | |
---|
[9857e8d] | 773 | void CodeGenerator::postvisit( CommaExpr * commaExpr ) { |
---|
[e04ef3a] | 774 | extension( commaExpr ); |
---|
[6c4ff37] | 775 | output << "("; |
---|
[42a36d9] | 776 | if ( options.genC ) { |
---|
[8a6cf7e] | 777 | // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings. |
---|
| 778 | commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); |
---|
| 779 | } |
---|
[9857e8d] | 780 | commaExpr->get_arg1()->accept( *visitor ); |
---|
[6c4ff37] | 781 | output << " , "; |
---|
[9857e8d] | 782 | commaExpr->get_arg2()->accept( *visitor ); |
---|
[6c4ff37] | 783 | output << ")"; |
---|
[51587aa] | 784 | } |
---|
[71f4e4f] | 785 | |
---|
[9857e8d] | 786 | void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { |
---|
[42a36d9] | 787 | assertf( ! options.genC, "TupleAssignExpr should not reach code generation." ); |
---|
[9857e8d] | 788 | tupleExpr->stmtExpr->accept( *visitor ); |
---|
[d104b02] | 789 | } |
---|
| 790 | |
---|
[9857e8d] | 791 | void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { |
---|
[42a36d9] | 792 | assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." ); |
---|
[f975c65] | 793 | extension( tupleExpr ); |
---|
[e39241b] | 794 | output << "["; |
---|
| 795 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 796 | output << "]"; |
---|
| 797 | } |
---|
[907eccb] | 798 | |
---|
[9857e8d] | 799 | void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { |
---|
[42a36d9] | 800 | assertf( ! options.genC, "TupleExpr should not reach code generation." ); |
---|
[f975c65] | 801 | extension( tupleExpr ); |
---|
[e39241b] | 802 | output << "["; |
---|
| 803 | genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); |
---|
| 804 | output << "]"; |
---|
| 805 | } |
---|
[71f4e4f] | 806 | |
---|
[9857e8d] | 807 | void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { |
---|
[42a36d9] | 808 | assertf( ! options.genC, "TupleIndexExpr should not reach code generation." ); |
---|
[f975c65] | 809 | extension( tupleExpr ); |
---|
[9857e8d] | 810 | tupleExpr->get_tuple()->accept( *visitor ); |
---|
[f975c65] | 811 | output << "." << tupleExpr->get_index(); |
---|
| 812 | } |
---|
| 813 | |
---|
[9857e8d] | 814 | void CodeGenerator::postvisit( TypeExpr * typeExpr ) { |
---|
[42a36d9] | 815 | // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; |
---|
| 816 | // assertf( ! options.genC, "TypeExpr should not reach code generation." ); |
---|
| 817 | if ( ! options.genC ) { |
---|
| 818 | output << genType( typeExpr->get_type(), "", options ); |
---|
[e4d829b] | 819 | } |
---|
[e39241b] | 820 | } |
---|
[2b6c1e0] | 821 | |
---|
[9857e8d] | 822 | void CodeGenerator::postvisit( AsmExpr * asmExpr ) { |
---|
[665f432] | 823 | if ( !asmExpr->inout.empty() ) { |
---|
[7f5566b] | 824 | output << "[ "; |
---|
[665f432] | 825 | output << asmExpr->inout; |
---|
[7f5566b] | 826 | output << " ] "; |
---|
| 827 | } // if |
---|
[665f432] | 828 | asmExpr->constraint->accept( *visitor ); |
---|
[7f5566b] | 829 | output << " ( "; |
---|
[665f432] | 830 | asmExpr->operand->accept( *visitor ); |
---|
[7f5566b] | 831 | output << " )"; |
---|
| 832 | } |
---|
| 833 | |
---|
[9857e8d] | 834 | void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { |
---|
[fbcde64] | 835 | assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); |
---|
[42a36d9] | 836 | output << "(" << genType( compLitExpr->get_result(), "", options ) << ")"; |
---|
[9857e8d] | 837 | compLitExpr->get_initializer()->accept( *visitor ); |
---|
[3c13c03] | 838 | } |
---|
| 839 | |
---|
[9857e8d] | 840 | void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { |
---|
[42a36d9] | 841 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
[d104b02] | 842 | output << "unq<" << unqExpr->get_id() << ">{ "; |
---|
[9857e8d] | 843 | unqExpr->get_expr()->accept( *visitor ); |
---|
[d104b02] | 844 | output << " }"; |
---|
| 845 | } |
---|
| 846 | |
---|
[9857e8d] | 847 | void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { |
---|
[44b4114] | 848 | std::list< Statement * > & stmts = stmtExpr->statements->kids; |
---|
[d22e90f] | 849 | output << "({" << endl; |
---|
[f7cb0bc] | 850 | ++indent; |
---|
[3c13c03] | 851 | unsigned int numStmts = stmts.size(); |
---|
| 852 | unsigned int i = 0; |
---|
| 853 | for ( Statement * stmt : stmts ) { |
---|
[058f549] | 854 | output << indent << printLabels( stmt->get_labels() ); |
---|
[3c13c03] | 855 | if ( i+1 == numStmts ) { |
---|
| 856 | // last statement in a statement expression needs to be handled specially - |
---|
| 857 | // cannot cast to void, otherwise the expression statement has no value |
---|
| 858 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { |
---|
[44b4114] | 859 | exprStmt->expr->accept( *visitor ); |
---|
[3c13c03] | 860 | output << ";" << endl; |
---|
| 861 | ++i; |
---|
| 862 | break; |
---|
| 863 | } |
---|
| 864 | } |
---|
[9857e8d] | 865 | stmt->accept( *visitor ); |
---|
[3c13c03] | 866 | output << endl; |
---|
| 867 | if ( wantSpacing( stmt ) ) { |
---|
| 868 | output << endl; |
---|
| 869 | } // if |
---|
| 870 | ++i; |
---|
| 871 | } |
---|
[f7cb0bc] | 872 | --indent; |
---|
[3c13c03] | 873 | output << indent << "})"; |
---|
[6eb8948] | 874 | } |
---|
| 875 | |
---|
[4e8949f] | 876 | void CodeGenerator::postvisit( ConstructorExpr * expr ) { |
---|
[42a36d9] | 877 | assertf( ! options.genC, "Unique expressions should not reach code generation." ); |
---|
[4e8949f] | 878 | expr->callExpr->accept( *visitor ); |
---|
| 879 | } |
---|
| 880 | |
---|
[44b4114] | 881 | void CodeGenerator::postvisit( DeletedExpr * expr ) { |
---|
[42a36d9] | 882 | assertf( ! options.genC, "Deleted expressions should not reach code generation." ); |
---|
[44b4114] | 883 | expr->expr->accept( *visitor ); |
---|
| 884 | } |
---|
| 885 | |
---|
[0f79853] | 886 | void CodeGenerator::postvisit( DefaultArgExpr * arg ) { |
---|
[42a36d9] | 887 | assertf( ! options.genC, "Default argument expressions should not reach code generation." ); |
---|
[0f79853] | 888 | arg->expr->accept( *visitor ); |
---|
| 889 | } |
---|
| 890 | |
---|
[d807ca28] | 891 | void CodeGenerator::postvisit( GenericExpr * expr ) { |
---|
[42a36d9] | 892 | assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." ); |
---|
[d807ca28] | 893 | output << "_Generic("; |
---|
| 894 | expr->control->accept( *visitor ); |
---|
| 895 | output << ", "; |
---|
| 896 | unsigned int numAssocs = expr->associations.size(); |
---|
| 897 | unsigned int i = 0; |
---|
| 898 | for ( GenericExpr::Association & assoc : expr->associations ) { |
---|
| 899 | if (assoc.isDefault) { |
---|
| 900 | output << "default: "; |
---|
| 901 | } else { |
---|
[42a36d9] | 902 | output << genType( assoc.type, "", options ) << ": "; |
---|
[d807ca28] | 903 | } |
---|
| 904 | assoc.expr->accept( *visitor ); |
---|
| 905 | if ( i+1 != numAssocs ) { |
---|
| 906 | output << ", "; |
---|
| 907 | } |
---|
| 908 | i++; |
---|
| 909 | } |
---|
| 910 | output << ")"; |
---|
| 911 | } |
---|
| 912 | |
---|
| 913 | |
---|
[4810867] | 914 | // *** Statements |
---|
[9857e8d] | 915 | void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { |
---|
[51587aa] | 916 | std::list<Statement*> ks = compoundStmt->get_kids(); |
---|
[2b6c1e0] | 917 | output << "{" << endl; |
---|
[51587aa] | 918 | |
---|
[f7cb0bc] | 919 | ++indent; |
---|
[51587aa] | 920 | |
---|
[7f5566b] | 921 | for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { |
---|
[cda48b6] | 922 | output << indent << printLabels( (*i)->get_labels() ); |
---|
[9857e8d] | 923 | (*i)->accept( *visitor ); |
---|
[2b6c1e0] | 924 | |
---|
[6c4ff37] | 925 | output << endl; |
---|
[2b6c1e0] | 926 | if ( wantSpacing( *i ) ) { |
---|
| 927 | output << endl; |
---|
[3778cb2] | 928 | } // if |
---|
[8688ce1] | 929 | } // for |
---|
[f7cb0bc] | 930 | --indent; |
---|
[51587aa] | 931 | |
---|
[cda48b6] | 932 | output << indent << "}"; |
---|
[51587aa] | 933 | } |
---|
| 934 | |
---|
[9857e8d] | 935 | void CodeGenerator::postvisit( ExprStmt * exprStmt ) { |
---|
[6c4ff37] | 936 | assert( exprStmt ); |
---|
[42a36d9] | 937 | if ( options.genC ) { |
---|
[262f085f] | 938 | // cast the top-level expression to void to reduce gcc warnings. |
---|
[8a6cf7e] | 939 | exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); |
---|
[262f085f] | 940 | } |
---|
[9857e8d] | 941 | exprStmt->get_expr()->accept( *visitor ); |
---|
[3eb1653] | 942 | output << ";"; |
---|
[51587aa] | 943 | } |
---|
| 944 | |
---|
[9857e8d] | 945 | void CodeGenerator::postvisit( AsmStmt * asmStmt ) { |
---|
[7f5566b] | 946 | output << "asm "; |
---|
| 947 | if ( asmStmt->get_voltile() ) output << "volatile "; |
---|
| 948 | if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; |
---|
| 949 | output << "( "; |
---|
[9857e8d] | 950 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[7f5566b] | 951 | output << " : "; |
---|
| 952 | genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); |
---|
| 953 | output << " : "; |
---|
| 954 | genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() ); |
---|
| 955 | output << " : "; |
---|
| 956 | genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() ); |
---|
| 957 | if ( ! asmStmt->get_gotolabels().empty() ) { |
---|
| 958 | output << " : "; |
---|
| 959 | for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) { |
---|
| 960 | output << *begin++; |
---|
| 961 | if ( begin == asmStmt->get_gotolabels().end() ) break; |
---|
| 962 | output << ", "; |
---|
| 963 | } // for |
---|
| 964 | } // if |
---|
[d7312ac] | 965 | output << " );"; |
---|
[7f5566b] | 966 | } |
---|
| 967 | |
---|
[9857e8d] | 968 | void CodeGenerator::postvisit( AsmDecl * asmDecl ) { |
---|
[e994912] | 969 | output << "asm "; |
---|
| 970 | AsmStmt * asmStmt = asmDecl->get_stmt(); |
---|
| 971 | output << "( "; |
---|
[9857e8d] | 972 | if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); |
---|
[d7312ac] | 973 | output << " )"; |
---|
[e994912] | 974 | } |
---|
| 975 | |
---|
[2d019af] | 976 | void CodeGenerator::postvisit( DirectiveDecl * directiveDecl ) { |
---|
| 977 | output << endl << directiveDecl->get_stmt()->directive; // endl prevents spaces before directive |
---|
| 978 | } |
---|
| 979 | |
---|
[cc32d83] | 980 | void CodeGenerator::postvisit( DirectiveStmt * dirStmt ) { |
---|
[d7312ac] | 981 | output << endl << dirStmt->directive; // endl prevents spaces before directive |
---|
[cc32d83] | 982 | } |
---|
| 983 | |
---|
[9857e8d] | 984 | void CodeGenerator::postvisit( IfStmt * ifStmt ) { |
---|
[7f5566b] | 985 | output << "if ( "; |
---|
[9857e8d] | 986 | ifStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 987 | output << " ) "; |
---|
[51587aa] | 988 | |
---|
[3b0bc16] | 989 | ifStmt->get_then()->accept( *visitor ); |
---|
[51587aa] | 990 | |
---|
[3b0bc16] | 991 | if ( ifStmt->get_else() != 0) { |
---|
[2b6c1e0] | 992 | output << " else "; |
---|
[3b0bc16] | 993 | ifStmt->get_else()->accept( *visitor ); |
---|
[51587aa] | 994 | } // if |
---|
| 995 | } |
---|
| 996 | |
---|
[9857e8d] | 997 | void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { |
---|
[d7312ac] | 998 | output << "switch ( "; |
---|
[9857e8d] | 999 | switchStmt->get_condition()->accept( *visitor ); |
---|
[7f5566b] | 1000 | output << " ) "; |
---|
[71f4e4f] | 1001 | |
---|
[d22e90f] | 1002 | output << "{" << endl; |
---|
[f7cb0bc] | 1003 | ++indent; |
---|
[9857e8d] | 1004 | acceptAll( switchStmt->get_statements(), *visitor ); |
---|
[f7cb0bc] | 1005 | --indent; |
---|
[cda48b6] | 1006 | output << indent << "}"; |
---|
[51587aa] | 1007 | } |
---|
| 1008 | |
---|
[9857e8d] | 1009 | void CodeGenerator::postvisit( CaseStmt * caseStmt ) { |
---|
[8bafacc] | 1010 | updateLocation( caseStmt ); |
---|
[1dcd9554] | 1011 | output << indent; |
---|
[eb3261f] | 1012 | if ( caseStmt->isDefault()) { |
---|
[2b6c1e0] | 1013 | output << "default"; |
---|
[eb3261f] | 1014 | } else { |
---|
[2b6c1e0] | 1015 | output << "case "; |
---|
[9857e8d] | 1016 | caseStmt->get_condition()->accept( *visitor ); |
---|
[17cd4eb] | 1017 | } // if |
---|
[d22e90f] | 1018 | output << ":" << endl; |
---|
[71f4e4f] | 1019 | |
---|
[51587aa] | 1020 | std::list<Statement *> sts = caseStmt->get_statements(); |
---|
| 1021 | |
---|
[f7cb0bc] | 1022 | ++indent; |
---|
[51587aa] | 1023 | for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { |
---|
[d7312ac] | 1024 | output << indent << printLabels( (*i)->get_labels() ) ; |
---|
[9857e8d] | 1025 | (*i)->accept( *visitor ); |
---|
[6c4ff37] | 1026 | output << endl; |
---|
[3778cb2] | 1027 | } // for |
---|
[f7cb0bc] | 1028 | --indent; |
---|
[51587aa] | 1029 | } |
---|
| 1030 | |
---|
[9857e8d] | 1031 | void CodeGenerator::postvisit( BranchStmt * branchStmt ) { |
---|
[51587aa] | 1032 | switch ( branchStmt->get_type()) { |
---|
| 1033 | case BranchStmt::Goto: |
---|
| 1034 | if ( ! branchStmt->get_target().empty() ) |
---|
[6c4ff37] | 1035 | output << "goto " << branchStmt->get_target(); |
---|
[71f4e4f] | 1036 | else { |
---|
[51587aa] | 1037 | if ( branchStmt->get_computedTarget() != 0 ) { |
---|
[6c4ff37] | 1038 | output << "goto *"; |
---|
[9857e8d] | 1039 | branchStmt->get_computedTarget()->accept( *visitor ); |
---|
[51587aa] | 1040 | } // if |
---|
| 1041 | } // if |
---|
| 1042 | break; |
---|
| 1043 | case BranchStmt::Break: |
---|
[6c4ff37] | 1044 | output << "break"; |
---|
[51587aa] | 1045 | break; |
---|
| 1046 | case BranchStmt::Continue: |
---|
[6c4ff37] | 1047 | output << "continue"; |
---|
[51587aa] | 1048 | break; |
---|
[7c2a7b6] | 1049 | case BranchStmt::FallThrough: |
---|
| 1050 | case BranchStmt::FallThroughDefault: |
---|
[42a36d9] | 1051 | assertf( ! options.genC, "fallthru should not reach code generation." ); |
---|
[60a8062] | 1052 | output << "fallthru"; |
---|
[7c2a7b6] | 1053 | break; |
---|
[6180274] | 1054 | default: ; // prevent warning |
---|
[3778cb2] | 1055 | } // switch |
---|
[7c2a7b6] | 1056 | // print branch target for labelled break/continue/fallthru in debug mode |
---|
[42a36d9] | 1057 | if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) { |
---|
[7c2a7b6] | 1058 | if ( ! branchStmt->get_target().empty() ) { |
---|
| 1059 | output << " " << branchStmt->get_target(); |
---|
| 1060 | } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) { |
---|
| 1061 | output << " default"; |
---|
| 1062 | } |
---|
| 1063 | } |
---|
[2b6c1e0] | 1064 | output << ";"; |
---|
[51587aa] | 1065 | } |
---|
| 1066 | |
---|
[9857e8d] | 1067 | void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { |
---|
[6c4ff37] | 1068 | output << "return "; |
---|
[9857e8d] | 1069 | maybeAccept( returnStmt->get_expr(), *visitor ); |
---|
[6c4ff37] | 1070 | output << ";"; |
---|
[51587aa] | 1071 | } |
---|
| 1072 | |
---|
[9857e8d] | 1073 | void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { |
---|
[42a36d9] | 1074 | assertf( ! options.genC, "Throw statements should not reach code generation." ); |
---|
[daf1af8] | 1075 | |
---|
| 1076 | output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? |
---|
[60a8062] | 1077 | "throw" : "throwResume"); |
---|
[daf1af8] | 1078 | if (throwStmt->get_expr()) { |
---|
| 1079 | output << " "; |
---|
[9857e8d] | 1080 | throwStmt->get_expr()->accept( *visitor ); |
---|
[daf1af8] | 1081 | } |
---|
| 1082 | if (throwStmt->get_target()) { |
---|
| 1083 | output << " _At "; |
---|
[9857e8d] | 1084 | throwStmt->get_target()->accept( *visitor ); |
---|
[daf1af8] | 1085 | } |
---|
| 1086 | output << ";"; |
---|
| 1087 | } |
---|
[e4ea10b7] | 1088 | void CodeGenerator::postvisit( CatchStmt * stmt ) { |
---|
[42a36d9] | 1089 | assertf( ! options.genC, "Catch statements should not reach code generation." ); |
---|
[e4ea10b7] | 1090 | |
---|
| 1091 | output << ((stmt->get_kind() == CatchStmt::Terminate) ? |
---|
[60a8062] | 1092 | "catch" : "catchResume"); |
---|
[e4ea10b7] | 1093 | output << "( "; |
---|
| 1094 | stmt->decl->accept( *visitor ); |
---|
| 1095 | output << " ) "; |
---|
| 1096 | |
---|
| 1097 | if( stmt->cond ) { |
---|
| 1098 | output << "if/when(?) ("; |
---|
| 1099 | stmt->cond->accept( *visitor ); |
---|
| 1100 | output << ") "; |
---|
| 1101 | } |
---|
| 1102 | stmt->body->accept( *visitor ); |
---|
| 1103 | } |
---|
| 1104 | |
---|
| 1105 | void CodeGenerator::postvisit( WaitForStmt * stmt ) { |
---|
[42a36d9] | 1106 | assertf( ! options.genC, "Waitfor statements should not reach code generation." ); |
---|
[e4ea10b7] | 1107 | |
---|
| 1108 | bool first = true; |
---|
| 1109 | for( auto & clause : stmt->clauses ) { |
---|
| 1110 | if(first) { output << "or "; first = false; } |
---|
| 1111 | if( clause.condition ) { |
---|
| 1112 | output << "when("; |
---|
| 1113 | stmt->timeout.condition->accept( *visitor ); |
---|
| 1114 | output << ") "; |
---|
| 1115 | } |
---|
| 1116 | output << "waitfor("; |
---|
| 1117 | clause.target.function->accept( *visitor ); |
---|
| 1118 | for( Expression * expr : clause.target.arguments ) { |
---|
| 1119 | output << ","; |
---|
| 1120 | expr->accept( *visitor ); |
---|
| 1121 | } |
---|
| 1122 | output << ") "; |
---|
| 1123 | clause.statement->accept( *visitor ); |
---|
| 1124 | } |
---|
| 1125 | |
---|
| 1126 | if( stmt->timeout.statement ) { |
---|
| 1127 | output << "or "; |
---|
| 1128 | if( stmt->timeout.condition ) { |
---|
| 1129 | output << "when("; |
---|
| 1130 | stmt->timeout.condition->accept( *visitor ); |
---|
| 1131 | output << ") "; |
---|
| 1132 | } |
---|
| 1133 | output << "timeout("; |
---|
| 1134 | stmt->timeout.time->accept( *visitor ); |
---|
| 1135 | output << ") "; |
---|
| 1136 | stmt->timeout.statement->accept( *visitor ); |
---|
| 1137 | } |
---|
| 1138 | |
---|
| 1139 | if( stmt->orelse.statement ) { |
---|
| 1140 | output << "or "; |
---|
| 1141 | if( stmt->orelse.condition ) { |
---|
| 1142 | output << "when("; |
---|
| 1143 | stmt->orelse.condition->accept( *visitor ); |
---|
| 1144 | output << ")"; |
---|
| 1145 | } |
---|
| 1146 | output << "else "; |
---|
| 1147 | stmt->orelse.statement->accept( *visitor ); |
---|
| 1148 | } |
---|
| 1149 | } |
---|
| 1150 | |
---|
[55d6e8de] | 1151 | void CodeGenerator::postvisit( WithStmt * with ) { |
---|
[42a36d9] | 1152 | if ( ! options.genC ) { |
---|
[55d6e8de] | 1153 | output << "with ( "; |
---|
| 1154 | genCommaList( with->exprs.begin(), with->exprs.end() ); |
---|
| 1155 | output << " ) "; |
---|
| 1156 | } |
---|
| 1157 | with->stmt->accept( *visitor ); |
---|
| 1158 | } |
---|
[daf1af8] | 1159 | |
---|
[3b0bc16] | 1160 | void CodeGenerator::postvisit( WhileDoStmt * whileDoStmt ) { |
---|
| 1161 | if ( whileDoStmt->get_isDoWhile() ) { |
---|
[d7312ac] | 1162 | output << "do"; |
---|
[321a2481] | 1163 | } else { |
---|
[d7312ac] | 1164 | output << "while ("; |
---|
[3b0bc16] | 1165 | whileDoStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1166 | output << ")"; |
---|
[51587aa] | 1167 | } // if |
---|
[2b6c1e0] | 1168 | output << " "; |
---|
[51587aa] | 1169 | |
---|
[3b0bc16] | 1170 | output << CodeGenerator::printLabels( whileDoStmt->get_body()->get_labels() ); |
---|
| 1171 | whileDoStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1172 | |
---|
[cda48b6] | 1173 | output << indent; |
---|
[51587aa] | 1174 | |
---|
[3b0bc16] | 1175 | if ( whileDoStmt->get_isDoWhile() ) { |
---|
[d7312ac] | 1176 | output << " while ("; |
---|
[3b0bc16] | 1177 | whileDoStmt->get_condition()->accept( *visitor ); |
---|
[6c4ff37] | 1178 | output << ");"; |
---|
[51587aa] | 1179 | } // if |
---|
| 1180 | } |
---|
| 1181 | |
---|
[9857e8d] | 1182 | void CodeGenerator::postvisit( ForStmt * forStmt ) { |
---|
[8e9cbb2] | 1183 | // initialization is always hoisted, so don't bother doing anything with that |
---|
[145f1fc] | 1184 | output << "for (;"; |
---|
[51587aa] | 1185 | |
---|
[321a2481] | 1186 | if ( forStmt->get_condition() != 0 ) { |
---|
[9857e8d] | 1187 | forStmt->get_condition()->accept( *visitor ); |
---|
[3778cb2] | 1188 | } // if |
---|
[6c4ff37] | 1189 | output << ";"; |
---|
[51587aa] | 1190 | |
---|
[321a2481] | 1191 | if ( forStmt->get_increment() != 0 ) { |
---|
| 1192 | // cast the top-level expression to void to reduce gcc warnings. |
---|
| 1193 | Expression * expr = new CastExpr( forStmt->get_increment() ); |
---|
[9857e8d] | 1194 | expr->accept( *visitor ); |
---|
[3778cb2] | 1195 | } // if |
---|
[2b6c1e0] | 1196 | output << ") "; |
---|
[51587aa] | 1197 | |
---|
| 1198 | if ( forStmt->get_body() != 0 ) { |
---|
[2b6c1e0] | 1199 | output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); |
---|
[9857e8d] | 1200 | forStmt->get_body()->accept( *visitor ); |
---|
[51587aa] | 1201 | } // if |
---|
| 1202 | } |
---|
| 1203 | |
---|
[9857e8d] | 1204 | void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { |
---|
[cda48b6] | 1205 | //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); |
---|
[6c4ff37] | 1206 | output << "/* null statement */ ;"; |
---|
[51587aa] | 1207 | } |
---|
| 1208 | |
---|
[9857e8d] | 1209 | void CodeGenerator::postvisit( DeclStmt * declStmt ) { |
---|
| 1210 | declStmt->get_decl()->accept( *visitor ); |
---|
[71f4e4f] | 1211 | |
---|
[51587aa] | 1212 | if ( doSemicolon( declStmt->get_decl() ) ) { |
---|
[6c4ff37] | 1213 | output << ";"; |
---|
[51587aa] | 1214 | } // if |
---|
| 1215 | } |
---|
| 1216 | |
---|
[9857e8d] | 1217 | void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { |
---|
[42a36d9] | 1218 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
[9857e8d] | 1219 | stmt->callStmt->accept( *visitor ); |
---|
| 1220 | } |
---|
| 1221 | |
---|
[6cebfef] | 1222 | void CodeGenerator::postvisit( MutexStmt * stmt ) { |
---|
| 1223 | assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); |
---|
| 1224 | stmt->stmt->accept( *visitor ); |
---|
| 1225 | } |
---|
| 1226 | |
---|
[dd020c0] | 1227 | void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) { |
---|
[fb04321] | 1228 | if ( decl->get_storageClasses().any() ) { |
---|
[6e8bd43] | 1229 | decl->get_storageClasses().print( output ); |
---|
[a7c90d4] | 1230 | } // if |
---|
[dd020c0] | 1231 | } // CodeGenerator::handleStorageClass |
---|
[9facf3b] | 1232 | |
---|
| 1233 | std::string genName( DeclarationWithType * decl ) { |
---|
[60a8062] | 1234 | const OperatorInfo * opInfo = operatorLookup( decl->get_name() ); |
---|
| 1235 | if ( opInfo ) { |
---|
| 1236 | return opInfo->outputName; |
---|
[9facf3b] | 1237 | } else { |
---|
| 1238 | return decl->get_name(); |
---|
| 1239 | } // if |
---|
| 1240 | } |
---|
[1931bb01] | 1241 | |
---|
| 1242 | std::string genName( ast::DeclWithType const * decl ) { |
---|
| 1243 | if ( const OperatorInfo * opInfo = operatorLookup( decl->name ) ) { |
---|
| 1244 | return opInfo->outputName; |
---|
| 1245 | } else { |
---|
| 1246 | return decl->name; |
---|
| 1247 | } |
---|
| 1248 | } |
---|
| 1249 | |
---|
[51b7345] | 1250 | } // namespace CodeGen |
---|
[51587aa] | 1251 | |
---|
| 1252 | // Local Variables: // |
---|
| 1253 | // tab-width: 4 // |
---|
| 1254 | // mode: c++ // |
---|
| 1255 | // compile-command: "make install" // |
---|
| 1256 | // End: // |
---|