@@ -200,6 +200,36 @@ enum TypeRootFilter {
200
200
TraitType ( String ) ,
201
201
}
202
202
203
+ #[ derive( Clone , Hash , Eq , PartialEq , Debug ) ]
204
+ enum TypeFilter {
205
+ Unknown ,
206
+ Never ,
207
+ Placeholder ,
208
+ TypeParam ( usize ) ,
209
+ StringSlice ,
210
+ StringArray ( usize ) ,
211
+ U8 ,
212
+ U16 ,
213
+ U32 ,
214
+ U64 ,
215
+ U256 ,
216
+ Bool ,
217
+ Custom ( String ) ,
218
+ B256 ,
219
+ Contract ,
220
+ ErrorRecovery ,
221
+ Tuple ( usize , Vec < TypeFilter > ) ,
222
+ Enum ( ParsedDeclId < EnumDeclaration > , Vec < TypeFilter > ) ,
223
+ Struct ( ParsedDeclId < StructDeclaration > , Vec < TypeFilter > ) ,
224
+ ContractCaller ( String ) ,
225
+ Array ( usize , Box < TypeFilter > ) ,
226
+ RawUntypedPtr ,
227
+ RawUntypedSlice ,
228
+ Ptr ( Box < TypeFilter > ) ,
229
+ Slice ( Box < TypeFilter > ) ,
230
+ TraitType ( String ) ,
231
+ }
232
+
203
233
/// Map holding trait implementations for types.
204
234
///
205
235
/// Note: "impl self" blocks are considered traits and are stored in the
@@ -208,7 +238,7 @@ enum TypeRootFilter {
208
238
pub struct TraitMap {
209
239
trait_impls : TraitImpls ,
210
240
satisfied_cache : HashSet < u64 > ,
211
- insert_for_type_cache : HashMap < TypeRootFilter , im:: Vector < TypeId > > ,
241
+ insert_for_type_cache : HashMap < TypeFilter , im:: Vector < TypeId > > ,
212
242
}
213
243
214
244
pub ( crate ) enum IsImplSelf {
@@ -538,7 +568,8 @@ impl TraitMap {
538
568
539
569
let trait_map = TraitMap {
540
570
trait_impls,
541
- satisfied_cache : HashMap :: < TypeRootFilter , im:: Vector < TypeId > > :: new ( ) ,
571
+ satisfied_cache : HashSet :: default ( ) ,
572
+ insert_for_type_cache : HashMap :: < TypeRootFilter , im:: Vector < TypeId > > :: new ( ) ,
542
573
} ;
543
574
544
575
self . extend ( trait_map, engines) ;
@@ -1623,4 +1654,88 @@ impl TraitMap {
1623
1654
} => Self :: get_type_root_filter ( engines, referenced_type. type_id ) ,
1624
1655
}
1625
1656
}
1657
+
1658
+ // This is used by the trait map to filter the entries into a HashMap with the return type string as key.
1659
+ fn get_type_filter ( engines : & Engines , type_id : TypeId ) -> TypeFilter {
1660
+ use TypeInfo :: * ;
1661
+ match & * engines. te ( ) . get ( type_id) {
1662
+ Unknown => TypeFilter :: Unknown ,
1663
+ Never => TypeFilter :: Never ,
1664
+ UnknownGeneric { .. } | Placeholder ( _) => TypeFilter :: Placeholder ,
1665
+ TypeParam ( n) => TypeFilter :: TypeParam ( * n) ,
1666
+ StringSlice => TypeFilter :: StringSlice ,
1667
+ StringArray ( x) => TypeFilter :: StringArray ( x. val ( ) ) ,
1668
+ UnsignedInteger ( x) => match x {
1669
+ IntegerBits :: Eight => TypeFilter :: U8 ,
1670
+ IntegerBits :: Sixteen => TypeFilter :: U16 ,
1671
+ IntegerBits :: ThirtyTwo => TypeFilter :: U32 ,
1672
+ IntegerBits :: SixtyFour => TypeFilter :: U64 ,
1673
+ IntegerBits :: V256 => TypeFilter :: U256 ,
1674
+ } ,
1675
+ Boolean => TypeFilter :: Bool ,
1676
+ Custom {
1677
+ qualified_call_path : call_path,
1678
+ ..
1679
+ } => TypeFilter :: Custom ( call_path. call_path . suffix . to_string ( ) ) ,
1680
+ B256 => TypeFilter :: B256 ,
1681
+ Numeric => TypeFilter :: U64 , // u64 is the default
1682
+ Contract => TypeFilter :: Contract ,
1683
+ ErrorRecovery ( _) => TypeFilter :: ErrorRecovery ,
1684
+ Tuple ( fields) => TypeFilter :: Tuple (
1685
+ fields. len ( ) ,
1686
+ fields
1687
+ . iter ( )
1688
+ . map ( |f| Self :: get_type_filter ( engines, f. type_id ) )
1689
+ . collect :: < Vec < _ > > ( ) ,
1690
+ ) ,
1691
+ UntypedEnum ( _) => unreachable ! ( ) ,
1692
+ UntypedStruct ( _) => unreachable ! ( ) ,
1693
+ Enum ( decl_id) => {
1694
+ // TODO Remove unwrap once #6475 is fixed
1695
+ TypeFilter :: Enum (
1696
+ engines. de ( ) . get_parsed_decl_id ( decl_id) . unwrap ( ) ,
1697
+ engines
1698
+ . de ( )
1699
+ . get_enum ( decl_id)
1700
+ . type_parameters
1701
+ . iter ( )
1702
+ . map ( |f| Self :: get_type_filter ( engines, f. type_id ) )
1703
+ . collect :: < Vec < _ > > ( ) ,
1704
+ )
1705
+ }
1706
+ Struct ( decl_id) => {
1707
+ // TODO Remove unwrap once #6475 is fixed
1708
+ TypeFilter :: Struct (
1709
+ engines. de ( ) . get_parsed_decl_id ( decl_id) . unwrap ( ) ,
1710
+ engines
1711
+ . de ( )
1712
+ . get_struct ( decl_id)
1713
+ . type_parameters
1714
+ . iter ( )
1715
+ . map ( |f| Self :: get_type_filter ( engines, f. type_id ) )
1716
+ . collect :: < Vec < _ > > ( ) ,
1717
+ )
1718
+ }
1719
+ ContractCaller { abi_name, .. } => TypeFilter :: ContractCaller ( abi_name. to_string ( ) ) ,
1720
+ Array ( type_argument, length) => TypeFilter :: Array (
1721
+ length. val ( ) ,
1722
+ Box :: new ( Self :: get_type_filter ( engines, type_argument. type_id ) ) ,
1723
+ ) ,
1724
+ RawUntypedPtr => TypeFilter :: RawUntypedPtr ,
1725
+ RawUntypedSlice => TypeFilter :: RawUntypedSlice ,
1726
+ Ptr ( type_argument) => TypeFilter :: Ptr ( Box :: new ( Self :: get_type_filter (
1727
+ engines,
1728
+ type_argument. type_id ,
1729
+ ) ) ) ,
1730
+ Slice ( type_argument) => TypeFilter :: Slice ( Box :: new ( Self :: get_type_filter (
1731
+ engines,
1732
+ type_argument. type_id ,
1733
+ ) ) ) ,
1734
+ Alias { ty, .. } => Self :: get_type_filter ( engines, ty. type_id ) ,
1735
+ TraitType { name, .. } => TypeFilter :: TraitType ( name. to_string ( ) ) ,
1736
+ Ref {
1737
+ referenced_type, ..
1738
+ } => Self :: get_type_filter ( engines, referenced_type. type_id ) ,
1739
+ }
1740
+ }
1626
1741
}
0 commit comments