blob: 095e2240cf626b4fd4caa77e2b5df3552436b7f5 [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"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000018#include "llvm/Constants.h"
19#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000020#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000021#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000022#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000023using namespace clang;
24using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000025
Chris Lattner9c033562007-08-21 04:25:47 +000026//===----------------------------------------------------------------------===//
27// Aggregate Expression Emitter
28//===----------------------------------------------------------------------===//
29
30namespace {
31class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
32 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000033 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000034 llvm::Value *DestPtr;
35 bool VolatileDest;
36public:
37 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000038 : CGF(cgf), Builder(CGF.Builder),
39 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000040 }
41
Chris Lattneree755f92007-08-21 04:59:27 +000042 //===--------------------------------------------------------------------===//
43 // Utilities
44 //===--------------------------------------------------------------------===//
45
Chris Lattner9c033562007-08-21 04:25:47 +000046 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
47 /// represents a value lvalue, this method emits the address of the lvalue,
48 /// then loads the result into DestPtr.
49 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000050
Chris Lattneree755f92007-08-21 04:59:27 +000051 //===--------------------------------------------------------------------===//
52 // Visitor Methods
53 //===--------------------------------------------------------------------===//
54
Chris Lattner9c033562007-08-21 04:25:47 +000055 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000056 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000057 }
58 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000059 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000060
61 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000062 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
63 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
64 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000065 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Eli Friedmanb1851242008-05-27 15:51:49 +000066 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E)
67 { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000068
69 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
70 EmitAggLoadOfLValue(E);
71 }
Douglas Gregor4c678342009-01-28 21:54:33 +000072
Mike Stumpf8575aa2009-03-18 15:54:29 +000073 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E)
74 { EmitAggLoadOfLValue(E); }
75
Chris Lattner9c033562007-08-21 04:25:47 +000076 // Operators.
77 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000078 // case Expr::CastExprClass:
Nuno Lopes7e916272009-01-15 20:14:33 +000079 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000080 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000081 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000082 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000083 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000084 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000085 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000086
Chris Lattner8fdf3282008-06-24 17:04:18 +000087 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000088 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
89 EmitAggLoadOfLValue(E);
90 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +000091 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +000092 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000093
94 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000095 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +000096 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
97 Visit(DAE->getExpr());
98 }
Eli Friedmanb1851242008-05-27 15:51:49 +000099 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000100
101 void EmitInitializationToLValue(Expr *E, LValue Address);
102 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000103 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000104
Chris Lattner9c033562007-08-21 04:25:47 +0000105};
106} // end anonymous namespace.
107
Chris Lattneree755f92007-08-21 04:59:27 +0000108//===----------------------------------------------------------------------===//
109// Utilities
110//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000111
Chris Lattner883f6a72007-08-11 00:04:45 +0000112/// EmitAggLoadOfLValue - Given an expression with aggregate type that
113/// represents a value lvalue, this method emits the address of the lvalue,
114/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000115void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
116 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000117 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
118 llvm::Value *SrcPtr = LV.getAddress();
119
120 // If the result is ignored, don't copy from the value.
121 if (DestPtr == 0)
122 // FIXME: If the source is volatile, we must read from it.
123 return;
124
Daniel Dunbar7482d122008-09-09 20:49:46 +0000125 CGF.EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000126}
127
Chris Lattneree755f92007-08-21 04:59:27 +0000128//===----------------------------------------------------------------------===//
129// Visitor Methods
130//===----------------------------------------------------------------------===//
131
Nuno Lopes7e916272009-01-15 20:14:33 +0000132void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
133 // GCC union extension
134 if (E->getType()->isUnionType()) {
135 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
136 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
137 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
138 return;
139 }
140
141 Visit(E->getSubExpr());
142}
143
Chris Lattner96196622008-07-26 22:37:01 +0000144void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000145 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000146 E->getSubExpr()->getType().getUnqualifiedType(),
147 E->getType().getUnqualifiedType()) &&
148 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000149 Visit(E->getSubExpr());
150}
151
Chris Lattner96196622008-07-26 22:37:01 +0000152void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlsson148fe672007-10-31 22:04:46 +0000153 RValue RV = CGF.EmitCallExpr(E);
154 assert(RV.isAggregate() && "Return value must be aggregate value!");
155
156 // If the result is ignored, don't copy from the value.
157 if (DestPtr == 0)
158 // FIXME: If the source is volatile, we must read from it.
159 return;
160
Daniel Dunbar7482d122008-09-09 20:49:46 +0000161 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Anders Carlsson148fe672007-10-31 22:04:46 +0000162}
Chris Lattner96196622008-07-26 22:37:01 +0000163
164void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000165 RValue RV = CGF.EmitObjCMessageExpr(E);
166 assert(RV.isAggregate() && "Return value must be aggregate value!");
Chris Lattner8fdf3282008-06-24 17:04:18 +0000167
168 // If the result is ignored, don't copy from the value.
169 if (DestPtr == 0)
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000170 // FIXME: If the source is volatile, we must read from it.
Chris Lattner8fdf3282008-06-24 17:04:18 +0000171 return;
172
Daniel Dunbar7482d122008-09-09 20:49:46 +0000173 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000174}
Anders Carlsson148fe672007-10-31 22:04:46 +0000175
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000176void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
177 RValue RV = CGF.EmitObjCPropertyGet(E);
178 assert(RV.isAggregate() && "Return value must be aggregate value!");
179
180 // If the result is ignored, don't copy from the value.
181 if (DestPtr == 0)
182 // FIXME: If the source is volatile, we must read from it.
183 return;
184
Daniel Dunbar7482d122008-09-09 20:49:46 +0000185 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000186}
187
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000188void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
189 RValue RV = CGF.EmitObjCPropertyGet(E);
190 assert(RV.isAggregate() && "Return value must be aggregate value!");
191
192 // If the result is ignored, don't copy from the value.
193 if (DestPtr == 0)
194 // FIXME: If the source is volatile, we must read from it.
195 return;
196
197 CGF.EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
198}
199
Chris Lattner96196622008-07-26 22:37:01 +0000200void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Eli Friedman07fa52a2008-05-20 07:56:31 +0000201 CGF.EmitAnyExpr(E->getLHS());
202 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
203}
204
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000205void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
206 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
207}
208
Chris Lattner9c033562007-08-21 04:25:47 +0000209void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000210 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000211}
212
Chris Lattner03d6fb92007-08-21 04:43:17 +0000213void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000214 // For an assignment to work, the value on the right has
215 // to be compatible with the value on the left.
216 assert(CGF.getContext().typesAreCompatible(
217 E->getLHS()->getType().getUnqualifiedType(),
218 E->getRHS()->getType().getUnqualifiedType())
219 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000220 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000221
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000222 // We have to special case property setters, otherwise we must have
223 // a simple lvalue (no aggregates inside vectors, bitfields).
224 if (LHS.isPropertyRef()) {
225 // FIXME: Volatility?
226 llvm::Value *AggLoc = DestPtr;
227 if (!AggLoc)
228 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
229 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
230 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
231 RValue::getAggregate(AggLoc));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000232 }
233 else if (LHS.isKVCRef()) {
234 // FIXME: Volatility?
235 llvm::Value *AggLoc = DestPtr;
236 if (!AggLoc)
237 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
238 CGF.EmitAggExpr(E->getRHS(), AggLoc, false);
239 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
240 RValue::getAggregate(AggLoc));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000241 } else {
242 // Codegen the RHS so that it stores directly into the LHS.
243 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
244
245 if (DestPtr == 0)
246 return;
247
248 // If the result of the assignment is used, copy the RHS there also.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000249 CGF.EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000250 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000251}
252
Chris Lattner9c033562007-08-21 04:25:47 +0000253void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000254 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
255 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
256 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000257
Chris Lattner9c033562007-08-21 04:25:47 +0000258 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000259 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000260
Chris Lattner9c033562007-08-21 04:25:47 +0000261 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000262
263 // Handle the GNU extension for missing LHS.
264 assert(E->getLHS() && "Must have LHS for aggregate value");
265
Chris Lattnerc748f272007-08-21 05:02:10 +0000266 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000267 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000268
Chris Lattner9c033562007-08-21 04:25:47 +0000269 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000270
Chris Lattnerc748f272007-08-21 05:02:10 +0000271 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000272 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000273
Chris Lattner9c033562007-08-21 04:25:47 +0000274 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000275}
Chris Lattneree755f92007-08-21 04:59:27 +0000276
Eli Friedmanb1851242008-05-27 15:51:49 +0000277void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000278 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000279 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
280
Sebastian Redl0262f022009-01-09 21:09:38 +0000281 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000282 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000283 return;
284 }
285
Eli Friedmanb1851242008-05-27 15:51:49 +0000286 if (DestPtr)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000287 // FIXME: volatility
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000288 CGF.EmitAggregateCopy(DestPtr, ArgPtr, VE->getType());
Eli Friedmanb1851242008-05-27 15:51:49 +0000289}
290
Chris Lattnerf81557c2008-04-04 18:42:16 +0000291void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
292 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000293 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000294 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000295 } else if (E->getType()->isComplexType()) {
296 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000297 } else if (CGF.hasAggregateLLVMType(E->getType())) {
298 CGF.EmitAnyExpr(E, LV.getAddress(), false);
299 } else {
300 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000301 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000302}
303
304void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
305 if (!CGF.hasAggregateLLVMType(T)) {
306 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000307 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
308 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000309 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000310 // Otherwise, just memset the whole thing to zero. This is legal
311 // because in LLVM, all default initializers are guaranteed to have a
312 // bit pattern of all zeros.
313 // There's a potential optimization opportunity in combining
314 // memsets; that would be easy for arrays, but relatively
315 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000316 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000317 }
318}
319
Chris Lattnerf81557c2008-04-04 18:42:16 +0000320void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000321#if 0
322 // FIXME: Disabled while we figure out what to do about
323 // test/CodeGen/bitfield.c
324 //
Eli Friedman994ffef2008-11-30 02:11:09 +0000325 // If we can, prefer a copy from a global; this is a lot less
326 // code for long globals, and it's easier for the current optimizers
327 // to analyze.
328 // FIXME: Should we really be doing this? Should we try to avoid
329 // cases where we emit a global with a lot of zeros? Should
330 // we try to avoid short globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000331 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000332 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
333 llvm::GlobalVariable* GV =
334 new llvm::GlobalVariable(C->getType(), true,
335 llvm::GlobalValue::InternalLinkage,
336 C, "", &CGF.CGM.getModule(), 0);
337 CGF.EmitAggregateCopy(DestPtr, GV, E->getType());
338 return;
339 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000340#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000341 if (E->hadArrayRangeDesignator()) {
342 CGF.ErrorUnsupported(E, "GNU array range designator extension");
343 }
344
Chris Lattnerf81557c2008-04-04 18:42:16 +0000345 // Handle initialization of an array.
346 if (E->getType()->isArrayType()) {
347 const llvm::PointerType *APType =
348 cast<llvm::PointerType>(DestPtr->getType());
349 const llvm::ArrayType *AType =
350 cast<llvm::ArrayType>(APType->getElementType());
351
352 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000353
Chris Lattner96196622008-07-26 22:37:01 +0000354 if (E->getNumInits() > 0) {
355 QualType T1 = E->getType();
356 QualType T2 = E->getInit(0)->getType();
357 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
358 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
359 EmitAggLoadOfLValue(E->getInit(0));
360 return;
361 }
Eli Friedman922696f2008-05-19 17:51:16 +0000362 }
363
Chris Lattnerf81557c2008-04-04 18:42:16 +0000364 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000365 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000366 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000367
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000368 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000369
Chris Lattnerf81557c2008-04-04 18:42:16 +0000370 for (uint64_t i = 0; i != NumArrayElements; ++i) {
371 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
372 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000373 EmitInitializationToLValue(E->getInit(i),
374 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000375 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000376 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000377 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000378 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000379 return;
380 }
381
382 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
383
384 // Do struct initialization; this code just sets each individual member
385 // to the approprate value. This makes bitfield support automatic;
386 // the disadvantage is that the generated code is more difficult for
387 // the optimizer, especially with bitfields.
388 unsigned NumInitElements = E->getNumInits();
389 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000390 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000391
392 if (E->getType()->isUnionType()) {
393 // Only initialize one field of a union. The field itself is
394 // specified by the initializer list.
395 if (!E->getInitializedFieldInUnion()) {
396 // Empty union; we have nothing to do.
397
398#ifndef NDEBUG
399 // Make sure that it's really an empty and not a failure of
400 // semantic analysis.
401 for (RecordDecl::field_iterator Field = SD->field_begin(),
402 FieldEnd = SD->field_end();
403 Field != FieldEnd; ++Field)
404 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
405#endif
406 return;
407 }
408
409 // FIXME: volatility
410 FieldDecl *Field = E->getInitializedFieldInUnion();
411 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
412
413 if (NumInitElements) {
414 // Store the initializer into the field
415 EmitInitializationToLValue(E->getInit(0), FieldLoc);
416 } else {
417 // Default-initialize to null
418 EmitNullInitializationToLValue(FieldLoc, Field->getType());
419 }
420
421 return;
422 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000423
424 // Here we iterate over the fields; this makes it simpler to both
425 // default-initialize fields and skip over unnamed fields.
Douglas Gregor44b43212008-12-11 16:49:14 +0000426 for (RecordDecl::field_iterator Field = SD->field_begin(),
427 FieldEnd = SD->field_end();
428 Field != FieldEnd; ++Field) {
429 // We're done once we hit the flexible array member
430 if (Field->getType()->isIncompleteArrayType())
431 break;
432
Douglas Gregor34e79462009-01-28 23:36:17 +0000433 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000434 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000435
Eli Friedman1e692ac2008-06-13 23:01:12 +0000436 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000437 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000438 if (CurInitVal < NumInitElements) {
439 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000440 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
441 } else {
442 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000443 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000444 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000445 }
Devang Patel636c3d02007-10-26 17:44:44 +0000446}
447
Chris Lattneree755f92007-08-21 04:59:27 +0000448//===----------------------------------------------------------------------===//
449// Entry Points into this File
450//===----------------------------------------------------------------------===//
451
452/// EmitAggExpr - Emit the computation of the specified expression of
453/// aggregate type. The result is computed into DestPtr. Note that if
454/// DestPtr is null, the value of the aggregate expression is not needed.
455void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
456 bool VolatileDest) {
457 assert(E && hasAggregateLLVMType(E->getType()) &&
458 "Invalid aggregate expression to emit");
459
460 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
461}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000462
463void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
464 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
465
466 EmitMemSetToZero(DestPtr, Ty);
467}
468
469void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
470 llvm::Value *SrcPtr, QualType Ty) {
471 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
472
Chris Lattner83c96292009-02-28 18:31:01 +0000473 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000474 // C99 6.5.16.1p3, which states "If the value being stored in an object is
475 // read from another object that overlaps in anyway the storage of the first
476 // object, then the overlap shall be exact and the two objects shall have
477 // qualified or unqualified versions of a compatible type."
478 //
Chris Lattner83c96292009-02-28 18:31:01 +0000479 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000480 // equal, but other compilers do this optimization, and almost every memcpy
481 // implementation handles this case safely. If there is a libc that does not
482 // safely handle this, we can add a target hook.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000483 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
484 if (DestPtr->getType() != BP)
485 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
486 if (SrcPtr->getType() != BP)
487 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
488
489 // Get size and alignment info for this aggregate.
490 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
491
492 // FIXME: Handle variable sized types.
493 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
494
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000495 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000496 DestPtr, SrcPtr,
497 // TypeInfo.first describes size in bits.
498 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
499 llvm::ConstantInt::get(llvm::Type::Int32Ty,
500 TypeInfo.second/8));
501}