|
20 | 20 | #include "swift/Subsystems.h"
|
21 | 21 | #include "swift/AST/ASTContext.h"
|
22 | 22 | #include "swift/AST/Decl.h"
|
| 23 | +#include "swift/AST/ExistentialLayout.h" |
23 | 24 | #include "swift/AST/GenericSignature.h"
|
24 | 25 | #include "swift/AST/Module.h"
|
25 | 26 | #include "swift/AST/NameLookup.h"
|
@@ -808,6 +809,9 @@ class RemoteASTContextImpl {
|
808 | 809 | getDeclForRemoteNominalTypeDescriptor(RemoteAddress descriptor) = 0;
|
809 | 810 | virtual Result<RemoteAddress>
|
810 | 811 | getHeapMetadataForObject(RemoteAddress object) = 0;
|
| 812 | + virtual Result<std::pair<Type, RemoteAddress>> |
| 813 | + getDynamicTypeAndAddressForExistential(RemoteAddress object, |
| 814 | + Type staticType) = 0; |
811 | 815 |
|
812 | 816 | Result<uint64_t>
|
813 | 817 | getOffsetOfMember(Type type, RemoteAddress optMetadata, StringRef memberName){
|
@@ -1160,6 +1164,85 @@ class RemoteASTContextConcreteImpl final : public RemoteASTContextImpl {
|
1160 | 1164 | if (result) return RemoteAddress(*result);
|
1161 | 1165 | return getFailure<RemoteAddress>();
|
1162 | 1166 | }
|
| 1167 | + |
| 1168 | + Result<std::pair<Type, RemoteAddress>> |
| 1169 | + getDynamicTypeAndAddressClassExistential(RemoteAddress object) { |
| 1170 | + auto pointed = Reader.readPointedValue(object.getAddressData()); |
| 1171 | + if (!pointed) |
| 1172 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1173 | + auto result = Reader.readMetadataFromInstance(*pointed); |
| 1174 | + if (!result) |
| 1175 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1176 | + auto typeResult = Reader.readTypeFromMetadata(result.getValue()); |
| 1177 | + if (!typeResult) |
| 1178 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1179 | + return std::make_pair<Type, RemoteAddress>(std::move(typeResult), |
| 1180 | + std::move(object)); |
| 1181 | + } |
| 1182 | + |
| 1183 | + Result<std::pair<Type, RemoteAddress>> |
| 1184 | + getDynamicTypeAndAddressErrorExistential(RemoteAddress object) { |
| 1185 | + auto pointed = Reader.readPointedValue(object.getAddressData()); |
| 1186 | + if (!pointed) |
| 1187 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1188 | + auto result = |
| 1189 | + Reader.readMetadataAndValueErrorExistential(RemoteAddress(*pointed)); |
| 1190 | + if (!result) |
| 1191 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1192 | + RemoteAddress metadataAddress = result->first; |
| 1193 | + RemoteAddress valueAddress = result->second; |
| 1194 | + |
| 1195 | + auto typeResult = |
| 1196 | + Reader.readTypeFromMetadata(metadataAddress.getAddressData()); |
| 1197 | + if (!typeResult) |
| 1198 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1199 | + return std::make_pair<Type, RemoteAddress>(std::move(typeResult), |
| 1200 | + std::move(valueAddress)); |
| 1201 | + } |
| 1202 | + |
| 1203 | + Result<std::pair<Type, RemoteAddress>> |
| 1204 | + getDynamicTypeAndAddressOpaqueExistential(RemoteAddress object) { |
| 1205 | + auto result = Reader.readMetadataAndValueOpaqueExistential(object); |
| 1206 | + if (!result) |
| 1207 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1208 | + RemoteAddress metadataAddress = result->first; |
| 1209 | + RemoteAddress valueAddress = result->second; |
| 1210 | + |
| 1211 | + auto typeResult = |
| 1212 | + Reader.readTypeFromMetadata(metadataAddress.getAddressData()); |
| 1213 | + if (!typeResult) |
| 1214 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1215 | + return std::make_pair<Type, RemoteAddress>(std::move(typeResult), |
| 1216 | + std::move(valueAddress)); |
| 1217 | + } |
| 1218 | + |
| 1219 | + /// Resolve the dynamic type and the value address of an existential, |
| 1220 | + /// given its address and its static type. For class and error existentials, |
| 1221 | + /// this API takes a pointer to the instance reference rather than the |
| 1222 | + /// instance reference itself. |
| 1223 | + Result<std::pair<Type, RemoteAddress>> |
| 1224 | + getDynamicTypeAndAddressForExistential(RemoteAddress object, |
| 1225 | + Type staticType) override { |
| 1226 | + // If this is not an existential, give up. |
| 1227 | + if (!staticType->isAnyExistentialType()) |
| 1228 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1229 | + |
| 1230 | + // TODO: implement support for ExistentialMetaTypes. |
| 1231 | + if (!staticType->isExistentialType()) |
| 1232 | + return getFailure<std::pair<Type, RemoteAddress>>(); |
| 1233 | + |
| 1234 | + // This should be an existential type at this point. |
| 1235 | + auto layout = staticType->getExistentialLayout(); |
| 1236 | + switch (layout.getKind()) { |
| 1237 | + case ExistentialLayout::Kind::Class: |
| 1238 | + return getDynamicTypeAndAddressClassExistential(object); |
| 1239 | + case ExistentialLayout::Kind::Error: |
| 1240 | + return getDynamicTypeAndAddressErrorExistential(object); |
| 1241 | + case ExistentialLayout::Kind::Opaque: |
| 1242 | + return getDynamicTypeAndAddressOpaqueExistential(object); |
| 1243 | + } |
| 1244 | + llvm_unreachable("invalid type kind"); |
| 1245 | + } |
1163 | 1246 | };
|
1164 | 1247 |
|
1165 | 1248 | } // end anonymous namespace
|
@@ -1217,3 +1300,10 @@ Result<remote::RemoteAddress>
|
1217 | 1300 | RemoteASTContext::getHeapMetadataForObject(remote::RemoteAddress address) {
|
1218 | 1301 | return asImpl(Impl)->getHeapMetadataForObject(address);
|
1219 | 1302 | }
|
| 1303 | + |
| 1304 | +Result<std::pair<Type, remote::RemoteAddress>> |
| 1305 | +RemoteASTContext::getDynamicTypeAndAddressForExistential( |
| 1306 | + remote::RemoteAddress address, Type staticType) { |
| 1307 | + return asImpl(Impl)->getDynamicTypeAndAddressForExistential(address, |
| 1308 | + staticType); |
| 1309 | +} |
0 commit comments