@@ -360,6 +360,8 @@ class TypeRefBuilder {
360360 using BuiltRequirement = TypeRefRequirement;
361361 using BuiltLayoutConstraint = TypeRefLayoutConstraint;
362362 using BuiltGenericTypeParam = const GenericTypeParameterTypeRef *;
363+ using BuiltGenericSignature = const GenericSignatureRef *;
364+ using BuiltSubstitutionMap = llvm::DenseMap<DepthAndIndex, const TypeRef *>;
363365
364366 TypeRefBuilder (const TypeRefBuilder &other) = delete ;
365367 TypeRefBuilder &operator =(const TypeRefBuilder &other) = delete ;
@@ -378,6 +380,8 @@ class TypeRefBuilder {
378380 // / Cache for field info lookups.
379381 std::unordered_map<std::string, RemoteRef<FieldDescriptor>> FieldTypeInfoCache;
380382
383+ std::vector<std::unique_ptr<const GenericSignatureRef>> SignatureRefPool;
384+
381385 TypeConverter TC;
382386 MetadataSourceBuilder MSB;
383387
@@ -394,6 +398,13 @@ class TypeRefBuilder {
394398 return TR;
395399 }
396400
401+ template <typename ... Args>
402+ const GenericSignatureRef *makeGenericSignatureRef (Args... args) {
403+ const auto TR = new GenericSignatureRef (::std::forward<Args>(args)...);
404+ SignatureRefPool.push_back (std::unique_ptr<const GenericSignatureRef>(TR));
405+ return TR;
406+ }
407+
397408 Demangle::NodeFactory &getNodeFactory () { return Dem; }
398409
399410 void clearNodeFactory () { Dem.clear (); }
@@ -599,6 +610,14 @@ class TypeRefBuilder {
599610 *this , {}, result, funcFlags, diffKind, nullptr );
600611 }
601612
613+ BuiltType createProtocolTypeFromDecl (BuiltProtocolDecl protocol) {
614+ if (protocol->second ) {
615+ return llvm::cast<TypeRef>(createObjCProtocolType (protocol->first ));
616+ } else {
617+ return llvm::cast<TypeRef>(createNominalType (protocol->first ));
618+ }
619+ }
620+
602621 const ProtocolCompositionTypeRef *
603622 createProtocolCompositionType (llvm::ArrayRef<BuiltProtocolDecl> protocols,
604623 BuiltType superclass, bool isClassBound,
@@ -608,10 +627,10 @@ class TypeRefBuilder {
608627 if (!protocol)
609628 continue ;
610629
611- if ( protocol-> second )
612- protocolRefs. push_back ( createObjCProtocolType (protocol-> first ));
613- else
614- protocolRefs.push_back (createNominalType (protocol-> first ) );
630+ auto protocolType = createProtocolTypeFromDecl (* protocol);
631+ if (!protocolType)
632+ continue ;
633+ protocolRefs.push_back (protocolType );
615634 }
616635
617636 return ProtocolCompositionTypeRef::create (*this , protocolRefs, superclass,
@@ -720,8 +739,7 @@ class TypeRefBuilder {
720739 return createObjCClassType (name);
721740 }
722741
723- const ObjCProtocolTypeRef *
724- createObjCProtocolType (const std::string &name) {
742+ const ObjCProtocolTypeRef *createObjCProtocolType (const std::string &name) {
725743 return ObjCProtocolTypeRef::create (*this , name);
726744 }
727745
@@ -739,6 +757,41 @@ class TypeRefBuilder {
739757 return OpaqueTypeRef::get ();
740758 }
741759
760+ BuiltGenericSignature
761+ createGenericSignature (llvm::ArrayRef<BuiltType> builtParams,
762+ llvm::ArrayRef<BuiltRequirement> requirements) {
763+ std::vector<BuiltGenericTypeParam> params;
764+ for (auto &builtParam : builtParams) {
765+ auto *genericRef =
766+ llvm::dyn_cast<GenericTypeParameterTypeRef>(builtParam);
767+ if (!genericRef)
768+ return nullptr ;
769+ params.push_back (genericRef);
770+ }
771+ return GenericSignatureRef::create (*this , params, requirements);
772+ }
773+
774+ BuiltSubstitutionMap
775+ createSubstitutionMap (BuiltGenericSignature sig,
776+ llvm::ArrayRef<BuiltType> replacements) {
777+ assert (sig->getParams ().size () == replacements.size () &&
778+ " Not enough replacement parameters!" );
779+ if (sig->getParams ().size () != replacements.size ())
780+ return BuiltSubstitutionMap{};
781+
782+ BuiltSubstitutionMap map{};
783+ for (unsigned paramIdx : indices (sig->getParams ())) {
784+ const auto *param = sig->getParams ()[paramIdx];
785+ auto replacement = replacements[paramIdx];
786+ map[{param->getDepth (), param->getIndex ()}] = replacement;
787+ }
788+ return map;
789+ }
790+
791+ BuiltType subst (BuiltType subject, const BuiltSubstitutionMap &Subs) {
792+ return subject->subst (*this , Subs);
793+ }
794+
742795 // /
743796 // / Parsing reflection metadata
744797 // /
0 commit comments