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