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