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