blob: f584980d7f8af1a12ae5ecdf676261a2bd789bb6 [file] [log] [blame]
Chris Lattner820dce82007-08-24 02:22:53 +00001//===--- CGExprAgg.cpp - Emit LLVM Code from Aggregate Expressions --------===//
Chris Lattner78ed8402007-08-10 20:13:28 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-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 Lattner78ed8402007-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 Lattnerbdb8ffb2007-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 Patelddde6d52007-10-26 17:44:44 +000019#include "llvm/GlobalVariable.h"
Chris Lattnerb50e3902007-08-21 04:25:47 +000020#include "llvm/Support/Compiler.h"
Chris Lattner6ee6ab02008-04-04 18:42:16 +000021#include "llvm/Intrinsics.h"
Chris Lattner78ed8402007-08-10 20:13:28 +000022using namespace clang;
23using namespace CodeGen;
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +000024
Chris Lattnerb50e3902007-08-21 04:25:47 +000025//===----------------------------------------------------------------------===//
26// Aggregate Expression Emitter
27//===----------------------------------------------------------------------===//
28
29namespace {
30class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
31 CodeGenFunction &CGF;
Chris Lattner676bf212008-04-13 07:32:11 +000032 llvm::IRBuilder &Builder;
Chris Lattnerb50e3902007-08-21 04:25:47 +000033 llvm::Value *DestPtr;
34 bool VolatileDest;
35public:
36 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool volatileDest)
Chris Lattner41b1fca2007-08-26 23:13:56 +000037 : CGF(cgf), Builder(CGF.Builder),
38 DestPtr(destPtr), VolatileDest(volatileDest) {
Chris Lattnerb50e3902007-08-21 04:25:47 +000039 }
40
Chris Lattnera7300102007-08-21 04:59:27 +000041 //===--------------------------------------------------------------------===//
42 // Utilities
43 //===--------------------------------------------------------------------===//
44
Chris Lattnerb50e3902007-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 Lattner41b1fca2007-08-26 23:13:56 +000050 void EmitAggregateCopy(llvm::Value *DestPtr, llvm::Value *SrcPtr,
51 QualType EltTy);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +000052
53 void EmitAggregateClear(llvm::Value *DestPtr, QualType Ty);
54
55 void EmitNonConstInit(InitListExpr *E);
Eli Friedman48ec5622008-05-19 17:51:16 +000056
Chris Lattnera7300102007-08-21 04:59:27 +000057 //===--------------------------------------------------------------------===//
58 // Visitor Methods
59 //===--------------------------------------------------------------------===//
60
Chris Lattnerb50e3902007-08-21 04:25:47 +000061 void VisitStmt(Stmt *S) {
Chris Lattnere8f49632007-12-02 01:49:16 +000062 CGF.WarnUnsupported(S, "aggregate expression");
Chris Lattnerb50e3902007-08-21 04:25:47 +000063 }
64 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
65
66 // l-values.
Seo Sanghyeon32666c52007-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 Sanghyeon3c2d5fb2007-12-23 03:11:58 +000070 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeon32666c52007-12-14 02:04:12 +000071
72 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
73 EmitAggLoadOfLValue(E);
74 }
Chris Lattnerb50e3902007-08-21 04:25:47 +000075
76 // Operators.
77 // case Expr::UnaryOperatorClass:
Chris Lattnerb50e3902007-08-21 04:25:47 +000078 // case Expr::CastExprClass:
Anders Carlssonf2712ac2008-01-14 06:28:57 +000079 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson0ae15412007-10-31 22:04:46 +000080 void VisitCallExpr(const CallExpr *E);
Chris Lattnerbab0bfb2007-08-31 22:54:14 +000081 void VisitStmtExpr(const StmtExpr *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000082 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner76cb1892007-08-21 04:43:17 +000083 void VisitBinAssign(const BinaryOperator *E);
Nate Begemanc6078c92008-01-31 05:38:29 +000084 void VisitOverloadExpr(const OverloadExpr *E);
Eli Friedman91fc7802008-05-20 07:56:31 +000085 void VisitBinComma(const BinaryOperator *E);
Chris Lattnerb50e3902007-08-21 04:25:47 +000086
87
88 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patelddde6d52007-10-26 17:44:44 +000089 void VisitInitListExpr(InitListExpr *E);
Chris Lattner3e254fb2008-04-08 04:40:51 +000090 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
91 Visit(DAE->getExpr());
92 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +000093
94 void EmitInitializationToLValue(Expr *E, LValue Address);
95 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattnerb50e3902007-08-21 04:25:47 +000096 // case Expr::ChooseExprClass:
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +000097
Chris Lattnerb50e3902007-08-21 04:25:47 +000098};
99} // end anonymous namespace.
100
Chris Lattnera7300102007-08-21 04:59:27 +0000101//===----------------------------------------------------------------------===//
102// Utilities
103//===----------------------------------------------------------------------===//
Chris Lattnerb50e3902007-08-21 04:25:47 +0000104
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000105void AggExprEmitter::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000106 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000107
108 // Aggregate assignment turns into llvm.memset.
109 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
110 if (DestPtr->getType() != BP)
111 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
112
113 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000114 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000115
116 // FIXME: Handle variable sized types.
117 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
118
119 llvm::Value *MemSetOps[4] = {
120 DestPtr,
121 llvm::ConstantInt::getNullValue(llvm::Type::Int8Ty),
122 // TypeInfo.first describes size in bits.
123 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
124 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
125 };
126
127 Builder.CreateCall(CGF.CGM.getMemSetFn(), MemSetOps, MemSetOps+4);
128}
129
Chris Lattner41b1fca2007-08-26 23:13:56 +0000130void AggExprEmitter::EmitAggregateCopy(llvm::Value *DestPtr,
131 llvm::Value *SrcPtr, QualType Ty) {
Chris Lattnerde0908b2008-04-04 16:54:41 +0000132 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
Chris Lattner41b1fca2007-08-26 23:13:56 +0000133
134 // Aggregate assignment turns into llvm.memcpy.
Christopher Lamb4fe5e702007-12-17 01:11:20 +0000135 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000136 if (DestPtr->getType() != BP)
137 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
138 if (SrcPtr->getType() != BP)
139 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
140
141 // Get size and alignment info for this aggregate.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000142 std::pair<uint64_t, unsigned> TypeInfo = CGF.getContext().getTypeInfo(Ty);
Chris Lattner41b1fca2007-08-26 23:13:56 +0000143
144 // FIXME: Handle variable sized types.
145 const llvm::Type *IntPtr = llvm::IntegerType::get(CGF.LLVMPointerWidth);
146
147 llvm::Value *MemCpyOps[4] = {
148 DestPtr, SrcPtr,
Devang Patelddde6d52007-10-26 17:44:44 +0000149 // TypeInfo.first describes size in bits.
150 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000151 llvm::ConstantInt::get(llvm::Type::Int32Ty, TypeInfo.second/8)
Chris Lattner41b1fca2007-08-26 23:13:56 +0000152 };
153
154 Builder.CreateCall(CGF.CGM.getMemCpyFn(), MemCpyOps, MemCpyOps+4);
155}
156
157
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000158/// EmitAggLoadOfLValue - Given an expression with aggregate type that
159/// represents a value lvalue, this method emits the address of the lvalue,
160/// then loads the result into DestPtr.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000161void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
162 LValue LV = CGF.EmitLValue(E);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000163 assert(LV.isSimple() && "Can't have aggregate bitfield, vector, etc");
164 llvm::Value *SrcPtr = LV.getAddress();
165
166 // If the result is ignored, don't copy from the value.
167 if (DestPtr == 0)
168 // FIXME: If the source is volatile, we must read from it.
169 return;
170
Chris Lattner41b1fca2007-08-26 23:13:56 +0000171 EmitAggregateCopy(DestPtr, SrcPtr, E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000172}
173
Chris Lattnera7300102007-08-21 04:59:27 +0000174//===----------------------------------------------------------------------===//
175// Visitor Methods
176//===----------------------------------------------------------------------===//
177
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000178void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E)
179{
180 QualType STy = E->getSubExpr()->getType().getCanonicalType();
181 QualType Ty = E->getType().getCanonicalType();
Eli Friedmanf0217122008-02-11 01:09:17 +0000182
183 assert(CGF.getContext().typesAreCompatible(
184 STy.getUnqualifiedType(), Ty.getUnqualifiedType())
185 && "Implicit cast types must be compatible");
Anders Carlssonf2712ac2008-01-14 06:28:57 +0000186
187 Visit(E->getSubExpr());
188}
189
Anders Carlsson0ae15412007-10-31 22:04:46 +0000190void AggExprEmitter::VisitCallExpr(const CallExpr *E)
191{
192 RValue RV = CGF.EmitCallExpr(E);
193 assert(RV.isAggregate() && "Return value must be aggregate value!");
194
195 // If the result is ignored, don't copy from the value.
196 if (DestPtr == 0)
197 // FIXME: If the source is volatile, we must read from it.
198 return;
199
200 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
201}
202
Nate Begemanc6078c92008-01-31 05:38:29 +0000203void AggExprEmitter::VisitOverloadExpr(const OverloadExpr *E)
204{
205 RValue RV = CGF.EmitCallExpr(E->getFn(), E->arg_begin(),
206 E->getNumArgs(CGF.getContext()));
207 assert(RV.isAggregate() && "Return value must be aggregate value!");
208
209 // If the result is ignored, don't copy from the value.
210 if (DestPtr == 0)
211 // FIXME: If the source is volatile, we must read from it.
212 return;
213
214 EmitAggregateCopy(DestPtr, RV.getAggregateAddr(), E->getType());
215}
216
Eli Friedman91fc7802008-05-20 07:56:31 +0000217void AggExprEmitter::VisitBinComma(const BinaryOperator *E)
218{
219 CGF.EmitAnyExpr(E->getLHS());
220 CGF.EmitAggExpr(E->getRHS(), DestPtr, false);
221}
222
Chris Lattnerbab0bfb2007-08-31 22:54:14 +0000223void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
224 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
225}
226
Chris Lattnerb50e3902007-08-21 04:25:47 +0000227void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Chris Lattnere8f49632007-12-02 01:49:16 +0000228 CGF.WarnUnsupported(E, "aggregate binary expression");
Chris Lattnera7300102007-08-21 04:59:27 +0000229}
230
Chris Lattner76cb1892007-08-21 04:43:17 +0000231void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanf0217122008-02-11 01:09:17 +0000232 // For an assignment to work, the value on the right has
233 // to be compatible with the value on the left.
234 assert(CGF.getContext().typesAreCompatible(
235 E->getLHS()->getType().getUnqualifiedType(),
236 E->getRHS()->getType().getUnqualifiedType())
237 && "Invalid assignment");
Chris Lattnerb50e3902007-08-21 04:25:47 +0000238 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000239
240 // Codegen the RHS so that it stores directly into the LHS.
Chris Lattnerb50e3902007-08-21 04:25:47 +0000241 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), false /*FIXME: VOLATILE LHS*/);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000242
Eli Friedmanf0217122008-02-11 01:09:17 +0000243 if (DestPtr == 0)
244 return;
245
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000246 // If the result of the assignment is used, copy the RHS there also.
Eli Friedmanf0217122008-02-11 01:09:17 +0000247 EmitAggregateCopy(DestPtr, LHS.getAddress(), E->getType());
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000248}
249
Chris Lattnerb50e3902007-08-21 04:25:47 +0000250void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Gabor Greif815e2c12008-04-06 20:42:52 +0000251 llvm::BasicBlock *LHSBlock = llvm::BasicBlock::Create("cond.?");
252 llvm::BasicBlock *RHSBlock = llvm::BasicBlock::Create("cond.:");
253 llvm::BasicBlock *ContBlock = llvm::BasicBlock::Create("cond.cont");
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000254
Chris Lattnerb50e3902007-08-21 04:25:47 +0000255 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000256 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000257
Chris Lattnerb50e3902007-08-21 04:25:47 +0000258 CGF.EmitBlock(LHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000259
260 // Handle the GNU extension for missing LHS.
261 assert(E->getLHS() && "Must have LHS for aggregate value");
262
Chris Lattner55165ba2007-08-21 05:02:10 +0000263 Visit(E->getLHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000264 Builder.CreateBr(ContBlock);
265 LHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000266
Chris Lattnerb50e3902007-08-21 04:25:47 +0000267 CGF.EmitBlock(RHSBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000268
Chris Lattner55165ba2007-08-21 05:02:10 +0000269 Visit(E->getRHS());
Chris Lattner41b1fca2007-08-26 23:13:56 +0000270 Builder.CreateBr(ContBlock);
271 RHSBlock = Builder.GetInsertBlock();
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000272
Chris Lattnerb50e3902007-08-21 04:25:47 +0000273 CGF.EmitBlock(ContBlock);
Chris Lattnerbdb8ffb2007-08-11 00:04:45 +0000274}
Chris Lattnera7300102007-08-21 04:59:27 +0000275
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000276void AggExprEmitter::EmitNonConstInit(InitListExpr *E) {
277
278 const llvm::PointerType *APType =
279 cast<llvm::PointerType>(DestPtr->getType());
280 const llvm::Type *DestType = APType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000281
282 if (const llvm::ArrayType *AType = dyn_cast<llvm::ArrayType>(DestType)) {
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000283 unsigned NumInitElements = E->getNumInits();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000284
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000285 unsigned i;
286 for (i = 0; i != NumInitElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000287 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000288 Expr *Init = E->getInit(i);
289 if (isa<InitListExpr>(Init))
290 CGF.EmitAggExpr(Init, NextVal, VolatileDest);
291 else
292 Builder.CreateStore(CGF.EmitScalarExpr(Init), NextVal);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000293 }
294
295 // Emit remaining default initializers
296 unsigned NumArrayElements = AType->getNumElements();
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000297 QualType QType = E->getInit(0)->getType();
298 const llvm::Type *EType = AType->getElementType();
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000299 for (/*Do not initialize i*/; i < NumArrayElements; ++i) {
Chris Lattner07307562008-03-19 05:19:41 +0000300 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
Dan Gohman377ba9f2008-05-22 22:12:56 +0000301 if (EType->isSingleValueType())
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000302 Builder.CreateStore(llvm::Constant::getNullValue(EType), NextVal);
303 else
304 EmitAggregateClear(NextVal, QType);
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000305 }
Lauro Ramos Venancio97b09572008-02-19 22:04:22 +0000306 } else
307 assert(false && "Invalid initializer");
Lauro Ramos Venancio9eb37a22008-02-18 22:44:02 +0000308}
309
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000310void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
311 // FIXME: Are initializers affected by volatile?
312 if (E->getType()->isComplexType()) {
313 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanbb822fb2008-05-12 15:06:05 +0000314 } else if (CGF.hasAggregateLLVMType(E->getType())) {
315 CGF.EmitAnyExpr(E, LV.getAddress(), false);
316 } else {
317 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000318 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000319}
320
321void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
322 if (!CGF.hasAggregateLLVMType(T)) {
323 // For non-aggregates, we can store zero
324 const llvm::Type *T =
325 cast<llvm::PointerType>(LV.getAddress()->getType())->getElementType();
326 Builder.CreateStore(llvm::Constant::getNullValue(T), LV.getAddress());
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000327 } else {
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000328 // Otherwise, just memset the whole thing to zero. This is legal
329 // because in LLVM, all default initializers are guaranteed to have a
330 // bit pattern of all zeros.
331 // There's a potential optimization opportunity in combining
332 // memsets; that would be easy for arrays, but relatively
333 // difficult for structures with the current code.
334 llvm::Value *MemSet = CGF.CGM.getIntrinsic(llvm::Intrinsic::memset_i64);
335 uint64_t Size = CGF.getContext().getTypeSize(T);
336
337 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
338 llvm::Value* DestPtr = Builder.CreateBitCast(LV.getAddress(), BP, "tmp");
Chris Lattner7db5e942008-05-06 00:56:42 +0000339 Builder.CreateCall4(MemSet, DestPtr,
340 llvm::ConstantInt::get(llvm::Type::Int8Ty, 0),
341 llvm::ConstantInt::get(llvm::Type::Int64Ty, Size/8),
342 llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000343 }
344}
345
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000346void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
347 if (E->isConstantExpr(CGF.getContext(), 0)) {
348 // FIXME: call into const expr emitter so that we can emit
349 // a memcpy instead of storing the individual members.
350 // This is purely for perf; both codepaths lead to equivalent
351 // (although not necessarily identical) code.
352 // It's worth noting that LLVM keeps on getting smarter, though,
353 // so it might not be worth bothering.
354 }
355
356 // Handle initialization of an array.
357 if (E->getType()->isArrayType()) {
358 const llvm::PointerType *APType =
359 cast<llvm::PointerType>(DestPtr->getType());
360 const llvm::ArrayType *AType =
361 cast<llvm::ArrayType>(APType->getElementType());
362
363 uint64_t NumInitElements = E->getNumInits();
Eli Friedman48ec5622008-05-19 17:51:16 +0000364
365 if (E->getNumInits() > 0 &&
366 E->getType().getCanonicalType().getUnqualifiedType() ==
367 E->getInit(0)->getType().getCanonicalType().getUnqualifiedType()) {
368 EmitAggLoadOfLValue(E->getInit(0));
369 return;
370 }
371
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000372 uint64_t NumArrayElements = AType->getNumElements();
373 QualType ElementType = E->getType()->getAsArrayType()->getElementType();
374
375 for (uint64_t i = 0; i != NumArrayElements; ++i) {
376 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
377 if (i < NumInitElements)
378 EmitInitializationToLValue(E->getInit(i), LValue::MakeAddr(NextVal));
379 else
380 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal),
381 ElementType);
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000382 }
Chris Lattner6ee6ab02008-04-04 18:42:16 +0000383 return;
384 }
385
386 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
387
388 // Do struct initialization; this code just sets each individual member
389 // to the approprate value. This makes bitfield support automatic;
390 // the disadvantage is that the generated code is more difficult for
391 // the optimizer, especially with bitfields.
392 unsigned NumInitElements = E->getNumInits();
393 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
394 unsigned NumMembers = SD->getNumMembers() - SD->hasFlexibleArrayMember();
395 unsigned CurInitVal = 0;
396 bool isUnion = E->getType()->isUnionType();
397
398 // Here we iterate over the fields; this makes it simpler to both
399 // default-initialize fields and skip over unnamed fields.
400 for (unsigned CurFieldNo = 0; CurFieldNo != NumMembers; ++CurFieldNo) {
401 if (CurInitVal >= NumInitElements) {
402 // No more initializers; we're done.
403 break;
404 }
405
406 FieldDecl *CurField = SD->getMember(CurFieldNo);
407 if (CurField->getIdentifier() == 0) {
408 // Initializers can't initialize unnamed fields, e.g. "int : 20;"
409 continue;
410 }
411 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, CurField, isUnion);
412 if (CurInitVal < NumInitElements) {
413 // Store the initializer into the field
414 // This will probably have to get a bit smarter when we support
415 // designators in initializers
416 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
417 } else {
418 // We're out of initalizers; default-initialize to null
419 EmitNullInitializationToLValue(FieldLoc, CurField->getType());
420 }
421
422 // Unions only initialize one field.
423 // (things can get weird with designators, but they aren't
424 // supported yet.)
425 if (E->getType()->isUnionType())
426 break;
Lauro Ramos Venancioa62b1df2008-02-19 19:27:31 +0000427 }
Devang Patelddde6d52007-10-26 17:44:44 +0000428}
429
Chris Lattnera7300102007-08-21 04:59:27 +0000430//===----------------------------------------------------------------------===//
431// Entry Points into this File
432//===----------------------------------------------------------------------===//
433
434/// EmitAggExpr - Emit the computation of the specified expression of
435/// aggregate type. The result is computed into DestPtr. Note that if
436/// DestPtr is null, the value of the aggregate expression is not needed.
437void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
438 bool VolatileDest) {
439 assert(E && hasAggregateLLVMType(E->getType()) &&
440 "Invalid aggregate expression to emit");
441
442 AggExprEmitter(*this, DestPtr, VolatileDest).Visit(const_cast<Expr*>(E));
443}