blob: 605f77975f9587dc0c480c0aafcba2c3d684ef50 [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 Carlsson4029ca72009-05-20 00:24:07 +000095RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson14c5cbf2009-08-16 07:36:22 +000096 QualType DestType,
97 bool IsInitializer) {
Anders Carlssone1b7ea12009-10-18 23:09:21 +000098 bool ShouldDestroyTemporaries = false;
99 unsigned OldNumLiveTemporaries = 0;
Eli Friedman27a9b722009-12-19 00:20:10 +0000100
101 if (const CXXDefaultArgExpr *DAE = dyn_cast<CXXDefaultArgExpr>(E))
102 E = DAE->getExpr();
103
Anders Carlssonb3f74422009-10-15 00:51:46 +0000104 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson0ece4912009-12-15 20:51:39 +0000105 ShouldDestroyTemporaries = true;
106
Chris Lattnereb99b012009-10-28 17:39:19 +0000107 // Keep track of the current cleanup stack depth.
Anders Carlsson0ece4912009-12-15 20:51:39 +0000108 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000109
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000110 E = TE->getSubExpr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000111 }
112
Eli Friedman5df0d422009-05-20 02:31:19 +0000113 RValue Val;
114 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson4bbab922009-05-20 00:36:58 +0000115 // Emit the expr as an lvalue.
116 LValue LV = EmitLValue(E);
Eli Friedman5df0d422009-05-20 02:31:19 +0000117 if (LV.isSimple())
118 return RValue::get(LV.getAddress());
119 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000120
121 if (ShouldDestroyTemporaries) {
122 // Pop temporaries.
123 while (LiveTemporaries.size() > OldNumLiveTemporaries)
124 PopCXXTemporary();
125 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000126 } else {
Anders Carlssonb3f74422009-10-15 00:51:46 +0000127 const CXXRecordDecl *BaseClassDecl = 0;
128 const CXXRecordDecl *DerivedClassDecl = 0;
129
130 if (const CastExpr *CE =
131 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
132 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
133 E = CE->getSubExpr();
134
135 BaseClassDecl =
136 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
137 DerivedClassDecl =
138 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
139 }
140 }
141
Anders Carlsson14c5cbf2009-08-16 07:36:22 +0000142 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
143 IsInitializer);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000144
Anders Carlssone1b7ea12009-10-18 23:09:21 +0000145 if (ShouldDestroyTemporaries) {
146 // Pop temporaries.
147 while (LiveTemporaries.size() > OldNumLiveTemporaries)
148 PopCXXTemporary();
149 }
150
Anders Carlsson2da76932009-08-16 17:54:29 +0000151 if (IsInitializer) {
152 // We might have to destroy the temporary variable.
153 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
154 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
155 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000156 const CXXDestructorDecl *Dtor =
Anders Carlsson2da76932009-08-16 17:54:29 +0000157 ClassDecl->getDestructor(getContext());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000158
Mike Stumpd88ea562009-12-09 03:35:49 +0000159 {
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000160 DelayedCleanupBlock Scope(*this);
Mike Stumpd88ea562009-12-09 03:35:49 +0000161 EmitCXXDestructorCall(Dtor, Dtor_Complete,
162 Val.getAggregateAddr());
Anders Carlsson6ec687d2009-12-11 01:00:09 +0000163
164 // Make sure to jump to the exit block.
165 EmitBranch(Scope.getCleanupExitBlock());
Mike Stumpd88ea562009-12-09 03:35:49 +0000166 }
167 if (Exceptions) {
168 EHCleanupBlock Cleanup(*this);
169 EmitCXXDestructorCall(Dtor, Dtor_Complete,
170 Val.getAggregateAddr());
171 }
Anders Carlsson2da76932009-08-16 17:54:29 +0000172 }
Anders Carlsson8478ce62009-08-16 17:50:25 +0000173 }
174 }
175 }
Anders Carlssonb3f74422009-10-15 00:51:46 +0000176
177 // Check if need to perform the derived-to-base cast.
178 if (BaseClassDecl) {
179 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlssonb3f74422009-10-15 00:51:46 +0000180 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +0000181 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
182 /*NullCheckValue=*/false);
Anders Carlssonb3f74422009-10-15 00:51:46 +0000183 return RValue::get(Base);
184 }
Anders Carlsson4bbab922009-05-20 00:36:58 +0000185 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000186
187 if (Val.isAggregate()) {
188 Val = RValue::get(Val.getAggregateAddr());
189 } else {
Anders Carlsson7cd3a642009-05-20 01:35:03 +0000190 // Create a temporary variable that we can bind the reference to.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000191 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlssone04d1c72009-05-20 01:03:17 +0000192 "reftmp");
Eli Friedman5df0d422009-05-20 02:31:19 +0000193 if (Val.isScalar())
194 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
195 else
196 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
197 Val = RValue::get(Temp);
Anders Carlssone04d1c72009-05-20 01:03:17 +0000198 }
Eli Friedman5df0d422009-05-20 02:31:19 +0000199
200 return Val;
Anders Carlsson4029ca72009-05-20 00:24:07 +0000201}
202
203
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000204/// getAccessedFieldNo - Given an encoded value and a result number, return the
205/// input field number being accessed.
206unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman4f8d1232008-05-22 00:50:06 +0000207 const llvm::Constant *Elts) {
208 if (isa<llvm::ConstantAggregateZero>(Elts))
209 return 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000210
Dan Gohman4f8d1232008-05-22 00:50:06 +0000211 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
212}
213
Mike Stumpb14e62d2009-12-16 02:57:00 +0000214void CodeGenFunction::EmitCheck(llvm::Value *Address, unsigned Size) {
215 if (!CatchUndefined)
216 return;
217
218 const llvm::IntegerType *Size_tTy
219 = llvm::IntegerType::get(VMContext, LLVMPointerWidth);
220 Address = Builder.CreateBitCast(Address, PtrToInt8Ty);
221
222 const llvm::Type *ResType[] = {
223 Size_tTy
224 };
225 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::objectsize, ResType, 1);
226 const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
227 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
228 // In time, people may want to control this and use a 1 here.
229 llvm::Value *Arg = llvm::ConstantInt::get(IntTy, 0);
230 llvm::Value *C = Builder.CreateCall2(F, Address, Arg);
231 llvm::BasicBlock *Cont = createBasicBlock();
232 llvm::BasicBlock *Check = createBasicBlock();
233 llvm::Value *NegativeOne = llvm::ConstantInt::get(Size_tTy, -1ULL);
234 Builder.CreateCondBr(Builder.CreateICmpEQ(C, NegativeOne), Cont, Check);
235
236 EmitBlock(Check);
237 Builder.CreateCondBr(Builder.CreateICmpUGE(C,
238 llvm::ConstantInt::get(Size_tTy, Size)),
239 Cont, getTrapBB());
240 EmitBlock(Cont);
241}
Chris Lattner9b655512007-08-31 22:49:20 +0000242
Chris Lattnerdd36d322010-01-09 21:40:03 +0000243
244llvm::Value *CodeGenFunction::
245EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
246 bool isInc, bool isPre) {
247 QualType ValTy = E->getSubExpr()->getType();
248 llvm::Value *InVal = EmitLoadOfLValue(LV, ValTy).getScalarVal();
249
250 int AmountVal = isInc ? 1 : -1;
251
252 if (ValTy->isPointerType() &&
253 ValTy->getAs<PointerType>()->isVariableArrayType()) {
254 // The amount of the addition/subtraction needs to account for the VLA size
255 ErrorUnsupported(E, "VLA pointer inc/dec");
256 }
257
258 llvm::Value *NextVal;
259 if (const llvm::PointerType *PT =
260 dyn_cast<llvm::PointerType>(InVal->getType())) {
261 llvm::Constant *Inc =
262 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), AmountVal);
263 if (!isa<llvm::FunctionType>(PT->getElementType())) {
264 QualType PTEE = ValTy->getPointeeType();
265 if (const ObjCInterfaceType *OIT =
266 dyn_cast<ObjCInterfaceType>(PTEE)) {
267 // Handle interface types, which are not represented with a concrete
268 // type.
269 int size = getContext().getTypeSize(OIT) / 8;
270 if (!isInc)
271 size = -size;
272 Inc = llvm::ConstantInt::get(Inc->getType(), size);
273 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
274 InVal = Builder.CreateBitCast(InVal, i8Ty);
275 NextVal = Builder.CreateGEP(InVal, Inc, "add.ptr");
276 llvm::Value *lhs = LV.getAddress();
277 lhs = Builder.CreateBitCast(lhs, llvm::PointerType::getUnqual(i8Ty));
278 LV = LValue::MakeAddr(lhs, MakeQualifiers(ValTy));
279 } else
280 NextVal = Builder.CreateInBoundsGEP(InVal, Inc, "ptrincdec");
281 } else {
282 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(VMContext);
283 NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
284 NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
285 NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
286 }
287 } else if (InVal->getType() == llvm::Type::getInt1Ty(VMContext) && isInc) {
288 // Bool++ is an interesting case, due to promotion rules, we get:
289 // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->
290 // Bool = ((int)Bool+1) != 0
291 // An interesting aspect of this is that increment is always true.
292 // Decrement does not have this property.
293 NextVal = llvm::ConstantInt::getTrue(VMContext);
294 } else if (isa<llvm::IntegerType>(InVal->getType())) {
295 NextVal = llvm::ConstantInt::get(InVal->getType(), AmountVal);
296
297 // Signed integer overflow is undefined behavior.
298 if (ValTy->isSignedIntegerType())
299 NextVal = Builder.CreateNSWAdd(InVal, NextVal, isInc ? "inc" : "dec");
300 else
301 NextVal = Builder.CreateAdd(InVal, NextVal, isInc ? "inc" : "dec");
302 } else {
303 // Add the inc/dec to the real part.
304 if (InVal->getType()->isFloatTy())
305 NextVal =
306 llvm::ConstantFP::get(VMContext,
307 llvm::APFloat(static_cast<float>(AmountVal)));
308 else if (InVal->getType()->isDoubleTy())
309 NextVal =
310 llvm::ConstantFP::get(VMContext,
311 llvm::APFloat(static_cast<double>(AmountVal)));
312 else {
313 llvm::APFloat F(static_cast<float>(AmountVal));
314 bool ignored;
315 F.convert(Target.getLongDoubleFormat(), llvm::APFloat::rmTowardZero,
316 &ignored);
317 NextVal = llvm::ConstantFP::get(VMContext, F);
318 }
319 NextVal = Builder.CreateFAdd(InVal, NextVal, isInc ? "inc" : "dec");
320 }
321
322 // Store the updated result through the lvalue.
323 if (LV.isBitfield())
324 EmitStoreThroughBitfieldLValue(RValue::get(NextVal), LV, ValTy, &NextVal);
325 else
326 EmitStoreThroughLValue(RValue::get(NextVal), LV, ValTy);
327
328 // If this is a postinc, return the value read from memory, otherwise use the
329 // updated value.
330 return isPre ? NextVal : InVal;
331}
332
333
334CodeGenFunction::ComplexPairTy CodeGenFunction::
335EmitComplexPrePostIncDec(const UnaryOperator *E, LValue LV,
336 bool isInc, bool isPre) {
337 ComplexPairTy InVal = LoadComplexFromAddr(LV.getAddress(),
338 LV.isVolatileQualified());
339
340 llvm::Value *NextVal;
341 if (isa<llvm::IntegerType>(InVal.first->getType())) {
342 uint64_t AmountVal = isInc ? 1 : -1;
343 NextVal = llvm::ConstantInt::get(InVal.first->getType(), AmountVal, true);
344
345 // Add the inc/dec to the real part.
346 NextVal = Builder.CreateAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
347 } else {
348 QualType ElemTy = E->getType()->getAs<ComplexType>()->getElementType();
349 llvm::APFloat FVal(getContext().getFloatTypeSemantics(ElemTy), 1);
350 if (!isInc)
351 FVal.changeSign();
352 NextVal = llvm::ConstantFP::get(getLLVMContext(), FVal);
353
354 // Add the inc/dec to the real part.
355 NextVal = Builder.CreateFAdd(InVal.first, NextVal, isInc ? "inc" : "dec");
356 }
357
358 ComplexPairTy IncVal(NextVal, InVal.second);
359
360 // Store the updated result through the lvalue.
361 StoreComplexToAddr(IncVal, LV.getAddress(), LV.isVolatileQualified());
362
363 // If this is a postinc, return the value read from memory, otherwise use the
364 // updated value.
365 return isPre ? IncVal : InVal;
366}
367
368
Reid Spencer5f016e22007-07-11 17:01:13 +0000369//===----------------------------------------------------------------------===//
370// LValue Expression Emission
371//===----------------------------------------------------------------------===//
372
Daniel Dunbar13e81732009-02-05 07:09:07 +0000373RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnereb99b012009-10-28 17:39:19 +0000374 if (Ty->isVoidType())
Daniel Dunbar13e81732009-02-05 07:09:07 +0000375 return RValue::get(0);
Chris Lattnereb99b012009-10-28 17:39:19 +0000376
377 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000378 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson03e20502009-07-30 23:11:26 +0000379 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000380 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnereb99b012009-10-28 17:39:19 +0000381 }
382
383 if (hasAggregateLLVMType(Ty)) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000384 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson03e20502009-07-30 23:11:26 +0000385 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000386 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000387
388 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000389}
390
Daniel Dunbar13e81732009-02-05 07:09:07 +0000391RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
392 const char *Name) {
393 ErrorUnsupported(E, Name);
394 return GetUndefRValue(E->getType());
395}
396
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000397LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
398 const char *Name) {
399 ErrorUnsupported(E, Name);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000400 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson03e20502009-07-30 23:11:26 +0000401 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall0953e762009-09-24 19:53:00 +0000402 MakeQualifiers(E->getType()));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000403}
404
Mike Stumpb14e62d2009-12-16 02:57:00 +0000405LValue CodeGenFunction::EmitCheckedLValue(const Expr *E) {
406 LValue LV = EmitLValue(E);
407 if (!isa<DeclRefExpr>(E) && !LV.isBitfield() && LV.isSimple())
408 EmitCheck(LV.getAddress(), getContext().getTypeSize(E->getType()) / 8);
409 return LV;
410}
411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412/// EmitLValue - Emit code to compute a designator that specifies the location
413/// of the expression.
414///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000415/// This can return one of two things: a simple address or a bitfield reference.
416/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
417/// an LLVM pointer type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000418///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000419/// If this returns a bitfield reference, nothing about the pointee type of the
420/// LLVM value is known: For example, it may not be a pointer to an integer.
Reid Spencer5f016e22007-07-11 17:01:13 +0000421///
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000422/// If this returns a normal address, and if the lvalue's C type is fixed size,
423/// this method guarantees that the returned pointer type will point to an LLVM
424/// type of the same size of the lvalue's type. If the lvalue has a variable
425/// length type, this is not possible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000426///
427LValue CodeGenFunction::EmitLValue(const Expr *E) {
428 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000429 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000430
Fariborz Jahanian820bca42009-12-09 23:35:29 +0000431 case Expr::ObjCIsaExprClass:
432 return EmitObjCIsaExpr(cast<ObjCIsaExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000433 case Expr::BinaryOperatorClass:
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000434 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000435 case Expr::CallExprClass:
Anders Carlssonfaf86642009-09-01 21:18:52 +0000436 case Expr::CXXMemberCallExprClass:
Douglas Gregorb4609802008-11-14 16:09:21 +0000437 case Expr::CXXOperatorCallExprClass:
438 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +0000439 case Expr::VAArgExprClass:
440 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000441 case Expr::DeclRefExprClass:
Douglas Gregor1a49af92009-01-06 05:10:23 +0000442 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000444 case Expr::PredefinedExprClass:
445 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 case Expr::StringLiteralClass:
447 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnereaf2bb82009-02-24 22:18:39 +0000448 case Expr::ObjCEncodeExprClass:
449 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000450
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000451 case Expr::BlockDeclRefExprClass:
Mike Stumpa99038c2009-02-28 09:07:16 +0000452 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
453
Anders Carlssonb58d0172009-05-30 23:23:33 +0000454 case Expr::CXXTemporaryObjectExprClass:
455 case Expr::CXXConstructExprClass:
Anders Carlssone61c9e82009-05-30 23:30:54 +0000456 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
457 case Expr::CXXBindTemporaryExprClass:
458 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlssoneb60edf2010-01-29 02:39:32 +0000459 case Expr::CXXBindReferenceExprClass:
460 return EmitLValue(cast<CXXBindReferenceExpr>(E)->getSubExpr());
Anders Carlssonb9ea0b52009-09-14 01:10:45 +0000461 case Expr::CXXExprWithTemporariesClass:
462 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson370e5382009-11-14 01:51:50 +0000463 case Expr::CXXZeroInitValueExprClass:
464 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
465 case Expr::CXXDefaultArgExprClass:
466 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc2e84ae2009-11-15 08:09:41 +0000467 case Expr::CXXTypeidExprClass:
468 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssone61c9e82009-05-30 23:30:54 +0000469
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000470 case Expr::ObjCMessageExprClass:
471 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000472 case Expr::ObjCIvarRefExprClass:
Chris Lattner391d77a2008-03-30 23:03:07 +0000473 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000474 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000475 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian09105f52009-08-20 17:02:02 +0000476 case Expr::ObjCImplicitSetterGetterRefExprClass:
477 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000478 case Expr::ObjCSuperExprClass:
Chris Lattner65459942009-04-25 19:35:26 +0000479 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000480
Chris Lattner65459942009-04-25 19:35:26 +0000481 case Expr::StmtExprClass:
482 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000483 case Expr::UnaryOperatorClass:
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
485 case Expr::ArraySubscriptExprClass:
486 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000487 case Expr::ExtVectorElementExprClass:
488 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000489 case Expr::MemberExprClass:
Douglas Gregorbd4c4ae2009-08-26 22:36:53 +0000490 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000491 case Expr::CompoundLiteralExprClass:
492 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbar90345582009-03-24 02:38:23 +0000493 case Expr::ConditionalOperatorClass:
Anders Carlsson6fcec8b2009-09-15 16:35:24 +0000494 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000495 case Expr::ChooseExprClass:
Eli Friedman79769322009-03-04 05:52:32 +0000496 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattnerc3953a62009-03-18 04:02:57 +0000497 case Expr::ImplicitCastExprClass:
498 case Expr::CStyleCastExprClass:
499 case Expr::CXXFunctionalCastExprClass:
500 case Expr::CXXStaticCastExprClass:
501 case Expr::CXXDynamicCastExprClass:
502 case Expr::CXXReinterpretCastExprClass:
503 case Expr::CXXConstCastExprClass:
Chris Lattner75dfeda2009-03-18 18:28:57 +0000504 return EmitCastLValue(cast<CastExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 }
506}
507
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000508llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
509 QualType Ty) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000510 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
511 if (Volatile)
512 Load->setVolatile(true);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000513
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000514 // Bool can have different representation in memory than in registers.
Daniel Dunbar2da84ff2009-11-29 21:23:36 +0000515 llvm::Value *V = Load;
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000516 if (Ty->isBooleanType())
Owen Anderson0032b272009-08-13 21:57:51 +0000517 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
518 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000519
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000520 return V;
521}
522
523void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000524 bool Volatile, QualType Ty) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000525
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000526 if (Ty->isBooleanType()) {
527 // Bool can have different representation in memory than in registers.
Anders Carlsson3bb423b2009-05-19 19:36:19 +0000528 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedman2b06d342009-12-01 22:31:51 +0000529 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000530 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000531 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000532}
533
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000534/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
535/// method emits the address of the lvalue, then loads the result as an rvalue,
536/// returning the rvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +0000537RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000538 if (LV.isObjCWeak()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000539 // load of a __weak object.
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000540 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnereb99b012009-10-28 17:39:19 +0000541 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
542 AddrWeakObj));
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000543 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000544
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 if (LV.isSimple()) {
546 llvm::Value *Ptr = LV.getAddress();
547 const llvm::Type *EltTy =
548 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 // Simple scalar l-value.
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000551 if (EltTy->isSingleValueType())
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000552 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar9d9cc872009-02-10 00:57:50 +0000553 ExprType));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000554
Chris Lattner883f6a72007-08-11 00:04:45 +0000555 assert(ExprType->isFunctionType() && "Unknown scalar value");
556 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000560 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
561 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
563 "vecext"));
564 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000565
566 // If this is a reference to a subset of the elements of a vector, either
567 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000568 if (LV.isExtVectorElt())
569 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000570
571 if (LV.isBitfield())
572 return EmitLoadOfBitfieldLValue(LV, ExprType);
573
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000574 if (LV.isPropertyRef())
575 return EmitLoadOfPropertyRefLValue(LV, ExprType);
576
Chris Lattner73525de2009-02-16 21:11:58 +0000577 assert(LV.isKVCRef() && "Unknown LValue type!");
578 return EmitLoadOfKVCRefLValue(LV, ExprType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000579}
580
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000581RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
582 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000583 unsigned StartBit = LV.getBitfieldStartBit();
584 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000585 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000586
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000587 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000588 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000589 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000590
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000591 // In some cases the bitfield may straddle two memory locations. Currently we
592 // load the entire bitfield, then do the magic to sign-extend it if
593 // necessary. This results in somewhat more code than necessary for the common
594 // case (one load), since two shifts accomplish both the masking and sign
595 // extension.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000596 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
597 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000598
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000599 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000600 if (StartBit)
Chris Lattner86b85b22009-12-06 17:22:42 +0000601 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000602
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000603 // Mask off unused bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000604 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000605 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000606 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000607
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000608 // Fetch the high bits if necessary.
609 if (LowBits < BitfieldSize) {
610 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000611 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000612 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
613 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000614 LV.isVolatileQualified(),
615 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000616
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000617 // Mask off unused bits.
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000618 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
619 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000620 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000621
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000622 // Shift to proper location and or in to bitfield value.
Chris Lattner86b85b22009-12-06 17:22:42 +0000623 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000624 Val = Builder.CreateOr(Val, HighVal, "bf.val");
625 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000626
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000627 // Sign extend if necessary.
628 if (LV.isBitfieldSigned()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000629 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000630 EltTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000631 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000632 ExtraBits, "bf.val.sext");
633 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000634
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000635 // The bitfield type and the normal type differ when the storage sizes differ
636 // (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000637 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000638
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000639 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000640}
641
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000642RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
643 QualType ExprType) {
644 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
645}
646
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000647RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
648 QualType ExprType) {
649 return EmitObjCPropertyGet(LV.getKVCRefExpr());
650}
651
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000652// If this is a reference to a subset of the elements of a vector, create an
653// appropriate shufflevector.
Nate Begeman213541a2008-04-18 23:10:10 +0000654RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
655 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000656 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
657 LV.isVolatileQualified(), "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000658
Nate Begeman8a997642008-05-09 06:41:27 +0000659 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000660
661 // If the result of the expression is a non-vector type, we must be extracting
662 // a single element. Just codegen as an extractelement.
John McCall183700f2009-09-21 23:43:11 +0000663 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattnercf60cd22007-08-10 17:10:08 +0000664 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000665 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000666 llvm::Value *Elt = llvm::ConstantInt::get(
667 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner34cdc862007-08-03 16:18:34 +0000668 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
669 }
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000670
671 // Always use shuffle vector to try to retain the original program structure
Chris Lattnercf60cd22007-08-10 17:10:08 +0000672 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000673
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000674 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner34cdc862007-08-03 16:18:34 +0000675 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000676 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000677 Mask.push_back(llvm::ConstantInt::get(
678 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner34cdc862007-08-03 16:18:34 +0000679 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000680
Owen Anderson4a289322009-07-28 21:22:35 +0000681 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000682 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson03e20502009-07-30 23:11:26 +0000683 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000684 MaskV, "tmp");
685 return RValue::get(Vec);
Chris Lattner34cdc862007-08-03 16:18:34 +0000686}
687
688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689
690/// EmitStoreThroughLValue - Store the specified rvalue into the specified
691/// lvalue, where both are guaranteed to the have the same type, and that type
692/// is 'Ty'.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000693void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Reid Spencer5f016e22007-07-11 17:01:13 +0000694 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000695 if (!Dst.isSimple()) {
696 if (Dst.isVectorElt()) {
697 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000698 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
699 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000700 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000701 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000702 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000703 return;
704 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000705
Nate Begeman213541a2008-04-18 23:10:10 +0000706 // If this is an update of extended vector elements, insert them as
707 // appropriate.
708 if (Dst.isExtVectorElt())
709 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000710
711 if (Dst.isBitfield())
712 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
713
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000714 if (Dst.isPropertyRef())
715 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
716
Chris Lattnereb99b012009-10-28 17:39:19 +0000717 assert(Dst.isKVCRef() && "Unknown LValue type");
718 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000719 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000720
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000721 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000722 // load of a __weak object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000723 llvm::Value *LvalueDst = Dst.getAddress();
724 llvm::Value *src = Src.getScalarVal();
Mike Stumpf33651c2009-04-14 00:57:29 +0000725 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000726 return;
727 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000728
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +0000729 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000730 // load of a __strong object.
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000731 llvm::Value *LvalueDst = Dst.getAddress();
732 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000733 if (Dst.isObjCIvar()) {
734 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
735 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
736 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000737 llvm::Value *dst = RHS;
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000738 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
739 llvm::Value *LHS =
740 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
741 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian76368e82009-09-25 00:00:20 +0000742 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000743 BytesBetween);
Chris Lattnereb99b012009-10-28 17:39:19 +0000744 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanianbf63b872009-05-04 23:27:20 +0000745 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
746 else
747 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000748 return;
749 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000750
Chris Lattner883f6a72007-08-11 00:04:45 +0000751 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlssonb4aa4662009-05-19 18:50:41 +0000752 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
753 Dst.isVolatileQualified(), Ty);
Reid Spencer5f016e22007-07-11 17:01:13 +0000754}
755
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000756void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000757 QualType Ty,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000758 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000759 unsigned StartBit = Dst.getBitfieldStartBit();
760 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000761 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000762
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000763 const llvm::Type *EltTy =
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000764 cast<llvm::PointerType>(Ptr->getType())->getElementType();
765 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
766
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000767 // Get the new value, cast to the appropriate type and masked to exactly the
768 // size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000769 llvm::Value *SrcVal = Src.getScalarVal();
770 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000771 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
772 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000773 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000774
Daniel Dunbared3849b2008-11-19 09:36:46 +0000775 // Return the new value of the bit-field, if requested.
776 if (Result) {
777 // Cast back to the proper type for result.
778 const llvm::Type *SrcTy = SrcVal->getType();
779 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
780 "bf.reload.val");
781
782 // Sign extend if necessary.
783 if (Dst.isBitfieldSigned()) {
784 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000785 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000786 SrcTySize - BitfieldSize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000787 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbared3849b2008-11-19 09:36:46 +0000788 ExtraBits, "bf.reload.sext");
789 }
790
791 *Result = SrcTrunc;
792 }
793
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000794 // In some cases the bitfield may straddle two memory locations. Emit the low
795 // part first and check to see if the high needs to be done.
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000796 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
797 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
798 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000799
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000800 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000801 llvm::Constant *InvMask =
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000802 llvm::ConstantInt::get(VMContext,
803 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000804
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000805 // Compute the new low part as
806 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
807 // with the shift of NewVal implicitly stripping the high bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000808 llvm::Value *NewLowVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000809 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000810 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
811 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000812
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000813 // Write back.
814 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000815
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000816 // If the low part doesn't cover the bitfield emit a high part.
817 if (LowBits < BitfieldSize) {
818 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson0032b272009-08-13 21:57:51 +0000819 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000820 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
821 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000822 Dst.isVolatileQualified(),
823 "bf.prev.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000824
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000825 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000826 llvm::Constant *InvMask =
827 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000828 HighBits));
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000829
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000830 // Compute the new high part as
831 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
832 // where the high bits of NewVal have already been cleared and the
833 // shift stripping the low bits.
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000834 llvm::Value *NewHighVal =
Chris Lattner86b85b22009-12-06 17:22:42 +0000835 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000836 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
837 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000838
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000839 // Write back.
840 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
841 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000842}
843
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000844void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
845 LValue Dst,
846 QualType Ty) {
847 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
848}
849
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000850void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
851 LValue Dst,
852 QualType Ty) {
853 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
854}
855
Nate Begeman213541a2008-04-18 23:10:10 +0000856void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
857 LValue Dst,
858 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000859 // This access turns into a read/modify/write of the vector. Load the input
860 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000861 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
862 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000863 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000864
Chris Lattner9b655512007-08-31 22:49:20 +0000865 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000866
John McCall183700f2009-09-21 23:43:11 +0000867 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000868 unsigned NumSrcElts = VTy->getNumElements();
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000869 unsigned NumDstElts =
870 cast<llvm::VectorType>(Vec->getType())->getNumElements();
871 if (NumDstElts == NumSrcElts) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000872 // Use shuffle vector is the src and destination are the same number of
873 // elements and restore the vector mask since it is on the side it will be
874 // stored.
Nate Begeman6e5dd862009-06-26 21:12:50 +0000875 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000876 for (unsigned i = 0; i != NumSrcElts; ++i) {
877 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson0032b272009-08-13 21:57:51 +0000878 Mask[InIdx] = llvm::ConstantInt::get(
879 llvm::Type::getInt32Ty(VMContext), i);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000880 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000881
Owen Anderson4a289322009-07-28 21:22:35 +0000882 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000883 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000884 llvm::UndefValue::get(Vec->getType()),
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000885 MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000886 } else if (NumDstElts > NumSrcElts) {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000887 // Extended the source vector to the same length and then shuffle it
888 // into the destination.
889 // FIXME: since we're shuffling with undef, can we just use the indices
890 // into that? This could be simpler.
891 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000892 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000893 unsigned i;
894 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000895 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000896 for (; i != NumDstElts; ++i)
Chris Lattnereb99b012009-10-28 17:39:19 +0000897 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson4a289322009-07-28 21:22:35 +0000898 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000899 ExtMask.size());
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000900 llvm::Value *ExtSrcVal =
Daniel Dunbarbb767732009-02-17 18:31:04 +0000901 Builder.CreateShuffleVector(SrcVal,
Owen Anderson03e20502009-07-30 23:11:26 +0000902 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbarbb767732009-02-17 18:31:04 +0000903 ExtMaskV, "tmp");
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000904 // build identity
905 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnereb99b012009-10-28 17:39:19 +0000906 for (unsigned i = 0; i != NumDstElts; ++i)
907 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
908
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000909 // modify when what gets shuffled in
910 for (unsigned i = 0; i != NumSrcElts; ++i) {
911 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000912 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000913 }
Owen Anderson4a289322009-07-28 21:22:35 +0000914 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000915 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stumpb3589f42009-07-30 22:28:39 +0000916 } else {
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000917 // We should never shorten the vector
918 assert(0 && "unexpected shorten vector length");
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000919 }
920 } else {
921 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000922 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnereb99b012009-10-28 17:39:19 +0000923 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
924 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000925 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000926 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +0000927
Eli Friedman1e692ac2008-06-13 23:01:12 +0000928 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000929}
930
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000931// setObjCGCLValueClass - sets class of he lvalue for the purpose of
932// generating write-barries API. It is currently a global, ivar,
933// or neither.
Chris Lattnereb99b012009-10-28 17:39:19 +0000934static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
935 LValue &LV) {
Fariborz Jahanianb9242592009-09-21 23:03:37 +0000936 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000937 return;
938
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000939 if (isa<ObjCIvarRefExpr>(E)) {
940 LV.SetObjCIvar(LV, true);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000941 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
942 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000943 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +0000944 return;
945 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000946
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000947 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
948 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
949 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
950 VD->isFileVarDecl())
951 LV.SetGlobalObjCRef(LV, true);
952 }
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000953 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +0000954 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000955 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000956
957 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000958 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000959 return;
960 }
961
962 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000963 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000964 if (LV.isObjCIvar()) {
965 // If cast is to a structure pointer, follow gcc's behavior and make it
966 // a non-ivar write-barrier.
967 QualType ExpTy = E->getType();
968 if (ExpTy->isPointerType())
969 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
970 if (ExpTy->isRecordType())
971 LV.SetObjCIvar(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000972 }
973 return;
Fariborz Jahanian75b08f12009-09-30 17:10:29 +0000974 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000975 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000976 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000977 return;
978 }
979
980 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000981 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnereb99b012009-10-28 17:39:19 +0000982 return;
983 }
984
985 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000986 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000987 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000988 // Using array syntax to assigning to what an ivar points to is not
989 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
990 LV.SetObjCIvar(LV, false);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +0000991 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
992 // Using array syntax to assigning to what global points to is not
993 // same as assigning to the global itself. {id *G;} G[i] = 0;
994 LV.SetGlobalObjCRef(LV, false);
Chris Lattnereb99b012009-10-28 17:39:19 +0000995 return;
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +0000996 }
Chris Lattnereb99b012009-10-28 17:39:19 +0000997
998 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahanianb123ea32009-09-16 21:37:16 +0000999 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian1c1afc42009-09-18 00:04:00 +00001000 // We don't know if member is an 'ivar', but this flag is looked at
1001 // only in the context of LV.isObjCIvar().
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001002 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnereb99b012009-10-28 17:39:19 +00001003 return;
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001004 }
1005}
1006
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001007static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
1008 const Expr *E, const VarDecl *VD) {
Daniel Dunbard2113f22009-11-08 09:46:46 +00001009 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001010 "Var decl must have external storage or be a file var decl!");
1011
1012 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
1013 if (VD->getType()->isReferenceType())
1014 V = CGF.Builder.CreateLoad(V, "tmp");
1015 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1016 setObjCGCLValueClass(CGF.getContext(), E, LV);
1017 return LV;
1018}
1019
Eli Friedman9a146302009-11-26 06:08:14 +00001020static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
1021 const Expr *E, const FunctionDecl *FD) {
1022 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
1023 if (!FD->hasPrototype()) {
1024 if (const FunctionProtoType *Proto =
1025 FD->getType()->getAs<FunctionProtoType>()) {
1026 // Ugly case: for a K&R-style definition, the type of the definition
1027 // isn't the same as the type of a use. Correct for this with a
1028 // bitcast.
1029 QualType NoProtoType =
1030 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
1031 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
1032 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
1033 }
1034 }
1035 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
1036}
1037
Reid Spencer5f016e22007-07-11 17:01:13 +00001038LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001039 const NamedDecl *ND = E->getDecl();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001040
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001041 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001042
1043 // Check if this is a global variable.
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001044 if (VD->hasExternalStorage() || VD->isFileVarDecl())
1045 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001046
1047 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
1048
1049 llvm::Value *V = LocalDeclMap[VD];
1050 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
1051
1052 Qualifiers Quals = MakeQualifiers(E->getType());
1053 // local variables do not get their gc attribute set.
1054 // local static?
1055 if (NonGCable) Quals.removeObjCGCAttr();
1056
1057 if (VD->hasAttr<BlocksAttr>()) {
1058 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00001059 V = Builder.CreateLoad(V);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001060 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
1061 VD->getNameAsString());
1062 }
1063 if (VD->getType()->isReferenceType())
1064 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001065 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson0bc70492009-11-07 22:46:42 +00001066 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001067 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +00001068 return LV;
Chris Lattnereb99b012009-10-28 17:39:19 +00001069 }
1070
Eli Friedman9a146302009-11-26 06:08:14 +00001071 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1072 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnereb99b012009-10-28 17:39:19 +00001073
Chris Lattnereb99b012009-10-28 17:39:19 +00001074 if (E->getQualifier()) {
Douglas Gregora2813ce2009-10-23 18:54:35 +00001075 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001076 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner41110242008-06-17 18:05:57 +00001077 }
Chris Lattnereb99b012009-10-28 17:39:19 +00001078
Anders Carlsson1e74c4f2009-11-07 22:53:10 +00001079 assert(false && "Unhandled DeclRefExpr");
1080
1081 // an invalid LValue, but the assert will
1082 // ensure that this point is never reached.
Chris Lattnerb1776cb2007-09-16 19:23:47 +00001083 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +00001084}
1085
Mike Stumpa99038c2009-02-28 09:07:16 +00001086LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001087 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stumpa99038c2009-02-28 09:07:16 +00001088}
1089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
1091 // __extension__ doesn't affect lvalue-ness.
1092 if (E->getOpcode() == UnaryOperator::Extension)
1093 return EmitLValue(E->getSubExpr());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001094
Chris Lattner96196622008-07-26 22:37:01 +00001095 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +00001096 switch (E->getOpcode()) {
1097 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnereb99b012009-10-28 17:39:19 +00001098 case UnaryOperator::Deref: {
1099 QualType T = E->getSubExpr()->getType()->getPointeeType();
1100 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001101
Chris Lattnereb99b012009-10-28 17:39:19 +00001102 Qualifiers Quals = MakeQualifiers(T);
1103 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall0953e762009-09-24 19:53:00 +00001104
Chris Lattnereb99b012009-10-28 17:39:19 +00001105 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
1106 // We should not generate __weak write barrier on indirect reference
1107 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
1108 // But, we continue to generate __strong write barrier on indirect write
1109 // into a pointer to object.
1110 if (getContext().getLangOptions().ObjC1 &&
1111 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
1112 LV.isObjCWeak())
1113 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
1114 return LV;
1115 }
Chris Lattner7da36f62007-10-30 22:53:42 +00001116 case UnaryOperator::Real:
Eli Friedmane401cd52009-11-09 04:20:47 +00001117 case UnaryOperator::Imag: {
Chris Lattner7da36f62007-10-30 22:53:42 +00001118 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +00001119 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
1120 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +00001121 Idx, "idx"),
John McCall0953e762009-09-24 19:53:00 +00001122 MakeQualifiers(ExprTy));
Chris Lattner7da36f62007-10-30 22:53:42 +00001123 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001124 case UnaryOperator::PreInc:
Chris Lattner197a3382010-01-09 21:44:40 +00001125 case UnaryOperator::PreDec: {
1126 LValue LV = EmitLValue(E->getSubExpr());
1127 bool isInc = E->getOpcode() == UnaryOperator::PreInc;
1128
1129 if (E->getType()->isAnyComplexType())
1130 EmitComplexPrePostIncDec(E, LV, isInc, true/*isPre*/);
1131 else
1132 EmitScalarPrePostIncDec(E, LV, isInc, true/*isPre*/);
1133 return LV;
1134 }
Eli Friedmane401cd52009-11-09 04:20:47 +00001135 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001136}
1137
1138LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall0953e762009-09-24 19:53:00 +00001139 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
1140 Qualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001141}
1142
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001143LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall0953e762009-09-24 19:53:00 +00001144 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
1145 Qualifiers());
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001146}
1147
1148
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001149LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +00001150 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001151
1152 switch (Type) {
Chris Lattnereb99b012009-10-28 17:39:19 +00001153 default: assert(0 && "Invalid type");
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001154 case PredefinedExpr::Func:
1155 GlobalVarName = "__func__.";
1156 break;
1157 case PredefinedExpr::Function:
1158 GlobalVarName = "__FUNCTION__.";
1159 break;
1160 case PredefinedExpr::PrettyFunction:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00001161 GlobalVarName = "__PRETTY_FUNCTION__.";
1162 break;
Anders Carlsson22742662007-07-21 05:21:51 +00001163 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001164
Daniel Dunbar0a23d762009-09-12 23:06:21 +00001165 llvm::StringRef FnName = CurFn->getName();
1166 if (FnName.startswith("\01"))
1167 FnName = FnName.substr(1);
1168 GlobalVarName += FnName;
1169
Anders Carlsson3a082d82009-09-08 18:24:21 +00001170 std::string FunctionName =
Mike Stump1eb44332009-09-09 15:08:12 +00001171 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson3a082d82009-09-08 18:24:21 +00001172 CurCodeDecl);
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001173
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001174 llvm::Constant *C =
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001175 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall0953e762009-09-24 19:53:00 +00001176 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001177}
1178
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001179LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbar662b71e2008-10-17 21:58:32 +00001180 switch (E->getIdentType()) {
1181 default:
1182 return EmitUnsupportedLValue(E, "predefined expression");
1183 case PredefinedExpr::Func:
1184 case PredefinedExpr::Function:
1185 case PredefinedExpr::PrettyFunction:
1186 return EmitPredefinedFunctionName(E->getIdentType());
1187 }
Anders Carlsson22742662007-07-21 05:21:51 +00001188}
1189
Mike Stumpd8af3602009-12-15 01:22:35 +00001190llvm::BasicBlock *CodeGenFunction::getTrapBB() {
Mike Stump41513442009-12-15 00:59:40 +00001191 const CodeGenOptions &GCO = CGM.getCodeGenOpts();
1192
1193 // If we are not optimzing, don't collapse all calls to trap in the function
1194 // to the same call, that way, in the debugger they can see which operation
1195 // did in fact fail. If we are optimizing, we collpase all call to trap down
1196 // to just one per function to save on codesize.
1197 if (GCO.OptimizationLevel
1198 && TrapBB)
Mike Stump15037ca2009-12-15 00:35:12 +00001199 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001200
1201 llvm::BasicBlock *Cont = 0;
1202 if (HaveInsertPoint()) {
1203 Cont = createBasicBlock("cont");
1204 EmitBranch(Cont);
1205 }
Mike Stump15037ca2009-12-15 00:35:12 +00001206 TrapBB = createBasicBlock("trap");
1207 EmitBlock(TrapBB);
1208
1209 llvm::Value *F = CGM.getIntrinsic(llvm::Intrinsic::trap, 0, 0);
1210 llvm::CallInst *TrapCall = Builder.CreateCall(F);
1211 TrapCall->setDoesNotReturn();
1212 TrapCall->setDoesNotThrow();
Mike Stump9c276ae2009-12-12 01:27:46 +00001213 Builder.CreateUnreachable();
1214
1215 if (Cont)
1216 EmitBlock(Cont);
Mike Stump15037ca2009-12-15 00:35:12 +00001217 return TrapBB;
Mike Stump9c276ae2009-12-12 01:27:46 +00001218}
1219
Reid Spencer5f016e22007-07-11 17:01:13 +00001220LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +00001221 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001222 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman61d004a2009-06-06 19:09:26 +00001223 QualType IdxTy = E->getIdx()->getType();
1224 bool IdxSigned = IdxTy->isSignedIntegerType();
1225
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // If the base is a vector type, then we are forming a vector element lvalue
1227 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001228 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +00001230 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +00001231 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001232 Idx = Builder.CreateIntCast(Idx,
Owen Anderson0032b272009-08-13 21:57:51 +00001233 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman1e692ac2008-06-13 23:01:12 +00001234 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall0953e762009-09-24 19:53:00 +00001235 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001237
Ted Kremenek23245122007-08-20 16:18:38 +00001238 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +00001239 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001240
Ted Kremenek23245122007-08-20 16:18:38 +00001241 // Extend or truncate the index type to 32 or 64-bits.
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta7cabee52009-04-24 02:40:57 +00001243 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson0032b272009-08-13 21:57:51 +00001244 Idx = Builder.CreateIntCast(Idx,
1245 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 IdxSigned, "idxprom");
1247
Mike Stumpb14e62d2009-12-16 02:57:00 +00001248 // FIXME: As llvm implements the object size checking, this can come out.
Mike Stump9c276ae2009-12-12 01:27:46 +00001249 if (CatchUndefined) {
Mike Stumpb14e62d2009-12-16 02:57:00 +00001250 if (const ImplicitCastExpr *ICE=dyn_cast<ImplicitCastExpr>(E->getBase())) {
Mike Stump9c276ae2009-12-12 01:27:46 +00001251 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr())) {
1252 if (ICE->getCastKind() == CastExpr::CK_ArrayToPointerDecay) {
1253 if (const ConstantArrayType *CAT
1254 = getContext().getAsConstantArrayType(DRE->getType())) {
1255 llvm::APInt Size = CAT->getSize();
1256 llvm::BasicBlock *Cont = createBasicBlock("cont");
Mike Stump750c85e2009-12-14 22:14:31 +00001257 Builder.CreateCondBr(Builder.CreateICmpULE(Idx,
Mike Stump9c276ae2009-12-12 01:27:46 +00001258 llvm::ConstantInt::get(Idx->getType(), Size)),
Mike Stump15037ca2009-12-15 00:35:12 +00001259 Cont, getTrapBB());
Mike Stump96a063a2009-12-14 20:52:00 +00001260 EmitBlock(Cont);
Mike Stump9c276ae2009-12-12 01:27:46 +00001261 }
1262 }
1263 }
1264 }
1265 }
1266
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001267 // We know that the pointer points to a type of the correct size, unless the
1268 // size is a VLA or Objective-C interface.
Daniel Dunbar2a866252009-04-25 05:08:32 +00001269 llvm::Value *Address = 0;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001270 if (const VariableArrayType *VAT =
Anders Carlsson8b33c082008-12-21 00:11:23 +00001271 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner881eb9c2009-08-14 23:43:22 +00001272 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001273
Anders Carlsson8b33c082008-12-21 00:11:23 +00001274 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001275
Anders Carlsson6183a992008-12-21 03:44:36 +00001276 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001277
Ken Dyck199c3d62010-01-11 17:06:35 +00001278 CharUnits BaseTypeSize = getContext().getTypeSizeInChars(BaseType);
Anders Carlsson8b33c082008-12-21 00:11:23 +00001279 Idx = Builder.CreateUDiv(Idx,
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001280 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001281 BaseTypeSize.getQuantity()));
Dan Gohman664f8932009-08-12 00:33:55 +00001282 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001283 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbar2a866252009-04-25 05:08:32 +00001284 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001285 llvm::Value *InterfaceSize =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001286 llvm::ConstantInt::get(Idx->getType(),
Ken Dyck199c3d62010-01-11 17:06:35 +00001287 getContext().getTypeSizeInChars(OIT).getQuantity());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001288
Daniel Dunbar2a866252009-04-25 05:08:32 +00001289 Idx = Builder.CreateMul(Idx, InterfaceSize);
1290
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00001291 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman664f8932009-08-12 00:33:55 +00001292 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbar2a866252009-04-25 05:08:32 +00001293 Idx, "arrayidx");
1294 Address = Builder.CreateBitCast(Address, Base->getType());
1295 } else {
Dan Gohman664f8932009-08-12 00:33:55 +00001296 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson8b33c082008-12-21 00:11:23 +00001297 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001298
Steve Naroff14108da2009-07-10 23:34:53 +00001299 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001300 assert(!T.isNull() &&
Steve Naroff14108da2009-07-10 23:34:53 +00001301 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001302
John McCall0953e762009-09-24 19:53:00 +00001303 Qualifiers Quals = MakeQualifiers(T);
1304 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1305
1306 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001307 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001308 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001309 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahanianb123ea32009-09-16 21:37:16 +00001310 setObjCGCLValueClass(getContext(), E, LV);
1311 }
Fariborz Jahanian643887a2009-02-21 23:37:19 +00001312 return LV;
Reid Spencer5f016e22007-07-11 17:01:13 +00001313}
1314
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001315static
Owen Andersona1cf15f2009-07-14 23:10:40 +00001316llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1317 llvm::SmallVector<unsigned, 4> &Elts) {
Chris Lattner998eab12009-12-23 21:31:11 +00001318 llvm::SmallVector<llvm::Constant*, 4> CElts;
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001319
Nate Begeman3b8d1162008-05-13 21:03:02 +00001320 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson0032b272009-08-13 21:57:51 +00001321 CElts.push_back(llvm::ConstantInt::get(
1322 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001323
Owen Anderson4a289322009-07-28 21:22:35 +00001324 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001325}
1326
Chris Lattner349aaec2007-08-02 23:37:31 +00001327LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +00001328EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner998eab12009-12-23 21:31:11 +00001329 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
1330
Chris Lattner349aaec2007-08-02 23:37:31 +00001331 // Emit the base vector as an l-value.
Chris Lattner73525de2009-02-16 21:11:58 +00001332 LValue Base;
1333
1334 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattner998eab12009-12-23 21:31:11 +00001335 if (E->isArrow()) {
1336 // If it is a pointer to a vector, emit the address and form an lvalue with
1337 // it.
Chris Lattner2140e902009-02-16 22:14:05 +00001338 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
Chris Lattner998eab12009-12-23 21:31:11 +00001339 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001340 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1341 Quals.removeObjCGCAttr();
1342 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner998eab12009-12-23 21:31:11 +00001343 } else if (E->getBase()->isLvalue(getContext()) == Expr::LV_Valid) {
1344 // Otherwise, if the base is an lvalue ( as in the case of foo.x.x),
1345 // emit the base as an lvalue.
1346 assert(E->getBase()->getType()->isVectorType());
1347 Base = EmitLValue(E->getBase());
1348 } else {
1349 // Otherwise, the base is a normal rvalue (as in (V+V).x), emit it as such.
Daniel Dunbar302c3c22010-01-04 18:02:28 +00001350 assert(E->getBase()->getType()->getAs<VectorType>() &&
1351 "Result must be a vector");
Chris Lattner998eab12009-12-23 21:31:11 +00001352 llvm::Value *Vec = EmitScalarExpr(E->getBase());
1353
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001354 // Store the vector to memory (because LValue wants an address).
Chris Lattner998eab12009-12-23 21:31:11 +00001355 llvm::Value *VecMem =CreateTempAlloca(ConvertType(E->getBase()->getType()));
1356 Builder.CreateStore(Vec, VecMem);
Chris Lattner0ad57fb2009-12-23 21:33:41 +00001357 Base = LValue::MakeAddr(VecMem, Qualifiers());
Chris Lattner998eab12009-12-23 21:31:11 +00001358 }
1359
Nate Begeman3b8d1162008-05-13 21:03:02 +00001360 // Encode the element access list into a vector of unsigned indices.
1361 llvm::SmallVector<unsigned, 4> Indices;
1362 E->getEncodedElementAccess(Indices);
1363
1364 if (Base.isSimple()) {
Owen Andersona1cf15f2009-07-14 23:10:40 +00001365 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +00001366 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall0953e762009-09-24 19:53:00 +00001367 Base.getVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +00001368 }
1369 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1370
1371 llvm::Constant *BaseElts = Base.getExtVectorElts();
1372 llvm::SmallVector<llvm::Constant *, 4> CElts;
1373
1374 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1375 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner67665862009-10-28 05:12:07 +00001376 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001377 else
Chris Lattner67665862009-10-28 05:12:07 +00001378 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begeman3b8d1162008-05-13 21:03:02 +00001379 }
Owen Anderson4a289322009-07-28 21:22:35 +00001380 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +00001381 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall0953e762009-09-24 19:53:00 +00001382 Base.getVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +00001383}
1384
Devang Patelb9b00ad2007-10-23 20:28:39 +00001385LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001386 bool isNonGC = false;
Devang Patel126a8562007-10-24 22:26:28 +00001387 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +00001388 llvm::Value *BaseValue = NULL;
John McCall0953e762009-09-24 19:53:00 +00001389 Qualifiers BaseQuals;
Eli Friedman1e692ac2008-06-13 23:01:12 +00001390
Chris Lattner12f65f62007-12-02 18:52:07 +00001391 // 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 +00001392 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +00001393 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001394 const PointerType *PTy =
Ted Kremenek6217b802009-07-29 21:53:49 +00001395 BaseExpr->getType()->getAs<PointerType>();
John McCall0953e762009-09-24 19:53:00 +00001396 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahaniand2e1eb02009-09-01 17:02:21 +00001397 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1398 isa<ObjCImplicitSetterGetterRefExpr>(
1399 BaseExpr->IgnoreParens())) {
Fariborz Jahanian35c33292009-01-12 23:27:26 +00001400 RValue RV = EmitObjCPropertyGet(BaseExpr);
1401 BaseValue = RV.getAggregateAddr();
John McCall0953e762009-09-24 19:53:00 +00001402 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattner1bd885e2009-02-16 22:25:49 +00001403 } else {
Chris Lattner12f65f62007-12-02 18:52:07 +00001404 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian4f676ed2009-02-21 00:30:43 +00001405 if (BaseLV.isNonGC())
1406 isNonGC = true;
Chris Lattner12f65f62007-12-02 18:52:07 +00001407 // FIXME: this isn't right for bitfields.
1408 BaseValue = BaseLV.getAddress();
Fariborz Jahaniana91d6a62009-07-29 00:44:13 +00001409 QualType BaseTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001410 BaseQuals = BaseTy.getQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +00001411 }
Devang Patelb9b00ad2007-10-23 20:28:39 +00001412
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001413 NamedDecl *ND = E->getMemberDecl();
1414 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
Anders Carlssone6d2a532010-01-29 05:05:36 +00001415 LValue LV = EmitLValueForField(BaseValue, Field,
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001416 BaseQuals.getCVRQualifiers());
1417 LValue::SetObjCNonGC(LV, isNonGC);
1418 setObjCGCLValueClass(getContext(), E, LV);
1419 return LV;
1420 }
1421
Anders Carlsson589f9e32009-11-07 23:16:50 +00001422 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1423 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedman9a146302009-11-26 06:08:14 +00001424
1425 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1426 return EmitFunctionDeclLValue(*this, E, FD);
1427
Anders Carlssonce53f7d2009-11-07 23:06:58 +00001428 assert(false && "Unhandled member declaration!");
1429 return LValue();
Eli Friedman472778e2008-02-09 08:50:58 +00001430}
Devang Patelb9b00ad2007-10-23 20:28:39 +00001431
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001432LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001433 const FieldDecl* Field,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001434 unsigned CVRQualifiers) {
Anders Carlsson8330cee2009-07-23 17:01:21 +00001435 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1436
Mike Stumpf5408fe2009-05-16 07:57:57 +00001437 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1438 // FieldTy (the appropriate type is ABI-dependent).
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001439 const llvm::Type *FieldTy =
Daniel Dunbarbb767732009-02-17 18:31:04 +00001440 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001441 const llvm::PointerType *BaseTy =
1442 cast<llvm::PointerType>(BaseValue->getType());
1443 unsigned AS = BaseTy->getAddressSpace();
1444 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001445 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001446 "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001447
1448 llvm::Value *Idx =
Owen Anderson0032b272009-08-13 21:57:51 +00001449 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8330cee2009-07-23 17:01:21 +00001450 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001451
Anders Carlsson8330cee2009-07-23 17:01:21 +00001452 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001453 Field->getType()->isSignedIntegerType(),
1454 Field->getType().getCVRQualifiers()|CVRQualifiers);
1455}
1456
Eli Friedman472778e2008-02-09 08:50:58 +00001457LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlsson0ed303c2009-11-17 03:57:07 +00001458 const FieldDecl* Field,
Mike Stump1eb44332009-09-09 15:08:12 +00001459 unsigned CVRQualifiers) {
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001460 if (Field->isBitField())
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001461 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001462
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001463 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001464 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +00001465
Devang Patelabad06c2007-10-26 19:42:18 +00001466 // Match union field type.
Anders Carlssone6d2a532010-01-29 05:05:36 +00001467 if (Field->getParent()->isUnion()) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001468 const llvm::Type *FieldTy =
Eli Friedman1e692ac2008-06-13 23:01:12 +00001469 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001470 const llvm::PointerType * BaseTy =
Devang Patele9b8c0a2007-10-30 20:59:40 +00001471 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +00001472 unsigned AS = BaseTy->getAddressSpace();
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001473 V = Builder.CreateBitCast(V,
1474 llvm::PointerType::get(FieldTy, AS),
Eli Friedman788d5712008-05-21 13:24:44 +00001475 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +00001476 }
Eli Friedman2be58612009-05-30 21:09:44 +00001477 if (Field->getType()->isReferenceType())
1478 V = Builder.CreateLoad(V, "tmp");
John McCall0953e762009-09-24 19:53:00 +00001479
1480 Qualifiers Quals = MakeQualifiers(Field->getType());
1481 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001482 // __weak attribute on a field is ignored.
John McCall0953e762009-09-24 19:53:00 +00001483 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1484 Quals.removeObjCGCAttr();
Fariborz Jahanianfd02ed72009-09-21 18:54:29 +00001485
John McCall0953e762009-09-24 19:53:00 +00001486 return LValue::MakeAddr(V, Quals);
Devang Patelb9b00ad2007-10-23 20:28:39 +00001487}
1488
Anders Carlsson06a29702010-01-29 05:24:29 +00001489LValue
1490CodeGenFunction::EmitLValueForFieldInitialization(llvm::Value* BaseValue,
1491 const FieldDecl* Field,
1492 unsigned CVRQualifiers) {
1493 QualType FieldType = Field->getType();
1494
1495 if (!FieldType->isReferenceType())
1496 return EmitLValueForField(BaseValue, Field, CVRQualifiers);
1497
1498 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
1499 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
1500
1501 assert(!FieldType.getObjCGCAttr() && "fields cannot have GC attrs");
1502
1503 return LValue::MakeAddr(V, MakeQualifiers(FieldType));
1504}
1505
Chris Lattner75dfeda2009-03-18 18:28:57 +00001506LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman06e863f2008-05-13 23:18:27 +00001507 const llvm::Type *LTy = ConvertType(E->getType());
1508 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1509
1510 const Expr* InitExpr = E->getInitializer();
John McCall0953e762009-09-24 19:53:00 +00001511 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman06e863f2008-05-13 23:18:27 +00001512
Chris Lattnereb99b012009-10-28 17:39:19 +00001513 if (E->getType()->isComplexType())
Eli Friedman06e863f2008-05-13 23:18:27 +00001514 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001515 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman06e863f2008-05-13 23:18:27 +00001516 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnereb99b012009-10-28 17:39:19 +00001517 else
Eli Friedman06e863f2008-05-13 23:18:27 +00001518 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman06e863f2008-05-13 23:18:27 +00001519
1520 return Result;
1521}
1522
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001523LValue
1524CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1525 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Eli Friedmanab189952009-12-25 05:29:40 +00001526 if (int Cond = ConstantFoldsToSimpleInteger(E->getCond())) {
1527 Expr *Live = Cond == 1 ? E->getLHS() : E->getRHS();
1528 if (Live)
1529 return EmitLValue(Live);
1530 }
1531
1532 if (!E->getLHS())
1533 return EmitUnsupportedLValue(E, "conditional operator with missing LHS");
1534
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001535 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1536 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1537 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1538
Eli Friedman8e274bd2009-12-25 06:17:05 +00001539 EmitBranchOnBoolExpr(E->getCond(), LHSBlock, RHSBlock);
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001540
1541 EmitBlock(LHSBlock);
Daniel Dunbar90345582009-03-24 02:38:23 +00001542
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001543 LValue LHS = EmitLValue(E->getLHS());
1544 if (!LHS.isSimple())
1545 return EmitUnsupportedLValue(E, "conditional operator");
1546
Chris Lattnereb99b012009-10-28 17:39:19 +00001547 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001548 Builder.CreateStore(LHS.getAddress(), Temp);
1549 EmitBranch(ContBlock);
1550
1551 EmitBlock(RHSBlock);
1552 LValue RHS = EmitLValue(E->getRHS());
1553 if (!RHS.isSimple())
1554 return EmitUnsupportedLValue(E, "conditional operator");
1555
1556 Builder.CreateStore(RHS.getAddress(), Temp);
1557 EmitBranch(ContBlock);
1558
1559 EmitBlock(ContBlock);
1560
1561 Temp = Builder.CreateLoad(Temp, "lv");
John McCall0953e762009-09-24 19:53:00 +00001562 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson6fcec8b2009-09-15 16:35:24 +00001563 }
1564
Daniel Dunbar90345582009-03-24 02:38:23 +00001565 // ?: here should be an aggregate.
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001566 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar90345582009-03-24 02:38:23 +00001567 !E->getType()->isAnyComplexType()) &&
1568 "Unexpected conditional operator!");
1569
1570 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1571 EmitAggExpr(E, Temp, false);
1572
John McCall0953e762009-09-24 19:53:00 +00001573 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar90345582009-03-24 02:38:23 +00001574}
1575
Mike Stumpc849c052009-11-16 06:50:58 +00001576/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1577/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1578/// otherwise if a cast is needed by the code generator in an lvalue context,
1579/// then it must mean that we need the address of an aggregate in order to
1580/// access one of its fields. This can happen for all the reasons that casts
1581/// are permitted with aggregate result, including noop aggregate casts, and
1582/// cast from scalar to union.
Chris Lattner75dfeda2009-03-18 18:28:57 +00001583LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001584 switch (E->getCastKind()) {
1585 default:
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001586 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1587
Mike Stumpc849c052009-11-16 06:50:58 +00001588 case CastExpr::CK_Dynamic: {
1589 LValue LV = EmitLValue(E->getSubExpr());
1590 llvm::Value *V = LV.getAddress();
1591 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1592 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1593 MakeQualifiers(E->getType()));
1594 }
1595
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001596 case CastExpr::CK_NoOp:
1597 case CastExpr::CK_ConstructorConversion:
1598 case CastExpr::CK_UserDefinedConversion:
Fariborz Jahaniana7fa7cd2009-12-15 21:34:52 +00001599 case CastExpr::CK_AnyPointerToObjCPointerCast:
Chris Lattner75dfeda2009-03-18 18:28:57 +00001600 return EmitLValue(E->getSubExpr());
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001601
1602 case CastExpr::CK_DerivedToBase: {
1603 const RecordType *DerivedClassTy =
1604 E->getSubExpr()->getType()->getAs<RecordType>();
1605 CXXRecordDecl *DerivedClassDecl =
1606 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner75dfeda2009-03-18 18:28:57 +00001607
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001608 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1609 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1610
1611 LValue LV = EmitLValue(E->getSubExpr());
1612
1613 // Perform the derived-to-base conversion
1614 llvm::Value *Base =
Anders Carlssona3697c92009-11-23 17:57:54 +00001615 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1616 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001617
John McCall0953e762009-09-24 19:53:00 +00001618 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001619 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001620 case CastExpr::CK_ToUnion: {
1621 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1622 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001623
John McCall0953e762009-09-24 19:53:00 +00001624 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson658e8122009-11-14 21:21:42 +00001625 }
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001626 case CastExpr::CK_BaseToDerived: {
Anders Carlssona3697c92009-11-23 17:57:54 +00001627 const RecordType *BaseClassTy =
1628 E->getSubExpr()->getType()->getAs<RecordType>();
1629 CXXRecordDecl *BaseClassDecl =
1630 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1631
1632 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1633 CXXRecordDecl *DerivedClassDecl =
1634 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1635
1636 LValue LV = EmitLValue(E->getSubExpr());
1637
1638 // Perform the base-to-derived conversion
1639 llvm::Value *Derived =
1640 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1641 DerivedClassDecl, /*NullCheckValue=*/false);
1642
1643 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001644 }
Anders Carlsson658e8122009-11-14 21:21:42 +00001645 case CastExpr::CK_BitCast: {
Eli Friedmaneaae78a2009-11-16 05:48:01 +00001646 // This must be a reinterpret_cast (or c-style equivalent).
1647 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson658e8122009-11-14 21:21:42 +00001648
1649 LValue LV = EmitLValue(E->getSubExpr());
1650 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1651 ConvertType(CE->getTypeAsWritten()));
1652 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1653 }
Anders Carlsson0ee33cf2009-09-12 16:16:49 +00001654 }
Chris Lattner75dfeda2009-03-18 18:28:57 +00001655}
1656
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001657LValue CodeGenFunction::EmitNullInitializationLValue(
1658 const CXXZeroInitValueExpr *E) {
1659 QualType Ty = E->getType();
1660 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1661 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
Ken Dyck3228f422010-01-26 18:35:11 +00001662 CharUnits Align = getContext().getTypeAlignInChars(Ty);
1663 Alloc->setAlignment(Align.getQuantity());
Fariborz Jahanian48620ba2009-10-20 23:29:04 +00001664 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1665 EmitMemSetToZero(lvalue.getAddress(), Ty);
1666 return lvalue;
1667}
1668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669//===--------------------------------------------------------------------===//
1670// Expression Emission
1671//===--------------------------------------------------------------------===//
1672
Chris Lattner7016a702007-08-20 22:37:10 +00001673
Anders Carlssond2490a92009-12-24 20:40:36 +00001674RValue CodeGenFunction::EmitCallExpr(const CallExpr *E,
1675 ReturnValueSlot ReturnValue) {
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001676 // Builtins never have block type.
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001677 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlssona1736c02009-12-24 21:13:40 +00001678 return EmitBlockCallExpr(E, ReturnValue);
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001679
Anders Carlsson774e7c62009-04-03 22:50:24 +00001680 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
Anders Carlssona1736c02009-12-24 21:13:40 +00001681 return EmitCXXMemberCallExpr(CE, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001682
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001683 const Decl *TargetDecl = 0;
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001684 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1685 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1686 TargetDecl = DRE->getDecl();
1687 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001688 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbardd7b8972009-02-20 19:34:33 +00001689 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarc0ef9f52009-02-20 18:06:48 +00001690 }
1691 }
1692
Chris Lattner5db7ae52009-06-13 00:26:38 +00001693 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson0f294632009-05-27 04:18:27 +00001694 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
Anders Carlssona1736c02009-12-24 21:13:40 +00001695 return EmitCXXOperatorMemberCallExpr(CE, MD, ReturnValue);
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001696
Eli Friedmanc4451db2009-12-08 02:09:46 +00001697 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregora71d8192009-09-04 17:36:40 +00001698 // C++ [expr.pseudo]p1:
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001699 // The result shall only be used as the operand for the function call
Douglas Gregora71d8192009-09-04 17:36:40 +00001700 // operator (), and the result of such a call has type void. The only
1701 // effect is the evaluation of the postfix-expression before the dot or
1702 // arrow.
1703 EmitScalarExpr(E->getCallee());
1704 return RValue::get(0);
1705 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001706
Chris Lattner7f02f722007-08-24 05:35:26 +00001707 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlssond2490a92009-12-24 20:40:36 +00001708 return EmitCall(E->getCallee()->getType(), Callee, ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001709 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattnerc5e940f2007-08-31 04:44:06 +00001710}
1711
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001712LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattner7a574cc2009-05-12 21:28:12 +00001713 // Comma expressions just emit their LHS then their RHS as an l-value.
1714 if (E->getOpcode() == BinaryOperator::Comma) {
1715 EmitAnyExpr(E->getLHS());
Eli Friedman130c69e2009-12-07 20:18:11 +00001716 EnsureInsertPoint();
Chris Lattner7a574cc2009-05-12 21:28:12 +00001717 return EmitLValue(E->getRHS());
1718 }
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001719
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001720 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1721 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001722 return EmitPointerToDataMemberBinaryExpr(E);
1723
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001724 // Can only get l-value for binary operator expressions which are a
1725 // simple assignment of aggregate type.
1726 if (E->getOpcode() != BinaryOperator::Assign)
1727 return EmitUnsupportedLValue(E, "binary l-value expression");
1728
Anders Carlsson86aa0cd2009-10-19 18:28:22 +00001729 if (!hasAggregateLLVMType(E->getType())) {
1730 // Emit the LHS as an l-value.
1731 LValue LV = EmitLValue(E->getLHS());
1732
1733 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1734 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1735 E->getType());
1736 return LV;
1737 }
1738
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001739 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1740 EmitAggExpr(E, Temp, false);
1741 // FIXME: Are these qualifiers correct?
John McCall0953e762009-09-24 19:53:00 +00001742 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar80e62c22008-09-04 03:20:13 +00001743}
1744
Christopher Lamb22c940e2007-12-29 05:02:41 +00001745LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lamb22c940e2007-12-29 05:02:41 +00001746 RValue RV = EmitCallExpr(E);
Anders Carlsson48265682009-05-27 01:45:47 +00001747
Chris Lattnereb99b012009-10-28 17:39:19 +00001748 if (!RV.isScalar())
1749 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1750
1751 assert(E->getCallReturnType()->isReferenceType() &&
1752 "Can't have a scalar return unless the return type is a "
1753 "reference type!");
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001754
Chris Lattnereb99b012009-10-28 17:39:19 +00001755 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lamb22c940e2007-12-29 05:02:41 +00001756}
1757
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001758LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1759 // FIXME: This shouldn't require another copy.
1760 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1761 EmitAggExpr(E, Temp, false);
John McCall0953e762009-09-24 19:53:00 +00001762 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar5b5c9ef2009-02-11 20:59:32 +00001763}
1764
Anders Carlssonb58d0172009-05-30 23:23:33 +00001765LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1766 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1767 EmitCXXConstructExpr(Temp, E);
John McCall0953e762009-09-24 19:53:00 +00001768 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlssonb58d0172009-05-30 23:23:33 +00001769}
1770
Anders Carlssone61c9e82009-05-30 23:30:54 +00001771LValue
Mike Stumpc2e84ae2009-11-15 08:09:41 +00001772CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1773 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1774 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1775}
1776
1777LValue
Anders Carlssone61c9e82009-05-30 23:30:54 +00001778CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1779 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson543ac0c2009-05-31 00:34:10 +00001780 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssone61c9e82009-05-30 23:30:54 +00001781 return LV;
1782}
1783
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001784LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1785 // Can only get l-value for message expression returning aggregate type
1786 RValue RV = EmitObjCMessageExpr(E);
1787 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001788 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbar0a04d772008-08-23 10:51:21 +00001789}
1790
Daniel Dunbar2a031922009-04-22 05:08:15 +00001791llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001792 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001793 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001794}
1795
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001796LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1797 llvm::Value *BaseValue,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001798 const ObjCIvarDecl *Ivar,
1799 unsigned CVRQualifiers) {
Chris Lattner26f074b2009-04-17 17:44:48 +00001800 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001801 Ivar, CVRQualifiers);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001802}
1803
1804LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001805 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1806 llvm::Value *BaseValue = 0;
1807 const Expr *BaseExpr = E->getBase();
John McCall0953e762009-09-24 19:53:00 +00001808 Qualifiers BaseQuals;
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001809 QualType ObjectTy;
Anders Carlsson29b7e502008-08-25 01:53:23 +00001810 if (E->isArrow()) {
1811 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff14108da2009-07-10 23:34:53 +00001812 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00001813 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001814 } else {
1815 LValue BaseLV = EmitLValue(BaseExpr);
1816 // FIXME: this isn't right for bitfields.
1817 BaseValue = BaseLV.getAddress();
Fariborz Jahanian45012a72009-02-03 00:09:52 +00001818 ObjectTy = BaseExpr->getType();
John McCall0953e762009-09-24 19:53:00 +00001819 BaseQuals = ObjectTy.getQualifiers();
Anders Carlsson29b7e502008-08-25 01:53:23 +00001820 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001821
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001822 LValue LV =
John McCall0953e762009-09-24 19:53:00 +00001823 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1824 BaseQuals.getCVRQualifiers());
Fariborz Jahaniandbf3cfd2009-09-16 23:11:23 +00001825 setObjCGCLValueClass(getContext(), E, LV);
1826 return LV;
Chris Lattner391d77a2008-03-30 23:03:07 +00001827}
1828
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001829LValue
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001830CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001831 // This is a special l-value that just issues sends when we load or store
1832 // through it.
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001833 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1834}
1835
Chris Lattnereb99b012009-10-28 17:39:19 +00001836LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian09105f52009-08-20 17:02:02 +00001837 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001838 // This is a special l-value that just issues sends when we load or store
1839 // through it.
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001840 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1841}
1842
Chris Lattnereb99b012009-10-28 17:39:19 +00001843LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001844 return EmitUnsupportedLValue(E, "use of super");
1845}
1846
Chris Lattner65459942009-04-25 19:35:26 +00001847LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattner65459942009-04-25 19:35:26 +00001848 // Can only get l-value for message expression returning aggregate type
1849 RValue RV = EmitAnyExprToTemp(E);
1850 // FIXME: can this be volatile?
John McCall0953e762009-09-24 19:53:00 +00001851 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattner65459942009-04-25 19:35:26 +00001852}
1853
1854
Anders Carlsson909fbf72009-11-07 22:00:15 +00001855LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanian39762952009-10-21 21:01:47 +00001856 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1857 QualType NNSpecTy =
1858 getContext().getCanonicalType(
1859 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001860 NNSpecTy = getContext().getPointerType(NNSpecTy);
1861 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlssone6d2a532010-01-29 05:05:36 +00001862 LValue MemExpLV = EmitLValueForField(V, Field, /*Qualifiers=*/0);
Chris Lattnereb99b012009-10-28 17:39:19 +00001863 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1864 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson909fbf72009-11-07 22:00:15 +00001865 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahaniana6362992009-10-21 18:38:00 +00001866}
1867
Anders Carlsson31777a22009-12-24 19:08:58 +00001868RValue CodeGenFunction::EmitCall(QualType CalleeType, llvm::Value *Callee,
Anders Carlssond2490a92009-12-24 20:40:36 +00001869 ReturnValueSlot ReturnValue,
Anders Carlsson98647712009-05-27 01:22:39 +00001870 CallExpr::const_arg_iterator ArgBeg,
1871 CallExpr::const_arg_iterator ArgEnd,
1872 const Decl *TargetDecl) {
Mike Stumpdb52dcd2009-09-09 13:00:44 +00001873 // Get the actual function type. The callee type will always be a pointer to
1874 // function type or a block pointer type.
1875 assert(CalleeType->isFunctionPointerType() &&
Anders Carlsson8ac67a72009-04-07 18:53:02 +00001876 "Call must have function pointer type!");
1877
John McCall00a1ad92009-10-23 08:22:42 +00001878 CalleeType = getContext().getCanonicalType(CalleeType);
1879
1880 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1881 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001882
1883 CallArgList Args;
John McCall00a1ad92009-10-23 08:22:42 +00001884 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001885
Daniel Dunbar8a9f3fd2009-09-11 22:25:00 +00001886 // FIXME: We should not need to do this, it should be part of the function
1887 // type.
1888 unsigned CallingConvention = 0;
1889 if (const llvm::Function *F =
1890 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1891 CallingConvention = F->getCallingConv();
1892 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1893 CallingConvention),
Anders Carlssond2490a92009-12-24 20:40:36 +00001894 Callee, ReturnValue, Args, TargetDecl);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001895}
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001896
Chris Lattnereb99b012009-10-28 17:39:19 +00001897LValue CodeGenFunction::
1898EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001899 llvm::Value *BaseV;
Fariborz Jahanian52f08bc2009-10-26 21:58:25 +00001900 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001901 BaseV = EmitScalarExpr(E->getLHS());
1902 else
1903 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001904 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1905 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman1c5c1a02009-11-18 05:01:17 +00001906 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001907 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnereb99b012009-10-28 17:39:19 +00001908
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001909 QualType Ty = E->getRHS()->getType();
Chris Lattnereb99b012009-10-28 17:39:19 +00001910 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1911
1912 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001913 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnereb99b012009-10-28 17:39:19 +00001914 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanian8bfd31f2009-10-22 22:57:31 +00001915}
1916