1 //===--- 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"
16 #include "CGObjCRuntime.h"
17 #include "clang/AST/APValue.h"
18 #include "clang/AST/ASTContext.h"
19 #include "clang/AST/StmtVisitor.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/GlobalVariable.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Target/TargetData.h"
25 using namespace clang;
26 using namespace CodeGen;
27 
28 namespace  {
29 class VISIBILITY_HIDDEN ConstExprEmitter :
30   public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
31   CodeGenModule &CGM;
32   CodeGenFunction *CGF;
33 public:
34   ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
35     : CGM(cgm), CGF(cgf) {
36   }
37 
38   //===--------------------------------------------------------------------===//
39   //                            Visitor Methods
40   //===--------------------------------------------------------------------===//
41 
42   llvm::Constant *VisitStmt(Stmt *S) {
43     return 0;
44   }
45 
46   llvm::Constant *VisitParenExpr(ParenExpr *PE) {
47     return Visit(PE->getSubExpr());
48   }
49 
50   llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
51     return Visit(E->getInitializer());
52   }
53 
54   llvm::Constant *VisitCastExpr(CastExpr* E) {
55     // GCC cast to union extension
56     if (E->getType()->isUnionType()) {
57       const llvm::Type *Ty = ConvertType(E->getType());
58       Expr *SubExpr = E->getSubExpr();
59       return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF),
60                        Ty);
61     }
62     if (CGM.getContext().getCanonicalType(E->getSubExpr()->getType()) ==
63         CGM.getContext().getCanonicalType(E->getType())) {
64       return Visit(E->getSubExpr());
65     }
66     return 0;
67   }
68 
69   llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
70     return Visit(DAE->getExpr());
71   }
72 
73   llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
74     std::vector<llvm::Constant*> Elts;
75     const llvm::ArrayType *AType =
76         cast<llvm::ArrayType>(ConvertType(ILE->getType()));
77     unsigned NumInitElements = ILE->getNumInits();
78     // FIXME: Check for wide strings
79     // FIXME: Check for NumInitElements exactly equal to 1??
80     if (NumInitElements > 0 &&
81         (isa<StringLiteral>(ILE->getInit(0)) ||
82          isa<ObjCEncodeExpr>(ILE->getInit(0))) &&
83         ILE->getType()->getArrayElementTypeNoTypeQual()->isCharType())
84       return Visit(ILE->getInit(0));
85     const llvm::Type *ElemTy = AType->getElementType();
86     unsigned NumElements = AType->getNumElements();
87 
88     // Initialising an array requires us to automatically
89     // initialise any elements that have not been initialised explicitly
90     unsigned NumInitableElts = std::min(NumInitElements, NumElements);
91 
92     // Copy initializer elements.
93     unsigned i = 0;
94     bool RewriteType = false;
95     for (; i < NumInitableElts; ++i) {
96       Expr *Init = ILE->getInit(i);
97       llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
98       if (!C)
99         return 0;
100       RewriteType |= (C->getType() != ElemTy);
101       Elts.push_back(C);
102     }
103 
104     // Initialize remaining array elements.
105     // FIXME: This doesn't handle member pointers correctly!
106     for (; i < NumElements; ++i)
107       Elts.push_back(llvm::Constant::getNullValue(ElemTy));
108 
109     if (RewriteType) {
110       // FIXME: Try to avoid packing the array
111       std::vector<const llvm::Type*> Types;
112       for (unsigned i = 0; i < Elts.size(); ++i)
113         Types.push_back(Elts[i]->getType());
114       const llvm::StructType *SType = llvm::StructType::get(Types, true);
115       return llvm::ConstantStruct::get(SType, Elts);
116     }
117 
118     return llvm::ConstantArray::get(AType, Elts);
119   }
120 
121   void InsertBitfieldIntoStruct(std::vector<llvm::Constant*>& Elts,
122                                 FieldDecl* Field, Expr* E) {
123     // Calculate the value to insert
124     llvm::Constant *C = CGM.EmitConstantExpr(E, Field->getType(), CGF);
125     if (!C)
126       return;
127 
128     llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
129     if (!CI) {
130       CGM.ErrorUnsupported(E, "bitfield initialization");
131       return;
132     }
133     llvm::APInt V = CI->getValue();
134 
135     // Calculate information about the relevant field
136     const llvm::Type* Ty = CI->getType();
137     const llvm::TargetData &TD = CGM.getTypes().getTargetData();
138     unsigned size = TD.getTypePaddedSizeInBits(Ty);
139     unsigned fieldOffset = CGM.getTypes().getLLVMFieldNo(Field) * size;
140     CodeGenTypes::BitFieldInfo bitFieldInfo =
141         CGM.getTypes().getBitFieldInfo(Field);
142     fieldOffset += bitFieldInfo.Begin;
143 
144     // Find where to start the insertion
145     // FIXME: This is O(n^2) in the number of bit-fields!
146     // FIXME: This won't work if the struct isn't completely packed!
147     unsigned offset = 0, i = 0;
148     while (offset < (fieldOffset & -8))
149       offset += TD.getTypePaddedSizeInBits(Elts[i++]->getType());
150 
151     // Advance over 0 sized elements (must terminate in bounds since
152     // the bitfield must have a size).
153     while (TD.getTypePaddedSizeInBits(Elts[i]->getType()) == 0)
154       ++i;
155 
156     // Promote the size of V if necessary
157     // FIXME: This should never occur, but currently it can because
158     // initializer constants are cast to bool, and because clang is
159     // not enforcing bitfield width limits.
160     if (bitFieldInfo.Size > V.getBitWidth())
161       V.zext(bitFieldInfo.Size);
162 
163     // Insert the bits into the struct
164     // FIXME: This algorthm is only correct on X86!
165     // FIXME: THis algorthm assumes bit-fields only have byte-size elements!
166     unsigned bitsToInsert = bitFieldInfo.Size;
167     unsigned curBits = std::min(8 - (fieldOffset & 7), bitsToInsert);
168     unsigned byte = V.getLoBits(curBits).getZExtValue() << (fieldOffset & 7);
169     do {
170       llvm::Constant* byteC = llvm::ConstantInt::get(llvm::Type::Int8Ty, byte);
171       Elts[i] = llvm::ConstantExpr::getOr(Elts[i], byteC);
172       ++i;
173       V = V.lshr(curBits);
174       bitsToInsert -= curBits;
175 
176       if (!bitsToInsert)
177         break;
178 
179       curBits = bitsToInsert > 8 ? 8 : bitsToInsert;
180       byte = V.getLoBits(curBits).getZExtValue();
181     } while (true);
182   }
183 
184   llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
185     const llvm::StructType *SType =
186         cast<llvm::StructType>(ConvertType(ILE->getType()));
187     RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
188     std::vector<llvm::Constant*> Elts;
189 
190     // Initialize the whole structure to zero.
191     // FIXME: This doesn't handle member pointers correctly!
192     for (unsigned i = 0; i < SType->getNumElements(); ++i) {
193       const llvm::Type *FieldTy = SType->getElementType(i);
194       Elts.push_back(llvm::Constant::getNullValue(FieldTy));
195     }
196 
197     // Copy initializer elements. Skip padding fields.
198     unsigned EltNo = 0;  // Element no in ILE
199     int FieldNo = 0; // Field no in RecordDecl
200     bool RewriteType = false;
201     for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
202                                  FieldEnd = RD->field_end(CGM.getContext());
203          EltNo < ILE->getNumInits() && Field != FieldEnd; ++Field) {
204       FieldNo++;
205       if (!Field->getIdentifier())
206         continue;
207 
208       if (Field->isBitField()) {
209         InsertBitfieldIntoStruct(Elts, *Field, ILE->getInit(EltNo));
210       } else {
211         unsigned FieldNo = CGM.getTypes().getLLVMFieldNo(*Field);
212         llvm::Constant *C = CGM.EmitConstantExpr(ILE->getInit(EltNo),
213                                                  Field->getType(), CGF);
214         if (!C) return 0;
215         RewriteType |= (C->getType() != Elts[FieldNo]->getType());
216         Elts[FieldNo] = C;
217       }
218       EltNo++;
219     }
220 
221     if (RewriteType) {
222       // FIXME: Make this work for non-packed structs
223       assert(SType->isPacked() && "Cannot recreate unpacked structs");
224       std::vector<const llvm::Type*> Types;
225       for (unsigned i = 0; i < Elts.size(); ++i)
226         Types.push_back(Elts[i]->getType());
227       SType = llvm::StructType::get(Types, true);
228     }
229 
230     return llvm::ConstantStruct::get(SType, Elts);
231   }
232 
233   llvm::Constant *EmitUnion(llvm::Constant *C, const llvm::Type *Ty) {
234     if (!C)
235       return 0;
236 
237     // Build a struct with the union sub-element as the first member,
238     // and padded to the appropriate size
239     std::vector<llvm::Constant*> Elts;
240     std::vector<const llvm::Type*> Types;
241     Elts.push_back(C);
242     Types.push_back(C->getType());
243     unsigned CurSize = CGM.getTargetData().getTypePaddedSize(C->getType());
244     unsigned TotalSize = CGM.getTargetData().getTypePaddedSize(Ty);
245     while (CurSize < TotalSize) {
246       Elts.push_back(llvm::Constant::getNullValue(llvm::Type::Int8Ty));
247       Types.push_back(llvm::Type::Int8Ty);
248       CurSize++;
249     }
250 
251     // This always generates a packed struct
252     // FIXME: Try to generate an unpacked struct when we can
253     llvm::StructType* STy = llvm::StructType::get(Types, true);
254     return llvm::ConstantStruct::get(STy, Elts);
255   }
256 
257   llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
258     const llvm::Type *Ty = ConvertType(ILE->getType());
259 
260     FieldDecl* curField = ILE->getInitializedFieldInUnion();
261     if (!curField) {
262       // There's no field to initialize, so value-initialize the union.
263 #ifndef NDEBUG
264       // Make sure that it's really an empty and not a failure of
265       // semantic analysis.
266       RecordDecl *RD = ILE->getType()->getAsRecordType()->getDecl();
267       for (RecordDecl::field_iterator Field = RD->field_begin(CGM.getContext()),
268                                    FieldEnd = RD->field_end(CGM.getContext());
269            Field != FieldEnd; ++Field)
270         assert(Field->isUnnamedBitfield() && "Only unnamed bitfields allowed");
271 #endif
272       return llvm::Constant::getNullValue(Ty);
273     }
274 
275     if (curField->isBitField()) {
276       // Create a dummy struct for bit-field insertion
277       unsigned NumElts = CGM.getTargetData().getTypePaddedSize(Ty) / 8;
278       llvm::Constant* NV = llvm::Constant::getNullValue(llvm::Type::Int8Ty);
279       std::vector<llvm::Constant*> Elts(NumElts, NV);
280 
281       InsertBitfieldIntoStruct(Elts, curField, ILE->getInit(0));
282       const llvm::ArrayType *RetTy =
283           llvm::ArrayType::get(NV->getType(), NumElts);
284       return llvm::ConstantArray::get(RetTy, Elts);
285     }
286 
287     llvm::Constant *InitElem;
288     if (ILE->getNumInits() > 0) {
289       Expr *Init = ILE->getInit(0);
290       InitElem = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
291     } else {
292       InitElem = CGM.EmitNullConstant(curField->getType());
293     }
294     return EmitUnion(InitElem, Ty);
295   }
296 
297   llvm::Constant *EmitVectorInitialization(InitListExpr *ILE) {
298     const llvm::VectorType *VType =
299         cast<llvm::VectorType>(ConvertType(ILE->getType()));
300     const llvm::Type *ElemTy = VType->getElementType();
301     std::vector<llvm::Constant*> Elts;
302     unsigned NumElements = VType->getNumElements();
303     unsigned NumInitElements = ILE->getNumInits();
304 
305     unsigned NumInitableElts = std::min(NumInitElements, NumElements);
306 
307     // Copy initializer elements.
308     unsigned i = 0;
309     for (; i < NumInitableElts; ++i) {
310       Expr *Init = ILE->getInit(i);
311       llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
312       if (!C)
313         return 0;
314       Elts.push_back(C);
315     }
316 
317     for (; i < NumElements; ++i)
318       Elts.push_back(llvm::Constant::getNullValue(ElemTy));
319 
320     return llvm::ConstantVector::get(VType, Elts);
321   }
322 
323   llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
324     return CGM.EmitNullConstant(E->getType());
325   }
326 
327   llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
328     if (ILE->getType()->isScalarType()) {
329       // We have a scalar in braces. Just use the first element.
330       if (ILE->getNumInits() > 0) {
331         Expr *Init = ILE->getInit(0);
332         return CGM.EmitConstantExpr(Init, Init->getType(), CGF);
333       }
334       return CGM.EmitNullConstant(ILE->getType());
335     }
336 
337     if (ILE->getType()->isArrayType())
338       return EmitArrayInitialization(ILE);
339 
340     if (ILE->getType()->isStructureType())
341       return EmitStructInitialization(ILE);
342 
343     if (ILE->getType()->isUnionType())
344       return EmitUnionInitialization(ILE);
345 
346     if (ILE->getType()->isVectorType())
347       return EmitVectorInitialization(ILE);
348 
349     assert(0 && "Unable to handle InitListExpr");
350     // Get rid of control reaches end of void function warning.
351     // Not reached.
352     return 0;
353   }
354 
355   llvm::Constant *VisitStringLiteral(StringLiteral *E) {
356     assert(!E->getType()->isPointerType() && "Strings are always arrays");
357 
358     // This must be a string initializing an array in a static initializer.
359     // Don't emit it as the address of the string, emit the string data itself
360     // as an inline array.
361     return llvm::ConstantArray::get(CGM.GetStringForStringLiteral(E), false);
362   }
363 
364   llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
365     // This must be an @encode initializing an array in a static initializer.
366     // Don't emit it as the address of the string, emit the string data itself
367     // as an inline array.
368     std::string Str;
369     CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
370     const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
371 
372     // Resize the string to the right size, adding zeros at the end, or
373     // truncating as needed.
374     Str.resize(CAT->getSize().getZExtValue(), '\0');
375     return llvm::ConstantArray::get(Str, false);
376   }
377 
378   llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
379     return Visit(E->getSubExpr());
380   }
381 
382   // Utility methods
383   const llvm::Type *ConvertType(QualType T) {
384     return CGM.getTypes().ConvertType(T);
385   }
386 
387 public:
388   llvm::Constant *EmitLValue(Expr *E) {
389     switch (E->getStmtClass()) {
390     default: break;
391     case Expr::CompoundLiteralExprClass: {
392       // Note that due to the nature of compound literals, this is guaranteed
393       // to be the only use of the variable, so we just generate it here.
394       CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
395       llvm::Constant* C = Visit(CLE->getInitializer());
396       // FIXME: "Leaked" on failure.
397       if (C)
398         C = new llvm::GlobalVariable(C->getType(),
399                                      E->getType().isConstQualified(),
400                                      llvm::GlobalValue::InternalLinkage,
401                                      C, ".compoundliteral", &CGM.getModule());
402       return C;
403     }
404     case Expr::DeclRefExprClass:
405     case Expr::QualifiedDeclRefExprClass: {
406       NamedDecl *Decl = cast<DeclRefExpr>(E)->getDecl();
407       if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
408         return CGM.GetAddrOfFunction(FD);
409       if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
410         // We can never refer to a variable with local storage.
411         if (!VD->hasLocalStorage()) {
412           if (VD->isFileVarDecl() || VD->hasExternalStorage())
413             return CGM.GetAddrOfGlobalVar(VD);
414           else if (VD->isBlockVarDecl()) {
415             assert(CGF && "Can't access static local vars without CGF");
416             return CGF->GetAddrOfStaticLocalVar(VD);
417           }
418         }
419       }
420       break;
421     }
422     case Expr::StringLiteralClass:
423       return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
424     case Expr::ObjCEncodeExprClass:
425       return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
426     case Expr::ObjCStringLiteralClass: {
427       ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
428       llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(SL);
429       return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
430     }
431     case Expr::PredefinedExprClass: {
432       // __func__/__FUNCTION__ -> "".  __PRETTY_FUNCTION__ -> "top level".
433       std::string Str;
434       if (cast<PredefinedExpr>(E)->getIdentType() ==
435           PredefinedExpr::PrettyFunction)
436         Str = "top level";
437 
438       return CGM.GetAddrOfConstantCString(Str, ".tmp");
439     }
440     case Expr::AddrLabelExprClass: {
441       assert(CGF && "Invalid address of label expression outside function.");
442       unsigned id = CGF->GetIDForAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
443       llvm::Constant *C = llvm::ConstantInt::get(llvm::Type::Int32Ty, id);
444       return llvm::ConstantExpr::getIntToPtr(C, ConvertType(E->getType()));
445     }
446     case Expr::CallExprClass: {
447       CallExpr* CE = cast<CallExpr>(E);
448       if (CE->isBuiltinCall(CGM.getContext()) !=
449             Builtin::BI__builtin___CFStringMakeConstantString)
450         break;
451       const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
452       const StringLiteral *Literal = cast<StringLiteral>(Arg);
453       // FIXME: need to deal with UCN conversion issues.
454       return CGM.GetAddrOfConstantCFString(Literal);
455     }
456     case Expr::BlockExprClass: {
457       std::string FunctionName;
458       if (CGF)
459         FunctionName = CGF->CurFn->getName();
460       else
461         FunctionName = "global";
462 
463       return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
464     }
465     }
466 
467     return 0;
468   }
469 };
470 
471 }  // end anonymous namespace.
472 
473 llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
474                                                 QualType DestType,
475                                                 CodeGenFunction *CGF) {
476   Expr::EvalResult Result;
477 
478   bool Success = false;
479 
480   if (DestType->isReferenceType()) {
481     // If the destination type is a reference type, we need to evaluate it
482     // as an lvalue.
483     if (E->EvaluateAsLValue(Result, Context)) {
484       if (const Expr *LVBase = Result.Val.getLValueBase()) {
485         if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(LVBase)) {
486           const ValueDecl *VD = cast<ValueDecl>(DRE->getDecl());
487 
488           // We can only initialize a reference with an lvalue if the lvalue
489           // is not a reference itself.
490           Success = !VD->getType()->isReferenceType();
491         }
492       }
493     }
494   } else
495     Success = E->Evaluate(Result, Context);
496 
497   if (Success) {
498     assert(!Result.HasSideEffects &&
499            "Constant expr should not have any side effects!");
500     switch (Result.Val.getKind()) {
501     case APValue::Uninitialized:
502       assert(0 && "Constant expressions should be initialized.");
503       return 0;
504     case APValue::LValue: {
505       const llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
506       llvm::Constant *Offset =
507         llvm::ConstantInt::get(llvm::Type::Int64Ty,
508                                Result.Val.getLValueOffset());
509 
510       llvm::Constant *C;
511       if (const Expr *LVBase = Result.Val.getLValueBase()) {
512         C = ConstExprEmitter(*this, CGF).EmitLValue(const_cast<Expr*>(LVBase));
513 
514         // Apply offset if necessary.
515         if (!Offset->isNullValue()) {
516           const llvm::Type *Type =
517             llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
518           llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Type);
519           Casted = llvm::ConstantExpr::getGetElementPtr(Casted, &Offset, 1);
520           C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
521         }
522 
523         // Convert to the appropriate type; this could be an lvalue for
524         // an integer.
525         if (isa<llvm::PointerType>(DestTy))
526           return llvm::ConstantExpr::getBitCast(C, DestTy);
527 
528         return llvm::ConstantExpr::getPtrToInt(C, DestTy);
529       } else {
530         C = Offset;
531 
532         // Convert to the appropriate type; this could be an lvalue for
533         // an integer.
534         if (isa<llvm::PointerType>(DestTy))
535           return llvm::ConstantExpr::getIntToPtr(C, DestTy);
536 
537         // If the types don't match this should only be a truncate.
538         if (C->getType() != DestTy)
539           return llvm::ConstantExpr::getTrunc(C, DestTy);
540 
541         return C;
542       }
543     }
544     case APValue::Int: {
545       llvm::Constant *C = llvm::ConstantInt::get(Result.Val.getInt());
546 
547       if (C->getType() == llvm::Type::Int1Ty) {
548         const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
549         C = llvm::ConstantExpr::getZExt(C, BoolTy);
550       }
551       return C;
552     }
553     case APValue::ComplexInt: {
554       llvm::Constant *Complex[2];
555 
556       Complex[0] = llvm::ConstantInt::get(Result.Val.getComplexIntReal());
557       Complex[1] = llvm::ConstantInt::get(Result.Val.getComplexIntImag());
558 
559       return llvm::ConstantStruct::get(Complex, 2);
560     }
561     case APValue::Float:
562       return llvm::ConstantFP::get(Result.Val.getFloat());
563     case APValue::ComplexFloat: {
564       llvm::Constant *Complex[2];
565 
566       Complex[0] = llvm::ConstantFP::get(Result.Val.getComplexFloatReal());
567       Complex[1] = llvm::ConstantFP::get(Result.Val.getComplexFloatImag());
568 
569       return llvm::ConstantStruct::get(Complex, 2);
570     }
571     case APValue::Vector: {
572       llvm::SmallVector<llvm::Constant *, 4> Inits;
573       unsigned NumElts = Result.Val.getVectorLength();
574 
575       for (unsigned i = 0; i != NumElts; ++i) {
576         APValue &Elt = Result.Val.getVectorElt(i);
577         if (Elt.isInt())
578           Inits.push_back(llvm::ConstantInt::get(Elt.getInt()));
579         else
580           Inits.push_back(llvm::ConstantFP::get(Elt.getFloat()));
581       }
582       return llvm::ConstantVector::get(&Inits[0], Inits.size());
583     }
584     }
585   }
586 
587   llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
588   if (C && C->getType() == llvm::Type::Int1Ty) {
589     const llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
590     C = llvm::ConstantExpr::getZExt(C, BoolTy);
591   }
592   return C;
593 }
594 
595 llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
596   // Always return an LLVM null constant for now; this will change when we
597   // get support for IRGen of member pointers.
598   return llvm::Constant::getNullValue(getTypes().ConvertType(T));
599 }
600