Changes in src/CodeGen/CodeGenerator.cc [0dd18fd:a5f0529]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r0dd18fd ra5f0529 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Aug 18 15:34:00 201713 // Update Count : 48 812 // Last Modified On : Tus Jul 25 15:29:00 2017 13 // Update Count : 486 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 66 74 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { 67 75 labels = &l; … … 79 87 } 80 88 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; 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; 99 108 } 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 ) {} 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 ) {} 114 112 115 113 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 202 200 output << " {" << endl; 203 201 204 ++indent;202 cur_indent += CodeGenerator::tabsize; 205 203 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 206 updateLocation( *i );204 output << lineDirective( *i ) << indent; 207 205 (*i)->accept( *this ); 208 206 output << ";" << endl; 209 207 } // for 210 208 211 --indent;209 cur_indent -= CodeGenerator::tabsize; 212 210 213 211 output << indent << "}"; … … 227 225 void CodeGenerator::visit( EnumDecl * enumDecl ) { 228 226 extension( enumDecl ); 229 updateLocation( enumDecl );227 output << lineDirective ( enumDecl ); 230 228 output << "enum "; 231 229 genAttributes( enumDecl->get_attributes() ); … … 239 237 output << " {" << endl; 240 238 241 ++indent;239 cur_indent += CodeGenerator::tabsize; 242 240 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 243 241 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 244 242 assert( obj ); 245 updateLocation( obj ); 246 output << mangleName( obj ); 243 output << lineDirective( obj ) << indent << mangleName( obj ); 247 244 if ( obj->get_init() ) { 248 245 output << " = "; … … 252 249 } // for 253 250 254 --indent;251 cur_indent -= CodeGenerator::tabsize; 255 252 256 253 output << indent << "}"; … … 262 259 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 263 260 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 264 updateLocation( typeDecl );261 output << lineDirective( typeDecl ); 265 262 output << "typedef "; 266 263 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; … … 763 760 void CodeGenerator::visit( StmtExpr * stmtExpr ) { 764 761 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 765 updateLocation( stmtExpr ); 766 output << "({" << std::endl; 767 ++indent; 762 output << lineDirective( stmtExpr) << "({" << std::endl; 763 cur_indent += CodeGenerator::tabsize; 768 764 unsigned int numStmts = stmts.size(); 769 765 unsigned int i = 0; 770 766 for ( Statement * stmt : stmts ) { 771 updateLocation( stmt );767 output << lineDirective( stmt ) << indent; 772 768 output << printLabels( stmt->get_labels() ); 773 769 if ( i+1 == numStmts ) { … … 788 784 ++i; 789 785 } 790 --indent;786 cur_indent -= CodeGenerator::tabsize; 791 787 output << indent << "})"; 792 788 } … … 797 793 output << "{" << endl; 798 794 799 ++indent;795 cur_indent += CodeGenerator::tabsize; 800 796 801 797 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { … … 808 804 } // if 809 805 } // for 810 --indent;806 cur_indent -= CodeGenerator::tabsize; 811 807 812 808 output << indent << "}"; … … 856 852 857 853 void CodeGenerator::visit( IfStmt * ifStmt ) { 858 updateLocation( ifStmt );854 output << lineDirective( ifStmt ); 859 855 output << "if ( "; 860 856 ifStmt->get_condition()->accept( *this ); … … 870 866 871 867 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 872 updateLocation( switchStmt );868 output << lineDirective( switchStmt ); 873 869 output << "switch ( " ; 874 870 switchStmt->get_condition()->accept( *this ); … … 876 872 877 873 output << "{" << std::endl; 878 ++indent;874 cur_indent += CodeGenerator::tabsize; 879 875 acceptAll( switchStmt->get_statements(), *this ); 880 --indent;876 cur_indent -= CodeGenerator::tabsize; 881 877 output << indent << "}"; 882 878 } 883 879 884 880 void CodeGenerator::visit( CaseStmt * caseStmt ) { 885 updateLocation( caseStmt ); 881 output << lineDirective( caseStmt ); 882 output << indent; 886 883 if ( caseStmt->isDefault()) { 887 884 output << "default"; … … 894 891 std::list<Statement *> sts = caseStmt->get_statements(); 895 892 896 ++indent;893 cur_indent += CodeGenerator::tabsize; 897 894 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 898 895 output << indent << printLabels( (*i)->get_labels() ) ; … … 900 897 output << endl; 901 898 } // for 902 --indent;899 cur_indent -= CodeGenerator::tabsize; 903 900 } 904 901
Note:
See TracChangeset
for help on using the changeset viewer.