CGExprConstant.cpp revision 4d4e5c1ae83f4510caa486b3ad19de13048f9f04
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 "CGCXXABI.h"
17#include "CGObjCRuntime.h"
18#include "CGRecordLayout.h"
19#include "clang/AST/APValue.h"
20#include "clang/AST/ASTContext.h"
21#include "clang/AST/RecordLayout.h"
22#include "clang/AST/StmtVisitor.h"
23#include "clang/Basic/Builtins.h"
24#include "llvm/Constants.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/Target/TargetData.h"
28using namespace clang;
29using namespace CodeGen;
30
31//===----------------------------------------------------------------------===//
32//                            ConstStructBuilder
33//===----------------------------------------------------------------------===//
34
35namespace {
36class ConstStructBuilder {
37  CodeGenModule &CGM;
38  CodeGenFunction *CGF;
39
40  bool Packed;
41  CharUnits NextFieldOffsetInChars;
42  CharUnits LLVMStructAlignment;
43  SmallVector<llvm::Constant *, 32> Elements;
44public:
45  static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
46                                     InitListExpr *ILE);
47  static llvm::Constant *BuildStruct(CodeGenModule &CGM, CodeGenFunction *CGF,
48                                     const APValue &Value, QualType ValTy);
49
50private:
51  ConstStructBuilder(CodeGenModule &CGM, CodeGenFunction *CGF)
52    : CGM(CGM), CGF(CGF), Packed(false),
53    NextFieldOffsetInChars(CharUnits::Zero()),
54    LLVMStructAlignment(CharUnits::One()) { }
55
56  void AppendField(const FieldDecl *Field, uint64_t FieldOffset,
57                   llvm::Constant *InitExpr);
58
59  void AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
60                      llvm::ConstantInt *InitExpr);
61
62  void AppendPadding(CharUnits PadSize);
63
64  void AppendTailPadding(CharUnits RecordSize);
65
66  void ConvertStructToPacked();
67
68  bool Build(InitListExpr *ILE);
69  void Build(const APValue &Val, QualType ValTy);
70  llvm::Constant *Finalize(QualType Ty);
71
72  CharUnits getAlignment(const llvm::Constant *C) const {
73    if (Packed)  return CharUnits::One();
74    return CharUnits::fromQuantity(
75        CGM.getTargetData().getABITypeAlignment(C->getType()));
76  }
77
78  CharUnits getSizeInChars(const llvm::Constant *C) const {
79    return CharUnits::fromQuantity(
80        CGM.getTargetData().getTypeAllocSize(C->getType()));
81  }
82};
83
84void ConstStructBuilder::
85AppendField(const FieldDecl *Field, uint64_t FieldOffset,
86            llvm::Constant *InitCst) {
87
88  const ASTContext &Context = CGM.getContext();
89
90  CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
91
92  assert(NextFieldOffsetInChars <= FieldOffsetInChars
93         && "Field offset mismatch!");
94
95  CharUnits FieldAlignment = getAlignment(InitCst);
96
97  // Round up the field offset to the alignment of the field type.
98  CharUnits AlignedNextFieldOffsetInChars =
99    NextFieldOffsetInChars.RoundUpToAlignment(FieldAlignment);
100
101  if (AlignedNextFieldOffsetInChars > FieldOffsetInChars) {
102    assert(!Packed && "Alignment is wrong even with a packed struct!");
103
104    // Convert the struct to a packed struct.
105    ConvertStructToPacked();
106
107    AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
108  }
109
110  if (AlignedNextFieldOffsetInChars < FieldOffsetInChars) {
111    // We need to append padding.
112    AppendPadding(FieldOffsetInChars - NextFieldOffsetInChars);
113
114    assert(NextFieldOffsetInChars == FieldOffsetInChars &&
115           "Did not add enough padding!");
116
117    AlignedNextFieldOffsetInChars = NextFieldOffsetInChars;
118  }
119
120  // Add the field.
121  Elements.push_back(InitCst);
122  NextFieldOffsetInChars = AlignedNextFieldOffsetInChars +
123                           getSizeInChars(InitCst);
124
125  if (Packed)
126    assert(LLVMStructAlignment == CharUnits::One() &&
127           "Packed struct not byte-aligned!");
128  else
129    LLVMStructAlignment = std::max(LLVMStructAlignment, FieldAlignment);
130}
131
132void ConstStructBuilder::AppendBitField(const FieldDecl *Field,
133                                        uint64_t FieldOffset,
134                                        llvm::ConstantInt *CI) {
135  const ASTContext &Context = CGM.getContext();
136  const uint64_t CharWidth = Context.getCharWidth();
137  uint64_t NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
138  if (FieldOffset > NextFieldOffsetInBits) {
139    // We need to add padding.
140    CharUnits PadSize = Context.toCharUnitsFromBits(
141      llvm::RoundUpToAlignment(FieldOffset - NextFieldOffsetInBits,
142                               Context.getTargetInfo().getCharAlign()));
143
144    AppendPadding(PadSize);
145  }
146
147  uint64_t FieldSize = Field->getBitWidthValue(Context);
148
149  llvm::APInt FieldValue = CI->getValue();
150
151  // Promote the size of FieldValue if necessary
152  // FIXME: This should never occur, but currently it can because initializer
153  // constants are cast to bool, and because clang is not enforcing bitfield
154  // width limits.
155  if (FieldSize > FieldValue.getBitWidth())
156    FieldValue = FieldValue.zext(FieldSize);
157
158  // Truncate the size of FieldValue to the bit field size.
159  if (FieldSize < FieldValue.getBitWidth())
160    FieldValue = FieldValue.trunc(FieldSize);
161
162  NextFieldOffsetInBits = Context.toBits(NextFieldOffsetInChars);
163  if (FieldOffset < NextFieldOffsetInBits) {
164    // Either part of the field or the entire field can go into the previous
165    // byte.
166    assert(!Elements.empty() && "Elements can't be empty!");
167
168    unsigned BitsInPreviousByte = NextFieldOffsetInBits - FieldOffset;
169
170    bool FitsCompletelyInPreviousByte =
171      BitsInPreviousByte >= FieldValue.getBitWidth();
172
173    llvm::APInt Tmp = FieldValue;
174
175    if (!FitsCompletelyInPreviousByte) {
176      unsigned NewFieldWidth = FieldSize - BitsInPreviousByte;
177
178      if (CGM.getTargetData().isBigEndian()) {
179        Tmp = Tmp.lshr(NewFieldWidth);
180        Tmp = Tmp.trunc(BitsInPreviousByte);
181
182        // We want the remaining high bits.
183        FieldValue = FieldValue.trunc(NewFieldWidth);
184      } else {
185        Tmp = Tmp.trunc(BitsInPreviousByte);
186
187        // We want the remaining low bits.
188        FieldValue = FieldValue.lshr(BitsInPreviousByte);
189        FieldValue = FieldValue.trunc(NewFieldWidth);
190      }
191    }
192
193    Tmp = Tmp.zext(CharWidth);
194    if (CGM.getTargetData().isBigEndian()) {
195      if (FitsCompletelyInPreviousByte)
196        Tmp = Tmp.shl(BitsInPreviousByte - FieldValue.getBitWidth());
197    } else {
198      Tmp = Tmp.shl(CharWidth - BitsInPreviousByte);
199    }
200
201    // 'or' in the bits that go into the previous byte.
202    llvm::Value *LastElt = Elements.back();
203    if (llvm::ConstantInt *Val = dyn_cast<llvm::ConstantInt>(LastElt))
204      Tmp |= Val->getValue();
205    else {
206      assert(isa<llvm::UndefValue>(LastElt));
207      // If there is an undef field that we're adding to, it can either be a
208      // scalar undef (in which case, we just replace it with our field) or it
209      // is an array.  If it is an array, we have to pull one byte off the
210      // array so that the other undef bytes stay around.
211      if (!isa<llvm::IntegerType>(LastElt->getType())) {
212        // The undef padding will be a multibyte array, create a new smaller
213        // padding and then an hole for our i8 to get plopped into.
214        assert(isa<llvm::ArrayType>(LastElt->getType()) &&
215               "Expected array padding of undefs");
216        llvm::ArrayType *AT = cast<llvm::ArrayType>(LastElt->getType());
217        assert(AT->getElementType()->isIntegerTy(CharWidth) &&
218               AT->getNumElements() != 0 &&
219               "Expected non-empty array padding of undefs");
220
221        // Remove the padding array.
222        NextFieldOffsetInChars -= CharUnits::fromQuantity(AT->getNumElements());
223        Elements.pop_back();
224
225        // Add the padding back in two chunks.
226        AppendPadding(CharUnits::fromQuantity(AT->getNumElements()-1));
227        AppendPadding(CharUnits::One());
228        assert(isa<llvm::UndefValue>(Elements.back()) &&
229               Elements.back()->getType()->isIntegerTy(CharWidth) &&
230               "Padding addition didn't work right");
231      }
232    }
233
234    Elements.back() = llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp);
235
236    if (FitsCompletelyInPreviousByte)
237      return;
238  }
239
240  while (FieldValue.getBitWidth() > CharWidth) {
241    llvm::APInt Tmp;
242
243    if (CGM.getTargetData().isBigEndian()) {
244      // We want the high bits.
245      Tmp =
246        FieldValue.lshr(FieldValue.getBitWidth() - CharWidth).trunc(CharWidth);
247    } else {
248      // We want the low bits.
249      Tmp = FieldValue.trunc(CharWidth);
250
251      FieldValue = FieldValue.lshr(CharWidth);
252    }
253
254    Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(), Tmp));
255    ++NextFieldOffsetInChars;
256
257    FieldValue = FieldValue.trunc(FieldValue.getBitWidth() - CharWidth);
258  }
259
260  assert(FieldValue.getBitWidth() > 0 &&
261         "Should have at least one bit left!");
262  assert(FieldValue.getBitWidth() <= CharWidth &&
263         "Should not have more than a byte left!");
264
265  if (FieldValue.getBitWidth() < CharWidth) {
266    if (CGM.getTargetData().isBigEndian()) {
267      unsigned BitWidth = FieldValue.getBitWidth();
268
269      FieldValue = FieldValue.zext(CharWidth) << (CharWidth - BitWidth);
270    } else
271      FieldValue = FieldValue.zext(CharWidth);
272  }
273
274  // Append the last element.
275  Elements.push_back(llvm::ConstantInt::get(CGM.getLLVMContext(),
276                                            FieldValue));
277  ++NextFieldOffsetInChars;
278}
279
280void ConstStructBuilder::AppendPadding(CharUnits PadSize) {
281  if (PadSize.isZero())
282    return;
283
284  llvm::Type *Ty = CGM.Int8Ty;
285  if (PadSize > CharUnits::One())
286    Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
287
288  llvm::Constant *C = llvm::UndefValue::get(Ty);
289  Elements.push_back(C);
290  assert(getAlignment(C) == CharUnits::One() &&
291         "Padding must have 1 byte alignment!");
292
293  NextFieldOffsetInChars += getSizeInChars(C);
294}
295
296void ConstStructBuilder::AppendTailPadding(CharUnits RecordSize) {
297  assert(NextFieldOffsetInChars <= RecordSize &&
298         "Size mismatch!");
299
300  AppendPadding(RecordSize - NextFieldOffsetInChars);
301}
302
303void ConstStructBuilder::ConvertStructToPacked() {
304  SmallVector<llvm::Constant *, 16> PackedElements;
305  CharUnits ElementOffsetInChars = CharUnits::Zero();
306
307  for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
308    llvm::Constant *C = Elements[i];
309
310    CharUnits ElementAlign = CharUnits::fromQuantity(
311      CGM.getTargetData().getABITypeAlignment(C->getType()));
312    CharUnits AlignedElementOffsetInChars =
313      ElementOffsetInChars.RoundUpToAlignment(ElementAlign);
314
315    if (AlignedElementOffsetInChars > ElementOffsetInChars) {
316      // We need some padding.
317      CharUnits NumChars =
318        AlignedElementOffsetInChars - ElementOffsetInChars;
319
320      llvm::Type *Ty = CGM.Int8Ty;
321      if (NumChars > CharUnits::One())
322        Ty = llvm::ArrayType::get(Ty, NumChars.getQuantity());
323
324      llvm::Constant *Padding = llvm::UndefValue::get(Ty);
325      PackedElements.push_back(Padding);
326      ElementOffsetInChars += getSizeInChars(Padding);
327    }
328
329    PackedElements.push_back(C);
330    ElementOffsetInChars += getSizeInChars(C);
331  }
332
333  assert(ElementOffsetInChars == NextFieldOffsetInChars &&
334         "Packing the struct changed its size!");
335
336  Elements.swap(PackedElements);
337  LLVMStructAlignment = CharUnits::One();
338  Packed = true;
339}
340
341bool ConstStructBuilder::Build(InitListExpr *ILE) {
342  RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl();
343  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
344
345  unsigned FieldNo = 0;
346  unsigned ElementNo = 0;
347  const FieldDecl *LastFD = 0;
348  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
349
350  for (RecordDecl::field_iterator Field = RD->field_begin(),
351       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
352    if (IsMsStruct) {
353      // Zero-length bitfields following non-bitfield members are
354      // ignored:
355      if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) {
356        --FieldNo;
357        continue;
358      }
359      LastFD = (*Field);
360    }
361
362    // If this is a union, skip all the fields that aren't being initialized.
363    if (RD->isUnion() && ILE->getInitializedFieldInUnion() != *Field)
364      continue;
365
366    // Don't emit anonymous bitfields, they just affect layout.
367    if (Field->isUnnamedBitfield()) {
368      LastFD = (*Field);
369      continue;
370    }
371
372    // Get the initializer.  A struct can include fields without initializers,
373    // we just use explicit null values for them.
374    llvm::Constant *EltInit;
375    if (ElementNo < ILE->getNumInits())
376      EltInit = CGM.EmitConstantExpr(ILE->getInit(ElementNo++),
377                                     Field->getType(), CGF);
378    else
379      EltInit = CGM.EmitNullConstant(Field->getType());
380
381    if (!EltInit)
382      return false;
383
384    if (!Field->isBitField()) {
385      // Handle non-bitfield members.
386      AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
387    } else {
388      // Otherwise we have a bitfield.
389      AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
390                     cast<llvm::ConstantInt>(EltInit));
391    }
392  }
393
394  return true;
395}
396
397void ConstStructBuilder::Build(const APValue &Val, QualType ValTy) {
398  RecordDecl *RD = ValTy->getAs<RecordType>()->getDecl();
399  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
400
401  if (CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
402    unsigned BaseNo = 0;
403    for (CXXRecordDecl::base_class_iterator Base = CD->bases_begin(),
404         BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
405      // Build the base class subobject at the appropriately-offset location
406      // within this object.
407      const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
408      CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
409      NextFieldOffsetInChars -= BaseOffset;
410
411      Build(Val.getStructBase(BaseNo), Base->getType());
412
413      NextFieldOffsetInChars += BaseOffset;
414    }
415  }
416
417  unsigned FieldNo = 0;
418  const FieldDecl *LastFD = 0;
419  bool IsMsStruct = RD->hasAttr<MsStructAttr>();
420
421  for (RecordDecl::field_iterator Field = RD->field_begin(),
422       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
423    if (IsMsStruct) {
424      // Zero-length bitfields following non-bitfield members are
425      // ignored:
426      if (CGM.getContext().ZeroBitfieldFollowsNonBitfield((*Field), LastFD)) {
427        --FieldNo;
428        continue;
429      }
430      LastFD = (*Field);
431    }
432
433    // If this is a union, skip all the fields that aren't being initialized.
434    if (RD->isUnion() && Val.getUnionField() != *Field)
435      continue;
436
437    // Don't emit anonymous bitfields, they just affect layout.
438    if (Field->isUnnamedBitfield()) {
439      LastFD = (*Field);
440      continue;
441    }
442
443    // Emit the value of the initializer.
444    const APValue &FieldValue =
445      RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
446    llvm::Constant *EltInit =
447      CGM.EmitConstantValue(FieldValue, Field->getType(), CGF);
448    assert(EltInit && "EmitConstantValue can't fail");
449
450    if (!Field->isBitField()) {
451      // Handle non-bitfield members.
452      AppendField(*Field, Layout.getFieldOffset(FieldNo), EltInit);
453    } else {
454      // Otherwise we have a bitfield.
455      AppendBitField(*Field, Layout.getFieldOffset(FieldNo),
456                     cast<llvm::ConstantInt>(EltInit));
457    }
458  }
459}
460
461llvm::Constant *ConstStructBuilder::Finalize(QualType Ty) {
462  RecordDecl *RD = Ty->getAs<RecordType>()->getDecl();
463  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
464
465  CharUnits LayoutSizeInChars = Layout.getSize();
466
467  if (NextFieldOffsetInChars > LayoutSizeInChars) {
468    // If the struct is bigger than the size of the record type,
469    // we must have a flexible array member at the end.
470    assert(RD->hasFlexibleArrayMember() &&
471           "Must have flexible array member if struct is bigger than type!");
472
473    // No tail padding is necessary.
474  } else {
475    // Append tail padding if necessary.
476    AppendTailPadding(LayoutSizeInChars);
477
478    CharUnits LLVMSizeInChars =
479      NextFieldOffsetInChars.RoundUpToAlignment(LLVMStructAlignment);
480
481    // Check if we need to convert the struct to a packed struct.
482    if (NextFieldOffsetInChars <= LayoutSizeInChars &&
483        LLVMSizeInChars > LayoutSizeInChars) {
484      assert(!Packed && "Size mismatch!");
485
486      ConvertStructToPacked();
487      assert(NextFieldOffsetInChars <= LayoutSizeInChars &&
488             "Converting to packed did not help!");
489    }
490
491    assert(LayoutSizeInChars == NextFieldOffsetInChars &&
492           "Tail padding mismatch!");
493  }
494
495  // Pick the type to use.  If the type is layout identical to the ConvertType
496  // type then use it, otherwise use whatever the builder produced for us.
497  llvm::StructType *STy =
498      llvm::ConstantStruct::getTypeForElements(CGM.getLLVMContext(),
499                                               Elements, Packed);
500  llvm::Type *ValTy = CGM.getTypes().ConvertType(Ty);
501  if (llvm::StructType *ValSTy = dyn_cast<llvm::StructType>(ValTy)) {
502    if (ValSTy->isLayoutIdentical(STy))
503      STy = ValSTy;
504  }
505
506  llvm::Constant *Result = llvm::ConstantStruct::get(STy, Elements);
507
508  assert(NextFieldOffsetInChars.RoundUpToAlignment(getAlignment(Result)) ==
509         getSizeInChars(Result) && "Size mismatch!");
510
511  return Result;
512}
513
514llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
515                                                CodeGenFunction *CGF,
516                                                InitListExpr *ILE) {
517  ConstStructBuilder Builder(CGM, CGF);
518
519  if (!Builder.Build(ILE))
520    return 0;
521
522  return Builder.Finalize(ILE->getType());
523}
524
525llvm::Constant *ConstStructBuilder::BuildStruct(CodeGenModule &CGM,
526                                                CodeGenFunction *CGF,
527                                                const APValue &Val,
528                                                QualType ValTy) {
529  ConstStructBuilder Builder(CGM, CGF);
530  Builder.Build(Val, ValTy);
531  return Builder.Finalize(ValTy);
532}
533
534
535//===----------------------------------------------------------------------===//
536//                             ConstExprEmitter
537//===----------------------------------------------------------------------===//
538
539/// This class only needs to handle two cases:
540/// 1) Literals (this is used by APValue emission to emit literals).
541/// 2) Arrays, structs and unions (outside C++11 mode, we don't currently
542///    constant fold these types).
543class ConstExprEmitter :
544  public StmtVisitor<ConstExprEmitter, llvm::Constant*> {
545  CodeGenModule &CGM;
546  CodeGenFunction *CGF;
547  llvm::LLVMContext &VMContext;
548public:
549  ConstExprEmitter(CodeGenModule &cgm, CodeGenFunction *cgf)
550    : CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()) {
551  }
552
553  //===--------------------------------------------------------------------===//
554  //                            Visitor Methods
555  //===--------------------------------------------------------------------===//
556
557  llvm::Constant *VisitStmt(Stmt *S) {
558    return 0;
559  }
560
561  llvm::Constant *VisitParenExpr(ParenExpr *PE) {
562    return Visit(PE->getSubExpr());
563  }
564
565  llvm::Constant *
566  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE) {
567    return Visit(PE->getReplacement());
568  }
569
570  llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE) {
571    return Visit(GE->getResultExpr());
572  }
573
574  llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
575    return Visit(E->getInitializer());
576  }
577
578  llvm::Constant *VisitCastExpr(CastExpr* E) {
579    Expr *subExpr = E->getSubExpr();
580    llvm::Constant *C = CGM.EmitConstantExpr(subExpr, subExpr->getType(), CGF);
581    if (!C) return 0;
582
583    llvm::Type *destType = ConvertType(E->getType());
584
585    switch (E->getCastKind()) {
586    case CK_ToUnion: {
587      // GCC cast to union extension
588      assert(E->getType()->isUnionType() &&
589             "Destination type is not union type!");
590
591      // Build a struct with the union sub-element as the first member,
592      // and padded to the appropriate size
593      SmallVector<llvm::Constant*, 2> Elts;
594      SmallVector<llvm::Type*, 2> Types;
595      Elts.push_back(C);
596      Types.push_back(C->getType());
597      unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
598      unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(destType);
599
600      assert(CurSize <= TotalSize && "Union size mismatch!");
601      if (unsigned NumPadBytes = TotalSize - CurSize) {
602        llvm::Type *Ty = CGM.Int8Ty;
603        if (NumPadBytes > 1)
604          Ty = llvm::ArrayType::get(Ty, NumPadBytes);
605
606        Elts.push_back(llvm::UndefValue::get(Ty));
607        Types.push_back(Ty);
608      }
609
610      llvm::StructType* STy =
611        llvm::StructType::get(C->getType()->getContext(), Types, false);
612      return llvm::ConstantStruct::get(STy, Elts);
613    }
614
615    case CK_LValueToRValue:
616    case CK_AtomicToNonAtomic:
617    case CK_NonAtomicToAtomic:
618    case CK_NoOp:
619      return C;
620
621    case CK_Dependent: llvm_unreachable("saw dependent cast!");
622
623    case CK_ReinterpretMemberPointer:
624    case CK_DerivedToBaseMemberPointer:
625    case CK_BaseToDerivedMemberPointer:
626      return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
627
628    // These will never be supported.
629    case CK_ObjCObjectLValueCast:
630    case CK_ARCProduceObject:
631    case CK_ARCConsumeObject:
632    case CK_ARCReclaimReturnedObject:
633    case CK_ARCExtendBlockObject:
634      return 0;
635
636    // These don't need to be handled here because Evaluate knows how to
637    // evaluate them in the cases where they can be folded.
638    case CK_BitCast:
639    case CK_ToVoid:
640    case CK_Dynamic:
641    case CK_LValueBitCast:
642    case CK_NullToMemberPointer:
643    case CK_UserDefinedConversion:
644    case CK_ConstructorConversion:
645    case CK_CPointerToObjCPointerCast:
646    case CK_BlockPointerToObjCPointerCast:
647    case CK_AnyPointerToBlockPointerCast:
648    case CK_ArrayToPointerDecay:
649    case CK_FunctionToPointerDecay:
650    case CK_BaseToDerived:
651    case CK_DerivedToBase:
652    case CK_UncheckedDerivedToBase:
653    case CK_MemberPointerToBoolean:
654    case CK_VectorSplat:
655    case CK_FloatingRealToComplex:
656    case CK_FloatingComplexToReal:
657    case CK_FloatingComplexToBoolean:
658    case CK_FloatingComplexCast:
659    case CK_FloatingComplexToIntegralComplex:
660    case CK_IntegralRealToComplex:
661    case CK_IntegralComplexToReal:
662    case CK_IntegralComplexToBoolean:
663    case CK_IntegralComplexCast:
664    case CK_IntegralComplexToFloatingComplex:
665    case CK_PointerToIntegral:
666    case CK_PointerToBoolean:
667    case CK_NullToPointer:
668    case CK_IntegralCast:
669    case CK_IntegralToPointer:
670    case CK_IntegralToBoolean:
671    case CK_IntegralToFloating:
672    case CK_FloatingToIntegral:
673    case CK_FloatingToBoolean:
674    case CK_FloatingCast:
675      return 0;
676    }
677    llvm_unreachable("Invalid CastKind");
678  }
679
680  llvm::Constant *VisitCXXDefaultArgExpr(CXXDefaultArgExpr *DAE) {
681    return Visit(DAE->getExpr());
682  }
683
684  llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
685    return Visit(E->GetTemporaryExpr());
686  }
687
688  llvm::Constant *EmitArrayInitialization(InitListExpr *ILE) {
689    unsigned NumInitElements = ILE->getNumInits();
690    if (NumInitElements == 1 && ILE->getType() == ILE->getInit(0)->getType() &&
691        (isa<StringLiteral>(ILE->getInit(0)) ||
692         isa<ObjCEncodeExpr>(ILE->getInit(0))))
693      return Visit(ILE->getInit(0));
694
695    llvm::ArrayType *AType =
696        cast<llvm::ArrayType>(ConvertType(ILE->getType()));
697    llvm::Type *ElemTy = AType->getElementType();
698    unsigned NumElements = AType->getNumElements();
699
700    // Initialising an array requires us to automatically
701    // initialise any elements that have not been initialised explicitly
702    unsigned NumInitableElts = std::min(NumInitElements, NumElements);
703
704    // Copy initializer elements.
705    std::vector<llvm::Constant*> Elts;
706    Elts.reserve(NumInitableElts + NumElements);
707
708    bool RewriteType = false;
709    for (unsigned i = 0; i < NumInitableElts; ++i) {
710      Expr *Init = ILE->getInit(i);
711      llvm::Constant *C = CGM.EmitConstantExpr(Init, Init->getType(), CGF);
712      if (!C)
713        return 0;
714      RewriteType |= (C->getType() != ElemTy);
715      Elts.push_back(C);
716    }
717
718    // Initialize remaining array elements.
719    // FIXME: This doesn't handle member pointers correctly!
720    llvm::Constant *fillC;
721    if (Expr *filler = ILE->getArrayFiller())
722      fillC = CGM.EmitConstantExpr(filler, filler->getType(), CGF);
723    else
724      fillC = llvm::Constant::getNullValue(ElemTy);
725    if (!fillC)
726      return 0;
727    RewriteType |= (fillC->getType() != ElemTy);
728    Elts.resize(NumElements, fillC);
729
730    if (RewriteType) {
731      // FIXME: Try to avoid packing the array
732      std::vector<llvm::Type*> Types;
733      Types.reserve(NumInitableElts + NumElements);
734      for (unsigned i = 0, e = Elts.size(); i < e; ++i)
735        Types.push_back(Elts[i]->getType());
736      llvm::StructType *SType = llvm::StructType::get(AType->getContext(),
737                                                            Types, true);
738      return llvm::ConstantStruct::get(SType, Elts);
739    }
740
741    return llvm::ConstantArray::get(AType, Elts);
742  }
743
744  llvm::Constant *EmitStructInitialization(InitListExpr *ILE) {
745    return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
746  }
747
748  llvm::Constant *EmitUnionInitialization(InitListExpr *ILE) {
749    return ConstStructBuilder::BuildStruct(CGM, CGF, ILE);
750  }
751
752  llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E) {
753    return CGM.EmitNullConstant(E->getType());
754  }
755
756  llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
757    if (ILE->getType()->isArrayType())
758      return EmitArrayInitialization(ILE);
759
760    if (ILE->getType()->isRecordType())
761      return EmitStructInitialization(ILE);
762
763    if (ILE->getType()->isUnionType())
764      return EmitUnionInitialization(ILE);
765
766    return 0;
767  }
768
769  llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E) {
770    if (!E->getConstructor()->isTrivial())
771      return 0;
772
773    QualType Ty = E->getType();
774
775    // FIXME: We should not have to call getBaseElementType here.
776    const RecordType *RT =
777      CGM.getContext().getBaseElementType(Ty)->getAs<RecordType>();
778    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
779
780    // If the class doesn't have a trivial destructor, we can't emit it as a
781    // constant expr.
782    if (!RD->hasTrivialDestructor())
783      return 0;
784
785    // Only copy and default constructors can be trivial.
786
787
788    if (E->getNumArgs()) {
789      assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
790      assert(E->getConstructor()->isCopyOrMoveConstructor() &&
791             "trivial ctor has argument but isn't a copy/move ctor");
792
793      Expr *Arg = E->getArg(0);
794      assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
795             "argument to copy ctor is of wrong type");
796
797      return Visit(Arg);
798    }
799
800    return CGM.EmitNullConstant(Ty);
801  }
802
803  llvm::Constant *VisitStringLiteral(StringLiteral *E) {
804    return CGM.GetConstantArrayFromStringLiteral(E);
805  }
806
807  llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
808    // This must be an @encode initializing an array in a static initializer.
809    // Don't emit it as the address of the string, emit the string data itself
810    // as an inline array.
811    std::string Str;
812    CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
813    const ConstantArrayType *CAT = cast<ConstantArrayType>(E->getType());
814
815    // Resize the string to the right size, adding zeros at the end, or
816    // truncating as needed.
817    Str.resize(CAT->getSize().getZExtValue(), '\0');
818    return llvm::ConstantDataArray::getString(VMContext, Str, false);
819  }
820
821  llvm::Constant *VisitUnaryExtension(const UnaryOperator *E) {
822    return Visit(E->getSubExpr());
823  }
824
825  // Utility methods
826  llvm::Type *ConvertType(QualType T) {
827    return CGM.getTypes().ConvertType(T);
828  }
829
830public:
831  llvm::Constant *EmitLValue(APValue::LValueBase LVBase) {
832    if (const ValueDecl *Decl = LVBase.dyn_cast<const ValueDecl*>()) {
833      if (Decl->hasAttr<WeakRefAttr>())
834        return CGM.GetWeakRefReference(Decl);
835      if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Decl))
836        return CGM.GetAddrOfFunction(FD);
837      if (const VarDecl* VD = dyn_cast<VarDecl>(Decl)) {
838        // We can never refer to a variable with local storage.
839        if (!VD->hasLocalStorage()) {
840          if (VD->isFileVarDecl() || VD->hasExternalStorage())
841            return CGM.GetAddrOfGlobalVar(VD);
842          else if (VD->isLocalVarDecl()) {
843            assert(CGF && "Can't access static local vars without CGF");
844            return CGF->GetAddrOfStaticLocalVar(VD);
845          }
846        }
847      }
848      return 0;
849    }
850
851    Expr *E = const_cast<Expr*>(LVBase.get<const Expr*>());
852    switch (E->getStmtClass()) {
853    default: break;
854    case Expr::CompoundLiteralExprClass: {
855      // Note that due to the nature of compound literals, this is guaranteed
856      // to be the only use of the variable, so we just generate it here.
857      CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E);
858      llvm::Constant* C = CGM.EmitConstantExpr(CLE->getInitializer(),
859                                               CLE->getType(), CGF);
860      // FIXME: "Leaked" on failure.
861      if (C)
862        C = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
863                                     E->getType().isConstant(CGM.getContext()),
864                                     llvm::GlobalValue::InternalLinkage,
865                                     C, ".compoundliteral", 0, false,
866                          CGM.getContext().getTargetAddressSpace(E->getType()));
867      return C;
868    }
869    case Expr::StringLiteralClass:
870      return CGM.GetAddrOfConstantStringFromLiteral(cast<StringLiteral>(E));
871    case Expr::ObjCEncodeExprClass:
872      return CGM.GetAddrOfConstantStringFromObjCEncode(cast<ObjCEncodeExpr>(E));
873    case Expr::ObjCStringLiteralClass: {
874      ObjCStringLiteral* SL = cast<ObjCStringLiteral>(E);
875      llvm::Constant *C =
876          CGM.getObjCRuntime().GenerateConstantString(SL->getString());
877      return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
878    }
879    case Expr::PredefinedExprClass: {
880      unsigned Type = cast<PredefinedExpr>(E)->getIdentType();
881      if (CGF) {
882        LValue Res = CGF->EmitPredefinedLValue(cast<PredefinedExpr>(E));
883        return cast<llvm::Constant>(Res.getAddress());
884      } else if (Type == PredefinedExpr::PrettyFunction) {
885        return CGM.GetAddrOfConstantCString("top level", ".tmp");
886      }
887
888      return CGM.GetAddrOfConstantCString("", ".tmp");
889    }
890    case Expr::AddrLabelExprClass: {
891      assert(CGF && "Invalid address of label expression outside function.");
892      llvm::Constant *Ptr =
893        CGF->GetAddrOfLabel(cast<AddrLabelExpr>(E)->getLabel());
894      return llvm::ConstantExpr::getBitCast(Ptr, ConvertType(E->getType()));
895    }
896    case Expr::CallExprClass: {
897      CallExpr* CE = cast<CallExpr>(E);
898      unsigned builtin = CE->isBuiltinCall();
899      if (builtin !=
900            Builtin::BI__builtin___CFStringMakeConstantString &&
901          builtin !=
902            Builtin::BI__builtin___NSStringMakeConstantString)
903        break;
904      const Expr *Arg = CE->getArg(0)->IgnoreParenCasts();
905      const StringLiteral *Literal = cast<StringLiteral>(Arg);
906      if (builtin ==
907            Builtin::BI__builtin___NSStringMakeConstantString) {
908        return CGM.getObjCRuntime().GenerateConstantString(Literal);
909      }
910      // FIXME: need to deal with UCN conversion issues.
911      return CGM.GetAddrOfConstantCFString(Literal);
912    }
913    case Expr::BlockExprClass: {
914      std::string FunctionName;
915      if (CGF)
916        FunctionName = CGF->CurFn->getName();
917      else
918        FunctionName = "global";
919
920      return CGM.GetAddrOfGlobalBlock(cast<BlockExpr>(E), FunctionName.c_str());
921    }
922    case Expr::CXXTypeidExprClass: {
923      CXXTypeidExpr *Typeid = cast<CXXTypeidExpr>(E);
924      QualType T;
925      if (Typeid->isTypeOperand())
926        T = Typeid->getTypeOperand();
927      else
928        T = Typeid->getExprOperand()->getType();
929      return CGM.GetAddrOfRTTIDescriptor(T);
930    }
931    }
932
933    return 0;
934  }
935};
936
937}  // end anonymous namespace.
938
939llvm::Constant *CodeGenModule::EmitConstantInit(const VarDecl &D,
940                                                CodeGenFunction *CGF) {
941  if (const APValue *Value = D.evaluateValue())
942    return EmitConstantValue(*Value, D.getType(), CGF);
943
944  // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
945  // reference is a constant expression, and the reference binds to a temporary,
946  // then constant initialization is performed. ConstExprEmitter will
947  // incorrectly emit a prvalue constant in this case, and the calling code
948  // interprets that as the (pointer) value of the reference, rather than the
949  // desired value of the referee.
950  if (D.getType()->isReferenceType())
951    return 0;
952
953  const Expr *E = D.getInit();
954  assert(E && "No initializer to emit");
955
956  llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
957  if (C && C->getType()->isIntegerTy(1)) {
958    llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
959    C = llvm::ConstantExpr::getZExt(C, BoolTy);
960  }
961  return C;
962}
963
964llvm::Constant *CodeGenModule::EmitConstantExpr(const Expr *E,
965                                                QualType DestType,
966                                                CodeGenFunction *CGF) {
967  Expr::EvalResult Result;
968
969  bool Success = false;
970
971  if (DestType->isReferenceType())
972    Success = E->EvaluateAsLValue(Result, Context);
973  else
974    Success = E->EvaluateAsRValue(Result, Context);
975
976  if (Success && !Result.HasSideEffects)
977    return EmitConstantValue(Result.Val, DestType, CGF);
978
979  llvm::Constant* C = ConstExprEmitter(*this, CGF).Visit(const_cast<Expr*>(E));
980  if (C && C->getType()->isIntegerTy(1)) {
981    llvm::Type *BoolTy = getTypes().ConvertTypeForMem(E->getType());
982    C = llvm::ConstantExpr::getZExt(C, BoolTy);
983  }
984  return C;
985}
986
987llvm::Constant *CodeGenModule::EmitConstantValue(const APValue &Value,
988                                                 QualType DestType,
989                                                 CodeGenFunction *CGF) {
990  switch (Value.getKind()) {
991  case APValue::Uninitialized:
992    llvm_unreachable("Constant expressions should be initialized.");
993  case APValue::LValue: {
994    llvm::Type *DestTy = getTypes().ConvertTypeForMem(DestType);
995    llvm::Constant *Offset =
996      llvm::ConstantInt::get(Int64Ty, Value.getLValueOffset().getQuantity());
997
998    llvm::Constant *C;
999    if (APValue::LValueBase LVBase = Value.getLValueBase()) {
1000      // An array can be represented as an lvalue referring to the base.
1001      if (isa<llvm::ArrayType>(DestTy)) {
1002        assert(Offset->isNullValue() && "offset on array initializer");
1003        return ConstExprEmitter(*this, CGF).Visit(
1004          const_cast<Expr*>(LVBase.get<const Expr*>()));
1005      }
1006
1007      C = ConstExprEmitter(*this, CGF).EmitLValue(LVBase);
1008
1009      // Apply offset if necessary.
1010      if (!Offset->isNullValue()) {
1011        llvm::Constant *Casted = llvm::ConstantExpr::getBitCast(C, Int8PtrTy);
1012        Casted = llvm::ConstantExpr::getGetElementPtr(Casted, Offset);
1013        C = llvm::ConstantExpr::getBitCast(Casted, C->getType());
1014      }
1015
1016      // Convert to the appropriate type; this could be an lvalue for
1017      // an integer.
1018      if (isa<llvm::PointerType>(DestTy))
1019        return llvm::ConstantExpr::getBitCast(C, DestTy);
1020
1021      return llvm::ConstantExpr::getPtrToInt(C, DestTy);
1022    } else {
1023      C = Offset;
1024
1025      // Convert to the appropriate type; this could be an lvalue for
1026      // an integer.
1027      if (isa<llvm::PointerType>(DestTy))
1028        return llvm::ConstantExpr::getIntToPtr(C, DestTy);
1029
1030      // If the types don't match this should only be a truncate.
1031      if (C->getType() != DestTy)
1032        return llvm::ConstantExpr::getTrunc(C, DestTy);
1033
1034      return C;
1035    }
1036  }
1037  case APValue::Int: {
1038    llvm::Constant *C = llvm::ConstantInt::get(VMContext,
1039                                               Value.getInt());
1040
1041    if (C->getType()->isIntegerTy(1)) {
1042      llvm::Type *BoolTy = getTypes().ConvertTypeForMem(DestType);
1043      C = llvm::ConstantExpr::getZExt(C, BoolTy);
1044    }
1045    return C;
1046  }
1047  case APValue::ComplexInt: {
1048    llvm::Constant *Complex[2];
1049
1050    Complex[0] = llvm::ConstantInt::get(VMContext,
1051                                        Value.getComplexIntReal());
1052    Complex[1] = llvm::ConstantInt::get(VMContext,
1053                                        Value.getComplexIntImag());
1054
1055    // FIXME: the target may want to specify that this is packed.
1056    llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1057                                                  Complex[1]->getType(),
1058                                                  NULL);
1059    return llvm::ConstantStruct::get(STy, Complex);
1060  }
1061  case APValue::Float: {
1062    const llvm::APFloat &Init = Value.getFloat();
1063    if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf)
1064      return llvm::ConstantInt::get(VMContext, Init.bitcastToAPInt());
1065    else
1066      return llvm::ConstantFP::get(VMContext, Init);
1067  }
1068  case APValue::ComplexFloat: {
1069    llvm::Constant *Complex[2];
1070
1071    Complex[0] = llvm::ConstantFP::get(VMContext,
1072                                       Value.getComplexFloatReal());
1073    Complex[1] = llvm::ConstantFP::get(VMContext,
1074                                       Value.getComplexFloatImag());
1075
1076    // FIXME: the target may want to specify that this is packed.
1077    llvm::StructType *STy = llvm::StructType::get(Complex[0]->getType(),
1078                                                  Complex[1]->getType(),
1079                                                  NULL);
1080    return llvm::ConstantStruct::get(STy, Complex);
1081  }
1082  case APValue::Vector: {
1083    SmallVector<llvm::Constant *, 4> Inits;
1084    unsigned NumElts = Value.getVectorLength();
1085
1086    for (unsigned i = 0; i != NumElts; ++i) {
1087      const APValue &Elt = Value.getVectorElt(i);
1088      if (Elt.isInt())
1089        Inits.push_back(llvm::ConstantInt::get(VMContext, Elt.getInt()));
1090      else
1091        Inits.push_back(llvm::ConstantFP::get(VMContext, Elt.getFloat()));
1092    }
1093    return llvm::ConstantVector::get(Inits);
1094  }
1095  case APValue::AddrLabelDiff: {
1096    const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
1097    const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
1098    llvm::Constant *LHS = EmitConstantExpr(LHSExpr, LHSExpr->getType(), CGF);
1099    llvm::Constant *RHS = EmitConstantExpr(RHSExpr, RHSExpr->getType(), CGF);
1100
1101    // Compute difference
1102    llvm::Type *ResultType = getTypes().ConvertType(DestType);
1103    LHS = llvm::ConstantExpr::getPtrToInt(LHS, IntPtrTy);
1104    RHS = llvm::ConstantExpr::getPtrToInt(RHS, IntPtrTy);
1105    llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
1106
1107    // LLVM is a bit sensitive about the exact format of the
1108    // address-of-label difference; make sure to truncate after
1109    // the subtraction.
1110    return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
1111  }
1112  case APValue::Struct:
1113  case APValue::Union:
1114    return ConstStructBuilder::BuildStruct(*this, CGF, Value, DestType);
1115  case APValue::Array: {
1116    const ArrayType *CAT = Context.getAsArrayType(DestType);
1117    unsigned NumElements = Value.getArraySize();
1118    unsigned NumInitElts = Value.getArrayInitializedElts();
1119
1120    std::vector<llvm::Constant*> Elts;
1121    Elts.reserve(NumElements);
1122
1123    // Emit array filler, if there is one.
1124    llvm::Constant *Filler = 0;
1125    if (Value.hasArrayFiller())
1126      Filler = EmitConstantValue(Value.getArrayFiller(),
1127                                 CAT->getElementType(), CGF);
1128
1129    // Emit initializer elements.
1130    llvm::Type *CommonElementType = 0;
1131    for (unsigned I = 0; I < NumElements; ++I) {
1132      llvm::Constant *C = Filler;
1133      if (I < NumInitElts)
1134        C = EmitConstantValue(Value.getArrayInitializedElt(I),
1135                              CAT->getElementType(), CGF);
1136      if (I == 0)
1137        CommonElementType = C->getType();
1138      else if (C->getType() != CommonElementType)
1139        CommonElementType = 0;
1140      Elts.push_back(C);
1141    }
1142
1143    if (!CommonElementType) {
1144      // FIXME: Try to avoid packing the array
1145      std::vector<llvm::Type*> Types;
1146      Types.reserve(NumElements);
1147      for (unsigned i = 0, e = Elts.size(); i < e; ++i)
1148        Types.push_back(Elts[i]->getType());
1149      llvm::StructType *SType = llvm::StructType::get(VMContext, Types, true);
1150      return llvm::ConstantStruct::get(SType, Elts);
1151    }
1152
1153    llvm::ArrayType *AType =
1154      llvm::ArrayType::get(CommonElementType, NumElements);
1155    return llvm::ConstantArray::get(AType, Elts);
1156  }
1157  case APValue::MemberPointer:
1158    return getCXXABI().EmitMemberPointer(Value, DestType);
1159  }
1160  llvm_unreachable("Unknown APValue kind");
1161}
1162
1163llvm::Constant *
1164CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
1165  assert(E->isFileScope() && "not a file-scope compound literal expr");
1166  return ConstExprEmitter(*this, 0).EmitLValue(E);
1167}
1168
1169llvm::Constant *
1170CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
1171  // Member pointer constants always have a very particular form.
1172  const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
1173  const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
1174
1175  // A member function pointer.
1176  if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
1177    return getCXXABI().EmitMemberPointer(method);
1178
1179  // Otherwise, a member data pointer.
1180  uint64_t fieldOffset = getContext().getFieldOffset(decl);
1181  CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
1182  return getCXXABI().EmitMemberDataPointer(type, chars);
1183}
1184
1185static void
1186FillInNullDataMemberPointers(CodeGenModule &CGM, QualType T,
1187                             SmallVectorImpl<llvm::Constant *> &Elements,
1188                             uint64_t StartOffset) {
1189  assert(StartOffset % CGM.getContext().getCharWidth() == 0 &&
1190         "StartOffset not byte aligned!");
1191
1192  if (CGM.getTypes().isZeroInitializable(T))
1193    return;
1194
1195  if (const ConstantArrayType *CAT =
1196        CGM.getContext().getAsConstantArrayType(T)) {
1197    QualType ElementTy = CAT->getElementType();
1198    uint64_t ElementSize = CGM.getContext().getTypeSize(ElementTy);
1199
1200    for (uint64_t I = 0, E = CAT->getSize().getZExtValue(); I != E; ++I) {
1201      FillInNullDataMemberPointers(CGM, ElementTy, Elements,
1202                                   StartOffset + I * ElementSize);
1203    }
1204  } else if (const RecordType *RT = T->getAs<RecordType>()) {
1205    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1206    const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
1207
1208    // Go through all bases and fill in any null pointer to data members.
1209    for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
1210         E = RD->bases_end(); I != E; ++I) {
1211      if (I->isVirtual()) {
1212        // Ignore virtual bases.
1213        continue;
1214      }
1215
1216      const CXXRecordDecl *BaseDecl =
1217      cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
1218
1219      // Ignore empty bases.
1220      if (BaseDecl->isEmpty())
1221        continue;
1222
1223      // Ignore bases that don't have any pointer to data members.
1224      if (CGM.getTypes().isZeroInitializable(BaseDecl))
1225        continue;
1226
1227      uint64_t BaseOffset = Layout.getBaseClassOffsetInBits(BaseDecl);
1228      FillInNullDataMemberPointers(CGM, I->getType(),
1229                                   Elements, StartOffset + BaseOffset);
1230    }
1231
1232    // Visit all fields.
1233    unsigned FieldNo = 0;
1234    for (RecordDecl::field_iterator I = RD->field_begin(),
1235         E = RD->field_end(); I != E; ++I, ++FieldNo) {
1236      QualType FieldType = I->getType();
1237
1238      if (CGM.getTypes().isZeroInitializable(FieldType))
1239        continue;
1240
1241      uint64_t FieldOffset = StartOffset + Layout.getFieldOffset(FieldNo);
1242      FillInNullDataMemberPointers(CGM, FieldType, Elements, FieldOffset);
1243    }
1244  } else {
1245    assert(T->isMemberPointerType() && "Should only see member pointers here!");
1246    assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1247           "Should only see pointers to data members here!");
1248
1249    CharUnits StartIndex = CGM.getContext().toCharUnitsFromBits(StartOffset);
1250    CharUnits EndIndex = StartIndex + CGM.getContext().getTypeSizeInChars(T);
1251
1252    // FIXME: hardcodes Itanium member pointer representation!
1253    llvm::Constant *NegativeOne =
1254      llvm::ConstantInt::get(CGM.Int8Ty, -1ULL, /*isSigned*/true);
1255
1256    // Fill in the null data member pointer.
1257    for (CharUnits I = StartIndex; I != EndIndex; ++I)
1258      Elements[I.getQuantity()] = NegativeOne;
1259  }
1260}
1261
1262static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1263                                               llvm::Type *baseType,
1264                                               const CXXRecordDecl *base);
1265
1266static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
1267                                        const CXXRecordDecl *record,
1268                                        bool asCompleteObject) {
1269  const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
1270  llvm::StructType *structure =
1271    (asCompleteObject ? layout.getLLVMType()
1272                      : layout.getBaseSubobjectLLVMType());
1273
1274  unsigned numElements = structure->getNumElements();
1275  std::vector<llvm::Constant *> elements(numElements);
1276
1277  // Fill in all the bases.
1278  for (CXXRecordDecl::base_class_const_iterator
1279         I = record->bases_begin(), E = record->bases_end(); I != E; ++I) {
1280    if (I->isVirtual()) {
1281      // Ignore virtual bases; if we're laying out for a complete
1282      // object, we'll lay these out later.
1283      continue;
1284    }
1285
1286    const CXXRecordDecl *base =
1287      cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1288
1289    // Ignore empty bases.
1290    if (base->isEmpty())
1291      continue;
1292
1293    unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
1294    llvm::Type *baseType = structure->getElementType(fieldIndex);
1295    elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1296  }
1297
1298  // Fill in all the fields.
1299  for (RecordDecl::field_iterator I = record->field_begin(),
1300         E = record->field_end(); I != E; ++I) {
1301    const FieldDecl *field = *I;
1302
1303    // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
1304    // will fill in later.)
1305    if (!field->isBitField()) {
1306      unsigned fieldIndex = layout.getLLVMFieldNo(field);
1307      elements[fieldIndex] = CGM.EmitNullConstant(field->getType());
1308    }
1309
1310    // For unions, stop after the first named field.
1311    if (record->isUnion() && field->getDeclName())
1312      break;
1313  }
1314
1315  // Fill in the virtual bases, if we're working with the complete object.
1316  if (asCompleteObject) {
1317    for (CXXRecordDecl::base_class_const_iterator
1318           I = record->vbases_begin(), E = record->vbases_end(); I != E; ++I) {
1319      const CXXRecordDecl *base =
1320        cast<CXXRecordDecl>(I->getType()->castAs<RecordType>()->getDecl());
1321
1322      // Ignore empty bases.
1323      if (base->isEmpty())
1324        continue;
1325
1326      unsigned fieldIndex = layout.getVirtualBaseIndex(base);
1327
1328      // We might have already laid this field out.
1329      if (elements[fieldIndex]) continue;
1330
1331      llvm::Type *baseType = structure->getElementType(fieldIndex);
1332      elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
1333    }
1334  }
1335
1336  // Now go through all other fields and zero them out.
1337  for (unsigned i = 0; i != numElements; ++i) {
1338    if (!elements[i])
1339      elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
1340  }
1341
1342  return llvm::ConstantStruct::get(structure, elements);
1343}
1344
1345/// Emit the null constant for a base subobject.
1346static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
1347                                               llvm::Type *baseType,
1348                                               const CXXRecordDecl *base) {
1349  const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
1350
1351  // Just zero out bases that don't have any pointer to data members.
1352  if (baseLayout.isZeroInitializableAsBase())
1353    return llvm::Constant::getNullValue(baseType);
1354
1355  // If the base type is a struct, we can just use its null constant.
1356  if (isa<llvm::StructType>(baseType)) {
1357    return EmitNullConstant(CGM, base, /*complete*/ false);
1358  }
1359
1360  // Otherwise, some bases are represented as arrays of i8 if the size
1361  // of the base is smaller than its corresponding LLVM type.  Figure
1362  // out how many elements this base array has.
1363  llvm::ArrayType *baseArrayType = cast<llvm::ArrayType>(baseType);
1364  unsigned numBaseElements = baseArrayType->getNumElements();
1365
1366  // Fill in null data member pointers.
1367  SmallVector<llvm::Constant *, 16> baseElements(numBaseElements);
1368  FillInNullDataMemberPointers(CGM, CGM.getContext().getTypeDeclType(base),
1369                               baseElements, 0);
1370
1371  // Now go through all other elements and zero them out.
1372  if (numBaseElements) {
1373    llvm::Constant *i8_zero = llvm::Constant::getNullValue(CGM.Int8Ty);
1374    for (unsigned i = 0; i != numBaseElements; ++i) {
1375      if (!baseElements[i])
1376        baseElements[i] = i8_zero;
1377    }
1378  }
1379
1380  return llvm::ConstantArray::get(baseArrayType, baseElements);
1381}
1382
1383llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
1384  if (getTypes().isZeroInitializable(T))
1385    return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
1386
1387  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
1388    llvm::ArrayType *ATy =
1389      cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
1390
1391    QualType ElementTy = CAT->getElementType();
1392
1393    llvm::Constant *Element = EmitNullConstant(ElementTy);
1394    unsigned NumElements = CAT->getSize().getZExtValue();
1395
1396    if (Element->isNullValue())
1397      return llvm::ConstantAggregateZero::get(ATy);
1398
1399    SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
1400    return llvm::ConstantArray::get(ATy, Array);
1401  }
1402
1403  if (const RecordType *RT = T->getAs<RecordType>()) {
1404    const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1405    return ::EmitNullConstant(*this, RD, /*complete object*/ true);
1406  }
1407
1408  assert(T->isMemberPointerType() && "Should only see member pointers here!");
1409  assert(!T->getAs<MemberPointerType>()->getPointeeType()->isFunctionType() &&
1410         "Should only see pointers to data members here!");
1411
1412  // Itanium C++ ABI 2.3:
1413  //   A NULL pointer is represented as -1.
1414  return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
1415}
1416
1417llvm::Constant *
1418CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
1419  return ::EmitNullConstant(*this, Record, false);
1420}
1421