| 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 | //
 | 
|---|
| 7 | // CodeGenerator.cpp --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Andrew Beach
 | 
|---|
| 10 | // Created On       : Tue Oct 17 15:54:00 2023
 | 
|---|
| 11 | // Last Modified By : Andrew Beach
 | 
|---|
| 12 | // Last Modified On : Wed Oct 25 18:28:00 2023
 | 
|---|
| 13 | // Update Count     : 0
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "CodeGenerator.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "AST/Print.hpp"
 | 
|---|
| 19 | #include "OperatorTable.hpp"         // for OperatorInfo, operatorLookup
 | 
|---|
| 20 | #include "CodeGen/GenType.hpp"       // for genType
 | 
|---|
| 21 | #include "Common/ToString.hpp"       // for toString
 | 
|---|
| 22 | #include "Common/UniqueName.hpp"     // for UniqueName
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace CodeGen {
 | 
|---|
| 25 | 
 | 
|---|
| 26 | int CodeGenerator::tabsize = 4;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // The kinds of statements that should be followed by whitespace.
 | 
|---|
| 29 | static bool wantSpacing( ast::Stmt const * stmt ) {
 | 
|---|
| 30 |         return dynamic_cast<ast::IfStmt const *>( stmt )
 | 
|---|
| 31 |                 || dynamic_cast<ast::CompoundStmt const *>( stmt )
 | 
|---|
| 32 |                 || dynamic_cast<ast::WhileDoStmt const *>( stmt )
 | 
|---|
| 33 |                 || dynamic_cast<ast::ForStmt const *>( stmt )
 | 
|---|
| 34 |                 || dynamic_cast<ast::SwitchStmt const *>( stmt );
 | 
|---|
| 35 | }
 | 
|---|
| 36 | 
 | 
|---|
| 37 | void CodeGenerator::extension( ast::Expr const * expr ) {
 | 
|---|
| 38 |         if ( expr->extension ) output << "__extension__ ";
 | 
|---|
| 39 | }
 | 
|---|
| 40 | 
 | 
|---|
| 41 | void CodeGenerator::extension( ast::Decl const * decl ) {
 | 
|---|
| 42 |         if ( decl->extension ) output << "__extension__ ";
 | 
|---|
| 43 | }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | void CodeGenerator::asmName( ast::DeclWithType const * decl ) {
 | 
|---|
| 46 |         if ( auto asmName = decl->asmName.as<ast::ConstantExpr>() ) {
 | 
|---|
| 47 |                 output << " asm ( " << asmName->rep << " )";
 | 
|---|
| 48 |         }
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()(
 | 
|---|
| 52 |                 std::vector<ast::Label> const & l ) {
 | 
|---|
| 53 |         labels = &l;
 | 
|---|
| 54 |         return *this;
 | 
|---|
| 55 | }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | std::ostream & CodeGenerator::LabelPrinter::operator()( std::ostream & output ) const {
 | 
|---|
| 58 |         const std::vector<ast::Label> & labels = *this->labels;
 | 
|---|
| 59 |         for ( const ast::Label & label : labels ) {
 | 
|---|
| 60 |                 output << label.name + ": ";
 | 
|---|
| 61 |                 this->cg.genAttributes( label.attributes );
 | 
|---|
| 62 |         }
 | 
|---|
| 63 |         return output;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | // Using updateLocation at the beginning of a node and endl within a node
 | 
|---|
| 67 | // should become the method of formating.
 | 
|---|
| 68 | void CodeGenerator::updateLocation( CodeLocation const & to ) {
 | 
|---|
| 69 |         // Skip if linemarks shouldn't appear or if location is unset.
 | 
|---|
| 70 |         if ( !options.lineMarks || to.isUnset() ) return;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         if ( currentLocation.followedBy( to, 0 ) ) {
 | 
|---|
| 73 |                 return;
 | 
|---|
| 74 |         } else if ( currentLocation.followedBy( to, 1 ) ) {
 | 
|---|
| 75 |                 output << "\n" << indent;
 | 
|---|
| 76 |                 currentLocation.first_line += 1;
 | 
|---|
| 77 |         } else if ( currentLocation.followedBy( to, 2 ) ) {
 | 
|---|
| 78 |                 output << "\n\n" << indent;
 | 
|---|
| 79 |                 currentLocation.first_line += 2;
 | 
|---|
| 80 |         } else {
 | 
|---|
| 81 |                 output << "\n# " << to.first_line << " \"" << to.filename.c_str()
 | 
|---|
| 82 |                        << "\"\n" << indent;
 | 
|---|
| 83 |                 currentLocation = to;
 | 
|---|
| 84 |         }
 | 
|---|
| 85 |         output << std::flush;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void CodeGenerator::updateLocation( ast::ParseNode const * to ) {
 | 
|---|
| 89 |         updateLocation( to->location );
 | 
|---|
| 90 | }
 | 
|---|
| 91 | 
 | 
|---|
| 92 | std::ostream & CodeGenerator::LineEnder::operator()( std::ostream & os ) const {
 | 
|---|
| 93 |         os << "\n" << std::flush;
 | 
|---|
| 94 |         cg.currentLocation.first_line++;
 | 
|---|
| 95 |         return os;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | CodeGenerator::CodeGenerator( std::ostream & os, const Options & options ) :
 | 
|---|
| 99 |                 indent( 0, CodeGenerator::tabsize ), output( os ),
 | 
|---|
| 100 |                 options( options ), printLabels( *this ), endl( *this )
 | 
|---|
| 101 | {}
 | 
|---|
| 102 | 
 | 
|---|
| 103 | std::string CodeGenerator::mangleName( ast::DeclWithType const * decl ) {
 | 
|---|
| 104 |         if ( !options.pretty && decl->linkage.is_mangled && decl->mangleName != "" ) {
 | 
|---|
| 105 |                 return decl->scopedMangleName();
 | 
|---|
| 106 |         } else {
 | 
|---|
| 107 |                 return decl->name;
 | 
|---|
| 108 |         }
 | 
|---|
| 109 | }
 | 
|---|
| 110 | 
 | 
|---|
| 111 | void CodeGenerator::genAttributes(
 | 
|---|
| 112 |                 const std::vector<ast::ptr<ast::Attribute>> & attributes ) {
 | 
|---|
| 113 |         if ( attributes.empty() ) return;
 | 
|---|
| 114 |         output << "__attribute__ ((";
 | 
|---|
| 115 |         for ( auto attr = attributes.begin() ;; ) {
 | 
|---|
| 116 |                 output << (*attr)->name;
 | 
|---|
| 117 |                 if ( !(*attr)->params.empty() ) {
 | 
|---|
| 118 |                         output << "(";
 | 
|---|
| 119 |                         genCommaList( (*attr)->params );
 | 
|---|
| 120 |                         output << ")";
 | 
|---|
| 121 |                 }
 | 
|---|
| 122 |                 if ( ++attr == attributes.end() ) break;
 | 
|---|
| 123 |                 output << ",";
 | 
|---|
| 124 |         }
 | 
|---|
| 125 |         output << ")) ";
 | 
|---|
| 126 | }
 | 
|---|
| 127 | 
 | 
|---|
| 128 | void CodeGenerator::previsit( ast::Node const * ) {
 | 
|---|
| 129 |         // All traversal is manual.
 | 
|---|
| 130 |         // TODO: Which means the ast::Pass is just providing a default no visit?
 | 
|---|
| 131 |         visit_children = false;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void CodeGenerator::previsit( ast::ParseNode const * node ) {
 | 
|---|
| 135 |         previsit( (ast::Node const *)node );
 | 
|---|
| 136 |         updateLocation( node );
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | void CodeGenerator::postvisit( ast::Node const * node ) {
 | 
|---|
| 140 |         std::stringstream ss;
 | 
|---|
| 141 |         ast::print( ss, node );
 | 
|---|
| 142 |         assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() );
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | void CodeGenerator::previsit( ast::Expr const * expr ) {
 | 
|---|
| 146 |         previsit( (ast::ParseNode const *)expr );
 | 
|---|
| 147 |         GuardAction( [this, expr](){
 | 
|---|
| 148 |                 if ( options.printExprTypes && expr->result ) {
 | 
|---|
| 149 |                         output << " /* " << genType( expr->result, "", options ) << " */ ";
 | 
|---|
| 150 |                 }
 | 
|---|
| 151 |         } );
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | void CodeGenerator::postvisit( ast::FunctionDecl const * decl ) {
 | 
|---|
| 155 |         // Deleted decls should never be used, so don't print them in C.
 | 
|---|
| 156 |         if ( decl->isDeleted && options.genC ) return;
 | 
|---|
| 157 |         extension( decl );
 | 
|---|
| 158 |         genAttributes( decl->attributes );
 | 
|---|
| 159 | 
 | 
|---|
| 160 |         handleStorageClass( decl );
 | 
|---|
| 161 |         ast::print( output, decl->funcSpec );
 | 
|---|
| 162 | 
 | 
|---|
| 163 |         Options subOptions = options;
 | 
|---|
| 164 |         subOptions.anonymousUnused = decl->stmts;
 | 
|---|
| 165 | 
 | 
|---|
| 166 |         std::ostringstream acc;
 | 
|---|
| 167 |         ast::Pass<CodeGenerator> subCG( acc, subOptions );
 | 
|---|
| 168 |         // Add the forall clause.
 | 
|---|
| 169 |         if ( !decl->type_params.empty() ) {
 | 
|---|
| 170 |                 assertf( !options.genC, "FunctionDecl forall should not reach code generation." );
 | 
|---|
| 171 |                 acc << "forall(";
 | 
|---|
| 172 |                 subCG.core.genCommaList( decl->type_params );
 | 
|---|
| 173 |                 acc << ")" << std::endl;
 | 
|---|
| 174 |         }
 | 
|---|
| 175 |         // The forall clause should be printed early as part of the preamble.
 | 
|---|
| 176 |         output << acc.str();
 | 
|---|
| 177 |         acc.str("");
 | 
|---|
| 178 | 
 | 
|---|
| 179 |         acc << mangleName( decl );
 | 
|---|
| 180 | 
 | 
|---|
| 181 |         if ( 0 == decl->params.size() ) {
 | 
|---|
| 182 |                 if ( !decl->type->isVarArgs ) {
 | 
|---|
| 183 |                         acc << "(void)";
 | 
|---|
| 184 |                 } else if ( options.genC ) {
 | 
|---|
| 185 |                         acc << "()";
 | 
|---|
| 186 |                 } else {
 | 
|---|
| 187 |                         acc << "(...)";
 | 
|---|
| 188 |                 }
 | 
|---|
| 189 |         } else {
 | 
|---|
| 190 |                 acc << "(";
 | 
|---|
| 191 |                 subCG.core.genCommaList( decl->params );
 | 
|---|
| 192 |                 if ( decl->type->isVarArgs ) {
 | 
|---|
| 193 |                         acc << ", ...";
 | 
|---|
| 194 |                 }
 | 
|---|
| 195 |                 acc << ")";
 | 
|---|
| 196 |         }
 | 
|---|
| 197 | 
 | 
|---|
| 198 |         if ( 1 == decl->returns.size() ) {
 | 
|---|
| 199 |                 ast::ptr<ast::Type> const & type = decl->returns[0]->get_type();
 | 
|---|
| 200 |                 output << genTypeNoAttr( type, acc.str(), subOptions );
 | 
|---|
| 201 |         } else if ( 0 == decl->returns.size() ) {
 | 
|---|
| 202 |                 output << "void " + acc.str();
 | 
|---|
| 203 |         } else {
 | 
|---|
| 204 |                 assertf( !options.genC, "Multi-return should not reach code generation." );
 | 
|---|
| 205 |                 ast::ptr<ast::Type> type = new ast::TupleType( copy( decl->type->returns ) );
 | 
|---|
| 206 |                 output << genTypeNoAttr( type, acc.str(), subOptions );
 | 
|---|
| 207 |         }
 | 
|---|
| 208 | 
 | 
|---|
| 209 |         asmName( decl );
 | 
|---|
| 210 | 
 | 
|---|
| 211 |         if ( decl->stmts ) {
 | 
|---|
| 212 |                 decl->stmts->accept( *visitor );
 | 
|---|
| 213 |         }
 | 
|---|
| 214 |         if ( decl->isDeleted ) {
 | 
|---|
| 215 |                 output << " = void";
 | 
|---|
| 216 |         }
 | 
|---|
| 217 | }
 | 
|---|
| 218 | 
 | 
|---|
| 219 | ast::ObjectDecl const * CodeGenerator::postvisit(
 | 
|---|
| 220 |                 ast::ObjectDecl const * decl ) {
 | 
|---|
| 221 |         // Deleted decls should never be used, so don't print them in C.
 | 
|---|
| 222 |         if ( decl->isDeleted && options.genC ) return decl;
 | 
|---|
| 223 | 
 | 
|---|
| 224 |         // GCC allows an empty declarator (no name) for bit-fields and C
 | 
|---|
| 225 |         // states: 6.7.2.1 Structure and union specifiers, point 4, page 113:
 | 
|---|
| 226 |         // If the (bit field) value is zero, the declaration shall have no
 | 
|---|
| 227 |         // declarator. For anything else, the anonymous name refers to the
 | 
|---|
| 228 |         // anonymous object for plan9 inheritance.
 | 
|---|
| 229 |         if ( decl->name.empty() && options.genC && !decl->bitfieldWidth ) {
 | 
|---|
| 230 |                 // TODO: Should this be changed in a pervious pass?
 | 
|---|
| 231 |                 auto mutDecl = ast::mutate( decl );
 | 
|---|
| 232 |                 // Only generate an anonymous name when generating C code,
 | 
|---|
| 233 |                 // otherwise it clutters the output too much.
 | 
|---|
| 234 |                 static UniqueName name = { "__anonymous_object" };
 | 
|---|
| 235 |                 mutDecl->name = name.newName();
 | 
|---|
| 236 |                 // Stops unused parameter warnings.
 | 
|---|
| 237 |                 if ( options.anonymousUnused ) {
 | 
|---|
| 238 |                         mutDecl->attributes.push_back( new ast::Attribute( "unused" ) );
 | 
|---|
| 239 |                 }
 | 
|---|
| 240 |                 decl = mutDecl;
 | 
|---|
| 241 |         }
 | 
|---|
| 242 | 
 | 
|---|
| 243 |         extension( decl );
 | 
|---|
| 244 |         genAttributes( decl->attributes );
 | 
|---|
| 245 | 
 | 
|---|
| 246 |         handleStorageClass( decl );
 | 
|---|
| 247 |         output << genType( decl->type, mangleName( decl ),
 | 
|---|
| 248 |                 Options( options.pretty, options.genC, false, false ) );
 | 
|---|
| 249 | 
 | 
|---|
| 250 |         asmName( decl );
 | 
|---|
| 251 | 
 | 
|---|
| 252 |         if ( decl->init ) {
 | 
|---|
| 253 |                 output << " = ";
 | 
|---|
| 254 |                 decl->init->accept( *visitor );
 | 
|---|
| 255 |         }
 | 
|---|
| 256 |         if ( decl->isDeleted ) {
 | 
|---|
| 257 |                 output << " = void";
 | 
|---|
| 258 |         }
 | 
|---|
| 259 | 
 | 
|---|
| 260 |         if ( decl->bitfieldWidth ) {
 | 
|---|
| 261 |                 output << ":";
 | 
|---|
| 262 |                 decl->bitfieldWidth->accept( *visitor );
 | 
|---|
| 263 |         }
 | 
|---|
| 264 |         return decl;
 | 
|---|
| 265 | }
 | 
|---|
| 266 | 
 | 
|---|
| 267 | void CodeGenerator::handleStorageClass( ast::DeclWithType const * decl ) {
 | 
|---|
| 268 |         if ( decl->storage.any() ) {
 | 
|---|
| 269 |                 ast::print( output, decl->storage );
 | 
|---|
| 270 |         }
 | 
|---|
| 271 | }
 | 
|---|
| 272 | 
 | 
|---|
| 273 | void CodeGenerator::handleAggregate(
 | 
|---|
| 274 |                 ast::AggregateDecl const * decl, std::string const & kind ) {
 | 
|---|
| 275 |         if ( !decl->params.empty() && !options.genC ) {
 | 
|---|
| 276 |                 output << "forall(";
 | 
|---|
| 277 |                 genCommaList( decl->params );
 | 
|---|
| 278 |                 output << ")\n" << indent << std::flush;
 | 
|---|
| 279 |         }
 | 
|---|
| 280 | 
 | 
|---|
| 281 |         output << kind;
 | 
|---|
| 282 |         genAttributes( decl->attributes );
 | 
|---|
| 283 |         output << decl->name;
 | 
|---|
| 284 | 
 | 
|---|
| 285 |         if ( decl->body ) {
 | 
|---|
| 286 |                 auto & members = decl->members;
 | 
|---|
| 287 |                 output << " {" << endl;
 | 
|---|
| 288 | 
 | 
|---|
| 289 |                 ++indent;
 | 
|---|
| 290 |                 for ( auto & member : members ) {
 | 
|---|
| 291 |                         output << indent;
 | 
|---|
| 292 |                         member->accept( *visitor );
 | 
|---|
| 293 |                         output << ";" << endl;
 | 
|---|
| 294 |                 }
 | 
|---|
| 295 |                 --indent;
 | 
|---|
| 296 | 
 | 
|---|
| 297 |                 output << indent << "}";
 | 
|---|
| 298 |         }
 | 
|---|
| 299 | }
 | 
|---|
| 300 | 
 | 
|---|
| 301 | void CodeGenerator::postvisit( ast::StructDecl const * decl ) {
 | 
|---|
| 302 |         extension( decl );
 | 
|---|
| 303 |         handleAggregate( decl, "struct " );
 | 
|---|
| 304 | }
 | 
|---|
| 305 | 
 | 
|---|
| 306 | void CodeGenerator::postvisit( ast::UnionDecl const * decl ) {
 | 
|---|
| 307 |         extension( decl );
 | 
|---|
| 308 |         handleAggregate( decl, "union " );
 | 
|---|
| 309 | }
 | 
|---|
| 310 | 
 | 
|---|
| 311 | template<typename pass_type>
 | 
|---|
| 312 | inline void genEnumInitializer( ast::Pass<pass_type> * visitor,
 | 
|---|
| 313 |                 ast::Type const * baseType, std::ostream & output,
 | 
|---|
| 314 |                 ast::Init const * init, long long * curVal, Options options ) {
 | 
|---|
| 315 |         auto baseTypeAsBasic = dynamic_cast<ast::BasicType const *>( baseType );
 | 
|---|
| 316 |         // Value is provided.
 | 
|---|
| 317 |         if ( init ) {
 | 
|---|
| 318 |                 output << " = (" << genType( baseType, "", options ) << ")";
 | 
|---|
| 319 |                 init->accept( *visitor );
 | 
|---|
| 320 |                 // If it is an integral type and initilizer offered,
 | 
|---|
| 321 |                 // need to update the curVal.
 | 
|---|
| 322 |                 if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) {
 | 
|---|
| 323 |                         ast::Expr const * expr = ((ast::SingleInit const *)(init))->value;
 | 
|---|
| 324 |                         // Unwrap introduced cast.
 | 
|---|
| 325 |                         while ( auto temp = dynamic_cast<ast::CastExpr const *>( expr ) ) {
 | 
|---|
| 326 |                                 expr = temp->arg;
 | 
|---|
| 327 |                         }
 | 
|---|
| 328 |                         *curVal = ((ast::ConstantExpr const *)(expr))->intValue() + 1;
 | 
|---|
| 329 |                 }
 | 
|---|
| 330 |         // Generate next value from previous value.
 | 
|---|
| 331 |         } else if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) {
 | 
|---|
| 332 |                 output << " = (" << genType( baseType, "", options ) << ")";
 | 
|---|
| 333 |                 output << (*curVal)++;
 | 
|---|
| 334 |         }
 | 
|---|
| 335 | }
 | 
|---|
| 336 | 
 | 
|---|
| 337 | void CodeGenerator::postvisit( ast::EnumDecl const * decl ) {
 | 
|---|
| 338 |         extension( decl );
 | 
|---|
| 339 |         auto members = decl->members;
 | 
|---|
| 340 |         // if ( decl->base && !members.empty() ) {
 | 
|---|
| 341 |         //      long long curVal = 0;
 | 
|---|
| 342 |         //      for ( auto member : members ) {
 | 
|---|
| 343 |         //              auto obj = member.strict_as<ast::ObjectDecl>();
 | 
|---|
| 344 |         //              output << "static ";
 | 
|---|
| 345 |         //              output << genType( decl->base, mangleName( obj ), options );
 | 
|---|
| 346 |         //              genEnumInitializer( visitor, decl->base, output, obj->init, &curVal, options );
 | 
|---|
| 347 |         //              output << ";" << endl;
 | 
|---|
| 348 |         //      }
 | 
|---|
| 349 |         // } else {
 | 
|---|
| 350 |                 output << "enum ";
 | 
|---|
| 351 |                 genAttributes( decl->attributes );
 | 
|---|
| 352 | 
 | 
|---|
| 353 |                 output << decl->name;
 | 
|---|
| 354 | 
 | 
|---|
| 355 |                 if ( !members.empty() ) {
 | 
|---|
| 356 |                         output << " {" << endl;
 | 
|---|
| 357 | 
 | 
|---|
| 358 |                         ++indent;
 | 
|---|
| 359 |                         for ( auto member : members ) {
 | 
|---|
| 360 |                                 auto obj = member.strict_as<ast::ObjectDecl>();
 | 
|---|
| 361 |                                 output << indent << mangleName( obj );
 | 
|---|
| 362 |                                 if ( !decl->base && obj->init ) {
 | 
|---|
| 363 |                                         output << " = ";
 | 
|---|
| 364 |                                         obj->init->accept( *visitor );
 | 
|---|
| 365 |                                 }
 | 
|---|
| 366 |                                 output << "," << endl;
 | 
|---|
| 367 |                         }
 | 
|---|
| 368 |                         --indent;
 | 
|---|
| 369 | 
 | 
|---|
| 370 |                         output << indent << "}";
 | 
|---|
| 371 |                 }
 | 
|---|
| 372 |         // }
 | 
|---|
| 373 | }
 | 
|---|
| 374 | 
 | 
|---|
| 375 | void CodeGenerator::postvisit( ast::TraitDecl const * decl ) {
 | 
|---|
| 376 |         assertf( !options.genC, "TraitDecls should not reach code generation." );
 | 
|---|
| 377 |         extension( decl );
 | 
|---|
| 378 |         handleAggregate( decl, "trait " );
 | 
|---|
| 379 | }
 | 
|---|
| 380 | 
 | 
|---|
| 381 | void CodeGenerator::postvisit( ast::TypedefDecl const * decl ) {
 | 
|---|
| 382 |         assertf( !options.genC, "Typedefs should not reach code generation." );
 | 
|---|
| 383 |         output << "typedef " << genType( decl->base, decl->name, options ) << endl;
 | 
|---|
| 384 | }
 | 
|---|
| 385 | 
 | 
|---|
| 386 | void CodeGenerator::postvisit( ast::TypeDecl const * decl ) {
 | 
|---|
| 387 |         assertf( !options.genC, "TypeDecls should not reach code generation." );
 | 
|---|
| 388 |         output << decl->genTypeString() << " " << decl->name;
 | 
|---|
| 389 |         if ( decl->sized ) {
 | 
|---|
| 390 |                 output << " | sized(" << decl->name << ")";
 | 
|---|
| 391 |         }
 | 
|---|
| 392 |         if ( !decl->assertions.empty() ) {
 | 
|---|
| 393 |                 output << " | { ";
 | 
|---|
| 394 |                 for ( ast::DeclWithType const * assert : decl->assertions ) {
 | 
|---|
| 395 |                         assert->accept( *visitor );
 | 
|---|
| 396 |                         output << "; ";
 | 
|---|
| 397 |                 }
 | 
|---|
| 398 |                 output << " }";
 | 
|---|
| 399 |         }
 | 
|---|
| 400 | }
 | 
|---|
| 401 | 
 | 
|---|
| 402 | void CodeGenerator::postvisit( ast::StaticAssertDecl const * decl ) {
 | 
|---|
| 403 |         output << "_Static_assert(";
 | 
|---|
| 404 |         decl->cond->accept( *visitor );
 | 
|---|
| 405 |         output << ", ";
 | 
|---|
| 406 |         decl->msg->accept( *visitor );
 | 
|---|
| 407 |         output << ")";
 | 
|---|
| 408 | }
 | 
|---|
| 409 | 
 | 
|---|
| 410 | void CodeGenerator::postvisit( ast::Designation const * designation ) {
 | 
|---|
| 411 |         auto designators = designation->designators;
 | 
|---|
| 412 |         if ( 0 == designators.size() ) return;
 | 
|---|
| 413 |         for ( ast::ptr<ast::Expr> const & des : designators ) {
 | 
|---|
| 414 |                 // If the expression is a NameExpr or VariableExpr, then it is a field.
 | 
|---|
| 415 |                 if ( des.as<ast::NameExpr>() || des.as<ast::VariableExpr>() ) {
 | 
|---|
| 416 |                         output << ".";
 | 
|---|
| 417 |                         des->accept( *visitor );
 | 
|---|
| 418 |                 // Otherwise, it is a ConstantExpr or CastExpr, then it is an index.
 | 
|---|
| 419 |                 } else {
 | 
|---|
| 420 |                         output << "[";
 | 
|---|
| 421 |                         des->accept( *visitor );
 | 
|---|
| 422 |                         output << "]";
 | 
|---|
| 423 |                 }
 | 
|---|
| 424 |         }
 | 
|---|
| 425 |         output << " = ";
 | 
|---|
| 426 | }
 | 
|---|
| 427 | 
 | 
|---|
| 428 | void CodeGenerator::postvisit( ast::SingleInit const * init ) {
 | 
|---|
| 429 |         init->value->accept( *visitor );
 | 
|---|
| 430 | }
 | 
|---|
| 431 | 
 | 
|---|
| 432 | void CodeGenerator::postvisit( ast::ListInit const * init ) {
 | 
|---|
| 433 |         auto initBegin = init->initializers.begin();
 | 
|---|
| 434 |         auto initEnd = init->initializers.end();
 | 
|---|
| 435 |         auto desigBegin = init->designations.begin();
 | 
|---|
| 436 |         auto desigEnd = init->designations.end();
 | 
|---|
| 437 | 
 | 
|---|
| 438 |         output << "{ ";
 | 
|---|
| 439 |         if ( initBegin != initEnd ) while (true) {
 | 
|---|
| 440 |                 (*desigBegin)->accept( *visitor );
 | 
|---|
| 441 |                 (*initBegin)->accept( *visitor );
 | 
|---|
| 442 |                 ++initBegin, ++desigBegin;
 | 
|---|
| 443 |                 if ( initBegin == initEnd ) break;
 | 
|---|
| 444 |                 output << ", ";
 | 
|---|
| 445 |         }
 | 
|---|
| 446 |         output << " }";
 | 
|---|
| 447 |         assertf( initBegin == initEnd && desigBegin == desigEnd,
 | 
|---|
| 448 |                 "Initializers and designators not the same length. %s", toCString( init ) );
 | 
|---|
| 449 | }
 | 
|---|
| 450 | 
 | 
|---|
| 451 | void CodeGenerator::postvisit( ast::ConstructorInit const * init ) {
 | 
|---|
| 452 |         assertf( !options.genC, "ConstructorInit nodes should not reach code generation." );
 | 
|---|
| 453 |         // This isn't actual code, but labels the constructor/destructor pairs.
 | 
|---|
| 454 |         output << "<ctorinit>{" << endl << ++indent << "ctor: ";
 | 
|---|
| 455 |         if ( init->ctor ) init->ctor->accept( *visitor );
 | 
|---|
| 456 |         output << ", " << endl << indent << "dtor: ";
 | 
|---|
| 457 |         if ( init->dtor ) init->dtor->accept( *visitor );
 | 
|---|
| 458 |         output << endl << --indent << "}";
 | 
|---|
| 459 | }
 | 
|---|
| 460 | 
 | 
|---|
| 461 | void CodeGenerator::postvisit( ast::ApplicationExpr const * expr ) {
 | 
|---|
| 462 |         extension( expr );
 | 
|---|
| 463 |         if ( auto var = expr->func.as<ast::VariableExpr>() ) {
 | 
|---|
| 464 |                 const OperatorInfo * opInfo;
 | 
|---|
| 465 |                 if ( var->var->linkage == ast::Linkage::Intrinsic &&
 | 
|---|
| 466 |                                 ( opInfo = operatorLookup( var->var->name ) ) ) {
 | 
|---|
| 467 |                         auto arg = expr->args.begin();
 | 
|---|
| 468 |                         switch ( opInfo->type ) {
 | 
|---|
| 469 |                         case OT_INDEX:
 | 
|---|
| 470 |                                 assert( 2 == expr->args.size() );
 | 
|---|
| 471 |                                 (*arg++)->accept( *visitor );
 | 
|---|
| 472 |                                 output << "[";
 | 
|---|
| 473 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 474 |                                 output << "]";
 | 
|---|
| 475 |                                 break;
 | 
|---|
| 476 | 
 | 
|---|
| 477 |                         // There are no intrinsic definitions of the function call operator.
 | 
|---|
| 478 |                         case OT_CALL:
 | 
|---|
| 479 |                                 assert( false );
 | 
|---|
| 480 |                                 break;
 | 
|---|
| 481 | 
 | 
|---|
| 482 |                         case OT_CTOR:
 | 
|---|
| 483 |                         case OT_DTOR:
 | 
|---|
| 484 |                                 // No-op constructor, but run the internal expression.
 | 
|---|
| 485 |                                 if ( 1 == expr->args.size() ) {
 | 
|---|
| 486 |                                         output << "(";
 | 
|---|
| 487 |                                         (*arg++)->accept( *visitor );
 | 
|---|
| 488 |                                         output << ") /* " << opInfo->inputName << " */";
 | 
|---|
| 489 |                                 // These are all implemented as some form of assignment.
 | 
|---|
| 490 |                                 } else if ( 2 == expr->args.size() ) {
 | 
|---|
| 491 |                                         output << "(";
 | 
|---|
| 492 |                                         (*arg++)->accept( *visitor );
 | 
|---|
| 493 |                                         output << opInfo->symbol;
 | 
|---|
| 494 |                                         (*arg)->accept( *visitor );
 | 
|---|
| 495 |                                         output << ") /* " << opInfo->inputName << " */";
 | 
|---|
| 496 |                                 // No constructors with 0 or more than 2 parameters.
 | 
|---|
| 497 |                                 } else {
 | 
|---|
| 498 |                                         assert( false );
 | 
|---|
| 499 |                                 }
 | 
|---|
| 500 |                                 break;
 | 
|---|
| 501 | 
 | 
|---|
| 502 |                         case OT_PREFIX:
 | 
|---|
| 503 |                         case OT_PREFIXASSIGN:
 | 
|---|
| 504 |                                 assert( 1 == expr->args.size() );
 | 
|---|
| 505 |                                 output << "(" << opInfo->symbol;
 | 
|---|
| 506 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 507 |                                 output << ")";
 | 
|---|
| 508 |                                 break;
 | 
|---|
| 509 | 
 | 
|---|
| 510 |                         case OT_POSTFIX:
 | 
|---|
| 511 |                         case OT_POSTFIXASSIGN:
 | 
|---|
| 512 |                                 assert( 1 == expr->args.size() );
 | 
|---|
| 513 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 514 |                                 output << opInfo->symbol;
 | 
|---|
| 515 |                                 break;
 | 
|---|
| 516 | 
 | 
|---|
| 517 |                         case OT_INFIX:
 | 
|---|
| 518 |                         case OT_INFIXASSIGN:
 | 
|---|
| 519 |                                 assert( 2 == expr->args.size() );
 | 
|---|
| 520 |                                 output << "(";
 | 
|---|
| 521 |                                 (*arg++)->accept( *visitor );
 | 
|---|
| 522 |                                 output << opInfo->symbol;
 | 
|---|
| 523 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 524 |                                 output << ")";
 | 
|---|
| 525 |                                 break;
 | 
|---|
| 526 | 
 | 
|---|
| 527 |                         // There are no intrinsic definitions of 0/1 or label address
 | 
|---|
| 528 |                         // as function.
 | 
|---|
| 529 |                         case OT_CONSTANT:
 | 
|---|
| 530 |                         case OT_LABELADDRESS:
 | 
|---|
| 531 |                                 assert( false );
 | 
|---|
| 532 |                         }
 | 
|---|
| 533 |                 // TODO: This is a work-around to make it a constant until a proper
 | 
|---|
| 534 |                 // constexpr solution is created.
 | 
|---|
| 535 |                 } else if ( var->var->linkage == ast::Linkage::BuiltinCFA &&
 | 
|---|
| 536 |                                 var->var->name == "intptr" ) {
 | 
|---|
| 537 |                         output << "((void*)";
 | 
|---|
| 538 |                         auto arg = expr->args.begin();
 | 
|---|
| 539 |                         (*arg++)->accept( *visitor );
 | 
|---|
| 540 |                         output << ")";
 | 
|---|
| 541 |                 } else {
 | 
|---|
| 542 |                         var->accept( *visitor );
 | 
|---|
| 543 |                         output << "(";
 | 
|---|
| 544 |                         genCommaList( expr->args );
 | 
|---|
| 545 |                         output << ")";
 | 
|---|
| 546 |                 }
 | 
|---|
| 547 |         } else {
 | 
|---|
| 548 |                 expr->func->accept( *visitor );
 | 
|---|
| 549 |                 output << "(";
 | 
|---|
| 550 |                 genCommaList( expr->args );
 | 
|---|
| 551 |                 output << ")";
 | 
|---|
| 552 |         }
 | 
|---|
| 553 | }
 | 
|---|
| 554 | 
 | 
|---|
| 555 | void CodeGenerator::postvisit( ast::UntypedExpr const * expr ) {
 | 
|---|
| 556 |         extension( expr );
 | 
|---|
| 557 |         if ( auto name = expr->func.as<ast::NameExpr>() ) {
 | 
|---|
| 558 |                 if ( const OperatorInfo * opInfo = operatorLookup( name->name ) ) {
 | 
|---|
| 559 |                         auto arg = expr->args.begin();
 | 
|---|
| 560 |                         switch ( opInfo->type ) {
 | 
|---|
| 561 |                         case OT_INDEX:
 | 
|---|
| 562 |                                 assert( 2 == expr->args.size() );
 | 
|---|
| 563 |                                 (*arg++)->accept( *visitor );
 | 
|---|
| 564 |                                 output << "[";
 | 
|---|
| 565 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 566 |                                 output << "]";
 | 
|---|
| 567 |                                 break;
 | 
|---|
| 568 | 
 | 
|---|
| 569 |                         case OT_CALL:
 | 
|---|
| 570 |                                 assert( false );
 | 
|---|
| 571 | 
 | 
|---|
| 572 |                         case OT_CTOR:
 | 
|---|
| 573 |                         case OT_DTOR:
 | 
|---|
| 574 |                                 // No-op constructor, but run the internal expression.
 | 
|---|
| 575 |                                 if ( 1 == expr->args.size() ) {
 | 
|---|
| 576 |                                         output << "(";
 | 
|---|
| 577 |                                         (*arg++)->accept( *visitor );
 | 
|---|
| 578 |                                         output << ")";
 | 
|---|
| 579 |                                 // These are all implemented as some form of assignment.
 | 
|---|
| 580 |                                 } else if ( 2 == expr->args.size() ) {
 | 
|---|
| 581 |                                         output << "(";
 | 
|---|
| 582 |                                         (*arg++)->accept( *visitor );
 | 
|---|
| 583 |                                         output << opInfo->symbol;
 | 
|---|
| 584 |                                         (*arg)->accept( *visitor );
 | 
|---|
| 585 |                                         output << ") /* " << opInfo->inputName << " */";
 | 
|---|
| 586 |                                 // No constructors with 0 or more than 2 parameters.
 | 
|---|
| 587 |                                 } else {
 | 
|---|
| 588 |                                         assertf( !options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );
 | 
|---|
| 589 |                                         output << "(";
 | 
|---|
| 590 |                                         (*arg++)->accept( *visitor );
 | 
|---|
| 591 |                                         output << opInfo->symbol << "{ ";
 | 
|---|
| 592 |                                         genCommaList( arg, expr->args.end() );
 | 
|---|
| 593 |                                         output << "}) /* " << opInfo->inputName << " */";
 | 
|---|
| 594 |                                 }
 | 
|---|
| 595 |                                 break;
 | 
|---|
| 596 | 
 | 
|---|
| 597 |                         case OT_PREFIX:
 | 
|---|
| 598 |                         case OT_PREFIXASSIGN:
 | 
|---|
| 599 |                         case OT_LABELADDRESS:
 | 
|---|
| 600 |                                 assert( 1 == expr->args.size() );
 | 
|---|
| 601 |                                 output << "(" << opInfo->symbol;
 | 
|---|
| 602 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 603 |                                 output << ")";
 | 
|---|
| 604 |                                 break;
 | 
|---|
| 605 | 
 | 
|---|
| 606 |                         case OT_POSTFIX:
 | 
|---|
| 607 |                         case OT_POSTFIXASSIGN:
 | 
|---|
| 608 |                                 assert( 1 == expr->args.size() );
 | 
|---|
| 609 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 610 |                                 output << opInfo->symbol;
 | 
|---|
| 611 |                                 break;
 | 
|---|
| 612 | 
 | 
|---|
| 613 |                         case OT_INFIX:
 | 
|---|
| 614 |                         case OT_INFIXASSIGN:
 | 
|---|
| 615 |                                 assert( 2 == expr->args.size() );
 | 
|---|
| 616 |                                 output << "(";
 | 
|---|
| 617 |                                 (*arg++)->accept( *visitor );
 | 
|---|
| 618 |                                 output << opInfo->symbol;
 | 
|---|
| 619 |                                 (*arg)->accept( *visitor );
 | 
|---|
| 620 |                                 output << ")";
 | 
|---|
| 621 |                                 break;
 | 
|---|
| 622 | 
 | 
|---|
| 623 |                         // There are no intrinsic definitions of 0/1 or label address
 | 
|---|
| 624 |                         // as function.
 | 
|---|
| 625 |                         case OT_CONSTANT:
 | 
|---|
| 626 |                                 assert( false );
 | 
|---|
| 627 |                         }
 | 
|---|
| 628 |                 // builtin routines
 | 
|---|
| 629 |                 } else {
 | 
|---|
| 630 |                         name->accept( *visitor );
 | 
|---|
| 631 |                         output << "(";
 | 
|---|
| 632 |                         genCommaList( expr->args );
 | 
|---|
| 633 |                         output << ")";
 | 
|---|
| 634 |                 }
 | 
|---|
| 635 |         } else {
 | 
|---|
| 636 |                 expr->func->accept( *visitor );
 | 
|---|
| 637 |                 output << "(";
 | 
|---|
| 638 |                 genCommaList( expr->args );
 | 
|---|
| 639 |                 output << ")";
 | 
|---|
| 640 |         }
 | 
|---|
| 641 | }
 | 
|---|
| 642 | 
 | 
|---|
| 643 | void CodeGenerator::postvisit( ast::RangeExpr const * expr ) {
 | 
|---|
| 644 |         expr->low->accept( *visitor );
 | 
|---|
| 645 |         output << " ... ";
 | 
|---|
| 646 |         expr->high->accept( *visitor );
 | 
|---|
| 647 | }
 | 
|---|
| 648 | 
 | 
|---|
| 649 | void CodeGenerator::postvisit( ast::NameExpr const * expr ) {
 | 
|---|
| 650 |         extension( expr );
 | 
|---|
| 651 |         if ( const OperatorInfo * opInfo = operatorLookup( expr->name ) ) {
 | 
|---|
| 652 |                 if ( OT_CONSTANT == opInfo->type ) {
 | 
|---|
| 653 |                         output << opInfo->symbol;
 | 
|---|
| 654 |                 } else {
 | 
|---|
| 655 |                         output << opInfo->outputName;
 | 
|---|
| 656 |                 }
 | 
|---|
| 657 |         } else {
 | 
|---|
| 658 |                 output << expr->name;
 | 
|---|
| 659 |         }
 | 
|---|
| 660 | }
 | 
|---|
| 661 | 
 | 
|---|
| 662 | void CodeGenerator::postvisit( ast::DimensionExpr const * expr ) {
 | 
|---|
| 663 |         extension( expr );
 | 
|---|
| 664 |         output << "/*non-type*/" << expr->name;
 | 
|---|
| 665 | }
 | 
|---|
| 666 | 
 | 
|---|
| 667 | void CodeGenerator::postvisit( ast::AddressExpr const * expr ) {
 | 
|---|
| 668 |         extension( expr );
 | 
|---|
| 669 |         output << "(&";
 | 
|---|
| 670 |         expr->arg->accept( *visitor );
 | 
|---|
| 671 |         output << ")";
 | 
|---|
| 672 | }
 | 
|---|
| 673 | 
 | 
|---|
| 674 | void CodeGenerator::postvisit( ast::LabelAddressExpr const * expr ) {
 | 
|---|
| 675 |         extension( expr );
 | 
|---|
| 676 |         output << "(&&" << expr->arg << ")";
 | 
|---|
| 677 | }
 | 
|---|
| 678 | 
 | 
|---|
| 679 | void CodeGenerator::postvisit( ast::CastExpr const * expr ) {
 | 
|---|
| 680 |         extension( expr );
 | 
|---|
| 681 |         output << "(";
 | 
|---|
| 682 |         switch ( expr->kind ) {
 | 
|---|
| 683 |         case ast::CCast:
 | 
|---|
| 684 |                 if ( expr->result->isVoid() ) {
 | 
|---|
| 685 |                         output << "(void)";
 | 
|---|
| 686 |                 } else {
 | 
|---|
| 687 |                         output << "(";
 | 
|---|
| 688 |                         output << genType( expr->result, "", options );
 | 
|---|
| 689 |                         output << ")";
 | 
|---|
| 690 |                 }
 | 
|---|
| 691 |                 break;
 | 
|---|
| 692 |         case ast::CoerceCast:
 | 
|---|
| 693 |                 assertf( ast::CoerceCast != expr->kind, "Coercion cast is not implemented." );
 | 
|---|
| 694 |                 // And likely shouldn't reach code generation when it is implemented.
 | 
|---|
| 695 |                 break;
 | 
|---|
| 696 |         case ast::ReturnCast:
 | 
|---|
| 697 |                 // This should be invisible in the resulting C code.
 | 
|---|
| 698 |                 // Can we insert a check here?
 | 
|---|
| 699 |                 //assert( ResolvExpr::typesCompatable(???) );
 | 
|---|
| 700 |                 if ( options.genC ) break;
 | 
|---|
| 701 |                 output << "(return ";
 | 
|---|
| 702 |                 output << genType( expr->result, "", options );
 | 
|---|
| 703 |                 output << ")";
 | 
|---|
| 704 |                 break;
 | 
|---|
| 705 |         }
 | 
|---|
| 706 |         expr->arg->accept( *visitor );
 | 
|---|
| 707 |         output << ")";
 | 
|---|
| 708 | }
 | 
|---|
| 709 | 
 | 
|---|
| 710 | void CodeGenerator::postvisit( ast::KeywordCastExpr const * expr ) {
 | 
|---|
| 711 |         assertf( !options.genC, "KeywordCastExpr should not reach code generation." );
 | 
|---|
| 712 |         extension( expr );
 | 
|---|
| 713 |         output << "((" << expr->targetString() << " &)";
 | 
|---|
| 714 |         expr->arg->accept( *visitor );
 | 
|---|
| 715 |         output << ")";
 | 
|---|
| 716 | }
 | 
|---|
| 717 | 
 | 
|---|
| 718 | void CodeGenerator::postvisit( ast::VirtualCastExpr const * expr ) {
 | 
|---|
| 719 |         assertf( !options.genC, "VirtualCastExpr should not reach code generation." );
 | 
|---|
| 720 |         extension( expr );
 | 
|---|
| 721 |         // TODO: Is this busted?
 | 
|---|
| 722 |         output << "(virtual ";
 | 
|---|
| 723 |         expr->arg->accept( *visitor );
 | 
|---|
| 724 |         output << ")";
 | 
|---|
| 725 | }
 | 
|---|
| 726 | 
 | 
|---|
| 727 | void CodeGenerator::postvisit( ast::UntypedMemberExpr const * expr ) {
 | 
|---|
| 728 |         assertf( !options.genC, "UntypedMemberExpr should not reach code generation." );
 | 
|---|
| 729 |         extension( expr );
 | 
|---|
| 730 |         expr->aggregate->accept( *visitor );
 | 
|---|
| 731 |         output << ".";
 | 
|---|
| 732 |         expr->member->accept( *visitor );
 | 
|---|
| 733 | }
 | 
|---|
| 734 | 
 | 
|---|
| 735 | void CodeGenerator::postvisit( ast::MemberExpr const * expr ) {
 | 
|---|
| 736 |         extension( expr );
 | 
|---|
| 737 |         expr->aggregate->accept( *visitor );
 | 
|---|
| 738 |         output << "." << mangleName( expr->member );
 | 
|---|
| 739 | }
 | 
|---|
| 740 | 
 | 
|---|
| 741 | void CodeGenerator::postvisit( ast::VariableExpr const * expr ) {
 | 
|---|
| 742 |         extension( expr );
 | 
|---|
| 743 |         const OperatorInfo * opInfo;
 | 
|---|
| 744 |         if ( dynamic_cast<ast::ZeroType const *>( expr->var->get_type() ) ) {
 | 
|---|
| 745 |                 output << "0";
 | 
|---|
| 746 |         } else if ( expr->var->linkage == ast::Linkage::Intrinsic
 | 
|---|
| 747 |                         && ( opInfo = operatorLookup( expr->var->name ) )
 | 
|---|
| 748 |                         && opInfo->type == OT_CONSTANT ) {
 | 
|---|
| 749 |                 output << opInfo->symbol;
 | 
|---|
| 750 |         } else {
 | 
|---|
| 751 |                 output << mangleName( expr->var );
 | 
|---|
| 752 |         }
 | 
|---|
| 753 | }
 | 
|---|
| 754 | 
 | 
|---|
| 755 | void CodeGenerator::postvisit( ast::ConstantExpr const * expr ) {
 | 
|---|
| 756 |         extension( expr );
 | 
|---|
| 757 |         output << expr->rep;
 | 
|---|
| 758 | }
 | 
|---|
| 759 | 
 | 
|---|
| 760 | void CodeGenerator::postvisit( ast::SizeofExpr const * expr ) {
 | 
|---|
| 761 |         extension( expr );
 | 
|---|
| 762 |         output << "sizeof(";
 | 
|---|
| 763 |         if ( auto type = expr->type.as<ast::TypeofType>() ) {
 | 
|---|
| 764 |                 type->expr->accept( *visitor );
 | 
|---|
| 765 |         } else {
 | 
|---|
| 766 |                 output << genType( expr->type, "", options );
 | 
|---|
| 767 |         }
 | 
|---|
| 768 |         output << ")";
 | 
|---|
| 769 | }
 | 
|---|
| 770 | 
 | 
|---|
| 771 | void CodeGenerator::postvisit( ast::AlignofExpr const * expr ) {
 | 
|---|
| 772 |         // Using the GCC extension to avoid changing the std to C11.
 | 
|---|
| 773 |         extension( expr );
 | 
|---|
| 774 |         output << "__alignof__(";
 | 
|---|
| 775 |         if ( auto type = expr->type.as<ast::TypeofType>() ) {
 | 
|---|
| 776 |                 type->expr->accept( *visitor );
 | 
|---|
| 777 |         } else {
 | 
|---|
| 778 |                 output << genType( expr->type, "", options );
 | 
|---|
| 779 |         }
 | 
|---|
| 780 |         output << ")";
 | 
|---|
| 781 | }
 | 
|---|
| 782 | 
 | 
|---|
| 783 | void CodeGenerator::postvisit( ast::UntypedOffsetofExpr const * expr ) {
 | 
|---|
| 784 |         assertf( !options.genC, "UntypedOffsetofExpr should not reach code generation." );
 | 
|---|
| 785 |         output << "offsetof(";
 | 
|---|
| 786 |         output << genType( expr->type, "", options );
 | 
|---|
| 787 |         output << ", " << expr->member;
 | 
|---|
| 788 |         output << ")";
 | 
|---|
| 789 | }
 | 
|---|
| 790 | 
 | 
|---|
| 791 | void CodeGenerator::postvisit( ast::OffsetofExpr const * expr ) {
 | 
|---|
| 792 |         // Use GCC builtin
 | 
|---|
| 793 |         output << "__builtin_offsetof(";
 | 
|---|
| 794 |         output << genType( expr->type, "", options );
 | 
|---|
| 795 |         output << ", " << mangleName( expr->member );
 | 
|---|
| 796 |         output << ")";
 | 
|---|
| 797 | }
 | 
|---|
| 798 | 
 | 
|---|
| 799 | void CodeGenerator::postvisit( ast::OffsetPackExpr const * expr ) {
 | 
|---|
| 800 |         assertf( !options.genC, "OffsetPackExpr should not reach code generation." );
 | 
|---|
| 801 |         output << "__CFA_offsetpack(" << genType( expr->type, "", options ) << ")";
 | 
|---|
| 802 | }
 | 
|---|
| 803 | 
 | 
|---|
| 804 | void CodeGenerator::postvisit( ast::LogicalExpr const * expr ) {
 | 
|---|
| 805 |         extension( expr );
 | 
|---|
| 806 |         output << "(";
 | 
|---|
| 807 |         expr->arg1->accept( *visitor );
 | 
|---|
| 808 |         output << ( ( expr->isAnd ) ? " && " : " || " );
 | 
|---|
| 809 |         expr->arg2->accept( *visitor );
 | 
|---|
| 810 |         output << ")";
 | 
|---|
| 811 | }
 | 
|---|
| 812 | 
 | 
|---|
| 813 | void CodeGenerator::postvisit( ast::ConditionalExpr const * expr ) {
 | 
|---|
| 814 |         extension( expr );
 | 
|---|
| 815 |         output << "(";
 | 
|---|
| 816 |         expr->arg1->accept( *visitor );
 | 
|---|
| 817 |         output << " ? ";
 | 
|---|
| 818 |         expr->arg2->accept( *visitor );
 | 
|---|
| 819 |         output << " : ";
 | 
|---|
| 820 |         expr->arg3->accept( *visitor );
 | 
|---|
| 821 |         output << ")";
 | 
|---|
| 822 | }
 | 
|---|
| 823 | 
 | 
|---|
| 824 | void CodeGenerator::postvisit( ast::CommaExpr const * expr ) {
 | 
|---|
| 825 |         extension( expr );
 | 
|---|
| 826 |         output << "(";
 | 
|---|
| 827 |         if ( options.genC ) {
 | 
|---|
| 828 |                 // arg1 of a comma expression is never used, so it can be safely cast
 | 
|---|
| 829 |                 // to void to reduce gcc warnings.
 | 
|---|
| 830 |                 ast::ptr<ast::Expr> arg1 = new ast::CastExpr( expr->location, expr->arg1 );
 | 
|---|
| 831 |                 arg1->accept( *visitor );
 | 
|---|
| 832 |         } else {
 | 
|---|
| 833 |                 expr->arg1->accept( *visitor );
 | 
|---|
| 834 |         }
 | 
|---|
| 835 |         output << " , ";
 | 
|---|
| 836 |         expr->arg2->accept( *visitor );
 | 
|---|
| 837 |         output << ")";
 | 
|---|
| 838 | }
 | 
|---|
| 839 | 
 | 
|---|
| 840 | void CodeGenerator::postvisit( ast::TupleAssignExpr const * expr ) {
 | 
|---|
| 841 |         assertf( !options.genC, "TupleAssignExpr should not reach code generation." );
 | 
|---|
| 842 |         expr->stmtExpr->accept( *visitor );
 | 
|---|
| 843 | }
 | 
|---|
| 844 | 
 | 
|---|
| 845 | void CodeGenerator::postvisit( ast::UntypedTupleExpr const * expr ) {
 | 
|---|
| 846 |         assertf( !options.genC, "UntypedTupleExpr should not reach code generation." );
 | 
|---|
| 847 |         extension( expr );
 | 
|---|
| 848 |         output << "[";
 | 
|---|
| 849 |         genCommaList( expr->exprs );
 | 
|---|
| 850 |         output << "]";
 | 
|---|
| 851 | }
 | 
|---|
| 852 | 
 | 
|---|
| 853 | void CodeGenerator::postvisit( ast::TupleExpr const * expr ) {
 | 
|---|
| 854 |         assertf( !options.genC, "TupleExpr should not reach code generation." );
 | 
|---|
| 855 |         extension( expr );
 | 
|---|
| 856 |         output << "[";
 | 
|---|
| 857 |         genCommaList( expr->exprs );
 | 
|---|
| 858 |         output << "]";
 | 
|---|
| 859 | }
 | 
|---|
| 860 | 
 | 
|---|
| 861 | void CodeGenerator::postvisit( ast::TupleIndexExpr const * expr ) {
 | 
|---|
| 862 |         assertf( !options.genC, "TupleIndexExpr should not reach code generation." );
 | 
|---|
| 863 |         extension( expr );
 | 
|---|
| 864 |         expr->tuple->accept( *visitor );
 | 
|---|
| 865 |         output << "." << expr->index;
 | 
|---|
| 866 | }
 | 
|---|
| 867 | 
 | 
|---|
| 868 | void CodeGenerator::postvisit( ast::TypeExpr const * expr ) {
 | 
|---|
| 869 |         // TODO: Should there be an assertion there?
 | 
|---|
| 870 |         if ( !options.genC ) {
 | 
|---|
| 871 |                 output << genType( expr->type, "", options );
 | 
|---|
| 872 |         }
 | 
|---|
| 873 | }
 | 
|---|
| 874 | 
 | 
|---|
| 875 | void CodeGenerator::postvisit( ast::AsmExpr const * expr ) {
 | 
|---|
| 876 |         if ( !expr->inout.empty() ) {
 | 
|---|
| 877 |                 output << "[ " << expr->inout << " ] ";
 | 
|---|
| 878 |         }
 | 
|---|
| 879 |         expr->constraint->accept( *visitor );
 | 
|---|
| 880 |         output << " ( ";
 | 
|---|
| 881 |         expr->operand->accept( *visitor );
 | 
|---|
| 882 |         output << " )";
 | 
|---|
| 883 | }
 | 
|---|
| 884 | 
 | 
|---|
| 885 | void CodeGenerator::postvisit( ast::CompoundLiteralExpr const * expr ) {
 | 
|---|
| 886 |         //assert( expr->result && dynamic_cast<ast::ListInit const *>( expr->init ) );
 | 
|---|
| 887 |         assert( expr->result && expr->init.as<ast::ListInit>() );
 | 
|---|
| 888 |         output << "(" << genType( expr->result, "", options ) << ")";
 | 
|---|
| 889 |         expr->init->accept( *visitor );
 | 
|---|
| 890 | }
 | 
|---|
| 891 | 
 | 
|---|
| 892 | void CodeGenerator::postvisit( ast::UniqueExpr const * expr ) {
 | 
|---|
| 893 |         assertf( !options.genC, "UniqueExpr should not reach code generation." );
 | 
|---|
| 894 |         output << "unq<" << expr->id << ">{ ";
 | 
|---|
| 895 |         expr->expr->accept( *visitor );
 | 
|---|
| 896 |         output << " }";
 | 
|---|
| 897 | }
 | 
|---|
| 898 | 
 | 
|---|
| 899 | void CodeGenerator::postvisit( ast::StmtExpr const * expr ) {
 | 
|---|
| 900 |         auto stmts = expr->stmts->kids;
 | 
|---|
| 901 |         output << "({" << endl;
 | 
|---|
| 902 |         ++indent;
 | 
|---|
| 903 |         unsigned int numStmts = stmts.size();
 | 
|---|
| 904 |         unsigned int i = 0;
 | 
|---|
| 905 |         for ( ast::ptr<ast::Stmt> const & stmt : stmts ) {
 | 
|---|
| 906 |                 output << indent << printLabels( stmt->labels );
 | 
|---|
| 907 |                 if ( i + 1 == numStmts ) {
 | 
|---|
| 908 |                         // Last statement in a statement expression needs to be handled
 | 
|---|
| 909 |                         // specially - cannot cast to void, otherwise the expression
 | 
|---|
| 910 |                         // statement has no value.
 | 
|---|
| 911 |                         if ( ast::ExprStmt const * exprStmt = stmt.as<ast::ExprStmt>() ) {
 | 
|---|
| 912 |                                 exprStmt->expr->accept( *visitor );
 | 
|---|
| 913 |                                 output << ";" << endl;
 | 
|---|
| 914 |                                 ++i;
 | 
|---|
| 915 |                                 break;
 | 
|---|
| 916 |                         }
 | 
|---|
| 917 |                 }
 | 
|---|
| 918 |                 stmt->accept( *visitor );
 | 
|---|
| 919 |                 output << endl;
 | 
|---|
| 920 |                 if ( wantSpacing( stmt ) ) output << endl;
 | 
|---|
| 921 |                 ++i;
 | 
|---|
| 922 |         }
 | 
|---|
| 923 |         --indent;
 | 
|---|
| 924 |         output << indent << "})";
 | 
|---|
| 925 | }
 | 
|---|
| 926 | 
 | 
|---|
| 927 | void CodeGenerator::postvisit( ast::ConstructorExpr const * expr ) {
 | 
|---|
| 928 |         assertf( !options.genC, "ConstructorExpr should not reach code generation." );
 | 
|---|
| 929 |         expr->callExpr->accept( *visitor );
 | 
|---|
| 930 | }
 | 
|---|
| 931 | 
 | 
|---|
| 932 | void CodeGenerator::postvisit( ast::DeletedExpr const * expr ) {
 | 
|---|
| 933 |         assertf( !options.genC, "DeletedExpr should not reach code generation." );
 | 
|---|
| 934 |         expr->expr->accept( *visitor );
 | 
|---|
| 935 | }
 | 
|---|
| 936 | 
 | 
|---|
| 937 | void CodeGenerator::postvisit( ast::DefaultArgExpr const * expr ) {
 | 
|---|
| 938 |         assertf( !options.genC, "DefaultArgExpr should not reach code generation." );
 | 
|---|
| 939 |         expr->expr->accept( *visitor );
 | 
|---|
| 940 | }
 | 
|---|
| 941 | 
 | 
|---|
| 942 | void CodeGenerator::postvisit( ast::GenericExpr const * expr ) {
 | 
|---|
| 943 |         assertf( !options.genC, "GenericExpr should not reach code generation." );
 | 
|---|
| 944 |         output << "_Generic(";
 | 
|---|
| 945 |         expr->control->accept( *visitor );
 | 
|---|
| 946 |         output << ", ";
 | 
|---|
| 947 |         unsigned int numAssocs = expr->associations.size();
 | 
|---|
| 948 |         unsigned int i = 0;
 | 
|---|
| 949 |         for ( const ast::GenericExpr::Association & assoc : expr->associations ) {
 | 
|---|
| 950 |                 if ( nullptr == assoc.type ) {
 | 
|---|
| 951 |                         output << "default: ";
 | 
|---|
| 952 |                 } else {
 | 
|---|
| 953 |                         output << genType( assoc.type, "", options ) << ": ";
 | 
|---|
| 954 |                 }
 | 
|---|
| 955 |                 assoc.expr->accept( *visitor );
 | 
|---|
| 956 |                 ++i;
 | 
|---|
| 957 |                 if ( i != numAssocs ) output << ", ";
 | 
|---|
| 958 |         }
 | 
|---|
| 959 |         output << ")";
 | 
|---|
| 960 | }
 | 
|---|
| 961 | 
 | 
|---|
| 962 | void CodeGenerator::postvisit( ast::CompoundStmt const * stmt ) {
 | 
|---|
| 963 |         output << "{" << endl;
 | 
|---|
| 964 | 
 | 
|---|
| 965 |         ++indent;
 | 
|---|
| 966 |         for ( auto kid : stmt->kids ) {
 | 
|---|
| 967 |                 output << indent << printLabels( kid->labels );
 | 
|---|
| 968 |                 kid->accept( *visitor );
 | 
|---|
| 969 |                 output << endl;
 | 
|---|
| 970 |                 if ( wantSpacing( kid ) ) output << endl;
 | 
|---|
| 971 |         }
 | 
|---|
| 972 |         --indent;
 | 
|---|
| 973 | 
 | 
|---|
| 974 |         output << indent << "}";
 | 
|---|
| 975 | }
 | 
|---|
| 976 | 
 | 
|---|
| 977 | void CodeGenerator::postvisit( ast::ExprStmt const * stmt ) {
 | 
|---|
| 978 |         assert( stmt );
 | 
|---|
| 979 |         // Cast the top-level expression to void to reduce gcc warnings.
 | 
|---|
| 980 |         if ( options.genC ) {
 | 
|---|
| 981 |                 ast::ptr<ast::Expr> expr = new ast::CastExpr( stmt->location, stmt->expr );
 | 
|---|
| 982 |                 expr->accept( *visitor );
 | 
|---|
| 983 |         } else {
 | 
|---|
| 984 |                 stmt->expr->accept( *visitor );
 | 
|---|
| 985 |         }
 | 
|---|
| 986 |         output << ";";
 | 
|---|
| 987 | }
 | 
|---|
| 988 | 
 | 
|---|
| 989 | void CodeGenerator::postvisit( ast::AsmStmt const * stmt ) {
 | 
|---|
| 990 |         output << "asm ";
 | 
|---|
| 991 |         if ( stmt->isVolatile ) output << "volatile ";
 | 
|---|
| 992 |         if ( !stmt->gotoLabels.empty() ) output << "goto ";
 | 
|---|
| 993 |         output << "( ";
 | 
|---|
| 994 |         if ( stmt->instruction ) stmt->instruction->accept( *visitor );
 | 
|---|
| 995 |         output << " : ";
 | 
|---|
| 996 |         genCommaList( stmt->output );
 | 
|---|
| 997 |         output << " : ";
 | 
|---|
| 998 |         genCommaList( stmt->input );
 | 
|---|
| 999 |         output << " : ";
 | 
|---|
| 1000 |         genCommaList( stmt->clobber );
 | 
|---|
| 1001 |         if ( !stmt->gotoLabels.empty() ) {
 | 
|---|
| 1002 |                 output << " : ";
 | 
|---|
| 1003 |                 auto it = stmt->gotoLabels.begin();
 | 
|---|
| 1004 |                 while (true) {
 | 
|---|
| 1005 |                         output << *it++;
 | 
|---|
| 1006 |                         if ( stmt->gotoLabels.end() == it ) break;
 | 
|---|
| 1007 |                         output << ", ";
 | 
|---|
| 1008 |                 }
 | 
|---|
| 1009 |         }
 | 
|---|
| 1010 |         output << " );";
 | 
|---|
| 1011 | }
 | 
|---|
| 1012 | 
 | 
|---|
| 1013 | void CodeGenerator::postvisit( ast::AsmDecl const * decl ) {
 | 
|---|
| 1014 |         output << "asm ";
 | 
|---|
| 1015 |         ast::AsmStmt const * stmt = decl->stmt;
 | 
|---|
| 1016 |         output << "( ";
 | 
|---|
| 1017 |         if ( stmt->instruction ) stmt->instruction->accept( *visitor );
 | 
|---|
| 1018 |         output << " )";
 | 
|---|
| 1019 | }
 | 
|---|
| 1020 | 
 | 
|---|
| 1021 | void CodeGenerator::postvisit( ast::DirectiveDecl const * decl ) {
 | 
|---|
| 1022 |         // endl prevents spaces before the directive.
 | 
|---|
| 1023 |         output << endl << decl->stmt->directive;
 | 
|---|
| 1024 | }
 | 
|---|
| 1025 | 
 | 
|---|
| 1026 | void CodeGenerator::postvisit( ast::DirectiveStmt const * stmt ) {
 | 
|---|
| 1027 |         // endl prevents spaces before the directive.
 | 
|---|
| 1028 |         output << endl << stmt->directive;
 | 
|---|
| 1029 | }
 | 
|---|
| 1030 | 
 | 
|---|
| 1031 | void CodeGenerator::postvisit( ast::IfStmt const * stmt ) {
 | 
|---|
| 1032 |         output << "if ( ";
 | 
|---|
| 1033 |         stmt->cond->accept( *visitor );
 | 
|---|
| 1034 |         output << " ) ";
 | 
|---|
| 1035 | 
 | 
|---|
| 1036 |         stmt->then->accept( *visitor );
 | 
|---|
| 1037 | 
 | 
|---|
| 1038 |         if ( nullptr != stmt->else_ ) {
 | 
|---|
| 1039 |                 output << " else ";
 | 
|---|
| 1040 |                 stmt->else_->accept( *visitor );
 | 
|---|
| 1041 |         }
 | 
|---|
| 1042 | }
 | 
|---|
| 1043 | 
 | 
|---|
| 1044 | void CodeGenerator::postvisit( ast::SwitchStmt const * stmt ) {
 | 
|---|
| 1045 |         output << "switch ( ";
 | 
|---|
| 1046 |         stmt->cond->accept( *visitor );
 | 
|---|
| 1047 |         output << " ) ";
 | 
|---|
| 1048 | 
 | 
|---|
| 1049 |         output << "{" << endl;
 | 
|---|
| 1050 |         ++indent;
 | 
|---|
| 1051 |         for ( auto node : stmt->cases ) {
 | 
|---|
| 1052 |                 node->accept( *visitor );
 | 
|---|
| 1053 |         }
 | 
|---|
| 1054 |         --indent;
 | 
|---|
| 1055 |         output << indent << "}";
 | 
|---|
| 1056 | }
 | 
|---|
| 1057 | 
 | 
|---|
| 1058 | void CodeGenerator::postvisit( ast::CaseClause const * clause ) {
 | 
|---|
| 1059 |         updateLocation( clause );
 | 
|---|
| 1060 |         output << indent;
 | 
|---|
| 1061 |         if ( clause->isDefault() ) {
 | 
|---|
| 1062 |                 output << "default";
 | 
|---|
| 1063 |         } else {
 | 
|---|
| 1064 |                 output << "case ";
 | 
|---|
| 1065 |                 clause->cond->accept( *visitor );
 | 
|---|
| 1066 |         }
 | 
|---|
| 1067 |         output << ":" << endl;
 | 
|---|
| 1068 | 
 | 
|---|
| 1069 |         ++indent;
 | 
|---|
| 1070 |         for ( auto stmt : clause->stmts ) {
 | 
|---|
| 1071 |                 output << indent << printLabels( stmt->labels ) ;
 | 
|---|
| 1072 |                 stmt->accept( *visitor );
 | 
|---|
| 1073 |                 output << endl;
 | 
|---|
| 1074 |         }
 | 
|---|
| 1075 |         --indent;
 | 
|---|
| 1076 | }
 | 
|---|
| 1077 | 
 | 
|---|
| 1078 | void CodeGenerator::postvisit( ast::BranchStmt const * stmt ) {
 | 
|---|
| 1079 |         switch ( stmt->kind ) {
 | 
|---|
| 1080 |         case ast::BranchStmt::Goto:
 | 
|---|
| 1081 |                 if ( !stmt->target.empty() ) {
 | 
|---|
| 1082 |                         output << "goto " << stmt->target;
 | 
|---|
| 1083 |                 } else if ( nullptr != stmt->computedTarget ) {
 | 
|---|
| 1084 |                         output << "goto *";
 | 
|---|
| 1085 |                         stmt->computedTarget->accept( *visitor );
 | 
|---|
| 1086 |                 }
 | 
|---|
| 1087 |                 break;
 | 
|---|
| 1088 |         case ast::BranchStmt::Break:
 | 
|---|
| 1089 |                 output << "break";
 | 
|---|
| 1090 |                 break;
 | 
|---|
| 1091 |         case ast::BranchStmt::Continue:
 | 
|---|
| 1092 |                 output << "continue";
 | 
|---|
| 1093 |                 break;
 | 
|---|
| 1094 |         case ast::BranchStmt::FallThrough:
 | 
|---|
| 1095 |         case ast::BranchStmt::FallThroughDefault:
 | 
|---|
| 1096 |                 assertf( !options.genC, "fallthru should not reach code generation." );
 | 
|---|
| 1097 |                 output << "fallthru";
 | 
|---|
| 1098 |                 break;
 | 
|---|
| 1099 |         default:
 | 
|---|
| 1100 |                 assertf( false, "Bad BranchStmt value." );
 | 
|---|
| 1101 |         }
 | 
|---|
| 1102 |         // Print branch target for labelled break/continue/fallthru in debug mode.
 | 
|---|
| 1103 |         if ( !options.genC && stmt->kind != ast::BranchStmt::Goto ) {
 | 
|---|
| 1104 |                 if ( !stmt->target.empty() ) {
 | 
|---|
| 1105 |                         output << " " << stmt->target;
 | 
|---|
| 1106 |                 } else if ( stmt->kind == ast::BranchStmt::FallThrough ) {
 | 
|---|
| 1107 |                         output << " default";
 | 
|---|
| 1108 |                 }
 | 
|---|
| 1109 |         }
 | 
|---|
| 1110 |         output << ";";
 | 
|---|
| 1111 | }
 | 
|---|
| 1112 | 
 | 
|---|
| 1113 | void CodeGenerator::postvisit( ast::ReturnStmt const * stmt ) {
 | 
|---|
| 1114 |         output << "return ";
 | 
|---|
| 1115 |         if ( stmt->expr ) stmt->expr->accept( *visitor );
 | 
|---|
| 1116 |         output << ";";
 | 
|---|
| 1117 | }
 | 
|---|
| 1118 | 
 | 
|---|
| 1119 | void CodeGenerator::postvisit( ast::ThrowStmt const * stmt ) {
 | 
|---|
| 1120 |         assertf( !options.genC, "ThrowStmt should not reach code generation." );
 | 
|---|
| 1121 | 
 | 
|---|
| 1122 |         output << ((stmt->kind == ast::Terminate) ? "throw" : "throwResume");
 | 
|---|
| 1123 |         if ( stmt->expr ) {
 | 
|---|
| 1124 |                 output << " ";
 | 
|---|
| 1125 |                 stmt->expr->accept( *visitor );
 | 
|---|
| 1126 |         }
 | 
|---|
| 1127 |         if ( stmt->target ) {
 | 
|---|
| 1128 |                 output << " _At ";
 | 
|---|
| 1129 |                 stmt->target->accept( *visitor );
 | 
|---|
| 1130 |         }
 | 
|---|
| 1131 |         output << ";";
 | 
|---|
| 1132 | }
 | 
|---|
| 1133 | 
 | 
|---|
| 1134 | void CodeGenerator::postvisit( ast::CatchClause const * stmt ) {
 | 
|---|
| 1135 |         assertf( !options.genC, "CatchClause should not reach code generation." );
 | 
|---|
| 1136 | 
 | 
|---|
| 1137 |         output << ((stmt->kind == ast::Terminate) ? "catch" : "catchResume");
 | 
|---|
| 1138 |         output << "( ";
 | 
|---|
| 1139 |         stmt->decl->accept( *visitor );
 | 
|---|
| 1140 |         if ( stmt->cond ) {
 | 
|---|
| 1141 |                 output << " ; ";
 | 
|---|
| 1142 |                 stmt->cond->accept( *visitor );
 | 
|---|
| 1143 |         }
 | 
|---|
| 1144 |         output << " ) ";
 | 
|---|
| 1145 |         stmt->body->accept( *visitor );
 | 
|---|
| 1146 | }
 | 
|---|
| 1147 | 
 | 
|---|
| 1148 | void CodeGenerator::postvisit( ast::WaitForStmt const * stmt ) {
 | 
|---|
| 1149 |         assertf( !options.genC, "WaitforStmt should not reach code generation." );
 | 
|---|
| 1150 | 
 | 
|---|
| 1151 |         bool first = true;
 | 
|---|
| 1152 |         for ( ast::ptr<ast::WaitForClause> const & clause : stmt->clauses ) {
 | 
|---|
| 1153 |                 if (first) { output << "or "; first = false; }
 | 
|---|
| 1154 |                 if ( clause->when_cond ) {
 | 
|---|
| 1155 |                         output << "when(";
 | 
|---|
| 1156 |                         clause->when_cond->accept( *visitor );
 | 
|---|
| 1157 |                         output << ") ";
 | 
|---|
| 1158 |                 }
 | 
|---|
| 1159 |                 output << "waitfor(";
 | 
|---|
| 1160 |                 clause->target->accept( *visitor );
 | 
|---|
| 1161 |                 for ( ast::ptr<ast::Expr> const & expr : clause->target_args ) {
 | 
|---|
| 1162 |                         output << ",";
 | 
|---|
| 1163 |                         expr->accept( *visitor );
 | 
|---|
| 1164 |                 }
 | 
|---|
| 1165 |                 output << ") ";
 | 
|---|
| 1166 |                 clause->stmt->accept( *visitor );
 | 
|---|
| 1167 |         }
 | 
|---|
| 1168 | 
 | 
|---|
| 1169 |         if ( stmt->timeout_stmt ) {
 | 
|---|
| 1170 |                 output << "or ";
 | 
|---|
| 1171 |                 if ( stmt->timeout_cond ) {
 | 
|---|
| 1172 |                         output << "when(";
 | 
|---|
| 1173 |                         stmt->timeout_cond->accept( *visitor );
 | 
|---|
| 1174 |                         output << ") ";
 | 
|---|
| 1175 |                 }
 | 
|---|
| 1176 |                 output << "timeout(";
 | 
|---|
| 1177 |                 stmt->timeout_time->accept( *visitor );
 | 
|---|
| 1178 |                 output << ") ";
 | 
|---|
| 1179 |                 stmt->timeout_stmt->accept( *visitor );
 | 
|---|
| 1180 |         }
 | 
|---|
| 1181 | 
 | 
|---|
| 1182 |         if ( stmt->else_stmt ) {
 | 
|---|
| 1183 |                 output << "or ";
 | 
|---|
| 1184 |                 if ( stmt->else_cond ) {
 | 
|---|
| 1185 |                         output << "when(";
 | 
|---|
| 1186 |                         stmt->else_cond->accept( *visitor );
 | 
|---|
| 1187 |                         output << ")";
 | 
|---|
| 1188 |                 }
 | 
|---|
| 1189 |                 output << "else ";
 | 
|---|
| 1190 |                 stmt->else_stmt->accept( *visitor );
 | 
|---|
| 1191 |         }
 | 
|---|
| 1192 | }
 | 
|---|
| 1193 | 
 | 
|---|
| 1194 | void CodeGenerator::postvisit( ast::WithStmt const * stmt ) {
 | 
|---|
| 1195 |         assertf( !options.genC, "WithStmt should not reach code generation." );
 | 
|---|
| 1196 | 
 | 
|---|
| 1197 |         output << "with ( ";
 | 
|---|
| 1198 |         genCommaList( stmt->exprs );
 | 
|---|
| 1199 |         output << " ) ";
 | 
|---|
| 1200 |         stmt->stmt->accept( *visitor );
 | 
|---|
| 1201 | }
 | 
|---|
| 1202 | 
 | 
|---|
| 1203 | void CodeGenerator::postvisit( ast::WhileDoStmt const * stmt ) {
 | 
|---|
| 1204 |         if ( stmt->isDoWhile ) {
 | 
|---|
| 1205 |                 output << "do";
 | 
|---|
| 1206 |         } else {
 | 
|---|
| 1207 |                 output << "while (";
 | 
|---|
| 1208 |                 stmt->cond->accept( *visitor );
 | 
|---|
| 1209 |                 output << ")";
 | 
|---|
| 1210 |         }
 | 
|---|
| 1211 |         output << " ";
 | 
|---|
| 1212 | 
 | 
|---|
| 1213 |         output << CodeGenerator::printLabels( stmt->body->labels );
 | 
|---|
| 1214 |         stmt->body->accept( *visitor );
 | 
|---|
| 1215 | 
 | 
|---|
| 1216 |         if ( stmt->isDoWhile ) {
 | 
|---|
| 1217 |                 output << " while (";
 | 
|---|
| 1218 |                 stmt->cond->accept( *visitor );
 | 
|---|
| 1219 |                 output << ( ( nullptr == stmt->else_ ) ? ");" : ")" );
 | 
|---|
| 1220 |         }
 | 
|---|
| 1221 |         if ( stmt->else_ ) {
 | 
|---|
| 1222 |                 output << " else ";
 | 
|---|
| 1223 |                 stmt->else_->accept( *visitor );
 | 
|---|
| 1224 |         }
 | 
|---|
| 1225 | }
 | 
|---|
| 1226 | 
 | 
|---|
| 1227 | void CodeGenerator::postvisit( ast::ForStmt const * stmt ) {
 | 
|---|
| 1228 |         // Initializer is always hoised so don't generate it.
 | 
|---|
| 1229 |         // TODO: Do an assertion check?
 | 
|---|
| 1230 |         output << "for (;";
 | 
|---|
| 1231 | 
 | 
|---|
| 1232 |         if ( nullptr != stmt->cond ) {
 | 
|---|
| 1233 |                 stmt->cond->accept( *visitor );
 | 
|---|
| 1234 |         }
 | 
|---|
| 1235 |         output << ";";
 | 
|---|
| 1236 | 
 | 
|---|
| 1237 |         if ( nullptr != stmt->inc ) {
 | 
|---|
| 1238 |                 // cast the top-level expression to void to reduce gcc warnings.
 | 
|---|
| 1239 |                 ast::Expr * expr = new ast::CastExpr( stmt->inc );
 | 
|---|
| 1240 |                 expr->accept( *visitor );
 | 
|---|
| 1241 |         }
 | 
|---|
| 1242 |         output << ") ";
 | 
|---|
| 1243 | 
 | 
|---|
| 1244 |         if ( nullptr != stmt->body ) {
 | 
|---|
| 1245 |                 output << printLabels( stmt->body->labels );
 | 
|---|
| 1246 |                 stmt->body->accept( *visitor );
 | 
|---|
| 1247 |         }
 | 
|---|
| 1248 | 
 | 
|---|
| 1249 |         if ( nullptr != stmt->else_ ) {
 | 
|---|
| 1250 |                 assertf( !options.genC, "Loop else should not reach code generation." );
 | 
|---|
| 1251 |                 output << " else ";
 | 
|---|
| 1252 |                 stmt->else_->accept( *visitor );
 | 
|---|
| 1253 |         }
 | 
|---|
| 1254 | }
 | 
|---|
| 1255 | 
 | 
|---|
| 1256 | void CodeGenerator::postvisit( ast::NullStmt const * ) {
 | 
|---|
| 1257 |         output << "/* null statement */ ;";
 | 
|---|
| 1258 | }
 | 
|---|
| 1259 | 
 | 
|---|
| 1260 | void CodeGenerator::postvisit( ast::DeclStmt const * stmt ) {
 | 
|---|
| 1261 |         stmt->decl->accept( *visitor );
 | 
|---|
| 1262 | 
 | 
|---|
| 1263 |         if ( doSemicolon( stmt->decl ) ) output << ";";
 | 
|---|
| 1264 | }
 | 
|---|
| 1265 | 
 | 
|---|
| 1266 | void CodeGenerator::postvisit( ast::ImplicitCtorDtorStmt const * stmt ) {
 | 
|---|
| 1267 |         assertf( !options.genC, "ImplicitCtorCtorStmt should not reach code generation." );
 | 
|---|
| 1268 |         stmt->callStmt->accept( *visitor );
 | 
|---|
| 1269 | }
 | 
|---|
| 1270 | 
 | 
|---|
| 1271 | void CodeGenerator::postvisit( ast::MutexStmt const * stmt ) {
 | 
|---|
| 1272 |         assertf( !options.genC, "MutexStmt should not reach code generation." );
 | 
|---|
| 1273 |         // TODO: But this isn't what a mutex statement looks like.
 | 
|---|
| 1274 |         stmt->stmt->accept( *visitor );
 | 
|---|
| 1275 | }
 | 
|---|
| 1276 | 
 | 
|---|
| 1277 | std::string genName( ast::DeclWithType const * decl ) {
 | 
|---|
| 1278 |         if ( const OperatorInfo * opInfo = operatorLookup( decl->name ) ) {
 | 
|---|
| 1279 |                 return opInfo->outputName;
 | 
|---|
| 1280 |         } else {
 | 
|---|
| 1281 |                 return decl->name;
 | 
|---|
| 1282 |         }
 | 
|---|
| 1283 | }
 | 
|---|
| 1284 | 
 | 
|---|
| 1285 | } // namespace CodeGen
 | 
|---|