Bug Summary

File:src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGCoroutine.cpp
Warning:line 95, column 21
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGCoroutine.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGCoroutine.cpp

/usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGCoroutine.cpp

1//===----- CGCoroutine.cpp - Emit LLVM Code for C++ coroutines ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code dealing with C++ code generation of coroutines.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGCleanup.h"
14#include "CodeGenFunction.h"
15#include "llvm/ADT/ScopeExit.h"
16#include "clang/AST/StmtCXX.h"
17#include "clang/AST/StmtVisitor.h"
18
19using namespace clang;
20using namespace CodeGen;
21
22using llvm::Value;
23using llvm::BasicBlock;
24
25namespace {
26enum class AwaitKind { Init, Normal, Yield, Final };
27static constexpr llvm::StringLiteral AwaitKindStr[] = {"init", "await", "yield",
28 "final"};
29}
30
31struct clang::CodeGen::CGCoroData {
32 // What is the current await expression kind and how many
33 // await/yield expressions were encountered so far.
34 // These are used to generate pretty labels for await expressions in LLVM IR.
35 AwaitKind CurrentAwaitKind = AwaitKind::Init;
36 unsigned AwaitNum = 0;
37 unsigned YieldNum = 0;
38
39 // How many co_return statements are in the coroutine. Used to decide whether
40 // we need to add co_return; equivalent at the end of the user authored body.
41 unsigned CoreturnCount = 0;
42
43 // A branch to this block is emitted when coroutine needs to suspend.
44 llvm::BasicBlock *SuspendBB = nullptr;
45
46 // The promise type's 'unhandled_exception' handler, if it defines one.
47 Stmt *ExceptionHandler = nullptr;
48
49 // A temporary i1 alloca that stores whether 'await_resume' threw an
50 // exception. If it did, 'true' is stored in this variable, and the coroutine
51 // body must be skipped. If the promise type does not define an exception
52 // handler, this is null.
53 llvm::Value *ResumeEHVar = nullptr;
54
55 // Stores the jump destination just before the coroutine memory is freed.
56 // This is the destination that every suspend point jumps to for the cleanup
57 // branch.
58 CodeGenFunction::JumpDest CleanupJD;
59
60 // Stores the jump destination just before the final suspend. The co_return
61 // statements jumps to this point after calling return_xxx promise member.
62 CodeGenFunction::JumpDest FinalJD;
63
64 // Stores the llvm.coro.id emitted in the function so that we can supply it
65 // as the first argument to coro.begin, coro.alloc and coro.free intrinsics.
66 // Note: llvm.coro.id returns a token that cannot be directly expressed in a
67 // builtin.
68 llvm::CallInst *CoroId = nullptr;
69
70 // Stores the llvm.coro.begin emitted in the function so that we can replace
71 // all coro.frame intrinsics with direct SSA value of coro.begin that returns
72 // the address of the coroutine frame of the current coroutine.
73 llvm::CallInst *CoroBegin = nullptr;
74
75 // Stores the last emitted coro.free for the deallocate expressions, we use it
76 // to wrap dealloc code with if(auto mem = coro.free) dealloc(mem).
77 llvm::CallInst *LastCoroFree = nullptr;
78
79 // If coro.id came from the builtin, remember the expression to give better
80 // diagnostic. If CoroIdExpr is nullptr, the coro.id was created by
81 // EmitCoroutineBody.
82 CallExpr const *CoroIdExpr = nullptr;
83};
84
85// Defining these here allows to keep CGCoroData private to this file.
86clang::CodeGen::CodeGenFunction::CGCoroInfo::CGCoroInfo() {}
87CodeGenFunction::CGCoroInfo::~CGCoroInfo() {}
88
89static void createCoroData(CodeGenFunction &CGF,
90 CodeGenFunction::CGCoroInfo &CurCoro,
91 llvm::CallInst *CoroId,
92 CallExpr const *CoroIdExpr = nullptr) {
93 if (CurCoro.Data) {
3
Calling 'unique_ptr::operator bool'
6
Returning from 'unique_ptr::operator bool'
7
Taking true branch
94 if (CurCoro.Data->CoroIdExpr)
8
Assuming field 'CoroIdExpr' is non-null
9
Taking true branch
95 CGF.CGM.Error(CoroIdExpr->getBeginLoc(),
10
Called C++ object pointer is null
96 "only one __builtin_coro_id can be used in a function");
97 else if (CoroIdExpr)
98 CGF.CGM.Error(CoroIdExpr->getBeginLoc(),
99 "__builtin_coro_id shall not be used in a C++ coroutine");
100 else
101 llvm_unreachable("EmitCoroutineBodyStatement called twice?")__builtin_unreachable();
102
103 return;
104 }
105
106 CurCoro.Data = std::unique_ptr<CGCoroData>(new CGCoroData);
107 CurCoro.Data->CoroId = CoroId;
108 CurCoro.Data->CoroIdExpr = CoroIdExpr;
109}
110
111// Synthesize a pretty name for a suspend point.
112static SmallString<32> buildSuspendPrefixStr(CGCoroData &Coro, AwaitKind Kind) {
113 unsigned No = 0;
114 switch (Kind) {
115 case AwaitKind::Init:
116 case AwaitKind::Final:
117 break;
118 case AwaitKind::Normal:
119 No = ++Coro.AwaitNum;
120 break;
121 case AwaitKind::Yield:
122 No = ++Coro.YieldNum;
123 break;
124 }
125 SmallString<32> Prefix(AwaitKindStr[static_cast<unsigned>(Kind)]);
126 if (No > 1) {
127 Twine(No).toVector(Prefix);
128 }
129 return Prefix;
130}
131
132static bool memberCallExpressionCanThrow(const Expr *E) {
133 if (const auto *CE = dyn_cast<CXXMemberCallExpr>(E))
134 if (const auto *Proto =
135 CE->getMethodDecl()->getType()->getAs<FunctionProtoType>())
136 if (isNoexceptExceptionSpec(Proto->getExceptionSpecType()) &&
137 Proto->canThrow() == CT_Cannot)
138 return false;
139 return true;
140}
141
142// Emit suspend expression which roughly looks like:
143//
144// auto && x = CommonExpr();
145// if (!x.await_ready()) {
146// llvm_coro_save();
147// x.await_suspend(...); (*)
148// llvm_coro_suspend(); (**)
149// }
150// x.await_resume();
151//
152// where the result of the entire expression is the result of x.await_resume()
153//
154// (*) If x.await_suspend return type is bool, it allows to veto a suspend:
155// if (x.await_suspend(...))
156// llvm_coro_suspend();
157//
158// (**) llvm_coro_suspend() encodes three possible continuations as
159// a switch instruction:
160//
161// %where-to = call i8 @llvm.coro.suspend(...)
162// switch i8 %where-to, label %coro.ret [ ; jump to epilogue to suspend
163// i8 0, label %yield.ready ; go here when resumed
164// i8 1, label %yield.cleanup ; go here when destroyed
165// ]
166//
167// See llvm's docs/Coroutines.rst for more details.
168//
169namespace {
170 struct LValueOrRValue {
171 LValue LV;
172 RValue RV;
173 };
174}
175static LValueOrRValue emitSuspendExpression(CodeGenFunction &CGF, CGCoroData &Coro,
176 CoroutineSuspendExpr const &S,
177 AwaitKind Kind, AggValueSlot aggSlot,
178 bool ignoreResult, bool forLValue) {
179 auto *E = S.getCommonExpr();
180
181 auto Binder =
182 CodeGenFunction::OpaqueValueMappingData::bind(CGF, S.getOpaqueValue(), E);
183 auto UnbindOnExit = llvm::make_scope_exit([&] { Binder.unbind(CGF); });
184
185 auto Prefix = buildSuspendPrefixStr(Coro, Kind);
186 BasicBlock *ReadyBlock = CGF.createBasicBlock(Prefix + Twine(".ready"));
187 BasicBlock *SuspendBlock = CGF.createBasicBlock(Prefix + Twine(".suspend"));
188 BasicBlock *CleanupBlock = CGF.createBasicBlock(Prefix + Twine(".cleanup"));
189
190 // If expression is ready, no need to suspend.
191 CGF.EmitBranchOnBoolExpr(S.getReadyExpr(), ReadyBlock, SuspendBlock, 0);
192
193 // Otherwise, emit suspend logic.
194 CGF.EmitBlock(SuspendBlock);
195
196 auto &Builder = CGF.Builder;
197 llvm::Function *CoroSave = CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_save);
198 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.CGM.Int8PtrTy);
199 auto *SaveCall = Builder.CreateCall(CoroSave, {NullPtr});
200
201 auto *SuspendRet = CGF.EmitScalarExpr(S.getSuspendExpr());
202 if (SuspendRet != nullptr && SuspendRet->getType()->isIntegerTy(1)) {
203 // Veto suspension if requested by bool returning await_suspend.
204 BasicBlock *RealSuspendBlock =
205 CGF.createBasicBlock(Prefix + Twine(".suspend.bool"));
206 CGF.Builder.CreateCondBr(SuspendRet, RealSuspendBlock, ReadyBlock);
207 CGF.EmitBlock(RealSuspendBlock);
208 }
209
210 // Emit the suspend point.
211 const bool IsFinalSuspend = (Kind == AwaitKind::Final);
212 llvm::Function *CoroSuspend =
213 CGF.CGM.getIntrinsic(llvm::Intrinsic::coro_suspend);
214 auto *SuspendResult = Builder.CreateCall(
215 CoroSuspend, {SaveCall, Builder.getInt1(IsFinalSuspend)});
216
217 // Create a switch capturing three possible continuations.
218 auto *Switch = Builder.CreateSwitch(SuspendResult, Coro.SuspendBB, 2);
219 Switch->addCase(Builder.getInt8(0), ReadyBlock);
220 Switch->addCase(Builder.getInt8(1), CleanupBlock);
221
222 // Emit cleanup for this suspend point.
223 CGF.EmitBlock(CleanupBlock);
224 CGF.EmitBranchThroughCleanup(Coro.CleanupJD);
225
226 // Emit await_resume expression.
227 CGF.EmitBlock(ReadyBlock);
228
229 // Exception handling requires additional IR. If the 'await_resume' function
230 // is marked as 'noexcept', we avoid generating this additional IR.
231 CXXTryStmt *TryStmt = nullptr;
232 if (Coro.ExceptionHandler && Kind == AwaitKind::Init &&
233 memberCallExpressionCanThrow(S.getResumeExpr())) {
234 Coro.ResumeEHVar =
235 CGF.CreateTempAlloca(Builder.getInt1Ty(), Prefix + Twine("resume.eh"));
236 Builder.CreateFlagStore(true, Coro.ResumeEHVar);
237
238 auto Loc = S.getResumeExpr()->getExprLoc();
239 auto *Catch = new (CGF.getContext())
240 CXXCatchStmt(Loc, /*exDecl=*/nullptr, Coro.ExceptionHandler);
241 auto *TryBody =
242 CompoundStmt::Create(CGF.getContext(), S.getResumeExpr(), Loc, Loc);
243 TryStmt = CXXTryStmt::Create(CGF.getContext(), Loc, TryBody, Catch);
244 CGF.EnterCXXTryStmt(*TryStmt);
245 }
246
247 LValueOrRValue Res;
248 if (forLValue)
249 Res.LV = CGF.EmitLValue(S.getResumeExpr());
250 else
251 Res.RV = CGF.EmitAnyExpr(S.getResumeExpr(), aggSlot, ignoreResult);
252
253 if (TryStmt) {
254 Builder.CreateFlagStore(false, Coro.ResumeEHVar);
255 CGF.ExitCXXTryStmt(*TryStmt);
256 }
257
258 return Res;
259}
260
261RValue CodeGenFunction::EmitCoawaitExpr(const CoawaitExpr &E,
262 AggValueSlot aggSlot,
263 bool ignoreResult) {
264 return emitSuspendExpression(*this, *CurCoro.Data, E,
265 CurCoro.Data->CurrentAwaitKind, aggSlot,
266 ignoreResult, /*forLValue*/false).RV;
267}
268RValue CodeGenFunction::EmitCoyieldExpr(const CoyieldExpr &E,
269 AggValueSlot aggSlot,
270 bool ignoreResult) {
271 return emitSuspendExpression(*this, *CurCoro.Data, E, AwaitKind::Yield,
272 aggSlot, ignoreResult, /*forLValue*/false).RV;
273}
274
275void CodeGenFunction::EmitCoreturnStmt(CoreturnStmt const &S) {
276 ++CurCoro.Data->CoreturnCount;
277 const Expr *RV = S.getOperand();
278 if (RV && RV->getType()->isVoidType() && !isa<InitListExpr>(RV)) {
279 // Make sure to evaluate the non initlist expression of a co_return
280 // with a void expression for side effects.
281 RunCleanupsScope cleanupScope(*this);
282 EmitIgnoredExpr(RV);
283 }
284 EmitStmt(S.getPromiseCall());
285 EmitBranchThroughCleanup(CurCoro.Data->FinalJD);
286}
287
288
289#ifndef NDEBUG1
290static QualType getCoroutineSuspendExprReturnType(const ASTContext &Ctx,
291 const CoroutineSuspendExpr *E) {
292 const auto *RE = E->getResumeExpr();
293 // Is it possible for RE to be a CXXBindTemporaryExpr wrapping
294 // a MemberCallExpr?
295 assert(isa<CallExpr>(RE) && "unexpected suspend expression type")((void)0);
296 return cast<CallExpr>(RE)->getCallReturnType(Ctx);
297}
298#endif
299
300LValue
301CodeGenFunction::EmitCoawaitLValue(const CoawaitExpr *E) {
302 assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&((void)0)
303 "Can't have a scalar return unless the return type is a "((void)0)
304 "reference type!")((void)0);
305 return emitSuspendExpression(*this, *CurCoro.Data, *E,
306 CurCoro.Data->CurrentAwaitKind, AggValueSlot::ignored(),
307 /*ignoreResult*/false, /*forLValue*/true).LV;
308}
309
310LValue
311CodeGenFunction::EmitCoyieldLValue(const CoyieldExpr *E) {
312 assert(getCoroutineSuspendExprReturnType(getContext(), E)->isReferenceType() &&((void)0)
313 "Can't have a scalar return unless the return type is a "((void)0)
314 "reference type!")((void)0);
315 return emitSuspendExpression(*this, *CurCoro.Data, *E,
316 AwaitKind::Yield, AggValueSlot::ignored(),
317 /*ignoreResult*/false, /*forLValue*/true).LV;
318}
319
320// Hunts for the parameter reference in the parameter copy/move declaration.
321namespace {
322struct GetParamRef : public StmtVisitor<GetParamRef> {
323public:
324 DeclRefExpr *Expr = nullptr;
325 GetParamRef() {}
326 void VisitDeclRefExpr(DeclRefExpr *E) {
327 assert(Expr == nullptr && "multilple declref in param move")((void)0);
328 Expr = E;
329 }
330 void VisitStmt(Stmt *S) {
331 for (auto *C : S->children()) {
332 if (C)
333 Visit(C);
334 }
335 }
336};
337}
338
339// This class replaces references to parameters to their copies by changing
340// the addresses in CGF.LocalDeclMap and restoring back the original values in
341// its destructor.
342
343namespace {
344 struct ParamReferenceReplacerRAII {
345 CodeGenFunction::DeclMapTy SavedLocals;
346 CodeGenFunction::DeclMapTy& LocalDeclMap;
347
348 ParamReferenceReplacerRAII(CodeGenFunction::DeclMapTy &LocalDeclMap)
349 : LocalDeclMap(LocalDeclMap) {}
350
351 void addCopy(DeclStmt const *PM) {
352 // Figure out what param it refers to.
353
354 assert(PM->isSingleDecl())((void)0);
355 VarDecl const*VD = static_cast<VarDecl const*>(PM->getSingleDecl());
356 Expr const *InitExpr = VD->getInit();
357 GetParamRef Visitor;
358 Visitor.Visit(const_cast<Expr*>(InitExpr));
359 assert(Visitor.Expr)((void)0);
360 DeclRefExpr *DREOrig = Visitor.Expr;
361 auto *PD = DREOrig->getDecl();
362
363 auto it = LocalDeclMap.find(PD);
364 assert(it != LocalDeclMap.end() && "parameter is not found")((void)0);
365 SavedLocals.insert({ PD, it->second });
366
367 auto copyIt = LocalDeclMap.find(VD);
368 assert(copyIt != LocalDeclMap.end() && "parameter copy is not found")((void)0);
369 it->second = copyIt->getSecond();
370 }
371
372 ~ParamReferenceReplacerRAII() {
373 for (auto&& SavedLocal : SavedLocals) {
374 LocalDeclMap.insert({SavedLocal.first, SavedLocal.second});
375 }
376 }
377 };
378}
379
380// For WinEH exception representation backend needs to know what funclet coro.end
381// belongs to. That information is passed in a funclet bundle.
382static SmallVector<llvm::OperandBundleDef, 1>
383getBundlesForCoroEnd(CodeGenFunction &CGF) {
384 SmallVector<llvm::OperandBundleDef, 1> BundleList;
385
386 if (llvm::Instruction *EHPad = CGF.CurrentFuncletPad)
387 BundleList.emplace_back("funclet", EHPad);
388
389 return BundleList;
390}
391
392namespace {
393// We will insert coro.end to cut any of the destructors for objects that
394// do not need to be destroyed once the coroutine is resumed.
395// See llvm/docs/Coroutines.rst for more details about coro.end.
396struct CallCoroEnd final : public EHScopeStack::Cleanup {
397 void Emit(CodeGenFunction &CGF, Flags flags) override {
398 auto &CGM = CGF.CGM;
399 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
400 llvm::Function *CoroEndFn = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
401 // See if we have a funclet bundle to associate coro.end with. (WinEH)
402 auto Bundles = getBundlesForCoroEnd(CGF);
403 auto *CoroEnd = CGF.Builder.CreateCall(
404 CoroEndFn, {NullPtr, CGF.Builder.getTrue()}, Bundles);
405 if (Bundles.empty()) {
406 // Otherwise, (landingpad model), create a conditional branch that leads
407 // either to a cleanup block or a block with EH resume instruction.
408 auto *ResumeBB = CGF.getEHResumeBlock(/*isCleanup=*/true);
409 auto *CleanupContBB = CGF.createBasicBlock("cleanup.cont");
410 CGF.Builder.CreateCondBr(CoroEnd, ResumeBB, CleanupContBB);
411 CGF.EmitBlock(CleanupContBB);
412 }
413 }
414};
415}
416
417namespace {
418// Make sure to call coro.delete on scope exit.
419struct CallCoroDelete final : public EHScopeStack::Cleanup {
420 Stmt *Deallocate;
421
422 // Emit "if (coro.free(CoroId, CoroBegin)) Deallocate;"
423
424 // Note: That deallocation will be emitted twice: once for a normal exit and
425 // once for exceptional exit. This usage is safe because Deallocate does not
426 // contain any declarations. The SubStmtBuilder::makeNewAndDeleteExpr()
427 // builds a single call to a deallocation function which is safe to emit
428 // multiple times.
429 void Emit(CodeGenFunction &CGF, Flags) override {
430 // Remember the current point, as we are going to emit deallocation code
431 // first to get to coro.free instruction that is an argument to a delete
432 // call.
433 BasicBlock *SaveInsertBlock = CGF.Builder.GetInsertBlock();
434
435 auto *FreeBB = CGF.createBasicBlock("coro.free");
436 CGF.EmitBlock(FreeBB);
437 CGF.EmitStmt(Deallocate);
438
439 auto *AfterFreeBB = CGF.createBasicBlock("after.coro.free");
440 CGF.EmitBlock(AfterFreeBB);
441
442 // We should have captured coro.free from the emission of deallocate.
443 auto *CoroFree = CGF.CurCoro.Data->LastCoroFree;
444 if (!CoroFree) {
445 CGF.CGM.Error(Deallocate->getBeginLoc(),
446 "Deallocation expressoin does not refer to coro.free");
447 return;
448 }
449
450 // Get back to the block we were originally and move coro.free there.
451 auto *InsertPt = SaveInsertBlock->getTerminator();
452 CoroFree->moveBefore(InsertPt);
453 CGF.Builder.SetInsertPoint(InsertPt);
454
455 // Add if (auto *mem = coro.free) Deallocate;
456 auto *NullPtr = llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
457 auto *Cond = CGF.Builder.CreateICmpNE(CoroFree, NullPtr);
458 CGF.Builder.CreateCondBr(Cond, FreeBB, AfterFreeBB);
459
460 // No longer need old terminator.
461 InsertPt->eraseFromParent();
462 CGF.Builder.SetInsertPoint(AfterFreeBB);
463 }
464 explicit CallCoroDelete(Stmt *DeallocStmt) : Deallocate(DeallocStmt) {}
465};
466}
467
468namespace {
469struct GetReturnObjectManager {
470 CodeGenFunction &CGF;
471 CGBuilderTy &Builder;
472 const CoroutineBodyStmt &S;
473
474 Address GroActiveFlag;
475 CodeGenFunction::AutoVarEmission GroEmission;
476
477 GetReturnObjectManager(CodeGenFunction &CGF, const CoroutineBodyStmt &S)
478 : CGF(CGF), Builder(CGF.Builder), S(S), GroActiveFlag(Address::invalid()),
479 GroEmission(CodeGenFunction::AutoVarEmission::invalid()) {}
480
481 // The gro variable has to outlive coroutine frame and coroutine promise, but,
482 // it can only be initialized after coroutine promise was created, thus, we
483 // split its emission in two parts. EmitGroAlloca emits an alloca and sets up
484 // cleanups. Later when coroutine promise is available we initialize the gro
485 // and sets the flag that the cleanup is now active.
486
487 void EmitGroAlloca() {
488 auto *GroDeclStmt = dyn_cast<DeclStmt>(S.getResultDecl());
489 if (!GroDeclStmt) {
490 // If get_return_object returns void, no need to do an alloca.
491 return;
492 }
493
494 auto *GroVarDecl = cast<VarDecl>(GroDeclStmt->getSingleDecl());
495
496 // Set GRO flag that it is not initialized yet
497 GroActiveFlag =
498 CGF.CreateTempAlloca(Builder.getInt1Ty(), CharUnits::One(), "gro.active");
499 Builder.CreateStore(Builder.getFalse(), GroActiveFlag);
500
501 GroEmission = CGF.EmitAutoVarAlloca(*GroVarDecl);
502
503 // Remember the top of EHStack before emitting the cleanup.
504 auto old_top = CGF.EHStack.stable_begin();
505 CGF.EmitAutoVarCleanups(GroEmission);
506 auto top = CGF.EHStack.stable_begin();
507
508 // Make the cleanup conditional on gro.active
509 for (auto b = CGF.EHStack.find(top), e = CGF.EHStack.find(old_top);
510 b != e; b++) {
511 if (auto *Cleanup = dyn_cast<EHCleanupScope>(&*b)) {
512 assert(!Cleanup->hasActiveFlag() && "cleanup already has active flag?")((void)0);
513 Cleanup->setActiveFlag(GroActiveFlag);
514 Cleanup->setTestFlagInEHCleanup();
515 Cleanup->setTestFlagInNormalCleanup();
516 }
517 }
518 }
519
520 void EmitGroInit() {
521 if (!GroActiveFlag.isValid()) {
522 // No Gro variable was allocated. Simply emit the call to
523 // get_return_object.
524 CGF.EmitStmt(S.getResultDecl());
525 return;
526 }
527
528 CGF.EmitAutoVarInit(GroEmission);
529 Builder.CreateStore(Builder.getTrue(), GroActiveFlag);
530 }
531};
532}
533
534static void emitBodyAndFallthrough(CodeGenFunction &CGF,
535 const CoroutineBodyStmt &S, Stmt *Body) {
536 CGF.EmitStmt(Body);
537 const bool CanFallthrough = CGF.Builder.GetInsertBlock();
538 if (CanFallthrough)
539 if (Stmt *OnFallthrough = S.getFallthroughHandler())
540 CGF.EmitStmt(OnFallthrough);
541}
542
543void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) {
544 auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
545 auto &TI = CGM.getContext().getTargetInfo();
546 unsigned NewAlign = TI.getNewAlign() / TI.getCharWidth();
547
548 auto *EntryBB = Builder.GetInsertBlock();
549 auto *AllocBB = createBasicBlock("coro.alloc");
550 auto *InitBB = createBasicBlock("coro.init");
551 auto *FinalBB = createBasicBlock("coro.final");
552 auto *RetBB = createBasicBlock("coro.ret");
553
554 auto *CoroId = Builder.CreateCall(
555 CGM.getIntrinsic(llvm::Intrinsic::coro_id),
556 {Builder.getInt32(NewAlign), NullPtr, NullPtr, NullPtr});
557 createCoroData(*this, CurCoro, CoroId);
1
Passing null pointer value via 4th parameter 'CoroIdExpr'
2
Calling 'createCoroData'
558 CurCoro.Data->SuspendBB = RetBB;
559 assert(ShouldEmitLifetimeMarkers &&((void)0)
560 "Must emit lifetime intrinsics for coroutines")((void)0);
561
562 // Backend is allowed to elide memory allocations, to help it, emit
563 // auto mem = coro.alloc() ? 0 : ... allocation code ...;
564 auto *CoroAlloc = Builder.CreateCall(
565 CGM.getIntrinsic(llvm::Intrinsic::coro_alloc), {CoroId});
566
567 Builder.CreateCondBr(CoroAlloc, AllocBB, InitBB);
568
569 EmitBlock(AllocBB);
570 auto *AllocateCall = EmitScalarExpr(S.getAllocate());
571 auto *AllocOrInvokeContBB = Builder.GetInsertBlock();
572
573 // Handle allocation failure if 'ReturnStmtOnAllocFailure' was provided.
574 if (auto *RetOnAllocFailure = S.getReturnStmtOnAllocFailure()) {
575 auto *RetOnFailureBB = createBasicBlock("coro.ret.on.failure");
576
577 // See if allocation was successful.
578 auto *NullPtr = llvm::ConstantPointerNull::get(Int8PtrTy);
579 auto *Cond = Builder.CreateICmpNE(AllocateCall, NullPtr);
580 Builder.CreateCondBr(Cond, InitBB, RetOnFailureBB);
581
582 // If not, return OnAllocFailure object.
583 EmitBlock(RetOnFailureBB);
584 EmitStmt(RetOnAllocFailure);
585 }
586 else {
587 Builder.CreateBr(InitBB);
588 }
589
590 EmitBlock(InitBB);
591
592 // Pass the result of the allocation to coro.begin.
593 auto *Phi = Builder.CreatePHI(VoidPtrTy, 2);
594 Phi->addIncoming(NullPtr, EntryBB);
595 Phi->addIncoming(AllocateCall, AllocOrInvokeContBB);
596 auto *CoroBegin = Builder.CreateCall(
597 CGM.getIntrinsic(llvm::Intrinsic::coro_begin), {CoroId, Phi});
598 CurCoro.Data->CoroBegin = CoroBegin;
599
600 GetReturnObjectManager GroManager(*this, S);
601 GroManager.EmitGroAlloca();
602
603 CurCoro.Data->CleanupJD = getJumpDestInCurrentScope(RetBB);
604 {
605 CGDebugInfo *DI = getDebugInfo();
606 ParamReferenceReplacerRAII ParamReplacer(LocalDeclMap);
607 CodeGenFunction::RunCleanupsScope ResumeScope(*this);
608 EHStack.pushCleanup<CallCoroDelete>(NormalAndEHCleanup, S.getDeallocate());
609
610 // Create mapping between parameters and copy-params for coroutine function.
611 auto ParamMoves = S.getParamMoves();
612 assert(((void)0)
613 (ParamMoves.size() == 0 || (ParamMoves.size() == FnArgs.size())) &&((void)0)
614 "ParamMoves and FnArgs should be the same size for coroutine function")((void)0);
615 if (ParamMoves.size() == FnArgs.size() && DI)
616 for (const auto Pair : llvm::zip(FnArgs, ParamMoves))
617 DI->getCoroutineParameterMappings().insert(
618 {std::get<0>(Pair), std::get<1>(Pair)});
619
620 // Create parameter copies. We do it before creating a promise, since an
621 // evolution of coroutine TS may allow promise constructor to observe
622 // parameter copies.
623 for (auto *PM : S.getParamMoves()) {
624 EmitStmt(PM);
625 ParamReplacer.addCopy(cast<DeclStmt>(PM));
626 // TODO: if(CoroParam(...)) need to surround ctor and dtor
627 // for the copy, so that llvm can elide it if the copy is
628 // not needed.
629 }
630
631 EmitStmt(S.getPromiseDeclStmt());
632
633 Address PromiseAddr = GetAddrOfLocalVar(S.getPromiseDecl());
634 auto *PromiseAddrVoidPtr =
635 new llvm::BitCastInst(PromiseAddr.getPointer(), VoidPtrTy, "", CoroId);
636 // Update CoroId to refer to the promise. We could not do it earlier because
637 // promise local variable was not emitted yet.
638 CoroId->setArgOperand(1, PromiseAddrVoidPtr);
639
640 // Now we have the promise, initialize the GRO
641 GroManager.EmitGroInit();
642
643 EHStack.pushCleanup<CallCoroEnd>(EHCleanup);
644
645 CurCoro.Data->CurrentAwaitKind = AwaitKind::Init;
646 CurCoro.Data->ExceptionHandler = S.getExceptionHandler();
647 EmitStmt(S.getInitSuspendStmt());
648 CurCoro.Data->FinalJD = getJumpDestInCurrentScope(FinalBB);
649
650 CurCoro.Data->CurrentAwaitKind = AwaitKind::Normal;
651
652 if (CurCoro.Data->ExceptionHandler) {
653 // If we generated IR to record whether an exception was thrown from
654 // 'await_resume', then use that IR to determine whether the coroutine
655 // body should be skipped.
656 // If we didn't generate the IR (perhaps because 'await_resume' was marked
657 // as 'noexcept'), then we skip this check.
658 BasicBlock *ContBB = nullptr;
659 if (CurCoro.Data->ResumeEHVar) {
660 BasicBlock *BodyBB = createBasicBlock("coro.resumed.body");
661 ContBB = createBasicBlock("coro.resumed.cont");
662 Value *SkipBody = Builder.CreateFlagLoad(CurCoro.Data->ResumeEHVar,
663 "coro.resumed.eh");
664 Builder.CreateCondBr(SkipBody, ContBB, BodyBB);
665 EmitBlock(BodyBB);
666 }
667
668 auto Loc = S.getBeginLoc();
669 CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr,
670 CurCoro.Data->ExceptionHandler);
671 auto *TryStmt =
672 CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch);
673
674 EnterCXXTryStmt(*TryStmt);
675 emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock());
676 ExitCXXTryStmt(*TryStmt);
677
678 if (ContBB)
679 EmitBlock(ContBB);
680 }
681 else {
682 emitBodyAndFallthrough(*this, S, S.getBody());
683 }
684
685 // See if we need to generate final suspend.
686 const bool CanFallthrough = Builder.GetInsertBlock();
687 const bool HasCoreturns = CurCoro.Data->CoreturnCount > 0;
688 if (CanFallthrough || HasCoreturns) {
689 EmitBlock(FinalBB);
690 CurCoro.Data->CurrentAwaitKind = AwaitKind::Final;
691 EmitStmt(S.getFinalSuspendStmt());
692 } else {
693 // We don't need FinalBB. Emit it to make sure the block is deleted.
694 EmitBlock(FinalBB, /*IsFinished=*/true);
695 }
696 }
697
698 EmitBlock(RetBB);
699 // Emit coro.end before getReturnStmt (and parameter destructors), since
700 // resume and destroy parts of the coroutine should not include them.
701 llvm::Function *CoroEnd = CGM.getIntrinsic(llvm::Intrinsic::coro_end);
702 Builder.CreateCall(CoroEnd, {NullPtr, Builder.getFalse()});
703
704 if (Stmt *Ret = S.getReturnStmt())
705 EmitStmt(Ret);
706}
707
708// Emit coroutine intrinsic and patch up arguments of the token type.
709RValue CodeGenFunction::EmitCoroutineIntrinsic(const CallExpr *E,
710 unsigned int IID) {
711 SmallVector<llvm::Value *, 8> Args;
712 switch (IID) {
713 default:
714 break;
715 // The coro.frame builtin is replaced with an SSA value of the coro.begin
716 // intrinsic.
717 case llvm::Intrinsic::coro_frame: {
718 if (CurCoro.Data && CurCoro.Data->CoroBegin) {
719 return RValue::get(CurCoro.Data->CoroBegin);
720 }
721 CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_begin "
722 "has been used earlier in this function");
723 auto NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy());
724 return RValue::get(NullPtr);
725 }
726 // The following three intrinsics take a token parameter referring to a token
727 // returned by earlier call to @llvm.coro.id. Since we cannot represent it in
728 // builtins, we patch it up here.
729 case llvm::Intrinsic::coro_alloc:
730 case llvm::Intrinsic::coro_begin:
731 case llvm::Intrinsic::coro_free: {
732 if (CurCoro.Data && CurCoro.Data->CoroId) {
733 Args.push_back(CurCoro.Data->CoroId);
734 break;
735 }
736 CGM.Error(E->getBeginLoc(), "this builtin expect that __builtin_coro_id has"
737 " been used earlier in this function");
738 // Fallthrough to the next case to add TokenNone as the first argument.
739 LLVM_FALLTHROUGH[[gnu::fallthrough]];
740 }
741 // @llvm.coro.suspend takes a token parameter. Add token 'none' as the first
742 // argument.
743 case llvm::Intrinsic::coro_suspend:
744 Args.push_back(llvm::ConstantTokenNone::get(getLLVMContext()));
745 break;
746 }
747 for (const Expr *Arg : E->arguments())
748 Args.push_back(EmitScalarExpr(Arg));
749
750 llvm::Function *F = CGM.getIntrinsic(IID);
751 llvm::CallInst *Call = Builder.CreateCall(F, Args);
752
753 // Note: The following code is to enable to emit coro.id and coro.begin by
754 // hand to experiment with coroutines in C.
755 // If we see @llvm.coro.id remember it in the CoroData. We will update
756 // coro.alloc, coro.begin and coro.free intrinsics to refer to it.
757 if (IID == llvm::Intrinsic::coro_id) {
758 createCoroData(*this, CurCoro, Call, E);
759 }
760 else if (IID == llvm::Intrinsic::coro_begin) {
761 if (CurCoro.Data)
762 CurCoro.Data->CoroBegin = Call;
763 }
764 else if (IID == llvm::Intrinsic::coro_free) {
765 // Remember the last coro_free as we need it to build the conditional
766 // deletion of the coroutine frame.
767 if (CurCoro.Data)
768 CurCoro.Data->LastCoroFree = Call;
769 }
770 return RValue::get(Call);
771}

/usr/include/c++/v1/__memory/unique_ptr.h

1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___MEMORY_UNIQUE_PTR_H
11#define _LIBCPP___MEMORY_UNIQUE_PTR_H
12
13#include <__config>
14#include <__functional_base>
15#include <__functional/hash.h>
16#include <__functional/operations.h>
17#include <__memory/allocator_traits.h> // __pointer
18#include <__memory/compressed_pair.h>
19#include <__utility/forward.h>
20#include <cstddef>
21#include <type_traits>
22#include <utility>
23
24#if _LIBCPP_STD_VER14 <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
25# include <__memory/auto_ptr.h>
26#endif
27
28#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29#pragma GCC system_header
30#endif
31
32_LIBCPP_PUSH_MACROSpush_macro("min") push_macro("max")
33#include <__undef_macros>
34
35_LIBCPP_BEGIN_NAMESPACE_STDnamespace std { inline namespace __1 {
36
37template <class _Tp>
38struct _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) default_delete {
39 static_assert(!is_function<_Tp>::value,
40 "default_delete cannot be instantiated for function types");
41#ifndef _LIBCPP_CXX03_LANG
42 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
constexpr default_delete() _NOEXCEPTnoexcept = default;
43#else
44 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
default_delete() {}
45#endif
46 template <class _Up>
47 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
48 default_delete(const default_delete<_Up>&,
49 typename enable_if<is_convertible<_Up*, _Tp*>::value>::type* =
50 0) _NOEXCEPTnoexcept {}
51
52 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
void operator()(_Tp* __ptr) const _NOEXCEPTnoexcept {
53 static_assert(sizeof(_Tp) > 0,
54 "default_delete can not delete incomplete type");
55 static_assert(!is_void<_Tp>::value,
56 "default_delete can not delete incomplete type");
57 delete __ptr;
58 }
59};
60
61template <class _Tp>
62struct _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) default_delete<_Tp[]> {
63private:
64 template <class _Up>
65 struct _EnableIfConvertible
66 : enable_if<is_convertible<_Up(*)[], _Tp(*)[]>::value> {};
67
68public:
69#ifndef _LIBCPP_CXX03_LANG
70 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
constexpr default_delete() _NOEXCEPTnoexcept = default;
71#else
72 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
default_delete() {}
73#endif
74
75 template <class _Up>
76 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
77 default_delete(const default_delete<_Up[]>&,
78 typename _EnableIfConvertible<_Up>::type* = 0) _NOEXCEPTnoexcept {}
79
80 template <class _Up>
81 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
82 typename _EnableIfConvertible<_Up>::type
83 operator()(_Up* __ptr) const _NOEXCEPTnoexcept {
84 static_assert(sizeof(_Tp) > 0,
85 "default_delete can not delete incomplete type");
86 static_assert(!is_void<_Tp>::value,
87 "default_delete can not delete void type");
88 delete[] __ptr;
89 }
90};
91
92template <class _Deleter>
93struct __unique_ptr_deleter_sfinae {
94 static_assert(!is_reference<_Deleter>::value, "incorrect specialization");
95 typedef const _Deleter& __lval_ref_type;
96 typedef _Deleter&& __good_rval_ref_type;
97 typedef true_type __enable_rval_overload;
98};
99
100template <class _Deleter>
101struct __unique_ptr_deleter_sfinae<_Deleter const&> {
102 typedef const _Deleter& __lval_ref_type;
103 typedef const _Deleter&& __bad_rval_ref_type;
104 typedef false_type __enable_rval_overload;
105};
106
107template <class _Deleter>
108struct __unique_ptr_deleter_sfinae<_Deleter&> {
109 typedef _Deleter& __lval_ref_type;
110 typedef _Deleter&& __bad_rval_ref_type;
111 typedef false_type __enable_rval_overload;
112};
113
114#if defined(_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI)
115# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI __attribute__((trivial_abi))
116#else
117# define _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI
118#endif
119
120template <class _Tp, class _Dp = default_delete<_Tp> >
121class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) unique_ptr {
122public:
123 typedef _Tp element_type;
124 typedef _Dp deleter_type;
125 typedef _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) typename __pointer<_Tp, deleter_type>::type pointer;
126
127 static_assert(!is_rvalue_reference<deleter_type>::value,
128 "the specified deleter type cannot be an rvalue reference");
129
130private:
131 __compressed_pair<pointer, deleter_type> __ptr_;
132
133 struct __nat { int __for_bool_; };
134
135 typedef _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
136
137 template <bool _Dummy>
138 using _LValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
139 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
140
141 template <bool _Dummy>
142 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
143 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
144
145 template <bool _Dummy>
146 using _BadRValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
147 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
148
149 template <bool _Dummy, class _Deleter = typename __dependent_type<
150 __identity<deleter_type>, _Dummy>::type>
151 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
152 typename enable_if<is_default_constructible<_Deleter>::value &&
153 !is_pointer<_Deleter>::value>::type;
154
155 template <class _ArgType>
156 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
157 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
158
159 template <class _UPtr, class _Up>
160 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
161 is_convertible<typename _UPtr::pointer, pointer>::value &&
162 !is_array<_Up>::value
163 >::type;
164
165 template <class _UDel>
166 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
167 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
168 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
169 >::type;
170
171 template <class _UDel>
172 using _EnableIfDeleterAssignable = typename enable_if<
173 is_assignable<_Dp&, _UDel&&>::value
174 >::type;
175
176public:
177 template <bool _Dummy = true,
178 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
179 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
180 _LIBCPP_CONSTEXPRconstexpr unique_ptr() _NOEXCEPTnoexcept : __ptr_(pointer(), __default_init_tag()) {}
181
182 template <bool _Dummy = true,
183 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
184 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
185 _LIBCPP_CONSTEXPRconstexpr unique_ptr(nullptr_t) _NOEXCEPTnoexcept : __ptr_(pointer(), __default_init_tag()) {}
186
187 template <bool _Dummy = true,
188 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
189 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
190 explicit unique_ptr(pointer __p) _NOEXCEPTnoexcept : __ptr_(__p, __default_init_tag()) {}
191
192 template <bool _Dummy = true,
193 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
194 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
195 unique_ptr(pointer __p, _LValRefType<_Dummy> __d) _NOEXCEPTnoexcept
196 : __ptr_(__p, __d) {}
197
198 template <bool _Dummy = true,
199 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
200 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
201 unique_ptr(pointer __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPTnoexcept
202 : __ptr_(__p, _VSTDstd::__1::move(__d)) {
203 static_assert(!is_reference<deleter_type>::value,
204 "rvalue deleter bound to reference");
205 }
206
207 template <bool _Dummy = true,
208 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> > >
209 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
210 unique_ptr(pointer __p, _BadRValRefType<_Dummy> __d) = delete;
211
212 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
213 unique_ptr(unique_ptr&& __u) _NOEXCEPTnoexcept
214 : __ptr_(__u.release(), _VSTDstd::__1::forward<deleter_type>(__u.get_deleter())) {
215 }
216
217 template <class _Up, class _Ep,
218 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
219 class = _EnableIfDeleterConvertible<_Ep>
220 >
221 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
222 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPTnoexcept
223 : __ptr_(__u.release(), _VSTDstd::__1::forward<_Ep>(__u.get_deleter())) {}
224
225#if _LIBCPP_STD_VER14 <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
226 template <class _Up>
227 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
228 unique_ptr(auto_ptr<_Up>&& __p,
229 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
230 is_same<_Dp, default_delete<_Tp> >::value,
231 __nat>::type = __nat()) _NOEXCEPTnoexcept
232 : __ptr_(__p.release(), __default_init_tag()) {}
233#endif
234
235 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
236 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPTnoexcept {
237 reset(__u.release());
238 __ptr_.second() = _VSTDstd::__1::forward<deleter_type>(__u.get_deleter());
239 return *this;
240 }
241
242 template <class _Up, class _Ep,
243 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
244 class = _EnableIfDeleterAssignable<_Ep>
245 >
246 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
247 unique_ptr& operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPTnoexcept {
248 reset(__u.release());
249 __ptr_.second() = _VSTDstd::__1::forward<_Ep>(__u.get_deleter());
250 return *this;
251 }
252
253#if _LIBCPP_STD_VER14 <= 14 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_AUTO_PTR)
254 template <class _Up>
255 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
256 typename enable_if<is_convertible<_Up*, _Tp*>::value &&
257 is_same<_Dp, default_delete<_Tp> >::value,
258 unique_ptr&>::type
259 operator=(auto_ptr<_Up> __p) {
260 reset(__p.release());
261 return *this;
262 }
263#endif
264
265#ifdef _LIBCPP_CXX03_LANG
266 unique_ptr(unique_ptr const&) = delete;
267 unique_ptr& operator=(unique_ptr const&) = delete;
268#endif
269
270
271 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
272 ~unique_ptr() { reset(); }
273
274 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
275 unique_ptr& operator=(nullptr_t) _NOEXCEPTnoexcept {
276 reset();
277 return *this;
278 }
279
280 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
281 typename add_lvalue_reference<_Tp>::type
282 operator*() const {
283 return *__ptr_.first();
284 }
285 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
286 pointer operator->() const _NOEXCEPTnoexcept {
287 return __ptr_.first();
288 }
289 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
290 pointer get() const _NOEXCEPTnoexcept {
291 return __ptr_.first();
292 }
293 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
294 deleter_type& get_deleter() _NOEXCEPTnoexcept {
295 return __ptr_.second();
296 }
297 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
298 const deleter_type& get_deleter() const _NOEXCEPTnoexcept {
299 return __ptr_.second();
300 }
301 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
302 explicit operator bool() const _NOEXCEPTnoexcept {
303 return __ptr_.first() != nullptr;
4
Assuming the condition is true
5
Returning the value 1, which participates in a condition later
304 }
305
306 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
307 pointer release() _NOEXCEPTnoexcept {
308 pointer __t = __ptr_.first();
309 __ptr_.first() = pointer();
310 return __t;
311 }
312
313 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
314 void reset(pointer __p = pointer()) _NOEXCEPTnoexcept {
315 pointer __tmp = __ptr_.first();
316 __ptr_.first() = __p;
317 if (__tmp)
318 __ptr_.second()(__tmp);
319 }
320
321 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
322 void swap(unique_ptr& __u) _NOEXCEPTnoexcept {
323 __ptr_.swap(__u.__ptr_);
324 }
325};
326
327
328template <class _Tp, class _Dp>
329class _LIBCPP_UNIQUE_PTR_TRIVIAL_ABI _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) unique_ptr<_Tp[], _Dp> {
330public:
331 typedef _Tp element_type;
332 typedef _Dp deleter_type;
333 typedef typename __pointer<_Tp, deleter_type>::type pointer;
334
335private:
336 __compressed_pair<pointer, deleter_type> __ptr_;
337
338 template <class _From>
339 struct _CheckArrayPointerConversion : is_same<_From, pointer> {};
340
341 template <class _FromElem>
342 struct _CheckArrayPointerConversion<_FromElem*>
343 : integral_constant<bool,
344 is_same<_FromElem*, pointer>::value ||
345 (is_same<pointer, element_type*>::value &&
346 is_convertible<_FromElem(*)[], element_type(*)[]>::value)
347 >
348 {};
349
350 typedef __unique_ptr_deleter_sfinae<_Dp> _DeleterSFINAE;
351
352 template <bool _Dummy>
353 using _LValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
354 typename __dependent_type<_DeleterSFINAE, _Dummy>::__lval_ref_type;
355
356 template <bool _Dummy>
357 using _GoodRValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
358 typename __dependent_type<_DeleterSFINAE, _Dummy>::__good_rval_ref_type;
359
360 template <bool _Dummy>
361 using _BadRValRefType _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
362 typename __dependent_type<_DeleterSFINAE, _Dummy>::__bad_rval_ref_type;
363
364 template <bool _Dummy, class _Deleter = typename __dependent_type<
365 __identity<deleter_type>, _Dummy>::type>
366 using _EnableIfDeleterDefaultConstructible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
367 typename enable_if<is_default_constructible<_Deleter>::value &&
368 !is_pointer<_Deleter>::value>::type;
369
370 template <class _ArgType>
371 using _EnableIfDeleterConstructible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) =
372 typename enable_if<is_constructible<deleter_type, _ArgType>::value>::type;
373
374 template <class _Pp>
375 using _EnableIfPointerConvertible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
376 _CheckArrayPointerConversion<_Pp>::value
377 >::type;
378
379 template <class _UPtr, class _Up,
380 class _ElemT = typename _UPtr::element_type>
381 using _EnableIfMoveConvertible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
382 is_array<_Up>::value &&
383 is_same<pointer, element_type*>::value &&
384 is_same<typename _UPtr::pointer, _ElemT*>::value &&
385 is_convertible<_ElemT(*)[], element_type(*)[]>::value
386 >::type;
387
388 template <class _UDel>
389 using _EnableIfDeleterConvertible _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
390 (is_reference<_Dp>::value && is_same<_Dp, _UDel>::value) ||
391 (!is_reference<_Dp>::value && is_convertible<_UDel, _Dp>::value)
392 >::type;
393
394 template <class _UDel>
395 using _EnableIfDeleterAssignable _LIBCPP_NODEBUG_TYPE__attribute__((nodebug)) = typename enable_if<
396 is_assignable<_Dp&, _UDel&&>::value
397 >::type;
398
399public:
400 template <bool _Dummy = true,
401 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
402 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
403 _LIBCPP_CONSTEXPRconstexpr unique_ptr() _NOEXCEPTnoexcept : __ptr_(pointer(), __default_init_tag()) {}
404
405 template <bool _Dummy = true,
406 class = _EnableIfDeleterDefaultConstructible<_Dummy> >
407 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
408 _LIBCPP_CONSTEXPRconstexpr unique_ptr(nullptr_t) _NOEXCEPTnoexcept : __ptr_(pointer(), __default_init_tag()) {}
409
410 template <class _Pp, bool _Dummy = true,
411 class = _EnableIfDeleterDefaultConstructible<_Dummy>,
412 class = _EnableIfPointerConvertible<_Pp> >
413 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
414 explicit unique_ptr(_Pp __p) _NOEXCEPTnoexcept
415 : __ptr_(__p, __default_init_tag()) {}
416
417 template <class _Pp, bool _Dummy = true,
418 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> >,
419 class = _EnableIfPointerConvertible<_Pp> >
420 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
421 unique_ptr(_Pp __p, _LValRefType<_Dummy> __d) _NOEXCEPTnoexcept
422 : __ptr_(__p, __d) {}
423
424 template <bool _Dummy = true,
425 class = _EnableIfDeleterConstructible<_LValRefType<_Dummy> > >
426 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
427 unique_ptr(nullptr_t, _LValRefType<_Dummy> __d) _NOEXCEPTnoexcept
428 : __ptr_(nullptr, __d) {}
429
430 template <class _Pp, bool _Dummy = true,
431 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> >,
432 class = _EnableIfPointerConvertible<_Pp> >
433 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
434 unique_ptr(_Pp __p, _GoodRValRefType<_Dummy> __d) _NOEXCEPTnoexcept
435 : __ptr_(__p, _VSTDstd::__1::move(__d)) {
436 static_assert(!is_reference<deleter_type>::value,
437 "rvalue deleter bound to reference");
438 }
439
440 template <bool _Dummy = true,
441 class = _EnableIfDeleterConstructible<_GoodRValRefType<_Dummy> > >
442 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
443 unique_ptr(nullptr_t, _GoodRValRefType<_Dummy> __d) _NOEXCEPTnoexcept
444 : __ptr_(nullptr, _VSTDstd::__1::move(__d)) {
445 static_assert(!is_reference<deleter_type>::value,
446 "rvalue deleter bound to reference");
447 }
448
449 template <class _Pp, bool _Dummy = true,
450 class = _EnableIfDeleterConstructible<_BadRValRefType<_Dummy> >,
451 class = _EnableIfPointerConvertible<_Pp> >
452 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
453 unique_ptr(_Pp __p, _BadRValRefType<_Dummy> __d) = delete;
454
455 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
456 unique_ptr(unique_ptr&& __u) _NOEXCEPTnoexcept
457 : __ptr_(__u.release(), _VSTDstd::__1::forward<deleter_type>(__u.get_deleter())) {
458 }
459
460 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
461 unique_ptr& operator=(unique_ptr&& __u) _NOEXCEPTnoexcept {
462 reset(__u.release());
463 __ptr_.second() = _VSTDstd::__1::forward<deleter_type>(__u.get_deleter());
464 return *this;
465 }
466
467 template <class _Up, class _Ep,
468 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
469 class = _EnableIfDeleterConvertible<_Ep>
470 >
471 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
472 unique_ptr(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPTnoexcept
473 : __ptr_(__u.release(), _VSTDstd::__1::forward<_Ep>(__u.get_deleter())) {
474 }
475
476 template <class _Up, class _Ep,
477 class = _EnableIfMoveConvertible<unique_ptr<_Up, _Ep>, _Up>,
478 class = _EnableIfDeleterAssignable<_Ep>
479 >
480 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
481 unique_ptr&
482 operator=(unique_ptr<_Up, _Ep>&& __u) _NOEXCEPTnoexcept {
483 reset(__u.release());
484 __ptr_.second() = _VSTDstd::__1::forward<_Ep>(__u.get_deleter());
485 return *this;
486 }
487
488#ifdef _LIBCPP_CXX03_LANG
489 unique_ptr(unique_ptr const&) = delete;
490 unique_ptr& operator=(unique_ptr const&) = delete;
491#endif
492
493public:
494 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
495 ~unique_ptr() { reset(); }
496
497 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
498 unique_ptr& operator=(nullptr_t) _NOEXCEPTnoexcept {
499 reset();
500 return *this;
501 }
502
503 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
504 typename add_lvalue_reference<_Tp>::type
505 operator[](size_t __i) const {
506 return __ptr_.first()[__i];
507 }
508 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
509 pointer get() const _NOEXCEPTnoexcept {
510 return __ptr_.first();
511 }
512
513 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
514 deleter_type& get_deleter() _NOEXCEPTnoexcept {
515 return __ptr_.second();
516 }
517
518 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
519 const deleter_type& get_deleter() const _NOEXCEPTnoexcept {
520 return __ptr_.second();
521 }
522 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
523 explicit operator bool() const _NOEXCEPTnoexcept {
524 return __ptr_.first() != nullptr;
525 }
526
527 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
528 pointer release() _NOEXCEPTnoexcept {
529 pointer __t = __ptr_.first();
530 __ptr_.first() = pointer();
531 return __t;
532 }
533
534 template <class _Pp>
535 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
536 typename enable_if<
537 _CheckArrayPointerConversion<_Pp>::value
538 >::type
539 reset(_Pp __p) _NOEXCEPTnoexcept {
540 pointer __tmp = __ptr_.first();
541 __ptr_.first() = __p;
542 if (__tmp)
543 __ptr_.second()(__tmp);
544 }
545
546 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
547 void reset(nullptr_t = nullptr) _NOEXCEPTnoexcept {
548 pointer __tmp = __ptr_.first();
549 __ptr_.first() = nullptr;
550 if (__tmp)
551 __ptr_.second()(__tmp);
552 }
553
554 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
555 void swap(unique_ptr& __u) _NOEXCEPTnoexcept {
556 __ptr_.swap(__u.__ptr_);
557 }
558
559};
560
561template <class _Tp, class _Dp>
562inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
563typename enable_if<
564 __is_swappable<_Dp>::value,
565 void
566>::type
567swap(unique_ptr<_Tp, _Dp>& __x, unique_ptr<_Tp, _Dp>& __y) _NOEXCEPTnoexcept {__x.swap(__y);}
568
569template <class _T1, class _D1, class _T2, class _D2>
570inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
571bool
572operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
573
574template <class _T1, class _D1, class _T2, class _D2>
575inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
576bool
577operator!=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x == __y);}
578
579template <class _T1, class _D1, class _T2, class _D2>
580inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
581bool
582operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
583{
584 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
585 typedef typename unique_ptr<_T2, _D2>::pointer _P2;
586 typedef typename common_type<_P1, _P2>::type _Vp;
587 return less<_Vp>()(__x.get(), __y.get());
588}
589
590template <class _T1, class _D1, class _T2, class _D2>
591inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
592bool
593operator> (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __y < __x;}
594
595template <class _T1, class _D1, class _T2, class _D2>
596inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
597bool
598operator<=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__y < __x);}
599
600template <class _T1, class _D1, class _T2, class _D2>
601inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
602bool
603operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
604
605template <class _T1, class _D1>
606inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
607bool
608operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPTnoexcept
609{
610 return !__x;
611}
612
613template <class _T1, class _D1>
614inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
615bool
616operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPTnoexcept
617{
618 return !__x;
619}
620
621template <class _T1, class _D1>
622inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
623bool
624operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPTnoexcept
625{
626 return static_cast<bool>(__x);
627}
628
629template <class _T1, class _D1>
630inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
631bool
632operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPTnoexcept
633{
634 return static_cast<bool>(__x);
635}
636
637template <class _T1, class _D1>
638inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
639bool
640operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
641{
642 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
643 return less<_P1>()(__x.get(), nullptr);
644}
645
646template <class _T1, class _D1>
647inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
648bool
649operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
650{
651 typedef typename unique_ptr<_T1, _D1>::pointer _P1;
652 return less<_P1>()(nullptr, __x.get());
653}
654
655template <class _T1, class _D1>
656inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
657bool
658operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
659{
660 return nullptr < __x;
661}
662
663template <class _T1, class _D1>
664inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
665bool
666operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
667{
668 return __x < nullptr;
669}
670
671template <class _T1, class _D1>
672inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
673bool
674operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
675{
676 return !(nullptr < __x);
677}
678
679template <class _T1, class _D1>
680inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
681bool
682operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
683{
684 return !(__x < nullptr);
685}
686
687template <class _T1, class _D1>
688inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
689bool
690operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
691{
692 return !(__x < nullptr);
693}
694
695template <class _T1, class _D1>
696inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
697bool
698operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
699{
700 return !(nullptr < __x);
701}
702
703#if _LIBCPP_STD_VER14 > 11
704
705template<class _Tp>
706struct __unique_if
707{
708 typedef unique_ptr<_Tp> __unique_single;
709};
710
711template<class _Tp>
712struct __unique_if<_Tp[]>
713{
714 typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
715};
716
717template<class _Tp, size_t _Np>
718struct __unique_if<_Tp[_Np]>
719{
720 typedef void __unique_array_known_bound;
721};
722
723template<class _Tp, class... _Args>
724inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
725typename __unique_if<_Tp>::__unique_single
726make_unique(_Args&&... __args)
727{
728 return unique_ptr<_Tp>(new _Tp(_VSTDstd::__1::forward<_Args>(__args)...));
729}
730
731template<class _Tp>
732inline _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
733typename __unique_if<_Tp>::__unique_array_unknown_bound
734make_unique(size_t __n)
735{
736 typedef typename remove_extent<_Tp>::type _Up;
737 return unique_ptr<_Tp>(new _Up[__n]());
738}
739
740template<class _Tp, class... _Args>
741 typename __unique_if<_Tp>::__unique_array_known_bound
742 make_unique(_Args&&...) = delete;
743
744#endif // _LIBCPP_STD_VER > 11
745
746template <class _Tp> struct _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) hash;
747
748template <class _Tp, class _Dp>
749#ifdef _LIBCPP_CXX03_LANG
750struct _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) hash<unique_ptr<_Tp, _Dp> >
751#else
752struct _LIBCPP_TEMPLATE_VIS__attribute__ ((__type_visibility__("default"))) hash<__enable_hash_helper<
753 unique_ptr<_Tp, _Dp>, typename unique_ptr<_Tp, _Dp>::pointer> >
754#endif
755{
756#if _LIBCPP_STD_VER14 <= 17 || defined(_LIBCPP_ENABLE_CXX20_REMOVED_BINDER_TYPEDEFS)
757 _LIBCPP_DEPRECATED_IN_CXX17 typedef unique_ptr<_Tp, _Dp> argument_type;
758 _LIBCPP_DEPRECATED_IN_CXX17 typedef size_t result_type;
759#endif
760
761 _LIBCPP_INLINE_VISIBILITY__attribute__ ((__visibility__("hidden"))) __attribute__ ((__exclude_from_explicit_instantiation__
))
762 size_t operator()(const unique_ptr<_Tp, _Dp>& __ptr) const
763 {
764 typedef typename unique_ptr<_Tp, _Dp>::pointer pointer;
765 return hash<pointer>()(__ptr.get());
766 }
767};
768
769_LIBCPP_END_NAMESPACE_STD} }
770
771_LIBCPP_POP_MACROSpop_macro("min") pop_macro("max")
772
773#endif // _LIBCPP___MEMORY_UNIQUE_PTR_H