18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringExtras.h"
31 "EntityLinker: Corrupted TUSummary or logic bug";
34 "{0} - {1} with {2} already exists in LUSummary";
37 "{0} - {1} missing linkage information in TUSummary";
40 "{0} - Duplicate {1} in EntityResolutionTable";
43 "{0} - {1} not found in EntityResolutionTable";
46 "{0} - Failed to insert data for {1} with {2} against {3} to LUSummary";
49 "failed to link TU summary: duplicate {0}";
52 "failed to link member {0} of static library {1}";
55 "target triple '{0}' of {1} does not match link unit target triple '{2}'";
58 "multi-arch static library {0} has no member for target triple '{1}' "
79 llvm_unreachable(
"Unhandled EntityLinkageType variant");
86 Output.LUNamespace, TUNamespace, OldName.Namespace,
Linkage.getLinkage());
88 EntityName NewName(OldName.USR, OldName.Suffix, NewNamespace);
94 EntityId NewId = Output.IdTable.getId(NewName);
96 auto [_, Inserted] = Output.LinkageTable.try_emplace(NewId,
Linkage);
112std::map<EntityId, EntityId>
114 std::map<EntityId, EntityId> EntityResolutionTable;
116 Summary.IdTable.
forEach([&](
const EntityName &OldName,
const EntityId OldId) {
117 auto Iter = Summary.LinkageTable.find(OldId);
118 if (Iter == Summary.LinkageTable.end()) {
123 const EntityLinkage &
Linkage = Iter->second;
125 EntityId NewId = resolveEntity(OldName,
Linkage,
126 NestedBuildNamespace(Summary.TUNamespace));
128 auto [_, Inserted] = EntityResolutionTable.insert({OldId, NewId});
135 return EntityResolutionTable;
138std::vector<EntitySummaryEncoding *>
140 const std::map<EntityId, EntityId> &EntityResolutionTable) {
141 std::vector<EntitySummaryEncoding *> PatchTargets;
143 for (
auto &[SN, DataMap] : Summary.Data) {
144 auto &OutputSummaryData = Output.Data[SN];
146 for (
auto &[OldId, ES] : DataMap) {
147 auto Iter = EntityResolutionTable.find(OldId);
148 if (Iter == EntityResolutionTable.end()) {
153 const auto NewId = Iter->second;
155 auto [It, Inserted] = OutputSummaryData.try_emplace(NewId, std::move(ES));
158 PatchTargets.push_back(It->second.get());
162 auto Linkage = Summary.LinkageTable.at(OldId);
185EntityLinker::patch(
const std::vector<EntitySummaryEncoding *> &PatchTargets,
186 const std::map<EntityId, EntityId> &EntityResolutionTable) {
187 for (
auto *PatchTarget : PatchTargets) {
188 assert(PatchTarget &&
"EntityLinker::patch: Patch target cannot be null");
190 if (
auto Err = PatchTarget->patch(EntityResolutionTable)) {
194 return llvm::Error::success();
198EntityLinker::checkTargetTriple(
const llvm::Triple &TargetTriple,
200 if (TargetTriple != Output.TargetTriple) {
203 TargetTriple, InputNamespace,
207 return llvm::Error::success();
212 checkTargetTriple(Summary->TargetTriple, Summary->TUNamespace)) {
216 auto [_, Inserted] = ProcessedTUNamespaces.insert(Summary->TUNamespace);
220 Summary->TUNamespace)
226 auto EntityResolutionTable = resolve(SummaryRef);
227 auto PatchTargets = merge(SummaryRef, EntityResolutionTable);
228 return patch(PatchTargets, EntityResolutionTable);
232 if (
auto Err = checkTargetTriple(
Library->TargetTriple,
Library->Namespace)) {
236 while (!
Library->Members.empty()) {
240 if (
auto Err =
link(std::move(Node.value()))) {
248 return llvm::Error::success();
253 auto MatchingMember = llvm::find_if(
254 Library->Members, [
this](
const std::unique_ptr<StaticLibrary> &
Member) {
255 return Member->TargetTriple == Output.TargetTriple;
258 if (MatchingMember ==
Library->Members.end()) {
259 auto TargetTriples = llvm::map_range(
260 Library->Members, [](
const std::unique_ptr<StaticLibrary> &
Member) {
261 return llvm::Triple::normalize(Member->TargetTriple.str());
263 std::string Available =
Library->Members.empty()
264 ? std::string(
"none")
265 : llvm::join(TargetTriples,
", ");
269 Library->Namespace, Output.TargetTriple,
274 return link(std::move(
Library->Members.extract(MatchingMember).value()));
static NestedBuildNamespace resolveNamespace(const NestedBuildNamespace &LUNamespace, const NestedBuildNamespace &TUNamespace, const NestedBuildNamespace &EntityNamespace, EntityLinkageType Linkage)
Represents a single namespace in the build process.
void forEach(llvm::function_ref< void(const EntityName &, EntityId)> Callback) const
Invokes the callback for each entity in the table.
Lightweight opaque handle representing an entity in an EntityIdTable.
Represents the linkage properties of an entity in the program model.
llvm::Error link(std::unique_ptr< TUSummaryEncoding > Summary)
Links a TU summary into a LU summary.
Uniquely identifies an entity in a program.
static ErrorBuilder create(std::error_code EC, const char *Fmt, Args &&...ArgVals)
Create an ErrorBuilder with an error code and formatted message.
static void fatal(const char *Fmt, Args &&...ArgVals)
Report a fatal error with formatted message and terminate execution.
ErrorBuilder & context(const char *Msg)
Add context information as a plain string.
llvm::Error build() const
Build and return the final error.
static ErrorBuilder wrap(llvm::Error E)
Wrap an existing error and optionally add context.
Represents a hierarchical sequence of build namespaces.
NestedBuildNamespace makeQualified(NestedBuildNamespace Namespace) const
Creates a new NestedBuildNamespace by appending additional namespace.
Represents a translation unit summary in its serialized encoding.
static constexpr const char * EntityAlreadyExistsInLinkageTable
static constexpr const char * DuplicateTUNamespace
static constexpr const char * EntityLinkerFatalErrorPrefix
static constexpr const char * DuplicateEntityIdInTUSummary
static constexpr const char * MissingLinkageInformation
static constexpr const char * NoMemberForTargetTriple
static constexpr const char * MismatchedTargetTriple
static constexpr const char * LinkingStaticLibraryMember
static constexpr const char * FailedToInsertEntityIntoOutputSummary
static constexpr const char * EntityNotFoundInResolutionTable
@ Internal
static functions/variables, anonymous namespace
@ External
globally visible across translation units (including parameters of functions with external linkage)
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.