1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
|
//===--- GenArchetype.cpp - Swift IR Generation for Archetype Types -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements IR generation for archetype types in Swift.
//
//===----------------------------------------------------------------------===//
#include "GenArchetype.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Decl.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/KnownProtocols.h"
#include "swift/AST/Types.h"
#include "swift/IRGen/Linking.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/TypeLowering.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "EnumPayload.h"
#include "Explosion.h"
#include "FixedTypeInfo.h"
#include "GenClass.h"
#include "GenHeap.h"
#include "GenMeta.h"
#include "GenOpaque.h"
#include "GenPointerAuth.h"
#include "GenPoly.h"
#include "GenProto.h"
#include "GenType.h"
#include "HeapTypeInfo.h"
#include "IRGenDebugInfo.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "LocalTypeData.h"
#include "MetadataRequest.h"
#include "Outlining.h"
#include "ProtocolInfo.h"
#include "ResilientTypeInfo.h"
#include "TypeInfo.h"
#include "TypeLayout.h"
using namespace swift;
using namespace irgen;
MetadataResponse
irgen::emitArchetypeTypeMetadataRef(IRGenFunction &IGF,
CanArchetypeType archetype,
DynamicMetadataRequest request) {
assert(!isa<PackArchetypeType>(archetype));
// Check for an existing cache entry.
if (auto response = IGF.tryGetLocalTypeMetadata(archetype, request))
return response;
// If this is an opaque archetype, we'll need to instantiate using its
// descriptor.
if (auto opaque = dyn_cast<OpaqueTypeArchetypeType>(archetype)) {
if (opaque->isRoot())
return emitOpaqueTypeMetadataRef(IGF, opaque, request);
}
#ifndef NDEBUG
if (!archetype->getParent()) {
llvm::errs() << "Metadata for archetype not bound in function.\n"
<< " The metadata could be missing entirely because it needs "
"to be passed to the function.\n"
<< " Or the metadata is present and not bound in which case "
"setScopedLocalTypeMetadata or similar must be called.\n";
llvm::errs() << "Archetype without metadata: " << archetype << "\n";
archetype->dump(llvm::errs());
llvm::errs() << "Function:\n";
IGF.CurFn->print(llvm::errs());
if (auto localTypeData = IGF.getLocalTypeData()) {
llvm::errs() << "LocalTypeData:\n";
localTypeData->dump();
} else {
llvm::errs() << "No LocalTypeDataCache for this function!\n";
}
}
#endif
// If there's no local or opaque metadata, it must be a nested type.
assert(archetype->getParent() && "Not a nested archetype");
CanArchetypeType parent(archetype->getParent());
AssociatedType association(
archetype->getInterfaceType()->castTo<DependentMemberType>()
->getAssocType());
MetadataResponse response =
emitAssociatedTypeMetadataRef(IGF, parent, association, request);
setTypeMetadataName(IGF.IGM, response.getMetadata(), archetype);
IGF.setScopedLocalTypeMetadata(archetype, response);
return response;
}
namespace {
/// A type implementation for an ArchetypeType, otherwise known as a
/// type variable: for example, Self in a protocol declaration, or T
/// in a generic declaration like foo<T>(x : T) -> T. The critical
/// thing here is that performing an operation involving archetypes
/// is dependent on the witness binding we can see.
class OpaqueArchetypeTypeInfo
: public ResilientTypeInfo<OpaqueArchetypeTypeInfo>
{
OpaqueArchetypeTypeInfo(llvm::Type *type, IsABIAccessible_t abiAccessible)
: ResilientTypeInfo(type, IsCopyable, abiAccessible) {}
public:
static const OpaqueArchetypeTypeInfo *
create(llvm::Type *type, IsABIAccessible_t abiAccessible) {
return new OpaqueArchetypeTypeInfo(type, abiAccessible);
}
void collectMetadataForOutlining(OutliningMetadataCollector &collector,
SILType T) const override {
// We'll need formal type metadata for this archetype.
collector.collectTypeMetadata(T);
}
TypeLayoutEntry
*buildTypeLayoutEntry(IRGenModule &IGM,
SILType T,
bool useStructLayouts) const override {
return IGM.typeLayoutCache.getOrCreateArchetypeEntry(T.getObjectType());
}
};
/// A type implementation for a class archetype, that is, an archetype
/// bounded by a class protocol constraint. These archetypes can be
/// represented by a refcounted pointer instead of an opaque value buffer.
/// If ObjC interop is disabled, we can use Swift refcounting entry
/// points, otherwise we have to use the unknown ones.
class ClassArchetypeTypeInfo
: public HeapTypeInfo<ClassArchetypeTypeInfo>
{
ReferenceCounting RefCount;
ClassArchetypeTypeInfo(llvm::PointerType *storageType,
Size size, const SpareBitVector &spareBits,
Alignment align,
ReferenceCounting refCount)
: HeapTypeInfo(refCount, storageType, size, spareBits, align),
RefCount(refCount)
{}
public:
static const ClassArchetypeTypeInfo *create(llvm::PointerType *storageType,
Size size, const SpareBitVector &spareBits,
Alignment align,
ReferenceCounting refCount) {
return new ClassArchetypeTypeInfo(storageType, size, spareBits, align,
refCount);
}
ReferenceCounting getReferenceCounting() const {
return RefCount;
}
};
class FixedSizeArchetypeTypeInfo
: public PODSingleScalarTypeInfo<FixedSizeArchetypeTypeInfo, LoadableTypeInfo>
{
FixedSizeArchetypeTypeInfo(llvm::Type *type, Size size, Alignment align,
const SpareBitVector &spareBits)
: PODSingleScalarTypeInfo(type, size, spareBits, align) {}
public:
static const FixedSizeArchetypeTypeInfo *
create(llvm::Type *type, Size size, Alignment align,
const SpareBitVector &spareBits) {
return new FixedSizeArchetypeTypeInfo(type, size, align, spareBits);
}
};
} // end anonymous namespace
/// Emit a single protocol witness table reference.
llvm::Value *irgen::emitArchetypeWitnessTableRef(IRGenFunction &IGF,
CanArchetypeType archetype,
ProtocolDecl *protocol) {
assert(Lowering::TypeConverter::protocolRequiresWitnessTable(protocol) &&
"looking up witness table for protocol that doesn't have one");
// The following approach assumes that a protocol will only appear in
// an archetype's conformsTo array if the archetype is either explicitly
// constrained to conform to that protocol (in which case we should have
// a cache entry for it) or there's an associated type declaration with
// that protocol listed as a direct requirement.
auto localDataKind =
LocalTypeDataKind::forAbstractProtocolWitnessTable(protocol);
// Check immediately for an existing cache entry.
// TODO: don't give this absolute precedence over other access paths.
auto wtable = IGF.tryGetLocalTypeData(archetype, localDataKind);
if (wtable) return wtable;
auto environment = archetype->getGenericEnvironment();
// Otherwise, ask the generic signature for the best path to the conformance.
// TODO: this isn't necessarily optimal if the direct conformance isn't
// concretely available; we really ought to be comparing the full paths
// to this conformance from concrete sources.
auto signature = environment->getGenericSignature().getCanonicalSignature();
auto archetypeDepType = archetype->getInterfaceType();
auto astPath = signature->getConformancePath(archetypeDepType, protocol);
auto i = astPath.begin(), e = astPath.end();
assert(i != e && "empty path!");
// The first entry in the path is a direct requirement of the signature,
// for which we should always have local type data available.
CanType rootArchetype =
environment->mapTypeIntoContext(i->first)->getCanonicalType();
ProtocolDecl *rootProtocol = i->second;
// Turn the rest of the path into a MetadataPath.
auto lastProtocol = rootProtocol;
MetadataPath path;
while (++i != e) {
auto &entry = *i;
CanType depType = CanType(entry.first);
ProtocolDecl *requirement = entry.second;
const ProtocolInfo &lastPI =
IGF.IGM.getProtocolInfo(lastProtocol,
ProtocolInfoKind::RequirementSignature);
// If it's a type parameter, it's self, and this is a base protocol
// requirement.
if (isa<GenericTypeParamType>(depType)) {
assert(depType->isEqual(lastProtocol->getSelfInterfaceType()));
WitnessIndex index = lastPI.getBaseIndex(requirement);
path.addInheritedProtocolComponent(index);
// Otherwise, it's an associated conformance requirement.
} else {
AssociatedConformance association(lastProtocol, depType, requirement);
WitnessIndex index = lastPI.getAssociatedConformanceIndex(association);
path.addAssociatedConformanceComponent(index);
}
lastProtocol = requirement;
}
assert(lastProtocol == protocol);
auto rootWTable = IGF.tryGetLocalTypeData(rootArchetype,
LocalTypeDataKind::forAbstractProtocolWitnessTable(rootProtocol));
// Fetch an opaque type's witness table if it wasn't cached yet.
if (!rootWTable) {
if (auto opaqueRoot = dyn_cast<OpaqueTypeArchetypeType>(rootArchetype)) {
rootWTable = emitOpaqueTypeWitnessTableRef(IGF, opaqueRoot,
rootProtocol);
}
#ifndef NDEBUG
if (!rootWTable) {
llvm::errs()
<< "Root witness table not bound in function.\n"
<< " The witness table could be missing entirely because it needs "
"to be passed to the function.\n"
<< " Or the witness table is present and not bound in which case "
"setScopedLocalTypeData or similar must be called.\n";
llvm::errs() << "Root archetype for conformance: " << rootArchetype
<< "\n";
rootArchetype->dump(llvm::errs());
llvm::errs() << "Root protocol without wtable: " << rootProtocol << "\n";
rootProtocol->dump(llvm::errs());
llvm::errs() << "Archetype for conformance: " << archetype << "\n";
archetype->dump(llvm::errs());
llvm::errs() << "Protocol for conformance: " << protocol << "\n";
protocol->dump(llvm::errs());
llvm::errs() << "Function:\n";
IGF.CurFn->print(llvm::errs());
if (auto localTypeData = IGF.getLocalTypeData()) {
llvm::errs() << "LocalTypeData:\n";
localTypeData->dump();
} else {
llvm::errs() << "No LocalTypeDataCache for this function!\n";
}
}
#endif
assert(rootWTable && "root witness table not bound in local context!");
}
wtable = path.followFromWitnessTable(IGF, rootArchetype,
ProtocolConformanceRef(rootProtocol),
MetadataResponse::forComplete(rootWTable),
/*request*/ MetadataState::Complete,
nullptr).getMetadata();
IGF.setScopedLocalTypeData(archetype, localDataKind, wtable);
return wtable;
}
MetadataResponse
irgen::emitAssociatedTypeMetadataRef(IRGenFunction &IGF,
CanArchetypeType origin,
AssociatedType association,
DynamicMetadataRequest request) {
// Find the conformance of the origin to the associated type's protocol.
llvm::Value *wtable = emitArchetypeWitnessTableRef(IGF, origin,
association.getSourceProtocol());
// Find the origin's type metadata.
llvm::Value *originMetadata =
emitArchetypeTypeMetadataRef(IGF, origin, MetadataState::Abstract)
.getMetadata();
return emitAssociatedTypeMetadataRef(IGF, originMetadata, wtable,
association, request);
}
const TypeInfo *TypeConverter::convertArchetypeType(ArchetypeType *archetype) {
assert(isExemplarArchetype(archetype) && "lowering non-exemplary archetype");
auto layout = archetype->getLayoutConstraint();
// If the archetype is class-constrained, use a class pointer
// representation.
if (layout && layout->isRefCounted()) {
auto refcount = archetype->getReferenceCounting();
llvm::PointerType *reprTy;
// If the archetype has a superclass constraint, it has at least the
// retain semantics of its superclass, and it can be represented with
// the supertype's pointer type.
if (auto super = archetype->getSuperclass()) {
auto &superTI = IGM.getTypeInfoForUnlowered(super);
reprTy = cast<llvm::PointerType>(superTI.StorageType);
} else {
if (refcount == ReferenceCounting::Native) {
reprTy = IGM.RefCountedPtrTy;
} else {
reprTy = IGM.UnknownRefCountedPtrTy;
}
}
// As a hack, assume class archetypes never have spare bits. There's a
// corresponding hack in MultiPayloadEnumImplStrategy::completeEnumTypeLayout
// to ignore spare bits of dependent-typed payloads.
auto spareBits =
SpareBitVector::getConstant(IGM.getPointerSize().getValueInBits(), false);
return ClassArchetypeTypeInfo::create(reprTy,
IGM.getPointerSize(),
spareBits,
IGM.getPointerAlignment(),
refcount);
}
// If the archetype is trivial fixed-size layout-constrained, use a fixed size
// representation.
if (layout && layout->isFixedSizeTrivial()) {
Size size(layout->getTrivialSizeInBytes());
auto layoutAlignment = layout->getAlignmentInBytes();
assert(layoutAlignment && "layout constraint alignment should not be 0");
Alignment align(layoutAlignment);
auto spareBits =
SpareBitVector::getConstant(size.getValueInBits(), false);
// Get an integer type of the required size.
auto ProperlySizedIntTy = SILType::getBuiltinIntegerType(
size.getValueInBits(), IGM.getSwiftModule()->getASTContext());
auto storageType = IGM.getStorageType(ProperlySizedIntTy);
return FixedSizeArchetypeTypeInfo::create(storageType, size, align,
spareBits);
}
// If the archetype is a trivial layout-constrained, use a POD
// representation. This type is not loadable, but it is known
// to be a POD.
if (layout && layout->isAddressOnlyTrivial()) {
// TODO: Create NonFixedSizeArchetypeTypeInfo and return it.
}
// Otherwise, for now, always use an opaque indirect type.
llvm::Type *storageType = IGM.OpaqueTy;
// Opaque result types can be private and from a different module. In this
// case we can't access their type metadata from another module.
IsABIAccessible_t abiAccessible = IsABIAccessible;
if (auto opaqueArchetype = dyn_cast<OpaqueTypeArchetypeType>(archetype)) {
auto ¤tSILModule = IGM.getSILModule();
abiAccessible =
currentSILModule.isTypeMetadataAccessible(archetype->getCanonicalType())
? IsABIAccessible
: IsNotABIAccessible;
}
// TODO: Should this conformance imply isAddressOnlyTrivial is true?
auto *bitwiseCopyableProtocol =
IGM.getSwiftModule()->getASTContext().getProtocol(
KnownProtocolKind::BitwiseCopyable);
// The protocol won't be present in swiftinterfaces from older SDKs.
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->checkConformance(
archetype, bitwiseCopyableProtocol)) {
return BitwiseCopyableTypeInfo::create(storageType, abiAccessible);
}
return OpaqueArchetypeTypeInfo::create(storageType, abiAccessible);
}
llvm::Value *irgen::emitDynamicTypeOfOpaqueArchetype(IRGenFunction &IGF,
Address addr,
SILType type) {
auto archetype = type.castTo<ArchetypeType>();
// Acquire the archetype's static metadata.
llvm::Value *metadata =
emitArchetypeTypeMetadataRef(IGF, archetype, MetadataState::Complete)
.getMetadata();
return IGF.Builder.CreateCall(
IGF.IGM.getGetDynamicTypeFunctionPointer(),
{addr.getAddress(), metadata, llvm::ConstantInt::get(IGF.IGM.Int1Ty, 0)});
}
static void
withOpaqueTypeGenericArgs(IRGenFunction &IGF,
CanOpaqueTypeArchetypeType archetype,
llvm::function_ref<void (llvm::Value*)> body) {
// Collect the generic arguments of the opaque decl.
auto opaqueDecl = archetype->getDecl();
auto generics = opaqueDecl->getGenericSignatureOfContext();
llvm::Value *genericArgs;
Address alloca;
Size allocaSize(0);
if (!generics || generics->areAllParamsConcrete()) {
genericArgs = llvm::UndefValue::get(IGF.IGM.Int8PtrTy);
} else {
SmallVector<llvm::Value *, 4> args;
SmallVector<llvm::Type *, 4> types;
enumerateGenericSignatureRequirements(
opaqueDecl->getGenericSignature().getCanonicalSignature(),
[&](GenericRequirement reqt) {
auto arg = emitGenericRequirementFromSubstitutions(
IGF, reqt, MetadataState::Abstract,
archetype->getSubstitutions());
args.push_back(arg);
types.push_back(args.back()->getType());
});
auto bufTy = llvm::StructType::get(IGF.IGM.getLLVMContext(), types);
alloca = IGF.createAlloca(bufTy, IGF.IGM.getPointerAlignment());
allocaSize = IGF.IGM.getPointerSize() * args.size();
IGF.Builder.CreateLifetimeStart(alloca, allocaSize);
for (auto i : indices(args)) {
IGF.Builder.CreateStore(args[i],
IGF.Builder.CreateStructGEP(alloca, i, i * IGF.IGM.getPointerSize()));
}
genericArgs = IGF.Builder.CreateBitCast(alloca.getAddress(),
IGF.IGM.Int8PtrTy);
}
// Pass them down to the body.
body(genericArgs);
// End the alloca's lifetime, if we allocated one.
if (alloca.getAddress()) {
IGF.Builder.CreateLifetimeEnd(alloca, allocaSize);
}
}
bool shouldUseOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *opaque) {
auto *namingDecl = opaque->getNamingDecl();
// Don't emit accessors for abstract storage that is not dynamic or a dynamic
// replacement.
if (auto *abstractStorage = dyn_cast<AbstractStorageDecl>(namingDecl)) {
return abstractStorage->hasAnyNativeDynamicAccessors() ||
abstractStorage->getDynamicallyReplacedDecl();
}
// Don't emit accessors for functions that are not dynamic or dynamic
// replacements.
return namingDecl->shouldUseNativeDynamicDispatch() ||
(bool)namingDecl->getDynamicallyReplacedDecl();
}
static llvm::Value *
getAddressOfOpaqueTypeDescriptor(IRGenFunction &IGF,
OpaqueTypeDecl *opaqueDecl) {
auto &IGM = IGF.IGM;
// Support dynamically replacing the return type as part of dynamic function
// replacement.
if (!IGM.getOptions().shouldOptimize() &&
shouldUseOpaqueTypeDescriptorAccessor(opaqueDecl)) {
auto descriptorAccessor = IGM.getAddrOfOpaqueTypeDescriptorAccessFunction(
opaqueDecl, NotForDefinition, false);
auto desc = IGF.Builder.CreateCall(descriptorAccessor, {});
desc->setDoesNotThrow();
desc->setCallingConv(IGM.SwiftCC);
return desc;
}
return IGM.getAddrOfOpaqueTypeDescriptor(opaqueDecl, ConstantInit());
}
MetadataResponse irgen::emitOpaqueTypeMetadataRef(IRGenFunction &IGF,
CanOpaqueTypeArchetypeType archetype,
DynamicMetadataRequest request) {
bool signedDescriptor = IGF.IGM.getAvailabilityContext().isContainedIn(
IGF.IGM.Context.getSignedDescriptorAvailability());
auto accessorFn = signedDescriptor ?
IGF.IGM.getGetOpaqueTypeMetadata2FunctionPointer() :
IGF.IGM.getGetOpaqueTypeMetadataFunctionPointer();
auto opaqueDecl = archetype->getDecl();
auto genericParam = archetype->getInterfaceType()
->castTo<GenericTypeParamType>();
auto *descriptor = getAddressOfOpaqueTypeDescriptor(IGF, opaqueDecl);
auto indexValue = llvm::ConstantInt::get(
IGF.IGM.SizeTy, genericParam->getIndex());
// Sign the descriptor.
auto schema =
IGF.IGM.getOptions().PointerAuth.OpaqueTypeDescriptorsAsArguments;
if (schema && signedDescriptor) {
auto authInfo = PointerAuthInfo::emit(
IGF, schema, nullptr,
PointerAuthEntity::Special::OpaqueTypeDescriptorAsArgument);
descriptor = emitPointerAuthSign(IGF, descriptor, authInfo);
}
llvm::CallInst *result = nullptr;
withOpaqueTypeGenericArgs(IGF, archetype,
[&](llvm::Value *genericArgs) {
result = IGF.Builder.CreateCall(accessorFn,
{request.get(IGF), genericArgs, descriptor, indexValue});
result->setDoesNotThrow();
result->setCallingConv(IGF.IGM.SwiftCC);
result->setOnlyReadsMemory();
});
assert(result);
auto response = MetadataResponse::handle(IGF, request, result);
IGF.setScopedLocalTypeMetadata(archetype, response);
return response;
}
llvm::Value *irgen::emitOpaqueTypeWitnessTableRef(IRGenFunction &IGF,
CanOpaqueTypeArchetypeType archetype,
ProtocolDecl *protocol) {
bool signedDescriptor = IGF.IGM.getAvailabilityContext().isContainedIn(
IGF.IGM.Context.getSignedDescriptorAvailability());
auto accessorFn = signedDescriptor ?
IGF.IGM.getGetOpaqueTypeConformance2FunctionPointer() :
IGF.IGM.getGetOpaqueTypeConformanceFunctionPointer();
auto opaqueDecl = archetype->getDecl();
assert(archetype->isRoot() && "Can only follow from the root");
llvm::Value *descriptor = getAddressOfOpaqueTypeDescriptor(IGF, opaqueDecl);
// Sign the descriptor.
auto schema =
IGF.IGM.getOptions().PointerAuth.OpaqueTypeDescriptorsAsArguments;
if (schema && signedDescriptor) {
auto authInfo = PointerAuthInfo::emit(
IGF, schema, nullptr,
PointerAuthEntity::Special::OpaqueTypeDescriptorAsArgument);
descriptor = emitPointerAuthSign(IGF, descriptor, authInfo);
}
// Compute the index at which this witness table resides.
unsigned index = opaqueDecl->getOpaqueGenericParams().size();
auto opaqueReqs =
opaqueDecl->getOpaqueInterfaceGenericSignature().getRequirements();
bool found = false;
for (const auto &req : opaqueReqs) {
auto reqProto = opaqueTypeRequiresWitnessTable(opaqueDecl, req);
if (!reqProto)
continue;
// Is this requirement the one we're looking for?
if (reqProto == protocol &&
req.getFirstType()->isEqual(archetype->getInterfaceType())) {
found = true;
break;
}
++index;
}
(void)found;
assert(found && "Opaque type does not conform to protocol");
auto indexValue = llvm::ConstantInt::get(IGF.IGM.SizeTy, index);
llvm::CallInst *result = nullptr;
withOpaqueTypeGenericArgs(IGF, archetype,
[&](llvm::Value *genericArgs) {
result = IGF.Builder.CreateCall(accessorFn,
{genericArgs, descriptor, indexValue});
result->setDoesNotThrow();
result->setCallingConv(IGF.IGM.SwiftCC);
result->setOnlyReadsMemory();
});
assert(result);
IGF.setScopedLocalTypeData(archetype,
LocalTypeDataKind::forAbstractProtocolWitnessTable(protocol),
result);
return result;
}
|