|
| 1 | +//===--- SILSymbolVisitor.h - Symbol Visitor for SIL ------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef SWIFT_SIL_SILSYMBOLVISITOR_H |
| 14 | +#define SWIFT_SIL_SILSYMBOLVISITOR_H |
| 15 | + |
| 16 | +#include "swift/AST/Decl.h" |
| 17 | +#include "swift/AST/ProtocolAssociations.h" |
| 18 | +#include "swift/AST/ProtocolConformance.h" |
| 19 | +#include "swift/SIL/SILDeclRef.h" |
| 20 | + |
| 21 | +namespace swift { |
| 22 | + |
| 23 | +/// Options dictating which symbols are enumerated by `SILSymbolVisitor`. |
| 24 | +struct SILSymbolVisitorOptions { |
| 25 | + /// Whether to visit the members of declarations like structs, classes, and |
| 26 | + /// protocols. |
| 27 | + bool VisitMembers = true; |
| 28 | + |
| 29 | + /// Whether to only visit declarations for which linker directive symbols |
| 30 | + /// are needed (e.g. decls with `@_originallyDefinedIn`. |
| 31 | + bool LinkerDirectivesOnly = false; |
| 32 | + |
| 33 | + /// Whether to only visit symbols with public linkage. |
| 34 | + bool PublicSymbolsOnly = true; |
| 35 | + |
| 36 | + /// Whether LLVM IR Virtual Function Elimination is enabled. |
| 37 | + bool VirtualFunctionElimination = false; |
| 38 | + |
| 39 | + /// Whether LLVM IR Witness Method Elimination is enabled. |
| 40 | + bool WitnessMethodElimination = false; |
| 41 | +}; |
| 42 | + |
| 43 | +/// Context for `SILSymbolVisitor` symbol enumeration. |
| 44 | +class SILSymbolVisitorContext { |
| 45 | + ModuleDecl *Module; |
| 46 | + const SILSymbolVisitorOptions Opts; |
| 47 | + |
| 48 | +public: |
| 49 | + SILSymbolVisitorContext(ModuleDecl *M, const SILSymbolVisitorOptions Opts) |
| 50 | + : Module{M}, Opts{Opts} { |
| 51 | + assert(M); |
| 52 | + }; |
| 53 | + |
| 54 | + ModuleDecl *getModule() const { return Module; } |
| 55 | + const SILSymbolVisitorOptions &getOpts() const { return Opts; } |
| 56 | +}; |
| 57 | + |
| 58 | +/// A visitor class which may be used to enumerate the entities representing |
| 59 | +/// linker symbols associated with a Swift declaration, file, or module. Some |
| 60 | +/// enumerated linker symbols can be represented as a `SILDeclRef`. The rest are |
| 61 | +/// enumerated ad-hoc. Additionally, there a some Obj-C entities (like methods) |
| 62 | +/// that don't actually have associated linker symbols but are enumerated for |
| 63 | +/// the convenience of clients using this utility for API surface discovery. |
| 64 | +class SILSymbolVisitor { |
| 65 | +public: |
| 66 | + virtual ~SILSymbolVisitor() {} |
| 67 | + |
| 68 | + /// Enumerate the symbols associated with the given decl. |
| 69 | + void visitDecl(Decl *D, const SILSymbolVisitorContext &ctx); |
| 70 | + |
| 71 | + /// Enumerate the symbols associated with the given file. |
| 72 | + void visitFile(FileUnit *file, const SILSymbolVisitorContext &ctx); |
| 73 | + |
| 74 | + /// Enumerate the symbols associated with the given modules. |
| 75 | + void visitModules(llvm::SmallVector<ModuleDecl *, 4> &modules, |
| 76 | + const SILSymbolVisitorContext &ctx); |
| 77 | + |
| 78 | + /// Override to prepare for enumeration of the symbols for a specific decl. |
| 79 | + virtual void willVisitDecl(Decl *D) {} |
| 80 | + |
| 81 | + /// Override to clean up after enumeration of the symbols for a specific decl. |
| 82 | + virtual void didVisitDecl(Decl *D) {} |
| 83 | + |
| 84 | + /// A classification for the dynamic dispatch metadata of a decl. |
| 85 | + enum class DynamicKind { |
| 86 | + /// May be replaced at runtime (e.g.`dynamic`). |
| 87 | + Replaceable, |
| 88 | + |
| 89 | + /// May replace another decl at runtime (e.g. `@_dynamicReplacement(for:)`). |
| 90 | + Replacement, |
| 91 | + }; |
| 92 | + |
| 93 | + virtual void addAssociatedConformanceDescriptor(AssociatedConformance AC) {} |
| 94 | + virtual void addAssociatedTypeDescriptor(AssociatedTypeDecl *ATD) {} |
| 95 | + virtual void addAsyncFunctionPointer(SILDeclRef declRef) {} |
| 96 | + virtual void addBaseConformanceDescriptor(BaseConformance BC) {} |
| 97 | + virtual void addClassMetadataBaseOffset(ClassDecl *CD) {} |
| 98 | + virtual void addDispatchThunk(SILDeclRef declRef) {} |
| 99 | + virtual void addDynamicFunction(AbstractFunctionDecl *AFD, |
| 100 | + DynamicKind dynKind) {} |
| 101 | + virtual void addEnumCase(EnumElementDecl *EED) {} |
| 102 | + virtual void addFieldOffset(VarDecl *VD) {} |
| 103 | + virtual void addFunction(SILDeclRef declRef) {} |
| 104 | + virtual void addFunction(StringRef name, SILDeclRef declRef) {} |
| 105 | + virtual void addGlobalVar(VarDecl *VD) {} |
| 106 | + virtual void addMethodDescriptor(SILDeclRef declRef) {} |
| 107 | + virtual void addMethodLookupFunction(ClassDecl *CD) {} |
| 108 | + virtual void addNominalTypeDescriptor(NominalTypeDecl *NTD) {} |
| 109 | + virtual void addObjCClass(ClassDecl *CD) {} |
| 110 | + virtual void addObjCInterface(ClassDecl *CD) {} |
| 111 | + virtual void addObjCMetaclass(ClassDecl *CD) {} |
| 112 | + virtual void addObjCMethod(AbstractFunctionDecl *AFD) {} |
| 113 | + virtual void addObjCResilientClassStub(ClassDecl *CD) {} |
| 114 | + virtual void addOpaqueTypeDescriptor(OpaqueTypeDecl *OTD) {} |
| 115 | + virtual void addOpaqueTypeDescriptorAccessor(OpaqueTypeDecl *OTD, |
| 116 | + DynamicKind dynKind) {} |
| 117 | + virtual void addPropertyDescriptor(AbstractStorageDecl *ASD) {} |
| 118 | + virtual void addProtocolConformanceDescriptor(RootProtocolConformance *C) {} |
| 119 | + virtual void addProtocolDescriptor(ProtocolDecl *PD) {} |
| 120 | + virtual void addProtocolRequirementsBaseDescriptor(ProtocolDecl *PD) {} |
| 121 | + virtual void addProtocolWitnessTable(RootProtocolConformance *C) {} |
| 122 | + virtual void addProtocolWitnessThunk(RootProtocolConformance *C, |
| 123 | + ValueDecl *requirementDecl) {} |
| 124 | + virtual void addSwiftMetaclassStub(ClassDecl *CD) {} |
| 125 | + virtual void addTypeMetadataAccessFunction(CanType T) {} |
| 126 | + virtual void addTypeMetadataAddress(CanType T) {} |
| 127 | +}; |
| 128 | + |
| 129 | +} // end namespace swift |
| 130 | + |
| 131 | +#endif |
0 commit comments