Bug Summary

File:include/llvm/Support/Error.h
Warning:line 201, column 5
Potential leak of memory pointed to by 'Payload._M_t._M_head_impl'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lli.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-8/lib/clang/8.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lli -I /build/llvm-toolchain-snapshot-8~svn345461/tools/lli -I /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/include -I /build/llvm-toolchain-snapshot-8~svn345461/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/8.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-8~svn345461/build-llvm/tools/lli -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-10-27-211344-32123-1 -x c++ /build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp -faddrsig

/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp

1//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility provides a simple wrapper around the LLVM Execution Engines,
11// which allow the direct execution of LLVM programs through a Just-In-Time
12// compiler, or through an interpreter if no JIT is available for this platform.
13//
14//===----------------------------------------------------------------------===//
15
16#include "RemoteJITUtils.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/ADT/Triple.h"
19#include "llvm/Bitcode/BitcodeReader.h"
20#include "llvm/CodeGen/CommandFlags.inc"
21#include "llvm/CodeGen/LinkAllCodegenComponents.h"
22#include "llvm/Config/llvm-config.h"
23#include "llvm/ExecutionEngine/GenericValue.h"
24#include "llvm/ExecutionEngine/Interpreter.h"
25#include "llvm/ExecutionEngine/JITEventListener.h"
26#include "llvm/ExecutionEngine/MCJIT.h"
27#include "llvm/ExecutionEngine/ObjectCache.h"
28#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
29#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
30#include "llvm/ExecutionEngine/Orc/LLJIT.h"
31#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
32#include "llvm/ExecutionEngine/OrcMCJITReplacement.h"
33#include "llvm/ExecutionEngine/SectionMemoryManager.h"
34#include "llvm/IR/IRBuilder.h"
35#include "llvm/IR/LLVMContext.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/Type.h"
38#include "llvm/IR/TypeBuilder.h"
39#include "llvm/IR/Verifier.h"
40#include "llvm/IRReader/IRReader.h"
41#include "llvm/Object/Archive.h"
42#include "llvm/Object/ObjectFile.h"
43#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/DynamicLibrary.h"
46#include "llvm/Support/Format.h"
47#include "llvm/Support/InitLLVM.h"
48#include "llvm/Support/ManagedStatic.h"
49#include "llvm/Support/MathExtras.h"
50#include "llvm/Support/Memory.h"
51#include "llvm/Support/MemoryBuffer.h"
52#include "llvm/Support/Path.h"
53#include "llvm/Support/PluginLoader.h"
54#include "llvm/Support/Process.h"
55#include "llvm/Support/Program.h"
56#include "llvm/Support/SourceMgr.h"
57#include "llvm/Support/TargetSelect.h"
58#include "llvm/Support/WithColor.h"
59#include "llvm/Support/raw_ostream.h"
60#include "llvm/Transforms/Instrumentation.h"
61#include <cerrno>
62
63#ifdef __CYGWIN__
64#include <cygwin/version.h>
65#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
66#define DO_NOTHING_ATEXIT 1
67#endif
68#endif
69
70using namespace llvm;
71
72#define DEBUG_TYPE"lli" "lli"
73
74namespace {
75
76 enum class JITKind { MCJIT, OrcMCJITReplacement, OrcLazy };
77
78 cl::opt<std::string>
79 InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
80
81 cl::list<std::string>
82 InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
83
84 cl::opt<bool> ForceInterpreter("force-interpreter",
85 cl::desc("Force interpretation: disable JIT"),
86 cl::init(false));
87
88 cl::opt<JITKind> UseJITKind("jit-kind",
89 cl::desc("Choose underlying JIT kind."),
90 cl::init(JITKind::MCJIT),
91 cl::values(
92 clEnumValN(JITKind::MCJIT, "mcjit",llvm::cl::OptionEnumValue { "mcjit", int(JITKind::MCJIT), "MCJIT"
}
93 "MCJIT")llvm::cl::OptionEnumValue { "mcjit", int(JITKind::MCJIT), "MCJIT"
}
,
94 clEnumValN(JITKind::OrcMCJITReplacement,llvm::cl::OptionEnumValue { "orc-mcjit", int(JITKind::OrcMCJITReplacement
), "Orc-based MCJIT replacement" }
95 "orc-mcjit",llvm::cl::OptionEnumValue { "orc-mcjit", int(JITKind::OrcMCJITReplacement
), "Orc-based MCJIT replacement" }
96 "Orc-based MCJIT replacement")llvm::cl::OptionEnumValue { "orc-mcjit", int(JITKind::OrcMCJITReplacement
), "Orc-based MCJIT replacement" }
,
97 clEnumValN(JITKind::OrcLazy,llvm::cl::OptionEnumValue { "orc-lazy", int(JITKind::OrcLazy)
, "Orc-based lazy JIT." }
98 "orc-lazy",llvm::cl::OptionEnumValue { "orc-lazy", int(JITKind::OrcLazy)
, "Orc-based lazy JIT." }
99 "Orc-based lazy JIT.")llvm::cl::OptionEnumValue { "orc-lazy", int(JITKind::OrcLazy)
, "Orc-based lazy JIT." }
));
100
101 cl::opt<unsigned>
102 LazyJITCompileThreads("compile-threads",
103 cl::desc("Choose the number of compile threads "
104 "(jit-kind=orc-lazy only)"),
105 cl::init(0));
106
107 cl::list<std::string>
108 ThreadEntryPoints("thread-entry",
109 cl::desc("calls the given entry-point on a new thread "
110 "(jit-kind=orc-lazy only)"));
111
112 cl::opt<bool> PerModuleLazy(
113 "per-module-lazy",
114 cl::desc("Performs lazy compilation on whole module boundaries "
115 "rather than individual functions"),
116 cl::init(false));
117
118 cl::list<std::string>
119 JITDylibs("jd",
120 cl::desc("Specifies the JITDylib to be used for any subsequent "
121 "-extra-module arguments."));
122
123 // The MCJIT supports building for a target address space separate from
124 // the JIT compilation process. Use a forked process and a copying
125 // memory manager with IPC to execute using this functionality.
126 cl::opt<bool> RemoteMCJIT("remote-mcjit",
127 cl::desc("Execute MCJIT'ed code in a separate process."),
128 cl::init(false));
129
130 // Manually specify the child process for remote execution. This overrides
131 // the simulated remote execution that allocates address space for child
132 // execution. The child process will be executed and will communicate with
133 // lli via stdin/stdout pipes.
134 cl::opt<std::string>
135 ChildExecPath("mcjit-remote-process",
136 cl::desc("Specify the filename of the process to launch "
137 "for remote MCJIT execution. If none is specified,"
138 "\n\tremote execution will be simulated in-process."),
139 cl::value_desc("filename"), cl::init(""));
140
141 // Determine optimization level.
142 cl::opt<char>
143 OptLevel("O",
144 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
145 "(default = '-O2')"),
146 cl::Prefix,
147 cl::ZeroOrMore,
148 cl::init(' '));
149
150 cl::opt<std::string>
151 TargetTriple("mtriple", cl::desc("Override target triple for module"));
152
153 cl::opt<std::string>
154 EntryFunc("entry-function",
155 cl::desc("Specify the entry function (default = 'main') "
156 "of the executable"),
157 cl::value_desc("function"),
158 cl::init("main"));
159
160 cl::list<std::string>
161 ExtraModules("extra-module",
162 cl::desc("Extra modules to be loaded"),
163 cl::value_desc("input bitcode"));
164
165 cl::list<std::string>
166 ExtraObjects("extra-object",
167 cl::desc("Extra object files to be loaded"),
168 cl::value_desc("input object"));
169
170 cl::list<std::string>
171 ExtraArchives("extra-archive",
172 cl::desc("Extra archive files to be loaded"),
173 cl::value_desc("input archive"));
174
175 cl::opt<bool>
176 EnableCacheManager("enable-cache-manager",
177 cl::desc("Use cache manager to save/load mdoules"),
178 cl::init(false));
179
180 cl::opt<std::string>
181 ObjectCacheDir("object-cache-dir",
182 cl::desc("Directory to store cached object files "
183 "(must be user writable)"),
184 cl::init(""));
185
186 cl::opt<std::string>
187 FakeArgv0("fake-argv0",
188 cl::desc("Override the 'argv[0]' value passed into the executing"
189 " program"), cl::value_desc("executable"));
190
191 cl::opt<bool>
192 DisableCoreFiles("disable-core-files", cl::Hidden,
193 cl::desc("Disable emission of core files if possible"));
194
195 cl::opt<bool>
196 NoLazyCompilation("disable-lazy-compilation",
197 cl::desc("Disable JIT lazy compilation"),
198 cl::init(false));
199
200 cl::opt<bool>
201 GenerateSoftFloatCalls("soft-float",
202 cl::desc("Generate software floating point library calls"),
203 cl::init(false));
204
205 enum class DumpKind {
206 NoDump,
207 DumpFuncsToStdOut,
208 DumpModsToStdOut,
209 DumpModsToDisk
210 };
211
212 cl::opt<DumpKind> OrcDumpKind(
213 "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
214 cl::init(DumpKind::NoDump),
215 cl::values(clEnumValN(DumpKind::NoDump, "no-dump",llvm::cl::OptionEnumValue { "no-dump", int(DumpKind::NoDump),
"Don't dump anything." }
216 "Don't dump anything.")llvm::cl::OptionEnumValue { "no-dump", int(DumpKind::NoDump),
"Don't dump anything." }
,
217 clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",llvm::cl::OptionEnumValue { "funcs-to-stdout", int(DumpKind::
DumpFuncsToStdOut), "Dump function names to stdout." }
218 "Dump function names to stdout.")llvm::cl::OptionEnumValue { "funcs-to-stdout", int(DumpKind::
DumpFuncsToStdOut), "Dump function names to stdout." }
,
219 clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",llvm::cl::OptionEnumValue { "mods-to-stdout", int(DumpKind::DumpModsToStdOut
), "Dump modules to stdout." }
220 "Dump modules to stdout.")llvm::cl::OptionEnumValue { "mods-to-stdout", int(DumpKind::DumpModsToStdOut
), "Dump modules to stdout." }
,
221 clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",llvm::cl::OptionEnumValue { "mods-to-disk", int(DumpKind::DumpModsToDisk
), "Dump modules to the current " "working directory. (WARNING: "
"will overwrite existing files)." }
222 "Dump modules to the current "llvm::cl::OptionEnumValue { "mods-to-disk", int(DumpKind::DumpModsToDisk
), "Dump modules to the current " "working directory. (WARNING: "
"will overwrite existing files)." }
223 "working directory. (WARNING: "llvm::cl::OptionEnumValue { "mods-to-disk", int(DumpKind::DumpModsToDisk
), "Dump modules to the current " "working directory. (WARNING: "
"will overwrite existing files)." }
224 "will overwrite existing files).")llvm::cl::OptionEnumValue { "mods-to-disk", int(DumpKind::DumpModsToDisk
), "Dump modules to the current " "working directory. (WARNING: "
"will overwrite existing files)." }
),
225 cl::Hidden);
226
227 ExitOnError ExitOnErr;
228}
229
230//===----------------------------------------------------------------------===//
231// Object cache
232//
233// This object cache implementation writes cached objects to disk to the
234// directory specified by CacheDir, using a filename provided in the module
235// descriptor. The cache tries to load a saved object using that path if the
236// file exists. CacheDir defaults to "", in which case objects are cached
237// alongside their originating bitcodes.
238//
239class LLIObjectCache : public ObjectCache {
240public:
241 LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
242 // Add trailing '/' to cache dir if necessary.
243 if (!this->CacheDir.empty() &&
244 this->CacheDir[this->CacheDir.size() - 1] != '/')
245 this->CacheDir += '/';
246 }
247 ~LLIObjectCache() override {}
248
249 void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
250 const std::string &ModuleID = M->getModuleIdentifier();
251 std::string CacheName;
252 if (!getCacheFilename(ModuleID, CacheName))
253 return;
254 if (!CacheDir.empty()) { // Create user-defined cache dir.
255 SmallString<128> dir(sys::path::parent_path(CacheName));
256 sys::fs::create_directories(Twine(dir));
257 }
258 std::error_code EC;
259 raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
260 outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
261 outfile.close();
262 }
263
264 std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
265 const std::string &ModuleID = M->getModuleIdentifier();
266 std::string CacheName;
267 if (!getCacheFilename(ModuleID, CacheName))
268 return nullptr;
269 // Load the object from the cache filename
270 ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
271 MemoryBuffer::getFile(CacheName, -1, false);
272 // If the file isn't there, that's OK.
273 if (!IRObjectBuffer)
274 return nullptr;
275 // MCJIT will want to write into this buffer, and we don't want that
276 // because the file has probably just been mmapped. Instead we make
277 // a copy. The filed-based buffer will be released when it goes
278 // out of scope.
279 return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
280 }
281
282private:
283 std::string CacheDir;
284
285 bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
286 std::string Prefix("file:");
287 size_t PrefixLength = Prefix.length();
288 if (ModID.substr(0, PrefixLength) != Prefix)
289 return false;
290 std::string CacheSubdir = ModID.substr(PrefixLength);
291#if defined(_WIN32)
292 // Transform "X:\foo" => "/X\foo" for convenience.
293 if (isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
294 CacheSubdir[1] = CacheSubdir[0];
295 CacheSubdir[0] = '/';
296 }
297#endif
298 CacheName = CacheDir + CacheSubdir;
299 size_t pos = CacheName.rfind('.');
300 CacheName.replace(pos, CacheName.length() - pos, ".o");
301 return true;
302 }
303};
304
305// On Mingw and Cygwin, an external symbol named '__main' is called from the
306// generated 'main' function to allow static initialization. To avoid linking
307// problems with remote targets (because lli's remote target support does not
308// currently handle external linking) we add a secondary module which defines
309// an empty '__main' function.
310static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
311 StringRef TargetTripleStr) {
312 IRBuilder<> Builder(Context);
313 Triple TargetTriple(TargetTripleStr);
314
315 // Create a new module.
316 std::unique_ptr<Module> M = make_unique<Module>("CygMingHelper", Context);
317 M->setTargetTriple(TargetTripleStr);
318
319 // Create an empty function named "__main".
320 Function *Result;
321 if (TargetTriple.isArch64Bit()) {
322 Result = Function::Create(
323 TypeBuilder<int64_t(void), false>::get(Context),
324 GlobalValue::ExternalLinkage, "__main", M.get());
325 } else {
326 Result = Function::Create(
327 TypeBuilder<int32_t(void), false>::get(Context),
328 GlobalValue::ExternalLinkage, "__main", M.get());
329 }
330 BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
331 Builder.SetInsertPoint(BB);
332 Value *ReturnVal;
333 if (TargetTriple.isArch64Bit())
334 ReturnVal = ConstantInt::get(Context, APInt(64, 0));
335 else
336 ReturnVal = ConstantInt::get(Context, APInt(32, 0));
337 Builder.CreateRet(ReturnVal);
338
339 // Add this new module to the ExecutionEngine.
340 EE.addModule(std::move(M));
341}
342
343CodeGenOpt::Level getOptLevel() {
344 switch (OptLevel) {
345 default:
346 WithColor::error(errs(), "lli") << "invalid optimization level.\n";
347 exit(1);
348 case '0': return CodeGenOpt::None;
349 case '1': return CodeGenOpt::Less;
350 case ' ':
351 case '2': return CodeGenOpt::Default;
352 case '3': return CodeGenOpt::Aggressive;
353 }
354 llvm_unreachable("Unrecognized opt level.")::llvm::llvm_unreachable_internal("Unrecognized opt level.", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp"
, 354)
;
355}
356
357LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
358static void reportError(SMDiagnostic Err, const char *ProgName) {
359 Err.print(ProgName, errs());
360 exit(1);
361}
362
363int runOrcLazyJIT(const char *ProgName);
364void disallowOrcOptions();
365
366//===----------------------------------------------------------------------===//
367// main Driver function
368//
369int main(int argc, char **argv, char * const *envp) {
370 InitLLVM X(argc, argv);
371
372 if (argc > 1)
373 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
374
375 // If we have a native target, initialize it to ensure it is linked in and
376 // usable by the JIT.
377 InitializeNativeTarget();
378 InitializeNativeTargetAsmPrinter();
379 InitializeNativeTargetAsmParser();
380
381 cl::ParseCommandLineOptions(argc, argv,
382 "llvm interpreter & dynamic compiler\n");
383
384 // If the user doesn't want core files, disable them.
385 if (DisableCoreFiles)
386 sys::Process::PreventCoreFiles();
387
388 if (UseJITKind == JITKind::OrcLazy)
389 return runOrcLazyJIT(argv[0]);
390 else
391 disallowOrcOptions();
392
393 LLVMContext Context;
394
395 // Load the bitcode...
396 SMDiagnostic Err;
397 std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
398 Module *Mod = Owner.get();
399 if (!Mod)
400 reportError(Err, argv[0]);
401
402 if (EnableCacheManager) {
403 std::string CacheName("file:");
404 CacheName.append(InputFile);
405 Mod->setModuleIdentifier(CacheName);
406 }
407
408 // If not jitting lazily, load the whole bitcode file eagerly too.
409 if (NoLazyCompilation) {
410 // Use *argv instead of argv[0] to work around a wrong GCC warning.
411 ExitOnError ExitOnErr(std::string(*argv) +
412 ": bitcode didn't read correctly: ");
413 ExitOnErr(Mod->materializeAll());
414 }
415
416 std::string ErrorMsg;
417 EngineBuilder builder(std::move(Owner));
418 builder.setMArch(MArch);
419 builder.setMCPU(getCPUStr());
420 builder.setMAttrs(getFeatureList());
421 if (RelocModel.getNumOccurrences())
422 builder.setRelocationModel(RelocModel);
423 if (CMModel.getNumOccurrences())
424 builder.setCodeModel(CMModel);
425 builder.setErrorStr(&ErrorMsg);
426 builder.setEngineKind(ForceInterpreter
427 ? EngineKind::Interpreter
428 : EngineKind::JIT);
429 builder.setUseOrcMCJITReplacement(UseJITKind == JITKind::OrcMCJITReplacement);
430
431 // If we are supposed to override the target triple, do so now.
432 if (!TargetTriple.empty())
433 Mod->setTargetTriple(Triple::normalize(TargetTriple));
434
435 // Enable MCJIT if desired.
436 RTDyldMemoryManager *RTDyldMM = nullptr;
437 if (!ForceInterpreter) {
438 if (RemoteMCJIT)
439 RTDyldMM = new ForwardingMemoryManager();
440 else
441 RTDyldMM = new SectionMemoryManager();
442
443 // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
444 // RTDyldMM: We still use it below, even though we don't own it.
445 builder.setMCJITMemoryManager(
446 std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
447 } else if (RemoteMCJIT) {
448 WithColor::error(errs(), argv[0])
449 << "remote process execution does not work with the interpreter.\n";
450 exit(1);
451 }
452
453 builder.setOptLevel(getOptLevel());
454
455 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
456 if (FloatABIForCalls != FloatABI::Default)
457 Options.FloatABIType = FloatABIForCalls;
458
459 builder.setTargetOptions(Options);
460
461 std::unique_ptr<ExecutionEngine> EE(builder.create());
462 if (!EE) {
463 if (!ErrorMsg.empty())
464 WithColor::error(errs(), argv[0])
465 << "error creating EE: " << ErrorMsg << "\n";
466 else
467 WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
468 exit(1);
469 }
470
471 std::unique_ptr<LLIObjectCache> CacheManager;
472 if (EnableCacheManager) {
473 CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
474 EE->setObjectCache(CacheManager.get());
475 }
476
477 // Load any additional modules specified on the command line.
478 for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
479 std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
480 if (!XMod)
481 reportError(Err, argv[0]);
482 if (EnableCacheManager) {
483 std::string CacheName("file:");
484 CacheName.append(ExtraModules[i]);
485 XMod->setModuleIdentifier(CacheName);
486 }
487 EE->addModule(std::move(XMod));
488 }
489
490 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
491 Expected<object::OwningBinary<object::ObjectFile>> Obj =
492 object::ObjectFile::createObjectFile(ExtraObjects[i]);
493 if (!Obj) {
494 // TODO: Actually report errors helpfully.
495 consumeError(Obj.takeError());
496 reportError(Err, argv[0]);
497 }
498 object::OwningBinary<object::ObjectFile> &O = Obj.get();
499 EE->addObjectFile(std::move(O));
500 }
501
502 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
503 ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
504 MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
505 if (!ArBufOrErr)
506 reportError(Err, argv[0]);
507 std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
508
509 Expected<std::unique_ptr<object::Archive>> ArOrErr =
510 object::Archive::create(ArBuf->getMemBufferRef());
511 if (!ArOrErr) {
512 std::string Buf;
513 raw_string_ostream OS(Buf);
514 logAllUnhandledErrors(ArOrErr.takeError(), OS, "");
515 OS.flush();
516 errs() << Buf;
517 exit(1);
518 }
519 std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
520
521 object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
522
523 EE->addArchive(std::move(OB));
524 }
525
526 // If the target is Cygwin/MingW and we are generating remote code, we
527 // need an extra module to help out with linking.
528 if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
529 addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
530 }
531
532 // The following functions have no effect if their respective profiling
533 // support wasn't enabled in the build configuration.
534 EE->RegisterJITEventListener(
535 JITEventListener::createOProfileJITEventListener());
536 EE->RegisterJITEventListener(
537 JITEventListener::createIntelJITEventListener());
538 if (!RemoteMCJIT)
539 EE->RegisterJITEventListener(
540 JITEventListener::createPerfJITEventListener());
541
542 if (!NoLazyCompilation && RemoteMCJIT) {
543 WithColor::warning(errs(), argv[0])
544 << "remote mcjit does not support lazy compilation\n";
545 NoLazyCompilation = true;
546 }
547 EE->DisableLazyCompilation(NoLazyCompilation);
548
549 // If the user specifically requested an argv[0] to pass into the program,
550 // do it now.
551 if (!FakeArgv0.empty()) {
552 InputFile = static_cast<std::string>(FakeArgv0);
553 } else {
554 // Otherwise, if there is a .bc suffix on the executable strip it off, it
555 // might confuse the program.
556 if (StringRef(InputFile).endswith(".bc"))
557 InputFile.erase(InputFile.length() - 3);
558 }
559
560 // Add the module's name to the start of the vector of arguments to main().
561 InputArgv.insert(InputArgv.begin(), InputFile);
562
563 // Call the main function from M as if its signature were:
564 // int main (int argc, char **argv, const char **envp)
565 // using the contents of Args to determine argc & argv, and the contents of
566 // EnvVars to determine envp.
567 //
568 Function *EntryFn = Mod->getFunction(EntryFunc);
569 if (!EntryFn) {
570 WithColor::error(errs(), argv[0])
571 << '\'' << EntryFunc << "\' function not found in module.\n";
572 return -1;
573 }
574
575 // Reset errno to zero on entry to main.
576 errno(*__errno_location ()) = 0;
577
578 int Result = -1;
579
580 // Sanity check use of remote-jit: LLI currently only supports use of the
581 // remote JIT on Unix platforms.
582 if (RemoteMCJIT) {
583#ifndef LLVM_ON_UNIX1
584 WithColor::warning(errs(), argv[0])
585 << "host does not support external remote targets.\n";
586 WithColor::note() << "defaulting to local execution\n";
587 return -1;
588#else
589 if (ChildExecPath.empty()) {
590 WithColor::error(errs(), argv[0])
591 << "-remote-mcjit requires -mcjit-remote-process.\n";
592 exit(1);
593 } else if (!sys::fs::can_execute(ChildExecPath)) {
594 WithColor::error(errs(), argv[0])
595 << "unable to find usable child executable: '" << ChildExecPath
596 << "'\n";
597 return -1;
598 }
599#endif
600 }
601
602 if (!RemoteMCJIT) {
603 // If the program doesn't explicitly call exit, we will need the Exit
604 // function later on to make an explicit call, so get the function now.
605 Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
606 Type::getInt32Ty(Context));
607
608 // Run static constructors.
609 if (!ForceInterpreter) {
610 // Give MCJIT a chance to apply relocations and set page permissions.
611 EE->finalizeObject();
612 }
613 EE->runStaticConstructorsDestructors(false);
614
615 // Trigger compilation separately so code regions that need to be
616 // invalidated will be known.
617 (void)EE->getPointerToFunction(EntryFn);
618 // Clear instruction cache before code will be executed.
619 if (RTDyldMM)
620 static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
621
622 // Run main.
623 Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
624
625 // Run static destructors.
626 EE->runStaticConstructorsDestructors(true);
627
628 // If the program didn't call exit explicitly, we should call it now.
629 // This ensures that any atexit handlers get called correctly.
630 if (Function *ExitF = dyn_cast<Function>(Exit)) {
631 std::vector<GenericValue> Args;
632 GenericValue ResultGV;
633 ResultGV.IntVal = APInt(32, Result);
634 Args.push_back(ResultGV);
635 EE->runFunction(ExitF, Args);
636 WithColor::error(errs(), argv[0]) << "exit(" << Result << ") returned!\n";
637 abort();
638 } else {
639 WithColor::error(errs(), argv[0])
640 << "exit defined with wrong prototype!\n";
641 abort();
642 }
643 } else {
644 // else == "if (RemoteMCJIT)"
645
646 // Remote target MCJIT doesn't (yet) support static constructors. No reason
647 // it couldn't. This is a limitation of the LLI implementation, not the
648 // MCJIT itself. FIXME.
649
650 // Lanch the remote process and get a channel to it.
651 std::unique_ptr<FDRawChannel> C = launchRemote();
652 if (!C) {
653 WithColor::error(errs(), argv[0]) << "failed to launch remote JIT.\n";
654 exit(1);
655 }
656
657 // Create a remote target client running over the channel.
658 llvm::orc::ExecutionSession ES;
659 ES.setErrorReporter([&](Error Err) { ExitOnErr(std::move(Err)); });
660 typedef orc::remote::OrcRemoteTargetClient MyRemote;
661 auto R = ExitOnErr(MyRemote::Create(*C, ES));
662
663 // Create a remote memory manager.
664 auto RemoteMM = ExitOnErr(R->createRemoteMemoryManager());
665
666 // Forward MCJIT's memory manager calls to the remote memory manager.
667 static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
668 std::move(RemoteMM));
669
670 // Forward MCJIT's symbol resolution calls to the remote.
671 static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
672 orc::createLambdaResolver(
673 [](const std::string &Name) { return nullptr; },
674 [&](const std::string &Name) {
675 if (auto Addr = ExitOnErr(R->getSymbolAddress(Name)))
1
Calling 'OrcRemoteTargetClient::getSymbolAddress'
676 return JITSymbol(Addr, JITSymbolFlags::Exported);
677 return JITSymbol(nullptr);
678 }));
679
680 // Grab the target address of the JIT'd main function on the remote and call
681 // it.
682 // FIXME: argv and envp handling.
683 JITTargetAddress Entry = EE->getFunctionAddress(EntryFn->getName().str());
684 EE->finalizeObject();
685 LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Executing '" << EntryFn->
getName() << "' at 0x" << format("%llx", Entry) <<
"\n"; } } while (false)
686 << format("%llx", Entry) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Executing '" << EntryFn->
getName() << "' at 0x" << format("%llx", Entry) <<
"\n"; } } while (false)
;
687 Result = ExitOnErr(R->callIntVoid(Entry));
688
689 // Like static constructors, the remote target MCJIT support doesn't handle
690 // this yet. It could. FIXME.
691
692 // Delete the EE - we need to tear it down *before* we terminate the session
693 // with the remote, otherwise it'll crash when it tries to release resources
694 // on a remote that has already been disconnected.
695 EE.reset();
696
697 // Signal the remote target that we're done JITing.
698 ExitOnErr(R->terminateSession());
699 }
700
701 return Result;
702}
703
704static orc::IRTransformLayer::TransformFunction createDebugDumper() {
705 switch (OrcDumpKind) {
706 case DumpKind::NoDump:
707 return [](orc::ThreadSafeModule TSM,
708 const orc::MaterializationResponsibility &R) { return TSM; };
709
710 case DumpKind::DumpFuncsToStdOut:
711 return [](orc::ThreadSafeModule TSM,
712 const orc::MaterializationResponsibility &R) {
713 printf("[ ");
714
715 for (const auto &F : *TSM.getModule()) {
716 if (F.isDeclaration())
717 continue;
718
719 if (F.hasName()) {
720 std::string Name(F.getName());
721 printf("%s ", Name.c_str());
722 } else
723 printf("<anon> ");
724 }
725
726 printf("]\n");
727 return TSM;
728 };
729
730 case DumpKind::DumpModsToStdOut:
731 return [](orc::ThreadSafeModule TSM,
732 const orc::MaterializationResponsibility &R) {
733 outs() << "----- Module Start -----\n"
734 << *TSM.getModule() << "----- Module End -----\n";
735
736 return TSM;
737 };
738
739 case DumpKind::DumpModsToDisk:
740 return [](orc::ThreadSafeModule TSM,
741 const orc::MaterializationResponsibility &R) {
742 std::error_code EC;
743 raw_fd_ostream Out(TSM.getModule()->getModuleIdentifier() + ".ll", EC,
744 sys::fs::F_Text);
745 if (EC) {
746 errs() << "Couldn't open " << TSM.getModule()->getModuleIdentifier()
747 << " for dumping.\nError:" << EC.message() << "\n";
748 exit(1);
749 }
750 Out << *TSM.getModule();
751 return TSM;
752 };
753 }
754 llvm_unreachable("Unknown DumpKind")::llvm::llvm_unreachable_internal("Unknown DumpKind", "/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp"
, 754)
;
755}
756
757static void exitOnLazyCallThroughFailure() { exit(1); }
758
759int runOrcLazyJIT(const char *ProgName) {
760 // Start setting up the JIT environment.
761
762 // Parse the main module.
763 orc::ThreadSafeContext TSCtx(llvm::make_unique<LLVMContext>());
764 SMDiagnostic Err;
765 auto MainModule = orc::ThreadSafeModule(
766 parseIRFile(InputFile, Err, *TSCtx.getContext()), TSCtx);
767 if (!MainModule)
768 reportError(Err, ProgName);
769
770 const auto &TT = MainModule.getModule()->getTargetTriple();
771 orc::JITTargetMachineBuilder JTMB =
772 TT.empty() ? ExitOnErr(orc::JITTargetMachineBuilder::detectHost())
773 : orc::JITTargetMachineBuilder(Triple(TT));
774
775 if (!MArch.empty())
776 JTMB.getTargetTriple().setArchName(MArch);
777
778 JTMB.setCPU(getCPUStr())
779 .addFeatures(getFeatureList())
780 .setRelocationModel(RelocModel.getNumOccurrences()
781 ? Optional<Reloc::Model>(RelocModel)
782 : None)
783 .setCodeModel(CMModel.getNumOccurrences()
784 ? Optional<CodeModel::Model>(CMModel)
785 : None);
786
787 DataLayout DL = ExitOnErr(JTMB.getDefaultDataLayoutForTarget());
788
789 auto J = ExitOnErr(orc::LLLazyJIT::Create(
790 std::move(JTMB), DL,
791 pointerToJITTargetAddress(exitOnLazyCallThroughFailure),
792 LazyJITCompileThreads));
793
794 if (PerModuleLazy)
795 J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
796
797 auto Dump = createDebugDumper();
798
799 J->setLazyCompileTransform([&](orc::ThreadSafeModule TSM,
800 const orc::MaterializationResponsibility &R) {
801 if (verifyModule(*TSM.getModule(), &dbgs())) {
802 dbgs() << "Bad module: " << *TSM.getModule() << "\n";
803 exit(1);
804 }
805 return Dump(std::move(TSM), R);
806 });
807 J->getMainJITDylib().setGenerator(
808 ExitOnErr(orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(DL)));
809
810 orc::MangleAndInterner Mangle(J->getExecutionSession(), DL);
811 orc::LocalCXXRuntimeOverrides CXXRuntimeOverrides;
812 ExitOnErr(CXXRuntimeOverrides.enable(J->getMainJITDylib(), Mangle));
813
814 // Add the main module.
815 ExitOnErr(J->addLazyIRModule(std::move(MainModule)));
816
817 // Create JITDylibs and add any extra modules.
818 {
819 // Create JITDylibs, keep a map from argument index to dylib. We will use
820 // -extra-module argument indexes to determine what dylib to use for each
821 // -extra-module.
822 std::map<unsigned, orc::JITDylib *> IdxToDylib;
823 IdxToDylib[0] = &J->getMainJITDylib();
824 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
825 JDItr != JDEnd; ++JDItr) {
826 IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] =
827 &J->createJITDylib(*JDItr);
828 }
829
830 for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
831 EMItr != EMEnd; ++EMItr) {
832 auto M = parseIRFile(*EMItr, Err, *TSCtx.getContext());
833 if (!M)
834 reportError(Err, ProgName);
835
836 auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
837 assert(EMIdx != 0 && "ExtraModule should have index > 0")((EMIdx != 0 && "ExtraModule should have index > 0"
) ? static_cast<void> (0) : __assert_fail ("EMIdx != 0 && \"ExtraModule should have index > 0\""
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp"
, 837, __PRETTY_FUNCTION__))
;
838 auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
839 auto &JD = *JDItr->second;
840 ExitOnErr(
841 J->addLazyIRModule(JD, orc::ThreadSafeModule(std::move(M), TSCtx)));
842 }
843 }
844
845 // Add the objects.
846 for (auto &ObjPath : ExtraObjects) {
847 auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
848 ExitOnErr(J->addObjectFile(std::move(Obj)));
849 }
850
851 // Generate a argument string.
852 std::vector<std::string> Args;
853 Args.push_back(InputFile);
854 for (auto &Arg : InputArgv)
855 Args.push_back(Arg);
856
857 // Run any static constructors.
858 ExitOnErr(J->runConstructors());
859
860 // Run any -thread-entry points.
861 std::vector<std::thread> AltEntryThreads;
862 for (auto &ThreadEntryPoint : ThreadEntryPoints) {
863 auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
864 typedef void (*EntryPointPtr)();
865 auto EntryPoint =
866 reinterpret_cast<EntryPointPtr>(static_cast<uintptr_t>(EntryPointSym.getAddress()));
867 AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
868 }
869
870 J->getExecutionSession().dump(llvm::dbgs());
871
872 // Run main.
873 auto MainSym = ExitOnErr(J->lookup("main"));
874 typedef int (*MainFnPtr)(int, const char *[]);
875 std::vector<const char *> ArgV;
876 for (auto &Arg : Args)
877 ArgV.push_back(Arg.c_str());
878 ArgV.push_back(nullptr);
879
880 int ArgC = ArgV.size() - 1;
881 auto Main =
882 reinterpret_cast<MainFnPtr>(static_cast<uintptr_t>(MainSym.getAddress()));
883 auto Result = Main(ArgC, (const char **)ArgV.data());
884
885 // Wait for -entry-point threads.
886 for (auto &AltEntryThread : AltEntryThreads)
887 AltEntryThread.join();
888
889 // Run destructors.
890 ExitOnErr(J->runDestructors());
891 CXXRuntimeOverrides.runDestructors();
892
893 return Result;
894}
895
896void disallowOrcOptions() {
897 // Make sure nobody used an orc-lazy specific option accidentally.
898
899 if (LazyJITCompileThreads != 0) {
900 errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
901 exit(1);
902 }
903
904 if (!ThreadEntryPoints.empty()) {
905 errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
906 exit(1);
907 }
908
909 if (PerModuleLazy) {
910 errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
911 exit(1);
912 }
913}
914
915std::unique_ptr<FDRawChannel> launchRemote() {
916#ifndef LLVM_ON_UNIX1
917 llvm_unreachable("launchRemote not supported on non-Unix platforms")::llvm::llvm_unreachable_internal("launchRemote not supported on non-Unix platforms"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp"
, 917)
;
918#else
919 int PipeFD[2][2];
920 pid_t ChildPID;
921
922 // Create two pipes.
923 if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
924 perror("Error creating pipe: ");
925
926 ChildPID = fork();
927
928 if (ChildPID == 0) {
929 // In the child...
930
931 // Close the parent ends of the pipes
932 close(PipeFD[0][1]);
933 close(PipeFD[1][0]);
934
935
936 // Execute the child process.
937 std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
938 {
939 ChildPath.reset(new char[ChildExecPath.size() + 1]);
940 std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
941 ChildPath[ChildExecPath.size()] = '\0';
942 std::string ChildInStr = utostr(PipeFD[0][0]);
943 ChildIn.reset(new char[ChildInStr.size() + 1]);
944 std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
945 ChildIn[ChildInStr.size()] = '\0';
946 std::string ChildOutStr = utostr(PipeFD[1][1]);
947 ChildOut.reset(new char[ChildOutStr.size() + 1]);
948 std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
949 ChildOut[ChildOutStr.size()] = '\0';
950 }
951
952 char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
953 int rc = execv(ChildExecPath.c_str(), args);
954 if (rc != 0)
955 perror("Error executing child process: ");
956 llvm_unreachable("Error executing child process")::llvm::llvm_unreachable_internal("Error executing child process"
, "/build/llvm-toolchain-snapshot-8~svn345461/tools/lli/lli.cpp"
, 956)
;
957 }
958 // else we're the parent...
959
960 // Close the child ends of the pipes
961 close(PipeFD[0][0]);
962 close(PipeFD[1][1]);
963
964 // Return an RPC channel connected to our end of the pipes.
965 return llvm::make_unique<FDRawChannel>(PipeFD[1][0], PipeFD[0][1]);
966#endif
967}

/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h

1//===- OrcRemoteTargetClient.h - Orc Remote-target Client -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the OrcRemoteTargetClient class and helpers. This class
11// can be used to communicate over an RawByteChannel with an
12// OrcRemoteTargetServer instance to support remote-JITing.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
17#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
18
19#include "llvm/ADT/Optional.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ExecutionEngine/JITSymbol.h"
24#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
25#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h"
26#include "llvm/ExecutionEngine/RuntimeDyld.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/Error.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/Format.h"
31#include "llvm/Support/MathExtras.h"
32#include "llvm/Support/Memory.h"
33#include "llvm/Support/raw_ostream.h"
34#include <algorithm>
35#include <cassert>
36#include <cstdint>
37#include <memory>
38#include <string>
39#include <tuple>
40#include <utility>
41#include <vector>
42
43#define DEBUG_TYPE"lli" "orc-remote"
44
45namespace llvm {
46namespace orc {
47namespace remote {
48
49/// This class provides utilities (including memory manager, indirect stubs
50/// manager, and compile callback manager types) that support remote JITing
51/// in ORC.
52///
53/// Each of the utility classes talks to a JIT server (an instance of the
54/// OrcRemoteTargetServer class) via an RPC system (see RPCUtils.h) to carry out
55/// its actions.
56class OrcRemoteTargetClient
57 : public rpc::SingleThreadedRPCEndpoint<rpc::RawByteChannel> {
58public:
59 /// Remote-mapped RuntimeDyld-compatible memory manager.
60 class RemoteRTDyldMemoryManager : public RuntimeDyld::MemoryManager {
61 friend class OrcRemoteTargetClient;
62
63 public:
64 ~RemoteRTDyldMemoryManager() {
65 Client.destroyRemoteAllocator(Id);
66 LLVM_DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Destroyed remote allocator " <<
Id << "\n"; } } while (false)
;
67 }
68
69 RemoteRTDyldMemoryManager(const RemoteRTDyldMemoryManager &) = delete;
70 RemoteRTDyldMemoryManager &
71 operator=(const RemoteRTDyldMemoryManager &) = delete;
72 RemoteRTDyldMemoryManager(RemoteRTDyldMemoryManager &&) = default;
73 RemoteRTDyldMemoryManager &operator=(RemoteRTDyldMemoryManager &&) = delete;
74
75 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
76 unsigned SectionID,
77 StringRef SectionName) override {
78 Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);
79 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
80 Unmapped.back().CodeAllocs.back().getLocalAddress());
81 LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated code for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated code for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
82 << SectionName << ": " << Alloc << " (" << Sizedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated code for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
83 << " bytes, alignment " << Alignment << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated code for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
;
84 return Alloc;
85 }
86
87 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
88 unsigned SectionID, StringRef SectionName,
89 bool IsReadOnly) override {
90 if (IsReadOnly) {
91 Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);
92 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
93 Unmapped.back().RODataAllocs.back().getLocalAddress());
94 LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated ro-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
95 << SectionName << ": " << Alloc << " (" << Sizedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated ro-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
96 << " bytes, alignment " << Alignment << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated ro-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
;
97 return Alloc;
98 } // else...
99
100 Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);
101 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
102 Unmapped.back().RWDataAllocs.back().getLocalAddress());
103 LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated rw-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
104 << SectionName << ": " << Alloc << " (" << Sizedo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated rw-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
105 << " bytes, alignment " << Alignment << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " allocated rw-data for "
<< SectionName << ": " << Alloc << " ("
<< Size << " bytes, alignment " << Alignment
<< ")\n"; } } while (false)
;
106 return Alloc;
107 }
108
109 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
110 uintptr_t RODataSize, uint32_t RODataAlign,
111 uintptr_t RWDataSize,
112 uint32_t RWDataAlign) override {
113 Unmapped.push_back(ObjectAllocs());
114
115 LLVM_DEBUG(dbgs() << "Allocator " << Id << " reserved:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " reserved:\n"
; } } while (false)
;
116
117 if (CodeSize != 0) {
118 Unmapped.back().RemoteCodeAddr =
119 Client.reserveMem(Id, CodeSize, CodeAlign);
120
121 LLVM_DEBUG(dbgs() << " code: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " code: " << format("0x%016x"
, Unmapped.back().RemoteCodeAddr) << " (" << CodeSize
<< " bytes, alignment " << CodeAlign << ")\n"
; } } while (false)
122 << format("0x%016x", Unmapped.back().RemoteCodeAddr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " code: " << format("0x%016x"
, Unmapped.back().RemoteCodeAddr) << " (" << CodeSize
<< " bytes, alignment " << CodeAlign << ")\n"
; } } while (false)
123 << " (" << CodeSize << " bytes, alignment "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " code: " << format("0x%016x"
, Unmapped.back().RemoteCodeAddr) << " (" << CodeSize
<< " bytes, alignment " << CodeAlign << ")\n"
; } } while (false)
124 << CodeAlign << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " code: " << format("0x%016x"
, Unmapped.back().RemoteCodeAddr) << " (" << CodeSize
<< " bytes, alignment " << CodeAlign << ")\n"
; } } while (false)
;
125 }
126
127 if (RODataSize != 0) {
128 Unmapped.back().RemoteRODataAddr =
129 Client.reserveMem(Id, RODataSize, RODataAlign);
130
131 LLVM_DEBUG(dbgs() << " ro-data: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " ro-data: " << format("0x%016x"
, Unmapped.back().RemoteRODataAddr) << " (" << RODataSize
<< " bytes, alignment " << RODataAlign << ")\n"
; } } while (false)
132 << format("0x%016x", Unmapped.back().RemoteRODataAddr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " ro-data: " << format("0x%016x"
, Unmapped.back().RemoteRODataAddr) << " (" << RODataSize
<< " bytes, alignment " << RODataAlign << ")\n"
; } } while (false)
133 << " (" << RODataSize << " bytes, alignment "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " ro-data: " << format("0x%016x"
, Unmapped.back().RemoteRODataAddr) << " (" << RODataSize
<< " bytes, alignment " << RODataAlign << ")\n"
; } } while (false)
134 << RODataAlign << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " ro-data: " << format("0x%016x"
, Unmapped.back().RemoteRODataAddr) << " (" << RODataSize
<< " bytes, alignment " << RODataAlign << ")\n"
; } } while (false)
;
135 }
136
137 if (RWDataSize != 0) {
138 Unmapped.back().RemoteRWDataAddr =
139 Client.reserveMem(Id, RWDataSize, RWDataAlign);
140
141 LLVM_DEBUG(dbgs() << " rw-data: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " rw-data: " << format("0x%016x"
, Unmapped.back().RemoteRWDataAddr) << " (" << RWDataSize
<< " bytes, alignment " << RWDataAlign << ")\n"
; } } while (false)
142 << format("0x%016x", Unmapped.back().RemoteRWDataAddr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " rw-data: " << format("0x%016x"
, Unmapped.back().RemoteRWDataAddr) << " (" << RWDataSize
<< " bytes, alignment " << RWDataAlign << ")\n"
; } } while (false)
143 << " (" << RWDataSize << " bytes, alignment "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " rw-data: " << format("0x%016x"
, Unmapped.back().RemoteRWDataAddr) << " (" << RWDataSize
<< " bytes, alignment " << RWDataAlign << ")\n"
; } } while (false)
144 << RWDataAlign << ")\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " rw-data: " << format("0x%016x"
, Unmapped.back().RemoteRWDataAddr) << " (" << RWDataSize
<< " bytes, alignment " << RWDataAlign << ")\n"
; } } while (false)
;
145 }
146 }
147
148 bool needsToReserveAllocationSpace() override { return true; }
149
150 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
151 size_t Size) override {
152 UnfinalizedEHFrames.push_back({LoadAddr, Size});
153 }
154
155 void deregisterEHFrames() override {
156 for (auto &Frame : RegisteredEHFrames) {
157 // FIXME: Add error poll.
158 Client.deregisterEHFrames(Frame.Addr, Frame.Size);
159 }
160 }
161
162 void notifyObjectLoaded(RuntimeDyld &Dyld,
163 const object::ObjectFile &Obj) override {
164 LLVM_DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " applied mappings:\n"
; } } while (false)
;
165 for (auto &ObjAllocs : Unmapped) {
166 mapAllocsToRemoteAddrs(Dyld, ObjAllocs.CodeAllocs,
167 ObjAllocs.RemoteCodeAddr);
168 mapAllocsToRemoteAddrs(Dyld, ObjAllocs.RODataAllocs,
169 ObjAllocs.RemoteRODataAddr);
170 mapAllocsToRemoteAddrs(Dyld, ObjAllocs.RWDataAllocs,
171 ObjAllocs.RemoteRWDataAddr);
172 Unfinalized.push_back(std::move(ObjAllocs));
173 }
174 Unmapped.clear();
175 }
176
177 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
178 LLVM_DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Allocator " << Id << " finalizing:\n"
; } } while (false)
;
179
180 for (auto &ObjAllocs : Unfinalized) {
181 if (copyAndProtect(ObjAllocs.CodeAllocs, ObjAllocs.RemoteCodeAddr,
182 sys::Memory::MF_READ | sys::Memory::MF_EXEC))
183 return true;
184
185 if (copyAndProtect(ObjAllocs.RODataAllocs, ObjAllocs.RemoteRODataAddr,
186 sys::Memory::MF_READ))
187 return true;
188
189 if (copyAndProtect(ObjAllocs.RWDataAllocs, ObjAllocs.RemoteRWDataAddr,
190 sys::Memory::MF_READ | sys::Memory::MF_WRITE))
191 return true;
192 }
193 Unfinalized.clear();
194
195 for (auto &EHFrame : UnfinalizedEHFrames) {
196 if (auto Err = Client.registerEHFrames(EHFrame.Addr, EHFrame.Size)) {
197 // FIXME: Replace this once finalizeMemory can return an Error.
198 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
199 if (ErrMsg) {
200 raw_string_ostream ErrOut(*ErrMsg);
201 EIB.log(ErrOut);
202 }
203 });
204 return false;
205 }
206 }
207 RegisteredEHFrames = std::move(UnfinalizedEHFrames);
208 UnfinalizedEHFrames = {};
209
210 return false;
211 }
212
213 private:
214 class Alloc {
215 public:
216 Alloc(uint64_t Size, unsigned Align)
217 : Size(Size), Align(Align), Contents(new char[Size + Align - 1]) {}
218
219 Alloc(const Alloc &) = delete;
220 Alloc &operator=(const Alloc &) = delete;
221 Alloc(Alloc &&) = default;
222 Alloc &operator=(Alloc &&) = default;
223
224 uint64_t getSize() const { return Size; }
225
226 unsigned getAlign() const { return Align; }
227
228 char *getLocalAddress() const {
229 uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
230 LocalAddr = alignTo(LocalAddr, Align);
231 return reinterpret_cast<char *>(LocalAddr);
232 }
233
234 void setRemoteAddress(JITTargetAddress RemoteAddr) {
235 this->RemoteAddr = RemoteAddr;
236 }
237
238 JITTargetAddress getRemoteAddress() const { return RemoteAddr; }
239
240 private:
241 uint64_t Size;
242 unsigned Align;
243 std::unique_ptr<char[]> Contents;
244 JITTargetAddress RemoteAddr = 0;
245 };
246
247 struct ObjectAllocs {
248 ObjectAllocs() = default;
249 ObjectAllocs(const ObjectAllocs &) = delete;
250 ObjectAllocs &operator=(const ObjectAllocs &) = delete;
251 ObjectAllocs(ObjectAllocs &&) = default;
252 ObjectAllocs &operator=(ObjectAllocs &&) = default;
253
254 JITTargetAddress RemoteCodeAddr = 0;
255 JITTargetAddress RemoteRODataAddr = 0;
256 JITTargetAddress RemoteRWDataAddr = 0;
257 std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
258 };
259
260 RemoteRTDyldMemoryManager(OrcRemoteTargetClient &Client,
261 ResourceIdMgr::ResourceId Id)
262 : Client(Client), Id(Id) {
263 LLVM_DEBUG(dbgs() << "Created remote allocator " << Id << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Created remote allocator " <<
Id << "\n"; } } while (false)
;
264 }
265
266 // Maps all allocations in Allocs to aligned blocks
267 void mapAllocsToRemoteAddrs(RuntimeDyld &Dyld, std::vector<Alloc> &Allocs,
268 JITTargetAddress NextAddr) {
269 for (auto &Alloc : Allocs) {
270 NextAddr = alignTo(NextAddr, Alloc.getAlign());
271 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextAddr);
272 LLVM_DEBUG(dbgs() << " "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " " << static_cast<void
*>(Alloc.getLocalAddress()) << " -> " << format
("0x%016x", NextAddr) << "\n"; } } while (false)
273 << static_cast<void *>(Alloc.getLocalAddress())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " " << static_cast<void
*>(Alloc.getLocalAddress()) << " -> " << format
("0x%016x", NextAddr) << "\n"; } } while (false)
274 << " -> " << format("0x%016x", NextAddr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " " << static_cast<void
*>(Alloc.getLocalAddress()) << " -> " << format
("0x%016x", NextAddr) << "\n"; } } while (false)
;
275 Alloc.setRemoteAddress(NextAddr);
276
277 // Only advance NextAddr if it was non-null to begin with,
278 // otherwise leave it as null.
279 if (NextAddr)
280 NextAddr += Alloc.getSize();
281 }
282 }
283
284 // Copies data for each alloc in the list, then set permissions on the
285 // segment.
286 bool copyAndProtect(const std::vector<Alloc> &Allocs,
287 JITTargetAddress RemoteSegmentAddr,
288 unsigned Permissions) {
289 if (RemoteSegmentAddr) {
290 assert(!Allocs.empty() && "No sections in allocated segment")((!Allocs.empty() && "No sections in allocated segment"
) ? static_cast<void> (0) : __assert_fail ("!Allocs.empty() && \"No sections in allocated segment\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 290, __PRETTY_FUNCTION__))
;
291
292 for (auto &Alloc : Allocs) {
293 LLVM_DEBUG(dbgs() << " copying section: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " copying section: " << static_cast
<void *>(Alloc.getLocalAddress()) << " -> " <<
format("0x%016x", Alloc.getRemoteAddress()) << " (" <<
Alloc.getSize() << " bytes)\n";; } } while (false)
294 << static_cast<void *>(Alloc.getLocalAddress())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " copying section: " << static_cast
<void *>(Alloc.getLocalAddress()) << " -> " <<
format("0x%016x", Alloc.getRemoteAddress()) << " (" <<
Alloc.getSize() << " bytes)\n";; } } while (false)
295 << " -> "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " copying section: " << static_cast
<void *>(Alloc.getLocalAddress()) << " -> " <<
format("0x%016x", Alloc.getRemoteAddress()) << " (" <<
Alloc.getSize() << " bytes)\n";; } } while (false)
296 << format("0x%016x", Alloc.getRemoteAddress())do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " copying section: " << static_cast
<void *>(Alloc.getLocalAddress()) << " -> " <<
format("0x%016x", Alloc.getRemoteAddress()) << " (" <<
Alloc.getSize() << " bytes)\n";; } } while (false)
297 << " (" << Alloc.getSize() << " bytes)\n";)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " copying section: " << static_cast
<void *>(Alloc.getLocalAddress()) << " -> " <<
format("0x%016x", Alloc.getRemoteAddress()) << " (" <<
Alloc.getSize() << " bytes)\n";; } } while (false)
;
298
299 if (Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
300 Alloc.getSize()))
301 return true;
302 }
303
304 LLVM_DEBUG(dbgs() << " setting "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
305 << (Permissions & sys::Memory::MF_READ ? 'R' : '-')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
306 << (Permissions & sys::Memory::MF_WRITE ? 'W' : '-')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
307 << (Permissions & sys::Memory::MF_EXEC ? 'X' : '-')do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
308 << " permissions on block: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
309 << format("0x%016x", RemoteSegmentAddr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << " setting " << (Permissions
& sys::Memory::MF_READ ? 'R' : '-') << (Permissions
& sys::Memory::MF_WRITE ? 'W' : '-') << (Permissions
& sys::Memory::MF_EXEC ? 'X' : '-') << " permissions on block: "
<< format("0x%016x", RemoteSegmentAddr) << "\n";
} } while (false)
;
310 if (Client.setProtections(Id, RemoteSegmentAddr, Permissions))
311 return true;
312 }
313 return false;
314 }
315
316 OrcRemoteTargetClient &Client;
317 ResourceIdMgr::ResourceId Id;
318 std::vector<ObjectAllocs> Unmapped;
319 std::vector<ObjectAllocs> Unfinalized;
320
321 struct EHFrame {
322 JITTargetAddress Addr;
323 uint64_t Size;
324 };
325 std::vector<EHFrame> UnfinalizedEHFrames;
326 std::vector<EHFrame> RegisteredEHFrames;
327 };
328
329 /// Remote indirect stubs manager.
330 class RemoteIndirectStubsManager : public IndirectStubsManager {
331 public:
332 RemoteIndirectStubsManager(OrcRemoteTargetClient &Client,
333 ResourceIdMgr::ResourceId Id)
334 : Client(Client), Id(Id) {}
335
336 ~RemoteIndirectStubsManager() override {
337 Client.destroyIndirectStubsManager(Id);
338 }
339
340 Error createStub(StringRef StubName, JITTargetAddress StubAddr,
341 JITSymbolFlags StubFlags) override {
342 if (auto Err = reserveStubs(1))
343 return Err;
344
345 return createStubInternal(StubName, StubAddr, StubFlags);
346 }
347
348 Error createStubs(const StubInitsMap &StubInits) override {
349 if (auto Err = reserveStubs(StubInits.size()))
350 return Err;
351
352 for (auto &Entry : StubInits)
353 if (auto Err = createStubInternal(Entry.first(), Entry.second.first,
354 Entry.second.second))
355 return Err;
356
357 return Error::success();
358 }
359
360 JITEvaluatedSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
361 auto I = StubIndexes.find(Name);
362 if (I == StubIndexes.end())
363 return nullptr;
364 auto Key = I->second.first;
365 auto Flags = I->second.second;
366 auto StubSymbol = JITEvaluatedSymbol(getStubAddr(Key), Flags);
367 if (ExportedStubsOnly && !StubSymbol.getFlags().isExported())
368 return nullptr;
369 return StubSymbol;
370 }
371
372 JITEvaluatedSymbol findPointer(StringRef Name) override {
373 auto I = StubIndexes.find(Name);
374 if (I == StubIndexes.end())
375 return nullptr;
376 auto Key = I->second.first;
377 auto Flags = I->second.second;
378 return JITEvaluatedSymbol(getPtrAddr(Key), Flags);
379 }
380
381 Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override {
382 auto I = StubIndexes.find(Name);
383 assert(I != StubIndexes.end() && "No stub pointer for symbol")((I != StubIndexes.end() && "No stub pointer for symbol"
) ? static_cast<void> (0) : __assert_fail ("I != StubIndexes.end() && \"No stub pointer for symbol\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 383, __PRETTY_FUNCTION__))
;
384 auto Key = I->second.first;
385 return Client.writePointer(getPtrAddr(Key), NewAddr);
386 }
387
388 private:
389 struct RemoteIndirectStubsInfo {
390 JITTargetAddress StubBase;
391 JITTargetAddress PtrBase;
392 unsigned NumStubs;
393 };
394
395 using StubKey = std::pair<uint16_t, uint16_t>;
396
397 Error reserveStubs(unsigned NumStubs) {
398 if (NumStubs <= FreeStubs.size())
399 return Error::success();
400
401 unsigned NewStubsRequired = NumStubs - FreeStubs.size();
402 JITTargetAddress StubBase;
403 JITTargetAddress PtrBase;
404 unsigned NumStubsEmitted;
405
406 if (auto StubInfoOrErr = Client.emitIndirectStubs(Id, NewStubsRequired))
407 std::tie(StubBase, PtrBase, NumStubsEmitted) = *StubInfoOrErr;
408 else
409 return StubInfoOrErr.takeError();
410
411 unsigned NewBlockId = RemoteIndirectStubsInfos.size();
412 RemoteIndirectStubsInfos.push_back({StubBase, PtrBase, NumStubsEmitted});
413
414 for (unsigned I = 0; I < NumStubsEmitted; ++I)
415 FreeStubs.push_back(std::make_pair(NewBlockId, I));
416
417 return Error::success();
418 }
419
420 Error createStubInternal(StringRef StubName, JITTargetAddress InitAddr,
421 JITSymbolFlags StubFlags) {
422 auto Key = FreeStubs.back();
423 FreeStubs.pop_back();
424 StubIndexes[StubName] = std::make_pair(Key, StubFlags);
425 return Client.writePointer(getPtrAddr(Key), InitAddr);
426 }
427
428 JITTargetAddress getStubAddr(StubKey K) {
429 assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&((RemoteIndirectStubsInfos[K.first].StubBase != 0 && "Missing stub address"
) ? static_cast<void> (0) : __assert_fail ("RemoteIndirectStubsInfos[K.first].StubBase != 0 && \"Missing stub address\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 430, __PRETTY_FUNCTION__))
430 "Missing stub address")((RemoteIndirectStubsInfos[K.first].StubBase != 0 && "Missing stub address"
) ? static_cast<void> (0) : __assert_fail ("RemoteIndirectStubsInfos[K.first].StubBase != 0 && \"Missing stub address\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 430, __PRETTY_FUNCTION__))
;
431 return RemoteIndirectStubsInfos[K.first].StubBase +
432 K.second * Client.getIndirectStubSize();
433 }
434
435 JITTargetAddress getPtrAddr(StubKey K) {
436 assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&((RemoteIndirectStubsInfos[K.first].PtrBase != 0 && "Missing pointer address"
) ? static_cast<void> (0) : __assert_fail ("RemoteIndirectStubsInfos[K.first].PtrBase != 0 && \"Missing pointer address\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 437, __PRETTY_FUNCTION__))
437 "Missing pointer address")((RemoteIndirectStubsInfos[K.first].PtrBase != 0 && "Missing pointer address"
) ? static_cast<void> (0) : __assert_fail ("RemoteIndirectStubsInfos[K.first].PtrBase != 0 && \"Missing pointer address\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 437, __PRETTY_FUNCTION__))
;
438 return RemoteIndirectStubsInfos[K.first].PtrBase +
439 K.second * Client.getPointerSize();
440 }
441
442 OrcRemoteTargetClient &Client;
443 ResourceIdMgr::ResourceId Id;
444 std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
445 std::vector<StubKey> FreeStubs;
446 StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
447 };
448
449 class RemoteTrampolinePool : public TrampolinePool {
450 public:
451 RemoteTrampolinePool(OrcRemoteTargetClient &Client) : Client(Client) {}
452
453 Expected<JITTargetAddress> getTrampoline() override {
454 std::lock_guard<std::mutex> Lock(RTPMutex);
455 if (AvailableTrampolines.empty()) {
456 if (auto Err = grow())
457 return std::move(Err);
458 }
459 assert(!AvailableTrampolines.empty() && "Failed to grow trampoline pool")((!AvailableTrampolines.empty() && "Failed to grow trampoline pool"
) ? static_cast<void> (0) : __assert_fail ("!AvailableTrampolines.empty() && \"Failed to grow trampoline pool\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 459, __PRETTY_FUNCTION__))
;
460 auto TrampolineAddr = AvailableTrampolines.back();
461 AvailableTrampolines.pop_back();
462 return TrampolineAddr;
463 }
464
465 private:
466 Error grow() {
467 JITTargetAddress BlockAddr = 0;
468 uint32_t NumTrampolines = 0;
469 if (auto TrampolineInfoOrErr = Client.emitTrampolineBlock())
470 std::tie(BlockAddr, NumTrampolines) = *TrampolineInfoOrErr;
471 else
472 return TrampolineInfoOrErr.takeError();
473
474 uint32_t TrampolineSize = Client.getTrampolineSize();
475 for (unsigned I = 0; I < NumTrampolines; ++I)
476 this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
477
478 return Error::success();
479 }
480
481 std::mutex RTPMutex;
482 OrcRemoteTargetClient &Client;
483 std::vector<JITTargetAddress> AvailableTrampolines;
484 };
485
486 /// Remote compile callback manager.
487 class RemoteCompileCallbackManager : public JITCompileCallbackManager {
488 public:
489 RemoteCompileCallbackManager(OrcRemoteTargetClient &Client,
490 ExecutionSession &ES,
491 JITTargetAddress ErrorHandlerAddress)
492 : JITCompileCallbackManager(
493 llvm::make_unique<RemoteTrampolinePool>(Client), ES,
494 ErrorHandlerAddress) {}
495 };
496
497 /// Create an OrcRemoteTargetClient.
498 /// Channel is the ChannelT instance to communicate on. It is assumed that
499 /// the channel is ready to be read from and written to.
500 static Expected<std::unique_ptr<OrcRemoteTargetClient>>
501 Create(rpc::RawByteChannel &Channel, ExecutionSession &ES) {
502 Error Err = Error::success();
503 auto Client = std::unique_ptr<OrcRemoteTargetClient>(
504 new OrcRemoteTargetClient(Channel, ES, Err));
505 if (Err)
506 return std::move(Err);
507 return std::move(Client);
508 }
509
510 /// Call the int(void) function at the given address in the target and return
511 /// its result.
512 Expected<int> callIntVoid(JITTargetAddress Addr) {
513 LLVM_DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling int(*)(void) " << format
("0x%016x", Addr) << "\n"; } } while (false)
514 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling int(*)(void) " << format
("0x%016x", Addr) << "\n"; } } while (false)
;
515 return callB<exec::CallIntVoid>(Addr);
516 }
517
518 /// Call the int(int, char*[]) function at the given address in the target and
519 /// return its result.
520 Expected<int> callMain(JITTargetAddress Addr,
521 const std::vector<std::string> &Args) {
522 LLVM_DEBUG(dbgs() << "Calling int(*)(int, char*[]) "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling int(*)(int, char*[]) " <<
format("0x%016x", Addr) << "\n"; } } while (false)
523 << format("0x%016x", Addr) << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling int(*)(int, char*[]) " <<
format("0x%016x", Addr) << "\n"; } } while (false)
;
524 return callB<exec::CallMain>(Addr, Args);
525 }
526
527 /// Call the void() function at the given address in the target and wait for
528 /// it to finish.
529 Error callVoidVoid(JITTargetAddress Addr) {
530 LLVM_DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling void(*)(void) " << format
("0x%016x", Addr) << "\n"; } } while (false)
531 << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType
("lli")) { dbgs() << "Calling void(*)(void) " << format
("0x%016x", Addr) << "\n"; } } while (false)
;
532 return callB<exec::CallVoidVoid>(Addr);
533 }
534
535 /// Create an RCMemoryManager which will allocate its memory on the remote
536 /// target.
537 Expected<std::unique_ptr<RemoteRTDyldMemoryManager>>
538 createRemoteMemoryManager() {
539 auto Id = AllocatorIds.getNext();
540 if (auto Err = callB<mem::CreateRemoteAllocator>(Id))
541 return std::move(Err);
542 return std::unique_ptr<RemoteRTDyldMemoryManager>(
543 new RemoteRTDyldMemoryManager(*this, Id));
544 }
545
546 /// Create an RCIndirectStubsManager that will allocate stubs on the remote
547 /// target.
548 Expected<std::unique_ptr<RemoteIndirectStubsManager>>
549 createIndirectStubsManager() {
550 auto Id = IndirectStubOwnerIds.getNext();
551 if (auto Err = callB<stubs::CreateIndirectStubsOwner>(Id))
552 return std::move(Err);
553 return llvm::make_unique<RemoteIndirectStubsManager>(*this, Id);
554 }
555
556 Expected<RemoteCompileCallbackManager &>
557 enableCompileCallbacks(JITTargetAddress ErrorHandlerAddress) {
558 assert(!CallbackManager && "CallbackManager already obtained")((!CallbackManager && "CallbackManager already obtained"
) ? static_cast<void> (0) : __assert_fail ("!CallbackManager && \"CallbackManager already obtained\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 558, __PRETTY_FUNCTION__))
;
559
560 // Emit the resolver block on the JIT server.
561 if (auto Err = callB<stubs::EmitResolverBlock>())
562 return std::move(Err);
563
564 // Create the callback manager.
565 CallbackManager.emplace(*this, ES, ErrorHandlerAddress);
566 RemoteCompileCallbackManager &Mgr = *CallbackManager;
567 return Mgr;
568 }
569
570 /// Search for symbols in the remote process. Note: This should be used by
571 /// symbol resolvers *after* they've searched the local symbol table in the
572 /// JIT stack.
573 Expected<JITTargetAddress> getSymbolAddress(StringRef Name) {
574 return callB<utils::GetSymbolAddress>(Name);
2
Calling 'SingleThreadedRPCEndpoint::callB'
575 }
576
577 /// Get the triple for the remote target.
578 const std::string &getTargetTriple() const { return RemoteTargetTriple; }
579
580 Error terminateSession() { return callB<utils::TerminateSession>(); }
581
582private:
583 OrcRemoteTargetClient(rpc::RawByteChannel &Channel, ExecutionSession &ES,
584 Error &Err)
585 : rpc::SingleThreadedRPCEndpoint<rpc::RawByteChannel>(Channel, true),
586 ES(ES) {
587 ErrorAsOutParameter EAO(&Err);
588
589 addHandler<utils::RequestCompile>(
590 [this](JITTargetAddress Addr) -> JITTargetAddress {
591 if (CallbackManager)
592 return CallbackManager->executeCompileCallback(Addr);
593 return 0;
594 });
595
596 if (auto RIOrErr = callB<utils::GetRemoteInfo>()) {
597 std::tie(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
598 RemoteTrampolineSize, RemoteIndirectStubSize) = *RIOrErr;
599 Err = Error::success();
600 } else
601 Err = RIOrErr.takeError();
602 }
603
604 void deregisterEHFrames(JITTargetAddress Addr, uint32_t Size) {
605 if (auto Err = callB<eh::RegisterEHFrames>(Addr, Size))
606 ES.reportError(std::move(Err));
607 }
608
609 void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
610 if (auto Err = callB<mem::DestroyRemoteAllocator>(Id)) {
611 // FIXME: This will be triggered by a removeModuleSet call: Propagate
612 // error return up through that.
613 llvm_unreachable("Failed to destroy remote allocator.")::llvm::llvm_unreachable_internal("Failed to destroy remote allocator."
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h"
, 613)
;
614 AllocatorIds.release(Id);
615 }
616 }
617
618 void destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
619 IndirectStubOwnerIds.release(Id);
620 if (auto Err = callB<stubs::DestroyIndirectStubsOwner>(Id))
621 ES.reportError(std::move(Err));
622 }
623
624 Expected<std::tuple<JITTargetAddress, JITTargetAddress, uint32_t>>
625 emitIndirectStubs(ResourceIdMgr::ResourceId Id, uint32_t NumStubsRequired) {
626 return callB<stubs::EmitIndirectStubs>(Id, NumStubsRequired);
627 }
628
629 Expected<std::tuple<JITTargetAddress, uint32_t>> emitTrampolineBlock() {
630 return callB<stubs::EmitTrampolineBlock>();
631 }
632
633 uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
634 uint32_t getPageSize() const { return RemotePageSize; }
635 uint32_t getPointerSize() const { return RemotePointerSize; }
636
637 uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
638
639 Expected<std::vector<uint8_t>> readMem(char *Dst, JITTargetAddress Src,
640 uint64_t Size) {
641 return callB<mem::ReadMem>(Src, Size);
642 }
643
644 Error registerEHFrames(JITTargetAddress &RAddr, uint32_t Size) {
645 // FIXME: Duplicate error and report it via ReportError too?
646 return callB<eh::RegisterEHFrames>(RAddr, Size);
647 }
648
649 JITTargetAddress reserveMem(ResourceIdMgr::ResourceId Id, uint64_t Size,
650 uint32_t Align) {
651 if (auto AddrOrErr = callB<mem::ReserveMem>(Id, Size, Align))
652 return *AddrOrErr;
653 else {
654 ES.reportError(AddrOrErr.takeError());
655 return 0;
656 }
657 }
658
659 bool setProtections(ResourceIdMgr::ResourceId Id,
660 JITTargetAddress RemoteSegAddr, unsigned ProtFlags) {
661 if (auto Err = callB<mem::SetProtections>(Id, RemoteSegAddr, ProtFlags)) {
662 ES.reportError(std::move(Err));
663 return true;
664 } else
665 return false;
666 }
667
668 bool writeMem(JITTargetAddress Addr, const char *Src, uint64_t Size) {
669 if (auto Err = callB<mem::WriteMem>(DirectBufferWriter(Src, Addr, Size))) {
670 ES.reportError(std::move(Err));
671 return true;
672 } else
673 return false;
674 }
675
676 Error writePointer(JITTargetAddress Addr, JITTargetAddress PtrVal) {
677 return callB<mem::WritePtr>(Addr, PtrVal);
678 }
679
680 static Error doNothing() { return Error::success(); }
681
682 ExecutionSession &ES;
683 std::function<void(Error)> ReportError;
684 std::string RemoteTargetTriple;
685 uint32_t RemotePointerSize = 0;
686 uint32_t RemotePageSize = 0;
687 uint32_t RemoteTrampolineSize = 0;
688 uint32_t RemoteIndirectStubSize = 0;
689 ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
690 Optional<RemoteCompileCallbackManager> CallbackManager;
691};
692
693} // end namespace remote
694} // end namespace orc
695} // end namespace llvm
696
697#undef DEBUG_TYPE"lli"
698
699#endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H

/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h

1//===------- RPCUTils.h - Utilities for building RPC APIs -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Utilities to support construction of simple RPC APIs.
11//
12// The RPC utilities aim for ease of use (minimal conceptual overhead) for C++
13// programmers, high performance, low memory overhead, and efficient use of the
14// communications channel.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
19#define LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
20
21#include <map>
22#include <thread>
23#include <vector>
24
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ExecutionEngine/Orc/OrcError.h"
27#include "llvm/ExecutionEngine/Orc/RPCSerialization.h"
28#include "llvm/Support/MSVCErrorWorkarounds.h"
29
30#include <future>
31
32namespace llvm {
33namespace orc {
34namespace rpc {
35
36/// Base class of all fatal RPC errors (those that necessarily result in the
37/// termination of the RPC session).
38class RPCFatalError : public ErrorInfo<RPCFatalError> {
39public:
40 static char ID;
41};
42
43/// RPCConnectionClosed is returned from RPC operations if the RPC connection
44/// has already been closed due to either an error or graceful disconnection.
45class ConnectionClosed : public ErrorInfo<ConnectionClosed> {
46public:
47 static char ID;
48 std::error_code convertToErrorCode() const override;
49 void log(raw_ostream &OS) const override;
50};
51
52/// BadFunctionCall is returned from handleOne when the remote makes a call with
53/// an unrecognized function id.
54///
55/// This error is fatal because Orc RPC needs to know how to parse a function
56/// call to know where the next call starts, and if it doesn't recognize the
57/// function id it cannot parse the call.
58template <typename FnIdT, typename SeqNoT>
59class BadFunctionCall
60 : public ErrorInfo<BadFunctionCall<FnIdT, SeqNoT>, RPCFatalError> {
61public:
62 static char ID;
63
64 BadFunctionCall(FnIdT FnId, SeqNoT SeqNo)
65 : FnId(std::move(FnId)), SeqNo(std::move(SeqNo)) {}
66
67 std::error_code convertToErrorCode() const override {
68 return orcError(OrcErrorCode::UnexpectedRPCCall);
69 }
70
71 void log(raw_ostream &OS) const override {
72 OS << "Call to invalid RPC function id '" << FnId << "' with "
73 "sequence number " << SeqNo;
74 }
75
76private:
77 FnIdT FnId;
78 SeqNoT SeqNo;
79};
80
81template <typename FnIdT, typename SeqNoT>
82char BadFunctionCall<FnIdT, SeqNoT>::ID = 0;
83
84/// InvalidSequenceNumberForResponse is returned from handleOne when a response
85/// call arrives with a sequence number that doesn't correspond to any in-flight
86/// function call.
87///
88/// This error is fatal because Orc RPC needs to know how to parse the rest of
89/// the response call to know where the next call starts, and if it doesn't have
90/// a result parser for this sequence number it can't do that.
91template <typename SeqNoT>
92class InvalidSequenceNumberForResponse
93 : public ErrorInfo<InvalidSequenceNumberForResponse<SeqNoT>, RPCFatalError> {
94public:
95 static char ID;
96
97 InvalidSequenceNumberForResponse(SeqNoT SeqNo)
98 : SeqNo(std::move(SeqNo)) {}
99
100 std::error_code convertToErrorCode() const override {
101 return orcError(OrcErrorCode::UnexpectedRPCCall);
102 };
103
104 void log(raw_ostream &OS) const override {
105 OS << "Response has unknown sequence number " << SeqNo;
106 }
107private:
108 SeqNoT SeqNo;
109};
110
111template <typename SeqNoT>
112char InvalidSequenceNumberForResponse<SeqNoT>::ID = 0;
113
114/// This non-fatal error will be passed to asynchronous result handlers in place
115/// of a result if the connection goes down before a result returns, or if the
116/// function to be called cannot be negotiated with the remote.
117class ResponseAbandoned : public ErrorInfo<ResponseAbandoned> {
118public:
119 static char ID;
120
121 std::error_code convertToErrorCode() const override;
122 void log(raw_ostream &OS) const override;
123};
124
125/// This error is returned if the remote does not have a handler installed for
126/// the given RPC function.
127class CouldNotNegotiate : public ErrorInfo<CouldNotNegotiate> {
128public:
129 static char ID;
130
131 CouldNotNegotiate(std::string Signature);
132 std::error_code convertToErrorCode() const override;
133 void log(raw_ostream &OS) const override;
134 const std::string &getSignature() const { return Signature; }
135private:
136 std::string Signature;
137};
138
139template <typename DerivedFunc, typename FnT> class Function;
140
141// RPC Function class.
142// DerivedFunc should be a user defined class with a static 'getName()' method
143// returning a const char* representing the function's name.
144template <typename DerivedFunc, typename RetT, typename... ArgTs>
145class Function<DerivedFunc, RetT(ArgTs...)> {
146public:
147 /// User defined function type.
148 using Type = RetT(ArgTs...);
149
150 /// Return type.
151 using ReturnType = RetT;
152
153 /// Returns the full function prototype as a string.
154 static const char *getPrototype() {
155 std::lock_guard<std::mutex> Lock(NameMutex);
156 if (Name.empty())
157 raw_string_ostream(Name)
158 << RPCTypeName<RetT>::getName() << " " << DerivedFunc::getName()
159 << "(" << llvm::orc::rpc::RPCTypeNameSequence<ArgTs...>() << ")";
160 return Name.data();
161 }
162
163private:
164 static std::mutex NameMutex;
165 static std::string Name;
166};
167
168template <typename DerivedFunc, typename RetT, typename... ArgTs>
169std::mutex Function<DerivedFunc, RetT(ArgTs...)>::NameMutex;
170
171template <typename DerivedFunc, typename RetT, typename... ArgTs>
172std::string Function<DerivedFunc, RetT(ArgTs...)>::Name;
173
174/// Allocates RPC function ids during autonegotiation.
175/// Specializations of this class must provide four members:
176///
177/// static T getInvalidId():
178/// Should return a reserved id that will be used to represent missing
179/// functions during autonegotiation.
180///
181/// static T getResponseId():
182/// Should return a reserved id that will be used to send function responses
183/// (return values).
184///
185/// static T getNegotiateId():
186/// Should return a reserved id for the negotiate function, which will be used
187/// to negotiate ids for user defined functions.
188///
189/// template <typename Func> T allocate():
190/// Allocate a unique id for function Func.
191template <typename T, typename = void> class RPCFunctionIdAllocator;
192
193/// This specialization of RPCFunctionIdAllocator provides a default
194/// implementation for integral types.
195template <typename T>
196class RPCFunctionIdAllocator<
197 T, typename std::enable_if<std::is_integral<T>::value>::type> {
198public:
199 static T getInvalidId() { return T(0); }
200 static T getResponseId() { return T(1); }
201 static T getNegotiateId() { return T(2); }
202
203 template <typename Func> T allocate() { return NextId++; }
204
205private:
206 T NextId = 3;
207};
208
209namespace detail {
210
211/// Provides a typedef for a tuple containing the decayed argument types.
212template <typename T> class FunctionArgsTuple;
213
214template <typename RetT, typename... ArgTs>
215class FunctionArgsTuple<RetT(ArgTs...)> {
216public:
217 using Type = std::tuple<typename std::decay<
218 typename std::remove_reference<ArgTs>::type>::type...>;
219};
220
221// ResultTraits provides typedefs and utilities specific to the return type
222// of functions.
223template <typename RetT> class ResultTraits {
224public:
225 // The return type wrapped in llvm::Expected.
226 using ErrorReturnType = Expected<RetT>;
227
228#ifdef _MSC_VER
229 // The ErrorReturnType wrapped in a std::promise.
230 using ReturnPromiseType = std::promise<MSVCPExpected<RetT>>;
231
232 // The ErrorReturnType wrapped in a std::future.
233 using ReturnFutureType = std::future<MSVCPExpected<RetT>>;
234#else
235 // The ErrorReturnType wrapped in a std::promise.
236 using ReturnPromiseType = std::promise<ErrorReturnType>;
237
238 // The ErrorReturnType wrapped in a std::future.
239 using ReturnFutureType = std::future<ErrorReturnType>;
240#endif
241
242 // Create a 'blank' value of the ErrorReturnType, ready and safe to
243 // overwrite.
244 static ErrorReturnType createBlankErrorReturnValue() {
245 return ErrorReturnType(RetT());
246 }
247
248 // Consume an abandoned ErrorReturnType.
249 static void consumeAbandoned(ErrorReturnType RetOrErr) {
250 consumeError(RetOrErr.takeError());
251 }
252};
253
254// ResultTraits specialization for void functions.
255template <> class ResultTraits<void> {
256public:
257 // For void functions, ErrorReturnType is llvm::Error.
258 using ErrorReturnType = Error;
259
260#ifdef _MSC_VER
261 // The ErrorReturnType wrapped in a std::promise.
262 using ReturnPromiseType = std::promise<MSVCPError>;
263
264 // The ErrorReturnType wrapped in a std::future.
265 using ReturnFutureType = std::future<MSVCPError>;
266#else
267 // The ErrorReturnType wrapped in a std::promise.
268 using ReturnPromiseType = std::promise<ErrorReturnType>;
269
270 // The ErrorReturnType wrapped in a std::future.
271 using ReturnFutureType = std::future<ErrorReturnType>;
272#endif
273
274 // Create a 'blank' value of the ErrorReturnType, ready and safe to
275 // overwrite.
276 static ErrorReturnType createBlankErrorReturnValue() {
277 return ErrorReturnType::success();
278 }
279
280 // Consume an abandoned ErrorReturnType.
281 static void consumeAbandoned(ErrorReturnType Err) {
282 consumeError(std::move(Err));
283 }
284};
285
286// ResultTraits<Error> is equivalent to ResultTraits<void>. This allows
287// handlers for void RPC functions to return either void (in which case they
288// implicitly succeed) or Error (in which case their error return is
289// propagated). See usage in HandlerTraits::runHandlerHelper.
290template <> class ResultTraits<Error> : public ResultTraits<void> {};
291
292// ResultTraits<Expected<T>> is equivalent to ResultTraits<T>. This allows
293// handlers for RPC functions returning a T to return either a T (in which
294// case they implicitly succeed) or Expected<T> (in which case their error
295// return is propagated). See usage in HandlerTraits::runHandlerHelper.
296template <typename RetT>
297class ResultTraits<Expected<RetT>> : public ResultTraits<RetT> {};
298
299// Determines whether an RPC function's defined error return type supports
300// error return value.
301template <typename T>
302class SupportsErrorReturn {
303public:
304 static const bool value = false;
305};
306
307template <>
308class SupportsErrorReturn<Error> {
309public:
310 static const bool value = true;
311};
312
313template <typename T>
314class SupportsErrorReturn<Expected<T>> {
315public:
316 static const bool value = true;
317};
318
319// RespondHelper packages return values based on whether or not the declared
320// RPC function return type supports error returns.
321template <bool FuncSupportsErrorReturn>
322class RespondHelper;
323
324// RespondHelper specialization for functions that support error returns.
325template <>
326class RespondHelper<true> {
327public:
328
329 // Send Expected<T>.
330 template <typename WireRetT, typename HandlerRetT, typename ChannelT,
331 typename FunctionIdT, typename SequenceNumberT>
332 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
333 SequenceNumberT SeqNo,
334 Expected<HandlerRetT> ResultOrErr) {
335 if (!ResultOrErr && ResultOrErr.template errorIsA<RPCFatalError>())
336 return ResultOrErr.takeError();
337
338 // Open the response message.
339 if (auto Err = C.startSendMessage(ResponseId, SeqNo))
340 return Err;
341
342 // Serialize the result.
343 if (auto Err =
344 SerializationTraits<ChannelT, WireRetT,
345 Expected<HandlerRetT>>::serialize(
346 C, std::move(ResultOrErr)))
347 return Err;
348
349 // Close the response message.
350 return C.endSendMessage();
351 }
352
353 template <typename ChannelT, typename FunctionIdT, typename SequenceNumberT>
354 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
355 SequenceNumberT SeqNo, Error Err) {
356 if (Err && Err.isA<RPCFatalError>())
357 return Err;
358 if (auto Err2 = C.startSendMessage(ResponseId, SeqNo))
359 return Err2;
360 if (auto Err2 = serializeSeq(C, std::move(Err)))
361 return Err2;
362 return C.endSendMessage();
363 }
364
365};
366
367// RespondHelper specialization for functions that do not support error returns.
368template <>
369class RespondHelper<false> {
370public:
371
372 template <typename WireRetT, typename HandlerRetT, typename ChannelT,
373 typename FunctionIdT, typename SequenceNumberT>
374 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
375 SequenceNumberT SeqNo,
376 Expected<HandlerRetT> ResultOrErr) {
377 if (auto Err = ResultOrErr.takeError())
378 return Err;
379
380 // Open the response message.
381 if (auto Err = C.startSendMessage(ResponseId, SeqNo))
382 return Err;
383
384 // Serialize the result.
385 if (auto Err =
386 SerializationTraits<ChannelT, WireRetT, HandlerRetT>::serialize(
387 C, *ResultOrErr))
388 return Err;
389
390 // Close the response message.
391 return C.endSendMessage();
392 }
393
394 template <typename ChannelT, typename FunctionIdT, typename SequenceNumberT>
395 static Error sendResult(ChannelT &C, const FunctionIdT &ResponseId,
396 SequenceNumberT SeqNo, Error Err) {
397 if (Err)
398 return Err;
399 if (auto Err2 = C.startSendMessage(ResponseId, SeqNo))
400 return Err2;
401 return C.endSendMessage();
402 }
403
404};
405
406
407// Send a response of the given wire return type (WireRetT) over the
408// channel, with the given sequence number.
409template <typename WireRetT, typename HandlerRetT, typename ChannelT,
410 typename FunctionIdT, typename SequenceNumberT>
411Error respond(ChannelT &C, const FunctionIdT &ResponseId,
412 SequenceNumberT SeqNo, Expected<HandlerRetT> ResultOrErr) {
413 return RespondHelper<SupportsErrorReturn<WireRetT>::value>::
414 template sendResult<WireRetT>(C, ResponseId, SeqNo, std::move(ResultOrErr));
415}
416
417// Send an empty response message on the given channel to indicate that
418// the handler ran.
419template <typename WireRetT, typename ChannelT, typename FunctionIdT,
420 typename SequenceNumberT>
421Error respond(ChannelT &C, const FunctionIdT &ResponseId, SequenceNumberT SeqNo,
422 Error Err) {
423 return RespondHelper<SupportsErrorReturn<WireRetT>::value>::
424 sendResult(C, ResponseId, SeqNo, std::move(Err));
425}
426
427// Converts a given type to the equivalent error return type.
428template <typename T> class WrappedHandlerReturn {
429public:
430 using Type = Expected<T>;
431};
432
433template <typename T> class WrappedHandlerReturn<Expected<T>> {
434public:
435 using Type = Expected<T>;
436};
437
438template <> class WrappedHandlerReturn<void> {
439public:
440 using Type = Error;
441};
442
443template <> class WrappedHandlerReturn<Error> {
444public:
445 using Type = Error;
446};
447
448template <> class WrappedHandlerReturn<ErrorSuccess> {
449public:
450 using Type = Error;
451};
452
453// Traits class that strips the response function from the list of handler
454// arguments.
455template <typename FnT> class AsyncHandlerTraits;
456
457template <typename ResultT, typename... ArgTs>
458class AsyncHandlerTraits<Error(std::function<Error(Expected<ResultT>)>, ArgTs...)> {
459public:
460 using Type = Error(ArgTs...);
461 using ResultType = Expected<ResultT>;
462};
463
464template <typename... ArgTs>
465class AsyncHandlerTraits<Error(std::function<Error(Error)>, ArgTs...)> {
466public:
467 using Type = Error(ArgTs...);
468 using ResultType = Error;
469};
470
471template <typename... ArgTs>
472class AsyncHandlerTraits<ErrorSuccess(std::function<Error(Error)>, ArgTs...)> {
473public:
474 using Type = Error(ArgTs...);
475 using ResultType = Error;
476};
477
478template <typename... ArgTs>
479class AsyncHandlerTraits<void(std::function<Error(Error)>, ArgTs...)> {
480public:
481 using Type = Error(ArgTs...);
482 using ResultType = Error;
483};
484
485template <typename ResponseHandlerT, typename... ArgTs>
486class AsyncHandlerTraits<Error(ResponseHandlerT, ArgTs...)> :
487 public AsyncHandlerTraits<Error(typename std::decay<ResponseHandlerT>::type,
488 ArgTs...)> {};
489
490// This template class provides utilities related to RPC function handlers.
491// The base case applies to non-function types (the template class is
492// specialized for function types) and inherits from the appropriate
493// speciilization for the given non-function type's call operator.
494template <typename HandlerT>
495class HandlerTraits : public HandlerTraits<decltype(
496 &std::remove_reference<HandlerT>::type::operator())> {
497};
498
499// Traits for handlers with a given function type.
500template <typename RetT, typename... ArgTs>
501class HandlerTraits<RetT(ArgTs...)> {
502public:
503 // Function type of the handler.
504 using Type = RetT(ArgTs...);
505
506 // Return type of the handler.
507 using ReturnType = RetT;
508
509 // Call the given handler with the given arguments.
510 template <typename HandlerT, typename... TArgTs>
511 static typename WrappedHandlerReturn<RetT>::Type
512 unpackAndRun(HandlerT &Handler, std::tuple<TArgTs...> &Args) {
513 return unpackAndRunHelper(Handler, Args,
514 llvm::index_sequence_for<TArgTs...>());
515 }
516
517 // Call the given handler with the given arguments.
518 template <typename HandlerT, typename ResponderT, typename... TArgTs>
519 static Error unpackAndRunAsync(HandlerT &Handler, ResponderT &Responder,
520 std::tuple<TArgTs...> &Args) {
521 return unpackAndRunAsyncHelper(Handler, Responder, Args,
522 llvm::index_sequence_for<TArgTs...>());
523 }
524
525 // Call the given handler with the given arguments.
526 template <typename HandlerT>
527 static typename std::enable_if<
528 std::is_void<typename HandlerTraits<HandlerT>::ReturnType>::value,
529 Error>::type
530 run(HandlerT &Handler, ArgTs &&... Args) {
531 Handler(std::move(Args)...);
532 return Error::success();
533 }
534
535 template <typename HandlerT, typename... TArgTs>
536 static typename std::enable_if<
537 !std::is_void<typename HandlerTraits<HandlerT>::ReturnType>::value,
538 typename HandlerTraits<HandlerT>::ReturnType>::type
539 run(HandlerT &Handler, TArgTs... Args) {
540 return Handler(std::move(Args)...);
541 }
542
543 // Serialize arguments to the channel.
544 template <typename ChannelT, typename... CArgTs>
545 static Error serializeArgs(ChannelT &C, const CArgTs... CArgs) {
546 return SequenceSerialization<ChannelT, ArgTs...>::serialize(C, CArgs...);
547 }
548
549 // Deserialize arguments from the channel.
550 template <typename ChannelT, typename... CArgTs>
551 static Error deserializeArgs(ChannelT &C, std::tuple<CArgTs...> &Args) {
552 return deserializeArgsHelper(C, Args,
553 llvm::index_sequence_for<CArgTs...>());
554 }
555
556private:
557 template <typename ChannelT, typename... CArgTs, size_t... Indexes>
558 static Error deserializeArgsHelper(ChannelT &C, std::tuple<CArgTs...> &Args,
559 llvm::index_sequence<Indexes...> _) {
560 return SequenceSerialization<ChannelT, ArgTs...>::deserialize(
561 C, std::get<Indexes>(Args)...);
562 }
563
564 template <typename HandlerT, typename ArgTuple, size_t... Indexes>
565 static typename WrappedHandlerReturn<
566 typename HandlerTraits<HandlerT>::ReturnType>::Type
567 unpackAndRunHelper(HandlerT &Handler, ArgTuple &Args,
568 llvm::index_sequence<Indexes...>) {
569 return run(Handler, std::move(std::get<Indexes>(Args))...);
570 }
571
572
573 template <typename HandlerT, typename ResponderT, typename ArgTuple,
574 size_t... Indexes>
575 static typename WrappedHandlerReturn<
576 typename HandlerTraits<HandlerT>::ReturnType>::Type
577 unpackAndRunAsyncHelper(HandlerT &Handler, ResponderT &Responder,
578 ArgTuple &Args,
579 llvm::index_sequence<Indexes...>) {
580 return run(Handler, Responder, std::move(std::get<Indexes>(Args))...);
581 }
582};
583
584// Handler traits for free functions.
585template <typename RetT, typename... ArgTs>
586class HandlerTraits<RetT(*)(ArgTs...)>
587 : public HandlerTraits<RetT(ArgTs...)> {};
588
589// Handler traits for class methods (especially call operators for lambdas).
590template <typename Class, typename RetT, typename... ArgTs>
591class HandlerTraits<RetT (Class::*)(ArgTs...)>
592 : public HandlerTraits<RetT(ArgTs...)> {};
593
594// Handler traits for const class methods (especially call operators for
595// lambdas).
596template <typename Class, typename RetT, typename... ArgTs>
597class HandlerTraits<RetT (Class::*)(ArgTs...) const>
598 : public HandlerTraits<RetT(ArgTs...)> {};
599
600// Utility to peel the Expected wrapper off a response handler error type.
601template <typename HandlerT> class ResponseHandlerArg;
602
603template <typename ArgT> class ResponseHandlerArg<Error(Expected<ArgT>)> {
604public:
605 using ArgType = Expected<ArgT>;
606 using UnwrappedArgType = ArgT;
607};
608
609template <typename ArgT>
610class ResponseHandlerArg<ErrorSuccess(Expected<ArgT>)> {
611public:
612 using ArgType = Expected<ArgT>;
613 using UnwrappedArgType = ArgT;
614};
615
616template <> class ResponseHandlerArg<Error(Error)> {
617public:
618 using ArgType = Error;
619};
620
621template <> class ResponseHandlerArg<ErrorSuccess(Error)> {
622public:
623 using ArgType = Error;
624};
625
626// ResponseHandler represents a handler for a not-yet-received function call
627// result.
628template <typename ChannelT> class ResponseHandler {
629public:
630 virtual ~ResponseHandler() {}
631
632 // Reads the function result off the wire and acts on it. The meaning of
633 // "act" will depend on how this method is implemented in any given
634 // ResponseHandler subclass but could, for example, mean running a
635 // user-specified handler or setting a promise value.
636 virtual Error handleResponse(ChannelT &C) = 0;
637
638 // Abandons this outstanding result.
639 virtual void abandon() = 0;
640
641 // Create an error instance representing an abandoned response.
642 static Error createAbandonedResponseError() {
643 return make_error<ResponseAbandoned>();
644 }
645};
646
647// ResponseHandler subclass for RPC functions with non-void returns.
648template <typename ChannelT, typename FuncRetT, typename HandlerT>
649class ResponseHandlerImpl : public ResponseHandler<ChannelT> {
650public:
651 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
652
653 // Handle the result by deserializing it from the channel then passing it
654 // to the user defined handler.
655 Error handleResponse(ChannelT &C) override {
656 using UnwrappedArgType = typename ResponseHandlerArg<
657 typename HandlerTraits<HandlerT>::Type>::UnwrappedArgType;
658 UnwrappedArgType Result;
659 if (auto Err =
660 SerializationTraits<ChannelT, FuncRetT,
661 UnwrappedArgType>::deserialize(C, Result))
662 return Err;
663 if (auto Err = C.endReceiveMessage())
664 return Err;
665 return Handler(std::move(Result));
666 }
667
668 // Abandon this response by calling the handler with an 'abandoned response'
669 // error.
670 void abandon() override {
671 if (auto Err = Handler(this->createAbandonedResponseError())) {
672 // Handlers should not fail when passed an abandoned response error.
673 report_fatal_error(std::move(Err));
674 }
675 }
676
677private:
678 HandlerT Handler;
679};
680
681// ResponseHandler subclass for RPC functions with void returns.
682template <typename ChannelT, typename HandlerT>
683class ResponseHandlerImpl<ChannelT, void, HandlerT>
684 : public ResponseHandler<ChannelT> {
685public:
686 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
687
688 // Handle the result (no actual value, just a notification that the function
689 // has completed on the remote end) by calling the user-defined handler with
690 // Error::success().
691 Error handleResponse(ChannelT &C) override {
692 if (auto Err = C.endReceiveMessage())
693 return Err;
694 return Handler(Error::success());
695 }
696
697 // Abandon this response by calling the handler with an 'abandoned response'
698 // error.
699 void abandon() override {
700 if (auto Err = Handler(this->createAbandonedResponseError())) {
701 // Handlers should not fail when passed an abandoned response error.
702 report_fatal_error(std::move(Err));
703 }
704 }
705
706private:
707 HandlerT Handler;
708};
709
710template <typename ChannelT, typename FuncRetT, typename HandlerT>
711class ResponseHandlerImpl<ChannelT, Expected<FuncRetT>, HandlerT>
712 : public ResponseHandler<ChannelT> {
713public:
714 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
715
716 // Handle the result by deserializing it from the channel then passing it
717 // to the user defined handler.
718 Error handleResponse(ChannelT &C) override {
719 using HandlerArgType = typename ResponseHandlerArg<
720 typename HandlerTraits<HandlerT>::Type>::ArgType;
721 HandlerArgType Result((typename HandlerArgType::value_type()));
722
723 if (auto Err =
724 SerializationTraits<ChannelT, Expected<FuncRetT>,
725 HandlerArgType>::deserialize(C, Result))
726 return Err;
727 if (auto Err = C.endReceiveMessage())
728 return Err;
729 return Handler(std::move(Result));
730 }
731
732 // Abandon this response by calling the handler with an 'abandoned response'
733 // error.
734 void abandon() override {
735 if (auto Err = Handler(this->createAbandonedResponseError())) {
736 // Handlers should not fail when passed an abandoned response error.
737 report_fatal_error(std::move(Err));
738 }
739 }
740
741private:
742 HandlerT Handler;
743};
744
745template <typename ChannelT, typename HandlerT>
746class ResponseHandlerImpl<ChannelT, Error, HandlerT>
747 : public ResponseHandler<ChannelT> {
748public:
749 ResponseHandlerImpl(HandlerT Handler) : Handler(std::move(Handler)) {}
750
751 // Handle the result by deserializing it from the channel then passing it
752 // to the user defined handler.
753 Error handleResponse(ChannelT &C) override {
754 Error Result = Error::success();
755 if (auto Err =
756 SerializationTraits<ChannelT, Error, Error>::deserialize(C, Result))
757 return Err;
758 if (auto Err = C.endReceiveMessage())
759 return Err;
760 return Handler(std::move(Result));
761 }
762
763 // Abandon this response by calling the handler with an 'abandoned response'
764 // error.
765 void abandon() override {
766 if (auto Err = Handler(this->createAbandonedResponseError())) {
767 // Handlers should not fail when passed an abandoned response error.
768 report_fatal_error(std::move(Err));
769 }
770 }
771
772private:
773 HandlerT Handler;
774};
775
776// Create a ResponseHandler from a given user handler.
777template <typename ChannelT, typename FuncRetT, typename HandlerT>
778std::unique_ptr<ResponseHandler<ChannelT>> createResponseHandler(HandlerT H) {
779 return llvm::make_unique<ResponseHandlerImpl<ChannelT, FuncRetT, HandlerT>>(
780 std::move(H));
781}
782
783// Helper for wrapping member functions up as functors. This is useful for
784// installing methods as result handlers.
785template <typename ClassT, typename RetT, typename... ArgTs>
786class MemberFnWrapper {
787public:
788 using MethodT = RetT (ClassT::*)(ArgTs...);
789 MemberFnWrapper(ClassT &Instance, MethodT Method)
790 : Instance(Instance), Method(Method) {}
791 RetT operator()(ArgTs &&... Args) {
792 return (Instance.*Method)(std::move(Args)...);
793 }
794
795private:
796 ClassT &Instance;
797 MethodT Method;
798};
799
800// Helper that provides a Functor for deserializing arguments.
801template <typename... ArgTs> class ReadArgs {
802public:
803 Error operator()() { return Error::success(); }
804};
805
806template <typename ArgT, typename... ArgTs>
807class ReadArgs<ArgT, ArgTs...> : public ReadArgs<ArgTs...> {
808public:
809 ReadArgs(ArgT &Arg, ArgTs &... Args)
810 : ReadArgs<ArgTs...>(Args...), Arg(Arg) {}
811
812 Error operator()(ArgT &ArgVal, ArgTs &... ArgVals) {
813 this->Arg = std::move(ArgVal);
814 return ReadArgs<ArgTs...>::operator()(ArgVals...);
815 }
816
817private:
818 ArgT &Arg;
819};
820
821// Manage sequence numbers.
822template <typename SequenceNumberT> class SequenceNumberManager {
823public:
824 // Reset, making all sequence numbers available.
825 void reset() {
826 std::lock_guard<std::mutex> Lock(SeqNoLock);
827 NextSequenceNumber = 0;
828 FreeSequenceNumbers.clear();
829 }
830
831 // Get the next available sequence number. Will re-use numbers that have
832 // been released.
833 SequenceNumberT getSequenceNumber() {
834 std::lock_guard<std::mutex> Lock(SeqNoLock);
835 if (FreeSequenceNumbers.empty())
836 return NextSequenceNumber++;
837 auto SequenceNumber = FreeSequenceNumbers.back();
838 FreeSequenceNumbers.pop_back();
839 return SequenceNumber;
840 }
841
842 // Release a sequence number, making it available for re-use.
843 void releaseSequenceNumber(SequenceNumberT SequenceNumber) {
844 std::lock_guard<std::mutex> Lock(SeqNoLock);
845 FreeSequenceNumbers.push_back(SequenceNumber);
846 }
847
848private:
849 std::mutex SeqNoLock;
850 SequenceNumberT NextSequenceNumber = 0;
851 std::vector<SequenceNumberT> FreeSequenceNumbers;
852};
853
854// Checks that predicate P holds for each corresponding pair of type arguments
855// from T1 and T2 tuple.
856template <template <class, class> class P, typename T1Tuple, typename T2Tuple>
857class RPCArgTypeCheckHelper;
858
859template <template <class, class> class P>
860class RPCArgTypeCheckHelper<P, std::tuple<>, std::tuple<>> {
861public:
862 static const bool value = true;
863};
864
865template <template <class, class> class P, typename T, typename... Ts,
866 typename U, typename... Us>
867class RPCArgTypeCheckHelper<P, std::tuple<T, Ts...>, std::tuple<U, Us...>> {
868public:
869 static const bool value =
870 P<T, U>::value &&
871 RPCArgTypeCheckHelper<P, std::tuple<Ts...>, std::tuple<Us...>>::value;
872};
873
874template <template <class, class> class P, typename T1Sig, typename T2Sig>
875class RPCArgTypeCheck {
876public:
877 using T1Tuple = typename FunctionArgsTuple<T1Sig>::Type;
878 using T2Tuple = typename FunctionArgsTuple<T2Sig>::Type;
879
880 static_assert(std::tuple_size<T1Tuple>::value >=
881 std::tuple_size<T2Tuple>::value,
882 "Too many arguments to RPC call");
883 static_assert(std::tuple_size<T1Tuple>::value <=
884 std::tuple_size<T2Tuple>::value,
885 "Too few arguments to RPC call");
886
887 static const bool value = RPCArgTypeCheckHelper<P, T1Tuple, T2Tuple>::value;
888};
889
890template <typename ChannelT, typename WireT, typename ConcreteT>
891class CanSerialize {
892private:
893 using S = SerializationTraits<ChannelT, WireT, ConcreteT>;
894
895 template <typename T>
896 static std::true_type
897 check(typename std::enable_if<
898 std::is_same<decltype(T::serialize(std::declval<ChannelT &>(),
899 std::declval<const ConcreteT &>())),
900 Error>::value,
901 void *>::type);
902
903 template <typename> static std::false_type check(...);
904
905public:
906 static const bool value = decltype(check<S>(0))::value;
907};
908
909template <typename ChannelT, typename WireT, typename ConcreteT>
910class CanDeserialize {
911private:
912 using S = SerializationTraits<ChannelT, WireT, ConcreteT>;
913
914 template <typename T>
915 static std::true_type
916 check(typename std::enable_if<
917 std::is_same<decltype(T::deserialize(std::declval<ChannelT &>(),
918 std::declval<ConcreteT &>())),
919 Error>::value,
920 void *>::type);
921
922 template <typename> static std::false_type check(...);
923
924public:
925 static const bool value = decltype(check<S>(0))::value;
926};
927
928/// Contains primitive utilities for defining, calling and handling calls to
929/// remote procedures. ChannelT is a bidirectional stream conforming to the
930/// RPCChannel interface (see RPCChannel.h), FunctionIdT is a procedure
931/// identifier type that must be serializable on ChannelT, and SequenceNumberT
932/// is an integral type that will be used to number in-flight function calls.
933///
934/// These utilities support the construction of very primitive RPC utilities.
935/// Their intent is to ensure correct serialization and deserialization of
936/// procedure arguments, and to keep the client and server's view of the API in
937/// sync.
938template <typename ImplT, typename ChannelT, typename FunctionIdT,
939 typename SequenceNumberT>
940class RPCEndpointBase {
941protected:
942 class OrcRPCInvalid : public Function<OrcRPCInvalid, void()> {
943 public:
944 static const char *getName() { return "__orc_rpc$invalid"; }
945 };
946
947 class OrcRPCResponse : public Function<OrcRPCResponse, void()> {
948 public:
949 static const char *getName() { return "__orc_rpc$response"; }
950 };
951
952 class OrcRPCNegotiate
953 : public Function<OrcRPCNegotiate, FunctionIdT(std::string)> {
954 public:
955 static const char *getName() { return "__orc_rpc$negotiate"; }
956 };
957
958 // Helper predicate for testing for the presence of SerializeTraits
959 // serializers.
960 template <typename WireT, typename ConcreteT>
961 class CanSerializeCheck : detail::CanSerialize<ChannelT, WireT, ConcreteT> {
962 public:
963 using detail::CanSerialize<ChannelT, WireT, ConcreteT>::value;
964
965 static_assert(value, "Missing serializer for argument (Can't serialize the "
966 "first template type argument of CanSerializeCheck "
967 "from the second)");
968 };
969
970 // Helper predicate for testing for the presence of SerializeTraits
971 // deserializers.
972 template <typename WireT, typename ConcreteT>
973 class CanDeserializeCheck
974 : detail::CanDeserialize<ChannelT, WireT, ConcreteT> {
975 public:
976 using detail::CanDeserialize<ChannelT, WireT, ConcreteT>::value;
977
978 static_assert(value, "Missing deserializer for argument (Can't deserialize "
979 "the second template type argument of "
980 "CanDeserializeCheck from the first)");
981 };
982
983public:
984 /// Construct an RPC instance on a channel.
985 RPCEndpointBase(ChannelT &C, bool LazyAutoNegotiation)
986 : C(C), LazyAutoNegotiation(LazyAutoNegotiation) {
987 // Hold ResponseId in a special variable, since we expect Response to be
988 // called relatively frequently, and want to avoid the map lookup.
989 ResponseId = FnIdAllocator.getResponseId();
990 RemoteFunctionIds[OrcRPCResponse::getPrototype()] = ResponseId;
991
992 // Register the negotiate function id and handler.
993 auto NegotiateId = FnIdAllocator.getNegotiateId();
994 RemoteFunctionIds[OrcRPCNegotiate::getPrototype()] = NegotiateId;
995 Handlers[NegotiateId] = wrapHandler<OrcRPCNegotiate>(
996 [this](const std::string &Name) { return handleNegotiate(Name); });
997 }
998
999
1000 /// Negotiate a function id for Func with the other end of the channel.
1001 template <typename Func> Error negotiateFunction(bool Retry = false) {
1002 return getRemoteFunctionId<Func>(true, Retry).takeError();
1003 }
1004
1005 /// Append a call Func, does not call send on the channel.
1006 /// The first argument specifies a user-defined handler to be run when the
1007 /// function returns. The handler should take an Expected<Func::ReturnType>,
1008 /// or an Error (if Func::ReturnType is void). The handler will be called
1009 /// with an error if the return value is abandoned due to a channel error.
1010 template <typename Func, typename HandlerT, typename... ArgTs>
1011 Error appendCallAsync(HandlerT Handler, const ArgTs &... Args) {
1012
1013 static_assert(
1014 detail::RPCArgTypeCheck<CanSerializeCheck, typename Func::Type,
1015 void(ArgTs...)>::value,
1016 "");
1017
1018 // Look up the function ID.
1019 FunctionIdT FnId;
1020 if (auto FnIdOrErr = getRemoteFunctionId<Func>(LazyAutoNegotiation, false))
4
Calling 'RPCEndpointBase::getRemoteFunctionId'
1021 FnId = *FnIdOrErr;
1022 else {
1023 // Negotiation failed. Notify the handler then return the negotiate-failed
1024 // error.
1025 cantFail(Handler(make_error<ResponseAbandoned>()));
1026 return FnIdOrErr.takeError();
1027 }
1028
1029 SequenceNumberT SeqNo; // initialized in locked scope below.
1030 {
1031 // Lock the pending responses map and sequence number manager.
1032 std::lock_guard<std::mutex> Lock(ResponsesMutex);
1033
1034 // Allocate a sequence number.
1035 SeqNo = SequenceNumberMgr.getSequenceNumber();
1036 assert(!PendingResponses.count(SeqNo) &&((!PendingResponses.count(SeqNo) && "Sequence number already allocated"
) ? static_cast<void> (0) : __assert_fail ("!PendingResponses.count(SeqNo) && \"Sequence number already allocated\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1037, __PRETTY_FUNCTION__))
1037 "Sequence number already allocated")((!PendingResponses.count(SeqNo) && "Sequence number already allocated"
) ? static_cast<void> (0) : __assert_fail ("!PendingResponses.count(SeqNo) && \"Sequence number already allocated\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1037, __PRETTY_FUNCTION__))
;
1038
1039 // Install the user handler.
1040 PendingResponses[SeqNo] =
1041 detail::createResponseHandler<ChannelT, typename Func::ReturnType>(
1042 std::move(Handler));
1043 }
1044
1045 // Open the function call message.
1046 if (auto Err = C.startSendMessage(FnId, SeqNo)) {
1047 abandonPendingResponses();
1048 return Err;
1049 }
1050
1051 // Serialize the call arguments.
1052 if (auto Err = detail::HandlerTraits<typename Func::Type>::serializeArgs(
1053 C, Args...)) {
1054 abandonPendingResponses();
1055 return Err;
1056 }
1057
1058 // Close the function call messagee.
1059 if (auto Err = C.endSendMessage()) {
1060 abandonPendingResponses();
1061 return Err;
1062 }
1063
1064 return Error::success();
1065 }
1066
1067 Error sendAppendedCalls() { return C.send(); };
1068
1069 template <typename Func, typename HandlerT, typename... ArgTs>
1070 Error callAsync(HandlerT Handler, const ArgTs &... Args) {
1071 if (auto Err = appendCallAsync<Func>(std::move(Handler), Args...))
1072 return Err;
1073 return C.send();
1074 }
1075
1076 /// Handle one incoming call.
1077 Error handleOne() {
1078 FunctionIdT FnId;
1079 SequenceNumberT SeqNo;
1080 if (auto Err = C.startReceiveMessage(FnId, SeqNo)) {
1081 abandonPendingResponses();
1082 return Err;
1083 }
1084 if (FnId == ResponseId)
1085 return handleResponse(SeqNo);
1086 auto I = Handlers.find(FnId);
1087 if (I != Handlers.end())
1088 return I->second(C, SeqNo);
1089
1090 // else: No handler found. Report error to client?
1091 return make_error<BadFunctionCall<FunctionIdT, SequenceNumberT>>(FnId,
1092 SeqNo);
1093 }
1094
1095 /// Helper for handling setter procedures - this method returns a functor that
1096 /// sets the variables referred to by Args... to values deserialized from the
1097 /// channel.
1098 /// E.g.
1099 ///
1100 /// typedef Function<0, bool, int> Func1;
1101 ///
1102 /// ...
1103 /// bool B;
1104 /// int I;
1105 /// if (auto Err = expect<Func1>(Channel, readArgs(B, I)))
1106 /// /* Handle Args */ ;
1107 ///
1108 template <typename... ArgTs>
1109 static detail::ReadArgs<ArgTs...> readArgs(ArgTs &... Args) {
1110 return detail::ReadArgs<ArgTs...>(Args...);
1111 }
1112
1113 /// Abandon all outstanding result handlers.
1114 ///
1115 /// This will call all currently registered result handlers to receive an
1116 /// "abandoned" error as their argument. This is used internally by the RPC
1117 /// in error situations, but can also be called directly by clients who are
1118 /// disconnecting from the remote and don't or can't expect responses to their
1119 /// outstanding calls. (Especially for outstanding blocking calls, calling
1120 /// this function may be necessary to avoid dead threads).
1121 void abandonPendingResponses() {
1122 // Lock the pending responses map and sequence number manager.
1123 std::lock_guard<std::mutex> Lock(ResponsesMutex);
1124
1125 for (auto &KV : PendingResponses)
1126 KV.second->abandon();
1127 PendingResponses.clear();
1128 SequenceNumberMgr.reset();
1129 }
1130
1131 /// Remove the handler for the given function.
1132 /// A handler must currently be registered for this function.
1133 template <typename Func>
1134 void removeHandler() {
1135 auto IdItr = LocalFunctionIds.find(Func::getPrototype());
1136 assert(IdItr != LocalFunctionIds.end() &&((IdItr != LocalFunctionIds.end() && "Function does not have a registered handler"
) ? static_cast<void> (0) : __assert_fail ("IdItr != LocalFunctionIds.end() && \"Function does not have a registered handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1137, __PRETTY_FUNCTION__))
1137 "Function does not have a registered handler")((IdItr != LocalFunctionIds.end() && "Function does not have a registered handler"
) ? static_cast<void> (0) : __assert_fail ("IdItr != LocalFunctionIds.end() && \"Function does not have a registered handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1137, __PRETTY_FUNCTION__))
;
1138 auto HandlerItr = Handlers.find(IdItr->second);
1139 assert(HandlerItr != Handlers.end() &&((HandlerItr != Handlers.end() && "Function does not have a registered handler"
) ? static_cast<void> (0) : __assert_fail ("HandlerItr != Handlers.end() && \"Function does not have a registered handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1140, __PRETTY_FUNCTION__))
1140 "Function does not have a registered handler")((HandlerItr != Handlers.end() && "Function does not have a registered handler"
) ? static_cast<void> (0) : __assert_fail ("HandlerItr != Handlers.end() && \"Function does not have a registered handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1140, __PRETTY_FUNCTION__))
;
1141 Handlers.erase(HandlerItr);
1142 }
1143
1144 /// Clear all handlers.
1145 void clearHandlers() {
1146 Handlers.clear();
1147 }
1148
1149protected:
1150
1151 FunctionIdT getInvalidFunctionId() const {
1152 return FnIdAllocator.getInvalidId();
1153 }
1154
1155 /// Add the given handler to the handler map and make it available for
1156 /// autonegotiation and execution.
1157 template <typename Func, typename HandlerT>
1158 void addHandlerImpl(HandlerT Handler) {
1159
1160 static_assert(detail::RPCArgTypeCheck<
1161 CanDeserializeCheck, typename Func::Type,
1162 typename detail::HandlerTraits<HandlerT>::Type>::value,
1163 "");
1164
1165 FunctionIdT NewFnId = FnIdAllocator.template allocate<Func>();
1166 LocalFunctionIds[Func::getPrototype()] = NewFnId;
1167 Handlers[NewFnId] = wrapHandler<Func>(std::move(Handler));
1168 }
1169
1170 template <typename Func, typename HandlerT>
1171 void addAsyncHandlerImpl(HandlerT Handler) {
1172
1173 static_assert(detail::RPCArgTypeCheck<
1174 CanDeserializeCheck, typename Func::Type,
1175 typename detail::AsyncHandlerTraits<
1176 typename detail::HandlerTraits<HandlerT>::Type
1177 >::Type>::value,
1178 "");
1179
1180 FunctionIdT NewFnId = FnIdAllocator.template allocate<Func>();
1181 LocalFunctionIds[Func::getPrototype()] = NewFnId;
1182 Handlers[NewFnId] = wrapAsyncHandler<Func>(std::move(Handler));
1183 }
1184
1185 Error handleResponse(SequenceNumberT SeqNo) {
1186 using Handler = typename decltype(PendingResponses)::mapped_type;
1187 Handler PRHandler;
1188
1189 {
1190 // Lock the pending responses map and sequence number manager.
1191 std::unique_lock<std::mutex> Lock(ResponsesMutex);
1192 auto I = PendingResponses.find(SeqNo);
1193
1194 if (I != PendingResponses.end()) {
1195 PRHandler = std::move(I->second);
1196 PendingResponses.erase(I);
1197 SequenceNumberMgr.releaseSequenceNumber(SeqNo);
1198 } else {
1199 // Unlock the pending results map to prevent recursive lock.
1200 Lock.unlock();
1201 abandonPendingResponses();
1202 return make_error<
1203 InvalidSequenceNumberForResponse<SequenceNumberT>>(SeqNo);
1204 }
1205 }
1206
1207 assert(PRHandler &&((PRHandler && "If we didn't find a response handler we should have bailed out"
) ? static_cast<void> (0) : __assert_fail ("PRHandler && \"If we didn't find a response handler we should have bailed out\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1208, __PRETTY_FUNCTION__))
1208 "If we didn't find a response handler we should have bailed out")((PRHandler && "If we didn't find a response handler we should have bailed out"
) ? static_cast<void> (0) : __assert_fail ("PRHandler && \"If we didn't find a response handler we should have bailed out\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ExecutionEngine/Orc/RPCUtils.h"
, 1208, __PRETTY_FUNCTION__))
;
1209
1210 if (auto Err = PRHandler->handleResponse(C)) {
1211 abandonPendingResponses();
1212 return Err;
1213 }
1214
1215 return Error::success();
1216 }
1217
1218 FunctionIdT handleNegotiate(const std::string &Name) {
1219 auto I = LocalFunctionIds.find(Name);
1220 if (I == LocalFunctionIds.end())
1221 return getInvalidFunctionId();
1222 return I->second;
1223 }
1224
1225 // Find the remote FunctionId for the given function.
1226 template <typename Func>
1227 Expected<FunctionIdT> getRemoteFunctionId(bool NegotiateIfNotInMap,
1228 bool NegotiateIfInvalid) {
1229 bool DoNegotiate;
1230
1231 // Check if we already have a function id...
1232 auto I = RemoteFunctionIds.find(Func::getPrototype());
1233 if (I != RemoteFunctionIds.end()) {
5
Assuming the condition is false
6
Taking false branch
1234 // If it's valid there's nothing left to do.
1235 if (I->second != getInvalidFunctionId())
1236 return I->second;
1237 DoNegotiate = NegotiateIfInvalid;
1238 } else
1239 DoNegotiate = NegotiateIfNotInMap;
1240
1241 // We don't have a function id for Func yet, but we're allowed to try to
1242 // negotiate one.
1243 if (DoNegotiate) {
7
Assuming 'DoNegotiate' is 0
8
Taking false branch
1244 auto &Impl = static_cast<ImplT &>(*this);
1245 if (auto RemoteIdOrErr =
1246 Impl.template callB<OrcRPCNegotiate>(Func::getPrototype())) {
1247 RemoteFunctionIds[Func::getPrototype()] = *RemoteIdOrErr;
1248 if (*RemoteIdOrErr == getInvalidFunctionId())
1249 return make_error<CouldNotNegotiate>(Func::getPrototype());
1250 return *RemoteIdOrErr;
1251 } else
1252 return RemoteIdOrErr.takeError();
1253 }
1254
1255 // No key was available in the map and we weren't allowed to try to
1256 // negotiate one, so return an unknown function error.
1257 return make_error<CouldNotNegotiate>(Func::getPrototype());
9
Calling 'make_error<llvm::orc::rpc::CouldNotNegotiate, const char *>'
1258 }
1259
1260 using WrappedHandlerFn = std::function<Error(ChannelT &, SequenceNumberT)>;
1261
1262 // Wrap the given user handler in the necessary argument-deserialization code,
1263 // result-serialization code, and call to the launch policy (if present).
1264 template <typename Func, typename HandlerT>
1265 WrappedHandlerFn wrapHandler(HandlerT Handler) {
1266 return [this, Handler](ChannelT &Channel,
1267 SequenceNumberT SeqNo) mutable -> Error {
1268 // Start by deserializing the arguments.
1269 using ArgsTuple =
1270 typename detail::FunctionArgsTuple<
1271 typename detail::HandlerTraits<HandlerT>::Type>::Type;
1272 auto Args = std::make_shared<ArgsTuple>();
1273
1274 if (auto Err =
1275 detail::HandlerTraits<typename Func::Type>::deserializeArgs(
1276 Channel, *Args))
1277 return Err;
1278
1279 // GCC 4.7 and 4.8 incorrectly issue a -Wunused-but-set-variable warning
1280 // for RPCArgs. Void cast RPCArgs to work around this for now.
1281 // FIXME: Remove this workaround once we can assume a working GCC version.
1282 (void)Args;
1283
1284 // End receieve message, unlocking the channel for reading.
1285 if (auto Err = Channel.endReceiveMessage())
1286 return Err;
1287
1288 using HTraits = detail::HandlerTraits<HandlerT>;
1289 using FuncReturn = typename Func::ReturnType;
1290 return detail::respond<FuncReturn>(Channel, ResponseId, SeqNo,
1291 HTraits::unpackAndRun(Handler, *Args));
1292 };
1293 }
1294
1295 // Wrap the given user handler in the necessary argument-deserialization code,
1296 // result-serialization code, and call to the launch policy (if present).
1297 template <typename Func, typename HandlerT>
1298 WrappedHandlerFn wrapAsyncHandler(HandlerT Handler) {
1299 return [this, Handler](ChannelT &Channel,
1300 SequenceNumberT SeqNo) mutable -> Error {
1301 // Start by deserializing the arguments.
1302 using AHTraits = detail::AsyncHandlerTraits<
1303 typename detail::HandlerTraits<HandlerT>::Type>;
1304 using ArgsTuple =
1305 typename detail::FunctionArgsTuple<typename AHTraits::Type>::Type;
1306 auto Args = std::make_shared<ArgsTuple>();
1307
1308 if (auto Err =
1309 detail::HandlerTraits<typename Func::Type>::deserializeArgs(
1310 Channel, *Args))
1311 return Err;
1312
1313 // GCC 4.7 and 4.8 incorrectly issue a -Wunused-but-set-variable warning
1314 // for RPCArgs. Void cast RPCArgs to work around this for now.
1315 // FIXME: Remove this workaround once we can assume a working GCC version.
1316 (void)Args;
1317
1318 // End receieve message, unlocking the channel for reading.
1319 if (auto Err = Channel.endReceiveMessage())
1320 return Err;
1321
1322 using HTraits = detail::HandlerTraits<HandlerT>;
1323 using FuncReturn = typename Func::ReturnType;
1324 auto Responder =
1325 [this, SeqNo](typename AHTraits::ResultType RetVal) -> Error {
1326 return detail::respond<FuncReturn>(C, ResponseId, SeqNo,
1327 std::move(RetVal));
1328 };
1329
1330 return HTraits::unpackAndRunAsync(Handler, Responder, *Args);
1331 };
1332 }
1333
1334 ChannelT &C;
1335
1336 bool LazyAutoNegotiation;
1337
1338 RPCFunctionIdAllocator<FunctionIdT> FnIdAllocator;
1339
1340 FunctionIdT ResponseId;
1341 std::map<std::string, FunctionIdT> LocalFunctionIds;
1342 std::map<const char *, FunctionIdT> RemoteFunctionIds;
1343
1344 std::map<FunctionIdT, WrappedHandlerFn> Handlers;
1345
1346 std::mutex ResponsesMutex;
1347 detail::SequenceNumberManager<SequenceNumberT> SequenceNumberMgr;
1348 std::map<SequenceNumberT, std::unique_ptr<detail::ResponseHandler<ChannelT>>>
1349 PendingResponses;
1350};
1351
1352} // end namespace detail
1353
1354template <typename ChannelT, typename FunctionIdT = uint32_t,
1355 typename SequenceNumberT = uint32_t>
1356class MultiThreadedRPCEndpoint
1357 : public detail::RPCEndpointBase<
1358 MultiThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1359 ChannelT, FunctionIdT, SequenceNumberT> {
1360private:
1361 using BaseClass =
1362 detail::RPCEndpointBase<
1363 MultiThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1364 ChannelT, FunctionIdT, SequenceNumberT>;
1365
1366public:
1367 MultiThreadedRPCEndpoint(ChannelT &C, bool LazyAutoNegotiation)
1368 : BaseClass(C, LazyAutoNegotiation) {}
1369
1370 /// Add a handler for the given RPC function.
1371 /// This installs the given handler functor for the given RPC Function, and
1372 /// makes the RPC function available for negotiation/calling from the remote.
1373 template <typename Func, typename HandlerT>
1374 void addHandler(HandlerT Handler) {
1375 return this->template addHandlerImpl<Func>(std::move(Handler));
1376 }
1377
1378 /// Add a class-method as a handler.
1379 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1380 void addHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1381 addHandler<Func>(
1382 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1383 }
1384
1385 template <typename Func, typename HandlerT>
1386 void addAsyncHandler(HandlerT Handler) {
1387 return this->template addAsyncHandlerImpl<Func>(std::move(Handler));
1388 }
1389
1390 /// Add a class-method as a handler.
1391 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1392 void addAsyncHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1393 addAsyncHandler<Func>(
1394 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1395 }
1396
1397 /// Return type for non-blocking call primitives.
1398 template <typename Func>
1399 using NonBlockingCallResult = typename detail::ResultTraits<
1400 typename Func::ReturnType>::ReturnFutureType;
1401
1402 /// Call Func on Channel C. Does not block, does not call send. Returns a pair
1403 /// of a future result and the sequence number assigned to the result.
1404 ///
1405 /// This utility function is primarily used for single-threaded mode support,
1406 /// where the sequence number can be used to wait for the corresponding
1407 /// result. In multi-threaded mode the appendCallNB method, which does not
1408 /// return the sequence numeber, should be preferred.
1409 template <typename Func, typename... ArgTs>
1410 Expected<NonBlockingCallResult<Func>> appendCallNB(const ArgTs &... Args) {
1411 using RTraits = detail::ResultTraits<typename Func::ReturnType>;
1412 using ErrorReturn = typename RTraits::ErrorReturnType;
1413 using ErrorReturnPromise = typename RTraits::ReturnPromiseType;
1414
1415 // FIXME: Stack allocate and move this into the handler once LLVM builds
1416 // with C++14.
1417 auto Promise = std::make_shared<ErrorReturnPromise>();
1418 auto FutureResult = Promise->get_future();
1419
1420 if (auto Err = this->template appendCallAsync<Func>(
1421 [Promise](ErrorReturn RetOrErr) {
1422 Promise->set_value(std::move(RetOrErr));
1423 return Error::success();
1424 },
1425 Args...)) {
1426 RTraits::consumeAbandoned(FutureResult.get());
1427 return std::move(Err);
1428 }
1429 return std::move(FutureResult);
1430 }
1431
1432 /// The same as appendCallNBWithSeq, except that it calls C.send() to
1433 /// flush the channel after serializing the call.
1434 template <typename Func, typename... ArgTs>
1435 Expected<NonBlockingCallResult<Func>> callNB(const ArgTs &... Args) {
1436 auto Result = appendCallNB<Func>(Args...);
1437 if (!Result)
1438 return Result;
1439 if (auto Err = this->C.send()) {
1440 this->abandonPendingResponses();
1441 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1442 std::move(Result->get()));
1443 return std::move(Err);
1444 }
1445 return Result;
1446 }
1447
1448 /// Call Func on Channel C. Blocks waiting for a result. Returns an Error
1449 /// for void functions or an Expected<T> for functions returning a T.
1450 ///
1451 /// This function is for use in threaded code where another thread is
1452 /// handling responses and incoming calls.
1453 template <typename Func, typename... ArgTs,
1454 typename AltRetT = typename Func::ReturnType>
1455 typename detail::ResultTraits<AltRetT>::ErrorReturnType
1456 callB(const ArgTs &... Args) {
1457 if (auto FutureResOrErr = callNB<Func>(Args...))
1458 return FutureResOrErr->get();
1459 else
1460 return FutureResOrErr.takeError();
1461 }
1462
1463 /// Handle incoming RPC calls.
1464 Error handlerLoop() {
1465 while (true)
1466 if (auto Err = this->handleOne())
1467 return Err;
1468 return Error::success();
1469 }
1470};
1471
1472template <typename ChannelT, typename FunctionIdT = uint32_t,
1473 typename SequenceNumberT = uint32_t>
1474class SingleThreadedRPCEndpoint
1475 : public detail::RPCEndpointBase<
1476 SingleThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1477 ChannelT, FunctionIdT, SequenceNumberT> {
1478private:
1479 using BaseClass =
1480 detail::RPCEndpointBase<
1481 SingleThreadedRPCEndpoint<ChannelT, FunctionIdT, SequenceNumberT>,
1482 ChannelT, FunctionIdT, SequenceNumberT>;
1483
1484public:
1485 SingleThreadedRPCEndpoint(ChannelT &C, bool LazyAutoNegotiation)
1486 : BaseClass(C, LazyAutoNegotiation) {}
1487
1488 template <typename Func, typename HandlerT>
1489 void addHandler(HandlerT Handler) {
1490 return this->template addHandlerImpl<Func>(std::move(Handler));
1491 }
1492
1493 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1494 void addHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1495 addHandler<Func>(
1496 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1497 }
1498
1499 template <typename Func, typename HandlerT>
1500 void addAsyncHandler(HandlerT Handler) {
1501 return this->template addAsyncHandlerImpl<Func>(std::move(Handler));
1502 }
1503
1504 /// Add a class-method as a handler.
1505 template <typename Func, typename ClassT, typename RetT, typename... ArgTs>
1506 void addAsyncHandler(ClassT &Object, RetT (ClassT::*Method)(ArgTs...)) {
1507 addAsyncHandler<Func>(
1508 detail::MemberFnWrapper<ClassT, RetT, ArgTs...>(Object, Method));
1509 }
1510
1511 template <typename Func, typename... ArgTs,
1512 typename AltRetT = typename Func::ReturnType>
1513 typename detail::ResultTraits<AltRetT>::ErrorReturnType
1514 callB(const ArgTs &... Args) {
1515 bool ReceivedResponse = false;
1516 using ResultType = typename detail::ResultTraits<AltRetT>::ErrorReturnType;
1517 auto Result = detail::ResultTraits<AltRetT>::createBlankErrorReturnValue();
1518
1519 // We have to 'Check' result (which we know is in a success state at this
1520 // point) so that it can be overwritten in the async handler.
1521 (void)!!Result;
1522
1523 if (auto Err = this->template appendCallAsync<Func>(
3
Calling 'RPCEndpointBase::appendCallAsync'
1524 [&](ResultType R) {
1525 Result = std::move(R);
1526 ReceivedResponse = true;
1527 return Error::success();
1528 },
1529 Args...)) {
1530 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1531 std::move(Result));
1532 return std::move(Err);
1533 }
1534
1535 while (!ReceivedResponse) {
1536 if (auto Err = this->handleOne()) {
1537 detail::ResultTraits<typename Func::ReturnType>::consumeAbandoned(
1538 std::move(Result));
1539 return std::move(Err);
1540 }
1541 }
1542
1543 return Result;
1544 }
1545};
1546
1547/// Asynchronous dispatch for a function on an RPC endpoint.
1548template <typename RPCClass, typename Func>
1549class RPCAsyncDispatch {
1550public:
1551 RPCAsyncDispatch(RPCClass &Endpoint) : Endpoint(Endpoint) {}
1552
1553 template <typename HandlerT, typename... ArgTs>
1554 Error operator()(HandlerT Handler, const ArgTs &... Args) const {
1555 return Endpoint.template appendCallAsync<Func>(std::move(Handler), Args...);
1556 }
1557
1558private:
1559 RPCClass &Endpoint;
1560};
1561
1562/// Construct an asynchronous dispatcher from an RPC endpoint and a Func.
1563template <typename Func, typename RPCEndpointT>
1564RPCAsyncDispatch<RPCEndpointT, Func> rpcAsyncDispatch(RPCEndpointT &Endpoint) {
1565 return RPCAsyncDispatch<RPCEndpointT, Func>(Endpoint);
1566}
1567
1568/// Allows a set of asynchrounous calls to be dispatched, and then
1569/// waited on as a group.
1570class ParallelCallGroup {
1571public:
1572
1573 ParallelCallGroup() = default;
1574 ParallelCallGroup(const ParallelCallGroup &) = delete;
1575 ParallelCallGroup &operator=(const ParallelCallGroup &) = delete;
1576
1577 /// Make as asynchronous call.
1578 template <typename AsyncDispatcher, typename HandlerT, typename... ArgTs>
1579 Error call(const AsyncDispatcher &AsyncDispatch, HandlerT Handler,
1580 const ArgTs &... Args) {
1581 // Increment the count of outstanding calls. This has to happen before
1582 // we invoke the call, as the handler may (depending on scheduling)
1583 // be run immediately on another thread, and we don't want the decrement
1584 // in the wrapped handler below to run before the increment.
1585 {
1586 std::unique_lock<std::mutex> Lock(M);
1587 ++NumOutstandingCalls;
1588 }
1589
1590 // Wrap the user handler in a lambda that will decrement the
1591 // outstanding calls count, then poke the condition variable.
1592 using ArgType = typename detail::ResponseHandlerArg<
1593 typename detail::HandlerTraits<HandlerT>::Type>::ArgType;
1594 // FIXME: Move handler into wrapped handler once we have C++14.
1595 auto WrappedHandler = [this, Handler](ArgType Arg) {
1596 auto Err = Handler(std::move(Arg));
1597 std::unique_lock<std::mutex> Lock(M);
1598 --NumOutstandingCalls;
1599 CV.notify_all();
1600 return Err;
1601 };
1602
1603 return AsyncDispatch(std::move(WrappedHandler), Args...);
1604 }
1605
1606 /// Blocks until all calls have been completed and their return value
1607 /// handlers run.
1608 void wait() {
1609 std::unique_lock<std::mutex> Lock(M);
1610 while (NumOutstandingCalls > 0)
1611 CV.wait(Lock);
1612 }
1613
1614private:
1615 std::mutex M;
1616 std::condition_variable CV;
1617 uint32_t NumOutstandingCalls = 0;
1618};
1619
1620/// Convenience class for grouping RPC Functions into APIs that can be
1621/// negotiated as a block.
1622///
1623template <typename... Funcs>
1624class APICalls {
1625public:
1626
1627 /// Test whether this API contains Function F.
1628 template <typename F>
1629 class Contains {
1630 public:
1631 static const bool value = false;
1632 };
1633
1634 /// Negotiate all functions in this API.
1635 template <typename RPCEndpoint>
1636 static Error negotiate(RPCEndpoint &R) {
1637 return Error::success();
1638 }
1639};
1640
1641template <typename Func, typename... Funcs>
1642class APICalls<Func, Funcs...> {
1643public:
1644
1645 template <typename F>
1646 class Contains {
1647 public:
1648 static const bool value = std::is_same<F, Func>::value |
1649 APICalls<Funcs...>::template Contains<F>::value;
1650 };
1651
1652 template <typename RPCEndpoint>
1653 static Error negotiate(RPCEndpoint &R) {
1654 if (auto Err = R.template negotiateFunction<Func>())
1655 return Err;
1656 return APICalls<Funcs...>::negotiate(R);
1657 }
1658
1659};
1660
1661template <typename... InnerFuncs, typename... Funcs>
1662class APICalls<APICalls<InnerFuncs...>, Funcs...> {
1663public:
1664
1665 template <typename F>
1666 class Contains {
1667 public:
1668 static const bool value =
1669 APICalls<InnerFuncs...>::template Contains<F>::value |
1670 APICalls<Funcs...>::template Contains<F>::value;
1671 };
1672
1673 template <typename RPCEndpoint>
1674 static Error negotiate(RPCEndpoint &R) {
1675 if (auto Err = APICalls<InnerFuncs...>::negotiate(R))
1676 return Err;
1677 return APICalls<Funcs...>::negotiate(R);
1678 }
1679
1680};
1681
1682} // end namespace rpc
1683} // end namespace orc
1684} // end namespace llvm
1685
1686#endif

/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h

1//===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an API used to report recoverable errors.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_ERROR_H
15#define LLVM_SUPPORT_ERROR_H
16
17#include "llvm-c/Error.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/Twine.h"
22#include "llvm/Config/abi-breaking.h"
23#include "llvm/Support/AlignOf.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/ErrorOr.h"
28#include "llvm/Support/Format.h"
29#include "llvm/Support/raw_ostream.h"
30#include <algorithm>
31#include <cassert>
32#include <cstdint>
33#include <cstdlib>
34#include <functional>
35#include <memory>
36#include <new>
37#include <string>
38#include <system_error>
39#include <type_traits>
40#include <utility>
41#include <vector>
42
43namespace llvm {
44
45class ErrorSuccess;
46
47/// Base class for error info classes. Do not extend this directly: Extend
48/// the ErrorInfo template subclass instead.
49class ErrorInfoBase {
50public:
51 virtual ~ErrorInfoBase() = default;
52
53 /// Print an error message to an output stream.
54 virtual void log(raw_ostream &OS) const = 0;
55
56 /// Return the error message as a string.
57 virtual std::string message() const {
58 std::string Msg;
59 raw_string_ostream OS(Msg);
60 log(OS);
61 return OS.str();
62 }
63
64 /// Convert this error to a std::error_code.
65 ///
66 /// This is a temporary crutch to enable interaction with code still
67 /// using std::error_code. It will be removed in the future.
68 virtual std::error_code convertToErrorCode() const = 0;
69
70 // Returns the class ID for this type.
71 static const void *classID() { return &ID; }
72
73 // Returns the class ID for the dynamic type of this ErrorInfoBase instance.
74 virtual const void *dynamicClassID() const = 0;
75
76 // Check whether this instance is a subclass of the class identified by
77 // ClassID.
78 virtual bool isA(const void *const ClassID) const {
79 return ClassID == classID();
80 }
81
82 // Check whether this instance is a subclass of ErrorInfoT.
83 template <typename ErrorInfoT> bool isA() const {
84 return isA(ErrorInfoT::classID());
85 }
86
87private:
88 virtual void anchor();
89
90 static char ID;
91};
92
93/// Lightweight error class with error context and mandatory checking.
94///
95/// Instances of this class wrap a ErrorInfoBase pointer. Failure states
96/// are represented by setting the pointer to a ErrorInfoBase subclass
97/// instance containing information describing the failure. Success is
98/// represented by a null pointer value.
99///
100/// Instances of Error also contains a 'Checked' flag, which must be set
101/// before the destructor is called, otherwise the destructor will trigger a
102/// runtime error. This enforces at runtime the requirement that all Error
103/// instances be checked or returned to the caller.
104///
105/// There are two ways to set the checked flag, depending on what state the
106/// Error instance is in. For Error instances indicating success, it
107/// is sufficient to invoke the boolean conversion operator. E.g.:
108///
109/// @code{.cpp}
110/// Error foo(<...>);
111///
112/// if (auto E = foo(<...>))
113/// return E; // <- Return E if it is in the error state.
114/// // We have verified that E was in the success state. It can now be safely
115/// // destroyed.
116/// @endcode
117///
118/// A success value *can not* be dropped. For example, just calling 'foo(<...>)'
119/// without testing the return value will raise a runtime error, even if foo
120/// returns success.
121///
122/// For Error instances representing failure, you must use either the
123/// handleErrors or handleAllErrors function with a typed handler. E.g.:
124///
125/// @code{.cpp}
126/// class MyErrorInfo : public ErrorInfo<MyErrorInfo> {
127/// // Custom error info.
128/// };
129///
130/// Error foo(<...>) { return make_error<MyErrorInfo>(...); }
131///
132/// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo.
133/// auto NewE =
134/// handleErrors(E,
135/// [](const MyErrorInfo &M) {
136/// // Deal with the error.
137/// },
138/// [](std::unique_ptr<OtherError> M) -> Error {
139/// if (canHandle(*M)) {
140/// // handle error.
141/// return Error::success();
142/// }
143/// // Couldn't handle this error instance. Pass it up the stack.
144/// return Error(std::move(M));
145/// );
146/// // Note - we must check or return NewE in case any of the handlers
147/// // returned a new error.
148/// @endcode
149///
150/// The handleAllErrors function is identical to handleErrors, except
151/// that it has a void return type, and requires all errors to be handled and
152/// no new errors be returned. It prevents errors (assuming they can all be
153/// handled) from having to be bubbled all the way to the top-level.
154///
155/// *All* Error instances must be checked before destruction, even if
156/// they're moved-assigned or constructed from Success values that have already
157/// been checked. This enforces checking through all levels of the call stack.
158class LLVM_NODISCARD[[clang::warn_unused_result]] Error {
159 // Both ErrorList and FileError need to be able to yank ErrorInfoBase
160 // pointers out of this class to add to the error list.
161 friend class ErrorList;
162 friend class FileError;
163
164 // handleErrors needs to be able to set the Checked flag.
165 template <typename... HandlerTs>
166 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
167
168 // Expected<T> needs to be able to steal the payload when constructed from an
169 // error.
170 template <typename T> friend class Expected;
171
172 // wrap needs to be able to steal the payload.
173 friend LLVMErrorRef wrap(Error);
174
175protected:
176 /// Create a success value. Prefer using 'Error::success()' for readability
177 Error() {
178 setPtr(nullptr);
179 setChecked(false);
180 }
181
182public:
183 /// Create a success value.
184 static ErrorSuccess success();
185
186 // Errors are not copy-constructable.
187 Error(const Error &Other) = delete;
188
189 /// Move-construct an error value. The newly constructed error is considered
190 /// unchecked, even if the source error had been checked. The original error
191 /// becomes a checked Success value, regardless of its original state.
192 Error(Error &&Other) {
193 setChecked(true);
194 *this = std::move(Other);
195 }
196
197 /// Create an error value. Prefer using the 'make_error' function, but
198 /// this constructor can be useful when "re-throwing" errors from handlers.
199 Error(std::unique_ptr<ErrorInfoBase> Payload) {
200 setPtr(Payload.release());
201 setChecked(false);
14
Potential leak of memory pointed to by 'Payload._M_t._M_head_impl'
202 }
203
204 // Errors are not copy-assignable.
205 Error &operator=(const Error &Other) = delete;
206
207 /// Move-assign an error value. The current error must represent success, you
208 /// you cannot overwrite an unhandled error. The current error is then
209 /// considered unchecked. The source error becomes a checked success value,
210 /// regardless of its original state.
211 Error &operator=(Error &&Other) {
212 // Don't allow overwriting of unchecked values.
213 assertIsChecked();
214 setPtr(Other.getPtr());
215
216 // This Error is unchecked, even if the source error was checked.
217 setChecked(false);
218
219 // Null out Other's payload and set its checked bit.
220 Other.setPtr(nullptr);
221 Other.setChecked(true);
222
223 return *this;
224 }
225
226 /// Destroy a Error. Fails with a call to abort() if the error is
227 /// unchecked.
228 ~Error() {
229 assertIsChecked();
230 delete getPtr();
231 }
232
233 /// Bool conversion. Returns true if this Error is in a failure state,
234 /// and false if it is in an accept state. If the error is in a Success state
235 /// it will be considered checked.
236 explicit operator bool() {
237 setChecked(getPtr() == nullptr);
238 return getPtr() != nullptr;
239 }
240
241 /// Check whether one error is a subclass of another.
242 template <typename ErrT> bool isA() const {
243 return getPtr() && getPtr()->isA(ErrT::classID());
244 }
245
246 /// Returns the dynamic class id of this error, or null if this is a success
247 /// value.
248 const void* dynamicClassID() const {
249 if (!getPtr())
250 return nullptr;
251 return getPtr()->dynamicClassID();
252 }
253
254private:
255#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
256 // assertIsChecked() happens very frequently, but under normal circumstances
257 // is supposed to be a no-op. So we want it to be inlined, but having a bunch
258 // of debug prints can cause the function to be too large for inlining. So
259 // it's important that we define this function out of line so that it can't be
260 // inlined.
261 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
262 void fatalUncheckedError() const;
263#endif
264
265 void assertIsChecked() {
266#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
267 if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false))
268 fatalUncheckedError();
269#endif
270 }
271
272 ErrorInfoBase *getPtr() const {
273 return reinterpret_cast<ErrorInfoBase*>(
274 reinterpret_cast<uintptr_t>(Payload) &
275 ~static_cast<uintptr_t>(0x1));
276 }
277
278 void setPtr(ErrorInfoBase *EI) {
279#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
280 Payload = reinterpret_cast<ErrorInfoBase*>(
281 (reinterpret_cast<uintptr_t>(EI) &
282 ~static_cast<uintptr_t>(0x1)) |
283 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
284#else
285 Payload = EI;
286#endif
287 }
288
289 bool getChecked() const {
290#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
291 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
292#else
293 return true;
294#endif
295 }
296
297 void setChecked(bool V) {
298 Payload = reinterpret_cast<ErrorInfoBase*>(
299 (reinterpret_cast<uintptr_t>(Payload) &
300 ~static_cast<uintptr_t>(0x1)) |
301 (V ? 0 : 1));
302 }
303
304 std::unique_ptr<ErrorInfoBase> takePayload() {
305 std::unique_ptr<ErrorInfoBase> Tmp(getPtr());
306 setPtr(nullptr);
307 setChecked(true);
308 return Tmp;
309 }
310
311 friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) {
312 if (auto P = E.getPtr())
313 P->log(OS);
314 else
315 OS << "success";
316 return OS;
317 }
318
319 ErrorInfoBase *Payload = nullptr;
320};
321
322/// Subclass of Error for the sole purpose of identifying the success path in
323/// the type system. This allows to catch invalid conversion to Expected<T> at
324/// compile time.
325class ErrorSuccess final : public Error {};
326
327inline ErrorSuccess Error::success() { return ErrorSuccess(); }
328
329/// Make a Error instance representing failure using the given error info
330/// type.
331template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) {
332 return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
10
Calling 'make_unique<llvm::orc::rpc::CouldNotNegotiate, const char *>'
12
Returned allocated memory
13
Calling constructor for 'Error'
333}
334
335/// Base class for user error types. Users should declare their error types
336/// like:
337///
338/// class MyError : public ErrorInfo<MyError> {
339/// ....
340/// };
341///
342/// This class provides an implementation of the ErrorInfoBase::kind
343/// method, which is used by the Error RTTI system.
344template <typename ThisErrT, typename ParentErrT = ErrorInfoBase>
345class ErrorInfo : public ParentErrT {
346public:
347 using ParentErrT::ParentErrT; // inherit constructors
348
349 static const void *classID() { return &ThisErrT::ID; }
350
351 const void *dynamicClassID() const override { return &ThisErrT::ID; }
352
353 bool isA(const void *const ClassID) const override {
354 return ClassID == classID() || ParentErrT::isA(ClassID);
355 }
356};
357
358/// Special ErrorInfo subclass representing a list of ErrorInfos.
359/// Instances of this class are constructed by joinError.
360class ErrorList final : public ErrorInfo<ErrorList> {
361 // handleErrors needs to be able to iterate the payload list of an
362 // ErrorList.
363 template <typename... HandlerTs>
364 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
365
366 // joinErrors is implemented in terms of join.
367 friend Error joinErrors(Error, Error);
368
369public:
370 void log(raw_ostream &OS) const override {
371 OS << "Multiple errors:\n";
372 for (auto &ErrPayload : Payloads) {
373 ErrPayload->log(OS);
374 OS << "\n";
375 }
376 }
377
378 std::error_code convertToErrorCode() const override;
379
380 // Used by ErrorInfo::classID.
381 static char ID;
382
383private:
384 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
385 std::unique_ptr<ErrorInfoBase> Payload2) {
386 assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((!Payload1->isA<ErrorList>() && !Payload2->
isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors"
) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 387, __PRETTY_FUNCTION__))
387 "ErrorList constructor payloads should be singleton errors")((!Payload1->isA<ErrorList>() && !Payload2->
isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors"
) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 387, __PRETTY_FUNCTION__))
;
388 Payloads.push_back(std::move(Payload1));
389 Payloads.push_back(std::move(Payload2));
390 }
391
392 static Error join(Error E1, Error E2) {
393 if (!E1)
394 return E2;
395 if (!E2)
396 return E1;
397 if (E1.isA<ErrorList>()) {
398 auto &E1List = static_cast<ErrorList &>(*E1.getPtr());
399 if (E2.isA<ErrorList>()) {
400 auto E2Payload = E2.takePayload();
401 auto &E2List = static_cast<ErrorList &>(*E2Payload);
402 for (auto &Payload : E2List.Payloads)
403 E1List.Payloads.push_back(std::move(Payload));
404 } else
405 E1List.Payloads.push_back(E2.takePayload());
406
407 return E1;
408 }
409 if (E2.isA<ErrorList>()) {
410 auto &E2List = static_cast<ErrorList &>(*E2.getPtr());
411 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
412 return E2;
413 }
414 return Error(std::unique_ptr<ErrorList>(
415 new ErrorList(E1.takePayload(), E2.takePayload())));
416 }
417
418 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
419};
420
421/// Concatenate errors. The resulting Error is unchecked, and contains the
422/// ErrorInfo(s), if any, contained in E1, followed by the
423/// ErrorInfo(s), if any, contained in E2.
424inline Error joinErrors(Error E1, Error E2) {
425 return ErrorList::join(std::move(E1), std::move(E2));
426}
427
428/// Tagged union holding either a T or a Error.
429///
430/// This class parallels ErrorOr, but replaces error_code with Error. Since
431/// Error cannot be copied, this class replaces getError() with
432/// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the
433/// error class type.
434template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected {
435 template <class T1> friend class ExpectedAsOutParameter;
436 template <class OtherT> friend class Expected;
437
438 static const bool isRef = std::is_reference<T>::value;
439
440 using wrap = std::reference_wrapper<typename std::remove_reference<T>::type>;
441
442 using error_type = std::unique_ptr<ErrorInfoBase>;
443
444public:
445 using storage_type = typename std::conditional<isRef, wrap, T>::type;
446 using value_type = T;
447
448private:
449 using reference = typename std::remove_reference<T>::type &;
450 using const_reference = const typename std::remove_reference<T>::type &;
451 using pointer = typename std::remove_reference<T>::type *;
452 using const_pointer = const typename std::remove_reference<T>::type *;
453
454public:
455 /// Create an Expected<T> error value from the given Error.
456 Expected(Error Err)
457 : HasError(true)
458#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
459 // Expected is unchecked upon construction in Debug builds.
460 , Unchecked(true)
461#endif
462 {
463 assert(Err && "Cannot create Expected<T> from Error success value.")((Err && "Cannot create Expected<T> from Error success value."
) ? static_cast<void> (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 463, __PRETTY_FUNCTION__))
;
464 new (getErrorStorage()) error_type(Err.takePayload());
465 }
466
467 /// Forbid to convert from Error::success() implicitly, this avoids having
468 /// Expected<T> foo() { return Error::success(); } which compiles otherwise
469 /// but triggers the assertion above.
470 Expected(ErrorSuccess) = delete;
471
472 /// Create an Expected<T> success value from the given OtherT value, which
473 /// must be convertible to T.
474 template <typename OtherT>
475 Expected(OtherT &&Val,
476 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
477 * = nullptr)
478 : HasError(false)
479#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
480 // Expected is unchecked upon construction in Debug builds.
481 , Unchecked(true)
482#endif
483 {
484 new (getStorage()) storage_type(std::forward<OtherT>(Val));
485 }
486
487 /// Move construct an Expected<T> value.
488 Expected(Expected &&Other) { moveConstruct(std::move(Other)); }
489
490 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
491 /// must be convertible to T.
492 template <class OtherT>
493 Expected(Expected<OtherT> &&Other,
494 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
495 * = nullptr) {
496 moveConstruct(std::move(Other));
497 }
498
499 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
500 /// isn't convertible to T.
501 template <class OtherT>
502 explicit Expected(
503 Expected<OtherT> &&Other,
504 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
505 nullptr) {
506 moveConstruct(std::move(Other));
507 }
508
509 /// Move-assign from another Expected<T>.
510 Expected &operator=(Expected &&Other) {
511 moveAssign(std::move(Other));
512 return *this;
513 }
514
515 /// Destroy an Expected<T>.
516 ~Expected() {
517 assertIsChecked();
518 if (!HasError)
519 getStorage()->~storage_type();
520 else
521 getErrorStorage()->~error_type();
522 }
523
524 /// Return false if there is an error.
525 explicit operator bool() {
526#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
527 Unchecked = HasError;
528#endif
529 return !HasError;
530 }
531
532 /// Returns a reference to the stored T value.
533 reference get() {
534 assertIsChecked();
535 return *getStorage();
536 }
537
538 /// Returns a const reference to the stored T value.
539 const_reference get() const {
540 assertIsChecked();
541 return const_cast<Expected<T> *>(this)->get();
542 }
543
544 /// Check that this Expected<T> is an error of type ErrT.
545 template <typename ErrT> bool errorIsA() const {
546 return HasError && (*getErrorStorage())->template isA<ErrT>();
547 }
548
549 /// Take ownership of the stored error.
550 /// After calling this the Expected<T> is in an indeterminate state that can
551 /// only be safely destructed. No further calls (beside the destructor) should
552 /// be made on the Expected<T> vaule.
553 Error takeError() {
554#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
555 Unchecked = false;
556#endif
557 return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
558 }
559
560 /// Returns a pointer to the stored T value.
561 pointer operator->() {
562 assertIsChecked();
563 return toPointer(getStorage());
564 }
565
566 /// Returns a const pointer to the stored T value.
567 const_pointer operator->() const {
568 assertIsChecked();
569 return toPointer(getStorage());
570 }
571
572 /// Returns a reference to the stored T value.
573 reference operator*() {
574 assertIsChecked();
575 return *getStorage();
576 }
577
578 /// Returns a const reference to the stored T value.
579 const_reference operator*() const {
580 assertIsChecked();
581 return *getStorage();
582 }
583
584private:
585 template <class T1>
586 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
587 return &a == &b;
588 }
589
590 template <class T1, class T2>
591 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
592 return false;
593 }
594
595 template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) {
596 HasError = Other.HasError;
597#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
598 Unchecked = true;
599 Other.Unchecked = false;
600#endif
601
602 if (!HasError)
603 new (getStorage()) storage_type(std::move(*Other.getStorage()));
604 else
605 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
606 }
607
608 template <class OtherT> void moveAssign(Expected<OtherT> &&Other) {
609 assertIsChecked();
610
611 if (compareThisIfSameType(*this, Other))
612 return;
613
614 this->~Expected();
615 new (this) Expected(std::move(Other));
616 }
617
618 pointer toPointer(pointer Val) { return Val; }
619
620 const_pointer toPointer(const_pointer Val) const { return Val; }
621
622 pointer toPointer(wrap *Val) { return &Val->get(); }
623
624 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
625
626 storage_type *getStorage() {
627 assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!"
) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 627, __PRETTY_FUNCTION__))
;
628 return reinterpret_cast<storage_type *>(TStorage.buffer);
629 }
630
631 const storage_type *getStorage() const {
632 assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!"
) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 632, __PRETTY_FUNCTION__))
;
633 return reinterpret_cast<const storage_type *>(TStorage.buffer);
634 }
635
636 error_type *getErrorStorage() {
637 assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!"
) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 637, __PRETTY_FUNCTION__))
;
638 return reinterpret_cast<error_type *>(ErrorStorage.buffer);
639 }
640
641 const error_type *getErrorStorage() const {
642 assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!"
) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 642, __PRETTY_FUNCTION__))
;
643 return reinterpret_cast<const error_type *>(ErrorStorage.buffer);
644 }
645
646 // Used by ExpectedAsOutParameter to reset the checked flag.
647 void setUnchecked() {
648#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
649 Unchecked = true;
650#endif
651 }
652
653#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
654 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
655 LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline))
656 void fatalUncheckedExpected() const {
657 dbgs() << "Expected<T> must be checked before access or destruction.\n";
658 if (HasError) {
659 dbgs() << "Unchecked Expected<T> contained error:\n";
660 (*getErrorStorage())->log(dbgs());
661 } else
662 dbgs() << "Expected<T> value was in success state. (Note: Expected<T> "
663 "values in success mode must still be checked prior to being "
664 "destroyed).\n";
665 abort();
666 }
667#endif
668
669 void assertIsChecked() {
670#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
671 if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false))
672 fatalUncheckedExpected();
673#endif
674 }
675
676 union {
677 AlignedCharArrayUnion<storage_type> TStorage;
678 AlignedCharArrayUnion<error_type> ErrorStorage;
679 };
680 bool HasError : 1;
681#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
682 bool Unchecked : 1;
683#endif
684};
685
686/// Report a serious error, calling any installed error handler. See
687/// ErrorHandling.h.
688LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err,
689 bool gen_crash_diag = true);
690
691/// Report a fatal error if Err is a failure value.
692///
693/// This function can be used to wrap calls to fallible functions ONLY when it
694/// is known that the Error will always be a success value. E.g.
695///
696/// @code{.cpp}
697/// // foo only attempts the fallible operation if DoFallibleOperation is
698/// // true. If DoFallibleOperation is false then foo always returns
699/// // Error::success().
700/// Error foo(bool DoFallibleOperation);
701///
702/// cantFail(foo(false));
703/// @endcode
704inline void cantFail(Error Err, const char *Msg = nullptr) {
705 if (Err) {
706 if (!Msg)
707 Msg = "Failure value returned from cantFail wrapped call";
708 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 708)
;
709 }
710}
711
712/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
713/// returns the contained value.
714///
715/// This function can be used to wrap calls to fallible functions ONLY when it
716/// is known that the Error will always be a success value. E.g.
717///
718/// @code{.cpp}
719/// // foo only attempts the fallible operation if DoFallibleOperation is
720/// // true. If DoFallibleOperation is false then foo always returns an int.
721/// Expected<int> foo(bool DoFallibleOperation);
722///
723/// int X = cantFail(foo(false));
724/// @endcode
725template <typename T>
726T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
727 if (ValOrErr)
728 return std::move(*ValOrErr);
729 else {
730 if (!Msg)
731 Msg = "Failure value returned from cantFail wrapped call";
732 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 732)
;
733 }
734}
735
736/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
737/// returns the contained reference.
738///
739/// This function can be used to wrap calls to fallible functions ONLY when it
740/// is known that the Error will always be a success value. E.g.
741///
742/// @code{.cpp}
743/// // foo only attempts the fallible operation if DoFallibleOperation is
744/// // true. If DoFallibleOperation is false then foo always returns a Bar&.
745/// Expected<Bar&> foo(bool DoFallibleOperation);
746///
747/// Bar &X = cantFail(foo(false));
748/// @endcode
749template <typename T>
750T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
751 if (ValOrErr)
752 return *ValOrErr;
753 else {
754 if (!Msg)
755 Msg = "Failure value returned from cantFail wrapped call";
756 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 756)
;
757 }
758}
759
760/// Helper for testing applicability of, and applying, handlers for
761/// ErrorInfo types.
762template <typename HandlerT>
763class ErrorHandlerTraits
764 : public ErrorHandlerTraits<decltype(
765 &std::remove_reference<HandlerT>::type::operator())> {};
766
767// Specialization functions of the form 'Error (const ErrT&)'.
768template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> {
769public:
770 static bool appliesTo(const ErrorInfoBase &E) {
771 return E.template isA<ErrT>();
772 }
773
774 template <typename HandlerT>
775 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
776 assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast
<void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 776, __PRETTY_FUNCTION__))
;
777 return H(static_cast<ErrT &>(*E));
778 }
779};
780
781// Specialization functions of the form 'void (const ErrT&)'.
782template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> {
783public:
784 static bool appliesTo(const ErrorInfoBase &E) {
785 return E.template isA<ErrT>();
786 }
787
788 template <typename HandlerT>
789 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
790 assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast
<void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 790, __PRETTY_FUNCTION__))
;
791 H(static_cast<ErrT &>(*E));
792 return Error::success();
793 }
794};
795
796/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
797template <typename ErrT>
798class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> {
799public:
800 static bool appliesTo(const ErrorInfoBase &E) {
801 return E.template isA<ErrT>();
802 }
803
804 template <typename HandlerT>
805 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
806 assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast
<void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 806, __PRETTY_FUNCTION__))
;
807 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
808 return H(std::move(SubE));
809 }
810};
811
812/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
813template <typename ErrT>
814class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> {
815public:
816 static bool appliesTo(const ErrorInfoBase &E) {
817 return E.template isA<ErrT>();
818 }
819
820 template <typename HandlerT>
821 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
822 assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast
<void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 822, __PRETTY_FUNCTION__))
;
823 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
824 H(std::move(SubE));
825 return Error::success();
826 }
827};
828
829// Specialization for member functions of the form 'RetT (const ErrT&)'.
830template <typename C, typename RetT, typename ErrT>
831class ErrorHandlerTraits<RetT (C::*)(ErrT &)>
832 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
833
834// Specialization for member functions of the form 'RetT (const ErrT&) const'.
835template <typename C, typename RetT, typename ErrT>
836class ErrorHandlerTraits<RetT (C::*)(ErrT &) const>
837 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
838
839// Specialization for member functions of the form 'RetT (const ErrT&)'.
840template <typename C, typename RetT, typename ErrT>
841class ErrorHandlerTraits<RetT (C::*)(const ErrT &)>
842 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
843
844// Specialization for member functions of the form 'RetT (const ErrT&) const'.
845template <typename C, typename RetT, typename ErrT>
846class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const>
847 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
848
849/// Specialization for member functions of the form
850/// 'RetT (std::unique_ptr<ErrT>)'.
851template <typename C, typename RetT, typename ErrT>
852class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
853 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
854
855/// Specialization for member functions of the form
856/// 'RetT (std::unique_ptr<ErrT>) const'.
857template <typename C, typename RetT, typename ErrT>
858class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const>
859 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
860
861inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) {
862 return Error(std::move(Payload));
863}
864
865template <typename HandlerT, typename... HandlerTs>
866Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload,
867 HandlerT &&Handler, HandlerTs &&... Handlers) {
868 if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload))
869 return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler),
870 std::move(Payload));
871 return handleErrorImpl(std::move(Payload),
872 std::forward<HandlerTs>(Handlers)...);
873}
874
875/// Pass the ErrorInfo(s) contained in E to their respective handlers. Any
876/// unhandled errors (or Errors returned by handlers) are re-concatenated and
877/// returned.
878/// Because this function returns an error, its result must also be checked
879/// or returned. If you intend to handle all errors use handleAllErrors
880/// (which returns void, and will abort() on unhandled errors) instead.
881template <typename... HandlerTs>
882Error handleErrors(Error E, HandlerTs &&... Hs) {
883 if (!E)
884 return Error::success();
885
886 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
887
888 if (Payload->isA<ErrorList>()) {
889 ErrorList &List = static_cast<ErrorList &>(*Payload);
890 Error R;
891 for (auto &P : List.Payloads)
892 R = ErrorList::join(
893 std::move(R),
894 handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...));
895 return R;
896 }
897
898 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
899}
900
901/// Behaves the same as handleErrors, except that by contract all errors
902/// *must* be handled by the given handlers (i.e. there must be no remaining
903/// errors after running the handlers, or llvm_unreachable is called).
904template <typename... HandlerTs>
905void handleAllErrors(Error E, HandlerTs &&... Handlers) {
906 cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...));
907}
908
909/// Check that E is a non-error, then drop it.
910/// If E is an error, llvm_unreachable will be called.
911inline void handleAllErrors(Error E) {
912 cantFail(std::move(E));
913}
914
915/// Handle any errors (if present) in an Expected<T>, then try a recovery path.
916///
917/// If the incoming value is a success value it is returned unmodified. If it
918/// is a failure value then it the contained error is passed to handleErrors.
919/// If handleErrors is able to handle the error then the RecoveryPath functor
920/// is called to supply the final result. If handleErrors is not able to
921/// handle all errors then the unhandled errors are returned.
922///
923/// This utility enables the follow pattern:
924///
925/// @code{.cpp}
926/// enum FooStrategy { Aggressive, Conservative };
927/// Expected<Foo> foo(FooStrategy S);
928///
929/// auto ResultOrErr =
930/// handleExpected(
931/// foo(Aggressive),
932/// []() { return foo(Conservative); },
933/// [](AggressiveStrategyError&) {
934/// // Implicitly conusme this - we'll recover by using a conservative
935/// // strategy.
936/// });
937///
938/// @endcode
939template <typename T, typename RecoveryFtor, typename... HandlerTs>
940Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath,
941 HandlerTs &&... Handlers) {
942 if (ValOrErr)
943 return ValOrErr;
944
945 if (auto Err = handleErrors(ValOrErr.takeError(),
946 std::forward<HandlerTs>(Handlers)...))
947 return std::move(Err);
948
949 return RecoveryPath();
950}
951
952/// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner
953/// will be printed before the first one is logged. A newline will be printed
954/// after each error.
955///
956/// This is useful in the base level of your program to allow clean termination
957/// (allowing clean deallocation of resources, etc.), while reporting error
958/// information to the user.
959void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner);
960
961/// Write all error messages (if any) in E to a string. The newline character
962/// is used to separate error messages.
963inline std::string toString(Error E) {
964 SmallVector<std::string, 2> Errors;
965 handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) {
966 Errors.push_back(EI.message());
967 });
968 return join(Errors.begin(), Errors.end(), "\n");
969}
970
971/// Consume a Error without doing anything. This method should be used
972/// only where an error can be considered a reasonable and expected return
973/// value.
974///
975/// Uses of this method are potentially indicative of design problems: If it's
976/// legitimate to do nothing while processing an "error", the error-producer
977/// might be more clearly refactored to return an Optional<T>.
978inline void consumeError(Error Err) {
979 handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {});
980}
981
982/// Helper for converting an Error to a bool.
983///
984/// This method returns true if Err is in an error state, or false if it is
985/// in a success state. Puts Err in a checked state in both cases (unlike
986/// Error::operator bool(), which only does this for success states).
987inline bool errorToBool(Error Err) {
988 bool IsError = static_cast<bool>(Err);
989 if (IsError)
990 consumeError(std::move(Err));
991 return IsError;
992}
993
994/// Helper for Errors used as out-parameters.
995///
996/// This helper is for use with the Error-as-out-parameter idiom, where an error
997/// is passed to a function or method by reference, rather than being returned.
998/// In such cases it is helpful to set the checked bit on entry to the function
999/// so that the error can be written to (unchecked Errors abort on assignment)
1000/// and clear the checked bit on exit so that clients cannot accidentally forget
1001/// to check the result. This helper performs these actions automatically using
1002/// RAII:
1003///
1004/// @code{.cpp}
1005/// Result foo(Error &Err) {
1006/// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set
1007/// // <body of foo>
1008/// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed.
1009/// }
1010/// @endcode
1011///
1012/// ErrorAsOutParameter takes an Error* rather than Error& so that it can be
1013/// used with optional Errors (Error pointers that are allowed to be null). If
1014/// ErrorAsOutParameter took an Error reference, an instance would have to be
1015/// created inside every condition that verified that Error was non-null. By
1016/// taking an Error pointer we can just create one instance at the top of the
1017/// function.
1018class ErrorAsOutParameter {
1019public:
1020 ErrorAsOutParameter(Error *Err) : Err(Err) {
1021 // Raise the checked bit if Err is success.
1022 if (Err)
1023 (void)!!*Err;
1024 }
1025
1026 ~ErrorAsOutParameter() {
1027 // Clear the checked bit.
1028 if (Err && !*Err)
1029 *Err = Error::success();
1030 }
1031
1032private:
1033 Error *Err;
1034};
1035
1036/// Helper for Expected<T>s used as out-parameters.
1037///
1038/// See ErrorAsOutParameter.
1039template <typename T>
1040class ExpectedAsOutParameter {
1041public:
1042 ExpectedAsOutParameter(Expected<T> *ValOrErr)
1043 : ValOrErr(ValOrErr) {
1044 if (ValOrErr)
1045 (void)!!*ValOrErr;
1046 }
1047
1048 ~ExpectedAsOutParameter() {
1049 if (ValOrErr)
1050 ValOrErr->setUnchecked();
1051 }
1052
1053private:
1054 Expected<T> *ValOrErr;
1055};
1056
1057/// This class wraps a std::error_code in a Error.
1058///
1059/// This is useful if you're writing an interface that returns a Error
1060/// (or Expected) and you want to call code that still returns
1061/// std::error_codes.
1062class ECError : public ErrorInfo<ECError> {
1063 friend Error errorCodeToError(std::error_code);
1064
1065public:
1066 void setErrorCode(std::error_code EC) { this->EC = EC; }
1067 std::error_code convertToErrorCode() const override { return EC; }
1068 void log(raw_ostream &OS) const override { OS << EC.message(); }
1069
1070 // Used by ErrorInfo::classID.
1071 static char ID;
1072
1073protected:
1074 ECError() = default;
1075 ECError(std::error_code EC) : EC(EC) {}
1076
1077 std::error_code EC;
1078};
1079
1080/// The value returned by this function can be returned from convertToErrorCode
1081/// for Error values where no sensible translation to std::error_code exists.
1082/// It should only be used in this situation, and should never be used where a
1083/// sensible conversion to std::error_code is available, as attempts to convert
1084/// to/from this error will result in a fatal error. (i.e. it is a programmatic
1085///error to try to convert such a value).
1086std::error_code inconvertibleErrorCode();
1087
1088/// Helper for converting an std::error_code to a Error.
1089Error errorCodeToError(std::error_code EC);
1090
1091/// Helper for converting an ECError to a std::error_code.
1092///
1093/// This method requires that Err be Error() or an ECError, otherwise it
1094/// will trigger a call to abort().
1095std::error_code errorToErrorCode(Error Err);
1096
1097/// Convert an ErrorOr<T> to an Expected<T>.
1098template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) {
1099 if (auto EC = EO.getError())
1100 return errorCodeToError(EC);
1101 return std::move(*EO);
1102}
1103
1104/// Convert an Expected<T> to an ErrorOr<T>.
1105template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) {
1106 if (auto Err = E.takeError())
1107 return errorToErrorCode(std::move(Err));
1108 return std::move(*E);
1109}
1110
1111/// This class wraps a string in an Error.
1112///
1113/// StringError is useful in cases where the client is not expected to be able
1114/// to consume the specific error message programmatically (for example, if the
1115/// error message is to be presented to the user).
1116///
1117/// StringError can also be used when additional information is to be printed
1118/// along with a error_code message. Depending on the constructor called, this
1119/// class can either display:
1120/// 1. the error_code message (ECError behavior)
1121/// 2. a string
1122/// 3. the error_code message and a string
1123///
1124/// These behaviors are useful when subtyping is required; for example, when a
1125/// specific library needs an explicit error type. In the example below,
1126/// PDBError is derived from StringError:
1127///
1128/// @code{.cpp}
1129/// Expected<int> foo() {
1130/// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading,
1131/// "Additional information");
1132/// }
1133/// @endcode
1134///
1135class StringError : public ErrorInfo<StringError> {
1136public:
1137 static char ID;
1138
1139 // Prints EC + S and converts to EC
1140 StringError(std::error_code EC, const Twine &S = Twine());
1141
1142 // Prints S and converts to EC
1143 StringError(const Twine &S, std::error_code EC);
1144
1145 void log(raw_ostream &OS) const override;
1146 std::error_code convertToErrorCode() const override;
1147
1148 const std::string &getMessage() const { return Msg; }
1149
1150private:
1151 std::string Msg;
1152 std::error_code EC;
1153 const bool PrintMsgOnly = false;
1154};
1155
1156/// Create formatted StringError object.
1157template <typename... Ts>
1158Error createStringError(std::error_code EC, char const *Fmt,
1159 const Ts &... Vals) {
1160 std::string Buffer;
1161 raw_string_ostream Stream(Buffer);
1162 Stream << format(Fmt, Vals...);
1163 return make_error<StringError>(Stream.str(), EC);
1164}
1165
1166Error createStringError(std::error_code EC, char const *Msg);
1167
1168/// This class wraps a filename and another Error.
1169///
1170/// In some cases, an error needs to live along a 'source' name, in order to
1171/// show more detailed information to the user.
1172class FileError final : public ErrorInfo<FileError> {
1173
1174 friend Error createFileError(std::string, Error);
1175
1176public:
1177 void log(raw_ostream &OS) const override {
1178 assert(Err && !FileName.empty() && "Trying to log after takeError().")((Err && !FileName.empty() && "Trying to log after takeError()."
) ? static_cast<void> (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 1178, __PRETTY_FUNCTION__))
;
1179 OS << "'" << FileName << "': ";
1180 Err->log(OS);
1181 }
1182
1183 Error takeError() { return Error(std::move(Err)); }
1184
1185 std::error_code convertToErrorCode() const override;
1186
1187 // Used by ErrorInfo::classID.
1188 static char ID;
1189
1190private:
1191 FileError(std::string F, std::unique_ptr<ErrorInfoBase> E) {
1192 assert(E && "Cannot create FileError from Error success value.")((E && "Cannot create FileError from Error success value."
) ? static_cast<void> (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 1192, __PRETTY_FUNCTION__))
;
1193 assert(!F.empty() &&((!F.empty() && "The file name provided to FileError must not be empty."
) ? static_cast<void> (0) : __assert_fail ("!F.empty() && \"The file name provided to FileError must not be empty.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 1194, __PRETTY_FUNCTION__))
1194 "The file name provided to FileError must not be empty.")((!F.empty() && "The file name provided to FileError must not be empty."
) ? static_cast<void> (0) : __assert_fail ("!F.empty() && \"The file name provided to FileError must not be empty.\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Support/Error.h"
, 1194, __PRETTY_FUNCTION__))
;
1195 FileName = F;
1196 Err = std::move(E);
1197 }
1198
1199 static Error build(std::string F, Error E) {
1200 return Error(std::unique_ptr<FileError>(new FileError(F, E.takePayload())));
1201 }
1202
1203 std::string FileName;
1204 std::unique_ptr<ErrorInfoBase> Err;
1205};
1206
1207/// Concatenate a source file path and/or name with an Error. The resulting
1208/// Error is unchecked.
1209inline Error createFileError(std::string F, Error E) {
1210 return FileError::build(F, std::move(E));
1211}
1212
1213Error createFileError(std::string F, ErrorSuccess) = delete;
1214
1215/// Helper for check-and-exit error handling.
1216///
1217/// For tool use only. NOT FOR USE IN LIBRARY CODE.
1218///
1219class ExitOnError {
1220public:
1221 /// Create an error on exit helper.
1222 ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1)
1223 : Banner(std::move(Banner)),
1224 GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {}
1225
1226 /// Set the banner string for any errors caught by operator().
1227 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1228
1229 /// Set the exit-code mapper function.
1230 void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) {
1231 this->GetExitCode = std::move(GetExitCode);
1232 }
1233
1234 /// Check Err. If it's in a failure state log the error(s) and exit.
1235 void operator()(Error Err) const { checkError(std::move(Err)); }
1236
1237 /// Check E. If it's in a success state then return the contained value. If
1238 /// it's in a failure state log the error(s) and exit.
1239 template <typename T> T operator()(Expected<T> &&E) const {
1240 checkError(E.takeError());
1241 return std::move(*E);
1242 }
1243
1244 /// Check E. If it's in a success state then return the contained reference. If
1245 /// it's in a failure state log the error(s) and exit.
1246 template <typename T> T& operator()(Expected<T&> &&E) const {
1247 checkError(E.takeError());
1248 return *E;
1249 }
1250
1251private:
1252 void checkError(Error Err) const {
1253 if (Err) {
1254 int ExitCode = GetExitCode(Err);
1255 logAllUnhandledErrors(std::move(Err), errs(), Banner);
1256 exit(ExitCode);
1257 }
1258 }
1259
1260 std::string Banner;
1261 std::function<int(const Error &)> GetExitCode;
1262};
1263
1264/// Conversion from Error to LLVMErrorRef for C error bindings.
1265inline LLVMErrorRef wrap(Error Err) {
1266 return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release());
1267}
1268
1269/// Conversion from LLVMErrorRef to Error for C error bindings.
1270inline Error unwrap(LLVMErrorRef ErrRef) {
1271 return Error(std::unique_ptr<ErrorInfoBase>(
1272 reinterpret_cast<ErrorInfoBase *>(ErrRef)));
1273}
1274
1275} // end namespace llvm
1276
1277#endif // LLVM_SUPPORT_ERROR_H

/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h

1//===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains some templates that are useful if you are working with the
11// STL at all.
12//
13// No library is required when using these functions.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ADT_STLEXTRAS_H
18#define LLVM_ADT_STLEXTRAS_H
19
20#include "llvm/ADT/Optional.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/iterator.h"
23#include "llvm/ADT/iterator_range.h"
24#include "llvm/Config/abi-breaking.h"
25#include "llvm/Support/ErrorHandling.h"
26#include <algorithm>
27#include <cassert>
28#include <cstddef>
29#include <cstdint>
30#include <cstdlib>
31#include <functional>
32#include <initializer_list>
33#include <iterator>
34#include <limits>
35#include <memory>
36#include <tuple>
37#include <type_traits>
38#include <utility>
39
40#ifdef EXPENSIVE_CHECKS
41#include <random> // for std::mt19937
42#endif
43
44namespace llvm {
45
46// Only used by compiler if both template types are the same. Useful when
47// using SFINAE to test for the existence of member functions.
48template <typename T, T> struct SameType;
49
50namespace detail {
51
52template <typename RangeT>
53using IterOfRange = decltype(std::begin(std::declval<RangeT &>()));
54
55template <typename RangeT>
56using ValueOfRange = typename std::remove_reference<decltype(
57 *std::begin(std::declval<RangeT &>()))>::type;
58
59} // end namespace detail
60
61//===----------------------------------------------------------------------===//
62// Extra additions to <type_traits>
63//===----------------------------------------------------------------------===//
64
65template <typename T>
66struct negation : std::integral_constant<bool, !bool(T::value)> {};
67
68template <typename...> struct conjunction : std::true_type {};
69template <typename B1> struct conjunction<B1> : B1 {};
70template <typename B1, typename... Bn>
71struct conjunction<B1, Bn...>
72 : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
73
74//===----------------------------------------------------------------------===//
75// Extra additions to <functional>
76//===----------------------------------------------------------------------===//
77
78template <class Ty> struct identity {
79 using argument_type = Ty;
80
81 Ty &operator()(Ty &self) const {
82 return self;
83 }
84 const Ty &operator()(const Ty &self) const {
85 return self;
86 }
87};
88
89template <class Ty> struct less_ptr {
90 bool operator()(const Ty* left, const Ty* right) const {
91 return *left < *right;
92 }
93};
94
95template <class Ty> struct greater_ptr {
96 bool operator()(const Ty* left, const Ty* right) const {
97 return *right < *left;
98 }
99};
100
101/// An efficient, type-erasing, non-owning reference to a callable. This is
102/// intended for use as the type of a function parameter that is not used
103/// after the function in question returns.
104///
105/// This class does not own the callable, so it is not in general safe to store
106/// a function_ref.
107template<typename Fn> class function_ref;
108
109template<typename Ret, typename ...Params>
110class function_ref<Ret(Params...)> {
111 Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
112 intptr_t callable;
113
114 template<typename Callable>
115 static Ret callback_fn(intptr_t callable, Params ...params) {
116 return (*reinterpret_cast<Callable*>(callable))(
117 std::forward<Params>(params)...);
118 }
119
120public:
121 function_ref() = default;
122 function_ref(std::nullptr_t) {}
123
124 template <typename Callable>
125 function_ref(Callable &&callable,
126 typename std::enable_if<
127 !std::is_same<typename std::remove_reference<Callable>::type,
128 function_ref>::value>::type * = nullptr)
129 : callback(callback_fn<typename std::remove_reference<Callable>::type>),
130 callable(reinterpret_cast<intptr_t>(&callable)) {}
131
132 Ret operator()(Params ...params) const {
133 return callback(callable, std::forward<Params>(params)...);
134 }
135
136 operator bool() const { return callback; }
137};
138
139// deleter - Very very very simple method that is used to invoke operator
140// delete on something. It is used like this:
141//
142// for_each(V.begin(), B.end(), deleter<Interval>);
143template <class T>
144inline void deleter(T *Ptr) {
145 delete Ptr;
146}
147
148//===----------------------------------------------------------------------===//
149// Extra additions to <iterator>
150//===----------------------------------------------------------------------===//
151
152namespace adl_detail {
153
154using std::begin;
155
156template <typename ContainerTy>
157auto adl_begin(ContainerTy &&container)
158 -> decltype(begin(std::forward<ContainerTy>(container))) {
159 return begin(std::forward<ContainerTy>(container));
160}
161
162using std::end;
163
164template <typename ContainerTy>
165auto adl_end(ContainerTy &&container)
166 -> decltype(end(std::forward<ContainerTy>(container))) {
167 return end(std::forward<ContainerTy>(container));
168}
169
170using std::swap;
171
172template <typename T>
173void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval<T>(),
174 std::declval<T>()))) {
175 swap(std::forward<T>(lhs), std::forward<T>(rhs));
176}
177
178} // end namespace adl_detail
179
180template <typename ContainerTy>
181auto adl_begin(ContainerTy &&container)
182 -> decltype(adl_detail::adl_begin(std::forward<ContainerTy>(container))) {
183 return adl_detail::adl_begin(std::forward<ContainerTy>(container));
184}
185
186template <typename ContainerTy>
187auto adl_end(ContainerTy &&container)
188 -> decltype(adl_detail::adl_end(std::forward<ContainerTy>(container))) {
189 return adl_detail::adl_end(std::forward<ContainerTy>(container));
190}
191
192template <typename T>
193void adl_swap(T &&lhs, T &&rhs) noexcept(
194 noexcept(adl_detail::adl_swap(std::declval<T>(), std::declval<T>()))) {
195 adl_detail::adl_swap(std::forward<T>(lhs), std::forward<T>(rhs));
196}
197
198// mapped_iterator - This is a simple iterator adapter that causes a function to
199// be applied whenever operator* is invoked on the iterator.
200
201template <typename ItTy, typename FuncTy,
202 typename FuncReturnTy =
203 decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))>
204class mapped_iterator
205 : public iterator_adaptor_base<
206 mapped_iterator<ItTy, FuncTy>, ItTy,
207 typename std::iterator_traits<ItTy>::iterator_category,
208 typename std::remove_reference<FuncReturnTy>::type> {
209public:
210 mapped_iterator(ItTy U, FuncTy F)
211 : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
212
213 ItTy getCurrent() { return this->I; }
214
215 FuncReturnTy operator*() { return F(*this->I); }
216
217private:
218 FuncTy F;
219};
220
221// map_iterator - Provide a convenient way to create mapped_iterators, just like
222// make_pair is useful for creating pairs...
223template <class ItTy, class FuncTy>
224inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) {
225 return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F));
226}
227
228/// Helper to determine if type T has a member called rbegin().
229template <typename Ty> class has_rbegin_impl {
230 using yes = char[1];
231 using no = char[2];
232
233 template <typename Inner>
234 static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr);
235
236 template <typename>
237 static no& test(...);
238
239public:
240 static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes);
241};
242
243/// Metafunction to determine if T& or T has a member called rbegin().
244template <typename Ty>
245struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> {
246};
247
248// Returns an iterator_range over the given container which iterates in reverse.
249// Note that the container must have rbegin()/rend() methods for this to work.
250template <typename ContainerTy>
251auto reverse(ContainerTy &&C,
252 typename std::enable_if<has_rbegin<ContainerTy>::value>::type * =
253 nullptr) -> decltype(make_range(C.rbegin(), C.rend())) {
254 return make_range(C.rbegin(), C.rend());
255}
256
257// Returns a std::reverse_iterator wrapped around the given iterator.
258template <typename IteratorTy>
259std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) {
260 return std::reverse_iterator<IteratorTy>(It);
261}
262
263// Returns an iterator_range over the given container which iterates in reverse.
264// Note that the container must have begin()/end() methods which return
265// bidirectional iterators for this to work.
266template <typename ContainerTy>
267auto reverse(
268 ContainerTy &&C,
269 typename std::enable_if<!has_rbegin<ContainerTy>::value>::type * = nullptr)
270 -> decltype(make_range(llvm::make_reverse_iterator(std::end(C)),
271 llvm::make_reverse_iterator(std::begin(C)))) {
272 return make_range(llvm::make_reverse_iterator(std::end(C)),
273 llvm::make_reverse_iterator(std::begin(C)));
274}
275
276/// An iterator adaptor that filters the elements of given inner iterators.
277///
278/// The predicate parameter should be a callable object that accepts the wrapped
279/// iterator's reference type and returns a bool. When incrementing or
280/// decrementing the iterator, it will call the predicate on each element and
281/// skip any where it returns false.
282///
283/// \code
284/// int A[] = { 1, 2, 3, 4 };
285/// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; });
286/// // R contains { 1, 3 }.
287/// \endcode
288///
289/// Note: filter_iterator_base implements support for forward iteration.
290/// filter_iterator_impl exists to provide support for bidirectional iteration,
291/// conditional on whether the wrapped iterator supports it.
292template <typename WrappedIteratorT, typename PredicateT, typename IterTag>
293class filter_iterator_base
294 : public iterator_adaptor_base<
295 filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>,
296 WrappedIteratorT,
297 typename std::common_type<
298 IterTag, typename std::iterator_traits<
299 WrappedIteratorT>::iterator_category>::type> {
300 using BaseT = iterator_adaptor_base<
301 filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>,
302 WrappedIteratorT,
303 typename std::common_type<
304 IterTag, typename std::iterator_traits<
305 WrappedIteratorT>::iterator_category>::type>;
306
307protected:
308 WrappedIteratorT End;
309 PredicateT Pred;
310
311 void findNextValid() {
312 while (this->I != End && !Pred(*this->I))
313 BaseT::operator++();
314 }
315
316 // Construct the iterator. The begin iterator needs to know where the end
317 // is, so that it can properly stop when it gets there. The end iterator only
318 // needs the predicate to support bidirectional iteration.
319 filter_iterator_base(WrappedIteratorT Begin, WrappedIteratorT End,
320 PredicateT Pred)
321 : BaseT(Begin), End(End), Pred(Pred) {
322 findNextValid();
323 }
324
325public:
326 using BaseT::operator++;
327
328 filter_iterator_base &operator++() {
329 BaseT::operator++();
330 findNextValid();
331 return *this;
332 }
333};
334
335/// Specialization of filter_iterator_base for forward iteration only.
336template <typename WrappedIteratorT, typename PredicateT,
337 typename IterTag = std::forward_iterator_tag>
338class filter_iterator_impl
339 : public filter_iterator_base<WrappedIteratorT, PredicateT, IterTag> {
340 using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>;
341
342public:
343 filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End,
344 PredicateT Pred)
345 : BaseT(Begin, End, Pred) {}
346};
347
348/// Specialization of filter_iterator_base for bidirectional iteration.
349template <typename WrappedIteratorT, typename PredicateT>
350class filter_iterator_impl<WrappedIteratorT, PredicateT,
351 std::bidirectional_iterator_tag>
352 : public filter_iterator_base<WrappedIteratorT, PredicateT,
353 std::bidirectional_iterator_tag> {
354 using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT,
355 std::bidirectional_iterator_tag>;
356 void findPrevValid() {
357 while (!this->Pred(*this->I))
358 BaseT::operator--();
359 }
360
361public:
362 using BaseT::operator--;
363
364 filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End,
365 PredicateT Pred)
366 : BaseT(Begin, End, Pred) {}
367
368 filter_iterator_impl &operator--() {
369 BaseT::operator--();
370 findPrevValid();
371 return *this;
372 }
373};
374
375namespace detail {
376
377template <bool is_bidirectional> struct fwd_or_bidi_tag_impl {
378 using type = std::forward_iterator_tag;
379};
380
381template <> struct fwd_or_bidi_tag_impl<true> {
382 using type = std::bidirectional_iterator_tag;
383};
384
385/// Helper which sets its type member to forward_iterator_tag if the category
386/// of \p IterT does not derive from bidirectional_iterator_tag, and to
387/// bidirectional_iterator_tag otherwise.
388template <typename IterT> struct fwd_or_bidi_tag {
389 using type = typename fwd_or_bidi_tag_impl<std::is_base_of<
390 std::bidirectional_iterator_tag,
391 typename std::iterator_traits<IterT>::iterator_category>::value>::type;
392};
393
394} // namespace detail
395
396/// Defines filter_iterator to a suitable specialization of
397/// filter_iterator_impl, based on the underlying iterator's category.
398template <typename WrappedIteratorT, typename PredicateT>
399using filter_iterator = filter_iterator_impl<
400 WrappedIteratorT, PredicateT,
401 typename detail::fwd_or_bidi_tag<WrappedIteratorT>::type>;
402
403/// Convenience function that takes a range of elements and a predicate,
404/// and return a new filter_iterator range.
405///
406/// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the
407/// lifetime of that temporary is not kept by the returned range object, and the
408/// temporary is going to be dropped on the floor after the make_iterator_range
409/// full expression that contains this function call.
410template <typename RangeT, typename PredicateT>
411iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
412make_filter_range(RangeT &&Range, PredicateT Pred) {
413 using FilterIteratorT =
414 filter_iterator<detail::IterOfRange<RangeT>, PredicateT>;
415 return make_range(
416 FilterIteratorT(std::begin(std::forward<RangeT>(Range)),
417 std::end(std::forward<RangeT>(Range)), Pred),
418 FilterIteratorT(std::end(std::forward<RangeT>(Range)),
419 std::end(std::forward<RangeT>(Range)), Pred));
420}
421
422/// A pseudo-iterator adaptor that is designed to implement "early increment"
423/// style loops.
424///
425/// This is *not a normal iterator* and should almost never be used directly. It
426/// is intended primarily to be used with range based for loops and some range
427/// algorithms.
428///
429/// The iterator isn't quite an `OutputIterator` or an `InputIterator` but
430/// somewhere between them. The constraints of these iterators are:
431///
432/// - On construction or after being incremented, it is comparable and
433/// dereferencable. It is *not* incrementable.
434/// - After being dereferenced, it is neither comparable nor dereferencable, it
435/// is only incrementable.
436///
437/// This means you can only dereference the iterator once, and you can only
438/// increment it once between dereferences.
439template <typename WrappedIteratorT>
440class early_inc_iterator_impl
441 : public iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>,
442 WrappedIteratorT, std::input_iterator_tag> {
443 using BaseT =
444 iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>,
445 WrappedIteratorT, std::input_iterator_tag>;
446
447 using PointerT = typename std::iterator_traits<WrappedIteratorT>::pointer;
448
449protected:
450#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
451 bool IsEarlyIncremented = false;
452#endif
453
454public:
455 early_inc_iterator_impl(WrappedIteratorT I) : BaseT(I) {}
456
457 using BaseT::operator*;
458 typename BaseT::reference operator*() {
459#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
460 assert(!IsEarlyIncremented && "Cannot dereference twice!")((!IsEarlyIncremented && "Cannot dereference twice!")
? static_cast<void> (0) : __assert_fail ("!IsEarlyIncremented && \"Cannot dereference twice!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 460, __PRETTY_FUNCTION__))
;
461 IsEarlyIncremented = true;
462#endif
463 return *(this->I)++;
464 }
465
466 using BaseT::operator++;
467 early_inc_iterator_impl &operator++() {
468#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
469 assert(IsEarlyIncremented && "Cannot increment before dereferencing!")((IsEarlyIncremented && "Cannot increment before dereferencing!"
) ? static_cast<void> (0) : __assert_fail ("IsEarlyIncremented && \"Cannot increment before dereferencing!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 469, __PRETTY_FUNCTION__))
;
470 IsEarlyIncremented = false;
471#endif
472 return *this;
473 }
474
475 using BaseT::operator==;
476 bool operator==(const early_inc_iterator_impl &RHS) const {
477#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
478 assert(!IsEarlyIncremented && "Cannot compare after dereferencing!")((!IsEarlyIncremented && "Cannot compare after dereferencing!"
) ? static_cast<void> (0) : __assert_fail ("!IsEarlyIncremented && \"Cannot compare after dereferencing!\""
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 478, __PRETTY_FUNCTION__))
;
479#endif
480 return BaseT::operator==(RHS);
481 }
482};
483
484/// Make a range that does early increment to allow mutation of the underlying
485/// range without disrupting iteration.
486///
487/// The underlying iterator will be incremented immediately after it is
488/// dereferenced, allowing deletion of the current node or insertion of nodes to
489/// not disrupt iteration provided they do not invalidate the *next* iterator --
490/// the current iterator can be invalidated.
491///
492/// This requires a very exact pattern of use that is only really suitable to
493/// range based for loops and other range algorithms that explicitly guarantee
494/// to dereference exactly once each element, and to increment exactly once each
495/// element.
496template <typename RangeT>
497iterator_range<early_inc_iterator_impl<detail::IterOfRange<RangeT>>>
498make_early_inc_range(RangeT &&Range) {
499 using EarlyIncIteratorT =
500 early_inc_iterator_impl<detail::IterOfRange<RangeT>>;
501 return make_range(EarlyIncIteratorT(std::begin(std::forward<RangeT>(Range))),
502 EarlyIncIteratorT(std::end(std::forward<RangeT>(Range))));
503}
504
505// forward declarations required by zip_shortest/zip_first
506template <typename R, typename UnaryPredicate>
507bool all_of(R &&range, UnaryPredicate P);
508
509template <size_t... I> struct index_sequence;
510
511template <class... Ts> struct index_sequence_for;
512
513namespace detail {
514
515using std::declval;
516
517// We have to alias this since inlining the actual type at the usage site
518// in the parameter list of iterator_facade_base<> below ICEs MSVC 2017.
519template<typename... Iters> struct ZipTupleType {
520 using type = std::tuple<decltype(*declval<Iters>())...>;
521};
522
523template <typename ZipType, typename... Iters>
524using zip_traits = iterator_facade_base<
525 ZipType, typename std::common_type<std::bidirectional_iterator_tag,
526 typename std::iterator_traits<
527 Iters>::iterator_category...>::type,
528 // ^ TODO: Implement random access methods.
529 typename ZipTupleType<Iters...>::type,
530 typename std::iterator_traits<typename std::tuple_element<
531 0, std::tuple<Iters...>>::type>::difference_type,
532 // ^ FIXME: This follows boost::make_zip_iterator's assumption that all
533 // inner iterators have the same difference_type. It would fail if, for
534 // instance, the second field's difference_type were non-numeric while the
535 // first is.
536 typename ZipTupleType<Iters...>::type *,
537 typename ZipTupleType<Iters...>::type>;
538
539template <typename ZipType, typename... Iters>
540struct zip_common : public zip_traits<ZipType, Iters...> {
541 using Base = zip_traits<ZipType, Iters...>;
542 using value_type = typename Base::value_type;
543
544 std::tuple<Iters...> iterators;
545
546protected:
547 template <size_t... Ns> value_type deref(index_sequence<Ns...>) const {
548 return value_type(*std::get<Ns>(iterators)...);
549 }
550
551 template <size_t... Ns>
552 decltype(iterators) tup_inc(index_sequence<Ns...>) const {
553 return std::tuple<Iters...>(std::next(std::get<Ns>(iterators))...);
554 }
555
556 template <size_t... Ns>
557 decltype(iterators) tup_dec(index_sequence<Ns...>) const {
558 return std::tuple<Iters...>(std::prev(std::get<Ns>(iterators))...);
559 }
560
561public:
562 zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {}
563
564 value_type operator*() { return deref(index_sequence_for<Iters...>{}); }
565
566 const value_type operator*() const {
567 return deref(index_sequence_for<Iters...>{});
568 }
569
570 ZipType &operator++() {
571 iterators = tup_inc(index_sequence_for<Iters...>{});
572 return *reinterpret_cast<ZipType *>(this);
573 }
574
575 ZipType &operator--() {
576 static_assert(Base::IsBidirectional,
577 "All inner iterators must be at least bidirectional.");
578 iterators = tup_dec(index_sequence_for<Iters...>{});
579 return *reinterpret_cast<ZipType *>(this);
580 }
581};
582
583template <typename... Iters>
584struct zip_first : public zip_common<zip_first<Iters...>, Iters...> {
585 using Base = zip_common<zip_first<Iters...>, Iters...>;
586
587 bool operator==(const zip_first<Iters...> &other) const {
588 return std::get<0>(this->iterators) == std::get<0>(other.iterators);
589 }
590
591 zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
592};
593
594template <typename... Iters>
595class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> {
596 template <size_t... Ns>
597 bool test(const zip_shortest<Iters...> &other, index_sequence<Ns...>) const {
598 return all_of(std::initializer_list<bool>{std::get<Ns>(this->iterators) !=
599 std::get<Ns>(other.iterators)...},
600 identity<bool>{});
601 }
602
603public:
604 using Base = zip_common<zip_shortest<Iters...>, Iters...>;
605
606 zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
607
608 bool operator==(const zip_shortest<Iters...> &other) const {
609 return !test(other, index_sequence_for<Iters...>{});
610 }
611};
612
613template <template <typename...> class ItType, typename... Args> class zippy {
614public:
615 using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>;
616 using iterator_category = typename iterator::iterator_category;
617 using value_type = typename iterator::value_type;
618 using difference_type = typename iterator::difference_type;
619 using pointer = typename iterator::pointer;
620 using reference = typename iterator::reference;
621
622private:
623 std::tuple<Args...> ts;
624
625 template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) const {
626 return iterator(std::begin(std::get<Ns>(ts))...);
627 }
628 template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) const {
629 return iterator(std::end(std::get<Ns>(ts))...);
630 }
631
632public:
633 zippy(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {}
634
635 iterator begin() const { return begin_impl(index_sequence_for<Args...>{}); }
636 iterator end() const { return end_impl(index_sequence_for<Args...>{}); }
637};
638
639} // end namespace detail
640
641/// zip iterator for two or more iteratable types.
642template <typename T, typename U, typename... Args>
643detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u,
644 Args &&... args) {
645 return detail::zippy<detail::zip_shortest, T, U, Args...>(
646 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
647}
648
649/// zip iterator that, for the sake of efficiency, assumes the first iteratee to
650/// be the shortest.
651template <typename T, typename U, typename... Args>
652detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u,
653 Args &&... args) {
654 return detail::zippy<detail::zip_first, T, U, Args...>(
655 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
656}
657
658/// Iterator wrapper that concatenates sequences together.
659///
660/// This can concatenate different iterators, even with different types, into
661/// a single iterator provided the value types of all the concatenated
662/// iterators expose `reference` and `pointer` types that can be converted to
663/// `ValueT &` and `ValueT *` respectively. It doesn't support more
664/// interesting/customized pointer or reference types.
665///
666/// Currently this only supports forward or higher iterator categories as
667/// inputs and always exposes a forward iterator interface.
668template <typename ValueT, typename... IterTs>
669class concat_iterator
670 : public iterator_facade_base<concat_iterator<ValueT, IterTs...>,
671 std::forward_iterator_tag, ValueT> {
672 using BaseT = typename concat_iterator::iterator_facade_base;
673
674 /// We store both the current and end iterators for each concatenated
675 /// sequence in a tuple of pairs.
676 ///
677 /// Note that something like iterator_range seems nice at first here, but the
678 /// range properties are of little benefit and end up getting in the way
679 /// because we need to do mutation on the current iterators.
680 std::tuple<IterTs...> Begins;
681 std::tuple<IterTs...> Ends;
682
683 /// Attempts to increment a specific iterator.
684 ///
685 /// Returns true if it was able to increment the iterator. Returns false if
686 /// the iterator is already at the end iterator.
687 template <size_t Index> bool incrementHelper() {
688 auto &Begin = std::get<Index>(Begins);
689 auto &End = std::get<Index>(Ends);
690 if (Begin == End)
691 return false;
692
693 ++Begin;
694 return true;
695 }
696
697 /// Increments the first non-end iterator.
698 ///
699 /// It is an error to call this with all iterators at the end.
700 template <size_t... Ns> void increment(index_sequence<Ns...>) {
701 // Build a sequence of functions to increment each iterator if possible.
702 bool (concat_iterator::*IncrementHelperFns[])() = {
703 &concat_iterator::incrementHelper<Ns>...};
704
705 // Loop over them, and stop as soon as we succeed at incrementing one.
706 for (auto &IncrementHelperFn : IncrementHelperFns)
707 if ((this->*IncrementHelperFn)())
708 return;
709
710 llvm_unreachable("Attempted to increment an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to increment an end concat iterator!"
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 710)
;
711 }
712
713 /// Returns null if the specified iterator is at the end. Otherwise,
714 /// dereferences the iterator and returns the address of the resulting
715 /// reference.
716 template <size_t Index> ValueT *getHelper() const {
717 auto &Begin = std::get<Index>(Begins);
718 auto &End = std::get<Index>(Ends);
719 if (Begin == End)
720 return nullptr;
721
722 return &*Begin;
723 }
724
725 /// Finds the first non-end iterator, dereferences, and returns the resulting
726 /// reference.
727 ///
728 /// It is an error to call this with all iterators at the end.
729 template <size_t... Ns> ValueT &get(index_sequence<Ns...>) const {
730 // Build a sequence of functions to get from iterator if possible.
731 ValueT *(concat_iterator::*GetHelperFns[])() const = {
732 &concat_iterator::getHelper<Ns>...};
733
734 // Loop over them, and return the first result we find.
735 for (auto &GetHelperFn : GetHelperFns)
736 if (ValueT *P = (this->*GetHelperFn)())
737 return *P;
738
739 llvm_unreachable("Attempted to get a pointer from an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to get a pointer from an end concat iterator!"
, "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 739)
;
740 }
741
742public:
743 /// Constructs an iterator from a squence of ranges.
744 ///
745 /// We need the full range to know how to switch between each of the
746 /// iterators.
747 template <typename... RangeTs>
748 explicit concat_iterator(RangeTs &&... Ranges)
749 : Begins(std::begin(Ranges)...), Ends(std::end(Ranges)...) {}
750
751 using BaseT::operator++;
752
753 concat_iterator &operator++() {
754 increment(index_sequence_for<IterTs...>());
755 return *this;
756 }
757
758 ValueT &operator*() const { return get(index_sequence_for<IterTs...>()); }
759
760 bool operator==(const concat_iterator &RHS) const {
761 return Begins == RHS.Begins && Ends == RHS.Ends;
762 }
763};
764
765namespace detail {
766
767/// Helper to store a sequence of ranges being concatenated and access them.
768///
769/// This is designed to facilitate providing actual storage when temporaries
770/// are passed into the constructor such that we can use it as part of range
771/// based for loops.
772template <typename ValueT, typename... RangeTs> class concat_range {
773public:
774 using iterator =
775 concat_iterator<ValueT,
776 decltype(std::begin(std::declval<RangeTs &>()))...>;
777
778private:
779 std::tuple<RangeTs...> Ranges;
780
781 template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) {
782 return iterator(std::get<Ns>(Ranges)...);
783 }
784 template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) {
785 return iterator(make_range(std::end(std::get<Ns>(Ranges)),
786 std::end(std::get<Ns>(Ranges)))...);
787 }
788
789public:
790 concat_range(RangeTs &&... Ranges)
791 : Ranges(std::forward<RangeTs>(Ranges)...) {}
792
793 iterator begin() { return begin_impl(index_sequence_for<RangeTs...>{}); }
794 iterator end() { return end_impl(index_sequence_for<RangeTs...>{}); }
795};
796
797} // end namespace detail
798
799/// Concatenated range across two or more ranges.
800///
801/// The desired value type must be explicitly specified.
802template <typename ValueT, typename... RangeTs>
803detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) {
804 static_assert(sizeof...(RangeTs) > 1,
805 "Need more than one range to concatenate!");
806 return detail::concat_range<ValueT, RangeTs...>(
807 std::forward<RangeTs>(Ranges)...);
808}
809
810//===----------------------------------------------------------------------===//
811// Extra additions to <utility>
812//===----------------------------------------------------------------------===//
813
814/// Function object to check whether the first component of a std::pair
815/// compares less than the first component of another std::pair.
816struct less_first {
817 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
818 return lhs.first < rhs.first;
819 }
820};
821
822/// Function object to check whether the second component of a std::pair
823/// compares less than the second component of another std::pair.
824struct less_second {
825 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
826 return lhs.second < rhs.second;
827 }
828};
829
830/// \brief Function object to apply a binary function to the first component of
831/// a std::pair.
832template<typename FuncTy>
833struct on_first {
834 FuncTy func;
835
836 template <typename T>
837 auto operator()(const T &lhs, const T &rhs) const
838 -> decltype(func(lhs.first, rhs.first)) {
839 return func(lhs.first, rhs.first);
840 }
841};
842
843// A subset of N3658. More stuff can be added as-needed.
844
845/// Represents a compile-time sequence of integers.
846template <class T, T... I> struct integer_sequence {
847 using value_type = T;
848
849 static constexpr size_t size() { return sizeof...(I); }
850};
851
852/// Alias for the common case of a sequence of size_ts.
853template <size_t... I>
854struct index_sequence : integer_sequence<std::size_t, I...> {};
855
856template <std::size_t N, std::size_t... I>
857struct build_index_impl : build_index_impl<N - 1, N - 1, I...> {};
858template <std::size_t... I>
859struct build_index_impl<0, I...> : index_sequence<I...> {};
860
861/// Creates a compile-time integer sequence for a parameter pack.
862template <class... Ts>
863struct index_sequence_for : build_index_impl<sizeof...(Ts)> {};
864
865/// Utility type to build an inheritance chain that makes it easy to rank
866/// overload candidates.
867template <int N> struct rank : rank<N - 1> {};
868template <> struct rank<0> {};
869
870/// traits class for checking whether type T is one of any of the given
871/// types in the variadic list.
872template <typename T, typename... Ts> struct is_one_of {
873 static const bool value = false;
874};
875
876template <typename T, typename U, typename... Ts>
877struct is_one_of<T, U, Ts...> {
878 static const bool value =
879 std::is_same<T, U>::value || is_one_of<T, Ts...>::value;
880};
881
882/// traits class for checking whether type T is a base class for all
883/// the given types in the variadic list.
884template <typename T, typename... Ts> struct are_base_of {
885 static const bool value = true;
886};
887
888template <typename T, typename U, typename... Ts>
889struct are_base_of<T, U, Ts...> {
890 static const bool value =
891 std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value;
892};
893
894//===----------------------------------------------------------------------===//
895// Extra additions for arrays
896//===----------------------------------------------------------------------===//
897
898/// Find the length of an array.
899template <class T, std::size_t N>
900constexpr inline size_t array_lengthof(T (&)[N]) {
901 return N;
902}
903
904/// Adapt std::less<T> for array_pod_sort.
905template<typename T>
906inline int array_pod_sort_comparator(const void *P1, const void *P2) {
907 if (std::less<T>()(*reinterpret_cast<const T*>(P1),
908 *reinterpret_cast<const T*>(P2)))
909 return -1;
910 if (std::less<T>()(*reinterpret_cast<const T*>(P2),
911 *reinterpret_cast<const T*>(P1)))
912 return 1;
913 return 0;
914}
915
916/// get_array_pod_sort_comparator - This is an internal helper function used to
917/// get type deduction of T right.
918template<typename T>
919inline int (*get_array_pod_sort_comparator(const T &))
920 (const void*, const void*) {
921 return array_pod_sort_comparator<T>;
922}
923
924/// array_pod_sort - This sorts an array with the specified start and end
925/// extent. This is just like std::sort, except that it calls qsort instead of
926/// using an inlined template. qsort is slightly slower than std::sort, but
927/// most sorts are not performance critical in LLVM and std::sort has to be
928/// template instantiated for each type, leading to significant measured code
929/// bloat. This function should generally be used instead of std::sort where
930/// possible.
931///
932/// This function assumes that you have simple POD-like types that can be
933/// compared with std::less and can be moved with memcpy. If this isn't true,
934/// you should use std::sort.
935///
936/// NOTE: If qsort_r were portable, we could allow a custom comparator and
937/// default to std::less.
938template<class IteratorTy>
939inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
940 // Don't inefficiently call qsort with one element or trigger undefined
941 // behavior with an empty sequence.
942 auto NElts = End - Start;
943 if (NElts <= 1) return;
944#ifdef EXPENSIVE_CHECKS
945 std::mt19937 Generator(std::random_device{}());
946 std::shuffle(Start, End, Generator);
947#endif
948 qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start));
949}
950
951template <class IteratorTy>
952inline void array_pod_sort(
953 IteratorTy Start, IteratorTy End,
954 int (*Compare)(
955 const typename std::iterator_traits<IteratorTy>::value_type *,
956 const typename std::iterator_traits<IteratorTy>::value_type *)) {
957 // Don't inefficiently call qsort with one element or trigger undefined
958 // behavior with an empty sequence.
959 auto NElts = End - Start;
960 if (NElts <= 1) return;
961#ifdef EXPENSIVE_CHECKS
962 std::mt19937 Generator(std::random_device{}());
963 std::shuffle(Start, End, Generator);
964#endif
965 qsort(&*Start, NElts, sizeof(*Start),
966 reinterpret_cast<int (*)(const void *, const void *)>(Compare));
967}
968
969// Provide wrappers to std::sort which shuffle the elements before sorting
970// to help uncover non-deterministic behavior (PR35135).
971template <typename IteratorTy>
972inline void sort(IteratorTy Start, IteratorTy End) {
973#ifdef EXPENSIVE_CHECKS
974 std::mt19937 Generator(std::random_device{}());
975 std::shuffle(Start, End, Generator);
976#endif
977 std::sort(Start, End);
978}
979
980template <typename Container> inline void sort(Container &&C) {
981 llvm::sort(adl_begin(C), adl_end(C));
982}
983
984template <typename IteratorTy, typename Compare>
985inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) {
986#ifdef EXPENSIVE_CHECKS
987 std::mt19937 Generator(std::random_device{}());
988 std::shuffle(Start, End, Generator);
989#endif
990 std::sort(Start, End, Comp);
991}
992
993template <typename Container, typename Compare>
994inline void sort(Container &&C, Compare Comp) {
995 llvm::sort(adl_begin(C), adl_end(C), Comp);
996}
997
998//===----------------------------------------------------------------------===//
999// Extra additions to <algorithm>
1000//===----------------------------------------------------------------------===//
1001
1002/// For a container of pointers, deletes the pointers and then clears the
1003/// container.
1004template<typename Container>
1005void DeleteContainerPointers(Container &C) {
1006 for (auto V : C)
1007 delete V;
1008 C.clear();
1009}
1010
1011/// In a container of pairs (usually a map) whose second element is a pointer,
1012/// deletes the second elements and then clears the container.
1013template<typename Container>
1014void DeleteContainerSeconds(Container &C) {
1015 for (auto &V : C)
1016 delete V.second;
1017 C.clear();
1018}
1019
1020/// Get the size of a range. This is a wrapper function around std::distance
1021/// which is only enabled when the operation is O(1).
1022template <typename R>
1023auto size(R &&Range, typename std::enable_if<
1024 std::is_same<typename std::iterator_traits<decltype(
1025 Range.begin())>::iterator_category,
1026 std::random_access_iterator_tag>::value,
1027 void>::type * = nullptr)
1028 -> decltype(std::distance(Range.begin(), Range.end())) {
1029 return std::distance(Range.begin(), Range.end());
1030}
1031
1032/// Provide wrappers to std::for_each which take ranges instead of having to
1033/// pass begin/end explicitly.
1034template <typename R, typename UnaryPredicate>
1035UnaryPredicate for_each(R &&Range, UnaryPredicate P) {
1036 return std::for_each(adl_begin(Range), adl_end(Range), P);
1037}
1038
1039/// Provide wrappers to std::all_of which take ranges instead of having to pass
1040/// begin/end explicitly.
1041template <typename R, typename UnaryPredicate>
1042bool all_of(R &&Range, UnaryPredicate P) {
1043 return std::all_of(adl_begin(Range), adl_end(Range), P);
1044}
1045
1046/// Provide wrappers to std::any_of which take ranges instead of having to pass
1047/// begin/end explicitly.
1048template <typename R, typename UnaryPredicate>
1049bool any_of(R &&Range, UnaryPredicate P) {
1050 return std::any_of(adl_begin(Range), adl_end(Range), P);
1051}
1052
1053/// Provide wrappers to std::none_of which take ranges instead of having to pass
1054/// begin/end explicitly.
1055template <typename R, typename UnaryPredicate>
1056bool none_of(R &&Range, UnaryPredicate P) {
1057 return std::none_of(adl_begin(Range), adl_end(Range), P);
1058}
1059
1060/// Provide wrappers to std::find which take ranges instead of having to pass
1061/// begin/end explicitly.
1062template <typename R, typename T>
1063auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range)) {
1064 return std::find(adl_begin(Range), adl_end(Range), Val);
1065}
1066
1067/// Provide wrappers to std::find_if which take ranges instead of having to pass
1068/// begin/end explicitly.
1069template <typename R, typename UnaryPredicate>
1070auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
1071 return std::find_if(adl_begin(Range), adl_end(Range), P);
1072}
1073
1074template <typename R, typename UnaryPredicate>
1075auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
1076 return std::find_if_not(adl_begin(Range), adl_end(Range), P);
1077}
1078
1079/// Provide wrappers to std::remove_if which take ranges instead of having to
1080/// pass begin/end explicitly.
1081template <typename R, typename UnaryPredicate>
1082auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
1083 return std::remove_if(adl_begin(Range), adl_end(Range), P);
1084}
1085
1086/// Provide wrappers to std::copy_if which take ranges instead of having to
1087/// pass begin/end explicitly.
1088template <typename R, typename OutputIt, typename UnaryPredicate>
1089OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) {
1090 return std::copy_if(adl_begin(Range), adl_end(Range), Out, P);
1091}
1092
1093template <typename R, typename OutputIt>
1094OutputIt copy(R &&Range, OutputIt Out) {
1095 return std::copy(adl_begin(Range), adl_end(Range), Out);
1096}
1097
1098/// Wrapper function around std::find to detect if an element exists
1099/// in a container.
1100template <typename R, typename E>
1101bool is_contained(R &&Range, const E &Element) {
1102 return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
1103}
1104
1105/// Wrapper function around std::count to count the number of times an element
1106/// \p Element occurs in the given range \p Range.
1107template <typename R, typename E>
1108auto count(R &&Range, const E &Element) ->
1109 typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type {
1110 return std::count(adl_begin(Range), adl_end(Range), Element);
1111}
1112
1113/// Wrapper function around std::count_if to count the number of times an
1114/// element satisfying a given predicate occurs in a range.
1115template <typename R, typename UnaryPredicate>
1116auto count_if(R &&Range, UnaryPredicate P) ->
1117 typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type {
1118 return std::count_if(adl_begin(Range), adl_end(Range), P);
1119}
1120
1121/// Wrapper function around std::transform to apply a function to a range and
1122/// store the result elsewhere.
1123template <typename R, typename OutputIt, typename UnaryPredicate>
1124OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) {
1125 return std::transform(adl_begin(Range), adl_end(Range), d_first, P);
1126}
1127
1128/// Provide wrappers to std::partition which take ranges instead of having to
1129/// pass begin/end explicitly.
1130template <typename R, typename UnaryPredicate>
1131auto partition(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) {
1132 return std::partition(adl_begin(Range), adl_end(Range), P);
1133}
1134
1135/// Provide wrappers to std::lower_bound which take ranges instead of having to
1136/// pass begin/end explicitly.
1137template <typename R, typename ForwardIt>
1138auto lower_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range)) {
1139 return std::lower_bound(adl_begin(Range), adl_end(Range), I);
1140}
1141
1142template <typename R, typename ForwardIt, typename Compare>
1143auto lower_bound(R &&Range, ForwardIt I, Compare C)
1144 -> decltype(adl_begin(Range)) {
1145 return std::lower_bound(adl_begin(Range), adl_end(Range), I, C);
1146}
1147
1148/// Provide wrappers to std::upper_bound which take ranges instead of having to
1149/// pass begin/end explicitly.
1150template <typename R, typename ForwardIt>
1151auto upper_bound(R &&Range, ForwardIt I) -> decltype(adl_begin(Range)) {
1152 return std::upper_bound(adl_begin(Range), adl_end(Range), I);
1153}
1154
1155template <typename R, typename ForwardIt, typename Compare>
1156auto upper_bound(R &&Range, ForwardIt I, Compare C)
1157 -> decltype(adl_begin(Range)) {
1158 return std::upper_bound(adl_begin(Range), adl_end(Range), I, C);
1159}
1160/// Wrapper function around std::equal to detect if all elements
1161/// in a container are same.
1162template <typename R>
1163bool is_splat(R &&Range) {
1164 size_t range_size = size(Range);
1165 return range_size != 0 && (range_size == 1 ||
1166 std::equal(adl_begin(Range) + 1, adl_end(Range), adl_begin(Range)));
1167}
1168
1169/// Given a range of type R, iterate the entire range and return a
1170/// SmallVector with elements of the vector. This is useful, for example,
1171/// when you want to iterate a range and then sort the results.
1172template <unsigned Size, typename R>
1173SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type, Size>
1174to_vector(R &&Range) {
1175 return {adl_begin(Range), adl_end(Range)};
1176}
1177
1178/// Provide a container algorithm similar to C++ Library Fundamentals v2's
1179/// `erase_if` which is equivalent to:
1180///
1181/// C.erase(remove_if(C, pred), C.end());
1182///
1183/// This version works for any container with an erase method call accepting
1184/// two iterators.
1185template <typename Container, typename UnaryPredicate>
1186void erase_if(Container &C, UnaryPredicate P) {
1187 C.erase(remove_if(C, P), C.end());
1188}
1189
1190//===----------------------------------------------------------------------===//
1191// Extra additions to <memory>
1192//===----------------------------------------------------------------------===//
1193
1194// Implement make_unique according to N3656.
1195
1196/// Constructs a `new T()` with the given args and returns a
1197/// `unique_ptr<T>` which owns the object.
1198///
1199/// Example:
1200///
1201/// auto p = make_unique<int>();
1202/// auto p = make_unique<std::tuple<int, int>>(0, 1);
1203template <class T, class... Args>
1204typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
1205make_unique(Args &&... args) {
1206 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
11
Memory is allocated
1207}
1208
1209/// Constructs a `new T[n]` with the given args and returns a
1210/// `unique_ptr<T[]>` which owns the object.
1211///
1212/// \param n size of the new array.
1213///
1214/// Example:
1215///
1216/// auto p = make_unique<int[]>(2); // value-initializes the array with 0's.
1217template <class T>
1218typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
1219 std::unique_ptr<T>>::type
1220make_unique(size_t n) {
1221 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
1222}
1223
1224/// This function isn't used and is only here to provide better compile errors.
1225template <class T, class... Args>
1226typename std::enable_if<std::extent<T>::value != 0>::type
1227make_unique(Args &&...) = delete;
1228
1229struct FreeDeleter {
1230 void operator()(void* v) {
1231 ::free(v);
1232 }
1233};
1234
1235template<typename First, typename Second>
1236struct pair_hash {
1237 size_t operator()(const std::pair<First, Second> &P) const {
1238 return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
1239 }
1240};
1241
1242/// A functor like C++14's std::less<void> in its absence.
1243struct less {
1244 template <typename A, typename B> bool operator()(A &&a, B &&b) const {
1245 return std::forward<A>(a) < std::forward<B>(b);
1246 }
1247};
1248
1249/// A functor like C++14's std::equal<void> in its absence.
1250struct equal {
1251 template <typename A, typename B> bool operator()(A &&a, B &&b) const {
1252 return std::forward<A>(a) == std::forward<B>(b);
1253 }
1254};
1255
1256/// Binary functor that adapts to any other binary functor after dereferencing
1257/// operands.
1258template <typename T> struct deref {
1259 T func;
1260
1261 // Could be further improved to cope with non-derivable functors and
1262 // non-binary functors (should be a variadic template member function
1263 // operator()).
1264 template <typename A, typename B>
1265 auto operator()(A &lhs, B &rhs) const -> decltype(func(*lhs, *rhs)) {
1266 assert(lhs)((lhs) ? static_cast<void> (0) : __assert_fail ("lhs", "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 1266, __PRETTY_FUNCTION__))
;
1267 assert(rhs)((rhs) ? static_cast<void> (0) : __assert_fail ("rhs", "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/ADT/STLExtras.h"
, 1267, __PRETTY_FUNCTION__))
;
1268 return func(*lhs, *rhs);
1269 }
1270};
1271
1272namespace detail {
1273
1274template <typename R> class enumerator_iter;
1275
1276template <typename R> struct result_pair {
1277 friend class enumerator_iter<R>;
1278
1279 result_pair() = default;
1280 result_pair(std::size_t Index, IterOfRange<R> Iter)
1281 : Index(Index), Iter(Iter) {}
1282
1283 result_pair<R> &operator=(const result_pair<R> &Other) {
1284 Index = Other.Index;
1285 Iter = Other.Iter;
1286 return *this;
1287 }
1288
1289 std::size_t index() const { return Index; }
1290 const ValueOfRange<R> &value() const { return *Iter; }
1291 ValueOfRange<R> &value() { return *Iter; }
1292
1293private:
1294 std::size_t Index = std::numeric_limits<std::size_t>::max();
1295 IterOfRange<R> Iter;
1296};
1297
1298template <typename R>
1299class enumerator_iter
1300 : public iterator_facade_base<
1301 enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
1302 typename std::iterator_traits<IterOfRange<R>>::difference_type,
1303 typename std::iterator_traits<IterOfRange<R>>::pointer,
1304 typename std::iterator_traits<IterOfRange<R>>::reference> {