blob: ab4f8f19353c1de9226c0a1819fb4cc43c673052 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
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.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Mike Stump15037ca2009-12-15 00:35:12 +000020#include "llvm/Intrinsics.h"
Mike Stump41513442009-12-15 00:59:40 +000021#include "clang/CodeGen/CodeGenOptions.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000022#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24using namespace CodeGen;
25
26//===--------------------------------------------------------------------===//
27// Miscellaneous Helper Methods
28//===--------------------------------------------------------------------===//
29
30/// CreateTempAlloca - This creates a alloca and inserts it into the entry
31/// block.
32llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000033 const llvm::Twine &Name) {
Chris Lattnerf1466842009-03-22 00:24:14 +000034 if (!Builder.isNamePreserving())
Daniel Dunbar259e9cc2009-10-19 01:21:05 +000035 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateld35e2e02009-10-12 22:29:02 +000036 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Reid Spencer5f016e22007-07-11 17:01:13 +000037}
38
39/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
40/// expression and compare the result against zero, returning an Int1Ty value.
41llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000042 QualType BoolTy = getContext().BoolTy;
Eli Friedman3a173702009-12-11 09:26:29 +000043 if (E->getType()->isMemberFunctionPointerType()) {
44 llvm::Value *Ptr = CreateTempAlloca(ConvertType(E->getType()));
45 EmitAggExpr(E, Ptr, /*VolatileDest=*/false);
46
47 // Get the pointer.
48 llvm::Value *FuncPtr = Builder.CreateStructGEP(Ptr, 0, "src.ptr");
49 FuncPtr = Builder.CreateLoad(FuncPtr);
50
51 llvm::Value *IsNotNull =
52 Builder.CreateICmpNE(FuncPtr,
53 llvm::Constant::getNullValue(FuncPtr->getType()),
54 "tobool");
55
56 return IsNotNull;
57 }
Chris Lattner9b2dc282008-04-04 16:54:41 +000058 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000059 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000060
Chris Lattner9069fa22007-08-26 16:46:58 +000061 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
Chris Lattner9b655512007-08-31 22:49:20 +000064/// EmitAnyExpr - Emit code to compute the specified expression which can have
65/// any type. The result is returned as an RValue struct. If this is an
Mike Stumpdb52dcd2009-09-09 13:00:44 +000066/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
67/// result should be returned.
68RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000069 bool IsAggLocVolatile, bool IgnoreResult,
70 bool IsInitializer) {
Chris Lattner9b655512007-08-31 22:49:20 +000071 if (!hasAggregateLLVMType(E->getType()))
Mike Stump7f79f9b2009-05-29 15:46:01 +000072 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattner9b2dc282008-04-04 16:54:41 +000073 else if (E->getType()->isAnyComplexType())
Mike Stump7f79f9b2009-05-29 15:46:01 +000074 return RValue::getComplex(EmitComplexExpr(E, false, false,
75 IgnoreResult, IgnoreResult));
Mike Stumpdb52dcd2009-09-09 13:00:44 +000076
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000077 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
78 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner9b655512007-08-31 22:49:20 +000079}
80
Mike Stumpdb52dcd2009-09-09 13:00:44 +000081/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
82/// always be accessible even if no aggregate location is provided.
83RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000084 bool IsAggLocVolatile,
85 bool IsInitializer) {
86 llvm::Value *AggLoc = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +000087
88 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar46f45b92008-09-09 01:06:48 +000089 !E->getType()->isAnyComplexType())
90 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +000091 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000092 IsInitializer);
Daniel Dunbar46f45b92008-09-09 01:06:48 +000093}
94
Anders Carlsson3aba0932010-01-31 18:34:51 +000095llvm::Value *
96CodeGenFunction::EmitCXXBindReferenceExpr(const CXXBindReferenceExpr *E) {
97 QualType T = E->getType();
98 assert(T->isAnyComplexType() && "FIXME: Unhandled bind expression!");
99
100 const Expr *SubExpr = E->getSubExpr();
101
102 if (!E->requiresTemporaryCopy())
103 return EmitLValue(SubExpr).getAddress();
104
105 llvm::Value *Value = CreateTempAlloca(ConvertTypeForMem(T), "reftmp");
106
107 if (T->isAnyComplexType())
108 EmitComplexExprIntoAddr(SubExpr, Value, /*DestIsVolatile=*/false);
109 else
110 assert(false && "Unhandled bind expression");
111
112 return Value;
113}
114
Anders Carlsson4029ca72009-05-20 00:24:07 +0000115RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000116 QualType DestType,
117 bool IsInitializer) {
Anders Carlsson3aba0932010-01-31 18:34:51 +0000118 assert(!E->getType()->isAnyComplexType() &&
119 "Should not use this function for complex types!");
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000120 bool ShouldDestroyTemporaries = false;
121 unsigned OldNumLiveTemporaries = 0;
Eli Friedman27a9b722009-12-19 00:20:10 +0000122
123 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
124 E = DAE->getExpr();
125
Anders Carlssonb3f74422009-10-15 00:51:46 +0000126 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson0ece4912009-12-15 20:51:39 +0000127 ShouldDestroyTemporaries = true;
128
Chris Lattnereb99b012009-10-28 17:39:19 +0000129 // Keep track of the current cleanup stack depth.
Anders Carlsson0ece4912009-12-15 20:51:39 +0000130 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000131
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000132 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000133 }
134
Eli Friedman5df0d422009-05-20 02:31:19 +0000135 RValue Val;
136 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +0000137 // Emit the expr as an lvalue.
138 LValue LV = EmitLValue(E);
Eli Friedman5df0d422009-05-20 02:31:19 +0000139 if (LV.isSimple())
140 return RValue::get(LV.getAddress());
141 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000142
143 if (ShouldDestroyTemporaries) {
144 // Pop temporaries.
145 while (LiveTemporaries.size() > OldNumLiveTemporaries)
146 PopCXXTemporary();
147 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000148 } else {
Anders Carlssonb3f74422009-10-15 00:51:46 +0000149 const CXXRecordDecl *BaseClassDecl = 0;
150 const CXXRecordDecl *DerivedClassDecl = 0;
151
152 if (const CastExpr *CE =
153 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
154 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
155 E = CE->getSubExpr();
156
157 BaseClassDecl =
158 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
159 DerivedClassDecl =
160 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
161 }
162 }
163
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000164 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
165 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000166
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000167 if (ShouldDestroyTemporaries) {
168 // Pop temporaries.
169 while (LiveTemporaries.size() > OldNumLiveTemporaries)
170 PopCXXTemporary();
171 }
172
Anders Carlsson2da76932009-08-16 17:54:29 +0000173 if (IsInitializer) {
174 // We might have to destroy the temporary variable.
175 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
176 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
177 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000178 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000179 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000180
Mike Stumpd88ea562009-12-09 03:35:49 +0000181 {
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000182 DelayedCleanupBlock Scope(*this);
Mike Stumpd88ea562009-12-09 03:35:49 +0000183 EmitCXXDestructorCall(Dtor, Dtor_Complete,
184 Val.getAggregateAddr());
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000185
186 // Make sure to jump to the exit block.
187 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpd88ea562009-12-09 03:35:49 +0000188 }
189 if (Exceptions) {
190 EHCleanupBlock Cleanup(*this);
191 EmitCXXDestructorCall(Dtor, Dtor_Complete,
192 Val.getAggregateAddr());
193 }
Anders Carlsson2da76932009-08-16 17:54:29 +0000194 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000195 }
196 }
197 }
Anders Carlssonb3f74422009-10-15 00:51:46 +0000198
199 // Check if need to perform the derived-to-base cast.
200 if (BaseClassDecl) {
201 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000202 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +0000203 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
204 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000205 return RValue::get(Base);
206 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000207 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000208
209 if (Val.isAggregate()) {
210 Val = RValue::get(Val.getAggregateAddr());
211 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000212 // Create a temporary variable that we can bind the reference to.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000213 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlssone04d1c72009-05-20 01:03:17 +0000214 "reftmp");
Eli Friedman5df0d422009-05-20 02:31:19 +0000215 if (Val.isScalar())
216 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
217 else
218 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
219 Val = RValue::get(Temp);
Anders Carlssone04d1c72009-05-20 01:03:17 +0000220 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000221
222 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000223}
224
225
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000226/// getAccessedFieldNo - Given an encoded value and a result number, return the
227/// input field number being accessed.
228unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman4f8d1232008-05-22 00:50:06 +0000229 const llvm::Constant *Elts) {
230 if (isa<llvm::ConstantAggregateZero>(Elts))
231 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000232
Dan Gohman4f8d1232008-05-22 00:50:06 +0000233 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
234}
235
Mike Stumpb14e62d2009-12-16 02:57:00 +0000236void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
237 if (!CatchUndefined)
238 return;
239
240 const llvm::IntegerType *Size_tTy
241 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
242 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
243
244 const llvm::Type *ResType[] = {
245 Size_tTy
246 };
247 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
248 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
249 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
250 // In time, people may want to control this and use a 1 here.
251 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
252 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
253 llvm::BasicBlock *Cont = createBasicBlock();
254 llvm::BasicBlock *Check = createBasicBlock();
255 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
256 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
257
258 EmitBlock(Check);
259 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
260 llvm::ConstantInt::get(Size_tTy, Size)),
261 Cont, getTrapBB());
262 EmitBlock(Cont);
263}
Chris Lattner9b655512007-08-31 22:49:20 +0000264
Chris Lattnerdd36d322010-01-09 21:40:03 +0000265
266llvm::Value *CodeGenFunction::
267EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
268 bool isInc, bool isPre) {
269 QualType ValTy = E->getSubExpr()->getType();
270 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
271
272 int AmountVal = isInc ? 1 : -1;
273
274 if (ValTy->isPointerType() &&
275 ValTy->getAs<PointerType>()->isVariableArrayType()) {
276 // The amount of the addition/subtraction needs to account for the VLA size
277 ErrorUnsupported(E, "VLA pointer inc/dec");
278 }
279
280 llvm::Value *NextVal;
281 if (const llvm::PointerType *PT =
282 dyn_cast<llvm::PointerType>(InVal->getType())) {
283 llvm::Constant *Inc =
284 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
285 if (!isa<llvm::FunctionType>(PT->getElementType())) {
286 QualType PTEE = ValTy->getPointeeType();
287 if (const ObjCInterfaceType *OIT =
288 dyn_cast<ObjCInterfaceType>(PTEE)) {
289 // Handle interface types, which are not represented with a concrete
290 // type.
291 int size = getContext().getTypeSize(OIT) / 8;
292 if (!isInc)
293 size = -size;
294 Inc = llvm::ConstantInt::get(Inc->getType(), size);
295 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
296 InVal = Builder.CreateBitCast(InVal, i8Ty);
297 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
298 llvm::Value *lhs = LV.getAddress();
299 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
300 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
301 } else
302 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
303 } else {
304 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
305 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
306 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
307 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
308 }
309 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
310 // Bool++ is an interesting case, due to promotion rules, we get:
311 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
312 // Bool = ((int)Bool+1) != 0
313 // An interesting aspect of this is that increment is always true.
314 // Decrement does not have this property.
315 NextVal = llvm::ConstantInt::getTrue(VMContext);
316 } else if (isa<llvm::IntegerType>(InVal->getType())) {
317 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
318
319 // Signed integer overflow is undefined behavior.
320 if (ValTy->isSignedIntegerType())
321 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
322 else
323 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
324 } else {
325 // Add the inc/dec to the real part.
326 if (InVal->getType()->isFloatTy())
327 NextVal =
328 llvm::ConstantFP::get(VMContext,
329 llvm::APFloat(static_cast<float>(AmountVal)));
330 else if (InVal->getType()->isDoubleTy())
331 NextVal =
332 llvm::ConstantFP::get(VMContext,
333 llvm::APFloat(static_cast<double>(AmountVal)));
334 else {
335 llvm::APFloat F(static_cast<float>(AmountVal));
336 bool ignored;
337 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
338 &ignored);
339 NextVal = llvm::ConstantFP::get(VMContext, F);
340 }
341 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
342 }
343
344 // Store the updated result through the lvalue.
345 if (LV.isBitfield())
346 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
347 else
348 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
349
350 // If this is a postinc, return the value read from memory, otherwise use the
351 // updated value.
352 return isPre ? NextVal : InVal;
353}
354
355
356CodeGenFunction::ComplexPairTy CodeGenFunction::
357EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
358 bool isInc, bool isPre) {
359 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
360 LV.isVolatileQualified());
361
362 llvm::Value *NextVal;
363 if (isa<llvm::IntegerType>(InVal.first->getType())) {
364 uint64_t AmountVal = isInc ? 1 : -1;
365 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
366
367 // Add the inc/dec to the real part.
368 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
369 } else {
370 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
371 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
372 if (!isInc)
373 FVal.changeSign();
374 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
375
376 // Add the inc/dec to the real part.
377 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
378 }
379
380 ComplexPairTy IncVal(NextVal, InVal.second);
381
382 // Store the updated result through the lvalue.
383 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
384
385 // If this is a postinc, return the value read from memory, otherwise use the
386 // updated value.
387 return isPre ? IncVal : InVal;
388}
389
390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391//===----------------------------------------------------------------------===//
392// LValue Expression Emission
393//===----------------------------------------------------------------------===//
394
Daniel Dunbar13e81732009-02-05 07:09:07 +0000395RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000396 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000397 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000398
399 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000400 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000401 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000402 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000403 }
404
405 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000406 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000407 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000408 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000409
410 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000411}
412
Daniel Dunbar13e81732009-02-05 07:09:07 +0000413RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
414 const char *Name) {
415 ErrorUnsupported(E, Name);
416 return GetUndefRValue(E->getType());
417}
418
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000419LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
420 const char *Name) {
421 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000422 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000423 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000424 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000425}
426
Mike Stumpb14e62d2009-12-16 02:57:00 +0000427LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
428 LValue LV = EmitLValue(E);
429 if (!isa<DeclRefExpr>(E) && !LV.isBitfield() && LV.isSimple())
430 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
431 return LV;
432}
433
Reid Spencer5f016e22007-07-11 17:01:13 +0000434/// EmitLValue - Emit code to compute a designator that specifies the location
435/// of the expression.
436///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000437/// This can return one of two things: a simple address or a bitfield reference.
438/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
439/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000440///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000441/// If this returns a bitfield reference, nothing about the pointee type of the
442/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000443///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000444/// If this returns a normal address, and if the lvalue's C type is fixed size,
445/// this method guarantees that the returned pointer type will point to an LLVM
446/// type of the same size of the lvalue's type. If the lvalue has a variable
447/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000448///
449LValue CodeGenFunction::EmitLValue(const Expr *E) {
450 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000451 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000452
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000453 case Expr::ObjCIsaExprClass:
454 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000455 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000456 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000457 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000458 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000459 case Expr::CXXOperatorCallExprClass:
460 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000461 case Expr::VAArgExprClass:
462 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000463 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000464 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000466 case Expr::PredefinedExprClass:
467 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 case Expr::StringLiteralClass:
469 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000470 case Expr::ObjCEncodeExprClass:
471 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000472
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000473 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000474 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
475
Anders Carlssonb58d0172009-05-30 23:23:33 +0000476 case Expr::CXXTemporaryObjectExprClass:
477 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000478 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
479 case Expr::CXXBindTemporaryExprClass:
480 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000481 case Expr::CXXBindReferenceExprClass:
482 return EmitLValue(cast<CXXBindReferenceExpr>(E)->getSubExpr());
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000483 case Expr::CXXExprWithTemporariesClass:
484 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000485 case Expr::CXXZeroInitValueExprClass:
486 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
487 case Expr::CXXDefaultArgExprClass:
488 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000489 case Expr::CXXTypeidExprClass:
490 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000491
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000492 case Expr::ObjCMessageExprClass:
493 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000494 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000495 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000496 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000497 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000498 case Expr::ObjCImplicitSetterGetterRefExprClass:
499 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000500 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000501 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000502
Chris Lattner65459942009-04-25 19:35:26 +0000503 case Expr::StmtExprClass:
504 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000505 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000506 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
507 case Expr::ArraySubscriptExprClass:
508 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000509 case Expr::ExtVectorElementExprClass:
510 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000511 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000512 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000513 case Expr::CompoundLiteralExprClass:
514 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000515 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000516 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000517 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000518 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000519 case Expr::ImplicitCastExprClass:
520 case Expr::CStyleCastExprClass:
521 case Expr::CXXFunctionalCastExprClass:
522 case Expr::CXXStaticCastExprClass:
523 case Expr::CXXDynamicCastExprClass:
524 case Expr::CXXReinterpretCastExprClass:
525 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000526 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 }
528}
529
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000530llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
531 QualType Ty) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000532 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
533 if (Volatile)
534 Load->setVolatile(true);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000535
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000536 // Bool can have different representation in memory than in registers.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000537 llvm::Value *V = Load;
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000538 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000539 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
540 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000541
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000542 return V;
543}
544
545void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000546 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000547
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000548 if (Ty->isBooleanType()) {
549 // Bool can have different representation in memory than in registers.
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000550 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedman2b06d342009-12-01 22:31:51 +0000551 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000552 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000553 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000554}
555
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000556/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
557/// method emits the address of the lvalue, then loads the result as an rvalue,
558/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000559RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000560 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000561 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000562 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000563 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
564 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000565 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 if (LV.isSimple()) {
568 llvm::Value *Ptr = LV.getAddress();
569 const llvm::Type *EltTy =
570 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000571
Reid Spencer5f016e22007-07-11 17:01:13 +0000572 // Simple scalar l-value.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000573 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000574 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000575 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000576
Chris Lattner883f6a72007-08-11 00:04:45 +0000577 assert(ExprType->isFunctionType() && "Unknown scalar value");
578 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000580
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000582 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
583 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
585 "vecext"));
586 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000587
588 // If this is a reference to a subset of the elements of a vector, either
589 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000590 if (LV.isExtVectorElt())
591 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000592
593 if (LV.isBitfield())
594 return EmitLoadOfBitfieldLValue(LV, ExprType);
595
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000596 if (LV.isPropertyRef())
597 return EmitLoadOfPropertyRefLValue(LV, ExprType);
598
Chris Lattner73525de2009-02-16 21:11:58 +0000599 assert(LV.isKVCRef() && "Unknown LValue type!");
600 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000601}
602
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000603RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
604 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000605 unsigned StartBit = LV.getBitfieldStartBit();
606 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000607 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000608
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000609 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000610 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000611 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000612
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000613 // In some cases the bitfield may straddle two memory locations. Currently we
614 // load the entire bitfield, then do the magic to sign-extend it if
615 // necessary. This results in somewhat more code than necessary for the common
616 // case (one load), since two shifts accomplish both the masking and sign
617 // extension.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000618 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
619 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000620
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000621 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000622 if (StartBit)
Chris Lattner86b85b22009-12-06 17:22:42 +0000623 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000624
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000625 // Mask off unused bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000626 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000627 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000628 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000629
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000630 // Fetch the high bits if necessary.
631 if (LowBits < BitfieldSize) {
632 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000633 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000634 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
635 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000636 LV.isVolatileQualified(),
637 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000638
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000639 // Mask off unused bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000640 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
641 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000642 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000643
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000644 // Shift to proper location and or in to bitfield value.
Chris Lattner86b85b22009-12-06 17:22:42 +0000645 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000646 Val = Builder.CreateOr(Val, HighVal, "bf.val");
647 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000648
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000649 // Sign extend if necessary.
650 if (LV.isBitfieldSigned()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000651 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000652 EltTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000653 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000654 ExtraBits, "bf.val.sext");
655 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000656
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000657 // The bitfield type and the normal type differ when the storage sizes differ
658 // (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000659 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000660
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000661 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000662}
663
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000664RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
665 QualType ExprType) {
666 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
667}
668
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000669RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
670 QualType ExprType) {
671 return EmitObjCPropertyGet(LV.getKVCRefExpr());
672}
673
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000674// If this is a reference to a subset of the elements of a vector, create an
675// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000676RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
677 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000678 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
679 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000680
Nate Begeman8a997642008-05-09 06:41:27 +0000681 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000682
683 // If the result of the expression is a non-vector type, we must be extracting
684 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000685 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000686 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000687 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000688 llvm::Value *Elt = llvm::ConstantInt::get(
689 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000690 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
691 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000692
693 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000694 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000695
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000696 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000697 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000698 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000699 Mask.push_back(llvm::ConstantInt::get(
700 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000701 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000702
Owen Anderson4a289322009-07-28 21:22:35 +0000703 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000704 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000705 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000706 MaskV, "tmp");
707 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000708}
709
710
Reid Spencer5f016e22007-07-11 17:01:13 +0000711
712/// EmitStoreThroughLValue - Store the specified rvalue into the specified
713/// lvalue, where both are guaranteed to the have the same type, and that type
714/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000715void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000717 if (!Dst.isSimple()) {
718 if (Dst.isVectorElt()) {
719 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000720 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
721 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000722 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000723 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000724 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000725 return;
726 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000727
Nate Begeman213541a2008-04-18 23:10:10 +0000728 // If this is an update of extended vector elements, insert them as
729 // appropriate.
730 if (Dst.isExtVectorElt())
731 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000732
733 if (Dst.isBitfield())
734 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
735
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000736 if (Dst.isPropertyRef())
737 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
738
Chris Lattnereb99b012009-10-28 17:39:19 +0000739 assert(Dst.isKVCRef() && "Unknown LValue type");
740 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000741 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000742
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000743 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000744 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000745 llvm::Value *LvalueDst = Dst.getAddress();
746 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000747 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000748 return;
749 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000750
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000751 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000752 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000753 llvm::Value *LvalueDst = Dst.getAddress();
754 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000755 if (Dst.isObjCIvar()) {
756 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
757 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
758 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000759 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000760 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
761 llvm::Value *LHS =
762 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
763 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000764 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000765 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000766 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000767 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
768 else
769 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000770 return;
771 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000772
Chris Lattner883f6a72007-08-11 00:04:45 +0000773 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000774 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
775 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776}
777
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000778void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000779 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000780 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000781 unsigned StartBit = Dst.getBitfieldStartBit();
782 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000783 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000784
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000785 const llvm::Type *EltTy =
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000786 cast<llvm::PointerType>(Ptr->getType())->getElementType();
787 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
788
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000789 // Get the new value, cast to the appropriate type and masked to exactly the
790 // size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000791 llvm::Value *SrcVal = Src.getScalarVal();
792 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000793 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
794 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000795 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000796
Daniel Dunbared3849b2008-11-19 09:36:46 +0000797 // Return the new value of the bit-field, if requested.
798 if (Result) {
799 // Cast back to the proper type for result.
800 const llvm::Type *SrcTy = SrcVal->getType();
801 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
802 "bf.reload.val");
803
804 // Sign extend if necessary.
805 if (Dst.isBitfieldSigned()) {
806 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000807 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000808 SrcTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000809 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbared3849b2008-11-19 09:36:46 +0000810 ExtraBits, "bf.reload.sext");
811 }
812
813 *Result = SrcTrunc;
814 }
815
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000816 // In some cases the bitfield may straddle two memory locations. Emit the low
817 // part first and check to see if the high needs to be done.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000818 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
819 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
820 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000821
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000822 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000823 llvm::Constant *InvMask =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000824 llvm::ConstantInt::get(VMContext,
825 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000826
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000827 // Compute the new low part as
828 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
829 // with the shift of NewVal implicitly stripping the high bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000830 llvm::Value *NewLowVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000831 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000832 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
833 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000834
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000835 // Write back.
836 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000837
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000838 // If the low part doesn't cover the bitfield emit a high part.
839 if (LowBits < BitfieldSize) {
840 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000841 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000842 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
843 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000844 Dst.isVolatileQualified(),
845 "bf.prev.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000846
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000847 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000848 llvm::Constant *InvMask =
849 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000850 HighBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000851
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000852 // Compute the new high part as
853 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
854 // where the high bits of NewVal have already been cleared and the
855 // shift stripping the low bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000856 llvm::Value *NewHighVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000857 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000858 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
859 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000860
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000861 // Write back.
862 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
863 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000864}
865
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000866void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
867 LValue Dst,
868 QualType Ty) {
869 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
870}
871
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000872void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
873 LValue Dst,
874 QualType Ty) {
875 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
876}
877
Nate Begeman213541a2008-04-18 23:10:10 +0000878void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
879 LValue Dst,
880 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000881 // This access turns into a read/modify/write of the vector. Load the input
882 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000883 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
884 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000885 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000886
Chris Lattner9b655512007-08-31 22:49:20 +0000887 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000888
John McCall183700f2009-09-21 23:43:11 +0000889 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000890 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000891 unsigned NumDstElts =
892 cast<llvm::VectorType>(Vec->getType())->getNumElements();
893 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000894 // Use shuffle vector is the src and destination are the same number of
895 // elements and restore the vector mask since it is on the side it will be
896 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000897 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000898 for (unsigned i = 0; i != NumSrcElts; ++i) {
899 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000900 Mask[InIdx] = llvm::ConstantInt::get(
901 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000902 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000903
Owen Anderson4a289322009-07-28 21:22:35 +0000904 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000905 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000906 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000907 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000908 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000909 // Extended the source vector to the same length and then shuffle it
910 // into the destination.
911 // FIXME: since we're shuffling with undef, can we just use the indices
912 // into that? This could be simpler.
913 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000914 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000915 unsigned i;
916 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000917 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000918 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000919 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000920 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000921 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000922 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000923 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000924 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000925 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000926 // build identity
927 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000928 for (unsigned i = 0; i != NumDstElts; ++i)
929 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
930
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000931 // modify when what gets shuffled in
932 for (unsigned i = 0; i != NumSrcElts; ++i) {
933 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000934 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000935 }
Owen Anderson4a289322009-07-28 21:22:35 +0000936 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000937 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000938 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000939 // We should never shorten the vector
940 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000941 }
942 } else {
943 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000944 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000945 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
946 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000947 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000948 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000949
Eli Friedman1e692ac2008-06-13 23:01:12 +0000950 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000951}
952
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000953// setObjCGCLValueClass - sets class of he lvalue for the purpose of
954// generating write-barries API. It is currently a global, ivar,
955// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000956static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
957 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000958 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000959 return;
960
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000961 if (isa<ObjCIvarRefExpr>(E)) {
962 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000963 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
964 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000965 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000966 return;
967 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000968
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000969 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
970 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
971 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
972 VD->isFileVarDecl())
973 LV.SetGlobalObjCRef(LV, true);
974 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000975 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000976 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000977 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000978
979 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000980 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000981 return;
982 }
983
984 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000985 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000986 if (LV.isObjCIvar()) {
987 // If cast is to a structure pointer, follow gcc's behavior and make it
988 // a non-ivar write-barrier.
989 QualType ExpTy = E->getType();
990 if (ExpTy->isPointerType())
991 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
992 if (ExpTy->isRecordType())
993 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000994 }
995 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000996 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000997 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000998 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000999 return;
1000 }
1001
1002 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001003 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +00001004 return;
1005 }
1006
1007 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001008 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001009 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001010 // Using array syntax to assigning to what an ivar points to is not
1011 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
1012 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001013 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
1014 // Using array syntax to assigning to what global points to is not
1015 // same as assigning to the global itself. {id *G;} G[i] = 0;
1016 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001017 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001018 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001019
1020 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001021 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001022 // We don't know if member is an 'ivar', but this flag is looked at
1023 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001024 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001025 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001026 }
1027}
1028
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001029static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1030 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +00001031 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001032 "Var decl must have external storage or be a file var decl!");
1033
1034 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1035 if (VD->getType()->isReferenceType())
1036 V = CGF.Builder.CreateLoad(V, "tmp");
1037 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1038 setObjCGCLValueClass(CGF.getContext(), E, LV);
1039 return LV;
1040}
1041
Eli Friedman9a146302009-11-26 06:08:14 +00001042static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1043 const Expr *E, const FunctionDecl *FD) {
1044 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1045 if (!FD->hasPrototype()) {
1046 if (const FunctionProtoType *Proto =
1047 FD->getType()->getAs<FunctionProtoType>()) {
1048 // Ugly case: for a K&R-style definition, the type of the definition
1049 // isn't the same as the type of a use. Correct for this with a
1050 // bitcast.
1051 QualType NoProtoType =
1052 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1053 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1054 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1055 }
1056 }
1057 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1058}
1059
Reid Spencer5f016e22007-07-11 17:01:13 +00001060LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001061 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001062
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001063 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001064
1065 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001066 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1067 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001068
1069 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1070
1071 llvm::Value *V = LocalDeclMap[VD];
1072 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1073
1074 Qualifiers Quals = MakeQualifiers(E->getType());
1075 // local variables do not get their gc attribute set.
1076 // local static?
1077 if (NonGCable) Quals.removeObjCGCAttr();
1078
1079 if (VD->hasAttr<BlocksAttr>()) {
1080 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00001081 V = Builder.CreateLoad(V);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001082 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1083 VD->getNameAsString());
1084 }
1085 if (VD->getType()->isReferenceType())
1086 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001087 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001088 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001089 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001090 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +00001091 }
1092
Eli Friedman9a146302009-11-26 06:08:14 +00001093 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1094 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +00001095
Anders Carlsson45147d02010-02-02 03:37:46 +00001096 // FIXME: the qualifier check does not seem sufficient here
Chris Lattnereb99b012009-10-28 17:39:19 +00001097 if (E->getQualifier()) {
Anders Carlsson45147d02010-02-02 03:37:46 +00001098 const FieldDecl *FD = cast<FieldDecl>(ND);
1099 llvm::Value *V = CGM.EmitPointerToDataMember(FD);
1100
1101 return LValue::MakeAddr(V, MakeQualifiers(FD->getType()));
Chris Lattner41110242008-06-17 18:05:57 +00001102 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001103
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001104 assert(false && "Unhandled DeclRefExpr");
1105
1106 // an invalid LValue, but the assert will
1107 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001108 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +00001109}
1110
Mike Stumpa99038c2009-02-28 09:07:16 +00001111LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001112 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +00001113}
1114
Reid Spencer5f016e22007-07-11 17:01:13 +00001115LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1116 // __extension__ doesn't affect lvalue-ness.
1117 if (E->getOpcode() == UnaryOperator::Extension)
1118 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001119
Chris Lattner96196622008-07-26 22:37:01 +00001120 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +00001121 switch (E->getOpcode()) {
1122 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +00001123 case UnaryOperator::Deref: {
1124 QualType T = E->getSubExpr()->getType()->getPointeeType();
1125 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001126
Chris Lattnereb99b012009-10-28 17:39:19 +00001127 Qualifiers Quals = MakeQualifiers(T);
1128 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +00001129
Chris Lattnereb99b012009-10-28 17:39:19 +00001130 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1131 // We should not generate __weak write barrier on indirect reference
1132 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1133 // But, we continue to generate __strong write barrier on indirect write
1134 // into a pointer to object.
1135 if (getContext().getLangOptions().ObjC1 &&
1136 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1137 LV.isObjCWeak())
1138 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1139 return LV;
1140 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001141 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +00001142 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +00001143 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +00001144 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1145 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +00001146 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +00001147 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +00001148 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001149 case UnaryOperator::PreInc:
Chris Lattner197a3382010-01-09 21:44:40 +00001150 case UnaryOperator::PreDec: {
1151 LValue LV = EmitLValue(E->getSubExpr());
1152 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1153
1154 if (E->getType()->isAnyComplexType())
1155 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1156 else
1157 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1158 return LV;
1159 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001160 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001161}
1162
1163LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +00001164 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1165 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001166}
1167
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001168LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001169 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1170 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001171}
1172
1173
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001174LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +00001175 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001176
1177 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +00001178 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001179 case PredefinedExpr::Func:
1180 GlobalVarName = "__func__.";
1181 break;
1182 case PredefinedExpr::Function:
1183 GlobalVarName = "__FUNCTION__.";
1184 break;
1185 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001186 GlobalVarName = "__PRETTY_FUNCTION__.";
1187 break;
Anders Carlsson22742662007-07-21 05:21:51 +00001188 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001189
Daniel Dunbar0a23d762009-09-12 23:06:21 +00001190 llvm::StringRef FnName = CurFn->getName();
1191 if (FnName.startswith("\01"))
1192 FnName = FnName.substr(1);
1193 GlobalVarName += FnName;
1194
Anders Carlsson3a082d82009-09-08 18:24:21 +00001195 std::string FunctionName =
Mike Stump1eb44332009-09-09 15:08:12 +00001196 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson3a082d82009-09-08 18:24:21 +00001197 CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001198
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001199 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001200 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +00001201 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001202}
1203
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001204LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001205 switch (E->getIdentType()) {
1206 default:
1207 return EmitUnsupportedLValue(E, "predefined expression");
1208 case PredefinedExpr::Func:
1209 case PredefinedExpr::Function:
1210 case PredefinedExpr::PrettyFunction:
1211 return EmitPredefinedFunctionName(E->getIdentType());
1212 }
Anders Carlsson22742662007-07-21 05:21:51 +00001213}
1214
Mike Stumpd8af3602009-12-15 01:22:35 +00001215llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump41513442009-12-15 00:59:40 +00001216 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1217
1218 // If we are not optimzing, don't collapse all calls to trap in the function
1219 // to the same call, that way, in the debugger they can see which operation
1220 // did in fact fail. If we are optimizing, we collpase all call to trap down
1221 // to just one per function to save on codesize.
1222 if (GCO.OptimizationLevel
1223 && TrapBB)
Mike Stump15037ca2009-12-15 00:35:12 +00001224 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001225
1226 llvm::BasicBlock *Cont = 0;
1227 if (HaveInsertPoint()) {
1228 Cont = createBasicBlock("cont");
1229 EmitBranch(Cont);
1230 }
Mike Stump15037ca2009-12-15 00:35:12 +00001231 TrapBB = createBasicBlock("trap");
1232 EmitBlock(TrapBB);
1233
1234 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1235 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1236 TrapCall->setDoesNotReturn();
1237 TrapCall->setDoesNotThrow();
Mike Stump9c276ae2009-12-12 01:27:46 +00001238 Builder.CreateUnreachable();
1239
1240 if (Cont)
1241 EmitBlock(Cont);
Mike Stump15037ca2009-12-15 00:35:12 +00001242 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001243}
1244
Reid Spencer5f016e22007-07-11 17:01:13 +00001245LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +00001246 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001247 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +00001248 QualType IdxTy = E->getIdx()->getType();
1249 bool IdxSigned = IdxTy->isSignedIntegerType();
1250
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 // If the base is a vector type, then we are forming a vector element lvalue
1252 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001253 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001255 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001256 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001257 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001258 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001259 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001260 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001262
Ted Kremenek23245122007-08-20 16:18:38 +00001263 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001264 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001265
Ted Kremenek23245122007-08-20 16:18:38 +00001266 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001268 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001269 Idx = Builder.CreateIntCast(Idx,
1270 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 IdxSigned, "idxprom");
1272
Mike Stumpb14e62d2009-12-16 02:57:00 +00001273 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stump9c276ae2009-12-12 01:27:46 +00001274 if (CatchUndefined) {
Mike Stumpb14e62d2009-12-16 02:57:00 +00001275 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stump9c276ae2009-12-12 01:27:46 +00001276 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1277 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1278 if (const ConstantArrayType *CAT
1279 = getContext().getAsConstantArrayType(DRE->getType())) {
1280 llvm::APInt Size = CAT->getSize();
1281 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump750c85e2009-12-14 22:14:31 +00001282 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stump9c276ae2009-12-12 01:27:46 +00001283 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stump15037ca2009-12-15 00:35:12 +00001284 Cont, getTrapBB());
Mike Stump96a063a2009-12-14 20:52:00 +00001285 EmitBlock(Cont);
Mike Stump9c276ae2009-12-12 01:27:46 +00001286 }
1287 }
1288 }
1289 }
1290 }
1291
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001292 // We know that the pointer points to a type of the correct size, unless the
1293 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001294 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001295 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001296 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001297 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001298
Anders Carlsson8b33c082008-12-21 00:11:23 +00001299 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001300
Anders Carlsson6183a992008-12-21 03:44:36 +00001301 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001302
Ken Dyck199c3d62010-01-11 17:06:35 +00001303 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson8b33c082008-12-21 00:11:23 +00001304 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001305 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001306 BaseTypeSize.getQuantity()));
Dan Gohman664f8932009-08-12 00:33:55 +00001307 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001308 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001309 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001310 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001311 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001312 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001313
Daniel Dunbar2a866252009-04-25 05:08:32 +00001314 Idx = Builder.CreateMul(Idx, InterfaceSize);
1315
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001316 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001317 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001318 Idx, "arrayidx");
1319 Address = Builder.CreateBitCast(Address, Base->getType());
1320 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001321 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001322 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001323
Steve Naroff14108da2009-07-10 23:34:53 +00001324 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001325 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001326 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001327
John McCall0953e762009-09-24 19:53:00 +00001328 Qualifiers Quals = MakeQualifiers(T);
1329 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1330
1331 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001332 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001333 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001334 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001335 setObjCGCLValueClass(getContext(), E, LV);
1336 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001337 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001338}
1339
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001340static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001341llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1342 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner998eab12009-12-23 21:31:11 +00001343 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001344
Nate Begeman3b8d1162008-05-13 21:03:02 +00001345 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001346 CElts.push_back(llvm::ConstantInt::get(
1347 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001348
Owen Anderson4a289322009-07-28 21:22:35 +00001349 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001350}
1351
Chris Lattner349aaec2007-08-02 23:37:31 +00001352LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001353EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner998eab12009-12-23 21:31:11 +00001354 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1355
Chris Lattner349aaec2007-08-02 23:37:31 +00001356 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001357 LValue Base;
1358
1359 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner998eab12009-12-23 21:31:11 +00001360 if (E->isArrow()) {
1361 // If it is a pointer to a vector, emit the address and form an lvalue with
1362 // it.
Chris Lattner2140e902009-02-16 22:14:05 +00001363 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner998eab12009-12-23 21:31:11 +00001364 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001365 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1366 Quals.removeObjCGCAttr();
1367 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner998eab12009-12-23 21:31:11 +00001368 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1369 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1370 // emit the base as an lvalue.
1371 assert(E->getBase()->getType()->isVectorType());
1372 Base = EmitLValue(E->getBase());
1373 } else {
1374 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar302c3c22010-01-04 18:02:28 +00001375 assert(E->getBase()->getType()->getAs<VectorType>() &&
1376 "Result must be a vector");
Chris Lattner998eab12009-12-23 21:31:11 +00001377 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1378
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001379 // Store the vector to memory (because LValue wants an address).
Chris Lattner998eab12009-12-23 21:31:11 +00001380 llvm::Value *VecMem =CreateTempAlloca(ConvertType(E->getBase()->getType()));
1381 Builder.CreateStore(Vec, VecMem);
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001382 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner998eab12009-12-23 21:31:11 +00001383 }
1384
Nate Begeman3b8d1162008-05-13 21:03:02 +00001385 // Encode the element access list into a vector of unsigned indices.
1386 llvm::SmallVector<unsigned, 4> Indices;
1387 E->getEncodedElementAccess(Indices);
1388
1389 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001390 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001391 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001392 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001393 }
1394 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1395
1396 llvm::Constant *BaseElts = Base.getExtVectorElts();
1397 llvm::SmallVector<llvm::Constant *, 4> CElts;
1398
1399 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1400 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001401 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001402 else
Chris Lattner67665862009-10-28 05:12:07 +00001403 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001404 }
Owen Anderson4a289322009-07-28 21:22:35 +00001405 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001406 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001407 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001408}
1409
Devang Patelb9b00ad2007-10-23 20:28:39 +00001410LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001411 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001412 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001413 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001414 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001415
Chris Lattner12f65f62007-12-02 18:52:07 +00001416 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +00001417 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001418 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001419 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001420 BaseExpr->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001421 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001422 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1423 isa<ObjCImplicitSetterGetterRefExpr>(
1424 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001425 RValue RV = EmitObjCPropertyGet(BaseExpr);
1426 BaseValue = RV.getAggregateAddr();
John McCall0953e762009-09-24 19:53:00 +00001427 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001428 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001429 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001430 if (BaseLV.isNonGC())
1431 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001432 // FIXME: this isn't right for bitfields.
1433 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001434 QualType BaseTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001435 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001436 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001437
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001438 NamedDecl *ND = E->getMemberDecl();
1439 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlssone6d2a532010-01-29 05:05:36 +00001440 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001441 BaseQuals.getCVRQualifiers());
1442 LValue::SetObjCNonGC(LV, isNonGC);
1443 setObjCGCLValueClass(getContext(), E, LV);
1444 return LV;
1445 }
1446
Anders Carlsson589f9e32009-11-07 23:16:50 +00001447 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1448 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001449
1450 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1451 return EmitFunctionDeclLValue(*this, E, FD);
1452
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001453 assert(false && "Unhandled member declaration!");
1454 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001455}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001456
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001457LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001458 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001459 unsigned CVRQualifiers) {
Anders Carlsson8330cee2009-07-23 17:01:21 +00001460 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1461
Mike Stumpf5408fe2009-05-16 07:57:57 +00001462 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1463 // FieldTy (the appropriate type is ABI-dependent).
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001464 const llvm::Type *FieldTy =
Daniel Dunbarbb767732009-02-17 18:31:04 +00001465 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001466 const llvm::PointerType *BaseTy =
1467 cast<llvm::PointerType>(BaseValue->getType());
1468 unsigned AS = BaseTy->getAddressSpace();
1469 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001470 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001471 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001472
1473 llvm::Value *Idx =
Owen Anderson0032b272009-08-13 21:57:51 +00001474 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8330cee2009-07-23 17:01:21 +00001475 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001476
Anders Carlsson8330cee2009-07-23 17:01:21 +00001477 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001478 Field->getType()->isSignedIntegerType(),
1479 Field->getType().getCVRQualifiers()|CVRQualifiers);
1480}
1481
Eli Friedman472778e2008-02-09 08:50:58 +00001482LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001483 const FieldDecl* Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001484 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001485 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001486 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001487
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001488 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001489 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001490
Devang Patelabad06c2007-10-26 19:42:18 +00001491 // Match union field type.
Anders Carlssone6d2a532010-01-29 05:05:36 +00001492 if (Field->getParent()->isUnion()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001493 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001494 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001495 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001496 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001497 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001498 V = Builder.CreateBitCast(V,
1499 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001500 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001501 }
Eli Friedman2be58612009-05-30 21:09:44 +00001502 if (Field->getType()->isReferenceType())
1503 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001504
1505 Qualifiers Quals = MakeQualifiers(Field->getType());
1506 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001507 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001508 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1509 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001510
John McCall0953e762009-09-24 19:53:00 +00001511 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001512}
1513
Anders Carlsson06a29702010-01-29 05:24:29 +00001514LValue
1515CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1516 const FieldDecl* Field,
1517 unsigned CVRQualifiers) {
1518 QualType FieldType = Field->getType();
1519
1520 if (!FieldType->isReferenceType())
1521 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1522
1523 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1524 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1525
1526 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1527
1528 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1529}
1530
Chris Lattner75dfeda2009-03-18 18:28:57 +00001531LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman06e863f2008-05-13 23:18:27 +00001532 const llvm::Type *LTy = ConvertType(E->getType());
1533 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1534
1535 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001536 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001537
Chris Lattnereb99b012009-10-28 17:39:19 +00001538 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001539 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001540 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001541 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001542 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001543 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001544
1545 return Result;
1546}
1547
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001548LValue
1549CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1550 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedmanab189952009-12-25 05:29:40 +00001551 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1552 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1553 if (Live)
1554 return EmitLValue(Live);
1555 }
1556
1557 if (!E->getLHS())
1558 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1559
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001560 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1561 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1562 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1563
Eli Friedman8e274bd2009-12-25 06:17:05 +00001564 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001565
1566 EmitBlock(LHSBlock);
Daniel Dunbar90345582009-03-24 02:38:23 +00001567
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001568 LValue LHS = EmitLValue(E->getLHS());
1569 if (!LHS.isSimple())
1570 return EmitUnsupportedLValue(E, "conditional operator");
1571
Chris Lattnereb99b012009-10-28 17:39:19 +00001572 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001573 Builder.CreateStore(LHS.getAddress(), Temp);
1574 EmitBranch(ContBlock);
1575
1576 EmitBlock(RHSBlock);
1577 LValue RHS = EmitLValue(E->getRHS());
1578 if (!RHS.isSimple())
1579 return EmitUnsupportedLValue(E, "conditional operator");
1580
1581 Builder.CreateStore(RHS.getAddress(), Temp);
1582 EmitBranch(ContBlock);
1583
1584 EmitBlock(ContBlock);
1585
1586 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001587 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001588 }
1589
Daniel Dunbar90345582009-03-24 02:38:23 +00001590 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001591 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001592 !E->getType()->isAnyComplexType()) &&
1593 "Unexpected conditional operator!");
1594
1595 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1596 EmitAggExpr(E, Temp, false);
1597
John McCall0953e762009-09-24 19:53:00 +00001598 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar90345582009-03-24 02:38:23 +00001599}
1600
Mike Stumpc849c052009-11-16 06:50:58 +00001601/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1602/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1603/// otherwise if a cast is needed by the code generator in an lvalue context,
1604/// then it must mean that we need the address of an aggregate in order to
1605/// access one of its fields. This can happen for all the reasons that casts
1606/// are permitted with aggregate result, including noop aggregate casts, and
1607/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001608LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001609 switch (E->getCastKind()) {
1610 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001611 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1612
Mike Stumpc849c052009-11-16 06:50:58 +00001613 case CastExpr::CK_Dynamic: {
1614 LValue LV = EmitLValue(E->getSubExpr());
1615 llvm::Value *V = LV.getAddress();
1616 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1617 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1618 MakeQualifiers(E->getType()));
1619 }
1620
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001621 case CastExpr::CK_NoOp:
1622 case CastExpr::CK_ConstructorConversion:
1623 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahaniana7fa7cd2009-12-15 21:34:52 +00001624 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001625 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001626
1627 case CastExpr::CK_DerivedToBase: {
1628 const RecordType *DerivedClassTy =
1629 E->getSubExpr()->getType()->getAs<RecordType>();
1630 CXXRecordDecl *DerivedClassDecl =
1631 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001632
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001633 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1634 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1635
1636 LValue LV = EmitLValue(E->getSubExpr());
1637
1638 // Perform the derived-to-base conversion
1639 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001640 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1641 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001642
John McCall0953e762009-09-24 19:53:00 +00001643 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001644 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001645 case CastExpr::CK_ToUnion: {
1646 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1647 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001648
John McCall0953e762009-09-24 19:53:00 +00001649 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson658e8122009-11-14 21:21:42 +00001650 }
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001651 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001652 const RecordType *BaseClassTy =
1653 E->getSubExpr()->getType()->getAs<RecordType>();
1654 CXXRecordDecl *BaseClassDecl =
1655 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1656
1657 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1658 CXXRecordDecl *DerivedClassDecl =
1659 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1660
1661 LValue LV = EmitLValue(E->getSubExpr());
1662
1663 // Perform the base-to-derived conversion
1664 llvm::Value *Derived =
1665 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1666 DerivedClassDecl, /*NullCheckValue=*/false);
1667
1668 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001669 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001670 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001671 // This must be a reinterpret_cast (or c-style equivalent).
1672 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001673
1674 LValue LV = EmitLValue(E->getSubExpr());
1675 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1676 ConvertType(CE->getTypeAsWritten()));
1677 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1678 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001679 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001680}
1681
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001682LValue CodeGenFunction::EmitNullInitializationLValue(
1683 const CXXZeroInitValueExpr *E) {
1684 QualType Ty = E->getType();
1685 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1686 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
Ken Dyck3228f422010-01-26 18:35:11 +00001687 CharUnits Align = getContext().getTypeAlignInChars(Ty);
1688 Alloc->setAlignment(Align.getQuantity());
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001689 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1690 EmitMemSetToZero(lvalue.getAddress(), Ty);
1691 return lvalue;
1692}
1693
Reid Spencer5f016e22007-07-11 17:01:13 +00001694//===--------------------------------------------------------------------===//
1695// Expression Emission
1696//===--------------------------------------------------------------------===//
1697
Chris Lattner7016a702007-08-20 22:37:10 +00001698
Anders Carlssond2490a92009-12-24 20:40:36 +00001699RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1700 ReturnValueSlot ReturnValue) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001701 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001702 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssona1736c02009-12-24 21:13:40 +00001703 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001704
Anders Carlsson774e7c62009-04-03 22:50:24 +00001705 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssona1736c02009-12-24 21:13:40 +00001706 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001707
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001708 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001709 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1710 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1711 TargetDecl = DRE->getDecl();
1712 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001713 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001714 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001715 }
1716 }
1717
Chris Lattner5db7ae52009-06-13 00:26:38 +00001718 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001719 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssona1736c02009-12-24 21:13:40 +00001720 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001721
Eli Friedmanc4451db2009-12-08 02:09:46 +00001722 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregora71d8192009-09-04 17:36:40 +00001723 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001724 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001725 // operator (), and the result of such a call has type void. The only
1726 // effect is the evaluation of the postfix-expression before the dot or
1727 // arrow.
1728 EmitScalarExpr(E->getCallee());
1729 return RValue::get(0);
1730 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001731
Chris Lattner7f02f722007-08-24 05:35:26 +00001732 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlssond2490a92009-12-24 20:40:36 +00001733 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001734 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001735}
1736
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001737LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001738 // Comma expressions just emit their LHS then their RHS as an l-value.
1739 if (E->getOpcode() == BinaryOperator::Comma) {
1740 EmitAnyExpr(E->getLHS());
Eli Friedman130c69e2009-12-07 20:18:11 +00001741 EnsureInsertPoint();
Chris Lattner7a574cc2009-05-12 21:28:12 +00001742 return EmitLValue(E->getRHS());
1743 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001744
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001745 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1746 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001747 return EmitPointerToDataMemberBinaryExpr(E);
1748
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001749 // Can only get l-value for binary operator expressions which are a
1750 // simple assignment of aggregate type.
1751 if (E->getOpcode() != BinaryOperator::Assign)
1752 return EmitUnsupportedLValue(E, "binary l-value expression");
1753
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001754 if (!hasAggregateLLVMType(E->getType())) {
1755 // Emit the LHS as an l-value.
1756 LValue LV = EmitLValue(E->getLHS());
1757
1758 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1759 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1760 E->getType());
1761 return LV;
1762 }
1763
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001764 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1765 EmitAggExpr(E, Temp, false);
1766 // FIXME: Are these qualifiers correct?
John McCall0953e762009-09-24 19:53:00 +00001767 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001768}
1769
Christopher Lamb22c940e2007-12-29 05:02:41 +00001770LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001771 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001772
Chris Lattnereb99b012009-10-28 17:39:19 +00001773 if (!RV.isScalar())
1774 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1775
1776 assert(E->getCallReturnType()->isReferenceType() &&
1777 "Can't have a scalar return unless the return type is a "
1778 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001779
Chris Lattnereb99b012009-10-28 17:39:19 +00001780 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001781}
1782
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001783LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1784 // FIXME: This shouldn't require another copy.
1785 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1786 EmitAggExpr(E, Temp, false);
John McCall0953e762009-09-24 19:53:00 +00001787 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001788}
1789
Anders Carlssonb58d0172009-05-30 23:23:33 +00001790LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1791 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1792 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001793 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001794}
1795
Anders Carlssone61c9e82009-05-30 23:30:54 +00001796LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001797CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1798 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1799 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1800}
1801
1802LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001803CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1804 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001805 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001806 return LV;
1807}
1808
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001809LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1810 // Can only get l-value for message expression returning aggregate type
1811 RValue RV = EmitObjCMessageExpr(E);
1812 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001813 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001814}
1815
Daniel Dunbar2a031922009-04-22 05:08:15 +00001816llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001817 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001818 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001819}
1820
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001821LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1822 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001823 const ObjCIvarDecl *Ivar,
1824 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001825 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001826 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001827}
1828
1829LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001830 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1831 llvm::Value *BaseValue = 0;
1832 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001833 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001834 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001835 if (E->isArrow()) {
1836 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001837 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001838 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001839 } else {
1840 LValue BaseLV = EmitLValue(BaseExpr);
1841 // FIXME: this isn't right for bitfields.
1842 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001843 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001844 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001845 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001846
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001847 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001848 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1849 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001850 setObjCGCLValueClass(getContext(), E, LV);
1851 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001852}
1853
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001854LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001855CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001856 // This is a special l-value that just issues sends when we load or store
1857 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001858 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1859}
1860
Chris Lattnereb99b012009-10-28 17:39:19 +00001861LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001862 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001863 // This is a special l-value that just issues sends when we load or store
1864 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001865 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1866}
1867
Chris Lattnereb99b012009-10-28 17:39:19 +00001868LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001869 return EmitUnsupportedLValue(E, "use of super");
1870}
1871
Chris Lattner65459942009-04-25 19:35:26 +00001872LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001873 // Can only get l-value for message expression returning aggregate type
1874 RValue RV = EmitAnyExprToTemp(E);
1875 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001876 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001877}
1878
Anders Carlsson31777a22009-12-24 19:08:58 +00001879RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00001880 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001881 CallExpr::const_arg_iterator ArgBeg,
1882 CallExpr::const_arg_iterator ArgEnd,
1883 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001884 // Get the actual function type. The callee type will always be a pointer to
1885 // function type or a block pointer type.
1886 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001887 "Call must have function pointer type!");
1888
John McCall00a1ad92009-10-23 08:22:42 +00001889 CalleeType = getContext().getCanonicalType(CalleeType);
1890
1891 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1892 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001893
1894 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001895 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001896
Daniel Dunbar8a9f3fd2009-09-11 22:25:00 +00001897 // FIXME: We should not need to do this, it should be part of the function
1898 // type.
1899 unsigned CallingConvention = 0;
1900 if (const llvm::Function *F =
1901 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1902 CallingConvention = F->getCallingConv();
1903 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1904 CallingConvention),
Anders Carlssond2490a92009-12-24 20:40:36 +00001905 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001906}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001907
Chris Lattnereb99b012009-10-28 17:39:19 +00001908LValue CodeGenFunction::
1909EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001910 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001911 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001912 BaseV = EmitScalarExpr(E->getLHS());
1913 else
1914 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001915 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1916 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001917 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001918 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001919
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001920 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001921 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1922
1923 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001924 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001925 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001926}
1927