blob: 6b68705af7329d99516a078f021a2d3920743034 [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;
Chris Lattner50b36742008-04-13 07:32:11 +000032 llvm::IRBuilder &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 Lattner04421082008-04-08 04:40:51 +000089 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
90 Visit(DAE->getExpr());
91 }
Chris Lattnerf81557c2008-04-04 18:42:16 +000092
93 void EmitInitializationToLValue(Expr *E, LValue Address);
94 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +000095 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +000096
Chris Lattner9c033562007-08-21 04:25:47 +000097};
98} // end anonymous namespace.
99
Chris Lattneree755f92007-08-21 04:59:27 +0000100//===----------------------------------------------------------------------===//
101// Utilities
102//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000103
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000104void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000105 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000106
107 // Aggregate assignment turns into llvm.memset.
108 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
109 if (DestPtr->getType() != BP)
110 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
111
112 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000113 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000114
115 // FIXME: Handle variable sized types.
116 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
117
118 llvm::Value *MemSetOps[4] = {
119 DestPtr,
120 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
121 // TypeInfo.first describes size in bits.
122 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
123 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
124 };
125
126 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
127}
128
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000129void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
130 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattner9b2dc282008-04-04 16:54:41 +0000131 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000132
133 // Aggregate assignment turns into llvm.memcpy.
Christopher Lambddc23f32007-12-17 01:11:20 +0000134 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000135 if (DestPtr->getType() != BP)
136 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
137 if (SrcPtr->getType() != BP)
138 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
139
140 // Get size and alignment info for this aggregate.
Chris Lattner98be4942008-03-05 18:54:05 +0000141 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000142
143 // FIXME: Handle variable sized types.
144 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
145
146 llvm::Value *MemCpyOps[4] = {
147 DestPtr, SrcPtr,
Devang Patel636c3d02007-10-26 17:44:44 +0000148 // TypeInfo.first describes size in bits.
149 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000150 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000151 };
152
153 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
154}
155
156
Chris Lattner883f6a72007-08-11 00:04:45 +0000157/// EmitAggLoadOfLValue - Given an expression with aggregate type that
158/// represents a value lvalue, this method emits the address of the lvalue,
159/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000160void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
161 LValue LV = CGF.EmitLValue(E);
Chris Lattner883f6a72007-08-11 00:04:45 +0000162 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
163 llvm::Value *SrcPtr = LV.getAddress();
164
165 // If the result is ignored, don't copy from the value.
166 if (DestPtr == 0)
167 // FIXME: If the source is volatile, we must read from it.
168 return;
169
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000170 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000171}
172
Chris Lattneree755f92007-08-21 04:59:27 +0000173//===----------------------------------------------------------------------===//
174// Visitor Methods
175//===----------------------------------------------------------------------===//
176
Anders Carlssone4707ff2008-01-14 06:28:57 +0000177void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
178{
179 QualType STy = E->getSubExpr()->getType().getCanonicalType();
180 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000181
182 assert(CGF.getContext().typesAreCompatible(
183 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
184 && "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000185
186 Visit(E->getSubExpr());
187}
188
Anders Carlsson148fe672007-10-31 22:04:46 +0000189void AggExprEmitter::VisitCallExpr(const CallExpr *E)
190{
191 RValue RV = CGF.EmitCallExpr(E);
192 assert(RV.isAggregate() && "Return value must be aggregate value!");
193
194 // If the result is ignored, don't copy from the value.
195 if (DestPtr == 0)
196 // FIXME: If the source is volatile, we must read from it.
197 return;
198
199 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
200}
201
Nate Begeman796ef3d2008-01-31 05:38:29 +0000202void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
203{
204 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
205 E->getNumArgs(CGF.getContext()));
206 assert(RV.isAggregate() && "Return value must be aggregate value!");
207
208 // If the result is ignored, don't copy from the value.
209 if (DestPtr == 0)
210 // FIXME: If the source is volatile, we must read from it.
211 return;
212
213 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
214}
215
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000216void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
217 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
218}
219
Chris Lattner9c033562007-08-21 04:25:47 +0000220void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnerdc4d2802007-12-02 01:49:16 +0000221 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000222}
223
Chris Lattner03d6fb92007-08-21 04:43:17 +0000224void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000225 // For an assignment to work, the value on the right has
226 // to be compatible with the value on the left.
227 assert(CGF.getContext().typesAreCompatible(
228 E->getLHS()->getType().getUnqualifiedType(),
229 E->getRHS()->getType().getUnqualifiedType())
230 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000231 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000232
233 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattner9c033562007-08-21 04:25:47 +0000234 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattner883f6a72007-08-11 00:04:45 +0000235
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000236 if (DestPtr == 0)
237 return;
238
Chris Lattner883f6a72007-08-11 00:04:45 +0000239 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000240 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattner883f6a72007-08-11 00:04:45 +0000241}
242
Chris Lattner9c033562007-08-21 04:25:47 +0000243void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif984d0b42008-04-06 20:42:52 +0000244 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
245 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
246 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattner883f6a72007-08-11 00:04:45 +0000247
Chris Lattner9c033562007-08-21 04:25:47 +0000248 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000249 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000250
Chris Lattner9c033562007-08-21 04:25:47 +0000251 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000252
253 // Handle the GNU extension for missing LHS.
254 assert(E->getLHS() && "Must have LHS for aggregate value");
255
Chris Lattnerc748f272007-08-21 05:02:10 +0000256 Visit(E->getLHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000257 Builder.CreateBr(ContBlock);
258 LHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000259
Chris Lattner9c033562007-08-21 04:25:47 +0000260 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000261
Chris Lattnerc748f272007-08-21 05:02:10 +0000262 Visit(E->getRHS());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000263 Builder.CreateBr(ContBlock);
264 RHSBlock = Builder.GetInsertBlock();
Chris Lattner883f6a72007-08-11 00:04:45 +0000265
Chris Lattner9c033562007-08-21 04:25:47 +0000266 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000267}
Chris Lattneree755f92007-08-21 04:59:27 +0000268
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000269void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
270
271 const llvm::PointerType *APType =
272 cast<llvm::PointerType>(DestPtr->getType());
273 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000274
275 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000276 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000277
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000278 unsigned i;
279 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000280 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000281 Expr *Init = E->getInit(i);
282 if (isa<InitListExpr>(Init))
283 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
284 else
285 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000286 }
287
288 // Emit remaining default initializers
289 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000290 QualType QType = E->getInit(0)->getType();
291 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000292 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000293 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000294 if (EType->isFirstClassType())
295 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
296 else
297 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000298 }
Lauro Ramos Venancio13e22cf2008-02-19 22:04:22 +0000299 } else
300 assert(false && "Invalid initializer");
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000301}
302
Chris Lattnerf81557c2008-04-04 18:42:16 +0000303void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
304 // FIXME: Are initializers affected by volatile?
305 if (E->getType()->isComplexType()) {
306 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000307 } else if (CGF.hasAggregateLLVMType(E->getType())) {
308 CGF.EmitAnyExpr(E, LV.getAddress(), false);
309 } else {
310 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000311 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000312}
313
314void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
315 if (!CGF.hasAggregateLLVMType(T)) {
316 // For non-aggregates, we can store zero
317 const llvm::Type *T =
318 cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
319 Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000320 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000321 // Otherwise, just memset the whole thing to zero. This is legal
322 // because in LLVM, all default initializers are guaranteed to have a
323 // bit pattern of all zeros.
324 // There's a potential optimization opportunity in combining
325 // memsets; that would be easy for arrays, but relatively
326 // difficult for structures with the current code.
327 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
328 uint64_t Size = CGF.getContext().getTypeSize(T);
329
330 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
331 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner3eae03e2008-05-06 00:56:42 +0000332 Builder.CreateCall4(MemSet, DestPtr,
333 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
334 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
335 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000336 }
337}
338
339
340void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
341 if (E->isConstantExpr(CGF.getContext(), 0)) {
342 // FIXME: call into const expr emitter so that we can emit
343 // a memcpy instead of storing the individual members.
344 // This is purely for perf; both codepaths lead to equivalent
345 // (although not necessarily identical) code.
346 // It's worth noting that LLVM keeps on getting smarter, though,
347 // so it might not be worth bothering.
348 }
349
350 // Handle initialization of an array.
351 if (E->getType()->isArrayType()) {
352 const llvm::PointerType *APType =
353 cast<llvm::PointerType>(DestPtr->getType());
354 const llvm::ArrayType *AType =
355 cast<llvm::ArrayType>(APType->getElementType());
356
357 uint64_t NumInitElements = E->getNumInits();
358 uint64_t NumArrayElements = AType->getNumElements();
359 QualType ElementType = E->getType()->getAsArrayType()->getElementType();
360
361 for (uint64_t i = 0; i != NumArrayElements; ++i) {
362 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
363 if (i < NumInitElements)
364 EmitInitializationToLValue(E->getInit(i), LValue::MakeAddr(NextVal));
365 else
366 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal),
367 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000368 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000369 return;
370 }
371
372 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
373
374 // Do struct initialization; this code just sets each individual member
375 // to the approprate value. This makes bitfield support automatic;
376 // the disadvantage is that the generated code is more difficult for
377 // the optimizer, especially with bitfields.
378 unsigned NumInitElements = E->getNumInits();
379 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
380 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
381 unsigned CurInitVal = 0;
382 bool isUnion = E->getType()->isUnionType();
383
384 // Here we iterate over the fields; this makes it simpler to both
385 // default-initialize fields and skip over unnamed fields.
386 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
387 if (CurInitVal >= NumInitElements) {
388 // No more initializers; we're done.
389 break;
390 }
391
392 FieldDecl *CurField = SD->getMember(CurFieldNo);
393 if (CurField->getIdentifier() == 0) {
394 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
395 continue;
396 }
397 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion);
398 if (CurInitVal < NumInitElements) {
399 // Store the initializer into the field
400 // This will probably have to get a bit smarter when we support
401 // designators in initializers
402 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
403 } else {
404 // We're out of initalizers; default-initialize to null
405 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
406 }
407
408 // Unions only initialize one field.
409 // (things can get weird with designators, but they aren't
410 // supported yet.)
411 if (E->getType()->isUnionType())
412 break;
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000413 }
Devang Patel636c3d02007-10-26 17:44:44 +0000414}
415
Chris Lattneree755f92007-08-21 04:59:27 +0000416//===----------------------------------------------------------------------===//
417// Entry Points into this File
418//===----------------------------------------------------------------------===//
419
420/// EmitAggExpr - Emit the computation of the specified expression of
421/// aggregate type. The result is computed into DestPtr. Note that if
422/// DestPtr is null, the value of the aggregate expression is not needed.
423void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
424 bool VolatileDest) {
425 assert(E && hasAggregateLLVMType(E->getType()) &&
426 "Invalid aggregate expression to emit");
427
428 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
429}