Bug Summary

File:tools/bugpoint/Miscompilation.cpp
Warning:line 348, column 9
Branch condition evaluates to a garbage value

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 Miscompilation.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -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-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/tools/bugpoint -I /build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint -I /build/llvm-toolchain-snapshot-7~svn326551/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn326551/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.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-7~svn326551/build-llvm/tools/bugpoint -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-03-02-155150-1477-1 -x c++ /build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint/Miscompilation.cpp

/build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint/Miscompilation.cpp

1//===- Miscompilation.cpp - Debug program miscompilations -----------------===//
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 implements optimizer and code generation miscompilation debugging
11// support.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
16#include "ListReducer.h"
17#include "ToolRunner.h"
18#include "llvm/Config/config.h" // for HAVE_LINK_R
19#include "llvm/IR/Constants.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/Verifier.h"
24#include "llvm/Linker/Linker.h"
25#include "llvm/Pass.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/FileUtilities.h"
28#include "llvm/Transforms/Utils/Cloning.h"
29
30using namespace llvm;
31
32namespace llvm {
33extern cl::opt<std::string> OutputPrefix;
34extern cl::list<std::string> InputArgv;
35} // end namespace llvm
36
37namespace {
38static llvm::cl::opt<bool> DisableLoopExtraction(
39 "disable-loop-extraction",
40 cl::desc("Don't extract loops when searching for miscompilations"),
41 cl::init(false));
42static llvm::cl::opt<bool> DisableBlockExtraction(
43 "disable-block-extraction",
44 cl::desc("Don't extract blocks when searching for miscompilations"),
45 cl::init(false));
46
47class ReduceMiscompilingPasses : public ListReducer<std::string> {
48 BugDriver &BD;
49
50public:
51 ReduceMiscompilingPasses(BugDriver &bd) : BD(bd) {}
52
53 Expected<TestResult> doTest(std::vector<std::string> &Prefix,
54 std::vector<std::string> &Suffix) override;
55};
56} // end anonymous namespace
57
58/// TestResult - After passes have been split into a test group and a control
59/// group, see if they still break the program.
60///
61Expected<ReduceMiscompilingPasses::TestResult>
62ReduceMiscompilingPasses::doTest(std::vector<std::string> &Prefix,
63 std::vector<std::string> &Suffix) {
64 // First, run the program with just the Suffix passes. If it is still broken
65 // with JUST the kept passes, discard the prefix passes.
66 outs() << "Checking to see if '" << getPassesString(Suffix)
67 << "' compiles correctly: ";
68
69 std::string BitcodeResult;
70 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/,
71 true /*quiet*/)) {
72 errs() << " Error running this sequence of passes"
73 << " on the input program!\n";
74 BD.setPassesToRun(Suffix);
75 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
76 // TODO: This should propagate the error instead of exiting.
77 if (Error E = BD.debugOptimizerCrash())
78 exit(1);
79 exit(0);
80 }
81
82 // Check to see if the finished program matches the reference output...
83 Expected<bool> Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
84 true /*delete bitcode*/);
85 if (Error E = Diff.takeError())
86 return std::move(E);
87 if (*Diff) {
88 outs() << " nope.\n";
89 if (Suffix.empty()) {
90 errs() << BD.getToolName() << ": I'm confused: the test fails when "
91 << "no passes are run, nondeterministic program?\n";
92 exit(1);
93 }
94 return KeepSuffix; // Miscompilation detected!
95 }
96 outs() << " yup.\n"; // No miscompilation!
97
98 if (Prefix.empty())
99 return NoFailure;
100
101 // Next, see if the program is broken if we run the "prefix" passes first,
102 // then separately run the "kept" passes.
103 outs() << "Checking to see if '" << getPassesString(Prefix)
104 << "' compiles correctly: ";
105
106 // If it is not broken with the kept passes, it's possible that the prefix
107 // passes must be run before the kept passes to break it. If the program
108 // WORKS after the prefix passes, but then fails if running the prefix AND
109 // kept passes, we can update our bitcode file to include the result of the
110 // prefix passes, then discard the prefix passes.
111 //
112 if (BD.runPasses(BD.getProgram(), Prefix, BitcodeResult, false /*delete*/,
113 true /*quiet*/)) {
114 errs() << " Error running this sequence of passes"
115 << " on the input program!\n";
116 BD.setPassesToRun(Prefix);
117 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
118 // TODO: This should propagate the error instead of exiting.
119 if (Error E = BD.debugOptimizerCrash())
120 exit(1);
121 exit(0);
122 }
123
124 // If the prefix maintains the predicate by itself, only keep the prefix!
125 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "", false);
126 if (Error E = Diff.takeError())
127 return std::move(E);
128 if (*Diff) {
129 outs() << " nope.\n";
130 sys::fs::remove(BitcodeResult);
131 return KeepPrefix;
132 }
133 outs() << " yup.\n"; // No miscompilation!
134
135 // Ok, so now we know that the prefix passes work, try running the suffix
136 // passes on the result of the prefix passes.
137 //
138 std::unique_ptr<Module> PrefixOutput =
139 parseInputFile(BitcodeResult, BD.getContext());
140 if (!PrefixOutput) {
141 errs() << BD.getToolName() << ": Error reading bitcode file '"
142 << BitcodeResult << "'!\n";
143 exit(1);
144 }
145 sys::fs::remove(BitcodeResult);
146
147 // Don't check if there are no passes in the suffix.
148 if (Suffix.empty())
149 return NoFailure;
150
151 outs() << "Checking to see if '" << getPassesString(Suffix)
152 << "' passes compile correctly after the '" << getPassesString(Prefix)
153 << "' passes: ";
154
155 std::unique_ptr<Module> OriginalInput =
156 BD.swapProgramIn(std::move(PrefixOutput));
157 if (BD.runPasses(BD.getProgram(), Suffix, BitcodeResult, false /*delete*/,
158 true /*quiet*/)) {
159 errs() << " Error running this sequence of passes"
160 << " on the input program!\n";
161 BD.setPassesToRun(Suffix);
162 BD.EmitProgressBitcode(BD.getProgram(), "pass-error", false);
163 // TODO: This should propagate the error instead of exiting.
164 if (Error E = BD.debugOptimizerCrash())
165 exit(1);
166 exit(0);
167 }
168
169 // Run the result...
170 Diff = BD.diffProgram(BD.getProgram(), BitcodeResult, "",
171 true /*delete bitcode*/);
172 if (Error E = Diff.takeError())
173 return std::move(E);
174 if (*Diff) {
175 outs() << " nope.\n";
176 return KeepSuffix;
177 }
178
179 // Otherwise, we must not be running the bad pass anymore.
180 outs() << " yup.\n"; // No miscompilation!
181 // Restore orig program & free test.
182 BD.setNewProgram(std::move(OriginalInput));
183 return NoFailure;
184}
185
186namespace {
187class ReduceMiscompilingFunctions : public ListReducer<Function *> {
188 BugDriver &BD;
189 Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,
190 std::unique_ptr<Module>);
191
192public:
193 ReduceMiscompilingFunctions(BugDriver &bd,
194 Expected<bool> (*F)(BugDriver &,
195 std::unique_ptr<Module>,
196 std::unique_ptr<Module>))
197 : BD(bd), TestFn(F) {}
198
199 Expected<TestResult> doTest(std::vector<Function *> &Prefix,
200 std::vector<Function *> &Suffix) override {
201 if (!Suffix.empty()) {
202 Expected<bool> Ret = TestFuncs(Suffix);
203 if (Error E = Ret.takeError())
204 return std::move(E);
205 if (*Ret)
206 return KeepSuffix;
207 }
208 if (!Prefix.empty()) {
209 Expected<bool> Ret = TestFuncs(Prefix);
210 if (Error E = Ret.takeError())
211 return std::move(E);
212 if (*Ret)
213 return KeepPrefix;
214 }
215 return NoFailure;
216 }
217
218 Expected<bool> TestFuncs(const std::vector<Function *> &Prefix);
219};
220} // end anonymous namespace
221
222/// Given two modules, link them together and run the program, checking to see
223/// if the program matches the diff. If there is an error, return NULL. If not,
224/// return the merged module. The Broken argument will be set to true if the
225/// output is different. If the DeleteInputs argument is set to true then this
226/// function deletes both input modules before it returns.
227///
228static Expected<std::unique_ptr<Module>> testMergedProgram(const BugDriver &BD,
229 const Module &M1,
230 const Module &M2,
231 bool &Broken) {
232 // Resulting merge of M1 and M2.
233 auto Merged = CloneModule(M1);
17
Calling move constructor for 'unique_ptr'
84
Returning from move constructor for 'unique_ptr'
234 if (Linker::linkModules(*Merged, CloneModule(M2)))
85
Calling 'unique_ptr::operator*'
98
Returning from 'unique_ptr::operator*'
99
Calling move constructor for 'unique_ptr'
166
Returning from move constructor for 'unique_ptr'
167
Assuming the condition is false
168
Taking false branch
235 // TODO: Shouldn't we thread the error up instead of exiting?
236 exit(1);
237
238 // Execute the program.
239 Expected<bool> Diff = BD.diffProgram(*Merged, "", "", false);
169
Calling 'unique_ptr::operator*'
182
Returning from 'unique_ptr::operator*'
183
Calling move constructor for 'Expected'
262
Returning from move constructor for 'Expected'
240 if (Error E = Diff.takeError())
263
Calling 'Expected::takeError'
324
Returning from 'Expected::takeError'
325
Calling move constructor for 'Error'
349
Returning from move constructor for 'Error'
350
Calling 'Error::operator bool'
359
Returning from 'Error::operator bool'
360
Taking true branch
241 return std::move(E);
361
Calling 'move'
362
Returning from 'move'
363
Calling move constructor for 'Error'
387
Returning from move constructor for 'Error'
388
Calling move constructor for 'Expected'
467
Returning from move constructor for 'Expected'
468
Calling '~Error'
475
Returning from '~Error'
476
Calling '~Expected'
479
Returning from '~Expected'
480
Calling '~unique_ptr'
506
Returning from '~unique_ptr'
242 Broken = *Diff;
243 return std::move(Merged);
244}
507
Returning without writing to 'Broken'
245
246/// split functions in a Module into two groups: those that are under
247/// consideration for miscompilation vs. those that are not, and test
248/// accordingly. Each group of functions becomes a separate Module.
249Expected<bool>
250ReduceMiscompilingFunctions::TestFuncs(const std::vector<Function *> &Funcs) {
251 // Test to see if the function is misoptimized if we ONLY run it on the
252 // functions listed in Funcs.
253 outs() << "Checking to see if the program is misoptimized when "
254 << (Funcs.size() == 1 ? "this function is" : "these functions are")
255 << " run through the pass"
256 << (BD.getPassesToRun().size() == 1 ? "" : "es") << ":";
257 PrintFunctionList(Funcs);
258 outs() << '\n';
259
260 // Create a clone for two reasons:
261 // * If the optimization passes delete any function, the deleted function
262 // will be in the clone and Funcs will still point to valid memory
263 // * If the optimization passes use interprocedural information to break
264 // a function, we want to continue with the original function. Otherwise
265 // we can conclude that a function triggers the bug when in fact one
266 // needs a larger set of original functions to do so.
267 ValueToValueMapTy VMap;
268 std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap);
269 std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone));
270
271 std::vector<Function *> FuncsOnClone;
272 for (unsigned i = 0, e = Funcs.size(); i != e; ++i) {
273 Function *F = cast<Function>(VMap[Funcs[i]]);
274 FuncsOnClone.push_back(F);
275 }
276
277 // Split the module into the two halves of the program we want.
278 VMap.clear();
279 std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
280 std::unique_ptr<Module> ToOptimize =
281 SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);
282
283 Expected<bool> Broken =
284 TestFn(BD, std::move(ToOptimize), std::move(ToNotOptimize));
285
286 BD.setNewProgram(std::move(Orig));
287
288 return Broken;
289}
290
291/// Give anonymous global values names.
292static void DisambiguateGlobalSymbols(Module &M) {
293 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E;
294 ++I)
295 if (!I->hasName())
296 I->setName("anon_global");
297 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
298 if (!I->hasName())
299 I->setName("anon_fn");
300}
301
302/// Given a reduced list of functions that still exposed the bug, check to see
303/// if we can extract the loops in the region without obscuring the bug. If so,
304/// it reduces the amount of code identified.
305///
306static Expected<bool>
307ExtractLoops(BugDriver &BD,
308 Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,
309 std::unique_ptr<Module>),
310 std::vector<Function *> &MiscompiledFunctions) {
311 bool MadeChange = false;
312 while (1) {
12
Loop condition is true. Entering loop body
313 if (BugpointIsInterrupted)
13
Taking false branch
314 return MadeChange;
315
316 ValueToValueMapTy VMap;
317 std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
318 std::unique_ptr<Module> ToOptimize = SplitFunctionsOutOfModule(
319 ToNotOptimize.get(), MiscompiledFunctions, VMap);
320 std::unique_ptr<Module> ToOptimizeLoopExtracted =
321 BD.extractLoop(ToOptimize.get());
322 if (!ToOptimizeLoopExtracted)
14
Taking false branch
323 // If the loop extractor crashed or if there were no extractible loops,
324 // then this chapter of our odyssey is over with.
325 return MadeChange;
326
327 errs() << "Extracted a loop from the breaking portion of the program.\n";
328
329 // Bugpoint is intentionally not very trusting of LLVM transformations. In
330 // particular, we're not going to assume that the loop extractor works, so
331 // we're going to test the newly loop extracted program to make sure nothing
332 // has broken. If something broke, then we'll inform the user and stop
333 // extraction.
334 AbstractInterpreter *AI = BD.switchToSafeInterpreter();
335 bool Failure;
15
'Failure' declared without an initial value
336 Expected<std::unique_ptr<Module>> New = testMergedProgram(
16
Calling 'testMergedProgram'
508
Returning from 'testMergedProgram'
337 BD, *ToOptimizeLoopExtracted, *ToNotOptimize, Failure);
338 if (Error E = New.takeError())
509
Taking false branch
339 return std::move(E);
340 if (!*New)
510
Taking false branch
341 return false;
342
343 // Delete the original and set the new program.
344 std::unique_ptr<Module> Old = BD.swapProgramIn(std::move(*New));
345 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
511
Assuming 'i' is equal to 'e'
512
Loop condition is false. Execution continues on line 348
346 MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);
347
348 if (Failure) {
513
Branch condition evaluates to a garbage value
349 BD.switchToInterpreter(AI);
350
351 // Merged program doesn't work anymore!
352 errs() << " *** ERROR: Loop extraction broke the program. :("
353 << " Please report a bug!\n";
354 errs() << " Continuing on with un-loop-extracted version.\n";
355
356 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-tno.bc",
357 *ToNotOptimize);
358 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to.bc",
359 *ToOptimize);
360 BD.writeProgramToFile(OutputPrefix + "-loop-extract-fail-to-le.bc",
361 *ToOptimizeLoopExtracted);
362
363 errs() << "Please submit the " << OutputPrefix
364 << "-loop-extract-fail-*.bc files.\n";
365 return MadeChange;
366 }
367 BD.switchToInterpreter(AI);
368
369 outs() << " Testing after loop extraction:\n";
370 // Clone modules, the tester function will free them.
371 std::unique_ptr<Module> TOLEBackup =
372 CloneModule(*ToOptimizeLoopExtracted, VMap);
373 std::unique_ptr<Module> TNOBackup = CloneModule(*ToNotOptimize, VMap);
374
375 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
376 MiscompiledFunctions[i] = cast<Function>(VMap[MiscompiledFunctions[i]]);
377
378 Expected<bool> Result = TestFn(BD, std::move(ToOptimizeLoopExtracted),
379 std::move(ToNotOptimize));
380 if (Error E = Result.takeError())
381 return std::move(E);
382
383 ToOptimizeLoopExtracted = std::move(TOLEBackup);
384 ToNotOptimize = std::move(TNOBackup);
385
386 if (!*Result) {
387 outs() << "*** Loop extraction masked the problem. Undoing.\n";
388 // If the program is not still broken, then loop extraction did something
389 // that masked the error. Stop loop extraction now.
390
391 std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;
392 for (Function *F : MiscompiledFunctions) {
393 MisCompFunctions.emplace_back(F->getName(), F->getFunctionType());
394 }
395
396 if (Linker::linkModules(*ToNotOptimize,
397 std::move(ToOptimizeLoopExtracted)))
398 exit(1);
399
400 MiscompiledFunctions.clear();
401 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
402 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
403
404 assert(NewF && "Function not found??")(static_cast <bool> (NewF && "Function not found??"
) ? void (0) : __assert_fail ("NewF && \"Function not found??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint/Miscompilation.cpp"
, 404, __extension__ __PRETTY_FUNCTION__))
;
405 MiscompiledFunctions.push_back(NewF);
406 }
407
408 BD.setNewProgram(std::move(ToNotOptimize));
409 return MadeChange;
410 }
411
412 outs() << "*** Loop extraction successful!\n";
413
414 std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;
415 for (Module::iterator I = ToOptimizeLoopExtracted->begin(),
416 E = ToOptimizeLoopExtracted->end();
417 I != E; ++I)
418 if (!I->isDeclaration())
419 MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
420
421 // Okay, great! Now we know that we extracted a loop and that loop
422 // extraction both didn't break the program, and didn't mask the problem.
423 // Replace the current program with the loop extracted version, and try to
424 // extract another loop.
425 if (Linker::linkModules(*ToNotOptimize, std::move(ToOptimizeLoopExtracted)))
426 exit(1);
427
428 // All of the Function*'s in the MiscompiledFunctions list are in the old
429 // module. Update this list to include all of the functions in the
430 // optimized and loop extracted module.
431 MiscompiledFunctions.clear();
432 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
433 Function *NewF = ToNotOptimize->getFunction(MisCompFunctions[i].first);
434
435 assert(NewF && "Function not found??")(static_cast <bool> (NewF && "Function not found??"
) ? void (0) : __assert_fail ("NewF && \"Function not found??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint/Miscompilation.cpp"
, 435, __extension__ __PRETTY_FUNCTION__))
;
436 MiscompiledFunctions.push_back(NewF);
437 }
438
439 BD.setNewProgram(std::move(ToNotOptimize));
440 MadeChange = true;
441 }
442}
443
444namespace {
445class ReduceMiscompiledBlocks : public ListReducer<BasicBlock *> {
446 BugDriver &BD;
447 Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,
448 std::unique_ptr<Module>);
449 std::vector<Function *> FunctionsBeingTested;
450
451public:
452 ReduceMiscompiledBlocks(BugDriver &bd,
453 Expected<bool> (*F)(BugDriver &,
454 std::unique_ptr<Module>,
455 std::unique_ptr<Module>),
456 const std::vector<Function *> &Fns)
457 : BD(bd), TestFn(F), FunctionsBeingTested(Fns) {}
458
459 Expected<TestResult> doTest(std::vector<BasicBlock *> &Prefix,
460 std::vector<BasicBlock *> &Suffix) override {
461 if (!Suffix.empty()) {
462 Expected<bool> Ret = TestFuncs(Suffix);
463 if (Error E = Ret.takeError())
464 return std::move(E);
465 if (*Ret)
466 return KeepSuffix;
467 }
468 if (!Prefix.empty()) {
469 Expected<bool> Ret = TestFuncs(Prefix);
470 if (Error E = Ret.takeError())
471 return std::move(E);
472 if (*Ret)
473 return KeepPrefix;
474 }
475 return NoFailure;
476 }
477
478 Expected<bool> TestFuncs(const std::vector<BasicBlock *> &BBs);
479};
480} // end anonymous namespace
481
482/// TestFuncs - Extract all blocks for the miscompiled functions except for the
483/// specified blocks. If the problem still exists, return true.
484///
485Expected<bool>
486ReduceMiscompiledBlocks::TestFuncs(const std::vector<BasicBlock *> &BBs) {
487 // Test to see if the function is misoptimized if we ONLY run it on the
488 // functions listed in Funcs.
489 outs() << "Checking to see if the program is misoptimized when all ";
490 if (!BBs.empty()) {
491 outs() << "but these " << BBs.size() << " blocks are extracted: ";
492 for (unsigned i = 0, e = BBs.size() < 10 ? BBs.size() : 10; i != e; ++i)
493 outs() << BBs[i]->getName() << " ";
494 if (BBs.size() > 10)
495 outs() << "...";
496 } else {
497 outs() << "blocks are extracted.";
498 }
499 outs() << '\n';
500
501 // Split the module into the two halves of the program we want.
502 ValueToValueMapTy VMap;
503 std::unique_ptr<Module> Clone = CloneModule(BD.getProgram(), VMap);
504 std::unique_ptr<Module> Orig = BD.swapProgramIn(std::move(Clone));
505 std::vector<Function *> FuncsOnClone;
506 std::vector<BasicBlock *> BBsOnClone;
507 for (unsigned i = 0, e = FunctionsBeingTested.size(); i != e; ++i) {
508 Function *F = cast<Function>(VMap[FunctionsBeingTested[i]]);
509 FuncsOnClone.push_back(F);
510 }
511 for (unsigned i = 0, e = BBs.size(); i != e; ++i) {
512 BasicBlock *BB = cast<BasicBlock>(VMap[BBs[i]]);
513 BBsOnClone.push_back(BB);
514 }
515 VMap.clear();
516
517 std::unique_ptr<Module> ToNotOptimize = CloneModule(BD.getProgram(), VMap);
518 std::unique_ptr<Module> ToOptimize =
519 SplitFunctionsOutOfModule(ToNotOptimize.get(), FuncsOnClone, VMap);
520
521 // Try the extraction. If it doesn't work, then the block extractor crashed
522 // or something, in which case bugpoint can't chase down this possibility.
523 if (std::unique_ptr<Module> New =
524 BD.extractMappedBlocksFromModule(BBsOnClone, ToOptimize.get())) {
525 Expected<bool> Ret = TestFn(BD, std::move(New), std::move(ToNotOptimize));
526 BD.setNewProgram(std::move(Orig));
527 return Ret;
528 }
529 BD.setNewProgram(std::move(Orig));
530 return false;
531}
532
533/// Given a reduced list of functions that still expose the bug, extract as many
534/// basic blocks from the region as possible without obscuring the bug.
535///
536static Expected<bool>
537ExtractBlocks(BugDriver &BD,
538 Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,
539 std::unique_ptr<Module>),
540 std::vector<Function *> &MiscompiledFunctions) {
541 if (BugpointIsInterrupted)
542 return false;
543
544 std::vector<BasicBlock *> Blocks;
545 for (unsigned i = 0, e = MiscompiledFunctions.size(); i != e; ++i)
546 for (BasicBlock &BB : *MiscompiledFunctions[i])
547 Blocks.push_back(&BB);
548
549 // Use the list reducer to identify blocks that can be extracted without
550 // obscuring the bug. The Blocks list will end up containing blocks that must
551 // be retained from the original program.
552 unsigned OldSize = Blocks.size();
553
554 // Check to see if all blocks are extractible first.
555 Expected<bool> Ret = ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
556 .TestFuncs(std::vector<BasicBlock *>());
557 if (Error E = Ret.takeError())
558 return std::move(E);
559 if (*Ret) {
560 Blocks.clear();
561 } else {
562 Expected<bool> Ret =
563 ReduceMiscompiledBlocks(BD, TestFn, MiscompiledFunctions)
564 .reduceList(Blocks);
565 if (Error E = Ret.takeError())
566 return std::move(E);
567 if (Blocks.size() == OldSize)
568 return false;
569 }
570
571 ValueToValueMapTy VMap;
572 std::unique_ptr<Module> ProgClone = CloneModule(BD.getProgram(), VMap);
573 std::unique_ptr<Module> ToExtract =
574 SplitFunctionsOutOfModule(ProgClone.get(), MiscompiledFunctions, VMap);
575 std::unique_ptr<Module> Extracted =
576 BD.extractMappedBlocksFromModule(Blocks, ToExtract.get());
577 if (!Extracted) {
578 // Weird, extraction should have worked.
579 errs() << "Nondeterministic problem extracting blocks??\n";
580 return false;
581 }
582
583 // Otherwise, block extraction succeeded. Link the two program fragments back
584 // together.
585
586 std::vector<std::pair<std::string, FunctionType *>> MisCompFunctions;
587 for (Module::iterator I = Extracted->begin(), E = Extracted->end(); I != E;
588 ++I)
589 if (!I->isDeclaration())
590 MisCompFunctions.emplace_back(I->getName(), I->getFunctionType());
591
592 if (Linker::linkModules(*ProgClone, std::move(Extracted)))
593 exit(1);
594
595 // Set the new program and delete the old one.
596 BD.setNewProgram(std::move(ProgClone));
597
598 // Update the list of miscompiled functions.
599 MiscompiledFunctions.clear();
600
601 for (unsigned i = 0, e = MisCompFunctions.size(); i != e; ++i) {
602 Function *NewF = ProgClone->getFunction(MisCompFunctions[i].first);
603 assert(NewF && "Function not found??")(static_cast <bool> (NewF && "Function not found??"
) ? void (0) : __assert_fail ("NewF && \"Function not found??\""
, "/build/llvm-toolchain-snapshot-7~svn326551/tools/bugpoint/Miscompilation.cpp"
, 603, __extension__ __PRETTY_FUNCTION__))
;
604 MiscompiledFunctions.push_back(NewF);
605 }
606
607 return true;
608}
609
610/// This is a generic driver to narrow down miscompilations, either in an
611/// optimization or a code generator.
612///
613static Expected<std::vector<Function *>> DebugAMiscompilation(
614 BugDriver &BD,
615 Expected<bool> (*TestFn)(BugDriver &, std::unique_ptr<Module>,
616 std::unique_ptr<Module>)) {
617 // Okay, now that we have reduced the list of passes which are causing the
618 // failure, see if we can pin down which functions are being
619 // miscompiled... first build a list of all of the non-external functions in
620 // the program.
621 std::vector<Function *> MiscompiledFunctions;
622 Module &Prog = BD.getProgram();
623 for (Function &F : Prog)
624 if (!F.isDeclaration())
625 MiscompiledFunctions.push_back(&F);
626
627 // Do the reduction...
628 if (!BugpointIsInterrupted) {
4
Assuming 'BugpointIsInterrupted' is not equal to 0
5
Taking false branch
629 Expected<bool> Ret = ReduceMiscompilingFunctions(BD, TestFn)
630 .reduceList(MiscompiledFunctions);
631 if (Error E = Ret.takeError()) {
632 errs() << "\n***Cannot reduce functions: ";
633 return std::move(E);
634 }
635 }
636 outs() << "\n*** The following function"
637 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
6
Assuming the condition is false
7
'?' condition is false
638 << " being miscompiled: ";
639 PrintFunctionList(MiscompiledFunctions);
640 outs() << '\n';
641
642 // See if we can rip any loops out of the miscompiled functions and still
643 // trigger the problem.
644
645 if (!BugpointIsInterrupted && !DisableLoopExtraction) {
8
Assuming 'BugpointIsInterrupted' is 0
9
Assuming the condition is true
10
Taking true branch
646 Expected<bool> Ret = ExtractLoops(BD, TestFn, MiscompiledFunctions);
11
Calling 'ExtractLoops'
647 if (Error E = Ret.takeError())
648 return std::move(E);
649 if (*Ret) {
650 // Okay, we extracted some loops and the problem still appears. See if
651 // we can eliminate some of the created functions from being candidates.
652 DisambiguateGlobalSymbols(BD.getProgram());
653
654 // Do the reduction...
655 if (!BugpointIsInterrupted)
656 Ret = ReduceMiscompilingFunctions(BD, TestFn)
657 .reduceList(MiscompiledFunctions);
658 if (Error E = Ret.takeError())
659 return std::move(E);
660
661 outs() << "\n*** The following function"
662 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
663 << " being miscompiled: ";
664 PrintFunctionList(MiscompiledFunctions);
665 outs() << '\n';
666 }
667 }
668
669 if (!BugpointIsInterrupted && !DisableBlockExtraction) {
670 Expected<bool> Ret = ExtractBlocks(BD, TestFn, MiscompiledFunctions);
671 if (Error E = Ret.takeError())
672 return std::move(E);
673 if (*Ret) {
674 // Okay, we extracted some blocks and the problem still appears. See if
675 // we can eliminate some of the created functions from being candidates.
676 DisambiguateGlobalSymbols(BD.getProgram());
677
678 // Do the reduction...
679 Ret = ReduceMiscompilingFunctions(BD, TestFn)
680 .reduceList(MiscompiledFunctions);
681 if (Error E = Ret.takeError())
682 return std::move(E);
683
684 outs() << "\n*** The following function"
685 << (MiscompiledFunctions.size() == 1 ? " is" : "s are")
686 << " being miscompiled: ";
687 PrintFunctionList(MiscompiledFunctions);
688 outs() << '\n';
689 }
690 }
691
692 return MiscompiledFunctions;
693}
694
695/// This is the predicate function used to check to see if the "Test" portion of
696/// the program is misoptimized. If so, return true. In any case, both module
697/// arguments are deleted.
698///
699static Expected<bool> TestOptimizer(BugDriver &BD, std::unique_ptr<Module> Test,
700 std::unique_ptr<Module> Safe) {
701 // Run the optimization passes on ToOptimize, producing a transformed version
702 // of the functions being tested.
703 outs() << " Optimizing functions being tested: ";
704 std::unique_ptr<Module> Optimized =
705 BD.runPassesOn(Test.get(), BD.getPassesToRun());
706 if (!Optimized) {
707 errs() << " Error running this sequence of passes"
708 << " on the input program!\n";
709 BD.setNewProgram(std::move(Test));
710 BD.EmitProgressBitcode(*Test, "pass-error", false);
711 if (Error E = BD.debugOptimizerCrash())
712 return std::move(E);
713 return false;
714 }
715 outs() << "done.\n";
716
717 outs() << " Checking to see if the merged program executes correctly: ";
718 bool Broken;
719 auto Result = testMergedProgram(BD, *Optimized, *Safe, Broken);
720 if (Error E = Result.takeError())
721 return std::move(E);
722 if (auto New = std::move(*Result)) {
723 outs() << (Broken ? " nope.\n" : " yup.\n");
724 // Delete the original and set the new program.
725 BD.setNewProgram(std::move(New));
726 }
727 return Broken;
728}
729
730/// debugMiscompilation - This method is used when the passes selected are not
731/// crashing, but the generated output is semantically different from the
732/// input.
733///
734Error BugDriver::debugMiscompilation() {
735 // Make sure something was miscompiled...
736 if (!BugpointIsInterrupted) {
737 Expected<bool> Result =
738 ReduceMiscompilingPasses(*this).reduceList(PassesToRun);
739 if (Error E = Result.takeError())
740 return E;
741 if (!*Result)
742 return make_error<StringError>(
743 "*** Optimized program matches reference output! No problem"
744 " detected...\nbugpoint can't help you with your problem!\n",
745 inconvertibleErrorCode());
746 }
747
748 outs() << "\n*** Found miscompiling pass"
749 << (getPassesToRun().size() == 1 ? "" : "es") << ": "
750 << getPassesString(getPassesToRun()) << '\n';
751 EmitProgressBitcode(*Program, "passinput");
752
753 Expected<std::vector<Function *>> MiscompiledFunctions =
754 DebugAMiscompilation(*this, TestOptimizer);
755 if (Error E = MiscompiledFunctions.takeError())
756 return E;
757
758 // Output a bunch of bitcode files for the user...
759 outs() << "Outputting reduced bitcode files which expose the problem:\n";
760 ValueToValueMapTy VMap;
761 Module *ToNotOptimize = CloneModule(getProgram(), VMap).release();
762 Module *ToOptimize =
763 SplitFunctionsOutOfModule(ToNotOptimize, *MiscompiledFunctions, VMap)
764 .release();
765
766 outs() << " Non-optimized portion: ";
767 EmitProgressBitcode(*ToNotOptimize, "tonotoptimize", true);
768 delete ToNotOptimize; // Delete hacked module.
769
770 outs() << " Portion that is input to optimizer: ";
771 EmitProgressBitcode(*ToOptimize, "tooptimize");
772 delete ToOptimize; // Delete hacked module.
773
774 return Error::success();
775}
776
777/// Get the specified modules ready for code generator testing.
778///
779static void CleanupAndPrepareModules(BugDriver &BD,
780 std::unique_ptr<Module> &Test,
781 Module *Safe) {
782 // Clean up the modules, removing extra cruft that we don't need anymore...
783 Test = BD.performFinalCleanups(Test.get());
784
785 // If we are executing the JIT, we have several nasty issues to take care of.
786 if (!BD.isExecutingJIT())
787 return;
788
789 // First, if the main function is in the Safe module, we must add a stub to
790 // the Test module to call into it. Thus, we create a new function `main'
791 // which just calls the old one.
792 if (Function *oldMain = Safe->getFunction("main"))
793 if (!oldMain->isDeclaration()) {
794 // Rename it
795 oldMain->setName("llvm_bugpoint_old_main");
796 // Create a NEW `main' function with same type in the test module.
797 Function *newMain =
798 Function::Create(oldMain->getFunctionType(),
799 GlobalValue::ExternalLinkage, "main", Test.get());
800 // Create an `oldmain' prototype in the test module, which will
801 // corresponds to the real main function in the same module.
802 Function *oldMainProto = Function::Create(oldMain->getFunctionType(),
803 GlobalValue::ExternalLinkage,
804 oldMain->getName(), Test.get());
805 // Set up and remember the argument list for the main function.
806 std::vector<Value *> args;
807 for (Function::arg_iterator I = newMain->arg_begin(),
808 E = newMain->arg_end(),
809 OI = oldMain->arg_begin();
810 I != E; ++I, ++OI) {
811 I->setName(OI->getName()); // Copy argument names from oldMain
812 args.push_back(&*I);
813 }
814
815 // Call the old main function and return its result
816 BasicBlock *BB = BasicBlock::Create(Safe->getContext(), "entry", newMain);
817 CallInst *call = CallInst::Create(oldMainProto, args, "", BB);
818
819 // If the type of old function wasn't void, return value of call
820 ReturnInst::Create(Safe->getContext(), call, BB);
821 }
822
823 // The second nasty issue we must deal with in the JIT is that the Safe
824 // module cannot directly reference any functions defined in the test
825 // module. Instead, we use a JIT API call to dynamically resolve the
826 // symbol.
827
828 // Add the resolver to the Safe module.
829 // Prototype: void *getPointerToNamedFunction(const char* Name)
830 Constant *resolverFunc = Safe->getOrInsertFunction(
831 "getPointerToNamedFunction", Type::getInt8PtrTy(Safe->getContext()),
832 Type::getInt8PtrTy(Safe->getContext()));
833
834 // Use the function we just added to get addresses of functions we need.
835 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
836 if (F->isDeclaration() && !F->use_empty() && &*F != resolverFunc &&
837 !F->isIntrinsic() /* ignore intrinsics */) {
838 Function *TestFn = Test->getFunction(F->getName());
839
840 // Don't forward functions which are external in the test module too.
841 if (TestFn && !TestFn->isDeclaration()) {
842 // 1. Add a string constant with its name to the global file
843 Constant *InitArray =
844 ConstantDataArray::getString(F->getContext(), F->getName());
845 GlobalVariable *funcName = new GlobalVariable(
846 *Safe, InitArray->getType(), true /*isConstant*/,
847 GlobalValue::InternalLinkage, InitArray, F->getName() + "_name");
848
849 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
850 // sbyte* so it matches the signature of the resolver function.
851
852 // GetElementPtr *funcName, ulong 0, ulong 0
853 std::vector<Constant *> GEPargs(
854 2, Constant::getNullValue(Type::getInt32Ty(F->getContext())));
855 Value *GEP = ConstantExpr::getGetElementPtr(InitArray->getType(),
856 funcName, GEPargs);
857 std::vector<Value *> ResolverArgs;
858 ResolverArgs.push_back(GEP);
859
860 // Rewrite uses of F in global initializers, etc. to uses of a wrapper
861 // function that dynamically resolves the calls to F via our JIT API
862 if (!F->use_empty()) {
863 // Create a new global to hold the cached function pointer.
864 Constant *NullPtr = ConstantPointerNull::get(F->getType());
865 GlobalVariable *Cache = new GlobalVariable(
866 *F->getParent(), F->getType(), false,
867 GlobalValue::InternalLinkage, NullPtr, F->getName() + ".fpcache");
868
869 // Construct a new stub function that will re-route calls to F
870 FunctionType *FuncTy = F->getFunctionType();
871 Function *FuncWrapper =
872 Function::Create(FuncTy, GlobalValue::InternalLinkage,
873 F->getName() + "_wrapper", F->getParent());
874 BasicBlock *EntryBB =
875 BasicBlock::Create(F->getContext(), "entry", FuncWrapper);
876 BasicBlock *DoCallBB =
877 BasicBlock::Create(F->getContext(), "usecache", FuncWrapper);
878 BasicBlock *LookupBB =
879 BasicBlock::Create(F->getContext(), "lookupfp", FuncWrapper);
880
881 // Check to see if we already looked up the value.
882 Value *CachedVal = new LoadInst(Cache, "fpcache", EntryBB);
883 Value *IsNull = new ICmpInst(*EntryBB, ICmpInst::ICMP_EQ, CachedVal,
884 NullPtr, "isNull");
885 BranchInst::Create(LookupBB, DoCallBB, IsNull, EntryBB);
886
887 // Resolve the call to function F via the JIT API:
888 //
889 // call resolver(GetElementPtr...)
890 CallInst *Resolver = CallInst::Create(resolverFunc, ResolverArgs,
891 "resolver", LookupBB);
892
893 // Cast the result from the resolver to correctly-typed function.
894 CastInst *CastedResolver = new BitCastInst(
895 Resolver, PointerType::getUnqual(F->getFunctionType()),
896 "resolverCast", LookupBB);
897
898 // Save the value in our cache.
899 new StoreInst(CastedResolver, Cache, LookupBB);
900 BranchInst::Create(DoCallBB, LookupBB);
901
902 PHINode *FuncPtr =
903 PHINode::Create(NullPtr->getType(), 2, "fp", DoCallBB);
904 FuncPtr->addIncoming(CastedResolver, LookupBB);
905 FuncPtr->addIncoming(CachedVal, EntryBB);
906
907 // Save the argument list.
908 std::vector<Value *> Args;
909 for (Argument &A : FuncWrapper->args())
910 Args.push_back(&A);
911
912 // Pass on the arguments to the real function, return its result
913 if (F->getReturnType()->isVoidTy()) {
914 CallInst::Create(FuncPtr, Args, "", DoCallBB);
915 ReturnInst::Create(F->getContext(), DoCallBB);
916 } else {
917 CallInst *Call =
918 CallInst::Create(FuncPtr, Args, "retval", DoCallBB);
919 ReturnInst::Create(F->getContext(), Call, DoCallBB);
920 }
921
922 // Use the wrapper function instead of the old function
923 F->replaceAllUsesWith(FuncWrapper);
924 }
925 }
926 }
927 }
928
929 if (verifyModule(*Test) || verifyModule(*Safe)) {
930 errs() << "Bugpoint has a bug, which corrupted a module!!\n";
931 abort();
932 }
933}
934
935/// This is the predicate function used to check to see if the "Test" portion of
936/// the program is miscompiled by the code generator under test. If so, return
937/// true. In any case, both module arguments are deleted.
938///
939static Expected<bool> TestCodeGenerator(BugDriver &BD,
940 std::unique_ptr<Module> Test,
941 std::unique_ptr<Module> Safe) {
942 CleanupAndPrepareModules(BD, Test, Safe.get());
943
944 SmallString<128> TestModuleBC;
945 int TestModuleFD;
946 std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",
947 TestModuleFD, TestModuleBC);
948 if (EC) {
949 errs() << BD.getToolName()
950 << "Error making unique filename: " << EC.message() << "\n";
951 exit(1);
952 }
953 if (BD.writeProgramToFile(TestModuleBC.str(), TestModuleFD, *Test)) {
954 errs() << "Error writing bitcode to `" << TestModuleBC.str()
955 << "'\nExiting.";
956 exit(1);
957 }
958
959 FileRemover TestModuleBCRemover(TestModuleBC.str(), !SaveTemps);
960
961 // Make the shared library
962 SmallString<128> SafeModuleBC;
963 int SafeModuleFD;
964 EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,
965 SafeModuleBC);
966 if (EC) {
967 errs() << BD.getToolName()
968 << "Error making unique filename: " << EC.message() << "\n";
969 exit(1);
970 }
971
972 if (BD.writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, *Safe)) {
973 errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";
974 exit(1);
975 }
976
977 FileRemover SafeModuleBCRemover(SafeModuleBC.str(), !SaveTemps);
978
979 Expected<std::string> SharedObject =
980 BD.compileSharedObject(SafeModuleBC.str());
981 if (Error E = SharedObject.takeError())
982 return std::move(E);
983
984 FileRemover SharedObjectRemover(*SharedObject, !SaveTemps);
985
986 // Run the code generator on the `Test' code, loading the shared library.
987 // The function returns whether or not the new output differs from reference.
988 Expected<bool> Result =
989 BD.diffProgram(BD.getProgram(), TestModuleBC.str(), *SharedObject, false);
990 if (Error E = Result.takeError())
991 return std::move(E);
992
993 if (*Result)
994 errs() << ": still failing!\n";
995 else
996 errs() << ": didn't fail.\n";
997
998 return Result;
999}
1000
1001/// debugCodeGenerator - debug errors in LLC, LLI, or CBE.
1002///
1003Error BugDriver::debugCodeGenerator() {
1004 if ((void *)SafeInterpreter == (void *)Interpreter) {
1
Assuming the condition is false
2
Taking false branch
1005 Expected<std::string> Result =
1006 executeProgramSafely(*Program, "bugpoint.safe.out");
1007 if (Result) {
1008 outs() << "\n*** The \"safe\" i.e. 'known good' backend cannot match "
1009 << "the reference diff. This may be due to a\n front-end "
1010 << "bug or a bug in the original program, but this can also "
1011 << "happen if bugpoint isn't running the program with the "
1012 << "right flags or input.\n I left the result of executing "
1013 << "the program with the \"safe\" backend in this file for "
1014 << "you: '" << *Result << "'.\n";
1015 }
1016 return Error::success();
1017 }
1018
1019 DisambiguateGlobalSymbols(*Program);
1020
1021 Expected<std::vector<Function *>> Funcs =
1022 DebugAMiscompilation(*this, TestCodeGenerator);
3
Calling 'DebugAMiscompilation'
1023 if (Error E = Funcs.takeError())
1024 return E;
1025
1026 // Split the module into the two halves of the program we want.
1027 ValueToValueMapTy VMap;
1028 std::unique_ptr<Module> ToNotCodeGen = CloneModule(getProgram(), VMap);
1029 std::unique_ptr<Module> ToCodeGen =
1030 SplitFunctionsOutOfModule(ToNotCodeGen.get(), *Funcs, VMap);
1031
1032 // Condition the modules
1033 CleanupAndPrepareModules(*this, ToCodeGen, ToNotCodeGen.get());
1034
1035 SmallString<128> TestModuleBC;
1036 int TestModuleFD;
1037 std::error_code EC = sys::fs::createTemporaryFile("bugpoint.test", "bc",
1038 TestModuleFD, TestModuleBC);
1039 if (EC) {
1040 errs() << getToolName() << "Error making unique filename: " << EC.message()
1041 << "\n";
1042 exit(1);
1043 }
1044
1045 if (writeProgramToFile(TestModuleBC.str(), TestModuleFD, *ToCodeGen)) {
1046 errs() << "Error writing bitcode to `" << TestModuleBC << "'\nExiting.";
1047 exit(1);
1048 }
1049
1050 // Make the shared library
1051 SmallString<128> SafeModuleBC;
1052 int SafeModuleFD;
1053 EC = sys::fs::createTemporaryFile("bugpoint.safe", "bc", SafeModuleFD,
1054 SafeModuleBC);
1055 if (EC) {
1056 errs() << getToolName() << "Error making unique filename: " << EC.message()
1057 << "\n";
1058 exit(1);
1059 }
1060
1061 if (writeProgramToFile(SafeModuleBC.str(), SafeModuleFD, *ToNotCodeGen)) {
1062 errs() << "Error writing bitcode to `" << SafeModuleBC << "'\nExiting.";
1063 exit(1);
1064 }
1065 Expected<std::string> SharedObject = compileSharedObject(SafeModuleBC.str());
1066 if (Error E = SharedObject.takeError())
1067 return E;
1068
1069 outs() << "You can reproduce the problem with the command line: \n";
1070 if (isExecutingJIT()) {
1071 outs() << " lli -load " << *SharedObject << " " << TestModuleBC;
1072 } else {
1073 outs() << " llc " << TestModuleBC << " -o " << TestModuleBC << ".s\n";
1074 outs() << " cc " << *SharedObject << " " << TestModuleBC.str() << ".s -o "
1075 << TestModuleBC << ".exe\n";
1076 outs() << " ./" << TestModuleBC << ".exe";
1077 }
1078 for (unsigned i = 0, e = InputArgv.size(); i != e; ++i)
1079 outs() << " " << InputArgv[i];
1080 outs() << '\n';
1081 outs() << "The shared object was created with:\n llc -march=c "
1082 << SafeModuleBC.str() << " -o temporary.c\n"
1083 << " cc -xc temporary.c -O2 -o " << *SharedObject;
1084 if (TargetTriple.getArch() == Triple::sparc)
1085 outs() << " -G"; // Compile a shared library, `-G' for Sparc
1086 else
1087 outs() << " -fPIC -shared"; // `-shared' for Linux/X86, maybe others
1088
1089 outs() << " -fno-strict-aliasing\n";
1090
1091 return Error::success();
1092}

/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h

1// unique_ptr implementation -*- C++ -*-
2
3// Copyright (C) 2008-2017 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/unique_ptr.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{memory}
28 */
29
30#ifndef _UNIQUE_PTR_H1
31#define _UNIQUE_PTR_H1 1
32
33#include <bits/c++config.h>
34#include <debug/assertions.h>
35#include <type_traits>
36#include <utility>
37#include <tuple>
38#include <bits/stl_function.h>
39#include <bits/functional_hash.h>
40
41namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
42{
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
44
45 /**
46 * @addtogroup pointer_abstractions
47 * @{
48 */
49
50#if _GLIBCXX_USE_DEPRECATED1
51 template<typename> class auto_ptr;
52#endif
53
54 /// Primary template of default_delete, used by unique_ptr
55 template<typename _Tp>
56 struct default_delete
57 {
58 /// Default constructor
59 constexpr default_delete() noexcept = default;
60
61 /** @brief Converting constructor.
62 *
63 * Allows conversion from a deleter for arrays of another type, @p _Up,
64 * only if @p _Up* is convertible to @p _Tp*.
65 */
66 template<typename _Up, typename = typename
67 enable_if<is_convertible<_Up*, _Tp*>::value>::type>
68 default_delete(const default_delete<_Up>&) noexcept { }
69
70 /// Calls @c delete @p __ptr
71 void
72 operator()(_Tp* __ptr) const
73 {
74 static_assert(!is_void<_Tp>::value,
75 "can't delete pointer to incomplete type");
76 static_assert(sizeof(_Tp)>0,
77 "can't delete pointer to incomplete type");
78 delete __ptr;
79 }
80 };
81
82 // _GLIBCXX_RESOLVE_LIB_DEFECTS
83 // DR 740 - omit specialization for array objects with a compile time length
84 /// Specialization for arrays, default_delete.
85 template<typename _Tp>
86 struct default_delete<_Tp[]>
87 {
88 public:
89 /// Default constructor
90 constexpr default_delete() noexcept = default;
91
92 /** @brief Converting constructor.
93 *
94 * Allows conversion from a deleter for arrays of another type, such as
95 * a const-qualified version of @p _Tp.
96 *
97 * Conversions from types derived from @c _Tp are not allowed because
98 * it is unsafe to @c delete[] an array of derived types through a
99 * pointer to the base type.
100 */
101 template<typename _Up, typename = typename
102 enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type>
103 default_delete(const default_delete<_Up[]>&) noexcept { }
104
105 /// Calls @c delete[] @p __ptr
106 template<typename _Up>
107 typename enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value>::type
108 operator()(_Up* __ptr) const
109 {
110 static_assert(sizeof(_Tp)>0,
111 "can't delete pointer to incomplete type");
112 delete [] __ptr;
113 }
114 };
115
116 template <typename _Tp, typename _Dp>
117 class __uniq_ptr_impl
118 {
119 template <typename _Up, typename _Ep, typename = void>
120 struct _Ptr
121 {
122 using type = _Up*;
123 };
124
125 template <typename _Up, typename _Ep>
126 struct
127 _Ptr<_Up, _Ep, __void_t<typename remove_reference<_Ep>::type::pointer>>
128 {
129 using type = typename remove_reference<_Ep>::type::pointer;
130 };
131
132 public:
133 using _DeleterConstraint = enable_if<
134 __and_<__not_<is_pointer<_Dp>>,
135 is_default_constructible<_Dp>>::value>;
136
137 using pointer = typename _Ptr<_Tp, _Dp>::type;
138
139 __uniq_ptr_impl() = default;
140 __uniq_ptr_impl(pointer __p) : _M_t() { _M_ptr() = __p; }
141
142 template<typename _Del>
143 __uniq_ptr_impl(pointer __p, _Del&& __d)
144 : _M_t(__p, std::forward<_Del>(__d)) { }
57
Calling 'forward'
58
Returning from 'forward'
59
Calling constructor for 'tuple'
82
Returning from constructor for 'tuple'
139
Calling 'forward'
140
Returning from 'forward'
141
Calling constructor for 'tuple'
164
Returning from constructor for 'tuple'
233
Calling 'forward'
234
Returning from 'forward'
235
Calling constructor for 'tuple'
258
Returning from constructor for 'tuple'
438
Calling 'forward'
439
Returning from 'forward'
440
Calling constructor for 'tuple'
463
Returning from constructor for 'tuple'
145
146 pointer& _M_ptr() { return std::get<0>(_M_t); }
32
Calling 'get'
39
Returning from 'get'
114
Calling 'get'
121
Returning from 'get'
208
Calling 'get'
215
Returning from 'get'
413
Calling 'get'
420
Returning from 'get'
482
Calling 'get'
489
Returning from 'get'
147 pointer _M_ptr() const { return std::get<0>(_M_t); }
21
Calling 'get'
28
Returning from 'get'
88
Calling 'get'
95
Returning from 'get'
103
Calling 'get'
110
Returning from 'get'
172
Calling 'get'
179
Returning from 'get'
197
Calling 'get'
204
Returning from 'get'
402
Calling 'get'
409
Returning from 'get'
148 _Dp& _M_deleter() { return std::get<1>(_M_t); }
44
Calling 'get'
51
Returning from 'get'
126
Calling 'get'
133
Returning from 'get'
220
Calling 'get'
227
Returning from 'get'
425
Calling 'get'
432
Returning from 'get'
494
Calling 'get'
501
Returning from 'get'
149 const _Dp& _M_deleter() const { return std::get<1>(_M_t); }
150
151 private:
152 tuple<pointer, _Dp> _M_t;
153 };
154
155 /// 20.7.1.2 unique_ptr for single objects.
156 template <typename _Tp, typename _Dp = default_delete<_Tp>>
157 class unique_ptr
158 {
159 template <class _Up>
160 using _DeleterConstraint =
161 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
162
163 __uniq_ptr_impl<_Tp, _Dp> _M_t;
164
165 public:
166 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
167 using element_type = _Tp;
168 using deleter_type = _Dp;
169
170 // helper template for detecting a safe conversion from another
171 // unique_ptr
172 template<typename _Up, typename _Ep>
173 using __safe_conversion_up = __and_<
174 is_convertible<typename unique_ptr<_Up, _Ep>::pointer, pointer>,
175 __not_<is_array<_Up>>,
176 __or_<__and_<is_reference<deleter_type>,
177 is_same<deleter_type, _Ep>>,
178 __and_<__not_<is_reference<deleter_type>>,
179 is_convertible<_Ep, deleter_type>>
180 >
181 >;
182
183 // Constructors.
184
185 /// Default constructor, creates a unique_ptr that owns nothing.
186 template <typename _Up = _Dp,
187 typename = _DeleterConstraint<_Up>>
188 constexpr unique_ptr() noexcept
189 : _M_t()
190 { }
191
192 /** Takes ownership of a pointer.
193 *
194 * @param __p A pointer to an object of @c element_type
195 *
196 * The deleter will be value-initialized.
197 */
198 template <typename _Up = _Dp,
199 typename = _DeleterConstraint<_Up>>
200 explicit
201 unique_ptr(pointer __p) noexcept
202 : _M_t(__p)
203 { }
204
205 /** Takes ownership of a pointer.
206 *
207 * @param __p A pointer to an object of @c element_type
208 * @param __d A reference to a deleter.
209 *
210 * The deleter will be initialized with @p __d
211 */
212 unique_ptr(pointer __p,
213 typename conditional<is_reference<deleter_type>::value,
214 deleter_type, const deleter_type&>::type __d) noexcept
215 : _M_t(__p, __d) { }
216
217 /** Takes ownership of a pointer.
218 *
219 * @param __p A pointer to an object of @c element_type
220 * @param __d An rvalue reference to a deleter.
221 *
222 * The deleter will be initialized with @p std::move(__d)
223 */
224 unique_ptr(pointer __p,
225 typename remove_reference<deleter_type>::type&& __d) noexcept
226 : _M_t(std::move(__p), std::move(__d))
227 { static_assert(!std::is_reference<deleter_type>::value,
228 "rvalue deleter bound to reference"); }
229
230 /// Creates a unique_ptr that owns nothing.
231 template <typename _Up = _Dp,
232 typename = _DeleterConstraint<_Up>>
233 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
234
235 // Move constructors.
236
237 /// Move constructor.
238 unique_ptr(unique_ptr&& __u) noexcept
239 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
18
Calling 'unique_ptr::release'
41
Returning from 'unique_ptr::release'
42
Calling 'unique_ptr::get_deleter'
53
Returning from 'unique_ptr::get_deleter'
54
Calling 'forward'
55
Returning from 'forward'
56
Calling constructor for '__uniq_ptr_impl'
83
Returning from constructor for '__uniq_ptr_impl'
100
Calling 'unique_ptr::release'
123
Returning from 'unique_ptr::release'
124
Calling 'unique_ptr::get_deleter'
135
Returning from 'unique_ptr::get_deleter'
136
Calling 'forward'
137
Returning from 'forward'
138
Calling constructor for '__uniq_ptr_impl'
165
Returning from constructor for '__uniq_ptr_impl'
194
Calling 'unique_ptr::release'
217
Returning from 'unique_ptr::release'
218
Calling 'unique_ptr::get_deleter'
229
Returning from 'unique_ptr::get_deleter'
230
Calling 'forward'
231
Returning from 'forward'
232
Calling constructor for '__uniq_ptr_impl'
259
Returning from constructor for '__uniq_ptr_impl'
399
Calling 'unique_ptr::release'
422
Returning from 'unique_ptr::release'
423
Calling 'unique_ptr::get_deleter'
434
Returning from 'unique_ptr::get_deleter'
435
Calling 'forward'
436
Returning from 'forward'
437
Calling constructor for '__uniq_ptr_impl'
464
Returning from constructor for '__uniq_ptr_impl'
240
241 /** @brief Converting constructor from another type
242 *
243 * Requires that the pointer owned by @p __u is convertible to the
244 * type of pointer owned by this object, @p __u does not own an array,
245 * and @p __u has a compatible deleter type.
246 */
247 template<typename _Up, typename _Ep, typename = _Require<
248 __safe_conversion_up<_Up, _Ep>,
249 typename conditional<is_reference<_Dp>::value,
250 is_same<_Ep, _Dp>,
251 is_convertible<_Ep, _Dp>>::type>>
252 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
253 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
254 { }
255
256#if _GLIBCXX_USE_DEPRECATED1
257 /// Converting constructor from @c auto_ptr
258 template<typename _Up, typename = _Require<
259 is_convertible<_Up*, _Tp*>, is_same<_Dp, default_delete<_Tp>>>>
260 unique_ptr(auto_ptr<_Up>&& __u) noexcept;
261#endif
262
263 /// Destructor, invokes the deleter if the stored pointer is not null.
264 ~unique_ptr() noexcept
265 {
266 auto& __ptr = _M_t._M_ptr();
481
Calling '__uniq_ptr_impl::_M_ptr'
490
Returning from '__uniq_ptr_impl::_M_ptr'
267 if (__ptr != nullptr)
491
Taking true branch
268 get_deleter()(__ptr);
492
Calling 'unique_ptr::get_deleter'
503
Returning from 'unique_ptr::get_deleter'
504
Calling 'default_delete::operator()'
505
Returning from 'default_delete::operator()'
269 __ptr = pointer();
270 }
271
272 // Assignment.
273
274 /** @brief Move assignment operator.
275 *
276 * @param __u The object to transfer ownership from.
277 *
278 * Invokes the deleter first if this object owns a pointer.
279 */
280 unique_ptr&
281 operator=(unique_ptr&& __u) noexcept
282 {
283 reset(__u.release());
284 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
285 return *this;
286 }
287
288 /** @brief Assignment from another type.
289 *
290 * @param __u The object to transfer ownership from, which owns a
291 * convertible pointer to a non-array object.
292 *
293 * Invokes the deleter first if this object owns a pointer.
294 */
295 template<typename _Up, typename _Ep>
296 typename enable_if< __and_<
297 __safe_conversion_up<_Up, _Ep>,
298 is_assignable<deleter_type&, _Ep&&>
299 >::value,
300 unique_ptr&>::type
301 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
302 {
303 reset(__u.release());
304 get_deleter() = std::forward<_Ep>(__u.get_deleter());
305 return *this;
306 }
307
308 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
309 unique_ptr&
310 operator=(nullptr_t) noexcept
311 {
312 reset();
313 return *this;
314 }
315
316 // Observers.
317
318 /// Dereference the stored pointer.
319 typename add_lvalue_reference<element_type>::type
320 operator*() const
321 {
322 __glibcxx_assert(get() != pointer());
323 return *get();
86
Calling 'unique_ptr::get'
97
Returning from 'unique_ptr::get'
170
Calling 'unique_ptr::get'
181
Returning from 'unique_ptr::get'
324 }
325
326 /// Return the stored pointer.
327 pointer
328 operator->() const noexcept
329 {
330 _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
331 return get();
332 }
333
334 /// Return the stored pointer.
335 pointer
336 get() const noexcept
337 { return _M_t._M_ptr(); }
20
Calling '__uniq_ptr_impl::_M_ptr'
29
Returning from '__uniq_ptr_impl::_M_ptr'
87
Calling '__uniq_ptr_impl::_M_ptr'
96
Returning from '__uniq_ptr_impl::_M_ptr'
102
Calling '__uniq_ptr_impl::_M_ptr'
111
Returning from '__uniq_ptr_impl::_M_ptr'
171
Calling '__uniq_ptr_impl::_M_ptr'
180
Returning from '__uniq_ptr_impl::_M_ptr'
196
Calling '__uniq_ptr_impl::_M_ptr'
205
Returning from '__uniq_ptr_impl::_M_ptr'
401
Calling '__uniq_ptr_impl::_M_ptr'
410
Returning from '__uniq_ptr_impl::_M_ptr'
338
339 /// Return a reference to the stored deleter.
340 deleter_type&
341 get_deleter() noexcept
342 { return _M_t._M_deleter(); }
43
Calling '__uniq_ptr_impl::_M_deleter'
52
Returning from '__uniq_ptr_impl::_M_deleter'
125
Calling '__uniq_ptr_impl::_M_deleter'
134
Returning from '__uniq_ptr_impl::_M_deleter'
219
Calling '__uniq_ptr_impl::_M_deleter'
228
Returning from '__uniq_ptr_impl::_M_deleter'
424
Calling '__uniq_ptr_impl::_M_deleter'
433
Returning from '__uniq_ptr_impl::_M_deleter'
493
Calling '__uniq_ptr_impl::_M_deleter'
502
Returning from '__uniq_ptr_impl::_M_deleter'
343
344 /// Return a reference to the stored deleter.
345 const deleter_type&
346 get_deleter() const noexcept
347 { return _M_t._M_deleter(); }
348
349 /// Return @c true if the stored pointer is not null.
350 explicit operator bool() const noexcept
351 { return get() == pointer() ? false : true; }
352
353 // Modifiers.
354
355 /// Release ownership of any stored pointer.
356 pointer
357 release() noexcept
358 {
359 pointer __p = get();
19
Calling 'unique_ptr::get'
30
Returning from 'unique_ptr::get'
101
Calling 'unique_ptr::get'
112
Returning from 'unique_ptr::get'
195
Calling 'unique_ptr::get'
206
Returning from 'unique_ptr::get'
400
Calling 'unique_ptr::get'
411
Returning from 'unique_ptr::get'
360 _M_t._M_ptr() = pointer();
31
Calling '__uniq_ptr_impl::_M_ptr'
40
Returning from '__uniq_ptr_impl::_M_ptr'
113
Calling '__uniq_ptr_impl::_M_ptr'
122
Returning from '__uniq_ptr_impl::_M_ptr'
207
Calling '__uniq_ptr_impl::_M_ptr'
216
Returning from '__uniq_ptr_impl::_M_ptr'
412
Calling '__uniq_ptr_impl::_M_ptr'
421
Returning from '__uniq_ptr_impl::_M_ptr'
361 return __p;
362 }
363
364 /** @brief Replace the stored pointer.
365 *
366 * @param __p The new pointer to store.
367 *
368 * The deleter will be invoked if a pointer is already owned.
369 */
370 void
371 reset(pointer __p = pointer()) noexcept
372 {
373 using std::swap;
374 swap(_M_t._M_ptr(), __p);
375 if (__p != pointer())
376 get_deleter()(__p);
377 }
378
379 /// Exchange the pointer and deleter with another object.
380 void
381 swap(unique_ptr& __u) noexcept
382 {
383 using std::swap;
384 swap(_M_t, __u._M_t);
385 }
386
387 // Disable copy from lvalue.
388 unique_ptr(const unique_ptr&) = delete;
389 unique_ptr& operator=(const unique_ptr&) = delete;
390 };
391
392 /// 20.7.1.3 unique_ptr for array objects with a runtime length
393 // [unique.ptr.runtime]
394 // _GLIBCXX_RESOLVE_LIB_DEFECTS
395 // DR 740 - omit specialization for array objects with a compile time length
396 template<typename _Tp, typename _Dp>
397 class unique_ptr<_Tp[], _Dp>
398 {
399 template <typename _Up>
400 using _DeleterConstraint =
401 typename __uniq_ptr_impl<_Tp, _Up>::_DeleterConstraint::type;
402
403 __uniq_ptr_impl<_Tp, _Dp> _M_t;
404
405 template<typename _Up>
406 using __remove_cv = typename remove_cv<_Up>::type;
407
408 // like is_base_of<_Tp, _Up> but false if unqualified types are the same
409 template<typename _Up>
410 using __is_derived_Tp
411 = __and_< is_base_of<_Tp, _Up>,
412 __not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
413
414 public:
415 using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
416 using element_type = _Tp;
417 using deleter_type = _Dp;
418
419 // helper template for detecting a safe conversion from another
420 // unique_ptr
421 template<typename _Up, typename _Ep,
422 typename _Up_up = unique_ptr<_Up, _Ep>,
423 typename _Up_element_type = typename _Up_up::element_type>
424 using __safe_conversion_up = __and_<
425 is_array<_Up>,
426 is_same<pointer, element_type*>,
427 is_same<typename _Up_up::pointer, _Up_element_type*>,
428 is_convertible<_Up_element_type(*)[], element_type(*)[]>,
429 __or_<__and_<is_reference<deleter_type>, is_same<deleter_type, _Ep>>,
430 __and_<__not_<is_reference<deleter_type>>,
431 is_convertible<_Ep, deleter_type>>>
432 >;
433
434 // helper template for detecting a safe conversion from a raw pointer
435 template<typename _Up>
436 using __safe_conversion_raw = __and_<
437 __or_<__or_<is_same<_Up, pointer>,
438 is_same<_Up, nullptr_t>>,
439 __and_<is_pointer<_Up>,
440 is_same<pointer, element_type*>,
441 is_convertible<
442 typename remove_pointer<_Up>::type(*)[],
443 element_type(*)[]>
444 >
445 >
446 >;
447
448 // Constructors.
449
450 /// Default constructor, creates a unique_ptr that owns nothing.
451 template <typename _Up = _Dp,
452 typename = _DeleterConstraint<_Up>>
453 constexpr unique_ptr() noexcept
454 : _M_t()
455 { }
456
457 /** Takes ownership of a pointer.
458 *
459 * @param __p A pointer to an array of a type safely convertible
460 * to an array of @c element_type
461 *
462 * The deleter will be value-initialized.
463 */
464 template<typename _Up,
465 typename _Vp = _Dp,
466 typename = _DeleterConstraint<_Vp>,
467 typename = typename enable_if<
468 __safe_conversion_raw<_Up>::value, bool>::type>
469 explicit
470 unique_ptr(_Up __p) noexcept
471 : _M_t(__p)
472 { }
473
474 /** Takes ownership of a pointer.
475 *
476 * @param __p A pointer to an array of a type safely convertible
477 * to an array of @c element_type
478 * @param __d A reference to a deleter.
479 *
480 * The deleter will be initialized with @p __d
481 */
482 template<typename _Up,
483 typename = typename enable_if<
484 __safe_conversion_raw<_Up>::value, bool>::type>
485 unique_ptr(_Up __p,
486 typename conditional<is_reference<deleter_type>::value,
487 deleter_type, const deleter_type&>::type __d) noexcept
488 : _M_t(__p, __d) { }
489
490 /** Takes ownership of a pointer.
491 *
492 * @param __p A pointer to an array of a type safely convertible
493 * to an array of @c element_type
494 * @param __d A reference to a deleter.
495 *
496 * The deleter will be initialized with @p std::move(__d)
497 */
498 template<typename _Up,
499 typename = typename enable_if<
500 __safe_conversion_raw<_Up>::value, bool>::type>
501 unique_ptr(_Up __p, typename
502 remove_reference<deleter_type>::type&& __d) noexcept
503 : _M_t(std::move(__p), std::move(__d))
504 { static_assert(!is_reference<deleter_type>::value,
505 "rvalue deleter bound to reference"); }
506
507 /// Move constructor.
508 unique_ptr(unique_ptr&& __u) noexcept
509 : _M_t(__u.release(), std::forward<deleter_type>(__u.get_deleter())) { }
510
511 /// Creates a unique_ptr that owns nothing.
512 template <typename _Up = _Dp,
513 typename = _DeleterConstraint<_Up>>
514 constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
515
516 template<typename _Up, typename _Ep,
517 typename = _Require<__safe_conversion_up<_Up, _Ep>>>
518 unique_ptr(unique_ptr<_Up, _Ep>&& __u) noexcept
519 : _M_t(__u.release(), std::forward<_Ep>(__u.get_deleter()))
520 { }
521
522 /// Destructor, invokes the deleter if the stored pointer is not null.
523 ~unique_ptr()
524 {
525 auto& __ptr = _M_t._M_ptr();
526 if (__ptr != nullptr)
527 get_deleter()(__ptr);
528 __ptr = pointer();
529 }
530
531 // Assignment.
532
533 /** @brief Move assignment operator.
534 *
535 * @param __u The object to transfer ownership from.
536 *
537 * Invokes the deleter first if this object owns a pointer.
538 */
539 unique_ptr&
540 operator=(unique_ptr&& __u) noexcept
541 {
542 reset(__u.release());
543 get_deleter() = std::forward<deleter_type>(__u.get_deleter());
544 return *this;
545 }
546
547 /** @brief Assignment from another type.
548 *
549 * @param __u The object to transfer ownership from, which owns a
550 * convertible pointer to an array object.
551 *
552 * Invokes the deleter first if this object owns a pointer.
553 */
554 template<typename _Up, typename _Ep>
555 typename
556 enable_if<__and_<__safe_conversion_up<_Up, _Ep>,
557 is_assignable<deleter_type&, _Ep&&>
558 >::value,
559 unique_ptr&>::type
560 operator=(unique_ptr<_Up, _Ep>&& __u) noexcept
561 {
562 reset(__u.release());
563 get_deleter() = std::forward<_Ep>(__u.get_deleter());
564 return *this;
565 }
566
567 /// Reset the %unique_ptr to empty, invoking the deleter if necessary.
568 unique_ptr&
569 operator=(nullptr_t) noexcept
570 {
571 reset();
572 return *this;
573 }
574
575 // Observers.
576
577 /// Access an element of owned array.
578 typename std::add_lvalue_reference<element_type>::type
579 operator[](size_t __i) const
580 {
581 __glibcxx_assert(get() != pointer());
582 return get()[__i];
583 }
584
585 /// Return the stored pointer.
586 pointer
587 get() const noexcept
588 { return _M_t._M_ptr(); }
589
590 /// Return a reference to the stored deleter.
591 deleter_type&
592 get_deleter() noexcept
593 { return _M_t._M_deleter(); }
594
595 /// Return a reference to the stored deleter.
596 const deleter_type&
597 get_deleter() const noexcept
598 { return _M_t._M_deleter(); }
599
600 /// Return @c true if the stored pointer is not null.
601 explicit operator bool() const noexcept
602 { return get() == pointer() ? false : true; }
603
604 // Modifiers.
605
606 /// Release ownership of any stored pointer.
607 pointer
608 release() noexcept
609 {
610 pointer __p = get();
611 _M_t._M_ptr() = pointer();
612 return __p;
613 }
614
615 /** @brief Replace the stored pointer.
616 *
617 * @param __p The new pointer to store.
618 *
619 * The deleter will be invoked if a pointer is already owned.
620 */
621 template <typename _Up,
622 typename = _Require<
623 __or_<is_same<_Up, pointer>,
624 __and_<is_same<pointer, element_type*>,
625 is_pointer<_Up>,
626 is_convertible<
627 typename remove_pointer<_Up>::type(*)[],
628 element_type(*)[]
629 >
630 >
631 >
632 >>
633 void
634 reset(_Up __p) noexcept
635 {
636 pointer __ptr = __p;
637 using std::swap;
638 swap(_M_t._M_ptr(), __ptr);
639 if (__ptr != nullptr)
640 get_deleter()(__ptr);
641 }
642
643 void reset(nullptr_t = nullptr) noexcept
644 {
645 reset(pointer());
646 }
647
648 /// Exchange the pointer and deleter with another object.
649 void
650 swap(unique_ptr& __u) noexcept
651 {
652 using std::swap;
653 swap(_M_t, __u._M_t);
654 }
655
656 // Disable copy from lvalue.
657 unique_ptr(const unique_ptr&) = delete;
658 unique_ptr& operator=(const unique_ptr&) = delete;
659 };
660
661 template<typename _Tp, typename _Dp>
662 inline
663#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
664 // Constrained free swap overload, see p0185r1
665 typename enable_if<__is_swappable<_Dp>::value>::type
666#else
667 void
668#endif
669 swap(unique_ptr<_Tp, _Dp>& __x,
670 unique_ptr<_Tp, _Dp>& __y) noexcept
671 { __x.swap(__y); }
672
673#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
674 template<typename _Tp, typename _Dp>
675 typename enable_if<!__is_swappable<_Dp>::value>::type
676 swap(unique_ptr<_Tp, _Dp>&,
677 unique_ptr<_Tp, _Dp>&) = delete;
678#endif
679
680 template<typename _Tp, typename _Dp,
681 typename _Up, typename _Ep>
682 inline bool
683 operator==(const unique_ptr<_Tp, _Dp>& __x,
684 const unique_ptr<_Up, _Ep>& __y)
685 { return __x.get() == __y.get(); }
686
687 template<typename _Tp, typename _Dp>
688 inline bool
689 operator==(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
690 { return !__x; }
691
692 template<typename _Tp, typename _Dp>
693 inline bool
694 operator==(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
695 { return !__x; }
696
697 template<typename _Tp, typename _Dp,
698 typename _Up, typename _Ep>
699 inline bool
700 operator!=(const unique_ptr<_Tp, _Dp>& __x,
701 const unique_ptr<_Up, _Ep>& __y)
702 { return __x.get() != __y.get(); }
703
704 template<typename _Tp, typename _Dp>
705 inline bool
706 operator!=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t) noexcept
707 { return (bool)__x; }
708
709 template<typename _Tp, typename _Dp>
710 inline bool
711 operator!=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x) noexcept
712 { return (bool)__x; }
713
714 template<typename _Tp, typename _Dp,
715 typename _Up, typename _Ep>
716 inline bool
717 operator<(const unique_ptr<_Tp, _Dp>& __x,
718 const unique_ptr<_Up, _Ep>& __y)
719 {
720 typedef typename
721 std::common_type<typename unique_ptr<_Tp, _Dp>::pointer,
722 typename unique_ptr<_Up, _Ep>::pointer>::type _CT;
723 return std::less<_CT>()(__x.get(), __y.get());
724 }
725
726 template<typename _Tp, typename _Dp>
727 inline bool
728 operator<(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
729 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
730 nullptr); }
731
732 template<typename _Tp, typename _Dp>
733 inline bool
734 operator<(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
735 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
736 __x.get()); }
737
738 template<typename _Tp, typename _Dp,
739 typename _Up, typename _Ep>
740 inline bool
741 operator<=(const unique_ptr<_Tp, _Dp>& __x,
742 const unique_ptr<_Up, _Ep>& __y)
743 { return !(__y < __x); }
744
745 template<typename _Tp, typename _Dp>
746 inline bool
747 operator<=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
748 { return !(nullptr < __x); }
749
750 template<typename _Tp, typename _Dp>
751 inline bool
752 operator<=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
753 { return !(__x < nullptr); }
754
755 template<typename _Tp, typename _Dp,
756 typename _Up, typename _Ep>
757 inline bool
758 operator>(const unique_ptr<_Tp, _Dp>& __x,
759 const unique_ptr<_Up, _Ep>& __y)
760 { return (__y < __x); }
761
762 template<typename _Tp, typename _Dp>
763 inline bool
764 operator>(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
765 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(nullptr,
766 __x.get()); }
767
768 template<typename _Tp, typename _Dp>
769 inline bool
770 operator>(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
771 { return std::less<typename unique_ptr<_Tp, _Dp>::pointer>()(__x.get(),
772 nullptr); }
773
774 template<typename _Tp, typename _Dp,
775 typename _Up, typename _Ep>
776 inline bool
777 operator>=(const unique_ptr<_Tp, _Dp>& __x,
778 const unique_ptr<_Up, _Ep>& __y)
779 { return !(__x < __y); }
780
781 template<typename _Tp, typename _Dp>
782 inline bool
783 operator>=(const unique_ptr<_Tp, _Dp>& __x, nullptr_t)
784 { return !(__x < nullptr); }
785
786 template<typename _Tp, typename _Dp>
787 inline bool
788 operator>=(nullptr_t, const unique_ptr<_Tp, _Dp>& __x)
789 { return !(nullptr < __x); }
790
791 /// std::hash specialization for unique_ptr.
792 template<typename _Tp, typename _Dp>
793 struct hash<unique_ptr<_Tp, _Dp>>
794 : public __hash_base<size_t, unique_ptr<_Tp, _Dp>>,
795 private __poison_hash<typename unique_ptr<_Tp, _Dp>::pointer>
796 {
797 size_t
798 operator()(const unique_ptr<_Tp, _Dp>& __u) const noexcept
799 {
800 typedef unique_ptr<_Tp, _Dp> _UP;
801 return std::hash<typename _UP::pointer>()(__u.get());
802 }
803 };
804
805#if __cplusplus201103L > 201103L
806
807#define __cpp_lib_make_unique 201304
808
809 template<typename _Tp>
810 struct _MakeUniq
811 { typedef unique_ptr<_Tp> __single_object; };
812
813 template<typename _Tp>
814 struct _MakeUniq<_Tp[]>
815 { typedef unique_ptr<_Tp[]> __array; };
816
817 template<typename _Tp, size_t _Bound>
818 struct _MakeUniq<_Tp[_Bound]>
819 { struct __invalid_type { }; };
820
821 /// std::make_unique for single objects
822 template<typename _Tp, typename... _Args>
823 inline typename _MakeUniq<_Tp>::__single_object
824 make_unique(_Args&&... __args)
825 { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); }
826
827 /// std::make_unique for arrays of unknown bound
828 template<typename _Tp>
829 inline typename _MakeUniq<_Tp>::__array
830 make_unique(size_t __num)
831 { return unique_ptr<_Tp>(new remove_extent_t<_Tp>[__num]()); }
832
833 /// Disable std::make_unique for arrays of known bound
834 template<typename _Tp, typename... _Args>
835 inline typename _MakeUniq<_Tp>::__invalid_type
836 make_unique(_Args&&...) = delete;
837#endif
838
839 // @} group pointer_abstractions
840
841_GLIBCXX_END_NAMESPACE_VERSION
842} // namespace
843
844#endif /* _UNIQUE_PTR_H */

/usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/tuple

1// <tuple> -*- C++ -*-
2
3// Copyright (C) 2007-2017 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file include/tuple
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _GLIBCXX_TUPLE1
30#define _GLIBCXX_TUPLE1 1
31
32#pragma GCC system_header
33
34#if __cplusplus201103L < 201103L
35# include <bits/c++0x_warning.h>
36#else
37
38#include <utility>
39#include <array>
40#include <bits/uses_allocator.h>
41#include <bits/invoke.h>
42
43namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default")))
44{
45_GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * @addtogroup utilities
49 * @{
50 */
51
52 template<typename... _Elements>
53 class tuple;
54
55 template<typename _Tp>
56 struct __is_empty_non_tuple : is_empty<_Tp> { };
57
58 // Using EBO for elements that are tuples causes ambiguous base errors.
59 template<typename _El0, typename... _El>
60 struct __is_empty_non_tuple<tuple<_El0, _El...>> : false_type { };
61
62 // Use the Empty Base-class Optimization for empty, non-final types.
63 template<typename _Tp>
64 using __empty_not_final
65 = typename conditional<__is_final(_Tp), false_type,
66 __is_empty_non_tuple<_Tp>>::type;
67
68 template<std::size_t _Idx, typename _Head,
69 bool = __empty_not_final<_Head>::value>
70 struct _Head_base;
71
72 template<std::size_t _Idx, typename _Head>
73 struct _Head_base<_Idx, _Head, true>
74 : public _Head
75 {
76 constexpr _Head_base()
77 : _Head() { }
78
79 constexpr _Head_base(const _Head& __h)
80 : _Head(__h) { }
81
82 constexpr _Head_base(const _Head_base&) = default;
83 constexpr _Head_base(_Head_base&&) = default;
84
85 template<typename _UHead>
86 constexpr _Head_base(_UHead&& __h)
87 : _Head(std::forward<_UHead>(__h)) { }
71
Calling 'forward'
72
Returning from 'forward'
153
Calling 'forward'
154
Returning from 'forward'
247
Calling 'forward'
248
Returning from 'forward'
452
Calling 'forward'
453
Returning from 'forward'
88
89 _Head_base(allocator_arg_t, __uses_alloc0)
90 : _Head() { }
91
92 template<typename _Alloc>
93 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
94 : _Head(allocator_arg, *__a._M_a) { }
95
96 template<typename _Alloc>
97 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
98 : _Head(*__a._M_a) { }
99
100 template<typename _UHead>
101 _Head_base(__uses_alloc0, _UHead&& __uhead)
102 : _Head(std::forward<_UHead>(__uhead)) { }
103
104 template<typename _Alloc, typename _UHead>
105 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
106 : _Head(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead)) { }
107
108 template<typename _Alloc, typename _UHead>
109 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
110 : _Head(std::forward<_UHead>(__uhead), *__a._M_a) { }
111
112 static constexpr _Head&
113 _M_head(_Head_base& __b) noexcept { return __b; }
114
115 static constexpr const _Head&
116 _M_head(const _Head_base& __b) noexcept { return __b; }
117 };
118
119 template<std::size_t _Idx, typename _Head>
120 struct _Head_base<_Idx, _Head, false>
121 {
122 constexpr _Head_base()
123 : _M_head_impl() { }
124
125 constexpr _Head_base(const _Head& __h)
126 : _M_head_impl(__h) { }
127
128 constexpr _Head_base(const _Head_base&) = default;
129 constexpr _Head_base(_Head_base&&) = default;
130
131 template<typename _UHead>
132 constexpr _Head_base(_UHead&& __h)
133 : _M_head_impl(std::forward<_UHead>(__h)) { }
78
Calling 'forward'
79
Returning from 'forward'
160
Calling 'forward'
161
Returning from 'forward'
254
Calling 'forward'
255
Returning from 'forward'
459
Calling 'forward'
460
Returning from 'forward'
134
135 _Head_base(allocator_arg_t, __uses_alloc0)
136 : _M_head_impl() { }
137
138 template<typename _Alloc>
139 _Head_base(allocator_arg_t, __uses_alloc1<_Alloc> __a)
140 : _M_head_impl(allocator_arg, *__a._M_a) { }
141
142 template<typename _Alloc>
143 _Head_base(allocator_arg_t, __uses_alloc2<_Alloc> __a)
144 : _M_head_impl(*__a._M_a) { }
145
146 template<typename _UHead>
147 _Head_base(__uses_alloc0, _UHead&& __uhead)
148 : _M_head_impl(std::forward<_UHead>(__uhead)) { }
149
150 template<typename _Alloc, typename _UHead>
151 _Head_base(__uses_alloc1<_Alloc> __a, _UHead&& __uhead)
152 : _M_head_impl(allocator_arg, *__a._M_a, std::forward<_UHead>(__uhead))
153 { }
154
155 template<typename _Alloc, typename _UHead>
156 _Head_base(__uses_alloc2<_Alloc> __a, _UHead&& __uhead)
157 : _M_head_impl(std::forward<_UHead>(__uhead), *__a._M_a) { }
158
159 static constexpr _Head&
160 _M_head(_Head_base& __b) noexcept { return __b._M_head_impl; }
161
162 static constexpr const _Head&
163 _M_head(const _Head_base& __b) noexcept { return __b._M_head_impl; }
164
165 _Head _M_head_impl;
166 };
167
168 /**
169 * Contains the actual implementation of the @c tuple template, stored
170 * as a recursive inheritance hierarchy from the first element (most
171 * derived class) to the last (least derived class). The @c Idx
172 * parameter gives the 0-based index of the element stored at this
173 * point in the hierarchy; we use it to implement a constant-time
174 * get() operation.
175 */
176 template<std::size_t _Idx, typename... _Elements>
177 struct _Tuple_impl;
178
179 /**
180 * Recursive tuple implementation. Here we store the @c Head element
181 * and derive from a @c Tuple_impl containing the remaining elements
182 * (which contains the @c Tail).
183 */
184 template<std::size_t _Idx, typename _Head, typename... _Tail>
185 struct _Tuple_impl<_Idx, _Head, _Tail...>
186 : public _Tuple_impl<_Idx + 1, _Tail...>,
187 private _Head_base<_Idx, _Head>
188 {
189 template<std::size_t, typename...> friend class _Tuple_impl;
190
191 typedef _Tuple_impl<_Idx + 1, _Tail...> _Inherited;
192 typedef _Head_base<_Idx, _Head> _Base;
193
194 static constexpr _Head&
195 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
35
Calling '_Head_base::_M_head'
36
Returning from '_Head_base::_M_head'
117
Calling '_Head_base::_M_head'
118
Returning from '_Head_base::_M_head'
211
Calling '_Head_base::_M_head'
212
Returning from '_Head_base::_M_head'
416
Calling '_Head_base::_M_head'
417
Returning from '_Head_base::_M_head'
485
Calling '_Head_base::_M_head'
486
Returning from '_Head_base::_M_head'
196
197 static constexpr const _Head&
198 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
24
Calling '_Head_base::_M_head'
25
Returning from '_Head_base::_M_head'
91
Calling '_Head_base::_M_head'
92
Returning from '_Head_base::_M_head'
106
Calling '_Head_base::_M_head'
107
Returning from '_Head_base::_M_head'
175
Calling '_Head_base::_M_head'
176
Returning from '_Head_base::_M_head'
200
Calling '_Head_base::_M_head'
201
Returning from '_Head_base::_M_head'
405
Calling '_Head_base::_M_head'
406
Returning from '_Head_base::_M_head'
199
200 static constexpr _Inherited&
201 _M_tail(_Tuple_impl& __t) noexcept { return __t; }
202
203 static constexpr const _Inherited&
204 _M_tail(const _Tuple_impl& __t) noexcept { return __t; }
205
206 constexpr _Tuple_impl()
207 : _Inherited(), _Base() { }
208
209 explicit
210 constexpr _Tuple_impl(const _Head& __head, const _Tail&... __tail)
211 : _Inherited(__tail...), _Base(__head) { }
212
213 template<typename _UHead, typename... _UTail, typename = typename
214 enable_if<sizeof...(_Tail) == sizeof...(_UTail)>::type>
215 explicit
216 constexpr _Tuple_impl(_UHead&& __head, _UTail&&... __tail)
217 : _Inherited(std::forward<_UTail>(__tail)...),
65
Calling 'forward'
66
Returning from 'forward'
67
Calling constructor for '_Tuple_impl'
74
Returning from constructor for '_Tuple_impl'
147
Calling 'forward'
148
Returning from 'forward'
149
Calling constructor for '_Tuple_impl'
156
Returning from constructor for '_Tuple_impl'
241
Calling 'forward'
242
Returning from 'forward'
243
Calling constructor for '_Tuple_impl'
250
Returning from constructor for '_Tuple_impl'
446
Calling 'forward'
447
Returning from 'forward'
448
Calling constructor for '_Tuple_impl'
455
Returning from constructor for '_Tuple_impl'
218 _Base(std::forward<_UHead>(__head)) { }
75
Calling 'forward'
76
Returning from 'forward'
77
Calling constructor for '_Head_base'
80
Returning from constructor for '_Head_base'
157
Calling 'forward'
158
Returning from 'forward'
159
Calling constructor for '_Head_base'
162
Returning from constructor for '_Head_base'
251
Calling 'forward'
252
Returning from 'forward'
253
Calling constructor for '_Head_base'
256
Returning from constructor for '_Head_base'
456
Calling 'forward'
457
Returning from 'forward'
458
Calling constructor for '_Head_base'
461
Returning from constructor for '_Head_base'
219
220 constexpr _Tuple_impl(const _Tuple_impl&) = default;
221
222 constexpr
223 _Tuple_impl(_Tuple_impl&& __in)
224 noexcept(__and_<is_nothrow_move_constructible<_Head>,
225 is_nothrow_move_constructible<_Inherited>>::value)
226 : _Inherited(std::move(_M_tail(__in))),
227 _Base(std::forward<_Head>(_M_head(__in))) { }
228
229 template<typename... _UElements>
230 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UElements...>& __in)
231 : _Inherited(_Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
232 _Base(_Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
233
234 template<typename _UHead, typename... _UTails>
235 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
236 : _Inherited(std::move
237 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
238 _Base(std::forward<_UHead>
239 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
240
241 template<typename _Alloc>
242 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
243 : _Inherited(__tag, __a),
244 _Base(__tag, __use_alloc<_Head>(__a)) { }
245
246 template<typename _Alloc>
247 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
248 const _Head& __head, const _Tail&... __tail)
249 : _Inherited(__tag, __a, __tail...),
250 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
251
252 template<typename _Alloc, typename _UHead, typename... _UTail,
253 typename = typename enable_if<sizeof...(_Tail)
254 == sizeof...(_UTail)>::type>
255 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
256 _UHead&& __head, _UTail&&... __tail)
257 : _Inherited(__tag, __a, std::forward<_UTail>(__tail)...),
258 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
259 std::forward<_UHead>(__head)) { }
260
261 template<typename _Alloc>
262 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
263 const _Tuple_impl& __in)
264 : _Inherited(__tag, __a, _M_tail(__in)),
265 _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
266
267 template<typename _Alloc>
268 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
269 _Tuple_impl&& __in)
270 : _Inherited(__tag, __a, std::move(_M_tail(__in))),
271 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
272 std::forward<_Head>(_M_head(__in))) { }
273
274 template<typename _Alloc, typename... _UElements>
275 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
276 const _Tuple_impl<_Idx, _UElements...>& __in)
277 : _Inherited(__tag, __a,
278 _Tuple_impl<_Idx, _UElements...>::_M_tail(__in)),
279 _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
280 _Tuple_impl<_Idx, _UElements...>::_M_head(__in)) { }
281
282 template<typename _Alloc, typename _UHead, typename... _UTails>
283 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
284 _Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
285 : _Inherited(__tag, __a, std::move
286 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in))),
287 _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
288 std::forward<_UHead>
289 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in))) { }
290
291 _Tuple_impl&
292 operator=(const _Tuple_impl& __in)
293 {
294 _M_head(*this) = _M_head(__in);
295 _M_tail(*this) = _M_tail(__in);
296 return *this;
297 }
298
299 _Tuple_impl&
300 operator=(_Tuple_impl&& __in)
301 noexcept(__and_<is_nothrow_move_assignable<_Head>,
302 is_nothrow_move_assignable<_Inherited>>::value)
303 {
304 _M_head(*this) = std::forward<_Head>(_M_head(__in));
305 _M_tail(*this) = std::move(_M_tail(__in));
306 return *this;
307 }
308
309 template<typename... _UElements>
310 _Tuple_impl&
311 operator=(const _Tuple_impl<_Idx, _UElements...>& __in)
312 {
313 _M_head(*this) = _Tuple_impl<_Idx, _UElements...>::_M_head(__in);
314 _M_tail(*this) = _Tuple_impl<_Idx, _UElements...>::_M_tail(__in);
315 return *this;
316 }
317
318 template<typename _UHead, typename... _UTails>
319 _Tuple_impl&
320 operator=(_Tuple_impl<_Idx, _UHead, _UTails...>&& __in)
321 {
322 _M_head(*this) = std::forward<_UHead>
323 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_head(__in));
324 _M_tail(*this) = std::move
325 (_Tuple_impl<_Idx, _UHead, _UTails...>::_M_tail(__in));
326 return *this;
327 }
328
329 protected:
330 void
331 _M_swap(_Tuple_impl& __in)
332 noexcept(__is_nothrow_swappable<_Head>::value
333 && noexcept(_M_tail(__in)._M_swap(_M_tail(__in))))
334 {
335 using std::swap;
336 swap(_M_head(*this), _M_head(__in));
337 _Inherited::_M_swap(_M_tail(__in));
338 }
339 };
340
341 // Basis case of inheritance recursion.
342 template<std::size_t _Idx, typename _Head>
343 struct _Tuple_impl<_Idx, _Head>
344 : private _Head_base<_Idx, _Head>
345 {
346 template<std::size_t, typename...> friend class _Tuple_impl;
347
348 typedef _Head_base<_Idx, _Head> _Base;
349
350 static constexpr _Head&
351 _M_head(_Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
47
Calling '_Head_base::_M_head'
48
Returning from '_Head_base::_M_head'
129
Calling '_Head_base::_M_head'
130
Returning from '_Head_base::_M_head'
223
Calling '_Head_base::_M_head'
224
Returning from '_Head_base::_M_head'
428
Calling '_Head_base::_M_head'
429
Returning from '_Head_base::_M_head'
497
Calling '_Head_base::_M_head'
498
Returning from '_Head_base::_M_head'
352
353 static constexpr const _Head&
354 _M_head(const _Tuple_impl& __t) noexcept { return _Base::_M_head(__t); }
355
356 constexpr _Tuple_impl()
357 : _Base() { }
358
359 explicit
360 constexpr _Tuple_impl(const _Head& __head)
361 : _Base(__head) { }
362
363 template<typename _UHead>
364 explicit
365 constexpr _Tuple_impl(_UHead&& __head)
366 : _Base(std::forward<_UHead>(__head)) { }
68
Calling 'forward'
69
Returning from 'forward'
70
Calling constructor for '_Head_base'
73
Returning from constructor for '_Head_base'
150
Calling 'forward'
151
Returning from 'forward'
152
Calling constructor for '_Head_base'
155
Returning from constructor for '_Head_base'
244
Calling 'forward'
245
Returning from 'forward'
246
Calling constructor for '_Head_base'
249
Returning from constructor for '_Head_base'
449
Calling 'forward'
450
Returning from 'forward'
451
Calling constructor for '_Head_base'
454
Returning from constructor for '_Head_base'
367
368 constexpr _Tuple_impl(const _Tuple_impl&) = default;
369
370 constexpr
371 _Tuple_impl(_Tuple_impl&& __in)
372 noexcept(is_nothrow_move_constructible<_Head>::value)
373 : _Base(std::forward<_Head>(_M_head(__in))) { }
374
375 template<typename _UHead>
376 constexpr _Tuple_impl(const _Tuple_impl<_Idx, _UHead>& __in)
377 : _Base(_Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
378
379 template<typename _UHead>
380 constexpr _Tuple_impl(_Tuple_impl<_Idx, _UHead>&& __in)
381 : _Base(std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
382 { }
383
384 template<typename _Alloc>
385 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a)
386 : _Base(__tag, __use_alloc<_Head>(__a)) { }
387
388 template<typename _Alloc>
389 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
390 const _Head& __head)
391 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), __head) { }
392
393 template<typename _Alloc, typename _UHead>
394 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
395 _UHead&& __head)
396 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
397 std::forward<_UHead>(__head)) { }
398
399 template<typename _Alloc>
400 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
401 const _Tuple_impl& __in)
402 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a), _M_head(__in)) { }
403
404 template<typename _Alloc>
405 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
406 _Tuple_impl&& __in)
407 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
408 std::forward<_Head>(_M_head(__in))) { }
409
410 template<typename _Alloc, typename _UHead>
411 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
412 const _Tuple_impl<_Idx, _UHead>& __in)
413 : _Base(__use_alloc<_Head, _Alloc, _Head>(__a),
414 _Tuple_impl<_Idx, _UHead>::_M_head(__in)) { }
415
416 template<typename _Alloc, typename _UHead>
417 _Tuple_impl(allocator_arg_t __tag, const _Alloc& __a,
418 _Tuple_impl<_Idx, _UHead>&& __in)
419 : _Base(__use_alloc<_Head, _Alloc, _UHead>(__a),
420 std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in)))
421 { }
422
423 _Tuple_impl&
424 operator=(const _Tuple_impl& __in)
425 {
426 _M_head(*this) = _M_head(__in);
427 return *this;
428 }
429
430 _Tuple_impl&
431 operator=(_Tuple_impl&& __in)
432 noexcept(is_nothrow_move_assignable<_Head>::value)
433 {
434 _M_head(*this) = std::forward<_Head>(_M_head(__in));
435 return *this;
436 }
437
438 template<typename _UHead>
439 _Tuple_impl&
440 operator=(const _Tuple_impl<_Idx, _UHead>& __in)
441 {
442 _M_head(*this) = _Tuple_impl<_Idx, _UHead>::_M_head(__in);
443 return *this;
444 }
445
446 template<typename _UHead>
447 _Tuple_impl&
448 operator=(_Tuple_impl<_Idx, _UHead>&& __in)
449 {
450 _M_head(*this)
451 = std::forward<_UHead>(_Tuple_impl<_Idx, _UHead>::_M_head(__in));
452 return *this;
453 }
454
455 protected:
456 void
457 _M_swap(_Tuple_impl& __in)
458 noexcept(__is_nothrow_swappable<_Head>::value)
459 {
460 using std::swap;
461 swap(_M_head(*this), _M_head(__in));
462 }
463 };
464
465 // Concept utility functions, reused in conditionally-explicit
466 // constructors.
467 template<bool, typename... _Elements>
468 struct _TC
469 {
470 template<typename... _UElements>
471 static constexpr bool _ConstructibleTuple()
472 {
473 return __and_<is_constructible<_Elements, const _UElements&>...>::value;
474 }
475
476 template<typename... _UElements>
477 static constexpr bool _ImplicitlyConvertibleTuple()
478 {
479 return __and_<is_convertible<const _UElements&, _Elements>...>::value;
480 }
481
482 template<typename... _UElements>
483 static constexpr bool _MoveConstructibleTuple()
484 {
485 return __and_<is_constructible<_Elements, _UElements&&>...>::value;
486 }
487
488 template<typename... _UElements>
489 static constexpr bool _ImplicitlyMoveConvertibleTuple()
490 {
491 return __and_<is_convertible<_UElements&&, _Elements>...>::value;
492 }
493
494 template<typename _SrcTuple>
495 static constexpr bool _NonNestedTuple()
496 {
497 return __and_<__not_<is_same<tuple<_Elements...>,
498 typename remove_cv<
499 typename remove_reference<_SrcTuple>::type
500 >::type>>,
501 __not_<is_convertible<_SrcTuple, _Elements...>>,
502 __not_<is_constructible<_Elements..., _SrcTuple>>
503 >::value;
504 }
505 template<typename... _UElements>
506 static constexpr bool _NotSameTuple()
507 {
508 return __not_<is_same<tuple<_Elements...>,
509 typename remove_const<
510 typename remove_reference<_UElements...>::type
511 >::type>>::value;
512 }
513 };
514
515 template<typename... _Elements>
516 struct _TC<false, _Elements...>
517 {
518 template<typename... _UElements>
519 static constexpr bool _ConstructibleTuple()
520 {
521 return false;
522 }
523
524 template<typename... _UElements>
525 static constexpr bool _ImplicitlyConvertibleTuple()
526 {
527 return false;
528 }
529
530 template<typename... _UElements>
531 static constexpr bool _MoveConstructibleTuple()
532 {
533 return false;
534 }
535
536 template<typename... _UElements>
537 static constexpr bool _ImplicitlyMoveConvertibleTuple()
538 {
539 return false;
540 }
541
542 template<typename... _UElements>
543 static constexpr bool _NonNestedTuple()
544 {
545 return true;
546 }
547 template<typename... _UElements>
548 static constexpr bool _NotSameTuple()
549 {
550 return true;
551 }
552 };
553
554 /// Primary class template, tuple
555 template<typename... _Elements>
556 class tuple : public _Tuple_impl<0, _Elements...>
557 {
558 typedef _Tuple_impl<0, _Elements...> _Inherited;
559
560 // Used for constraining the default constructor so
561 // that it becomes dependent on the constraints.
562 template<typename _Dummy>
563 struct _TC2
564 {
565 static constexpr bool _DefaultConstructibleTuple()
566 {
567 return __and_<is_default_constructible<_Elements>...>::value;
568 }
569 static constexpr bool _ImplicitlyDefaultConstructibleTuple()
570 {
571 return __and_<__is_implicitly_default_constructible<_Elements>...>
572 ::value;
573 }
574 };
575
576 public:
577 template<typename _Dummy = void,
578 typename enable_if<_TC2<_Dummy>::
579 _ImplicitlyDefaultConstructibleTuple(),
580 bool>::type = true>
581 constexpr tuple()
582 : _Inherited() { }
583
584 template<typename _Dummy = void,
585 typename enable_if<_TC2<_Dummy>::
586 _DefaultConstructibleTuple()
587 &&
588 !_TC2<_Dummy>::
589 _ImplicitlyDefaultConstructibleTuple(),
590 bool>::type = false>
591 explicit constexpr tuple()
592 : _Inherited() { }
593
594 // Shortcut for the cases where constructors taking _Elements...
595 // need to be constrained.
596 template<typename _Dummy> using _TCC =
597 _TC<is_same<_Dummy, void>::value,
598 _Elements...>;
599
600 template<typename _Dummy = void,
601 typename enable_if<
602 _TCC<_Dummy>::template
603 _ConstructibleTuple<_Elements...>()
604 && _TCC<_Dummy>::template
605 _ImplicitlyConvertibleTuple<_Elements...>()
606 && (sizeof...(_Elements) >= 1),
607 bool>::type=true>
608 constexpr tuple(const _Elements&... __elements)
609 : _Inherited(__elements...) { }
610
611 template<typename _Dummy = void,
612 typename enable_if<
613 _TCC<_Dummy>::template
614 _ConstructibleTuple<_Elements...>()
615 && !_TCC<_Dummy>::template
616 _ImplicitlyConvertibleTuple<_Elements...>()
617 && (sizeof...(_Elements) >= 1),
618 bool>::type=false>
619 explicit constexpr tuple(const _Elements&... __elements)
620 : _Inherited(__elements...) { }
621
622 // Shortcut for the cases where constructors taking _UElements...
623 // need to be constrained.
624 template<typename... _UElements> using _TMC =
625 _TC<(sizeof...(_Elements) == sizeof...(_UElements))
626 && (_TC<(sizeof...(_UElements)==1), _Elements...>::
627 template _NotSameTuple<_UElements...>()),
628 _Elements...>;
629
630 // Shortcut for the cases where constructors taking tuple<_UElements...>
631 // need to be constrained.
632 template<typename... _UElements> using _TMCT =
633 _TC<(sizeof...(_Elements) == sizeof...(_UElements))
634 && !is_same<tuple<_Elements...>,
635 tuple<_UElements...>>::value,
636 _Elements...>;
637
638 template<typename... _UElements, typename
639 enable_if<
640 _TMC<_UElements...>::template
641 _MoveConstructibleTuple<_UElements...>()
642 && _TMC<_UElements...>::template
643 _ImplicitlyMoveConvertibleTuple<_UElements...>()
644 && (sizeof...(_Elements) >= 1),
645 bool>::type=true>
646 constexpr tuple(_UElements&&... __elements)
647 : _Inherited(std::forward<_UElements>(__elements)...) { }
648
649 template<typename... _UElements, typename
650 enable_if<
651 _TMC<_UElements...>::template
652 _MoveConstructibleTuple<_UElements...>()
653 && !_TMC<_UElements...>::template
654 _ImplicitlyMoveConvertibleTuple<_UElements...>()
655 && (sizeof...(_Elements) >= 1),
656 bool>::type=false>
657 explicit constexpr tuple(_UElements&&... __elements)
658 : _Inherited(std::forward<_UElements>(__elements)...) { }
659
660 constexpr tuple(const tuple&) = default;
661
662 constexpr tuple(tuple&&) = default;
663
664 // Shortcut for the cases where constructors taking tuples
665 // must avoid creating temporaries.
666 template<typename _Dummy> using _TNTC =
667 _TC<is_same<_Dummy, void>::value && sizeof...(_Elements) == 1,
668 _Elements...>;
669
670 template<typename... _UElements, typename _Dummy = void, typename
671 enable_if<_TMCT<_UElements...>::template
672 _ConstructibleTuple<_UElements...>()
673 && _TMCT<_UElements...>::template
674 _ImplicitlyConvertibleTuple<_UElements...>()
675 && _TNTC<_Dummy>::template
676 _NonNestedTuple<const tuple<_UElements...>&>(),
677 bool>::type=true>
678 constexpr tuple(const tuple<_UElements...>& __in)
679 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
680 { }
681
682 template<typename... _UElements, typename _Dummy = void, typename
683 enable_if<_TMCT<_UElements...>::template
684 _ConstructibleTuple<_UElements...>()
685 && !_TMCT<_UElements...>::template
686 _ImplicitlyConvertibleTuple<_UElements...>()
687 && _TNTC<_Dummy>::template
688 _NonNestedTuple<const tuple<_UElements...>&>(),
689 bool>::type=false>
690 explicit constexpr tuple(const tuple<_UElements...>& __in)
691 : _Inherited(static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
692 { }
693
694 template<typename... _UElements, typename _Dummy = void, typename
695 enable_if<_TMCT<_UElements...>::template
696 _MoveConstructibleTuple<_UElements...>()
697 && _TMCT<_UElements...>::template
698 _ImplicitlyMoveConvertibleTuple<_UElements...>()
699 && _TNTC<_Dummy>::template
700 _NonNestedTuple<tuple<_UElements...>&&>(),
701 bool>::type=true>
702 constexpr tuple(tuple<_UElements...>&& __in)
703 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
704
705 template<typename... _UElements, typename _Dummy = void, typename
706 enable_if<_TMCT<_UElements...>::template
707 _MoveConstructibleTuple<_UElements...>()
708 && !_TMCT<_UElements...>::template
709 _ImplicitlyMoveConvertibleTuple<_UElements...>()
710 && _TNTC<_Dummy>::template
711 _NonNestedTuple<tuple<_UElements...>&&>(),
712 bool>::type=false>
713 explicit constexpr tuple(tuple<_UElements...>&& __in)
714 : _Inherited(static_cast<_Tuple_impl<0, _UElements...>&&>(__in)) { }
715
716 // Allocator-extended constructors.
717
718 template<typename _Alloc>
719 tuple(allocator_arg_t __tag, const _Alloc& __a)
720 : _Inherited(__tag, __a) { }
721
722 template<typename _Alloc, typename _Dummy = void,
723 typename enable_if<
724 _TCC<_Dummy>::template
725 _ConstructibleTuple<_Elements...>()
726 && _TCC<_Dummy>::template
727 _ImplicitlyConvertibleTuple<_Elements...>(),
728 bool>::type=true>
729 tuple(allocator_arg_t __tag, const _Alloc& __a,
730 const _Elements&... __elements)
731 : _Inherited(__tag, __a, __elements...) { }
732
733 template<typename _Alloc, typename _Dummy = void,
734 typename enable_if<
735 _TCC<_Dummy>::template
736 _ConstructibleTuple<_Elements...>()
737 && !_TCC<_Dummy>::template
738 _ImplicitlyConvertibleTuple<_Elements...>(),
739 bool>::type=false>
740 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
741 const _Elements&... __elements)
742 : _Inherited(__tag, __a, __elements...) { }
743
744 template<typename _Alloc, typename... _UElements, typename
745 enable_if<_TMC<_UElements...>::template
746 _MoveConstructibleTuple<_UElements...>()
747 && _TMC<_UElements...>::template
748 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
749 bool>::type=true>
750 tuple(allocator_arg_t __tag, const _Alloc& __a,
751 _UElements&&... __elements)
752 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
753 { }
754
755 template<typename _Alloc, typename... _UElements, typename
756 enable_if<_TMC<_UElements...>::template
757 _MoveConstructibleTuple<_UElements...>()
758 && !_TMC<_UElements...>::template
759 _ImplicitlyMoveConvertibleTuple<_UElements...>(),
760 bool>::type=false>
761 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
762 _UElements&&... __elements)
763 : _Inherited(__tag, __a, std::forward<_UElements>(__elements)...)
764 { }
765
766 template<typename _Alloc>
767 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
768 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
769
770 template<typename _Alloc>
771 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
772 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
773
774 template<typename _Alloc, typename _Dummy = void,
775 typename... _UElements, typename
776 enable_if<_TMCT<_UElements...>::template
777 _ConstructibleTuple<_UElements...>()
778 && _TMCT<_UElements...>::template
779 _ImplicitlyConvertibleTuple<_UElements...>()
780 && _TNTC<_Dummy>::template
781 _NonNestedTuple<tuple<_UElements...>&&>(),
782 bool>::type=true>
783 tuple(allocator_arg_t __tag, const _Alloc& __a,
784 const tuple<_UElements...>& __in)
785 : _Inherited(__tag, __a,
786 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
787 { }
788
789 template<typename _Alloc, typename _Dummy = void,
790 typename... _UElements, typename
791 enable_if<_TMCT<_UElements...>::template
792 _ConstructibleTuple<_UElements...>()
793 && !_TMCT<_UElements...>::template
794 _ImplicitlyConvertibleTuple<_UElements...>()
795 && _TNTC<_Dummy>::template
796 _NonNestedTuple<tuple<_UElements...>&&>(),
797 bool>::type=false>
798 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
799 const tuple<_UElements...>& __in)
800 : _Inherited(__tag, __a,
801 static_cast<const _Tuple_impl<0, _UElements...>&>(__in))
802 { }
803
804 template<typename _Alloc, typename _Dummy = void,
805 typename... _UElements, typename
806 enable_if<_TMCT<_UElements...>::template
807 _MoveConstructibleTuple<_UElements...>()
808 && _TMCT<_UElements...>::template
809 _ImplicitlyMoveConvertibleTuple<_UElements...>()
810 && _TNTC<_Dummy>::template
811 _NonNestedTuple<tuple<_UElements...>&&>(),
812 bool>::type=true>
813 tuple(allocator_arg_t __tag, const _Alloc& __a,
814 tuple<_UElements...>&& __in)
815 : _Inherited(__tag, __a,
816 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
817 { }
818
819 template<typename _Alloc, typename _Dummy = void,
820 typename... _UElements, typename
821 enable_if<_TMCT<_UElements...>::template
822 _MoveConstructibleTuple<_UElements...>()
823 && !_TMCT<_UElements...>::template
824 _ImplicitlyMoveConvertibleTuple<_UElements...>()
825 && _TNTC<_Dummy>::template
826 _NonNestedTuple<tuple<_UElements...>&&>(),
827 bool>::type=false>
828 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
829 tuple<_UElements...>&& __in)
830 : _Inherited(__tag, __a,
831 static_cast<_Tuple_impl<0, _UElements...>&&>(__in))
832 { }
833
834 tuple&
835 operator=(const tuple& __in)
836 {
837 static_cast<_Inherited&>(*this) = __in;
838 return *this;
839 }
840
841 tuple&
842 operator=(tuple&& __in)
843 noexcept(is_nothrow_move_assignable<_Inherited>::value)
844 {
845 static_cast<_Inherited&>(*this) = std::move(__in);
846 return *this;
847 }
848
849 template<typename... _UElements>
850 typename
851 enable_if<sizeof...(_UElements)
852 == sizeof...(_Elements), tuple&>::type
853 operator=(const tuple<_UElements...>& __in)
854 {
855 static_cast<_Inherited&>(*this) = __in;
856 return *this;
857 }
858
859 template<typename... _UElements>
860 typename
861 enable_if<sizeof...(_UElements)
862 == sizeof...(_Elements), tuple&>::type
863 operator=(tuple<_UElements...>&& __in)
864 {
865 static_cast<_Inherited&>(*this) = std::move(__in);
866 return *this;
867 }
868
869 void
870 swap(tuple& __in)
871 noexcept(noexcept(__in._M_swap(__in)))
872 { _Inherited::_M_swap(__in); }
873 };
874
875#if __cpp_deduction_guides >= 201606
876 template<typename... _UTypes>
877 tuple(_UTypes...) -> tuple<_UTypes...>;
878 template<typename _T1, typename _T2>
879 tuple(pair<_T1, _T2>) -> tuple<_T1, _T2>;
880 template<typename _Alloc, typename... _UTypes>
881 tuple(allocator_arg_t, _Alloc, _UTypes...) -> tuple<_UTypes...>;
882 template<typename _Alloc, typename _T1, typename _T2>
883 tuple(allocator_arg_t, _Alloc, pair<_T1, _T2>) -> tuple<_T1, _T2>;
884 template<typename _Alloc, typename... _UTypes>
885 tuple(allocator_arg_t, _Alloc, tuple<_UTypes...>) -> tuple<_UTypes...>;
886#endif
887
888 // Explicit specialization, zero-element tuple.
889 template<>
890 class tuple<>
891 {
892 public:
893 void swap(tuple&) noexcept { /* no-op */ }
894 // We need the default since we're going to define no-op
895 // allocator constructors.
896 tuple() = default;
897 // No-op allocator constructors.
898 template<typename _Alloc>
899 tuple(allocator_arg_t, const _Alloc&) { }
900 template<typename _Alloc>
901 tuple(allocator_arg_t, const _Alloc&, const tuple&) { }
902 };
903
904 /// Partial specialization, 2-element tuple.
905 /// Includes construction and assignment from a pair.
906 template<typename _T1, typename _T2>
907 class tuple<_T1, _T2> : public _Tuple_impl<0, _T1, _T2>
908 {
909 typedef _Tuple_impl<0, _T1, _T2> _Inherited;
910
911 public:
912 template <typename _U1 = _T1,
913 typename _U2 = _T2,
914 typename enable_if<__and_<
915 __is_implicitly_default_constructible<_U1>,
916 __is_implicitly_default_constructible<_U2>>
917 ::value, bool>::type = true>
918
919 constexpr tuple()
920 : _Inherited() { }
921
922 template <typename _U1 = _T1,
923 typename _U2 = _T2,
924 typename enable_if<
925 __and_<
926 is_default_constructible<_U1>,
927 is_default_constructible<_U2>,
928 __not_<
929 __and_<__is_implicitly_default_constructible<_U1>,
930 __is_implicitly_default_constructible<_U2>>>>
931 ::value, bool>::type = false>
932
933 explicit constexpr tuple()
934 : _Inherited() { }
935
936 // Shortcut for the cases where constructors taking _T1, _T2
937 // need to be constrained.
938 template<typename _Dummy> using _TCC =
939 _TC<is_same<_Dummy, void>::value, _T1, _T2>;
940
941 template<typename _Dummy = void, typename
942 enable_if<_TCC<_Dummy>::template
943 _ConstructibleTuple<_T1, _T2>()
944 && _TCC<_Dummy>::template
945 _ImplicitlyConvertibleTuple<_T1, _T2>(),
946 bool>::type = true>
947 constexpr tuple(const _T1& __a1, const _T2& __a2)
948 : _Inherited(__a1, __a2) { }
949
950 template<typename _Dummy = void, typename
951 enable_if<_TCC<_Dummy>::template
952 _ConstructibleTuple<_T1, _T2>()
953 && !_TCC<_Dummy>::template
954 _ImplicitlyConvertibleTuple<_T1, _T2>(),
955 bool>::type = false>
956 explicit constexpr tuple(const _T1& __a1, const _T2& __a2)
957 : _Inherited(__a1, __a2) { }
958
959 // Shortcut for the cases where constructors taking _U1, _U2
960 // need to be constrained.
961 using _TMC = _TC<true, _T1, _T2>;
962
963 template<typename _U1, typename _U2, typename
964 enable_if<_TMC::template
965 _MoveConstructibleTuple<_U1, _U2>()
966 && _TMC::template
967 _ImplicitlyMoveConvertibleTuple<_U1, _U2>()
968 && !is_same<typename decay<_U1>::type,
969 allocator_arg_t>::value,
970 bool>::type = true>
971 constexpr tuple(_U1&& __a1, _U2&& __a2)
972 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
60
Calling 'forward'
61
Returning from 'forward'
62
Calling 'forward'
63
Returning from 'forward'
64
Calling constructor for '_Tuple_impl'
81
Returning from constructor for '_Tuple_impl'
142
Calling 'forward'
143
Returning from 'forward'
144
Calling 'forward'
145
Returning from 'forward'
146
Calling constructor for '_Tuple_impl'
163
Returning from constructor for '_Tuple_impl'
236
Calling 'forward'
237
Returning from 'forward'
238
Calling 'forward'
239
Returning from 'forward'
240
Calling constructor for '_Tuple_impl'
257
Returning from constructor for '_Tuple_impl'
441
Calling 'forward'
442
Returning from 'forward'
443
Calling 'forward'
444
Returning from 'forward'
445
Calling constructor for '_Tuple_impl'
462
Returning from constructor for '_Tuple_impl'
973
974 template<typename _U1, typename _U2, typename
975 enable_if<_TMC::template
976 _MoveConstructibleTuple<_U1, _U2>()
977 && !_TMC::template
978 _ImplicitlyMoveConvertibleTuple<_U1, _U2>()
979 && !is_same<typename decay<_U1>::type,
980 allocator_arg_t>::value,
981 bool>::type = false>
982 explicit constexpr tuple(_U1&& __a1, _U2&& __a2)
983 : _Inherited(std::forward<_U1>(__a1), std::forward<_U2>(__a2)) { }
984
985 constexpr tuple(const tuple&) = default;
986
987 constexpr tuple(tuple&&) = default;
988
989 template<typename _U1, typename _U2, typename
990 enable_if<_TMC::template
991 _ConstructibleTuple<_U1, _U2>()
992 && _TMC::template
993 _ImplicitlyConvertibleTuple<_U1, _U2>(),
994 bool>::type = true>
995 constexpr tuple(const tuple<_U1, _U2>& __in)
996 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
997
998 template<typename _U1, typename _U2, typename
999 enable_if<_TMC::template
1000 _ConstructibleTuple<_U1, _U2>()
1001 && !_TMC::template
1002 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1003 bool>::type = false>
1004 explicit constexpr tuple(const tuple<_U1, _U2>& __in)
1005 : _Inherited(static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in)) { }
1006
1007 template<typename _U1, typename _U2, typename
1008 enable_if<_TMC::template
1009 _MoveConstructibleTuple<_U1, _U2>()
1010 && _TMC::template
1011 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1012 bool>::type = true>
1013 constexpr tuple(tuple<_U1, _U2>&& __in)
1014 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1015
1016 template<typename _U1, typename _U2, typename
1017 enable_if<_TMC::template
1018 _MoveConstructibleTuple<_U1, _U2>()
1019 && !_TMC::template
1020 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1021 bool>::type = false>
1022 explicit constexpr tuple(tuple<_U1, _U2>&& __in)
1023 : _Inherited(static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in)) { }
1024
1025 template<typename _U1, typename _U2, typename
1026 enable_if<_TMC::template
1027 _ConstructibleTuple<_U1, _U2>()
1028 && _TMC::template
1029 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1030 bool>::type = true>
1031 constexpr tuple(const pair<_U1, _U2>& __in)
1032 : _Inherited(__in.first, __in.second) { }
1033
1034 template<typename _U1, typename _U2, typename
1035 enable_if<_TMC::template
1036 _ConstructibleTuple<_U1, _U2>()
1037 && !_TMC::template
1038 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1039 bool>::type = false>
1040 explicit constexpr tuple(const pair<_U1, _U2>& __in)
1041 : _Inherited(__in.first, __in.second) { }
1042
1043 template<typename _U1, typename _U2, typename
1044 enable_if<_TMC::template
1045 _MoveConstructibleTuple<_U1, _U2>()
1046 && _TMC::template
1047 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1048 bool>::type = true>
1049 constexpr tuple(pair<_U1, _U2>&& __in)
1050 : _Inherited(std::forward<_U1>(__in.first),
1051 std::forward<_U2>(__in.second)) { }
1052
1053 template<typename _U1, typename _U2, typename
1054 enable_if<_TMC::template
1055 _MoveConstructibleTuple<_U1, _U2>()
1056 && !_TMC::template
1057 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1058 bool>::type = false>
1059 explicit constexpr tuple(pair<_U1, _U2>&& __in)
1060 : _Inherited(std::forward<_U1>(__in.first),
1061 std::forward<_U2>(__in.second)) { }
1062
1063 // Allocator-extended constructors.
1064
1065 template<typename _Alloc>
1066 tuple(allocator_arg_t __tag, const _Alloc& __a)
1067 : _Inherited(__tag, __a) { }
1068
1069 template<typename _Alloc, typename _Dummy = void,
1070 typename enable_if<
1071 _TCC<_Dummy>::template
1072 _ConstructibleTuple<_T1, _T2>()
1073 && _TCC<_Dummy>::template
1074 _ImplicitlyConvertibleTuple<_T1, _T2>(),
1075 bool>::type=true>
1076
1077 tuple(allocator_arg_t __tag, const _Alloc& __a,
1078 const _T1& __a1, const _T2& __a2)
1079 : _Inherited(__tag, __a, __a1, __a2) { }
1080
1081 template<typename _Alloc, typename _Dummy = void,
1082 typename enable_if<
1083 _TCC<_Dummy>::template
1084 _ConstructibleTuple<_T1, _T2>()
1085 && !_TCC<_Dummy>::template
1086 _ImplicitlyConvertibleTuple<_T1, _T2>(),
1087 bool>::type=false>
1088
1089 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1090 const _T1& __a1, const _T2& __a2)
1091 : _Inherited(__tag, __a, __a1, __a2) { }
1092
1093 template<typename _Alloc, typename _U1, typename _U2, typename
1094 enable_if<_TMC::template
1095 _MoveConstructibleTuple<_U1, _U2>()
1096 && _TMC::template
1097 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1098 bool>::type = true>
1099 tuple(allocator_arg_t __tag, const _Alloc& __a, _U1&& __a1, _U2&& __a2)
1100 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1101 std::forward<_U2>(__a2)) { }
1102
1103 template<typename _Alloc, typename _U1, typename _U2, typename
1104 enable_if<_TMC::template
1105 _MoveConstructibleTuple<_U1, _U2>()
1106 && !_TMC::template
1107 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1108 bool>::type = false>
1109 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1110 _U1&& __a1, _U2&& __a2)
1111 : _Inherited(__tag, __a, std::forward<_U1>(__a1),
1112 std::forward<_U2>(__a2)) { }
1113
1114 template<typename _Alloc>
1115 tuple(allocator_arg_t __tag, const _Alloc& __a, const tuple& __in)
1116 : _Inherited(__tag, __a, static_cast<const _Inherited&>(__in)) { }
1117
1118 template<typename _Alloc>
1119 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
1120 : _Inherited(__tag, __a, static_cast<_Inherited&&>(__in)) { }
1121
1122 template<typename _Alloc, typename _U1, typename _U2, typename
1123 enable_if<_TMC::template
1124 _ConstructibleTuple<_U1, _U2>()
1125 && _TMC::template
1126 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1127 bool>::type = true>
1128 tuple(allocator_arg_t __tag, const _Alloc& __a,
1129 const tuple<_U1, _U2>& __in)
1130 : _Inherited(__tag, __a,
1131 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1132 { }
1133
1134 template<typename _Alloc, typename _U1, typename _U2, typename
1135 enable_if<_TMC::template
1136 _ConstructibleTuple<_U1, _U2>()
1137 && !_TMC::template
1138 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1139 bool>::type = false>
1140 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1141 const tuple<_U1, _U2>& __in)
1142 : _Inherited(__tag, __a,
1143 static_cast<const _Tuple_impl<0, _U1, _U2>&>(__in))
1144 { }
1145
1146 template<typename _Alloc, typename _U1, typename _U2, typename
1147 enable_if<_TMC::template
1148 _MoveConstructibleTuple<_U1, _U2>()
1149 && _TMC::template
1150 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1151 bool>::type = true>
1152 tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
1153 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1154 { }
1155
1156 template<typename _Alloc, typename _U1, typename _U2, typename
1157 enable_if<_TMC::template
1158 _MoveConstructibleTuple<_U1, _U2>()
1159 && !_TMC::template
1160 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1161 bool>::type = false>
1162 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1163 tuple<_U1, _U2>&& __in)
1164 : _Inherited(__tag, __a, static_cast<_Tuple_impl<0, _U1, _U2>&&>(__in))
1165 { }
1166
1167 template<typename _Alloc, typename _U1, typename _U2, typename
1168 enable_if<_TMC::template
1169 _ConstructibleTuple<_U1, _U2>()
1170 && _TMC::template
1171 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1172 bool>::type = true>
1173 tuple(allocator_arg_t __tag, const _Alloc& __a,
1174 const pair<_U1, _U2>& __in)
1175 : _Inherited(__tag, __a, __in.first, __in.second) { }
1176
1177 template<typename _Alloc, typename _U1, typename _U2, typename
1178 enable_if<_TMC::template
1179 _ConstructibleTuple<_U1, _U2>()
1180 && !_TMC::template
1181 _ImplicitlyConvertibleTuple<_U1, _U2>(),
1182 bool>::type = false>
1183 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1184 const pair<_U1, _U2>& __in)
1185 : _Inherited(__tag, __a, __in.first, __in.second) { }
1186
1187 template<typename _Alloc, typename _U1, typename _U2, typename
1188 enable_if<_TMC::template
1189 _MoveConstructibleTuple<_U1, _U2>()
1190 && _TMC::template
1191 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1192 bool>::type = true>
1193 tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
1194 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1195 std::forward<_U2>(__in.second)) { }
1196
1197 template<typename _Alloc, typename _U1, typename _U2, typename
1198 enable_if<_TMC::template
1199 _MoveConstructibleTuple<_U1, _U2>()
1200 && !_TMC::template
1201 _ImplicitlyMoveConvertibleTuple<_U1, _U2>(),
1202 bool>::type = false>
1203 explicit tuple(allocator_arg_t __tag, const _Alloc& __a,
1204 pair<_U1, _U2>&& __in)
1205 : _Inherited(__tag, __a, std::forward<_U1>(__in.first),
1206 std::forward<_U2>(__in.second)) { }
1207
1208 tuple&
1209 operator=(const tuple& __in)
1210 {
1211 static_cast<_Inherited&>(*this) = __in;
1212 return *this;
1213 }
1214
1215 tuple&
1216 operator=(tuple&& __in)
1217 noexcept(is_nothrow_move_assignable<_Inherited>::value)
1218 {
1219 static_cast<_Inherited&>(*this) = std::move(__in);
1220 return *this;
1221 }
1222
1223 template<typename _U1, typename _U2>
1224 tuple&
1225 operator=(const tuple<_U1, _U2>& __in)
1226 {
1227 static_cast<_Inherited&>(*this) = __in;
1228 return *this;
1229 }
1230
1231 template<typename _U1, typename _U2>
1232 tuple&
1233 operator=(tuple<_U1, _U2>&& __in)
1234 {
1235 static_cast<_Inherited&>(*this) = std::move(__in);
1236 return *this;
1237 }
1238
1239 template<typename _U1, typename _U2>
1240 tuple&
1241 operator=(const pair<_U1, _U2>& __in)
1242 {
1243 this->_M_head(*this) = __in.first;
1244 this->_M_tail(*this)._M_head(*this) = __in.second;
1245 return *this;
1246 }
1247
1248 template<typename _U1, typename _U2>
1249 tuple&
1250 operator=(pair<_U1, _U2>&& __in)
1251 {
1252 this->_M_head(*this) = std::forward<_U1>(__in.first);
1253 this->_M_tail(*this)._M_head(*this) = std::forward<_U2>(__in.second);
1254 return *this;
1255 }
1256
1257 void
1258 swap(tuple& __in)
1259 noexcept(noexcept(__in._M_swap(__in)))
1260 { _Inherited::_M_swap(__in); }
1261 };
1262
1263
1264 /// class tuple_size
1265 template<typename... _Elements>
1266 struct tuple_size<tuple<_Elements...>>
1267 : public integral_constant<std::size_t, sizeof...(_Elements)> { };
1268
1269#if __cplusplus201103L > 201402L
1270 template <typename _Tp>
1271 inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
1272#endif
1273
1274 /**
1275 * Recursive case for tuple_element: strip off the first element in
1276 * the tuple and retrieve the (i-1)th element of the remaining tuple.
1277 */
1278 template<std::size_t __i, typename _Head, typename... _Tail>
1279 struct tuple_element<__i, tuple<_Head, _Tail...> >
1280 : tuple_element<__i - 1, tuple<_Tail...> > { };
1281
1282 /**
1283 * Basis case for tuple_element: The first element is the one we're seeking.
1284 */
1285 template<typename _Head, typename... _Tail>
1286 struct tuple_element<0, tuple<_Head, _Tail...> >
1287 {
1288 typedef _Head type;
1289 };
1290
1291 /**
1292 * Error case for tuple_element: invalid index.
1293 */
1294 template<size_t __i>
1295 struct tuple_element<__i, tuple<>>
1296 {
1297 static_assert(__i < tuple_size<tuple<>>::value,
1298 "tuple index is in range");
1299 };
1300
1301 template<std::size_t __i, typename _Head, typename... _Tail>
1302 constexpr _Head&
1303 __get_helper(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1304 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
34
Calling '_Tuple_impl::_M_head'
37
Returning from '_Tuple_impl::_M_head'
46
Calling '_Tuple_impl::_M_head'
49
Returning from '_Tuple_impl::_M_head'
116
Calling '_Tuple_impl::_M_head'
119
Returning from '_Tuple_impl::_M_head'
128
Calling '_Tuple_impl::_M_head'
131
Returning from '_Tuple_impl::_M_head'
210
Calling '_Tuple_impl::_M_head'
213
Returning from '_Tuple_impl::_M_head'
222
Calling '_Tuple_impl::_M_head'
225
Returning from '_Tuple_impl::_M_head'
415
Calling '_Tuple_impl::_M_head'
418
Returning from '_Tuple_impl::_M_head'
427
Calling '_Tuple_impl::_M_head'
430
Returning from '_Tuple_impl::_M_head'
484
Calling '_Tuple_impl::_M_head'
487
Returning from '_Tuple_impl::_M_head'
496
Calling '_Tuple_impl::_M_head'
499
Returning from '_Tuple_impl::_M_head'
1305
1306 template<std::size_t __i, typename _Head, typename... _Tail>
1307 constexpr const _Head&
1308 __get_helper(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1309 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
23
Calling '_Tuple_impl::_M_head'
26
Returning from '_Tuple_impl::_M_head'
90
Calling '_Tuple_impl::_M_head'
93
Returning from '_Tuple_impl::_M_head'
105
Calling '_Tuple_impl::_M_head'
108
Returning from '_Tuple_impl::_M_head'
174
Calling '_Tuple_impl::_M_head'
177
Returning from '_Tuple_impl::_M_head'
199
Calling '_Tuple_impl::_M_head'
202
Returning from '_Tuple_impl::_M_head'
404
Calling '_Tuple_impl::_M_head'
407
Returning from '_Tuple_impl::_M_head'
1310
1311 /// Return a reference to the ith element of a tuple.
1312 template<std::size_t __i, typename... _Elements>
1313 constexpr __tuple_element_t<__i, tuple<_Elements...>>&
1314 get(tuple<_Elements...>& __t) noexcept
1315 { return std::__get_helper<__i>(__t); }
33
Calling '__get_helper'
38
Returning from '__get_helper'
45
Calling '__get_helper'
50
Returning from '__get_helper'
115
Calling '__get_helper'
120
Returning from '__get_helper'
127
Calling '__get_helper'
132
Returning from '__get_helper'
209
Calling '__get_helper'
214
Returning from '__get_helper'
221
Calling '__get_helper'
226
Returning from '__get_helper'
414
Calling '__get_helper'
419
Returning from '__get_helper'
426
Calling '__get_helper'
431
Returning from '__get_helper'
483
Calling '__get_helper'
488
Returning from '__get_helper'
495
Calling '__get_helper'
500
Returning from '__get_helper'
1316
1317 /// Return a const reference to the ith element of a const tuple.
1318 template<std::size_t __i, typename... _Elements>
1319 constexpr const __tuple_element_t<__i, tuple<_Elements...>>&
1320 get(const tuple<_Elements...>& __t) noexcept
1321 { return std::__get_helper<__i>(__t); }
22
Calling '__get_helper'
27
Returning from '__get_helper'
89
Calling '__get_helper'
94
Returning from '__get_helper'
104
Calling '__get_helper'
109
Returning from '__get_helper'
173
Calling '__get_helper'
178
Returning from '__get_helper'
198
Calling '__get_helper'
203
Returning from '__get_helper'
403
Calling '__get_helper'
408
Returning from '__get_helper'
1322
1323 /// Return an rvalue reference to the ith element of a tuple rvalue.
1324 template<std::size_t __i, typename... _Elements>
1325 constexpr __tuple_element_t<__i, tuple<_Elements...>>&&
1326 get(tuple<_Elements...>&& __t) noexcept
1327 {
1328 typedef __tuple_element_t<__i, tuple<_Elements...>> __element_type;
1329 return std::forward<__element_type&&>(std::get<__i>(__t));
1330 }
1331
1332#if __cplusplus201103L > 201103L
1333
1334#define __cpp_lib_tuples_by_type 201304
1335
1336 template<typename _Head, size_t __i, typename... _Tail>
1337 constexpr _Head&
1338 __get_helper2(_Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1339 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1340
1341 template<typename _Head, size_t __i, typename... _Tail>
1342 constexpr const _Head&
1343 __get_helper2(const _Tuple_impl<__i, _Head, _Tail...>& __t) noexcept
1344 { return _Tuple_impl<__i, _Head, _Tail...>::_M_head(__t); }
1345
1346 /// Return a reference to the unique element of type _Tp of a tuple.
1347 template <typename _Tp, typename... _Types>
1348 constexpr _Tp&
1349 get(tuple<_Types...>& __t) noexcept
1350 { return std::__get_helper2<_Tp>(__t); }
1351
1352 /// Return a reference to the unique element of type _Tp of a tuple rvalue.
1353 template <typename _Tp, typename... _Types>
1354 constexpr _Tp&&
1355 get(tuple<_Types...>&& __t) noexcept
1356 { return std::forward<_Tp&&>(std::__get_helper2<_Tp>(__t)); }
1357
1358 /// Return a const reference to the unique element of type _Tp of a tuple.
1359 template <typename _Tp, typename... _Types>
1360 constexpr const _Tp&
1361 get(const tuple<_Types...>& __t) noexcept
1362 { return std::__get_helper2<_Tp>(__t); }
1363#endif
1364
1365 // This class performs the comparison operations on tuples
1366 template<typename _Tp, typename _Up, size_t __i, size_t __size>
1367 struct __tuple_compare
1368 {
1369 static constexpr bool
1370 __eq(const _Tp& __t, const _Up& __u)
1371 {
1372 return bool(std::get<__i>(__t) == std::get<__i>(__u))
1373 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__eq(__t, __u);
1374 }
1375
1376 static constexpr bool
1377 __less(const _Tp& __t, const _Up& __u)
1378 {
1379 return bool(std::get<__i>(__t) < std::get<__i>(__u))
1380 || (!bool(std::get<__i>(__u) < std::get<__i>(__t))
1381 && __tuple_compare<_Tp, _Up, __i + 1, __size>::__less(__t, __u));
1382 }
1383 };
1384
1385 template<typename _Tp, typename _Up, size_t __size>
1386 struct __tuple_compare<_Tp, _Up, __size, __size>
1387 {
1388 static constexpr bool
1389 __eq(const _Tp&, const _Up&) { return true; }
1390
1391 static constexpr bool
1392 __less(const _Tp&, const _Up&) { return false; }
1393 };
1394
1395 template<typename... _TElements, typename... _UElements>
1396 constexpr bool
1397 operator==(const tuple<_TElements...>& __t,
1398 const tuple<_UElements...>& __u)
1399 {
1400 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1401 "tuple objects can only be compared if they have equal sizes.");
1402 using __compare = __tuple_compare<tuple<_TElements...>,
1403 tuple<_UElements...>,
1404 0, sizeof...(_TElements)>;
1405 return __compare::__eq(__t, __u);
1406 }
1407
1408 template<typename... _TElements, typename... _UElements>
1409 constexpr bool
1410 operator<(const tuple<_TElements...>& __t,
1411 const tuple<_UElements...>& __u)
1412 {
1413 static_assert(sizeof...(_TElements) == sizeof...(_UElements),
1414 "tuple objects can only be compared if they have equal sizes.");
1415 using __compare = __tuple_compare<tuple<_TElements...>,
1416 tuple<_UElements...>,
1417 0, sizeof...(_TElements)>;
1418 return __compare::__less(__t, __u);
1419 }
1420
1421 template<typename... _TElements, typename... _UElements>
1422 constexpr bool
1423 operator!=(const tuple<_TElements...>& __t,
1424 const tuple<_UElements...>& __u)
1425 { return !(__t == __u); }
1426
1427 template<typename... _TElements, typename... _UElements>
1428 constexpr bool
1429 operator>(const tuple<_TElements...>& __t,
1430 const tuple<_UElements...>& __u)
1431 { return __u < __t; }
1432
1433 template<typename... _TElements, typename... _UElements>
1434 constexpr bool
1435 operator<=(const tuple<_TElements...>& __t,
1436 const tuple<_UElements...>& __u)
1437 { return !(__u < __t); }
1438
1439 template<typename... _TElements, typename... _UElements>
1440 constexpr bool
1441 operator>=(const tuple<_TElements...>& __t,
1442 const tuple<_UElements...>& __u)
1443 { return !(__t < __u); }
1444
1445 // NB: DR 705.
1446 template<typename... _Elements>
1447 constexpr tuple<typename __decay_and_strip<_Elements>::__type...>
1448 make_tuple(_Elements&&... __args)
1449 {
1450 typedef tuple<typename __decay_and_strip<_Elements>::__type...>
1451 __result_type;
1452 return __result_type(std::forward<_Elements>(__args)...);
1453 }
1454
1455 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1456 // 2275. Why is forward_as_tuple not constexpr?
1457 template<typename... _Elements>
1458 constexpr tuple<_Elements&&...>
1459 forward_as_tuple(_Elements&&... __args) noexcept
1460 { return tuple<_Elements&&...>(std::forward<_Elements>(__args)...); }
1461
1462 template<size_t, typename, typename, size_t>
1463 struct __make_tuple_impl;
1464
1465 template<size_t _Idx, typename _Tuple, typename... _Tp, size_t _Nm>
1466 struct __make_tuple_impl<_Idx, tuple<_Tp...>, _Tuple, _Nm>
1467 : __make_tuple_impl<_Idx + 1,
1468 tuple<_Tp..., __tuple_element_t<_Idx, _Tuple>>,
1469 _Tuple, _Nm>
1470 { };
1471
1472 template<std::size_t _Nm, typename _Tuple, typename... _Tp>
1473 struct __make_tuple_impl<_Nm, tuple<_Tp...>, _Tuple, _Nm>
1474 {
1475 typedef tuple<_Tp...> __type;
1476 };
1477
1478 template<typename _Tuple>
1479 struct __do_make_tuple
1480 : __make_tuple_impl<0, tuple<>, _Tuple, std::tuple_size<_Tuple>::value>
1481 { };
1482
1483 // Returns the std::tuple equivalent of a tuple-like type.
1484 template<typename _Tuple>
1485 struct __make_tuple
1486 : public __do_make_tuple<typename std::remove_cv
1487 <typename std::remove_reference<_Tuple>::type>::type>
1488 { };
1489
1490 // Combines several std::tuple's into a single one.
1491 template<typename...>
1492 struct __combine_tuples;
1493
1494 template<>
1495 struct __combine_tuples<>
1496 {
1497 typedef tuple<> __type;
1498 };
1499
1500 template<typename... _Ts>
1501 struct __combine_tuples<tuple<_Ts...>>
1502 {
1503 typedef tuple<_Ts...> __type;
1504 };
1505
1506 template<typename... _T1s, typename... _T2s, typename... _Rem>
1507 struct __combine_tuples<tuple<_T1s...>, tuple<_T2s...>, _Rem...>
1508 {
1509 typedef typename __combine_tuples<tuple<_T1s..., _T2s...>,
1510 _Rem...>::__type __type;
1511 };
1512
1513 // Computes the result type of tuple_cat given a set of tuple-like types.
1514 template<typename... _Tpls>
1515 struct __tuple_cat_result
1516 {
1517 typedef typename __combine_tuples
1518 <typename __make_tuple<_Tpls>::__type...>::__type __type;
1519 };
1520
1521 // Helper to determine the index set for the first tuple-like
1522 // type of a given set.
1523 template<typename...>
1524 struct __make_1st_indices;
1525
1526 template<>
1527 struct __make_1st_indices<>
1528 {
1529 typedef std::_Index_tuple<> __type;
1530 };
1531
1532 template<typename _Tp, typename... _Tpls>
1533 struct __make_1st_indices<_Tp, _Tpls...>
1534 {
1535 typedef typename std::_Build_index_tuple<std::tuple_size<
1536 typename std::remove_reference<_Tp>::type>::value>::__type __type;
1537 };
1538
1539 // Performs the actual concatenation by step-wise expanding tuple-like
1540 // objects into the elements, which are finally forwarded into the
1541 // result tuple.
1542 template<typename _Ret, typename _Indices, typename... _Tpls>
1543 struct __tuple_concater;
1544
1545 template<typename _Ret, std::size_t... _Is, typename _Tp, typename... _Tpls>
1546 struct __tuple_concater<_Ret, std::_Index_tuple<_Is...>, _Tp, _Tpls...>
1547 {
1548 template<typename... _Us>
1549 static constexpr _Ret
1550 _S_do(_Tp&& __tp, _Tpls&&... __tps, _Us&&... __us)
1551 {
1552 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1553 typedef __tuple_concater<_Ret, __idx, _Tpls...> __next;
1554 return __next::_S_do(std::forward<_Tpls>(__tps)...,
1555 std::forward<_Us>(__us)...,
1556 std::get<_Is>(std::forward<_Tp>(__tp))...);
1557 }
1558 };
1559
1560 template<typename _Ret>
1561 struct __tuple_concater<_Ret, std::_Index_tuple<>>
1562 {
1563 template<typename... _Us>
1564 static constexpr _Ret
1565 _S_do(_Us&&... __us)
1566 {
1567 return _Ret(std::forward<_Us>(__us)...);
1568 }
1569 };
1570
1571 /// tuple_cat
1572 template<typename... _Tpls, typename = typename
1573 enable_if<__and_<__is_tuple_like<_Tpls>...>::value>::type>
1574 constexpr auto
1575 tuple_cat(_Tpls&&... __tpls)
1576 -> typename __tuple_cat_result<_Tpls...>::__type
1577 {
1578 typedef typename __tuple_cat_result<_Tpls...>::__type __ret;
1579 typedef typename __make_1st_indices<_Tpls...>::__type __idx;
1580 typedef __tuple_concater<__ret, __idx, _Tpls...> __concater;
1581 return __concater::_S_do(std::forward<_Tpls>(__tpls)...);
1582 }
1583
1584 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1585 // 2301. Why is tie not constexpr?
1586 /// tie
1587 template<typename... _Elements>
1588 constexpr tuple<_Elements&...>
1589 tie(_Elements&... __args) noexcept
1590 { return tuple<_Elements&...>(__args...); }
1591
1592 /// swap
1593 template<typename... _Elements>
1594 inline
1595#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
1596 // Constrained free swap overload, see p0185r1
1597 typename enable_if<__and_<__is_swappable<_Elements>...>::value
1598 >::type
1599#else
1600 void
1601#endif
1602 swap(tuple<_Elements...>& __x, tuple<_Elements...>& __y)
1603 noexcept(noexcept(__x.swap(__y)))
1604 { __x.swap(__y); }
1605
1606#if __cplusplus201103L > 201402L || !defined(__STRICT_ANSI__1) // c++1z or gnu++11
1607 template<typename... _Elements>
1608 typename enable_if<!__and_<__is_swappable<_Elements>...>::value>::type
1609 swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
1610#endif
1611
1612 // A class (and instance) which can be used in 'tie' when an element
1613 // of a tuple is not required.
1614 // _GLIBCXX14_CONSTEXPR
1615 // 2933. PR for LWG 2773 could be clearer
1616 struct _Swallow_assign
1617 {
1618 template<class _Tp>
1619 _GLIBCXX14_CONSTEXPR const _Swallow_assign&
1620 operator=(const _Tp&) const
1621 { return *this; }
1622 };
1623
1624 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1625 // 2773. Making std::ignore constexpr
1626 _GLIBCXX17_INLINE constexpr _Swallow_assign ignore{};
1627
1628 /// Partial specialization for tuples
1629 template<typename... _Types, typename _Alloc>
1630 struct uses_allocator<tuple<_Types...>, _Alloc> : true_type { };
1631
1632 // See stl_pair.h...
1633 template<class _T1, class _T2>
1634 template<typename... _Args1, typename... _Args2>
1635 inline
1636 pair<_T1, _T2>::
1637 pair(piecewise_construct_t,
1638 tuple<_Args1...> __first, tuple<_Args2...> __second)
1639 : pair(__first, __second,
1640 typename _Build_index_tuple<sizeof...(_Args1)>::__type(),
1641 typename _Build_index_tuple<sizeof...(_Args2)>::__type())
1642 { }
1643
1644 template<class _T1, class _T2>
1645 template<typename... _Args1, std::size_t... _Indexes1,
1646 typename... _Args2, std::size_t... _Indexes2>
1647 inline
1648 pair<_T1, _T2>::
1649 pair(tuple<_Args1...>& __tuple1, tuple<_Args2...>& __tuple2,
1650 _Index_tuple<_Indexes1...>, _Index_tuple<_Indexes2...>)
1651 : first(std::forward<_Args1>(std::get<_Indexes1>(__tuple1))...),
1652 second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
1653 { }
1654
1655#if __cplusplus201103L > 201402L
1656# define __cpp_lib_apply 201603
1657
1658 template <typename _Fn, typename _Tuple, size_t... _Idx>
1659 constexpr decltype(auto)
1660 __apply_impl(_Fn&& __f, _Tuple&& __t, index_sequence<_Idx...>)
1661 {
1662 return std::__invoke(std::forward<_Fn>(__f),
1663 std::get<_Idx>(std::forward<_Tuple>(__t))...);
1664 }
1665
1666 template <typename _Fn, typename _Tuple>
1667 constexpr decltype(auto)
1668 apply(_Fn&& __f, _Tuple&& __t)
1669 {
1670 using _Indices = make_index_sequence<tuple_size_v<decay_t<_Tuple>>>;
1671 return std::__apply_impl(std::forward<_Fn>(__f),
1672 std::forward<_Tuple>(__t),
1673 _Indices{});
1674 }
1675
1676#define __cpp_lib_make_from_tuple 201606
1677
1678 template <typename _Tp, typename _Tuple, size_t... _Idx>
1679 constexpr _Tp
1680 __make_from_tuple_impl(_Tuple&& __t, index_sequence<_Idx...>)
1681 { return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...); }
1682
1683 template <typename _Tp, typename _Tuple>
1684 constexpr _Tp
1685 make_from_tuple(_Tuple&& __t)
1686 {
1687 return __make_from_tuple_impl<_Tp>(
1688 std::forward<_Tuple>(__t),
1689 make_index_sequence<tuple_size_v<decay_t<_Tuple>>>{});
1690 }
1691#endif // C++17
1692
1693 /// @}
1694
1695_GLIBCXX_END_NAMESPACE_VERSION
1696} // namespace std
1697
1698#endif // C++11
1699
1700#endif // _GLIBCXX_TUPLE

/build/llvm-toolchain-snapshot-7~svn326551/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/ADT/SmallVector.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/Config/abi-breaking.h"
22#include "llvm/Support/AlignOf.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/ErrorOr.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29#include <cassert>
30#include <cstdint>
31#include <cstdlib>
32#include <functional>
33#include <memory>
34#include <new>
35#include <string>
36#include <system_error>
37#include <type_traits>
38#include <utility>
39#include <vector>
40
41namespace llvm {
42
43class ErrorSuccess;
44
45/// Base class for error info classes. Do not extend this directly: Extend
46/// the ErrorInfo template subclass instead.
47class ErrorInfoBase {
48public:
49 virtual ~ErrorInfoBase() = default;
50
51 /// Print an error message to an output stream.
52 virtual void log(raw_ostream &OS) const = 0;
53
54 /// Return the error message as a string.
55 virtual std::string message() const {
56 std::string Msg;
57 raw_string_ostream OS(Msg);
58 log(OS);
59 return OS.str();
60 }
61
62 /// Convert this error to a std::error_code.
63 ///
64 /// This is a temporary crutch to enable interaction with code still
65 /// using std::error_code. It will be removed in the future.
66 virtual std::error_code convertToErrorCode() const = 0;
67
68 // Returns the class ID for this type.
69 static const void *classID() { return &ID; }
70
71 // Returns the class ID for the dynamic type of this ErrorInfoBase instance.
72 virtual const void *dynamicClassID() const = 0;
73
74 // Check whether this instance is a subclass of the class identified by
75 // ClassID.
76 virtual bool isA(const void *const ClassID) const {
77 return ClassID == classID();
78 }
79
80 // Check whether this instance is a subclass of ErrorInfoT.
81 template <typename ErrorInfoT> bool isA() const {
82 return isA(ErrorInfoT::classID());
83 }
84
85private:
86 virtual void anchor();
87
88 static char ID;
89};
90
91/// Lightweight error class with error context and mandatory checking.
92///
93/// Instances of this class wrap a ErrorInfoBase pointer. Failure states
94/// are represented by setting the pointer to a ErrorInfoBase subclass
95/// instance containing information describing the failure. Success is
96/// represented by a null pointer value.
97///
98/// Instances of Error also contains a 'Checked' flag, which must be set
99/// before the destructor is called, otherwise the destructor will trigger a
100/// runtime error. This enforces at runtime the requirement that all Error
101/// instances be checked or returned to the caller.
102///
103/// There are two ways to set the checked flag, depending on what state the
104/// Error instance is in. For Error instances indicating success, it
105/// is sufficient to invoke the boolean conversion operator. E.g.:
106///
107/// @code{.cpp}
108/// Error foo(<...>);
109///
110/// if (auto E = foo(<...>))
111/// return E; // <- Return E if it is in the error state.
112/// // We have verified that E was in the success state. It can now be safely
113/// // destroyed.
114/// @endcode
115///
116/// A success value *can not* be dropped. For example, just calling 'foo(<...>)'
117/// without testing the return value will raise a runtime error, even if foo
118/// returns success.
119///
120/// For Error instances representing failure, you must use either the
121/// handleErrors or handleAllErrors function with a typed handler. E.g.:
122///
123/// @code{.cpp}
124/// class MyErrorInfo : public ErrorInfo<MyErrorInfo> {
125/// // Custom error info.
126/// };
127///
128/// Error foo(<...>) { return make_error<MyErrorInfo>(...); }
129///
130/// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo.
131/// auto NewE =
132/// handleErrors(E,
133/// [](const MyErrorInfo &M) {
134/// // Deal with the error.
135/// },
136/// [](std::unique_ptr<OtherError> M) -> Error {
137/// if (canHandle(*M)) {
138/// // handle error.
139/// return Error::success();
140/// }
141/// // Couldn't handle this error instance. Pass it up the stack.
142/// return Error(std::move(M));
143/// );
144/// // Note - we must check or return NewE in case any of the handlers
145/// // returned a new error.
146/// @endcode
147///
148/// The handleAllErrors function is identical to handleErrors, except
149/// that it has a void return type, and requires all errors to be handled and
150/// no new errors be returned. It prevents errors (assuming they can all be
151/// handled) from having to be bubbled all the way to the top-level.
152///
153/// *All* Error instances must be checked before destruction, even if
154/// they're moved-assigned or constructed from Success values that have already
155/// been checked. This enforces checking through all levels of the call stack.
156class LLVM_NODISCARD[[clang::warn_unused_result]] Error {
157 // ErrorList needs to be able to yank ErrorInfoBase pointers out of this
158 // class to add to the error list.
159 friend class ErrorList;
160
161 // handleErrors needs to be able to set the Checked flag.
162 template <typename... HandlerTs>
163 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
164
165 // Expected<T> needs to be able to steal the payload when constructed from an
166 // error.
167 template <typename T> friend class Expected;
168
169protected:
170 /// Create a success value. Prefer using 'Error::success()' for readability
171 Error() {
172 setPtr(nullptr);
269
Calling 'Error::setPtr'
270
Returning from 'Error::setPtr'
173 setChecked(false);
174 }
175
176public:
177 /// Create a success value.
178 static ErrorSuccess success();
179
180 // Errors are not copy-constructable.
181 Error(const Error &Other) = delete;
182
183 /// Move-construct an error value. The newly constructed error is considered
184 /// unchecked, even if the source error had been checked. The original error
185 /// becomes a checked Success value, regardless of its original state.
186 Error(Error &&Other) {
187 setChecked(true);
326
Calling 'Error::setChecked'
328
Returning from 'Error::setChecked'
364
Calling 'Error::setChecked'
366
Returning from 'Error::setChecked'
188 *this = std::move(Other);
275
Calling 'move'
276
Returning from 'move'
277
Calling move assignment operator for 'Error'
284
Returning from move assignment operator for 'Error'
289
Calling 'move'
290
Returning from 'move'
291
Calling move assignment operator for 'Error'
298
Returning from move assignment operator for 'Error'
301
Calling 'move'
302
Returning from 'move'
303
Calling move assignment operator for 'Error'
310
Returning from move assignment operator for 'Error'
313
Calling 'move'
314
Returning from 'move'
315
Calling move assignment operator for 'Error'
322
Returning from move assignment operator for 'Error'
329
Calling 'move'
330
Returning from 'move'
331
Calling move assignment operator for 'Error'
348
Returning from move assignment operator for 'Error'
367
Calling 'move'
368
Returning from 'move'
369
Calling move assignment operator for 'Error'
386
Returning from move assignment operator for 'Error'
189 }
190
191 /// Create an error value. Prefer using the 'make_error' function, but
192 /// this constructor can be useful when "re-throwing" errors from handlers.
193 Error(std::unique_ptr<ErrorInfoBase> Payload) {
194 setPtr(Payload.release());
195 setChecked(false);
196 }
197
198 // Errors are not copy-assignable.
199 Error &operator=(const Error &Other) = delete;
200
201 /// Move-assign an error value. The current error must represent success, you
202 /// you cannot overwrite an unhandled error. The current error is then
203 /// considered unchecked. The source error becomes a checked success value,
204 /// regardless of its original state.
205 Error &operator=(Error &&Other) {
206 // Don't allow overwriting of unchecked values.
207 assertIsChecked();
332
Calling 'Error::assertIsChecked'
335
Returning from 'Error::assertIsChecked'
370
Calling 'Error::assertIsChecked'
373
Returning from 'Error::assertIsChecked'
208 setPtr(Other.getPtr());
278
Calling 'Error::getPtr'
279
Returning from 'Error::getPtr'
280
Calling 'Error::setPtr'
281
Returning from 'Error::setPtr'
292
Calling 'Error::getPtr'
293
Returning from 'Error::getPtr'
294
Calling 'Error::setPtr'
295
Returning from 'Error::setPtr'
304
Calling 'Error::getPtr'
305
Returning from 'Error::getPtr'
306
Calling 'Error::setPtr'
307
Returning from 'Error::setPtr'
316
Calling 'Error::getPtr'
317
Returning from 'Error::getPtr'
318
Calling 'Error::setPtr'
319
Returning from 'Error::setPtr'
336
Calling 'Error::getPtr'
337
Returning from 'Error::getPtr'
338
Calling 'Error::setPtr'
339
Returning from 'Error::setPtr'
374
Calling 'Error::getPtr'
375
Returning from 'Error::getPtr'
376
Calling 'Error::setPtr'
377
Returning from 'Error::setPtr'
209
210 // This Error is unchecked, even if the source error was checked.
211 setChecked(false);
340
Calling 'Error::setChecked'
342
Returning from 'Error::setChecked'
378
Calling 'Error::setChecked'
380
Returning from 'Error::setChecked'
212
213 // Null out Other's payload and set its checked bit.
214 Other.setPtr(nullptr);
282
Calling 'Error::setPtr'
283
Returning from 'Error::setPtr'
296
Calling 'Error::setPtr'
297
Returning from 'Error::setPtr'
308
Calling 'Error::setPtr'
309
Returning from 'Error::setPtr'
320
Calling 'Error::setPtr'
321
Returning from 'Error::setPtr'
343
Calling 'Error::setPtr'
344
Returning from 'Error::setPtr'
381
Calling 'Error::setPtr'
382
Returning from 'Error::setPtr'
215 Other.setChecked(true);
345
Calling 'Error::setChecked'
347
Returning from 'Error::setChecked'
383
Calling 'Error::setChecked'
385
Returning from 'Error::setChecked'
216
217 return *this;
218 }
219
220 /// Destroy a Error. Fails with a call to abort() if the error is
221 /// unchecked.
222 ~Error() {
223 assertIsChecked();
469
Calling 'Error::assertIsChecked'
472
Returning from 'Error::assertIsChecked'
224 delete getPtr();
473
Calling 'Error::getPtr'
474
Returning from 'Error::getPtr'
225 }
226
227 /// Bool conversion. Returns true if this Error is in a failure state,
228 /// and false if it is in an accept state. If the error is in a Success state
229 /// it will be considered checked.
230 explicit operator bool() {
231 setChecked(getPtr() == nullptr);
351
Calling 'Error::getPtr'
352
Returning from 'Error::getPtr'
353
Calling 'Error::setChecked'
356
Returning from 'Error::setChecked'
232 return getPtr() != nullptr;
357
Calling 'Error::getPtr'
358
Returning from 'Error::getPtr'
233 }
234
235 /// Check whether one error is a subclass of another.
236 template <typename ErrT> bool isA() const {
237 return getPtr() && getPtr()->isA(ErrT::classID());
238 }
239
240 /// Returns the dynamic class id of this error, or null if this is a success
241 /// value.
242 const void* dynamicClassID() const {
243 if (!getPtr())
244 return nullptr;
245 return getPtr()->dynamicClassID();
246 }
247
248private:
249#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
250 // assertIsChecked() happens very frequently, but under normal circumstances
251 // is supposed to be a no-op. So we want it to be inlined, but having a bunch
252 // of debug prints can cause the function to be too large for inlining. So
253 // it's important that we define this function out of line so that it can't be
254 // inlined.
255 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
256 void fatalUncheckedError() const;
257#endif
258
259 void assertIsChecked() {
260#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
261 if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false))
333
Within the expansion of the macro 'LLVM_UNLIKELY':
a
Calling 'Error::getChecked'
b
Returning from 'Error::getChecked'
c
Calling 'Error::getPtr'
d
Returning from 'Error::getPtr'
334
Taking false branch
371
Within the expansion of the macro 'LLVM_UNLIKELY':
a
Calling 'Error::getChecked'
b
Returning from 'Error::getChecked'
c
Calling 'Error::getPtr'
d
Returning from 'Error::getPtr'
372
Taking false branch
470
Within the expansion of the macro 'LLVM_UNLIKELY':
a
Calling 'Error::getChecked'
b
Returning from 'Error::getChecked'
471
Taking false branch
262 fatalUncheckedError();
263#endif
264 }
265
266 ErrorInfoBase *getPtr() const {
267 return reinterpret_cast<ErrorInfoBase*>(
268 reinterpret_cast<uintptr_t>(Payload) &
269 ~static_cast<uintptr_t>(0x1));
270 }
271
272 void setPtr(ErrorInfoBase *EI) {
273#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
274 Payload = reinterpret_cast<ErrorInfoBase*>(
275 (reinterpret_cast<uintptr_t>(EI) &
276 ~static_cast<uintptr_t>(0x1)) |
277 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
278#else
279 Payload = EI;
280#endif
281 }
282
283 bool getChecked() const {
284#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
285 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
286#else
287 return true;
288#endif
289 }
290
291 void setChecked(bool V) {
292 Payload = reinterpret_cast<ErrorInfoBase*>(
293 (reinterpret_cast<uintptr_t>(Payload) &
294 ~static_cast<uintptr_t>(0x1)) |
295 (V ? 0 : 1));
327
'?' condition is true
341
'?' condition is false
346
'?' condition is true
354
Assuming 'V' is 0
355
'?' condition is false
365
'?' condition is true
379
'?' condition is false
384
'?' condition is true
296 }
297
298 std::unique_ptr<ErrorInfoBase> takePayload() {
299 std::unique_ptr<ErrorInfoBase> Tmp(getPtr());
300 setPtr(nullptr);
301 setChecked(true);
302 return Tmp;
303 }
304
305 ErrorInfoBase *Payload = nullptr;
306};
307
308/// Subclass of Error for the sole purpose of identifying the success path in
309/// the type system. This allows to catch invalid conversion to Expected<T> at
310/// compile time.
311class ErrorSuccess : public Error {};
312
313inline ErrorSuccess Error::success() { return ErrorSuccess(); }
267
Calling implicit default constructor for 'ErrorSuccess'
268
Calling default constructor for 'Error'
271
Returning from default constructor for 'Error'
272
Returning from default constructor for 'ErrorSuccess'
273
Calling implicit move constructor for 'ErrorSuccess'
274
Calling move constructor for 'Error'
285
Returning from move constructor for 'Error'
286
Returning from move constructor for 'ErrorSuccess'
314
315/// Make a Error instance representing failure using the given error info
316/// type.
317template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) {
318 return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(Args)...));
319}
320
321/// Base class for user error types. Users should declare their error types
322/// like:
323///
324/// class MyError : public ErrorInfo<MyError> {
325/// ....
326/// };
327///
328/// This class provides an implementation of the ErrorInfoBase::kind
329/// method, which is used by the Error RTTI system.
330template <typename ThisErrT, typename ParentErrT = ErrorInfoBase>
331class ErrorInfo : public ParentErrT {
332public:
333 static const void *classID() { return &ThisErrT::ID; }
334
335 const void *dynamicClassID() const override { return &ThisErrT::ID; }
336
337 bool isA(const void *const ClassID) const override {
338 return ClassID == classID() || ParentErrT::isA(ClassID);
339 }
340};
341
342/// Special ErrorInfo subclass representing a list of ErrorInfos.
343/// Instances of this class are constructed by joinError.
344class ErrorList final : public ErrorInfo<ErrorList> {
345 // handleErrors needs to be able to iterate the payload list of an
346 // ErrorList.
347 template <typename... HandlerTs>
348 friend Error handleErrors(Error E, HandlerTs &&... Handlers);
349
350 // joinErrors is implemented in terms of join.
351 friend Error joinErrors(Error, Error);
352
353public:
354 void log(raw_ostream &OS) const override {
355 OS << "Multiple errors:\n";
356 for (auto &ErrPayload : Payloads) {
357 ErrPayload->log(OS);
358 OS << "\n";
359 }
360 }
361
362 std::error_code convertToErrorCode() const override;
363
364 // Used by ErrorInfo::classID.
365 static char ID;
366
367private:
368 ErrorList(std::unique_ptr<ErrorInfoBase> Payload1,
369 std::unique_ptr<ErrorInfoBase> Payload2) {
370 assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&(static_cast <bool> (!Payload1->isA<ErrorList>
() && !Payload2->isA<ErrorList>() &&
"ErrorList constructor payloads should be singleton errors")
? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 371, __extension__ __PRETTY_FUNCTION__))
371 "ErrorList constructor payloads should be singleton errors")(static_cast <bool> (!Payload1->isA<ErrorList>
() && !Payload2->isA<ErrorList>() &&
"ErrorList constructor payloads should be singleton errors")
? void (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 371, __extension__ __PRETTY_FUNCTION__))
;
372 Payloads.push_back(std::move(Payload1));
373 Payloads.push_back(std::move(Payload2));
374 }
375
376 static Error join(Error E1, Error E2) {
377 if (!E1)
378 return E2;
379 if (!E2)
380 return E1;
381 if (E1.isA<ErrorList>()) {
382 auto &E1List = static_cast<ErrorList &>(*E1.getPtr());
383 if (E2.isA<ErrorList>()) {
384 auto E2Payload = E2.takePayload();
385 auto &E2List = static_cast<ErrorList &>(*E2Payload);
386 for (auto &Payload : E2List.Payloads)
387 E1List.Payloads.push_back(std::move(Payload));
388 } else
389 E1List.Payloads.push_back(E2.takePayload());
390
391 return E1;
392 }
393 if (E2.isA<ErrorList>()) {
394 auto &E2List = static_cast<ErrorList &>(*E2.getPtr());
395 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
396 return E2;
397 }
398 return Error(std::unique_ptr<ErrorList>(
399 new ErrorList(E1.takePayload(), E2.takePayload())));
400 }
401
402 std::vector<std::unique_ptr<ErrorInfoBase>> Payloads;
403};
404
405/// Concatenate errors. The resulting Error is unchecked, and contains the
406/// ErrorInfo(s), if any, contained in E1, followed by the
407/// ErrorInfo(s), if any, contained in E2.
408inline Error joinErrors(Error E1, Error E2) {
409 return ErrorList::join(std::move(E1), std::move(E2));
410}
411
412/// Tagged union holding either a T or a Error.
413///
414/// This class parallels ErrorOr, but replaces error_code with Error. Since
415/// Error cannot be copied, this class replaces getError() with
416/// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the
417/// error class type.
418template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected {
419 template <class T1> friend class ExpectedAsOutParameter;
420 template <class OtherT> friend class Expected;
421
422 static const bool isRef = std::is_reference<T>::value;
423
424 using wrap = ReferenceStorage<typename std::remove_reference<T>::type>;
425
426 using error_type = std::unique_ptr<ErrorInfoBase>;
427
428public:
429 using storage_type = typename std::conditional<isRef, wrap, T>::type;
430 using value_type = T;
431
432private:
433 using reference = typename std::remove_reference<T>::type &;
434 using const_reference = const typename std::remove_reference<T>::type &;
435 using pointer = typename std::remove_reference<T>::type *;
436 using const_pointer = const typename std::remove_reference<T>::type *;
437
438public:
439 /// Create an Expected<T> error value from the given Error.
440 Expected(Error Err)
441 : HasError(true)
442#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
443 // Expected is unchecked upon construction in Debug builds.
444 , Unchecked(true)
445#endif
446 {
447 assert(Err && "Cannot create Expected<T> from Error success value.")(static_cast <bool> (Err && "Cannot create Expected<T> from Error success value."
) ? void (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 447, __extension__ __PRETTY_FUNCTION__))
;
448 new (getErrorStorage()) error_type(Err.takePayload());
449 }
450
451 /// Forbid to convert from Error::success() implicitly, this avoids having
452 /// Expected<T> foo() { return Error::success(); } which compiles otherwise
453 /// but triggers the assertion above.
454 Expected(ErrorSuccess) = delete;
455
456 /// Create an Expected<T> success value from the given OtherT value, which
457 /// must be convertible to T.
458 template <typename OtherT>
459 Expected(OtherT &&Val,
460 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
461 * = nullptr)
462 : HasError(false)
463#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
464 // Expected is unchecked upon construction in Debug builds.
465 , Unchecked(true)
466#endif
467 {
468 new (getStorage()) storage_type(std::forward<OtherT>(Val));
469 }
470
471 /// Move construct an Expected<T> value.
472 Expected(Expected &&Other) { moveConstruct(std::move(Other)); }
184
Calling 'move'
185
Returning from 'move'
186
Calling 'Expected::moveConstruct'
261
Returning from 'Expected::moveConstruct'
389
Calling 'move'
390
Returning from 'move'
391
Calling 'Expected::moveConstruct'
466
Returning from 'Expected::moveConstruct'
473
474 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
475 /// must be convertible to T.
476 template <class OtherT>
477 Expected(Expected<OtherT> &&Other,
478 typename std::enable_if<std::is_convertible<OtherT, T>::value>::type
479 * = nullptr) {
480 moveConstruct(std::move(Other));
481 }
482
483 /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT
484 /// isn't convertible to T.
485 template <class OtherT>
486 explicit Expected(
487 Expected<OtherT> &&Other,
488 typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * =
489 nullptr) {
490 moveConstruct(std::move(Other));
491 }
492
493 /// Move-assign from another Expected<T>.
494 Expected &operator=(Expected &&Other) {
495 moveAssign(std::move(Other));
496 return *this;
497 }
498
499 /// Destroy an Expected<T>.
500 ~Expected() {
501 assertIsChecked();
502 if (!HasError)
477
Assuming the condition is true
478
Taking true branch
503 getStorage()->~storage_type();
504 else
505 getErrorStorage()->~error_type();
506 }
507
508 /// \brief Return false if there is an error.
509 explicit operator bool() {
510#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
511 Unchecked = HasError;
512#endif
513 return !HasError;
514 }
515
516 /// \brief Returns a reference to the stored T value.
517 reference get() {
518 assertIsChecked();
519 return *getStorage();
520 }
521
522 /// \brief Returns a const reference to the stored T value.
523 const_reference get() const {
524 assertIsChecked();
525 return const_cast<Expected<T> *>(this)->get();
526 }
527
528 /// \brief Check that this Expected<T> is an error of type ErrT.
529 template <typename ErrT> bool errorIsA() const {
530 return HasError && (*getErrorStorage())->template isA<ErrT>();
531 }
532
533 /// \brief Take ownership of the stored error.
534 /// After calling this the Expected<T> is in an indeterminate state that can
535 /// only be safely destructed. No further calls (beside the destructor) should
536 /// be made on the Expected<T> vaule.
537 Error takeError() {
538#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
539 Unchecked = false;
540#endif
541 return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
264
Assuming the condition is false
265
'?' condition is false
266
Calling 'Error::success'
287
Returning from 'Error::success'
288
Calling move constructor for 'Error'
299
Returning from move constructor for 'Error'
300
Calling move constructor for 'Error'
311
Returning from move constructor for 'Error'
312
Calling move constructor for 'Error'
323
Returning from move constructor for 'Error'
542 }
543
544 /// \brief Returns a pointer to the stored T value.
545 pointer operator->() {
546 assertIsChecked();
547 return toPointer(getStorage());
548 }
549
550 /// \brief Returns a const pointer to the stored T value.
551 const_pointer operator->() const {
552 assertIsChecked();
553 return toPointer(getStorage());
554 }
555
556 /// \brief Returns a reference to the stored T value.
557 reference operator*() {
558 assertIsChecked();
559 return *getStorage();
560 }
561
562 /// \brief Returns a const reference to the stored T value.
563 const_reference operator*() const {
564 assertIsChecked();
565 return *getStorage();
566 }
567
568private:
569 template <class T1>
570 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
571 return &a == &b;
572 }
573
574 template <class T1, class T2>
575 static bool compareThisIfSameType(const T1 &a, const T2 &b) {
576 return false;
577 }
578
579 template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) {
580 HasError = Other.HasError;
581#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
582 Unchecked = true;
583 Other.Unchecked = false;
584#endif
585
586 if (!HasError)
187
Assuming the condition is false
188
Taking false branch
392
Assuming the condition is false
393
Taking false branch
587 new (getStorage()) storage_type(std::move(*Other.getStorage()));
588 else
589 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
189
Calling 'operator new'
190
Returning from 'operator new'
191
Calling 'move'
192
Returning from 'move'
193
Calling move constructor for 'unique_ptr'
260
Returning from move constructor for 'unique_ptr'
394
Calling 'operator new'
395
Returning from 'operator new'
396
Calling 'move'
397
Returning from 'move'
398
Calling move constructor for 'unique_ptr'
465
Returning from move constructor for 'unique_ptr'
590 }
591
592 template <class OtherT> void moveAssign(Expected<OtherT> &&Other) {
593 assertIsChecked();
594
595 if (compareThisIfSameType(*this, Other))
596 return;
597
598 this->~Expected();
599 new (this) Expected(std::move(Other));
600 }
601
602 pointer toPointer(pointer Val) { return Val; }
603
604 const_pointer toPointer(const_pointer Val) const { return Val; }
605
606 pointer toPointer(wrap *Val) { return &Val->get(); }
607
608 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
609
610 storage_type *getStorage() {
611 assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!"
) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 611, __extension__ __PRETTY_FUNCTION__))
;
612 return reinterpret_cast<storage_type *>(TStorage.buffer);
613 }
614
615 const storage_type *getStorage() const {
616 assert(!HasError && "Cannot get value when an error exists!")(static_cast <bool> (!HasError && "Cannot get value when an error exists!"
) ? void (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 616, __extension__ __PRETTY_FUNCTION__))
;
617 return reinterpret_cast<const storage_type *>(TStorage.buffer);
618 }
619
620 error_type *getErrorStorage() {
621 assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!"
) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 621, __extension__ __PRETTY_FUNCTION__))
;
622 return reinterpret_cast<error_type *>(ErrorStorage.buffer);
623 }
624
625 const error_type *getErrorStorage() const {
626 assert(HasError && "Cannot get error when a value exists!")(static_cast <bool> (HasError && "Cannot get error when a value exists!"
) ? void (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 626, __extension__ __PRETTY_FUNCTION__))
;
627 return reinterpret_cast<const error_type *>(ErrorStorage.buffer);
628 }
629
630 // Used by ExpectedAsOutParameter to reset the checked flag.
631 void setUnchecked() {
632#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
633 Unchecked = true;
634#endif
635 }
636
637#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
638 LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn))
639 LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline))
640 void fatalUncheckedExpected() const {
641 dbgs() << "Expected<T> must be checked before access or destruction.\n";
642 if (HasError) {
643 dbgs() << "Unchecked Expected<T> contained error:\n";
644 (*getErrorStorage())->log(dbgs());
645 } else
646 dbgs() << "Expected<T> value was in success state. (Note: Expected<T> "
647 "values in success mode must still be checked prior to being "
648 "destroyed).\n";
649 abort();
650 }
651#endif
652
653 void assertIsChecked() {
654#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
655 if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false))
656 fatalUncheckedExpected();
657#endif
658 }
659
660 union {
661 AlignedCharArrayUnion<storage_type> TStorage;
662 AlignedCharArrayUnion<error_type> ErrorStorage;
663 };
664 bool HasError : 1;
665#if LLVM_ENABLE_ABI_BREAKING_CHECKS1
666 bool Unchecked : 1;
667#endif
668};
669
670/// Report a serious error, calling any installed error handler. See
671/// ErrorHandling.h.
672LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err,
673 bool gen_crash_diag = true);
674
675/// Report a fatal error if Err is a failure value.
676///
677/// This function can be used to wrap calls to fallible functions ONLY when it
678/// is known that the Error will always be a success value. E.g.
679///
680/// @code{.cpp}
681/// // foo only attempts the fallible operation if DoFallibleOperation is
682/// // true. If DoFallibleOperation is false then foo always returns
683/// // Error::success().
684/// Error foo(bool DoFallibleOperation);
685///
686/// cantFail(foo(false));
687/// @endcode
688inline void cantFail(Error Err, const char *Msg = nullptr) {
689 if (Err) {
690 if (!Msg)
691 Msg = "Failure value returned from cantFail wrapped call";
692 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 692)
;
693 }
694}
695
696/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
697/// returns the contained value.
698///
699/// This function can be used to wrap calls to fallible functions ONLY when it
700/// is known that the Error will always be a success value. E.g.
701///
702/// @code{.cpp}
703/// // foo only attempts the fallible operation if DoFallibleOperation is
704/// // true. If DoFallibleOperation is false then foo always returns an int.
705/// Expected<int> foo(bool DoFallibleOperation);
706///
707/// int X = cantFail(foo(false));
708/// @endcode
709template <typename T>
710T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) {
711 if (ValOrErr)
712 return std::move(*ValOrErr);
713 else {
714 if (!Msg)
715 Msg = "Failure value returned from cantFail wrapped call";
716 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 716)
;
717 }
718}
719
720/// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and
721/// returns the contained reference.
722///
723/// This function can be used to wrap calls to fallible functions ONLY when it
724/// is known that the Error will always be a success value. E.g.
725///
726/// @code{.cpp}
727/// // foo only attempts the fallible operation if DoFallibleOperation is
728/// // true. If DoFallibleOperation is false then foo always returns a Bar&.
729/// Expected<Bar&> foo(bool DoFallibleOperation);
730///
731/// Bar &X = cantFail(foo(false));
732/// @endcode
733template <typename T>
734T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) {
735 if (ValOrErr)
736 return *ValOrErr;
737 else {
738 if (!Msg)
739 Msg = "Failure value returned from cantFail wrapped call";
740 llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 740)
;
741 }
742}
743
744/// Helper for testing applicability of, and applying, handlers for
745/// ErrorInfo types.
746template <typename HandlerT>
747class ErrorHandlerTraits
748 : public ErrorHandlerTraits<decltype(
749 &std::remove_reference<HandlerT>::type::operator())> {};
750
751// Specialization functions of the form 'Error (const ErrT&)'.
752template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> {
753public:
754 static bool appliesTo(const ErrorInfoBase &E) {
755 return E.template isA<ErrT>();
756 }
757
758 template <typename HandlerT>
759 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
760 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 760, __extension__ __PRETTY_FUNCTION__))
;
761 return H(static_cast<ErrT &>(*E));
762 }
763};
764
765// Specialization functions of the form 'void (const ErrT&)'.
766template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> {
767public:
768 static bool appliesTo(const ErrorInfoBase &E) {
769 return E.template isA<ErrT>();
770 }
771
772 template <typename HandlerT>
773 static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) {
774 assert(appliesTo(*E) && "Applying incorrect handler")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 774, __extension__ __PRETTY_FUNCTION__))
;
775 H(static_cast<ErrT &>(*E));
776 return Error::success();
777 }
778};
779
780/// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'.
781template <typename ErrT>
782class ErrorHandlerTraits<Error (&)(std::unique_ptr<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")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 790, __extension__ __PRETTY_FUNCTION__))
;
791 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
792 return H(std::move(SubE));
793 }
794};
795
796/// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'.
797template <typename ErrT>
798class ErrorHandlerTraits<void (&)(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")(static_cast <bool> (appliesTo(*E) && "Applying incorrect handler"
) ? void (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\""
, "/build/llvm-toolchain-snapshot-7~svn326551/include/llvm/Support/Error.h"
, 806, __extension__ __PRETTY_FUNCTION__))
;
807 std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release()));
808 H(std::move(SubE));
809 return Error::success();
810 }
811};
812
813// Specialization for member functions of the form 'RetT (const ErrT&)'.
814template <typename C, typename RetT, typename ErrT>
815class ErrorHandlerTraits<RetT (C::*)(ErrT &)>
816 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
817
818// Specialization for member functions of the form 'RetT (const ErrT&) const'.
819template <typename C, typename RetT, typename ErrT>
820class ErrorHandlerTraits<RetT (C::*)(ErrT &) const>
821 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
822
823// Specialization for member functions of the form 'RetT (const ErrT&)'.
824template <typename C, typename RetT, typename ErrT>
825class ErrorHandlerTraits<RetT (C::*)(const ErrT &)>
826 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
827
828// Specialization for member functions of the form 'RetT (const ErrT&) const'.
829template <typename C, typename RetT, typename ErrT>
830class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const>
831 : public ErrorHandlerTraits<RetT (&)(ErrT &)> {};
832
833/// Specialization for member functions of the form
834/// 'RetT (std::unique_ptr<ErrT>)'.
835template <typename C, typename RetT, typename ErrT>
836class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)>
837 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
838
839/// Specialization for member functions of the form
840/// 'RetT (std::unique_ptr<ErrT>) const'.
841template <typename C, typename RetT, typename ErrT>
842class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const>
843 : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {};
844
845inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) {
846 return Error(std::move(Payload));
847}
848
849template <typename HandlerT, typename... HandlerTs>
850Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload,
851 HandlerT &&Handler, HandlerTs &&... Handlers) {
852 if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload))
853 return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler),
854 std::move(Payload));
855 return handleErrorImpl(std::move(Payload),
856 std::forward<HandlerTs>(Handlers)...);
857}
858
859/// Pass the ErrorInfo(s) contained in E to their respective handlers. Any
860/// unhandled errors (or Errors returned by handlers) are re-concatenated and
861/// returned.
862/// Because this function returns an error, its result must also be checked
863/// or returned. If you intend to handle all errors use handleAllErrors
864/// (which returns void, and will abort() on unhandled errors) instead.
865template <typename... HandlerTs>
866Error handleErrors(Error E, HandlerTs &&... Hs) {
867 if (!E)
868 return Error::success();
869
870 std::unique_ptr<ErrorInfoBase> Payload = E.takePayload();
871
872 if (Payload->isA<ErrorList>()) {
873 ErrorList &List = static_cast<ErrorList &>(*Payload);
874 Error R;
875 for (auto &P : List.Payloads)
876 R = ErrorList::join(
877 std::move(R),
878 handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...));
879 return R;
880 }
881
882 return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...);
883}
884
885/// Behaves the same as handleErrors, except that by contract all errors
886/// *must* be handled by the given handlers (i.e. there must be no remaining
887/// errors after running the handlers, or llvm_unreachable is called).
888template <typename... HandlerTs>
889void handleAllErrors(Error E, HandlerTs &&... Handlers) {
890 cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...));
891}
892
893/// Check that E is a non-error, then drop it.
894/// If E is an error, llvm_unreachable will be called.
895inline void handleAllErrors(Error E) {
896 cantFail(std::move(E));
897}
898
899/// Handle any errors (if present) in an Expected<T>, then try a recovery path.
900///
901/// If the incoming value is a success value it is returned unmodified. If it
902/// is a failure value then it the contained error is passed to handleErrors.
903/// If handleErrors is able to handle the error then the RecoveryPath functor
904/// is called to supply the final result. If handleErrors is not able to
905/// handle all errors then the unhandled errors are returned.
906///
907/// This utility enables the follow pattern:
908///
909/// @code{.cpp}
910/// enum FooStrategy { Aggressive, Conservative };
911/// Expected<Foo> foo(FooStrategy S);
912///
913/// auto ResultOrErr =
914/// handleExpected(
915/// foo(Aggressive),
916/// []() { return foo(Conservative); },
917/// [](AggressiveStrategyError&) {
918/// // Implicitly conusme this - we'll recover by using a conservative
919/// // strategy.
920/// });
921///
922/// @endcode
923template <typename T, typename RecoveryFtor, typename... HandlerTs>
924Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath,
925 HandlerTs &&... Handlers) {
926 if (ValOrErr)
927 return ValOrErr;
928
929 if (auto Err = handleErrors(ValOrErr.takeError(),
930 std::forward<HandlerTs>(Handlers)...))
931 return std::move(Err);
932
933 return RecoveryPath();
934}
935
936/// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner
937/// will be printed before the first one is logged. A newline will be printed
938/// after each error.
939///
940/// This is useful in the base level of your program to allow clean termination
941/// (allowing clean deallocation of resources, etc.), while reporting error
942/// information to the user.
943void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner);
944
945/// Write all error messages (if any) in E to a string. The newline character
946/// is used to separate error messages.
947inline std::string toString(Error E) {
948 SmallVector<std::string, 2> Errors;
949 handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) {
950 Errors.push_back(EI.message());
951 });