clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name CGExprConstant.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model static -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/../include -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -I /usr/src/gnu/usr.bin/clang/libclangCodeGen/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libclangCodeGen/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -stack-protector 2 -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libclangCodeGen/../../../llvm/clang/lib/CodeGen/CGExprConstant.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
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 | |
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.CharTy; |
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.CharTy, ZeroSize.getQuantity()); |
69 | return llvm::ConstantAggregateZero::get(Ty); |
70 | } |
71 | }; |
72 | |
73 | |
74 | |
75 | class ConstantAggregateBuilder : private ConstantAggregateBuilderUtils { |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | llvm::SmallVector<llvm::Constant*, 32> Elems; |
85 | llvm::SmallVector<CharUnits, 32> Offsets; |
86 | |
87 | |
88 | |
89 | |
90 | CharUnits Size = CharUnits::Zero(); |
91 | |
92 | |
93 | |
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 | |
111 | |
112 | |
113 | |
114 | |
115 | bool add(llvm::Constant *C, CharUnits Offset, bool AllowOverwrite); |
116 | |
117 | |
118 | bool addBits(llvm::APInt Bits, uint64_t OffsetInBits, bool AllowOverwrite); |
119 | |
120 | |
121 | |
122 | void condense(CharUnits Offset, llvm::Type *DesiredTy); |
123 | |
124 | |
125 | |
126 | |
127 | |
128 | |
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 | |
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 | |
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 | |
186 | |
187 | unsigned OffsetWithinChar = OffsetInBits % CharWidth; |
188 | |
189 | |
190 | |
191 | for (CharUnits OffsetInChars = |
192 | Context.toCharUnitsFromBits(OffsetInBits - OffsetWithinChar); |
193 | ; ++OffsetInChars) { |
194 | |
195 | unsigned WantedBits = |
196 | std::min((uint64_t)Bits.getBitWidth(), CharWidth - OffsetWithinChar); |
197 | |
198 | |
199 | |
200 | llvm::APInt BitsThisChar = Bits; |
201 | if (BitsThisChar.getBitWidth() < CharWidth) |
202 | BitsThisChar = BitsThisChar.zext(CharWidth); |
203 | if (CGM.getDataLayout().isBigEndian()) { |
204 | |
205 | |
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 | |
219 | add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar), |
220 | OffsetInChars, AllowOverwrite); |
221 | } else { |
222 | |
223 | |
224 | |
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 | |
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 | |
248 | add(llvm::ConstantInt::get(CGM.getLLVMContext(), BitsThisChar), |
249 | OffsetInChars, true); |
250 | } else { |
251 | llvm::Constant *&ToUpdate = Elems[*FirstElemToUpdate]; |
252 | |
253 | |
254 | auto *CI = dyn_cast<llvm::ConstantInt>(ToUpdate); |
255 | if (!CI) |
256 | return false; |
257 | |
258 | |
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 | |
268 | if (WantedBits == Bits.getBitWidth()) |
269 | break; |
270 | |
271 | |
272 | if (!CGM.getDataLayout().isBigEndian()) |
273 | Bits.lshrInPlace(WantedBits); |
274 | Bits = Bits.trunc(Bits.getBitWidth() - WantedBits); |
275 | |
276 | |
277 | OffsetWithinChar = 0; |
278 | } |
279 | |
280 | return true; |
281 | } |
282 | |
283 | |
284 | |
285 | |
286 | |
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 | |
297 | size_t LastAtOrBeforePosIndex = FirstAfterPos - Offsets.begin() - 1; |
298 | if (Offsets[LastAtOrBeforePosIndex] == Pos) |
299 | return LastAtOrBeforePosIndex; |
300 | |
301 | |
302 | if (Offsets[LastAtOrBeforePosIndex] + |
303 | getSize(Elems[LastAtOrBeforePosIndex]) <= Pos) |
304 | return LastAtOrBeforePosIndex + 1; |
305 | |
306 | |
307 | if (!split(LastAtOrBeforePosIndex, Pos)) |
308 | return None; |
309 | } |
310 | } |
311 | |
312 | |
313 | |
314 | |
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 | |
322 | |
323 | replace(Elems, Index, Index + 1, |
324 | llvm::map_range(llvm::seq(0u, CA->getNumOperands()), |
325 | [&](unsigned Op) { return CA->getOperand(Op); })); |
326 | if (isa<llvm::ArrayType>(CA->getType()) || |
327 | isa<llvm::VectorType>(CA->getType())) { |
328 | |
329 | llvm::Type *ElemTy = |
330 | llvm::GetElementPtrInst::getTypeAtIndex(CA->getType(), (uint64_t)0); |
331 | CharUnits ElemSize = getSize(ElemTy); |
332 | replace( |
333 | Offsets, Index, Index + 1, |
334 | llvm::map_range(llvm::seq(0u, CA->getNumOperands()), |
335 | [&](unsigned Op) { return Offset + Op * ElemSize; })); |
336 | } else { |
337 | |
338 | auto *ST = cast<llvm::StructType>(CA->getType()); |
339 | const llvm::StructLayout *Layout = |
340 | CGM.getDataLayout().getStructLayout(ST); |
341 | replace(Offsets, Index, Index + 1, |
342 | llvm::map_range( |
343 | llvm::seq(0u, CA->getNumOperands()), [&](unsigned Op) { |
344 | return Offset + CharUnits::fromQuantity( |
345 | Layout->getElementOffset(Op)); |
346 | })); |
347 | } |
348 | return true; |
349 | } |
350 | |
351 | if (auto *CDS = dyn_cast<llvm::ConstantDataSequential>(C)) { |
352 | |
353 | |
354 | |
355 | CharUnits ElemSize = getSize(CDS->getElementType()); |
356 | replace(Elems, Index, Index + 1, |
357 | llvm::map_range(llvm::seq(0u, CDS->getNumElements()), |
358 | [&](unsigned Elem) { |
359 | return CDS->getElementAsConstant(Elem); |
360 | })); |
361 | replace(Offsets, Index, Index + 1, |
362 | llvm::map_range( |
363 | llvm::seq(0u, CDS->getNumElements()), |
364 | [&](unsigned Elem) { return Offset + Elem * ElemSize; })); |
365 | return true; |
366 | } |
367 | |
368 | if (isa<llvm::ConstantAggregateZero>(C)) { |
369 | |
370 | CharUnits ElemSize = getSize(C); |
371 | assert(Hint > Offset && Hint < Offset + ElemSize && "nothing to split"); |
372 | replace(Elems, Index, Index + 1, |
373 | {getZeroes(Hint - Offset), getZeroes(Offset + ElemSize - Hint)}); |
374 | replace(Offsets, Index, Index + 1, {Offset, Hint}); |
375 | return true; |
376 | } |
377 | |
378 | if (isa<llvm::UndefValue>(C)) { |
379 | |
380 | replace(Elems, Index, Index + 1, {}); |
381 | replace(Offsets, Index, Index + 1, {}); |
382 | return true; |
383 | } |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | return false; |
390 | } |
391 | |
392 | static llvm::Constant * |
393 | EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType, |
394 | llvm::Type *CommonElementType, unsigned ArrayBound, |
395 | SmallVectorImpl<llvm::Constant *> &Elements, |
396 | llvm::Constant *Filler); |
397 | |
398 | llvm::Constant *ConstantAggregateBuilder::buildFrom( |
399 | CodeGenModule &CGM, ArrayRef<llvm::Constant *> Elems, |
400 | ArrayRef<CharUnits> Offsets, CharUnits StartOffset, CharUnits Size, |
401 | bool NaturalLayout, llvm::Type *DesiredTy, bool AllowOversized) { |
402 | ConstantAggregateBuilderUtils Utils(CGM); |
403 | |
404 | if (Elems.empty()) |
405 | return llvm::UndefValue::get(DesiredTy); |
406 | |
407 | auto Offset = [&](size_t I) { return Offsets[I] - StartOffset; }; |
408 | |
409 | |
410 | |
411 | if (llvm::ArrayType *ATy = dyn_cast<llvm::ArrayType>(DesiredTy)) { |
412 | assert(!AllowOversized && "oversized array emission not supported"); |
413 | |
414 | bool CanEmitArray = true; |
415 | llvm::Type *CommonType = Elems[0]->getType(); |
416 | llvm::Constant *Filler = llvm::Constant::getNullValue(CommonType); |
417 | CharUnits ElemSize = Utils.getSize(ATy->getElementType()); |
418 | SmallVector<llvm::Constant*, 32> ArrayElements; |
419 | for (size_t I = 0; I != Elems.size(); ++I) { |
420 | |
421 | if (Elems[I]->isNullValue()) |
422 | continue; |
423 | |
424 | |
425 | if (Elems[I]->getType() != CommonType || |
426 | Offset(I) % ElemSize != 0) { |
427 | CanEmitArray = false; |
428 | break; |
429 | } |
430 | ArrayElements.resize(Offset(I) / ElemSize + 1, Filler); |
431 | ArrayElements.back() = Elems[I]; |
432 | } |
433 | |
434 | if (CanEmitArray) { |
435 | return EmitArrayConstant(CGM, ATy, CommonType, ATy->getNumElements(), |
436 | ArrayElements, Filler); |
437 | } |
438 | |
439 | |
440 | } |
441 | |
442 | CharUnits DesiredSize = Utils.getSize(DesiredTy); |
443 | CharUnits Align = CharUnits::One(); |
444 | for (llvm::Constant *C : Elems) |
445 | Align = std::max(Align, Utils.getAlignment(C)); |
446 | CharUnits AlignedSize = Size.alignTo(Align); |
447 | |
448 | bool Packed = false; |
449 | ArrayRef<llvm::Constant*> UnpackedElems = Elems; |
450 | llvm::SmallVector<llvm::Constant*, 32> UnpackedElemStorage; |
451 | if ((DesiredSize < AlignedSize && !AllowOversized) || |
452 | DesiredSize.alignTo(Align) != DesiredSize) { |
453 | |
454 | NaturalLayout = false; |
455 | Packed = true; |
456 | } else if (DesiredSize > AlignedSize) { |
457 | |
458 | UnpackedElemStorage.assign(Elems.begin(), Elems.end()); |
459 | UnpackedElemStorage.push_back(Utils.getPadding(DesiredSize - Size)); |
460 | UnpackedElems = UnpackedElemStorage; |
461 | } |
462 | |
463 | |
464 | |
465 | |
466 | llvm::SmallVector<llvm::Constant*, 32> PackedElems; |
467 | if (!NaturalLayout) { |
468 | CharUnits SizeSoFar = CharUnits::Zero(); |
469 | for (size_t I = 0; I != Elems.size(); ++I) { |
470 | CharUnits Align = Utils.getAlignment(Elems[I]); |
471 | CharUnits NaturalOffset = SizeSoFar.alignTo(Align); |
472 | CharUnits DesiredOffset = Offset(I); |
473 | assert(DesiredOffset >= SizeSoFar && "elements out of order"); |
474 | |
475 | if (DesiredOffset != NaturalOffset) |
476 | Packed = true; |
477 | if (DesiredOffset != SizeSoFar) |
478 | PackedElems.push_back(Utils.getPadding(DesiredOffset - SizeSoFar)); |
479 | PackedElems.push_back(Elems[I]); |
480 | SizeSoFar = DesiredOffset + Utils.getSize(Elems[I]); |
481 | } |
482 | |
483 | |
484 | if (Packed) { |
485 | assert((SizeSoFar <= DesiredSize || AllowOversized) && |
486 | "requested size is too small for contents"); |
487 | if (SizeSoFar < DesiredSize) |
488 | PackedElems.push_back(Utils.getPadding(DesiredSize - SizeSoFar)); |
489 | } |
490 | } |
491 | |
492 | llvm::StructType *STy = llvm::ConstantStruct::getTypeForElements( |
493 | CGM.getLLVMContext(), Packed ? PackedElems : UnpackedElems, Packed); |
494 | |
495 | |
496 | |
497 | if (llvm::StructType *DesiredSTy = dyn_cast<llvm::StructType>(DesiredTy)) { |
498 | if (DesiredSTy->isLayoutIdentical(STy)) |
499 | STy = DesiredSTy; |
500 | } |
501 | |
502 | return llvm::ConstantStruct::get(STy, Packed ? PackedElems : UnpackedElems); |
503 | } |
504 | |
505 | void ConstantAggregateBuilder::condense(CharUnits Offset, |
506 | llvm::Type *DesiredTy) { |
507 | CharUnits Size = getSize(DesiredTy); |
508 | |
509 | llvm::Optional<size_t> FirstElemToReplace = splitAt(Offset); |
510 | if (!FirstElemToReplace) |
511 | return; |
512 | size_t First = *FirstElemToReplace; |
513 | |
514 | llvm::Optional<size_t> LastElemToReplace = splitAt(Offset + Size); |
515 | if (!LastElemToReplace) |
516 | return; |
517 | size_t Last = *LastElemToReplace; |
518 | |
519 | size_t Length = Last - First; |
520 | if (Length == 0) |
521 | return; |
522 | |
523 | if (Length == 1 && Offsets[First] == Offset && |
524 | getSize(Elems[First]) == Size) { |
525 | |
526 | |
527 | auto *STy = dyn_cast<llvm::StructType>(DesiredTy); |
528 | if (STy && STy->getNumElements() == 1 && |
529 | STy->getElementType(0) == Elems[First]->getType()) |
530 | Elems[First] = llvm::ConstantStruct::get(STy, Elems[First]); |
531 | return; |
532 | } |
533 | |
534 | llvm::Constant *Replacement = buildFrom( |
535 | CGM, makeArrayRef(Elems).slice(First, Length), |
536 | makeArrayRef(Offsets).slice(First, Length), Offset, getSize(DesiredTy), |
537 | false, DesiredTy, false); |
538 | replace(Elems, First, Last, {Replacement}); |
539 | replace(Offsets, First, Last, {Offset}); |
540 | } |
541 | |
542 | |
543 | |
544 | |
545 | |
546 | class ConstStructBuilder { |
547 | CodeGenModule &CGM; |
548 | ConstantEmitter &Emitter; |
549 | ConstantAggregateBuilder &Builder; |
550 | CharUnits StartOffset; |
551 | |
552 | public: |
553 | static llvm::Constant *BuildStruct(ConstantEmitter &Emitter, |
554 | InitListExpr *ILE, QualType StructTy); |
555 | static llvm::Constant *BuildStruct(ConstantEmitter &Emitter, |
556 | const APValue &Value, QualType ValTy); |
557 | static bool UpdateStruct(ConstantEmitter &Emitter, |
558 | ConstantAggregateBuilder &Const, CharUnits Offset, |
559 | InitListExpr *Updater); |
560 | |
561 | private: |
562 | ConstStructBuilder(ConstantEmitter &Emitter, |
563 | ConstantAggregateBuilder &Builder, CharUnits StartOffset) |
564 | : CGM(Emitter.CGM), Emitter(Emitter), Builder(Builder), |
565 | StartOffset(StartOffset) {} |
566 | |
567 | bool AppendField(const FieldDecl *Field, uint64_t FieldOffset, |
568 | llvm::Constant *InitExpr, bool AllowOverwrite = false); |
569 | |
570 | bool AppendBytes(CharUnits FieldOffsetInChars, llvm::Constant *InitCst, |
571 | bool AllowOverwrite = false); |
572 | |
573 | bool AppendBitField(const FieldDecl *Field, uint64_t FieldOffset, |
574 | llvm::ConstantInt *InitExpr, bool AllowOverwrite = false); |
575 | |
576 | bool Build(InitListExpr *ILE, bool AllowOverwrite); |
577 | bool Build(const APValue &Val, const RecordDecl *RD, bool IsPrimaryBase, |
578 | const CXXRecordDecl *VTableClass, CharUnits BaseOffset); |
579 | llvm::Constant *Finalize(QualType Ty); |
580 | }; |
581 | |
582 | bool ConstStructBuilder::AppendField( |
583 | const FieldDecl *Field, uint64_t FieldOffset, llvm::Constant *InitCst, |
584 | bool AllowOverwrite) { |
585 | const ASTContext &Context = CGM.getContext(); |
586 | |
587 | CharUnits FieldOffsetInChars = Context.toCharUnitsFromBits(FieldOffset); |
588 | |
589 | return AppendBytes(FieldOffsetInChars, InitCst, AllowOverwrite); |
590 | } |
591 | |
592 | bool ConstStructBuilder::AppendBytes(CharUnits FieldOffsetInChars, |
593 | llvm::Constant *InitCst, |
594 | bool AllowOverwrite) { |
595 | return Builder.add(InitCst, StartOffset + FieldOffsetInChars, AllowOverwrite); |
596 | } |
597 | |
598 | bool ConstStructBuilder::AppendBitField( |
599 | const FieldDecl *Field, uint64_t FieldOffset, llvm::ConstantInt *CI, |
600 | bool AllowOverwrite) { |
601 | const CGRecordLayout &RL = |
602 | CGM.getTypes().getCGRecordLayout(Field->getParent()); |
603 | const CGBitFieldInfo &Info = RL.getBitFieldInfo(Field); |
604 | llvm::APInt FieldValue = CI->getValue(); |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | if (Info.Size > FieldValue.getBitWidth()) |
611 | FieldValue = FieldValue.zext(Info.Size); |
612 | |
613 | |
614 | if (Info.Size < FieldValue.getBitWidth()) |
615 | FieldValue = FieldValue.trunc(Info.Size); |
616 | |
617 | return Builder.addBits(FieldValue, |
618 | CGM.getContext().toBits(StartOffset) + FieldOffset, |
619 | AllowOverwrite); |
620 | } |
621 | |
622 | static bool EmitDesignatedInitUpdater(ConstantEmitter &Emitter, |
623 | ConstantAggregateBuilder &Const, |
624 | CharUnits Offset, QualType Type, |
625 | InitListExpr *Updater) { |
626 | if (Type->isRecordType()) |
627 | return ConstStructBuilder::UpdateStruct(Emitter, Const, Offset, Updater); |
628 | |
629 | auto CAT = Emitter.CGM.getContext().getAsConstantArrayType(Type); |
630 | if (!CAT) |
631 | return false; |
632 | QualType ElemType = CAT->getElementType(); |
633 | CharUnits ElemSize = Emitter.CGM.getContext().getTypeSizeInChars(ElemType); |
634 | llvm::Type *ElemTy = Emitter.CGM.getTypes().ConvertTypeForMem(ElemType); |
635 | |
636 | llvm::Constant *FillC = nullptr; |
637 | if (Expr *Filler = Updater->getArrayFiller()) { |
638 | if (!isa<NoInitExpr>(Filler)) { |
639 | FillC = Emitter.tryEmitAbstractForMemory(Filler, ElemType); |
640 | if (!FillC) |
641 | return false; |
642 | } |
643 | } |
644 | |
645 | unsigned NumElementsToUpdate = |
646 | FillC ? CAT->getSize().getZExtValue() : Updater->getNumInits(); |
647 | for (unsigned I = 0; I != NumElementsToUpdate; ++I, Offset += ElemSize) { |
648 | Expr *Init = nullptr; |
649 | if (I < Updater->getNumInits()) |
650 | Init = Updater->getInit(I); |
651 | |
652 | if (!Init && FillC) { |
653 | if (!Const.add(FillC, Offset, true)) |
654 | return false; |
655 | } else if (!Init || isa<NoInitExpr>(Init)) { |
656 | continue; |
657 | } else if (InitListExpr *ChildILE = dyn_cast<InitListExpr>(Init)) { |
658 | if (!EmitDesignatedInitUpdater(Emitter, Const, Offset, ElemType, |
659 | ChildILE)) |
660 | return false; |
661 | |
662 | Const.condense(Offset, ElemTy); |
663 | } else { |
664 | llvm::Constant *Val = Emitter.tryEmitPrivateForMemory(Init, ElemType); |
665 | if (!Const.add(Val, Offset, true)) |
666 | return false; |
667 | } |
668 | } |
669 | |
670 | return true; |
671 | } |
672 | |
673 | bool ConstStructBuilder::Build(InitListExpr *ILE, bool AllowOverwrite) { |
674 | RecordDecl *RD = ILE->getType()->castAs<RecordType>()->getDecl(); |
675 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
676 | |
677 | unsigned FieldNo = -1; |
678 | unsigned ElementNo = 0; |
679 | |
680 | |
681 | |
682 | |
683 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
684 | if (CXXRD->getNumBases()) |
685 | return false; |
686 | |
687 | for (FieldDecl *Field : RD->fields()) { |
688 | ++FieldNo; |
689 | |
690 | |
691 | if (RD->isUnion() && |
692 | !declaresSameEntity(ILE->getInitializedFieldInUnion(), Field)) |
693 | continue; |
694 | |
695 | |
696 | if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext())) |
697 | continue; |
698 | |
699 | |
700 | |
701 | Expr *Init = nullptr; |
702 | if (ElementNo < ILE->getNumInits()) |
703 | Init = ILE->getInit(ElementNo++); |
704 | if (Init && isa<NoInitExpr>(Init)) |
705 | continue; |
706 | |
707 | |
708 | |
709 | |
710 | if (AllowOverwrite && |
711 | (Field->getType()->isArrayType() || Field->getType()->isRecordType())) { |
712 | if (auto *SubILE = dyn_cast<InitListExpr>(Init)) { |
713 | CharUnits Offset = CGM.getContext().toCharUnitsFromBits( |
714 | Layout.getFieldOffset(FieldNo)); |
715 | if (!EmitDesignatedInitUpdater(Emitter, Builder, StartOffset + Offset, |
716 | Field->getType(), SubILE)) |
717 | return false; |
718 | |
719 | |
720 | Builder.condense(StartOffset + Offset, |
721 | CGM.getTypes().ConvertTypeForMem(Field->getType())); |
722 | continue; |
723 | } |
724 | } |
725 | |
726 | llvm::Constant *EltInit = |
727 | Init ? Emitter.tryEmitPrivateForMemory(Init, Field->getType()) |
728 | : Emitter.emitNullForMemory(Field->getType()); |
729 | if (!EltInit) |
730 | return false; |
731 | |
732 | if (!Field->isBitField()) { |
733 | |
734 | if (!AppendField(Field, Layout.getFieldOffset(FieldNo), EltInit, |
735 | AllowOverwrite)) |
736 | return false; |
737 | |
738 | |
739 | if (Field->hasAttr<NoUniqueAddressAttr>()) |
740 | AllowOverwrite = true; |
741 | } else { |
742 | |
743 | if (auto *CI = dyn_cast<llvm::ConstantInt>(EltInit)) { |
744 | if (!AppendBitField(Field, Layout.getFieldOffset(FieldNo), CI, |
745 | AllowOverwrite)) |
746 | return false; |
747 | } else { |
748 | |
749 | |
750 | return false; |
751 | } |
752 | } |
753 | } |
754 | |
755 | return true; |
756 | } |
757 | |
758 | namespace { |
759 | struct BaseInfo { |
760 | BaseInfo(const CXXRecordDecl *Decl, CharUnits Offset, unsigned Index) |
761 | : Decl(Decl), Offset(Offset), Index(Index) { |
762 | } |
763 | |
764 | const CXXRecordDecl *Decl; |
765 | CharUnits Offset; |
766 | unsigned Index; |
767 | |
768 | bool operator<(const BaseInfo &O) const { return Offset < O.Offset; } |
769 | }; |
770 | } |
771 | |
772 | bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD, |
773 | bool IsPrimaryBase, |
774 | const CXXRecordDecl *VTableClass, |
775 | CharUnits Offset) { |
776 | const ASTRecordLayout &Layout = CGM.getContext().getASTRecordLayout(RD); |
777 | |
778 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
779 | |
780 | if (Layout.hasOwnVFPtr()) { |
781 | llvm::Constant *VTableAddressPoint = |
782 | CGM.getCXXABI().getVTableAddressPointForConstExpr( |
783 | BaseSubobject(CD, Offset), VTableClass); |
784 | if (!AppendBytes(Offset, VTableAddressPoint)) |
785 | return false; |
786 | } |
787 | |
788 | |
789 | |
790 | SmallVector<BaseInfo, 8> Bases; |
791 | Bases.reserve(CD->getNumBases()); |
792 | unsigned BaseNo = 0; |
793 | for (CXXRecordDecl::base_class_const_iterator Base = CD->bases_begin(), |
794 | BaseEnd = CD->bases_end(); Base != BaseEnd; ++Base, ++BaseNo) { |
795 | assert(!Base->isVirtual() && "should not have virtual bases here"); |
796 | const CXXRecordDecl *BD = Base->getType()->getAsCXXRecordDecl(); |
797 | CharUnits BaseOffset = Layout.getBaseClassOffset(BD); |
798 | Bases.push_back(BaseInfo(BD, BaseOffset, BaseNo)); |
799 | } |
800 | llvm::stable_sort(Bases); |
801 | |
802 | for (unsigned I = 0, N = Bases.size(); I != N; ++I) { |
803 | BaseInfo &Base = Bases[I]; |
804 | |
805 | bool IsPrimaryBase = Layout.getPrimaryBase() == Base.Decl; |
806 | Build(Val.getStructBase(Base.Index), Base.Decl, IsPrimaryBase, |
807 | VTableClass, Offset + Base.Offset); |
808 | } |
809 | } |
810 | |
811 | unsigned FieldNo = 0; |
812 | uint64_t OffsetBits = CGM.getContext().toBits(Offset); |
813 | |
814 | bool AllowOverwrite = false; |
815 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
816 | FieldEnd = RD->field_end(); Field != FieldEnd; ++Field, ++FieldNo) { |
817 | |
818 | if (RD->isUnion() && !declaresSameEntity(Val.getUnionField(), *Field)) |
819 | continue; |
820 | |
821 | |
822 | if (Field->isUnnamedBitfield() || Field->isZeroSize(CGM.getContext())) |
823 | continue; |
824 | |
825 | |
826 | const APValue &FieldValue = |
827 | RD->isUnion() ? Val.getUnionValue() : Val.getStructField(FieldNo); |
828 | llvm::Constant *EltInit = |
829 | Emitter.tryEmitPrivateForMemory(FieldValue, Field->getType()); |
830 | if (!EltInit) |
831 | return false; |
832 | |
833 | if (!Field->isBitField()) { |
834 | |
835 | if (!AppendField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, |
836 | EltInit, AllowOverwrite)) |
837 | return false; |
838 | |
839 | |
840 | if (Field->hasAttr<NoUniqueAddressAttr>()) |
841 | AllowOverwrite = true; |
842 | } else { |
843 | |
844 | if (!AppendBitField(*Field, Layout.getFieldOffset(FieldNo) + OffsetBits, |
845 | cast<llvm::ConstantInt>(EltInit), AllowOverwrite)) |
846 | return false; |
847 | } |
848 | } |
849 | |
850 | return true; |
851 | } |
852 | |
853 | llvm::Constant *ConstStructBuilder::Finalize(QualType Type) { |
854 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
855 | llvm::Type *ValTy = CGM.getTypes().ConvertType(Type); |
856 | return Builder.build(ValTy, RD->hasFlexibleArrayMember()); |
857 | } |
858 | |
859 | llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter, |
860 | InitListExpr *ILE, |
861 | QualType ValTy) { |
862 | ConstantAggregateBuilder Const(Emitter.CGM); |
863 | ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero()); |
864 | |
865 | if (!Builder.Build(ILE, false)) |
866 | return nullptr; |
867 | |
868 | return Builder.Finalize(ValTy); |
869 | } |
870 | |
871 | llvm::Constant *ConstStructBuilder::BuildStruct(ConstantEmitter &Emitter, |
872 | const APValue &Val, |
873 | QualType ValTy) { |
874 | ConstantAggregateBuilder Const(Emitter.CGM); |
875 | ConstStructBuilder Builder(Emitter, Const, CharUnits::Zero()); |
876 | |
877 | const RecordDecl *RD = ValTy->castAs<RecordType>()->getDecl(); |
878 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
879 | if (!Builder.Build(Val, RD, false, CD, CharUnits::Zero())) |
880 | return nullptr; |
881 | |
882 | return Builder.Finalize(ValTy); |
883 | } |
884 | |
885 | bool ConstStructBuilder::UpdateStruct(ConstantEmitter &Emitter, |
886 | ConstantAggregateBuilder &Const, |
887 | CharUnits Offset, InitListExpr *Updater) { |
888 | return ConstStructBuilder(Emitter, Const, Offset) |
889 | .Build(Updater, true); |
890 | } |
891 | |
892 | |
893 | |
894 | |
895 | |
896 | static ConstantAddress tryEmitGlobalCompoundLiteral(CodeGenModule &CGM, |
897 | CodeGenFunction *CGF, |
898 | const CompoundLiteralExpr *E) { |
899 | CharUnits Align = CGM.getContext().getTypeAlignInChars(E->getType()); |
900 | if (llvm::GlobalVariable *Addr = |
| |
| |
901 | CGM.getAddrOfConstantCompoundLiteralIfEmitted(E)) |
902 | return ConstantAddress(Addr, Align); |
903 | |
904 | LangAS addressSpace = E->getType().getAddressSpace(); |
905 | |
906 | ConstantEmitter emitter(CGM, CGF); |
907 | llvm::Constant *C = emitter.tryEmitForInitializer(E->getInitializer(), |
| 4 | | Calling 'ConstantEmitter::tryEmitForInitializer' | |
|
908 | addressSpace, E->getType()); |
909 | if (!C) { |
910 | assert(!E->isFileScope() && |
911 | "file-scope compound literal did not have constant initializer!"); |
912 | return ConstantAddress::invalid(); |
913 | } |
914 | |
915 | auto GV = new llvm::GlobalVariable(CGM.getModule(), C->getType(), |
916 | CGM.isTypeConstant(E->getType(), true), |
917 | llvm::GlobalValue::InternalLinkage, |
918 | C, ".compoundliteral", nullptr, |
919 | llvm::GlobalVariable::NotThreadLocal, |
920 | CGM.getContext().getTargetAddressSpace(addressSpace)); |
921 | emitter.finalize(GV); |
922 | GV->setAlignment(Align.getAsAlign()); |
923 | CGM.setAddrOfConstantCompoundLiteral(E, GV); |
924 | return ConstantAddress(GV, Align); |
925 | } |
926 | |
927 | static llvm::Constant * |
928 | EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType, |
929 | llvm::Type *CommonElementType, unsigned ArrayBound, |
930 | SmallVectorImpl<llvm::Constant *> &Elements, |
931 | llvm::Constant *Filler) { |
932 | |
933 | unsigned NonzeroLength = ArrayBound; |
934 | if (Elements.size() < NonzeroLength && Filler->isNullValue()) |
| 34 | | Assuming the condition is false | |
|
935 | NonzeroLength = Elements.size(); |
936 | if (NonzeroLength == Elements.size()) { |
| 35 | | Assuming the condition is false | |
|
| |
937 | while (NonzeroLength > 0 && Elements[NonzeroLength - 1]->isNullValue()) |
938 | --NonzeroLength; |
939 | } |
940 | |
941 | if (NonzeroLength == 0) |
| |
942 | return llvm::ConstantAggregateZero::get(DesiredType); |
943 | |
944 | |
945 | unsigned TrailingZeroes = ArrayBound - NonzeroLength; |
946 | if (TrailingZeroes >= 8) { |
| |
947 | assert(Elements.size() >= NonzeroLength && |
948 | "missing initializer for non-zero element"); |
949 | |
950 | |
951 | |
952 | if (CommonElementType && NonzeroLength >= 8) { |
953 | llvm::Constant *Initial = llvm::ConstantArray::get( |
954 | llvm::ArrayType::get(CommonElementType, NonzeroLength), |
955 | makeArrayRef(Elements).take_front(NonzeroLength)); |
956 | Elements.resize(2); |
957 | Elements[0] = Initial; |
958 | } else { |
959 | Elements.resize(NonzeroLength + 1); |
960 | } |
961 | |
962 | auto *FillerType = |
963 | CommonElementType ? CommonElementType : DesiredType->getElementType(); |
964 | FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes); |
965 | Elements.back() = llvm::ConstantAggregateZero::get(FillerType); |
966 | CommonElementType = nullptr; |
967 | } else if (Elements.size() != ArrayBound) { |
| |
968 | |
969 | Elements.resize(ArrayBound, Filler); |
970 | if (Filler->getType() != CommonElementType) |
| 40 | | Called C++ object pointer is null |
|
971 | CommonElementType = nullptr; |
972 | } |
973 | |
974 | |
975 | if (CommonElementType) |
976 | return llvm::ConstantArray::get( |
977 | llvm::ArrayType::get(CommonElementType, ArrayBound), Elements); |
978 | |
979 | |
980 | llvm::SmallVector<llvm::Type *, 16> Types; |
981 | Types.reserve(Elements.size()); |
982 | for (llvm::Constant *Elt : Elements) |
983 | Types.push_back(Elt->getType()); |
984 | llvm::StructType *SType = |
985 | llvm::StructType::get(CGM.getLLVMContext(), Types, true); |
986 | return llvm::ConstantStruct::get(SType, Elements); |
987 | } |
988 | |
989 | |
990 | |
991 | |
992 | |
993 | |
994 | |
995 | class ConstExprEmitter : |
996 | public StmtVisitor<ConstExprEmitter, llvm::Constant*, QualType> { |
997 | CodeGenModule &CGM; |
998 | ConstantEmitter &Emitter; |
999 | llvm::LLVMContext &VMContext; |
1000 | public: |
1001 | ConstExprEmitter(ConstantEmitter &emitter) |
1002 | : CGM(emitter.CGM), Emitter(emitter), VMContext(CGM.getLLVMContext()) { |
1003 | } |
1004 | |
1005 | |
1006 | |
1007 | |
1008 | |
1009 | llvm::Constant *VisitStmt(Stmt *S, QualType T) { |
1010 | return nullptr; |
1011 | } |
1012 | |
1013 | llvm::Constant *VisitConstantExpr(ConstantExpr *CE, QualType T) { |
1014 | if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(CE)) |
1015 | return Result; |
1016 | return Visit(CE->getSubExpr(), T); |
1017 | } |
1018 | |
1019 | llvm::Constant *VisitParenExpr(ParenExpr *PE, QualType T) { |
1020 | return Visit(PE->getSubExpr(), T); |
1021 | } |
1022 | |
1023 | llvm::Constant * |
1024 | VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *PE, |
1025 | QualType T) { |
1026 | return Visit(PE->getReplacement(), T); |
1027 | } |
1028 | |
1029 | llvm::Constant *VisitGenericSelectionExpr(GenericSelectionExpr *GE, |
1030 | QualType T) { |
1031 | return Visit(GE->getResultExpr(), T); |
1032 | } |
1033 | |
1034 | llvm::Constant *VisitChooseExpr(ChooseExpr *CE, QualType T) { |
1035 | return Visit(CE->getChosenSubExpr(), T); |
1036 | } |
1037 | |
1038 | llvm::Constant *VisitCompoundLiteralExpr(CompoundLiteralExpr *E, QualType T) { |
1039 | return Visit(E->getInitializer(), T); |
1040 | } |
1041 | |
1042 | llvm::Constant *VisitCastExpr(CastExpr *E, QualType destType) { |
1043 | if (const auto *ECE = dyn_cast<ExplicitCastExpr>(E)) |
1044 | CGM.EmitExplicitCastExprType(ECE, Emitter.CGF); |
1045 | Expr *subExpr = E->getSubExpr(); |
1046 | |
1047 | switch (E->getCastKind()) { |
1048 | case CK_ToUnion: { |
1049 | |
1050 | assert(E->getType()->isUnionType() && |
1051 | "Destination type is not union type!"); |
1052 | |
1053 | auto field = E->getTargetUnionField(); |
1054 | |
1055 | auto C = Emitter.tryEmitPrivateForMemory(subExpr, field->getType()); |
1056 | if (!C) return nullptr; |
1057 | |
1058 | auto destTy = ConvertType(destType); |
1059 | if (C->getType() == destTy) return C; |
1060 | |
1061 | |
1062 | |
1063 | SmallVector<llvm::Constant*, 2> Elts; |
1064 | SmallVector<llvm::Type*, 2> Types; |
1065 | Elts.push_back(C); |
1066 | Types.push_back(C->getType()); |
1067 | unsigned CurSize = CGM.getDataLayout().getTypeAllocSize(C->getType()); |
1068 | unsigned TotalSize = CGM.getDataLayout().getTypeAllocSize(destTy); |
1069 | |
1070 | assert(CurSize <= TotalSize && "Union size mismatch!"); |
1071 | if (unsigned NumPadBytes = TotalSize - CurSize) { |
1072 | llvm::Type *Ty = CGM.CharTy; |
1073 | if (NumPadBytes > 1) |
1074 | Ty = llvm::ArrayType::get(Ty, NumPadBytes); |
1075 | |
1076 | Elts.push_back(llvm::UndefValue::get(Ty)); |
1077 | Types.push_back(Ty); |
1078 | } |
1079 | |
1080 | llvm::StructType *STy = llvm::StructType::get(VMContext, Types, false); |
1081 | return llvm::ConstantStruct::get(STy, Elts); |
1082 | } |
1083 | |
1084 | case CK_AddressSpaceConversion: { |
1085 | auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); |
1086 | if (!C) return nullptr; |
1087 | LangAS destAS = E->getType()->getPointeeType().getAddressSpace(); |
1088 | LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace(); |
1089 | llvm::Type *destTy = ConvertType(E->getType()); |
1090 | return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS, |
1091 | destAS, destTy); |
1092 | } |
1093 | |
1094 | case CK_LValueToRValue: |
1095 | case CK_AtomicToNonAtomic: |
1096 | case CK_NonAtomicToAtomic: |
1097 | case CK_NoOp: |
1098 | case CK_ConstructorConversion: |
1099 | return Visit(subExpr, destType); |
1100 | |
1101 | case CK_IntToOCLSampler: |
1102 | llvm_unreachable("global sampler variables are not generated"); |
1103 | |
1104 | case CK_Dependent: llvm_unreachable("saw dependent cast!"); |
1105 | |
1106 | case CK_BuiltinFnToFnPtr: |
1107 | llvm_unreachable("builtin functions are handled elsewhere"); |
1108 | |
1109 | case CK_ReinterpretMemberPointer: |
1110 | case CK_DerivedToBaseMemberPointer: |
1111 | case CK_BaseToDerivedMemberPointer: { |
1112 | auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); |
1113 | if (!C) return nullptr; |
1114 | return CGM.getCXXABI().EmitMemberPointerConversion(E, C); |
1115 | } |
1116 | |
1117 | |
1118 | case CK_ObjCObjectLValueCast: |
1119 | case CK_ARCProduceObject: |
1120 | case CK_ARCConsumeObject: |
1121 | case CK_ARCReclaimReturnedObject: |
1122 | case CK_ARCExtendBlockObject: |
1123 | case CK_CopyAndAutoreleaseBlockObject: |
1124 | return nullptr; |
1125 | |
1126 | |
1127 | |
1128 | case CK_BitCast: |
1129 | case CK_ToVoid: |
1130 | case CK_Dynamic: |
1131 | case CK_LValueBitCast: |
1132 | case CK_LValueToRValueBitCast: |
1133 | case CK_NullToMemberPointer: |
1134 | case CK_UserDefinedConversion: |
1135 | case CK_CPointerToObjCPointerCast: |
1136 | case CK_BlockPointerToObjCPointerCast: |
1137 | case CK_AnyPointerToBlockPointerCast: |
1138 | case CK_ArrayToPointerDecay: |
1139 | case CK_FunctionToPointerDecay: |
1140 | case CK_BaseToDerived: |
1141 | case CK_DerivedToBase: |
1142 | case CK_UncheckedDerivedToBase: |
1143 | case CK_MemberPointerToBoolean: |
1144 | case CK_VectorSplat: |
1145 | case CK_FloatingRealToComplex: |
1146 | case CK_FloatingComplexToReal: |
1147 | case CK_FloatingComplexToBoolean: |
1148 | case CK_FloatingComplexCast: |
1149 | case CK_FloatingComplexToIntegralComplex: |
1150 | case CK_IntegralRealToComplex: |
1151 | case CK_IntegralComplexToReal: |
1152 | case CK_IntegralComplexToBoolean: |
1153 | case CK_IntegralComplexCast: |
1154 | case CK_IntegralComplexToFloatingComplex: |
1155 | case CK_PointerToIntegral: |
1156 | case CK_PointerToBoolean: |
1157 | case CK_NullToPointer: |
1158 | case CK_IntegralCast: |
1159 | case CK_BooleanToSignedIntegral: |
1160 | case CK_IntegralToPointer: |
1161 | case CK_IntegralToBoolean: |
1162 | case CK_IntegralToFloating: |
1163 | case CK_FloatingToIntegral: |
1164 | case CK_FloatingToBoolean: |
1165 | case CK_FloatingCast: |
1166 | case CK_FloatingToFixedPoint: |
1167 | case CK_FixedPointToFloating: |
1168 | case CK_FixedPointCast: |
1169 | case CK_FixedPointToBoolean: |
1170 | case CK_FixedPointToIntegral: |
1171 | case CK_IntegralToFixedPoint: |
1172 | case CK_ZeroToOCLOpaqueType: |
1173 | case CK_MatrixCast: |
1174 | return nullptr; |
1175 | } |
1176 | llvm_unreachable("Invalid CastKind"); |
1177 | } |
1178 | |
1179 | llvm::Constant *VisitCXXDefaultInitExpr(CXXDefaultInitExpr *DIE, QualType T) { |
1180 | |
1181 | |
1182 | return Visit(DIE->getExpr(), T); |
1183 | } |
1184 | |
1185 | llvm::Constant *VisitExprWithCleanups(ExprWithCleanups *E, QualType T) { |
1186 | return Visit(E->getSubExpr(), T); |
1187 | } |
1188 | |
1189 | llvm::Constant *VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E, |
1190 | QualType T) { |
1191 | return Visit(E->getSubExpr(), T); |
1192 | } |
1193 | |
1194 | llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) { |
1195 | auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType()); |
1196 | assert(CAT && "can't emit array init for non-constant-bound array"); |
1197 | unsigned NumInitElements = ILE->getNumInits(); |
1198 | unsigned NumElements = CAT->getSize().getZExtValue(); |
1199 | |
1200 | |
1201 | |
1202 | unsigned NumInitableElts = std::min(NumInitElements, NumElements); |
1203 | |
1204 | QualType EltType = CAT->getElementType(); |
1205 | |
1206 | |
1207 | llvm::Constant *fillC = nullptr; |
1208 | if (Expr *filler = ILE->getArrayFiller()) { |
1209 | fillC = Emitter.tryEmitAbstractForMemory(filler, EltType); |
1210 | if (!fillC) |
1211 | return nullptr; |
1212 | } |
1213 | |
1214 | |
1215 | SmallVector<llvm::Constant*, 16> Elts; |
1216 | if (fillC && fillC->isNullValue()) |
1217 | Elts.reserve(NumInitableElts + 1); |
1218 | else |
1219 | Elts.reserve(NumElements); |
1220 | |
1221 | llvm::Type *CommonElementType = nullptr; |
1222 | for (unsigned i = 0; i < NumInitableElts; ++i) { |
1223 | Expr *Init = ILE->getInit(i); |
1224 | llvm::Constant *C = Emitter.tryEmitPrivateForMemory(Init, EltType); |
1225 | if (!C) |
1226 | return nullptr; |
1227 | if (i == 0) |
1228 | CommonElementType = C->getType(); |
1229 | else if (C->getType() != CommonElementType) |
1230 | CommonElementType = nullptr; |
1231 | Elts.push_back(C); |
1232 | } |
1233 | |
1234 | llvm::ArrayType *Desired = |
1235 | cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType())); |
1236 | return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts, |
1237 | fillC); |
1238 | } |
1239 | |
1240 | llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) { |
1241 | return ConstStructBuilder::BuildStruct(Emitter, ILE, T); |
1242 | } |
1243 | |
1244 | llvm::Constant *VisitImplicitValueInitExpr(ImplicitValueInitExpr* E, |
1245 | QualType T) { |
1246 | return CGM.EmitNullConstant(T); |
1247 | } |
1248 | |
1249 | llvm::Constant *VisitInitListExpr(InitListExpr *ILE, QualType T) { |
1250 | if (ILE->isTransparent()) |
1251 | return Visit(ILE->getInit(0), T); |
1252 | |
1253 | if (ILE->getType()->isArrayType()) |
1254 | return EmitArrayInitialization(ILE, T); |
1255 | |
1256 | if (ILE->getType()->isRecordType()) |
1257 | return EmitRecordInitialization(ILE, T); |
1258 | |
1259 | return nullptr; |
1260 | } |
1261 | |
1262 | llvm::Constant *VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E, |
1263 | QualType destType) { |
1264 | auto C = Visit(E->getBase(), destType); |
1265 | if (!C) |
1266 | return nullptr; |
1267 | |
1268 | ConstantAggregateBuilder Const(CGM); |
1269 | Const.add(C, CharUnits::Zero(), false); |
1270 | |
1271 | if (!EmitDesignatedInitUpdater(Emitter, Const, CharUnits::Zero(), destType, |
1272 | E->getUpdater())) |
1273 | return nullptr; |
1274 | |
1275 | llvm::Type *ValTy = CGM.getTypes().ConvertType(destType); |
1276 | bool HasFlexibleArray = false; |
1277 | if (auto *RT = destType->getAs<RecordType>()) |
1278 | HasFlexibleArray = RT->getDecl()->hasFlexibleArrayMember(); |
1279 | return Const.build(ValTy, HasFlexibleArray); |
1280 | } |
1281 | |
1282 | llvm::Constant *VisitCXXConstructExpr(CXXConstructExpr *E, QualType Ty) { |
1283 | if (!E->getConstructor()->isTrivial()) |
1284 | return nullptr; |
1285 | |
1286 | |
1287 | if (E->getNumArgs()) { |
1288 | assert(E->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
1289 | assert(E->getConstructor()->isCopyOrMoveConstructor() && |
1290 | "trivial ctor has argument but isn't a copy/move ctor"); |
1291 | |
1292 | Expr *Arg = E->getArg(0); |
1293 | assert(CGM.getContext().hasSameUnqualifiedType(Ty, Arg->getType()) && |
1294 | "argument to copy ctor is of wrong type"); |
1295 | |
1296 | return Visit(Arg, Ty); |
1297 | } |
1298 | |
1299 | return CGM.EmitNullConstant(Ty); |
1300 | } |
1301 | |
1302 | llvm::Constant *VisitStringLiteral(StringLiteral *E, QualType T) { |
1303 | |
1304 | return CGM.GetConstantArrayFromStringLiteral(E); |
1305 | } |
1306 | |
1307 | llvm::Constant *VisitObjCEncodeExpr(ObjCEncodeExpr *E, QualType T) { |
1308 | |
1309 | |
1310 | |
1311 | std::string Str; |
1312 | CGM.getContext().getObjCEncodingForType(E->getEncodedType(), Str); |
1313 | const ConstantArrayType *CAT = CGM.getContext().getAsConstantArrayType(T); |
1314 | |
1315 | |
1316 | |
1317 | Str.resize(CAT->getSize().getZExtValue(), '\0'); |
1318 | return llvm::ConstantDataArray::getString(VMContext, Str, false); |
1319 | } |
1320 | |
1321 | llvm::Constant *VisitUnaryExtension(const UnaryOperator *E, QualType T) { |
1322 | return Visit(E->getSubExpr(), T); |
1323 | } |
1324 | |
1325 | |
1326 | llvm::Type *ConvertType(QualType T) { |
1327 | return CGM.getTypes().ConvertType(T); |
1328 | } |
1329 | }; |
1330 | |
1331 | } |
1332 | |
1333 | llvm::Constant *ConstantEmitter::validateAndPopAbstract(llvm::Constant *C, |
1334 | AbstractState saved) { |
1335 | Abstract = saved.OldValue; |
1336 | |
1337 | assert(saved.OldPlaceholdersSize == PlaceholderAddresses.size() && |
1338 | "created a placeholder while doing an abstract emission?"); |
1339 | |
1340 | |
1341 | |
1342 | return C; |
1343 | } |
1344 | |
1345 | llvm::Constant * |
1346 | ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) { |
1347 | auto state = pushAbstract(); |
1348 | auto C = tryEmitPrivateForVarInit(D); |
1349 | return validateAndPopAbstract(C, state); |
1350 | } |
1351 | |
1352 | llvm::Constant * |
1353 | ConstantEmitter::tryEmitAbstract(const Expr *E, QualType destType) { |
1354 | auto state = pushAbstract(); |
1355 | auto C = tryEmitPrivate(E, destType); |
1356 | return validateAndPopAbstract(C, state); |
1357 | } |
1358 | |
1359 | llvm::Constant * |
1360 | ConstantEmitter::tryEmitAbstract(const APValue &value, QualType destType) { |
1361 | auto state = pushAbstract(); |
1362 | auto C = tryEmitPrivate(value, destType); |
1363 | return validateAndPopAbstract(C, state); |
1364 | } |
1365 | |
1366 | llvm::Constant *ConstantEmitter::tryEmitConstantExpr(const ConstantExpr *CE) { |
1367 | if (!CE->hasAPValueResult()) |
1368 | return nullptr; |
1369 | const Expr *Inner = CE->getSubExpr()->IgnoreImplicit(); |
1370 | QualType RetType; |
1371 | if (auto *Call = dyn_cast<CallExpr>(Inner)) |
1372 | RetType = Call->getCallReturnType(CGF->getContext()); |
1373 | else if (auto *Ctor = dyn_cast<CXXConstructExpr>(Inner)) |
1374 | RetType = Ctor->getType(); |
1375 | llvm::Constant *Res = |
1376 | emitAbstract(CE->getBeginLoc(), CE->getAPValueResult(), RetType); |
1377 | return Res; |
1378 | } |
1379 | |
1380 | llvm::Constant * |
1381 | ConstantEmitter::emitAbstract(const Expr *E, QualType destType) { |
1382 | auto state = pushAbstract(); |
1383 | auto C = tryEmitPrivate(E, destType); |
1384 | C = validateAndPopAbstract(C, state); |
1385 | if (!C) { |
1386 | CGM.Error(E->getExprLoc(), |
1387 | "internal error: could not emit constant value \"abstractly\""); |
1388 | C = CGM.EmitNullConstant(destType); |
1389 | } |
1390 | return C; |
1391 | } |
1392 | |
1393 | llvm::Constant * |
1394 | ConstantEmitter::emitAbstract(SourceLocation loc, const APValue &value, |
1395 | QualType destType) { |
1396 | auto state = pushAbstract(); |
1397 | auto C = tryEmitPrivate(value, destType); |
1398 | C = validateAndPopAbstract(C, state); |
1399 | if (!C) { |
1400 | CGM.Error(loc, |
1401 | "internal error: could not emit constant value \"abstractly\""); |
1402 | C = CGM.EmitNullConstant(destType); |
1403 | } |
1404 | return C; |
1405 | } |
1406 | |
1407 | llvm::Constant *ConstantEmitter::tryEmitForInitializer(const VarDecl &D) { |
1408 | initializeNonAbstract(D.getType().getAddressSpace()); |
1409 | return markIfFailed(tryEmitPrivateForVarInit(D)); |
1410 | } |
1411 | |
1412 | llvm::Constant *ConstantEmitter::tryEmitForInitializer(const Expr *E, |
1413 | LangAS destAddrSpace, |
1414 | QualType destType) { |
1415 | initializeNonAbstract(destAddrSpace); |
1416 | return markIfFailed(tryEmitPrivateForMemory(E, destType)); |
| 5 | | Calling 'ConstantEmitter::tryEmitPrivateForMemory' | |
|
1417 | } |
1418 | |
1419 | llvm::Constant *ConstantEmitter::emitForInitializer(const APValue &value, |
1420 | LangAS destAddrSpace, |
1421 | QualType destType) { |
1422 | initializeNonAbstract(destAddrSpace); |
1423 | auto C = tryEmitPrivateForMemory(value, destType); |
1424 | assert(C && "couldn't emit constant value non-abstractly?"); |
1425 | return C; |
1426 | } |
1427 | |
1428 | llvm::GlobalValue *ConstantEmitter::getCurrentAddrPrivate() { |
1429 | assert(!Abstract && "cannot get current address for abstract constant"); |
1430 | |
1431 | |
1432 | |
1433 | |
1434 | |
1435 | auto global = new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, true, |
1436 | llvm::GlobalValue::PrivateLinkage, |
1437 | nullptr, |
1438 | "", |
1439 | nullptr, |
1440 | llvm::GlobalVariable::NotThreadLocal, |
1441 | CGM.getContext().getTargetAddressSpace(DestAddressSpace)); |
1442 | |
1443 | PlaceholderAddresses.push_back(std::make_pair(nullptr, global)); |
1444 | |
1445 | return global; |
1446 | } |
1447 | |
1448 | void ConstantEmitter::registerCurrentAddrPrivate(llvm::Constant *signal, |
1449 | llvm::GlobalValue *placeholder) { |
1450 | assert(!PlaceholderAddresses.empty()); |
1451 | assert(PlaceholderAddresses.back().first == nullptr); |
1452 | assert(PlaceholderAddresses.back().second == placeholder); |
1453 | PlaceholderAddresses.back().first = signal; |
1454 | } |
1455 | |
1456 | namespace { |
1457 | struct ReplacePlaceholders { |
1458 | CodeGenModule &CGM; |
1459 | |
1460 | |
1461 | llvm::Constant *Base; |
1462 | llvm::Type *BaseValueTy = nullptr; |
1463 | |
1464 | |
1465 | llvm::DenseMap<llvm::Constant*, llvm::GlobalVariable*> PlaceholderAddresses; |
1466 | |
1467 | |
1468 | llvm::DenseMap<llvm::GlobalVariable*, llvm::Constant*> Locations; |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | llvm::SmallVector<unsigned, 8> Indices; |
1474 | llvm::SmallVector<llvm::Constant*, 8> IndexValues; |
1475 | |
1476 | ReplacePlaceholders(CodeGenModule &CGM, llvm::Constant *base, |
1477 | ArrayRef<std::pair<llvm::Constant*, |
1478 | llvm::GlobalVariable*>> addresses) |
1479 | : CGM(CGM), Base(base), |
1480 | PlaceholderAddresses(addresses.begin(), addresses.end()) { |
1481 | } |
1482 | |
1483 | void replaceInInitializer(llvm::Constant *init) { |
1484 | |
1485 | BaseValueTy = init->getType(); |
1486 | |
1487 | |
1488 | Indices.push_back(0); |
1489 | IndexValues.push_back(nullptr); |
1490 | |
1491 | |
1492 | findLocations(init); |
1493 | |
1494 | |
1495 | assert(IndexValues.size() == Indices.size() && "mismatch"); |
1496 | assert(Indices.size() == 1 && "didn't pop all indices"); |
1497 | |
1498 | |
1499 | assert(Locations.size() == PlaceholderAddresses.size() && |
1500 | "missed a placeholder?"); |
1501 | |
1502 | |
1503 | |
1504 | |
1505 | |
1506 | for (auto &entry : Locations) { |
1507 | assert(entry.first->getParent() == nullptr && "not a placeholder!"); |
1508 | entry.first->replaceAllUsesWith(entry.second); |
1509 | entry.first->eraseFromParent(); |
1510 | } |
1511 | } |
1512 | |
1513 | private: |
1514 | void findLocations(llvm::Constant *init) { |
1515 | |
1516 | if (auto agg = dyn_cast<llvm::ConstantAggregate>(init)) { |
1517 | for (unsigned i = 0, e = agg->getNumOperands(); i != e; ++i) { |
1518 | Indices.push_back(i); |
1519 | IndexValues.push_back(nullptr); |
1520 | |
1521 | findLocations(agg->getOperand(i)); |
1522 | |
1523 | IndexValues.pop_back(); |
1524 | Indices.pop_back(); |
1525 | } |
1526 | return; |
1527 | } |
1528 | |
1529 | |
1530 | while (true) { |
1531 | auto it = PlaceholderAddresses.find(init); |
1532 | if (it != PlaceholderAddresses.end()) { |
1533 | setLocation(it->second); |
1534 | break; |
1535 | } |
1536 | |
1537 | |
1538 | if (auto expr = dyn_cast<llvm::ConstantExpr>(init)) { |
1539 | init = expr->getOperand(0); |
1540 | } else { |
1541 | break; |
1542 | } |
1543 | } |
1544 | } |
1545 | |
1546 | void setLocation(llvm::GlobalVariable *placeholder) { |
1547 | assert(Locations.find(placeholder) == Locations.end() && |
1548 | "already found location for placeholder!"); |
1549 | |
1550 | |
1551 | |
1552 | |
1553 | assert(Indices.size() == IndexValues.size()); |
1554 | for (size_t i = Indices.size() - 1; i != size_t(-1); --i) { |
1555 | if (IndexValues[i]) { |
1556 | #ifndef NDEBUG |
1557 | for (size_t j = 0; j != i + 1; ++j) { |
1558 | assert(IndexValues[j] && |
1559 | isa<llvm::ConstantInt>(IndexValues[j]) && |
1560 | cast<llvm::ConstantInt>(IndexValues[j])->getZExtValue() |
1561 | == Indices[j]); |
1562 | } |
1563 | #endif |
1564 | break; |
1565 | } |
1566 | |
1567 | IndexValues[i] = llvm::ConstantInt::get(CGM.Int32Ty, Indices[i]); |
1568 | } |
1569 | |
1570 | |
1571 | |
1572 | llvm::Constant *location = |
1573 | llvm::ConstantExpr::getInBoundsGetElementPtr(BaseValueTy, |
1574 | Base, IndexValues); |
1575 | location = llvm::ConstantExpr::getBitCast(location, |
1576 | placeholder->getType()); |
1577 | |
1578 | Locations.insert({placeholder, location}); |
1579 | } |
1580 | }; |
1581 | } |
1582 | |
1583 | void ConstantEmitter::finalize(llvm::GlobalVariable *global) { |
1584 | assert(InitializedNonAbstract && |
1585 | "finalizing emitter that was used for abstract emission?"); |
1586 | assert(!Finalized && "finalizing emitter multiple times"); |
1587 | assert(global->getInitializer()); |
1588 | |
1589 | |
1590 | Finalized = true; |
1591 | |
1592 | if (!PlaceholderAddresses.empty()) { |
1593 | ReplacePlaceholders(CGM, global, PlaceholderAddresses) |
1594 | .replaceInInitializer(global->getInitializer()); |
1595 | PlaceholderAddresses.clear(); |
1596 | } |
1597 | } |
1598 | |
1599 | ConstantEmitter::~ConstantEmitter() { |
1600 | assert((!InitializedNonAbstract || Finalized || Failed) && |
1601 | "not finalized after being initialized for non-abstract emission"); |
1602 | assert(PlaceholderAddresses.empty() && "unhandled placeholders"); |
1603 | } |
1604 | |
1605 | static QualType getNonMemoryType(CodeGenModule &CGM, QualType type) { |
1606 | if (auto AT = type->getAs<AtomicType>()) { |
1607 | return CGM.getContext().getQualifiedType(AT->getValueType(), |
1608 | type.getQualifiers()); |
1609 | } |
1610 | return type; |
1611 | } |
1612 | |
1613 | llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) { |
1614 | |
1615 | |
1616 | |
1617 | if (!D.hasLocalStorage()) { |
1618 | QualType Ty = CGM.getContext().getBaseElementType(D.getType()); |
1619 | if (Ty->isRecordType()) |
1620 | if (const CXXConstructExpr *E = |
1621 | dyn_cast_or_null<CXXConstructExpr>(D.getInit())) { |
1622 | const CXXConstructorDecl *CD = E->getConstructor(); |
1623 | if (CD->isTrivial() && CD->isDefaultConstructor()) |
1624 | return CGM.EmitNullConstant(D.getType()); |
1625 | } |
1626 | } |
1627 | InConstantContext = D.hasConstantInitialization(); |
1628 | |
1629 | QualType destType = D.getType(); |
1630 | |
1631 | |
1632 | |
1633 | if (auto value = D.evaluateValue()) { |
1634 | return tryEmitPrivateForMemory(*value, destType); |
1635 | } |
1636 | |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | |
1643 | if (destType->isReferenceType()) |
1644 | return nullptr; |
1645 | |
1646 | const Expr *E = D.getInit(); |
1647 | assert(E && "No initializer to emit"); |
1648 | |
1649 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1650 | auto C = |
1651 | ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType); |
1652 | return (C ? emitForMemory(C, destType) : nullptr); |
1653 | } |
1654 | |
1655 | llvm::Constant * |
1656 | ConstantEmitter::tryEmitAbstractForMemory(const Expr *E, QualType destType) { |
1657 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1658 | auto C = tryEmitAbstract(E, nonMemoryDestType); |
1659 | return (C ? emitForMemory(C, destType) : nullptr); |
1660 | } |
1661 | |
1662 | llvm::Constant * |
1663 | ConstantEmitter::tryEmitAbstractForMemory(const APValue &value, |
1664 | QualType destType) { |
1665 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1666 | auto C = tryEmitAbstract(value, nonMemoryDestType); |
1667 | return (C ? emitForMemory(C, destType) : nullptr); |
1668 | } |
1669 | |
1670 | llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const Expr *E, |
1671 | QualType destType) { |
1672 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1673 | llvm::Constant *C = tryEmitPrivate(E, nonMemoryDestType); |
| 6 | | Calling 'ConstantEmitter::tryEmitPrivate' | |
|
1674 | return (C ? emitForMemory(C, destType) : nullptr); |
1675 | } |
1676 | |
1677 | llvm::Constant *ConstantEmitter::tryEmitPrivateForMemory(const APValue &value, |
1678 | QualType destType) { |
1679 | auto nonMemoryDestType = getNonMemoryType(CGM, destType); |
1680 | auto C = tryEmitPrivate(value, nonMemoryDestType); |
1681 | return (C ? emitForMemory(C, destType) : nullptr); |
| 22 | | Assuming 'C' is non-null | |
|
| |
| 24 | | Returning pointer, which participates in a condition later | |
|
1682 | } |
1683 | |
1684 | llvm::Constant *ConstantEmitter::emitForMemory(CodeGenModule &CGM, |
1685 | llvm::Constant *C, |
1686 | QualType destType) { |
1687 | |
1688 | if (auto AT = destType->getAs<AtomicType>()) { |
1689 | QualType destValueType = AT->getValueType(); |
1690 | C = emitForMemory(CGM, C, destValueType); |
1691 | |
1692 | uint64_t innerSize = CGM.getContext().getTypeSize(destValueType); |
1693 | uint64_t outerSize = CGM.getContext().getTypeSize(destType); |
1694 | if (innerSize == outerSize) |
1695 | return C; |
1696 | |
1697 | assert(innerSize < outerSize && "emitted over-large constant for atomic"); |
1698 | llvm::Constant *elts[] = { |
1699 | C, |
1700 | llvm::ConstantAggregateZero::get( |
1701 | llvm::ArrayType::get(CGM.Int8Ty, (outerSize - innerSize) / 8)) |
1702 | }; |
1703 | return llvm::ConstantStruct::getAnon(elts); |
1704 | } |
1705 | |
1706 | |
1707 | if (C->getType()->isIntegerTy(1)) { |
1708 | llvm::Type *boolTy = CGM.getTypes().ConvertTypeForMem(destType); |
1709 | return llvm::ConstantExpr::getZExt(C, boolTy); |
1710 | } |
1711 | |
1712 | return C; |
1713 | } |
1714 | |
1715 | llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E, |
1716 | QualType destType) { |
1717 | Expr::EvalResult Result; |
1718 | |
1719 | bool Success = false; |
1720 | |
1721 | if (destType->isReferenceType()) |
| |
1722 | Success = E->EvaluateAsLValue(Result, CGM.getContext()); |
1723 | else |
1724 | Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext); |
1725 | |
1726 | llvm::Constant *C; |
1727 | if (Success && !Result.HasSideEffects) |
| 8 | | Assuming 'Success' is true | |
|
| 9 | | Assuming field 'HasSideEffects' is false | |
|
| |
1728 | C = tryEmitPrivate(Result.Val, destType); |
| 11 | | Calling 'ConstantEmitter::tryEmitPrivate' | |
|
1729 | else |
1730 | C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType); |
1731 | |
1732 | return C; |
1733 | } |
1734 | |
1735 | llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) { |
1736 | return getTargetCodeGenInfo().getNullPointer(*this, T, QT); |
1737 | } |
1738 | |
1739 | namespace { |
1740 | |
1741 | |
1742 | struct ConstantLValue { |
1743 | llvm::Constant *Value; |
1744 | bool HasOffsetApplied; |
1745 | |
1746 | ConstantLValue(llvm::Constant *value, |
1747 | bool hasOffsetApplied = false) |
1748 | : Value(value), HasOffsetApplied(hasOffsetApplied) {} |
1749 | |
1750 | ConstantLValue(ConstantAddress address) |
1751 | : ConstantLValue(address.getPointer()) {} |
1752 | }; |
1753 | |
1754 | |
1755 | class ConstantLValueEmitter : public ConstStmtVisitor<ConstantLValueEmitter, |
1756 | ConstantLValue> { |
1757 | CodeGenModule &CGM; |
1758 | ConstantEmitter &Emitter; |
1759 | const APValue &Value; |
1760 | QualType DestType; |
1761 | |
1762 | |
1763 | friend StmtVisitorBase; |
1764 | |
1765 | public: |
1766 | ConstantLValueEmitter(ConstantEmitter &emitter, const APValue &value, |
1767 | QualType destType) |
1768 | : CGM(emitter.CGM), Emitter(emitter), Value(value), DestType(destType) {} |
1769 | |
1770 | llvm::Constant *tryEmit(); |
1771 | |
1772 | private: |
1773 | llvm::Constant *tryEmitAbsolute(llvm::Type *destTy); |
1774 | ConstantLValue tryEmitBase(const APValue::LValueBase &base); |
1775 | |
1776 | ConstantLValue VisitStmt(const Stmt *S) { return nullptr; } |
1777 | ConstantLValue VisitConstantExpr(const ConstantExpr *E); |
1778 | ConstantLValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
1779 | ConstantLValue VisitStringLiteral(const StringLiteral *E); |
1780 | ConstantLValue VisitObjCBoxedExpr(const ObjCBoxedExpr *E); |
1781 | ConstantLValue VisitObjCEncodeExpr(const ObjCEncodeExpr *E); |
1782 | ConstantLValue VisitObjCStringLiteral(const ObjCStringLiteral *E); |
1783 | ConstantLValue VisitPredefinedExpr(const PredefinedExpr *E); |
1784 | ConstantLValue VisitAddrLabelExpr(const AddrLabelExpr *E); |
1785 | ConstantLValue VisitCallExpr(const CallExpr *E); |
1786 | ConstantLValue VisitBlockExpr(const BlockExpr *E); |
1787 | ConstantLValue VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
1788 | ConstantLValue VisitMaterializeTemporaryExpr( |
1789 | const MaterializeTemporaryExpr *E); |
1790 | |
1791 | bool hasNonZeroOffset() const { |
1792 | return !Value.getLValueOffset().isZero(); |
1793 | } |
1794 | |
1795 | |
1796 | llvm::Constant *getOffset() { |
1797 | return llvm::ConstantInt::get(CGM.Int64Ty, |
1798 | Value.getLValueOffset().getQuantity()); |
1799 | } |
1800 | |
1801 | |
1802 | llvm::Constant *applyOffset(llvm::Constant *C) { |
1803 | if (!hasNonZeroOffset()) |
1804 | return C; |
1805 | |
1806 | llvm::Type *origPtrTy = C->getType(); |
1807 | unsigned AS = origPtrTy->getPointerAddressSpace(); |
1808 | llvm::Type *charPtrTy = CGM.Int8Ty->getPointerTo(AS); |
1809 | C = llvm::ConstantExpr::getBitCast(C, charPtrTy); |
1810 | C = llvm::ConstantExpr::getGetElementPtr(CGM.Int8Ty, C, getOffset()); |
1811 | C = llvm::ConstantExpr::getPointerCast(C, origPtrTy); |
1812 | return C; |
1813 | } |
1814 | }; |
1815 | |
1816 | } |
1817 | |
1818 | llvm::Constant *ConstantLValueEmitter::tryEmit() { |
1819 | const APValue::LValueBase &base = Value.getLValueBase(); |
1820 | |
1821 | |
1822 | |
1823 | |
1824 | |
1825 | |
1826 | |
1827 | |
1828 | auto destTy = CGM.getTypes().ConvertTypeForMem(DestType); |
1829 | assert(isa<llvm::IntegerType>(destTy) || isa<llvm::PointerType>(destTy)); |
1830 | |
1831 | |
1832 | |
1833 | if (!base) { |
1834 | return tryEmitAbsolute(destTy); |
1835 | } |
1836 | |
1837 | |
1838 | ConstantLValue result = tryEmitBase(base); |
1839 | |
1840 | |
1841 | llvm::Constant *value = result.Value; |
1842 | if (!value) return nullptr; |
1843 | |
1844 | |
1845 | if (!result.HasOffsetApplied) { |
1846 | value = applyOffset(value); |
1847 | } |
1848 | |
1849 | |
1850 | |
1851 | if (isa<llvm::PointerType>(destTy)) |
1852 | return llvm::ConstantExpr::getPointerCast(value, destTy); |
1853 | |
1854 | return llvm::ConstantExpr::getPtrToInt(value, destTy); |
1855 | } |
1856 | |
1857 | |
1858 | |
1859 | llvm::Constant * |
1860 | ConstantLValueEmitter::tryEmitAbsolute(llvm::Type *destTy) { |
1861 | |
1862 | auto destPtrTy = cast<llvm::PointerType>(destTy); |
1863 | if (Value.isNullPointer()) { |
1864 | |
1865 | return CGM.getNullPointer(destPtrTy, DestType); |
1866 | } |
1867 | |
1868 | |
1869 | |
1870 | |
1871 | auto intptrTy = CGM.getDataLayout().getIntPtrType(destPtrTy); |
1872 | llvm::Constant *C; |
1873 | C = llvm::ConstantExpr::getIntegerCast(getOffset(), intptrTy, |
1874 | false); |
1875 | C = llvm::ConstantExpr::getIntToPtr(C, destPtrTy); |
1876 | return C; |
1877 | } |
1878 | |
1879 | ConstantLValue |
1880 | ConstantLValueEmitter::tryEmitBase(const APValue::LValueBase &base) { |
1881 | |
1882 | if (const ValueDecl *D = base.dyn_cast<const ValueDecl*>()) { |
1883 | |
1884 | |
1885 | D = cast<ValueDecl>(D->getMostRecentDecl()); |
1886 | |
1887 | if (D->hasAttr<WeakRefAttr>()) |
1888 | return CGM.GetWeakRefReference(D).getPointer(); |
1889 | |
1890 | if (auto FD = dyn_cast<FunctionDecl>(D)) |
1891 | return CGM.GetAddrOfFunction(FD); |
1892 | |
1893 | if (auto VD = dyn_cast<VarDecl>(D)) { |
1894 | |
1895 | if (!VD->hasLocalStorage()) { |
1896 | if (VD->isFileVarDecl() || VD->hasExternalStorage()) |
1897 | return CGM.GetAddrOfGlobalVar(VD); |
1898 | |
1899 | if (VD->isLocalVarDecl()) { |
1900 | return CGM.getOrCreateStaticVarDecl( |
1901 | *VD, CGM.getLLVMLinkageVarDefinition(VD, false)); |
1902 | } |
1903 | } |
1904 | } |
1905 | |
1906 | if (auto *GD = dyn_cast<MSGuidDecl>(D)) |
1907 | return CGM.GetAddrOfMSGuidDecl(GD); |
1908 | |
1909 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) |
1910 | return CGM.GetAddrOfTemplateParamObject(TPO); |
1911 | |
1912 | return nullptr; |
1913 | } |
1914 | |
1915 | |
1916 | if (TypeInfoLValue TI = base.dyn_cast<TypeInfoLValue>()) { |
1917 | llvm::Type *StdTypeInfoPtrTy = |
1918 | CGM.getTypes().ConvertType(base.getTypeInfoType())->getPointerTo(); |
1919 | llvm::Constant *TypeInfo = |
1920 | CGM.GetAddrOfRTTIDescriptor(QualType(TI.getType(), 0)); |
1921 | if (TypeInfo->getType() != StdTypeInfoPtrTy) |
1922 | TypeInfo = llvm::ConstantExpr::getBitCast(TypeInfo, StdTypeInfoPtrTy); |
1923 | return TypeInfo; |
1924 | } |
1925 | |
1926 | |
1927 | return Visit(base.get<const Expr*>()); |
1928 | } |
1929 | |
1930 | ConstantLValue |
1931 | ConstantLValueEmitter::VisitConstantExpr(const ConstantExpr *E) { |
1932 | if (llvm::Constant *Result = Emitter.tryEmitConstantExpr(E)) |
1933 | return Result; |
1934 | return Visit(E->getSubExpr()); |
1935 | } |
1936 | |
1937 | ConstantLValue |
1938 | ConstantLValueEmitter::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
1939 | return tryEmitGlobalCompoundLiteral(CGM, Emitter.CGF, E); |
1940 | } |
1941 | |
1942 | ConstantLValue |
1943 | ConstantLValueEmitter::VisitStringLiteral(const StringLiteral *E) { |
1944 | return CGM.GetAddrOfConstantStringFromLiteral(E); |
1945 | } |
1946 | |
1947 | ConstantLValue |
1948 | ConstantLValueEmitter::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { |
1949 | return CGM.GetAddrOfConstantStringFromObjCEncode(E); |
1950 | } |
1951 | |
1952 | static ConstantLValue emitConstantObjCStringLiteral(const StringLiteral *S, |
1953 | QualType T, |
1954 | CodeGenModule &CGM) { |
1955 | auto C = CGM.getObjCRuntime().GenerateConstantString(S); |
1956 | return C.getElementBitCast(CGM.getTypes().ConvertTypeForMem(T)); |
1957 | } |
1958 | |
1959 | ConstantLValue |
1960 | ConstantLValueEmitter::VisitObjCStringLiteral(const ObjCStringLiteral *E) { |
1961 | return emitConstantObjCStringLiteral(E->getString(), E->getType(), CGM); |
1962 | } |
1963 | |
1964 | ConstantLValue |
1965 | ConstantLValueEmitter::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
1966 | assert(E->isExpressibleAsConstantInitializer() && |
1967 | "this boxed expression can't be emitted as a compile-time constant"); |
1968 | auto *SL = cast<StringLiteral>(E->getSubExpr()->IgnoreParenCasts()); |
1969 | return emitConstantObjCStringLiteral(SL, E->getType(), CGM); |
1970 | } |
1971 | |
1972 | ConstantLValue |
1973 | ConstantLValueEmitter::VisitPredefinedExpr(const PredefinedExpr *E) { |
1974 | return CGM.GetAddrOfConstantStringFromLiteral(E->getFunctionName()); |
1975 | } |
1976 | |
1977 | ConstantLValue |
1978 | ConstantLValueEmitter::VisitAddrLabelExpr(const AddrLabelExpr *E) { |
1979 | assert(Emitter.CGF && "Invalid address of label expression outside function"); |
1980 | llvm::Constant *Ptr = Emitter.CGF->GetAddrOfLabel(E->getLabel()); |
1981 | Ptr = llvm::ConstantExpr::getBitCast(Ptr, |
1982 | CGM.getTypes().ConvertType(E->getType())); |
1983 | return Ptr; |
1984 | } |
1985 | |
1986 | ConstantLValue |
1987 | ConstantLValueEmitter::VisitCallExpr(const CallExpr *E) { |
1988 | unsigned builtin = E->getBuiltinCallee(); |
1989 | if (builtin != Builtin::BI__builtin___CFStringMakeConstantString && |
1990 | builtin != Builtin::BI__builtin___NSStringMakeConstantString) |
1991 | return nullptr; |
1992 | |
1993 | auto literal = cast<StringLiteral>(E->getArg(0)->IgnoreParenCasts()); |
1994 | if (builtin == Builtin::BI__builtin___NSStringMakeConstantString) { |
1995 | return CGM.getObjCRuntime().GenerateConstantString(literal); |
1996 | } else { |
1997 | |
1998 | return CGM.GetAddrOfConstantCFString(literal); |
1999 | } |
2000 | } |
2001 | |
2002 | ConstantLValue |
2003 | ConstantLValueEmitter::VisitBlockExpr(const BlockExpr *E) { |
2004 | StringRef functionName; |
2005 | if (auto CGF = Emitter.CGF) |
2006 | functionName = CGF->CurFn->getName(); |
2007 | else |
2008 | functionName = "global"; |
2009 | |
2010 | return CGM.GetAddrOfGlobalBlock(E, functionName); |
2011 | } |
2012 | |
2013 | ConstantLValue |
2014 | ConstantLValueEmitter::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
2015 | QualType T; |
2016 | if (E->isTypeOperand()) |
2017 | T = E->getTypeOperand(CGM.getContext()); |
2018 | else |
2019 | T = E->getExprOperand()->getType(); |
2020 | return CGM.GetAddrOfRTTIDescriptor(T); |
2021 | } |
2022 | |
2023 | ConstantLValue |
2024 | ConstantLValueEmitter::VisitMaterializeTemporaryExpr( |
2025 | const MaterializeTemporaryExpr *E) { |
2026 | assert(E->getStorageDuration() == SD_Static); |
2027 | SmallVector<const Expr *, 2> CommaLHSs; |
2028 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
2029 | const Expr *Inner = |
2030 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
2031 | return CGM.GetAddrOfGlobalTemporary(E, Inner); |
2032 | } |
2033 | |
2034 | llvm::Constant *ConstantEmitter::tryEmitPrivate(const APValue &Value, |
2035 | QualType DestType) { |
2036 | switch (Value.getKind()) { |
| 12 | | Control jumps to 'case Array:' at line 2120 | |
|
2037 | case APValue::None: |
2038 | case APValue::Indeterminate: |
2039 | |
2040 | return llvm::UndefValue::get(CGM.getTypes().ConvertType(DestType)); |
2041 | case APValue::LValue: |
2042 | return ConstantLValueEmitter(*this, Value, DestType).tryEmit(); |
2043 | case APValue::Int: |
2044 | return llvm::ConstantInt::get(CGM.getLLVMContext(), Value.getInt()); |
2045 | case APValue::FixedPoint: |
2046 | return llvm::ConstantInt::get(CGM.getLLVMContext(), |
2047 | Value.getFixedPoint().getValue()); |
2048 | case APValue::ComplexInt: { |
2049 | llvm::Constant *Complex[2]; |
2050 | |
2051 | Complex[0] = llvm::ConstantInt::get(CGM.getLLVMContext(), |
2052 | Value.getComplexIntReal()); |
2053 | Complex[1] = llvm::ConstantInt::get(CGM.getLLVMContext(), |
2054 | Value.getComplexIntImag()); |
2055 | |
2056 | |
2057 | llvm::StructType *STy = |
2058 | llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType()); |
2059 | return llvm::ConstantStruct::get(STy, Complex); |
2060 | } |
2061 | case APValue::Float: { |
2062 | const llvm::APFloat &Init = Value.getFloat(); |
2063 | if (&Init.getSemantics() == &llvm::APFloat::IEEEhalf() && |
2064 | !CGM.getContext().getLangOpts().NativeHalfType && |
2065 | CGM.getContext().getTargetInfo().useFP16ConversionIntrinsics()) |
2066 | return llvm::ConstantInt::get(CGM.getLLVMContext(), |
2067 | Init.bitcastToAPInt()); |
2068 | else |
2069 | return llvm::ConstantFP::get(CGM.getLLVMContext(), Init); |
2070 | } |
2071 | case APValue::ComplexFloat: { |
2072 | llvm::Constant *Complex[2]; |
2073 | |
2074 | Complex[0] = llvm::ConstantFP::get(CGM.getLLVMContext(), |
2075 | Value.getComplexFloatReal()); |
2076 | Complex[1] = llvm::ConstantFP::get(CGM.getLLVMContext(), |
2077 | Value.getComplexFloatImag()); |
2078 | |
2079 | |
2080 | llvm::StructType *STy = |
2081 | llvm::StructType::get(Complex[0]->getType(), Complex[1]->getType()); |
2082 | return llvm::ConstantStruct::get(STy, Complex); |
2083 | } |
2084 | case APValue::Vector: { |
2085 | unsigned NumElts = Value.getVectorLength(); |
2086 | SmallVector<llvm::Constant *, 4> Inits(NumElts); |
2087 | |
2088 | for (unsigned I = 0; I != NumElts; ++I) { |
2089 | const APValue &Elt = Value.getVectorElt(I); |
2090 | if (Elt.isInt()) |
2091 | Inits[I] = llvm::ConstantInt::get(CGM.getLLVMContext(), Elt.getInt()); |
2092 | else if (Elt.isFloat()) |
2093 | Inits[I] = llvm::ConstantFP::get(CGM.getLLVMContext(), Elt.getFloat()); |
2094 | else |
2095 | llvm_unreachable("unsupported vector element type"); |
2096 | } |
2097 | return llvm::ConstantVector::get(Inits); |
2098 | } |
2099 | case APValue::AddrLabelDiff: { |
2100 | const AddrLabelExpr *LHSExpr = Value.getAddrLabelDiffLHS(); |
2101 | const AddrLabelExpr *RHSExpr = Value.getAddrLabelDiffRHS(); |
2102 | llvm::Constant *LHS = tryEmitPrivate(LHSExpr, LHSExpr->getType()); |
2103 | llvm::Constant *RHS = tryEmitPrivate(RHSExpr, RHSExpr->getType()); |
2104 | if (!LHS || !RHS) return nullptr; |
2105 | |
2106 | |
2107 | llvm::Type *ResultType = CGM.getTypes().ConvertType(DestType); |
2108 | LHS = llvm::ConstantExpr::getPtrToInt(LHS, CGM.IntPtrTy); |
2109 | RHS = llvm::ConstantExpr::getPtrToInt(RHS, CGM.IntPtrTy); |
2110 | llvm::Constant *AddrLabelDiff = llvm::ConstantExpr::getSub(LHS, RHS); |
2111 | |
2112 | |
2113 | |
2114 | |
2115 | return llvm::ConstantExpr::getTruncOrBitCast(AddrLabelDiff, ResultType); |
2116 | } |
2117 | case APValue::Struct: |
2118 | case APValue::Union: |
2119 | return ConstStructBuilder::BuildStruct(*this, Value, DestType); |
2120 | case APValue::Array: { |
2121 | const ArrayType *ArrayTy = CGM.getContext().getAsArrayType(DestType); |
2122 | unsigned NumElements = Value.getArraySize(); |
2123 | unsigned NumInitElts = Value.getArrayInitializedElts(); |
2124 | |
2125 | |
2126 | llvm::Constant *Filler = nullptr; |
| 13 | | 'Filler' initialized to a null pointer value | |
|
2127 | if (Value.hasArrayFiller()) { |
| 14 | | Calling 'APValue::hasArrayFiller' | |
|
| 17 | | Returning from 'APValue::hasArrayFiller' | |
|
| |
2128 | Filler = tryEmitAbstractForMemory(Value.getArrayFiller(), |
2129 | ArrayTy->getElementType()); |
2130 | if (!Filler) |
2131 | return nullptr; |
2132 | } |
2133 | |
2134 | |
2135 | SmallVector<llvm::Constant*, 16> Elts; |
2136 | if (Filler && Filler->isNullValue()) |
2137 | Elts.reserve(NumInitElts + 1); |
2138 | else |
2139 | Elts.reserve(NumElements); |
2140 | |
2141 | llvm::Type *CommonElementType = nullptr; |
2142 | for (unsigned I = 0; I < NumInitElts; ++I) { |
| 19 | | Assuming 'I' is < 'NumInitElts' | |
|
| 20 | | Loop condition is true. Entering loop body | |
|
| 29 | | Assuming 'I' is >= 'NumInitElts' | |
|
| 30 | | Loop condition is false. Execution continues on line 2155 | |
|
2143 | llvm::Constant *C = tryEmitPrivateForMemory( |
| 21 | | Calling 'ConstantEmitter::tryEmitPrivateForMemory' | |
|
| 25 | | Returning from 'ConstantEmitter::tryEmitPrivateForMemory' | |
|
2144 | Value.getArrayInitializedElt(I), ArrayTy->getElementType()); |
2145 | if (!C) return nullptr; |
| 26 | | Assuming 'C' is non-null | |
|
| |
2146 | |
2147 | if (I == 0) |
| |
2148 | CommonElementType = C->getType(); |
2149 | else if (C->getType() != CommonElementType) |
2150 | CommonElementType = nullptr; |
2151 | Elts.push_back(C); |
2152 | } |
2153 | |
2154 | llvm::ArrayType *Desired = |
2155 | cast<llvm::ArrayType>(CGM.getTypes().ConvertType(DestType)); |
| 31 | | The object is a 'ArrayType' | |
|
2156 | return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts, |
| 33 | | Calling 'EmitArrayConstant' | |
|
2157 | Filler); |
| 32 | | Passing null pointer value via 6th parameter 'Filler' | |
|
2158 | } |
2159 | case APValue::MemberPointer: |
2160 | return CGM.getCXXABI().EmitMemberPointer(Value, DestType); |
2161 | } |
2162 | llvm_unreachable("Unknown APValue kind"); |
2163 | } |
2164 | |
2165 | llvm::GlobalVariable *CodeGenModule::getAddrOfConstantCompoundLiteralIfEmitted( |
2166 | const CompoundLiteralExpr *E) { |
2167 | return EmittedCompoundLiterals.lookup(E); |
2168 | } |
2169 | |
2170 | void CodeGenModule::setAddrOfConstantCompoundLiteral( |
2171 | const CompoundLiteralExpr *CLE, llvm::GlobalVariable *GV) { |
2172 | bool Ok = EmittedCompoundLiterals.insert(std::make_pair(CLE, GV)).second; |
2173 | (void)Ok; |
2174 | assert(Ok && "CLE has already been emitted!"); |
2175 | } |
2176 | |
2177 | ConstantAddress |
2178 | CodeGenModule::GetAddrOfConstantCompoundLiteral(const CompoundLiteralExpr *E) { |
2179 | assert(E->isFileScope() && "not a file-scope compound literal expr"); |
2180 | return tryEmitGlobalCompoundLiteral(*this, nullptr, E); |
| 1 | Calling 'tryEmitGlobalCompoundLiteral' | |
|
2181 | } |
2182 | |
2183 | llvm::Constant * |
2184 | CodeGenModule::getMemberPointerConstant(const UnaryOperator *uo) { |
2185 | |
2186 | const MemberPointerType *type = cast<MemberPointerType>(uo->getType()); |
2187 | const ValueDecl *decl = cast<DeclRefExpr>(uo->getSubExpr())->getDecl(); |
2188 | |
2189 | |
2190 | if (const CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(decl)) |
2191 | return getCXXABI().EmitMemberFunctionPointer(method); |
2192 | |
2193 | |
2194 | uint64_t fieldOffset = getContext().getFieldOffset(decl); |
2195 | CharUnits chars = getContext().toCharUnitsFromBits((int64_t) fieldOffset); |
2196 | return getCXXABI().EmitMemberDataPointer(type, chars); |
2197 | } |
2198 | |
2199 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
2200 | llvm::Type *baseType, |
2201 | const CXXRecordDecl *base); |
2202 | |
2203 | static llvm::Constant *EmitNullConstant(CodeGenModule &CGM, |
2204 | const RecordDecl *record, |
2205 | bool asCompleteObject) { |
2206 | const CGRecordLayout &layout = CGM.getTypes().getCGRecordLayout(record); |
2207 | llvm::StructType *structure = |
2208 | (asCompleteObject ? layout.getLLVMType() |
2209 | : layout.getBaseSubobjectLLVMType()); |
2210 | |
2211 | unsigned numElements = structure->getNumElements(); |
2212 | std::vector<llvm::Constant *> elements(numElements); |
2213 | |
2214 | auto CXXR = dyn_cast<CXXRecordDecl>(record); |
2215 | |
2216 | if (CXXR) { |
2217 | for (const auto &I : CXXR->bases()) { |
2218 | if (I.isVirtual()) { |
2219 | |
2220 | |
2221 | continue; |
2222 | } |
2223 | |
2224 | const CXXRecordDecl *base = |
2225 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
2226 | |
2227 | |
2228 | if (base->isEmpty() || |
2229 | CGM.getContext().getASTRecordLayout(base).getNonVirtualSize() |
2230 | .isZero()) |
2231 | continue; |
2232 | |
2233 | unsigned fieldIndex = layout.getNonVirtualBaseLLVMFieldNo(base); |
2234 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
2235 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
2236 | } |
2237 | } |
2238 | |
2239 | |
2240 | for (const auto *Field : record->fields()) { |
2241 | |
2242 | |
2243 | if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())) { |
2244 | unsigned fieldIndex = layout.getLLVMFieldNo(Field); |
2245 | elements[fieldIndex] = CGM.EmitNullConstant(Field->getType()); |
2246 | } |
2247 | |
2248 | |
2249 | if (record->isUnion()) { |
2250 | if (Field->getIdentifier()) |
2251 | break; |
2252 | if (const auto *FieldRD = Field->getType()->getAsRecordDecl()) |
2253 | if (FieldRD->findFirstNamedDataMember()) |
2254 | break; |
2255 | } |
2256 | } |
2257 | |
2258 | |
2259 | if (CXXR && asCompleteObject) { |
2260 | for (const auto &I : CXXR->vbases()) { |
2261 | const CXXRecordDecl *base = |
2262 | cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl()); |
2263 | |
2264 | |
2265 | if (base->isEmpty()) |
2266 | continue; |
2267 | |
2268 | unsigned fieldIndex = layout.getVirtualBaseIndex(base); |
2269 | |
2270 | |
2271 | if (elements[fieldIndex]) continue; |
2272 | |
2273 | llvm::Type *baseType = structure->getElementType(fieldIndex); |
2274 | elements[fieldIndex] = EmitNullConstantForBase(CGM, baseType, base); |
2275 | } |
2276 | } |
2277 | |
2278 | |
2279 | for (unsigned i = 0; i != numElements; ++i) { |
2280 | if (!elements[i]) |
2281 | elements[i] = llvm::Constant::getNullValue(structure->getElementType(i)); |
2282 | } |
2283 | |
2284 | return llvm::ConstantStruct::get(structure, elements); |
2285 | } |
2286 | |
2287 | |
2288 | static llvm::Constant *EmitNullConstantForBase(CodeGenModule &CGM, |
2289 | llvm::Type *baseType, |
2290 | const CXXRecordDecl *base) { |
2291 | const CGRecordLayout &baseLayout = CGM.getTypes().getCGRecordLayout(base); |
2292 | |
2293 | |
2294 | if (baseLayout.isZeroInitializableAsBase()) |
2295 | return llvm::Constant::getNullValue(baseType); |
2296 | |
2297 | |
2298 | return EmitNullConstant(CGM, base, false); |
2299 | } |
2300 | |
2301 | llvm::Constant *ConstantEmitter::emitNullForMemory(CodeGenModule &CGM, |
2302 | QualType T) { |
2303 | return emitForMemory(CGM, CGM.EmitNullConstant(T), T); |
2304 | } |
2305 | |
2306 | llvm::Constant *CodeGenModule::EmitNullConstant(QualType T) { |
2307 | if (T->getAs<PointerType>()) |
2308 | return getNullPointer( |
2309 | cast<llvm::PointerType>(getTypes().ConvertTypeForMem(T)), T); |
2310 | |
2311 | if (getTypes().isZeroInitializable(T)) |
2312 | return llvm::Constant::getNullValue(getTypes().ConvertTypeForMem(T)); |
2313 | |
2314 | if (const ConstantArrayType *CAT = Context.getAsConstantArrayType(T)) { |
2315 | llvm::ArrayType *ATy = |
2316 | cast<llvm::ArrayType>(getTypes().ConvertTypeForMem(T)); |
2317 | |
2318 | QualType ElementTy = CAT->getElementType(); |
2319 | |
2320 | llvm::Constant *Element = |
2321 | ConstantEmitter::emitNullForMemory(*this, ElementTy); |
2322 | unsigned NumElements = CAT->getSize().getZExtValue(); |
2323 | SmallVector<llvm::Constant *, 8> Array(NumElements, Element); |
2324 | return llvm::ConstantArray::get(ATy, Array); |
2325 | } |
2326 | |
2327 | if (const RecordType *RT = T->getAs<RecordType>()) |
2328 | return ::EmitNullConstant(*this, RT->getDecl(), true); |
2329 | |
2330 | assert(T->isMemberDataPointerType() && |
2331 | "Should only see pointers to data members here!"); |
2332 | |
2333 | return getCXXABI().EmitNullMemberPointer(T->castAs<MemberPointerType>()); |
2334 | } |
2335 | |
2336 | llvm::Constant * |
2337 | CodeGenModule::EmitNullConstantForBase(const CXXRecordDecl *Record) { |
2338 | return ::EmitNullConstant(*this, Record, false); |
2339 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_AST_APVALUE_H |
14 | #define LLVM_CLANG_AST_APVALUE_H |
15 | |
16 | #include "clang/Basic/LLVM.h" |
17 | #include "llvm/ADT/APFixedPoint.h" |
18 | #include "llvm/ADT/APFloat.h" |
19 | #include "llvm/ADT/APSInt.h" |
20 | #include "llvm/ADT/FoldingSet.h" |
21 | #include "llvm/ADT/PointerIntPair.h" |
22 | #include "llvm/ADT/PointerUnion.h" |
23 | #include "llvm/Support/AlignOf.h" |
24 | |
25 | namespace clang { |
26 | namespace serialization { |
27 | template <typename T> class BasicReaderBase; |
28 | } |
29 | |
30 | class AddrLabelExpr; |
31 | class ASTContext; |
32 | class CharUnits; |
33 | class CXXRecordDecl; |
34 | class Decl; |
35 | class DiagnosticBuilder; |
36 | class Expr; |
37 | class FieldDecl; |
38 | struct PrintingPolicy; |
39 | class Type; |
40 | class ValueDecl; |
41 | class QualType; |
42 | |
43 | |
44 | class TypeInfoLValue { |
45 | const Type *T; |
46 | |
47 | public: |
48 | TypeInfoLValue() : T() {} |
49 | explicit TypeInfoLValue(const Type *T); |
50 | |
51 | const Type *getType() const { return T; } |
52 | explicit operator bool() const { return T; } |
53 | |
54 | void *getOpaqueValue() { return const_cast<Type*>(T); } |
55 | static TypeInfoLValue getFromOpaqueValue(void *Value) { |
56 | TypeInfoLValue V; |
57 | V.T = reinterpret_cast<const Type*>(Value); |
58 | return V; |
59 | } |
60 | |
61 | void print(llvm::raw_ostream &Out, const PrintingPolicy &Policy) const; |
62 | }; |
63 | |
64 | |
65 | class DynamicAllocLValue { |
66 | unsigned Index; |
67 | |
68 | public: |
69 | DynamicAllocLValue() : Index(0) {} |
70 | explicit DynamicAllocLValue(unsigned Index) : Index(Index + 1) {} |
71 | unsigned getIndex() { return Index - 1; } |
72 | |
73 | explicit operator bool() const { return Index != 0; } |
74 | |
75 | void *getOpaqueValue() { |
76 | return reinterpret_cast<void *>(static_cast<uintptr_t>(Index) |
77 | << NumLowBitsAvailable); |
78 | } |
79 | static DynamicAllocLValue getFromOpaqueValue(void *Value) { |
80 | DynamicAllocLValue V; |
81 | V.Index = reinterpret_cast<uintptr_t>(Value) >> NumLowBitsAvailable; |
82 | return V; |
83 | } |
84 | |
85 | static unsigned getMaxIndex() { |
86 | return (std::numeric_limits<unsigned>::max() >> NumLowBitsAvailable) - 1; |
87 | } |
88 | |
89 | static constexpr int NumLowBitsAvailable = 3; |
90 | }; |
91 | } |
92 | |
93 | namespace llvm { |
94 | template<> struct PointerLikeTypeTraits<clang::TypeInfoLValue> { |
95 | static void *getAsVoidPointer(clang::TypeInfoLValue V) { |
96 | return V.getOpaqueValue(); |
97 | } |
98 | static clang::TypeInfoLValue getFromVoidPointer(void *P) { |
99 | return clang::TypeInfoLValue::getFromOpaqueValue(P); |
100 | } |
101 | |
102 | |
103 | static constexpr int NumLowBitsAvailable = 3; |
104 | }; |
105 | |
106 | template<> struct PointerLikeTypeTraits<clang::DynamicAllocLValue> { |
107 | static void *getAsVoidPointer(clang::DynamicAllocLValue V) { |
108 | return V.getOpaqueValue(); |
109 | } |
110 | static clang::DynamicAllocLValue getFromVoidPointer(void *P) { |
111 | return clang::DynamicAllocLValue::getFromOpaqueValue(P); |
112 | } |
113 | static constexpr int NumLowBitsAvailable = |
114 | clang::DynamicAllocLValue::NumLowBitsAvailable; |
115 | }; |
116 | } |
117 | |
118 | namespace clang { |
119 | |
120 | |
121 | |
122 | class APValue { |
123 | typedef llvm::APFixedPoint APFixedPoint; |
124 | typedef llvm::APSInt APSInt; |
125 | typedef llvm::APFloat APFloat; |
126 | public: |
127 | enum ValueKind { |
128 | |
129 | None, |
130 | |
131 | Indeterminate, |
132 | Int, |
133 | Float, |
134 | FixedPoint, |
135 | ComplexInt, |
136 | ComplexFloat, |
137 | LValue, |
138 | Vector, |
139 | Array, |
140 | Struct, |
141 | Union, |
142 | MemberPointer, |
143 | AddrLabelDiff |
144 | }; |
145 | |
146 | class LValueBase { |
147 | typedef llvm::PointerUnion<const ValueDecl *, const Expr *, TypeInfoLValue, |
148 | DynamicAllocLValue> |
149 | PtrTy; |
150 | |
151 | public: |
152 | LValueBase() : Local{} {} |
153 | LValueBase(const ValueDecl *P, unsigned I = 0, unsigned V = 0); |
154 | LValueBase(const Expr *P, unsigned I = 0, unsigned V = 0); |
155 | static LValueBase getDynamicAlloc(DynamicAllocLValue LV, QualType Type); |
156 | static LValueBase getTypeInfo(TypeInfoLValue LV, QualType TypeInfo); |
157 | |
158 | void Profile(llvm::FoldingSetNodeID &ID) const; |
159 | |
160 | template <class T> |
161 | bool is() const { return Ptr.is<T>(); } |
162 | |
163 | template <class T> |
164 | T get() const { return Ptr.get<T>(); } |
165 | |
166 | template <class T> |
167 | T dyn_cast() const { return Ptr.dyn_cast<T>(); } |
168 | |
169 | void *getOpaqueValue() const; |
170 | |
171 | bool isNull() const; |
172 | |
173 | explicit operator bool() const; |
174 | |
175 | unsigned getCallIndex() const; |
176 | unsigned getVersion() const; |
177 | QualType getTypeInfoType() const; |
178 | QualType getDynamicAllocType() const; |
179 | |
180 | QualType getType() const; |
181 | |
182 | friend bool operator==(const LValueBase &LHS, const LValueBase &RHS); |
183 | friend bool operator!=(const LValueBase &LHS, const LValueBase &RHS) { |
184 | return !(LHS == RHS); |
185 | } |
186 | friend llvm::hash_code hash_value(const LValueBase &Base); |
187 | friend struct llvm::DenseMapInfo<LValueBase>; |
188 | |
189 | private: |
190 | PtrTy Ptr; |
191 | struct LocalState { |
192 | unsigned CallIndex, Version; |
193 | }; |
194 | union { |
195 | LocalState Local; |
196 | |
197 | void *TypeInfoType; |
198 | |
199 | void *DynamicAllocType; |
200 | }; |
201 | }; |
202 | |
203 | |
204 | |
205 | typedef llvm::PointerIntPair<const Decl *, 1, bool> BaseOrMemberType; |
206 | |
207 | |
208 | class LValuePathEntry { |
209 | static_assert(sizeof(uintptr_t) <= sizeof(uint64_t), |
210 | "pointer doesn't fit in 64 bits?"); |
211 | uint64_t Value; |
212 | |
213 | public: |
214 | LValuePathEntry() : Value() {} |
215 | LValuePathEntry(BaseOrMemberType BaseOrMember); |
216 | static LValuePathEntry ArrayIndex(uint64_t Index) { |
217 | LValuePathEntry Result; |
218 | Result.Value = Index; |
219 | return Result; |
220 | } |
221 | |
222 | BaseOrMemberType getAsBaseOrMember() const { |
223 | return BaseOrMemberType::getFromOpaqueValue( |
224 | reinterpret_cast<void *>(Value)); |
225 | } |
226 | uint64_t getAsArrayIndex() const { return Value; } |
227 | |
228 | void Profile(llvm::FoldingSetNodeID &ID) const; |
229 | |
230 | friend bool operator==(LValuePathEntry A, LValuePathEntry B) { |
231 | return A.Value == B.Value; |
232 | } |
233 | friend bool operator!=(LValuePathEntry A, LValuePathEntry B) { |
234 | return A.Value != B.Value; |
235 | } |
236 | friend llvm::hash_code hash_value(LValuePathEntry A) { |
237 | return llvm::hash_value(A.Value); |
238 | } |
239 | }; |
240 | class LValuePathSerializationHelper { |
241 | const void *ElemTy; |
242 | |
243 | public: |
244 | ArrayRef<LValuePathEntry> Path; |
245 | |
246 | LValuePathSerializationHelper(ArrayRef<LValuePathEntry>, QualType); |
247 | QualType getType(); |
248 | }; |
249 | struct NoLValuePath {}; |
250 | struct UninitArray {}; |
251 | struct UninitStruct {}; |
252 | |
253 | template <typename Impl> friend class clang::serialization::BasicReaderBase; |
254 | friend class ASTImporter; |
255 | friend class ASTNodeImporter; |
256 | |
257 | private: |
258 | ValueKind Kind; |
259 | |
260 | struct ComplexAPSInt { |
261 | APSInt Real, Imag; |
262 | ComplexAPSInt() : Real(1), Imag(1) {} |
263 | }; |
264 | struct ComplexAPFloat { |
265 | APFloat Real, Imag; |
266 | ComplexAPFloat() : Real(0.0), Imag(0.0) {} |
267 | }; |
268 | struct LV; |
269 | struct Vec { |
270 | APValue *Elts; |
271 | unsigned NumElts; |
272 | Vec() : Elts(nullptr), NumElts(0) {} |
273 | ~Vec() { delete[] Elts; } |
274 | }; |
275 | struct Arr { |
276 | APValue *Elts; |
277 | unsigned NumElts, ArrSize; |
278 | Arr(unsigned NumElts, unsigned ArrSize); |
279 | ~Arr(); |
280 | }; |
281 | struct StructData { |
282 | APValue *Elts; |
283 | unsigned NumBases; |
284 | unsigned NumFields; |
285 | StructData(unsigned NumBases, unsigned NumFields); |
286 | ~StructData(); |
287 | }; |
288 | struct UnionData { |
289 | const FieldDecl *Field; |
290 | APValue *Value; |
291 | UnionData(); |
292 | ~UnionData(); |
293 | }; |
294 | struct AddrLabelDiffData { |
295 | const AddrLabelExpr* LHSExpr; |
296 | const AddrLabelExpr* RHSExpr; |
297 | }; |
298 | struct MemberPointerData; |
299 | |
300 | |
301 | typedef llvm::AlignedCharArrayUnion<void *, APSInt, APFloat, ComplexAPSInt, |
302 | ComplexAPFloat, Vec, Arr, StructData, |
303 | UnionData, AddrLabelDiffData> DataType; |
304 | static const size_t DataSize = sizeof(DataType); |
305 | |
306 | DataType Data; |
307 | |
308 | public: |
309 | APValue() : Kind(None) {} |
310 | explicit APValue(APSInt I) : Kind(None) { |
311 | MakeInt(); setInt(std::move(I)); |
312 | } |
313 | explicit APValue(APFloat F) : Kind(None) { |
314 | MakeFloat(); setFloat(std::move(F)); |
315 | } |
316 | explicit APValue(APFixedPoint FX) : Kind(None) { |
317 | MakeFixedPoint(std::move(FX)); |
318 | } |
319 | explicit APValue(const APValue *E, unsigned N) : Kind(None) { |
320 | MakeVector(); setVector(E, N); |
321 | } |
322 | APValue(APSInt R, APSInt I) : Kind(None) { |
323 | MakeComplexInt(); setComplexInt(std::move(R), std::move(I)); |
324 | } |
325 | APValue(APFloat R, APFloat I) : Kind(None) { |
326 | MakeComplexFloat(); setComplexFloat(std::move(R), std::move(I)); |
327 | } |
328 | APValue(const APValue &RHS); |
329 | APValue(APValue &&RHS); |
330 | APValue(LValueBase B, const CharUnits &O, NoLValuePath N, |
331 | bool IsNullPtr = false) |
332 | : Kind(None) { |
333 | MakeLValue(); setLValue(B, O, N, IsNullPtr); |
334 | } |
335 | APValue(LValueBase B, const CharUnits &O, ArrayRef<LValuePathEntry> Path, |
336 | bool OnePastTheEnd, bool IsNullPtr = false) |
337 | : Kind(None) { |
338 | MakeLValue(); setLValue(B, O, Path, OnePastTheEnd, IsNullPtr); |
339 | } |
340 | APValue(UninitArray, unsigned InitElts, unsigned Size) : Kind(None) { |
341 | MakeArray(InitElts, Size); |
342 | } |
343 | APValue(UninitStruct, unsigned B, unsigned M) : Kind(None) { |
344 | MakeStruct(B, M); |
345 | } |
346 | explicit APValue(const FieldDecl *D, const APValue &V = APValue()) |
347 | : Kind(None) { |
348 | MakeUnion(); setUnion(D, V); |
349 | } |
350 | APValue(const ValueDecl *Member, bool IsDerivedMember, |
351 | ArrayRef<const CXXRecordDecl*> Path) : Kind(None) { |
352 | MakeMemberPointer(Member, IsDerivedMember, Path); |
353 | } |
354 | APValue(const AddrLabelExpr* LHSExpr, const AddrLabelExpr* RHSExpr) |
355 | : Kind(None) { |
356 | MakeAddrLabelDiff(); setAddrLabelDiff(LHSExpr, RHSExpr); |
357 | } |
358 | static APValue IndeterminateValue() { |
359 | APValue Result; |
360 | Result.Kind = Indeterminate; |
361 | return Result; |
362 | } |
363 | |
364 | APValue &operator=(const APValue &RHS); |
365 | APValue &operator=(APValue &&RHS); |
366 | |
367 | ~APValue() { |
368 | if (Kind != None && Kind != Indeterminate) |
369 | DestroyDataAndMakeUninit(); |
370 | } |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | |
377 | bool needsCleanup() const; |
378 | |
379 | |
380 | void swap(APValue &RHS); |
381 | |
382 | |
383 | |
384 | |
385 | void Profile(llvm::FoldingSetNodeID &ID) const; |
386 | |
387 | ValueKind getKind() const { return Kind; } |
388 | |
389 | bool isAbsent() const { return Kind == None; } |
390 | bool isIndeterminate() const { return Kind == Indeterminate; } |
391 | bool hasValue() const { return Kind != None && Kind != Indeterminate; } |
392 | |
393 | bool isInt() const { return Kind == Int; } |
394 | bool isFloat() const { return Kind == Float; } |
395 | bool isFixedPoint() const { return Kind == FixedPoint; } |
396 | bool isComplexInt() const { return Kind == ComplexInt; } |
397 | bool isComplexFloat() const { return Kind == ComplexFloat; } |
398 | bool isLValue() const { return Kind == LValue; } |
399 | bool isVector() const { return Kind == Vector; } |
400 | bool isArray() const { return Kind == Array; } |
401 | bool isStruct() const { return Kind == Struct; } |
402 | bool isUnion() const { return Kind == Union; } |
403 | bool isMemberPointer() const { return Kind == MemberPointer; } |
404 | bool isAddrLabelDiff() const { return Kind == AddrLabelDiff; } |
405 | |
406 | void dump() const; |
407 | void dump(raw_ostream &OS, const ASTContext &Context) const; |
408 | |
409 | void printPretty(raw_ostream &OS, const ASTContext &Ctx, QualType Ty) const; |
410 | void printPretty(raw_ostream &OS, const PrintingPolicy &Policy, QualType Ty, |
411 | const ASTContext *Ctx = nullptr) const; |
412 | |
413 | std::string getAsString(const ASTContext &Ctx, QualType Ty) const; |
414 | |
415 | APSInt &getInt() { |
416 | assert(isInt() && "Invalid accessor"); |
417 | return *(APSInt *)(char *)&Data; |
418 | } |
419 | const APSInt &getInt() const { |
420 | return const_cast<APValue*>(this)->getInt(); |
421 | } |
422 | |
423 | |
424 | |
425 | |
426 | bool toIntegralConstant(APSInt &Result, QualType SrcTy, |
427 | const ASTContext &Ctx) const; |
428 | |
429 | APFloat &getFloat() { |
430 | assert(isFloat() && "Invalid accessor"); |
431 | return *(APFloat *)(char *)&Data; |
432 | } |
433 | const APFloat &getFloat() const { |
434 | return const_cast<APValue*>(this)->getFloat(); |
435 | } |
436 | |
437 | APFixedPoint &getFixedPoint() { |
438 | assert(isFixedPoint() && "Invalid accessor"); |
439 | return *(APFixedPoint *)(char *)&Data; |
440 | } |
441 | const APFixedPoint &getFixedPoint() const { |
442 | return const_cast<APValue *>(this)->getFixedPoint(); |
443 | } |
444 | |
445 | APSInt &getComplexIntReal() { |
446 | assert(isComplexInt() && "Invalid accessor"); |
447 | return ((ComplexAPSInt *)(char *)&Data)->Real; |
448 | } |
449 | const APSInt &getComplexIntReal() const { |
450 | return const_cast<APValue*>(this)->getComplexIntReal(); |
451 | } |
452 | |
453 | APSInt &getComplexIntImag() { |
454 | assert(isComplexInt() && "Invalid accessor"); |
455 | return ((ComplexAPSInt *)(char *)&Data)->Imag; |
456 | } |
457 | const APSInt &getComplexIntImag() const { |
458 | return const_cast<APValue*>(this)->getComplexIntImag(); |
459 | } |
460 | |
461 | APFloat &getComplexFloatReal() { |
462 | assert(isComplexFloat() && "Invalid accessor"); |
463 | return ((ComplexAPFloat *)(char *)&Data)->Real; |
464 | } |
465 | const APFloat &getComplexFloatReal() const { |
466 | return const_cast<APValue*>(this)->getComplexFloatReal(); |
467 | } |
468 | |
469 | APFloat &getComplexFloatImag() { |
470 | assert(isComplexFloat() && "Invalid accessor"); |
471 | return ((ComplexAPFloat *)(char *)&Data)->Imag; |
472 | } |
473 | const APFloat &getComplexFloatImag() const { |
474 | return const_cast<APValue*>(this)->getComplexFloatImag(); |
475 | } |
476 | |
477 | const LValueBase getLValueBase() const; |
478 | CharUnits &getLValueOffset(); |
479 | const CharUnits &getLValueOffset() const { |
480 | return const_cast<APValue*>(this)->getLValueOffset(); |
481 | } |
482 | bool isLValueOnePastTheEnd() const; |
483 | bool hasLValuePath() const; |
484 | ArrayRef<LValuePathEntry> getLValuePath() const; |
485 | unsigned getLValueCallIndex() const; |
486 | unsigned getLValueVersion() const; |
487 | bool isNullPointer() const; |
488 | |
489 | APValue &getVectorElt(unsigned I) { |
490 | assert(isVector() && "Invalid accessor"); |
491 | assert(I < getVectorLength() && "Index out of range"); |
492 | return ((Vec *)(char *)&Data)->Elts[I]; |
493 | } |
494 | const APValue &getVectorElt(unsigned I) const { |
495 | return const_cast<APValue*>(this)->getVectorElt(I); |
496 | } |
497 | unsigned getVectorLength() const { |
498 | assert(isVector() && "Invalid accessor"); |
499 | return ((const Vec *)(const void *)&Data)->NumElts; |
500 | } |
501 | |
502 | APValue &getArrayInitializedElt(unsigned I) { |
503 | assert(isArray() && "Invalid accessor"); |
504 | assert(I < getArrayInitializedElts() && "Index out of range"); |
505 | return ((Arr *)(char *)&Data)->Elts[I]; |
506 | } |
507 | const APValue &getArrayInitializedElt(unsigned I) const { |
508 | return const_cast<APValue*>(this)->getArrayInitializedElt(I); |
509 | } |
510 | bool hasArrayFiller() const { |
511 | return getArrayInitializedElts() != getArraySize(); |
| 15 | | Assuming the condition is false | |
|
| 16 | | Returning zero, which participates in a condition later | |
|
512 | } |
513 | APValue &getArrayFiller() { |
514 | assert(isArray() && "Invalid accessor"); |
515 | assert(hasArrayFiller() && "No array filler"); |
516 | return ((Arr *)(char *)&Data)->Elts[getArrayInitializedElts()]; |
517 | } |
518 | const APValue &getArrayFiller() const { |
519 | return const_cast<APValue*>(this)->getArrayFiller(); |
520 | } |
521 | unsigned getArrayInitializedElts() const { |
522 | assert(isArray() && "Invalid accessor"); |
523 | return ((const Arr *)(const void *)&Data)->NumElts; |
524 | } |
525 | unsigned getArraySize() const { |
526 | assert(isArray() && "Invalid accessor"); |
527 | return ((const Arr *)(const void *)&Data)->ArrSize; |
528 | } |
529 | |
530 | unsigned getStructNumBases() const { |
531 | assert(isStruct() && "Invalid accessor"); |
532 | return ((const StructData *)(const char *)&Data)->NumBases; |
533 | } |
534 | unsigned getStructNumFields() const { |
535 | assert(isStruct() && "Invalid accessor"); |
536 | return ((const StructData *)(const char *)&Data)->NumFields; |
537 | } |
538 | APValue &getStructBase(unsigned i) { |
539 | assert(isStruct() && "Invalid accessor"); |
540 | assert(i < getStructNumBases() && "base class index OOB"); |
541 | return ((StructData *)(char *)&Data)->Elts[i]; |
542 | } |
543 | APValue &getStructField(unsigned i) { |
544 | assert(isStruct() && "Invalid accessor"); |
545 | assert(i < getStructNumFields() && "field index OOB"); |
546 | return ((StructData *)(char *)&Data)->Elts[getStructNumBases() + i]; |
547 | } |
548 | const APValue &getStructBase(unsigned i) const { |
549 | return const_cast<APValue*>(this)->getStructBase(i); |
550 | } |
551 | const APValue &getStructField(unsigned i) const { |
552 | return const_cast<APValue*>(this)->getStructField(i); |
553 | } |
554 | |
555 | const FieldDecl *getUnionField() const { |
556 | assert(isUnion() && "Invalid accessor"); |
557 | return ((const UnionData *)(const char *)&Data)->Field; |
558 | } |
559 | APValue &getUnionValue() { |
560 | assert(isUnion() && "Invalid accessor"); |
561 | return *((UnionData *)(char *)&Data)->Value; |
562 | } |
563 | const APValue &getUnionValue() const { |
564 | return const_cast<APValue*>(this)->getUnionValue(); |
565 | } |
566 | |
567 | const ValueDecl *getMemberPointerDecl() const; |
568 | bool isMemberPointerToDerivedMember() const; |
569 | ArrayRef<const CXXRecordDecl*> getMemberPointerPath() const; |
570 | |
571 | const AddrLabelExpr* getAddrLabelDiffLHS() const { |
572 | assert(isAddrLabelDiff() && "Invalid accessor"); |
573 | return ((const AddrLabelDiffData *)(const char *)&Data)->LHSExpr; |
574 | } |
575 | const AddrLabelExpr* getAddrLabelDiffRHS() const { |
576 | assert(isAddrLabelDiff() && "Invalid accessor"); |
577 | return ((const AddrLabelDiffData *)(const char *)&Data)->RHSExpr; |
578 | } |
579 | |
580 | void setInt(APSInt I) { |
581 | assert(isInt() && "Invalid accessor"); |
582 | *(APSInt *)(char *)&Data = std::move(I); |
583 | } |
584 | void setFloat(APFloat F) { |
585 | assert(isFloat() && "Invalid accessor"); |
586 | *(APFloat *)(char *)&Data = std::move(F); |
587 | } |
588 | void setFixedPoint(APFixedPoint FX) { |
589 | assert(isFixedPoint() && "Invalid accessor"); |
590 | *(APFixedPoint *)(char *)&Data = std::move(FX); |
591 | } |
592 | void setVector(const APValue *E, unsigned N) { |
593 | MutableArrayRef<APValue> InternalElts = setVectorUninit(N); |
594 | for (unsigned i = 0; i != N; ++i) |
595 | InternalElts[i] = E[i]; |
596 | } |
597 | void setComplexInt(APSInt R, APSInt I) { |
598 | assert(R.getBitWidth() == I.getBitWidth() && |
599 | "Invalid complex int (type mismatch)."); |
600 | assert(isComplexInt() && "Invalid accessor"); |
601 | ((ComplexAPSInt *)(char *)&Data)->Real = std::move(R); |
602 | ((ComplexAPSInt *)(char *)&Data)->Imag = std::move(I); |
603 | } |
604 | void setComplexFloat(APFloat R, APFloat I) { |
605 | assert(&R.getSemantics() == &I.getSemantics() && |
606 | "Invalid complex float (type mismatch)."); |
607 | assert(isComplexFloat() && "Invalid accessor"); |
608 | ((ComplexAPFloat *)(char *)&Data)->Real = std::move(R); |
609 | ((ComplexAPFloat *)(char *)&Data)->Imag = std::move(I); |
610 | } |
611 | void setLValue(LValueBase B, const CharUnits &O, NoLValuePath, |
612 | bool IsNullPtr); |
613 | void setLValue(LValueBase B, const CharUnits &O, |
614 | ArrayRef<LValuePathEntry> Path, bool OnePastTheEnd, |
615 | bool IsNullPtr); |
616 | void setUnion(const FieldDecl *Field, const APValue &Value); |
617 | void setAddrLabelDiff(const AddrLabelExpr* LHSExpr, |
618 | const AddrLabelExpr* RHSExpr) { |
619 | ((AddrLabelDiffData *)(char *)&Data)->LHSExpr = LHSExpr; |
620 | ((AddrLabelDiffData *)(char *)&Data)->RHSExpr = RHSExpr; |
621 | } |
622 | |
623 | private: |
624 | void DestroyDataAndMakeUninit(); |
625 | void MakeInt() { |
626 | assert(isAbsent() && "Bad state change"); |
627 | new ((void *)&Data) APSInt(1); |
628 | Kind = Int; |
629 | } |
630 | void MakeFloat() { |
631 | assert(isAbsent() && "Bad state change"); |
632 | new ((void *)(char *)&Data) APFloat(0.0); |
633 | Kind = Float; |
634 | } |
635 | void MakeFixedPoint(APFixedPoint &&FX) { |
636 | assert(isAbsent() && "Bad state change"); |
637 | new ((void *)(char *)&Data) APFixedPoint(std::move(FX)); |
638 | Kind = FixedPoint; |
639 | } |
640 | void MakeVector() { |
641 | assert(isAbsent() && "Bad state change"); |
642 | new ((void *)(char *)&Data) Vec(); |
643 | Kind = Vector; |
644 | } |
645 | void MakeComplexInt() { |
646 | assert(isAbsent() && "Bad state change"); |
647 | new ((void *)(char *)&Data) ComplexAPSInt(); |
648 | Kind = ComplexInt; |
649 | } |
650 | void MakeComplexFloat() { |
651 | assert(isAbsent() && "Bad state change"); |
652 | new ((void *)(char *)&Data) ComplexAPFloat(); |
653 | Kind = ComplexFloat; |
654 | } |
655 | void MakeLValue(); |
656 | void MakeArray(unsigned InitElts, unsigned Size); |
657 | void MakeStruct(unsigned B, unsigned M) { |
658 | assert(isAbsent() && "Bad state change"); |
659 | new ((void *)(char *)&Data) StructData(B, M); |
660 | Kind = Struct; |
661 | } |
662 | void MakeUnion() { |
663 | assert(isAbsent() && "Bad state change"); |
664 | new ((void *)(char *)&Data) UnionData(); |
665 | Kind = Union; |
666 | } |
667 | void MakeMemberPointer(const ValueDecl *Member, bool IsDerivedMember, |
668 | ArrayRef<const CXXRecordDecl*> Path); |
669 | void MakeAddrLabelDiff() { |
670 | assert(isAbsent() && "Bad state change"); |
671 | new ((void *)(char *)&Data) AddrLabelDiffData(); |
672 | Kind = AddrLabelDiff; |
673 | } |
674 | |
675 | private: |
676 | |
677 | |
678 | |
679 | MutableArrayRef<APValue> setVectorUninit(unsigned N) { |
680 | assert(isVector() && "Invalid accessor"); |
681 | Vec *V = ((Vec *)(char *)&Data); |
682 | V->Elts = new APValue[N]; |
683 | V->NumElts = N; |
684 | return {V->Elts, V->NumElts}; |
685 | } |
686 | MutableArrayRef<LValuePathEntry> |
687 | setLValueUninit(LValueBase B, const CharUnits &O, unsigned Size, |
688 | bool OnePastTheEnd, bool IsNullPtr); |
689 | MutableArrayRef<const CXXRecordDecl *> |
690 | setMemberPointerUninit(const ValueDecl *Member, bool IsDerivedMember, |
691 | unsigned Size); |
692 | }; |
693 | |
694 | } |
695 | |
696 | namespace llvm { |
697 | template<> struct DenseMapInfo<clang::APValue::LValueBase> { |
698 | static clang::APValue::LValueBase getEmptyKey(); |
699 | static clang::APValue::LValueBase getTombstoneKey(); |
700 | static unsigned getHashValue(const clang::APValue::LValueBase &Base); |
701 | static bool isEqual(const clang::APValue::LValueBase &LHS, |
702 | const clang::APValue::LValueBase &RHS); |
703 | }; |
704 | } |
705 | |
706 | #endif |