blob: 59c00528a838bd5bac0105ef0f044564402f015d [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"
Anders Carlssonb14095a2009-04-17 00:06:03 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/StmtVisitor.h"
Chris Lattner883f6a72007-08-11 00:04:45 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
Devang Patel636c3d02007-10-26 17:44:44 +000021#include "llvm/GlobalVariable.h"
Chris Lattner9c033562007-08-21 04:25:47 +000022#include "llvm/Support/Compiler.h"
Chris Lattnerf81557c2008-04-04 18:42:16 +000023#include "llvm/Intrinsics.h"
Chris Lattneraf6f5282007-08-10 20:13:28 +000024using namespace clang;
25using namespace CodeGen;
Chris Lattner883f6a72007-08-11 00:04:45 +000026
Chris Lattner9c033562007-08-21 04:25:47 +000027//===----------------------------------------------------------------------===//
28// Aggregate Expression Emitter
29//===----------------------------------------------------------------------===//
30
31namespace {
32class VISIBILITY_HIDDEN AggExprEmitter : public StmtVisitor<AggExprEmitter> {
33 CodeGenFunction &CGF;
Daniel Dunbar45d196b2008-11-01 01:53:16 +000034 CGBuilderTy &Builder;
Chris Lattner9c033562007-08-21 04:25:47 +000035 llvm::Value *DestPtr;
36 bool VolatileDest;
Mike Stump49d1cd52009-05-26 22:03:21 +000037 bool IgnoreResult;
38
Chris Lattner9c033562007-08-21 04:25:47 +000039public:
Mike Stumpff4bf3b2009-05-27 01:42:21 +000040 AggExprEmitter(CodeGenFunction &cgf, llvm::Value *destPtr, bool v,
41 bool ignore)
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +000042 : CGF(cgf), Builder(CGF.Builder),
Mike Stumpff4bf3b2009-05-27 01:42:21 +000043 DestPtr(destPtr), VolatileDest(v), IgnoreResult(ignore) {
Chris Lattner9c033562007-08-21 04:25:47 +000044 }
45
Chris Lattneree755f92007-08-21 04:59:27 +000046 //===--------------------------------------------------------------------===//
47 // Utilities
48 //===--------------------------------------------------------------------===//
49
Chris Lattner9c033562007-08-21 04:25:47 +000050 /// EmitAggLoadOfLValue - Given an expression with aggregate type that
51 /// represents a value lvalue, this method emits the address of the lvalue,
52 /// then loads the result into DestPtr.
53 void EmitAggLoadOfLValue(const Expr *E);
Eli Friedman922696f2008-05-19 17:51:16 +000054
Mike Stump4ac20dd2009-05-23 20:28:01 +000055 /// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +000056 void EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore = false);
57 void EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore = false);
Mike Stump4ac20dd2009-05-23 20:28:01 +000058
Chris Lattneree755f92007-08-21 04:59:27 +000059 //===--------------------------------------------------------------------===//
60 // Visitor Methods
61 //===--------------------------------------------------------------------===//
62
Chris Lattner9c033562007-08-21 04:25:47 +000063 void VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000064 CGF.ErrorUnsupported(S, "aggregate expression");
Chris Lattner9c033562007-08-21 04:25:47 +000065 }
66 void VisitParenExpr(ParenExpr *PE) { Visit(PE->getSubExpr()); }
Eli Friedman12444a22009-01-27 09:03:41 +000067 void VisitUnaryExtension(UnaryOperator *E) { Visit(E->getSubExpr()); }
Chris Lattner9c033562007-08-21 04:25:47 +000068
69 // l-values.
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000070 void VisitDeclRefExpr(DeclRefExpr *DRE) { EmitAggLoadOfLValue(DRE); }
71 void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); }
72 void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); }
Seo Sanghyeonad6ebd62007-12-23 03:11:58 +000073 void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000074 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
75 EmitAggLoadOfLValue(E);
76 }
Seo Sanghyeon9b73b392007-12-14 02:04:12 +000077 void VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
78 EmitAggLoadOfLValue(E);
79 }
Chris Lattnerf0a990c2009-04-21 23:00:09 +000080 void VisitBlockDeclRefExpr(const BlockDeclRefExpr *E) {
81 EmitAggLoadOfLValue(E);
82 }
83 void VisitPredefinedExpr(const PredefinedExpr *E) {
84 EmitAggLoadOfLValue(E);
85 }
Douglas Gregor4c678342009-01-28 21:54:33 +000086
Chris Lattner9c033562007-08-21 04:25:47 +000087 // Operators.
Nuno Lopes7e916272009-01-15 20:14:33 +000088 void VisitCStyleCastExpr(CStyleCastExpr *E);
Anders Carlssone4707ff2008-01-14 06:28:57 +000089 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Anders Carlsson148fe672007-10-31 22:04:46 +000090 void VisitCallExpr(const CallExpr *E);
Chris Lattnerb2d963f2007-08-31 22:54:14 +000091 void VisitStmtExpr(const StmtExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +000092 void VisitBinaryOperator(const BinaryOperator *BO);
Chris Lattner03d6fb92007-08-21 04:43:17 +000093 void VisitBinAssign(const BinaryOperator *E);
Eli Friedman07fa52a2008-05-20 07:56:31 +000094 void VisitBinComma(const BinaryOperator *E);
Chris Lattner9c033562007-08-21 04:25:47 +000095
Chris Lattner8fdf3282008-06-24 17:04:18 +000096 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Daniel Dunbar0a04d772008-08-23 10:51:21 +000097 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
98 EmitAggLoadOfLValue(E);
99 }
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000100 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000101 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Chris Lattner9c033562007-08-21 04:25:47 +0000102
103 void VisitConditionalOperator(const ConditionalOperator *CO);
Devang Patel636c3d02007-10-26 17:44:44 +0000104 void VisitInitListExpr(InitListExpr *E);
Chris Lattner04421082008-04-08 04:40:51 +0000105 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
106 Visit(DAE->getExpr());
107 }
Anders Carlsson31ccf372009-05-03 17:47:16 +0000108 void VisitCXXConstructExpr(const CXXConstructExpr *E);
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000109 void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
110
Eli Friedmanb1851242008-05-27 15:51:49 +0000111 void VisitVAArgExpr(VAArgExpr *E);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000112
113 void EmitInitializationToLValue(Expr *E, LValue Address);
114 void EmitNullInitializationToLValue(LValue Address, QualType T);
Chris Lattner9c033562007-08-21 04:25:47 +0000115 // case Expr::ChooseExprClass:
Lauro Ramos Venancio305762c2008-02-18 22:44:02 +0000116
Chris Lattner9c033562007-08-21 04:25:47 +0000117};
118} // end anonymous namespace.
119
Chris Lattneree755f92007-08-21 04:59:27 +0000120//===----------------------------------------------------------------------===//
121// Utilities
122//===----------------------------------------------------------------------===//
Chris Lattner9c033562007-08-21 04:25:47 +0000123
Chris Lattner883f6a72007-08-11 00:04:45 +0000124/// EmitAggLoadOfLValue - Given an expression with aggregate type that
125/// represents a value lvalue, this method emits the address of the lvalue,
126/// then loads the result into DestPtr.
Chris Lattner9c033562007-08-21 04:25:47 +0000127void AggExprEmitter::EmitAggLoadOfLValue(const Expr *E) {
128 LValue LV = CGF.EmitLValue(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000129 EmitFinalDestCopy(E, LV);
130}
131
132/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000133void AggExprEmitter::EmitFinalDestCopy(const Expr *E, RValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000134 assert(Src.isAggregate() && "value must be aggregate value!");
135
Chris Lattner883f6a72007-08-11 00:04:45 +0000136 // If the result is ignored, don't copy from the value.
Mike Stump9ccb1032009-05-23 22:01:27 +0000137 if (DestPtr == 0) {
Mike Stump49d1cd52009-05-26 22:03:21 +0000138 if (!Src.isVolatileQualified() || (IgnoreResult && Ignore))
Mike Stump9ccb1032009-05-23 22:01:27 +0000139 return;
Mike Stump49d1cd52009-05-26 22:03:21 +0000140 // If the source is volatile, we must read from it; to do that, we need
141 // some place to put it.
142 DestPtr = CGF.CreateTempAlloca(CGF.ConvertType(E->getType()), "agg.tmp");
Mike Stump9ccb1032009-05-23 22:01:27 +0000143 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000144
Mike Stump4ac20dd2009-05-23 20:28:01 +0000145 // If the result of the assignment is used, copy the LHS there also.
146 // FIXME: Pass VolatileDest as well. I think we also need to merge volatile
147 // from the source as well, as we can't eliminate it if either operand
148 // is volatile, unless copy has volatile for both source and destination..
Mike Stump27fe2e62009-05-23 22:29:41 +0000149 CGF.EmitAggregateCopy(DestPtr, Src.getAggregateAddr(), E->getType(),
150 VolatileDest|Src.isVolatileQualified());
Mike Stump4ac20dd2009-05-23 20:28:01 +0000151}
152
153/// EmitFinalDestCopy - Perform the final copy to DestPtr, if desired.
Mike Stump49d1cd52009-05-26 22:03:21 +0000154void AggExprEmitter::EmitFinalDestCopy(const Expr *E, LValue Src, bool Ignore) {
Mike Stump4ac20dd2009-05-23 20:28:01 +0000155 assert(Src.isSimple() && "Can't have aggregate bitfield, vector, etc");
156
157 EmitFinalDestCopy(E, RValue::getAggregate(Src.getAddress(),
Mike Stump49d1cd52009-05-26 22:03:21 +0000158 Src.isVolatileQualified()),
159 Ignore);
Chris Lattner883f6a72007-08-11 00:04:45 +0000160}
161
Chris Lattneree755f92007-08-21 04:59:27 +0000162//===----------------------------------------------------------------------===//
163// Visitor Methods
164//===----------------------------------------------------------------------===//
165
Nuno Lopes7e916272009-01-15 20:14:33 +0000166void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
167 // GCC union extension
168 if (E->getType()->isUnionType()) {
169 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000170 LValue FieldLoc = CGF.EmitLValueForField(DestPtr,
171 *SD->field_begin(CGF.getContext()),
172 true, 0);
Nuno Lopes7e916272009-01-15 20:14:33 +0000173 EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
174 return;
175 }
176
177 Visit(E->getSubExpr());
178}
179
Chris Lattner96196622008-07-26 22:37:01 +0000180void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000181 assert(CGF.getContext().typesAreCompatible(
Chris Lattner96196622008-07-26 22:37:01 +0000182 E->getSubExpr()->getType().getUnqualifiedType(),
183 E->getType().getUnqualifiedType()) &&
184 "Implicit cast types must be compatible");
Anders Carlssone4707ff2008-01-14 06:28:57 +0000185 Visit(E->getSubExpr());
186}
187
Chris Lattner96196622008-07-26 22:37:01 +0000188void AggExprEmitter::VisitCallExpr(const CallExpr *E) {
Anders Carlssone70e8f72009-05-27 16:45:02 +0000189 if (E->getCallReturnType()->isReferenceType()) {
190 EmitAggLoadOfLValue(E);
191 return;
192 }
193
Anders Carlsson148fe672007-10-31 22:04:46 +0000194 RValue RV = CGF.EmitCallExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000195 EmitFinalDestCopy(E, RV);
Anders Carlsson148fe672007-10-31 22:04:46 +0000196}
Chris Lattner96196622008-07-26 22:37:01 +0000197
198void AggExprEmitter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000199 RValue RV = CGF.EmitObjCMessageExpr(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000200 EmitFinalDestCopy(E, RV);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000201}
Anders Carlsson148fe672007-10-31 22:04:46 +0000202
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000203void AggExprEmitter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
204 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000205 EmitFinalDestCopy(E, RV);
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000206}
207
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000208void AggExprEmitter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
209 RValue RV = CGF.EmitObjCPropertyGet(E);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000210 EmitFinalDestCopy(E, RV);
Fariborz Jahanian5daf5702008-11-22 18:39:36 +0000211}
212
Chris Lattner96196622008-07-26 22:37:01 +0000213void AggExprEmitter::VisitBinComma(const BinaryOperator *E) {
Mike Stump9ccb1032009-05-23 22:01:27 +0000214 CGF.EmitAnyExpr(E->getLHS());
Mike Stumpf1b97f22009-05-23 21:40:07 +0000215 CGF.EmitAggExpr(E->getRHS(), DestPtr, VolatileDest);
Eli Friedman07fa52a2008-05-20 07:56:31 +0000216}
217
Chris Lattnerb2d963f2007-08-31 22:54:14 +0000218void AggExprEmitter::VisitStmtExpr(const StmtExpr *E) {
219 CGF.EmitCompoundStmt(*E->getSubStmt(), true, DestPtr, VolatileDest);
220}
221
Chris Lattner9c033562007-08-21 04:25:47 +0000222void AggExprEmitter::VisitBinaryOperator(const BinaryOperator *E) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000223 CGF.ErrorUnsupported(E, "aggregate binary expression");
Chris Lattneree755f92007-08-21 04:59:27 +0000224}
225
Chris Lattner03d6fb92007-08-21 04:43:17 +0000226void AggExprEmitter::VisitBinAssign(const BinaryOperator *E) {
Eli Friedmanff6e2b72008-02-11 01:09:17 +0000227 // For an assignment to work, the value on the right has
228 // to be compatible with the value on the left.
229 assert(CGF.getContext().typesAreCompatible(
230 E->getLHS()->getType().getUnqualifiedType(),
231 E->getRHS()->getType().getUnqualifiedType())
232 && "Invalid assignment");
Chris Lattner9c033562007-08-21 04:25:47 +0000233 LValue LHS = CGF.EmitLValue(E->getLHS());
Chris Lattner883f6a72007-08-11 00:04:45 +0000234
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000235 // We have to special case property setters, otherwise we must have
236 // a simple lvalue (no aggregates inside vectors, bitfields).
237 if (LHS.isPropertyRef()) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000238 llvm::Value *AggLoc = DestPtr;
239 if (!AggLoc)
240 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stump240993d2009-05-23 23:48:13 +0000241 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000242 CGF.EmitObjCPropertySet(LHS.getPropertyRefExpr(),
Mike Stump240993d2009-05-23 23:48:13 +0000243 RValue::getAggregate(AggLoc, VolatileDest));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000244 }
245 else if (LHS.isKVCRef()) {
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000246 llvm::Value *AggLoc = DestPtr;
247 if (!AggLoc)
248 AggLoc = CGF.CreateTempAlloca(CGF.ConvertType(E->getRHS()->getType()));
Mike Stumpa49af1a2009-05-23 23:52:31 +0000249 CGF.EmitAggExpr(E->getRHS(), AggLoc, VolatileDest);
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000250 CGF.EmitObjCPropertySet(LHS.getKVCRefExpr(),
Mike Stumpa49af1a2009-05-23 23:52:31 +0000251 RValue::getAggregate(AggLoc, VolatileDest));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000252 } else {
253 // Codegen the RHS so that it stores directly into the LHS.
Mike Stumpfde64202009-05-23 04:13:59 +0000254 CGF.EmitAggExpr(E->getRHS(), LHS.getAddress(), LHS.isVolatileQualified());
Mike Stump49d1cd52009-05-26 22:03:21 +0000255 EmitFinalDestCopy(E, LHS, true);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000256 }
Chris Lattner883f6a72007-08-11 00:04:45 +0000257}
258
Chris Lattner9c033562007-08-21 04:25:47 +0000259void AggExprEmitter::VisitConditionalOperator(const ConditionalOperator *E) {
Daniel Dunbar9615ecb2008-11-13 01:38:36 +0000260 llvm::BasicBlock *LHSBlock = CGF.createBasicBlock("cond.true");
261 llvm::BasicBlock *RHSBlock = CGF.createBasicBlock("cond.false");
262 llvm::BasicBlock *ContBlock = CGF.createBasicBlock("cond.end");
Chris Lattner883f6a72007-08-11 00:04:45 +0000263
Chris Lattner9c033562007-08-21 04:25:47 +0000264 llvm::Value *Cond = CGF.EvaluateExprAsBool(E->getCond());
Chris Lattnerbfc0c1a2007-08-26 23:13:56 +0000265 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000266
Chris Lattner9c033562007-08-21 04:25:47 +0000267 CGF.EmitBlock(LHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000268
269 // Handle the GNU extension for missing LHS.
270 assert(E->getLHS() && "Must have LHS for aggregate value");
271
Chris Lattnerc748f272007-08-21 05:02:10 +0000272 Visit(E->getLHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000273 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000274
Chris Lattner9c033562007-08-21 04:25:47 +0000275 CGF.EmitBlock(RHSBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000276
Chris Lattnerc748f272007-08-21 05:02:10 +0000277 Visit(E->getRHS());
Daniel Dunbard57a8712008-11-11 09:41:28 +0000278 CGF.EmitBranch(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000279
Chris Lattner9c033562007-08-21 04:25:47 +0000280 CGF.EmitBlock(ContBlock);
Chris Lattner883f6a72007-08-11 00:04:45 +0000281}
Chris Lattneree755f92007-08-21 04:59:27 +0000282
Eli Friedmanb1851242008-05-27 15:51:49 +0000283void AggExprEmitter::VisitVAArgExpr(VAArgExpr *VE) {
Daniel Dunbar07855702009-02-11 22:25:55 +0000284 llvm::Value *ArgValue = CGF.EmitVAListRef(VE->getSubExpr());
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000285 llvm::Value *ArgPtr = CGF.EmitVAArg(ArgValue, VE->getType());
286
Sebastian Redl0262f022009-01-09 21:09:38 +0000287 if (!ArgPtr) {
Anders Carlssonddf7cac2008-11-04 05:30:00 +0000288 CGF.ErrorUnsupported(VE, "aggregate va_arg expression");
Sebastian Redl0262f022009-01-09 21:09:38 +0000289 return;
290 }
291
Mike Stump4ac20dd2009-05-23 20:28:01 +0000292 EmitFinalDestCopy(VE, LValue::MakeAddr(ArgPtr, 0));
Eli Friedmanb1851242008-05-27 15:51:49 +0000293}
294
Anders Carlssonb14095a2009-04-17 00:06:03 +0000295void
Anders Carlsson31ccf372009-05-03 17:47:16 +0000296AggExprEmitter::VisitCXXConstructExpr(const CXXConstructExpr *E) {
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000297 llvm::Value *V = DestPtr;
Anders Carlssonb14095a2009-04-17 00:06:03 +0000298
Anders Carlsson7f6ad152009-05-19 04:48:36 +0000299 if (!V) {
300 assert(isa<CXXTempVarDecl>(E->getVarDecl()) &&
301 "Must have a temp var decl when there's no destination!");
302
303 V = CGF.CreateTempAlloca(CGF.ConvertType(E->getVarDecl()->getType()),
304 "tmpvar");
305 }
306
307 CGF.EmitCXXConstructExpr(V, E);
308}
309
310void AggExprEmitter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
311 // FIXME: Do something with the temporaries!
312 Visit(E->getSubExpr());
Anders Carlssonb14095a2009-04-17 00:06:03 +0000313}
314
Chris Lattnerf81557c2008-04-04 18:42:16 +0000315void AggExprEmitter::EmitInitializationToLValue(Expr* E, LValue LV) {
316 // FIXME: Are initializers affected by volatile?
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000317 if (isa<ImplicitValueInitExpr>(E)) {
Douglas Gregor4c678342009-01-28 21:54:33 +0000318 EmitNullInitializationToLValue(LV, E->getType());
Douglas Gregor3498bdb2009-01-29 17:44:32 +0000319 } else if (E->getType()->isComplexType()) {
320 CGF.EmitComplexExprIntoAddr(E, LV.getAddress(), false);
Eli Friedmanc8ba9612008-05-12 15:06:05 +0000321 } else if (CGF.hasAggregateLLVMType(E->getType())) {
322 CGF.EmitAnyExpr(E, LV.getAddress(), false);
323 } else {
324 CGF.EmitStoreThroughLValue(CGF.EmitAnyExpr(E), LV, E->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000325 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000326}
327
328void AggExprEmitter::EmitNullInitializationToLValue(LValue LV, QualType T) {
329 if (!CGF.hasAggregateLLVMType(T)) {
330 // For non-aggregates, we can store zero
Daniel Dunbar82397132008-08-06 05:32:55 +0000331 llvm::Value *Null = llvm::Constant::getNullValue(CGF.ConvertType(T));
332 CGF.EmitStoreThroughLValue(RValue::get(Null), LV, T);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000333 } else {
Chris Lattnerf81557c2008-04-04 18:42:16 +0000334 // Otherwise, just memset the whole thing to zero. This is legal
335 // because in LLVM, all default initializers are guaranteed to have a
336 // bit pattern of all zeros.
Eli Friedman0f593122009-04-13 21:47:26 +0000337 // FIXME: That isn't true for member pointers!
Chris Lattnerf81557c2008-04-04 18:42:16 +0000338 // There's a potential optimization opportunity in combining
339 // memsets; that would be easy for arrays, but relatively
340 // difficult for structures with the current code.
Eli Friedmanccf0ed82009-03-28 03:10:45 +0000341 CGF.EmitMemSetToZero(LV.getAddress(), T);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000342 }
343}
344
Chris Lattnerf81557c2008-04-04 18:42:16 +0000345void AggExprEmitter::VisitInitListExpr(InitListExpr *E) {
Eli Friedmana385b3c2008-12-02 01:17:45 +0000346#if 0
347 // FIXME: Disabled while we figure out what to do about
348 // test/CodeGen/bitfield.c
349 //
Mike Stumpf5408fe2009-05-16 07:57:57 +0000350 // If we can, prefer a copy from a global; this is a lot less code for long
351 // globals, and it's easier for the current optimizers to analyze.
352 // FIXME: Should we really be doing this? Should we try to avoid cases where
353 // we emit a global with a lot of zeros? Should we try to avoid short
354 // globals?
Eli Friedmanc9e8f602009-01-25 02:32:41 +0000355 if (E->isConstantInitializer(CGF.getContext(), 0)) {
Eli Friedman994ffef2008-11-30 02:11:09 +0000356 llvm::Constant* C = CGF.CGM.EmitConstantExpr(E, &CGF);
357 llvm::GlobalVariable* GV =
358 new llvm::GlobalVariable(C->getType(), true,
359 llvm::GlobalValue::InternalLinkage,
360 C, "", &CGF.CGM.getModule(), 0);
Mike Stump4ac20dd2009-05-23 20:28:01 +0000361 EmitFinalDestCopy(E, LValue::MakeAddr(GV, 0));
Eli Friedman994ffef2008-11-30 02:11:09 +0000362 return;
363 }
Eli Friedmana385b3c2008-12-02 01:17:45 +0000364#endif
Douglas Gregora9c87802009-01-29 19:42:23 +0000365 if (E->hadArrayRangeDesignator()) {
366 CGF.ErrorUnsupported(E, "GNU array range designator extension");
367 }
368
Chris Lattnerf81557c2008-04-04 18:42:16 +0000369 // Handle initialization of an array.
370 if (E->getType()->isArrayType()) {
371 const llvm::PointerType *APType =
372 cast<llvm::PointerType>(DestPtr->getType());
373 const llvm::ArrayType *AType =
374 cast<llvm::ArrayType>(APType->getElementType());
375
376 uint64_t NumInitElements = E->getNumInits();
Eli Friedman922696f2008-05-19 17:51:16 +0000377
Chris Lattner96196622008-07-26 22:37:01 +0000378 if (E->getNumInits() > 0) {
379 QualType T1 = E->getType();
380 QualType T2 = E->getInit(0)->getType();
381 if (CGF.getContext().getCanonicalType(T1).getUnqualifiedType() ==
382 CGF.getContext().getCanonicalType(T2).getUnqualifiedType()) {
383 EmitAggLoadOfLValue(E->getInit(0));
384 return;
385 }
Eli Friedman922696f2008-05-19 17:51:16 +0000386 }
387
Chris Lattnerf81557c2008-04-04 18:42:16 +0000388 uint64_t NumArrayElements = AType->getNumElements();
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000389 QualType ElementType = CGF.getContext().getCanonicalType(E->getType());
Douglas Gregor4c678342009-01-28 21:54:33 +0000390 ElementType = CGF.getContext().getAsArrayType(ElementType)->getElementType();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000391
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000392 unsigned CVRqualifier = ElementType.getCVRQualifiers();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000393
Chris Lattnerf81557c2008-04-04 18:42:16 +0000394 for (uint64_t i = 0; i != NumArrayElements; ++i) {
395 llvm::Value *NextVal = Builder.CreateStructGEP(DestPtr, i, ".array");
396 if (i < NumInitElements)
Eli Friedman1e692ac2008-06-13 23:01:12 +0000397 EmitInitializationToLValue(E->getInit(i),
398 LValue::MakeAddr(NextVal, CVRqualifier));
Chris Lattnerf81557c2008-04-04 18:42:16 +0000399 else
Eli Friedman1e692ac2008-06-13 23:01:12 +0000400 EmitNullInitializationToLValue(LValue::MakeAddr(NextVal, CVRqualifier),
Chris Lattnerf81557c2008-04-04 18:42:16 +0000401 ElementType);
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000402 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000403 return;
404 }
405
406 assert(E->getType()->isRecordType() && "Only support structs/unions here!");
407
408 // Do struct initialization; this code just sets each individual member
409 // to the approprate value. This makes bitfield support automatic;
410 // the disadvantage is that the generated code is more difficult for
411 // the optimizer, especially with bitfields.
412 unsigned NumInitElements = E->getNumInits();
413 RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
Chris Lattnerf81557c2008-04-04 18:42:16 +0000414 unsigned CurInitVal = 0;
Douglas Gregor0bb76892009-01-29 16:53:55 +0000415
416 if (E->getType()->isUnionType()) {
417 // Only initialize one field of a union. The field itself is
418 // specified by the initializer list.
419 if (!E->getInitializedFieldInUnion()) {
420 // Empty union; we have nothing to do.
421
422#ifndef NDEBUG
423 // Make sure that it's really an empty and not a failure of
424 // semantic analysis.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000425 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
426 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor0bb76892009-01-29 16:53:55 +0000427 Field != FieldEnd; ++Field)
428 assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
429#endif
430 return;
431 }
432
433 // FIXME: volatility
434 FieldDecl *Field = E->getInitializedFieldInUnion();
435 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, Field, true, 0);
436
437 if (NumInitElements) {
438 // Store the initializer into the field
439 EmitInitializationToLValue(E->getInit(0), FieldLoc);
440 } else {
441 // Default-initialize to null
442 EmitNullInitializationToLValue(FieldLoc, Field->getType());
443 }
444
445 return;
446 }
Chris Lattnerf81557c2008-04-04 18:42:16 +0000447
448 // Here we iterate over the fields; this makes it simpler to both
449 // default-initialize fields and skip over unnamed fields.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000450 for (RecordDecl::field_iterator Field = SD->field_begin(CGF.getContext()),
451 FieldEnd = SD->field_end(CGF.getContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000452 Field != FieldEnd; ++Field) {
453 // We're done once we hit the flexible array member
454 if (Field->getType()->isIncompleteArrayType())
455 break;
456
Douglas Gregor34e79462009-01-28 23:36:17 +0000457 if (Field->isUnnamedBitfield())
Chris Lattnerf81557c2008-04-04 18:42:16 +0000458 continue;
Douglas Gregor34e79462009-01-28 23:36:17 +0000459
Eli Friedman1e692ac2008-06-13 23:01:12 +0000460 // FIXME: volatility
Douglas Gregor0bb76892009-01-29 16:53:55 +0000461 LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *Field, false, 0);
Chris Lattnerf81557c2008-04-04 18:42:16 +0000462 if (CurInitVal < NumInitElements) {
463 // Store the initializer into the field
Chris Lattnerf81557c2008-04-04 18:42:16 +0000464 EmitInitializationToLValue(E->getInit(CurInitVal++), FieldLoc);
465 } else {
466 // We're out of initalizers; default-initialize to null
Douglas Gregor44b43212008-12-11 16:49:14 +0000467 EmitNullInitializationToLValue(FieldLoc, Field->getType());
Chris Lattnerf81557c2008-04-04 18:42:16 +0000468 }
Lauro Ramos Venancio145cd892008-02-19 19:27:31 +0000469 }
Devang Patel636c3d02007-10-26 17:44:44 +0000470}
471
Chris Lattneree755f92007-08-21 04:59:27 +0000472//===----------------------------------------------------------------------===//
473// Entry Points into this File
474//===----------------------------------------------------------------------===//
475
Mike Stumpe1129a92009-05-26 18:57:45 +0000476/// EmitAggExpr - Emit the computation of the specified expression of aggregate
477/// type. The result is computed into DestPtr. Note that if DestPtr is null,
478/// the value of the aggregate expression is not needed. If VolatileDest is
479/// true, DestPtr cannot be 0.
Chris Lattneree755f92007-08-21 04:59:27 +0000480void CodeGenFunction::EmitAggExpr(const Expr *E, llvm::Value *DestPtr,
Mike Stump49d1cd52009-05-26 22:03:21 +0000481 bool VolatileDest, bool IgnoreResult) {
Chris Lattneree755f92007-08-21 04:59:27 +0000482 assert(E && hasAggregateLLVMType(E->getType()) &&
483 "Invalid aggregate expression to emit");
Mike Stumpe1129a92009-05-26 18:57:45 +0000484 assert ((DestPtr != 0 || VolatileDest == false)
485 && "volatile aggregate can't be 0");
Chris Lattneree755f92007-08-21 04:59:27 +0000486
Mike Stump49d1cd52009-05-26 22:03:21 +0000487 AggExprEmitter(*this, DestPtr, VolatileDest, IgnoreResult)
488 .Visit(const_cast<Expr*>(E));
Chris Lattneree755f92007-08-21 04:59:27 +0000489}
Daniel Dunbar7482d122008-09-09 20:49:46 +0000490
491void CodeGenFunction::EmitAggregateClear(llvm::Value *DestPtr, QualType Ty) {
492 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
493
494 EmitMemSetToZero(DestPtr, Ty);
495}
496
497void CodeGenFunction::EmitAggregateCopy(llvm::Value *DestPtr,
Mike Stump27fe2e62009-05-23 22:29:41 +0000498 llvm::Value *SrcPtr, QualType Ty,
499 bool isVolatile) {
Daniel Dunbar7482d122008-09-09 20:49:46 +0000500 assert(!Ty->isAnyComplexType() && "Shouldn't happen for complex");
501
Chris Lattner83c96292009-02-28 18:31:01 +0000502 // Aggregate assignment turns into llvm.memcpy. This is almost valid per
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000503 // C99 6.5.16.1p3, which states "If the value being stored in an object is
504 // read from another object that overlaps in anyway the storage of the first
505 // object, then the overlap shall be exact and the two objects shall have
506 // qualified or unqualified versions of a compatible type."
507 //
Chris Lattner83c96292009-02-28 18:31:01 +0000508 // memcpy is not defined if the source and destination pointers are exactly
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000509 // equal, but other compilers do this optimization, and almost every memcpy
510 // implementation handles this case safely. If there is a libc that does not
511 // safely handle this, we can add a target hook.
Daniel Dunbar7482d122008-09-09 20:49:46 +0000512 const llvm::Type *BP = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
513 if (DestPtr->getType() != BP)
514 DestPtr = Builder.CreateBitCast(DestPtr, BP, "tmp");
515 if (SrcPtr->getType() != BP)
516 SrcPtr = Builder.CreateBitCast(SrcPtr, BP, "tmp");
517
518 // Get size and alignment info for this aggregate.
519 std::pair<uint64_t, unsigned> TypeInfo = getContext().getTypeInfo(Ty);
520
521 // FIXME: Handle variable sized types.
522 const llvm::Type *IntPtr = llvm::IntegerType::get(LLVMPointerWidth);
523
Mike Stumpfde64202009-05-23 04:13:59 +0000524 // FIXME: If we have a volatile struct, the optimizer can remove what might
525 // appear to be `extra' memory ops:
526 //
527 // volatile struct { int i; } a, b;
528 //
529 // int main() {
530 // a = b;
531 // a = b;
532 // }
533 //
Mike Stump49d1cd52009-05-26 22:03:21 +0000534 // we need to use a differnt call here. We use isVolatile to indicate when
535 // either the source or the destination is volatile.
Chris Lattnerca4fc2c2009-02-28 18:18:58 +0000536 Builder.CreateCall4(CGM.getMemCpyFn(),
Daniel Dunbar7482d122008-09-09 20:49:46 +0000537 DestPtr, SrcPtr,
538 // TypeInfo.first describes size in bits.
539 llvm::ConstantInt::get(IntPtr, TypeInfo.first/8),
540 llvm::ConstantInt::get(llvm::Type::Int32Ty,
541 TypeInfo.second/8));
542}