Changeset d22e90f
- Timestamp:
- Sep 20, 2017, 11:47:26 AM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 47b5b63
- Parents:
- a9a4771
- git-author:
- Rob Schluntz <rschlunt@…> (09/20/17 11:46:26)
- git-committer:
- Rob Schluntz <rschlunt@…> (09/20/17 11:47:26)
- Location:
- src/CodeGen
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
ra9a4771 rd22e90f 79 79 } 80 80 81 /* Using updateLocation at the beginning of a node and nextLine81 /* Using updateLocation at the beginning of a node and endl 82 82 * within a node should become the method of formating. 83 83 */ 84 84 void CodeGenerator::updateLocation( CodeLocation const & to ) { 85 if ( !lineMarks ) { 86 return; 87 } else if ( currentLocation.followedBy( to, 0 ) ) { 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 ) ) { 88 89 return; 89 90 } else if ( currentLocation.followedBy( to, 1 ) ) { … … 105 106 } 106 107 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 ) {} 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; 117 } 118 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 ) {} 114 120 115 121 string CodeGenerator::mangleName( DeclarationWithType * decl ) { … … 140 146 141 147 // *** BaseSyntaxNode 142 void CodeGenerator::previsit( BaseSyntaxNode * ) {148 void CodeGenerator::previsit( BaseSyntaxNode * node ) { 143 149 // turn off automatic recursion for all nodes, to allow each visitor to 144 150 // precisely control the order in which its children are visited. 145 151 visit_children = false; 152 updateLocation( node ); 146 153 } 147 154 … … 165 172 asmName( functionDecl ); 166 173 167 // acceptAll( functionDecl->get_oldDecls(), *visitor );168 174 if ( functionDecl->get_statements() ) { 169 175 functionDecl->get_statements()->accept( *visitor ); … … 216 222 ++indent; 217 223 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) { 218 updateLocation( *i );219 224 output << indent; 220 225 (*i)->accept( *visitor ); … … 240 245 void CodeGenerator::postvisit( EnumDecl * enumDecl ) { 241 246 extension( enumDecl ); 242 updateLocation( enumDecl );243 247 output << "enum "; 244 248 genAttributes( enumDecl->get_attributes() ); … … 255 259 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 256 260 assert( obj ); 257 updateLocation( obj );258 261 output << indent << mangleName( obj ); 259 262 if ( obj->get_init() ) { … … 278 281 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { 279 282 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 280 updateLocation( typeDecl );281 283 output << "typedef "; 282 284 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; … … 337 339 } 338 340 339 void CodeGenerator::postvisit( __attribute__((unused))ConstructorInit * init ){341 void CodeGenerator::postvisit( ConstructorInit * init ){ 340 342 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 341 343 // pseudo-output for constructor/destructor pairs 342 output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";344 output << "<ctorinit>{" << endl << ++indent << "ctor: "; 343 345 maybeAccept( init->get_ctor(), *visitor ); 344 output << ", " << std::endl << indent << "dtor: ";346 output << ", " << endl << indent << "dtor: "; 345 347 maybeAccept( init->get_dtor(), *visitor ); 346 output << std::endl << --indent << "}";348 output << endl << --indent << "}"; 347 349 } 348 350 … … 763 765 void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { 764 766 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 765 updateLocation( stmtExpr ); 766 output << "({" << std::endl; 767 output << "({" << endl; 767 768 ++indent; 768 769 unsigned int numStmts = stmts.size(); 769 770 unsigned int i = 0; 770 771 for ( Statement * stmt : stmts ) { 771 updateLocation( stmt );772 772 output << indent << printLabels( stmt->get_labels() ); 773 773 if ( i+1 == numStmts ) { … … 860 860 861 861 void CodeGenerator::postvisit( IfStmt * ifStmt ) { 862 updateLocation( ifStmt );863 862 output << "if ( "; 864 863 ifStmt->get_condition()->accept( *visitor ); … … 874 873 875 874 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { 876 updateLocation( switchStmt );877 875 output << "switch ( " ; 878 876 switchStmt->get_condition()->accept( *visitor ); 879 877 output << " ) "; 880 878 881 output << "{" << std::endl;879 output << "{" << endl; 882 880 ++indent; 883 881 acceptAll( switchStmt->get_statements(), *visitor ); … … 887 885 888 886 void CodeGenerator::postvisit( CaseStmt * caseStmt ) { 889 updateLocation( caseStmt );890 887 if ( caseStmt->isDefault()) { 891 888 output << "default"; … … 894 891 caseStmt->get_condition()->accept( *visitor ); 895 892 } // if 896 output << ": \n";893 output << ":" << endl; 897 894 898 895 std::list<Statement *> sts = caseStmt->get_statements(); -
src/CodeGen/CodeGenerator.h
ra9a4771 rd22e90f 31 31 32 32 CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false ); 33 CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false );34 CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false );35 33 36 34 //*** Turn off visit_children for all nodes … … 125 123 126 124 void updateLocation( BaseSyntaxNode const * to ); 125 struct LineEnder { 126 CodeGenerator & cg; 127 LineEnder( CodeGenerator & cg ) : cg( cg ) {} 128 std::ostream & operator()(std::ostream &) const; 129 }; 127 130 private: 128 131 Indenter indent; 129 bool insideFunction; 130 std::ostream &output; 132 std::ostream & output; 131 133 LabelPrinter printLabels; 132 134 bool pretty = false; // pretty print 133 135 bool genC = false; // true if output has to be C code 134 136 bool lineMarks = false; 137 public: 138 LineEnder endl; 139 private: 135 140 136 141 CodeLocation currentLocation; 137 142 void updateLocation( CodeLocation const & to ); 138 void nextLine();139 143 140 144 void handleStorageClass( DeclarationWithType *decl ); … … 163 167 /// returns C-compatible name of declaration 164 168 std::string genName( DeclarationWithType * decl ); 169 170 inline std::ostream & operator<<( std::ostream & os, const CodeGenerator::LineEnder & endl ) { 171 return endl( os ); 172 } 165 173 } // namespace CodeGen 166 174 -
src/CodeGen/Generate.cc
ra9a4771 rd22e90f 57 57 os << ";"; 58 58 } // if 59 os << std::endl;59 os << cgv.pass.endl; 60 60 } // if 61 61 } // for
Note: See TracChangeset
for help on using the changeset viewer.