blob: 84841bf5b9f854cf4a6da31bdd3b18b2fbfcfe6b [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 Lattnerf81557c2008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000026
Chris Lattner9c033562007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
Benjamin Kramer85b45212009-11-28 19:45:26 +000032class AggExprEmitter : public StmtVisitor<AggExprEmitter> {
Chris Lattner9c033562007-08-21 04:25:47 +000033 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
Mike Stump49d1cd52009-05-26 22:03:21 +000037 bool IgnoreResult;
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000038 bool IsInitializer;
Fariborz Jahanian08c32132009-08-31 19:33:16 +000039 bool RequiresGCollection;
Chris Lattner9c033562007-08-21 04:25:47 +000040public:
Mike Stumpff4bf3b2009-05-27 01:42:21 +000041 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
Fariborz Jahanian08c32132009-08-31 19:33:16 +000042 bool ignore, bool isinit, bool requiresGCollection)
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),
Fariborz Jahanian08c32132009-08-31 19:33:16 +000045 IsInitializer(isinit), RequiresGCollection(requiresGCollection) {
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 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000064
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); }
Daniel Dunbar5be028f2010-01-04 18:47:06 +000075 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000076 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000077 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000078 }
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) {
Mike Stump1eb44332009-09-09 15:08:12 +000083 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000084 }
85 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000086 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000087 }
Mike Stump1eb44332009-09-09 15:08:12 +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);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +000094 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000095 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000096 void VisitBinComma(const BinaryOperator *E);
Anders Carlssona024d172009-10-03 15:43:24 +000097 void VisitUnaryAddrOf(const UnaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000098
Chris Lattner8fdf3282008-06-24 17:04:18 +000099 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000100 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
101 EmitAggLoadOfLValue(E);
102 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000103 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000104 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattner9c033562007-08-21 04:25:47 +0000106 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000107 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000108 void VisitInitListExpr(InitListExpr *E);
Anders Carlsson30311fa2009-12-16 06:57:54 +0000109 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000110 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
111 Visit(DAE->getExpr());
112 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000113 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000114 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000115 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
Nuno Lopes329763b2009-10-18 15:18:11 +0000116 void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E);
Mike Stump2710c412009-11-18 00:40:12 +0000117 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000118
Eli Friedmanb1851242008-05-27 15:51:49 +0000119 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000120
Anders Carlsson78e83f82010-02-03 17:33:16 +0000121 void EmitInitializationToLValue(Expr *E, LValue Address, QualType T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000122 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000123 // case Expr::ChooseExprClass:
Mike Stump39406b12009-12-09 19:24:08 +0000124 void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
Chris Lattner9c033562007-08-21 04:25:47 +0000125};
126} // end anonymous namespace.
127
Chris Lattneree755f92007-08-21 04:59:27 +0000128//===----------------------------------------------------------------------===//
129// Utilities
130//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000131
Chris Lattner883f6a72007-08-11 00:04:45 +0000132/// EmitAggLoadOfLValue - Given an expression with aggregate type that
133/// represents a value lvalue, this method emits the address of the lvalue,
134/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000135void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
136 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000137 EmitFinalDestCopy(E, LV);
138}
139
140/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000141void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000142 assert(Src.isAggregate() && "value must be aggregate value!");
143
Chris Lattner883f6a72007-08-11 00:04:45 +0000144 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000145 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000146 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000147 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000148 // If the source is volatile, we must read from it; to do that, we need
149 // some place to put it.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000150 DestPtr = CGF.CreateMemTemp(E->getType(), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000151 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000152
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000153 if (RequiresGCollection) {
154 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
155 DestPtr, Src.getAggregateAddr(),
156 E->getType());
157 return;
158 }
Mike Stump4ac20dd2009-05-23 20:28:01 +0000159 // If the result of the assignment is used, copy the LHS there also.
160 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
161 // from the source as well, as we can't eliminate it if either operand
162 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000163 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
164 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000165}
166
167/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000168void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000169 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
170
171 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000172 Src.isVolatileQualified()),
173 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000174}
175
Chris Lattneree755f92007-08-21 04:59:27 +0000176//===----------------------------------------------------------------------===//
177// Visitor Methods
178//===----------------------------------------------------------------------===//
179
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000180void AggExprEmitter::VisitCastExpr(CastExpr *E) {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000181 if (!DestPtr) {
182 Visit(E->getSubExpr());
183 return;
184 }
185
Anders Carlsson30168422009-09-29 01:23:39 +0000186 switch (E->getCastKind()) {
187 default: assert(0 && "Unhandled cast kind!");
188
189 case CastExpr::CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000190 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000191 QualType PtrTy =
Anders Carlsson30168422009-09-29 01:23:39 +0000192 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000193 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
194 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000195 EmitInitializationToLValue(E->getSubExpr(),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000196 LValue::MakeAddr(CastPtr, Qualifiers()),
Eli Friedman18da88a2010-02-23 17:58:35 +0000197 E->getSubExpr()->getType());
Anders Carlsson30168422009-09-29 01:23:39 +0000198 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000199 }
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000201 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson30168422009-09-29 01:23:39 +0000202 case CastExpr::CK_Unknown:
203 case CastExpr::CK_NoOp:
204 case CastExpr::CK_UserDefinedConversion:
205 case CastExpr::CK_ConstructorConversion:
206 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
207 E->getType()) &&
208 "Implicit cast types must be compatible");
209 Visit(E->getSubExpr());
210 break;
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000211
212 case CastExpr::CK_NullToMemberPointer: {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000213 // If the subexpression's type is the C++0x nullptr_t, emit the
214 // subexpression, which may have side effects.
215 if (E->getSubExpr()->getType()->isNullPtrType())
216 Visit(E->getSubExpr());
217
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000218 const llvm::Type *PtrDiffTy =
219 CGF.ConvertType(CGF.getContext().getPointerDiffType());
220
221 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
222 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
223 Builder.CreateStore(NullValue, Ptr, VolatileDest);
224
225 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
226 Builder.CreateStore(NullValue, Adj, VolatileDest);
227
228 break;
229 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000230
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000231 case CastExpr::CK_BitCast: {
232 // This must be a member function pointer cast.
233 Visit(E->getSubExpr());
234 break;
235 }
236
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000237 case CastExpr::CK_DerivedToBaseMemberPointer:
Anders Carlsson84080ec2009-09-29 03:13:20 +0000238 case CastExpr::CK_BaseToDerivedMemberPointer: {
239 QualType SrcType = E->getSubExpr()->getType();
240
Daniel Dunbar195337d2010-02-09 02:48:28 +0000241 llvm::Value *Src = CGF.CreateMemTemp(SrcType, "tmp");
Anders Carlsson84080ec2009-09-29 03:13:20 +0000242 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
243
244 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
245 SrcPtr = Builder.CreateLoad(SrcPtr);
246
247 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
248 SrcAdj = Builder.CreateLoad(SrcAdj);
249
250 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
251 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
252
253 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
254
255 // Now See if we need to update the adjustment.
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000256 const CXXRecordDecl *BaseDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000257 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
258 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000259 const CXXRecordDecl *DerivedDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000260 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
261 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000262 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
263 std::swap(DerivedDecl, BaseDecl);
264
Anders Carlssonbb7e17b2010-01-31 01:36:53 +0000265 if (llvm::Constant *Adj =
Anders Carlssone04d45e2010-04-24 21:27:51 +0000266 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, E->getBasePath())) {
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000267 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
268 SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
269 else
270 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
271 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000272
273 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
274 break;
275 }
Anders Carlsson30168422009-09-29 01:23:39 +0000276 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000277}
278
Chris Lattner96196622008-07-26 22:37:01 +0000279void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000280 if (E->getCallReturnType()->isReferenceType()) {
281 EmitAggLoadOfLValue(E);
282 return;
283 }
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Anders Carlssond2490a92009-12-24 20:40:36 +0000285 // If the struct doesn't require GC, we can just pass the destination
286 // directly to EmitCall.
287 if (!RequiresGCollection) {
288 CGF.EmitCallExpr(E, ReturnValueSlot(DestPtr, VolatileDest));
289 return;
290 }
291
Anders Carlsson148fe672007-10-31 22:04:46 +0000292 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000293 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000294}
Chris Lattner96196622008-07-26 22:37:01 +0000295
296void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000297 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000298 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000299}
Anders Carlsson148fe672007-10-31 22:04:46 +0000300
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000301void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
302 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000303 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000304}
305
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000306void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
307 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000308 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000309 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000310}
311
Chris Lattner96196622008-07-26 22:37:01 +0000312void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000313 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000314 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
315 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000316}
317
Anders Carlssona024d172009-10-03 15:43:24 +0000318void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
319 // We have a member function pointer.
320 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
Daniel Dunbarbabac132009-10-17 09:39:30 +0000321 (void) MPT;
Anders Carlssona024d172009-10-03 15:43:24 +0000322 assert(MPT->getPointeeType()->isFunctionProtoType() &&
323 "Unexpected member pointer type!");
324
Douglas Gregora2813ce2009-10-23 18:54:35 +0000325 const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000326 const CXXMethodDecl *MD =
327 cast<CXXMethodDecl>(DRE->getDecl())->getCanonicalDecl();
Anders Carlssona024d172009-10-03 15:43:24 +0000328
329 const llvm::Type *PtrDiffTy =
330 CGF.ConvertType(CGF.getContext().getPointerDiffType());
331
332 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
333 llvm::Value *FuncPtr;
334
335 if (MD->isVirtual()) {
Anders Carlsson046c2942010-04-17 20:15:18 +0000336 int64_t Index = CGF.CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlssona024d172009-10-03 15:43:24 +0000337
Anders Carlssone8a81f72010-02-04 16:38:05 +0000338 // Itanium C++ ABI 2.3:
339 // For a non-virtual function, this field is a simple function pointer.
340 // For a virtual function, it is 1 plus the virtual table offset
341 // (in bytes) of the function, represented as a ptrdiff_t.
342 FuncPtr = llvm::ConstantInt::get(PtrDiffTy, (Index * 8) + 1);
Anders Carlssona024d172009-10-03 15:43:24 +0000343 } else {
Anders Carlsson585fa682010-02-07 17:37:13 +0000344 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
345 const llvm::Type *Ty =
346 CGF.CGM.getTypes().GetFunctionType(CGF.CGM.getTypes().getFunctionInfo(MD),
347 FPT->isVariadic());
348 llvm::Constant *Fn = CGF.CGM.GetAddrOfFunction(MD, Ty);
349 FuncPtr = llvm::ConstantExpr::getPtrToInt(Fn, PtrDiffTy);
Anders Carlssona024d172009-10-03 15:43:24 +0000350 }
351 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
352
353 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
354
355 // The adjustment will always be 0.
356 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
357 VolatileDest);
358}
359
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000360void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
361 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
362}
363
Chris Lattner9c033562007-08-21 04:25:47 +0000364void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +0000365 if (E->getOpcode() == BinaryOperator::PtrMemD ||
366 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000367 VisitPointerToDataMemberBinaryOperator(E);
368 else
369 CGF.ErrorUnsupported(E, "aggregate binary expression");
370}
371
372void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
373 const BinaryOperator *E) {
374 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
375 EmitFinalDestCopy(E, LV);
Chris Lattneree755f92007-08-21 04:59:27 +0000376}
377
Chris Lattner03d6fb92007-08-21 04:43:17 +0000378void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000379 // For an assignment to work, the value on the right has
380 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000381 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
382 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000383 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000384 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000385
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000386 // We have to special case property setters, otherwise we must have
387 // a simple lvalue (no aggregates inside vectors, bitfields).
388 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000389 llvm::Value *AggLoc = DestPtr;
390 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000391 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stump240993d2009-05-23 23:48:13 +0000392 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000393 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000394 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000395 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000396 llvm::Value *AggLoc = DestPtr;
397 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000398 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stumpa49af1a2009-05-23 23:52:31 +0000399 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000400 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000401 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000402 } else {
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000403 bool RequiresGCollection = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000404 if (CGF.getContext().getLangOptions().NeXTRuntime) {
405 QualType LHSTy = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000406 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000407 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000408 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000409 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000410 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
411 false, false, RequiresGCollection);
Mike Stump49d1cd52009-05-26 22:03:21 +0000412 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000413 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000414}
415
Chris Lattner9c033562007-08-21 04:25:47 +0000416void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Eli Friedman8e274bd2009-12-25 06:17:05 +0000417 if (!E->getLHS()) {
418 CGF.ErrorUnsupported(E, "conditional operator with missing LHS");
419 return;
420 }
421
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000422 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
423 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
424 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Eli Friedman8e274bd2009-12-25 06:17:05 +0000426 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Anders Carlsson72119a82010-02-04 17:18:07 +0000428 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000429 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner883f6a72007-08-11 00:04:45 +0000431 // Handle the GNU extension for missing LHS.
432 assert(E->getLHS() && "Must have LHS for aggregate value");
433
Chris Lattnerc748f272007-08-21 05:02:10 +0000434 Visit(E->getLHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000435 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000436 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Anders Carlsson72119a82010-02-04 17:18:07 +0000438 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000439 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Chris Lattnerc748f272007-08-21 05:02:10 +0000441 Visit(E->getRHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000442 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000443 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner9c033562007-08-21 04:25:47 +0000445 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000446}
Chris Lattneree755f92007-08-21 04:59:27 +0000447
Anders Carlssona294ca82009-07-08 18:33:14 +0000448void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
449 Visit(CE->getChosenSubExpr(CGF.getContext()));
450}
451
Eli Friedmanb1851242008-05-27 15:51:49 +0000452void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000453 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000454 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
455
Sebastian Redl0262f022009-01-09 21:09:38 +0000456 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000457 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000458 return;
459 }
460
John McCall0953e762009-09-24 19:53:00 +0000461 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000462}
463
Anders Carlssonb58d0172009-05-30 23:23:33 +0000464void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
465 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Anders Carlssonb58d0172009-05-30 23:23:33 +0000467 if (!Val) {
468 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000469 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000470
471 // FIXME: volatile
472 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000473 } else
Anders Carlssonb58d0172009-05-30 23:23:33 +0000474 Visit(E->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000476 // Don't make this a live temporary if we're emitting an initializer expr.
477 if (!IsInitializer)
478 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000479}
480
Anders Carlssonb14095a2009-04-17 00:06:03 +0000481void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000482AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000483 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Anders Carlssonb58d0172009-05-30 23:23:33 +0000485 if (!Val) {
486 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000487 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000488 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000489
Douglas Gregor16006c92009-12-16 18:50:27 +0000490 if (E->requiresZeroInitialization())
491 EmitNullInitializationToLValue(LValue::MakeAddr(Val,
Mike Stumpdd5614b2009-12-18 02:14:27 +0000492 // FIXME: Qualifiers()?
Douglas Gregor16006c92009-12-16 18:50:27 +0000493 E->getType().getQualifiers()),
494 E->getType());
495
Anders Carlssonb58d0172009-05-30 23:23:33 +0000496 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000497}
498
499void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000500 llvm::Value *Val = DestPtr;
501
Mike Stumpdd5614b2009-12-18 02:14:27 +0000502 CGF.EmitCXXExprWithTemporaries(E, Val, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000503}
504
Nuno Lopes329763b2009-10-18 15:18:11 +0000505void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000506 llvm::Value *Val = DestPtr;
507
508 if (!Val) {
509 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000510 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000511 }
512 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000513 EmitNullInitializationToLValue(LV, E->getType());
514}
515
516void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000517 llvm::Value *Val = DestPtr;
518
519 if (!Val) {
520 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000521 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000522 }
523 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000524 EmitNullInitializationToLValue(LV, E->getType());
Nuno Lopes329763b2009-10-18 15:18:11 +0000525}
526
Anders Carlsson78e83f82010-02-03 17:33:16 +0000527void
528AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000529 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000530 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000531 if (isa<ImplicitValueInitExpr>(E)) {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000532 EmitNullInitializationToLValue(LV, T);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000533 } else if (T->isReferenceType()) {
534 RValue RV = CGF.EmitReferenceBindingToExpr(E, /*IsInitializer=*/false);
535 CGF.EmitStoreThroughLValue(RV, LV, T);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000536 } else if (T->isAnyComplexType()) {
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000537 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000538 } else if (CGF.hasAggregateLLVMType(T)) {
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000539 CGF.EmitAnyExpr(E, LV.getAddress(), false);
540 } else {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000541 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000542 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000543}
544
545void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
546 if (!CGF.hasAggregateLLVMType(T)) {
547 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000548 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000549 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000550 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000551 // Otherwise, just memset the whole thing to zero. This is legal
552 // because in LLVM, all default initializers are guaranteed to have a
553 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000554 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000555 // There's a potential optimization opportunity in combining
556 // memsets; that would be easy for arrays, but relatively
557 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000558 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000559 }
560}
561
Chris Lattnerf81557c2008-04-04 18:42:16 +0000562void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000563#if 0
Eli Friedman13a5be12009-12-04 01:30:56 +0000564 // FIXME: Assess perf here? Figure out what cases are worth optimizing here
565 // (Length of globals? Chunks of zeroed-out space?).
Eli Friedmana385b3c2008-12-02 01:17:45 +0000566 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000567 // If we can, prefer a copy from a global; this is a lot less code for long
568 // globals, and it's easier for the current optimizers to analyze.
Eli Friedman13a5be12009-12-04 01:30:56 +0000569 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000570 llvm::GlobalVariable* GV =
Eli Friedman13a5be12009-12-04 01:30:56 +0000571 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
572 llvm::GlobalValue::InternalLinkage, C, "");
573 EmitFinalDestCopy(E, LValue::MakeAddr(GV, Qualifiers()));
Eli Friedman994ffef2008-11-30 02:11:09 +0000574 return;
575 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000576#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000577 if (E->hadArrayRangeDesignator()) {
578 CGF.ErrorUnsupported(E, "GNU array range designator extension");
579 }
580
Chris Lattnerf81557c2008-04-04 18:42:16 +0000581 // Handle initialization of an array.
582 if (E->getType()->isArrayType()) {
583 const llvm::PointerType *APType =
584 cast<llvm::PointerType>(DestPtr->getType());
585 const llvm::ArrayType *AType =
586 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattnerf81557c2008-04-04 18:42:16 +0000588 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000589
Chris Lattner96196622008-07-26 22:37:01 +0000590 if (E->getNumInits() > 0) {
591 QualType T1 = E->getType();
592 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000593 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000594 EmitAggLoadOfLValue(E->getInit(0));
595 return;
596 }
Eli Friedman922696f2008-05-19 17:51:16 +0000597 }
598
Chris Lattnerf81557c2008-04-04 18:42:16 +0000599 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000600 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000601 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000602
John McCall0953e762009-09-24 19:53:00 +0000603 // FIXME: were we intentionally ignoring address spaces and GC attributes?
604 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000605
Chris Lattnerf81557c2008-04-04 18:42:16 +0000606 for (uint64_t i = 0; i != NumArrayElements; ++i) {
607 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
608 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000609 EmitInitializationToLValue(E->getInit(i),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000610 LValue::MakeAddr(NextVal, Quals),
611 ElementType);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000612 else
John McCall0953e762009-09-24 19:53:00 +0000613 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000614 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000615 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000616 return;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Chris Lattnerf81557c2008-04-04 18:42:16 +0000619 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattnerf81557c2008-04-04 18:42:16 +0000621 // Do struct initialization; this code just sets each individual member
622 // to the approprate value. This makes bitfield support automatic;
623 // the disadvantage is that the generated code is more difficult for
624 // the optimizer, especially with bitfields.
625 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000626 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000627 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000628
629 if (E->getType()->isUnionType()) {
630 // Only initialize one field of a union. The field itself is
631 // specified by the initializer list.
632 if (!E->getInitializedFieldInUnion()) {
633 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Douglas Gregor0bb76892009-01-29 16:53:55 +0000635#ifndef NDEBUG
636 // Make sure that it's really an empty and not a failure of
637 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000638 for (RecordDecl::field_iterator Field = SD->field_begin(),
639 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000640 Field != FieldEnd; ++Field)
641 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
642#endif
643 return;
644 }
645
646 // FIXME: volatility
647 FieldDecl *Field = E->getInitializedFieldInUnion();
Anders Carlssone78ccb42010-02-03 19:13:55 +0000648 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
Douglas Gregor0bb76892009-01-29 16:53:55 +0000649
650 if (NumInitElements) {
651 // Store the initializer into the field
Anders Carlsson78e83f82010-02-03 17:33:16 +0000652 EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000653 } else {
654 // Default-initialize to null
655 EmitNullInitializationToLValue(FieldLoc, Field->getType());
656 }
657
658 return;
659 }
Chris Lattnerb35baae2010-03-08 21:08:07 +0000660
661 // If we're initializing the whole aggregate, just do it in place.
662 // FIXME: This is a hack around an AST bug (PR6537).
663 if (NumInitElements == 1 && E->getType() == E->getInit(0)->getType()) {
664 EmitInitializationToLValue(E->getInit(0),
665 LValue::MakeAddr(DestPtr, Qualifiers()),
666 E->getType());
667 return;
668 }
669
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Chris Lattnerf81557c2008-04-04 18:42:16 +0000671 // Here we iterate over the fields; this makes it simpler to both
672 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000673 for (RecordDecl::field_iterator Field = SD->field_begin(),
674 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000675 Field != FieldEnd; ++Field) {
676 // We're done once we hit the flexible array member
677 if (Field->getType()->isIncompleteArrayType())
678 break;
679
Douglas Gregor34e79462009-01-28 23:36:17 +0000680 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000681 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000682
Eli Friedman1e692ac2008-06-13 23:01:12 +0000683 // FIXME: volatility
Anders Carlssone78ccb42010-02-03 19:13:55 +0000684 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000685 // We never generate write-barries for initialized fields.
686 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000687 if (CurInitVal < NumInitElements) {
Chris Lattnerb35baae2010-03-08 21:08:07 +0000688 // Store the initializer into the field.
689 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
Anders Carlsson78e83f82010-02-03 17:33:16 +0000690 Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000691 } else {
692 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000693 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000694 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000695 }
Devang Patel636c3d02007-10-26 17:44:44 +0000696}
697
Chris Lattneree755f92007-08-21 04:59:27 +0000698//===----------------------------------------------------------------------===//
699// Entry Points into this File
700//===----------------------------------------------------------------------===//
701
Mike Stumpe1129a92009-05-26 18:57:45 +0000702/// EmitAggExpr - Emit the computation of the specified expression of aggregate
703/// type. The result is computed into DestPtr. Note that if DestPtr is null,
704/// the value of the aggregate expression is not needed. If VolatileDest is
705/// true, DestPtr cannot be 0.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000706//
707// FIXME: Take Qualifiers object.
Chris Lattneree755f92007-08-21 04:59:27 +0000708void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000709 bool VolatileDest, bool IgnoreResult,
Mike Stump1eb44332009-09-09 15:08:12 +0000710 bool IsInitializer,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000711 bool RequiresGCollection) {
Chris Lattneree755f92007-08-21 04:59:27 +0000712 assert(E && hasAggregateLLVMType(E->getType()) &&
713 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000714 assert ((DestPtr != 0 || VolatileDest == false)
715 && "volatile aggregate can't be 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000717 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
718 RequiresGCollection)
Mike Stump49d1cd52009-05-26 22:03:21 +0000719 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000720}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000721
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000722LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
723 assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
724 Qualifiers Q = MakeQualifiers(E->getType());
Daniel Dunbar195337d2010-02-09 02:48:28 +0000725 llvm::Value *Temp = CreateMemTemp(E->getType());
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000726 EmitAggExpr(E, Temp, Q.hasVolatile());
727 return LValue::MakeAddr(Temp, Q);
728}
729
Daniel Dunbar7482d122008-09-09 20:49:46 +0000730void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
731 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
732
733 EmitMemSetToZero(DestPtr, Ty);
734}
735
736void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000737 llvm::Value *SrcPtr, QualType Ty,
738 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000739 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattner83c96292009-02-28 18:31:01 +0000741 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000742 // C99 6.5.16.1p3, which states "If the value being stored in an object is
743 // read from another object that overlaps in anyway the storage of the first
744 // object, then the overlap shall be exact and the two objects shall have
745 // qualified or unqualified versions of a compatible type."
746 //
Chris Lattner83c96292009-02-28 18:31:01 +0000747 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000748 // equal, but other compilers do this optimization, and almost every memcpy
749 // implementation handles this case safely. If there is a libc that does not
750 // safely handle this, we can add a target hook.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000751 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar7482d122008-09-09 20:49:46 +0000752 if (DestPtr->getType() != BP)
753 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
754 if (SrcPtr->getType() != BP)
755 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Daniel Dunbar7482d122008-09-09 20:49:46 +0000757 // Get size and alignment info for this aggregate.
758 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Daniel Dunbar7482d122008-09-09 20:49:46 +0000760 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000761 const llvm::Type *IntPtr =
762 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Mike Stumpfde64202009-05-23 04:13:59 +0000764 // FIXME: If we have a volatile struct, the optimizer can remove what might
765 // appear to be `extra' memory ops:
766 //
767 // volatile struct { int i; } a, b;
768 //
769 // int main() {
770 // a = b;
771 // a = b;
772 // }
773 //
Mon P Wang3ecd7852010-04-04 03:10:52 +0000774 // we need to use a different call here. We use isVolatile to indicate when
Mike Stump49d1cd52009-05-26 22:03:21 +0000775 // either the source or the destination is volatile.
Mon P Wang3ecd7852010-04-04 03:10:52 +0000776 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(VMContext);
777 const llvm::Type *I8Ty = llvm::Type::getInt8Ty(VMContext);
778 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
779
780 const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
781 const llvm::Type *DBP = llvm::PointerType::get(I8Ty, DPT->getAddressSpace());
782 if (DestPtr->getType() != DBP)
783 DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
784
785 const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
786 const llvm::Type *SBP = llvm::PointerType::get(I8Ty, SPT->getAddressSpace());
787 if (SrcPtr->getType() != SBP)
788 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
789
790 Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(),
791 IntPtr),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000792 DestPtr, SrcPtr,
793 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000794 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000795 llvm::ConstantInt::get(I32Ty, TypeInfo.second/8),
796 llvm::ConstantInt::get(I1Ty, isVolatile));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000797}