blob: 20a7693faf620c4109355957f0c2d3bd6b4861f0 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- CGExpr.cpp - Emit LLVM Code from Expressions ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbar0dbe2272008-09-08 21:33:45 +000016#include "CGCall.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000017#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000019#include "clang/AST/DeclObjC.h"
Eli Friedman316bb1b2008-05-17 20:03:47 +000020#include "llvm/Target/TargetData.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22using namespace CodeGen;
23
24//===--------------------------------------------------------------------===//
25// Miscellaneous Helper Methods
26//===--------------------------------------------------------------------===//
27
28/// CreateTempAlloca - This creates a alloca and inserts it into the entry
29/// block.
30llvm::AllocaInst *CodeGenFunction::CreateTempAlloca(const llvm::Type *Ty,
31 const char *Name) {
32 return new llvm::AllocaInst(Ty, 0, Name, AllocaInsertPt);
33}
34
35/// EvaluateExprAsBool - Perform the usual unary conversions on the specified
36/// expression and compare the result against zero, returning an Int1Ty value.
37llvm::Value *CodeGenFunction::EvaluateExprAsBool(const Expr *E) {
Chris Lattner9069fa22007-08-26 16:46:58 +000038 QualType BoolTy = getContext().BoolTy;
Chris Lattner9b2dc282008-04-04 16:54:41 +000039 if (!E->getType()->isAnyComplexType())
Chris Lattner9069fa22007-08-26 16:46:58 +000040 return EmitScalarConversion(EmitScalarExpr(E), E->getType(), BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000041
Chris Lattner9069fa22007-08-26 16:46:58 +000042 return EmitComplexToScalarConversion(EmitComplexExpr(E), E->getType(),BoolTy);
Reid Spencer5f016e22007-07-11 17:01:13 +000043}
44
Chris Lattner9b655512007-08-31 22:49:20 +000045/// EmitAnyExpr - Emit code to compute the specified expression which can have
46/// any type. The result is returned as an RValue struct. If this is an
47/// aggregate expression, the aggloc/agglocvolatile arguments indicate where
48/// the result should be returned.
49RValue CodeGenFunction::EmitAnyExpr(const Expr *E, llvm::Value *AggLoc,
50 bool isAggLocVolatile) {
51 if (!hasAggregateLLVMType(E->getType()))
52 return RValue::get(EmitScalarExpr(E));
Chris Lattner9b2dc282008-04-04 16:54:41 +000053 else if (E->getType()->isAnyComplexType())
Chris Lattner9b655512007-08-31 22:49:20 +000054 return RValue::getComplex(EmitComplexExpr(E));
55
56 EmitAggExpr(E, AggLoc, isAggLocVolatile);
57 return RValue::getAggregate(AggLoc);
58}
59
Daniel Dunbar46f45b92008-09-09 01:06:48 +000060/// EmitAnyExprToTemp - Similary to EmitAnyExpr(), however, the result
61/// will always be accessible even if no aggregate location is
62/// provided.
63RValue CodeGenFunction::EmitAnyExprToTemp(const Expr *E, llvm::Value *AggLoc,
64 bool isAggLocVolatile) {
65 if (!AggLoc && hasAggregateLLVMType(E->getType()) &&
66 !E->getType()->isAnyComplexType())
67 AggLoc = CreateTempAlloca(ConvertType(E->getType()), "agg.tmp");
68 return EmitAnyExpr(E, AggLoc, isAggLocVolatile);
69}
70
Dan Gohman4f8d1232008-05-22 00:50:06 +000071/// getAccessedFieldNo - Given an encoded value and a result number, return
72/// the input field number being accessed.
73unsigned CodeGenFunction::getAccessedFieldNo(unsigned Idx,
74 const llvm::Constant *Elts) {
75 if (isa<llvm::ConstantAggregateZero>(Elts))
76 return 0;
77
78 return cast<llvm::ConstantInt>(Elts->getOperand(Idx))->getZExtValue();
79}
80
Chris Lattner9b655512007-08-31 22:49:20 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082//===----------------------------------------------------------------------===//
83// LValue Expression Emission
84//===----------------------------------------------------------------------===//
85
Daniel Dunbarce1d38b2009-01-09 16:50:52 +000086RValue CodeGenFunction::EmitUnsupportedRValue(const Expr *E,
87 const char *Name) {
88 ErrorUnsupported(E, Name);
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +000089 if (const ComplexType *CTy = E->getType()->getAsComplexType()) {
90 const llvm::Type *EltTy = ConvertType(CTy->getElementType());
91 llvm::Value *U = llvm::UndefValue::get(EltTy);
92 return RValue::getComplex(std::make_pair(U, U));
93 } else if (hasAggregateLLVMType(E->getType())) {
94 const llvm::Type *Ty =
95 llvm::PointerType::getUnqual(ConvertType(E->getType()));
96 return RValue::getAggregate(llvm::UndefValue::get(Ty));
97 } else {
98 const llvm::Type *Ty = ConvertType(E->getType());
99 return RValue::get(llvm::UndefValue::get(Ty));
100 }
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000101}
102
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000103LValue CodeGenFunction::EmitUnsupportedLValue(const Expr *E,
104 const char *Name) {
105 ErrorUnsupported(E, Name);
106 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
107 return LValue::MakeAddr(llvm::UndefValue::get(Ty),
108 E->getType().getCVRQualifiers());
109}
110
Reid Spencer5f016e22007-07-11 17:01:13 +0000111/// EmitLValue - Emit code to compute a designator that specifies the location
112/// of the expression.
113///
114/// This can return one of two things: a simple address or a bitfield
115/// reference. In either case, the LLVM Value* in the LValue structure is
116/// guaranteed to be an LLVM pointer type.
117///
118/// If this returns a bitfield reference, nothing about the pointee type of
119/// the LLVM value is known: For example, it may not be a pointer to an
120/// integer.
121///
122/// If this returns a normal address, and if the lvalue's C type is fixed
123/// size, this method guarantees that the returned pointer type will point to
124/// an LLVM type of the same size of the lvalue's type. If the lvalue has a
125/// variable length type, this is not possible.
126///
127LValue CodeGenFunction::EmitLValue(const Expr *E) {
128 switch (E->getStmtClass()) {
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000129 default: return EmitUnsupportedLValue(E, "l-value expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000130
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000131 case Expr::BinaryOperatorClass:
132 return EmitBinaryOperatorLValue(cast<BinaryOperator>(E));
Douglas Gregorb4609802008-11-14 16:09:21 +0000133 case Expr::CallExprClass:
134 case Expr::CXXOperatorCallExprClass:
135 return EmitCallExprLValue(cast<CallExpr>(E));
Douglas Gregor1a49af92009-01-06 05:10:23 +0000136 case Expr::DeclRefExprClass:
137 case Expr::QualifiedDeclRefExprClass:
138 return EmitDeclRefLValue(cast<DeclRefExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 case Expr::ParenExprClass:return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerd9f69102008-08-10 01:53:14 +0000140 case Expr::PredefinedExprClass:
141 return EmitPredefinedLValue(cast<PredefinedExpr>(E));
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 case Expr::StringLiteralClass:
143 return EmitStringLiteralLValue(cast<StringLiteral>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000144
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +0000145 case Expr::CXXConditionDeclExprClass:
146 return EmitCXXConditionDeclLValue(cast<CXXConditionDeclExpr>(E));
147
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000148 case Expr::ObjCMessageExprClass:
149 return EmitObjCMessageExprLValue(cast<ObjCMessageExpr>(E));
Chris Lattner391d77a2008-03-30 23:03:07 +0000150 case Expr::ObjCIvarRefExprClass:
151 return EmitObjCIvarRefLValue(cast<ObjCIvarRefExpr>(E));
Daniel Dunbar6ba82a42008-08-25 20:45:57 +0000152 case Expr::ObjCPropertyRefExprClass:
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000153 return EmitObjCPropertyRefLValue(cast<ObjCPropertyRefExpr>(E));
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000154 case Expr::ObjCKVCRefExprClass:
155 return EmitObjCKVCRefLValue(cast<ObjCKVCRefExpr>(E));
Douglas Gregorcd9b46e2008-11-04 14:56:14 +0000156 case Expr::ObjCSuperExprClass:
157 return EmitObjCSuperExpr(cast<ObjCSuperExpr>(E));
158
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 case Expr::UnaryOperatorClass:
160 return EmitUnaryOpLValue(cast<UnaryOperator>(E));
161 case Expr::ArraySubscriptExprClass:
162 return EmitArraySubscriptExpr(cast<ArraySubscriptExpr>(E));
Nate Begeman213541a2008-04-18 23:10:10 +0000163 case Expr::ExtVectorElementExprClass:
164 return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
Devang Patelb9b00ad2007-10-23 20:28:39 +0000165 case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
Eli Friedman06e863f2008-05-13 23:18:27 +0000166 case Expr::CompoundLiteralExprClass:
167 return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
Chris Lattner670a62c2008-12-12 05:35:08 +0000168 case Expr::ChooseExprClass:
169 // __builtin_choose_expr is the lvalue of the selected operand.
170 if (cast<ChooseExpr>(E)->isConditionTrue(getContext()))
171 return EmitLValue(cast<ChooseExpr>(E)->getLHS());
172 else
173 return EmitLValue(cast<ChooseExpr>(E)->getRHS());
Reid Spencer5f016e22007-07-11 17:01:13 +0000174 }
175}
176
177/// EmitLoadOfLValue - Given an expression that represents a value lvalue,
178/// this method emits the address of the lvalue, then loads the result as an
179/// rvalue, returning the rvalue.
180RValue CodeGenFunction::EmitLoadOfLValue(LValue LV, QualType ExprType) {
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000181 if (LV.isObjCWeak()) {
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000182 // load of a __weak object.
183 llvm::Value *AddrWeakObj = LV.getAddress();
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000184 llvm::Value *read_weak = CGM.getObjCRuntime().EmitObjCWeakRead(*this,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000185 AddrWeakObj);
186 return RValue::get(read_weak);
187 }
188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 if (LV.isSimple()) {
190 llvm::Value *Ptr = LV.getAddress();
191 const llvm::Type *EltTy =
192 cast<llvm::PointerType>(Ptr->getType())->getElementType();
193
194 // Simple scalar l-value.
Dan Gohmand79a7262008-05-22 22:12:56 +0000195 if (EltTy->isSingleValueType()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000196 llvm::Value *V = Builder.CreateLoad(Ptr, LV.isVolatileQualified(),"tmp");
Chris Lattner01e3c9e2008-01-30 07:01:17 +0000197
198 // Bool can have different representation in memory than in registers.
199 if (ExprType->isBooleanType()) {
200 if (V->getType() != llvm::Type::Int1Ty)
201 V = Builder.CreateTrunc(V, llvm::Type::Int1Ty, "tobool");
202 }
203
204 return RValue::get(V);
205 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000206
Chris Lattner883f6a72007-08-11 00:04:45 +0000207 assert(ExprType->isFunctionType() && "Unknown scalar value");
208 return RValue::get(Ptr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 }
210
211 if (LV.isVectorElt()) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000212 llvm::Value *Vec = Builder.CreateLoad(LV.getVectorAddr(),
213 LV.isVolatileQualified(), "tmp");
Reid Spencer5f016e22007-07-11 17:01:13 +0000214 return RValue::get(Builder.CreateExtractElement(Vec, LV.getVectorIdx(),
215 "vecext"));
216 }
Chris Lattner46ea8eb2007-08-03 00:16:29 +0000217
218 // If this is a reference to a subset of the elements of a vector, either
219 // shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000220 if (LV.isExtVectorElt())
221 return EmitLoadOfExtVectorElementLValue(LV, ExprType);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000222
223 if (LV.isBitfield())
224 return EmitLoadOfBitfieldLValue(LV, ExprType);
225
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000226 if (LV.isPropertyRef())
227 return EmitLoadOfPropertyRefLValue(LV, ExprType);
228
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000229 if (LV.isKVCRef())
230 return EmitLoadOfKVCRefLValue(LV, ExprType);
231
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000232 assert(0 && "Unknown LValue type!");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000233 //an invalid RValue, but the assert will
234 //ensure that this point is never reached
235 return RValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236}
237
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000238RValue CodeGenFunction::EmitLoadOfBitfieldLValue(LValue LV,
239 QualType ExprType) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000240 unsigned StartBit = LV.getBitfieldStartBit();
241 unsigned BitfieldSize = LV.getBitfieldSize();
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000242 llvm::Value *Ptr = LV.getBitfieldAddr();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000243
244 const llvm::Type *EltTy =
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000245 cast<llvm::PointerType>(Ptr->getType())->getElementType();
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000246 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000247
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000248 // In some cases the bitfield may straddle two memory locations.
249 // Currently we load the entire bitfield, then do the magic to
250 // sign-extend it if necessary. This results in somewhat more code
251 // than necessary for the common case (one load), since two shifts
252 // accomplish both the masking and sign extension.
253 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
254 llvm::Value *Val = Builder.CreateLoad(Ptr, LV.isVolatileQualified(), "tmp");
255
256 // Shift to proper location.
Daniel Dunbarf3edc2f2008-11-13 02:20:34 +0000257 if (StartBit)
258 Val = Builder.CreateLShr(Val, llvm::ConstantInt::get(EltTy, StartBit),
259 "bf.lo");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000260
261 // Mask off unused bits.
262 llvm::Constant *LowMask =
263 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, LowBits));
264 Val = Builder.CreateAnd(Val, LowMask, "bf.lo.cleared");
265
266 // Fetch the high bits if necessary.
267 if (LowBits < BitfieldSize) {
268 unsigned HighBits = BitfieldSize - LowBits;
269 llvm::Value *HighPtr =
270 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
271 "bf.ptr.hi");
272 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
273 LV.isVolatileQualified(),
274 "tmp");
275
276 // Mask off unused bits.
277 llvm::Constant *HighMask =
278 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, HighBits));
279 HighVal = Builder.CreateAnd(HighVal, HighMask, "bf.lo.cleared");
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000280
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000281 // Shift to proper location and or in to bitfield value.
282 HighVal = Builder.CreateShl(HighVal,
283 llvm::ConstantInt::get(EltTy, LowBits));
284 Val = Builder.CreateOr(Val, HighVal, "bf.val");
285 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000286
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000287 // Sign extend if necessary.
288 if (LV.isBitfieldSigned()) {
289 llvm::Value *ExtraBits = llvm::ConstantInt::get(EltTy,
290 EltTySize - BitfieldSize);
291 Val = Builder.CreateAShr(Builder.CreateShl(Val, ExtraBits),
292 ExtraBits, "bf.val.sext");
293 }
Eli Friedman316bb1b2008-05-17 20:03:47 +0000294
295 // The bitfield type and the normal type differ when the storage sizes
296 // differ (currently just _Bool).
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000297 Val = Builder.CreateIntCast(Val, ConvertType(ExprType), false, "tmp");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000298
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000299 return RValue::get(Val);
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000300}
301
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000302RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV,
303 QualType ExprType) {
304 return EmitObjCPropertyGet(LV.getPropertyRefExpr());
305}
306
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000307RValue CodeGenFunction::EmitLoadOfKVCRefLValue(LValue LV,
308 QualType ExprType) {
309 return EmitObjCPropertyGet(LV.getKVCRefExpr());
310}
311
Chris Lattner34cdc862007-08-03 16:18:34 +0000312// If this is a reference to a subset of the elements of a vector, either
313// shuffle the input or extract/insert them as appropriate.
Nate Begeman213541a2008-04-18 23:10:10 +0000314RValue CodeGenFunction::EmitLoadOfExtVectorElementLValue(LValue LV,
315 QualType ExprType) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000316 llvm::Value *Vec = Builder.CreateLoad(LV.getExtVectorAddr(),
317 LV.isVolatileQualified(), "tmp");
Chris Lattner34cdc862007-08-03 16:18:34 +0000318
Nate Begeman8a997642008-05-09 06:41:27 +0000319 const llvm::Constant *Elts = LV.getExtVectorElts();
Chris Lattner34cdc862007-08-03 16:18:34 +0000320
321 // If the result of the expression is a non-vector type, we must be
322 // extracting a single element. Just codegen as an extractelement.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000323 const VectorType *ExprVT = ExprType->getAsVectorType();
324 if (!ExprVT) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000325 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner34cdc862007-08-03 16:18:34 +0000326 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
327 return RValue::get(Builder.CreateExtractElement(Vec, Elt, "tmp"));
328 }
329
330 // If the source and destination have the same number of elements, use a
331 // vector shuffle instead of insert/extracts.
Chris Lattnercf60cd22007-08-10 17:10:08 +0000332 unsigned NumResultElts = ExprVT->getNumElements();
Chris Lattner34cdc862007-08-03 16:18:34 +0000333 unsigned NumSourceElts =
334 cast<llvm::VectorType>(Vec->getType())->getNumElements();
335
336 if (NumResultElts == NumSourceElts) {
337 llvm::SmallVector<llvm::Constant*, 4> Mask;
338 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000339 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner34cdc862007-08-03 16:18:34 +0000340 Mask.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx));
341 }
342
343 llvm::Value *MaskV = llvm::ConstantVector::get(&Mask[0], Mask.size());
344 Vec = Builder.CreateShuffleVector(Vec,
345 llvm::UndefValue::get(Vec->getType()),
346 MaskV, "tmp");
347 return RValue::get(Vec);
348 }
349
350 // Start out with an undef of the result type.
351 llvm::Value *Result = llvm::UndefValue::get(ConvertType(ExprType));
352
353 // Extract/Insert each element of the result.
354 for (unsigned i = 0; i != NumResultElts; ++i) {
Dan Gohman4f8d1232008-05-22 00:50:06 +0000355 unsigned InIdx = getAccessedFieldNo(i, Elts);
Chris Lattner34cdc862007-08-03 16:18:34 +0000356 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
357 Elt = Builder.CreateExtractElement(Vec, Elt, "tmp");
358
359 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
360 Result = Builder.CreateInsertElement(Result, Elt, OutIdx, "tmp");
361 }
362
363 return RValue::get(Result);
364}
365
366
Reid Spencer5f016e22007-07-11 17:01:13 +0000367
368/// EmitStoreThroughLValue - Store the specified rvalue into the specified
369/// lvalue, where both are guaranteed to the have the same type, and that type
370/// is 'Ty'.
371void CodeGenFunction::EmitStoreThroughLValue(RValue Src, LValue Dst,
372 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000373 if (!Dst.isSimple()) {
374 if (Dst.isVectorElt()) {
375 // Read/modify/write the vector, inserting the new element.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000376 llvm::Value *Vec = Builder.CreateLoad(Dst.getVectorAddr(),
377 Dst.isVolatileQualified(), "tmp");
Chris Lattner9b655512007-08-31 22:49:20 +0000378 Vec = Builder.CreateInsertElement(Vec, Src.getScalarVal(),
Chris Lattner017d6aa2007-08-03 16:28:33 +0000379 Dst.getVectorIdx(), "vecins");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000380 Builder.CreateStore(Vec, Dst.getVectorAddr(),Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000381 return;
382 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000383
Nate Begeman213541a2008-04-18 23:10:10 +0000384 // If this is an update of extended vector elements, insert them as
385 // appropriate.
386 if (Dst.isExtVectorElt())
387 return EmitStoreThroughExtVectorComponentLValue(Src, Dst, Ty);
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000388
389 if (Dst.isBitfield())
390 return EmitStoreThroughBitfieldLValue(Src, Dst, Ty);
391
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000392 if (Dst.isPropertyRef())
393 return EmitStoreThroughPropertyRefLValue(Src, Dst, Ty);
394
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000395 if (Dst.isKVCRef())
396 return EmitStoreThroughKVCRefLValue(Src, Dst, Ty);
397
Lauro Ramos Venancio65539822008-01-22 22:38:35 +0000398 assert(0 && "Unknown LValue type");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000399 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000400
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000401 if (Dst.isObjCWeak()) {
402 // load of a __weak object.
403 llvm::Value *LvalueDst = Dst.getAddress();
404 llvm::Value *src = Src.getScalarVal();
405 CGM.getObjCRuntime().EmitObjCWeakAssign(*this, src, LvalueDst);
406 return;
407 }
408
409 if (Dst.isObjCStrong()) {
410 // load of a __strong object.
411 llvm::Value *LvalueDst = Dst.getAddress();
412 llvm::Value *src = Src.getScalarVal();
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000413 if (Dst.isObjCIvar())
414 CGM.getObjCRuntime().EmitObjCIvarAssign(*this, src, LvalueDst);
415 else
416 CGM.getObjCRuntime().EmitObjCGlobalAssign(*this, src, LvalueDst);
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +0000417 return;
418 }
419
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 llvm::Value *DstAddr = Dst.getAddress();
Chris Lattner883f6a72007-08-11 00:04:45 +0000421 assert(Src.isScalar() && "Can't emit an agg store with this method");
422 // FIXME: Handle volatility etc.
Chris Lattner9b655512007-08-31 22:49:20 +0000423 const llvm::Type *SrcTy = Src.getScalarVal()->getType();
Christopher Lambddc23f32007-12-17 01:11:20 +0000424 const llvm::PointerType *DstPtr = cast<llvm::PointerType>(DstAddr->getType());
425 const llvm::Type *AddrTy = DstPtr->getElementType();
426 unsigned AS = DstPtr->getAddressSpace();
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
Chris Lattner883f6a72007-08-11 00:04:45 +0000428 if (AddrTy != SrcTy)
Christopher Lambddc23f32007-12-17 01:11:20 +0000429 DstAddr = Builder.CreateBitCast(DstAddr,
430 llvm::PointerType::get(SrcTy, AS),
Chris Lattner883f6a72007-08-11 00:04:45 +0000431 "storetmp");
Eli Friedman1e692ac2008-06-13 23:01:12 +0000432 Builder.CreateStore(Src.getScalarVal(), DstAddr, Dst.isVolatileQualified());
Reid Spencer5f016e22007-07-11 17:01:13 +0000433}
434
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000435void CodeGenFunction::EmitStoreThroughBitfieldLValue(RValue Src, LValue Dst,
Daniel Dunbared3849b2008-11-19 09:36:46 +0000436 QualType Ty,
437 llvm::Value **Result) {
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000438 unsigned StartBit = Dst.getBitfieldStartBit();
439 unsigned BitfieldSize = Dst.getBitfieldSize();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000440 llvm::Value *Ptr = Dst.getBitfieldAddr();
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000441
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000442 const llvm::Type *EltTy =
443 cast<llvm::PointerType>(Ptr->getType())->getElementType();
444 unsigned EltTySize = CGM.getTargetData().getTypeSizeInBits(EltTy);
445
446 // Get the new value, cast to the appropriate type and masked to
447 // exactly the size of the bit-field.
Daniel Dunbared3849b2008-11-19 09:36:46 +0000448 llvm::Value *SrcVal = Src.getScalarVal();
449 llvm::Value *NewVal = Builder.CreateIntCast(SrcVal, EltTy, false, "tmp");
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000450 llvm::Constant *Mask =
451 llvm::ConstantInt::get(llvm::APInt::getLowBitsSet(EltTySize, BitfieldSize));
452 NewVal = Builder.CreateAnd(NewVal, Mask, "bf.value");
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000453
Daniel Dunbared3849b2008-11-19 09:36:46 +0000454 // Return the new value of the bit-field, if requested.
455 if (Result) {
456 // Cast back to the proper type for result.
457 const llvm::Type *SrcTy = SrcVal->getType();
458 llvm::Value *SrcTrunc = Builder.CreateIntCast(NewVal, SrcTy, false,
459 "bf.reload.val");
460
461 // Sign extend if necessary.
462 if (Dst.isBitfieldSigned()) {
463 unsigned SrcTySize = CGM.getTargetData().getTypeSizeInBits(SrcTy);
464 llvm::Value *ExtraBits = llvm::ConstantInt::get(SrcTy,
465 SrcTySize - BitfieldSize);
466 SrcTrunc = Builder.CreateAShr(Builder.CreateShl(SrcTrunc, ExtraBits),
467 ExtraBits, "bf.reload.sext");
468 }
469
470 *Result = SrcTrunc;
471 }
472
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000473 // In some cases the bitfield may straddle two memory locations.
474 // Emit the low part first and check to see if the high needs to be
475 // done.
476 unsigned LowBits = std::min(BitfieldSize, EltTySize - StartBit);
477 llvm::Value *LowVal = Builder.CreateLoad(Ptr, Dst.isVolatileQualified(),
478 "bf.prev.low");
Eli Friedman316bb1b2008-05-17 20:03:47 +0000479
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000480 // Compute the mask for zero-ing the low part of this bitfield.
481 llvm::Constant *InvMask =
482 llvm::ConstantInt::get(~llvm::APInt::getBitsSet(EltTySize, StartBit,
483 StartBit + LowBits));
484
485 // Compute the new low part as
486 // LowVal = (LowVal & InvMask) | (NewVal << StartBit),
487 // with the shift of NewVal implicitly stripping the high bits.
488 llvm::Value *NewLowVal =
489 Builder.CreateShl(NewVal, llvm::ConstantInt::get(EltTy, StartBit),
490 "bf.value.lo");
491 LowVal = Builder.CreateAnd(LowVal, InvMask, "bf.prev.lo.cleared");
492 LowVal = Builder.CreateOr(LowVal, NewLowVal, "bf.new.lo");
493
494 // Write back.
495 Builder.CreateStore(LowVal, Ptr, Dst.isVolatileQualified());
Eli Friedman316bb1b2008-05-17 20:03:47 +0000496
Daniel Dunbar10e3ded2008-08-06 05:08:45 +0000497 // If the low part doesn't cover the bitfield emit a high part.
498 if (LowBits < BitfieldSize) {
499 unsigned HighBits = BitfieldSize - LowBits;
500 llvm::Value *HighPtr =
501 Builder.CreateGEP(Ptr, llvm::ConstantInt::get(llvm::Type::Int32Ty, 1),
502 "bf.ptr.hi");
503 llvm::Value *HighVal = Builder.CreateLoad(HighPtr,
504 Dst.isVolatileQualified(),
505 "bf.prev.hi");
506
507 // Compute the mask for zero-ing the high part of this bitfield.
508 llvm::Constant *InvMask =
509 llvm::ConstantInt::get(~llvm::APInt::getLowBitsSet(EltTySize, HighBits));
510
511 // Compute the new high part as
512 // HighVal = (HighVal & InvMask) | (NewVal lshr LowBits),
513 // where the high bits of NewVal have already been cleared and the
514 // shift stripping the low bits.
515 llvm::Value *NewHighVal =
516 Builder.CreateLShr(NewVal, llvm::ConstantInt::get(EltTy, LowBits),
517 "bf.value.high");
518 HighVal = Builder.CreateAnd(HighVal, InvMask, "bf.prev.hi.cleared");
519 HighVal = Builder.CreateOr(HighVal, NewHighVal, "bf.new.hi");
520
521 // Write back.
522 Builder.CreateStore(HighVal, HighPtr, Dst.isVolatileQualified());
523 }
Lauro Ramos Venancioa0c5d0e2008-01-22 22:36:45 +0000524}
525
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000526void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src,
527 LValue Dst,
528 QualType Ty) {
529 EmitObjCPropertySet(Dst.getPropertyRefExpr(), Src);
530}
531
Fariborz Jahanian43f44702008-11-22 22:30:21 +0000532void CodeGenFunction::EmitStoreThroughKVCRefLValue(RValue Src,
533 LValue Dst,
534 QualType Ty) {
535 EmitObjCPropertySet(Dst.getKVCRefExpr(), Src);
536}
537
Nate Begeman213541a2008-04-18 23:10:10 +0000538void CodeGenFunction::EmitStoreThroughExtVectorComponentLValue(RValue Src,
539 LValue Dst,
540 QualType Ty) {
Chris Lattner017d6aa2007-08-03 16:28:33 +0000541 // This access turns into a read/modify/write of the vector. Load the input
542 // value now.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000543 llvm::Value *Vec = Builder.CreateLoad(Dst.getExtVectorAddr(),
544 Dst.isVolatileQualified(), "tmp");
Nate Begeman8a997642008-05-09 06:41:27 +0000545 const llvm::Constant *Elts = Dst.getExtVectorElts();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000546
Chris Lattner9b655512007-08-31 22:49:20 +0000547 llvm::Value *SrcVal = Src.getScalarVal();
Chris Lattner017d6aa2007-08-03 16:28:33 +0000548
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000549 if (const VectorType *VTy = Ty->getAsVectorType()) {
550 unsigned NumSrcElts = VTy->getNumElements();
551
552 // Extract/Insert each element.
553 for (unsigned i = 0; i != NumSrcElts; ++i) {
554 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, i);
555 Elt = Builder.CreateExtractElement(SrcVal, Elt, "tmp");
556
Dan Gohman4f8d1232008-05-22 00:50:06 +0000557 unsigned Idx = getAccessedFieldNo(i, Elts);
Chris Lattner7e6b51b2007-08-03 16:37:04 +0000558 llvm::Value *OutIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, Idx);
559 Vec = Builder.CreateInsertElement(Vec, Elt, OutIdx, "tmp");
560 }
561 } else {
562 // If the Src is a scalar (not a vector) it must be updating one element.
Dan Gohman4f8d1232008-05-22 00:50:06 +0000563 unsigned InIdx = getAccessedFieldNo(0, Elts);
Chris Lattner017d6aa2007-08-03 16:28:33 +0000564 llvm::Value *Elt = llvm::ConstantInt::get(llvm::Type::Int32Ty, InIdx);
565 Vec = Builder.CreateInsertElement(Vec, SrcVal, Elt, "tmp");
Chris Lattner017d6aa2007-08-03 16:28:33 +0000566 }
567
Eli Friedman1e692ac2008-06-13 23:01:12 +0000568 Builder.CreateStore(Vec, Dst.getExtVectorAddr(), Dst.isVolatileQualified());
Chris Lattner017d6aa2007-08-03 16:28:33 +0000569}
570
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000571/// SetVarDeclObjCAttribute - Set __weak/__strong attributes into the LValue
572/// object.
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +0000573static void SetVarDeclObjCAttribute(ASTContext &Ctx, const Decl *VD,
Fariborz Jahanian80b0b422008-11-20 18:10:58 +0000574 const QualType &Ty, LValue &LV)
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000575{
576 if (const ObjCGCAttr *A = VD->getAttr<ObjCGCAttr>()) {
577 ObjCGCAttr::GCAttrTypes attrType = A->getType();
578 LValue::SetObjCType(attrType == ObjCGCAttr::Weak,
579 attrType == ObjCGCAttr::Strong, LV);
580 }
Fariborz Jahanian80b0b422008-11-20 18:10:58 +0000581 else if (Ctx.getLangOptions().ObjC1 &&
582 Ctx.getLangOptions().getGCMode() != LangOptions::NonGC) {
583 // Default behavious under objective-c's gc is for objective-c pointers
584 // be treated as though they were declared as __strong.
585 if (Ctx.isObjCObjectPointerType(Ty))
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000586 LValue::SetObjCType(false, true, LV);
587 }
588}
Reid Spencer5f016e22007-07-11 17:01:13 +0000589
590LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) {
Steve Naroff248a7532008-04-15 22:42:06 +0000591 const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl());
592
Chris Lattner41110242008-06-17 18:05:57 +0000593 if (VD && (VD->isBlockVarDecl() || isa<ParmVarDecl>(VD) ||
594 isa<ImplicitParamDecl>(VD))) {
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000595 LValue LV;
596 if (VD->getStorageClass() == VarDecl::Extern) {
597 LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
598 E->getType().getCVRQualifiers());
599 }
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000600 else {
Steve Naroff248a7532008-04-15 22:42:06 +0000601 llvm::Value *V = LocalDeclMap[VD];
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000602 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000603 LV = LValue::MakeAddr(V, E->getType().getCVRQualifiers());
Lauro Ramos Venanciofea90b82008-02-16 22:30:38 +0000604 }
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000605 if (VD->isBlockVarDecl() &&
606 (VD->getStorageClass() == VarDecl::Static ||
607 VD->getStorageClass() == VarDecl::Extern))
Fariborz Jahanian80b0b422008-11-20 18:10:58 +0000608 SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000609 return LV;
Steve Naroff248a7532008-04-15 22:42:06 +0000610 } else if (VD && VD->isFileVarDecl()) {
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000611 LValue LV = LValue::MakeAddr(CGM.GetAddrOfGlobalVar(VD),
612 E->getType().getCVRQualifiers());
Fariborz Jahanian80b0b422008-11-20 18:10:58 +0000613 SetVarDeclObjCAttribute(getContext(), VD, E->getType(), LV);
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000614 return LV;
Steve Naroff248a7532008-04-15 22:42:06 +0000615 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000616 return LValue::MakeAddr(CGM.GetAddrOfFunction(FD),
Eli Friedman1e692ac2008-06-13 23:01:12 +0000617 E->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 }
Chris Lattner41110242008-06-17 18:05:57 +0000619 else if (const ImplicitParamDecl *IPD =
620 dyn_cast<ImplicitParamDecl>(E->getDecl())) {
621 llvm::Value *V = LocalDeclMap[IPD];
622 assert(V && "BlockVarDecl not entered in LocalDeclMap?");
623 return LValue::MakeAddr(V, E->getType().getCVRQualifiers());
624 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 assert(0 && "Unimp declref");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000626 //an invalid LValue, but the assert will
627 //ensure that this point is never reached.
628 return LValue();
Reid Spencer5f016e22007-07-11 17:01:13 +0000629}
630
631LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
632 // __extension__ doesn't affect lvalue-ness.
633 if (E->getOpcode() == UnaryOperator::Extension)
634 return EmitLValue(E->getSubExpr());
635
Chris Lattner96196622008-07-26 22:37:01 +0000636 QualType ExprTy = getContext().getCanonicalType(E->getSubExpr()->getType());
Chris Lattner7da36f62007-10-30 22:53:42 +0000637 switch (E->getOpcode()) {
638 default: assert(0 && "Unknown unary operator lvalue!");
639 case UnaryOperator::Deref:
Eli Friedman1e692ac2008-06-13 23:01:12 +0000640 return LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000641 ExprTy->getAsPointerType()->getPointeeType()
642 .getCVRQualifiers());
Chris Lattner7da36f62007-10-30 22:53:42 +0000643 case UnaryOperator::Real:
644 case UnaryOperator::Imag:
645 LValue LV = EmitLValue(E->getSubExpr());
Chris Lattner36b6a0a2008-03-19 05:19:41 +0000646 unsigned Idx = E->getOpcode() == UnaryOperator::Imag;
647 return LValue::MakeAddr(Builder.CreateStructGEP(LV.getAddress(),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000648 Idx, "idx"),
649 ExprTy.getCVRQualifiers());
Chris Lattner7da36f62007-10-30 22:53:42 +0000650 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000651}
652
653LValue CodeGenFunction::EmitStringLiteralLValue(const StringLiteral *E) {
Daniel Dunbar61432932008-08-13 23:20:05 +0000654 return LValue::MakeAddr(CGM.GetAddrOfConstantStringFromLiteral(E), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000655}
656
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000657LValue CodeGenFunction::EmitPredefinedFunctionName(unsigned Type) {
Anders Carlsson22742662007-07-21 05:21:51 +0000658 std::string GlobalVarName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000659
660 switch (Type) {
Anders Carlsson22742662007-07-21 05:21:51 +0000661 default:
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000662 assert(0 && "Invalid type");
Chris Lattnerd9f69102008-08-10 01:53:14 +0000663 case PredefinedExpr::Func:
Anders Carlsson22742662007-07-21 05:21:51 +0000664 GlobalVarName = "__func__.";
665 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000666 case PredefinedExpr::Function:
Anders Carlsson22742662007-07-21 05:21:51 +0000667 GlobalVarName = "__FUNCTION__.";
668 break;
Chris Lattnerd9f69102008-08-10 01:53:14 +0000669 case PredefinedExpr::PrettyFunction:
Anders Carlsson22742662007-07-21 05:21:51 +0000670 // FIXME:: Demangle C++ method names
671 GlobalVarName = "__PRETTY_FUNCTION__.";
672 break;
673 }
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000674
675 std::string FunctionName;
676 if(const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurFuncDecl)) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000677 FunctionName = FD->getNameAsString();
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000678 } else {
679 // Just get the mangled name.
680 FunctionName = CurFn->getName();
681 }
682
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000683 GlobalVarName += FunctionName;
Daniel Dunbar662b71e2008-10-17 21:58:32 +0000684 llvm::Constant *C =
685 CGM.GetAddrOfConstantCString(FunctionName, GlobalVarName.c_str());
686 return LValue::MakeAddr(C, 0);
687}
688
689LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
690 switch (E->getIdentType()) {
691 default:
692 return EmitUnsupportedLValue(E, "predefined expression");
693 case PredefinedExpr::Func:
694 case PredefinedExpr::Function:
695 case PredefinedExpr::PrettyFunction:
696 return EmitPredefinedFunctionName(E->getIdentType());
697 }
Anders Carlsson22742662007-07-21 05:21:51 +0000698}
699
Reid Spencer5f016e22007-07-11 17:01:13 +0000700LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E) {
Ted Kremenek23245122007-08-20 16:18:38 +0000701 // The index must always be an integer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000702 llvm::Value *Idx = EmitScalarExpr(E->getIdx());
Reid Spencer5f016e22007-07-11 17:01:13 +0000703
704 // If the base is a vector type, then we are forming a vector element lvalue
705 // with this subscript.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000706 if (E->getBase()->getType()->isVectorType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000707 // Emit the vector as an lvalue to get its address.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000708 LValue LHS = EmitLValue(E->getBase());
Ted Kremenek23245122007-08-20 16:18:38 +0000709 assert(LHS.isSimple() && "Can only subscript lvalue vectors here!");
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // FIXME: This should properly sign/zero/extend or truncate Idx to i32.
Eli Friedman1e692ac2008-06-13 23:01:12 +0000711 return LValue::MakeVectorElt(LHS.getAddress(), Idx,
712 E->getBase()->getType().getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 }
714
Ted Kremenek23245122007-08-20 16:18:38 +0000715 // The base must be a pointer, which is not an aggregate. Emit it.
Chris Lattner7f02f722007-08-24 05:35:26 +0000716 llvm::Value *Base = EmitScalarExpr(E->getBase());
Reid Spencer5f016e22007-07-11 17:01:13 +0000717
Ted Kremenek23245122007-08-20 16:18:38 +0000718 // Extend or truncate the index type to 32 or 64-bits.
Chris Lattnerd4f08022007-08-08 17:43:05 +0000719 QualType IdxTy = E->getIdx()->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 bool IdxSigned = IdxTy->isSignedIntegerType();
721 unsigned IdxBitwidth = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
722 if (IdxBitwidth != LLVMPointerWidth)
723 Idx = Builder.CreateIntCast(Idx, llvm::IntegerType::get(LLVMPointerWidth),
724 IdxSigned, "idxprom");
725
726 // We know that the pointer points to a type of the correct size, unless the
727 // size is a VLA.
Anders Carlsson8b33c082008-12-21 00:11:23 +0000728 if (const VariableArrayType *VAT =
729 getContext().getAsVariableArrayType(E->getType())) {
730 llvm::Value *VLASize = VLASizeMap[VAT];
731
732 Idx = Builder.CreateMul(Idx, VLASize);
733
Anders Carlsson6183a992008-12-21 03:44:36 +0000734 QualType BaseType = getContext().getBaseElementType(VAT);
Anders Carlsson8b33c082008-12-21 00:11:23 +0000735
736 uint64_t BaseTypeSize = getContext().getTypeSize(BaseType) / 8;
737 Idx = Builder.CreateUDiv(Idx,
738 llvm::ConstantInt::get(Idx->getType(),
739 BaseTypeSize));
740 }
741
Chris Lattner96196622008-07-26 22:37:01 +0000742 QualType ExprTy = getContext().getCanonicalType(E->getBase()->getType());
Chris Lattnerb77792e2008-07-26 22:17:49 +0000743
Eli Friedman1e692ac2008-06-13 23:01:12 +0000744 return LValue::MakeAddr(Builder.CreateGEP(Base, Idx, "arrayidx"),
Chris Lattnerb77792e2008-07-26 22:17:49 +0000745 ExprTy->getAsPointerType()->getPointeeType()
746 .getCVRQualifiers());
Reid Spencer5f016e22007-07-11 17:01:13 +0000747}
748
Nate Begeman3b8d1162008-05-13 21:03:02 +0000749static
750llvm::Constant *GenerateConstantVector(llvm::SmallVector<unsigned, 4> &Elts) {
751 llvm::SmallVector<llvm::Constant *, 4> CElts;
752
753 for (unsigned i = 0, e = Elts.size(); i != e; ++i)
754 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, Elts[i]));
755
756 return llvm::ConstantVector::get(&CElts[0], CElts.size());
757}
758
Chris Lattner349aaec2007-08-02 23:37:31 +0000759LValue CodeGenFunction::
Nate Begeman213541a2008-04-18 23:10:10 +0000760EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
Chris Lattner349aaec2007-08-02 23:37:31 +0000761 // Emit the base vector as an l-value.
762 LValue Base = EmitLValue(E->getBase());
Chris Lattner349aaec2007-08-02 23:37:31 +0000763
Nate Begeman3b8d1162008-05-13 21:03:02 +0000764 // Encode the element access list into a vector of unsigned indices.
765 llvm::SmallVector<unsigned, 4> Indices;
766 E->getEncodedElementAccess(Indices);
767
768 if (Base.isSimple()) {
769 llvm::Constant *CV = GenerateConstantVector(Indices);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000770 return LValue::MakeExtVectorElt(Base.getAddress(), CV,
771 E->getBase()->getType().getCVRQualifiers());
Nate Begeman3b8d1162008-05-13 21:03:02 +0000772 }
773 assert(Base.isExtVectorElt() && "Can only subscript lvalue vec elts here!");
774
775 llvm::Constant *BaseElts = Base.getExtVectorElts();
776 llvm::SmallVector<llvm::Constant *, 4> CElts;
777
778 for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
779 if (isa<llvm::ConstantAggregateZero>(BaseElts))
780 CElts.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
781 else
782 CElts.push_back(BaseElts->getOperand(Indices[i]));
783 }
784 llvm::Constant *CV = llvm::ConstantVector::get(&CElts[0], CElts.size());
Eli Friedman1e692ac2008-06-13 23:01:12 +0000785 return LValue::MakeExtVectorElt(Base.getExtVectorAddr(), CV,
786 E->getBase()->getType().getCVRQualifiers());
Chris Lattner349aaec2007-08-02 23:37:31 +0000787}
788
Devang Patelb9b00ad2007-10-23 20:28:39 +0000789LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
Devang Patelfe2419a2007-12-11 21:33:16 +0000790 bool isUnion = false;
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000791 bool isIvar = false;
Devang Patel126a8562007-10-24 22:26:28 +0000792 Expr *BaseExpr = E->getBase();
Devang Patel126a8562007-10-24 22:26:28 +0000793 llvm::Value *BaseValue = NULL;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000794 unsigned CVRQualifiers=0;
795
Chris Lattner12f65f62007-12-02 18:52:07 +0000796 // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
Devang Patelfe2419a2007-12-11 21:33:16 +0000797 if (E->isArrow()) {
Devang Patel0a961182007-10-26 18:15:21 +0000798 BaseValue = EmitScalarExpr(BaseExpr);
Devang Patelfe2419a2007-12-11 21:33:16 +0000799 const PointerType *PTy =
Chris Lattner96196622008-07-26 22:37:01 +0000800 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
Devang Patelfe2419a2007-12-11 21:33:16 +0000801 if (PTy->getPointeeType()->isUnionType())
802 isUnion = true;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000803 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
Devang Patelfe2419a2007-12-11 21:33:16 +0000804 }
Chris Lattner12f65f62007-12-02 18:52:07 +0000805 else {
806 LValue BaseLV = EmitLValue(BaseExpr);
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000807 if (BaseLV.isObjCIvar())
808 isIvar = true;
Chris Lattner12f65f62007-12-02 18:52:07 +0000809 // FIXME: this isn't right for bitfields.
810 BaseValue = BaseLV.getAddress();
Devang Patelfe2419a2007-12-11 21:33:16 +0000811 if (BaseExpr->getType()->isUnionType())
812 isUnion = true;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000813 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
Chris Lattner12f65f62007-12-02 18:52:07 +0000814 }
Devang Patelb9b00ad2007-10-23 20:28:39 +0000815
Douglas Gregor86f19402008-12-20 23:49:58 +0000816 FieldDecl *Field = dyn_cast<FieldDecl>(E->getMemberDecl());
817 // FIXME: Handle non-field member expressions
818 assert(Field && "No code generation for non-field member references");
Fariborz Jahanian2ab19682008-11-21 18:14:01 +0000819 LValue MemExpLV = EmitLValueForField(BaseValue, Field, isUnion, CVRQualifiers);
820 LValue::SetObjCIvar(MemExpLV, isIvar);
821 return MemExpLV;
Eli Friedman472778e2008-02-09 08:50:58 +0000822}
Devang Patelb9b00ad2007-10-23 20:28:39 +0000823
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000824LValue CodeGenFunction::EmitLValueForBitfield(llvm::Value* BaseValue,
825 FieldDecl* Field,
826 unsigned CVRQualifiers,
827 unsigned idx) {
828 // FIXME: CodeGenTypes should expose a method to get the appropriate
829 // type for FieldTy (the appropriate type is ABI-dependent).
830 const llvm::Type *FieldTy = CGM.getTypes().ConvertTypeForMem(Field->getType());
831 const llvm::PointerType *BaseTy =
832 cast<llvm::PointerType>(BaseValue->getType());
833 unsigned AS = BaseTy->getAddressSpace();
834 BaseValue = Builder.CreateBitCast(BaseValue,
835 llvm::PointerType::get(FieldTy, AS),
836 "tmp");
837 llvm::Value *V = Builder.CreateGEP(BaseValue,
838 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx),
839 "tmp");
840
841 CodeGenTypes::BitFieldInfo bitFieldInfo =
842 CGM.getTypes().getBitFieldInfo(Field);
843 return LValue::MakeBitfield(V, bitFieldInfo.Begin, bitFieldInfo.Size,
844 Field->getType()->isSignedIntegerType(),
845 Field->getType().getCVRQualifiers()|CVRQualifiers);
846}
847
Eli Friedman472778e2008-02-09 08:50:58 +0000848LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue,
849 FieldDecl* Field,
Eli Friedman1e692ac2008-06-13 23:01:12 +0000850 bool isUnion,
851 unsigned CVRQualifiers)
Eli Friedman472778e2008-02-09 08:50:58 +0000852{
Eli Friedman472778e2008-02-09 08:50:58 +0000853 unsigned idx = CGM.getTypes().getLLVMFieldNo(Field);
Lauro Ramos Venanciod957aa02008-02-07 19:29:53 +0000854
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000855 if (Field->isBitField())
856 return EmitLValueForBitfield(BaseValue, Field, CVRQualifiers, idx);
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000857
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000858 llvm::Value *V = Builder.CreateStructGEP(BaseValue, idx, "tmp");
Eli Friedman1e86b342008-05-29 11:33:25 +0000859
Devang Patelabad06c2007-10-26 19:42:18 +0000860 // Match union field type.
Lauro Ramos Venanciod957aa02008-02-07 19:29:53 +0000861 if (isUnion) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000862 const llvm::Type *FieldTy =
863 CGM.getTypes().ConvertTypeForMem(Field->getType());
Devang Patele9b8c0a2007-10-30 20:59:40 +0000864 const llvm::PointerType * BaseTy =
865 cast<llvm::PointerType>(BaseValue->getType());
Eli Friedman788d5712008-05-21 13:24:44 +0000866 unsigned AS = BaseTy->getAddressSpace();
867 V = Builder.CreateBitCast(V,
868 llvm::PointerType::get(FieldTy, AS),
869 "tmp");
Devang Patelabad06c2007-10-26 19:42:18 +0000870 }
Lauro Ramos Venancio3b8c22d2008-01-22 20:17:04 +0000871
Fariborz Jahanian2682d8b2008-11-20 00:15:42 +0000872 LValue LV =
873 LValue::MakeAddr(V,
874 Field->getType().getCVRQualifiers()|CVRQualifiers);
875 if (const ObjCGCAttr *A = Field->getAttr<ObjCGCAttr>()) {
876 ObjCGCAttr::GCAttrTypes attrType = A->getType();
877 // __weak attribute on a field is ignored.
878 LValue::SetObjCType(false, attrType == ObjCGCAttr::Strong, LV);
879 }
880 else if (CGM.getLangOptions().ObjC1 &&
881 CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
882 QualType ExprTy = Field->getType();
883 if (getContext().isObjCObjectPointerType(ExprTy))
884 LValue::SetObjCType(false, true, LV);
885 }
886 return LV;
Devang Patelb9b00ad2007-10-23 20:28:39 +0000887}
888
Eli Friedman1e692ac2008-06-13 23:01:12 +0000889LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E)
890{
Eli Friedman06e863f2008-05-13 23:18:27 +0000891 const llvm::Type *LTy = ConvertType(E->getType());
892 llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral");
893
894 const Expr* InitExpr = E->getInitializer();
Eli Friedman1e692ac2008-06-13 23:01:12 +0000895 LValue Result = LValue::MakeAddr(DeclPtr, E->getType().getCVRQualifiers());
Eli Friedman06e863f2008-05-13 23:18:27 +0000896
897 if (E->getType()->isComplexType()) {
898 EmitComplexExprIntoAddr(InitExpr, DeclPtr, false);
899 } else if (hasAggregateLLVMType(E->getType())) {
900 EmitAnyExpr(InitExpr, DeclPtr, false);
901 } else {
902 EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType());
903 }
904
905 return Result;
906}
907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908//===--------------------------------------------------------------------===//
909// Expression Emission
910//===--------------------------------------------------------------------===//
911
Chris Lattner7016a702007-08-20 22:37:10 +0000912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
Anders Carlsson022012e2007-08-20 18:05:56 +0000914 if (const ImplicitCastExpr *IcExpr =
915 dyn_cast<const ImplicitCastExpr>(E->getCallee()))
916 if (const DeclRefExpr *DRExpr =
917 dyn_cast<const DeclRefExpr>(IcExpr->getSubExpr()))
918 if (const FunctionDecl *FDecl =
919 dyn_cast<const FunctionDecl>(DRExpr->getDecl()))
920 if (unsigned builtinID = FDecl->getIdentifier()->getBuiltinID())
921 return EmitBuiltinExpr(builtinID, E);
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000922
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000923 if (E->getCallee()->getType()->isBlockPointerType())
Daniel Dunbar8fa73ed2009-01-09 20:09:28 +0000924 return EmitUnsupportedRValue(E, "block pointer reference");
Daniel Dunbarce1d38b2009-01-09 16:50:52 +0000925
Chris Lattner7f02f722007-08-24 05:35:26 +0000926 llvm::Value *Callee = EmitScalarExpr(E->getCallee());
Eli Friedman5193b8a2008-01-30 01:32:06 +0000927 return EmitCallExpr(Callee, E->getCallee()->getType(),
Ted Kremenek55499762008-06-17 02:43:46 +0000928 E->arg_begin(), E->arg_end());
Nate Begemane2ce1d92008-01-17 17:46:27 +0000929}
930
Ted Kremenek55499762008-06-17 02:43:46 +0000931RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr,
932 CallExpr::const_arg_iterator ArgBeg,
933 CallExpr::const_arg_iterator ArgEnd) {
934
Nate Begemane2ce1d92008-01-17 17:46:27 +0000935 llvm::Value *Callee = EmitScalarExpr(FnExpr);
Ted Kremenek55499762008-06-17 02:43:46 +0000936 return EmitCallExpr(Callee, FnExpr->getType(), ArgBeg, ArgEnd);
Chris Lattnerc5e940f2007-08-31 04:44:06 +0000937}
938
Daniel Dunbar80e62c22008-09-04 03:20:13 +0000939LValue CodeGenFunction::EmitBinaryOperatorLValue(const BinaryOperator *E) {
940 // Can only get l-value for binary operator expressions which are a
941 // simple assignment of aggregate type.
942 if (E->getOpcode() != BinaryOperator::Assign)
943 return EmitUnsupportedLValue(E, "binary l-value expression");
944
945 llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType()));
946 EmitAggExpr(E, Temp, false);
947 // FIXME: Are these qualifiers correct?
948 return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers());
949}
950
Christopher Lamb22c940e2007-12-29 05:02:41 +0000951LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) {
952 // Can only get l-value for call expression returning aggregate type
953 RValue RV = EmitCallExpr(E);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000954 // FIXME: can this be volatile?
955 return LValue::MakeAddr(RV.getAggregateAddr(),
956 E->getType().getCVRQualifiers());
Christopher Lamb22c940e2007-12-29 05:02:41 +0000957}
958
Argyrios Kyrtzidise3a09e62008-09-10 02:36:38 +0000959LValue
960CodeGenFunction::EmitCXXConditionDeclLValue(const CXXConditionDeclExpr *E) {
961 EmitLocalBlockVarDecl(*E->getVarDecl());
962 return EmitDeclRefLValue(E);
963}
964
Daniel Dunbar0a04d772008-08-23 10:51:21 +0000965LValue CodeGenFunction::EmitObjCMessageExprLValue(const ObjCMessageExpr *E) {
966 // Can only get l-value for message expression returning aggregate type
967 RValue RV = EmitObjCMessageExpr(E);
968 // FIXME: can this be volatile?
969 return LValue::MakeAddr(RV.getAggregateAddr(),
970 E->getType().getCVRQualifiers());
971}
972
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000973llvm::Value *CodeGenFunction::EmitIvarOffset(ObjCInterfaceDecl *Interface,
974 const ObjCIvarDecl *Ivar) {
Chris Lattner391d77a2008-03-30 23:03:07 +0000975 // Objective-C objects are traditionally C structures with their layout
976 // defined at compile-time. In some implementations, their layout is not
977 // defined until run time in order to allow instance variables to be added to
978 // a class without recompiling all of the subclasses. If this is the case
979 // then the CGObjCRuntime subclass must return true to LateBoundIvars and
980 // implement the lookup itself.
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000981 if (CGM.getObjCRuntime().LateBoundIVars())
982 assert(0 && "late-bound ivars are unsupported");
Chris Lattnerc8aa5f12008-04-04 04:07:35 +0000983
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000984 const llvm::Type *InterfaceLTy =
985 CGM.getTypes().ConvertType(getContext().getObjCInterfaceType(Interface));
986 const llvm::StructLayout *Layout =
987 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy));
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000988 FieldDecl *Field = Interface->lookupFieldDeclForIvar(getContext(), Ivar);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000989 uint64_t Offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000990 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000991
992 return llvm::ConstantInt::get(CGM.getTypes().ConvertType(getContext().LongTy),
993 Offset);
994}
995
996LValue CodeGenFunction::EmitLValueForIvar(llvm::Value *BaseValue,
997 const ObjCIvarDecl *Ivar,
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000998 const FieldDecl *Field,
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +0000999 unsigned CVRQualifiers) {
1000 // See comment in EmitIvarOffset.
1001 if (CGM.getObjCRuntime().LateBoundIVars())
1002 assert(0 && "late-bound ivars are unsupported");
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001003 // TODO: Add a special case for isa (index 0)
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001004 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001005
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +00001006 if (Ivar->isBitField()) {
1007 return EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1008 CVRQualifiers, Index);
1009 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001010 llvm::Value *V = Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +00001011 LValue LV = LValue::MakeAddr(V, Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1012 SetVarDeclObjCAttribute(getContext(), Ivar, Ivar->getType(), LV);
Fariborz Jahanian2ab19682008-11-21 18:14:01 +00001013 LValue::SetObjCIvar(LV, true);
Fariborz Jahaniand1cc8042008-11-20 20:53:20 +00001014 return LV;
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001015}
1016
1017LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) {
Anders Carlsson29b7e502008-08-25 01:53:23 +00001018 // FIXME: A lot of the code below could be shared with EmitMemberExpr.
1019 llvm::Value *BaseValue = 0;
1020 const Expr *BaseExpr = E->getBase();
1021 unsigned CVRQualifiers = 0;
1022 if (E->isArrow()) {
1023 BaseValue = EmitScalarExpr(BaseExpr);
1024 const PointerType *PTy =
1025 cast<PointerType>(getContext().getCanonicalType(BaseExpr->getType()));
1026 CVRQualifiers = PTy->getPointeeType().getCVRQualifiers();
1027 } else {
1028 LValue BaseLV = EmitLValue(BaseExpr);
1029 // FIXME: this isn't right for bitfields.
1030 BaseValue = BaseLV.getAddress();
1031 CVRQualifiers = BaseExpr->getType().getCVRQualifiers();
1032 }
Daniel Dunbar29e0bcc2008-09-24 04:00:38 +00001033
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +00001034 return EmitLValueForIvar(BaseValue, E->getDecl(),
1035 getContext().getFieldDecl(E), CVRQualifiers);
Chris Lattner391d77a2008-03-30 23:03:07 +00001036}
1037
Daniel Dunbar85c59ed2008-08-29 08:11:39 +00001038LValue
1039CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) {
1040 // This is a special l-value that just issues sends when we load or
1041 // store through it.
1042 return LValue::MakePropertyRef(E, E->getType().getCVRQualifiers());
1043}
1044
Fariborz Jahanian43f44702008-11-22 22:30:21 +00001045LValue
1046CodeGenFunction::EmitObjCKVCRefLValue(const ObjCKVCRefExpr *E) {
1047 // This is a special l-value that just issues sends when we load or
1048 // store through it.
1049 return LValue::MakeKVCRef(E, E->getType().getCVRQualifiers());
1050}
1051
Douglas Gregorcd9b46e2008-11-04 14:56:14 +00001052LValue
1053CodeGenFunction::EmitObjCSuperExpr(const ObjCSuperExpr *E) {
1054 return EmitUnsupportedLValue(E, "use of super");
1055}
1056
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001057RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType CalleeType,
Ted Kremenek55499762008-06-17 02:43:46 +00001058 CallExpr::const_arg_iterator ArgBeg,
1059 CallExpr::const_arg_iterator ArgEnd) {
Daniel Dunbarce1d38b2009-01-09 16:50:52 +00001060 // Get the actual function type. The callee type will always be a
1061 // pointer to function type or a block pointer type.
1062 QualType ResultType;
1063 if (const BlockPointerType *BPT = dyn_cast<BlockPointerType>(CalleeType)) {
1064 ResultType = BPT->getPointeeType()->getAsFunctionType()->getResultType();
1065 } else {
1066 assert(CalleeType->isFunctionPointerType() &&
1067 "Call must have function pointer type!");
1068 QualType FnType = CalleeType->getAsPointerType()->getPointeeType();
1069 ResultType = FnType->getAsFunctionType()->getResultType();
1070 }
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001071
1072 CallArgList Args;
1073 for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I)
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001074 Args.push_back(std::make_pair(EmitAnyExprToTemp(*I),
1075 I->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001076
1077 return EmitCall(Callee, ResultType, Args);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001078}