blob: 002c77430f8b806b4077e3c031290dded610e09b [file] [log] [blame]
Chris Lattner566b6ce2007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattneraf6f5282007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattneraf6f5282007-08-10 20:13:28 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Aggregate Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Fariborz Jahanian082b02e2009-07-08 01:18:33 +000016#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonb14095a2009-04-17 00:06:03 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000020#include "llvm/Constants.h"
21#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000022#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000023#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000024#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000025using namespace clang;
26using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000027
Chris Lattner9c033562007-08-21 04:25:47 +000028//===----------------------------------------------------------------------===//
29// Aggregate Expression Emitter
30//===----------------------------------------------------------------------===//
31
32namespace {
33class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000036 llvm::Value *DestPtr;
37 bool VolatileDest;
Mike Stump49d1cd52009-05-26 22:03:21 +000038 bool IgnoreResult;
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000039 bool IsInitializer;
Chris Lattner9c033562007-08-21 04:25:47 +000040public:
Mike Stumpff4bf3b2009-05-27 01:42:21 +000041 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000042 bool ignore, bool isinit)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000043 : CGF(cgf), Builder(CGF.Builder),
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000044 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore),
45 IsInitializer(isinit) {
Chris Lattner9c033562007-08-21 04:25:47 +000046 }
47
Chris Lattneree755f92007-08-21 04:59:27 +000048 //===--------------------------------------------------------------------===//
49 // Utilities
50 //===--------------------------------------------------------------------===//
51
Chris Lattner9c033562007-08-21 04:25:47 +000052 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
53 /// represents a value lvalue, this method emits the address of the lvalue,
54 /// then loads the result into DestPtr.
55 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000056
Mike Stump4ac20dd2009-05-23 20:28:01 +000057 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +000058 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
59 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump4ac20dd2009-05-23 20:28:01 +000060
Chris Lattneree755f92007-08-21 04:59:27 +000061 //===--------------------------------------------------------------------===//
62 // Visitor Methods
63 //===--------------------------------------------------------------------===//
64
Chris Lattner9c033562007-08-21 04:25:47 +000065 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000066 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000067 }
68 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000069 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000070
71 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000072 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
73 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
74 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000075 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000076 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
77 EmitAggLoadOfLValue(E);
78 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000079 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
80 EmitAggLoadOfLValue(E);
81 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000082 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
83 EmitAggLoadOfLValue(E);
84 }
85 void VisitPredefinedExpr(const PredefinedExpr *E) {
86 EmitAggLoadOfLValue(E);
87 }
Douglas Gregor4c678342009-01-28 21:54:33 +000088
Chris Lattner9c033562007-08-21 04:25:47 +000089 // Operators.
Anders Carlsson4d8673b2009-08-07 23:22:37 +000090 void VisitCastExpr(CastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000091 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000092 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000093 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000094 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000095 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000096
Chris Lattner8fdf3282008-06-24 17:04:18 +000097 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000098 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
99 EmitAggLoadOfLValue(E);
100 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000101 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000102 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000103
104 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000105 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000106 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000107 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
108 Visit(DAE->getExpr());
109 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000110 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000111 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000112 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
113
Eli Friedmanb1851242008-05-27 15:51:49 +0000114 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000115
116 void EmitInitializationToLValue(Expr *E, LValue Address);
117 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000118 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000119
Chris Lattner9c033562007-08-21 04:25:47 +0000120};
121} // end anonymous namespace.
122
Chris Lattneree755f92007-08-21 04:59:27 +0000123//===----------------------------------------------------------------------===//
124// Utilities
125//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000126
Chris Lattner883f6a72007-08-11 00:04:45 +0000127/// EmitAggLoadOfLValue - Given an expression with aggregate type that
128/// represents a value lvalue, this method emits the address of the lvalue,
129/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000130void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
131 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000132 EmitFinalDestCopy(E, LV);
133}
134
135/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000136void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000137 assert(Src.isAggregate() && "value must be aggregate value!");
138
Chris Lattner883f6a72007-08-11 00:04:45 +0000139 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000140 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000141 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000142 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000143 // If the source is volatile, we must read from it; to do that, we need
144 // some place to put it.
145 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000146 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000147
Mike Stump4ac20dd2009-05-23 20:28:01 +0000148 // If the result of the assignment is used, copy the LHS there also.
149 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
150 // from the source as well, as we can't eliminate it if either operand
151 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000152 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
153 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000154}
155
156/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000157void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000158 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
159
160 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000161 Src.isVolatileQualified()),
162 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000163}
164
Chris Lattneree755f92007-08-21 04:59:27 +0000165//===----------------------------------------------------------------------===//
166// Visitor Methods
167//===----------------------------------------------------------------------===//
168
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000169void AggExprEmitter::VisitCastExpr(CastExpr *E) {
170 if (E->getCastKind() == CastExpr::CK_ToUnion) {
171 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000172 QualType PtrTy =
173 CGF.getContext().getPointerType(E->getSubExpr()->getType());
174 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
175 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000176 EmitInitializationToLValue(E->getSubExpr(),
177 LValue::MakeAddr(CastPtr, 0));
Nuno Lopes7e916272009-01-15 20:14:33 +0000178 return;
179 }
Fariborz Jahanian64e690e2009-08-26 23:31:30 +0000180 if (E->getCastKind() == CastExpr::CK_UserDefinedConversion) {
181 CXXFunctionalCastExpr *CXXFExpr = cast<CXXFunctionalCastExpr>(E);
182 CGF.EmitCXXFunctionalCastExpr(CXXFExpr);
183 return;
184 }
185
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000186 // FIXME: Remove the CK_Unknown check here.
187 assert((E->getCastKind() == CastExpr::CK_NoOp ||
188 E->getCastKind() == CastExpr::CK_Unknown) &&
189 "Only no-op casts allowed!");
Eli Friedman2dce5f82009-05-28 23:04:00 +0000190 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
191 E->getType()) &&
Chris Lattner96196622008-07-26 22:37:01 +0000192 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000193 Visit(E->getSubExpr());
194}
195
Chris Lattner96196622008-07-26 22:37:01 +0000196void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000197 if (E->getCallReturnType()->isReferenceType()) {
198 EmitAggLoadOfLValue(E);
199 return;
200 }
201
Anders Carlsson148fe672007-10-31 22:04:46 +0000202 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000203 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000204}
Chris Lattner96196622008-07-26 22:37:01 +0000205
206void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000207 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000208 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000209}
Anders Carlsson148fe672007-10-31 22:04:46 +0000210
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000211void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
212 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000213 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000214}
215
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000216void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
217 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000218 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000219 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000220}
221
Chris Lattner96196622008-07-26 22:37:01 +0000222void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000223 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000224 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
225 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000226}
227
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000228void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
229 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
230}
231
Chris Lattner9c033562007-08-21 04:25:47 +0000232void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000233 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000234}
235
Chris Lattner03d6fb92007-08-21 04:43:17 +0000236void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000237 // For an assignment to work, the value on the right has
238 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000239 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
240 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000241 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000242 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000243
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000244 // We have to special case property setters, otherwise we must have
245 // a simple lvalue (no aggregates inside vectors, bitfields).
246 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000247 llvm::Value *AggLoc = DestPtr;
248 if (!AggLoc)
249 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump240993d2009-05-23 23:48:13 +0000250 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000251 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000252 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000253 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000254 llvm::Value *AggLoc = DestPtr;
255 if (!AggLoc)
256 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpa49af1a2009-05-23 23:52:31 +0000257 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000258 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000259 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000260 } else {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000261 if (CGF.getContext().getLangOptions().NeXTRuntime) {
262 QualType LHSTy = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000263 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000264 if (FDTTy->getDecl()->hasObjectMember()) {
265 LValue RHS = CGF.EmitLValue(E->getRHS());
266 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, LHS.getAddress(),
267 RHS.getAddress(),
268 CGF.getContext().getTypeSize(LHSTy) / 8);
269 return;
270 }
271 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000272 // Codegen the RHS so that it stores directly into the LHS.
Mike Stumpfde64202009-05-23 04:13:59 +0000273 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stump49d1cd52009-05-26 22:03:21 +0000274 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000275 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000276}
277
Chris Lattner9c033562007-08-21 04:25:47 +0000278void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000279 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
280 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
281 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000282
Chris Lattner9c033562007-08-21 04:25:47 +0000283 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000284 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000285
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000286 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000287 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000288
289 // Handle the GNU extension for missing LHS.
290 assert(E->getLHS() && "Must have LHS for aggregate value");
291
Chris Lattnerc748f272007-08-21 05:02:10 +0000292 Visit(E->getLHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000293 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000294 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000295
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000296 CGF.PushConditionalTempDestruction();
Chris Lattner9c033562007-08-21 04:25:47 +0000297 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000298
Chris Lattnerc748f272007-08-21 05:02:10 +0000299 Visit(E->getRHS());
Anders Carlssonfb6fa302009-06-04 03:00:32 +0000300 CGF.PopConditionalTempDestruction();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000301 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000302
Chris Lattner9c033562007-08-21 04:25:47 +0000303 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000304}
Chris Lattneree755f92007-08-21 04:59:27 +0000305
Anders Carlssona294ca82009-07-08 18:33:14 +0000306void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
307 Visit(CE->getChosenSubExpr(CGF.getContext()));
308}
309
Eli Friedmanb1851242008-05-27 15:51:49 +0000310void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000311 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000312 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
313
Sebastian Redl0262f022009-01-09 21:09:38 +0000314 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000315 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000316 return;
317 }
318
Mike Stump4ac20dd2009-05-23 20:28:01 +0000319 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedmanb1851242008-05-27 15:51:49 +0000320}
321
Anders Carlssonb58d0172009-05-30 23:23:33 +0000322void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
323 llvm::Value *Val = DestPtr;
324
325 if (!Val) {
326 // Create a temporary variable.
327 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
328
329 // FIXME: volatile
330 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
331 } else
332 Visit(E->getSubExpr());
333
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000334 // Don't make this a live temporary if we're emitting an initializer expr.
335 if (!IsInitializer)
336 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000337}
338
Anders Carlssonb14095a2009-04-17 00:06:03 +0000339void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000340AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000341 llvm::Value *Val = DestPtr;
342
343 if (!Val) {
344 // Create a temporary variable.
345 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
346 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000347
Anders Carlssonb58d0172009-05-30 23:23:33 +0000348 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000349}
350
351void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000352 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000353}
354
Chris Lattnerf81557c2008-04-04 18:42:16 +0000355void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000356 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000357 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000358 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000359 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000360 } else if (E->getType()->isComplexType()) {
361 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000362 } else if (CGF.hasAggregateLLVMType(E->getType())) {
363 CGF.EmitAnyExpr(E, LV.getAddress(), false);
364 } else {
365 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000366 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000367}
368
369void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
370 if (!CGF.hasAggregateLLVMType(T)) {
371 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000372 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000373 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000374 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000375 // Otherwise, just memset the whole thing to zero. This is legal
376 // because in LLVM, all default initializers are guaranteed to have a
377 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000378 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379 // There's a potential optimization opportunity in combining
380 // memsets; that would be easy for arrays, but relatively
381 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000382 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000383 }
384}
385
Chris Lattnerf81557c2008-04-04 18:42:16 +0000386void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000387#if 0
388 // FIXME: Disabled while we figure out what to do about
389 // test/CodeGen/bitfield.c
390 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000391 // If we can, prefer a copy from a global; this is a lot less code for long
392 // globals, and it's easier for the current optimizers to analyze.
393 // FIXME: Should we really be doing this? Should we try to avoid cases where
394 // we emit a global with a lot of zeros? Should we try to avoid short
395 // globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000396 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000397 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
398 llvm::GlobalVariable* GV =
399 new llvm::GlobalVariable(C->getType(), true,
400 llvm::GlobalValue::InternalLinkage,
401 C, "", &CGF.CGM.getModule(), 0);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000402 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedman994ffef2008-11-30 02:11:09 +0000403 return;
404 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000405#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000406 if (E->hadArrayRangeDesignator()) {
407 CGF.ErrorUnsupported(E, "GNU array range designator extension");
408 }
409
Chris Lattnerf81557c2008-04-04 18:42:16 +0000410 // Handle initialization of an array.
411 if (E->getType()->isArrayType()) {
412 const llvm::PointerType *APType =
413 cast<llvm::PointerType>(DestPtr->getType());
414 const llvm::ArrayType *AType =
415 cast<llvm::ArrayType>(APType->getElementType());
416
417 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000418
Chris Lattner96196622008-07-26 22:37:01 +0000419 if (E->getNumInits() > 0) {
420 QualType T1 = E->getType();
421 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000422 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000423 EmitAggLoadOfLValue(E->getInit(0));
424 return;
425 }
Eli Friedman922696f2008-05-19 17:51:16 +0000426 }
427
Chris Lattnerf81557c2008-04-04 18:42:16 +0000428 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000429 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000430 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000431
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000432 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000433
Chris Lattnerf81557c2008-04-04 18:42:16 +0000434 for (uint64_t i = 0; i != NumArrayElements; ++i) {
435 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
436 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000437 EmitInitializationToLValue(E->getInit(i),
438 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000439 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000440 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000441 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000442 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000443 return;
444 }
445
446 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
447
448 // Do struct initialization; this code just sets each individual member
449 // to the approprate value. This makes bitfield support automatic;
450 // the disadvantage is that the generated code is more difficult for
451 // the optimizer, especially with bitfields.
452 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000453 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000454 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000455
456 if (E->getType()->isUnionType()) {
457 // Only initialize one field of a union. The field itself is
458 // specified by the initializer list.
459 if (!E->getInitializedFieldInUnion()) {
460 // Empty union; we have nothing to do.
461
462#ifndef NDEBUG
463 // Make sure that it's really an empty and not a failure of
464 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000465 for (RecordDecl::field_iterator Field = SD->field_begin(),
466 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000467 Field != FieldEnd; ++Field)
468 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
469#endif
470 return;
471 }
472
473 // FIXME: volatility
474 FieldDecl *Field = E->getInitializedFieldInUnion();
475 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
476
477 if (NumInitElements) {
478 // Store the initializer into the field
479 EmitInitializationToLValue(E->getInit(0), FieldLoc);
480 } else {
481 // Default-initialize to null
482 EmitNullInitializationToLValue(FieldLoc, Field->getType());
483 }
484
485 return;
486 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000487
488 // Here we iterate over the fields; this makes it simpler to both
489 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000490 for (RecordDecl::field_iterator Field = SD->field_begin(),
491 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000492 Field != FieldEnd; ++Field) {
493 // We're done once we hit the flexible array member
494 if (Field->getType()->isIncompleteArrayType())
495 break;
496
Douglas Gregor34e79462009-01-28 23:36:17 +0000497 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000498 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000499
Eli Friedman1e692ac2008-06-13 23:01:12 +0000500 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000501 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000502 // We never generate write-barries for initialized fields.
503 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000504 if (CurInitVal < NumInitElements) {
505 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000506 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
507 } else {
508 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000509 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000510 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000511 }
Devang Patel636c3d02007-10-26 17:44:44 +0000512}
513
Chris Lattneree755f92007-08-21 04:59:27 +0000514//===----------------------------------------------------------------------===//
515// Entry Points into this File
516//===----------------------------------------------------------------------===//
517
Mike Stumpe1129a92009-05-26 18:57:45 +0000518/// EmitAggExpr - Emit the computation of the specified expression of aggregate
519/// type. The result is computed into DestPtr. Note that if DestPtr is null,
520/// the value of the aggregate expression is not needed. If VolatileDest is
521/// true, DestPtr cannot be 0.
Chris Lattneree755f92007-08-21 04:59:27 +0000522void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000523 bool VolatileDest, bool IgnoreResult,
524 bool IsInitializer) {
Chris Lattneree755f92007-08-21 04:59:27 +0000525 assert(E && hasAggregateLLVMType(E->getType()) &&
526 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000527 assert ((DestPtr != 0 || VolatileDest == false)
528 && "volatile aggregate can't be 0");
Chris Lattneree755f92007-08-21 04:59:27 +0000529
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000530 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer)
Mike Stump49d1cd52009-05-26 22:03:21 +0000531 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000532}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000533
534void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
535 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
536
537 EmitMemSetToZero(DestPtr, Ty);
538}
539
540void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000541 llvm::Value *SrcPtr, QualType Ty,
542 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000543 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
544
Chris Lattner83c96292009-02-28 18:31:01 +0000545 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000546 // C99 6.5.16.1p3, which states "If the value being stored in an object is
547 // read from another object that overlaps in anyway the storage of the first
548 // object, then the overlap shall be exact and the two objects shall have
549 // qualified or unqualified versions of a compatible type."
550 //
Chris Lattner83c96292009-02-28 18:31:01 +0000551 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000552 // equal, but other compilers do this optimization, and almost every memcpy
553 // implementation handles this case safely. If there is a libc that does not
554 // safely handle this, we can add a target hook.
Owen Anderson0032b272009-08-13 21:57:51 +0000555 const llvm::Type *BP =
556 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000557 if (DestPtr->getType() != BP)
558 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
559 if (SrcPtr->getType() != BP)
560 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
561
562 // Get size and alignment info for this aggregate.
563 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
564
565 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000566 const llvm::Type *IntPtr =
567 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Daniel Dunbar7482d122008-09-09 20:49:46 +0000568
Mike Stumpfde64202009-05-23 04:13:59 +0000569 // FIXME: If we have a volatile struct, the optimizer can remove what might
570 // appear to be `extra' memory ops:
571 //
572 // volatile struct { int i; } a, b;
573 //
574 // int main() {
575 // a = b;
576 // a = b;
577 // }
578 //
Mike Stump49d1cd52009-05-26 22:03:21 +0000579 // we need to use a differnt call here. We use isVolatile to indicate when
580 // either the source or the destination is volatile.
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000581 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000582 DestPtr, SrcPtr,
583 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000584 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Owen Anderson0032b272009-08-13 21:57:51 +0000585 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000586 TypeInfo.second/8));
587}