blob: fde944ffcd685fa6bc44e81c5c3c859bc8c282a8 [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
Mike Stumpaff69af2009-12-09 03:35:49 +0000140 {
141 DelayedCleanupBlock scope(*this);
142 EmitCXXDestructorCall(Dtor, Dtor_Complete,
143 Val.getAggregateAddr());
144 }
145 if (Exceptions) {
146 EHCleanupBlock Cleanup(*this);
147 EmitCXXDestructorCall(Dtor, Dtor_Complete,
148 Val.getAggregateAddr());
149 }
Anders Carlsson3b848942009-08-16 17:54:29 +0000150 }
Anders Carlssonb80760b2009-08-16 17:50:25 +0000151 }
152 }
153 }
Anders Carlsson66413c22009-10-15 00:51:46 +0000154
155 // Check if need to perform the derived-to-base cast.
156 if (BaseClassDecl) {
157 llvm::Value *Derived = Val.getAggregateAddr();
Anders Carlsson66413c22009-10-15 00:51:46 +0000158 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +0000159 GetAddressOfBaseClass(Derived, DerivedClassDecl, BaseClassDecl,
160 /*NullCheckValue=*/false);
Anders Carlsson66413c22009-10-15 00:51:46 +0000161 return RValue::get(Base);
162 }
Anders Carlsson7d4c0832009-05-20 00:36:58 +0000163 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000164
165 if (Val.isAggregate()) {
166 Val = RValue::get(Val.getAggregateAddr());
167 } else {
Anders Carlsson02bb7f02009-05-20 01:35:03 +0000168 // Create a temporary variable that we can bind the reference to.
Mike Stump4a3999f2009-09-09 13:00:44 +0000169 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()),
Anders Carlsson145eae52009-05-20 01:03:17 +0000170 "reftmp");
Eli Friedmanc21cb442009-05-20 02:31:19 +0000171 if (Val.isScalar())
172 EmitStoreOfScalar(Val.getScalarVal(), Temp, false, E->getType());
173 else
174 StoreComplexToAddr(Val.getComplexVal(), Temp, false);
175 Val = RValue::get(Temp);
Anders Carlsson145eae52009-05-20 01:03:17 +0000176 }
Eli Friedmanc21cb442009-05-20 02:31:19 +0000177
178 return Val;
Anders Carlsson6f5a0152009-05-20 00:24:07 +0000179}
180
181
Mike Stump4a3999f2009-09-09 13:00:44 +0000182/// getAccessedFieldNo - Given an encoded value and a result number, return the
183/// input field number being accessed.
184unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
Dan Gohman75d69da2008-05-22 00:50:06 +0000185 const llvm::Constant *Elts) {
186 if (isa<llvm::ConstantAggregateZero>(Elts))
187 return 0;
Mike Stump4a3999f2009-09-09 13:00:44 +0000188
Dan Gohman75d69da2008-05-22 00:50:06 +0000189 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
190}
191
Chris Lattner4647a212007-08-31 22:49:20 +0000192
Chris Lattnera45c5af2007-06-02 19:47:04 +0000193//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000194// LValue Expression Emission
Chris Lattnera45c5af2007-06-02 19:47:04 +0000195//===----------------------------------------------------------------------===//
Chris Lattnerd7f58862007-06-02 05:24:33 +0000196
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000197RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000198 if (Ty->isVoidType())
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000199 return RValue::get(0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000200
201 if (const ComplexType *CTy = Ty->getAs<ComplexType>()) {
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000202 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
Owen Anderson7ec07a52009-07-30 23:11:26 +0000203 llvm::Value *U = llvm::UndefValue::get(EltTy);
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000204 return RValue::getComplex(std::make_pair(U, U));
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000205 }
206
207 if (hasAggregateLLVMType(Ty)) {
Owen Anderson9793f0e2009-07-29 22:16:19 +0000208 const llvm::Type *LTy = llvm::PointerType::getUnqual(ConvertType(Ty));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000209 return RValue::getAggregate(llvm::UndefValue::get(LTy));
Daniel Dunbar8429dbc2009-01-09 20:09:28 +0000210 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000211
212 return RValue::get(llvm::UndefValue::get(ConvertType(Ty)));
Daniel Dunbarbb197e42009-01-09 16:50:52 +0000213}
214
Daniel Dunbarc79407f2009-02-05 07:09:07 +0000215RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
216 const char *Name) {
217 ErrorUnsupported(E, Name);
218 return GetUndefRValue(E->getType());
219}
220
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000221LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
222 const char *Name) {
223 ErrorUnsupported(E, Name);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000224 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
Owen Anderson7ec07a52009-07-30 23:11:26 +0000225 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
John McCall8ccfcb52009-09-24 19:53:00 +0000226 MakeQualifiers(E->getType()));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000227}
228
Chris Lattner8394d792007-06-05 20:53:16 +0000229/// EmitLValue - Emit code to compute a designator that specifies the location
230/// of the expression.
231///
Mike Stump4a3999f2009-09-09 13:00:44 +0000232/// This can return one of two things: a simple address or a bitfield reference.
233/// In either case, the LLVM Value* in the LValue structure is guaranteed to be
234/// an LLVM pointer type.
Chris Lattner8394d792007-06-05 20:53:16 +0000235///
Mike Stump4a3999f2009-09-09 13:00:44 +0000236/// If this returns a bitfield reference, nothing about the pointee type of the
237/// LLVM value is known: For example, it may not be a pointer to an integer.
Chris Lattner8394d792007-06-05 20:53:16 +0000238///
Mike Stump4a3999f2009-09-09 13:00:44 +0000239/// If this returns a normal address, and if the lvalue's C type is fixed size,
240/// this method guarantees that the returned pointer type will point to an LLVM
241/// type of the same size of the lvalue's type. If the lvalue has a variable
242/// length type, this is not possible.
Chris Lattner8394d792007-06-05 20:53:16 +0000243///
Chris Lattnerd7f58862007-06-02 05:24:33 +0000244LValue CodeGenFunction::EmitLValue(const Expr *E) {
245 switch (E->getStmtClass()) {
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000246 default: return EmitUnsupportedLValue(E, "l-value expression");
Chris Lattnerd7f58862007-06-02 05:24:33 +0000247
Mike Stump4a3999f2009-09-09 13:00:44 +0000248 case Expr::BinaryOperatorClass:
Daniel Dunbar8cde00a2008-09-04 03:20:13 +0000249 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000250 case Expr::CallExprClass:
Anders Carlssonc82555f2009-09-01 21:18:52 +0000251 case Expr::CXXMemberCallExprClass:
Douglas Gregor993603d2008-11-14 16:09:21 +0000252 case Expr::CXXOperatorCallExprClass:
253 return EmitCallExprLValue(cast<CallExpr>(E));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +0000254 case Expr::VAArgExprClass:
255 return EmitVAArgExprLValue(cast<VAArgExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000256 case Expr::DeclRefExprClass:
Douglas Gregorc7acfdf2009-01-06 05:10:23 +0000257 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Chris Lattner946aa312007-06-05 03:59:43 +0000258 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattner6307f192008-08-10 01:53:14 +0000259 case Expr::PredefinedExprClass:
260 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Chris Lattner4347e3692007-06-06 04:54:52 +0000261 case Expr::StringLiteralClass:
262 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000263 case Expr::ObjCEncodeExprClass:
264 return EmitObjCEncodeExprLValue(cast<ObjCEncodeExpr>(E));
Chris Lattner4bd55962008-03-30 23:03:07 +0000265
Mike Stump4a3999f2009-09-09 13:00:44 +0000266 case Expr::BlockDeclRefExprClass:
Mike Stump1db7d042009-02-28 09:07:16 +0000267 return EmitBlockDeclRefLValue(cast<BlockDeclRefExpr>(E));
268
Anders Carlsson3be22e22009-05-30 23:23:33 +0000269 case Expr::CXXTemporaryObjectExprClass:
270 case Expr::CXXConstructExprClass:
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000271 return EmitCXXConstructLValue(cast<CXXConstructExpr>(E));
272 case Expr::CXXBindTemporaryExprClass:
273 return EmitCXXBindTemporaryLValue(cast<CXXBindTemporaryExpr>(E));
Anders Carlsson96bad9a2009-09-14 01:10:45 +0000274 case Expr::CXXExprWithTemporariesClass:
275 return EmitCXXExprWithTemporariesLValue(cast<CXXExprWithTemporaries>(E));
Anders Carlsson52ce3bb2009-11-14 01:51:50 +0000276 case Expr::CXXZeroInitValueExprClass:
277 return EmitNullInitializationLValue(cast<CXXZeroInitValueExpr>(E));
278 case Expr::CXXDefaultArgExprClass:
279 return EmitLValue(cast<CXXDefaultArgExpr>(E)->getExpr());
Mike Stumpc9b231c2009-11-15 08:09:41 +0000280 case Expr::CXXTypeidExprClass:
281 return EmitCXXTypeidLValue(cast<CXXTypeidExpr>(E));
Anders Carlssonfd2af0c2009-05-30 23:30:54 +0000282
Daniel Dunbarc8317a42008-08-23 10:51:21 +0000283 case Expr::ObjCMessageExprClass:
284 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000285 case Expr::ObjCIvarRefExprClass:
Chris Lattner4bd55962008-03-30 23:03:07 +0000286 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbarf2e69882008-08-25 20:45:57 +0000287 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000288 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian9a846652009-08-20 17:02:02 +0000289 case Expr::ObjCImplicitSetterGetterRefExprClass:
290 return EmitObjCKVCRefLValue(cast<ObjCImplicitSetterGetterRefExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000291 case Expr::ObjCSuperExprClass:
Chris Lattnera4185c52009-04-25 19:35:26 +0000292 return EmitObjCSuperExprLValue(cast<ObjCSuperExpr>(E));
Douglas Gregor8ea1f532008-11-04 14:56:14 +0000293
Chris Lattnera4185c52009-04-25 19:35:26 +0000294 case Expr::StmtExprClass:
295 return EmitStmtExprLValue(cast<StmtExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000296 case Expr::UnaryOperatorClass:
Chris Lattner8394d792007-06-05 20:53:16 +0000297 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000298 case Expr::ArraySubscriptExprClass:
299 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begemance4d7fc2008-04-18 23:10:10 +0000300 case Expr::ExtVectorElementExprClass:
301 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Mike Stump4a3999f2009-09-09 13:00:44 +0000302 case Expr::MemberExprClass:
Douglas Gregorc1905232009-08-26 22:36:53 +0000303 return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman9fd8b682008-05-13 23:18:27 +0000304 case Expr::CompoundLiteralExprClass:
305 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +0000306 case Expr::ConditionalOperatorClass:
Anders Carlsson1450adb2009-09-15 16:35:24 +0000307 return EmitConditionalOperatorLValue(cast<ConditionalOperator>(E));
Chris Lattner053441f2008-12-12 05:35:08 +0000308 case Expr::ChooseExprClass:
Eli Friedmane0a5b8b2009-03-04 05:52:32 +0000309 return EmitLValue(cast<ChooseExpr>(E)->getChosenSubExpr(getContext()));
Chris Lattner63d06ab2009-03-18 04:02:57 +0000310 case Expr::ImplicitCastExprClass:
311 case Expr::CStyleCastExprClass:
312 case Expr::CXXFunctionalCastExprClass:
313 case Expr::CXXStaticCastExprClass:
314 case Expr::CXXDynamicCastExprClass:
315 case Expr::CXXReinterpretCastExprClass:
316 case Expr::CXXConstCastExprClass:
Chris Lattner28bcf1a2009-03-18 18:28:57 +0000317 return EmitCastLValue(cast<CastExpr>(E));
Chris Lattnerd7f58862007-06-02 05:24:33 +0000318 }
319}
320
Daniel Dunbar1d425462009-02-10 00:57:50 +0000321llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile,
322 QualType Ty) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000323 llvm::LoadInst *Load = Builder.CreateLoad(Addr, "tmp");
324 if (Volatile)
325 Load->setVolatile(true);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000326
Anders Carlsson29a1be32009-05-19 19:36:19 +0000327 // Bool can have different representation in memory than in registers.
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000328 llvm::Value *V = Load;
Daniel Dunbar1d425462009-02-10 00:57:50 +0000329 if (Ty->isBooleanType())
Owen Anderson41a75022009-08-13 21:57:51 +0000330 if (V->getType() != llvm::Type::getInt1Ty(VMContext))
331 V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool");
Mike Stump4a3999f2009-09-09 13:00:44 +0000332
Daniel Dunbar1d425462009-02-10 00:57:50 +0000333 return V;
334}
335
336void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr,
Anders Carlsson83709642009-05-19 18:50:41 +0000337 bool Volatile, QualType Ty) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000338
Anders Carlsson29a1be32009-05-19 19:36:19 +0000339 if (Ty->isBooleanType()) {
340 // Bool can have different representation in memory than in registers.
Anders Carlsson29a1be32009-05-19 19:36:19 +0000341 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
Eli Friedmanb2b120f2009-12-01 22:31:51 +0000342 Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000343 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000344 Builder.CreateStore(Value, Addr, Volatile);
Daniel Dunbar1d425462009-02-10 00:57:50 +0000345}
346
Mike Stump4a3999f2009-09-09 13:00:44 +0000347/// EmitLoadOfLValue - Given an expression that represents a value lvalue, this
348/// method emits the address of the lvalue, then loads the result as an rvalue,
349/// returning the rvalue.
Chris Lattner9369a562007-06-29 16:31:29 +0000350RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000351 if (LV.isObjCWeak()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000352 // load of a __weak object.
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000353 llvm::Value *AddrWeakObj = LV.getAddress();
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000354 return RValue::get(CGM.getObjCRuntime().EmitObjCWeakRead(*this,
355 AddrWeakObj));
Fariborz Jahanianf5125d12008-11-18 21:45:40 +0000356 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000357
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000358 if (LV.isSimple()) {
359 llvm::Value *Ptr = LV.getAddress();
360 const llvm::Type *EltTy =
361 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Mike Stump4a3999f2009-09-09 13:00:44 +0000362
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000363 // Simple scalar l-value.
Daniel Dunbar1d425462009-02-10 00:57:50 +0000364 if (EltTy->isSingleValueType())
Mike Stump4a3999f2009-09-09 13:00:44 +0000365 return RValue::get(EmitLoadOfScalar(Ptr, LV.isVolatileQualified(),
Daniel Dunbar1d425462009-02-10 00:57:50 +0000366 ExprType));
Mike Stump4a3999f2009-09-09 13:00:44 +0000367
Chris Lattner6278e6a2007-08-11 00:04:45 +0000368 assert(ExprType->isFunctionType() && "Unknown scalar value");
369 return RValue::get(Ptr);
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000370 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000371
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000372 if (LV.isVectorElt()) {
Eli Friedman327944b2008-06-13 23:01:12 +0000373 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
374 LV.isVolatileQualified(), "tmp");
Chris Lattner08c4b9f2007-07-10 21:17:59 +0000375 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
376 "vecext"));
377 }
Chris Lattner73ab9b32007-08-03 00:16:29 +0000378
379 // If this is a reference to a subset of the elements of a vector, either
380 // shuffle the input or extract/insert them as appropriate.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000381 if (LV.isExtVectorElt())
382 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000383
384 if (LV.isBitfield())
385 return EmitLoadOfBitfieldLValue(LV, ExprType);
386
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000387 if (LV.isPropertyRef())
388 return EmitLoadOfPropertyRefLValue(LV, ExprType);
389
Chris Lattner6c7ce102009-02-16 21:11:58 +0000390 assert(LV.isKVCRef() && "Unknown LValue type!");
391 return EmitLoadOfKVCRefLValue(LV, ExprType);
Chris Lattner8394d792007-06-05 20:53:16 +0000392}
393
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000394RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
395 QualType ExprType) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000396 unsigned StartBit = LV.getBitfieldStartBit();
397 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000398 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000399
Mike Stump4a3999f2009-09-09 13:00:44 +0000400 const llvm::Type *EltTy =
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000401 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbaread7c912008-08-06 05:08:45 +0000402 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000403
Mike Stump4a3999f2009-09-09 13:00:44 +0000404 // In some cases the bitfield may straddle two memory locations. Currently we
405 // load the entire bitfield, then do the magic to sign-extend it if
406 // necessary. This results in somewhat more code than necessary for the common
407 // case (one load), since two shifts accomplish both the masking and sign
408 // extension.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000409 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
410 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000411
Daniel Dunbaread7c912008-08-06 05:08:45 +0000412 // Shift to proper location.
Daniel Dunbarf7fb7502008-11-13 02:20:34 +0000413 if (StartBit)
Chris Lattner72ecc682009-12-06 17:22:42 +0000414 Val = Builder.CreateLShr(Val, StartBit, "bf.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000415
Daniel Dunbaread7c912008-08-06 05:08:45 +0000416 // Mask off unused bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000417 llvm::Constant *LowMask = llvm::ConstantInt::get(VMContext,
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000418 llvm::APInt::getLowBitsSet(EltTySize, LowBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000419 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
Mike Stump4a3999f2009-09-09 13:00:44 +0000420
Daniel Dunbaread7c912008-08-06 05:08:45 +0000421 // Fetch the high bits if necessary.
422 if (LowBits < BitfieldSize) {
423 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000424 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000425 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
426 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000427 LV.isVolatileQualified(),
428 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000429
Daniel Dunbaread7c912008-08-06 05:08:45 +0000430 // Mask off unused bits.
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000431 llvm::Constant *HighMask = llvm::ConstantInt::get(VMContext,
432 llvm::APInt::getLowBitsSet(EltTySize, HighBits));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000433 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000434
Daniel Dunbaread7c912008-08-06 05:08:45 +0000435 // Shift to proper location and or in to bitfield value.
Chris Lattner72ecc682009-12-06 17:22:42 +0000436 HighVal = Builder.CreateShl(HighVal, LowBits);
Daniel Dunbaread7c912008-08-06 05:08:45 +0000437 Val = Builder.CreateOr(Val, HighVal, "bf.val");
438 }
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000439
Daniel Dunbaread7c912008-08-06 05:08:45 +0000440 // Sign extend if necessary.
441 if (LV.isBitfieldSigned()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000442 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000443 EltTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000444 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
Daniel Dunbaread7c912008-08-06 05:08:45 +0000445 ExtraBits, "bf.val.sext");
446 }
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000447
Mike Stump4a3999f2009-09-09 13:00:44 +0000448 // The bitfield type and the normal type differ when the storage sizes differ
449 // (currently just _Bool).
Daniel Dunbaread7c912008-08-06 05:08:45 +0000450 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000451
Daniel Dunbaread7c912008-08-06 05:08:45 +0000452 return RValue::get(Val);
Lauro Ramos Venancio2ddcb25a32008-01-22 20:17:04 +0000453}
454
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000455RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
456 QualType ExprType) {
457 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
458}
459
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000460RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
461 QualType ExprType) {
462 return EmitObjCPropertyGet(LV.getKVCRefExpr());
463}
464
Nate Begemanb699c9b2009-01-18 06:42:49 +0000465// If this is a reference to a subset of the elements of a vector, create an
466// appropriate shufflevector.
Nate Begemance4d7fc2008-04-18 23:10:10 +0000467RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
468 QualType ExprType) {
Eli Friedman327944b2008-06-13 23:01:12 +0000469 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
470 LV.isVolatileQualified(), "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +0000471
Nate Begemanf322eab2008-05-09 06:41:27 +0000472 const llvm::Constant *Elts = LV.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000473
474 // If the result of the expression is a non-vector type, we must be extracting
475 // a single element. Just codegen as an extractelement.
John McCall9dd450b2009-09-21 23:43:11 +0000476 const VectorType *ExprVT = ExprType->getAs<VectorType>();
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000477 if (!ExprVT) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000478 unsigned InIdx = getAccessedFieldNo(0, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000479 llvm::Value *Elt = llvm::ConstantInt::get(
480 llvm::Type::getInt32Ty(VMContext), InIdx);
Chris Lattner40ff7012007-08-03 16:18:34 +0000481 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
482 }
Nate Begemanb699c9b2009-01-18 06:42:49 +0000483
484 // Always use shuffle vector to try to retain the original program structure
Chris Lattner8eab8ff2007-08-10 17:10:08 +0000485 unsigned NumResultElts = ExprVT->getNumElements();
Mike Stump4a3999f2009-09-09 13:00:44 +0000486
Nate Begemanb699c9b2009-01-18 06:42:49 +0000487 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattner40ff7012007-08-03 16:18:34 +0000488 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman75d69da2008-05-22 00:50:06 +0000489 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000490 Mask.push_back(llvm::ConstantInt::get(
491 llvm::Type::getInt32Ty(VMContext), InIdx));
Chris Lattner40ff7012007-08-03 16:18:34 +0000492 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000493
Owen Anderson3cc120a2009-07-28 21:22:35 +0000494 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000495 Vec = Builder.CreateShuffleVector(Vec,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000496 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000497 MaskV, "tmp");
498 return RValue::get(Vec);
Chris Lattner40ff7012007-08-03 16:18:34 +0000499}
500
501
Chris Lattner9369a562007-06-29 16:31:29 +0000502
Chris Lattner8394d792007-06-05 20:53:16 +0000503/// EmitStoreThroughLValue - Store the specified rvalue into the specified
504/// lvalue, where both are guaranteed to the have the same type, and that type
505/// is 'Ty'.
Mike Stump4a3999f2009-09-09 13:00:44 +0000506void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
Chris Lattner8394d792007-06-05 20:53:16 +0000507 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000508 if (!Dst.isSimple()) {
509 if (Dst.isVectorElt()) {
510 // Read/modify/write the vector, inserting the new element.
Eli Friedman327944b2008-06-13 23:01:12 +0000511 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
512 Dst.isVolatileQualified(), "tmp");
Chris Lattner4647a212007-08-31 22:49:20 +0000513 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner41d480e2007-08-03 16:28:33 +0000514 Dst.getVectorIdx(), "vecins");
Eli Friedman327944b2008-06-13 23:01:12 +0000515 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000516 return;
517 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000518
Nate Begemance4d7fc2008-04-18 23:10:10 +0000519 // If this is an update of extended vector elements, insert them as
520 // appropriate.
521 if (Dst.isExtVectorElt())
522 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000523
524 if (Dst.isBitfield())
525 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
526
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000527 if (Dst.isPropertyRef())
528 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
529
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000530 assert(Dst.isKVCRef() && "Unknown LValue type");
531 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
Chris Lattner41d480e2007-08-03 16:28:33 +0000532 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000533
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000534 if (Dst.isObjCWeak() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000535 // load of a __weak object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000536 llvm::Value *LvalueDst = Dst.getAddress();
537 llvm::Value *src = Src.getScalarVal();
Mike Stumpca5ae662009-04-14 00:57:29 +0000538 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000539 return;
540 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000541
Fariborz Jahanian10bec102009-02-21 00:30:43 +0000542 if (Dst.isObjCStrong() && !Dst.isNonGC()) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000543 // load of a __strong object.
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000544 llvm::Value *LvalueDst = Dst.getAddress();
545 llvm::Value *src = Src.getScalarVal();
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000546 if (Dst.isObjCIvar()) {
547 assert(Dst.getBaseIvarExp() && "BaseIvarExp is NULL");
548 const llvm::Type *ResultType = ConvertType(getContext().LongTy);
549 llvm::Value *RHS = EmitScalarExpr(Dst.getBaseIvarExp());
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000550 llvm::Value *dst = RHS;
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000551 RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
552 llvm::Value *LHS =
553 Builder.CreatePtrToInt(LvalueDst, ResultType, "sub.ptr.lhs.cast");
554 llvm::Value *BytesBetween = Builder.CreateSub(LHS, RHS, "ivar.offset");
Fariborz Jahanian1f9ed582009-09-25 00:00:20 +0000555 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000556 BytesBetween);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000557 } else if (Dst.isGlobalObjCRef())
Fariborz Jahanian32ff7ae2009-05-04 23:27:20 +0000558 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
559 else
560 CGM.getObjCRuntime().EmitObjCStrongCastAssign(*this, src, LvalueDst);
Fariborz Jahanian50a12702008-11-19 17:34:06 +0000561 return;
562 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000563
Chris Lattner6278e6a2007-08-11 00:04:45 +0000564 assert(Src.isScalar() && "Can't emit an agg store with this method");
Anders Carlsson83709642009-05-19 18:50:41 +0000565 EmitStoreOfScalar(Src.getScalarVal(), Dst.getAddress(),
566 Dst.isVolatileQualified(), Ty);
Chris Lattner8394d792007-06-05 20:53:16 +0000567}
568
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000569void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Mike Stump4a3999f2009-09-09 13:00:44 +0000570 QualType Ty,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000571 llvm::Value **Result) {
Daniel Dunbaread7c912008-08-06 05:08:45 +0000572 unsigned StartBit = Dst.getBitfieldStartBit();
573 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000574 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000575
Mike Stump4a3999f2009-09-09 13:00:44 +0000576 const llvm::Type *EltTy =
Daniel Dunbaread7c912008-08-06 05:08:45 +0000577 cast<llvm::PointerType>(Ptr->getType())->getElementType();
578 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
579
Mike Stump4a3999f2009-09-09 13:00:44 +0000580 // Get the new value, cast to the appropriate type and masked to exactly the
581 // size of the bit-field.
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000582 llvm::Value *SrcVal = Src.getScalarVal();
583 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000584 llvm::Constant *Mask = llvm::ConstantInt::get(VMContext,
585 llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
Daniel Dunbaread7c912008-08-06 05:08:45 +0000586 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000587
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000588 // Return the new value of the bit-field, if requested.
589 if (Result) {
590 // Cast back to the proper type for result.
591 const llvm::Type *SrcTy = SrcVal->getType();
592 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
593 "bf.reload.val");
594
595 // Sign extend if necessary.
596 if (Dst.isBitfieldSigned()) {
597 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000598 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000599 SrcTySize - BitfieldSize);
Mike Stump4a3999f2009-09-09 13:00:44 +0000600 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
Daniel Dunbar9b1335e2008-11-19 09:36:46 +0000601 ExtraBits, "bf.reload.sext");
602 }
603
604 *Result = SrcTrunc;
605 }
606
Mike Stump4a3999f2009-09-09 13:00:44 +0000607 // In some cases the bitfield may straddle two memory locations. Emit the low
608 // part first and check to see if the high needs to be done.
Daniel Dunbaread7c912008-08-06 05:08:45 +0000609 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
610 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
611 "bf.prev.low");
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000612
Daniel Dunbaread7c912008-08-06 05:08:45 +0000613 // Compute the mask for zero-ing the low part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000614 llvm::Constant *InvMask =
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000615 llvm::ConstantInt::get(VMContext,
616 ~llvm::APInt::getBitsSet(EltTySize, StartBit, StartBit + LowBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000617
Daniel Dunbaread7c912008-08-06 05:08:45 +0000618 // Compute the new low part as
619 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
620 // with the shift of NewVal implicitly stripping the high bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000621 llvm::Value *NewLowVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000622 Builder.CreateShl(NewVal, StartBit, "bf.value.lo");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000623 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
624 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
Mike Stump4a3999f2009-09-09 13:00:44 +0000625
Daniel Dunbaread7c912008-08-06 05:08:45 +0000626 // Write back.
627 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedmanf2442dc2008-05-17 20:03:47 +0000628
Daniel Dunbaread7c912008-08-06 05:08:45 +0000629 // If the low part doesn't cover the bitfield emit a high part.
630 if (LowBits < BitfieldSize) {
631 unsigned HighBits = BitfieldSize - LowBits;
Owen Anderson41a75022009-08-13 21:57:51 +0000632 llvm::Value *HighPtr = Builder.CreateGEP(Ptr, llvm::ConstantInt::get(
Mike Stump4a3999f2009-09-09 13:00:44 +0000633 llvm::Type::getInt32Ty(VMContext), 1), "bf.ptr.hi");
634 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
Daniel Dunbaread7c912008-08-06 05:08:45 +0000635 Dst.isVolatileQualified(),
636 "bf.prev.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000637
Daniel Dunbaread7c912008-08-06 05:08:45 +0000638 // Compute the mask for zero-ing the high part of this bitfield.
Mike Stump4a3999f2009-09-09 13:00:44 +0000639 llvm::Constant *InvMask =
640 llvm::ConstantInt::get(VMContext, ~llvm::APInt::getLowBitsSet(EltTySize,
Owen Anderson170229f2009-07-14 23:10:40 +0000641 HighBits));
Mike Stump4a3999f2009-09-09 13:00:44 +0000642
Daniel Dunbaread7c912008-08-06 05:08:45 +0000643 // Compute the new high part as
644 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
645 // where the high bits of NewVal have already been cleared and the
646 // shift stripping the low bits.
Mike Stump4a3999f2009-09-09 13:00:44 +0000647 llvm::Value *NewHighVal =
Chris Lattner72ecc682009-12-06 17:22:42 +0000648 Builder.CreateLShr(NewVal, LowBits, "bf.value.high");
Daniel Dunbaread7c912008-08-06 05:08:45 +0000649 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
650 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
Mike Stump4a3999f2009-09-09 13:00:44 +0000651
Daniel Dunbaread7c912008-08-06 05:08:45 +0000652 // Write back.
653 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
654 }
Lauro Ramos Venancio09af71c2008-01-22 22:36:45 +0000655}
656
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +0000657void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
658 LValue Dst,
659 QualType Ty) {
660 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
661}
662
Fariborz Jahanian9ac53512008-11-22 22:30:21 +0000663void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
664 LValue Dst,
665 QualType Ty) {
666 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
667}
668
Nate Begemance4d7fc2008-04-18 23:10:10 +0000669void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
670 LValue Dst,
671 QualType Ty) {
Chris Lattner41d480e2007-08-03 16:28:33 +0000672 // This access turns into a read/modify/write of the vector. Load the input
673 // value now.
Eli Friedman327944b2008-06-13 23:01:12 +0000674 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
675 Dst.isVolatileQualified(), "tmp");
Nate Begemanf322eab2008-05-09 06:41:27 +0000676 const llvm::Constant *Elts = Dst.getExtVectorElts();
Mike Stump4a3999f2009-09-09 13:00:44 +0000677
Chris Lattner4647a212007-08-31 22:49:20 +0000678 llvm::Value *SrcVal = Src.getScalarVal();
Mike Stump4a3999f2009-09-09 13:00:44 +0000679
John McCall9dd450b2009-09-21 23:43:11 +0000680 if (const VectorType *VTy = Ty->getAs<VectorType>()) {
Chris Lattner3a44aa72007-08-03 16:37:04 +0000681 unsigned NumSrcElts = VTy->getNumElements();
Nate Begemanb699c9b2009-01-18 06:42:49 +0000682 unsigned NumDstElts =
683 cast<llvm::VectorType>(Vec->getType())->getNumElements();
684 if (NumDstElts == NumSrcElts) {
Mike Stump4a3999f2009-09-09 13:00:44 +0000685 // Use shuffle vector is the src and destination are the same number of
686 // elements and restore the vector mask since it is on the side it will be
687 // stored.
Nate Begemanea12f6e2009-06-26 21:12:50 +0000688 llvm::SmallVector<llvm::Constant*, 4> Mask(NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000689 for (unsigned i = 0; i != NumSrcElts; ++i) {
690 unsigned InIdx = getAccessedFieldNo(i, Elts);
Owen Anderson41a75022009-08-13 21:57:51 +0000691 Mask[InIdx] = llvm::ConstantInt::get(
692 llvm::Type::getInt32Ty(VMContext), i);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000693 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000694
Owen Anderson3cc120a2009-07-28 21:22:35 +0000695 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000696 Vec = Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000697 llvm::UndefValue::get(Vec->getType()),
Nate Begemanb699c9b2009-01-18 06:42:49 +0000698 MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000699 } else if (NumDstElts > NumSrcElts) {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000700 // Extended the source vector to the same length and then shuffle it
701 // into the destination.
702 // FIXME: since we're shuffling with undef, can we just use the indices
703 // into that? This could be simpler.
704 llvm::SmallVector<llvm::Constant*, 4> ExtMask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000705 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000706 unsigned i;
707 for (i = 0; i != NumSrcElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000708 ExtMask.push_back(llvm::ConstantInt::get(Int32Ty, i));
Nate Begemanb699c9b2009-01-18 06:42:49 +0000709 for (; i != NumDstElts; ++i)
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000710 ExtMask.push_back(llvm::UndefValue::get(Int32Ty));
Owen Anderson3cc120a2009-07-28 21:22:35 +0000711 llvm::Value *ExtMaskV = llvm::ConstantVector::get(&ExtMask[0],
Nate Begemanb699c9b2009-01-18 06:42:49 +0000712 ExtMask.size());
Mike Stump4a3999f2009-09-09 13:00:44 +0000713 llvm::Value *ExtSrcVal =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000714 Builder.CreateShuffleVector(SrcVal,
Owen Anderson7ec07a52009-07-30 23:11:26 +0000715 llvm::UndefValue::get(SrcVal->getType()),
Daniel Dunbar3d926cb2009-02-17 18:31:04 +0000716 ExtMaskV, "tmp");
Nate Begemanb699c9b2009-01-18 06:42:49 +0000717 // build identity
718 llvm::SmallVector<llvm::Constant*, 4> Mask;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000719 for (unsigned i = 0; i != NumDstElts; ++i)
720 Mask.push_back(llvm::ConstantInt::get(Int32Ty, i));
721
Nate Begemanb699c9b2009-01-18 06:42:49 +0000722 // modify when what gets shuffled in
723 for (unsigned i = 0; i != NumSrcElts; ++i) {
724 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000725 Mask[Idx] = llvm::ConstantInt::get(Int32Ty, i+NumDstElts);
Nate Begemanb699c9b2009-01-18 06:42:49 +0000726 }
Owen Anderson3cc120a2009-07-28 21:22:35 +0000727 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
Nate Begemanb699c9b2009-01-18 06:42:49 +0000728 Vec = Builder.CreateShuffleVector(Vec, ExtSrcVal, MaskV, "tmp");
Mike Stump658fe022009-07-30 22:28:39 +0000729 } else {
Nate Begemanb699c9b2009-01-18 06:42:49 +0000730 // We should never shorten the vector
731 assert(0 && "unexpected shorten vector length");
Chris Lattner3a44aa72007-08-03 16:37:04 +0000732 }
733 } else {
734 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman75d69da2008-05-22 00:50:06 +0000735 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000736 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
737 llvm::Value *Elt = llvm::ConstantInt::get(Int32Ty, InIdx);
Chris Lattner41d480e2007-08-03 16:28:33 +0000738 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner41d480e2007-08-03 16:28:33 +0000739 }
Mike Stump4a3999f2009-09-09 13:00:44 +0000740
Eli Friedman327944b2008-06-13 23:01:12 +0000741 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner41d480e2007-08-03 16:28:33 +0000742}
743
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000744// setObjCGCLValueClass - sets class of he lvalue for the purpose of
745// generating write-barries API. It is currently a global, ivar,
746// or neither.
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000747static void setObjCGCLValueClass(const ASTContext &Ctx, const Expr *E,
748 LValue &LV) {
Fariborz Jahanian71848a32009-09-21 23:03:37 +0000749 if (Ctx.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000750 return;
751
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000752 if (isa<ObjCIvarRefExpr>(E)) {
753 LV.SetObjCIvar(LV, true);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000754 ObjCIvarRefExpr *Exp = cast<ObjCIvarRefExpr>(const_cast<Expr*>(E));
755 LV.setBaseIvarExp(Exp->getBase());
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000756 LV.SetObjCArray(LV, E->getType()->isArrayType());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +0000757 return;
758 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000759
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000760 if (const DeclRefExpr *Exp = dyn_cast<DeclRefExpr>(E)) {
761 if (const VarDecl *VD = dyn_cast<VarDecl>(Exp->getDecl())) {
762 if ((VD->isBlockVarDecl() && !VD->hasLocalStorage()) ||
763 VD->isFileVarDecl())
764 LV.SetGlobalObjCRef(LV, true);
765 }
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000766 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000767 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000768 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000769
770 if (const UnaryOperator *Exp = dyn_cast<UnaryOperator>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000771 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000772 return;
773 }
774
775 if (const ParenExpr *Exp = dyn_cast<ParenExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000776 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000777 if (LV.isObjCIvar()) {
778 // If cast is to a structure pointer, follow gcc's behavior and make it
779 // a non-ivar write-barrier.
780 QualType ExpTy = E->getType();
781 if (ExpTy->isPointerType())
782 ExpTy = ExpTy->getAs<PointerType>()->getPointeeType();
783 if (ExpTy->isRecordType())
784 LV.SetObjCIvar(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000785 }
786 return;
Fariborz Jahaniane01e4342009-09-30 17:10:29 +0000787 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000788 if (const ImplicitCastExpr *Exp = dyn_cast<ImplicitCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000789 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000790 return;
791 }
792
793 if (const CStyleCastExpr *Exp = dyn_cast<CStyleCastExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000794 setObjCGCLValueClass(Ctx, Exp->getSubExpr(), LV);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000795 return;
796 }
797
798 if (const ArraySubscriptExpr *Exp = dyn_cast<ArraySubscriptExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000799 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000800 if (LV.isObjCIvar() && !LV.isObjCArray())
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000801 // Using array syntax to assigning to what an ivar points to is not
802 // same as assigning to the ivar itself. {id *Names;} Names[i] = 0;
803 LV.SetObjCIvar(LV, false);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000804 else if (LV.isGlobalObjCRef() && !LV.isObjCArray())
805 // Using array syntax to assigning to what global points to is not
806 // same as assigning to the global itself. {id *G;} G[i] = 0;
807 LV.SetGlobalObjCRef(LV, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000808 return;
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000809 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000810
811 if (const MemberExpr *Exp = dyn_cast<MemberExpr>(E)) {
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000812 setObjCGCLValueClass(Ctx, Exp->getBase(), LV);
Fariborz Jahanian2e32ddc2009-09-18 00:04:00 +0000813 // We don't know if member is an 'ivar', but this flag is looked at
814 // only in the context of LV.isObjCIvar().
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +0000815 LV.SetObjCArray(LV, E->getType()->isArrayType());
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000816 return;
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000817 }
818}
819
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000820static LValue EmitGlobalVarDeclLValue(CodeGenFunction &CGF,
821 const Expr *E, const VarDecl *VD) {
Daniel Dunbar7e215ea2009-11-08 09:46:46 +0000822 assert((VD->hasExternalStorage() || VD->isFileVarDecl()) &&
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000823 "Var decl must have external storage or be a file var decl!");
824
825 llvm::Value *V = CGF.CGM.GetAddrOfGlobalVar(VD);
826 if (VD->getType()->isReferenceType())
827 V = CGF.Builder.CreateLoad(V, "tmp");
828 LValue LV = LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
829 setObjCGCLValueClass(CGF.getContext(), E, LV);
830 return LV;
831}
832
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000833static LValue EmitFunctionDeclLValue(CodeGenFunction &CGF,
834 const Expr *E, const FunctionDecl *FD) {
835 llvm::Value* V = CGF.CGM.GetAddrOfFunction(FD);
836 if (!FD->hasPrototype()) {
837 if (const FunctionProtoType *Proto =
838 FD->getType()->getAs<FunctionProtoType>()) {
839 // Ugly case: for a K&R-style definition, the type of the definition
840 // isn't the same as the type of a use. Correct for this with a
841 // bitcast.
842 QualType NoProtoType =
843 CGF.getContext().getFunctionNoProtoType(Proto->getResultType());
844 NoProtoType = CGF.getContext().getPointerType(NoProtoType);
845 V = CGF.Builder.CreateBitCast(V, CGF.ConvertType(NoProtoType), "tmp");
846 }
847 }
848 return LValue::MakeAddr(V, CGF.MakeQualifiers(E->getType()));
849}
850
Chris Lattnerd7f58862007-06-02 05:24:33 +0000851LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000852 const NamedDecl *ND = E->getDecl();
Mike Stump4a3999f2009-09-09 13:00:44 +0000853
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000854 if (const VarDecl *VD = dyn_cast<VarDecl>(ND)) {
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000855
856 // Check if this is a global variable.
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000857 if (VD->hasExternalStorage() || VD->isFileVarDecl())
858 return EmitGlobalVarDeclLValue(*this, E, VD);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000859
860 bool NonGCable = VD->hasLocalStorage() && !VD->hasAttr<BlocksAttr>();
861
862 llvm::Value *V = LocalDeclMap[VD];
863 assert(V && "DeclRefExpr not entered in LocalDeclMap?");
864
865 Qualifiers Quals = MakeQualifiers(E->getType());
866 // local variables do not get their gc attribute set.
867 // local static?
868 if (NonGCable) Quals.removeObjCGCAttr();
869
870 if (VD->hasAttr<BlocksAttr>()) {
871 V = Builder.CreateStructGEP(V, 1, "forwarding");
Daniel Dunbarc76493a2009-11-29 21:23:36 +0000872 V = Builder.CreateLoad(V);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000873 V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
874 VD->getNameAsString());
875 }
876 if (VD->getType()->isReferenceType())
877 V = Builder.CreateLoad(V, "tmp");
Anders Carlssonea4c30b2009-11-07 23:06:58 +0000878 LValue LV = LValue::MakeAddr(V, Quals);
Anders Carlsson6eee9722009-11-07 22:46:42 +0000879 LValue::SetObjCNonGC(LV, NonGCable);
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +0000880 setObjCGCLValueClass(getContext(), E, LV);
Fariborz Jahanian003e8302008-11-20 00:15:42 +0000881 return LV;
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000882 }
883
Eli Friedmand15eb34d2009-11-26 06:08:14 +0000884 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
885 return EmitFunctionDeclLValue(*this, E, FD);
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000886
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000887 if (E->getQualifier()) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000888 // FIXME: the qualifier check does not seem sufficient here
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000889 return EmitPointerToDataMemberLValue(cast<FieldDecl>(ND));
Chris Lattner5696e7b2008-06-17 18:05:57 +0000890 }
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000891
Anders Carlsson2ff6395d2009-11-07 22:53:10 +0000892 assert(false && "Unhandled DeclRefExpr");
893
894 // an invalid LValue, but the assert will
895 // ensure that this point is never reached.
Chris Lattner793d10c2007-09-16 19:23:47 +0000896 return LValue();
Chris Lattnerd7f58862007-06-02 05:24:33 +0000897}
Chris Lattnere47e4402007-06-01 18:02:12 +0000898
Mike Stump1db7d042009-02-28 09:07:16 +0000899LValue CodeGenFunction::EmitBlockDeclRefLValue(const BlockDeclRefExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000900 return LValue::MakeAddr(GetAddrOfBlockDecl(E), MakeQualifiers(E->getType()));
Mike Stump1db7d042009-02-28 09:07:16 +0000901}
902
Chris Lattner8394d792007-06-05 20:53:16 +0000903LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
904 // __extension__ doesn't affect lvalue-ness.
905 if (E->getOpcode() == UnaryOperator::Extension)
906 return EmitLValue(E->getSubExpr());
Mike Stump4a3999f2009-09-09 13:00:44 +0000907
Chris Lattner0f398c42008-07-26 22:37:01 +0000908 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner595db862007-10-30 22:53:42 +0000909 switch (E->getOpcode()) {
910 default: assert(0 && "Unknown unary operator lvalue!");
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000911 case UnaryOperator::Deref: {
912 QualType T = E->getSubExpr()->getType()->getPointeeType();
913 assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
Mike Stump4a3999f2009-09-09 13:00:44 +0000914
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000915 Qualifiers Quals = MakeQualifiers(T);
916 Quals.setAddressSpace(ExprTy.getAddressSpace());
John McCall8ccfcb52009-09-24 19:53:00 +0000917
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000918 LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()), Quals);
919 // We should not generate __weak write barrier on indirect reference
920 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
921 // But, we continue to generate __strong write barrier on indirect write
922 // into a pointer to object.
923 if (getContext().getLangOptions().ObjC1 &&
924 getContext().getLangOptions().getGCMode() != LangOptions::NonGC &&
925 LV.isObjCWeak())
926 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
927 return LV;
928 }
Chris Lattner595db862007-10-30 22:53:42 +0000929 case UnaryOperator::Real:
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000930 case UnaryOperator::Imag: {
Chris Lattner595db862007-10-30 22:53:42 +0000931 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner3e593cd2008-03-19 05:19:41 +0000932 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
933 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattner574dee62008-07-26 22:17:49 +0000934 Idx, "idx"),
John McCall8ccfcb52009-09-24 19:53:00 +0000935 MakeQualifiers(ExprTy));
Chris Lattner595db862007-10-30 22:53:42 +0000936 }
Eli Friedmana72bf0f2009-11-09 04:20:47 +0000937 case UnaryOperator::PreInc:
938 case UnaryOperator::PreDec:
939 return EmitUnsupportedLValue(E, "pre-inc/dec expression");
940 }
Chris Lattner8394d792007-06-05 20:53:16 +0000941}
942
Chris Lattner4347e3692007-06-06 04:54:52 +0000943LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000944 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E),
945 Qualifiers());
Chris Lattner4347e3692007-06-06 04:54:52 +0000946}
947
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000948LValue CodeGenFunction::EmitObjCEncodeExprLValue(const ObjCEncodeExpr *E) {
John McCall8ccfcb52009-09-24 19:53:00 +0000949 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromObjCEncode(E),
950 Qualifiers());
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000951}
952
953
Daniel Dunbarb3517472008-10-17 21:58:32 +0000954LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson625bfc82007-07-21 05:21:51 +0000955 std::string GlobalVarName;
Daniel Dunbarb3517472008-10-17 21:58:32 +0000956
957 switch (Type) {
Chris Lattnerab5e0af2009-10-28 17:39:19 +0000958 default: assert(0 && "Invalid type");
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000959 case PredefinedExpr::Func:
960 GlobalVarName = "__func__.";
961 break;
962 case PredefinedExpr::Function:
963 GlobalVarName = "__FUNCTION__.";
964 break;
965 case PredefinedExpr::PrettyFunction:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +0000966 GlobalVarName = "__PRETTY_FUNCTION__.";
967 break;
Anders Carlsson625bfc82007-07-21 05:21:51 +0000968 }
Daniel Dunbarb3517472008-10-17 21:58:32 +0000969
Daniel Dunbar0482cfd2009-09-12 23:06:21 +0000970 llvm::StringRef FnName = CurFn->getName();
971 if (FnName.startswith("\01"))
972 FnName = FnName.substr(1);
973 GlobalVarName += FnName;
974
Anders Carlsson2fb08242009-09-08 18:24:21 +0000975 std::string FunctionName =
Mike Stump11289f42009-09-09 15:08:12 +0000976 PredefinedExpr::ComputeName(getContext(), (PredefinedExpr::IdentType)Type,
Anders Carlsson2fb08242009-09-08 18:24:21 +0000977 CurCodeDecl);
Daniel Dunbarb3517472008-10-17 21:58:32 +0000978
Mike Stump4a3999f2009-09-09 13:00:44 +0000979 llvm::Constant *C =
Daniel Dunbarb3517472008-10-17 21:58:32 +0000980 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
John McCall8ccfcb52009-09-24 19:53:00 +0000981 return LValue::MakeAddr(C, Qualifiers());
Daniel Dunbarb3517472008-10-17 21:58:32 +0000982}
983
Mike Stump4a3999f2009-09-09 13:00:44 +0000984LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
Daniel Dunbarb3517472008-10-17 21:58:32 +0000985 switch (E->getIdentType()) {
986 default:
987 return EmitUnsupportedLValue(E, "predefined expression");
988 case PredefinedExpr::Func:
989 case PredefinedExpr::Function:
990 case PredefinedExpr::PrettyFunction:
991 return EmitPredefinedFunctionName(E->getIdentType());
992 }
Anders Carlsson625bfc82007-07-21 05:21:51 +0000993}
994
Chris Lattnerd9d2fb12007-06-08 23:31:14 +0000995LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenekc81614d2007-08-20 16:18:38 +0000996 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +0000997 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Eli Friedman07bbeca2009-06-06 19:09:26 +0000998 QualType IdxTy = E->getIdx()->getType();
999 bool IdxSigned = IdxTy->isSignedIntegerType();
1000
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001001 // If the base is a vector type, then we are forming a vector element lvalue
1002 // with this subscript.
Eli Friedman327944b2008-06-13 23:01:12 +00001003 if (E->getBase()->getType()->isVectorType()) {
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001004 // Emit the vector as an lvalue to get its address.
Eli Friedman327944b2008-06-13 23:01:12 +00001005 LValue LHS = EmitLValue(E->getBase());
Ted Kremenekc81614d2007-08-20 16:18:38 +00001006 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001007 Idx = Builder.CreateIntCast(Idx,
Owen Anderson41a75022009-08-13 21:57:51 +00001008 llvm::Type::getInt32Ty(VMContext), IdxSigned, "vidx");
Eli Friedman327944b2008-06-13 23:01:12 +00001009 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
John McCall8ccfcb52009-09-24 19:53:00 +00001010 E->getBase()->getType().getCVRQualifiers());
Chris Lattner08c4b9f2007-07-10 21:17:59 +00001011 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001012
Ted Kremenekc81614d2007-08-20 16:18:38 +00001013 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner2da04b32007-08-24 05:35:26 +00001014 llvm::Value *Base = EmitScalarExpr(E->getBase());
Mike Stump4a3999f2009-09-09 13:00:44 +00001015
Ted Kremenekc81614d2007-08-20 16:18:38 +00001016 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattner23b7eb62007-06-15 23:05:46 +00001017 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
Sanjiv Gupta47425152009-04-24 02:40:57 +00001018 if (IdxBitwidth != LLVMPointerWidth)
Owen Anderson41a75022009-08-13 21:57:51 +00001019 Idx = Builder.CreateIntCast(Idx,
1020 llvm::IntegerType::get(VMContext, LLVMPointerWidth),
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001021 IdxSigned, "idxprom");
1022
Mike Stump4a3999f2009-09-09 13:00:44 +00001023 // We know that the pointer points to a type of the correct size, unless the
1024 // size is a VLA or Objective-C interface.
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001025 llvm::Value *Address = 0;
Mike Stump4a3999f2009-09-09 13:00:44 +00001026 if (const VariableArrayType *VAT =
Anders Carlsson3d312f82008-12-21 00:11:23 +00001027 getContext().getAsVariableArrayType(E->getType())) {
Chris Lattner19efdd62009-08-14 23:43:22 +00001028 llvm::Value *VLASize = GetVLASize(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001029
Anders Carlsson3d312f82008-12-21 00:11:23 +00001030 Idx = Builder.CreateMul(Idx, VLASize);
Mike Stump4a3999f2009-09-09 13:00:44 +00001031
Anders Carlssone0808df2008-12-21 03:44:36 +00001032 QualType BaseType = getContext().getBaseElementType(VAT);
Mike Stump4a3999f2009-09-09 13:00:44 +00001033
Anders Carlsson3d312f82008-12-21 00:11:23 +00001034 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
1035 Idx = Builder.CreateUDiv(Idx,
Mike Stump4a3999f2009-09-09 13:00:44 +00001036 llvm::ConstantInt::get(Idx->getType(),
Anders Carlsson3d312f82008-12-21 00:11:23 +00001037 BaseTypeSize));
Dan Gohman43b44842009-08-12 00:33:55 +00001038 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Mike Stump4a3999f2009-09-09 13:00:44 +00001039 } else if (const ObjCInterfaceType *OIT =
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001040 dyn_cast<ObjCInterfaceType>(E->getType())) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001041 llvm::Value *InterfaceSize =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001042 llvm::ConstantInt::get(Idx->getType(),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001043 getContext().getTypeSize(OIT) / 8);
Mike Stump4a3999f2009-09-09 13:00:44 +00001044
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001045 Idx = Builder.CreateMul(Idx, InterfaceSize);
1046
Benjamin Kramerabd5b902009-10-13 10:07:13 +00001047 const llvm::Type *i8PTy = llvm::Type::getInt8PtrTy(VMContext);
Dan Gohman43b44842009-08-12 00:33:55 +00001048 Address = Builder.CreateGEP(Builder.CreateBitCast(Base, i8PTy),
Daniel Dunbaref2ffbc2009-04-25 05:08:32 +00001049 Idx, "arrayidx");
1050 Address = Builder.CreateBitCast(Address, Base->getType());
1051 } else {
Dan Gohman43b44842009-08-12 00:33:55 +00001052 Address = Builder.CreateInBoundsGEP(Base, Idx, "arrayidx");
Anders Carlsson3d312f82008-12-21 00:11:23 +00001053 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001054
Steve Naroff7cae42b2009-07-10 23:34:53 +00001055 QualType T = E->getBase()->getType()->getPointeeType();
Mike Stump4a3999f2009-09-09 13:00:44 +00001056 assert(!T.isNull() &&
Steve Naroff7cae42b2009-07-10 23:34:53 +00001057 "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
Mike Stump4a3999f2009-09-09 13:00:44 +00001058
John McCall8ccfcb52009-09-24 19:53:00 +00001059 Qualifiers Quals = MakeQualifiers(T);
1060 Quals.setAddressSpace(E->getBase()->getType().getAddressSpace());
1061
1062 LValue LV = LValue::MakeAddr(Address, Quals);
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001063 if (getContext().getLangOptions().ObjC1 &&
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001064 getContext().getLangOptions().getGCMode() != LangOptions::NonGC) {
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001065 LValue::SetObjCNonGC(LV, !E->isOBJCGCCandidate(getContext()));
Fariborz Jahaniana7fa6be2009-09-16 21:37:16 +00001066 setObjCGCLValueClass(getContext(), E, LV);
1067 }
Fariborz Jahaniana9fecf32009-02-21 23:37:19 +00001068 return LV;
Chris Lattnerd9d2fb12007-06-08 23:31:14 +00001069}
1070
Mike Stump4a3999f2009-09-09 13:00:44 +00001071static
Owen Anderson170229f2009-07-14 23:10:40 +00001072llvm::Constant *GenerateConstantVector(llvm::LLVMContext &VMContext,
1073 llvm::SmallVector<unsigned, 4> &Elts) {
Nate Begemand3862152008-05-13 21:03:02 +00001074 llvm::SmallVector<llvm::Constant *, 4> CElts;
Mike Stump4a3999f2009-09-09 13:00:44 +00001075
Nate Begemand3862152008-05-13 21:03:02 +00001076 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
Owen Anderson41a75022009-08-13 21:57:51 +00001077 CElts.push_back(llvm::ConstantInt::get(
1078 llvm::Type::getInt32Ty(VMContext), Elts[i]));
Nate Begemand3862152008-05-13 21:03:02 +00001079
Owen Anderson3cc120a2009-07-28 21:22:35 +00001080 return llvm::ConstantVector::get(&CElts[0], CElts.size());
Nate Begemand3862152008-05-13 21:03:02 +00001081}
1082
Chris Lattner9e751ca2007-08-02 23:37:31 +00001083LValue CodeGenFunction::
Nate Begemance4d7fc2008-04-18 23:10:10 +00001084EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner9e751ca2007-08-02 23:37:31 +00001085 // Emit the base vector as an l-value.
Chris Lattner6c7ce102009-02-16 21:11:58 +00001086 LValue Base;
1087
1088 // ExtVectorElementExpr's base can either be a vector or pointer to vector.
Chris Lattnerb8211f62009-02-16 22:14:05 +00001089 if (!E->isArrow()) {
Chris Lattner6c7ce102009-02-16 21:11:58 +00001090 assert(E->getBase()->getType()->isVectorType());
1091 Base = EmitLValue(E->getBase());
Chris Lattnerb8211f62009-02-16 22:14:05 +00001092 } else {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001093 const PointerType *PT = E->getBase()->getType()->getAs<PointerType>();
Chris Lattnerb8211f62009-02-16 22:14:05 +00001094 llvm::Value *Ptr = EmitScalarExpr(E->getBase());
John McCall8ccfcb52009-09-24 19:53:00 +00001095 Qualifiers Quals = MakeQualifiers(PT->getPointeeType());
1096 Quals.removeObjCGCAttr();
1097 Base = LValue::MakeAddr(Ptr, Quals);
Chris Lattner6c7ce102009-02-16 21:11:58 +00001098 }
Chris Lattner9e751ca2007-08-02 23:37:31 +00001099
Nate Begemand3862152008-05-13 21:03:02 +00001100 // Encode the element access list into a vector of unsigned indices.
1101 llvm::SmallVector<unsigned, 4> Indices;
1102 E->getEncodedElementAccess(Indices);
1103
1104 if (Base.isSimple()) {
Owen Anderson170229f2009-07-14 23:10:40 +00001105 llvm::Constant *CV = GenerateConstantVector(VMContext, Indices);
Eli Friedman327944b2008-06-13 23:01:12 +00001106 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001107 Base.getVRQualifiers());
Nate Begemand3862152008-05-13 21:03:02 +00001108 }
1109 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
1110
1111 llvm::Constant *BaseElts = Base.getExtVectorElts();
1112 llvm::SmallVector<llvm::Constant *, 4> CElts;
1113
Chris Lattner5e71d432009-10-28 05:12:07 +00001114 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Nate Begemand3862152008-05-13 21:03:02 +00001115 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
1116 if (isa<llvm::ConstantAggregateZero>(BaseElts))
Chris Lattner5e71d432009-10-28 05:12:07 +00001117 CElts.push_back(llvm::ConstantInt::get(Int32Ty, 0));
Nate Begemand3862152008-05-13 21:03:02 +00001118 else
Chris Lattner5e71d432009-10-28 05:12:07 +00001119 CElts.push_back(cast<llvm::Constant>(BaseElts->getOperand(Indices[i])));
Nate Begemand3862152008-05-13 21:03:02 +00001120 }
Owen Anderson3cc120a2009-07-28 21:22:35 +00001121 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman327944b2008-06-13 23:01:12 +00001122 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
John McCall8ccfcb52009-09-24 19:53:00 +00001123 Base.getVRQualifiers());
Chris Lattner9e751ca2007-08-02 23:37:31 +00001124}
1125
Devang Patel30efa2e2007-10-23 20:28:39 +00001126LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelb37b12d2007-12-11 21:33:16 +00001127 bool isUnion = false;
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001128 bool isNonGC = false;
Devang Pateld68df202007-10-24 22:26:28 +00001129 Expr *BaseExpr = E->getBase();
Devang Pateld68df202007-10-24 22:26:28 +00001130 llvm::Value *BaseValue = NULL;
John McCall8ccfcb52009-09-24 19:53:00 +00001131 Qualifiers BaseQuals;
Eli Friedman327944b2008-06-13 23:01:12 +00001132
Chris Lattner4e4186b2007-12-02 18:52:07 +00001133 // 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 +00001134 if (E->isArrow()) {
Devang Patel7718d7a2007-10-26 18:15:21 +00001135 BaseValue = EmitScalarExpr(BaseExpr);
Mike Stump4a3999f2009-09-09 13:00:44 +00001136 const PointerType *PTy =
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001137 BaseExpr->getType()->getAs<PointerType>();
Devang Patelb37b12d2007-12-11 21:33:16 +00001138 if (PTy->getPointeeType()->isUnionType())
1139 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001140 BaseQuals = PTy->getPointeeType().getQualifiers();
Fariborz Jahanian1a504772009-09-01 17:02:21 +00001141 } else if (isa<ObjCPropertyRefExpr>(BaseExpr->IgnoreParens()) ||
1142 isa<ObjCImplicitSetterGetterRefExpr>(
1143 BaseExpr->IgnoreParens())) {
Fariborz Jahanian30e78642009-01-12 23:27:26 +00001144 RValue RV = EmitObjCPropertyGet(BaseExpr);
1145 BaseValue = RV.getAggregateAddr();
1146 if (BaseExpr->getType()->isUnionType())
1147 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001148 BaseQuals = BaseExpr->getType().getQualifiers();
Chris Lattnere084c012009-02-16 22:25:49 +00001149 } else {
Chris Lattner4e4186b2007-12-02 18:52:07 +00001150 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian10bec102009-02-21 00:30:43 +00001151 if (BaseLV.isNonGC())
1152 isNonGC = true;
Chris Lattner4e4186b2007-12-02 18:52:07 +00001153 // FIXME: this isn't right for bitfields.
1154 BaseValue = BaseLV.getAddress();
Fariborz Jahanian82e28742009-07-29 00:44:13 +00001155 QualType BaseTy = BaseExpr->getType();
1156 if (BaseTy->isUnionType())
Devang Patelb37b12d2007-12-11 21:33:16 +00001157 isUnion = true;
John McCall8ccfcb52009-09-24 19:53:00 +00001158 BaseQuals = BaseTy.getQualifiers();
Chris Lattner4e4186b2007-12-02 18:52:07 +00001159 }
Devang Patel30efa2e2007-10-23 20:28:39 +00001160
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001161 NamedDecl *ND = E->getMemberDecl();
1162 if (FieldDecl *Field = dyn_cast<FieldDecl>(ND)) {
1163 LValue LV = EmitLValueForField(BaseValue, Field, isUnion,
1164 BaseQuals.getCVRQualifiers());
1165 LValue::SetObjCNonGC(LV, isNonGC);
1166 setObjCGCLValueClass(getContext(), E, LV);
1167 return LV;
1168 }
1169
Anders Carlsson5bbdc9f2009-11-07 23:16:50 +00001170 if (VarDecl *VD = dyn_cast<VarDecl>(ND))
1171 return EmitGlobalVarDeclLValue(*this, E, VD);
Eli Friedmand15eb34d2009-11-26 06:08:14 +00001172
1173 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
1174 return EmitFunctionDeclLValue(*this, E, FD);
1175
Anders Carlssonea4c30b2009-11-07 23:06:58 +00001176 assert(false && "Unhandled member declaration!");
1177 return LValue();
Eli Friedmana62f3e12008-02-09 08:50:58 +00001178}
Devang Patel30efa2e2007-10-23 20:28:39 +00001179
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001180LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001181 const FieldDecl* Field,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001182 unsigned CVRQualifiers) {
Anders Carlsson8af896c2009-07-23 17:01:21 +00001183 CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field);
1184
Mike Stump18bb9282009-05-16 07:57:57 +00001185 // FIXME: CodeGenTypes should expose a method to get the appropriate type for
1186 // FieldTy (the appropriate type is ABI-dependent).
Mike Stump4a3999f2009-09-09 13:00:44 +00001187 const llvm::Type *FieldTy =
Daniel Dunbar3d926cb2009-02-17 18:31:04 +00001188 CGM.getTypes().ConvertTypeForMem(Field->getType());
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001189 const llvm::PointerType *BaseTy =
1190 cast<llvm::PointerType>(BaseValue->getType());
1191 unsigned AS = BaseTy->getAddressSpace();
1192 BaseValue = Builder.CreateBitCast(BaseValue,
Owen Anderson9793f0e2009-07-29 22:16:19 +00001193 llvm::PointerType::get(FieldTy, AS),
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001194 "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001195
1196 llvm::Value *Idx =
Owen Anderson41a75022009-08-13 21:57:51 +00001197 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Info.FieldNo);
Anders Carlsson8af896c2009-07-23 17:01:21 +00001198 llvm::Value *V = Builder.CreateGEP(BaseValue, Idx, "tmp");
Mike Stump4a3999f2009-09-09 13:00:44 +00001199
Anders Carlsson8af896c2009-07-23 17:01:21 +00001200 return LValue::MakeBitfield(V, Info.Start, Info.Size,
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001201 Field->getType()->isSignedIntegerType(),
1202 Field->getType().getCVRQualifiers()|CVRQualifiers);
1203}
1204
Eli Friedmana62f3e12008-02-09 08:50:58 +00001205LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
Anders Carlssoncfd30122009-11-17 03:57:07 +00001206 const FieldDecl* Field,
Eli Friedman327944b2008-06-13 23:01:12 +00001207 bool isUnion,
Mike Stump11289f42009-09-09 15:08:12 +00001208 unsigned CVRQualifiers) {
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001209 if (Field->isBitField())
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001210 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers);
Mike Stump4a3999f2009-09-09 13:00:44 +00001211
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001212 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Fariborz Jahanianb517e902008-12-15 20:35:07 +00001213 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman133e8042008-05-29 11:33:25 +00001214
Devang Pateled93c3c2007-10-26 19:42:18 +00001215 // Match union field type.
Lauro Ramos Venancio9eff02d2008-02-07 19:29:53 +00001216 if (isUnion) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001217 const llvm::Type *FieldTy =
Eli Friedman327944b2008-06-13 23:01:12 +00001218 CGM.getTypes().ConvertTypeForMem(Field->getType());
Mike Stump4a3999f2009-09-09 13:00:44 +00001219 const llvm::PointerType * BaseTy =
Devang Patelffe1e212007-10-30 20:59:40 +00001220 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001221 unsigned AS = BaseTy->getAddressSpace();
Mike Stump4a3999f2009-09-09 13:00:44 +00001222 V = Builder.CreateBitCast(V,
1223 llvm::PointerType::get(FieldTy, AS),
Eli Friedman9a5ffcb2008-05-21 13:24:44 +00001224 "tmp");
Devang Pateled93c3c2007-10-26 19:42:18 +00001225 }
Eli Friedmanf7f9f682009-05-30 21:09:44 +00001226 if (Field->getType()->isReferenceType())
1227 V = Builder.CreateLoad(V, "tmp");
John McCall8ccfcb52009-09-24 19:53:00 +00001228
1229 Qualifiers Quals = MakeQualifiers(Field->getType());
1230 Quals.addCVRQualifiers(CVRQualifiers);
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001231 // __weak attribute on a field is ignored.
John McCall8ccfcb52009-09-24 19:53:00 +00001232 if (Quals.getObjCGCAttr() == Qualifiers::Weak)
1233 Quals.removeObjCGCAttr();
Fariborz Jahanian38c3ae92009-09-21 18:54:29 +00001234
John McCall8ccfcb52009-09-24 19:53:00 +00001235 return LValue::MakeAddr(V, Quals);
Devang Patel30efa2e2007-10-23 20:28:39 +00001236}
1237
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001238LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){
Eli Friedman9fd8b682008-05-13 23:18:27 +00001239 const llvm::Type *LTy = ConvertType(E->getType());
1240 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
1241
1242 const Expr* InitExpr = E->getInitializer();
John McCall8ccfcb52009-09-24 19:53:00 +00001243 LValue Result = LValue::MakeAddr(DeclPtr, MakeQualifiers(E->getType()));
Eli Friedman9fd8b682008-05-13 23:18:27 +00001244
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001245 if (E->getType()->isComplexType())
Eli Friedman9fd8b682008-05-13 23:18:27 +00001246 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001247 else if (hasAggregateLLVMType(E->getType()))
Eli Friedman9fd8b682008-05-13 23:18:27 +00001248 EmitAnyExpr(InitExpr, DeclPtr, false);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001249 else
Eli Friedman9fd8b682008-05-13 23:18:27 +00001250 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
Eli Friedman9fd8b682008-05-13 23:18:27 +00001251
1252 return Result;
1253}
1254
Anders Carlsson1450adb2009-09-15 16:35:24 +00001255LValue
1256CodeGenFunction::EmitConditionalOperatorLValue(const ConditionalOperator* E) {
1257 if (E->isLvalue(getContext()) == Expr::LV_Valid) {
1258 llvm::BasicBlock *LHSBlock = createBasicBlock("cond.true");
1259 llvm::BasicBlock *RHSBlock = createBasicBlock("cond.false");
1260 llvm::BasicBlock *ContBlock = createBasicBlock("cond.end");
1261
1262 llvm::Value *Cond = EvaluateExprAsBool(E->getCond());
1263 Builder.CreateCondBr(Cond, LHSBlock, RHSBlock);
1264
1265 EmitBlock(LHSBlock);
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001266
Anders Carlsson1450adb2009-09-15 16:35:24 +00001267 LValue LHS = EmitLValue(E->getLHS());
1268 if (!LHS.isSimple())
1269 return EmitUnsupportedLValue(E, "conditional operator");
1270
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001271 llvm::Value *Temp = CreateTempAlloca(LHS.getAddress()->getType(),"condtmp");
Anders Carlsson1450adb2009-09-15 16:35:24 +00001272 Builder.CreateStore(LHS.getAddress(), Temp);
1273 EmitBranch(ContBlock);
1274
1275 EmitBlock(RHSBlock);
1276 LValue RHS = EmitLValue(E->getRHS());
1277 if (!RHS.isSimple())
1278 return EmitUnsupportedLValue(E, "conditional operator");
1279
1280 Builder.CreateStore(RHS.getAddress(), Temp);
1281 EmitBranch(ContBlock);
1282
1283 EmitBlock(ContBlock);
1284
1285 Temp = Builder.CreateLoad(Temp, "lv");
John McCall8ccfcb52009-09-24 19:53:00 +00001286 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson1450adb2009-09-15 16:35:24 +00001287 }
1288
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001289 // ?: here should be an aggregate.
Mike Stump4a3999f2009-09-09 13:00:44 +00001290 assert((hasAggregateLLVMType(E->getType()) &&
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001291 !E->getType()->isAnyComplexType()) &&
1292 "Unexpected conditional operator!");
1293
1294 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1295 EmitAggExpr(E, Temp, false);
1296
John McCall8ccfcb52009-09-24 19:53:00 +00001297 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbarbf1fe8c2009-03-24 02:38:23 +00001298}
1299
Mike Stump65511702009-11-16 06:50:58 +00001300/// EmitCastLValue - Casts are never lvalues unless that cast is a dynamic_cast.
1301/// If the cast is a dynamic_cast, we can have the usual lvalue result,
1302/// otherwise if a cast is needed by the code generator in an lvalue context,
1303/// then it must mean that we need the address of an aggregate in order to
1304/// access one of its fields. This can happen for all the reasons that casts
1305/// are permitted with aggregate result, including noop aggregate casts, and
1306/// cast from scalar to union.
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001307LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
Anders Carlssond95f9602009-09-12 16:16:49 +00001308 switch (E->getCastKind()) {
1309 default:
Eli Friedman8c98dff2009-11-16 05:48:01 +00001310 return EmitUnsupportedLValue(E, "unexpected cast lvalue");
1311
Mike Stump65511702009-11-16 06:50:58 +00001312 case CastExpr::CK_Dynamic: {
1313 LValue LV = EmitLValue(E->getSubExpr());
1314 llvm::Value *V = LV.getAddress();
1315 const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
1316 return LValue::MakeAddr(EmitDynamicCast(V, DCE),
1317 MakeQualifiers(E->getType()));
1318 }
1319
Anders Carlssond95f9602009-09-12 16:16:49 +00001320 case CastExpr::CK_NoOp:
1321 case CastExpr::CK_ConstructorConversion:
1322 case CastExpr::CK_UserDefinedConversion:
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001323 return EmitLValue(E->getSubExpr());
Anders Carlssond95f9602009-09-12 16:16:49 +00001324
1325 case CastExpr::CK_DerivedToBase: {
1326 const RecordType *DerivedClassTy =
1327 E->getSubExpr()->getType()->getAs<RecordType>();
1328 CXXRecordDecl *DerivedClassDecl =
1329 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001330
Anders Carlssond95f9602009-09-12 16:16:49 +00001331 const RecordType *BaseClassTy = E->getType()->getAs<RecordType>();
1332 CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
1333
1334 LValue LV = EmitLValue(E->getSubExpr());
1335
1336 // Perform the derived-to-base conversion
1337 llvm::Value *Base =
Anders Carlsson8c793172009-11-23 17:57:54 +00001338 GetAddressOfBaseClass(LV.getAddress(), DerivedClassDecl,
1339 BaseClassDecl, /*NullCheckValue=*/false);
Anders Carlssond95f9602009-09-12 16:16:49 +00001340
John McCall8ccfcb52009-09-24 19:53:00 +00001341 return LValue::MakeAddr(Base, MakeQualifiers(E->getType()));
Anders Carlssond95f9602009-09-12 16:16:49 +00001342 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001343 case CastExpr::CK_ToUnion: {
1344 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1345 EmitAnyExpr(E->getSubExpr(), Temp, false);
Mike Stump4a3999f2009-09-09 13:00:44 +00001346
John McCall8ccfcb52009-09-24 19:53:00 +00001347 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson50cb3212009-11-14 21:21:42 +00001348 }
Eli Friedman8c98dff2009-11-16 05:48:01 +00001349 case CastExpr::CK_BaseToDerived: {
Anders Carlsson8c793172009-11-23 17:57:54 +00001350 const RecordType *BaseClassTy =
1351 E->getSubExpr()->getType()->getAs<RecordType>();
1352 CXXRecordDecl *BaseClassDecl =
1353 cast<CXXRecordDecl>(BaseClassTy->getDecl());
1354
1355 const RecordType *DerivedClassTy = E->getType()->getAs<RecordType>();
1356 CXXRecordDecl *DerivedClassDecl =
1357 cast<CXXRecordDecl>(DerivedClassTy->getDecl());
1358
1359 LValue LV = EmitLValue(E->getSubExpr());
1360
1361 // Perform the base-to-derived conversion
1362 llvm::Value *Derived =
1363 GetAddressOfDerivedClass(LV.getAddress(), BaseClassDecl,
1364 DerivedClassDecl, /*NullCheckValue=*/false);
1365
1366 return LValue::MakeAddr(Derived, MakeQualifiers(E->getType()));
Eli Friedman8c98dff2009-11-16 05:48:01 +00001367 }
Anders Carlsson50cb3212009-11-14 21:21:42 +00001368 case CastExpr::CK_BitCast: {
Eli Friedman8c98dff2009-11-16 05:48:01 +00001369 // This must be a reinterpret_cast (or c-style equivalent).
1370 const ExplicitCastExpr *CE = cast<ExplicitCastExpr>(E);
Anders Carlsson50cb3212009-11-14 21:21:42 +00001371
1372 LValue LV = EmitLValue(E->getSubExpr());
1373 llvm::Value *V = Builder.CreateBitCast(LV.getAddress(),
1374 ConvertType(CE->getTypeAsWritten()));
1375 return LValue::MakeAddr(V, MakeQualifiers(E->getType()));
1376 }
Anders Carlssond95f9602009-09-12 16:16:49 +00001377 }
Chris Lattner28bcf1a2009-03-18 18:28:57 +00001378}
1379
Fariborz Jahaniane4d94ce2009-10-20 23:29:04 +00001380LValue CodeGenFunction::EmitNullInitializationLValue(
1381 const CXXZeroInitValueExpr *E) {
1382 QualType Ty = E->getType();
1383 const llvm::Type *LTy = ConvertTypeForMem(Ty);
1384 llvm::AllocaInst *Alloc = CreateTempAlloca(LTy);
1385 unsigned Align = getContext().getTypeAlign(Ty)/8;
1386 Alloc->setAlignment(Align);
1387 LValue lvalue = LValue::MakeAddr(Alloc, Qualifiers());
1388 EmitMemSetToZero(lvalue.getAddress(), Ty);
1389 return lvalue;
1390}
1391
Chris Lattnere47e4402007-06-01 18:02:12 +00001392//===--------------------------------------------------------------------===//
1393// Expression Emission
1394//===--------------------------------------------------------------------===//
1395
Chris Lattner76ba8492007-08-20 22:37:10 +00001396
Chris Lattner2b228c92007-06-15 21:34:29 +00001397RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001398 // Builtins never have block type.
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001399 if (E->getCallee()->getType()->isBlockPointerType())
Anders Carlsson2437cbf2009-02-12 00:39:25 +00001400 return EmitBlockCallExpr(E);
Daniel Dunbarbb197e42009-01-09 16:50:52 +00001401
Anders Carlssone5fd6f22009-04-03 22:50:24 +00001402 if (const CXXMemberCallExpr *CE = dyn_cast<CXXMemberCallExpr>(E))
1403 return EmitCXXMemberCallExpr(CE);
Mike Stump4a3999f2009-09-09 13:00:44 +00001404
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001405 const Decl *TargetDecl = 0;
Daniel Dunbar27032de2009-02-20 19:34:33 +00001406 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E->getCallee())) {
1407 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CE->getSubExpr())) {
1408 TargetDecl = DRE->getDecl();
1409 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(TargetDecl))
Douglas Gregor15fc9562009-09-12 00:22:50 +00001410 if (unsigned builtinID = FD->getBuiltinID())
Daniel Dunbar27032de2009-02-20 19:34:33 +00001411 return EmitBuiltinExpr(FD, builtinID, E);
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001412 }
1413 }
1414
Chris Lattner4ca97c32009-06-13 00:26:38 +00001415 if (const CXXOperatorCallExpr *CE = dyn_cast<CXXOperatorCallExpr>(E))
Anders Carlsson4034a952009-05-27 04:18:27 +00001416 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(TargetDecl))
1417 return EmitCXXOperatorMemberCallExpr(CE, MD);
Mike Stump4a3999f2009-09-09 13:00:44 +00001418
Eli Friedman8aaff692009-12-08 02:09:46 +00001419 if (isa<CXXPseudoDestructorExpr>(E->getCallee()->IgnoreParens())) {
Douglas Gregorad8a3362009-09-04 17:36:40 +00001420 // C++ [expr.pseudo]p1:
Mike Stump4a3999f2009-09-09 13:00:44 +00001421 // The result shall only be used as the operand for the function call
Douglas Gregorad8a3362009-09-04 17:36:40 +00001422 // operator (), and the result of such a call has type void. The only
1423 // effect is the evaluation of the postfix-expression before the dot or
1424 // arrow.
1425 EmitScalarExpr(E->getCallee());
1426 return RValue::get(0);
1427 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001428
Chris Lattner2da04b32007-08-24 05:35:26 +00001429 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001430 return EmitCall(Callee, E->getCallee()->getType(),
1431 E->arg_begin(), E->arg_end(), TargetDecl);
Chris Lattner9e47ead2007-08-31 04:44:06 +00001432}
1433
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001434LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
Chris Lattnere541ea32009-05-12 21:28:12 +00001435 // Comma expressions just emit their LHS then their RHS as an l-value.
1436 if (E->getOpcode() == BinaryOperator::Comma) {
1437 EmitAnyExpr(E->getLHS());
Eli Friedman5445f6e2009-12-07 20:18:11 +00001438 EnsureInsertPoint();
Chris Lattnere541ea32009-05-12 21:28:12 +00001439 return EmitLValue(E->getRHS());
1440 }
Mike Stump4a3999f2009-09-09 13:00:44 +00001441
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001442 if (E->getOpcode() == BinaryOperator::PtrMemD ||
1443 E->getOpcode() == BinaryOperator::PtrMemI)
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001444 return EmitPointerToDataMemberBinaryExpr(E);
1445
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001446 // Can only get l-value for binary operator expressions which are a
1447 // simple assignment of aggregate type.
1448 if (E->getOpcode() != BinaryOperator::Assign)
1449 return EmitUnsupportedLValue(E, "binary l-value expression");
1450
Anders Carlsson0999aaf2009-10-19 18:28:22 +00001451 if (!hasAggregateLLVMType(E->getType())) {
1452 // Emit the LHS as an l-value.
1453 LValue LV = EmitLValue(E->getLHS());
1454
1455 llvm::Value *RHS = EmitScalarExpr(E->getRHS());
1456 EmitStoreOfScalar(RHS, LV.getAddress(), LV.isVolatileQualified(),
1457 E->getType());
1458 return LV;
1459 }
1460
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001461 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1462 EmitAggExpr(E, Temp, false);
1463 // FIXME: Are these qualifiers correct?
John McCall8ccfcb52009-09-24 19:53:00 +00001464 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8cde00a2008-09-04 03:20:13 +00001465}
1466
Christopher Lambd91c3d42007-12-29 05:02:41 +00001467LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
Christopher Lambd91c3d42007-12-29 05:02:41 +00001468 RValue RV = EmitCallExpr(E);
Anders Carlsson4ae70ff2009-05-27 01:45:47 +00001469
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001470 if (!RV.isScalar())
1471 return LValue::MakeAddr(RV.getAggregateAddr(),MakeQualifiers(E->getType()));
1472
1473 assert(E->getCallReturnType()->isReferenceType() &&
1474 "Can't have a scalar return unless the return type is a "
1475 "reference type!");
Mike Stump4a3999f2009-09-09 13:00:44 +00001476
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001477 return LValue::MakeAddr(RV.getScalarVal(), MakeQualifiers(E->getType()));
Christopher Lambd91c3d42007-12-29 05:02:41 +00001478}
1479
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001480LValue CodeGenFunction::EmitVAArgExprLValue(const VAArgExpr *E) {
1481 // FIXME: This shouldn't require another copy.
1482 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
1483 EmitAggExpr(E, Temp, false);
John McCall8ccfcb52009-09-24 19:53:00 +00001484 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Daniel Dunbar8d9dc4a2009-02-11 20:59:32 +00001485}
1486
Anders Carlsson3be22e22009-05-30 23:23:33 +00001487LValue CodeGenFunction::EmitCXXConstructLValue(const CXXConstructExpr *E) {
1488 llvm::Value *Temp = CreateTempAlloca(ConvertTypeForMem(E->getType()), "tmp");
1489 EmitCXXConstructExpr(Temp, E);
John McCall8ccfcb52009-09-24 19:53:00 +00001490 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
Anders Carlsson3be22e22009-05-30 23:23:33 +00001491}
1492
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001493LValue
Mike Stumpc9b231c2009-11-15 08:09:41 +00001494CodeGenFunction::EmitCXXTypeidLValue(const CXXTypeidExpr *E) {
1495 llvm::Value *Temp = EmitCXXTypeidExpr(E);
1496 return LValue::MakeAddr(Temp, MakeQualifiers(E->getType()));
1497}
1498
1499LValue
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001500CodeGenFunction::EmitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *E) {
1501 LValue LV = EmitLValue(E->getSubExpr());
Anders Carlsson8eb93e72009-05-31 00:34:10 +00001502 PushCXXTemporary(E->getTemporary(), LV.getAddress());
Anders Carlssonfd2af0c2009-05-30 23:30:54 +00001503 return LV;
1504}
1505
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001506LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
1507 // Can only get l-value for message expression returning aggregate type
1508 RValue RV = EmitObjCMessageExpr(E);
1509 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001510 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Daniel Dunbarc8317a42008-08-23 10:51:21 +00001511}
1512
Daniel Dunbar722f4242009-04-22 05:08:15 +00001513llvm::Value *CodeGenFunction::EmitIvarOffset(const ObjCInterfaceDecl *Interface,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001514 const ObjCIvarDecl *Ivar) {
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001515 return CGM.getObjCRuntime().EmitIvarOffset(*this, Interface, Ivar);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001516}
1517
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001518LValue CodeGenFunction::EmitLValueForIvar(QualType ObjectTy,
1519 llvm::Value *BaseValue,
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001520 const ObjCIvarDecl *Ivar,
1521 unsigned CVRQualifiers) {
Chris Lattnerc4688d22009-04-17 17:44:48 +00001522 return CGM.getObjCRuntime().EmitObjCValueForIvar(*this, ObjectTy, BaseValue,
Daniel Dunbar9ebf9512009-04-21 01:19:28 +00001523 Ivar, CVRQualifiers);
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001524}
1525
1526LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001527 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1528 llvm::Value *BaseValue = 0;
1529 const Expr *BaseExpr = E->getBase();
John McCall8ccfcb52009-09-24 19:53:00 +00001530 Qualifiers BaseQuals;
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001531 QualType ObjectTy;
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001532 if (E->isArrow()) {
1533 BaseValue = EmitScalarExpr(BaseExpr);
Steve Naroff7cae42b2009-07-10 23:34:53 +00001534 ObjectTy = BaseExpr->getType()->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00001535 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001536 } else {
1537 LValue BaseLV = EmitLValue(BaseExpr);
1538 // FIXME: this isn't right for bitfields.
1539 BaseValue = BaseLV.getAddress();
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00001540 ObjectTy = BaseExpr->getType();
John McCall8ccfcb52009-09-24 19:53:00 +00001541 BaseQuals = ObjectTy.getQualifiers();
Anders Carlssonc13b85a2008-08-25 01:53:23 +00001542 }
Daniel Dunbar1c64e5d2008-09-24 04:00:38 +00001543
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001544 LValue LV =
John McCall8ccfcb52009-09-24 19:53:00 +00001545 EmitLValueForIvar(ObjectTy, BaseValue, E->getDecl(),
1546 BaseQuals.getCVRQualifiers());
Fariborz Jahaniande1d3242009-09-16 23:11:23 +00001547 setObjCGCLValueClass(getContext(), E, LV);
1548 return LV;
Chris Lattner4bd55962008-03-30 23:03:07 +00001549}
1550
Mike Stump4a3999f2009-09-09 13:00:44 +00001551LValue
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001552CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001553 // This is a special l-value that just issues sends when we load or store
1554 // through it.
Daniel Dunbar9e22c0d2008-08-29 08:11:39 +00001555 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1556}
1557
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001558LValue CodeGenFunction::EmitObjCKVCRefLValue(
Fariborz Jahanian9a846652009-08-20 17:02:02 +00001559 const ObjCImplicitSetterGetterRefExpr *E) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001560 // This is a special l-value that just issues sends when we load or store
1561 // through it.
Fariborz Jahanian9ac53512008-11-22 22:30:21 +00001562 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1563}
1564
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001565LValue CodeGenFunction::EmitObjCSuperExprLValue(const ObjCSuperExpr *E) {
Douglas Gregor8ea1f532008-11-04 14:56:14 +00001566 return EmitUnsupportedLValue(E, "use of super");
1567}
1568
Chris Lattnera4185c52009-04-25 19:35:26 +00001569LValue CodeGenFunction::EmitStmtExprLValue(const StmtExpr *E) {
Chris Lattnera4185c52009-04-25 19:35:26 +00001570 // Can only get l-value for message expression returning aggregate type
1571 RValue RV = EmitAnyExprToTemp(E);
1572 // FIXME: can this be volatile?
John McCall8ccfcb52009-09-24 19:53:00 +00001573 return LValue::MakeAddr(RV.getAggregateAddr(), MakeQualifiers(E->getType()));
Chris Lattnera4185c52009-04-25 19:35:26 +00001574}
1575
1576
Anders Carlsson509850e2009-11-07 22:00:15 +00001577LValue CodeGenFunction::EmitPointerToDataMemberLValue(const FieldDecl *Field) {
Fariborz Jahanianb25817a2009-10-21 21:01:47 +00001578 const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(Field->getDeclContext());
1579 QualType NNSpecTy =
1580 getContext().getCanonicalType(
1581 getContext().getTypeDeclType(const_cast<CXXRecordDecl*>(ClassDecl)));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001582 NNSpecTy = getContext().getPointerType(NNSpecTy);
1583 llvm::Value *V = llvm::Constant::getNullValue(ConvertType(NNSpecTy));
Anders Carlssoncfd30122009-11-17 03:57:07 +00001584 LValue MemExpLV = EmitLValueForField(V, Field, /*isUnion=*/false,
1585 /*Qualifiers=*/0);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001586 const llvm::Type *ResultType = ConvertType(getContext().getPointerDiffType());
1587 V = Builder.CreatePtrToInt(MemExpLV.getAddress(), ResultType, "datamember");
Anders Carlsson509850e2009-11-07 22:00:15 +00001588 return LValue::MakeAddr(V, MakeQualifiers(Field->getType()));
Fariborz Jahanian4ebdff52009-10-21 18:38:00 +00001589}
1590
Mike Stump4a3999f2009-09-09 13:00:44 +00001591RValue CodeGenFunction::EmitCall(llvm::Value *Callee, QualType CalleeType,
Anders Carlsson3a9463b2009-05-27 01:22:39 +00001592 CallExpr::const_arg_iterator ArgBeg,
1593 CallExpr::const_arg_iterator ArgEnd,
1594 const Decl *TargetDecl) {
Mike Stump4a3999f2009-09-09 13:00:44 +00001595 // Get the actual function type. The callee type will always be a pointer to
1596 // function type or a block pointer type.
1597 assert(CalleeType->isFunctionPointerType() &&
Anders Carlssond8db8532009-04-07 18:53:02 +00001598 "Call must have function pointer type!");
1599
John McCall6fd4c232009-10-23 08:22:42 +00001600 CalleeType = getContext().getCanonicalType(CalleeType);
1601
1602 QualType FnType = cast<PointerType>(CalleeType)->getPointeeType();
1603 QualType ResultType = cast<FunctionType>(FnType)->getResultType();
Daniel Dunbarc722b852008-08-30 03:02:31 +00001604
1605 CallArgList Args;
John McCall6fd4c232009-10-23 08:22:42 +00001606 EmitCallArgs(Args, dyn_cast<FunctionProtoType>(FnType), ArgBeg, ArgEnd);
Daniel Dunbarc722b852008-08-30 03:02:31 +00001607
Daniel Dunbarbbaeca42009-09-11 22:25:00 +00001608 // FIXME: We should not need to do this, it should be part of the function
1609 // type.
1610 unsigned CallingConvention = 0;
1611 if (const llvm::Function *F =
1612 dyn_cast<llvm::Function>(Callee->stripPointerCasts()))
1613 CallingConvention = F->getCallingConv();
1614 return EmitCall(CGM.getTypes().getFunctionInfo(ResultType, Args,
1615 CallingConvention),
Daniel Dunbarcdbb5e32009-02-20 18:06:48 +00001616 Callee, Args, TargetDecl);
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001617}
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001618
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001619LValue CodeGenFunction::
1620EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E) {
Eli Friedman928a5672009-11-18 05:01:17 +00001621 llvm::Value *BaseV;
Fariborz Jahanian038374f2009-10-26 21:58:25 +00001622 if (E->getOpcode() == BinaryOperator::PtrMemI)
Eli Friedman928a5672009-11-18 05:01:17 +00001623 BaseV = EmitScalarExpr(E->getLHS());
1624 else
1625 BaseV = EmitLValue(E->getLHS()).getAddress();
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001626 const llvm::Type *i8Ty = llvm::Type::getInt8PtrTy(getLLVMContext());
1627 BaseV = Builder.CreateBitCast(BaseV, i8Ty);
Eli Friedman928a5672009-11-18 05:01:17 +00001628 llvm::Value *OffsetV = EmitScalarExpr(E->getRHS());
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001629 llvm::Value *AddV = Builder.CreateInBoundsGEP(BaseV, OffsetV, "add.ptr");
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001630
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001631 QualType Ty = E->getRHS()->getType();
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001632 Ty = Ty->getAs<MemberPointerType>()->getPointeeType();
1633
1634 const llvm::Type *PType = ConvertType(getContext().getPointerType(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001635 AddV = Builder.CreateBitCast(AddV, PType);
Chris Lattnerab5e0af2009-10-28 17:39:19 +00001636 return LValue::MakeAddr(AddV, MakeQualifiers(Ty));
Fariborz Jahanianffba6622009-10-22 22:57:31 +00001637}
1638