Changeset 20a5977
- Timestamp:
- May 23, 2019, 4:22:55 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- af1e8f56
- Parents:
- 68c9165
- Location:
- src/AST
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r68c9165 r20a5977 700 700 } 701 701 702 bool isIntlikeConstantType(const ast::Type *t) {703 if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) {704 if ( basicType->isInteger() ) {705 return true;706 }707 } else if ( dynamic_cast< const ast::OneType * >( t ) ) {708 return true;709 } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) {710 return true;711 } else if ( dynamic_cast< const ast::PointerType * >( t ) ) {712 // null pointer constants, with zero int-values713 return true;714 }715 return false;716 }717 718 bool isFloatlikeConstantType(const ast::Type *t) {719 if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) {720 if ( ! bty->isInteger() ) {721 return true;722 }723 }724 return false;725 }726 727 bool isStringlikeConstantType(const ast::Type *t) {728 if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) {729 if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) {730 if ( bty->kind == ast::BasicType::Kind::Char ) {731 return true;732 }733 }734 }735 return false;736 }737 738 702 const ast::Expr * visit( const ast::ConstantExpr * node ) override final { 739 703 ConstantExpr *rslt = nullptr; 740 if (isIntlikeConstantType(node->result)) { 741 rslt = new ConstantExpr(Constant( 742 get<Type>().accept1(node->result), 704 switch ( node->kind ) { 705 case ast::ConstantExpr::Integer: 706 rslt = new ConstantExpr{Constant{ 707 get<Type>().accept1( node->result ), 743 708 node->rep, 744 709 (unsigned long long) node->intValue() 745 )); 746 } else if (isFloatlikeConstantType(node->result)) { 747 rslt = new ConstantExpr(Constant( 710 }}; 711 break; 712 case ast::ConstantExpr::FloatingPoint: 713 rslt = new ConstantExpr{Constant{ 748 714 get<Type>().accept1(node->result), 749 715 node->rep, 750 716 (double) node->floatValue() 751 ));752 } else if (isStringlikeConstantType(node->result)) {753 rslt = new ConstantExpr(Constant::from_string(754 node->rep755 ));717 }}; 718 break; 719 case ast::ConstantExpr::String: 720 rslt = new ConstantExpr{Constant::from_string( node->rep )}; 721 break; 756 722 } 757 723 assert(rslt); -
src/AST/Expr.cpp
r68c9165 r20a5977 241 241 FixedLen, DynamicDim }, 242 242 std::string{"\""} + s + "\"", 243 (unsigned long long)0 }; 243 (unsigned long long)0, 244 ConstantExpr::String }; 244 245 } 245 246 -
src/AST/Expr.hpp
r68c9165 r20a5977 337 337 public: 338 338 std::string rep; 339 enum Kind { Integer, FloatingPoint, String } kind; 339 340 340 341 ConstantExpr( 341 const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v ) 342 : Expr( loc, ty ), val( v ), rep( r ) {} 342 const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v, 343 Kind k = Integer ) 344 : Expr( loc, ty ), val( v ), rep( r ), kind( k ) {} 343 345 ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v ) 344 : Expr( loc, ty ), val( v ), rep( r ) {}346 : Expr( loc, ty ), val( v ), rep( r ), kind( FloatingPoint ) {} 345 347 346 348 /// Gets the value of this constant as an integer -
src/AST/Print.cpp
r68c9165 r20a5977 59 59 } 60 60 61 /// call if mandatory field is missing 62 void undefined() { 63 os << "UNDEFINED"; 64 } 65 66 /// call for fields that should be mandatory 67 void safe_print( const ast::Node * n ) { 68 if ( n ) n->accept( *this ); 69 else undefined(); 70 } 71 72 /// call to print short form. Incorporates features of safe_print() 73 void short_print( const ast::Node * n ) { 74 if ( ! n ) { undefined(); return; } 75 bool old_short = short_mode; short_mode = true; 76 n->accept( *this ); 77 short_mode = old_short; 78 } 79 61 80 62 81 static const char* Names[]; … … 99 118 } 100 119 120 void print( const ast::Expr::InferUnion & inferred, unsigned level = 0 ) { 121 switch ( inferred.mode ) { 122 case ast::Expr::InferUnion::Empty: return; 123 case ast::Expr::InferUnion::Slots: { 124 os << indent << "with " << inferred.data.resnSlots.size() << " pending inference slots" 125 << std::endl; 126 return; 127 } 128 case ast::Expr::InferUnion::Params: { 129 os << indent << "with inferred parameters " << level << ":" << std::endl; 130 ++indent; 131 for ( const auto & i : inferred.data.inferParams ) { 132 os << indent; 133 short_print( Decl::fromId( i.second.decl ) ); 134 os << std::endl; 135 print( i.second.expr->inferred, level+1 ); 136 } 137 --indent; 138 return; 139 } 140 } 141 } 142 101 143 void print( const ast::ParameterizedType::ForallList & forall ) { 102 144 if ( forall.empty() ) return; … … 122 164 printAll( params ); 123 165 --indent; 166 } 167 168 void print( const ast::AggregateDecl * node ) { 169 os << node->typeString() << " " << node->name << ":"; 170 if ( node->linkage != Linkage::Cforall ) { 171 os << " " << Linkage::name( node->linkage ); 172 } // if 173 os << " with body : " << (node->body ? "yes " : "no "); 174 175 if ( ! node->params.empty() ) { 176 os << endl << indent << "... with parameters" << endl; 177 ++indent; 178 printAll( node->params ); 179 --indent; 180 } // if 181 if ( ! node->members.empty() ) { 182 os << endl << indent << "... with members" << endl; 183 ++indent; 184 printAll( node->members ); 185 --indent; 186 } // if 187 if ( ! node->attributes.empty() ) { 188 os << endl << indent << "... with attributes" << endl; 189 ++indent; 190 printAll( node->attributes ); 191 --indent; 192 } // if 193 os << endl; 194 } 195 196 void print( const ast::NamedTypeDecl * node ) { 197 if ( !node->name.empty() ) os << node->name << ": "; 198 199 if ( node->linkage != Linkage::Cforall ) { 200 os << Linkage::name( node->linkage ) << " "; 201 } // if 202 print( node->storage ); 203 os << node->typeString(); 204 if ( node->base ) { 205 os << " for "; 206 ++indent; 207 node->base->accept( *this ); 208 --indent; 209 } // if 210 if ( ! node->params.empty() ) { 211 os << endl << indent << "... with parameters" << endl; 212 ++indent; 213 printAll( node->params ); 214 --indent; 215 } // if 216 if ( ! node->assertions.empty() ) { 217 os << endl << indent << "... with assertions" << endl; 218 ++indent; 219 printAll( node->assertions ); 220 --indent; 221 } // if 222 } 223 224 void postprint( const ast::Expr * node ) { 225 print( node->inferred ); 226 227 if ( node->env ) { 228 os << std::endl << indent << "... with environment:" << std::endl; 229 ++indent; 230 node->env->accept( *this ); 231 --indent; 232 } 233 234 if ( node->extension ) { 235 os << std::endl << indent << "... with extension"; 236 } 124 237 } 125 238 … … 138 251 print( node->qualifiers ); 139 252 } 140 141 void print( const ast::AggregateDecl * node ) {142 os << node->typeString() << " " << node->name << ":";143 if ( node->linkage != Linkage::Cforall ) {144 os << " " << Linkage::name( node->linkage );145 } // if146 os << " with body : " << (node->body ? "yes " : "no ");147 148 if ( ! node->params.empty() ) {149 os << endl << indent << "... with parameters" << endl;150 ++indent;151 printAll( node->params );152 --indent;153 } // if154 if ( ! node->members.empty() ) {155 os << endl << indent << "... with members" << endl;156 ++indent;157 printAll( node->members );158 --indent;159 } // if160 if ( ! node->attributes.empty() ) {161 os << endl << indent << "... with attributes" << endl;162 ++indent;163 printAll( node->attributes );164 --indent;165 } // if166 os << endl;167 }168 169 void print( const ast::NamedTypeDecl * node ) {170 if ( !node->name.empty() ) os << node->name << ": ";171 172 if ( node->linkage != Linkage::Cforall ) {173 os << Linkage::name( node->linkage ) << " ";174 } // if175 print( node->storage );176 os << node->typeString();177 if ( node->base ) {178 os << " for ";179 ++indent;180 node->base->accept( *this );181 --indent;182 } // if183 if ( ! node->params.empty() ) {184 os << endl << indent << "... with parameters" << endl;185 ++indent;186 printAll( node->params );187 --indent;188 } // if189 if ( ! node->assertions.empty() ) {190 os << endl << indent << "... with assertions" << endl;191 ++indent;192 printAll( node->assertions );193 --indent;194 } // if195 }196 253 197 254 public: … … 208 265 node->type->accept( *this ); 209 266 } else { 210 os << " untyped entity";267 os << "untyped entity"; 211 268 } // if 212 269 … … 254 311 node->type->accept( *this ); 255 312 } else { 256 os << "untyped entity 313 os << "untyped entity"; 257 314 } // if 258 315 … … 331 388 ++indent; 332 389 os << "Expression Statement:" << endl << indent; 333 node->expr->accept( *this);390 safe_print( node->expr ); 334 391 --indent; 335 392 return node; … … 366 423 os << indent+1; 367 424 ++indent; 368 node->cond->accept( *this);425 safe_print( node->cond ); 369 426 --indent; 370 427 … … 384 441 ++indent; 385 442 os << indent; 386 node->thenPart->accept( *this);443 safe_print( node->thenPart ); 387 444 --indent; 388 445 … … 458 515 459 516 virtual const ast::Expr * visit( const ast::ApplicationExpr * node ) { 517 ++indent; 518 os << "Application of" << std::endl << indent; 519 safe_print( node->func ); 520 os << std::endl; 521 if ( ! node->args.empty() ) { 522 os << indent << "... to arguments" << std::endl; 523 printAll( node->args ); 524 } 525 --indent; 526 postprint( node ); 527 460 528 return node; 461 529 } 462 530 463 531 virtual const ast::Expr * visit( const ast::UntypedExpr * node ) { 532 ++indent; 533 os << "Applying untyped:" << std::endl; 534 os << indent; 535 safe_print( node->func ); 536 os << std::endl << indent-1 << "...to:" << std::endl; 537 printAll( node->args ); 538 --indent; 539 postprint( node ); 540 464 541 return node; 465 542 } 466 543 467 544 virtual const ast::Expr * visit( const ast::NameExpr * node ) { 545 os << "Name: " << node->name; 546 postprint( node ); 547 468 548 return node; 469 549 } 470 550 471 551 virtual const ast::Expr * visit( const ast::AddressExpr * node ) { 552 os << "Address of:" << std::endl; 553 ++indent; 554 os << indent; 555 safe_print( node->arg ); 556 557 --indent; 558 472 559 return node; 473 560 } 474 561 475 562 virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) { 563 os << "Address of label:" << node->arg; 564 476 565 return node; 477 566 } 478 567 479 568 virtual const ast::Expr * visit( const ast::CastExpr * node ) { 569 ++indent; 570 os << (node->isGenerated ? "Generated" : "Explicit") << " cast of:" << std::endl << indent; 571 safe_print( node->arg ); 572 os << std::endl << indent-1 << "... to:"; 573 if ( ! node->result ) { 574 os << " "; 575 undefined(); 576 } else if ( node->result->isVoid() ) { 577 os << " nothing"; 578 } else { 579 os << std::endl << indent; 580 node->result->accept( *this ); 581 } // if 582 --indent; 583 postprint( node ); 584 480 585 return node; 481 586 } 482 587 483 588 virtual const ast::Expr * visit( const ast::KeywordCastExpr * node ) { 589 ++indent; 590 os << "Keyword Cast of:" << std::endl << indent; 591 safe_print( node->arg ); 592 --indent; 593 os << std::endl << indent << "... to: " << node->targetString(); 594 postprint( node ); 595 484 596 return node; 485 597 } 486 598 487 599 virtual const ast::Expr * visit( const ast::VirtualCastExpr * node ) { 600 ++indent; 601 os << "Virtual Cast of:" << std::endl << indent; 602 safe_print( node->arg ); 603 os << std::endl << indent-1 << "... to:"; 604 if ( ! node->result ) { 605 os << " unknown"; 606 } else { 607 os << std::endl << indent; 608 node->result->accept( *this ); 609 } 610 --indent; 611 postprint( node ); 612 488 613 return node; 489 614 } 490 615 491 616 virtual const ast::Expr * visit( const ast::UntypedMemberExpr * node ) { 617 ++indent; 618 os << "Untyped Member Expression, with field: " << std::endl << indent; 619 safe_print( node->member ); 620 os << indent-1 << "... from aggregate:" << std::endl << indent; 621 safe_print( node->aggregate ); 622 --indent; 623 postprint( node ); 624 492 625 return node; 493 626 } 494 627 495 628 virtual const ast::Expr * visit( const ast::MemberExpr * node ) { 629 ++indent; 630 os << "Member Expression, with field:" << std::endl << indent; 631 safe_print( node->member ); 632 os << std::endl << indent-1 << "... from aggregate:" << std::endl << indent; 633 safe_print( node->aggregate ); 634 --indent; 635 postprint( node ); 636 496 637 return node; 497 638 } 498 639 499 640 virtual const ast::Expr * visit( const ast::VariableExpr * node ) { 641 os << "Variable Expression: "; 642 short_print( node->var ); 643 postprint( node ); 644 500 645 return node; 501 646 } 502 647 503 648 virtual const ast::Expr * visit( const ast::ConstantExpr * node ) { 649 os << "Constant Expression (" << node->rep; 650 if ( node->result ) { 651 os << ": "; 652 node->result->accept( *this ); 653 } 654 os << ")"; 655 postprint( node ); 656 504 657 return node; 505 658 } … … 635 788 } 636 789 } 637 638 if ( node->base ) { 639 node->base->accept( *this ); 640 } else { 641 os << "UNDEFINED"; 642 } 790 safe_print( node->base ); 791 643 792 return node; 644 793 } … … 658 807 } 659 808 660 if ( node->base ) { 661 node->base->accept( *this ); 662 } else { 663 os << "UNDEFINED"; 664 } 809 safe_print( node->base ); 665 810 666 811 if ( node->dimension ) { … … 674 819 virtual const ast::Type * visit( const ast::ReferenceType * node ) { 675 820 preprint( node ); 676 677 821 os << "reference to "; 678 if ( node->base ) { 679 node->base->accept( *this ); 680 } else { 681 os << "UNDEFINED"; 682 } 822 safe_print( node->base ); 683 823 684 824 return node; … … 687 827 virtual const ast::Type * visit( const ast::QualifiedType * node ) { 688 828 preprint( node ); 689 690 829 ++indent; 691 830 os << "Qualified Type:" << std::endl << indent; 692 node->parent->accept( *this);831 safe_print( node->parent ); 693 832 os << std::endl << indent; 694 node->child->accept( *this);833 safe_print( node->child ); 695 834 os << std::endl; 696 835 --indent; … … 701 840 virtual const ast::Type * visit( const ast::FunctionType * node ) { 702 841 preprint( node ); 703 842 704 843 os << "function" << std::endl; 705 844 if ( ! node->params.empty() ) { … … 730 869 virtual const ast::Type * visit( const ast::StructInstType * node ) { 731 870 preprint( node ); 732 733 871 os << "instance of struct " << node->name; 734 872 if ( node->base ) { … … 742 880 virtual const ast::Type * visit( const ast::UnionInstType * node ) { 743 881 preprint( node ); 744 745 882 os << "instance of union " << node->name; 746 883 if ( node->base ) { … … 754 891 virtual const ast::Type * visit( const ast::EnumInstType * node ) { 755 892 preprint( node ); 756 757 893 os << "instance of enum " << node->name; 758 894 if ( node->base ) { … … 766 902 virtual const ast::Type * visit( const ast::TraitInstType * node ) { 767 903 preprint( node ); 768 769 904 os << "instance of trait " << node->name; 770 905 print( node->params ); … … 775 910 virtual const ast::Type * visit( const ast::TypeInstType * node ) { 776 911 preprint( node ); 777 778 912 os << "instance of type " << node->name 779 913 << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)"; … … 785 919 virtual const ast::Type * visit( const ast::TupleType * node ) { 786 920 preprint( node ); 787 788 921 os << "tuple of types" << std::endl; 789 922 ++indent; … … 796 929 virtual const ast::Type * visit( const ast::TypeofType * node ) { 797 930 preprint( node ); 798 799 931 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; } 800 932 os << "type-of expression "; 801 if ( node->expr ) { 802 node->expr->accept( *this ); 803 } else { 804 os << "UNDEFINED"; 805 } 933 safe_print( node->expr ); 806 934 807 935 return node; … … 847 975 virtual const ast::Init * visit( const ast::SingleInit * node ) { 848 976 os << "Simple Initializer: "; 849 node->value->accept( *this);977 safe_print( node->value ); 850 978 return node; 851 979 } … … 910 1038 os << indent+1 << i.first << " -> "; 911 1039 indent += 2; 912 i.second->accept( *this);1040 safe_print( i.second ); 913 1041 indent -= 2; 914 1042 os << std::endl; … … 918 1046 os << indent+1 << i->first << " -> "; 919 1047 indent += 2; 920 i->second->accept( *this);1048 safe_print( i->second ); 921 1049 indent -= 2; 922 1050 os << std::endl;
Note: See TracChangeset
for help on using the changeset viewer.