blob: 99b46d41a812cc8f157670b2a82b2c92f2af128a [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;
John McCall558d2ab2010-09-15 10:14:12 +000035 AggValueSlot Dest;
Mike Stump49d1cd52009-05-26 22:03:21 +000036 bool IgnoreResult;
John McCallef072fd2010-05-22 01:48:05 +000037
38 ReturnValueSlot getReturnValueSlot() const {
John McCallfa037bd2010-05-22 22:13:32 +000039 // If the destination slot requires garbage collection, we can't
40 // use the real return value slot, because we have to use the GC
41 // API.
John McCalld1a5f132010-09-16 03:13:23 +000042 if (Dest.requiresGCollection()) return ReturnValueSlot();
John McCallfa037bd2010-05-22 22:13:32 +000043
John McCall558d2ab2010-09-15 10:14:12 +000044 return ReturnValueSlot(Dest.getAddr(), Dest.isVolatile());
45 }
46
47 AggValueSlot EnsureSlot(QualType T) {
48 if (!Dest.isIgnored()) return Dest;
49 return CGF.CreateAggTemp(T, "agg.tmp.ensured");
John McCallef072fd2010-05-22 01:48:05 +000050 }
John McCallfa037bd2010-05-22 22:13:32 +000051
Chris Lattner9c033562007-08-21 04:25:47 +000052public:
John McCall558d2ab2010-09-15 10:14:12 +000053 AggExprEmitter(CodeGenFunction &cgf, AggValueSlot Dest,
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +000054 bool ignore)
John McCall558d2ab2010-09-15 10:14:12 +000055 : CGF(cgf), Builder(CGF.Builder), Dest(Dest),
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +000056 IgnoreResult(ignore) {
Chris Lattner9c033562007-08-21 04:25:47 +000057 }
58
Chris Lattneree755f92007-08-21 04:59:27 +000059 //===--------------------------------------------------------------------===//
60 // Utilities
61 //===--------------------------------------------------------------------===//
62
Chris Lattner9c033562007-08-21 04:25:47 +000063 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
64 /// represents a value lvalue, this method emits the address of the lvalue,
65 /// then loads the result into DestPtr.
66 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000067
Mike Stump4ac20dd2009-05-23 20:28:01 +000068 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +000069 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
70 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump4ac20dd2009-05-23 20:28:01 +000071
John McCallfa037bd2010-05-22 22:13:32 +000072 void EmitGCMove(const Expr *E, RValue Src);
73
74 bool TypeRequiresGCollection(QualType T);
75
Chris Lattneree755f92007-08-21 04:59:27 +000076 //===--------------------------------------------------------------------===//
77 // Visitor Methods
78 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +000079
Chris Lattner9c033562007-08-21 04:25:47 +000080 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000081 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000082 }
83 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000084 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000085
86 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000087 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
88 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
89 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Daniel Dunbar5be028f2010-01-04 18:47:06 +000090 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000091 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000092 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000093 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000094 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
95 EmitAggLoadOfLValue(E);
96 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000097 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +000098 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +000099 }
100 void VisitPredefinedExpr(const PredefinedExpr *E) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 EmitAggLoadOfLValue(E);
Chris Lattnerf0a990c2009-04-21 23:00:09 +0000102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner9c033562007-08-21 04:25:47 +0000104 // Operators.
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000105 void VisitCastExpr(CastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +0000106 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000107 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000108 void VisitBinaryOperator(const BinaryOperator *BO);
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000109 void VisitPointerToDataMemberBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +0000110 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000111 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000112
Chris Lattner8fdf3282008-06-24 17:04:18 +0000113 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000114 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
115 EmitAggLoadOfLValue(E);
116 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000117 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000118 void VisitObjCImplicitSetterGetterRefExpr(ObjCImplicitSetterGetterRefExpr *E);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner9c033562007-08-21 04:25:47 +0000120 void VisitConditionalOperator(const ConditionalOperator *CO);
Anders Carlssona294ca82009-07-08 18:33:14 +0000121 void VisitChooseExpr(const ChooseExpr *CE);
Devang Patel636c3d02007-10-26 17:44:44 +0000122 void VisitInitListExpr(InitListExpr *E);
Anders Carlsson30311fa2009-12-16 06:57:54 +0000123 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000124 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
125 Visit(DAE->getExpr());
126 }
Anders Carlssonb58d0172009-05-30 23:23:33 +0000127 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
Anders Carlsson31ccf372009-05-03 17:47:16 +0000128 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000129 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
Douglas Gregored8abf12010-07-08 06:14:04 +0000130 void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
Mike Stump2710c412009-11-18 00:40:12 +0000131 void VisitCXXTypeidExpr(CXXTypeidExpr *E) { EmitAggLoadOfLValue(E); }
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000132
Eli Friedmanb1851242008-05-27 15:51:49 +0000133 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000134
Anders Carlsson78e83f82010-02-03 17:33:16 +0000135 void EmitInitializationToLValue(Expr *E, LValue Address, QualType T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000136 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000137 // case Expr::ChooseExprClass:
Mike Stump39406b12009-12-09 19:24:08 +0000138 void VisitCXXThrowExpr(const CXXThrowExpr *E) { CGF.EmitCXXThrowExpr(E); }
Chris Lattner9c033562007-08-21 04:25:47 +0000139};
140} // end anonymous namespace.
141
Chris Lattneree755f92007-08-21 04:59:27 +0000142//===----------------------------------------------------------------------===//
143// Utilities
144//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000145
Chris Lattner883f6a72007-08-11 00:04:45 +0000146/// EmitAggLoadOfLValue - Given an expression with aggregate type that
147/// represents a value lvalue, this method emits the address of the lvalue,
148/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000149void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
Fariborz Jahanian07ed93f2010-10-22 21:01:02 +0000150 if (CGF.getContext().getLangOptions().CPlusPlus) {
151 if (const CXXConstructExpr *CE = Dest.getCtorExpr()) {
152 // Perform copy initialization of Src into Dest.
153 const CXXConstructorDecl *CD = CE->getConstructor();
154 CXXCtorType Type =
155 (CE->getConstructionKind() == CXXConstructExpr::CK_Complete)
156 ? Ctor_Complete : Ctor_Base;
157 bool ForVirtualBase =
158 CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase;
159 // Call the constructor.
160 const Stmt * S = dyn_cast<Stmt>(E);
161 clang::ConstExprIterator BegExp(&S);
162 CGF.EmitCXXConstructorCall(CD, Type, ForVirtualBase, Dest.getAddr(),
163 BegExp, (BegExp+1));
164 return;
165 }
166 }
Chris Lattner9c033562007-08-21 04:25:47 +0000167 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000168 EmitFinalDestCopy(E, LV);
169}
170
John McCallfa037bd2010-05-22 22:13:32 +0000171/// \brief True if the given aggregate type requires special GC API calls.
172bool AggExprEmitter::TypeRequiresGCollection(QualType T) {
173 // Only record types have members that might require garbage collection.
174 const RecordType *RecordTy = T->getAs<RecordType>();
175 if (!RecordTy) return false;
176
177 // Don't mess with non-trivial C++ types.
178 RecordDecl *Record = RecordTy->getDecl();
179 if (isa<CXXRecordDecl>(Record) &&
180 (!cast<CXXRecordDecl>(Record)->hasTrivialCopyConstructor() ||
181 !cast<CXXRecordDecl>(Record)->hasTrivialDestructor()))
182 return false;
183
184 // Check whether the type has an object member.
185 return Record->hasObjectMember();
186}
187
188/// \brief Perform the final move to DestPtr if RequiresGCollection is set.
189///
190/// The idea is that you do something like this:
191/// RValue Result = EmitSomething(..., getReturnValueSlot());
192/// EmitGCMove(E, Result);
193/// If GC doesn't interfere, this will cause the result to be emitted
194/// directly into the return value slot. If GC does interfere, a final
195/// move will be performed.
196void AggExprEmitter::EmitGCMove(const Expr *E, RValue Src) {
John McCalld1a5f132010-09-16 03:13:23 +0000197 if (Dest.requiresGCollection()) {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000198 std::pair<uint64_t, unsigned> TypeInfo =
199 CGF.getContext().getTypeInfo(E->getType());
200 unsigned long size = TypeInfo.first/8;
201 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
202 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
John McCall558d2ab2010-09-15 10:14:12 +0000203 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF, Dest.getAddr(),
John McCallfa037bd2010-05-22 22:13:32 +0000204 Src.getAggregateAddr(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000205 SizeVal);
206 }
John McCallfa037bd2010-05-22 22:13:32 +0000207}
208
Mike Stump4ac20dd2009-05-23 20:28:01 +0000209/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000210void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000211 assert(Src.isAggregate() && "value must be aggregate value!");
212
John McCall558d2ab2010-09-15 10:14:12 +0000213 // If Dest is ignored, then we're evaluating an aggregate expression
John McCalla8f28da2010-08-25 02:50:31 +0000214 // in a context (like an expression statement) that doesn't care
215 // about the result. C says that an lvalue-to-rvalue conversion is
216 // performed in these cases; C++ says that it is not. In either
217 // case, we don't actually need to do anything unless the value is
218 // volatile.
John McCall558d2ab2010-09-15 10:14:12 +0000219 if (Dest.isIgnored()) {
John McCalla8f28da2010-08-25 02:50:31 +0000220 if (!Src.isVolatileQualified() ||
221 CGF.CGM.getLangOptions().CPlusPlus ||
222 (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000223 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000224 // If the source is volatile, we must read from it; to do that, we need
225 // some place to put it.
John McCall558d2ab2010-09-15 10:14:12 +0000226 Dest = CGF.CreateAggTemp(E->getType(), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000227 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000228
John McCalld1a5f132010-09-16 03:13:23 +0000229 if (Dest.requiresGCollection()) {
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000230 std::pair<uint64_t, unsigned> TypeInfo =
231 CGF.getContext().getTypeInfo(E->getType());
232 unsigned long size = TypeInfo.first/8;
233 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
234 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000235 CGF.CGM.getObjCRuntime().EmitGCMemmoveCollectable(CGF,
John McCall558d2ab2010-09-15 10:14:12 +0000236 Dest.getAddr(),
237 Src.getAggregateAddr(),
238 SizeVal);
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000239 return;
240 }
Fariborz Jahanian07ed93f2010-10-22 21:01:02 +0000241
Mike Stump4ac20dd2009-05-23 20:28:01 +0000242 // If the result of the assignment is used, copy the LHS there also.
243 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
244 // from the source as well, as we can't eliminate it if either operand
245 // is volatile, unless copy has volatile for both source and destination..
John McCall558d2ab2010-09-15 10:14:12 +0000246 CGF.EmitAggregateCopy(Dest.getAddr(), Src.getAggregateAddr(), E->getType(),
247 Dest.isVolatile()|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000248}
249
250/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000251void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000252 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
253
254 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000255 Src.isVolatileQualified()),
256 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000257}
258
Chris Lattneree755f92007-08-21 04:59:27 +0000259//===----------------------------------------------------------------------===//
260// Visitor Methods
261//===----------------------------------------------------------------------===//
262
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000263void AggExprEmitter::VisitCastExpr(CastExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000264 if (Dest.isIgnored() && E->getCastKind() != CK_Dynamic) {
Douglas Gregor4ce46c22010-03-07 23:24:59 +0000265 Visit(E->getSubExpr());
266 return;
267 }
268
Anders Carlsson30168422009-09-29 01:23:39 +0000269 switch (E->getCastKind()) {
270 default: assert(0 && "Unhandled cast kind!");
271
John McCall2de56d12010-08-25 11:45:40 +0000272 case CK_Dynamic: {
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000273 assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
274 LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
275 // FIXME: Do we also need to handle property references here?
276 if (LV.isSimple())
277 CGF.EmitDynamicCast(LV.getAddress(), cast<CXXDynamicCastExpr>(E));
278 else
279 CGF.CGM.ErrorUnsupported(E, "non-simple lvalue dynamic_cast");
280
John McCall558d2ab2010-09-15 10:14:12 +0000281 if (!Dest.isIgnored())
282 CGF.CGM.ErrorUnsupported(E, "lvalue dynamic_cast with a destination");
Douglas Gregor69cfeb12010-05-14 21:31:02 +0000283 break;
284 }
285
John McCall2de56d12010-08-25 11:45:40 +0000286 case CK_ToUnion: {
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000287 // GCC union extension
Daniel Dunbar79c39282010-08-21 03:15:20 +0000288 QualType Ty = E->getSubExpr()->getType();
289 QualType PtrTy = CGF.getContext().getPointerType(Ty);
John McCall558d2ab2010-09-15 10:14:12 +0000290 llvm::Value *CastPtr = Builder.CreateBitCast(Dest.getAddr(),
Eli Friedman34ebf4d2009-06-03 20:45:06 +0000291 CGF.ConvertType(PtrTy));
Daniel Dunbar79c39282010-08-21 03:15:20 +0000292 EmitInitializationToLValue(E->getSubExpr(), CGF.MakeAddrLValue(CastPtr, Ty),
293 Ty);
Anders Carlsson30168422009-09-29 01:23:39 +0000294 break;
Nuno Lopes7e916272009-01-15 20:14:33 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
John McCall2de56d12010-08-25 11:45:40 +0000297 case CK_DerivedToBase:
298 case CK_BaseToDerived:
299 case CK_UncheckedDerivedToBase: {
Douglas Gregor2d6b0e92010-05-22 05:17:18 +0000300 assert(0 && "cannot perform hierarchy conversion in EmitAggExpr: "
301 "should have been unpacked before we got here");
302 break;
303 }
304
Anders Carlsson4d8673b2009-08-07 23:22:37 +0000305 // FIXME: Remove the CK_Unknown check here.
John McCall2de56d12010-08-25 11:45:40 +0000306 case CK_Unknown:
307 case CK_NoOp:
308 case CK_UserDefinedConversion:
309 case CK_ConstructorConversion:
Anders Carlsson30168422009-09-29 01:23:39 +0000310 assert(CGF.getContext().hasSameUnqualifiedType(E->getSubExpr()->getType(),
311 E->getType()) &&
312 "Implicit cast types must be compatible");
313 Visit(E->getSubExpr());
314 break;
Anders Carlssone9d34dc2009-09-29 02:09:01 +0000315
John McCall2de56d12010-08-25 11:45:40 +0000316 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000317 llvm_unreachable("there are no lvalue bit-casts on aggregates");
318 break;
Anders Carlsson30168422009-09-29 01:23:39 +0000319 }
Anders Carlssone4707ff2008-01-14 06:28:57 +0000320}
321
Chris Lattner96196622008-07-26 22:37:01 +0000322void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000323 if (E->getCallReturnType()->isReferenceType()) {
324 EmitAggLoadOfLValue(E);
325 return;
326 }
Mike Stump1eb44332009-09-09 15:08:12 +0000327
John McCallfa037bd2010-05-22 22:13:32 +0000328 RValue RV = CGF.EmitCallExpr(E, getReturnValueSlot());
329 EmitGCMove(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000330}
Chris Lattner96196622008-07-26 22:37:01 +0000331
332void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000333 RValue RV = CGF.EmitObjCMessageExpr(E, getReturnValueSlot());
334 EmitGCMove(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000335}
Anders Carlsson148fe672007-10-31 22:04:46 +0000336
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000337void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000338 RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot());
339 EmitGCMove(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000340}
341
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000342void AggExprEmitter::VisitObjCImplicitSetterGetterRefExpr(
343 ObjCImplicitSetterGetterRefExpr *E) {
John McCallfa037bd2010-05-22 22:13:32 +0000344 RValue RV = CGF.EmitObjCPropertyGet(E, getReturnValueSlot());
345 EmitGCMove(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000346}
347
Chris Lattner96196622008-07-26 22:37:01 +0000348void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000349 CGF.EmitAnyExpr(E->getLHS(), AggValueSlot::ignored(), true);
350 Visit(E->getRHS());
Eli Friedman07fa52a2008-05-20 07:56:31 +0000351}
352
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000353void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000354 CGF.EmitCompoundStmt(*E->getSubStmt(), true, Dest);
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000355}
356
Chris Lattner9c033562007-08-21 04:25:47 +0000357void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
John McCall2de56d12010-08-25 11:45:40 +0000358 if (E->getOpcode() == BO_PtrMemD || E->getOpcode() == BO_PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +0000359 VisitPointerToDataMemberBinaryOperator(E);
360 else
361 CGF.ErrorUnsupported(E, "aggregate binary expression");
362}
363
364void AggExprEmitter::VisitPointerToDataMemberBinaryOperator(
365 const BinaryOperator *E) {
366 LValue LV = CGF.EmitPointerToDataMemberBinaryExpr(E);
367 EmitFinalDestCopy(E, LV);
Chris Lattneree755f92007-08-21 04:59:27 +0000368}
369
Chris Lattner03d6fb92007-08-21 04:43:17 +0000370void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000371 // For an assignment to work, the value on the right has
372 // to be compatible with the value on the left.
Eli Friedman2dce5f82009-05-28 23:04:00 +0000373 assert(CGF.getContext().hasSameUnqualifiedType(E->getLHS()->getType(),
374 E->getRHS()->getType())
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000375 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000376 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000377
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000378 // We have to special case property setters, otherwise we must have
379 // a simple lvalue (no aggregates inside vectors, bitfields).
380 if (LHS.isPropertyRef()) {
John McCall558d2ab2010-09-15 10:14:12 +0000381 AggValueSlot Slot = EnsureSlot(E->getRHS()->getType());
382 CGF.EmitAggExpr(E->getRHS(), Slot);
383 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(), Slot.asRValue());
Mike Stumpb3589f42009-07-30 22:28:39 +0000384 } else if (LHS.isKVCRef()) {
John McCall558d2ab2010-09-15 10:14:12 +0000385 AggValueSlot Slot = EnsureSlot(E->getRHS()->getType());
386 CGF.EmitAggExpr(E->getRHS(), Slot);
387 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(), Slot.asRValue());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000388 } else {
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000389 bool GCollection = false;
John McCallfa037bd2010-05-22 22:13:32 +0000390 if (CGF.getContext().getLangOptions().getGCMode())
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000391 GCollection = TypeRequiresGCollection(E->getLHS()->getType());
John McCallfa037bd2010-05-22 22:13:32 +0000392
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000393 // Codegen the RHS so that it stores directly into the LHS.
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000394 AggValueSlot LHSSlot = AggValueSlot::forLValue(LHS, true,
395 GCollection);
396 CGF.EmitAggExpr(E->getRHS(), LHSSlot, false);
Mike Stump49d1cd52009-05-26 22:03:21 +0000397 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000398 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000399}
400
Chris Lattner9c033562007-08-21 04:25:47 +0000401void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Eli Friedman8e274bd2009-12-25 06:17:05 +0000402 if (!E->getLHS()) {
403 CGF.ErrorUnsupported(E, "conditional operator with missing LHS");
404 return;
405 }
406
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000407 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
408 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
409 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Eli Friedman8e274bd2009-12-25 06:17:05 +0000411 CGF.EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Anders Carlsson72119a82010-02-04 17:18:07 +0000413 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000414 CGF.EmitBlock(LHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattner883f6a72007-08-11 00:04:45 +0000416 // Handle the GNU extension for missing LHS.
417 assert(E->getLHS() && "Must have LHS for aggregate value");
418
Chris Lattnerc748f272007-08-21 05:02:10 +0000419 Visit(E->getLHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000420 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000421 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Anders Carlsson72119a82010-02-04 17:18:07 +0000423 CGF.BeginConditionalBranch();
Chris Lattner9c033562007-08-21 04:25:47 +0000424 CGF.EmitBlock(RHSBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattnerc748f272007-08-21 05:02:10 +0000426 Visit(E->getRHS());
Anders Carlsson72119a82010-02-04 17:18:07 +0000427 CGF.EndConditionalBranch();
Daniel Dunbard57a8712008-11-11 09:41:28 +0000428 CGF.EmitBranch(ContBlock);
Mike Stump1eb44332009-09-09 15:08:12 +0000429
Chris Lattner9c033562007-08-21 04:25:47 +0000430 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000431}
Chris Lattneree755f92007-08-21 04:59:27 +0000432
Anders Carlssona294ca82009-07-08 18:33:14 +0000433void AggExprEmitter::VisitChooseExpr(const ChooseExpr *CE) {
434 Visit(CE->getChosenSubExpr(CGF.getContext()));
435}
436
Eli Friedmanb1851242008-05-27 15:51:49 +0000437void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000438 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000439 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
440
Sebastian Redl0262f022009-01-09 21:09:38 +0000441 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000442 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000443 return;
444 }
445
Daniel Dunbar79c39282010-08-21 03:15:20 +0000446 EmitFinalDestCopy(VE, CGF.MakeAddrLValue(ArgPtr, VE->getType()));
Eli Friedmanb1851242008-05-27 15:51:49 +0000447}
448
Anders Carlssonb58d0172009-05-30 23:23:33 +0000449void AggExprEmitter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000450 // Ensure that we have a slot, but if we already do, remember
451 // whether its lifetime was externally managed.
452 bool WasManaged = Dest.isLifetimeExternallyManaged();
453 Dest = EnsureSlot(E->getType());
454 Dest.setLifetimeExternallyManaged();
Mike Stump1eb44332009-09-09 15:08:12 +0000455
John McCall558d2ab2010-09-15 10:14:12 +0000456 Visit(E->getSubExpr());
Anders Carlssonb58d0172009-05-30 23:23:33 +0000457
John McCall558d2ab2010-09-15 10:14:12 +0000458 // Set up the temporary's destructor if its lifetime wasn't already
459 // being managed.
460 if (!WasManaged)
461 CGF.EmitCXXTemporary(E->getTemporary(), Dest.getAddr());
Anders Carlssonb58d0172009-05-30 23:23:33 +0000462}
463
Anders Carlssonb14095a2009-04-17 00:06:03 +0000464void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000465AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000466 AggValueSlot Slot = EnsureSlot(E->getType());
467 CGF.EmitCXXConstructExpr(E, Slot);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000468}
469
470void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000471 CGF.EmitCXXExprWithTemporaries(E, Dest);
Anders Carlssonb14095a2009-04-17 00:06:03 +0000472}
473
Douglas Gregored8abf12010-07-08 06:14:04 +0000474void AggExprEmitter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000475 QualType T = E->getType();
476 AggValueSlot Slot = EnsureSlot(T);
477 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
Anders Carlsson30311fa2009-12-16 06:57:54 +0000478}
479
480void AggExprEmitter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
John McCall558d2ab2010-09-15 10:14:12 +0000481 QualType T = E->getType();
482 AggValueSlot Slot = EnsureSlot(T);
483 EmitNullInitializationToLValue(CGF.MakeAddrLValue(Slot.getAddr(), T), T);
Nuno Lopes329763b2009-10-18 15:18:11 +0000484}
485
Anders Carlsson78e83f82010-02-03 17:33:16 +0000486void
487AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV, QualType T) {
Mike Stump7f79f9b2009-05-29 15:46:01 +0000488 // FIXME: Ignore result?
Chris Lattnerf81557c2008-04-04 18:42:16 +0000489 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000490 if (isa<ImplicitValueInitExpr>(E)) {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000491 EmitNullInitializationToLValue(LV, T);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000492 } else if (T->isReferenceType()) {
Anders Carlsson32f36ba2010-06-26 16:35:32 +0000493 RValue RV = CGF.EmitReferenceBindingToExpr(E, /*InitializedDecl=*/0);
Anders Carlssone78ccb42010-02-03 19:13:55 +0000494 CGF.EmitStoreThroughLValue(RV, LV, T);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000495 } else if (T->isAnyComplexType()) {
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000496 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Anders Carlsson78e83f82010-02-03 17:33:16 +0000497 } else if (CGF.hasAggregateLLVMType(T)) {
John McCall558d2ab2010-09-15 10:14:12 +0000498 CGF.EmitAggExpr(E, AggValueSlot::forAddr(LV.getAddress(), false, true));
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000499 } else {
Anders Carlsson78e83f82010-02-03 17:33:16 +0000500 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000501 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000502}
503
504void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
505 if (!CGF.hasAggregateLLVMType(T)) {
506 // For non-aggregates, we can store zero
Owen Andersonc9c88b42009-07-31 20:28:54 +0000507 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
Daniel Dunbar82397132008-08-06 05:32:55 +0000508 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000509 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000510 // There's a potential optimization opportunity in combining
511 // memsets; that would be easy for arrays, but relatively
512 // difficult for structures with the current code.
Anders Carlsson1884eb02010-05-22 17:35:42 +0000513 CGF.EmitNullInitialization(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000514 }
515}
516
Chris Lattnerf81557c2008-04-04 18:42:16 +0000517void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000518#if 0
Eli Friedman13a5be12009-12-04 01:30:56 +0000519 // FIXME: Assess perf here? Figure out what cases are worth optimizing here
520 // (Length of globals? Chunks of zeroed-out space?).
Eli Friedmana385b3c2008-12-02 01:17:45 +0000521 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000522 // If we can, prefer a copy from a global; this is a lot less code for long
523 // globals, and it's easier for the current optimizers to analyze.
Eli Friedman13a5be12009-12-04 01:30:56 +0000524 if (llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, E->getType(), &CGF)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000525 llvm::GlobalVariable* GV =
Eli Friedman13a5be12009-12-04 01:30:56 +0000526 new llvm::GlobalVariable(CGF.CGM.getModule(), C->getType(), true,
527 llvm::GlobalValue::InternalLinkage, C, "");
Daniel Dunbar79c39282010-08-21 03:15:20 +0000528 EmitFinalDestCopy(E, CGF.MakeAddrLValue(GV, E->getType()));
Eli Friedman994ffef2008-11-30 02:11:09 +0000529 return;
530 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000531#endif
Chris Lattnerd0db03a2010-09-06 00:11:41 +0000532 if (E->hadArrayRangeDesignator())
Douglas Gregora9c87802009-01-29 19:42:23 +0000533 CGF.ErrorUnsupported(E, "GNU array range designator extension");
Douglas Gregora9c87802009-01-29 19:42:23 +0000534
John McCall558d2ab2010-09-15 10:14:12 +0000535 llvm::Value *DestPtr = Dest.getAddr();
536
Chris Lattnerf81557c2008-04-04 18:42:16 +0000537 // Handle initialization of an array.
538 if (E->getType()->isArrayType()) {
539 const llvm::PointerType *APType =
540 cast<llvm::PointerType>(DestPtr->getType());
541 const llvm::ArrayType *AType =
542 cast<llvm::ArrayType>(APType->getElementType());
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerf81557c2008-04-04 18:42:16 +0000544 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000545
Chris Lattner96196622008-07-26 22:37:01 +0000546 if (E->getNumInits() > 0) {
547 QualType T1 = E->getType();
548 QualType T2 = E->getInit(0)->getType();
Eli Friedman2dce5f82009-05-28 23:04:00 +0000549 if (CGF.getContext().hasSameUnqualifiedType(T1, T2)) {
Chris Lattner96196622008-07-26 22:37:01 +0000550 EmitAggLoadOfLValue(E->getInit(0));
551 return;
552 }
Eli Friedman922696f2008-05-19 17:51:16 +0000553 }
554
Chris Lattnerf81557c2008-04-04 18:42:16 +0000555 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000556 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000557 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +0000558
John McCall0953e762009-09-24 19:53:00 +0000559 // FIXME: were we intentionally ignoring address spaces and GC attributes?
Eli Friedman1e692ac2008-06-13 23:01:12 +0000560
Chris Lattnerf81557c2008-04-04 18:42:16 +0000561 for (uint64_t i = 0; i != NumArrayElements; ++i) {
562 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Daniel Dunbar9f553f52010-08-21 03:08:16 +0000563 LValue LV = CGF.MakeAddrLValue(NextVal, ElementType);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000564 if (i < NumInitElements)
Daniel Dunbar9f553f52010-08-21 03:08:16 +0000565 EmitInitializationToLValue(E->getInit(i), LV, ElementType);
566
Chris Lattnerf81557c2008-04-04 18:42:16 +0000567 else
Daniel Dunbar9f553f52010-08-21 03:08:16 +0000568 EmitNullInitializationToLValue(LV, ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000569 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000570 return;
571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Chris Lattnerf81557c2008-04-04 18:42:16 +0000573 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Chris Lattnerf81557c2008-04-04 18:42:16 +0000575 // Do struct initialization; this code just sets each individual member
576 // to the approprate value. This makes bitfield support automatic;
577 // the disadvantage is that the generated code is more difficult for
578 // the optimizer, especially with bitfields.
579 unsigned NumInitElements = E->getNumInits();
Ted Kremenek6217b802009-07-29 21:53:49 +0000580 RecordDecl *SD = E->getType()->getAs<RecordType>()->getDecl();
Chris Lattnerbd7de382010-09-06 00:13:11 +0000581
Douglas Gregor0bb76892009-01-29 16:53:55 +0000582 if (E->getType()->isUnionType()) {
583 // Only initialize one field of a union. The field itself is
584 // specified by the initializer list.
585 if (!E->getInitializedFieldInUnion()) {
586 // Empty union; we have nothing to do.
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Douglas Gregor0bb76892009-01-29 16:53:55 +0000588#ifndef NDEBUG
589 // Make sure that it's really an empty and not a failure of
590 // semantic analysis.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000591 for (RecordDecl::field_iterator Field = SD->field_begin(),
592 FieldEnd = SD->field_end();
Douglas Gregor0bb76892009-01-29 16:53:55 +0000593 Field != FieldEnd; ++Field)
594 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
595#endif
596 return;
597 }
598
599 // FIXME: volatility
600 FieldDecl *Field = E->getInitializedFieldInUnion();
Anders Carlssone78ccb42010-02-03 19:13:55 +0000601 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, Field, 0);
Douglas Gregor0bb76892009-01-29 16:53:55 +0000602
603 if (NumInitElements) {
604 // Store the initializer into the field
Anders Carlsson78e83f82010-02-03 17:33:16 +0000605 EmitInitializationToLValue(E->getInit(0), FieldLoc, Field->getType());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000606 } else {
607 // Default-initialize to null
608 EmitNullInitializationToLValue(FieldLoc, Field->getType());
609 }
610
611 return;
612 }
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Chris Lattnerf81557c2008-04-04 18:42:16 +0000614 // Here we iterate over the fields; this makes it simpler to both
615 // default-initialize fields and skip over unnamed fields.
Chris Lattnerbd7de382010-09-06 00:13:11 +0000616 unsigned CurInitVal = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000617 for (RecordDecl::field_iterator Field = SD->field_begin(),
618 FieldEnd = SD->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000619 Field != FieldEnd; ++Field) {
620 // We're done once we hit the flexible array member
621 if (Field->getType()->isIncompleteArrayType())
622 break;
623
Douglas Gregor34e79462009-01-28 23:36:17 +0000624 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000625 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000626
Eli Friedman1e692ac2008-06-13 23:01:12 +0000627 // FIXME: volatility
Anders Carlssone78ccb42010-02-03 19:13:55 +0000628 LValue FieldLoc = CGF.EmitLValueForFieldInitialization(DestPtr, *Field, 0);
Fariborz Jahanian14674ff2009-05-27 19:54:11 +0000629 // We never generate write-barries for initialized fields.
Daniel Dunbarea619172010-08-21 03:22:38 +0000630 FieldLoc.setNonGC(true);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000631 if (CurInitVal < NumInitElements) {
Chris Lattnerb35baae2010-03-08 21:08:07 +0000632 // Store the initializer into the field.
633 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc,
Anders Carlsson78e83f82010-02-03 17:33:16 +0000634 Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000635 } else {
636 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000637 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000638 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000639 }
Devang Patel636c3d02007-10-26 17:44:44 +0000640}
641
Chris Lattneree755f92007-08-21 04:59:27 +0000642//===----------------------------------------------------------------------===//
643// Entry Points into this File
644//===----------------------------------------------------------------------===//
645
Mike Stumpe1129a92009-05-26 18:57:45 +0000646/// EmitAggExpr - Emit the computation of the specified expression of aggregate
647/// type. The result is computed into DestPtr. Note that if DestPtr is null,
648/// the value of the aggregate expression is not needed. If VolatileDest is
649/// true, DestPtr cannot be 0.
John McCall558d2ab2010-09-15 10:14:12 +0000650///
651/// \param IsInitializer - true if this evaluation is initializing an
652/// object whose lifetime is already being managed.
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000653//
654// FIXME: Take Qualifiers object.
John McCall558d2ab2010-09-15 10:14:12 +0000655void CodeGenFunction::EmitAggExpr(const Expr *E, AggValueSlot Slot,
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000656 bool IgnoreResult) {
Chris Lattneree755f92007-08-21 04:59:27 +0000657 assert(E && hasAggregateLLVMType(E->getType()) &&
658 "Invalid aggregate expression to emit");
John McCall558d2ab2010-09-15 10:14:12 +0000659 assert((Slot.getAddr() != 0 || Slot.isIgnored())
660 && "slot has bits but no address");
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Fariborz Jahanian474e2fe2010-09-16 00:20:07 +0000662 AggExprEmitter(*this, Slot, IgnoreResult)
Mike Stump49d1cd52009-05-26 22:03:21 +0000663 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000664}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000665
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000666LValue CodeGenFunction::EmitAggExprToLValue(const Expr *E) {
667 assert(hasAggregateLLVMType(E->getType()) && "Invalid argument!");
Daniel Dunbar195337d2010-02-09 02:48:28 +0000668 llvm::Value *Temp = CreateMemTemp(E->getType());
Daniel Dunbar79c39282010-08-21 03:15:20 +0000669 LValue LV = MakeAddrLValue(Temp, E->getType());
John McCall558d2ab2010-09-15 10:14:12 +0000670 AggValueSlot Slot
671 = AggValueSlot::forAddr(Temp, LV.isVolatileQualified(), false);
672 EmitAggExpr(E, Slot);
Daniel Dunbar79c39282010-08-21 03:15:20 +0000673 return LV;
Daniel Dunbar18aba0d2010-02-05 19:38:31 +0000674}
675
Daniel Dunbar7482d122008-09-09 20:49:46 +0000676void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000677 llvm::Value *SrcPtr, QualType Ty,
678 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000679 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000681 if (getContext().getLangOptions().CPlusPlus) {
682 if (const RecordType *RT = Ty->getAs<RecordType>()) {
Douglas Gregore9979482010-05-20 15:39:01 +0000683 CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
684 assert((Record->hasTrivialCopyConstructor() ||
Fariborz Jahanian1d49f212010-05-20 16:46:55 +0000685 Record->hasTrivialCopyAssignment()) &&
Douglas Gregore9979482010-05-20 15:39:01 +0000686 "Trying to aggregate-copy a type without a trivial copy "
687 "constructor or assignment operator");
Douglas Gregor419aa962010-05-20 15:48:29 +0000688 // Ignore empty classes in C++.
Douglas Gregore9979482010-05-20 15:39:01 +0000689 if (Record->isEmpty())
Anders Carlsson0d7c5832010-05-03 01:20:20 +0000690 return;
691 }
692 }
693
Chris Lattner83c96292009-02-28 18:31:01 +0000694 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000695 // C99 6.5.16.1p3, which states "If the value being stored in an object is
696 // read from another object that overlaps in anyway the storage of the first
697 // object, then the overlap shall be exact and the two objects shall have
698 // qualified or unqualified versions of a compatible type."
699 //
Chris Lattner83c96292009-02-28 18:31:01 +0000700 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000701 // equal, but other compilers do this optimization, and almost every memcpy
702 // implementation handles this case safely. If there is a libc that does not
703 // safely handle this, we can add a target hook.
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Daniel Dunbar7482d122008-09-09 20:49:46 +0000705 // Get size and alignment info for this aggregate.
706 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Daniel Dunbar7482d122008-09-09 20:49:46 +0000708 // FIXME: Handle variable sized types.
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Mike Stumpfde64202009-05-23 04:13:59 +0000710 // FIXME: If we have a volatile struct, the optimizer can remove what might
711 // appear to be `extra' memory ops:
712 //
713 // volatile struct { int i; } a, b;
714 //
715 // int main() {
716 // a = b;
717 // a = b;
718 // }
719 //
Mon P Wang3ecd7852010-04-04 03:10:52 +0000720 // we need to use a different call here. We use isVolatile to indicate when
Mike Stump49d1cd52009-05-26 22:03:21 +0000721 // either the source or the destination is volatile.
Mon P Wang3ecd7852010-04-04 03:10:52 +0000722
723 const llvm::PointerType *DPT = cast<llvm::PointerType>(DestPtr->getType());
Chris Lattner098432c2010-07-08 00:07:45 +0000724 const llvm::Type *DBP =
725 llvm::Type::getInt8PtrTy(VMContext, DPT->getAddressSpace());
726 DestPtr = Builder.CreateBitCast(DestPtr, DBP, "tmp");
Mon P Wang3ecd7852010-04-04 03:10:52 +0000727
728 const llvm::PointerType *SPT = cast<llvm::PointerType>(SrcPtr->getType());
Chris Lattner098432c2010-07-08 00:07:45 +0000729 const llvm::Type *SBP =
730 llvm::Type::getInt8PtrTy(VMContext, SPT->getAddressSpace());
731 SrcPtr = Builder.CreateBitCast(SrcPtr, SBP, "tmp");
Mon P Wang3ecd7852010-04-04 03:10:52 +0000732
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000733 if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
734 RecordDecl *Record = RecordTy->getDecl();
735 if (Record->hasObjectMember()) {
736 unsigned long size = TypeInfo.first/8;
737 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
738 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
739 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
740 SizeVal);
741 return;
742 }
743 } else if (getContext().getAsArrayType(Ty)) {
744 QualType BaseType = getContext().getBaseElementType(Ty);
745 if (const RecordType *RecordTy = BaseType->getAs<RecordType>()) {
746 if (RecordTy->getDecl()->hasObjectMember()) {
747 unsigned long size = TypeInfo.first/8;
748 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
749 llvm::Value *SizeVal = llvm::ConstantInt::get(SizeTy, size);
750 CGM.getObjCRuntime().EmitGCMemmoveCollectable(*this, DestPtr, SrcPtr,
751 SizeVal);
752 return;
753 }
754 }
755 }
756
Mon P Wang3ecd7852010-04-04 03:10:52 +0000757 Builder.CreateCall5(CGM.getMemCpyFn(DestPtr->getType(), SrcPtr->getType(),
Chris Lattner77b89b82010-06-27 07:15:29 +0000758 IntPtrTy),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000759 DestPtr, SrcPtr,
760 // TypeInfo.first describes size in bits.
Chris Lattner77b89b82010-06-27 07:15:29 +0000761 llvm::ConstantInt::get(IntPtrTy, TypeInfo.first/8),
Chris Lattner098432c2010-07-08 00:07:45 +0000762 Builder.getInt32(TypeInfo.second/8),
763 Builder.getInt1(isVolatile));
Daniel Dunbar7482d122008-09-09 20:49:46 +0000764}