blob: 8d1d05dc01ed32b95b9e7963cc673bcfc8cf1d36 [file] [log] [blame]
Chris Lattnere47e4402007-06-01 18:02:12 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnere47e4402007-06-01 18:02:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
Chris Lattnerb6984c42007-06-20 04:44:43 +000015#include "CodeGenModule.h"
Daniel Dunbar3d7c90b2008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarad319a72008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedmanf2442dc2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Chris Lattnere47e4402007-06-01 18:02:12 +000021using namespace clang;
22using namespace CodeGen;
23
Chris Lattnerd7f58862007-06-02 05:24:33 +000024//===--------------------------------------------------------------------===//
Chris Lattnerf0106d22007-06-02 19:33:17 +000025// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
Chris Lattnere9a64532007-06-22 21:44:33 +000028/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000031 const llvm::Twine &Name) {
Chris Lattner47640222009-03-22 00:24:14 +000032 if (!Builder.isNamePreserving())
Daniel Dunbarb5aacc22009-10-19 01:21:05 +000033 return new llvm::AllocaInst(Ty, 0, "", AllocaInsertPt);
Devang Pateldac79de2009-10-12 22:29:02 +000034 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
Chris Lattnere9a64532007-06-22 21:44:33 +000035}
Chris Lattner8394d792007-06-05 20:53:16 +000036
37/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
38/// expression and compare the result against zero, returning an Int1Ty value.
Chris Lattner23b7eb62007-06-15 23:05:46 +000039llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner268fcce2007-08-26 16:46:58 +000040 QualType BoolTy = getContext().BoolTy;
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000041 if (!E->getType()->isAnyComplexType())
Chris Lattner268fcce2007-08-26 16:46:58 +000042 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Chris Lattner8394d792007-06-05 20:53:16 +000043
Chris Lattner268fcce2007-08-26 16:46:58 +000044 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Chris Lattnerf0106d22007-06-02 19:33:17 +000045}
46
Chris Lattner4647a212007-08-31 22:49:20 +000047/// EmitAnyExpr - Emit code to compute the specified expression which can have
48/// any type. The result is returned as an RValue struct. If this is an
Mike Stump4a3999f2009-09-09 13:00:44 +000049/// aggregate expression, the aggloc/agglocvolatile arguments indicate where the
50/// result should be returned.
51RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
Anders Carlsson5b106a72009-08-16 07:36:22 +000052 bool IsAggLocVolatile, bool IgnoreResult,
53 bool IsInitializer) {
Chris Lattner4647a212007-08-31 22:49:20 +000054 if (!hasAggregateLLVMType(E->getType()))
Mike Stumpdf0fe272009-05-29 15:46:01 +000055 return RValue::get(EmitScalarExpr(E, IgnoreResult));
Chris Lattnerf3bc75a2008-04-04 16:54:41 +000056 else if (E->getType()->isAnyComplexType())
Mike Stumpdf0fe272009-05-29 15:46:01 +000057 return RValue::getComplex(EmitComplexExpr(E, false, false,
58 IgnoreResult, IgnoreResult));
Mike Stump4a3999f2009-09-09 13:00:44 +000059
Anders Carlsson5b106a72009-08-16 07:36:22 +000060 EmitAggExpr(E, AggLoc, IsAggLocVolatile, IgnoreResult, IsInitializer);
61 return RValue::getAggregate(AggLoc, IsAggLocVolatile);
Chris Lattner4647a212007-08-31 22:49:20 +000062}
63
Mike Stump4a3999f2009-09-09 13:00:44 +000064/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result will
65/// always be accessible even if no aggregate location is provided.
66RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000067 bool IsAggLocVolatile,
68 bool IsInitializer) {
69 llvm::Value *AggLoc = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +000070
71 if (hasAggregateLLVMType(E->getType()) &&
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000072 !E->getType()->isAnyComplexType())
73 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +000074 return EmitAnyExpr(E, AggLoc, IsAggLocVolatile, /*IgnoreResult=*/false,
Anders Carlsson5b106a72009-08-16 07:36:22 +000075 IsInitializer);
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000076}
77
Anders Carlsson6f5a0152009-05-20 00:24:07 +000078RValue CodeGenFunction::EmitReferenceBindingToExpr(const Expr* E,
Anders Carlsson5b106a72009-08-16 07:36:22 +000079 QualType DestType,
80 bool IsInitializer) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +000081 bool ShouldDestroyTemporaries = false;
82 unsigned OldNumLiveTemporaries = 0;
83
Anders Carlsson66413c22009-10-15 00:51:46 +000084 if (const CXXExprWithTemporaries *TE = dyn_cast<CXXExprWithTemporaries>(E)) {
Anders Carlsson69c2c4b2009-10-18 23:09:21 +000085 ShouldDestroyTemporaries = TE->shouldDestroyTemporaries();
86
Chris Lattnerab5e0af2009-10-28 17:39:19 +000087 // Keep track of the current cleanup stack depth.
88 if (ShouldDestroyTemporaries)
Anders Carlsson69c2c4b2009-10-18 23:09:21 +000089 OldNumLiveTemporaries = LiveTemporaries.size();
Anders Carlsson66413c22009-10-15 00:51:46 +000090
Anders Carlsson69c2c4b2009-10-18 23:09:21 +000091 E = TE->getSubExpr();
Anders Carlsson66413c22009-10-15 00:51:46 +000092 }
93
Eli Friedmanc21cb442009-05-20 02:31:19 +000094 RValue Val;
95 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
Anders Carlsson7d4c0832009-05-20 00:36:58 +000096 // Emit the expr as an lvalue.
97 LValue LV = EmitLValue(E);
Eli Friedmanc21cb442009-05-20 02:31:19 +000098 if (LV.isSimple())
99 return RValue::get(LV.getAddress());
100 Val = EmitLoadOfLValue(LV, E->getType());
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000101
102 if (ShouldDestroyTemporaries) {
103 // Pop temporaries.
104 while (LiveTemporaries.size() > OldNumLiveTemporaries)
105 PopCXXTemporary();
106 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000107 } else {
Anders Carlsson66413c22009-10-15 00:51:46 +0000108 const CXXRecordDecl *BaseClassDecl = 0;
109 const CXXRecordDecl *DerivedClassDecl = 0;
110
111 if (const CastExpr *CE =
112 dyn_cast<CastExpr>(E->IgnoreParenNoopCasts(getContext()))) {
113 if (CE->getCastKind() == CastExpr::CK_DerivedToBase) {
114 E = CE->getSubExpr();
115
116 BaseClassDecl =
117 cast<CXXRecordDecl>(CE->getType()->getAs<RecordType>()->getDecl());
118 DerivedClassDecl =
119 cast<CXXRecordDecl>(E->getType()->getAs<RecordType>()->getDecl());
120 }
121 }
122
Anders Carlsson5b106a72009-08-16 07:36:22 +0000123 Val = EmitAnyExprToTemp(E, /*IsAggLocVolatile=*/false,
124 IsInitializer);
Mike Stump4a3999f2009-09-09 13:00:44 +0000125
Anders Carlsson69c2c4b2009-10-18 23:09:21 +0000126 if (ShouldDestroyTemporaries) {
127 // Pop temporaries.
128 while (LiveTemporaries.size() > OldNumLiveTemporaries)
129 PopCXXTemporary();
130 }
131
Anders Carlsson3b848942009-08-16 17:54:29 +0000132 if (IsInitializer) {
133 // We might have to destroy the temporary variable.
134 if (const RecordType *RT = E->getType()->getAs<RecordType>()) {
135 if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
136 if (!ClassDecl->hasTrivialDestructor()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000137 const CXXDestructorDecl *Dtor =
Anders Carlsson3b848942009-08-16 17:54:29 +0000138 ClassDecl->getDestructor(getContext());
Mike Stump4a3999f2009-09-09 13:00:44 +0000139
Douglas Gregor48a409e2009-11-24 16:21:10 +0000140 DelayedCleanupBlock scope(*this);
Anders Carlsson3b848942009-08-16 17:54:29 +0000141 EmitCXXDestructorCall(Dtor, Dtor_Complete, Val.getAggregateAddr());
142 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000143 }
144 }
145 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000146
147 // Check if need to perform the derived-to-base cast.
148 if (BaseClassDecl) {
149 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000150 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000151 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
152 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000153 return RValue::get(Base);
154 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000155 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000156
157 if (Val.isAggregate()) {
158 Val = RValue::get(Val.getAggregateAddr());
159 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000160 // Create a temporary variable that we can bind the reference to.
Mike Stump4a3999f2009-09-09 13:00:44 +0000161 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlsson145eae52009-05-20 01:03:17 +0000162 "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000163 if (Val.isScalar())
164 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
165 else
166 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
167 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000168 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000169
170 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000171}
172
173
Mike Stump4a3999f2009-09-09 13:00:44 +0000174/// getAccessedFieldNo - Given an encoded value and a result number, return the
175/// input field number being accessed.
176unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000177 const llvm::Constant *Elts) {
178 if (isa<llvm::ConstantAggregateZero>(Elts))
179 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000180
Dan Gohman75d69da2008-05-22 00:50:06 +0000181 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
182}
183
Chris Lattner4647a212007-08-31 22:49:20 +0000184
Chris Lattnera45c5af2007-06-02 19:47:04 +0000185//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000186// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000187//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000188
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000189RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000190 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000191 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000192
193 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000194 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000195 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000196 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000197 }
198
199 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000200 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000201 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000202 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000203
204 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000205}
206
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000207RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
208 const char *Name) {
209 ErrorUnsupported(E, Name);
210 return GetUndefRValue(E->getType());
211}
212
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000213LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
214 const char *Name) {
215 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000216 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000217 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000218 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000219}
220
Chris Lattner8394d792007-06-05 20:53:16 +0000221/// EmitLValue - Emit code to compute a designator that specifies the location
222/// of the expression.
223///
Mike Stump4a3999f2009-09-09 13:00:44 +0000224/// This can return one of two things: a simple address or a bitfield reference.
225/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
226/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000227///
Mike Stump4a3999f2009-09-09 13:00:44 +0000228/// If this returns a bitfield reference, nothing about the pointee type of the
229/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000230///
Mike Stump4a3999f2009-09-09 13:00:44 +0000231/// If this returns a normal address, and if the lvalue's C type is fixed size,
232/// this method guarantees that the returned pointer type will point to an LLVM
233/// type of the same size of the lvalue's type. If the lvalue has a variable
234/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000235///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000236LValue CodeGenFunction::EmitLValue(const Expr *E) {
237 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000238 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000239
Mike Stump4a3999f2009-09-09 13:00:44 +0000240 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000241 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000242 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000243 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000244 case Expr::CXXOperatorCallExprClass:
245 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000246 case Expr::VAArgExprClass:
247 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000248 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000249 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000250 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000251 case Expr::PredefinedExprClass:
252 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000253 case Expr::StringLiteralClass:
254 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000255 case Expr::ObjCEncodeExprClass:
256 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000257
Mike Stump4a3999f2009-09-09 13:00:44 +0000258 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000259 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
260
Anders Carlsson3be22e22009-05-30 23:23:33 +0000261 case Expr::CXXTemporaryObjectExprClass:
262 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000263 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
264 case Expr::CXXBindTemporaryExprClass:
265 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000266 case Expr::CXXExprWithTemporariesClass:
267 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000268 case Expr::CXXZeroInitValueExprClass:
269 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
270 case Expr::CXXDefaultArgExprClass:
271 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000272 case Expr::CXXTypeidExprClass:
273 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000274
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000275 case Expr::ObjCMessageExprClass:
276 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000277 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000278 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000279 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000280 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000281 case Expr::ObjCImplicitSetterGetterRefExprClass:
282 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000283 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000284 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000285
Chris Lattnera4185c52009-04-25 19:35:26 +0000286 case Expr::StmtExprClass:
287 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000288 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000289 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000290 case Expr::ArraySubscriptExprClass:
291 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000292 case Expr::ExtVectorElementExprClass:
293 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000294 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000295 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000296 case Expr::CompoundLiteralExprClass:
297 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000298 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000299 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000300 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000301 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000302 case Expr::ImplicitCastExprClass:
303 case Expr::CStyleCastExprClass:
304 case Expr::CXXFunctionalCastExprClass:
305 case Expr::CXXStaticCastExprClass:
306 case Expr::CXXDynamicCastExprClass:
307 case Expr::CXXReinterpretCastExprClass:
308 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000309 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000310 }
311}
312
Daniel Dunbar1d425462009-02-10 00:57:50 +0000313llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
314 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000315 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
316 if (Volatile)
317 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000318
Anders Carlsson29a1be32009-05-19 19:36:19 +0000319 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000320 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000321 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000322 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
323 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000324
Daniel Dunbar1d425462009-02-10 00:57:50 +0000325 return V;
326}
327
328void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000329 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000330
Anders Carlsson29a1be32009-05-19 19:36:19 +0000331 if (Ty->isBooleanType()) {
332 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000333 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000334 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000335 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000336 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000337}
338
Mike Stump4a3999f2009-09-09 13:00:44 +0000339/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
340/// method emits the address of the lvalue, then loads the result as an rvalue,
341/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000342RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000343 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000344 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000345 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000346 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
347 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000348 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000349
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000350 if (LV.isSimple()) {
351 llvm::Value *Ptr = LV.getAddress();
352 const llvm::Type *EltTy =
353 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000354
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000355 // Simple scalar l-value.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000356 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000357 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000358 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000359
Chris Lattner6278e6a2007-08-11 00:04:45 +0000360 assert(ExprType->isFunctionType() && "Unknown scalar value");
361 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000362 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000363
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000364 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000365 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
366 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000367 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
368 "vecext"));
369 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000370
371 // If this is a reference to a subset of the elements of a vector, either
372 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000373 if (LV.isExtVectorElt())
374 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000375
376 if (LV.isBitfield())
377 return EmitLoadOfBitfieldLValue(LV, ExprType);
378
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000379 if (LV.isPropertyRef())
380 return EmitLoadOfPropertyRefLValue(LV, ExprType);
381
Chris Lattner6c7ce102009-02-16 21:11:58 +0000382 assert(LV.isKVCRef() && "Unknown LValue type!");
383 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000384}
385
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000386RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
387 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000388 unsigned StartBit = LV.getBitfieldStartBit();
389 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000390 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000391
Mike Stump4a3999f2009-09-09 13:00:44 +0000392 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000393 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000394 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000395
Mike Stump4a3999f2009-09-09 13:00:44 +0000396 // In some cases the bitfield may straddle two memory locations. Currently we
397 // load the entire bitfield, then do the magic to sign-extend it if
398 // necessary. This results in somewhat more code than necessary for the common
399 // case (one load), since two shifts accomplish both the masking and sign
400 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000401 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
402 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000403
Daniel Dunbaread7c912008-08-06 05:08:45 +0000404 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000405 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000406 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000407
Daniel Dunbaread7c912008-08-06 05:08:45 +0000408 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000409 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000410 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000411 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000412
Daniel Dunbaread7c912008-08-06 05:08:45 +0000413 // Fetch the high bits if necessary.
414 if (LowBits < BitfieldSize) {
415 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000416 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000417 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
418 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000419 LV.isVolatileQualified(),
420 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000421
Daniel Dunbaread7c912008-08-06 05:08:45 +0000422 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000423 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
424 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000425 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000426
Daniel Dunbaread7c912008-08-06 05:08:45 +0000427 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000428 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000429 Val = Builder.CreateOr(Val, HighVal, "bf.val");
430 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000431
Daniel Dunbaread7c912008-08-06 05:08:45 +0000432 // Sign extend if necessary.
433 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000434 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000435 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000436 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000437 ExtraBits, "bf.val.sext");
438 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000439
Mike Stump4a3999f2009-09-09 13:00:44 +0000440 // The bitfield type and the normal type differ when the storage sizes differ
441 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000442 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000443
Daniel Dunbaread7c912008-08-06 05:08:45 +0000444 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000445}
446
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000447RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
448 QualType ExprType) {
449 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
450}
451
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000452RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
453 QualType ExprType) {
454 return EmitObjCPropertyGet(LV.getKVCRefExpr());
455}
456
Nate Begemanb699c9b2009-01-18 06:42:49 +0000457// If this is a reference to a subset of the elements of a vector, create an
458// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000459RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
460 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000461 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
462 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000463
Nate Begemanf322eab2008-05-09 06:41:27 +0000464 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000465
466 // If the result of the expression is a non-vector type, we must be extracting
467 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000468 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000469 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000470 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000471 llvm::Value *Elt = llvm::ConstantInt::get(
472 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000473 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
474 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000475
476 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000477 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000478
Nate Begemanb699c9b2009-01-18 06:42:49 +0000479 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000480 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000481 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000482 Mask.push_back(llvm::ConstantInt::get(
483 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000484 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000485
Owen Anderson3cc120a2009-07-28 21:22:35 +0000486 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000487 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000488 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000489 MaskV, "tmp");
490 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000491}
492
493
Chris Lattner9369a562007-06-29 16:31:29 +0000494
Chris Lattner8394d792007-06-05 20:53:16 +0000495/// EmitStoreThroughLValue - Store the specified rvalue into the specified
496/// lvalue, where both are guaranteed to the have the same type, and that type
497/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000498void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000499 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000500 if (!Dst.isSimple()) {
501 if (Dst.isVectorElt()) {
502 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000503 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
504 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000505 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000506 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000507 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000508 return;
509 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000510
Nate Begemance4d7fc2008-04-18 23:10:10 +0000511 // If this is an update of extended vector elements, insert them as
512 // appropriate.
513 if (Dst.isExtVectorElt())
514 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000515
516 if (Dst.isBitfield())
517 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
518
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000519 if (Dst.isPropertyRef())
520 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
521
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000522 assert(Dst.isKVCRef() && "Unknown LValue type");
523 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000524 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000525
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000526 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000527 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000528 llvm::Value *LvalueDst = Dst.getAddress();
529 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000530 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000531 return;
532 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000533
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000534 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000535 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000536 llvm::Value *LvalueDst = Dst.getAddress();
537 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000538 if (Dst.isObjCIvar()) {
539 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
540 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
541 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000542 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000543 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
544 llvm::Value *LHS =
545 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
546 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000547 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000548 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000549 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000550 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
551 else
552 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000553 return;
554 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000555
Chris Lattner6278e6a2007-08-11 00:04:45 +0000556 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000557 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
558 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000559}
560
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000561void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000562 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000563 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000564 unsigned StartBit = Dst.getBitfieldStartBit();
565 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000566 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000567
Mike Stump4a3999f2009-09-09 13:00:44 +0000568 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000569 cast<llvm::PointerType>(Ptr->getType())->getElementType();
570 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
571
Mike Stump4a3999f2009-09-09 13:00:44 +0000572 // Get the new value, cast to the appropriate type and masked to exactly the
573 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000574 llvm::Value *SrcVal = Src.getScalarVal();
575 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000576 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
577 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000578 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000579
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000580 // Return the new value of the bit-field, if requested.
581 if (Result) {
582 // Cast back to the proper type for result.
583 const llvm::Type *SrcTy = SrcVal->getType();
584 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
585 "bf.reload.val");
586
587 // Sign extend if necessary.
588 if (Dst.isBitfieldSigned()) {
589 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000590 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000591 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000592 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000593 ExtraBits, "bf.reload.sext");
594 }
595
596 *Result = SrcTrunc;
597 }
598
Mike Stump4a3999f2009-09-09 13:00:44 +0000599 // In some cases the bitfield may straddle two memory locations. Emit the low
600 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000601 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
602 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
603 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000604
Daniel Dunbaread7c912008-08-06 05:08:45 +0000605 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000606 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000607 llvm::ConstantInt::get(VMContext,
608 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000609
Daniel Dunbaread7c912008-08-06 05:08:45 +0000610 // Compute the new low part as
611 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
612 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000613 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000614 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000615 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
616 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000617
Daniel Dunbaread7c912008-08-06 05:08:45 +0000618 // Write back.
619 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000620
Daniel Dunbaread7c912008-08-06 05:08:45 +0000621 // If the low part doesn't cover the bitfield emit a high part.
622 if (LowBits < BitfieldSize) {
623 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000624 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000625 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
626 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000627 Dst.isVolatileQualified(),
628 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000629
Daniel Dunbaread7c912008-08-06 05:08:45 +0000630 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000631 llvm::Constant *InvMask =
632 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000633 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000634
Daniel Dunbaread7c912008-08-06 05:08:45 +0000635 // Compute the new high part as
636 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
637 // where the high bits of NewVal have already been cleared and the
638 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000639 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000640 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000641 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
642 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000643
Daniel Dunbaread7c912008-08-06 05:08:45 +0000644 // Write back.
645 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
646 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000647}
648
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000649void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
650 LValue Dst,
651 QualType Ty) {
652 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
653}
654
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000655void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
656 LValue Dst,
657 QualType Ty) {
658 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
659}
660
Nate Begemance4d7fc2008-04-18 23:10:10 +0000661void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
662 LValue Dst,
663 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000664 // This access turns into a read/modify/write of the vector. Load the input
665 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000666 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
667 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000668 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000669
Chris Lattner4647a212007-08-31 22:49:20 +0000670 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000671
John McCall9dd450b2009-09-21 23:43:11 +0000672 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000673 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000674 unsigned NumDstElts =
675 cast<llvm::VectorType>(Vec->getType())->getNumElements();
676 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000677 // Use shuffle vector is the src and destination are the same number of
678 // elements and restore the vector mask since it is on the side it will be
679 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000680 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000681 for (unsigned i = 0; i != NumSrcElts; ++i) {
682 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000683 Mask[InIdx] = llvm::ConstantInt::get(
684 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000685 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000686
Owen Anderson3cc120a2009-07-28 21:22:35 +0000687 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000688 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000689 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000690 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000691 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000692 // Extended the source vector to the same length and then shuffle it
693 // into the destination.
694 // FIXME: since we're shuffling with undef, can we just use the indices
695 // into that? This could be simpler.
696 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000697 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000698 unsigned i;
699 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000700 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000701 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000702 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000703 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000704 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000705 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000706 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000707 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000708 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000709 // build identity
710 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000711 for (unsigned i = 0; i != NumDstElts; ++i)
712 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
713
Nate Begemanb699c9b2009-01-18 06:42:49 +0000714 // modify when what gets shuffled in
715 for (unsigned i = 0; i != NumSrcElts; ++i) {
716 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000717 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000718 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000719 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000720 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000721 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000722 // We should never shorten the vector
723 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000724 }
725 } else {
726 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000727 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000728 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
729 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000730 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000731 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000732
Eli Friedman327944b2008-06-13 23:01:12 +0000733 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000734}
735
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000736// setObjCGCLValueClass - sets class of he lvalue for the purpose of
737// generating write-barries API. It is currently a global, ivar,
738// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000739static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
740 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000741 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000742 return;
743
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000744 if (isa<ObjCIvarRefExpr>(E)) {
745 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000746 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
747 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000748 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000749 return;
750 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000751
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000752 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
753 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
754 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
755 VD->isFileVarDecl())
756 LV.SetGlobalObjCRef(LV, true);
757 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000758 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000759 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000760 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000761
762 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000763 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000764 return;
765 }
766
767 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000768 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000769 if (LV.isObjCIvar()) {
770 // If cast is to a structure pointer, follow gcc's behavior and make it
771 // a non-ivar write-barrier.
772 QualType ExpTy = E->getType();
773 if (ExpTy->isPointerType())
774 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
775 if (ExpTy->isRecordType())
776 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000777 }
778 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000779 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000780 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000781 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000782 return;
783 }
784
785 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000786 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000787 return;
788 }
789
790 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000791 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000792 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000793 // Using array syntax to assigning to what an ivar points to is not
794 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
795 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000796 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
797 // Using array syntax to assigning to what global points to is not
798 // same as assigning to the global itself. {id *G;} G[i] = 0;
799 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000800 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000801 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000802
803 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000804 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000805 // We don't know if member is an 'ivar', but this flag is looked at
806 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000807 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000808 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000809 }
810}
811
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000812static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
813 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +0000814 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000815 "Var decl must have external storage or be a file var decl!");
816
817 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
818 if (VD->getType()->isReferenceType())
819 V = CGF.Builder.CreateLoad(V, "tmp");
820 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
821 setObjCGCLValueClass(CGF.getContext(), E, LV);
822 return LV;
823}
824
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000825static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
826 const Expr *E, const FunctionDecl *FD) {
827 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
828 if (!FD->hasPrototype()) {
829 if (const FunctionProtoType *Proto =
830 FD->getType()->getAs<FunctionProtoType>()) {
831 // Ugly case: for a K&R-style definition, the type of the definition
832 // isn't the same as the type of a use. Correct for this with a
833 // bitcast.
834 QualType NoProtoType =
835 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
836 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
837 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
838 }
839 }
840 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
841}
842
Chris Lattnerd7f58862007-06-02 05:24:33 +0000843LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000844 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +0000845
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000846 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000847
848 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000849 if (VD->hasExternalStorage() || VD->isFileVarDecl())
850 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000851
852 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
853
854 llvm::Value *V = LocalDeclMap[VD];
855 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
856
857 Qualifiers Quals = MakeQualifiers(E->getType());
858 // local variables do not get their gc attribute set.
859 // local static?
860 if (NonGCable) Quals.removeObjCGCAttr();
861
862 if (VD->hasAttr<BlocksAttr>()) {
863 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000864 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000865 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
866 VD->getNameAsString());
867 }
868 if (VD->getType()->isReferenceType())
869 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000870 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000871 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000872 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000873 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000874 }
875
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000876 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
877 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000878
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000879 if (E->getQualifier()) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000880 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000881 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000882 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000883
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000884 assert(false && "Unhandled DeclRefExpr");
885
886 // an invalid LValue, but the assert will
887 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +0000888 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +0000889}
Chris Lattnere47e4402007-06-01 18:02:12 +0000890
Mike Stump1db7d042009-02-28 09:07:16 +0000891LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000892 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +0000893}
894
Chris Lattner8394d792007-06-05 20:53:16 +0000895LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
896 // __extension__ doesn't affect lvalue-ness.
897 if (E->getOpcode() == UnaryOperator::Extension)
898 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000899
Chris Lattner0f398c42008-07-26 22:37:01 +0000900 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +0000901 switch (E->getOpcode()) {
902 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000903 case UnaryOperator::Deref: {
904 QualType T = E->getSubExpr()->getType()->getPointeeType();
905 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000906
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000907 Qualifiers Quals = MakeQualifiers(T);
908 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +0000909
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000910 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
911 // We should not generate __weak write barrier on indirect reference
912 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
913 // But, we continue to generate __strong write barrier on indirect write
914 // into a pointer to object.
915 if (getContext().getLangOptions().ObjC1 &&
916 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
917 LV.isObjCWeak())
918 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
919 return LV;
920 }
Chris Lattner595db862007-10-30 22:53:42 +0000921 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000922 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +0000923 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +0000924 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
925 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +0000926 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +0000927 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +0000928 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000929 case UnaryOperator::PreInc:
930 case UnaryOperator::PreDec:
931 return EmitUnsupportedLValue(E, "pre-inc/dec expression");
932 }
Chris Lattner8394d792007-06-05 20:53:16 +0000933}
934
Chris Lattner4347e3692007-06-06 04:54:52 +0000935LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000936 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
937 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +0000938}
939
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000940LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000941 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
942 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000943}
944
945
Daniel Dunbarb3517472008-10-17 21:58:32 +0000946LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000947 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +0000948
949 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000950 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000951 case PredefinedExpr::Func:
952 GlobalVarName = "__func__.";
953 break;
954 case PredefinedExpr::Function:
955 GlobalVarName = "__FUNCTION__.";
956 break;
957 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000958 GlobalVarName = "__PRETTY_FUNCTION__.";
959 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000960 }
Daniel Dunbarb3517472008-10-17 21:58:32 +0000961
Daniel Dunbar0482cfd2009-09-12 23:06:21 +0000962 llvm::StringRef FnName = CurFn->getName();
963 if (FnName.startswith("\01"))
964 FnName = FnName.substr(1);
965 GlobalVarName += FnName;
966
Anders Carlsson2fb08242009-09-08 18:24:21 +0000967 std::string FunctionName =
Mike Stump11289f42009-09-09 15:08:12 +0000968 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson2fb08242009-09-08 18:24:21 +0000969 CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +0000970
Mike Stump4a3999f2009-09-09 13:00:44 +0000971 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +0000972 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +0000973 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +0000974}
975
Mike Stump4a3999f2009-09-09 13:00:44 +0000976LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +0000977 switch (E->getIdentType()) {
978 default:
979 return EmitUnsupportedLValue(E, "predefined expression");
980 case PredefinedExpr::Func:
981 case PredefinedExpr::Function:
982 case PredefinedExpr::PrettyFunction:
983 return EmitPredefinedFunctionName(E->getIdentType());
984 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000985}
986
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000987LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000988 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000989 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +0000990 QualType IdxTy = E->getIdx()->getType();
991 bool IdxSigned = IdxTy->isSignedIntegerType();
992
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000993 // If the base is a vector type, then we are forming a vector element lvalue
994 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +0000995 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000996 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +0000997 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +0000998 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +0000999 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001000 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001001 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001002 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001003 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001004
Ted Kremenekc81614d2007-08-20 16:18:38 +00001005 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001006 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001007
Ted Kremenekc81614d2007-08-20 16:18:38 +00001008 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001009 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001010 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001011 Idx = Builder.CreateIntCast(Idx,
1012 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001013 IdxSigned, "idxprom");
1014
Mike Stump4a3999f2009-09-09 13:00:44 +00001015 // We know that the pointer points to a type of the correct size, unless the
1016 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001017 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001018 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001019 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001020 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001021
Anders Carlsson3d312f82008-12-21 00:11:23 +00001022 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001023
Anders Carlssone0808df2008-12-21 03:44:36 +00001024 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001025
Anders Carlsson3d312f82008-12-21 00:11:23 +00001026 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1027 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001028 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson3d312f82008-12-21 00:11:23 +00001029 BaseTypeSize));
Dan Gohman43b44842009-08-12 00:33:55 +00001030 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001031 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001032 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001033 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001034 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001035 getContext().getTypeSize(OIT) / 8);
Mike Stump4a3999f2009-09-09 13:00:44 +00001036
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001037 Idx = Builder.CreateMul(Idx, InterfaceSize);
1038
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001039 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001040 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001041 Idx, "arrayidx");
1042 Address = Builder.CreateBitCast(Address, Base->getType());
1043 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001044 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001045 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001046
Steve Naroff7cae42b2009-07-10 23:34:53 +00001047 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001048 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001049 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001050
John McCall8ccfcb52009-09-24 19:53:00 +00001051 Qualifiers Quals = MakeQualifiers(T);
1052 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1053
1054 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001055 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001056 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001057 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001058 setObjCGCLValueClass(getContext(), E, LV);
1059 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001060 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001061}
1062
Mike Stump4a3999f2009-09-09 13:00:44 +00001063static
Owen Anderson170229f2009-07-14 23:10:40 +00001064llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1065 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begemand3862152008-05-13 21:03:02 +00001066 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001067
Nate Begemand3862152008-05-13 21:03:02 +00001068 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001069 CElts.push_back(llvm::ConstantInt::get(
1070 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001071
Owen Anderson3cc120a2009-07-28 21:22:35 +00001072 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001073}
1074
Chris Lattner9e751ca2007-08-02 23:37:31 +00001075LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001076EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner9e751ca2007-08-02 23:37:31 +00001077 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001078 LValue Base;
1079
1080 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001081 if (!E->isArrow()) {
Chris Lattner6c7ce102009-02-16 21:11:58 +00001082 assert(E->getBase()->getType()->isVectorType());
1083 Base = EmitLValue(E->getBase());
Chris Lattnerb8211f62009-02-16 22:14:05 +00001084 } else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001085 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattnerb8211f62009-02-16 22:14:05 +00001086 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
John McCall8ccfcb52009-09-24 19:53:00 +00001087 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1088 Quals.removeObjCGCAttr();
1089 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner6c7ce102009-02-16 21:11:58 +00001090 }
Chris Lattner9e751ca2007-08-02 23:37:31 +00001091
Nate Begemand3862152008-05-13 21:03:02 +00001092 // Encode the element access list into a vector of unsigned indices.
1093 llvm::SmallVector<unsigned, 4> Indices;
1094 E->getEncodedElementAccess(Indices);
1095
1096 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001097 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001098 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001099 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001100 }
1101 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1102
1103 llvm::Constant *BaseElts = Base.getExtVectorElts();
1104 llvm::SmallVector<llvm::Constant *, 4> CElts;
1105
Chris Lattner5e71d432009-10-28 05:12:07 +00001106 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemand3862152008-05-13 21:03:02 +00001107 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1108 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001109 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001110 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001111 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001112 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001113 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001114 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001115 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001116}
1117
Devang Patel30efa2e2007-10-23 20:28:39 +00001118LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelb37b12d2007-12-11 21:33:16 +00001119 bool isUnion = false;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001120 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001121 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001122 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001123 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001124
Chris Lattner4e4186b2007-12-02 18:52:07 +00001125 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelb37b12d2007-12-11 21:33:16 +00001126 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001127 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001128 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001129 BaseExpr->getType()->getAs<PointerType>();
Devang Patelb37b12d2007-12-11 21:33:16 +00001130 if (PTy->getPointeeType()->isUnionType())
1131 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001132 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001133 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1134 isa<ObjCImplicitSetterGetterRefExpr>(
1135 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001136 RValue RV = EmitObjCPropertyGet(BaseExpr);
1137 BaseValue = RV.getAggregateAddr();
1138 if (BaseExpr->getType()->isUnionType())
1139 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001140 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001141 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001142 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001143 if (BaseLV.isNonGC())
1144 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001145 // FIXME: this isn't right for bitfields.
1146 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001147 QualType BaseTy = BaseExpr->getType();
1148 if (BaseTy->isUnionType())
Devang Patelb37b12d2007-12-11 21:33:16 +00001149 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001150 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001151 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001152
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001153 NamedDecl *ND = E->getMemberDecl();
1154 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1155 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1156 BaseQuals.getCVRQualifiers());
1157 LValue::SetObjCNonGC(LV, isNonGC);
1158 setObjCGCLValueClass(getContext(), E, LV);
1159 return LV;
1160 }
1161
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001162 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1163 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001164
1165 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1166 return EmitFunctionDeclLValue(*this, E, FD);
1167
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001168 assert(false && "Unhandled member declaration!");
1169 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001170}
Devang Patel30efa2e2007-10-23 20:28:39 +00001171
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001172LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001173 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001174 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001175 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1176
Mike Stump18bb9282009-05-16 07:57:57 +00001177 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1178 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001179 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001180 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001181 const llvm::PointerType *BaseTy =
1182 cast<llvm::PointerType>(BaseValue->getType());
1183 unsigned AS = BaseTy->getAddressSpace();
1184 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001185 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001186 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001187
1188 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001189 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001190 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001191
Anders Carlsson8af896c2009-07-23 17:01:21 +00001192 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001193 Field->getType()->isSignedIntegerType(),
1194 Field->getType().getCVRQualifiers()|CVRQualifiers);
1195}
1196
Eli Friedmana62f3e12008-02-09 08:50:58 +00001197LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001198 const FieldDecl* Field,
Eli Friedman327944b2008-06-13 23:01:12 +00001199 bool isUnion,
Mike Stump11289f42009-09-09 15:08:12 +00001200 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001201 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001202 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001203
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001204 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001205 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001206
Devang Pateled93c3c2007-10-26 19:42:18 +00001207 // Match union field type.
Lauro Ramos Venancio9eff02d2008-02-07 19:29:53 +00001208 if (isUnion) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001209 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001210 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001211 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001212 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001213 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001214 V = Builder.CreateBitCast(V,
1215 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001216 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001217 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001218 if (Field->getType()->isReferenceType())
1219 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001220
1221 Qualifiers Quals = MakeQualifiers(Field->getType());
1222 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001223 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001224 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1225 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001226
John McCall8ccfcb52009-09-24 19:53:00 +00001227 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001228}
1229
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001230LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman9fd8b682008-05-13 23:18:27 +00001231 const llvm::Type *LTy = ConvertType(E->getType());
1232 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1233
1234 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001235 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001236
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001237 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001238 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001239 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001240 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001241 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001242 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001243
1244 return Result;
1245}
1246
Anders Carlsson1450adb2009-09-15 16:35:24 +00001247LValue
1248CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1249 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1250 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1251 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1252 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1253
1254 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1255 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1256
1257 EmitBlock(LHSBlock);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001258
Anders Carlsson1450adb2009-09-15 16:35:24 +00001259 LValue LHS = EmitLValue(E->getLHS());
1260 if (!LHS.isSimple())
1261 return EmitUnsupportedLValue(E, "conditional operator");
1262
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001263 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001264 Builder.CreateStore(LHS.getAddress(), Temp);
1265 EmitBranch(ContBlock);
1266
1267 EmitBlock(RHSBlock);
1268 LValue RHS = EmitLValue(E->getRHS());
1269 if (!RHS.isSimple())
1270 return EmitUnsupportedLValue(E, "conditional operator");
1271
1272 Builder.CreateStore(RHS.getAddress(), Temp);
1273 EmitBranch(ContBlock);
1274
1275 EmitBlock(ContBlock);
1276
1277 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001278 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001279 }
1280
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001281 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001282 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001283 !E->getType()->isAnyComplexType()) &&
1284 "Unexpected conditional operator!");
1285
1286 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1287 EmitAggExpr(E, Temp, false);
1288
John McCall8ccfcb52009-09-24 19:53:00 +00001289 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001290}
1291
Mike Stump65511702009-11-16 06:50:58 +00001292/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1293/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1294/// otherwise if a cast is needed by the code generator in an lvalue context,
1295/// then it must mean that we need the address of an aggregate in order to
1296/// access one of its fields. This can happen for all the reasons that casts
1297/// are permitted with aggregate result, including noop aggregate casts, and
1298/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001299LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001300 switch (E->getCastKind()) {
1301 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001302 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1303
Mike Stump65511702009-11-16 06:50:58 +00001304 case CastExpr::CK_Dynamic: {
1305 LValue LV = EmitLValue(E->getSubExpr());
1306 llvm::Value *V = LV.getAddress();
1307 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1308 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1309 MakeQualifiers(E->getType()));
1310 }
1311
Anders Carlssond95f9602009-09-12 16:16:49 +00001312 case CastExpr::CK_NoOp:
1313 case CastExpr::CK_ConstructorConversion:
1314 case CastExpr::CK_UserDefinedConversion:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001315 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001316
1317 case CastExpr::CK_DerivedToBase: {
1318 const RecordType *DerivedClassTy =
1319 E->getSubExpr()->getType()->getAs<RecordType>();
1320 CXXRecordDecl *DerivedClassDecl =
1321 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001322
Anders Carlssond95f9602009-09-12 16:16:49 +00001323 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1324 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1325
1326 LValue LV = EmitLValue(E->getSubExpr());
1327
1328 // Perform the derived-to-base conversion
1329 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001330 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1331 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001332
John McCall8ccfcb52009-09-24 19:53:00 +00001333 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001334 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001335 case CastExpr::CK_ToUnion: {
1336 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1337 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stump4a3999f2009-09-09 13:00:44 +00001338
John McCall8ccfcb52009-09-24 19:53:00 +00001339 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson50cb3212009-11-14 21:21:42 +00001340 }
Eli Friedman8c98dff2009-11-16 05:48:01 +00001341 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001342 const RecordType *BaseClassTy =
1343 E->getSubExpr()->getType()->getAs<RecordType>();
1344 CXXRecordDecl *BaseClassDecl =
1345 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1346
1347 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1348 CXXRecordDecl *DerivedClassDecl =
1349 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1350
1351 LValue LV = EmitLValue(E->getSubExpr());
1352
1353 // Perform the base-to-derived conversion
1354 llvm::Value *Derived =
1355 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1356 DerivedClassDecl, /*NullCheckValue=*/false);
1357
1358 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001359 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001360 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001361 // This must be a reinterpret_cast (or c-style equivalent).
1362 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001363
1364 LValue LV = EmitLValue(E->getSubExpr());
1365 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1366 ConvertType(CE->getTypeAsWritten()));
1367 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1368 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001369 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001370}
1371
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001372LValue CodeGenFunction::EmitNullInitializationLValue(
1373 const CXXZeroInitValueExpr *E) {
1374 QualType Ty = E->getType();
1375 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1376 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1377 unsigned Align = getContext().getTypeAlign(Ty)/8;
1378 Alloc->setAlignment(Align);
1379 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1380 EmitMemSetToZero(lvalue.getAddress(), Ty);
1381 return lvalue;
1382}
1383
Chris Lattnere47e4402007-06-01 18:02:12 +00001384//===--------------------------------------------------------------------===//
1385// Expression Emission
1386//===--------------------------------------------------------------------===//
1387
Chris Lattner76ba8492007-08-20 22:37:10 +00001388
Chris Lattner2b228c92007-06-15 21:34:29 +00001389RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001390 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001391 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlsson2437cbf2009-02-12 00:39:25 +00001392 return EmitBlockCallExpr(E);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001393
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001394 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1395 return EmitCXXMemberCallExpr(CE);
Mike Stump4a3999f2009-09-09 13:00:44 +00001396
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001397 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001398 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1399 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1400 TargetDecl = DRE->getDecl();
1401 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001402 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001403 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001404 }
1405 }
1406
Chris Lattner4ca97c32009-06-13 00:26:38 +00001407 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001408 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1409 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stump4a3999f2009-09-09 13:00:44 +00001410
Douglas Gregorad8a3362009-09-04 17:36:40 +00001411 if (isa<CXXPseudoDestructorExpr>(E->getCallee())) {
1412 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001413 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001414 // operator (), and the result of such a call has type void. The only
1415 // effect is the evaluation of the postfix-expression before the dot or
1416 // arrow.
1417 EmitScalarExpr(E->getCallee());
1418 return RValue::get(0);
1419 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001420
Chris Lattner2da04b32007-08-24 05:35:26 +00001421 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001422 return EmitCall(Callee, E->getCallee()->getType(),
1423 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001424}
1425
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001426LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001427 // Comma expressions just emit their LHS then their RHS as an l-value.
1428 if (E->getOpcode() == BinaryOperator::Comma) {
1429 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001430 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001431 return EmitLValue(E->getRHS());
1432 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001433
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001434 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1435 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001436 return EmitPointerToDataMemberBinaryExpr(E);
1437
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001438 // Can only get l-value for binary operator expressions which are a
1439 // simple assignment of aggregate type.
1440 if (E->getOpcode() != BinaryOperator::Assign)
1441 return EmitUnsupportedLValue(E, "binary l-value expression");
1442
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001443 if (!hasAggregateLLVMType(E->getType())) {
1444 // Emit the LHS as an l-value.
1445 LValue LV = EmitLValue(E->getLHS());
1446
1447 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1448 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1449 E->getType());
1450 return LV;
1451 }
1452
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001453 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1454 EmitAggExpr(E, Temp, false);
1455 // FIXME: Are these qualifiers correct?
John McCall8ccfcb52009-09-24 19:53:00 +00001456 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001457}
1458
Christopher Lambd91c3d42007-12-29 05:02:41 +00001459LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001460 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001461
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001462 if (!RV.isScalar())
1463 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1464
1465 assert(E->getCallReturnType()->isReferenceType() &&
1466 "Can't have a scalar return unless the return type is a "
1467 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001468
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001469 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001470}
1471
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001472LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1473 // FIXME: This shouldn't require another copy.
1474 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1475 EmitAggExpr(E, Temp, false);
John McCall8ccfcb52009-09-24 19:53:00 +00001476 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001477}
1478
Anders Carlsson3be22e22009-05-30 23:23:33 +00001479LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1480 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1481 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001482 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001483}
1484
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001485LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001486CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1487 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1488 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1489}
1490
1491LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001492CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1493 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001494 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001495 return LV;
1496}
1497
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001498LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1499 // Can only get l-value for message expression returning aggregate type
1500 RValue RV = EmitObjCMessageExpr(E);
1501 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001502 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001503}
1504
Daniel Dunbar722f4242009-04-22 05:08:15 +00001505llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001506 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001507 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001508}
1509
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001510LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1511 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001512 const ObjCIvarDecl *Ivar,
1513 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001514 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001515 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001516}
1517
1518LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001519 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1520 llvm::Value *BaseValue = 0;
1521 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001522 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001523 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001524 if (E->isArrow()) {
1525 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001526 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001527 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001528 } else {
1529 LValue BaseLV = EmitLValue(BaseExpr);
1530 // FIXME: this isn't right for bitfields.
1531 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001532 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001533 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001534 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001535
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001536 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001537 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1538 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001539 setObjCGCLValueClass(getContext(), E, LV);
1540 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001541}
1542
Mike Stump4a3999f2009-09-09 13:00:44 +00001543LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001544CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001545 // This is a special l-value that just issues sends when we load or store
1546 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001547 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1548}
1549
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001550LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001551 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001552 // This is a special l-value that just issues sends when we load or store
1553 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001554 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1555}
1556
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001557LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001558 return EmitUnsupportedLValue(E, "use of super");
1559}
1560
Chris Lattnera4185c52009-04-25 19:35:26 +00001561LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001562 // Can only get l-value for message expression returning aggregate type
1563 RValue RV = EmitAnyExprToTemp(E);
1564 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001565 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001566}
1567
1568
Anders Carlsson509850e2009-11-07 22:00:15 +00001569LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanianb25817a2009-10-21 21:01:47 +00001570 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1571 QualType NNSpecTy =
1572 getContext().getCanonicalType(
1573 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001574 NNSpecTy = getContext().getPointerType(NNSpecTy);
1575 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlssoncfd30122009-11-17 03:57:07 +00001576 LValue MemExpLV = EmitLValueForField(V, Field, /*isUnion=*/false,
1577 /*Qualifiers=*/0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001578 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1579 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson509850e2009-11-07 22:00:15 +00001580 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001581}
1582
Mike Stump4a3999f2009-09-09 13:00:44 +00001583RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001584 CallExpr::const_arg_iterator ArgBeg,
1585 CallExpr::const_arg_iterator ArgEnd,
1586 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001587 // Get the actual function type. The callee type will always be a pointer to
1588 // function type or a block pointer type.
1589 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001590 "Call must have function pointer type!");
1591
John McCall6fd4c232009-10-23 08:22:42 +00001592 CalleeType = getContext().getCanonicalType(CalleeType);
1593
1594 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1595 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001596
1597 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001598 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001599
Daniel Dunbarbbaeca42009-09-11 22:25:00 +00001600 // FIXME: We should not need to do this, it should be part of the function
1601 // type.
1602 unsigned CallingConvention = 0;
1603 if (const llvm::Function *F =
1604 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1605 CallingConvention = F->getCallingConv();
1606 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1607 CallingConvention),
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001608 Callee, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001609}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001610
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001611LValue CodeGenFunction::
1612EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001613 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001614 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001615 BaseV = EmitScalarExpr(E->getLHS());
1616 else
1617 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001618 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1619 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001620 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001621 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001622
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001623 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001624 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1625
1626 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001627 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001628 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001629}
1630