Changes in / [907c545:a1b154d]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r907c545 ra1b154d 44 44 class ConverterNewToOld : public ast::Visitor { 45 45 BaseSyntaxNode * node = nullptr; 46 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; 47 Cache cache; 46 std::unordered_map< ast::Node *, BaseSyntaxNode * > cache; 48 47 49 48 template<typename T> … … 95 94 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; } 96 95 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; 96 template<typename NewT, typename OldT> 97 NewT * cached( const OldT & old ) { 98 auto it = cache.find( old.get() ); 99 if ( it == cache.end() ) { 100 // doesn't update cache, that should be handled by the accept function 101 return get< NewT >().accept1( old ); 102 } else { 103 return strict_dynamic_cast< NewT * >( it->second ); 104 } 103 105 } 104 106 … … 128 130 decl->isDeleted = node->isDeleted; 129 131 // fs comes from constructor 130 cache.emplace( node, decl );131 132 return nullptr; 132 133 } 133 134 134 135 const ast::DeclWithType * visit( const ast::ObjectDecl * node ) override final { 135 if ( inCache( node ) ) return nullptr;136 136 auto decl = new ObjectDecl( 137 137 node->name, … … 148 148 149 149 const ast::DeclWithType * visit( const ast::FunctionDecl * node ) override final { 150 if ( inCache( node ) ) return nullptr;151 150 auto decl = new FunctionDecl( 152 151 node->name, … … 172 171 173 172 const ast::Decl * visit( const ast::TypeDecl * node ) override final { 174 if ( inCache( node ) ) return nullptr; 173 TypeDecl::Kind kind; 174 switch (node->kind) { 175 case ast::TypeVar::Dtype: 176 kind = TypeDecl::Dtype; 177 break; 178 case ast::TypeVar::Ftype: 179 kind = TypeDecl::Ftype; 180 break; 181 case ast::TypeVar::Ttype: 182 kind = TypeDecl::Ttype; 183 break; 184 default: 185 assertf(false, "Invalid ast::TypeVar::Kind: %d\n", node->kind); 186 }; 175 187 auto decl = new TypeDecl( 176 188 node->name, 177 189 Type::StorageClasses( node->storage.val ), 178 190 get<Type>().accept1( node->base ), 179 (TypeDecl::Kind)(unsigned)node->kind,191 kind, 180 192 node->sized, 181 193 get<Type>().accept1( node->init ) 182 194 ); 183 cache.emplace( node, decl );184 195 return namedTypePostamble( decl, node ); 185 196 } … … 201 212 decl->body = node->body; 202 213 // attributes come from constructor 203 decl->parent = get<AggregateDecl>().accept1( node->parent ); 204 cache.emplace( node, decl ); 214 // TODO: Need caching for: decl->parent = node->parent; 205 215 return nullptr; 206 216 } 207 217 208 218 const ast::Decl * visit( const ast::StructDecl * node ) override final { 209 if ( inCache( node ) ) return nullptr;210 219 auto decl = new StructDecl( 211 220 node->name, … … 218 227 219 228 const ast::Decl * visit( const ast::UnionDecl * node ) override final { 220 if ( inCache( node ) ) return nullptr;221 229 auto decl = new UnionDecl( 222 230 node->name, … … 228 236 229 237 const ast::Decl * visit( const ast::EnumDecl * node ) override final { 230 if ( inCache( node ) ) return nullptr;231 238 auto decl = new EnumDecl( 232 239 node->name, … … 238 245 239 246 const ast::Decl * visit( const ast::TraitDecl * node ) override final { 240 if ( inCache( node ) ) return nullptr;241 247 auto decl = new TraitDecl( 242 248 node->name, … … 798 804 } 799 805 800 void postvisit( const ast::ReferenceToType * old, ReferenceToType * ty ) {801 ty->forall = get<TypeDecl>().acceptL( old->forall );802 ty->parameters = get<Expression>().acceptL( old->params );803 ty->hoistType = old->hoistType;804 }805 806 806 const ast::Type * visit( const ast::StructInstType * node ) override final { 807 StructInstType * ty; 808 if ( node->base ) { 809 ty = new StructInstType{ 810 cv( node ), 811 get<StructDecl>().accept1( node->base ), 812 get<Attribute>().acceptL( node->attributes ) 813 }; 814 } else { 815 ty = new StructInstType{ 816 cv( node ), 817 node->name, 818 get<Attribute>().acceptL( node->attributes ) 819 }; 820 } 821 postvisit( node, ty ); 822 this->node = ty; 807 (void)node; 823 808 return nullptr; 824 809 } 825 810 826 811 const ast::Type * visit( const ast::UnionInstType * node ) override final { 827 UnionInstType * ty; 828 if ( node->base ) { 829 ty = new UnionInstType{ 830 cv( node ), 831 get<UnionDecl>().accept1( node->base ), 832 get<Attribute>().acceptL( node->attributes ) 833 }; 834 } else { 835 ty = new UnionInstType{ 836 cv( node ), 837 node->name, 838 get<Attribute>().acceptL( node->attributes ) 839 }; 840 } 841 postvisit( node, ty ); 842 this->node = ty; 812 (void)node; 843 813 return nullptr; 844 814 } 845 815 846 816 const ast::Type * visit( const ast::EnumInstType * node ) override final { 847 EnumInstType * ty; 848 if ( node->base ) { 849 ty = new EnumInstType{ 850 cv( node ), 851 get<EnumDecl>().accept1( node->base ), 852 get<Attribute>().acceptL( node->attributes ) 853 }; 854 } else { 855 ty = new EnumInstType{ 856 cv( node ), 857 node->name, 858 get<Attribute>().acceptL( node->attributes ) 859 }; 860 } 861 postvisit( node, ty ); 862 this->node = ty; 817 (void)node; 863 818 return nullptr; 864 819 } 865 820 866 821 const ast::Type * visit( const ast::TraitInstType * node ) override final { 867 TraitInstType * ty; 868 if ( node->base ) { 869 ty = new TraitInstType{ 870 cv( node ), 871 get<TraitDecl>().accept1( node->base ), 872 get<Attribute>().acceptL( node->attributes ) 873 }; 874 } else { 875 ty = new TraitInstType{ 876 cv( node ), 877 node->name, 878 get<Attribute>().acceptL( node->attributes ) 879 }; 880 } 881 postvisit( node, ty ); 882 this->node = ty; 822 (void)node; 883 823 return nullptr; 884 824 } 885 825 886 826 const ast::Type * visit( const ast::TypeInstType * node ) override final { 887 TypeInstType * ty; 888 if ( node->base ) { 889 ty = new TypeInstType{ 890 cv( node ), 891 node->name, 892 get<TypeDecl>().accept1( node->base ), 893 get<Attribute>().acceptL( node->attributes ) 894 }; 895 } else { 896 ty = new TypeInstType{ 897 cv( node ), 898 node->name, 899 node->kind == ast::TypeVar::Ftype, 900 get<Attribute>().acceptL( node->attributes ) 901 }; 902 } 903 postvisit( node, ty ); 904 this->node = ty; 827 (void)node; 905 828 return nullptr; 906 829 } … … 1038 961 static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; } 1039 962 1040 /// returns true and sets `node` if in cache1041 bool inCache( BaseSyntaxNode* old ) {963 template<typename NewT, typename OldT> 964 NewT * cached( OldT * old ) { 1042 965 auto it = cache.find( old ); 1043 if ( it == cache.end() ) return false;1044 node =it->second;1045 return true;966 // doesn't update cache, that should be handled by the accept function 967 ast::Node * nw = it == cache.end() ? getAccept1< NewT >( old ) : it->second; 968 return strict_dynamic_cast< NewT * >( nw ); 1046 969 } 1047 970 … … 1049 972 1050 973 virtual void visit( ObjectDecl * old ) override final { 1051 if ( inCache( old ) ) return;1052 974 auto decl = new ast::ObjectDecl( 1053 975 old->location, … … 1066 988 decl->uniqueId = old->uniqueId; 1067 989 decl->extension = old->extension; 1068 cache.emplace( old, decl );1069 990 1070 991 this->node = decl; 1071 992 } 1072 993 1073 virtual void visit( FunctionDecl * old ) override final { 1074 if ( inCache( old ) ) return; 1075 // TODO 1076 auto decl = (ast::FunctionDecl *)nullptr; 1077 cache.emplace( old, decl ); 994 virtual void visit( FunctionDecl * ) override final { 995 1078 996 } 1079 997 1080 998 virtual void visit( StructDecl * old ) override final { 1081 if ( inCache( old ) ) return;1082 999 auto decl = new ast::StructDecl( 1083 1000 old->location, … … 1094 1011 decl->uniqueId = old->uniqueId; 1095 1012 decl->storage = { old->storageClasses.val }; 1096 cache.emplace( old, decl );1097 1013 1098 1014 this->node = decl; … … 1100 1016 1101 1017 virtual void visit( UnionDecl * old ) override final { 1102 if ( inCache( old ) ) return;1103 1018 auto decl = new ast::UnionDecl( 1104 1019 old->location, … … 1114 1029 decl->uniqueId = old->uniqueId; 1115 1030 decl->storage = { old->storageClasses.val }; 1116 cache.emplace( old, decl );1117 1031 1118 1032 this->node = decl; … … 1120 1034 1121 1035 virtual void visit( EnumDecl * old ) override final { 1122 if ( inCache( old ) ) return;1123 1036 auto decl = new ast::UnionDecl( 1124 1037 old->location, … … 1134 1047 decl->uniqueId = old->uniqueId; 1135 1048 decl->storage = { old->storageClasses.val }; 1136 cache.emplace( old, decl );1137 1049 1138 1050 this->node = decl; … … 1140 1052 1141 1053 virtual void visit( TraitDecl * old ) override final { 1142 if ( inCache( old ) ) return;1143 1054 auto decl = new ast::UnionDecl( 1144 1055 old->location, … … 1154 1065 decl->uniqueId = old->uniqueId; 1155 1066 decl->storage = { old->storageClasses.val }; 1156 cache.emplace( old, decl );1157 1067 1158 1068 this->node = decl; 1159 1069 } 1160 1070 1161 virtual void visit( TypeDecl * old ) override final { 1162 if ( inCache( old ) ) return; 1163 // TODO 1164 auto decl = (ast::TypeDecl *)nullptr; 1165 cache.emplace( old, decl ); 1071 virtual void visit( TypeDecl * ) override final { 1072 1166 1073 } 1167 1074 … … 1735 1642 1736 1643 virtual void visit( StructInstType * old ) override final { 1737 ast::StructInstType * ty; 1738 if ( old->baseStruct ) { 1739 ty = new ast::StructInstType{ 1740 GET_ACCEPT_1( baseStruct, StructDecl ), 1741 cv( old ), 1742 GET_ACCEPT_V( attributes, Attribute ) 1743 }; 1744 } else { 1745 ty = new ast::StructInstType{ 1746 old->name, 1747 cv( old ), 1748 GET_ACCEPT_V( attributes, Attribute ) 1749 }; 1750 } 1644 auto ty = new ast::StructInstType{ 1645 cached< ast::StructDecl >( old->baseStruct ), 1646 cv( old ), 1647 GET_ACCEPT_V( attributes, Attribute ) 1648 }; 1751 1649 postvisit( old, ty ); 1752 1650 this->node = ty; … … 1754 1652 1755 1653 virtual void visit( UnionInstType * old ) override final { 1756 ast::UnionInstType * ty; 1757 if ( old->baseUnion ) { 1758 ty = new ast::UnionInstType{ 1759 GET_ACCEPT_1( baseUnion, UnionDecl ), 1760 cv( old ), 1761 GET_ACCEPT_V( attributes, Attribute ) 1762 }; 1763 } else { 1764 ty = new ast::UnionInstType{ 1765 old->name, 1766 cv( old ), 1767 GET_ACCEPT_V( attributes, Attribute ) 1768 }; 1769 } 1654 auto ty = new ast::UnionInstType{ 1655 cached< ast::UnionDecl >( old->baseUnion ), 1656 cv( old ), 1657 GET_ACCEPT_V( attributes, Attribute ) 1658 }; 1770 1659 postvisit( old, ty ); 1771 1660 this->node = ty; … … 1773 1662 1774 1663 virtual void visit( EnumInstType * old ) override final { 1775 ast::EnumInstType * ty; 1776 if ( old->baseEnum ) { 1777 ty = new ast::EnumInstType{ 1778 GET_ACCEPT_1( baseEnum, EnumDecl ), 1779 cv( old ), 1780 GET_ACCEPT_V( attributes, Attribute ) 1781 }; 1782 } else { 1783 ty = new ast::EnumInstType{ 1784 old->name, 1785 cv( old ), 1786 GET_ACCEPT_V( attributes, Attribute ) 1787 }; 1788 } 1664 auto ty = new ast::EnumInstType{ 1665 cached< ast::EnumDecl >( old->baseEnum ), 1666 cv( old ), 1667 GET_ACCEPT_V( attributes, Attribute ) 1668 }; 1789 1669 postvisit( old, ty ); 1790 1670 this->node = ty; … … 1792 1672 1793 1673 virtual void visit( TraitInstType * old ) override final { 1794 ast::TraitInstType * ty; 1795 if ( old->baseTrait ) { 1796 ty = new ast::TraitInstType{ 1797 GET_ACCEPT_1( baseTrait, TraitDecl ), 1798 cv( old ), 1799 GET_ACCEPT_V( attributes, Attribute ) 1800 }; 1801 } else { 1802 ty = new ast::TraitInstType{ 1803 old->name, 1804 cv( old ), 1805 GET_ACCEPT_V( attributes, Attribute ) 1806 }; 1807 } 1674 auto ty = new ast::TraitInstType{ 1675 cached< ast::TraitDecl >( old->baseTrait ), 1676 cv( old ), 1677 GET_ACCEPT_V( attributes, Attribute ) 1678 }; 1808 1679 postvisit( old, ty ); 1809 1680 this->node = ty; … … 1815 1686 ty = new ast::TypeInstType{ 1816 1687 old->name, 1817 GET_ACCEPT_1( baseType, TypeDecl),1688 cached< ast::TypeDecl >( old->baseType ), 1818 1689 cv( old ), 1819 1690 GET_ACCEPT_V( attributes, Attribute )
Note: See TracChangeset
for help on using the changeset viewer.