Changes in / [a1d5d2a:b5b0907]
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
ra1d5d2a rb5b0907 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 : Sat Jun 13 07:12:27201513 // Update Count : 1 2911 // Last Modified By : Rob Schluntz 12 // Last Modified On : Mon Jun 15 12:43:21 2015 13 // Update Count : 138 14 14 // 15 15 … … 44 44 } 45 45 46 CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { } 47 48 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp ) 49 : cur_indent( indent ), insideFunction( infunp ), output( os ) { 46 ostream & CodeGenerator::Indenter::operator()( ostream & output ) { 47 return output << string( cg.cur_indent, ' ' ); 48 } 49 50 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) { 51 return indent( output ); 52 } 53 54 CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { } 55 56 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp ) 57 : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) { 50 58 //output << std::string( init ); 51 59 } 52 60 53 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent , bool infunp )54 : cur_indent( indent), insideFunction( infunp ), output( os ) {61 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp ) 62 : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) { 55 63 //output << std::string( init ); 56 64 } … … 63 71 } // if 64 72 } 65 73 66 74 //*** Declarations 67 75 void CodeGenerator::visit( FunctionDecl *functionDecl ) { … … 105 113 106 114 if ( ! memb.empty() ) { 107 output << endl << string( cur_indent, ' ' )<< "{" << endl;115 output << endl << indent << "{" << endl; 108 116 109 117 cur_indent += CodeGenerator::tabsize; 110 118 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 111 output << string( cur_indent, ' ' );119 output << indent; 112 120 (*i)->accept(*this ); 113 121 output << ";" << endl; … … 116 124 cur_indent -= CodeGenerator::tabsize; 117 125 118 output << string( cur_indent, ' ' )<< "}";126 output << indent << "}"; 119 127 } // if 120 128 } … … 139 147 140 148 if ( ! memb.empty() ) { 141 output << endl << "{" << endl;149 output << " {" << endl; 142 150 143 151 cur_indent += CodeGenerator::tabsize; … … 145 153 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 146 154 assert( obj ); 147 output << string( cur_indent, ' ' )<< mangleName( obj );155 output << indent << mangleName( obj ); 148 156 if ( obj->get_init() ) { 149 157 output << " = "; … … 155 163 cur_indent -= CodeGenerator::tabsize; 156 164 157 output << "}" << endl;165 output << indent << "}"; 158 166 } // if 159 167 } … … 445 453 446 454 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { 447 output << string( cur_indent, ' ' )<< printLabels( (*i)->get_labels() );455 output << indent << printLabels( (*i)->get_labels() ); 448 456 (*i)->accept(*this ); 449 457 … … 455 463 cur_indent -= CodeGenerator::tabsize; 456 464 457 output << string( cur_indent, ' ' )<< "}";465 output << indent << "}"; 458 466 } 459 467 … … 495 503 cur_indent -= CodeGenerator::tabsize; 496 504 497 output << string( cur_indent, ' ' )<< "}";505 output << indent << "}"; 498 506 } 499 507 500 508 void CodeGenerator::visit( CaseStmt *caseStmt ) { 501 output << string( cur_indent, ' ' );509 output << indent; 502 510 if ( caseStmt->isDefault()) { 503 511 output << "default"; … … 512 520 cur_indent += CodeGenerator::tabsize; 513 521 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 514 output << string( cur_indent, ' ' )<< printLabels( (*i)->get_labels() ) ;522 output << indent << printLabels( (*i)->get_labels() ) ; 515 523 (*i)->accept(*this ); 516 524 output << endl; … … 565 573 whileStmt->get_body()->accept( *this ); 566 574 567 output << string( cur_indent, ' ' );575 output << indent; 568 576 569 577 if ( whileStmt->get_isDoWhile() ) { … … 597 605 598 606 void CodeGenerator::visit( NullStmt *nullStmt ) { 599 //output << string( cur_indent, ' ' )<< CodeGenerator::printLabels( nullStmt->get_labels() );607 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 600 608 output << "/* null statement */ ;"; 601 609 } -
src/CodeGen/CodeGenerator.h
ra1d5d2a rb5b0907 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 : Mon Jun 8 14:34:43 201513 // Update Count : 1511 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Jun 11 13:24:23 2015 13 // Update Count : 23 14 14 // 15 15 … … 80 80 81 81 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 82 83 struct Indenter { 84 Indenter(CodeGenerator &cg) : cg(cg) {} 85 CodeGenerator & cg; 86 std::ostream& operator()(std::ostream & os); 87 }; 82 88 private: 89 90 Indenter indent; 83 91 int cur_indent; 84 92 bool insideFunction; -
src/Parser/ExpressionNode.cc
ra1d5d2a rb5b0907 9 9 // Author : Rodolfo G. Esteves 10 10 // Created On : Sat May 16 13:17:07 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Mon Jun 8 17:33:40201513 // Update Count : 1 4711 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 10 14:57:52 2015 13 // Update Count : 151 14 14 // 15 15 … … 252 252 new ConstantExpr( 253 253 Constant( new BasicType( q, BasicType::UnsignedInt ), 254 toString( value.size() + 1 ) ) ), // account for '\0'254 toString( value.size()+1-2 ) ) ), // +1 for '\0' and -2 for '"' 255 255 false, false ); 256 256 return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) ); -
src/ResolvExpr/Resolver.cc
ra1d5d2a rb5b0907 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 12:17:01 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Jun 7 21:50:37201513 // Update Count : 2311 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 10 16:11:19 2015 13 // Update Count : 77 14 14 // 15 15 … … 37 37 virtual void visit( ObjectDecl *functionDecl ); 38 38 virtual void visit( TypeDecl *typeDecl ); 39 virtual void visit( ArrayType * at ); 39 40 40 41 virtual void visit( ExprStmt *exprStmt ); … … 157 158 initContext = new_type; 158 159 SymTab::Indexer::visit( objectDecl ); 159 160 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){ 161 if ( at->get_dimension() ) { 162 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 163 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 164 Expression *newExpr = findSingleExpression( castExpr, *this ); 165 delete at->get_dimension(); 166 at->set_dimension( newExpr ); 167 } 168 } 160 } 161 162 void Resolver::visit( ArrayType * at ) { 163 if ( at->get_dimension() ) { 164 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); 165 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() ); 166 Expression *newExpr = findSingleExpression( castExpr, *this ); 167 delete at->get_dimension(); 168 at->set_dimension( newExpr ); 169 170 } 171 Visitor::visit( at ); 169 172 } 170 173 … … 270 273 returnStmt->set_expr( newExpr ); 271 274 } // if 275 } 276 277 template< typename T > 278 bool isCharType( T t ) { 279 if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) { 280 return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar || 281 bt->get_kind() == BasicType::UnsignedChar; 282 } 283 return false; 272 284 } 273 285 … … 296 308 delete castExpr; 297 309 singleInit->set_value( newExpr ); 310 311 // check if initializing type is char[] 312 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 313 if ( isCharType( at->get_base() ) ) { 314 // check if the resolved type is char * 315 if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) { 316 if ( isCharType( pt->get_base() ) ) { 317 // strip cast if we're initializing a char[] with a char *, e.g. 318 // char x[] = "hello"; 319 CastExpr *ce = dynamic_cast< CastExpr * >( newExpr ); 320 singleInit->set_value( ce->get_arg() ); 321 ce->set_arg( NULL ); 322 delete ce; 323 } 324 } 325 } 326 } 298 327 } // if 299 328 // singleInit->get_value()->accept( *this ); … … 301 330 302 331 void Resolver::visit( ListInit *listInit ) { 303 Visitor::visit(listInit); 332 if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) { 333 std::list< Initializer * >::iterator iter( listInit->begin_initializers() ); 334 for ( ; iter != listInit->end_initializers(); ++iter ) { 335 initContext = at->get_base(); 336 (*iter)->accept( *this ); 337 } // for 338 } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) { 339 DeclarationWithType *dt = dynamic_cast< DeclarationWithType * >( *st->get_baseUnion()->get_members().begin() ); 340 initContext = dt->get_type(); 341 (*listInit->begin_initializers())->accept( *this ); 342 } else { 343 // basic types are handled here 344 Visitor::visit( listInit ); 345 } 346 304 347 #if 0 305 348 if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) { -
src/SynTree/Constant.cc
ra1d5d2a rb5b0907 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 : Sun Jun 7 08:45:30201513 // Update Count : 511 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed Jun 10 14:41:03 2015 13 // Update Count : 8 14 14 // 15 15 … … 27 27 28 28 void Constant::print( std::ostream &os ) const { 29 os << value;29 os << "(" << value; 30 30 if ( type ) { 31 os << " ";31 os << ": "; 32 32 type->print( os ); 33 33 } // if 34 os << ")"; 34 35 } 35 36
Note: See TracChangeset
for help on using the changeset viewer.