blob: 177c6db3e8f0ccd79be7ebca96aa309a78da2d7f [file] [log] [blame]
Anders Carlsson2ce66122009-06-03 18:40:21 +00001//===--- CGCXXTemp.cpp - Emit LLVM Code for C++ temporaries ---------------===//
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"
15using namespace clang;
16using namespace CodeGen;
17
Mike Stump1eb44332009-09-09 15:08:12 +000018void CodeGenFunction::PushCXXTemporary(const CXXTemporary *Temporary,
Anders Carlsson2ce66122009-06-03 18:40:21 +000019 llvm::Value *Ptr) {
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000020 llvm::BasicBlock *DtorBlock = createBasicBlock("temp.dtor");
Mike Stump1eb44332009-09-09 15:08:12 +000021
Anders Carlsson8c0b2032009-06-04 02:47:33 +000022 llvm::Value *CondPtr = 0;
Mike Stump1eb44332009-09-09 15:08:12 +000023
24 // Check if temporaries need to be conditional. If so, we'll create a
25 // condition boolean, initialize it to 0 and
Anders Carlsson8c0b2032009-06-04 02:47:33 +000026 if (!ConditionalTempDestructionStack.empty()) {
Owen Anderson0032b272009-08-13 21:57:51 +000027 CondPtr = CreateTempAlloca(llvm::Type::getInt1Ty(VMContext), "cond");
Mike Stump1eb44332009-09-09 15:08:12 +000028
Anders Carlsson8c0b2032009-06-04 02:47:33 +000029 // Initialize it to false. This initialization takes place right after
30 // the alloca insert point.
Mike Stump1eb44332009-09-09 15:08:12 +000031 llvm::StoreInst *SI =
Owen Anderson3b144ba2009-07-31 17:39:36 +000032 new llvm::StoreInst(llvm::ConstantInt::getFalse(VMContext), CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000033 llvm::BasicBlock *Block = AllocaInsertPt->getParent();
34 Block->getInstList().insertAfter((llvm::Instruction *)AllocaInsertPt, SI);
35
36 // Now set it to true.
Owen Anderson3b144ba2009-07-31 17:39:36 +000037 Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000038 }
Mike Stump1eb44332009-09-09 15:08:12 +000039
40 LiveTemporaries.push_back(CXXLiveTemporaryInfo(Temporary, Ptr, DtorBlock,
Anders Carlsson8c0b2032009-06-04 02:47:33 +000041 CondPtr));
Anders Carlssond8bc5a92009-06-04 02:08:08 +000042
43 PushCleanupBlock(DtorBlock);
Anders Carlsson2ce66122009-06-03 18:40:21 +000044}
45
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000046void CodeGenFunction::PopCXXTemporary() {
47 const CXXLiveTemporaryInfo& Info = LiveTemporaries.back();
Mike Stump1eb44332009-09-09 15:08:12 +000048
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000049 CleanupBlockInfo CleanupInfo = PopCleanupBlock();
Mike Stump1eb44332009-09-09 15:08:12 +000050 assert(CleanupInfo.CleanupBlock == Info.DtorBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000051 "Cleanup block mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +000052 assert(!CleanupInfo.SwitchBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000053 "Should not have a switch block for temporary cleanup!");
Mike Stump1eb44332009-09-09 15:08:12 +000054 assert(!CleanupInfo.EndBlock &&
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000055 "Should not have an end block for temporary cleanup!");
Mike Stump1eb44332009-09-09 15:08:12 +000056
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000057 EmitBlock(Info.DtorBlock);
58
Anders Carlsson8c0b2032009-06-04 02:47:33 +000059 llvm::BasicBlock *CondEnd = 0;
60
61 // If this is a conditional temporary, we need to check the condition
62 // boolean and only call the destructor if it's true.
63 if (Info.CondPtr) {
64 llvm::BasicBlock *CondBlock = createBasicBlock("cond.dtor.call");
65 CondEnd = createBasicBlock("cond.dtor.end");
Mike Stump1eb44332009-09-09 15:08:12 +000066
Anders Carlsson8c0b2032009-06-04 02:47:33 +000067 llvm::Value *Cond = Builder.CreateLoad(Info.CondPtr);
68 Builder.CreateCondBr(Cond, CondBlock, CondEnd);
69 EmitBlock(CondBlock);
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000072 EmitCXXDestructorCall(Info.Temporary->getDestructor(),
73 Dtor_Complete, Info.ThisPtr);
74
Anders Carlsson8c0b2032009-06-04 02:47:33 +000075 if (CondEnd) {
76 // Reset the condition. to false.
Owen Anderson3b144ba2009-07-31 17:39:36 +000077 Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), Info.CondPtr);
Anders Carlsson8c0b2032009-06-04 02:47:33 +000078 EmitBlock(CondEnd);
79 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Anders Carlssonf4b8fea2009-06-03 19:05:16 +000081 LiveTemporaries.pop_back();
82}
83
84RValue
Anders Carlsson2ce66122009-06-03 18:40:21 +000085CodeGenFunction::EmitCXXExprWithTemporaries(const CXXExprWithTemporaries *E,
86 llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000087 bool IsAggLocVolatile,
88 bool IsInitializer) {
Anders Carlssonf54741e2009-06-16 03:37:31 +000089 // If we shouldn't destroy the temporaries, just emit the
90 // child expression.
91 if (!E->shouldDestroyTemporaries())
Mike Stump1eb44332009-09-09 15:08:12 +000092 return EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000093 /*IgnoreResult=*/false, IsInitializer);
Anders Carlssonf54741e2009-06-16 03:37:31 +000094
Anders Carlsson2ce66122009-06-03 18:40:21 +000095 // Keep track of the current cleanup stack depth.
96 size_t CleanupStackDepth = CleanupEntries.size();
Daniel Dunbarb4d4c4b2009-06-05 02:03:25 +000097 (void) CleanupStackDepth;
Anders Carlsson2ce66122009-06-03 18:40:21 +000098
99 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000100
101 RValue RV = EmitAnyExpr(E->getSubExpr(), AggLoc, IsAggLocVolatile,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000102 /*IgnoreResult=*/false, IsInitializer);
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000104 // Pop temporaries.
105 while (LiveTemporaries.size() > OldNumLiveTemporaries)
106 PopCXXTemporary();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000108 assert(CleanupEntries.size() == CleanupStackDepth &&
109 "Cleanup size mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Anders Carlsson2ce66122009-06-03 18:40:21 +0000111 return RV;
112}
Anders Carlsson1d847502009-06-04 02:22:12 +0000113
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000114LValue CodeGenFunction::EmitCXXExprWithTemporariesLValue(
115 const CXXExprWithTemporaries *E) {
116 // If we shouldn't destroy the temporaries, just emit the
117 // child expression.
118 if (!E->shouldDestroyTemporaries())
119 return EmitLValue(E->getSubExpr());
120
121 // Keep track of the current cleanup stack depth.
122 size_t CleanupStackDepth = CleanupEntries.size();
123 (void) CleanupStackDepth;
124
125 unsigned OldNumLiveTemporaries = LiveTemporaries.size();
126
127 LValue LV = EmitLValue(E->getSubExpr());
128
129 // Pop temporaries.
130 while (LiveTemporaries.size() > OldNumLiveTemporaries)
131 PopCXXTemporary();
132
133 assert(CleanupEntries.size() == CleanupStackDepth &&
134 "Cleanup size mismatch!");
135
136 return LV;
137}
138
Mike Stump1eb44332009-09-09 15:08:12 +0000139void
Anders Carlsson1d847502009-06-04 02:22:12 +0000140CodeGenFunction::PushConditionalTempDestruction() {
141 // Store the current number of live temporaries.
142 ConditionalTempDestructionStack.push_back(LiveTemporaries.size());
143}
144
145void CodeGenFunction::PopConditionalTempDestruction() {
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000146 size_t NumLiveTemporaries = ConditionalTempDestructionStack.back();
147 ConditionalTempDestructionStack.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000149 // Pop temporaries.
150 while (LiveTemporaries.size() > NumLiveTemporaries) {
Mike Stump1eb44332009-09-09 15:08:12 +0000151 assert(LiveTemporaries.back().CondPtr &&
Daniel Dunbarb4d4c4b2009-06-05 02:03:25 +0000152 "Conditional temporary must have a cond ptr!");
Anders Carlsson8c0b2032009-06-04 02:47:33 +0000153
154 PopCXXTemporary();
Mike Stump1eb44332009-09-09 15:08:12 +0000155 }
Anders Carlsson1d847502009-06-04 02:22:12 +0000156}
Mike Stump1eb44332009-09-09 15:08:12 +0000157