Anders Carlsson | 5b95592 | 2009-11-24 05:51:11 +0000 | [diff] [blame] | 1 | //===--- CGTemporaries.cpp - Emit LLVM Code for C++ temporaries -----------===// |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 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 contains code dealing with C++ code generation of temporaries |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | using namespace clang; |
| 16 | using namespace CodeGen; |
| 17 | |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 18 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 19 | struct DestroyTemporary : EHScopeStack::Cleanup { |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 20 | const CXXTemporary *Temporary; |
| 21 | llvm::Value *Addr; |
| 22 | llvm::Value *CondPtr; |
| 23 | |
| 24 | DestroyTemporary(const CXXTemporary *Temporary, llvm::Value *Addr, |
| 25 | llvm::Value *CondPtr) |
| 26 | : Temporary(Temporary), Addr(Addr), CondPtr(CondPtr) {} |
| 27 | |
| 28 | void Emit(CodeGenFunction &CGF, bool IsForEH) { |
| 29 | llvm::BasicBlock *CondEnd = 0; |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 30 | |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 31 | // If this is a conditional temporary, we need to check the condition |
| 32 | // boolean and only call the destructor if it's true. |
| 33 | if (CondPtr) { |
| 34 | llvm::BasicBlock *CondBlock = |
| 35 | CGF.createBasicBlock("temp.cond-dtor.call"); |
| 36 | CondEnd = CGF.createBasicBlock("temp.cond-dtor.cont"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 38 | llvm::Value *Cond = CGF.Builder.CreateLoad(CondPtr); |
| 39 | CGF.Builder.CreateCondBr(Cond, CondBlock, CondEnd); |
| 40 | CGF.EmitBlock(CondBlock); |
| 41 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 42 | |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 43 | CGF.EmitCXXDestructorCall(Temporary->getDestructor(), |
| 44 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 45 | Addr); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 46 | |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 47 | if (CondPtr) { |
| 48 | // Reset the condition to false. |
| 49 | CGF.Builder.CreateStore(CGF.Builder.getFalse(), CondPtr); |
| 50 | CGF.EmitBlock(CondEnd); |
| 51 | } |
| 52 | } |
| 53 | }; |
| 54 | } |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 55 | |
| 56 | /// Emits all the code to cause the given temporary to be cleaned up. |
| 57 | void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary, |
| 58 | llvm::Value *Ptr) { |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 59 | llvm::AllocaInst *CondPtr = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
| 61 | // Check if temporaries need to be conditional. If so, we'll create a |
| 62 | // condition boolean, initialize it to 0 and |
Anders Carlsson | a36bf8f | 2009-11-20 17:27:56 +0000 | [diff] [blame] | 63 | if (ConditionalBranchLevel != 0) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 64 | CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 66 | // Initialize it to false. This initialization takes place right after |
| 67 | // the alloca insert point. |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 68 | InitTempAlloca(CondPtr, llvm::ConstantInt::getFalse(VMContext)); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 69 | |
| 70 | // Now set it to true. |
John McCall | 45483b4 | 2010-07-21 06:44:28 +0000 | [diff] [blame] | 71 | Builder.CreateStore(Builder.getTrue(), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 72 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 74 | EHStack.pushCleanup<DestroyTemporary>(NormalAndEHCleanup, |
| 75 | Temporary, Ptr, CondPtr); |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 78 | RValue |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame^] | 79 | CodeGenFunction::EmitExprWithCleanups(const ExprWithCleanups *E, |
| 80 | AggValueSlot Slot) { |
John McCall | df054db | 2010-07-21 06:45:54 +0000 | [diff] [blame] | 81 | RunCleanupsScope Scope(*this); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 82 | return EmitAnyExpr(E->getSubExpr(), Slot); |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 83 | } |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 84 | |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame^] | 85 | LValue CodeGenFunction::EmitExprWithCleanupsLValue(const ExprWithCleanups *E) { |
John McCall | df054db | 2010-07-21 06:45:54 +0000 | [diff] [blame] | 86 | RunCleanupsScope Scope(*this); |
| 87 | return EmitLValue(E->getSubExpr()); |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 88 | } |