1//===- DWARFLinkerGlobalData.h ----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
10#define LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
11
12#include "TypePool.h"
13#include "llvm/DWARFLinker/Parallel/DWARFLinker.h"
14#include "llvm/DWARFLinker/StringPool.h"
15#include "llvm/Support/PerThreadBumpPtrAllocator.h"
16
17namespace llvm {
18
19class DWARFDie;
20
21namespace dwarf_linker {
22namespace parallel {
23
24using TranslatorFuncTy = std::function<StringRef(StringRef)>;
25using MessageHandlerTy = std::function<void(
26    const Twine &Warning, StringRef Context, const DWARFDie *DIE)>;
27
28/// linking options
29struct DWARFLinkerOptions {
30  /// DWARF version for the output.
31  uint16_t TargetDWARFVersion = 0;
32
33  /// Generate processing log to the standard output.
34  bool Verbose = false;
35
36  /// Print statistics.
37  bool Statistics = false;
38
39  /// Verify the input DWARF.
40  bool VerifyInputDWARF = false;
41
42  /// Do not unique types according to ODR
43  bool NoODR = false;
44
45  /// Update index tables.
46  bool UpdateIndexTablesOnly = false;
47
48  /// Whether we want a static variable to force us to keep its enclosing
49  /// function.
50  bool KeepFunctionForStatic = false;
51
52  /// Allow to generate valid, but non deterministic output.
53  bool AllowNonDeterministicOutput = false;
54
55  /// Number of threads.
56  unsigned Threads = 1;
57
58  /// The accelerator table kinds
59  SmallVector<DWARFLinkerBase::AccelTableKind, 1> AccelTables;
60
61  /// Prepend path for the clang modules.
62  std::string PrependPath;
63
64  /// input verification handler(it might be called asynchronously).
65  DWARFLinkerBase::InputVerificationHandlerTy InputVerificationHandler =
66      nullptr;
67
68  /// A list of all .swiftinterface files referenced by the debug
69  /// info, mapping Module name to path on disk. The entries need to
70  /// be uniqued and sorted and there are only few entries expected
71  /// per compile unit, which is why this is a std::map.
72  /// this is dsymutil specific fag.
73  ///
74  /// (it might be called asynchronously).
75  DWARFLinkerBase::SwiftInterfacesMapTy *ParseableSwiftInterfaces = nullptr;
76
77  /// A list of remappings to apply to file paths.
78  ///
79  /// (it might be called asynchronously).
80  DWARFLinkerBase::ObjectPrefixMapTy *ObjectPrefixMap = nullptr;
81};
82
83class DWARFLinkerImpl;
84
85/// This class keeps data and services common for the whole linking process.
86class LinkingGlobalData {
87  friend DWARFLinkerImpl;
88
89public:
90  /// Returns global per-thread allocator.
91  llvm::parallel::PerThreadBumpPtrAllocator &getAllocator() {
92    return Allocator;
93  }
94
95  /// Returns global string pool.
96  StringPool &getStringPool() { return Strings; }
97
98  /// Set translation function.
99  void setTranslator(TranslatorFuncTy Translator) {
100    this->Translator = Translator;
101  }
102
103  /// Translate specified string.
104  StringRef translateString(StringRef String) {
105    if (Translator)
106      return Translator(String);
107
108    return String;
109  }
110
111  /// Returns linking options.
112  const DWARFLinkerOptions &getOptions() const { return Options; }
113
114  /// Set warning handler.
115  void setWarningHandler(MessageHandlerTy Handler) { WarningHandler = Handler; }
116
117  /// Set error handler.
118  void setErrorHandler(MessageHandlerTy Handler) { ErrorHandler = Handler; }
119
120  /// Report warning.
121  void warn(const Twine &Warning, StringRef Context,
122            const DWARFDie *DIE = nullptr) {
123    if (WarningHandler)
124      (WarningHandler)(Warning, Context, DIE);
125  }
126
127  /// Report warning.
128  void warn(Error Warning, StringRef Context, const DWARFDie *DIE = nullptr) {
129    handleAllErrors(std::move(Warning), [&](ErrorInfoBase &Info) {
130      warn(Info.message(), Context, DIE);
131    });
132  }
133
134  /// Report error.
135  void error(const Twine &Err, StringRef Context,
136             const DWARFDie *DIE = nullptr) {
137    if (ErrorHandler)
138      (ErrorHandler)(Err, Context, DIE);
139  }
140
141  /// Report error.
142  void error(Error Err, StringRef Context, const DWARFDie *DIE = nullptr) {
143    handleAllErrors(std::move(Err), [&](ErrorInfoBase &Info) {
144      error(Info.message(), Context, DIE);
145    });
146  }
147
148  /// Set target triple.
149  void setTargetTriple(const Triple &TargetTriple) {
150    this->TargetTriple = TargetTriple;
151  }
152
153  /// Optionally return target triple.
154  std::optional<std::reference_wrapper<const Triple>> getTargetTriple() {
155    if (TargetTriple)
156      return std::cref(*TargetTriple);
157
158    return std::nullopt;
159  }
160
161protected:
162  llvm::parallel::PerThreadBumpPtrAllocator Allocator;
163  StringPool Strings;
164  TranslatorFuncTy Translator;
165  DWARFLinkerOptions Options;
166  MessageHandlerTy WarningHandler;
167  MessageHandlerTy ErrorHandler;
168
169  /// Triple for output data. May be not set if generation of output
170  /// data is not requested.
171  std::optional<Triple> TargetTriple;
172};
173
174} // end of namespace parallel
175} // end of namespace dwarf_linker
176} // end of namespace llvm
177
178#endif // LLVM_LIB_DWARFLINKER_PARALLEL_DWARFLINKERGLOBALDATA_H
179