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