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 |
|
5967 | if (!Obj) |
5968 | return false; |
5969 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
5970 | llvm::reverse(UnionPathLengths)) { |
5971 | |
5972 | SubobjectDesignator D = LHS.Designator; |
5973 | D.truncate(Info.Ctx, LHS.Base, LengthAndField.first); |
5974 | |
5975 | bool DuringInit = Info.isEvaluatingCtorDtor(LHS.Base, D.Entries) == |
5976 | ConstructionPhase::AfterBases; |
5977 | StartLifetimeOfUnionMemberHandler StartLifetime{ |
5978 | Info, LHSExpr, LengthAndField.second, DuringInit}; |
5979 | if (!findSubobject(Info, LHSExpr, Obj, D, StartLifetime)) |
5980 | return false; |
5981 | } |
5982 | |
5983 | return true; |
5984 | } |
5985 | |
5986 | static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg, |
5987 | CallRef Call, EvalInfo &Info, |
5988 | bool NonNull = false) { |
5989 | LValue LV; |
5990 | |
5991 | |
5992 | |
5993 | |
5994 | |
5995 | APValue &V = PVD ? Info.CurrentCall->createParam(Call, PVD, LV) |
5996 | : Info.CurrentCall->createTemporary(Arg, Arg->getType(), |
5997 | ScopeKind::Call, LV); |
5998 | if (!EvaluateInPlace(V, Info, LV, Arg)) |
5999 | return false; |
6000 | |
6001 | |
6002 | |
6003 | if (NonNull && V.isLValue() && V.isNullPointer()) { |
6004 | Info.CCEDiag(Arg, diag::note_non_null_attribute_failed); |
6005 | return false; |
6006 | } |
6007 | |
6008 | return true; |
6009 | } |
6010 | |
6011 | |
6012 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call, |
6013 | EvalInfo &Info, const FunctionDecl *Callee, |
6014 | bool RightToLeft = false) { |
6015 | bool Success = true; |
6016 | llvm::SmallBitVector ForbiddenNullArgs; |
6017 | if (Callee->hasAttr<NonNullAttr>()) { |
6018 | ForbiddenNullArgs.resize(Args.size()); |
6019 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
6020 | if (!Attr->args_size()) { |
6021 | ForbiddenNullArgs.set(); |
6022 | break; |
6023 | } else |
6024 | for (auto Idx : Attr->args()) { |
6025 | unsigned ASTIdx = Idx.getASTIndex(); |
6026 | if (ASTIdx >= Args.size()) |
6027 | continue; |
6028 | ForbiddenNullArgs[ASTIdx] = 1; |
6029 | } |
6030 | } |
6031 | } |
6032 | for (unsigned I = 0; I < Args.size(); I++) { |
6033 | unsigned Idx = RightToLeft ? Args.size() - I - 1 : I; |
6034 | const ParmVarDecl *PVD = |
6035 | Idx < Callee->getNumParams() ? Callee->getParamDecl(Idx) : nullptr; |
6036 | bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx]; |
6037 | if (!EvaluateCallArg(PVD, Args[Idx], Call, Info, NonNull)) { |
6038 | |
6039 | |
6040 | if (!Info.noteFailure()) |
6041 | return false; |
6042 | Success = false; |
6043 | } |
6044 | } |
6045 | return Success; |
6046 | } |
6047 | |
6048 | |
6049 | |
6050 | static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param, |
6051 | const Expr *E, APValue &Result, |
6052 | bool CopyObjectRepresentation) { |
6053 | |
6054 | CallStackFrame *Frame = Info.CurrentCall; |
6055 | APValue *RefValue = Info.getParamSlot(Frame->Arguments, Param); |
| 57 | | Calling 'EvalInfo::getParamSlot' | |
|
| 65 | | Returning from 'EvalInfo::getParamSlot' | |
|
6056 | if (!RefValue) { |
| 66 | | Assuming 'RefValue' is non-null | |
|
| |
6057 | Info.FFDiag(E); |
6058 | return false; |
6059 | } |
6060 | |
6061 | |
6062 | LValue RefLValue; |
6063 | RefLValue.setFrom(Info.Ctx, *RefValue); |
6064 | return handleLValueToRValueConversion( |
| 68 | | Calling 'handleLValueToRValueConversion' | |
|
| 84 | | Returning from 'handleLValueToRValueConversion' | |
|
| 85 | | Returning value, which participates in a condition later | |
|
6065 | Info, E, Param->getType().getNonReferenceType(), RefLValue, Result, |
6066 | CopyObjectRepresentation); |
6067 | } |
6068 | |
6069 | |
6070 | static bool HandleFunctionCall(SourceLocation CallLoc, |
6071 | const FunctionDecl *Callee, const LValue *This, |
6072 | ArrayRef<const Expr *> Args, CallRef Call, |
6073 | const Stmt *Body, EvalInfo &Info, |
6074 | APValue &Result, const LValue *ResultSlot) { |
6075 | if (!Info.CheckCallLimit(CallLoc)) |
| 17 | | Calling 'EvalInfo::CheckCallLimit' | |
|
| 28 | | Returning from 'EvalInfo::CheckCallLimit' | |
|
| |
6076 | return false; |
6077 | |
6078 | CallStackFrame Frame(Info, CallLoc, Callee, This, Call); |
6079 | |
6080 | |
6081 | |
6082 | |
6083 | |
6084 | |
6085 | |
6086 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
| 30 | | 'Callee' is a 'CXXMethodDecl' | |
|
6087 | if (MD && MD->isDefaulted() && |
| 31 | | Assuming the condition is true | |
|
| |
6088 | (MD->getParent()->isUnion() || |
| 32 | | Calling 'TagDecl::isUnion' | |
|
| 35 | | Returning from 'TagDecl::isUnion' | |
|
6089 | (MD->isTrivial() && |
| 36 | | Assuming the condition is true | |
|
6090 | isReadByLvalueToRvalueConversion(MD->getParent())))) { |
| 37 | | Calling 'isReadByLvalueToRvalueConversion' | |
|
| 54 | | Returning from 'isReadByLvalueToRvalueConversion' | |
|
6091 | assert(This && |
6092 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
6093 | APValue RHSValue; |
6094 | if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue, |
| 56 | | Calling 'handleTrivialCopy' | |
|
| 86 | | Returning from 'handleTrivialCopy' | |
|
| 87 | | Assuming the condition is false | |
|
| |
6095 | MD->getParent()->isUnion())) |
6096 | return false; |
6097 | if (Info.getLangOpts().CPlusPlus20 && MD->isTrivial() && |
| 89 | | Assuming field 'CPlusPlus20' is not equal to 0 | |
|
6098 | !HandleUnionActiveMemberChange(Info, Args[0], *This)) |
| 90 | | Passing value via 2nd parameter 'LHSExpr' | |
|
| 91 | | Calling 'HandleUnionActiveMemberChange' | |
|
6099 | return false; |
6100 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(), |
6101 | RHSValue)) |
6102 | return false; |
6103 | This->moveInto(Result); |
6104 | return true; |
6105 | } else if (MD && isLambdaCallOperator(MD)) { |
6106 | |
6107 | |
6108 | |
6109 | |
6110 | |
6111 | |
6112 | if (!Info.checkingPotentialConstantExpression()) |
6113 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
6114 | Frame.LambdaThisCaptureField); |
6115 | } |
6116 | |
6117 | StmtResult Ret = {Result, ResultSlot}; |
6118 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
6119 | if (ESR == ESR_Succeeded) { |
6120 | if (Callee->getReturnType()->isVoidType()) |
6121 | return true; |
6122 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
6123 | } |
6124 | return ESR == ESR_Returned; |
6125 | } |
6126 | |
6127 | |
6128 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6129 | CallRef Call, |
6130 | const CXXConstructorDecl *Definition, |
6131 | EvalInfo &Info, APValue &Result) { |
6132 | SourceLocation CallLoc = E->getExprLoc(); |
6133 | if (!Info.CheckCallLimit(CallLoc)) |
6134 | return false; |
6135 | |
6136 | const CXXRecordDecl *RD = Definition->getParent(); |
6137 | if (RD->getNumVBases()) { |
6138 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6139 | return false; |
6140 | } |
6141 | |
6142 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
6143 | Info, |
6144 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
6145 | RD->getNumBases()); |
6146 | CallStackFrame Frame(Info, CallLoc, Definition, &This, Call); |
6147 | |
6148 | |
6149 | |
6150 | APValue RetVal; |
6151 | StmtResult Ret = {RetVal, nullptr}; |
6152 | |
6153 | |
6154 | if (Definition->isDelegatingConstructor()) { |
6155 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
6156 | if ((*I)->getInit()->isValueDependent()) { |
6157 | if (!EvaluateDependentExpr((*I)->getInit(), Info)) |
6158 | return false; |
6159 | } else { |
6160 | FullExpressionRAII InitScope(Info); |
6161 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit()) || |
6162 | !InitScope.destroy()) |
6163 | return false; |
6164 | } |
6165 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
6166 | } |
6167 | |
6168 | |
6169 | |
6170 | |
6171 | |
6172 | |
6173 | |
6174 | |
6175 | |
6176 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
6177 | (Definition->getParent()->isUnion() || |
6178 | (Definition->isTrivial() && |
6179 | isReadByLvalueToRvalueConversion(Definition->getParent())))) { |
6180 | return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result, |
6181 | Definition->getParent()->isUnion()); |
6182 | } |
6183 | |
6184 | |
6185 | if (!Result.hasValue()) { |
6186 | if (!RD->isUnion()) |
6187 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
6188 | std::distance(RD->field_begin(), RD->field_end())); |
6189 | else |
6190 | |
6191 | Result = APValue((const FieldDecl*)nullptr); |
6192 | } |
6193 | |
6194 | if (RD->isInvalidDecl()) return false; |
6195 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6196 | |
6197 | |
6198 | BlockScopeRAII LifetimeExtendedScope(Info); |
6199 | |
6200 | bool Success = true; |
6201 | unsigned BasesSeen = 0; |
6202 | #ifndef NDEBUG |
6203 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
6204 | #endif |
6205 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
6206 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
6207 | |
6208 | |
6209 | if (FieldIt == RD->field_end() || |
6210 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
6211 | assert(Indirect && "fields out of order?"); |
6212 | return; |
6213 | } |
6214 | |
6215 | |
6216 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
6217 | assert(FieldIt != RD->field_end() && "missing field?"); |
6218 | if (!FieldIt->isUnnamedBitfield()) |
6219 | Success &= getDefaultInitValue( |
6220 | FieldIt->getType(), |
6221 | Result.getStructField(FieldIt->getFieldIndex())); |
6222 | } |
6223 | ++FieldIt; |
6224 | }; |
6225 | for (const auto *I : Definition->inits()) { |
6226 | LValue Subobject = This; |
6227 | LValue SubobjectParent = This; |
6228 | APValue *Value = &Result; |
6229 | |
6230 | |
6231 | FieldDecl *FD = nullptr; |
6232 | if (I->isBaseInitializer()) { |
6233 | QualType BaseType(I->getBaseClass(), 0); |
6234 | #ifndef NDEBUG |
6235 | |
6236 | |
6237 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
6238 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
6239 | "base class initializers not in expected order"); |
6240 | ++BaseIt; |
6241 | #endif |
6242 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
6243 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6244 | return false; |
6245 | Value = &Result.getStructBase(BasesSeen++); |
6246 | } else if ((FD = I->getMember())) { |
6247 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
6248 | return false; |
6249 | if (RD->isUnion()) { |
6250 | Result = APValue(FD); |
6251 | Value = &Result.getUnionValue(); |
6252 | } else { |
6253 | SkipToField(FD, false); |
6254 | Value = &Result.getStructField(FD->getFieldIndex()); |
6255 | } |
6256 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
6257 | |
6258 | |
6259 | auto IndirectFieldChain = IFD->chain(); |
6260 | for (auto *C : IndirectFieldChain) { |
6261 | FD = cast<FieldDecl>(C); |
6262 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
6263 | |
6264 | |
6265 | |
6266 | |
6267 | |
6268 | if (!Value->hasValue() || |
6269 | (Value->isUnion() && Value->getUnionField() != FD)) { |
6270 | if (CD->isUnion()) |
6271 | *Value = APValue(FD); |
6272 | else |
6273 | |
6274 | |
6275 | |
6276 | Success &= getDefaultInitValue(Info.Ctx.getRecordType(CD), *Value); |
6277 | } |
6278 | |
6279 | |
6280 | if (C == IndirectFieldChain.back()) |
6281 | SubobjectParent = Subobject; |
6282 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
6283 | return false; |
6284 | if (CD->isUnion()) |
6285 | Value = &Value->getUnionValue(); |
6286 | else { |
6287 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
6288 | SkipToField(FD, true); |
6289 | Value = &Value->getStructField(FD->getFieldIndex()); |
6290 | } |
6291 | } |
6292 | } else { |
6293 | llvm_unreachable("unknown base initializer kind"); |
6294 | } |
6295 | |
6296 | |
6297 | |
6298 | |
6299 | const Expr *Init = I->getInit(); |
6300 | if (Init->isValueDependent()) { |
6301 | if (!EvaluateDependentExpr(Init, Info)) |
6302 | return false; |
6303 | } else { |
6304 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
6305 | isa<CXXDefaultInitExpr>(Init)); |
6306 | FullExpressionRAII InitScope(Info); |
6307 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
6308 | (FD && FD->isBitField() && |
6309 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
6310 | |
6311 | |
6312 | if (!Info.noteFailure()) |
6313 | return false; |
6314 | Success = false; |
6315 | } |
6316 | } |
6317 | |
6318 | |
6319 | |
6320 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
6321 | EvalObj.finishedConstructingBases(); |
6322 | } |
6323 | |
6324 | |
6325 | if (!RD->isUnion()) { |
6326 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
6327 | if (!FieldIt->isUnnamedBitfield()) |
6328 | Success &= getDefaultInitValue( |
6329 | FieldIt->getType(), |
6330 | Result.getStructField(FieldIt->getFieldIndex())); |
6331 | } |
6332 | } |
6333 | |
6334 | EvalObj.finishedConstructingFields(); |
6335 | |
6336 | return Success && |
6337 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed && |
6338 | LifetimeExtendedScope.destroy(); |
6339 | } |
6340 | |
6341 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6342 | ArrayRef<const Expr*> Args, |
6343 | const CXXConstructorDecl *Definition, |
6344 | EvalInfo &Info, APValue &Result) { |
6345 | CallScopeRAII CallScope(Info); |
6346 | CallRef Call = Info.CurrentCall->createCall(Definition); |
6347 | if (!EvaluateArgs(Args, Call, Info, Definition)) |
6348 | return false; |
6349 | |
6350 | return HandleConstructorCall(E, This, Call, Definition, Info, Result) && |
6351 | CallScope.destroy(); |
6352 | } |
6353 | |
6354 | static bool HandleDestructionImpl(EvalInfo &Info, SourceLocation CallLoc, |
6355 | const LValue &This, APValue &Value, |
6356 | QualType T) { |
6357 | |
6358 | |
6359 | |
6360 | |
6361 | if (Value.isAbsent() && !T->isNullPtrType()) { |
6362 | APValue Printable; |
6363 | This.moveInto(Printable); |
6364 | Info.FFDiag(CallLoc, diag::note_constexpr_destroy_out_of_lifetime) |
6365 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
6366 | return false; |
6367 | } |
6368 | |
6369 | |
6370 | |
6371 | OpaqueValueExpr LocE(CallLoc, Info.Ctx.IntTy, VK_PRValue); |
6372 | |
6373 | |
6374 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
6375 | uint64_t Size = CAT->getSize().getZExtValue(); |
6376 | QualType ElemT = CAT->getElementType(); |
6377 | |
6378 | LValue ElemLV = This; |
6379 | ElemLV.addArray(Info, &LocE, CAT); |
6380 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
6381 | return false; |
6382 | |
6383 | |
6384 | |
6385 | |
6386 | if (Size && Size > Value.getArrayInitializedElts()) |
6387 | expandArray(Value, Value.getArraySize() - 1); |
6388 | |
6389 | for (; Size != 0; --Size) { |
6390 | APValue &Elem = Value.getArrayInitializedElt(Size - 1); |
6391 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
6392 | !HandleDestructionImpl(Info, CallLoc, ElemLV, Elem, ElemT)) |
6393 | return false; |
6394 | } |
6395 | |
6396 | |
6397 | Value = APValue(); |
6398 | return true; |
6399 | } |
6400 | |
6401 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
6402 | if (!RD) { |
6403 | if (T.isDestructedType()) { |
6404 | Info.FFDiag(CallLoc, diag::note_constexpr_unsupported_destruction) << T; |
6405 | return false; |
6406 | } |
6407 | |
6408 | Value = APValue(); |
6409 | return true; |
6410 | } |
6411 | |
6412 | if (RD->getNumVBases()) { |
6413 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6414 | return false; |
6415 | } |
6416 | |
6417 | const CXXDestructorDecl *DD = RD->getDestructor(); |
6418 | if (!DD && !RD->hasTrivialDestructor()) { |
6419 | Info.FFDiag(CallLoc); |
6420 | return false; |
6421 | } |
6422 | |
6423 | if (!DD || DD->isTrivial() || |
6424 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
6425 | |
6426 | |
6427 | |
6428 | |
6429 | |
6430 | |
6431 | |
6432 | |
6433 | |
6434 | Value = APValue(); |
6435 | return true; |
6436 | } |
6437 | |
6438 | if (!Info.CheckCallLimit(CallLoc)) |
6439 | return false; |
6440 | |
6441 | const FunctionDecl *Definition = nullptr; |
6442 | const Stmt *Body = DD->getBody(Definition); |
6443 | |
6444 | if (!CheckConstexprFunction(Info, CallLoc, DD, Definition, Body)) |
6445 | return false; |
6446 | |
6447 | CallStackFrame Frame(Info, CallLoc, Definition, &This, CallRef()); |
6448 | |
6449 | |
6450 | unsigned BasesLeft = RD->getNumBases(); |
6451 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
6452 | Info, |
6453 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
6454 | if (!EvalObj.DidInsert) { |
6455 | |
6456 | |
6457 | |
6458 | |
6459 | |
6460 | |
6461 | Info.FFDiag(CallLoc, diag::note_constexpr_double_destroy); |
6462 | return false; |
6463 | } |
6464 | |
6465 | |
6466 | |
6467 | APValue RetVal; |
6468 | StmtResult Ret = {RetVal, nullptr}; |
6469 | if (EvaluateStmt(Ret, Info, Definition->getBody()) == ESR_Failed) |
6470 | return false; |
6471 | |
6472 | |
6473 | if (RD->isUnion()) |
6474 | return true; |
6475 | |
6476 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6477 | |
6478 | |
6479 | |
6480 | SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
6481 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
6482 | if (FD->isUnnamedBitfield()) |
6483 | continue; |
6484 | |
6485 | LValue Subobject = This; |
6486 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
6487 | return false; |
6488 | |
6489 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
6490 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
6491 | FD->getType())) |
6492 | return false; |
6493 | } |
6494 | |
6495 | if (BasesLeft != 0) |
6496 | EvalObj.startedDestroyingBases(); |
6497 | |
6498 | |
6499 | for (const CXXBaseSpecifier &Base : llvm::reverse(RD->bases())) { |
6500 | --BasesLeft; |
6501 | |
6502 | QualType BaseType = Base.getType(); |
6503 | LValue Subobject = This; |
6504 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
6505 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6506 | return false; |
6507 | |
6508 | APValue *SubobjectValue = &Value.getStructBase(BasesLeft); |
6509 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
6510 | BaseType)) |
6511 | return false; |
6512 | } |
6513 | assert(BasesLeft == 0 && "NumBases was wrong?"); |
6514 | |
6515 | |
6516 | Value = APValue(); |
6517 | return true; |
6518 | } |
6519 | |
6520 | namespace { |
6521 | struct DestroyObjectHandler { |
6522 | EvalInfo &Info; |
6523 | const Expr *E; |
6524 | const LValue &This; |
6525 | const AccessKinds AccessKind; |
6526 | |
6527 | typedef bool result_type; |
6528 | bool failed() { return false; } |
6529 | bool found(APValue &Subobj, QualType SubobjType) { |
6530 | return HandleDestructionImpl(Info, E->getExprLoc(), This, Subobj, |
6531 | SubobjType); |
6532 | } |
6533 | bool found(APSInt &Value, QualType SubobjType) { |
6534 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6535 | return false; |
6536 | } |
6537 | bool found(APFloat &Value, QualType SubobjType) { |
6538 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6539 | return false; |
6540 | } |
6541 | }; |
6542 | } |
6543 | |
6544 | |
6545 | |
6546 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
6547 | const LValue &This, QualType ThisType) { |
6548 | CompleteObject Obj = findCompleteObject(Info, E, AK_Destroy, This, ThisType); |
6549 | DestroyObjectHandler Handler = {Info, E, This, AK_Destroy}; |
6550 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
6551 | } |
6552 | |
6553 | |
6554 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
6555 | APValue::LValueBase LVBase, APValue &Value, |
6556 | QualType T) { |
6557 | |
6558 | |
6559 | if (Info.EvalStatus.HasSideEffects) |
6560 | return false; |
6561 | |
6562 | LValue LV; |
6563 | LV.set({LVBase}); |
6564 | return HandleDestructionImpl(Info, Loc, LV, Value, T); |
6565 | } |
6566 | |
6567 | |
6568 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
6569 | LValue &Result) { |
6570 | if (Info.checkingPotentialConstantExpression() || |
6571 | Info.SpeculativeEvaluationDepth) |
6572 | return false; |
6573 | |
6574 | |
6575 | auto Caller = Info.getStdAllocatorCaller("allocate"); |
6576 | if (!Caller) { |
6577 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus20 |
6578 | ? diag::note_constexpr_new_untyped |
6579 | : diag::note_constexpr_new); |
6580 | return false; |
6581 | } |
6582 | |
6583 | QualType ElemType = Caller.ElemType; |
6584 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
6585 | Info.FFDiag(E->getExprLoc(), |
6586 | diag::note_constexpr_new_not_complete_object_type) |
6587 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
6588 | return false; |
6589 | } |
6590 | |
6591 | APSInt ByteSize; |
6592 | if (!EvaluateInteger(E->getArg(0), ByteSize, Info)) |
6593 | return false; |
6594 | bool IsNothrow = false; |
6595 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
6596 | EvaluateIgnoredValue(Info, E->getArg(I)); |
6597 | IsNothrow |= E->getType()->isNothrowT(); |
6598 | } |
6599 | |
6600 | CharUnits ElemSize; |
6601 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
6602 | return false; |
6603 | APInt Size, Remainder; |
6604 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
6605 | APInt::udivrem(ByteSize, ElemSizeAP, Size, Remainder); |
6606 | if (Remainder != 0) { |
6607 | |
6608 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
6609 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
6610 | return false; |
6611 | } |
6612 | |
6613 | if (ByteSize.getActiveBits() > ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
6614 | if (IsNothrow) { |
6615 | Result.setNull(Info.Ctx, E->getType()); |
6616 | return true; |
6617 | } |
6618 | |
6619 | Info.FFDiag(E, diag::note_constexpr_new_too_large) << APSInt(Size, true); |
6620 | return false; |
6621 | } |
6622 | |
6623 | QualType AllocType = Info.Ctx.getConstantArrayType(ElemType, Size, nullptr, |
6624 | ArrayType::Normal, 0); |
6625 | APValue *Val = Info.createHeapAlloc(E, AllocType, Result); |
6626 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
6627 | Result.addArray(Info, E, cast<ConstantArrayType>(AllocType)); |
6628 | return true; |
6629 | } |
6630 | |
6631 | static bool hasVirtualDestructor(QualType T) { |
6632 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6633 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6634 | return DD->isVirtual(); |
6635 | return false; |
6636 | } |
6637 | |
6638 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
6639 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6640 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6641 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
6642 | return nullptr; |
6643 | } |
6644 | |
6645 | |
6646 | |
6647 | |
6648 | |
6649 | |
6650 | static Optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
6651 | const LValue &Pointer, |
6652 | DynAlloc::Kind DeallocKind) { |
6653 | auto PointerAsString = [&] { |
6654 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
6655 | }; |
6656 | |
6657 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
6658 | if (!DA) { |
6659 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
6660 | << PointerAsString(); |
6661 | if (Pointer.Base) |
6662 | NoteLValueLocation(Info, Pointer.Base); |
6663 | return None; |
6664 | } |
6665 | |
6666 | Optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
6667 | if (!Alloc) { |
6668 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
6669 | return None; |
6670 | } |
6671 | |
6672 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
6673 | if (DeallocKind != (*Alloc)->getKind()) { |
6674 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
6675 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
6676 | NoteLValueLocation(Info, Pointer.Base); |
6677 | return None; |
6678 | } |
6679 | |
6680 | bool Subobject = false; |
6681 | if (DeallocKind == DynAlloc::New) { |
6682 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
6683 | Pointer.Designator.isOnePastTheEnd(); |
6684 | } else { |
6685 | Subobject = Pointer.Designator.Entries.size() != 1 || |
6686 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
6687 | } |
6688 | if (Subobject) { |
6689 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
6690 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
6691 | return None; |
6692 | } |
6693 | |
6694 | return Alloc; |
6695 | } |
6696 | |
6697 | |
6698 | bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
6699 | if (Info.checkingPotentialConstantExpression() || |
6700 | Info.SpeculativeEvaluationDepth) |
6701 | return false; |
6702 | |
6703 | |
6704 | if (!Info.getStdAllocatorCaller("deallocate")) { |
6705 | Info.FFDiag(E->getExprLoc()); |
6706 | return true; |
6707 | } |
6708 | |
6709 | LValue Pointer; |
6710 | if (!EvaluatePointer(E->getArg(0), Pointer, Info)) |
6711 | return false; |
6712 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
6713 | EvaluateIgnoredValue(Info, E->getArg(I)); |
6714 | |
6715 | if (Pointer.Designator.Invalid) |
6716 | return false; |
6717 | |
6718 | |
6719 | |
6720 | if (Pointer.isNullPointer()) { |
6721 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null); |
6722 | return true; |
6723 | } |
6724 | |
6725 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
6726 | return false; |
6727 | |
6728 | Info.HeapAllocs.erase(Pointer.Base.get<DynamicAllocLValue>()); |
6729 | return true; |
6730 | } |
6731 | |
6732 | |
6733 | |
6734 | |
6735 | namespace { |
6736 | |
6737 | class BitCastBuffer { |
6738 | |
6739 | |
6740 | |
6741 | |
6742 | |
6743 | SmallVector<Optional<unsigned char>, 32> Bytes; |
6744 | |
6745 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
6746 | "Need at least 8 bit unsigned char"); |
6747 | |
6748 | bool TargetIsLittleEndian; |
6749 | |
6750 | public: |
6751 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
6752 | : Bytes(Width.getQuantity()), |
6753 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
6754 | |
6755 | LLVM_NODISCARD |
6756 | bool readObject(CharUnits Offset, CharUnits Width, |
6757 | SmallVectorImpl<unsigned char> &Output) const { |
6758 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
6759 | |
6760 | |
6761 | if (!Bytes[I.getQuantity()]) |
6762 | return false; |
6763 | Output.push_back(*Bytes[I.getQuantity()]); |
6764 | } |
6765 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6766 | std::reverse(Output.begin(), Output.end()); |
6767 | return true; |
6768 | } |
6769 | |
6770 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
6771 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6772 | std::reverse(Input.begin(), Input.end()); |
6773 | |
6774 | size_t Index = 0; |
6775 | for (unsigned char Byte : Input) { |
6776 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?"); |
6777 | Bytes[Offset.getQuantity() + Index] = Byte; |
6778 | ++Index; |
6779 | } |
6780 | } |
6781 | |
6782 | size_t size() { return Bytes.size(); } |
6783 | }; |
6784 | |
6785 | |
6786 | |
6787 | class APValueToBufferConverter { |
6788 | EvalInfo &Info; |
6789 | BitCastBuffer Buffer; |
6790 | const CastExpr *BCE; |
6791 | |
6792 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
6793 | const CastExpr *BCE) |
6794 | : Info(Info), |
6795 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
6796 | BCE(BCE) {} |
6797 | |
6798 | bool visit(const APValue &Val, QualType Ty) { |
6799 | return visit(Val, Ty, CharUnits::fromQuantity(0)); |
6800 | } |
6801 | |
6802 | |
6803 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
6804 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
6805 | |
6806 | |
6807 | if (Ty->isNullPtrType()) |
6808 | return true; |
6809 | |
6810 | |
6811 | switch (Val.getKind()) { |
6812 | case APValue::Indeterminate: |
6813 | case APValue::None: |
6814 | return true; |
6815 | |
6816 | case APValue::Int: |
6817 | return visitInt(Val.getInt(), Ty, Offset); |
6818 | case APValue::Float: |
6819 | return visitFloat(Val.getFloat(), Ty, Offset); |
6820 | case APValue::Array: |
6821 | return visitArray(Val, Ty, Offset); |
6822 | case APValue::Struct: |
6823 | return visitRecord(Val, Ty, Offset); |
6824 | |
6825 | case APValue::ComplexInt: |
6826 | case APValue::ComplexFloat: |
6827 | case APValue::Vector: |
6828 | case APValue::FixedPoint: |
6829 | |
6830 | |
6831 | case APValue::Union: |
6832 | case APValue::MemberPointer: |
6833 | case APValue::AddrLabelDiff: { |
6834 | Info.FFDiag(BCE->getBeginLoc(), |
6835 | diag::note_constexpr_bit_cast_unsupported_type) |
6836 | << Ty; |
6837 | return false; |
6838 | } |
6839 | |
6840 | case APValue::LValue: |
6841 | llvm_unreachable("LValue subobject in bit_cast?"); |
6842 | } |
6843 | llvm_unreachable("Unhandled APValue::ValueKind"); |
6844 | } |
6845 | |
6846 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
6847 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
6848 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6849 | |
6850 | |
6851 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
6852 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
6853 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
6854 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
6855 | |
6856 | if (!visitRecord(Val.getStructBase(I), BS.getType(), |
6857 | Layout.getBaseClassOffset(BaseDecl) + Offset)) |
6858 | return false; |
6859 | } |
6860 | } |
6861 | |
6862 | |
6863 | unsigned FieldIdx = 0; |
6864 | for (FieldDecl *FD : RD->fields()) { |
6865 | if (FD->isBitField()) { |
6866 | Info.FFDiag(BCE->getBeginLoc(), |
6867 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
6868 | return false; |
6869 | } |
6870 | |
6871 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
6872 | |
6873 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
6874 | "only bit-fields can have sub-char alignment"); |
6875 | CharUnits FieldOffset = |
6876 | Info.Ctx.toCharUnitsFromBits(FieldOffsetBits) + Offset; |
6877 | QualType FieldTy = FD->getType(); |
6878 | if (!visit(Val.getStructField(FieldIdx), FieldTy, FieldOffset)) |
6879 | return false; |
6880 | ++FieldIdx; |
6881 | } |
6882 | |
6883 | return true; |
6884 | } |
6885 | |
6886 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
6887 | const auto *CAT = |
6888 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
6889 | if (!CAT) |
6890 | return false; |
6891 | |
6892 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
6893 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
6894 | unsigned ArraySize = Val.getArraySize(); |
6895 | |
6896 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
6897 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
6898 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
6899 | return false; |
6900 | } |
6901 | |
6902 | |
6903 | if (Val.hasArrayFiller()) { |
6904 | const APValue &Filler = Val.getArrayFiller(); |
6905 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
6906 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
6907 | return false; |
6908 | } |
6909 | } |
6910 | |
6911 | return true; |
6912 | } |
6913 | |
6914 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
6915 | APSInt AdjustedVal = Val; |
6916 | unsigned Width = AdjustedVal.getBitWidth(); |
6917 | if (Ty->isBooleanType()) { |
6918 | Width = Info.Ctx.getTypeSize(Ty); |
6919 | AdjustedVal = AdjustedVal.extend(Width); |
6920 | } |
6921 | |
6922 | SmallVector<unsigned char, 8> Bytes(Width / 8); |
6923 | llvm::StoreIntToMemory(AdjustedVal, &*Bytes.begin(), Width / 8); |
6924 | Buffer.writeObject(Offset, Bytes); |
6925 | return true; |
6926 | } |
6927 | |
6928 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
6929 | APSInt AsInt(Val.bitcastToAPInt()); |
6930 | return visitInt(AsInt, Ty, Offset); |
6931 | } |
6932 | |
6933 | public: |
6934 | static Optional<BitCastBuffer> convert(EvalInfo &Info, const APValue &Src, |
6935 | const CastExpr *BCE) { |
6936 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
6937 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
6938 | if (!Converter.visit(Src, BCE->getSubExpr()->getType())) |
6939 | return None; |
6940 | return Converter.Buffer; |
6941 | } |
6942 | }; |
6943 | |
6944 | |
6945 | class BufferToAPValueConverter { |
6946 | EvalInfo &Info; |
6947 | const BitCastBuffer &Buffer; |
6948 | const CastExpr *BCE; |
6949 | |
6950 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
6951 | const CastExpr *BCE) |
6952 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
6953 | |
6954 | |
6955 | |
6956 | |
6957 | llvm::NoneType unsupportedType(QualType Ty) { |
6958 | Info.FFDiag(BCE->getBeginLoc(), |
6959 | diag::note_constexpr_bit_cast_unsupported_type) |
6960 | << Ty; |
6961 | return None; |
6962 | } |
6963 | |
6964 | llvm::NoneType unrepresentableValue(QualType Ty, const APSInt &Val) { |
6965 | Info.FFDiag(BCE->getBeginLoc(), |
6966 | diag::note_constexpr_bit_cast_unrepresentable_value) |
6967 | << Ty << toString(Val, 10); |
6968 | return None; |
6969 | } |
6970 | |
6971 | Optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
6972 | const EnumType *EnumSugar = nullptr) { |
6973 | if (T->isNullPtrType()) { |
6974 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QualType(T, 0)); |
6975 | return APValue((Expr *)nullptr, |
6976 | CharUnits::fromQuantity(NullValue), |
6977 | APValue::NoLValuePath{}, true); |
6978 | } |
6979 | |
6980 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
6981 | |
6982 | |
6983 | |
6984 | |
6985 | if (T->isRealFloatingType()) { |
6986 | const llvm::fltSemantics &Semantics = |
6987 | Info.Ctx.getFloatTypeSemantics(QualType(T, 0)); |
6988 | unsigned NumBits = llvm::APFloatBase::getSizeInBits(Semantics); |
6989 | assert(NumBits % 8 == 0); |
6990 | CharUnits NumBytes = CharUnits::fromQuantity(NumBits / 8); |
6991 | if (NumBytes != SizeOf) |
6992 | SizeOf = NumBytes; |
6993 | } |
6994 | |
6995 | SmallVector<uint8_t, 8> Bytes; |
6996 | if (!Buffer.readObject(Offset, SizeOf, Bytes)) { |
6997 | |
6998 | |
6999 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
7000 | bool IsUChar = |
7001 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
7002 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
7003 | if (!IsStdByte && !IsUChar) { |
7004 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
7005 | Info.FFDiag(BCE->getExprLoc(), |
7006 | diag::note_constexpr_bit_cast_indet_dest) |
7007 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
7008 | return None; |
7009 | } |
7010 | |
7011 | return APValue::IndeterminateValue(); |
7012 | } |
7013 | |
7014 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
7015 | llvm::LoadIntFromMemory(Val, &*Bytes.begin(), Bytes.size()); |
7016 | |
7017 | if (T->isIntegralOrEnumerationType()) { |
7018 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
7019 | |
7020 | unsigned IntWidth = Info.Ctx.getIntWidth(QualType(T, 0)); |
7021 | if (IntWidth != Val.getBitWidth()) { |
7022 | APSInt Truncated = Val.trunc(IntWidth); |
7023 | if (Truncated.extend(Val.getBitWidth()) != Val) |
7024 | return unrepresentableValue(QualType(T, 0), Val); |
7025 | Val = Truncated; |
7026 | } |
7027 | |
7028 | return APValue(Val); |
7029 | } |
7030 | |
7031 | if (T->isRealFloatingType()) { |
7032 | const llvm::fltSemantics &Semantics = |
7033 | Info.Ctx.getFloatTypeSemantics(QualType(T, 0)); |
7034 | return APValue(APFloat(Semantics, Val)); |
7035 | } |
7036 | |
7037 | return unsupportedType(QualType(T, 0)); |
7038 | } |
7039 | |
7040 | Optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
7041 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
7042 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
7043 | |
7044 | unsigned NumBases = 0; |
7045 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
7046 | NumBases = CXXRD->getNumBases(); |
7047 | |
7048 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
7049 | std::distance(RD->field_begin(), RD->field_end())); |
7050 | |
7051 | |
7052 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
7053 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7054 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7055 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7056 | if (BaseDecl->isEmpty() || |
7057 | Info.Ctx.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()) |
7058 | continue; |
7059 | |
7060 | Optional<APValue> SubObj = visitType( |
7061 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
7062 | if (!SubObj) |
7063 | return None; |
7064 | ResultVal.getStructBase(I) = *SubObj; |
7065 | } |
7066 | } |
7067 | |
7068 | |
7069 | unsigned FieldIdx = 0; |
7070 | for (FieldDecl *FD : RD->fields()) { |
7071 | |
7072 | |
7073 | if (FD->isBitField()) { |
7074 | Info.FFDiag(BCE->getBeginLoc(), |
7075 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7076 | return None; |
7077 | } |
7078 | |
7079 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
7080 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
7081 | |
7082 | CharUnits FieldOffset = |
7083 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
7084 | Offset; |
7085 | QualType FieldTy = FD->getType(); |
7086 | Optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
7087 | if (!SubObj) |
7088 | return None; |
7089 | ResultVal.getStructField(FieldIdx) = *SubObj; |
7090 | ++FieldIdx; |
7091 | } |
7092 | |
7093 | return ResultVal; |
7094 | } |
7095 | |
7096 | Optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
7097 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
7098 | assert(!RepresentationType.isNull() && |
7099 | "enum forward decl should be caught by Sema"); |
7100 | const auto *AsBuiltin = |
7101 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
7102 | |
7103 | |
7104 | return visit(AsBuiltin, Offset, Ty); |
7105 | } |
7106 | |
7107 | Optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
7108 | size_t Size = Ty->getSize().getLimitedValue(); |
7109 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
7110 | |
7111 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
7112 | for (size_t I = 0; I != Size; ++I) { |
7113 | Optional<APValue> ElementValue = |
7114 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7115 | if (!ElementValue) |
7116 | return None; |
7117 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
7118 | } |
7119 | |
7120 | return ArrayValue; |
7121 | } |
7122 | |
7123 | Optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
7124 | return unsupportedType(QualType(Ty, 0)); |
7125 | } |
7126 | |
7127 | Optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
7128 | QualType Can = Ty.getCanonicalType(); |
7129 | |
7130 | switch (Can->getTypeClass()) { |
7131 | #define TYPE(Class, Base) \ |
7132 | case Type::Class: \ |
7133 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
7134 | #define ABSTRACT_TYPE(Class, Base) |
7135 | #define NON_CANONICAL_TYPE(Class, Base) \ |
7136 | case Type::Class: \ |
7137 | llvm_unreachable("non-canonical type should be impossible!"); |
7138 | #define DEPENDENT_TYPE(Class, Base) \ |
7139 | case Type::Class: \ |
7140 | llvm_unreachable( \ |
7141 | "dependent types aren't supported in the constant evaluator!"); |
7142 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
7143 | case Type::Class: \ |
7144 | llvm_unreachable("either dependent or not canonical!"); |
7145 | #include "clang/AST/TypeNodes.inc" |
7146 | } |
7147 | llvm_unreachable("Unhandled Type::TypeClass"); |
7148 | } |
7149 | |
7150 | public: |
7151 | |
7152 | static Optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
7153 | const CastExpr *BCE) { |
7154 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
7155 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(0)); |
7156 | } |
7157 | }; |
7158 | |
7159 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
7160 | QualType Ty, EvalInfo *Info, |
7161 | const ASTContext &Ctx, |
7162 | bool CheckingDest) { |
7163 | Ty = Ty.getCanonicalType(); |
7164 | |
7165 | auto diag = [&](int Reason) { |
7166 | if (Info) |
7167 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
7168 | << CheckingDest << (Reason == 4) << Reason; |
7169 | return false; |
7170 | }; |
7171 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
7172 | if (Info) |
7173 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
7174 | << NoteTy << Construct << Ty; |
7175 | return false; |
7176 | }; |
7177 | |
7178 | if (Ty->isUnionType()) |
7179 | return diag(0); |
7180 | if (Ty->isPointerType()) |
7181 | return diag(1); |
7182 | if (Ty->isMemberPointerType()) |
7183 | return diag(2); |
7184 | if (Ty.isVolatileQualified()) |
7185 | return diag(3); |
7186 | |
7187 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
7188 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
7189 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
7190 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
7191 | CheckingDest)) |
7192 | return note(1, BS.getType(), BS.getBeginLoc()); |
7193 | } |
7194 | for (FieldDecl *FD : Record->fields()) { |
7195 | if (FD->getType()->isReferenceType()) |
7196 | return diag(4); |
7197 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
7198 | CheckingDest)) |
7199 | return note(0, FD->getType(), FD->getBeginLoc()); |
7200 | } |
7201 | } |
7202 | |
7203 | if (Ty->isArrayType() && |
7204 | !checkBitCastConstexprEligibilityType(Loc, Ctx.getBaseElementType(Ty), |
7205 | Info, Ctx, CheckingDest)) |
7206 | return false; |
7207 | |
7208 | return true; |
7209 | } |
7210 | |
7211 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
7212 | const ASTContext &Ctx, |
7213 | const CastExpr *BCE) { |
7214 | bool DestOK = checkBitCastConstexprEligibilityType( |
7215 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
7216 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
7217 | BCE->getBeginLoc(), |
7218 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
7219 | return SourceOK; |
7220 | } |
7221 | |
7222 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7223 | APValue &SourceValue, |
7224 | const CastExpr *BCE) { |
7225 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7226 | "no host or target supports non 8-bit chars"); |
7227 | assert(SourceValue.isLValue() && |
7228 | "LValueToRValueBitcast requires an lvalue operand!"); |
7229 | |
7230 | if (!checkBitCastConstexprEligibility(&Info, Info.Ctx, BCE)) |
7231 | return false; |
7232 | |
7233 | LValue SourceLValue; |
7234 | APValue SourceRValue; |
7235 | SourceLValue.setFrom(Info.Ctx, SourceValue); |
7236 | if (!handleLValueToRValueConversion( |
7237 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
7238 | SourceRValue, true)) |
7239 | return false; |
7240 | |
7241 | |
7242 | Optional<BitCastBuffer> Buffer = |
7243 | APValueToBufferConverter::convert(Info, SourceRValue, BCE); |
7244 | if (!Buffer) |
7245 | return false; |
7246 | |
7247 | |
7248 | Optional<APValue> MaybeDestValue = |
7249 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
7250 | if (!MaybeDestValue) |
7251 | return false; |
7252 | |
7253 | DestValue = std::move(*MaybeDestValue); |
7254 | return true; |
7255 | } |
7256 | |
7257 | template <class Derived> |
7258 | class ExprEvaluatorBase |
7259 | : public ConstStmtVisitor<Derived, bool> { |
7260 | private: |
7261 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
7262 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
7263 | return getDerived().Success(V, E); |
7264 | } |
7265 | bool DerivedZeroInitialization(const Expr *E) { |
7266 | return getDerived().ZeroInitialization(E); |
7267 | } |
7268 | |
7269 | |
7270 | |
7271 | |
7272 | template<typename ConditionalOperator> |
7273 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
7274 | assert(Info.checkingPotentialConstantExpression()); |
7275 | |
7276 | |
7277 | SmallVector<PartialDiagnosticAt, 8> Diag; |
7278 | { |
7279 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7280 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7281 | if (Diag.empty()) |
7282 | return; |
7283 | } |
7284 | |
7285 | { |
7286 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7287 | Diag.clear(); |
7288 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7289 | if (Diag.empty()) |
7290 | return; |
7291 | } |
7292 | |
7293 | Error(E, diag::note_constexpr_conditional_never_const); |
7294 | } |
7295 | |
7296 | |
7297 | template<typename ConditionalOperator> |
7298 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
7299 | bool BoolResult; |
7300 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
7301 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
7302 | CheckPotentialConstantConditional(E); |
7303 | return false; |
7304 | } |
7305 | if (Info.noteFailure()) { |
7306 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7307 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7308 | } |
7309 | return false; |
7310 | } |
7311 | |
7312 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
7313 | return StmtVisitorTy::Visit(EvalExpr); |
7314 | } |
7315 | |
7316 | protected: |
7317 | EvalInfo &Info; |
7318 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
7319 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
7320 | |
7321 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
7322 | return Info.CCEDiag(E, D); |
7323 | } |
7324 | |
7325 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
7326 | |
7327 | public: |
7328 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
7329 | |
7330 | EvalInfo &getEvalInfo() { return Info; } |
7331 | |
7332 | |
7333 | |
7334 | bool Error(const Expr *E, diag::kind D) { |
7335 | Info.FFDiag(E, D); |
7336 | return false; |
7337 | } |
7338 | bool Error(const Expr *E) { |
7339 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
7340 | } |
7341 | |
7342 | bool VisitStmt(const Stmt *) { |
7343 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
7344 | } |
7345 | bool VisitExpr(const Expr *E) { |
7346 | return Error(E); |
7347 | } |
7348 | |
7349 | bool VisitConstantExpr(const ConstantExpr *E) { |
7350 | if (E->hasAPValueResult()) |
7351 | return DerivedSuccess(E->getAPValueResult(), E); |
7352 | |
7353 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7354 | } |
7355 | |
7356 | bool VisitParenExpr(const ParenExpr *E) |
7357 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7358 | bool VisitUnaryExtension(const UnaryOperator *E) |
7359 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7360 | bool VisitUnaryPlus(const UnaryOperator *E) |
7361 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7362 | bool VisitChooseExpr(const ChooseExpr *E) |
7363 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
7364 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
7365 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
7366 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
7367 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
7368 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
7369 | TempVersionRAII RAII(*Info.CurrentCall); |
7370 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7371 | return StmtVisitorTy::Visit(E->getExpr()); |
7372 | } |
7373 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
7374 | TempVersionRAII RAII(*Info.CurrentCall); |
7375 | |
7376 | if (!E->getExpr()) |
7377 | return Error(E); |
7378 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7379 | return StmtVisitorTy::Visit(E->getExpr()); |
7380 | } |
7381 | |
7382 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
7383 | FullExpressionRAII Scope(Info); |
7384 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
7385 | } |
7386 | |
7387 | |
7388 | |
7389 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
7390 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7391 | } |
7392 | |
7393 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
7394 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
7395 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7396 | } |
7397 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
7398 | if (!Info.Ctx.getLangOpts().CPlusPlus20) |
7399 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
7400 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7401 | } |
7402 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
7403 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7404 | } |
7405 | |
7406 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7407 | switch (E->getOpcode()) { |
7408 | default: |
7409 | return Error(E); |
7410 | |
7411 | case BO_Comma: |
7412 | VisitIgnoredValue(E->getLHS()); |
7413 | return StmtVisitorTy::Visit(E->getRHS()); |
7414 | |
7415 | case BO_PtrMemD: |
7416 | case BO_PtrMemI: { |
7417 | LValue Obj; |
7418 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
7419 | return false; |
7420 | APValue Result; |
7421 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
7422 | return false; |
7423 | return DerivedSuccess(Result, E); |
7424 | } |
7425 | } |
7426 | } |
7427 | |
7428 | bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) { |
7429 | return StmtVisitorTy::Visit(E->getSemanticForm()); |
7430 | } |
7431 | |
7432 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
7433 | |
7434 | |
7435 | LValue CommonLV; |
7436 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7437 | E->getOpaqueValue(), |
7438 | getStorageType(Info.Ctx, E->getOpaqueValue()), |
7439 | ScopeKind::FullExpression, CommonLV), |
7440 | Info, E->getCommon())) |
7441 | return false; |
7442 | |
7443 | return HandleConditionalOperator(E); |
7444 | } |
7445 | |
7446 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
7447 | bool IsBcpCall = false; |
7448 | |
7449 | |
7450 | |
7451 | |
7452 | if (const CallExpr *CallCE = |
7453 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
7454 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
7455 | IsBcpCall = true; |
7456 | |
7457 | |
7458 | |
7459 | |
7460 | |
7461 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
7462 | return false; |
7463 | |
7464 | FoldConstant Fold(Info, IsBcpCall); |
7465 | if (!HandleConditionalOperator(E)) { |
7466 | Fold.keepDiagnostics(); |
7467 | return false; |
7468 | } |
7469 | |
7470 | return true; |
7471 | } |
7472 | |
7473 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
7474 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
7475 | return DerivedSuccess(*Value, E); |
7476 | |
7477 | const Expr *Source = E->getSourceExpr(); |
7478 | if (!Source) |
7479 | return Error(E); |
7480 | if (Source == E) { |
7481 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
7482 | return Error(E); |
7483 | } |
7484 | return StmtVisitorTy::Visit(Source); |
7485 | } |
7486 | |
7487 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
7488 | for (const Expr *SemE : E->semantics()) { |
7489 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { |
7490 | |
7491 | |
7492 | |
7493 | if (SemE == E->getResultExpr()) |
7494 | return Error(E); |
7495 | |
7496 | |
7497 | |
7498 | if (OVE->isUnique()) |
7499 | continue; |
7500 | |
7501 | LValue LV; |
7502 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7503 | OVE, getStorageType(Info.Ctx, OVE), |
7504 | ScopeKind::FullExpression, LV), |
7505 | Info, OVE->getSourceExpr())) |
7506 | return false; |
7507 | } else if (SemE == E->getResultExpr()) { |
7508 | if (!StmtVisitorTy::Visit(SemE)) |
7509 | return false; |
7510 | } else { |
7511 | if (!EvaluateIgnoredValue(Info, SemE)) |
7512 | return false; |
7513 | } |
7514 | } |
7515 | return true; |
7516 | } |
7517 | |
7518 | bool VisitCallExpr(const CallExpr *E) { |
7519 | APValue Result; |
7520 | if (!handleCallExpr(E, Result, nullptr)) |
7521 | return false; |
7522 | return DerivedSuccess(Result, E); |
7523 | } |
7524 | |
7525 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
7526 | const LValue *ResultSlot) { |
7527 | CallScopeRAII CallScope(Info); |
7528 | |
7529 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
7530 | QualType CalleeType = Callee->getType(); |
7531 | |
7532 | const FunctionDecl *FD = nullptr; |
7533 | LValue *This = nullptr, ThisVal; |
7534 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
7535 | bool HasQualifier = false; |
7536 | |
7537 | CallRef Call; |
7538 | |
7539 | |
7540 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
7541 | const CXXMethodDecl *Member = nullptr; |
7542 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
7543 | |
7544 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
7545 | return false; |
7546 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
7547 | if (!Member) |
7548 | return Error(Callee); |
7549 | This = &ThisVal; |
7550 | HasQualifier = ME->hasQualifier(); |
7551 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
7552 | |
7553 | const ValueDecl *D = |
7554 | HandleMemberPointerAccess(Info, BE, ThisVal, false); |
7555 | if (!D) |
7556 | return false; |
7557 | Member = dyn_cast<CXXMethodDecl>(D); |
7558 | if (!Member) |
7559 | return Error(Callee); |
7560 | This = &ThisVal; |
7561 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
7562 | if (!Info.getLangOpts().CPlusPlus20) |
7563 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
7564 | return EvaluateObjectArgument(Info, PDE->getBase(), ThisVal) && |
7565 | HandleDestruction(Info, PDE, ThisVal, PDE->getDestroyedType()); |
7566 | } else |
7567 | return Error(Callee); |
7568 | FD = Member; |
7569 | } else if (CalleeType->isFunctionPointerType()) { |
7570 | LValue CalleeLV; |
7571 | if (!EvaluatePointer(Callee, CalleeLV, Info)) |
7572 | return false; |
7573 | |
7574 | if (!CalleeLV.getLValueOffset().isZero()) |
7575 | return Error(Callee); |
7576 | FD = dyn_cast_or_null<FunctionDecl>( |
7577 | CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>()); |
7578 | if (!FD) |
7579 | return Error(Callee); |
7580 | |
7581 | |
7582 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
7583 | CalleeType->getPointeeType(), FD->getType())) { |
7584 | return Error(E); |
7585 | } |
7586 | |
7587 | |
7588 | |
7589 | auto *OCE = dyn_cast<CXXOperatorCallExpr>(E); |
7590 | if (OCE && OCE->isAssignmentOp()) { |
7591 | assert(Args.size() == 2 && "wrong number of arguments in assignment"); |
7592 | Call = Info.CurrentCall->createCall(FD); |
7593 | if (!EvaluateArgs(isa<CXXMethodDecl>(FD) ? Args.slice(1) : Args, Call, |
7594 | Info, FD, true)) |
7595 | return false; |
7596 | } |
7597 | |
7598 | |
7599 | |
7600 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
7601 | if (MD && !MD->isStatic()) { |
7602 | |
7603 | |
7604 | |
7605 | if (Args.empty()) |
7606 | return Error(E); |
7607 | |
7608 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
7609 | return false; |
7610 | This = &ThisVal; |
7611 | Args = Args.slice(1); |
7612 | } else if (MD && MD->isLambdaStaticInvoker()) { |
7613 | |
7614 | |
7615 | |
7616 | |
7617 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
7618 | assert( |
7619 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
7620 | "Number of captures must be zero for conversion to function-ptr"); |
7621 | |
7622 | const CXXMethodDecl *LambdaCallOp = |
7623 | ClosureClass->getLambdaCallOperator(); |
7624 | |
7625 | |
7626 | |
7627 | |
7628 | |
7629 | if (ClosureClass->isGenericLambda()) { |
7630 | assert(MD->isFunctionTemplateSpecialization() && |
7631 | "A generic lambda's static-invoker function must be a " |
7632 | "template specialization"); |
7633 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
7634 | FunctionTemplateDecl *CallOpTemplate = |
7635 | LambdaCallOp->getDescribedFunctionTemplate(); |
7636 | void *InsertPos = nullptr; |
7637 | FunctionDecl *CorrespondingCallOpSpecialization = |
7638 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
7639 | assert(CorrespondingCallOpSpecialization && |
7640 | "We must always have a function call operator specialization " |
7641 | "that corresponds to our static invoker specialization"); |
7642 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
7643 | } else |
7644 | FD = LambdaCallOp; |
7645 | } else if (FD->isReplaceableGlobalAllocationFunction()) { |
7646 | if (FD->getDeclName().getCXXOverloadedOperator() == OO_New || |
7647 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New) { |
7648 | LValue Ptr; |
7649 | if (!HandleOperatorNewCall(Info, E, Ptr)) |
7650 | return false; |
7651 | Ptr.moveInto(Result); |
7652 | return CallScope.destroy(); |
7653 | } else { |
7654 | return HandleOperatorDeleteCall(Info, E) && CallScope.destroy(); |
7655 | } |
7656 | } |
7657 | } else |
7658 | return Error(E); |
7659 | |
7660 | |
7661 | if (!Call) { |
7662 | Call = Info.CurrentCall->createCall(FD); |
7663 | if (!EvaluateArgs(Args, Call, Info, FD)) |
7664 | return false; |
7665 | } |
7666 | |
7667 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
7668 | if (This) { |
7669 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
7670 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
7671 | |
7672 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
7673 | CovariantAdjustmentPath); |
7674 | if (!FD) |
7675 | return false; |
7676 | } else { |
7677 | |
7678 | |
7679 | |
7680 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
7681 | return false; |
7682 | } |
7683 | } |
7684 | |
7685 | |
7686 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
7687 | assert(This && "no 'this' pointer for destructor call"); |
7688 | return HandleDestruction(Info, E, *This, |
7689 | Info.Ctx.getRecordType(DD->getParent())) && |
7690 | CallScope.destroy(); |
7691 | } |
7692 | |
7693 | const FunctionDecl *Definition = nullptr; |
7694 | Stmt *Body = FD->getBody(Definition); |
7695 | |
7696 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
7697 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Call, |
7698 | Body, Info, Result, ResultSlot)) |
7699 | return false; |
7700 | |
7701 | if (!CovariantAdjustmentPath.empty() && |
7702 | !HandleCovariantReturnAdjustment(Info, E, Result, |
7703 | CovariantAdjustmentPath)) |
7704 | return false; |
7705 | |
7706 | return CallScope.destroy(); |
7707 | } |
7708 | |
7709 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
7710 | return StmtVisitorTy::Visit(E->getInitializer()); |
7711 | } |
7712 | bool VisitInitListExpr(const InitListExpr *E) { |
7713 | if (E->getNumInits() == 0) |
7714 | return DerivedZeroInitialization(E); |
7715 | if (E->getNumInits() == 1) |
7716 | return StmtVisitorTy::Visit(E->getInit(0)); |
7717 | return Error(E); |
7718 | } |
7719 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
7720 | return DerivedZeroInitialization(E); |
7721 | } |
7722 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
7723 | return DerivedZeroInitialization(E); |
7724 | } |
7725 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
7726 | return DerivedZeroInitialization(E); |
7727 | } |
7728 | |
7729 | |
7730 | bool VisitMemberExpr(const MemberExpr *E) { |
7731 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
7732 | "missing temporary materialization conversion"); |
7733 | assert(!E->isArrow() && "missing call to bound member function?"); |
7734 | |
7735 | APValue Val; |
7736 | if (!Evaluate(Val, Info, E->getBase())) |
7737 | return false; |
7738 | |
7739 | QualType BaseTy = E->getBase()->getType(); |
7740 | |
7741 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
7742 | if (!FD) return Error(E); |
7743 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
7744 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
7745 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
7746 | |
7747 | |
7748 | |
7749 | |
7750 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
7751 | SubobjectDesignator Designator(BaseTy); |
7752 | Designator.addDeclUnchecked(FD); |
7753 | |
7754 | APValue Result; |
7755 | return extractSubobject(Info, E, Obj, Designator, Result) && |
7756 | DerivedSuccess(Result, E); |
7757 | } |
7758 | |
7759 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
7760 | APValue Val; |
7761 | if (!Evaluate(Val, Info, E->getBase())) |
7762 | return false; |
7763 | |
7764 | if (Val.isVector()) { |
7765 | SmallVector<uint32_t, 4> Indices; |
7766 | E->getEncodedElementAccess(Indices); |
7767 | if (Indices.size() == 1) { |
7768 | |
7769 | return DerivedSuccess(Val.getVectorElt(Indices[0]), E); |
7770 | } else { |
7771 | |
7772 | SmallVector<APValue, 4> Elts; |
7773 | for (unsigned I = 0; I < Indices.size(); ++I) { |
7774 | Elts.push_back(Val.getVectorElt(Indices[I])); |
7775 | } |
7776 | APValue VecResult(Elts.data(), Indices.size()); |
7777 | return DerivedSuccess(VecResult, E); |
7778 | } |
7779 | } |
7780 | |
7781 | return false; |
7782 | } |
7783 | |
7784 | bool VisitCastExpr(const CastExpr *E) { |
7785 | switch (E->getCastKind()) { |
7786 | default: |
7787 | break; |
7788 | |
7789 | case CK_AtomicToNonAtomic: { |
7790 | APValue AtomicVal; |
7791 | |
7792 | |
7793 | |
7794 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
7795 | return false; |
7796 | return DerivedSuccess(AtomicVal, E); |
7797 | } |
7798 | |
7799 | case CK_NoOp: |
7800 | case CK_UserDefinedConversion: |
7801 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7802 | |
7803 | case CK_LValueToRValue: { |
7804 | LValue LVal; |
7805 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
7806 | return false; |
7807 | APValue RVal; |
7808 | |
7809 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
7810 | LVal, RVal)) |
7811 | return false; |
7812 | return DerivedSuccess(RVal, E); |
7813 | } |
7814 | case CK_LValueToRValueBitCast: { |
7815 | APValue DestValue, SourceValue; |
7816 | if (!Evaluate(SourceValue, Info, E->getSubExpr())) |
7817 | return false; |
7818 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, E)) |
7819 | return false; |
7820 | return DerivedSuccess(DestValue, E); |
7821 | } |
7822 | |
7823 | case CK_AddressSpaceConversion: { |
7824 | APValue Value; |
7825 | if (!Evaluate(Value, Info, E->getSubExpr())) |
7826 | return false; |
7827 | return DerivedSuccess(Value, E); |
7828 | } |
7829 | } |
7830 | |
7831 | return Error(E); |
7832 | } |
7833 | |
7834 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
7835 | return VisitUnaryPostIncDec(UO); |
7836 | } |
7837 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
7838 | return VisitUnaryPostIncDec(UO); |
7839 | } |
7840 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
7841 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
7842 | return Error(UO); |
7843 | |
7844 | LValue LVal; |
7845 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
7846 | return false; |
7847 | APValue RVal; |
7848 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
7849 | UO->isIncrementOp(), &RVal)) |
7850 | return false; |
7851 | return DerivedSuccess(RVal, UO); |
7852 | } |
7853 | |
7854 | bool VisitStmtExpr(const StmtExpr *E) { |
7855 | |
7856 | |
7857 | llvm::SaveAndRestore<bool> NotCheckingForUB( |
7858 | Info.CheckingForUndefinedBehavior, false); |
7859 | |
7860 | const CompoundStmt *CS = E->getSubStmt(); |
7861 | if (CS->body_empty()) |
7862 | return true; |
7863 | |
7864 | BlockScopeRAII Scope(Info); |
7865 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
7866 | BE = CS->body_end(); |
7867 | ; ++BI) { |
7868 | if (BI + 1 == BE) { |
7869 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
7870 | if (!FinalExpr) { |
7871 | Info.FFDiag((*BI)->getBeginLoc(), |
7872 | diag::note_constexpr_stmt_expr_unsupported); |
7873 | return false; |
7874 | } |
7875 | return this->Visit(FinalExpr) && Scope.destroy(); |
7876 | } |
7877 | |
7878 | APValue ReturnValue; |
7879 | StmtResult Result = { ReturnValue, nullptr }; |
7880 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
7881 | if (ESR != ESR_Succeeded) { |
7882 | |
7883 | |
7884 | |
7885 | if (ESR != ESR_Failed) |
7886 | Info.FFDiag((*BI)->getBeginLoc(), |
7887 | diag::note_constexpr_stmt_expr_unsupported); |
7888 | return false; |
7889 | } |
7890 | } |
7891 | |
7892 | llvm_unreachable("Return from function from the loop above."); |
7893 | } |
7894 | |
7895 | |
7896 | void VisitIgnoredValue(const Expr *E) { |
7897 | EvaluateIgnoredValue(Info, E); |
7898 | } |
7899 | |
7900 | |
7901 | void VisitIgnoredBaseExpression(const Expr *E) { |
7902 | |
7903 | |
7904 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
7905 | return; |
7906 | VisitIgnoredValue(E); |
7907 | } |
7908 | }; |
7909 | |
7910 | } |
7911 | |
7912 | |
7913 | |
7914 | |
7915 | namespace { |
7916 | template<class Derived> |
7917 | class LValueExprEvaluatorBase |
7918 | : public ExprEvaluatorBase<Derived> { |
7919 | protected: |
7920 | LValue &Result; |
7921 | bool InvalidBaseOK; |
7922 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
7923 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
7924 | |
7925 | bool Success(APValue::LValueBase B) { |
7926 | Result.set(B); |
7927 | return true; |
7928 | } |
7929 | |
7930 | bool evaluatePointer(const Expr *E, LValue &Result) { |
7931 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
7932 | } |
7933 | |
7934 | public: |
7935 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
7936 | : ExprEvaluatorBaseTy(Info), Result(Result), |
7937 | InvalidBaseOK(InvalidBaseOK) {} |
7938 | |
7939 | bool Success(const APValue &V, const Expr *E) { |
7940 | Result.setFrom(this->Info.Ctx, V); |
7941 | return true; |
7942 | } |
7943 | |
7944 | bool VisitMemberExpr(const MemberExpr *E) { |
7945 | |
7946 | QualType BaseTy; |
7947 | bool EvalOK; |
7948 | if (E->isArrow()) { |
7949 | EvalOK = evaluatePointer(E->getBase(), Result); |
7950 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
7951 | } else if (E->getBase()->isPRValue()) { |
7952 | assert(E->getBase()->getType()->isRecordType()); |
7953 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
7954 | BaseTy = E->getBase()->getType(); |
7955 | } else { |
7956 | EvalOK = this->Visit(E->getBase()); |
7957 | BaseTy = E->getBase()->getType(); |
7958 | } |
7959 | if (!EvalOK) { |
7960 | if (!InvalidBaseOK) |
7961 | return false; |
7962 | Result.setInvalid(E); |
7963 | return true; |
7964 | } |
7965 | |
7966 | const ValueDecl *MD = E->getMemberDecl(); |
7967 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
7968 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
7969 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
7970 | (void)BaseTy; |
7971 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
7972 | return false; |
7973 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
7974 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
7975 | return false; |
7976 | } else |
7977 | return this->Error(E); |
7978 | |
7979 | if (MD->getType()->isReferenceType()) { |
7980 | APValue RefValue; |
7981 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
7982 | RefValue)) |
7983 | return false; |
7984 | return Success(RefValue, E); |
7985 | } |
7986 | return true; |
7987 | } |
7988 | |
7989 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7990 | switch (E->getOpcode()) { |
7991 | default: |
7992 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
7993 | |
7994 | case BO_PtrMemD: |
7995 | case BO_PtrMemI: |
7996 | return HandleMemberPointerAccess(this->Info, E, Result); |
7997 | } |
7998 | } |
7999 | |
8000 | bool VisitCastExpr(const CastExpr *E) { |
8001 | switch (E->getCastKind()) { |
8002 | default: |
8003 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8004 | |
8005 | case CK_DerivedToBase: |
8006 | case CK_UncheckedDerivedToBase: |
8007 | if (!this->Visit(E->getSubExpr())) |
8008 | return false; |
8009 | |
8010 | |
8011 | |
8012 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
8013 | Result); |
8014 | } |
8015 | } |
8016 | }; |
8017 | } |
8018 | |
8019 | |
8020 | |
8021 | |
8022 | |
8023 | |
8024 | |
8025 | |
8026 | |
8027 | |
8028 | |
8029 | |
8030 | |
8031 | |
8032 | |
8033 | |
8034 | |
8035 | |
8036 | |
8037 | |
8038 | |
8039 | |
8040 | |
8041 | |
8042 | |
8043 | |
8044 | |
8045 | |
8046 | |
8047 | |
8048 | |
8049 | |
8050 | |
8051 | |
8052 | namespace { |
8053 | class LValueExprEvaluator |
8054 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
8055 | public: |
8056 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
8057 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
8058 | |
8059 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
8060 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
8061 | |
8062 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
8063 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
8064 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
8065 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
8066 | bool VisitMemberExpr(const MemberExpr *E); |
8067 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
8068 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
8069 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
8070 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
8071 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
8072 | bool VisitUnaryDeref(const UnaryOperator *E); |
8073 | bool VisitUnaryReal(const UnaryOperator *E); |
8074 | bool VisitUnaryImag(const UnaryOperator *E); |
8075 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
8076 | return VisitUnaryPreIncDec(UO); |
8077 | } |
8078 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
8079 | return VisitUnaryPreIncDec(UO); |
8080 | } |
8081 | bool VisitBinAssign(const BinaryOperator *BO); |
8082 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
8083 | |
8084 | bool VisitCastExpr(const CastExpr *E) { |
8085 | switch (E->getCastKind()) { |
8086 | default: |
8087 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
8088 | |
8089 | case CK_LValueBitCast: |
8090 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8091 | if (!Visit(E->getSubExpr())) |
8092 | return false; |
8093 | Result.Designator.setInvalid(); |
8094 | return true; |
8095 | |
8096 | case CK_BaseToDerived: |
8097 | if (!Visit(E->getSubExpr())) |
8098 | return false; |
8099 | return HandleBaseToDerivedCast(Info, E, Result); |
8100 | |
8101 | case CK_Dynamic: |
8102 | if (!Visit(E->getSubExpr())) |
8103 | return false; |
8104 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
8105 | } |
8106 | } |
8107 | }; |
8108 | } |
8109 | |
8110 | |
8111 | |
8112 | |
8113 | |
8114 | |
8115 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
8116 | bool InvalidBaseOK) { |
8117 | assert(!E->isValueDependent()); |
8118 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
8119 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
8120 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8121 | } |
8122 | |
8123 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
8124 | const NamedDecl *D = E->getDecl(); |
8125 | if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl>(D)) |
8126 | return Success(cast<ValueDecl>(D)); |
8127 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
8128 | return VisitVarDecl(E, VD); |
8129 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(D)) |
8130 | return Visit(BD->getBinding()); |
8131 | return Error(E); |
8132 | } |
8133 | |
8134 | |
8135 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
8136 | |
8137 | |
8138 | |
8139 | |
8140 | |
8141 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
8142 | isa<DeclRefExpr>(E) && |
8143 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
8144 | |
8145 | |
8146 | |
8147 | |
8148 | if (Info.checkingPotentialConstantExpression()) |
8149 | return false; |
8150 | |
8151 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
8152 | |
8153 | Result = *Info.CurrentCall->This; |
8154 | |
8155 | |
8156 | if (!HandleLValueMember(Info, E, Result, FD)) |
8157 | return false; |
8158 | |
8159 | |
8160 | if (FD->getType()->isReferenceType()) { |
8161 | APValue RVal; |
8162 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
8163 | RVal)) |
8164 | return false; |
8165 | Result.setFrom(Info.Ctx, RVal); |
8166 | } |
8167 | return true; |
8168 | } |
8169 | } |
8170 | |
8171 | CallStackFrame *Frame = nullptr; |
8172 | unsigned Version = 0; |
8173 | if (VD->hasLocalStorage()) { |
8174 | |
8175 | |
8176 | |
8177 | |
8178 | |
8179 | |
8180 | CallStackFrame *CurrFrame = Info.CurrentCall; |
8181 | if (CurrFrame->Callee && CurrFrame->Callee->Equals(VD->getDeclContext())) { |
8182 | |
8183 | |
8184 | |
8185 | if (auto *PVD = dyn_cast<ParmVarDecl>(VD)) { |
8186 | if (CurrFrame->Arguments) { |
8187 | VD = CurrFrame->Arguments.getOrigParam(PVD); |
8188 | Frame = |
8189 | Info.getCallFrameAndDepth(CurrFrame->Arguments.CallIndex).first; |
8190 | Version = CurrFrame->Arguments.Version; |
8191 | } |
8192 | } else { |
8193 | Frame = CurrFrame; |
8194 | Version = CurrFrame->getCurrentTemporaryVersion(VD); |
8195 | } |
8196 | } |
8197 | } |
8198 | |
8199 | if (!VD->getType()->isReferenceType()) { |
8200 | if (Frame) { |
8201 | Result.set({VD, Frame->Index, Version}); |
8202 | return true; |
8203 | } |
8204 | return Success(VD); |
8205 | } |
8206 | |
8207 | if (!Info.getLangOpts().CPlusPlus11) { |
8208 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1) |
8209 | << VD << VD->getType(); |
8210 | Info.Note(VD->getLocation(), diag::note_declared_at); |
8211 | } |
8212 | |
8213 | APValue *V; |
8214 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, V)) |
8215 | return false; |
8216 | if (!V->hasValue()) { |
8217 | |
8218 | |
8219 | if (!Info.checkingPotentialConstantExpression()) |
8220 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
8221 | return false; |
8222 | } |
8223 | return Success(*V, E); |
8224 | } |
8225 | |
8226 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
8227 | const MaterializeTemporaryExpr *E) { |
8228 | |
8229 | SmallVector<const Expr *, 2> CommaLHSs; |
8230 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
8231 | const Expr *Inner = |
8232 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
8233 | |
8234 | |
8235 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
8236 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
8237 | return false; |
8238 | |
8239 | |
8240 | |
8241 | |
8242 | APValue *Value; |
8243 | if (E->getStorageDuration() == SD_Static) { |
8244 | |
8245 | Value = E->getOrCreateValue(true); |
8246 | *Value = APValue(); |
8247 | Result.set(E); |
8248 | } else { |
8249 | Value = &Info.CurrentCall->createTemporary( |
8250 | E, E->getType(), |
8251 | E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression |
8252 | : ScopeKind::Block, |
8253 | Result); |
8254 | } |
8255 | |
8256 | QualType Type = Inner->getType(); |
8257 | |
8258 | |
8259 | if (!EvaluateInPlace(*Value, Info, Result, Inner)) { |
8260 | *Value = APValue(); |
8261 | return false; |
8262 | } |
8263 | |
8264 | |
8265 | for (unsigned I = Adjustments.size(); I != 0; ) { |
8266 | --I; |
8267 | switch (Adjustments[I].Kind) { |
8268 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
8269 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
8270 | Type, Result)) |
8271 | return false; |
8272 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
8273 | break; |
8274 | |
8275 | case SubobjectAdjustment::FieldAdjustment: |
8276 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
8277 | return false; |
8278 | Type = Adjustments[I].Field->getType(); |
8279 | break; |
8280 | |
8281 | case SubobjectAdjustment::MemberPointerAdjustment: |
8282 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
8283 | Adjustments[I].Ptr.RHS)) |
8284 | return false; |
8285 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
8286 | break; |
8287 | } |
8288 | } |
8289 | |
8290 | return true; |
8291 | } |
8292 | |
8293 | bool |
8294 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8295 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
8296 | "lvalue compound literal in c++?"); |
8297 | |
8298 | |
8299 | return Success(E); |
8300 | } |
8301 | |
8302 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
8303 | TypeInfoLValue TypeInfo; |
8304 | |
8305 | if (!E->isPotentiallyEvaluated()) { |
8306 | if (E->isTypeOperand()) |
8307 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Info.Ctx).getTypePtr()); |
8308 | else |
8309 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
8310 | } else { |
8311 | if (!Info.Ctx.getLangOpts().CPlusPlus20) { |
8312 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
8313 | << E->getExprOperand()->getType() |
8314 | << E->getExprOperand()->getSourceRange(); |
8315 | } |
8316 | |
8317 | if (!Visit(E->getExprOperand())) |
8318 | return false; |
8319 | |
8320 | Optional<DynamicType> DynType = |
8321 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
8322 | if (!DynType) |
8323 | return false; |
8324 | |
8325 | TypeInfo = |
8326 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
8327 | } |
8328 | |
8329 | return Success(APValue::LValueBase::getTypeInfo(TypeInfo, E->getType())); |
8330 | } |
8331 | |
8332 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
8333 | return Success(E->getGuidDecl()); |
8334 | } |
8335 | |
8336 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
8337 | |
8338 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
8339 | VisitIgnoredBaseExpression(E->getBase()); |
8340 | return VisitVarDecl(E, VD); |
8341 | } |
8342 | |
8343 | |
8344 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
8345 | if (MD->isStatic()) { |
8346 | VisitIgnoredBaseExpression(E->getBase()); |
8347 | return Success(MD); |
8348 | } |
8349 | } |
8350 | |
8351 | |
8352 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
8353 | } |
8354 | |
8355 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
8356 | |
8357 | if (E->getBase()->getType()->isVectorType()) |
8358 | return Error(E); |
8359 | |
8360 | APSInt Index; |
8361 | bool Success = true; |
8362 | |
8363 | |
8364 | |
8365 | for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) { |
8366 | if (SubExpr == E->getBase() ? !evaluatePointer(SubExpr, Result) |
8367 | : !EvaluateInteger(SubExpr, Index, Info)) { |
8368 | if (!Info.noteFailure()) |
8369 | return false; |
8370 | Success = false; |
8371 | } |
8372 | } |
8373 | |
8374 | return Success && |
8375 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
8376 | } |
8377 | |
8378 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
8379 | return evaluatePointer(E->getSubExpr(), Result); |
8380 | } |
8381 | |
8382 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
8383 | if (!Visit(E->getSubExpr())) |
8384 | return false; |
8385 | |
8386 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
8387 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
8388 | return true; |
8389 | } |
8390 | |
8391 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
8392 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
8393 | "lvalue __imag__ on scalar?"); |
8394 | if (!Visit(E->getSubExpr())) |
8395 | return false; |
8396 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
8397 | return true; |
8398 | } |
8399 | |
8400 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
8401 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8402 | return Error(UO); |
8403 | |
8404 | if (!this->Visit(UO->getSubExpr())) |
8405 | return false; |
8406 | |
8407 | return handleIncDec( |
8408 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
8409 | UO->isIncrementOp(), nullptr); |
8410 | } |
8411 | |
8412 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
8413 | const CompoundAssignOperator *CAO) { |
8414 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8415 | return Error(CAO); |
8416 | |
8417 | bool Success = true; |
8418 | |
8419 | |
8420 | APValue RHS; |
8421 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) { |
8422 | if (!Info.noteFailure()) |
8423 | return false; |
8424 | Success = false; |
8425 | } |
8426 | |
8427 | |
8428 | if (!this->Visit(CAO->getLHS()) || !Success) |
8429 | return false; |
8430 | |
8431 | return handleCompoundAssignment( |
8432 | this->Info, CAO, |
8433 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
8434 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
8435 | } |
8436 | |
8437 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
8438 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8439 | return Error(E); |
8440 | |
8441 | bool Success = true; |
8442 | |
8443 | |
8444 | APValue NewVal; |
8445 | if (!Evaluate(NewVal, this->Info, E->getRHS())) { |
8446 | if (!Info.noteFailure()) |
8447 | return false; |
8448 | Success = false; |
8449 | } |
8450 | |
8451 | if (!this->Visit(E->getLHS()) || !Success) |
8452 | return false; |
8453 | |
8454 | if (Info.getLangOpts().CPlusPlus20 && |
8455 | !HandleUnionActiveMemberChange(Info, E->getLHS(), Result)) |
8456 | return false; |
8457 | |
8458 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
8459 | NewVal); |
8460 | } |
8461 | |
8462 | |
8463 | |
8464 | |
8465 | |
8466 | |
8467 | |
8468 | |
8469 | |
8470 | |
8471 | |
8472 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8473 | const CallExpr *Call, |
8474 | llvm::APInt &Result) { |
8475 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
8476 | |
8477 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
8478 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
8479 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
8480 | if (Call->getNumArgs() <= SizeArgNo) |
8481 | return false; |
8482 | |
8483 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
8484 | Expr::EvalResult ExprResult; |
8485 | if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects)) |
8486 | return false; |
8487 | Into = ExprResult.Val.getInt(); |
8488 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
8489 | return false; |
8490 | Into = Into.zextOrSelf(BitsInSizeT); |
8491 | return true; |
8492 | }; |
8493 | |
8494 | APSInt SizeOfElem; |
8495 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
8496 | return false; |
8497 | |
8498 | if (!AllocSize->getNumElemsParam().isValid()) { |
8499 | Result = std::move(SizeOfElem); |
8500 | return true; |
8501 | } |
8502 | |
8503 | APSInt NumberOfElems; |
8504 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
8505 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
8506 | return false; |
8507 | |
8508 | bool Overflow; |
8509 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
8510 | if (Overflow) |
8511 | return false; |
8512 | |
8513 | Result = std::move(BytesAvailable); |
8514 | return true; |
8515 | } |
8516 | |
8517 | |
8518 | |
8519 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8520 | const LValue &LVal, |
8521 | llvm::APInt &Result) { |
8522 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8523 | "Can't get the size of a non alloc_size function"); |
8524 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
8525 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
8526 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
8527 | } |
8528 | |
8529 | |
8530 | |
8531 | |
8532 | |
8533 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
8534 | LValue &Result) { |
8535 | if (Base.isNull()) |
8536 | return false; |
8537 | |
8538 | |
8539 | |
8540 | |
8541 | |
8542 | |
8543 | const auto *VD = |
8544 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
8545 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
8546 | return false; |
8547 | |
8548 | const Expr *Init = VD->getAnyInitializer(); |
8549 | if (!Init) |
8550 | return false; |
8551 | |
8552 | const Expr *E = Init->IgnoreParens(); |
8553 | if (!tryUnwrapAllocSizeCall(E)) |
8554 | return false; |
8555 | |
8556 | |
8557 | |
8558 | Result.setInvalid(E); |
8559 | |
8560 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
8561 | Result.addUnsizedArray(Info, E, Pointee); |
8562 | return true; |
8563 | } |
8564 | |
8565 | namespace { |
8566 | class PointerExprEvaluator |
8567 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
8568 | LValue &Result; |
8569 | bool InvalidBaseOK; |
8570 | |
8571 | bool Success(const Expr *E) { |
8572 | Result.set(E); |
8573 | return true; |
8574 | } |
8575 | |
8576 | bool evaluateLValue(const Expr *E, LValue &Result) { |
8577 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
8578 | } |
8579 | |
8580 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8581 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
8582 | } |
8583 | |
8584 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
8585 | public: |
8586 | |
8587 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
8588 | : ExprEvaluatorBaseTy(info), Result(Result), |
8589 | InvalidBaseOK(InvalidBaseOK) {} |
8590 | |
8591 | bool Success(const APValue &V, const Expr *E) { |
8592 | Result.setFrom(Info.Ctx, V); |
8593 | return true; |
8594 | } |
8595 | bool ZeroInitialization(const Expr *E) { |
8596 | Result.setNull(Info.Ctx, E->getType()); |
8597 | return true; |
8598 | } |
8599 | |
8600 | bool VisitBinaryOperator(const BinaryOperator *E); |
8601 | bool VisitCastExpr(const CastExpr* E); |
8602 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
8603 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
8604 | { return Success(E); } |
8605 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
8606 | if (E->isExpressibleAsConstantInitializer()) |
8607 | return Success(E); |
8608 | if (Info.noteFailure()) |
8609 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
8610 | return Error(E); |
8611 | } |
8612 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
8613 | { return Success(E); } |
8614 | bool VisitCallExpr(const CallExpr *E); |
8615 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
8616 | bool VisitBlockExpr(const BlockExpr *E) { |
8617 | if (!E->getBlockDecl()->hasCaptures()) |
8618 | return Success(E); |
8619 | return Error(E); |
8620 | } |
8621 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
8622 | |
8623 | if (Info.checkingPotentialConstantExpression()) |
8624 | return false; |
8625 | if (!Info.CurrentCall->This) { |
8626 | if (Info.getLangOpts().CPlusPlus11) |
8627 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
8628 | else |
8629 | Info.FFDiag(E); |
8630 | return false; |
8631 | } |
8632 | Result = *Info.CurrentCall->This; |
8633 | |
8634 | |
8635 | |
8636 | |
8637 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
8638 | |
8639 | |
8640 | if (!Info.CurrentCall->LambdaThisCaptureField) |
8641 | return false; |
8642 | |
8643 | |
8644 | |
8645 | if (!HandleLValueMember(Info, E, Result, |
8646 | Info.CurrentCall->LambdaThisCaptureField)) |
8647 | return false; |
8648 | |
8649 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
8650 | ->isPointerType()) { |
8651 | APValue RVal; |
8652 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
8653 | RVal)) |
8654 | return false; |
8655 | |
8656 | Result.setFrom(Info.Ctx, RVal); |
8657 | } |
8658 | } |
8659 | return true; |
8660 | } |
8661 | |
8662 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
8663 | |
8664 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
8665 | assert(E->isStringType() && "SourceLocExpr isn't a pointer type?"); |
8666 | APValue LValResult = E->EvaluateInContext( |
8667 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
8668 | Result.setFrom(Info.Ctx, LValResult); |
8669 | return true; |
8670 | } |
8671 | |
8672 | bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) { |
8673 | std::string ResultStr = E->ComputeName(Info.Ctx); |
8674 | |
8675 | Info.Ctx.SYCLUniqueStableNameEvaluatedValues[E] = ResultStr; |
8676 | |
8677 | QualType CharTy = Info.Ctx.CharTy.withConst(); |
8678 | APInt Size(Info.Ctx.getTypeSize(Info.Ctx.getSizeType()), |
8679 | ResultStr.size() + 1); |
8680 | QualType ArrayTy = Info.Ctx.getConstantArrayType(CharTy, Size, nullptr, |
8681 | ArrayType::Normal, 0); |
8682 | |
8683 | StringLiteral *SL = |
8684 | StringLiteral::Create(Info.Ctx, ResultStr, StringLiteral::Ascii, |
8685 | false, ArrayTy, E->getLocation()); |
8686 | |
8687 | evaluateLValue(SL, Result); |
8688 | Result.addArray(Info, E, cast<ConstantArrayType>(ArrayTy)); |
8689 | return true; |
8690 | } |
8691 | |
8692 | |
8693 | }; |
8694 | } |
8695 | |
8696 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
8697 | bool InvalidBaseOK) { |
8698 | assert(!E->isValueDependent()); |
8699 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
8700 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8701 | } |
8702 | |
8703 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
8704 | if (E->getOpcode() != BO_Add && |
8705 | E->getOpcode() != BO_Sub) |
8706 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
8707 | |
8708 | const Expr *PExp = E->getLHS(); |
8709 | const Expr *IExp = E->getRHS(); |
8710 | if (IExp->getType()->isPointerType()) |
8711 | std::swap(PExp, IExp); |
8712 | |
8713 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
8714 | if (!EvalPtrOK && !Info.noteFailure()) |
8715 | return false; |
8716 | |
8717 | llvm::APSInt Offset; |
8718 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
8719 | return false; |
8720 | |
8721 | if (E->getOpcode() == BO_Sub) |
8722 | negateAsSigned(Offset); |
8723 | |
8724 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
8725 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
8726 | } |
8727 | |
8728 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
8729 | return evaluateLValue(E->getSubExpr(), Result); |
8730 | } |
8731 | |
8732 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
8733 | const Expr *SubExpr = E->getSubExpr(); |
8734 | |
8735 | switch (E->getCastKind()) { |
8736 | default: |
8737 | break; |
8738 | case CK_BitCast: |
8739 | case CK_CPointerToObjCPointerCast: |
8740 | case CK_BlockPointerToObjCPointerCast: |
8741 | case CK_AnyPointerToBlockPointerCast: |
8742 | case CK_AddressSpaceConversion: |
8743 | if (!Visit(SubExpr)) |
8744 | return false; |
8745 | |
8746 | |
8747 | |
8748 | if (!E->getType()->isVoidPointerType()) { |
8749 | if (!Result.InvalidBase && !Result.Designator.Invalid && |
8750 | !Result.IsNullPtr && |
8751 | Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx), |
8752 | E->getType()->getPointeeType()) && |
8753 | Info.getStdAllocatorCaller("allocate")) { |
8754 | |
8755 | |
8756 | |
8757 | } else { |
8758 | Result.Designator.setInvalid(); |
8759 | if (SubExpr->getType()->isVoidPointerType()) |
8760 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
8761 | << 3 << SubExpr->getType(); |
8762 | else |
8763 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8764 | } |
8765 | } |
8766 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
8767 | ZeroInitialization(E); |
8768 | return true; |
8769 | |
8770 | case CK_DerivedToBase: |
8771 | case CK_UncheckedDerivedToBase: |
8772 | if (!evaluatePointer(E->getSubExpr(), Result)) |
8773 | return false; |
8774 | if (!Result.Base && Result.Offset.isZero()) |
8775 | return true; |
8776 | |
8777 | |
8778 | |
8779 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
8780 | castAs<PointerType>()->getPointeeType(), |
8781 | Result); |
8782 | |
8783 | case CK_BaseToDerived: |
8784 | if (!Visit(E->getSubExpr())) |
8785 | return false; |
8786 | if (!Result.Base && Result.Offset.isZero()) |
8787 | return true; |
8788 | return HandleBaseToDerivedCast(Info, E, Result); |
8789 | |
8790 | case CK_Dynamic: |
8791 | if (!Visit(E->getSubExpr())) |
8792 | return false; |
8793 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
8794 | |
8795 | case CK_NullToPointer: |
8796 | VisitIgnoredValue(E->getSubExpr()); |
8797 | return ZeroInitialization(E); |
8798 | |
8799 | case CK_IntegralToPointer: { |
8800 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8801 | |
8802 | APValue Value; |
8803 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
8804 | break; |
8805 | |
8806 | if (Value.isInt()) { |
8807 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
8808 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
8809 | Result.Base = (Expr*)nullptr; |
8810 | Result.InvalidBase = false; |
8811 | Result.Offset = CharUnits::fromQuantity(N); |
8812 | Result.Designator.setInvalid(); |
8813 | Result.IsNullPtr = false; |
8814 | return true; |
8815 | } else { |
8816 | |
8817 | Result.setFrom(Info.Ctx, Value); |
8818 | return true; |
8819 | } |
8820 | } |
8821 | |
8822 | case CK_ArrayToPointerDecay: { |
8823 | if (SubExpr->isGLValue()) { |
8824 | if (!evaluateLValue(SubExpr, Result)) |
8825 | return false; |
8826 | } else { |
8827 | APValue &Value = Info.CurrentCall->createTemporary( |
8828 | SubExpr, SubExpr->getType(), ScopeKind::FullExpression, Result); |
8829 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
8830 | return false; |
8831 | } |
8832 | |
8833 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
8834 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
8835 | Result.addArray(Info, E, CAT); |
8836 | else |
8837 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
8838 | return true; |
8839 | } |
8840 | |
8841 | case CK_FunctionToPointerDecay: |
8842 | return evaluateLValue(SubExpr, Result); |
8843 | |
8844 | case CK_LValueToRValue: { |
8845 | LValue LVal; |
8846 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
8847 | return false; |
8848 | |
8849 | APValue RVal; |
8850 | |
8851 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
8852 | LVal, RVal)) |
8853 | return InvalidBaseOK && |
8854 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
8855 | return Success(RVal, E); |
8856 | } |
8857 | } |
8858 | |
8859 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8860 | } |
8861 | |
8862 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
8863 | UnaryExprOrTypeTrait ExprKind) { |
8864 | |
8865 | |
8866 | |
8867 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
8868 | T = Ref->getPointeeType(); |
8869 | |
8870 | if (T.getQualifiers().hasUnaligned()) |
8871 | return CharUnits::One(); |
8872 | |
8873 | const bool AlignOfReturnsPreferred = |
8874 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
8875 | |
8876 | |
8877 | |
8878 | |
8879 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
8880 | return Info.Ctx.toCharUnitsFromBits( |
8881 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
8882 | |
8883 | else if (ExprKind == UETT_AlignOf) |
8884 | return Info.Ctx.getTypeAlignInChars(T.getTypePtr()); |
8885 | else |
8886 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
8887 | } |
8888 | |
8889 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
8890 | UnaryExprOrTypeTrait ExprKind) { |
8891 | E = E->IgnoreParens(); |
8892 | |
8893 | |
8894 | |
8895 | |
8896 | |
8897 | |
8898 | |
8899 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
8900 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
8901 | true); |
8902 | |
8903 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
8904 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
8905 | true); |
8906 | |
8907 | return GetAlignOfType(Info, E->getType(), ExprKind); |
8908 | } |
8909 | |
8910 | static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) { |
8911 | if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>()) |
8912 | return Info.Ctx.getDeclAlign(VD); |
8913 | if (const auto *E = Value.Base.dyn_cast<const Expr *>()) |
8914 | return GetAlignOfExpr(Info, E, UETT_AlignOf); |
8915 | return GetAlignOfType(Info, Value.Base.getTypeInfoType(), UETT_AlignOf); |
8916 | } |
8917 | |
8918 | |
8919 | |
8920 | static bool getAlignmentArgument(const Expr *E, QualType ForType, |
8921 | EvalInfo &Info, APSInt &Alignment) { |
8922 | if (!EvaluateInteger(E, Alignment, Info)) |
8923 | return false; |
8924 | if (Alignment < 0 || !Alignment.isPowerOf2()) { |
8925 | Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment; |
8926 | return false; |
8927 | } |
8928 | unsigned SrcWidth = Info.Ctx.getIntWidth(ForType); |
8929 | APSInt MaxValue(APInt::getOneBitSet(SrcWidth, SrcWidth - 1)); |
8930 | if (APSInt::compareValues(Alignment, MaxValue) > 0) { |
8931 | Info.FFDiag(E, diag::note_constexpr_alignment_too_big) |
8932 | << MaxValue << ForType << Alignment; |
8933 | return false; |
8934 | } |
8935 | |
8936 | |
8937 | APSInt ExtAlignment = |
8938 | APSInt(Alignment.zextOrTrunc(SrcWidth), true); |
8939 | assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 && |
8940 | "Alignment should not be changed by ext/trunc"); |
8941 | Alignment = ExtAlignment; |
8942 | assert(Alignment.getBitWidth() == SrcWidth); |
8943 | return true; |
8944 | } |
8945 | |
8946 | |
8947 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
8948 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
8949 | return true; |
8950 | |
8951 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
8952 | return false; |
8953 | |
8954 | Result.setInvalid(E); |
8955 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
8956 | Result.addUnsizedArray(Info, E, PointeeTy); |
8957 | return true; |
8958 | } |
8959 | |
8960 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
8961 | if (IsStringLiteralCall(E)) |
8962 | return Success(E); |
8963 | |
8964 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
8965 | return VisitBuiltinCallExpr(E, BuiltinOp); |
8966 | |
8967 | return visitNonBuiltinCallExpr(E); |
8968 | } |
8969 | |
8970 | |
8971 | |
8972 | static bool isOneByteCharacterType(QualType T) { |
8973 | return T->isCharType() || T->isChar8Type(); |
8974 | } |
8975 | |
8976 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
8977 | unsigned BuiltinOp) { |
8978 | switch (BuiltinOp) { |
8979 | case Builtin::BI__builtin_addressof: |
8980 | return evaluateLValue(E->getArg(0), Result); |
8981 | case Builtin::BI__builtin_assume_aligned: { |
8982 | |
8983 | |
8984 | |
8985 | if (!evaluatePointer(E->getArg(0), Result)) |
8986 | return false; |
8987 | |
8988 | LValue OffsetResult(Result); |
8989 | APSInt Alignment; |
8990 | if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info, |
8991 | Alignment)) |
8992 | return false; |
8993 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
8994 | |
8995 | if (E->getNumArgs() > 2) { |
8996 | APSInt Offset; |
8997 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
8998 | return false; |
8999 | |
9000 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
9001 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
9002 | } |
9003 | |
9004 | |
9005 | if (OffsetResult.Base) { |
9006 | CharUnits BaseAlignment = getBaseAlignment(Info, OffsetResult); |
9007 | |
9008 | if (BaseAlignment < Align) { |
9009 | Result.Designator.setInvalid(); |
9010 | |
9011 | CCEDiag(E->getArg(0), |
9012 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
9013 | << (unsigned)BaseAlignment.getQuantity() |
9014 | << (unsigned)Align.getQuantity(); |
9015 | return false; |
9016 | } |
9017 | } |
9018 | |
9019 | |
9020 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
9021 | Result.Designator.setInvalid(); |
9022 | |
9023 | (OffsetResult.Base |
9024 | ? CCEDiag(E->getArg(0), |
9025 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
9026 | : CCEDiag(E->getArg(0), |
9027 | diag::note_constexpr_baa_value_insufficient_alignment)) |
9028 | << (int)OffsetResult.Offset.getQuantity() |
9029 | << (unsigned)Align.getQuantity(); |
9030 | return false; |
9031 | } |
9032 | |
9033 | return true; |
9034 | } |
9035 | case Builtin::BI__builtin_align_up: |
9036 | case Builtin::BI__builtin_align_down: { |
9037 | if (!evaluatePointer(E->getArg(0), Result)) |
9038 | return false; |
9039 | APSInt Alignment; |
9040 | if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info, |
9041 | Alignment)) |
9042 | return false; |
9043 | CharUnits BaseAlignment = getBaseAlignment(Info, Result); |
9044 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Result.Offset); |
9045 | |
9046 | |
9047 | if (PtrAlign.getQuantity() >= Alignment) |
9048 | return true; |
9049 | |
9050 | |
9051 | |
9052 | |
9053 | |
9054 | |
9055 | if (BaseAlignment.getQuantity() >= Alignment) { |
9056 | assert(Alignment.getBitWidth() <= 64 && |
9057 | "Cannot handle > 64-bit address-space"); |
9058 | uint64_t Alignment64 = Alignment.getZExtValue(); |
9059 | CharUnits NewOffset = CharUnits::fromQuantity( |
9060 | BuiltinOp == Builtin::BI__builtin_align_down |
9061 | ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64) |
9062 | : llvm::alignTo(Result.Offset.getQuantity(), Alignment64)); |
9063 | Result.adjustOffset(NewOffset - Result.Offset); |
9064 | |
9065 | return true; |
9066 | } |
9067 | |
9068 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust) |
9069 | << Alignment; |
9070 | return false; |
9071 | } |
9072 | case Builtin::BI__builtin_operator_new: |
9073 | return HandleOperatorNewCall(Info, E, Result); |
9074 | case Builtin::BI__builtin_launder: |
9075 | return evaluatePointer(E->getArg(0), Result); |
9076 | case Builtin::BIstrchr: |
9077 | case Builtin::BIwcschr: |
9078 | case Builtin::BImemchr: |
9079 | case Builtin::BIwmemchr: |
9080 | if (Info.getLangOpts().CPlusPlus11) |
9081 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9082 | << 0 << 0 |
9083 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
9084 | else |
9085 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9086 | LLVM_FALLTHROUGH; |
9087 | case Builtin::BI__builtin_strchr: |
9088 | case Builtin::BI__builtin_wcschr: |
9089 | case Builtin::BI__builtin_memchr: |
9090 | case Builtin::BI__builtin_char_memchr: |
9091 | case Builtin::BI__builtin_wmemchr: { |
9092 | if (!Visit(E->getArg(0))) |
9093 | return false; |
9094 | APSInt Desired; |
9095 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
9096 | return false; |
9097 | uint64_t MaxLength = uint64_t(-1); |
9098 | if (BuiltinOp != Builtin::BIstrchr && |
9099 | BuiltinOp != Builtin::BIwcschr && |
9100 | BuiltinOp != Builtin::BI__builtin_strchr && |
9101 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
9102 | APSInt N; |
9103 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
9104 | return false; |
9105 | MaxLength = N.getExtValue(); |
9106 | } |
9107 | |
9108 | if (MaxLength == 0u) |
9109 | return ZeroInitialization(E); |
9110 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
9111 | Result.Designator.Invalid) |
9112 | return false; |
9113 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
9114 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
9115 | BuiltinOp == Builtin::BI__builtin_memchr; |
9116 | assert(IsRawByte || |
9117 | Info.Ctx.hasSameUnqualifiedType( |
9118 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
9119 | |
9120 | if (IsRawByte && CharTy->isIncompleteType()) { |
9121 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
9122 | return false; |
9123 | } |
9124 | |
9125 | |
9126 | if (IsRawByte && !isOneByteCharacterType(CharTy)) { |
9127 | Info.FFDiag(E, diag::note_constexpr_memchr_unsupported) |
9128 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'") |
9129 | << CharTy; |
9130 | return false; |
9131 | } |
9132 | |
9133 | |
9134 | uint64_t DesiredVal; |
9135 | bool StopAtNull = false; |
9136 | switch (BuiltinOp) { |
9137 | case Builtin::BIstrchr: |
9138 | case Builtin::BI__builtin_strchr: |
9139 | |
9140 | |
9141 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
9142 | E->getArg(1)->getType(), |
9143 | Desired), |
9144 | Desired)) |
9145 | return ZeroInitialization(E); |
9146 | StopAtNull = true; |
9147 | LLVM_FALLTHROUGH; |
9148 | case Builtin::BImemchr: |
9149 | case Builtin::BI__builtin_memchr: |
9150 | case Builtin::BI__builtin_char_memchr: |
9151 | |
9152 | |
9153 | |
9154 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
9155 | break; |
9156 | |
9157 | case Builtin::BIwcschr: |
9158 | case Builtin::BI__builtin_wcschr: |
9159 | StopAtNull = true; |
9160 | LLVM_FALLTHROUGH; |
9161 | case Builtin::BIwmemchr: |
9162 | case Builtin::BI__builtin_wmemchr: |
9163 | |
9164 | DesiredVal = Desired.getZExtValue(); |
9165 | break; |
9166 | } |
9167 | |
9168 | for (; MaxLength; --MaxLength) { |
9169 | APValue Char; |
9170 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
9171 | !Char.isInt()) |
9172 | return false; |
9173 | if (Char.getInt().getZExtValue() == DesiredVal) |
9174 | return true; |
9175 | if (StopAtNull && !Char.getInt()) |
9176 | break; |
9177 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
9178 | return false; |
9179 | } |
9180 | |
9181 | return ZeroInitialization(E); |
9182 | } |
9183 | |
9184 | case Builtin::BImemcpy: |
9185 | case Builtin::BImemmove: |
9186 | case Builtin::BIwmemcpy: |
9187 | case Builtin::BIwmemmove: |
9188 | if (Info.getLangOpts().CPlusPlus11) |
9189 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9190 | << 0 << 0 |
9191 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
9192 | else |
9193 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9194 | LLVM_FALLTHROUGH; |
9195 | case Builtin::BI__builtin_memcpy: |
9196 | case Builtin::BI__builtin_memmove: |
9197 | case Builtin::BI__builtin_wmemcpy: |
9198 | case Builtin::BI__builtin_wmemmove: { |
9199 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
9200 | BuiltinOp == Builtin::BIwmemmove || |
9201 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
9202 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9203 | bool Move = BuiltinOp == Builtin::BImemmove || |
9204 | BuiltinOp == Builtin::BIwmemmove || |
9205 | BuiltinOp == Builtin::BI__builtin_memmove || |
9206 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9207 | |
9208 | |
9209 | if (!Visit(E->getArg(0))) |
9210 | return false; |
9211 | LValue Dest = Result; |
9212 | |
9213 | LValue Src; |
9214 | if (!EvaluatePointer(E->getArg(1), Src, Info)) |
9215 | return false; |
9216 | |
9217 | APSInt N; |
9218 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
9219 | return false; |
9220 | assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
9221 | |
9222 | |
9223 | |
9224 | if (!N) |
9225 | return true; |
9226 | |
9227 | |
9228 | |
9229 | |
9230 | if (!Src.Base || !Dest.Base) { |
9231 | APValue Val; |
9232 | (!Src.Base ? Src : Dest).moveInto(Val); |
9233 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
9234 | << Move << WChar << !!Src.Base |
9235 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
9236 | return false; |
9237 | } |
9238 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
9239 | return false; |
9240 | |
9241 | |
9242 | |
9243 | |
9244 | QualType T = Dest.Designator.getType(Info.Ctx); |
9245 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
9246 | if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) { |
9247 | |
9248 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
9249 | return false; |
9250 | } |
9251 | if (T->isIncompleteType()) { |
9252 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
9253 | return false; |
9254 | } |
9255 | if (!T.isTriviallyCopyableType(Info.Ctx)) { |
9256 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
9257 | return false; |
9258 | } |
9259 | |
9260 | |
9261 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
9262 | if (!WChar) { |
9263 | uint64_t Remainder; |
9264 | llvm::APInt OrigN = N; |
9265 | llvm::APInt::udivrem(OrigN, TSize, N, Remainder); |
9266 | if (Remainder) { |
9267 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9268 | << Move << WChar << 0 << T << toString(OrigN, 10, false) |
9269 | << (unsigned)TSize; |
9270 | return false; |
9271 | } |
9272 | } |
9273 | |
9274 | |
9275 | |
9276 | |
9277 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
9278 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
9279 | if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { |
9280 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9281 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
9282 | << toString(N, 10, false); |
9283 | return false; |
9284 | } |
9285 | uint64_t NElems = N.getZExtValue(); |
9286 | uint64_t NBytes = NElems * TSize; |
9287 | |
9288 | |
9289 | int Direction = 1; |
9290 | if (HasSameBase(Src, Dest)) { |
9291 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
9292 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
9293 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
9294 | |
9295 | if (!Move) { |
9296 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9297 | return false; |
9298 | } |
9299 | |
9300 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
9301 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
9302 | return false; |
9303 | Direction = -1; |
9304 | } else if (!Move && SrcOffset >= DestOffset && |
9305 | SrcOffset - DestOffset < NBytes) { |
9306 | |
9307 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9308 | return false; |
9309 | } |
9310 | } |
9311 | |
9312 | while (true) { |
9313 | APValue Val; |
9314 | |
9315 | |
9316 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
9317 | !handleAssignment(Info, E, Dest, T, Val)) |
9318 | return false; |
9319 | |
9320 | |
9321 | if (--NElems == 0) |
9322 | return true; |
9323 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
9324 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
9325 | return false; |
9326 | } |
9327 | } |
9328 | |
9329 | default: |
9330 | break; |
9331 | } |
9332 | |
9333 | return visitNonBuiltinCallExpr(E); |
9334 | } |
9335 | |
9336 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
9337 | APValue &Result, const InitListExpr *ILE, |
9338 | QualType AllocType); |
9339 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
9340 | APValue &Result, |
9341 | const CXXConstructExpr *CCE, |
9342 | QualType AllocType); |
9343 | |
9344 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
9345 | if (!Info.getLangOpts().CPlusPlus20) |
9346 | Info.CCEDiag(E, diag::note_constexpr_new); |
9347 | |
9348 | |
9349 | if (Info.SpeculativeEvaluationDepth) |
9350 | return false; |
9351 | |
9352 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
9353 | |
9354 | bool IsNothrow = false; |
9355 | bool IsPlacement = false; |
9356 | if (OperatorNew->isReservedGlobalPlacementOperator() && |
9357 | Info.CurrentCall->isStdFunction() && !E->isArray()) { |
9358 | |
9359 | assert(E->getNumPlacementArgs() == 1); |
9360 | if (!EvaluatePointer(E->getPlacementArg(0), Result, Info)) |
9361 | return false; |
9362 | if (Result.Designator.Invalid) |
9363 | return false; |
9364 | IsPlacement = true; |
9365 | } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) { |
9366 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
9367 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
9368 | return false; |
9369 | } else if (E->getNumPlacementArgs()) { |
9370 | |
9371 | |
9372 | |
9373 | |
9374 | |
9375 | |
9376 | |
9377 | |
9378 | |
9379 | |
9380 | if (E->getNumPlacementArgs() != 1 || |
9381 | !E->getPlacementArg(0)->getType()->isNothrowT()) |
9382 | return Error(E, diag::note_constexpr_new_placement); |
9383 | |
9384 | LValue Nothrow; |
9385 | if (!EvaluateLValue(E->getPlacementArg(0), Nothrow, Info)) |
9386 | return false; |
9387 | IsNothrow = true; |
9388 | } |
9389 | |
9390 | const Expr *Init = E->getInitializer(); |
9391 | const InitListExpr *ResizedArrayILE = nullptr; |
9392 | const CXXConstructExpr *ResizedArrayCCE = nullptr; |
9393 | bool ValueInit = false; |
9394 | |
9395 | QualType AllocType = E->getAllocatedType(); |
9396 | if (Optional<const Expr*> ArraySize = E->getArraySize()) { |
9397 | const Expr *Stripped = *ArraySize; |
9398 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped); |
9399 | Stripped = ICE->getSubExpr()) |
9400 | if (ICE->getCastKind() != CK_NoOp && |
9401 | ICE->getCastKind() != CK_IntegralCast) |
9402 | break; |
9403 | |
9404 | llvm::APSInt ArrayBound; |
9405 | if (!EvaluateInteger(Stripped, ArrayBound, Info)) |
9406 | return false; |
9407 | |
9408 | |
9409 | |
9410 | |
9411 | |
9412 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
9413 | if (IsNothrow) |
9414 | return ZeroInitialization(E); |
9415 | |
9416 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
9417 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9418 | return false; |
9419 | } |
9420 | |
9421 | |
9422 | |
9423 | if (ConstantArrayType::getNumAddressingBits(Info.Ctx, AllocType, |
9424 | ArrayBound) > |
9425 | ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
9426 | if (IsNothrow) |
9427 | return ZeroInitialization(E); |
9428 | |
9429 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_large) |
9430 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9431 | return false; |
9432 | } |
9433 | |
9434 | |
9435 | |
9436 | |
9437 | if (!Init) { |
9438 | |
9439 | } else if (isa<CXXScalarValueInitExpr>(Init) || |
9440 | isa<ImplicitValueInitExpr>(Init)) { |
9441 | ValueInit = true; |
9442 | } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
9443 | ResizedArrayCCE = CCE; |
9444 | } else { |
9445 | auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType()); |
9446 | assert(CAT && "unexpected type for array initializer"); |
9447 | |
9448 | unsigned Bits = |
9449 | std::max(CAT->getSize().getBitWidth(), ArrayBound.getBitWidth()); |
9450 | llvm::APInt InitBound = CAT->getSize().zextOrSelf(Bits); |
9451 | llvm::APInt AllocBound = ArrayBound.zextOrSelf(Bits); |
9452 | if (InitBound.ugt(AllocBound)) { |
9453 | if (IsNothrow) |
9454 | return ZeroInitialization(E); |
9455 | |
9456 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
9457 | << toString(AllocBound, 10, false) |
9458 | << toString(InitBound, 10, false) |
9459 | << (*ArraySize)->getSourceRange(); |
9460 | return false; |
9461 | } |
9462 | |
9463 | |
9464 | |
9465 | if (InitBound != AllocBound) |
9466 | ResizedArrayILE = cast<InitListExpr>(Init); |
9467 | } |
9468 | |
9469 | AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr, |
9470 | ArrayType::Normal, 0); |
9471 | } else { |
9472 | assert(!AllocType->isArrayType() && |
9473 | "array allocation with non-array new"); |
9474 | } |
9475 | |
9476 | APValue *Val; |
9477 | if (IsPlacement) { |
9478 | AccessKinds AK = AK_Construct; |
9479 | struct FindObjectHandler { |
9480 | EvalInfo &Info; |
9481 | const Expr *E; |
9482 | QualType AllocType; |
9483 | const AccessKinds AccessKind; |
9484 | APValue *Value; |
9485 | |
9486 | typedef bool result_type; |
9487 | bool failed() { return false; } |
9488 | bool found(APValue &Subobj, QualType SubobjType) { |
9489 | |
9490 | |
9491 | if (!Info.Ctx.hasSameUnqualifiedType(SubobjType, AllocType)) { |
9492 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) << |
9493 | SubobjType << AllocType; |
9494 | return false; |
9495 | } |
9496 | Value = &Subobj; |
9497 | return true; |
9498 | } |
9499 | bool found(APSInt &Value, QualType SubobjType) { |
9500 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9501 | return false; |
9502 | } |
9503 | bool found(APFloat &Value, QualType SubobjType) { |
9504 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9505 | return false; |
9506 | } |
9507 | } Handler = {Info, E, AllocType, AK, nullptr}; |
9508 | |
9509 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
9510 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
9511 | return false; |
9512 | |
9513 | Val = Handler.Value; |
9514 | |
9515 | |
9516 | |
9517 | |
9518 | |
9519 | *Val = APValue(); |
9520 | } else { |
9521 | |
9522 | Val = Info.createHeapAlloc(E, AllocType, Result); |
9523 | if (!Val) |
9524 | return false; |
9525 | } |
9526 | |
9527 | if (ValueInit) { |
9528 | ImplicitValueInitExpr VIE(AllocType); |
9529 | if (!EvaluateInPlace(*Val, Info, Result, &VIE)) |
9530 | return false; |
9531 | } else if (ResizedArrayILE) { |
9532 | if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE, |
9533 | AllocType)) |
9534 | return false; |
9535 | } else if (ResizedArrayCCE) { |
9536 | if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE, |
9537 | AllocType)) |
9538 | return false; |
9539 | } else if (Init) { |
9540 | if (!EvaluateInPlace(*Val, Info, Result, Init)) |
9541 | return false; |
9542 | } else if (!getDefaultInitValue(AllocType, *Val)) { |
9543 | return false; |
9544 | } |
9545 | |
9546 | |
9547 | |
9548 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
9549 | Result.addArray(Info, E, cast<ConstantArrayType>(AT)); |
9550 | |
9551 | return true; |
9552 | } |
9553 | |
9554 | |
9555 | |
9556 | |
9557 | namespace { |
9558 | class MemberPointerExprEvaluator |
9559 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
9560 | MemberPtr &Result; |
9561 | |
9562 | bool Success(const ValueDecl *D) { |
9563 | Result = MemberPtr(D); |
9564 | return true; |
9565 | } |
9566 | public: |
9567 | |
9568 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
9569 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
9570 | |
9571 | bool Success(const APValue &V, const Expr *E) { |
9572 | Result.setFrom(V); |
9573 | return true; |
9574 | } |
9575 | bool ZeroInitialization(const Expr *E) { |
9576 | return Success((const ValueDecl*)nullptr); |
9577 | } |
9578 | |
9579 | bool VisitCastExpr(const CastExpr *E); |
9580 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
9581 | }; |
9582 | } |
9583 | |
9584 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
9585 | EvalInfo &Info) { |
9586 | assert(!E->isValueDependent()); |
9587 | assert(E->isPRValue() && E->getType()->isMemberPointerType()); |
9588 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
9589 | } |
9590 | |
9591 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9592 | switch (E->getCastKind()) { |
9593 | default: |
9594 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9595 | |
9596 | case CK_NullToMemberPointer: |
9597 | VisitIgnoredValue(E->getSubExpr()); |
9598 | return ZeroInitialization(E); |
9599 | |
9600 | case CK_BaseToDerivedMemberPointer: { |
9601 | if (!Visit(E->getSubExpr())) |
9602 | return false; |
9603 | if (E->path_empty()) |
9604 | return true; |
9605 | |
9606 | |
9607 | |
9608 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
9609 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
9610 | PathI != PathE; ++PathI) { |
9611 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
9612 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
9613 | if (!Result.castToDerived(Derived)) |
9614 | return Error(E); |
9615 | } |
9616 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
9617 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
9618 | return Error(E); |
9619 | return true; |
9620 | } |
9621 | |
9622 | case CK_DerivedToBaseMemberPointer: |
9623 | if (!Visit(E->getSubExpr())) |
9624 | return false; |
9625 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
9626 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
9627 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
9628 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
9629 | if (!Result.castToBase(Base)) |
9630 | return Error(E); |
9631 | } |
9632 | return true; |
9633 | } |
9634 | } |
9635 | |
9636 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
9637 | |
9638 | |
9639 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
9640 | } |
9641 | |
9642 | |
9643 | |
9644 | |
9645 | |
9646 | namespace { |
9647 | class RecordExprEvaluator |
9648 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
9649 | const LValue &This; |
9650 | APValue &Result; |
9651 | public: |
9652 | |
9653 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
9654 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
9655 | |
9656 | bool Success(const APValue &V, const Expr *E) { |
9657 | Result = V; |
9658 | return true; |
9659 | } |
9660 | bool ZeroInitialization(const Expr *E) { |
9661 | return ZeroInitialization(E, E->getType()); |
9662 | } |
9663 | bool ZeroInitialization(const Expr *E, QualType T); |
9664 | |
9665 | bool VisitCallExpr(const CallExpr *E) { |
9666 | return handleCallExpr(E, Result, &This); |
9667 | } |
9668 | bool VisitCastExpr(const CastExpr *E); |
9669 | bool VisitInitListExpr(const InitListExpr *E); |
9670 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
9671 | return VisitCXXConstructExpr(E, E->getType()); |
9672 | } |
9673 | bool VisitLambdaExpr(const LambdaExpr *E); |
9674 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
9675 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
9676 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
9677 | bool VisitBinCmp(const BinaryOperator *E); |
9678 | }; |
9679 | } |
9680 | |
9681 | |
9682 | |
9683 | |
9684 | |
9685 | |
9686 | |
9687 | |
9688 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
9689 | const RecordDecl *RD, |
9690 | const LValue &This, APValue &Result) { |
9691 | assert(!RD->isUnion() && "Expected non-union class type"); |
9692 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
9693 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
9694 | std::distance(RD->field_begin(), RD->field_end())); |
9695 | |
9696 | if (RD->isInvalidDecl()) return false; |
9697 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
9698 | |
9699 | if (CD) { |
9700 | unsigned Index = 0; |
9701 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
9702 | End = CD->bases_end(); I != End; ++I, ++Index) { |
9703 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
9704 | LValue Subobject = This; |
9705 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
9706 | return false; |
9707 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
9708 | Result.getStructBase(Index))) |
9709 | return false; |
9710 | } |
9711 | } |
9712 | |
9713 | for (const auto *I : RD->fields()) { |
9714 | |
9715 | if (I->isUnnamedBitfield() || I->getType()->isReferenceType()) |
9716 | continue; |
9717 | |
9718 | LValue Subobject = This; |
9719 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
9720 | return false; |
9721 | |
9722 | ImplicitValueInitExpr VIE(I->getType()); |
9723 | if (!EvaluateInPlace( |
9724 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
9725 | return false; |
9726 | } |
9727 | |
9728 | return true; |
9729 | } |
9730 | |
9731 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
9732 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
9733 | if (RD->isInvalidDecl()) return false; |
9734 | if (RD->isUnion()) { |
9735 | |
9736 | |
9737 | RecordDecl::field_iterator I = RD->field_begin(); |
9738 | while (I != RD->field_end() && (*I)->isUnnamedBitfield()) |
9739 | ++I; |
9740 | if (I == RD->field_end()) { |
9741 | Result = APValue((const FieldDecl*)nullptr); |
9742 | return true; |
9743 | } |
9744 | |
9745 | LValue Subobject = This; |
9746 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
9747 | return false; |
9748 | Result = APValue(*I); |
9749 | ImplicitValueInitExpr VIE(I->getType()); |
9750 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
9751 | } |
9752 | |
9753 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
9754 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
9755 | return false; |
9756 | } |
9757 | |
9758 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
9759 | } |
9760 | |
9761 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9762 | switch (E->getCastKind()) { |
9763 | default: |
9764 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9765 | |
9766 | case CK_ConstructorConversion: |
9767 | return Visit(E->getSubExpr()); |
9768 | |
9769 | case CK_DerivedToBase: |
9770 | case CK_UncheckedDerivedToBase: { |
9771 | APValue DerivedObject; |
9772 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
9773 | return false; |
9774 | if (!DerivedObject.isStruct()) |
9775 | return Error(E->getSubExpr()); |
9776 | |
9777 | |
9778 | APValue *Value = &DerivedObject; |
9779 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
9780 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
9781 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
9782 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
9783 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
9784 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
9785 | RD = Base; |
9786 | } |
9787 | Result = *Value; |
9788 | return true; |
9789 | } |
9790 | } |
9791 | } |
9792 | |
9793 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
9794 | if (E->isTransparent()) |
9795 | return Visit(E->getInit(0)); |
9796 | |
9797 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
9798 | if (RD->isInvalidDecl()) return false; |
9799 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
9800 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
9801 | |
9802 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
9803 | Info, |
9804 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
9805 | CXXRD && CXXRD->getNumBases()); |
9806 | |
9807 | if (RD->isUnion()) { |
9808 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
9809 | Result = APValue(Field); |
9810 | if (!Field) |
9811 | return true; |
9812 | |
9813 | |
9814 | |
9815 | |
9816 | |
9817 | |
9818 | ImplicitValueInitExpr VIE(Field->getType()); |
9819 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
9820 | |
9821 | LValue Subobject = This; |
9822 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
9823 | return false; |
9824 | |
9825 | |
9826 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
9827 | isa<CXXDefaultInitExpr>(InitExpr)); |
9828 | |
9829 | if (EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr)) { |
9830 | if (Field->isBitField()) |
9831 | return truncateBitfieldValue(Info, InitExpr, Result.getUnionValue(), |
9832 | Field); |
9833 | return true; |
9834 | } |
9835 | |
9836 | return false; |
9837 | } |
9838 | |
9839 | if (!Result.hasValue()) |
9840 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
9841 | std::distance(RD->field_begin(), RD->field_end())); |
9842 | unsigned ElementNo = 0; |
9843 | bool Success = true; |
9844 | |
9845 | |
9846 | if (CXXRD && CXXRD->getNumBases()) { |
9847 | for (const auto &Base : CXXRD->bases()) { |
9848 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
9849 | const Expr *Init = E->getInit(ElementNo); |
9850 | |
9851 | LValue Subobject = This; |
9852 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
9853 | return false; |
9854 | |
9855 | APValue &FieldVal = Result.getStructBase(ElementNo); |
9856 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
9857 | if (!Info.noteFailure()) |
9858 | return false; |
9859 | Success = false; |
9860 | } |
9861 | ++ElementNo; |
9862 | } |
9863 | |
9864 | EvalObj.finishedConstructingBases(); |
9865 | } |
9866 | |
9867 | |
9868 | for (const auto *Field : RD->fields()) { |
9869 | |
9870 | |
9871 | if (Field->isUnnamedBitfield()) |
9872 | continue; |
9873 | |
9874 | LValue Subobject = This; |
9875 | |
9876 | bool HaveInit = ElementNo < E->getNumInits(); |
9877 | |
9878 | |
9879 | |
9880 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
9881 | Subobject, Field, &Layout)) |
9882 | return false; |
9883 | |
9884 | |
9885 | |
9886 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
9887 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
9888 | |
9889 | |
9890 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
9891 | isa<CXXDefaultInitExpr>(Init)); |
9892 | |
9893 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
9894 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
9895 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
9896 | FieldVal, Field))) { |
9897 | if (!Info.noteFailure()) |
9898 | return false; |
9899 | Success = false; |
9900 | } |
9901 | } |
9902 | |
9903 | EvalObj.finishedConstructingFields(); |
9904 | |
9905 | return Success; |
9906 | } |
9907 | |
9908 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
9909 | QualType T) { |
9910 | |
9911 | |
9912 | const CXXConstructorDecl *FD = E->getConstructor(); |
9913 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
9914 | |
9915 | bool ZeroInit = E->requiresZeroInitialization(); |
9916 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
9917 | |
9918 | if (Result.hasValue()) |
9919 | return true; |
9920 | |
9921 | if (ZeroInit) |
9922 | return ZeroInitialization(E, T); |
9923 | |
9924 | return getDefaultInitValue(T, Result); |
9925 | } |
9926 | |
9927 | const FunctionDecl *Definition = nullptr; |
9928 | auto Body = FD->getBody(Definition); |
9929 | |
9930 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
9931 | return false; |
9932 | |
9933 | |
9934 | if (E->isElidable() && !ZeroInit) { |
9935 | |
9936 | |
9937 | |
9938 | |
9939 | |
9940 | const Expr *SrcObj = E->getArg(0); |
9941 | assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent())); |
9942 | assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType())); |
9943 | if (const MaterializeTemporaryExpr *ME = |
9944 | dyn_cast<MaterializeTemporaryExpr>(SrcObj)) |
9945 | return Visit(ME->getSubExpr()); |
9946 | } |
9947 | |
9948 | if (ZeroInit && !ZeroInitialization(E, T)) |
9949 | return false; |
9950 | |
9951 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
9952 | return HandleConstructorCall(E, This, Args, |
9953 | cast<CXXConstructorDecl>(Definition), Info, |
9954 | Result); |
9955 | } |
9956 | |
9957 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
9958 | const CXXInheritedCtorInitExpr *E) { |
9959 | if (!Info.CurrentCall) { |
9960 | assert(Info.checkingPotentialConstantExpression()); |
9961 | return false; |
9962 | } |
9963 | |
9964 | const CXXConstructorDecl *FD = E->getConstructor(); |
9965 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
9966 | return false; |
9967 | |
9968 | const FunctionDecl *Definition = nullptr; |
9969 | auto Body = FD->getBody(Definition); |
9970 | |
9971 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
9972 | return false; |
9973 | |
9974 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
9975 | cast<CXXConstructorDecl>(Definition), Info, |
9976 | Result); |
9977 | } |
9978 | |
9979 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
9980 | const CXXStdInitializerListExpr *E) { |
9981 | const ConstantArrayType *ArrayType = |
9982 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
9983 | |
9984 | LValue Array; |
9985 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
9986 | return false; |
9987 | |
9988 | |
9989 | Array.addArray(Info, E, ArrayType); |
9990 | |
9991 | auto InvalidType = [&] { |
9992 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
9993 | << E->getType(); |
9994 | return false; |
9995 | }; |
9996 | |
9997 | |
9998 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
9999 | RecordDecl::field_iterator Field = Record->field_begin(); |
10000 | if (Field == Record->field_end()) |
10001 | return InvalidType(); |
10002 | |
10003 | |
10004 | if (!Field->getType()->isPointerType() || |
10005 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10006 | ArrayType->getElementType())) |
10007 | return InvalidType(); |
10008 | |
10009 | |
10010 | Result = APValue(APValue::UninitStruct(), 0, 2); |
10011 | Array.moveInto(Result.getStructField(0)); |
10012 | |
10013 | if (++Field == Record->field_end()) |
10014 | return InvalidType(); |
10015 | |
10016 | if (Field->getType()->isPointerType() && |
10017 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10018 | ArrayType->getElementType())) { |
10019 | |
10020 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
10021 | ArrayType->getElementType(), |
10022 | ArrayType->getSize().getZExtValue())) |
10023 | return false; |
10024 | Array.moveInto(Result.getStructField(1)); |
10025 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
10026 | |
10027 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
10028 | else |
10029 | return InvalidType(); |
10030 | |
10031 | if (++Field != Record->field_end()) |
10032 | return InvalidType(); |
10033 | |
10034 | return true; |
10035 | } |
10036 | |
10037 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
10038 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
10039 | if (ClosureClass->isInvalidDecl()) |
10040 | return false; |
10041 | |
10042 | const size_t NumFields = |
10043 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
10044 | |
10045 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
10046 | E->capture_init_end()) && |
10047 | "The number of lambda capture initializers should equal the number of " |
10048 | "fields within the closure type"); |
10049 | |
10050 | Result = APValue(APValue::UninitStruct(), 0, NumFields); |
10051 | |
10052 | |
10053 | auto *CaptureInitIt = E->capture_init_begin(); |
10054 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
10055 | bool Success = true; |
10056 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass); |
10057 | for (const auto *Field : ClosureClass->fields()) { |
10058 | assert(CaptureInitIt != E->capture_init_end()); |
10059 | |
10060 | Expr *const CurFieldInit = *CaptureInitIt++; |
10061 | |
10062 | |
10063 | |
10064 | if (!CurFieldInit) |
10065 | return Error(E); |
10066 | |
10067 | LValue Subobject = This; |
10068 | |
10069 | if (!HandleLValueMember(Info, E, Subobject, Field, &Layout)) |
10070 | return false; |
10071 | |
10072 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
10073 | if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) { |
10074 | if (!Info.keepEvaluatingAfterFailure()) |
10075 | return false; |
10076 | Success = false; |
10077 | } |
10078 | ++CaptureIt; |
10079 | } |
10080 | return Success; |
10081 | } |
10082 | |
10083 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
10084 | APValue &Result, EvalInfo &Info) { |
10085 | assert(!E->isValueDependent()); |
10086 | assert(E->isPRValue() && E->getType()->isRecordType() && |
10087 | "can't evaluate expression as a record rvalue"); |
10088 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
10089 | } |
10090 | |
10091 | |
10092 | |
10093 | |
10094 | |
10095 | |
10096 | |
10097 | |
10098 | namespace { |
10099 | class TemporaryExprEvaluator |
10100 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
10101 | public: |
10102 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
10103 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
10104 | |
10105 | |
10106 | bool VisitConstructExpr(const Expr *E) { |
10107 | APValue &Value = Info.CurrentCall->createTemporary( |
10108 | E, E->getType(), ScopeKind::FullExpression, Result); |
10109 | return EvaluateInPlace(Value, Info, Result, E); |
10110 | } |
10111 | |
10112 | bool VisitCastExpr(const CastExpr *E) { |
10113 | switch (E->getCastKind()) { |
10114 | default: |
10115 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
10116 | |
10117 | case CK_ConstructorConversion: |
10118 | return VisitConstructExpr(E->getSubExpr()); |
10119 | } |
10120 | } |
10121 | bool VisitInitListExpr(const InitListExpr *E) { |
10122 | return VisitConstructExpr(E); |
10123 | } |
10124 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10125 | return VisitConstructExpr(E); |
10126 | } |
10127 | bool VisitCallExpr(const CallExpr *E) { |
10128 | return VisitConstructExpr(E); |
10129 | } |
10130 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
10131 | return VisitConstructExpr(E); |
10132 | } |
10133 | bool VisitLambdaExpr(const LambdaExpr *E) { |
10134 | return VisitConstructExpr(E); |
10135 | } |
10136 | }; |
10137 | } |
10138 | |
10139 | |
10140 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
10141 | assert(!E->isValueDependent()); |
10142 | assert(E->isPRValue() && E->getType()->isRecordType()); |
10143 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
10144 | } |
10145 | |
10146 | |
10147 | |
10148 | |
10149 | |
10150 | namespace { |
10151 | class VectorExprEvaluator |
10152 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
10153 | APValue &Result; |
10154 | public: |
10155 | |
10156 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
10157 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
10158 | |
10159 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
10160 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
10161 | |
10162 | Result = APValue(V.data(), V.size()); |
10163 | return true; |
10164 | } |
10165 | bool Success(const APValue &V, const Expr *E) { |
10166 | assert(V.isVector()); |
10167 | Result = V; |
10168 | return true; |
10169 | } |
10170 | bool ZeroInitialization(const Expr *E); |
10171 | |
10172 | bool VisitUnaryReal(const UnaryOperator *E) |
10173 | { return Visit(E->getSubExpr()); } |
10174 | bool VisitCastExpr(const CastExpr* E); |
10175 | bool VisitInitListExpr(const InitListExpr *E); |
10176 | bool VisitUnaryImag(const UnaryOperator *E); |
10177 | bool VisitBinaryOperator(const BinaryOperator *E); |
10178 | |
10179 | |
10180 | }; |
10181 | } |
10182 | |
10183 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
10184 | assert(E->isPRValue() && E->getType()->isVectorType() && |
10185 | "not a vector prvalue"); |
10186 | return VectorExprEvaluator(Info, Result).Visit(E); |
10187 | } |
10188 | |
10189 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10190 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
10191 | unsigned NElts = VTy->getNumElements(); |
10192 | |
10193 | const Expr *SE = E->getSubExpr(); |
10194 | QualType SETy = SE->getType(); |
10195 | |
10196 | switch (E->getCastKind()) { |
10197 | case CK_VectorSplat: { |
10198 | APValue Val = APValue(); |
10199 | if (SETy->isIntegerType()) { |
10200 | APSInt IntResult; |
10201 | if (!EvaluateInteger(SE, IntResult, Info)) |
10202 | return false; |
10203 | Val = APValue(std::move(IntResult)); |
10204 | } else if (SETy->isRealFloatingType()) { |
10205 | APFloat FloatResult(0.0); |
10206 | if (!EvaluateFloat(SE, FloatResult, Info)) |
10207 | return false; |
10208 | Val = APValue(std::move(FloatResult)); |
10209 | } else { |
10210 | return Error(E); |
10211 | } |
10212 | |
10213 | |
10214 | SmallVector<APValue, 4> Elts(NElts, Val); |
10215 | return Success(Elts, E); |
10216 | } |
10217 | case CK_BitCast: { |
10218 | |
10219 | llvm::APInt SValInt; |
10220 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
10221 | return false; |
10222 | |
10223 | QualType EltTy = VTy->getElementType(); |
10224 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
10225 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
10226 | SmallVector<APValue, 4> Elts; |
10227 | if (EltTy->isRealFloatingType()) { |
10228 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
10229 | unsigned FloatEltSize = EltSize; |
10230 | if (&Sem == &APFloat::x87DoubleExtended()) |
10231 | FloatEltSize = 80; |
10232 | for (unsigned i = 0; i < NElts; i++) { |
10233 | llvm::APInt Elt; |
10234 | if (BigEndian) |
10235 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
10236 | else |
10237 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
10238 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
10239 | } |
10240 | } else if (EltTy->isIntegerType()) { |
10241 | for (unsigned i = 0; i < NElts; i++) { |
10242 | llvm::APInt Elt; |
10243 | if (BigEndian) |
10244 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
10245 | else |
10246 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
10247 | Elts.push_back(APValue(APSInt(Elt, !EltTy->isSignedIntegerType()))); |
10248 | } |
10249 | } else { |
10250 | return Error(E); |
10251 | } |
10252 | return Success(Elts, E); |
10253 | } |
10254 | default: |
10255 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10256 | } |
10257 | } |
10258 | |
10259 | bool |
10260 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10261 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
10262 | unsigned NumInits = E->getNumInits(); |
10263 | unsigned NumElements = VT->getNumElements(); |
10264 | |
10265 | QualType EltTy = VT->getElementType(); |
10266 | SmallVector<APValue, 4> Elements; |
10267 | |
10268 | |
10269 | |
10270 | |
10271 | |
10272 | unsigned CountInits = 0, CountElts = 0; |
10273 | while (CountElts < NumElements) { |
10274 | |
10275 | if (CountInits < NumInits |
10276 | && E->getInit(CountInits)->getType()->isVectorType()) { |
10277 | APValue v; |
10278 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
10279 | return Error(E); |
10280 | unsigned vlen = v.getVectorLength(); |
10281 | for (unsigned j = 0; j < vlen; j++) |
10282 | Elements.push_back(v.getVectorElt(j)); |
10283 | CountElts += vlen; |
10284 | } else if (EltTy->isIntegerType()) { |
10285 | llvm::APSInt sInt(32); |
10286 | if (CountInits < NumInits) { |
10287 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
10288 | return false; |
10289 | } else |
10290 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
10291 | Elements.push_back(APValue(sInt)); |
10292 | CountElts++; |
10293 | } else { |
10294 | llvm::APFloat f(0.0); |
10295 | if (CountInits < NumInits) { |
10296 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
10297 | return false; |
10298 | } else |
10299 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
10300 | Elements.push_back(APValue(f)); |
10301 | CountElts++; |
10302 | } |
10303 | CountInits++; |
10304 | } |
10305 | return Success(Elements, E); |
10306 | } |
10307 | |
10308 | bool |
10309 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
10310 | const auto *VT = E->getType()->castAs<VectorType>(); |
10311 | QualType EltTy = VT->getElementType(); |
10312 | APValue ZeroElement; |
10313 | if (EltTy->isIntegerType()) |
10314 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
10315 | else |
10316 | ZeroElement = |
10317 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
10318 | |
10319 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
10320 | return Success(Elements, E); |
10321 | } |
10322 | |
10323 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
10324 | VisitIgnoredValue(E->getSubExpr()); |
10325 | return ZeroInitialization(E); |
10326 | } |
10327 | |
10328 | bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10329 | BinaryOperatorKind Op = E->getOpcode(); |
10330 | assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp && |
10331 | "Operation not supported on vector types"); |
10332 | |
10333 | if (Op == BO_Comma) |
10334 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10335 | |
10336 | Expr *LHS = E->getLHS(); |
10337 | Expr *RHS = E->getRHS(); |
10338 | |
10339 | assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() && |
10340 | "Must both be vector types"); |
10341 | |
10342 | |
10343 | assert(LHS->getType()->castAs<VectorType>()->getNumElements() == |
10344 | E->getType()->castAs<VectorType>()->getNumElements() && |
10345 | RHS->getType()->castAs<VectorType>()->getNumElements() == |
10346 | E->getType()->castAs<VectorType>()->getNumElements() && |
10347 | "All operands must be the same size."); |
10348 | |
10349 | APValue LHSValue; |
10350 | APValue RHSValue; |
10351 | bool LHSOK = Evaluate(LHSValue, Info, LHS); |
10352 | if (!LHSOK && !Info.noteFailure()) |
10353 | return false; |
10354 | if (!Evaluate(RHSValue, Info, RHS) || !LHSOK) |
10355 | return false; |
10356 | |
10357 | if (!handleVectorVectorBinOp(Info, E, Op, LHSValue, RHSValue)) |
10358 | return false; |
10359 | |
10360 | return Success(LHSValue, E); |
10361 | } |
10362 | |
10363 | |
10364 | |
10365 | |
10366 | |
10367 | namespace { |
10368 | class ArrayExprEvaluator |
10369 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
10370 | const LValue &This; |
10371 | APValue &Result; |
10372 | public: |
10373 | |
10374 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
10375 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
10376 | |
10377 | bool Success(const APValue &V, const Expr *E) { |
10378 | assert(V.isArray() && "expected array"); |
10379 | Result = V; |
10380 | return true; |
10381 | } |
10382 | |
10383 | bool ZeroInitialization(const Expr *E) { |
10384 | const ConstantArrayType *CAT = |
10385 | Info.Ctx.getAsConstantArrayType(E->getType()); |
10386 | if (!CAT) { |
10387 | if (E->getType()->isIncompleteArrayType()) { |
10388 | |
10389 | |
10390 | |
10391 | Result = APValue(APValue::UninitArray(), 0, 0); |
10392 | return true; |
10393 | } |
10394 | |
10395 | return Error(E); |
10396 | } |
10397 | |
10398 | Result = APValue(APValue::UninitArray(), 0, |
10399 | CAT->getSize().getZExtValue()); |
10400 | if (!Result.hasArrayFiller()) |
10401 | return true; |
10402 | |
10403 | |
10404 | LValue Subobject = This; |
10405 | Subobject.addArray(Info, E, CAT); |
10406 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
10407 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
10408 | } |
10409 | |
10410 | bool VisitCallExpr(const CallExpr *E) { |
10411 | return handleCallExpr(E, Result, &This); |
10412 | } |
10413 | bool VisitInitListExpr(const InitListExpr *E, |
10414 | QualType AllocType = QualType()); |
10415 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
10416 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
10417 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
10418 | const LValue &Subobject, |
10419 | APValue *Value, QualType Type); |
10420 | bool VisitStringLiteral(const StringLiteral *E, |
10421 | QualType AllocType = QualType()) { |
10422 | expandStringLiteral(Info, E, Result, AllocType); |
10423 | return true; |
10424 | } |
10425 | }; |
10426 | } |
10427 | |
10428 | static bool EvaluateArray(const Expr *E, const LValue &This, |
10429 | APValue &Result, EvalInfo &Info) { |
10430 | assert(!E->isValueDependent()); |
10431 | assert(E->isPRValue() && E->getType()->isArrayType() && |
10432 | "not an array prvalue"); |
10433 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
10434 | } |
10435 | |
10436 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
10437 | APValue &Result, const InitListExpr *ILE, |
10438 | QualType AllocType) { |
10439 | assert(!ILE->isValueDependent()); |
10440 | assert(ILE->isPRValue() && ILE->getType()->isArrayType() && |
10441 | "not an array prvalue"); |
10442 | return ArrayExprEvaluator(Info, This, Result) |
10443 | .VisitInitListExpr(ILE, AllocType); |
10444 | } |
10445 | |
10446 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
10447 | APValue &Result, |
10448 | const CXXConstructExpr *CCE, |
10449 | QualType AllocType) { |
10450 | assert(!CCE->isValueDependent()); |
10451 | assert(CCE->isPRValue() && CCE->getType()->isArrayType() && |
10452 | "not an array prvalue"); |
10453 | return ArrayExprEvaluator(Info, This, Result) |
10454 | .VisitCXXConstructExpr(CCE, This, &Result, AllocType); |
10455 | } |
10456 | |
10457 | |
10458 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
10459 | |
10460 | |
10461 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
10462 | return false; |
10463 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
10464 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
10465 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
10466 | return true; |
10467 | } |
10468 | return false; |
10469 | } |
10470 | return true; |
10471 | } |
10472 | |
10473 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
10474 | QualType AllocType) { |
10475 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
10476 | AllocType.isNull() ? E->getType() : AllocType); |
10477 | if (!CAT) |
10478 | return Error(E); |
10479 | |
10480 | |
10481 | |
10482 | if (E->isStringLiteralInit()) { |
10483 | auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParens()); |
10484 | |
10485 | |
10486 | if (!SL) |
10487 | return Error(E); |
10488 | return VisitStringLiteral(SL, AllocType); |
10489 | } |
10490 | |
10491 | bool Success = true; |
10492 | |
10493 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
10494 | "zero-initialized array shouldn't have any initialized elts"); |
10495 | APValue Filler; |
10496 | if (Result.isArray() && Result.hasArrayFiller()) |
10497 | Filler = Result.getArrayFiller(); |
10498 | |
10499 | unsigned NumEltsToInit = E->getNumInits(); |
10500 | unsigned NumElts = CAT->getSize().getZExtValue(); |
10501 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
10502 | |
10503 | |
10504 | |
10505 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
10506 | NumEltsToInit = NumElts; |
10507 | |
10508 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
10509 | << NumEltsToInit << ".\n"); |
10510 | |
10511 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
10512 | |
10513 | |
10514 | |
10515 | if (Filler.hasValue()) { |
10516 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
10517 | Result.getArrayInitializedElt(I) = Filler; |
10518 | if (Result.hasArrayFiller()) |
10519 | Result.getArrayFiller() = Filler; |
10520 | } |
10521 | |
10522 | LValue Subobject = This; |
10523 | Subobject.addArray(Info, E, CAT); |
10524 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
10525 | const Expr *Init = |
10526 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
10527 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
10528 | Info, Subobject, Init) || |
10529 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
10530 | CAT->getElementType(), 1)) { |
10531 | if (!Info.noteFailure()) |
10532 | return false; |
10533 | Success = false; |
10534 | } |
10535 | } |
10536 | |
10537 | if (!Result.hasArrayFiller()) |
10538 | return Success; |
10539 | |
10540 | |
10541 | |
10542 | assert(FillerExpr && "no array filler for incomplete init list"); |
10543 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
10544 | FillerExpr) && Success; |
10545 | } |
10546 | |
10547 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
10548 | LValue CommonLV; |
10549 | if (E->getCommonExpr() && |
10550 | !Evaluate(Info.CurrentCall->createTemporary( |
10551 | E->getCommonExpr(), |
10552 | getStorageType(Info.Ctx, E->getCommonExpr()), |
10553 | ScopeKind::FullExpression, CommonLV), |
10554 | Info, E->getCommonExpr()->getSourceExpr())) |
10555 | return false; |
10556 | |
10557 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
10558 | |
10559 | uint64_t Elements = CAT->getSize().getZExtValue(); |
10560 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
10561 | |
10562 | LValue Subobject = This; |
10563 | Subobject.addArray(Info, E, CAT); |
10564 | |
10565 | bool Success = true; |
10566 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
10567 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
10568 | Info, Subobject, E->getSubExpr()) || |
10569 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
10570 | CAT->getElementType(), 1)) { |
10571 | if (!Info.noteFailure()) |
10572 | return false; |
10573 | Success = false; |
10574 | } |
10575 | } |
10576 | |
10577 | return Success; |
10578 | } |
10579 | |
10580 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10581 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
10582 | } |
10583 | |
10584 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
10585 | const LValue &Subobject, |
10586 | APValue *Value, |
10587 | QualType Type) { |
10588 | bool HadZeroInit = Value->hasValue(); |
10589 | |
10590 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
10591 | unsigned N = CAT->getSize().getZExtValue(); |
10592 | |
10593 | |
10594 | APValue Filler = |
10595 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
10596 | : APValue(); |
10597 | |
10598 | *Value = APValue(APValue::UninitArray(), N, N); |
10599 | |
10600 | if (HadZeroInit) |
10601 | for (unsigned I = 0; I != N; ++I) |
10602 | Value->getArrayInitializedElt(I) = Filler; |
10603 | |
10604 | |
10605 | LValue ArrayElt = Subobject; |
10606 | ArrayElt.addArray(Info, E, CAT); |
10607 | for (unsigned I = 0; I != N; ++I) |
10608 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
10609 | CAT->getElementType()) || |
10610 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
10611 | CAT->getElementType(), 1)) |
10612 | return false; |
10613 | |
10614 | return true; |
10615 | } |
10616 | |
10617 | if (!Type->isRecordType()) |
10618 | return Error(E); |
10619 | |
10620 | return RecordExprEvaluator(Info, Subobject, *Value) |
10621 | .VisitCXXConstructExpr(E, Type); |
10622 | } |
10623 | |
10624 | |
10625 | |
10626 | |
10627 | |
10628 | |
10629 | |
10630 | |
10631 | |
10632 | namespace { |
10633 | class IntExprEvaluator |
10634 | : public ExprEvaluatorBase<IntExprEvaluator> { |
10635 | APValue &Result; |
10636 | public: |
10637 | IntExprEvaluator(EvalInfo &info, APValue &result) |
10638 | : ExprEvaluatorBaseTy(info), Result(result) {} |
10639 | |
10640 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
10641 | assert(E->getType()->isIntegralOrEnumerationType() && |
10642 | "Invalid evaluation result."); |
10643 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
10644 | "Invalid evaluation result."); |
10645 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10646 | "Invalid evaluation result."); |
10647 | Result = APValue(SI); |
10648 | return true; |
10649 | } |
10650 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
10651 | return Success(SI, E, Result); |
10652 | } |
10653 | |
10654 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
10655 | assert(E->getType()->isIntegralOrEnumerationType() && |
10656 | "Invalid evaluation result."); |
10657 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10658 | "Invalid evaluation result."); |
10659 | Result = APValue(APSInt(I)); |
10660 | Result.getInt().setIsUnsigned( |
10661 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
10662 | return true; |
10663 | } |
10664 | bool Success(const llvm::APInt &I, const Expr *E) { |
10665 | return Success(I, E, Result); |
10666 | } |
10667 | |
10668 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
10669 | assert(E->getType()->isIntegralOrEnumerationType() && |
10670 | "Invalid evaluation result."); |
10671 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
10672 | return true; |
10673 | } |
10674 | bool Success(uint64_t Value, const Expr *E) { |
10675 | return Success(Value, E, Result); |
10676 | } |
10677 | |
10678 | bool Success(CharUnits Size, const Expr *E) { |
10679 | return Success(Size.getQuantity(), E); |
10680 | } |
10681 | |
10682 | bool Success(const APValue &V, const Expr *E) { |
10683 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate()) { |
10684 | Result = V; |
10685 | return true; |
10686 | } |
10687 | return Success(V.getInt(), E); |
10688 | } |
10689 | |
10690 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
10691 | |
10692 | |
10693 | |
10694 | |
10695 | |
10696 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
10697 | return Success(E->getValue(), E); |
10698 | } |
10699 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
10700 | return Success(E->getValue(), E); |
10701 | } |
10702 | |
10703 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
10704 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
10705 | if (CheckReferencedDecl(E, E->getDecl())) |
10706 | return true; |
10707 | |
10708 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
10709 | } |
10710 | bool VisitMemberExpr(const MemberExpr *E) { |
10711 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
10712 | VisitIgnoredBaseExpression(E->getBase()); |
10713 | return true; |
10714 | } |
10715 | |
10716 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
10717 | } |
10718 | |
10719 | bool VisitCallExpr(const CallExpr *E); |
10720 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
10721 | bool VisitBinaryOperator(const BinaryOperator *E); |
10722 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
10723 | bool VisitUnaryOperator(const UnaryOperator *E); |
10724 | |
10725 | bool VisitCastExpr(const CastExpr* E); |
10726 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
10727 | |
10728 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
10729 | return Success(E->getValue(), E); |
10730 | } |
10731 | |
10732 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
10733 | return Success(E->getValue(), E); |
10734 | } |
10735 | |
10736 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
10737 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
10738 | |
10739 | |
10740 | Info.FFDiag(E); |
10741 | return false; |
10742 | } |
10743 | return Success(Info.ArrayInitIndex, E); |
10744 | } |
10745 | |
10746 | |
10747 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
10748 | return ZeroInitialization(E); |
10749 | } |
10750 | |
10751 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
10752 | return Success(E->getValue(), E); |
10753 | } |
10754 | |
10755 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
10756 | return Success(E->getValue(), E); |
10757 | } |
10758 | |
10759 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
10760 | return Success(E->getValue(), E); |
10761 | } |
10762 | |
10763 | bool VisitUnaryReal(const UnaryOperator *E); |
10764 | bool VisitUnaryImag(const UnaryOperator *E); |
10765 | |
10766 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
10767 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
10768 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
10769 | bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E); |
10770 | bool VisitRequiresExpr(const RequiresExpr *E); |
10771 | |
10772 | }; |
10773 | |
10774 | class FixedPointExprEvaluator |
10775 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
10776 | APValue &Result; |
10777 | |
10778 | public: |
10779 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
10780 | : ExprEvaluatorBaseTy(info), Result(result) {} |
10781 | |
10782 | bool Success(const llvm::APInt &I, const Expr *E) { |
10783 | return Success( |
10784 | APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
10785 | } |
10786 | |
10787 | bool Success(uint64_t Value, const Expr *E) { |
10788 | return Success( |
10789 | APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
10790 | } |
10791 | |
10792 | bool Success(const APValue &V, const Expr *E) { |
10793 | return Success(V.getFixedPoint(), E); |
10794 | } |
10795 | |
10796 | bool Success(const APFixedPoint &V, const Expr *E) { |
10797 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
10798 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10799 | "Invalid evaluation result."); |
10800 | Result = APValue(V); |
10801 | return true; |
10802 | } |
10803 | |
10804 | |
10805 | |
10806 | |
10807 | |
10808 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
10809 | return Success(E->getValue(), E); |
10810 | } |
10811 | |
10812 | bool VisitCastExpr(const CastExpr *E); |
10813 | bool VisitUnaryOperator(const UnaryOperator *E); |
10814 | bool VisitBinaryOperator(const BinaryOperator *E); |
10815 | }; |
10816 | } |
10817 | |
10818 | |
10819 | |
10820 | |
10821 | |
10822 | |
10823 | |
10824 | |
10825 | |
10826 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
10827 | EvalInfo &Info) { |
10828 | assert(!E->isValueDependent()); |
10829 | assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType()); |
10830 | return IntExprEvaluator(Info, Result).Visit(E); |
10831 | } |
10832 | |
10833 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
10834 | assert(!E->isValueDependent()); |
10835 | APValue Val; |
10836 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
10837 | return false; |
10838 | if (!Val.isInt()) { |
10839 | |
10840 | |
10841 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
10842 | return false; |
10843 | } |
10844 | Result = Val.getInt(); |
10845 | return true; |
10846 | } |
10847 | |
10848 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
10849 | APValue Evaluated = E->EvaluateInContext( |
10850 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
10851 | return Success(Evaluated, E); |
10852 | } |
10853 | |
10854 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
10855 | EvalInfo &Info) { |
10856 | assert(!E->isValueDependent()); |
10857 | if (E->getType()->isFixedPointType()) { |
10858 | APValue Val; |
10859 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
10860 | return false; |
10861 | if (!Val.isFixedPoint()) |
10862 | return false; |
10863 | |
10864 | Result = Val.getFixedPoint(); |
10865 | return true; |
10866 | } |
10867 | return false; |
10868 | } |
10869 | |
10870 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
10871 | EvalInfo &Info) { |
10872 | assert(!E->isValueDependent()); |
10873 | if (E->getType()->isIntegerType()) { |
10874 | auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType()); |
10875 | APSInt Val; |
10876 | if (!EvaluateInteger(E, Val, Info)) |
10877 | return false; |
10878 | Result = APFixedPoint(Val, FXSema); |
10879 | return true; |
10880 | } else if (E->getType()->isFixedPointType()) { |
10881 | return EvaluateFixedPoint(E, Result, Info); |
10882 | } |
10883 | return false; |
10884 | } |
10885 | |
10886 | |
10887 | |
10888 | |
10889 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
10890 | |
10891 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
10892 | |
10893 | bool SameSign = (ECD->getInitVal().isSigned() |
10894 | == E->getType()->isSignedIntegerOrEnumerationType()); |
10895 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
10896 | == Info.Ctx.getIntWidth(E->getType())); |
10897 | if (SameSign && SameWidth) |
10898 | return Success(ECD->getInitVal(), E); |
10899 | else { |
10900 | |
10901 | |
10902 | llvm::APSInt Val = ECD->getInitVal(); |
10903 | if (!SameSign) |
10904 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
10905 | if (!SameWidth) |
10906 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
10907 | return Success(Val, E); |
10908 | } |
10909 | } |
10910 | return false; |
10911 | } |
10912 | |
10913 | |
10914 | |
10915 | enum class GCCTypeClass { |
10916 | None = -1, |
10917 | Void = 0, |
10918 | Integer = 1, |
10919 | |
10920 | |
10921 | Enum = 3, |
10922 | Bool = 4, |
10923 | Pointer = 5, |
10924 | |
10925 | |
10926 | PointerToDataMember = 7, |
10927 | RealFloat = 8, |
10928 | Complex = 9, |
10929 | |
10930 | |
10931 | |
10932 | |
10933 | |
10934 | PointerToMemberFunction = 12, |
10935 | ClassOrStruct = 12, |
10936 | Union = 13, |
10937 | |
10938 | |
10939 | |
10940 | |
10941 | }; |
10942 | |
10943 | |
10944 | |
10945 | static GCCTypeClass |
10946 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
10947 | assert(!T->isDependentType() && "unexpected dependent type"); |
10948 | |
10949 | QualType CanTy = T.getCanonicalType(); |
10950 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
10951 | |
10952 | switch (CanTy->getTypeClass()) { |
10953 | #define TYPE(ID, BASE) |
10954 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
10955 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
10956 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
10957 | #include "clang/AST/TypeNodes.inc" |
10958 | case Type::Auto: |
10959 | case Type::DeducedTemplateSpecialization: |
10960 | llvm_unreachable("unexpected non-canonical or dependent type"); |
10961 | |
10962 | case Type::Builtin: |
10963 | switch (BT->getKind()) { |
10964 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
10965 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
10966 | case BuiltinType::ID: return GCCTypeClass::Integer; |
10967 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
10968 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
10969 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
10970 | case BuiltinType::ID: break; |
10971 | #include "clang/AST/BuiltinTypes.def" |
10972 | case BuiltinType::Void: |
10973 | return GCCTypeClass::Void; |
10974 | |
10975 | case BuiltinType::Bool: |
10976 | return GCCTypeClass::Bool; |
10977 | |
10978 | case BuiltinType::Char_U: |
10979 | case BuiltinType::UChar: |
10980 | case BuiltinType::WChar_U: |
10981 | case BuiltinType::Char8: |
10982 | case BuiltinType::Char16: |
10983 | case BuiltinType::Char32: |
10984 | case BuiltinType::UShort: |
10985 | case BuiltinType::UInt: |
10986 | case BuiltinType::ULong: |
10987 | case BuiltinType::ULongLong: |
10988 | case BuiltinType::UInt128: |
10989 | return GCCTypeClass::Integer; |
10990 | |
10991 | case BuiltinType::UShortAccum: |
10992 | case BuiltinType::UAccum: |
10993 | case BuiltinType::ULongAccum: |
10994 | case BuiltinType::UShortFract: |
10995 | case BuiltinType::UFract: |
10996 | case BuiltinType::ULongFract: |
10997 | case BuiltinType::SatUShortAccum: |
10998 | case BuiltinType::SatUAccum: |
10999 | case BuiltinType::SatULongAccum: |
11000 | case BuiltinType::SatUShortFract: |
11001 | case BuiltinType::SatUFract: |
11002 | case BuiltinType::SatULongFract: |
11003 | return GCCTypeClass::None; |
11004 | |
11005 | case BuiltinType::NullPtr: |
11006 | |
11007 | case BuiltinType::ObjCId: |
11008 | case BuiltinType::ObjCClass: |
11009 | case BuiltinType::ObjCSel: |
11010 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
11011 | case BuiltinType::Id: |
11012 | #include "clang/Basic/OpenCLImageTypes.def" |
11013 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
11014 | case BuiltinType::Id: |
11015 | #include "clang/Basic/OpenCLExtensionTypes.def" |
11016 | case BuiltinType::OCLSampler: |
11017 | case BuiltinType::OCLEvent: |
11018 | case BuiltinType::OCLClkEvent: |
11019 | case BuiltinType::OCLQueue: |
11020 | case BuiltinType::OCLReserveID: |
11021 | #define SVE_TYPE(Name, Id, SingletonId) \ |
11022 | case BuiltinType::Id: |
11023 | #include "clang/Basic/AArch64SVEACLETypes.def" |
11024 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
11025 | case BuiltinType::Id: |
11026 | #include "clang/Basic/PPCTypes.def" |
11027 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11028 | #include "clang/Basic/RISCVVTypes.def" |
11029 | return GCCTypeClass::None; |
11030 | |
11031 | case BuiltinType::Dependent: |
11032 | llvm_unreachable("unexpected dependent type"); |
11033 | }; |
11034 | llvm_unreachable("unexpected placeholder type"); |
11035 | |
11036 | case Type::Enum: |
11037 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
11038 | |
11039 | case Type::Pointer: |
11040 | case Type::ConstantArray: |
11041 | case Type::VariableArray: |
11042 | case Type::IncompleteArray: |
11043 | case Type::FunctionNoProto: |
11044 | case Type::FunctionProto: |
11045 | return GCCTypeClass::Pointer; |
11046 | |
11047 | case Type::MemberPointer: |
11048 | return CanTy->isMemberDataPointerType() |
11049 | ? GCCTypeClass::PointerToDataMember |
11050 | : GCCTypeClass::PointerToMemberFunction; |
11051 | |
11052 | case Type::Complex: |
11053 | return GCCTypeClass::Complex; |
11054 | |
11055 | case Type::Record: |
11056 | return CanTy->isUnionType() ? GCCTypeClass::Union |
11057 | : GCCTypeClass::ClassOrStruct; |
11058 | |
11059 | case Type::Atomic: |
11060 | |
11061 | return EvaluateBuiltinClassifyType( |
11062 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
11063 | |
11064 | case Type::BlockPointer: |
11065 | case Type::Vector: |
11066 | case Type::ExtVector: |
11067 | case Type::ConstantMatrix: |
11068 | case Type::ObjCObject: |
11069 | case Type::ObjCInterface: |
11070 | case Type::ObjCObjectPointer: |
11071 | case Type::Pipe: |
11072 | case Type::ExtInt: |
11073 | |
11074 | |
11075 | return GCCTypeClass::None; |
11076 | |
11077 | case Type::LValueReference: |
11078 | case Type::RValueReference: |
11079 | llvm_unreachable("invalid type for expression"); |
11080 | } |
11081 | |
11082 | llvm_unreachable("unexpected type class"); |
11083 | } |
11084 | |
11085 | |
11086 | |
11087 | static GCCTypeClass |
11088 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
11089 | |
11090 | |
11091 | if (E->getNumArgs() == 0) |
11092 | return GCCTypeClass::None; |
11093 | |
11094 | |
11095 | |
11096 | |
11097 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
11098 | } |
11099 | |
11100 | |
11101 | |
11102 | |
11103 | |
11104 | |
11105 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
11106 | APValue::LValueBase Base = LV.getLValueBase(); |
11107 | if (Base.isNull()) { |
11108 | |
11109 | return true; |
11110 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
11111 | if (!isa<StringLiteral>(E)) |
11112 | return false; |
11113 | return LV.getLValueOffset().isZero(); |
11114 | } else if (Base.is<TypeInfoLValue>()) { |
11115 | |
11116 | |
11117 | return true; |
11118 | } else { |
11119 | |
11120 | return false; |
11121 | } |
11122 | } |
11123 | |
11124 | |
11125 | |
11126 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
11127 | |
11128 | |
11129 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11130 | |
11131 | |
11132 | |
11133 | |
11134 | FoldConstant Fold(Info, true); |
11135 | |
11136 | QualType ArgType = Arg->getType(); |
11137 | |
11138 | |
11139 | |
11140 | |
11141 | |
11142 | |
11143 | |
11144 | |
11145 | |
11146 | |
11147 | |
11148 | |
11149 | |
11150 | |
11151 | |
11152 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
11153 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
11154 | ArgType->isNullPtrType()) { |
11155 | APValue V; |
11156 | if (!::EvaluateAsRValue(Info, Arg, V) || Info.EvalStatus.HasSideEffects) { |
11157 | Fold.keepDiagnostics(); |
11158 | return false; |
11159 | } |
11160 | |
11161 | |
11162 | if (V.getKind() == APValue::LValue) |
11163 | return EvaluateBuiltinConstantPForLValue(V); |
11164 | |
11165 | |
11166 | return V.hasValue(); |
11167 | } |
11168 | |
11169 | |
11170 | return false; |
11171 | } |
11172 | |
11173 | |
11174 | |
11175 | static QualType getObjectType(APValue::LValueBase B) { |
11176 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
11177 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
11178 | return VD->getType(); |
11179 | } else if (const Expr *E = B.dyn_cast<const Expr*>()) { |
11180 | if (isa<CompoundLiteralExpr>(E)) |
11181 | return E->getType(); |
11182 | } else if (B.is<TypeInfoLValue>()) { |
11183 | return B.getTypeInfoType(); |
11184 | } else if (B.is<DynamicAllocLValue>()) { |
11185 | return B.getDynamicAllocType(); |
11186 | } |
11187 | |
11188 | return QualType(); |
11189 | } |
11190 | |
11191 | |
11192 | |
11193 | |
11194 | |
11195 | |
11196 | |
11197 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
11198 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
11199 | |
11200 | auto *NoParens = E->IgnoreParens(); |
11201 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
11202 | if (Cast == nullptr) |
11203 | return NoParens; |
11204 | |
11205 | |
11206 | |
11207 | auto CastKind = Cast->getCastKind(); |
11208 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
11209 | CastKind != CK_AddressSpaceConversion) |
11210 | return NoParens; |
11211 | |
11212 | auto *SubExpr = Cast->getSubExpr(); |
11213 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue()) |
11214 | return NoParens; |
11215 | return ignorePointerCastsAndParens(SubExpr); |
11216 | } |
11217 | |
11218 | |
11219 | |
11220 | |
11221 | |
11222 | |
11223 | |
11224 | |
11225 | |
11226 | |
11227 | |
11228 | |
11229 | |
11230 | |
11231 | |
11232 | |
11233 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
11234 | assert(!LVal.Designator.Invalid); |
11235 | |
11236 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
11237 | const RecordDecl *Parent = FD->getParent(); |
11238 | Invalid = Parent->isInvalidDecl(); |
11239 | if (Invalid || Parent->isUnion()) |
11240 | return true; |
11241 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
11242 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
11243 | }; |
11244 | |
11245 | auto &Base = LVal.getLValueBase(); |
11246 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
11247 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
11248 | bool Invalid; |
11249 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11250 | return Invalid; |
11251 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
11252 | for (auto *FD : IFD->chain()) { |
11253 | bool Invalid; |
11254 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
11255 | return Invalid; |
11256 | } |
11257 | } |
11258 | } |
11259 | |
11260 | unsigned I = 0; |
11261 | QualType BaseType = getType(Base); |
11262 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
11263 | |
11264 | |
11265 | ++I; |
11266 | if (BaseType->isIncompleteArrayType()) |
11267 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
11268 | else |
11269 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
11270 | } |
11271 | |
11272 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
11273 | const auto &Entry = LVal.Designator.Entries[I]; |
11274 | if (BaseType->isArrayType()) { |
11275 | |
11276 | |
11277 | if (I + 1 == E) |
11278 | return true; |
11279 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
11280 | uint64_t Index = Entry.getAsArrayIndex(); |
11281 | if (Index + 1 != CAT->getSize()) |
11282 | return false; |
11283 | BaseType = CAT->getElementType(); |
11284 | } else if (BaseType->isAnyComplexType()) { |
11285 | const auto *CT = BaseType->castAs<ComplexType>(); |
11286 | uint64_t Index = Entry.getAsArrayIndex(); |
11287 | if (Index != 1) |
11288 | return false; |
11289 | BaseType = CT->getElementType(); |
11290 | } else if (auto *FD = getAsField(Entry)) { |
11291 | bool Invalid; |
11292 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11293 | return Invalid; |
11294 | BaseType = FD->getType(); |
11295 | } else { |
11296 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
11297 | return false; |
11298 | } |
11299 | } |
11300 | return true; |
11301 | } |
11302 | |
11303 | |
11304 | |
11305 | |
11306 | |
11307 | static bool refersToCompleteObject(const LValue &LVal) { |
11308 | if (LVal.Designator.Invalid) |
11309 | return false; |
11310 | |
11311 | if (!LVal.Designator.Entries.empty()) |
11312 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
11313 | |
11314 | if (!LVal.InvalidBase) |
11315 | return true; |
11316 | |
11317 | |
11318 | |
11319 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
11320 | return !E || !isa<MemberExpr>(E); |
11321 | } |
11322 | |
11323 | |
11324 | |
11325 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
11326 | const SubobjectDesignator &Designator = LVal.Designator; |
11327 | |
11328 | |
11329 | |
11330 | |
11331 | |
11332 | |
11333 | |
11334 | |
11335 | |
11336 | |
11337 | |
11338 | return LVal.InvalidBase && |
11339 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
11340 | Designator.MostDerivedIsArrayElement && |
11341 | isDesignatorAtObjectEnd(Ctx, LVal); |
11342 | } |
11343 | |
11344 | |
11345 | |
11346 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
11347 | CharUnits &Result) { |
11348 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
11349 | if (Int.ugt(CharUnitsMax)) |
11350 | return false; |
11351 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
11352 | return true; |
11353 | } |
11354 | |
11355 | |
11356 | |
11357 | |
11358 | |
11359 | |
11360 | |
11361 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
11362 | unsigned Type, const LValue &LVal, |
11363 | CharUnits &EndOffset) { |
11364 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
11365 | |
11366 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
11367 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
11368 | return false; |
11369 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
11370 | }; |
11371 | |
11372 | |
11373 | |
11374 | |
11375 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
11376 | |
11377 | if (Type == 3 && !DetermineForCompleteObject) |
11378 | return false; |
11379 | |
11380 | llvm::APInt APEndOffset; |
11381 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
11382 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
11383 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
11384 | |
11385 | if (LVal.InvalidBase) |
11386 | return false; |
11387 | |
11388 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
11389 | return CheckedHandleSizeof(BaseTy, EndOffset); |
11390 | } |
11391 | |
11392 | |
11393 | const SubobjectDesignator &Designator = LVal.Designator; |
11394 | |
11395 | |
11396 | |
11397 | |
11398 | |
11399 | |
11400 | |
11401 | |
11402 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
11403 | |
11404 | |
11405 | llvm::APInt APEndOffset; |
11406 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
11407 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
11408 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
11409 | |
11410 | |
11411 | |
11412 | |
11413 | if (Type == 1) |
11414 | return false; |
11415 | } |
11416 | |
11417 | CharUnits BytesPerElem; |
11418 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
11419 | return false; |
11420 | |
11421 | |
11422 | |
11423 | |
11424 | int64_t ElemsRemaining; |
11425 | if (Designator.MostDerivedIsArrayElement && |
11426 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
11427 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
11428 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
11429 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
11430 | } else { |
11431 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
11432 | } |
11433 | |
11434 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
11435 | return true; |
11436 | } |
11437 | |
11438 | |
11439 | |
11440 | |
11441 | |
11442 | |
11443 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
11444 | EvalInfo &Info, uint64_t &Size) { |
11445 | |
11446 | LValue LVal; |
11447 | { |
11448 | |
11449 | |
11450 | |
11451 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11452 | IgnoreSideEffectsRAII Fold(Info); |
11453 | |
11454 | if (E->isGLValue()) { |
11455 | |
11456 | |
11457 | APValue RVal; |
11458 | if (!EvaluateAsRValue(Info, E, RVal)) |
11459 | return false; |
11460 | LVal.setFrom(Info.Ctx, RVal); |
11461 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
11462 | true)) |
11463 | return false; |
11464 | } |
11465 | |
11466 | |
11467 | |
11468 | if (LVal.getLValueOffset().isNegative()) { |
11469 | Size = 0; |
11470 | return true; |
11471 | } |
11472 | |
11473 | CharUnits EndOffset; |
11474 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
11475 | return false; |
11476 | |
11477 | |
11478 | |
11479 | if (EndOffset <= LVal.getLValueOffset()) |
11480 | Size = 0; |
11481 | else |
11482 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
11483 | return true; |
11484 | } |
11485 | |
11486 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
11487 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
11488 | return VisitBuiltinCallExpr(E, BuiltinOp); |
11489 | |
11490 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
11491 | } |
11492 | |
11493 | static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info, |
11494 | APValue &Val, APSInt &Alignment) { |
11495 | QualType SrcTy = E->getArg(0)->getType(); |
11496 | if (!getAlignmentArgument(E->getArg(1), SrcTy, Info, Alignment)) |
11497 | return false; |
11498 | |
11499 | |
11500 | if (SrcTy->isPointerType()) { |
11501 | LValue Ptr; |
11502 | if (!EvaluatePointer(E->getArg(0), Ptr, Info)) |
11503 | return false; |
11504 | Ptr.moveInto(Val); |
11505 | } else if (!SrcTy->isIntegralOrEnumerationType()) { |
11506 | Info.FFDiag(E->getArg(0)); |
11507 | return false; |
11508 | } else { |
11509 | APSInt SrcInt; |
11510 | if (!EvaluateInteger(E->getArg(0), SrcInt, Info)) |
11511 | return false; |
11512 | assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() && |
11513 | "Bit widths must be the same"); |
11514 | Val = APValue(SrcInt); |
11515 | } |
11516 | assert(Val.hasValue()); |
11517 | return true; |
11518 | } |
11519 | |
11520 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
11521 | unsigned BuiltinOp) { |
11522 | switch (BuiltinOp) { |
11523 | default: |
11524 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
11525 | |
11526 | case Builtin::BI__builtin_dynamic_object_size: |
11527 | case Builtin::BI__builtin_object_size: { |
11528 | |
11529 | unsigned Type = |
11530 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
11531 | assert(Type <= 3 && "unexpected type"); |
11532 | |
11533 | uint64_t Size; |
11534 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
11535 | return Success(Size, E); |
11536 | |
11537 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
11538 | return Success((Type & 2) ? 0 : -1, E); |
11539 | |
11540 | |
11541 | |
11542 | switch (Info.EvalMode) { |
11543 | case EvalInfo::EM_ConstantExpression: |
11544 | case EvalInfo::EM_ConstantFold: |
11545 | case EvalInfo::EM_IgnoreSideEffects: |
11546 | |
11547 | return Error(E); |
11548 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
11549 | |
11550 | return Success((Type & 2) ? 0 : -1, E); |
11551 | } |
11552 | |
11553 | llvm_unreachable("unexpected EvalMode"); |
11554 | } |
11555 | |
11556 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
11557 | analyze_os_log::OSLogBufferLayout Layout; |
11558 | analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout); |
11559 | return Success(Layout.size().getQuantity(), E); |
11560 | } |
11561 | |
11562 | case Builtin::BI__builtin_is_aligned: { |
11563 | APValue Src; |
11564 | APSInt Alignment; |
11565 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11566 | return false; |
11567 | if (Src.isLValue()) { |
11568 | |
11569 | LValue Ptr; |
11570 | Ptr.setFrom(Info.Ctx, Src); |
11571 | CharUnits BaseAlignment = getBaseAlignment(Info, Ptr); |
11572 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Ptr.Offset); |
11573 | |
11574 | |
11575 | assert(PtrAlign.isPowerOfTwo()); |
11576 | assert(Alignment.isPowerOf2()); |
11577 | if (PtrAlign.getQuantity() >= Alignment) |
11578 | return Success(1, E); |
11579 | |
11580 | |
11581 | |
11582 | |
11583 | if (BaseAlignment.getQuantity() >= Alignment && |
11584 | PtrAlign.getQuantity() < Alignment) |
11585 | return Success(0, E); |
11586 | |
11587 | |
11588 | |
11589 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute) |
11590 | << Alignment; |
11591 | return false; |
11592 | } |
11593 | assert(Src.isInt()); |
11594 | return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E); |
11595 | } |
11596 | case Builtin::BI__builtin_align_up: { |
11597 | APValue Src; |
11598 | APSInt Alignment; |
11599 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11600 | return false; |
11601 | if (!Src.isInt()) |
11602 | return Error(E); |
11603 | APSInt AlignedVal = |
11604 | APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1), |
11605 | Src.getInt().isUnsigned()); |
11606 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
11607 | return Success(AlignedVal, E); |
11608 | } |
11609 | case Builtin::BI__builtin_align_down: { |
11610 | APValue Src; |
11611 | APSInt Alignment; |
11612 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11613 | return false; |
11614 | if (!Src.isInt()) |
11615 | return Error(E); |
11616 | APSInt AlignedVal = |
11617 | APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned()); |
11618 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
11619 | return Success(AlignedVal, E); |
11620 | } |
11621 | |
11622 | case Builtin::BI__builtin_bitreverse8: |
11623 | case Builtin::BI__builtin_bitreverse16: |
11624 | case Builtin::BI__builtin_bitreverse32: |
11625 | case Builtin::BI__builtin_bitreverse64: { |
11626 | APSInt Val; |
11627 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11628 | return false; |
11629 | |
11630 | return Success(Val.reverseBits(), E); |
11631 | } |
11632 | |
11633 | case Builtin::BI__builtin_bswap16: |
11634 | case Builtin::BI__builtin_bswap32: |
11635 | case Builtin::BI__builtin_bswap64: { |
11636 | APSInt Val; |
11637 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11638 | return false; |
11639 | |
11640 | return Success(Val.byteSwap(), E); |
11641 | } |
11642 | |
11643 | case Builtin::BI__builtin_classify_type: |
11644 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
11645 | |
11646 | case Builtin::BI__builtin_clrsb: |
11647 | case Builtin::BI__builtin_clrsbl: |
11648 | case Builtin::BI__builtin_clrsbll: { |
11649 | APSInt Val; |
11650 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11651 | return false; |
11652 | |
11653 | return Success(Val.getBitWidth() - Val.getMinSignedBits(), E); |
11654 | } |
11655 | |
11656 | case Builtin::BI__builtin_clz: |
11657 | case Builtin::BI__builtin_clzl: |
11658 | case Builtin::BI__builtin_clzll: |
11659 | case Builtin::BI__builtin_clzs: { |
11660 | APSInt Val; |
11661 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11662 | return false; |
11663 | if (!Val) |
11664 | return Error(E); |
11665 | |
11666 | return Success(Val.countLeadingZeros(), E); |
11667 | } |
11668 | |
11669 | case Builtin::BI__builtin_constant_p: { |
11670 | const Expr *Arg = E->getArg(0); |
11671 | if (EvaluateBuiltinConstantP(Info, Arg)) |
11672 | return Success(true, E); |
11673 | if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) { |
11674 | |
11675 | |
11676 | |
11677 | return Success(false, E); |
11678 | } |
11679 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11680 | return false; |
11681 | } |
11682 | |
11683 | case Builtin::BI__builtin_is_constant_evaluated: { |
11684 | const auto *Callee = Info.CurrentCall->getCallee(); |
11685 | if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression && |
11686 | (Info.CallStackDepth == 1 || |
11687 | (Info.CallStackDepth == 2 && Callee->isInStdNamespace() && |
11688 | Callee->getIdentifier() && |
11689 | Callee->getIdentifier()->isStr("is_constant_evaluated")))) { |
11690 | |
11691 | if (Info.EvalStatus.Diag) |
11692 | Info.report((Info.CallStackDepth == 1) ? E->getExprLoc() |
11693 | : Info.CurrentCall->CallLoc, |
11694 | diag::warn_is_constant_evaluated_always_true_constexpr) |
11695 | << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated" |
11696 | : "std::is_constant_evaluated"); |
11697 | } |
11698 | |
11699 | return Success(Info.InConstantContext, E); |
11700 | } |
11701 | |
11702 | case Builtin::BI__builtin_ctz: |
11703 | case Builtin::BI__builtin_ctzl: |
11704 | case Builtin::BI__builtin_ctzll: |
11705 | case Builtin::BI__builtin_ctzs: { |
11706 | APSInt Val; |
11707 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11708 | return false; |
11709 | if (!Val) |
11710 | return Error(E); |
11711 | |
11712 | return Success(Val.countTrailingZeros(), E); |
11713 | } |
11714 | |
11715 | case Builtin::BI__builtin_eh_return_data_regno: { |
11716 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
11717 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
11718 | return Success(Operand, E); |
11719 | } |
11720 | |
11721 | case Builtin::BI__builtin_expect: |
11722 | case Builtin::BI__builtin_expect_with_probability: |
11723 | return Visit(E->getArg(0)); |
11724 | |
11725 | case Builtin::BI__builtin_ffs: |
11726 | case Builtin::BI__builtin_ffsl: |
11727 | case Builtin::BI__builtin_ffsll: { |
11728 | APSInt Val; |
11729 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11730 | return false; |
11731 | |
11732 | unsigned N = Val.countTrailingZeros(); |
11733 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
11734 | } |
11735 | |
11736 | case Builtin::BI__builtin_fpclassify: { |
11737 | APFloat Val(0.0); |
11738 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
11739 | return false; |
11740 | unsigned Arg; |
11741 | switch (Val.getCategory()) { |
11742 | case APFloat::fcNaN: Arg = 0; break; |
11743 | case APFloat::fcInfinity: Arg = 1; break; |
11744 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
11745 | case APFloat::fcZero: Arg = 4; break; |
11746 | } |
11747 | return Visit(E->getArg(Arg)); |
11748 | } |
11749 | |
11750 | case Builtin::BI__builtin_isinf_sign: { |
11751 | APFloat Val(0.0); |
11752 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11753 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
11754 | } |
11755 | |
11756 | case Builtin::BI__builtin_isinf: { |
11757 | APFloat Val(0.0); |
11758 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11759 | Success(Val.isInfinity() ? 1 : 0, E); |
11760 | } |
11761 | |
11762 | case Builtin::BI__builtin_isfinite: { |
11763 | APFloat Val(0.0); |
11764 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11765 | Success(Val.isFinite() ? 1 : 0, E); |
11766 | } |
11767 | |
11768 | case Builtin::BI__builtin_isnan: { |
11769 | APFloat Val(0.0); |
11770 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11771 | Success(Val.isNaN() ? 1 : 0, E); |
11772 | } |
11773 | |
11774 | case Builtin::BI__builtin_isnormal: { |
11775 | APFloat Val(0.0); |
11776 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11777 | Success(Val.isNormal() ? 1 : 0, E); |
11778 | } |
11779 | |
11780 | case Builtin::BI__builtin_parity: |
11781 | case Builtin::BI__builtin_parityl: |
11782 | case Builtin::BI__builtin_parityll: { |
11783 | APSInt Val; |
11784 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11785 | return false; |
11786 | |
11787 | return Success(Val.countPopulation() % 2, E); |
11788 | } |
11789 | |
11790 | case Builtin::BI__builtin_popcount: |
11791 | case Builtin::BI__builtin_popcountl: |
11792 | case Builtin::BI__builtin_popcountll: { |
11793 | APSInt Val; |
11794 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11795 | return false; |
11796 | |
11797 | return Success(Val.countPopulation(), E); |
11798 | } |
11799 | |
11800 | case Builtin::BI__builtin_rotateleft8: |
11801 | case Builtin::BI__builtin_rotateleft16: |
11802 | case Builtin::BI__builtin_rotateleft32: |
11803 | case Builtin::BI__builtin_rotateleft64: |
11804 | case Builtin::BI_rotl8: |
11805 | case Builtin::BI_rotl16: |
11806 | case Builtin::BI_rotl: |
11807 | case Builtin::BI_lrotl: |
11808 | case Builtin::BI_rotl64: { |
11809 | APSInt Val, Amt; |
11810 | if (!EvaluateInteger(E->getArg(0), Val, Info) || |
11811 | !EvaluateInteger(E->getArg(1), Amt, Info)) |
11812 | return false; |
11813 | |
11814 | return Success(Val.rotl(Amt.urem(Val.getBitWidth())), E); |
11815 | } |
11816 | |
11817 | case Builtin::BI__builtin_rotateright8: |
11818 | case Builtin::BI__builtin_rotateright16: |
11819 | case Builtin::BI__builtin_rotateright32: |
11820 | case Builtin::BI__builtin_rotateright64: |
11821 | case Builtin::BI_rotr8: |
11822 | case Builtin::BI_rotr16: |
11823 | case Builtin::BI_rotr: |
11824 | case Builtin::BI_lrotr: |
11825 | case Builtin::BI_rotr64: { |
11826 | APSInt Val, Amt; |
11827 | if (!EvaluateInteger(E->getArg(0), Val, Info) || |
11828 | !EvaluateInteger(E->getArg(1), Amt, Info)) |
11829 | return false; |
11830 | |
11831 | return Success(Val.rotr(Amt.urem(Val.getBitWidth())), E); |
11832 | } |
11833 | |
11834 | case Builtin::BIstrlen: |
11835 | case Builtin::BIwcslen: |
11836 | |
11837 | if (Info.getLangOpts().CPlusPlus11) |
11838 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
11839 | << 0 << 0 |
11840 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
11841 | else |
11842 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11843 | LLVM_FALLTHROUGH; |
11844 | case Builtin::BI__builtin_strlen: |
11845 | case Builtin::BI__builtin_wcslen: { |
11846 | |
11847 | |
11848 | LValue String; |
11849 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
11850 | return false; |
11851 | |
11852 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
11853 | |
11854 | |
11855 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
11856 | String.getLValueBase().dyn_cast<const Expr *>())) { |
11857 | |
11858 | |
11859 | StringRef Str = S->getBytes(); |
11860 | int64_t Off = String.Offset.getQuantity(); |
11861 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
11862 | S->getCharByteWidth() == 1 && |
11863 | |
11864 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
11865 | Str = Str.substr(Off); |
11866 | |
11867 | StringRef::size_type Pos = Str.find(0); |
11868 | if (Pos != StringRef::npos) |
11869 | Str = Str.substr(0, Pos); |
11870 | |
11871 | return Success(Str.size(), E); |
11872 | } |
11873 | |
11874 | |
11875 | } |
11876 | |
11877 | |
11878 | for (uint64_t Strlen = 0; ; ++Strlen) { |
11879 | APValue Char; |
11880 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
11881 | !Char.isInt()) |
11882 | return false; |
11883 | if (!Char.getInt()) |
11884 | return Success(Strlen, E); |
11885 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
11886 | return false; |
11887 | } |
11888 | } |
11889 | |
11890 | case Builtin::BIstrcmp: |
11891 | case Builtin::BIwcscmp: |
11892 | case Builtin::BIstrncmp: |
11893 | case Builtin::BIwcsncmp: |
11894 | case Builtin::BImemcmp: |
11895 | case Builtin::BIbcmp: |
11896 | case Builtin::BIwmemcmp: |
11897 | |
11898 | if (Info.getLangOpts().CPlusPlus11) |
11899 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
11900 | << 0 << 0 |
11901 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
11902 | else |
11903 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11904 | LLVM_FALLTHROUGH; |
11905 | case Builtin::BI__builtin_strcmp: |
11906 | case Builtin::BI__builtin_wcscmp: |
11907 | case Builtin::BI__builtin_strncmp: |
11908 | case Builtin::BI__builtin_wcsncmp: |
11909 | case Builtin::BI__builtin_memcmp: |
11910 | case Builtin::BI__builtin_bcmp: |
11911 | case Builtin::BI__builtin_wmemcmp: { |
11912 | LValue String1, String2; |
11913 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
11914 | !EvaluatePointer(E->getArg(1), String2, Info)) |
11915 | return false; |
11916 | |
11917 | uint64_t MaxLength = uint64_t(-1); |
11918 | if (BuiltinOp != Builtin::BIstrcmp && |
11919 | BuiltinOp != Builtin::BIwcscmp && |
11920 | BuiltinOp != Builtin::BI__builtin_strcmp && |
11921 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
11922 | APSInt N; |
11923 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
11924 | return false; |
11925 | MaxLength = N.getExtValue(); |
11926 | } |
11927 | |
11928 | |
11929 | if (MaxLength == 0u) |
11930 | return Success(0, E); |
11931 | |
11932 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
11933 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
11934 | String1.Designator.Invalid || String2.Designator.Invalid) |
11935 | return false; |
11936 | |
11937 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
11938 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
11939 | |
11940 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
11941 | BuiltinOp == Builtin::BIbcmp || |
11942 | BuiltinOp == Builtin::BI__builtin_memcmp || |
11943 | BuiltinOp == Builtin::BI__builtin_bcmp; |
11944 | |
11945 | assert(IsRawByte || |
11946 | (Info.Ctx.hasSameUnqualifiedType( |
11947 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
11948 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
11949 | |
11950 | |
11951 | |
11952 | if (IsRawByte && |
11953 | !(isOneByteCharacterType(CharTy1) && isOneByteCharacterType(CharTy2))) { |
11954 | |
11955 | Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported) |
11956 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'") |
11957 | << CharTy1 << CharTy2; |
11958 | return false; |
11959 | } |
11960 | |
11961 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
11962 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
11963 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
11964 | Char1.isInt() && Char2.isInt(); |
11965 | }; |
11966 | const auto &AdvanceElems = [&] { |
11967 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
11968 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
11969 | }; |
11970 | |
11971 | bool StopAtNull = |
11972 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
11973 | BuiltinOp != Builtin::BIwmemcmp && |
11974 | BuiltinOp != Builtin::BI__builtin_memcmp && |
11975 | BuiltinOp != Builtin::BI__builtin_bcmp && |
11976 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
11977 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
11978 | BuiltinOp == Builtin::BIwcsncmp || |
11979 | BuiltinOp == Builtin::BIwmemcmp || |
11980 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
11981 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
11982 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
11983 | |
11984 | for (; MaxLength; --MaxLength) { |
11985 | APValue Char1, Char2; |
11986 | if (!ReadCurElems(Char1, Char2)) |
11987 | return false; |
11988 | if (Char1.getInt().ne(Char2.getInt())) { |
11989 | if (IsWide) |
11990 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
11991 | |
11992 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
11993 | } |
11994 | if (StopAtNull && !Char1.getInt()) |
11995 | return Success(0, E); |
11996 | assert(!(StopAtNull && !Char2.getInt())); |
11997 | if (!AdvanceElems()) |
11998 | return false; |
11999 | } |
12000 | |
12001 | return Success(0, E); |
12002 | } |
12003 | |
12004 | case Builtin::BI__atomic_always_lock_free: |
12005 | case Builtin::BI__atomic_is_lock_free: |
12006 | case Builtin::BI__c11_atomic_is_lock_free: { |
12007 | APSInt SizeVal; |
12008 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
12009 | return false; |
12010 | |
12011 | |
12012 | |
12013 | |
12014 | |
12015 | |
12016 | |
12017 | |
12018 | |
12019 | |
12020 | |
12021 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
12022 | if (Size.isPowerOfTwo()) { |
12023 | |
12024 | unsigned InlineWidthBits = |
12025 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
12026 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
12027 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
12028 | Size == CharUnits::One() || |
12029 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
12030 | Expr::NPC_NeverValueDependent)) |
12031 | |
12032 | |
12033 | return Success(1, E); |
12034 | |
12035 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
12036 | castAs<PointerType>()->getPointeeType(); |
12037 | if (!PointeeType->isIncompleteType() && |
12038 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
12039 | |
12040 | return Success(1, E); |
12041 | } |
12042 | } |
12043 | } |
12044 | |
12045 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
12046 | Success(0, E) : Error(E); |
12047 | } |
12048 | case Builtin::BI__builtin_add_overflow: |
12049 | case Builtin::BI__builtin_sub_overflow: |
12050 | case Builtin::BI__builtin_mul_overflow: |
12051 | case Builtin::BI__builtin_sadd_overflow: |
12052 | case Builtin::BI__builtin_uadd_overflow: |
12053 | case Builtin::BI__builtin_uaddl_overflow: |
12054 | case Builtin::BI__builtin_uaddll_overflow: |
12055 | case Builtin::BI__builtin_usub_overflow: |
12056 | case Builtin::BI__builtin_usubl_overflow: |
12057 | case Builtin::BI__builtin_usubll_overflow: |
12058 | case Builtin::BI__builtin_umul_overflow: |
12059 | case Builtin::BI__builtin_umull_overflow: |
12060 | case Builtin::BI__builtin_umulll_overflow: |
12061 | case Builtin::BI__builtin_saddl_overflow: |
12062 | case Builtin::BI__builtin_saddll_overflow: |
12063 | case Builtin::BI__builtin_ssub_overflow: |
12064 | case Builtin::BI__builtin_ssubl_overflow: |
12065 | case Builtin::BI__builtin_ssubll_overflow: |
12066 | case Builtin::BI__builtin_smul_overflow: |
12067 | case Builtin::BI__builtin_smull_overflow: |
12068 | case Builtin::BI__builtin_smulll_overflow: { |
12069 | LValue ResultLValue; |
12070 | APSInt LHS, RHS; |
12071 | |
12072 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
12073 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
12074 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
12075 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
12076 | return false; |
12077 | |
12078 | APSInt Result; |
12079 | bool DidOverflow = false; |
12080 | |
12081 | |
12082 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12083 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12084 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12085 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
12086 | ResultType->isSignedIntegerOrEnumerationType(); |
12087 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
12088 | ResultType->isSignedIntegerOrEnumerationType(); |
12089 | uint64_t LHSSize = LHS.getBitWidth(); |
12090 | uint64_t RHSSize = RHS.getBitWidth(); |
12091 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
12092 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
12093 | |
12094 | |
12095 | |
12096 | |
12097 | |
12098 | if (IsSigned && !AllSigned) |
12099 | ++MaxBits; |
12100 | |
12101 | LHS = APSInt(LHS.extOrTrunc(MaxBits), !IsSigned); |
12102 | RHS = APSInt(RHS.extOrTrunc(MaxBits), !IsSigned); |
12103 | Result = APSInt(MaxBits, !IsSigned); |
12104 | } |
12105 | |
12106 | |
12107 | switch (BuiltinOp) { |
12108 | default: |
12109 | llvm_unreachable("Invalid value for BuiltinOp"); |
12110 | case Builtin::BI__builtin_add_overflow: |
12111 | case Builtin::BI__builtin_sadd_overflow: |
12112 | case Builtin::BI__builtin_saddl_overflow: |
12113 | case Builtin::BI__builtin_saddll_overflow: |
12114 | case Builtin::BI__builtin_uadd_overflow: |
12115 | case Builtin::BI__builtin_uaddl_overflow: |
12116 | case Builtin::BI__builtin_uaddll_overflow: |
12117 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
12118 | : LHS.uadd_ov(RHS, DidOverflow); |
12119 | break; |
12120 | case Builtin::BI__builtin_sub_overflow: |
12121 | case Builtin::BI__builtin_ssub_overflow: |
12122 | case Builtin::BI__builtin_ssubl_overflow: |
12123 | case Builtin::BI__builtin_ssubll_overflow: |
12124 | case Builtin::BI__builtin_usub_overflow: |
12125 | case Builtin::BI__builtin_usubl_overflow: |
12126 | case Builtin::BI__builtin_usubll_overflow: |
12127 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
12128 | : LHS.usub_ov(RHS, DidOverflow); |
12129 | break; |
12130 | case Builtin::BI__builtin_mul_overflow: |
12131 | case Builtin::BI__builtin_smul_overflow: |
12132 | case Builtin::BI__builtin_smull_overflow: |
12133 | case Builtin::BI__builtin_smulll_overflow: |
12134 | case Builtin::BI__builtin_umul_overflow: |
12135 | case Builtin::BI__builtin_umull_overflow: |
12136 | case Builtin::BI__builtin_umulll_overflow: |
12137 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
12138 | : LHS.umul_ov(RHS, DidOverflow); |
12139 | break; |
12140 | } |
12141 | |
12142 | |
12143 | |
12144 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12145 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12146 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12147 | |
12148 | |
12149 | |
12150 | |
12151 | |
12152 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
12153 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
12154 | |
12155 | if (!APSInt::isSameValue(Temp, Result)) |
12156 | DidOverflow = true; |
12157 | Result = Temp; |
12158 | } |
12159 | |
12160 | APValue APV{Result}; |
12161 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
12162 | return false; |
12163 | return Success(DidOverflow, E); |
12164 | } |
12165 | } |
12166 | } |
12167 | |
12168 | |
12169 | |
12170 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
12171 | const LValue &LV) { |
12172 | |
12173 | |
12174 | if (!LV.getLValueBase()) |
12175 | return false; |
12176 | |
12177 | |
12178 | |
12179 | if (!LV.getLValueDesignator().Invalid && |
12180 | !LV.getLValueDesignator().isOnePastTheEnd()) |
12181 | return false; |
12182 | |
12183 | |
12184 | |
12185 | QualType Ty = getType(LV.getLValueBase()); |
12186 | if (Ty->isIncompleteType()) |
12187 | return true; |
12188 | |
12189 | |
12190 | |
12191 | auto Size = Ctx.getTypeSizeInChars(Ty); |
12192 | return LV.getLValueOffset() == Size; |
12193 | } |
12194 | |
12195 | namespace { |
12196 | |
12197 | |
12198 | |
12199 | |
12200 | |
12201 | |
12202 | class DataRecursiveIntBinOpEvaluator { |
12203 | struct EvalResult { |
12204 | APValue Val; |
12205 | bool Failed; |
12206 | |
12207 | EvalResult() : Failed(false) { } |
12208 | |
12209 | void swap(EvalResult &RHS) { |
12210 | Val.swap(RHS.Val); |
12211 | Failed = RHS.Failed; |
12212 | RHS.Failed = false; |
12213 | } |
12214 | }; |
12215 | |
12216 | struct Job { |
12217 | const Expr *E; |
12218 | EvalResult LHSResult; |
12219 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
12220 | |
12221 | Job() = default; |
12222 | Job(Job &&) = default; |
12223 | |
12224 | void startSpeculativeEval(EvalInfo &Info) { |
12225 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
12226 | } |
12227 | |
12228 | private: |
12229 | SpeculativeEvaluationRAII SpecEvalRAII; |
12230 | }; |
12231 | |
12232 | SmallVector<Job, 16> Queue; |
12233 | |
12234 | IntExprEvaluator &IntEval; |
12235 | EvalInfo &Info; |
12236 | APValue &FinalResult; |
12237 | |
12238 | public: |
12239 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
12240 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
12241 | |
12242 | |
12243 | |
12244 | |
12245 | |
12246 | static bool shouldEnqueue(const BinaryOperator *E) { |
12247 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
12248 | (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() && |
12249 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12250 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12251 | } |
12252 | |
12253 | bool Traverse(const BinaryOperator *E) { |
12254 | enqueue(E); |
12255 | EvalResult PrevResult; |
12256 | while (!Queue.empty()) |
12257 | process(PrevResult); |
12258 | |
12259 | if (PrevResult.Failed) return false; |
12260 | |
12261 | FinalResult.swap(PrevResult.Val); |
12262 | return true; |
12263 | } |
12264 | |
12265 | private: |
12266 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
12267 | return IntEval.Success(Value, E, Result); |
12268 | } |
12269 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
12270 | return IntEval.Success(Value, E, Result); |
12271 | } |
12272 | bool Error(const Expr *E) { |
12273 | return IntEval.Error(E); |
12274 | } |
12275 | bool Error(const Expr *E, diag::kind D) { |
12276 | return IntEval.Error(E, D); |
12277 | } |
12278 | |
12279 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
12280 | return Info.CCEDiag(E, D); |
12281 | } |
12282 | |
12283 | |
12284 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12285 | bool &SuppressRHSDiags); |
12286 | |
12287 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
12288 | const BinaryOperator *E, APValue &Result); |
12289 | |
12290 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
12291 | Result.Failed = !Evaluate(Result.Val, Info, E); |
12292 | if (Result.Failed) |
12293 | Result.Val = APValue(); |
12294 | } |
12295 | |
12296 | void process(EvalResult &Result); |
12297 | |
12298 | void enqueue(const Expr *E) { |
12299 | E = E->IgnoreParens(); |
12300 | Queue.resize(Queue.size()+1); |
12301 | Queue.back().E = E; |
12302 | Queue.back().Kind = Job::AnyExprKind; |
12303 | } |
12304 | }; |
12305 | |
12306 | } |
12307 | |
12308 | bool DataRecursiveIntBinOpEvaluator:: |
12309 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12310 | bool &SuppressRHSDiags) { |
12311 | if (E->getOpcode() == BO_Comma) { |
12312 | |
12313 | if (LHSResult.Failed) |
12314 | return Info.noteSideEffect(); |
12315 | return true; |
12316 | } |
12317 | |
12318 | if (E->isLogicalOp()) { |
12319 | bool LHSAsBool; |
12320 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
12321 | |
12322 | |
12323 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
12324 | Success(LHSAsBool, E, LHSResult.Val); |
12325 | return false; |
12326 | } |
12327 | } else { |
12328 | LHSResult.Failed = true; |
12329 | |
12330 | |
12331 | |
12332 | if (!Info.noteSideEffect()) |
12333 | return false; |
12334 | |
12335 | |
12336 | |
12337 | |
12338 | SuppressRHSDiags = true; |
12339 | } |
12340 | |
12341 | return true; |
12342 | } |
12343 | |
12344 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12345 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12346 | |
12347 | if (LHSResult.Failed && !Info.noteFailure()) |
12348 | return false; |
12349 | |
12350 | return true; |
12351 | } |
12352 | |
12353 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
12354 | bool IsSub) { |
12355 | |
12356 | |
12357 | |
12358 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
12359 | CharUnits &Offset = LVal.getLValueOffset(); |
12360 | uint64_t Offset64 = Offset.getQuantity(); |
12361 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
12362 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
12363 | : Offset64 + Index64); |
12364 | } |
12365 | |
12366 | bool DataRecursiveIntBinOpEvaluator:: |
12367 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
12368 | const BinaryOperator *E, APValue &Result) { |
12369 | if (E->getOpcode() == BO_Comma) { |
12370 | if (RHSResult.Failed) |
12371 | return false; |
12372 | Result = RHSResult.Val; |
12373 | return true; |
12374 | } |
12375 | |
12376 | if (E->isLogicalOp()) { |
12377 | bool lhsResult, rhsResult; |
12378 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
12379 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
12380 | |
12381 | if (LHSIsOK) { |
12382 | if (RHSIsOK) { |
12383 | if (E->getOpcode() == BO_LOr) |
12384 | return Success(lhsResult || rhsResult, E, Result); |
12385 | else |
12386 | return Success(lhsResult && rhsResult, E, Result); |
12387 | } |
12388 | } else { |
12389 | if (RHSIsOK) { |
12390 | |
12391 | |
12392 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
12393 | return Success(rhsResult, E, Result); |
12394 | } |
12395 | } |
12396 | |
12397 | return false; |
12398 | } |
12399 | |
12400 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12401 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12402 | |
12403 | if (LHSResult.Failed || RHSResult.Failed) |
12404 | return false; |
12405 | |
12406 | const APValue &LHSVal = LHSResult.Val; |
12407 | const APValue &RHSVal = RHSResult.Val; |
12408 | |
12409 | |
12410 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
12411 | Result = LHSVal; |
12412 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
12413 | return true; |
12414 | } |
12415 | |
12416 | |
12417 | if (E->getOpcode() == BO_Add && |
12418 | RHSVal.isLValue() && LHSVal.isInt()) { |
12419 | Result = RHSVal; |
12420 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), false); |
12421 | return true; |
12422 | } |
12423 | |
12424 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
12425 | |
12426 | if (!LHSVal.getLValueOffset().isZero() || |
12427 | !RHSVal.getLValueOffset().isZero()) |
12428 | return false; |
12429 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
12430 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
12431 | if (!LHSExpr || !RHSExpr) |
12432 | return false; |
12433 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
12434 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
12435 | if (!LHSAddrExpr || !RHSAddrExpr) |
12436 | return false; |
12437 | |
12438 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
12439 | RHSAddrExpr->getLabel()->getDeclContext()) |
12440 | return false; |
12441 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
12442 | return true; |
12443 | } |
12444 | |
12445 | |
12446 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
12447 | return Error(E); |
12448 | |
12449 | |
12450 | |
12451 | |
12452 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
12453 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
12454 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
12455 | RHSVal.getInt(), Value)) |
12456 | return false; |
12457 | return Success(Value, E, Result); |
12458 | } |
12459 | |
12460 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
12461 | Job &job = Queue.back(); |
12462 | |
12463 | switch (job.Kind) { |
12464 | case Job::AnyExprKind: { |
12465 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
12466 | if (shouldEnqueue(Bop)) { |
12467 | job.Kind = Job::BinOpKind; |
12468 | enqueue(Bop->getLHS()); |
12469 | return; |
12470 | } |
12471 | } |
12472 | |
12473 | EvaluateExpr(job.E, Result); |
12474 | Queue.pop_back(); |
12475 | return; |
12476 | } |
12477 | |
12478 | case Job::BinOpKind: { |
12479 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
12480 | bool SuppressRHSDiags = false; |
12481 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
12482 | Queue.pop_back(); |
12483 | return; |
12484 | } |
12485 | if (SuppressRHSDiags) |
12486 | job.startSpeculativeEval(Info); |
12487 | job.LHSResult.swap(Result); |
12488 | job.Kind = Job::BinOpVisitedLHSKind; |
12489 | enqueue(Bop->getRHS()); |
12490 | return; |
12491 | } |
12492 | |
12493 | case Job::BinOpVisitedLHSKind: { |
12494 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
12495 | EvalResult RHS; |
12496 | RHS.swap(Result); |
12497 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
12498 | Queue.pop_back(); |
12499 | return; |
12500 | } |
12501 | } |
12502 | |
12503 | llvm_unreachable("Invalid Job::Kind!"); |
12504 | } |
12505 | |
12506 | namespace { |
12507 | enum class CmpResult { |
12508 | Unequal, |
12509 | Less, |
12510 | Equal, |
12511 | Greater, |
12512 | Unordered, |
12513 | }; |
12514 | } |
12515 | |
12516 | template <class SuccessCB, class AfterCB> |
12517 | static bool |
12518 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
12519 | SuccessCB &&Success, AfterCB &&DoAfter) { |
12520 | assert(!E->isValueDependent()); |
12521 | assert(E->isComparisonOp() && "expected comparison operator"); |
12522 | assert((E->getOpcode() == BO_Cmp || |
12523 | E->getType()->isIntegralOrEnumerationType()) && |
12524 | "unsupported binary expression evaluation"); |
12525 | auto Error = [&](const Expr *E) { |
12526 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12527 | return false; |
12528 | }; |
12529 | |
12530 | bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp; |
12531 | bool IsEquality = E->isEqualityOp(); |
12532 | |
12533 | QualType LHSTy = E->getLHS()->getType(); |
12534 | QualType RHSTy = E->getRHS()->getType(); |
12535 | |
12536 | if (LHSTy->isIntegralOrEnumerationType() && |
12537 | RHSTy->isIntegralOrEnumerationType()) { |
12538 | APSInt LHS, RHS; |
12539 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
12540 | if (!LHSOK && !Info.noteFailure()) |
12541 | return false; |
12542 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
12543 | return false; |
12544 | if (LHS < RHS) |
12545 | return Success(CmpResult::Less, E); |
12546 | if (LHS > RHS) |
12547 | return Success(CmpResult::Greater, E); |
12548 | return Success(CmpResult::Equal, E); |
12549 | } |
12550 | |
12551 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
12552 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy)); |
12553 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy)); |
12554 | |
12555 | bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info); |
12556 | if (!LHSOK && !Info.noteFailure()) |
12557 | return false; |
12558 | if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK) |
12559 | return false; |
12560 | if (LHSFX < RHSFX) |
12561 | return Success(CmpResult::Less, E); |
12562 | if (LHSFX > RHSFX) |
12563 | return Success(CmpResult::Greater, E); |
12564 | return Success(CmpResult::Equal, E); |
12565 | } |
12566 | |
12567 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
12568 | ComplexValue LHS, RHS; |
12569 | bool LHSOK; |
12570 | if (E->isAssignmentOp()) { |
12571 | LValue LV; |
12572 | EvaluateLValue(E->getLHS(), LV, Info); |
12573 | LHSOK = false; |
12574 | } else if (LHSTy->isRealFloatingType()) { |
12575 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
12576 | if (LHSOK) { |
12577 | LHS.makeComplexFloat(); |
12578 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
12579 | } |
12580 | } else { |
12581 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
12582 | } |
12583 | if (!LHSOK && !Info.noteFailure()) |
12584 | return false; |
12585 | |
12586 | if (E->getRHS()->getType()->isRealFloatingType()) { |
12587 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
12588 | return false; |
12589 | RHS.makeComplexFloat(); |
12590 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
12591 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
12592 | return false; |
12593 | |
12594 | if (LHS.isComplexFloat()) { |
12595 | APFloat::cmpResult CR_r = |
12596 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
12597 | APFloat::cmpResult CR_i = |
12598 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
12599 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
12600 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
12601 | } else { |
12602 | assert(IsEquality && "invalid complex comparison"); |
12603 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
12604 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
12605 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
12606 | } |
12607 | } |
12608 | |
12609 | if (LHSTy->isRealFloatingType() && |
12610 | RHSTy->isRealFloatingType()) { |
12611 | APFloat RHS(0.0), LHS(0.0); |
12612 | |
12613 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
12614 | if (!LHSOK && !Info.noteFailure()) |
12615 | return false; |
12616 | |
12617 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
12618 | return false; |
12619 | |
12620 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
12621 | llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS); |
12622 | if (!Info.InConstantContext && |
12623 | APFloatCmpResult == APFloat::cmpUnordered && |
12624 | E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).isFPConstrained()) { |
12625 | |
12626 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
12627 | return false; |
12628 | } |
12629 | auto GetCmpRes = [&]() { |
12630 | switch (APFloatCmpResult) { |
12631 | case APFloat::cmpEqual: |
12632 | return CmpResult::Equal; |
12633 | case APFloat::cmpLessThan: |
12634 | return CmpResult::Less; |
12635 | case APFloat::cmpGreaterThan: |
12636 | return CmpResult::Greater; |
12637 | case APFloat::cmpUnordered: |
12638 | return CmpResult::Unordered; |
12639 | } |
12640 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
12641 | }; |
12642 | return Success(GetCmpRes(), E); |
12643 | } |
12644 | |
12645 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
12646 | LValue LHSValue, RHSValue; |
12647 | |
12648 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
12649 | if (!LHSOK && !Info.noteFailure()) |
12650 | return false; |
12651 | |
12652 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12653 | return false; |
12654 | |
12655 | |
12656 | |
12657 | if (!HasSameBase(LHSValue, RHSValue)) { |
12658 | |
12659 | |
12660 | if (!IsEquality) { |
12661 | Info.FFDiag(E, diag::note_constexpr_pointer_comparison_unspecified); |
12662 | return false; |
12663 | } |
12664 | |
12665 | |
12666 | |
12667 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
12668 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
12669 | return Error(E); |
12670 | |
12671 | |
12672 | |
12673 | |
12674 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
12675 | LHSValue.Base && RHSValue.Base) |
12676 | return Error(E); |
12677 | |
12678 | |
12679 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
12680 | return Error(E); |
12681 | |
12682 | |
12683 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
12684 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
12685 | (RHSValue.Base && RHSValue.Offset.isZero() && |
12686 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
12687 | return Error(E); |
12688 | |
12689 | |
12690 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
12691 | (LHSValue.Base && isZeroSized(RHSValue))) |
12692 | return Error(E); |
12693 | return Success(CmpResult::Unequal, E); |
12694 | } |
12695 | |
12696 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
12697 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
12698 | |
12699 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
12700 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
12701 | |
12702 | |
12703 | |
12704 | |
12705 | |
12706 | |
12707 | |
12708 | |
12709 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
12710 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
12711 | |
12712 | |
12713 | |
12714 | |
12715 | |
12716 | |
12717 | |
12718 | |
12719 | |
12720 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
12721 | bool WasArrayIndex; |
12722 | unsigned Mismatch = FindDesignatorMismatch( |
12723 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
12724 | |
12725 | |
12726 | |
12727 | |
12728 | |
12729 | |
12730 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
12731 | Mismatch < RHSDesignator.Entries.size()) { |
12732 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
12733 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
12734 | if (!LF && !RF) |
12735 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
12736 | else if (!LF) |
12737 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
12738 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
12739 | << RF->getParent() << RF; |
12740 | else if (!RF) |
12741 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
12742 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
12743 | << LF->getParent() << LF; |
12744 | else if (!LF->getParent()->isUnion() && |
12745 | LF->getAccess() != RF->getAccess()) |
12746 | Info.CCEDiag(E, |
12747 | diag::note_constexpr_pointer_comparison_differing_access) |
12748 | << LF << LF->getAccess() << RF << RF->getAccess() |
12749 | << LF->getParent(); |
12750 | } |
12751 | } |
12752 | |
12753 | |
12754 | |
12755 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
12756 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
12757 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
12758 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
12759 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
12760 | CompareLHS &= Mask; |
12761 | CompareRHS &= Mask; |
12762 | |
12763 | |
12764 | |
12765 | |
12766 | if (!LHSValue.Base.isNull() && IsRelational) { |
12767 | QualType BaseTy = getType(LHSValue.Base); |
12768 | if (BaseTy->isIncompleteType()) |
12769 | return Error(E); |
12770 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
12771 | uint64_t OffsetLimit = Size.getQuantity(); |
12772 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
12773 | return Error(E); |
12774 | } |
12775 | |
12776 | if (CompareLHS < CompareRHS) |
12777 | return Success(CmpResult::Less, E); |
12778 | if (CompareLHS > CompareRHS) |
12779 | return Success(CmpResult::Greater, E); |
12780 | return Success(CmpResult::Equal, E); |
12781 | } |
12782 | |
12783 | if (LHSTy->isMemberPointerType()) { |
12784 | assert(IsEquality && "unexpected member pointer operation"); |
12785 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
12786 | |
12787 | MemberPtr LHSValue, RHSValue; |
12788 | |
12789 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
12790 | if (!LHSOK && !Info.noteFailure()) |
12791 | return false; |
12792 | |
12793 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12794 | return false; |
12795 | |
12796 | |
12797 | |
12798 | |
12799 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
12800 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
12801 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
12802 | } |
12803 | |
12804 | |
12805 | |
12806 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
12807 | if (MD->isVirtual()) |
12808 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
12809 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
12810 | if (MD->isVirtual()) |
12811 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
12812 | |
12813 | |
12814 | |
12815 | |
12816 | |
12817 | bool Equal = LHSValue == RHSValue; |
12818 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
12819 | } |
12820 | |
12821 | if (LHSTy->isNullPtrType()) { |
12822 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
12823 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
12824 | |
12825 | |
12826 | |
12827 | return Success(CmpResult::Equal, E); |
12828 | } |
12829 | |
12830 | return DoAfter(); |
12831 | } |
12832 | |
12833 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
12834 | if (!CheckLiteralType(Info, E)) |
12835 | return false; |
12836 | |
12837 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
12838 | ComparisonCategoryResult CCR; |
12839 | switch (CR) { |
12840 | case CmpResult::Unequal: |
12841 | llvm_unreachable("should never produce Unequal for three-way comparison"); |
12842 | case CmpResult::Less: |
12843 | CCR = ComparisonCategoryResult::Less; |
12844 | break; |
12845 | case CmpResult::Equal: |
12846 | CCR = ComparisonCategoryResult::Equal; |
12847 | break; |
12848 | case CmpResult::Greater: |
12849 | CCR = ComparisonCategoryResult::Greater; |
12850 | break; |
12851 | case CmpResult::Unordered: |
12852 | CCR = ComparisonCategoryResult::Unordered; |
12853 | break; |
12854 | } |
12855 | |
12856 | |
12857 | const ComparisonCategoryInfo &CmpInfo = |
12858 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
12859 | const VarDecl *VD = CmpInfo.getValueInfo(CmpInfo.makeWeakResult(CCR))->VD; |
12860 | |
12861 | LValue LV; |
12862 | LV.set(VD); |
12863 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
12864 | return false; |
12865 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
12866 | ConstantExprKind::Normal); |
12867 | }; |
12868 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
12869 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
12870 | }); |
12871 | } |
12872 | |
12873 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
12874 | |
12875 | |
12876 | if (E->isAssignmentOp()) { |
12877 | Error(E); |
12878 | if (!Info.noteFailure()) |
12879 | return false; |
12880 | } |
12881 | |
12882 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
12883 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
12884 | |
12885 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
12886 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
12887 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
12888 | |
12889 | if (E->isComparisonOp()) { |
12890 | |
12891 | |
12892 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
12893 | assert((CR != CmpResult::Unequal || E->isEqualityOp()) && |
12894 | "should only produce Unequal for equality comparisons"); |
12895 | bool IsEqual = CR == CmpResult::Equal, |
12896 | IsLess = CR == CmpResult::Less, |
12897 | IsGreater = CR == CmpResult::Greater; |
12898 | auto Op = E->getOpcode(); |
12899 | switch (Op) { |
12900 | default: |
12901 | llvm_unreachable("unsupported binary operator"); |
12902 | case BO_EQ: |
12903 | case BO_NE: |
12904 | return Success(IsEqual == (Op == BO_EQ), E); |
12905 | case BO_LT: |
12906 | return Success(IsLess, E); |
12907 | case BO_GT: |
12908 | return Success(IsGreater, E); |
12909 | case BO_LE: |
12910 | return Success(IsEqual || IsLess, E); |
12911 | case BO_GE: |
12912 | return Success(IsEqual || IsGreater, E); |
12913 | } |
12914 | }; |
12915 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
12916 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
12917 | }); |
12918 | } |
12919 | |
12920 | QualType LHSTy = E->getLHS()->getType(); |
12921 | QualType RHSTy = E->getRHS()->getType(); |
12922 | |
12923 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
12924 | E->getOpcode() == BO_Sub) { |
12925 | LValue LHSValue, RHSValue; |
12926 | |
12927 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
12928 | if (!LHSOK && !Info.noteFailure()) |
12929 | return false; |
12930 | |
12931 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12932 | return false; |
12933 | |
12934 | |
12935 | |
12936 | if (!HasSameBase(LHSValue, RHSValue)) { |
12937 | |
12938 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
12939 | return Error(E); |
12940 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
12941 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
12942 | if (!LHSExpr || !RHSExpr) |
12943 | return Error(E); |
12944 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
12945 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
12946 | if (!LHSAddrExpr || !RHSAddrExpr) |
12947 | return Error(E); |
12948 | |
12949 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
12950 | RHSAddrExpr->getLabel()->getDeclContext()) |
12951 | return Error(E); |
12952 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
12953 | } |
12954 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
12955 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
12956 | |
12957 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
12958 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
12959 | |
12960 | |
12961 | |
12962 | |
12963 | |
12964 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
12965 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
12966 | RHSDesignator)) |
12967 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
12968 | |
12969 | QualType Type = E->getLHS()->getType(); |
12970 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
12971 | |
12972 | CharUnits ElementSize; |
12973 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
12974 | return false; |
12975 | |
12976 | |
12977 | |
12978 | |
12979 | if (ElementSize.isZero()) { |
12980 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
12981 | << ElementType; |
12982 | return false; |
12983 | } |
12984 | |
12985 | |
12986 | |
12987 | |
12988 | |
12989 | |
12990 | |
12991 | |
12992 | |
12993 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
12994 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
12995 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
12996 | false); |
12997 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
12998 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
12999 | |
13000 | if (Result.extend(65) != TrueResult && |
13001 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
13002 | return false; |
13003 | return Success(Result, E); |
13004 | } |
13005 | |
13006 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13007 | } |
13008 | |
13009 | |
13010 | |
13011 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
13012 | const UnaryExprOrTypeTraitExpr *E) { |
13013 | switch(E->getKind()) { |
13014 | case UETT_PreferredAlignOf: |
13015 | case UETT_AlignOf: { |
13016 | if (E->isArgumentType()) |
13017 | return Success(GetAlignOfType(Info, E->getArgumentType(), E->getKind()), |
13018 | E); |
13019 | else |
13020 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr(), E->getKind()), |
13021 | E); |
13022 | } |
13023 | |
13024 | case UETT_VecStep: { |
13025 | QualType Ty = E->getTypeOfArgument(); |
13026 | |
13027 | if (Ty->isVectorType()) { |
13028 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
13029 | |
13030 | |
13031 | |
13032 | if (n == 3) |
13033 | n = 4; |
13034 | |
13035 | return Success(n, E); |
13036 | } else |
13037 | return Success(1, E); |
13038 | } |
13039 | |
13040 | case UETT_SizeOf: { |
13041 | QualType SrcTy = E->getTypeOfArgument(); |
13042 | |
13043 | |
13044 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
13045 | SrcTy = Ref->getPointeeType(); |
13046 | |
13047 | CharUnits Sizeof; |
13048 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
13049 | return false; |
13050 | return Success(Sizeof, E); |
13051 | } |
13052 | case UETT_OpenMPRequiredSimdAlign: |
13053 | assert(E->isArgumentType()); |
13054 | return Success( |
13055 | Info.Ctx.toCharUnitsFromBits( |
13056 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
13057 | .getQuantity(), |
13058 | E); |
13059 | } |
13060 | |
13061 | llvm_unreachable("unknown expr/type trait"); |
13062 | } |
13063 | |
13064 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
13065 | CharUnits Result; |
13066 | unsigned n = OOE->getNumComponents(); |
13067 | if (n == 0) |
13068 | return Error(OOE); |
13069 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
13070 | for (unsigned i = 0; i != n; ++i) { |
13071 | OffsetOfNode ON = OOE->getComponent(i); |
13072 | switch (ON.getKind()) { |
13073 | case OffsetOfNode::Array: { |
13074 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
13075 | APSInt IdxResult; |
13076 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
13077 | return false; |
13078 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
13079 | if (!AT) |
13080 | return Error(OOE); |
13081 | CurrentType = AT->getElementType(); |
13082 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
13083 | Result += IdxResult.getSExtValue() * ElementSize; |
13084 | break; |
13085 | } |
13086 | |
13087 | case OffsetOfNode::Field: { |
13088 | FieldDecl *MemberDecl = ON.getField(); |
13089 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13090 | if (!RT) |
13091 | return Error(OOE); |
13092 | RecordDecl *RD = RT->getDecl(); |
13093 | if (RD->isInvalidDecl()) return false; |
13094 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
13095 | unsigned i = MemberDecl->getFieldIndex(); |
13096 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
13097 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
13098 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
13099 | break; |
13100 | } |
13101 | |
13102 | case OffsetOfNode::Identifier: |
13103 | llvm_unreachable("dependent __builtin_offsetof"); |
13104 | |
13105 | case OffsetOfNode::Base: { |
13106 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
13107 | if (BaseSpec->isVirtual()) |
13108 | return Error(OOE); |
13109 | |
13110 | |
13111 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13112 | if (!RT) |
13113 | return Error(OOE); |
13114 | RecordDecl *RD = RT->getDecl(); |
13115 | if (RD->isInvalidDecl()) return false; |
13116 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
13117 | |
13118 | |
13119 | CurrentType = BaseSpec->getType(); |
13120 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
13121 | if (!BaseRT) |
13122 | return Error(OOE); |
13123 | |
13124 | |
13125 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
13126 | break; |
13127 | } |
13128 | } |
13129 | } |
13130 | return Success(Result, OOE); |
13131 | } |
13132 | |
13133 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13134 | switch (E->getOpcode()) { |
13135 | default: |
13136 | |
13137 | |
13138 | return Error(E); |
13139 | case UO_Extension: |
13140 | |
13141 | |
13142 | return Visit(E->getSubExpr()); |
13143 | case UO_Plus: |
13144 | |
13145 | return Visit(E->getSubExpr()); |
13146 | case UO_Minus: { |
13147 | if (!Visit(E->getSubExpr())) |
13148 | return false; |
13149 | if (!Result.isInt()) return Error(E); |
13150 | const APSInt &Value = Result.getInt(); |
13151 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
13152 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
13153 | E->getType())) |
13154 | return false; |
13155 | return Success(-Value, E); |
13156 | } |
13157 | case UO_Not: { |
13158 | if (!Visit(E->getSubExpr())) |
13159 | return false; |
13160 | if (!Result.isInt()) return Error(E); |
13161 | return Success(~Result.getInt(), E); |
13162 | } |
13163 | case UO_LNot: { |
13164 | bool bres; |
13165 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
13166 | return false; |
13167 | return Success(!bres, E); |
13168 | } |
13169 | } |
13170 | } |
13171 | |
13172 | |
13173 | |
13174 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13175 | const Expr *SubExpr = E->getSubExpr(); |
13176 | QualType DestType = E->getType(); |
13177 | QualType SrcType = SubExpr->getType(); |
13178 | |
13179 | switch (E->getCastKind()) { |
13180 | case CK_BaseToDerived: |
13181 | case CK_DerivedToBase: |
13182 | case CK_UncheckedDerivedToBase: |
13183 | case CK_Dynamic: |
13184 | case CK_ToUnion: |
13185 | case CK_ArrayToPointerDecay: |
13186 | case CK_FunctionToPointerDecay: |
13187 | case CK_NullToPointer: |
13188 | case CK_NullToMemberPointer: |
13189 | case CK_BaseToDerivedMemberPointer: |
13190 | case CK_DerivedToBaseMemberPointer: |
13191 | case CK_ReinterpretMemberPointer: |
13192 | case CK_ConstructorConversion: |
13193 | case CK_IntegralToPointer: |
13194 | case CK_ToVoid: |
13195 | case CK_VectorSplat: |
13196 | case CK_IntegralToFloating: |
13197 | case CK_FloatingCast: |
13198 | case CK_CPointerToObjCPointerCast: |
13199 | case CK_BlockPointerToObjCPointerCast: |
13200 | case CK_AnyPointerToBlockPointerCast: |
13201 | case CK_ObjCObjectLValueCast: |
13202 | case CK_FloatingRealToComplex: |
13203 | case CK_FloatingComplexToReal: |
13204 | case CK_FloatingComplexCast: |
13205 | case CK_FloatingComplexToIntegralComplex: |
13206 | case CK_IntegralRealToComplex: |
13207 | case CK_IntegralComplexCast: |
13208 | case CK_IntegralComplexToFloatingComplex: |
13209 | case CK_BuiltinFnToFnPtr: |
13210 | case CK_ZeroToOCLOpaqueType: |
13211 | case CK_NonAtomicToAtomic: |
13212 | case CK_AddressSpaceConversion: |
13213 | case CK_IntToOCLSampler: |
13214 | case CK_FloatingToFixedPoint: |
13215 | case CK_FixedPointToFloating: |
13216 | case CK_FixedPointCast: |
13217 | case CK_IntegralToFixedPoint: |
13218 | case CK_MatrixCast: |
13219 | llvm_unreachable("invalid cast kind for integral value"); |
13220 | |
13221 | case CK_BitCast: |
13222 | case CK_Dependent: |
13223 | case CK_LValueBitCast: |
13224 | case CK_ARCProduceObject: |
13225 | case CK_ARCConsumeObject: |
13226 | case CK_ARCReclaimReturnedObject: |
13227 | case CK_ARCExtendBlockObject: |
13228 | case CK_CopyAndAutoreleaseBlockObject: |
13229 | return Error(E); |
13230 | |
13231 | case CK_UserDefinedConversion: |
13232 | case CK_LValueToRValue: |
13233 | case CK_AtomicToNonAtomic: |
13234 | case CK_NoOp: |
13235 | case CK_LValueToRValueBitCast: |
13236 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13237 | |
13238 | case CK_MemberPointerToBoolean: |
13239 | case CK_PointerToBoolean: |
13240 | case CK_IntegralToBoolean: |
13241 | case CK_FloatingToBoolean: |
13242 | case CK_BooleanToSignedIntegral: |
13243 | case CK_FloatingComplexToBoolean: |
13244 | case CK_IntegralComplexToBoolean: { |
13245 | bool BoolResult; |
13246 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
13247 | return false; |
13248 | uint64_t IntResult = BoolResult; |
13249 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
13250 | IntResult = (uint64_t)-1; |
13251 | return Success(IntResult, E); |
13252 | } |
13253 | |
13254 | case CK_FixedPointToIntegral: { |
13255 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType)); |
13256 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
13257 | return false; |
13258 | bool Overflowed; |
13259 | llvm::APSInt Result = Src.convertToInt( |
13260 | Info.Ctx.getIntWidth(DestType), |
13261 | DestType->isSignedIntegerOrEnumerationType(), &Overflowed); |
13262 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
13263 | return false; |
13264 | return Success(Result, E); |
13265 | } |
13266 | |
13267 | case CK_FixedPointToBoolean: { |
13268 | |
13269 | APValue Val; |
13270 | if (!Evaluate(Val, Info, SubExpr)) |
13271 | return false; |
13272 | return Success(Val.getFixedPoint().getBoolValue(), E); |
13273 | } |
13274 | |
13275 | case CK_IntegralCast: { |
13276 | if (!Visit(SubExpr)) |
13277 | return false; |
13278 | |
13279 | if (!Result.isInt()) { |
13280 | |
13281 | |
13282 | |
13283 | |
13284 | |
13285 | if (Result.isAddrLabelDiff()) |
13286 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
13287 | |
13288 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
13289 | } |
13290 | |
13291 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
13292 | Result.getInt()), E); |
13293 | } |
13294 | |
13295 | case CK_PointerToIntegral: { |
13296 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
13297 | |
13298 | LValue LV; |
13299 | if (!EvaluatePointer(SubExpr, LV, Info)) |
13300 | return false; |
13301 | |
13302 | if (LV.getLValueBase()) { |
13303 | |
13304 | |
13305 | |
13306 | |
13307 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
13308 | return Error(E); |
13309 | |
13310 | LV.Designator.setInvalid(); |
13311 | LV.moveInto(Result); |
13312 | return true; |
13313 | } |
13314 | |
13315 | APSInt AsInt; |
13316 | APValue V; |
13317 | LV.moveInto(V); |
13318 | if (!V.toIntegralConstant(AsInt, SrcType, Info.Ctx)) |
13319 | llvm_unreachable("Can't cast this!"); |
13320 | |
13321 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
13322 | } |
13323 | |
13324 | case CK_IntegralComplexToReal: { |
13325 | ComplexValue C; |
13326 | if (!EvaluateComplex(SubExpr, C, Info)) |
13327 | return false; |
13328 | return Success(C.getComplexIntReal(), E); |
13329 | } |
13330 | |
13331 | case CK_FloatingToIntegral: { |
13332 | APFloat F(0.0); |
13333 | if (!EvaluateFloat(SubExpr, F, Info)) |
13334 | return false; |
13335 | |
13336 | APSInt Value; |
13337 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
13338 | return false; |
13339 | return Success(Value, E); |
13340 | } |
13341 | } |
13342 | |
13343 | llvm_unreachable("unknown cast resulting in integral value"); |
13344 | } |
13345 | |
13346 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
13347 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13348 | ComplexValue LV; |
13349 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
13350 | return false; |
13351 | if (!LV.isComplexInt()) |
13352 | return Error(E); |
13353 | return Success(LV.getComplexIntReal(), E); |
13354 | } |
13355 | |
13356 | return Visit(E->getSubExpr()); |
13357 | } |
13358 | |
13359 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
13360 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
13361 | ComplexValue LV; |
13362 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
13363 | return false; |
13364 | if (!LV.isComplexInt()) |
13365 | return Error(E); |
13366 | return Success(LV.getComplexIntImag(), E); |
13367 | } |
13368 | |
13369 | VisitIgnoredValue(E->getSubExpr()); |
13370 | return Success(0, E); |
13371 | } |
13372 | |
13373 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
13374 | return Success(E->getPackLength(), E); |
13375 | } |
13376 | |
13377 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
13378 | return Success(E->getValue(), E); |
13379 | } |
13380 | |
13381 | bool IntExprEvaluator::VisitConceptSpecializationExpr( |
13382 | const ConceptSpecializationExpr *E) { |
13383 | return Success(E->isSatisfied(), E); |
13384 | } |
13385 | |
13386 | bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) { |
13387 | return Success(E->isSatisfied(), E); |
13388 | } |
13389 | |
13390 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13391 | switch (E->getOpcode()) { |
13392 | default: |
13393 | |
13394 | return Error(E); |
13395 | case UO_Plus: |
13396 | |
13397 | return Visit(E->getSubExpr()); |
13398 | case UO_Minus: { |
13399 | if (!Visit(E->getSubExpr())) return false; |
13400 | if (!Result.isFixedPoint()) |
13401 | return Error(E); |
13402 | bool Overflowed; |
13403 | APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed); |
13404 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
13405 | return false; |
13406 | return Success(Negated, E); |
13407 | } |
13408 | case UO_LNot: { |
13409 | bool bres; |
13410 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
13411 | return false; |
13412 | return Success(!bres, E); |
13413 | } |
13414 | } |
13415 | } |
13416 | |
13417 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13418 | const Expr *SubExpr = E->getSubExpr(); |
13419 | QualType DestType = E->getType(); |
13420 | assert(DestType->isFixedPointType() && |
13421 | "Expected destination type to be a fixed point type"); |
13422 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType); |
13423 | |
13424 | switch (E->getCastKind()) { |
13425 | case CK_FixedPointCast: { |
13426 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
13427 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
13428 | return false; |
13429 | bool Overflowed; |
13430 | APFixedPoint Result = Src.convert(DestFXSema, &Overflowed); |
13431 | if (Overflowed) { |
13432 | if (Info.checkingForUndefinedBehavior()) |
13433 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13434 | diag::warn_fixedpoint_constant_overflow) |
13435 | << Result.toString() << E->getType(); |
13436 | if (!HandleOverflow(Info, E, Result, E->getType())) |
13437 | return false; |
13438 | } |
13439 | return Success(Result, E); |
13440 | } |
13441 | case CK_IntegralToFixedPoint: { |
13442 | APSInt Src; |
13443 | if (!EvaluateInteger(SubExpr, Src, Info)) |
13444 | return false; |
13445 | |
13446 | bool Overflowed; |
13447 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
13448 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
13449 | |
13450 | if (Overflowed) { |
13451 | if (Info.checkingForUndefinedBehavior()) |
13452 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13453 | diag::warn_fixedpoint_constant_overflow) |
13454 | << IntResult.toString() << E->getType(); |
13455 | if (!HandleOverflow(Info, E, IntResult, E->getType())) |
13456 | return false; |
13457 | } |
13458 | |
13459 | return Success(IntResult, E); |
13460 | } |
13461 | case CK_FloatingToFixedPoint: { |
13462 | APFloat Src(0.0); |
13463 | if (!EvaluateFloat(SubExpr, Src, Info)) |
13464 | return false; |
13465 | |
13466 | bool Overflowed; |
13467 | APFixedPoint Result = APFixedPoint::getFromFloatValue( |
13468 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
13469 | |
13470 | if (Overflowed) { |
13471 | if (Info.checkingForUndefinedBehavior()) |
13472 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13473 | diag::warn_fixedpoint_constant_overflow) |
13474 | << Result.toString() << E->getType(); |
13475 | if (!HandleOverflow(Info, E, Result, E->getType())) |
13476 | return false; |
13477 | } |
13478 | |
13479 | return Success(Result, E); |
13480 | } |
13481 | case CK_NoOp: |
13482 | case CK_LValueToRValue: |
13483 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13484 | default: |
13485 | return Error(E); |
13486 | } |
13487 | } |
13488 | |
13489 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13490 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
13491 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13492 | |
13493 | const Expr *LHS = E->getLHS(); |
13494 | const Expr *RHS = E->getRHS(); |
13495 | FixedPointSemantics ResultFXSema = |
13496 | Info.Ctx.getFixedPointSemantics(E->getType()); |
13497 | |
13498 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType())); |
13499 | if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info)) |
13500 | return false; |
13501 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType())); |
13502 | if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info)) |
13503 | return false; |
13504 | |
13505 | bool OpOverflow = false, ConversionOverflow = false; |
13506 | APFixedPoint Result(LHSFX.getSemantics()); |
13507 | switch (E->getOpcode()) { |
13508 | case BO_Add: { |
13509 | Result = LHSFX.add(RHSFX, &OpOverflow) |
13510 | .convert(ResultFXSema, &ConversionOverflow); |
13511 | break; |
13512 | } |
13513 | case BO_Sub: { |
13514 | Result = LHSFX.sub(RHSFX, &OpOverflow) |
13515 | .convert(ResultFXSema, &ConversionOverflow); |
13516 | break; |
13517 | } |
13518 | case BO_Mul: { |
13519 | Result = LHSFX.mul(RHSFX, &OpOverflow) |
13520 | .convert(ResultFXSema, &ConversionOverflow); |
13521 | break; |
13522 | } |
13523 | case BO_Div: { |
13524 | if (RHSFX.getValue() == 0) { |
13525 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
13526 | return false; |
13527 | } |
13528 | Result = LHSFX.div(RHSFX, &OpOverflow) |
13529 | .convert(ResultFXSema, &ConversionOverflow); |
13530 | break; |
13531 | } |
13532 | case BO_Shl: |
13533 | case BO_Shr: { |
13534 | FixedPointSemantics LHSSema = LHSFX.getSemantics(); |
13535 | llvm::APSInt RHSVal = RHSFX.getValue(); |
13536 | |
13537 | unsigned ShiftBW = |
13538 | LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding(); |
13539 | unsigned Amt = RHSVal.getLimitedValue(ShiftBW - 1); |
13540 | |
13541 | |
13542 | |
13543 | if (RHSVal.isNegative()) |
13544 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal; |
13545 | else if (Amt != RHSVal) |
13546 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
13547 | << RHSVal << E->getType() << ShiftBW; |
13548 | |
13549 | if (E->getOpcode() == BO_Shl) |
13550 | Result = LHSFX.shl(Amt, &OpOverflow); |
13551 | else |
13552 | Result = LHSFX.shr(Amt, &OpOverflow); |
13553 | break; |
13554 | } |
13555 | default: |
13556 | return false; |
13557 | } |
13558 | if (OpOverflow || ConversionOverflow) { |
13559 | if (Info.checkingForUndefinedBehavior()) |
13560 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13561 | diag::warn_fixedpoint_constant_overflow) |
13562 | << Result.toString() << E->getType(); |
13563 | if (!HandleOverflow(Info, E, Result, E->getType())) |
13564 | return false; |
13565 | } |
13566 | return Success(Result, E); |
13567 | } |
13568 | |
13569 | |
13570 | |
13571 | |
13572 | |
13573 | namespace { |
13574 | class FloatExprEvaluator |
13575 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
13576 | APFloat &Result; |
13577 | public: |
13578 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
13579 | : ExprEvaluatorBaseTy(info), Result(result) {} |
13580 | |
13581 | bool Success(const APValue &V, const Expr *e) { |
13582 | Result = V.getFloat(); |
13583 | return true; |
13584 | } |
13585 | |
13586 | bool ZeroInitialization(const Expr *E) { |
13587 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
13588 | return true; |
13589 | } |
13590 | |
13591 | bool VisitCallExpr(const CallExpr *E); |
13592 | |
13593 | bool VisitUnaryOperator(const UnaryOperator *E); |
13594 | bool VisitBinaryOperator(const BinaryOperator *E); |
13595 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
13596 | bool VisitCastExpr(const CastExpr *E); |
13597 | |
13598 | bool VisitUnaryReal(const UnaryOperator *E); |
13599 | bool VisitUnaryImag(const UnaryOperator *E); |
13600 | |
13601 | |
13602 | }; |
13603 | } |
13604 | |
13605 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
13606 | assert(!E->isValueDependent()); |
13607 | assert(E->isPRValue() && E->getType()->isRealFloatingType()); |
13608 | return FloatExprEvaluator(Info, Result).Visit(E); |
13609 | } |
13610 | |
13611 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
13612 | QualType ResultTy, |
13613 | const Expr *Arg, |
13614 | bool SNaN, |
13615 | llvm::APFloat &Result) { |
13616 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
13617 | if (!S) return false; |
13618 | |
13619 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
13620 | |
13621 | llvm::APInt fill; |
13622 | |
13623 | |
13624 | if (S->getString().empty()) |
13625 | fill = llvm::APInt(32, 0); |
13626 | else if (S->getString().getAsInteger(0, fill)) |
13627 | return false; |
13628 | |
13629 | if (Context.getTargetInfo().isNan2008()) { |
13630 | if (SNaN) |
13631 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
13632 | else |
13633 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
13634 | } else { |
13635 | |
13636 | |
13637 | |
13638 | |
13639 | |
13640 | if (SNaN) |
13641 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
13642 | else |
13643 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
13644 | } |
13645 | |
13646 | return true; |
13647 | } |
13648 | |
13649 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
13650 | switch (E->getBuiltinCallee()) { |
13651 | default: |
13652 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
13653 | |
13654 | case Builtin::BI__builtin_huge_val: |
13655 | case Builtin::BI__builtin_huge_valf: |
13656 | case Builtin::BI__builtin_huge_vall: |
13657 | case Builtin::BI__builtin_huge_valf128: |
13658 | case Builtin::BI__builtin_inf: |
13659 | case Builtin::BI__builtin_inff: |
13660 | case Builtin::BI__builtin_infl: |
13661 | case Builtin::BI__builtin_inff128: { |
13662 | const llvm::fltSemantics &Sem = |
13663 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
13664 | Result = llvm::APFloat::getInf(Sem); |
13665 | return true; |
13666 | } |
13667 | |
13668 | case Builtin::BI__builtin_nans: |
13669 | case Builtin::BI__builtin_nansf: |
13670 | case Builtin::BI__builtin_nansl: |
13671 | case Builtin::BI__builtin_nansf128: |
13672 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
13673 | true, Result)) |
13674 | return Error(E); |
13675 | return true; |
13676 | |
13677 | case Builtin::BI__builtin_nan: |
13678 | case Builtin::BI__builtin_nanf: |
13679 | case Builtin::BI__builtin_nanl: |
13680 | case Builtin::BI__builtin_nanf128: |
13681 | |
13682 | |
13683 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
13684 | false, Result)) |
13685 | return Error(E); |
13686 | return true; |
13687 | |
13688 | case Builtin::BI__builtin_fabs: |
13689 | case Builtin::BI__builtin_fabsf: |
13690 | case Builtin::BI__builtin_fabsl: |
13691 | case Builtin::BI__builtin_fabsf128: |
13692 | |
13693 | |
13694 | |
13695 | |
13696 | |
13697 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
13698 | return false; |
13699 | |
13700 | if (Result.isNegative()) |
13701 | Result.changeSign(); |
13702 | return true; |
13703 | |
13704 | case Builtin::BI__arithmetic_fence: |
13705 | return EvaluateFloat(E->getArg(0), Result, Info); |
13706 | |
13707 | |
13708 | |
13709 | |
13710 | |
13711 | case Builtin::BI__builtin_copysign: |
13712 | case Builtin::BI__builtin_copysignf: |
13713 | case Builtin::BI__builtin_copysignl: |
13714 | case Builtin::BI__builtin_copysignf128: { |
13715 | APFloat RHS(0.); |
13716 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
13717 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
13718 | return false; |
13719 | Result.copySign(RHS); |
13720 | return true; |
13721 | } |
13722 | } |
13723 | } |
13724 | |
13725 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
13726 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13727 | ComplexValue CV; |
13728 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
13729 | return false; |
13730 | Result = CV.FloatReal; |
13731 | return true; |
13732 | } |
13733 | |
13734 | return Visit(E->getSubExpr()); |
13735 | } |
13736 | |
13737 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
13738 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13739 | ComplexValue CV; |
13740 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
13741 | return false; |
13742 | Result = CV.FloatImag; |
13743 | return true; |
13744 | } |
13745 | |
13746 | VisitIgnoredValue(E->getSubExpr()); |
13747 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
13748 | Result = llvm::APFloat::getZero(Sem); |
13749 | return true; |
13750 | } |
13751 | |
13752 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13753 | switch (E->getOpcode()) { |
13754 | default: return Error(E); |
13755 | case UO_Plus: |
13756 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
13757 | case UO_Minus: |
13758 | |
13759 | |
13760 | |
13761 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
13762 | return false; |
13763 | Result.changeSign(); |
13764 | return true; |
13765 | } |
13766 | } |
13767 | |
13768 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13769 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
13770 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13771 | |
13772 | APFloat RHS(0.0); |
13773 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
13774 | if (!LHSOK && !Info.noteFailure()) |
13775 | return false; |
13776 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
13777 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
13778 | } |
13779 | |
13780 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
13781 | Result = E->getValue(); |
13782 | return true; |
13783 | } |
13784 | |
13785 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13786 | const Expr* SubExpr = E->getSubExpr(); |
13787 | |
13788 | switch (E->getCastKind()) { |
13789 | default: |
13790 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13791 | |
13792 | case CK_IntegralToFloating: { |
13793 | APSInt IntResult; |
13794 | const FPOptions FPO = E->getFPFeaturesInEffect( |
13795 | Info.Ctx.getLangOpts()); |
13796 | return EvaluateInteger(SubExpr, IntResult, Info) && |
13797 | HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(), |
13798 | IntResult, E->getType(), Result); |
13799 | } |
13800 | |
13801 | case CK_FixedPointToFloating: { |
13802 | APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
13803 | if (!EvaluateFixedPoint(SubExpr, FixResult, Info)) |
13804 | return false; |
13805 | Result = |
13806 | FixResult.convertToFloat(Info.Ctx.getFloatTypeSemantics(E->getType())); |
13807 | return true; |
13808 | } |
13809 | |
13810 | case CK_FloatingCast: { |
13811 | if (!Visit(SubExpr)) |
13812 | return false; |
13813 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
13814 | Result); |
13815 | } |
13816 | |
13817 | case CK_FloatingComplexToReal: { |
13818 | ComplexValue V; |
13819 | if (!EvaluateComplex(SubExpr, V, Info)) |
13820 | return false; |
13821 | Result = V.getComplexFloatReal(); |
13822 | return true; |
13823 | } |
13824 | } |
13825 | } |
13826 | |
13827 | |
13828 | |
13829 | |
13830 | |
13831 | namespace { |
13832 | class ComplexExprEvaluator |
13833 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
13834 | ComplexValue &Result; |
13835 | |
13836 | public: |
13837 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
13838 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
13839 | |
13840 | bool Success(const APValue &V, const Expr *e) { |
13841 | Result.setFrom(V); |
13842 | return true; |
13843 | } |
13844 | |
13845 | bool ZeroInitialization(const Expr *E); |
13846 | |
13847 | |
13848 | |
13849 | |
13850 | |
13851 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
13852 | bool VisitCastExpr(const CastExpr *E); |
13853 | bool VisitBinaryOperator(const BinaryOperator *E); |
13854 | bool VisitUnaryOperator(const UnaryOperator *E); |
13855 | bool VisitInitListExpr(const InitListExpr *E); |
13856 | bool VisitCallExpr(const CallExpr *E); |
13857 | }; |
13858 | } |
13859 | |
13860 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
13861 | EvalInfo &Info) { |
13862 | assert(!E->isValueDependent()); |
13863 | assert(E->isPRValue() && E->getType()->isAnyComplexType()); |
13864 | return ComplexExprEvaluator(Info, Result).Visit(E); |
13865 | } |
13866 | |
13867 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
13868 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
13869 | if (ElemTy->isRealFloatingType()) { |
13870 | Result.makeComplexFloat(); |
13871 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
13872 | Result.FloatReal = Zero; |
13873 | Result.FloatImag = Zero; |
13874 | } else { |
13875 | Result.makeComplexInt(); |
13876 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
13877 | Result.IntReal = Zero; |
13878 | Result.IntImag = Zero; |
13879 | } |
13880 | return true; |
13881 | } |
13882 | |
13883 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
13884 | const Expr* SubExpr = E->getSubExpr(); |
13885 | |
13886 | if (SubExpr->getType()->isRealFloatingType()) { |
13887 | Result.makeComplexFloat(); |
13888 | APFloat &Imag = Result.FloatImag; |
13889 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
13890 | return false; |
13891 | |
13892 | Result.FloatReal = APFloat(Imag.getSemantics()); |
13893 | return true; |
13894 | } else { |
13895 | assert(SubExpr->getType()->isIntegerType() && |
13896 | "Unexpected imaginary literal."); |
13897 | |
13898 | Result.makeComplexInt(); |
13899 | APSInt &Imag = Result.IntImag; |
13900 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
13901 | return false; |
13902 | |
13903 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
13904 | return true; |
13905 | } |
13906 | } |
13907 | |
13908 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13909 | |
13910 | switch (E->getCastKind()) { |
13911 | case CK_BitCast: |
13912 | case CK_BaseToDerived: |
13913 | case CK_DerivedToBase: |
13914 | case CK_UncheckedDerivedToBase: |
13915 | case CK_Dynamic: |
13916 | case CK_ToUnion: |
13917 | case CK_ArrayToPointerDecay: |
13918 | case CK_FunctionToPointerDecay: |
13919 | case CK_NullToPointer: |
13920 | case CK_NullToMemberPointer: |
13921 | case CK_BaseToDerivedMemberPointer: |
13922 | case CK_DerivedToBaseMemberPointer: |
13923 | case CK_MemberPointerToBoolean: |
13924 | case CK_ReinterpretMemberPointer: |
13925 | case CK_ConstructorConversion: |
13926 | case CK_IntegralToPointer: |
13927 | case CK_PointerToIntegral: |
13928 | case CK_PointerToBoolean: |
13929 | case CK_ToVoid: |
13930 | case CK_VectorSplat: |
13931 | case CK_IntegralCast: |
13932 | case CK_BooleanToSignedIntegral: |
13933 | case CK_IntegralToBoolean: |
13934 | case CK_IntegralToFloating: |
13935 | case CK_FloatingToIntegral: |
13936 | case CK_FloatingToBoolean: |
13937 | case CK_FloatingCast: |
13938 | case CK_CPointerToObjCPointerCast: |
13939 | case CK_BlockPointerToObjCPointerCast: |
13940 | case CK_AnyPointerToBlockPointerCast: |
13941 | case CK_ObjCObjectLValueCast: |
13942 | case CK_FloatingComplexToReal: |
13943 | case CK_FloatingComplexToBoolean: |
13944 | case CK_IntegralComplexToReal: |
13945 | case CK_IntegralComplexToBoolean: |
13946 | case CK_ARCProduceObject: |
13947 | case CK_ARCConsumeObject: |
13948 | case CK_ARCReclaimReturnedObject: |
13949 | case CK_ARCExtendBlockObject: |
13950 | case CK_CopyAndAutoreleaseBlockObject: |
13951 | case CK_BuiltinFnToFnPtr: |
13952 | case CK_ZeroToOCLOpaqueType: |
13953 | case CK_NonAtomicToAtomic: |
13954 | case CK_AddressSpaceConversion: |
13955 | case CK_IntToOCLSampler: |
13956 | case CK_FloatingToFixedPoint: |
13957 | case CK_FixedPointToFloating: |
13958 | case CK_FixedPointCast: |
13959 | case CK_FixedPointToBoolean: |
13960 | case CK_FixedPointToIntegral: |
13961 | case CK_IntegralToFixedPoint: |
13962 | case CK_MatrixCast: |
13963 | llvm_unreachable("invalid cast kind for complex value"); |
13964 | |
13965 | case CK_LValueToRValue: |
13966 | case CK_AtomicToNonAtomic: |
13967 | case CK_NoOp: |
13968 | case CK_LValueToRValueBitCast: |
13969 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13970 | |
13971 | case CK_Dependent: |
13972 | case CK_LValueBitCast: |
13973 | case CK_UserDefinedConversion: |
13974 | return Error(E); |
13975 | |
13976 | case CK_FloatingRealToComplex: { |
13977 | APFloat &Real = Result.FloatReal; |
13978 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
13979 | return false; |
13980 | |
13981 | Result.makeComplexFloat(); |
13982 | Result.FloatImag = APFloat(Real.getSemantics()); |
13983 | return true; |
13984 | } |
13985 | |
13986 | case CK_FloatingComplexCast: { |
13987 | if (!Visit(E->getSubExpr())) |
13988 | return false; |
13989 | |
13990 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
13991 | QualType From |
13992 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
13993 | |
13994 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
13995 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
13996 | } |
13997 | |
13998 | case CK_FloatingComplexToIntegralComplex: { |
13999 | if (!Visit(E->getSubExpr())) |
14000 | return false; |
14001 | |
14002 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14003 | QualType From |
14004 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14005 | Result.makeComplexInt(); |
14006 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
14007 | To, Result.IntReal) && |
14008 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
14009 | To, Result.IntImag); |
14010 | } |
14011 | |
14012 | case CK_IntegralRealToComplex: { |
14013 | APSInt &Real = Result.IntReal; |
14014 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
14015 | return false; |
14016 | |
14017 | Result.makeComplexInt(); |
14018 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
14019 | return true; |
14020 | } |
14021 | |
14022 | case CK_IntegralComplexCast: { |
14023 | if (!Visit(E->getSubExpr())) |
14024 | return false; |
14025 | |
14026 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14027 | QualType From |
14028 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14029 | |
14030 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
14031 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
14032 | return true; |
14033 | } |
14034 | |
14035 | case CK_IntegralComplexToFloatingComplex: { |
14036 | if (!Visit(E->getSubExpr())) |
14037 | return false; |
14038 | |
14039 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14040 | Info.Ctx.getLangOpts()); |
14041 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14042 | QualType From |
14043 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14044 | Result.makeComplexFloat(); |
14045 | return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal, |
14046 | To, Result.FloatReal) && |
14047 | HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag, |
14048 | To, Result.FloatImag); |
14049 | } |
14050 | } |
14051 | |
14052 | llvm_unreachable("unknown cast resulting in complex value"); |
14053 | } |
14054 | |
14055 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14056 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14057 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14058 | |
14059 | |
14060 | |
14061 | bool LHSReal = false, RHSReal = false; |
14062 | |
14063 | bool LHSOK; |
14064 | if (E->getLHS()->getType()->isRealFloatingType()) { |
14065 | LHSReal = true; |
14066 | APFloat &Real = Result.FloatReal; |
14067 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
14068 | if (LHSOK) { |
14069 | Result.makeComplexFloat(); |
14070 | Result.FloatImag = APFloat(Real.getSemantics()); |
14071 | } |
14072 | } else { |
14073 | LHSOK = Visit(E->getLHS()); |
14074 | } |
14075 | if (!LHSOK && !Info.noteFailure()) |
14076 | return false; |
14077 | |
14078 | ComplexValue RHS; |
14079 | if (E->getRHS()->getType()->isRealFloatingType()) { |
14080 | RHSReal = true; |
14081 | APFloat &Real = RHS.FloatReal; |
14082 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
14083 | return false; |
14084 | RHS.makeComplexFloat(); |
14085 | RHS.FloatImag = APFloat(Real.getSemantics()); |
14086 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
14087 | return false; |
14088 | |
14089 | assert(!(LHSReal && RHSReal) && |
14090 | "Cannot have both operands of a complex operation be real."); |
14091 | switch (E->getOpcode()) { |
14092 | default: return Error(E); |
14093 | case BO_Add: |
14094 | if (Result.isComplexFloat()) { |
14095 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
14096 | APFloat::rmNearestTiesToEven); |
14097 | if (LHSReal) |
14098 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14099 | else if (!RHSReal) |
14100 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
14101 | APFloat::rmNearestTiesToEven); |
14102 | } else { |
14103 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
14104 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
14105 | } |
14106 | break; |
14107 | case BO_Sub: |
14108 | if (Result.isComplexFloat()) { |
14109 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
14110 | APFloat::rmNearestTiesToEven); |
14111 | if (LHSReal) { |
14112 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14113 | Result.getComplexFloatImag().changeSign(); |
14114 | } else if (!RHSReal) { |
14115 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
14116 | APFloat::rmNearestTiesToEven); |
14117 | } |
14118 | } else { |
14119 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
14120 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
14121 | } |
14122 | break; |
14123 | case BO_Mul: |
14124 | if (Result.isComplexFloat()) { |
14125 | |
14126 | |
14127 | |
14128 | |
14129 | ComplexValue LHS = Result; |
14130 | APFloat &A = LHS.getComplexFloatReal(); |
14131 | APFloat &B = LHS.getComplexFloatImag(); |
14132 | APFloat &C = RHS.getComplexFloatReal(); |
14133 | APFloat &D = RHS.getComplexFloatImag(); |
14134 | APFloat &ResR = Result.getComplexFloatReal(); |
14135 | APFloat &ResI = Result.getComplexFloatImag(); |
14136 | if (LHSReal) { |
14137 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
14138 | ResR = A * C; |
14139 | ResI = A * D; |
14140 | } else if (RHSReal) { |
14141 | ResR = C * A; |
14142 | ResI = C * B; |
14143 | } else { |
14144 | |
14145 | |
14146 | APFloat AC = A * C; |
14147 | APFloat BD = B * D; |
14148 | APFloat AD = A * D; |
14149 | APFloat BC = B * C; |
14150 | ResR = AC - BD; |
14151 | ResI = AD + BC; |
14152 | if (ResR.isNaN() && ResI.isNaN()) { |
14153 | bool Recalc = false; |
14154 | if (A.isInfinity() || B.isInfinity()) { |
14155 | A = APFloat::copySign( |
14156 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
14157 | B = APFloat::copySign( |
14158 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
14159 | if (C.isNaN()) |
14160 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
14161 | if (D.isNaN()) |
14162 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
14163 | Recalc = true; |
14164 | } |
14165 | if (C.isInfinity() || D.isInfinity()) { |
14166 | C = APFloat::copySign( |
14167 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
14168 | D = APFloat::copySign( |
14169 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
14170 | if (A.isNaN()) |
14171 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
14172 | if (B.isNaN()) |
14173 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
14174 | Recalc = true; |
14175 | } |
14176 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
14177 | AD.isInfinity() || BC.isInfinity())) { |
14178 | if (A.isNaN()) |
14179 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
14180 | if (B.isNaN()) |
14181 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
14182 | if (C.isNaN()) |
14183 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
14184 | if (D.isNaN()) |
14185 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
14186 | Recalc = true; |
14187 | } |
14188 | if (Recalc) { |
14189 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
14190 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
14191 | } |
14192 | } |
14193 | } |
14194 | } else { |
14195 | ComplexValue LHS = Result; |
14196 | Result.getComplexIntReal() = |
14197 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
14198 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
14199 | Result.getComplexIntImag() = |
14200 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
14201 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
14202 | } |
14203 | break; |
14204 | case BO_Div: |
14205 | if (Result.isComplexFloat()) { |
14206 | |
14207 | |
14208 | |
14209 | |
14210 | ComplexValue LHS = Result; |
14211 | APFloat &A = LHS.getComplexFloatReal(); |
14212 | APFloat &B = LHS.getComplexFloatImag(); |
14213 | APFloat &C = RHS.getComplexFloatReal(); |
14214 | APFloat &D = RHS.getComplexFloatImag(); |
14215 | APFloat &ResR = Result.getComplexFloatReal(); |
14216 | APFloat &ResI = Result.getComplexFloatImag(); |
14217 | if (RHSReal) { |
14218 | ResR = A / C; |
14219 | ResI = B / C; |
14220 | } else { |
14221 | if (LHSReal) { |
14222 | |
14223 | B = APFloat::getZero(A.getSemantics()); |
14224 | } |
14225 | int DenomLogB = 0; |
14226 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
14227 | if (MaxCD.isFinite()) { |
14228 | DenomLogB = ilogb(MaxCD); |
14229 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
14230 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
14231 | } |
14232 | APFloat Denom = C * C + D * D; |
14233 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
14234 | APFloat::rmNearestTiesToEven); |
14235 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
14236 | APFloat::rmNearestTiesToEven); |
14237 | if (ResR.isNaN() && ResI.isNaN()) { |
14238 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
14239 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
14240 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
14241 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
14242 | D.isFinite()) { |
14243 | A = APFloat::copySign( |
14244 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
14245 | B = APFloat::copySign( |
14246 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
14247 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
14248 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
14249 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
14250 | C = APFloat::copySign( |
14251 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
14252 | D = APFloat::copySign( |
14253 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
14254 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
14255 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
14256 | } |
14257 | } |
14258 | } |
14259 | } else { |
14260 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
14261 | return Error(E, diag::note_expr_divide_by_zero); |
14262 | |
14263 | ComplexValue LHS = Result; |
14264 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
14265 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
14266 | Result.getComplexIntReal() = |
14267 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
14268 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
14269 | Result.getComplexIntImag() = |
14270 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
14271 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
14272 | } |
14273 | break; |
14274 | } |
14275 | |
14276 | return true; |
14277 | } |
14278 | |
14279 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14280 | |
14281 | if (!Visit(E->getSubExpr())) |
14282 | return false; |
14283 | |
14284 | switch (E->getOpcode()) { |
14285 | default: |
14286 | return Error(E); |
14287 | case UO_Extension: |
14288 | return true; |
14289 | case UO_Plus: |
14290 | |
14291 | return true; |
14292 | case UO_Minus: |
14293 | if (Result.isComplexFloat()) { |
14294 | Result.getComplexFloatReal().changeSign(); |
14295 | Result.getComplexFloatImag().changeSign(); |
14296 | } |
14297 | else { |
14298 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
14299 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
14300 | } |
14301 | return true; |
14302 | case UO_Not: |
14303 | if (Result.isComplexFloat()) |
14304 | Result.getComplexFloatImag().changeSign(); |
14305 | else |
14306 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
14307 | return true; |
14308 | } |
14309 | } |
14310 | |
14311 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
14312 | if (E->getNumInits() == 2) { |
14313 | if (E->getType()->isComplexType()) { |
14314 | Result.makeComplexFloat(); |
14315 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
14316 | return false; |
14317 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
14318 | return false; |
14319 | } else { |
14320 | Result.makeComplexInt(); |
14321 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
14322 | return false; |
14323 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
14324 | return false; |
14325 | } |
14326 | return true; |
14327 | } |
14328 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
14329 | } |
14330 | |
14331 | bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) { |
14332 | switch (E->getBuiltinCallee()) { |
14333 | case Builtin::BI__builtin_complex: |
14334 | Result.makeComplexFloat(); |
14335 | if (!EvaluateFloat(E->getArg(0), Result.FloatReal, Info)) |
14336 | return false; |
14337 | if (!EvaluateFloat(E->getArg(1), Result.FloatImag, Info)) |
14338 | return false; |
14339 | return true; |
14340 | |
14341 | default: |
14342 | break; |
14343 | } |
14344 | |
14345 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14346 | } |
14347 | |
14348 | |
14349 | |
14350 | |
14351 | |
14352 | |
14353 | namespace { |
14354 | class AtomicExprEvaluator : |
14355 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
14356 | const LValue *This; |
14357 | APValue &Result; |
14358 | public: |
14359 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
14360 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
14361 | |
14362 | bool Success(const APValue &V, const Expr *E) { |
14363 | Result = V; |
14364 | return true; |
14365 | } |
14366 | |
14367 | bool ZeroInitialization(const Expr *E) { |
14368 | ImplicitValueInitExpr VIE( |
14369 | E->getType()->castAs<AtomicType>()->getValueType()); |
14370 | |
14371 | |
14372 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
14373 | : Evaluate(Result, Info, &VIE); |
14374 | } |
14375 | |
14376 | bool VisitCastExpr(const CastExpr *E) { |
14377 | switch (E->getCastKind()) { |
14378 | default: |
14379 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14380 | case CK_NonAtomicToAtomic: |
14381 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
14382 | : Evaluate(Result, Info, E->getSubExpr()); |
14383 | } |
14384 | } |
14385 | }; |
14386 | } |
14387 | |
14388 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
14389 | EvalInfo &Info) { |
14390 | assert(!E->isValueDependent()); |
14391 | assert(E->isPRValue() && E->getType()->isAtomicType()); |
14392 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
14393 | } |
14394 | |
14395 | |
14396 | |
14397 | |
14398 | |
14399 | |
14400 | namespace { |
14401 | class VoidExprEvaluator |
14402 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
14403 | public: |
14404 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
14405 | |
14406 | bool Success(const APValue &V, const Expr *e) { return true; } |
14407 | |
14408 | bool ZeroInitialization(const Expr *E) { return true; } |
14409 | |
14410 | bool VisitCastExpr(const CastExpr *E) { |
14411 | switch (E->getCastKind()) { |
14412 | default: |
14413 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14414 | case CK_ToVoid: |
14415 | VisitIgnoredValue(E->getSubExpr()); |
14416 | return true; |
14417 | } |
14418 | } |
14419 | |
14420 | bool VisitCallExpr(const CallExpr *E) { |
14421 | switch (E->getBuiltinCallee()) { |
14422 | case Builtin::BI__assume: |
14423 | case Builtin::BI__builtin_assume: |
14424 | |
14425 | return true; |
14426 | |
14427 | case Builtin::BI__builtin_operator_delete: |
14428 | return HandleOperatorDeleteCall(Info, E); |
14429 | |
14430 | default: |
14431 | break; |
14432 | } |
14433 | |
14434 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14435 | } |
14436 | |
14437 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
14438 | }; |
14439 | } |
14440 | |
14441 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
14442 | |
14443 | if (Info.SpeculativeEvaluationDepth) |
14444 | return false; |
14445 | |
14446 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
14447 | if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) { |
14448 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
14449 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
14450 | return false; |
14451 | } |
14452 | |
14453 | const Expr *Arg = E->getArgument(); |
14454 | |
14455 | LValue Pointer; |
14456 | if (!EvaluatePointer(Arg, Pointer, Info)) |
14457 | return false; |
14458 | if (Pointer.Designator.Invalid) |
14459 | return false; |
14460 | |
14461 | |
14462 | if (Pointer.isNullPointer()) { |
14463 | |
14464 | |
14465 | |
14466 | if (!Info.getLangOpts().CPlusPlus20) |
14467 | Info.CCEDiag(E, diag::note_constexpr_new); |
14468 | return true; |
14469 | } |
14470 | |
14471 | Optional<DynAlloc *> Alloc = CheckDeleteKind( |
14472 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
14473 | if (!Alloc) |
14474 | return false; |
14475 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
14476 | |
14477 | |
14478 | |
14479 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
14480 | !hasVirtualDestructor(Arg->getType()->getPointeeType())) { |
14481 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
14482 | << Arg->getType()->getPointeeType() << AllocType; |
14483 | return false; |
14484 | } |
14485 | |
14486 | |
14487 | |
14488 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
14489 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(AllocType); |
14490 | if (VirtualDelete && |
14491 | !VirtualDelete->isReplaceableGlobalAllocationFunction()) { |
14492 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
14493 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
14494 | return false; |
14495 | } |
14496 | } |
14497 | |
14498 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
14499 | (*Alloc)->Value, AllocType)) |
14500 | return false; |
14501 | |
14502 | if (!Info.HeapAllocs.erase(Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
14503 | |
14504 | |
14505 | |
14506 | |
14507 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
14508 | return false; |
14509 | } |
14510 | |
14511 | return true; |
14512 | } |
14513 | |
14514 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
14515 | assert(!E->isValueDependent()); |
14516 | assert(E->isPRValue() && E->getType()->isVoidType()); |
14517 | return VoidExprEvaluator(Info).Visit(E); |
14518 | } |
14519 | |
14520 | |
14521 | |
14522 | |
14523 | |
14524 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
14525 | assert(!E->isValueDependent()); |
14526 | |
14527 | |
14528 | QualType T = E->getType(); |
14529 | if (E->isGLValue() || T->isFunctionType()) { |
14530 | LValue LV; |
14531 | if (!EvaluateLValue(E, LV, Info)) |
14532 | return false; |
14533 | LV.moveInto(Result); |
14534 | } else if (T->isVectorType()) { |
14535 | if (!EvaluateVector(E, Result, Info)) |
14536 | return false; |
14537 | } else if (T->isIntegralOrEnumerationType()) { |
14538 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
14539 | return false; |
14540 | } else if (T->hasPointerRepresentation()) { |
14541 | LValue LV; |
14542 | if (!EvaluatePointer(E, LV, Info)) |
14543 | return false; |
14544 | LV.moveInto(Result); |
14545 | } else if (T->isRealFloatingType()) { |
14546 | llvm::APFloat F(0.0); |
14547 | if (!EvaluateFloat(E, F, Info)) |
14548 | return false; |
14549 | Result = APValue(F); |
14550 | } else if (T->isAnyComplexType()) { |
14551 | ComplexValue C; |
14552 | if (!EvaluateComplex(E, C, Info)) |
14553 | return false; |
14554 | C.moveInto(Result); |
14555 | } else if (T->isFixedPointType()) { |
14556 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
14557 | } else if (T->isMemberPointerType()) { |
14558 | MemberPtr P; |
14559 | if (!EvaluateMemberPointer(E, P, Info)) |
14560 | return false; |
14561 | P.moveInto(Result); |
14562 | return true; |
14563 | } else if (T->isArrayType()) { |
14564 | LValue LV; |
14565 | APValue &Value = |
14566 | Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV); |
14567 | if (!EvaluateArray(E, LV, Value, Info)) |
14568 | return false; |
14569 | Result = Value; |
14570 | } else if (T->isRecordType()) { |
14571 | LValue LV; |
14572 | APValue &Value = |
14573 | Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV); |
14574 | if (!EvaluateRecord(E, LV, Value, Info)) |
14575 | return false; |
14576 | Result = Value; |
14577 | } else if (T->isVoidType()) { |
14578 | if (!Info.getLangOpts().CPlusPlus11) |
14579 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
14580 | << E->getType(); |
14581 | if (!EvaluateVoid(E, Info)) |
14582 | return false; |
14583 | } else if (T->isAtomicType()) { |
14584 | QualType Unqual = T.getAtomicUnqualifiedType(); |
14585 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
14586 | LValue LV; |
14587 | APValue &Value = Info.CurrentCall->createTemporary( |
14588 | E, Unqual, ScopeKind::FullExpression, LV); |
14589 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
14590 | return false; |
14591 | } else { |
14592 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
14593 | return false; |
14594 | } |
14595 | } else if (Info.getLangOpts().CPlusPlus11) { |
14596 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
14597 | return false; |
14598 | } else { |
14599 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
14600 | return false; |
14601 | } |
14602 | |
14603 | return true; |
14604 | } |
14605 | |
14606 | |
14607 | |
14608 | |
14609 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
14610 | const Expr *E, bool AllowNonLiteralTypes) { |
14611 | assert(!E->isValueDependent()); |
14612 | |
14613 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
14614 | return false; |
14615 | |
14616 | if (E->isPRValue()) { |
14617 | |
14618 | |
14619 | QualType T = E->getType(); |
14620 | if (T->isArrayType()) |
14621 | return EvaluateArray(E, This, Result, Info); |
14622 | else if (T->isRecordType()) |
14623 | return EvaluateRecord(E, This, Result, Info); |
14624 | else if (T->isAtomicType()) { |
14625 | QualType Unqual = T.getAtomicUnqualifiedType(); |
14626 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
14627 | return EvaluateAtomic(E, &This, Result, Info); |
14628 | } |
14629 | } |
14630 | |
14631 | |
14632 | return Evaluate(Result, Info, E); |
14633 | } |
14634 | |
14635 | |
14636 | |
14637 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
14638 | assert(!E->isValueDependent()); |
14639 | if (Info.EnableNewConstInterp) { |
14640 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Info, E, Result)) |
14641 | return false; |
14642 | } else { |
14643 | if (E->getType().isNull()) |
14644 | return false; |
14645 | |
14646 | if (!CheckLiteralType(Info, E)) |
14647 | return false; |
14648 | |
14649 | if (!::Evaluate(Result, Info, E)) |
14650 | return false; |
14651 | |
14652 | if (E->isGLValue()) { |
14653 | LValue LV; |
14654 | LV.setFrom(Info.Ctx, Result); |
14655 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
14656 | return false; |
14657 | } |
14658 | } |
14659 | |
14660 | |
14661 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
14662 | ConstantExprKind::Normal) && |
14663 | CheckMemoryLeaks(Info); |
14664 | } |
14665 | |
14666 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
14667 | const ASTContext &Ctx, bool &IsConst) { |
14668 | |
14669 | |
14670 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
14671 | Result.Val = APValue(APSInt(L->getValue(), |
14672 | L->getType()->isUnsignedIntegerType())); |
14673 | IsConst = true; |
14674 | return true; |
14675 | } |
14676 | |
14677 | |
14678 | |
14679 | if (Exp->getType().isNull()) { |
14680 | IsConst = false; |
14681 | return true; |
14682 | } |
14683 | |
14684 | |
14685 | |
14686 | if (Exp->isPRValue() && |
14687 | (Exp->getType()->isArrayType() || Exp->getType()->isRecordType()) && |
14688 | !Ctx.getLangOpts().CPlusPlus11) { |
14689 | IsConst = false; |
14690 | return true; |
14691 | } |
14692 | return false; |
14693 | } |
14694 | |
14695 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
14696 | Expr::SideEffectsKind SEK) { |
14697 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
14698 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
14699 | } |
14700 | |
14701 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
14702 | const ASTContext &Ctx, EvalInfo &Info) { |
14703 | assert(!E->isValueDependent()); |
14704 | bool IsConst; |
14705 | if (FastEvaluateAsRValue(E, Result, Ctx, IsConst)) |
14706 | return IsConst; |
14707 | |
14708 | return EvaluateAsRValue(Info, E, Result.Val); |
14709 | } |
14710 | |
14711 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
14712 | const ASTContext &Ctx, |
14713 | Expr::SideEffectsKind AllowSideEffects, |
14714 | EvalInfo &Info) { |
14715 | assert(!E->isValueDependent()); |
14716 | if (!E->getType()->isIntegralOrEnumerationType()) |
14717 | return false; |
14718 | |
14719 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) || |
14720 | !ExprResult.Val.isInt() || |
14721 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14722 | return false; |
14723 | |
14724 | return true; |
14725 | } |
14726 | |
14727 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
14728 | const ASTContext &Ctx, |
14729 | Expr::SideEffectsKind AllowSideEffects, |
14730 | EvalInfo &Info) { |
14731 | assert(!E->isValueDependent()); |
14732 | if (!E->getType()->isFixedPointType()) |
14733 | return false; |
14734 | |
14735 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info)) |
14736 | return false; |
14737 | |
14738 | if (!ExprResult.Val.isFixedPoint() || |
14739 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14740 | return false; |
14741 | |
14742 | return true; |
14743 | } |
14744 | |
14745 | |
14746 | |
14747 | |
14748 | |
14749 | |
14750 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
14751 | bool InConstantContext) const { |
14752 | assert(!isValueDependent() && |
14753 | "Expression evaluator can't be called on a dependent expression."); |
14754 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14755 | Info.InConstantContext = InConstantContext; |
14756 | return ::EvaluateAsRValue(this, Result, Ctx, Info); |
14757 | } |
14758 | |
14759 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
14760 | bool InConstantContext) const { |
14761 | assert(!isValueDependent() && |
14762 | "Expression evaluator can't be called on a dependent expression."); |
14763 | EvalResult Scratch; |
14764 | return EvaluateAsRValue(Scratch, Ctx, InConstantContext) && |
14765 | HandleConversionToBool(Scratch.Val, Result); |
14766 | } |
14767 | |
14768 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
14769 | SideEffectsKind AllowSideEffects, |
14770 | bool InConstantContext) const { |
14771 | assert(!isValueDependent() && |
14772 | "Expression evaluator can't be called on a dependent expression."); |
14773 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14774 | Info.InConstantContext = InConstantContext; |
14775 | return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info); |
14776 | } |
14777 | |
14778 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
14779 | SideEffectsKind AllowSideEffects, |
14780 | bool InConstantContext) const { |
14781 | assert(!isValueDependent() && |
14782 | "Expression evaluator can't be called on a dependent expression."); |
14783 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14784 | Info.InConstantContext = InConstantContext; |
14785 | return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info); |
14786 | } |
14787 | |
14788 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
14789 | SideEffectsKind AllowSideEffects, |
14790 | bool InConstantContext) const { |
14791 | assert(!isValueDependent() && |
14792 | "Expression evaluator can't be called on a dependent expression."); |
14793 | |
14794 | if (!getType()->isRealFloatingType()) |
14795 | return false; |
14796 | |
14797 | EvalResult ExprResult; |
14798 | if (!EvaluateAsRValue(ExprResult, Ctx, InConstantContext) || |
14799 | !ExprResult.Val.isFloat() || |
14800 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14801 | return false; |
14802 | |
14803 | Result = ExprResult.Val.getFloat(); |
14804 | return true; |
14805 | } |
14806 | |
14807 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
14808 | bool InConstantContext) const { |
14809 | assert(!isValueDependent() && |
14810 | "Expression evaluator can't be called on a dependent expression."); |
14811 | |
14812 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
14813 | Info.InConstantContext = InConstantContext; |
14814 | LValue LV; |
14815 | CheckedTemporaries CheckedTemps; |
14816 | if (!EvaluateLValue(this, LV, Info) || !Info.discardCleanups() || |
14817 | Result.HasSideEffects || |
14818 | !CheckLValueConstantExpression(Info, getExprLoc(), |
14819 | Ctx.getLValueReferenceType(getType()), LV, |
14820 | ConstantExprKind::Normal, CheckedTemps)) |
14821 | return false; |
14822 | |
14823 | LV.moveInto(Result.Val); |
14824 | return true; |
14825 | } |
14826 | |
14827 | static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base, |
14828 | APValue DestroyedValue, QualType Type, |
14829 | SourceLocation Loc, Expr::EvalStatus &EStatus, |
14830 | bool IsConstantDestruction) { |
14831 | EvalInfo Info(Ctx, EStatus, |
14832 | IsConstantDestruction ? EvalInfo::EM_ConstantExpression |
14833 | : EvalInfo::EM_ConstantFold); |
14834 | Info.setEvaluatingDecl(Base, DestroyedValue, |
14835 | EvalInfo::EvaluatingDeclKind::Dtor); |
14836 | Info.InConstantContext = IsConstantDestruction; |
14837 | |
14838 | LValue LVal; |
14839 | LVal.set(Base); |
14840 | |
14841 | if (!HandleDestruction(Info, Loc, Base, DestroyedValue, Type) || |
14842 | EStatus.HasSideEffects) |
14843 | return false; |
14844 | |
14845 | if (!Info.discardCleanups()) |
14846 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14847 | |
14848 | return true; |
14849 | } |
14850 | |
14851 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, |
14852 | ConstantExprKind Kind) const { |
14853 | assert(!isValueDependent() && |
14854 | "Expression evaluator can't be called on a dependent expression."); |
14855 | |
14856 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
14857 | EvalInfo Info(Ctx, Result, EM); |
14858 | Info.InConstantContext = true; |
14859 | |
14860 | |
14861 | QualType T = getType(); |
14862 | if (Kind == ConstantExprKind::ClassTemplateArgument) |
14863 | T.addConst(); |
14864 | |
14865 | |
14866 | |
14867 | |
14868 | MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true); |
14869 | APValue::LValueBase Base(&BaseMTE); |
14870 | |
14871 | Info.setEvaluatingDecl(Base, Result.Val); |
14872 | LValue LVal; |
14873 | LVal.set(Base); |
14874 | |
14875 | if (!::EvaluateInPlace(Result.Val, Info, LVal, this) || Result.HasSideEffects) |
14876 | return false; |
14877 | |
14878 | if (!Info.discardCleanups()) |
14879 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14880 | |
14881 | if (!CheckConstantExpression(Info, getExprLoc(), getStorageType(Ctx, this), |
14882 | Result.Val, Kind)) |
14883 | return false; |
14884 | if (!CheckMemoryLeaks(Info)) |
14885 | return false; |
14886 | |
14887 | |
14888 | |
14889 | if (Kind == ConstantExprKind::ClassTemplateArgument && |
14890 | (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result, |
14891 | true) || |
14892 | Result.HasSideEffects)) { |
14893 | |
14894 | |
14895 | return false; |
14896 | } |
14897 | |
14898 | return true; |
14899 | } |
14900 | |
14901 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
14902 | const VarDecl *VD, |
14903 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
14904 | bool IsConstantInitialization) const { |
14905 | assert(!isValueDependent() && |
14906 | "Expression evaluator can't be called on a dependent expression."); |
14907 | |
14908 | |
14909 | |
14910 | if (isPRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
14911 | !Ctx.getLangOpts().CPlusPlus11) |
14912 | return false; |
14913 | |
14914 | Expr::EvalStatus EStatus; |
14915 | EStatus.Diag = &Notes; |
14916 | |
14917 | EvalInfo Info(Ctx, EStatus, |
14918 | (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11) |
14919 | ? EvalInfo::EM_ConstantExpression |
14920 | : EvalInfo::EM_ConstantFold); |
14921 | Info.setEvaluatingDecl(VD, Value); |
14922 | Info.InConstantContext = IsConstantInitialization; |
14923 | |
14924 | SourceLocation DeclLoc = VD->getLocation(); |
14925 | QualType DeclTy = VD->getType(); |
14926 | |
14927 | if (Info.EnableNewConstInterp) { |
14928 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
14929 | if (!InterpCtx.evaluateAsInitializer(Info, VD, Value)) |
14930 | return false; |
14931 | } else { |
14932 | LValue LVal; |
14933 | LVal.set(VD); |
14934 | |
14935 | if (!EvaluateInPlace(Value, Info, LVal, this, |
14936 | true) || |
14937 | EStatus.HasSideEffects) |
14938 | return false; |
14939 | |
14940 | |
14941 | |
14942 | Info.performLifetimeExtension(); |
14943 | |
14944 | if (!Info.discardCleanups()) |
14945 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14946 | } |
14947 | return CheckConstantExpression(Info, DeclLoc, DeclTy, Value, |
14948 | ConstantExprKind::Normal) && |
14949 | CheckMemoryLeaks(Info); |
14950 | } |
14951 | |
14952 | bool VarDecl::evaluateDestruction( |
14953 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
14954 | Expr::EvalStatus EStatus; |
14955 | EStatus.Diag = &Notes; |
14956 | |
14957 | |
14958 | |
14959 | bool IsConstantDestruction = hasConstantInitialization(); |
14960 | |
14961 | |
14962 | |
14963 | |
14964 | APValue DestroyedValue; |
14965 | if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent()) |
14966 | DestroyedValue = *getEvaluatedValue(); |
14967 | else if (!getDefaultInitValue(getType(), DestroyedValue)) |
14968 | return false; |
14969 | |
14970 | if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue), |
14971 | getType(), getLocation(), EStatus, |
14972 | IsConstantDestruction) || |
14973 | EStatus.HasSideEffects) |
14974 | return false; |
14975 | |
14976 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
14977 | return true; |
14978 | } |
14979 | |
14980 | |
14981 | |
14982 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
14983 | assert(!isValueDependent() && |
14984 | "Expression evaluator can't be called on a dependent expression."); |
14985 | |
14986 | EvalResult Result; |
14987 | return EvaluateAsRValue(Result, Ctx, true) && |
14988 | !hasUnacceptableSideEffect(Result, SEK); |
14989 | } |
14990 | |
14991 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
14992 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
14993 | assert(!isValueDependent() && |
14994 | "Expression evaluator can't be called on a dependent expression."); |
14995 | |
14996 | EvalResult EVResult; |
14997 | EVResult.Diag = Diag; |
14998 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
14999 | Info.InConstantContext = true; |
15000 | |
15001 | bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info); |
15002 | (void)Result; |
15003 | assert(Result && "Could not evaluate expression"); |
15004 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
15005 | |
15006 | return EVResult.Val.getInt(); |
15007 | } |
15008 | |
15009 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
15010 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
15011 | assert(!isValueDependent() && |
15012 | "Expression evaluator can't be called on a dependent expression."); |
15013 | |
15014 | EvalResult EVResult; |
15015 | EVResult.Diag = Diag; |
15016 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
15017 | Info.InConstantContext = true; |
15018 | Info.CheckingForUndefinedBehavior = true; |
15019 | |
15020 | bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val); |
15021 | (void)Result; |
15022 | assert(Result && "Could not evaluate expression"); |
15023 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
15024 | |
15025 | return EVResult.Val.getInt(); |
15026 | } |
15027 | |
15028 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
15029 | assert(!isValueDependent() && |
15030 | "Expression evaluator can't be called on a dependent expression."); |
15031 | |
15032 | bool IsConst; |
15033 | EvalResult EVResult; |
15034 | if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) { |
15035 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
15036 | Info.CheckingForUndefinedBehavior = true; |
15037 | (void)::EvaluateAsRValue(Info, this, EVResult.Val); |
15038 | } |
15039 | } |
15040 | |
15041 | bool Expr::EvalResult::isGlobalLValue() const { |
15042 | assert(Val.isLValue()); |
15043 | return IsGlobalLValue(Val.getLValueBase()); |
15044 | } |
15045 | |
15046 | |
15047 | |
15048 | |
15049 | |
15050 | |
15051 | |
15052 | |
15053 | |
15054 | |
15055 | |
15056 | |
15057 | |
15058 | |
15059 | |
15060 | |
15061 | namespace { |
15062 | |
15063 | enum ICEKind { |
15064 | |
15065 | IK_ICE, |
15066 | |
15067 | |
15068 | |
15069 | IK_ICEIfUnevaluated, |
15070 | |
15071 | IK_NotICE |
15072 | }; |
15073 | |
15074 | struct ICEDiag { |
15075 | ICEKind Kind; |
15076 | SourceLocation Loc; |
15077 | |
15078 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
15079 | }; |
15080 | |
15081 | } |
15082 | |
15083 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
15084 | |
15085 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
15086 | |
15087 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
15088 | Expr::EvalResult EVResult; |
15089 | Expr::EvalStatus Status; |
15090 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
15091 | |
15092 | Info.InConstantContext = true; |
15093 | if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects || |
15094 | !EVResult.Val.isInt()) |
15095 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15096 | |
15097 | return NoDiag(); |
15098 | } |
15099 | |
15100 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
15101 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
15102 | if (!E->getType()->isIntegralOrEnumerationType()) |
15103 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15104 | |
15105 | switch (E->getStmtClass()) { |
15106 | #define ABSTRACT_STMT(Node) |
15107 | #define STMT(Node, Base) case Expr::Node##Class: |
15108 | #define EXPR(Node, Base) |
15109 | #include "clang/AST/StmtNodes.inc" |
15110 | case Expr::PredefinedExprClass: |
15111 | case Expr::FloatingLiteralClass: |
15112 | case Expr::ImaginaryLiteralClass: |
15113 | case Expr::StringLiteralClass: |
15114 | case Expr::ArraySubscriptExprClass: |
15115 | case Expr::MatrixSubscriptExprClass: |
15116 | case Expr::OMPArraySectionExprClass: |
15117 | case Expr::OMPArrayShapingExprClass: |
15118 | case Expr::OMPIteratorExprClass: |
15119 | case Expr::MemberExprClass: |
15120 | case Expr::CompoundAssignOperatorClass: |
15121 | case Expr::CompoundLiteralExprClass: |
15122 | case Expr::ExtVectorElementExprClass: |
15123 | case Expr::DesignatedInitExprClass: |
15124 | case Expr::ArrayInitLoopExprClass: |
15125 | case Expr::ArrayInitIndexExprClass: |
15126 | case Expr::NoInitExprClass: |
15127 | case Expr::DesignatedInitUpdateExprClass: |
15128 | case Expr::ImplicitValueInitExprClass: |
15129 | case Expr::ParenListExprClass: |
15130 | case Expr::VAArgExprClass: |
15131 | case Expr::AddrLabelExprClass: |
15132 | case Expr::StmtExprClass: |
15133 | case Expr::CXXMemberCallExprClass: |
15134 | case Expr::CUDAKernelCallExprClass: |
15135 | case Expr::CXXAddrspaceCastExprClass: |
15136 | case Expr::CXXDynamicCastExprClass: |
15137 | case Expr::CXXTypeidExprClass: |
15138 | case Expr::CXXUuidofExprClass: |
15139 | case Expr::MSPropertyRefExprClass: |
15140 | case Expr::MSPropertySubscriptExprClass: |
15141 | case Expr::CXXNullPtrLiteralExprClass: |
15142 | case Expr::UserDefinedLiteralClass: |
15143 | case Expr::CXXThisExprClass: |
15144 | case Expr::CXXThrowExprClass: |
15145 | case Expr::CXXNewExprClass: |
15146 | case Expr::CXXDeleteExprClass: |
15147 | case Expr::CXXPseudoDestructorExprClass: |
15148 | case Expr::UnresolvedLookupExprClass: |
15149 | case Expr::TypoExprClass: |
15150 | case Expr::RecoveryExprClass: |
15151 | case Expr::DependentScopeDeclRefExprClass: |
15152 | case Expr::CXXConstructExprClass: |
15153 | case Expr::CXXInheritedCtorInitExprClass: |
15154 | case Expr::CXXStdInitializerListExprClass: |
15155 | case Expr::CXXBindTemporaryExprClass: |
15156 | case Expr::ExprWithCleanupsClass: |
15157 | case Expr::CXXTemporaryObjectExprClass: |
15158 | case Expr::CXXUnresolvedConstructExprClass: |
15159 | case Expr::CXXDependentScopeMemberExprClass: |
15160 | case Expr::UnresolvedMemberExprClass: |
15161 | case Expr::ObjCStringLiteralClass: |
15162 | case Expr::ObjCBoxedExprClass: |
15163 | case Expr::ObjCArrayLiteralClass: |
15164 | case Expr::ObjCDictionaryLiteralClass: |
15165 | case Expr::ObjCEncodeExprClass: |
15166 | case Expr::ObjCMessageExprClass: |
15167 | case Expr::ObjCSelectorExprClass: |
15168 | case Expr::ObjCProtocolExprClass: |
15169 | case Expr::ObjCIvarRefExprClass: |
15170 | case Expr::ObjCPropertyRefExprClass: |
15171 | case Expr::ObjCSubscriptRefExprClass: |
15172 | case Expr::ObjCIsaExprClass: |
15173 | case Expr::ObjCAvailabilityCheckExprClass: |
15174 | case Expr::ShuffleVectorExprClass: |
15175 | case Expr::ConvertVectorExprClass: |
15176 | case Expr::BlockExprClass: |
15177 | case Expr::NoStmtClass: |
15178 | case Expr::OpaqueValueExprClass: |
15179 | case Expr::PackExpansionExprClass: |
15180 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
15181 | case Expr::FunctionParmPackExprClass: |
15182 | case Expr::AsTypeExprClass: |
15183 | case Expr::ObjCIndirectCopyRestoreExprClass: |
15184 | case Expr::MaterializeTemporaryExprClass: |
15185 | case Expr::PseudoObjectExprClass: |
15186 | case Expr::AtomicExprClass: |
15187 | case Expr::LambdaExprClass: |
15188 | case Expr::CXXFoldExprClass: |
15189 | case Expr::CoawaitExprClass: |
15190 | case Expr::DependentCoawaitExprClass: |
15191 | case Expr::CoyieldExprClass: |
15192 | case Expr::SYCLUniqueStableNameExprClass: |
15193 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15194 | |
15195 | case Expr::InitListExprClass: { |
15196 | |
15197 | |
15198 | |
15199 | |
15200 | if (E->isPRValue()) |
15201 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
15202 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
15203 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15204 | } |
15205 | |
15206 | case Expr::SizeOfPackExprClass: |
15207 | case Expr::GNUNullExprClass: |
15208 | case Expr::SourceLocExprClass: |
15209 | return NoDiag(); |
15210 | |
15211 | case Expr::SubstNonTypeTemplateParmExprClass: |
15212 | return |
15213 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
15214 | |
15215 | case Expr::ConstantExprClass: |
15216 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
15217 | |
15218 | case Expr::ParenExprClass: |
15219 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
15220 | case Expr::GenericSelectionExprClass: |
15221 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
15222 | case Expr::IntegerLiteralClass: |
15223 | case Expr::FixedPointLiteralClass: |
15224 | case Expr::CharacterLiteralClass: |
15225 | case Expr::ObjCBoolLiteralExprClass: |
15226 | case Expr::CXXBoolLiteralExprClass: |
15227 | case Expr::CXXScalarValueInitExprClass: |
15228 | case Expr::TypeTraitExprClass: |
15229 | case Expr::ConceptSpecializationExprClass: |
15230 | case Expr::RequiresExprClass: |
15231 | case Expr::ArrayTypeTraitExprClass: |
15232 | case Expr::ExpressionTraitExprClass: |
15233 | case Expr::CXXNoexceptExprClass: |
15234 | return NoDiag(); |
15235 | case Expr::CallExprClass: |
15236 | case Expr::CXXOperatorCallExprClass: { |
15237 | |
15238 | |
15239 | |
15240 | const CallExpr *CE = cast<CallExpr>(E); |
15241 | if (CE->getBuiltinCallee()) |
15242 | return CheckEvalInICE(E, Ctx); |
15243 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15244 | } |
15245 | case Expr::CXXRewrittenBinaryOperatorClass: |
15246 | return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(), |
15247 | Ctx); |
15248 | case Expr::DeclRefExprClass: { |
15249 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
15250 | if (isa<EnumConstantDecl>(D)) |
15251 | return NoDiag(); |
15252 | |
15253 | |
15254 | |
15255 | |
15256 | |
15257 | |
15258 | |
15259 | |
15260 | |
15261 | |
15262 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
15263 | if (VD && VD->isUsableInConstantExpressions(Ctx) && |
15264 | !VD->getType()->isReferenceType()) |
15265 | return NoDiag(); |
15266 | |
15267 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15268 | } |
15269 | case Expr::UnaryOperatorClass: { |
15270 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
15271 | switch (Exp->getOpcode()) { |
15272 | case UO_PostInc: |
15273 | case UO_PostDec: |
15274 | case UO_PreInc: |
15275 | case UO_PreDec: |
15276 | case UO_AddrOf: |
15277 | case UO_Deref: |
15278 | case UO_Coawait: |
15279 | |
15280 | |
15281 | |
15282 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15283 | case UO_Extension: |
15284 | case UO_LNot: |
15285 | case UO_Plus: |
15286 | case UO_Minus: |
15287 | case UO_Not: |
15288 | case UO_Real: |
15289 | case UO_Imag: |
15290 | return CheckICE(Exp->getSubExpr(), Ctx); |
15291 | } |
15292 | llvm_unreachable("invalid unary operator class"); |
15293 | } |
15294 | case Expr::OffsetOfExprClass: { |
15295 | |
15296 | |
15297 | |
15298 | |
15299 | |
15300 | |
15301 | return CheckEvalInICE(E, Ctx); |
15302 | } |
15303 | case Expr::UnaryExprOrTypeTraitExprClass: { |
15304 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
15305 | if ((Exp->getKind() == UETT_SizeOf) && |
15306 | Exp->getTypeOfArgument()->isVariableArrayType()) |
15307 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15308 | return NoDiag(); |
15309 | } |
15310 | case Expr::BinaryOperatorClass: { |
15311 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
15312 | switch (Exp->getOpcode()) { |
15313 | case BO_PtrMemD: |
15314 | case BO_PtrMemI: |
15315 | case BO_Assign: |
15316 | case BO_MulAssign: |
15317 | case BO_DivAssign: |
15318 | case BO_RemAssign: |
15319 | case BO_AddAssign: |
15320 | case BO_SubAssign: |
15321 | case BO_ShlAssign: |
15322 | case BO_ShrAssign: |
15323 | case BO_AndAssign: |
15324 | case BO_XorAssign: |
15325 | case BO_OrAssign: |
15326 | |
15327 | |
15328 | |
15329 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15330 | |
15331 | case BO_Mul: |
15332 | case BO_Div: |
15333 | case BO_Rem: |
15334 | case BO_Add: |
15335 | case BO_Sub: |
15336 | case BO_Shl: |
15337 | case BO_Shr: |
15338 | case BO_LT: |
15339 | case BO_GT: |
15340 | case BO_LE: |
15341 | case BO_GE: |
15342 | case BO_EQ: |
15343 | case BO_NE: |
15344 | case BO_And: |
15345 | case BO_Xor: |
15346 | case BO_Or: |
15347 | case BO_Comma: |
15348 | case BO_Cmp: { |
15349 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
15350 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
15351 | if (Exp->getOpcode() == BO_Div || |
15352 | Exp->getOpcode() == BO_Rem) { |
15353 | |
15354 | |
15355 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
15356 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
15357 | if (REval == 0) |
15358 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15359 | if (REval.isSigned() && REval.isAllOnesValue()) { |
15360 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
15361 | if (LEval.isMinSignedValue()) |
15362 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15363 | } |
15364 | } |
15365 | } |
15366 | if (Exp->getOpcode() == BO_Comma) { |
15367 | if (Ctx.getLangOpts().C99) { |
15368 | |
15369 | |
15370 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
15371 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15372 | } else { |
15373 | |
15374 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15375 | } |
15376 | } |
15377 | return Worst(LHSResult, RHSResult); |
15378 | } |
15379 | case BO_LAnd: |
15380 | case BO_LOr: { |
15381 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
15382 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
15383 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
15384 | |
15385 | |
15386 | |
15387 | if ((Exp->getOpcode() == BO_LAnd) != |
15388 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
15389 | return RHSResult; |
15390 | return NoDiag(); |
15391 | } |
15392 | |
15393 | return Worst(LHSResult, RHSResult); |
15394 | } |
15395 | } |
15396 | llvm_unreachable("invalid binary operator kind"); |
15397 | } |
15398 | case Expr::ImplicitCastExprClass: |
15399 | case Expr::CStyleCastExprClass: |
15400 | case Expr::CXXFunctionalCastExprClass: |
15401 | case Expr::CXXStaticCastExprClass: |
15402 | case Expr::CXXReinterpretCastExprClass: |
15403 | case Expr::CXXConstCastExprClass: |
15404 | case Expr::ObjCBridgedCastExprClass: { |
15405 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
15406 | if (isa<ExplicitCastExpr>(E)) { |
15407 | if (const FloatingLiteral *FL |
15408 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
15409 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
15410 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
15411 | APSInt IgnoredVal(DestWidth, !DestSigned); |
15412 | bool Ignored; |
15413 | |
15414 | |
15415 | |
15416 | if (FL->getValue().convertToInteger(IgnoredVal, |
15417 | llvm::APFloat::rmTowardZero, |
15418 | &Ignored) & APFloat::opInvalidOp) |
15419 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15420 | return NoDiag(); |
15421 | } |
15422 | } |
15423 | switch (cast<CastExpr>(E)->getCastKind()) { |
15424 | case CK_LValueToRValue: |
15425 | case CK_AtomicToNonAtomic: |
15426 | case CK_NonAtomicToAtomic: |
15427 | case CK_NoOp: |
15428 | case CK_IntegralToBoolean: |
15429 | case CK_IntegralCast: |
15430 | return CheckICE(SubExpr, Ctx); |
15431 | default: |
15432 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15433 | } |
15434 | } |
15435 | case Expr::BinaryConditionalOperatorClass: { |
15436 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
15437 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
15438 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
15439 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
15440 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
15441 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
15442 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
15443 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
15444 | return FalseResult; |
15445 | } |
15446 | case Expr::ConditionalOperatorClass: { |
15447 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
15448 | |
15449 | |
15450 | |
15451 | |
15452 | if (const CallExpr *CallCE |
15453 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
15454 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
15455 | return CheckEvalInICE(E, Ctx); |
15456 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
15457 | if (CondResult.Kind == IK_NotICE) |
15458 | return CondResult; |
15459 | |
15460 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
15461 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
15462 | |
15463 | if (TrueResult.Kind == IK_NotICE) |
15464 | return TrueResult; |
15465 | if (FalseResult.Kind == IK_NotICE) |
15466 | return FalseResult; |
15467 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
15468 | return CondResult; |
15469 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
15470 | return NoDiag(); |
15471 | |
15472 | |
15473 | |
15474 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
15475 | return FalseResult; |
15476 | return TrueResult; |
15477 | } |
15478 | case Expr::CXXDefaultArgExprClass: |
15479 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
15480 | case Expr::CXXDefaultInitExprClass: |
15481 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
15482 | case Expr::ChooseExprClass: { |
15483 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
15484 | } |
15485 | case Expr::BuiltinBitCastExprClass: { |
15486 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
15487 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15488 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
15489 | } |
15490 | } |
15491 | |
15492 | llvm_unreachable("Invalid StmtClass!"); |
15493 | } |
15494 | |
15495 | |
15496 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
15497 | const Expr *E, |
15498 | llvm::APSInt *Value, |
15499 | SourceLocation *Loc) { |
15500 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
15501 | if (Loc) *Loc = E->getExprLoc(); |
15502 | return false; |
15503 | } |
15504 | |
15505 | APValue Result; |
15506 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
15507 | return false; |
15508 | |
15509 | if (!Result.isInt()) { |
15510 | if (Loc) *Loc = E->getExprLoc(); |
15511 | return false; |
15512 | } |
15513 | |
15514 | if (Value) *Value = Result.getInt(); |
15515 | return true; |
15516 | } |
15517 | |
15518 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
15519 | SourceLocation *Loc) const { |
15520 | assert(!isValueDependent() && |
15521 | "Expression evaluator can't be called on a dependent expression."); |
15522 | |
15523 | if (Ctx.getLangOpts().CPlusPlus11) |
15524 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
15525 | |
15526 | ICEDiag D = CheckICE(this, Ctx); |
15527 | if (D.Kind != IK_ICE) { |
15528 | if (Loc) *Loc = D.Loc; |
15529 | return false; |
15530 | } |
15531 | return true; |
15532 | } |
15533 | |
15534 | Optional<llvm::APSInt> Expr::getIntegerConstantExpr(const ASTContext &Ctx, |
15535 | SourceLocation *Loc, |
15536 | bool isEvaluated) const { |
15537 | assert(!isValueDependent() && |
15538 | "Expression evaluator can't be called on a dependent expression."); |
15539 | |
15540 | APSInt Value; |
15541 | |
15542 | if (Ctx.getLangOpts().CPlusPlus11) { |
15543 | if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc)) |
15544 | return Value; |
15545 | return None; |
15546 | } |
15547 | |
15548 | if (!isIntegerConstantExpr(Ctx, Loc)) |
15549 | return None; |
15550 | |
15551 | |
15552 | |
15553 | |
15554 | |
15555 | EvalResult ExprResult; |
15556 | Expr::EvalStatus Status; |
15557 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
15558 | Info.InConstantContext = true; |
15559 | |
15560 | if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) |
15561 | llvm_unreachable("ICE cannot be evaluated!"); |
15562 | |
15563 | return ExprResult.Val.getInt(); |
15564 | } |
15565 | |
15566 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
15567 | assert(!isValueDependent() && |
15568 | "Expression evaluator can't be called on a dependent expression."); |
15569 | |
15570 | return CheckICE(this, Ctx).Kind == IK_ICE; |
15571 | } |
15572 | |
15573 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
15574 | SourceLocation *Loc) const { |
15575 | assert(!isValueDependent() && |
15576 | "Expression evaluator can't be called on a dependent expression."); |
15577 | |
15578 | |
15579 | |
15580 | assert(Ctx.getLangOpts().CPlusPlus); |
15581 | |
15582 | |
15583 | Expr::EvalStatus Status; |
15584 | SmallVector<PartialDiagnosticAt, 8> Diags; |
15585 | Status.Diag = &Diags; |
15586 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
15587 | |
15588 | APValue Scratch; |
15589 | bool IsConstExpr = |
15590 | ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) && |
15591 | |
15592 | |
15593 | Info.discardCleanups() && !Status.HasSideEffects; |
15594 | |
15595 | if (!Diags.empty()) { |
15596 | IsConstExpr = false; |
15597 | if (Loc) *Loc = Diags[0].first; |
15598 | } else if (!IsConstExpr) { |
15599 | |
15600 | if (Loc) *Loc = getExprLoc(); |
15601 | } |
15602 | |
15603 | return IsConstExpr; |
15604 | } |
15605 | |
15606 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
15607 | const FunctionDecl *Callee, |
15608 | ArrayRef<const Expr*> Args, |
15609 | const Expr *This) const { |
15610 | assert(!isValueDependent() && |
15611 | "Expression evaluator can't be called on a dependent expression."); |
15612 | |
15613 | Expr::EvalStatus Status; |
15614 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
15615 | Info.InConstantContext = true; |
15616 | |
15617 | LValue ThisVal; |
15618 | const LValue *ThisPtr = nullptr; |
15619 | if (This) { |
15620 | #ifndef NDEBUG |
15621 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
15622 | assert(MD && "Don't provide `this` for non-methods."); |
15623 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
15624 | #endif |
15625 | if (!This->isValueDependent() && |
15626 | EvaluateObjectArgument(Info, This, ThisVal) && |
15627 | !Info.EvalStatus.HasSideEffects) |
15628 | ThisPtr = &ThisVal; |
15629 | |
15630 | |
15631 | |
15632 | Info.EvalStatus.HasSideEffects = false; |
15633 | } |
15634 | |
15635 | CallRef Call = Info.CurrentCall->createCall(Callee); |
15636 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
15637 | I != E; ++I) { |
15638 | unsigned Idx = I - Args.begin(); |
15639 | if (Idx >= Callee->getNumParams()) |
15640 | break; |
15641 | const ParmVarDecl *PVD = Callee->getParamDecl(Idx); |
15642 | if ((*I)->isValueDependent() || |
15643 | !EvaluateCallArg(PVD, *I, Call, Info) || |
15644 | Info.EvalStatus.HasSideEffects) { |
15645 | |
15646 | if (APValue *Slot = Info.getParamSlot(Call, PVD)) |
15647 | *Slot = APValue(); |
15648 | } |
15649 | |
15650 | |
15651 | |
15652 | Info.EvalStatus.HasSideEffects = false; |
15653 | } |
15654 | |
15655 | |
15656 | |
15657 | Info.discardCleanups(); |
15658 | Info.EvalStatus.HasSideEffects = false; |
15659 | |
15660 | |
15661 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, Call); |
15662 | |
15663 | FullExpressionRAII Scope(Info); |
15664 | return Evaluate(Value, Info, this) && Scope.destroy() && |
15665 | !Info.EvalStatus.HasSideEffects; |
15666 | } |
15667 | |
15668 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
15669 | SmallVectorImpl< |
15670 | PartialDiagnosticAt> &Diags) { |
15671 | |
15672 | |
15673 | |
15674 | if (FD->isDependentContext()) |
| 1 | Assuming the condition is false | |
|
| |
15675 | return true; |
15676 | |
15677 | Expr::EvalStatus Status; |
15678 | Status.Diag = &Diags; |
15679 | |
15680 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
| 3 | | Calling constructor for 'EvalInfo' | |
|
| 6 | | Returning from constructor for 'EvalInfo' | |
|
15681 | Info.InConstantContext = true; |
15682 | Info.CheckingPotentialConstantExpression = true; |
15683 | |
15684 | |
15685 | if (Info.EnableNewConstInterp) { |
| 7 | | Assuming field 'EnableNewConstInterp' is false | |
|
| |
15686 | Info.Ctx.getInterpContext().isPotentialConstantExpr(Info, FD); |
15687 | return Diags.empty(); |
15688 | } |
15689 | |
15690 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 9 | | Assuming 'FD' is a 'CXXMethodDecl' | |
|
15691 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
| |
15692 | |
15693 | |
15694 | |
15695 | LValue This; |
15696 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
| 11 | | Assuming 'RD' is non-null | |
|
| |
15697 | This.set({&VIE, Info.CurrentCall->Index}); |
15698 | |
15699 | ArrayRef<const Expr*> Args; |
15700 | |
15701 | APValue Scratch; |
15702 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
| 13 | | Assuming 'FD' is not a 'CXXConstructorDecl' | |
|
| |
15703 | |
15704 | |
15705 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
15706 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
15707 | } else { |
15708 | SourceLocation Loc = FD->getLocation(); |
15709 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
| |
| 16 | | Calling 'HandleFunctionCall' | |
|
15710 | Args, CallRef(), FD->getBody(), Info, Scratch, nullptr); |
15711 | } |
15712 | |
15713 | return Diags.empty(); |
15714 | } |
15715 | |
15716 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
15717 | const FunctionDecl *FD, |
15718 | SmallVectorImpl< |
15719 | PartialDiagnosticAt> &Diags) { |
15720 | assert(!E->isValueDependent() && |
15721 | "Expression evaluator can't be called on a dependent expression."); |
15722 | |
15723 | Expr::EvalStatus Status; |
15724 | Status.Diag = &Diags; |
15725 | |
15726 | EvalInfo Info(FD->getASTContext(), Status, |
15727 | EvalInfo::EM_ConstantExpressionUnevaluated); |
15728 | Info.InConstantContext = true; |
15729 | Info.CheckingPotentialConstantExpression = true; |
15730 | |
15731 | |
15732 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, CallRef()); |
15733 | |
15734 | APValue ResultScratch; |
15735 | Evaluate(ResultScratch, Info, E); |
15736 | return Diags.empty(); |
15737 | } |
15738 | |
15739 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
15740 | unsigned Type) const { |
15741 | if (!getType()->isPointerType()) |
15742 | return false; |
15743 | |
15744 | Expr::EvalStatus Status; |
15745 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
15746 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
15747 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_AST_DECL_H |
14 | #define LLVM_CLANG_AST_DECL_H |
15 | |
16 | #include "clang/AST/APValue.h" |
17 | #include "clang/AST/ASTContextAllocate.h" |
18 | #include "clang/AST/DeclAccessPair.h" |
19 | #include "clang/AST/DeclBase.h" |
20 | #include "clang/AST/DeclarationName.h" |
21 | #include "clang/AST/ExternalASTSource.h" |
22 | #include "clang/AST/NestedNameSpecifier.h" |
23 | #include "clang/AST/Redeclarable.h" |
24 | #include "clang/AST/Type.h" |
25 | #include "clang/Basic/AddressSpaces.h" |
26 | #include "clang/Basic/Diagnostic.h" |
27 | #include "clang/Basic/IdentifierTable.h" |
28 | #include "clang/Basic/LLVM.h" |
29 | #include "clang/Basic/Linkage.h" |
30 | #include "clang/Basic/OperatorKinds.h" |
31 | #include "clang/Basic/PartialDiagnostic.h" |
32 | #include "clang/Basic/PragmaKinds.h" |
33 | #include "clang/Basic/SourceLocation.h" |
34 | #include "clang/Basic/Specifiers.h" |
35 | #include "clang/Basic/Visibility.h" |
36 | #include "llvm/ADT/APSInt.h" |
37 | #include "llvm/ADT/ArrayRef.h" |
38 | #include "llvm/ADT/Optional.h" |
39 | #include "llvm/ADT/PointerIntPair.h" |
40 | #include "llvm/ADT/PointerUnion.h" |
41 | #include "llvm/ADT/StringRef.h" |
42 | #include "llvm/ADT/iterator_range.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include "llvm/Support/Compiler.h" |
45 | #include "llvm/Support/TrailingObjects.h" |
46 | #include <cassert> |
47 | #include <cstddef> |
48 | #include <cstdint> |
49 | #include <string> |
50 | #include <utility> |
51 | |
52 | namespace clang { |
53 | |
54 | class ASTContext; |
55 | struct ASTTemplateArgumentListInfo; |
56 | class Attr; |
57 | class CompoundStmt; |
58 | class DependentFunctionTemplateSpecializationInfo; |
59 | class EnumDecl; |
60 | class Expr; |
61 | class FunctionTemplateDecl; |
62 | class FunctionTemplateSpecializationInfo; |
63 | class FunctionTypeLoc; |
64 | class LabelStmt; |
65 | class MemberSpecializationInfo; |
66 | class Module; |
67 | class NamespaceDecl; |
68 | class ParmVarDecl; |
69 | class RecordDecl; |
70 | class Stmt; |
71 | class StringLiteral; |
72 | class TagDecl; |
73 | class TemplateArgumentList; |
74 | class TemplateArgumentListInfo; |
75 | class TemplateParameterList; |
76 | class TypeAliasTemplateDecl; |
77 | class TypeLoc; |
78 | class UnresolvedSetImpl; |
79 | class VarTemplateDecl; |
80 | |
81 | |
82 | class TranslationUnitDecl : public Decl, |
83 | public DeclContext, |
84 | public Redeclarable<TranslationUnitDecl> { |
85 | using redeclarable_base = Redeclarable<TranslationUnitDecl>; |
86 | |
87 | TranslationUnitDecl *getNextRedeclarationImpl() override { |
88 | return getNextRedeclaration(); |
89 | } |
90 | |
91 | TranslationUnitDecl *getPreviousDeclImpl() override { |
92 | return getPreviousDecl(); |
93 | } |
94 | |
95 | TranslationUnitDecl *getMostRecentDeclImpl() override { |
96 | return getMostRecentDecl(); |
97 | } |
98 | |
99 | ASTContext &Ctx; |
100 | |
101 | |
102 | |
103 | NamespaceDecl *AnonymousNamespace = nullptr; |
104 | |
105 | explicit TranslationUnitDecl(ASTContext &ctx); |
106 | |
107 | virtual void anchor(); |
108 | |
109 | public: |
110 | using redecl_range = redeclarable_base::redecl_range; |
111 | using redecl_iterator = redeclarable_base::redecl_iterator; |
112 | |
113 | using redeclarable_base::getMostRecentDecl; |
114 | using redeclarable_base::getPreviousDecl; |
115 | using redeclarable_base::isFirstDecl; |
116 | using redeclarable_base::redecls; |
117 | using redeclarable_base::redecls_begin; |
118 | using redeclarable_base::redecls_end; |
119 | |
120 | ASTContext &getASTContext() const { return Ctx; } |
121 | |
122 | NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } |
123 | void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; } |
124 | |
125 | static TranslationUnitDecl *Create(ASTContext &C); |
126 | |
127 | |
128 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
129 | static bool classofKind(Kind K) { return K == TranslationUnit; } |
130 | static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { |
131 | return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); |
132 | } |
133 | static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { |
134 | return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); |
135 | } |
136 | }; |
137 | |
138 | |
139 | |
140 | class PragmaCommentDecl final |
141 | : public Decl, |
142 | private llvm::TrailingObjects<PragmaCommentDecl, char> { |
143 | friend class ASTDeclReader; |
144 | friend class ASTDeclWriter; |
145 | friend TrailingObjects; |
146 | |
147 | PragmaMSCommentKind CommentKind; |
148 | |
149 | PragmaCommentDecl(TranslationUnitDecl *TU, SourceLocation CommentLoc, |
150 | PragmaMSCommentKind CommentKind) |
151 | : Decl(PragmaComment, TU, CommentLoc), CommentKind(CommentKind) {} |
152 | |
153 | virtual void anchor(); |
154 | |
155 | public: |
156 | static PragmaCommentDecl *Create(const ASTContext &C, TranslationUnitDecl *DC, |
157 | SourceLocation CommentLoc, |
158 | PragmaMSCommentKind CommentKind, |
159 | StringRef Arg); |
160 | static PragmaCommentDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
161 | unsigned ArgSize); |
162 | |
163 | PragmaMSCommentKind getCommentKind() const { return CommentKind; } |
164 | |
165 | StringRef getArg() const { return getTrailingObjects<char>(); } |
166 | |
167 | |
168 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
169 | static bool classofKind(Kind K) { return K == PragmaComment; } |
170 | }; |
171 | |
172 | |
173 | |
174 | class PragmaDetectMismatchDecl final |
175 | : public Decl, |
176 | private llvm::TrailingObjects<PragmaDetectMismatchDecl, char> { |
177 | friend class ASTDeclReader; |
178 | friend class ASTDeclWriter; |
179 | friend TrailingObjects; |
180 | |
181 | size_t ValueStart; |
182 | |
183 | PragmaDetectMismatchDecl(TranslationUnitDecl *TU, SourceLocation Loc, |
184 | size_t ValueStart) |
185 | : Decl(PragmaDetectMismatch, TU, Loc), ValueStart(ValueStart) {} |
186 | |
187 | virtual void anchor(); |
188 | |
189 | public: |
190 | static PragmaDetectMismatchDecl *Create(const ASTContext &C, |
191 | TranslationUnitDecl *DC, |
192 | SourceLocation Loc, StringRef Name, |
193 | StringRef Value); |
194 | static PragmaDetectMismatchDecl * |
195 | CreateDeserialized(ASTContext &C, unsigned ID, unsigned NameValueSize); |
196 | |
197 | StringRef getName() const { return getTrailingObjects<char>(); } |
198 | StringRef getValue() const { return getTrailingObjects<char>() + ValueStart; } |
199 | |
200 | |
201 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
202 | static bool classofKind(Kind K) { return K == PragmaDetectMismatch; } |
203 | }; |
204 | |
205 | |
206 | |
207 | |
208 | |
209 | |
210 | |
211 | |
212 | |
213 | |
214 | |
215 | |
216 | |
217 | |
218 | |
219 | |
220 | |
221 | |
222 | class ExternCContextDecl : public Decl, public DeclContext { |
223 | explicit ExternCContextDecl(TranslationUnitDecl *TU) |
224 | : Decl(ExternCContext, TU, SourceLocation()), |
225 | DeclContext(ExternCContext) {} |
226 | |
227 | virtual void anchor(); |
228 | |
229 | public: |
230 | static ExternCContextDecl *Create(const ASTContext &C, |
231 | TranslationUnitDecl *TU); |
232 | |
233 | |
234 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
235 | static bool classofKind(Kind K) { return K == ExternCContext; } |
236 | static DeclContext *castToDeclContext(const ExternCContextDecl *D) { |
237 | return static_cast<DeclContext *>(const_cast<ExternCContextDecl*>(D)); |
238 | } |
239 | static ExternCContextDecl *castFromDeclContext(const DeclContext *DC) { |
240 | return static_cast<ExternCContextDecl *>(const_cast<DeclContext*>(DC)); |
241 | } |
242 | }; |
243 | |
244 | |
245 | |
246 | |
247 | |
248 | |
249 | class NamedDecl : public Decl { |
250 | |
251 | |
252 | |
253 | DeclarationName Name; |
254 | |
255 | virtual void anchor(); |
256 | |
257 | private: |
258 | NamedDecl *getUnderlyingDeclImpl() LLVM_READONLY; |
259 | |
260 | protected: |
261 | NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) |
262 | : Decl(DK, DC, L), Name(N) {} |
263 | |
264 | public: |
265 | |
266 | |
267 | |
268 | |
269 | |
270 | IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } |
271 | |
272 | |
273 | |
274 | |
275 | |
276 | StringRef getName() const { |
277 | assert(Name.isIdentifier() && "Name is not a simple identifier"); |
278 | return getIdentifier() ? getIdentifier()->getName() : ""; |
279 | } |
280 | |
281 | |
282 | |
283 | |
284 | |
285 | |
286 | |
287 | |
288 | |
289 | |
290 | |
291 | |
292 | std::string getNameAsString() const { return Name.getAsString(); } |
293 | |
294 | |
295 | |
296 | virtual void printName(raw_ostream &os) const; |
297 | |
298 | |
299 | |
300 | |
301 | |
302 | |
303 | |
304 | |
305 | |
306 | |
307 | |
308 | |
309 | |
310 | |
311 | |
312 | |
313 | DeclarationName getDeclName() const { return Name; } |
314 | |
315 | |
316 | void setDeclName(DeclarationName N) { Name = N; } |
317 | |
318 | |
319 | |
320 | |
321 | |
322 | |
323 | |
324 | |
325 | |
326 | void printQualifiedName(raw_ostream &OS) const; |
327 | void printQualifiedName(raw_ostream &OS, const PrintingPolicy &Policy) const; |
328 | |
329 | |
330 | |
331 | |
332 | |
333 | void printNestedNameSpecifier(raw_ostream &OS) const; |
334 | void printNestedNameSpecifier(raw_ostream &OS, |
335 | const PrintingPolicy &Policy) const; |
336 | |
337 | |
338 | std::string getQualifiedNameAsString() const; |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | virtual void getNameForDiagnostic(raw_ostream &OS, |
347 | const PrintingPolicy &Policy, |
348 | bool Qualified) const; |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | |
360 | bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer = true) const; |
361 | |
362 | |
363 | bool hasLinkage() const; |
364 | |
365 | using Decl::isModulePrivate; |
366 | using Decl::setModulePrivate; |
367 | |
368 | |
369 | bool isCXXClassMember() const { |
370 | const DeclContext *DC = getDeclContext(); |
371 | |
372 | |
373 | |
374 | |
375 | if (isa<EnumDecl>(DC)) |
376 | DC = DC->getRedeclContext(); |
377 | |
378 | return DC->isRecord(); |
379 | } |
380 | |
381 | |
382 | |
383 | bool isCXXInstanceMember() const; |
384 | |
385 | |
386 | |
387 | ReservedIdentifierStatus isReserved(const LangOptions &LangOpts) const; |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | |
394 | Linkage getLinkageInternal() const; |
395 | |
396 | |
397 | |
398 | Linkage getFormalLinkage() const { |
399 | return clang::getFormalLinkage(getLinkageInternal()); |
400 | } |
401 | |
402 | |
403 | bool hasExternalFormalLinkage() const { |
404 | return isExternalFormalLinkage(getLinkageInternal()); |
405 | } |
406 | |
407 | bool isExternallyVisible() const { |
408 | return clang::isExternallyVisible(getLinkageInternal()); |
409 | } |
410 | |
411 | |
412 | |
413 | bool isExternallyDeclarable() const { |
414 | return isExternallyVisible() && !getOwningModuleForLinkage(); |
415 | } |
416 | |
417 | |
418 | Visibility getVisibility() const { |
419 | return getLinkageAndVisibility().getVisibility(); |
420 | } |
421 | |
422 | |
423 | LinkageInfo getLinkageAndVisibility() const; |
424 | |
425 | |
426 | enum ExplicitVisibilityKind { |
427 | |
428 | |
429 | |
430 | VisibilityForType, |
431 | |
432 | |
433 | |
434 | |
435 | VisibilityForValue |
436 | }; |
437 | |
438 | |
439 | |
440 | Optional<Visibility> |
441 | getExplicitVisibility(ExplicitVisibilityKind kind) const; |
442 | |
443 | |
444 | |
445 | bool isLinkageValid() const; |
446 | |
447 | |
448 | |
449 | |
450 | |
451 | |
452 | |
453 | bool hasLinkageBeenComputed() const { |
454 | return hasCachedLinkage(); |
455 | } |
456 | |
457 | |
458 | |
459 | NamedDecl *getUnderlyingDecl() { |
460 | |
461 | if (this->getKind() != UsingShadow && |
462 | this->getKind() != ConstructorUsingShadow && |
463 | this->getKind() != ObjCCompatibleAlias && |
464 | this->getKind() != NamespaceAlias) |
465 | return this; |
466 | |
467 | return getUnderlyingDeclImpl(); |
468 | } |
469 | const NamedDecl *getUnderlyingDecl() const { |
470 | return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); |
471 | } |
472 | |
473 | NamedDecl *getMostRecentDecl() { |
474 | return cast<NamedDecl>(static_cast<Decl *>(this)->getMostRecentDecl()); |
475 | } |
476 | const NamedDecl *getMostRecentDecl() const { |
477 | return const_cast<NamedDecl*>(this)->getMostRecentDecl(); |
478 | } |
479 | |
480 | ObjCStringFormatFamily getObjCFStringFormattingFamily() const; |
481 | |
482 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
483 | static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } |
484 | }; |
485 | |
486 | inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { |
487 | ND.printName(OS); |
488 | return OS; |
489 | } |
490 | |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | class LabelDecl : public NamedDecl { |
497 | LabelStmt *TheStmt; |
498 | StringRef MSAsmName; |
499 | bool MSAsmNameResolved = false; |
500 | |
501 | |
502 | |
503 | |
504 | SourceLocation LocStart; |
505 | |
506 | LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, |
507 | LabelStmt *S, SourceLocation StartL) |
508 | : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} |
509 | |
510 | void anchor() override; |
511 | |
512 | public: |
513 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
514 | SourceLocation IdentL, IdentifierInfo *II); |
515 | static LabelDecl *Create(ASTContext &C, DeclContext *DC, |
516 | SourceLocation IdentL, IdentifierInfo *II, |
517 | SourceLocation GnuLabelL); |
518 | static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
519 | |
520 | LabelStmt *getStmt() const { return TheStmt; } |
521 | void setStmt(LabelStmt *T) { TheStmt = T; } |
522 | |
523 | bool isGnuLocal() const { return LocStart != getLocation(); } |
524 | void setLocStart(SourceLocation L) { LocStart = L; } |
525 | |
526 | SourceRange getSourceRange() const override LLVM_READONLY { |
527 | return SourceRange(LocStart, getLocation()); |
528 | } |
529 | |
530 | bool isMSAsmLabel() const { return !MSAsmName.empty(); } |
531 | bool isResolvedMSAsmLabel() const { return isMSAsmLabel() && MSAsmNameResolved; } |
532 | void setMSAsmLabel(StringRef Name); |
533 | StringRef getMSAsmLabel() const { return MSAsmName; } |
534 | void setMSAsmLabelResolved() { MSAsmNameResolved = true; } |
535 | |
536 | |
537 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
538 | static bool classofKind(Kind K) { return K == Label; } |
539 | }; |
540 | |
541 | |
542 | class NamespaceDecl : public NamedDecl, public DeclContext, |
543 | public Redeclarable<NamespaceDecl> |
544 | { |
545 | |
546 | |
547 | SourceLocation LocStart; |
548 | |
549 | |
550 | SourceLocation RBraceLoc; |
551 | |
552 | |
553 | |
554 | |
555 | |
556 | llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline; |
557 | |
558 | NamespaceDecl(ASTContext &C, DeclContext *DC, bool Inline, |
559 | SourceLocation StartLoc, SourceLocation IdLoc, |
560 | IdentifierInfo *Id, NamespaceDecl *PrevDecl); |
561 | |
562 | using redeclarable_base = Redeclarable<NamespaceDecl>; |
563 | |
564 | NamespaceDecl *getNextRedeclarationImpl() override; |
565 | NamespaceDecl *getPreviousDeclImpl() override; |
566 | NamespaceDecl *getMostRecentDeclImpl() override; |
567 | |
568 | public: |
569 | friend class ASTDeclReader; |
570 | friend class ASTDeclWriter; |
571 | |
572 | static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, |
573 | bool Inline, SourceLocation StartLoc, |
574 | SourceLocation IdLoc, IdentifierInfo *Id, |
575 | NamespaceDecl *PrevDecl); |
576 | |
577 | static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
578 | |
579 | using redecl_range = redeclarable_base::redecl_range; |
580 | using redecl_iterator = redeclarable_base::redecl_iterator; |
581 | |
582 | using redeclarable_base::redecls_begin; |
583 | using redeclarable_base::redecls_end; |
584 | using redeclarable_base::redecls; |
585 | using redeclarable_base::getPreviousDecl; |
586 | using redeclarable_base::getMostRecentDecl; |
587 | using redeclarable_base::isFirstDecl; |
588 | |
589 | |
590 | |
591 | |
592 | |
593 | |
594 | |
595 | |
596 | |
597 | |
598 | bool isAnonymousNamespace() const { |
599 | return !getIdentifier(); |
600 | } |
601 | |
602 | |
603 | bool isInline() const { |
604 | return AnonOrFirstNamespaceAndInline.getInt(); |
605 | } |
606 | |
607 | |
608 | void setInline(bool Inline) { |
609 | AnonOrFirstNamespaceAndInline.setInt(Inline); |
610 | } |
611 | |
612 | |
613 | bool isRedundantInlineQualifierFor(DeclarationName Name) const { |
614 | if (!isInline()) |
615 | return false; |
616 | auto X = lookup(Name); |
617 | auto Y = getParent()->lookup(Name); |
618 | return std::distance(X.begin(), X.end()) == |
619 | std::distance(Y.begin(), Y.end()); |
620 | } |
621 | |
622 | |
623 | NamespaceDecl *getOriginalNamespace(); |
624 | |
625 | |
626 | const NamespaceDecl *getOriginalNamespace() const; |
627 | |
628 | |
629 | |
630 | |
631 | bool isOriginalNamespace() const; |
632 | |
633 | |
634 | |
635 | NamespaceDecl *getAnonymousNamespace() const { |
636 | return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer(); |
637 | } |
638 | |
639 | void setAnonymousNamespace(NamespaceDecl *D) { |
640 | getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D); |
641 | } |
642 | |
643 | |
644 | NamespaceDecl *getCanonicalDecl() override { |
645 | return getOriginalNamespace(); |
646 | } |
647 | const NamespaceDecl *getCanonicalDecl() const { |
648 | return getOriginalNamespace(); |
649 | } |
650 | |
651 | SourceRange getSourceRange() const override LLVM_READONLY { |
652 | return SourceRange(LocStart, RBraceLoc); |
653 | } |
654 | |
655 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
656 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
657 | void setLocStart(SourceLocation L) { LocStart = L; } |
658 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
659 | |
660 | |
661 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
662 | static bool classofKind(Kind K) { return K == Namespace; } |
663 | static DeclContext *castToDeclContext(const NamespaceDecl *D) { |
664 | return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); |
665 | } |
666 | static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { |
667 | return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); |
668 | } |
669 | }; |
670 | |
671 | |
672 | |
673 | |
674 | class ValueDecl : public NamedDecl { |
675 | QualType DeclType; |
676 | |
677 | void anchor() override; |
678 | |
679 | protected: |
680 | ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, |
681 | DeclarationName N, QualType T) |
682 | : NamedDecl(DK, DC, L, N), DeclType(T) {} |
683 | |
684 | public: |
685 | QualType getType() const { return DeclType; } |
686 | void setType(QualType newType) { DeclType = newType; } |
687 | |
688 | |
689 | |
690 | bool isWeak() const; |
691 | |
692 | |
693 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
694 | static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } |
695 | }; |
696 | |
697 | |
698 | |
699 | struct QualifierInfo { |
700 | NestedNameSpecifierLoc QualifierLoc; |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | unsigned NumTemplParamLists = 0; |
707 | |
708 | |
709 | |
710 | |
711 | |
712 | |
713 | TemplateParameterList** TemplParamLists = nullptr; |
714 | |
715 | QualifierInfo() = default; |
716 | QualifierInfo(const QualifierInfo &) = delete; |
717 | QualifierInfo& operator=(const QualifierInfo &) = delete; |
718 | |
719 | |
720 | void setTemplateParameterListsInfo(ASTContext &Context, |
721 | ArrayRef<TemplateParameterList *> TPLists); |
722 | }; |
723 | |
724 | |
725 | |
726 | class DeclaratorDecl : public ValueDecl { |
727 | |
728 | |
729 | |
730 | struct ExtInfo : public QualifierInfo { |
731 | TypeSourceInfo *TInfo; |
732 | Expr *TrailingRequiresClause = nullptr; |
733 | }; |
734 | |
735 | llvm::PointerUnion<TypeSourceInfo *, ExtInfo *> DeclInfo; |
736 | |
737 | |
738 | |
739 | SourceLocation InnerLocStart; |
740 | |
741 | bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); } |
742 | ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); } |
743 | const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); } |
744 | |
745 | protected: |
746 | DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, |
747 | DeclarationName N, QualType T, TypeSourceInfo *TInfo, |
748 | SourceLocation StartL) |
749 | : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) {} |
750 | |
751 | public: |
752 | friend class ASTDeclReader; |
753 | friend class ASTDeclWriter; |
754 | |
755 | TypeSourceInfo *getTypeSourceInfo() const { |
756 | return hasExtInfo() |
757 | ? getExtInfo()->TInfo |
758 | : DeclInfo.get<TypeSourceInfo*>(); |
759 | } |
760 | |
761 | void setTypeSourceInfo(TypeSourceInfo *TI) { |
762 | if (hasExtInfo()) |
763 | getExtInfo()->TInfo = TI; |
764 | else |
765 | DeclInfo = TI; |
766 | } |
767 | |
768 | |
769 | SourceLocation getInnerLocStart() const { return InnerLocStart; } |
770 | void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } |
771 | |
772 | |
773 | |
774 | SourceLocation getOuterLocStart() const; |
775 | |
776 | SourceRange getSourceRange() const override LLVM_READONLY; |
777 | |
778 | SourceLocation getBeginLoc() const LLVM_READONLY { |
779 | return getOuterLocStart(); |
780 | } |
781 | |
782 | |
783 | |
784 | NestedNameSpecifier *getQualifier() const { |
785 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
786 | : nullptr; |
787 | } |
788 | |
789 | |
790 | |
791 | |
792 | NestedNameSpecifierLoc getQualifierLoc() const { |
793 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
794 | : NestedNameSpecifierLoc(); |
795 | } |
796 | |
797 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
798 | |
799 | |
800 | |
801 | |
802 | Expr *getTrailingRequiresClause() { |
803 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
804 | : nullptr; |
805 | } |
806 | |
807 | const Expr *getTrailingRequiresClause() const { |
808 | return hasExtInfo() ? getExtInfo()->TrailingRequiresClause |
809 | : nullptr; |
810 | } |
811 | |
812 | void setTrailingRequiresClause(Expr *TrailingRequiresClause); |
813 | |
814 | unsigned getNumTemplateParameterLists() const { |
815 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
816 | } |
817 | |
818 | TemplateParameterList *getTemplateParameterList(unsigned index) const { |
819 | assert(index < getNumTemplateParameterLists()); |
820 | return getExtInfo()->TemplParamLists[index]; |
821 | } |
822 | |
823 | void setTemplateParameterListsInfo(ASTContext &Context, |
824 | ArrayRef<TemplateParameterList *> TPLists); |
825 | |
826 | SourceLocation getTypeSpecStartLoc() const; |
827 | SourceLocation getTypeSpecEndLoc() const; |
828 | |
829 | |
830 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
831 | static bool classofKind(Kind K) { |
832 | return K >= firstDeclarator && K <= lastDeclarator; |
833 | } |
834 | }; |
835 | |
836 | |
837 | |
838 | |
839 | struct EvaluatedStmt { |
840 | |
841 | bool WasEvaluated : 1; |
842 | |
843 | |
844 | bool IsEvaluating : 1; |
845 | |
846 | |
847 | |
848 | |
849 | |
850 | bool HasConstantInitialization : 1; |
851 | |
852 | |
853 | |
854 | |
855 | |
856 | |
857 | bool HasConstantDestruction : 1; |
858 | |
859 | |
860 | |
861 | bool HasICEInit : 1; |
862 | bool CheckedForICEInit : 1; |
863 | |
864 | Stmt *Value; |
865 | APValue Evaluated; |
866 | |
867 | EvaluatedStmt() |
868 | : WasEvaluated(false), IsEvaluating(false), |
869 | HasConstantInitialization(false), HasConstantDestruction(false), |
870 | HasICEInit(false), CheckedForICEInit(false) {} |
871 | }; |
872 | |
873 | |
874 | class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { |
875 | public: |
876 | |
877 | enum InitializationStyle { |
878 | |
879 | CInit, |
880 | |
881 | |
882 | CallInit, |
883 | |
884 | |
885 | ListInit |
886 | }; |
887 | |
888 | |
889 | enum TLSKind { |
890 | |
891 | TLS_None, |
892 | |
893 | |
894 | TLS_Static, |
895 | |
896 | |
897 | TLS_Dynamic |
898 | }; |
899 | |
900 | |
901 | |
902 | |
903 | static const char *getStorageClassSpecifierString(StorageClass SC); |
904 | |
905 | protected: |
906 | |
907 | |
908 | |
909 | |
910 | |
911 | |
912 | |
913 | using InitType = llvm::PointerUnion<Stmt *, EvaluatedStmt *>; |
914 | |
915 | |
916 | |
917 | mutable InitType Init; |
918 | |
919 | private: |
920 | friend class ASTDeclReader; |
921 | friend class ASTNodeImporter; |
922 | friend class StmtIteratorBase; |
923 | |
924 | class VarDeclBitfields { |
925 | friend class ASTDeclReader; |
926 | friend class VarDecl; |
927 | |
928 | unsigned SClass : 3; |
929 | unsigned TSCSpec : 2; |
930 | unsigned InitStyle : 2; |
931 | |
932 | |
933 | |
934 | unsigned ARCPseudoStrong : 1; |
935 | }; |
936 | enum { NumVarDeclBits = 8 }; |
937 | |
938 | protected: |
939 | enum { NumParameterIndexBits = 8 }; |
940 | |
941 | enum DefaultArgKind { |
942 | DAK_None, |
943 | DAK_Unparsed, |
944 | DAK_Uninstantiated, |
945 | DAK_Normal |
946 | }; |
947 | |
948 | enum { NumScopeDepthOrObjCQualsBits = 7 }; |
949 | |
950 | class ParmVarDeclBitfields { |
951 | friend class ASTDeclReader; |
952 | friend class ParmVarDecl; |
953 | |
954 | unsigned : NumVarDeclBits; |
955 | |
956 | |
957 | |
958 | unsigned HasInheritedDefaultArg : 1; |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | unsigned DefaultArgKind : 2; |
965 | |
966 | |
967 | unsigned IsKNRPromoted : 1; |
968 | |
969 | |
970 | unsigned IsObjCMethodParam : 1; |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | unsigned ScopeDepthOrObjCQuals : NumScopeDepthOrObjCQualsBits; |
977 | |
978 | |
979 | |
980 | unsigned ParameterIndex : NumParameterIndexBits; |
981 | }; |
982 | |
983 | class NonParmVarDeclBitfields { |
984 | friend class ASTDeclReader; |
985 | friend class ImplicitParamDecl; |
986 | friend class VarDecl; |
987 | |
988 | unsigned : NumVarDeclBits; |
989 | |
990 | |
991 | |
992 | |
993 | unsigned IsThisDeclarationADemotedDefinition : 1; |
994 | |
995 | |
996 | |
997 | unsigned ExceptionVar : 1; |
998 | |
999 | |
1000 | |
1001 | |
1002 | unsigned NRVOVariable : 1; |
1003 | |
1004 | |
1005 | |
1006 | unsigned CXXForRangeDecl : 1; |
1007 | |
1008 | |
1009 | unsigned ObjCForDecl : 1; |
1010 | |
1011 | |
1012 | unsigned IsInline : 1; |
1013 | |
1014 | |
1015 | unsigned IsInlineSpecified : 1; |
1016 | |
1017 | |
1018 | unsigned IsConstexpr : 1; |
1019 | |
1020 | |
1021 | |
1022 | unsigned IsInitCapture : 1; |
1023 | |
1024 | |
1025 | |
1026 | |
1027 | unsigned PreviousDeclInSameBlockScope : 1; |
1028 | |
1029 | |
1030 | |
1031 | unsigned ImplicitParamKind : 3; |
1032 | |
1033 | unsigned EscapingByref : 1; |
1034 | }; |
1035 | |
1036 | union { |
1037 | unsigned AllBits; |
1038 | VarDeclBitfields VarDeclBits; |
1039 | ParmVarDeclBitfields ParmVarDeclBits; |
1040 | NonParmVarDeclBitfields NonParmVarDeclBits; |
1041 | }; |
1042 | |
1043 | VarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1044 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1045 | TypeSourceInfo *TInfo, StorageClass SC); |
1046 | |
1047 | using redeclarable_base = Redeclarable<VarDecl>; |
1048 | |
1049 | VarDecl *getNextRedeclarationImpl() override { |
1050 | return getNextRedeclaration(); |
1051 | } |
1052 | |
1053 | VarDecl *getPreviousDeclImpl() override { |
1054 | return getPreviousDecl(); |
1055 | } |
1056 | |
1057 | VarDecl *getMostRecentDeclImpl() override { |
1058 | return getMostRecentDecl(); |
1059 | } |
1060 | |
1061 | public: |
1062 | using redecl_range = redeclarable_base::redecl_range; |
1063 | using redecl_iterator = redeclarable_base::redecl_iterator; |
1064 | |
1065 | using redeclarable_base::redecls_begin; |
1066 | using redeclarable_base::redecls_end; |
1067 | using redeclarable_base::redecls; |
1068 | using redeclarable_base::getPreviousDecl; |
1069 | using redeclarable_base::getMostRecentDecl; |
1070 | using redeclarable_base::isFirstDecl; |
1071 | |
1072 | static VarDecl *Create(ASTContext &C, DeclContext *DC, |
1073 | SourceLocation StartLoc, SourceLocation IdLoc, |
1074 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
1075 | StorageClass S); |
1076 | |
1077 | static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1078 | |
1079 | SourceRange getSourceRange() const override LLVM_READONLY; |
1080 | |
1081 | |
1082 | |
1083 | StorageClass getStorageClass() const { |
1084 | return (StorageClass) VarDeclBits.SClass; |
1085 | } |
1086 | void setStorageClass(StorageClass SC); |
1087 | |
1088 | void setTSCSpec(ThreadStorageClassSpecifier TSC) { |
1089 | VarDeclBits.TSCSpec = TSC; |
1090 | assert(VarDeclBits.TSCSpec == TSC && "truncation"); |
1091 | } |
1092 | ThreadStorageClassSpecifier getTSCSpec() const { |
1093 | return static_cast<ThreadStorageClassSpecifier>(VarDeclBits.TSCSpec); |
1094 | } |
1095 | TLSKind getTLSKind() const; |
1096 | |
1097 | |
1098 | |
1099 | bool hasLocalStorage() const { |
1100 | if (getStorageClass() == SC_None) { |
1101 | |
1102 | |
1103 | |
1104 | |
1105 | if (getType().getAddressSpace() == LangAS::opencl_constant) |
1106 | return false; |
1107 | |
1108 | return !isFileVarDecl() && getTSCSpec() == TSCS_unspecified; |
1109 | } |
1110 | |
1111 | |
1112 | if (getStorageClass() == SC_Register && !isLocalVarDeclOrParm()) |
1113 | return false; |
1114 | |
1115 | |
1116 | |
1117 | |
1118 | return getStorageClass() >= SC_Auto; |
1119 | } |
1120 | |
1121 | |
1122 | |
1123 | bool isStaticLocal() const { |
1124 | return (getStorageClass() == SC_Static || |
1125 | |
1126 | (getStorageClass() == SC_None && getTSCSpec() == TSCS_thread_local)) |
1127 | && !isFileVarDecl(); |
1128 | } |
1129 | |
1130 | |
1131 | |
1132 | bool hasExternalStorage() const { |
1133 | return getStorageClass() == SC_Extern || |
1134 | getStorageClass() == SC_PrivateExtern; |
1135 | } |
1136 | |
1137 | |
1138 | |
1139 | |
1140 | |
1141 | bool hasGlobalStorage() const { return !hasLocalStorage(); } |
1142 | |
1143 | |
1144 | StorageDuration getStorageDuration() const { |
1145 | return hasLocalStorage() ? SD_Automatic : |
1146 | getTSCSpec() ? SD_Thread : SD_Static; |
1147 | } |
1148 | |
1149 | |
1150 | LanguageLinkage getLanguageLinkage() const; |
1151 | |
1152 | |
1153 | bool isExternC() const; |
1154 | |
1155 | |
1156 | |
1157 | bool isInExternCContext() const; |
1158 | |
1159 | |
1160 | |
1161 | bool isInExternCXXContext() const; |
1162 | |
1163 | |
1164 | |
1165 | |
1166 | |
1167 | |
1168 | bool isLocalVarDecl() const { |
1169 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1170 | return false; |
1171 | if (const DeclContext *DC = getLexicalDeclContext()) |
1172 | return DC->getRedeclContext()->isFunctionOrMethod(); |
1173 | return false; |
1174 | } |
1175 | |
1176 | |
1177 | bool isLocalVarDeclOrParm() const { |
1178 | return isLocalVarDecl() || getKind() == Decl::ParmVar; |
1179 | } |
1180 | |
1181 | |
1182 | bool isFunctionOrMethodVarDecl() const { |
1183 | if (getKind() != Decl::Var && getKind() != Decl::Decomposition) |
1184 | return false; |
1185 | const DeclContext *DC = getLexicalDeclContext()->getRedeclContext(); |
1186 | return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; |
1187 | } |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | bool isStaticDataMember() const { |
1199 | |
1200 | return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); |
1201 | } |
1202 | |
1203 | VarDecl *getCanonicalDecl() override; |
1204 | const VarDecl *getCanonicalDecl() const { |
1205 | return const_cast<VarDecl*>(this)->getCanonicalDecl(); |
1206 | } |
1207 | |
1208 | enum DefinitionKind { |
1209 | |
1210 | DeclarationOnly, |
1211 | |
1212 | |
1213 | TentativeDefinition, |
1214 | |
1215 | |
1216 | Definition |
1217 | }; |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | DefinitionKind isThisDeclarationADefinition(ASTContext &) const; |
1223 | DefinitionKind isThisDeclarationADefinition() const { |
1224 | return isThisDeclarationADefinition(getASTContext()); |
1225 | } |
1226 | |
1227 | |
1228 | DefinitionKind hasDefinition(ASTContext &) const; |
1229 | DefinitionKind hasDefinition() const { |
1230 | return hasDefinition(getASTContext()); |
1231 | } |
1232 | |
1233 | |
1234 | |
1235 | VarDecl *getActingDefinition(); |
1236 | const VarDecl *getActingDefinition() const { |
1237 | return const_cast<VarDecl*>(this)->getActingDefinition(); |
1238 | } |
1239 | |
1240 | |
1241 | VarDecl *getDefinition(ASTContext &); |
1242 | const VarDecl *getDefinition(ASTContext &C) const { |
1243 | return const_cast<VarDecl*>(this)->getDefinition(C); |
1244 | } |
1245 | VarDecl *getDefinition() { |
1246 | return getDefinition(getASTContext()); |
1247 | } |
1248 | const VarDecl *getDefinition() const { |
1249 | return const_cast<VarDecl*>(this)->getDefinition(); |
1250 | } |
1251 | |
1252 | |
1253 | |
1254 | bool isOutOfLine() const override; |
1255 | |
1256 | |
1257 | bool isFileVarDecl() const { |
1258 | Kind K = getKind(); |
1259 | if (K == ParmVar || K == ImplicitParam) |
1260 | return false; |
1261 | |
1262 | if (getLexicalDeclContext()->getRedeclContext()->isFileContext()) |
1263 | return true; |
1264 | |
1265 | if (isStaticDataMember()) |
1266 | return true; |
1267 | |
1268 | return false; |
1269 | } |
1270 | |
1271 | |
1272 | |
1273 | const Expr *getAnyInitializer() const { |
1274 | const VarDecl *D; |
1275 | return getAnyInitializer(D); |
1276 | } |
1277 | |
1278 | |
1279 | |
1280 | const Expr *getAnyInitializer(const VarDecl *&D) const; |
1281 | |
1282 | bool hasInit() const; |
1283 | const Expr *getInit() const { |
1284 | return const_cast<VarDecl *>(this)->getInit(); |
1285 | } |
1286 | Expr *getInit(); |
1287 | |
1288 | |
1289 | Stmt **getInitAddress(); |
1290 | |
1291 | void setInit(Expr *I); |
1292 | |
1293 | |
1294 | |
1295 | |
1296 | VarDecl *getInitializingDeclaration(); |
1297 | const VarDecl *getInitializingDeclaration() const { |
1298 | return const_cast<VarDecl *>(this)->getInitializingDeclaration(); |
1299 | } |
1300 | |
1301 | |
1302 | |
1303 | |
1304 | |
1305 | |
1306 | |
1307 | |
1308 | bool mightBeUsableInConstantExpressions(const ASTContext &C) const; |
1309 | |
1310 | |
1311 | |
1312 | |
1313 | bool isUsableInConstantExpressions(const ASTContext &C) const; |
1314 | |
1315 | EvaluatedStmt *ensureEvaluatedStmt() const; |
1316 | EvaluatedStmt *getEvaluatedStmt() const; |
1317 | |
1318 | |
1319 | |
1320 | |
1321 | APValue *evaluateValue() const; |
1322 | |
1323 | private: |
1324 | APValue *evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
1325 | bool IsConstantInitialization) const; |
1326 | |
1327 | public: |
1328 | |
1329 | |
1330 | |
1331 | APValue *getEvaluatedValue() const; |
1332 | |
1333 | |
1334 | |
1335 | |
1336 | |
1337 | |
1338 | |
1339 | bool evaluateDestruction(SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1340 | |
1341 | |
1342 | |
1343 | |
1344 | |
1345 | |
1346 | |
1347 | bool hasConstantInitialization() const; |
1348 | |
1349 | |
1350 | |
1351 | |
1352 | bool hasICEInitializer(const ASTContext &Context) const; |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | bool checkForConstantInitialization( |
1358 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const; |
1359 | |
1360 | void setInitStyle(InitializationStyle Style) { |
1361 | VarDeclBits.InitStyle = Style; |
1362 | } |
1363 | |
1364 | |
1365 | |
1366 | |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | |
1373 | |
1374 | InitializationStyle getInitStyle() const { |
1375 | return static_cast<InitializationStyle>(VarDeclBits.InitStyle); |
1376 | } |
1377 | |
1378 | |
1379 | bool isDirectInit() const { |
1380 | return getInitStyle() != CInit; |
1381 | } |
1382 | |
1383 | |
1384 | bool isThisDeclarationADemotedDefinition() const { |
1385 | return isa<ParmVarDecl>(this) ? false : |
1386 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition; |
1387 | } |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | void demoteThisDefinitionToDeclaration() { |
1395 | assert(isThisDeclarationADefinition() && "Not a definition!"); |
1396 | assert(!isa<ParmVarDecl>(this) && "Cannot demote ParmVarDecls!"); |
1397 | NonParmVarDeclBits.IsThisDeclarationADemotedDefinition = 1; |
1398 | } |
1399 | |
1400 | |
1401 | |
1402 | bool isExceptionVariable() const { |
1403 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.ExceptionVar; |
1404 | } |
1405 | void setExceptionVariable(bool EV) { |
1406 | assert(!isa<ParmVarDecl>(this)); |
1407 | NonParmVarDeclBits.ExceptionVar = EV; |
1408 | } |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | bool isNRVOVariable() const { |
1421 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.NRVOVariable; |
1422 | } |
1423 | void setNRVOVariable(bool NRVO) { |
1424 | assert(!isa<ParmVarDecl>(this)); |
1425 | NonParmVarDeclBits.NRVOVariable = NRVO; |
1426 | } |
1427 | |
1428 | |
1429 | |
1430 | bool isCXXForRangeDecl() const { |
1431 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.CXXForRangeDecl; |
1432 | } |
1433 | void setCXXForRangeDecl(bool FRD) { |
1434 | assert(!isa<ParmVarDecl>(this)); |
1435 | NonParmVarDeclBits.CXXForRangeDecl = FRD; |
1436 | } |
1437 | |
1438 | |
1439 | |
1440 | bool isObjCForDecl() const { |
1441 | return NonParmVarDeclBits.ObjCForDecl; |
1442 | } |
1443 | |
1444 | void setObjCForDecl(bool FRD) { |
1445 | NonParmVarDeclBits.ObjCForDecl = FRD; |
1446 | } |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } |
1456 | void setARCPseudoStrong(bool PS) { VarDeclBits.ARCPseudoStrong = PS; } |
1457 | |
1458 | |
1459 | bool isInline() const { |
1460 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInline; |
1461 | } |
1462 | bool isInlineSpecified() const { |
1463 | return isa<ParmVarDecl>(this) ? false |
1464 | : NonParmVarDeclBits.IsInlineSpecified; |
1465 | } |
1466 | void setInlineSpecified() { |
1467 | assert(!isa<ParmVarDecl>(this)); |
1468 | NonParmVarDeclBits.IsInline = true; |
1469 | NonParmVarDeclBits.IsInlineSpecified = true; |
1470 | } |
1471 | void setImplicitlyInline() { |
1472 | assert(!isa<ParmVarDecl>(this)); |
1473 | NonParmVarDeclBits.IsInline = true; |
1474 | } |
1475 | |
1476 | |
1477 | bool isConstexpr() const { |
1478 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsConstexpr; |
1479 | } |
1480 | void setConstexpr(bool IC) { |
1481 | assert(!isa<ParmVarDecl>(this)); |
1482 | NonParmVarDeclBits.IsConstexpr = IC; |
1483 | } |
1484 | |
1485 | |
1486 | bool isInitCapture() const { |
1487 | return isa<ParmVarDecl>(this) ? false : NonParmVarDeclBits.IsInitCapture; |
1488 | } |
1489 | void setInitCapture(bool IC) { |
1490 | assert(!isa<ParmVarDecl>(this)); |
1491 | NonParmVarDeclBits.IsInitCapture = IC; |
1492 | } |
1493 | |
1494 | |
1495 | |
1496 | bool isParameterPack() const; |
1497 | |
1498 | |
1499 | |
1500 | bool isPreviousDeclInSameBlockScope() const { |
1501 | return isa<ParmVarDecl>(this) |
1502 | ? false |
1503 | : NonParmVarDeclBits.PreviousDeclInSameBlockScope; |
1504 | } |
1505 | void setPreviousDeclInSameBlockScope(bool Same) { |
1506 | assert(!isa<ParmVarDecl>(this)); |
1507 | NonParmVarDeclBits.PreviousDeclInSameBlockScope = Same; |
1508 | } |
1509 | |
1510 | |
1511 | |
1512 | |
1513 | bool isEscapingByref() const; |
1514 | |
1515 | |
1516 | |
1517 | bool isNonEscapingByref() const; |
1518 | |
1519 | void setEscapingByref() { |
1520 | NonParmVarDeclBits.EscapingByref = true; |
1521 | } |
1522 | |
1523 | |
1524 | bool hasDependentAlignment() const; |
1525 | |
1526 | |
1527 | |
1528 | VarDecl *getTemplateInstantiationPattern() const; |
1529 | |
1530 | |
1531 | |
1532 | |
1533 | VarDecl *getInstantiatedFromStaticDataMember() const; |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
1539 | |
1540 | |
1541 | |
1542 | |
1543 | TemplateSpecializationKind |
1544 | getTemplateSpecializationKindForInstantiation() const; |
1545 | |
1546 | |
1547 | |
1548 | |
1549 | SourceLocation getPointOfInstantiation() const; |
1550 | |
1551 | |
1552 | |
1553 | |
1554 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
1555 | |
1556 | |
1557 | |
1558 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
1559 | SourceLocation PointOfInstantiation = SourceLocation()); |
1560 | |
1561 | |
1562 | |
1563 | void setInstantiationOfStaticDataMember(VarDecl *VD, |
1564 | TemplateSpecializationKind TSK); |
1565 | |
1566 | |
1567 | |
1568 | |
1569 | |
1570 | |
1571 | |
1572 | |
1573 | |
1574 | |
1575 | |
1576 | |
1577 | VarTemplateDecl *getDescribedVarTemplate() const; |
1578 | |
1579 | void setDescribedVarTemplate(VarTemplateDecl *Template); |
1580 | |
1581 | |
1582 | |
1583 | |
1584 | bool isKnownToBeDefined() const; |
1585 | |
1586 | |
1587 | |
1588 | bool isNoDestroy(const ASTContext &) const; |
1589 | |
1590 | |
1591 | |
1592 | QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const; |
1593 | |
1594 | |
1595 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1596 | static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } |
1597 | }; |
1598 | |
1599 | class ImplicitParamDecl : public VarDecl { |
1600 | void anchor() override; |
1601 | |
1602 | public: |
1603 | |
1604 | |
1605 | |
1606 | enum ImplicitParamKind : unsigned { |
1607 | |
1608 | ObjCSelf, |
1609 | |
1610 | |
1611 | ObjCCmd, |
1612 | |
1613 | |
1614 | CXXThis, |
1615 | |
1616 | |
1617 | CXXVTT, |
1618 | |
1619 | |
1620 | CapturedContext, |
1621 | |
1622 | |
1623 | Other, |
1624 | }; |
1625 | |
1626 | |
1627 | static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, |
1628 | SourceLocation IdLoc, IdentifierInfo *Id, |
1629 | QualType T, ImplicitParamKind ParamKind); |
1630 | static ImplicitParamDecl *Create(ASTContext &C, QualType T, |
1631 | ImplicitParamKind ParamKind); |
1632 | |
1633 | static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1634 | |
1635 | ImplicitParamDecl(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, |
1636 | IdentifierInfo *Id, QualType Type, |
1637 | ImplicitParamKind ParamKind) |
1638 | : VarDecl(ImplicitParam, C, DC, IdLoc, IdLoc, Id, Type, |
1639 | nullptr, SC_None) { |
1640 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1641 | setImplicit(); |
1642 | } |
1643 | |
1644 | ImplicitParamDecl(ASTContext &C, QualType Type, ImplicitParamKind ParamKind) |
1645 | : VarDecl(ImplicitParam, C, nullptr, SourceLocation(), |
1646 | SourceLocation(), nullptr, Type, |
1647 | nullptr, SC_None) { |
1648 | NonParmVarDeclBits.ImplicitParamKind = ParamKind; |
1649 | setImplicit(); |
1650 | } |
1651 | |
1652 | |
1653 | ImplicitParamKind getParameterKind() const { |
1654 | return static_cast<ImplicitParamKind>(NonParmVarDeclBits.ImplicitParamKind); |
1655 | } |
1656 | |
1657 | |
1658 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1659 | static bool classofKind(Kind K) { return K == ImplicitParam; } |
1660 | }; |
1661 | |
1662 | |
1663 | class ParmVarDecl : public VarDecl { |
1664 | public: |
1665 | enum { MaxFunctionScopeDepth = 255 }; |
1666 | enum { MaxFunctionScopeIndex = 255 }; |
1667 | |
1668 | protected: |
1669 | ParmVarDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1670 | SourceLocation IdLoc, IdentifierInfo *Id, QualType T, |
1671 | TypeSourceInfo *TInfo, StorageClass S, Expr *DefArg) |
1672 | : VarDecl(DK, C, DC, StartLoc, IdLoc, Id, T, TInfo, S) { |
1673 | assert(ParmVarDeclBits.HasInheritedDefaultArg == false); |
1674 | assert(ParmVarDeclBits.DefaultArgKind == DAK_None); |
1675 | assert(ParmVarDeclBits.IsKNRPromoted == false); |
1676 | assert(ParmVarDeclBits.IsObjCMethodParam == false); |
1677 | setDefaultArg(DefArg); |
1678 | } |
1679 | |
1680 | public: |
1681 | static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, |
1682 | SourceLocation StartLoc, |
1683 | SourceLocation IdLoc, IdentifierInfo *Id, |
1684 | QualType T, TypeSourceInfo *TInfo, |
1685 | StorageClass S, Expr *DefArg); |
1686 | |
1687 | static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
1688 | |
1689 | SourceRange getSourceRange() const override LLVM_READONLY; |
1690 | |
1691 | void setObjCMethodScopeInfo(unsigned parameterIndex) { |
1692 | ParmVarDeclBits.IsObjCMethodParam = true; |
1693 | setParameterIndex(parameterIndex); |
1694 | } |
1695 | |
1696 | void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { |
1697 | assert(!ParmVarDeclBits.IsObjCMethodParam); |
1698 | |
1699 | ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; |
1700 | assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth |
1701 | && "truncation!"); |
1702 | |
1703 | setParameterIndex(parameterIndex); |
1704 | } |
1705 | |
1706 | bool isObjCMethodParameter() const { |
1707 | return ParmVarDeclBits.IsObjCMethodParam; |
1708 | } |
1709 | |
1710 | |
1711 | bool isDestroyedInCallee() const; |
1712 | |
1713 | unsigned getFunctionScopeDepth() const { |
1714 | if (ParmVarDeclBits.IsObjCMethodParam) return 0; |
1715 | return ParmVarDeclBits.ScopeDepthOrObjCQuals; |
1716 | } |
1717 | |
1718 | static constexpr unsigned getMaxFunctionScopeDepth() { |
1719 | return (1u << NumScopeDepthOrObjCQualsBits) - 1; |
1720 | } |
1721 | |
1722 | |
1723 | unsigned getFunctionScopeIndex() const { |
1724 | return getParameterIndex(); |
1725 | } |
1726 | |
1727 | ObjCDeclQualifier getObjCDeclQualifier() const { |
1728 | if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; |
1729 | return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); |
1730 | } |
1731 | void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { |
1732 | assert(ParmVarDeclBits.IsObjCMethodParam); |
1733 | ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; |
1734 | } |
1735 | |
1736 | |
1737 | |
1738 | |
1739 | |
1740 | |
1741 | |
1742 | |
1743 | |
1744 | bool isKNRPromoted() const { |
1745 | return ParmVarDeclBits.IsKNRPromoted; |
1746 | } |
1747 | void setKNRPromoted(bool promoted) { |
1748 | ParmVarDeclBits.IsKNRPromoted = promoted; |
1749 | } |
1750 | |
1751 | Expr *getDefaultArg(); |
1752 | const Expr *getDefaultArg() const { |
1753 | return const_cast<ParmVarDecl *>(this)->getDefaultArg(); |
1754 | } |
1755 | |
1756 | void setDefaultArg(Expr *defarg); |
1757 | |
1758 | |
1759 | |
1760 | SourceRange getDefaultArgRange() const; |
1761 | void setUninstantiatedDefaultArg(Expr *arg); |
1762 | Expr *getUninstantiatedDefaultArg(); |
1763 | const Expr *getUninstantiatedDefaultArg() const { |
1764 | return const_cast<ParmVarDecl *>(this)->getUninstantiatedDefaultArg(); |
1765 | } |
1766 | |
1767 | |
1768 | |
1769 | bool hasDefaultArg() const; |
1770 | |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | bool hasUnparsedDefaultArg() const { |
1781 | return ParmVarDeclBits.DefaultArgKind == DAK_Unparsed; |
1782 | } |
1783 | |
1784 | bool hasUninstantiatedDefaultArg() const { |
1785 | return ParmVarDeclBits.DefaultArgKind == DAK_Uninstantiated; |
1786 | } |
1787 | |
1788 | |
1789 | |
1790 | |
1791 | |
1792 | void setUnparsedDefaultArg() { |
1793 | ParmVarDeclBits.DefaultArgKind = DAK_Unparsed; |
1794 | } |
1795 | |
1796 | bool hasInheritedDefaultArg() const { |
1797 | return ParmVarDeclBits.HasInheritedDefaultArg; |
1798 | } |
1799 | |
1800 | void setHasInheritedDefaultArg(bool I = true) { |
1801 | ParmVarDeclBits.HasInheritedDefaultArg = I; |
1802 | } |
1803 | |
1804 | QualType getOriginalType() const; |
1805 | |
1806 | |
1807 | |
1808 | |
1809 | |
1810 | void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } |
1811 | |
1812 | |
1813 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
1814 | static bool classofKind(Kind K) { return K == ParmVar; } |
1815 | |
1816 | private: |
1817 | enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; |
1818 | |
1819 | void setParameterIndex(unsigned parameterIndex) { |
1820 | if (parameterIndex >= ParameterIndexSentinel) { |
1821 | setParameterIndexLarge(parameterIndex); |
1822 | return; |
1823 | } |
1824 | |
1825 | ParmVarDeclBits.ParameterIndex = parameterIndex; |
1826 | assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!"); |
1827 | } |
1828 | unsigned getParameterIndex() const { |
1829 | unsigned d = ParmVarDeclBits.ParameterIndex; |
1830 | return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; |
1831 | } |
1832 | |
1833 | void setParameterIndexLarge(unsigned parameterIndex); |
1834 | unsigned getParameterIndexLarge() const; |
1835 | }; |
1836 | |
1837 | enum class MultiVersionKind { |
1838 | None, |
1839 | Target, |
1840 | CPUSpecific, |
1841 | CPUDispatch |
1842 | }; |
1843 | |
1844 | |
1845 | |
1846 | |
1847 | |
1848 | |
1849 | |
1850 | |
1851 | |
1852 | |
1853 | |
1854 | class FunctionDecl : public DeclaratorDecl, |
1855 | public DeclContext, |
1856 | public Redeclarable<FunctionDecl> { |
1857 | |
1858 | |
1859 | public: |
1860 | |
1861 | enum TemplatedKind { |
1862 | |
1863 | TK_NonTemplate, |
1864 | |
1865 | TK_FunctionTemplate, |
1866 | |
1867 | |
1868 | TK_MemberSpecialization, |
1869 | |
1870 | |
1871 | |
1872 | TK_FunctionTemplateSpecialization, |
1873 | |
1874 | |
1875 | TK_DependentFunctionTemplateSpecialization |
1876 | }; |
1877 | |
1878 | |
1879 | |
1880 | class DefaultedFunctionInfo final |
1881 | : llvm::TrailingObjects<DefaultedFunctionInfo, DeclAccessPair> { |
1882 | friend TrailingObjects; |
1883 | unsigned NumLookups; |
1884 | |
1885 | public: |
1886 | static DefaultedFunctionInfo *Create(ASTContext &Context, |
1887 | ArrayRef<DeclAccessPair> Lookups); |
1888 | |
1889 | |
1890 | ArrayRef<DeclAccessPair> getUnqualifiedLookups() const { |
1891 | return {getTrailingObjects<DeclAccessPair>(), NumLookups}; |
1892 | } |
1893 | }; |
1894 | |
1895 | private: |
1896 | |
1897 | |
1898 | |
1899 | ParmVarDecl **ParamInfo = nullptr; |
1900 | |
1901 | |
1902 | |
1903 | union { |
1904 | |
1905 | LazyDeclStmtPtr Body; |
1906 | |
1907 | DefaultedFunctionInfo *DefaultedInfo; |
1908 | }; |
1909 | |
1910 | unsigned ODRHash; |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | SourceLocation EndRangeLoc; |
1920 | |
1921 | |
1922 | |
1923 | |
1924 | |
1925 | |
1926 | |
1927 | |
1928 | |
1929 | |
1930 | |
1931 | |
1932 | |
1933 | llvm::PointerUnion<FunctionTemplateDecl *, |
1934 | MemberSpecializationInfo *, |
1935 | FunctionTemplateSpecializationInfo *, |
1936 | DependentFunctionTemplateSpecializationInfo *> |
1937 | TemplateOrSpecialization; |
1938 | |
1939 | |
1940 | |
1941 | DeclarationNameLoc DNLoc; |
1942 | |
1943 | |
1944 | |
1945 | |
1946 | |
1947 | |
1948 | |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | |
1955 | |
1956 | |
1957 | |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | |
1963 | |
1964 | void setFunctionTemplateSpecialization(ASTContext &C, |
1965 | FunctionTemplateDecl *Template, |
1966 | const TemplateArgumentList *TemplateArgs, |
1967 | void *InsertPos, |
1968 | TemplateSpecializationKind TSK, |
1969 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
1970 | SourceLocation PointOfInstantiation); |
1971 | |
1972 | |
1973 | |
1974 | void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, |
1975 | TemplateSpecializationKind TSK); |
1976 | |
1977 | void setParams(ASTContext &C, ArrayRef<ParmVarDecl *> NewParamInfo); |
1978 | |
1979 | |
1980 | |
1981 | |
1982 | bool isDeletedBit() const { return FunctionDeclBits.IsDeleted; } |
1983 | |
1984 | |
1985 | bool hasODRHash() const { return FunctionDeclBits.HasODRHash; } |
1986 | |
1987 | |
1988 | void setHasODRHash(bool B = true) { FunctionDeclBits.HasODRHash = B; } |
1989 | |
1990 | protected: |
1991 | FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
1992 | const DeclarationNameInfo &NameInfo, QualType T, |
1993 | TypeSourceInfo *TInfo, StorageClass S, bool isInlineSpecified, |
1994 | ConstexprSpecKind ConstexprKind, |
1995 | Expr *TrailingRequiresClause = nullptr); |
1996 | |
1997 | using redeclarable_base = Redeclarable<FunctionDecl>; |
1998 | |
1999 | FunctionDecl *getNextRedeclarationImpl() override { |
2000 | return getNextRedeclaration(); |
2001 | } |
2002 | |
2003 | FunctionDecl *getPreviousDeclImpl() override { |
2004 | return getPreviousDecl(); |
2005 | } |
2006 | |
2007 | FunctionDecl *getMostRecentDeclImpl() override { |
2008 | return getMostRecentDecl(); |
2009 | } |
2010 | |
2011 | public: |
2012 | friend class ASTDeclReader; |
2013 | friend class ASTDeclWriter; |
2014 | |
2015 | using redecl_range = redeclarable_base::redecl_range; |
2016 | using redecl_iterator = redeclarable_base::redecl_iterator; |
2017 | |
2018 | using redeclarable_base::redecls_begin; |
2019 | using redeclarable_base::redecls_end; |
2020 | using redeclarable_base::redecls; |
2021 | using redeclarable_base::getPreviousDecl; |
2022 | using redeclarable_base::getMostRecentDecl; |
2023 | using redeclarable_base::isFirstDecl; |
2024 | |
2025 | static FunctionDecl * |
2026 | Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
2027 | SourceLocation NLoc, DeclarationName N, QualType T, |
2028 | TypeSourceInfo *TInfo, StorageClass SC, bool isInlineSpecified = false, |
2029 | bool hasWrittenPrototype = true, |
2030 | ConstexprSpecKind ConstexprKind = ConstexprSpecKind::Unspecified, |
2031 | Expr *TrailingRequiresClause = nullptr) { |
2032 | DeclarationNameInfo NameInfo(N, NLoc); |
2033 | return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, SC, |
2034 | isInlineSpecified, hasWrittenPrototype, |
2035 | ConstexprKind, TrailingRequiresClause); |
2036 | } |
2037 | |
2038 | static FunctionDecl *Create(ASTContext &C, DeclContext *DC, |
2039 | SourceLocation StartLoc, |
2040 | const DeclarationNameInfo &NameInfo, QualType T, |
2041 | TypeSourceInfo *TInfo, StorageClass SC, |
2042 | bool isInlineSpecified, bool hasWrittenPrototype, |
2043 | ConstexprSpecKind ConstexprKind, |
2044 | Expr *TrailingRequiresClause); |
2045 | |
2046 | static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2047 | |
2048 | DeclarationNameInfo getNameInfo() const { |
2049 | return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); |
2050 | } |
2051 | |
2052 | void getNameForDiagnostic(raw_ostream &OS, const PrintingPolicy &Policy, |
2053 | bool Qualified) const override; |
2054 | |
2055 | void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } |
2056 | |
2057 | |
2058 | SourceLocation getEllipsisLoc() const { |
2059 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
2060 | if (FPT && FPT->isVariadic()) |
2061 | return FPT->getEllipsisLoc(); |
2062 | return SourceLocation(); |
2063 | } |
2064 | |
2065 | SourceRange getSourceRange() const override LLVM_READONLY; |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | |
2085 | |
2086 | bool hasBody(const FunctionDecl *&Definition) const; |
2087 | |
2088 | bool hasBody() const override { |
2089 | const FunctionDecl* Definition; |
2090 | return hasBody(Definition); |
2091 | } |
2092 | |
2093 | |
2094 | |
2095 | bool hasTrivialBody() const; |
2096 | |
2097 | |
2098 | |
2099 | |
2100 | |
2101 | |
2102 | |
2103 | |
2104 | |
2105 | |
2106 | |
2107 | |
2108 | bool isDefined(const FunctionDecl *&Definition, |
2109 | bool CheckForPendingFriendDefinition = false) const; |
2110 | |
2111 | bool isDefined() const { |
2112 | const FunctionDecl* Definition; |
2113 | return isDefined(Definition); |
2114 | } |
2115 | |
2116 | |
2117 | FunctionDecl *getDefinition() { |
2118 | const FunctionDecl *Definition; |
2119 | if (isDefined(Definition)) |
2120 | return const_cast<FunctionDecl *>(Definition); |
2121 | return nullptr; |
2122 | } |
2123 | const FunctionDecl *getDefinition() const { |
2124 | return const_cast<FunctionDecl *>(this)->getDefinition(); |
2125 | } |
2126 | |
2127 | |
2128 | |
2129 | |
2130 | |
2131 | |
2132 | |
2133 | Stmt *getBody(const FunctionDecl *&Definition) const; |
2134 | |
2135 | Stmt *getBody() const override { |
2136 | const FunctionDecl* Definition; |
2137 | return getBody(Definition); |
2138 | } |
2139 | |
2140 | |
2141 | |
2142 | |
2143 | |
2144 | |
2145 | |
2146 | |
2147 | |
2148 | |
2149 | bool isThisDeclarationADefinition() const { |
2150 | return isDeletedAsWritten() || isDefaulted() || |
2151 | doesThisDeclarationHaveABody() || hasSkippedBody() || |
2152 | willHaveBody() || hasDefiningAttr(); |
2153 | } |
2154 | |
2155 | |
2156 | |
2157 | |
2158 | bool isThisDeclarationInstantiatedFromAFriendDefinition() const; |
2159 | |
2160 | |
2161 | bool doesThisDeclarationHaveABody() const { |
2162 | return (!FunctionDeclBits.HasDefaultedFunctionInfo && Body) || |
2163 | isLateTemplateParsed(); |
2164 | } |
2165 | |
2166 | void setBody(Stmt *B); |
2167 | void setLazyBody(uint64_t Offset) { |
2168 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
2169 | Body = LazyDeclStmtPtr(Offset); |
2170 | } |
2171 | |
2172 | void setDefaultedFunctionInfo(DefaultedFunctionInfo *Info); |
2173 | DefaultedFunctionInfo *getDefaultedFunctionInfo() const; |
2174 | |
2175 | |
2176 | bool isVariadic() const; |
2177 | |
2178 | |
2179 | bool isVirtualAsWritten() const { |
2180 | return FunctionDeclBits.IsVirtualAsWritten; |
2181 | } |
2182 | |
2183 | |
2184 | void setVirtualAsWritten(bool V) { FunctionDeclBits.IsVirtualAsWritten = V; } |
2185 | |
2186 | |
2187 | |
2188 | bool isPure() const { return FunctionDeclBits.IsPure; } |
2189 | void setPure(bool P = true); |
2190 | |
2191 | |
2192 | bool isLateTemplateParsed() const { |
2193 | return FunctionDeclBits.IsLateTemplateParsed; |
2194 | } |
2195 | |
2196 | |
2197 | void setLateTemplateParsed(bool ILT = true) { |
2198 | FunctionDeclBits.IsLateTemplateParsed = ILT; |
2199 | } |
2200 | |
2201 | |
2202 | |
2203 | |
2204 | |
2205 | bool isTrivial() const { return FunctionDeclBits.IsTrivial; } |
2206 | void setTrivial(bool IT) { FunctionDeclBits.IsTrivial = IT; } |
2207 | |
2208 | bool isTrivialForCall() const { return FunctionDeclBits.IsTrivialForCall; } |
2209 | void setTrivialForCall(bool IT) { FunctionDeclBits.IsTrivialForCall = IT; } |
2210 | |
2211 | |
2212 | |
2213 | bool isDefaulted() const { return FunctionDeclBits.IsDefaulted; } |
2214 | void setDefaulted(bool D = true) { FunctionDeclBits.IsDefaulted = D; } |
2215 | |
2216 | |
2217 | bool isExplicitlyDefaulted() const { |
2218 | return FunctionDeclBits.IsExplicitlyDefaulted; |
2219 | } |
2220 | |
2221 | |
2222 | void setExplicitlyDefaulted(bool ED = true) { |
2223 | FunctionDeclBits.IsExplicitlyDefaulted = ED; |
2224 | } |
2225 | |
2226 | |
2227 | |
2228 | bool isUserProvided() const { |
2229 | auto *DeclAsWritten = this; |
2230 | if (FunctionDecl *Pattern = getTemplateInstantiationPattern()) |
2231 | DeclAsWritten = Pattern; |
2232 | return !(DeclAsWritten->isDeleted() || |
2233 | DeclAsWritten->getCanonicalDecl()->isDefaulted()); |
2234 | } |
2235 | |
2236 | |
2237 | |
2238 | |
2239 | bool hasImplicitReturnZero() const { |
2240 | return FunctionDeclBits.HasImplicitReturnZero; |
2241 | } |
2242 | |
2243 | |
2244 | |
2245 | |
2246 | void setHasImplicitReturnZero(bool IRZ) { |
2247 | FunctionDeclBits.HasImplicitReturnZero = IRZ; |
2248 | } |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | bool hasPrototype() const { |
2255 | return hasWrittenPrototype() || hasInheritedPrototype(); |
2256 | } |
2257 | |
2258 | |
2259 | bool hasWrittenPrototype() const { |
2260 | return FunctionDeclBits.HasWrittenPrototype; |
2261 | } |
2262 | |
2263 | |
2264 | void setHasWrittenPrototype(bool P = true) { |
2265 | FunctionDeclBits.HasWrittenPrototype = P; |
2266 | } |
2267 | |
2268 | |
2269 | |
2270 | bool hasInheritedPrototype() const { |
2271 | return FunctionDeclBits.HasInheritedPrototype; |
2272 | } |
2273 | |
2274 | |
2275 | |
2276 | void setHasInheritedPrototype(bool P = true) { |
2277 | FunctionDeclBits.HasInheritedPrototype = P; |
2278 | } |
2279 | |
2280 | |
2281 | bool isConstexpr() const { |
2282 | return getConstexprKind() != ConstexprSpecKind::Unspecified; |
2283 | } |
2284 | void setConstexprKind(ConstexprSpecKind CSK) { |
2285 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(CSK); |
2286 | } |
2287 | ConstexprSpecKind getConstexprKind() const { |
2288 | return static_cast<ConstexprSpecKind>(FunctionDeclBits.ConstexprKind); |
2289 | } |
2290 | bool isConstexprSpecified() const { |
2291 | return getConstexprKind() == ConstexprSpecKind::Constexpr; |
2292 | } |
2293 | bool isConsteval() const { |
2294 | return getConstexprKind() == ConstexprSpecKind::Consteval; |
2295 | } |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | |
2301 | |
2302 | |
2303 | bool instantiationIsPending() const { |
2304 | return FunctionDeclBits.InstantiationIsPending; |
2305 | } |
2306 | |
2307 | |
2308 | |
2309 | void setInstantiationIsPending(bool IC) { |
2310 | FunctionDeclBits.InstantiationIsPending = IC; |
2311 | } |
2312 | |
2313 | |
2314 | bool usesSEHTry() const { return FunctionDeclBits.UsesSEHTry; } |
2315 | void setUsesSEHTry(bool UST) { FunctionDeclBits.UsesSEHTry = UST; } |
2316 | |
2317 | |
2318 | |
2319 | |
2320 | |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | |
2336 | bool isDeleted() const { |
2337 | return getCanonicalDecl()->FunctionDeclBits.IsDeleted; |
2338 | } |
2339 | |
2340 | bool isDeletedAsWritten() const { |
2341 | return FunctionDeclBits.IsDeleted && !isDefaulted(); |
2342 | } |
2343 | |
2344 | void setDeletedAsWritten(bool D = true) { FunctionDeclBits.IsDeleted = D; } |
2345 | |
2346 | |
2347 | |
2348 | bool isMain() const; |
2349 | |
2350 | |
2351 | |
2352 | bool isMSVCRTEntryPoint() const; |
2353 | |
2354 | |
2355 | |
2356 | |
2357 | |
2358 | |
2359 | |
2360 | |
2361 | |
2362 | |
2363 | |
2364 | |
2365 | |
2366 | |
2367 | bool isReservedGlobalPlacementOperator() const; |
2368 | |
2369 | |
2370 | |
2371 | |
2372 | |
2373 | |
2374 | |
2375 | |
2376 | |
2377 | |
2378 | |
2379 | |
2380 | |
2381 | |
2382 | |
2383 | |
2384 | |
2385 | |
2386 | |
2387 | |
2388 | |
2389 | |
2390 | bool isReplaceableGlobalAllocationFunction( |
2391 | Optional<unsigned> *AlignmentParam = nullptr, |
2392 | bool *IsNothrow = nullptr) const; |
2393 | |
2394 | |
2395 | bool isInlineBuiltinDeclaration() const; |
2396 | |
2397 | |
2398 | bool isDestroyingOperatorDelete() const; |
2399 | |
2400 | |
2401 | LanguageLinkage getLanguageLinkage() const; |
2402 | |
2403 | |
2404 | |
2405 | bool isExternC() const; |
2406 | |
2407 | |
2408 | |
2409 | bool isInExternCContext() const; |
2410 | |
2411 | |
2412 | |
2413 | bool isInExternCXXContext() const; |
2414 | |
2415 | |
2416 | bool isGlobal() const; |
2417 | |
2418 | |
2419 | |
2420 | bool isNoReturn() const; |
2421 | |
2422 | |
2423 | bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } |
2424 | void setHasSkippedBody(bool Skipped = true) { |
2425 | FunctionDeclBits.HasSkippedBody = Skipped; |
2426 | } |
2427 | |
2428 | |
2429 | bool willHaveBody() const { return FunctionDeclBits.WillHaveBody; } |
2430 | void setWillHaveBody(bool V = true) { FunctionDeclBits.WillHaveBody = V; } |
2431 | |
2432 | |
2433 | bool isMultiVersion() const { |
2434 | return getCanonicalDecl()->FunctionDeclBits.IsMultiVersion; |
2435 | } |
2436 | |
2437 | |
2438 | |
2439 | void setIsMultiVersion(bool V = true) { |
2440 | getCanonicalDecl()->FunctionDeclBits.IsMultiVersion = V; |
2441 | } |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | MultiVersionKind getMultiVersionKind() const; |
2447 | |
2448 | |
2449 | |
2450 | |
2451 | bool isCPUDispatchMultiVersion() const; |
2452 | |
2453 | |
2454 | bool isCPUSpecificMultiVersion() const; |
2455 | |
2456 | |
2457 | |
2458 | bool isTargetMultiVersion() const; |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 | |
2466 | void getAssociatedConstraints(SmallVectorImpl<const Expr *> &AC) const { |
2467 | if (auto *TRC = getTrailingRequiresClause()) |
2468 | AC.push_back(TRC); |
2469 | } |
2470 | |
2471 | void setPreviousDeclaration(FunctionDecl * PrevDecl); |
2472 | |
2473 | FunctionDecl *getCanonicalDecl() override; |
2474 | const FunctionDecl *getCanonicalDecl() const { |
2475 | return const_cast<FunctionDecl*>(this)->getCanonicalDecl(); |
2476 | } |
2477 | |
2478 | unsigned getBuiltinID(bool ConsiderWrapperFunctions = false) const; |
2479 | |
2480 | |
2481 | ArrayRef<ParmVarDecl *> parameters() const { |
2482 | return {ParamInfo, getNumParams()}; |
2483 | } |
2484 | MutableArrayRef<ParmVarDecl *> parameters() { |
2485 | return {ParamInfo, getNumParams()}; |
2486 | } |
2487 | |
2488 | |
2489 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
2490 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
2491 | |
2492 | bool param_empty() const { return parameters().empty(); } |
2493 | param_iterator param_begin() { return parameters().begin(); } |
2494 | param_iterator param_end() { return parameters().end(); } |
2495 | param_const_iterator param_begin() const { return parameters().begin(); } |
2496 | param_const_iterator param_end() const { return parameters().end(); } |
2497 | size_t param_size() const { return parameters().size(); } |
2498 | |
2499 | |
2500 | |
2501 | |
2502 | unsigned getNumParams() const; |
2503 | |
2504 | const ParmVarDecl *getParamDecl(unsigned i) const { |
2505 | assert(i < getNumParams() && "Illegal param #"); |
2506 | return ParamInfo[i]; |
2507 | } |
2508 | ParmVarDecl *getParamDecl(unsigned i) { |
2509 | assert(i < getNumParams() && "Illegal param #"); |
2510 | return ParamInfo[i]; |
2511 | } |
2512 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
2513 | setParams(getASTContext(), NewParamInfo); |
2514 | } |
2515 | |
2516 | |
2517 | |
2518 | |
2519 | unsigned getMinRequiredArguments() const; |
2520 | |
2521 | |
2522 | |
2523 | |
2524 | |
2525 | |
2526 | |
2527 | bool hasOneParamOrDefaultArgs() const; |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | |
2533 | FunctionTypeLoc getFunctionTypeLoc() const; |
2534 | |
2535 | QualType getReturnType() const { |
2536 | return getType()->castAs<FunctionType>()->getReturnType(); |
2537 | } |
2538 | |
2539 | |
2540 | |
2541 | |
2542 | SourceRange getReturnTypeSourceRange() const; |
2543 | |
2544 | |
2545 | |
2546 | |
2547 | |
2548 | SourceRange getParametersSourceRange() const; |
2549 | |
2550 | |
2551 | |
2552 | QualType getDeclaredReturnType() const { |
2553 | auto *TSI = getTypeSourceInfo(); |
2554 | QualType T = TSI ? TSI->getType() : getType(); |
2555 | return T->castAs<FunctionType>()->getReturnType(); |
2556 | } |
2557 | |
2558 | |
2559 | ExceptionSpecificationType getExceptionSpecType() const { |
2560 | auto *TSI = getTypeSourceInfo(); |
2561 | QualType T = TSI ? TSI->getType() : getType(); |
2562 | const auto *FPT = T->getAs<FunctionProtoType>(); |
2563 | return FPT ? FPT->getExceptionSpecType() : EST_None; |
2564 | } |
2565 | |
2566 | |
2567 | |
2568 | SourceRange getExceptionSpecSourceRange() const; |
2569 | |
2570 | |
2571 | QualType getCallResultType() const { |
2572 | return getType()->castAs<FunctionType>()->getCallResultType( |
2573 | getASTContext()); |
2574 | } |
2575 | |
2576 | |
2577 | |
2578 | StorageClass getStorageClass() const { |
2579 | return static_cast<StorageClass>(FunctionDeclBits.SClass); |
2580 | } |
2581 | |
2582 | |
2583 | void setStorageClass(StorageClass SClass) { |
2584 | FunctionDeclBits.SClass = SClass; |
2585 | } |
2586 | |
2587 | |
2588 | |
2589 | bool isInlineSpecified() const { return FunctionDeclBits.IsInlineSpecified; } |
2590 | |
2591 | |
2592 | void setInlineSpecified(bool I) { |
2593 | FunctionDeclBits.IsInlineSpecified = I; |
2594 | FunctionDeclBits.IsInline = I; |
2595 | } |
2596 | |
2597 | |
2598 | void setImplicitlyInline(bool I = true) { FunctionDeclBits.IsInline = I; } |
2599 | |
2600 | |
2601 | |
2602 | |
2603 | bool isInlined() const { return FunctionDeclBits.IsInline; } |
2604 | |
2605 | bool isInlineDefinitionExternallyVisible() const; |
2606 | |
2607 | bool isMSExternInline() const; |
2608 | |
2609 | bool doesDeclarationForceExternallyVisibleDefinition() const; |
2610 | |
2611 | bool isStatic() const { return getStorageClass() == SC_Static; } |
2612 | |
2613 | |
2614 | |
2615 | bool isOverloadedOperator() const { |
2616 | return getOverloadedOperator() != OO_None; |
2617 | } |
2618 | |
2619 | OverloadedOperatorKind getOverloadedOperator() const; |
2620 | |
2621 | const IdentifierInfo *getLiteralIdentifier() const; |
2622 | |
2623 | |
2624 | |
2625 | |
2626 | |
2627 | |
2628 | |
2629 | |
2630 | |
2631 | |
2632 | |
2633 | |
2634 | |
2635 | |
2636 | |
2637 | |
2638 | |
2639 | |
2640 | |
2641 | |
2642 | |
2643 | |
2644 | FunctionDecl *getInstantiatedFromMemberFunction() const; |
2645 | |
2646 | |
2647 | TemplatedKind getTemplatedKind() const; |
2648 | |
2649 | |
2650 | |
2651 | |
2652 | MemberSpecializationInfo *getMemberSpecializationInfo() const; |
2653 | |
2654 | |
2655 | |
2656 | void setInstantiationOfMemberFunction(FunctionDecl *FD, |
2657 | TemplateSpecializationKind TSK) { |
2658 | setInstantiationOfMemberFunction(getASTContext(), FD, TSK); |
2659 | } |
2660 | |
2661 | |
2662 | |
2663 | |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | |
2669 | |
2670 | |
2671 | |
2672 | |
2673 | FunctionTemplateDecl *getDescribedFunctionTemplate() const; |
2674 | |
2675 | void setDescribedFunctionTemplate(FunctionTemplateDecl *Template); |
2676 | |
2677 | |
2678 | |
2679 | bool isFunctionTemplateSpecialization() const { |
2680 | return getPrimaryTemplate() != nullptr; |
2681 | } |
2682 | |
2683 | |
2684 | |
2685 | |
2686 | FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const; |
2687 | |
2688 | |
2689 | |
2690 | |
2691 | bool isImplicitlyInstantiable() const; |
2692 | |
2693 | |
2694 | |
2695 | bool isTemplateInstantiation() const; |
2696 | |
2697 | |
2698 | |
2699 | |
2700 | |
2701 | |
2702 | |
2703 | |
2704 | |
2705 | FunctionDecl * |
2706 | getTemplateInstantiationPattern(bool ForDefinition = true) const; |
2707 | |
2708 | |
2709 | |
2710 | |
2711 | |
2712 | |
2713 | FunctionTemplateDecl *getPrimaryTemplate() const; |
2714 | |
2715 | |
2716 | |
2717 | |
2718 | |
2719 | |
2720 | const TemplateArgumentList *getTemplateSpecializationArgs() const; |
2721 | |
2722 | |
2723 | |
2724 | |
2725 | |
2726 | |
2727 | |
2728 | |
2729 | const ASTTemplateArgumentListInfo* |
2730 | getTemplateSpecializationArgsAsWritten() const; |
2731 | |
2732 | |
2733 | |
2734 | |
2735 | |
2736 | |
2737 | |
2738 | |
2739 | |
2740 | |
2741 | |
2742 | |
2743 | |
2744 | |
2745 | |
2746 | |
2747 | |
2748 | |
2749 | |
2750 | |
2751 | void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, |
2752 | const TemplateArgumentList *TemplateArgs, |
2753 | void *InsertPos, |
2754 | TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, |
2755 | const TemplateArgumentListInfo *TemplateArgsAsWritten = nullptr, |
2756 | SourceLocation PointOfInstantiation = SourceLocation()) { |
2757 | setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, |
2758 | InsertPos, TSK, TemplateArgsAsWritten, |
2759 | PointOfInstantiation); |
2760 | } |
2761 | |
2762 | |
2763 | |
2764 | void setDependentTemplateSpecialization(ASTContext &Context, |
2765 | const UnresolvedSetImpl &Templates, |
2766 | const TemplateArgumentListInfo &TemplateArgs); |
2767 | |
2768 | DependentFunctionTemplateSpecializationInfo * |
2769 | getDependentSpecializationInfo() const; |
2770 | |
2771 | |
2772 | |
2773 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
2774 | |
2775 | |
2776 | |
2777 | TemplateSpecializationKind |
2778 | getTemplateSpecializationKindForInstantiation() const; |
2779 | |
2780 | |
2781 | |
2782 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
2783 | SourceLocation PointOfInstantiation = SourceLocation()); |
2784 | |
2785 | |
2786 | |
2787 | |
2788 | |
2789 | |
2790 | |
2791 | SourceLocation getPointOfInstantiation() const; |
2792 | |
2793 | |
2794 | |
2795 | bool isOutOfLine() const override; |
2796 | |
2797 | |
2798 | |
2799 | |
2800 | |
2801 | unsigned getMemoryFunctionKind() const; |
2802 | |
2803 | |
2804 | |
2805 | unsigned getODRHash(); |
2806 | |
2807 | |
2808 | |
2809 | unsigned getODRHash() const; |
2810 | |
2811 | |
2812 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
2813 | static bool classofKind(Kind K) { |
2814 | return K >= firstFunction && K <= lastFunction; |
2815 | } |
2816 | static DeclContext *castToDeclContext(const FunctionDecl *D) { |
2817 | return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); |
2818 | } |
2819 | static FunctionDecl *castFromDeclContext(const DeclContext *DC) { |
2820 | return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); |
2821 | } |
2822 | }; |
2823 | |
2824 | |
2825 | class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> { |
2826 | unsigned BitField : 1; |
2827 | unsigned Mutable : 1; |
2828 | mutable unsigned CachedFieldIndex : 30; |
2829 | |
2830 | |
2831 | |
2832 | |
2833 | |
2834 | enum InitStorageKind { |
2835 | |
2836 | |
2837 | |
2838 | ISK_NoInit = (unsigned) ICIS_NoInit, |
2839 | |
2840 | |
2841 | |
2842 | ISK_InClassCopyInit = (unsigned) ICIS_CopyInit, |
2843 | |
2844 | |
2845 | |
2846 | ISK_InClassListInit = (unsigned) ICIS_ListInit, |
2847 | |
2848 | |
2849 | |
2850 | ISK_CapturedVLAType, |
2851 | }; |
2852 | |
2853 | |
2854 | |
2855 | struct InitAndBitWidth { |
2856 | Expr *Init; |
2857 | Expr *BitWidth; |
2858 | }; |
2859 | |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | |
2867 | |
2868 | |
2869 | llvm::PointerIntPair<void *, 2, InitStorageKind> InitStorage; |
2870 | |
2871 | protected: |
2872 | FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, |
2873 | SourceLocation IdLoc, IdentifierInfo *Id, |
2874 | QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2875 | InClassInitStyle InitStyle) |
2876 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
2877 | BitField(false), Mutable(Mutable), CachedFieldIndex(0), |
2878 | InitStorage(nullptr, (InitStorageKind) InitStyle) { |
2879 | if (BW) |
2880 | setBitWidth(BW); |
2881 | } |
2882 | |
2883 | public: |
2884 | friend class ASTDeclReader; |
2885 | friend class ASTDeclWriter; |
2886 | |
2887 | static FieldDecl *Create(const ASTContext &C, DeclContext *DC, |
2888 | SourceLocation StartLoc, SourceLocation IdLoc, |
2889 | IdentifierInfo *Id, QualType T, |
2890 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
2891 | InClassInitStyle InitStyle); |
2892 | |
2893 | static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
2894 | |
2895 | |
2896 | |
2897 | unsigned getFieldIndex() const; |
2898 | |
2899 | |
2900 | bool isMutable() const { return Mutable; } |
2901 | |
2902 | |
2903 | bool isBitField() const { return BitField; } |
2904 | |
2905 | |
2906 | bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); } |
2907 | |
2908 | |
2909 | |
2910 | |
2911 | |
2912 | bool isAnonymousStructOrUnion() const; |
2913 | |
2914 | Expr *getBitWidth() const { |
2915 | if (!BitField) |
2916 | return nullptr; |
2917 | void *Ptr = InitStorage.getPointer(); |
2918 | if (getInClassInitStyle()) |
2919 | return static_cast<InitAndBitWidth*>(Ptr)->BitWidth; |
2920 | return static_cast<Expr*>(Ptr); |
2921 | } |
2922 | |
2923 | unsigned getBitWidthValue(const ASTContext &Ctx) const; |
2924 | |
2925 | |
2926 | |
2927 | void setBitWidth(Expr *Width) { |
2928 | assert(!hasCapturedVLAType() && !BitField && |
2929 | "bit width or captured type already set"); |
2930 | assert(Width && "no bit width specified"); |
2931 | InitStorage.setPointer( |
2932 | InitStorage.getInt() |
2933 | ? new (getASTContext()) |
2934 | InitAndBitWidth{getInClassInitializer(), Width} |
2935 | : static_cast<void*>(Width)); |
2936 | BitField = true; |
2937 | } |
2938 | |
2939 | |
2940 | |
2941 | void removeBitWidth() { |
2942 | assert(isBitField() && "no bitfield width to remove"); |
2943 | InitStorage.setPointer(getInClassInitializer()); |
2944 | BitField = false; |
2945 | } |
2946 | |
2947 | |
2948 | |
2949 | |
2950 | bool isZeroLengthBitField(const ASTContext &Ctx) const; |
2951 | |
2952 | |
2953 | |
2954 | |
2955 | bool isZeroSize(const ASTContext &Ctx) const; |
2956 | |
2957 | |
2958 | InClassInitStyle getInClassInitStyle() const { |
2959 | InitStorageKind storageKind = InitStorage.getInt(); |
2960 | return (storageKind == ISK_CapturedVLAType |
2961 | ? ICIS_NoInit : (InClassInitStyle) storageKind); |
2962 | } |
2963 | |
2964 | |
2965 | bool hasInClassInitializer() const { |
2966 | return getInClassInitStyle() != ICIS_NoInit; |
2967 | } |
2968 | |
2969 | |
2970 | |
2971 | |
2972 | Expr *getInClassInitializer() const { |
2973 | if (!hasInClassInitializer()) |
2974 | return nullptr; |
2975 | void *Ptr = InitStorage.getPointer(); |
2976 | if (BitField) |
2977 | return static_cast<InitAndBitWidth*>(Ptr)->Init; |
2978 | return static_cast<Expr*>(Ptr); |
2979 | } |
2980 | |
2981 | |
2982 | void setInClassInitializer(Expr *Init) { |
2983 | assert(hasInClassInitializer() && !getInClassInitializer()); |
2984 | if (BitField) |
2985 | static_cast<InitAndBitWidth*>(InitStorage.getPointer())->Init = Init; |
2986 | else |
2987 | InitStorage.setPointer(Init); |
2988 | } |
2989 | |
2990 | |
2991 | void removeInClassInitializer() { |
2992 | assert(hasInClassInitializer() && "no initializer to remove"); |
2993 | InitStorage.setPointerAndInt(getBitWidth(), ISK_NoInit); |
2994 | } |
2995 | |
2996 | |
2997 | |
2998 | bool hasCapturedVLAType() const { |
2999 | return InitStorage.getInt() == ISK_CapturedVLAType; |
3000 | } |
3001 | |
3002 | |
3003 | const VariableArrayType *getCapturedVLAType() const { |
3004 | return hasCapturedVLAType() ? static_cast<const VariableArrayType *>( |
3005 | InitStorage.getPointer()) |
3006 | : nullptr; |
3007 | } |
3008 | |
3009 | |
3010 | void setCapturedVLAType(const VariableArrayType *VLAType); |
3011 | |
3012 | |
3013 | |
3014 | |
3015 | |
3016 | |
3017 | const RecordDecl *getParent() const { |
3018 | return dyn_cast<RecordDecl>(getDeclContext()); |
3019 | } |
3020 | |
3021 | RecordDecl *getParent() { |
3022 | return dyn_cast<RecordDecl>(getDeclContext()); |
3023 | } |
3024 | |
3025 | SourceRange getSourceRange() const override LLVM_READONLY; |
3026 | |
3027 | |
3028 | FieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3029 | const FieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3030 | |
3031 | |
3032 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3033 | static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } |
3034 | }; |
3035 | |
3036 | |
3037 | |
3038 | |
3039 | |
3040 | class EnumConstantDecl : public ValueDecl, public Mergeable<EnumConstantDecl> { |
3041 | Stmt *Init; |
3042 | llvm::APSInt Val; |
3043 | |
3044 | protected: |
3045 | EnumConstantDecl(DeclContext *DC, SourceLocation L, |
3046 | IdentifierInfo *Id, QualType T, Expr *E, |
3047 | const llvm::APSInt &V) |
3048 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} |
3049 | |
3050 | public: |
3051 | friend class StmtIteratorBase; |
3052 | |
3053 | static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, |
3054 | SourceLocation L, IdentifierInfo *Id, |
3055 | QualType T, Expr *E, |
3056 | const llvm::APSInt &V); |
3057 | static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3058 | |
3059 | const Expr *getInitExpr() const { return (const Expr*) Init; } |
3060 | Expr *getInitExpr() { return (Expr*) Init; } |
3061 | const llvm::APSInt &getInitVal() const { return Val; } |
3062 | |
3063 | void setInitExpr(Expr *E) { Init = (Stmt*) E; } |
3064 | void setInitVal(const llvm::APSInt &V) { Val = V; } |
3065 | |
3066 | SourceRange getSourceRange() const override LLVM_READONLY; |
3067 | |
3068 | |
3069 | EnumConstantDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3070 | const EnumConstantDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3071 | |
3072 | |
3073 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3074 | static bool classofKind(Kind K) { return K == EnumConstant; } |
3075 | }; |
3076 | |
3077 | |
3078 | |
3079 | class IndirectFieldDecl : public ValueDecl, |
3080 | public Mergeable<IndirectFieldDecl> { |
3081 | NamedDecl **Chaining; |
3082 | unsigned ChainingSize; |
3083 | |
3084 | IndirectFieldDecl(ASTContext &C, DeclContext *DC, SourceLocation L, |
3085 | DeclarationName N, QualType T, |
3086 | MutableArrayRef<NamedDecl *> CH); |
3087 | |
3088 | void anchor() override; |
3089 | |
3090 | public: |
3091 | friend class ASTDeclReader; |
3092 | |
3093 | static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, |
3094 | SourceLocation L, IdentifierInfo *Id, |
3095 | QualType T, llvm::MutableArrayRef<NamedDecl *> CH); |
3096 | |
3097 | static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3098 | |
3099 | using chain_iterator = ArrayRef<NamedDecl *>::const_iterator; |
3100 | |
3101 | ArrayRef<NamedDecl *> chain() const { |
3102 | return llvm::makeArrayRef(Chaining, ChainingSize); |
3103 | } |
3104 | chain_iterator chain_begin() const { return chain().begin(); } |
3105 | chain_iterator chain_end() const { return chain().end(); } |
3106 | |
3107 | unsigned getChainingSize() const { return ChainingSize; } |
3108 | |
3109 | FieldDecl *getAnonField() const { |
3110 | assert(chain().size() >= 2); |
3111 | return cast<FieldDecl>(chain().back()); |
3112 | } |
3113 | |
3114 | VarDecl *getVarDecl() const { |
3115 | assert(chain().size() >= 2); |
3116 | return dyn_cast<VarDecl>(chain().front()); |
3117 | } |
3118 | |
3119 | IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3120 | const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3121 | |
3122 | |
3123 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3124 | static bool classofKind(Kind K) { return K == IndirectField; } |
3125 | }; |
3126 | |
3127 | |
3128 | class TypeDecl : public NamedDecl { |
3129 | friend class ASTContext; |
3130 | |
3131 | |
3132 | |
3133 | |
3134 | |
3135 | mutable const Type *TypeForDecl = nullptr; |
3136 | |
3137 | |
3138 | SourceLocation LocStart; |
3139 | |
3140 | void anchor() override; |
3141 | |
3142 | protected: |
3143 | TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, |
3144 | SourceLocation StartL = SourceLocation()) |
3145 | : NamedDecl(DK, DC, L, Id), LocStart(StartL) {} |
3146 | |
3147 | public: |
3148 | |
3149 | |
3150 | |
3151 | |
3152 | const Type *getTypeForDecl() const { return TypeForDecl; } |
3153 | void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } |
3154 | |
3155 | SourceLocation getBeginLoc() const LLVM_READONLY { return LocStart; } |
3156 | void setLocStart(SourceLocation L) { LocStart = L; } |
3157 | SourceRange getSourceRange() const override LLVM_READONLY { |
3158 | if (LocStart.isValid()) |
3159 | return SourceRange(LocStart, getLocation()); |
3160 | else |
3161 | return SourceRange(getLocation()); |
3162 | } |
3163 | |
3164 | |
3165 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3166 | static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } |
3167 | }; |
3168 | |
3169 | |
3170 | class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { |
3171 | struct alignas(8) ModedTInfo { |
3172 | TypeSourceInfo *first; |
3173 | QualType second; |
3174 | }; |
3175 | |
3176 | |
3177 | |
3178 | mutable llvm::PointerIntPair< |
3179 | llvm::PointerUnion<TypeSourceInfo *, ModedTInfo *>, 2> |
3180 | MaybeModedTInfo; |
3181 | |
3182 | void anchor() override; |
3183 | |
3184 | protected: |
3185 | TypedefNameDecl(Kind DK, ASTContext &C, DeclContext *DC, |
3186 | SourceLocation StartLoc, SourceLocation IdLoc, |
3187 | IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3188 | : TypeDecl(DK, DC, IdLoc, Id, StartLoc), redeclarable_base(C), |
3189 | MaybeModedTInfo(TInfo, 0) {} |
3190 | |
3191 | using redeclarable_base = Redeclarable<TypedefNameDecl>; |
3192 | |
3193 | TypedefNameDecl *getNextRedeclarationImpl() override { |
3194 | return getNextRedeclaration(); |
3195 | } |
3196 | |
3197 | TypedefNameDecl *getPreviousDeclImpl() override { |
3198 | return getPreviousDecl(); |
3199 | } |
3200 | |
3201 | TypedefNameDecl *getMostRecentDeclImpl() override { |
3202 | return getMostRecentDecl(); |
3203 | } |
3204 | |
3205 | public: |
3206 | using redecl_range = redeclarable_base::redecl_range; |
3207 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3208 | |
3209 | using redeclarable_base::redecls_begin; |
3210 | using redeclarable_base::redecls_end; |
3211 | using redeclarable_base::redecls; |
3212 | using redeclarable_base::getPreviousDecl; |
3213 | using redeclarable_base::getMostRecentDecl; |
3214 | using redeclarable_base::isFirstDecl; |
3215 | |
3216 | bool isModed() const { |
3217 | return MaybeModedTInfo.getPointer().is<ModedTInfo *>(); |
3218 | } |
3219 | |
3220 | TypeSourceInfo *getTypeSourceInfo() const { |
3221 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->first |
3222 | : MaybeModedTInfo.getPointer().get<TypeSourceInfo *>(); |
3223 | } |
3224 | |
3225 | QualType getUnderlyingType() const { |
3226 | return isModed() ? MaybeModedTInfo.getPointer().get<ModedTInfo *>()->second |
3227 | : MaybeModedTInfo.getPointer() |
3228 | .get<TypeSourceInfo *>() |
3229 | ->getType(); |
3230 | } |
3231 | |
3232 | void setTypeSourceInfo(TypeSourceInfo *newType) { |
3233 | MaybeModedTInfo.setPointer(newType); |
3234 | } |
3235 | |
3236 | void setModedTypeSourceInfo(TypeSourceInfo *unmodedTSI, QualType modedTy) { |
3237 | MaybeModedTInfo.setPointer(new (getASTContext(), 8) |
3238 | ModedTInfo({unmodedTSI, modedTy})); |
3239 | } |
3240 | |
3241 | |
3242 | TypedefNameDecl *getCanonicalDecl() override { return getFirstDecl(); } |
3243 | const TypedefNameDecl *getCanonicalDecl() const { return getFirstDecl(); } |
3244 | |
3245 | |
3246 | |
3247 | |
3248 | |
3249 | |
3250 | TagDecl *getAnonDeclWithTypedefName(bool AnyRedecl = false) const; |
3251 | |
3252 | |
3253 | |
3254 | bool isTransparentTag() const { |
3255 | if (MaybeModedTInfo.getInt()) |
3256 | return MaybeModedTInfo.getInt() & 0x2; |
3257 | return isTransparentTagSlow(); |
3258 | } |
3259 | |
3260 | |
3261 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3262 | static bool classofKind(Kind K) { |
3263 | return K >= firstTypedefName && K <= lastTypedefName; |
3264 | } |
3265 | |
3266 | private: |
3267 | bool isTransparentTagSlow() const; |
3268 | }; |
3269 | |
3270 | |
3271 | |
3272 | class TypedefDecl : public TypedefNameDecl { |
3273 | TypedefDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3274 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3275 | : TypedefNameDecl(Typedef, C, DC, StartLoc, IdLoc, Id, TInfo) {} |
3276 | |
3277 | public: |
3278 | static TypedefDecl *Create(ASTContext &C, DeclContext *DC, |
3279 | SourceLocation StartLoc, SourceLocation IdLoc, |
3280 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3281 | static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3282 | |
3283 | SourceRange getSourceRange() const override LLVM_READONLY; |
3284 | |
3285 | |
3286 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3287 | static bool classofKind(Kind K) { return K == Typedef; } |
3288 | }; |
3289 | |
3290 | |
3291 | |
3292 | class TypeAliasDecl : public TypedefNameDecl { |
3293 | |
3294 | TypeAliasTemplateDecl *Template; |
3295 | |
3296 | TypeAliasDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3297 | SourceLocation IdLoc, IdentifierInfo *Id, TypeSourceInfo *TInfo) |
3298 | : TypedefNameDecl(TypeAlias, C, DC, StartLoc, IdLoc, Id, TInfo), |
3299 | Template(nullptr) {} |
3300 | |
3301 | public: |
3302 | static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, |
3303 | SourceLocation StartLoc, SourceLocation IdLoc, |
3304 | IdentifierInfo *Id, TypeSourceInfo *TInfo); |
3305 | static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3306 | |
3307 | SourceRange getSourceRange() const override LLVM_READONLY; |
3308 | |
3309 | TypeAliasTemplateDecl *getDescribedAliasTemplate() const { return Template; } |
3310 | void setDescribedAliasTemplate(TypeAliasTemplateDecl *TAT) { Template = TAT; } |
3311 | |
3312 | |
3313 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3314 | static bool classofKind(Kind K) { return K == TypeAlias; } |
3315 | }; |
3316 | |
3317 | |
3318 | class TagDecl : public TypeDecl, |
3319 | public DeclContext, |
3320 | public Redeclarable<TagDecl> { |
3321 | |
3322 | |
3323 | public: |
3324 | |
3325 | using TagKind = TagTypeKind; |
3326 | |
3327 | private: |
3328 | SourceRange BraceRange; |
3329 | |
3330 | |
3331 | |
3332 | using ExtInfo = QualifierInfo; |
3333 | |
3334 | |
3335 | |
3336 | |
3337 | |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | llvm::PointerUnion<TypedefNameDecl *, ExtInfo *> TypedefNameDeclOrQualifier; |
3343 | |
3344 | bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo *>(); } |
3345 | ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo *>(); } |
3346 | const ExtInfo *getExtInfo() const { |
3347 | return TypedefNameDeclOrQualifier.get<ExtInfo *>(); |
3348 | } |
3349 | |
3350 | protected: |
3351 | TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3352 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
3353 | SourceLocation StartL); |
3354 | |
3355 | using redeclarable_base = Redeclarable<TagDecl>; |
3356 | |
3357 | TagDecl *getNextRedeclarationImpl() override { |
3358 | return getNextRedeclaration(); |
3359 | } |
3360 | |
3361 | TagDecl *getPreviousDeclImpl() override { |
3362 | return getPreviousDecl(); |
3363 | } |
3364 | |
3365 | TagDecl *getMostRecentDeclImpl() override { |
3366 | return getMostRecentDecl(); |
3367 | } |
3368 | |
3369 | |
3370 | |
3371 | |
3372 | void completeDefinition(); |
3373 | |
3374 | |
3375 | void setBeingDefined(bool V = true) { TagDeclBits.IsBeingDefined = V; } |
3376 | |
3377 | |
3378 | |
3379 | |
3380 | |
3381 | void setMayHaveOutOfDateDef(bool V = true) { |
3382 | TagDeclBits.MayHaveOutOfDateDef = V; |
3383 | } |
3384 | |
3385 | public: |
3386 | friend class ASTDeclReader; |
3387 | friend class ASTDeclWriter; |
3388 | |
3389 | using redecl_range = redeclarable_base::redecl_range; |
3390 | using redecl_iterator = redeclarable_base::redecl_iterator; |
3391 | |
3392 | using redeclarable_base::redecls_begin; |
3393 | using redeclarable_base::redecls_end; |
3394 | using redeclarable_base::redecls; |
3395 | using redeclarable_base::getPreviousDecl; |
3396 | using redeclarable_base::getMostRecentDecl; |
3397 | using redeclarable_base::isFirstDecl; |
3398 | |
3399 | SourceRange getBraceRange() const { return BraceRange; } |
3400 | void setBraceRange(SourceRange R) { BraceRange = R; } |
3401 | |
3402 | |
3403 | |
3404 | SourceLocation getInnerLocStart() const { return getBeginLoc(); } |
3405 | |
3406 | |
3407 | |
3408 | SourceLocation getOuterLocStart() const; |
3409 | SourceRange getSourceRange() const override LLVM_READONLY; |
3410 | |
3411 | TagDecl *getCanonicalDecl() override; |
3412 | const TagDecl *getCanonicalDecl() const { |
3413 | return const_cast<TagDecl*>(this)->getCanonicalDecl(); |
3414 | } |
3415 | |
3416 | |
3417 | |
3418 | bool isThisDeclarationADefinition() const { |
3419 | return isCompleteDefinition(); |
3420 | } |
3421 | |
3422 | |
3423 | bool isCompleteDefinition() const { return TagDeclBits.IsCompleteDefinition; } |
3424 | |
3425 | |
3426 | void setCompleteDefinition(bool V = true) { |
3427 | TagDeclBits.IsCompleteDefinition = V; |
3428 | } |
3429 | |
3430 | |
3431 | |
3432 | bool isCompleteDefinitionRequired() const { |
3433 | return TagDeclBits.IsCompleteDefinitionRequired; |
3434 | } |
3435 | |
3436 | |
3437 | |
3438 | void setCompleteDefinitionRequired(bool V = true) { |
3439 | TagDeclBits.IsCompleteDefinitionRequired = V; |
3440 | } |
3441 | |
3442 | |
3443 | bool isBeingDefined() const { return TagDeclBits.IsBeingDefined; } |
3444 | |
3445 | |
3446 | |
3447 | bool isEmbeddedInDeclarator() const { |
3448 | return TagDeclBits.IsEmbeddedInDeclarator; |
3449 | } |
3450 | |
3451 | |
3452 | |
3453 | void setEmbeddedInDeclarator(bool isInDeclarator) { |
3454 | TagDeclBits.IsEmbeddedInDeclarator = isInDeclarator; |
3455 | } |
3456 | |
3457 | |
3458 | bool isFreeStanding() const { return TagDeclBits.IsFreeStanding; } |
3459 | |
3460 | |
3461 | void setFreeStanding(bool isFreeStanding = true) { |
3462 | TagDeclBits.IsFreeStanding = isFreeStanding; |
3463 | } |
3464 | |
3465 | |
3466 | |
3467 | |
3468 | |
3469 | bool mayHaveOutOfDateDef() const { return TagDeclBits.MayHaveOutOfDateDef; } |
3470 | |
3471 | |
3472 | |
3473 | |
3474 | bool isDependentType() const { return isDependentContext(); } |
3475 | |
3476 | |
3477 | |
3478 | |
3479 | |
3480 | |
3481 | void startDefinition(); |
3482 | |
3483 | |
3484 | |
3485 | |
3486 | |
3487 | |
3488 | |
3489 | |
3490 | |
3491 | TagDecl *getDefinition() const; |
3492 | |
3493 | StringRef getKindName() const { |
3494 | return TypeWithKeyword::getTagTypeKindName(getTagKind()); |
3495 | } |
3496 | |
3497 | TagKind getTagKind() const { |
3498 | return static_cast<TagKind>(TagDeclBits.TagDeclKind); |
3499 | } |
3500 | |
3501 | void setTagKind(TagKind TK) { TagDeclBits.TagDeclKind = TK; } |
3502 | |
3503 | bool isStruct() const { return getTagKind() == TTK_Struct; } |
3504 | bool isInterface() const { return getTagKind() == TTK_Interface; } |
3505 | bool isClass() const { return getTagKind() == TTK_Class; } |
3506 | bool isUnion() const { return getTagKind() == TTK_Union; } |
| 33 | | Assuming the condition is false | |
|
| 34 | | Returning zero, which participates in a condition later | |
|
| 39 | | Assuming the condition is true | |
|
| 40 | | Returning the value 1, which participates in a condition later | |
|
3507 | bool isEnum() const { return getTagKind() == TTK_Enum; } |
3508 | |
3509 | |
3510 | |
3511 | |
3512 | |
3513 | |
3514 | |
3515 | |
3516 | |
3517 | |
3518 | |
3519 | |
3520 | |
3521 | |
3522 | |
3523 | |
3524 | bool hasNameForLinkage() const { |
3525 | return (getDeclName() || getTypedefNameForAnonDecl()); |
3526 | } |
3527 | |
3528 | TypedefNameDecl *getTypedefNameForAnonDecl() const { |
3529 | return hasExtInfo() ? nullptr |
3530 | : TypedefNameDeclOrQualifier.get<TypedefNameDecl *>(); |
3531 | } |
3532 | |
3533 | void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); |
3534 | |
3535 | |
3536 | |
3537 | NestedNameSpecifier *getQualifier() const { |
3538 | return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() |
3539 | : nullptr; |
3540 | } |
3541 | |
3542 | |
3543 | |
3544 | |
3545 | NestedNameSpecifierLoc getQualifierLoc() const { |
3546 | return hasExtInfo() ? getExtInfo()->QualifierLoc |
3547 | : NestedNameSpecifierLoc(); |
3548 | } |
3549 | |
3550 | void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); |
3551 | |
3552 | unsigned getNumTemplateParameterLists() const { |
3553 | return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; |
3554 | } |
3555 | |
3556 | TemplateParameterList *getTemplateParameterList(unsigned i) const { |
3557 | assert(i < getNumTemplateParameterLists()); |
3558 | return getExtInfo()->TemplParamLists[i]; |
3559 | } |
3560 | |
3561 | void setTemplateParameterListsInfo(ASTContext &Context, |
3562 | ArrayRef<TemplateParameterList *> TPLists); |
3563 | |
3564 | |
3565 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3566 | static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } |
3567 | |
3568 | static DeclContext *castToDeclContext(const TagDecl *D) { |
3569 | return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); |
3570 | } |
3571 | |
3572 | static TagDecl *castFromDeclContext(const DeclContext *DC) { |
3573 | return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); |
3574 | } |
3575 | }; |
3576 | |
3577 | |
3578 | |
3579 | |
3580 | class EnumDecl : public TagDecl { |
3581 | |
3582 | |
3583 | |
3584 | |
3585 | |
3586 | |
3587 | |
3588 | |
3589 | |
3590 | |
3591 | |
3592 | |
3593 | |
3594 | |
3595 | |
3596 | |
3597 | |
3598 | llvm::PointerUnion<const Type *, TypeSourceInfo *> IntegerType; |
3599 | |
3600 | |
3601 | |
3602 | |
3603 | |
3604 | QualType PromotionType; |
3605 | |
3606 | |
3607 | |
3608 | |
3609 | MemberSpecializationInfo *SpecializationInfo = nullptr; |
3610 | |
3611 | |
3612 | |
3613 | |
3614 | unsigned ODRHash; |
3615 | |
3616 | EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
3617 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
3618 | bool Scoped, bool ScopedUsingClassTag, bool Fixed); |
3619 | |
3620 | void anchor() override; |
3621 | |
3622 | void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
3623 | TemplateSpecializationKind TSK); |
3624 | |
3625 | |
3626 | |
3627 | void setNumPositiveBits(unsigned Num) { |
3628 | EnumDeclBits.NumPositiveBits = Num; |
3629 | assert(EnumDeclBits.NumPositiveBits == Num && "can't store this bitcount"); |
3630 | } |
3631 | |
3632 | |
3633 | |
3634 | void setNumNegativeBits(unsigned Num) { EnumDeclBits.NumNegativeBits = Num; } |
3635 | |
3636 | public: |
3637 | |
3638 | |
3639 | void setScoped(bool Scoped = true) { EnumDeclBits.IsScoped = Scoped; } |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | void setScopedUsingClassTag(bool ScopedUCT = true) { |
3646 | EnumDeclBits.IsScopedUsingClassTag = ScopedUCT; |
3647 | } |
3648 | |
3649 | |
3650 | |
3651 | void setFixed(bool Fixed = true) { EnumDeclBits.IsFixed = Fixed; } |
3652 | |
3653 | private: |
3654 | |
3655 | bool hasODRHash() const { return EnumDeclBits.HasODRHash; } |
3656 | void setHasODRHash(bool Hash = true) { EnumDeclBits.HasODRHash = Hash; } |
3657 | |
3658 | public: |
3659 | friend class ASTDeclReader; |
3660 | |
3661 | EnumDecl *getCanonicalDecl() override { |
3662 | return cast<EnumDecl>(TagDecl::getCanonicalDecl()); |
3663 | } |
3664 | const EnumDecl *getCanonicalDecl() const { |
3665 | return const_cast<EnumDecl*>(this)->getCanonicalDecl(); |
3666 | } |
3667 | |
3668 | EnumDecl *getPreviousDecl() { |
3669 | return cast_or_null<EnumDecl>( |
3670 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3671 | } |
3672 | const EnumDecl *getPreviousDecl() const { |
3673 | return const_cast<EnumDecl*>(this)->getPreviousDecl(); |
3674 | } |
3675 | |
3676 | EnumDecl *getMostRecentDecl() { |
3677 | return cast<EnumDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3678 | } |
3679 | const EnumDecl *getMostRecentDecl() const { |
3680 | return const_cast<EnumDecl*>(this)->getMostRecentDecl(); |
3681 | } |
3682 | |
3683 | EnumDecl *getDefinition() const { |
3684 | return cast_or_null<EnumDecl>(TagDecl::getDefinition()); |
3685 | } |
3686 | |
3687 | static EnumDecl *Create(ASTContext &C, DeclContext *DC, |
3688 | SourceLocation StartLoc, SourceLocation IdLoc, |
3689 | IdentifierInfo *Id, EnumDecl *PrevDecl, |
3690 | bool IsScoped, bool IsScopedUsingClassTag, |
3691 | bool IsFixed); |
3692 | static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
3693 | |
3694 | |
3695 | |
3696 | |
3697 | |
3698 | |
3699 | void completeDefinition(QualType NewType, |
3700 | QualType PromotionType, |
3701 | unsigned NumPositiveBits, |
3702 | unsigned NumNegativeBits); |
3703 | |
3704 | |
3705 | using enumerator_iterator = specific_decl_iterator<EnumConstantDecl>; |
3706 | using enumerator_range = |
3707 | llvm::iterator_range<specific_decl_iterator<EnumConstantDecl>>; |
3708 | |
3709 | enumerator_range enumerators() const { |
3710 | return enumerator_range(enumerator_begin(), enumerator_end()); |
3711 | } |
3712 | |
3713 | enumerator_iterator enumerator_begin() const { |
3714 | const EnumDecl *E = getDefinition(); |
3715 | if (!E) |
3716 | E = this; |
3717 | return enumerator_iterator(E->decls_begin()); |
3718 | } |
3719 | |
3720 | enumerator_iterator enumerator_end() const { |
3721 | const EnumDecl *E = getDefinition(); |
3722 | if (!E) |
3723 | E = this; |
3724 | return enumerator_iterator(E->decls_end()); |
3725 | } |
3726 | |
3727 | |
3728 | QualType getPromotionType() const { return PromotionType; } |
3729 | |
3730 | |
3731 | void setPromotionType(QualType T) { PromotionType = T; } |
3732 | |
3733 | |
3734 | |
3735 | |
3736 | QualType getIntegerType() const { |
3737 | if (!IntegerType) |
3738 | return QualType(); |
3739 | if (const Type *T = IntegerType.dyn_cast<const Type*>()) |
3740 | return QualType(T, 0); |
3741 | return IntegerType.get<TypeSourceInfo*>()->getType().getUnqualifiedType(); |
3742 | } |
3743 | |
3744 | |
3745 | void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } |
3746 | |
3747 | |
3748 | void setIntegerTypeSourceInfo(TypeSourceInfo *TInfo) { IntegerType = TInfo; } |
3749 | |
3750 | |
3751 | |
3752 | TypeSourceInfo *getIntegerTypeSourceInfo() const { |
3753 | return IntegerType.dyn_cast<TypeSourceInfo*>(); |
3754 | } |
3755 | |
3756 | |
3757 | |
3758 | SourceRange getIntegerTypeRange() const LLVM_READONLY; |
3759 | |
3760 | |
3761 | |
3762 | unsigned getNumPositiveBits() const { return EnumDeclBits.NumPositiveBits; } |
3763 | |
3764 | |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | |
3770 | |
3771 | |
3772 | |
3773 | unsigned getNumNegativeBits() const { return EnumDeclBits.NumNegativeBits; } |
3774 | |
3775 | |
3776 | bool isScoped() const { return EnumDeclBits.IsScoped; } |
3777 | |
3778 | |
3779 | bool isScopedUsingClassTag() const { |
3780 | return EnumDeclBits.IsScopedUsingClassTag; |
3781 | } |
3782 | |
3783 | |
3784 | |
3785 | bool isFixed() const { return EnumDeclBits.IsFixed; } |
3786 | |
3787 | unsigned getODRHash(); |
3788 | |
3789 | |
3790 | bool isComplete() const { |
3791 | |
3792 | |
3793 | return isCompleteDefinition() || IntegerType; |
3794 | } |
3795 | |
3796 | |
3797 | |
3798 | bool isClosed() const; |
3799 | |
3800 | |
3801 | |
3802 | bool isClosedFlag() const; |
3803 | |
3804 | |
3805 | |
3806 | bool isClosedNonFlag() const; |
3807 | |
3808 | |
3809 | |
3810 | EnumDecl *getTemplateInstantiationPattern() const; |
3811 | |
3812 | |
3813 | |
3814 | |
3815 | EnumDecl *getInstantiatedFromMemberEnum() const; |
3816 | |
3817 | |
3818 | |
3819 | |
3820 | TemplateSpecializationKind getTemplateSpecializationKind() const; |
3821 | |
3822 | |
3823 | |
3824 | void setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
3825 | SourceLocation PointOfInstantiation = SourceLocation()); |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | MemberSpecializationInfo *getMemberSpecializationInfo() const { |
3831 | return SpecializationInfo; |
3832 | } |
3833 | |
3834 | |
3835 | |
3836 | void setInstantiationOfMemberEnum(EnumDecl *ED, |
3837 | TemplateSpecializationKind TSK) { |
3838 | setInstantiationOfMemberEnum(getASTContext(), ED, TSK); |
3839 | } |
3840 | |
3841 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
3842 | static bool classofKind(Kind K) { return K == Enum; } |
3843 | }; |
3844 | |
3845 | |
3846 | |
3847 | |
3848 | |
3849 | class RecordDecl : public TagDecl { |
3850 | |
3851 | |
3852 | public: |
3853 | friend class DeclContext; |
3854 | |
3855 | |
3856 | |
3857 | |
3858 | enum ArgPassingKind : unsigned { |
3859 | |
3860 | APK_CanPassInRegs, |
3861 | |
3862 | |
3863 | |
3864 | |
3865 | |
3866 | |
3867 | |
3868 | |
3869 | APK_CannotPassInRegs, |
3870 | |
3871 | |
3872 | |
3873 | |
3874 | APK_CanNeverPassInRegs |
3875 | }; |
3876 | |
3877 | protected: |
3878 | RecordDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
3879 | SourceLocation StartLoc, SourceLocation IdLoc, |
3880 | IdentifierInfo *Id, RecordDecl *PrevDecl); |
3881 | |
3882 | public: |
3883 | static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
3884 | SourceLocation StartLoc, SourceLocation IdLoc, |
3885 | IdentifierInfo *Id, RecordDecl* PrevDecl = nullptr); |
3886 | static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); |
3887 | |
3888 | RecordDecl *getPreviousDecl() { |
3889 | return cast_or_null<RecordDecl>( |
3890 | static_cast<TagDecl *>(this)->getPreviousDecl()); |
3891 | } |
3892 | const RecordDecl *getPreviousDecl() const { |
3893 | return const_cast<RecordDecl*>(this)->getPreviousDecl(); |
3894 | } |
3895 | |
3896 | RecordDecl *getMostRecentDecl() { |
3897 | return cast<RecordDecl>(static_cast<TagDecl *>(this)->getMostRecentDecl()); |
3898 | } |
3899 | const RecordDecl *getMostRecentDecl() const { |
3900 | return const_cast<RecordDecl*>(this)->getMostRecentDecl(); |
3901 | } |
3902 | |
3903 | bool hasFlexibleArrayMember() const { |
3904 | return RecordDeclBits.HasFlexibleArrayMember; |
3905 | } |
3906 | |
3907 | void setHasFlexibleArrayMember(bool V) { |
3908 | RecordDeclBits.HasFlexibleArrayMember = V; |
3909 | } |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | bool isAnonymousStructOrUnion() const { |
3923 | return RecordDeclBits.AnonymousStructOrUnion; |
3924 | } |
3925 | |
3926 | void setAnonymousStructOrUnion(bool Anon) { |
3927 | RecordDeclBits.AnonymousStructOrUnion = Anon; |
3928 | } |
3929 | |
3930 | bool hasObjectMember() const { return RecordDeclBits.HasObjectMember; } |
3931 | void setHasObjectMember(bool val) { RecordDeclBits.HasObjectMember = val; } |
3932 | |
3933 | bool hasVolatileMember() const { return RecordDeclBits.HasVolatileMember; } |
3934 | |
3935 | void setHasVolatileMember(bool val) { |
3936 | RecordDeclBits.HasVolatileMember = val; |
3937 | } |
3938 | |
3939 | bool hasLoadedFieldsFromExternalStorage() const { |
3940 | return RecordDeclBits.LoadedFieldsFromExternalStorage; |
3941 | } |
3942 | |
3943 | void setHasLoadedFieldsFromExternalStorage(bool val) const { |
3944 | RecordDeclBits.LoadedFieldsFromExternalStorage = val; |
3945 | } |
3946 | |
3947 | |
3948 | bool isNonTrivialToPrimitiveDefaultInitialize() const { |
3949 | return RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize; |
3950 | } |
3951 | |
3952 | void setNonTrivialToPrimitiveDefaultInitialize(bool V) { |
3953 | RecordDeclBits.NonTrivialToPrimitiveDefaultInitialize = V; |
3954 | } |
3955 | |
3956 | bool isNonTrivialToPrimitiveCopy() const { |
3957 | return RecordDeclBits.NonTrivialToPrimitiveCopy; |
3958 | } |
3959 | |
3960 | void setNonTrivialToPrimitiveCopy(bool V) { |
3961 | RecordDeclBits.NonTrivialToPrimitiveCopy = V; |
3962 | } |
3963 | |
3964 | bool isNonTrivialToPrimitiveDestroy() const { |
3965 | return RecordDeclBits.NonTrivialToPrimitiveDestroy; |
3966 | } |
3967 | |
3968 | void setNonTrivialToPrimitiveDestroy(bool V) { |
3969 | RecordDeclBits.NonTrivialToPrimitiveDestroy = V; |
3970 | } |
3971 | |
3972 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
3973 | return RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion; |
3974 | } |
3975 | |
3976 | void setHasNonTrivialToPrimitiveDefaultInitializeCUnion(bool V) { |
3977 | RecordDeclBits.HasNonTrivialToPrimitiveDefaultInitializeCUnion = V; |
3978 | } |
3979 | |
3980 | bool hasNonTrivialToPrimitiveDestructCUnion() const { |
3981 | return RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion; |
3982 | } |
3983 | |
3984 | void setHasNonTrivialToPrimitiveDestructCUnion(bool V) { |
3985 | RecordDeclBits.HasNonTrivialToPrimitiveDestructCUnion = V; |
3986 | } |
3987 | |
3988 | bool hasNonTrivialToPrimitiveCopyCUnion() const { |
3989 | return RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion; |
3990 | } |
3991 | |
3992 | void setHasNonTrivialToPrimitiveCopyCUnion(bool V) { |
3993 | RecordDeclBits.HasNonTrivialToPrimitiveCopyCUnion = V; |
3994 | } |
3995 | |
3996 | |
3997 | |
3998 | |
3999 | bool canPassInRegisters() const { |
4000 | return getArgPassingRestrictions() == APK_CanPassInRegs; |
4001 | } |
4002 | |
4003 | ArgPassingKind getArgPassingRestrictions() const { |
4004 | return static_cast<ArgPassingKind>(RecordDeclBits.ArgPassingRestrictions); |
4005 | } |
4006 | |
4007 | void setArgPassingRestrictions(ArgPassingKind Kind) { |
4008 | RecordDeclBits.ArgPassingRestrictions = Kind; |
4009 | } |
4010 | |
4011 | bool isParamDestroyedInCallee() const { |
4012 | return RecordDeclBits.ParamDestroyedInCallee; |
4013 | } |
4014 | |
4015 | void setParamDestroyedInCallee(bool V) { |
4016 | RecordDeclBits.ParamDestroyedInCallee = V; |
4017 | } |
4018 | |
4019 | |
4020 | |
4021 | |
4022 | |
4023 | |
4024 | |
4025 | |
4026 | |
4027 | |
4028 | |
4029 | |
4030 | |
4031 | |
4032 | bool isInjectedClassName() const; |
4033 | |
4034 | |
4035 | |
4036 | bool isLambda() const; |
4037 | |
4038 | |
4039 | |
4040 | bool isCapturedRecord() const; |
4041 | |
4042 | |
4043 | |
4044 | void setCapturedRecord(); |
4045 | |
4046 | |
4047 | |
4048 | |
4049 | |
4050 | |
4051 | |
4052 | |
4053 | |
4054 | RecordDecl *getDefinition() const { |
4055 | return cast_or_null<RecordDecl>(TagDecl::getDefinition()); |
4056 | } |
4057 | |
4058 | |
4059 | |
4060 | |
4061 | bool isOrContainsUnion() const; |
4062 | |
4063 | |
4064 | |
4065 | |
4066 | using field_iterator = specific_decl_iterator<FieldDecl>; |
4067 | using field_range = llvm::iterator_range<specific_decl_iterator<FieldDecl>>; |
4068 | |
4069 | field_range fields() const { return field_range(field_begin(), field_end()); } |
4070 | field_iterator field_begin() const; |
4071 | |
4072 | field_iterator field_end() const { |
4073 | return field_iterator(decl_iterator()); |
4074 | } |
4075 | |
4076 | |
4077 | bool field_empty() const { |
4078 | return field_begin() == field_end(); |
| |
| 50 | | Returning from 'operator==' | |
|
| 51 | | Returning zero, which participates in a condition later | |
|
4079 | } |
4080 | |
4081 | |
4082 | virtual void completeDefinition(); |
4083 | |
4084 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4085 | static bool classofKind(Kind K) { |
4086 | return K >= firstRecord && K <= lastRecord; |
4087 | } |
4088 | |
4089 | |
4090 | |
4091 | |
4092 | bool isMsStruct(const ASTContext &C) const; |
4093 | |
4094 | |
4095 | |
4096 | |
4097 | bool mayInsertExtraPadding(bool EmitRemark = false) const; |
4098 | |
4099 | |
4100 | |
4101 | const FieldDecl *findFirstNamedDataMember() const; |
4102 | |
4103 | private: |
4104 | |
4105 | void LoadFieldsFromExternalStorage() const; |
4106 | }; |
4107 | |
4108 | class FileScopeAsmDecl : public Decl { |
4109 | StringLiteral *AsmString; |
4110 | SourceLocation RParenLoc; |
4111 | |
4112 | FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, |
4113 | SourceLocation StartL, SourceLocation EndL) |
4114 | : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} |
4115 | |
4116 | virtual void anchor(); |
4117 | |
4118 | public: |
4119 | static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, |
4120 | StringLiteral *Str, SourceLocation AsmLoc, |
4121 | SourceLocation RParenLoc); |
4122 | |
4123 | static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4124 | |
4125 | SourceLocation getAsmLoc() const { return getLocation(); } |
4126 | SourceLocation getRParenLoc() const { return RParenLoc; } |
4127 | void setRParenLoc(SourceLocation L) { RParenLoc = L; } |
4128 | SourceRange getSourceRange() const override LLVM_READONLY { |
4129 | return SourceRange(getAsmLoc(), getRParenLoc()); |
4130 | } |
4131 | |
4132 | const StringLiteral *getAsmString() const { return AsmString; } |
4133 | StringLiteral *getAsmString() { return AsmString; } |
4134 | void setAsmString(StringLiteral *Asm) { AsmString = Asm; } |
4135 | |
4136 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4137 | static bool classofKind(Kind K) { return K == FileScopeAsm; } |
4138 | }; |
4139 | |
4140 | |
4141 | |
4142 | |
4143 | class BlockDecl : public Decl, public DeclContext { |
4144 | |
4145 | |
4146 | public: |
4147 | |
4148 | |
4149 | class Capture { |
4150 | enum { |
4151 | flag_isByRef = 0x1, |
4152 | flag_isNested = 0x2 |
4153 | }; |
4154 | |
4155 | |
4156 | llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; |
4157 | |
4158 | |
4159 | |
4160 | |
4161 | Expr *CopyExpr; |
4162 | |
4163 | public: |
4164 | Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) |
4165 | : VariableAndFlags(variable, |
4166 | (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), |
4167 | CopyExpr(copy) {} |
4168 | |
4169 | |
4170 | VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } |
4171 | |
4172 | |
4173 | |
4174 | bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } |
4175 | |
4176 | bool isEscapingByref() const { |
4177 | return getVariable()->isEscapingByref(); |
4178 | } |
4179 | |
4180 | bool isNonEscapingByref() const { |
4181 | return getVariable()->isNonEscapingByref(); |
4182 | } |
4183 | |
4184 | |
4185 | |
4186 | bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } |
4187 | |
4188 | bool hasCopyExpr() const { return CopyExpr != nullptr; } |
4189 | Expr *getCopyExpr() const { return CopyExpr; } |
4190 | void setCopyExpr(Expr *e) { CopyExpr = e; } |
4191 | }; |
4192 | |
4193 | private: |
4194 | |
4195 | |
4196 | |
4197 | ParmVarDecl **ParamInfo = nullptr; |
4198 | unsigned NumParams = 0; |
4199 | |
4200 | Stmt *Body = nullptr; |
4201 | TypeSourceInfo *SignatureAsWritten = nullptr; |
4202 | |
4203 | const Capture *Captures = nullptr; |
4204 | unsigned NumCaptures = 0; |
4205 | |
4206 | unsigned ManglingNumber = 0; |
4207 | Decl *ManglingContextDecl = nullptr; |
4208 | |
4209 | protected: |
4210 | BlockDecl(DeclContext *DC, SourceLocation CaretLoc); |
4211 | |
4212 | public: |
4213 | static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); |
4214 | static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4215 | |
4216 | SourceLocation getCaretLocation() const { return getLocation(); } |
4217 | |
4218 | bool isVariadic() const { return BlockDeclBits.IsVariadic; } |
4219 | void setIsVariadic(bool value) { BlockDeclBits.IsVariadic = value; } |
4220 | |
4221 | CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } |
4222 | Stmt *getBody() const override { return (Stmt*) Body; } |
4223 | void setBody(CompoundStmt *B) { Body = (Stmt*) B; } |
4224 | |
4225 | void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } |
4226 | TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } |
4227 | |
4228 | |
4229 | ArrayRef<ParmVarDecl *> parameters() const { |
4230 | return {ParamInfo, getNumParams()}; |
4231 | } |
4232 | MutableArrayRef<ParmVarDecl *> parameters() { |
4233 | return {ParamInfo, getNumParams()}; |
4234 | } |
4235 | |
4236 | |
4237 | using param_iterator = MutableArrayRef<ParmVarDecl *>::iterator; |
4238 | using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator; |
4239 | |
4240 | bool param_empty() const { return parameters().empty(); } |
4241 | param_iterator param_begin() { return parameters().begin(); } |
4242 | param_iterator param_end() { return parameters().end(); } |
4243 | param_const_iterator param_begin() const { return parameters().begin(); } |
4244 | param_const_iterator param_end() const { return parameters().end(); } |
4245 | size_t param_size() const { return parameters().size(); } |
4246 | |
4247 | unsigned getNumParams() const { return NumParams; } |
4248 | |
4249 | const ParmVarDecl *getParamDecl(unsigned i) const { |
4250 | assert(i < getNumParams() && "Illegal param #"); |
4251 | return ParamInfo[i]; |
4252 | } |
4253 | ParmVarDecl *getParamDecl(unsigned i) { |
4254 | assert(i < getNumParams() && "Illegal param #"); |
4255 | return ParamInfo[i]; |
4256 | } |
4257 | |
4258 | void setParams(ArrayRef<ParmVarDecl *> NewParamInfo); |
4259 | |
4260 | |
4261 | |
4262 | bool hasCaptures() const { return NumCaptures || capturesCXXThis(); } |
4263 | |
4264 | |
4265 | |
4266 | unsigned getNumCaptures() const { return NumCaptures; } |
4267 | |
4268 | using capture_const_iterator = ArrayRef<Capture>::const_iterator; |
4269 | |
4270 | ArrayRef<Capture> captures() const { return {Captures, NumCaptures}; } |
4271 | |
4272 | capture_const_iterator capture_begin() const { return captures().begin(); } |
4273 | capture_const_iterator capture_end() const { return captures().end(); } |
4274 | |
4275 | bool capturesCXXThis() const { return BlockDeclBits.CapturesCXXThis; } |
4276 | void setCapturesCXXThis(bool B = true) { BlockDeclBits.CapturesCXXThis = B; } |
4277 | |
4278 | bool blockMissingReturnType() const { |
4279 | return BlockDeclBits.BlockMissingReturnType; |
4280 | } |
4281 | |
4282 | void setBlockMissingReturnType(bool val = true) { |
4283 | BlockDeclBits.BlockMissingReturnType = val; |
4284 | } |
4285 | |
4286 | bool isConversionFromLambda() const { |
4287 | return BlockDeclBits.IsConversionFromLambda; |
4288 | } |
4289 | |
4290 | void setIsConversionFromLambda(bool val = true) { |
4291 | BlockDeclBits.IsConversionFromLambda = val; |
4292 | } |
4293 | |
4294 | bool doesNotEscape() const { return BlockDeclBits.DoesNotEscape; } |
4295 | void setDoesNotEscape(bool B = true) { BlockDeclBits.DoesNotEscape = B; } |
4296 | |
4297 | bool canAvoidCopyToHeap() const { |
4298 | return BlockDeclBits.CanAvoidCopyToHeap; |
4299 | } |
4300 | void setCanAvoidCopyToHeap(bool B = true) { |
4301 | BlockDeclBits.CanAvoidCopyToHeap = B; |
4302 | } |
4303 | |
4304 | bool capturesVariable(const VarDecl *var) const; |
4305 | |
4306 | void setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
4307 | bool CapturesCXXThis); |
4308 | |
4309 | unsigned getBlockManglingNumber() const { return ManglingNumber; } |
4310 | |
4311 | Decl *getBlockManglingContextDecl() const { return ManglingContextDecl; } |
4312 | |
4313 | void setBlockMangling(unsigned Number, Decl *Ctx) { |
4314 | ManglingNumber = Number; |
4315 | ManglingContextDecl = Ctx; |
4316 | } |
4317 | |
4318 | SourceRange getSourceRange() const override LLVM_READONLY; |
4319 | |
4320 | |
4321 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4322 | static bool classofKind(Kind K) { return K == Block; } |
4323 | static DeclContext *castToDeclContext(const BlockDecl *D) { |
4324 | return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); |
4325 | } |
4326 | static BlockDecl *castFromDeclContext(const DeclContext *DC) { |
4327 | return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); |
4328 | } |
4329 | }; |
4330 | |
4331 | |
4332 | class CapturedDecl final |
4333 | : public Decl, |
4334 | public DeclContext, |
4335 | private llvm::TrailingObjects<CapturedDecl, ImplicitParamDecl *> { |
4336 | protected: |
4337 | size_t numTrailingObjects(OverloadToken<ImplicitParamDecl>) { |
4338 | return NumParams; |
4339 | } |
4340 | |
4341 | private: |
4342 | |
4343 | unsigned NumParams; |
4344 | |
4345 | |
4346 | unsigned ContextParam; |
4347 | |
4348 | |
4349 | llvm::PointerIntPair<Stmt *, 1, bool> BodyAndNothrow; |
4350 | |
4351 | explicit CapturedDecl(DeclContext *DC, unsigned NumParams); |
4352 | |
4353 | ImplicitParamDecl *const *getParams() const { |
4354 | return getTrailingObjects<ImplicitParamDecl *>(); |
4355 | } |
4356 | |
4357 | ImplicitParamDecl **getParams() { |
4358 | return getTrailingObjects<ImplicitParamDecl *>(); |
4359 | } |
4360 | |
4361 | public: |
4362 | friend class ASTDeclReader; |
4363 | friend class ASTDeclWriter; |
4364 | friend TrailingObjects; |
4365 | |
4366 | static CapturedDecl *Create(ASTContext &C, DeclContext *DC, |
4367 | unsigned NumParams); |
4368 | static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4369 | unsigned NumParams); |
4370 | |
4371 | Stmt *getBody() const override; |
4372 | void setBody(Stmt *B); |
4373 | |
4374 | bool isNothrow() const; |
4375 | void setNothrow(bool Nothrow = true); |
4376 | |
4377 | unsigned getNumParams() const { return NumParams; } |
4378 | |
4379 | ImplicitParamDecl *getParam(unsigned i) const { |
4380 | assert(i < NumParams); |
4381 | return getParams()[i]; |
4382 | } |
4383 | void setParam(unsigned i, ImplicitParamDecl *P) { |
4384 | assert(i < NumParams); |
4385 | getParams()[i] = P; |
4386 | } |
4387 | |
4388 | |
4389 | ArrayRef<ImplicitParamDecl *> parameters() const { |
4390 | return {getParams(), getNumParams()}; |
4391 | } |
4392 | MutableArrayRef<ImplicitParamDecl *> parameters() { |
4393 | return {getParams(), getNumParams()}; |
4394 | } |
4395 | |
4396 | |
4397 | ImplicitParamDecl *getContextParam() const { |
4398 | assert(ContextParam < NumParams); |
4399 | return getParam(ContextParam); |
4400 | } |
4401 | void setContextParam(unsigned i, ImplicitParamDecl *P) { |
4402 | assert(i < NumParams); |
4403 | ContextParam = i; |
4404 | setParam(i, P); |
4405 | } |
4406 | unsigned getContextParamPosition() const { return ContextParam; } |
4407 | |
4408 | using param_iterator = ImplicitParamDecl *const *; |
4409 | using param_range = llvm::iterator_range<param_iterator>; |
4410 | |
4411 | |
4412 | param_iterator param_begin() const { return getParams(); } |
4413 | |
4414 | param_iterator param_end() const { return getParams() + NumParams; } |
4415 | |
4416 | |
4417 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4418 | static bool classofKind(Kind K) { return K == Captured; } |
4419 | static DeclContext *castToDeclContext(const CapturedDecl *D) { |
4420 | return static_cast<DeclContext *>(const_cast<CapturedDecl *>(D)); |
4421 | } |
4422 | static CapturedDecl *castFromDeclContext(const DeclContext *DC) { |
4423 | return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC)); |
4424 | } |
4425 | }; |
4426 | |
4427 | |
4428 | |
4429 | |
4430 | |
4431 | |
4432 | |
4433 | |
4434 | |
4435 | |
4436 | |
4437 | class ImportDecl final : public Decl, |
4438 | llvm::TrailingObjects<ImportDecl, SourceLocation> { |
4439 | friend class ASTContext; |
4440 | friend class ASTDeclReader; |
4441 | friend class ASTReader; |
4442 | friend TrailingObjects; |
4443 | |
4444 | |
4445 | Module *ImportedModule = nullptr; |
4446 | |
4447 | |
4448 | |
4449 | |
4450 | |
4451 | |
4452 | |
4453 | |
4454 | |
4455 | llvm::PointerIntPair<ImportDecl *, 1, bool> NextLocalImportAndComplete; |
4456 | |
4457 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4458 | ArrayRef<SourceLocation> IdentifierLocs); |
4459 | |
4460 | ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, |
4461 | SourceLocation EndLoc); |
4462 | |
4463 | ImportDecl(EmptyShell Empty) : Decl(Import, Empty) {} |
4464 | |
4465 | bool isImportComplete() const { return NextLocalImportAndComplete.getInt(); } |
4466 | |
4467 | void setImportComplete(bool C) { NextLocalImportAndComplete.setInt(C); } |
4468 | |
4469 | |
4470 | |
4471 | ImportDecl *getNextLocalImport() const { |
4472 | return NextLocalImportAndComplete.getPointer(); |
4473 | } |
4474 | |
4475 | void setNextLocalImport(ImportDecl *Import) { |
4476 | NextLocalImportAndComplete.setPointer(Import); |
4477 | } |
4478 | |
4479 | public: |
4480 | |
4481 | static ImportDecl *Create(ASTContext &C, DeclContext *DC, |
4482 | SourceLocation StartLoc, Module *Imported, |
4483 | ArrayRef<SourceLocation> IdentifierLocs); |
4484 | |
4485 | |
4486 | |
4487 | static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, |
4488 | SourceLocation StartLoc, Module *Imported, |
4489 | SourceLocation EndLoc); |
4490 | |
4491 | |
4492 | static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID, |
4493 | unsigned NumLocations); |
4494 | |
4495 | |
4496 | Module *getImportedModule() const { return ImportedModule; } |
4497 | |
4498 | |
4499 | |
4500 | |
4501 | |
4502 | |
4503 | ArrayRef<SourceLocation> getIdentifierLocs() const; |
4504 | |
4505 | SourceRange getSourceRange() const override LLVM_READONLY; |
4506 | |
4507 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4508 | static bool classofKind(Kind K) { return K == Import; } |
4509 | }; |
4510 | |
4511 | |
4512 | |
4513 | |
4514 | |
4515 | |
4516 | |
4517 | class ExportDecl final : public Decl, public DeclContext { |
4518 | virtual void anchor(); |
4519 | |
4520 | private: |
4521 | friend class ASTDeclReader; |
4522 | |
4523 | |
4524 | SourceLocation RBraceLoc; |
4525 | |
4526 | ExportDecl(DeclContext *DC, SourceLocation ExportLoc) |
4527 | : Decl(Export, DC, ExportLoc), DeclContext(Export), |
4528 | RBraceLoc(SourceLocation()) {} |
4529 | |
4530 | public: |
4531 | static ExportDecl *Create(ASTContext &C, DeclContext *DC, |
4532 | SourceLocation ExportLoc); |
4533 | static ExportDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4534 | |
4535 | SourceLocation getExportLoc() const { return getLocation(); } |
4536 | SourceLocation getRBraceLoc() const { return RBraceLoc; } |
4537 | void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } |
4538 | |
4539 | bool hasBraces() const { return RBraceLoc.isValid(); } |
4540 | |
4541 | SourceLocation getEndLoc() const LLVM_READONLY { |
4542 | if (hasBraces()) |
4543 | return RBraceLoc; |
4544 | |
4545 | |
4546 | return decls_empty() ? getLocation() : decls_begin()->getEndLoc(); |
4547 | } |
4548 | |
4549 | SourceRange getSourceRange() const override LLVM_READONLY { |
4550 | return SourceRange(getLocation(), getEndLoc()); |
4551 | } |
4552 | |
4553 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4554 | static bool classofKind(Kind K) { return K == Export; } |
4555 | static DeclContext *castToDeclContext(const ExportDecl *D) { |
4556 | return static_cast<DeclContext *>(const_cast<ExportDecl*>(D)); |
4557 | } |
4558 | static ExportDecl *castFromDeclContext(const DeclContext *DC) { |
4559 | return static_cast<ExportDecl *>(const_cast<DeclContext*>(DC)); |
4560 | } |
4561 | }; |
4562 | |
4563 | |
4564 | class EmptyDecl : public Decl { |
4565 | EmptyDecl(DeclContext *DC, SourceLocation L) : Decl(Empty, DC, L) {} |
4566 | |
4567 | virtual void anchor(); |
4568 | |
4569 | public: |
4570 | static EmptyDecl *Create(ASTContext &C, DeclContext *DC, |
4571 | SourceLocation L); |
4572 | static EmptyDecl *CreateDeserialized(ASTContext &C, unsigned ID); |
4573 | |
4574 | static bool classof(const Decl *D) { return classofKind(D->getKind()); } |
4575 | static bool classofKind(Kind K) { return K == Empty; } |
4576 | }; |
4577 | |
4578 | |
4579 | |
4580 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
4581 | const NamedDecl *ND) { |
4582 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND), |
4583 | DiagnosticsEngine::ak_nameddecl); |
4584 | return PD; |
4585 | } |
4586 | |
4587 | template<typename decl_type> |
4588 | void Redeclarable<decl_type>::setPreviousDecl(decl_type *PrevDecl) { |
4589 | |
4590 | |
4591 | assert(RedeclLink.isFirst() && |
4592 | "setPreviousDecl on a decl already in a redeclaration chain"); |
4593 | |
4594 | if (PrevDecl) { |
4595 | |
4596 | |
4597 | |
4598 | First = PrevDecl->getFirstDecl(); |
4599 | assert(First->RedeclLink.isFirst() && "Expected first"); |
4600 | decl_type *MostRecent = First->getNextRedeclaration(); |
4601 | RedeclLink = PreviousDeclLink(cast<decl_type>(MostRecent)); |
4602 | |
4603 | |
4604 | |
4605 | static_cast<decl_type*>(this)->IdentifierNamespace |= |
4606 | MostRecent->getIdentifierNamespace() & |
4607 | (Decl::IDNS_Ordinary | Decl::IDNS_Tag | Decl::IDNS_Type); |
4608 | } else { |
4609 | |
4610 | First = static_cast<decl_type*>(this); |
4611 | } |
4612 | |
4613 | |
4614 | First->RedeclLink.setLatest(static_cast<decl_type*>(this)); |
4615 | |
4616 | assert(!isa<NamedDecl>(static_cast<decl_type*>(this)) || |
4617 | cast<NamedDecl>(static_cast<decl_type*>(this))->isLinkageValid()); |
4618 | } |
4619 | |
4620 | |
4621 | |
4622 | |
4623 | |
4624 | |
4625 | |
4626 | inline bool IsEnumDeclComplete(EnumDecl *ED) { |
4627 | return ED->isComplete(); |
4628 | } |
4629 | |
4630 | |
4631 | |
4632 | |
4633 | |
4634 | inline bool IsEnumDeclScoped(EnumDecl *ED) { |
4635 | return ED->isScoped(); |
4636 | } |
4637 | |
4638 | |
4639 | |
4640 | |
4641 | static constexpr StringRef getOpenMPVariantManglingSeparatorStr() { |
4642 | return "$ompvariant"; |
4643 | } |
4644 | |
4645 | } |
4646 | |
4647 | #endif // LLVM_CLANG_AST_DECL_H |