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 | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 18 | static void EmitTemporaryCleanup(CodeGenFunction &CGF, |
| 19 | const CXXTemporary *Temporary, |
| 20 | llvm::Value *Addr, |
| 21 | llvm::Value *CondPtr) { |
| 22 | llvm::BasicBlock *CondEnd = 0; |
| 23 | |
| 24 | // If this is a conditional temporary, we need to check the condition |
| 25 | // boolean and only call the destructor if it's true. |
| 26 | if (CondPtr) { |
| 27 | llvm::BasicBlock *CondBlock = CGF.createBasicBlock("temp.cond-dtor.call"); |
| 28 | CondEnd = CGF.createBasicBlock("temp.cond-dtor.cont"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 29 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 30 | llvm::Value *Cond = CGF.Builder.CreateLoad(CondPtr); |
| 31 | CGF.Builder.CreateCondBr(Cond, CondBlock, CondEnd); |
| 32 | CGF.EmitBlock(CondBlock); |
| 33 | } |
| 34 | |
| 35 | CGF.EmitCXXDestructorCall(Temporary->getDestructor(), |
| 36 | Dtor_Complete, /*ForVirtualBase=*/false, |
| 37 | Addr); |
| 38 | |
| 39 | if (CondPtr) { |
| 40 | // Reset the condition to false. |
| 41 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()), |
| 42 | CondPtr); |
| 43 | CGF.EmitBlock(CondEnd); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Emits all the code to cause the given temporary to be cleaned up. |
| 48 | void CodeGenFunction::EmitCXXTemporary(const CXXTemporary *Temporary, |
| 49 | llvm::Value *Ptr) { |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 50 | llvm::AllocaInst *CondPtr = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
| 52 | // Check if temporaries need to be conditional. If so, we'll create a |
| 53 | // condition boolean, initialize it to 0 and |
Anders Carlsson | a36bf8f | 2009-11-20 17:27:56 +0000 | [diff] [blame] | 54 | if (ConditionalBranchLevel != 0) { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 55 | CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 57 | // Initialize it to false. This initialization takes place right after |
| 58 | // the alloca insert point. |
John McCall | ac41816 | 2010-04-22 01:10:34 +0000 | [diff] [blame] | 59 | InitTempAlloca(CondPtr, llvm::ConstantInt::getFalse(VMContext)); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 60 | |
| 61 | // Now set it to true. |
Owen Anderson | 3b144ba | 2009-07-31 17:39:36 +0000 | [diff] [blame] | 62 | Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr); |
Anders Carlsson | 8c0b203 | 2009-06-04 02:47:33 +0000 | [diff] [blame] | 63 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 65 | CleanupBlock Cleanup(*this, NormalCleanup); |
| 66 | EmitTemporaryCleanup(*this, Temporary, Ptr, CondPtr); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 67 | |
| 68 | if (Exceptions) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 69 | Cleanup.beginEHCleanup(); |
| 70 | EmitTemporaryCleanup(*this, Temporary, Ptr, CondPtr); |
Mike Stump | f2945c0 | 2009-12-17 06:08:47 +0000 | [diff] [blame] | 71 | } |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Anders Carlsson | f4b8fea | 2009-06-03 19:05:16 +0000 | [diff] [blame] | 74 | RValue |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 75 | CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E, |
| 76 | llvm::Value *AggLoc, |
Anders Carlsson | 14c5cbf | 2009-08-16 07:36:22 +0000 | [diff] [blame] | 77 | bool IsAggLocVolatile, |
| 78 | bool IsInitializer) { |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 79 | RValue RV; |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 80 | { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 81 | RunCleanupsScope Scope(*this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Anders Carlsson | 44ec82b | 2010-03-30 03:14:41 +0000 | [diff] [blame] | 83 | RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile, |
| 84 | /*IgnoreResult=*/false, IsInitializer); |
| 85 | } |
Anders Carlsson | 2ce6612 | 2009-06-03 18:40:21 +0000 | [diff] [blame] | 86 | return RV; |
| 87 | } |
Anders Carlsson | 1d84750 | 2009-06-04 02:22:12 +0000 | [diff] [blame] | 88 | |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 89 | LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue( |
| 90 | const CXXExprWithTemporaries *E) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 91 | LValue LV; |
| 92 | { |
| 93 | RunCleanupsScope Scope(*this); |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 94 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 95 | LV = EmitLValue(E->getSubExpr()); |
| 96 | } |
Anders Carlsson | b9ea0b5 | 2009-09-14 01:10:45 +0000 | [diff] [blame] | 97 | return LV; |
| 98 | } |