blob: 3bc8c1297462ba5b0b189de0df19ab5a9646ba31 [file] [log] [blame]
Chris Lattner11e0de52007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattnerd79671f2007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerd79671f2007-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 Lattner6278e6a2007-08-11 00:04:45 +000015#include "CodeGenModule.h"
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +000016#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
Anders Carlssonb7f8f592009-04-17 00:06:03 +000018#include "clang/AST/DeclCXX.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000019#include "clang/AST/StmtVisitor.h"
Chris Lattner6278e6a2007-08-11 00:04:45 +000020#include "llvm/Constants.h"
21#include "llvm/Function.h"
Devang Patel87174172007-10-26 17:44:44 +000022#include "llvm/GlobalVariable.h"
Chris Lattner4758b402007-08-21 04:25:47 +000023#include "llvm/Support/Compiler.h"
Chris Lattner579a05d2008-04-04 18:42:16 +000024#include "llvm/Intrinsics.h"
Chris Lattnerd79671f2007-08-10 20:13:28 +000025using namespace clang;
26using namespace CodeGen;
Chris Lattner6278e6a2007-08-11 00:04:45 +000027
Chris Lattner4758b402007-08-21 04:25:47 +000028//===----------------------------------------------------------------------===//
29// Aggregate Expression Emitter
30//===----------------------------------------------------------------------===//
31
32namespace {
33class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
34 CodeGenFunction &CGF;
Daniel Dunbarcb463852008-11-01 01:53:16 +000035 CGBuilderTy &Builder;
Chris Lattner4758b402007-08-21 04:25:47 +000036 llvm::Value *DestPtr;
37 bool VolatileDest;
Mike Stumpec3cbfe2009-05-26 22:03:21 +000038 bool IgnoreResult;
Anders Carlsson5b106a72009-08-16 07:36:22 +000039 bool IsInitializer;
Fariborz Jahanian879d7262009-08-31 19:33:16 +000040 bool RequiresGCollection;
Chris Lattner4758b402007-08-21 04:25:47 +000041public:
Mike Stump3e97f3b2009-05-27 01:42:21 +000042 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
Fariborz Jahanian879d7262009-08-31 19:33:16 +000043 bool ignore, bool isinit, bool requiresGCollection)
Chris Lattnerbda69f82007-08-26 23:13:56 +000044 : CGF(cgf), Builder(CGF.Builder),
Anders Carlsson5b106a72009-08-16 07:36:22 +000045 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore),
Fariborz Jahanian879d7262009-08-31 19:33:16 +000046 IsInitializer(isinit), RequiresGCollection(requiresGCollection) {
Chris Lattner4758b402007-08-21 04:25:47 +000047 }
48
Chris Lattner835635d2007-08-21 04:59:27 +000049 //===--------------------------------------------------------------------===//
50 // Utilities
51 //===--------------------------------------------------------------------===//
52
Chris Lattner4758b402007-08-21 04:25:47 +000053 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
54 /// represents a value lvalue, this method emits the address of the lvalue,
55 /// then loads the result into DestPtr.
56 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedmanf23b6fa2008-05-19 17:51:16 +000057
Mike Stumpca9fc092009-05-23 20:28:01 +000058 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +000059 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
60 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stumpca9fc092009-05-23 20:28:01 +000061
Chris Lattner835635d2007-08-21 04:59:27 +000062 //===--------------------------------------------------------------------===//
63 // Visitor Methods
64 //===--------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattner4758b402007-08-21 04:25:47 +000066 void VisitStmt(Stmt *S) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +000067 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner4758b402007-08-21 04:25:47 +000068 }
69 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman3f66b842009-01-27 09:03:41 +000070 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner4758b402007-08-21 04:25:47 +000071
72 // l-values.
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000073 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
74 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
75 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon6f1b2742007-12-23 03:11:58 +000076 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattner2f343dd2009-04-21 23:00:09 +000077 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +000078 EmitAggLoadOfLValue(E);
Chris Lattner2f343dd2009-04-21 23:00:09 +000079 }
Seo Sanghyeond4d8c3c2007-12-14 02:04:12 +000080 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
Chris Lattner2f343dd2009-04-21 23:00:09 +000083 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +000084 EmitAggLoadOfLValue(E);
Chris Lattner2f343dd2009-04-21 23:00:09 +000085 }
86 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump11289f42009-09-09 15:08:12 +000087 EmitAggLoadOfLValue(E);
Chris Lattner2f343dd2009-04-21 23:00:09 +000088 }
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattner4758b402007-08-21 04:25:47 +000090 // Operators.
Anders Carlssonec143772009-08-07 23:22:37 +000091 void VisitCastExpr(CastExpr *E);
Anders Carlsson0370eb22007-10-31 22:04:46 +000092 void VisitCallExpr(const CallExpr *E);
Chris Lattner49e3bfa2007-08-31 22:54:14 +000093 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner4758b402007-08-21 04:25:47 +000094 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattnercd9fb242007-08-21 04:43:17 +000095 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman4b0e2a32008-05-20 07:56:31 +000096 void VisitBinComma(const BinaryOperator *E);
Chris Lattner4758b402007-08-21 04:25:47 +000097
Chris Lattnerb1d329d2008-06-24 17:04:18 +000098 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbarc8317a42008-08-23 10:51:21 +000099 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
100 EmitAggLoadOfLValue(E);
101 }
Daniel Dunbar55310df2008-08-27 06:57:25 +0000102 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000103 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner4758b402007-08-21 04:25:47 +0000105 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlsson5b2095c2009-07-08 18:33:14 +0000106 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel87174172007-10-26 17:44:44 +0000107 void VisitInitListExpr(InitListExpr *E);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000108 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
109 Visit(DAE->getExpr());
110 }
Anders Carlsson3be22e22009-05-30 23:23:33 +0000111 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson1619a5042009-05-03 17:47:16 +0000112 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000113 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
114
Eli Friedman21911e82008-05-27 15:51:49 +0000115 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattner579a05d2008-04-04 18:42:16 +0000116
117 void EmitInitializationToLValue(Expr *E, LValue Address);
118 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner4758b402007-08-21 04:25:47 +0000119 // case Expr::ChooseExprClass:
Lauro Ramos Venanciodec89732008-02-18 22:44:02 +0000120
Chris Lattner4758b402007-08-21 04:25:47 +0000121};
122} // end anonymous namespace.
123
Chris Lattner835635d2007-08-21 04:59:27 +0000124//===----------------------------------------------------------------------===//
125// Utilities
126//===----------------------------------------------------------------------===//
Chris Lattner4758b402007-08-21 04:25:47 +0000127
Chris Lattner6278e6a2007-08-11 00:04:45 +0000128/// EmitAggLoadOfLValue - Given an expression with aggregate type that
129/// represents a value lvalue, this method emits the address of the lvalue,
130/// then loads the result into DestPtr.
Chris Lattner4758b402007-08-21 04:25:47 +0000131void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
132 LValue LV = CGF.EmitLValue(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000133 EmitFinalDestCopy(E, LV);
134}
135
136/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000137void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stumpca9fc092009-05-23 20:28:01 +0000138 assert(Src.isAggregate() && "value must be aggregate value!");
139
Chris Lattner6278e6a2007-08-11 00:04:45 +0000140 // If the result is ignored, don't copy from the value.
Mike Stump332ec2c2009-05-23 22:01:27 +0000141 if (DestPtr == 0) {
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000142 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump332ec2c2009-05-23 22:01:27 +0000143 return;
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000144 // If the source is volatile, we must read from it; to do that, we need
145 // some place to put it.
146 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump332ec2c2009-05-23 22:01:27 +0000147 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000148
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000149 if (RequiresGCollection) {
150 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
151 DestPtr, Src.getAggregateAddr(),
152 E->getType());
153 return;
154 }
Mike Stumpca9fc092009-05-23 20:28:01 +0000155 // If the result of the assignment is used, copy the LHS there also.
156 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
157 // from the source as well, as we can't eliminate it if either operand
158 // is volatile, unless copy has volatile for both source and destination..
Mike Stump5e9e61b2009-05-23 22:29:41 +0000159 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
160 VolatileDest|Src.isVolatileQualified());
Mike Stumpca9fc092009-05-23 20:28:01 +0000161}
162
163/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000164void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stumpca9fc092009-05-23 20:28:01 +0000165 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
166
167 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000168 Src.isVolatileQualified()),
169 Ignore);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000170}
171
Chris Lattner835635d2007-08-21 04:59:27 +0000172//===----------------------------------------------------------------------===//
173// Visitor Methods
174//===----------------------------------------------------------------------===//
175
Anders Carlssonec143772009-08-07 23:22:37 +0000176void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Anders Carlsson1fb7ae92009-09-29 01:23:39 +0000177 switch (E->getCastKind()) {
178 default: assert(0 && "Unhandled cast kind!");
179
180 case CastExpr::CK_ToUnion: {
Anders Carlssonec143772009-08-07 23:22:37 +0000181 // GCC union extension
Eli Friedmandd274842009-06-03 20:45:06 +0000182 QualType PtrTy =
Anders Carlsson1fb7ae92009-09-29 01:23:39 +0000183 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedmandd274842009-06-03 20:45:06 +0000184 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
185 CGF.ConvertType(PtrTy));
Mon P Wangacedf772009-07-22 03:08:17 +0000186 EmitInitializationToLValue(E->getSubExpr(),
John McCall8ccfcb52009-09-24 19:53:00 +0000187 LValue::MakeAddr(CastPtr, Qualifiers()));
Anders Carlsson1fb7ae92009-09-29 01:23:39 +0000188 break;
Nuno Lopes7ffcf932009-01-15 20:14:33 +0000189 }
Mike Stump11289f42009-09-09 15:08:12 +0000190
Anders Carlssonec143772009-08-07 23:22:37 +0000191 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson1fb7ae92009-09-29 01:23:39 +0000192 case CastExpr::CK_Unknown:
193 case CastExpr::CK_NoOp:
194 case CastExpr::CK_UserDefinedConversion:
195 case CastExpr::CK_ConstructorConversion:
196 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
197 E->getType()) &&
198 "Implicit cast types must be compatible");
199 Visit(E->getSubExpr());
200 break;
Anders Carlssonb05a3e52009-09-29 02:09:01 +0000201
202 case CastExpr::CK_NullToMemberPointer: {
203 QualType T = E->getType();
204 const llvm::Type *PtrDiffTy =
205 CGF.ConvertType(CGF.getContext().getPointerDiffType());
206
207 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
208 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
209 Builder.CreateStore(NullValue, Ptr, VolatileDest);
210
211 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
212 Builder.CreateStore(NullValue, Adj, VolatileDest);
213
214 break;
215 }
Anders Carlsson1fb7ae92009-09-29 01:23:39 +0000216 }
Anders Carlsson1ba25ca2008-01-14 06:28:57 +0000217}
218
Chris Lattner0f398c42008-07-26 22:37:01 +0000219void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssonddcbfe72009-05-27 16:45:02 +0000220 if (E->getCallReturnType()->isReferenceType()) {
221 EmitAggLoadOfLValue(E);
222 return;
223 }
Mike Stump11289f42009-09-09 15:08:12 +0000224
Anders Carlsson0370eb22007-10-31 22:04:46 +0000225 RValue RV = CGF.EmitCallExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000226 EmitFinalDestCopy(E, RV);
Anders Carlsson0370eb22007-10-31 22:04:46 +0000227}
Chris Lattner0f398c42008-07-26 22:37:01 +0000228
229void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar97db84c2008-08-23 03:46:30 +0000230 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000231 EmitFinalDestCopy(E, RV);
Chris Lattnerb1d329d2008-06-24 17:04:18 +0000232}
Anders Carlsson0370eb22007-10-31 22:04:46 +0000233
Daniel Dunbar55310df2008-08-27 06:57:25 +0000234void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
235 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000236 EmitFinalDestCopy(E, RV);
Daniel Dunbar55310df2008-08-27 06:57:25 +0000237}
238
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000239void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
240 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000241 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stumpca9fc092009-05-23 20:28:01 +0000242 EmitFinalDestCopy(E, RV);
Fariborz Jahanian8a1810f2008-11-22 18:39:36 +0000243}
244
Chris Lattner0f398c42008-07-26 22:37:01 +0000245void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000246 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson5b106a72009-08-16 07:36:22 +0000247 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
248 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman4b0e2a32008-05-20 07:56:31 +0000249}
250
Chris Lattner49e3bfa2007-08-31 22:54:14 +0000251void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
252 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
253}
254
Chris Lattner4758b402007-08-21 04:25:47 +0000255void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbara7c8cf62008-08-16 00:56:44 +0000256 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattner835635d2007-08-21 04:59:27 +0000257}
258
Chris Lattnercd9fb242007-08-21 04:43:17 +0000259void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000260 // For an assignment to work, the value on the right has
261 // to be compatible with the value on the left.
Eli Friedman2a695472009-05-28 23:04:00 +0000262 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
263 E->getRHS()->getType())
Eli Friedmanf54c4e52008-02-11 01:09:17 +0000264 && "Invalid assignment");
Chris Lattner4758b402007-08-21 04:25:47 +0000265 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner6278e6a2007-08-11 00:04:45 +0000266
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000267 // We have to special case property setters, otherwise we must have
268 // a simple lvalue (no aggregates inside vectors, bitfields).
269 if (LHS.isPropertyRef()) {
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000270 llvm::Value *AggLoc = DestPtr;
271 if (!AggLoc)
272 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump9afc476d2009-05-23 23:48:13 +0000273 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump11289f42009-09-09 15:08:12 +0000274 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump9afc476d2009-05-23 23:48:13 +0000275 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stump658fe022009-07-30 22:28:39 +0000276 } else if (LHS.isKVCRef()) {
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000277 llvm::Value *AggLoc = DestPtr;
278 if (!AggLoc)
279 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpb9f25182009-05-23 23:52:31 +0000280 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump11289f42009-09-09 15:08:12 +0000281 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpb9f25182009-05-23 23:52:31 +0000282 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000283 } else {
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000284 bool RequiresGCollection = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000285 if (CGF.getContext().getLangOptions().NeXTRuntime) {
286 QualType LHSTy = E->getLHS()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000287 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Mike Stump11289f42009-09-09 15:08:12 +0000288 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000289 }
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000290 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000291 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
292 false, false, RequiresGCollection);
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000293 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +0000294 }
Chris Lattner6278e6a2007-08-11 00:04:45 +0000295}
296
Chris Lattner4758b402007-08-21 04:25:47 +0000297void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbara612e792008-11-13 01:38:36 +0000298 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
299 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
300 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump11289f42009-09-09 15:08:12 +0000301
Chris Lattner4758b402007-08-21 04:25:47 +0000302 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbda69f82007-08-26 23:13:56 +0000303 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000304
Anders Carlsson43c52cd2009-06-04 03:00:32 +0000305 CGF.PushConditionalTempDestruction();
Chris Lattner4758b402007-08-21 04:25:47 +0000306 CGF.EmitBlock(LHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000307
Chris Lattner6278e6a2007-08-11 00:04:45 +0000308 // Handle the GNU extension for missing LHS.
309 assert(E->getLHS() && "Must have LHS for aggregate value");
310
Chris Lattner926792f2007-08-21 05:02:10 +0000311 Visit(E->getLHS());
Anders Carlsson43c52cd2009-06-04 03:00:32 +0000312 CGF.PopConditionalTempDestruction();
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000313 CGF.EmitBranch(ContBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000314
Anders Carlsson43c52cd2009-06-04 03:00:32 +0000315 CGF.PushConditionalTempDestruction();
Chris Lattner4758b402007-08-21 04:25:47 +0000316 CGF.EmitBlock(RHSBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000317
Chris Lattner926792f2007-08-21 05:02:10 +0000318 Visit(E->getRHS());
Anders Carlsson43c52cd2009-06-04 03:00:32 +0000319 CGF.PopConditionalTempDestruction();
Daniel Dunbarc56e6762008-11-11 09:41:28 +0000320 CGF.EmitBranch(ContBlock);
Mike Stump11289f42009-09-09 15:08:12 +0000321
Chris Lattner4758b402007-08-21 04:25:47 +0000322 CGF.EmitBlock(ContBlock);
Chris Lattner6278e6a2007-08-11 00:04:45 +0000323}
Chris Lattner835635d2007-08-21 04:59:27 +0000324
Anders Carlsson5b2095c2009-07-08 18:33:14 +0000325void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
326 Visit(CE->getChosenSubExpr(CGF.getContext()));
327}
328
Eli Friedman21911e82008-05-27 15:51:49 +0000329void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbare9fcadd22009-02-11 22:25:55 +0000330 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000331 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
332
Sebastian Redl020cddc2009-01-09 21:09:38 +0000333 if (!ArgPtr) {
Anders Carlsson13abd7e2008-11-04 05:30:00 +0000334 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl020cddc2009-01-09 21:09:38 +0000335 return;
336 }
337
John McCall8ccfcb52009-09-24 19:53:00 +0000338 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedman21911e82008-05-27 15:51:49 +0000339}
340
Anders Carlsson3be22e22009-05-30 23:23:33 +0000341void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
342 llvm::Value *Val = DestPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000343
Anders Carlsson3be22e22009-05-30 23:23:33 +0000344 if (!Val) {
345 // Create a temporary variable.
346 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
347
348 // FIXME: volatile
349 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump11289f42009-09-09 15:08:12 +0000350 } else
Anders Carlsson3be22e22009-05-30 23:23:33 +0000351 Visit(E->getSubExpr());
Mike Stump11289f42009-09-09 15:08:12 +0000352
Anders Carlsson5b106a72009-08-16 07:36:22 +0000353 // Don't make this a live temporary if we're emitting an initializer expr.
354 if (!IsInitializer)
355 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlsson3be22e22009-05-30 23:23:33 +0000356}
357
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000358void
Anders Carlsson1619a5042009-05-03 17:47:16 +0000359AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlsson3be22e22009-05-30 23:23:33 +0000360 llvm::Value *Val = DestPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000361
Anders Carlsson3be22e22009-05-30 23:23:33 +0000362 if (!Val) {
363 // Create a temporary variable.
364 Val = CGF.CreateTempAlloca(CGF.ConvertTypeForMem(E->getType()), "tmp");
365 }
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000366
Anders Carlsson3be22e22009-05-30 23:23:33 +0000367 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlssonc82b86d2009-05-19 04:48:36 +0000368}
369
370void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Anders Carlsson5b106a72009-08-16 07:36:22 +0000371 CGF.EmitCXXExprWithTemporaries(E, DestPtr, VolatileDest, IsInitializer);
Anders Carlssonb7f8f592009-04-17 00:06:03 +0000372}
373
Chris Lattner579a05d2008-04-04 18:42:16 +0000374void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
Mike Stumpdf0fe272009-05-29 15:46:01 +0000375 // FIXME: Ignore result?
Chris Lattner579a05d2008-04-04 18:42:16 +0000376 // FIXME: Are initializers affected by volatile?
Douglas Gregor0202cb42009-01-29 17:44:32 +0000377 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000378 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor0202cb42009-01-29 17:44:32 +0000379 } else if (E->getType()->isComplexType()) {
380 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedman6e313212008-05-12 15:06:05 +0000381 } else if (CGF.hasAggregateLLVMType(E->getType())) {
382 CGF.EmitAnyExpr(E, LV.getAddress(), false);
383 } else {
384 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000385 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000386}
387
388void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
389 if (!CGF.hasAggregateLLVMType(T)) {
390 // For non-aggregates, we can store zero
Owen Anderson0b75f232009-07-31 20:28:54 +0000391 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbare8bdce42008-08-06 05:32:55 +0000392 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000393 } else {
Chris Lattner579a05d2008-04-04 18:42:16 +0000394 // Otherwise, just memset the whole thing to zero. This is legal
395 // because in LLVM, all default initializers are guaranteed to have a
396 // bit pattern of all zeros.
Eli Friedman20bb5e02009-04-13 21:47:26 +0000397 // FIXME: That isn't true for member pointers!
Chris Lattner579a05d2008-04-04 18:42:16 +0000398 // There's a potential optimization opportunity in combining
399 // memsets; that would be easy for arrays, but relatively
400 // difficult for structures with the current code.
Eli Friedman9127aa12009-03-28 03:10:45 +0000401 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattner579a05d2008-04-04 18:42:16 +0000402 }
403}
404
Chris Lattner579a05d2008-04-04 18:42:16 +0000405void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000406#if 0
Mike Stump11289f42009-09-09 15:08:12 +0000407 // FIXME: Disabled while we figure out what to do about
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000408 // test/CodeGen/bitfield.c
409 //
Mike Stump18bb9282009-05-16 07:57:57 +0000410 // If we can, prefer a copy from a global; this is a lot less code for long
411 // globals, and it's easier for the current optimizers to analyze.
412 // FIXME: Should we really be doing this? Should we try to avoid cases where
413 // we emit a global with a lot of zeros? Should we try to avoid short
414 // globals?
Eli Friedman7139af42009-01-25 02:32:41 +0000415 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedmanc59bb482008-11-30 02:11:09 +0000416 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
417 llvm::GlobalVariable* GV =
418 new llvm::GlobalVariable(C->getType(), true,
419 llvm::GlobalValue::InternalLinkage,
420 C, "", &CGF.CGM.getModule(), 0);
Mike Stumpca9fc092009-05-23 20:28:01 +0000421 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedmanc59bb482008-11-30 02:11:09 +0000422 return;
423 }
Eli Friedmanf5d08c92008-12-02 01:17:45 +0000424#endif
Douglas Gregorbf7207a2009-01-29 19:42:23 +0000425 if (E->hadArrayRangeDesignator()) {
426 CGF.ErrorUnsupported(E, "GNU array range designator extension");
427 }
428
Chris Lattner579a05d2008-04-04 18:42:16 +0000429 // Handle initialization of an array.
430 if (E->getType()->isArrayType()) {
431 const llvm::PointerType *APType =
432 cast<llvm::PointerType>(DestPtr->getType());
433 const llvm::ArrayType *AType =
434 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattner579a05d2008-04-04 18:42:16 +0000436 uint64_t NumInitElements = E->getNumInits();
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000437
Chris Lattner0f398c42008-07-26 22:37:01 +0000438 if (E->getNumInits() > 0) {
439 QualType T1 = E->getType();
440 QualType T2 = E->getInit(0)->getType();
Eli Friedman2a695472009-05-28 23:04:00 +0000441 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner0f398c42008-07-26 22:37:01 +0000442 EmitAggLoadOfLValue(E->getInit(0));
443 return;
444 }
Eli Friedmanf23b6fa2008-05-19 17:51:16 +0000445 }
446
Chris Lattner579a05d2008-04-04 18:42:16 +0000447 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattner7adf0762008-08-04 07:31:14 +0000448 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor347f7ea2009-01-28 21:54:33 +0000449 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +0000450
John McCall8ccfcb52009-09-24 19:53:00 +0000451 // FIXME: were we intentionally ignoring address spaces and GC attributes?
452 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman327944b2008-06-13 23:01:12 +0000453
Chris Lattner579a05d2008-04-04 18:42:16 +0000454 for (uint64_t i = 0; i != NumArrayElements; ++i) {
455 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
456 if (i < NumInitElements)
Eli Friedman327944b2008-06-13 23:01:12 +0000457 EmitInitializationToLValue(E->getInit(i),
John McCall8ccfcb52009-09-24 19:53:00 +0000458 LValue::MakeAddr(NextVal, Quals));
Chris Lattner579a05d2008-04-04 18:42:16 +0000459 else
John McCall8ccfcb52009-09-24 19:53:00 +0000460 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattner579a05d2008-04-04 18:42:16 +0000461 ElementType);
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000462 }
Chris Lattner579a05d2008-04-04 18:42:16 +0000463 return;
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Chris Lattner579a05d2008-04-04 18:42:16 +0000466 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner579a05d2008-04-04 18:42:16 +0000468 // Do struct initialization; this code just sets each individual member
469 // to the approprate value. This makes bitfield support automatic;
470 // the disadvantage is that the generated code is more difficult for
471 // the optimizer, especially with bitfields.
472 unsigned NumInitElements = E->getNumInits();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000473 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattner579a05d2008-04-04 18:42:16 +0000474 unsigned CurInitVal = 0;
Douglas Gregor51695702009-01-29 16:53:55 +0000475
476 if (E->getType()->isUnionType()) {
477 // Only initialize one field of a union. The field itself is
478 // specified by the initializer list.
479 if (!E->getInitializedFieldInUnion()) {
480 // Empty union; we have nothing to do.
Mike Stump11289f42009-09-09 15:08:12 +0000481
Douglas Gregor51695702009-01-29 16:53:55 +0000482#ifndef NDEBUG
483 // Make sure that it's really an empty and not a failure of
484 // semantic analysis.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000485 for (RecordDecl::field_iterator Field = SD->field_begin(),
486 FieldEnd = SD->field_end();
Douglas Gregor51695702009-01-29 16:53:55 +0000487 Field != FieldEnd; ++Field)
488 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
489#endif
490 return;
491 }
492
493 // FIXME: volatility
494 FieldDecl *Field = E->getInitializedFieldInUnion();
495 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
496
497 if (NumInitElements) {
498 // Store the initializer into the field
499 EmitInitializationToLValue(E->getInit(0), FieldLoc);
500 } else {
501 // Default-initialize to null
502 EmitNullInitializationToLValue(FieldLoc, Field->getType());
503 }
504
505 return;
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
Chris Lattner579a05d2008-04-04 18:42:16 +0000508 // Here we iterate over the fields; this makes it simpler to both
509 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000510 for (RecordDecl::field_iterator Field = SD->field_begin(),
511 FieldEnd = SD->field_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000512 Field != FieldEnd; ++Field) {
513 // We're done once we hit the flexible array member
514 if (Field->getType()->isIncompleteArrayType())
515 break;
516
Douglas Gregor17bd0942009-01-28 23:36:17 +0000517 if (Field->isUnnamedBitfield())
Chris Lattner579a05d2008-04-04 18:42:16 +0000518 continue;
Douglas Gregor17bd0942009-01-28 23:36:17 +0000519
Eli Friedman327944b2008-06-13 23:01:12 +0000520 // FIXME: volatility
Douglas Gregor51695702009-01-29 16:53:55 +0000521 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Fariborz Jahanian7c1baf42009-05-27 19:54:11 +0000522 // We never generate write-barries for initialized fields.
523 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattner579a05d2008-04-04 18:42:16 +0000524 if (CurInitVal < NumInitElements) {
525 // Store the initializer into the field
Chris Lattner579a05d2008-04-04 18:42:16 +0000526 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
527 } else {
528 // We're out of initalizers; default-initialize to null
Douglas Gregor91f84212008-12-11 16:49:14 +0000529 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattner579a05d2008-04-04 18:42:16 +0000530 }
Lauro Ramos Venancioe2162c62008-02-19 19:27:31 +0000531 }
Devang Patel87174172007-10-26 17:44:44 +0000532}
533
Chris Lattner835635d2007-08-21 04:59:27 +0000534//===----------------------------------------------------------------------===//
535// Entry Points into this File
536//===----------------------------------------------------------------------===//
537
Mike Stump25306ca2009-05-26 18:57:45 +0000538/// EmitAggExpr - Emit the computation of the specified expression of aggregate
539/// type. The result is computed into DestPtr. Note that if DestPtr is null,
540/// the value of the aggregate expression is not needed. If VolatileDest is
541/// true, DestPtr cannot be 0.
Chris Lattner835635d2007-08-21 04:59:27 +0000542void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson5b106a72009-08-16 07:36:22 +0000543 bool VolatileDest, bool IgnoreResult,
Mike Stump11289f42009-09-09 15:08:12 +0000544 bool IsInitializer,
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000545 bool RequiresGCollection) {
Chris Lattner835635d2007-08-21 04:59:27 +0000546 assert(E && hasAggregateLLVMType(E->getType()) &&
547 "Invalid aggregate expression to emit");
Mike Stump25306ca2009-05-26 18:57:45 +0000548 assert ((DestPtr != 0 || VolatileDest == false)
549 && "volatile aggregate can't be 0");
Mike Stump11289f42009-09-09 15:08:12 +0000550
Fariborz Jahanian879d7262009-08-31 19:33:16 +0000551 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
552 RequiresGCollection)
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000553 .Visit(const_cast<Expr*>(E));
Chris Lattner835635d2007-08-21 04:59:27 +0000554}
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000555
556void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
557 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
558
559 EmitMemSetToZero(DestPtr, Ty);
560}
561
562void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump5e9e61b2009-05-23 22:29:41 +0000563 llvm::Value *SrcPtr, QualType Ty,
564 bool isVolatile) {
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000565 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000567 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattner3ef668c2009-02-28 18:18:58 +0000568 // C99 6.5.16.1p3, which states "If the value being stored in an object is
569 // read from another object that overlaps in anyway the storage of the first
570 // object, then the overlap shall be exact and the two objects shall have
571 // qualified or unqualified versions of a compatible type."
572 //
Chris Lattnerca05dfe2009-02-28 18:31:01 +0000573 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattner3ef668c2009-02-28 18:18:58 +0000574 // equal, but other compilers do this optimization, and almost every memcpy
575 // implementation handles this case safely. If there is a libc that does not
576 // safely handle this, we can add a target hook.
Owen Anderson41a75022009-08-13 21:57:51 +0000577 const llvm::Type *BP =
578 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000579 if (DestPtr->getType() != BP)
580 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
581 if (SrcPtr->getType() != BP)
582 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump11289f42009-09-09 15:08:12 +0000583
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000584 // Get size and alignment info for this aggregate.
585 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000586
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000587 // FIXME: Handle variable sized types.
Owen Anderson41a75022009-08-13 21:57:51 +0000588 const llvm::Type *IntPtr =
589 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump11289f42009-09-09 15:08:12 +0000590
Mike Stump86736572009-05-23 04:13:59 +0000591 // FIXME: If we have a volatile struct, the optimizer can remove what might
592 // appear to be `extra' memory ops:
593 //
594 // volatile struct { int i; } a, b;
595 //
596 // int main() {
597 // a = b;
598 // a = b;
599 // }
600 //
Mike Stumpec3cbfe2009-05-26 22:03:21 +0000601 // we need to use a differnt call here. We use isVolatile to indicate when
602 // either the source or the destination is volatile.
Chris Lattner3ef668c2009-02-28 18:18:58 +0000603 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000604 DestPtr, SrcPtr,
605 // TypeInfo.first describes size in bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000606 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mike Stump11289f42009-09-09 15:08:12 +0000607 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar0bc8e862008-09-09 20:49:46 +0000608 TypeInfo.second/8));
609}