blob: 3bf2f8a52025f58fbf1a94cd52c73b365436bb8a [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 Gregor69cfeb12010-05-14 21:31:02 +0000181 if (!DestPtr && E->getCastKind() != CastExpr::CK_Dynamic) {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000182 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
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000189 case CastExpr::CK_Dynamic: {
190 assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
191 LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
192 // FIXME: Do we also need to handle property references here?
193 if (LV.isSimple())
194 CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
195 else
196 CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
197
198 if (DestPtr)
199 CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
200 break;
201 }
202
Anders Carlsson30168422009-09-29 01:23:39 +0000203 case CastExpr::CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000204 // GCC union extension
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000205 QualType PtrTy =
Anders Carlsson30168422009-09-29 01:23:39 +0000206 CGF.getContext().getPointerType(E->getSubExpr()->getType());
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000207 llvm::Value *CastPtr = Builder.CreateBitCast(DestPtr,
208 CGF.ConvertType(PtrTy));
Mon P Wangc6a38a42009-07-22 03:08:17 +0000209 EmitInitializationToLValue(E->getSubExpr(),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000210 LValue::MakeAddr(CastPtr, Qualifiers()),
Eli Friedman18da88a2010-02-23 17:58:35 +0000211 E->getSubExpr()->getType());
Anders Carlsson30168422009-09-29 01:23:39 +0000212 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000215 // FIXME: Remove the CK_Unknown check here.
Anders Carlsson30168422009-09-29 01:23:39 +0000216 case CastExpr::CK_Unknown:
217 case CastExpr::CK_NoOp:
218 case CastExpr::CK_UserDefinedConversion:
219 case CastExpr::CK_ConstructorConversion:
220 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
221 E->getType()) &&
222 "Implicit cast types must be compatible");
223 Visit(E->getSubExpr());
224 break;
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000225
226 case CastExpr::CK_NullToMemberPointer: {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000227 // If the subexpression's type is the C++0x nullptr_t, emit the
228 // subexpression, which may have side effects.
229 if (E->getSubExpr()->getType()->isNullPtrType())
230 Visit(E->getSubExpr());
231
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000232 const llvm::Type *PtrDiffTy =
233 CGF.ConvertType(CGF.getContext().getPointerDiffType());
234
235 llvm::Value *NullValue = llvm::Constant::getNullValue(PtrDiffTy);
236 llvm::Value *Ptr = Builder.CreateStructGEP(DestPtr, 0, "ptr");
237 Builder.CreateStore(NullValue, Ptr, VolatileDest);
238
239 llvm::Value *Adj = Builder.CreateStructGEP(DestPtr, 1, "adj");
240 Builder.CreateStore(NullValue, Adj, VolatileDest);
241
242 break;
243 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000244
Anders Carlssonbb378cb2009-10-18 20:31:03 +0000245 case CastExpr::CK_BitCast: {
246 // This must be a member function pointer cast.
247 Visit(E->getSubExpr());
248 break;
249 }
250
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000251 case CastExpr::CK_DerivedToBaseMemberPointer:
Anders Carlsson84080ec2009-09-29 03:13:20 +0000252 case CastExpr::CK_BaseToDerivedMemberPointer: {
253 QualType SrcType = E->getSubExpr()->getType();
254
Daniel Dunbar195337d2010-02-09 02:48:28 +0000255 llvm::Value *Src = CGF.CreateMemTemp(SrcType, "tmp");
Anders Carlsson84080ec2009-09-29 03:13:20 +0000256 CGF.EmitAggExpr(E->getSubExpr(), Src, SrcType.isVolatileQualified());
257
258 llvm::Value *SrcPtr = Builder.CreateStructGEP(Src, 0, "src.ptr");
259 SrcPtr = Builder.CreateLoad(SrcPtr);
260
261 llvm::Value *SrcAdj = Builder.CreateStructGEP(Src, 1, "src.adj");
262 SrcAdj = Builder.CreateLoad(SrcAdj);
263
264 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
265 Builder.CreateStore(SrcPtr, DstPtr, VolatileDest);
266
267 llvm::Value *DstAdj = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
268
269 // Now See if we need to update the adjustment.
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000270 const CXXRecordDecl *BaseDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000271 cast<CXXRecordDecl>(SrcType->getAs<MemberPointerType>()->
272 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000273 const CXXRecordDecl *DerivedDecl =
Anders Carlsson84080ec2009-09-29 03:13:20 +0000274 cast<CXXRecordDecl>(E->getType()->getAs<MemberPointerType>()->
275 getClass()->getAs<RecordType>()->getDecl());
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000276 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
277 std::swap(DerivedDecl, BaseDecl);
278
Anders Carlssonbb7e17b2010-01-31 01:36:53 +0000279 if (llvm::Constant *Adj =
Anders Carlssone04d45e2010-04-24 21:27:51 +0000280 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl, E->getBasePath())) {
Eli Friedmanc6fdb7762009-11-27 04:46:20 +0000281 if (E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer)
282 SrcAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
283 else
284 SrcAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
285 }
Anders Carlsson84080ec2009-09-29 03:13:20 +0000286
287 Builder.CreateStore(SrcAdj, DstAdj, VolatileDest);
288 break;
289 }
Anders Carlsson30168422009-09-29 01:23:39 +0000290 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000291}
292
Chris Lattner96196622008-07-26 22:37:01 +0000293void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000294 if (E->getCallReturnType()->isReferenceType()) {
295 EmitAggLoadOfLValue(E);
296 return;
297 }
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Anders Carlssond2490a92009-12-24 20:40:36 +0000299 // If the struct doesn't require GC, we can just pass the destination
300 // directly to EmitCall.
301 if (!RequiresGCollection) {
302 CGF.EmitCallExpr(E, ReturnValueSlot(DestPtr, VolatileDest));
303 return;
304 }
305
Anders Carlsson148fe672007-10-31 22:04:46 +0000306 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000307 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000308}
Chris Lattner96196622008-07-26 22:37:01 +0000309
310void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000311 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000312 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000313}
Anders Carlsson148fe672007-10-31 22:04:46 +0000314
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000315void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
316 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000317 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000318}
319
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000320void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
321 ObjCImplicitSetterGetterRefExpr *E) {
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000322 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000323 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000324}
325
Chris Lattner96196622008-07-26 22:37:01 +0000326void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000327 CGF.EmitAnyExpr(E->getLHS(), 0, false, true);
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000328 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest,
329 /*IgnoreResult=*/false, IsInitializer);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000330}
331
Anders Carlssona024d172009-10-03 15:43:24 +0000332void AggExprEmitter::VisitUnaryAddrOf(const UnaryOperator *E) {
333 // We have a member function pointer.
334 const MemberPointerType *MPT = E->getType()->getAs<MemberPointerType>();
Daniel Dunbarbabac132009-10-17 09:39:30 +0000335 (void) MPT;
Anders Carlssona024d172009-10-03 15:43:24 +0000336 assert(MPT->getPointeeType()->isFunctionProtoType() &&
337 "Unexpected member pointer type!");
Douglas Gregore174bd02010-05-03 20:00:27 +0000338
339 // The creation of member function pointers has no side effects; if
340 // there is no destination pointer, we have nothing to do.
341 if (!DestPtr)
342 return;
Anders Carlssona024d172009-10-03 15:43:24 +0000343
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344 const DeclRefExpr *DRE = cast<DeclRefExpr>(E->getSubExpr());
Anders Carlsson7af4ec72010-01-05 05:04:05 +0000345 const CXXMethodDecl *MD =
346 cast<CXXMethodDecl>(DRE->getDecl())->getCanonicalDecl();
Anders Carlssona024d172009-10-03 15:43:24 +0000347
348 const llvm::Type *PtrDiffTy =
349 CGF.ConvertType(CGF.getContext().getPointerDiffType());
350
Douglas Gregore174bd02010-05-03 20:00:27 +0000351
Anders Carlssona024d172009-10-03 15:43:24 +0000352 llvm::Value *DstPtr = Builder.CreateStructGEP(DestPtr, 0, "dst.ptr");
353 llvm::Value *FuncPtr;
354
355 if (MD->isVirtual()) {
Anders Carlsson046c2942010-04-17 20:15:18 +0000356 int64_t Index = CGF.CGM.getVTables().getMethodVTableIndex(MD);
Anders Carlssona024d172009-10-03 15:43:24 +0000357
Anders Carlsson32897fd2010-05-03 16:05:06 +0000358 // FIXME: We shouldn't use / 8 here.
359 uint64_t PointerWidthInBytes =
360 CGF.CGM.getContext().Target.getPointerWidth(0) / 8;
361
Anders Carlssone8a81f72010-02-04 16:38:05 +0000362 // Itanium C++ ABI 2.3:
363 // For a non-virtual function, this field is a simple function pointer.
364 // For a virtual function, it is 1 plus the virtual table offset
365 // (in bytes) of the function, represented as a ptrdiff_t.
Anders Carlsson32897fd2010-05-03 16:05:06 +0000366 FuncPtr = llvm::ConstantInt::get(PtrDiffTy,
367 (Index * PointerWidthInBytes) + 1);
Anders Carlssona024d172009-10-03 15:43:24 +0000368 } else {
Anders Carlsson585fa682010-02-07 17:37:13 +0000369 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
370 const llvm::Type *Ty =
371 CGF.CGM.getTypes().GetFunctionType(CGF.CGM.getTypes().getFunctionInfo(MD),
372 FPT->isVariadic());
373 llvm::Constant *Fn = CGF.CGM.GetAddrOfFunction(MD, Ty);
374 FuncPtr = llvm::ConstantExpr::getPtrToInt(Fn, PtrDiffTy);
Anders Carlssona024d172009-10-03 15:43:24 +0000375 }
376 Builder.CreateStore(FuncPtr, DstPtr, VolatileDest);
377
378 llvm::Value *AdjPtr = Builder.CreateStructGEP(DestPtr, 1, "dst.adj");
379
380 // The adjustment will always be 0.
381 Builder.CreateStore(llvm::ConstantInt::get(PtrDiffTy, 0), AdjPtr,
382 VolatileDest);
383}
384
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000385void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
386 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
387}
388
Chris Lattner9c033562007-08-21 04:25:47 +0000389void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +0000390 if (E->getOpcode() == BinaryOperator::PtrMemD ||
391 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000392 VisitPointerToDataMemberBinaryOperator(E);
393 else
394 CGF.ErrorUnsupported(E, "aggregate binary expression");
395}
396
397void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
398 const BinaryOperator *E) {
399 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
400 EmitFinalDestCopy(E, LV);
Chris Lattneree755f92007-08-21 04:59:27 +0000401}
402
Chris Lattner03d6fb92007-08-21 04:43:17 +0000403void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000404 // For an assignment to work, the value on the right has
405 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000406 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
407 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000408 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000409 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000410
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000411 // We have to special case property setters, otherwise we must have
412 // a simple lvalue (no aggregates inside vectors, bitfields).
413 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000414 llvm::Value *AggLoc = DestPtr;
415 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000416 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stump240993d2009-05-23 23:48:13 +0000417 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000418 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000419 RValue::getAggregate(AggLoc, VolatileDest));
Mike Stumpb3589f42009-07-30 22:28:39 +0000420 } else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000421 llvm::Value *AggLoc = DestPtr;
422 if (!AggLoc)
Daniel Dunbar195337d2010-02-09 02:48:28 +0000423 AggLoc = CGF.CreateMemTemp(E->getRHS()->getType());
Mike Stumpa49af1a2009-05-23 23:52:31 +0000424 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Mike Stump1eb44332009-09-09 15:08:12 +0000425 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000426 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000427 } else {
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000428 bool RequiresGCollection = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000429 if (CGF.getContext().getLangOptions().NeXTRuntime) {
430 QualType LHSTy = E->getLHS()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000431 if (const RecordType *FDTTy = LHSTy.getTypePtr()->getAs<RecordType>())
Mike Stump1eb44332009-09-09 15:08:12 +0000432 RequiresGCollection = FDTTy->getDecl()->hasObjectMember();
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000433 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000434 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000435 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified(),
436 false, false, RequiresGCollection);
Mike Stump49d1cd52009-05-26 22:03:21 +0000437 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000438 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000439}
440
Chris Lattner9c033562007-08-21 04:25:47 +0000441void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Eli Friedman8e274bd2009-12-25 06:17:05 +0000442 if (!E->getLHS()) {
443 CGF.ErrorUnsupported(E, "conditional operator with missing LHS");
444 return;
445 }
446
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000447 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
448 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
449 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Eli Friedman8e274bd2009-12-25 06:17:05 +0000451 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Anders Carlsson72119a82010-02-04 17:18:07 +0000453 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000454 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Chris Lattner883f6a72007-08-11 00:04:45 +0000456 // Handle the GNU extension for missing LHS.
457 assert(E->getLHS() && "Must have LHS for aggregate value");
458
Chris Lattnerc748f272007-08-21 05:02:10 +0000459 Visit(E->getLHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000460 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000461 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Anders Carlsson72119a82010-02-04 17:18:07 +0000463 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000464 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Chris Lattnerc748f272007-08-21 05:02:10 +0000466 Visit(E->getRHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000467 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000468 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner9c033562007-08-21 04:25:47 +0000470 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000471}
Chris Lattneree755f92007-08-21 04:59:27 +0000472
Anders Carlssona294ca82009-07-08 18:33:14 +0000473void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
474 Visit(CE->getChosenSubExpr(CGF.getContext()));
475}
476
Eli Friedmanb1851242008-05-27 15:51:49 +0000477void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000478 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000479 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
480
Sebastian Redl0262f022009-01-09 21:09:38 +0000481 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000482 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000483 return;
484 }
485
John McCall0953e762009-09-24 19:53:00 +0000486 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, Qualifiers()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000487}
488
Anders Carlssonb58d0172009-05-30 23:23:33 +0000489void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
490 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlssonb58d0172009-05-30 23:23:33 +0000492 if (!Val) {
493 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000494 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000495
496 // FIXME: volatile
497 CGF.EmitAggExpr(E->getSubExpr(), Val, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000498 } else
Anders Carlssonb58d0172009-05-30 23:23:33 +0000499 Visit(E->getSubExpr());
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000501 // Don't make this a live temporary if we're emitting an initializer expr.
502 if (!IsInitializer)
503 CGF.PushCXXTemporary(E->getTemporary(), Val);
Anders Carlssonb58d0172009-05-30 23:23:33 +0000504}
505
Anders Carlssonb14095a2009-04-17 00:06:03 +0000506void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000507AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlssonb58d0172009-05-30 23:23:33 +0000508 llvm::Value *Val = DestPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Anders Carlssonb58d0172009-05-30 23:23:33 +0000510 if (!Val) {
511 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000512 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Anders Carlssonb58d0172009-05-30 23:23:33 +0000513 }
Anders Carlsson8e587a12009-05-30 20:56:46 +0000514
Douglas Gregor16006c92009-12-16 18:50:27 +0000515 if (E->requiresZeroInitialization())
516 EmitNullInitializationToLValue(LValue::MakeAddr(Val,
Mike Stumpdd5614b2009-12-18 02:14:27 +0000517 // FIXME: Qualifiers()?
Douglas Gregor16006c92009-12-16 18:50:27 +0000518 E->getType().getQualifiers()),
519 E->getType());
520
Anders Carlssonb58d0172009-05-30 23:23:33 +0000521 CGF.EmitCXXConstructExpr(Val, E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000522}
523
524void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000525 llvm::Value *Val = DestPtr;
526
Mike Stumpdd5614b2009-12-18 02:14:27 +0000527 CGF.EmitCXXExprWithTemporaries(E, Val, VolatileDest, IsInitializer);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000528}
529
Nuno Lopes329763b2009-10-18 15:18:11 +0000530void AggExprEmitter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000531 llvm::Value *Val = DestPtr;
532
533 if (!Val) {
534 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000535 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000536 }
537 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000538 EmitNullInitializationToLValue(LV, E->getType());
539}
540
541void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
Mike Stumpdd5614b2009-12-18 02:14:27 +0000542 llvm::Value *Val = DestPtr;
543
544 if (!Val) {
545 // Create a temporary variable.
Daniel Dunbar195337d2010-02-09 02:48:28 +0000546 Val = CGF.CreateMemTemp(E->getType(), "tmp");
Mike Stumpdd5614b2009-12-18 02:14:27 +0000547 }
548 LValue LV = LValue::MakeAddr(Val, Qualifiers());
Anders Carlsson30311fa2009-12-16 06:57:54 +0000549 EmitNullInitializationToLValue(LV, E->getType());
Nuno Lopes329763b2009-10-18 15:18:11 +0000550}
551
Anders Carlsson78e83f82010-02-03 17:33:16 +0000552void
553AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000554 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000555 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000556 if (isa<ImplicitValueInitExpr>(E)) {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000557 EmitNullInitializationToLValue(LV, T);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000558 } else if (T->isReferenceType()) {
559 RValue RV = CGF.EmitReferenceBindingToExpr(E, /*IsInitializer=*/false);
560 CGF.EmitStoreThroughLValue(RV, LV, T);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000561 } else if (T->isAnyComplexType()) {
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000562 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000563 } else if (CGF.hasAggregateLLVMType(T)) {
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000564 CGF.EmitAnyExpr(E, LV.getAddress(), false);
565 } else {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000566 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000567 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000568}
569
570void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
571 if (!CGF.hasAggregateLLVMType(T)) {
572 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000573 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000574 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000575 } else {
Anders Carlsson892fa6c2010-05-21 22:17:48 +0000576 // Otherwise, just memset the whole thing to zero. This is legal
577 // because in LLVM, all default initializers are guaranteed to have a
578 // bit pattern of all zeros.
579 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000580 // There's a potential optimization opportunity in combining
581 // memsets; that would be easy for arrays, but relatively
582 // difficult for structures with the current code.
Anders Carlsson892fa6c2010-05-21 22:17:48 +0000583 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000584 }
585}
586
Chris Lattnerf81557c2008-04-04 18:42:16 +0000587void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000588#if 0
Eli Friedman13a5be12009-12-04 01:30:56 +0000589 // FIXME: Assess perf here? Figure out what cases are worth optimizing here
590 // (Length of globals? Chunks of zeroed-out space?).
Eli Friedmana385b3c2008-12-02 01:17:45 +0000591 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000592 // If we can, prefer a copy from a global; this is a lot less code for long
593 // globals, and it's easier for the current optimizers to analyze.
Eli Friedman13a5be12009-12-04 01:30:56 +0000594 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000595 llvm::GlobalVariable* GV =
Eli Friedman13a5be12009-12-04 01:30:56 +0000596 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
597 llvm::GlobalValue::InternalLinkage, C, "");
598 EmitFinalDestCopy(E, LValue::MakeAddr(GV, Qualifiers()));
Eli Friedman994ffef2008-11-30 02:11:09 +0000599 return;
600 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000601#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000602 if (E->hadArrayRangeDesignator()) {
603 CGF.ErrorUnsupported(E, "GNU array range designator extension");
604 }
605
Chris Lattnerf81557c2008-04-04 18:42:16 +0000606 // Handle initialization of an array.
607 if (E->getType()->isArrayType()) {
608 const llvm::PointerType *APType =
609 cast<llvm::PointerType>(DestPtr->getType());
610 const llvm::ArrayType *AType =
611 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattnerf81557c2008-04-04 18:42:16 +0000613 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000614
Chris Lattner96196622008-07-26 22:37:01 +0000615 if (E->getNumInits() > 0) {
616 QualType T1 = E->getType();
617 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000618 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000619 EmitAggLoadOfLValue(E->getInit(0));
620 return;
621 }
Eli Friedman922696f2008-05-19 17:51:16 +0000622 }
623
Chris Lattnerf81557c2008-04-04 18:42:16 +0000624 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000625 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000626 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000627
John McCall0953e762009-09-24 19:53:00 +0000628 // FIXME: were we intentionally ignoring address spaces and GC attributes?
629 Qualifiers Quals = CGF.MakeQualifiers(ElementType);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000630
Chris Lattnerf81557c2008-04-04 18:42:16 +0000631 for (uint64_t i = 0; i != NumArrayElements; ++i) {
632 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
633 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000634 EmitInitializationToLValue(E->getInit(i),
Anders Carlsson78e83f82010-02-03 17:33:16 +0000635 LValue::MakeAddr(NextVal, Quals),
636 ElementType);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000637 else
John McCall0953e762009-09-24 19:53:00 +0000638 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, Quals),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000639 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000640 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000641 return;
642 }
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Chris Lattnerf81557c2008-04-04 18:42:16 +0000644 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattnerf81557c2008-04-04 18:42:16 +0000646 // Do struct initialization; this code just sets each individual member
647 // to the approprate value. This makes bitfield support automatic;
648 // the disadvantage is that the generated code is more difficult for
649 // the optimizer, especially with bitfields.
650 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000651 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000652 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000653
654 if (E->getType()->isUnionType()) {
655 // Only initialize one field of a union. The field itself is
656 // specified by the initializer list.
657 if (!E->getInitializedFieldInUnion()) {
658 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Douglas Gregor0bb76892009-01-29 16:53:55 +0000660#ifndef NDEBUG
661 // Make sure that it's really an empty and not a failure of
662 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663 for (RecordDecl::field_iterator Field = SD->field_begin(),
664 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000665 Field != FieldEnd; ++Field)
666 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
667#endif
668 return;
669 }
670
671 // FIXME: volatility
672 FieldDecl *Field = E->getInitializedFieldInUnion();
Anders Carlssone78ccb42010-02-03 19:13:55 +0000673 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
Douglas Gregor0bb76892009-01-29 16:53:55 +0000674
675 if (NumInitElements) {
676 // Store the initializer into the field
Anders Carlsson78e83f82010-02-03 17:33:16 +0000677 EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000678 } else {
679 // Default-initialize to null
680 EmitNullInitializationToLValue(FieldLoc, Field->getType());
681 }
682
683 return;
684 }
Chris Lattnerb35baae2010-03-08 21:08:07 +0000685
686 // If we're initializing the whole aggregate, just do it in place.
687 // FIXME: This is a hack around an AST bug (PR6537).
688 if (NumInitElements == 1 && E->getType() == E->getInit(0)->getType()) {
689 EmitInitializationToLValue(E->getInit(0),
690 LValue::MakeAddr(DestPtr, Qualifiers()),
691 E->getType());
692 return;
693 }
694
Mike Stump1eb44332009-09-09 15:08:12 +0000695
Chris Lattnerf81557c2008-04-04 18:42:16 +0000696 // Here we iterate over the fields; this makes it simpler to both
697 // default-initialize fields and skip over unnamed fields.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000698 for (RecordDecl::field_iterator Field = SD->field_begin(),
699 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000700 Field != FieldEnd; ++Field) {
701 // We're done once we hit the flexible array member
702 if (Field->getType()->isIncompleteArrayType())
703 break;
704
Douglas Gregor34e79462009-01-28 23:36:17 +0000705 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000706 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000707
Eli Friedman1e692ac2008-06-13 23:01:12 +0000708 // FIXME: volatility
Anders Carlssone78ccb42010-02-03 19:13:55 +0000709 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000710 // We never generate write-barries for initialized fields.
711 LValue::SetObjCNonGC(FieldLoc, true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000712 if (CurInitVal < NumInitElements) {
Chris Lattnerb35baae2010-03-08 21:08:07 +0000713 // Store the initializer into the field.
714 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
Anders Carlsson78e83f82010-02-03 17:33:16 +0000715 Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000716 } else {
717 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000718 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000719 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000720 }
Devang Patel636c3d02007-10-26 17:44:44 +0000721}
722
Chris Lattneree755f92007-08-21 04:59:27 +0000723//===----------------------------------------------------------------------===//
724// Entry Points into this File
725//===----------------------------------------------------------------------===//
726
Mike Stumpe1129a92009-05-26 18:57:45 +0000727/// EmitAggExpr - Emit the computation of the specified expression of aggregate
728/// type. The result is computed into DestPtr. Note that if DestPtr is null,
729/// the value of the aggregate expression is not needed. If VolatileDest is
730/// true, DestPtr cannot be 0.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000731//
732// FIXME: Take Qualifiers object.
Chris Lattneree755f92007-08-21 04:59:27 +0000733void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000734 bool VolatileDest, bool IgnoreResult,
Mike Stump1eb44332009-09-09 15:08:12 +0000735 bool IsInitializer,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000736 bool RequiresGCollection) {
Chris Lattneree755f92007-08-21 04:59:27 +0000737 assert(E && hasAggregateLLVMType(E->getType()) &&
738 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000739 assert ((DestPtr != 0 || VolatileDest == false)
740 && "volatile aggregate can't be 0");
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000742 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult, IsInitializer,
743 RequiresGCollection)
Mike Stump49d1cd52009-05-26 22:03:21 +0000744 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000745}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000746
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000747LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
748 assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
749 Qualifiers Q = MakeQualifiers(E->getType());
Daniel Dunbar195337d2010-02-09 02:48:28 +0000750 llvm::Value *Temp = CreateMemTemp(E->getType());
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000751 EmitAggExpr(E, Temp, Q.hasVolatile());
752 return LValue::MakeAddr(Temp, Q);
753}
754
Daniel Dunbar7482d122008-09-09 20:49:46 +0000755void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000756 llvm::Value *SrcPtr, QualType Ty,
757 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000758 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000760 if (getContext().getLangOptions().CPlusPlus) {
761 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Douglas Gregore9979482010-05-20 15:39:01 +0000762 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
763 assert((Record->hasTrivialCopyConstructor() ||
Fariborz Jahanian1d49f212010-05-20 16:46:55 +0000764 Record->hasTrivialCopyAssignment()) &&
Douglas Gregore9979482010-05-20 15:39:01 +0000765 "Trying to aggregate-copy a type without a trivial copy "
766 "constructor or assignment operator");
Douglas Gregor419aa962010-05-20 15:48:29 +0000767 // Ignore empty classes in C++.
Douglas Gregore9979482010-05-20 15:39:01 +0000768 if (Record->isEmpty())
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000769 return;
770 }
771 }
772
Chris Lattner83c96292009-02-28 18:31:01 +0000773 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000774 // C99 6.5.16.1p3, which states "If the value being stored in an object is
775 // read from another object that overlaps in anyway the storage of the first
776 // object, then the overlap shall be exact and the two objects shall have
777 // qualified or unqualified versions of a compatible type."
778 //
Chris Lattner83c96292009-02-28 18:31:01 +0000779 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000780 // equal, but other compilers do this optimization, and almost every memcpy
781 // implementation handles this case safely. If there is a libc that does not
782 // safely handle this, we can add a target hook.
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000783 const llvm::Type *BP = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar7482d122008-09-09 20:49:46 +0000784 if (DestPtr->getType() != BP)
785 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
786 if (SrcPtr->getType() != BP)
787 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Daniel Dunbar7482d122008-09-09 20:49:46 +0000789 // Get size and alignment info for this aggregate.
790 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Daniel Dunbar7482d122008-09-09 20:49:46 +0000792 // FIXME: Handle variable sized types.
Owen Anderson0032b272009-08-13 21:57:51 +0000793 const llvm::Type *IntPtr =
794 llvm::IntegerType::get(VMContext, LLVMPointerWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Mike Stumpfde64202009-05-23 04:13:59 +0000796 // FIXME: If we have a volatile struct, the optimizer can remove what might
797 // appear to be `extra' memory ops:
798 //
799 // volatile struct { int i; } a, b;
800 //
801 // int main() {
802 // a = b;
803 // a = b;
804 // }
805 //
Mon P Wang3ecd7852010-04-04 03:10:52 +0000806 // we need to use a different call here. We use isVolatile to indicate when
Mike Stump49d1cd52009-05-26 22:03:21 +0000807 // either the source or the destination is volatile.
Mon P Wang3ecd7852010-04-04 03:10:52 +0000808 const llvm::Type *I1Ty = llvm::Type::getInt1Ty(VMContext);
809 const llvm::Type *I8Ty = llvm::Type::getInt8Ty(VMContext);
810 const llvm::Type *I32Ty = llvm::Type::getInt32Ty(VMContext);
811
812 const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
813 const llvm::Type *DBP = llvm::PointerType::get(I8Ty, DPT->getAddressSpace());
814 if (DestPtr->getType() != DBP)
815 DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
816
817 const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
818 const llvm::Type *SBP = llvm::PointerType::get(I8Ty, SPT->getAddressSpace());
819 if (SrcPtr->getType() != SBP)
820 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
821
822 Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(),
823 IntPtr),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000824 DestPtr, SrcPtr,
825 // TypeInfo.first describes size in bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000826 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Mon P Wang3ecd7852010-04-04 03:10:52 +0000827 llvm::ConstantInt::get(I32Ty, TypeInfo.second/8),
828 llvm::ConstantInt::get(I1Ty, isVolatile));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000829}