1//===--- CGExprConstant.cpp - Emit LLVM Code from Constant Expressions ----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This contains code to emit Constant Expr nodes as LLVM code.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CGCXXABI.h"
14#include "CGObjCRuntime.h"
15#include "CGRecordLayout.h"
16#include "CodeGenFunction.h"
17#include "CodeGenModule.h"
18#include "ConstantEmitter.h"
19#include "TargetInfo.h"
20#include "clang/AST/APValue.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/Attr.h"
23#include "clang/AST/RecordLayout.h"
24#include "clang/AST/StmtVisitor.h"
25#include "clang/Basic/Builtins.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/Sequence.h"
28#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalVariable.h"
32#include <optional>
33using namespace clang;
34using namespace CodeGen;
35
36//===----------------------------------------------------------------------===//
37//                            ConstantAggregateBuilder
38//===----------------------------------------------------------------------===//
39
40namespace {
41class ConstExprEmitter;
42
43struct ConstantAggregateBuilderUtils {
44  CodeGenModule &CGM;
45
46  ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {}
47
48  CharUnits getAlignment(const llvm::Constant *C) const {
49    return CharUnits::fromQuantity(
50        CGM.getDataLayout().getABITypeAlign(C->getType()));
51  }
52
53  CharUnits getSize(llvm::Type *Ty) const {
54    return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty));
55  }
56
57  CharUnits getSize(const llvm::Constant *C) const {
58    return getSize(C->getType());
59  }
60
61  llvm::Constant *getPadding(CharUnits PadSize) const {
62    llvm::Type *Ty = CGM.CharTy;
63    if (PadSize > CharUnits::One())
64      Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
65    return llvm::UndefValue::get(Ty);
66  }
67
68  llvm::Constant *getZeroes(CharUnits ZeroSize) const {
69    llvm::Type *Ty = llvm::ArrayType::get(CGM.CharTy, ZeroSize.getQuantity());
70    return llvm::ConstantAggregateZero::get(Ty);
71  }
72};
73
74/// Incremental builder for an llvm::Constant* holding a struct or array
75/// constant.
76class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils {
77  /// The elements of the constant. These two arrays must have the same size;
78  /// Offsets[i] describes the offset of Elems[i] within the constant. The
79  /// elements are kept in increasing offset order, and we ensure that there
80  /// is no overlap: Offsets[i+1] >= Offsets[i] + getSize(Elemes[i]).
81  ///
82  /// This may contain explicit padding elements (in order to create a
83  /// natural layout), but need not. Gaps between elements are implicitly
84  /// considered to be filled with undef.
85  llvm::SmallVector<llvm::Constant*, 32> Elems;
86  llvm::SmallVector<CharUnits, 32> Offsets;
87
88  /// The size of the constant (the maximum end offset of any added element).
89  /// May be larger than the end of Elems.back() if we split the last element
90  /// and removed some trailing undefs.
91  CharUnits Size = CharUnits::Zero();
92
93  /// This is true only if laying out Elems in order as the elements of a
94  /// non-packed LLVM struct will give the correct layout.
95  bool NaturalLayout = true;
96
97  bool split(size_t Index, CharUnits Hint);
98  std::optional<size_t> splitAt(CharUnits Pos);
99
100  static llvm::Constant *buildFrom(CodeGenModule &CGM,
101                                   ArrayRef<llvm::Constant *> Elems,
102                                   ArrayRef<CharUnits> Offsets,
103                                   CharUnits StartOffset, CharUnits Size,
104                                   bool NaturalLayout, llvm::Type *DesiredTy,
105                                   bool AllowOversized);
106
107public:
108  ConstantAggregateBuilder(CodeGenModule &CGM)
109      : ConstantAggregateBuilderUtils(CGM) {}
110
111  /// Update or overwrite the value starting at \p Offset with \c C.
112  ///
113  /// \param AllowOverwrite If \c true, this constant might overwrite (part of)
114  ///        a constant that has already been added. This flag is only used to
115  ///        detect bugs.
116  bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite);
117
118  /// Update or overwrite the bits starting at \p OffsetInBits with \p Bits.
119  bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite);
120
121  /// Attempt to condense the value starting at \p Offset to a constant of type
122  /// \p DesiredTy.
123  void condense(CharUnits Offset, llvm::Type *DesiredTy);
124
125  /// Produce a constant representing the entire accumulated value, ideally of
126  /// the specified type. If \p AllowOversized, the constant might be larger
127  /// than implied by \p DesiredTy (eg, if there is a flexible array member).
128  /// Otherwise, the constant will be of exactly the same size as \p DesiredTy
129  /// even if we can't represent it as that type.
130  llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const {
131    return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size,
132                     NaturalLayout, DesiredTy, AllowOversized);
133  }
134};
135
136template<typename Container, typename Range = std::initializer_list<
137                                 typename Container::value_type>>
138static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
139  assert(BeginOff <= EndOff && "invalid replacement range");
140  llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
141}
142
143bool ConstantAggregateBuilder::add(llvm::Constant *C, CharUnits Offset,
144                          bool AllowOverwrite) {
145  // Common case: appending to a layout.
146  if (Offset >= Size) {
147    CharUnits Align = getAlignment(C);
148    CharUnits AlignedSize = Size.alignTo(Align);
149    if (AlignedSize > Offset || Offset.alignTo(Align) != Offset)
150      NaturalLayout = false;
151    else if (AlignedSize < Offset) {
152      Elems.push_back(getPadding(Offset - Size));
153      Offsets.push_back(Size);
154    }
155    Elems.push_back(C);
156    Offsets.push_back(Offset);
157    Size = Offset + getSize(C);
158    return true;
159  }
160
161  // Uncommon case: constant overlaps what we've already created.
162  std::optional<size_t> FirstElemToReplace = splitAt(Offset);
163  if (!FirstElemToReplace)
164    return false;
165
166  CharUnits CSize = getSize(C);
167  std::optional<size_t> LastElemToReplace = splitAt(Offset + CSize);
168  if (!LastElemToReplace)
169    return false;
170
171  assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) &&
172         "unexpectedly overwriting field");
173
174  replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C});
175  replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset});
176  Size = std::max(Size, Offset + CSize);
177  NaturalLayout = false;
178  return true;
179}
180
181bool ConstantAggregateBuilder::addBits(llvm::APInt Bits, uint64_t OffsetInBits,
182                              bool AllowOverwrite) {
183  const ASTContext &Context = CGM.getContext();
184  const uint64_t CharWidth = CGM.getContext().getCharWidth();
185
186  // Offset of where we want the first bit to go within the bits of the
187  // current char.
188  unsigned OffsetWithinChar = OffsetInBits % CharWidth;
189
190  // We split bit-fields up into individual bytes. Walk over the bytes and
191  // update them.
192  for (CharUnits OffsetInChars =
193           Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar);
194       /**/; ++OffsetInChars) {
195    // Number of bits we want to fill in this char.
196    unsigned WantedBits =
197        std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar);
198
199    // Get a char containing the bits we want in the right places. The other
200    // bits have unspecified values.
201    llvm::APInt BitsThisChar = Bits;
202    if (BitsThisChar.getBitWidth() < CharWidth)
203      BitsThisChar = BitsThisChar.zext(CharWidth);
204    if (CGM.getDataLayout().isBigEndian()) {
205      // Figure out how much to shift by. We may need to left-shift if we have
206      // less than one byte of Bits left.
207      int Shift = Bits.getBitWidth() - CharWidth + OffsetWithinChar;
208      if (Shift > 0)
209        BitsThisChar.lshrInPlace(Shift);
210      else if (Shift < 0)
211        BitsThisChar = BitsThisChar.shl(-Shift);
212    } else {
213      BitsThisChar = BitsThisChar.shl(OffsetWithinChar);
214    }
215    if (BitsThisChar.getBitWidth() > CharWidth)
216      BitsThisChar = BitsThisChar.trunc(CharWidth);
217
218    if (WantedBits == CharWidth) {
219      // Got a full byte: just add it directly.
220      add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
221          OffsetInChars, AllowOverwrite);
222    } else {
223      // Partial byte: update the existing integer if there is one. If we
224      // can't split out a 1-CharUnit range to update, then we can't add
225      // these bits and fail the entire constant emission.
226      std::optional<size_t> FirstElemToUpdate = splitAt(OffsetInChars);
227      if (!FirstElemToUpdate)
228        return false;
229      std::optional<size_t> LastElemToUpdate =
230          splitAt(OffsetInChars + CharUnits::One());
231      if (!LastElemToUpdate)
232        return false;
233      assert(*LastElemToUpdate - *FirstElemToUpdate < 2 &&
234             "should have at most one element covering one byte");
235
236      // Figure out which bits we want and discard the rest.
237      llvm::APInt UpdateMask(CharWidth, 0);
238      if (CGM.getDataLayout().isBigEndian())
239        UpdateMask.setBits(CharWidth - OffsetWithinChar - WantedBits,
240                           CharWidth - OffsetWithinChar);
241      else
242        UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits);
243      BitsThisChar &= UpdateMask;
244
245      if (*FirstElemToUpdate == *LastElemToUpdate ||
246          Elems[*FirstElemToUpdate]->isNullValue() ||
247          isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])) {
248        // All existing bits are either zero or undef.
249        add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
250            OffsetInChars, /*AllowOverwrite*/ true);
251      } else {
252        llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate];
253        // In order to perform a partial update, we need the existing bitwise
254        // value, which we can only extract for a constant int.
255        auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate);
256        if (!CI)
257          return false;
258        // Because this is a 1-CharUnit range, the constant occupying it must
259        // be exactly one CharUnit wide.
260        assert(CI->getBitWidth() == CharWidth && "splitAt failed");
261        assert((!(CI->getValue() & UpdateMask) || AllowOverwrite) &&
262               "unexpectedly overwriting bitfield");
263        BitsThisChar |= (CI->getValue() & ~UpdateMask);
264        ToUpdate = llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar);
265      }
266    }
267
268    // Stop if we've added all the bits.
269    if (WantedBits == Bits.getBitWidth())
270      break;
271
272    // Remove the consumed bits from Bits.
273    if (!CGM.getDataLayout().isBigEndian())
274      Bits.lshrInPlace(WantedBits);
275    Bits = Bits.trunc(Bits.getBitWidth() - WantedBits);
276
277    // The remanining bits go at the start of the following bytes.
278    OffsetWithinChar = 0;
279  }
280
281  return true;
282}
283
284/// Returns a position within Elems and Offsets such that all elements
285/// before the returned index end before Pos and all elements at or after
286/// the returned index begin at or after Pos. Splits elements as necessary
287/// to ensure this. Returns std::nullopt if we find something we can't split.
288std::optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits Pos) {
289  if (Pos >= Size)
290    return Offsets.size();
291
292  while (true) {
293    auto FirstAfterPos = llvm::upper_bound(Offsets, Pos);
294    if (FirstAfterPos == Offsets.begin())
295      return 0;
296
297    // If we already have an element starting at Pos, we're done.
298    size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1;
299    if (Offsets[LastAtOrBeforePosIndex] == Pos)
300      return LastAtOrBeforePosIndex;
301
302    // We found an element starting before Pos. Check for overlap.
303    if (Offsets[LastAtOrBeforePosIndex] +
304        getSize(Elems[LastAtOrBeforePosIndex]) <= Pos)
305      return LastAtOrBeforePosIndex + 1;
306
307    // Try to decompose it into smaller constants.
308    if (!split(LastAtOrBeforePosIndex, Pos))
309      return std::nullopt;
310  }
311}
312
313/// Split the constant at index Index, if possible. Return true if we did.
314/// Hint indicates the location at which we'd like to split, but may be
315/// ignored.
316bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
317  NaturalLayout = false;
318  llvm::Constant *C = Elems[Index];
319  CharUnits Offset = Offsets[Index];
320
321  if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) {
322    // Expand the sequence into its contained elements.
323    // FIXME: This assumes vector elements are byte-sized.
324    replace(Elems, Index, Index + 1,
325            llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
326                            [&](unsigned Op) { return CA->getOperand(Op); }));
327    if (isa<llvm::ArrayType>(CA->getType()) ||
328        isa<llvm::VectorType>(CA->getType())) {
329      // Array or vector.
330      llvm::Type *ElemTy =
331          llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0);
332      CharUnits ElemSize = getSize(ElemTy);
333      replace(
334          Offsets, Index, Index + 1,
335          llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
336                          [&](unsigned Op) { return Offset + Op * ElemSize; }));
337    } else {
338      // Must be a struct.
339      auto *ST = cast<llvm::StructType>(CA->getType());
340      const llvm::StructLayout *Layout =
341          CGM.getDataLayout().getStructLayout(ST);
342      replace(Offsets, Index, Index + 1,
343              llvm::map_range(
344                  llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) {
345                    return Offset + CharUnits::fromQuantity(
346                                        Layout->getElementOffset(Op));
347                  }));
348    }
349    return true;
350  }
351
352  if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) {
353    // Expand the sequence into its contained elements.
354    // FIXME: This assumes vector elements are byte-sized.
355    // FIXME: If possible, split into two ConstantDataSequentials at Hint.
356    CharUnits ElemSize = getSize(CDS->getElementType());
357    replace(Elems, Index, Index + 1,
358            llvm::map_range(llvm::seq(0u, CDS->getNumElements()),
359                            [&](unsigned Elem) {
360                              return CDS->getElementAsConstant(Elem);
361                            }));
362    replace(Offsets, Index, Index + 1,
363            llvm::map_range(
364                llvm::seq(0u, CDS->getNumElements()),
365                [&](unsigned Elem) { return Offset + Elem * ElemSize; }));
366    return true;
367  }
368
369  if (isa<llvm::ConstantAggregateZero>(C)) {
370    // Split into two zeros at the hinted offset.
371    CharUnits ElemSize = getSize(C);
372    assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split");
373    replace(Elems, Index, Index + 1,
374            {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)});
375    replace(Offsets, Index, Index + 1, {Offset, Hint});
376    return true;
377  }
378
379  if (isa<llvm::UndefValue>(C)) {
380    // Drop undef; it doesn't contribute to the final layout.
381    replace(Elems, Index, Index + 1, {});
382    replace(Offsets, Index, Index + 1, {});
383    return true;
384  }
385
386  // FIXME: We could split a ConstantInt if the need ever arose.
387  // We don't need to do this to handle bit-fields because we always eagerly
388  // split them into 1-byte chunks.
389
390  return false;
391}
392
393static llvm::Constant *
394EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
395                  llvm::Type *CommonElementType, unsigned ArrayBound,
396                  SmallVectorImpl<llvm::Constant *> &Elements,
397                  llvm::Constant *Filler);
398
399llvm::Constant *ConstantAggregateBuilder::buildFrom(
400    CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems,
401    ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size,
402    bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) {
403  ConstantAggregateBuilderUtils Utils(CGM);
404
405  if (Elems.empty())
406    return llvm::UndefValue::get(DesiredTy);
407
408  auto Offset = [&](size_t I) { return Offsets[I] - StartOffset; };
409
410  // If we want an array type, see if all the elements are the same type and
411  // appropriately spaced.
412  if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) {
413    assert(!AllowOversized && "oversized array emission not supported");
414
415    bool CanEmitArray = true;
416    llvm::Type *CommonType = Elems[0]->getType();
417    llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType);
418    CharUnits ElemSize = Utils.getSize(ATy->getElementType());
419    SmallVector<llvm::Constant*, 32> ArrayElements;
420    for (size_t I = 0; I != Elems.size(); ++I) {
421      // Skip zeroes; we'll use a zero value as our array filler.
422      if (Elems[I]->isNullValue())
423        continue;
424
425      // All remaining elements must be the same type.
426      if (Elems[I]->getType() != CommonType ||
427          Offset(I) % ElemSize != 0) {
428        CanEmitArray = false;
429        break;
430      }
431      ArrayElements.resize(Offset(I) / ElemSize + 1, Filler);
432      ArrayElements.back() = Elems[I];
433    }
434
435    if (CanEmitArray) {
436      return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(),
437                               ArrayElements, Filler);
438    }
439
440    // Can't emit as an array, carry on to emit as a struct.
441  }
442
443  // The size of the constant we plan to generate.  This is usually just
444  // the size of the initialized type, but in AllowOversized mode (i.e.
445  // flexible array init), it can be larger.
446  CharUnits DesiredSize = Utils.getSize(DesiredTy);
447  if (Size > DesiredSize) {
448    assert(AllowOversized && "Elems are oversized");
449    DesiredSize = Size;
450  }
451
452  // The natural alignment of an unpacked LLVM struct with the given elements.
453  CharUnits Align = CharUnits::One();
454  for (llvm::Constant *C : Elems)
455    Align = std::max(Align, Utils.getAlignment(C));
456
457  // The natural size of an unpacked LLVM struct with the given elements.
458  CharUnits AlignedSize = Size.alignTo(Align);
459
460  bool Packed = false;
461  ArrayRef<llvm::Constant*> UnpackedElems = Elems;
462  llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage;
463  if (DesiredSize < AlignedSize || DesiredSize.alignTo(Align) != DesiredSize) {
464    // The natural layout would be too big; force use of a packed layout.
465    NaturalLayout = false;
466    Packed = true;
467  } else if (DesiredSize > AlignedSize) {
468    // The natural layout would be too small. Add padding to fix it. (This
469    // is ignored if we choose a packed layout.)
470    UnpackedElemStorage.assign(Elems.begin(), Elems.end());
471    UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size));
472    UnpackedElems = UnpackedElemStorage;
473  }
474
475  // If we don't have a natural layout, insert padding as necessary.
476  // As we go, double-check to see if we can actually just emit Elems
477  // as a non-packed struct and do so opportunistically if possible.
478  llvm::SmallVector<llvm::Constant*, 32> PackedElems;
479  if (!NaturalLayout) {
480    CharUnits SizeSoFar = CharUnits::Zero();
481    for (size_t I = 0; I != Elems.size(); ++I) {
482      CharUnits Align = Utils.getAlignment(Elems[I]);
483      CharUnits NaturalOffset = SizeSoFar.alignTo(Align);
484      CharUnits DesiredOffset = Offset(I);
485      assert(DesiredOffset >= SizeSoFar && "elements out of order");
486
487      if (DesiredOffset != NaturalOffset)
488        Packed = true;
489      if (DesiredOffset != SizeSoFar)
490        PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar));
491      PackedElems.push_back(Elems[I]);
492      SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]);
493    }
494    // If we're using the packed layout, pad it out to the desired size if
495    // necessary.
496    if (Packed) {
497      assert(SizeSoFar <= DesiredSize &&
498             "requested size is too small for contents");
499      if (SizeSoFar < DesiredSize)
500        PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar));
501    }
502  }
503
504  llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements(
505      CGM.getLLVMContext(), Packed ? PackedElems : UnpackedElems, Packed);
506
507  // Pick the type to use.  If the type is layout identical to the desired
508  // type then use it, otherwise use whatever the builder produced for us.
509  if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) {
510    if (DesiredSTy->isLayoutIdentical(STy))
511      STy = DesiredSTy;
512  }
513
514  return llvm::ConstantStruct::get(STy, Packed ? PackedElems : UnpackedElems);
515}
516
517void ConstantAggregateBuilder::condense(CharUnits Offset,
518                                        llvm::Type *DesiredTy) {
519  CharUnits Size = getSize(DesiredTy);
520
521  std::optional<size_t> FirstElemToReplace = splitAt(Offset);
522  if (!FirstElemToReplace)
523    return;
524  size_t First = *FirstElemToReplace;
525
526  std::optional<size_t> LastElemToReplace = splitAt(Offset + Size);
527  if (!LastElemToReplace)
528    return;
529  size_t Last = *LastElemToReplace;
530
531  size_t Length = Last - First;
532  if (Length == 0)
533    return;
534
535  if (Length == 1 && Offsets[First] == Offset &&
536      getSize(Elems[First]) == Size) {
537    // Re-wrap single element structs if necessary. Otherwise, leave any single
538    // element constant of the right size alone even if it has the wrong type.
539    auto *STy = dyn_cast<llvm::StructType>(DesiredTy);
540    if (STy && STy->getNumElements() == 1 &&
541        STy->getElementType(0) == Elems[First]->getType())
542      Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]);
543    return;
544  }
545
546  llvm::Constant *Replacement = buildFrom(
547      CGM, ArrayRef(Elems).slice(First, Length),
548      ArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy),
549      /*known to have natural layout=*/false, DesiredTy, false);
550  replace(Elems, First, Last, {Replacement});
551  replace(Offsets, First, Last, {Offset});
552}
553
554//===----------------------------------------------------------------------===//
555//                            ConstStructBuilder
556//===----------------------------------------------------------------------===//
557
558class ConstStructBuilder {
559  CodeGenModule &CGM;
560  ConstantEmitter &Emitter;
561  ConstantAggregateBuilder &Builder;
562  CharUnits StartOffset;
563
564public:
565  static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
566                                     InitListExpr *ILE, QualType StructTy);
567  static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
568                                     const APValue &Value, QualType ValTy);
569  static bool UpdateStruct(ConstantEmitter &Emitter,
570                           ConstantAggregateBuilder &Const, CharUnits Offset,
571                           InitListExpr *Updater);
572
573private:
574  ConstStructBuilder(ConstantEmitter &Emitter,
575                     ConstantAggregateBuilder &Builder, CharUnits StartOffset)
576      : CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder),
577        StartOffset(StartOffset) {}
578
579  bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
580                   llvm::Constant *InitExpr, bool AllowOverwrite = false);
581
582  bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst,
583                   bool AllowOverwrite = false);
584
585  bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
586                      llvm::ConstantInt *InitExpr, bool AllowOverwrite = false);
587
588  bool Build(InitListExpr *ILE, bool AllowOverwrite);
589  bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
590             const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
591  llvm::Constant *Finalize(QualType Ty);
592};
593
594bool ConstStructBuilder::AppendField(
595    const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst,
596    bool AllowOverwrite) {
597  const ASTContext &Context = CGM.getContext();
598
599  CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
600
601  return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite);
602}
603
604bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars,
605                                     llvm::Constant *InitCst,
606                                     bool AllowOverwrite) {
607  return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite);
608}
609
610bool ConstStructBuilder::AppendBitField(
611    const FieldDecl *Field, uint64_t FieldOffset, llvm::ConstantInt *CI,
612    bool AllowOverwrite) {
613  const CGRecordLayout &RL =
614      CGM.getTypes().getCGRecordLayout(Field->getParent());
615  const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
616  llvm::APInt FieldValue = CI->getValue();
617
618  // Promote the size of FieldValue if necessary
619  // FIXME: This should never occur, but currently it can because initializer
620  // constants are cast to bool, and because clang is not enforcing bitfield
621  // width limits.
622  if (Info.Size > FieldValue.getBitWidth())
623    FieldValue = FieldValue.zext(Info.Size);
624
625  // Truncate the size of FieldValue to the bit field size.
626  if (Info.Size < FieldValue.getBitWidth())
627    FieldValue = FieldValue.trunc(Info.Size);
628
629  return Builder.addBits(FieldValue,
630                         CGM.getContext().toBits(StartOffset) + FieldOffset,
631                         AllowOverwrite);
632}
633
634static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
635                                      ConstantAggregateBuilder &Const,
636                                      CharUnits Offset, QualType Type,
637                                      InitListExpr *Updater) {
638  if (Type->isRecordType())
639    return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater);
640
641  auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type);
642  if (!CAT)
643    return false;
644  QualType ElemType = CAT->getElementType();
645  CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType);
646  llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType);
647
648  llvm::Constant *FillC = nullptr;
649  if (Expr *Filler = Updater->getArrayFiller()) {
650    if (!isa<NoInitExpr>(Filler)) {
651      FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType);
652      if (!FillC)
653        return false;
654    }
655  }
656
657  unsigned NumElementsToUpdate =
658      FillC ? CAT->getSize().getZExtValue() : Updater->getNumInits();
659  for (unsigned I = 0; I != NumElementsToUpdate; ++I, Offset += ElemSize) {
660    Expr *Init = nullptr;
661    if (I < Updater->getNumInits())
662      Init = Updater->getInit(I);
663
664    if (!Init && FillC) {
665      if (!Const.add(FillC, Offset, true))
666        return false;
667    } else if (!Init || isa<NoInitExpr>(Init)) {
668      continue;
669    } else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init)) {
670      if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType,
671                                     ChildILE))
672        return false;
673      // Attempt to reduce the array element to a single constant if necessary.
674      Const.condense(Offset, ElemTy);
675    } else {
676      llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType);
677      if (!Const.add(Val, Offset, true))
678        return false;
679    }
680  }
681
682  return true;
683}
684
685bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) {
686  RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl();
687  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
688
689  unsigned FieldNo = -1;
690  unsigned ElementNo = 0;
691
692  // Bail out if we have base classes. We could support these, but they only
693  // arise in C++1z where we will have already constant folded most interesting
694  // cases. FIXME: There are still a few more cases we can handle this way.
695  if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
696    if (CXXRD->getNumBases())
697      return false;
698
699  for (FieldDecl *Field : RD->fields()) {
700    ++FieldNo;
701
702    // If this is a union, skip all the fields that aren't being initialized.
703    if (RD->isUnion() &&
704        !declaresSameEntity(ILE->getInitializedFieldInUnion(), Field))
705      continue;
706
707    // Don't emit anonymous bitfields.
708    if (Field->isUnnamedBitfield())
709      continue;
710
711    // Get the initializer.  A struct can include fields without initializers,
712    // we just use explicit null values for them.
713    Expr *Init = nullptr;
714    if (ElementNo < ILE->getNumInits())
715      Init = ILE->getInit(ElementNo++);
716    if (Init && isa<NoInitExpr>(Init))
717      continue;
718
719    // Zero-sized fields are not emitted, but their initializers may still
720    // prevent emission of this struct as a constant.
721    if (Field->isZeroSize(CGM.getContext())) {
722      if (Init->HasSideEffects(CGM.getContext()))
723        return false;
724      continue;
725    }
726
727    // When emitting a DesignatedInitUpdateExpr, a nested InitListExpr
728    // represents additional overwriting of our current constant value, and not
729    // a new constant to emit independently.
730    if (AllowOverwrite &&
731        (Field->getType()->isArrayType() || Field->getType()->isRecordType())) {
732      if (auto *SubILE = dyn_cast<InitListExpr>(Init)) {
733        CharUnits Offset = CGM.getContext().toCharUnitsFromBits(
734            Layout.getFieldOffset(FieldNo));
735        if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset,
736                                       Field->getType(), SubILE))
737          return false;
738        // If we split apart the field's value, try to collapse it down to a
739        // single value now.
740        Builder.condense(StartOffset + Offset,
741                         CGM.getTypes().ConvertTypeForMem(Field->getType()));
742        continue;
743      }
744    }
745
746    llvm::Constant *EltInit =
747        Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType())
748             : Emitter.emitNullForMemory(Field->getType());
749    if (!EltInit)
750      return false;
751
752    if (!Field->isBitField()) {
753      // Handle non-bitfield members.
754      if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit,
755                       AllowOverwrite))
756        return false;
757      // After emitting a non-empty field with [[no_unique_address]], we may
758      // need to overwrite its tail padding.
759      if (Field->hasAttr<NoUniqueAddressAttr>())
760        AllowOverwrite = true;
761    } else {
762      // Otherwise we have a bitfield.
763      if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) {
764        if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI,
765                            AllowOverwrite))
766          return false;
767      } else {
768        // We are trying to initialize a bitfield with a non-trivial constant,
769        // this must require run-time code.
770        return false;
771      }
772    }
773  }
774
775  return true;
776}
777
778namespace {
779struct BaseInfo {
780  BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
781    : Decl(Decl), Offset(Offset), Index(Index) {
782  }
783
784  const CXXRecordDecl *Decl;
785  CharUnits Offset;
786  unsigned Index;
787
788  bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
789};
790}
791
792bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
793                               bool IsPrimaryBase,
794                               const CXXRecordDecl *VTableClass,
795                               CharUnits Offset) {
796  const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
797
798  if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
799    // Add a vtable pointer, if we need one and it hasn't already been added.
800    if (Layout.hasOwnVFPtr()) {
801      llvm::Constant *VTableAddressPoint =
802          CGM.getCXXABI().getVTableAddressPointForConstExpr(
803              BaseSubobject(CD, Offset), VTableClass);
804      if (!AppendBytes(Offset, VTableAddressPoint))
805        return false;
806    }
807
808    // Accumulate and sort bases, in order to visit them in address order, which
809    // may not be the same as declaration order.
810    SmallVector<BaseInfo, 8> Bases;
811    Bases.reserve(CD->getNumBases());
812    unsigned BaseNo = 0;
813    for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
814         BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
815      assert(!Base->isVirtual() && "should not have virtual bases here");
816      const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
817      CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
818      Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
819    }
820    llvm::stable_sort(Bases);
821
822    for (unsigned I = 0, N = Bases.size(); I != N; ++I) {
823      BaseInfo &Base = Bases[I];
824
825      bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
826      Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
827            VTableClass, Offset + Base.Offset);
828    }
829  }
830
831  unsigned FieldNo = 0;
832  uint64_t OffsetBits = CGM.getContext().toBits(Offset);
833
834  bool AllowOverwrite = false;
835  for (RecordDecl::field_iterator Field = RD->field_begin(),
836       FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
837    // If this is a union, skip all the fields that aren't being initialized.
838    if (RD->isUnion() && !declaresSameEntity(Val.getUnionField(), *Field))
839      continue;
840
841    // Don't emit anonymous bitfields or zero-sized fields.
842    if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext()))
843      continue;
844
845    // Emit the value of the initializer.
846    const APValue &FieldValue =
847      RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
848    llvm::Constant *EltInit =
849      Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
850    if (!EltInit)
851      return false;
852
853    if (!Field->isBitField()) {
854      // Handle non-bitfield members.
855      if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
856                       EltInit, AllowOverwrite))
857        return false;
858      // After emitting a non-empty field with [[no_unique_address]], we may
859      // need to overwrite its tail padding.
860      if (Field->hasAttr<NoUniqueAddressAttr>())
861        AllowOverwrite = true;
862    } else {
863      // Otherwise we have a bitfield.
864      if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
865                          cast<llvm::ConstantInt>(EltInit), AllowOverwrite))
866        return false;
867    }
868  }
869
870  return true;
871}
872
873llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
874  Type = Type.getNonReferenceType();
875  RecordDecl *RD = Type->castAs<RecordType>()->getDecl();
876  llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
877  return Builder.build(ValTy, RD->hasFlexibleArrayMember());
878}
879
880llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
881                                                InitListExpr *ILE,
882                                                QualType ValTy) {
883  ConstantAggregateBuilder Const(Emitter.CGM);
884  ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
885
886  if (!Builder.Build(ILE, /*AllowOverwrite*/false))
887    return nullptr;
888
889  return Builder.Finalize(ValTy);
890}
891
892llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
893                                                const APValue &Val,
894                                                QualType ValTy) {
895  ConstantAggregateBuilder Const(Emitter.CGM);
896  ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
897
898  const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl();
899  const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
900  if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
901    return nullptr;
902
903  return Builder.Finalize(ValTy);
904}
905
906bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter,
907                                      ConstantAggregateBuilder &Const,
908                                      CharUnits Offset, InitListExpr *Updater) {
909  return ConstStructBuilder(Emitter, Const, Offset)
910      .Build(Updater, /*AllowOverwrite*/ true);
911}
912
913//===----------------------------------------------------------------------===//
914//                             ConstExprEmitter
915//===----------------------------------------------------------------------===//
916
917static ConstantAddress
918tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
919                             const CompoundLiteralExpr *E) {
920  CodeGenModule &CGM = emitter.CGM;
921  CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
922  if (llvm::GlobalVariable *Addr =
923          CGM.getAddrOfConstantCompoundLiteralIfEmitted(E))
924    return ConstantAddress(Addr, Addr->getValueType(), Align);
925
926  LangAS addressSpace = E->getType().getAddressSpace();
927  llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
928                                                    addressSpace, E->getType());
929  if (!C) {
930    assert(!E->isFileScope() &&
931           "file-scope compound literal did not have constant initializer!");
932    return ConstantAddress::invalid();
933  }
934
935  auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(),
936                                     CGM.isTypeConstant(E->getType(), true),
937                                     llvm::GlobalValue::InternalLinkage,
938                                     C, ".compoundliteral", nullptr,
939                                     llvm::GlobalVariable::NotThreadLocal,
940                    CGM.getContext().getTargetAddressSpace(addressSpace));
941  emitter.finalize(GV);
942  GV->setAlignment(Align.getAsAlign());
943  CGM.setAddrOfConstantCompoundLiteral(E, GV);
944  return ConstantAddress(GV, GV->getValueType(), Align);
945}
946
947static llvm::Constant *
948EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
949                  llvm::Type *CommonElementType, unsigned ArrayBound,
950                  SmallVectorImpl<llvm::Constant *> &Elements,
951                  llvm::Constant *Filler) {
952  // Figure out how long the initial prefix of non-zero elements is.
953  unsigned NonzeroLength = ArrayBound;
954  if (Elements.size() < NonzeroLength && Filler->isNullValue())
955    NonzeroLength = Elements.size();
956  if (NonzeroLength == Elements.size()) {
957    while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue())
958      --NonzeroLength;
959  }
960
961  if (NonzeroLength == 0)
962    return llvm::ConstantAggregateZero::get(DesiredType);
963
964  // Add a zeroinitializer array filler if we have lots of trailing zeroes.
965  unsigned TrailingZeroes = ArrayBound - NonzeroLength;
966  if (TrailingZeroes >= 8) {
967    assert(Elements.size() >= NonzeroLength &&
968           "missing initializer for non-zero element");
969
970    // If all the elements had the same type up to the trailing zeroes, emit a
971    // struct of two arrays (the nonzero data and the zeroinitializer).
972    if (CommonElementType && NonzeroLength >= 8) {
973      llvm::Constant *Initial = llvm::ConstantArray::get(
974          llvm::ArrayType::get(CommonElementType, NonzeroLength),
975          ArrayRef(Elements).take_front(NonzeroLength));
976      Elements.resize(2);
977      Elements[0] = Initial;
978    } else {
979      Elements.resize(NonzeroLength + 1);
980    }
981
982    auto *FillerType =
983        CommonElementType ? CommonElementType : DesiredType->getElementType();
984    FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
985    Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
986    CommonElementType = nullptr;
987  } else if (Elements.size() != ArrayBound) {
988    // Otherwise pad to the right size with the filler if necessary.
989    Elements.resize(ArrayBound, Filler);
990    if (Filler->getType() != CommonElementType)
991      CommonElementType = nullptr;
992  }
993
994  // If all elements have the same type, just emit an array constant.
995  if (CommonElementType)
996    return llvm::ConstantArray::get(
997        llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
998
999  // We have mixed types. Use a packed struct.
1000  llvm::SmallVector<llvm::Type *, 16> Types;
1001  Types.reserve(Elements.size());
1002  for (llvm::Constant *Elt : Elements)
1003    Types.push_back(Elt->getType());
1004  llvm::StructType *SType =
1005      llvm::StructType::get(CGM.getLLVMContext(), Types, true);
1006  return llvm::ConstantStruct::get(SType, Elements);
1007}
1008
1009// This class only needs to handle arrays, structs and unions. Outside C++11
1010// mode, we don't currently constant fold those types.  All other types are
1011// handled by constant folding.
1012//
1013// Constant folding is currently missing support for a few features supported
1014// here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr.
1015class ConstExprEmitter :
1016  public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> {
1017  CodeGenModule &CGM;
1018  ConstantEmitter &Emitter;
1019  llvm::LLVMContext &VMContext;
1020public:
1021  ConstExprEmitter(ConstantEmitter &emitter)
1022    : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
1023  }
1024
1025  //===--------------------------------------------------------------------===//
1026  //                            Visitor Methods
1027  //===--------------------------------------------------------------------===//
1028
1029  llvm::Constant *VisitStmt(Stmt *S, QualType T) {
1030    return nullptr;
1031  }
1032
1033  llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) {
1034    if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(CE))
1035      return Result;
1036    return Visit(CE->getSubExpr(), T);
1037  }
1038
1039  llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) {
1040    return Visit(PE->getSubExpr(), T);
1041  }
1042
1043  llvm::Constant *
1044  VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE,
1045                                    QualType T) {
1046    return Visit(PE->getReplacement(), T);
1047  }
1048
1049  llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE,
1050                                            QualType T) {
1051    return Visit(GE->getResultExpr(), T);
1052  }
1053
1054  llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) {
1055    return Visit(CE->getChosenSubExpr(), T);
1056  }
1057
1058  llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) {
1059    return Visit(E->getInitializer(), T);
1060  }
1061
1062  llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) {
1063    if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1064      CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
1065    Expr *subExpr = E->getSubExpr();
1066
1067    switch (E->getCastKind()) {
1068    case CK_ToUnion: {
1069      // GCC cast to union extension
1070      assert(E->getType()->isUnionType() &&
1071             "Destination type is not union type!");
1072
1073      auto field = E->getTargetUnionField();
1074
1075      auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
1076      if (!C) return nullptr;
1077
1078      auto destTy = ConvertType(destType);
1079      if (C->getType() == destTy) return C;
1080
1081      // Build a struct with the union sub-element as the first member,
1082      // and padded to the appropriate size.
1083      SmallVector<llvm::Constant*, 2> Elts;
1084      SmallVector<llvm::Type*, 2> Types;
1085      Elts.push_back(C);
1086      Types.push_back(C->getType());
1087      unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
1088      unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
1089
1090      assert(CurSize <= TotalSize && "Union size mismatch!");
1091      if (unsigned NumPadBytes = TotalSize - CurSize) {
1092        llvm::Type *Ty = CGM.CharTy;
1093        if (NumPadBytes > 1)
1094          Ty = llvm::ArrayType::get(Ty, NumPadBytes);
1095
1096        Elts.push_back(llvm::UndefValue::get(Ty));
1097        Types.push_back(Ty);
1098      }
1099
1100      llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
1101      return llvm::ConstantStruct::get(STy, Elts);
1102    }
1103
1104    case CK_AddressSpaceConversion: {
1105      auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1106      if (!C) return nullptr;
1107      LangAS destAS = E->getType()->getPointeeType().getAddressSpace();
1108      LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace();
1109      llvm::Type *destTy = ConvertType(E->getType());
1110      return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS,
1111                                                             destAS, destTy);
1112    }
1113
1114    case CK_LValueToRValue: {
1115      // We don't really support doing lvalue-to-rvalue conversions here; any
1116      // interesting conversions should be done in Evaluate().  But as a
1117      // special case, allow compound literals to support the gcc extension
1118      // allowing "struct x {int x;} x = (struct x) {};".
1119      if (auto *E = dyn_cast<CompoundLiteralExpr>(subExpr->IgnoreParens()))
1120        return Visit(E->getInitializer(), destType);
1121      return nullptr;
1122    }
1123
1124    case CK_AtomicToNonAtomic:
1125    case CK_NonAtomicToAtomic:
1126    case CK_NoOp:
1127    case CK_ConstructorConversion:
1128      return Visit(subExpr, destType);
1129
1130    case CK_IntToOCLSampler:
1131      llvm_unreachable("global sampler variables are not generated");
1132
1133    case CK_Dependent: llvm_unreachable("saw dependent cast!");
1134
1135    case CK_BuiltinFnToFnPtr:
1136      llvm_unreachable("builtin functions are handled elsewhere");
1137
1138    case CK_ReinterpretMemberPointer:
1139    case CK_DerivedToBaseMemberPointer:
1140    case CK_BaseToDerivedMemberPointer: {
1141      auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1142      if (!C) return nullptr;
1143      return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
1144    }
1145
1146    // These will never be supported.
1147    case CK_ObjCObjectLValueCast:
1148    case CK_ARCProduceObject:
1149    case CK_ARCConsumeObject:
1150    case CK_ARCReclaimReturnedObject:
1151    case CK_ARCExtendBlockObject:
1152    case CK_CopyAndAutoreleaseBlockObject:
1153      return nullptr;
1154
1155    // These don't need to be handled here because Evaluate knows how to
1156    // evaluate them in the cases where they can be folded.
1157    case CK_BitCast:
1158    case CK_ToVoid:
1159    case CK_Dynamic:
1160    case CK_LValueBitCast:
1161    case CK_LValueToRValueBitCast:
1162    case CK_NullToMemberPointer:
1163    case CK_UserDefinedConversion:
1164    case CK_CPointerToObjCPointerCast:
1165    case CK_BlockPointerToObjCPointerCast:
1166    case CK_AnyPointerToBlockPointerCast:
1167    case CK_ArrayToPointerDecay:
1168    case CK_FunctionToPointerDecay:
1169    case CK_BaseToDerived:
1170    case CK_DerivedToBase:
1171    case CK_UncheckedDerivedToBase:
1172    case CK_MemberPointerToBoolean:
1173    case CK_VectorSplat:
1174    case CK_FloatingRealToComplex:
1175    case CK_FloatingComplexToReal:
1176    case CK_FloatingComplexToBoolean:
1177    case CK_FloatingComplexCast:
1178    case CK_FloatingComplexToIntegralComplex:
1179    case CK_IntegralRealToComplex:
1180    case CK_IntegralComplexToReal:
1181    case CK_IntegralComplexToBoolean:
1182    case CK_IntegralComplexCast:
1183    case CK_IntegralComplexToFloatingComplex:
1184    case CK_PointerToIntegral:
1185    case CK_PointerToBoolean:
1186    case CK_NullToPointer:
1187    case CK_IntegralCast:
1188    case CK_BooleanToSignedIntegral:
1189    case CK_IntegralToPointer:
1190    case CK_IntegralToBoolean:
1191    case CK_IntegralToFloating:
1192    case CK_FloatingToIntegral:
1193    case CK_FloatingToBoolean:
1194    case CK_FloatingCast:
1195    case CK_FloatingToFixedPoint:
1196    case CK_FixedPointToFloating:
1197    case CK_FixedPointCast:
1198    case CK_FixedPointToBoolean:
1199    case CK_FixedPointToIntegral:
1200    case CK_IntegralToFixedPoint:
1201    case CK_ZeroToOCLOpaqueType:
1202    case CK_MatrixCast:
1203      return nullptr;
1204    }
1205    llvm_unreachable("Invalid CastKind");
1206  }
1207
1208  llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) {
1209    // No need for a DefaultInitExprScope: we don't handle 'this' in a
1210    // constant expression.
1211    return Visit(DIE->getExpr(), T);
1212  }
1213
1214  llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) {
1215    return Visit(E->getSubExpr(), T);
1216  }
1217
1218  llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E,
1219                                                QualType T) {
1220    return Visit(E->getSubExpr(), T);
1221  }
1222
1223  llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
1224    auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
1225    assert(CAT && "can't emit array init for non-constant-bound array");
1226    unsigned NumInitElements = ILE->getNumInits();
1227    unsigned NumElements = CAT->getSize().getZExtValue();
1228
1229    // Initialising an array requires us to automatically
1230    // initialise any elements that have not been initialised explicitly
1231    unsigned NumInitableElts = std::min(NumInitElements, NumElements);
1232
1233    QualType EltType = CAT->getElementType();
1234
1235    // Initialize remaining array elements.
1236    llvm::Constant *fillC = nullptr;
1237    if (Expr *filler = ILE->getArrayFiller()) {
1238      fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
1239      if (!fillC)
1240        return nullptr;
1241    }
1242
1243    // Copy initializer elements.
1244    SmallVector<llvm::Constant*, 16> Elts;
1245    if (fillC && fillC->isNullValue())
1246      Elts.reserve(NumInitableElts + 1);
1247    else
1248      Elts.reserve(NumElements);
1249
1250    llvm::Type *CommonElementType = nullptr;
1251    for (unsigned i = 0; i < NumInitableElts; ++i) {
1252      Expr *Init = ILE->getInit(i);
1253      llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType);
1254      if (!C)
1255        return nullptr;
1256      if (i == 0)
1257        CommonElementType = C->getType();
1258      else if (C->getType() != CommonElementType)
1259        CommonElementType = nullptr;
1260      Elts.push_back(C);
1261    }
1262
1263    llvm::ArrayType *Desired =
1264        cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType()));
1265    return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
1266                             fillC);
1267  }
1268
1269  llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {
1270    return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
1271  }
1272
1273  llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E,
1274                                             QualType T) {
1275    return CGM.EmitNullConstant(T);
1276  }
1277
1278  llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) {
1279    if (ILE->isTransparent())
1280      return Visit(ILE->getInit(0), T);
1281
1282    if (ILE->getType()->isArrayType())
1283      return EmitArrayInitialization(ILE, T);
1284
1285    if (ILE->getType()->isRecordType())
1286      return EmitRecordInitialization(ILE, T);
1287
1288    return nullptr;
1289  }
1290
1291  llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E,
1292                                                QualType destType) {
1293    auto C = Visit(E->getBase(), destType);
1294    if (!C)
1295      return nullptr;
1296
1297    ConstantAggregateBuilder Const(CGM);
1298    Const.add(C, CharUnits::Zero(), false);
1299
1300    if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType,
1301                                   E->getUpdater()))
1302      return nullptr;
1303
1304    llvm::Type *ValTy = CGM.getTypes().ConvertType(destType);
1305    bool HasFlexibleArray = false;
1306    if (auto *RT = destType->getAs<RecordType>())
1307      HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember();
1308    return Const.build(ValTy, HasFlexibleArray);
1309  }
1310
1311  llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) {
1312    if (!E->getConstructor()->isTrivial())
1313      return nullptr;
1314
1315    // Only default and copy/move constructors can be trivial.
1316    if (E->getNumArgs()) {
1317      assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
1318      assert(E->getConstructor()->isCopyOrMoveConstructor() &&
1319             "trivial ctor has argument but isn't a copy/move ctor");
1320
1321      Expr *Arg = E->getArg(0);
1322      assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
1323             "argument to copy ctor is of wrong type");
1324
1325      return Visit(Arg, Ty);
1326    }
1327
1328    return CGM.EmitNullConstant(Ty);
1329  }
1330
1331  llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) {
1332    // This is a string literal initializing an array in an initializer.
1333    return CGM.GetConstantArrayFromStringLiteral(E);
1334  }
1335
1336  llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) {
1337    // This must be an @encode initializing an array in a static initializer.
1338    // Don't emit it as the address of the string, emit the string data itself
1339    // as an inline array.
1340    std::string Str;
1341    CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str);
1342    const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
1343
1344    // Resize the string to the right size, adding zeros at the end, or
1345    // truncating as needed.
1346    Str.resize(CAT->getSize().getZExtValue(), '\0');
1347    return llvm::ConstantDataArray::getString(VMContext, Str, false);
1348  }
1349
1350  llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1351    return Visit(E->getSubExpr(), T);
1352  }
1353
1354  // Utility methods
1355  llvm::Type *ConvertType(QualType T) {
1356    return CGM.getTypes().ConvertType(T);
1357  }
1358};
1359
1360}  // end anonymous namespace.
1361
1362llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
1363                                                        AbstractState saved) {
1364  Abstract = saved.OldValue;
1365
1366  assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
1367         "created a placeholder while doing an abstract emission?");
1368
1369  // No validation necessary for now.
1370  // No cleanup to do for now.
1371  return C;
1372}
1373
1374llvm::Constant *
1375ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
1376  auto state = pushAbstract();
1377  auto C = tryEmitPrivateForVarInit(D);
1378  return validateAndPopAbstract(C, state);
1379}
1380
1381llvm::Constant *
1382ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) {
1383  auto state = pushAbstract();
1384  auto C = tryEmitPrivate(E, destType);
1385  return validateAndPopAbstract(C, state);
1386}
1387
1388llvm::Constant *
1389ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) {
1390  auto state = pushAbstract();
1391  auto C = tryEmitPrivate(value, destType);
1392  return validateAndPopAbstract(C, state);
1393}
1394
1395llvm::Constant *ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) {
1396  if (!CE->hasAPValueResult())
1397    return nullptr;
1398
1399  QualType RetType = CE->getType();
1400  if (CE->isGLValue())
1401    RetType = CGM.getContext().getLValueReferenceType(RetType);
1402
1403  return emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(), RetType);
1404}
1405
1406llvm::Constant *
1407ConstantEmitter::emitAbstract(const Expr *E, QualType destType) {
1408  auto state = pushAbstract();
1409  auto C = tryEmitPrivate(E, destType);
1410  C = validateAndPopAbstract(C, state);
1411  if (!C) {
1412    CGM.Error(E->getExprLoc(),
1413              "internal error: could not emit constant value \"abstractly\"");
1414    C = CGM.EmitNullConstant(destType);
1415  }
1416  return C;
1417}
1418
1419llvm::Constant *
1420ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value,
1421                              QualType destType) {
1422  auto state = pushAbstract();
1423  auto C = tryEmitPrivate(value, destType);
1424  C = validateAndPopAbstract(C, state);
1425  if (!C) {
1426    CGM.Error(loc,
1427              "internal error: could not emit constant value \"abstractly\"");
1428    C = CGM.EmitNullConstant(destType);
1429  }
1430  return C;
1431}
1432
1433llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) {
1434  initializeNonAbstract(D.getType().getAddressSpace());
1435  return markIfFailed(tryEmitPrivateForVarInit(D));
1436}
1437
1438llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E,
1439                                                       LangAS destAddrSpace,
1440                                                       QualType destType) {
1441  initializeNonAbstract(destAddrSpace);
1442  return markIfFailed(tryEmitPrivateForMemory(E, destType));
1443}
1444
1445llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value,
1446                                                    LangAS destAddrSpace,
1447                                                    QualType destType) {
1448  initializeNonAbstract(destAddrSpace);
1449  auto C = tryEmitPrivateForMemory(value, destType);
1450  assert(C && "couldn't emit constant value non-abstractly?");
1451  return C;
1452}
1453
1454llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() {
1455  assert(!Abstract && "cannot get current address for abstract constant");
1456
1457
1458
1459  // Make an obviously ill-formed global that should blow up compilation
1460  // if it survives.
1461  auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
1462                                         llvm::GlobalValue::PrivateLinkage,
1463                                         /*init*/ nullptr,
1464                                         /*name*/ "",
1465                                         /*before*/ nullptr,
1466                                         llvm::GlobalVariable::NotThreadLocal,
1467                                         CGM.getContext().getTargetAddressSpace(DestAddressSpace));
1468
1469  PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1470
1471  return global;
1472}
1473
1474void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal,
1475                                           llvm::GlobalValue *placeholder) {
1476  assert(!PlaceholderAddresses.empty());
1477  assert(PlaceholderAddresses.back().first == nullptr);
1478  assert(PlaceholderAddresses.back().second == placeholder);
1479  PlaceholderAddresses.back().first = signal;
1480}
1481
1482namespace {
1483  struct ReplacePlaceholders {
1484    CodeGenModule &CGM;
1485
1486    /// The base address of the global.
1487    llvm::Constant *Base;
1488    llvm::Type *BaseValueTy = nullptr;
1489
1490    /// The placeholder addresses that were registered during emission.
1491    llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
1492
1493    /// The locations of the placeholder signals.
1494    llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
1495
1496    /// The current index stack.  We use a simple unsigned stack because
1497    /// we assume that placeholders will be relatively sparse in the
1498    /// initializer, but we cache the index values we find just in case.
1499    llvm::SmallVector<unsigned, 8> Indices;
1500    llvm::SmallVector<llvm::Constant*, 8> IndexValues;
1501
1502    ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
1503                        ArrayRef<std::pair<llvm::Constant*,
1504                                           llvm::GlobalVariable*>> addresses)
1505        : CGM(CGM), Base(base),
1506          PlaceholderAddresses(addresses.begin(), addresses.end()) {
1507    }
1508
1509    void replaceInInitializer(llvm::Constant *init) {
1510      // Remember the type of the top-most initializer.
1511      BaseValueTy = init->getType();
1512
1513      // Initialize the stack.
1514      Indices.push_back(0);
1515      IndexValues.push_back(nullptr);
1516
1517      // Recurse into the initializer.
1518      findLocations(init);
1519
1520      // Check invariants.
1521      assert(IndexValues.size() == Indices.size() && "mismatch");
1522      assert(Indices.size() == 1 && "didn't pop all indices");
1523
1524      // Do the replacement; this basically invalidates 'init'.
1525      assert(Locations.size() == PlaceholderAddresses.size() &&
1526             "missed a placeholder?");
1527
1528      // We're iterating over a hashtable, so this would be a source of
1529      // non-determinism in compiler output *except* that we're just
1530      // messing around with llvm::Constant structures, which never itself
1531      // does anything that should be visible in compiler output.
1532      for (auto &entry : Locations) {
1533        assert(entry.first->getParent() == nullptr && "not a placeholder!");
1534        entry.first->replaceAllUsesWith(entry.second);
1535        entry.first->eraseFromParent();
1536      }
1537    }
1538
1539  private:
1540    void findLocations(llvm::Constant *init) {
1541      // Recurse into aggregates.
1542      if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1543        for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1544          Indices.push_back(i);
1545          IndexValues.push_back(nullptr);
1546
1547          findLocations(agg->getOperand(i));
1548
1549          IndexValues.pop_back();
1550          Indices.pop_back();
1551        }
1552        return;
1553      }
1554
1555      // Otherwise, check for registered constants.
1556      while (true) {
1557        auto it = PlaceholderAddresses.find(init);
1558        if (it != PlaceholderAddresses.end()) {
1559          setLocation(it->second);
1560          break;
1561        }
1562
1563        // Look through bitcasts or other expressions.
1564        if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
1565          init = expr->getOperand(0);
1566        } else {
1567          break;
1568        }
1569      }
1570    }
1571
1572    void setLocation(llvm::GlobalVariable *placeholder) {
1573      assert(Locations.find(placeholder) == Locations.end() &&
1574             "already found location for placeholder!");
1575
1576      // Lazily fill in IndexValues with the values from Indices.
1577      // We do this in reverse because we should always have a strict
1578      // prefix of indices from the start.
1579      assert(Indices.size() == IndexValues.size());
1580      for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
1581        if (IndexValues[i]) {
1582#ifndef NDEBUG
1583          for (size_t j = 0; j != i + 1; ++j) {
1584            assert(IndexValues[j] &&
1585                   isa<llvm::ConstantInt>(IndexValues[j]) &&
1586                   cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
1587                     == Indices[j]);
1588          }
1589#endif
1590          break;
1591        }
1592
1593        IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
1594      }
1595
1596      // Form a GEP and then bitcast to the placeholder type so that the
1597      // replacement will succeed.
1598      llvm::Constant *location =
1599        llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy,
1600                                                     Base, IndexValues);
1601      location = llvm::ConstantExpr::getBitCast(location,
1602                                                placeholder->getType());
1603
1604      Locations.insert({placeholder, location});
1605    }
1606  };
1607}
1608
1609void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
1610  assert(InitializedNonAbstract &&
1611         "finalizing emitter that was used for abstract emission?");
1612  assert(!Finalized && "finalizing emitter multiple times");
1613  assert(global->getInitializer());
1614
1615  // Note that we might also be Failed.
1616  Finalized = true;
1617
1618  if (!PlaceholderAddresses.empty()) {
1619    ReplacePlaceholders(CGM, global, PlaceholderAddresses)
1620      .replaceInInitializer(global->getInitializer());
1621    PlaceholderAddresses.clear(); // satisfy
1622  }
1623}
1624
1625ConstantEmitter::~ConstantEmitter() {
1626  assert((!InitializedNonAbstract || Finalized || Failed) &&
1627         "not finalized after being initialized for non-abstract emission");
1628  assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1629}
1630
1631static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) {
1632  if (auto AT = type->getAs<AtomicType>()) {
1633    return CGM.getContext().getQualifiedType(AT->getValueType(),
1634                                             type.getQualifiers());
1635  }
1636  return type;
1637}
1638
1639llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
1640  // Make a quick check if variable can be default NULL initialized
1641  // and avoid going through rest of code which may do, for c++11,
1642  // initialization of memory to all NULLs.
1643  if (!D.hasLocalStorage()) {
1644    QualType Ty = CGM.getContext().getBaseElementType(D.getType());
1645    if (Ty->isRecordType())
1646      if (const CXXConstructExpr *E =
1647          dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1648        const CXXConstructorDecl *CD = E->getConstructor();
1649        if (CD->isTrivial() && CD->isDefaultConstructor())
1650          return CGM.EmitNullConstant(D.getType());
1651      }
1652  }
1653  InConstantContext = D.hasConstantInitialization();
1654
1655  QualType destType = D.getType();
1656
1657  // Try to emit the initializer.  Note that this can allow some things that
1658  // are not allowed by tryEmitPrivateForMemory alone.
1659  if (auto value = D.evaluateValue()) {
1660    return tryEmitPrivateForMemory(*value, destType);
1661  }
1662
1663  // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
1664  // reference is a constant expression, and the reference binds to a temporary,
1665  // then constant initialization is performed. ConstExprEmitter will
1666  // incorrectly emit a prvalue constant in this case, and the calling code
1667  // interprets that as the (pointer) value of the reference, rather than the
1668  // desired value of the referee.
1669  if (destType->isReferenceType())
1670    return nullptr;
1671
1672  const Expr *E = D.getInit();
1673  assert(E && "No initializer to emit");
1674
1675  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1676  auto C =
1677    ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
1678  return (C ? emitForMemory(C, destType) : nullptr);
1679}
1680
1681llvm::Constant *
1682ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) {
1683  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1684  auto C = tryEmitAbstract(E, nonMemoryDestType);
1685  return (C ? emitForMemory(C, destType) : nullptr);
1686}
1687
1688llvm::Constant *
1689ConstantEmitter::tryEmitAbstractForMemory(const APValue &value,
1690                                          QualType destType) {
1691  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1692  auto C = tryEmitAbstract(value, nonMemoryDestType);
1693  return (C ? emitForMemory(C, destType) : nullptr);
1694}
1695
1696llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E,
1697                                                         QualType destType) {
1698  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1699  llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1700  return (C ? emitForMemory(C, destType) : nullptr);
1701}
1702
1703llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value,
1704                                                         QualType destType) {
1705  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1706  auto C = tryEmitPrivate(value, nonMemoryDestType);
1707  return (C ? emitForMemory(C, destType) : nullptr);
1708}
1709
1710llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM,
1711                                               llvm::Constant *C,
1712                                               QualType destType) {
1713  // For an _Atomic-qualified constant, we may need to add tail padding.
1714  if (auto AT = destType->getAs<AtomicType>()) {
1715    QualType destValueType = AT->getValueType();
1716    C = emitForMemory(CGM, C, destValueType);
1717
1718    uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
1719    uint64_t outerSize = CGM.getContext().getTypeSize(destType);
1720    if (innerSize == outerSize)
1721      return C;
1722
1723    assert(innerSize < outerSize && "emitted over-large constant for atomic");
1724    llvm::Constant *elts[] = {
1725      C,
1726      llvm::ConstantAggregateZero::get(
1727          llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
1728    };
1729    return llvm::ConstantStruct::getAnon(elts);
1730  }
1731
1732  // Zero-extend bool.
1733  if (C->getType()->isIntegerTy(1)) {
1734    llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
1735    return llvm::ConstantExpr::getZExt(C, boolTy);
1736  }
1737
1738  return C;
1739}
1740
1741llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
1742                                                QualType destType) {
1743  assert(!destType->isVoidType() && "can't emit a void constant");
1744
1745  Expr::EvalResult Result;
1746
1747  bool Success = false;
1748
1749  if (destType->isReferenceType())
1750    Success = E->EvaluateAsLValue(Result, CGM.getContext());
1751  else
1752    Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
1753
1754  llvm::Constant *C;
1755  if (Success && !Result.HasSideEffects)
1756    C = tryEmitPrivate(Result.Val, destType);
1757  else
1758    C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
1759
1760  return C;
1761}
1762
1763llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
1764  return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
1765}
1766
1767namespace {
1768/// A struct which can be used to peephole certain kinds of finalization
1769/// that normally happen during l-value emission.
1770struct ConstantLValue {
1771  llvm::Constant *Value;
1772  bool HasOffsetApplied;
1773
1774  /*implicit*/ ConstantLValue(llvm::Constant *value,
1775                              bool hasOffsetApplied = false)
1776    : Value(value), HasOffsetApplied(hasOffsetApplied) {}
1777
1778  /*implicit*/ ConstantLValue(ConstantAddress address)
1779    : ConstantLValue(address.getPointer()) {}
1780};
1781
1782/// A helper class for emitting constant l-values.
1783class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
1784                                                      ConstantLValue> {
1785  CodeGenModule &CGM;
1786  ConstantEmitter &Emitter;
1787  const APValue &Value;
1788  QualType DestType;
1789
1790  // Befriend StmtVisitorBase so that we don't have to expose Visit*.
1791  friend StmtVisitorBase;
1792
1793public:
1794  ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
1795                        QualType destType)
1796    : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {}
1797
1798  llvm::Constant *tryEmit();
1799
1800private:
1801  llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
1802  ConstantLValue tryEmitBase(const APValue::LValueBase &base);
1803
1804  ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
1805  ConstantLValue VisitConstantExpr(const ConstantExpr *E);
1806  ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
1807  ConstantLValue VisitStringLiteral(const StringLiteral *E);
1808  ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
1809  ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
1810  ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
1811  ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
1812  ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
1813  ConstantLValue VisitCallExpr(const CallExpr *E);
1814  ConstantLValue VisitBlockExpr(const BlockExpr *E);
1815  ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
1816  ConstantLValue VisitMaterializeTemporaryExpr(
1817                                         const MaterializeTemporaryExpr *E);
1818
1819  bool hasNonZeroOffset() const {
1820    return !Value.getLValueOffset().isZero();
1821  }
1822
1823  /// Return the value offset.
1824  llvm::Constant *getOffset() {
1825    return llvm::ConstantInt::get(CGM.Int64Ty,
1826                                  Value.getLValueOffset().getQuantity());
1827  }
1828
1829  /// Apply the value offset to the given constant.
1830  llvm::Constant *applyOffset(llvm::Constant *C) {
1831    if (!hasNonZeroOffset())
1832      return C;
1833
1834    llvm::Type *origPtrTy = C->getType();
1835    unsigned AS = origPtrTy->getPointerAddressSpace();
1836    llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS);
1837    C = llvm::ConstantExpr::getBitCast(C, charPtrTy);
1838    C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset());
1839    C = llvm::ConstantExpr::getPointerCast(C, origPtrTy);
1840    return C;
1841  }
1842};
1843
1844}
1845
1846llvm::Constant *ConstantLValueEmitter::tryEmit() {
1847  const APValue::LValueBase &base = Value.getLValueBase();
1848
1849  // The destination type should be a pointer or reference
1850  // type, but it might also be a cast thereof.
1851  //
1852  // FIXME: the chain of casts required should be reflected in the APValue.
1853  // We need this in order to correctly handle things like a ptrtoint of a
1854  // non-zero null pointer and addrspace casts that aren't trivially
1855  // represented in LLVM IR.
1856  auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
1857  assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
1858
1859  // If there's no base at all, this is a null or absolute pointer,
1860  // possibly cast back to an integer type.
1861  if (!base) {
1862    return tryEmitAbsolute(destTy);
1863  }
1864
1865  // Otherwise, try to emit the base.
1866  ConstantLValue result = tryEmitBase(base);
1867
1868  // If that failed, we're done.
1869  llvm::Constant *value = result.Value;
1870  if (!value) return nullptr;
1871
1872  // Apply the offset if necessary and not already done.
1873  if (!result.HasOffsetApplied) {
1874    value = applyOffset(value);
1875  }
1876
1877  // Convert to the appropriate type; this could be an lvalue for
1878  // an integer.  FIXME: performAddrSpaceCast
1879  if (isa<llvm::PointerType>(destTy))
1880    return llvm::ConstantExpr::getPointerCast(value, destTy);
1881
1882  return llvm::ConstantExpr::getPtrToInt(value, destTy);
1883}
1884
1885/// Try to emit an absolute l-value, such as a null pointer or an integer
1886/// bitcast to pointer type.
1887llvm::Constant *
1888ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
1889  // If we're producing a pointer, this is easy.
1890  auto destPtrTy = cast<llvm::PointerType>(destTy);
1891  if (Value.isNullPointer()) {
1892    // FIXME: integer offsets from non-zero null pointers.
1893    return CGM.getNullPointer(destPtrTy, DestType);
1894  }
1895
1896  // Convert the integer to a pointer-sized integer before converting it
1897  // to a pointer.
1898  // FIXME: signedness depends on the original integer type.
1899  auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
1900  llvm::Constant *C;
1901  C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy,
1902                                         /*isSigned*/ false);
1903  C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
1904  return C;
1905}
1906
1907ConstantLValue
1908ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
1909  // Handle values.
1910  if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
1911    // The constant always points to the canonical declaration. We want to look
1912    // at properties of the most recent declaration at the point of emission.
1913    D = cast<ValueDecl>(D->getMostRecentDecl());
1914
1915    if (D->hasAttr<WeakRefAttr>())
1916      return CGM.GetWeakRefReference(D).getPointer();
1917
1918    if (auto FD = dyn_cast<FunctionDecl>(D))
1919      return CGM.GetAddrOfFunction(FD);
1920
1921    if (auto VD = dyn_cast<VarDecl>(D)) {
1922      // We can never refer to a variable with local storage.
1923      if (!VD->hasLocalStorage()) {
1924        if (VD->isFileVarDecl() || VD->hasExternalStorage())
1925          return CGM.GetAddrOfGlobalVar(VD);
1926
1927        if (VD->isLocalVarDecl()) {
1928          return CGM.getOrCreateStaticVarDecl(
1929              *VD, CGM.getLLVMLinkageVarDefinition(VD, /*IsConstant=*/false));
1930        }
1931      }
1932    }
1933
1934    if (auto *GD = dyn_cast<MSGuidDecl>(D))
1935      return CGM.GetAddrOfMSGuidDecl(GD);
1936
1937    if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(D))
1938      return CGM.GetAddrOfUnnamedGlobalConstantDecl(GCD);
1939
1940    if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D))
1941      return CGM.GetAddrOfTemplateParamObject(TPO);
1942
1943    return nullptr;
1944  }
1945
1946  // Handle typeid(T).
1947  if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) {
1948    llvm::Type *StdTypeInfoPtrTy =
1949        CGM.getTypes().ConvertType(base.getTypeInfoType())->getPointerTo();
1950    llvm::Constant *TypeInfo =
1951        CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0));
1952    if (TypeInfo->getType() != StdTypeInfoPtrTy)
1953      TypeInfo = llvm::ConstantExpr::getBitCast(TypeInfo, StdTypeInfoPtrTy);
1954    return TypeInfo;
1955  }
1956
1957  // Otherwise, it must be an expression.
1958  return Visit(base.get<const Expr*>());
1959}
1960
1961ConstantLValue
1962ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
1963  if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(E))
1964    return Result;
1965  return Visit(E->getSubExpr());
1966}
1967
1968ConstantLValue
1969ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
1970  ConstantEmitter CompoundLiteralEmitter(CGM, Emitter.CGF);
1971  CompoundLiteralEmitter.setInConstantContext(Emitter.isInConstantContext());
1972  return tryEmitGlobalCompoundLiteral(CompoundLiteralEmitter, E);
1973}
1974
1975ConstantLValue
1976ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
1977  return CGM.GetAddrOfConstantStringFromLiteral(E);
1978}
1979
1980ConstantLValue
1981ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
1982  return CGM.GetAddrOfConstantStringFromObjCEncode(E);
1983}
1984
1985static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S,
1986                                                    QualType T,
1987                                                    CodeGenModule &CGM) {
1988  auto C = CGM.getObjCRuntime().GenerateConstantString(S);
1989  return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(T));
1990}
1991
1992ConstantLValue
1993ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
1994  return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM);
1995}
1996
1997ConstantLValue
1998ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
1999  assert(E->isExpressibleAsConstantInitializer() &&
2000         "this boxed expression can't be emitted as a compile-time constant");
2001  auto *SL = cast<StringLiteral>(E->getSubExpr()->IgnoreParenCasts());
2002  return emitConstantObjCStringLiteral(SL, E->getType(), CGM);
2003}
2004
2005ConstantLValue
2006ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
2007  return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName());
2008}
2009
2010ConstantLValue
2011ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2012  assert(Emitter.CGF && "Invalid address of label expression outside function");
2013  llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
2014  Ptr = llvm::ConstantExpr::getBitCast(Ptr,
2015                                   CGM.getTypes().ConvertType(E->getType()));
2016  return Ptr;
2017}
2018
2019ConstantLValue
2020ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
2021  unsigned builtin = E->getBuiltinCallee();
2022  if (builtin == Builtin::BI__builtin_function_start)
2023    return CGM.GetFunctionStart(
2024        E->getArg(0)->getAsBuiltinConstantDeclRef(CGM.getContext()));
2025  if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
2026      builtin != Builtin::BI__builtin___NSStringMakeConstantString)
2027    return nullptr;
2028
2029  auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
2030  if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
2031    return CGM.getObjCRuntime().GenerateConstantString(literal);
2032  } else {
2033    // FIXME: need to deal with UCN conversion issues.
2034    return CGM.GetAddrOfConstantCFString(literal);
2035  }
2036}
2037
2038ConstantLValue
2039ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
2040  StringRef functionName;
2041  if (auto CGF = Emitter.CGF)
2042    functionName = CGF->CurFn->getName();
2043  else
2044    functionName = "global";
2045
2046  return CGM.GetAddrOfGlobalBlock(E, functionName);
2047}
2048
2049ConstantLValue
2050ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2051  QualType T;
2052  if (E->isTypeOperand())
2053    T = E->getTypeOperand(CGM.getContext());
2054  else
2055    T = E->getExprOperand()->getType();
2056  return CGM.GetAddrOfRTTIDescriptor(T);
2057}
2058
2059ConstantLValue
2060ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
2061                                            const MaterializeTemporaryExpr *E) {
2062  assert(E->getStorageDuration() == SD_Static);
2063  SmallVector<const Expr *, 2> CommaLHSs;
2064  SmallVector<SubobjectAdjustment, 2> Adjustments;
2065  const Expr *Inner =
2066      E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
2067  return CGM.GetAddrOfGlobalTemporary(E, Inner);
2068}
2069
2070llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value,
2071                                                QualType DestType) {
2072  switch (Value.getKind()) {
2073  case APValue::None:
2074  case APValue::Indeterminate:
2075    // Out-of-lifetime and indeterminate values can be modeled as 'undef'.
2076    return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType));
2077  case APValue::LValue:
2078    return ConstantLValueEmitter(*this, Value, DestType).tryEmit();
2079  case APValue::Int:
2080    return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
2081  case APValue::FixedPoint:
2082    return llvm::ConstantInt::get(CGM.getLLVMContext(),
2083                                  Value.getFixedPoint().getValue());
2084  case APValue::ComplexInt: {
2085    llvm::Constant *Complex[2];
2086
2087    Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2088                                        Value.getComplexIntReal());
2089    Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2090                                        Value.getComplexIntImag());
2091
2092    // FIXME: the target may want to specify that this is packed.
2093    llvm::StructType *STy =
2094        llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2095    return llvm::ConstantStruct::get(STy, Complex);
2096  }
2097  case APValue::Float: {
2098    const llvm::APFloat &Init = Value.getFloat();
2099    if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
2100        !CGM.getContext().getLangOpts().NativeHalfType &&
2101        CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics())
2102      return llvm::ConstantInt::get(CGM.getLLVMContext(),
2103                                    Init.bitcastToAPInt());
2104    else
2105      return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
2106  }
2107  case APValue::ComplexFloat: {
2108    llvm::Constant *Complex[2];
2109
2110    Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2111                                       Value.getComplexFloatReal());
2112    Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2113                                       Value.getComplexFloatImag());
2114
2115    // FIXME: the target may want to specify that this is packed.
2116    llvm::StructType *STy =
2117        llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2118    return llvm::ConstantStruct::get(STy, Complex);
2119  }
2120  case APValue::Vector: {
2121    unsigned NumElts = Value.getVectorLength();
2122    SmallVector<llvm::Constant *, 4> Inits(NumElts);
2123
2124    for (unsigned I = 0; I != NumElts; ++I) {
2125      const APValue &Elt = Value.getVectorElt(I);
2126      if (Elt.isInt())
2127        Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
2128      else if (Elt.isFloat())
2129        Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
2130      else
2131        llvm_unreachable("unsupported vector element type");
2132    }
2133    return llvm::ConstantVector::get(Inits);
2134  }
2135  case APValue::AddrLabelDiff: {
2136    const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
2137    const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
2138    llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
2139    llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
2140    if (!LHS || !RHS) return nullptr;
2141
2142    // Compute difference
2143    llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
2144    LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
2145    RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
2146    llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
2147
2148    // LLVM is a bit sensitive about the exact format of the
2149    // address-of-label difference; make sure to truncate after
2150    // the subtraction.
2151    return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
2152  }
2153  case APValue::Struct:
2154  case APValue::Union:
2155    return ConstStructBuilder::BuildStruct(*this, Value, DestType);
2156  case APValue::Array: {
2157    const ArrayType *ArrayTy = CGM.getContext().getAsArrayType(DestType);
2158    unsigned NumElements = Value.getArraySize();
2159    unsigned NumInitElts = Value.getArrayInitializedElts();
2160
2161    // Emit array filler, if there is one.
2162    llvm::Constant *Filler = nullptr;
2163    if (Value.hasArrayFiller()) {
2164      Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
2165                                        ArrayTy->getElementType());
2166      if (!Filler)
2167        return nullptr;
2168    }
2169
2170    // Emit initializer elements.
2171    SmallVector<llvm::Constant*, 16> Elts;
2172    if (Filler && Filler->isNullValue())
2173      Elts.reserve(NumInitElts + 1);
2174    else
2175      Elts.reserve(NumElements);
2176
2177    llvm::Type *CommonElementType = nullptr;
2178    for (unsigned I = 0; I < NumInitElts; ++I) {
2179      llvm::Constant *C = tryEmitPrivateForMemory(
2180          Value.getArrayInitializedElt(I), ArrayTy->getElementType());
2181      if (!C) return nullptr;
2182
2183      if (I == 0)
2184        CommonElementType = C->getType();
2185      else if (C->getType() != CommonElementType)
2186        CommonElementType = nullptr;
2187      Elts.push_back(C);
2188    }
2189
2190    llvm::ArrayType *Desired =
2191        cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
2192    return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
2193                             Filler);
2194  }
2195  case APValue::MemberPointer:
2196    return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
2197  }
2198  llvm_unreachable("Unknown APValue kind");
2199}
2200
2201llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted(
2202    const CompoundLiteralExpr *E) {
2203  return EmittedCompoundLiterals.lookup(E);
2204}
2205
2206void CodeGenModule::setAddrOfConstantCompoundLiteral(
2207    const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
2208  bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
2209  (void)Ok;
2210  assert(Ok && "CLE has already been emitted!");
2211}
2212
2213ConstantAddress
2214CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) {
2215  assert(E->isFileScope() && "not a file-scope compound literal expr");
2216  ConstantEmitter emitter(*this);
2217  return tryEmitGlobalCompoundLiteral(emitter, E);
2218}
2219
2220llvm::Constant *
2221CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) {
2222  // Member pointer constants always have a very particular form.
2223  const MemberPointerType *type = cast<MemberPointerType>(uo->getType());
2224  const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2225
2226  // A member function pointer.
2227  if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2228    return getCXXABI().EmitMemberFunctionPointer(method);
2229
2230  // Otherwise, a member data pointer.
2231  uint64_t fieldOffset = getContext().getFieldOffset(decl);
2232  CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2233  return getCXXABI().EmitMemberDataPointer(type, chars);
2234}
2235
2236static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2237                                               llvm::Type *baseType,
2238                                               const CXXRecordDecl *base);
2239
2240static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
2241                                        const RecordDecl *record,
2242                                        bool asCompleteObject) {
2243  const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
2244  llvm::StructType *structure =
2245    (asCompleteObject ? layout.getLLVMType()
2246                      : layout.getBaseSubobjectLLVMType());
2247
2248  unsigned numElements = structure->getNumElements();
2249  std::vector<llvm::Constant *> elements(numElements);
2250
2251  auto CXXR = dyn_cast<CXXRecordDecl>(record);
2252  // Fill in all the bases.
2253  if (CXXR) {
2254    for (const auto &I : CXXR->bases()) {
2255      if (I.isVirtual()) {
2256        // Ignore virtual bases; if we're laying out for a complete
2257        // object, we'll lay these out later.
2258        continue;
2259      }
2260
2261      const CXXRecordDecl *base =
2262        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2263
2264      // Ignore empty bases.
2265      if (base->isEmpty() ||
2266          CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
2267              .isZero())
2268        continue;
2269
2270      unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2271      llvm::Type *baseType = structure->getElementType(fieldIndex);
2272      elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2273    }
2274  }
2275
2276  // Fill in all the fields.
2277  for (const auto *Field : record->fields()) {
2278    // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
2279    // will fill in later.)
2280    if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())) {
2281      unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2282      elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
2283    }
2284
2285    // For unions, stop after the first named field.
2286    if (record->isUnion()) {
2287      if (Field->getIdentifier())
2288        break;
2289      if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
2290        if (FieldRD->findFirstNamedDataMember())
2291          break;
2292    }
2293  }
2294
2295  // Fill in the virtual bases, if we're working with the complete object.
2296  if (CXXR && asCompleteObject) {
2297    for (const auto &I : CXXR->vbases()) {
2298      const CXXRecordDecl *base =
2299        cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
2300
2301      // Ignore empty bases.
2302      if (base->isEmpty())
2303        continue;
2304
2305      unsigned fieldIndex = layout.getVirtualBaseIndex(base);
2306
2307      // We might have already laid this field out.
2308      if (elements[fieldIndex]) continue;
2309
2310      llvm::Type *baseType = structure->getElementType(fieldIndex);
2311      elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2312    }
2313  }
2314
2315  // Now go through all other fields and zero them out.
2316  for (unsigned i = 0; i != numElements; ++i) {
2317    if (!elements[i])
2318      elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
2319  }
2320
2321  return llvm::ConstantStruct::get(structure, elements);
2322}
2323
2324/// Emit the null constant for a base subobject.
2325static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2326                                               llvm::Type *baseType,
2327                                               const CXXRecordDecl *base) {
2328  const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
2329
2330  // Just zero out bases that don't have any pointer to data members.
2331  if (baseLayout.isZeroInitializableAsBase())
2332    return llvm::Constant::getNullValue(baseType);
2333
2334  // Otherwise, we can just use its null constant.
2335  return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2336}
2337
2338llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM,
2339                                                   QualType T) {
2340  return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2341}
2342
2343llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) {
2344  if (T->getAs<PointerType>())
2345    return getNullPointer(
2346        cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2347
2348  if (getTypes().isZeroInitializable(T))
2349    return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2350
2351  if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2352    llvm::ArrayType *ATy =
2353      cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2354
2355    QualType ElementTy = CAT->getElementType();
2356
2357    llvm::Constant *Element =
2358      ConstantEmitter::emitNullForMemory(*this, ElementTy);
2359    unsigned NumElements = CAT->getSize().getZExtValue();
2360    SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2361    return llvm::ConstantArray::get(ATy, Array);
2362  }
2363
2364  if (const RecordType *RT = T->getAs<RecordType>())
2365    return ::EmitNullConstant(*this, RT->getDecl(), /*complete object*/ true);
2366
2367  assert(T->isMemberDataPointerType() &&
2368         "Should only see pointers to data members here!");
2369
2370  return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2371}
2372
2373llvm::Constant *
2374CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) {
2375  return ::EmitNullConstant(*this, Record, false);
2376}
2377