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