Changes in src/CodeGen/CodeGenerator.cc [5f642e38:29cf9c8]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r5f642e38 r29cf9c8 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : T hu Mar 30 16:38:01201713 // Update Count : 48 211 // Last Modified By : Andrew Beach 12 // Last Modified On : Tus May 9 16:50:00 2017 13 // Update Count : 484 14 14 // 15 15 … … 41 41 namespace CodeGen { 42 42 int CodeGenerator::tabsize = 4; 43 44 // Pseudo Function: output << lineDirective(currentNode); 45 struct lineDirective { 46 CodeLocation const & loc; 47 lineDirective(CodeLocation const & location) : loc(location) {} 48 lineDirective(BaseSyntaxNode const * node) : loc(node->location) {} 49 }; 50 std::ostream & operator<<(std::ostream & out, lineDirective const & ld) { 51 if (ld.loc.isSet()) 52 return out << "\n# " << ld.loc.linenumber << " \"" 53 << ld.loc.filename << "\"\n"; 54 return out << "\n// Unset Location\n"; 55 } 43 56 44 57 // the kinds of statements that would ideally be followed by whitespace … … 128 141 129 142 130 // *** Declarations143 // *** Declarations 131 144 void CodeGenerator::visit( FunctionDecl * functionDecl ) { 145 output << lineDirective( functionDecl ); 146 132 147 extension( functionDecl ); 133 148 genAttributes( functionDecl->get_attributes() ); … … 153 168 } 154 169 170 output << lineDirective( objectDecl ); 171 155 172 extension( objectDecl ); 156 173 genAttributes( objectDecl->get_attributes() ); … … 192 209 cur_indent += CodeGenerator::tabsize; 193 210 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 194 output << indent;211 output << lineDirective( *i ) << indent; 195 212 (*i)->accept( *this ); 196 213 output << ";" << endl; … … 204 221 205 222 void CodeGenerator::visit( StructDecl * structDecl ) { 223 output << lineDirective( structDecl ); 224 206 225 extension( structDecl ); 207 226 handleAggregate( structDecl, "struct " ); … … 209 228 210 229 void CodeGenerator::visit( UnionDecl * unionDecl ) { 230 output << lineDirective( unionDecl ); 231 211 232 extension( unionDecl ); 212 233 handleAggregate( unionDecl, "union " ); … … 215 236 void CodeGenerator::visit( EnumDecl * enumDecl ) { 216 237 extension( enumDecl ); 238 output << lineDirective ( enumDecl ); 217 239 output << "enum "; 218 240 genAttributes( enumDecl->get_attributes() ); … … 230 252 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 231 253 assert( obj ); 232 output << indent << mangleName( obj );254 output << lineDirective( obj ) << indent << mangleName( obj ); 233 255 if ( obj->get_init() ) { 234 256 output << " = "; … … 248 270 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 249 271 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 272 output << lineDirective( typeDecl ); 250 273 output << "typedef "; 251 274 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; … … 262 285 } // if 263 286 } else { 264 output << typeDecl->typeString() << " " << typeDecl->get_name(); 287 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); 288 if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) { 289 output << " | sized(" << typeDecl->get_name() << ")"; 290 } 265 291 if ( ! typeDecl->get_assertions().empty() ) { 266 292 output << " | { "; … … 316 342 } 317 343 318 // *** Expressions344 // *** Expressions 319 345 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 320 346 extension( applicationExpr ); … … 719 745 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 720 746 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 721 output << "({" << std::endl;747 output << lineDirective( stmtExpr) << "({" << std::endl; 722 748 cur_indent += CodeGenerator::tabsize; 723 749 unsigned int numStmts = stmts.size(); 724 750 unsigned int i = 0; 725 751 for ( Statement * stmt : stmts ) { 726 output << indent << printLabels( stmt->get_labels() ); 752 output << lineDirective( stmt ) << indent; 753 output << printLabels( stmt->get_labels() ); 727 754 if ( i+1 == numStmts ) { 728 755 // last statement in a statement expression needs to be handled specially - … … 746 773 } 747 774 748 // *** Statements775 // *** Statements 749 776 void CodeGenerator::visit( CompoundStmt * compoundStmt ) { 750 777 std::list<Statement*> ks = compoundStmt->get_kids(); … … 769 796 void CodeGenerator::visit( ExprStmt * exprStmt ) { 770 797 assert( exprStmt ); 771 // cast the top-level expression to void to reduce gcc warnings. 772 Expression * expr = new CastExpr( exprStmt->get_expr() ); 798 Expression * expr = exprStmt->get_expr(); 799 if ( genC ) { 800 // cast the top-level expression to void to reduce gcc warnings. 801 expr = new CastExpr( expr ); 802 } 773 803 expr->accept( *this ); 774 804 output << ";"; … … 807 837 808 838 void CodeGenerator::visit( IfStmt * ifStmt ) { 839 output << lineDirective( ifStmt ); 809 840 output << "if ( "; 810 841 ifStmt->get_condition()->accept( *this ); … … 820 851 821 852 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 853 output << lineDirective( switchStmt ); 822 854 output << "switch ( " ; 823 855 switchStmt->get_condition()->accept( *this ); … … 832 864 833 865 void CodeGenerator::visit( CaseStmt * caseStmt ) { 866 output << lineDirective( caseStmt ); 834 867 output << indent; 835 868 if ( caseStmt->isDefault()) {
Note: See TracChangeset
for help on using the changeset viewer.