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