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