Changes in / [af1e8f56:effe5b0]
- Location:
- src/AST
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
raf1e8f56 reffe5b0 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-values 713 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 702 738 const ast::Expr * visit( const ast::ConstantExpr * node ) override final { 703 739 ConstantExpr *rslt = nullptr; 704 switch ( node->kind ) { 705 case ast::ConstantExpr::Integer: 706 rslt = new ConstantExpr{Constant{ 707 get<Type>().accept1( node->result ), 740 if (isIntlikeConstantType(node->result)) { 741 rslt = new ConstantExpr(Constant( 742 get<Type>().accept1(node->result), 708 743 node->rep, 709 744 (unsigned long long) node->intValue() 710 }}; 711 break; 712 case ast::ConstantExpr::FloatingPoint: 713 rslt = new ConstantExpr{Constant{ 745 )); 746 } else if (isFloatlikeConstantType(node->result)) { 747 rslt = new ConstantExpr(Constant( 714 748 get<Type>().accept1(node->result), 715 749 node->rep, 716 750 (double) node->floatValue() 717 }};718 break;719 case ast::ConstantExpr::String:720 rslt = new ConstantExpr{Constant::from_string( node->rep )};721 break;751 )); 752 } else if (isStringlikeConstantType(node->result)) { 753 rslt = new ConstantExpr(Constant::from_string( 754 node->rep 755 )); 722 756 } 723 757 assert(rslt); -
src/AST/Expr.cpp
raf1e8f56 reffe5b0 241 241 FixedLen, DynamicDim }, 242 242 std::string{"\""} + s + "\"", 243 (unsigned long long)0, 244 ConstantExpr::String }; 243 (unsigned long long)0 }; 245 244 } 246 245 -
src/AST/Expr.hpp
raf1e8f56 reffe5b0 337 337 public: 338 338 std::string rep; 339 enum Kind { Integer, FloatingPoint, String } kind;340 339 341 340 ConstantExpr( 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 ) {} 341 const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v ) 342 : Expr( loc, ty ), val( v ), rep( r ) {} 345 343 ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v ) 346 : Expr( loc, ty ), val( v ), rep( r ) , kind( FloatingPoint ){}344 : Expr( loc, ty ), val( v ), rep( r ) {} 347 345 348 346 /// Gets the value of this constant as an integer -
src/AST/Print.cpp
raf1e8f56 reffe5b0 59 59 } 60 60 61 /// call if mandatory field is missing62 void undefined() {63 os << "UNDEFINED";64 }65 66 /// call for fields that should be mandatory67 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 80 61 81 62 static const char* Names[]; … … 118 99 } 119 100 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 143 101 void print( const ast::ParameterizedType::ForallList & forall ) { 144 102 if ( forall.empty() ) return; … … 166 124 } 167 125 126 void preprint( const ast::Type * node ) { 127 print( node->qualifiers ); 128 } 129 130 void preprint( const ast::ParameterizedType * node ) { 131 print( node->forall ); 132 print( node->qualifiers ); 133 } 134 135 void preprint( const ast::ReferenceToType * node ) { 136 print( node->forall ); 137 print( node->attributes ); 138 print( node->qualifiers ); 139 } 140 168 141 void print( const ast::AggregateDecl * node ) { 169 142 os << node->typeString() << " " << node->name << ":"; … … 222 195 } 223 196 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 }237 }238 239 void preprint( const ast::Type * node ) {240 print( node->qualifiers );241 }242 243 void preprint( const ast::ParameterizedType * node ) {244 print( node->forall );245 print( node->qualifiers );246 }247 248 void preprint( const ast::ReferenceToType * node ) {249 print( node->forall );250 print( node->attributes );251 print( node->qualifiers );252 }253 254 197 public: 255 198 virtual const ast::DeclWithType * visit( const ast::ObjectDecl * node ) { … … 265 208 node->type->accept( *this ); 266 209 } else { 267 os << " untyped entity";210 os << " untyped entity "; 268 211 } // if 269 212 … … 311 254 node->type->accept( *this ); 312 255 } else { 313 os << "untyped entity ";256 os << "untyped entity "; 314 257 } // if 315 258 … … 388 331 ++indent; 389 332 os << "Expression Statement:" << endl << indent; 390 safe_print( node->expr);333 node->expr->accept( *this ); 391 334 --indent; 392 335 return node; … … 423 366 os << indent+1; 424 367 ++indent; 425 safe_print( node->cond);368 node->cond->accept( *this ); 426 369 --indent; 427 370 … … 441 384 ++indent; 442 385 os << indent; 443 safe_print( node->thenPart);386 node->thenPart->accept( *this ); 444 387 --indent; 445 388 … … 515 458 516 459 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 528 460 return node; 529 461 } 530 462 531 463 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 541 464 return node; 542 465 } 543 466 544 467 virtual const ast::Expr * visit( const ast::NameExpr * node ) { 545 os << "Name: " << node->name;546 postprint( node );547 548 468 return node; 549 469 } 550 470 551 471 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 559 472 return node; 560 473 } 561 474 562 475 virtual const ast::Expr * visit( const ast::LabelAddressExpr * node ) { 563 os << "Address of label:" << node->arg;564 565 476 return node; 566 477 } 567 478 568 479 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 } // if582 --indent;583 postprint( node );584 585 480 return node; 586 481 } 587 482 588 483 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 596 484 return node; 597 485 } 598 486 599 487 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 613 488 return node; 614 489 } 615 490 616 491 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 625 492 return node; 626 493 } 627 494 628 495 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 637 496 return node; 638 497 } 639 498 640 499 virtual const ast::Expr * visit( const ast::VariableExpr * node ) { 641 os << "Variable Expression: ";642 short_print( node->var );643 postprint( node );644 645 500 return node; 646 501 } 647 502 648 503 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 657 504 return node; 658 505 } … … 788 635 } 789 636 } 790 safe_print( node->base ); 791 637 638 if ( node->base ) { 639 node->base->accept( *this ); 640 } else { 641 os << "UNDEFINED"; 642 } 792 643 return node; 793 644 } … … 807 658 } 808 659 809 safe_print( node->base ); 660 if ( node->base ) { 661 node->base->accept( *this ); 662 } else { 663 os << "UNDEFINED"; 664 } 810 665 811 666 if ( node->dimension ) { … … 819 674 virtual const ast::Type * visit( const ast::ReferenceType * node ) { 820 675 preprint( node ); 676 821 677 os << "reference to "; 822 safe_print( node->base ); 678 if ( node->base ) { 679 node->base->accept( *this ); 680 } else { 681 os << "UNDEFINED"; 682 } 823 683 824 684 return node; … … 827 687 virtual const ast::Type * visit( const ast::QualifiedType * node ) { 828 688 preprint( node ); 689 829 690 ++indent; 830 691 os << "Qualified Type:" << std::endl << indent; 831 safe_print( node->parent);692 node->parent->accept( *this ); 832 693 os << std::endl << indent; 833 safe_print( node->child);694 node->child->accept( *this ); 834 695 os << std::endl; 835 696 --indent; … … 840 701 virtual const ast::Type * visit( const ast::FunctionType * node ) { 841 702 preprint( node ); 842 703 843 704 os << "function" << std::endl; 844 705 if ( ! node->params.empty() ) { … … 869 730 virtual const ast::Type * visit( const ast::StructInstType * node ) { 870 731 preprint( node ); 732 871 733 os << "instance of struct " << node->name; 872 734 if ( node->base ) { … … 880 742 virtual const ast::Type * visit( const ast::UnionInstType * node ) { 881 743 preprint( node ); 744 882 745 os << "instance of union " << node->name; 883 746 if ( node->base ) { … … 891 754 virtual const ast::Type * visit( const ast::EnumInstType * node ) { 892 755 preprint( node ); 756 893 757 os << "instance of enum " << node->name; 894 758 if ( node->base ) { … … 902 766 virtual const ast::Type * visit( const ast::TraitInstType * node ) { 903 767 preprint( node ); 768 904 769 os << "instance of trait " << node->name; 905 770 print( node->params ); … … 910 775 virtual const ast::Type * visit( const ast::TypeInstType * node ) { 911 776 preprint( node ); 777 912 778 os << "instance of type " << node->name 913 779 << " (" << (node->kind == ast::TypeVar::Ftype ? "" : "not ") << "function type)"; … … 919 785 virtual const ast::Type * visit( const ast::TupleType * node ) { 920 786 preprint( node ); 787 921 788 os << "tuple of types" << std::endl; 922 789 ++indent; … … 929 796 virtual const ast::Type * visit( const ast::TypeofType * node ) { 930 797 preprint( node ); 798 931 799 if ( node->kind == ast::TypeofType::Basetypeof ) { os << "base-"; } 932 800 os << "type-of expression "; 933 safe_print( node->expr ); 801 if ( node->expr ) { 802 node->expr->accept( *this ); 803 } else { 804 os << "UNDEFINED"; 805 } 934 806 935 807 return node; … … 975 847 virtual const ast::Init * visit( const ast::SingleInit * node ) { 976 848 os << "Simple Initializer: "; 977 safe_print( node->value);849 node->value->accept( *this ); 978 850 return node; 979 851 } … … 1038 910 os << indent+1 << i.first << " -> "; 1039 911 indent += 2; 1040 safe_print( i.second);912 i.second->accept( *this ); 1041 913 indent -= 2; 1042 914 os << std::endl; … … 1046 918 os << indent+1 << i->first << " -> "; 1047 919 indent += 2; 1048 safe_print( i->second);920 i->second->accept( *this ); 1049 921 indent -= 2; 1050 922 os << std::endl;
Note:
See TracChangeset
for help on using the changeset viewer.