Changes in src/CodeGen/CodeGenerator.cc [a5f0529:0dd18fd]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
ra5f0529 r0dd18fd 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Tus Jul 25 15:29:00 201713 // Update Count : 48 612 // Last Modified On : Fri Aug 18 15:34:00 2017 13 // Update Count : 488 14 14 // 15 15 #include "CodeGenerator.h" … … 64 64 } // extension 65 65 66 ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {67 return output << string( cg.cur_indent, ' ' );68 }69 70 ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {71 return indent( output );72 }73 74 66 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { 75 67 labels = &l; … … 87 79 } 88 80 89 CodeGenerator::LineMarker::LineMarker( 90 CodeLocation const & loc, bool toPrint) : 91 loc(loc), toPrint(toPrint) 92 {} 93 94 CodeGenerator::LineMarker CodeGenerator::lineDirective( 95 BaseSyntaxNode const * node) { 96 return LineMarker(node->location, lineMarks); 97 } 98 99 std::ostream & operator<<(std::ostream & out, 100 CodeGenerator::LineMarker const & marker) { 101 if (marker.toPrint && marker.loc.isSet()) { 102 return out << "\n# " << marker.loc.linenumber << " \"" 103 << marker.loc.filename << "\"\n"; 104 } else if (marker.toPrint) { 105 return out << "\n/* Missing CodeLocation */\n"; 106 } else { 107 return out; 81 /* Using updateLocation at the beginning of a node and nextLine 82 * within a node should become the method of formating. 83 */ 84 void CodeGenerator::updateLocation( CodeLocation const & to ) { 85 if ( !lineMarks ) { 86 return; 87 } else if ( currentLocation.followedBy( to, 0 ) ) { 88 return; 89 } else if ( currentLocation.followedBy( to, 1 ) ) { 90 output << "\n" << indent; 91 currentLocation.linenumber += 1; 92 } else if ( currentLocation.followedBy( to, 2 ) ) { 93 output << "\n\n" << indent; 94 currentLocation.linenumber += 2; 95 } else { 96 output << "\n# " << to.linenumber << " \"" << to.filename 97 << "\"\n" << indent; 98 currentLocation = to; 108 99 } 109 } 110 111 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} 100 output << std::flush; 101 } 102 103 void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) { 104 updateLocation( to->location ); 105 } 106 107 void CodeGenerator::nextLine() { 108 if ( !lineMarks ) { 109 output << "\n" << indent << std::flush; 110 } 111 } 112 113 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {} 112 114 113 115 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 200 202 output << " {" << endl; 201 203 202 cur_indent += CodeGenerator::tabsize;204 ++indent; 203 205 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 204 output << lineDirective( *i ) << indent;206 updateLocation( *i ); 205 207 (*i)->accept( *this ); 206 208 output << ";" << endl; 207 209 } // for 208 210 209 cur_indent -= CodeGenerator::tabsize;211 --indent; 210 212 211 213 output << indent << "}"; … … 225 227 void CodeGenerator::visit( EnumDecl * enumDecl ) { 226 228 extension( enumDecl ); 227 output << lineDirective( enumDecl );229 updateLocation( enumDecl ); 228 230 output << "enum "; 229 231 genAttributes( enumDecl->get_attributes() ); … … 237 239 output << " {" << endl; 238 240 239 cur_indent += CodeGenerator::tabsize;241 ++indent; 240 242 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 241 243 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 242 244 assert( obj ); 243 output << lineDirective( obj ) << indent << mangleName( obj ); 245 updateLocation( obj ); 246 output << mangleName( obj ); 244 247 if ( obj->get_init() ) { 245 248 output << " = "; … … 249 252 } // for 250 253 251 cur_indent -= CodeGenerator::tabsize;254 --indent; 252 255 253 256 output << indent << "}"; … … 259 262 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 260 263 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 261 output << lineDirective( typeDecl );264 updateLocation( typeDecl ); 262 265 output << "typedef "; 263 266 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; … … 760 763 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 761 764 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 762 output << lineDirective( stmtExpr) << "({" << std::endl; 763 cur_indent += CodeGenerator::tabsize; 765 updateLocation( stmtExpr ); 766 output << "({" << std::endl; 767 ++indent; 764 768 unsigned int numStmts = stmts.size(); 765 769 unsigned int i = 0; 766 770 for ( Statement * stmt : stmts ) { 767 output << lineDirective( stmt ) << indent;771 updateLocation( stmt ); 768 772 output << printLabels( stmt->get_labels() ); 769 773 if ( i+1 == numStmts ) { … … 784 788 ++i; 785 789 } 786 cur_indent -= CodeGenerator::tabsize;790 --indent; 787 791 output << indent << "})"; 788 792 } … … 793 797 output << "{" << endl; 794 798 795 cur_indent += CodeGenerator::tabsize;799 ++indent; 796 800 797 801 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { … … 804 808 } // if 805 809 } // for 806 cur_indent -= CodeGenerator::tabsize;810 --indent; 807 811 808 812 output << indent << "}"; … … 852 856 853 857 void CodeGenerator::visit( IfStmt * ifStmt ) { 854 output << lineDirective( ifStmt );858 updateLocation( ifStmt ); 855 859 output << "if ( "; 856 860 ifStmt->get_condition()->accept( *this ); … … 866 870 867 871 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 868 output << lineDirective( switchStmt );872 updateLocation( switchStmt ); 869 873 output << "switch ( " ; 870 874 switchStmt->get_condition()->accept( *this ); … … 872 876 873 877 output << "{" << std::endl; 874 cur_indent += CodeGenerator::tabsize;878 ++indent; 875 879 acceptAll( switchStmt->get_statements(), *this ); 876 cur_indent -= CodeGenerator::tabsize;880 --indent; 877 881 output << indent << "}"; 878 882 } 879 883 880 884 void CodeGenerator::visit( CaseStmt * caseStmt ) { 881 output << lineDirective( caseStmt ); 882 output << indent; 885 updateLocation( caseStmt ); 883 886 if ( caseStmt->isDefault()) { 884 887 output << "default"; … … 891 894 std::list<Statement *> sts = caseStmt->get_statements(); 892 895 893 cur_indent += CodeGenerator::tabsize;896 ++indent; 894 897 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 895 898 output << indent << printLabels( (*i)->get_labels() ) ; … … 897 900 output << endl; 898 901 } // for 899 cur_indent -= CodeGenerator::tabsize;902 --indent; 900 903 } 901 904
Note:
See TracChangeset
for help on using the changeset viewer.