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
|
//===--- IRSymbolVisitor.cpp - IR Linker Symbol Visitor ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 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 liker symbol enumeration for IRSymbolVisitor.
//
//===----------------------------------------------------------------------===//
#include "swift/IRGen/IRSymbolVisitor.h"
#include "swift/SIL/SILSymbolVisitor.h"
using namespace swift;
using namespace irgen;
/// Determine whether dynamic replacement should be emitted for the allocator or
/// the initializer given a decl. The rule is that structs and convenience init
/// of classes emit a dynamic replacement for the allocator. A designated init
/// of a class emits a dynamic replacement for the initializer. This is because
/// the super class init call is emitted to the initializer and needs to be
/// dynamic.
static bool shouldUseAllocatorMangling(const AbstractFunctionDecl *AFD) {
auto constructor = dyn_cast<ConstructorDecl>(AFD);
if (!constructor)
return false;
return constructor->getParent()->getSelfClassDecl() == nullptr ||
constructor->isConvenienceInit();
}
/// The underlying implementation of the IR symbol visitor logic. This class is
/// responsible for overriding the abstract methods of `SILSymbolVisitor` and
/// emitting `LinkEntity` instances to the downstream visitor in addition to
/// passing through `SILDeclRef`s produced by the SIL symbol visitor.
class IRSymbolVisitorImpl : public SILSymbolVisitor {
IRSymbolVisitor &Visitor;
const IRSymbolVisitorContext &Ctx;
bool PublicOrPackageSymbolsOnly;
/// Emits the given `LinkEntity` to the downstream visitor as long as the
/// entity has the required linkage.
///
/// FIXME: The need for an ignoreVisibility flag here possibly indicates that
/// there is something broken about the linkage computation below.
void addLinkEntity(LinkEntity entity, bool ignoreVisibility = false) {
if (!ignoreVisibility) {
auto linkage =
LinkInfo::get(Ctx.getLinkInfo(), Ctx.getSILCtx().getModule(), entity,
ForDefinition);
auto externallyVisible =
llvm::GlobalValue::isExternalLinkage(linkage.getLinkage()) &&
linkage.getVisibility() != llvm::GlobalValue::HiddenVisibility;
if (PublicOrPackageSymbolsOnly && !externallyVisible)
return;
}
Visitor.addLinkEntity(entity);
}
public:
IRSymbolVisitorImpl(IRSymbolVisitor &Visitor,
const IRSymbolVisitorContext &Ctx)
: Visitor{Visitor}, Ctx{Ctx},
PublicOrPackageSymbolsOnly{Ctx.getSILCtx().getOpts().PublicOrPackageSymbolsOnly} {}
bool willVisitDecl(Decl *D) override {
return Visitor.willVisitDecl(D);
}
void didVisitDecl(Decl *D) override {
Visitor.didVisitDecl(D);
}
void addAssociatedConformanceDescriptor(AssociatedConformance AC) override {
addLinkEntity(LinkEntity::forAssociatedConformanceDescriptor(AC));
}
void addAssociatedTypeDescriptor(AssociatedTypeDecl *ATD) override {
addLinkEntity(LinkEntity::forAssociatedTypeDescriptor(ATD));
}
void addAsyncFunctionPointer(SILDeclRef declRef) override {
addLinkEntity(LinkEntity::forAsyncFunctionPointer(declRef),
/*ignoreVisibility=*/true);
}
void addBaseConformanceDescriptor(BaseConformance BC) override {
addLinkEntity(LinkEntity::forBaseConformanceDescriptor(BC));
}
void addClassMetadataBaseOffset(ClassDecl *CD) override {
addLinkEntity(LinkEntity::forClassMetadataBaseOffset(CD));
}
void addDispatchThunk(SILDeclRef declRef) override {
auto entity = LinkEntity::forDispatchThunk(declRef);
addLinkEntity(entity);
if (declRef.getAbstractFunctionDecl()->hasAsync())
addLinkEntity(LinkEntity::forAsyncFunctionPointer(entity));
}
void addDynamicFunction(AbstractFunctionDecl *AFD,
DynamicKind dynKind) override {
bool useAllocator = shouldUseAllocatorMangling(AFD);
addLinkEntity(LinkEntity::forDynamicallyReplaceableFunctionVariable(
AFD, useAllocator));
switch (dynKind) {
case DynamicKind::Replaceable:
addLinkEntity(
LinkEntity::forDynamicallyReplaceableFunctionKey(AFD, useAllocator));
break;
case DynamicKind::Replacement:
addLinkEntity(
LinkEntity::forDynamicallyReplaceableFunctionImpl(AFD, useAllocator));
break;
}
}
void addEnumCase(EnumElementDecl *EED) override {
addLinkEntity(LinkEntity::forEnumCase(EED));
}
void addFieldOffset(VarDecl *VD) override {
addLinkEntity(LinkEntity::forFieldOffset(VD));
}
void addFunction(SILDeclRef declRef) override {
Visitor.addFunction(declRef);
}
void addFunction(StringRef name, SILDeclRef declRef) override {
Visitor.addFunction(name, declRef);
}
void addGlobalVar(VarDecl *VD) override {
Visitor.addGlobalVar(VD);
}
void addMethodDescriptor(SILDeclRef declRef) override {
addLinkEntity(LinkEntity::forMethodDescriptor(declRef));
}
void addMethodLookupFunction(ClassDecl *CD) override {
addLinkEntity(LinkEntity::forMethodLookupFunction(CD));
}
void addNominalTypeDescriptor(NominalTypeDecl *NTD) override {
addLinkEntity(LinkEntity::forNominalTypeDescriptor(NTD));
}
void addObjCInterface(ClassDecl *CD) override {
Visitor.addObjCInterface(CD);
}
void addObjCMetaclass(ClassDecl *CD) override {
addLinkEntity(LinkEntity::forObjCMetaclass(CD));
}
void addObjCMethod(AbstractFunctionDecl *AFD) override {
// Pass through; Obj-C methods don't have linkable symbols.
Visitor.addObjCMethod(AFD);
}
void addObjCResilientClassStub(ClassDecl *CD) override {
addLinkEntity(LinkEntity::forObjCResilientClassStub(
CD, TypeMetadataAddress::AddressPoint));
}
void addOpaqueTypeDescriptor(OpaqueTypeDecl *OTD) override {
addLinkEntity(LinkEntity::forOpaqueTypeDescriptor(OTD));
}
void addOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *OTD,
DynamicKind dynKind) override {
addLinkEntity(LinkEntity::forOpaqueTypeDescriptorAccessor(OTD));
switch (dynKind) {
case DynamicKind::Replaceable:
addLinkEntity(LinkEntity::forOpaqueTypeDescriptorAccessorImpl(OTD));
addLinkEntity(LinkEntity::forOpaqueTypeDescriptorAccessorKey(OTD));
break;
case DynamicKind::Replacement:
break;
}
addLinkEntity(LinkEntity::forOpaqueTypeDescriptorAccessorVar(OTD));
}
void addPropertyDescriptor(AbstractStorageDecl *ASD) override {
addLinkEntity(LinkEntity::forPropertyDescriptor(ASD));
}
void addProtocolConformanceDescriptor(RootProtocolConformance *C) override {
addLinkEntity(LinkEntity::forProtocolConformanceDescriptor(C));
}
void addProtocolDescriptor(ProtocolDecl *PD) override {
addLinkEntity(LinkEntity::forProtocolDescriptor(PD));
}
void addProtocolRequirementsBaseDescriptor(ProtocolDecl *PD) override {
addLinkEntity(LinkEntity::forProtocolRequirementsBaseDescriptor(PD));
}
void addProtocolWitnessTable(RootProtocolConformance *C) override {
addLinkEntity(LinkEntity::forProtocolWitnessTable(C));
}
void addProtocolWitnessThunk(RootProtocolConformance *C,
ValueDecl *requirementDecl) override {
Visitor.addProtocolWitnessThunk(C, requirementDecl);
}
void addSwiftMetaclassStub(ClassDecl *CD) override {
addLinkEntity(LinkEntity::forSwiftMetaclassStub(CD));
}
void addTypeMetadataAccessFunction(CanType T) override {
addLinkEntity(LinkEntity::forTypeMetadataAccessFunction(T));
}
void addTypeMetadataAddress(CanType T) override {
addLinkEntity(
LinkEntity::forTypeMetadata(T, TypeMetadataAddress::AddressPoint));
}
};
void IRSymbolVisitor::visit(Decl *D, const IRSymbolVisitorContext &Ctx) {
IRSymbolVisitorImpl(*this, Ctx).visitDecl(D, Ctx.getSILCtx());
}
void IRSymbolVisitor::visitFile(FileUnit *file,
const IRSymbolVisitorContext &Ctx) {
IRSymbolVisitorImpl(*this, Ctx).visitFile(file, Ctx.getSILCtx());
}
void IRSymbolVisitor::visitModules(llvm::SmallVector<ModuleDecl *, 4> &modules,
const IRSymbolVisitorContext &Ctx) {
IRSymbolVisitorImpl(*this, Ctx).visitModules(modules, Ctx.getSILCtx());
}
|