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