- Timestamp:
- May 22, 2019, 10:09:50 AM (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:
- ed5e798
- Parents:
- 907c545
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r907c545 r28c89f4 571 571 572 572 const ast::Expr * visit( const ast::AddressExpr * node ) override final { 573 (void)node; 573 auto expr = visitBaseExpr( node, 574 new AddressExpr( 575 get<Expression>().accept1(node->arg) 576 ) 577 ); 578 this->node = expr; 574 579 return nullptr; 575 580 } 576 581 577 582 const ast::Expr * visit( const ast::LabelAddressExpr * node ) override final { 578 (void)node; 583 auto expr = visitBaseExpr( node, 584 new LabelAddressExpr( 585 makeLabel(nullptr, node->arg) 586 ) 587 ); 588 this->node = expr; 579 589 return nullptr; 580 590 } 581 591 582 592 const ast::Expr * visit( const ast::CastExpr * node ) override final { 583 (void)node; 593 auto expr = visitBaseExpr( node, 594 new CastExpr( 595 get<Expression>().accept1(node->arg), 596 (node->isGenerated == ast::GeneratedCast) 597 ) 598 ); 599 this->node = expr; 584 600 return nullptr; 585 601 } 586 602 587 603 const ast::Expr * visit( const ast::KeywordCastExpr * node ) override final { 588 (void)node; 604 KeywordCastExpr::Target castTarget = KeywordCastExpr::NUMBER_OF_TARGETS; 605 switch (node->target) { 606 case ast::KeywordCastExpr::Coroutine: 607 castTarget = KeywordCastExpr::Coroutine; 608 break; 609 case ast::KeywordCastExpr::Thread: 610 castTarget = KeywordCastExpr::Thread; 611 break; 612 case ast::KeywordCastExpr::Monitor: 613 castTarget = KeywordCastExpr::Monitor; 614 break; 615 default: 616 break; 617 } 618 assert ( castTarget < KeywordCastExpr::NUMBER_OF_TARGETS ); 619 auto expr = visitBaseExpr( node, 620 new KeywordCastExpr( 621 get<Expression>().accept1(node->arg), 622 castTarget 623 ) 624 ); 625 this->node = expr; 589 626 return nullptr; 590 627 } 591 628 592 629 const ast::Expr * visit( const ast::VirtualCastExpr * node ) override final { 593 (void)node; 630 auto expr = visitBaseExpr( node, 631 new VirtualCastExpr( 632 get<Expression>().accept1(node->arg), 633 nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr 634 ) 635 ); 636 this->node = expr; 594 637 return nullptr; 595 638 } 596 639 597 640 const ast::Expr * visit( const ast::UntypedMemberExpr * node ) override final { 598 (void)node; 641 auto expr = visitBaseExpr( node, 642 new UntypedMemberExpr( 643 get<Expression>().accept1(node->member), 644 get<Expression>().accept1(node->aggregate) 645 ) 646 ); 647 this->node = expr; 599 648 return nullptr; 600 649 } 601 650 602 651 const ast::Expr * visit( const ast::MemberExpr * node ) override final { 603 (void)node; 652 auto expr = visitBaseExpr( node, 653 new MemberExpr( 654 inCache(node->member) ? 655 dynamic_cast<DeclarationWithType *>(this->node) : 656 get<DeclarationWithType>().accept1(node->member), 657 get<Expression>().accept1(node->aggregate) 658 ) 659 ); 660 this->node = expr; 604 661 return nullptr; 605 662 } 606 663 607 664 const ast::Expr * visit( const ast::VariableExpr * node ) override final { 608 (void)node; 609 return nullptr; 665 auto expr = visitBaseExpr( node, 666 new VariableExpr( 667 inCache(node->var) ? 668 dynamic_cast<DeclarationWithType *>(this->node) : 669 get<DeclarationWithType>().accept1(node->var) 670 ) 671 ); 672 this->node = expr; 673 return nullptr; 674 } 675 676 bool isIntlikeConstantType(const ast::Type *t) { 677 if ( const ast::BasicType * basicType = dynamic_cast< const ast::BasicType * >( t ) ) { 678 if ( basicType->isInteger() ) { 679 return true; 680 } 681 } else if ( dynamic_cast< const ast::OneType * >( t ) ) { 682 return true; 683 } else if ( dynamic_cast< const ast::ZeroType * >( t ) ) { 684 return true; 685 } else if ( dynamic_cast< const ast::PointerType * >( t ) ) { 686 // null pointer constants, with zero int-values 687 return true; 688 } 689 return false; 690 } 691 692 bool isFloatlikeConstantType(const ast::Type *t) { 693 if ( const ast::BasicType * bty = dynamic_cast< const ast::BasicType * >( t ) ) { 694 if ( ! bty->isInteger() ) { 695 return true; 696 } 697 } 698 return false; 699 } 700 701 bool isStringlikeConstantType(const ast::Type *t) { 702 if ( const ast::ArrayType * aty = dynamic_cast< const ast::ArrayType * >( t ) ) { 703 if ( const ast::BasicType * bty = aty->base.as<ast::BasicType>() ) { 704 if ( bty->kind == ast::BasicType::Kind::Char ) { 705 return true; 706 } 707 } 708 } 709 return false; 610 710 } 611 711 612 712 const ast::Expr * visit( const ast::ConstantExpr * node ) override final { 613 (void)node; 713 ConstantExpr *rslt = nullptr; 714 if (isIntlikeConstantType(node->result)) { 715 rslt = new ConstantExpr(Constant( 716 get<Type>().accept1(node->result), 717 node->rep, 718 (unsigned long long) node->intValue() 719 )); 720 } else if (isFloatlikeConstantType(node->result)) { 721 rslt = new ConstantExpr(Constant( 722 get<Type>().accept1(node->result), 723 node->rep, 724 (double) node->floatValue() 725 )); 726 } else if (isStringlikeConstantType(node->result)) { 727 rslt = new ConstantExpr(Constant::from_string( 728 node->rep 729 )); 730 } 731 assert(rslt); 732 auto expr = visitBaseExpr( node, rslt ); 733 this->node = expr; 614 734 return nullptr; 615 735 } 616 736 617 737 const ast::Expr * visit( const ast::SizeofExpr * node ) override final { 618 (void)node; 738 assert (node->expr || node->type); 739 assert (! (node->expr && node->type)); 740 SizeofExpr *rslt; 741 if (node->expr) { 742 rslt = new SizeofExpr( 743 get<Expression>().accept1(node->expr) 744 ); 745 assert (!rslt->isType); 746 } 747 if (node->type) { 748 rslt = new SizeofExpr( 749 get<Type>().accept1(node->type) 750 ); 751 assert (rslt->isType); 752 } 753 auto expr = visitBaseExpr( node, rslt ); 754 this->node = expr; 619 755 return nullptr; 620 756 } 621 757 622 758 const ast::Expr * visit( const ast::AlignofExpr * node ) override final { 623 (void)node; 759 assert (node->expr || node->type); 760 assert (! (node->expr && node->type)); 761 AlignofExpr *rslt; 762 if (node->expr) { 763 rslt = new AlignofExpr( 764 get<Expression>().accept1(node->expr) 765 ); 766 assert (!rslt->isType); 767 } 768 if (node->type) { 769 rslt = new AlignofExpr( 770 get<Type>().accept1(node->type) 771 ); 772 assert (rslt->isType); 773 } 774 auto expr = visitBaseExpr( node, rslt ); 775 this->node = expr; 624 776 return nullptr; 625 777 } 626 778 627 779 const ast::Expr * visit( const ast::UntypedOffsetofExpr * node ) override final { 628 (void)node; 780 auto expr = visitBaseExpr( node, 781 new UntypedOffsetofExpr( 782 get<Type>().accept1(node->type), 783 node->member 784 ) 785 ); 786 this->node = expr; 629 787 return nullptr; 630 788 } 631 789 632 790 const ast::Expr * visit( const ast::OffsetofExpr * node ) override final { 633 (void)node; 791 auto expr = visitBaseExpr( node, 792 new OffsetofExpr( 793 get<Type>().accept1(node->type), 794 inCache(node->member) ? 795 dynamic_cast<DeclarationWithType *>(this->node) : 796 get<DeclarationWithType>().accept1(node->member) 797 ) 798 ); 799 this->node = expr; 634 800 return nullptr; 635 801 } 636 802 637 803 const ast::Expr * visit( const ast::OffsetPackExpr * node ) override final { 638 (void)node; 804 auto expr = visitBaseExpr( node, 805 new OffsetPackExpr( 806 get<StructInstType>().accept1(node->type) 807 ) 808 ); 809 this->node = expr; 639 810 return nullptr; 640 811 } 641 812 642 813 const ast::Expr * visit( const ast::LogicalExpr * node ) override final { 643 (void)node; 814 assert (node->isAnd == ast::LogicalFlag::AndExpr || 815 node->isAnd == ast::LogicalFlag::OrExpr ); 816 auto expr = visitBaseExpr( node, 817 new LogicalExpr( 818 get<Expression>().accept1(node->arg1), 819 get<Expression>().accept1(node->arg2), 820 (node->isAnd == ast::LogicalFlag::AndExpr) 821 ) 822 ); 823 this->node = expr; 644 824 return nullptr; 645 825 } 646 826 647 827 const ast::Expr * visit( const ast::ConditionalExpr * node ) override final { 648 (void)node; 828 auto expr = visitBaseExpr( node, 829 new ConditionalExpr( 830 get<Expression>().accept1(node->arg1), 831 get<Expression>().accept1(node->arg2), 832 get<Expression>().accept1(node->arg3) 833 ) 834 ); 835 this->node = expr; 649 836 return nullptr; 650 837 } 651 838 652 839 const ast::Expr * visit( const ast::CommaExpr * node ) override final { 653 (void)node; 840 auto expr = visitBaseExpr( node, 841 new CommaExpr( 842 get<Expression>().accept1(node->arg1), 843 get<Expression>().accept1(node->arg2) 844 ) 845 ); 846 this->node = expr; 654 847 return nullptr; 655 848 } … … 1536 1729 new ast::CastExpr( 1537 1730 old->location, 1538 nullptr, // cast's "to" type is expr's result type; converted in visitBaseExpr1731 GET_ACCEPT_1(arg, Expr), 1539 1732 old->isGenerated ? ast::GeneratedCast : ast::ExplicitCast 1540 1733 ) … … 1542 1735 } 1543 1736 1544 virtual void visit( KeywordCastExpr * ) override final { 1545 1546 } 1547 1548 virtual void visit( VirtualCastExpr * ) override final { 1549 1550 } 1551 1552 virtual void visit( AddressExpr * ) override final { 1553 1554 } 1555 1556 virtual void visit( LabelAddressExpr * ) override final { 1557 1558 } 1559 1560 virtual void visit( UntypedMemberExpr * ) override final { 1561 1562 } 1563 1564 virtual void visit( MemberExpr * ) override final { 1565 1566 } 1567 1568 virtual void visit( VariableExpr * ) override final { 1569 1570 } 1571 1572 virtual void visit( ConstantExpr * ) override final { 1573 1574 } 1575 1576 virtual void visit( SizeofExpr * ) override final { 1577 1578 } 1579 1580 virtual void visit( AlignofExpr * ) override final { 1581 1582 } 1583 1584 virtual void visit( UntypedOffsetofExpr * ) override final { 1585 1586 } 1587 1588 virtual void visit( OffsetofExpr * ) override final { 1589 1590 } 1591 1592 virtual void visit( OffsetPackExpr * ) override final { 1593 1594 } 1595 1596 virtual void visit( LogicalExpr * ) override final { 1597 1598 } 1599 1600 virtual void visit( ConditionalExpr * ) override final { 1601 1602 } 1603 1604 virtual void visit( CommaExpr * ) override final { 1605 1737 virtual void visit( KeywordCastExpr * old) override final { 1738 ast::KeywordCastExpr::Target castTarget = ast::KeywordCastExpr::NUMBER_OF_TARGETS; 1739 switch (old->target) { 1740 case KeywordCastExpr::Coroutine: 1741 castTarget = ast::KeywordCastExpr::Coroutine; 1742 break; 1743 case KeywordCastExpr::Thread: 1744 castTarget = ast::KeywordCastExpr::Thread; 1745 break; 1746 case KeywordCastExpr::Monitor: 1747 castTarget = ast::KeywordCastExpr::Monitor; 1748 break; 1749 default: 1750 break; 1751 } 1752 assert ( castTarget < ast::KeywordCastExpr::NUMBER_OF_TARGETS ); 1753 this->node = visitBaseExpr( old, 1754 new ast::KeywordCastExpr( 1755 old->location, 1756 GET_ACCEPT_1(arg, Expr), 1757 castTarget 1758 ) 1759 ); 1760 } 1761 1762 virtual void visit( VirtualCastExpr * old ) override final { 1763 this->node = visitBaseExpr( old, 1764 new ast::VirtualCastExpr( 1765 old->location, 1766 GET_ACCEPT_1(arg, Expr), 1767 nullptr // cast's "to" type is expr's result type; converted in visitBaseExpr 1768 ) 1769 ); 1770 } 1771 1772 virtual void visit( AddressExpr * old ) override final { 1773 this->node = visitBaseExpr( old, 1774 new ast::AddressExpr( 1775 old->location, 1776 GET_ACCEPT_1(arg, Expr) 1777 ) 1778 ); 1779 } 1780 1781 virtual void visit( LabelAddressExpr * old ) override final { 1782 this->node = visitBaseExpr( old, 1783 new ast::LabelAddressExpr( 1784 old->location, 1785 make_label(&old->arg) 1786 ) 1787 ); 1788 } 1789 1790 virtual void visit( UntypedMemberExpr * old ) override final { 1791 this->node = visitBaseExpr( old, 1792 new ast::UntypedMemberExpr( 1793 old->location, 1794 GET_ACCEPT_1(member, Expr), 1795 GET_ACCEPT_1(aggregate, Expr) 1796 ) 1797 ); 1798 } 1799 1800 virtual void visit( MemberExpr * old ) override final { 1801 this->node = visitBaseExpr( old, 1802 new ast::MemberExpr( 1803 old->location, 1804 inCache(old->member) ? 1805 dynamic_cast<ast::DeclWithType *>(this->node) : 1806 GET_ACCEPT_1(member, DeclWithType), 1807 GET_ACCEPT_1(aggregate, Expr) 1808 ) 1809 ); 1810 } 1811 1812 virtual void visit( VariableExpr * old ) override final { 1813 this->node = visitBaseExpr( old, 1814 new ast::VariableExpr( 1815 old->location, 1816 inCache(old->var) ? 1817 dynamic_cast<ast::DeclWithType *>(this->node) : 1818 GET_ACCEPT_1(var, DeclWithType) 1819 ) 1820 ); 1821 } 1822 1823 bool isIntlikeConstantType(const Type *t) { 1824 if ( const BasicType * basicType = dynamic_cast< const BasicType * >( t ) ) { 1825 if ( basicType->isInteger() ) { 1826 return true; 1827 } 1828 } else if ( dynamic_cast< const OneType * >( t ) ) { 1829 return true; 1830 } else if ( dynamic_cast< const ZeroType * >( t ) ) { 1831 return true; 1832 } else if ( dynamic_cast< const PointerType * >( t ) ) { 1833 // null pointer constants, with zero int-values 1834 return true; 1835 } 1836 return false; 1837 } 1838 1839 int isFloatlikeConstantType(const Type *t) { 1840 if ( const BasicType * bty = dynamic_cast< const BasicType * >( t ) ) { 1841 if ( ! bty->isInteger() ) { 1842 return true; 1843 } 1844 } 1845 return false; 1846 } 1847 1848 int isStringlikeConstantType(const Type *t) { 1849 if ( const ArrayType * aty = dynamic_cast< const ArrayType * >( t ) ) { 1850 if ( const BasicType * bty = dynamic_cast< const BasicType * >( aty->base ) ) { 1851 if ( bty->kind == BasicType::Kind::Char ) { 1852 return true; 1853 } 1854 } 1855 } 1856 return false; 1857 } 1858 1859 virtual void visit( ConstantExpr * old ) override final { 1860 ast::ConstantExpr *rslt = nullptr; 1861 if (isIntlikeConstantType(old->result)) { 1862 rslt = new ast::ConstantExpr( 1863 old->location, 1864 GET_ACCEPT_1(result, Type), 1865 old->constant.get_value(), 1866 (unsigned long long) old->intValue() 1867 ); 1868 } else if (isFloatlikeConstantType(old->result)) { 1869 rslt = new ast::ConstantExpr( 1870 old->location, 1871 GET_ACCEPT_1(result, Type), 1872 old->constant.get_value(), 1873 (double) old->constant.get_dval() 1874 ); 1875 } else if (isStringlikeConstantType(old->result)) { 1876 rslt = ast::ConstantExpr::from_string( 1877 old->location, 1878 old->constant.get_value() 1879 ); 1880 } 1881 assert(rslt); 1882 this->node = visitBaseExpr( old, rslt ); 1883 } 1884 1885 virtual void visit( SizeofExpr * old ) override final { 1886 assert (old->expr || old->type); 1887 assert (! (old->expr && old->type)); 1888 ast::SizeofExpr *rslt; 1889 if (old->expr) { 1890 assert(!old->isType); 1891 rslt = new ast::SizeofExpr( 1892 old->location, 1893 GET_ACCEPT_1(expr, Expr) 1894 ); 1895 } 1896 if (old->type) { 1897 assert(old->isType); 1898 rslt = new ast::SizeofExpr( 1899 old->location, 1900 GET_ACCEPT_1(type, Type) 1901 ); 1902 } 1903 this->node = visitBaseExpr( old, rslt ); 1904 } 1905 1906 virtual void visit( AlignofExpr * old ) override final { 1907 assert (old->expr || old->type); 1908 assert (! (old->expr && old->type)); 1909 ast::AlignofExpr *rslt; 1910 if (old->expr) { 1911 assert(!old->isType); 1912 rslt = new ast::AlignofExpr( 1913 old->location, 1914 GET_ACCEPT_1(expr, Expr) 1915 ); 1916 } 1917 if (old->type) { 1918 assert(old->isType); 1919 rslt = new ast::AlignofExpr( 1920 old->location, 1921 GET_ACCEPT_1(type, Type) 1922 ); 1923 } 1924 this->node = visitBaseExpr( old, rslt ); 1925 } 1926 1927 virtual void visit( UntypedOffsetofExpr * old ) override final { 1928 this->node = visitBaseExpr( old, 1929 new ast::UntypedOffsetofExpr( 1930 old->location, 1931 GET_ACCEPT_1(type, Type), 1932 old->member 1933 ) 1934 ); 1935 } 1936 1937 virtual void visit( OffsetofExpr * old ) override final { 1938 this->node = visitBaseExpr( old, 1939 new ast::OffsetofExpr( 1940 old->location, 1941 GET_ACCEPT_1(type, Type), 1942 inCache(old->member) ? 1943 dynamic_cast<ast::DeclWithType *>(this->node) : 1944 GET_ACCEPT_1(member, DeclWithType) 1945 ) 1946 ); 1947 } 1948 1949 virtual void visit( OffsetPackExpr * old ) override final { 1950 this->node = visitBaseExpr( old, 1951 new ast::OffsetPackExpr( 1952 old->location, 1953 GET_ACCEPT_1(type, StructInstType) 1954 ) 1955 ); 1956 } 1957 1958 virtual void visit( LogicalExpr * old ) override final { 1959 this->node = visitBaseExpr( old, 1960 new ast::LogicalExpr( 1961 old->location, 1962 GET_ACCEPT_1(arg1, Expr), 1963 GET_ACCEPT_1(arg2, Expr), 1964 old->get_isAnd() ? 1965 ast::LogicalFlag::AndExpr : 1966 ast::LogicalFlag::OrExpr 1967 ) 1968 ); 1969 } 1970 1971 virtual void visit( ConditionalExpr * old ) override final { 1972 this->node = visitBaseExpr( old, 1973 new ast::ConditionalExpr( 1974 old->location, 1975 GET_ACCEPT_1(arg1, Expr), 1976 GET_ACCEPT_1(arg2, Expr), 1977 GET_ACCEPT_1(arg3, Expr) 1978 ) 1979 ); 1980 } 1981 1982 virtual void visit( CommaExpr * old ) override final { 1983 this->node = visitBaseExpr( old, 1984 new ast::CommaExpr( 1985 old->location, 1986 GET_ACCEPT_1(arg1, Expr), 1987 GET_ACCEPT_1(arg2, Expr) 1988 ) 1989 ); 1606 1990 } 1607 1991
Note: See TracChangeset
for help on using the changeset viewer.