Bug Summary

File:tools/clang/tools/driver/cc1as_main.cpp
Warning:line 502, column 5
Use of memory after it is freed

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 cc1as_main.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 -relaxed-aliasing -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~svn329677/build-llvm/tools/clang/tools/driver -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver -I /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/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~svn329677/build-llvm/tools/clang/tools/driver -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fno-common -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-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp

/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp

1//===-- cc1as_main.cpp - Clang Assembler ---------------------------------===//
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 is the entry point to the clang -cc1as functionality, which implements
11// the direct interface to the LLVM MC based assembler.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/DiagnosticOptions.h"
17#include "clang/Driver/DriverDiagnostic.h"
18#include "clang/Driver/Options.h"
19#include "clang/Frontend/FrontendDiagnostic.h"
20#include "clang/Frontend/TextDiagnosticPrinter.h"
21#include "clang/Frontend/Utils.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/StringSwitch.h"
24#include "llvm/ADT/Triple.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/MC/MCAsmBackend.h"
27#include "llvm/MC/MCAsmInfo.h"
28#include "llvm/MC/MCCodeEmitter.h"
29#include "llvm/MC/MCContext.h"
30#include "llvm/MC/MCInstrInfo.h"
31#include "llvm/MC/MCObjectFileInfo.h"
32#include "llvm/MC/MCParser/MCAsmParser.h"
33#include "llvm/MC/MCParser/MCTargetAsmParser.h"
34#include "llvm/MC/MCRegisterInfo.h"
35#include "llvm/MC/MCStreamer.h"
36#include "llvm/MC/MCSubtargetInfo.h"
37#include "llvm/MC/MCTargetOptions.h"
38#include "llvm/Option/Arg.h"
39#include "llvm/Option/ArgList.h"
40#include "llvm/Option/OptTable.h"
41#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/ErrorHandling.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/FormattedStream.h"
45#include "llvm/Support/Host.h"
46#include "llvm/Support/MemoryBuffer.h"
47#include "llvm/Support/Path.h"
48#include "llvm/Support/Signals.h"
49#include "llvm/Support/SourceMgr.h"
50#include "llvm/Support/TargetRegistry.h"
51#include "llvm/Support/TargetSelect.h"
52#include "llvm/Support/Timer.h"
53#include "llvm/Support/raw_ostream.h"
54#include <memory>
55#include <system_error>
56using namespace clang;
57using namespace clang::driver;
58using namespace clang::driver::options;
59using namespace llvm;
60using namespace llvm::opt;
61
62namespace {
63
64/// \brief Helper class for representing a single invocation of the assembler.
65struct AssemblerInvocation {
66 /// @name Target Options
67 /// @{
68
69 /// The name of the target triple to assemble for.
70 std::string Triple;
71
72 /// If given, the name of the target CPU to determine which instructions
73 /// are legal.
74 std::string CPU;
75
76 /// The list of target specific features to enable or disable -- this should
77 /// be a list of strings starting with '+' or '-'.
78 std::vector<std::string> Features;
79
80 /// The list of symbol definitions.
81 std::vector<std::string> SymbolDefs;
82
83 /// @}
84 /// @name Language Options
85 /// @{
86
87 std::vector<std::string> IncludePaths;
88 unsigned NoInitialTextSection : 1;
89 unsigned SaveTemporaryLabels : 1;
90 unsigned GenDwarfForAssembly : 1;
91 unsigned RelaxELFRelocations : 1;
92 unsigned DwarfVersion;
93 std::string DwarfDebugFlags;
94 std::string DwarfDebugProducer;
95 std::string DebugCompilationDir;
96 llvm::DebugCompressionType CompressDebugSections =
97 llvm::DebugCompressionType::None;
98 std::string MainFileName;
99
100 /// @}
101 /// @name Frontend Options
102 /// @{
103
104 std::string InputFile;
105 std::vector<std::string> LLVMArgs;
106 std::string OutputPath;
107 enum FileType {
108 FT_Asm, ///< Assembly (.s) output, transliterate mode.
109 FT_Null, ///< No output, for timing purposes.
110 FT_Obj ///< Object file output.
111 };
112 FileType OutputType;
113 unsigned ShowHelp : 1;
114 unsigned ShowVersion : 1;
115
116 /// @}
117 /// @name Transliterate Options
118 /// @{
119
120 unsigned OutputAsmVariant;
121 unsigned ShowEncoding : 1;
122 unsigned ShowInst : 1;
123
124 /// @}
125 /// @name Assembler Options
126 /// @{
127
128 unsigned RelaxAll : 1;
129 unsigned NoExecStack : 1;
130 unsigned FatalWarnings : 1;
131 unsigned IncrementalLinkerCompatible : 1;
132
133 /// The name of the relocation model to use.
134 std::string RelocationModel;
135
136 /// @}
137
138public:
139 AssemblerInvocation() {
140 Triple = "";
141 NoInitialTextSection = 0;
142 InputFile = "-";
143 OutputPath = "-";
144 OutputType = FT_Asm;
145 OutputAsmVariant = 0;
146 ShowInst = 0;
147 ShowEncoding = 0;
148 RelaxAll = 0;
149 NoExecStack = 0;
150 FatalWarnings = 0;
151 IncrementalLinkerCompatible = 0;
152 DwarfVersion = 0;
153 }
154
155 static bool CreateFromArgs(AssemblerInvocation &Res,
156 ArrayRef<const char *> Argv,
157 DiagnosticsEngine &Diags);
158};
159
160}
161
162bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
163 ArrayRef<const char *> Argv,
164 DiagnosticsEngine &Diags) {
165 bool Success = true;
166
167 // Parse the arguments.
168 std::unique_ptr<OptTable> OptTbl(createDriverOptTable());
169
170 const unsigned IncludedFlagsBitmask = options::CC1AsOption;
171 unsigned MissingArgIndex, MissingArgCount;
172 InputArgList Args = OptTbl->ParseArgs(Argv, MissingArgIndex, MissingArgCount,
173 IncludedFlagsBitmask);
174
175 // Check for missing argument error.
176 if (MissingArgCount) {
177 Diags.Report(diag::err_drv_missing_argument)
178 << Args.getArgString(MissingArgIndex) << MissingArgCount;
179 Success = false;
180 }
181
182 // Issue errors on unknown arguments.
183 for (const Arg *A : Args.filtered(OPT_UNKNOWN)) {
184 auto ArgString = A->getAsString(Args);
185 std::string Nearest;
186 if (OptTbl->findNearest(ArgString, Nearest, IncludedFlagsBitmask) > 1)
187 Diags.Report(diag::err_drv_unknown_argument) << ArgString;
188 else
189 Diags.Report(diag::err_drv_unknown_argument_with_suggestion)
190 << ArgString << Nearest;
191 Success = false;
192 }
193
194 // Construct the invocation.
195
196 // Target Options
197 Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple));
198 Opts.CPU = Args.getLastArgValue(OPT_target_cpu);
199 Opts.Features = Args.getAllArgValues(OPT_target_feature);
200
201 // Use the default target triple if unspecified.
202 if (Opts.Triple.empty())
203 Opts.Triple = llvm::sys::getDefaultTargetTriple();
204
205 // Language Options
206 Opts.IncludePaths = Args.getAllArgValues(OPT_I);
207 Opts.NoInitialTextSection = Args.hasArg(OPT_n);
208 Opts.SaveTemporaryLabels = Args.hasArg(OPT_msave_temp_labels);
209 // Any DebugInfoKind implies GenDwarfForAssembly.
210 Opts.GenDwarfForAssembly = Args.hasArg(OPT_debug_info_kind_EQ);
211
212 if (const Arg *A = Args.getLastArg(OPT_compress_debug_sections,
213 OPT_compress_debug_sections_EQ)) {
214 if (A->getOption().getID() == OPT_compress_debug_sections) {
215 // TODO: be more clever about the compression type auto-detection
216 Opts.CompressDebugSections = llvm::DebugCompressionType::GNU;
217 } else {
218 Opts.CompressDebugSections =
219 llvm::StringSwitch<llvm::DebugCompressionType>(A->getValue())
220 .Case("none", llvm::DebugCompressionType::None)
221 .Case("zlib", llvm::DebugCompressionType::Z)
222 .Case("zlib-gnu", llvm::DebugCompressionType::GNU)
223 .Default(llvm::DebugCompressionType::None);
224 }
225 }
226
227 Opts.RelaxELFRelocations = Args.hasArg(OPT_mrelax_relocations);
228 Opts.DwarfVersion = getLastArgIntValue(Args, OPT_dwarf_version_EQ, 2, Diags);
229 Opts.DwarfDebugFlags = Args.getLastArgValue(OPT_dwarf_debug_flags);
230 Opts.DwarfDebugProducer = Args.getLastArgValue(OPT_dwarf_debug_producer);
231 Opts.DebugCompilationDir = Args.getLastArgValue(OPT_fdebug_compilation_dir);
232 Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name);
233
234 // Frontend Options
235 if (Args.hasArg(OPT_INPUT)) {
236 bool First = true;
237 for (const Arg *A : Args.filtered(OPT_INPUT)) {
238 if (First) {
239 Opts.InputFile = A->getValue();
240 First = false;
241 } else {
242 Diags.Report(diag::err_drv_unknown_argument) << A->getAsString(Args);
243 Success = false;
244 }
245 }
246 }
247 Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm);
248 Opts.OutputPath = Args.getLastArgValue(OPT_o);
249 if (Arg *A = Args.getLastArg(OPT_filetype)) {
250 StringRef Name = A->getValue();
251 unsigned OutputType = StringSwitch<unsigned>(Name)
252 .Case("asm", FT_Asm)
253 .Case("null", FT_Null)
254 .Case("obj", FT_Obj)
255 .Default(~0U);
256 if (OutputType == ~0U) {
257 Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Name;
258 Success = false;
259 } else
260 Opts.OutputType = FileType(OutputType);
261 }
262 Opts.ShowHelp = Args.hasArg(OPT_help);
263 Opts.ShowVersion = Args.hasArg(OPT_version);
264
265 // Transliterate Options
266 Opts.OutputAsmVariant =
267 getLastArgIntValue(Args, OPT_output_asm_variant, 0, Diags);
268 Opts.ShowEncoding = Args.hasArg(OPT_show_encoding);
269 Opts.ShowInst = Args.hasArg(OPT_show_inst);
270
271 // Assemble Options
272 Opts.RelaxAll = Args.hasArg(OPT_mrelax_all);
273 Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack);
274 Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings);
275 Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
276 Opts.IncrementalLinkerCompatible =
277 Args.hasArg(OPT_mincremental_linker_compatible);
278 Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym);
279
280 return Success;
281}
282
283static std::unique_ptr<raw_fd_ostream>
284getOutputStream(AssemblerInvocation &Opts, DiagnosticsEngine &Diags,
285 bool Binary) {
286 if (Opts.OutputPath.empty())
287 Opts.OutputPath = "-";
288
289 // Make sure that the Out file gets unlinked from the disk if we get a
290 // SIGINT.
291 if (Opts.OutputPath != "-")
292 sys::RemoveFileOnSignal(Opts.OutputPath);
293
294 std::error_code EC;
295 auto Out = llvm::make_unique<raw_fd_ostream>(
296 Opts.OutputPath, EC, (Binary ? sys::fs::F_None : sys::fs::F_Text));
297 if (EC) {
298 Diags.Report(diag::err_fe_unable_to_open_output) << Opts.OutputPath
299 << EC.message();
300 return nullptr;
301 }
302
303 return Out;
304}
305
306static bool ExecuteAssembler(AssemblerInvocation &Opts,
307 DiagnosticsEngine &Diags) {
308 // Get the target specific parser.
309 std::string Error;
310 const Target *TheTarget = TargetRegistry::lookupTarget(Opts.Triple, Error);
311 if (!TheTarget)
10
Assuming 'TheTarget' is non-null
11
Taking false branch
312 return Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
313
314 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
315 MemoryBuffer::getFileOrSTDIN(Opts.InputFile);
316
317 if (std::error_code EC = Buffer.getError()) {
12
Taking false branch
318 Error = EC.message();
319 return Diags.Report(diag::err_fe_error_reading) << Opts.InputFile;
320 }
321
322 SourceMgr SrcMgr;
323
324 // Tell SrcMgr about this buffer, which is what the parser will pick up.
325 SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc());
326
327 // Record the location of the include directories so that the lexer can find
328 // it later.
329 SrcMgr.setIncludeDirs(Opts.IncludePaths);
330
331 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(Opts.Triple));
332 assert(MRI && "Unable to create target register info!")(static_cast <bool> (MRI && "Unable to create target register info!"
) ? void (0) : __assert_fail ("MRI && \"Unable to create target register info!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 332, __extension__ __PRETTY_FUNCTION__))
;
333
334 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, Opts.Triple));
335 assert(MAI && "Unable to create target asm info!")(static_cast <bool> (MAI && "Unable to create target asm info!"
) ? void (0) : __assert_fail ("MAI && \"Unable to create target asm info!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 335, __extension__ __PRETTY_FUNCTION__))
;
336
337 // Ensure MCAsmInfo initialization occurs before any use, otherwise sections
338 // may be created with a combination of default and explicit settings.
339 MAI->setCompressDebugSections(Opts.CompressDebugSections);
340
341 MAI->setRelaxELFRelocations(Opts.RelaxELFRelocations);
342
343 bool IsBinary = Opts.OutputType == AssemblerInvocation::FT_Obj;
13
Assuming the condition is false
344 std::unique_ptr<raw_fd_ostream> FDOS = getOutputStream(Opts, Diags, IsBinary);
345 if (!FDOS)
14
Taking false branch
346 return true;
347
348 // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and
349 // MCObjectFileInfo needs a MCContext reference in order to initialize itself.
350 std::unique_ptr<MCObjectFileInfo> MOFI(new MCObjectFileInfo());
351
352 MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &SrcMgr);
353
354 bool PIC = false;
355 if (Opts.RelocationModel == "static") {
15
Taking true branch
356 PIC = false;
357 } else if (Opts.RelocationModel == "pic") {
358 PIC = true;
359 } else {
360 assert(Opts.RelocationModel == "dynamic-no-pic" &&(static_cast <bool> (Opts.RelocationModel == "dynamic-no-pic"
&& "Invalid PIC model!") ? void (0) : __assert_fail (
"Opts.RelocationModel == \"dynamic-no-pic\" && \"Invalid PIC model!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 361, __extension__ __PRETTY_FUNCTION__))
361 "Invalid PIC model!")(static_cast <bool> (Opts.RelocationModel == "dynamic-no-pic"
&& "Invalid PIC model!") ? void (0) : __assert_fail (
"Opts.RelocationModel == \"dynamic-no-pic\" && \"Invalid PIC model!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 361, __extension__ __PRETTY_FUNCTION__))
;
362 PIC = false;
363 }
364
365 MOFI->InitMCObjectFileInfo(Triple(Opts.Triple), PIC, Ctx);
366 if (Opts.SaveTemporaryLabels)
16
Assuming the condition is false
17
Taking false branch
367 Ctx.setAllowTemporaryLabels(false);
368 if (Opts.GenDwarfForAssembly)
18
Assuming the condition is false
19
Taking false branch
369 Ctx.setGenDwarfForAssembly(true);
370 if (!Opts.DwarfDebugFlags.empty())
20
Assuming the condition is false
21
Taking false branch
371 Ctx.setDwarfDebugFlags(StringRef(Opts.DwarfDebugFlags));
372 if (!Opts.DwarfDebugProducer.empty())
22
Assuming the condition is false
23
Taking false branch
373 Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer));
374 if (!Opts.DebugCompilationDir.empty())
24
Assuming the condition is false
25
Taking false branch
375 Ctx.setCompilationDir(Opts.DebugCompilationDir);
376 if (!Opts.MainFileName.empty())
26
Assuming the condition is false
27
Taking false branch
377 Ctx.setMainFileName(StringRef(Opts.MainFileName));
378 Ctx.setDwarfVersion(Opts.DwarfVersion);
379
380 // Build up the feature string from the target feature list.
381 std::string FS;
382 if (!Opts.Features.empty()) {
28
Assuming the condition is false
29
Taking false branch
383 FS = Opts.Features[0];
384 for (unsigned i = 1, e = Opts.Features.size(); i != e; ++i)
385 FS += "," + Opts.Features[i];
386 }
387
388 std::unique_ptr<MCStreamer> Str;
389
390 std::unique_ptr<MCInstrInfo> MCII(TheTarget->createMCInstrInfo());
391 std::unique_ptr<MCSubtargetInfo> STI(
392 TheTarget->createMCSubtargetInfo(Opts.Triple, Opts.CPU, FS));
393
394 raw_pwrite_stream *Out = FDOS.get();
395 std::unique_ptr<buffer_ostream> BOS;
396
397 // FIXME: There is a bit of code duplication with addPassesToEmitFile.
398 if (Opts.OutputType == AssemblerInvocation::FT_Asm) {
30
Assuming the condition is true
31
Taking true branch
399 MCInstPrinter *IP = TheTarget->createMCInstPrinter(
400 llvm::Triple(Opts.Triple), Opts.OutputAsmVariant, *MAI, *MCII, *MRI);
401 MCCodeEmitter *CE = nullptr;
402 MCAsmBackend *MAB = nullptr;
403 if (Opts.ShowEncoding) {
32
Assuming the condition is false
33
Taking false branch
404 CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
405 MCTargetOptions Options;
406 MAB = TheTarget->createMCAsmBackend(*STI, *MRI, Options);
407 }
408 auto FOut = llvm::make_unique<formatted_raw_ostream>(*Out);
409 Str.reset(TheTarget->createAsmStreamer(
34
Calling 'Target::createAsmStreamer'
410 Ctx, std::move(FOut), /*asmverbose*/ true,
411 /*useDwarfDirectory*/ true, IP, CE, MAB, Opts.ShowInst));
412 } else if (Opts.OutputType == AssemblerInvocation::FT_Null) {
413 Str.reset(createNullStreamer(Ctx));
414 } else {
415 assert(Opts.OutputType == AssemblerInvocation::FT_Obj &&(static_cast <bool> (Opts.OutputType == AssemblerInvocation
::FT_Obj && "Invalid file type!") ? void (0) : __assert_fail
("Opts.OutputType == AssemblerInvocation::FT_Obj && \"Invalid file type!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 416, __extension__ __PRETTY_FUNCTION__))
416 "Invalid file type!")(static_cast <bool> (Opts.OutputType == AssemblerInvocation
::FT_Obj && "Invalid file type!") ? void (0) : __assert_fail
("Opts.OutputType == AssemblerInvocation::FT_Obj && \"Invalid file type!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/clang/tools/driver/cc1as_main.cpp"
, 416, __extension__ __PRETTY_FUNCTION__))
;
417 if (!FDOS->supportsSeeking()) {
418 BOS = make_unique<buffer_ostream>(*FDOS);
419 Out = BOS.get();
420 }
421
422 MCCodeEmitter *CE = TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx);
423 MCTargetOptions Options;
424 MCAsmBackend *MAB = TheTarget->createMCAsmBackend(*STI, *MRI, Options);
425 Triple T(Opts.Triple);
426 Str.reset(TheTarget->createMCObjectStreamer(
427 T, Ctx, std::unique_ptr<MCAsmBackend>(MAB), *Out, std::unique_ptr<MCCodeEmitter>(CE), *STI,
428 Opts.RelaxAll, Opts.IncrementalLinkerCompatible,
429 /*DWARFMustBeAtTheEnd*/ true));
430 Str.get()->InitSections(Opts.NoExecStack);
431 }
432
433 bool Failed = false;
434
435 std::unique_ptr<MCAsmParser> Parser(
436 createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI));
437
438 // FIXME: init MCTargetOptions from sanitizer flags here.
439 MCTargetOptions Options;
440 std::unique_ptr<MCTargetAsmParser> TAP(
441 TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options));
442 if (!TAP)
443 Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple;
444
445 // Set values for symbols, if any.
446 for (auto &S : Opts.SymbolDefs) {
447 auto Pair = StringRef(S).split('=');
448 auto Sym = Pair.first;
449 auto Val = Pair.second;
450 int64_t Value;
451 // We have already error checked this in the driver.
452 Val.getAsInteger(0, Value);
453 Ctx.setSymbolValue(Parser->getStreamer(), Sym, Value);
454 }
455
456 if (!Failed) {
457 Parser->setTargetParser(*TAP.get());
458 Failed = Parser->Run(Opts.NoInitialTextSection);
459 }
460
461 // Close Streamer first.
462 // It might have a reference to the output stream.
463 Str.reset();
464 // Close the output stream early.
465 BOS.reset();
466 FDOS.reset();
467
468 // Delete output file if there were errors.
469 if (Failed && Opts.OutputPath != "-")
470 sys::fs::remove(Opts.OutputPath);
471
472 return Failed;
473}
474
475static void LLVMErrorHandler(void *UserData, const std::string &Message,
476 bool GenCrashDiag) {
477 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
478
479 Diags.Report(diag::err_fe_error_backend) << Message;
480
481 // We cannot recover from llvm errors.
482 exit(1);
483}
484
485int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) {
486 // Initialize targets and assembly printers/parsers.
487 InitializeAllTargetInfos();
488 InitializeAllTargetMCs();
489 InitializeAllAsmParsers();
490
491 // Construct our diagnostic client.
492 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
493 TextDiagnosticPrinter *DiagClient
494 = new TextDiagnosticPrinter(errs(), &*DiagOpts);
495 DiagClient->setPrefix("clang -cc1as");
496 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
497 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
498
499 // Set an error handler, so that any LLVM backend diagnostics go through our
500 // error handler.
501 ScopedFatalErrorHandler FatalErrorHandler
502 (LLVMErrorHandler, static_cast<void*>(&Diags));
503
504 // Parse the arguments.
505 AssemblerInvocation Asm;
506 if (!AssemblerInvocation::CreateFromArgs(Asm, Argv, Diags))
1
Taking false branch
507 return 1;
508
509 if (Asm.ShowHelp) {
2
Assuming the condition is false
3
Taking false branch
510 std::unique_ptr<OptTable> Opts(driver::createDriverOptTable());
511 Opts->PrintHelp(llvm::outs(), "clang -cc1as", "Clang Integrated Assembler",
512 /*Include=*/driver::options::CC1AsOption, /*Exclude=*/0,
513 /*ShowAllAliases=*/false);
514 return 0;
515 }
516
517 // Honor -version.
518 //
519 // FIXME: Use a better -version message?
520 if (Asm.ShowVersion) {
4
Assuming the condition is false
5
Taking false branch
521 llvm::cl::PrintVersionMessage();
522 return 0;
523 }
524
525 // Honor -mllvm.
526 //
527 // FIXME: Remove this, one day.
528 if (!Asm.LLVMArgs.empty()) {
6
Assuming the condition is false
7
Taking false branch
529 unsigned NumArgs = Asm.LLVMArgs.size();
530 auto Args = llvm::make_unique<const char*[]>(NumArgs + 2);
531 Args[0] = "clang (LLVM option parsing)";
532 for (unsigned i = 0; i != NumArgs; ++i)
533 Args[i + 1] = Asm.LLVMArgs[i].c_str();
534 Args[NumArgs + 1] = nullptr;
535 llvm::cl::ParseCommandLineOptions(NumArgs + 1, Args.get());
536 }
537
538 // Execute the invocation, unless there were parsing errors.
539 bool Failed = Diags.hasErrorOccurred() || ExecuteAssembler(Asm, Diags);
8
Assuming the condition is false
9
Calling 'ExecuteAssembler'
540
541 // If any timers were active but haven't been destroyed yet, print their
542 // results now.
543 TimerGroup::printAll(errs());
544
545 return !!Failed;
546}

/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/TargetRegistry.h

1//===- Support/TargetRegistry.h - Target Registration -----------*- 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 exposes the TargetRegistry interface, which tools can use to access
11// the appropriate target specific classes (TargetMachine, AsmPrinter, etc.)
12// which have been registered.
13//
14// Target specific class implementations should register themselves using the
15// appropriate TargetRegistry interfaces.
16//
17//===----------------------------------------------------------------------===//
18
19#ifndef LLVM_SUPPORT_TARGETREGISTRY_H
20#define LLVM_SUPPORT_TARGETREGISTRY_H
21
22#include "llvm-c/DisassemblerTypes.h"
23#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/Triple.h"
26#include "llvm/ADT/iterator_range.h"
27#include "llvm/Support/CodeGen.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/FormattedStream.h"
30#include <algorithm>
31#include <cassert>
32#include <cstddef>
33#include <iterator>
34#include <memory>
35#include <string>
36
37namespace llvm {
38
39class AsmPrinter;
40class MCAsmBackend;
41class MCAsmInfo;
42class MCAsmParser;
43class MCCodeEmitter;
44class MCContext;
45class MCDisassembler;
46class MCInstPrinter;
47class MCInstrAnalysis;
48class MCInstrInfo;
49class MCRegisterInfo;
50class MCRelocationInfo;
51class MCStreamer;
52class MCSubtargetInfo;
53class MCSymbolizer;
54class MCTargetAsmParser;
55class MCTargetOptions;
56class MCTargetStreamer;
57class raw_ostream;
58class raw_pwrite_stream;
59class TargetMachine;
60class TargetOptions;
61
62MCStreamer *createNullStreamer(MCContext &Ctx);
63MCStreamer *createAsmStreamer(MCContext &Ctx,
64 std::unique_ptr<formatted_raw_ostream> OS,
65 bool isVerboseAsm, bool useDwarfDirectory,
66 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
67 MCAsmBackend *TAB, bool ShowInst);
68
69/// Takes ownership of \p TAB and \p CE.
70MCStreamer *createELFStreamer(MCContext &Ctx,
71 std::unique_ptr<MCAsmBackend> &&TAB,
72 raw_pwrite_stream &OS,
73 std::unique_ptr<MCCodeEmitter> &&CE,
74 bool RelaxAll);
75MCStreamer *createMachOStreamer(MCContext &Ctx,
76 std::unique_ptr<MCAsmBackend> &&TAB,
77 raw_pwrite_stream &OS,
78 std::unique_ptr<MCCodeEmitter> &&CE,
79 bool RelaxAll, bool DWARFMustBeAtTheEnd,
80 bool LabelSections = false);
81MCStreamer *createWasmStreamer(MCContext &Ctx,
82 std::unique_ptr<MCAsmBackend> &&TAB,
83 raw_pwrite_stream &OS,
84 std::unique_ptr<MCCodeEmitter> &&CE,
85 bool RelaxAll);
86
87MCRelocationInfo *createMCRelocationInfo(const Triple &TT, MCContext &Ctx);
88
89MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
90 LLVMSymbolLookupCallback SymbolLookUp,
91 void *DisInfo, MCContext *Ctx,
92 std::unique_ptr<MCRelocationInfo> &&RelInfo);
93
94/// Target - Wrapper for Target specific information.
95///
96/// For registration purposes, this is a POD type so that targets can be
97/// registered without the use of static constructors.
98///
99/// Targets should implement a single global instance of this class (which
100/// will be zero initialized), and pass that instance to the TargetRegistry as
101/// part of their initialization.
102class Target {
103public:
104 friend struct TargetRegistry;
105
106 using ArchMatchFnTy = bool (*)(Triple::ArchType Arch);
107
108 using MCAsmInfoCtorFnTy = MCAsmInfo *(*)(const MCRegisterInfo &MRI,
109 const Triple &TT);
110 using MCInstrInfoCtorFnTy = MCInstrInfo *(*)();
111 using MCInstrAnalysisCtorFnTy = MCInstrAnalysis *(*)(const MCInstrInfo *Info);
112 using MCRegInfoCtorFnTy = MCRegisterInfo *(*)(const Triple &TT);
113 using MCSubtargetInfoCtorFnTy = MCSubtargetInfo *(*)(const Triple &TT,
114 StringRef CPU,
115 StringRef Features);
116 using TargetMachineCtorTy = TargetMachine
117 *(*)(const Target &T, const Triple &TT, StringRef CPU, StringRef Features,
118 const TargetOptions &Options, Optional<Reloc::Model> RM,
119 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT);
120 // If it weren't for layering issues (this header is in llvm/Support, but
121 // depends on MC?) this should take the Streamer by value rather than rvalue
122 // reference.
123 using AsmPrinterCtorTy = AsmPrinter *(*)(
124 TargetMachine &TM, std::unique_ptr<MCStreamer> &&Streamer);
125 using MCAsmBackendCtorTy = MCAsmBackend *(*)(const Target &T,
126 const MCSubtargetInfo &STI,
127 const MCRegisterInfo &MRI,
128 const MCTargetOptions &Options);
129 using MCAsmParserCtorTy = MCTargetAsmParser *(*)(
130 const MCSubtargetInfo &STI, MCAsmParser &P, const MCInstrInfo &MII,
131 const MCTargetOptions &Options);
132 using MCDisassemblerCtorTy = MCDisassembler *(*)(const Target &T,
133 const MCSubtargetInfo &STI,
134 MCContext &Ctx);
135 using MCInstPrinterCtorTy = MCInstPrinter *(*)(const Triple &T,
136 unsigned SyntaxVariant,
137 const MCAsmInfo &MAI,
138 const MCInstrInfo &MII,
139 const MCRegisterInfo &MRI);
140 using MCCodeEmitterCtorTy = MCCodeEmitter *(*)(const MCInstrInfo &II,
141 const MCRegisterInfo &MRI,
142 MCContext &Ctx);
143 using ELFStreamerCtorTy =
144 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
145 std::unique_ptr<MCAsmBackend> &&TAB,
146 raw_pwrite_stream &OS,
147 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
148 using MachOStreamerCtorTy =
149 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
150 raw_pwrite_stream &OS,
151 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
152 bool DWARFMustBeAtTheEnd);
153 using COFFStreamerCtorTy =
154 MCStreamer *(*)(MCContext &Ctx, std::unique_ptr<MCAsmBackend> &&TAB,
155 raw_pwrite_stream &OS,
156 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
157 bool IncrementalLinkerCompatible);
158 using WasmStreamerCtorTy =
159 MCStreamer *(*)(const Triple &T, MCContext &Ctx,
160 std::unique_ptr<MCAsmBackend> &&TAB,
161 raw_pwrite_stream &OS,
162 std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll);
163 using NullTargetStreamerCtorTy = MCTargetStreamer *(*)(MCStreamer &S);
164 using AsmTargetStreamerCtorTy = MCTargetStreamer *(*)(
165 MCStreamer &S, formatted_raw_ostream &OS, MCInstPrinter *InstPrint,
166 bool IsVerboseAsm);
167 using ObjectTargetStreamerCtorTy = MCTargetStreamer *(*)(
168 MCStreamer &S, const MCSubtargetInfo &STI);
169 using MCRelocationInfoCtorTy = MCRelocationInfo *(*)(const Triple &TT,
170 MCContext &Ctx);
171 using MCSymbolizerCtorTy = MCSymbolizer *(*)(
172 const Triple &TT, LLVMOpInfoCallback GetOpInfo,
173 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
174 std::unique_ptr<MCRelocationInfo> &&RelInfo);
175
176private:
177 /// Next - The next registered target in the linked list, maintained by the
178 /// TargetRegistry.
179 Target *Next;
180
181 /// The target function for checking if an architecture is supported.
182 ArchMatchFnTy ArchMatchFn;
183
184 /// Name - The target name.
185 const char *Name;
186
187 /// ShortDesc - A short description of the target.
188 const char *ShortDesc;
189
190 /// BackendName - The name of the backend implementation. This must match the
191 /// name of the 'def X : Target ...' in TableGen.
192 const char *BackendName;
193
194 /// HasJIT - Whether this target supports the JIT.
195 bool HasJIT;
196
197 /// MCAsmInfoCtorFn - Constructor function for this target's MCAsmInfo, if
198 /// registered.
199 MCAsmInfoCtorFnTy MCAsmInfoCtorFn;
200
201 /// MCInstrInfoCtorFn - Constructor function for this target's MCInstrInfo,
202 /// if registered.
203 MCInstrInfoCtorFnTy MCInstrInfoCtorFn;
204
205 /// MCInstrAnalysisCtorFn - Constructor function for this target's
206 /// MCInstrAnalysis, if registered.
207 MCInstrAnalysisCtorFnTy MCInstrAnalysisCtorFn;
208
209 /// MCRegInfoCtorFn - Constructor function for this target's MCRegisterInfo,
210 /// if registered.
211 MCRegInfoCtorFnTy MCRegInfoCtorFn;
212
213 /// MCSubtargetInfoCtorFn - Constructor function for this target's
214 /// MCSubtargetInfo, if registered.
215 MCSubtargetInfoCtorFnTy MCSubtargetInfoCtorFn;
216
217 /// TargetMachineCtorFn - Construction function for this target's
218 /// TargetMachine, if registered.
219 TargetMachineCtorTy TargetMachineCtorFn;
220
221 /// MCAsmBackendCtorFn - Construction function for this target's
222 /// MCAsmBackend, if registered.
223 MCAsmBackendCtorTy MCAsmBackendCtorFn;
224
225 /// MCAsmParserCtorFn - Construction function for this target's
226 /// MCTargetAsmParser, if registered.
227 MCAsmParserCtorTy MCAsmParserCtorFn;
228
229 /// AsmPrinterCtorFn - Construction function for this target's AsmPrinter,
230 /// if registered.
231 AsmPrinterCtorTy AsmPrinterCtorFn;
232
233 /// MCDisassemblerCtorFn - Construction function for this target's
234 /// MCDisassembler, if registered.
235 MCDisassemblerCtorTy MCDisassemblerCtorFn;
236
237 /// MCInstPrinterCtorFn - Construction function for this target's
238 /// MCInstPrinter, if registered.
239 MCInstPrinterCtorTy MCInstPrinterCtorFn;
240
241 /// MCCodeEmitterCtorFn - Construction function for this target's
242 /// CodeEmitter, if registered.
243 MCCodeEmitterCtorTy MCCodeEmitterCtorFn;
244
245 // Construction functions for the various object formats, if registered.
246 COFFStreamerCtorTy COFFStreamerCtorFn = nullptr;
247 MachOStreamerCtorTy MachOStreamerCtorFn = nullptr;
248 ELFStreamerCtorTy ELFStreamerCtorFn = nullptr;
249 WasmStreamerCtorTy WasmStreamerCtorFn = nullptr;
250
251 /// Construction function for this target's null TargetStreamer, if
252 /// registered (default = nullptr).
253 NullTargetStreamerCtorTy NullTargetStreamerCtorFn = nullptr;
254
255 /// Construction function for this target's asm TargetStreamer, if
256 /// registered (default = nullptr).
257 AsmTargetStreamerCtorTy AsmTargetStreamerCtorFn = nullptr;
258
259 /// Construction function for this target's obj TargetStreamer, if
260 /// registered (default = nullptr).
261 ObjectTargetStreamerCtorTy ObjectTargetStreamerCtorFn = nullptr;
262
263 /// MCRelocationInfoCtorFn - Construction function for this target's
264 /// MCRelocationInfo, if registered (default = llvm::createMCRelocationInfo)
265 MCRelocationInfoCtorTy MCRelocationInfoCtorFn = nullptr;
266
267 /// MCSymbolizerCtorFn - Construction function for this target's
268 /// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
269 MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
270
271public:
272 Target() = default;
273
274 /// @name Target Information
275 /// @{
276
277 // getNext - Return the next registered target.
278 const Target *getNext() const { return Next; }
279
280 /// getName - Get the target name.
281 const char *getName() const { return Name; }
282
283 /// getShortDescription - Get a short description of the target.
284 const char *getShortDescription() const { return ShortDesc; }
285
286 /// getBackendName - Get the backend name.
287 const char *getBackendName() const { return BackendName; }
288
289 /// @}
290 /// @name Feature Predicates
291 /// @{
292
293 /// hasJIT - Check if this targets supports the just-in-time compilation.
294 bool hasJIT() const { return HasJIT; }
295
296 /// hasTargetMachine - Check if this target supports code generation.
297 bool hasTargetMachine() const { return TargetMachineCtorFn != nullptr; }
298
299 /// hasMCAsmBackend - Check if this target supports .o generation.
300 bool hasMCAsmBackend() const { return MCAsmBackendCtorFn != nullptr; }
301
302 /// hasMCAsmParser - Check if this target supports assembly parsing.
303 bool hasMCAsmParser() const { return MCAsmParserCtorFn != nullptr; }
304
305 /// @}
306 /// @name Feature Constructors
307 /// @{
308
309 /// createMCAsmInfo - Create a MCAsmInfo implementation for the specified
310 /// target triple.
311 ///
312 /// \param TheTriple This argument is used to determine the target machine
313 /// feature set; it should always be provided. Generally this should be
314 /// either the target triple from the module, or the target triple of the
315 /// host if that does not exist.
316 MCAsmInfo *createMCAsmInfo(const MCRegisterInfo &MRI,
317 StringRef TheTriple) const {
318 if (!MCAsmInfoCtorFn)
319 return nullptr;
320 return MCAsmInfoCtorFn(MRI, Triple(TheTriple));
321 }
322
323 /// createMCInstrInfo - Create a MCInstrInfo implementation.
324 ///
325 MCInstrInfo *createMCInstrInfo() const {
326 if (!MCInstrInfoCtorFn)
327 return nullptr;
328 return MCInstrInfoCtorFn();
329 }
330
331 /// createMCInstrAnalysis - Create a MCInstrAnalysis implementation.
332 ///
333 MCInstrAnalysis *createMCInstrAnalysis(const MCInstrInfo *Info) const {
334 if (!MCInstrAnalysisCtorFn)
335 return nullptr;
336 return MCInstrAnalysisCtorFn(Info);
337 }
338
339 /// createMCRegInfo - Create a MCRegisterInfo implementation.
340 ///
341 MCRegisterInfo *createMCRegInfo(StringRef TT) const {
342 if (!MCRegInfoCtorFn)
343 return nullptr;
344 return MCRegInfoCtorFn(Triple(TT));
345 }
346
347 /// createMCSubtargetInfo - Create a MCSubtargetInfo implementation.
348 ///
349 /// \param TheTriple This argument is used to determine the target machine
350 /// feature set; it should always be provided. Generally this should be
351 /// either the target triple from the module, or the target triple of the
352 /// host if that does not exist.
353 /// \param CPU This specifies the name of the target CPU.
354 /// \param Features This specifies the string representation of the
355 /// additional target features.
356 MCSubtargetInfo *createMCSubtargetInfo(StringRef TheTriple, StringRef CPU,
357 StringRef Features) const {
358 if (!MCSubtargetInfoCtorFn)
359 return nullptr;
360 return MCSubtargetInfoCtorFn(Triple(TheTriple), CPU, Features);
361 }
362
363 /// createTargetMachine - Create a target specific machine implementation
364 /// for the specified \p Triple.
365 ///
366 /// \param TT This argument is used to determine the target machine
367 /// feature set; it should always be provided. Generally this should be
368 /// either the target triple from the module, or the target triple of the
369 /// host if that does not exist.
370 TargetMachine *createTargetMachine(StringRef TT, StringRef CPU,
371 StringRef Features,
372 const TargetOptions &Options,
373 Optional<Reloc::Model> RM,
374 Optional<CodeModel::Model> CM = None,
375 CodeGenOpt::Level OL = CodeGenOpt::Default,
376 bool JIT = false) const {
377 if (!TargetMachineCtorFn)
378 return nullptr;
379 return TargetMachineCtorFn(*this, Triple(TT), CPU, Features, Options, RM,
380 CM, OL, JIT);
381 }
382
383 /// createMCAsmBackend - Create a target specific assembly parser.
384 MCAsmBackend *createMCAsmBackend(const MCSubtargetInfo &STI,
385 const MCRegisterInfo &MRI,
386 const MCTargetOptions &Options) const {
387 if (!MCAsmBackendCtorFn)
388 return nullptr;
389 return MCAsmBackendCtorFn(*this, STI, MRI, Options);
390 }
391
392 /// createMCAsmParser - Create a target specific assembly parser.
393 ///
394 /// \param Parser The target independent parser implementation to use for
395 /// parsing and lexing.
396 MCTargetAsmParser *createMCAsmParser(const MCSubtargetInfo &STI,
397 MCAsmParser &Parser,
398 const MCInstrInfo &MII,
399 const MCTargetOptions &Options) const {
400 if (!MCAsmParserCtorFn)
401 return nullptr;
402 return MCAsmParserCtorFn(STI, Parser, MII, Options);
403 }
404
405 /// createAsmPrinter - Create a target specific assembly printer pass. This
406 /// takes ownership of the MCStreamer object.
407 AsmPrinter *createAsmPrinter(TargetMachine &TM,
408 std::unique_ptr<MCStreamer> &&Streamer) const {
409 if (!AsmPrinterCtorFn)
410 return nullptr;
411 return AsmPrinterCtorFn(TM, std::move(Streamer));
412 }
413
414 MCDisassembler *createMCDisassembler(const MCSubtargetInfo &STI,
415 MCContext &Ctx) const {
416 if (!MCDisassemblerCtorFn)
417 return nullptr;
418 return MCDisassemblerCtorFn(*this, STI, Ctx);
419 }
420
421 MCInstPrinter *createMCInstPrinter(const Triple &T, unsigned SyntaxVariant,
422 const MCAsmInfo &MAI,
423 const MCInstrInfo &MII,
424 const MCRegisterInfo &MRI) const {
425 if (!MCInstPrinterCtorFn)
426 return nullptr;
427 return MCInstPrinterCtorFn(T, SyntaxVariant, MAI, MII, MRI);
428 }
429
430 /// createMCCodeEmitter - Create a target specific code emitter.
431 MCCodeEmitter *createMCCodeEmitter(const MCInstrInfo &II,
432 const MCRegisterInfo &MRI,
433 MCContext &Ctx) const {
434 if (!MCCodeEmitterCtorFn)
435 return nullptr;
436 return MCCodeEmitterCtorFn(II, MRI, Ctx);
437 }
438
439 /// Create a target specific MCStreamer.
440 ///
441 /// \param T The target triple.
442 /// \param Ctx The target context.
443 /// \param TAB The target assembler backend object. Takes ownership.
444 /// \param OS The stream object.
445 /// \param Emitter The target independent assembler object.Takes ownership.
446 /// \param RelaxAll Relax all fixups?
447 MCStreamer *createMCObjectStreamer(const Triple &T, MCContext &Ctx,
448 std::unique_ptr<MCAsmBackend> &&TAB,
449 raw_pwrite_stream &OS,
450 std::unique_ptr<MCCodeEmitter> &&Emitter,
451 const MCSubtargetInfo &STI, bool RelaxAll,
452 bool IncrementalLinkerCompatible,
453 bool DWARFMustBeAtTheEnd) const {
454 MCStreamer *S;
455 switch (T.getObjectFormat()) {
456 default:
457 llvm_unreachable("Unknown object format")::llvm::llvm_unreachable_internal("Unknown object format", "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/TargetRegistry.h"
, 457)
;
458 case Triple::COFF:
459 assert(T.isOSWindows() && "only Windows COFF is supported")(static_cast <bool> (T.isOSWindows() && "only Windows COFF is supported"
) ? void (0) : __assert_fail ("T.isOSWindows() && \"only Windows COFF is supported\""
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/TargetRegistry.h"
, 459, __extension__ __PRETTY_FUNCTION__))
;
460 S = COFFStreamerCtorFn(Ctx, std::move(TAB), OS, std::move(Emitter),
461 RelaxAll, IncrementalLinkerCompatible);
462 break;
463 case Triple::MachO:
464 if (MachOStreamerCtorFn)
465 S = MachOStreamerCtorFn(Ctx, std::move(TAB), OS, std::move(Emitter),
466 RelaxAll, DWARFMustBeAtTheEnd);
467 else
468 S = createMachOStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
469 RelaxAll, DWARFMustBeAtTheEnd);
470 break;
471 case Triple::ELF:
472 if (ELFStreamerCtorFn)
473 S = ELFStreamerCtorFn(T, Ctx, std::move(TAB), OS, std::move(Emitter),
474 RelaxAll);
475 else
476 S = createELFStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
477 RelaxAll);
478 break;
479 case Triple::Wasm:
480 if (WasmStreamerCtorFn)
481 S = WasmStreamerCtorFn(T, Ctx, std::move(TAB), OS, std::move(Emitter),
482 RelaxAll);
483 else
484 S = createWasmStreamer(Ctx, std::move(TAB), OS, std::move(Emitter),
485 RelaxAll);
486 break;
487 }
488 if (ObjectTargetStreamerCtorFn)
489 ObjectTargetStreamerCtorFn(*S, STI);
490 return S;
491 }
492
493 MCStreamer *createAsmStreamer(MCContext &Ctx,
494 std::unique_ptr<formatted_raw_ostream> OS,
495 bool IsVerboseAsm, bool UseDwarfDirectory,
496 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
497 MCAsmBackend *TAB, bool ShowInst) const {
498 formatted_raw_ostream &OSRef = *OS;
499 MCStreamer *S = llvm::createAsmStreamer(Ctx, std::move(OS), IsVerboseAsm,
35
Calling '~unique_ptr'
40
Returning from '~unique_ptr'
500 UseDwarfDirectory, InstPrint, CE,
501 TAB, ShowInst);
502 createAsmTargetStreamer(*S, OSRef, InstPrint, IsVerboseAsm);
41
Use of memory after it is freed
503 return S;
504 }
505
506 MCTargetStreamer *createAsmTargetStreamer(MCStreamer &S,
507 formatted_raw_ostream &OS,
508 MCInstPrinter *InstPrint,
509 bool IsVerboseAsm) const {
510 if (AsmTargetStreamerCtorFn)
511 return AsmTargetStreamerCtorFn(S, OS, InstPrint, IsVerboseAsm);
512 return nullptr;
513 }
514
515 MCStreamer *createNullStreamer(MCContext &Ctx) const {
516 MCStreamer *S = llvm::createNullStreamer(Ctx);
517 createNullTargetStreamer(*S);
518 return S;
519 }
520
521 MCTargetStreamer *createNullTargetStreamer(MCStreamer &S) const {
522 if (NullTargetStreamerCtorFn)
523 return NullTargetStreamerCtorFn(S);
524 return nullptr;
525 }
526
527 /// createMCRelocationInfo - Create a target specific MCRelocationInfo.
528 ///
529 /// \param TT The target triple.
530 /// \param Ctx The target context.
531 MCRelocationInfo *createMCRelocationInfo(StringRef TT, MCContext &Ctx) const {
532 MCRelocationInfoCtorTy Fn = MCRelocationInfoCtorFn
533 ? MCRelocationInfoCtorFn
534 : llvm::createMCRelocationInfo;
535 return Fn(Triple(TT), Ctx);
536 }
537
538 /// createMCSymbolizer - Create a target specific MCSymbolizer.
539 ///
540 /// \param TT The target triple.
541 /// \param GetOpInfo The function to get the symbolic information for
542 /// operands.
543 /// \param SymbolLookUp The function to lookup a symbol name.
544 /// \param DisInfo The pointer to the block of symbolic information for above
545 /// call
546 /// back.
547 /// \param Ctx The target context.
548 /// \param RelInfo The relocation information for this target. Takes
549 /// ownership.
550 MCSymbolizer *
551 createMCSymbolizer(StringRef TT, LLVMOpInfoCallback GetOpInfo,
552 LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo,
553 MCContext *Ctx,
554 std::unique_ptr<MCRelocationInfo> &&RelInfo) const {
555 MCSymbolizerCtorTy Fn =
556 MCSymbolizerCtorFn ? MCSymbolizerCtorFn : llvm::createMCSymbolizer;
557 return Fn(Triple(TT), GetOpInfo, SymbolLookUp, DisInfo, Ctx,
558 std::move(RelInfo));
559 }
560
561 /// @}
562};
563
564/// TargetRegistry - Generic interface to target specific features.
565struct TargetRegistry {
566 // FIXME: Make this a namespace, probably just move all the Register*
567 // functions into Target (currently they all just set members on the Target
568 // anyway, and Target friends this class so those functions can...
569 // function).
570 TargetRegistry() = delete;
571
572 class iterator
573 : public std::iterator<std::forward_iterator_tag, Target, ptrdiff_t> {
574 friend struct TargetRegistry;
575
576 const Target *Current = nullptr;
577
578 explicit iterator(Target *T) : Current(T) {}
579
580 public:
581 iterator() = default;
582
583 bool operator==(const iterator &x) const { return Current == x.Current; }
584 bool operator!=(const iterator &x) const { return !operator==(x); }
585
586 // Iterator traversal: forward iteration only
587 iterator &operator++() { // Preincrement
588 assert(Current && "Cannot increment end iterator!")(static_cast <bool> (Current && "Cannot increment end iterator!"
) ? void (0) : __assert_fail ("Current && \"Cannot increment end iterator!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/TargetRegistry.h"
, 588, __extension__ __PRETTY_FUNCTION__))
;
589 Current = Current->getNext();
590 return *this;
591 }
592 iterator operator++(int) { // Postincrement
593 iterator tmp = *this;
594 ++*this;
595 return tmp;
596 }
597
598 const Target &operator*() const {
599 assert(Current && "Cannot dereference end iterator!")(static_cast <bool> (Current && "Cannot dereference end iterator!"
) ? void (0) : __assert_fail ("Current && \"Cannot dereference end iterator!\""
, "/build/llvm-toolchain-snapshot-7~svn329677/include/llvm/Support/TargetRegistry.h"
, 599, __extension__ __PRETTY_FUNCTION__))
;
600 return *Current;
601 }
602
603 const Target *operator->() const { return &operator*(); }
604 };
605
606 /// printRegisteredTargetsForVersion - Print the registered targets
607 /// appropriately for inclusion in a tool's version output.
608 static void printRegisteredTargetsForVersion(raw_ostream &OS);
609
610 /// @name Registry Access
611 /// @{
612
613 static iterator_range<iterator> targets();
614
615 /// lookupTarget - Lookup a target based on a target triple.
616 ///
617 /// \param Triple - The triple to use for finding a target.
618 /// \param Error - On failure, an error string describing why no target was
619 /// found.
620 static const Target *lookupTarget(const std::string &Triple,
621 std::string &Error);
622
623 /// lookupTarget - Lookup a target based on an architecture name
624 /// and a target triple. If the architecture name is non-empty,
625 /// then the lookup is done by architecture. Otherwise, the target
626 /// triple is used.
627 ///
628 /// \param ArchName - The architecture to use for finding a target.
629 /// \param TheTriple - The triple to use for finding a target. The
630 /// triple is updated with canonical architecture name if a lookup
631 /// by architecture is done.
632 /// \param Error - On failure, an error string describing why no target was
633 /// found.
634 static const Target *lookupTarget(const std::string &ArchName,
635 Triple &TheTriple, std::string &Error);
636
637 /// @}
638 /// @name Target Registration
639 /// @{
640
641 /// RegisterTarget - Register the given target. Attempts to register a
642 /// target which has already been registered will be ignored.
643 ///
644 /// Clients are responsible for ensuring that registration doesn't occur
645 /// while another thread is attempting to access the registry. Typically
646 /// this is done by initializing all targets at program startup.
647 ///
648 /// @param T - The target being registered.
649 /// @param Name - The target name. This should be a static string.
650 /// @param ShortDesc - A short target description. This should be a static
651 /// string.
652 /// @param BackendName - The name of the backend. This should be a static
653 /// string that is the same for all targets that share a backend
654 /// implementation and must match the name used in the 'def X : Target ...' in
655 /// TableGen.
656 /// @param ArchMatchFn - The arch match checking function for this target.
657 /// @param HasJIT - Whether the target supports JIT code
658 /// generation.
659 static void RegisterTarget(Target &T, const char *Name, const char *ShortDesc,
660 const char *BackendName,
661 Target::ArchMatchFnTy ArchMatchFn,
662 bool HasJIT = false);
663
664 /// RegisterMCAsmInfo - Register a MCAsmInfo implementation for the
665 /// given target.
666 ///
667 /// Clients are responsible for ensuring that registration doesn't occur
668 /// while another thread is attempting to access the registry. Typically
669 /// this is done by initializing all targets at program startup.
670 ///
671 /// @param T - The target being registered.
672 /// @param Fn - A function to construct a MCAsmInfo for the target.
673 static void RegisterMCAsmInfo(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
674 T.MCAsmInfoCtorFn = Fn;
675 }
676
677 /// RegisterMCInstrInfo - Register a MCInstrInfo implementation for the
678 /// given target.
679 ///
680 /// Clients are responsible for ensuring that registration doesn't occur
681 /// while another thread is attempting to access the registry. Typically
682 /// this is done by initializing all targets at program startup.
683 ///
684 /// @param T - The target being registered.
685 /// @param Fn - A function to construct a MCInstrInfo for the target.
686 static void RegisterMCInstrInfo(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
687 T.MCInstrInfoCtorFn = Fn;
688 }
689
690 /// RegisterMCInstrAnalysis - Register a MCInstrAnalysis implementation for
691 /// the given target.
692 static void RegisterMCInstrAnalysis(Target &T,
693 Target::MCInstrAnalysisCtorFnTy Fn) {
694 T.MCInstrAnalysisCtorFn = Fn;
695 }
696
697 /// RegisterMCRegInfo - Register a MCRegisterInfo implementation for the
698 /// given target.
699 ///
700 /// Clients are responsible for ensuring that registration doesn't occur
701 /// while another thread is attempting to access the registry. Typically
702 /// this is done by initializing all targets at program startup.
703 ///
704 /// @param T - The target being registered.
705 /// @param Fn - A function to construct a MCRegisterInfo for the target.
706 static void RegisterMCRegInfo(Target &T, Target::MCRegInfoCtorFnTy Fn) {
707 T.MCRegInfoCtorFn = Fn;
708 }
709
710 /// RegisterMCSubtargetInfo - Register a MCSubtargetInfo implementation for
711 /// the given target.
712 ///
713 /// Clients are responsible for ensuring that registration doesn't occur
714 /// while another thread is attempting to access the registry. Typically
715 /// this is done by initializing all targets at program startup.
716 ///
717 /// @param T - The target being registered.
718 /// @param Fn - A function to construct a MCSubtargetInfo for the target.
719 static void RegisterMCSubtargetInfo(Target &T,
720 Target::MCSubtargetInfoCtorFnTy Fn) {
721 T.MCSubtargetInfoCtorFn = Fn;
722 }
723
724 /// RegisterTargetMachine - Register a TargetMachine implementation for the
725 /// given target.
726 ///
727 /// Clients are responsible for ensuring that registration doesn't occur
728 /// while another thread is attempting to access the registry. Typically
729 /// this is done by initializing all targets at program startup.
730 ///
731 /// @param T - The target being registered.
732 /// @param Fn - A function to construct a TargetMachine for the target.
733 static void RegisterTargetMachine(Target &T, Target::TargetMachineCtorTy Fn) {
734 T.TargetMachineCtorFn = Fn;
735 }
736
737 /// RegisterMCAsmBackend - Register a MCAsmBackend implementation for the
738 /// given target.
739 ///
740 /// Clients are responsible for ensuring that registration doesn't occur
741 /// while another thread is attempting to access the registry. Typically
742 /// this is done by initializing all targets at program startup.
743 ///
744 /// @param T - The target being registered.
745 /// @param Fn - A function to construct an AsmBackend for the target.
746 static void RegisterMCAsmBackend(Target &T, Target::MCAsmBackendCtorTy Fn) {
747 T.MCAsmBackendCtorFn = Fn;
748 }
749
750 /// RegisterMCAsmParser - Register a MCTargetAsmParser implementation for
751 /// the given target.
752 ///
753 /// Clients are responsible for ensuring that registration doesn't occur
754 /// while another thread is attempting to access the registry. Typically
755 /// this is done by initializing all targets at program startup.
756 ///
757 /// @param T - The target being registered.
758 /// @param Fn - A function to construct an MCTargetAsmParser for the target.
759 static void RegisterMCAsmParser(Target &T, Target::MCAsmParserCtorTy Fn) {
760 T.MCAsmParserCtorFn = Fn;
761 }
762
763 /// RegisterAsmPrinter - Register an AsmPrinter implementation for the given
764 /// target.
765 ///
766 /// Clients are responsible for ensuring that registration doesn't occur
767 /// while another thread is attempting to access the registry. Typically
768 /// this is done by initializing all targets at program startup.
769 ///
770 /// @param T - The target being registered.
771 /// @param Fn - A function to construct an AsmPrinter for the target.
772 static void RegisterAsmPrinter(Target &T, Target::AsmPrinterCtorTy Fn) {
773 T.AsmPrinterCtorFn = Fn;
774 }
775
776 /// RegisterMCDisassembler - Register a MCDisassembler implementation for
777 /// the given target.
778 ///
779 /// Clients are responsible for ensuring that registration doesn't occur
780 /// while another thread is attempting to access the registry. Typically
781 /// this is done by initializing all targets at program startup.
782 ///
783 /// @param T - The target being registered.
784 /// @param Fn - A function to construct an MCDisassembler for the target.
785 static void RegisterMCDisassembler(Target &T,
786 Target::MCDisassemblerCtorTy Fn) {
787 T.MCDisassemblerCtorFn = Fn;
788 }
789
790 /// RegisterMCInstPrinter - Register a MCInstPrinter implementation for the
791 /// given target.
792 ///
793 /// Clients are responsible for ensuring that registration doesn't occur
794 /// while another thread is attempting to access the registry. Typically
795 /// this is done by initializing all targets at program startup.
796 ///
797 /// @param T - The target being registered.
798 /// @param Fn - A function to construct an MCInstPrinter for the target.
799 static void RegisterMCInstPrinter(Target &T, Target::MCInstPrinterCtorTy Fn) {
800 T.MCInstPrinterCtorFn = Fn;
801 }
802
803 /// RegisterMCCodeEmitter - Register a MCCodeEmitter implementation for the
804 /// given target.
805 ///
806 /// Clients are responsible for ensuring that registration doesn't occur
807 /// while another thread is attempting to access the registry. Typically
808 /// this is done by initializing all targets at program startup.
809 ///
810 /// @param T - The target being registered.
811 /// @param Fn - A function to construct an MCCodeEmitter for the target.
812 static void RegisterMCCodeEmitter(Target &T, Target::MCCodeEmitterCtorTy Fn) {
813 T.MCCodeEmitterCtorFn = Fn;
814 }
815
816 static void RegisterCOFFStreamer(Target &T, Target::COFFStreamerCtorTy Fn) {
817 T.COFFStreamerCtorFn = Fn;
818 }
819
820 static void RegisterMachOStreamer(Target &T, Target::MachOStreamerCtorTy Fn) {
821 T.MachOStreamerCtorFn = Fn;
822 }
823
824 static void RegisterELFStreamer(Target &T, Target::ELFStreamerCtorTy Fn) {
825 T.ELFStreamerCtorFn = Fn;
826 }
827
828 static void RegisterWasmStreamer(Target &T, Target::WasmStreamerCtorTy Fn) {
829 T.WasmStreamerCtorFn = Fn;
830 }
831
832 static void RegisterNullTargetStreamer(Target &T,
833 Target::NullTargetStreamerCtorTy Fn) {
834 T.NullTargetStreamerCtorFn = Fn;
835 }
836
837 static void RegisterAsmTargetStreamer(Target &T,
838 Target::AsmTargetStreamerCtorTy Fn) {
839 T.AsmTargetStreamerCtorFn = Fn;
840 }
841
842 static void
843 RegisterObjectTargetStreamer(Target &T,
844 Target::ObjectTargetStreamerCtorTy Fn) {
845 T.ObjectTargetStreamerCtorFn = Fn;
846 }
847
848 /// RegisterMCRelocationInfo - Register an MCRelocationInfo
849 /// implementation for the given target.
850 ///
851 /// Clients are responsible for ensuring that registration doesn't occur
852 /// while another thread is attempting to access the registry. Typically
853 /// this is done by initializing all targets at program startup.
854 ///
855 /// @param T - The target being registered.
856 /// @param Fn - A function to construct an MCRelocationInfo for the target.
857 static void RegisterMCRelocationInfo(Target &T,
858 Target::MCRelocationInfoCtorTy Fn) {
859 T.MCRelocationInfoCtorFn = Fn;
860 }
861
862 /// RegisterMCSymbolizer - Register an MCSymbolizer
863 /// implementation for the given target.
864 ///
865 /// Clients are responsible for ensuring that registration doesn't occur
866 /// while another thread is attempting to access the registry. Typically
867 /// this is done by initializing all targets at program startup.
868 ///
869 /// @param T - The target being registered.
870 /// @param Fn - A function to construct an MCSymbolizer for the target.
871 static void RegisterMCSymbolizer(Target &T, Target::MCSymbolizerCtorTy Fn) {
872 T.MCSymbolizerCtorFn = Fn;
873 }
874
875 /// @}
876};
877
878//===--------------------------------------------------------------------===//
879
880/// RegisterTarget - Helper template for registering a target, for use in the
881/// target's initialization function. Usage:
882///
883///
884/// Target &getTheFooTarget() { // The global target instance.
885/// static Target TheFooTarget;
886/// return TheFooTarget;
887/// }
888/// extern "C" void LLVMInitializeFooTargetInfo() {
889/// RegisterTarget<Triple::foo> X(getTheFooTarget(), "foo", "Foo
890/// description", "Foo" /* Backend Name */);
891/// }
892template <Triple::ArchType TargetArchType = Triple::UnknownArch,
893 bool HasJIT = false>
894struct RegisterTarget {
895 RegisterTarget(Target &T, const char *Name, const char *Desc,
896 const char *BackendName) {
897 TargetRegistry::RegisterTarget(T, Name, Desc, BackendName, &getArchMatch,
898 HasJIT);
899 }
900
901 static bool getArchMatch(Triple::ArchType Arch) {
902 return Arch == TargetArchType;
903 }
904};
905
906/// RegisterMCAsmInfo - Helper template for registering a target assembly info
907/// implementation. This invokes the static "Create" method on the class to
908/// actually do the construction. Usage:
909///
910/// extern "C" void LLVMInitializeFooTarget() {
911/// extern Target TheFooTarget;
912/// RegisterMCAsmInfo<FooMCAsmInfo> X(TheFooTarget);
913/// }
914template <class MCAsmInfoImpl> struct RegisterMCAsmInfo {
915 RegisterMCAsmInfo(Target &T) {
916 TargetRegistry::RegisterMCAsmInfo(T, &Allocator);
917 }
918
919private:
920 static MCAsmInfo *Allocator(const MCRegisterInfo & /*MRI*/,
921 const Triple &TT) {
922 return new MCAsmInfoImpl(TT);
923 }
924};
925
926/// RegisterMCAsmInfoFn - Helper template for registering a target assembly info
927/// implementation. This invokes the specified function to do the
928/// construction. Usage:
929///
930/// extern "C" void LLVMInitializeFooTarget() {
931/// extern Target TheFooTarget;
932/// RegisterMCAsmInfoFn X(TheFooTarget, TheFunction);
933/// }
934struct RegisterMCAsmInfoFn {
935 RegisterMCAsmInfoFn(Target &T, Target::MCAsmInfoCtorFnTy Fn) {
936 TargetRegistry::RegisterMCAsmInfo(T, Fn);
937 }
938};
939
940/// RegisterMCInstrInfo - Helper template for registering a target instruction
941/// info implementation. This invokes the static "Create" method on the class
942/// to actually do the construction. Usage:
943///
944/// extern "C" void LLVMInitializeFooTarget() {
945/// extern Target TheFooTarget;
946/// RegisterMCInstrInfo<FooMCInstrInfo> X(TheFooTarget);
947/// }
948template <class MCInstrInfoImpl> struct RegisterMCInstrInfo {
949 RegisterMCInstrInfo(Target &T) {
950 TargetRegistry::RegisterMCInstrInfo(T, &Allocator);
951 }
952
953private:
954 static MCInstrInfo *Allocator() { return new MCInstrInfoImpl(); }
955};
956
957/// RegisterMCInstrInfoFn - Helper template for registering a target
958/// instruction info implementation. This invokes the specified function to
959/// do the construction. Usage:
960///
961/// extern "C" void LLVMInitializeFooTarget() {
962/// extern Target TheFooTarget;
963/// RegisterMCInstrInfoFn X(TheFooTarget, TheFunction);
964/// }
965struct RegisterMCInstrInfoFn {
966 RegisterMCInstrInfoFn(Target &T, Target::MCInstrInfoCtorFnTy Fn) {
967 TargetRegistry::RegisterMCInstrInfo(T, Fn);
968 }
969};
970
971/// RegisterMCInstrAnalysis - Helper template for registering a target
972/// instruction analyzer implementation. This invokes the static "Create"
973/// method on the class to actually do the construction. Usage:
974///
975/// extern "C" void LLVMInitializeFooTarget() {
976/// extern Target TheFooTarget;
977/// RegisterMCInstrAnalysis<FooMCInstrAnalysis> X(TheFooTarget);
978/// }
979template <class MCInstrAnalysisImpl> struct RegisterMCInstrAnalysis {
980 RegisterMCInstrAnalysis(Target &T) {
981 TargetRegistry::RegisterMCInstrAnalysis(T, &Allocator);
982 }
983
984private:
985 static MCInstrAnalysis *Allocator(const MCInstrInfo *Info) {
986 return new MCInstrAnalysisImpl(Info);
987 }
988};
989
990/// RegisterMCInstrAnalysisFn - Helper template for registering a target
991/// instruction analyzer implementation. This invokes the specified function
992/// to do the construction. Usage:
993///
994/// extern "C" void LLVMInitializeFooTarget() {
995/// extern Target TheFooTarget;
996/// RegisterMCInstrAnalysisFn X(TheFooTarget, TheFunction);
997/// }
998struct RegisterMCInstrAnalysisFn {
999 RegisterMCInstrAnalysisFn(Target &T, Target::MCInstrAnalysisCtorFnTy Fn) {
1000 TargetRegistry::RegisterMCInstrAnalysis(T, Fn);
1001 }
1002};
1003
1004/// RegisterMCRegInfo - Helper template for registering a target register info
1005/// implementation. This invokes the static "Create" method on the class to
1006/// actually do the construction. Usage:
1007///
1008/// extern "C" void LLVMInitializeFooTarget() {
1009/// extern Target TheFooTarget;
1010/// RegisterMCRegInfo<FooMCRegInfo> X(TheFooTarget);
1011/// }
1012template <class MCRegisterInfoImpl> struct RegisterMCRegInfo {
1013 RegisterMCRegInfo(Target &T) {
1014 TargetRegistry::RegisterMCRegInfo(T, &Allocator);
1015 }
1016
1017private:
1018 static MCRegisterInfo *Allocator(const Triple & /*TT*/) {
1019 return new MCRegisterInfoImpl();
1020 }
1021};
1022
1023/// RegisterMCRegInfoFn - Helper template for registering a target register
1024/// info implementation. This invokes the specified function to do the
1025/// construction. Usage:
1026///
1027/// extern "C" void LLVMInitializeFooTarget() {
1028/// extern Target TheFooTarget;
1029/// RegisterMCRegInfoFn X(TheFooTarget, TheFunction);
1030/// }
1031struct RegisterMCRegInfoFn {
1032 RegisterMCRegInfoFn(Target &T, Target::MCRegInfoCtorFnTy Fn) {
1033 TargetRegistry::RegisterMCRegInfo(T, Fn);
1034 }
1035};
1036
1037/// RegisterMCSubtargetInfo - Helper template for registering a target
1038/// subtarget info implementation. This invokes the static "Create" method
1039/// on the class to actually do the construction. Usage:
1040///
1041/// extern "C" void LLVMInitializeFooTarget() {
1042/// extern Target TheFooTarget;
1043/// RegisterMCSubtargetInfo<FooMCSubtargetInfo> X(TheFooTarget);
1044/// }
1045template <class MCSubtargetInfoImpl> struct RegisterMCSubtargetInfo {
1046 RegisterMCSubtargetInfo(Target &T) {
1047 TargetRegistry::RegisterMCSubtargetInfo(T, &Allocator);
1048 }
1049
1050private:
1051 static MCSubtargetInfo *Allocator(const Triple & /*TT*/, StringRef /*CPU*/,
1052 StringRef /*FS*/) {
1053 return new MCSubtargetInfoImpl();
1054 }
1055};
1056
1057/// RegisterMCSubtargetInfoFn - Helper template for registering a target
1058/// subtarget info implementation. This invokes the specified function to
1059/// do the construction. Usage:
1060///
1061/// extern "C" void LLVMInitializeFooTarget() {
1062/// extern Target TheFooTarget;
1063/// RegisterMCSubtargetInfoFn X(TheFooTarget, TheFunction);
1064/// }
1065struct RegisterMCSubtargetInfoFn {
1066 RegisterMCSubtargetInfoFn(Target &T, Target::MCSubtargetInfoCtorFnTy Fn) {
1067 TargetRegistry::RegisterMCSubtargetInfo(T, Fn);
1068 }
1069};
1070
1071/// RegisterTargetMachine - Helper template for registering a target machine
1072/// implementation, for use in the target machine initialization
1073/// function. Usage:
1074///
1075/// extern "C" void LLVMInitializeFooTarget() {
1076/// extern Target TheFooTarget;
1077/// RegisterTargetMachine<FooTargetMachine> X(TheFooTarget);
1078/// }
1079template <class TargetMachineImpl> struct RegisterTargetMachine {
1080 RegisterTargetMachine(Target &T) {
1081 TargetRegistry::RegisterTargetMachine(T, &Allocator);
1082 }
1083
1084private:
1085 static TargetMachine *
1086 Allocator(const Target &T, const Triple &TT, StringRef CPU, StringRef FS,
1087 const TargetOptions &Options, Optional<Reloc::Model> RM,
1088 Optional<CodeModel::Model> CM, CodeGenOpt::Level OL, bool JIT) {
1089 return new TargetMachineImpl(T, TT, CPU, FS, Options, RM, CM, OL, JIT);
1090 }
1091};
1092
1093/// RegisterMCAsmBackend - Helper template for registering a target specific
1094/// assembler backend. Usage:
1095///
1096/// extern "C" void LLVMInitializeFooMCAsmBackend() {
1097/// extern Target TheFooTarget;
1098/// RegisterMCAsmBackend<FooAsmLexer> X(TheFooTarget);
1099/// }
1100template <class MCAsmBackendImpl> struct RegisterMCAsmBackend {
1101 RegisterMCAsmBackend(Target &T) {
1102 TargetRegistry::RegisterMCAsmBackend(T, &Allocator);
1103 }
1104
1105private:
1106 static MCAsmBackend *Allocator(const Target &T, const MCSubtargetInfo &STI,
1107 const MCRegisterInfo &MRI,
1108 const MCTargetOptions &Options) {
1109 return new MCAsmBackendImpl(T, STI, MRI);
1110 }
1111};
1112
1113/// RegisterMCAsmParser - Helper template for registering a target specific
1114/// assembly parser, for use in the target machine initialization
1115/// function. Usage:
1116///
1117/// extern "C" void LLVMInitializeFooMCAsmParser() {
1118/// extern Target TheFooTarget;
1119/// RegisterMCAsmParser<FooAsmParser> X(TheFooTarget);
1120/// }
1121template <class MCAsmParserImpl> struct RegisterMCAsmParser {
1122 RegisterMCAsmParser(Target &T) {
1123 TargetRegistry::RegisterMCAsmParser(T, &Allocator);
1124 }
1125
1126private:
1127 static MCTargetAsmParser *Allocator(const MCSubtargetInfo &STI,
1128 MCAsmParser &P, const MCInstrInfo &MII,
1129 const MCTargetOptions &Options) {
1130 return new MCAsmParserImpl(STI, P, MII, Options);
1131 }
1132};
1133
1134/// RegisterAsmPrinter - Helper template for registering a target specific
1135/// assembly printer, for use in the target machine initialization
1136/// function. Usage:
1137///
1138/// extern "C" void LLVMInitializeFooAsmPrinter() {
1139/// extern Target TheFooTarget;
1140/// RegisterAsmPrinter<FooAsmPrinter> X(TheFooTarget);
1141/// }
1142template <class AsmPrinterImpl> struct RegisterAsmPrinter {
1143 RegisterAsmPrinter(Target &T) {
1144 TargetRegistry::RegisterAsmPrinter(T, &Allocator);
1145 }
1146
1147private:
1148 static AsmPrinter *Allocator(TargetMachine &TM,
1149 std::unique_ptr<MCStreamer> &&Streamer) {
1150 return new AsmPrinterImpl(TM, std::move(Streamer));
1151 }
1152};
1153
1154/// RegisterMCCodeEmitter - Helper template for registering a target specific
1155/// machine code emitter, for use in the target initialization
1156/// function. Usage:
1157///
1158/// extern "C" void LLVMInitializeFooMCCodeEmitter() {
1159/// extern Target TheFooTarget;
1160/// RegisterMCCodeEmitter<FooCodeEmitter> X(TheFooTarget);
1161/// }
1162template <class MCCodeEmitterImpl> struct RegisterMCCodeEmitter {
1163 RegisterMCCodeEmitter(Target &T) {
1164 TargetRegistry::RegisterMCCodeEmitter(T, &Allocator);
1165 }
1166
1167private:
1168 static MCCodeEmitter *Allocator(const MCInstrInfo & /*II*/,
1169 const MCRegisterInfo & /*MRI*/,
1170 MCContext & /*Ctx*/) {
1171 return new MCCodeEmitterImpl();
1172 }
1173};
1174
1175} // end namespace llvm
1176
1177#endif // LLVM_SUPPORT_TARGETREGISTRY_H

/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;
38
Memory is released
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)) { }
145
146 pointer& _M_ptr() { return std::get<0>(_M_t); }
147 pointer _M_ptr() const { return std::get<0>(_M_t); }
148 _Dp& _M_deleter() { return std::get<1>(_M_t); }
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())) { }
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();
267 if (__ptr != nullptr)
36
Taking true branch
268 get_deleter()(__ptr);
37
Calling 'default_delete::operator()'
39
Returning; memory was released via 2nd parameter
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();
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(); }
338
339 /// Return a reference to the stored deleter.
340 deleter_type&
341 get_deleter() noexcept
342 { return _M_t._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();
360 _M_t._M_ptr() = pointer();
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 */