blob: ec6a75f3569bfb3aa6bc108ea76f71748b352e83 [file] [log] [blame]
Anders Carlsson3b1d57b2008-01-26 01:36:00 +00001//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Constant Expr nodes as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CodeGenFunction.h"
15#include "CodeGenModule.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000016#include "CGObjCRuntime.h"
Daniel Dunbarde7fb842008-08-11 05:00:27 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/StmtVisitor.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000019#include "llvm/Constants.h"
20#include "llvm/Function.h"
21#include "llvm/GlobalVariable.h"
22#include "llvm/Support/Compiler.h"
Eli Friedmana8234002008-05-30 10:24:46 +000023#include "llvm/Target/TargetData.h"
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000024using namespace clang;
25using namespace CodeGen;
26
27namespace {
Anders Carlsson84005b42008-01-26 04:30:23 +000028class VISIBILITY_HIDDEN ConstExprEmitter :
29 public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000030 CodeGenModule &CGM;
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000031 CodeGenFunction *CGF;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000032public:
Lauro Ramos Venancio81373352008-02-26 21:41:45 +000033 ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
34 : CGM(cgm), CGF(cgf) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000035 }
36
37 //===--------------------------------------------------------------------===//
38 // Visitor Methods
39 //===--------------------------------------------------------------------===//
40
41 llvm::Constant *VisitStmt(Stmt *S) {
Daniel Dunbar488e9932008-08-16 00:56:44 +000042 CGM.ErrorUnsupported(S, "constant expression");
Chris Lattner98be4942008-03-05 18:54:05 +000043 QualType T = cast<Expr>(S)->getType();
44 return llvm::UndefValue::get(CGM.getTypes().ConvertType(T));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000045 }
46
47 llvm::Constant *VisitParenExpr(ParenExpr *PE) {
48 return Visit(PE->getSubExpr());
49 }
50
51 // Leaves
52 llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *E) {
53 return llvm::ConstantInt::get(E->getValue());
54 }
55 llvm::Constant *VisitFloatingLiteral(const FloatingLiteral *E) {
Chris Lattner59138ba2008-04-20 00:45:53 +000056 return llvm::ConstantFP::get(E->getValue());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000057 }
58 llvm::Constant *VisitCharacterLiteral(const CharacterLiteral *E) {
59 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
60 }
61 llvm::Constant *VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
62 return llvm::ConstantInt::get(ConvertType(E->getType()), E->getValue());
63 }
Chris Lattner13fd7e52008-06-21 21:44:18 +000064 llvm::Constant *VisitObjCStringLiteral(const ObjCStringLiteral *E) {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000065 std::string S(E->getString()->getStrData(),
66 E->getString()->getByteLength());
67 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(S);
68 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner13fd7e52008-06-21 21:44:18 +000069 }
Anders Carlsson3b1d57b2008-01-26 01:36:00 +000070
71 llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
72 return Visit(E->getInitializer());
73 }
74
75 llvm::Constant *VisitCastExpr(const CastExpr* E) {
76 llvm::Constant *C = Visit(E->getSubExpr());
77
78 return EmitConversion(C, E->getSubExpr()->getType(), E->getType());
79 }
Devang Pateleae15602008-02-05 02:39:50 +000080
Chris Lattner04421082008-04-08 04:40:51 +000081 llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
82 return Visit(DAE->getExpr());
83 }
84
Eli Friedmana8234002008-05-30 10:24:46 +000085 llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
Eli Friedman4d1c0182008-02-21 17:45:41 +000086 std::vector<llvm::Constant*> Elts;
Eli Friedmana8234002008-05-30 10:24:46 +000087 const llvm::ArrayType *AType =
88 cast<llvm::ArrayType>(ConvertType(ILE->getType()));
Eli Friedman4d1c0182008-02-21 17:45:41 +000089 unsigned NumInitElements = ILE->getNumInits();
90 // FIXME: Check for wide strings
91 if (NumInitElements > 0 && isa<StringLiteral>(ILE->getInit(0)) &&
Chris Lattnerc63a1f22008-08-04 07:31:14 +000092 ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
Eli Friedman4d1c0182008-02-21 17:45:41 +000093 return Visit(ILE->getInit(0));
Devang Pateleae15602008-02-05 02:39:50 +000094 const llvm::Type *ElemTy = AType->getElementType();
95 unsigned NumElements = AType->getNumElements();
96
97 // Initialising an array requires us to automatically
98 // initialise any elements that have not been initialised explicitly
99 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
100
101 // Copy initializer elements.
102 unsigned i = 0;
Eli Friedmana04a1532008-05-30 19:58:50 +0000103 bool RewriteType = false;
Devang Pateleae15602008-02-05 02:39:50 +0000104 for (; i < NumInitableElts; ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000105 llvm::Constant *C = Visit(ILE->getInit(i));
Eli Friedmana04a1532008-05-30 19:58:50 +0000106 RewriteType |= (C->getType() != ElemTy);
Devang Pateleae15602008-02-05 02:39:50 +0000107 Elts.push_back(C);
108 }
Eli Friedmana04a1532008-05-30 19:58:50 +0000109
Devang Pateleae15602008-02-05 02:39:50 +0000110 // Initialize remaining array elements.
111 for (; i < NumElements; ++i)
112 Elts.push_back(llvm::Constant::getNullValue(ElemTy));
113
Eli Friedmana04a1532008-05-30 19:58:50 +0000114 if (RewriteType) {
115 // FIXME: Try to avoid packing the array
116 std::vector<const llvm::Type*> Types;
117 for (unsigned i = 0; i < Elts.size(); ++i)
118 Types.push_back(Elts[i]->getType());
119 const llvm::StructType *SType = llvm::StructType::get(Types, true);
120 return llvm::ConstantStruct::get(SType, Elts);
121 }
122
Devang Pateleae15602008-02-05 02:39:50 +0000123 return llvm::ConstantArray::get(AType, Elts);
124 }
125
Eli Friedmana8234002008-05-30 10:24:46 +0000126 void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
127 FieldDecl* Field, Expr* E) {
128 // Calculate the value to insert
129 llvm::Constant *C = Visit(E);
130 llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
131 if (!CI) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000132 CGM.ErrorUnsupported(E, "bitfield initialization");
Eli Friedmana8234002008-05-30 10:24:46 +0000133 return;
134 }
135 llvm::APInt V = CI->getValue();
Devang Pateleae15602008-02-05 02:39:50 +0000136
Eli Friedmana8234002008-05-30 10:24:46 +0000137 // Calculate information about the relevant field
138 const llvm::Type* Ty = CI->getType();
Daniel Dunbar9ca8bb02008-08-10 07:00:24 +0000139 const llvm::TargetData &TD = CGM.getTypes().getTargetData();
140 unsigned size = TD.getTypeStoreSizeInBits(Ty);
Eli Friedmana8234002008-05-30 10:24:46 +0000141 unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size;
142 CodeGenTypes::BitFieldInfo bitFieldInfo =
143 CGM.getTypes().getBitFieldInfo(Field);
144 fieldOffset += bitFieldInfo.Begin;
145
146 // Find where to start the insertion
147 // FIXME: This is O(n^2) in the number of bit-fields!
148 // FIXME: This won't work if the struct isn't completely packed!
149 unsigned offset = 0, i = 0;
150 while (offset < (fieldOffset & -8))
Daniel Dunbar9ca8bb02008-08-10 07:00:24 +0000151 offset += TD.getTypeStoreSizeInBits(Elts[i++]->getType());
152
153 // Advance over 0 sized elements (must terminate in bounds since
154 // the bitfield must have a size).
155 while (TD.getTypeStoreSizeInBits(Elts[i]->getType()) == 0)
156 ++i;
Eli Friedmana8234002008-05-30 10:24:46 +0000157
Daniel Dunbar1f3846e2008-07-17 21:07:48 +0000158 // Promote the size of V if necessary
159 // FIXME: This should never occur, but currently it can because
160 // initializer constants are cast to bool, and because clang is
161 // not enforcing bitfield width limits.
162 if (bitFieldInfo.Size > V.getBitWidth())
163 V.zext(bitFieldInfo.Size);
164
Eli Friedmana8234002008-05-30 10:24:46 +0000165 // Insert the bits into the struct
166 // FIXME: This algorthm is only correct on X86!
167 // FIXME: THis algorthm assumes bit-fields only have byte-size elements!
168 unsigned bitsToInsert = bitFieldInfo.Size;
169 unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert);
170 unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7);
171 do {
172 llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte);
173 Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC);
174 ++i;
175 V = V.lshr(curBits);
176 bitsToInsert -= curBits;
177
178 if (!bitsToInsert)
179 break;
180
181 curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
182 byte = V.getLoBits(curBits).getZExtValue();
183 } while (true);
184 }
185
186 llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
187 const llvm::StructType *SType =
188 cast<llvm::StructType>(ConvertType(ILE->getType()));
Eli Friedman017cbdf2008-05-20 00:11:07 +0000189 RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
Devang Pateleae15602008-02-05 02:39:50 +0000190 std::vector<llvm::Constant*> Elts;
Devang Pateleae15602008-02-05 02:39:50 +0000191
Eli Friedman017cbdf2008-05-20 00:11:07 +0000192 // Initialize the whole structure to zero.
193 for (unsigned i = 0; i < SType->getNumElements(); ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000194 const llvm::Type *FieldTy = SType->getElementType(i);
195 Elts.push_back(llvm::Constant::getNullValue(FieldTy));
196 }
Eli Friedman017cbdf2008-05-20 00:11:07 +0000197
198 // Copy initializer elements. Skip padding fields.
199 unsigned EltNo = 0; // Element no in ILE
200 int FieldNo = 0; // Field no in RecordDecl
Eli Friedmana04a1532008-05-30 19:58:50 +0000201 bool RewriteType = false;
Eli Friedman017cbdf2008-05-20 00:11:07 +0000202 while (EltNo < ILE->getNumInits() && FieldNo < RD->getNumMembers()) {
203 FieldDecl* curField = RD->getMember(FieldNo);
204 FieldNo++;
205 if (!curField->getIdentifier())
206 continue;
207
Eli Friedman017cbdf2008-05-20 00:11:07 +0000208 if (curField->isBitField()) {
Eli Friedmana8234002008-05-30 10:24:46 +0000209 InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(EltNo));
Eli Friedman017cbdf2008-05-20 00:11:07 +0000210 } else {
Eli Friedmana04a1532008-05-30 19:58:50 +0000211 unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(curField);
212 llvm::Constant* C = Visit(ILE->getInit(EltNo));
213 RewriteType |= (C->getType() != Elts[FieldNo]->getType());
214 Elts[FieldNo] = C;
Eli Friedman017cbdf2008-05-20 00:11:07 +0000215 }
216 EltNo++;
217 }
218
Eli Friedmana04a1532008-05-30 19:58:50 +0000219 if (RewriteType) {
220 // FIXME: Make this work for non-packed structs
221 assert(SType->isPacked() && "Cannot recreate unpacked structs");
222 std::vector<const llvm::Type*> Types;
223 for (unsigned i = 0; i < Elts.size(); ++i)
224 Types.push_back(Elts[i]->getType());
225 SType = llvm::StructType::get(Types, true);
226 }
227
Devang Pateleae15602008-02-05 02:39:50 +0000228 return llvm::ConstantStruct::get(SType, Elts);
229 }
230
Eli Friedmana8234002008-05-30 10:24:46 +0000231 llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
Eli Friedmana8234002008-05-30 10:24:46 +0000232 RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
233 const llvm::Type *Ty = ConvertType(ILE->getType());
234
235 // Find the field decl we're initializing, if any
236 int FieldNo = 0; // Field no in RecordDecl
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000237 FieldDecl* curField = 0;
238 while (FieldNo < RD->getNumMembers()) {
Eli Friedmana8234002008-05-30 10:24:46 +0000239 curField = RD->getMember(FieldNo);
240 FieldNo++;
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000241 if (curField->getIdentifier())
242 break;
243 }
Eli Friedmana8234002008-05-30 10:24:46 +0000244
Eli Friedmanc55f98d2008-06-06 20:12:37 +0000245 if (!curField || !curField->getIdentifier() || ILE->getNumInits() == 0)
Eli Friedmana8234002008-05-30 10:24:46 +0000246 return llvm::Constant::getNullValue(Ty);
247
248 if (curField->isBitField()) {
249 // Create a dummy struct for bit-field insertion
250 unsigned NumElts = CGM.getTargetData().getABITypeSize(Ty) / 8;
251 llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
252 std::vector<llvm::Constant*> Elts(NumElts, NV);
253
254 InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
255 const llvm::ArrayType *RetTy =
256 llvm::ArrayType::get(NV->getType(), NumElts);
257 return llvm::ConstantArray::get(RetTy, Elts);
258 }
259
260 llvm::Constant *C = Visit(ILE->getInit(0));
261
262 // Build a struct with the union sub-element as the first member,
263 // and padded to the appropriate size
264 std::vector<llvm::Constant*> Elts;
265 std::vector<const llvm::Type*> Types;
266 Elts.push_back(C);
267 Types.push_back(C->getType());
268 unsigned CurSize = CGM.getTargetData().getTypeStoreSize(C->getType());
269 unsigned TotalSize = CGM.getTargetData().getTypeStoreSize(Ty);
270 while (CurSize < TotalSize) {
271 Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
272 Types.push_back(llvm::Type::Int8Ty);
273 CurSize++;
274 }
275
276 // This always generates a packed struct
277 // FIXME: Try to generate an unpacked struct when we can
278 llvm::StructType* STy = llvm::StructType::get(Types, true);
279 return llvm::ConstantStruct::get(STy, Elts);
280 }
281
282 llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
283 const llvm::VectorType *VType =
284 cast<llvm::VectorType>(ConvertType(ILE->getType()));
Eli Friedmancfb313b2008-05-30 12:02:14 +0000285 const llvm::Type *ElemTy = VType->getElementType();
286 std::vector<llvm::Constant*> Elts;
Devang Pateleae15602008-02-05 02:39:50 +0000287 unsigned NumElements = VType->getNumElements();
Eli Friedmancfb313b2008-05-30 12:02:14 +0000288 unsigned NumInitElements = ILE->getNumInits();
Devang Pateleae15602008-02-05 02:39:50 +0000289
Eli Friedmancfb313b2008-05-30 12:02:14 +0000290 unsigned NumInitableElts = std::min(NumInitElements, NumElements);
291
Devang Pateleae15602008-02-05 02:39:50 +0000292 // Copy initializer elements.
293 unsigned i = 0;
Eli Friedmancfb313b2008-05-30 12:02:14 +0000294 for (; i < NumInitableElts; ++i) {
Devang Pateleae15602008-02-05 02:39:50 +0000295 llvm::Constant *C = Visit(ILE->getInit(i));
Devang Pateleae15602008-02-05 02:39:50 +0000296 Elts.push_back(C);
297 }
298
Eli Friedmancfb313b2008-05-30 12:02:14 +0000299 for (; i < NumElements; ++i)
300 Elts.push_back(llvm::Constant::getNullValue(ElemTy));
301
Devang Pateleae15602008-02-05 02:39:50 +0000302 return llvm::ConstantVector::get(VType, Elts);
303 }
304
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000305 llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
Eli Friedmana8234002008-05-30 10:24:46 +0000306 if (ILE->getType()->isScalarType()) {
307 // We have a scalar in braces. Just use the first element.
308 if (ILE->getNumInits() > 0)
Anders Carlssonf6884ac2008-01-29 01:15:48 +0000309 return Visit(ILE->getInit(0));
Eli Friedmana8234002008-05-30 10:24:46 +0000310
311 const llvm::Type* RetTy = CGM.getTypes().ConvertType(ILE->getType());
312 return llvm::Constant::getNullValue(RetTy);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000313 }
Devang Patel47fb6972008-01-29 23:23:18 +0000314
Eli Friedmana8234002008-05-30 10:24:46 +0000315 if (ILE->getType()->isArrayType())
316 return EmitArrayInitialization(ILE);
Devang Patel47fb6972008-01-29 23:23:18 +0000317
Eli Friedmana8234002008-05-30 10:24:46 +0000318 if (ILE->getType()->isStructureType())
319 return EmitStructInitialization(ILE);
320
321 if (ILE->getType()->isUnionType())
322 return EmitUnionInitialization(ILE);
323
324 if (ILE->getType()->isVectorType())
325 return EmitVectorInitialization(ILE);
326
Devang Pateleae15602008-02-05 02:39:50 +0000327 assert(0 && "Unable to handle InitListExpr");
Chris Lattner2b94fe32008-03-01 08:45:05 +0000328 // Get rid of control reaches end of void function warning.
329 // Not reached.
330 return 0;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000331 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000332
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000333 llvm::Constant *VisitImplicitCastExpr(ImplicitCastExpr *ICExpr) {
Eli Friedman3b707e72008-02-21 17:57:49 +0000334 Expr* SExpr = ICExpr->getSubExpr();
335 QualType SType = SExpr->getType();
336 llvm::Constant *C; // the intermediate expression
337 QualType T; // the type of the intermediate expression
338 if (SType->isArrayType()) {
339 // Arrays decay to a pointer to the first element
340 // VLAs would require special handling, but they can't occur here
341 C = EmitLValue(SExpr);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000342 llvm::Constant *Idx0 = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000343 llvm::Constant *Ops[] = {Idx0, Idx0};
344 C = llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000345 T = CGM.getContext().getArrayDecayedType(SType);
Eli Friedman3b707e72008-02-21 17:57:49 +0000346 } else if (SType->isFunctionType()) {
347 // Function types decay to a pointer to the function
348 C = EmitLValue(SExpr);
349 T = CGM.getContext().getPointerType(SType);
350 } else {
351 C = Visit(SExpr);
352 T = SType;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000353 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000354
355 // Perform the conversion; note that an implicit cast can both promote
356 // and convert an array/function
357 return EmitConversion(C, T, ICExpr->getType());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000358 }
Eli Friedman3b707e72008-02-21 17:57:49 +0000359
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000360 llvm::Constant *VisitStringLiteral(StringLiteral *E) {
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000361 assert(!E->getType()->isPointerType() && "Strings are always arrays");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000362
363 // Otherwise this must be a string initializing an array in a static
364 // initializer. Don't emit it as the address of the string, emit the string
365 // data itself as an inline array.
Daniel Dunbar61432932008-08-13 23:20:05 +0000366 return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000367 }
368
369 llvm::Constant *VisitDeclRefExpr(DeclRefExpr *E) {
370 const ValueDecl *Decl = E->getDecl();
Anders Carlssona3881fc2008-01-29 01:28:48 +0000371 if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(Decl))
372 return llvm::ConstantInt::get(EC->getInitVal());
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000373 assert(0 && "Unsupported decl ref type!");
374 return 0;
375 }
376
377 llvm::Constant *VisitSizeOfAlignOfTypeExpr(const SizeOfAlignOfTypeExpr *E) {
378 return EmitSizeAlignOf(E->getArgumentType(), E->getType(), E->isSizeOf());
379 }
380
381 // Unary operators
382 llvm::Constant *VisitUnaryPlus(const UnaryOperator *E) {
383 return Visit(E->getSubExpr());
384 }
385 llvm::Constant *VisitUnaryMinus(const UnaryOperator *E) {
386 return llvm::ConstantExpr::getNeg(Visit(E->getSubExpr()));
387 }
388 llvm::Constant *VisitUnaryNot(const UnaryOperator *E) {
389 return llvm::ConstantExpr::getNot(Visit(E->getSubExpr()));
390 }
391 llvm::Constant *VisitUnaryLNot(const UnaryOperator *E) {
392 llvm::Constant *SubExpr = Visit(E->getSubExpr());
393
394 if (E->getSubExpr()->getType()->isRealFloatingType()) {
395 // Compare against 0.0 for fp scalars.
396 llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType());
397 SubExpr = llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UEQ, SubExpr,
398 Zero);
399 } else {
400 assert((E->getSubExpr()->getType()->isIntegerType() ||
401 E->getSubExpr()->getType()->isPointerType()) &&
402 "Unknown scalar type to convert");
403 // Compare against an integer or pointer null.
404 llvm::Constant *Zero = llvm::Constant::getNullValue(SubExpr->getType());
405 SubExpr = llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, SubExpr,
406 Zero);
407 }
408
409 return llvm::ConstantExpr::getZExt(SubExpr, ConvertType(E->getType()));
410 }
411 llvm::Constant *VisitUnarySizeOf(const UnaryOperator *E) {
412 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), true);
413 }
414 llvm::Constant *VisitUnaryAlignOf(const UnaryOperator *E) {
415 return EmitSizeAlignOf(E->getSubExpr()->getType(), E->getType(), false);
416 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000417 llvm::Constant *VisitUnaryAddrOf(const UnaryOperator *E) {
418 return EmitLValue(E->getSubExpr());
419 }
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000420 llvm::Constant *VisitUnaryOffsetOf(const UnaryOperator *E) {
421 int64_t Val = E->evaluateOffsetOf(CGM.getContext());
422
423 assert(E->getType()->isIntegerType() && "Result type must be an integer!");
424
Chris Lattner98be4942008-03-05 18:54:05 +0000425 uint32_t ResultWidth =
426 static_cast<uint32_t>(CGM.getContext().getTypeSize(E->getType()));
Anders Carlsson5a1deb82008-01-29 15:56:48 +0000427 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
428 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000429
430 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
431 return Visit(E->getSubExpr());
432 }
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000433
Anders Carlssonce6237e2008-01-29 01:33:32 +0000434 // Binary operators
435 llvm::Constant *VisitBinOr(const BinaryOperator *E) {
436 llvm::Constant *LHS = Visit(E->getLHS());
437 llvm::Constant *RHS = Visit(E->getRHS());
438
439 return llvm::ConstantExpr::getOr(LHS, RHS);
440 }
Anders Carlsson7be3d012008-02-05 20:54:21 +0000441 llvm::Constant *VisitBinSub(const BinaryOperator *E) {
442 llvm::Constant *LHS = Visit(E->getLHS());
443 llvm::Constant *RHS = Visit(E->getRHS());
444
445 if (!isa<llvm::PointerType>(RHS->getType())) {
446 // pointer - int
447 if (isa<llvm::PointerType>(LHS->getType())) {
448 llvm::Constant *Idx = llvm::ConstantExpr::getNeg(RHS);
449
450 return llvm::ConstantExpr::getGetElementPtr(LHS, &Idx, 1);
451 }
452
453 // int - int
454 return llvm::ConstantExpr::getSub(LHS, RHS);
455 }
Nuno Lopes77e1dfc2008-06-06 16:41:08 +0000456
457 assert(isa<llvm::PointerType>(LHS->getType()));
458
459 const llvm::Type *ResultType = ConvertType(E->getType());
460 const QualType Type = E->getLHS()->getType();
461 const QualType ElementType = Type->getAsPointerType()->getPointeeType();
462
463 LHS = llvm::ConstantExpr::getPtrToInt(LHS, ResultType);
464 RHS = llvm::ConstantExpr::getPtrToInt(RHS, ResultType);
465
466 llvm::Constant *sub = llvm::ConstantExpr::getSub(LHS, RHS);
467 llvm::Constant *size = EmitSizeAlignOf(ElementType, E->getType(), true);
468 return llvm::ConstantExpr::getSDiv(sub, size);
Anders Carlsson7be3d012008-02-05 20:54:21 +0000469 }
470
471 llvm::Constant *VisitBinShl(const BinaryOperator *E) {
472 llvm::Constant *LHS = Visit(E->getLHS());
473 llvm::Constant *RHS = Visit(E->getRHS());
Anders Carlssonce6237e2008-01-29 01:33:32 +0000474
Anders Carlsson7be3d012008-02-05 20:54:21 +0000475 // LLVM requires the LHS and RHS to be the same type: promote or truncate the
476 // RHS to the same size as the LHS.
477 if (LHS->getType() != RHS->getType())
478 RHS = llvm::ConstantExpr::getIntegerCast(RHS, LHS->getType(), false);
479
480 return llvm::ConstantExpr::getShl(LHS, RHS);
481 }
482
483 llvm::Constant *VisitBinMul(const BinaryOperator *E) {
484 llvm::Constant *LHS = Visit(E->getLHS());
485 llvm::Constant *RHS = Visit(E->getRHS());
486
487 return llvm::ConstantExpr::getMul(LHS, RHS);
488 }
489
490 llvm::Constant *VisitBinDiv(const BinaryOperator *E) {
491 llvm::Constant *LHS = Visit(E->getLHS());
492 llvm::Constant *RHS = Visit(E->getRHS());
493
494 if (LHS->getType()->isFPOrFPVector())
495 return llvm::ConstantExpr::getFDiv(LHS, RHS);
496 else if (E->getType()->isUnsignedIntegerType())
497 return llvm::ConstantExpr::getUDiv(LHS, RHS);
498 else
499 return llvm::ConstantExpr::getSDiv(LHS, RHS);
500 }
501
502 llvm::Constant *VisitBinAdd(const BinaryOperator *E) {
503 llvm::Constant *LHS = Visit(E->getLHS());
504 llvm::Constant *RHS = Visit(E->getRHS());
505
506 if (!E->getType()->isPointerType())
507 return llvm::ConstantExpr::getAdd(LHS, RHS);
508
Anders Carlsson87a98752008-02-06 07:23:14 +0000509 llvm::Constant *Ptr, *Idx;
510 if (isa<llvm::PointerType>(LHS->getType())) { // pointer + int
511 Ptr = LHS;
512 Idx = RHS;
513 } else { // int + pointer
514 Ptr = RHS;
515 Idx = LHS;
516 }
517
518 return llvm::ConstantExpr::getGetElementPtr(Ptr, &Idx, 1);
Anders Carlsson7be3d012008-02-05 20:54:21 +0000519 }
520
521 llvm::Constant *VisitBinAnd(const BinaryOperator *E) {
522 llvm::Constant *LHS = Visit(E->getLHS());
523 llvm::Constant *RHS = Visit(E->getRHS());
524
525 return llvm::ConstantExpr::getAnd(LHS, RHS);
526 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000527
Eli Friedmanb8965962008-05-29 15:17:49 +0000528 llvm::Constant *EmitCmp(const BinaryOperator *E,
529 llvm::CmpInst::Predicate SignedPred,
530 llvm::CmpInst::Predicate UnsignedPred,
531 llvm::CmpInst::Predicate FloatPred) {
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000532 llvm::Constant *LHS = Visit(E->getLHS());
533 llvm::Constant *RHS = Visit(E->getRHS());
Eli Friedmanb8965962008-05-29 15:17:49 +0000534 llvm::Constant *Result;
535 if (LHS->getType()->isInteger() ||
536 isa<llvm::PointerType>(LHS->getType())) {
537 if (E->getLHS()->getType()->isSignedIntegerType())
538 Result = llvm::ConstantExpr::getICmp(SignedPred, LHS, RHS);
539 else
540 Result = llvm::ConstantExpr::getICmp(UnsignedPred, LHS, RHS);
541 } else if (LHS->getType()->isFloatingPoint()) {
542 Result = llvm::ConstantExpr::getFCmp(FloatPred, LHS, RHS);
543 } else {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000544 CGM.ErrorUnsupported(E, "constant expression");
Eli Friedmanb8965962008-05-29 15:17:49 +0000545 Result = llvm::ConstantInt::getFalse();
546 }
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000547
548 const llvm::Type* ResultType = ConvertType(E->getType());
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000549 return llvm::ConstantExpr::getZExtOrBitCast(Result, ResultType);
550 }
551
Eli Friedmanb8965962008-05-29 15:17:49 +0000552 llvm::Constant *VisitBinNE(const BinaryOperator *E) {
553 return EmitCmp(E, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_NE,
554 llvm::CmpInst::FCMP_ONE);
555 }
556
557 llvm::Constant *VisitBinEQ(const BinaryOperator *E) {
558 return EmitCmp(E, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_EQ,
559 llvm::CmpInst::FCMP_OEQ);
560 }
561
562 llvm::Constant *VisitBinLT(const BinaryOperator *E) {
563 return EmitCmp(E, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_ULT,
564 llvm::CmpInst::FCMP_OLT);
565 }
566
567 llvm::Constant *VisitBinLE(const BinaryOperator *E) {
568 return EmitCmp(E, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_ULE,
569 llvm::CmpInst::FCMP_OLE);
570 }
571
572 llvm::Constant *VisitBinGT(const BinaryOperator *E) {
573 return EmitCmp(E, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_UGT,
574 llvm::CmpInst::FCMP_OGT);
575 }
576
577 llvm::Constant *VisitBinGE(const BinaryOperator *E) {
578 return EmitCmp(E, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGE,
579 llvm::CmpInst::FCMP_OGE);
580 }
581
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000582 llvm::Constant *VisitConditionalOperator(const ConditionalOperator *E) {
583 llvm::Constant *Cond = Visit(E->getCond());
584 llvm::Constant *CondVal = EmitConversionToBool(Cond, E->getType());
585 llvm::ConstantInt *CondValInt = dyn_cast<llvm::ConstantInt>(CondVal);
586 if (!CondValInt) {
Daniel Dunbar488e9932008-08-16 00:56:44 +0000587 CGM.ErrorUnsupported(E, "constant expression");
Eli Friedmanbc5ed6e2008-05-29 11:22:45 +0000588 return llvm::Constant::getNullValue(ConvertType(E->getType()));
589 }
590 if (CondValInt->isOne()) {
591 if (E->getLHS())
592 return Visit(E->getLHS());
593 return Cond;
594 }
595
596 return Visit(E->getRHS());
597 }
Anders Carlsson7be3d012008-02-05 20:54:21 +0000598
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000599 // Utility methods
600 const llvm::Type *ConvertType(QualType T) {
601 return CGM.getTypes().ConvertType(T);
602 }
603
604 llvm::Constant *EmitConversionToBool(llvm::Constant *Src, QualType SrcType) {
605 assert(SrcType->isCanonical() && "EmitConversion strips typedefs");
606
607 if (SrcType->isRealFloatingType()) {
608 // Compare against 0.0 for fp scalars.
609 llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType());
610 return llvm::ConstantExpr::getFCmp(llvm::FCmpInst::FCMP_UNE, Src, Zero);
611 }
612
613 assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
614 "Unknown scalar type to convert");
615
616 // Compare against an integer or pointer null.
617 llvm::Constant *Zero = llvm::Constant::getNullValue(Src->getType());
618 return llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, Src, Zero);
619 }
620
621 llvm::Constant *EmitConversion(llvm::Constant *Src, QualType SrcType,
622 QualType DstType) {
Chris Lattnerd394cc62008-07-26 22:39:33 +0000623 SrcType = CGM.getContext().getCanonicalType(SrcType);
624 DstType = CGM.getContext().getCanonicalType(DstType);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000625 if (SrcType == DstType) return Src;
626
627 // Handle conversions to bool first, they are special: comparisons against 0.
628 if (DstType->isBooleanType())
629 return EmitConversionToBool(Src, SrcType);
630
631 const llvm::Type *DstTy = ConvertType(DstType);
632
633 // Ignore conversions like int -> uint.
634 if (Src->getType() == DstTy)
635 return Src;
636
637 // Handle pointer conversions next: pointers can only be converted to/from
638 // other pointers and integers.
Eli Friedmanc53296e2008-06-07 17:27:51 +0000639 if (isa<llvm::PointerType>(DstTy)) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000640 // The source value may be an integer, or a pointer.
641 if (isa<llvm::PointerType>(Src->getType()))
642 return llvm::ConstantExpr::getBitCast(Src, DstTy);
Anders Carlsson84005b42008-01-26 04:30:23 +0000643 assert(SrcType->isIntegerType() &&"Not ptr->ptr or int->ptr conversion?");
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000644 return llvm::ConstantExpr::getIntToPtr(Src, DstTy);
645 }
646
Eli Friedmanc53296e2008-06-07 17:27:51 +0000647 if (isa<llvm::PointerType>(Src->getType())) {
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000648 // Must be an ptr to int cast.
649 assert(isa<llvm::IntegerType>(DstTy) && "not ptr->int?");
650 return llvm::ConstantExpr::getPtrToInt(Src, DstTy);
651 }
652
653 // A scalar source can be splatted to a vector of the same element type
654 if (isa<llvm::VectorType>(DstTy) && !isa<VectorType>(SrcType)) {
655 const llvm::VectorType *VT = cast<llvm::VectorType>(DstTy);
656 assert((VT->getElementType() == Src->getType()) &&
657 "Vector element type must match scalar type to splat.");
658 unsigned NumElements = DstType->getAsVectorType()->getNumElements();
659 llvm::SmallVector<llvm::Constant*, 16> Elements;
660 for (unsigned i = 0; i < NumElements; i++)
661 Elements.push_back(Src);
662
663 return llvm::ConstantVector::get(&Elements[0], NumElements);
664 }
665
666 if (isa<llvm::VectorType>(Src->getType()) ||
667 isa<llvm::VectorType>(DstTy)) {
668 return llvm::ConstantExpr::getBitCast(Src, DstTy);
669 }
670
671 // Finally, we have the arithmetic types: real int/float.
672 if (isa<llvm::IntegerType>(Src->getType())) {
673 bool InputSigned = SrcType->isSignedIntegerType();
674 if (isa<llvm::IntegerType>(DstTy))
675 return llvm::ConstantExpr::getIntegerCast(Src, DstTy, InputSigned);
676 else if (InputSigned)
677 return llvm::ConstantExpr::getSIToFP(Src, DstTy);
678 else
679 return llvm::ConstantExpr::getUIToFP(Src, DstTy);
680 }
681
682 assert(Src->getType()->isFloatingPoint() && "Unknown real conversion");
683 if (isa<llvm::IntegerType>(DstTy)) {
684 if (DstType->isSignedIntegerType())
685 return llvm::ConstantExpr::getFPToSI(Src, DstTy);
686 else
687 return llvm::ConstantExpr::getFPToUI(Src, DstTy);
688 }
689
690 assert(DstTy->isFloatingPoint() && "Unknown real conversion");
691 if (DstTy->getTypeID() < Src->getType()->getTypeID())
692 return llvm::ConstantExpr::getFPTrunc(Src, DstTy);
693 else
694 return llvm::ConstantExpr::getFPExtend(Src, DstTy);
695 }
696
697 llvm::Constant *EmitSizeAlignOf(QualType TypeToSize,
698 QualType RetType, bool isSizeOf) {
699 std::pair<uint64_t, unsigned> Info =
Chris Lattner98be4942008-03-05 18:54:05 +0000700 CGM.getContext().getTypeInfo(TypeToSize);
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000701
702 uint64_t Val = isSizeOf ? Info.first : Info.second;
703 Val /= 8; // Return size in bytes, not bits.
704
705 assert(RetType->isIntegerType() && "Result type must be an integer!");
706
Chris Lattner98be4942008-03-05 18:54:05 +0000707 uint32_t ResultWidth =
708 static_cast<uint32_t>(CGM.getContext().getTypeSize(RetType));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000709 return llvm::ConstantInt::get(llvm::APInt(ResultWidth, Val));
710 }
Anders Carlsson84005b42008-01-26 04:30:23 +0000711
712 llvm::Constant *EmitLValue(Expr *E) {
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000713 switch (E->getStmtClass()) {
Eli Friedman546d94c2008-02-11 00:23:10 +0000714 default: break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000715 case Expr::ParenExprClass:
716 // Elide parenthesis
717 return EmitLValue(cast<ParenExpr>(E)->getSubExpr());
718 case Expr::CompoundLiteralExprClass: {
719 // Note that due to the nature of compound literals, this is guaranteed
720 // to be the only use of the variable, so we just generate it here.
Anders Carlsson84005b42008-01-26 04:30:23 +0000721 CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
722 llvm::Constant* C = Visit(CLE->getInitializer());
Chris Lattnera7ad98f2008-02-11 00:02:17 +0000723 C = new llvm::GlobalVariable(C->getType(),E->getType().isConstQualified(),
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000724 llvm::GlobalValue::InternalLinkage,
725 C, ".compoundliteral", &CGM.getModule());
726 return C;
Anders Carlsson84005b42008-01-26 04:30:23 +0000727 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000728 case Expr::DeclRefExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000729 ValueDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000730 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000731 return CGM.GetAddrOfFunction(FD);
Steve Naroff248a7532008-04-15 22:42:06 +0000732 if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
733 if (VD->isFileVarDecl())
Daniel Dunbarbd012ff2008-07-29 23:18:29 +0000734 return CGM.GetAddrOfGlobalVar(VD);
Steve Naroff248a7532008-04-15 22:42:06 +0000735 else if (VD->isBlockVarDecl()) {
736 assert(CGF && "Can't access static local vars without CGF");
737 return CGF->GetAddrOfStaticLocalVar(VD);
738 }
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000739 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000740 break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000741 }
742 case Expr::MemberExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000743 MemberExpr* ME = cast<MemberExpr>(E);
Anders Carlsson84005b42008-01-26 04:30:23 +0000744 llvm::Constant *Base;
745 if (ME->isArrow())
746 Base = Visit(ME->getBase());
747 else
748 Base = EmitLValue(ME->getBase());
Lauro Ramos Venancio5bfe18c2008-01-30 21:23:20 +0000749
750 unsigned FieldNumber = CGM.getTypes().getLLVMFieldNo(ME->getMemberDecl());
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000751 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
752 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
753 FieldNumber);
754 llvm::Value *Ops[] = {Zero, Idx};
755 return llvm::ConstantExpr::getGetElementPtr(Base, Ops, 2);
756 }
757 case Expr::ArraySubscriptExprClass: {
Anders Carlsson84005b42008-01-26 04:30:23 +0000758 ArraySubscriptExpr* ASExpr = cast<ArraySubscriptExpr>(E);
759 llvm::Constant *Base = Visit(ASExpr->getBase());
760 llvm::Constant *Index = Visit(ASExpr->getIdx());
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000761 assert(!ASExpr->getBase()->getType()->isVectorType() &&
762 "Taking the address of a vector component is illegal!");
763 return llvm::ConstantExpr::getGetElementPtr(Base, &Index, 1);
764 }
Daniel Dunbar61432932008-08-13 23:20:05 +0000765 case Expr::StringLiteralClass:
766 return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
Anders Carlsson84005b42008-01-26 04:30:23 +0000767 case Expr::UnaryOperatorClass: {
768 UnaryOperator *Exp = cast<UnaryOperator>(E);
769 switch (Exp->getOpcode()) {
Eli Friedman546d94c2008-02-11 00:23:10 +0000770 default: break;
771 case UnaryOperator::Extension:
Anders Carlsson84005b42008-01-26 04:30:23 +0000772 // Extension is just a wrapper for expressions
773 return EmitLValue(Exp->getSubExpr());
774 case UnaryOperator::Real:
775 case UnaryOperator::Imag: {
776 // The address of __real or __imag is just a GEP off the address
777 // of the internal expression
778 llvm::Constant* C = EmitLValue(Exp->getSubExpr());
779 llvm::Constant *Zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
780 llvm::Constant *Idx = llvm::ConstantInt::get(llvm::Type::Int32Ty,
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000781 Exp->getOpcode() == UnaryOperator::Imag);
Anders Carlsson84005b42008-01-26 04:30:23 +0000782 llvm::Value *Ops[] = {Zero, Idx};
783 return llvm::ConstantExpr::getGetElementPtr(C, Ops, 2);
784 }
785 case UnaryOperator::Deref:
786 // The address of a deref is just the value of the expression
787 return Visit(Exp->getSubExpr());
788 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000789 break;
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000790 }
Eli Friedman546d94c2008-02-11 00:23:10 +0000791 }
Daniel Dunbar488e9932008-08-16 00:56:44 +0000792 CGM.ErrorUnsupported(E, "constant l-value expression");
Eli Friedman546d94c2008-02-11 00:23:10 +0000793 llvm::Type *Ty = llvm::PointerType::getUnqual(ConvertType(E->getType()));
794 return llvm::UndefValue::get(Ty);
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000795 }
Anders Carlssone3d3b7d2008-01-26 02:08:50 +0000796
797};
798
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000799} // end anonymous namespace.
800
801
Lauro Ramos Venancio81373352008-02-26 21:41:45 +0000802llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
Chris Lattner96196622008-07-26 22:37:01 +0000803 CodeGenFunction *CGF) {
804 QualType type = Context.getCanonicalType(E->getType());
Eli Friedman7dfa6392008-06-01 15:31:44 +0000805
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000806 if (type->isIntegerType()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000807 llvm::APSInt Value(static_cast<uint32_t>(Context.getTypeSize(type)));
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000808 if (E->isIntegerConstantExpr(Value, Context)) {
809 return llvm::ConstantInt::get(Value);
810 }
811 }
Eli Friedman7dfa6392008-06-01 15:31:44 +0000812
813 llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
814 if (C->getType() == llvm::Type::Int1Ty) {
815 const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
816 C = llvm::ConstantExpr::getZExt(C, BoolTy);
817 }
818 return C;
Anders Carlsson3b1d57b2008-01-26 01:36:00 +0000819}