30
30
#include " swift/Runtime/HeapObject.h"
31
31
#include " swift/Basic/Unreachable.h"
32
32
33
+ #include < type_traits>
33
34
#include < vector>
34
35
#include < unordered_map>
35
36
36
37
#include < inttypes.h>
37
38
38
39
namespace swift {
40
+ namespace reflection {
41
+ class TypeRefBuilder ;
42
+ }
39
43
namespace remote {
40
44
41
45
template <typename BuiltType>
@@ -2905,25 +2909,8 @@ class MetadataReader {
2905
2909
return builtSubsts;
2906
2910
}
2907
2911
2908
- BuiltType readNominalTypeFromMetadata (MetadataRef origMetadata,
2909
- bool skipArtificialSubclasses = false ) {
2910
- auto metadata = origMetadata;
2911
- auto descriptorAddress =
2912
- readAddressOfNominalTypeDescriptor (metadata,
2913
- skipArtificialSubclasses);
2914
- if (!descriptorAddress)
2915
- return BuiltType ();
2916
-
2917
- // If we've skipped an artificial subclasses, check the cache at
2918
- // the superclass. (This also protects against recursion.)
2919
- if (skipArtificialSubclasses && metadata != origMetadata) {
2920
- auto it = TypeCache.find (getAddress (metadata));
2921
- if (it != TypeCache.end ())
2922
- return it->second ;
2923
- }
2924
-
2925
- // Read the nominal type descriptor.
2926
- ContextDescriptorRef descriptor = readContextDescriptor (descriptorAddress);
2912
+ BuiltType readNominalTypeFromDescriptor (MetadataRef metadata,
2913
+ ContextDescriptorRef descriptor) {
2927
2914
if (!descriptor)
2928
2915
return BuiltType ();
2929
2916
@@ -2932,34 +2919,67 @@ class MetadataReader {
2932
2919
if (!typeDecl)
2933
2920
return BuiltType ();
2934
2921
2922
+ BuiltType parent;
2923
+ // Attempt to build the parent (if it exists). We only do this when building
2924
+ // typerefs as that's the only implementation of builder that actually needs
2925
+ // the parent to build the nominal type correctly.
2926
+ if (std::is_same<BuilderType, reflection::TypeRefBuilder>::value)
2927
+ if (auto parentDescriptor = readParentContextDescriptor (descriptor))
2928
+ if (parentDescriptor->isResolved ())
2929
+ if (auto resolvedParent = parentDescriptor->getResolved ())
2930
+ parent = readNominalTypeFromDescriptor (metadata, resolvedParent);
2931
+
2935
2932
// Build the nominal type.
2936
2933
BuiltType nominal;
2937
2934
if (descriptor->isGeneric ()) {
2938
2935
// Resolve the generic arguments.
2939
2936
auto builtGenerics = getGenericSubst (metadata, descriptor);
2940
2937
if (builtGenerics.empty ())
2941
2938
return BuiltType ();
2942
- nominal = Builder.createBoundGenericType (typeDecl, builtGenerics);
2939
+ nominal = Builder.createBoundGenericType (typeDecl, builtGenerics, parent );
2943
2940
} else {
2944
- nominal = Builder.createNominalType (typeDecl);
2941
+ nominal = Builder.createNominalType (typeDecl, parent );
2945
2942
}
2946
2943
2944
+ return nominal;
2945
+ }
2946
+
2947
+ BuiltType readNominalTypeFromMetadata (MetadataRef origMetadata,
2948
+ bool skipArtificialSubclasses = false ) {
2949
+ auto metadata = origMetadata;
2950
+ auto descriptorAddress =
2951
+ readAddressOfNominalTypeDescriptor (metadata, skipArtificialSubclasses);
2952
+ if (!descriptorAddress)
2953
+ return BuiltType ();
2954
+
2955
+ // If we've skipped an artificial subclasses, check the cache at
2956
+ // the superclass. (This also protects against recursion.)
2957
+ if (skipArtificialSubclasses && metadata != origMetadata) {
2958
+ auto it = TypeCache.find (getAddress (metadata));
2959
+ if (it != TypeCache.end ())
2960
+ return it->second ;
2961
+ }
2962
+
2963
+ // Read the nominal type descriptor.
2964
+ ContextDescriptorRef descriptor = readContextDescriptor (descriptorAddress);
2965
+ auto nominal = readNominalTypeFromDescriptor (origMetadata, descriptor);
2966
+
2947
2967
if (!nominal)
2948
2968
return BuiltType ();
2949
-
2969
+
2950
2970
TypeCache[getAddress (metadata)] = nominal;
2951
2971
2952
2972
// If we've skipped an artificial subclass, remove the
2953
2973
// recursion-protection entry we made for it.
2954
2974
if (skipArtificialSubclasses && metadata != origMetadata) {
2955
2975
TypeCache.erase (getAddress (origMetadata));
2956
2976
}
2957
-
2958
2977
return nominal;
2959
2978
}
2960
2979
2961
- BuiltType readNominalTypeFromClassMetadata (MetadataRef origMetadata,
2962
- bool skipArtificialSubclasses = false ) {
2980
+ BuiltType
2981
+ readNominalTypeFromClassMetadata (MetadataRef origMetadata,
2982
+ bool skipArtificialSubclasses = false ) {
2963
2983
auto classMeta = cast<TargetClassMetadata>(origMetadata);
2964
2984
if (classMeta->isTypeMetadata ())
2965
2985
return readNominalTypeFromMetadata (origMetadata, skipArtificialSubclasses);
0 commit comments