Changeset a4a000d
- Timestamp:
- Sep 13, 2018, 4:37:26 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:
- 6e9ffd1
- Parents:
- 4d59ff9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeTools/ResolvProtoDump.cc
r4d59ff9 ra4a000d 39 39 /// Visitor for dumping resolver prototype output 40 40 class ProtoDump : public WithShortCircuiting, public WithVisitorRef<ProtoDump> { 41 std::set<std::string> decls; ///< Declarations in this scope 42 std::vector<std::string> exprs; ///< Expressions in this scope 43 std::vector<ProtoDump> subs; ///< Sub-scopes 44 const ProtoDump* parent; ///< Outer lexical scope 45 std::unique_ptr<Type> rtnType; ///< Return type for this scope 41 std::set<std::string> decls; ///< Declarations in this scope 42 std::vector<std::string> exprs; ///< Expressions in this scope 43 std::vector<ProtoDump> subs; ///< Sub-scopes 44 std::unordered_set<std::string> closed; ///< Closed type variables 45 const ProtoDump* parent; ///< Outer lexical scope 46 std::unique_ptr<Type> rtnType; ///< Return type for this scope 46 47 47 48 public: 48 49 /// Default constructor for root ProtoDump 49 ProtoDump() : decls(), exprs(), subs(), parent(nullptr), rtnType(nullptr) {}50 ProtoDump() : decls(), exprs(), subs(), closed(), parent(nullptr), rtnType(nullptr) {} 50 51 51 52 /// Child constructor 52 ProtoDump(const ProtoDump* p, Type* r) : decls(), exprs(), subs(), parent(p), rtnType(r) {} 53 ProtoDump(const ProtoDump* p, Type* r) 54 : decls(), exprs(), subs(), closed(p->closed), parent(p), rtnType(r) {} 53 55 54 56 // Fix copy issues 55 ProtoDump( const ProtoDump& o)56 : decls(o.decls), exprs(o.exprs), subs(o.subs), parent(o.parent),57 ProtoDump(const ProtoDump& o) 58 : decls(o.decls), exprs(o.exprs), subs(o.subs), closed(o.closed), parent(o.parent), 57 59 rtnType(maybeClone(o.rtnType.get())) {} 58 60 ProtoDump( ProtoDump&& ) = default; 59 61 60 ProtoDump& operator= ( const ProtoDump& o) {62 ProtoDump& operator= (const ProtoDump& o) { 61 63 if ( this == &o ) return *this; 62 64 … … 64 66 exprs = o.exprs; 65 67 subs = o.subs; 68 closed = o.closed; 66 69 parent = o.parent; 67 70 rtnType.reset( maybeClone(o.rtnType.get()) ); … … 69 72 return *this; 70 73 } 71 ProtoDump& operator= ( ProtoDump&&) = default;74 ProtoDump& operator= (ProtoDump&&) = default; 72 75 73 76 private: … … 205 208 /// Visitor for printing types 206 209 struct TypePrinter : public WithShortCircuiting, WithVisitorRef<TypePrinter>, WithGuards { 207 std::stringstream& ss; ///< Output to print to 208 unsigned depth; ///< Depth of nesting from root type 209 210 TypePrinter( std::stringstream& ss ) : ss(ss), depth(0) {} 210 std::stringstream& ss; ///< Output to print to 211 const std::unordered_set<std::string>& closed; ///< Closed type variables 212 unsigned depth; ///< Depth of nesting from root type 213 214 TypePrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss ) 215 : ss(ss), closed(closed), depth(0) {} 211 216 212 217 // basic type represented as integer type … … 274 279 void previsit( EnumInstType* ) { ss << (int)BasicType::SignedInt; } 275 280 276 // make sure first letter of TypeInstType is capitalized277 281 void previsit( TypeInstType* vt ) { 278 ti_name( vt->name, ss ); 282 // print closed variables as named types 283 if ( closed.count( vt->name ) ) { ss << '#' << vt->name; } 284 // otherwise make sure first letter is capitalized 285 else { ti_name( vt->name, ss ); } 279 286 } 280 287 … … 302 309 /// builds description of function 303 310 void build( const std::string& name, FunctionType* fnTy, std::stringstream& ss ) { 304 PassVisitor<TypePrinter> printTy{ ss }; 311 PassVisitor<TypePrinter> printTy{ closed, ss }; 312 // print return values 305 313 build( printTy, from_decls( fnTy->returnVals ), ss, terminated ); 314 // print name 306 315 rp_name( name, ss ); 316 // print parameters 307 317 build( printTy, from_decls( fnTy->parameters ), ss, preceded ); 308 // TODO handle assertions 318 // print assertions 319 for ( TypeDecl* tyvar : fnTy->forall ) { 320 for ( DeclarationWithType* assn : tyvar->assertions ) { 321 ss << " | "; 322 build( assn->name, assn->get_type(), ss ); 323 } 324 } 309 325 } 310 326 … … 326 342 327 343 // print variable declaration as zero-arg function 328 PassVisitor<TypePrinter> printTy{ ss };344 PassVisitor<TypePrinter> printTy{ closed, ss }; 329 345 norefs->accept( printTy ); 330 346 ss << ' '; … … 338 354 339 355 // print access as new field name 340 PassVisitor<TypePrinter> printTy{ ss };356 PassVisitor<TypePrinter> printTy{ closed, ss }; 341 357 norefs->accept( printTy ); 342 358 ss << ' '; … … 359 375 struct ExprPrinter : WithShortCircuiting, WithVisitorRef<ExprPrinter> { 360 376 // TODO change interface to generate multiple expression candidates 361 362 std::stringstream& ss; ///< Output to print to 363 364 ExprPrinter( std::stringstream& ss ) : ss(ss) {} 377 const std::unordered_set<std::string>& closed; ///< set of closed type vars 378 std::stringstream& ss; ///< Output to print to 379 380 ExprPrinter( const std::unordered_set<std::string>& closed, std::stringstream& ss ) 381 : closed(closed), ss(ss) {} 365 382 366 383 /// Names handled as nullary function calls … … 401 418 /// Address-of handled as operator 402 419 void previsit( AddressExpr* expr ) { 403 // TODO global function to implement this404 420 ss << "$addr( "; 405 421 expr->arg->accept( *visitor ); … … 411 427 /// TODO put cast target functions in, and add second expression for target 412 428 void previsit( CastExpr* cast ) { 413 PassVisitor<TypePrinter> tyPrinter{ ss };429 PassVisitor<TypePrinter> tyPrinter{ closed, ss }; 414 430 cast->result->accept( tyPrinter ); 415 431 visit_children = false; … … 438 454 /// Constant expression replaced by its type 439 455 void previsit( ConstantExpr* expr ) { 440 PassVisitor<TypePrinter> tyPrinter{ ss };456 PassVisitor<TypePrinter> tyPrinter{ closed, ss }; 441 457 expr->constant.get_type()->accept( tyPrinter ); 442 458 visit_children = false; … … 460 476 /// Logical expressions represented as operators 461 477 void previsit( LogicalExpr* expr ) { 462 // TODO global functions for these463 478 ss << '$' << ( expr->get_isAnd() ? "and" : "or" ) << "( "; 464 479 expr->arg1->accept( *visitor ); … … 471 486 /// Conditional expression represented as operator 472 487 void previsit( ConditionalExpr* expr ) { 473 // TODO global function for this474 488 ss << "$if( "; 475 489 expr->arg1->accept( *visitor ); … … 484 498 /// Comma expression represented as operator 485 499 void previsit( CommaExpr* expr ) { 486 // TODO global function for this487 500 ss << "$seq( "; 488 501 expr->arg1->accept( *visitor ); … … 498 511 void build( Initializer* init, std::stringstream& ss ) { 499 512 if ( SingleInit* si = dynamic_cast<SingleInit*>(init) ) { 500 PassVisitor<ExprPrinter> exprPrinter{ ss };513 PassVisitor<ExprPrinter> exprPrinter{ closed, ss }; 501 514 si->value->accept( exprPrinter ); 502 515 ss << ' '; … … 521 534 void build( Type* rtnType, Expression* expr, std::stringstream& ss ) { 522 535 ss << "$constructor( "; 523 PassVisitor<TypePrinter> tyPrinter{ ss };536 PassVisitor<TypePrinter> tyPrinter{ closed, ss }; 524 537 rtnType->accept( tyPrinter ); 525 538 ss << ' '; 526 PassVisitor<ExprPrinter> exprPrinter{ ss };539 PassVisitor<ExprPrinter> exprPrinter{ closed, ss }; 527 540 expr->accept( exprPrinter ); 528 541 ss << " )"; … … 587 600 } 588 601 PassVisitor<ProtoDump> body{ this, rtn }; 602 603 for ( TypeDecl* tyvar : decl->type->forall ) { 604 // add set of "closed" types to body so that it can print them as NamedType 605 body.pass.closed.insert( tyvar->name ); 606 607 // add assertions to local scope as declarations as well 608 for ( DeclarationWithType* assn : tyvar->assertions ) { 609 assn->accept( body ); 610 } 611 } 589 612 590 613 // add named parameters and returns to local scope 591 614 body.pass.addAll( decl->type->returnVals ); 592 615 body.pass.addAll( decl->type->parameters ); 593 594 // TODO add assertions to local scope595 596 // TODO add set of "closed" types to body so that it can print them as NamedType597 616 598 617 // add contents of function to new scope … … 636 655 void previsit( Expression* expr ) { 637 656 std::stringstream ss; 638 PassVisitor<ExprPrinter> exPrinter{ ss};657 PassVisitor<ExprPrinter> exPrinter{ closed, ss }; 639 658 expr->accept( exPrinter ); 640 659 addExpr( ss.str() ); … … 642 661 } 643 662 663 /// Print non-prelude global declarations for resolv proto 664 void printGlobals() const { 665 std::cout << "#ptr<T> $addr T" << std::endl; // &? 666 int i = (int)BasicType::SignedInt; 667 std::cout << i << " $and " << i << ' ' << i << std::endl; // ?&&? 668 std::cout << i << " $or " << i << ' ' << i << std::endl; // ?||? 669 std::cout << "T $if " << i << " T T" << std::endl; // ternary operator 670 std::cout << "T $seq X T" << std::endl; // ?,? 671 } 672 644 673 public: 645 674 /// Prints this ProtoDump instance 646 675 void print(unsigned indent = 0) const { 676 // print globals at root level 677 if ( ! parent ) printGlobals(); 678 // print decls 647 679 std::string tab( indent, '\t' ); 648 // print decls649 680 for ( const std::string& d : decls ) { 650 681 std::cout << tab << d << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.