@@ -71,13 +71,21 @@ std::string toFullyQualifiedTypeNameString(const swift::Type &Type) {
71
71
Options.AlwaysDesugarArraySliceTypes = true ;
72
72
Options.AlwaysDesugarDictionaryTypes = true ;
73
73
Options.AlwaysDesugarOptionalTypes = true ;
74
+ Options.OpaqueReturnTypePrinting =
75
+ PrintOptions::OpaqueReturnTypePrintingMode::WithOpaqueKeyword;
74
76
Type.print (OutputStream, Options);
75
77
OutputStream.flush ();
76
78
return TypeNameOutput;
77
79
}
78
80
81
+ std::string toFullyQualifiedProtocolNameString (const swift::ProtocolDecl &Protocol) {
82
+ // Protocols cannot be nested in other declarations, so the only fully-qualified
83
+ // context is the declaring module name.
84
+ return Protocol.getParentModule ()->getNameStr ().str () + " ." + Protocol.getNameStr ().str ();
85
+ }
86
+
79
87
std::string toMangledTypeNameString (const swift::Type &Type) {
80
- return Mangle::ASTMangler ().mangleTypeWithoutPrefix (Type);
88
+ return Mangle::ASTMangler ().mangleTypeWithoutPrefix (Type-> getCanonicalType () );
81
89
}
82
90
83
91
} // namespace
@@ -803,46 +811,178 @@ void writeAttrInformation(llvm::json::OStream &JSON,
803
811
});
804
812
}
805
813
814
+ void writeParameterizedProtocolSameTypeRequirements (
815
+ llvm::json::OStream &JSON,
816
+ const ParameterizedProtocolType &ParameterizedProtoTy) {
817
+ auto Protocol = ParameterizedProtoTy.getProtocol ();
818
+ auto ProtocolTy = ParameterizedProtoTy.getBaseType ();
819
+ auto Requirements = Protocol->getProtocolRequirements ();
820
+ auto ParameterTypeNames = Protocol->getPrimaryAssociatedTypeNames ();
821
+ auto ProtocolArguments = ParameterizedProtoTy.getArgs ();
822
+ llvm::dbgs () << Requirements.size () << " \n " ;
823
+ assert (ProtocolArguments.size () >= ParameterTypeNames.size ());
824
+
825
+ for (size_t i = 0 ; i < ProtocolArguments.size (); ++i) {
826
+ auto ProtocolArgumentTy = ProtocolArguments[i];
827
+ std::string ArgumentName = ParameterTypeNames.size () > i
828
+ ? ParameterTypeNames[i].first .str ().str ()
829
+ : " unknown" ;
830
+
831
+ JSON.object ([&] {
832
+ auto QualifiedTypeAliasName = toFullyQualifiedProtocolNameString (
833
+ *ParameterizedProtoTy.getProtocol ()) +
834
+ " ." + ArgumentName;
835
+ JSON.attribute (" typeAliasName" , QualifiedTypeAliasName);
836
+ JSON.attribute (" substitutedTypeName" ,
837
+ toFullyQualifiedTypeNameString (ProtocolArgumentTy));
838
+ JSON.attribute (" substitutedMangledTypeName" ,
839
+ toMangledTypeNameString (ProtocolArgumentTy));
840
+ });
841
+ }
842
+ }
843
+
844
+ void writeOpaqueTypeProtocolCompositionSameTypeRequirements (
845
+ llvm::json::OStream &JSON,
846
+ const ProtocolCompositionType &ProtocolCompositionTy) {
847
+ for (auto CompositionMemberProto : ProtocolCompositionTy.getMembers ()) {
848
+ if (auto ParameterizedProtoTy =
849
+ CompositionMemberProto->getAs <ParameterizedProtocolType>()) {
850
+ writeParameterizedProtocolSameTypeRequirements (JSON,
851
+ *ParameterizedProtoTy);
852
+ }
853
+ }
854
+ }
855
+
856
+ void writeSubstitutedOpaqueTypeAliasDetails (
857
+ llvm::json::OStream &JSON, const OpaqueTypeArchetypeType &OpaqueTy) {
858
+ JSON.attributeArray (" opaqueTypeProtocolRequirements" , [&] {
859
+ auto ConformsToProtocols = OpaqueTy.getConformsTo ();
860
+ for (auto Proto : ConformsToProtocols) {
861
+ JSON.value (toFullyQualifiedProtocolNameString (*Proto));
862
+ }
863
+ });
864
+ JSON.attributeArray (" opaqueTypeSameTypeRequirements" , [&] {
865
+ auto GenericSig = OpaqueTy.getDecl ()
866
+ ->getNamingDecl ()
867
+ ->getInnermostDeclContext ()
868
+ ->getGenericSignatureOfContext ();
869
+ auto ConstraintTy = OpaqueTy.getExistentialType ();
870
+ if (auto existential = ConstraintTy->getAs <ExistentialType>())
871
+ ConstraintTy = existential->getConstraintType ();
872
+
873
+ // Opaque archetype substitutions are always canonical, so
874
+ // re-sugar the constraint type using the owning
875
+ // declaration's generic parameter names.
876
+ if (GenericSig)
877
+ ConstraintTy = GenericSig->getSugaredType (ConstraintTy);
878
+
879
+ if (auto ParameterizedProtoTy =
880
+ ConstraintTy->getAs <ParameterizedProtocolType>()) {
881
+ writeParameterizedProtocolSameTypeRequirements (JSON,
882
+ *ParameterizedProtoTy);
883
+ } else if (auto ProtocolCompositionTy =
884
+ ConstraintTy->getAs <ProtocolCompositionType>()) {
885
+ writeOpaqueTypeProtocolCompositionSameTypeRequirements (
886
+ JSON, *ProtocolCompositionTy);
887
+ }
888
+ });
889
+ }
890
+
891
+ void writeAssociatedTypeAliases (llvm::json::OStream &JSON,
892
+ const NominalTypeDecl &NomTypeDecl) {
893
+ JSON.attributeArray (" associatedTypeAliases" , [&] {
894
+ for (auto &Conformance : NomTypeDecl.getAllConformances ()) {
895
+ Conformance->forEachTypeWitness (
896
+ [&](AssociatedTypeDecl *assoc, Type type, TypeDecl *typeDecl) {
897
+ JSON.object ([&] {
898
+ JSON.attribute (" typeAliasName" , assoc->getName ().str ().str ());
899
+ JSON.attribute (" substitutedTypeName" ,
900
+ toFullyQualifiedTypeNameString (type));
901
+ JSON.attribute (" substitutedMangledTypeName" ,
902
+ toMangledTypeNameString (type));
903
+ if (auto OpaqueTy = dyn_cast<OpaqueTypeArchetypeType>(type)) {
904
+ writeSubstitutedOpaqueTypeAliasDetails (JSON, *OpaqueTy);
905
+ }
906
+ });
907
+ return false ;
908
+ });
909
+ }
910
+ });
911
+ }
912
+
913
+ void writeProperties (llvm::json::OStream &JSON,
914
+ const ConstValueTypeInfo &TypeInfo,
915
+ const NominalTypeDecl &NomTypeDecl) {
916
+ JSON.attributeArray (" properties" , [&] {
917
+ for (const auto &PropertyInfo : TypeInfo.Properties ) {
918
+ JSON.object ([&] {
919
+ const auto *decl = PropertyInfo.VarDecl ;
920
+ JSON.attribute (" label" , decl->getName ().str ().str ());
921
+ JSON.attribute (" type" , toFullyQualifiedTypeNameString (decl->getType ()));
922
+ JSON.attribute (" mangledTypeName" , toMangledTypeNameString (decl->getType ()));
923
+ JSON.attribute (" isStatic" , decl->isStatic () ? " true" : " false" );
924
+ JSON.attribute (" isComputed" , !decl->hasStorage () ? " true" : " false" );
925
+ writeLocationInformation (JSON, decl->getLoc (),
926
+ decl->getDeclContext ()->getASTContext ());
927
+ writeValue (JSON, PropertyInfo.Value );
928
+ writePropertyWrapperAttributes (JSON, PropertyInfo.PropertyWrappers ,
929
+ decl->getASTContext ());
930
+ writeRuntimeMetadataAttributes (JSON,
931
+ PropertyInfo.RuntimeMetadataAttributes ,
932
+ decl->getASTContext ());
933
+ writeResultBuilderInformation (JSON, &NomTypeDecl, decl);
934
+ writeAttrInformation (JSON, decl->getAttrs ());
935
+ });
936
+ }
937
+ });
938
+ }
939
+
940
+ void writeConformances (llvm::json::OStream &JSON,
941
+ const NominalTypeDecl &NomTypeDecl) {
942
+ JSON.attributeArray (" conformances" , [&] {
943
+ for (auto &Protocol : NomTypeDecl.getAllProtocols ()) {
944
+ JSON.value (toFullyQualifiedProtocolNameString (*Protocol));
945
+ }
946
+ });
947
+ }
948
+
949
+ void writeTypeName (llvm::json::OStream &JSON, const TypeDecl &TypeDecl) {
950
+ JSON.attribute (" typeName" ,
951
+ toFullyQualifiedTypeNameString (
952
+ TypeDecl.getDeclaredInterfaceType ()));
953
+ JSON.attribute (" mangledTypeName" ,
954
+ toMangledTypeNameString (TypeDecl.getDeclaredInterfaceType ()));
955
+ }
956
+
957
+ void writeNominalTypeKind (llvm::json::OStream &JSON,
958
+ const NominalTypeDecl &NomTypeDecl) {
959
+ JSON.attribute (
960
+ " kind" ,
961
+ NomTypeDecl.getDescriptiveKindName (NomTypeDecl.getDescriptiveKind ())
962
+ .str ());
963
+ }
964
+
806
965
bool writeAsJSONToFile (const std::vector<ConstValueTypeInfo> &ConstValueInfos,
807
966
llvm::raw_ostream &OS) {
808
967
llvm::json::OStream JSON (OS, 2 );
809
968
JSON.array ([&] {
810
969
for (const auto &TypeInfo : ConstValueInfos) {
970
+ assert (isa<NominalTypeDecl>(TypeInfo.TypeDecl ) &&
971
+ " Expected Nominal Type Decl for a conformance" );
972
+ const auto *NomTypeDecl = cast<NominalTypeDecl>(TypeInfo.TypeDecl );
973
+ const auto SourceLoc =
974
+ extractNearestSourceLoc (NomTypeDecl->getInnermostDeclContext ());
975
+ const auto &Ctx = NomTypeDecl->getInnermostDeclContext ()->getASTContext ();
976
+
811
977
JSON.object ([&] {
812
- const auto *TypeDecl = TypeInfo.TypeDecl ;
813
- JSON.attribute (" typeName" , toFullyQualifiedTypeNameString (
814
- TypeDecl->getDeclaredInterfaceType ()));
815
- JSON.attribute (
816
- " kind" ,
817
- TypeDecl->getDescriptiveKindName (TypeDecl->getDescriptiveKind ())
818
- .str ());
819
- writeLocationInformation (
820
- JSON, extractNearestSourceLoc (TypeDecl->getInnermostDeclContext ()),
821
- TypeDecl->getInnermostDeclContext ()->getASTContext ());
822
- JSON.attributeArray (" properties" , [&] {
823
- for (const auto &PropertyInfo : TypeInfo.Properties ) {
824
- JSON.object ([&] {
825
- const auto *decl = PropertyInfo.VarDecl ;
826
- JSON.attribute (" label" , decl->getName ().str ().str ());
827
- JSON.attribute (" type" ,
828
- toFullyQualifiedTypeNameString (decl->getType ()));
829
- JSON.attribute (" isStatic" , decl->isStatic () ? " true" : " false" );
830
- JSON.attribute (" isComputed" ,
831
- !decl->hasStorage () ? " true" : " false" );
832
- writeLocationInformation (JSON, decl->getLoc (),
833
- decl->getDeclContext ()->getASTContext ());
834
- writeValue (JSON, PropertyInfo.Value );
835
- writePropertyWrapperAttributes (
836
- JSON, PropertyInfo.PropertyWrappers , decl->getASTContext ());
837
- writeRuntimeMetadataAttributes (
838
- JSON, PropertyInfo.RuntimeMetadataAttributes , decl->getASTContext ());
839
- writeResultBuilderInformation (JSON, TypeDecl, decl);
840
- writeAttrInformation (JSON, decl->getAttrs ());
841
- });
842
- }
843
- });
978
+ writeTypeName (JSON, *NomTypeDecl);
979
+ writeNominalTypeKind (JSON, *NomTypeDecl);
980
+ writeLocationInformation (JSON, SourceLoc, Ctx);
981
+ writeConformances (JSON, *NomTypeDecl);
982
+ writeAssociatedTypeAliases (JSON, *NomTypeDecl);
983
+ writeProperties (JSON, TypeInfo, *NomTypeDecl);
844
984
writeEnumCases (JSON, TypeInfo.EnumElements );
845
- writeAttrInformation (JSON, TypeDecl ->getAttrs ());
985
+ writeAttrInformation (JSON, NomTypeDecl ->getAttrs ());
846
986
});
847
987
}
848
988
});
0 commit comments