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