Changes in src/AST/Convert.cpp [112fe04:746ae82]
- File:
-
- 1 edited
-
src/AST/Convert.cpp (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r112fe04 r746ae82 16 16 #include "Convert.hpp" 17 17 18 #include <unordered_map> 19 18 20 #include "AST/Attribute.hpp" 19 21 #include "AST/Decl.hpp" … … 42 44 class ConverterNewToOld : public ast::Visitor { 43 45 BaseSyntaxNode * node = nullptr; 46 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; 47 Cache cache; 44 48 45 49 template<typename T> … … 47 51 ConverterNewToOld & visitor; 48 52 49 template<typename U> 50 T * accept1( const ast::ptr<U> & ptr ) { 53 template<typename U, enum ast::Node::ref_type R> 54 T * accept1( const ast::ptr_base<U, R> & ptr ) { 55 if ( ! ptr ) return nullptr; 51 56 ptr->accept( visitor ); 52 57 T * ret = strict_dynamic_cast< T * >( visitor.node ); … … 85 90 } 86 91 return ret; 92 } 93 94 /// get new qualifiers from old type 95 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; } 96 97 /// returns true and sets `node` if in cache 98 bool inCache( const ast::Node * node ) { 99 auto it = cache.find( node ); 100 if ( it == cache.end() ) return false; 101 this->node = it->second; 102 return true; 87 103 } 88 104 … … 112 128 decl->isDeleted = node->isDeleted; 113 129 // fs comes from constructor 130 cache.emplace( node, decl ); 114 131 return nullptr; 115 132 } 116 133 117 134 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 135 if ( inCache( node ) ) return nullptr; 118 136 auto decl = new ObjectDecl( 119 137 node->name, … … 130 148 131 149 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final { 150 if ( inCache( node ) ) return nullptr; 132 151 auto decl = new FunctionDecl( 133 152 node->name, … … 153 172 154 173 const ast::Decl * visit( const ast::TypeDecl * node ) override final { 155 TypeDecl::Kind kind; 156 switch (node->kind) { 157 case ast::TypeVar::Dtype: 158 kind = TypeDecl::Dtype; 159 break; 160 case ast::TypeVar::Ftype: 161 kind = TypeDecl::Ftype; 162 break; 163 case ast::TypeVar::Ttype: 164 kind = TypeDecl::Ttype; 165 break; 166 default: 167 assertf(false, "Invalid ast::TypeVar::Kind: %d\n", node->kind); 168 }; 174 if ( inCache( node ) ) return nullptr; 169 175 auto decl = new TypeDecl( 170 176 node->name, 171 177 Type::StorageClasses( node->storage.val ), 172 178 get<Type>().accept1( node->base ), 173 kind,179 (TypeDecl::Kind)(unsigned)node->kind, 174 180 node->sized, 175 181 get<Type>().accept1( node->init ) 176 182 ); 183 cache.emplace( node, decl ); 177 184 return namedTypePostamble( decl, node ); 178 185 } … … 194 201 decl->body = node->body; 195 202 // attributes come from constructor 196 // TODO: Need caching for: decl->parent = node->parent; 203 decl->parent = get<AggregateDecl>().accept1( node->parent ); 204 cache.emplace( node, decl ); 197 205 return nullptr; 198 206 } 199 207 200 208 const ast::Decl * visit( const ast::StructDecl * node ) override final { 209 if ( inCache( node ) ) return nullptr; 201 210 auto decl = new StructDecl( 202 211 node->name, … … 209 218 210 219 const ast::Decl * visit( const ast::UnionDecl * node ) override final { 220 if ( inCache( node ) ) return nullptr; 211 221 auto decl = new UnionDecl( 212 222 node->name, … … 218 228 219 229 const ast::Decl * visit( const ast::EnumDecl * node ) override final { 230 if ( inCache( node ) ) return nullptr; 220 231 auto decl = new EnumDecl( 221 232 node->name, … … 227 238 228 239 const ast::Decl * visit( const ast::TraitDecl * node ) override final { 240 if ( inCache( node ) ) return nullptr; 229 241 auto decl = new TraitDecl( 230 242 node->name, … … 729 741 730 742 const ast::Type * visit( const ast::VoidType * node ) override final { 731 (void)node;743 this->node = new VoidType{ cv( node ) }; 732 744 return nullptr; 733 745 } 734 746 735 747 const ast::Type * visit( const ast::BasicType * node ) override final { 736 (void)node;748 this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind }; 737 749 return nullptr; 738 750 } 739 751 740 752 const ast::Type * visit( const ast::PointerType * node ) override final { 741 (void)node; 753 this->node = new PointerType{ 754 cv( node ), 755 get<Type>().accept1( node->base ), 756 get<Expression>().accept1( node->dimension ), 757 (bool)node->isVarLen, 758 (bool)node->isStatic 759 }; 742 760 return nullptr; 743 761 } 744 762 745 763 const ast::Type * visit( const ast::ArrayType * node ) override final { 746 (void)node; 764 this->node = new ArrayType{ 765 cv( node ), 766 get<Type>().accept1( node->base ), 767 get<Expression>().accept1( node->dimension ), 768 (bool)node->isVarLen, 769 (bool)node->isStatic 770 }; 747 771 return nullptr; 748 772 } 749 773 750 774 const ast::Type * visit( const ast::ReferenceType * node ) override final { 751 (void)node; 775 this->node = new ReferenceType{ 776 cv( node ), 777 get<Type>().accept1( node->base ) 778 }; 752 779 return nullptr; 753 780 } 754 781 755 782 const ast::Type * visit( const ast::QualifiedType * node ) override final { 756 (void)node; 783 this->node = new QualifiedType{ 784 cv( node ), 785 get<Type>().accept1( node->parent ), 786 get<Type>().accept1( node->child ) 787 }; 757 788 return nullptr; 758 789 } 759 790 760 791 const ast::Type * visit( const ast::FunctionType * node ) override final { 761 (void)node; 762 return nullptr; 792 auto ty = new FunctionType { 793 cv( node ), 794 (bool)node->isVarArgs 795 }; 796 ty->returnVals = get<DeclarationWithType>().acceptL( node->returns ); 797 ty->parameters = get<DeclarationWithType>().acceptL( node->params ); 798 ty->forall = get<TypeDecl>().acceptL( node->forall ); 799 this->node = ty; 800 return nullptr; 801 } 802 803 void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) { 804 ty->forall = get<TypeDecl>().acceptL( old->forall ); 805 ty->parameters = get<Expression>().acceptL( old->params ); 806 ty->hoistType = old->hoistType; 763 807 } 764 808 765 809 const ast::Type * visit( const ast::StructInstType * node ) override final { 766 (void)node; 810 StructInstType * ty; 811 if ( node->base ) { 812 ty = new StructInstType{ 813 cv( node ), 814 get<StructDecl>().accept1( node->base ), 815 get<Attribute>().acceptL( node->attributes ) 816 }; 817 } else { 818 ty = new StructInstType{ 819 cv( node ), 820 node->name, 821 get<Attribute>().acceptL( node->attributes ) 822 }; 823 } 824 postvisit( node, ty ); 825 this->node = ty; 767 826 return nullptr; 768 827 } 769 828 770 829 const ast::Type * visit( const ast::UnionInstType * node ) override final { 771 (void)node; 830 UnionInstType * ty; 831 if ( node->base ) { 832 ty = new UnionInstType{ 833 cv( node ), 834 get<UnionDecl>().accept1( node->base ), 835 get<Attribute>().acceptL( node->attributes ) 836 }; 837 } else { 838 ty = new UnionInstType{ 839 cv( node ), 840 node->name, 841 get<Attribute>().acceptL( node->attributes ) 842 }; 843 } 844 postvisit( node, ty ); 845 this->node = ty; 772 846 return nullptr; 773 847 } 774 848 775 849 const ast::Type * visit( const ast::EnumInstType * node ) override final { 776 (void)node; 850 EnumInstType * ty; 851 if ( node->base ) { 852 ty = new EnumInstType{ 853 cv( node ), 854 get<EnumDecl>().accept1( node->base ), 855 get<Attribute>().acceptL( node->attributes ) 856 }; 857 } else { 858 ty = new EnumInstType{ 859 cv( node ), 860 node->name, 861 get<Attribute>().acceptL( node->attributes ) 862 }; 863 } 864 postvisit( node, ty ); 865 this->node = ty; 777 866 return nullptr; 778 867 } 779 868 780 869 const ast::Type * visit( const ast::TraitInstType * node ) override final { 781 (void)node; 870 TraitInstType * ty; 871 if ( node->base ) { 872 ty = new TraitInstType{ 873 cv( node ), 874 get<TraitDecl>().accept1( node->base ), 875 get<Attribute>().acceptL( node->attributes ) 876 }; 877 } else { 878 ty = new TraitInstType{ 879 cv( node ), 880 node->name, 881 get<Attribute>().acceptL( node->attributes ) 882 }; 883 } 884 postvisit( node, ty ); 885 this->node = ty; 782 886 return nullptr; 783 887 } 784 888 785 889 const ast::Type * visit( const ast::TypeInstType * node ) override final { 786 (void)node; 890 TypeInstType * ty; 891 if ( node->base ) { 892 ty = new TypeInstType{ 893 cv( node ), 894 node->name, 895 get<TypeDecl>().accept1( node->base ), 896 get<Attribute>().acceptL( node->attributes ) 897 }; 898 } else { 899 ty = new TypeInstType{ 900 cv( node ), 901 node->name, 902 node->kind == ast::TypeVar::Ftype, 903 get<Attribute>().acceptL( node->attributes ) 904 }; 905 } 906 postvisit( node, ty ); 907 this->node = ty; 787 908 return nullptr; 788 909 } 789 910 790 911 const ast::Type * visit( const ast::TupleType * node ) override final { 791 (void)node; 912 this->node = new TupleType{ 913 cv( node ), 914 get<Type>().acceptL( node->types ) 915 // members generated by TupleType c'tor 916 }; 792 917 return nullptr; 793 918 } 794 919 795 920 const ast::Type * visit( const ast::TypeofType * node ) override final { 796 (void)node; 921 this->node = new TypeofType{ 922 cv( node ), 923 get<Expression>().accept1( node->expr ), 924 (bool)node->kind 925 }; 797 926 return nullptr; 798 927 } 799 928 800 929 const ast::Type * visit( const ast::VarArgsType * node ) override final { 801 (void)node;930 this->node = new VarArgsType{ cv( node ) }; 802 931 return nullptr; 803 932 } 804 933 805 934 const ast::Type * visit( const ast::ZeroType * node ) override final { 806 (void)node;935 this->node = new ZeroType{ cv( node ) }; 807 936 return nullptr; 808 937 } 809 938 810 939 const ast::Type * visit( const ast::OneType * node ) override final { 811 (void)node;812 return nullptr; 813 } 814 815 const ast::Type * visit( const ast::GlobalScopeType * node) override final {816 (void)node;940 this->node = new OneType{ cv( node ) }; 941 return nullptr; 942 } 943 944 const ast::Type * visit( const ast::GlobalScopeType * ) override final { 945 this->node = new GlobalScopeType{}; 817 946 return nullptr; 818 947 } … … 867 996 } 868 997 private: 998 /// conversion output 869 999 ast::Node * node; 1000 /// cache of nodes that might be referenced by readonly<> for de-duplication 1001 std::unordered_map< BaseSyntaxNode *, ast::Node * > cache; 870 1002 871 1003 // Local Utilities: … … 873 1005 template<typename NewT, typename OldT> 874 1006 NewT * getAccept1( OldT old ) { 1007 if ( ! old ) return nullptr; 875 1008 old->accept(*this); 876 1009 return strict_dynamic_cast< NewT * >( node ); … … 913 1046 # define GET_LABELS_V(labels) \ 914 1047 to<std::vector>::from( make_labels( std::move( labels ) ) ) 1048 1049 static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; } 1050 1051 /// returns true and sets `node` if in cache 1052 bool inCache( BaseSyntaxNode * old ) { 1053 auto it = cache.find( old ); 1054 if ( it == cache.end() ) return false; 1055 node = it->second; 1056 return true; 1057 } 915 1058 916 1059 // Now all the visit functions: 917 1060 918 1061 virtual void visit( ObjectDecl * old ) override final { 1062 if ( inCache( old ) ) return; 919 1063 auto decl = new ast::ObjectDecl( 920 1064 old->location, … … 933 1077 decl->uniqueId = old->uniqueId; 934 1078 decl->extension = old->extension; 1079 cache.emplace( old, decl ); 935 1080 936 1081 this->node = decl; 937 1082 } 938 1083 939 virtual void visit( FunctionDecl * ) override final { 940 1084 virtual void visit( FunctionDecl * old ) override final { 1085 if ( inCache( old ) ) return; 1086 // TODO 1087 auto decl = (ast::FunctionDecl *)nullptr; 1088 cache.emplace( old, decl ); 941 1089 } 942 1090 943 1091 virtual void visit( StructDecl * old ) override final { 1092 if ( inCache( old ) ) return; 944 1093 auto decl = new ast::StructDecl( 945 1094 old->location, … … 956 1105 decl->uniqueId = old->uniqueId; 957 1106 decl->storage = { old->storageClasses.val }; 1107 cache.emplace( old, decl ); 958 1108 959 1109 this->node = decl; … … 961 1111 962 1112 virtual void visit( UnionDecl * old ) override final { 1113 if ( inCache( old ) ) return; 963 1114 auto decl = new ast::UnionDecl( 964 1115 old->location, … … 974 1125 decl->uniqueId = old->uniqueId; 975 1126 decl->storage = { old->storageClasses.val }; 1127 cache.emplace( old, decl ); 976 1128 977 1129 this->node = decl; … … 979 1131 980 1132 virtual void visit( EnumDecl * old ) override final { 1133 if ( inCache( old ) ) return; 981 1134 auto decl = new ast::UnionDecl( 982 1135 old->location, … … 992 1145 decl->uniqueId = old->uniqueId; 993 1146 decl->storage = { old->storageClasses.val }; 1147 cache.emplace( old, decl ); 994 1148 995 1149 this->node = decl; … … 997 1151 998 1152 virtual void visit( TraitDecl * old ) override final { 1153 if ( inCache( old ) ) return; 999 1154 auto decl = new ast::UnionDecl( 1000 1155 old->location, … … 1010 1165 decl->uniqueId = old->uniqueId; 1011 1166 decl->storage = { old->storageClasses.val }; 1167 cache.emplace( old, decl ); 1012 1168 1013 1169 this->node = decl; 1014 1170 } 1015 1171 1016 virtual void visit( TypeDecl * ) override final { 1017 1172 virtual void visit( TypeDecl * old ) override final { 1173 if ( inCache( old ) ) return; 1174 // TODO 1175 auto decl = (ast::TypeDecl *)nullptr; 1176 cache.emplace( old, decl ); 1018 1177 } 1019 1178 … … 1526 1685 } 1527 1686 1528 virtual void visit( VoidType * ) override final { 1529 1530 } 1531 1532 virtual void visit( BasicType * ) override final { 1533 1534 } 1535 1536 virtual void visit( PointerType * ) override final { 1537 1538 } 1539 1540 virtual void visit( ArrayType * ) override final { 1541 1542 } 1543 1544 virtual void visit( ReferenceType * ) override final { 1545 1546 } 1547 1548 virtual void visit( QualifiedType * ) override final { 1549 1550 } 1551 1552 virtual void visit( FunctionType * ) override final { 1553 1554 } 1555 1556 virtual void visit( StructInstType * ) override final { 1557 1558 } 1559 1560 virtual void visit( UnionInstType * ) override final { 1561 1562 } 1563 1564 virtual void visit( EnumInstType * ) override final { 1565 1566 } 1567 1568 virtual void visit( TraitInstType * ) override final { 1569 1570 } 1571 1572 virtual void visit( TypeInstType * ) override final { 1573 1574 } 1575 1576 virtual void visit( TupleType * ) override final { 1577 1578 } 1579 1580 virtual void visit( TypeofType * ) override final { 1581 1687 virtual void visit( VoidType * old ) override final { 1688 this->node = new ast::VoidType{ cv( old ) }; 1689 } 1690 1691 virtual void visit( BasicType * old ) override final { 1692 this->node = new ast::BasicType{ (ast::BasicType::Kind)(unsigned)old->kind, cv( old ) }; 1693 } 1694 1695 virtual void visit( PointerType * old ) override final { 1696 this->node = new ast::PointerType{ 1697 GET_ACCEPT_1( base, Type ), 1698 GET_ACCEPT_1( dimension, Expr ), 1699 (ast::LengthFlag)old->isVarLen, 1700 (ast::DimensionFlag)old->isStatic, 1701 cv( old ) 1702 }; 1703 } 1704 1705 virtual void visit( ArrayType * old ) override final { 1706 this->node = new ast::ArrayType{ 1707 GET_ACCEPT_1( base, Type ), 1708 GET_ACCEPT_1( dimension, Expr ), 1709 (ast::LengthFlag)old->isVarLen, 1710 (ast::DimensionFlag)old->isStatic, 1711 cv( old ) 1712 }; 1713 } 1714 1715 virtual void visit( ReferenceType * old ) override final { 1716 this->node = new ast::ReferenceType{ 1717 GET_ACCEPT_1( base, Type ), 1718 cv( old ) 1719 }; 1720 } 1721 1722 virtual void visit( QualifiedType * old ) override final { 1723 this->node = new ast::QualifiedType{ 1724 GET_ACCEPT_1( parent, Type ), 1725 GET_ACCEPT_1( child, Type ), 1726 cv( old ) 1727 }; 1728 } 1729 1730 virtual void visit( FunctionType * old ) override final { 1731 auto ty = new ast::FunctionType { 1732 (ast::ArgumentFlag)old->isVarArgs, 1733 cv( old ) 1734 }; 1735 ty->returns = GET_ACCEPT_V( returnVals, DeclWithType ); 1736 ty->params = GET_ACCEPT_V( parameters, DeclWithType ); 1737 ty->forall = GET_ACCEPT_V( forall, TypeDecl ); 1738 this->node = ty; 1739 } 1740 1741 void postvisit( ReferenceToType * old, ast::ReferenceToType * ty ) { 1742 ty->forall = GET_ACCEPT_V( forall, TypeDecl ); 1743 ty->params = GET_ACCEPT_V( parameters, Expr ); 1744 ty->hoistType = old->hoistType; 1745 } 1746 1747 virtual void visit( StructInstType * old ) override final { 1748 ast::StructInstType * ty; 1749 if ( old->baseStruct ) { 1750 ty = new ast::StructInstType{ 1751 GET_ACCEPT_1( baseStruct, StructDecl ), 1752 cv( old ), 1753 GET_ACCEPT_V( attributes, Attribute ) 1754 }; 1755 } else { 1756 ty = new ast::StructInstType{ 1757 old->name, 1758 cv( old ), 1759 GET_ACCEPT_V( attributes, Attribute ) 1760 }; 1761 } 1762 postvisit( old, ty ); 1763 this->node = ty; 1764 } 1765 1766 virtual void visit( UnionInstType * old ) override final { 1767 ast::UnionInstType * ty; 1768 if ( old->baseUnion ) { 1769 ty = new ast::UnionInstType{ 1770 GET_ACCEPT_1( baseUnion, UnionDecl ), 1771 cv( old ), 1772 GET_ACCEPT_V( attributes, Attribute ) 1773 }; 1774 } else { 1775 ty = new ast::UnionInstType{ 1776 old->name, 1777 cv( old ), 1778 GET_ACCEPT_V( attributes, Attribute ) 1779 }; 1780 } 1781 postvisit( old, ty ); 1782 this->node = ty; 1783 } 1784 1785 virtual void visit( EnumInstType * old ) override final { 1786 ast::EnumInstType * ty; 1787 if ( old->baseEnum ) { 1788 ty = new ast::EnumInstType{ 1789 GET_ACCEPT_1( baseEnum, EnumDecl ), 1790 cv( old ), 1791 GET_ACCEPT_V( attributes, Attribute ) 1792 }; 1793 } else { 1794 ty = new ast::EnumInstType{ 1795 old->name, 1796 cv( old ), 1797 GET_ACCEPT_V( attributes, Attribute ) 1798 }; 1799 } 1800 postvisit( old, ty ); 1801 this->node = ty; 1802 } 1803 1804 virtual void visit( TraitInstType * old ) override final { 1805 ast::TraitInstType * ty; 1806 if ( old->baseTrait ) { 1807 ty = new ast::TraitInstType{ 1808 GET_ACCEPT_1( baseTrait, TraitDecl ), 1809 cv( old ), 1810 GET_ACCEPT_V( attributes, Attribute ) 1811 }; 1812 } else { 1813 ty = new ast::TraitInstType{ 1814 old->name, 1815 cv( old ), 1816 GET_ACCEPT_V( attributes, Attribute ) 1817 }; 1818 } 1819 postvisit( old, ty ); 1820 this->node = ty; 1821 } 1822 1823 virtual void visit( TypeInstType * old ) override final { 1824 ast::TypeInstType * ty; 1825 if ( old->baseType ) { 1826 ty = new ast::TypeInstType{ 1827 old->name, 1828 GET_ACCEPT_1( baseType, TypeDecl ), 1829 cv( old ), 1830 GET_ACCEPT_V( attributes, Attribute ) 1831 }; 1832 } else { 1833 ty = new ast::TypeInstType{ 1834 old->name, 1835 old->isFtype ? ast::TypeVar::Ftype : ast::TypeVar::Dtype, 1836 cv( old ), 1837 GET_ACCEPT_V( attributes, Attribute ) 1838 }; 1839 } 1840 postvisit( old, ty ); 1841 this->node = ty; 1842 } 1843 1844 virtual void visit( TupleType * old ) override final { 1845 this->node = new ast::TupleType{ 1846 GET_ACCEPT_V( types, Type ), 1847 // members generated by TupleType c'tor 1848 cv( old ) 1849 }; 1850 } 1851 1852 virtual void visit( TypeofType * old ) override final { 1853 this->node = new ast::TypeofType{ 1854 GET_ACCEPT_1( expr, Expr ), 1855 (ast::TypeofType::Kind)old->is_basetypeof, 1856 cv( old ) 1857 }; 1582 1858 } 1583 1859 1584 1860 virtual void visit( AttrType * ) override final { 1585 1586 } 1587 1588 virtual void visit( VarArgsType * ) override final {1589 1590 } 1591 1592 virtual void visit( ZeroType * ) override final {1593 1594 } 1595 1596 virtual void visit( OneType * ) override final {1597 1861 assertf( false, "AttrType deprecated in new AST." ); 1862 } 1863 1864 virtual void visit( VarArgsType * old ) override final { 1865 this->node = new ast::VarArgsType{ cv( old ) }; 1866 } 1867 1868 virtual void visit( ZeroType * old ) override final { 1869 this->node = new ast::ZeroType{ cv( old ) }; 1870 } 1871 1872 virtual void visit( OneType * old ) override final { 1873 this->node = new ast::OneType{ cv( old ) }; 1598 1874 } 1599 1875 1600 1876 virtual void visit( GlobalScopeType * ) override final { 1601 1877 this->node = new ast::GlobalScopeType{}; 1602 1878 } 1603 1879
Note:
See TracChangeset
for help on using the changeset viewer.