clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ExprConstant.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/libclangAST/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libclangAST/obj/../include/clang/AST -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/clang/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libclangAST/../include -I /usr/src/gnu/usr.bin/clang/libclangAST/obj -I /usr/src/gnu/usr.bin/clang/libclangAST/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/libclangAST/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/libclangAST/../../../llvm/clang/lib/AST/ExprConstant.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | #include "Interp/Context.h" |
36 | #include "Interp/Frame.h" |
37 | #include "Interp/State.h" |
38 | #include "clang/AST/APValue.h" |
39 | #include "clang/AST/ASTContext.h" |
40 | #include "clang/AST/ASTDiagnostic.h" |
41 | #include "clang/AST/ASTLambda.h" |
42 | #include "clang/AST/Attr.h" |
43 | #include "clang/AST/CXXInheritance.h" |
44 | #include "clang/AST/CharUnits.h" |
45 | #include "clang/AST/CurrentSourceLocExprScope.h" |
46 | #include "clang/AST/Expr.h" |
47 | #include "clang/AST/OSLog.h" |
48 | #include "clang/AST/OptionalDiagnostic.h" |
49 | #include "clang/AST/RecordLayout.h" |
50 | #include "clang/AST/StmtVisitor.h" |
51 | #include "clang/AST/TypeLoc.h" |
52 | #include "clang/Basic/Builtins.h" |
53 | #include "clang/Basic/TargetInfo.h" |
54 | #include "llvm/ADT/APFixedPoint.h" |
55 | #include "llvm/ADT/Optional.h" |
56 | #include "llvm/ADT/SmallBitVector.h" |
57 | #include "llvm/Support/Debug.h" |
58 | #include "llvm/Support/SaveAndRestore.h" |
59 | #include "llvm/Support/raw_ostream.h" |
60 | #include <cstring> |
61 | #include <functional> |
62 | |
63 | #define DEBUG_TYPE "exprconstant" |
64 | |
65 | using namespace clang; |
66 | using llvm::APFixedPoint; |
67 | using llvm::APInt; |
68 | using llvm::APSInt; |
69 | using llvm::APFloat; |
70 | using llvm::FixedPointSemantics; |
71 | using llvm::Optional; |
72 | |
73 | namespace { |
74 | struct LValue; |
75 | class CallStackFrame; |
76 | class EvalInfo; |
77 | |
78 | using SourceLocExprScopeGuard = |
79 | CurrentSourceLocExprScope::SourceLocExprScopeGuard; |
80 | |
81 | static QualType getType(APValue::LValueBase B) { |
82 | return B.getType(); |
83 | } |
84 | |
85 | |
86 | |
87 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
88 | return dyn_cast_or_null<FieldDecl>(E.getAsBaseOrMember().getPointer()); |
89 | } |
90 | |
91 | |
92 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
93 | return dyn_cast_or_null<CXXRecordDecl>(E.getAsBaseOrMember().getPointer()); |
94 | } |
95 | |
96 | |
97 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
98 | return E.getAsBaseOrMember().getInt(); |
99 | } |
100 | |
101 | |
102 | |
103 | static QualType getStorageType(const ASTContext &Ctx, const Expr *E) { |
104 | if (E->isPRValue()) |
105 | return E->getType(); |
106 | return Ctx.getLValueReferenceType(E->getType()); |
107 | } |
108 | |
109 | |
110 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
111 | if (const FunctionDecl *DirectCallee = CE->getDirectCallee()) |
112 | return DirectCallee->getAttr<AllocSizeAttr>(); |
113 | if (const Decl *IndirectCallee = CE->getCalleeDecl()) |
114 | return IndirectCallee->getAttr<AllocSizeAttr>(); |
115 | return nullptr; |
116 | } |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
123 | if (!E->getType()->isPointerType()) |
124 | return nullptr; |
125 | |
126 | E = E->IgnoreParens(); |
127 | |
128 | |
129 | |
130 | if (const auto *FE = dyn_cast<FullExpr>(E)) |
131 | E = FE->getSubExpr()->IgnoreParens(); |
132 | |
133 | if (const auto *Cast = dyn_cast<CastExpr>(E)) |
134 | E = Cast->getSubExpr()->IgnoreParens(); |
135 | |
136 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
137 | return getAllocSizeAttr(CE) ? CE : nullptr; |
138 | return nullptr; |
139 | } |
140 | |
141 | |
142 | |
143 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
144 | const auto *E = Base.dyn_cast<const Expr *>(); |
145 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
146 | } |
147 | |
148 | |
149 | |
150 | |
151 | static bool isForManglingOnly(ConstantExprKind Kind) { |
152 | switch (Kind) { |
153 | case ConstantExprKind::Normal: |
154 | case ConstantExprKind::ClassTemplateArgument: |
155 | case ConstantExprKind::ImmediateInvocation: |
156 | |
157 | |
158 | return false; |
159 | |
160 | case ConstantExprKind::NonClassTemplateArgument: |
161 | return true; |
162 | } |
163 | llvm_unreachable("unknown ConstantExprKind"); |
164 | } |
165 | |
166 | static bool isTemplateArgument(ConstantExprKind Kind) { |
167 | switch (Kind) { |
168 | case ConstantExprKind::Normal: |
169 | case ConstantExprKind::ImmediateInvocation: |
170 | return false; |
171 | |
172 | case ConstantExprKind::ClassTemplateArgument: |
173 | case ConstantExprKind::NonClassTemplateArgument: |
174 | return true; |
175 | } |
176 | llvm_unreachable("unknown ConstantExprKind"); |
177 | } |
178 | |
179 | |
180 | |
181 | |
182 | static const uint64_t AssumedSizeForUnsizedArray = |
183 | std::numeric_limits<uint64_t>::max() / 2; |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | static unsigned |
190 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
191 | ArrayRef<APValue::LValuePathEntry> Path, |
192 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
193 | bool &FirstEntryIsUnsizedArray) { |
194 | |
195 | |
196 | assert(!isBaseAnAllocSizeCall(Base) && |
197 | "Unsized arrays shouldn't appear here"); |
198 | unsigned MostDerivedLength = 0; |
199 | Type = getType(Base); |
200 | |
201 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
202 | if (Type->isArrayType()) { |
203 | const ArrayType *AT = Ctx.getAsArrayType(Type); |
204 | Type = AT->getElementType(); |
205 | MostDerivedLength = I + 1; |
206 | IsArray = true; |
207 | |
208 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
209 | ArraySize = CAT->getSize().getZExtValue(); |
210 | } else { |
211 | assert(I == 0 && "unexpected unsized array designator"); |
212 | FirstEntryIsUnsizedArray = true; |
213 | ArraySize = AssumedSizeForUnsizedArray; |
214 | } |
215 | } else if (Type->isAnyComplexType()) { |
216 | const ComplexType *CT = Type->castAs<ComplexType>(); |
217 | Type = CT->getElementType(); |
218 | ArraySize = 2; |
219 | MostDerivedLength = I + 1; |
220 | IsArray = true; |
221 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
222 | Type = FD->getType(); |
223 | ArraySize = 0; |
224 | MostDerivedLength = I + 1; |
225 | IsArray = false; |
226 | } else { |
227 | |
228 | ArraySize = 0; |
229 | IsArray = false; |
230 | } |
231 | } |
232 | return MostDerivedLength; |
233 | } |
234 | |
235 | |
236 | struct SubobjectDesignator { |
237 | |
238 | |
239 | |
240 | unsigned Invalid : 1; |
241 | |
242 | |
243 | unsigned IsOnePastTheEnd : 1; |
244 | |
245 | |
246 | unsigned FirstEntryIsAnUnsizedArray : 1; |
247 | |
248 | |
249 | unsigned MostDerivedIsArrayElement : 1; |
250 | |
251 | |
252 | |
253 | unsigned MostDerivedPathLength : 28; |
254 | |
255 | |
256 | |
257 | |
258 | |
259 | |
260 | |
261 | |
262 | uint64_t MostDerivedArraySize; |
263 | |
264 | |
265 | QualType MostDerivedType; |
266 | |
267 | typedef APValue::LValuePathEntry PathEntry; |
268 | |
269 | |
270 | SmallVector<PathEntry, 8> Entries; |
271 | |
272 | SubobjectDesignator() : Invalid(true) {} |
273 | |
274 | explicit SubobjectDesignator(QualType T) |
275 | : Invalid(false), IsOnePastTheEnd(false), |
276 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
277 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
278 | MostDerivedType(T) {} |
279 | |
280 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
281 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
282 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
283 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
284 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
285 | if (!Invalid) { |
286 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
287 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
288 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
289 | if (V.getLValueBase()) { |
290 | bool IsArray = false; |
291 | bool FirstIsUnsizedArray = false; |
292 | MostDerivedPathLength = findMostDerivedSubobject( |
293 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
294 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
295 | MostDerivedIsArrayElement = IsArray; |
296 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
297 | } |
298 | } |
299 | } |
300 | |
301 | void truncate(ASTContext &Ctx, APValue::LValueBase Base, |
302 | unsigned NewLength) { |
303 | if (Invalid) |
304 | return; |
305 | |
306 | assert(Base && "cannot truncate path for null pointer"); |
307 | assert(NewLength <= Entries.size() && "not a truncation"); |
308 | |
309 | if (NewLength == Entries.size()) |
310 | return; |
311 | Entries.resize(NewLength); |
312 | |
313 | bool IsArray = false; |
314 | bool FirstIsUnsizedArray = false; |
315 | MostDerivedPathLength = findMostDerivedSubobject( |
316 | Ctx, Base, Entries, MostDerivedArraySize, MostDerivedType, IsArray, |
317 | FirstIsUnsizedArray); |
318 | MostDerivedIsArrayElement = IsArray; |
319 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
320 | } |
321 | |
322 | void setInvalid() { |
323 | Invalid = true; |
324 | Entries.clear(); |
325 | } |
326 | |
327 | |
328 | |
329 | bool isMostDerivedAnUnsizedArray() const { |
330 | assert(!Invalid && "Calling this makes no sense on invalid designators"); |
331 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
332 | } |
333 | |
334 | |
335 | |
336 | uint64_t getMostDerivedArraySize() const { |
337 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
338 | return MostDerivedArraySize; |
339 | } |
340 | |
341 | |
342 | bool isOnePastTheEnd() const { |
343 | assert(!Invalid); |
344 | if (IsOnePastTheEnd) |
345 | return true; |
346 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
347 | Entries[MostDerivedPathLength - 1].getAsArrayIndex() == |
348 | MostDerivedArraySize) |
349 | return true; |
350 | return false; |
351 | } |
352 | |
353 | |
354 | |
355 | |
356 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
357 | if (Invalid || isMostDerivedAnUnsizedArray()) |
358 | return {0, 0}; |
359 | |
360 | |
361 | |
362 | |
363 | bool IsArray = MostDerivedPathLength == Entries.size() && |
364 | MostDerivedIsArrayElement; |
365 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
366 | : (uint64_t)IsOnePastTheEnd; |
367 | uint64_t ArraySize = |
368 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
369 | return {ArrayIndex, ArraySize - ArrayIndex}; |
370 | } |
371 | |
372 | |
373 | bool isValidSubobject() const { |
374 | if (Invalid) |
375 | return false; |
376 | return !isOnePastTheEnd(); |
377 | } |
378 | |
379 | |
380 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
381 | |
382 | |
383 | QualType getType(ASTContext &Ctx) const { |
384 | assert(!Invalid && "invalid designator has no subobject type"); |
385 | return MostDerivedPathLength == Entries.size() |
386 | ? MostDerivedType |
387 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
388 | } |
389 | |
390 | |
391 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
392 | Entries.push_back(PathEntry::ArrayIndex(0)); |
393 | |
394 | |
395 | MostDerivedType = CAT->getElementType(); |
396 | MostDerivedIsArrayElement = true; |
397 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
398 | MostDerivedPathLength = Entries.size(); |
399 | } |
400 | |
401 | |
402 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
403 | Entries.push_back(PathEntry::ArrayIndex(0)); |
404 | |
405 | MostDerivedType = ElemTy; |
406 | MostDerivedIsArrayElement = true; |
407 | |
408 | |
409 | |
410 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
411 | MostDerivedPathLength = Entries.size(); |
412 | } |
413 | |
414 | |
415 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
416 | Entries.push_back(APValue::BaseOrMemberType(D, Virtual)); |
417 | |
418 | |
419 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
420 | MostDerivedType = FD->getType(); |
421 | MostDerivedIsArrayElement = false; |
422 | MostDerivedArraySize = 0; |
423 | MostDerivedPathLength = Entries.size(); |
424 | } |
425 | } |
426 | |
427 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
428 | Entries.push_back(PathEntry::ArrayIndex(Imag)); |
429 | |
430 | |
431 | |
432 | MostDerivedType = EltTy; |
433 | MostDerivedIsArrayElement = true; |
434 | MostDerivedArraySize = 2; |
435 | MostDerivedPathLength = Entries.size(); |
436 | } |
437 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
438 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
439 | const APSInt &N); |
440 | |
441 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
442 | if (Invalid || !N) return; |
443 | uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue(); |
444 | if (isMostDerivedAnUnsizedArray()) { |
445 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
446 | |
447 | |
448 | |
449 | Entries.back() = PathEntry::ArrayIndex( |
450 | Entries.back().getAsArrayIndex() + TruncatedN); |
451 | return; |
452 | } |
453 | |
454 | |
455 | |
456 | |
457 | bool IsArray = MostDerivedPathLength == Entries.size() && |
458 | MostDerivedIsArrayElement; |
459 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
460 | : (uint64_t)IsOnePastTheEnd; |
461 | uint64_t ArraySize = |
462 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
463 | |
464 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
465 | |
466 | |
467 | N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); |
468 | (llvm::APInt&)N += ArrayIndex; |
469 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
470 | diagnosePointerArithmetic(Info, E, N); |
471 | setInvalid(); |
472 | return; |
473 | } |
474 | |
475 | ArrayIndex += TruncatedN; |
476 | assert(ArrayIndex <= ArraySize && |
477 | "bounds check succeeded for out-of-bounds index"); |
478 | |
479 | if (IsArray) |
480 | Entries.back() = PathEntry::ArrayIndex(ArrayIndex); |
481 | else |
482 | IsOnePastTheEnd = (ArrayIndex != 0); |
483 | } |
484 | }; |
485 | |
486 | |
487 | enum class ScopeKind { |
488 | Block, |
489 | FullExpression, |
490 | Call |
491 | }; |
492 | |
493 | |
494 | struct CallRef { |
495 | CallRef() : OrigCallee(), CallIndex(0), Version() {} |
496 | CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version) |
497 | : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {} |
498 | |
499 | explicit operator bool() const { return OrigCallee; } |
500 | |
501 | |
502 | |
503 | const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const { |
504 | return OrigCallee ? OrigCallee->getParamDecl(PVD->getFunctionScopeIndex()) |
505 | : PVD; |
506 | } |
507 | |
508 | |
509 | |
510 | |
511 | |
512 | const FunctionDecl *OrigCallee; |
513 | |
514 | unsigned CallIndex; |
515 | |
516 | unsigned Version; |
517 | }; |
518 | |
519 | |
520 | class CallStackFrame : public interp::Frame { |
521 | public: |
522 | EvalInfo &Info; |
523 | |
524 | |
525 | CallStackFrame *Caller; |
526 | |
527 | |
528 | const FunctionDecl *Callee; |
529 | |
530 | |
531 | const LValue *This; |
532 | |
533 | |
534 | |
535 | |
536 | CallRef Arguments; |
537 | |
538 | |
539 | |
540 | CurrentSourceLocExprScope CurSourceLocExprScope; |
541 | |
542 | |
543 | |
544 | typedef std::pair<const void *, unsigned> MapKeyTy; |
545 | typedef std::map<MapKeyTy, APValue> MapTy; |
546 | |
547 | MapTy Temporaries; |
548 | |
549 | |
550 | SourceLocation CallLoc; |
551 | |
552 | |
553 | unsigned Index; |
554 | |
555 | |
556 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
557 | unsigned CurTempVersion = TempVersionStack.back(); |
558 | |
559 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
560 | |
561 | void pushTempVersion() { |
562 | TempVersionStack.push_back(++CurTempVersion); |
563 | } |
564 | |
565 | void popTempVersion() { |
566 | TempVersionStack.pop_back(); |
567 | } |
568 | |
569 | CallRef createCall(const FunctionDecl *Callee) { |
570 | return {Callee, Index, ++CurTempVersion}; |
571 | } |
572 | |
573 | |
574 | |
575 | |
576 | |
577 | |
578 | |
579 | |
580 | |
581 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
582 | FieldDecl *LambdaThisCaptureField; |
583 | |
584 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
585 | const FunctionDecl *Callee, const LValue *This, |
586 | CallRef Arguments); |
587 | ~CallStackFrame(); |
588 | |
589 | |
590 | APValue *getTemporary(const void *Key, unsigned Version) { |
591 | MapKeyTy KV(Key, Version); |
592 | auto LB = Temporaries.lower_bound(KV); |
593 | if (LB != Temporaries.end() && LB->first == KV) |
| 60 | | Assuming the condition is true | |
|
| |
594 | return &LB->second; |
| 62 | | Returning pointer, which participates in a condition later | |
|
595 | |
596 | |
597 | assert((LB == Temporaries.end() || LB->first.first != Key) && |
598 | (LB == Temporaries.begin() || std::prev(LB)->first.first != Key) && |
599 | "Element with key 'Key' found in map"); |
600 | return nullptr; |
601 | } |
602 | |
603 | |
604 | APValue *getCurrentTemporary(const void *Key) { |
605 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
606 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
607 | return &std::prev(UB)->second; |
608 | return nullptr; |
609 | } |
610 | |
611 | |
612 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
613 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
614 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
615 | return std::prev(UB)->first.second; |
616 | return 0; |
617 | } |
618 | |
619 | |
620 | |
621 | |
622 | |
623 | template<typename KeyT> |
624 | APValue &createTemporary(const KeyT *Key, QualType T, |
625 | ScopeKind Scope, LValue &LV); |
626 | |
627 | |
628 | APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV); |
629 | |
630 | void describe(llvm::raw_ostream &OS) override; |
631 | |
632 | Frame *getCaller() const override { return Caller; } |
633 | SourceLocation getCallLocation() const override { return CallLoc; } |
634 | const FunctionDecl *getCallee() const override { return Callee; } |
635 | |
636 | bool isStdFunction() const { |
637 | for (const DeclContext *DC = Callee; DC; DC = DC->getParent()) |
638 | if (DC->isStdNamespace()) |
639 | return true; |
640 | return false; |
641 | } |
642 | |
643 | private: |
644 | APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T, |
645 | ScopeKind Scope); |
646 | }; |
647 | |
648 | |
649 | class ThisOverrideRAII { |
650 | public: |
651 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
652 | : Frame(Frame), OldThis(Frame.This) { |
653 | if (Enable) |
654 | Frame.This = NewThis; |
655 | } |
656 | ~ThisOverrideRAII() { |
657 | Frame.This = OldThis; |
658 | } |
659 | private: |
660 | CallStackFrame &Frame; |
661 | const LValue *OldThis; |
662 | }; |
663 | } |
664 | |
665 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
666 | const LValue &This, QualType ThisType); |
667 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
668 | APValue::LValueBase LVBase, APValue &Value, |
669 | QualType T); |
670 | |
671 | namespace { |
672 | |
673 | class Cleanup { |
674 | llvm::PointerIntPair<APValue*, 2, ScopeKind> Value; |
675 | APValue::LValueBase Base; |
676 | QualType T; |
677 | |
678 | public: |
679 | Cleanup(APValue *Val, APValue::LValueBase Base, QualType T, |
680 | ScopeKind Scope) |
681 | : Value(Val, Scope), Base(Base), T(T) {} |
682 | |
683 | |
684 | |
685 | bool isDestroyedAtEndOf(ScopeKind K) const { |
686 | return (int)Value.getInt() >= (int)K; |
687 | } |
688 | bool endLifetime(EvalInfo &Info, bool RunDestructors) { |
689 | if (RunDestructors) { |
690 | SourceLocation Loc; |
691 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
692 | Loc = VD->getLocation(); |
693 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
694 | Loc = E->getExprLoc(); |
695 | return HandleDestruction(Info, Loc, Base, *Value.getPointer(), T); |
696 | } |
697 | *Value.getPointer() = APValue(); |
698 | return true; |
699 | } |
700 | |
701 | bool hasSideEffect() { |
702 | return T.isDestructedType(); |
703 | } |
704 | }; |
705 | |
706 | |
707 | struct ObjectUnderConstruction { |
708 | APValue::LValueBase Base; |
709 | ArrayRef<APValue::LValuePathEntry> Path; |
710 | friend bool operator==(const ObjectUnderConstruction &LHS, |
711 | const ObjectUnderConstruction &RHS) { |
712 | return LHS.Base == RHS.Base && LHS.Path == RHS.Path; |
713 | } |
714 | friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) { |
715 | return llvm::hash_combine(Obj.Base, Obj.Path); |
716 | } |
717 | }; |
718 | enum class ConstructionPhase { |
719 | None, |
720 | Bases, |
721 | AfterBases, |
722 | AfterFields, |
723 | Destroying, |
724 | DestroyingBases |
725 | }; |
726 | } |
727 | |
728 | namespace llvm { |
729 | template<> struct DenseMapInfo<ObjectUnderConstruction> { |
730 | using Base = DenseMapInfo<APValue::LValueBase>; |
731 | static ObjectUnderConstruction getEmptyKey() { |
732 | return {Base::getEmptyKey(), {}}; } |
733 | static ObjectUnderConstruction getTombstoneKey() { |
734 | return {Base::getTombstoneKey(), {}}; |
735 | } |
736 | static unsigned getHashValue(const ObjectUnderConstruction &Object) { |
737 | return hash_value(Object); |
738 | } |
739 | static bool isEqual(const ObjectUnderConstruction &LHS, |
740 | const ObjectUnderConstruction &RHS) { |
741 | return LHS == RHS; |
742 | } |
743 | }; |
744 | } |
745 | |
746 | namespace { |
747 | |
748 | struct DynAlloc { |
749 | |
750 | APValue Value; |
751 | |
752 | |
753 | |
754 | const Expr *AllocExpr = nullptr; |
755 | |
756 | enum Kind { |
757 | New, |
758 | ArrayNew, |
759 | StdAllocator |
760 | }; |
761 | |
762 | |
763 | |
764 | Kind getKind() const { |
765 | if (auto *NE = dyn_cast<CXXNewExpr>(AllocExpr)) |
766 | return NE->isArray() ? ArrayNew : New; |
767 | assert(isa<CallExpr>(AllocExpr)); |
768 | return StdAllocator; |
769 | } |
770 | }; |
771 | |
772 | struct DynAllocOrder { |
773 | bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const { |
774 | return L.getIndex() < R.getIndex(); |
775 | } |
776 | }; |
777 | |
778 | |
779 | |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | class EvalInfo : public interp::State { |
793 | public: |
794 | ASTContext &Ctx; |
795 | |
796 | |
797 | Expr::EvalStatus &EvalStatus; |
798 | |
799 | |
800 | CallStackFrame *CurrentCall; |
801 | |
802 | |
803 | unsigned CallStackDepth; |
804 | |
805 | |
806 | unsigned NextCallIndex; |
807 | |
808 | |
809 | |
810 | |
811 | unsigned StepsLeft; |
812 | |
813 | |
814 | |
815 | bool EnableNewConstInterp; |
816 | |
817 | |
818 | |
819 | CallStackFrame BottomFrame; |
820 | |
821 | |
822 | |
823 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
824 | |
825 | |
826 | |
827 | APValue::LValueBase EvaluatingDecl; |
828 | |
829 | enum class EvaluatingDeclKind { |
830 | None, |
831 | |
832 | Ctor, |
833 | |
834 | Dtor, |
835 | }; |
836 | EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None; |
837 | |
838 | |
839 | |
840 | APValue *EvaluatingDeclValue; |
841 | |
842 | |
843 | llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase> |
844 | ObjectsUnderConstruction; |
845 | |
846 | |
847 | |
848 | |
849 | std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs; |
850 | |
851 | |
852 | unsigned NumHeapAllocs = 0; |
853 | |
854 | struct EvaluatingConstructorRAII { |
855 | EvalInfo &EI; |
856 | ObjectUnderConstruction Object; |
857 | bool DidInsert; |
858 | EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object, |
859 | bool HasBases) |
860 | : EI(EI), Object(Object) { |
861 | DidInsert = |
862 | EI.ObjectsUnderConstruction |
863 | .insert({Object, HasBases ? ConstructionPhase::Bases |
864 | : ConstructionPhase::AfterBases}) |
865 | .second; |
866 | } |
867 | void finishedConstructingBases() { |
868 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases; |
869 | } |
870 | void finishedConstructingFields() { |
871 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields; |
872 | } |
873 | ~EvaluatingConstructorRAII() { |
874 | if (DidInsert) EI.ObjectsUnderConstruction.erase(Object); |
875 | } |
876 | }; |
877 | |
878 | struct EvaluatingDestructorRAII { |
879 | EvalInfo &EI; |
880 | ObjectUnderConstruction Object; |
881 | bool DidInsert; |
882 | EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object) |
883 | : EI(EI), Object(Object) { |
884 | DidInsert = EI.ObjectsUnderConstruction |
885 | .insert({Object, ConstructionPhase::Destroying}) |
886 | .second; |
887 | } |
888 | void startedDestroyingBases() { |
889 | EI.ObjectsUnderConstruction[Object] = |
890 | ConstructionPhase::DestroyingBases; |
891 | } |
892 | ~EvaluatingDestructorRAII() { |
893 | if (DidInsert) |
894 | EI.ObjectsUnderConstruction.erase(Object); |
895 | } |
896 | }; |
897 | |
898 | ConstructionPhase |
899 | isEvaluatingCtorDtor(APValue::LValueBase Base, |
900 | ArrayRef<APValue::LValuePathEntry> Path) { |
901 | return ObjectsUnderConstruction.lookup({Base, Path}); |
902 | } |
903 | |
904 | |
905 | |
906 | unsigned SpeculativeEvaluationDepth = 0; |
907 | |
908 | |
909 | |
910 | uint64_t ArrayInitIndex = -1; |
911 | |
912 | |
913 | |
914 | bool HasActiveDiagnostic; |
915 | |
916 | |
917 | |
918 | bool HasFoldFailureDiagnostic; |
919 | |
920 | |
921 | |
922 | bool InConstantContext; |
923 | |
924 | |
925 | |
926 | |
927 | bool CheckingPotentialConstantExpression = false; |
928 | |
929 | |
930 | |
931 | |
932 | |
933 | |
934 | |
935 | bool CheckingForUndefinedBehavior = false; |
936 | |
937 | enum EvaluationMode { |
938 | |
939 | |
940 | EM_ConstantExpression, |
941 | |
942 | |
943 | |
944 | |
945 | |
946 | |
947 | EM_ConstantExpressionUnevaluated, |
948 | |
949 | |
950 | |
951 | EM_ConstantFold, |
952 | |
953 | |
954 | |
955 | EM_IgnoreSideEffects, |
956 | } EvalMode; |
957 | |
958 | |
959 | |
960 | bool checkingPotentialConstantExpression() const override { |
961 | return CheckingPotentialConstantExpression; |
| 19 | | Returning the value 1, which participates in a condition later | |
|
962 | } |
963 | |
964 | |
965 | |
966 | |
967 | bool checkingForUndefinedBehavior() const override { |
968 | return CheckingForUndefinedBehavior; |
969 | } |
970 | |
971 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
972 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
973 | CallStackDepth(0), NextCallIndex(1), |
974 | StepsLeft(C.getLangOpts().ConstexprStepLimit), |
975 | EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp), |
976 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, CallRef()), |
| 4 | | Calling constructor for 'CallStackFrame' | |
|
| 5 | | Returning from constructor for 'CallStackFrame' | |
|
977 | EvaluatingDecl((const ValueDecl *)nullptr), |
978 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
979 | HasFoldFailureDiagnostic(false), InConstantContext(false), |
980 | EvalMode(Mode) {} |
981 | |
982 | ~EvalInfo() { |
983 | discardCleanups(); |
984 | } |
985 | |
986 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value, |
987 | EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) { |
988 | EvaluatingDecl = Base; |
989 | IsEvaluatingDecl = EDK; |
990 | EvaluatingDeclValue = &Value; |
991 | } |
992 | |
993 | bool CheckCallLimit(SourceLocation Loc) { |
994 | |
995 | |
996 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
| 18 | | Calling 'EvalInfo::checkingPotentialConstantExpression' | |
|
| 20 | | Returning from 'EvalInfo::checkingPotentialConstantExpression' | |
|
| 21 | | Assuming field 'CallStackDepth' is <= 1 | |
|
| |
997 | return false; |
998 | if (NextCallIndex == 0) { |
| 23 | | Assuming field 'NextCallIndex' is not equal to 0 | |
|
| |
999 | |
1000 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
1001 | return false; |
1002 | } |
1003 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
| 25 | | Assuming field 'CallStackDepth' is <= field 'ConstexprCallDepth' | |
|
| |
1004 | return true; |
| 27 | | Returning the value 1, which participates in a condition later | |
|
1005 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
1006 | << getLangOpts().ConstexprCallDepth; |
1007 | return false; |
1008 | } |
1009 | |
1010 | std::pair<CallStackFrame *, unsigned> |
1011 | getCallFrameAndDepth(unsigned CallIndex) { |
1012 | assert(CallIndex && "no call index in getCallFrameAndDepth"); |
1013 | |
1014 | |
1015 | unsigned Depth = CallStackDepth; |
1016 | CallStackFrame *Frame = CurrentCall; |
1017 | while (Frame->Index > CallIndex) { |
1018 | Frame = Frame->Caller; |
1019 | --Depth; |
1020 | } |
1021 | if (Frame->Index == CallIndex) |
1022 | return {Frame, Depth}; |
1023 | return {nullptr, 0}; |
1024 | } |
1025 | |
1026 | bool nextStep(const Stmt *S) { |
1027 | if (!StepsLeft) { |
1028 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
1029 | return false; |
1030 | } |
1031 | --StepsLeft; |
1032 | return true; |
1033 | } |
1034 | |
1035 | APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV); |
1036 | |
1037 | Optional<DynAlloc*> lookupDynamicAlloc(DynamicAllocLValue DA) { |
1038 | Optional<DynAlloc*> Result; |
1039 | auto It = HeapAllocs.find(DA); |
1040 | if (It != HeapAllocs.end()) |
1041 | Result = &It->second; |
1042 | return Result; |
1043 | } |
1044 | |
1045 | |
1046 | APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) { |
1047 | CallStackFrame *Frame = getCallFrameAndDepth(Call.CallIndex).first; |
1048 | return Frame ? Frame->getTemporary(Call.getOrigParam(PVD), Call.Version) |
| |
| 59 | | Calling 'CallStackFrame::getTemporary' | |
|
| 63 | | Returning from 'CallStackFrame::getTemporary' | |
|
| 64 | | Returning pointer, which participates in a condition later | |
|
1049 | : nullptr; |
1050 | } |
1051 | |
1052 | |
1053 | struct StdAllocatorCaller { |
1054 | unsigned FrameIndex; |
1055 | QualType ElemType; |
1056 | explicit operator bool() const { return FrameIndex != 0; }; |
1057 | }; |
1058 | |
1059 | StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const { |
1060 | for (const CallStackFrame *Call = CurrentCall; Call != &BottomFrame; |
1061 | Call = Call->Caller) { |
1062 | const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Call->Callee); |
1063 | if (!MD) |
1064 | continue; |
1065 | const IdentifierInfo *FnII = MD->getIdentifier(); |
1066 | if (!FnII || !FnII->isStr(FnName)) |
1067 | continue; |
1068 | |
1069 | const auto *CTSD = |
1070 | dyn_cast<ClassTemplateSpecializationDecl>(MD->getParent()); |
1071 | if (!CTSD) |
1072 | continue; |
1073 | |
1074 | const IdentifierInfo *ClassII = CTSD->getIdentifier(); |
1075 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
1076 | if (CTSD->isInStdNamespace() && ClassII && |
1077 | ClassII->isStr("allocator") && TAL.size() >= 1 && |
1078 | TAL[0].getKind() == TemplateArgument::Type) |
1079 | return {Call->Index, TAL[0].getAsType()}; |
1080 | } |
1081 | |
1082 | return {}; |
1083 | } |
1084 | |
1085 | void performLifetimeExtension() { |
1086 | |
1087 | CleanupStack.erase(std::remove_if(CleanupStack.begin(), |
1088 | CleanupStack.end(), |
1089 | [](Cleanup &C) { |
1090 | return !C.isDestroyedAtEndOf( |
1091 | ScopeKind::FullExpression); |
1092 | }), |
1093 | CleanupStack.end()); |
1094 | } |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | bool discardCleanups() { |
1100 | for (Cleanup &C : CleanupStack) { |
1101 | if (C.hasSideEffect() && !noteSideEffect()) { |
1102 | CleanupStack.clear(); |
1103 | return false; |
1104 | } |
1105 | } |
1106 | CleanupStack.clear(); |
1107 | return true; |
1108 | } |
1109 | |
1110 | private: |
1111 | interp::Frame *getCurrentFrame() override { return CurrentCall; } |
1112 | const interp::Frame *getBottomFrame() const override { return &BottomFrame; } |
1113 | |
1114 | bool hasActiveDiagnostic() override { return HasActiveDiagnostic; } |
1115 | void setActiveDiagnostic(bool Flag) override { HasActiveDiagnostic = Flag; } |
1116 | |
1117 | void setFoldFailureDiagnostic(bool Flag) override { |
1118 | HasFoldFailureDiagnostic = Flag; |
1119 | } |
1120 | |
1121 | Expr::EvalStatus &getEvalStatus() const override { return EvalStatus; } |
1122 | |
1123 | ASTContext &getCtx() const override { return Ctx; } |
1124 | |
1125 | |
1126 | |
1127 | |
1128 | |
1129 | |
1130 | |
1131 | bool hasPriorDiagnostic() override { |
1132 | if (!EvalStatus.Diag->empty()) { |
1133 | switch (EvalMode) { |
1134 | case EM_ConstantFold: |
1135 | case EM_IgnoreSideEffects: |
1136 | if (!HasFoldFailureDiagnostic) |
1137 | break; |
1138 | |
1139 | LLVM_FALLTHROUGH; |
1140 | case EM_ConstantExpression: |
1141 | case EM_ConstantExpressionUnevaluated: |
1142 | setActiveDiagnostic(false); |
1143 | return true; |
1144 | } |
1145 | } |
1146 | return false; |
1147 | } |
1148 | |
1149 | unsigned getCallStackDepth() override { return CallStackDepth; } |
1150 | |
1151 | public: |
1152 | |
1153 | |
1154 | bool keepEvaluatingAfterSideEffect() { |
1155 | switch (EvalMode) { |
1156 | case EM_IgnoreSideEffects: |
1157 | return true; |
1158 | |
1159 | case EM_ConstantExpression: |
1160 | case EM_ConstantExpressionUnevaluated: |
1161 | case EM_ConstantFold: |
1162 | |
1163 | |
1164 | return checkingPotentialConstantExpression() || |
1165 | checkingForUndefinedBehavior(); |
1166 | } |
1167 | llvm_unreachable("Missed EvalMode case"); |
1168 | } |
1169 | |
1170 | |
1171 | |
1172 | bool noteSideEffect() { |
1173 | EvalStatus.HasSideEffects = true; |
1174 | return keepEvaluatingAfterSideEffect(); |
1175 | } |
1176 | |
1177 | |
1178 | bool keepEvaluatingAfterUndefinedBehavior() { |
1179 | switch (EvalMode) { |
1180 | case EM_IgnoreSideEffects: |
1181 | case EM_ConstantFold: |
1182 | return true; |
1183 | |
1184 | case EM_ConstantExpression: |
1185 | case EM_ConstantExpressionUnevaluated: |
1186 | return checkingForUndefinedBehavior(); |
1187 | } |
1188 | llvm_unreachable("Missed EvalMode case"); |
1189 | } |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | bool noteUndefinedBehavior() override { |
1195 | EvalStatus.HasUndefinedBehavior = true; |
1196 | return keepEvaluatingAfterUndefinedBehavior(); |
1197 | } |
1198 | |
1199 | |
1200 | |
1201 | bool keepEvaluatingAfterFailure() const override { |
1202 | if (!StepsLeft) |
1203 | return false; |
1204 | |
1205 | switch (EvalMode) { |
1206 | case EM_ConstantExpression: |
1207 | case EM_ConstantExpressionUnevaluated: |
1208 | case EM_ConstantFold: |
1209 | case EM_IgnoreSideEffects: |
1210 | return checkingPotentialConstantExpression() || |
1211 | checkingForUndefinedBehavior(); |
1212 | } |
1213 | llvm_unreachable("Missed EvalMode case"); |
1214 | } |
1215 | |
1216 | |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | |
1223 | |
1224 | |
1225 | |
1226 | LLVM_NODISCARD bool noteFailure() { |
1227 | |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | bool KeepGoing = keepEvaluatingAfterFailure(); |
1235 | EvalStatus.HasSideEffects |= KeepGoing; |
1236 | return KeepGoing; |
1237 | } |
1238 | |
1239 | class ArrayInitLoopIndex { |
1240 | EvalInfo &Info; |
1241 | uint64_t OuterIndex; |
1242 | |
1243 | public: |
1244 | ArrayInitLoopIndex(EvalInfo &Info) |
1245 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
1246 | Info.ArrayInitIndex = 0; |
1247 | } |
1248 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
1249 | |
1250 | operator uint64_t&() { return Info.ArrayInitIndex; } |
1251 | }; |
1252 | }; |
1253 | |
1254 | |
1255 | struct FoldConstant { |
1256 | EvalInfo &Info; |
1257 | bool Enabled; |
1258 | bool HadNoPriorDiags; |
1259 | EvalInfo::EvaluationMode OldMode; |
1260 | |
1261 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
1262 | : Info(Info), |
1263 | Enabled(Enabled), |
1264 | HadNoPriorDiags(Info.EvalStatus.Diag && |
1265 | Info.EvalStatus.Diag->empty() && |
1266 | !Info.EvalStatus.HasSideEffects), |
1267 | OldMode(Info.EvalMode) { |
1268 | if (Enabled) |
1269 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
1270 | } |
1271 | void keepDiagnostics() { Enabled = false; } |
1272 | ~FoldConstant() { |
1273 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
1274 | !Info.EvalStatus.HasSideEffects) |
1275 | Info.EvalStatus.Diag->clear(); |
1276 | Info.EvalMode = OldMode; |
1277 | } |
1278 | }; |
1279 | |
1280 | |
1281 | |
1282 | struct IgnoreSideEffectsRAII { |
1283 | EvalInfo &Info; |
1284 | EvalInfo::EvaluationMode OldMode; |
1285 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
1286 | : Info(Info), OldMode(Info.EvalMode) { |
1287 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
1288 | } |
1289 | |
1290 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
1291 | }; |
1292 | |
1293 | |
1294 | |
1295 | class SpeculativeEvaluationRAII { |
1296 | EvalInfo *Info = nullptr; |
1297 | Expr::EvalStatus OldStatus; |
1298 | unsigned OldSpeculativeEvaluationDepth; |
1299 | |
1300 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
1301 | Info = Other.Info; |
1302 | OldStatus = Other.OldStatus; |
1303 | OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth; |
1304 | Other.Info = nullptr; |
1305 | } |
1306 | |
1307 | void maybeRestoreState() { |
1308 | if (!Info) |
1309 | return; |
1310 | |
1311 | Info->EvalStatus = OldStatus; |
1312 | Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth; |
1313 | } |
1314 | |
1315 | public: |
1316 | SpeculativeEvaluationRAII() = default; |
1317 | |
1318 | SpeculativeEvaluationRAII( |
1319 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
1320 | : Info(&Info), OldStatus(Info.EvalStatus), |
1321 | OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) { |
1322 | Info.EvalStatus.Diag = NewDiag; |
1323 | Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1; |
1324 | } |
1325 | |
1326 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
1327 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
1328 | moveFromAndCancel(std::move(Other)); |
1329 | } |
1330 | |
1331 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
1332 | maybeRestoreState(); |
1333 | moveFromAndCancel(std::move(Other)); |
1334 | return *this; |
1335 | } |
1336 | |
1337 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
1338 | }; |
1339 | |
1340 | |
1341 | |
1342 | template<ScopeKind Kind> |
1343 | class ScopeRAII { |
1344 | EvalInfo &Info; |
1345 | unsigned OldStackSize; |
1346 | public: |
1347 | ScopeRAII(EvalInfo &Info) |
1348 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
1349 | |
1350 | |
1351 | Info.CurrentCall->pushTempVersion(); |
1352 | } |
1353 | bool destroy(bool RunDestructors = true) { |
1354 | bool OK = cleanup(Info, RunDestructors, OldStackSize); |
1355 | OldStackSize = -1U; |
1356 | return OK; |
1357 | } |
1358 | ~ScopeRAII() { |
1359 | if (OldStackSize != -1U) |
1360 | destroy(false); |
1361 | |
1362 | |
1363 | Info.CurrentCall->popTempVersion(); |
1364 | } |
1365 | private: |
1366 | static bool cleanup(EvalInfo &Info, bool RunDestructors, |
1367 | unsigned OldStackSize) { |
1368 | assert(OldStackSize <= Info.CleanupStack.size() && |
1369 | "running cleanups out of order?"); |
1370 | |
1371 | |
1372 | |
1373 | bool Success = true; |
1374 | for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) { |
1375 | if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(Kind)) { |
1376 | if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) { |
1377 | Success = false; |
1378 | break; |
1379 | } |
1380 | } |
1381 | } |
1382 | |
1383 | |
1384 | auto NewEnd = Info.CleanupStack.begin() + OldStackSize; |
1385 | if (Kind != ScopeKind::Block) |
1386 | NewEnd = |
1387 | std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) { |
1388 | return C.isDestroyedAtEndOf(Kind); |
1389 | }); |
1390 | Info.CleanupStack.erase(NewEnd, Info.CleanupStack.end()); |
1391 | return Success; |
1392 | } |
1393 | }; |
1394 | typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII; |
1395 | typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII; |
1396 | typedef ScopeRAII<ScopeKind::Call> CallScopeRAII; |
1397 | } |
1398 | |
1399 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
1400 | CheckSubobjectKind CSK) { |
1401 | if (Invalid) |
1402 | return false; |
1403 | if (isOnePastTheEnd()) { |
1404 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
1405 | << CSK; |
1406 | setInvalid(); |
1407 | return false; |
1408 | } |
1409 | |
1410 | |
1411 | |
1412 | return true; |
1413 | } |
1414 | |
1415 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
1416 | const Expr *E) { |
1417 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
1418 | |
1419 | |
1420 | } |
1421 | |
1422 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
1423 | const Expr *E, |
1424 | const APSInt &N) { |
1425 | |
1426 | |
1427 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
1428 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1429 | << N << 0 |
1430 | << static_cast<unsigned>(getMostDerivedArraySize()); |
1431 | else |
1432 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1433 | << N << 1; |
1434 | setInvalid(); |
1435 | } |
1436 | |
1437 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
1438 | const FunctionDecl *Callee, const LValue *This, |
1439 | CallRef Call) |
1440 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
1441 | Arguments(Call), CallLoc(CallLoc), Index(Info.NextCallIndex++) { |
1442 | Info.CurrentCall = this; |
1443 | ++Info.CallStackDepth; |
1444 | } |
1445 | |
1446 | CallStackFrame::~CallStackFrame() { |
1447 | assert(Info.CurrentCall == this && "calls retired out of order"); |
1448 | --Info.CallStackDepth; |
1449 | Info.CurrentCall = Caller; |
1450 | } |
1451 | |
1452 | static bool isRead(AccessKinds AK) { |
1453 | return AK == AK_Read || AK == AK_ReadObjectRepresentation; |
1454 | } |
1455 | |
1456 | static bool isModification(AccessKinds AK) { |
1457 | switch (AK) { |
1458 | case AK_Read: |
1459 | case AK_ReadObjectRepresentation: |
1460 | case AK_MemberCall: |
1461 | case AK_DynamicCast: |
1462 | case AK_TypeId: |
1463 | return false; |
1464 | case AK_Assign: |
1465 | case AK_Increment: |
1466 | case AK_Decrement: |
1467 | case AK_Construct: |
1468 | case AK_Destroy: |
1469 | return true; |
1470 | } |
1471 | llvm_unreachable("unknown access kind"); |
1472 | } |
1473 | |
1474 | static bool isAnyAccess(AccessKinds AK) { |
1475 | return isRead(AK) || isModification(AK); |
1476 | } |
1477 | |
1478 | |
1479 | static bool isFormalAccess(AccessKinds AK) { |
1480 | return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy; |
1481 | } |
1482 | |
1483 | |
1484 | static bool isValidIndeterminateAccess(AccessKinds AK) { |
1485 | switch (AK) { |
1486 | case AK_Read: |
1487 | case AK_Increment: |
1488 | case AK_Decrement: |
1489 | |
1490 | return false; |
1491 | |
1492 | case AK_ReadObjectRepresentation: |
1493 | case AK_Assign: |
1494 | case AK_Construct: |
1495 | case AK_Destroy: |
1496 | |
1497 | return true; |
1498 | |
1499 | case AK_MemberCall: |
1500 | case AK_DynamicCast: |
1501 | case AK_TypeId: |
1502 | |
1503 | return true; |
1504 | } |
1505 | llvm_unreachable("unknown access kind"); |
1506 | } |
1507 | |
1508 | namespace { |
1509 | struct ComplexValue { |
1510 | private: |
1511 | bool IsInt; |
1512 | |
1513 | public: |
1514 | APSInt IntReal, IntImag; |
1515 | APFloat FloatReal, FloatImag; |
1516 | |
1517 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
1518 | |
1519 | void makeComplexFloat() { IsInt = false; } |
1520 | bool isComplexFloat() const { return !IsInt; } |
1521 | APFloat &getComplexFloatReal() { return FloatReal; } |
1522 | APFloat &getComplexFloatImag() { return FloatImag; } |
1523 | |
1524 | void makeComplexInt() { IsInt = true; } |
1525 | bool isComplexInt() const { return IsInt; } |
1526 | APSInt &getComplexIntReal() { return IntReal; } |
1527 | APSInt &getComplexIntImag() { return IntImag; } |
1528 | |
1529 | void moveInto(APValue &v) const { |
1530 | if (isComplexFloat()) |
1531 | v = APValue(FloatReal, FloatImag); |
1532 | else |
1533 | v = APValue(IntReal, IntImag); |
1534 | } |
1535 | void setFrom(const APValue &v) { |
1536 | assert(v.isComplexFloat() || v.isComplexInt()); |
1537 | if (v.isComplexFloat()) { |
1538 | makeComplexFloat(); |
1539 | FloatReal = v.getComplexFloatReal(); |
1540 | FloatImag = v.getComplexFloatImag(); |
1541 | } else { |
1542 | makeComplexInt(); |
1543 | IntReal = v.getComplexIntReal(); |
1544 | IntImag = v.getComplexIntImag(); |
1545 | } |
1546 | } |
1547 | }; |
1548 | |
1549 | struct LValue { |
1550 | APValue::LValueBase Base; |
1551 | CharUnits Offset; |
1552 | SubobjectDesignator Designator; |
1553 | bool IsNullPtr : 1; |
1554 | bool InvalidBase : 1; |
1555 | |
1556 | const APValue::LValueBase getLValueBase() const { return Base; } |
1557 | CharUnits &getLValueOffset() { return Offset; } |
1558 | const CharUnits &getLValueOffset() const { return Offset; } |
1559 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
1560 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
1561 | bool isNullPointer() const { return IsNullPtr;} |
1562 | |
1563 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
1564 | unsigned getLValueVersion() const { return Base.getVersion(); } |
1565 | |
1566 | void moveInto(APValue &V) const { |
1567 | if (Designator.Invalid) |
1568 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
1569 | else { |
1570 | assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
1571 | V = APValue(Base, Offset, Designator.Entries, |
1572 | Designator.IsOnePastTheEnd, IsNullPtr); |
1573 | } |
1574 | } |
1575 | void setFrom(ASTContext &Ctx, const APValue &V) { |
1576 | assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
1577 | Base = V.getLValueBase(); |
1578 | Offset = V.getLValueOffset(); |
1579 | InvalidBase = false; |
1580 | Designator = SubobjectDesignator(Ctx, V); |
1581 | IsNullPtr = V.isNullPointer(); |
1582 | } |
1583 | |
1584 | void set(APValue::LValueBase B, bool BInvalid = false) { |
1585 | #ifndef NDEBUG |
1586 | |
1587 | if (BInvalid) { |
1588 | const auto *E = B.get<const Expr *>(); |
1589 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
1590 | "Unexpected type of invalid base"); |
1591 | } |
1592 | #endif |
1593 | |
1594 | Base = B; |
1595 | Offset = CharUnits::fromQuantity(0); |
1596 | InvalidBase = BInvalid; |
1597 | Designator = SubobjectDesignator(getType(B)); |
1598 | IsNullPtr = false; |
1599 | } |
1600 | |
1601 | void setNull(ASTContext &Ctx, QualType PointerTy) { |
1602 | Base = (const ValueDecl *)nullptr; |
1603 | Offset = |
1604 | CharUnits::fromQuantity(Ctx.getTargetNullPointerValue(PointerTy)); |
1605 | InvalidBase = false; |
1606 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
1607 | IsNullPtr = true; |
1608 | } |
1609 | |
1610 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
1611 | set(B, true); |
1612 | } |
1613 | |
1614 | std::string toString(ASTContext &Ctx, QualType T) const { |
1615 | APValue Printable; |
1616 | moveInto(Printable); |
1617 | return Printable.getAsString(Ctx, T); |
1618 | } |
1619 | |
1620 | private: |
1621 | |
1622 | |
1623 | template <typename GenDiagType> |
1624 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
1625 | if (Designator.Invalid) |
1626 | return false; |
1627 | if (IsNullPtr) { |
1628 | GenDiag(); |
1629 | Designator.setInvalid(); |
1630 | return false; |
1631 | } |
1632 | return true; |
1633 | } |
1634 | |
1635 | public: |
1636 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
1637 | CheckSubobjectKind CSK) { |
1638 | return checkNullPointerDiagnosingWith([&Info, E, CSK] { |
1639 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
1640 | }); |
1641 | } |
1642 | |
1643 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
1644 | AccessKinds AK) { |
1645 | return checkNullPointerDiagnosingWith([&Info, E, AK] { |
1646 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
1647 | }); |
1648 | } |
1649 | |
1650 | |
1651 | |
1652 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
1653 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
1654 | Designator.checkSubobject(Info, E, CSK); |
1655 | } |
1656 | |
1657 | void addDecl(EvalInfo &Info, const Expr *E, |
1658 | const Decl *D, bool Virtual = false) { |
1659 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
1660 | Designator.addDeclUnchecked(D, Virtual); |
1661 | } |
1662 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
1663 | if (!Designator.Entries.empty()) { |
1664 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
1665 | Designator.setInvalid(); |
1666 | return; |
1667 | } |
1668 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) { |
1669 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
1670 | Designator.FirstEntryIsAnUnsizedArray = true; |
1671 | Designator.addUnsizedArrayUnchecked(ElemTy); |
1672 | } |
1673 | } |
1674 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
1675 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
1676 | Designator.addArrayUnchecked(CAT); |
1677 | } |
1678 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
1679 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
1680 | Designator.addComplexUnchecked(EltTy, Imag); |
1681 | } |
1682 | void clearIsNullPointer() { |
1683 | IsNullPtr = false; |
1684 | } |
1685 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
1686 | const APSInt &Index, CharUnits ElementSize) { |
1687 | |
1688 | |
1689 | if (!Index) |
1690 | return; |
1691 | |
1692 | |
1693 | |
1694 | |
1695 | uint64_t Offset64 = Offset.getQuantity(); |
1696 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
1697 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
1698 | Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); |
1699 | |
1700 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
1701 | Designator.adjustIndex(Info, E, Index); |
1702 | clearIsNullPointer(); |
1703 | } |
1704 | void adjustOffset(CharUnits N) { |
1705 | Offset += N; |
1706 | if (N.getQuantity()) |
1707 | clearIsNullPointer(); |
1708 | } |
1709 | }; |
1710 | |
1711 | struct MemberPtr { |
1712 | MemberPtr() {} |
1713 | explicit MemberPtr(const ValueDecl *Decl) : |
1714 | DeclAndIsDerivedMember(Decl, false), Path() {} |
1715 | |
1716 | |
1717 | |
1718 | const ValueDecl *getDecl() const { |
1719 | return DeclAndIsDerivedMember.getPointer(); |
1720 | } |
1721 | |
1722 | bool isDerivedMember() const { |
1723 | return DeclAndIsDerivedMember.getInt(); |
1724 | } |
1725 | |
1726 | const CXXRecordDecl *getContainingRecord() const { |
1727 | return cast<CXXRecordDecl>( |
1728 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
1729 | } |
1730 | |
1731 | void moveInto(APValue &V) const { |
1732 | V = APValue(getDecl(), isDerivedMember(), Path); |
1733 | } |
1734 | void setFrom(const APValue &V) { |
1735 | assert(V.isMemberPointer()); |
1736 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
1737 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
1738 | Path.clear(); |
1739 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
1740 | Path.insert(Path.end(), P.begin(), P.end()); |
1741 | } |
1742 | |
1743 | |
1744 | |
1745 | |
1746 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
1747 | |
1748 | |
1749 | SmallVector<const CXXRecordDecl*, 4> Path; |
1750 | |
1751 | |
1752 | |
1753 | bool castBack(const CXXRecordDecl *Class) { |
1754 | assert(!Path.empty()); |
1755 | const CXXRecordDecl *Expected; |
1756 | if (Path.size() >= 2) |
1757 | Expected = Path[Path.size() - 2]; |
1758 | else |
1759 | Expected = getContainingRecord(); |
1760 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
1761 | |
1762 | |
1763 | |
1764 | |
1765 | |
1766 | |
1767 | return false; |
1768 | } |
1769 | Path.pop_back(); |
1770 | return true; |
1771 | } |
1772 | |
1773 | bool castToDerived(const CXXRecordDecl *Derived) { |
1774 | if (!getDecl()) |
1775 | return true; |
1776 | if (!isDerivedMember()) { |
1777 | Path.push_back(Derived); |
1778 | return true; |
1779 | } |
1780 | if (!castBack(Derived)) |
1781 | return false; |
1782 | if (Path.empty()) |
1783 | DeclAndIsDerivedMember.setInt(false); |
1784 | return true; |
1785 | } |
1786 | |
1787 | bool castToBase(const CXXRecordDecl *Base) { |
1788 | if (!getDecl()) |
1789 | return true; |
1790 | if (Path.empty()) |
1791 | DeclAndIsDerivedMember.setInt(true); |
1792 | if (isDerivedMember()) { |
1793 | Path.push_back(Base); |
1794 | return true; |
1795 | } |
1796 | return castBack(Base); |
1797 | } |
1798 | }; |
1799 | |
1800 | |
1801 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
1802 | if (!LHS.getDecl() || !RHS.getDecl()) |
1803 | return !LHS.getDecl() && !RHS.getDecl(); |
1804 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
1805 | return false; |
1806 | return LHS.Path == RHS.Path; |
1807 | } |
1808 | } |
1809 | |
1810 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
1811 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
1812 | const LValue &This, const Expr *E, |
1813 | bool AllowNonLiteralTypes = false); |
1814 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
1815 | bool InvalidBaseOK = false); |
1816 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
1817 | bool InvalidBaseOK = false); |
1818 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
1819 | EvalInfo &Info); |
1820 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
1821 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
1822 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
1823 | EvalInfo &Info); |
1824 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
1825 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
1826 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
1827 | EvalInfo &Info); |
1828 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
1829 | |
1830 | |
1831 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
1832 | EvalInfo &Info); |
1833 | |
1834 | |
1835 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
1836 | EvalInfo &Info); |
1837 | |
1838 | |
1839 | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | static void negateAsSigned(APSInt &Int) { |
1845 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
1846 | Int = Int.extend(Int.getBitWidth() + 1); |
1847 | Int.setIsSigned(true); |
1848 | } |
1849 | Int = -Int; |
1850 | } |
1851 | |
1852 | template<typename KeyT> |
1853 | APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T, |
1854 | ScopeKind Scope, LValue &LV) { |
1855 | unsigned Version = getTempVersion(); |
1856 | APValue::LValueBase Base(Key, Index, Version); |
1857 | LV.set(Base); |
1858 | return createLocal(Base, Key, T, Scope); |
1859 | } |
1860 | |
1861 | |
1862 | APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD, |
1863 | LValue &LV) { |
1864 | assert(Args.CallIndex == Index && "creating parameter in wrong frame"); |
1865 | APValue::LValueBase Base(PVD, Index, Args.Version); |
1866 | LV.set(Base); |
1867 | |
1868 | |
1869 | |
1870 | return createLocal(Base, PVD, PVD->getType(), ScopeKind::Call); |
1871 | } |
1872 | |
1873 | APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key, |
1874 | QualType T, ScopeKind Scope) { |
1875 | assert(Base.getCallIndex() == Index && "lvalue for wrong frame"); |
1876 | unsigned Version = Base.getVersion(); |
1877 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
1878 | assert(Result.isAbsent() && "local created multiple times"); |
1879 | |
1880 | |
1881 | |
1882 | |
1883 | |
1884 | if (Index <= Info.SpeculativeEvaluationDepth) { |
1885 | if (T.isDestructedType()) |
1886 | Info.noteSideEffect(); |
1887 | } else { |
1888 | Info.CleanupStack.push_back(Cleanup(&Result, Base, T, Scope)); |
1889 | } |
1890 | return Result; |
1891 | } |
1892 | |
1893 | APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) { |
1894 | if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) { |
1895 | FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded); |
1896 | return nullptr; |
1897 | } |
1898 | |
1899 | DynamicAllocLValue DA(NumHeapAllocs++); |
1900 | LV.set(APValue::LValueBase::getDynamicAlloc(DA, T)); |
1901 | auto Result = HeapAllocs.emplace(std::piecewise_construct, |
1902 | std::forward_as_tuple(DA), std::tuple<>()); |
1903 | assert(Result.second && "reused a heap alloc index?"); |
1904 | Result.first->second.AllocExpr = E; |
1905 | return &Result.first->second.Value; |
1906 | } |
1907 | |
1908 | |
1909 | void CallStackFrame::describe(raw_ostream &Out) { |
1910 | unsigned ArgIndex = 0; |
1911 | bool IsMemberCall = isa<CXXMethodDecl>(Callee) && |
1912 | !isa<CXXConstructorDecl>(Callee) && |
1913 | cast<CXXMethodDecl>(Callee)->isInstance(); |
1914 | |
1915 | if (!IsMemberCall) |
1916 | Out << *Callee << '('; |
1917 | |
1918 | if (This && IsMemberCall) { |
1919 | APValue Val; |
1920 | This->moveInto(Val); |
1921 | Val.printPretty(Out, Info.Ctx, |
1922 | This->Designator.MostDerivedType); |
1923 | |
1924 | Out << "->" << *Callee << '('; |
1925 | IsMemberCall = false; |
1926 | } |
1927 | |
1928 | for (FunctionDecl::param_const_iterator I = Callee->param_begin(), |
1929 | E = Callee->param_end(); I != E; ++I, ++ArgIndex) { |
1930 | if (ArgIndex > (unsigned)IsMemberCall) |
1931 | Out << ", "; |
1932 | |
1933 | const ParmVarDecl *Param = *I; |
1934 | APValue *V = Info.getParamSlot(Arguments, Param); |
1935 | if (V) |
1936 | V->printPretty(Out, Info.Ctx, Param->getType()); |
1937 | else |
1938 | Out << "<...>"; |
1939 | |
1940 | if (ArgIndex == 0 && IsMemberCall) |
1941 | Out << "->" << *Callee << '('; |
1942 | } |
1943 | |
1944 | Out << ')'; |
1945 | } |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
1951 | assert(!E->isValueDependent()); |
1952 | APValue Scratch; |
1953 | if (!Evaluate(Scratch, Info, E)) |
1954 | |
1955 | return Info.noteSideEffect(); |
1956 | return true; |
1957 | } |
1958 | |
1959 | |
1960 | static bool IsStringLiteralCall(const CallExpr *E) { |
1961 | unsigned Builtin = E->getBuiltinCallee(); |
1962 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
1963 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
1964 | } |
1965 | |
1966 | static bool IsGlobalLValue(APValue::LValueBase B) { |
1967 | |
1968 | |
1969 | |
1970 | |
1971 | |
1972 | if (!B) return true; |
1973 | |
1974 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
1975 | |
1976 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
1977 | return VD->hasGlobalStorage(); |
1978 | if (isa<TemplateParamObjectDecl>(D)) |
1979 | return true; |
1980 | |
1981 | |
1982 | return isa<FunctionDecl>(D) || isa<MSGuidDecl>(D); |
1983 | } |
1984 | |
1985 | if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>()) |
1986 | return true; |
1987 | |
1988 | const Expr *E = B.get<const Expr*>(); |
1989 | switch (E->getStmtClass()) { |
1990 | default: |
1991 | return false; |
1992 | case Expr::CompoundLiteralExprClass: { |
1993 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
1994 | return CLE->isFileScope() && CLE->isLValue(); |
1995 | } |
1996 | case Expr::MaterializeTemporaryExprClass: |
1997 | |
1998 | |
1999 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
2000 | |
2001 | case Expr::StringLiteralClass: |
2002 | case Expr::PredefinedExprClass: |
2003 | case Expr::ObjCStringLiteralClass: |
2004 | case Expr::ObjCEncodeExprClass: |
2005 | return true; |
2006 | case Expr::ObjCBoxedExprClass: |
2007 | return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer(); |
2008 | case Expr::CallExprClass: |
2009 | return IsStringLiteralCall(cast<CallExpr>(E)); |
2010 | |
2011 | case Expr::AddrLabelExprClass: |
2012 | return true; |
2013 | |
2014 | |
2015 | case Expr::BlockExprClass: |
2016 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
2017 | case Expr::ImplicitValueInitExprClass: |
2018 | |
2019 | |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | return true; |
2025 | } |
2026 | } |
2027 | |
2028 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
2029 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
2030 | } |
2031 | |
2032 | static bool IsLiteralLValue(const LValue &Value) { |
2033 | if (Value.getLValueCallIndex()) |
2034 | return false; |
2035 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
2036 | return E && !isa<MaterializeTemporaryExpr>(E); |
2037 | } |
2038 | |
2039 | static bool IsWeakLValue(const LValue &Value) { |
2040 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
2041 | return Decl && Decl->isWeak(); |
2042 | } |
2043 | |
2044 | static bool isZeroSized(const LValue &Value) { |
2045 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
2046 | if (Decl && isa<VarDecl>(Decl)) { |
2047 | QualType Ty = Decl->getType(); |
2048 | if (Ty->isArrayType()) |
2049 | return Ty->isIncompleteType() || |
2050 | Decl->getASTContext().getTypeSize(Ty) == 0; |
2051 | } |
2052 | return false; |
2053 | } |
2054 | |
2055 | static bool HasSameBase(const LValue &A, const LValue &B) { |
2056 | if (!A.getLValueBase()) |
2057 | return !B.getLValueBase(); |
2058 | if (!B.getLValueBase()) |
2059 | return false; |
2060 | |
2061 | if (A.getLValueBase().getOpaqueValue() != |
2062 | B.getLValueBase().getOpaqueValue()) |
2063 | return false; |
2064 | |
2065 | return A.getLValueCallIndex() == B.getLValueCallIndex() && |
2066 | A.getLValueVersion() == B.getLValueVersion(); |
2067 | } |
2068 | |
2069 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
2070 | assert(Base && "no location for a null lvalue"); |
2071 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(VD)) { |
2077 | unsigned Idx = PVD->getFunctionScopeIndex(); |
2078 | for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) { |
2079 | if (F->Arguments.CallIndex == Base.getCallIndex() && |
2080 | F->Arguments.Version == Base.getVersion() && F->Callee && |
2081 | Idx < F->Callee->getNumParams()) { |
2082 | VD = F->Callee->getParamDecl(Idx); |
2083 | break; |
2084 | } |
2085 | } |
2086 | } |
2087 | |
2088 | if (VD) |
2089 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2090 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
2091 | Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here); |
2092 | else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
2093 | |
2094 | if (Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA)) |
2095 | Info.Note((*Alloc)->AllocExpr->getExprLoc(), |
2096 | diag::note_constexpr_dynamic_alloc_here); |
2097 | } |
2098 | |
2099 | } |
2100 | |
2101 | enum class CheckEvaluationResultKind { |
2102 | ConstantExpression, |
2103 | FullyInitialized, |
2104 | }; |
2105 | |
2106 | |
2107 | |
2108 | using CheckedTemporaries = |
2109 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>; |
2110 | |
2111 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2112 | EvalInfo &Info, SourceLocation DiagLoc, |
2113 | QualType Type, const APValue &Value, |
2114 | ConstantExprKind Kind, |
2115 | SourceLocation SubobjectLoc, |
2116 | CheckedTemporaries &CheckedTemps); |
2117 | |
2118 | |
2119 | |
2120 | |
2121 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
2122 | QualType Type, const LValue &LVal, |
2123 | ConstantExprKind Kind, |
2124 | CheckedTemporaries &CheckedTemps) { |
2125 | bool IsReferenceType = Type->isReferenceType(); |
2126 | |
2127 | APValue::LValueBase Base = LVal.getLValueBase(); |
2128 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
2129 | |
2130 | const Expr *BaseE = Base.dyn_cast<const Expr *>(); |
2131 | const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>(); |
2132 | |
2133 | |
2134 | |
2135 | |
2136 | if (isTemplateArgument(Kind)) { |
2137 | int InvalidBaseKind = -1; |
2138 | StringRef Ident; |
2139 | if (Base.is<TypeInfoLValue>()) |
2140 | InvalidBaseKind = 0; |
2141 | else if (isa_and_nonnull<StringLiteral>(BaseE)) |
2142 | InvalidBaseKind = 1; |
2143 | else if (isa_and_nonnull<MaterializeTemporaryExpr>(BaseE) || |
2144 | isa_and_nonnull<LifetimeExtendedTemporaryDecl>(BaseVD)) |
2145 | InvalidBaseKind = 2; |
2146 | else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(BaseE)) { |
2147 | InvalidBaseKind = 3; |
2148 | Ident = PE->getIdentKindName(); |
2149 | } |
2150 | |
2151 | if (InvalidBaseKind != -1) { |
2152 | Info.FFDiag(Loc, diag::note_constexpr_invalid_template_arg) |
2153 | << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind |
2154 | << Ident; |
2155 | return false; |
2156 | } |
2157 | } |
2158 | |
2159 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(BaseVD)) { |
2160 | if (FD->isConsteval()) { |
2161 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) |
2162 | << !Type->isAnyPointerType(); |
2163 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2164 | return false; |
2165 | } |
2166 | } |
2167 | |
2168 | |
2169 | |
2170 | |
2171 | if (!IsGlobalLValue(Base)) { |
2172 | if (Info.getLangOpts().CPlusPlus11) { |
2173 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2174 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
2175 | << IsReferenceType << !Designator.Entries.empty() |
2176 | << !!VD << VD; |
2177 | |
2178 | auto *VarD = dyn_cast_or_null<VarDecl>(VD); |
2179 | if (VarD && VarD->isConstexpr()) { |
2180 | |
2181 | |
2182 | |
2183 | |
2184 | |
2185 | Info.Note(VarD->getLocation(), diag::note_constexpr_not_static) |
2186 | << VarD |
2187 | << FixItHint::CreateInsertion(VarD->getBeginLoc(), "static "); |
2188 | } else { |
2189 | NoteLValueLocation(Info, Base); |
2190 | } |
2191 | } else { |
2192 | Info.FFDiag(Loc); |
2193 | } |
2194 | |
2195 | return false; |
2196 | } |
2197 | assert((Info.checkingPotentialConstantExpression() || |
2198 | LVal.getLValueCallIndex() == 0) && |
2199 | "have call index for global lvalue"); |
2200 | |
2201 | if (Base.is<DynamicAllocLValue>()) { |
2202 | Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc) |
2203 | << IsReferenceType << !Designator.Entries.empty(); |
2204 | NoteLValueLocation(Info, Base); |
2205 | return false; |
2206 | } |
2207 | |
2208 | if (BaseVD) { |
2209 | if (const VarDecl *Var = dyn_cast<const VarDecl>(BaseVD)) { |
2210 | |
2211 | if (Var->getTLSKind()) |
2212 | |
2213 | return false; |
2214 | |
2215 | |
2216 | |
2217 | if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>()) |
2218 | |
2219 | return false; |
2220 | } |
2221 | if (const auto *FD = dyn_cast<const FunctionDecl>(BaseVD)) { |
2222 | |
2223 | |
2224 | |
2225 | |
2226 | |
2227 | |
2228 | |
2229 | |
2230 | |
2231 | |
2232 | if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) && |
2233 | FD->hasAttr<DLLImportAttr>()) |
2234 | |
2235 | return false; |
2236 | } |
2237 | } else if (const auto *MTE = |
2238 | dyn_cast_or_null<MaterializeTemporaryExpr>(BaseE)) { |
2239 | if (CheckedTemps.insert(MTE).second) { |
2240 | QualType TempType = getType(Base); |
2241 | if (TempType.isDestructedType()) { |
2242 | Info.FFDiag(MTE->getExprLoc(), |
2243 | diag::note_constexpr_unsupported_temporary_nontrivial_dtor) |
2244 | << TempType; |
2245 | return false; |
2246 | } |
2247 | |
2248 | APValue *V = MTE->getOrCreateValue(false); |
2249 | assert(V && "evasluation result refers to uninitialised temporary"); |
2250 | if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2251 | Info, MTE->getExprLoc(), TempType, *V, |
2252 | Kind, SourceLocation(), CheckedTemps)) |
2253 | return false; |
2254 | } |
2255 | } |
2256 | |
2257 | |
2258 | |
2259 | if (!IsReferenceType) |
2260 | return true; |
2261 | |
2262 | |
2263 | if (!Base) { |
2264 | |
2265 | Info.CCEDiag(Loc); |
2266 | return true; |
2267 | } |
2268 | |
2269 | |
2270 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
2271 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
2272 | << !Designator.Entries.empty() << !!BaseVD << BaseVD; |
2273 | NoteLValueLocation(Info, Base); |
2274 | } |
2275 | |
2276 | return true; |
2277 | } |
2278 | |
2279 | |
2280 | |
2281 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
2282 | SourceLocation Loc, |
2283 | QualType Type, |
2284 | const APValue &Value, |
2285 | ConstantExprKind Kind) { |
2286 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
2287 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
2288 | if (!FD) |
2289 | return true; |
2290 | if (FD->isConsteval()) { |
2291 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) << 0; |
2292 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2293 | return false; |
2294 | } |
2295 | return isForManglingOnly(Kind) || FD->isVirtual() || |
2296 | !FD->hasAttr<DLLImportAttr>(); |
2297 | } |
2298 | |
2299 | |
2300 | |
2301 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
2302 | const LValue *This = nullptr) { |
2303 | if (!E->isPRValue() || E->getType()->isLiteralType(Info.Ctx)) |
2304 | return true; |
2305 | |
2306 | |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | |
2313 | |
2314 | |
2315 | |
2316 | |
2317 | |
2318 | |
2319 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
2320 | return true; |
2321 | |
2322 | |
2323 | if (Info.getLangOpts().CPlusPlus11) |
2324 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
2325 | << E->getType(); |
2326 | else |
2327 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2328 | return false; |
2329 | } |
2330 | |
2331 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2332 | EvalInfo &Info, SourceLocation DiagLoc, |
2333 | QualType Type, const APValue &Value, |
2334 | ConstantExprKind Kind, |
2335 | SourceLocation SubobjectLoc, |
2336 | CheckedTemporaries &CheckedTemps) { |
2337 | if (!Value.hasValue()) { |
2338 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2339 | << true << Type; |
2340 | if (SubobjectLoc.isValid()) |
2341 | Info.Note(SubobjectLoc, diag::note_constexpr_subobject_declared_here); |
2342 | return false; |
2343 | } |
2344 | |
2345 | |
2346 | |
2347 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
2348 | Type = AT->getValueType(); |
2349 | |
2350 | |
2351 | |
2352 | |
2353 | if (Value.isArray()) { |
2354 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
2355 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
2356 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
2357 | Value.getArrayInitializedElt(I), Kind, |
2358 | SubobjectLoc, CheckedTemps)) |
2359 | return false; |
2360 | } |
2361 | if (!Value.hasArrayFiller()) |
2362 | return true; |
2363 | return CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
2364 | Value.getArrayFiller(), Kind, SubobjectLoc, |
2365 | CheckedTemps); |
2366 | } |
2367 | if (Value.isUnion() && Value.getUnionField()) { |
2368 | return CheckEvaluationResult( |
2369 | CERK, Info, DiagLoc, Value.getUnionField()->getType(), |
2370 | Value.getUnionValue(), Kind, Value.getUnionField()->getLocation(), |
2371 | CheckedTemps); |
2372 | } |
2373 | if (Value.isStruct()) { |
2374 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
2375 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
2376 | unsigned BaseIndex = 0; |
2377 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
2378 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(), |
2379 | Value.getStructBase(BaseIndex), Kind, |
2380 | BS.getBeginLoc(), CheckedTemps)) |
2381 | return false; |
2382 | ++BaseIndex; |
2383 | } |
2384 | } |
2385 | for (const auto *I : RD->fields()) { |
2386 | if (I->isUnnamedBitfield()) |
2387 | continue; |
2388 | |
2389 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(), |
2390 | Value.getStructField(I->getFieldIndex()), |
2391 | Kind, I->getLocation(), CheckedTemps)) |
2392 | return false; |
2393 | } |
2394 | } |
2395 | |
2396 | if (Value.isLValue() && |
2397 | CERK == CheckEvaluationResultKind::ConstantExpression) { |
2398 | LValue LVal; |
2399 | LVal.setFrom(Info.Ctx, Value); |
2400 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Kind, |
2401 | CheckedTemps); |
2402 | } |
2403 | |
2404 | if (Value.isMemberPointer() && |
2405 | CERK == CheckEvaluationResultKind::ConstantExpression) |
2406 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Kind); |
2407 | |
2408 | |
2409 | return true; |
2410 | } |
2411 | |
2412 | |
2413 | |
2414 | |
2415 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
2416 | QualType Type, const APValue &Value, |
2417 | ConstantExprKind Kind) { |
2418 | |
2419 | if (Type->isVoidType()) |
2420 | return true; |
2421 | |
2422 | CheckedTemporaries CheckedTemps; |
2423 | return CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2424 | Info, DiagLoc, Type, Value, Kind, |
2425 | SourceLocation(), CheckedTemps); |
2426 | } |
2427 | |
2428 | |
2429 | |
2430 | static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, |
2431 | QualType Type, const APValue &Value) { |
2432 | CheckedTemporaries CheckedTemps; |
2433 | return CheckEvaluationResult( |
2434 | CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value, |
2435 | ConstantExprKind::Normal, SourceLocation(), CheckedTemps); |
2436 | } |
2437 | |
2438 | |
2439 | |
2440 | static bool CheckMemoryLeaks(EvalInfo &Info) { |
2441 | if (!Info.HeapAllocs.empty()) { |
2442 | |
2443 | |
2444 | |
2445 | Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr, |
2446 | diag::note_constexpr_memory_leak) |
2447 | << unsigned(Info.HeapAllocs.size() - 1); |
2448 | } |
2449 | return true; |
2450 | } |
2451 | |
2452 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
2453 | |
2454 | |
2455 | if (!Value.getLValueBase()) { |
2456 | Result = !Value.getLValueOffset().isZero(); |
2457 | return true; |
2458 | } |
2459 | |
2460 | |
2461 | |
2462 | Result = true; |
2463 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
2464 | return !Decl || !Decl->isWeak(); |
2465 | } |
2466 | |
2467 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
2468 | switch (Val.getKind()) { |
2469 | case APValue::None: |
2470 | case APValue::Indeterminate: |
2471 | return false; |
2472 | case APValue::Int: |
2473 | Result = Val.getInt().getBoolValue(); |
2474 | return true; |
2475 | case APValue::FixedPoint: |
2476 | Result = Val.getFixedPoint().getBoolValue(); |
2477 | return true; |
2478 | case APValue::Float: |
2479 | Result = !Val.getFloat().isZero(); |
2480 | return true; |
2481 | case APValue::ComplexInt: |
2482 | Result = Val.getComplexIntReal().getBoolValue() || |
2483 | Val.getComplexIntImag().getBoolValue(); |
2484 | return true; |
2485 | case APValue::ComplexFloat: |
2486 | Result = !Val.getComplexFloatReal().isZero() || |
2487 | !Val.getComplexFloatImag().isZero(); |
2488 | return true; |
2489 | case APValue::LValue: |
2490 | return EvalPointerValueAsBool(Val, Result); |
2491 | case APValue::MemberPointer: |
2492 | Result = Val.getMemberPointerDecl(); |
2493 | return true; |
2494 | case APValue::Vector: |
2495 | case APValue::Array: |
2496 | case APValue::Struct: |
2497 | case APValue::Union: |
2498 | case APValue::AddrLabelDiff: |
2499 | return false; |
2500 | } |
2501 | |
2502 | llvm_unreachable("unknown APValue kind"); |
2503 | } |
2504 | |
2505 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
2506 | EvalInfo &Info) { |
2507 | assert(!E->isValueDependent()); |
2508 | assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
2509 | APValue Val; |
2510 | if (!Evaluate(Val, Info, E)) |
2511 | return false; |
2512 | return HandleConversionToBool(Val, Result); |
2513 | } |
2514 | |
2515 | template<typename T> |
2516 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
2517 | const T &SrcValue, QualType DestType) { |
2518 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
2519 | << SrcValue << DestType; |
2520 | return Info.noteUndefinedBehavior(); |
2521 | } |
2522 | |
2523 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
2524 | QualType SrcType, const APFloat &Value, |
2525 | QualType DestType, APSInt &Result) { |
2526 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2527 | |
2528 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
2529 | |
2530 | Result = APSInt(DestWidth, !DestSigned); |
2531 | bool ignored; |
2532 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
2533 | & APFloat::opInvalidOp) |
2534 | return HandleOverflow(Info, E, Value, DestType); |
2535 | return true; |
2536 | } |
2537 | |
2538 | |
2539 | |
2540 | |
2541 | |
2542 | |
2543 | |
2544 | static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E, |
2545 | bool &DynamicRM) { |
2546 | llvm::RoundingMode RM = |
2547 | E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode(); |
2548 | DynamicRM = (RM == llvm::RoundingMode::Dynamic); |
2549 | if (DynamicRM) |
2550 | RM = llvm::RoundingMode::NearestTiesToEven; |
2551 | return RM; |
2552 | } |
2553 | |
2554 | |
2555 | static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E, |
2556 | APFloat::opStatus St) { |
2557 | |
2558 | |
2559 | if (Info.InConstantContext) |
2560 | return true; |
2561 | |
2562 | FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()); |
2563 | if ((St & APFloat::opInexact) && |
2564 | FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) { |
2565 | |
2566 | |
2567 | Info.FFDiag(E, diag::note_constexpr_dynamic_rounding); |
2568 | return false; |
2569 | } |
2570 | |
2571 | if ((St != APFloat::opOK) && |
2572 | (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic || |
2573 | FPO.getFPExceptionMode() != LangOptions::FPE_Ignore || |
2574 | FPO.getAllowFEnvAccess())) { |
2575 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2576 | return false; |
2577 | } |
2578 | |
2579 | if ((St & APFloat::opStatus::opInvalidOp) && |
2580 | FPO.getFPExceptionMode() != LangOptions::FPE_Ignore) { |
2581 | |
2582 | Info.FFDiag(E); |
2583 | return false; |
2584 | } |
2585 | |
2586 | |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | |
2592 | return true; |
2593 | } |
2594 | |
2595 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
2596 | QualType SrcType, QualType DestType, |
2597 | APFloat &Result) { |
2598 | assert(isa<CastExpr>(E) || isa<CompoundAssignOperator>(E)); |
2599 | bool DynamicRM; |
2600 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E, DynamicRM); |
2601 | APFloat::opStatus St; |
2602 | APFloat Value = Result; |
2603 | bool ignored; |
2604 | St = Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), RM, &ignored); |
2605 | return checkFloatingPointResult(Info, E, St); |
2606 | } |
2607 | |
2608 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
2609 | QualType DestType, QualType SrcType, |
2610 | const APSInt &Value) { |
2611 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2612 | |
2613 | |
2614 | APSInt Result = Value.extOrTrunc(DestWidth); |
2615 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
2616 | if (DestType->isBooleanType()) |
2617 | Result = Value.getBoolValue(); |
2618 | return Result; |
2619 | } |
2620 | |
2621 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
2622 | const FPOptions FPO, |
2623 | QualType SrcType, const APSInt &Value, |
2624 | QualType DestType, APFloat &Result) { |
2625 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
2626 | APFloat::opStatus St = Result.convertFromAPInt(Value, Value.isSigned(), |
2627 | APFloat::rmNearestTiesToEven); |
2628 | if (!Info.InConstantContext && St != llvm::APFloatBase::opOK && |
2629 | FPO.isFPConstrained()) { |
2630 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2631 | return false; |
2632 | } |
2633 | return true; |
2634 | } |
2635 | |
2636 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
2637 | APValue &Value, const FieldDecl *FD) { |
2638 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
2639 | |
2640 | if (!Value.isInt()) { |
2641 | |
2642 | |
2643 | |
2644 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
2645 | Info.FFDiag(E); |
2646 | return false; |
2647 | } |
2648 | |
2649 | APSInt &Int = Value.getInt(); |
2650 | unsigned OldBitWidth = Int.getBitWidth(); |
2651 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
2652 | if (NewBitWidth < OldBitWidth) |
2653 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
2654 | return true; |
2655 | } |
2656 | |
2657 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
2658 | llvm::APInt &Res) { |
2659 | APValue SVal; |
2660 | if (!Evaluate(SVal, Info, E)) |
2661 | return false; |
2662 | if (SVal.isInt()) { |
2663 | Res = SVal.getInt(); |
2664 | return true; |
2665 | } |
2666 | if (SVal.isFloat()) { |
2667 | Res = SVal.getFloat().bitcastToAPInt(); |
2668 | return true; |
2669 | } |
2670 | if (SVal.isVector()) { |
2671 | QualType VecTy = E->getType(); |
2672 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
2673 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
2674 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
2675 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
2676 | Res = llvm::APInt::getNullValue(VecSize); |
2677 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
2678 | APValue &Elt = SVal.getVectorElt(i); |
2679 | llvm::APInt EltAsInt; |
2680 | if (Elt.isInt()) { |
2681 | EltAsInt = Elt.getInt(); |
2682 | } else if (Elt.isFloat()) { |
2683 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
2684 | } else { |
2685 | |
2686 | |
2687 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2688 | return false; |
2689 | } |
2690 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
2691 | if (BigEndian) |
2692 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
2693 | else |
2694 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
2695 | } |
2696 | return true; |
2697 | } |
2698 | |
2699 | |
2700 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2701 | return false; |
2702 | } |
2703 | |
2704 | |
2705 | |
2706 | |
2707 | template<typename Operation> |
2708 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
2709 | const APSInt &LHS, const APSInt &RHS, |
2710 | unsigned BitWidth, Operation Op, |
2711 | APSInt &Result) { |
2712 | if (LHS.isUnsigned()) { |
2713 | Result = Op(LHS, RHS); |
2714 | return true; |
2715 | } |
2716 | |
2717 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
2718 | Result = Value.trunc(LHS.getBitWidth()); |
2719 | if (Result.extend(BitWidth) != Value) { |
2720 | if (Info.checkingForUndefinedBehavior()) |
2721 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
2722 | diag::warn_integer_constant_overflow) |
2723 | << toString(Result, 10) << E->getType(); |
2724 | return HandleOverflow(Info, E, Value, E->getType()); |
2725 | } |
2726 | return true; |
2727 | } |
2728 | |
2729 | |
2730 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
2731 | BinaryOperatorKind Opcode, APSInt RHS, |
2732 | APSInt &Result) { |
2733 | switch (Opcode) { |
2734 | default: |
2735 | Info.FFDiag(E); |
2736 | return false; |
2737 | case BO_Mul: |
2738 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2739 | std::multiplies<APSInt>(), Result); |
2740 | case BO_Add: |
2741 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2742 | std::plus<APSInt>(), Result); |
2743 | case BO_Sub: |
2744 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2745 | std::minus<APSInt>(), Result); |
2746 | case BO_And: Result = LHS & RHS; return true; |
2747 | case BO_Xor: Result = LHS ^ RHS; return true; |
2748 | case BO_Or: Result = LHS | RHS; return true; |
2749 | case BO_Div: |
2750 | case BO_Rem: |
2751 | if (RHS == 0) { |
2752 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
2753 | return false; |
2754 | } |
2755 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2756 | |
2757 | |
2758 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
2759 | LHS.isSigned() && LHS.isMinSignedValue()) |
2760 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
2761 | E->getType()); |
2762 | return true; |
2763 | case BO_Shl: { |
2764 | if (Info.getLangOpts().OpenCL) |
2765 | |
2766 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2767 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2768 | RHS.isUnsigned()); |
2769 | else if (RHS.isSigned() && RHS.isNegative()) { |
2770 | |
2771 | |
2772 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2773 | RHS = -RHS; |
2774 | goto shift_right; |
2775 | } |
2776 | shift_left: |
2777 | |
2778 | |
2779 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2780 | if (SA != RHS) { |
2781 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2782 | << RHS << E->getType() << LHS.getBitWidth(); |
2783 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) { |
2784 | |
2785 | |
2786 | |
2787 | |
2788 | if (LHS.isNegative()) |
2789 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
2790 | else if (LHS.countLeadingZeros() < SA) |
2791 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
2792 | } |
2793 | Result = LHS << SA; |
2794 | return true; |
2795 | } |
2796 | case BO_Shr: { |
2797 | if (Info.getLangOpts().OpenCL) |
2798 | |
2799 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2800 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2801 | RHS.isUnsigned()); |
2802 | else if (RHS.isSigned() && RHS.isNegative()) { |
2803 | |
2804 | |
2805 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2806 | RHS = -RHS; |
2807 | goto shift_left; |
2808 | } |
2809 | shift_right: |
2810 | |
2811 | |
2812 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2813 | if (SA != RHS) |
2814 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2815 | << RHS << E->getType() << LHS.getBitWidth(); |
2816 | Result = LHS >> SA; |
2817 | return true; |
2818 | } |
2819 | |
2820 | case BO_LT: Result = LHS < RHS; return true; |
2821 | case BO_GT: Result = LHS > RHS; return true; |
2822 | case BO_LE: Result = LHS <= RHS; return true; |
2823 | case BO_GE: Result = LHS >= RHS; return true; |
2824 | case BO_EQ: Result = LHS == RHS; return true; |
2825 | case BO_NE: Result = LHS != RHS; return true; |
2826 | case BO_Cmp: |
2827 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
2828 | } |
2829 | } |
2830 | |
2831 | |
2832 | static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E, |
2833 | APFloat &LHS, BinaryOperatorKind Opcode, |
2834 | const APFloat &RHS) { |
2835 | bool DynamicRM; |
2836 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E, DynamicRM); |
2837 | APFloat::opStatus St; |
2838 | switch (Opcode) { |
2839 | default: |
2840 | Info.FFDiag(E); |
2841 | return false; |
2842 | case BO_Mul: |
2843 | St = LHS.multiply(RHS, RM); |
2844 | break; |
2845 | case BO_Add: |
2846 | St = LHS.add(RHS, RM); |
2847 | break; |
2848 | case BO_Sub: |
2849 | St = LHS.subtract(RHS, RM); |
2850 | break; |
2851 | case BO_Div: |
2852 | |
2853 | |
2854 | if (RHS.isZero()) |
2855 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
2856 | St = LHS.divide(RHS, RM); |
2857 | break; |
2858 | } |
2859 | |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | if (LHS.isNaN()) { |
2865 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
2866 | return Info.noteUndefinedBehavior(); |
2867 | } |
2868 | |
2869 | return checkFloatingPointResult(Info, E, St); |
2870 | } |
2871 | |
2872 | static bool handleLogicalOpForVector(const APInt &LHSValue, |
2873 | BinaryOperatorKind Opcode, |
2874 | const APInt &RHSValue, APInt &Result) { |
2875 | bool LHS = (LHSValue != 0); |
2876 | bool RHS = (RHSValue != 0); |
2877 | |
2878 | if (Opcode == BO_LAnd) |
2879 | Result = LHS && RHS; |
2880 | else |
2881 | Result = LHS || RHS; |
2882 | return true; |
2883 | } |
2884 | static bool handleLogicalOpForVector(const APFloat &LHSValue, |
2885 | BinaryOperatorKind Opcode, |
2886 | const APFloat &RHSValue, APInt &Result) { |
2887 | bool LHS = !LHSValue.isZero(); |
2888 | bool RHS = !RHSValue.isZero(); |
2889 | |
2890 | if (Opcode == BO_LAnd) |
2891 | Result = LHS && RHS; |
2892 | else |
2893 | Result = LHS || RHS; |
2894 | return true; |
2895 | } |
2896 | |
2897 | static bool handleLogicalOpForVector(const APValue &LHSValue, |
2898 | BinaryOperatorKind Opcode, |
2899 | const APValue &RHSValue, APInt &Result) { |
2900 | |
2901 | if (LHSValue.getKind() == APValue::Int) |
2902 | return handleLogicalOpForVector(LHSValue.getInt(), Opcode, |
2903 | RHSValue.getInt(), Result); |
2904 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
2905 | return handleLogicalOpForVector(LHSValue.getFloat(), Opcode, |
2906 | RHSValue.getFloat(), Result); |
2907 | } |
2908 | |
2909 | template <typename APTy> |
2910 | static bool |
2911 | handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode, |
2912 | const APTy &RHSValue, APInt &Result) { |
2913 | switch (Opcode) { |
2914 | default: |
2915 | llvm_unreachable("unsupported binary operator"); |
2916 | case BO_EQ: |
2917 | Result = (LHSValue == RHSValue); |
2918 | break; |
2919 | case BO_NE: |
2920 | Result = (LHSValue != RHSValue); |
2921 | break; |
2922 | case BO_LT: |
2923 | Result = (LHSValue < RHSValue); |
2924 | break; |
2925 | case BO_GT: |
2926 | Result = (LHSValue > RHSValue); |
2927 | break; |
2928 | case BO_LE: |
2929 | Result = (LHSValue <= RHSValue); |
2930 | break; |
2931 | case BO_GE: |
2932 | Result = (LHSValue >= RHSValue); |
2933 | break; |
2934 | } |
2935 | |
2936 | return true; |
2937 | } |
2938 | |
2939 | static bool handleCompareOpForVector(const APValue &LHSValue, |
2940 | BinaryOperatorKind Opcode, |
2941 | const APValue &RHSValue, APInt &Result) { |
2942 | |
2943 | if (LHSValue.getKind() == APValue::Int) |
2944 | return handleCompareOpForVectorHelper(LHSValue.getInt(), Opcode, |
2945 | RHSValue.getInt(), Result); |
2946 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
2947 | return handleCompareOpForVectorHelper(LHSValue.getFloat(), Opcode, |
2948 | RHSValue.getFloat(), Result); |
2949 | } |
2950 | |
2951 | |
2952 | static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E, |
2953 | BinaryOperatorKind Opcode, |
2954 | APValue &LHSValue, |
2955 | const APValue &RHSValue) { |
2956 | assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI && |
2957 | "Operation not supported on vector types"); |
2958 | |
2959 | const auto *VT = E->getType()->castAs<VectorType>(); |
2960 | unsigned NumElements = VT->getNumElements(); |
2961 | QualType EltTy = VT->getElementType(); |
2962 | |
2963 | |
2964 | |
2965 | |
2966 | if (!LHSValue.isVector()) { |
2967 | assert(LHSValue.isLValue() && |
2968 | "A vector result that isn't a vector OR uncalculated LValue"); |
2969 | Info.FFDiag(E); |
2970 | return false; |
2971 | } |
2972 | |
2973 | assert(LHSValue.getVectorLength() == NumElements && |
2974 | RHSValue.getVectorLength() == NumElements && "Different vector sizes"); |
2975 | |
2976 | SmallVector<APValue, 4> ResultElements; |
2977 | |
2978 | for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) { |
2979 | APValue LHSElt = LHSValue.getVectorElt(EltNum); |
2980 | APValue RHSElt = RHSValue.getVectorElt(EltNum); |
2981 | |
2982 | if (EltTy->isIntegerType()) { |
2983 | APSInt EltResult{Info.Ctx.getIntWidth(EltTy), |
2984 | EltTy->isUnsignedIntegerType()}; |
2985 | bool Success = true; |
2986 | |
2987 | if (BinaryOperator::isLogicalOp(Opcode)) |
2988 | Success = handleLogicalOpForVector(LHSElt, Opcode, RHSElt, EltResult); |
2989 | else if (BinaryOperator::isComparisonOp(Opcode)) |
2990 | Success = handleCompareOpForVector(LHSElt, Opcode, RHSElt, EltResult); |
2991 | else |
2992 | Success = handleIntIntBinOp(Info, E, LHSElt.getInt(), Opcode, |
2993 | RHSElt.getInt(), EltResult); |
2994 | |
2995 | if (!Success) { |
2996 | Info.FFDiag(E); |
2997 | return false; |
2998 | } |
2999 | ResultElements.emplace_back(EltResult); |
3000 | |
3001 | } else if (EltTy->isFloatingType()) { |
3002 | assert(LHSElt.getKind() == APValue::Float && |
3003 | RHSElt.getKind() == APValue::Float && |
3004 | "Mismatched LHS/RHS/Result Type"); |
3005 | APFloat LHSFloat = LHSElt.getFloat(); |
3006 | |
3007 | if (!handleFloatFloatBinOp(Info, E, LHSFloat, Opcode, |
3008 | RHSElt.getFloat())) { |
3009 | Info.FFDiag(E); |
3010 | return false; |
3011 | } |
3012 | |
3013 | ResultElements.emplace_back(LHSFloat); |
3014 | } |
3015 | } |
3016 | |
3017 | LHSValue = APValue(ResultElements.data(), ResultElements.size()); |
3018 | return true; |
3019 | } |
3020 | |
3021 | |
3022 | |
3023 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3024 | const RecordDecl *TruncatedType, |
3025 | unsigned TruncatedElements) { |
3026 | SubobjectDesignator &D = Result.Designator; |
3027 | |
3028 | |
3029 | if (TruncatedElements == D.Entries.size()) |
3030 | return true; |
3031 | assert(TruncatedElements >= D.MostDerivedPathLength && |
3032 | "not casting to a derived class"); |
3033 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
3034 | return false; |
3035 | |
3036 | |
3037 | const RecordDecl *RD = TruncatedType; |
3038 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
3039 | if (RD->isInvalidDecl()) return false; |
3040 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
3041 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
3042 | if (isVirtualBaseClass(D.Entries[I])) |
3043 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
3044 | else |
3045 | Result.Offset -= Layout.getBaseClassOffset(Base); |
3046 | RD = Base; |
3047 | } |
3048 | D.Entries.resize(TruncatedElements); |
3049 | return true; |
3050 | } |
3051 | |
3052 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3053 | const CXXRecordDecl *Derived, |
3054 | const CXXRecordDecl *Base, |
3055 | const ASTRecordLayout *RL = nullptr) { |
3056 | if (!RL) { |
3057 | if (Derived->isInvalidDecl()) return false; |
3058 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
3059 | } |
3060 | |
3061 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
3062 | Obj.addDecl(Info, E, Base, false); |
3063 | return true; |
3064 | } |
3065 | |
3066 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3067 | const CXXRecordDecl *DerivedDecl, |
3068 | const CXXBaseSpecifier *Base) { |
3069 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
3070 | |
3071 | if (!Base->isVirtual()) |
3072 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
3073 | |
3074 | SubobjectDesignator &D = Obj.Designator; |
3075 | if (D.Invalid) |
3076 | return false; |
3077 | |
3078 | |
3079 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
3080 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
3081 | return false; |
3082 | |
3083 | |
3084 | if (DerivedDecl->isInvalidDecl()) return false; |
3085 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
3086 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
3087 | Obj.addDecl(Info, E, BaseDecl, true); |
3088 | return true; |
3089 | } |
3090 | |
3091 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
3092 | QualType Type, LValue &Result) { |
3093 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
3094 | PathE = E->path_end(); |
3095 | PathI != PathE; ++PathI) { |
3096 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
3097 | *PathI)) |
3098 | return false; |
3099 | Type = (*PathI)->getType(); |
3100 | } |
3101 | return true; |
3102 | } |
3103 | |
3104 | |
3105 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3106 | const CXXRecordDecl *DerivedRD, |
3107 | const CXXRecordDecl *BaseRD) { |
3108 | CXXBasePaths Paths(false, |
3109 | true, false); |
3110 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
3111 | llvm_unreachable("Class must be derived from the passed in base class!"); |
3112 | |
3113 | for (CXXBasePathElement &Elem : Paths.front()) |
3114 | if (!HandleLValueBase(Info, E, Result, Elem.Class, Elem.Base)) |
3115 | return false; |
3116 | return true; |
3117 | } |
3118 | |
3119 | |
3120 | |
3121 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
3122 | const FieldDecl *FD, |
3123 | const ASTRecordLayout *RL = nullptr) { |
3124 | if (!RL) { |
3125 | if (FD->getParent()->isInvalidDecl()) return false; |
3126 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
3127 | } |
3128 | |
3129 | unsigned I = FD->getFieldIndex(); |
3130 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
3131 | LVal.addDecl(Info, E, FD); |
3132 | return true; |
3133 | } |
3134 | |
3135 | |
3136 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
3137 | LValue &LVal, |
3138 | const IndirectFieldDecl *IFD) { |
3139 | for (const auto *C : IFD->chain()) |
3140 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
3141 | return false; |
3142 | return true; |
3143 | } |
3144 | |
3145 | |
3146 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
3147 | QualType Type, CharUnits &Size) { |
3148 | |
3149 | |
3150 | if (Type->isVoidType() || Type->isFunctionType()) { |
3151 | Size = CharUnits::One(); |
3152 | return true; |
3153 | } |
3154 | |
3155 | if (Type->isDependentType()) { |
3156 | Info.FFDiag(Loc); |
3157 | return false; |
3158 | } |
3159 | |
3160 | if (!Type->isConstantSizeType()) { |
3161 | |
3162 | |
3163 | Info.FFDiag(Loc); |
3164 | return false; |
3165 | } |
3166 | |
3167 | Size = Info.Ctx.getTypeSizeInChars(Type); |
3168 | return true; |
3169 | } |
3170 | |
3171 | |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3178 | LValue &LVal, QualType EltTy, |
3179 | APSInt Adjustment) { |
3180 | CharUnits SizeOfPointee; |
3181 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
3182 | return false; |
3183 | |
3184 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
3185 | return true; |
3186 | } |
3187 | |
3188 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3189 | LValue &LVal, QualType EltTy, |
3190 | int64_t Adjustment) { |
3191 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
3192 | APSInt::get(Adjustment)); |
3193 | } |
3194 | |
3195 | |
3196 | |
3197 | |
3198 | |
3199 | |
3200 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
3201 | LValue &LVal, QualType EltTy, |
3202 | bool Imag) { |
3203 | if (Imag) { |
3204 | CharUnits SizeOfComponent; |
3205 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
3206 | return false; |
3207 | LVal.Offset += SizeOfComponent; |
3208 | } |
3209 | LVal.addComplex(Info, E, EltTy, Imag); |
3210 | return true; |
3211 | } |
3212 | |
3213 | |
3214 | |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | |
3221 | |
3222 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
3223 | const VarDecl *VD, CallStackFrame *Frame, |
3224 | unsigned Version, APValue *&Result) { |
3225 | APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version); |
3226 | |
3227 | |
3228 | if (Frame) { |
3229 | Result = Frame->getTemporary(VD, Version); |
3230 | if (Result) |
3231 | return true; |
3232 | |
3233 | if (!isa<ParmVarDecl>(VD)) { |
3234 | |
3235 | |
3236 | |
3237 | |
3238 | assert(isLambdaCallOperator(Frame->Callee) && |
3239 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
3240 | "missing value for local variable"); |
3241 | if (Info.checkingPotentialConstantExpression()) |
3242 | return false; |
3243 | |
3244 | |
3245 | Info.FFDiag(E->getBeginLoc(), |
3246 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
3247 | << "captures not currently allowed"; |
3248 | return false; |
3249 | } |
3250 | } |
3251 | |
3252 | |
3253 | |
3254 | if (Info.EvaluatingDecl == Base) { |
3255 | Result = Info.EvaluatingDeclValue; |
3256 | return true; |
3257 | } |
3258 | |
3259 | if (isa<ParmVarDecl>(VD)) { |
3260 | |
3261 | |
3262 | if (!Info.checkingPotentialConstantExpression() || |
3263 | !Info.CurrentCall->Callee || |
3264 | !Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
3265 | if (Info.getLangOpts().CPlusPlus11) { |
3266 | Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown) |
3267 | << VD; |
3268 | NoteLValueLocation(Info, Base); |
3269 | } else { |
3270 | Info.FFDiag(E); |
3271 | } |
3272 | } |
3273 | return false; |
3274 | } |
3275 | |
3276 | |
3277 | |
3278 | |
3279 | const Expr *Init = VD->getAnyInitializer(VD); |
3280 | if (!Init) { |
3281 | |
3282 | |
3283 | if (!Info.checkingPotentialConstantExpression()) { |
3284 | Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) |
3285 | << VD; |
3286 | NoteLValueLocation(Info, Base); |
3287 | } |
3288 | return false; |
3289 | } |
3290 | |
3291 | if (Init->isValueDependent()) { |
3292 | |
3293 | |
3294 | |
3295 | |
3296 | |
3297 | assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx)); |
3298 | if (!Info.checkingPotentialConstantExpression()) { |
3299 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
3300 | ? diag::note_constexpr_ltor_non_constexpr |
3301 | : diag::note_constexpr_ltor_non_integral, 1) |
3302 | << VD << VD->getType(); |
3303 | NoteLValueLocation(Info, Base); |
3304 | } |
3305 | return false; |
3306 | } |
3307 | |
3308 | |
3309 | |
3310 | if (!VD->evaluateValue()) { |
3311 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3312 | NoteLValueLocation(Info, Base); |
3313 | return false; |
3314 | } |
3315 | |
3316 | |
3317 | |
3318 | |
3319 | |
3320 | |
3321 | |
3322 | |
3323 | |
3324 | |
3325 | if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() && |
3326 | VD->mightBeUsableInConstantExpressions(Info.Ctx)) || |
3327 | ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) && |
3328 | !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) { |
3329 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3330 | NoteLValueLocation(Info, Base); |
3331 | } |
3332 | |
3333 | |
3334 | |
3335 | if (VD->isWeak()) { |
3336 | Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD; |
3337 | NoteLValueLocation(Info, Base); |
3338 | return false; |
3339 | } |
3340 | |
3341 | Result = VD->getEvaluatedValue(); |
3342 | return true; |
3343 | } |
3344 | |
3345 | |
3346 | |
3347 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
3348 | const CXXRecordDecl *Base) { |
3349 | Base = Base->getCanonicalDecl(); |
3350 | unsigned Index = 0; |
3351 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
3352 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
3353 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
3354 | return Index; |
3355 | } |
3356 | |
3357 | llvm_unreachable("base class missing from derived class's bases list"); |
3358 | } |
3359 | |
3360 | |
3361 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
3362 | uint64_t Index) { |
3363 | assert(!isa<SourceLocExpr>(Lit) && |
3364 | "SourceLocExpr should have already been converted to a StringLiteral"); |
3365 | |
3366 | |
3367 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
3368 | std::string Str; |
3369 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
3370 | assert(Index <= Str.size() && "Index too large"); |
3371 | return APSInt::getUnsigned(Str.c_str()[Index]); |
3372 | } |
3373 | |
3374 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
3375 | Lit = PE->getFunctionName(); |
3376 | const StringLiteral *S = cast<StringLiteral>(Lit); |
3377 | const ConstantArrayType *CAT = |
3378 | Info.Ctx.getAsConstantArrayType(S->getType()); |
3379 | assert(CAT && "string literal isn't an array"); |
3380 | QualType CharType = CAT->getElementType(); |
3381 | assert(CharType->isIntegerType() && "unexpected character type"); |
3382 | |
3383 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
3384 | CharType->isUnsignedIntegerType()); |
3385 | if (Index < S->getLength()) |
3386 | Value = S->getCodeUnit(Index); |
3387 | return Value; |
3388 | } |
3389 | |
3390 | |
3391 | |
3392 | |
3393 | |
3394 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
3395 | APValue &Result, |
3396 | QualType AllocType = QualType()) { |
3397 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
3398 | AllocType.isNull() ? S->getType() : AllocType); |
3399 | assert(CAT && "string literal isn't an array"); |
3400 | QualType CharType = CAT->getElementType(); |
3401 | assert(CharType->isIntegerType() && "unexpected character type"); |
3402 | |
3403 | unsigned Elts = CAT->getSize().getZExtValue(); |
3404 | Result = APValue(APValue::UninitArray(), |
3405 | std::min(S->getLength(), Elts), Elts); |
3406 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
3407 | CharType->isUnsignedIntegerType()); |
3408 | if (Result.hasArrayFiller()) |
3409 | Result.getArrayFiller() = APValue(Value); |
3410 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
3411 | Value = S->getCodeUnit(I); |
3412 | Result.getArrayInitializedElt(I) = APValue(Value); |
3413 | } |
3414 | } |
3415 | |
3416 | |
3417 | static void expandArray(APValue &Array, unsigned Index) { |
3418 | unsigned Size = Array.getArraySize(); |
3419 | assert(Index < Size); |
3420 | |
3421 | |
3422 | unsigned OldElts = Array.getArrayInitializedElts(); |
3423 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
3424 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
3425 | |
3426 | |
3427 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
3428 | for (unsigned I = 0; I != OldElts; ++I) |
3429 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
3430 | for (unsigned I = OldElts; I != NewElts; ++I) |
3431 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
3432 | if (NewValue.hasArrayFiller()) |
3433 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
3434 | Array.swap(NewValue); |
3435 | } |
3436 | |
3437 | |
3438 | |
3439 | |
3440 | |
3441 | |
3442 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD); |
3443 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
3444 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3445 | return !RD || isReadByLvalueToRvalueConversion(RD); |
3446 | } |
3447 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) { |
3448 | |
3449 | |
3450 | if (RD->isUnion()) |
| 38 | | Calling 'TagDecl::isUnion' | |
|
| 41 | | Returning from 'TagDecl::isUnion' | |
|
| |
3451 | return !RD->field_empty(); |
| 43 | | Calling 'RecordDecl::field_empty' | |
|
| 52 | | Returning from 'RecordDecl::field_empty' | |
|
| 53 | | Returning the value 1, which participates in a condition later | |
|
3452 | if (RD->isEmpty()) |
3453 | return false; |
3454 | |
3455 | for (auto *Field : RD->fields()) |
3456 | if (!Field->isUnnamedBitfield() && |
3457 | isReadByLvalueToRvalueConversion(Field->getType())) |
3458 | return true; |
3459 | |
3460 | for (auto &BaseSpec : RD->bases()) |
3461 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
3462 | return true; |
3463 | |
3464 | return false; |
3465 | } |
3466 | |
3467 | |
3468 | |
3469 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
3470 | QualType T) { |
3471 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3472 | if (!RD) |
3473 | return false; |
3474 | |
3475 | if (!RD->hasMutableFields()) |
3476 | return false; |
3477 | |
3478 | for (auto *Field : RD->fields()) { |
3479 | |
3480 | |
3481 | |
3482 | |
3483 | if (Field->isMutable() && |
3484 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
3485 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
3486 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3487 | return true; |
3488 | } |
3489 | |
3490 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
3491 | return true; |
3492 | } |
3493 | |
3494 | for (auto &BaseSpec : RD->bases()) |
3495 | if (diagnoseMutableFields(Info, E, AK, BaseSpec.getType())) |
3496 | return true; |
3497 | |
3498 | |
3499 | return false; |
3500 | } |
3501 | |
3502 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
3503 | APValue::LValueBase Base, |
3504 | bool MutableSubobject = false) { |
3505 | |
3506 | if (Base.getCallIndex() || Base.is<DynamicAllocLValue>()) |
3507 | return true; |
3508 | |
3509 | switch (Info.IsEvaluatingDecl) { |
3510 | case EvalInfo::EvaluatingDeclKind::None: |
3511 | return false; |
3512 | |
3513 | case EvalInfo::EvaluatingDeclKind::Ctor: |
3514 | |
3515 | if (Info.EvaluatingDecl == Base) |
3516 | return true; |
3517 | |
3518 | |
3519 | |
3520 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
3521 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(BaseE)) |
3522 | return Info.EvaluatingDecl == BaseMTE->getExtendingDecl(); |
3523 | return false; |
3524 | |
3525 | case EvalInfo::EvaluatingDeclKind::Dtor: |
3526 | |
3527 | |
3528 | |
3529 | |
3530 | if (MutableSubobject || Base != Info.EvaluatingDecl) |
3531 | return false; |
3532 | |
3533 | |
3534 | |
3535 | QualType T = getType(Base); |
3536 | return T.isConstQualified() || T->isReferenceType(); |
3537 | } |
3538 | |
3539 | llvm_unreachable("unknown evaluating decl kind"); |
3540 | } |
3541 | |
3542 | namespace { |
3543 | |
3544 | |
3545 | struct CompleteObject { |
3546 | |
3547 | APValue::LValueBase Base; |
3548 | |
3549 | APValue *Value; |
3550 | |
3551 | QualType Type; |
3552 | |
3553 | CompleteObject() : Value(nullptr) {} |
3554 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
3555 | : Base(Base), Value(Value), Type(Type) {} |
3556 | |
3557 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | if (!isAnyAccess(AK)) |
3563 | return true; |
3564 | |
3565 | |
3566 | |
3567 | |
3568 | if (!Info.getLangOpts().CPlusPlus14) |
3569 | return false; |
3570 | return lifetimeStartedInEvaluation(Info, Base, true); |
3571 | } |
3572 | |
3573 | explicit operator bool() const { return !Type.isNull(); } |
3574 | }; |
3575 | } |
3576 | |
3577 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
3578 | bool IsMutable = false) { |
3579 | |
3580 | |
3581 | |
3582 | if (ObjType.isConstQualified() && !IsMutable) |
3583 | SubobjType.addConst(); |
3584 | |
3585 | |
3586 | if (ObjType.isVolatileQualified()) |
3587 | SubobjType.addVolatile(); |
3588 | return SubobjType; |
3589 | } |
3590 | |
3591 | |
3592 | template<typename SubobjectHandler> |
3593 | typename SubobjectHandler::result_type |
3594 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
3595 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
3596 | if (Sub.Invalid) |
3597 | |
3598 | return handler.failed(); |
3599 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
3600 | if (Info.getLangOpts().CPlusPlus11) |
3601 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
3602 | ? diag::note_constexpr_access_past_end |
3603 | : diag::note_constexpr_access_unsized_array) |
3604 | << handler.AccessKind; |
3605 | else |
3606 | Info.FFDiag(E); |
3607 | return handler.failed(); |
3608 | } |
3609 | |
3610 | APValue *O = Obj.Value; |
3611 | QualType ObjType = Obj.Type; |
3612 | const FieldDecl *LastField = nullptr; |
3613 | const FieldDecl *VolatileField = nullptr; |
3614 | |
3615 | |
3616 | for (unsigned I = 0, N = Sub.Entries.size(); ; ++I) { |
3617 | |
3618 | if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) || |
3619 | (O->isIndeterminate() && |
3620 | !isValidIndeterminateAccess(handler.AccessKind))) { |
3621 | if (!Info.checkingPotentialConstantExpression()) |
3622 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
3623 | << handler.AccessKind << O->isIndeterminate(); |
3624 | return handler.failed(); |
3625 | } |
3626 | |
3627 | |
3628 | |
3629 | |
3630 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
3631 | ObjType->isRecordType() && |
3632 | Info.isEvaluatingCtorDtor( |
3633 | Obj.Base, llvm::makeArrayRef(Sub.Entries.begin(), |
3634 | Sub.Entries.begin() + I)) != |
3635 | ConstructionPhase::None) { |
3636 | ObjType = Info.Ctx.getCanonicalType(ObjType); |
3637 | ObjType.removeLocalConst(); |
3638 | ObjType.removeLocalVolatile(); |
3639 | } |
3640 | |
3641 | |
3642 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
3643 | |
3644 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
3645 | if (Info.getLangOpts().CPlusPlus) { |
3646 | int DiagKind; |
3647 | SourceLocation Loc; |
3648 | const NamedDecl *Decl = nullptr; |
3649 | if (VolatileField) { |
3650 | DiagKind = 2; |
3651 | Loc = VolatileField->getLocation(); |
3652 | Decl = VolatileField; |
3653 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
3654 | DiagKind = 1; |
3655 | Loc = VD->getLocation(); |
3656 | Decl = VD; |
3657 | } else { |
3658 | DiagKind = 0; |
3659 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
3660 | Loc = E->getExprLoc(); |
3661 | } |
3662 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3663 | << handler.AccessKind << DiagKind << Decl; |
3664 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
3665 | } else { |
3666 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
3667 | } |
3668 | return handler.failed(); |
3669 | } |
3670 | |
3671 | |
3672 | |
3673 | |
3674 | |
3675 | if (ObjType->isRecordType() && |
3676 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind) && |
3677 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
3678 | return handler.failed(); |
3679 | } |
3680 | |
3681 | if (I == N) { |
3682 | if (!handler.found(*O, ObjType)) |
3683 | return false; |
3684 | |
3685 | |
3686 | if (isModification(handler.AccessKind) && |
3687 | LastField && LastField->isBitField() && |
3688 | !truncateBitfieldValue(Info, E, *O, LastField)) |
3689 | return false; |
3690 | |
3691 | return true; |
3692 | } |
3693 | |
3694 | LastField = nullptr; |
3695 | if (ObjType->isArrayType()) { |
3696 | |
3697 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
3698 | assert(CAT && "vla in literal type?"); |
3699 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3700 | if (CAT->getSize().ule(Index)) { |
3701 | |
3702 | |
3703 | if (Info.getLangOpts().CPlusPlus11) |
3704 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3705 | << handler.AccessKind; |
3706 | else |
3707 | Info.FFDiag(E); |
3708 | return handler.failed(); |
3709 | } |
3710 | |
3711 | ObjType = CAT->getElementType(); |
3712 | |
3713 | if (O->getArrayInitializedElts() > Index) |
3714 | O = &O->getArrayInitializedElt(Index); |
3715 | else if (!isRead(handler.AccessKind)) { |
3716 | expandArray(*O, Index); |
3717 | O = &O->getArrayInitializedElt(Index); |
3718 | } else |
3719 | O = &O->getArrayFiller(); |
3720 | } else if (ObjType->isAnyComplexType()) { |
3721 | |
3722 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3723 | if (Index > 1) { |
3724 | if (Info.getLangOpts().CPlusPlus11) |
3725 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3726 | << handler.AccessKind; |
3727 | else |
3728 | Info.FFDiag(E); |
3729 | return handler.failed(); |
3730 | } |
3731 | |
3732 | ObjType = getSubobjectType( |
3733 | ObjType, ObjType->castAs<ComplexType>()->getElementType()); |
3734 | |
3735 | assert(I == N - 1 && "extracting subobject of scalar?"); |
3736 | if (O->isComplexInt()) { |
3737 | return handler.found(Index ? O->getComplexIntImag() |
3738 | : O->getComplexIntReal(), ObjType); |
3739 | } else { |
3740 | assert(O->isComplexFloat()); |
3741 | return handler.found(Index ? O->getComplexFloatImag() |
3742 | : O->getComplexFloatReal(), ObjType); |
3743 | } |
3744 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
3745 | if (Field->isMutable() && |
3746 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind)) { |
3747 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
3748 | << handler.AccessKind << Field; |
3749 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3750 | return handler.failed(); |
3751 | } |
3752 | |
3753 | |
3754 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
3755 | if (RD->isUnion()) { |
3756 | const FieldDecl *UnionField = O->getUnionField(); |
3757 | if (!UnionField || |
3758 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
3759 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
3760 | |
3761 | O->setUnion(Field, APValue()); |
3762 | } else { |
3763 | |
3764 | |
3765 | |
3766 | |
3767 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
3768 | << handler.AccessKind << Field << !UnionField << UnionField; |
3769 | return handler.failed(); |
3770 | } |
3771 | } |
3772 | O = &O->getUnionValue(); |
3773 | } else |
3774 | O = &O->getStructField(Field->getFieldIndex()); |
3775 | |
3776 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
3777 | LastField = Field; |
3778 | if (Field->getType().isVolatileQualified()) |
3779 | VolatileField = Field; |
3780 | } else { |
3781 | |
3782 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
3783 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
3784 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
3785 | |
3786 | ObjType = getSubobjectType(ObjType, Info.Ctx.getRecordType(Base)); |
3787 | } |
3788 | } |
3789 | } |
3790 | |
3791 | namespace { |
3792 | struct ExtractSubobjectHandler { |
3793 | EvalInfo &Info; |
3794 | const Expr *E; |
3795 | APValue &Result; |
3796 | const AccessKinds AccessKind; |
3797 | |
3798 | typedef bool result_type; |
3799 | bool failed() { return false; } |
3800 | bool found(APValue &Subobj, QualType SubobjType) { |
3801 | Result = Subobj; |
3802 | if (AccessKind == AK_ReadObjectRepresentation) |
3803 | return true; |
3804 | return CheckFullyInitialized(Info, E->getExprLoc(), SubobjType, Result); |
3805 | } |
3806 | bool found(APSInt &Value, QualType SubobjType) { |
3807 | Result = APValue(Value); |
3808 | return true; |
3809 | } |
3810 | bool found(APFloat &Value, QualType SubobjType) { |
3811 | Result = APValue(Value); |
3812 | return true; |
3813 | } |
3814 | }; |
3815 | } |
3816 | |
3817 | |
3818 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
3819 | const CompleteObject &Obj, |
3820 | const SubobjectDesignator &Sub, APValue &Result, |
3821 | AccessKinds AK = AK_Read) { |
3822 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
3823 | ExtractSubobjectHandler Handler = {Info, E, Result, AK}; |
3824 | return findSubobject(Info, E, Obj, Sub, Handler); |
3825 | } |
3826 | |
3827 | namespace { |
3828 | struct ModifySubobjectHandler { |
3829 | EvalInfo &Info; |
3830 | APValue &NewVal; |
3831 | const Expr *E; |
3832 | |
3833 | typedef bool result_type; |
3834 | static const AccessKinds AccessKind = AK_Assign; |
3835 | |
3836 | bool checkConst(QualType QT) { |
3837 | |
3838 | if (QT.isConstQualified()) { |
3839 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3840 | return false; |
3841 | } |
3842 | return true; |
3843 | } |
3844 | |
3845 | bool failed() { return false; } |
3846 | bool found(APValue &Subobj, QualType SubobjType) { |
3847 | if (!checkConst(SubobjType)) |
3848 | return false; |
3849 | |
3850 | Subobj.swap(NewVal); |
3851 | return true; |
3852 | } |
3853 | bool found(APSInt &Value, QualType SubobjType) { |
3854 | if (!checkConst(SubobjType)) |
3855 | return false; |
3856 | if (!NewVal.isInt()) { |
3857 | |
3858 | Info.FFDiag(E); |
3859 | return false; |
3860 | } |
3861 | Value = NewVal.getInt(); |
3862 | return true; |
3863 | } |
3864 | bool found(APFloat &Value, QualType SubobjType) { |
3865 | if (!checkConst(SubobjType)) |
3866 | return false; |
3867 | Value = NewVal.getFloat(); |
3868 | return true; |
3869 | } |
3870 | }; |
3871 | } |
3872 | |
3873 | const AccessKinds ModifySubobjectHandler::AccessKind; |
3874 | |
3875 | |
3876 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
3877 | const CompleteObject &Obj, |
3878 | const SubobjectDesignator &Sub, |
3879 | APValue &NewVal) { |
3880 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
3881 | return findSubobject(Info, E, Obj, Sub, Handler); |
3882 | } |
3883 | |
3884 | |
3885 | |
3886 | static unsigned FindDesignatorMismatch(QualType ObjType, |
3887 | const SubobjectDesignator &A, |
3888 | const SubobjectDesignator &B, |
3889 | bool &WasArrayIndex) { |
3890 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
3891 | for (; I != N; ++I) { |
3892 | if (!ObjType.isNull() && |
3893 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
3894 | |
3895 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
3896 | WasArrayIndex = true; |
3897 | return I; |
3898 | } |
3899 | if (ObjType->isAnyComplexType()) |
3900 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
3901 | else |
3902 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
3903 | } else { |
3904 | if (A.Entries[I].getAsBaseOrMember() != |
3905 | B.Entries[I].getAsBaseOrMember()) { |
3906 | WasArrayIndex = false; |
3907 | return I; |
3908 | } |
3909 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
3910 | |
3911 | ObjType = FD->getType(); |
3912 | else |
3913 | |
3914 | ObjType = QualType(); |
3915 | } |
3916 | } |
3917 | WasArrayIndex = false; |
3918 | return I; |
3919 | } |
3920 | |
3921 | |
3922 | |
3923 | static bool AreElementsOfSameArray(QualType ObjType, |
3924 | const SubobjectDesignator &A, |
3925 | const SubobjectDesignator &B) { |
3926 | if (A.Entries.size() != B.Entries.size()) |
3927 | return false; |
3928 | |
3929 | bool IsArray = A.MostDerivedIsArrayElement; |
3930 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
3931 | |
3932 | return false; |
3933 | |
3934 | |
3935 | |
3936 | |
3937 | bool WasArrayIndex; |
3938 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
3939 | return CommonLength >= A.Entries.size() - IsArray; |
3940 | } |
3941 | |
3942 | |
3943 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
3944 | AccessKinds AK, const LValue &LVal, |
3945 | QualType LValType) { |
3946 | if (LVal.InvalidBase) { |
3947 | Info.FFDiag(E); |
3948 | return CompleteObject(); |
3949 | } |
3950 | |
3951 | if (!LVal.Base) { |
3952 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
3953 | return CompleteObject(); |
3954 | } |
3955 | |
3956 | CallStackFrame *Frame = nullptr; |
3957 | unsigned Depth = 0; |
3958 | if (LVal.getLValueCallIndex()) { |
3959 | std::tie(Frame, Depth) = |
3960 | Info.getCallFrameAndDepth(LVal.getLValueCallIndex()); |
3961 | if (!Frame) { |
3962 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
3963 | << AK << LVal.Base.is<const ValueDecl*>(); |
3964 | NoteLValueLocation(Info, LVal.Base); |
3965 | return CompleteObject(); |
3966 | } |
3967 | } |
3968 | |
3969 | bool IsAccess = isAnyAccess(AK); |
3970 | |
3971 | |
3972 | |
3973 | |
3974 | |
3975 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
3976 | if (Info.getLangOpts().CPlusPlus) |
3977 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
3978 | << AK << LValType; |
3979 | else |
3980 | Info.FFDiag(E); |
3981 | return CompleteObject(); |
3982 | } |
3983 | |
3984 | |
3985 | APValue *BaseVal = nullptr; |
3986 | QualType BaseType = getType(LVal.Base); |
3987 | |
3988 | if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl && |
3989 | lifetimeStartedInEvaluation(Info, LVal.Base)) { |
3990 | |
3991 | |
3992 | BaseVal = Info.EvaluatingDeclValue; |
3993 | } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) { |
3994 | |
3995 | if (auto *GD = dyn_cast<MSGuidDecl>(D)) { |
3996 | if (isModification(AK)) { |
3997 | |
3998 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
3999 | return CompleteObject(); |
4000 | } |
4001 | APValue &V = GD->getAsAPValue(); |
4002 | if (V.isAbsent()) { |
4003 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
4004 | << GD->getType(); |
4005 | return CompleteObject(); |
4006 | } |
4007 | return CompleteObject(LVal.Base, &V, GD->getType()); |
4008 | } |
4009 | |
4010 | |
4011 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) { |
4012 | if (isModification(AK)) { |
4013 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4014 | return CompleteObject(); |
4015 | } |
4016 | return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()), |
4017 | TPO->getType()); |
4018 | } |
4019 | |
4020 | |
4021 | |
4022 | |
4023 | |
4024 | |
4025 | |
4026 | |
4027 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
4028 | if (VD) { |
4029 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
4030 | VD = VDef; |
4031 | } |
4032 | if (!VD || VD->isInvalidDecl()) { |
4033 | Info.FFDiag(E); |
4034 | return CompleteObject(); |
4035 | } |
4036 | |
4037 | bool IsConstant = BaseType.isConstant(Info.Ctx); |
4038 | |
4039 | |
4040 | |
4041 | if (!Frame) { |
4042 | if (IsAccess && isa<ParmVarDecl>(VD)) { |
4043 | |
4044 | |
4045 | |
4046 | } else if (Info.getLangOpts().CPlusPlus14 && |
4047 | lifetimeStartedInEvaluation(Info, LVal.Base)) { |
4048 | |
4049 | |
4050 | |
4051 | } else if (isModification(AK)) { |
4052 | |
4053 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4054 | return CompleteObject(); |
4055 | } else if (VD->isConstexpr()) { |
4056 | |
4057 | } else if (BaseType->isIntegralOrEnumerationType()) { |
4058 | if (!IsConstant) { |
4059 | if (!IsAccess) |
4060 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4061 | if (Info.getLangOpts().CPlusPlus) { |
4062 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
4063 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4064 | } else { |
4065 | Info.FFDiag(E); |
4066 | } |
4067 | return CompleteObject(); |
4068 | } |
4069 | } else if (!IsAccess) { |
4070 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4071 | } else if (IsConstant && Info.checkingPotentialConstantExpression() && |
4072 | BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) { |
4073 | |
4074 | } else if (IsConstant) { |
4075 | |
4076 | |
4077 | |
4078 | if (Info.getLangOpts().CPlusPlus) { |
4079 | Info.CCEDiag(E, Info.getLangOpts().CPlusPlus11 |
4080 | ? diag::note_constexpr_ltor_non_constexpr |
4081 | : diag::note_constexpr_ltor_non_integral, 1) |
4082 | << VD << BaseType; |
4083 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4084 | } else { |
4085 | Info.CCEDiag(E); |
4086 | } |
4087 | } else { |
4088 | |
4089 | if (Info.getLangOpts().CPlusPlus) { |
4090 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
4091 | ? diag::note_constexpr_ltor_non_constexpr |
4092 | : diag::note_constexpr_ltor_non_integral, 1) |
4093 | << VD << BaseType; |
4094 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4095 | } else { |
4096 | Info.FFDiag(E); |
4097 | } |
4098 | return CompleteObject(); |
4099 | } |
4100 | } |
4101 | |
4102 | if (!evaluateVarDeclInit(Info, E, VD, Frame, LVal.getLValueVersion(), BaseVal)) |
4103 | return CompleteObject(); |
4104 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
4105 | Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA); |
4106 | if (!Alloc) { |
4107 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
4108 | return CompleteObject(); |
4109 | } |
4110 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
4111 | LVal.Base.getDynamicAllocType()); |
4112 | } else { |
4113 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4114 | |
4115 | if (!Frame) { |
4116 | if (const MaterializeTemporaryExpr *MTE = |
4117 | dyn_cast_or_null<MaterializeTemporaryExpr>(Base)) { |
4118 | assert(MTE->getStorageDuration() == SD_Static && |
4119 | "should have a frame for a non-global materialized temporary"); |
4120 | |
4121 | |
4122 | |
4123 | |
4124 | |
4125 | |
4126 | |
4127 | |
4128 | |
4129 | |
4130 | |
4131 | |
4132 | |
4133 | |
4134 | |
4135 | |
4136 | |
4137 | |
4138 | |
4139 | |
4140 | |
4141 | |
4142 | |
4143 | |
4144 | |
4145 | |
4146 | if (!MTE->isUsableInConstantExpressions(Info.Ctx) && |
4147 | !lifetimeStartedInEvaluation(Info, LVal.Base)) { |
4148 | if (!IsAccess) |
4149 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4150 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
4151 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
4152 | return CompleteObject(); |
4153 | } |
4154 | |
4155 | BaseVal = MTE->getOrCreateValue(false); |
4156 | assert(BaseVal && "got reference to unevaluated temporary"); |
4157 | } else { |
4158 | if (!IsAccess) |
4159 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4160 | APValue Val; |
4161 | LVal.moveInto(Val); |
4162 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
4163 | << AK |
4164 | << Val.getAsString(Info.Ctx, |
4165 | Info.Ctx.getLValueReferenceType(LValType)); |
4166 | NoteLValueLocation(Info, LVal.Base); |
4167 | return CompleteObject(); |
4168 | } |
4169 | } else { |
4170 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
4171 | assert(BaseVal && "missing value for temporary"); |
4172 | } |
4173 | } |
4174 | |
4175 | |
4176 | |
4177 | |
4178 | |
4179 | |
4180 | |
4181 | |
4182 | unsigned VisibleDepth = Depth; |
4183 | if (llvm::isa_and_nonnull<ParmVarDecl>( |
4184 | LVal.Base.dyn_cast<const ValueDecl *>())) |
4185 | ++VisibleDepth; |
4186 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
4187 | Info.EvalStatus.HasSideEffects) || |
4188 | (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth)) |
4189 | return CompleteObject(); |
4190 | |
4191 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
4192 | } |
4193 | |
4194 | |
4195 | |
4196 | |
4197 | |
4198 | |
4199 | |
4200 | |
4201 | |
4202 | |
4203 | |
4204 | |
4205 | |
4206 | |
4207 | |
4208 | static bool |
4209 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
4210 | const LValue &LVal, APValue &RVal, |
4211 | bool WantObjectRepresentation = false) { |
4212 | if (LVal.Designator.Invalid) |
| 69 | | Assuming field 'Invalid' is 0 | |
|
| |
4213 | return false; |
4214 | |
4215 | |
4216 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
| 71 | | Calling 'LValueBase::dyn_cast' | |
|
| 81 | | Returning from 'LValueBase::dyn_cast' | |
|
4217 | |
4218 | AccessKinds AK = |
4219 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
| |
4220 | |
4221 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
4222 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
4223 | |
4224 | |
4225 | |
4226 | if (Type.isVolatileQualified()) { |
4227 | Info.FFDiag(Conv); |
4228 | return false; |
4229 | } |
4230 | APValue Lit; |
4231 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
4232 | return false; |
4233 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
4234 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
4235 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
4236 | |
4237 | |
4238 | assert(LVal.Designator.Entries.size() <= 1 && |
4239 | "Can only read characters from string literals"); |
4240 | if (LVal.Designator.Entries.empty()) { |
4241 | |
4242 | |
4243 | |
4244 | Info.FFDiag(Conv); |
4245 | return false; |
4246 | } |
4247 | if (LVal.Designator.isOnePastTheEnd()) { |
4248 | if (Info.getLangOpts().CPlusPlus11) |
4249 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
4250 | else |
4251 | Info.FFDiag(Conv); |
4252 | return false; |
4253 | } |
4254 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
4255 | RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex)); |
4256 | return true; |
4257 | } |
4258 | } |
4259 | |
4260 | CompleteObject Obj = findCompleteObject(Info, Conv, AK, LVal, Type); |
4261 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
| 83 | | Returning value, which participates in a condition later | |
|
4262 | } |
4263 | |
4264 | |
4265 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4266 | QualType LValType, APValue &Val) { |
4267 | if (LVal.Designator.Invalid) |
4268 | return false; |
4269 | |
4270 | if (!Info.getLangOpts().CPlusPlus14) { |
4271 | Info.FFDiag(E); |
4272 | return false; |
4273 | } |
4274 | |
4275 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4276 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
4277 | } |
4278 | |
4279 | namespace { |
4280 | struct CompoundAssignSubobjectHandler { |
4281 | EvalInfo &Info; |
4282 | const CompoundAssignOperator *E; |
4283 | QualType PromotedLHSType; |
4284 | BinaryOperatorKind Opcode; |
4285 | const APValue &RHS; |
4286 | |
4287 | static const AccessKinds AccessKind = AK_Assign; |
4288 | |
4289 | typedef bool result_type; |
4290 | |
4291 | bool checkConst(QualType QT) { |
4292 | |
4293 | if (QT.isConstQualified()) { |
4294 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4295 | return false; |
4296 | } |
4297 | return true; |
4298 | } |
4299 | |
4300 | bool failed() { return false; } |
4301 | bool found(APValue &Subobj, QualType SubobjType) { |
4302 | switch (Subobj.getKind()) { |
4303 | case APValue::Int: |
4304 | return found(Subobj.getInt(), SubobjType); |
4305 | case APValue::Float: |
4306 | return found(Subobj.getFloat(), SubobjType); |
4307 | case APValue::ComplexInt: |
4308 | case APValue::ComplexFloat: |
4309 | |
4310 | Info.FFDiag(E); |
4311 | return false; |
4312 | case APValue::LValue: |
4313 | return foundPointer(Subobj, SubobjType); |
4314 | case APValue::Vector: |
4315 | return foundVector(Subobj, SubobjType); |
4316 | default: |
4317 | |
4318 | Info.FFDiag(E); |
4319 | return false; |
4320 | } |
4321 | } |
4322 | |
4323 | bool foundVector(APValue &Value, QualType SubobjType) { |
4324 | if (!checkConst(SubobjType)) |
4325 | return false; |
4326 | |
4327 | if (!SubobjType->isVectorType()) { |
4328 | Info.FFDiag(E); |
4329 | return false; |
4330 | } |
4331 | return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS); |
4332 | } |
4333 | |
4334 | bool found(APSInt &Value, QualType SubobjType) { |
4335 | if (!checkConst(SubobjType)) |
4336 | return false; |
4337 | |
4338 | if (!SubobjType->isIntegerType()) { |
4339 | |
4340 | |
4341 | Info.FFDiag(E); |
4342 | return false; |
4343 | } |
4344 | |
4345 | if (RHS.isInt()) { |
4346 | APSInt LHS = |
4347 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
4348 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
4349 | return false; |
4350 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
4351 | return true; |
4352 | } else if (RHS.isFloat()) { |
4353 | const FPOptions FPO = E->getFPFeaturesInEffect( |
4354 | Info.Ctx.getLangOpts()); |
4355 | APFloat FValue(0.0); |
4356 | return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value, |
4357 | PromotedLHSType, FValue) && |
4358 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
4359 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
4360 | Value); |
4361 | } |
4362 | |
4363 | Info.FFDiag(E); |
4364 | return false; |
4365 | } |
4366 | bool found(APFloat &Value, QualType SubobjType) { |
4367 | return checkConst(SubobjType) && |
4368 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
4369 | Value) && |
4370 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
4371 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
4372 | } |
4373 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4374 | if (!checkConst(SubobjType)) |
4375 | return false; |
4376 | |
4377 | QualType PointeeType; |
4378 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4379 | PointeeType = PT->getPointeeType(); |
4380 | |
4381 | if (PointeeType.isNull() || !RHS.isInt() || |
4382 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
4383 | Info.FFDiag(E); |
4384 | return false; |
4385 | } |
4386 | |
4387 | APSInt Offset = RHS.getInt(); |
4388 | if (Opcode == BO_Sub) |
4389 | negateAsSigned(Offset); |
4390 | |
4391 | LValue LVal; |
4392 | LVal.setFrom(Info.Ctx, Subobj); |
4393 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
4394 | return false; |
4395 | LVal.moveInto(Subobj); |
4396 | return true; |
4397 | } |
4398 | }; |
4399 | } |
4400 | |
4401 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
4402 | |
4403 | |
4404 | static bool handleCompoundAssignment(EvalInfo &Info, |
4405 | const CompoundAssignOperator *E, |
4406 | const LValue &LVal, QualType LValType, |
4407 | QualType PromotedLValType, |
4408 | BinaryOperatorKind Opcode, |
4409 | const APValue &RVal) { |
4410 | if (LVal.Designator.Invalid) |
4411 | return false; |
4412 | |
4413 | if (!Info.getLangOpts().CPlusPlus14) { |
4414 | Info.FFDiag(E); |
4415 | return false; |
4416 | } |
4417 | |
4418 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4419 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
4420 | RVal }; |
4421 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4422 | } |
4423 | |
4424 | namespace { |
4425 | struct IncDecSubobjectHandler { |
4426 | EvalInfo &Info; |
4427 | const UnaryOperator *E; |
4428 | AccessKinds AccessKind; |
4429 | APValue *Old; |
4430 | |
4431 | typedef bool result_type; |
4432 | |
4433 | bool checkConst(QualType QT) { |
4434 | |
4435 | if (QT.isConstQualified()) { |
4436 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4437 | return false; |
4438 | } |
4439 | return true; |
4440 | } |
4441 | |
4442 | bool failed() { return false; } |
4443 | bool found(APValue &Subobj, QualType SubobjType) { |
4444 | |
4445 | |
4446 | if (Old) { |
4447 | *Old = Subobj; |
4448 | Old = nullptr; |
4449 | } |
4450 | |
4451 | switch (Subobj.getKind()) { |
4452 | case APValue::Int: |
4453 | return found(Subobj.getInt(), SubobjType); |
4454 | case APValue::Float: |
4455 | return found(Subobj.getFloat(), SubobjType); |
4456 | case APValue::ComplexInt: |
4457 | return found(Subobj.getComplexIntReal(), |
4458 | SubobjType->castAs<ComplexType>()->getElementType() |
4459 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
4460 | case APValue::ComplexFloat: |
4461 | return found(Subobj.getComplexFloatReal(), |
4462 | SubobjType->castAs<ComplexType>()->getElementType() |
4463 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
4464 | case APValue::LValue: |
4465 | return foundPointer(Subobj, SubobjType); |
4466 | default: |
4467 | |
4468 | Info.FFDiag(E); |
4469 | return false; |
4470 | } |
4471 | } |
4472 | bool found(APSInt &Value, QualType SubobjType) { |
4473 | if (!checkConst(SubobjType)) |
4474 | return false; |
4475 | |
4476 | if (!SubobjType->isIntegerType()) { |
4477 | |
4478 | |
4479 | Info.FFDiag(E); |
4480 | return false; |
4481 | } |
4482 | |
4483 | if (Old) *Old = APValue(Value); |
4484 | |
4485 | |
4486 | |
4487 | if (SubobjType->isBooleanType()) { |
4488 | if (AccessKind == AK_Increment) |
4489 | Value = 1; |
4490 | else |
4491 | Value = !Value; |
4492 | return true; |
4493 | } |
4494 | |
4495 | bool WasNegative = Value.isNegative(); |
4496 | if (AccessKind == AK_Increment) { |
4497 | ++Value; |
4498 | |
4499 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
4500 | APSInt ActualValue(Value, true); |
4501 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4502 | } |
4503 | } else { |
4504 | --Value; |
4505 | |
4506 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
4507 | unsigned BitWidth = Value.getBitWidth(); |
4508 | APSInt ActualValue(Value.sext(BitWidth + 1), false); |
4509 | ActualValue.setBit(BitWidth); |
4510 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4511 | } |
4512 | } |
4513 | return true; |
4514 | } |
4515 | bool found(APFloat &Value, QualType SubobjType) { |
4516 | if (!checkConst(SubobjType)) |
4517 | return false; |
4518 | |
4519 | if (Old) *Old = APValue(Value); |
4520 | |
4521 | APFloat One(Value.getSemantics(), 1); |
4522 | if (AccessKind == AK_Increment) |
4523 | Value.add(One, APFloat::rmNearestTiesToEven); |
4524 | else |
4525 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
4526 | return true; |
4527 | } |
4528 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4529 | if (!checkConst(SubobjType)) |
4530 | return false; |
4531 | |
4532 | QualType PointeeType; |
4533 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4534 | PointeeType = PT->getPointeeType(); |
4535 | else { |
4536 | Info.FFDiag(E); |
4537 | return false; |
4538 | } |
4539 | |
4540 | LValue LVal; |
4541 | LVal.setFrom(Info.Ctx, Subobj); |
4542 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
4543 | AccessKind == AK_Increment ? 1 : -1)) |
4544 | return false; |
4545 | LVal.moveInto(Subobj); |
4546 | return true; |
4547 | } |
4548 | }; |
4549 | } |
4550 | |
4551 | |
4552 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4553 | QualType LValType, bool IsIncrement, APValue *Old) { |
4554 | if (LVal.Designator.Invalid) |
4555 | return false; |
4556 | |
4557 | if (!Info.getLangOpts().CPlusPlus14) { |
4558 | Info.FFDiag(E); |
4559 | return false; |
4560 | } |
4561 | |
4562 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
4563 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
4564 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
4565 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4566 | } |
4567 | |
4568 | |
4569 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
4570 | LValue &This) { |
4571 | if (Object->getType()->isPointerType() && Object->isPRValue()) |
4572 | return EvaluatePointer(Object, This, Info); |
4573 | |
4574 | if (Object->isGLValue()) |
4575 | return EvaluateLValue(Object, This, Info); |
4576 | |
4577 | if (Object->getType()->isLiteralType(Info.Ctx)) |
4578 | return EvaluateTemporary(Object, This, Info); |
4579 | |
4580 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
4581 | return false; |
4582 | } |
4583 | |
4584 | |
4585 | |
4586 | |
4587 | |
4588 | |
4589 | |
4590 | |
4591 | |
4592 | |
4593 | |
4594 | |
4595 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4596 | QualType LVType, |
4597 | LValue &LV, |
4598 | const Expr *RHS, |
4599 | bool IncludeMember = true) { |
4600 | MemberPtr MemPtr; |
4601 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
4602 | return nullptr; |
4603 | |
4604 | |
4605 | |
4606 | if (!MemPtr.getDecl()) { |
4607 | |
4608 | Info.FFDiag(RHS); |
4609 | return nullptr; |
4610 | } |
4611 | |
4612 | if (MemPtr.isDerivedMember()) { |
4613 | |
4614 | |
4615 | |
4616 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
4617 | LV.Designator.Entries.size()) { |
4618 | Info.FFDiag(RHS); |
4619 | return nullptr; |
4620 | } |
4621 | unsigned PathLengthToMember = |
4622 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
4623 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
4624 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
4625 | LV.Designator.Entries[PathLengthToMember + I]); |
4626 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
4627 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
4628 | Info.FFDiag(RHS); |
4629 | return nullptr; |
4630 | } |
4631 | } |
4632 | |
4633 | |
4634 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
4635 | PathLengthToMember)) |
4636 | return nullptr; |
4637 | } else if (!MemPtr.Path.empty()) { |
4638 | |
4639 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
4640 | MemPtr.Path.size() + IncludeMember); |
4641 | |
4642 | |
4643 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
4644 | LVType = PT->getPointeeType(); |
4645 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
4646 | assert(RD && "member pointer access on non-class-type expression"); |
4647 | |
4648 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
4649 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
4650 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
4651 | return nullptr; |
4652 | RD = Base; |
4653 | } |
4654 | |
4655 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
4656 | MemPtr.getContainingRecord())) |
4657 | return nullptr; |
4658 | } |
4659 | |
4660 | |
4661 | if (IncludeMember) { |
4662 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
4663 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
4664 | return nullptr; |
4665 | } else if (const IndirectFieldDecl *IFD = |
4666 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
4667 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
4668 | return nullptr; |
4669 | } else { |
4670 | llvm_unreachable("can't construct reference to bound member function"); |
4671 | } |
4672 | } |
4673 | |
4674 | return MemPtr.getDecl(); |
4675 | } |
4676 | |
4677 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4678 | const BinaryOperator *BO, |
4679 | LValue &LV, |
4680 | bool IncludeMember = true) { |
4681 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
4682 | |
4683 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
4684 | if (Info.noteFailure()) { |
4685 | MemberPtr MemPtr; |
4686 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
4687 | } |
4688 | return nullptr; |
4689 | } |
4690 | |
4691 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
4692 | BO->getRHS(), IncludeMember); |
4693 | } |
4694 | |
4695 | |
4696 | |
4697 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
4698 | LValue &Result) { |
4699 | SubobjectDesignator &D = Result.Designator; |
4700 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
4701 | return false; |
4702 | |
4703 | QualType TargetQT = E->getType(); |
4704 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
4705 | TargetQT = PT->getPointeeType(); |
4706 | |
4707 | |
4708 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
4709 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4710 | << D.MostDerivedType << TargetQT; |
4711 | return false; |
4712 | } |
4713 | |
4714 | |
4715 | |
4716 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
4717 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
4718 | const CXXRecordDecl *FinalType; |
4719 | if (NewEntriesSize == D.MostDerivedPathLength) |
4720 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
4721 | else |
4722 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
4723 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
4724 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4725 | << D.MostDerivedType << TargetQT; |
4726 | return false; |
4727 | } |
4728 | |
4729 | |
4730 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
4731 | } |
4732 | |
4733 | |
4734 | |
4735 | static bool getDefaultInitValue(QualType T, APValue &Result) { |
4736 | bool Success = true; |
4737 | if (auto *RD = T->getAsCXXRecordDecl()) { |
4738 | if (RD->isInvalidDecl()) { |
4739 | Result = APValue(); |
4740 | return false; |
4741 | } |
4742 | if (RD->isUnion()) { |
4743 | Result = APValue((const FieldDecl *)nullptr); |
4744 | return true; |
4745 | } |
4746 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
4747 | std::distance(RD->field_begin(), RD->field_end())); |
4748 | |
4749 | unsigned Index = 0; |
4750 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
4751 | End = RD->bases_end(); |
4752 | I != End; ++I, ++Index) |
4753 | Success &= getDefaultInitValue(I->getType(), Result.getStructBase(Index)); |
4754 | |
4755 | for (const auto *I : RD->fields()) { |
4756 | if (I->isUnnamedBitfield()) |
4757 | continue; |
4758 | Success &= getDefaultInitValue(I->getType(), |
4759 | Result.getStructField(I->getFieldIndex())); |
4760 | } |
4761 | return Success; |
4762 | } |
4763 | |
4764 | if (auto *AT = |
4765 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
4766 | Result = APValue(APValue::UninitArray(), 0, AT->getSize().getZExtValue()); |
4767 | if (Result.hasArrayFiller()) |
4768 | Success &= |
4769 | getDefaultInitValue(AT->getElementType(), Result.getArrayFiller()); |
4770 | |
4771 | return Success; |
4772 | } |
4773 | |
4774 | Result = APValue::IndeterminateValue(); |
4775 | return true; |
4776 | } |
4777 | |
4778 | namespace { |
4779 | enum EvalStmtResult { |
4780 | |
4781 | ESR_Failed, |
4782 | |
4783 | ESR_Returned, |
4784 | |
4785 | ESR_Succeeded, |
4786 | |
4787 | ESR_Continue, |
4788 | |
4789 | ESR_Break, |
4790 | |
4791 | ESR_CaseNotFound |
4792 | }; |
4793 | } |
4794 | |
4795 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
4796 | |
4797 | if (!VD->hasLocalStorage()) |
4798 | return true; |
4799 | |
4800 | LValue Result; |
4801 | APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(), |
4802 | ScopeKind::Block, Result); |
4803 | |
4804 | const Expr *InitE = VD->getInit(); |
4805 | if (!InitE) { |
4806 | if (VD->getType()->isDependentType()) |
4807 | return Info.noteSideEffect(); |
4808 | return getDefaultInitValue(VD->getType(), Val); |
4809 | } |
4810 | if (InitE->isValueDependent()) |
4811 | return false; |
4812 | |
4813 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
4814 | |
4815 | |
4816 | Val = APValue(); |
4817 | return false; |
4818 | } |
4819 | |
4820 | return true; |
4821 | } |
4822 | |
4823 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
4824 | bool OK = true; |
4825 | |
4826 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
4827 | OK &= EvaluateVarDecl(Info, VD); |
4828 | |
4829 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
4830 | for (auto *BD : DD->bindings()) |
4831 | if (auto *VD = BD->getHoldingVar()) |
4832 | OK &= EvaluateDecl(Info, VD); |
4833 | |
4834 | return OK; |
4835 | } |
4836 | |
4837 | static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) { |
4838 | assert(E->isValueDependent()); |
4839 | if (Info.noteSideEffect()) |
4840 | return true; |
4841 | assert(E->containsErrors() && "valid value-dependent expression should never " |
4842 | "reach invalid code path."); |
4843 | return false; |
4844 | } |
4845 | |
4846 | |
4847 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
4848 | const Expr *Cond, bool &Result) { |
4849 | if (Cond->isValueDependent()) |
4850 | return false; |
4851 | FullExpressionRAII Scope(Info); |
4852 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
4853 | return false; |
4854 | if (!EvaluateAsBooleanCondition(Cond, Result, Info)) |
4855 | return false; |
4856 | return Scope.destroy(); |
4857 | } |
4858 | |
4859 | namespace { |
4860 | |
4861 | |
4862 | struct StmtResult { |
4863 | |
4864 | APValue &Value; |
4865 | |
4866 | const LValue *Slot; |
4867 | }; |
4868 | |
4869 | struct TempVersionRAII { |
4870 | CallStackFrame &Frame; |
4871 | |
4872 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
4873 | Frame.pushTempVersion(); |
4874 | } |
4875 | |
4876 | ~TempVersionRAII() { |
4877 | Frame.popTempVersion(); |
4878 | } |
4879 | }; |
4880 | |
4881 | } |
4882 | |
4883 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
4884 | const Stmt *S, |
4885 | const SwitchCase *SC = nullptr); |
4886 | |
4887 | |
4888 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
4889 | const Stmt *Body, |
4890 | const SwitchCase *Case = nullptr) { |
4891 | BlockScopeRAII Scope(Info); |
4892 | |
4893 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case); |
4894 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
4895 | ESR = ESR_Failed; |
4896 | |
4897 | switch (ESR) { |
4898 | case ESR_Break: |
4899 | return ESR_Succeeded; |
4900 | case ESR_Succeeded: |
4901 | case ESR_Continue: |
4902 | return ESR_Continue; |
4903 | case ESR_Failed: |
4904 | case ESR_Returned: |
4905 | case ESR_CaseNotFound: |
4906 | return ESR; |
4907 | } |
4908 | llvm_unreachable("Invalid EvalStmtResult!"); |
4909 | } |
4910 | |
4911 | |
4912 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
4913 | const SwitchStmt *SS) { |
4914 | BlockScopeRAII Scope(Info); |
4915 | |
4916 | |
4917 | APSInt Value; |
4918 | { |
4919 | if (const Stmt *Init = SS->getInit()) { |
4920 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
4921 | if (ESR != ESR_Succeeded) { |
4922 | if (ESR != ESR_Failed && !Scope.destroy()) |
4923 | ESR = ESR_Failed; |
4924 | return ESR; |
4925 | } |
4926 | } |
4927 | |
4928 | FullExpressionRAII CondScope(Info); |
4929 | if (SS->getConditionVariable() && |
4930 | !EvaluateDecl(Info, SS->getConditionVariable())) |
4931 | return ESR_Failed; |
4932 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
4933 | return ESR_Failed; |
4934 | if (!CondScope.destroy()) |
4935 | return ESR_Failed; |
4936 | } |
4937 | |
4938 | |
4939 | |
4940 | const SwitchCase *Found = nullptr; |
4941 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
4942 | SC = SC->getNextSwitchCase()) { |
4943 | if (isa<DefaultStmt>(SC)) { |
4944 | Found = SC; |
4945 | continue; |
4946 | } |
4947 | |
4948 | const CaseStmt *CS = cast<CaseStmt>(SC); |
4949 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
4950 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
4951 | : LHS; |
4952 | if (LHS <= Value && Value <= RHS) { |
4953 | Found = SC; |
4954 | break; |
4955 | } |
4956 | } |
4957 | |
4958 | if (!Found) |
4959 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
4960 | |
4961 | |
4962 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found); |
4963 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
4964 | return ESR_Failed; |
4965 | |
4966 | switch (ESR) { |
4967 | case ESR_Break: |
4968 | return ESR_Succeeded; |
4969 | case ESR_Succeeded: |
4970 | case ESR_Continue: |
4971 | case ESR_Failed: |
4972 | case ESR_Returned: |
4973 | return ESR; |
4974 | case ESR_CaseNotFound: |
4975 | |
4976 | |
4977 | Info.FFDiag(Found->getBeginLoc(), |
4978 | diag::note_constexpr_stmt_expr_unsupported); |
4979 | return ESR_Failed; |
4980 | } |
4981 | llvm_unreachable("Invalid EvalStmtResult!"); |
4982 | } |
4983 | |
4984 | |
4985 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
4986 | const Stmt *S, const SwitchCase *Case) { |
4987 | if (!Info.nextStep(S)) |
4988 | return ESR_Failed; |
4989 | |
4990 | |
4991 | |
4992 | if (Case) { |
4993 | switch (S->getStmtClass()) { |
4994 | case Stmt::CompoundStmtClass: |
4995 | |
4996 | |
4997 | |
4998 | case Stmt::LabelStmtClass: |
4999 | case Stmt::AttributedStmtClass: |
5000 | case Stmt::DoStmtClass: |
5001 | break; |
5002 | |
5003 | case Stmt::CaseStmtClass: |
5004 | case Stmt::DefaultStmtClass: |
5005 | if (Case == S) |
5006 | Case = nullptr; |
5007 | break; |
5008 | |
5009 | case Stmt::IfStmtClass: { |
5010 | |
5011 | |
5012 | const IfStmt *IS = cast<IfStmt>(S); |
5013 | |
5014 | |
5015 | |
5016 | BlockScopeRAII Scope(Info); |
5017 | |
5018 | |
5019 | |
5020 | if (const Stmt *Init = IS->getInit()) { |
5021 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
5022 | if (ESR != ESR_CaseNotFound) { |
5023 | assert(ESR != ESR_Succeeded); |
5024 | return ESR; |
5025 | } |
5026 | } |
5027 | |
5028 | |
5029 | |
5030 | |
5031 | |
5032 | |
5033 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
5034 | if (ESR == ESR_Failed) |
5035 | return ESR; |
5036 | if (ESR != ESR_CaseNotFound) |
5037 | return Scope.destroy() ? ESR : ESR_Failed; |
5038 | if (!IS->getElse()) |
5039 | return ESR_CaseNotFound; |
5040 | |
5041 | ESR = EvaluateStmt(Result, Info, IS->getElse(), Case); |
5042 | if (ESR == ESR_Failed) |
5043 | return ESR; |
5044 | if (ESR != ESR_CaseNotFound) |
5045 | return Scope.destroy() ? ESR : ESR_Failed; |
5046 | return ESR_CaseNotFound; |
5047 | } |
5048 | |
5049 | case Stmt::WhileStmtClass: { |
5050 | EvalStmtResult ESR = |
5051 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
5052 | if (ESR != ESR_Continue) |
5053 | return ESR; |
5054 | break; |
5055 | } |
5056 | |
5057 | case Stmt::ForStmtClass: { |
5058 | const ForStmt *FS = cast<ForStmt>(S); |
5059 | BlockScopeRAII Scope(Info); |
5060 | |
5061 | |
5062 | |
5063 | if (const Stmt *Init = FS->getInit()) { |
5064 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
5065 | if (ESR != ESR_CaseNotFound) { |
5066 | assert(ESR != ESR_Succeeded); |
5067 | return ESR; |
5068 | } |
5069 | } |
5070 | |
5071 | EvalStmtResult ESR = |
5072 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
5073 | if (ESR != ESR_Continue) |
5074 | return ESR; |
5075 | if (const auto *Inc = FS->getInc()) { |
5076 | if (Inc->isValueDependent()) { |
5077 | if (!EvaluateDependentExpr(Inc, Info)) |
5078 | return ESR_Failed; |
5079 | } else { |
5080 | FullExpressionRAII IncScope(Info); |
5081 | if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy()) |
5082 | return ESR_Failed; |
5083 | } |
5084 | } |
5085 | break; |
5086 | } |
5087 | |
5088 | case Stmt::DeclStmtClass: { |
5089 | |
5090 | |
5091 | const DeclStmt *DS = cast<DeclStmt>(S); |
5092 | for (const auto *D : DS->decls()) { |
5093 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
5094 | if (VD->hasLocalStorage() && !VD->getInit()) |
5095 | if (!EvaluateVarDecl(Info, VD)) |
5096 | return ESR_Failed; |
5097 | |
5098 | |
5099 | |
5100 | } |
5101 | } |
5102 | return ESR_CaseNotFound; |
5103 | } |
5104 | |
5105 | default: |
5106 | return ESR_CaseNotFound; |
5107 | } |
5108 | } |
5109 | |
5110 | switch (S->getStmtClass()) { |
5111 | default: |
5112 | if (const Expr *E = dyn_cast<Expr>(S)) { |
5113 | if (E->isValueDependent()) { |
5114 | if (!EvaluateDependentExpr(E, Info)) |
5115 | return ESR_Failed; |
5116 | } else { |
5117 | |
5118 | |
5119 | |
5120 | |
5121 | FullExpressionRAII Scope(Info); |
5122 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
5123 | return ESR_Failed; |
5124 | } |
5125 | return ESR_Succeeded; |
5126 | } |
5127 | |
5128 | Info.FFDiag(S->getBeginLoc()); |
5129 | return ESR_Failed; |
5130 | |
5131 | case Stmt::NullStmtClass: |
5132 | return ESR_Succeeded; |
5133 | |
5134 | case Stmt::DeclStmtClass: { |
5135 | const DeclStmt *DS = cast<DeclStmt>(S); |
5136 | for (const auto *D : DS->decls()) { |
5137 | |
5138 | FullExpressionRAII Scope(Info); |
5139 | if (!EvaluateDecl(Info, D) && !Info.noteFailure()) |
5140 | return ESR_Failed; |
5141 | if (!Scope.destroy()) |
5142 | return ESR_Failed; |
5143 | } |
5144 | return ESR_Succeeded; |
5145 | } |
5146 | |
5147 | case Stmt::ReturnStmtClass: { |
5148 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
5149 | FullExpressionRAII Scope(Info); |
5150 | if (RetExpr && RetExpr->isValueDependent()) { |
5151 | EvaluateDependentExpr(RetExpr, Info); |
5152 | |
5153 | return ESR_Failed; |
5154 | } |
5155 | if (RetExpr && |
5156 | !(Result.Slot |
5157 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
5158 | : Evaluate(Result.Value, Info, RetExpr))) |
5159 | return ESR_Failed; |
5160 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
5161 | } |
5162 | |
5163 | case Stmt::CompoundStmtClass: { |
5164 | BlockScopeRAII Scope(Info); |
5165 | |
5166 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
5167 | for (const auto *BI : CS->body()) { |
5168 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
5169 | if (ESR == ESR_Succeeded) |
5170 | Case = nullptr; |
5171 | else if (ESR != ESR_CaseNotFound) { |
5172 | if (ESR != ESR_Failed && !Scope.destroy()) |
5173 | return ESR_Failed; |
5174 | return ESR; |
5175 | } |
5176 | } |
5177 | if (Case) |
5178 | return ESR_CaseNotFound; |
5179 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5180 | } |
5181 | |
5182 | case Stmt::IfStmtClass: { |
5183 | const IfStmt *IS = cast<IfStmt>(S); |
5184 | |
5185 | |
5186 | BlockScopeRAII Scope(Info); |
5187 | if (const Stmt *Init = IS->getInit()) { |
5188 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
5189 | if (ESR != ESR_Succeeded) { |
5190 | if (ESR != ESR_Failed && !Scope.destroy()) |
5191 | return ESR_Failed; |
5192 | return ESR; |
5193 | } |
5194 | } |
5195 | bool Cond; |
5196 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
5197 | return ESR_Failed; |
5198 | |
5199 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
5200 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
5201 | if (ESR != ESR_Succeeded) { |
5202 | if (ESR != ESR_Failed && !Scope.destroy()) |
5203 | return ESR_Failed; |
5204 | return ESR; |
5205 | } |
5206 | } |
5207 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5208 | } |
5209 | |
5210 | case Stmt::WhileStmtClass: { |
5211 | const WhileStmt *WS = cast<WhileStmt>(S); |
5212 | while (true) { |
5213 | BlockScopeRAII Scope(Info); |
5214 | bool Continue; |
5215 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
5216 | Continue)) |
5217 | return ESR_Failed; |
5218 | if (!Continue) |
5219 | break; |
5220 | |
5221 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
5222 | if (ESR != ESR_Continue) { |
5223 | if (ESR != ESR_Failed && !Scope.destroy()) |
5224 | return ESR_Failed; |
5225 | return ESR; |
5226 | } |
5227 | if (!Scope.destroy()) |
5228 | return ESR_Failed; |
5229 | } |
5230 | return ESR_Succeeded; |
5231 | } |
5232 | |
5233 | case Stmt::DoStmtClass: { |
5234 | const DoStmt *DS = cast<DoStmt>(S); |
5235 | bool Continue; |
5236 | do { |
5237 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
5238 | if (ESR != ESR_Continue) |
5239 | return ESR; |
5240 | Case = nullptr; |
5241 | |
5242 | if (DS->getCond()->isValueDependent()) { |
5243 | EvaluateDependentExpr(DS->getCond(), Info); |
5244 | |
5245 | return ESR_Failed; |
5246 | } |
5247 | FullExpressionRAII CondScope(Info); |
5248 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info) || |
5249 | !CondScope.destroy()) |
5250 | return ESR_Failed; |
5251 | } while (Continue); |
5252 | return ESR_Succeeded; |
5253 | } |
5254 | |
5255 | case Stmt::ForStmtClass: { |
5256 | const ForStmt *FS = cast<ForStmt>(S); |
5257 | BlockScopeRAII ForScope(Info); |
5258 | if (FS->getInit()) { |
5259 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
5260 | if (ESR != ESR_Succeeded) { |
5261 | if (ESR != ESR_Failed && !ForScope.destroy()) |
5262 | return ESR_Failed; |
5263 | return ESR; |
5264 | } |
5265 | } |
5266 | while (true) { |
5267 | BlockScopeRAII IterScope(Info); |
5268 | bool Continue = true; |
5269 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
5270 | FS->getCond(), Continue)) |
5271 | return ESR_Failed; |
5272 | if (!Continue) |
5273 | break; |
5274 | |
5275 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
5276 | if (ESR != ESR_Continue) { |
5277 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
5278 | return ESR_Failed; |
5279 | return ESR; |
5280 | } |
5281 | |
5282 | if (const auto *Inc = FS->getInc()) { |
5283 | if (Inc->isValueDependent()) { |
5284 | if (!EvaluateDependentExpr(Inc, Info)) |
5285 | return ESR_Failed; |
5286 | } else { |
5287 | FullExpressionRAII IncScope(Info); |
5288 | if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy()) |
5289 | return ESR_Failed; |
5290 | } |
5291 | } |
5292 | |
5293 | if (!IterScope.destroy()) |
5294 | return ESR_Failed; |
5295 | } |
5296 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
5297 | } |
5298 | |
5299 | case Stmt::CXXForRangeStmtClass: { |
5300 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
5301 | BlockScopeRAII Scope(Info); |
5302 | |
5303 | |
5304 | if (FS->getInit()) { |
5305 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
5306 | if (ESR != ESR_Succeeded) { |
5307 | if (ESR != ESR_Failed && !Scope.destroy()) |
5308 | return ESR_Failed; |
5309 | return ESR; |
5310 | } |
5311 | } |
5312 | |
5313 | |
5314 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
5315 | if (ESR != ESR_Succeeded) { |
5316 | if (ESR != ESR_Failed && !Scope.destroy()) |
5317 | return ESR_Failed; |
5318 | return ESR; |
5319 | } |
5320 | |
5321 | |
5322 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
5323 | if (ESR != ESR_Succeeded) { |
5324 | if (ESR != ESR_Failed && !Scope.destroy()) |
5325 | return ESR_Failed; |
5326 | return ESR; |
5327 | } |
5328 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
5329 | if (ESR != ESR_Succeeded) { |
5330 | if (ESR != ESR_Failed && !Scope.destroy()) |
5331 | return ESR_Failed; |
5332 | return ESR; |
5333 | } |
5334 | |
5335 | while (true) { |
5336 | |
5337 | { |
5338 | if (FS->getCond()->isValueDependent()) { |
5339 | EvaluateDependentExpr(FS->getCond(), Info); |
5340 | |
5341 | return ESR_Failed; |
5342 | } |
5343 | bool Continue = true; |
5344 | FullExpressionRAII CondExpr(Info); |
5345 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
5346 | return ESR_Failed; |
5347 | if (!Continue) |
5348 | break; |
5349 | } |
5350 | |
5351 | |
5352 | BlockScopeRAII InnerScope(Info); |
5353 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
5354 | if (ESR != ESR_Succeeded) { |
5355 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5356 | return ESR_Failed; |
5357 | return ESR; |
5358 | } |
5359 | |
5360 | |
5361 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
5362 | if (ESR != ESR_Continue) { |
5363 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5364 | return ESR_Failed; |
5365 | return ESR; |
5366 | } |
5367 | if (FS->getInc()->isValueDependent()) { |
5368 | if (!EvaluateDependentExpr(FS->getInc(), Info)) |
5369 | return ESR_Failed; |
5370 | } else { |
5371 | |
5372 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
5373 | return ESR_Failed; |
5374 | } |
5375 | |
5376 | if (!InnerScope.destroy()) |
5377 | return ESR_Failed; |
5378 | } |
5379 | |
5380 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5381 | } |
5382 | |
5383 | case Stmt::SwitchStmtClass: |
5384 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
5385 | |
5386 | case Stmt::ContinueStmtClass: |
5387 | return ESR_Continue; |
5388 | |
5389 | case Stmt::BreakStmtClass: |
5390 | return ESR_Break; |
5391 | |
5392 | case Stmt::LabelStmtClass: |
5393 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
5394 | |
5395 | case Stmt::AttributedStmtClass: |
5396 | |
5397 | |
5398 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
5399 | Case); |
5400 | |
5401 | case Stmt::CaseStmtClass: |
5402 | case Stmt::DefaultStmtClass: |
5403 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
5404 | case Stmt::CXXTryStmtClass: |
5405 | |
5406 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case); |
5407 | } |
5408 | } |
5409 | |
5410 | |
5411 | |
5412 | |
5413 | |
5414 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
5415 | const CXXConstructorDecl *CD, |
5416 | bool IsValueInitialization) { |
5417 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
5418 | return false; |
5419 | |
5420 | |
5421 | |
5422 | |
5423 | if (!CD->isConstexpr() && !IsValueInitialization) { |
5424 | if (Info.getLangOpts().CPlusPlus11) { |
5425 | |
5426 | |
5427 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
5428 | << 0 << 1 << CD; |
5429 | Info.Note(CD->getLocation(), diag::note_declared_at); |
5430 | } else { |
5431 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
5432 | } |
5433 | } |
5434 | return true; |
5435 | } |
5436 | |
5437 | |
5438 | |
5439 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
5440 | const FunctionDecl *Declaration, |
5441 | const FunctionDecl *Definition, |
5442 | const Stmt *Body) { |
5443 | |
5444 | |
5445 | if (Info.checkingPotentialConstantExpression() && !Definition && |
5446 | Declaration->isConstexpr()) |
5447 | return false; |
5448 | |
5449 | |
5450 | |
5451 | |
5452 | if (Declaration->isInvalidDecl()) { |
5453 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5454 | return false; |
5455 | } |
5456 | |
5457 | |
5458 | |
5459 | |
5460 | if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) && |
5461 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
5462 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
5463 | |
5464 | if (Definition && Definition->isInvalidDecl()) { |
5465 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5466 | return false; |
5467 | } |
5468 | |
5469 | |
5470 | if (Definition && Definition->isConstexpr() && Body) |
5471 | return true; |
5472 | |
5473 | if (Info.getLangOpts().CPlusPlus11) { |
5474 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
5475 | |
5476 | |
5477 | |
5478 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
5479 | if (CD && CD->isInheritingConstructor()) { |
5480 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
5481 | if (!Inherited->isConstexpr()) |
5482 | DiagDecl = CD = Inherited; |
5483 | } |
5484 | |
5485 | |
5486 | |
5487 | |
5488 | if (CD && CD->isInheritingConstructor()) |
5489 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
5490 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
5491 | else |
5492 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
5493 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
5494 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
5495 | } else { |
5496 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5497 | } |
5498 | return false; |
5499 | } |
5500 | |
5501 | namespace { |
5502 | struct CheckDynamicTypeHandler { |
5503 | AccessKinds AccessKind; |
5504 | typedef bool result_type; |
5505 | bool failed() { return false; } |
5506 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
5507 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
5508 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
5509 | }; |
5510 | } |
5511 | |
5512 | |
5513 | |
5514 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
5515 | AccessKinds AK, bool Polymorphic) { |
5516 | if (This.Designator.Invalid) |
5517 | return false; |
5518 | |
5519 | CompleteObject Obj = findCompleteObject(Info, E, AK, This, QualType()); |
5520 | |
5521 | if (!Obj) |
5522 | return false; |
5523 | |
5524 | if (!Obj.Value) { |
5525 | |
5526 | |
5527 | |
5528 | if (This.Designator.isOnePastTheEnd() || |
5529 | This.Designator.isMostDerivedAnUnsizedArray()) { |
5530 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
5531 | ? diag::note_constexpr_access_past_end |
5532 | : diag::note_constexpr_access_unsized_array) |
5533 | << AK; |
5534 | return false; |
5535 | } else if (Polymorphic) { |
5536 | |
5537 | |
5538 | APValue Val; |
5539 | This.moveInto(Val); |
5540 | QualType StarThisType = |
5541 | Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx)); |
5542 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
5543 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
5544 | return false; |
5545 | } |
5546 | return true; |
5547 | } |
5548 | |
5549 | CheckDynamicTypeHandler Handler{AK}; |
5550 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
5551 | } |
5552 | |
5553 | |
5554 | |
5555 | static bool |
5556 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
5557 | const LValue &This, |
5558 | const CXXMethodDecl *NamedMember) { |
5559 | return checkDynamicType( |
5560 | Info, E, This, |
5561 | isa<CXXDestructorDecl>(NamedMember) ? AK_Destroy : AK_MemberCall, false); |
5562 | } |
5563 | |
5564 | struct DynamicType { |
5565 | |
5566 | const CXXRecordDecl *Type; |
5567 | |
5568 | unsigned PathLength; |
5569 | }; |
5570 | |
5571 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
5572 | unsigned PathLength) { |
5573 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
5574 | Designator.Entries.size() && "invalid path length"); |
5575 | return (PathLength == Designator.MostDerivedPathLength) |
5576 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
5577 | : getAsBaseClass(Designator.Entries[PathLength - 1]); |
5578 | } |
5579 | |
5580 | |
5581 | static Optional<DynamicType> ComputeDynamicType(EvalInfo &Info, const Expr *E, |
5582 | LValue &This, AccessKinds AK) { |
5583 | |
5584 | |
5585 | |
5586 | if (!checkDynamicType(Info, E, This, AK, true)) |
5587 | return None; |
5588 | |
5589 | |
5590 | |
5591 | |
5592 | |
5593 | |
5594 | |
5595 | const CXXRecordDecl *Class = |
5596 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
5597 | if (!Class || Class->getNumVBases()) { |
5598 | Info.FFDiag(E); |
5599 | return None; |
5600 | } |
5601 | |
5602 | |
5603 | |
5604 | |
5605 | |
5606 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
5607 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
5608 | PathLength <= Path.size(); ++PathLength) { |
5609 | switch (Info.isEvaluatingCtorDtor(This.getLValueBase(), |
5610 | Path.slice(0, PathLength))) { |
5611 | case ConstructionPhase::Bases: |
5612 | case ConstructionPhase::DestroyingBases: |
5613 | |
5614 | |
5615 | break; |
5616 | |
5617 | case ConstructionPhase::None: |
5618 | case ConstructionPhase::AfterBases: |
5619 | case ConstructionPhase::AfterFields: |
5620 | case ConstructionPhase::Destroying: |
5621 | |
5622 | |
5623 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
5624 | PathLength}; |
5625 | } |
5626 | } |
5627 | |
5628 | |
5629 | |
5630 | |
5631 | Info.FFDiag(E); |
5632 | return None; |
5633 | } |
5634 | |
5635 | |
5636 | static const CXXMethodDecl *HandleVirtualDispatch( |
5637 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
5638 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
5639 | Optional<DynamicType> DynType = ComputeDynamicType( |
5640 | Info, E, This, |
5641 | isa<CXXDestructorDecl>(Found) ? AK_Destroy : AK_MemberCall); |
5642 | if (!DynType) |
5643 | return nullptr; |
5644 | |
5645 | |
5646 | |
5647 | |
5648 | |
5649 | const CXXMethodDecl *Callee = Found; |
5650 | unsigned PathLength = DynType->PathLength; |
5651 | for (; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
5652 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
5653 | const CXXMethodDecl *Overrider = |
5654 | Found->getCorrespondingMethodDeclaredInClass(Class, false); |
5655 | if (Overrider) { |
5656 | Callee = Overrider; |
5657 | break; |
5658 | } |
5659 | } |
5660 | |
5661 | |
5662 | |
5663 | |
5664 | if (Callee->isPure()) { |
5665 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
5666 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
5667 | return nullptr; |
5668 | } |
5669 | |
5670 | |
5671 | |
5672 | if (!Info.Ctx.hasSameUnqualifiedType(Callee->getReturnType(), |
5673 | Found->getReturnType())) { |
5674 | CovariantAdjustmentPath.push_back(Callee->getReturnType()); |
5675 | for (unsigned CovariantPathLength = PathLength + 1; |
5676 | CovariantPathLength != This.Designator.Entries.size(); |
5677 | ++CovariantPathLength) { |
5678 | const CXXRecordDecl *NextClass = |
5679 | getBaseClassType(This.Designator, CovariantPathLength); |
5680 | const CXXMethodDecl *Next = |
5681 | Found->getCorrespondingMethodDeclaredInClass(NextClass, false); |
5682 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
5683 | Next->getReturnType(), CovariantAdjustmentPath.back())) |
5684 | CovariantAdjustmentPath.push_back(Next->getReturnType()); |
5685 | } |
5686 | if (!Info.Ctx.hasSameUnqualifiedType(Found->getReturnType(), |
5687 | CovariantAdjustmentPath.back())) |
5688 | CovariantAdjustmentPath.push_back(Found->getReturnType()); |
5689 | } |
5690 | |
5691 | |
5692 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
5693 | return nullptr; |
5694 | |
5695 | return Callee; |
5696 | } |
5697 | |
5698 | |
5699 | |
5700 | |
5701 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
5702 | APValue &Result, |
5703 | ArrayRef<QualType> Path) { |
5704 | assert(Result.isLValue() && |
5705 | "unexpected kind of APValue for covariant return"); |
5706 | if (Result.isNullPointer()) |
5707 | return true; |
5708 | |
5709 | LValue LVal; |
5710 | LVal.setFrom(Info.Ctx, Result); |
5711 | |
5712 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
5713 | for (unsigned I = 1; I != Path.size(); ++I) { |
5714 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
5715 | assert(OldClass && NewClass && "unexpected kind of covariant return"); |
5716 | if (OldClass != NewClass && |
5717 | !CastToBaseClass(Info, E, LVal, OldClass, NewClass)) |
5718 | return false; |
5719 | OldClass = NewClass; |
5720 | } |
5721 | |
5722 | LVal.moveInto(Result); |
5723 | return true; |
5724 | } |
5725 | |
5726 | |
5727 | |
5728 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
5729 | const CXXRecordDecl *Base) { |
5730 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
5731 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
5732 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
5733 | return BaseSpec.getAccessSpecifier() == AS_public; |
5734 | } |
5735 | llvm_unreachable("Base is not a direct base of Derived"); |
5736 | } |
5737 | |
5738 | |
5739 | |
5740 | |
5741 | |
5742 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
5743 | LValue &Ptr) { |
5744 | |
5745 | SubobjectDesignator &D = Ptr.Designator; |
5746 | if (D.Invalid) |
5747 | return false; |
5748 | |
5749 | |
5750 | |
5751 | if (Ptr.isNullPointer() && !E->isGLValue()) |
5752 | return true; |
5753 | |
5754 | |
5755 | |
5756 | |
5757 | Optional<DynamicType> DynType = |
5758 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
5759 | if (!DynType) |
5760 | return false; |
5761 | |
5762 | |
5763 | |
5764 | |
5765 | if (E->getType()->isVoidPointerType()) |
5766 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
5767 | |
5768 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
5769 | assert(C && "dynamic_cast target is not void pointer nor class"); |
5770 | CanQualType CQT = Info.Ctx.getCanonicalType(Info.Ctx.getRecordType(C)); |
5771 | |
5772 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
5773 | |
5774 | if (!E->isGLValue()) { |
5775 | |
5776 | |
5777 | Ptr.setNull(Info.Ctx, E->getType()); |
5778 | return true; |
5779 | } |
5780 | |
5781 | |
5782 | unsigned DiagKind; |
5783 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
5784 | DynType->Type->isDerivedFrom(C))) |
5785 | DiagKind = 0; |
5786 | else if (!Paths || Paths->begin() == Paths->end()) |
5787 | DiagKind = 1; |
5788 | else if (Paths->isAmbiguous(CQT)) |
5789 | DiagKind = 2; |
5790 | else { |
5791 | assert(Paths->front().Access != AS_public && "why did the cast fail?"); |
5792 | DiagKind = 3; |
5793 | } |
5794 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
5795 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
5796 | << Info.Ctx.getRecordType(DynType->Type) |
5797 | << E->getType().getUnqualifiedType(); |
5798 | return false; |
5799 | }; |
5800 | |
5801 | |
5802 | |
5803 | |
5804 | for (int PathLength = Ptr.Designator.Entries.size(); |
5805 | PathLength >= (int)DynType->PathLength; --PathLength) { |
5806 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
5807 | if (declaresSameEntity(Class, C)) |
5808 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
5809 | |
5810 | if (PathLength > (int)DynType->PathLength && |
5811 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
5812 | Class)) |
5813 | return RuntimeCheckFailed(nullptr); |
5814 | } |
5815 | |
5816 | |
5817 | |
5818 | CXXBasePaths Paths(true, |
5819 | true, false); |
5820 | if (DynType->Type->isDerivedFrom(C, Paths) && !Paths.isAmbiguous(CQT) && |
5821 | Paths.front().Access == AS_public) { |
5822 | |
5823 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
5824 | return false; |
5825 | |
5826 | for (CXXBasePathElement &Elem : Paths.front()) |
5827 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
5828 | return false; |
5829 | return true; |
5830 | } |
5831 | |
5832 | |
5833 | return RuntimeCheckFailed(&Paths); |
5834 | } |
5835 | |
5836 | namespace { |
5837 | struct StartLifetimeOfUnionMemberHandler { |
5838 | EvalInfo &Info; |
5839 | const Expr *LHSExpr; |
5840 | const FieldDecl *Field; |
5841 | bool DuringInit; |
5842 | bool Failed = false; |
5843 | static const AccessKinds AccessKind = AK_Assign; |
5844 | |
5845 | typedef bool result_type; |
5846 | bool failed() { return Failed; } |
5847 | bool found(APValue &Subobj, QualType SubobjType) { |
5848 | |
5849 | |
5850 | |
5851 | |
5852 | |
5853 | |
5854 | |
5855 | |
5856 | assert(SubobjType->isUnionType()); |
5857 | if (declaresSameEntity(Subobj.getUnionField(), Field)) { |
5858 | |
5859 | |
5860 | if (Subobj.getUnionValue().hasValue()) |
5861 | return true; |
5862 | } else if (DuringInit) { |
5863 | |
5864 | |
5865 | |
5866 | Info.FFDiag(LHSExpr, |
5867 | diag::note_constexpr_union_member_change_during_init); |
5868 | return false; |
5869 | } |
5870 | APValue Result; |
5871 | Failed = !getDefaultInitValue(Field->getType(), Result); |
5872 | Subobj.setUnion(Field, Result); |
5873 | return true; |
5874 | } |
5875 | bool found(APSInt &Value, QualType SubobjType) { |
5876 | llvm_unreachable("wrong value kind for union object"); |
5877 | } |
5878 | bool found(APFloat &Value, QualType SubobjType) { |
5879 | llvm_unreachable("wrong value kind for union object"); |
5880 | } |
5881 | }; |
5882 | } |
5883 | |
5884 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
5885 | |
5886 | |
5887 | |
5888 | |
5889 | |
5890 | static bool HandleUnionActiveMemberChange(EvalInfo &Info, const Expr *LHSExpr, |
5891 | const LValue &LHS) { |
5892 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
| 92 | | Assuming field 'InvalidBase' is false | |
|
| 93 | | Assuming field 'Invalid' is 0 | |
|
| |
5893 | return false; |
5894 | |
5895 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
5896 | |
5897 | |
5898 | unsigned PathLength = LHS.Designator.Entries.size(); |
5899 | for (const Expr *E = LHSExpr; E != nullptr;) { |
| 95 | | Assuming pointer value is null | |
|
| 96 | | Loop condition is false. Execution continues on line 5960 | |
|
5900 | |
5901 | if (auto *ME = dyn_cast<MemberExpr>(E)) { |
5902 | auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
5903 | |
5904 | |
5905 | if (!FD || FD->getType()->isReferenceType()) |
5906 | break; |
5907 | |
5908 | |
5909 | if (FD->getParent()->isUnion()) { |
5910 | |
5911 | |
5912 | |
5913 | auto *RD = |
5914 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
5915 | if (!RD || RD->hasTrivialDefaultConstructor()) |
5916 | UnionPathLengths.push_back({PathLength - 1, FD}); |
5917 | } |
5918 | |
5919 | E = ME->getBase(); |
5920 | --PathLength; |
5921 | assert(declaresSameEntity(FD, |
5922 | LHS.Designator.Entries[PathLength] |
5923 | .getAsBaseOrMember().getPointer())); |
5924 | |
5925 | |
5926 | |
5927 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
5928 | |
5929 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
5930 | if (!Base->getType()->isArrayType()) |
5931 | break; |
5932 | |
5933 | E = Base; |
5934 | --PathLength; |
5935 | |
5936 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
5937 | |
5938 | E = ICE->getSubExpr(); |
5939 | if (ICE->getCastKind() == CK_NoOp) |
5940 | continue; |
5941 | if (ICE->getCastKind() != CK_DerivedToBase && |
5942 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
5943 | break; |
5944 | |
5945 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
5946 | --PathLength; |
5947 | (void)Elt; |
5948 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
5949 | LHS.Designator.Entries[PathLength] |
5950 | .getAsBaseOrMember().getPointer())); |
5951 | } |
5952 | |
5953 | |
5954 | } else { |
5955 | break; |
5956 | } |
5957 | } |
5958 | |
5959 | |
5960 | if (UnionPathLengths.empty()) |
| 97 | | Calling 'SmallVectorBase::empty' | |
|
| 100 | | Returning from 'SmallVectorBase::empty' | |
|
| |
5961 | return true; |
5962 | |
5963 | |
5964 | |
5965 | CompleteObject Obj = |
5966 | findCompleteObject(Info, LHSExpr, AK_Assign, LHS, LHSExpr->getType()); |
| 102 | | Called C++ object pointer is null |
|