blob: 1ce46ae4b449921a85b5ec42bc8784e9a0f47742 [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"
16#include "clang/AST/AST.h"
17#include "llvm/Constants.h"
18#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000021#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000022using namespace clang;
23using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000024
Chris Lattner9c033562007-08-21 04:25:47 +000025//===----------------------------------------------------------------------===//
26// Aggregate Expression Emitter
27//===----------------------------------------------------------------------===//
28
29namespace {
30class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
31 CodeGenFunction &CGF;
Devang Patel50c90342007-10-09 19:49:58 +000032 llvm::LLVMFoldingBuilder &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000033 llvm::Value *DestPtr;
34 bool VolatileDest;
35public:
36 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000037 : CGF(cgf), Builder(CGF.Builder),
38 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattner9c033562007-08-21 04:25:47 +000039 }
40
Chris Lattneree755f92007-08-21 04:59:27 +000041 //===--------------------------------------------------------------------===//
42 // Utilities
43 //===--------------------------------------------------------------------===//
44
Chris Lattner9c033562007-08-21 04:25:47 +000045 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
46 /// represents a value lvalue, this method emits the address of the lvalue,
47 /// then loads the result into DestPtr.
48 void EmitAggLoadOfLValue(const Expr *E);
49
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000050 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
51 QualType EltTy);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +000052
53 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
54
55 void EmitNonConstInit(InitListExpr *E);
Chris Lattneree755f92007-08-21 04:59:27 +000056
57 //===--------------------------------------------------------------------===//
58 // Visitor Methods
59 //===--------------------------------------------------------------------===//
60
Chris Lattner9c033562007-08-21 04:25:47 +000061 void VisitStmt(Stmt *S) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +000062 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000063 }
64 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
65
66 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000067 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
68 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
69 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000071
72 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
73 EmitAggLoadOfLValue(E);
74 }
Chris Lattner9c033562007-08-21 04:25:47 +000075
76 // Operators.
77 // case Expr::UnaryOperatorClass:
Chris Lattner9c033562007-08-21 04:25:47 +000078 // case Expr::CastExprClass:
Anders Carlssone4707ff2008-01-14 06:28:57 +000079 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000080 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000081 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000082 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000083 void VisitBinAssign(const BinaryOperator *E);
Nate Begeman796ef3d2008-01-31 05:38:29 +000084 void VisitOverloadExpr(const OverloadExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000085
86
87 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +000088 void VisitInitListExpr(InitListExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +000089
90 void EmitInitializationToLValue(Expr *E, LValue Address);
91 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +000092 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +000093
Chris Lattner9c033562007-08-21 04:25:47 +000094};
95} // end anonymous namespace.
96
Chris Lattneree755f92007-08-21 04:59:27 +000097//===----------------------------------------------------------------------===//
98// Utilities
99//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000100
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000101void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000102 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000103
104 // Aggregate assignment turns into llvm.memset.
105 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
106 if (DestPtr->getType() != BP)
107 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
108
109 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000110 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000111
112 // FIXME: Handle variable sized types.
113 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
114
115 llvm::Value *MemSetOps[4] = {
116 DestPtr,
117 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
118 // TypeInfo.first describes size in bits.
119 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
120 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
121 };
122
123 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
124}
125
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000126void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
127 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000128 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000129
130 // Aggregate assignment turns into llvm.memcpy.
Christopher Lambddc23f32007-12-17 01:11:20 +0000131 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000132 if (DestPtr->getType() != BP)
133 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
134 if (SrcPtr->getType() != BP)
135 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
136
137 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000138 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000139
140 // FIXME: Handle variable sized types.
141 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
142
143 llvm::Value *MemCpyOps[4] = {
144 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000145 // TypeInfo.first describes size in bits.
146 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000147 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000148 };
149
150 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
151}
152
153
Chris Lattner883f6a72007-08-11 00:04:45 +0000154/// EmitAggLoadOfLValue - Given an expression with aggregate type that
155/// represents a value lvalue, this method emits the address of the lvalue,
156/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000157void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
158 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000159 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
160 llvm::Value *SrcPtr = LV.getAddress();
161
162 // If the result is ignored, don't copy from the value.
163 if (DestPtr == 0)
164 // FIXME: If the source is volatile, we must read from it.
165 return;
166
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000167 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000168}
169
Chris Lattneree755f92007-08-21 04:59:27 +0000170//===----------------------------------------------------------------------===//
171// Visitor Methods
172//===----------------------------------------------------------------------===//
173
Anders Carlssone4707ff2008-01-14 06:28:57 +0000174void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
175{
176 QualType STy = E->getSubExpr()->getType().getCanonicalType();
177 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000178
179 assert(CGF.getContext().typesAreCompatible(
180 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
181 && "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000182
183 Visit(E->getSubExpr());
184}
185
Anders Carlsson148fe672007-10-31 22:04:46 +0000186void AggExprEmitter::VisitCallExpr(const CallExpr *E)
187{
188 RValue RV = CGF.EmitCallExpr(E);
189 assert(RV.isAggregate() && "Return value must be aggregate value!");
190
191 // If the result is ignored, don't copy from the value.
192 if (DestPtr == 0)
193 // FIXME: If the source is volatile, we must read from it.
194 return;
195
196 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
197}
198
Nate Begeman796ef3d2008-01-31 05:38:29 +0000199void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
200{
201 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
202 E->getNumArgs(CGF.getContext()));
203 assert(RV.isAggregate() && "Return value must be aggregate value!");
204
205 // If the result is ignored, don't copy from the value.
206 if (DestPtr == 0)
207 // FIXME: If the source is volatile, we must read from it.
208 return;
209
210 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
211}
212
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000213void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
214 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
215}
216
Chris Lattner9c033562007-08-21 04:25:47 +0000217void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000218 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000219}
220
Chris Lattner03d6fb92007-08-21 04:43:17 +0000221void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000222 // For an assignment to work, the value on the right has
223 // to be compatible with the value on the left.
224 assert(CGF.getContext().typesAreCompatible(
225 E->getLHS()->getType().getUnqualifiedType(),
226 E->getRHS()->getType().getUnqualifiedType())
227 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000228 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000229
230 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000231 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000232
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000233 if (DestPtr == 0)
234 return;
235
Chris Lattner883f6a72007-08-11 00:04:45 +0000236 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000237 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000238}
239
Chris Lattner9c033562007-08-21 04:25:47 +0000240void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Chris Lattner883f6a72007-08-11 00:04:45 +0000241 llvm::BasicBlock *LHSBlock = new llvm::BasicBlock("cond.?");
242 llvm::BasicBlock *RHSBlock = new llvm::BasicBlock("cond.:");
243 llvm::BasicBlock *ContBlock = new llvm::BasicBlock("cond.cont");
244
Chris Lattner9c033562007-08-21 04:25:47 +0000245 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000246 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000247
Chris Lattner9c033562007-08-21 04:25:47 +0000248 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000249
250 // Handle the GNU extension for missing LHS.
251 assert(E->getLHS() && "Must have LHS for aggregate value");
252
Chris Lattnerc748f272007-08-21 05:02:10 +0000253 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000254 Builder.CreateBr(ContBlock);
255 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000256
Chris Lattner9c033562007-08-21 04:25:47 +0000257 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000258
Chris Lattnerc748f272007-08-21 05:02:10 +0000259 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000260 Builder.CreateBr(ContBlock);
261 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000262
Chris Lattner9c033562007-08-21 04:25:47 +0000263 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000264}
Chris Lattneree755f92007-08-21 04:59:27 +0000265
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000266void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
267
268 const llvm::PointerType *APType =
269 cast<llvm::PointerType>(DestPtr->getType());
270 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000271
272 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000273 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000274
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000275 unsigned i;
276 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000277 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000278 Expr *Init = E->getInit(i);
279 if (isa<InitListExpr>(Init))
280 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
281 else
282 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000283 }
284
285 // Emit remaining default initializers
286 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000287 QualType QType = E->getInit(0)->getType();
288 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000289 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000290 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000291 if (EType->isFirstClassType())
292 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
293 else
294 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000295 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000296 } else
297 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000298}
299
Chris Lattnerf81557c2008-04-04 18:42:16 +0000300void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
301 // FIXME: Are initializers affected by volatile?
302 if (E->getType()->isComplexType()) {
303 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Devang Patel636c3d02007-10-26 17:44:44 +0000304 return;
Chris Lattnerf81557c2008-04-04 18:42:16 +0000305 }
306 RValue RV = CGF.EmitAnyExpr(E, LV.getAddress(), false);
307 if (CGF.hasAggregateLLVMType(E->getType()))
308 return;
309 CGF.EmitStoreThroughLValue(RV, LV, E->getType());
310}
311
312void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
313 if (!CGF.hasAggregateLLVMType(T)) {
314 // For non-aggregates, we can store zero
315 const llvm::Type *T =
316 cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
317 Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000318 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000319 // Otherwise, just memset the whole thing to zero. This is legal
320 // because in LLVM, all default initializers are guaranteed to have a
321 // bit pattern of all zeros.
322 // There's a potential optimization opportunity in combining
323 // memsets; that would be easy for arrays, but relatively
324 // difficult for structures with the current code.
325 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
326 uint64_t Size = CGF.getContext().getTypeSize(T);
327
328 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
329 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
330
331 llvm::Value *MemSetOps[4] = {
332 DestPtr, llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
333 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
334 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0)
335 };
336
337 Builder.CreateCall(MemSet, MemSetOps, MemSetOps+4);
338 }
339}
340
341
342void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
343 if (E->isConstantExpr(CGF.getContext(), 0)) {
344 // FIXME: call into const expr emitter so that we can emit
345 // a memcpy instead of storing the individual members.
346 // This is purely for perf; both codepaths lead to equivalent
347 // (although not necessarily identical) code.
348 // It's worth noting that LLVM keeps on getting smarter, though,
349 // so it might not be worth bothering.
350 }
351
352 // Handle initialization of an array.
353 if (E->getType()->isArrayType()) {
354 const llvm::PointerType *APType =
355 cast<llvm::PointerType>(DestPtr->getType());
356 const llvm::ArrayType *AType =
357 cast<llvm::ArrayType>(APType->getElementType());
358
359 uint64_t NumInitElements = E->getNumInits();
360 uint64_t NumArrayElements = AType->getNumElements();
361 QualType ElementType = E->getType()->getAsArrayType()->getElementType();
362
363 for (uint64_t i = 0; i != NumArrayElements; ++i) {
364 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
365 if (i < NumInitElements)
366 EmitInitializationToLValue(E->getInit(i), LValue::MakeAddr(NextVal));
367 else
368 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal),
369 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000370 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000371 return;
372 }
373
374 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
375
376 // Do struct initialization; this code just sets each individual member
377 // to the approprate value. This makes bitfield support automatic;
378 // the disadvantage is that the generated code is more difficult for
379 // the optimizer, especially with bitfields.
380 unsigned NumInitElements = E->getNumInits();
381 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
382 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
383 unsigned CurInitVal = 0;
384 bool isUnion = E->getType()->isUnionType();
385
386 // Here we iterate over the fields; this makes it simpler to both
387 // default-initialize fields and skip over unnamed fields.
388 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
389 if (CurInitVal >= NumInitElements) {
390 // No more initializers; we're done.
391 break;
392 }
393
394 FieldDecl *CurField = SD->getMember(CurFieldNo);
395 if (CurField->getIdentifier() == 0) {
396 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
397 continue;
398 }
399 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion);
400 if (CurInitVal < NumInitElements) {
401 // Store the initializer into the field
402 // This will probably have to get a bit smarter when we support
403 // designators in initializers
404 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
405 } else {
406 // We're out of initalizers; default-initialize to null
407 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
408 }
409
410 // Unions only initialize one field.
411 // (things can get weird with designators, but they aren't
412 // supported yet.)
413 if (E->getType()->isUnionType())
414 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000415 }
Devang Patel636c3d02007-10-26 17:44:44 +0000416}
417
Chris Lattneree755f92007-08-21 04:59:27 +0000418//===----------------------------------------------------------------------===//
419// Entry Points into this File
420//===----------------------------------------------------------------------===//
421
422/// EmitAggExpr - Emit the computation of the specified expression of
423/// aggregate type. The result is computed into DestPtr. Note that if
424/// DestPtr is null, the value of the aggregate expression is not needed.
425void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
426 bool VolatileDest) {
427 assert(E && hasAggregateLLVMType(E->getType()) &&
428 "Invalid aggregate expression to emit");
429
430 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
431}