Changeset 4d59ff9
- Timestamp:
- Sep 13, 2018, 2:30:53 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- a4a000d
- Parents:
- 40cd873
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeTools/ResolvProtoDump.cc
r40cd873 r4d59ff9 27 27 28 28 #include "Common/PassVisitor.h" 29 #include "Common/utility.h" 29 30 #include "CodeGen/OperatorTable.h" 30 31 #include "SynTree/Declaration.h" 31 32 #include "SynTree/Expression.h" 33 #include "SynTree/Initializer.h" 32 34 #include "SynTree/Statement.h" 33 35 #include "SynTree/Type.h" … … 41 43 std::vector<ProtoDump> subs; ///< Sub-scopes 42 44 const ProtoDump* parent; ///< Outer lexical scope 45 std::unique_ptr<Type> rtnType; ///< Return type for this scope 43 46 44 47 public: 45 48 /// Default constructor for root ProtoDump 46 ProtoDump() : decls(), exprs(), subs(), parent(nullptr) {}49 ProtoDump() : decls(), exprs(), subs(), parent(nullptr), rtnType(nullptr) {} 47 50 48 51 /// Child constructor 49 ProtoDump(const ProtoDump* p) : decls(), exprs(), subs(), parent(p) {} 52 ProtoDump(const ProtoDump* p, Type* r) : decls(), exprs(), subs(), parent(p), rtnType(r) {} 53 54 // Fix copy issues 55 ProtoDump( const ProtoDump& o ) 56 : decls(o.decls), exprs(o.exprs), subs(o.subs), parent(o.parent), 57 rtnType(maybeClone(o.rtnType.get())) {} 58 ProtoDump( ProtoDump&& ) = default; 59 60 ProtoDump& operator= ( const ProtoDump& o ) { 61 if ( this == &o ) return *this; 62 63 decls = o.decls; 64 exprs = o.exprs; 65 subs = o.subs; 66 parent = o.parent; 67 rtnType.reset( maybeClone(o.rtnType.get()) ); 68 69 return *this; 70 } 71 ProtoDump& operator= ( ProtoDump&& ) = default; 50 72 51 73 private: … … 474 496 }; 475 497 498 void build( Initializer* init, std::stringstream& ss ) { 499 if ( SingleInit* si = dynamic_cast<SingleInit*>(init) ) { 500 PassVisitor<ExprPrinter> exprPrinter{ ss }; 501 si->value->accept( exprPrinter ); 502 ss << ' '; 503 } else if ( ListInit* li = dynamic_cast<ListInit*>(init) ) { 504 for ( Initializer* it : li->initializers ) { 505 build( it, ss ); 506 ss << ' '; 507 } 508 } 509 } 510 511 /// Adds an object initializer to the list of expressions 512 void build( const std::string& name, Initializer* init, std::stringstream& ss ) { 513 ss << "$constructor( "; 514 rp_name( name, ss ); 515 ss << "() "; 516 build( init, ss ); 517 ss << ')'; 518 } 519 520 /// Adds a return expression to the list of expressions 521 void build( Type* rtnType, Expression* expr, std::stringstream& ss ) { 522 ss << "$constructor( "; 523 PassVisitor<TypePrinter> tyPrinter{ ss }; 524 rtnType->accept( tyPrinter ); 525 ss << ' '; 526 PassVisitor<ExprPrinter> exprPrinter{ ss }; 527 expr->accept( exprPrinter ); 528 ss << " )"; 529 } 530 476 531 /// Adds all named declarations in a list to the local scope 477 532 void addAll( const std::list<DeclarationWithType*>& decls ) { … … 507 562 build( obj->name, obj->type, ss ); 508 563 addDecl( ss.str() ); 564 565 // add initializer as expression if applicable 566 if ( obj->init ) { 567 std::stringstream ss; 568 build( obj->name, obj->init, ss ); 569 addExpr( ss.str() ); 570 } 509 571 } 510 572 … … 517 579 // add body if available 518 580 if ( decl->statements ) { 519 PassVisitor<ProtoDump> body{ this }; 581 std::list<Type*> rtns = from_decls( decl->type->returnVals ); 582 Type* rtn = nullptr; 583 if ( rtns.size() == 1 ) { 584 if ( ! dynamic_cast<VoidType*>(rtns.front()) ) rtn = rtns.front()->clone(); 585 } else if ( rtns.size() > 1 ) { 586 rtn = new TupleType{ Type::Qualifiers{}, rtns }; 587 } 588 PassVisitor<ProtoDump> body{ this, rtn }; 520 589 521 590 // add named parameters and returns to local scope … … 554 623 } 555 624 625 void previsit( ReturnStmt* stmt ) { 626 // do nothing for void-returning functions or statements returning nothing 627 if ( ! rtnType || ! stmt->expr ) return; 628 629 // otherwise construct the return type from the expression 630 std::stringstream ss; 631 build( rtnType.get(), stmt->expr, ss ); 632 addExpr( ss.str() ); 633 visit_children = false; 634 } 635 556 636 void previsit( Expression* expr ) { 557 637 std::stringstream ss; … … 571 651 } 572 652 // print divider 573 std::cout << tab << "%%" << std::endl;653 std::cout << '\n' << tab << "%%\n" << std::endl; 574 654 // print top-level expressions 575 655 for ( const std::string& e : exprs ) {
Note: See TracChangeset
for help on using the changeset viewer.