- Timestamp:
- May 21, 2019, 6:11:24 PM (5 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:
- 907c545
- Parents:
- 514a791
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r514a791 rb869ec5 44 44 class ConverterNewToOld : public ast::Visitor { 45 45 BaseSyntaxNode * node = nullptr; 46 std::unordered_map< ast::Node *, BaseSyntaxNode * > cache; 46 using Cache = std::unordered_map< const ast::Node *, BaseSyntaxNode * >; 47 Cache cache; 47 48 48 49 template<typename T> … … 94 95 Type::Qualifiers cv( const ast::Type * ty ) { return { ty->qualifiers.val }; } 95 96 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 } 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; 105 103 } 106 104 … … 130 128 decl->isDeleted = node->isDeleted; 131 129 // fs comes from constructor 130 cache.emplace( node, decl ); 132 131 return nullptr; 133 132 } 134 133 135 134 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; 150 151 auto decl = new FunctionDecl( 151 152 node->name, … … 171 172 172 173 const ast::Decl * visit( const ast::TypeDecl * node ) override final { 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 }; 174 if ( inCache( node ) ) return nullptr; 187 175 auto decl = new TypeDecl( 188 176 node->name, 189 177 Type::StorageClasses( node->storage.val ), 190 178 get<Type>().accept1( node->base ), 191 kind,179 (TypeDecl::Kind)(unsigned)node->kind, 192 180 node->sized, 193 181 get<Type>().accept1( node->init ) 194 182 ); 183 cache.emplace( node, decl ); 195 184 return namedTypePostamble( decl, node ); 196 185 } … … 212 201 decl->body = node->body; 213 202 // attributes come from constructor 214 // TODO: Need caching for: decl->parent = node->parent; 203 decl->parent = get<AggregateDecl>().accept1( node->parent ); 204 cache.emplace( node, decl ); 215 205 return nullptr; 216 206 } 217 207 218 208 const ast::Decl * visit( const ast::StructDecl * node ) override final { 209 if ( inCache( node ) ) return nullptr; 219 210 auto decl = new StructDecl( 220 211 node->name, … … 227 218 228 219 const ast::Decl * visit( const ast::UnionDecl * node ) override final { 220 if ( inCache( node ) ) return nullptr; 229 221 auto decl = new UnionDecl( 230 222 node->name, … … 236 228 237 229 const ast::Decl * visit( const ast::EnumDecl * node ) override final { 230 if ( inCache( node ) ) return nullptr; 238 231 auto decl = new EnumDecl( 239 232 node->name, … … 245 238 246 239 const ast::Decl * visit( const ast::TraitDecl * node ) override final { 240 if ( inCache( node ) ) return nullptr; 247 241 auto decl = new TraitDecl( 248 242 node->name, … … 804 798 } 805 799 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 (void)node; 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; 808 823 return nullptr; 809 824 } 810 825 811 826 const ast::Type * visit( const ast::UnionInstType * node ) override final { 812 (void)node; 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; 813 843 return nullptr; 814 844 } 815 845 816 846 const ast::Type * visit( const ast::EnumInstType * node ) override final { 817 (void)node; 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; 818 863 return nullptr; 819 864 } 820 865 821 866 const ast::Type * visit( const ast::TraitInstType * node ) override final { 822 (void)node; 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; 823 883 return nullptr; 824 884 } 825 885 826 886 const ast::Type * visit( const ast::TypeInstType * node ) override final { 827 (void)node; 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; 828 905 return nullptr; 829 906 } … … 961 1038 static ast::CV::Qualifiers cv( Type * ty ) { return { ty->get_qualifiers().val }; } 962 1039 963 template<typename NewT, typename OldT>964 NewT * cached( OldT* old ) {1040 /// returns true and sets `node` if in cache 1041 bool inCache( BaseSyntaxNode * old ) { 965 1042 auto it = cache.find( old ); 966 // doesn't update cache, that should be handled by the accept function967 ast::Node * nw = it == cache.end() ? getAccept1< NewT >( old ) :it->second;968 return strict_dynamic_cast< NewT * >( nw );1043 if ( it == cache.end() ) return false; 1044 node = it->second; 1045 return true; 969 1046 } 970 1047 … … 972 1049 973 1050 virtual void visit( ObjectDecl * old ) override final { 1051 if ( inCache( old ) ) return; 974 1052 auto decl = new ast::ObjectDecl( 975 1053 old->location, … … 988 1066 decl->uniqueId = old->uniqueId; 989 1067 decl->extension = old->extension; 1068 cache.emplace( old, decl ); 990 1069 991 1070 this->node = decl; 992 1071 } 993 1072 994 virtual void visit( FunctionDecl * ) override final { 995 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 ); 996 1078 } 997 1079 998 1080 virtual void visit( StructDecl * old ) override final { 1081 if ( inCache( old ) ) return; 999 1082 auto decl = new ast::StructDecl( 1000 1083 old->location, … … 1011 1094 decl->uniqueId = old->uniqueId; 1012 1095 decl->storage = { old->storageClasses.val }; 1096 cache.emplace( old, decl ); 1013 1097 1014 1098 this->node = decl; … … 1016 1100 1017 1101 virtual void visit( UnionDecl * old ) override final { 1102 if ( inCache( old ) ) return; 1018 1103 auto decl = new ast::UnionDecl( 1019 1104 old->location, … … 1029 1114 decl->uniqueId = old->uniqueId; 1030 1115 decl->storage = { old->storageClasses.val }; 1116 cache.emplace( old, decl ); 1031 1117 1032 1118 this->node = decl; … … 1034 1120 1035 1121 virtual void visit( EnumDecl * old ) override final { 1122 if ( inCache( old ) ) return; 1036 1123 auto decl = new ast::UnionDecl( 1037 1124 old->location, … … 1047 1134 decl->uniqueId = old->uniqueId; 1048 1135 decl->storage = { old->storageClasses.val }; 1136 cache.emplace( old, decl ); 1049 1137 1050 1138 this->node = decl; … … 1052 1140 1053 1141 virtual void visit( TraitDecl * old ) override final { 1142 if ( inCache( old ) ) return; 1054 1143 auto decl = new ast::UnionDecl( 1055 1144 old->location, … … 1065 1154 decl->uniqueId = old->uniqueId; 1066 1155 decl->storage = { old->storageClasses.val }; 1156 cache.emplace( old, decl ); 1067 1157 1068 1158 this->node = decl; 1069 1159 } 1070 1160 1071 virtual void visit( TypeDecl * ) override final { 1072 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 ); 1073 1166 } 1074 1167 … … 1642 1735 1643 1736 virtual void visit( StructInstType * old ) override final { 1644 auto ty = new ast::StructInstType{ 1645 cached< ast::StructDecl >( old->baseStruct ), 1646 cv( old ), 1647 GET_ACCEPT_V( attributes, Attribute ) 1648 }; 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 } 1649 1751 postvisit( old, ty ); 1650 1752 this->node = ty; … … 1652 1754 1653 1755 virtual void visit( UnionInstType * old ) override final { 1654 auto ty = new ast::UnionInstType{ 1655 cached< ast::UnionDecl >( old->baseUnion ), 1656 cv( old ), 1657 GET_ACCEPT_V( attributes, Attribute ) 1658 }; 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 } 1659 1770 postvisit( old, ty ); 1660 1771 this->node = ty; … … 1662 1773 1663 1774 virtual void visit( EnumInstType * old ) override final { 1664 auto ty = new ast::EnumInstType{ 1665 cached< ast::EnumDecl >( old->baseEnum ), 1666 cv( old ), 1667 GET_ACCEPT_V( attributes, Attribute ) 1668 }; 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 } 1669 1789 postvisit( old, ty ); 1670 1790 this->node = ty; … … 1672 1792 1673 1793 virtual void visit( TraitInstType * old ) override final { 1674 auto ty = new ast::TraitInstType{ 1675 cached< ast::TraitDecl >( old->baseTrait ), 1676 cv( old ), 1677 GET_ACCEPT_V( attributes, Attribute ) 1678 }; 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 } 1679 1808 postvisit( old, ty ); 1680 1809 this->node = ty; … … 1686 1815 ty = new ast::TypeInstType{ 1687 1816 old->name, 1688 cached< ast::TypeDecl >( old->baseType),1817 GET_ACCEPT_1( baseType, TypeDecl ), 1689 1818 cv( old ), 1690 1819 GET_ACCEPT_V( attributes, Attribute )
Note: See TracChangeset
for help on using the changeset viewer.