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
|
//===--- TBDGenVisitor.h - AST Visitor for TBD generation -----------------===//
//
// 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 defines the visitor that finds all symbols in a swift AST.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_TBDGEN_TBDGENVISITOR_H
#define SWIFT_TBDGEN_TBDGENVISITOR_H
#include "swift/AST/ASTMangler.h"
#include "swift/AST/FileUnit.h"
#include "swift/AST/Module.h"
#include "swift/Basic/LLVM.h"
#include "swift/IRGen/IRSymbolVisitor.h"
#include "swift/IRGen/Linking.h"
#include "swift/SIL/SILDeclRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/TargetParser/Triple.h"
#include "llvm/TextAPI/InterfaceFile.h"
using namespace swift::irgen;
using StringSet = llvm::StringSet<>;
namespace llvm {
class DataLayout;
}
namespace swift {
class TBDGenDescriptor;
struct TBDGenOptions;
class SymbolSource;
namespace tbdgen {
enum class LinkerPlatformId: uint8_t {
#define LD_PLATFORM(Name, Id) Name = Id,
#include "ldPlatformKinds.def"
};
struct InstallNameStore {
// The default install name to use when no specific install name is specified.
std::string InstallName;
// The install name specific to the platform id. This takes precedence over
// the default install name.
std::map<LinkerPlatformId, std::string> PlatformInstallName;
StringRef getInstallName(LinkerPlatformId Id) const;
};
/// A set of callbacks for recording APIs.
class APIRecorder {
public:
virtual ~APIRecorder() {}
virtual void addSymbol(StringRef name, llvm::MachO::EncodeKind kind,
SymbolSource source, Decl *decl,
llvm::MachO::SymbolFlags flags) {}
virtual void addObjCInterface(const ClassDecl *decl) {}
virtual void addObjCCategory(const ExtensionDecl *decl) {}
virtual void addObjCMethod(const GenericContext *ctx, SILDeclRef method) {}
};
class SimpleAPIRecorder final : public APIRecorder {
public:
using SymbolCallbackFn =
llvm::function_ref<void(StringRef, llvm::MachO::EncodeKind, SymbolSource,
Decl *, llvm::MachO::SymbolFlags)>;
SimpleAPIRecorder(SymbolCallbackFn func) : func(func) {}
void addSymbol(StringRef symbol, llvm::MachO::EncodeKind kind,
SymbolSource source, Decl *decl,
llvm::MachO::SymbolFlags flags) override {
func(symbol, kind, source, decl, flags);
}
private:
SymbolCallbackFn func;
};
class TBDGenVisitor : public IRSymbolVisitor {
#ifndef NDEBUG
/// Tracks the symbols emitted to ensure we don't emit any duplicates.
llvm::StringSet<> DuplicateSymbolChecker;
#endif
std::optional<llvm::DataLayout> DataLayout = std::nullopt;
const StringRef DataLayoutDescription;
UniversalLinkageInfo UniversalLinkInfo;
ModuleDecl *SwiftModule;
const TBDGenOptions &Opts;
APIRecorder &recorder;
using EncodeKind = llvm::MachO::EncodeKind;
using SymbolFlags = llvm::MachO::SymbolFlags;
std::vector<Decl*> DeclStack;
std::unique_ptr<std::map<std::string, InstallNameStore>>
previousInstallNameMap;
std::unique_ptr<std::map<std::string, InstallNameStore>>
parsePreviousModuleInstallNameMap();
void addSymbolInternal(StringRef name, EncodeKind kind, SymbolSource source,
SymbolFlags);
void addLinkerDirectiveSymbolsLdHide(StringRef name, EncodeKind kind);
void addLinkerDirectiveSymbolsLdPrevious(StringRef name, EncodeKind kind);
void addSymbol(StringRef name, SymbolSource source, SymbolFlags flags,
EncodeKind kind = EncodeKind::GlobalSymbol);
void addSymbol(LinkEntity entity);
bool addClassMetadata(ClassDecl *CD);
public:
TBDGenVisitor(const llvm::Triple &target, const StringRef dataLayoutString,
ModuleDecl *swiftModule, const TBDGenOptions &opts,
APIRecorder &recorder)
: DataLayoutDescription(dataLayoutString),
UniversalLinkInfo(target, opts.HasMultipleIGMs, /*forcePublic*/ false,
/*static=*/false),
SwiftModule(swiftModule), Opts(opts), recorder(recorder),
previousInstallNameMap(parsePreviousModuleInstallNameMap()) {}
/// Create a new visitor using the target and layout information from a
/// TBDGenDescriptor.
TBDGenVisitor(const TBDGenDescriptor &desc, APIRecorder &recorder);
~TBDGenVisitor() { assert(DeclStack.empty()); }
/// Adds the global symbols associated with the first file.
void addFirstFileSymbols();
/// Visit the files specified by a given TBDGenDescriptor.
void visit(const TBDGenDescriptor &desc);
// --- IRSymbolVisitor ---
bool willVisitDecl(Decl *D) override;
void didVisitDecl(Decl *D) override;
void addFunction(SILDeclRef declRef) override;
void addFunction(StringRef name, SILDeclRef declRef) override;
void addGlobalVar(VarDecl *VD) override;
void addLinkEntity(LinkEntity entity) override;
void addObjCInterface(ClassDecl *CD) override;
void addObjCMethod(AbstractFunctionDecl *AFD) override;
void addProtocolWitnessThunk(RootProtocolConformance *C,
ValueDecl *requirementDecl) override;
};
} // end namespace tbdgen
} // end namespace swift
#endif
|