clang 23.0.0git
CGExprConstant.cpp
Go to the documentation of this file.
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 "ABIInfoImpl.h"
14#include "CGCXXABI.h"
15#include "CGObjCRuntime.h"
16#include "CGRecordLayout.h"
17#include "CodeGenFunction.h"
18#include "CodeGenModule.h"
19#include "ConstantEmitter.h"
20#include "TargetInfo.h"
21#include "clang/AST/APValue.h"
23#include "clang/AST/Attr.h"
24#include "clang/AST/NSAPI.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/Sequence.h"
30#include "llvm/Analysis/ConstantFolding.h"
31#include "llvm/IR/Constants.h"
32#include "llvm/IR/DataLayout.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/Support/SipHash.h"
36#include <optional>
37using namespace clang;
38using namespace CodeGen;
39
40//===----------------------------------------------------------------------===//
41// ConstantAggregateBuilder
42//===----------------------------------------------------------------------===//
43
44namespace {
45class ConstExprEmitter;
46
47llvm::Constant *getPadding(const CodeGenModule &CGM, CharUnits PadSize) {
48 llvm::Type *Ty = CGM.CharTy;
49 if (PadSize > CharUnits::One())
50 Ty = llvm::ArrayType::get(Ty, PadSize.getQuantity());
51 if (CGM.shouldZeroInitPadding()) {
52 return llvm::Constant::getNullValue(Ty);
53 }
54 return llvm::UndefValue::get(Ty);
55}
56
57struct ConstantAggregateBuilderUtils {
58 CodeGenModule &CGM;
59
60 ConstantAggregateBuilderUtils(CodeGenModule &CGM) : CGM(CGM) {}
61
62 CharUnits getAlignment(const llvm::Constant *C) const {
64 CGM.getDataLayout().getABITypeAlign(C->getType()));
65 }
66
67 CharUnits getSize(llvm::Type *Ty) const {
68 return CharUnits::fromQuantity(CGM.getDataLayout().getTypeAllocSize(Ty));
69 }
70
71 CharUnits getSize(const llvm::Constant *C) const {
72 return getSize(C->getType());
73 }
74
75 llvm::Constant *getPadding(CharUnits PadSize) const {
76 return ::getPadding(CGM, PadSize);
77 }
78
79 llvm::Constant *getZeroes(CharUnits ZeroSize) const {
80 llvm::Type *Ty = llvm::ArrayType::get(CGM.CharTy, ZeroSize.getQuantity());
81 return llvm::ConstantAggregateZero::get(Ty);
82 }
83};
84
85/// Incremental builder for an llvm::Constant* holding a struct or array
86/// constant.
87class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils {
88 /// The elements of the constant. These two arrays must have the same size;
89 /// Offsets[i] describes the offset of Elems[i] within the constant. The
90 /// elements are kept in increasing offset order, and we ensure that there
91 /// is no overlap: Offsets[i+1] >= Offsets[i] + getSize(Elemes[i]).
92 ///
93 /// This may contain explicit padding elements (in order to create a
94 /// natural layout), but need not. Gaps between elements are implicitly
95 /// considered to be filled with undef.
96 llvm::SmallVector<llvm::Constant*, 32> Elems;
97 llvm::SmallVector<CharUnits, 32> Offsets;
98
99 /// The size of the constant (the maximum end offset of any added element).
100 /// May be larger than the end of Elems.back() if we split the last element
101 /// and removed some trailing undefs.
102 CharUnits Size = CharUnits::Zero();
103
104 /// This is true only if laying out Elems in order as the elements of a
105 /// non-packed LLVM struct will give the correct layout.
106 bool NaturalLayout = true;
107
108 bool split(size_t Index, CharUnits Hint);
109 std::optional<size_t> splitAt(CharUnits Pos);
110
111 static llvm::Constant *buildFrom(CodeGenModule &CGM,
112 ArrayRef<llvm::Constant *> Elems,
113 ArrayRef<CharUnits> Offsets,
114 CharUnits StartOffset, CharUnits Size,
115 bool NaturalLayout, llvm::Type *DesiredTy,
116 bool AllowOversized);
117
118public:
119 ConstantAggregateBuilder(CodeGenModule &CGM)
120 : ConstantAggregateBuilderUtils(CGM) {}
121
122 /// Update or overwrite the value starting at \p Offset with \c C.
123 ///
124 /// \param AllowOverwrite If \c true, this constant might overwrite (part of)
125 /// a constant that has already been added. This flag is only used to
126 /// detect bugs.
127 bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite);
128
129 /// Update or overwrite the bits starting at \p OffsetInBits with \p Bits.
130 bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite);
131
132 /// Attempt to condense the value starting at \p Offset to a constant of type
133 /// \p DesiredTy.
134 void condense(CharUnits Offset, llvm::Type *DesiredTy);
135
136 /// Produce a constant representing the entire accumulated value, ideally of
137 /// the specified type. If \p AllowOversized, the constant might be larger
138 /// than implied by \p DesiredTy (eg, if there is a flexible array member).
139 /// Otherwise, the constant will be of exactly the same size as \p DesiredTy
140 /// even if we can't represent it as that type.
141 llvm::Constant *build(llvm::Type *DesiredTy, bool AllowOversized) const {
142 return buildFrom(CGM, Elems, Offsets, CharUnits::Zero(), Size,
143 NaturalLayout, DesiredTy, AllowOversized);
144 }
145};
146
147template<typename Container, typename Range = std::initializer_list<
148 typename Container::value_type>>
149static void replace(Container &C, size_t BeginOff, size_t EndOff, Range Vals) {
150 assert(BeginOff <= EndOff && "invalid replacement range");
151 llvm::replace(C, C.begin() + BeginOff, C.begin() + EndOff, Vals);
152}
153
154bool ConstantAggregateBuilder::add(llvm::Constant *C, CharUnits Offset,
155 bool AllowOverwrite) {
156 // Common case: appending to a layout.
157 if (Offset >= Size) {
158 CharUnits Align = getAlignment(C);
159 CharUnits AlignedSize = Size.alignTo(Align);
160 if (AlignedSize > Offset || Offset.alignTo(Align) != Offset)
161 NaturalLayout = false;
162 else if (AlignedSize < Offset) {
163 Elems.push_back(getPadding(Offset - Size));
164 Offsets.push_back(Size);
165 }
166 Elems.push_back(C);
167 Offsets.push_back(Offset);
168 Size = Offset + getSize(C);
169 return true;
170 }
171
172 // Uncommon case: constant overlaps what we've already created.
173 std::optional<size_t> FirstElemToReplace = splitAt(Offset);
174 if (!FirstElemToReplace)
175 return false;
176
177 CharUnits CSize = getSize(C);
178 std::optional<size_t> LastElemToReplace = splitAt(Offset + CSize);
179 if (!LastElemToReplace)
180 return false;
181
182 assert((FirstElemToReplace == LastElemToReplace || AllowOverwrite) &&
183 "unexpectedly overwriting field");
184
185 replace(Elems, *FirstElemToReplace, *LastElemToReplace, {C});
186 replace(Offsets, *FirstElemToReplace, *LastElemToReplace, {Offset});
187 Size = std::max(Size, Offset + CSize);
188 NaturalLayout = false;
189 return true;
190}
191
192bool ConstantAggregateBuilder::addBits(llvm::APInt Bits, uint64_t OffsetInBits,
193 bool AllowOverwrite) {
194 const ASTContext &Context = CGM.getContext();
195 const uint64_t CharWidth = CGM.getContext().getCharWidth();
196
197 // Offset of where we want the first bit to go within the bits of the
198 // current char.
199 unsigned OffsetWithinChar = OffsetInBits % CharWidth;
200
201 // We split bit-fields up into individual bytes. Walk over the bytes and
202 // update them.
203 for (CharUnits OffsetInChars =
204 Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar);
205 /**/; ++OffsetInChars) {
206 // Number of bits we want to fill in this char.
207 unsigned WantedBits =
208 std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar);
209
210 // Get a char containing the bits we want in the right places. The other
211 // bits have unspecified values.
212 llvm::APInt BitsThisChar = Bits;
213 if (BitsThisChar.getBitWidth() < CharWidth)
214 BitsThisChar = BitsThisChar.zext(CharWidth);
215 if (CGM.getDataLayout().isBigEndian()) {
216 // Figure out how much to shift by. We may need to left-shift if we have
217 // less than one byte of Bits left.
218 int Shift = Bits.getBitWidth() - CharWidth + OffsetWithinChar;
219 if (Shift > 0)
220 BitsThisChar.lshrInPlace(Shift);
221 else if (Shift < 0)
222 BitsThisChar = BitsThisChar.shl(-Shift);
223 } else {
224 BitsThisChar = BitsThisChar.shl(OffsetWithinChar);
225 }
226 if (BitsThisChar.getBitWidth() > CharWidth)
227 BitsThisChar = BitsThisChar.trunc(CharWidth);
228
229 if (WantedBits == CharWidth) {
230 // Got a full byte: just add it directly.
231 add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
232 OffsetInChars, AllowOverwrite);
233 } else {
234 // Partial byte: update the existing integer if there is one. If we
235 // can't split out a 1-CharUnit range to update, then we can't add
236 // these bits and fail the entire constant emission.
237 std::optional<size_t> FirstElemToUpdate = splitAt(OffsetInChars);
238 if (!FirstElemToUpdate)
239 return false;
240 std::optional<size_t> LastElemToUpdate =
241 splitAt(OffsetInChars + CharUnits::One());
242 if (!LastElemToUpdate)
243 return false;
244 assert(*LastElemToUpdate - *FirstElemToUpdate < 2 &&
245 "should have at most one element covering one byte");
246
247 // Figure out which bits we want and discard the rest.
248 llvm::APInt UpdateMask(CharWidth, 0);
249 if (CGM.getDataLayout().isBigEndian())
250 UpdateMask.setBits(CharWidth - OffsetWithinChar - WantedBits,
251 CharWidth - OffsetWithinChar);
252 else
253 UpdateMask.setBits(OffsetWithinChar, OffsetWithinChar + WantedBits);
254 BitsThisChar &= UpdateMask;
255
256 if (*FirstElemToUpdate == *LastElemToUpdate ||
257 Elems[*FirstElemToUpdate]->isNullValue() ||
258 isa<llvm::UndefValue>(Elems[*FirstElemToUpdate])) {
259 // All existing bits are either zero or undef.
260 add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar),
261 OffsetInChars, /*AllowOverwrite*/ true);
262 } else {
263 llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate];
264 // In order to perform a partial update, we need the existing bitwise
265 // value, which we can only extract for a constant int.
266 auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate);
267 if (!CI)
268 return false;
269 // Because this is a 1-CharUnit range, the constant occupying it must
270 // be exactly one CharUnit wide.
271 assert(CI->getBitWidth() == CharWidth && "splitAt failed");
272 assert((!(CI->getValue() & UpdateMask) || AllowOverwrite) &&
273 "unexpectedly overwriting bitfield");
274 BitsThisChar |= (CI->getValue() & ~UpdateMask);
275 ToUpdate = llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar);
276 }
277 }
278
279 // Stop if we've added all the bits.
280 if (WantedBits == Bits.getBitWidth())
281 break;
282
283 // Remove the consumed bits from Bits.
284 if (!CGM.getDataLayout().isBigEndian())
285 Bits.lshrInPlace(WantedBits);
286 Bits = Bits.trunc(Bits.getBitWidth() - WantedBits);
287
288 // The remanining bits go at the start of the following bytes.
289 OffsetWithinChar = 0;
290 }
291
292 return true;
293}
294
295/// Returns a position within Elems and Offsets such that all elements
296/// before the returned index end before Pos and all elements at or after
297/// the returned index begin at or after Pos. Splits elements as necessary
298/// to ensure this. Returns std::nullopt if we find something we can't split.
299std::optional<size_t> ConstantAggregateBuilder::splitAt(CharUnits Pos) {
300 if (Pos >= Size)
301 return Offsets.size();
302
303 while (true) {
304 auto FirstAfterPos = llvm::upper_bound(Offsets, Pos);
305 if (FirstAfterPos == Offsets.begin())
306 return 0;
307
308 // If we already have an element starting at Pos, we're done.
309 size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1;
310 if (Offsets[LastAtOrBeforePosIndex] == Pos)
311 return LastAtOrBeforePosIndex;
312
313 // We found an element starting before Pos. Check for overlap.
314 if (Offsets[LastAtOrBeforePosIndex] +
315 getSize(Elems[LastAtOrBeforePosIndex]) <= Pos)
316 return LastAtOrBeforePosIndex + 1;
317
318 // Try to decompose it into smaller constants.
319 if (!split(LastAtOrBeforePosIndex, Pos))
320 return std::nullopt;
321 }
322}
323
324/// Split the constant at index Index, if possible. Return true if we did.
325/// Hint indicates the location at which we'd like to split, but may be
326/// ignored.
327bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
328 NaturalLayout = false;
329 llvm::Constant *C = Elems[Index];
330 CharUnits Offset = Offsets[Index];
331
332 if (auto *CA = dyn_cast<llvm::ConstantAggregate>(C)) {
333 // Expand the sequence into its contained elements.
334 // FIXME: This assumes vector elements are byte-sized.
335 replace(Elems, Index, Index + 1,
336 llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
337 [&](unsigned Op) { return CA->getOperand(Op); }));
338 if (isa<llvm::ArrayType>(CA->getType()) ||
339 isa<llvm::VectorType>(CA->getType())) {
340 // Array or vector.
341 llvm::Type *ElemTy =
342 llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0);
343 CharUnits ElemSize = getSize(ElemTy);
344 replace(
345 Offsets, Index, Index + 1,
346 llvm::map_range(llvm::seq(0u, CA->getNumOperands()),
347 [&](unsigned Op) { return Offset + Op * ElemSize; }));
348 } else {
349 // Must be a struct.
350 auto *ST = cast<llvm::StructType>(CA->getType());
351 const llvm::StructLayout *Layout =
352 CGM.getDataLayout().getStructLayout(ST);
353 replace(Offsets, Index, Index + 1,
354 llvm::map_range(
355 llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) {
356 return Offset + CharUnits::fromQuantity(
357 Layout->getElementOffset(Op));
358 }));
359 }
360 return true;
361 }
362
363 if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) {
364 // Expand the sequence into its contained elements.
365 // FIXME: This assumes vector elements are byte-sized.
366 // FIXME: If possible, split into two ConstantDataSequentials at Hint.
367 CharUnits ElemSize = getSize(CDS->getElementType());
368 replace(Elems, Index, Index + 1,
369 llvm::map_range(llvm::seq(uint64_t(0u), CDS->getNumElements()),
370 [&](uint64_t Elem) {
371 return CDS->getElementAsConstant(Elem);
372 }));
373 replace(Offsets, Index, Index + 1,
374 llvm::map_range(
375 llvm::seq(uint64_t(0u), CDS->getNumElements()),
376 [&](uint64_t Elem) { return Offset + Elem * ElemSize; }));
377 return true;
378 }
379
381 // Split into two zeros at the hinted offset.
382 CharUnits ElemSize = getSize(C);
383 assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split");
384 replace(Elems, Index, Index + 1,
385 {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)});
386 replace(Offsets, Index, Index + 1, {Offset, Hint});
387 return true;
388 }
389
391 // Drop undef; it doesn't contribute to the final layout.
392 replace(Elems, Index, Index + 1, {});
393 replace(Offsets, Index, Index + 1, {});
394 return true;
395 }
396
397 // FIXME: We could split a ConstantInt if the need ever arose.
398 // We don't need to do this to handle bit-fields because we always eagerly
399 // split them into 1-byte chunks.
400
401 return false;
402}
403
404static llvm::Constant *
405EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
406 llvm::Type *CommonElementType, uint64_t ArrayBound,
407 SmallVectorImpl<llvm::Constant *> &Elements,
408 llvm::Constant *Filler);
409
410llvm::Constant *ConstantAggregateBuilder::buildFrom(
411 CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems,
412 ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size,
413 bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) {
414 ConstantAggregateBuilderUtils Utils(CGM);
415
416 if (Elems.empty())
417 return llvm::UndefValue::get(DesiredTy);
418
419 auto Offset = [&](size_t I) { return Offsets[I] - StartOffset; };
420
421 // If we want an array type, see if all the elements are the same type and
422 // appropriately spaced.
423 if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) {
424 assert(!AllowOversized && "oversized array emission not supported");
425
426 bool CanEmitArray = true;
427 llvm::Type *CommonType = Elems[0]->getType();
428 llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType);
429 CharUnits ElemSize = Utils.getSize(ATy->getElementType());
430 SmallVector<llvm::Constant*, 32> ArrayElements;
431 for (size_t I = 0; I != Elems.size(); ++I) {
432 // Skip zeroes; we'll use a zero value as our array filler.
433 if (Elems[I]->isNullValue())
434 continue;
435
436 // All remaining elements must be the same type.
437 if (Elems[I]->getType() != CommonType ||
438 !Offset(I).isMultipleOf(ElemSize)) {
439 CanEmitArray = false;
440 break;
441 }
442 ArrayElements.resize(Offset(I) / ElemSize + 1, Filler);
443 ArrayElements.back() = Elems[I];
444 }
445
446 if (CanEmitArray) {
447 return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(),
448 ArrayElements, Filler);
449 }
450
451 // Can't emit as an array, carry on to emit as a struct.
452 }
453
454 // The size of the constant we plan to generate. This is usually just
455 // the size of the initialized type, but in AllowOversized mode (i.e.
456 // flexible array init), it can be larger.
457 CharUnits DesiredSize = Utils.getSize(DesiredTy);
458 if (Size > DesiredSize) {
459 assert(AllowOversized && "Elems are oversized");
460 DesiredSize = Size;
461 }
462
463 // The natural alignment of an unpacked LLVM struct with the given elements.
464 CharUnits Align = CharUnits::One();
465 for (llvm::Constant *C : Elems)
466 Align = std::max(Align, Utils.getAlignment(C));
467
468 // The natural size of an unpacked LLVM struct with the given elements.
469 CharUnits AlignedSize = Size.alignTo(Align);
470
471 bool Packed = false;
472 ArrayRef<llvm::Constant*> UnpackedElems = Elems;
473 llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage;
474 if (DesiredSize < AlignedSize || DesiredSize.alignTo(Align) != DesiredSize) {
475 // The natural layout would be too big; force use of a packed layout.
476 NaturalLayout = false;
477 Packed = true;
478 } else if (DesiredSize > AlignedSize) {
479 // The natural layout would be too small. Add padding to fix it. (This
480 // is ignored if we choose a packed layout.)
481 UnpackedElemStorage.assign(Elems.begin(), Elems.end());
482 UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size));
483 UnpackedElems = UnpackedElemStorage;
484 }
485
486 // If we don't have a natural layout, insert padding as necessary.
487 // As we go, double-check to see if we can actually just emit Elems
488 // as a non-packed struct and do so opportunistically if possible.
489 llvm::SmallVector<llvm::Constant*, 32> PackedElems;
490 if (!NaturalLayout) {
491 CharUnits SizeSoFar = CharUnits::Zero();
492 for (size_t I = 0; I != Elems.size(); ++I) {
493 CharUnits Align = Utils.getAlignment(Elems[I]);
494 CharUnits NaturalOffset = SizeSoFar.alignTo(Align);
495 CharUnits DesiredOffset = Offset(I);
496 assert(DesiredOffset >= SizeSoFar && "elements out of order");
497
498 if (DesiredOffset != NaturalOffset)
499 Packed = true;
500 if (DesiredOffset != SizeSoFar)
501 PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar));
502 PackedElems.push_back(Elems[I]);
503 SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]);
504 }
505 // If we're using the packed layout, pad it out to the desired size if
506 // necessary.
507 if (Packed) {
508 assert(SizeSoFar <= DesiredSize &&
509 "requested size is too small for contents");
510 if (SizeSoFar < DesiredSize)
511 PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar));
512 }
513 }
514
515 llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements(
516 CGM.getLLVMContext(), Packed ? PackedElems : UnpackedElems, Packed);
517
518 // Pick the type to use. If the type is layout identical to the desired
519 // type then use it, otherwise use whatever the builder produced for us.
520 if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) {
521 if (DesiredSTy->isLayoutIdentical(STy))
522 STy = DesiredSTy;
523 }
524
525 return llvm::ConstantStruct::get(STy, Packed ? PackedElems : UnpackedElems);
526}
527
528void ConstantAggregateBuilder::condense(CharUnits Offset,
529 llvm::Type *DesiredTy) {
530 CharUnits Size = getSize(DesiredTy);
531
532 std::optional<size_t> FirstElemToReplace = splitAt(Offset);
533 if (!FirstElemToReplace)
534 return;
535 size_t First = *FirstElemToReplace;
536
537 std::optional<size_t> LastElemToReplace = splitAt(Offset + Size);
538 if (!LastElemToReplace)
539 return;
540 size_t Last = *LastElemToReplace;
541
542 size_t Length = Last - First;
543 if (Length == 0)
544 return;
545
546 if (Length == 1 && Offsets[First] == Offset &&
547 getSize(Elems[First]) == Size) {
548 // Re-wrap single element structs if necessary. Otherwise, leave any single
549 // element constant of the right size alone even if it has the wrong type.
550 auto *STy = dyn_cast<llvm::StructType>(DesiredTy);
551 if (STy && STy->getNumElements() == 1 &&
552 STy->getElementType(0) == Elems[First]->getType())
553 Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]);
554 return;
555 }
556
557 llvm::Constant *Replacement = buildFrom(
558 CGM, ArrayRef(Elems).slice(First, Length),
559 ArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy),
560 /*known to have natural layout=*/false, DesiredTy, false);
561 replace(Elems, First, Last, {Replacement});
562 replace(Offsets, First, Last, {Offset});
563}
564
565//===----------------------------------------------------------------------===//
566// ConstStructBuilder
567//===----------------------------------------------------------------------===//
568
569class ConstStructBuilder {
570 CodeGenModule &CGM;
571 ConstantEmitter &Emitter;
572 ConstantAggregateBuilder &Builder;
573 CharUnits StartOffset;
574
575public:
576 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
577 const InitListExpr *ILE,
578 QualType StructTy);
579 static llvm::Constant *BuildStruct(ConstantEmitter &Emitter,
580 const APValue &Value, QualType ValTy);
581 static bool UpdateStruct(ConstantEmitter &Emitter,
582 ConstantAggregateBuilder &Const, CharUnits Offset,
583 const InitListExpr *Updater);
584
585private:
586 ConstStructBuilder(ConstantEmitter &Emitter,
587 ConstantAggregateBuilder &Builder, CharUnits StartOffset)
588 : CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder),
589 StartOffset(StartOffset) {}
590
591 bool AppendField(const FieldDecl *Field, uint64_t FieldOffset,
592 llvm::Constant *InitExpr, bool AllowOverwrite = false);
593
594 bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst,
595 bool AllowOverwrite = false);
596
597 bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset,
598 llvm::Constant *InitExpr, bool AllowOverwrite = false);
599
600 bool Build(const InitListExpr *ILE, bool AllowOverwrite);
601 bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase,
602 const CXXRecordDecl *VTableClass, CharUnits BaseOffset);
603 bool DoZeroInitPadding(const ASTRecordLayout &Layout, unsigned FieldNo,
604 const FieldDecl &Field, bool AllowOverwrite,
605 CharUnits &SizeSoFar, bool &ZeroFieldSize);
606 bool DoZeroInitPadding(const ASTRecordLayout &Layout, bool AllowOverwrite,
607 CharUnits SizeSoFar);
608 llvm::Constant *Finalize(QualType Ty);
609};
610
611bool ConstStructBuilder::AppendField(
612 const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst,
613 bool AllowOverwrite) {
614 const ASTContext &Context = CGM.getContext();
615
616 CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset);
617
618 return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite);
619}
620
621bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars,
622 llvm::Constant *InitCst,
623 bool AllowOverwrite) {
624 return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite);
625}
626
627bool ConstStructBuilder::AppendBitField(const FieldDecl *Field,
628 uint64_t FieldOffset, llvm::Constant *C,
629 bool AllowOverwrite) {
630
631 llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C);
632 if (!CI) {
633 // Constants for long _BitInt types are sometimes split into individual
634 // bytes. Try to fold these back into an integer constant. If that doesn't
635 // work out, then we are trying to initialize a bitfield with a non-trivial
636 // constant, this must require run-time code.
637 llvm::Type *LoadType =
638 CGM.getTypes().convertTypeForLoadStore(Field->getType(), C->getType());
639 llvm::Constant *FoldedConstant = llvm::ConstantFoldLoadFromConst(
640 C, LoadType, llvm::APInt::getZero(32), CGM.getDataLayout());
641 CI = dyn_cast_if_present<llvm::ConstantInt>(FoldedConstant);
642 if (!CI)
643 return false;
644 }
645
646 const CGRecordLayout &RL =
647 CGM.getTypes().getCGRecordLayout(Field->getParent());
648 const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field);
649 llvm::APInt FieldValue = CI->getValue();
650
651 // Promote the size of FieldValue if necessary
652 // FIXME: This should never occur, but currently it can because initializer
653 // constants are cast to bool, and because clang is not enforcing bitfield
654 // width limits.
655 if (Info.Size > FieldValue.getBitWidth())
656 FieldValue = FieldValue.zext(Info.Size);
657
658 // Truncate the size of FieldValue to the bit field size.
659 if (Info.Size < FieldValue.getBitWidth())
660 FieldValue = FieldValue.trunc(Info.Size);
661
662 return Builder.addBits(FieldValue,
663 CGM.getContext().toBits(StartOffset) + FieldOffset,
664 AllowOverwrite);
665}
666
667static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter,
668 ConstantAggregateBuilder &Const,
669 CharUnits Offset, QualType Type,
670 const InitListExpr *Updater) {
671 if (Type->isRecordType())
672 return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater);
673
674 auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type);
675 if (!CAT)
676 return false;
677 QualType ElemType = CAT->getElementType();
678 CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType);
679 llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType);
680
681 llvm::Constant *FillC = nullptr;
682 if (const Expr *Filler = Updater->getArrayFiller()) {
683 if (!isa<NoInitExpr>(Filler)) {
684 FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType);
685 if (!FillC)
686 return false;
687 }
688 }
689
690 unsigned NumElementsToUpdate =
691 FillC ? CAT->getZExtSize() : Updater->getNumInits();
692 for (unsigned I = 0; I != NumElementsToUpdate; ++I, Offset += ElemSize) {
693 const Expr *Init = nullptr;
694 if (I < Updater->getNumInits())
695 Init = Updater->getInit(I);
696
697 if (!Init && FillC) {
698 if (!Const.add(FillC, Offset, true))
699 return false;
700 } else if (!Init || isa<NoInitExpr>(Init)) {
701 continue;
702 } else if (const auto *ChildILE = dyn_cast<InitListExpr>(Init)) {
703 if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType,
704 ChildILE))
705 return false;
706 // Attempt to reduce the array element to a single constant if necessary.
707 Const.condense(Offset, ElemTy);
708 } else {
709 llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType);
710 if (!Const.add(Val, Offset, true))
711 return false;
712 }
713 }
714
715 return true;
716}
717
718bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
719 auto *RD = ILE->getType()->castAsRecordDecl();
720 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
721
722 unsigned FieldNo = -1;
723 unsigned ElementNo = 0;
724
725 // Bail out if we have base classes. We could support these, but they only
726 // arise in C++1z where we will have already constant folded most interesting
727 // cases. FIXME: There are still a few more cases we can handle this way.
728 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
729 if (CXXRD->getNumBases())
730 return false;
731
732 const bool ZeroInitPadding = CGM.shouldZeroInitPadding();
733 bool ZeroFieldSize = false;
734 CharUnits SizeSoFar = CharUnits::Zero();
735
736 for (FieldDecl *Field : RD->fields()) {
737 ++FieldNo;
738
739 // If this is a union, skip all the fields that aren't being initialized.
740 if (RD->isUnion() &&
742 continue;
743
744 // Don't emit anonymous bitfields.
745 if (Field->isUnnamedBitField())
746 continue;
747
748 // Get the initializer. A struct can include fields without initializers,
749 // we just use explicit null values for them.
750 const Expr *Init = nullptr;
751 if (ElementNo < ILE->getNumInits())
752 Init = ILE->getInit(ElementNo++);
753 if (isa_and_nonnull<NoInitExpr>(Init)) {
754 if (ZeroInitPadding &&
755 !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar,
756 ZeroFieldSize))
757 return false;
758 continue;
759 }
760
761 // Zero-sized fields are not emitted, but their initializers may still
762 // prevent emission of this struct as a constant.
763 if (isEmptyFieldForLayout(CGM.getContext(), Field)) {
764 if (Init && Init->HasSideEffects(CGM.getContext()))
765 return false;
766 continue;
767 }
768
769 if (ZeroInitPadding &&
770 !DoZeroInitPadding(Layout, FieldNo, *Field, AllowOverwrite, SizeSoFar,
771 ZeroFieldSize))
772 return false;
773
774 // When emitting a DesignatedInitUpdateExpr, a nested InitListExpr
775 // represents additional overwriting of our current constant value, and not
776 // a new constant to emit independently.
777 if (AllowOverwrite &&
778 (Field->getType()->isArrayType() || Field->getType()->isRecordType())) {
779 if (auto *SubILE = dyn_cast<InitListExpr>(Init)) {
780 CharUnits Offset = CGM.getContext().toCharUnitsFromBits(
781 Layout.getFieldOffset(FieldNo));
782 if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset,
783 Field->getType(), SubILE))
784 return false;
785 // If we split apart the field's value, try to collapse it down to a
786 // single value now.
787 Builder.condense(StartOffset + Offset,
788 CGM.getTypes().ConvertTypeForMem(Field->getType()));
789 continue;
790 }
791 }
792
793 llvm::Constant *EltInit =
794 Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType())
795 : Emitter.emitNullForMemory(Field->getType());
796 if (!EltInit)
797 return false;
798
799 if (ZeroInitPadding && ZeroFieldSize)
800 SizeSoFar += CharUnits::fromQuantity(
801 CGM.getDataLayout().getTypeAllocSize(EltInit->getType()));
802
803 if (!Field->isBitField()) {
804 // Handle non-bitfield members.
805 if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit,
806 AllowOverwrite))
807 return false;
808 // After emitting a non-empty field with [[no_unique_address]], we may
809 // need to overwrite its tail padding.
810 if (Field->hasAttr<NoUniqueAddressAttr>())
811 AllowOverwrite = true;
812 } else {
813 // Otherwise we have a bitfield.
814 if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), EltInit,
815 AllowOverwrite))
816 return false;
817 }
818 }
819
820 if (ZeroInitPadding && !DoZeroInitPadding(Layout, AllowOverwrite, SizeSoFar))
821 return false;
822
823 return true;
824}
825
826namespace {
827struct BaseInfo {
828 BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index)
829 : Decl(Decl), Offset(Offset), Index(Index) {
830 }
831
832 const CXXRecordDecl *Decl;
833 CharUnits Offset;
834 unsigned Index;
835
836 bool operator<(const BaseInfo &O) const { return Offset < O.Offset; }
837};
838}
839
840bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
841 bool IsPrimaryBase,
842 const CXXRecordDecl *VTableClass,
843 CharUnits Offset) {
844 const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD);
845
846 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) {
847 // Add a vtable pointer, if we need one and it hasn't already been added.
848 if (Layout.hasOwnVFPtr()) {
849 llvm::Constant *VTableAddressPoint =
850 CGM.getCXXABI().getVTableAddressPoint(BaseSubobject(CD, Offset),
851 VTableClass);
852 if (auto Authentication = CGM.getVTablePointerAuthentication(CD)) {
853 VTableAddressPoint = Emitter.tryEmitConstantSignedPointer(
854 VTableAddressPoint, *Authentication);
855 if (!VTableAddressPoint)
856 return false;
857 }
858 if (!AppendBytes(Offset, VTableAddressPoint))
859 return false;
860 }
861
862 // Accumulate and sort bases, in order to visit them in address order, which
863 // may not be the same as declaration order.
864 SmallVector<BaseInfo, 8> Bases;
865 Bases.reserve(CD->getNumBases());
866 unsigned BaseNo = 0;
867 for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(),
868 BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) {
869 assert(!Base->isVirtual() && "should not have virtual bases here");
870 const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl();
871 CharUnits BaseOffset = Layout.getBaseClassOffset(BD);
872 Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo));
873 }
874 llvm::stable_sort(Bases);
875
876 for (const BaseInfo &Base : Bases) {
877 bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl;
878 if (!Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase,
879 VTableClass, Offset + Base.Offset))
880 return false;
881 }
882 }
883
884 unsigned FieldNo = 0;
885 uint64_t OffsetBits = CGM.getContext().toBits(Offset);
886 const bool ZeroInitPadding = CGM.shouldZeroInitPadding();
887 bool ZeroFieldSize = false;
888 CharUnits SizeSoFar = CharUnits::Zero();
889
890 bool AllowOverwrite = false;
891 for (RecordDecl::field_iterator Field = RD->field_begin(),
892 FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) {
893 // If this is a union, skip all the fields that aren't being initialized.
894 if (RD->isUnion() && !declaresSameEntity(Val.getUnionField(), *Field))
895 continue;
896
897 // Don't emit anonymous bitfields or zero-sized fields.
898 if (Field->isUnnamedBitField() ||
899 isEmptyFieldForLayout(CGM.getContext(), *Field))
900 continue;
901
902 // Emit the value of the initializer.
903 const APValue &FieldValue =
904 RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo);
905 llvm::Constant *EltInit =
906 Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType());
907 if (!EltInit)
908 return false;
909
910 if (CGM.getContext().isPFPField(*Field)) {
911 llvm::ConstantInt *Disc;
912 llvm::Constant *AddrDisc;
914 uint64_t FieldSignature =
915 llvm::getPointerAuthStableSipHash(CGM.getPFPFieldName(*Field));
916 Disc = llvm::ConstantInt::get(CGM.Int64Ty, FieldSignature);
917 AddrDisc = llvm::ConstantPointerNull::get(CGM.VoidPtrTy);
918 } else if (Emitter.isAbstract()) {
919 // isAbstract means that we don't know the global's address. Since we
920 // can only form a pointer without knowing the address if the fields are
921 // trivially copyable, we need to return false otherwise.
922 return false;
923 } else {
924 Disc = llvm::ConstantInt::get(CGM.Int64Ty,
925 -(Layout.getFieldOffset(FieldNo) / 8));
926 AddrDisc = Emitter.getCurrentAddrPrivate();
927 }
928 EltInit = llvm::ConstantPtrAuth::get(
929 EltInit, llvm::ConstantInt::get(CGM.Int32Ty, 2), Disc, AddrDisc,
930 CGM.getPFPDeactivationSymbol(*Field));
932 Emitter.registerCurrentAddrPrivate(EltInit,
933 cast<llvm::GlobalValue>(AddrDisc));
934 }
935
936 if (ZeroInitPadding) {
937 if (!DoZeroInitPadding(Layout, FieldNo, **Field, AllowOverwrite,
938 SizeSoFar, ZeroFieldSize))
939 return false;
940 if (ZeroFieldSize)
941 SizeSoFar += CharUnits::fromQuantity(
942 CGM.getDataLayout().getTypeAllocSize(EltInit->getType()));
943 }
944
945 if (!Field->isBitField()) {
946 // Handle non-bitfield members.
947 if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
948 EltInit, AllowOverwrite))
949 return false;
950 // After emitting a non-empty field with [[no_unique_address]], we may
951 // need to overwrite its tail padding.
952 if (Field->hasAttr<NoUniqueAddressAttr>())
953 AllowOverwrite = true;
954 } else {
955 // Otherwise we have a bitfield.
956 if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits,
957 EltInit, AllowOverwrite))
958 return false;
959 }
960 }
961 if (ZeroInitPadding && !DoZeroInitPadding(Layout, AllowOverwrite, SizeSoFar))
962 return false;
963
964 return true;
965}
966
967bool ConstStructBuilder::DoZeroInitPadding(
968 const ASTRecordLayout &Layout, unsigned FieldNo, const FieldDecl &Field,
969 bool AllowOverwrite, CharUnits &SizeSoFar, bool &ZeroFieldSize) {
970 uint64_t StartBitOffset = Layout.getFieldOffset(FieldNo);
971 CharUnits StartOffset = CGM.getContext().toCharUnitsFromBits(StartBitOffset);
972 if (SizeSoFar < StartOffset)
973 if (!AppendBytes(SizeSoFar, getPadding(CGM, StartOffset - SizeSoFar),
974 AllowOverwrite))
975 return false;
976
977 if (!Field.isBitField()) {
978 CharUnits FieldSize = CGM.getContext().getTypeSizeInChars(Field.getType());
979 SizeSoFar = StartOffset + FieldSize;
980 ZeroFieldSize = FieldSize.isZero();
981 } else {
982 const CGRecordLayout &RL =
983 CGM.getTypes().getCGRecordLayout(Field.getParent());
984 const CGBitFieldInfo &Info = RL.getBitFieldInfo(&Field);
985 uint64_t EndBitOffset = StartBitOffset + Info.Size;
986 SizeSoFar = CGM.getContext().toCharUnitsFromBits(EndBitOffset);
987 if (EndBitOffset % CGM.getContext().getCharWidth() != 0) {
988 SizeSoFar++;
989 }
990 ZeroFieldSize = Info.Size == 0;
991 }
992 return true;
993}
994
995bool ConstStructBuilder::DoZeroInitPadding(const ASTRecordLayout &Layout,
996 bool AllowOverwrite,
997 CharUnits SizeSoFar) {
998 CharUnits TotalSize = Layout.getSize();
999 if (SizeSoFar < TotalSize)
1000 if (!AppendBytes(SizeSoFar, getPadding(CGM, TotalSize - SizeSoFar),
1001 AllowOverwrite))
1002 return false;
1003 SizeSoFar = TotalSize;
1004 return true;
1005}
1006
1007llvm::Constant *ConstStructBuilder::Finalize(QualType Type) {
1008 Type = Type.getNonReferenceType();
1009 auto *RD = Type->castAsRecordDecl();
1010 llvm::Type *ValTy = CGM.getTypes().ConvertType(Type);
1011 return Builder.build(ValTy, RD->hasFlexibleArrayMember());
1012}
1013
1014llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
1015 const InitListExpr *ILE,
1016 QualType ValTy) {
1017 ConstantAggregateBuilder Const(Emitter.CGM);
1018 ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
1019
1020 if (!Builder.Build(ILE, /*AllowOverwrite*/false))
1021 return nullptr;
1022
1023 return Builder.Finalize(ValTy);
1024}
1025
1026llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter,
1027 const APValue &Val,
1028 QualType ValTy) {
1029 ConstantAggregateBuilder Const(Emitter.CGM);
1030 ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero());
1031
1032 const auto *RD = ValTy->castAsRecordDecl();
1033 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD);
1034 if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero()))
1035 return nullptr;
1036
1037 return Builder.Finalize(ValTy);
1038}
1039
1040bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter,
1041 ConstantAggregateBuilder &Const,
1042 CharUnits Offset,
1043 const InitListExpr *Updater) {
1044 return ConstStructBuilder(Emitter, Const, Offset)
1045 .Build(Updater, /*AllowOverwrite*/ true);
1046}
1047
1048//===----------------------------------------------------------------------===//
1049// ConstExprEmitter
1050//===----------------------------------------------------------------------===//
1051
1052static ConstantAddress
1053tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
1054 const CompoundLiteralExpr *E) {
1055 CodeGenModule &CGM = emitter.CGM;
1056 CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType());
1057 if (llvm::GlobalVariable *Addr =
1059 return ConstantAddress(Addr, Addr->getValueType(), Align);
1060
1061 LangAS addressSpace = E->getType().getAddressSpace();
1062 llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(),
1063 addressSpace, E->getType());
1064 if (!C) {
1065 assert(!E->isFileScope() &&
1066 "file-scope compound literal did not have constant initializer!");
1067 return ConstantAddress::invalid();
1068 }
1069
1070 auto GV = new llvm::GlobalVariable(
1071 CGM.getModule(), C->getType(),
1072 E->getType().isConstantStorage(CGM.getContext(), true, false),
1073 llvm::GlobalValue::InternalLinkage, C, ".compoundliteral", nullptr,
1074 llvm::GlobalVariable::NotThreadLocal,
1075 CGM.getContext().getTargetAddressSpace(addressSpace));
1076 emitter.finalize(GV);
1077 GV->setAlignment(Align.getAsAlign());
1079 return ConstantAddress(GV, GV->getValueType(), Align);
1080}
1081
1082static llvm::Constant *
1083EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
1084 llvm::Type *CommonElementType, uint64_t ArrayBound,
1085 SmallVectorImpl<llvm::Constant *> &Elements,
1086 llvm::Constant *Filler) {
1087 // Figure out how long the initial prefix of non-zero elements is.
1088 uint64_t NonzeroLength = ArrayBound;
1089 if (Elements.size() < NonzeroLength && Filler->isNullValue())
1090 NonzeroLength = Elements.size();
1091 if (NonzeroLength == Elements.size()) {
1092 while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue())
1093 --NonzeroLength;
1094 }
1095
1096 if (NonzeroLength == 0)
1097 return llvm::ConstantAggregateZero::get(DesiredType);
1098
1099 // Add a zeroinitializer array filler if we have lots of trailing zeroes.
1100 uint64_t TrailingZeroes = ArrayBound - NonzeroLength;
1101 if (TrailingZeroes >= 8) {
1102 assert(Elements.size() >= NonzeroLength &&
1103 "missing initializer for non-zero element");
1104
1105 // If all the elements had the same type up to the trailing zeroes, emit a
1106 // struct of two arrays (the nonzero data and the zeroinitializer).
1107 if (CommonElementType && NonzeroLength >= 8) {
1108 llvm::Constant *Initial = llvm::ConstantArray::get(
1109 llvm::ArrayType::get(CommonElementType, NonzeroLength),
1110 ArrayRef(Elements).take_front(NonzeroLength));
1111 Elements.resize(2);
1112 Elements[0] = Initial;
1113 } else {
1114 Elements.resize(NonzeroLength + 1);
1115 }
1116
1117 auto *FillerType =
1118 CommonElementType ? CommonElementType : DesiredType->getElementType();
1119 FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
1120 Elements.back() = llvm::ConstantAggregateZero::get(FillerType);
1121 CommonElementType = nullptr;
1122 } else if (Elements.size() != ArrayBound) {
1123 // Otherwise pad to the right size with the filler if necessary.
1124 Elements.resize(ArrayBound, Filler);
1125 if (Filler->getType() != CommonElementType)
1126 CommonElementType = nullptr;
1127 }
1128
1129 // If all elements have the same type, just emit an array constant.
1130 if (CommonElementType)
1131 return llvm::ConstantArray::get(
1132 llvm::ArrayType::get(CommonElementType, ArrayBound), Elements);
1133
1134 // We have mixed types. Use a packed struct.
1135 llvm::SmallVector<llvm::Type *, 16> Types;
1136 Types.reserve(Elements.size());
1137 for (llvm::Constant *Elt : Elements)
1138 Types.push_back(Elt->getType());
1139 llvm::StructType *SType =
1140 llvm::StructType::get(CGM.getLLVMContext(), Types, true);
1141 return llvm::ConstantStruct::get(SType, Elements);
1142}
1143
1144// This class only needs to handle arrays, structs and unions. Outside C++11
1145// mode, we don't currently constant fold those types. All other types are
1146// handled by constant folding.
1147//
1148// Constant folding is currently missing support for a few features supported
1149// here: CK_ToUnion, CK_ReinterpretMemberPointer, and DesignatedInitUpdateExpr.
1150class ConstExprEmitter
1151 : public ConstStmtVisitor<ConstExprEmitter, llvm::Constant *, QualType> {
1152 CodeGenModule &CGM;
1153 ConstantEmitter &Emitter;
1154 llvm::LLVMContext &VMContext;
1155public:
1156 ConstExprEmitter(ConstantEmitter &emitter)
1157 : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) {
1158 }
1159
1160 //===--------------------------------------------------------------------===//
1161 // Visitor Methods
1162 //===--------------------------------------------------------------------===//
1163
1164 llvm::Constant *VisitStmt(const Stmt *S, QualType T) { return nullptr; }
1165
1166 llvm::Constant *VisitConstantExpr(const ConstantExpr *CE, QualType T) {
1167 if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(CE))
1168 return Result;
1169 return Visit(CE->getSubExpr(), T);
1170 }
1171
1172 llvm::Constant *VisitParenExpr(const ParenExpr *PE, QualType T) {
1173 return Visit(PE->getSubExpr(), T);
1174 }
1175
1176 llvm::Constant *
1177 VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *PE,
1178 QualType T) {
1179 return Visit(PE->getReplacement(), T);
1180 }
1181
1182 llvm::Constant *VisitGenericSelectionExpr(const GenericSelectionExpr *GE,
1183 QualType T) {
1184 return Visit(GE->getResultExpr(), T);
1185 }
1186
1187 llvm::Constant *VisitChooseExpr(const ChooseExpr *CE, QualType T) {
1188 return Visit(CE->getChosenSubExpr(), T);
1189 }
1190
1191 llvm::Constant *VisitCompoundLiteralExpr(const CompoundLiteralExpr *E,
1192 QualType T) {
1193 return Visit(E->getInitializer(), T);
1194 }
1195
1196 llvm::Constant *ProduceIntToIntCast(const Expr *E, QualType DestType) {
1197 QualType FromType = E->getType();
1198 // See also HandleIntToIntCast in ExprConstant.cpp
1199 if (FromType->isIntegerType())
1200 if (llvm::Constant *C = Visit(E, FromType))
1201 if (auto *CI = dyn_cast<llvm::ConstantInt>(C)) {
1202 unsigned SrcWidth = CGM.getContext().getIntWidth(FromType);
1203 unsigned DstWidth = CGM.getContext().getIntWidth(DestType);
1204 if (DstWidth == SrcWidth)
1205 return CI;
1206 llvm::APInt A = FromType->isSignedIntegerType()
1207 ? CI->getValue().sextOrTrunc(DstWidth)
1208 : CI->getValue().zextOrTrunc(DstWidth);
1209 return llvm::ConstantInt::get(CGM.getLLVMContext(), A);
1210 }
1211 return nullptr;
1212 }
1213
1214 llvm::Constant *VisitCastExpr(const CastExpr *E, QualType destType) {
1215 if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E))
1216 CGM.EmitExplicitCastExprType(ECE, Emitter.CGF);
1217 const Expr *subExpr = E->getSubExpr();
1218
1219 switch (E->getCastKind()) {
1220 case CK_ToUnion: {
1221 // GCC cast to union extension
1222 assert(E->getType()->isUnionType() &&
1223 "Destination type is not union type!");
1224
1225 auto field = E->getTargetUnionField();
1226
1227 auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType());
1228 if (!C) return nullptr;
1229
1230 auto destTy = ConvertType(destType);
1231 if (C->getType() == destTy) return C;
1232
1233 // Build a struct with the union sub-element as the first member,
1234 // and padded to the appropriate size.
1235 SmallVector<llvm::Constant*, 2> Elts;
1236 SmallVector<llvm::Type*, 2> Types;
1237 Elts.push_back(C);
1238 Types.push_back(C->getType());
1239 unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType());
1240 unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy);
1241
1242 assert(CurSize <= TotalSize && "Union size mismatch!");
1243 if (unsigned NumPadBytes = TotalSize - CurSize) {
1244 llvm::Constant *Padding =
1245 getPadding(CGM, CharUnits::fromQuantity(NumPadBytes));
1246 Elts.push_back(Padding);
1247 Types.push_back(Padding->getType());
1248 }
1249
1250 llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false);
1251 return llvm::ConstantStruct::get(STy, Elts);
1252 }
1253
1254 case CK_AddressSpaceConversion: {
1255 llvm::Constant *C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1256 if (!C)
1257 return nullptr;
1258 llvm::Type *destTy = ConvertType(E->getType());
1259 return CGM.performAddrSpaceCast(C, destTy);
1260 }
1261
1262 case CK_LValueToRValue: {
1263 // We don't really support doing lvalue-to-rvalue conversions here; any
1264 // interesting conversions should be done in Evaluate(). But as a
1265 // special case, allow compound literals to support the gcc extension
1266 // allowing "struct x {int x;} x = (struct x) {};".
1267 if (const auto *E =
1268 dyn_cast<CompoundLiteralExpr>(subExpr->IgnoreParens()))
1269 return Visit(E->getInitializer(), destType);
1270 return nullptr;
1271 }
1272
1273 case CK_AtomicToNonAtomic:
1274 case CK_NonAtomicToAtomic:
1275 case CK_NoOp:
1276 case CK_ConstructorConversion:
1277 return Visit(subExpr, destType);
1278
1279 case CK_ArrayToPointerDecay:
1280 if (const auto *S = dyn_cast<StringLiteral>(subExpr))
1282 return nullptr;
1283 case CK_NullToPointer:
1284 if (Visit(subExpr, destType))
1285 return CGM.EmitNullConstant(destType);
1286 return nullptr;
1287
1288 case CK_IntToOCLSampler:
1289 llvm_unreachable("global sampler variables are not generated");
1290
1291 case CK_IntegralCast:
1292 return ProduceIntToIntCast(subExpr, destType);
1293
1294 case CK_Dependent: llvm_unreachable("saw dependent cast!");
1295
1296 case CK_BuiltinFnToFnPtr:
1297 llvm_unreachable("builtin functions are handled elsewhere");
1298
1299 case CK_ReinterpretMemberPointer:
1300 case CK_DerivedToBaseMemberPointer:
1301 case CK_BaseToDerivedMemberPointer: {
1302 auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType());
1303 if (!C) return nullptr;
1304 return CGM.getCXXABI().EmitMemberPointerConversion(E, C);
1305 }
1306
1307 // These will never be supported.
1308 case CK_ObjCObjectLValueCast:
1309 case CK_ARCProduceObject:
1310 case CK_ARCConsumeObject:
1311 case CK_ARCReclaimReturnedObject:
1312 case CK_ARCExtendBlockObject:
1313 case CK_CopyAndAutoreleaseBlockObject:
1314 return nullptr;
1315
1316 // These don't need to be handled here because Evaluate knows how to
1317 // evaluate them in the cases where they can be folded.
1318 case CK_BitCast:
1319 case CK_ToVoid:
1320 case CK_Dynamic:
1321 case CK_LValueBitCast:
1322 case CK_LValueToRValueBitCast:
1323 case CK_NullToMemberPointer:
1324 case CK_UserDefinedConversion:
1325 case CK_CPointerToObjCPointerCast:
1326 case CK_BlockPointerToObjCPointerCast:
1327 case CK_AnyPointerToBlockPointerCast:
1328 case CK_FunctionToPointerDecay:
1329 case CK_BaseToDerived:
1330 case CK_DerivedToBase:
1331 case CK_UncheckedDerivedToBase:
1332 case CK_MemberPointerToBoolean:
1333 case CK_VectorSplat:
1334 case CK_FloatingRealToComplex:
1335 case CK_FloatingComplexToReal:
1336 case CK_FloatingComplexToBoolean:
1337 case CK_FloatingComplexCast:
1338 case CK_FloatingComplexToIntegralComplex:
1339 case CK_IntegralRealToComplex:
1340 case CK_IntegralComplexToReal:
1341 case CK_IntegralComplexToBoolean:
1342 case CK_IntegralComplexCast:
1343 case CK_IntegralComplexToFloatingComplex:
1344 case CK_PointerToIntegral:
1345 case CK_PointerToBoolean:
1346 case CK_BooleanToSignedIntegral:
1347 case CK_IntegralToPointer:
1348 case CK_IntegralToBoolean:
1349 case CK_IntegralToFloating:
1350 case CK_FloatingToIntegral:
1351 case CK_FloatingToBoolean:
1352 case CK_FloatingCast:
1353 case CK_FloatingToFixedPoint:
1354 case CK_FixedPointToFloating:
1355 case CK_FixedPointCast:
1356 case CK_FixedPointToBoolean:
1357 case CK_FixedPointToIntegral:
1358 case CK_IntegralToFixedPoint:
1359 case CK_ZeroToOCLOpaqueType:
1360 case CK_MatrixCast:
1361 case CK_HLSLVectorTruncation:
1362 case CK_HLSLMatrixTruncation:
1363 case CK_HLSLArrayRValue:
1364 case CK_HLSLElementwiseCast:
1365 case CK_HLSLAggregateSplatCast:
1366 return nullptr;
1367 }
1368 llvm_unreachable("Invalid CastKind");
1369 }
1370
1371 llvm::Constant *VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *DIE,
1372 QualType T) {
1373 // No need for a DefaultInitExprScope: we don't handle 'this' in a
1374 // constant expression.
1375 return Visit(DIE->getExpr(), T);
1376 }
1377
1378 llvm::Constant *VisitExprWithCleanups(const ExprWithCleanups *E, QualType T) {
1379 return Visit(E->getSubExpr(), T);
1380 }
1381
1382 llvm::Constant *VisitIntegerLiteral(const IntegerLiteral *I, QualType T) {
1383 return llvm::ConstantInt::get(CGM.getLLVMContext(), I->getValue());
1384 }
1385
1386 static APValue withDestType(ASTContext &Ctx, const Expr *E, QualType SrcType,
1387 QualType DestType, const llvm::APSInt &Value) {
1388 if (!Ctx.hasSameType(SrcType, DestType)) {
1389 if (DestType->isFloatingType()) {
1390 llvm::APFloat Result =
1391 llvm::APFloat(Ctx.getFloatTypeSemantics(DestType), 1);
1392 llvm::RoundingMode RM =
1394 if (RM == llvm::RoundingMode::Dynamic)
1395 RM = llvm::RoundingMode::NearestTiesToEven;
1396 Result.convertFromAPInt(Value, Value.isSigned(), RM);
1397 return APValue(Result);
1398 }
1399 }
1400 return APValue(Value);
1401 }
1402
1403 llvm::Constant *EmitArrayInitialization(const InitListExpr *ILE, QualType T) {
1404 auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
1405 assert(CAT && "can't emit array init for non-constant-bound array");
1406 uint64_t NumInitElements = ILE->getNumInits();
1407 const uint64_t NumElements = CAT->getZExtSize();
1408 for (const auto *Init : ILE->inits()) {
1409 if (const auto *Embed =
1410 dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1411 NumInitElements += Embed->getDataElementCount() - 1;
1412 if (NumInitElements > NumElements) {
1413 NumInitElements = NumElements;
1414 break;
1415 }
1416 }
1417 }
1418
1419 // Initialising an array requires us to automatically
1420 // initialise any elements that have not been initialised explicitly
1421 uint64_t NumInitableElts = std::min<uint64_t>(NumInitElements, NumElements);
1422
1423 QualType EltType = CAT->getElementType();
1424
1425 // Initialize remaining array elements.
1426 llvm::Constant *fillC = nullptr;
1427 if (const Expr *filler = ILE->getArrayFiller()) {
1428 fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
1429 if (!fillC)
1430 return nullptr;
1431 }
1432
1433 // Copy initializer elements.
1434 SmallVector<llvm::Constant *, 16> Elts;
1435 if (fillC && fillC->isNullValue())
1436 Elts.reserve(NumInitableElts + 1);
1437 else
1438 Elts.reserve(NumElements);
1439
1440 llvm::Type *CommonElementType = nullptr;
1441 auto Emit = [&](const Expr *Init, unsigned ArrayIndex) {
1442 llvm::Constant *C = nullptr;
1443 C = Emitter.tryEmitPrivateForMemory(Init, EltType);
1444 if (!C)
1445 return false;
1446 if (ArrayIndex == 0)
1447 CommonElementType = C->getType();
1448 else if (C->getType() != CommonElementType)
1449 CommonElementType = nullptr;
1450 Elts.push_back(C);
1451 return true;
1452 };
1453
1454 unsigned ArrayIndex = 0;
1455 QualType DestTy = CAT->getElementType();
1456 for (unsigned i = 0; i < ILE->getNumInits(); ++i) {
1457 const Expr *Init = ILE->getInit(i);
1458 if (auto *EmbedS = dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1459 StringLiteral *SL = EmbedS->getDataStringLiteral();
1460 llvm::APSInt Value(CGM.getContext().getTypeSize(DestTy),
1461 DestTy->isUnsignedIntegerType());
1462 llvm::Constant *C;
1463 for (unsigned I = EmbedS->getStartingElementPos(),
1464 N = EmbedS->getDataElementCount();
1465 I != EmbedS->getStartingElementPos() + N; ++I) {
1466 Value = SL->getCodeUnit(I);
1467 if (DestTy->isIntegerType()) {
1468 C = llvm::ConstantInt::get(CGM.getLLVMContext(), Value);
1469 } else {
1470 C = Emitter.tryEmitPrivateForMemory(
1471 withDestType(CGM.getContext(), Init, EmbedS->getType(), DestTy,
1472 Value),
1473 EltType);
1474 }
1475 if (!C)
1476 return nullptr;
1477 Elts.push_back(C);
1478 ArrayIndex++;
1479 }
1480 if ((ArrayIndex - EmbedS->getDataElementCount()) == 0)
1481 CommonElementType = C->getType();
1482 else if (C->getType() != CommonElementType)
1483 CommonElementType = nullptr;
1484 } else {
1485 if (!Emit(Init, ArrayIndex))
1486 return nullptr;
1487 ArrayIndex++;
1488 }
1489 }
1490
1491 llvm::ArrayType *Desired =
1493 return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
1494 fillC);
1495 }
1496
1497 llvm::Constant *EmitRecordInitialization(const InitListExpr *ILE,
1498 QualType T) {
1499 return ConstStructBuilder::BuildStruct(Emitter, ILE, T);
1500 }
1501
1502 llvm::Constant *VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E,
1503 QualType T) {
1504 return CGM.EmitNullConstant(T);
1505 }
1506
1507 llvm::Constant *VisitInitListExpr(const InitListExpr *ILE, QualType T) {
1508 if (ILE->isTransparent())
1509 return Visit(ILE->getInit(0), T);
1510
1511 if (ILE->getType()->isArrayType())
1512 return EmitArrayInitialization(ILE, T);
1513
1514 if (ILE->getType()->isRecordType())
1515 return EmitRecordInitialization(ILE, T);
1516
1517 return nullptr;
1518 }
1519
1520 llvm::Constant *
1521 VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E,
1522 QualType destType) {
1523 auto C = Visit(E->getBase(), destType);
1524 if (!C)
1525 return nullptr;
1526
1527 ConstantAggregateBuilder Const(CGM);
1528 Const.add(C, CharUnits::Zero(), false);
1529
1530 if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType,
1531 E->getUpdater()))
1532 return nullptr;
1533
1534 llvm::Type *ValTy = CGM.getTypes().ConvertType(destType);
1535 bool HasFlexibleArray = false;
1536 if (const auto *RD = destType->getAsRecordDecl())
1537 HasFlexibleArray = RD->hasFlexibleArrayMember();
1538 return Const.build(ValTy, HasFlexibleArray);
1539 }
1540
1541 llvm::Constant *VisitCXXConstructExpr(const CXXConstructExpr *E,
1542 QualType Ty) {
1543 if (!E->getConstructor()->isTrivial())
1544 return nullptr;
1545
1546 // Only default and copy/move constructors can be trivial.
1547 if (E->getNumArgs()) {
1548 assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument");
1549 assert(E->getConstructor()->isCopyOrMoveConstructor() &&
1550 "trivial ctor has argument but isn't a copy/move ctor");
1551
1552 const Expr *Arg = E->getArg(0);
1553 assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) &&
1554 "argument to copy ctor is of wrong type");
1555
1556 // Look through the temporary; it's just converting the value to an
1557 // lvalue to pass it to the constructor.
1558 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(Arg))
1559 return Visit(MTE->getSubExpr(), Ty);
1560 // Don't try to support arbitrary lvalue-to-rvalue conversions for now.
1561 return nullptr;
1562 }
1563
1564 return CGM.EmitNullConstant(Ty);
1565 }
1566
1567 llvm::Constant *VisitStringLiteral(const StringLiteral *E, QualType T) {
1568 // This is a string literal initializing an array in an initializer.
1570 }
1571
1572 llvm::Constant *VisitObjCEncodeExpr(const ObjCEncodeExpr *E, QualType T) {
1573 // This must be an @encode initializing an array in a static initializer.
1574 // Don't emit it as the address of the string, emit the string data itself
1575 // as an inline array.
1576 std::string Str;
1578 const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T);
1579 assert(CAT && "String data not of constant array type!");
1580
1581 // Resize the string to the right size, adding zeros at the end, or
1582 // truncating as needed.
1583 Str.resize(CAT->getZExtSize(), '\0');
1584 return llvm::ConstantDataArray::getString(VMContext, Str, false);
1585 }
1586
1587 llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) {
1588 return Visit(E->getSubExpr(), T);
1589 }
1590
1591 llvm::Constant *VisitUnaryMinus(const UnaryOperator *U, QualType T) {
1592 if (llvm::Constant *C = Visit(U->getSubExpr(), T))
1593 if (auto *CI = dyn_cast<llvm::ConstantInt>(C))
1594 return llvm::ConstantInt::get(CGM.getLLVMContext(), -CI->getValue());
1595 return nullptr;
1596 }
1597
1598 llvm::Constant *VisitPackIndexingExpr(const PackIndexingExpr *E, QualType T) {
1599 return Visit(E->getSelectedExpr(), T);
1600 }
1601
1602 // Utility methods
1603 llvm::Type *ConvertType(QualType T) {
1604 return CGM.getTypes().ConvertType(T);
1605 }
1606};
1607
1608} // end anonymous namespace.
1609
1610llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C,
1611 AbstractState saved) {
1612 Abstract = saved.OldValue;
1613
1614 assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() &&
1615 "created a placeholder while doing an abstract emission?");
1616
1617 // No validation necessary for now.
1618 // No cleanup to do for now.
1619 return C;
1620}
1621
1622llvm::Constant *
1624 auto state = pushAbstract();
1625 auto C = tryEmitPrivateForVarInit(D);
1626 return validateAndPopAbstract(C, state);
1627}
1628
1629llvm::Constant *
1631 auto state = pushAbstract();
1632 auto C = tryEmitPrivate(E, destType);
1633 return validateAndPopAbstract(C, state);
1634}
1635
1636llvm::Constant *
1638 auto state = pushAbstract();
1639 auto C = tryEmitPrivate(value, destType);
1640 return validateAndPopAbstract(C, state);
1641}
1642
1644 if (!CE->hasAPValueResult())
1645 return nullptr;
1646
1647 QualType RetType = CE->getType();
1648 if (CE->isGLValue())
1649 RetType = CGM.getContext().getLValueReferenceType(RetType);
1650
1651 return tryEmitAbstract(CE->getAPValueResult(), RetType);
1652}
1653
1654llvm::Constant *
1656 auto state = pushAbstract();
1657 auto C = tryEmitPrivate(E, destType);
1658 C = validateAndPopAbstract(C, state);
1659 if (!C) {
1660 CGM.Error(E->getExprLoc(),
1661 "internal error: could not emit constant value \"abstractly\"");
1662 C = CGM.EmitNullConstant(destType);
1663 }
1664 return C;
1665}
1666
1667llvm::Constant *
1669 QualType destType,
1670 bool EnablePtrAuthFunctionTypeDiscrimination) {
1671 auto state = pushAbstract();
1672 auto C =
1673 tryEmitPrivate(value, destType, EnablePtrAuthFunctionTypeDiscrimination);
1674 C = validateAndPopAbstract(C, state);
1675 if (!C) {
1676 CGM.Error(loc,
1677 "internal error: could not emit constant value \"abstractly\"");
1678 C = CGM.EmitNullConstant(destType);
1679 }
1680 return C;
1681}
1682
1684 initializeNonAbstract(D.getType().getAddressSpace());
1685 llvm::Constant *Init = tryEmitPrivateForVarInit(D);
1686
1687 // If a placeholder address was needed for a TLS variable, implying that the
1688 // initializer's value depends on its address, then the object may not be
1689 // initialized in .tdata because the initializer will be memcpy'd to the
1690 // thread's TLS. Instead the initialization must be done in code.
1691 if (!PlaceholderAddresses.empty() && D.getTLSKind() != VarDecl::TLS_None) {
1692 for (auto [_, GV] : PlaceholderAddresses)
1693 GV->eraseFromParent();
1694 PlaceholderAddresses.clear();
1695 Init = nullptr;
1696 }
1697
1698 return markIfFailed(Init);
1699}
1700
1702 LangAS destAddrSpace,
1703 QualType destType) {
1704 initializeNonAbstract(destAddrSpace);
1705 return markIfFailed(tryEmitPrivateForMemory(E, destType));
1706}
1707
1709 LangAS destAddrSpace,
1710 QualType destType) {
1711 initializeNonAbstract(destAddrSpace);
1712 auto C = tryEmitPrivateForMemory(value, destType);
1713 assert(C && "couldn't emit constant value non-abstractly?");
1714 return C;
1715}
1716
1718 assert(!Abstract && "cannot get current address for abstract constant");
1719
1720
1721
1722 // Make an obviously ill-formed global that should blow up compilation
1723 // if it survives.
1724 auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true,
1725 llvm::GlobalValue::PrivateLinkage,
1726 /*init*/ nullptr,
1727 /*name*/ "",
1728 /*before*/ nullptr,
1729 llvm::GlobalVariable::NotThreadLocal,
1730 CGM.getContext().getTargetAddressSpace(DestAddressSpace));
1731
1732 PlaceholderAddresses.push_back(std::make_pair(nullptr, global));
1733
1734 return global;
1735}
1736
1738 llvm::GlobalValue *placeholder) {
1739 assert(!PlaceholderAddresses.empty());
1740 assert(PlaceholderAddresses.back().first == nullptr);
1741 assert(PlaceholderAddresses.back().second == placeholder);
1742 PlaceholderAddresses.back().first = signal;
1743}
1744
1745namespace {
1746 struct ReplacePlaceholders {
1747 CodeGenModule &CGM;
1748
1749 /// The base address of the global.
1750 llvm::Constant *Base;
1751 llvm::Type *BaseValueTy = nullptr;
1752
1753 /// The placeholder addresses that were registered during emission.
1754 llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses;
1755
1756 /// The locations of the placeholder signals.
1757 llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations;
1758
1759 /// The current index stack. We use a simple unsigned stack because
1760 /// we assume that placeholders will be relatively sparse in the
1761 /// initializer, but we cache the index values we find just in case.
1764
1765 ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base,
1766 ArrayRef<std::pair<llvm::Constant*,
1767 llvm::GlobalVariable*>> addresses)
1768 : CGM(CGM), Base(base),
1769 PlaceholderAddresses(addresses.begin(), addresses.end()) {
1770 }
1771
1772 void replaceInInitializer(llvm::Constant *init) {
1773 // Remember the type of the top-most initializer.
1774 BaseValueTy = init->getType();
1775
1776 // Initialize the stack.
1777 Indices.push_back(0);
1778 IndexValues.push_back(nullptr);
1779
1780 // Recurse into the initializer.
1781 findLocations(init);
1782
1783 // Check invariants.
1784 assert(IndexValues.size() == Indices.size() && "mismatch");
1785 assert(Indices.size() == 1 && "didn't pop all indices");
1786
1787 // Do the replacement; this basically invalidates 'init'.
1788 assert(Locations.size() == PlaceholderAddresses.size() &&
1789 "missed a placeholder?");
1790
1791 // We're iterating over a hashtable, so this would be a source of
1792 // non-determinism in compiler output *except* that we're just
1793 // messing around with llvm::Constant structures, which never itself
1794 // does anything that should be visible in compiler output.
1795 for (auto &entry : Locations) {
1796 assert(entry.first->getName() == "" && "not a placeholder!");
1797 entry.first->replaceAllUsesWith(entry.second);
1798 entry.first->eraseFromParent();
1799 }
1800 }
1801
1802 private:
1803 void findLocations(llvm::Constant *init) {
1804 // Recurse into aggregates.
1805 if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) {
1806 for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) {
1807 Indices.push_back(i);
1808 IndexValues.push_back(nullptr);
1809
1810 findLocations(agg->getOperand(i));
1811
1812 IndexValues.pop_back();
1813 Indices.pop_back();
1814 }
1815 return;
1816 }
1817
1818 // Otherwise, check for registered constants.
1819 while (true) {
1820 auto it = PlaceholderAddresses.find(init);
1821 if (it != PlaceholderAddresses.end()) {
1822 setLocation(it->second);
1823 break;
1824 }
1825
1826 // Look through bitcasts or other expressions.
1827 if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) {
1828 init = expr->getOperand(0);
1829 } else {
1830 break;
1831 }
1832 }
1833 }
1834
1835 void setLocation(llvm::GlobalVariable *placeholder) {
1836 assert(!Locations.contains(placeholder) &&
1837 "already found location for placeholder!");
1838
1839 // Lazily fill in IndexValues with the values from Indices.
1840 // We do this in reverse because we should always have a strict
1841 // prefix of indices from the start.
1842 assert(Indices.size() == IndexValues.size());
1843 for (size_t i = Indices.size() - 1; i != size_t(-1); --i) {
1844 if (IndexValues[i]) {
1845#ifndef NDEBUG
1846 for (size_t j = 0; j != i + 1; ++j) {
1847 assert(IndexValues[j] &&
1848 isa<llvm::ConstantInt>(IndexValues[j]) &&
1849 cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue()
1850 == Indices[j]);
1851 }
1852#endif
1853 break;
1854 }
1855
1856 IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]);
1857 }
1858
1859 llvm::Constant *location = llvm::ConstantExpr::getInBoundsGetElementPtr(
1860 BaseValueTy, Base, IndexValues);
1861
1862 Locations.insert({placeholder, location});
1863 }
1864 };
1865}
1866
1867void ConstantEmitter::finalize(llvm::GlobalVariable *global) {
1868 assert(InitializedNonAbstract &&
1869 "finalizing emitter that was used for abstract emission?");
1870 assert(!Finalized && "finalizing emitter multiple times");
1871 assert(global->getInitializer());
1872
1873 // Note that we might also be Failed.
1874 Finalized = true;
1875
1876 if (!PlaceholderAddresses.empty()) {
1877 ReplacePlaceholders(CGM, global, PlaceholderAddresses)
1878 .replaceInInitializer(global->getInitializer());
1879 PlaceholderAddresses.clear(); // satisfy
1880 }
1881}
1882
1884 assert((!InitializedNonAbstract || Finalized || Failed) &&
1885 "not finalized after being initialized for non-abstract emission");
1886 assert(PlaceholderAddresses.empty() && "unhandled placeholders");
1887}
1888
1890 if (auto AT = type->getAs<AtomicType>()) {
1891 return CGM.getContext().getQualifiedType(AT->getValueType(),
1892 type.getQualifiers());
1893 }
1894 return type;
1895}
1896
1898 // Make a quick check if variable can be default NULL initialized
1899 // and avoid going through rest of code which may do, for c++11,
1900 // initialization of memory to all NULLs.
1901 if (!D.hasLocalStorage()) {
1902 QualType Ty = CGM.getContext().getBaseElementType(D.getType());
1903 if (Ty->isRecordType())
1904 if (const CXXConstructExpr *E =
1905 dyn_cast_or_null<CXXConstructExpr>(D.getInit())) {
1906 const CXXConstructorDecl *CD = E->getConstructor();
1907 if (CD->isTrivial() && CD->isDefaultConstructor())
1908 return CGM.EmitNullConstant(D.getType());
1909 }
1910 }
1911 InConstantContext = D.hasConstantInitialization();
1912
1913 QualType destType = D.getType();
1914 const Expr *E = D.getInit();
1915 assert(E && "No initializer to emit");
1916
1917 if (!destType->isReferenceType()) {
1918 QualType nonMemoryDestType = getNonMemoryType(CGM, destType);
1919 if (llvm::Constant *C = ConstExprEmitter(*this).Visit(E, nonMemoryDestType))
1920 return emitForMemory(C, destType);
1921 }
1922
1923 // Try to emit the initializer. Note that this can allow some things that
1924 // are not allowed by tryEmitPrivateForMemory alone.
1925 if (APValue *value = D.evaluateValue()) {
1926 assert(!value->allowConstexprUnknown() &&
1927 "Constexpr unknown values are not allowed in CodeGen");
1928 return tryEmitPrivateForMemory(*value, destType);
1929 }
1930
1931 return nullptr;
1932}
1933
1934llvm::Constant *
1936 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1937 auto C = tryEmitAbstract(E, nonMemoryDestType);
1938 return (C ? emitForMemory(C, destType) : nullptr);
1939}
1940
1941llvm::Constant *
1943 QualType destType) {
1944 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1945 auto C = tryEmitAbstract(value, nonMemoryDestType);
1946 return (C ? emitForMemory(C, destType) : nullptr);
1947}
1948
1950 QualType destType) {
1951 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1952 llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType);
1953 return (C ? emitForMemory(C, destType) : nullptr);
1954}
1955
1957 QualType destType) {
1958 auto nonMemoryDestType = getNonMemoryType(CGM, destType);
1959 auto C = tryEmitPrivate(value, nonMemoryDestType);
1960 return (C ? emitForMemory(C, destType) : nullptr);
1961}
1962
1963/// Try to emit a constant signed pointer, given a raw pointer and the
1964/// destination ptrauth qualifier.
1965///
1966/// This can fail if the qualifier needs address discrimination and the
1967/// emitter is in an abstract mode.
1968llvm::Constant *
1970 PointerAuthQualifier Schema) {
1971 assert(Schema && "applying trivial ptrauth schema");
1972
1973 if (Schema.hasKeyNone())
1974 return UnsignedPointer;
1975
1976 unsigned Key = Schema.getKey();
1977
1978 // Create an address placeholder if we're using address discrimination.
1979 llvm::GlobalValue *StorageAddress = nullptr;
1980 if (Schema.isAddressDiscriminated()) {
1981 // We can't do this if the emitter is in an abstract state.
1982 if (isAbstract())
1983 return nullptr;
1984
1985 StorageAddress = getCurrentAddrPrivate();
1986 }
1987
1988 llvm::ConstantInt *Discriminator =
1989 llvm::ConstantInt::get(CGM.IntPtrTy, Schema.getExtraDiscriminator());
1990
1991 llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
1992 UnsignedPointer, Key, StorageAddress, Discriminator);
1993
1994 if (Schema.isAddressDiscriminated())
1995 registerCurrentAddrPrivate(SignedPointer, StorageAddress);
1996
1997 return SignedPointer;
1998}
1999
2001 llvm::Constant *C,
2002 QualType destType) {
2003 // For an _Atomic-qualified constant, we may need to add tail padding.
2004 if (auto AT = destType->getAs<AtomicType>()) {
2005 QualType destValueType = AT->getValueType();
2006 C = emitForMemory(CGM, C, destValueType);
2007
2008 uint64_t innerSize = CGM.getContext().getTypeSize(destValueType);
2009 uint64_t outerSize = CGM.getContext().getTypeSize(destType);
2010 if (innerSize == outerSize)
2011 return C;
2012
2013 assert(innerSize < outerSize && "emitted over-large constant for atomic");
2014 llvm::Constant *elts[] = {
2015 C,
2016 llvm::ConstantAggregateZero::get(
2017 llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8))
2018 };
2019 return llvm::ConstantStruct::getAnon(elts);
2020 }
2021
2022 // Zero-extend bool.
2023 // In HLSL bool vectors are stored in memory as a vector of i32
2024 if ((C->getType()->isIntegerTy(1) && !destType->isBitIntType()) ||
2025 (destType->isExtVectorBoolType() &&
2026 !destType->isPackedVectorBoolType(CGM.getContext()))) {
2027 llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType);
2028 llvm::Constant *Res = llvm::ConstantFoldCastOperand(
2029 llvm::Instruction::ZExt, C, boolTy, CGM.getDataLayout());
2030 assert(Res && "Constant folding must succeed");
2031 return Res;
2032 }
2033
2034 if (destType->isBitIntType()) {
2035 llvm::Type *MemTy = CGM.getTypes().ConvertTypeForMem(destType);
2036 if (C->getType() != MemTy) {
2037 ConstantAggregateBuilder Builder(CGM);
2038 llvm::Type *LoadStoreTy =
2039 CGM.getTypes().convertTypeForLoadStore(destType);
2040 // ptrtoint/inttoptr should not involve _BitInt in constant expressions,
2041 // so casting to ConstantInt is safe here.
2042 auto *CI = cast<llvm::ConstantInt>(C);
2043 llvm::Constant *Res = llvm::ConstantFoldCastOperand(
2045 ? llvm::Instruction::SExt
2046 : llvm::Instruction::ZExt,
2047 CI, LoadStoreTy, CGM.getDataLayout());
2048 if (CGM.getTypes().typeRequiresSplitIntoByteArray(destType,
2049 C->getType())) {
2050 // Long _BitInt has array of bytes as in-memory type.
2051 // So, split constant into individual bytes.
2052 llvm::APInt Value = cast<llvm::ConstantInt>(Res)->getValue();
2053 Builder.addBits(Value, /*OffsetInBits=*/0, /*AllowOverwrite=*/false);
2054 return Builder.build(MemTy, /*AllowOversized*/ false);
2055 }
2056 return Res;
2057 }
2058 }
2059
2060 return C;
2061}
2062
2063llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
2064 QualType destType) {
2065 assert(!destType->isVoidType() && "can't emit a void constant");
2066
2067 if (!destType->isReferenceType())
2068 if (llvm::Constant *C = ConstExprEmitter(*this).Visit(E, destType))
2069 return C;
2070
2072
2073 bool Success = false;
2074
2075 if (destType->isReferenceType())
2076 Success = E->EvaluateAsLValue(Result, CGM.getContext());
2077 else
2078 Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
2079
2080 if (Success && !Result.HasSideEffects)
2081 return tryEmitPrivate(Result.Val, destType);
2082
2083 return nullptr;
2084}
2085
2086llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {
2087 return getTargetCodeGenInfo().getNullPointer(*this, T, QT);
2088}
2089
2090namespace {
2091/// A struct which can be used to peephole certain kinds of finalization
2092/// that normally happen during l-value emission.
2093struct ConstantLValue {
2094 llvm::Constant *Value;
2095 bool HasOffsetApplied;
2096 bool HasDestPointerAuth;
2097
2098 /*implicit*/ ConstantLValue(llvm::Constant *value,
2099 bool hasOffsetApplied = false,
2100 bool hasDestPointerAuth = false)
2101 : Value(value), HasOffsetApplied(hasOffsetApplied),
2102 HasDestPointerAuth(hasDestPointerAuth) {}
2103
2104 /*implicit*/ ConstantLValue(ConstantAddress address)
2105 : ConstantLValue(address.getPointer()) {}
2106};
2107
2108/// A helper class for emitting constant l-values.
2109class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter,
2110 ConstantLValue> {
2111 CodeGenModule &CGM;
2112 ConstantEmitter &Emitter;
2113 const APValue &Value;
2114 QualType DestType;
2115 bool EnablePtrAuthFunctionTypeDiscrimination;
2116
2117 // Befriend StmtVisitorBase so that we don't have to expose Visit*.
2118 friend StmtVisitorBase;
2119
2120public:
2121 ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value,
2122 QualType destType,
2123 bool EnablePtrAuthFunctionTypeDiscrimination = true)
2124 : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType),
2125 EnablePtrAuthFunctionTypeDiscrimination(
2126 EnablePtrAuthFunctionTypeDiscrimination) {}
2127
2128 llvm::Constant *tryEmit();
2129
2130private:
2131 llvm::Constant *tryEmitAbsolute(llvm::Type *destTy);
2132 ConstantLValue tryEmitBase(const APValue::LValueBase &base);
2133
2134 ConstantLValue VisitStmt(const Stmt *S) { return nullptr; }
2135 ConstantLValue VisitConstantExpr(const ConstantExpr *E);
2136 ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
2137 ConstantLValue VisitStringLiteral(const StringLiteral *E);
2138 ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E);
2139 ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E);
2140 ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E);
2141 llvm::Constant *VisitObjCCollectionElement(const Expr *E);
2142 ConstantLValue VisitObjCArrayLiteral(const ObjCArrayLiteral *E);
2143 ConstantLValue VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E);
2144 ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E);
2145 ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E);
2146 ConstantLValue VisitCallExpr(const CallExpr *E);
2147 ConstantLValue VisitBlockExpr(const BlockExpr *E);
2148 ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E);
2149 ConstantLValue VisitMaterializeTemporaryExpr(
2150 const MaterializeTemporaryExpr *E);
2151
2152 ConstantLValue emitPointerAuthSignConstant(const CallExpr *E);
2153 llvm::Constant *emitPointerAuthPointer(const Expr *E);
2154 unsigned emitPointerAuthKey(const Expr *E);
2155 std::pair<llvm::Constant *, llvm::ConstantInt *>
2156 emitPointerAuthDiscriminator(const Expr *E);
2157
2158 bool hasNonZeroOffset() const {
2159 return !Value.getLValueOffset().isZero();
2160 }
2161
2162 /// Return the value offset.
2163 llvm::Constant *getOffset() {
2164 return llvm::ConstantInt::get(CGM.Int64Ty,
2165 Value.getLValueOffset().getQuantity());
2166 }
2167
2168 /// Apply the value offset to the given constant.
2169 llvm::Constant *applyOffset(llvm::Constant *C) {
2170 if (!hasNonZeroOffset())
2171 return C;
2172
2173 return llvm::ConstantExpr::getPtrAdd(C, getOffset());
2174 }
2175};
2176
2177}
2178
2179llvm::Constant *ConstantLValueEmitter::tryEmit() {
2180 const APValue::LValueBase &base = Value.getLValueBase();
2181
2182 // The destination type should be a pointer or reference
2183 // type, but it might also be a cast thereof.
2184 //
2185 // FIXME: the chain of casts required should be reflected in the APValue.
2186 // We need this in order to correctly handle things like a ptrtoint of a
2187 // non-zero null pointer and addrspace casts that aren't trivially
2188 // represented in LLVM IR.
2189 auto destTy = CGM.getTypes().ConvertTypeForMem(DestType);
2190 assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy));
2191
2192 // If there's no base at all, this is a null or absolute pointer,
2193 // possibly cast back to an integer type.
2194 if (!base) {
2195 return tryEmitAbsolute(destTy);
2196 }
2197
2198 // Otherwise, try to emit the base.
2199 ConstantLValue result = tryEmitBase(base);
2200
2201 // If that failed, we're done.
2202 llvm::Constant *value = result.Value;
2203 if (!value) return nullptr;
2204
2205 // Apply the offset if necessary and not already done.
2206 if (!result.HasOffsetApplied) {
2207 value = applyOffset(value);
2208 }
2209
2210 // Apply pointer-auth signing from the destination type.
2211 if (PointerAuthQualifier PointerAuth = DestType.getPointerAuth();
2212 PointerAuth && !result.HasDestPointerAuth) {
2213 value = Emitter.tryEmitConstantSignedPointer(value, PointerAuth);
2214 if (!value)
2215 return nullptr;
2216 }
2217
2218 // Convert to the appropriate type; this could be an lvalue for
2219 // an integer. FIXME: performAddrSpaceCast
2220 if (isa<llvm::PointerType>(destTy))
2221 return llvm::ConstantExpr::getPointerCast(value, destTy);
2222
2223 return llvm::ConstantExpr::getPtrToInt(value, destTy);
2224}
2225
2226/// Try to emit an absolute l-value, such as a null pointer or an integer
2227/// bitcast to pointer type.
2228llvm::Constant *
2229ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) {
2230 // If we're producing a pointer, this is easy.
2231 auto destPtrTy = cast<llvm::PointerType>(destTy);
2232 if (Value.isNullPointer()) {
2233 // FIXME: integer offsets from non-zero null pointers.
2234 return CGM.getNullPointer(destPtrTy, DestType);
2235 }
2236
2237 // Convert the integer to a pointer-sized integer before converting it
2238 // to a pointer.
2239 // FIXME: signedness depends on the original integer type.
2240 auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy);
2241 llvm::Constant *C;
2242 C = llvm::ConstantFoldIntegerCast(getOffset(), intptrTy, /*isSigned*/ false,
2243 CGM.getDataLayout());
2244 assert(C && "Must have folded, as Offset is a ConstantInt");
2245 C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy);
2246 return C;
2247}
2248
2249ConstantLValue
2250ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) {
2251 // Handle values.
2252 if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) {
2253 // The constant always points to the canonical declaration. We want to look
2254 // at properties of the most recent declaration at the point of emission.
2255 D = cast<ValueDecl>(D->getMostRecentDecl());
2256
2257 if (D->hasAttr<WeakRefAttr>())
2258 return CGM.GetWeakRefReference(D).getPointer();
2259
2260 auto PtrAuthSign = [&](llvm::Constant *C) {
2261 if (PointerAuthQualifier PointerAuth = DestType.getPointerAuth()) {
2262 C = applyOffset(C);
2263 C = Emitter.tryEmitConstantSignedPointer(C, PointerAuth);
2264 return ConstantLValue(C, /*applied offset*/ true, /*signed*/ true);
2265 }
2266
2267 CGPointerAuthInfo AuthInfo;
2268
2269 if (EnablePtrAuthFunctionTypeDiscrimination)
2270 AuthInfo = CGM.getFunctionPointerAuthInfo(DestType);
2271
2272 if (AuthInfo) {
2273 if (hasNonZeroOffset())
2274 return ConstantLValue(nullptr);
2275
2276 C = applyOffset(C);
2278 C, AuthInfo.getKey(), nullptr,
2279 cast_or_null<llvm::ConstantInt>(AuthInfo.getDiscriminator()));
2280 return ConstantLValue(C, /*applied offset*/ true, /*signed*/ true);
2281 }
2282
2283 return ConstantLValue(C);
2284 };
2285
2286 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2287 llvm::Constant *C = CGM.getRawFunctionPointer(FD);
2288 if (FD->getType()->isCFIUncheckedCalleeFunctionType())
2289 C = llvm::NoCFIValue::get(cast<llvm::GlobalValue>(C));
2290 return PtrAuthSign(C);
2291 }
2292
2293 if (const auto *VD = dyn_cast<VarDecl>(D)) {
2294 // We can never refer to a variable with local storage.
2295 if (!VD->hasLocalStorage()) {
2296 if (VD->isFileVarDecl() || VD->hasExternalStorage())
2297 return CGM.GetAddrOfGlobalVar(VD);
2298
2299 if (VD->isLocalVarDecl()) {
2300 return CGM.getOrCreateStaticVarDecl(
2301 *VD, CGM.getLLVMLinkageVarDefinition(VD));
2302 }
2303 }
2304 }
2305
2306 if (const auto *GD = dyn_cast<MSGuidDecl>(D))
2307 return CGM.GetAddrOfMSGuidDecl(GD);
2308
2309 if (const auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(D))
2310 return CGM.GetAddrOfUnnamedGlobalConstantDecl(GCD);
2311
2312 if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(D))
2313 return CGM.GetAddrOfTemplateParamObject(TPO);
2314
2315 return nullptr;
2316 }
2317
2318 // Handle typeid(T).
2319 if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>())
2320 return CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0));
2321
2322 // Otherwise, it must be an expression.
2323 return Visit(base.get<const Expr*>());
2324}
2325
2326ConstantLValue
2327ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) {
2328 if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(E))
2329 return Result;
2330 return Visit(E->getSubExpr());
2331}
2332
2333ConstantLValue
2334ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2335 ConstantEmitter CompoundLiteralEmitter(CGM, Emitter.CGF);
2336 CompoundLiteralEmitter.setInConstantContext(Emitter.isInConstantContext());
2337 return tryEmitGlobalCompoundLiteral(CompoundLiteralEmitter, E);
2338}
2339
2340ConstantLValue
2341ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) {
2343}
2344
2345ConstantLValue
2346ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2348}
2349
2350static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S,
2351 QualType T,
2352 CodeGenModule &CGM) {
2353 auto C = CGM.getObjCRuntime().GenerateConstantString(S);
2354 return C.withElementType(CGM.getTypes().ConvertTypeForMem(T));
2355}
2356
2357ConstantLValue
2358ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
2359 return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM);
2360}
2361
2362ConstantLValue
2363ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
2364 ASTContext &Context = CGM.getContext();
2365 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
2366 const Expr *SubExpr = E->getSubExpr();
2367 const QualType &Ty = SubExpr->IgnoreParens()->getType();
2368
2369 assert(SubExpr->isEvaluatable(Context) &&
2370 "Non const NSNumber is being emitted as a constant");
2371
2372 if (const auto *SL = dyn_cast<StringLiteral>(SubExpr->IgnoreParenCasts()))
2373 return emitConstantObjCStringLiteral(SL, E->getType(), CGM);
2374
2375 // Note `@YES` `@NO` need to be handled explicitly
2376 // to meet existing plist encoding / decoding expectations
2377 const bool IsBoolType =
2378 (Ty->isBooleanType() || NSAPI(Context).isObjCBOOLType(Ty));
2379 bool BoolValue = false;
2380 if (IsBoolType && SubExpr->EvaluateAsBooleanCondition(BoolValue, Context)) {
2381 ConstantAddress C = Runtime.GenerateConstantNumber(BoolValue, Ty);
2383 }
2384
2385 Expr::EvalResult IntResult{};
2386 if (SubExpr->EvaluateAsInt(IntResult, Context)) {
2387 ConstantAddress C =
2388 Runtime.GenerateConstantNumber(IntResult.Val.getInt(), Ty);
2390 }
2391
2392 llvm::APFloat FloatValue(0.0);
2393 if (SubExpr->EvaluateAsFloat(FloatValue, Context)) {
2394 ConstantAddress C = Runtime.GenerateConstantNumber(FloatValue, Ty);
2396 }
2397
2398 llvm_unreachable("SubExpr is expected to be evaluated as a numeric type");
2399}
2400
2401llvm::Constant *
2402ConstantLValueEmitter::VisitObjCCollectionElement(const Expr *E) {
2403 auto CE = cast<CastExpr>(E);
2404 const Expr *Elm = CE->getSubExpr();
2405 QualType DestTy = CE->getType();
2406
2407 assert(CE->getCastKind() == CK_BitCast &&
2408 "Expected a CK_BitCast type for valid items in constant objc "
2409 "collection literals");
2410
2411 llvm::Type *DstTy = CGM.getTypes().ConvertType(DestTy);
2412 ConstantLValue LV = Visit(Elm);
2413 llvm::Constant *ConstVal = cast<llvm::Constant>(LV.Value);
2414 llvm::Constant *Val = llvm::ConstantExpr::getBitCast(ConstVal, DstTy);
2415 return Val;
2416}
2417
2418ConstantLValue
2419ConstantLValueEmitter::VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
2420 SmallVector<llvm::Constant *, 16> ObjectExpressions;
2421 uint64_t NumElements = E->getNumElements();
2422 ObjectExpressions.reserve(NumElements);
2423
2424 for (uint64_t i = 0; i < NumElements; i++) {
2425 llvm::Constant *Val = VisitObjCCollectionElement(E->getElement(i));
2426 ObjectExpressions.push_back(Val);
2427 }
2428 ConstantAddress C =
2429 CGM.getObjCRuntime().GenerateConstantArray(ObjectExpressions);
2431}
2432
2433ConstantLValue ConstantLValueEmitter::VisitObjCDictionaryLiteral(
2434 const ObjCDictionaryLiteral *E) {
2435 SmallVector<std::pair<llvm::Constant *, llvm::Constant *>, 16> KeysAndObjects;
2436 uint64_t NumElements = E->getNumElements();
2437 KeysAndObjects.reserve(NumElements);
2438
2439 for (uint64_t i = 0; i < NumElements; i++) {
2440 llvm::Constant *Key =
2441 VisitObjCCollectionElement(E->getKeyValueElement(i).Key);
2442 llvm::Constant *Val =
2443 VisitObjCCollectionElement(E->getKeyValueElement(i).Value);
2444 KeysAndObjects.push_back({Key, Val});
2445 }
2446 ConstantAddress C =
2447 CGM.getObjCRuntime().GenerateConstantDictionary(E, KeysAndObjects);
2449}
2450
2451ConstantLValue
2452ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) {
2454}
2455
2456ConstantLValue
2457ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) {
2458 assert(Emitter.CGF && "Invalid address of label expression outside function");
2459 llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel());
2460 return Ptr;
2461}
2462
2463ConstantLValue
2464ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) {
2465 unsigned builtin = E->getBuiltinCallee();
2466 if (builtin == Builtin::BI__builtin_function_start)
2467 return CGM.GetFunctionStart(
2469
2470 if (builtin == Builtin::BI__builtin_ptrauth_sign_constant)
2471 return emitPointerAuthSignConstant(E);
2472
2473 if (builtin != Builtin::BI__builtin___CFStringMakeConstantString &&
2474 builtin != Builtin::BI__builtin___NSStringMakeConstantString)
2475 return nullptr;
2476
2477 const auto *Literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts());
2478 if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) {
2479 return CGM.getObjCRuntime().GenerateConstantString(Literal);
2480 } else {
2481 // FIXME: need to deal with UCN conversion issues.
2482 return CGM.GetAddrOfConstantCFString(Literal);
2483 }
2484}
2485
2486ConstantLValue
2487ConstantLValueEmitter::emitPointerAuthSignConstant(const CallExpr *E) {
2488 llvm::Constant *UnsignedPointer = emitPointerAuthPointer(E->getArg(0));
2489 unsigned Key = emitPointerAuthKey(E->getArg(1));
2490 auto [StorageAddress, OtherDiscriminator] =
2491 emitPointerAuthDiscriminator(E->getArg(2));
2492
2493 llvm::Constant *SignedPointer = CGM.getConstantSignedPointer(
2494 UnsignedPointer, Key, StorageAddress, OtherDiscriminator);
2495 return SignedPointer;
2496}
2497
2498llvm::Constant *ConstantLValueEmitter::emitPointerAuthPointer(const Expr *E) {
2499 Expr::EvalResult Result;
2500 bool Succeeded = E->EvaluateAsRValue(Result, CGM.getContext());
2501 assert(Succeeded);
2502 (void)Succeeded;
2503
2504 // The assertions here are all checked by Sema.
2505 assert(Result.Val.isLValue());
2506 if (isa<FunctionDecl>(Result.Val.getLValueBase().get<const ValueDecl *>()))
2507 assert(Result.Val.getLValueOffset().isZero());
2508 return ConstantEmitter(CGM, Emitter.CGF)
2509 .emitAbstract(E->getExprLoc(), Result.Val, E->getType(), false);
2510}
2511
2512unsigned ConstantLValueEmitter::emitPointerAuthKey(const Expr *E) {
2513 return E->EvaluateKnownConstInt(CGM.getContext()).getZExtValue();
2514}
2515
2516std::pair<llvm::Constant *, llvm::ConstantInt *>
2517ConstantLValueEmitter::emitPointerAuthDiscriminator(const Expr *E) {
2518 E = E->IgnoreParens();
2519
2520 if (const auto *Call = dyn_cast<CallExpr>(E)) {
2521 if (Call->getBuiltinCallee() ==
2522 Builtin::BI__builtin_ptrauth_blend_discriminator) {
2523 llvm::Constant *Pointer = ConstantEmitter(CGM).emitAbstract(
2524 Call->getArg(0), Call->getArg(0)->getType());
2525 auto *Extra = cast<llvm::ConstantInt>(ConstantEmitter(CGM).emitAbstract(
2526 Call->getArg(1), Call->getArg(1)->getType()));
2527 return {Pointer, Extra};
2528 }
2529 }
2530
2531 llvm::Constant *Result = ConstantEmitter(CGM).emitAbstract(E, E->getType());
2532 if (Result->getType()->isPointerTy())
2533 return {Result, nullptr};
2534 return {nullptr, cast<llvm::ConstantInt>(Result)};
2535}
2536
2537ConstantLValue
2538ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) {
2539 StringRef functionName;
2540 if (auto CGF = Emitter.CGF)
2541 functionName = CGF->CurFn->getName();
2542 else
2543 functionName = "global";
2544
2545 return CGM.GetAddrOfGlobalBlock(E, functionName);
2546}
2547
2548ConstantLValue
2549ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
2550 QualType T;
2551 if (E->isTypeOperand())
2552 T = E->getTypeOperand(CGM.getContext());
2553 else
2554 T = E->getExprOperand()->getType();
2555 return CGM.GetAddrOfRTTIDescriptor(T);
2556}
2557
2558ConstantLValue
2559ConstantLValueEmitter::VisitMaterializeTemporaryExpr(
2560 const MaterializeTemporaryExpr *E) {
2561 assert(E->getStorageDuration() == SD_Static);
2562 const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
2563 return CGM.GetAddrOfGlobalTemporary(E, Inner);
2564}
2565
2566llvm::Constant *
2568 bool EnablePtrAuthFunctionTypeDiscrimination) {
2569 switch (Value.getKind()) {
2570 case APValue::None:
2572 // Out-of-lifetime and indeterminate values can be modeled as 'undef'.
2573 return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType));
2574 case APValue::LValue:
2575 return ConstantLValueEmitter(*this, Value, DestType,
2576 EnablePtrAuthFunctionTypeDiscrimination)
2577 .tryEmit();
2578 case APValue::Int:
2579 if (PointerAuthQualifier PointerAuth = DestType.getPointerAuth();
2580 PointerAuth &&
2581 (PointerAuth.authenticatesNullValues() || Value.getInt() != 0))
2582 return nullptr;
2583 return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt());
2585 return llvm::ConstantInt::get(CGM.getLLVMContext(),
2586 Value.getFixedPoint().getValue());
2587 case APValue::ComplexInt: {
2588 llvm::Constant *Complex[2];
2589
2590 Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2591 Value.getComplexIntReal());
2592 Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(),
2593 Value.getComplexIntImag());
2594
2595 // FIXME: the target may want to specify that this is packed.
2596 llvm::StructType *STy =
2597 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2598 return llvm::ConstantStruct::get(STy, Complex);
2599 }
2600 case APValue::Float: {
2601 const llvm::APFloat &Init = Value.getFloat();
2602 if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() &&
2603 !CGM.getContext().getLangOpts().NativeHalfType &&
2604 CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics())
2605 return llvm::ConstantInt::get(CGM.getLLVMContext(),
2606 Init.bitcastToAPInt());
2607 else
2608 return llvm::ConstantFP::get(CGM.getLLVMContext(), Init);
2609 }
2610 case APValue::ComplexFloat: {
2611 llvm::Constant *Complex[2];
2612
2613 Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2614 Value.getComplexFloatReal());
2615 Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(),
2616 Value.getComplexFloatImag());
2617
2618 // FIXME: the target may want to specify that this is packed.
2619 llvm::StructType *STy =
2620 llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType());
2621 return llvm::ConstantStruct::get(STy, Complex);
2622 }
2623 case APValue::Vector: {
2624 unsigned NumElts = Value.getVectorLength();
2626
2627 for (unsigned I = 0; I != NumElts; ++I) {
2628 const APValue &Elt = Value.getVectorElt(I);
2629 if (Elt.isInt())
2630 Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
2631 else if (Elt.isFloat())
2632 Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
2633 else if (Elt.isIndeterminate())
2634 Inits[I] = llvm::UndefValue::get(CGM.getTypes().ConvertType(
2635 DestType->castAs<VectorType>()->getElementType()));
2636 else
2637 llvm_unreachable("unsupported vector element type");
2638 }
2639 return llvm::ConstantVector::get(Inits);
2640 }
2641 case APValue::Matrix: {
2642 const auto *MT = DestType->castAs<ConstantMatrixType>();
2643 unsigned NumRows = Value.getMatrixNumRows();
2644 unsigned NumCols = Value.getMatrixNumColumns();
2645 unsigned NumElts = NumRows * NumCols;
2647
2648 bool IsRowMajor = CGM.getLangOpts().getDefaultMatrixMemoryLayout() ==
2650
2651 for (unsigned Row = 0; Row != NumRows; ++Row) {
2652 for (unsigned Col = 0; Col != NumCols; ++Col) {
2653 const APValue &Elt = Value.getMatrixElt(Row, Col);
2654 unsigned Idx = MT->getFlattenedIndex(Row, Col, IsRowMajor);
2655 if (Elt.isInt())
2656 Inits[Idx] =
2657 llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt());
2658 else if (Elt.isFloat())
2659 Inits[Idx] =
2660 llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat());
2661 else if (Elt.isIndeterminate())
2662 Inits[Idx] = llvm::PoisonValue::get(
2663 CGM.getTypes().ConvertType(MT->getElementType()));
2664 else
2665 llvm_unreachable("unsupported matrix element type");
2666 }
2667 }
2668 return llvm::ConstantVector::get(Inits);
2669 }
2671 const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS();
2672 const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS();
2673 llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType());
2674 llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType());
2675 if (!LHS || !RHS) return nullptr;
2676
2677 // Compute difference
2678 llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType);
2679 LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy);
2680 RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy);
2681 llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS);
2682
2683 // LLVM is a bit sensitive about the exact format of the
2684 // address-of-label difference; make sure to truncate after
2685 // the subtraction.
2686 return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType);
2687 }
2688 case APValue::Struct:
2689 case APValue::Union:
2690 return ConstStructBuilder::BuildStruct(*this, Value, DestType);
2691 case APValue::Array: {
2692 const ArrayType *ArrayTy = CGM.getContext().getAsArrayType(DestType);
2693 unsigned NumElements = Value.getArraySize();
2694 unsigned NumInitElts = Value.getArrayInitializedElts();
2695
2696 // Emit array filler, if there is one.
2697 llvm::Constant *Filler = nullptr;
2698 if (Value.hasArrayFiller()) {
2699 Filler = tryEmitAbstractForMemory(Value.getArrayFiller(),
2700 ArrayTy->getElementType());
2701 if (!Filler)
2702 return nullptr;
2703 }
2704
2705 // Emit initializer elements.
2707 if (Filler && Filler->isNullValue())
2708 Elts.reserve(NumInitElts + 1);
2709 else
2710 Elts.reserve(NumElements);
2711
2712 llvm::Type *CommonElementType = nullptr;
2713 for (unsigned I = 0; I < NumInitElts; ++I) {
2714 llvm::Constant *C = tryEmitPrivateForMemory(
2715 Value.getArrayInitializedElt(I), ArrayTy->getElementType());
2716 if (!C) return nullptr;
2717
2718 if (I == 0)
2719 CommonElementType = C->getType();
2720 else if (C->getType() != CommonElementType)
2721 CommonElementType = nullptr;
2722 Elts.push_back(C);
2723 }
2724
2725 llvm::ArrayType *Desired =
2726 cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType));
2727
2728 // Fix the type of incomplete arrays if the initializer isn't empty.
2729 if (DestType->isIncompleteArrayType() && !Elts.empty())
2730 Desired = llvm::ArrayType::get(Desired->getElementType(), Elts.size());
2731
2732 return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
2733 Filler);
2734 }
2736 return CGM.getCXXABI().EmitMemberPointer(Value, DestType);
2737 }
2738 llvm_unreachable("Unknown APValue kind");
2739}
2740
2742 const CompoundLiteralExpr *E) {
2743 return EmittedCompoundLiterals.lookup(E);
2744}
2745
2747 const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) {
2748 bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second;
2749 (void)Ok;
2750 assert(Ok && "CLE has already been emitted!");
2751}
2752
2755 assert(E->isFileScope() && "not a file-scope compound literal expr");
2756 ConstantEmitter emitter(*this);
2757 return tryEmitGlobalCompoundLiteral(emitter, E);
2758}
2759
2760llvm::Constant *
2762 // Member pointer constants always have a very particular form.
2764 const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl();
2765
2766 // A member function pointer.
2767 if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl))
2768 return getCXXABI().EmitMemberFunctionPointer(method);
2769
2770 // Otherwise, a member data pointer.
2771 getContext().recordMemberDataPointerEvaluation(decl);
2772 uint64_t fieldOffset = getContext().getFieldOffset(decl);
2773 CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset);
2774 return getCXXABI().EmitMemberDataPointer(type, chars);
2775}
2776
2777static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2778 llvm::Type *baseType,
2779 const CXXRecordDecl *base);
2780
2781static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
2782 const RecordDecl *record,
2783 bool asCompleteObject) {
2784 const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record);
2785 llvm::StructType *structure =
2786 (asCompleteObject ? layout.getLLVMType()
2787 : layout.getBaseSubobjectLLVMType());
2788
2789 unsigned numElements = structure->getNumElements();
2790 std::vector<llvm::Constant *> elements(numElements);
2791
2792 auto CXXR = dyn_cast<CXXRecordDecl>(record);
2793 // Fill in all the bases.
2794 if (CXXR) {
2795 for (const auto &I : CXXR->bases()) {
2796 if (I.isVirtual()) {
2797 // Ignore virtual bases; if we're laying out for a complete
2798 // object, we'll lay these out later.
2799 continue;
2800 }
2801
2802 const auto *base = I.getType()->castAsCXXRecordDecl();
2803 // Ignore empty bases.
2804 if (isEmptyRecordForLayout(CGM.getContext(), I.getType()) ||
2805 CGM.getContext()
2806 .getASTRecordLayout(base)
2808 .isZero())
2809 continue;
2810
2811 unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base);
2812 llvm::Type *baseType = structure->getElementType(fieldIndex);
2813 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2814 }
2815 }
2816
2817 // Fill in all the fields.
2818 for (const auto *Field : record->fields()) {
2819 // Fill in non-bitfields. (Bitfields always use a zero pattern, which we
2820 // will fill in later.)
2821 if (!Field->isBitField() &&
2822 !isEmptyFieldForLayout(CGM.getContext(), Field)) {
2823 unsigned fieldIndex = layout.getLLVMFieldNo(Field);
2824 elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
2825 }
2826
2827 // For unions, stop after the first named field.
2828 if (record->isUnion()) {
2829 if (Field->getIdentifier())
2830 break;
2831 if (const auto *FieldRD = Field->getType()->getAsRecordDecl())
2832 if (FieldRD->findFirstNamedDataMember())
2833 break;
2834 }
2835 }
2836
2837 // Fill in the virtual bases, if we're working with the complete object.
2838 if (CXXR && asCompleteObject) {
2839 for (const auto &I : CXXR->vbases()) {
2840 const auto *base = I.getType()->castAsCXXRecordDecl();
2841 // Ignore empty bases.
2842 if (isEmptyRecordForLayout(CGM.getContext(), I.getType()))
2843 continue;
2844
2845 unsigned fieldIndex = layout.getVirtualBaseIndex(base);
2846
2847 // We might have already laid this field out.
2848 if (elements[fieldIndex]) continue;
2849
2850 llvm::Type *baseType = structure->getElementType(fieldIndex);
2851 elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base);
2852 }
2853 }
2854
2855 // Now go through all other fields and zero them out.
2856 for (unsigned i = 0; i != numElements; ++i) {
2857 if (!elements[i])
2858 elements[i] = llvm::Constant::getNullValue(structure->getElementType(i));
2859 }
2860
2861 return llvm::ConstantStruct::get(structure, elements);
2862}
2863
2864/// Emit the null constant for a base subobject.
2865static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM,
2866 llvm::Type *baseType,
2867 const CXXRecordDecl *base) {
2868 const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base);
2869
2870 // Just zero out bases that don't have any pointer to data members.
2871 if (baseLayout.isZeroInitializableAsBase())
2872 return llvm::Constant::getNullValue(baseType);
2873
2874 // Otherwise, we can just use its null constant.
2875 return EmitNullConstant(CGM, base, /*asCompleteObject=*/false);
2876}
2877
2879 QualType T) {
2880 return emitForMemory(CGM, CGM.EmitNullConstant(T), T);
2881}
2882
2884 if (T->getAs<PointerType>())
2885 return getNullPointer(
2886 cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T);
2887
2888 if (getTypes().isZeroInitializable(T))
2889 return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T));
2890
2891 if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) {
2892 llvm::ArrayType *ATy =
2893 cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T));
2894
2895 QualType ElementTy = CAT->getElementType();
2896
2897 llvm::Constant *Element =
2898 ConstantEmitter::emitNullForMemory(*this, ElementTy);
2899 unsigned NumElements = CAT->getZExtSize();
2900 SmallVector<llvm::Constant *, 8> Array(NumElements, Element);
2901 return llvm::ConstantArray::get(ATy, Array);
2902 }
2903
2904 if (const auto *RD = T->getAsRecordDecl())
2905 return ::EmitNullConstant(*this, RD,
2906 /*asCompleteObject=*/true);
2907
2908 assert(T->isMemberDataPointerType() &&
2909 "Should only see pointers to data members here!");
2910
2911 return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>());
2912}
2913
2914llvm::Constant *
2916 return ::EmitNullConstant(*this, Record, false);
2917}
Defines the clang::ASTContext interface.
Defines enum values for all the target-independent builtin functions.
static QualType getNonMemoryType(CodeGenModule &CGM, QualType type)
static llvm::Constant * EmitNullConstant(CodeGenModule &CGM, const RecordDecl *record, bool asCompleteObject)
static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S, QualType T, CodeGenModule &CGM)
static llvm::Constant * EmitNullConstantForBase(CodeGenModule &CGM, llvm::Type *baseType, const CXXRecordDecl *base)
Emit the null constant for a base subobject.
TokenType getType() const
Returns the token's type, e.g.
llvm::MachO::Record Record
Definition MachO.h:31
llvm::json::Array Array
llvm::APInt getValue() const
QualType getType() const
Definition APValue.cpp:63
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
APSInt & getInt()
Definition APValue.h:508
APValue & getStructField(unsigned i)
Definition APValue.h:667
const FieldDecl * getUnionField() const
Definition APValue.h:679
bool isFloat() const
Definition APValue.h:486
APValue & getUnionValue()
Definition APValue.h:683
bool isIndeterminate() const
Definition APValue.h:482
bool isInt() const
Definition APValue.h:485
@ Indeterminate
This object has an indeterminate value (C++ [basic.indet]).
Definition APValue.h:131
@ None
There is no such object (it's outside its lifetime).
Definition APValue.h:129
APFloat & getFloat()
Definition APValue.h:522
APValue & getStructBase(unsigned i)
Definition APValue.h:662
const ConstantArrayType * getAsConstantArrayType(QualType T) const
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getIntWidth(QualType T) const
const llvm::fltSemantics & getFloatTypeSemantics(QualType T) const
Return the APFloat 'semantics' for the specified scalar floating point type.
bool isPFPField(const FieldDecl *Field) const
void getObjCEncodingForType(QualType T, std::string &S, const FieldDecl *Field=nullptr, QualType *NotEncodedT=nullptr) const
Emit the Objective-CC type encoding for the given type T into S.
const ASTRecordLayout & getASTRecordLayout(const RecordDecl *D) const
Get or compute information about the layout of the specified record (struct/union/class) D,...
const LangOptions & getLangOpts() const
Definition ASTContext.h:952
bool arePFPFieldsTriviallyCopyable(const RecordDecl *RD) const
Returns whether this record's PFP fields (if any) are trivially copyable (i.e.
QualType getQualifiedType(SplitQualType split) const
Un-split a SplitQualType.
int64_t toBits(CharUnits CharSize) const
Convert a size in characters to a size in bits.
uint64_t getTypeSize(QualType T) const
Return the size of the specified (complete) type T, in bits.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
static bool hasSameType(QualType T1, QualType T2)
Determine whether the given types T1 and T2 are equivalent.
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
unsigned getTargetAddressSpace(LangAS AS) const
static bool hasSameUnqualifiedType(QualType T1, QualType T2)
Determine whether the given types are equivalent after cvr-qualifiers have been removed.
uint64_t getCharWidth() const
Return the size of the character type, in bits.
bool hasOwnVFPtr() const
hasOwnVFPtr - Does this class provide its own virtual-function table pointer, rather than inheriting ...
CharUnits getSize() const
getSize - Get the record size in characters.
uint64_t getFieldOffset(unsigned FieldNo) const
getFieldOffset - Get the offset of the given field index, in bits.
CharUnits getBaseClassOffset(const CXXRecordDecl *Base) const
getBaseClassOffset - Get the offset, in chars, for the given base class.
const CXXRecordDecl * getPrimaryBase() const
getPrimaryBase - Get the primary base for this record.
CharUnits getNonVirtualSize() const
getNonVirtualSize - Get the non-virtual size (in chars) of an object, which is the size of the object...
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition Expr.h:4553
LabelDecl * getLabel() const
Definition Expr.h:4576
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3772
QualType getElementType() const
Definition TypeBase.h:3784
Represents a call to a C++ constructor.
Definition ExprCXX.h:1549
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition ExprCXX.h:1692
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1612
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Definition ExprCXX.h:1689
Represents a C++ constructor within a class.
Definition DeclCXX.h:2624
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3051
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition DeclCXX.cpp:3071
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1105
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2136
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
const CXXBaseSpecifier * base_class_const_iterator
Iterator that traverses the base classes of a class.
Definition DeclCXX.h:520
bool isTypeOperand() const
Definition ExprCXX.h:885
QualType getTypeOperand(const ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition ExprCXX.cpp:161
Expr * getExprOperand() const
Definition ExprCXX.h:896
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
unsigned getBuiltinCallee() const
getBuiltinCallee - If this is a call to a builtin, return the builtin ID of the callee.
Definition Expr.cpp:1592
CastKind getCastKind() const
Definition Expr.h:3723
const FieldDecl * getTargetUnionField() const
Definition Expr.h:3773
Expr * getSubExpr()
Definition Expr.h:3729
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition CharUnits.h:189
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition CharUnits.h:58
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
CharUnits alignTo(const CharUnits &Align) const
alignTo - Returns the next integer (mod 2**64) that is greater than or equal to this quantity and is ...
Definition CharUnits.h:201
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition CharUnits.h:53
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition Expr.h:4887
virtual llvm::Constant * getVTableAddressPoint(BaseSubobject Base, const CXXRecordDecl *VTableClass)=0
Get the address point of the vtable for the given base subobject.
virtual llvm::Value * EmitMemberPointerConversion(CodeGenFunction &CGF, const CastExpr *E, llvm::Value *Src)
Perform a derived-to-base, base-to-derived, or bitcast member pointer conversion.
Definition CGCXXABI.cpp:71
virtual ConstantAddress GenerateConstantNumber(const bool Value, const QualType &Ty)=0
virtual ConstantAddress GenerateConstantDictionary(const ObjCDictionaryLiteral *E, ArrayRef< std::pair< llvm::Constant *, llvm::Constant * > > KeysAndObjects)=0
virtual ConstantAddress GenerateConstantString(const StringLiteral *)=0
Generate a constant string object.
virtual ConstantAddress GenerateConstantArray(const ArrayRef< llvm::Constant * > &Objects)=0
llvm::Value * getDiscriminator() const
CGRecordLayout - This class handles struct and union layout info while lowering AST types to LLVM typ...
unsigned getNonVirtualBaseLLVMFieldNo(const CXXRecordDecl *RD) const
llvm::StructType * getLLVMType() const
Return the "complete object" LLVM type associated with this record.
const CGBitFieldInfo & getBitFieldInfo(const FieldDecl *FD) const
Return the BitFieldInfo that corresponds to the field FD.
bool isZeroInitializableAsBase() const
Check whether this struct can be C++ zero-initialized with a zeroinitializer when considered as a bas...
llvm::StructType * getBaseSubobjectLLVMType() const
Return the "base subobject" LLVM type associated with this record.
unsigned getLLVMFieldNo(const FieldDecl *FD) const
Return llvm::StructType element number that corresponds to the field FD.
unsigned getVirtualBaseIndex(const CXXRecordDecl *base) const
Return the LLVM field index corresponding to the given virtual base.
llvm::BlockAddress * GetAddrOfLabel(const LabelDecl *L)
This class organizes the cross-function state that is used while generating LLVM code.
ConstantAddress GetAddrOfMSGuidDecl(const MSGuidDecl *GD)
Get the address of a GUID.
void EmitExplicitCastExprType(const ExplicitCastExpr *E, CodeGenFunction *CGF=nullptr)
Emit type info if type of an expression is a variably modified type.
Definition CGExpr.cpp:1387
llvm::Module & getModule() const
llvm::GlobalValue * getPFPDeactivationSymbol(const FieldDecl *FD)
llvm::Constant * performAddrSpaceCast(llvm::Constant *Src, llvm::Type *DestTy)
ConstantAddress GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E)
Returns a pointer to a constant global variable for the given file-scope compound literal expression.
llvm::Constant * EmitNullConstantForBase(const CXXRecordDecl *Record)
Return a null constant appropriate for zero-initializing a base class with the given type.
llvm::Constant * getRawFunctionPointer(GlobalDecl GD, llvm::Type *Ty=nullptr)
Return a function pointer for a reference to the given function.
Definition CGExpr.cpp:3449
llvm::Constant * GetAddrOfRTTIDescriptor(QualType Ty, bool ForEH=false)
Get the address of the RTTI descriptor for the given type.
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
llvm::Constant * GetAddrOfGlobalBlock(const BlockExpr *BE, StringRef Name)
Gets the address of a block which requires no captures.
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD)
Returns LLVM linkage for a declarator.
llvm::Constant * getMemberPointerConstant(const UnaryOperator *e)
const llvm::DataLayout & getDataLayout() const
ConstantAddress GetWeakRefReference(const ValueDecl *VD)
Get a reference to the target of VD.
std::string getPFPFieldName(const FieldDecl *FD)
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType T)
Return the abstract pointer authentication schema for a pointer to the given function type.
llvm::Constant * GetFunctionStart(const ValueDecl *Decl)
llvm::GlobalVariable * getAddrOfConstantCompoundLiteralIfEmitted(const CompoundLiteralExpr *E)
If it's been emitted already, returns the GlobalVariable corresponding to a compound literal.
std::optional< PointerAuthQualifier > getVTablePointerAuthentication(const CXXRecordDecl *thisClass)
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition CGDecl.cpp:256
ConstantAddress GetAddrOfConstantCFString(const StringLiteral *Literal)
Return a pointer to a constant CFString object for the given string.
ConstantAddress GetAddrOfConstantStringFromLiteral(const StringLiteral *S, StringRef Name=".str")
Return a pointer to a constant array for the given string literal.
ASTContext & getContext() const
ConstantAddress GetAddrOfTemplateParamObject(const TemplateParamObjectDecl *TPO)
Get the address of a template parameter object.
ConstantAddress GetAddrOfUnnamedGlobalConstantDecl(const UnnamedGlobalConstantDecl *GCD)
Get the address of a UnnamedGlobalConstant.
llvm::Constant * GetAddrOfGlobalVar(const VarDecl *D, llvm::Type *Ty=nullptr, ForDefinition_t IsForDefinition=NotForDefinition)
Return the llvm::Constant for the address of the given global variable.
void setAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV)
Notes that CLE's GlobalVariable is GV.
const TargetCodeGenInfo & getTargetCodeGenInfo()
llvm::Constant * GetConstantArrayFromStringLiteral(const StringLiteral *E)
Return a constant array for the given string.
llvm::LLVMContext & getLLVMContext()
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
ConstantAddress GetAddrOfGlobalTemporary(const MaterializeTemporaryExpr *E, const Expr *Inner)
Returns a pointer to a global variable representing a temporary with static or thread storage duratio...
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
llvm::Constant * getConstantSignedPointer(llvm::Constant *Pointer, const PointerAuthSchema &Schema, llvm::Constant *StorageAddress, GlobalDecl SchemaDecl, QualType SchemaType)
Sign a constant pointer using the given scheme, producing a constant with the same IR type.
ConstantAddress GetAddrOfConstantStringFromObjCEncode(const ObjCEncodeExpr *)
Return a pointer to a constant array for the given ObjCEncodeExpr node.
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::Type * convertTypeForLoadStore(QualType T, llvm::Type *LLVMTy=nullptr)
Given that T is a scalar type, return the IR type that should be used for load and store operations.
const CGRecordLayout & getCGRecordLayout(const RecordDecl *)
getCGRecordLayout - Return record layout info for the given record decl.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
A specialization of Address that requires the address to be an LLVM Constant.
Definition Address.h:296
ConstantAddress withElementType(llvm::Type *ElemTy) const
Definition Address.h:312
static ConstantAddress invalid()
Definition Address.h:304
llvm::Constant * getPointer() const
Definition Address.h:308
llvm::Constant * tryEmitPrivateForMemory(const Expr *E, QualType T)
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
llvm::Constant * tryEmitPrivateForVarInit(const VarDecl &D)
llvm::Constant * tryEmitPrivate(const Expr *E, QualType T)
void finalize(llvm::GlobalVariable *global)
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
llvm::Constant * emitAbstract(const Expr *E, QualType T)
Emit the result of the given expression as an abstract constant, asserting that it succeeded.
llvm::GlobalValue * getCurrentAddrPrivate()
Get the address of the current location.
llvm::Constant * tryEmitConstantExpr(const ConstantExpr *CE)
llvm::Constant * emitForMemory(llvm::Constant *C, QualType T)
llvm::Constant * emitNullForMemory(QualType T)
llvm::Constant * tryEmitAbstract(const Expr *E, QualType T)
Try to emit the result of the given expression as an abstract constant.
void registerCurrentAddrPrivate(llvm::Constant *signal, llvm::GlobalValue *placeholder)
Register a 'signal' value with the emitter to inform it where to resolve a placeholder.
llvm::Constant * emitForInitializer(const APValue &value, LangAS destAddrSpace, QualType destType)
llvm::Constant * tryEmitAbstractForMemory(const Expr *E, QualType T)
bool isAbstract() const
Is the current emission context abstract?
llvm::Constant * tryEmitConstantSignedPointer(llvm::Constant *Ptr, PointerAuthQualifier Auth)
Try to emit a constant signed pointer, given a raw pointer and the destination ptrauth qualifier.
CompoundLiteralExpr - [C99 6.5.2.5].
Definition Expr.h:3608
bool isFileScope() const
Definition Expr.h:3640
const Expr * getInitializer() const
Definition Expr.h:3636
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3810
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition TypeBase.h:3886
ConstantExpr - An expression that occurs in a constant context and optionally the result of evaluatin...
Definition Expr.h:1085
APValue getAPValueResult() const
Definition Expr.cpp:413
bool hasAPValueResult() const
Definition Expr.h:1160
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4437
InitListExpr * getUpdater() const
Definition Expr.h:5939
This represents one expression.
Definition Expr.h:112
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:84
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool isGLValue() const
Definition Expr.h:287
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition Expr.cpp:3095
llvm::APSInt EvaluateKnownConstInt(const ASTContext &Ctx) const
EvaluateKnownConstInt - Call EvaluateAsRValue and return the folded integer.
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Returns the set of floating point options that apply to this expression.
Definition Expr.cpp:3989
bool EvaluateAsFloat(llvm::APFloat &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsFloat - Return true if this is a constant which we can fold and convert to a floating point...
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3086
bool EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsLValue - Evaluate an expression to see if we can fold it to an lvalue with link time known ...
bool isEvaluatable(const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects) const
isEvaluatable - Call EvaluateAsRValue to see if this expression can be constant folded without side-e...
bool EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsRValue - Return true if this is a constant which we can fold to an rvalue using any crazy t...
bool EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, bool InConstantContext=false) const
EvaluateAsBooleanCondition - Return true if this is a constant which we can fold and convert to a boo...
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition Expr.cpp:277
QualType getType() const
Definition Expr.h:144
const ValueDecl * getAsBuiltinConstantDeclRef(const ASTContext &Context) const
If this expression is an unambiguous reference to a single declaration, in the style of __builtin_fun...
Definition Expr.cpp:226
RoundingMode getRoundingMode() const
const Expr * getSubExpr() const
Definition Expr.h:1065
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2392
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
Definition Expr.cpp:2462
FieldDecl * getInitializedFieldInUnion()
If this initializes a union, specifies which field in the union to initialize.
Definition Expr.h:5428
unsigned getNumInits() const
Definition Expr.h:5332
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition Expr.h:5404
const Expr * getInit(unsigned Init) const
Definition Expr.h:5356
ArrayRef< Expr * > inits()
Definition Expr.h:5352
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4946
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4938
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3703
Expr * getElement(unsigned Index)
getElement - Return the Element at the specified index.
Definition ExprObjC.h:265
unsigned getNumElements() const
getNumElements - Return number of elements of objective-c array literal.
Definition ExprObjC.h:257
unsigned getNumElements() const
getNumElements - Return number of elements of objective-c dictionary literal.
Definition ExprObjC.h:392
ObjCDictionaryElement getKeyValueElement(unsigned Index) const
Definition ExprObjC.h:394
QualType getEncodedType() const
Definition ExprObjC.h:460
StringLiteral * getString()
Definition ExprObjC.h:96
Expr * getSelectedExpr() const
Definition ExprCXX.h:4640
const Expr * getSubExpr() const
Definition Expr.h:2202
Pointer-authentication qualifiers.
Definition TypeBase.h:152
bool isAddressDiscriminated() const
Definition TypeBase.h:265
unsigned getExtraDiscriminator() const
Definition TypeBase.h:270
unsigned getKey() const
Definition TypeBase.h:258
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3378
StringLiteral * getFunctionName()
Definition Expr.h:2052
A (possibly-)qualified type.
Definition TypeBase.h:937
PointerAuthQualifier getPointerAuth() const
Definition TypeBase.h:1459
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8557
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1036
Represents a struct/union/class.
Definition Decl.h:4342
bool hasFlexibleArrayMember() const
Definition Decl.h:4375
field_iterator field_end() const
Definition Decl.h:4548
field_range fields() const
Definition Decl.h:4545
specific_decl_iterator< FieldDecl > field_iterator
Definition Decl.h:4542
field_iterator field_begin() const
Definition Decl.cpp:5277
Encodes a location in the source.
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1802
uint32_t getCodeUnit(size_t i) const
Definition Expr.h:1885
bool isUnion() const
Definition Decl.h:3943
bool isVoidType() const
Definition TypeBase.h:9034
bool isBooleanType() const
Definition TypeBase.h:9171
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition Type.cpp:2254
bool isPackedVectorBoolType(const ASTContext &ctx) const
Definition Type.cpp:420
bool isIncompleteArrayType() const
Definition TypeBase.h:8775
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition Type.cpp:2231
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isArrayType() const
Definition TypeBase.h:8767
CXXRecordDecl * castAsCXXRecordDecl() const
Definition Type.h:36
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9078
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9328
bool isReferenceType() const
Definition TypeBase.h:8692
bool isExtVectorBoolType() const
Definition TypeBase.h:8815
bool isBitIntType() const
Definition TypeBase.h:8943
RecordDecl * castAsRecordDecl() const
Definition Type.h:48
bool isFloatingType() const
Definition Type.cpp:2342
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition Type.cpp:2285
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9261
bool isRecordType() const
Definition TypeBase.h:8795
bool isUnionType() const
Definition Type.cpp:720
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2247
Expr * getSubExpr() const
Definition Expr.h:2288
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Kind getKind() const
Definition Value.h:137
Represents a variable declaration or definition.
Definition Decl.h:926
TLSKind getTLSKind() const
Definition Decl.cpp:2181
APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition Decl.cpp:2588
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2661
const Expr * getInit() const
Definition Decl.h:1383
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition Decl.h:1184
@ TLS_None
Not a TLS variable.
Definition Decl.h:946
Represents a GCC generic vector type.
Definition TypeBase.h:4225
QualType getElementType() const
Definition TypeBase.h:4239
bool isEmptyRecordForLayout(const ASTContext &Context, QualType T)
isEmptyRecordForLayout - Return true iff a structure contains only empty base classes (per isEmptyRec...
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD)
isEmptyFieldForLayout - Return true iff the field is "empty", that is, either a zero-width bit-field ...
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
uint32_t Literal
Literals are represented as positive integers.
Definition CNFFormula.h:35
bool Const(InterpState &S, CodePtr OpPC, const T &Arg)
Definition Interp.h:1401
bool GE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1358
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ Success
Annotation was successful.
Definition Parser.h:65
@ Finalize
'finalize' clause, allowed on 'exit data' directive.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
@ SD_Static
Static storage duration.
Definition Specifiers.h:343
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ Type
The name was classified as a type.
Definition Sema.h:564
LangAS
Defines the address space values used by the address space qualifier of QualType.
bool declaresSameEntity(const Decl *D1, const Decl *D2)
Determine whether two declarations declare the same entity.
Definition DeclBase.h:1301
U cast(CodeGen::Address addr)
Definition Address.h:327
@ ArrayBound
Array bound in array declarator or new-expression.
Definition Sema.h:844
unsigned long uint64_t
unsigned Size
The total size of the bit-field, in bits.
llvm::IntegerType * CharTy
char
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:650
Expr * Value
The value of the dictionary element.
Definition ExprObjC.h:300
Expr * Key
The key for the dictionary element.
Definition ExprObjC.h:297