Changes in src/AST/Convert.cpp [746ae82:112fe04]
- File:
-
- 1 edited
-
src/AST/Convert.cpp (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r746ae82 r112fe04 16 16 #include "Convert.hpp" 17 17 18 #include <unordered_map>19 20 18 #include "AST/Attribute.hpp" 21 19 #include "AST/Decl.hpp" … … 44 42 class ConverterNewToOld : public ast::Visitor { 45 43 BaseSyntaxNode * node = nullptr; 46 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >;47 Cache cache;48 44 49 45 template<typename T> … … 51 47 ConverterNewToOld & visitor; 52 48 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; 49 template<typename U> 50 T * accept1( const ast::ptr<U> & ptr ) { 56 51 ptr->accept( visitor ); 57 52 T * ret = strict_dynamic_cast< T * >( visitor.node ); … … 90 85 } 91 86 return ret; 92 }93 94 /// get new qualifiers from old type95 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; }96 97 /// returns true and sets `node` if in cache98 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;103 87 } 104 88 … … 128 112 decl->isDeleted = node->isDeleted; 129 113 // fs comes from constructor 130 cache.emplace( node, decl );131 114 return nullptr; 132 115 } 133 116 134 117 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 135 if ( inCache( node ) ) return nullptr;136 118 auto decl = new ObjectDecl( 137 119 node->name, … … 148 130 149 131 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final { 150 if ( inCache( node ) ) return nullptr;151 132 auto decl = new FunctionDecl( 152 133 node->name, … … 172 153 173 154 const ast::Decl * visit( const ast::TypeDecl * node ) override final { 174 if ( inCache( node ) ) return nullptr; 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 }; 175 169 auto decl = new TypeDecl( 176 170 node->name, 177 171 Type::StorageClasses( node->storage.val ), 178 172 get<Type>().accept1( node->base ), 179 (TypeDecl::Kind)(unsigned)node->kind,173 kind, 180 174 node->sized, 181 175 get<Type>().accept1( node->init ) 182 176 ); 183 cache.emplace( node, decl );184 177 return namedTypePostamble( decl, node ); 185 178 } … … 201 194 decl->body = node->body; 202 195 // attributes come from constructor 203 decl->parent = get<AggregateDecl>().accept1( node->parent ); 204 cache.emplace( node, decl ); 196 // TODO: Need caching for: decl->parent = node->parent; 205 197 return nullptr; 206 198 } 207 199 208 200 const ast::Decl * visit( const ast::StructDecl * node ) override final { 209 if ( inCache( node ) ) return nullptr;210 201 auto decl = new StructDecl( 211 202 node->name, … … 218 209 219 210 const ast::Decl * visit( const ast::UnionDecl * node ) override final { 220 if ( inCache( node ) ) return nullptr;221 211 auto decl = new UnionDecl( 222 212 node->name, … … 228 218 229 219 const ast::Decl * visit( const ast::EnumDecl * node ) override final { 230 if ( inCache( node ) ) return nullptr;231 220 auto decl = new EnumDecl( 232 221 node->name, … … 238 227 239 228 const ast::Decl * visit( const ast::TraitDecl * node ) override final { 240 if ( inCache( node ) ) return nullptr;241 229 auto decl = new TraitDecl( 242 230 node->name, … … 741 729 742 730 const ast::Type * visit( const ast::VoidType * node ) override final { 743 this->node = new VoidType{ cv( node ) };731 (void)node; 744 732 return nullptr; 745 733 } 746 734 747 735 const ast::Type * visit( const ast::BasicType * node ) override final { 748 this->node = new BasicType{ cv( node ), (BasicType::Kind)(unsigned)node->kind };736 (void)node; 749 737 return nullptr; 750 738 } 751 739 752 740 const ast::Type * visit( const ast::PointerType * node ) override final { 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 }; 741 (void)node; 760 742 return nullptr; 761 743 } 762 744 763 745 const ast::Type * visit( const ast::ArrayType * node ) override final { 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 }; 746 (void)node; 771 747 return nullptr; 772 748 } 773 749 774 750 const ast::Type * visit( const ast::ReferenceType * node ) override final { 775 this->node = new ReferenceType{ 776 cv( node ), 777 get<Type>().accept1( node->base ) 778 }; 751 (void)node; 779 752 return nullptr; 780 753 } 781 754 782 755 const ast::Type * visit( const ast::QualifiedType * node ) override final { 783 this->node = new QualifiedType{ 784 cv( node ), 785 get<Type>().accept1( node->parent ), 786 get<Type>().accept1( node->child ) 787 }; 756 (void)node; 788 757 return nullptr; 789 758 } 790 759 791 760 const ast::Type * visit( const ast::FunctionType * node ) override final { 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; 761 (void)node; 762 return nullptr; 807 763 } 808 764 809 765 const ast::Type * visit( const ast::StructInstType * node ) override final { 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; 766 (void)node; 826 767 return nullptr; 827 768 } 828 769 829 770 const ast::Type * visit( const ast::UnionInstType * node ) override final { 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; 771 (void)node; 846 772 return nullptr; 847 773 } 848 774 849 775 const ast::Type * visit( const ast::EnumInstType * node ) override final { 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; 776 (void)node; 866 777 return nullptr; 867 778 } 868 779 869 780 const ast::Type * visit( const ast::TraitInstType * node ) override final { 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; 781 (void)node; 886 782 return nullptr; 887 783 } 888 784 889 785 const ast::Type * visit( const ast::TypeInstType * node ) override final { 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; 786 (void)node; 908 787 return nullptr; 909 788 } 910 789 911 790 const ast::Type * visit( const ast::TupleType * node ) override final { 912 this->node = new TupleType{ 913 cv( node ), 914 get<Type>().acceptL( node->types ) 915 // members generated by TupleType c'tor 916 }; 791 (void)node; 917 792 return nullptr; 918 793 } 919 794 920 795 const ast::Type * visit( const ast::TypeofType * node ) override final { 921 this->node = new TypeofType{ 922 cv( node ), 923 get<Expression>().accept1( node->expr ), 924 (bool)node->kind 925 }; 796 (void)node; 926 797 return nullptr; 927 798 } 928 799 929 800 const ast::Type * visit( const ast::VarArgsType * node ) override final { 930 this->node = new VarArgsType{ cv( node ) };801 (void)node; 931 802 return nullptr; 932 803 } 933 804 934 805 const ast::Type * visit( const ast::ZeroType * node ) override final { 935 this->node = new ZeroType{ cv( node ) };806 (void)node; 936 807 return nullptr; 937 808 } 938 809 939 810 const ast::Type * visit( const ast::OneType * node ) override final { 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{};811 (void)node; 812 return nullptr; 813 } 814 815 const ast::Type * visit( const ast::GlobalScopeType * node ) override final { 816 (void)node; 946 817 return nullptr; 947 818 } … … 996 867 } 997 868 private: 998 /// conversion output999 869 ast::Node * node; 1000 /// cache of nodes that might be referenced by readonly<> for de-duplication1001 std::unordered_map< BaseSyntaxNode *, ast::Node * > cache;1002 870 1003 871 // Local Utilities: … … 1005 873 template<typename NewT, typename OldT> 1006 874 NewT * getAccept1( OldT old ) { 1007 if ( ! old ) return nullptr;1008 875 old->accept(*this); 1009 876 return strict_dynamic_cast< NewT * >( node ); … … 1046 913 # define GET_LABELS_V(labels) \ 1047 914 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 cache1052 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 }1058 915 1059 916 // Now all the visit functions: 1060 917 1061 918 virtual void visit( ObjectDecl * old ) override final { 1062 if ( inCache( old ) ) return;1063 919 auto decl = new ast::ObjectDecl( 1064 920 old->location, … … 1077 933 decl->uniqueId = old->uniqueId; 1078 934 decl->extension = old->extension; 1079 cache.emplace( old, decl );1080 935 1081 936 this->node = decl; 1082 937 } 1083 938 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 ); 939 virtual void visit( FunctionDecl * ) override final { 940 1089 941 } 1090 942 1091 943 virtual void visit( StructDecl * old ) override final { 1092 if ( inCache( old ) ) return;1093 944 auto decl = new ast::StructDecl( 1094 945 old->location, … … 1105 956 decl->uniqueId = old->uniqueId; 1106 957 decl->storage = { old->storageClasses.val }; 1107 cache.emplace( old, decl );1108 958 1109 959 this->node = decl; … … 1111 961 1112 962 virtual void visit( UnionDecl * old ) override final { 1113 if ( inCache( old ) ) return;1114 963 auto decl = new ast::UnionDecl( 1115 964 old->location, … … 1125 974 decl->uniqueId = old->uniqueId; 1126 975 decl->storage = { old->storageClasses.val }; 1127 cache.emplace( old, decl );1128 976 1129 977 this->node = decl; … … 1131 979 1132 980 virtual void visit( EnumDecl * old ) override final { 1133 if ( inCache( old ) ) return;1134 981 auto decl = new ast::UnionDecl( 1135 982 old->location, … … 1145 992 decl->uniqueId = old->uniqueId; 1146 993 decl->storage = { old->storageClasses.val }; 1147 cache.emplace( old, decl );1148 994 1149 995 this->node = decl; … … 1151 997 1152 998 virtual void visit( TraitDecl * old ) override final { 1153 if ( inCache( old ) ) return;1154 999 auto decl = new ast::UnionDecl( 1155 1000 old->location, … … 1165 1010 decl->uniqueId = old->uniqueId; 1166 1011 decl->storage = { old->storageClasses.val }; 1167 cache.emplace( old, decl );1168 1012 1169 1013 this->node = decl; 1170 1014 } 1171 1015 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 ); 1016 virtual void visit( TypeDecl * ) override final { 1017 1177 1018 } 1178 1019 … … 1685 1526 } 1686 1527 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 }; 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 1858 1582 } 1859 1583 1860 1584 virtual void visit( AttrType * ) override final { 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 ) }; 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 1874 1598 } 1875 1599 1876 1600 virtual void visit( GlobalScopeType * ) override final { 1877 this->node = new ast::GlobalScopeType{}; 1601 1878 1602 } 1879 1603
Note:
See TracChangeset
for help on using the changeset viewer.