clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -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 -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/include -I /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/llvm/include -D NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-09-04-040900-46481-1 -x c++ /build/llvm-toolchain-snapshot-14~++20210903100615+fd66b44ec19e/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() {} |
| 4 | | Returning without writing to 'this->OrigCallee', which participates in a condition later | |
|
496 | CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version) |
497 | : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {} |
498 | |
499 | explicit operator bool() const { return OrigCallee; } |
| 38 | | Returning zero, which participates in a condition later | |
|
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) |
594 | return &LB->second; |
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; |
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()), |
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) |
997 | return false; |
998 | if (NextCallIndex == 0) { |
999 | |
1000 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
1001 | return false; |
1002 | } |
1003 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
1004 | return true; |
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) |
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 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
1830 | EvalInfo &Info); |
1831 | |
1832 | |
1833 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
1834 | EvalInfo &Info); |
1835 | |
1836 | |
1837 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
1838 | EvalInfo &Info); |
1839 | |
1840 | |
1841 | |
1842 | |
1843 | |
1844 | |
1845 | |
1846 | static void negateAsSigned(APSInt &Int) { |
1847 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
1848 | Int = Int.extend(Int.getBitWidth() + 1); |
1849 | Int.setIsSigned(true); |
1850 | } |
1851 | Int = -Int; |
1852 | } |
1853 | |
1854 | template<typename KeyT> |
1855 | APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T, |
1856 | ScopeKind Scope, LValue &LV) { |
1857 | unsigned Version = getTempVersion(); |
1858 | APValue::LValueBase Base(Key, Index, Version); |
1859 | LV.set(Base); |
1860 | return createLocal(Base, Key, T, Scope); |
1861 | } |
1862 | |
1863 | |
1864 | APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD, |
1865 | LValue &LV) { |
1866 | assert(Args.CallIndex == Index && "creating parameter in wrong frame"); |
1867 | APValue::LValueBase Base(PVD, Index, Args.Version); |
1868 | LV.set(Base); |
1869 | |
1870 | |
1871 | |
1872 | return createLocal(Base, PVD, PVD->getType(), ScopeKind::Call); |
1873 | } |
1874 | |
1875 | APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key, |
1876 | QualType T, ScopeKind Scope) { |
1877 | assert(Base.getCallIndex() == Index && "lvalue for wrong frame"); |
1878 | unsigned Version = Base.getVersion(); |
1879 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
1880 | assert(Result.isAbsent() && "local created multiple times"); |
1881 | |
1882 | |
1883 | |
1884 | |
1885 | |
1886 | if (Index <= Info.SpeculativeEvaluationDepth) { |
1887 | if (T.isDestructedType()) |
1888 | Info.noteSideEffect(); |
1889 | } else { |
1890 | Info.CleanupStack.push_back(Cleanup(&Result, Base, T, Scope)); |
1891 | } |
1892 | return Result; |
1893 | } |
1894 | |
1895 | APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) { |
1896 | if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) { |
1897 | FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded); |
1898 | return nullptr; |
1899 | } |
1900 | |
1901 | DynamicAllocLValue DA(NumHeapAllocs++); |
1902 | LV.set(APValue::LValueBase::getDynamicAlloc(DA, T)); |
1903 | auto Result = HeapAllocs.emplace(std::piecewise_construct, |
1904 | std::forward_as_tuple(DA), std::tuple<>()); |
1905 | assert(Result.second && "reused a heap alloc index?"); |
1906 | Result.first->second.AllocExpr = E; |
1907 | return &Result.first->second.Value; |
1908 | } |
1909 | |
1910 | |
1911 | void CallStackFrame::describe(raw_ostream &Out) { |
1912 | unsigned ArgIndex = 0; |
1913 | bool IsMemberCall = isa<CXXMethodDecl>(Callee) && |
1914 | !isa<CXXConstructorDecl>(Callee) && |
1915 | cast<CXXMethodDecl>(Callee)->isInstance(); |
1916 | |
1917 | if (!IsMemberCall) |
1918 | Out << *Callee << '('; |
1919 | |
1920 | if (This && IsMemberCall) { |
1921 | APValue Val; |
1922 | This->moveInto(Val); |
1923 | Val.printPretty(Out, Info.Ctx, |
1924 | This->Designator.MostDerivedType); |
1925 | |
1926 | Out << "->" << *Callee << '('; |
1927 | IsMemberCall = false; |
1928 | } |
1929 | |
1930 | for (FunctionDecl::param_const_iterator I = Callee->param_begin(), |
1931 | E = Callee->param_end(); I != E; ++I, ++ArgIndex) { |
1932 | if (ArgIndex > (unsigned)IsMemberCall) |
1933 | Out << ", "; |
1934 | |
1935 | const ParmVarDecl *Param = *I; |
1936 | APValue *V = Info.getParamSlot(Arguments, Param); |
1937 | if (V) |
1938 | V->printPretty(Out, Info.Ctx, Param->getType()); |
1939 | else |
1940 | Out << "<...>"; |
1941 | |
1942 | if (ArgIndex == 0 && IsMemberCall) |
1943 | Out << "->" << *Callee << '('; |
1944 | } |
1945 | |
1946 | Out << ')'; |
1947 | } |
1948 | |
1949 | |
1950 | |
1951 | |
1952 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
1953 | assert(!E->isValueDependent()); |
1954 | APValue Scratch; |
1955 | if (!Evaluate(Scratch, Info, E)) |
1956 | |
1957 | return Info.noteSideEffect(); |
1958 | return true; |
1959 | } |
1960 | |
1961 | |
1962 | static bool IsStringLiteralCall(const CallExpr *E) { |
1963 | unsigned Builtin = E->getBuiltinCallee(); |
1964 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
1965 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
1966 | } |
1967 | |
1968 | static bool IsGlobalLValue(APValue::LValueBase B) { |
1969 | |
1970 | |
1971 | |
1972 | |
1973 | |
1974 | if (!B) return true; |
1975 | |
1976 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
1977 | |
1978 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
1979 | return VD->hasGlobalStorage(); |
1980 | if (isa<TemplateParamObjectDecl>(D)) |
1981 | return true; |
1982 | |
1983 | |
1984 | return isa<FunctionDecl>(D) || isa<MSGuidDecl>(D); |
1985 | } |
1986 | |
1987 | if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>()) |
1988 | return true; |
1989 | |
1990 | const Expr *E = B.get<const Expr*>(); |
1991 | switch (E->getStmtClass()) { |
1992 | default: |
1993 | return false; |
1994 | case Expr::CompoundLiteralExprClass: { |
1995 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
1996 | return CLE->isFileScope() && CLE->isLValue(); |
1997 | } |
1998 | case Expr::MaterializeTemporaryExprClass: |
1999 | |
2000 | |
2001 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
2002 | |
2003 | case Expr::StringLiteralClass: |
2004 | case Expr::PredefinedExprClass: |
2005 | case Expr::ObjCStringLiteralClass: |
2006 | case Expr::ObjCEncodeExprClass: |
2007 | return true; |
2008 | case Expr::ObjCBoxedExprClass: |
2009 | return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer(); |
2010 | case Expr::CallExprClass: |
2011 | return IsStringLiteralCall(cast<CallExpr>(E)); |
2012 | |
2013 | case Expr::AddrLabelExprClass: |
2014 | return true; |
2015 | |
2016 | |
2017 | case Expr::BlockExprClass: |
2018 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
2019 | case Expr::ImplicitValueInitExprClass: |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | |
2025 | |
2026 | return true; |
2027 | } |
2028 | } |
2029 | |
2030 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
2031 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
2032 | } |
2033 | |
2034 | static bool IsLiteralLValue(const LValue &Value) { |
2035 | if (Value.getLValueCallIndex()) |
2036 | return false; |
2037 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
2038 | return E && !isa<MaterializeTemporaryExpr>(E); |
2039 | } |
2040 | |
2041 | static bool IsWeakLValue(const LValue &Value) { |
2042 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
2043 | return Decl && Decl->isWeak(); |
2044 | } |
2045 | |
2046 | static bool isZeroSized(const LValue &Value) { |
2047 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
2048 | if (Decl && isa<VarDecl>(Decl)) { |
2049 | QualType Ty = Decl->getType(); |
2050 | if (Ty->isArrayType()) |
2051 | return Ty->isIncompleteType() || |
2052 | Decl->getASTContext().getTypeSize(Ty) == 0; |
2053 | } |
2054 | return false; |
2055 | } |
2056 | |
2057 | static bool HasSameBase(const LValue &A, const LValue &B) { |
2058 | if (!A.getLValueBase()) |
2059 | return !B.getLValueBase(); |
2060 | if (!B.getLValueBase()) |
2061 | return false; |
2062 | |
2063 | if (A.getLValueBase().getOpaqueValue() != |
2064 | B.getLValueBase().getOpaqueValue()) |
2065 | return false; |
2066 | |
2067 | return A.getLValueCallIndex() == B.getLValueCallIndex() && |
2068 | A.getLValueVersion() == B.getLValueVersion(); |
2069 | } |
2070 | |
2071 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
2072 | assert(Base && "no location for a null lvalue"); |
2073 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(VD)) { |
2079 | unsigned Idx = PVD->getFunctionScopeIndex(); |
2080 | for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) { |
2081 | if (F->Arguments.CallIndex == Base.getCallIndex() && |
2082 | F->Arguments.Version == Base.getVersion() && F->Callee && |
2083 | Idx < F->Callee->getNumParams()) { |
2084 | VD = F->Callee->getParamDecl(Idx); |
2085 | break; |
2086 | } |
2087 | } |
2088 | } |
2089 | |
2090 | if (VD) |
2091 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2092 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
2093 | Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here); |
2094 | else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
2095 | |
2096 | if (Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA)) |
2097 | Info.Note((*Alloc)->AllocExpr->getExprLoc(), |
2098 | diag::note_constexpr_dynamic_alloc_here); |
2099 | } |
2100 | |
2101 | } |
2102 | |
2103 | enum class CheckEvaluationResultKind { |
2104 | ConstantExpression, |
2105 | FullyInitialized, |
2106 | }; |
2107 | |
2108 | |
2109 | |
2110 | using CheckedTemporaries = |
2111 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>; |
2112 | |
2113 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2114 | EvalInfo &Info, SourceLocation DiagLoc, |
2115 | QualType Type, const APValue &Value, |
2116 | ConstantExprKind Kind, |
2117 | SourceLocation SubobjectLoc, |
2118 | CheckedTemporaries &CheckedTemps); |
2119 | |
2120 | |
2121 | |
2122 | |
2123 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
2124 | QualType Type, const LValue &LVal, |
2125 | ConstantExprKind Kind, |
2126 | CheckedTemporaries &CheckedTemps) { |
2127 | bool IsReferenceType = Type->isReferenceType(); |
2128 | |
2129 | APValue::LValueBase Base = LVal.getLValueBase(); |
2130 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
2131 | |
2132 | const Expr *BaseE = Base.dyn_cast<const Expr *>(); |
2133 | const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>(); |
2134 | |
2135 | |
2136 | |
2137 | |
2138 | if (isTemplateArgument(Kind)) { |
2139 | int InvalidBaseKind = -1; |
2140 | StringRef Ident; |
2141 | if (Base.is<TypeInfoLValue>()) |
2142 | InvalidBaseKind = 0; |
2143 | else if (isa_and_nonnull<StringLiteral>(BaseE)) |
2144 | InvalidBaseKind = 1; |
2145 | else if (isa_and_nonnull<MaterializeTemporaryExpr>(BaseE) || |
2146 | isa_and_nonnull<LifetimeExtendedTemporaryDecl>(BaseVD)) |
2147 | InvalidBaseKind = 2; |
2148 | else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(BaseE)) { |
2149 | InvalidBaseKind = 3; |
2150 | Ident = PE->getIdentKindName(); |
2151 | } |
2152 | |
2153 | if (InvalidBaseKind != -1) { |
2154 | Info.FFDiag(Loc, diag::note_constexpr_invalid_template_arg) |
2155 | << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind |
2156 | << Ident; |
2157 | return false; |
2158 | } |
2159 | } |
2160 | |
2161 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(BaseVD)) { |
2162 | if (FD->isConsteval()) { |
2163 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) |
2164 | << !Type->isAnyPointerType(); |
2165 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2166 | return false; |
2167 | } |
2168 | } |
2169 | |
2170 | |
2171 | |
2172 | |
2173 | if (!IsGlobalLValue(Base)) { |
2174 | if (Info.getLangOpts().CPlusPlus11) { |
2175 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2176 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
2177 | << IsReferenceType << !Designator.Entries.empty() |
2178 | << !!VD << VD; |
2179 | |
2180 | auto *VarD = dyn_cast_or_null<VarDecl>(VD); |
2181 | if (VarD && VarD->isConstexpr()) { |
2182 | |
2183 | |
2184 | |
2185 | |
2186 | |
2187 | Info.Note(VarD->getLocation(), diag::note_constexpr_not_static) |
2188 | << VarD |
2189 | << FixItHint::CreateInsertion(VarD->getBeginLoc(), "static "); |
2190 | } else { |
2191 | NoteLValueLocation(Info, Base); |
2192 | } |
2193 | } else { |
2194 | Info.FFDiag(Loc); |
2195 | } |
2196 | |
2197 | return false; |
2198 | } |
2199 | assert((Info.checkingPotentialConstantExpression() || |
2200 | LVal.getLValueCallIndex() == 0) && |
2201 | "have call index for global lvalue"); |
2202 | |
2203 | if (Base.is<DynamicAllocLValue>()) { |
2204 | Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc) |
2205 | << IsReferenceType << !Designator.Entries.empty(); |
2206 | NoteLValueLocation(Info, Base); |
2207 | return false; |
2208 | } |
2209 | |
2210 | if (BaseVD) { |
2211 | if (const VarDecl *Var = dyn_cast<const VarDecl>(BaseVD)) { |
2212 | |
2213 | if (Var->getTLSKind()) |
2214 | |
2215 | return false; |
2216 | |
2217 | |
2218 | |
2219 | if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>()) |
2220 | |
2221 | return false; |
2222 | } |
2223 | if (const auto *FD = dyn_cast<const FunctionDecl>(BaseVD)) { |
2224 | |
2225 | |
2226 | |
2227 | |
2228 | |
2229 | |
2230 | |
2231 | |
2232 | |
2233 | |
2234 | if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) && |
2235 | FD->hasAttr<DLLImportAttr>()) |
2236 | |
2237 | return false; |
2238 | } |
2239 | } else if (const auto *MTE = |
2240 | dyn_cast_or_null<MaterializeTemporaryExpr>(BaseE)) { |
2241 | if (CheckedTemps.insert(MTE).second) { |
2242 | QualType TempType = getType(Base); |
2243 | if (TempType.isDestructedType()) { |
2244 | Info.FFDiag(MTE->getExprLoc(), |
2245 | diag::note_constexpr_unsupported_temporary_nontrivial_dtor) |
2246 | << TempType; |
2247 | return false; |
2248 | } |
2249 | |
2250 | APValue *V = MTE->getOrCreateValue(false); |
2251 | assert(V && "evasluation result refers to uninitialised temporary"); |
2252 | if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2253 | Info, MTE->getExprLoc(), TempType, *V, |
2254 | Kind, SourceLocation(), CheckedTemps)) |
2255 | return false; |
2256 | } |
2257 | } |
2258 | |
2259 | |
2260 | |
2261 | if (!IsReferenceType) |
2262 | return true; |
2263 | |
2264 | |
2265 | if (!Base) { |
2266 | |
2267 | Info.CCEDiag(Loc); |
2268 | return true; |
2269 | } |
2270 | |
2271 | |
2272 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
2273 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
2274 | << !Designator.Entries.empty() << !!BaseVD << BaseVD; |
2275 | NoteLValueLocation(Info, Base); |
2276 | } |
2277 | |
2278 | return true; |
2279 | } |
2280 | |
2281 | |
2282 | |
2283 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
2284 | SourceLocation Loc, |
2285 | QualType Type, |
2286 | const APValue &Value, |
2287 | ConstantExprKind Kind) { |
2288 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
2289 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
2290 | if (!FD) |
2291 | return true; |
2292 | if (FD->isConsteval()) { |
2293 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) << 0; |
2294 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2295 | return false; |
2296 | } |
2297 | return isForManglingOnly(Kind) || FD->isVirtual() || |
2298 | !FD->hasAttr<DLLImportAttr>(); |
2299 | } |
2300 | |
2301 | |
2302 | |
2303 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
2304 | const LValue *This = nullptr) { |
2305 | if (!E->isPRValue() || E->getType()->isLiteralType(Info.Ctx)) |
2306 | return true; |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | |
2313 | |
2314 | |
2315 | |
2316 | |
2317 | |
2318 | |
2319 | |
2320 | |
2321 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
2322 | return true; |
2323 | |
2324 | |
2325 | if (Info.getLangOpts().CPlusPlus11) |
2326 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
2327 | << E->getType(); |
2328 | else |
2329 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2330 | return false; |
2331 | } |
2332 | |
2333 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2334 | EvalInfo &Info, SourceLocation DiagLoc, |
2335 | QualType Type, const APValue &Value, |
2336 | ConstantExprKind Kind, |
2337 | SourceLocation SubobjectLoc, |
2338 | CheckedTemporaries &CheckedTemps) { |
2339 | if (!Value.hasValue()) { |
2340 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2341 | << true << Type; |
2342 | if (SubobjectLoc.isValid()) |
2343 | Info.Note(SubobjectLoc, diag::note_constexpr_subobject_declared_here); |
2344 | return false; |
2345 | } |
2346 | |
2347 | |
2348 | |
2349 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
2350 | Type = AT->getValueType(); |
2351 | |
2352 | |
2353 | |
2354 | |
2355 | if (Value.isArray()) { |
2356 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
2357 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
2358 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
2359 | Value.getArrayInitializedElt(I), Kind, |
2360 | SubobjectLoc, CheckedTemps)) |
2361 | return false; |
2362 | } |
2363 | if (!Value.hasArrayFiller()) |
2364 | return true; |
2365 | return CheckEvaluationResult(CERK, Info, DiagLoc, EltTy, |
2366 | Value.getArrayFiller(), Kind, SubobjectLoc, |
2367 | CheckedTemps); |
2368 | } |
2369 | if (Value.isUnion() && Value.getUnionField()) { |
2370 | return CheckEvaluationResult( |
2371 | CERK, Info, DiagLoc, Value.getUnionField()->getType(), |
2372 | Value.getUnionValue(), Kind, Value.getUnionField()->getLocation(), |
2373 | CheckedTemps); |
2374 | } |
2375 | if (Value.isStruct()) { |
2376 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
2377 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
2378 | unsigned BaseIndex = 0; |
2379 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
2380 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, BS.getType(), |
2381 | Value.getStructBase(BaseIndex), Kind, |
2382 | BS.getBeginLoc(), CheckedTemps)) |
2383 | return false; |
2384 | ++BaseIndex; |
2385 | } |
2386 | } |
2387 | for (const auto *I : RD->fields()) { |
2388 | if (I->isUnnamedBitfield()) |
2389 | continue; |
2390 | |
2391 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(), |
2392 | Value.getStructField(I->getFieldIndex()), |
2393 | Kind, I->getLocation(), CheckedTemps)) |
2394 | return false; |
2395 | } |
2396 | } |
2397 | |
2398 | if (Value.isLValue() && |
2399 | CERK == CheckEvaluationResultKind::ConstantExpression) { |
2400 | LValue LVal; |
2401 | LVal.setFrom(Info.Ctx, Value); |
2402 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Kind, |
2403 | CheckedTemps); |
2404 | } |
2405 | |
2406 | if (Value.isMemberPointer() && |
2407 | CERK == CheckEvaluationResultKind::ConstantExpression) |
2408 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Kind); |
2409 | |
2410 | |
2411 | return true; |
2412 | } |
2413 | |
2414 | |
2415 | |
2416 | |
2417 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
2418 | QualType Type, const APValue &Value, |
2419 | ConstantExprKind Kind) { |
2420 | |
2421 | if (Type->isVoidType()) |
2422 | return true; |
2423 | |
2424 | CheckedTemporaries CheckedTemps; |
2425 | return CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2426 | Info, DiagLoc, Type, Value, Kind, |
2427 | SourceLocation(), CheckedTemps); |
2428 | } |
2429 | |
2430 | |
2431 | |
2432 | static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, |
2433 | QualType Type, const APValue &Value) { |
2434 | CheckedTemporaries CheckedTemps; |
2435 | return CheckEvaluationResult( |
2436 | CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value, |
2437 | ConstantExprKind::Normal, SourceLocation(), CheckedTemps); |
2438 | } |
2439 | |
2440 | |
2441 | |
2442 | static bool CheckMemoryLeaks(EvalInfo &Info) { |
2443 | if (!Info.HeapAllocs.empty()) { |
2444 | |
2445 | |
2446 | |
2447 | Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr, |
2448 | diag::note_constexpr_memory_leak) |
2449 | << unsigned(Info.HeapAllocs.size() - 1); |
2450 | } |
2451 | return true; |
2452 | } |
2453 | |
2454 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
2455 | |
2456 | |
2457 | if (!Value.getLValueBase()) { |
2458 | Result = !Value.getLValueOffset().isZero(); |
2459 | return true; |
2460 | } |
2461 | |
2462 | |
2463 | |
2464 | Result = true; |
2465 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
2466 | return !Decl || !Decl->isWeak(); |
2467 | } |
2468 | |
2469 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
2470 | switch (Val.getKind()) { |
2471 | case APValue::None: |
2472 | case APValue::Indeterminate: |
2473 | return false; |
2474 | case APValue::Int: |
2475 | Result = Val.getInt().getBoolValue(); |
2476 | return true; |
2477 | case APValue::FixedPoint: |
2478 | Result = Val.getFixedPoint().getBoolValue(); |
2479 | return true; |
2480 | case APValue::Float: |
2481 | Result = !Val.getFloat().isZero(); |
2482 | return true; |
2483 | case APValue::ComplexInt: |
2484 | Result = Val.getComplexIntReal().getBoolValue() || |
2485 | Val.getComplexIntImag().getBoolValue(); |
2486 | return true; |
2487 | case APValue::ComplexFloat: |
2488 | Result = !Val.getComplexFloatReal().isZero() || |
2489 | !Val.getComplexFloatImag().isZero(); |
2490 | return true; |
2491 | case APValue::LValue: |
2492 | return EvalPointerValueAsBool(Val, Result); |
2493 | case APValue::MemberPointer: |
2494 | Result = Val.getMemberPointerDecl(); |
2495 | return true; |
2496 | case APValue::Vector: |
2497 | case APValue::Array: |
2498 | case APValue::Struct: |
2499 | case APValue::Union: |
2500 | case APValue::AddrLabelDiff: |
2501 | return false; |
2502 | } |
2503 | |
2504 | llvm_unreachable("unknown APValue kind"); |
2505 | } |
2506 | |
2507 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
2508 | EvalInfo &Info) { |
2509 | assert(!E->isValueDependent()); |
2510 | assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
2511 | APValue Val; |
2512 | if (!Evaluate(Val, Info, E)) |
2513 | return false; |
2514 | return HandleConversionToBool(Val, Result); |
2515 | } |
2516 | |
2517 | template<typename T> |
2518 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
2519 | const T &SrcValue, QualType DestType) { |
2520 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
2521 | << SrcValue << DestType; |
2522 | return Info.noteUndefinedBehavior(); |
2523 | } |
2524 | |
2525 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
2526 | QualType SrcType, const APFloat &Value, |
2527 | QualType DestType, APSInt &Result) { |
2528 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2529 | |
2530 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
2531 | |
2532 | Result = APSInt(DestWidth, !DestSigned); |
2533 | bool ignored; |
2534 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
2535 | & APFloat::opInvalidOp) |
2536 | return HandleOverflow(Info, E, Value, DestType); |
2537 | return true; |
2538 | } |
2539 | |
2540 | |
2541 | |
2542 | |
2543 | |
2544 | |
2545 | |
2546 | static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E, |
2547 | bool &DynamicRM) { |
2548 | llvm::RoundingMode RM = |
2549 | E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).getRoundingMode(); |
2550 | DynamicRM = (RM == llvm::RoundingMode::Dynamic); |
2551 | if (DynamicRM) |
2552 | RM = llvm::RoundingMode::NearestTiesToEven; |
2553 | return RM; |
2554 | } |
2555 | |
2556 | |
2557 | static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E, |
2558 | APFloat::opStatus St) { |
2559 | |
2560 | |
2561 | if (Info.InConstantContext) |
2562 | return true; |
2563 | |
2564 | FPOptions FPO = E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()); |
2565 | if ((St & APFloat::opInexact) && |
2566 | FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) { |
2567 | |
2568 | |
2569 | Info.FFDiag(E, diag::note_constexpr_dynamic_rounding); |
2570 | return false; |
2571 | } |
2572 | |
2573 | if ((St != APFloat::opOK) && |
2574 | (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic || |
2575 | FPO.getFPExceptionMode() != LangOptions::FPE_Ignore || |
2576 | FPO.getAllowFEnvAccess())) { |
2577 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2578 | return false; |
2579 | } |
2580 | |
2581 | if ((St & APFloat::opStatus::opInvalidOp) && |
2582 | FPO.getFPExceptionMode() != LangOptions::FPE_Ignore) { |
2583 | |
2584 | Info.FFDiag(E); |
2585 | return false; |
2586 | } |
2587 | |
2588 | |
2589 | |
2590 | |
2591 | |
2592 | |
2593 | |
2594 | return true; |
2595 | } |
2596 | |
2597 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
2598 | QualType SrcType, QualType DestType, |
2599 | APFloat &Result) { |
2600 | assert(isa<CastExpr>(E) || isa<CompoundAssignOperator>(E)); |
2601 | bool DynamicRM; |
2602 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E, DynamicRM); |
2603 | APFloat::opStatus St; |
2604 | APFloat Value = Result; |
2605 | bool ignored; |
2606 | St = Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), RM, &ignored); |
2607 | return checkFloatingPointResult(Info, E, St); |
2608 | } |
2609 | |
2610 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
2611 | QualType DestType, QualType SrcType, |
2612 | const APSInt &Value) { |
2613 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2614 | |
2615 | |
2616 | APSInt Result = Value.extOrTrunc(DestWidth); |
2617 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
2618 | if (DestType->isBooleanType()) |
2619 | Result = Value.getBoolValue(); |
2620 | return Result; |
2621 | } |
2622 | |
2623 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
2624 | const FPOptions FPO, |
2625 | QualType SrcType, const APSInt &Value, |
2626 | QualType DestType, APFloat &Result) { |
2627 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
2628 | APFloat::opStatus St = Result.convertFromAPInt(Value, Value.isSigned(), |
2629 | APFloat::rmNearestTiesToEven); |
2630 | if (!Info.InConstantContext && St != llvm::APFloatBase::opOK && |
2631 | FPO.isFPConstrained()) { |
2632 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2633 | return false; |
2634 | } |
2635 | return true; |
2636 | } |
2637 | |
2638 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
2639 | APValue &Value, const FieldDecl *FD) { |
2640 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
2641 | |
2642 | if (!Value.isInt()) { |
2643 | |
2644 | |
2645 | |
2646 | assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
2647 | Info.FFDiag(E); |
2648 | return false; |
2649 | } |
2650 | |
2651 | APSInt &Int = Value.getInt(); |
2652 | unsigned OldBitWidth = Int.getBitWidth(); |
2653 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
2654 | if (NewBitWidth < OldBitWidth) |
2655 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
2656 | return true; |
2657 | } |
2658 | |
2659 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
2660 | llvm::APInt &Res) { |
2661 | APValue SVal; |
2662 | if (!Evaluate(SVal, Info, E)) |
2663 | return false; |
2664 | if (SVal.isInt()) { |
2665 | Res = SVal.getInt(); |
2666 | return true; |
2667 | } |
2668 | if (SVal.isFloat()) { |
2669 | Res = SVal.getFloat().bitcastToAPInt(); |
2670 | return true; |
2671 | } |
2672 | if (SVal.isVector()) { |
2673 | QualType VecTy = E->getType(); |
2674 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
2675 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
2676 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
2677 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
2678 | Res = llvm::APInt::getNullValue(VecSize); |
2679 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
2680 | APValue &Elt = SVal.getVectorElt(i); |
2681 | llvm::APInt EltAsInt; |
2682 | if (Elt.isInt()) { |
2683 | EltAsInt = Elt.getInt(); |
2684 | } else if (Elt.isFloat()) { |
2685 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
2686 | } else { |
2687 | |
2688 | |
2689 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2690 | return false; |
2691 | } |
2692 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
2693 | if (BigEndian) |
2694 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
2695 | else |
2696 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
2697 | } |
2698 | return true; |
2699 | } |
2700 | |
2701 | |
2702 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2703 | return false; |
2704 | } |
2705 | |
2706 | |
2707 | |
2708 | |
2709 | template<typename Operation> |
2710 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
2711 | const APSInt &LHS, const APSInt &RHS, |
2712 | unsigned BitWidth, Operation Op, |
2713 | APSInt &Result) { |
2714 | if (LHS.isUnsigned()) { |
2715 | Result = Op(LHS, RHS); |
2716 | return true; |
2717 | } |
2718 | |
2719 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
2720 | Result = Value.trunc(LHS.getBitWidth()); |
2721 | if (Result.extend(BitWidth) != Value) { |
2722 | if (Info.checkingForUndefinedBehavior()) |
2723 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
2724 | diag::warn_integer_constant_overflow) |
2725 | << toString(Result, 10) << E->getType(); |
2726 | return HandleOverflow(Info, E, Value, E->getType()); |
2727 | } |
2728 | return true; |
2729 | } |
2730 | |
2731 | |
2732 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
2733 | BinaryOperatorKind Opcode, APSInt RHS, |
2734 | APSInt &Result) { |
2735 | switch (Opcode) { |
2736 | default: |
2737 | Info.FFDiag(E); |
2738 | return false; |
2739 | case BO_Mul: |
2740 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2741 | std::multiplies<APSInt>(), Result); |
2742 | case BO_Add: |
2743 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2744 | std::plus<APSInt>(), Result); |
2745 | case BO_Sub: |
2746 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2747 | std::minus<APSInt>(), Result); |
2748 | case BO_And: Result = LHS & RHS; return true; |
2749 | case BO_Xor: Result = LHS ^ RHS; return true; |
2750 | case BO_Or: Result = LHS | RHS; return true; |
2751 | case BO_Div: |
2752 | case BO_Rem: |
2753 | if (RHS == 0) { |
2754 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
2755 | return false; |
2756 | } |
2757 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2758 | |
2759 | |
2760 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
2761 | LHS.isSigned() && LHS.isMinSignedValue()) |
2762 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
2763 | E->getType()); |
2764 | return true; |
2765 | case BO_Shl: { |
2766 | if (Info.getLangOpts().OpenCL) |
2767 | |
2768 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2769 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2770 | RHS.isUnsigned()); |
2771 | else if (RHS.isSigned() && RHS.isNegative()) { |
2772 | |
2773 | |
2774 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2775 | RHS = -RHS; |
2776 | goto shift_right; |
2777 | } |
2778 | shift_left: |
2779 | |
2780 | |
2781 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2782 | if (SA != RHS) { |
2783 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2784 | << RHS << E->getType() << LHS.getBitWidth(); |
2785 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) { |
2786 | |
2787 | |
2788 | |
2789 | |
2790 | if (LHS.isNegative()) |
2791 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
2792 | else if (LHS.countLeadingZeros() < SA) |
2793 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
2794 | } |
2795 | Result = LHS << SA; |
2796 | return true; |
2797 | } |
2798 | case BO_Shr: { |
2799 | if (Info.getLangOpts().OpenCL) |
2800 | |
2801 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2802 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2803 | RHS.isUnsigned()); |
2804 | else if (RHS.isSigned() && RHS.isNegative()) { |
2805 | |
2806 | |
2807 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2808 | RHS = -RHS; |
2809 | goto shift_left; |
2810 | } |
2811 | shift_right: |
2812 | |
2813 | |
2814 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2815 | if (SA != RHS) |
2816 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2817 | << RHS << E->getType() << LHS.getBitWidth(); |
2818 | Result = LHS >> SA; |
2819 | return true; |
2820 | } |
2821 | |
2822 | case BO_LT: Result = LHS < RHS; return true; |
2823 | case BO_GT: Result = LHS > RHS; return true; |
2824 | case BO_LE: Result = LHS <= RHS; return true; |
2825 | case BO_GE: Result = LHS >= RHS; return true; |
2826 | case BO_EQ: Result = LHS == RHS; return true; |
2827 | case BO_NE: Result = LHS != RHS; return true; |
2828 | case BO_Cmp: |
2829 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
2830 | } |
2831 | } |
2832 | |
2833 | |
2834 | static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E, |
2835 | APFloat &LHS, BinaryOperatorKind Opcode, |
2836 | const APFloat &RHS) { |
2837 | bool DynamicRM; |
2838 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E, DynamicRM); |
2839 | APFloat::opStatus St; |
2840 | switch (Opcode) { |
2841 | default: |
2842 | Info.FFDiag(E); |
2843 | return false; |
2844 | case BO_Mul: |
2845 | St = LHS.multiply(RHS, RM); |
2846 | break; |
2847 | case BO_Add: |
2848 | St = LHS.add(RHS, RM); |
2849 | break; |
2850 | case BO_Sub: |
2851 | St = LHS.subtract(RHS, RM); |
2852 | break; |
2853 | case BO_Div: |
2854 | |
2855 | |
2856 | if (RHS.isZero()) |
2857 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
2858 | St = LHS.divide(RHS, RM); |
2859 | break; |
2860 | } |
2861 | |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | if (LHS.isNaN()) { |
2867 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
2868 | return Info.noteUndefinedBehavior(); |
2869 | } |
2870 | |
2871 | return checkFloatingPointResult(Info, E, St); |
2872 | } |
2873 | |
2874 | static bool handleLogicalOpForVector(const APInt &LHSValue, |
2875 | BinaryOperatorKind Opcode, |
2876 | const APInt &RHSValue, APInt &Result) { |
2877 | bool LHS = (LHSValue != 0); |
2878 | bool RHS = (RHSValue != 0); |
2879 | |
2880 | if (Opcode == BO_LAnd) |
2881 | Result = LHS && RHS; |
2882 | else |
2883 | Result = LHS || RHS; |
2884 | return true; |
2885 | } |
2886 | static bool handleLogicalOpForVector(const APFloat &LHSValue, |
2887 | BinaryOperatorKind Opcode, |
2888 | const APFloat &RHSValue, APInt &Result) { |
2889 | bool LHS = !LHSValue.isZero(); |
2890 | bool RHS = !RHSValue.isZero(); |
2891 | |
2892 | if (Opcode == BO_LAnd) |
2893 | Result = LHS && RHS; |
2894 | else |
2895 | Result = LHS || RHS; |
2896 | return true; |
2897 | } |
2898 | |
2899 | static bool handleLogicalOpForVector(const APValue &LHSValue, |
2900 | BinaryOperatorKind Opcode, |
2901 | const APValue &RHSValue, APInt &Result) { |
2902 | |
2903 | if (LHSValue.getKind() == APValue::Int) |
2904 | return handleLogicalOpForVector(LHSValue.getInt(), Opcode, |
2905 | RHSValue.getInt(), Result); |
2906 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
2907 | return handleLogicalOpForVector(LHSValue.getFloat(), Opcode, |
2908 | RHSValue.getFloat(), Result); |
2909 | } |
2910 | |
2911 | template <typename APTy> |
2912 | static bool |
2913 | handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode, |
2914 | const APTy &RHSValue, APInt &Result) { |
2915 | switch (Opcode) { |
2916 | default: |
2917 | llvm_unreachable("unsupported binary operator"); |
2918 | case BO_EQ: |
2919 | Result = (LHSValue == RHSValue); |
2920 | break; |
2921 | case BO_NE: |
2922 | Result = (LHSValue != RHSValue); |
2923 | break; |
2924 | case BO_LT: |
2925 | Result = (LHSValue < RHSValue); |
2926 | break; |
2927 | case BO_GT: |
2928 | Result = (LHSValue > RHSValue); |
2929 | break; |
2930 | case BO_LE: |
2931 | Result = (LHSValue <= RHSValue); |
2932 | break; |
2933 | case BO_GE: |
2934 | Result = (LHSValue >= RHSValue); |
2935 | break; |
2936 | } |
2937 | |
2938 | return true; |
2939 | } |
2940 | |
2941 | static bool handleCompareOpForVector(const APValue &LHSValue, |
2942 | BinaryOperatorKind Opcode, |
2943 | const APValue &RHSValue, APInt &Result) { |
2944 | |
2945 | if (LHSValue.getKind() == APValue::Int) |
2946 | return handleCompareOpForVectorHelper(LHSValue.getInt(), Opcode, |
2947 | RHSValue.getInt(), Result); |
2948 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options"); |
2949 | return handleCompareOpForVectorHelper(LHSValue.getFloat(), Opcode, |
2950 | RHSValue.getFloat(), Result); |
2951 | } |
2952 | |
2953 | |
2954 | static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E, |
2955 | BinaryOperatorKind Opcode, |
2956 | APValue &LHSValue, |
2957 | const APValue &RHSValue) { |
2958 | assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI && |
2959 | "Operation not supported on vector types"); |
2960 | |
2961 | const auto *VT = E->getType()->castAs<VectorType>(); |
2962 | unsigned NumElements = VT->getNumElements(); |
2963 | QualType EltTy = VT->getElementType(); |
2964 | |
2965 | |
2966 | |
2967 | |
2968 | if (!LHSValue.isVector()) { |
2969 | assert(LHSValue.isLValue() && |
2970 | "A vector result that isn't a vector OR uncalculated LValue"); |
2971 | Info.FFDiag(E); |
2972 | return false; |
2973 | } |
2974 | |
2975 | assert(LHSValue.getVectorLength() == NumElements && |
2976 | RHSValue.getVectorLength() == NumElements && "Different vector sizes"); |
2977 | |
2978 | SmallVector<APValue, 4> ResultElements; |
2979 | |
2980 | for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) { |
2981 | APValue LHSElt = LHSValue.getVectorElt(EltNum); |
2982 | APValue RHSElt = RHSValue.getVectorElt(EltNum); |
2983 | |
2984 | if (EltTy->isIntegerType()) { |
2985 | APSInt EltResult{Info.Ctx.getIntWidth(EltTy), |
2986 | EltTy->isUnsignedIntegerType()}; |
2987 | bool Success = true; |
2988 | |
2989 | if (BinaryOperator::isLogicalOp(Opcode)) |
2990 | Success = handleLogicalOpForVector(LHSElt, Opcode, RHSElt, EltResult); |
2991 | else if (BinaryOperator::isComparisonOp(Opcode)) |
2992 | Success = handleCompareOpForVector(LHSElt, Opcode, RHSElt, EltResult); |
2993 | else |
2994 | Success = handleIntIntBinOp(Info, E, LHSElt.getInt(), Opcode, |
2995 | RHSElt.getInt(), EltResult); |
2996 | |
2997 | if (!Success) { |
2998 | Info.FFDiag(E); |
2999 | return false; |
3000 | } |
3001 | ResultElements.emplace_back(EltResult); |
3002 | |
3003 | } else if (EltTy->isFloatingType()) { |
3004 | assert(LHSElt.getKind() == APValue::Float && |
3005 | RHSElt.getKind() == APValue::Float && |
3006 | "Mismatched LHS/RHS/Result Type"); |
3007 | APFloat LHSFloat = LHSElt.getFloat(); |
3008 | |
3009 | if (!handleFloatFloatBinOp(Info, E, LHSFloat, Opcode, |
3010 | RHSElt.getFloat())) { |
3011 | Info.FFDiag(E); |
3012 | return false; |
3013 | } |
3014 | |
3015 | ResultElements.emplace_back(LHSFloat); |
3016 | } |
3017 | } |
3018 | |
3019 | LHSValue = APValue(ResultElements.data(), ResultElements.size()); |
3020 | return true; |
3021 | } |
3022 | |
3023 | |
3024 | |
3025 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3026 | const RecordDecl *TruncatedType, |
3027 | unsigned TruncatedElements) { |
3028 | SubobjectDesignator &D = Result.Designator; |
3029 | |
3030 | |
3031 | if (TruncatedElements == D.Entries.size()) |
3032 | return true; |
3033 | assert(TruncatedElements >= D.MostDerivedPathLength && |
3034 | "not casting to a derived class"); |
3035 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
3036 | return false; |
3037 | |
3038 | |
3039 | const RecordDecl *RD = TruncatedType; |
3040 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
3041 | if (RD->isInvalidDecl()) return false; |
3042 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
3043 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
3044 | if (isVirtualBaseClass(D.Entries[I])) |
3045 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
3046 | else |
3047 | Result.Offset -= Layout.getBaseClassOffset(Base); |
3048 | RD = Base; |
3049 | } |
3050 | D.Entries.resize(TruncatedElements); |
3051 | return true; |
3052 | } |
3053 | |
3054 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3055 | const CXXRecordDecl *Derived, |
3056 | const CXXRecordDecl *Base, |
3057 | const ASTRecordLayout *RL = nullptr) { |
3058 | if (!RL) { |
3059 | if (Derived->isInvalidDecl()) return false; |
3060 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
3061 | } |
3062 | |
3063 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
3064 | Obj.addDecl(Info, E, Base, false); |
3065 | return true; |
3066 | } |
3067 | |
3068 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3069 | const CXXRecordDecl *DerivedDecl, |
3070 | const CXXBaseSpecifier *Base) { |
3071 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
3072 | |
3073 | if (!Base->isVirtual()) |
3074 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
3075 | |
3076 | SubobjectDesignator &D = Obj.Designator; |
3077 | if (D.Invalid) |
3078 | return false; |
3079 | |
3080 | |
3081 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
3082 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
3083 | return false; |
3084 | |
3085 | |
3086 | if (DerivedDecl->isInvalidDecl()) return false; |
3087 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
3088 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
3089 | Obj.addDecl(Info, E, BaseDecl, true); |
3090 | return true; |
3091 | } |
3092 | |
3093 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
3094 | QualType Type, LValue &Result) { |
3095 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
3096 | PathE = E->path_end(); |
3097 | PathI != PathE; ++PathI) { |
3098 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
3099 | *PathI)) |
3100 | return false; |
3101 | Type = (*PathI)->getType(); |
3102 | } |
3103 | return true; |
3104 | } |
3105 | |
3106 | |
3107 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3108 | const CXXRecordDecl *DerivedRD, |
3109 | const CXXRecordDecl *BaseRD) { |
3110 | CXXBasePaths Paths(false, |
3111 | true, false); |
3112 | if (!DerivedRD->isDerivedFrom(BaseRD, Paths)) |
3113 | llvm_unreachable("Class must be derived from the passed in base class!"); |
3114 | |
3115 | for (CXXBasePathElement &Elem : Paths.front()) |
3116 | if (!HandleLValueBase(Info, E, Result, Elem.Class, Elem.Base)) |
3117 | return false; |
3118 | return true; |
3119 | } |
3120 | |
3121 | |
3122 | |
3123 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
3124 | const FieldDecl *FD, |
3125 | const ASTRecordLayout *RL = nullptr) { |
3126 | if (!RL) { |
3127 | if (FD->getParent()->isInvalidDecl()) return false; |
3128 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
3129 | } |
3130 | |
3131 | unsigned I = FD->getFieldIndex(); |
3132 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
3133 | LVal.addDecl(Info, E, FD); |
3134 | return true; |
3135 | } |
3136 | |
3137 | |
3138 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
3139 | LValue &LVal, |
3140 | const IndirectFieldDecl *IFD) { |
3141 | for (const auto *C : IFD->chain()) |
3142 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
3143 | return false; |
3144 | return true; |
3145 | } |
3146 | |
3147 | |
3148 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
3149 | QualType Type, CharUnits &Size) { |
3150 | |
3151 | |
3152 | if (Type->isVoidType() || Type->isFunctionType()) { |
3153 | Size = CharUnits::One(); |
3154 | return true; |
3155 | } |
3156 | |
3157 | if (Type->isDependentType()) { |
3158 | Info.FFDiag(Loc); |
3159 | return false; |
3160 | } |
3161 | |
3162 | if (!Type->isConstantSizeType()) { |
3163 | |
3164 | |
3165 | Info.FFDiag(Loc); |
3166 | return false; |
3167 | } |
3168 | |
3169 | Size = Info.Ctx.getTypeSizeInChars(Type); |
3170 | return true; |
3171 | } |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | |
3178 | |
3179 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3180 | LValue &LVal, QualType EltTy, |
3181 | APSInt Adjustment) { |
3182 | CharUnits SizeOfPointee; |
3183 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
3184 | return false; |
3185 | |
3186 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
3187 | return true; |
3188 | } |
3189 | |
3190 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3191 | LValue &LVal, QualType EltTy, |
3192 | int64_t Adjustment) { |
3193 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
3194 | APSInt::get(Adjustment)); |
3195 | } |
3196 | |
3197 | |
3198 | |
3199 | |
3200 | |
3201 | |
3202 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
3203 | LValue &LVal, QualType EltTy, |
3204 | bool Imag) { |
3205 | if (Imag) { |
3206 | CharUnits SizeOfComponent; |
3207 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
3208 | return false; |
3209 | LVal.Offset += SizeOfComponent; |
3210 | } |
3211 | LVal.addComplex(Info, E, EltTy, Imag); |
3212 | return true; |
3213 | } |
3214 | |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | |
3221 | |
3222 | |
3223 | |
3224 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
3225 | const VarDecl *VD, CallStackFrame *Frame, |
3226 | unsigned Version, APValue *&Result) { |
3227 | APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version); |
3228 | |
3229 | |
3230 | if (Frame) { |
3231 | Result = Frame->getTemporary(VD, Version); |
3232 | if (Result) |
3233 | return true; |
3234 | |
3235 | if (!isa<ParmVarDecl>(VD)) { |
3236 | |
3237 | |
3238 | |
3239 | |
3240 | assert(isLambdaCallOperator(Frame->Callee) && |
3241 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
3242 | "missing value for local variable"); |
3243 | if (Info.checkingPotentialConstantExpression()) |
3244 | return false; |
3245 | |
3246 | |
3247 | Info.FFDiag(E->getBeginLoc(), |
3248 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
3249 | << "captures not currently allowed"; |
3250 | return false; |
3251 | } |
3252 | } |
3253 | |
3254 | |
3255 | |
3256 | if (Info.EvaluatingDecl == Base) { |
3257 | Result = Info.EvaluatingDeclValue; |
3258 | return true; |
3259 | } |
3260 | |
3261 | if (isa<ParmVarDecl>(VD)) { |
3262 | |
3263 | |
3264 | if (!Info.checkingPotentialConstantExpression() || |
3265 | !Info.CurrentCall->Callee || |
3266 | !Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
3267 | if (Info.getLangOpts().CPlusPlus11) { |
3268 | Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown) |
3269 | << VD; |
3270 | NoteLValueLocation(Info, Base); |
3271 | } else { |
3272 | Info.FFDiag(E); |
3273 | } |
3274 | } |
3275 | return false; |
3276 | } |
3277 | |
3278 | |
3279 | |
3280 | |
3281 | const Expr *Init = VD->getAnyInitializer(VD); |
3282 | if (!Init) { |
3283 | |
3284 | |
3285 | if (!Info.checkingPotentialConstantExpression()) { |
3286 | Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) |
3287 | << VD; |
3288 | NoteLValueLocation(Info, Base); |
3289 | } |
3290 | return false; |
3291 | } |
3292 | |
3293 | if (Init->isValueDependent()) { |
3294 | |
3295 | |
3296 | |
3297 | |
3298 | |
3299 | assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx)); |
3300 | if (!Info.checkingPotentialConstantExpression()) { |
3301 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
3302 | ? diag::note_constexpr_ltor_non_constexpr |
3303 | : diag::note_constexpr_ltor_non_integral, 1) |
3304 | << VD << VD->getType(); |
3305 | NoteLValueLocation(Info, Base); |
3306 | } |
3307 | return false; |
3308 | } |
3309 | |
3310 | |
3311 | |
3312 | if (!VD->evaluateValue()) { |
3313 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3314 | NoteLValueLocation(Info, Base); |
3315 | return false; |
3316 | } |
3317 | |
3318 | |
3319 | |
3320 | |
3321 | |
3322 | |
3323 | |
3324 | |
3325 | |
3326 | |
3327 | if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() && |
3328 | VD->mightBeUsableInConstantExpressions(Info.Ctx)) || |
3329 | ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) && |
3330 | !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Info.Ctx))) { |
3331 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3332 | NoteLValueLocation(Info, Base); |
3333 | } |
3334 | |
3335 | |
3336 | |
3337 | if (VD->isWeak()) { |
3338 | Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD; |
3339 | NoteLValueLocation(Info, Base); |
3340 | return false; |
3341 | } |
3342 | |
3343 | Result = VD->getEvaluatedValue(); |
3344 | return true; |
3345 | } |
3346 | |
3347 | |
3348 | |
3349 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
3350 | const CXXRecordDecl *Base) { |
3351 | Base = Base->getCanonicalDecl(); |
3352 | unsigned Index = 0; |
3353 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
3354 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
3355 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
3356 | return Index; |
3357 | } |
3358 | |
3359 | llvm_unreachable("base class missing from derived class's bases list"); |
3360 | } |
3361 | |
3362 | |
3363 | static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit, |
3364 | uint64_t Index) { |
3365 | assert(!isa<SourceLocExpr>(Lit) && |
3366 | "SourceLocExpr should have already been converted to a StringLiteral"); |
3367 | |
3368 | |
3369 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
3370 | std::string Str; |
3371 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
3372 | assert(Index <= Str.size() && "Index too large"); |
3373 | return APSInt::getUnsigned(Str.c_str()[Index]); |
3374 | } |
3375 | |
3376 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
3377 | Lit = PE->getFunctionName(); |
3378 | const StringLiteral *S = cast<StringLiteral>(Lit); |
3379 | const ConstantArrayType *CAT = |
3380 | Info.Ctx.getAsConstantArrayType(S->getType()); |
3381 | assert(CAT && "string literal isn't an array"); |
3382 | QualType CharType = CAT->getElementType(); |
3383 | assert(CharType->isIntegerType() && "unexpected character type"); |
3384 | |
3385 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
3386 | CharType->isUnsignedIntegerType()); |
3387 | if (Index < S->getLength()) |
3388 | Value = S->getCodeUnit(Index); |
3389 | return Value; |
3390 | } |
3391 | |
3392 | |
3393 | |
3394 | |
3395 | |
3396 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
3397 | APValue &Result, |
3398 | QualType AllocType = QualType()) { |
3399 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
3400 | AllocType.isNull() ? S->getType() : AllocType); |
3401 | assert(CAT && "string literal isn't an array"); |
3402 | QualType CharType = CAT->getElementType(); |
3403 | assert(CharType->isIntegerType() && "unexpected character type"); |
3404 | |
3405 | unsigned Elts = CAT->getSize().getZExtValue(); |
3406 | Result = APValue(APValue::UninitArray(), |
3407 | std::min(S->getLength(), Elts), Elts); |
3408 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
3409 | CharType->isUnsignedIntegerType()); |
3410 | if (Result.hasArrayFiller()) |
3411 | Result.getArrayFiller() = APValue(Value); |
3412 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
3413 | Value = S->getCodeUnit(I); |
3414 | Result.getArrayInitializedElt(I) = APValue(Value); |
3415 | } |
3416 | } |
3417 | |
3418 | |
3419 | static void expandArray(APValue &Array, unsigned Index) { |
3420 | unsigned Size = Array.getArraySize(); |
3421 | assert(Index < Size); |
3422 | |
3423 | |
3424 | unsigned OldElts = Array.getArrayInitializedElts(); |
3425 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
3426 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
3427 | |
3428 | |
3429 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
3430 | for (unsigned I = 0; I != OldElts; ++I) |
3431 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
3432 | for (unsigned I = OldElts; I != NewElts; ++I) |
3433 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
3434 | if (NewValue.hasArrayFiller()) |
3435 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
3436 | Array.swap(NewValue); |
3437 | } |
3438 | |
3439 | |
3440 | |
3441 | |
3442 | |
3443 | |
3444 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD); |
3445 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
3446 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3447 | return !RD || isReadByLvalueToRvalueConversion(RD); |
3448 | } |
3449 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) { |
3450 | |
3451 | |
3452 | if (RD->isUnion()) |
3453 | return !RD->field_empty(); |
3454 | if (RD->isEmpty()) |
3455 | return false; |
3456 | |
3457 | for (auto *Field : RD->fields()) |
3458 | if (!Field->isUnnamedBitfield() && |
3459 | isReadByLvalueToRvalueConversion(Field->getType())) |
3460 | return true; |
3461 | |
3462 | for (auto &BaseSpec : RD->bases()) |
3463 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
3464 | return true; |
3465 | |
3466 | return false; |
3467 | } |
3468 | |
3469 | |
3470 | |
3471 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
3472 | QualType T) { |
3473 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3474 | if (!RD) |
3475 | return false; |
3476 | |
3477 | if (!RD->hasMutableFields()) |
3478 | return false; |
3479 | |
3480 | for (auto *Field : RD->fields()) { |
3481 | |
3482 | |
3483 | |
3484 | |
3485 | if (Field->isMutable() && |
3486 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
3487 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
3488 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3489 | return true; |
3490 | } |
3491 | |
3492 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
3493 | return true; |
3494 | } |
3495 | |
3496 | for (auto &BaseSpec : RD->bases()) |
3497 | if (diagnoseMutableFields(Info, E, AK, BaseSpec.getType())) |
3498 | return true; |
3499 | |
3500 | |
3501 | return false; |
3502 | } |
3503 | |
3504 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
3505 | APValue::LValueBase Base, |
3506 | bool MutableSubobject = false) { |
3507 | |
3508 | if (Base.getCallIndex() || Base.is<DynamicAllocLValue>()) |
3509 | return true; |
3510 | |
3511 | switch (Info.IsEvaluatingDecl) { |
3512 | case EvalInfo::EvaluatingDeclKind::None: |
3513 | return false; |
3514 | |
3515 | case EvalInfo::EvaluatingDeclKind::Ctor: |
3516 | |
3517 | if (Info.EvaluatingDecl == Base) |
3518 | return true; |
3519 | |
3520 | |
3521 | |
3522 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
3523 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(BaseE)) |
3524 | return Info.EvaluatingDecl == BaseMTE->getExtendingDecl(); |
3525 | return false; |
3526 | |
3527 | case EvalInfo::EvaluatingDeclKind::Dtor: |
3528 | |
3529 | |
3530 | |
3531 | |
3532 | if (MutableSubobject || Base != Info.EvaluatingDecl) |
3533 | return false; |
3534 | |
3535 | |
3536 | |
3537 | QualType T = getType(Base); |
3538 | return T.isConstQualified() || T->isReferenceType(); |
3539 | } |
3540 | |
3541 | llvm_unreachable("unknown evaluating decl kind"); |
3542 | } |
3543 | |
3544 | namespace { |
3545 | |
3546 | |
3547 | struct CompleteObject { |
3548 | |
3549 | APValue::LValueBase Base; |
3550 | |
3551 | APValue *Value; |
3552 | |
3553 | QualType Type; |
3554 | |
3555 | CompleteObject() : Value(nullptr) {} |
3556 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
3557 | : Base(Base), Value(Value), Type(Type) {} |
3558 | |
3559 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | if (!isAnyAccess(AK)) |
3565 | return true; |
3566 | |
3567 | |
3568 | |
3569 | |
3570 | if (!Info.getLangOpts().CPlusPlus14) |
3571 | return false; |
3572 | return lifetimeStartedInEvaluation(Info, Base, true); |
3573 | } |
3574 | |
3575 | explicit operator bool() const { return !Type.isNull(); } |
3576 | }; |
3577 | } |
3578 | |
3579 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
3580 | bool IsMutable = false) { |
3581 | |
3582 | |
3583 | |
3584 | if (ObjType.isConstQualified() && !IsMutable) |
3585 | SubobjType.addConst(); |
3586 | |
3587 | |
3588 | if (ObjType.isVolatileQualified()) |
3589 | SubobjType.addVolatile(); |
3590 | return SubobjType; |
3591 | } |
3592 | |
3593 | |
3594 | template<typename SubobjectHandler> |
3595 | typename SubobjectHandler::result_type |
3596 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
3597 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
3598 | if (Sub.Invalid) |
3599 | |
3600 | return handler.failed(); |
3601 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
3602 | if (Info.getLangOpts().CPlusPlus11) |
3603 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
3604 | ? diag::note_constexpr_access_past_end |
3605 | : diag::note_constexpr_access_unsized_array) |
3606 | << handler.AccessKind; |
3607 | else |
3608 | Info.FFDiag(E); |
3609 | return handler.failed(); |
3610 | } |
3611 | |
3612 | APValue *O = Obj.Value; |
3613 | QualType ObjType = Obj.Type; |
3614 | const FieldDecl *LastField = nullptr; |
3615 | const FieldDecl *VolatileField = nullptr; |
3616 | |
3617 | |
3618 | for (unsigned I = 0, N = Sub.Entries.size(); ; ++I) { |
3619 | |
3620 | if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) || |
3621 | (O->isIndeterminate() && |
3622 | !isValidIndeterminateAccess(handler.AccessKind))) { |
3623 | if (!Info.checkingPotentialConstantExpression()) |
3624 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
3625 | << handler.AccessKind << O->isIndeterminate(); |
3626 | return handler.failed(); |
3627 | } |
3628 | |
3629 | |
3630 | |
3631 | |
3632 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
3633 | ObjType->isRecordType() && |
3634 | Info.isEvaluatingCtorDtor( |
3635 | Obj.Base, llvm::makeArrayRef(Sub.Entries.begin(), |
3636 | Sub.Entries.begin() + I)) != |
3637 | ConstructionPhase::None) { |
3638 | ObjType = Info.Ctx.getCanonicalType(ObjType); |
3639 | ObjType.removeLocalConst(); |
3640 | ObjType.removeLocalVolatile(); |
3641 | } |
3642 | |
3643 | |
3644 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
3645 | |
3646 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
3647 | if (Info.getLangOpts().CPlusPlus) { |
3648 | int DiagKind; |
3649 | SourceLocation Loc; |
3650 | const NamedDecl *Decl = nullptr; |
3651 | if (VolatileField) { |
3652 | DiagKind = 2; |
3653 | Loc = VolatileField->getLocation(); |
3654 | Decl = VolatileField; |
3655 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
3656 | DiagKind = 1; |
3657 | Loc = VD->getLocation(); |
3658 | Decl = VD; |
3659 | } else { |
3660 | DiagKind = 0; |
3661 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
3662 | Loc = E->getExprLoc(); |
3663 | } |
3664 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3665 | << handler.AccessKind << DiagKind << Decl; |
3666 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
3667 | } else { |
3668 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
3669 | } |
3670 | return handler.failed(); |
3671 | } |
3672 | |
3673 | |
3674 | |
3675 | |
3676 | |
3677 | if (ObjType->isRecordType() && |
3678 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind) && |
3679 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
3680 | return handler.failed(); |
3681 | } |
3682 | |
3683 | if (I == N) { |
3684 | if (!handler.found(*O, ObjType)) |
3685 | return false; |
3686 | |
3687 | |
3688 | if (isModification(handler.AccessKind) && |
3689 | LastField && LastField->isBitField() && |
3690 | !truncateBitfieldValue(Info, E, *O, LastField)) |
3691 | return false; |
3692 | |
3693 | return true; |
3694 | } |
3695 | |
3696 | LastField = nullptr; |
3697 | if (ObjType->isArrayType()) { |
3698 | |
3699 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
3700 | assert(CAT && "vla in literal type?"); |
3701 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3702 | if (CAT->getSize().ule(Index)) { |
3703 | |
3704 | |
3705 | if (Info.getLangOpts().CPlusPlus11) |
3706 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3707 | << handler.AccessKind; |
3708 | else |
3709 | Info.FFDiag(E); |
3710 | return handler.failed(); |
3711 | } |
3712 | |
3713 | ObjType = CAT->getElementType(); |
3714 | |
3715 | if (O->getArrayInitializedElts() > Index) |
3716 | O = &O->getArrayInitializedElt(Index); |
3717 | else if (!isRead(handler.AccessKind)) { |
3718 | expandArray(*O, Index); |
3719 | O = &O->getArrayInitializedElt(Index); |
3720 | } else |
3721 | O = &O->getArrayFiller(); |
3722 | } else if (ObjType->isAnyComplexType()) { |
3723 | |
3724 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3725 | if (Index > 1) { |
3726 | if (Info.getLangOpts().CPlusPlus11) |
3727 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3728 | << handler.AccessKind; |
3729 | else |
3730 | Info.FFDiag(E); |
3731 | return handler.failed(); |
3732 | } |
3733 | |
3734 | ObjType = getSubobjectType( |
3735 | ObjType, ObjType->castAs<ComplexType>()->getElementType()); |
3736 | |
3737 | assert(I == N - 1 && "extracting subobject of scalar?"); |
3738 | if (O->isComplexInt()) { |
3739 | return handler.found(Index ? O->getComplexIntImag() |
3740 | : O->getComplexIntReal(), ObjType); |
3741 | } else { |
3742 | assert(O->isComplexFloat()); |
3743 | return handler.found(Index ? O->getComplexFloatImag() |
3744 | : O->getComplexFloatReal(), ObjType); |
3745 | } |
3746 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
3747 | if (Field->isMutable() && |
3748 | !Obj.mayAccessMutableMembers(Info, handler.AccessKind)) { |
3749 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
3750 | << handler.AccessKind << Field; |
3751 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3752 | return handler.failed(); |
3753 | } |
3754 | |
3755 | |
3756 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
3757 | if (RD->isUnion()) { |
3758 | const FieldDecl *UnionField = O->getUnionField(); |
3759 | if (!UnionField || |
3760 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
3761 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
3762 | |
3763 | O->setUnion(Field, APValue()); |
3764 | } else { |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
3770 | << handler.AccessKind << Field << !UnionField << UnionField; |
3771 | return handler.failed(); |
3772 | } |
3773 | } |
3774 | O = &O->getUnionValue(); |
3775 | } else |
3776 | O = &O->getStructField(Field->getFieldIndex()); |
3777 | |
3778 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
3779 | LastField = Field; |
3780 | if (Field->getType().isVolatileQualified()) |
3781 | VolatileField = Field; |
3782 | } else { |
3783 | |
3784 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
3785 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
3786 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
3787 | |
3788 | ObjType = getSubobjectType(ObjType, Info.Ctx.getRecordType(Base)); |
3789 | } |
3790 | } |
3791 | } |
3792 | |
3793 | namespace { |
3794 | struct ExtractSubobjectHandler { |
3795 | EvalInfo &Info; |
3796 | const Expr *E; |
3797 | APValue &Result; |
3798 | const AccessKinds AccessKind; |
3799 | |
3800 | typedef bool result_type; |
3801 | bool failed() { return false; } |
3802 | bool found(APValue &Subobj, QualType SubobjType) { |
3803 | Result = Subobj; |
3804 | if (AccessKind == AK_ReadObjectRepresentation) |
3805 | return true; |
3806 | return CheckFullyInitialized(Info, E->getExprLoc(), SubobjType, Result); |
3807 | } |
3808 | bool found(APSInt &Value, QualType SubobjType) { |
3809 | Result = APValue(Value); |
3810 | return true; |
3811 | } |
3812 | bool found(APFloat &Value, QualType SubobjType) { |
3813 | Result = APValue(Value); |
3814 | return true; |
3815 | } |
3816 | }; |
3817 | } |
3818 | |
3819 | |
3820 | static bool extractSubobject(EvalInfo &Info, const Expr *E, |
3821 | const CompleteObject &Obj, |
3822 | const SubobjectDesignator &Sub, APValue &Result, |
3823 | AccessKinds AK = AK_Read) { |
3824 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
3825 | ExtractSubobjectHandler Handler = {Info, E, Result, AK}; |
3826 | return findSubobject(Info, E, Obj, Sub, Handler); |
3827 | } |
3828 | |
3829 | namespace { |
3830 | struct ModifySubobjectHandler { |
3831 | EvalInfo &Info; |
3832 | APValue &NewVal; |
3833 | const Expr *E; |
3834 | |
3835 | typedef bool result_type; |
3836 | static const AccessKinds AccessKind = AK_Assign; |
3837 | |
3838 | bool checkConst(QualType QT) { |
3839 | |
3840 | if (QT.isConstQualified()) { |
3841 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3842 | return false; |
3843 | } |
3844 | return true; |
3845 | } |
3846 | |
3847 | bool failed() { return false; } |
3848 | bool found(APValue &Subobj, QualType SubobjType) { |
3849 | if (!checkConst(SubobjType)) |
3850 | return false; |
3851 | |
3852 | Subobj.swap(NewVal); |
3853 | return true; |
3854 | } |
3855 | bool found(APSInt &Value, QualType SubobjType) { |
3856 | if (!checkConst(SubobjType)) |
3857 | return false; |
3858 | if (!NewVal.isInt()) { |
3859 | |
3860 | Info.FFDiag(E); |
3861 | return false; |
3862 | } |
3863 | Value = NewVal.getInt(); |
3864 | return true; |
3865 | } |
3866 | bool found(APFloat &Value, QualType SubobjType) { |
3867 | if (!checkConst(SubobjType)) |
3868 | return false; |
3869 | Value = NewVal.getFloat(); |
3870 | return true; |
3871 | } |
3872 | }; |
3873 | } |
3874 | |
3875 | const AccessKinds ModifySubobjectHandler::AccessKind; |
3876 | |
3877 | |
3878 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
3879 | const CompleteObject &Obj, |
3880 | const SubobjectDesignator &Sub, |
3881 | APValue &NewVal) { |
3882 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
3883 | return findSubobject(Info, E, Obj, Sub, Handler); |
3884 | } |
3885 | |
3886 | |
3887 | |
3888 | static unsigned FindDesignatorMismatch(QualType ObjType, |
3889 | const SubobjectDesignator &A, |
3890 | const SubobjectDesignator &B, |
3891 | bool &WasArrayIndex) { |
3892 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
3893 | for (; I != N; ++I) { |
3894 | if (!ObjType.isNull() && |
3895 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
3896 | |
3897 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
3898 | WasArrayIndex = true; |
3899 | return I; |
3900 | } |
3901 | if (ObjType->isAnyComplexType()) |
3902 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
3903 | else |
3904 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
3905 | } else { |
3906 | if (A.Entries[I].getAsBaseOrMember() != |
3907 | B.Entries[I].getAsBaseOrMember()) { |
3908 | WasArrayIndex = false; |
3909 | return I; |
3910 | } |
3911 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
3912 | |
3913 | ObjType = FD->getType(); |
3914 | else |
3915 | |
3916 | ObjType = QualType(); |
3917 | } |
3918 | } |
3919 | WasArrayIndex = false; |
3920 | return I; |
3921 | } |
3922 | |
3923 | |
3924 | |
3925 | static bool AreElementsOfSameArray(QualType ObjType, |
3926 | const SubobjectDesignator &A, |
3927 | const SubobjectDesignator &B) { |
3928 | if (A.Entries.size() != B.Entries.size()) |
3929 | return false; |
3930 | |
3931 | bool IsArray = A.MostDerivedIsArrayElement; |
3932 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
3933 | |
3934 | return false; |
3935 | |
3936 | |
3937 | |
3938 | |
3939 | bool WasArrayIndex; |
3940 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
3941 | return CommonLength >= A.Entries.size() - IsArray; |
3942 | } |
3943 | |
3944 | |
3945 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
3946 | AccessKinds AK, const LValue &LVal, |
3947 | QualType LValType) { |
3948 | if (LVal.InvalidBase) { |
3949 | Info.FFDiag(E); |
3950 | return CompleteObject(); |
3951 | } |
3952 | |
3953 | if (!LVal.Base) { |
3954 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
3955 | return CompleteObject(); |
3956 | } |
3957 | |
3958 | CallStackFrame *Frame = nullptr; |
3959 | unsigned Depth = 0; |
3960 | if (LVal.getLValueCallIndex()) { |
3961 | std::tie(Frame, Depth) = |
3962 | Info.getCallFrameAndDepth(LVal.getLValueCallIndex()); |
3963 | if (!Frame) { |
3964 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
3965 | << AK << LVal.Base.is<const ValueDecl*>(); |
3966 | NoteLValueLocation(Info, LVal.Base); |
3967 | return CompleteObject(); |
3968 | } |
3969 | } |
3970 | |
3971 | bool IsAccess = isAnyAccess(AK); |
3972 | |
3973 | |
3974 | |
3975 | |
3976 | |
3977 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
3978 | if (Info.getLangOpts().CPlusPlus) |
3979 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
3980 | << AK << LValType; |
3981 | else |
3982 | Info.FFDiag(E); |
3983 | return CompleteObject(); |
3984 | } |
3985 | |
3986 | |
3987 | APValue *BaseVal = nullptr; |
3988 | QualType BaseType = getType(LVal.Base); |
3989 | |
3990 | if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl && |
3991 | lifetimeStartedInEvaluation(Info, LVal.Base)) { |
3992 | |
3993 | |
3994 | BaseVal = Info.EvaluatingDeclValue; |
3995 | } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) { |
3996 | |
3997 | if (auto *GD = dyn_cast<MSGuidDecl>(D)) { |
3998 | if (isModification(AK)) { |
3999 | |
4000 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4001 | return CompleteObject(); |
4002 | } |
4003 | APValue &V = GD->getAsAPValue(); |
4004 | if (V.isAbsent()) { |
4005 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
4006 | << GD->getType(); |
4007 | return CompleteObject(); |
4008 | } |
4009 | return CompleteObject(LVal.Base, &V, GD->getType()); |
4010 | } |
4011 | |
4012 | |
4013 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(D)) { |
4014 | if (isModification(AK)) { |
4015 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4016 | return CompleteObject(); |
4017 | } |
4018 | return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()), |
4019 | TPO->getType()); |
4020 | } |
4021 | |
4022 | |
4023 | |
4024 | |
4025 | |
4026 | |
4027 | |
4028 | |
4029 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
4030 | if (VD) { |
4031 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
4032 | VD = VDef; |
4033 | } |
4034 | if (!VD || VD->isInvalidDecl()) { |
4035 | Info.FFDiag(E); |
4036 | return CompleteObject(); |
4037 | } |
4038 | |
4039 | bool IsConstant = BaseType.isConstant(Info.Ctx); |
4040 | |
4041 | |
4042 | |
4043 | if (!Frame) { |
4044 | if (IsAccess && isa<ParmVarDecl>(VD)) { |
4045 | |
4046 | |
4047 | |
4048 | } else if (Info.getLangOpts().CPlusPlus14 && |
4049 | lifetimeStartedInEvaluation(Info, LVal.Base)) { |
4050 | |
4051 | |
4052 | |
4053 | } else if (isModification(AK)) { |
4054 | |
4055 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4056 | return CompleteObject(); |
4057 | } else if (VD->isConstexpr()) { |
4058 | |
4059 | } else if (BaseType->isIntegralOrEnumerationType()) { |
4060 | if (!IsConstant) { |
4061 | if (!IsAccess) |
4062 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4063 | if (Info.getLangOpts().CPlusPlus) { |
4064 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
4065 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4066 | } else { |
4067 | Info.FFDiag(E); |
4068 | } |
4069 | return CompleteObject(); |
4070 | } |
4071 | } else if (!IsAccess) { |
4072 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4073 | } else if (IsConstant && Info.checkingPotentialConstantExpression() && |
4074 | BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) { |
4075 | |
4076 | } else if (IsConstant) { |
4077 | |
4078 | |
4079 | |
4080 | if (Info.getLangOpts().CPlusPlus) { |
4081 | Info.CCEDiag(E, Info.getLangOpts().CPlusPlus11 |
4082 | ? diag::note_constexpr_ltor_non_constexpr |
4083 | : diag::note_constexpr_ltor_non_integral, 1) |
4084 | << VD << BaseType; |
4085 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4086 | } else { |
4087 | Info.CCEDiag(E); |
4088 | } |
4089 | } else { |
4090 | |
4091 | if (Info.getLangOpts().CPlusPlus) { |
4092 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
4093 | ? diag::note_constexpr_ltor_non_constexpr |
4094 | : diag::note_constexpr_ltor_non_integral, 1) |
4095 | << VD << BaseType; |
4096 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4097 | } else { |
4098 | Info.FFDiag(E); |
4099 | } |
4100 | return CompleteObject(); |
4101 | } |
4102 | } |
4103 | |
4104 | if (!evaluateVarDeclInit(Info, E, VD, Frame, LVal.getLValueVersion(), BaseVal)) |
4105 | return CompleteObject(); |
4106 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
4107 | Optional<DynAlloc*> Alloc = Info.lookupDynamicAlloc(DA); |
4108 | if (!Alloc) { |
4109 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
4110 | return CompleteObject(); |
4111 | } |
4112 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
4113 | LVal.Base.getDynamicAllocType()); |
4114 | } else { |
4115 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4116 | |
4117 | if (!Frame) { |
4118 | if (const MaterializeTemporaryExpr *MTE = |
4119 | dyn_cast_or_null<MaterializeTemporaryExpr>(Base)) { |
4120 | assert(MTE->getStorageDuration() == SD_Static && |
4121 | "should have a frame for a non-global materialized temporary"); |
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 | |
4147 | |
4148 | if (!MTE->isUsableInConstantExpressions(Info.Ctx) && |
4149 | !lifetimeStartedInEvaluation(Info, LVal.Base)) { |
4150 | if (!IsAccess) |
4151 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4152 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
4153 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
4154 | return CompleteObject(); |
4155 | } |
4156 | |
4157 | BaseVal = MTE->getOrCreateValue(false); |
4158 | assert(BaseVal && "got reference to unevaluated temporary"); |
4159 | } else { |
4160 | if (!IsAccess) |
4161 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4162 | APValue Val; |
4163 | LVal.moveInto(Val); |
4164 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
4165 | << AK |
4166 | << Val.getAsString(Info.Ctx, |
4167 | Info.Ctx.getLValueReferenceType(LValType)); |
4168 | NoteLValueLocation(Info, LVal.Base); |
4169 | return CompleteObject(); |
4170 | } |
4171 | } else { |
4172 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
4173 | assert(BaseVal && "missing value for temporary"); |
4174 | } |
4175 | } |
4176 | |
4177 | |
4178 | |
4179 | |
4180 | |
4181 | |
4182 | |
4183 | |
4184 | unsigned VisibleDepth = Depth; |
4185 | if (llvm::isa_and_nonnull<ParmVarDecl>( |
4186 | LVal.Base.dyn_cast<const ValueDecl *>())) |
4187 | ++VisibleDepth; |
4188 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
4189 | Info.EvalStatus.HasSideEffects) || |
4190 | (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth)) |
4191 | return CompleteObject(); |
4192 | |
4193 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
4194 | } |
4195 | |
4196 | |
4197 | |
4198 | |
4199 | |
4200 | |
4201 | |
4202 | |
4203 | |
4204 | |
4205 | |
4206 | |
4207 | |
4208 | |
4209 | |
4210 | static bool |
4211 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
4212 | const LValue &LVal, APValue &RVal, |
4213 | bool WantObjectRepresentation = false) { |
4214 | if (LVal.Designator.Invalid) |
4215 | return false; |
4216 | |
4217 | |
4218 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4219 | |
4220 | AccessKinds AK = |
4221 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
4222 | |
4223 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
4224 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
4225 | |
4226 | |
4227 | |
4228 | if (Type.isVolatileQualified()) { |
4229 | Info.FFDiag(Conv); |
4230 | return false; |
4231 | } |
4232 | APValue Lit; |
4233 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
4234 | return false; |
4235 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
4236 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
4237 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
4238 | |
4239 | |
4240 | assert(LVal.Designator.Entries.size() <= 1 && |
4241 | "Can only read characters from string literals"); |
4242 | if (LVal.Designator.Entries.empty()) { |
4243 | |
4244 | |
4245 | |
4246 | Info.FFDiag(Conv); |
4247 | return false; |
4248 | } |
4249 | if (LVal.Designator.isOnePastTheEnd()) { |
4250 | if (Info.getLangOpts().CPlusPlus11) |
4251 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
4252 | else |
4253 | Info.FFDiag(Conv); |
4254 | return false; |
4255 | } |
4256 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
4257 | RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex)); |
4258 | return true; |
4259 | } |
4260 | } |
4261 | |
4262 | CompleteObject Obj = findCompleteObject(Info, Conv, AK, LVal, Type); |
4263 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
4264 | } |
4265 | |
4266 | |
4267 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4268 | QualType LValType, APValue &Val) { |
4269 | if (LVal.Designator.Invalid) |
4270 | return false; |
4271 | |
4272 | if (!Info.getLangOpts().CPlusPlus14) { |
4273 | Info.FFDiag(E); |
4274 | return false; |
4275 | } |
4276 | |
4277 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4278 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
4279 | } |
4280 | |
4281 | namespace { |
4282 | struct CompoundAssignSubobjectHandler { |
4283 | EvalInfo &Info; |
4284 | const CompoundAssignOperator *E; |
4285 | QualType PromotedLHSType; |
4286 | BinaryOperatorKind Opcode; |
4287 | const APValue &RHS; |
4288 | |
4289 | static const AccessKinds AccessKind = AK_Assign; |
4290 | |
4291 | typedef bool result_type; |
4292 | |
4293 | bool checkConst(QualType QT) { |
4294 | |
4295 | if (QT.isConstQualified()) { |
4296 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4297 | return false; |
4298 | } |
4299 | return true; |
4300 | } |
4301 | |
4302 | bool failed() { return false; } |
4303 | bool found(APValue &Subobj, QualType SubobjType) { |
4304 | switch (Subobj.getKind()) { |
4305 | case APValue::Int: |
4306 | return found(Subobj.getInt(), SubobjType); |
4307 | case APValue::Float: |
4308 | return found(Subobj.getFloat(), SubobjType); |
4309 | case APValue::ComplexInt: |
4310 | case APValue::ComplexFloat: |
4311 | |
4312 | Info.FFDiag(E); |
4313 | return false; |
4314 | case APValue::LValue: |
4315 | return foundPointer(Subobj, SubobjType); |
4316 | case APValue::Vector: |
4317 | return foundVector(Subobj, SubobjType); |
4318 | default: |
4319 | |
4320 | Info.FFDiag(E); |
4321 | return false; |
4322 | } |
4323 | } |
4324 | |
4325 | bool foundVector(APValue &Value, QualType SubobjType) { |
4326 | if (!checkConst(SubobjType)) |
4327 | return false; |
4328 | |
4329 | if (!SubobjType->isVectorType()) { |
4330 | Info.FFDiag(E); |
4331 | return false; |
4332 | } |
4333 | return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS); |
4334 | } |
4335 | |
4336 | bool found(APSInt &Value, QualType SubobjType) { |
4337 | if (!checkConst(SubobjType)) |
4338 | return false; |
4339 | |
4340 | if (!SubobjType->isIntegerType()) { |
4341 | |
4342 | |
4343 | Info.FFDiag(E); |
4344 | return false; |
4345 | } |
4346 | |
4347 | if (RHS.isInt()) { |
4348 | APSInt LHS = |
4349 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
4350 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
4351 | return false; |
4352 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
4353 | return true; |
4354 | } else if (RHS.isFloat()) { |
4355 | const FPOptions FPO = E->getFPFeaturesInEffect( |
4356 | Info.Ctx.getLangOpts()); |
4357 | APFloat FValue(0.0); |
4358 | return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value, |
4359 | PromotedLHSType, FValue) && |
4360 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
4361 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
4362 | Value); |
4363 | } |
4364 | |
4365 | Info.FFDiag(E); |
4366 | return false; |
4367 | } |
4368 | bool found(APFloat &Value, QualType SubobjType) { |
4369 | return checkConst(SubobjType) && |
4370 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
4371 | Value) && |
4372 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
4373 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
4374 | } |
4375 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4376 | if (!checkConst(SubobjType)) |
4377 | return false; |
4378 | |
4379 | QualType PointeeType; |
4380 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4381 | PointeeType = PT->getPointeeType(); |
4382 | |
4383 | if (PointeeType.isNull() || !RHS.isInt() || |
4384 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
4385 | Info.FFDiag(E); |
4386 | return false; |
4387 | } |
4388 | |
4389 | APSInt Offset = RHS.getInt(); |
4390 | if (Opcode == BO_Sub) |
4391 | negateAsSigned(Offset); |
4392 | |
4393 | LValue LVal; |
4394 | LVal.setFrom(Info.Ctx, Subobj); |
4395 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
4396 | return false; |
4397 | LVal.moveInto(Subobj); |
4398 | return true; |
4399 | } |
4400 | }; |
4401 | } |
4402 | |
4403 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
4404 | |
4405 | |
4406 | static bool handleCompoundAssignment(EvalInfo &Info, |
4407 | const CompoundAssignOperator *E, |
4408 | const LValue &LVal, QualType LValType, |
4409 | QualType PromotedLValType, |
4410 | BinaryOperatorKind Opcode, |
4411 | const APValue &RVal) { |
4412 | if (LVal.Designator.Invalid) |
4413 | return false; |
4414 | |
4415 | if (!Info.getLangOpts().CPlusPlus14) { |
4416 | Info.FFDiag(E); |
4417 | return false; |
4418 | } |
4419 | |
4420 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4421 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
4422 | RVal }; |
4423 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4424 | } |
4425 | |
4426 | namespace { |
4427 | struct IncDecSubobjectHandler { |
4428 | EvalInfo &Info; |
4429 | const UnaryOperator *E; |
4430 | AccessKinds AccessKind; |
4431 | APValue *Old; |
4432 | |
4433 | typedef bool result_type; |
4434 | |
4435 | bool checkConst(QualType QT) { |
4436 | |
4437 | if (QT.isConstQualified()) { |
4438 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4439 | return false; |
4440 | } |
4441 | return true; |
4442 | } |
4443 | |
4444 | bool failed() { return false; } |
4445 | bool found(APValue &Subobj, QualType SubobjType) { |
4446 | |
4447 | |
4448 | if (Old) { |
4449 | *Old = Subobj; |
4450 | Old = nullptr; |
4451 | } |
4452 | |
4453 | switch (Subobj.getKind()) { |
4454 | case APValue::Int: |
4455 | return found(Subobj.getInt(), SubobjType); |
4456 | case APValue::Float: |
4457 | return found(Subobj.getFloat(), SubobjType); |
4458 | case APValue::ComplexInt: |
4459 | return found(Subobj.getComplexIntReal(), |
4460 | SubobjType->castAs<ComplexType>()->getElementType() |
4461 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
4462 | case APValue::ComplexFloat: |
4463 | return found(Subobj.getComplexFloatReal(), |
4464 | SubobjType->castAs<ComplexType>()->getElementType() |
4465 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
4466 | case APValue::LValue: |
4467 | return foundPointer(Subobj, SubobjType); |
4468 | default: |
4469 | |
4470 | Info.FFDiag(E); |
4471 | return false; |
4472 | } |
4473 | } |
4474 | bool found(APSInt &Value, QualType SubobjType) { |
4475 | if (!checkConst(SubobjType)) |
4476 | return false; |
4477 | |
4478 | if (!SubobjType->isIntegerType()) { |
4479 | |
4480 | |
4481 | Info.FFDiag(E); |
4482 | return false; |
4483 | } |
4484 | |
4485 | if (Old) *Old = APValue(Value); |
4486 | |
4487 | |
4488 | |
4489 | if (SubobjType->isBooleanType()) { |
4490 | if (AccessKind == AK_Increment) |
4491 | Value = 1; |
4492 | else |
4493 | Value = !Value; |
4494 | return true; |
4495 | } |
4496 | |
4497 | bool WasNegative = Value.isNegative(); |
4498 | if (AccessKind == AK_Increment) { |
4499 | ++Value; |
4500 | |
4501 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
4502 | APSInt ActualValue(Value, true); |
4503 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4504 | } |
4505 | } else { |
4506 | --Value; |
4507 | |
4508 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
4509 | unsigned BitWidth = Value.getBitWidth(); |
4510 | APSInt ActualValue(Value.sext(BitWidth + 1), false); |
4511 | ActualValue.setBit(BitWidth); |
4512 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4513 | } |
4514 | } |
4515 | return true; |
4516 | } |
4517 | bool found(APFloat &Value, QualType SubobjType) { |
4518 | if (!checkConst(SubobjType)) |
4519 | return false; |
4520 | |
4521 | if (Old) *Old = APValue(Value); |
4522 | |
4523 | APFloat One(Value.getSemantics(), 1); |
4524 | if (AccessKind == AK_Increment) |
4525 | Value.add(One, APFloat::rmNearestTiesToEven); |
4526 | else |
4527 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
4528 | return true; |
4529 | } |
4530 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4531 | if (!checkConst(SubobjType)) |
4532 | return false; |
4533 | |
4534 | QualType PointeeType; |
4535 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4536 | PointeeType = PT->getPointeeType(); |
4537 | else { |
4538 | Info.FFDiag(E); |
4539 | return false; |
4540 | } |
4541 | |
4542 | LValue LVal; |
4543 | LVal.setFrom(Info.Ctx, Subobj); |
4544 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
4545 | AccessKind == AK_Increment ? 1 : -1)) |
4546 | return false; |
4547 | LVal.moveInto(Subobj); |
4548 | return true; |
4549 | } |
4550 | }; |
4551 | } |
4552 | |
4553 | |
4554 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4555 | QualType LValType, bool IsIncrement, APValue *Old) { |
4556 | if (LVal.Designator.Invalid) |
4557 | return false; |
4558 | |
4559 | if (!Info.getLangOpts().CPlusPlus14) { |
4560 | Info.FFDiag(E); |
4561 | return false; |
4562 | } |
4563 | |
4564 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
4565 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
4566 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
4567 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4568 | } |
4569 | |
4570 | |
4571 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
4572 | LValue &This) { |
4573 | if (Object->getType()->isPointerType() && Object->isPRValue()) |
4574 | return EvaluatePointer(Object, This, Info); |
4575 | |
4576 | if (Object->isGLValue()) |
4577 | return EvaluateLValue(Object, This, Info); |
4578 | |
4579 | if (Object->getType()->isLiteralType(Info.Ctx)) |
4580 | return EvaluateTemporary(Object, This, Info); |
4581 | |
4582 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
4583 | return false; |
4584 | } |
4585 | |
4586 | |
4587 | |
4588 | |
4589 | |
4590 | |
4591 | |
4592 | |
4593 | |
4594 | |
4595 | |
4596 | |
4597 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4598 | QualType LVType, |
4599 | LValue &LV, |
4600 | const Expr *RHS, |
4601 | bool IncludeMember = true) { |
4602 | MemberPtr MemPtr; |
4603 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
4604 | return nullptr; |
4605 | |
4606 | |
4607 | |
4608 | if (!MemPtr.getDecl()) { |
4609 | |
4610 | Info.FFDiag(RHS); |
4611 | return nullptr; |
4612 | } |
4613 | |
4614 | if (MemPtr.isDerivedMember()) { |
4615 | |
4616 | |
4617 | |
4618 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
4619 | LV.Designator.Entries.size()) { |
4620 | Info.FFDiag(RHS); |
4621 | return nullptr; |
4622 | } |
4623 | unsigned PathLengthToMember = |
4624 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
4625 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
4626 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
4627 | LV.Designator.Entries[PathLengthToMember + I]); |
4628 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
4629 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
4630 | Info.FFDiag(RHS); |
4631 | return nullptr; |
4632 | } |
4633 | } |
4634 | |
4635 | |
4636 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
4637 | PathLengthToMember)) |
4638 | return nullptr; |
4639 | } else if (!MemPtr.Path.empty()) { |
4640 | |
4641 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
4642 | MemPtr.Path.size() + IncludeMember); |
4643 | |
4644 | |
4645 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
4646 | LVType = PT->getPointeeType(); |
4647 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
4648 | assert(RD && "member pointer access on non-class-type expression"); |
4649 | |
4650 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
4651 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
4652 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
4653 | return nullptr; |
4654 | RD = Base; |
4655 | } |
4656 | |
4657 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
4658 | MemPtr.getContainingRecord())) |
4659 | return nullptr; |
4660 | } |
4661 | |
4662 | |
4663 | if (IncludeMember) { |
4664 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
4665 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
4666 | return nullptr; |
4667 | } else if (const IndirectFieldDecl *IFD = |
4668 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
4669 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
4670 | return nullptr; |
4671 | } else { |
4672 | llvm_unreachable("can't construct reference to bound member function"); |
4673 | } |
4674 | } |
4675 | |
4676 | return MemPtr.getDecl(); |
4677 | } |
4678 | |
4679 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4680 | const BinaryOperator *BO, |
4681 | LValue &LV, |
4682 | bool IncludeMember = true) { |
4683 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
4684 | |
4685 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
4686 | if (Info.noteFailure()) { |
4687 | MemberPtr MemPtr; |
4688 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
4689 | } |
4690 | return nullptr; |
4691 | } |
4692 | |
4693 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
4694 | BO->getRHS(), IncludeMember); |
4695 | } |
4696 | |
4697 | |
4698 | |
4699 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
4700 | LValue &Result) { |
4701 | SubobjectDesignator &D = Result.Designator; |
4702 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
4703 | return false; |
4704 | |
4705 | QualType TargetQT = E->getType(); |
4706 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
4707 | TargetQT = PT->getPointeeType(); |
4708 | |
4709 | |
4710 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
4711 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4712 | << D.MostDerivedType << TargetQT; |
4713 | return false; |
4714 | } |
4715 | |
4716 | |
4717 | |
4718 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
4719 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
4720 | const CXXRecordDecl *FinalType; |
4721 | if (NewEntriesSize == D.MostDerivedPathLength) |
4722 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
4723 | else |
4724 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
4725 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
4726 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4727 | << D.MostDerivedType << TargetQT; |
4728 | return false; |
4729 | } |
4730 | |
4731 | |
4732 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
4733 | } |
4734 | |
4735 | |
4736 | |
4737 | static bool getDefaultInitValue(QualType T, APValue &Result) { |
4738 | bool Success = true; |
4739 | if (auto *RD = T->getAsCXXRecordDecl()) { |
4740 | if (RD->isInvalidDecl()) { |
4741 | Result = APValue(); |
4742 | return false; |
4743 | } |
4744 | if (RD->isUnion()) { |
4745 | Result = APValue((const FieldDecl *)nullptr); |
4746 | return true; |
4747 | } |
4748 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
4749 | std::distance(RD->field_begin(), RD->field_end())); |
4750 | |
4751 | unsigned Index = 0; |
4752 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
4753 | End = RD->bases_end(); |
4754 | I != End; ++I, ++Index) |
4755 | Success &= getDefaultInitValue(I->getType(), Result.getStructBase(Index)); |
4756 | |
4757 | for (const auto *I : RD->fields()) { |
4758 | if (I->isUnnamedBitfield()) |
4759 | continue; |
4760 | Success &= getDefaultInitValue(I->getType(), |
4761 | Result.getStructField(I->getFieldIndex())); |
4762 | } |
4763 | return Success; |
4764 | } |
4765 | |
4766 | if (auto *AT = |
4767 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
4768 | Result = APValue(APValue::UninitArray(), 0, AT->getSize().getZExtValue()); |
4769 | if (Result.hasArrayFiller()) |
4770 | Success &= |
4771 | getDefaultInitValue(AT->getElementType(), Result.getArrayFiller()); |
4772 | |
4773 | return Success; |
4774 | } |
4775 | |
4776 | Result = APValue::IndeterminateValue(); |
4777 | return true; |
4778 | } |
4779 | |
4780 | namespace { |
4781 | enum EvalStmtResult { |
4782 | |
4783 | ESR_Failed, |
4784 | |
4785 | ESR_Returned, |
4786 | |
4787 | ESR_Succeeded, |
4788 | |
4789 | ESR_Continue, |
4790 | |
4791 | ESR_Break, |
4792 | |
4793 | ESR_CaseNotFound |
4794 | }; |
4795 | } |
4796 | |
4797 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
4798 | |
4799 | if (!VD->hasLocalStorage()) |
4800 | return true; |
4801 | |
4802 | LValue Result; |
4803 | APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(), |
4804 | ScopeKind::Block, Result); |
4805 | |
4806 | const Expr *InitE = VD->getInit(); |
4807 | if (!InitE) { |
4808 | if (VD->getType()->isDependentType()) |
4809 | return Info.noteSideEffect(); |
4810 | return getDefaultInitValue(VD->getType(), Val); |
4811 | } |
4812 | if (InitE->isValueDependent()) |
4813 | return false; |
4814 | |
4815 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
4816 | |
4817 | |
4818 | Val = APValue(); |
4819 | return false; |
4820 | } |
4821 | |
4822 | return true; |
4823 | } |
4824 | |
4825 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
4826 | bool OK = true; |
4827 | |
4828 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
4829 | OK &= EvaluateVarDecl(Info, VD); |
4830 | |
4831 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
4832 | for (auto *BD : DD->bindings()) |
4833 | if (auto *VD = BD->getHoldingVar()) |
4834 | OK &= EvaluateDecl(Info, VD); |
4835 | |
4836 | return OK; |
4837 | } |
4838 | |
4839 | static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) { |
4840 | assert(E->isValueDependent()); |
4841 | if (Info.noteSideEffect()) |
4842 | return true; |
4843 | assert(E->containsErrors() && "valid value-dependent expression should never " |
4844 | "reach invalid code path."); |
4845 | return false; |
4846 | } |
4847 | |
4848 | |
4849 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
4850 | const Expr *Cond, bool &Result) { |
4851 | if (Cond->isValueDependent()) |
4852 | return false; |
4853 | FullExpressionRAII Scope(Info); |
4854 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
4855 | return false; |
4856 | if (!EvaluateAsBooleanCondition(Cond, Result, Info)) |
4857 | return false; |
4858 | return Scope.destroy(); |
4859 | } |
4860 | |
4861 | namespace { |
4862 | |
4863 | |
4864 | struct StmtResult { |
4865 | |
4866 | APValue &Value; |
4867 | |
4868 | const LValue *Slot; |
4869 | }; |
4870 | |
4871 | struct TempVersionRAII { |
4872 | CallStackFrame &Frame; |
4873 | |
4874 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
4875 | Frame.pushTempVersion(); |
4876 | } |
4877 | |
4878 | ~TempVersionRAII() { |
4879 | Frame.popTempVersion(); |
4880 | } |
4881 | }; |
4882 | |
4883 | } |
4884 | |
4885 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
4886 | const Stmt *S, |
4887 | const SwitchCase *SC = nullptr); |
4888 | |
4889 | |
4890 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
4891 | const Stmt *Body, |
4892 | const SwitchCase *Case = nullptr) { |
4893 | BlockScopeRAII Scope(Info); |
4894 | |
4895 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case); |
4896 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
4897 | ESR = ESR_Failed; |
4898 | |
4899 | switch (ESR) { |
4900 | case ESR_Break: |
4901 | return ESR_Succeeded; |
4902 | case ESR_Succeeded: |
4903 | case ESR_Continue: |
4904 | return ESR_Continue; |
4905 | case ESR_Failed: |
4906 | case ESR_Returned: |
4907 | case ESR_CaseNotFound: |
4908 | return ESR; |
4909 | } |
4910 | llvm_unreachable("Invalid EvalStmtResult!"); |
4911 | } |
4912 | |
4913 | |
4914 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
4915 | const SwitchStmt *SS) { |
4916 | BlockScopeRAII Scope(Info); |
4917 | |
4918 | |
4919 | APSInt Value; |
4920 | { |
4921 | if (const Stmt *Init = SS->getInit()) { |
4922 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
4923 | if (ESR != ESR_Succeeded) { |
4924 | if (ESR != ESR_Failed && !Scope.destroy()) |
4925 | ESR = ESR_Failed; |
4926 | return ESR; |
4927 | } |
4928 | } |
4929 | |
4930 | FullExpressionRAII CondScope(Info); |
4931 | if (SS->getConditionVariable() && |
4932 | !EvaluateDecl(Info, SS->getConditionVariable())) |
4933 | return ESR_Failed; |
4934 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
4935 | return ESR_Failed; |
4936 | if (!CondScope.destroy()) |
4937 | return ESR_Failed; |
4938 | } |
4939 | |
4940 | |
4941 | |
4942 | const SwitchCase *Found = nullptr; |
4943 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
4944 | SC = SC->getNextSwitchCase()) { |
4945 | if (isa<DefaultStmt>(SC)) { |
4946 | Found = SC; |
4947 | continue; |
4948 | } |
4949 | |
4950 | const CaseStmt *CS = cast<CaseStmt>(SC); |
4951 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
4952 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
4953 | : LHS; |
4954 | if (LHS <= Value && Value <= RHS) { |
4955 | Found = SC; |
4956 | break; |
4957 | } |
4958 | } |
4959 | |
4960 | if (!Found) |
4961 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
4962 | |
4963 | |
4964 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found); |
4965 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
4966 | return ESR_Failed; |
4967 | |
4968 | switch (ESR) { |
4969 | case ESR_Break: |
4970 | return ESR_Succeeded; |
4971 | case ESR_Succeeded: |
4972 | case ESR_Continue: |
4973 | case ESR_Failed: |
4974 | case ESR_Returned: |
4975 | return ESR; |
4976 | case ESR_CaseNotFound: |
4977 | |
4978 | |
4979 | Info.FFDiag(Found->getBeginLoc(), |
4980 | diag::note_constexpr_stmt_expr_unsupported); |
4981 | return ESR_Failed; |
4982 | } |
4983 | llvm_unreachable("Invalid EvalStmtResult!"); |
4984 | } |
4985 | |
4986 | |
4987 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
4988 | const Stmt *S, const SwitchCase *Case) { |
4989 | if (!Info.nextStep(S)) |
4990 | return ESR_Failed; |
4991 | |
4992 | |
4993 | |
4994 | if (Case) { |
4995 | switch (S->getStmtClass()) { |
4996 | case Stmt::CompoundStmtClass: |
4997 | |
4998 | |
4999 | |
5000 | case Stmt::LabelStmtClass: |
5001 | case Stmt::AttributedStmtClass: |
5002 | case Stmt::DoStmtClass: |
5003 | break; |
5004 | |
5005 | case Stmt::CaseStmtClass: |
5006 | case Stmt::DefaultStmtClass: |
5007 | if (Case == S) |
5008 | Case = nullptr; |
5009 | break; |
5010 | |
5011 | case Stmt::IfStmtClass: { |
5012 | |
5013 | |
5014 | const IfStmt *IS = cast<IfStmt>(S); |
5015 | |
5016 | |
5017 | |
5018 | BlockScopeRAII Scope(Info); |
5019 | |
5020 | |
5021 | |
5022 | if (const Stmt *Init = IS->getInit()) { |
5023 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
5024 | if (ESR != ESR_CaseNotFound) { |
5025 | assert(ESR != ESR_Succeeded); |
5026 | return ESR; |
5027 | } |
5028 | } |
5029 | |
5030 | |
5031 | |
5032 | |
5033 | |
5034 | |
5035 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
5036 | if (ESR == ESR_Failed) |
5037 | return ESR; |
5038 | if (ESR != ESR_CaseNotFound) |
5039 | return Scope.destroy() ? ESR : ESR_Failed; |
5040 | if (!IS->getElse()) |
5041 | return ESR_CaseNotFound; |
5042 | |
5043 | ESR = EvaluateStmt(Result, Info, IS->getElse(), Case); |
5044 | if (ESR == ESR_Failed) |
5045 | return ESR; |
5046 | if (ESR != ESR_CaseNotFound) |
5047 | return Scope.destroy() ? ESR : ESR_Failed; |
5048 | return ESR_CaseNotFound; |
5049 | } |
5050 | |
5051 | case Stmt::WhileStmtClass: { |
5052 | EvalStmtResult ESR = |
5053 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
5054 | if (ESR != ESR_Continue) |
5055 | return ESR; |
5056 | break; |
5057 | } |
5058 | |
5059 | case Stmt::ForStmtClass: { |
5060 | const ForStmt *FS = cast<ForStmt>(S); |
5061 | BlockScopeRAII Scope(Info); |
5062 | |
5063 | |
5064 | |
5065 | if (const Stmt *Init = FS->getInit()) { |
5066 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init, Case); |
5067 | if (ESR != ESR_CaseNotFound) { |
5068 | assert(ESR != ESR_Succeeded); |
5069 | return ESR; |
5070 | } |
5071 | } |
5072 | |
5073 | EvalStmtResult ESR = |
5074 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
5075 | if (ESR != ESR_Continue) |
5076 | return ESR; |
5077 | if (const auto *Inc = FS->getInc()) { |
5078 | if (Inc->isValueDependent()) { |
5079 | if (!EvaluateDependentExpr(Inc, Info)) |
5080 | return ESR_Failed; |
5081 | } else { |
5082 | FullExpressionRAII IncScope(Info); |
5083 | if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy()) |
5084 | return ESR_Failed; |
5085 | } |
5086 | } |
5087 | break; |
5088 | } |
5089 | |
5090 | case Stmt::DeclStmtClass: { |
5091 | |
5092 | |
5093 | const DeclStmt *DS = cast<DeclStmt>(S); |
5094 | for (const auto *D : DS->decls()) { |
5095 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
5096 | if (VD->hasLocalStorage() && !VD->getInit()) |
5097 | if (!EvaluateVarDecl(Info, VD)) |
5098 | return ESR_Failed; |
5099 | |
5100 | |
5101 | |
5102 | } |
5103 | } |
5104 | return ESR_CaseNotFound; |
5105 | } |
5106 | |
5107 | default: |
5108 | return ESR_CaseNotFound; |
5109 | } |
5110 | } |
5111 | |
5112 | switch (S->getStmtClass()) { |
5113 | default: |
5114 | if (const Expr *E = dyn_cast<Expr>(S)) { |
5115 | if (E->isValueDependent()) { |
5116 | if (!EvaluateDependentExpr(E, Info)) |
5117 | return ESR_Failed; |
5118 | } else { |
5119 | |
5120 | |
5121 | |
5122 | |
5123 | FullExpressionRAII Scope(Info); |
5124 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
5125 | return ESR_Failed; |
5126 | } |
5127 | return ESR_Succeeded; |
5128 | } |
5129 | |
5130 | Info.FFDiag(S->getBeginLoc()); |
5131 | return ESR_Failed; |
5132 | |
5133 | case Stmt::NullStmtClass: |
5134 | return ESR_Succeeded; |
5135 | |
5136 | case Stmt::DeclStmtClass: { |
5137 | const DeclStmt *DS = cast<DeclStmt>(S); |
5138 | for (const auto *D : DS->decls()) { |
5139 | |
5140 | FullExpressionRAII Scope(Info); |
5141 | if (!EvaluateDecl(Info, D) && !Info.noteFailure()) |
5142 | return ESR_Failed; |
5143 | if (!Scope.destroy()) |
5144 | return ESR_Failed; |
5145 | } |
5146 | return ESR_Succeeded; |
5147 | } |
5148 | |
5149 | case Stmt::ReturnStmtClass: { |
5150 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
5151 | FullExpressionRAII Scope(Info); |
5152 | if (RetExpr && RetExpr->isValueDependent()) { |
5153 | EvaluateDependentExpr(RetExpr, Info); |
5154 | |
5155 | return ESR_Failed; |
5156 | } |
5157 | if (RetExpr && |
5158 | !(Result.Slot |
5159 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
5160 | : Evaluate(Result.Value, Info, RetExpr))) |
5161 | return ESR_Failed; |
5162 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
5163 | } |
5164 | |
5165 | case Stmt::CompoundStmtClass: { |
5166 | BlockScopeRAII Scope(Info); |
5167 | |
5168 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
5169 | for (const auto *BI : CS->body()) { |
5170 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
5171 | if (ESR == ESR_Succeeded) |
5172 | Case = nullptr; |
5173 | else if (ESR != ESR_CaseNotFound) { |
5174 | if (ESR != ESR_Failed && !Scope.destroy()) |
5175 | return ESR_Failed; |
5176 | return ESR; |
5177 | } |
5178 | } |
5179 | if (Case) |
5180 | return ESR_CaseNotFound; |
5181 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5182 | } |
5183 | |
5184 | case Stmt::IfStmtClass: { |
5185 | const IfStmt *IS = cast<IfStmt>(S); |
5186 | |
5187 | |
5188 | BlockScopeRAII Scope(Info); |
5189 | if (const Stmt *Init = IS->getInit()) { |
5190 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
5191 | if (ESR != ESR_Succeeded) { |
5192 | if (ESR != ESR_Failed && !Scope.destroy()) |
5193 | return ESR_Failed; |
5194 | return ESR; |
5195 | } |
5196 | } |
5197 | bool Cond; |
5198 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
5199 | return ESR_Failed; |
5200 | |
5201 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
5202 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
5203 | if (ESR != ESR_Succeeded) { |
5204 | if (ESR != ESR_Failed && !Scope.destroy()) |
5205 | return ESR_Failed; |
5206 | return ESR; |
5207 | } |
5208 | } |
5209 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5210 | } |
5211 | |
5212 | case Stmt::WhileStmtClass: { |
5213 | const WhileStmt *WS = cast<WhileStmt>(S); |
5214 | while (true) { |
5215 | BlockScopeRAII Scope(Info); |
5216 | bool Continue; |
5217 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
5218 | Continue)) |
5219 | return ESR_Failed; |
5220 | if (!Continue) |
5221 | break; |
5222 | |
5223 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
5224 | if (ESR != ESR_Continue) { |
5225 | if (ESR != ESR_Failed && !Scope.destroy()) |
5226 | return ESR_Failed; |
5227 | return ESR; |
5228 | } |
5229 | if (!Scope.destroy()) |
5230 | return ESR_Failed; |
5231 | } |
5232 | return ESR_Succeeded; |
5233 | } |
5234 | |
5235 | case Stmt::DoStmtClass: { |
5236 | const DoStmt *DS = cast<DoStmt>(S); |
5237 | bool Continue; |
5238 | do { |
5239 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
5240 | if (ESR != ESR_Continue) |
5241 | return ESR; |
5242 | Case = nullptr; |
5243 | |
5244 | if (DS->getCond()->isValueDependent()) { |
5245 | EvaluateDependentExpr(DS->getCond(), Info); |
5246 | |
5247 | return ESR_Failed; |
5248 | } |
5249 | FullExpressionRAII CondScope(Info); |
5250 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info) || |
5251 | !CondScope.destroy()) |
5252 | return ESR_Failed; |
5253 | } while (Continue); |
5254 | return ESR_Succeeded; |
5255 | } |
5256 | |
5257 | case Stmt::ForStmtClass: { |
5258 | const ForStmt *FS = cast<ForStmt>(S); |
5259 | BlockScopeRAII ForScope(Info); |
5260 | if (FS->getInit()) { |
5261 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
5262 | if (ESR != ESR_Succeeded) { |
5263 | if (ESR != ESR_Failed && !ForScope.destroy()) |
5264 | return ESR_Failed; |
5265 | return ESR; |
5266 | } |
5267 | } |
5268 | while (true) { |
5269 | BlockScopeRAII IterScope(Info); |
5270 | bool Continue = true; |
5271 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
5272 | FS->getCond(), Continue)) |
5273 | return ESR_Failed; |
5274 | if (!Continue) |
5275 | break; |
5276 | |
5277 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
5278 | if (ESR != ESR_Continue) { |
5279 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
5280 | return ESR_Failed; |
5281 | return ESR; |
5282 | } |
5283 | |
5284 | if (const auto *Inc = FS->getInc()) { |
5285 | if (Inc->isValueDependent()) { |
5286 | if (!EvaluateDependentExpr(Inc, Info)) |
5287 | return ESR_Failed; |
5288 | } else { |
5289 | FullExpressionRAII IncScope(Info); |
5290 | if (!EvaluateIgnoredValue(Info, Inc) || !IncScope.destroy()) |
5291 | return ESR_Failed; |
5292 | } |
5293 | } |
5294 | |
5295 | if (!IterScope.destroy()) |
5296 | return ESR_Failed; |
5297 | } |
5298 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
5299 | } |
5300 | |
5301 | case Stmt::CXXForRangeStmtClass: { |
5302 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
5303 | BlockScopeRAII Scope(Info); |
5304 | |
5305 | |
5306 | if (FS->getInit()) { |
5307 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
5308 | if (ESR != ESR_Succeeded) { |
5309 | if (ESR != ESR_Failed && !Scope.destroy()) |
5310 | return ESR_Failed; |
5311 | return ESR; |
5312 | } |
5313 | } |
5314 | |
5315 | |
5316 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
5317 | if (ESR != ESR_Succeeded) { |
5318 | if (ESR != ESR_Failed && !Scope.destroy()) |
5319 | return ESR_Failed; |
5320 | return ESR; |
5321 | } |
5322 | |
5323 | |
5324 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
5325 | if (ESR != ESR_Succeeded) { |
5326 | if (ESR != ESR_Failed && !Scope.destroy()) |
5327 | return ESR_Failed; |
5328 | return ESR; |
5329 | } |
5330 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
5331 | if (ESR != ESR_Succeeded) { |
5332 | if (ESR != ESR_Failed && !Scope.destroy()) |
5333 | return ESR_Failed; |
5334 | return ESR; |
5335 | } |
5336 | |
5337 | while (true) { |
5338 | |
5339 | { |
5340 | if (FS->getCond()->isValueDependent()) { |
5341 | EvaluateDependentExpr(FS->getCond(), Info); |
5342 | |
5343 | return ESR_Failed; |
5344 | } |
5345 | bool Continue = true; |
5346 | FullExpressionRAII CondExpr(Info); |
5347 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
5348 | return ESR_Failed; |
5349 | if (!Continue) |
5350 | break; |
5351 | } |
5352 | |
5353 | |
5354 | BlockScopeRAII InnerScope(Info); |
5355 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
5356 | if (ESR != ESR_Succeeded) { |
5357 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5358 | return ESR_Failed; |
5359 | return ESR; |
5360 | } |
5361 | |
5362 | |
5363 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
5364 | if (ESR != ESR_Continue) { |
5365 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5366 | return ESR_Failed; |
5367 | return ESR; |
5368 | } |
5369 | if (FS->getInc()->isValueDependent()) { |
5370 | if (!EvaluateDependentExpr(FS->getInc(), Info)) |
5371 | return ESR_Failed; |
5372 | } else { |
5373 | |
5374 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
5375 | return ESR_Failed; |
5376 | } |
5377 | |
5378 | if (!InnerScope.destroy()) |
5379 | return ESR_Failed; |
5380 | } |
5381 | |
5382 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5383 | } |
5384 | |
5385 | case Stmt::SwitchStmtClass: |
5386 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
5387 | |
5388 | case Stmt::ContinueStmtClass: |
5389 | return ESR_Continue; |
5390 | |
5391 | case Stmt::BreakStmtClass: |
5392 | return ESR_Break; |
5393 | |
5394 | case Stmt::LabelStmtClass: |
5395 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
5396 | |
5397 | case Stmt::AttributedStmtClass: |
5398 | |
5399 | |
5400 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
5401 | Case); |
5402 | |
5403 | case Stmt::CaseStmtClass: |
5404 | case Stmt::DefaultStmtClass: |
5405 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
5406 | case Stmt::CXXTryStmtClass: |
5407 | |
5408 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case); |
5409 | } |
5410 | } |
5411 | |
5412 | |
5413 | |
5414 | |
5415 | |
5416 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
5417 | const CXXConstructorDecl *CD, |
5418 | bool IsValueInitialization) { |
5419 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
5420 | return false; |
5421 | |
5422 | |
5423 | |
5424 | |
5425 | if (!CD->isConstexpr() && !IsValueInitialization) { |
5426 | if (Info.getLangOpts().CPlusPlus11) { |
5427 | |
5428 | |
5429 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
5430 | << 0 << 1 << CD; |
5431 | Info.Note(CD->getLocation(), diag::note_declared_at); |
5432 | } else { |
5433 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
5434 | } |
5435 | } |
5436 | return true; |
5437 | } |
5438 | |
5439 | |
5440 | |
5441 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
5442 | const FunctionDecl *Declaration, |
5443 | const FunctionDecl *Definition, |
5444 | const Stmt *Body) { |
5445 | |
5446 | |
5447 | if (Info.checkingPotentialConstantExpression() && !Definition && |
5448 | Declaration->isConstexpr()) |
5449 | return false; |
5450 | |
5451 | |
5452 | |
5453 | |
5454 | if (Declaration->isInvalidDecl()) { |
5455 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5456 | return false; |
5457 | } |
5458 | |
5459 | |
5460 | |
5461 | |
5462 | if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) && |
5463 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
5464 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
5465 | |
5466 | if (Definition && Definition->isInvalidDecl()) { |
5467 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5468 | return false; |
5469 | } |
5470 | |
5471 | |
5472 | if (Definition && Definition->isConstexpr() && Body) |
5473 | return true; |
5474 | |
5475 | if (Info.getLangOpts().CPlusPlus11) { |
5476 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
5477 | |
5478 | |
5479 | |
5480 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
5481 | if (CD && CD->isInheritingConstructor()) { |
5482 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
5483 | if (!Inherited->isConstexpr()) |
5484 | DiagDecl = CD = Inherited; |
5485 | } |
5486 | |
5487 | |
5488 | |
5489 | |
5490 | if (CD && CD->isInheritingConstructor()) |
5491 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
5492 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
5493 | else |
5494 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
5495 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
5496 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
5497 | } else { |
5498 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5499 | } |
5500 | return false; |
5501 | } |
5502 | |
5503 | namespace { |
5504 | struct CheckDynamicTypeHandler { |
5505 | AccessKinds AccessKind; |
5506 | typedef bool result_type; |
5507 | bool failed() { return false; } |
5508 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
5509 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
5510 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
5511 | }; |
5512 | } |
5513 | |
5514 | |
5515 | |
5516 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
5517 | AccessKinds AK, bool Polymorphic) { |
5518 | if (This.Designator.Invalid) |
5519 | return false; |
5520 | |
5521 | CompleteObject Obj = findCompleteObject(Info, E, AK, This, QualType()); |
5522 | |
5523 | if (!Obj) |
5524 | return false; |
5525 | |
5526 | if (!Obj.Value) { |
5527 | |
5528 | |
5529 | |
5530 | if (This.Designator.isOnePastTheEnd() || |
5531 | This.Designator.isMostDerivedAnUnsizedArray()) { |
5532 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
5533 | ? diag::note_constexpr_access_past_end |
5534 | : diag::note_constexpr_access_unsized_array) |
5535 | << AK; |
5536 | return false; |
5537 | } else if (Polymorphic) { |
5538 | |
5539 | |
5540 | APValue Val; |
5541 | This.moveInto(Val); |
5542 | QualType StarThisType = |
5543 | Info.Ctx.getLValueReferenceType(This.Designator.getType(Info.Ctx)); |
5544 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
5545 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
5546 | return false; |
5547 | } |
5548 | return true; |
5549 | } |
5550 | |
5551 | CheckDynamicTypeHandler Handler{AK}; |
5552 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
5553 | } |
5554 | |
5555 | |
5556 | |
5557 | static bool |
5558 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
5559 | const LValue &This, |
5560 | const CXXMethodDecl *NamedMember) { |
5561 | return checkDynamicType( |
5562 | Info, E, This, |
5563 | isa<CXXDestructorDecl>(NamedMember) ? AK_Destroy : AK_MemberCall, false); |
5564 | } |
5565 | |
5566 | struct DynamicType { |
5567 | |
5568 | const CXXRecordDecl *Type; |
5569 | |
5570 | unsigned PathLength; |
5571 | }; |
5572 | |
5573 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
5574 | unsigned PathLength) { |
5575 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
5576 | Designator.Entries.size() && "invalid path length"); |
5577 | return (PathLength == Designator.MostDerivedPathLength) |
5578 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
5579 | : getAsBaseClass(Designator.Entries[PathLength - 1]); |
5580 | } |
5581 | |
5582 | |
5583 | static Optional<DynamicType> ComputeDynamicType(EvalInfo &Info, const Expr *E, |
5584 | LValue &This, AccessKinds AK) { |
5585 | |
5586 | |
5587 | |
5588 | if (!checkDynamicType(Info, E, This, AK, true)) |
5589 | return None; |
5590 | |
5591 | |
5592 | |
5593 | |
5594 | |
5595 | |
5596 | |
5597 | const CXXRecordDecl *Class = |
5598 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
5599 | if (!Class || Class->getNumVBases()) { |
5600 | Info.FFDiag(E); |
5601 | return None; |
5602 | } |
5603 | |
5604 | |
5605 | |
5606 | |
5607 | |
5608 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
5609 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
5610 | PathLength <= Path.size(); ++PathLength) { |
5611 | switch (Info.isEvaluatingCtorDtor(This.getLValueBase(), |
5612 | Path.slice(0, PathLength))) { |
5613 | case ConstructionPhase::Bases: |
5614 | case ConstructionPhase::DestroyingBases: |
5615 | |
5616 | |
5617 | break; |
5618 | |
5619 | case ConstructionPhase::None: |
5620 | case ConstructionPhase::AfterBases: |
5621 | case ConstructionPhase::AfterFields: |
5622 | case ConstructionPhase::Destroying: |
5623 | |
5624 | |
5625 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
5626 | PathLength}; |
5627 | } |
5628 | } |
5629 | |
5630 | |
5631 | |
5632 | |
5633 | Info.FFDiag(E); |
5634 | return None; |
5635 | } |
5636 | |
5637 | |
5638 | static const CXXMethodDecl *HandleVirtualDispatch( |
5639 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
5640 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
5641 | Optional<DynamicType> DynType = ComputeDynamicType( |
5642 | Info, E, This, |
5643 | isa<CXXDestructorDecl>(Found) ? AK_Destroy : AK_MemberCall); |
5644 | if (!DynType) |
5645 | return nullptr; |
5646 | |
5647 | |
5648 | |
5649 | |
5650 | |
5651 | const CXXMethodDecl *Callee = Found; |
5652 | unsigned PathLength = DynType->PathLength; |
5653 | for (; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
5654 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
5655 | const CXXMethodDecl *Overrider = |
5656 | Found->getCorrespondingMethodDeclaredInClass(Class, false); |
5657 | if (Overrider) { |
5658 | Callee = Overrider; |
5659 | break; |
5660 | } |
5661 | } |
5662 | |
5663 | |
5664 | |
5665 | |
5666 | if (Callee->isPure()) { |
5667 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
5668 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
5669 | return nullptr; |
5670 | } |
5671 | |
5672 | |
5673 | |
5674 | if (!Info.Ctx.hasSameUnqualifiedType(Callee->getReturnType(), |
5675 | Found->getReturnType())) { |
5676 | CovariantAdjustmentPath.push_back(Callee->getReturnType()); |
5677 | for (unsigned CovariantPathLength = PathLength + 1; |
5678 | CovariantPathLength != This.Designator.Entries.size(); |
5679 | ++CovariantPathLength) { |
5680 | const CXXRecordDecl *NextClass = |
5681 | getBaseClassType(This.Designator, CovariantPathLength); |
5682 | const CXXMethodDecl *Next = |
5683 | Found->getCorrespondingMethodDeclaredInClass(NextClass, false); |
5684 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
5685 | Next->getReturnType(), CovariantAdjustmentPath.back())) |
5686 | CovariantAdjustmentPath.push_back(Next->getReturnType()); |
5687 | } |
5688 | if (!Info.Ctx.hasSameUnqualifiedType(Found->getReturnType(), |
5689 | CovariantAdjustmentPath.back())) |
5690 | CovariantAdjustmentPath.push_back(Found->getReturnType()); |
5691 | } |
5692 | |
5693 | |
5694 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
5695 | return nullptr; |
5696 | |
5697 | return Callee; |
5698 | } |
5699 | |
5700 | |
5701 | |
5702 | |
5703 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
5704 | APValue &Result, |
5705 | ArrayRef<QualType> Path) { |
5706 | assert(Result.isLValue() && |
5707 | "unexpected kind of APValue for covariant return"); |
5708 | if (Result.isNullPointer()) |
5709 | return true; |
5710 | |
5711 | LValue LVal; |
5712 | LVal.setFrom(Info.Ctx, Result); |
5713 | |
5714 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
5715 | for (unsigned I = 1; I != Path.size(); ++I) { |
5716 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
5717 | assert(OldClass && NewClass && "unexpected kind of covariant return"); |
5718 | if (OldClass != NewClass && |
5719 | !CastToBaseClass(Info, E, LVal, OldClass, NewClass)) |
5720 | return false; |
5721 | OldClass = NewClass; |
5722 | } |
5723 | |
5724 | LVal.moveInto(Result); |
5725 | return true; |
5726 | } |
5727 | |
5728 | |
5729 | |
5730 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
5731 | const CXXRecordDecl *Base) { |
5732 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
5733 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
5734 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
5735 | return BaseSpec.getAccessSpecifier() == AS_public; |
5736 | } |
5737 | llvm_unreachable("Base is not a direct base of Derived"); |
5738 | } |
5739 | |
5740 | |
5741 | |
5742 | |
5743 | |
5744 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
5745 | LValue &Ptr) { |
5746 | |
5747 | SubobjectDesignator &D = Ptr.Designator; |
5748 | if (D.Invalid) |
5749 | return false; |
5750 | |
5751 | |
5752 | |
5753 | if (Ptr.isNullPointer() && !E->isGLValue()) |
5754 | return true; |
5755 | |
5756 | |
5757 | |
5758 | |
5759 | Optional<DynamicType> DynType = |
5760 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
5761 | if (!DynType) |
5762 | return false; |
5763 | |
5764 | |
5765 | |
5766 | |
5767 | if (E->getType()->isVoidPointerType()) |
5768 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
5769 | |
5770 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
5771 | assert(C && "dynamic_cast target is not void pointer nor class"); |
5772 | CanQualType CQT = Info.Ctx.getCanonicalType(Info.Ctx.getRecordType(C)); |
5773 | |
5774 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
5775 | |
5776 | if (!E->isGLValue()) { |
5777 | |
5778 | |
5779 | Ptr.setNull(Info.Ctx, E->getType()); |
5780 | return true; |
5781 | } |
5782 | |
5783 | |
5784 | unsigned DiagKind; |
5785 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
5786 | DynType->Type->isDerivedFrom(C))) |
5787 | DiagKind = 0; |
5788 | else if (!Paths || Paths->begin() == Paths->end()) |
5789 | DiagKind = 1; |
5790 | else if (Paths->isAmbiguous(CQT)) |
5791 | DiagKind = 2; |
5792 | else { |
5793 | assert(Paths->front().Access != AS_public && "why did the cast fail?"); |
5794 | DiagKind = 3; |
5795 | } |
5796 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
5797 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
5798 | << Info.Ctx.getRecordType(DynType->Type) |
5799 | << E->getType().getUnqualifiedType(); |
5800 | return false; |
5801 | }; |
5802 | |
5803 | |
5804 | |
5805 | |
5806 | for (int PathLength = Ptr.Designator.Entries.size(); |
5807 | PathLength >= (int)DynType->PathLength; --PathLength) { |
5808 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
5809 | if (declaresSameEntity(Class, C)) |
5810 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
5811 | |
5812 | if (PathLength > (int)DynType->PathLength && |
5813 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
5814 | Class)) |
5815 | return RuntimeCheckFailed(nullptr); |
5816 | } |
5817 | |
5818 | |
5819 | |
5820 | CXXBasePaths Paths(true, |
5821 | true, false); |
5822 | if (DynType->Type->isDerivedFrom(C, Paths) && !Paths.isAmbiguous(CQT) && |
5823 | Paths.front().Access == AS_public) { |
5824 | |
5825 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
5826 | return false; |
5827 | |
5828 | for (CXXBasePathElement &Elem : Paths.front()) |
5829 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
5830 | return false; |
5831 | return true; |
5832 | } |
5833 | |
5834 | |
5835 | return RuntimeCheckFailed(&Paths); |
5836 | } |
5837 | |
5838 | namespace { |
5839 | struct StartLifetimeOfUnionMemberHandler { |
5840 | EvalInfo &Info; |
5841 | const Expr *LHSExpr; |
5842 | const FieldDecl *Field; |
5843 | bool DuringInit; |
5844 | bool Failed = false; |
5845 | static const AccessKinds AccessKind = AK_Assign; |
5846 | |
5847 | typedef bool result_type; |
5848 | bool failed() { return Failed; } |
5849 | bool found(APValue &Subobj, QualType SubobjType) { |
5850 | |
5851 | |
5852 | |
5853 | |
5854 | |
5855 | |
5856 | |
5857 | |
5858 | assert(SubobjType->isUnionType()); |
5859 | if (declaresSameEntity(Subobj.getUnionField(), Field)) { |
5860 | |
5861 | |
5862 | if (Subobj.getUnionValue().hasValue()) |
5863 | return true; |
5864 | } else if (DuringInit) { |
5865 | |
5866 | |
5867 | |
5868 | Info.FFDiag(LHSExpr, |
5869 | diag::note_constexpr_union_member_change_during_init); |
5870 | return false; |
5871 | } |
5872 | APValue Result; |
5873 | Failed = !getDefaultInitValue(Field->getType(), Result); |
5874 | Subobj.setUnion(Field, Result); |
5875 | return true; |
5876 | } |
5877 | bool found(APSInt &Value, QualType SubobjType) { |
5878 | llvm_unreachable("wrong value kind for union object"); |
5879 | } |
5880 | bool found(APFloat &Value, QualType SubobjType) { |
5881 | llvm_unreachable("wrong value kind for union object"); |
5882 | } |
5883 | }; |
5884 | } |
5885 | |
5886 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
5887 | |
5888 | |
5889 | |
5890 | |
5891 | |
5892 | static bool HandleUnionActiveMemberChange(EvalInfo &Info, const Expr *LHSExpr, |
5893 | const LValue &LHS) { |
5894 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
5895 | return false; |
5896 | |
5897 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
5898 | |
5899 | |
5900 | unsigned PathLength = LHS.Designator.Entries.size(); |
5901 | for (const Expr *E = LHSExpr; E != nullptr;) { |
5902 | |
5903 | if (auto *ME = dyn_cast<MemberExpr>(E)) { |
5904 | auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()); |
5905 | |
5906 | |
5907 | if (!FD || FD->getType()->isReferenceType()) |
5908 | break; |
5909 | |
5910 | |
5911 | if (FD->getParent()->isUnion()) { |
5912 | |
5913 | |
5914 | |
5915 | auto *RD = |
5916 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
5917 | if (!RD || RD->hasTrivialDefaultConstructor()) |
5918 | UnionPathLengths.push_back({PathLength - 1, FD}); |
5919 | } |
5920 | |
5921 | E = ME->getBase(); |
5922 | --PathLength; |
5923 | assert(declaresSameEntity(FD, |
5924 | LHS.Designator.Entries[PathLength] |
5925 | .getAsBaseOrMember().getPointer())); |
5926 | |
5927 | |
5928 | |
5929 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
5930 | |
5931 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
5932 | if (!Base->getType()->isArrayType()) |
5933 | break; |
5934 | |
5935 | E = Base; |
5936 | --PathLength; |
5937 | |
5938 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
5939 | |
5940 | E = ICE->getSubExpr(); |
5941 | if (ICE->getCastKind() == CK_NoOp) |
5942 | continue; |
5943 | if (ICE->getCastKind() != CK_DerivedToBase && |
5944 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
5945 | break; |
5946 | |
5947 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
5948 | --PathLength; |
5949 | (void)Elt; |
5950 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
5951 | LHS.Designator.Entries[PathLength] |
5952 | .getAsBaseOrMember().getPointer())); |
5953 | } |
5954 | |
5955 | |
5956 | } else { |
5957 | break; |
5958 | } |
5959 | } |
5960 | |
5961 | |
5962 | if (UnionPathLengths.empty()) |
5963 | return true; |
5964 | |
5965 | |
5966 | |
5967 | CompleteObject Obj = |
5968 | findCompleteObject(Info, LHSExpr, AK_Assign, LHS, LHSExpr->getType()); |
5969 | if (!Obj) |
5970 | return false; |
5971 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
5972 | llvm::reverse(UnionPathLengths)) { |
5973 | |
5974 | SubobjectDesignator D = LHS.Designator; |
5975 | D.truncate(Info.Ctx, LHS.Base, LengthAndField.first); |
5976 | |
5977 | bool DuringInit = Info.isEvaluatingCtorDtor(LHS.Base, D.Entries) == |
5978 | ConstructionPhase::AfterBases; |
5979 | StartLifetimeOfUnionMemberHandler StartLifetime{ |
5980 | Info, LHSExpr, LengthAndField.second, DuringInit}; |
5981 | if (!findSubobject(Info, LHSExpr, Obj, D, StartLifetime)) |
5982 | return false; |
5983 | } |
5984 | |
5985 | return true; |
5986 | } |
5987 | |
5988 | static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg, |
5989 | CallRef Call, EvalInfo &Info, |
5990 | bool NonNull = false) { |
5991 | LValue LV; |
5992 | |
5993 | |
5994 | |
5995 | |
5996 | |
5997 | APValue &V = PVD ? Info.CurrentCall->createParam(Call, PVD, LV) |
5998 | : Info.CurrentCall->createTemporary(Arg, Arg->getType(), |
5999 | ScopeKind::Call, LV); |
6000 | if (!EvaluateInPlace(V, Info, LV, Arg)) |
6001 | return false; |
6002 | |
6003 | |
6004 | |
6005 | if (NonNull && V.isLValue() && V.isNullPointer()) { |
6006 | Info.CCEDiag(Arg, diag::note_non_null_attribute_failed); |
6007 | return false; |
6008 | } |
6009 | |
6010 | return true; |
6011 | } |
6012 | |
6013 | |
6014 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call, |
6015 | EvalInfo &Info, const FunctionDecl *Callee, |
6016 | bool RightToLeft = false) { |
6017 | bool Success = true; |
6018 | llvm::SmallBitVector ForbiddenNullArgs; |
6019 | if (Callee->hasAttr<NonNullAttr>()) { |
6020 | ForbiddenNullArgs.resize(Args.size()); |
6021 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
6022 | if (!Attr->args_size()) { |
6023 | ForbiddenNullArgs.set(); |
6024 | break; |
6025 | } else |
6026 | for (auto Idx : Attr->args()) { |
6027 | unsigned ASTIdx = Idx.getASTIndex(); |
6028 | if (ASTIdx >= Args.size()) |
6029 | continue; |
6030 | ForbiddenNullArgs[ASTIdx] = 1; |
6031 | } |
6032 | } |
6033 | } |
6034 | for (unsigned I = 0; I < Args.size(); I++) { |
6035 | unsigned Idx = RightToLeft ? Args.size() - I - 1 : I; |
6036 | const ParmVarDecl *PVD = |
6037 | Idx < Callee->getNumParams() ? Callee->getParamDecl(Idx) : nullptr; |
6038 | bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx]; |
6039 | if (!EvaluateCallArg(PVD, Args[Idx], Call, Info, NonNull)) { |
6040 | |
6041 | |
6042 | if (!Info.noteFailure()) |
6043 | return false; |
6044 | Success = false; |
6045 | } |
6046 | } |
6047 | return Success; |
6048 | } |
6049 | |
6050 | |
6051 | |
6052 | static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param, |
6053 | const Expr *E, APValue &Result, |
6054 | bool CopyObjectRepresentation) { |
6055 | |
6056 | CallStackFrame *Frame = Info.CurrentCall; |
6057 | APValue *RefValue = Info.getParamSlot(Frame->Arguments, Param); |
6058 | if (!RefValue) { |
6059 | Info.FFDiag(E); |
6060 | return false; |
6061 | } |
6062 | |
6063 | |
6064 | LValue RefLValue; |
6065 | RefLValue.setFrom(Info.Ctx, *RefValue); |
6066 | return handleLValueToRValueConversion( |
6067 | Info, E, Param->getType().getNonReferenceType(), RefLValue, Result, |
6068 | CopyObjectRepresentation); |
6069 | } |
6070 | |
6071 | |
6072 | static bool HandleFunctionCall(SourceLocation CallLoc, |
6073 | const FunctionDecl *Callee, const LValue *This, |
6074 | ArrayRef<const Expr *> Args, CallRef Call, |
6075 | const Stmt *Body, EvalInfo &Info, |
6076 | APValue &Result, const LValue *ResultSlot) { |
6077 | if (!Info.CheckCallLimit(CallLoc)) |
6078 | return false; |
6079 | |
6080 | CallStackFrame Frame(Info, CallLoc, Callee, This, Call); |
6081 | |
6082 | |
6083 | |
6084 | |
6085 | |
6086 | |
6087 | |
6088 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
6089 | if (MD && MD->isDefaulted() && |
6090 | (MD->getParent()->isUnion() || |
6091 | (MD->isTrivial() && |
6092 | isReadByLvalueToRvalueConversion(MD->getParent())))) { |
6093 | assert(This && |
6094 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
6095 | APValue RHSValue; |
6096 | if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue, |
6097 | MD->getParent()->isUnion())) |
6098 | return false; |
6099 | if (Info.getLangOpts().CPlusPlus20 && MD->isTrivial() && |
6100 | !HandleUnionActiveMemberChange(Info, Args[0], *This)) |
6101 | return false; |
6102 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(), |
6103 | RHSValue)) |
6104 | return false; |
6105 | This->moveInto(Result); |
6106 | return true; |
6107 | } else if (MD && isLambdaCallOperator(MD)) { |
6108 | |
6109 | |
6110 | |
6111 | |
6112 | |
6113 | |
6114 | if (!Info.checkingPotentialConstantExpression()) |
6115 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
6116 | Frame.LambdaThisCaptureField); |
6117 | } |
6118 | |
6119 | StmtResult Ret = {Result, ResultSlot}; |
6120 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
6121 | if (ESR == ESR_Succeeded) { |
6122 | if (Callee->getReturnType()->isVoidType()) |
6123 | return true; |
6124 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
6125 | } |
6126 | return ESR == ESR_Returned; |
6127 | } |
6128 | |
6129 | |
6130 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6131 | CallRef Call, |
6132 | const CXXConstructorDecl *Definition, |
6133 | EvalInfo &Info, APValue &Result) { |
6134 | SourceLocation CallLoc = E->getExprLoc(); |
6135 | if (!Info.CheckCallLimit(CallLoc)) |
6136 | return false; |
6137 | |
6138 | const CXXRecordDecl *RD = Definition->getParent(); |
6139 | if (RD->getNumVBases()) { |
6140 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6141 | return false; |
6142 | } |
6143 | |
6144 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
6145 | Info, |
6146 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
6147 | RD->getNumBases()); |
6148 | CallStackFrame Frame(Info, CallLoc, Definition, &This, Call); |
6149 | |
6150 | |
6151 | |
6152 | APValue RetVal; |
6153 | StmtResult Ret = {RetVal, nullptr}; |
6154 | |
6155 | |
6156 | if (Definition->isDelegatingConstructor()) { |
6157 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
6158 | if ((*I)->getInit()->isValueDependent()) { |
6159 | if (!EvaluateDependentExpr((*I)->getInit(), Info)) |
6160 | return false; |
6161 | } else { |
6162 | FullExpressionRAII InitScope(Info); |
6163 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit()) || |
6164 | !InitScope.destroy()) |
6165 | return false; |
6166 | } |
6167 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
6168 | } |
6169 | |
6170 | |
6171 | |
6172 | |
6173 | |
6174 | |
6175 | |
6176 | |
6177 | |
6178 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
6179 | (Definition->getParent()->isUnion() || |
6180 | (Definition->isTrivial() && |
6181 | isReadByLvalueToRvalueConversion(Definition->getParent())))) { |
6182 | return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result, |
6183 | Definition->getParent()->isUnion()); |
6184 | } |
6185 | |
6186 | |
6187 | if (!Result.hasValue()) { |
6188 | if (!RD->isUnion()) |
6189 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
6190 | std::distance(RD->field_begin(), RD->field_end())); |
6191 | else |
6192 | |
6193 | Result = APValue((const FieldDecl*)nullptr); |
6194 | } |
6195 | |
6196 | if (RD->isInvalidDecl()) return false; |
6197 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6198 | |
6199 | |
6200 | BlockScopeRAII LifetimeExtendedScope(Info); |
6201 | |
6202 | bool Success = true; |
6203 | unsigned BasesSeen = 0; |
6204 | #ifndef NDEBUG |
6205 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
6206 | #endif |
6207 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
6208 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
6209 | |
6210 | |
6211 | if (FieldIt == RD->field_end() || |
6212 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
6213 | assert(Indirect && "fields out of order?"); |
6214 | return; |
6215 | } |
6216 | |
6217 | |
6218 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
6219 | assert(FieldIt != RD->field_end() && "missing field?"); |
6220 | if (!FieldIt->isUnnamedBitfield()) |
6221 | Success &= getDefaultInitValue( |
6222 | FieldIt->getType(), |
6223 | Result.getStructField(FieldIt->getFieldIndex())); |
6224 | } |
6225 | ++FieldIt; |
6226 | }; |
6227 | for (const auto *I : Definition->inits()) { |
6228 | LValue Subobject = This; |
6229 | LValue SubobjectParent = This; |
6230 | APValue *Value = &Result; |
6231 | |
6232 | |
6233 | FieldDecl *FD = nullptr; |
6234 | if (I->isBaseInitializer()) { |
6235 | QualType BaseType(I->getBaseClass(), 0); |
6236 | #ifndef NDEBUG |
6237 | |
6238 | |
6239 | assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
6240 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
6241 | "base class initializers not in expected order"); |
6242 | ++BaseIt; |
6243 | #endif |
6244 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
6245 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6246 | return false; |
6247 | Value = &Result.getStructBase(BasesSeen++); |
6248 | } else if ((FD = I->getMember())) { |
6249 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
6250 | return false; |
6251 | if (RD->isUnion()) { |
6252 | Result = APValue(FD); |
6253 | Value = &Result.getUnionValue(); |
6254 | } else { |
6255 | SkipToField(FD, false); |
6256 | Value = &Result.getStructField(FD->getFieldIndex()); |
6257 | } |
6258 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
6259 | |
6260 | |
6261 | auto IndirectFieldChain = IFD->chain(); |
6262 | for (auto *C : IndirectFieldChain) { |
6263 | FD = cast<FieldDecl>(C); |
6264 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
6265 | |
6266 | |
6267 | |
6268 | |
6269 | |
6270 | if (!Value->hasValue() || |
6271 | (Value->isUnion() && Value->getUnionField() != FD)) { |
6272 | if (CD->isUnion()) |
6273 | *Value = APValue(FD); |
6274 | else |
6275 | |
6276 | |
6277 | |
6278 | Success &= getDefaultInitValue(Info.Ctx.getRecordType(CD), *Value); |
6279 | } |
6280 | |
6281 | |
6282 | if (C == IndirectFieldChain.back()) |
6283 | SubobjectParent = Subobject; |
6284 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
6285 | return false; |
6286 | if (CD->isUnion()) |
6287 | Value = &Value->getUnionValue(); |
6288 | else { |
6289 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
6290 | SkipToField(FD, true); |
6291 | Value = &Value->getStructField(FD->getFieldIndex()); |
6292 | } |
6293 | } |
6294 | } else { |
6295 | llvm_unreachable("unknown base initializer kind"); |
6296 | } |
6297 | |
6298 | |
6299 | |
6300 | |
6301 | const Expr *Init = I->getInit(); |
6302 | if (Init->isValueDependent()) { |
6303 | if (!EvaluateDependentExpr(Init, Info)) |
6304 | return false; |
6305 | } else { |
6306 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
6307 | isa<CXXDefaultInitExpr>(Init)); |
6308 | FullExpressionRAII InitScope(Info); |
6309 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
6310 | (FD && FD->isBitField() && |
6311 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
6312 | |
6313 | |
6314 | if (!Info.noteFailure()) |
6315 | return false; |
6316 | Success = false; |
6317 | } |
6318 | } |
6319 | |
6320 | |
6321 | |
6322 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
6323 | EvalObj.finishedConstructingBases(); |
6324 | } |
6325 | |
6326 | |
6327 | if (!RD->isUnion()) { |
6328 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
6329 | if (!FieldIt->isUnnamedBitfield()) |
6330 | Success &= getDefaultInitValue( |
6331 | FieldIt->getType(), |
6332 | Result.getStructField(FieldIt->getFieldIndex())); |
6333 | } |
6334 | } |
6335 | |
6336 | EvalObj.finishedConstructingFields(); |
6337 | |
6338 | return Success && |
6339 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed && |
6340 | LifetimeExtendedScope.destroy(); |
6341 | } |
6342 | |
6343 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6344 | ArrayRef<const Expr*> Args, |
6345 | const CXXConstructorDecl *Definition, |
6346 | EvalInfo &Info, APValue &Result) { |
6347 | CallScopeRAII CallScope(Info); |
6348 | CallRef Call = Info.CurrentCall->createCall(Definition); |
6349 | if (!EvaluateArgs(Args, Call, Info, Definition)) |
6350 | return false; |
6351 | |
6352 | return HandleConstructorCall(E, This, Call, Definition, Info, Result) && |
6353 | CallScope.destroy(); |
6354 | } |
6355 | |
6356 | static bool HandleDestructionImpl(EvalInfo &Info, SourceLocation CallLoc, |
6357 | const LValue &This, APValue &Value, |
6358 | QualType T) { |
6359 | |
6360 | |
6361 | |
6362 | |
6363 | if (Value.isAbsent() && !T->isNullPtrType()) { |
6364 | APValue Printable; |
6365 | This.moveInto(Printable); |
6366 | Info.FFDiag(CallLoc, diag::note_constexpr_destroy_out_of_lifetime) |
6367 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
6368 | return false; |
6369 | } |
6370 | |
6371 | |
6372 | |
6373 | OpaqueValueExpr LocE(CallLoc, Info.Ctx.IntTy, VK_PRValue); |
6374 | |
6375 | |
6376 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
6377 | uint64_t Size = CAT->getSize().getZExtValue(); |
6378 | QualType ElemT = CAT->getElementType(); |
6379 | |
6380 | LValue ElemLV = This; |
6381 | ElemLV.addArray(Info, &LocE, CAT); |
6382 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
6383 | return false; |
6384 | |
6385 | |
6386 | |
6387 | |
6388 | if (Size && Size > Value.getArrayInitializedElts()) |
6389 | expandArray(Value, Value.getArraySize() - 1); |
6390 | |
6391 | for (; Size != 0; --Size) { |
6392 | APValue &Elem = Value.getArrayInitializedElt(Size - 1); |
6393 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
6394 | !HandleDestructionImpl(Info, CallLoc, ElemLV, Elem, ElemT)) |
6395 | return false; |
6396 | } |
6397 | |
6398 | |
6399 | Value = APValue(); |
6400 | return true; |
6401 | } |
6402 | |
6403 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
6404 | if (!RD) { |
6405 | if (T.isDestructedType()) { |
6406 | Info.FFDiag(CallLoc, diag::note_constexpr_unsupported_destruction) << T; |
6407 | return false; |
6408 | } |
6409 | |
6410 | Value = APValue(); |
6411 | return true; |
6412 | } |
6413 | |
6414 | if (RD->getNumVBases()) { |
6415 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6416 | return false; |
6417 | } |
6418 | |
6419 | const CXXDestructorDecl *DD = RD->getDestructor(); |
6420 | if (!DD && !RD->hasTrivialDestructor()) { |
6421 | Info.FFDiag(CallLoc); |
6422 | return false; |
6423 | } |
6424 | |
6425 | if (!DD || DD->isTrivial() || |
6426 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
6427 | |
6428 | |
6429 | |
6430 | |
6431 | |
6432 | |
6433 | |
6434 | |
6435 | |
6436 | Value = APValue(); |
6437 | return true; |
6438 | } |
6439 | |
6440 | if (!Info.CheckCallLimit(CallLoc)) |
6441 | return false; |
6442 | |
6443 | const FunctionDecl *Definition = nullptr; |
6444 | const Stmt *Body = DD->getBody(Definition); |
6445 | |
6446 | if (!CheckConstexprFunction(Info, CallLoc, DD, Definition, Body)) |
6447 | return false; |
6448 | |
6449 | CallStackFrame Frame(Info, CallLoc, Definition, &This, CallRef()); |
6450 | |
6451 | |
6452 | unsigned BasesLeft = RD->getNumBases(); |
6453 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
6454 | Info, |
6455 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
6456 | if (!EvalObj.DidInsert) { |
6457 | |
6458 | |
6459 | |
6460 | |
6461 | |
6462 | |
6463 | Info.FFDiag(CallLoc, diag::note_constexpr_double_destroy); |
6464 | return false; |
6465 | } |
6466 | |
6467 | |
6468 | |
6469 | APValue RetVal; |
6470 | StmtResult Ret = {RetVal, nullptr}; |
6471 | if (EvaluateStmt(Ret, Info, Definition->getBody()) == ESR_Failed) |
6472 | return false; |
6473 | |
6474 | |
6475 | if (RD->isUnion()) |
6476 | return true; |
6477 | |
6478 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6479 | |
6480 | |
6481 | |
6482 | SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
6483 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
6484 | if (FD->isUnnamedBitfield()) |
6485 | continue; |
6486 | |
6487 | LValue Subobject = This; |
6488 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
6489 | return false; |
6490 | |
6491 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
6492 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
6493 | FD->getType())) |
6494 | return false; |
6495 | } |
6496 | |
6497 | if (BasesLeft != 0) |
6498 | EvalObj.startedDestroyingBases(); |
6499 | |
6500 | |
6501 | for (const CXXBaseSpecifier &Base : llvm::reverse(RD->bases())) { |
6502 | --BasesLeft; |
6503 | |
6504 | QualType BaseType = Base.getType(); |
6505 | LValue Subobject = This; |
6506 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
6507 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6508 | return false; |
6509 | |
6510 | APValue *SubobjectValue = &Value.getStructBase(BasesLeft); |
6511 | if (!HandleDestructionImpl(Info, CallLoc, Subobject, *SubobjectValue, |
6512 | BaseType)) |
6513 | return false; |
6514 | } |
6515 | assert(BasesLeft == 0 && "NumBases was wrong?"); |
6516 | |
6517 | |
6518 | Value = APValue(); |
6519 | return true; |
6520 | } |
6521 | |
6522 | namespace { |
6523 | struct DestroyObjectHandler { |
6524 | EvalInfo &Info; |
6525 | const Expr *E; |
6526 | const LValue &This; |
6527 | const AccessKinds AccessKind; |
6528 | |
6529 | typedef bool result_type; |
6530 | bool failed() { return false; } |
6531 | bool found(APValue &Subobj, QualType SubobjType) { |
6532 | return HandleDestructionImpl(Info, E->getExprLoc(), This, Subobj, |
6533 | SubobjType); |
6534 | } |
6535 | bool found(APSInt &Value, QualType SubobjType) { |
6536 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6537 | return false; |
6538 | } |
6539 | bool found(APFloat &Value, QualType SubobjType) { |
6540 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6541 | return false; |
6542 | } |
6543 | }; |
6544 | } |
6545 | |
6546 | |
6547 | |
6548 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
6549 | const LValue &This, QualType ThisType) { |
6550 | CompleteObject Obj = findCompleteObject(Info, E, AK_Destroy, This, ThisType); |
6551 | DestroyObjectHandler Handler = {Info, E, This, AK_Destroy}; |
6552 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
6553 | } |
6554 | |
6555 | |
6556 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
6557 | APValue::LValueBase LVBase, APValue &Value, |
6558 | QualType T) { |
6559 | |
6560 | |
6561 | if (Info.EvalStatus.HasSideEffects) |
6562 | return false; |
6563 | |
6564 | LValue LV; |
6565 | LV.set({LVBase}); |
6566 | return HandleDestructionImpl(Info, Loc, LV, Value, T); |
6567 | } |
6568 | |
6569 | |
6570 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
6571 | LValue &Result) { |
6572 | if (Info.checkingPotentialConstantExpression() || |
6573 | Info.SpeculativeEvaluationDepth) |
6574 | return false; |
6575 | |
6576 | |
6577 | auto Caller = Info.getStdAllocatorCaller("allocate"); |
6578 | if (!Caller) { |
6579 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus20 |
6580 | ? diag::note_constexpr_new_untyped |
6581 | : diag::note_constexpr_new); |
6582 | return false; |
6583 | } |
6584 | |
6585 | QualType ElemType = Caller.ElemType; |
6586 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
6587 | Info.FFDiag(E->getExprLoc(), |
6588 | diag::note_constexpr_new_not_complete_object_type) |
6589 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
6590 | return false; |
6591 | } |
6592 | |
6593 | APSInt ByteSize; |
6594 | if (!EvaluateInteger(E->getArg(0), ByteSize, Info)) |
6595 | return false; |
6596 | bool IsNothrow = false; |
6597 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
6598 | EvaluateIgnoredValue(Info, E->getArg(I)); |
6599 | IsNothrow |= E->getType()->isNothrowT(); |
6600 | } |
6601 | |
6602 | CharUnits ElemSize; |
6603 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
6604 | return false; |
6605 | APInt Size, Remainder; |
6606 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
6607 | APInt::udivrem(ByteSize, ElemSizeAP, Size, Remainder); |
6608 | if (Remainder != 0) { |
6609 | |
6610 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
6611 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
6612 | return false; |
6613 | } |
6614 | |
6615 | if (ByteSize.getActiveBits() > ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
6616 | if (IsNothrow) { |
6617 | Result.setNull(Info.Ctx, E->getType()); |
6618 | return true; |
6619 | } |
6620 | |
6621 | Info.FFDiag(E, diag::note_constexpr_new_too_large) << APSInt(Size, true); |
6622 | return false; |
6623 | } |
6624 | |
6625 | QualType AllocType = Info.Ctx.getConstantArrayType(ElemType, Size, nullptr, |
6626 | ArrayType::Normal, 0); |
6627 | APValue *Val = Info.createHeapAlloc(E, AllocType, Result); |
6628 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
6629 | Result.addArray(Info, E, cast<ConstantArrayType>(AllocType)); |
6630 | return true; |
6631 | } |
6632 | |
6633 | static bool hasVirtualDestructor(QualType T) { |
6634 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6635 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6636 | return DD->isVirtual(); |
6637 | return false; |
6638 | } |
6639 | |
6640 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
6641 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6642 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6643 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
6644 | return nullptr; |
6645 | } |
6646 | |
6647 | |
6648 | |
6649 | |
6650 | |
6651 | |
6652 | static Optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
6653 | const LValue &Pointer, |
6654 | DynAlloc::Kind DeallocKind) { |
6655 | auto PointerAsString = [&] { |
6656 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
6657 | }; |
6658 | |
6659 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
6660 | if (!DA) { |
6661 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
6662 | << PointerAsString(); |
6663 | if (Pointer.Base) |
6664 | NoteLValueLocation(Info, Pointer.Base); |
6665 | return None; |
6666 | } |
6667 | |
6668 | Optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
6669 | if (!Alloc) { |
6670 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
6671 | return None; |
6672 | } |
6673 | |
6674 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
6675 | if (DeallocKind != (*Alloc)->getKind()) { |
6676 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
6677 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
6678 | NoteLValueLocation(Info, Pointer.Base); |
6679 | return None; |
6680 | } |
6681 | |
6682 | bool Subobject = false; |
6683 | if (DeallocKind == DynAlloc::New) { |
6684 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
6685 | Pointer.Designator.isOnePastTheEnd(); |
6686 | } else { |
6687 | Subobject = Pointer.Designator.Entries.size() != 1 || |
6688 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
6689 | } |
6690 | if (Subobject) { |
6691 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
6692 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
6693 | return None; |
6694 | } |
6695 | |
6696 | return Alloc; |
6697 | } |
6698 | |
6699 | |
6700 | bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
6701 | if (Info.checkingPotentialConstantExpression() || |
6702 | Info.SpeculativeEvaluationDepth) |
6703 | return false; |
6704 | |
6705 | |
6706 | if (!Info.getStdAllocatorCaller("deallocate")) { |
6707 | Info.FFDiag(E->getExprLoc()); |
6708 | return true; |
6709 | } |
6710 | |
6711 | LValue Pointer; |
6712 | if (!EvaluatePointer(E->getArg(0), Pointer, Info)) |
6713 | return false; |
6714 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
6715 | EvaluateIgnoredValue(Info, E->getArg(I)); |
6716 | |
6717 | if (Pointer.Designator.Invalid) |
6718 | return false; |
6719 | |
6720 | |
6721 | |
6722 | if (Pointer.isNullPointer()) { |
6723 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null); |
6724 | return true; |
6725 | } |
6726 | |
6727 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
6728 | return false; |
6729 | |
6730 | Info.HeapAllocs.erase(Pointer.Base.get<DynamicAllocLValue>()); |
6731 | return true; |
6732 | } |
6733 | |
6734 | |
6735 | |
6736 | |
6737 | namespace { |
6738 | |
6739 | class BitCastBuffer { |
6740 | |
6741 | |
6742 | |
6743 | |
6744 | |
6745 | SmallVector<Optional<unsigned char>, 32> Bytes; |
6746 | |
6747 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
6748 | "Need at least 8 bit unsigned char"); |
6749 | |
6750 | bool TargetIsLittleEndian; |
6751 | |
6752 | public: |
6753 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
6754 | : Bytes(Width.getQuantity()), |
6755 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
6756 | |
6757 | LLVM_NODISCARD |
6758 | bool readObject(CharUnits Offset, CharUnits Width, |
6759 | SmallVectorImpl<unsigned char> &Output) const { |
6760 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
6761 | |
6762 | |
6763 | if (!Bytes[I.getQuantity()]) |
6764 | return false; |
6765 | Output.push_back(*Bytes[I.getQuantity()]); |
6766 | } |
6767 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6768 | std::reverse(Output.begin(), Output.end()); |
6769 | return true; |
6770 | } |
6771 | |
6772 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
6773 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6774 | std::reverse(Input.begin(), Input.end()); |
6775 | |
6776 | size_t Index = 0; |
6777 | for (unsigned char Byte : Input) { |
6778 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?"); |
6779 | Bytes[Offset.getQuantity() + Index] = Byte; |
6780 | ++Index; |
6781 | } |
6782 | } |
6783 | |
6784 | size_t size() { return Bytes.size(); } |
6785 | }; |
6786 | |
6787 | |
6788 | |
6789 | class APValueToBufferConverter { |
6790 | EvalInfo &Info; |
6791 | BitCastBuffer Buffer; |
6792 | const CastExpr *BCE; |
6793 | |
6794 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
6795 | const CastExpr *BCE) |
6796 | : Info(Info), |
6797 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
6798 | BCE(BCE) {} |
6799 | |
6800 | bool visit(const APValue &Val, QualType Ty) { |
6801 | return visit(Val, Ty, CharUnits::fromQuantity(0)); |
6802 | } |
6803 | |
6804 | |
6805 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
6806 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
6807 | |
6808 | |
6809 | if (Ty->isNullPtrType()) |
6810 | return true; |
6811 | |
6812 | |
6813 | switch (Val.getKind()) { |
6814 | case APValue::Indeterminate: |
6815 | case APValue::None: |
6816 | return true; |
6817 | |
6818 | case APValue::Int: |
6819 | return visitInt(Val.getInt(), Ty, Offset); |
6820 | case APValue::Float: |
6821 | return visitFloat(Val.getFloat(), Ty, Offset); |
6822 | case APValue::Array: |
6823 | return visitArray(Val, Ty, Offset); |
6824 | case APValue::Struct: |
6825 | return visitRecord(Val, Ty, Offset); |
6826 | |
6827 | case APValue::ComplexInt: |
6828 | case APValue::ComplexFloat: |
6829 | case APValue::Vector: |
6830 | case APValue::FixedPoint: |
6831 | |
6832 | |
6833 | case APValue::Union: |
6834 | case APValue::MemberPointer: |
6835 | case APValue::AddrLabelDiff: { |
6836 | Info.FFDiag(BCE->getBeginLoc(), |
6837 | diag::note_constexpr_bit_cast_unsupported_type) |
6838 | << Ty; |
6839 | return false; |
6840 | } |
6841 | |
6842 | case APValue::LValue: |
6843 | llvm_unreachable("LValue subobject in bit_cast?"); |
6844 | } |
6845 | llvm_unreachable("Unhandled APValue::ValueKind"); |
6846 | } |
6847 | |
6848 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
6849 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
6850 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6851 | |
6852 | |
6853 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
6854 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
6855 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
6856 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
6857 | |
6858 | if (!visitRecord(Val.getStructBase(I), BS.getType(), |
6859 | Layout.getBaseClassOffset(BaseDecl) + Offset)) |
6860 | return false; |
6861 | } |
6862 | } |
6863 | |
6864 | |
6865 | unsigned FieldIdx = 0; |
6866 | for (FieldDecl *FD : RD->fields()) { |
6867 | if (FD->isBitField()) { |
6868 | Info.FFDiag(BCE->getBeginLoc(), |
6869 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
6870 | return false; |
6871 | } |
6872 | |
6873 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
6874 | |
6875 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
6876 | "only bit-fields can have sub-char alignment"); |
6877 | CharUnits FieldOffset = |
6878 | Info.Ctx.toCharUnitsFromBits(FieldOffsetBits) + Offset; |
6879 | QualType FieldTy = FD->getType(); |
6880 | if (!visit(Val.getStructField(FieldIdx), FieldTy, FieldOffset)) |
6881 | return false; |
6882 | ++FieldIdx; |
6883 | } |
6884 | |
6885 | return true; |
6886 | } |
6887 | |
6888 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
6889 | const auto *CAT = |
6890 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
6891 | if (!CAT) |
6892 | return false; |
6893 | |
6894 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
6895 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
6896 | unsigned ArraySize = Val.getArraySize(); |
6897 | |
6898 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
6899 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
6900 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
6901 | return false; |
6902 | } |
6903 | |
6904 | |
6905 | if (Val.hasArrayFiller()) { |
6906 | const APValue &Filler = Val.getArrayFiller(); |
6907 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
6908 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
6909 | return false; |
6910 | } |
6911 | } |
6912 | |
6913 | return true; |
6914 | } |
6915 | |
6916 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
6917 | APSInt AdjustedVal = Val; |
6918 | unsigned Width = AdjustedVal.getBitWidth(); |
6919 | if (Ty->isBooleanType()) { |
6920 | Width = Info.Ctx.getTypeSize(Ty); |
6921 | AdjustedVal = AdjustedVal.extend(Width); |
6922 | } |
6923 | |
6924 | SmallVector<unsigned char, 8> Bytes(Width / 8); |
6925 | llvm::StoreIntToMemory(AdjustedVal, &*Bytes.begin(), Width / 8); |
6926 | Buffer.writeObject(Offset, Bytes); |
6927 | return true; |
6928 | } |
6929 | |
6930 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
6931 | APSInt AsInt(Val.bitcastToAPInt()); |
6932 | return visitInt(AsInt, Ty, Offset); |
6933 | } |
6934 | |
6935 | public: |
6936 | static Optional<BitCastBuffer> convert(EvalInfo &Info, const APValue &Src, |
6937 | const CastExpr *BCE) { |
6938 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
6939 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
6940 | if (!Converter.visit(Src, BCE->getSubExpr()->getType())) |
6941 | return None; |
6942 | return Converter.Buffer; |
6943 | } |
6944 | }; |
6945 | |
6946 | |
6947 | class BufferToAPValueConverter { |
6948 | EvalInfo &Info; |
6949 | const BitCastBuffer &Buffer; |
6950 | const CastExpr *BCE; |
6951 | |
6952 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
6953 | const CastExpr *BCE) |
6954 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
6955 | |
6956 | |
6957 | |
6958 | |
6959 | llvm::NoneType unsupportedType(QualType Ty) { |
6960 | Info.FFDiag(BCE->getBeginLoc(), |
6961 | diag::note_constexpr_bit_cast_unsupported_type) |
6962 | << Ty; |
6963 | return None; |
6964 | } |
6965 | |
6966 | llvm::NoneType unrepresentableValue(QualType Ty, const APSInt &Val) { |
6967 | Info.FFDiag(BCE->getBeginLoc(), |
6968 | diag::note_constexpr_bit_cast_unrepresentable_value) |
6969 | << Ty << toString(Val, 10); |
6970 | return None; |
6971 | } |
6972 | |
6973 | Optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
6974 | const EnumType *EnumSugar = nullptr) { |
6975 | if (T->isNullPtrType()) { |
6976 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QualType(T, 0)); |
6977 | return APValue((Expr *)nullptr, |
6978 | CharUnits::fromQuantity(NullValue), |
6979 | APValue::NoLValuePath{}, true); |
6980 | } |
6981 | |
6982 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
6983 | |
6984 | |
6985 | |
6986 | |
6987 | if (T->isRealFloatingType()) { |
6988 | const llvm::fltSemantics &Semantics = |
6989 | Info.Ctx.getFloatTypeSemantics(QualType(T, 0)); |
6990 | unsigned NumBits = llvm::APFloatBase::getSizeInBits(Semantics); |
6991 | assert(NumBits % 8 == 0); |
6992 | CharUnits NumBytes = CharUnits::fromQuantity(NumBits / 8); |
6993 | if (NumBytes != SizeOf) |
6994 | SizeOf = NumBytes; |
6995 | } |
6996 | |
6997 | SmallVector<uint8_t, 8> Bytes; |
6998 | if (!Buffer.readObject(Offset, SizeOf, Bytes)) { |
6999 | |
7000 | |
7001 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
7002 | bool IsUChar = |
7003 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
7004 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
7005 | if (!IsStdByte && !IsUChar) { |
7006 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
7007 | Info.FFDiag(BCE->getExprLoc(), |
7008 | diag::note_constexpr_bit_cast_indet_dest) |
7009 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
7010 | return None; |
7011 | } |
7012 | |
7013 | return APValue::IndeterminateValue(); |
7014 | } |
7015 | |
7016 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
7017 | llvm::LoadIntFromMemory(Val, &*Bytes.begin(), Bytes.size()); |
7018 | |
7019 | if (T->isIntegralOrEnumerationType()) { |
7020 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
7021 | |
7022 | unsigned IntWidth = Info.Ctx.getIntWidth(QualType(T, 0)); |
7023 | if (IntWidth != Val.getBitWidth()) { |
7024 | APSInt Truncated = Val.trunc(IntWidth); |
7025 | if (Truncated.extend(Val.getBitWidth()) != Val) |
7026 | return unrepresentableValue(QualType(T, 0), Val); |
7027 | Val = Truncated; |
7028 | } |
7029 | |
7030 | return APValue(Val); |
7031 | } |
7032 | |
7033 | if (T->isRealFloatingType()) { |
7034 | const llvm::fltSemantics &Semantics = |
7035 | Info.Ctx.getFloatTypeSemantics(QualType(T, 0)); |
7036 | return APValue(APFloat(Semantics, Val)); |
7037 | } |
7038 | |
7039 | return unsupportedType(QualType(T, 0)); |
7040 | } |
7041 | |
7042 | Optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
7043 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
7044 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
7045 | |
7046 | unsigned NumBases = 0; |
7047 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
7048 | NumBases = CXXRD->getNumBases(); |
7049 | |
7050 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
7051 | std::distance(RD->field_begin(), RD->field_end())); |
7052 | |
7053 | |
7054 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
7055 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7056 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7057 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7058 | if (BaseDecl->isEmpty() || |
7059 | Info.Ctx.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()) |
7060 | continue; |
7061 | |
7062 | Optional<APValue> SubObj = visitType( |
7063 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
7064 | if (!SubObj) |
7065 | return None; |
7066 | ResultVal.getStructBase(I) = *SubObj; |
7067 | } |
7068 | } |
7069 | |
7070 | |
7071 | unsigned FieldIdx = 0; |
7072 | for (FieldDecl *FD : RD->fields()) { |
7073 | |
7074 | |
7075 | if (FD->isBitField()) { |
7076 | Info.FFDiag(BCE->getBeginLoc(), |
7077 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7078 | return None; |
7079 | } |
7080 | |
7081 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
7082 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
7083 | |
7084 | CharUnits FieldOffset = |
7085 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
7086 | Offset; |
7087 | QualType FieldTy = FD->getType(); |
7088 | Optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
7089 | if (!SubObj) |
7090 | return None; |
7091 | ResultVal.getStructField(FieldIdx) = *SubObj; |
7092 | ++FieldIdx; |
7093 | } |
7094 | |
7095 | return ResultVal; |
7096 | } |
7097 | |
7098 | Optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
7099 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
7100 | assert(!RepresentationType.isNull() && |
7101 | "enum forward decl should be caught by Sema"); |
7102 | const auto *AsBuiltin = |
7103 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
7104 | |
7105 | |
7106 | return visit(AsBuiltin, Offset, Ty); |
7107 | } |
7108 | |
7109 | Optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
7110 | size_t Size = Ty->getSize().getLimitedValue(); |
7111 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
7112 | |
7113 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
7114 | for (size_t I = 0; I != Size; ++I) { |
7115 | Optional<APValue> ElementValue = |
7116 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7117 | if (!ElementValue) |
7118 | return None; |
7119 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
7120 | } |
7121 | |
7122 | return ArrayValue; |
7123 | } |
7124 | |
7125 | Optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
7126 | return unsupportedType(QualType(Ty, 0)); |
7127 | } |
7128 | |
7129 | Optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
7130 | QualType Can = Ty.getCanonicalType(); |
7131 | |
7132 | switch (Can->getTypeClass()) { |
7133 | #define TYPE(Class, Base) \ |
7134 | case Type::Class: \ |
7135 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
7136 | #define ABSTRACT_TYPE(Class, Base) |
7137 | #define NON_CANONICAL_TYPE(Class, Base) \ |
7138 | case Type::Class: \ |
7139 | llvm_unreachable("non-canonical type should be impossible!"); |
7140 | #define DEPENDENT_TYPE(Class, Base) \ |
7141 | case Type::Class: \ |
7142 | llvm_unreachable( \ |
7143 | "dependent types aren't supported in the constant evaluator!"); |
7144 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
7145 | case Type::Class: \ |
7146 | llvm_unreachable("either dependent or not canonical!"); |
7147 | #include "clang/AST/TypeNodes.inc" |
7148 | } |
7149 | llvm_unreachable("Unhandled Type::TypeClass"); |
7150 | } |
7151 | |
7152 | public: |
7153 | |
7154 | static Optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
7155 | const CastExpr *BCE) { |
7156 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
7157 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(0)); |
7158 | } |
7159 | }; |
7160 | |
7161 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
7162 | QualType Ty, EvalInfo *Info, |
7163 | const ASTContext &Ctx, |
7164 | bool CheckingDest) { |
7165 | Ty = Ty.getCanonicalType(); |
7166 | |
7167 | auto diag = [&](int Reason) { |
7168 | if (Info) |
7169 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
7170 | << CheckingDest << (Reason == 4) << Reason; |
7171 | return false; |
7172 | }; |
7173 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
7174 | if (Info) |
7175 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
7176 | << NoteTy << Construct << Ty; |
7177 | return false; |
7178 | }; |
7179 | |
7180 | if (Ty->isUnionType()) |
7181 | return diag(0); |
7182 | if (Ty->isPointerType()) |
7183 | return diag(1); |
7184 | if (Ty->isMemberPointerType()) |
7185 | return diag(2); |
7186 | if (Ty.isVolatileQualified()) |
7187 | return diag(3); |
7188 | |
7189 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
7190 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
7191 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
7192 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
7193 | CheckingDest)) |
7194 | return note(1, BS.getType(), BS.getBeginLoc()); |
7195 | } |
7196 | for (FieldDecl *FD : Record->fields()) { |
7197 | if (FD->getType()->isReferenceType()) |
7198 | return diag(4); |
7199 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
7200 | CheckingDest)) |
7201 | return note(0, FD->getType(), FD->getBeginLoc()); |
7202 | } |
7203 | } |
7204 | |
7205 | if (Ty->isArrayType() && |
7206 | !checkBitCastConstexprEligibilityType(Loc, Ctx.getBaseElementType(Ty), |
7207 | Info, Ctx, CheckingDest)) |
7208 | return false; |
7209 | |
7210 | return true; |
7211 | } |
7212 | |
7213 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
7214 | const ASTContext &Ctx, |
7215 | const CastExpr *BCE) { |
7216 | bool DestOK = checkBitCastConstexprEligibilityType( |
7217 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
7218 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
7219 | BCE->getBeginLoc(), |
7220 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
7221 | return SourceOK; |
7222 | } |
7223 | |
7224 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7225 | APValue &SourceValue, |
7226 | const CastExpr *BCE) { |
7227 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7228 | "no host or target supports non 8-bit chars"); |
7229 | assert(SourceValue.isLValue() && |
7230 | "LValueToRValueBitcast requires an lvalue operand!"); |
7231 | |
7232 | if (!checkBitCastConstexprEligibility(&Info, Info.Ctx, BCE)) |
7233 | return false; |
7234 | |
7235 | LValue SourceLValue; |
7236 | APValue SourceRValue; |
7237 | SourceLValue.setFrom(Info.Ctx, SourceValue); |
7238 | if (!handleLValueToRValueConversion( |
7239 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
7240 | SourceRValue, true)) |
7241 | return false; |
7242 | |
7243 | |
7244 | Optional<BitCastBuffer> Buffer = |
7245 | APValueToBufferConverter::convert(Info, SourceRValue, BCE); |
7246 | if (!Buffer) |
7247 | return false; |
7248 | |
7249 | |
7250 | Optional<APValue> MaybeDestValue = |
7251 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
7252 | if (!MaybeDestValue) |
7253 | return false; |
7254 | |
7255 | DestValue = std::move(*MaybeDestValue); |
7256 | return true; |
7257 | } |
7258 | |
7259 | template <class Derived> |
7260 | class ExprEvaluatorBase |
7261 | : public ConstStmtVisitor<Derived, bool> { |
7262 | private: |
7263 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
7264 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
7265 | return getDerived().Success(V, E); |
7266 | } |
7267 | bool DerivedZeroInitialization(const Expr *E) { |
7268 | return getDerived().ZeroInitialization(E); |
7269 | } |
7270 | |
7271 | |
7272 | |
7273 | |
7274 | template<typename ConditionalOperator> |
7275 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
7276 | assert(Info.checkingPotentialConstantExpression()); |
7277 | |
7278 | |
7279 | SmallVector<PartialDiagnosticAt, 8> Diag; |
7280 | { |
7281 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7282 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7283 | if (Diag.empty()) |
7284 | return; |
7285 | } |
7286 | |
7287 | { |
7288 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7289 | Diag.clear(); |
7290 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7291 | if (Diag.empty()) |
7292 | return; |
7293 | } |
7294 | |
7295 | Error(E, diag::note_constexpr_conditional_never_const); |
7296 | } |
7297 | |
7298 | |
7299 | template<typename ConditionalOperator> |
7300 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
7301 | bool BoolResult; |
7302 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
7303 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
7304 | CheckPotentialConstantConditional(E); |
7305 | return false; |
7306 | } |
7307 | if (Info.noteFailure()) { |
7308 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7309 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7310 | } |
7311 | return false; |
7312 | } |
7313 | |
7314 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
7315 | return StmtVisitorTy::Visit(EvalExpr); |
7316 | } |
7317 | |
7318 | protected: |
7319 | EvalInfo &Info; |
7320 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
7321 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
7322 | |
7323 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
7324 | return Info.CCEDiag(E, D); |
7325 | } |
7326 | |
7327 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
7328 | |
7329 | public: |
7330 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
7331 | |
7332 | EvalInfo &getEvalInfo() { return Info; } |
7333 | |
7334 | |
7335 | |
7336 | bool Error(const Expr *E, diag::kind D) { |
7337 | Info.FFDiag(E, D); |
7338 | return false; |
7339 | } |
7340 | bool Error(const Expr *E) { |
7341 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
7342 | } |
7343 | |
7344 | bool VisitStmt(const Stmt *) { |
7345 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
7346 | } |
7347 | bool VisitExpr(const Expr *E) { |
7348 | return Error(E); |
7349 | } |
7350 | |
7351 | bool VisitConstantExpr(const ConstantExpr *E) { |
7352 | if (E->hasAPValueResult()) |
7353 | return DerivedSuccess(E->getAPValueResult(), E); |
7354 | |
7355 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7356 | } |
7357 | |
7358 | bool VisitParenExpr(const ParenExpr *E) |
7359 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7360 | bool VisitUnaryExtension(const UnaryOperator *E) |
7361 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7362 | bool VisitUnaryPlus(const UnaryOperator *E) |
7363 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7364 | bool VisitChooseExpr(const ChooseExpr *E) |
7365 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
7366 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
7367 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
7368 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
7369 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
7370 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
7371 | TempVersionRAII RAII(*Info.CurrentCall); |
7372 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7373 | return StmtVisitorTy::Visit(E->getExpr()); |
7374 | } |
7375 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
7376 | TempVersionRAII RAII(*Info.CurrentCall); |
7377 | |
7378 | if (!E->getExpr()) |
7379 | return Error(E); |
7380 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7381 | return StmtVisitorTy::Visit(E->getExpr()); |
7382 | } |
7383 | |
7384 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
7385 | FullExpressionRAII Scope(Info); |
7386 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
7387 | } |
7388 | |
7389 | |
7390 | |
7391 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
7392 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7393 | } |
7394 | |
7395 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
7396 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
7397 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7398 | } |
7399 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
7400 | if (!Info.Ctx.getLangOpts().CPlusPlus20) |
7401 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
7402 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7403 | } |
7404 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
7405 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7406 | } |
7407 | |
7408 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7409 | switch (E->getOpcode()) { |
7410 | default: |
7411 | return Error(E); |
7412 | |
7413 | case BO_Comma: |
7414 | VisitIgnoredValue(E->getLHS()); |
7415 | return StmtVisitorTy::Visit(E->getRHS()); |
7416 | |
7417 | case BO_PtrMemD: |
7418 | case BO_PtrMemI: { |
7419 | LValue Obj; |
7420 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
7421 | return false; |
7422 | APValue Result; |
7423 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
7424 | return false; |
7425 | return DerivedSuccess(Result, E); |
7426 | } |
7427 | } |
7428 | } |
7429 | |
7430 | bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) { |
7431 | return StmtVisitorTy::Visit(E->getSemanticForm()); |
7432 | } |
7433 | |
7434 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
7435 | |
7436 | |
7437 | LValue CommonLV; |
7438 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7439 | E->getOpaqueValue(), |
7440 | getStorageType(Info.Ctx, E->getOpaqueValue()), |
7441 | ScopeKind::FullExpression, CommonLV), |
7442 | Info, E->getCommon())) |
7443 | return false; |
7444 | |
7445 | return HandleConditionalOperator(E); |
7446 | } |
7447 | |
7448 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
7449 | bool IsBcpCall = false; |
7450 | |
7451 | |
7452 | |
7453 | |
7454 | if (const CallExpr *CallCE = |
7455 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
7456 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
7457 | IsBcpCall = true; |
7458 | |
7459 | |
7460 | |
7461 | |
7462 | |
7463 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
7464 | return false; |
7465 | |
7466 | FoldConstant Fold(Info, IsBcpCall); |
7467 | if (!HandleConditionalOperator(E)) { |
7468 | Fold.keepDiagnostics(); |
7469 | return false; |
7470 | } |
7471 | |
7472 | return true; |
7473 | } |
7474 | |
7475 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
7476 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
7477 | return DerivedSuccess(*Value, E); |
7478 | |
7479 | const Expr *Source = E->getSourceExpr(); |
7480 | if (!Source) |
7481 | return Error(E); |
7482 | if (Source == E) { |
7483 | assert(0 && "OpaqueValueExpr recursively refers to itself"); |
7484 | return Error(E); |
7485 | } |
7486 | return StmtVisitorTy::Visit(Source); |
7487 | } |
7488 | |
7489 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
7490 | for (const Expr *SemE : E->semantics()) { |
7491 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { |
7492 | |
7493 | |
7494 | |
7495 | if (SemE == E->getResultExpr()) |
7496 | return Error(E); |
7497 | |
7498 | |
7499 | |
7500 | if (OVE->isUnique()) |
7501 | continue; |
7502 | |
7503 | LValue LV; |
7504 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7505 | OVE, getStorageType(Info.Ctx, OVE), |
7506 | ScopeKind::FullExpression, LV), |
7507 | Info, OVE->getSourceExpr())) |
7508 | return false; |
7509 | } else if (SemE == E->getResultExpr()) { |
7510 | if (!StmtVisitorTy::Visit(SemE)) |
7511 | return false; |
7512 | } else { |
7513 | if (!EvaluateIgnoredValue(Info, SemE)) |
7514 | return false; |
7515 | } |
7516 | } |
7517 | return true; |
7518 | } |
7519 | |
7520 | bool VisitCallExpr(const CallExpr *E) { |
7521 | APValue Result; |
7522 | if (!handleCallExpr(E, Result, nullptr)) |
7523 | return false; |
7524 | return DerivedSuccess(Result, E); |
7525 | } |
7526 | |
7527 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
7528 | const LValue *ResultSlot) { |
7529 | CallScopeRAII CallScope(Info); |
7530 | |
7531 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
7532 | QualType CalleeType = Callee->getType(); |
7533 | |
7534 | const FunctionDecl *FD = nullptr; |
7535 | LValue *This = nullptr, ThisVal; |
| 2 | | 'This' initialized to a null pointer value | |
|
7536 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
7537 | bool HasQualifier = false; |
7538 | |
7539 | CallRef Call; |
| 3 | | Calling default constructor for 'CallRef' | |
|
| 5 | | Returning from default constructor for 'CallRef' | |
|
7540 | |
7541 | |
7542 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
| 6 | | Calling 'Type::isSpecificBuiltinType' | |
|
| 10 | | Returning from 'Type::isSpecificBuiltinType' | |
|
| |
7543 | const CXXMethodDecl *Member = nullptr; |
7544 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
7545 | |
7546 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
7547 | return false; |
7548 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
7549 | if (!Member) |
7550 | return Error(Callee); |
7551 | This = &ThisVal; |
7552 | HasQualifier = ME->hasQualifier(); |
7553 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
7554 | |
7555 | const ValueDecl *D = |
7556 | HandleMemberPointerAccess(Info, BE, ThisVal, false); |
7557 | if (!D) |
7558 | return false; |
7559 | Member = dyn_cast<CXXMethodDecl>(D); |
7560 | if (!Member) |
7561 | return Error(Callee); |
7562 | This = &ThisVal; |
7563 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
7564 | if (!Info.getLangOpts().CPlusPlus20) |
7565 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
7566 | return EvaluateObjectArgument(Info, PDE->getBase(), ThisVal) && |
7567 | HandleDestruction(Info, PDE, ThisVal, PDE->getDestroyedType()); |
7568 | } else |
7569 | return Error(Callee); |
7570 | FD = Member; |
7571 | } else if (CalleeType->isFunctionPointerType()) { |
| 12 | | Calling 'Type::isFunctionPointerType' | |
|
| 20 | | Returning from 'Type::isFunctionPointerType' | |
|
| |
7572 | LValue CalleeLV; |
7573 | if (!EvaluatePointer(Callee, CalleeLV, Info)) |
| 22 | | Assuming the condition is false | |
|
| |
7574 | return false; |
7575 | |
7576 | if (!CalleeLV.getLValueOffset().isZero()) |
| 24 | | Calling 'CharUnits::isZero' | |
|
| 27 | | Returning from 'CharUnits::isZero' | |
|
| |
7577 | return Error(Callee); |
7578 | FD = dyn_cast_or_null<FunctionDecl>( |
| 29 | | Assuming the object is a 'FunctionDecl' | |
|
7579 | CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>()); |
7580 | if (!FD) |
| |
7581 | return Error(Callee); |
7582 | |
7583 | |
7584 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
| 31 | | Assuming the condition is false | |
|
| |
7585 | CalleeType->getPointeeType(), FD->getType())) { |
7586 | return Error(E); |
7587 | } |
7588 | |
7589 | |
7590 | |
7591 | auto *OCE = dyn_cast<CXXOperatorCallExpr>(E); |
| 33 | | Assuming 'E' is not a 'CXXOperatorCallExpr' | |
|
7592 | if (OCE && OCE->isAssignmentOp()) { |
7593 | assert(Args.size() == 2 && "wrong number of arguments in assignment"); |
7594 | Call = Info.CurrentCall->createCall(FD); |
7595 | if (!EvaluateArgs(isa<CXXMethodDecl>(FD) ? Args.slice(1) : Args, Call, |
7596 | Info, FD, true)) |
7597 | return false; |
7598 | } |
7599 | |
7600 | |
7601 | |
7602 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 34 | | Assuming 'FD' is not a 'CXXMethodDecl' | |
|
7603 | if (MD && !MD->isStatic()) { |
7604 | |
7605 | |
7606 | |
7607 | if (Args.empty()) |
7608 | return Error(E); |
7609 | |
7610 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
7611 | return false; |
7612 | This = &ThisVal; |
7613 | Args = Args.slice(1); |
7614 | } else if (MD && MD->isLambdaStaticInvoker()) { |
7615 | |
7616 | |
7617 | |
7618 | |
7619 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
7620 | assert( |
7621 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
7622 | "Number of captures must be zero for conversion to function-ptr"); |
7623 | |
7624 | const CXXMethodDecl *LambdaCallOp = |
7625 | ClosureClass->getLambdaCallOperator(); |
7626 | |
7627 | |
7628 | |
7629 | |
7630 | |
7631 | if (ClosureClass->isGenericLambda()) { |
7632 | assert(MD->isFunctionTemplateSpecialization() && |
7633 | "A generic lambda's static-invoker function must be a " |
7634 | "template specialization"); |
7635 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
7636 | FunctionTemplateDecl *CallOpTemplate = |
7637 | LambdaCallOp->getDescribedFunctionTemplate(); |
7638 | void *InsertPos = nullptr; |
7639 | FunctionDecl *CorrespondingCallOpSpecialization = |
7640 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
7641 | assert(CorrespondingCallOpSpecialization && |
7642 | "We must always have a function call operator specialization " |
7643 | "that corresponds to our static invoker specialization"); |
7644 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
7645 | } else |
7646 | FD = LambdaCallOp; |
7647 | } else if (FD->isReplaceableGlobalAllocationFunction()) { |
| 35 | | Assuming the condition is false | |
|
| |
7648 | if (FD->getDeclName().getCXXOverloadedOperator() == OO_New || |
7649 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New) { |
7650 | LValue Ptr; |
7651 | if (!HandleOperatorNewCall(Info, E, Ptr)) |
7652 | return false; |
7653 | Ptr.moveInto(Result); |
7654 | return CallScope.destroy(); |
7655 | } else { |
7656 | return HandleOperatorDeleteCall(Info, E) && CallScope.destroy(); |
7657 | } |
7658 | } |
7659 | } else |
7660 | return Error(E); |
7661 | |
7662 | |
7663 | if (!Call) { |
| 37 | | Calling 'CallRef::operator bool' | |
|
| 39 | | Returning from 'CallRef::operator bool' | |
|
| |
7664 | Call = Info.CurrentCall->createCall(FD); |
7665 | if (!EvaluateArgs(Args, Call, Info, FD)) |
| 41 | | Assuming the condition is false | |
|
| |
7666 | return false; |
7667 | } |
7668 | |
7669 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
7670 | if (This) { |
| |
7671 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
7672 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
7673 | |
7674 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
7675 | CovariantAdjustmentPath); |
7676 | if (!FD) |
7677 | return false; |
7678 | } else { |
7679 | |
7680 | |
7681 | |
7682 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
7683 | return false; |
7684 | } |
7685 | } |
7686 | |
7687 | |
7688 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
| 44 | | Assuming 'FD' is a 'CXXDestructorDecl' | |
|
| |
7689 | assert(This && "no 'this' pointer for destructor call"); |
7690 | return HandleDestruction(Info, E, *This, |
| 46 | | Forming reference to null pointer |
|
7691 | Info.Ctx.getRecordType(DD->getParent())) && |
7692 | CallScope.destroy(); |
7693 | } |
7694 | |
7695 | const FunctionDecl *Definition = nullptr; |
7696 | Stmt *Body = FD->getBody(Definition); |
7697 | |
7698 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
7699 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Call, |
7700 | Body, Info, Result, ResultSlot)) |
7701 | return false; |
7702 | |
7703 | if (!CovariantAdjustmentPath.empty() && |
7704 | !HandleCovariantReturnAdjustment(Info, E, Result, |
7705 | CovariantAdjustmentPath)) |
7706 | return false; |
7707 | |
7708 | return CallScope.destroy(); |
7709 | } |
7710 | |
7711 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
7712 | return StmtVisitorTy::Visit(E->getInitializer()); |
7713 | } |
7714 | bool VisitInitListExpr(const InitListExpr *E) { |
7715 | if (E->getNumInits() == 0) |
7716 | return DerivedZeroInitialization(E); |
7717 | if (E->getNumInits() == 1) |
7718 | return StmtVisitorTy::Visit(E->getInit(0)); |
7719 | return Error(E); |
7720 | } |
7721 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
7722 | return DerivedZeroInitialization(E); |
7723 | } |
7724 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
7725 | return DerivedZeroInitialization(E); |
7726 | } |
7727 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
7728 | return DerivedZeroInitialization(E); |
7729 | } |
7730 | |
7731 | |
7732 | bool VisitMemberExpr(const MemberExpr *E) { |
7733 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
7734 | "missing temporary materialization conversion"); |
7735 | assert(!E->isArrow() && "missing call to bound member function?"); |
7736 | |
7737 | APValue Val; |
7738 | if (!Evaluate(Val, Info, E->getBase())) |
7739 | return false; |
7740 | |
7741 | QualType BaseTy = E->getBase()->getType(); |
7742 | |
7743 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
7744 | if (!FD) return Error(E); |
7745 | assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
7746 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
7747 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
7748 | |
7749 | |
7750 | |
7751 | |
7752 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
7753 | SubobjectDesignator Designator(BaseTy); |
7754 | Designator.addDeclUnchecked(FD); |
7755 | |
7756 | APValue Result; |
7757 | return extractSubobject(Info, E, Obj, Designator, Result) && |
7758 | DerivedSuccess(Result, E); |
7759 | } |
7760 | |
7761 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
7762 | APValue Val; |
7763 | if (!Evaluate(Val, Info, E->getBase())) |
7764 | return false; |
7765 | |
7766 | if (Val.isVector()) { |
7767 | SmallVector<uint32_t, 4> Indices; |
7768 | E->getEncodedElementAccess(Indices); |
7769 | if (Indices.size() == 1) { |
7770 | |
7771 | return DerivedSuccess(Val.getVectorElt(Indices[0]), E); |
7772 | } else { |
7773 | |
7774 | SmallVector<APValue, 4> Elts; |
7775 | for (unsigned I = 0; I < Indices.size(); ++I) { |
7776 | Elts.push_back(Val.getVectorElt(Indices[I])); |
7777 | } |
7778 | APValue VecResult(Elts.data(), Indices.size()); |
7779 | return DerivedSuccess(VecResult, E); |
7780 | } |
7781 | } |
7782 | |
7783 | return false; |
7784 | } |
7785 | |
7786 | bool VisitCastExpr(const CastExpr *E) { |
7787 | switch (E->getCastKind()) { |
7788 | default: |
7789 | break; |
7790 | |
7791 | case CK_AtomicToNonAtomic: { |
7792 | APValue AtomicVal; |
7793 | |
7794 | |
7795 | |
7796 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
7797 | return false; |
7798 | return DerivedSuccess(AtomicVal, E); |
7799 | } |
7800 | |
7801 | case CK_NoOp: |
7802 | case CK_UserDefinedConversion: |
7803 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7804 | |
7805 | case CK_LValueToRValue: { |
7806 | LValue LVal; |
7807 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
7808 | return false; |
7809 | APValue RVal; |
7810 | |
7811 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
7812 | LVal, RVal)) |
7813 | return false; |
7814 | return DerivedSuccess(RVal, E); |
7815 | } |
7816 | case CK_LValueToRValueBitCast: { |
7817 | APValue DestValue, SourceValue; |
7818 | if (!Evaluate(SourceValue, Info, E->getSubExpr())) |
7819 | return false; |
7820 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, E)) |
7821 | return false; |
7822 | return DerivedSuccess(DestValue, E); |
7823 | } |
7824 | |
7825 | case CK_AddressSpaceConversion: { |
7826 | APValue Value; |
7827 | if (!Evaluate(Value, Info, E->getSubExpr())) |
7828 | return false; |
7829 | return DerivedSuccess(Value, E); |
7830 | } |
7831 | } |
7832 | |
7833 | return Error(E); |
7834 | } |
7835 | |
7836 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
7837 | return VisitUnaryPostIncDec(UO); |
7838 | } |
7839 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
7840 | return VisitUnaryPostIncDec(UO); |
7841 | } |
7842 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
7843 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
7844 | return Error(UO); |
7845 | |
7846 | LValue LVal; |
7847 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
7848 | return false; |
7849 | APValue RVal; |
7850 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
7851 | UO->isIncrementOp(), &RVal)) |
7852 | return false; |
7853 | return DerivedSuccess(RVal, UO); |
7854 | } |
7855 | |
7856 | bool VisitStmtExpr(const StmtExpr *E) { |
7857 | |
7858 | |
7859 | llvm::SaveAndRestore<bool> NotCheckingForUB( |
7860 | Info.CheckingForUndefinedBehavior, false); |
7861 | |
7862 | const CompoundStmt *CS = E->getSubStmt(); |
7863 | if (CS->body_empty()) |
7864 | return true; |
7865 | |
7866 | BlockScopeRAII Scope(Info); |
7867 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
7868 | BE = CS->body_end(); |
7869 | ; ++BI) { |
7870 | if (BI + 1 == BE) { |
7871 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
7872 | if (!FinalExpr) { |
7873 | Info.FFDiag((*BI)->getBeginLoc(), |
7874 | diag::note_constexpr_stmt_expr_unsupported); |
7875 | return false; |
7876 | } |
7877 | return this->Visit(FinalExpr) && Scope.destroy(); |
7878 | } |
7879 | |
7880 | APValue ReturnValue; |
7881 | StmtResult Result = { ReturnValue, nullptr }; |
7882 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
7883 | if (ESR != ESR_Succeeded) { |
7884 | |
7885 | |
7886 | |
7887 | if (ESR != ESR_Failed) |
7888 | Info.FFDiag((*BI)->getBeginLoc(), |
7889 | diag::note_constexpr_stmt_expr_unsupported); |
7890 | return false; |
7891 | } |
7892 | } |
7893 | |
7894 | llvm_unreachable("Return from function from the loop above."); |
7895 | } |
7896 | |
7897 | |
7898 | void VisitIgnoredValue(const Expr *E) { |
7899 | EvaluateIgnoredValue(Info, E); |
7900 | } |
7901 | |
7902 | |
7903 | void VisitIgnoredBaseExpression(const Expr *E) { |
7904 | |
7905 | |
7906 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
7907 | return; |
7908 | VisitIgnoredValue(E); |
7909 | } |
7910 | }; |
7911 | |
7912 | } |
7913 | |
7914 | |
7915 | |
7916 | |
7917 | namespace { |
7918 | template<class Derived> |
7919 | class LValueExprEvaluatorBase |
7920 | : public ExprEvaluatorBase<Derived> { |
7921 | protected: |
7922 | LValue &Result; |
7923 | bool InvalidBaseOK; |
7924 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
7925 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
7926 | |
7927 | bool Success(APValue::LValueBase B) { |
7928 | Result.set(B); |
7929 | return true; |
7930 | } |
7931 | |
7932 | bool evaluatePointer(const Expr *E, LValue &Result) { |
7933 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
7934 | } |
7935 | |
7936 | public: |
7937 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
7938 | : ExprEvaluatorBaseTy(Info), Result(Result), |
7939 | InvalidBaseOK(InvalidBaseOK) {} |
7940 | |
7941 | bool Success(const APValue &V, const Expr *E) { |
7942 | Result.setFrom(this->Info.Ctx, V); |
7943 | return true; |
7944 | } |
7945 | |
7946 | bool VisitMemberExpr(const MemberExpr *E) { |
7947 | |
7948 | QualType BaseTy; |
7949 | bool EvalOK; |
7950 | if (E->isArrow()) { |
7951 | EvalOK = evaluatePointer(E->getBase(), Result); |
7952 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
7953 | } else if (E->getBase()->isPRValue()) { |
7954 | assert(E->getBase()->getType()->isRecordType()); |
7955 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
7956 | BaseTy = E->getBase()->getType(); |
7957 | } else { |
7958 | EvalOK = this->Visit(E->getBase()); |
7959 | BaseTy = E->getBase()->getType(); |
7960 | } |
7961 | if (!EvalOK) { |
7962 | if (!InvalidBaseOK) |
7963 | return false; |
7964 | Result.setInvalid(E); |
7965 | return true; |
7966 | } |
7967 | |
7968 | const ValueDecl *MD = E->getMemberDecl(); |
7969 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
7970 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
7971 | FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
7972 | (void)BaseTy; |
7973 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
7974 | return false; |
7975 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
7976 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
7977 | return false; |
7978 | } else |
7979 | return this->Error(E); |
7980 | |
7981 | if (MD->getType()->isReferenceType()) { |
7982 | APValue RefValue; |
7983 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
7984 | RefValue)) |
7985 | return false; |
7986 | return Success(RefValue, E); |
7987 | } |
7988 | return true; |
7989 | } |
7990 | |
7991 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7992 | switch (E->getOpcode()) { |
7993 | default: |
7994 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
7995 | |
7996 | case BO_PtrMemD: |
7997 | case BO_PtrMemI: |
7998 | return HandleMemberPointerAccess(this->Info, E, Result); |
7999 | } |
8000 | } |
8001 | |
8002 | bool VisitCastExpr(const CastExpr *E) { |
8003 | switch (E->getCastKind()) { |
8004 | default: |
8005 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8006 | |
8007 | case CK_DerivedToBase: |
8008 | case CK_UncheckedDerivedToBase: |
8009 | if (!this->Visit(E->getSubExpr())) |
8010 | return false; |
8011 | |
8012 | |
8013 | |
8014 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
8015 | Result); |
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 | |
8053 | |
8054 | namespace { |
8055 | class LValueExprEvaluator |
8056 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
8057 | public: |
8058 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
8059 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
8060 | |
8061 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
8062 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
8063 | |
8064 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
8065 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
8066 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
8067 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
8068 | bool VisitMemberExpr(const MemberExpr *E); |
8069 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
8070 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
8071 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
8072 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
8073 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
8074 | bool VisitUnaryDeref(const UnaryOperator *E); |
8075 | bool VisitUnaryReal(const UnaryOperator *E); |
8076 | bool VisitUnaryImag(const UnaryOperator *E); |
8077 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
8078 | return VisitUnaryPreIncDec(UO); |
8079 | } |
8080 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
8081 | return VisitUnaryPreIncDec(UO); |
8082 | } |
8083 | bool VisitBinAssign(const BinaryOperator *BO); |
8084 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
8085 | |
8086 | bool VisitCastExpr(const CastExpr *E) { |
8087 | switch (E->getCastKind()) { |
8088 | default: |
8089 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
8090 | |
8091 | case CK_LValueBitCast: |
8092 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8093 | if (!Visit(E->getSubExpr())) |
8094 | return false; |
8095 | Result.Designator.setInvalid(); |
8096 | return true; |
8097 | |
8098 | case CK_BaseToDerived: |
8099 | if (!Visit(E->getSubExpr())) |
8100 | return false; |
8101 | return HandleBaseToDerivedCast(Info, E, Result); |
8102 | |
8103 | case CK_Dynamic: |
8104 | if (!Visit(E->getSubExpr())) |
8105 | return false; |
8106 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
8107 | } |
8108 | } |
8109 | }; |
8110 | } |
8111 | |
8112 | |
8113 | |
8114 | |
8115 | |
8116 | |
8117 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
8118 | bool InvalidBaseOK) { |
8119 | assert(!E->isValueDependent()); |
8120 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
8121 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
8122 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8123 | } |
8124 | |
8125 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
8126 | const NamedDecl *D = E->getDecl(); |
8127 | if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl>(D)) |
8128 | return Success(cast<ValueDecl>(D)); |
8129 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
8130 | return VisitVarDecl(E, VD); |
8131 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(D)) |
8132 | return Visit(BD->getBinding()); |
8133 | return Error(E); |
8134 | } |
8135 | |
8136 | |
8137 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
8138 | |
8139 | |
8140 | |
8141 | |
8142 | |
8143 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
8144 | isa<DeclRefExpr>(E) && |
8145 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
8146 | |
8147 | |
8148 | |
8149 | |
8150 | if (Info.checkingPotentialConstantExpression()) |
8151 | return false; |
8152 | |
8153 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
8154 | |
8155 | Result = *Info.CurrentCall->This; |
8156 | |
8157 | |
8158 | if (!HandleLValueMember(Info, E, Result, FD)) |
8159 | return false; |
8160 | |
8161 | |
8162 | if (FD->getType()->isReferenceType()) { |
8163 | APValue RVal; |
8164 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
8165 | RVal)) |
8166 | return false; |
8167 | Result.setFrom(Info.Ctx, RVal); |
8168 | } |
8169 | return true; |
8170 | } |
8171 | } |
8172 | |
8173 | CallStackFrame *Frame = nullptr; |
8174 | unsigned Version = 0; |
8175 | if (VD->hasLocalStorage()) { |
8176 | |
8177 | |
8178 | |
8179 | |
8180 | |
8181 | |
8182 | CallStackFrame *CurrFrame = Info.CurrentCall; |
8183 | if (CurrFrame->Callee && CurrFrame->Callee->Equals(VD->getDeclContext())) { |
8184 | |
8185 | |
8186 | |
8187 | if (auto *PVD = dyn_cast<ParmVarDecl>(VD)) { |
8188 | if (CurrFrame->Arguments) { |
8189 | VD = CurrFrame->Arguments.getOrigParam(PVD); |
8190 | Frame = |
8191 | Info.getCallFrameAndDepth(CurrFrame->Arguments.CallIndex).first; |
8192 | Version = CurrFrame->Arguments.Version; |
8193 | } |
8194 | } else { |
8195 | Frame = CurrFrame; |
8196 | Version = CurrFrame->getCurrentTemporaryVersion(VD); |
8197 | } |
8198 | } |
8199 | } |
8200 | |
8201 | if (!VD->getType()->isReferenceType()) { |
8202 | if (Frame) { |
8203 | Result.set({VD, Frame->Index, Version}); |
8204 | return true; |
8205 | } |
8206 | return Success(VD); |
8207 | } |
8208 | |
8209 | if (!Info.getLangOpts().CPlusPlus11) { |
8210 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1) |
8211 | << VD << VD->getType(); |
8212 | Info.Note(VD->getLocation(), diag::note_declared_at); |
8213 | } |
8214 | |
8215 | APValue *V; |
8216 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, V)) |
8217 | return false; |
8218 | if (!V->hasValue()) { |
8219 | |
8220 | |
8221 | if (!Info.checkingPotentialConstantExpression()) |
8222 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
8223 | return false; |
8224 | } |
8225 | return Success(*V, E); |
8226 | } |
8227 | |
8228 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
8229 | const MaterializeTemporaryExpr *E) { |
8230 | |
8231 | SmallVector<const Expr *, 2> CommaLHSs; |
8232 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
8233 | const Expr *Inner = |
8234 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
8235 | |
8236 | |
8237 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
8238 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
8239 | return false; |
8240 | |
8241 | |
8242 | |
8243 | |
8244 | APValue *Value; |
8245 | if (E->getStorageDuration() == SD_Static) { |
8246 | |
8247 | Value = E->getOrCreateValue(true); |
8248 | *Value = APValue(); |
8249 | Result.set(E); |
8250 | } else { |
8251 | Value = &Info.CurrentCall->createTemporary( |
8252 | E, E->getType(), |
8253 | E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression |
8254 | : ScopeKind::Block, |
8255 | Result); |
8256 | } |
8257 | |
8258 | QualType Type = Inner->getType(); |
8259 | |
8260 | |
8261 | if (!EvaluateInPlace(*Value, Info, Result, Inner)) { |
8262 | *Value = APValue(); |
8263 | return false; |
8264 | } |
8265 | |
8266 | |
8267 | for (unsigned I = Adjustments.size(); I != 0; ) { |
8268 | --I; |
8269 | switch (Adjustments[I].Kind) { |
8270 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
8271 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
8272 | Type, Result)) |
8273 | return false; |
8274 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
8275 | break; |
8276 | |
8277 | case SubobjectAdjustment::FieldAdjustment: |
8278 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
8279 | return false; |
8280 | Type = Adjustments[I].Field->getType(); |
8281 | break; |
8282 | |
8283 | case SubobjectAdjustment::MemberPointerAdjustment: |
8284 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
8285 | Adjustments[I].Ptr.RHS)) |
8286 | return false; |
8287 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
8288 | break; |
8289 | } |
8290 | } |
8291 | |
8292 | return true; |
8293 | } |
8294 | |
8295 | bool |
8296 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8297 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
8298 | "lvalue compound literal in c++?"); |
8299 | |
8300 | |
8301 | return Success(E); |
8302 | } |
8303 | |
8304 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
8305 | TypeInfoLValue TypeInfo; |
8306 | |
8307 | if (!E->isPotentiallyEvaluated()) { |
8308 | if (E->isTypeOperand()) |
8309 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Info.Ctx).getTypePtr()); |
8310 | else |
8311 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
8312 | } else { |
8313 | if (!Info.Ctx.getLangOpts().CPlusPlus20) { |
8314 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
8315 | << E->getExprOperand()->getType() |
8316 | << E->getExprOperand()->getSourceRange(); |
8317 | } |
8318 | |
8319 | if (!Visit(E->getExprOperand())) |
8320 | return false; |
8321 | |
8322 | Optional<DynamicType> DynType = |
8323 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
8324 | if (!DynType) |
8325 | return false; |
8326 | |
8327 | TypeInfo = |
8328 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
8329 | } |
8330 | |
8331 | return Success(APValue::LValueBase::getTypeInfo(TypeInfo, E->getType())); |
8332 | } |
8333 | |
8334 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
8335 | return Success(E->getGuidDecl()); |
8336 | } |
8337 | |
8338 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
8339 | |
8340 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
8341 | VisitIgnoredBaseExpression(E->getBase()); |
8342 | return VisitVarDecl(E, VD); |
8343 | } |
8344 | |
8345 | |
8346 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
8347 | if (MD->isStatic()) { |
8348 | VisitIgnoredBaseExpression(E->getBase()); |
8349 | return Success(MD); |
8350 | } |
8351 | } |
8352 | |
8353 | |
8354 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
8355 | } |
8356 | |
8357 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
8358 | |
8359 | if (E->getBase()->getType()->isVectorType()) |
8360 | return Error(E); |
8361 | |
8362 | APSInt Index; |
8363 | bool Success = true; |
8364 | |
8365 | |
8366 | |
8367 | for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) { |
8368 | if (SubExpr == E->getBase() ? !evaluatePointer(SubExpr, Result) |
8369 | : !EvaluateInteger(SubExpr, Index, Info)) { |
8370 | if (!Info.noteFailure()) |
8371 | return false; |
8372 | Success = false; |
8373 | } |
8374 | } |
8375 | |
8376 | return Success && |
8377 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
8378 | } |
8379 | |
8380 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
8381 | return evaluatePointer(E->getSubExpr(), Result); |
8382 | } |
8383 | |
8384 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
8385 | if (!Visit(E->getSubExpr())) |
8386 | return false; |
8387 | |
8388 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
8389 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
8390 | return true; |
8391 | } |
8392 | |
8393 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
8394 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
8395 | "lvalue __imag__ on scalar?"); |
8396 | if (!Visit(E->getSubExpr())) |
8397 | return false; |
8398 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
8399 | return true; |
8400 | } |
8401 | |
8402 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
8403 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8404 | return Error(UO); |
8405 | |
8406 | if (!this->Visit(UO->getSubExpr())) |
8407 | return false; |
8408 | |
8409 | return handleIncDec( |
8410 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
8411 | UO->isIncrementOp(), nullptr); |
8412 | } |
8413 | |
8414 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
8415 | const CompoundAssignOperator *CAO) { |
8416 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8417 | return Error(CAO); |
8418 | |
8419 | bool Success = true; |
8420 | |
8421 | |
8422 | APValue RHS; |
8423 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) { |
8424 | if (!Info.noteFailure()) |
8425 | return false; |
8426 | Success = false; |
8427 | } |
8428 | |
8429 | |
8430 | if (!this->Visit(CAO->getLHS()) || !Success) |
8431 | return false; |
8432 | |
8433 | return handleCompoundAssignment( |
8434 | this->Info, CAO, |
8435 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
8436 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
8437 | } |
8438 | |
8439 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
8440 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8441 | return Error(E); |
8442 | |
8443 | bool Success = true; |
8444 | |
8445 | |
8446 | APValue NewVal; |
8447 | if (!Evaluate(NewVal, this->Info, E->getRHS())) { |
8448 | if (!Info.noteFailure()) |
8449 | return false; |
8450 | Success = false; |
8451 | } |
8452 | |
8453 | if (!this->Visit(E->getLHS()) || !Success) |
8454 | return false; |
8455 | |
8456 | if (Info.getLangOpts().CPlusPlus20 && |
8457 | !HandleUnionActiveMemberChange(Info, E->getLHS(), Result)) |
8458 | return false; |
8459 | |
8460 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
8461 | NewVal); |
8462 | } |
8463 | |
8464 | |
8465 | |
8466 | |
8467 | |
8468 | |
8469 | |
8470 | |
8471 | |
8472 | |
8473 | |
8474 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8475 | const CallExpr *Call, |
8476 | llvm::APInt &Result) { |
8477 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
8478 | |
8479 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
8480 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
8481 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
8482 | if (Call->getNumArgs() <= SizeArgNo) |
8483 | return false; |
8484 | |
8485 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
8486 | Expr::EvalResult ExprResult; |
8487 | if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects)) |
8488 | return false; |
8489 | Into = ExprResult.Val.getInt(); |
8490 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
8491 | return false; |
8492 | Into = Into.zextOrSelf(BitsInSizeT); |
8493 | return true; |
8494 | }; |
8495 | |
8496 | APSInt SizeOfElem; |
8497 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
8498 | return false; |
8499 | |
8500 | if (!AllocSize->getNumElemsParam().isValid()) { |
8501 | Result = std::move(SizeOfElem); |
8502 | return true; |
8503 | } |
8504 | |
8505 | APSInt NumberOfElems; |
8506 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
8507 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
8508 | return false; |
8509 | |
8510 | bool Overflow; |
8511 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
8512 | if (Overflow) |
8513 | return false; |
8514 | |
8515 | Result = std::move(BytesAvailable); |
8516 | return true; |
8517 | } |
8518 | |
8519 | |
8520 | |
8521 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8522 | const LValue &LVal, |
8523 | llvm::APInt &Result) { |
8524 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8525 | "Can't get the size of a non alloc_size function"); |
8526 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
8527 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
8528 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
8529 | } |
8530 | |
8531 | |
8532 | |
8533 | |
8534 | |
8535 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
8536 | LValue &Result) { |
8537 | if (Base.isNull()) |
8538 | return false; |
8539 | |
8540 | |
8541 | |
8542 | |
8543 | |
8544 | |
8545 | const auto *VD = |
8546 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
8547 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
8548 | return false; |
8549 | |
8550 | const Expr *Init = VD->getAnyInitializer(); |
8551 | if (!Init) |
8552 | return false; |
8553 | |
8554 | const Expr *E = Init->IgnoreParens(); |
8555 | if (!tryUnwrapAllocSizeCall(E)) |
8556 | return false; |
8557 | |
8558 | |
8559 | |
8560 | Result.setInvalid(E); |
8561 | |
8562 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
8563 | Result.addUnsizedArray(Info, E, Pointee); |
8564 | return true; |
8565 | } |
8566 | |
8567 | namespace { |
8568 | class PointerExprEvaluator |
8569 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
8570 | LValue &Result; |
8571 | bool InvalidBaseOK; |
8572 | |
8573 | bool Success(const Expr *E) { |
8574 | Result.set(E); |
8575 | return true; |
8576 | } |
8577 | |
8578 | bool evaluateLValue(const Expr *E, LValue &Result) { |
8579 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
8580 | } |
8581 | |
8582 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8583 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
8584 | } |
8585 | |
8586 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
8587 | public: |
8588 | |
8589 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
8590 | : ExprEvaluatorBaseTy(info), Result(Result), |
8591 | InvalidBaseOK(InvalidBaseOK) {} |
8592 | |
8593 | bool Success(const APValue &V, const Expr *E) { |
8594 | Result.setFrom(Info.Ctx, V); |
8595 | return true; |
8596 | } |
8597 | bool ZeroInitialization(const Expr *E) { |
8598 | Result.setNull(Info.Ctx, E->getType()); |
8599 | return true; |
8600 | } |
8601 | |
8602 | bool VisitBinaryOperator(const BinaryOperator *E); |
8603 | bool VisitCastExpr(const CastExpr* E); |
8604 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
8605 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
8606 | { return Success(E); } |
8607 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
8608 | if (E->isExpressibleAsConstantInitializer()) |
8609 | return Success(E); |
8610 | if (Info.noteFailure()) |
8611 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
8612 | return Error(E); |
8613 | } |
8614 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
8615 | { return Success(E); } |
8616 | bool VisitCallExpr(const CallExpr *E); |
8617 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
8618 | bool VisitBlockExpr(const BlockExpr *E) { |
8619 | if (!E->getBlockDecl()->hasCaptures()) |
8620 | return Success(E); |
8621 | return Error(E); |
8622 | } |
8623 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
8624 | |
8625 | if (Info.checkingPotentialConstantExpression()) |
8626 | return false; |
8627 | if (!Info.CurrentCall->This) { |
8628 | if (Info.getLangOpts().CPlusPlus11) |
8629 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
8630 | else |
8631 | Info.FFDiag(E); |
8632 | return false; |
8633 | } |
8634 | Result = *Info.CurrentCall->This; |
8635 | |
8636 | |
8637 | |
8638 | |
8639 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
8640 | |
8641 | |
8642 | if (!Info.CurrentCall->LambdaThisCaptureField) |
8643 | return false; |
8644 | |
8645 | |
8646 | |
8647 | if (!HandleLValueMember(Info, E, Result, |
8648 | Info.CurrentCall->LambdaThisCaptureField)) |
8649 | return false; |
8650 | |
8651 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
8652 | ->isPointerType()) { |
8653 | APValue RVal; |
8654 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
8655 | RVal)) |
8656 | return false; |
8657 | |
8658 | Result.setFrom(Info.Ctx, RVal); |
8659 | } |
8660 | } |
8661 | return true; |
8662 | } |
8663 | |
8664 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
8665 | |
8666 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
8667 | assert(E->isStringType() && "SourceLocExpr isn't a pointer type?"); |
8668 | APValue LValResult = E->EvaluateInContext( |
8669 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
8670 | Result.setFrom(Info.Ctx, LValResult); |
8671 | return true; |
8672 | } |
8673 | |
8674 | bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) { |
8675 | std::string ResultStr = E->ComputeName(Info.Ctx); |
8676 | |
8677 | Info.Ctx.SYCLUniqueStableNameEvaluatedValues[E] = ResultStr; |
8678 | |
8679 | QualType CharTy = Info.Ctx.CharTy.withConst(); |
8680 | APInt Size(Info.Ctx.getTypeSize(Info.Ctx.getSizeType()), |
8681 | ResultStr.size() + 1); |
8682 | QualType ArrayTy = Info.Ctx.getConstantArrayType(CharTy, Size, nullptr, |
8683 | ArrayType::Normal, 0); |
8684 | |
8685 | StringLiteral *SL = |
8686 | StringLiteral::Create(Info.Ctx, ResultStr, StringLiteral::Ascii, |
8687 | false, ArrayTy, E->getLocation()); |
8688 | |
8689 | evaluateLValue(SL, Result); |
8690 | Result.addArray(Info, E, cast<ConstantArrayType>(ArrayTy)); |
8691 | return true; |
8692 | } |
8693 | |
8694 | |
8695 | }; |
8696 | } |
8697 | |
8698 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
8699 | bool InvalidBaseOK) { |
8700 | assert(!E->isValueDependent()); |
8701 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
8702 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8703 | } |
8704 | |
8705 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
8706 | if (E->getOpcode() != BO_Add && |
8707 | E->getOpcode() != BO_Sub) |
8708 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
8709 | |
8710 | const Expr *PExp = E->getLHS(); |
8711 | const Expr *IExp = E->getRHS(); |
8712 | if (IExp->getType()->isPointerType()) |
8713 | std::swap(PExp, IExp); |
8714 | |
8715 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
8716 | if (!EvalPtrOK && !Info.noteFailure()) |
8717 | return false; |
8718 | |
8719 | llvm::APSInt Offset; |
8720 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
8721 | return false; |
8722 | |
8723 | if (E->getOpcode() == BO_Sub) |
8724 | negateAsSigned(Offset); |
8725 | |
8726 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
8727 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
8728 | } |
8729 | |
8730 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
8731 | return evaluateLValue(E->getSubExpr(), Result); |
8732 | } |
8733 | |
8734 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
8735 | const Expr *SubExpr = E->getSubExpr(); |
8736 | |
8737 | switch (E->getCastKind()) { |
8738 | default: |
8739 | break; |
8740 | case CK_BitCast: |
8741 | case CK_CPointerToObjCPointerCast: |
8742 | case CK_BlockPointerToObjCPointerCast: |
8743 | case CK_AnyPointerToBlockPointerCast: |
8744 | case CK_AddressSpaceConversion: |
8745 | if (!Visit(SubExpr)) |
8746 | return false; |
8747 | |
8748 | |
8749 | |
8750 | if (!E->getType()->isVoidPointerType()) { |
8751 | if (!Result.InvalidBase && !Result.Designator.Invalid && |
8752 | !Result.IsNullPtr && |
8753 | Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx), |
8754 | E->getType()->getPointeeType()) && |
8755 | Info.getStdAllocatorCaller("allocate")) { |
8756 | |
8757 | |
8758 | |
8759 | } else { |
8760 | Result.Designator.setInvalid(); |
8761 | if (SubExpr->getType()->isVoidPointerType()) |
8762 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
8763 | << 3 << SubExpr->getType(); |
8764 | else |
8765 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8766 | } |
8767 | } |
8768 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
8769 | ZeroInitialization(E); |
8770 | return true; |
8771 | |
8772 | case CK_DerivedToBase: |
8773 | case CK_UncheckedDerivedToBase: |
8774 | if (!evaluatePointer(E->getSubExpr(), Result)) |
8775 | return false; |
8776 | if (!Result.Base && Result.Offset.isZero()) |
8777 | return true; |
8778 | |
8779 | |
8780 | |
8781 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
8782 | castAs<PointerType>()->getPointeeType(), |
8783 | Result); |
8784 | |
8785 | case CK_BaseToDerived: |
8786 | if (!Visit(E->getSubExpr())) |
8787 | return false; |
8788 | if (!Result.Base && Result.Offset.isZero()) |
8789 | return true; |
8790 | return HandleBaseToDerivedCast(Info, E, Result); |
8791 | |
8792 | case CK_Dynamic: |
8793 | if (!Visit(E->getSubExpr())) |
8794 | return false; |
8795 | return HandleDynamicCast(Info, cast<ExplicitCastExpr>(E), Result); |
8796 | |
8797 | case CK_NullToPointer: |
8798 | VisitIgnoredValue(E->getSubExpr()); |
8799 | return ZeroInitialization(E); |
8800 | |
8801 | case CK_IntegralToPointer: { |
8802 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
8803 | |
8804 | APValue Value; |
8805 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
8806 | break; |
8807 | |
8808 | if (Value.isInt()) { |
8809 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
8810 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
8811 | Result.Base = (Expr*)nullptr; |
8812 | Result.InvalidBase = false; |
8813 | Result.Offset = CharUnits::fromQuantity(N); |
8814 | Result.Designator.setInvalid(); |
8815 | Result.IsNullPtr = false; |
8816 | return true; |
8817 | } else { |
8818 | |
8819 | Result.setFrom(Info.Ctx, Value); |
8820 | return true; |
8821 | } |
8822 | } |
8823 | |
8824 | case CK_ArrayToPointerDecay: { |
8825 | if (SubExpr->isGLValue()) { |
8826 | if (!evaluateLValue(SubExpr, Result)) |
8827 | return false; |
8828 | } else { |
8829 | APValue &Value = Info.CurrentCall->createTemporary( |
8830 | SubExpr, SubExpr->getType(), ScopeKind::FullExpression, Result); |
8831 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
8832 | return false; |
8833 | } |
8834 | |
8835 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
8836 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
8837 | Result.addArray(Info, E, CAT); |
8838 | else |
8839 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
8840 | return true; |
8841 | } |
8842 | |
8843 | case CK_FunctionToPointerDecay: |
8844 | return evaluateLValue(SubExpr, Result); |
8845 | |
8846 | case CK_LValueToRValue: { |
8847 | LValue LVal; |
8848 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
8849 | return false; |
8850 | |
8851 | APValue RVal; |
8852 | |
8853 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
8854 | LVal, RVal)) |
8855 | return InvalidBaseOK && |
8856 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
8857 | return Success(RVal, E); |
8858 | } |
8859 | } |
8860 | |
8861 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8862 | } |
8863 | |
8864 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
8865 | UnaryExprOrTypeTrait ExprKind) { |
8866 | |
8867 | |
8868 | |
8869 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
8870 | T = Ref->getPointeeType(); |
8871 | |
8872 | if (T.getQualifiers().hasUnaligned()) |
8873 | return CharUnits::One(); |
8874 | |
8875 | const bool AlignOfReturnsPreferred = |
8876 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
8877 | |
8878 | |
8879 | |
8880 | |
8881 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
8882 | return Info.Ctx.toCharUnitsFromBits( |
8883 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
8884 | |
8885 | else if (ExprKind == UETT_AlignOf) |
8886 | return Info.Ctx.getTypeAlignInChars(T.getTypePtr()); |
8887 | else |
8888 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
8889 | } |
8890 | |
8891 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
8892 | UnaryExprOrTypeTrait ExprKind) { |
8893 | E = E->IgnoreParens(); |
8894 | |
8895 | |
8896 | |
8897 | |
8898 | |
8899 | |
8900 | |
8901 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
8902 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
8903 | true); |
8904 | |
8905 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
8906 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
8907 | true); |
8908 | |
8909 | return GetAlignOfType(Info, E->getType(), ExprKind); |
8910 | } |
8911 | |
8912 | static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) { |
8913 | if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>()) |
8914 | return Info.Ctx.getDeclAlign(VD); |
8915 | if (const auto *E = Value.Base.dyn_cast<const Expr *>()) |
8916 | return GetAlignOfExpr(Info, E, UETT_AlignOf); |
8917 | return GetAlignOfType(Info, Value.Base.getTypeInfoType(), UETT_AlignOf); |
8918 | } |
8919 | |
8920 | |
8921 | |
8922 | static bool getAlignmentArgument(const Expr *E, QualType ForType, |
8923 | EvalInfo &Info, APSInt &Alignment) { |
8924 | if (!EvaluateInteger(E, Alignment, Info)) |
8925 | return false; |
8926 | if (Alignment < 0 || !Alignment.isPowerOf2()) { |
8927 | Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment; |
8928 | return false; |
8929 | } |
8930 | unsigned SrcWidth = Info.Ctx.getIntWidth(ForType); |
8931 | APSInt MaxValue(APInt::getOneBitSet(SrcWidth, SrcWidth - 1)); |
8932 | if (APSInt::compareValues(Alignment, MaxValue) > 0) { |
8933 | Info.FFDiag(E, diag::note_constexpr_alignment_too_big) |
8934 | << MaxValue << ForType << Alignment; |
8935 | return false; |
8936 | } |
8937 | |
8938 | |
8939 | APSInt ExtAlignment = |
8940 | APSInt(Alignment.zextOrTrunc(SrcWidth), true); |
8941 | assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 && |
8942 | "Alignment should not be changed by ext/trunc"); |
8943 | Alignment = ExtAlignment; |
8944 | assert(Alignment.getBitWidth() == SrcWidth); |
8945 | return true; |
8946 | } |
8947 | |
8948 | |
8949 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
8950 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
8951 | return true; |
8952 | |
8953 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
8954 | return false; |
8955 | |
8956 | Result.setInvalid(E); |
8957 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
8958 | Result.addUnsizedArray(Info, E, PointeeTy); |
8959 | return true; |
8960 | } |
8961 | |
8962 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
8963 | if (IsStringLiteralCall(E)) |
8964 | return Success(E); |
8965 | |
8966 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
8967 | return VisitBuiltinCallExpr(E, BuiltinOp); |
8968 | |
8969 | return visitNonBuiltinCallExpr(E); |
8970 | } |
8971 | |
8972 | |
8973 | |
8974 | static bool isOneByteCharacterType(QualType T) { |
8975 | return T->isCharType() || T->isChar8Type(); |
8976 | } |
8977 | |
8978 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
8979 | unsigned BuiltinOp) { |
8980 | switch (BuiltinOp) { |
8981 | case Builtin::BI__builtin_addressof: |
8982 | return evaluateLValue(E->getArg(0), Result); |
8983 | case Builtin::BI__builtin_assume_aligned: { |
8984 | |
8985 | |
8986 | |
8987 | if (!evaluatePointer(E->getArg(0), Result)) |
8988 | return false; |
8989 | |
8990 | LValue OffsetResult(Result); |
8991 | APSInt Alignment; |
8992 | if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info, |
8993 | Alignment)) |
8994 | return false; |
8995 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
8996 | |
8997 | if (E->getNumArgs() > 2) { |
8998 | APSInt Offset; |
8999 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
9000 | return false; |
9001 | |
9002 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
9003 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
9004 | } |
9005 | |
9006 | |
9007 | if (OffsetResult.Base) { |
9008 | CharUnits BaseAlignment = getBaseAlignment(Info, OffsetResult); |
9009 | |
9010 | if (BaseAlignment < Align) { |
9011 | Result.Designator.setInvalid(); |
9012 | |
9013 | CCEDiag(E->getArg(0), |
9014 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
9015 | << (unsigned)BaseAlignment.getQuantity() |
9016 | << (unsigned)Align.getQuantity(); |
9017 | return false; |
9018 | } |
9019 | } |
9020 | |
9021 | |
9022 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
9023 | Result.Designator.setInvalid(); |
9024 | |
9025 | (OffsetResult.Base |
9026 | ? CCEDiag(E->getArg(0), |
9027 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
9028 | : CCEDiag(E->getArg(0), |
9029 | diag::note_constexpr_baa_value_insufficient_alignment)) |
9030 | << (int)OffsetResult.Offset.getQuantity() |
9031 | << (unsigned)Align.getQuantity(); |
9032 | return false; |
9033 | } |
9034 | |
9035 | return true; |
9036 | } |
9037 | case Builtin::BI__builtin_align_up: |
9038 | case Builtin::BI__builtin_align_down: { |
9039 | if (!evaluatePointer(E->getArg(0), Result)) |
9040 | return false; |
9041 | APSInt Alignment; |
9042 | if (!getAlignmentArgument(E->getArg(1), E->getArg(0)->getType(), Info, |
9043 | Alignment)) |
9044 | return false; |
9045 | CharUnits BaseAlignment = getBaseAlignment(Info, Result); |
9046 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Result.Offset); |
9047 | |
9048 | |
9049 | if (PtrAlign.getQuantity() >= Alignment) |
9050 | return true; |
9051 | |
9052 | |
9053 | |
9054 | |
9055 | |
9056 | |
9057 | if (BaseAlignment.getQuantity() >= Alignment) { |
9058 | assert(Alignment.getBitWidth() <= 64 && |
9059 | "Cannot handle > 64-bit address-space"); |
9060 | uint64_t Alignment64 = Alignment.getZExtValue(); |
9061 | CharUnits NewOffset = CharUnits::fromQuantity( |
9062 | BuiltinOp == Builtin::BI__builtin_align_down |
9063 | ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64) |
9064 | : llvm::alignTo(Result.Offset.getQuantity(), Alignment64)); |
9065 | Result.adjustOffset(NewOffset - Result.Offset); |
9066 | |
9067 | return true; |
9068 | } |
9069 | |
9070 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust) |
9071 | << Alignment; |
9072 | return false; |
9073 | } |
9074 | case Builtin::BI__builtin_operator_new: |
9075 | return HandleOperatorNewCall(Info, E, Result); |
9076 | case Builtin::BI__builtin_launder: |
9077 | return evaluatePointer(E->getArg(0), Result); |
9078 | case Builtin::BIstrchr: |
9079 | case Builtin::BIwcschr: |
9080 | case Builtin::BImemchr: |
9081 | case Builtin::BIwmemchr: |
9082 | if (Info.getLangOpts().CPlusPlus11) |
9083 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9084 | << 0 << 0 |
9085 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
9086 | else |
9087 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9088 | LLVM_FALLTHROUGH; |
9089 | case Builtin::BI__builtin_strchr: |
9090 | case Builtin::BI__builtin_wcschr: |
9091 | case Builtin::BI__builtin_memchr: |
9092 | case Builtin::BI__builtin_char_memchr: |
9093 | case Builtin::BI__builtin_wmemchr: { |
9094 | if (!Visit(E->getArg(0))) |
9095 | return false; |
9096 | APSInt Desired; |
9097 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
9098 | return false; |
9099 | uint64_t MaxLength = uint64_t(-1); |
9100 | if (BuiltinOp != Builtin::BIstrchr && |
9101 | BuiltinOp != Builtin::BIwcschr && |
9102 | BuiltinOp != Builtin::BI__builtin_strchr && |
9103 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
9104 | APSInt N; |
9105 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
9106 | return false; |
9107 | MaxLength = N.getExtValue(); |
9108 | } |
9109 | |
9110 | if (MaxLength == 0u) |
9111 | return ZeroInitialization(E); |
9112 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
9113 | Result.Designator.Invalid) |
9114 | return false; |
9115 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
9116 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
9117 | BuiltinOp == Builtin::BI__builtin_memchr; |
9118 | assert(IsRawByte || |
9119 | Info.Ctx.hasSameUnqualifiedType( |
9120 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
9121 | |
9122 | if (IsRawByte && CharTy->isIncompleteType()) { |
9123 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
9124 | return false; |
9125 | } |
9126 | |
9127 | |
9128 | if (IsRawByte && !isOneByteCharacterType(CharTy)) { |
9129 | Info.FFDiag(E, diag::note_constexpr_memchr_unsupported) |
9130 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'") |
9131 | << CharTy; |
9132 | return false; |
9133 | } |
9134 | |
9135 | |
9136 | uint64_t DesiredVal; |
9137 | bool StopAtNull = false; |
9138 | switch (BuiltinOp) { |
9139 | case Builtin::BIstrchr: |
9140 | case Builtin::BI__builtin_strchr: |
9141 | |
9142 | |
9143 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
9144 | E->getArg(1)->getType(), |
9145 | Desired), |
9146 | Desired)) |
9147 | return ZeroInitialization(E); |
9148 | StopAtNull = true; |
9149 | LLVM_FALLTHROUGH; |
9150 | case Builtin::BImemchr: |
9151 | case Builtin::BI__builtin_memchr: |
9152 | case Builtin::BI__builtin_char_memchr: |
9153 | |
9154 | |
9155 | |
9156 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
9157 | break; |
9158 | |
9159 | case Builtin::BIwcschr: |
9160 | case Builtin::BI__builtin_wcschr: |
9161 | StopAtNull = true; |
9162 | LLVM_FALLTHROUGH; |
9163 | case Builtin::BIwmemchr: |
9164 | case Builtin::BI__builtin_wmemchr: |
9165 | |
9166 | DesiredVal = Desired.getZExtValue(); |
9167 | break; |
9168 | } |
9169 | |
9170 | for (; MaxLength; --MaxLength) { |
9171 | APValue Char; |
9172 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
9173 | !Char.isInt()) |
9174 | return false; |
9175 | if (Char.getInt().getZExtValue() == DesiredVal) |
9176 | return true; |
9177 | if (StopAtNull && !Char.getInt()) |
9178 | break; |
9179 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
9180 | return false; |
9181 | } |
9182 | |
9183 | return ZeroInitialization(E); |
9184 | } |
9185 | |
9186 | case Builtin::BImemcpy: |
9187 | case Builtin::BImemmove: |
9188 | case Builtin::BIwmemcpy: |
9189 | case Builtin::BIwmemmove: |
9190 | if (Info.getLangOpts().CPlusPlus11) |
9191 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9192 | << 0 << 0 |
9193 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
9194 | else |
9195 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9196 | LLVM_FALLTHROUGH; |
9197 | case Builtin::BI__builtin_memcpy: |
9198 | case Builtin::BI__builtin_memmove: |
9199 | case Builtin::BI__builtin_wmemcpy: |
9200 | case Builtin::BI__builtin_wmemmove: { |
9201 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
9202 | BuiltinOp == Builtin::BIwmemmove || |
9203 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
9204 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9205 | bool Move = BuiltinOp == Builtin::BImemmove || |
9206 | BuiltinOp == Builtin::BIwmemmove || |
9207 | BuiltinOp == Builtin::BI__builtin_memmove || |
9208 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9209 | |
9210 | |
9211 | if (!Visit(E->getArg(0))) |
9212 | return false; |
9213 | LValue Dest = Result; |
9214 | |
9215 | LValue Src; |
9216 | if (!EvaluatePointer(E->getArg(1), Src, Info)) |
9217 | return false; |
9218 | |
9219 | APSInt N; |
9220 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
9221 | return false; |
9222 | assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
9223 | |
9224 | |
9225 | |
9226 | if (!N) |
9227 | return true; |
9228 | |
9229 | |
9230 | |
9231 | |
9232 | if (!Src.Base || !Dest.Base) { |
9233 | APValue Val; |
9234 | (!Src.Base ? Src : Dest).moveInto(Val); |
9235 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
9236 | << Move << WChar << !!Src.Base |
9237 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
9238 | return false; |
9239 | } |
9240 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
9241 | return false; |
9242 | |
9243 | |
9244 | |
9245 | |
9246 | QualType T = Dest.Designator.getType(Info.Ctx); |
9247 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
9248 | if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) { |
9249 | |
9250 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
9251 | return false; |
9252 | } |
9253 | if (T->isIncompleteType()) { |
9254 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
9255 | return false; |
9256 | } |
9257 | if (!T.isTriviallyCopyableType(Info.Ctx)) { |
9258 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
9259 | return false; |
9260 | } |
9261 | |
9262 | |
9263 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
9264 | if (!WChar) { |
9265 | uint64_t Remainder; |
9266 | llvm::APInt OrigN = N; |
9267 | llvm::APInt::udivrem(OrigN, TSize, N, Remainder); |
9268 | if (Remainder) { |
9269 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9270 | << Move << WChar << 0 << T << toString(OrigN, 10, false) |
9271 | << (unsigned)TSize; |
9272 | return false; |
9273 | } |
9274 | } |
9275 | |
9276 | |
9277 | |
9278 | |
9279 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
9280 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
9281 | if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { |
9282 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9283 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
9284 | << toString(N, 10, false); |
9285 | return false; |
9286 | } |
9287 | uint64_t NElems = N.getZExtValue(); |
9288 | uint64_t NBytes = NElems * TSize; |
9289 | |
9290 | |
9291 | int Direction = 1; |
9292 | if (HasSameBase(Src, Dest)) { |
9293 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
9294 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
9295 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
9296 | |
9297 | if (!Move) { |
9298 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9299 | return false; |
9300 | } |
9301 | |
9302 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
9303 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
9304 | return false; |
9305 | Direction = -1; |
9306 | } else if (!Move && SrcOffset >= DestOffset && |
9307 | SrcOffset - DestOffset < NBytes) { |
9308 | |
9309 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9310 | return false; |
9311 | } |
9312 | } |
9313 | |
9314 | while (true) { |
9315 | APValue Val; |
9316 | |
9317 | |
9318 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
9319 | !handleAssignment(Info, E, Dest, T, Val)) |
9320 | return false; |
9321 | |
9322 | |
9323 | if (--NElems == 0) |
9324 | return true; |
9325 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
9326 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
9327 | return false; |
9328 | } |
9329 | } |
9330 | |
9331 | default: |
9332 | break; |
9333 | } |
9334 | |
9335 | return visitNonBuiltinCallExpr(E); |
9336 | } |
9337 | |
9338 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
9339 | APValue &Result, const InitListExpr *ILE, |
9340 | QualType AllocType); |
9341 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
9342 | APValue &Result, |
9343 | const CXXConstructExpr *CCE, |
9344 | QualType AllocType); |
9345 | |
9346 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
9347 | if (!Info.getLangOpts().CPlusPlus20) |
9348 | Info.CCEDiag(E, diag::note_constexpr_new); |
9349 | |
9350 | |
9351 | if (Info.SpeculativeEvaluationDepth) |
9352 | return false; |
9353 | |
9354 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
9355 | |
9356 | bool IsNothrow = false; |
9357 | bool IsPlacement = false; |
9358 | if (OperatorNew->isReservedGlobalPlacementOperator() && |
9359 | Info.CurrentCall->isStdFunction() && !E->isArray()) { |
9360 | |
9361 | assert(E->getNumPlacementArgs() == 1); |
9362 | if (!EvaluatePointer(E->getPlacementArg(0), Result, Info)) |
9363 | return false; |
9364 | if (Result.Designator.Invalid) |
9365 | return false; |
9366 | IsPlacement = true; |
9367 | } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) { |
9368 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
9369 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
9370 | return false; |
9371 | } else if (E->getNumPlacementArgs()) { |
9372 | |
9373 | |
9374 | |
9375 | |
9376 | |
9377 | |
9378 | |
9379 | |
9380 | |
9381 | |
9382 | if (E->getNumPlacementArgs() != 1 || |
9383 | !E->getPlacementArg(0)->getType()->isNothrowT()) |
9384 | return Error(E, diag::note_constexpr_new_placement); |
9385 | |
9386 | LValue Nothrow; |
9387 | if (!EvaluateLValue(E->getPlacementArg(0), Nothrow, Info)) |
9388 | return false; |
9389 | IsNothrow = true; |
9390 | } |
9391 | |
9392 | const Expr *Init = E->getInitializer(); |
9393 | const InitListExpr *ResizedArrayILE = nullptr; |
9394 | const CXXConstructExpr *ResizedArrayCCE = nullptr; |
9395 | bool ValueInit = false; |
9396 | |
9397 | QualType AllocType = E->getAllocatedType(); |
9398 | if (Optional<const Expr*> ArraySize = E->getArraySize()) { |
9399 | const Expr *Stripped = *ArraySize; |
9400 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped); |
9401 | Stripped = ICE->getSubExpr()) |
9402 | if (ICE->getCastKind() != CK_NoOp && |
9403 | ICE->getCastKind() != CK_IntegralCast) |
9404 | break; |
9405 | |
9406 | llvm::APSInt ArrayBound; |
9407 | if (!EvaluateInteger(Stripped, ArrayBound, Info)) |
9408 | return false; |
9409 | |
9410 | |
9411 | |
9412 | |
9413 | |
9414 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
9415 | if (IsNothrow) |
9416 | return ZeroInitialization(E); |
9417 | |
9418 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
9419 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9420 | return false; |
9421 | } |
9422 | |
9423 | |
9424 | |
9425 | if (ConstantArrayType::getNumAddressingBits(Info.Ctx, AllocType, |
9426 | ArrayBound) > |
9427 | ConstantArrayType::getMaxSizeBits(Info.Ctx)) { |
9428 | if (IsNothrow) |
9429 | return ZeroInitialization(E); |
9430 | |
9431 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_large) |
9432 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9433 | return false; |
9434 | } |
9435 | |
9436 | |
9437 | |
9438 | |
9439 | if (!Init) { |
9440 | |
9441 | } else if (isa<CXXScalarValueInitExpr>(Init) || |
9442 | isa<ImplicitValueInitExpr>(Init)) { |
9443 | ValueInit = true; |
9444 | } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Init)) { |
9445 | ResizedArrayCCE = CCE; |
9446 | } else { |
9447 | auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType()); |
9448 | assert(CAT && "unexpected type for array initializer"); |
9449 | |
9450 | unsigned Bits = |
9451 | std::max(CAT->getSize().getBitWidth(), ArrayBound.getBitWidth()); |
9452 | llvm::APInt InitBound = CAT->getSize().zextOrSelf(Bits); |
9453 | llvm::APInt AllocBound = ArrayBound.zextOrSelf(Bits); |
9454 | if (InitBound.ugt(AllocBound)) { |
9455 | if (IsNothrow) |
9456 | return ZeroInitialization(E); |
9457 | |
9458 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
9459 | << toString(AllocBound, 10, false) |
9460 | << toString(InitBound, 10, false) |
9461 | << (*ArraySize)->getSourceRange(); |
9462 | return false; |
9463 | } |
9464 | |
9465 | |
9466 | |
9467 | if (InitBound != AllocBound) |
9468 | ResizedArrayILE = cast<InitListExpr>(Init); |
9469 | } |
9470 | |
9471 | AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr, |
9472 | ArrayType::Normal, 0); |
9473 | } else { |
9474 | assert(!AllocType->isArrayType() && |
9475 | "array allocation with non-array new"); |
9476 | } |
9477 | |
9478 | APValue *Val; |
9479 | if (IsPlacement) { |
9480 | AccessKinds AK = AK_Construct; |
9481 | struct FindObjectHandler { |
9482 | EvalInfo &Info; |
9483 | const Expr *E; |
9484 | QualType AllocType; |
9485 | const AccessKinds AccessKind; |
9486 | APValue *Value; |
9487 | |
9488 | typedef bool result_type; |
9489 | bool failed() { return false; } |
9490 | bool found(APValue &Subobj, QualType SubobjType) { |
9491 | |
9492 | |
9493 | if (!Info.Ctx.hasSameUnqualifiedType(SubobjType, AllocType)) { |
9494 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) << |
9495 | SubobjType << AllocType; |
9496 | return false; |
9497 | } |
9498 | Value = &Subobj; |
9499 | return true; |
9500 | } |
9501 | bool found(APSInt &Value, QualType SubobjType) { |
9502 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9503 | return false; |
9504 | } |
9505 | bool found(APFloat &Value, QualType SubobjType) { |
9506 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9507 | return false; |
9508 | } |
9509 | } Handler = {Info, E, AllocType, AK, nullptr}; |
9510 | |
9511 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
9512 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
9513 | return false; |
9514 | |
9515 | Val = Handler.Value; |
9516 | |
9517 | |
9518 | |
9519 | |
9520 | |
9521 | *Val = APValue(); |
9522 | } else { |
9523 | |
9524 | Val = Info.createHeapAlloc(E, AllocType, Result); |
9525 | if (!Val) |
9526 | return false; |
9527 | } |
9528 | |
9529 | if (ValueInit) { |
9530 | ImplicitValueInitExpr VIE(AllocType); |
9531 | if (!EvaluateInPlace(*Val, Info, Result, &VIE)) |
9532 | return false; |
9533 | } else if (ResizedArrayILE) { |
9534 | if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE, |
9535 | AllocType)) |
9536 | return false; |
9537 | } else if (ResizedArrayCCE) { |
9538 | if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE, |
9539 | AllocType)) |
9540 | return false; |
9541 | } else if (Init) { |
9542 | if (!EvaluateInPlace(*Val, Info, Result, Init)) |
9543 | return false; |
9544 | } else if (!getDefaultInitValue(AllocType, *Val)) { |
9545 | return false; |
9546 | } |
9547 | |
9548 | |
9549 | |
9550 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
9551 | Result.addArray(Info, E, cast<ConstantArrayType>(AT)); |
9552 | |
9553 | return true; |
9554 | } |
9555 | |
9556 | |
9557 | |
9558 | |
9559 | namespace { |
9560 | class MemberPointerExprEvaluator |
9561 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
9562 | MemberPtr &Result; |
9563 | |
9564 | bool Success(const ValueDecl *D) { |
9565 | Result = MemberPtr(D); |
9566 | return true; |
9567 | } |
9568 | public: |
9569 | |
9570 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
9571 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
9572 | |
9573 | bool Success(const APValue &V, const Expr *E) { |
9574 | Result.setFrom(V); |
9575 | return true; |
9576 | } |
9577 | bool ZeroInitialization(const Expr *E) { |
9578 | return Success((const ValueDecl*)nullptr); |
9579 | } |
9580 | |
9581 | bool VisitCastExpr(const CastExpr *E); |
9582 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
9583 | }; |
9584 | } |
9585 | |
9586 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
9587 | EvalInfo &Info) { |
9588 | assert(!E->isValueDependent()); |
9589 | assert(E->isPRValue() && E->getType()->isMemberPointerType()); |
9590 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
9591 | } |
9592 | |
9593 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9594 | switch (E->getCastKind()) { |
9595 | default: |
9596 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9597 | |
9598 | case CK_NullToMemberPointer: |
9599 | VisitIgnoredValue(E->getSubExpr()); |
9600 | return ZeroInitialization(E); |
9601 | |
9602 | case CK_BaseToDerivedMemberPointer: { |
9603 | if (!Visit(E->getSubExpr())) |
9604 | return false; |
9605 | if (E->path_empty()) |
9606 | return true; |
9607 | |
9608 | |
9609 | |
9610 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
9611 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
9612 | PathI != PathE; ++PathI) { |
9613 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
9614 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
9615 | if (!Result.castToDerived(Derived)) |
9616 | return Error(E); |
9617 | } |
9618 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
9619 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
9620 | return Error(E); |
9621 | return true; |
9622 | } |
9623 | |
9624 | case CK_DerivedToBaseMemberPointer: |
9625 | if (!Visit(E->getSubExpr())) |
9626 | return false; |
9627 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
9628 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
9629 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
9630 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
9631 | if (!Result.castToBase(Base)) |
9632 | return Error(E); |
9633 | } |
9634 | return true; |
9635 | } |
9636 | } |
9637 | |
9638 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
9639 | |
9640 | |
9641 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
9642 | } |
9643 | |
9644 | |
9645 | |
9646 | |
9647 | |
9648 | namespace { |
9649 | class RecordExprEvaluator |
9650 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
9651 | const LValue &This; |
9652 | APValue &Result; |
9653 | public: |
9654 | |
9655 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
9656 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
9657 | |
9658 | bool Success(const APValue &V, const Expr *E) { |
9659 | Result = V; |
9660 | return true; |
9661 | } |
9662 | bool ZeroInitialization(const Expr *E) { |
9663 | return ZeroInitialization(E, E->getType()); |
9664 | } |
9665 | bool ZeroInitialization(const Expr *E, QualType T); |
9666 | |
9667 | bool VisitCallExpr(const CallExpr *E) { |
9668 | return handleCallExpr(E, Result, &This); |
9669 | } |
9670 | bool VisitCastExpr(const CastExpr *E); |
9671 | bool VisitInitListExpr(const InitListExpr *E); |
9672 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
9673 | return VisitCXXConstructExpr(E, E->getType()); |
9674 | } |
9675 | bool VisitLambdaExpr(const LambdaExpr *E); |
9676 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
9677 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
9678 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
9679 | bool VisitBinCmp(const BinaryOperator *E); |
9680 | }; |
9681 | } |
9682 | |
9683 | |
9684 | |
9685 | |
9686 | |
9687 | |
9688 | |
9689 | |
9690 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
9691 | const RecordDecl *RD, |
9692 | const LValue &This, APValue &Result) { |
9693 | assert(!RD->isUnion() && "Expected non-union class type"); |
9694 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
9695 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
9696 | std::distance(RD->field_begin(), RD->field_end())); |
9697 | |
9698 | if (RD->isInvalidDecl()) return false; |
9699 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
9700 | |
9701 | if (CD) { |
9702 | unsigned Index = 0; |
9703 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
9704 | End = CD->bases_end(); I != End; ++I, ++Index) { |
9705 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
9706 | LValue Subobject = This; |
9707 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
9708 | return false; |
9709 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
9710 | Result.getStructBase(Index))) |
9711 | return false; |
9712 | } |
9713 | } |
9714 | |
9715 | for (const auto *I : RD->fields()) { |
9716 | |
9717 | if (I->isUnnamedBitfield() || I->getType()->isReferenceType()) |
9718 | continue; |
9719 | |
9720 | LValue Subobject = This; |
9721 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
9722 | return false; |
9723 | |
9724 | ImplicitValueInitExpr VIE(I->getType()); |
9725 | if (!EvaluateInPlace( |
9726 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
9727 | return false; |
9728 | } |
9729 | |
9730 | return true; |
9731 | } |
9732 | |
9733 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
9734 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
9735 | if (RD->isInvalidDecl()) return false; |
9736 | if (RD->isUnion()) { |
9737 | |
9738 | |
9739 | RecordDecl::field_iterator I = RD->field_begin(); |
9740 | while (I != RD->field_end() && (*I)->isUnnamedBitfield()) |
9741 | ++I; |
9742 | if (I == RD->field_end()) { |
9743 | Result = APValue((const FieldDecl*)nullptr); |
9744 | return true; |
9745 | } |
9746 | |
9747 | LValue Subobject = This; |
9748 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
9749 | return false; |
9750 | Result = APValue(*I); |
9751 | ImplicitValueInitExpr VIE(I->getType()); |
9752 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
9753 | } |
9754 | |
9755 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
9756 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
9757 | return false; |
9758 | } |
9759 | |
9760 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
9761 | } |
9762 | |
9763 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9764 | switch (E->getCastKind()) { |
9765 | default: |
9766 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9767 | |
9768 | case CK_ConstructorConversion: |
9769 | return Visit(E->getSubExpr()); |
9770 | |
9771 | case CK_DerivedToBase: |
9772 | case CK_UncheckedDerivedToBase: { |
9773 | APValue DerivedObject; |
9774 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
9775 | return false; |
9776 | if (!DerivedObject.isStruct()) |
9777 | return Error(E->getSubExpr()); |
9778 | |
9779 | |
9780 | APValue *Value = &DerivedObject; |
9781 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
9782 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
9783 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
9784 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
9785 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
9786 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
9787 | RD = Base; |
9788 | } |
9789 | Result = *Value; |
9790 | return true; |
9791 | } |
9792 | } |
9793 | } |
9794 | |
9795 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
9796 | if (E->isTransparent()) |
9797 | return Visit(E->getInit(0)); |
9798 | |
9799 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
9800 | if (RD->isInvalidDecl()) return false; |
9801 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
9802 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
9803 | |
9804 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
9805 | Info, |
9806 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
9807 | CXXRD && CXXRD->getNumBases()); |
9808 | |
9809 | if (RD->isUnion()) { |
9810 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
9811 | Result = APValue(Field); |
9812 | if (!Field) |
9813 | return true; |
9814 | |
9815 | |
9816 | |
9817 | |
9818 | |
9819 | |
9820 | ImplicitValueInitExpr VIE(Field->getType()); |
9821 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
9822 | |
9823 | LValue Subobject = This; |
9824 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
9825 | return false; |
9826 | |
9827 | |
9828 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
9829 | isa<CXXDefaultInitExpr>(InitExpr)); |
9830 | |
9831 | if (EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr)) { |
9832 | if (Field->isBitField()) |
9833 | return truncateBitfieldValue(Info, InitExpr, Result.getUnionValue(), |
9834 | Field); |
9835 | return true; |
9836 | } |
9837 | |
9838 | return false; |
9839 | } |
9840 | |
9841 | if (!Result.hasValue()) |
9842 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
9843 | std::distance(RD->field_begin(), RD->field_end())); |
9844 | unsigned ElementNo = 0; |
9845 | bool Success = true; |
9846 | |
9847 | |
9848 | if (CXXRD && CXXRD->getNumBases()) { |
9849 | for (const auto &Base : CXXRD->bases()) { |
9850 | assert(ElementNo < E->getNumInits() && "missing init for base class"); |
9851 | const Expr *Init = E->getInit(ElementNo); |
9852 | |
9853 | LValue Subobject = This; |
9854 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
9855 | return false; |
9856 | |
9857 | APValue &FieldVal = Result.getStructBase(ElementNo); |
9858 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
9859 | if (!Info.noteFailure()) |
9860 | return false; |
9861 | Success = false; |
9862 | } |
9863 | ++ElementNo; |
9864 | } |
9865 | |
9866 | EvalObj.finishedConstructingBases(); |
9867 | } |
9868 | |
9869 | |
9870 | for (const auto *Field : RD->fields()) { |
9871 | |
9872 | |
9873 | if (Field->isUnnamedBitfield()) |
9874 | continue; |
9875 | |
9876 | LValue Subobject = This; |
9877 | |
9878 | bool HaveInit = ElementNo < E->getNumInits(); |
9879 | |
9880 | |
9881 | |
9882 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
9883 | Subobject, Field, &Layout)) |
9884 | return false; |
9885 | |
9886 | |
9887 | |
9888 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
9889 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
9890 | |
9891 | |
9892 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
9893 | isa<CXXDefaultInitExpr>(Init)); |
9894 | |
9895 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
9896 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
9897 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
9898 | FieldVal, Field))) { |
9899 | if (!Info.noteFailure()) |
9900 | return false; |
9901 | Success = false; |
9902 | } |
9903 | } |
9904 | |
9905 | EvalObj.finishedConstructingFields(); |
9906 | |
9907 | return Success; |
9908 | } |
9909 | |
9910 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
9911 | QualType T) { |
9912 | |
9913 | |
9914 | const CXXConstructorDecl *FD = E->getConstructor(); |
9915 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
9916 | |
9917 | bool ZeroInit = E->requiresZeroInitialization(); |
9918 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
9919 | |
9920 | if (Result.hasValue()) |
9921 | return true; |
9922 | |
9923 | if (ZeroInit) |
9924 | return ZeroInitialization(E, T); |
9925 | |
9926 | return getDefaultInitValue(T, Result); |
9927 | } |
9928 | |
9929 | const FunctionDecl *Definition = nullptr; |
9930 | auto Body = FD->getBody(Definition); |
9931 | |
9932 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
9933 | return false; |
9934 | |
9935 | |
9936 | if (E->isElidable() && !ZeroInit) |
9937 | if (const MaterializeTemporaryExpr *ME |
9938 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
9939 | return Visit(ME->getSubExpr()); |
9940 | |
9941 | if (ZeroInit && !ZeroInitialization(E, T)) |
9942 | return false; |
9943 | |
9944 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
9945 | return HandleConstructorCall(E, This, Args, |
9946 | cast<CXXConstructorDecl>(Definition), Info, |
9947 | Result); |
9948 | } |
9949 | |
9950 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
9951 | const CXXInheritedCtorInitExpr *E) { |
9952 | if (!Info.CurrentCall) { |
9953 | assert(Info.checkingPotentialConstantExpression()); |
9954 | return false; |
9955 | } |
9956 | |
9957 | const CXXConstructorDecl *FD = E->getConstructor(); |
9958 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
9959 | return false; |
9960 | |
9961 | const FunctionDecl *Definition = nullptr; |
9962 | auto Body = FD->getBody(Definition); |
9963 | |
9964 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
9965 | return false; |
9966 | |
9967 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
9968 | cast<CXXConstructorDecl>(Definition), Info, |
9969 | Result); |
9970 | } |
9971 | |
9972 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
9973 | const CXXStdInitializerListExpr *E) { |
9974 | const ConstantArrayType *ArrayType = |
9975 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
9976 | |
9977 | LValue Array; |
9978 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
9979 | return false; |
9980 | |
9981 | |
9982 | Array.addArray(Info, E, ArrayType); |
9983 | |
9984 | auto InvalidType = [&] { |
9985 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
9986 | << E->getType(); |
9987 | return false; |
9988 | }; |
9989 | |
9990 | |
9991 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
9992 | RecordDecl::field_iterator Field = Record->field_begin(); |
9993 | if (Field == Record->field_end()) |
9994 | return InvalidType(); |
9995 | |
9996 | |
9997 | if (!Field->getType()->isPointerType() || |
9998 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
9999 | ArrayType->getElementType())) |
10000 | return InvalidType(); |
10001 | |
10002 | |
10003 | Result = APValue(APValue::UninitStruct(), 0, 2); |
10004 | Array.moveInto(Result.getStructField(0)); |
10005 | |
10006 | if (++Field == Record->field_end()) |
10007 | return InvalidType(); |
10008 | |
10009 | if (Field->getType()->isPointerType() && |
10010 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10011 | ArrayType->getElementType())) { |
10012 | |
10013 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
10014 | ArrayType->getElementType(), |
10015 | ArrayType->getSize().getZExtValue())) |
10016 | return false; |
10017 | Array.moveInto(Result.getStructField(1)); |
10018 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
10019 | |
10020 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
10021 | else |
10022 | return InvalidType(); |
10023 | |
10024 | if (++Field != Record->field_end()) |
10025 | return InvalidType(); |
10026 | |
10027 | return true; |
10028 | } |
10029 | |
10030 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
10031 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
10032 | if (ClosureClass->isInvalidDecl()) |
10033 | return false; |
10034 | |
10035 | const size_t NumFields = |
10036 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
10037 | |
10038 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
10039 | E->capture_init_end()) && |
10040 | "The number of lambda capture initializers should equal the number of " |
10041 | "fields within the closure type"); |
10042 | |
10043 | Result = APValue(APValue::UninitStruct(), 0, NumFields); |
10044 | |
10045 | |
10046 | auto *CaptureInitIt = E->capture_init_begin(); |
10047 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
10048 | bool Success = true; |
10049 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass); |
10050 | for (const auto *Field : ClosureClass->fields()) { |
10051 | assert(CaptureInitIt != E->capture_init_end()); |
10052 | |
10053 | Expr *const CurFieldInit = *CaptureInitIt++; |
10054 | |
10055 | |
10056 | |
10057 | if (!CurFieldInit) |
10058 | return Error(E); |
10059 | |
10060 | LValue Subobject = This; |
10061 | |
10062 | if (!HandleLValueMember(Info, E, Subobject, Field, &Layout)) |
10063 | return false; |
10064 | |
10065 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
10066 | if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) { |
10067 | if (!Info.keepEvaluatingAfterFailure()) |
10068 | return false; |
10069 | Success = false; |
10070 | } |
10071 | ++CaptureIt; |
10072 | } |
10073 | return Success; |
10074 | } |
10075 | |
10076 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
10077 | APValue &Result, EvalInfo &Info) { |
10078 | assert(!E->isValueDependent()); |
10079 | assert(E->isPRValue() && E->getType()->isRecordType() && |
10080 | "can't evaluate expression as a record rvalue"); |
10081 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
10082 | } |
10083 | |
10084 | |
10085 | |
10086 | |
10087 | |
10088 | |
10089 | |
10090 | |
10091 | namespace { |
10092 | class TemporaryExprEvaluator |
10093 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
10094 | public: |
10095 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
10096 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
10097 | |
10098 | |
10099 | bool VisitConstructExpr(const Expr *E) { |
10100 | APValue &Value = Info.CurrentCall->createTemporary( |
10101 | E, E->getType(), ScopeKind::FullExpression, Result); |
10102 | return EvaluateInPlace(Value, Info, Result, E); |
10103 | } |
10104 | |
10105 | bool VisitCastExpr(const CastExpr *E) { |
10106 | switch (E->getCastKind()) { |
10107 | default: |
10108 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
10109 | |
10110 | case CK_ConstructorConversion: |
10111 | return VisitConstructExpr(E->getSubExpr()); |
10112 | } |
10113 | } |
10114 | bool VisitInitListExpr(const InitListExpr *E) { |
10115 | return VisitConstructExpr(E); |
10116 | } |
10117 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10118 | return VisitConstructExpr(E); |
10119 | } |
10120 | bool VisitCallExpr(const CallExpr *E) { |
10121 | return VisitConstructExpr(E); |
10122 | } |
10123 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
10124 | return VisitConstructExpr(E); |
10125 | } |
10126 | bool VisitLambdaExpr(const LambdaExpr *E) { |
10127 | return VisitConstructExpr(E); |
10128 | } |
10129 | }; |
10130 | } |
10131 | |
10132 | |
10133 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
10134 | assert(!E->isValueDependent()); |
10135 | assert(E->isPRValue() && E->getType()->isRecordType()); |
10136 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
10137 | } |
10138 | |
10139 | |
10140 | |
10141 | |
10142 | |
10143 | namespace { |
10144 | class VectorExprEvaluator |
10145 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
10146 | APValue &Result; |
10147 | public: |
10148 | |
10149 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
10150 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
10151 | |
10152 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
10153 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
10154 | |
10155 | Result = APValue(V.data(), V.size()); |
10156 | return true; |
10157 | } |
10158 | bool Success(const APValue &V, const Expr *E) { |
10159 | assert(V.isVector()); |
10160 | Result = V; |
10161 | return true; |
10162 | } |
10163 | bool ZeroInitialization(const Expr *E); |
10164 | |
10165 | bool VisitUnaryReal(const UnaryOperator *E) |
10166 | { return Visit(E->getSubExpr()); } |
10167 | bool VisitCastExpr(const CastExpr* E); |
10168 | bool VisitInitListExpr(const InitListExpr *E); |
10169 | bool VisitUnaryImag(const UnaryOperator *E); |
10170 | bool VisitBinaryOperator(const BinaryOperator *E); |
10171 | |
10172 | |
10173 | }; |
10174 | } |
10175 | |
10176 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
10177 | assert(E->isPRValue() && E->getType()->isVectorType() && |
10178 | "not a vector prvalue"); |
10179 | return VectorExprEvaluator(Info, Result).Visit(E); |
10180 | } |
10181 | |
10182 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10183 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
10184 | unsigned NElts = VTy->getNumElements(); |
10185 | |
10186 | const Expr *SE = E->getSubExpr(); |
10187 | QualType SETy = SE->getType(); |
10188 | |
10189 | switch (E->getCastKind()) { |
10190 | case CK_VectorSplat: { |
10191 | APValue Val = APValue(); |
10192 | if (SETy->isIntegerType()) { |
10193 | APSInt IntResult; |
10194 | if (!EvaluateInteger(SE, IntResult, Info)) |
10195 | return false; |
10196 | Val = APValue(std::move(IntResult)); |
10197 | } else if (SETy->isRealFloatingType()) { |
10198 | APFloat FloatResult(0.0); |
10199 | if (!EvaluateFloat(SE, FloatResult, Info)) |
10200 | return false; |
10201 | Val = APValue(std::move(FloatResult)); |
10202 | } else { |
10203 | return Error(E); |
10204 | } |
10205 | |
10206 | |
10207 | SmallVector<APValue, 4> Elts(NElts, Val); |
10208 | return Success(Elts, E); |
10209 | } |
10210 | case CK_BitCast: { |
10211 | |
10212 | llvm::APInt SValInt; |
10213 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
10214 | return false; |
10215 | |
10216 | QualType EltTy = VTy->getElementType(); |
10217 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
10218 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
10219 | SmallVector<APValue, 4> Elts; |
10220 | if (EltTy->isRealFloatingType()) { |
10221 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
10222 | unsigned FloatEltSize = EltSize; |
10223 | if (&Sem == &APFloat::x87DoubleExtended()) |
10224 | FloatEltSize = 80; |
10225 | for (unsigned i = 0; i < NElts; i++) { |
10226 | llvm::APInt Elt; |
10227 | if (BigEndian) |
10228 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
10229 | else |
10230 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
10231 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
10232 | } |
10233 | } else if (EltTy->isIntegerType()) { |
10234 | for (unsigned i = 0; i < NElts; i++) { |
10235 | llvm::APInt Elt; |
10236 | if (BigEndian) |
10237 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
10238 | else |
10239 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
10240 | Elts.push_back(APValue(APSInt(Elt, !EltTy->isSignedIntegerType()))); |
10241 | } |
10242 | } else { |
10243 | return Error(E); |
10244 | } |
10245 | return Success(Elts, E); |
10246 | } |
10247 | default: |
10248 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10249 | } |
10250 | } |
10251 | |
10252 | bool |
10253 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10254 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
10255 | unsigned NumInits = E->getNumInits(); |
10256 | unsigned NumElements = VT->getNumElements(); |
10257 | |
10258 | QualType EltTy = VT->getElementType(); |
10259 | SmallVector<APValue, 4> Elements; |
10260 | |
10261 | |
10262 | |
10263 | |
10264 | |
10265 | unsigned CountInits = 0, CountElts = 0; |
10266 | while (CountElts < NumElements) { |
10267 | |
10268 | if (CountInits < NumInits |
10269 | && E->getInit(CountInits)->getType()->isVectorType()) { |
10270 | APValue v; |
10271 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
10272 | return Error(E); |
10273 | unsigned vlen = v.getVectorLength(); |
10274 | for (unsigned j = 0; j < vlen; j++) |
10275 | Elements.push_back(v.getVectorElt(j)); |
10276 | CountElts += vlen; |
10277 | } else if (EltTy->isIntegerType()) { |
10278 | llvm::APSInt sInt(32); |
10279 | if (CountInits < NumInits) { |
10280 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
10281 | return false; |
10282 | } else |
10283 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
10284 | Elements.push_back(APValue(sInt)); |
10285 | CountElts++; |
10286 | } else { |
10287 | llvm::APFloat f(0.0); |
10288 | if (CountInits < NumInits) { |
10289 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
10290 | return false; |
10291 | } else |
10292 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
10293 | Elements.push_back(APValue(f)); |
10294 | CountElts++; |
10295 | } |
10296 | CountInits++; |
10297 | } |
10298 | return Success(Elements, E); |
10299 | } |
10300 | |
10301 | bool |
10302 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
10303 | const auto *VT = E->getType()->castAs<VectorType>(); |
10304 | QualType EltTy = VT->getElementType(); |
10305 | APValue ZeroElement; |
10306 | if (EltTy->isIntegerType()) |
10307 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
10308 | else |
10309 | ZeroElement = |
10310 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
10311 | |
10312 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
10313 | return Success(Elements, E); |
10314 | } |
10315 | |
10316 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
10317 | VisitIgnoredValue(E->getSubExpr()); |
10318 | return ZeroInitialization(E); |
10319 | } |
10320 | |
10321 | bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10322 | BinaryOperatorKind Op = E->getOpcode(); |
10323 | assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp && |
10324 | "Operation not supported on vector types"); |
10325 | |
10326 | if (Op == BO_Comma) |
10327 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10328 | |
10329 | Expr *LHS = E->getLHS(); |
10330 | Expr *RHS = E->getRHS(); |
10331 | |
10332 | assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() && |
10333 | "Must both be vector types"); |
10334 | |
10335 | |
10336 | assert(LHS->getType()->castAs<VectorType>()->getNumElements() == |
10337 | E->getType()->castAs<VectorType>()->getNumElements() && |
10338 | RHS->getType()->castAs<VectorType>()->getNumElements() == |
10339 | E->getType()->castAs<VectorType>()->getNumElements() && |
10340 | "All operands must be the same size."); |
10341 | |
10342 | APValue LHSValue; |
10343 | APValue RHSValue; |
10344 | bool LHSOK = Evaluate(LHSValue, Info, LHS); |
10345 | if (!LHSOK && !Info.noteFailure()) |
10346 | return false; |
10347 | if (!Evaluate(RHSValue, Info, RHS) || !LHSOK) |
10348 | return false; |
10349 | |
10350 | if (!handleVectorVectorBinOp(Info, E, Op, LHSValue, RHSValue)) |
10351 | return false; |
10352 | |
10353 | return Success(LHSValue, E); |
10354 | } |
10355 | |
10356 | |
10357 | |
10358 | |
10359 | |
10360 | namespace { |
10361 | class ArrayExprEvaluator |
10362 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
10363 | const LValue &This; |
10364 | APValue &Result; |
10365 | public: |
10366 | |
10367 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
10368 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
10369 | |
10370 | bool Success(const APValue &V, const Expr *E) { |
10371 | assert(V.isArray() && "expected array"); |
10372 | Result = V; |
10373 | return true; |
10374 | } |
10375 | |
10376 | bool ZeroInitialization(const Expr *E) { |
10377 | const ConstantArrayType *CAT = |
10378 | Info.Ctx.getAsConstantArrayType(E->getType()); |
10379 | if (!CAT) { |
10380 | if (E->getType()->isIncompleteArrayType()) { |
10381 | |
10382 | |
10383 | |
10384 | Result = APValue(APValue::UninitArray(), 0, 0); |
10385 | return true; |
10386 | } |
10387 | |
10388 | return Error(E); |
10389 | } |
10390 | |
10391 | Result = APValue(APValue::UninitArray(), 0, |
10392 | CAT->getSize().getZExtValue()); |
10393 | if (!Result.hasArrayFiller()) |
10394 | return true; |
10395 | |
10396 | |
10397 | LValue Subobject = This; |
10398 | Subobject.addArray(Info, E, CAT); |
10399 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
10400 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
10401 | } |
10402 | |
10403 | bool VisitCallExpr(const CallExpr *E) { |
10404 | return handleCallExpr(E, Result, &This); |
| 1 | Calling 'ExprEvaluatorBase::handleCallExpr' | |
|
10405 | } |
10406 | bool VisitInitListExpr(const InitListExpr *E, |
10407 | QualType AllocType = QualType()); |
10408 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
10409 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
10410 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
10411 | const LValue &Subobject, |
10412 | APValue *Value, QualType Type); |
10413 | bool VisitStringLiteral(const StringLiteral *E, |
10414 | QualType AllocType = QualType()) { |
10415 | expandStringLiteral(Info, E, Result, AllocType); |
10416 | return true; |
10417 | } |
10418 | }; |
10419 | } |
10420 | |
10421 | static bool EvaluateArray(const Expr *E, const LValue &This, |
10422 | APValue &Result, EvalInfo &Info) { |
10423 | assert(!E->isValueDependent()); |
10424 | assert(E->isPRValue() && E->getType()->isArrayType() && |
10425 | "not an array prvalue"); |
10426 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
10427 | } |
10428 | |
10429 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
10430 | APValue &Result, const InitListExpr *ILE, |
10431 | QualType AllocType) { |
10432 | assert(!ILE->isValueDependent()); |
10433 | assert(ILE->isPRValue() && ILE->getType()->isArrayType() && |
10434 | "not an array prvalue"); |
10435 | return ArrayExprEvaluator(Info, This, Result) |
10436 | .VisitInitListExpr(ILE, AllocType); |
10437 | } |
10438 | |
10439 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
10440 | APValue &Result, |
10441 | const CXXConstructExpr *CCE, |
10442 | QualType AllocType) { |
10443 | assert(!CCE->isValueDependent()); |
10444 | assert(CCE->isPRValue() && CCE->getType()->isArrayType() && |
10445 | "not an array prvalue"); |
10446 | return ArrayExprEvaluator(Info, This, Result) |
10447 | .VisitCXXConstructExpr(CCE, This, &Result, AllocType); |
10448 | } |
10449 | |
10450 | |
10451 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
10452 | |
10453 | |
10454 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
10455 | return false; |
10456 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
10457 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
10458 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
10459 | return true; |
10460 | } |
10461 | return false; |
10462 | } |
10463 | return true; |
10464 | } |
10465 | |
10466 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
10467 | QualType AllocType) { |
10468 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
10469 | AllocType.isNull() ? E->getType() : AllocType); |
10470 | if (!CAT) |
10471 | return Error(E); |
10472 | |
10473 | |
10474 | |
10475 | if (E->isStringLiteralInit()) { |
10476 | auto *SL = dyn_cast<StringLiteral>(E->getInit(0)->IgnoreParenImpCasts()); |
10477 | |
10478 | |
10479 | if (!SL) |
10480 | return Error(E); |
10481 | return VisitStringLiteral(SL, AllocType); |
10482 | } |
10483 | |
10484 | |
10485 | assert(!E->isTransparent() && |
10486 | "transparent array list initialization is not string literal init?"); |
10487 | |
10488 | bool Success = true; |
10489 | |
10490 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
10491 | "zero-initialized array shouldn't have any initialized elts"); |
10492 | APValue Filler; |
10493 | if (Result.isArray() && Result.hasArrayFiller()) |
10494 | Filler = Result.getArrayFiller(); |
10495 | |
10496 | unsigned NumEltsToInit = E->getNumInits(); |
10497 | unsigned NumElts = CAT->getSize().getZExtValue(); |
10498 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
10499 | |
10500 | |
10501 | |
10502 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
10503 | NumEltsToInit = NumElts; |
10504 | |
10505 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
10506 | << NumEltsToInit << ".\n"); |
10507 | |
10508 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
10509 | |
10510 | |
10511 | |
10512 | if (Filler.hasValue()) { |
10513 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
10514 | Result.getArrayInitializedElt(I) = Filler; |
10515 | if (Result.hasArrayFiller()) |
10516 | Result.getArrayFiller() = Filler; |
10517 | } |
10518 | |
10519 | LValue Subobject = This; |
10520 | Subobject.addArray(Info, E, CAT); |
10521 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
10522 | const Expr *Init = |
10523 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
10524 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
10525 | Info, Subobject, Init) || |
10526 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
10527 | CAT->getElementType(), 1)) { |
10528 | if (!Info.noteFailure()) |
10529 | return false; |
10530 | Success = false; |
10531 | } |
10532 | } |
10533 | |
10534 | if (!Result.hasArrayFiller()) |
10535 | return Success; |
10536 | |
10537 | |
10538 | |
10539 | assert(FillerExpr && "no array filler for incomplete init list"); |
10540 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
10541 | FillerExpr) && Success; |
10542 | } |
10543 | |
10544 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
10545 | LValue CommonLV; |
10546 | if (E->getCommonExpr() && |
10547 | !Evaluate(Info.CurrentCall->createTemporary( |
10548 | E->getCommonExpr(), |
10549 | getStorageType(Info.Ctx, E->getCommonExpr()), |
10550 | ScopeKind::FullExpression, CommonLV), |
10551 | Info, E->getCommonExpr()->getSourceExpr())) |
10552 | return false; |
10553 | |
10554 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
10555 | |
10556 | uint64_t Elements = CAT->getSize().getZExtValue(); |
10557 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
10558 | |
10559 | LValue Subobject = This; |
10560 | Subobject.addArray(Info, E, CAT); |
10561 | |
10562 | bool Success = true; |
10563 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
10564 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
10565 | Info, Subobject, E->getSubExpr()) || |
10566 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
10567 | CAT->getElementType(), 1)) { |
10568 | if (!Info.noteFailure()) |
10569 | return false; |
10570 | Success = false; |
10571 | } |
10572 | } |
10573 | |
10574 | return Success; |
10575 | } |
10576 | |
10577 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10578 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
10579 | } |
10580 | |
10581 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
10582 | const LValue &Subobject, |
10583 | APValue *Value, |
10584 | QualType Type) { |
10585 | bool HadZeroInit = Value->hasValue(); |
10586 | |
10587 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
10588 | unsigned N = CAT->getSize().getZExtValue(); |
10589 | |
10590 | |
10591 | APValue Filler = |
10592 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
10593 | : APValue(); |
10594 | |
10595 | *Value = APValue(APValue::UninitArray(), N, N); |
10596 | |
10597 | if (HadZeroInit) |
10598 | for (unsigned I = 0; I != N; ++I) |
10599 | Value->getArrayInitializedElt(I) = Filler; |
10600 | |
10601 | |
10602 | LValue ArrayElt = Subobject; |
10603 | ArrayElt.addArray(Info, E, CAT); |
10604 | for (unsigned I = 0; I != N; ++I) |
10605 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
10606 | CAT->getElementType()) || |
10607 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
10608 | CAT->getElementType(), 1)) |
10609 | return false; |
10610 | |
10611 | return true; |
10612 | } |
10613 | |
10614 | if (!Type->isRecordType()) |
10615 | return Error(E); |
10616 | |
10617 | return RecordExprEvaluator(Info, Subobject, *Value) |
10618 | .VisitCXXConstructExpr(E, Type); |
10619 | } |
10620 | |
10621 | |
10622 | |
10623 | |
10624 | |
10625 | |
10626 | |
10627 | |
10628 | |
10629 | namespace { |
10630 | class IntExprEvaluator |
10631 | : public ExprEvaluatorBase<IntExprEvaluator> { |
10632 | APValue &Result; |
10633 | public: |
10634 | IntExprEvaluator(EvalInfo &info, APValue &result) |
10635 | : ExprEvaluatorBaseTy(info), Result(result) {} |
10636 | |
10637 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
10638 | assert(E->getType()->isIntegralOrEnumerationType() && |
10639 | "Invalid evaluation result."); |
10640 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
10641 | "Invalid evaluation result."); |
10642 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10643 | "Invalid evaluation result."); |
10644 | Result = APValue(SI); |
10645 | return true; |
10646 | } |
10647 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
10648 | return Success(SI, E, Result); |
10649 | } |
10650 | |
10651 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
10652 | assert(E->getType()->isIntegralOrEnumerationType() && |
10653 | "Invalid evaluation result."); |
10654 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10655 | "Invalid evaluation result."); |
10656 | Result = APValue(APSInt(I)); |
10657 | Result.getInt().setIsUnsigned( |
10658 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
10659 | return true; |
10660 | } |
10661 | bool Success(const llvm::APInt &I, const Expr *E) { |
10662 | return Success(I, E, Result); |
10663 | } |
10664 | |
10665 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
10666 | assert(E->getType()->isIntegralOrEnumerationType() && |
10667 | "Invalid evaluation result."); |
10668 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
10669 | return true; |
10670 | } |
10671 | bool Success(uint64_t Value, const Expr *E) { |
10672 | return Success(Value, E, Result); |
10673 | } |
10674 | |
10675 | bool Success(CharUnits Size, const Expr *E) { |
10676 | return Success(Size.getQuantity(), E); |
10677 | } |
10678 | |
10679 | bool Success(const APValue &V, const Expr *E) { |
10680 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate()) { |
10681 | Result = V; |
10682 | return true; |
10683 | } |
10684 | return Success(V.getInt(), E); |
10685 | } |
10686 | |
10687 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
10688 | |
10689 | |
10690 | |
10691 | |
10692 | |
10693 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
10694 | return Success(E->getValue(), E); |
10695 | } |
10696 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
10697 | return Success(E->getValue(), E); |
10698 | } |
10699 | |
10700 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
10701 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
10702 | if (CheckReferencedDecl(E, E->getDecl())) |
10703 | return true; |
10704 | |
10705 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
10706 | } |
10707 | bool VisitMemberExpr(const MemberExpr *E) { |
10708 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
10709 | VisitIgnoredBaseExpression(E->getBase()); |
10710 | return true; |
10711 | } |
10712 | |
10713 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
10714 | } |
10715 | |
10716 | bool VisitCallExpr(const CallExpr *E); |
10717 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
10718 | bool VisitBinaryOperator(const BinaryOperator *E); |
10719 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
10720 | bool VisitUnaryOperator(const UnaryOperator *E); |
10721 | |
10722 | bool VisitCastExpr(const CastExpr* E); |
10723 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
10724 | |
10725 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
10726 | return Success(E->getValue(), E); |
10727 | } |
10728 | |
10729 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
10730 | return Success(E->getValue(), E); |
10731 | } |
10732 | |
10733 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
10734 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
10735 | |
10736 | |
10737 | Info.FFDiag(E); |
10738 | return false; |
10739 | } |
10740 | return Success(Info.ArrayInitIndex, E); |
10741 | } |
10742 | |
10743 | |
10744 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
10745 | return ZeroInitialization(E); |
10746 | } |
10747 | |
10748 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
10749 | return Success(E->getValue(), E); |
10750 | } |
10751 | |
10752 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
10753 | return Success(E->getValue(), E); |
10754 | } |
10755 | |
10756 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
10757 | return Success(E->getValue(), E); |
10758 | } |
10759 | |
10760 | bool VisitUnaryReal(const UnaryOperator *E); |
10761 | bool VisitUnaryImag(const UnaryOperator *E); |
10762 | |
10763 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
10764 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
10765 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
10766 | bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E); |
10767 | bool VisitRequiresExpr(const RequiresExpr *E); |
10768 | |
10769 | }; |
10770 | |
10771 | class FixedPointExprEvaluator |
10772 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
10773 | APValue &Result; |
10774 | |
10775 | public: |
10776 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
10777 | : ExprEvaluatorBaseTy(info), Result(result) {} |
10778 | |
10779 | bool Success(const llvm::APInt &I, const Expr *E) { |
10780 | return Success( |
10781 | APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
10782 | } |
10783 | |
10784 | bool Success(uint64_t Value, const Expr *E) { |
10785 | return Success( |
10786 | APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
10787 | } |
10788 | |
10789 | bool Success(const APValue &V, const Expr *E) { |
10790 | return Success(V.getFixedPoint(), E); |
10791 | } |
10792 | |
10793 | bool Success(const APFixedPoint &V, const Expr *E) { |
10794 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
10795 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
10796 | "Invalid evaluation result."); |
10797 | Result = APValue(V); |
10798 | return true; |
10799 | } |
10800 | |
10801 | |
10802 | |
10803 | |
10804 | |
10805 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
10806 | return Success(E->getValue(), E); |
10807 | } |
10808 | |
10809 | bool VisitCastExpr(const CastExpr *E); |
10810 | bool VisitUnaryOperator(const UnaryOperator *E); |
10811 | bool VisitBinaryOperator(const BinaryOperator *E); |
10812 | }; |
10813 | } |
10814 | |
10815 | |
10816 | |
10817 | |
10818 | |
10819 | |
10820 | |
10821 | |
10822 | |
10823 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
10824 | EvalInfo &Info) { |
10825 | assert(!E->isValueDependent()); |
10826 | assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType()); |
10827 | return IntExprEvaluator(Info, Result).Visit(E); |
10828 | } |
10829 | |
10830 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
10831 | assert(!E->isValueDependent()); |
10832 | APValue Val; |
10833 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
10834 | return false; |
10835 | if (!Val.isInt()) { |
10836 | |
10837 | |
10838 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
10839 | return false; |
10840 | } |
10841 | Result = Val.getInt(); |
10842 | return true; |
10843 | } |
10844 | |
10845 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
10846 | APValue Evaluated = E->EvaluateInContext( |
10847 | Info.Ctx, Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
10848 | return Success(Evaluated, E); |
10849 | } |
10850 | |
10851 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
10852 | EvalInfo &Info) { |
10853 | assert(!E->isValueDependent()); |
10854 | if (E->getType()->isFixedPointType()) { |
10855 | APValue Val; |
10856 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
10857 | return false; |
10858 | if (!Val.isFixedPoint()) |
10859 | return false; |
10860 | |
10861 | Result = Val.getFixedPoint(); |
10862 | return true; |
10863 | } |
10864 | return false; |
10865 | } |
10866 | |
10867 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
10868 | EvalInfo &Info) { |
10869 | assert(!E->isValueDependent()); |
10870 | if (E->getType()->isIntegerType()) { |
10871 | auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType()); |
10872 | APSInt Val; |
10873 | if (!EvaluateInteger(E, Val, Info)) |
10874 | return false; |
10875 | Result = APFixedPoint(Val, FXSema); |
10876 | return true; |
10877 | } else if (E->getType()->isFixedPointType()) { |
10878 | return EvaluateFixedPoint(E, Result, Info); |
10879 | } |
10880 | return false; |
10881 | } |
10882 | |
10883 | |
10884 | |
10885 | |
10886 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
10887 | |
10888 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
10889 | |
10890 | bool SameSign = (ECD->getInitVal().isSigned() |
10891 | == E->getType()->isSignedIntegerOrEnumerationType()); |
10892 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
10893 | == Info.Ctx.getIntWidth(E->getType())); |
10894 | if (SameSign && SameWidth) |
10895 | return Success(ECD->getInitVal(), E); |
10896 | else { |
10897 | |
10898 | |
10899 | llvm::APSInt Val = ECD->getInitVal(); |
10900 | if (!SameSign) |
10901 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
10902 | if (!SameWidth) |
10903 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
10904 | return Success(Val, E); |
10905 | } |
10906 | } |
10907 | return false; |
10908 | } |
10909 | |
10910 | |
10911 | |
10912 | enum class GCCTypeClass { |
10913 | None = -1, |
10914 | Void = 0, |
10915 | Integer = 1, |
10916 | |
10917 | |
10918 | Enum = 3, |
10919 | Bool = 4, |
10920 | Pointer = 5, |
10921 | |
10922 | |
10923 | PointerToDataMember = 7, |
10924 | RealFloat = 8, |
10925 | Complex = 9, |
10926 | |
10927 | |
10928 | |
10929 | |
10930 | |
10931 | PointerToMemberFunction = 12, |
10932 | ClassOrStruct = 12, |
10933 | Union = 13, |
10934 | |
10935 | |
10936 | |
10937 | |
10938 | }; |
10939 | |
10940 | |
10941 | |
10942 | static GCCTypeClass |
10943 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
10944 | assert(!T->isDependentType() && "unexpected dependent type"); |
10945 | |
10946 | QualType CanTy = T.getCanonicalType(); |
10947 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
10948 | |
10949 | switch (CanTy->getTypeClass()) { |
10950 | #define TYPE(ID, BASE) |
10951 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
10952 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
10953 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
10954 | #include "clang/AST/TypeNodes.inc" |
10955 | case Type::Auto: |
10956 | case Type::DeducedTemplateSpecialization: |
10957 | llvm_unreachable("unexpected non-canonical or dependent type"); |
10958 | |
10959 | case Type::Builtin: |
10960 | switch (BT->getKind()) { |
10961 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
10962 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
10963 | case BuiltinType::ID: return GCCTypeClass::Integer; |
10964 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
10965 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
10966 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
10967 | case BuiltinType::ID: break; |
10968 | #include "clang/AST/BuiltinTypes.def" |
10969 | case BuiltinType::Void: |
10970 | return GCCTypeClass::Void; |
10971 | |
10972 | case BuiltinType::Bool: |
10973 | return GCCTypeClass::Bool; |
10974 | |
10975 | case BuiltinType::Char_U: |
10976 | case BuiltinType::UChar: |
10977 | case BuiltinType::WChar_U: |
10978 | case BuiltinType::Char8: |
10979 | case BuiltinType::Char16: |
10980 | case BuiltinType::Char32: |
10981 | case BuiltinType::UShort: |
10982 | case BuiltinType::UInt: |
10983 | case BuiltinType::ULong: |
10984 | case BuiltinType::ULongLong: |
10985 | case BuiltinType::UInt128: |
10986 | return GCCTypeClass::Integer; |
10987 | |
10988 | case BuiltinType::UShortAccum: |
10989 | case BuiltinType::UAccum: |
10990 | case BuiltinType::ULongAccum: |
10991 | case BuiltinType::UShortFract: |
10992 | case BuiltinType::UFract: |
10993 | case BuiltinType::ULongFract: |
10994 | case BuiltinType::SatUShortAccum: |
10995 | case BuiltinType::SatUAccum: |
10996 | case BuiltinType::SatULongAccum: |
10997 | case BuiltinType::SatUShortFract: |
10998 | case BuiltinType::SatUFract: |
10999 | case BuiltinType::SatULongFract: |
11000 | return GCCTypeClass::None; |
11001 | |
11002 | case BuiltinType::NullPtr: |
11003 | |
11004 | case BuiltinType::ObjCId: |
11005 | case BuiltinType::ObjCClass: |
11006 | case BuiltinType::ObjCSel: |
11007 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
11008 | case BuiltinType::Id: |
11009 | #include "clang/Basic/OpenCLImageTypes.def" |
11010 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
11011 | case BuiltinType::Id: |
11012 | #include "clang/Basic/OpenCLExtensionTypes.def" |
11013 | case BuiltinType::OCLSampler: |
11014 | case BuiltinType::OCLEvent: |
11015 | case BuiltinType::OCLClkEvent: |
11016 | case BuiltinType::OCLQueue: |
11017 | case BuiltinType::OCLReserveID: |
11018 | #define SVE_TYPE(Name, Id, SingletonId) \ |
11019 | case BuiltinType::Id: |
11020 | #include "clang/Basic/AArch64SVEACLETypes.def" |
11021 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
11022 | case BuiltinType::Id: |
11023 | #include "clang/Basic/PPCTypes.def" |
11024 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11025 | #include "clang/Basic/RISCVVTypes.def" |
11026 | return GCCTypeClass::None; |
11027 | |
11028 | case BuiltinType::Dependent: |
11029 | llvm_unreachable("unexpected dependent type"); |
11030 | }; |
11031 | llvm_unreachable("unexpected placeholder type"); |
11032 | |
11033 | case Type::Enum: |
11034 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
11035 | |
11036 | case Type::Pointer: |
11037 | case Type::ConstantArray: |
11038 | case Type::VariableArray: |
11039 | case Type::IncompleteArray: |
11040 | case Type::FunctionNoProto: |
11041 | case Type::FunctionProto: |
11042 | return GCCTypeClass::Pointer; |
11043 | |
11044 | case Type::MemberPointer: |
11045 | return CanTy->isMemberDataPointerType() |
11046 | ? GCCTypeClass::PointerToDataMember |
11047 | : GCCTypeClass::PointerToMemberFunction; |
11048 | |
11049 | case Type::Complex: |
11050 | return GCCTypeClass::Complex; |
11051 | |
11052 | case Type::Record: |
11053 | return CanTy->isUnionType() ? GCCTypeClass::Union |
11054 | : GCCTypeClass::ClassOrStruct; |
11055 | |
11056 | case Type::Atomic: |
11057 | |
11058 | return EvaluateBuiltinClassifyType( |
11059 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
11060 | |
11061 | case Type::BlockPointer: |
11062 | case Type::Vector: |
11063 | case Type::ExtVector: |
11064 | case Type::ConstantMatrix: |
11065 | case Type::ObjCObject: |
11066 | case Type::ObjCInterface: |
11067 | case Type::ObjCObjectPointer: |
11068 | case Type::Pipe: |
11069 | case Type::ExtInt: |
11070 | |
11071 | |
11072 | return GCCTypeClass::None; |
11073 | |
11074 | case Type::LValueReference: |
11075 | case Type::RValueReference: |
11076 | llvm_unreachable("invalid type for expression"); |
11077 | } |
11078 | |
11079 | llvm_unreachable("unexpected type class"); |
11080 | } |
11081 | |
11082 | |
11083 | |
11084 | static GCCTypeClass |
11085 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
11086 | |
11087 | |
11088 | if (E->getNumArgs() == 0) |
11089 | return GCCTypeClass::None; |
11090 | |
11091 | |
11092 | |
11093 | |
11094 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
11095 | } |
11096 | |
11097 | |
11098 | |
11099 | |
11100 | |
11101 | |
11102 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
11103 | APValue::LValueBase Base = LV.getLValueBase(); |
11104 | if (Base.isNull()) { |
11105 | |
11106 | return true; |
11107 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
11108 | if (!isa<StringLiteral>(E)) |
11109 | return false; |
11110 | return LV.getLValueOffset().isZero(); |
11111 | } else if (Base.is<TypeInfoLValue>()) { |
11112 | |
11113 | |
11114 | return true; |
11115 | } else { |
11116 | |
11117 | return false; |
11118 | } |
11119 | } |
11120 | |
11121 | |
11122 | |
11123 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
11124 | |
11125 | |
11126 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11127 | |
11128 | |
11129 | |
11130 | |
11131 | FoldConstant Fold(Info, true); |
11132 | |
11133 | QualType ArgType = Arg->getType(); |
11134 | |
11135 | |
11136 | |
11137 | |
11138 | |
11139 | |
11140 | |
11141 | |
11142 | |
11143 | |
11144 | |
11145 | |
11146 | |
11147 | |
11148 | |
11149 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
11150 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
11151 | ArgType->isNullPtrType()) { |
11152 | APValue V; |
11153 | if (!::EvaluateAsRValue(Info, Arg, V) || Info.EvalStatus.HasSideEffects) { |
11154 | Fold.keepDiagnostics(); |
11155 | return false; |
11156 | } |
11157 | |
11158 | |
11159 | if (V.getKind() == APValue::LValue) |
11160 | return EvaluateBuiltinConstantPForLValue(V); |
11161 | |
11162 | |
11163 | return V.hasValue(); |
11164 | } |
11165 | |
11166 | |
11167 | return false; |
11168 | } |
11169 | |
11170 | |
11171 | |
11172 | static QualType getObjectType(APValue::LValueBase B) { |
11173 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
11174 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
11175 | return VD->getType(); |
11176 | } else if (const Expr *E = B.dyn_cast<const Expr*>()) { |
11177 | if (isa<CompoundLiteralExpr>(E)) |
11178 | return E->getType(); |
11179 | } else if (B.is<TypeInfoLValue>()) { |
11180 | return B.getTypeInfoType(); |
11181 | } else if (B.is<DynamicAllocLValue>()) { |
11182 | return B.getDynamicAllocType(); |
11183 | } |
11184 | |
11185 | return QualType(); |
11186 | } |
11187 | |
11188 | |
11189 | |
11190 | |
11191 | |
11192 | |
11193 | |
11194 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
11195 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
11196 | |
11197 | auto *NoParens = E->IgnoreParens(); |
11198 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
11199 | if (Cast == nullptr) |
11200 | return NoParens; |
11201 | |
11202 | |
11203 | |
11204 | auto CastKind = Cast->getCastKind(); |
11205 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
11206 | CastKind != CK_AddressSpaceConversion) |
11207 | return NoParens; |
11208 | |
11209 | auto *SubExpr = Cast->getSubExpr(); |
11210 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue()) |
11211 | return NoParens; |
11212 | return ignorePointerCastsAndParens(SubExpr); |
11213 | } |
11214 | |
11215 | |
11216 | |
11217 | |
11218 | |
11219 | |
11220 | |
11221 | |
11222 | |
11223 | |
11224 | |
11225 | |
11226 | |
11227 | |
11228 | |
11229 | |
11230 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
11231 | assert(!LVal.Designator.Invalid); |
11232 | |
11233 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
11234 | const RecordDecl *Parent = FD->getParent(); |
11235 | Invalid = Parent->isInvalidDecl(); |
11236 | if (Invalid || Parent->isUnion()) |
11237 | return true; |
11238 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
11239 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
11240 | }; |
11241 | |
11242 | auto &Base = LVal.getLValueBase(); |
11243 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
11244 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
11245 | bool Invalid; |
11246 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11247 | return Invalid; |
11248 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
11249 | for (auto *FD : IFD->chain()) { |
11250 | bool Invalid; |
11251 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
11252 | return Invalid; |
11253 | } |
11254 | } |
11255 | } |
11256 | |
11257 | unsigned I = 0; |
11258 | QualType BaseType = getType(Base); |
11259 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
11260 | |
11261 | |
11262 | ++I; |
11263 | if (BaseType->isIncompleteArrayType()) |
11264 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
11265 | else |
11266 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
11267 | } |
11268 | |
11269 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
11270 | const auto &Entry = LVal.Designator.Entries[I]; |
11271 | if (BaseType->isArrayType()) { |
11272 | |
11273 | |
11274 | if (I + 1 == E) |
11275 | return true; |
11276 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
11277 | uint64_t Index = Entry.getAsArrayIndex(); |
11278 | if (Index + 1 != CAT->getSize()) |
11279 | return false; |
11280 | BaseType = CAT->getElementType(); |
11281 | } else if (BaseType->isAnyComplexType()) { |
11282 | const auto *CT = BaseType->castAs<ComplexType>(); |
11283 | uint64_t Index = Entry.getAsArrayIndex(); |
11284 | if (Index != 1) |
11285 | return false; |
11286 | BaseType = CT->getElementType(); |
11287 | } else if (auto *FD = getAsField(Entry)) { |
11288 | bool Invalid; |
11289 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11290 | return Invalid; |
11291 | BaseType = FD->getType(); |
11292 | } else { |
11293 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
11294 | return false; |
11295 | } |
11296 | } |
11297 | return true; |
11298 | } |
11299 | |
11300 | |
11301 | |
11302 | |
11303 | |
11304 | static bool refersToCompleteObject(const LValue &LVal) { |
11305 | if (LVal.Designator.Invalid) |
11306 | return false; |
11307 | |
11308 | if (!LVal.Designator.Entries.empty()) |
11309 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
11310 | |
11311 | if (!LVal.InvalidBase) |
11312 | return true; |
11313 | |
11314 | |
11315 | |
11316 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
11317 | return !E || !isa<MemberExpr>(E); |
11318 | } |
11319 | |
11320 | |
11321 | |
11322 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
11323 | const SubobjectDesignator &Designator = LVal.Designator; |
11324 | |
11325 | |
11326 | |
11327 | |
11328 | |
11329 | |
11330 | |
11331 | |
11332 | |
11333 | |
11334 | |
11335 | return LVal.InvalidBase && |
11336 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
11337 | Designator.MostDerivedIsArrayElement && |
11338 | isDesignatorAtObjectEnd(Ctx, LVal); |
11339 | } |
11340 | |
11341 | |
11342 | |
11343 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
11344 | CharUnits &Result) { |
11345 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
11346 | if (Int.ugt(CharUnitsMax)) |
11347 | return false; |
11348 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
11349 | return true; |
11350 | } |
11351 | |
11352 | |
11353 | |
11354 | |
11355 | |
11356 | |
11357 | |
11358 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
11359 | unsigned Type, const LValue &LVal, |
11360 | CharUnits &EndOffset) { |
11361 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
11362 | |
11363 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
11364 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
11365 | return false; |
11366 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
11367 | }; |
11368 | |
11369 | |
11370 | |
11371 | |
11372 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
11373 | |
11374 | if (Type == 3 && !DetermineForCompleteObject) |
11375 | return false; |
11376 | |
11377 | llvm::APInt APEndOffset; |
11378 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
11379 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
11380 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
11381 | |
11382 | if (LVal.InvalidBase) |
11383 | return false; |
11384 | |
11385 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
11386 | return CheckedHandleSizeof(BaseTy, EndOffset); |
11387 | } |
11388 | |
11389 | |
11390 | const SubobjectDesignator &Designator = LVal.Designator; |
11391 | |
11392 | |
11393 | |
11394 | |
11395 | |
11396 | |
11397 | |
11398 | |
11399 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
11400 | |
11401 | |
11402 | llvm::APInt APEndOffset; |
11403 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
11404 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
11405 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
11406 | |
11407 | |
11408 | |
11409 | |
11410 | if (Type == 1) |
11411 | return false; |
11412 | } |
11413 | |
11414 | CharUnits BytesPerElem; |
11415 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
11416 | return false; |
11417 | |
11418 | |
11419 | |
11420 | |
11421 | int64_t ElemsRemaining; |
11422 | if (Designator.MostDerivedIsArrayElement && |
11423 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
11424 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
11425 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
11426 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
11427 | } else { |
11428 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
11429 | } |
11430 | |
11431 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
11432 | return true; |
11433 | } |
11434 | |
11435 | |
11436 | |
11437 | |
11438 | |
11439 | |
11440 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
11441 | EvalInfo &Info, uint64_t &Size) { |
11442 | |
11443 | LValue LVal; |
11444 | { |
11445 | |
11446 | |
11447 | |
11448 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11449 | IgnoreSideEffectsRAII Fold(Info); |
11450 | |
11451 | if (E->isGLValue()) { |
11452 | |
11453 | |
11454 | APValue RVal; |
11455 | if (!EvaluateAsRValue(Info, E, RVal)) |
11456 | return false; |
11457 | LVal.setFrom(Info.Ctx, RVal); |
11458 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
11459 | true)) |
11460 | return false; |
11461 | } |
11462 | |
11463 | |
11464 | |
11465 | if (LVal.getLValueOffset().isNegative()) { |
11466 | Size = 0; |
11467 | return true; |
11468 | } |
11469 | |
11470 | CharUnits EndOffset; |
11471 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
11472 | return false; |
11473 | |
11474 | |
11475 | |
11476 | if (EndOffset <= LVal.getLValueOffset()) |
11477 | Size = 0; |
11478 | else |
11479 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
11480 | return true; |
11481 | } |
11482 | |
11483 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
11484 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
11485 | return VisitBuiltinCallExpr(E, BuiltinOp); |
11486 | |
11487 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
11488 | } |
11489 | |
11490 | static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info, |
11491 | APValue &Val, APSInt &Alignment) { |
11492 | QualType SrcTy = E->getArg(0)->getType(); |
11493 | if (!getAlignmentArgument(E->getArg(1), SrcTy, Info, Alignment)) |
11494 | return false; |
11495 | |
11496 | |
11497 | if (SrcTy->isPointerType()) { |
11498 | LValue Ptr; |
11499 | if (!EvaluatePointer(E->getArg(0), Ptr, Info)) |
11500 | return false; |
11501 | Ptr.moveInto(Val); |
11502 | } else if (!SrcTy->isIntegralOrEnumerationType()) { |
11503 | Info.FFDiag(E->getArg(0)); |
11504 | return false; |
11505 | } else { |
11506 | APSInt SrcInt; |
11507 | if (!EvaluateInteger(E->getArg(0), SrcInt, Info)) |
11508 | return false; |
11509 | assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() && |
11510 | "Bit widths must be the same"); |
11511 | Val = APValue(SrcInt); |
11512 | } |
11513 | assert(Val.hasValue()); |
11514 | return true; |
11515 | } |
11516 | |
11517 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
11518 | unsigned BuiltinOp) { |
11519 | switch (BuiltinOp) { |
11520 | default: |
11521 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
11522 | |
11523 | case Builtin::BI__builtin_dynamic_object_size: |
11524 | case Builtin::BI__builtin_object_size: { |
11525 | |
11526 | unsigned Type = |
11527 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
11528 | assert(Type <= 3 && "unexpected type"); |
11529 | |
11530 | uint64_t Size; |
11531 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
11532 | return Success(Size, E); |
11533 | |
11534 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
11535 | return Success((Type & 2) ? 0 : -1, E); |
11536 | |
11537 | |
11538 | |
11539 | switch (Info.EvalMode) { |
11540 | case EvalInfo::EM_ConstantExpression: |
11541 | case EvalInfo::EM_ConstantFold: |
11542 | case EvalInfo::EM_IgnoreSideEffects: |
11543 | |
11544 | return Error(E); |
11545 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
11546 | |
11547 | return Success((Type & 2) ? 0 : -1, E); |
11548 | } |
11549 | |
11550 | llvm_unreachable("unexpected EvalMode"); |
11551 | } |
11552 | |
11553 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
11554 | analyze_os_log::OSLogBufferLayout Layout; |
11555 | analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout); |
11556 | return Success(Layout.size().getQuantity(), E); |
11557 | } |
11558 | |
11559 | case Builtin::BI__builtin_is_aligned: { |
11560 | APValue Src; |
11561 | APSInt Alignment; |
11562 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11563 | return false; |
11564 | if (Src.isLValue()) { |
11565 | |
11566 | LValue Ptr; |
11567 | Ptr.setFrom(Info.Ctx, Src); |
11568 | CharUnits BaseAlignment = getBaseAlignment(Info, Ptr); |
11569 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Ptr.Offset); |
11570 | |
11571 | |
11572 | assert(PtrAlign.isPowerOfTwo()); |
11573 | assert(Alignment.isPowerOf2()); |
11574 | if (PtrAlign.getQuantity() >= Alignment) |
11575 | return Success(1, E); |
11576 | |
11577 | |
11578 | |
11579 | |
11580 | if (BaseAlignment.getQuantity() >= Alignment && |
11581 | PtrAlign.getQuantity() < Alignment) |
11582 | return Success(0, E); |
11583 | |
11584 | |
11585 | |
11586 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute) |
11587 | << Alignment; |
11588 | return false; |
11589 | } |
11590 | assert(Src.isInt()); |
11591 | return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E); |
11592 | } |
11593 | case Builtin::BI__builtin_align_up: { |
11594 | APValue Src; |
11595 | APSInt Alignment; |
11596 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11597 | return false; |
11598 | if (!Src.isInt()) |
11599 | return Error(E); |
11600 | APSInt AlignedVal = |
11601 | APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1), |
11602 | Src.getInt().isUnsigned()); |
11603 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
11604 | return Success(AlignedVal, E); |
11605 | } |
11606 | case Builtin::BI__builtin_align_down: { |
11607 | APValue Src; |
11608 | APSInt Alignment; |
11609 | if (!getBuiltinAlignArguments(E, Info, Src, Alignment)) |
11610 | return false; |
11611 | if (!Src.isInt()) |
11612 | return Error(E); |
11613 | APSInt AlignedVal = |
11614 | APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned()); |
11615 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
11616 | return Success(AlignedVal, E); |
11617 | } |
11618 | |
11619 | case Builtin::BI__builtin_bitreverse8: |
11620 | case Builtin::BI__builtin_bitreverse16: |
11621 | case Builtin::BI__builtin_bitreverse32: |
11622 | case Builtin::BI__builtin_bitreverse64: { |
11623 | APSInt Val; |
11624 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11625 | return false; |
11626 | |
11627 | return Success(Val.reverseBits(), E); |
11628 | } |
11629 | |
11630 | case Builtin::BI__builtin_bswap16: |
11631 | case Builtin::BI__builtin_bswap32: |
11632 | case Builtin::BI__builtin_bswap64: { |
11633 | APSInt Val; |
11634 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11635 | return false; |
11636 | |
11637 | return Success(Val.byteSwap(), E); |
11638 | } |
11639 | |
11640 | case Builtin::BI__builtin_classify_type: |
11641 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
11642 | |
11643 | case Builtin::BI__builtin_clrsb: |
11644 | case Builtin::BI__builtin_clrsbl: |
11645 | case Builtin::BI__builtin_clrsbll: { |
11646 | APSInt Val; |
11647 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11648 | return false; |
11649 | |
11650 | return Success(Val.getBitWidth() - Val.getMinSignedBits(), E); |
11651 | } |
11652 | |
11653 | case Builtin::BI__builtin_clz: |
11654 | case Builtin::BI__builtin_clzl: |
11655 | case Builtin::BI__builtin_clzll: |
11656 | case Builtin::BI__builtin_clzs: { |
11657 | APSInt Val; |
11658 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11659 | return false; |
11660 | if (!Val) |
11661 | return Error(E); |
11662 | |
11663 | return Success(Val.countLeadingZeros(), E); |
11664 | } |
11665 | |
11666 | case Builtin::BI__builtin_constant_p: { |
11667 | const Expr *Arg = E->getArg(0); |
11668 | if (EvaluateBuiltinConstantP(Info, Arg)) |
11669 | return Success(true, E); |
11670 | if (Info.InConstantContext || Arg->HasSideEffects(Info.Ctx)) { |
11671 | |
11672 | |
11673 | |
11674 | return Success(false, E); |
11675 | } |
11676 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11677 | return false; |
11678 | } |
11679 | |
11680 | case Builtin::BI__builtin_is_constant_evaluated: { |
11681 | const auto *Callee = Info.CurrentCall->getCallee(); |
11682 | if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression && |
11683 | (Info.CallStackDepth == 1 || |
11684 | (Info.CallStackDepth == 2 && Callee->isInStdNamespace() && |
11685 | Callee->getIdentifier() && |
11686 | Callee->getIdentifier()->isStr("is_constant_evaluated")))) { |
11687 | |
11688 | if (Info.EvalStatus.Diag) |
11689 | Info.report((Info.CallStackDepth == 1) ? E->getExprLoc() |
11690 | : Info.CurrentCall->CallLoc, |
11691 | diag::warn_is_constant_evaluated_always_true_constexpr) |
11692 | << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated" |
11693 | : "std::is_constant_evaluated"); |
11694 | } |
11695 | |
11696 | return Success(Info.InConstantContext, E); |
11697 | } |
11698 | |
11699 | case Builtin::BI__builtin_ctz: |
11700 | case Builtin::BI__builtin_ctzl: |
11701 | case Builtin::BI__builtin_ctzll: |
11702 | case Builtin::BI__builtin_ctzs: { |
11703 | APSInt Val; |
11704 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11705 | return false; |
11706 | if (!Val) |
11707 | return Error(E); |
11708 | |
11709 | return Success(Val.countTrailingZeros(), E); |
11710 | } |
11711 | |
11712 | case Builtin::BI__builtin_eh_return_data_regno: { |
11713 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
11714 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
11715 | return Success(Operand, E); |
11716 | } |
11717 | |
11718 | case Builtin::BI__builtin_expect: |
11719 | case Builtin::BI__builtin_expect_with_probability: |
11720 | return Visit(E->getArg(0)); |
11721 | |
11722 | case Builtin::BI__builtin_ffs: |
11723 | case Builtin::BI__builtin_ffsl: |
11724 | case Builtin::BI__builtin_ffsll: { |
11725 | APSInt Val; |
11726 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11727 | return false; |
11728 | |
11729 | unsigned N = Val.countTrailingZeros(); |
11730 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
11731 | } |
11732 | |
11733 | case Builtin::BI__builtin_fpclassify: { |
11734 | APFloat Val(0.0); |
11735 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
11736 | return false; |
11737 | unsigned Arg; |
11738 | switch (Val.getCategory()) { |
11739 | case APFloat::fcNaN: Arg = 0; break; |
11740 | case APFloat::fcInfinity: Arg = 1; break; |
11741 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
11742 | case APFloat::fcZero: Arg = 4; break; |
11743 | } |
11744 | return Visit(E->getArg(Arg)); |
11745 | } |
11746 | |
11747 | case Builtin::BI__builtin_isinf_sign: { |
11748 | APFloat Val(0.0); |
11749 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11750 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
11751 | } |
11752 | |
11753 | case Builtin::BI__builtin_isinf: { |
11754 | APFloat Val(0.0); |
11755 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11756 | Success(Val.isInfinity() ? 1 : 0, E); |
11757 | } |
11758 | |
11759 | case Builtin::BI__builtin_isfinite: { |
11760 | APFloat Val(0.0); |
11761 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11762 | Success(Val.isFinite() ? 1 : 0, E); |
11763 | } |
11764 | |
11765 | case Builtin::BI__builtin_isnan: { |
11766 | APFloat Val(0.0); |
11767 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11768 | Success(Val.isNaN() ? 1 : 0, E); |
11769 | } |
11770 | |
11771 | case Builtin::BI__builtin_isnormal: { |
11772 | APFloat Val(0.0); |
11773 | return EvaluateFloat(E->getArg(0), Val, Info) && |
11774 | Success(Val.isNormal() ? 1 : 0, E); |
11775 | } |
11776 | |
11777 | case Builtin::BI__builtin_parity: |
11778 | case Builtin::BI__builtin_parityl: |
11779 | case Builtin::BI__builtin_parityll: { |
11780 | APSInt Val; |
11781 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11782 | return false; |
11783 | |
11784 | return Success(Val.countPopulation() % 2, E); |
11785 | } |
11786 | |
11787 | case Builtin::BI__builtin_popcount: |
11788 | case Builtin::BI__builtin_popcountl: |
11789 | case Builtin::BI__builtin_popcountll: { |
11790 | APSInt Val; |
11791 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
11792 | return false; |
11793 | |
11794 | return Success(Val.countPopulation(), E); |
11795 | } |
11796 | |
11797 | case Builtin::BI__builtin_rotateleft8: |
11798 | case Builtin::BI__builtin_rotateleft16: |
11799 | case Builtin::BI__builtin_rotateleft32: |
11800 | case Builtin::BI__builtin_rotateleft64: |
11801 | case Builtin::BI_rotl8: |
11802 | case Builtin::BI_rotl16: |
11803 | case Builtin::BI_rotl: |
11804 | case Builtin::BI_lrotl: |
11805 | case Builtin::BI_rotl64: { |
11806 | APSInt Val, Amt; |
11807 | if (!EvaluateInteger(E->getArg(0), Val, Info) || |
11808 | !EvaluateInteger(E->getArg(1), Amt, Info)) |
11809 | return false; |
11810 | |
11811 | return Success(Val.rotl(Amt.urem(Val.getBitWidth())), E); |
11812 | } |
11813 | |
11814 | case Builtin::BI__builtin_rotateright8: |
11815 | case Builtin::BI__builtin_rotateright16: |
11816 | case Builtin::BI__builtin_rotateright32: |
11817 | case Builtin::BI__builtin_rotateright64: |
11818 | case Builtin::BI_rotr8: |
11819 | case Builtin::BI_rotr16: |
11820 | case Builtin::BI_rotr: |
11821 | case Builtin::BI_lrotr: |
11822 | case Builtin::BI_rotr64: { |
11823 | APSInt Val, Amt; |
11824 | if (!EvaluateInteger(E->getArg(0), Val, Info) || |
11825 | !EvaluateInteger(E->getArg(1), Amt, Info)) |
11826 | return false; |
11827 | |
11828 | return Success(Val.rotr(Amt.urem(Val.getBitWidth())), E); |
11829 | } |
11830 | |
11831 | case Builtin::BIstrlen: |
11832 | case Builtin::BIwcslen: |
11833 | |
11834 | if (Info.getLangOpts().CPlusPlus11) |
11835 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
11836 | << 0 << 0 |
11837 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
11838 | else |
11839 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11840 | LLVM_FALLTHROUGH; |
11841 | case Builtin::BI__builtin_strlen: |
11842 | case Builtin::BI__builtin_wcslen: { |
11843 | |
11844 | |
11845 | uint64_t StrLen; |
11846 | if (EvaluateBuiltinStrLen(E->getArg(0), StrLen, Info)) |
11847 | return Success(StrLen, E); |
11848 | return false; |
11849 | } |
11850 | |
11851 | case Builtin::BIstrcmp: |
11852 | case Builtin::BIwcscmp: |
11853 | case Builtin::BIstrncmp: |
11854 | case Builtin::BIwcsncmp: |
11855 | case Builtin::BImemcmp: |
11856 | case Builtin::BIbcmp: |
11857 | case Builtin::BIwmemcmp: |
11858 | |
11859 | if (Info.getLangOpts().CPlusPlus11) |
11860 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
11861 | << 0 << 0 |
11862 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
11863 | else |
11864 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11865 | LLVM_FALLTHROUGH; |
11866 | case Builtin::BI__builtin_strcmp: |
11867 | case Builtin::BI__builtin_wcscmp: |
11868 | case Builtin::BI__builtin_strncmp: |
11869 | case Builtin::BI__builtin_wcsncmp: |
11870 | case Builtin::BI__builtin_memcmp: |
11871 | case Builtin::BI__builtin_bcmp: |
11872 | case Builtin::BI__builtin_wmemcmp: { |
11873 | LValue String1, String2; |
11874 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
11875 | !EvaluatePointer(E->getArg(1), String2, Info)) |
11876 | return false; |
11877 | |
11878 | uint64_t MaxLength = uint64_t(-1); |
11879 | if (BuiltinOp != Builtin::BIstrcmp && |
11880 | BuiltinOp != Builtin::BIwcscmp && |
11881 | BuiltinOp != Builtin::BI__builtin_strcmp && |
11882 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
11883 | APSInt N; |
11884 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
11885 | return false; |
11886 | MaxLength = N.getExtValue(); |
11887 | } |
11888 | |
11889 | |
11890 | if (MaxLength == 0u) |
11891 | return Success(0, E); |
11892 | |
11893 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
11894 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
11895 | String1.Designator.Invalid || String2.Designator.Invalid) |
11896 | return false; |
11897 | |
11898 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
11899 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
11900 | |
11901 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
11902 | BuiltinOp == Builtin::BIbcmp || |
11903 | BuiltinOp == Builtin::BI__builtin_memcmp || |
11904 | BuiltinOp == Builtin::BI__builtin_bcmp; |
11905 | |
11906 | assert(IsRawByte || |
11907 | (Info.Ctx.hasSameUnqualifiedType( |
11908 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
11909 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
11910 | |
11911 | |
11912 | |
11913 | if (IsRawByte && |
11914 | !(isOneByteCharacterType(CharTy1) && isOneByteCharacterType(CharTy2))) { |
11915 | |
11916 | Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported) |
11917 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'") |
11918 | << CharTy1 << CharTy2; |
11919 | return false; |
11920 | } |
11921 | |
11922 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
11923 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
11924 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
11925 | Char1.isInt() && Char2.isInt(); |
11926 | }; |
11927 | const auto &AdvanceElems = [&] { |
11928 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
11929 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
11930 | }; |
11931 | |
11932 | bool StopAtNull = |
11933 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
11934 | BuiltinOp != Builtin::BIwmemcmp && |
11935 | BuiltinOp != Builtin::BI__builtin_memcmp && |
11936 | BuiltinOp != Builtin::BI__builtin_bcmp && |
11937 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
11938 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
11939 | BuiltinOp == Builtin::BIwcsncmp || |
11940 | BuiltinOp == Builtin::BIwmemcmp || |
11941 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
11942 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
11943 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
11944 | |
11945 | for (; MaxLength; --MaxLength) { |
11946 | APValue Char1, Char2; |
11947 | if (!ReadCurElems(Char1, Char2)) |
11948 | return false; |
11949 | if (Char1.getInt().ne(Char2.getInt())) { |
11950 | if (IsWide) |
11951 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
11952 | |
11953 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
11954 | } |
11955 | if (StopAtNull && !Char1.getInt()) |
11956 | return Success(0, E); |
11957 | assert(!(StopAtNull && !Char2.getInt())); |
11958 | if (!AdvanceElems()) |
11959 | return false; |
11960 | } |
11961 | |
11962 | return Success(0, E); |
11963 | } |
11964 | |
11965 | case Builtin::BI__atomic_always_lock_free: |
11966 | case Builtin::BI__atomic_is_lock_free: |
11967 | case Builtin::BI__c11_atomic_is_lock_free: { |
11968 | APSInt SizeVal; |
11969 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
11970 | return false; |
11971 | |
11972 | |
11973 | |
11974 | |
11975 | |
11976 | |
11977 | |
11978 | |
11979 | |
11980 | |
11981 | |
11982 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
11983 | if (Size.isPowerOfTwo()) { |
11984 | |
11985 | unsigned InlineWidthBits = |
11986 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
11987 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
11988 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
11989 | Size == CharUnits::One() || |
11990 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
11991 | Expr::NPC_NeverValueDependent)) |
11992 | |
11993 | |
11994 | return Success(1, E); |
11995 | |
11996 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
11997 | castAs<PointerType>()->getPointeeType(); |
11998 | if (!PointeeType->isIncompleteType() && |
11999 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
12000 | |
12001 | return Success(1, E); |
12002 | } |
12003 | } |
12004 | } |
12005 | |
12006 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
12007 | Success(0, E) : Error(E); |
12008 | } |
12009 | case Builtin::BI__builtin_add_overflow: |
12010 | case Builtin::BI__builtin_sub_overflow: |
12011 | case Builtin::BI__builtin_mul_overflow: |
12012 | case Builtin::BI__builtin_sadd_overflow: |
12013 | case Builtin::BI__builtin_uadd_overflow: |
12014 | case Builtin::BI__builtin_uaddl_overflow: |
12015 | case Builtin::BI__builtin_uaddll_overflow: |
12016 | case Builtin::BI__builtin_usub_overflow: |
12017 | case Builtin::BI__builtin_usubl_overflow: |
12018 | case Builtin::BI__builtin_usubll_overflow: |
12019 | case Builtin::BI__builtin_umul_overflow: |
12020 | case Builtin::BI__builtin_umull_overflow: |
12021 | case Builtin::BI__builtin_umulll_overflow: |
12022 | case Builtin::BI__builtin_saddl_overflow: |
12023 | case Builtin::BI__builtin_saddll_overflow: |
12024 | case Builtin::BI__builtin_ssub_overflow: |
12025 | case Builtin::BI__builtin_ssubl_overflow: |
12026 | case Builtin::BI__builtin_ssubll_overflow: |
12027 | case Builtin::BI__builtin_smul_overflow: |
12028 | case Builtin::BI__builtin_smull_overflow: |
12029 | case Builtin::BI__builtin_smulll_overflow: { |
12030 | LValue ResultLValue; |
12031 | APSInt LHS, RHS; |
12032 | |
12033 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
12034 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
12035 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
12036 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
12037 | return false; |
12038 | |
12039 | APSInt Result; |
12040 | bool DidOverflow = false; |
12041 | |
12042 | |
12043 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12044 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12045 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12046 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
12047 | ResultType->isSignedIntegerOrEnumerationType(); |
12048 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
12049 | ResultType->isSignedIntegerOrEnumerationType(); |
12050 | uint64_t LHSSize = LHS.getBitWidth(); |
12051 | uint64_t RHSSize = RHS.getBitWidth(); |
12052 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
12053 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
12054 | |
12055 | |
12056 | |
12057 | |
12058 | |
12059 | if (IsSigned && !AllSigned) |
12060 | ++MaxBits; |
12061 | |
12062 | LHS = APSInt(LHS.extOrTrunc(MaxBits), !IsSigned); |
12063 | RHS = APSInt(RHS.extOrTrunc(MaxBits), !IsSigned); |
12064 | Result = APSInt(MaxBits, !IsSigned); |
12065 | } |
12066 | |
12067 | |
12068 | switch (BuiltinOp) { |
12069 | default: |
12070 | llvm_unreachable("Invalid value for BuiltinOp"); |
12071 | case Builtin::BI__builtin_add_overflow: |
12072 | case Builtin::BI__builtin_sadd_overflow: |
12073 | case Builtin::BI__builtin_saddl_overflow: |
12074 | case Builtin::BI__builtin_saddll_overflow: |
12075 | case Builtin::BI__builtin_uadd_overflow: |
12076 | case Builtin::BI__builtin_uaddl_overflow: |
12077 | case Builtin::BI__builtin_uaddll_overflow: |
12078 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
12079 | : LHS.uadd_ov(RHS, DidOverflow); |
12080 | break; |
12081 | case Builtin::BI__builtin_sub_overflow: |
12082 | case Builtin::BI__builtin_ssub_overflow: |
12083 | case Builtin::BI__builtin_ssubl_overflow: |
12084 | case Builtin::BI__builtin_ssubll_overflow: |
12085 | case Builtin::BI__builtin_usub_overflow: |
12086 | case Builtin::BI__builtin_usubl_overflow: |
12087 | case Builtin::BI__builtin_usubll_overflow: |
12088 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
12089 | : LHS.usub_ov(RHS, DidOverflow); |
12090 | break; |
12091 | case Builtin::BI__builtin_mul_overflow: |
12092 | case Builtin::BI__builtin_smul_overflow: |
12093 | case Builtin::BI__builtin_smull_overflow: |
12094 | case Builtin::BI__builtin_smulll_overflow: |
12095 | case Builtin::BI__builtin_umul_overflow: |
12096 | case Builtin::BI__builtin_umull_overflow: |
12097 | case Builtin::BI__builtin_umulll_overflow: |
12098 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
12099 | : LHS.umul_ov(RHS, DidOverflow); |
12100 | break; |
12101 | } |
12102 | |
12103 | |
12104 | |
12105 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12106 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12107 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12108 | |
12109 | |
12110 | |
12111 | |
12112 | |
12113 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
12114 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
12115 | |
12116 | if (!APSInt::isSameValue(Temp, Result)) |
12117 | DidOverflow = true; |
12118 | Result = Temp; |
12119 | } |
12120 | |
12121 | APValue APV{Result}; |
12122 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
12123 | return false; |
12124 | return Success(DidOverflow, E); |
12125 | } |
12126 | } |
12127 | } |
12128 | |
12129 | |
12130 | |
12131 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
12132 | const LValue &LV) { |
12133 | |
12134 | |
12135 | if (!LV.getLValueBase()) |
12136 | return false; |
12137 | |
12138 | |
12139 | |
12140 | if (!LV.getLValueDesignator().Invalid && |
12141 | !LV.getLValueDesignator().isOnePastTheEnd()) |
12142 | return false; |
12143 | |
12144 | |
12145 | |
12146 | QualType Ty = getType(LV.getLValueBase()); |
12147 | if (Ty->isIncompleteType()) |
12148 | return true; |
12149 | |
12150 | |
12151 | |
12152 | auto Size = Ctx.getTypeSizeInChars(Ty); |
12153 | return LV.getLValueOffset() == Size; |
12154 | } |
12155 | |
12156 | namespace { |
12157 | |
12158 | |
12159 | |
12160 | |
12161 | |
12162 | |
12163 | class DataRecursiveIntBinOpEvaluator { |
12164 | struct EvalResult { |
12165 | APValue Val; |
12166 | bool Failed; |
12167 | |
12168 | EvalResult() : Failed(false) { } |
12169 | |
12170 | void swap(EvalResult &RHS) { |
12171 | Val.swap(RHS.Val); |
12172 | Failed = RHS.Failed; |
12173 | RHS.Failed = false; |
12174 | } |
12175 | }; |
12176 | |
12177 | struct Job { |
12178 | const Expr *E; |
12179 | EvalResult LHSResult; |
12180 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
12181 | |
12182 | Job() = default; |
12183 | Job(Job &&) = default; |
12184 | |
12185 | void startSpeculativeEval(EvalInfo &Info) { |
12186 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
12187 | } |
12188 | |
12189 | private: |
12190 | SpeculativeEvaluationRAII SpecEvalRAII; |
12191 | }; |
12192 | |
12193 | SmallVector<Job, 16> Queue; |
12194 | |
12195 | IntExprEvaluator &IntEval; |
12196 | EvalInfo &Info; |
12197 | APValue &FinalResult; |
12198 | |
12199 | public: |
12200 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
12201 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
12202 | |
12203 | |
12204 | |
12205 | |
12206 | |
12207 | static bool shouldEnqueue(const BinaryOperator *E) { |
12208 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
12209 | (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() && |
12210 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12211 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12212 | } |
12213 | |
12214 | bool Traverse(const BinaryOperator *E) { |
12215 | enqueue(E); |
12216 | EvalResult PrevResult; |
12217 | while (!Queue.empty()) |
12218 | process(PrevResult); |
12219 | |
12220 | if (PrevResult.Failed) return false; |
12221 | |
12222 | FinalResult.swap(PrevResult.Val); |
12223 | return true; |
12224 | } |
12225 | |
12226 | private: |
12227 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
12228 | return IntEval.Success(Value, E, Result); |
12229 | } |
12230 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
12231 | return IntEval.Success(Value, E, Result); |
12232 | } |
12233 | bool Error(const Expr *E) { |
12234 | return IntEval.Error(E); |
12235 | } |
12236 | bool Error(const Expr *E, diag::kind D) { |
12237 | return IntEval.Error(E, D); |
12238 | } |
12239 | |
12240 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
12241 | return Info.CCEDiag(E, D); |
12242 | } |
12243 | |
12244 | |
12245 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12246 | bool &SuppressRHSDiags); |
12247 | |
12248 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
12249 | const BinaryOperator *E, APValue &Result); |
12250 | |
12251 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
12252 | Result.Failed = !Evaluate(Result.Val, Info, E); |
12253 | if (Result.Failed) |
12254 | Result.Val = APValue(); |
12255 | } |
12256 | |
12257 | void process(EvalResult &Result); |
12258 | |
12259 | void enqueue(const Expr *E) { |
12260 | E = E->IgnoreParens(); |
12261 | Queue.resize(Queue.size()+1); |
12262 | Queue.back().E = E; |
12263 | Queue.back().Kind = Job::AnyExprKind; |
12264 | } |
12265 | }; |
12266 | |
12267 | } |
12268 | |
12269 | bool DataRecursiveIntBinOpEvaluator:: |
12270 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12271 | bool &SuppressRHSDiags) { |
12272 | if (E->getOpcode() == BO_Comma) { |
12273 | |
12274 | if (LHSResult.Failed) |
12275 | return Info.noteSideEffect(); |
12276 | return true; |
12277 | } |
12278 | |
12279 | if (E->isLogicalOp()) { |
12280 | bool LHSAsBool; |
12281 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
12282 | |
12283 | |
12284 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
12285 | Success(LHSAsBool, E, LHSResult.Val); |
12286 | return false; |
12287 | } |
12288 | } else { |
12289 | LHSResult.Failed = true; |
12290 | |
12291 | |
12292 | |
12293 | if (!Info.noteSideEffect()) |
12294 | return false; |
12295 | |
12296 | |
12297 | |
12298 | |
12299 | SuppressRHSDiags = true; |
12300 | } |
12301 | |
12302 | return true; |
12303 | } |
12304 | |
12305 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12306 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12307 | |
12308 | if (LHSResult.Failed && !Info.noteFailure()) |
12309 | return false; |
12310 | |
12311 | return true; |
12312 | } |
12313 | |
12314 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
12315 | bool IsSub) { |
12316 | |
12317 | |
12318 | |
12319 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
12320 | CharUnits &Offset = LVal.getLValueOffset(); |
12321 | uint64_t Offset64 = Offset.getQuantity(); |
12322 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
12323 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
12324 | : Offset64 + Index64); |
12325 | } |
12326 | |
12327 | bool DataRecursiveIntBinOpEvaluator:: |
12328 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
12329 | const BinaryOperator *E, APValue &Result) { |
12330 | if (E->getOpcode() == BO_Comma) { |
12331 | if (RHSResult.Failed) |
12332 | return false; |
12333 | Result = RHSResult.Val; |
12334 | return true; |
12335 | } |
12336 | |
12337 | if (E->isLogicalOp()) { |
12338 | bool lhsResult, rhsResult; |
12339 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
12340 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
12341 | |
12342 | if (LHSIsOK) { |
12343 | if (RHSIsOK) { |
12344 | if (E->getOpcode() == BO_LOr) |
12345 | return Success(lhsResult || rhsResult, E, Result); |
12346 | else |
12347 | return Success(lhsResult && rhsResult, E, Result); |
12348 | } |
12349 | } else { |
12350 | if (RHSIsOK) { |
12351 | |
12352 | |
12353 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
12354 | return Success(rhsResult, E, Result); |
12355 | } |
12356 | } |
12357 | |
12358 | return false; |
12359 | } |
12360 | |
12361 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12362 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12363 | |
12364 | if (LHSResult.Failed || RHSResult.Failed) |
12365 | return false; |
12366 | |
12367 | const APValue &LHSVal = LHSResult.Val; |
12368 | const APValue &RHSVal = RHSResult.Val; |
12369 | |
12370 | |
12371 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
12372 | Result = LHSVal; |
12373 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
12374 | return true; |
12375 | } |
12376 | |
12377 | |
12378 | if (E->getOpcode() == BO_Add && |
12379 | RHSVal.isLValue() && LHSVal.isInt()) { |
12380 | Result = RHSVal; |
12381 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), false); |
12382 | return true; |
12383 | } |
12384 | |
12385 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
12386 | |
12387 | if (!LHSVal.getLValueOffset().isZero() || |
12388 | !RHSVal.getLValueOffset().isZero()) |
12389 | return false; |
12390 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
12391 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
12392 | if (!LHSExpr || !RHSExpr) |
12393 | return false; |
12394 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
12395 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
12396 | if (!LHSAddrExpr || !RHSAddrExpr) |
12397 | return false; |
12398 | |
12399 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
12400 | RHSAddrExpr->getLabel()->getDeclContext()) |
12401 | return false; |
12402 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
12403 | return true; |
12404 | } |
12405 | |
12406 | |
12407 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
12408 | return Error(E); |
12409 | |
12410 | |
12411 | |
12412 | |
12413 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
12414 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
12415 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
12416 | RHSVal.getInt(), Value)) |
12417 | return false; |
12418 | return Success(Value, E, Result); |
12419 | } |
12420 | |
12421 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
12422 | Job &job = Queue.back(); |
12423 | |
12424 | switch (job.Kind) { |
12425 | case Job::AnyExprKind: { |
12426 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
12427 | if (shouldEnqueue(Bop)) { |
12428 | job.Kind = Job::BinOpKind; |
12429 | enqueue(Bop->getLHS()); |
12430 | return; |
12431 | } |
12432 | } |
12433 | |
12434 | EvaluateExpr(job.E, Result); |
12435 | Queue.pop_back(); |
12436 | return; |
12437 | } |
12438 | |
12439 | case Job::BinOpKind: { |
12440 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
12441 | bool SuppressRHSDiags = false; |
12442 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
12443 | Queue.pop_back(); |
12444 | return; |
12445 | } |
12446 | if (SuppressRHSDiags) |
12447 | job.startSpeculativeEval(Info); |
12448 | job.LHSResult.swap(Result); |
12449 | job.Kind = Job::BinOpVisitedLHSKind; |
12450 | enqueue(Bop->getRHS()); |
12451 | return; |
12452 | } |
12453 | |
12454 | case Job::BinOpVisitedLHSKind: { |
12455 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
12456 | EvalResult RHS; |
12457 | RHS.swap(Result); |
12458 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
12459 | Queue.pop_back(); |
12460 | return; |
12461 | } |
12462 | } |
12463 | |
12464 | llvm_unreachable("Invalid Job::Kind!"); |
12465 | } |
12466 | |
12467 | namespace { |
12468 | enum class CmpResult { |
12469 | Unequal, |
12470 | Less, |
12471 | Equal, |
12472 | Greater, |
12473 | Unordered, |
12474 | }; |
12475 | } |
12476 | |
12477 | template <class SuccessCB, class AfterCB> |
12478 | static bool |
12479 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
12480 | SuccessCB &&Success, AfterCB &&DoAfter) { |
12481 | assert(!E->isValueDependent()); |
12482 | assert(E->isComparisonOp() && "expected comparison operator"); |
12483 | assert((E->getOpcode() == BO_Cmp || |
12484 | E->getType()->isIntegralOrEnumerationType()) && |
12485 | "unsupported binary expression evaluation"); |
12486 | auto Error = [&](const Expr *E) { |
12487 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12488 | return false; |
12489 | }; |
12490 | |
12491 | bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp; |
12492 | bool IsEquality = E->isEqualityOp(); |
12493 | |
12494 | QualType LHSTy = E->getLHS()->getType(); |
12495 | QualType RHSTy = E->getRHS()->getType(); |
12496 | |
12497 | if (LHSTy->isIntegralOrEnumerationType() && |
12498 | RHSTy->isIntegralOrEnumerationType()) { |
12499 | APSInt LHS, RHS; |
12500 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
12501 | if (!LHSOK && !Info.noteFailure()) |
12502 | return false; |
12503 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
12504 | return false; |
12505 | if (LHS < RHS) |
12506 | return Success(CmpResult::Less, E); |
12507 | if (LHS > RHS) |
12508 | return Success(CmpResult::Greater, E); |
12509 | return Success(CmpResult::Equal, E); |
12510 | } |
12511 | |
12512 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
12513 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy)); |
12514 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy)); |
12515 | |
12516 | bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info); |
12517 | if (!LHSOK && !Info.noteFailure()) |
12518 | return false; |
12519 | if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK) |
12520 | return false; |
12521 | if (LHSFX < RHSFX) |
12522 | return Success(CmpResult::Less, E); |
12523 | if (LHSFX > RHSFX) |
12524 | return Success(CmpResult::Greater, E); |
12525 | return Success(CmpResult::Equal, E); |
12526 | } |
12527 | |
12528 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
12529 | ComplexValue LHS, RHS; |
12530 | bool LHSOK; |
12531 | if (E->isAssignmentOp()) { |
12532 | LValue LV; |
12533 | EvaluateLValue(E->getLHS(), LV, Info); |
12534 | LHSOK = false; |
12535 | } else if (LHSTy->isRealFloatingType()) { |
12536 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
12537 | if (LHSOK) { |
12538 | LHS.makeComplexFloat(); |
12539 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
12540 | } |
12541 | } else { |
12542 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
12543 | } |
12544 | if (!LHSOK && !Info.noteFailure()) |
12545 | return false; |
12546 | |
12547 | if (E->getRHS()->getType()->isRealFloatingType()) { |
12548 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
12549 | return false; |
12550 | RHS.makeComplexFloat(); |
12551 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
12552 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
12553 | return false; |
12554 | |
12555 | if (LHS.isComplexFloat()) { |
12556 | APFloat::cmpResult CR_r = |
12557 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
12558 | APFloat::cmpResult CR_i = |
12559 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
12560 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
12561 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
12562 | } else { |
12563 | assert(IsEquality && "invalid complex comparison"); |
12564 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
12565 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
12566 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
12567 | } |
12568 | } |
12569 | |
12570 | if (LHSTy->isRealFloatingType() && |
12571 | RHSTy->isRealFloatingType()) { |
12572 | APFloat RHS(0.0), LHS(0.0); |
12573 | |
12574 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
12575 | if (!LHSOK && !Info.noteFailure()) |
12576 | return false; |
12577 | |
12578 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
12579 | return false; |
12580 | |
12581 | assert(E->isComparisonOp() && "Invalid binary operator!"); |
12582 | llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS); |
12583 | if (!Info.InConstantContext && |
12584 | APFloatCmpResult == APFloat::cmpUnordered && |
12585 | E->getFPFeaturesInEffect(Info.Ctx.getLangOpts()).isFPConstrained()) { |
12586 | |
12587 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
12588 | return false; |
12589 | } |
12590 | auto GetCmpRes = [&]() { |
12591 | switch (APFloatCmpResult) { |
12592 | case APFloat::cmpEqual: |
12593 | return CmpResult::Equal; |
12594 | case APFloat::cmpLessThan: |
12595 | return CmpResult::Less; |
12596 | case APFloat::cmpGreaterThan: |
12597 | return CmpResult::Greater; |
12598 | case APFloat::cmpUnordered: |
12599 | return CmpResult::Unordered; |
12600 | } |
12601 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
12602 | }; |
12603 | return Success(GetCmpRes(), E); |
12604 | } |
12605 | |
12606 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
12607 | LValue LHSValue, RHSValue; |
12608 | |
12609 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
12610 | if (!LHSOK && !Info.noteFailure()) |
12611 | return false; |
12612 | |
12613 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12614 | return false; |
12615 | |
12616 | |
12617 | |
12618 | if (!HasSameBase(LHSValue, RHSValue)) { |
12619 | |
12620 | |
12621 | if (!IsEquality) { |
12622 | Info.FFDiag(E, diag::note_constexpr_pointer_comparison_unspecified); |
12623 | return false; |
12624 | } |
12625 | |
12626 | |
12627 | |
12628 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
12629 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
12630 | return Error(E); |
12631 | |
12632 | |
12633 | |
12634 | |
12635 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
12636 | LHSValue.Base && RHSValue.Base) |
12637 | return Error(E); |
12638 | |
12639 | |
12640 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
12641 | return Error(E); |
12642 | |
12643 | |
12644 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
12645 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
12646 | (RHSValue.Base && RHSValue.Offset.isZero() && |
12647 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
12648 | return Error(E); |
12649 | |
12650 | |
12651 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
12652 | (LHSValue.Base && isZeroSized(RHSValue))) |
12653 | return Error(E); |
12654 | return Success(CmpResult::Unequal, E); |
12655 | } |
12656 | |
12657 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
12658 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
12659 | |
12660 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
12661 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
12662 | |
12663 | |
12664 | |
12665 | |
12666 | |
12667 | |
12668 | |
12669 | |
12670 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
12671 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
12672 | |
12673 | |
12674 | |
12675 | |
12676 | |
12677 | |
12678 | |
12679 | |
12680 | |
12681 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
12682 | bool WasArrayIndex; |
12683 | unsigned Mismatch = FindDesignatorMismatch( |
12684 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
12685 | |
12686 | |
12687 | |
12688 | |
12689 | |
12690 | |
12691 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
12692 | Mismatch < RHSDesignator.Entries.size()) { |
12693 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
12694 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
12695 | if (!LF && !RF) |
12696 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
12697 | else if (!LF) |
12698 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
12699 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
12700 | << RF->getParent() << RF; |
12701 | else if (!RF) |
12702 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
12703 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
12704 | << LF->getParent() << LF; |
12705 | else if (!LF->getParent()->isUnion() && |
12706 | LF->getAccess() != RF->getAccess()) |
12707 | Info.CCEDiag(E, |
12708 | diag::note_constexpr_pointer_comparison_differing_access) |
12709 | << LF << LF->getAccess() << RF << RF->getAccess() |
12710 | << LF->getParent(); |
12711 | } |
12712 | } |
12713 | |
12714 | |
12715 | |
12716 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
12717 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
12718 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
12719 | assert(PtrSize <= 64 && "Unexpected pointer width"); |
12720 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
12721 | CompareLHS &= Mask; |
12722 | CompareRHS &= Mask; |
12723 | |
12724 | |
12725 | |
12726 | |
12727 | if (!LHSValue.Base.isNull() && IsRelational) { |
12728 | QualType BaseTy = getType(LHSValue.Base); |
12729 | if (BaseTy->isIncompleteType()) |
12730 | return Error(E); |
12731 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
12732 | uint64_t OffsetLimit = Size.getQuantity(); |
12733 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
12734 | return Error(E); |
12735 | } |
12736 | |
12737 | if (CompareLHS < CompareRHS) |
12738 | return Success(CmpResult::Less, E); |
12739 | if (CompareLHS > CompareRHS) |
12740 | return Success(CmpResult::Greater, E); |
12741 | return Success(CmpResult::Equal, E); |
12742 | } |
12743 | |
12744 | if (LHSTy->isMemberPointerType()) { |
12745 | assert(IsEquality && "unexpected member pointer operation"); |
12746 | assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
12747 | |
12748 | MemberPtr LHSValue, RHSValue; |
12749 | |
12750 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
12751 | if (!LHSOK && !Info.noteFailure()) |
12752 | return false; |
12753 | |
12754 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12755 | return false; |
12756 | |
12757 | |
12758 | |
12759 | |
12760 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
12761 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
12762 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
12763 | } |
12764 | |
12765 | |
12766 | |
12767 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
12768 | if (MD->isVirtual()) |
12769 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
12770 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
12771 | if (MD->isVirtual()) |
12772 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
12773 | |
12774 | |
12775 | |
12776 | |
12777 | |
12778 | bool Equal = LHSValue == RHSValue; |
12779 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
12780 | } |
12781 | |
12782 | if (LHSTy->isNullPtrType()) { |
12783 | assert(E->isComparisonOp() && "unexpected nullptr operation"); |
12784 | assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
12785 | |
12786 | |
12787 | |
12788 | return Success(CmpResult::Equal, E); |
12789 | } |
12790 | |
12791 | return DoAfter(); |
12792 | } |
12793 | |
12794 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
12795 | if (!CheckLiteralType(Info, E)) |
12796 | return false; |
12797 | |
12798 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
12799 | ComparisonCategoryResult CCR; |
12800 | switch (CR) { |
12801 | case CmpResult::Unequal: |
12802 | llvm_unreachable("should never produce Unequal for three-way comparison"); |
12803 | case CmpResult::Less: |
12804 | CCR = ComparisonCategoryResult::Less; |
12805 | break; |
12806 | case CmpResult::Equal: |
12807 | CCR = ComparisonCategoryResult::Equal; |
12808 | break; |
12809 | case CmpResult::Greater: |
12810 | CCR = ComparisonCategoryResult::Greater; |
12811 | break; |
12812 | case CmpResult::Unordered: |
12813 | CCR = ComparisonCategoryResult::Unordered; |
12814 | break; |
12815 | } |
12816 | |
12817 | |
12818 | const ComparisonCategoryInfo &CmpInfo = |
12819 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
12820 | const VarDecl *VD = CmpInfo.getValueInfo(CmpInfo.makeWeakResult(CCR))->VD; |
12821 | |
12822 | LValue LV; |
12823 | LV.set(VD); |
12824 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
12825 | return false; |
12826 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
12827 | ConstantExprKind::Normal); |
12828 | }; |
12829 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
12830 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
12831 | }); |
12832 | } |
12833 | |
12834 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
12835 | |
12836 | |
12837 | if (E->isAssignmentOp()) { |
12838 | Error(E); |
12839 | if (!Info.noteFailure()) |
12840 | return false; |
12841 | } |
12842 | |
12843 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
12844 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
12845 | |
12846 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
12847 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
12848 | "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
12849 | |
12850 | if (E->isComparisonOp()) { |
12851 | |
12852 | |
12853 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
12854 | assert((CR != CmpResult::Unequal || E->isEqualityOp()) && |
12855 | "should only produce Unequal for equality comparisons"); |
12856 | bool IsEqual = CR == CmpResult::Equal, |
12857 | IsLess = CR == CmpResult::Less, |
12858 | IsGreater = CR == CmpResult::Greater; |
12859 | auto Op = E->getOpcode(); |
12860 | switch (Op) { |
12861 | default: |
12862 | llvm_unreachable("unsupported binary operator"); |
12863 | case BO_EQ: |
12864 | case BO_NE: |
12865 | return Success(IsEqual == (Op == BO_EQ), E); |
12866 | case BO_LT: |
12867 | return Success(IsLess, E); |
12868 | case BO_GT: |
12869 | return Success(IsGreater, E); |
12870 | case BO_LE: |
12871 | return Success(IsEqual || IsLess, E); |
12872 | case BO_GE: |
12873 | return Success(IsEqual || IsGreater, E); |
12874 | } |
12875 | }; |
12876 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
12877 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
12878 | }); |
12879 | } |
12880 | |
12881 | QualType LHSTy = E->getLHS()->getType(); |
12882 | QualType RHSTy = E->getRHS()->getType(); |
12883 | |
12884 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
12885 | E->getOpcode() == BO_Sub) { |
12886 | LValue LHSValue, RHSValue; |
12887 | |
12888 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
12889 | if (!LHSOK && !Info.noteFailure()) |
12890 | return false; |
12891 | |
12892 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
12893 | return false; |
12894 | |
12895 | |
12896 | |
12897 | if (!HasSameBase(LHSValue, RHSValue)) { |
12898 | |
12899 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
12900 | return Error(E); |
12901 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
12902 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
12903 | if (!LHSExpr || !RHSExpr) |
12904 | return Error(E); |
12905 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
12906 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
12907 | if (!LHSAddrExpr || !RHSAddrExpr) |
12908 | return Error(E); |
12909 | |
12910 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
12911 | RHSAddrExpr->getLabel()->getDeclContext()) |
12912 | return Error(E); |
12913 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
12914 | } |
12915 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
12916 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
12917 | |
12918 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
12919 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
12920 | |
12921 | |
12922 | |
12923 | |
12924 | |
12925 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
12926 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
12927 | RHSDesignator)) |
12928 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
12929 | |
12930 | QualType Type = E->getLHS()->getType(); |
12931 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
12932 | |
12933 | CharUnits ElementSize; |
12934 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
12935 | return false; |
12936 | |
12937 | |
12938 | |
12939 | |
12940 | if (ElementSize.isZero()) { |
12941 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
12942 | << ElementType; |
12943 | return false; |
12944 | } |
12945 | |
12946 | |
12947 | |
12948 | |
12949 | |
12950 | |
12951 | |
12952 | |
12953 | |
12954 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
12955 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
12956 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
12957 | false); |
12958 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
12959 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
12960 | |
12961 | if (Result.extend(65) != TrueResult && |
12962 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
12963 | return false; |
12964 | return Success(Result, E); |
12965 | } |
12966 | |
12967 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
12968 | } |
12969 | |
12970 | |
12971 | |
12972 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
12973 | const UnaryExprOrTypeTraitExpr *E) { |
12974 | switch(E->getKind()) { |
12975 | case UETT_PreferredAlignOf: |
12976 | case UETT_AlignOf: { |
12977 | if (E->isArgumentType()) |
12978 | return Success(GetAlignOfType(Info, E->getArgumentType(), E->getKind()), |
12979 | E); |
12980 | else |
12981 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr(), E->getKind()), |
12982 | E); |
12983 | } |
12984 | |
12985 | case UETT_VecStep: { |
12986 | QualType Ty = E->getTypeOfArgument(); |
12987 | |
12988 | if (Ty->isVectorType()) { |
12989 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
12990 | |
12991 | |
12992 | |
12993 | if (n == 3) |
12994 | n = 4; |
12995 | |
12996 | return Success(n, E); |
12997 | } else |
12998 | return Success(1, E); |
12999 | } |
13000 | |
13001 | case UETT_SizeOf: { |
13002 | QualType SrcTy = E->getTypeOfArgument(); |
13003 | |
13004 | |
13005 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
13006 | SrcTy = Ref->getPointeeType(); |
13007 | |
13008 | CharUnits Sizeof; |
13009 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
13010 | return false; |
13011 | return Success(Sizeof, E); |
13012 | } |
13013 | case UETT_OpenMPRequiredSimdAlign: |
13014 | assert(E->isArgumentType()); |
13015 | return Success( |
13016 | Info.Ctx.toCharUnitsFromBits( |
13017 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
13018 | .getQuantity(), |
13019 | E); |
13020 | } |
13021 | |
13022 | llvm_unreachable("unknown expr/type trait"); |
13023 | } |
13024 | |
13025 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
13026 | CharUnits Result; |
13027 | unsigned n = OOE->getNumComponents(); |
13028 | if (n == 0) |
13029 | return Error(OOE); |
13030 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
13031 | for (unsigned i = 0; i != n; ++i) { |
13032 | OffsetOfNode ON = OOE->getComponent(i); |
13033 | switch (ON.getKind()) { |
13034 | case OffsetOfNode::Array: { |
13035 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
13036 | APSInt IdxResult; |
13037 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
13038 | return false; |
13039 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
13040 | if (!AT) |
13041 | return Error(OOE); |
13042 | CurrentType = AT->getElementType(); |
13043 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
13044 | Result += IdxResult.getSExtValue() * ElementSize; |
13045 | break; |
13046 | } |
13047 | |
13048 | case OffsetOfNode::Field: { |
13049 | FieldDecl *MemberDecl = ON.getField(); |
13050 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13051 | if (!RT) |
13052 | return Error(OOE); |
13053 | RecordDecl *RD = RT->getDecl(); |
13054 | if (RD->isInvalidDecl()) return false; |
13055 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
13056 | unsigned i = MemberDecl->getFieldIndex(); |
13057 | assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
13058 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
13059 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
13060 | break; |
13061 | } |
13062 | |
13063 | case OffsetOfNode::Identifier: |
13064 | llvm_unreachable("dependent __builtin_offsetof"); |
13065 | |
13066 | case OffsetOfNode::Base: { |
13067 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
13068 | if (BaseSpec->isVirtual()) |
13069 | return Error(OOE); |
13070 | |
13071 | |
13072 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13073 | if (!RT) |
13074 | return Error(OOE); |
13075 | RecordDecl *RD = RT->getDecl(); |
13076 | if (RD->isInvalidDecl()) return false; |
13077 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
13078 | |
13079 | |
13080 | CurrentType = BaseSpec->getType(); |
13081 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
13082 | if (!BaseRT) |
13083 | return Error(OOE); |
13084 | |
13085 | |
13086 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
13087 | break; |
13088 | } |
13089 | } |
13090 | } |
13091 | return Success(Result, OOE); |
13092 | } |
13093 | |
13094 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13095 | switch (E->getOpcode()) { |
13096 | default: |
13097 | |
13098 | |
13099 | return Error(E); |
13100 | case UO_Extension: |
13101 | |
13102 | |
13103 | return Visit(E->getSubExpr()); |
13104 | case UO_Plus: |
13105 | |
13106 | return Visit(E->getSubExpr()); |
13107 | case UO_Minus: { |
13108 | if (!Visit(E->getSubExpr())) |
13109 | return false; |
13110 | if (!Result.isInt()) return Error(E); |
13111 | const APSInt &Value = Result.getInt(); |
13112 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
13113 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
13114 | E->getType())) |
13115 | return false; |
13116 | return Success(-Value, E); |
13117 | } |
13118 | case UO_Not: { |
13119 | if (!Visit(E->getSubExpr())) |
13120 | return false; |
13121 | if (!Result.isInt()) return Error(E); |
13122 | return Success(~Result.getInt(), E); |
13123 | } |
13124 | case UO_LNot: { |
13125 | bool bres; |
13126 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
13127 | return false; |
13128 | return Success(!bres, E); |
13129 | } |
13130 | } |
13131 | } |
13132 | |
13133 | |
13134 | |
13135 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13136 | const Expr *SubExpr = E->getSubExpr(); |
13137 | QualType DestType = E->getType(); |
13138 | QualType SrcType = SubExpr->getType(); |
13139 | |
13140 | switch (E->getCastKind()) { |
13141 | case CK_BaseToDerived: |
13142 | case CK_DerivedToBase: |
13143 | case CK_UncheckedDerivedToBase: |
13144 | case CK_Dynamic: |
13145 | case CK_ToUnion: |
13146 | case CK_ArrayToPointerDecay: |
13147 | case CK_FunctionToPointerDecay: |
13148 | case CK_NullToPointer: |
13149 | case CK_NullToMemberPointer: |
13150 | case CK_BaseToDerivedMemberPointer: |
13151 | case CK_DerivedToBaseMemberPointer: |
13152 | case CK_ReinterpretMemberPointer: |
13153 | case CK_ConstructorConversion: |
13154 | case CK_IntegralToPointer: |
13155 | case CK_ToVoid: |
13156 | case CK_VectorSplat: |
13157 | case CK_IntegralToFloating: |
13158 | case CK_FloatingCast: |
13159 | case CK_CPointerToObjCPointerCast: |
13160 | case CK_BlockPointerToObjCPointerCast: |
13161 | case CK_AnyPointerToBlockPointerCast: |
13162 | case CK_ObjCObjectLValueCast: |
13163 | case CK_FloatingRealToComplex: |
13164 | case CK_FloatingComplexToReal: |
13165 | case CK_FloatingComplexCast: |
13166 | case CK_FloatingComplexToIntegralComplex: |
13167 | case CK_IntegralRealToComplex: |
13168 | case CK_IntegralComplexCast: |
13169 | case CK_IntegralComplexToFloatingComplex: |
13170 | case CK_BuiltinFnToFnPtr: |
13171 | case CK_ZeroToOCLOpaqueType: |
13172 | case CK_NonAtomicToAtomic: |
13173 | case CK_AddressSpaceConversion: |
13174 | case CK_IntToOCLSampler: |
13175 | case CK_FloatingToFixedPoint: |
13176 | case CK_FixedPointToFloating: |
13177 | case CK_FixedPointCast: |
13178 | case CK_IntegralToFixedPoint: |
13179 | case CK_MatrixCast: |
13180 | llvm_unreachable("invalid cast kind for integral value"); |
13181 | |
13182 | case CK_BitCast: |
13183 | case CK_Dependent: |
13184 | case CK_LValueBitCast: |
13185 | case CK_ARCProduceObject: |
13186 | case CK_ARCConsumeObject: |
13187 | case CK_ARCReclaimReturnedObject: |
13188 | case CK_ARCExtendBlockObject: |
13189 | case CK_CopyAndAutoreleaseBlockObject: |
13190 | return Error(E); |
13191 | |
13192 | case CK_UserDefinedConversion: |
13193 | case CK_LValueToRValue: |
13194 | case CK_AtomicToNonAtomic: |
13195 | case CK_NoOp: |
13196 | case CK_LValueToRValueBitCast: |
13197 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13198 | |
13199 | case CK_MemberPointerToBoolean: |
13200 | case CK_PointerToBoolean: |
13201 | case CK_IntegralToBoolean: |
13202 | case CK_FloatingToBoolean: |
13203 | case CK_BooleanToSignedIntegral: |
13204 | case CK_FloatingComplexToBoolean: |
13205 | case CK_IntegralComplexToBoolean: { |
13206 | bool BoolResult; |
13207 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
13208 | return false; |
13209 | uint64_t IntResult = BoolResult; |
13210 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
13211 | IntResult = (uint64_t)-1; |
13212 | return Success(IntResult, E); |
13213 | } |
13214 | |
13215 | case CK_FixedPointToIntegral: { |
13216 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType)); |
13217 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
13218 | return false; |
13219 | bool Overflowed; |
13220 | llvm::APSInt Result = Src.convertToInt( |
13221 | Info.Ctx.getIntWidth(DestType), |
13222 | DestType->isSignedIntegerOrEnumerationType(), &Overflowed); |
13223 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
13224 | return false; |
13225 | return Success(Result, E); |
13226 | } |
13227 | |
13228 | case CK_FixedPointToBoolean: { |
13229 | |
13230 | APValue Val; |
13231 | if (!Evaluate(Val, Info, SubExpr)) |
13232 | return false; |
13233 | return Success(Val.getFixedPoint().getBoolValue(), E); |
13234 | } |
13235 | |
13236 | case CK_IntegralCast: { |
13237 | if (!Visit(SubExpr)) |
13238 | return false; |
13239 | |
13240 | if (!Result.isInt()) { |
13241 | |
13242 | |
13243 | |
13244 | |
13245 | |
13246 | if (Result.isAddrLabelDiff()) |
13247 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
13248 | |
13249 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
13250 | } |
13251 | |
13252 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
13253 | Result.getInt()), E); |
13254 | } |
13255 | |
13256 | case CK_PointerToIntegral: { |
13257 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
13258 | |
13259 | LValue LV; |
13260 | if (!EvaluatePointer(SubExpr, LV, Info)) |
13261 | return false; |
13262 | |
13263 | if (LV.getLValueBase()) { |
13264 | |
13265 | |
13266 | |
13267 | |
13268 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
13269 | return Error(E); |
13270 | |
13271 | LV.Designator.setInvalid(); |
13272 | LV.moveInto(Result); |
13273 | return true; |
13274 | } |
13275 | |
13276 | APSInt AsInt; |
13277 | APValue V; |
13278 | LV.moveInto(V); |
13279 | if (!V.toIntegralConstant(AsInt, SrcType, Info.Ctx)) |
13280 | llvm_unreachable("Can't cast this!"); |
13281 | |
13282 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
13283 | } |
13284 | |
13285 | case CK_IntegralComplexToReal: { |
13286 | ComplexValue C; |
13287 | if (!EvaluateComplex(SubExpr, C, Info)) |
13288 | return false; |
13289 | return Success(C.getComplexIntReal(), E); |
13290 | } |
13291 | |
13292 | case CK_FloatingToIntegral: { |
13293 | APFloat F(0.0); |
13294 | if (!EvaluateFloat(SubExpr, F, Info)) |
13295 | return false; |
13296 | |
13297 | APSInt Value; |
13298 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
13299 | return false; |
13300 | return Success(Value, E); |
13301 | } |
13302 | } |
13303 | |
13304 | llvm_unreachable("unknown cast resulting in integral value"); |
13305 | } |
13306 | |
13307 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
13308 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13309 | ComplexValue LV; |
13310 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
13311 | return false; |
13312 | if (!LV.isComplexInt()) |
13313 | return Error(E); |
13314 | return Success(LV.getComplexIntReal(), E); |
13315 | } |
13316 | |
13317 | return Visit(E->getSubExpr()); |
13318 | } |
13319 | |
13320 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
13321 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
13322 | ComplexValue LV; |
13323 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
13324 | return false; |
13325 | if (!LV.isComplexInt()) |
13326 | return Error(E); |
13327 | return Success(LV.getComplexIntImag(), E); |
13328 | } |
13329 | |
13330 | VisitIgnoredValue(E->getSubExpr()); |
13331 | return Success(0, E); |
13332 | } |
13333 | |
13334 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
13335 | return Success(E->getPackLength(), E); |
13336 | } |
13337 | |
13338 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
13339 | return Success(E->getValue(), E); |
13340 | } |
13341 | |
13342 | bool IntExprEvaluator::VisitConceptSpecializationExpr( |
13343 | const ConceptSpecializationExpr *E) { |
13344 | return Success(E->isSatisfied(), E); |
13345 | } |
13346 | |
13347 | bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) { |
13348 | return Success(E->isSatisfied(), E); |
13349 | } |
13350 | |
13351 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13352 | switch (E->getOpcode()) { |
13353 | default: |
13354 | |
13355 | return Error(E); |
13356 | case UO_Plus: |
13357 | |
13358 | return Visit(E->getSubExpr()); |
13359 | case UO_Minus: { |
13360 | if (!Visit(E->getSubExpr())) return false; |
13361 | if (!Result.isFixedPoint()) |
13362 | return Error(E); |
13363 | bool Overflowed; |
13364 | APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed); |
13365 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
13366 | return false; |
13367 | return Success(Negated, E); |
13368 | } |
13369 | case UO_LNot: { |
13370 | bool bres; |
13371 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
13372 | return false; |
13373 | return Success(!bres, E); |
13374 | } |
13375 | } |
13376 | } |
13377 | |
13378 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13379 | const Expr *SubExpr = E->getSubExpr(); |
13380 | QualType DestType = E->getType(); |
13381 | assert(DestType->isFixedPointType() && |
13382 | "Expected destination type to be a fixed point type"); |
13383 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType); |
13384 | |
13385 | switch (E->getCastKind()) { |
13386 | case CK_FixedPointCast: { |
13387 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
13388 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
13389 | return false; |
13390 | bool Overflowed; |
13391 | APFixedPoint Result = Src.convert(DestFXSema, &Overflowed); |
13392 | if (Overflowed) { |
13393 | if (Info.checkingForUndefinedBehavior()) |
13394 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13395 | diag::warn_fixedpoint_constant_overflow) |
13396 | << Result.toString() << E->getType(); |
13397 | if (!HandleOverflow(Info, E, Result, E->getType())) |
13398 | return false; |
13399 | } |
13400 | return Success(Result, E); |
13401 | } |
13402 | case CK_IntegralToFixedPoint: { |
13403 | APSInt Src; |
13404 | if (!EvaluateInteger(SubExpr, Src, Info)) |
13405 | return false; |
13406 | |
13407 | bool Overflowed; |
13408 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
13409 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
13410 | |
13411 | if (Overflowed) { |
13412 | if (Info.checkingForUndefinedBehavior()) |
13413 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13414 | diag::warn_fixedpoint_constant_overflow) |
13415 | << IntResult.toString() << E->getType(); |
13416 | if (!HandleOverflow(Info, E, IntResult, E->getType())) |
13417 | return false; |
13418 | } |
13419 | |
13420 | return Success(IntResult, E); |
13421 | } |
13422 | case CK_FloatingToFixedPoint: { |
13423 | APFloat Src(0.0); |
13424 | if (!EvaluateFloat(SubExpr, Src, Info)) |
13425 | return false; |
13426 | |
13427 | bool Overflowed; |
13428 | APFixedPoint Result = APFixedPoint::getFromFloatValue( |
13429 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
13430 | |
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 | |
13440 | return Success(Result, E); |
13441 | } |
13442 | case CK_NoOp: |
13443 | case CK_LValueToRValue: |
13444 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13445 | default: |
13446 | return Error(E); |
13447 | } |
13448 | } |
13449 | |
13450 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13451 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
13452 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13453 | |
13454 | const Expr *LHS = E->getLHS(); |
13455 | const Expr *RHS = E->getRHS(); |
13456 | FixedPointSemantics ResultFXSema = |
13457 | Info.Ctx.getFixedPointSemantics(E->getType()); |
13458 | |
13459 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType())); |
13460 | if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info)) |
13461 | return false; |
13462 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType())); |
13463 | if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info)) |
13464 | return false; |
13465 | |
13466 | bool OpOverflow = false, ConversionOverflow = false; |
13467 | APFixedPoint Result(LHSFX.getSemantics()); |
13468 | switch (E->getOpcode()) { |
13469 | case BO_Add: { |
13470 | Result = LHSFX.add(RHSFX, &OpOverflow) |
13471 | .convert(ResultFXSema, &ConversionOverflow); |
13472 | break; |
13473 | } |
13474 | case BO_Sub: { |
13475 | Result = LHSFX.sub(RHSFX, &OpOverflow) |
13476 | .convert(ResultFXSema, &ConversionOverflow); |
13477 | break; |
13478 | } |
13479 | case BO_Mul: { |
13480 | Result = LHSFX.mul(RHSFX, &OpOverflow) |
13481 | .convert(ResultFXSema, &ConversionOverflow); |
13482 | break; |
13483 | } |
13484 | case BO_Div: { |
13485 | if (RHSFX.getValue() == 0) { |
13486 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
13487 | return false; |
13488 | } |
13489 | Result = LHSFX.div(RHSFX, &OpOverflow) |
13490 | .convert(ResultFXSema, &ConversionOverflow); |
13491 | break; |
13492 | } |
13493 | case BO_Shl: |
13494 | case BO_Shr: { |
13495 | FixedPointSemantics LHSSema = LHSFX.getSemantics(); |
13496 | llvm::APSInt RHSVal = RHSFX.getValue(); |
13497 | |
13498 | unsigned ShiftBW = |
13499 | LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding(); |
13500 | unsigned Amt = RHSVal.getLimitedValue(ShiftBW - 1); |
13501 | |
13502 | |
13503 | |
13504 | if (RHSVal.isNegative()) |
13505 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal; |
13506 | else if (Amt != RHSVal) |
13507 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
13508 | << RHSVal << E->getType() << ShiftBW; |
13509 | |
13510 | if (E->getOpcode() == BO_Shl) |
13511 | Result = LHSFX.shl(Amt, &OpOverflow); |
13512 | else |
13513 | Result = LHSFX.shr(Amt, &OpOverflow); |
13514 | break; |
13515 | } |
13516 | default: |
13517 | return false; |
13518 | } |
13519 | if (OpOverflow || ConversionOverflow) { |
13520 | if (Info.checkingForUndefinedBehavior()) |
13521 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13522 | diag::warn_fixedpoint_constant_overflow) |
13523 | << Result.toString() << E->getType(); |
13524 | if (!HandleOverflow(Info, E, Result, E->getType())) |
13525 | return false; |
13526 | } |
13527 | return Success(Result, E); |
13528 | } |
13529 | |
13530 | |
13531 | |
13532 | |
13533 | |
13534 | namespace { |
13535 | class FloatExprEvaluator |
13536 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
13537 | APFloat &Result; |
13538 | public: |
13539 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
13540 | : ExprEvaluatorBaseTy(info), Result(result) {} |
13541 | |
13542 | bool Success(const APValue &V, const Expr *e) { |
13543 | Result = V.getFloat(); |
13544 | return true; |
13545 | } |
13546 | |
13547 | bool ZeroInitialization(const Expr *E) { |
13548 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
13549 | return true; |
13550 | } |
13551 | |
13552 | bool VisitCallExpr(const CallExpr *E); |
13553 | |
13554 | bool VisitUnaryOperator(const UnaryOperator *E); |
13555 | bool VisitBinaryOperator(const BinaryOperator *E); |
13556 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
13557 | bool VisitCastExpr(const CastExpr *E); |
13558 | |
13559 | bool VisitUnaryReal(const UnaryOperator *E); |
13560 | bool VisitUnaryImag(const UnaryOperator *E); |
13561 | |
13562 | |
13563 | }; |
13564 | } |
13565 | |
13566 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
13567 | assert(!E->isValueDependent()); |
13568 | assert(E->isPRValue() && E->getType()->isRealFloatingType()); |
13569 | return FloatExprEvaluator(Info, Result).Visit(E); |
13570 | } |
13571 | |
13572 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
13573 | QualType ResultTy, |
13574 | const Expr *Arg, |
13575 | bool SNaN, |
13576 | llvm::APFloat &Result) { |
13577 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
13578 | if (!S) return false; |
13579 | |
13580 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
13581 | |
13582 | llvm::APInt fill; |
13583 | |
13584 | |
13585 | if (S->getString().empty()) |
13586 | fill = llvm::APInt(32, 0); |
13587 | else if (S->getString().getAsInteger(0, fill)) |
13588 | return false; |
13589 | |
13590 | if (Context.getTargetInfo().isNan2008()) { |
13591 | if (SNaN) |
13592 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
13593 | else |
13594 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
13595 | } else { |
13596 | |
13597 | |
13598 | |
13599 | |
13600 | |
13601 | if (SNaN) |
13602 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
13603 | else |
13604 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
13605 | } |
13606 | |
13607 | return true; |
13608 | } |
13609 | |
13610 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
13611 | switch (E->getBuiltinCallee()) { |
13612 | default: |
13613 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
13614 | |
13615 | case Builtin::BI__builtin_huge_val: |
13616 | case Builtin::BI__builtin_huge_valf: |
13617 | case Builtin::BI__builtin_huge_vall: |
13618 | case Builtin::BI__builtin_huge_valf128: |
13619 | case Builtin::BI__builtin_inf: |
13620 | case Builtin::BI__builtin_inff: |
13621 | case Builtin::BI__builtin_infl: |
13622 | case Builtin::BI__builtin_inff128: { |
13623 | const llvm::fltSemantics &Sem = |
13624 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
13625 | Result = llvm::APFloat::getInf(Sem); |
13626 | return true; |
13627 | } |
13628 | |
13629 | case Builtin::BI__builtin_nans: |
13630 | case Builtin::BI__builtin_nansf: |
13631 | case Builtin::BI__builtin_nansl: |
13632 | case Builtin::BI__builtin_nansf128: |
13633 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
13634 | true, Result)) |
13635 | return Error(E); |
13636 | return true; |
13637 | |
13638 | case Builtin::BI__builtin_nan: |
13639 | case Builtin::BI__builtin_nanf: |
13640 | case Builtin::BI__builtin_nanl: |
13641 | case Builtin::BI__builtin_nanf128: |
13642 | |
13643 | |
13644 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
13645 | false, Result)) |
13646 | return Error(E); |
13647 | return true; |
13648 | |
13649 | case Builtin::BI__builtin_fabs: |
13650 | case Builtin::BI__builtin_fabsf: |
13651 | case Builtin::BI__builtin_fabsl: |
13652 | case Builtin::BI__builtin_fabsf128: |
13653 | |
13654 | |
13655 | |
13656 | |
13657 | |
13658 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
13659 | return false; |
13660 | |
13661 | if (Result.isNegative()) |
13662 | Result.changeSign(); |
13663 | return true; |
13664 | |
13665 | case Builtin::BI__arithmetic_fence: |
13666 | return EvaluateFloat(E->getArg(0), Result, Info); |
13667 | |
13668 | |
13669 | |
13670 | |
13671 | |
13672 | case Builtin::BI__builtin_copysign: |
13673 | case Builtin::BI__builtin_copysignf: |
13674 | case Builtin::BI__builtin_copysignl: |
13675 | case Builtin::BI__builtin_copysignf128: { |
13676 | APFloat RHS(0.); |
13677 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
13678 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
13679 | return false; |
13680 | Result.copySign(RHS); |
13681 | return true; |
13682 | } |
13683 | } |
13684 | } |
13685 | |
13686 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
13687 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13688 | ComplexValue CV; |
13689 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
13690 | return false; |
13691 | Result = CV.FloatReal; |
13692 | return true; |
13693 | } |
13694 | |
13695 | return Visit(E->getSubExpr()); |
13696 | } |
13697 | |
13698 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
13699 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
13700 | ComplexValue CV; |
13701 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
13702 | return false; |
13703 | Result = CV.FloatImag; |
13704 | return true; |
13705 | } |
13706 | |
13707 | VisitIgnoredValue(E->getSubExpr()); |
13708 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
13709 | Result = llvm::APFloat::getZero(Sem); |
13710 | return true; |
13711 | } |
13712 | |
13713 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13714 | switch (E->getOpcode()) { |
13715 | default: return Error(E); |
13716 | case UO_Plus: |
13717 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
13718 | case UO_Minus: |
13719 | |
13720 | |
13721 | |
13722 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
13723 | return false; |
13724 | Result.changeSign(); |
13725 | return true; |
13726 | } |
13727 | } |
13728 | |
13729 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13730 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
13731 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13732 | |
13733 | APFloat RHS(0.0); |
13734 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
13735 | if (!LHSOK && !Info.noteFailure()) |
13736 | return false; |
13737 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
13738 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
13739 | } |
13740 | |
13741 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
13742 | Result = E->getValue(); |
13743 | return true; |
13744 | } |
13745 | |
13746 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13747 | const Expr* SubExpr = E->getSubExpr(); |
13748 | |
13749 | switch (E->getCastKind()) { |
13750 | default: |
13751 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13752 | |
13753 | case CK_IntegralToFloating: { |
13754 | APSInt IntResult; |
13755 | const FPOptions FPO = E->getFPFeaturesInEffect( |
13756 | Info.Ctx.getLangOpts()); |
13757 | return EvaluateInteger(SubExpr, IntResult, Info) && |
13758 | HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(), |
13759 | IntResult, E->getType(), Result); |
13760 | } |
13761 | |
13762 | case CK_FixedPointToFloating: { |
13763 | APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
13764 | if (!EvaluateFixedPoint(SubExpr, FixResult, Info)) |
13765 | return false; |
13766 | Result = |
13767 | FixResult.convertToFloat(Info.Ctx.getFloatTypeSemantics(E->getType())); |
13768 | return true; |
13769 | } |
13770 | |
13771 | case CK_FloatingCast: { |
13772 | if (!Visit(SubExpr)) |
13773 | return false; |
13774 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
13775 | Result); |
13776 | } |
13777 | |
13778 | case CK_FloatingComplexToReal: { |
13779 | ComplexValue V; |
13780 | if (!EvaluateComplex(SubExpr, V, Info)) |
13781 | return false; |
13782 | Result = V.getComplexFloatReal(); |
13783 | return true; |
13784 | } |
13785 | } |
13786 | } |
13787 | |
13788 | |
13789 | |
13790 | |
13791 | |
13792 | namespace { |
13793 | class ComplexExprEvaluator |
13794 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
13795 | ComplexValue &Result; |
13796 | |
13797 | public: |
13798 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
13799 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
13800 | |
13801 | bool Success(const APValue &V, const Expr *e) { |
13802 | Result.setFrom(V); |
13803 | return true; |
13804 | } |
13805 | |
13806 | bool ZeroInitialization(const Expr *E); |
13807 | |
13808 | |
13809 | |
13810 | |
13811 | |
13812 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
13813 | bool VisitCastExpr(const CastExpr *E); |
13814 | bool VisitBinaryOperator(const BinaryOperator *E); |
13815 | bool VisitUnaryOperator(const UnaryOperator *E); |
13816 | bool VisitInitListExpr(const InitListExpr *E); |
13817 | bool VisitCallExpr(const CallExpr *E); |
13818 | }; |
13819 | } |
13820 | |
13821 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
13822 | EvalInfo &Info) { |
13823 | assert(!E->isValueDependent()); |
13824 | assert(E->isPRValue() && E->getType()->isAnyComplexType()); |
13825 | return ComplexExprEvaluator(Info, Result).Visit(E); |
13826 | } |
13827 | |
13828 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
13829 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
13830 | if (ElemTy->isRealFloatingType()) { |
13831 | Result.makeComplexFloat(); |
13832 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
13833 | Result.FloatReal = Zero; |
13834 | Result.FloatImag = Zero; |
13835 | } else { |
13836 | Result.makeComplexInt(); |
13837 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
13838 | Result.IntReal = Zero; |
13839 | Result.IntImag = Zero; |
13840 | } |
13841 | return true; |
13842 | } |
13843 | |
13844 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
13845 | const Expr* SubExpr = E->getSubExpr(); |
13846 | |
13847 | if (SubExpr->getType()->isRealFloatingType()) { |
13848 | Result.makeComplexFloat(); |
13849 | APFloat &Imag = Result.FloatImag; |
13850 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
13851 | return false; |
13852 | |
13853 | Result.FloatReal = APFloat(Imag.getSemantics()); |
13854 | return true; |
13855 | } else { |
13856 | assert(SubExpr->getType()->isIntegerType() && |
13857 | "Unexpected imaginary literal."); |
13858 | |
13859 | Result.makeComplexInt(); |
13860 | APSInt &Imag = Result.IntImag; |
13861 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
13862 | return false; |
13863 | |
13864 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
13865 | return true; |
13866 | } |
13867 | } |
13868 | |
13869 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13870 | |
13871 | switch (E->getCastKind()) { |
13872 | case CK_BitCast: |
13873 | case CK_BaseToDerived: |
13874 | case CK_DerivedToBase: |
13875 | case CK_UncheckedDerivedToBase: |
13876 | case CK_Dynamic: |
13877 | case CK_ToUnion: |
13878 | case CK_ArrayToPointerDecay: |
13879 | case CK_FunctionToPointerDecay: |
13880 | case CK_NullToPointer: |
13881 | case CK_NullToMemberPointer: |
13882 | case CK_BaseToDerivedMemberPointer: |
13883 | case CK_DerivedToBaseMemberPointer: |
13884 | case CK_MemberPointerToBoolean: |
13885 | case CK_ReinterpretMemberPointer: |
13886 | case CK_ConstructorConversion: |
13887 | case CK_IntegralToPointer: |
13888 | case CK_PointerToIntegral: |
13889 | case CK_PointerToBoolean: |
13890 | case CK_ToVoid: |
13891 | case CK_VectorSplat: |
13892 | case CK_IntegralCast: |
13893 | case CK_BooleanToSignedIntegral: |
13894 | case CK_IntegralToBoolean: |
13895 | case CK_IntegralToFloating: |
13896 | case CK_FloatingToIntegral: |
13897 | case CK_FloatingToBoolean: |
13898 | case CK_FloatingCast: |
13899 | case CK_CPointerToObjCPointerCast: |
13900 | case CK_BlockPointerToObjCPointerCast: |
13901 | case CK_AnyPointerToBlockPointerCast: |
13902 | case CK_ObjCObjectLValueCast: |
13903 | case CK_FloatingComplexToReal: |
13904 | case CK_FloatingComplexToBoolean: |
13905 | case CK_IntegralComplexToReal: |
13906 | case CK_IntegralComplexToBoolean: |
13907 | case CK_ARCProduceObject: |
13908 | case CK_ARCConsumeObject: |
13909 | case CK_ARCReclaimReturnedObject: |
13910 | case CK_ARCExtendBlockObject: |
13911 | case CK_CopyAndAutoreleaseBlockObject: |
13912 | case CK_BuiltinFnToFnPtr: |
13913 | case CK_ZeroToOCLOpaqueType: |
13914 | case CK_NonAtomicToAtomic: |
13915 | case CK_AddressSpaceConversion: |
13916 | case CK_IntToOCLSampler: |
13917 | case CK_FloatingToFixedPoint: |
13918 | case CK_FixedPointToFloating: |
13919 | case CK_FixedPointCast: |
13920 | case CK_FixedPointToBoolean: |
13921 | case CK_FixedPointToIntegral: |
13922 | case CK_IntegralToFixedPoint: |
13923 | case CK_MatrixCast: |
13924 | llvm_unreachable("invalid cast kind for complex value"); |
13925 | |
13926 | case CK_LValueToRValue: |
13927 | case CK_AtomicToNonAtomic: |
13928 | case CK_NoOp: |
13929 | case CK_LValueToRValueBitCast: |
13930 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13931 | |
13932 | case CK_Dependent: |
13933 | case CK_LValueBitCast: |
13934 | case CK_UserDefinedConversion: |
13935 | return Error(E); |
13936 | |
13937 | case CK_FloatingRealToComplex: { |
13938 | APFloat &Real = Result.FloatReal; |
13939 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
13940 | return false; |
13941 | |
13942 | Result.makeComplexFloat(); |
13943 | Result.FloatImag = APFloat(Real.getSemantics()); |
13944 | return true; |
13945 | } |
13946 | |
13947 | case CK_FloatingComplexCast: { |
13948 | if (!Visit(E->getSubExpr())) |
13949 | return false; |
13950 | |
13951 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
13952 | QualType From |
13953 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
13954 | |
13955 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
13956 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
13957 | } |
13958 | |
13959 | case CK_FloatingComplexToIntegralComplex: { |
13960 | if (!Visit(E->getSubExpr())) |
13961 | return false; |
13962 | |
13963 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
13964 | QualType From |
13965 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
13966 | Result.makeComplexInt(); |
13967 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
13968 | To, Result.IntReal) && |
13969 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
13970 | To, Result.IntImag); |
13971 | } |
13972 | |
13973 | case CK_IntegralRealToComplex: { |
13974 | APSInt &Real = Result.IntReal; |
13975 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
13976 | return false; |
13977 | |
13978 | Result.makeComplexInt(); |
13979 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
13980 | return true; |
13981 | } |
13982 | |
13983 | case CK_IntegralComplexCast: { |
13984 | if (!Visit(E->getSubExpr())) |
13985 | return false; |
13986 | |
13987 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
13988 | QualType From |
13989 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
13990 | |
13991 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
13992 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
13993 | return true; |
13994 | } |
13995 | |
13996 | case CK_IntegralComplexToFloatingComplex: { |
13997 | if (!Visit(E->getSubExpr())) |
13998 | return false; |
13999 | |
14000 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14001 | Info.Ctx.getLangOpts()); |
14002 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14003 | QualType From |
14004 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14005 | Result.makeComplexFloat(); |
14006 | return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal, |
14007 | To, Result.FloatReal) && |
14008 | HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag, |
14009 | To, Result.FloatImag); |
14010 | } |
14011 | } |
14012 | |
14013 | llvm_unreachable("unknown cast resulting in complex value"); |
14014 | } |
14015 | |
14016 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14017 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14018 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14019 | |
14020 | |
14021 | |
14022 | bool LHSReal = false, RHSReal = false; |
14023 | |
14024 | bool LHSOK; |
14025 | if (E->getLHS()->getType()->isRealFloatingType()) { |
14026 | LHSReal = true; |
14027 | APFloat &Real = Result.FloatReal; |
14028 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
14029 | if (LHSOK) { |
14030 | Result.makeComplexFloat(); |
14031 | Result.FloatImag = APFloat(Real.getSemantics()); |
14032 | } |
14033 | } else { |
14034 | LHSOK = Visit(E->getLHS()); |
14035 | } |
14036 | if (!LHSOK && !Info.noteFailure()) |
14037 | return false; |
14038 | |
14039 | ComplexValue RHS; |
14040 | if (E->getRHS()->getType()->isRealFloatingType()) { |
14041 | RHSReal = true; |
14042 | APFloat &Real = RHS.FloatReal; |
14043 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
14044 | return false; |
14045 | RHS.makeComplexFloat(); |
14046 | RHS.FloatImag = APFloat(Real.getSemantics()); |
14047 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
14048 | return false; |
14049 | |
14050 | assert(!(LHSReal && RHSReal) && |
14051 | "Cannot have both operands of a complex operation be real."); |
14052 | switch (E->getOpcode()) { |
14053 | default: return Error(E); |
14054 | case BO_Add: |
14055 | if (Result.isComplexFloat()) { |
14056 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
14057 | APFloat::rmNearestTiesToEven); |
14058 | if (LHSReal) |
14059 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14060 | else if (!RHSReal) |
14061 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
14062 | APFloat::rmNearestTiesToEven); |
14063 | } else { |
14064 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
14065 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
14066 | } |
14067 | break; |
14068 | case BO_Sub: |
14069 | if (Result.isComplexFloat()) { |
14070 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
14071 | APFloat::rmNearestTiesToEven); |
14072 | if (LHSReal) { |
14073 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14074 | Result.getComplexFloatImag().changeSign(); |
14075 | } else if (!RHSReal) { |
14076 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
14077 | APFloat::rmNearestTiesToEven); |
14078 | } |
14079 | } else { |
14080 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
14081 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
14082 | } |
14083 | break; |
14084 | case BO_Mul: |
14085 | if (Result.isComplexFloat()) { |
14086 | |
14087 | |
14088 | |
14089 | |
14090 | ComplexValue LHS = Result; |
14091 | APFloat &A = LHS.getComplexFloatReal(); |
14092 | APFloat &B = LHS.getComplexFloatImag(); |
14093 | APFloat &C = RHS.getComplexFloatReal(); |
14094 | APFloat &D = RHS.getComplexFloatImag(); |
14095 | APFloat &ResR = Result.getComplexFloatReal(); |
14096 | APFloat &ResI = Result.getComplexFloatImag(); |
14097 | if (LHSReal) { |
14098 | assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
14099 | ResR = A * C; |
14100 | ResI = A * D; |
14101 | } else if (RHSReal) { |
14102 | ResR = C * A; |
14103 | ResI = C * B; |
14104 | } else { |
14105 | |
14106 | |
14107 | APFloat AC = A * C; |
14108 | APFloat BD = B * D; |
14109 | APFloat AD = A * D; |
14110 | APFloat BC = B * C; |
14111 | ResR = AC - BD; |
14112 | ResI = AD + BC; |
14113 | if (ResR.isNaN() && ResI.isNaN()) { |
14114 | bool Recalc = false; |
14115 | if (A.isInfinity() || B.isInfinity()) { |
14116 | A = APFloat::copySign( |
14117 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
14118 | B = APFloat::copySign( |
14119 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
14120 | if (C.isNaN()) |
14121 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
14122 | if (D.isNaN()) |
14123 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
14124 | Recalc = true; |
14125 | } |
14126 | if (C.isInfinity() || D.isInfinity()) { |
14127 | C = APFloat::copySign( |
14128 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
14129 | D = APFloat::copySign( |
14130 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
14131 | if (A.isNaN()) |
14132 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
14133 | if (B.isNaN()) |
14134 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
14135 | Recalc = true; |
14136 | } |
14137 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
14138 | AD.isInfinity() || BC.isInfinity())) { |
14139 | if (A.isNaN()) |
14140 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
14141 | if (B.isNaN()) |
14142 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
14143 | if (C.isNaN()) |
14144 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
14145 | if (D.isNaN()) |
14146 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
14147 | Recalc = true; |
14148 | } |
14149 | if (Recalc) { |
14150 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
14151 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
14152 | } |
14153 | } |
14154 | } |
14155 | } else { |
14156 | ComplexValue LHS = Result; |
14157 | Result.getComplexIntReal() = |
14158 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
14159 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
14160 | Result.getComplexIntImag() = |
14161 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
14162 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
14163 | } |
14164 | break; |
14165 | case BO_Div: |
14166 | if (Result.isComplexFloat()) { |
14167 | |
14168 | |
14169 | |
14170 | |
14171 | ComplexValue LHS = Result; |
14172 | APFloat &A = LHS.getComplexFloatReal(); |
14173 | APFloat &B = LHS.getComplexFloatImag(); |
14174 | APFloat &C = RHS.getComplexFloatReal(); |
14175 | APFloat &D = RHS.getComplexFloatImag(); |
14176 | APFloat &ResR = Result.getComplexFloatReal(); |
14177 | APFloat &ResI = Result.getComplexFloatImag(); |
14178 | if (RHSReal) { |
14179 | ResR = A / C; |
14180 | ResI = B / C; |
14181 | } else { |
14182 | if (LHSReal) { |
14183 | |
14184 | B = APFloat::getZero(A.getSemantics()); |
14185 | } |
14186 | int DenomLogB = 0; |
14187 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
14188 | if (MaxCD.isFinite()) { |
14189 | DenomLogB = ilogb(MaxCD); |
14190 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
14191 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
14192 | } |
14193 | APFloat Denom = C * C + D * D; |
14194 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
14195 | APFloat::rmNearestTiesToEven); |
14196 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
14197 | APFloat::rmNearestTiesToEven); |
14198 | if (ResR.isNaN() && ResI.isNaN()) { |
14199 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
14200 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
14201 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
14202 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
14203 | D.isFinite()) { |
14204 | A = APFloat::copySign( |
14205 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
14206 | B = APFloat::copySign( |
14207 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
14208 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
14209 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
14210 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
14211 | C = APFloat::copySign( |
14212 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
14213 | D = APFloat::copySign( |
14214 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
14215 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
14216 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
14217 | } |
14218 | } |
14219 | } |
14220 | } else { |
14221 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
14222 | return Error(E, diag::note_expr_divide_by_zero); |
14223 | |
14224 | ComplexValue LHS = Result; |
14225 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
14226 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
14227 | Result.getComplexIntReal() = |
14228 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
14229 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
14230 | Result.getComplexIntImag() = |
14231 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
14232 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
14233 | } |
14234 | break; |
14235 | } |
14236 | |
14237 | return true; |
14238 | } |
14239 | |
14240 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14241 | |
14242 | if (!Visit(E->getSubExpr())) |
14243 | return false; |
14244 | |
14245 | switch (E->getOpcode()) { |
14246 | default: |
14247 | return Error(E); |
14248 | case UO_Extension: |
14249 | return true; |
14250 | case UO_Plus: |
14251 | |
14252 | return true; |
14253 | case UO_Minus: |
14254 | if (Result.isComplexFloat()) { |
14255 | Result.getComplexFloatReal().changeSign(); |
14256 | Result.getComplexFloatImag().changeSign(); |
14257 | } |
14258 | else { |
14259 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
14260 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
14261 | } |
14262 | return true; |
14263 | case UO_Not: |
14264 | if (Result.isComplexFloat()) |
14265 | Result.getComplexFloatImag().changeSign(); |
14266 | else |
14267 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
14268 | return true; |
14269 | } |
14270 | } |
14271 | |
14272 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
14273 | if (E->getNumInits() == 2) { |
14274 | if (E->getType()->isComplexType()) { |
14275 | Result.makeComplexFloat(); |
14276 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
14277 | return false; |
14278 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
14279 | return false; |
14280 | } else { |
14281 | Result.makeComplexInt(); |
14282 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
14283 | return false; |
14284 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
14285 | return false; |
14286 | } |
14287 | return true; |
14288 | } |
14289 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
14290 | } |
14291 | |
14292 | bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) { |
14293 | switch (E->getBuiltinCallee()) { |
14294 | case Builtin::BI__builtin_complex: |
14295 | Result.makeComplexFloat(); |
14296 | if (!EvaluateFloat(E->getArg(0), Result.FloatReal, Info)) |
14297 | return false; |
14298 | if (!EvaluateFloat(E->getArg(1), Result.FloatImag, Info)) |
14299 | return false; |
14300 | return true; |
14301 | |
14302 | default: |
14303 | break; |
14304 | } |
14305 | |
14306 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14307 | } |
14308 | |
14309 | |
14310 | |
14311 | |
14312 | |
14313 | |
14314 | namespace { |
14315 | class AtomicExprEvaluator : |
14316 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
14317 | const LValue *This; |
14318 | APValue &Result; |
14319 | public: |
14320 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
14321 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
14322 | |
14323 | bool Success(const APValue &V, const Expr *E) { |
14324 | Result = V; |
14325 | return true; |
14326 | } |
14327 | |
14328 | bool ZeroInitialization(const Expr *E) { |
14329 | ImplicitValueInitExpr VIE( |
14330 | E->getType()->castAs<AtomicType>()->getValueType()); |
14331 | |
14332 | |
14333 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
14334 | : Evaluate(Result, Info, &VIE); |
14335 | } |
14336 | |
14337 | bool VisitCastExpr(const CastExpr *E) { |
14338 | switch (E->getCastKind()) { |
14339 | default: |
14340 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14341 | case CK_NonAtomicToAtomic: |
14342 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
14343 | : Evaluate(Result, Info, E->getSubExpr()); |
14344 | } |
14345 | } |
14346 | }; |
14347 | } |
14348 | |
14349 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
14350 | EvalInfo &Info) { |
14351 | assert(!E->isValueDependent()); |
14352 | assert(E->isPRValue() && E->getType()->isAtomicType()); |
14353 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
14354 | } |
14355 | |
14356 | |
14357 | |
14358 | |
14359 | |
14360 | |
14361 | namespace { |
14362 | class VoidExprEvaluator |
14363 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
14364 | public: |
14365 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
14366 | |
14367 | bool Success(const APValue &V, const Expr *e) { return true; } |
14368 | |
14369 | bool ZeroInitialization(const Expr *E) { return true; } |
14370 | |
14371 | bool VisitCastExpr(const CastExpr *E) { |
14372 | switch (E->getCastKind()) { |
14373 | default: |
14374 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14375 | case CK_ToVoid: |
14376 | VisitIgnoredValue(E->getSubExpr()); |
14377 | return true; |
14378 | } |
14379 | } |
14380 | |
14381 | bool VisitCallExpr(const CallExpr *E) { |
14382 | switch (E->getBuiltinCallee()) { |
14383 | case Builtin::BI__assume: |
14384 | case Builtin::BI__builtin_assume: |
14385 | |
14386 | return true; |
14387 | |
14388 | case Builtin::BI__builtin_operator_delete: |
14389 | return HandleOperatorDeleteCall(Info, E); |
14390 | |
14391 | default: |
14392 | break; |
14393 | } |
14394 | |
14395 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14396 | } |
14397 | |
14398 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
14399 | }; |
14400 | } |
14401 | |
14402 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
14403 | |
14404 | if (Info.SpeculativeEvaluationDepth) |
14405 | return false; |
14406 | |
14407 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
14408 | if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) { |
14409 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
14410 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
14411 | return false; |
14412 | } |
14413 | |
14414 | const Expr *Arg = E->getArgument(); |
14415 | |
14416 | LValue Pointer; |
14417 | if (!EvaluatePointer(Arg, Pointer, Info)) |
14418 | return false; |
14419 | if (Pointer.Designator.Invalid) |
14420 | return false; |
14421 | |
14422 | |
14423 | if (Pointer.isNullPointer()) { |
14424 | |
14425 | |
14426 | |
14427 | if (!Info.getLangOpts().CPlusPlus20) |
14428 | Info.CCEDiag(E, diag::note_constexpr_new); |
14429 | return true; |
14430 | } |
14431 | |
14432 | Optional<DynAlloc *> Alloc = CheckDeleteKind( |
14433 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
14434 | if (!Alloc) |
14435 | return false; |
14436 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
14437 | |
14438 | |
14439 | |
14440 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
14441 | !hasVirtualDestructor(Arg->getType()->getPointeeType())) { |
14442 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
14443 | << Arg->getType()->getPointeeType() << AllocType; |
14444 | return false; |
14445 | } |
14446 | |
14447 | |
14448 | |
14449 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
14450 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(AllocType); |
14451 | if (VirtualDelete && |
14452 | !VirtualDelete->isReplaceableGlobalAllocationFunction()) { |
14453 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
14454 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
14455 | return false; |
14456 | } |
14457 | } |
14458 | |
14459 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
14460 | (*Alloc)->Value, AllocType)) |
14461 | return false; |
14462 | |
14463 | if (!Info.HeapAllocs.erase(Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
14464 | |
14465 | |
14466 | |
14467 | |
14468 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
14469 | return false; |
14470 | } |
14471 | |
14472 | return true; |
14473 | } |
14474 | |
14475 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
14476 | assert(!E->isValueDependent()); |
14477 | assert(E->isPRValue() && E->getType()->isVoidType()); |
14478 | return VoidExprEvaluator(Info).Visit(E); |
14479 | } |
14480 | |
14481 | |
14482 | |
14483 | |
14484 | |
14485 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
14486 | assert(!E->isValueDependent()); |
14487 | |
14488 | |
14489 | QualType T = E->getType(); |
14490 | if (E->isGLValue() || T->isFunctionType()) { |
14491 | LValue LV; |
14492 | if (!EvaluateLValue(E, LV, Info)) |
14493 | return false; |
14494 | LV.moveInto(Result); |
14495 | } else if (T->isVectorType()) { |
14496 | if (!EvaluateVector(E, Result, Info)) |
14497 | return false; |
14498 | } else if (T->isIntegralOrEnumerationType()) { |
14499 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
14500 | return false; |
14501 | } else if (T->hasPointerRepresentation()) { |
14502 | LValue LV; |
14503 | if (!EvaluatePointer(E, LV, Info)) |
14504 | return false; |
14505 | LV.moveInto(Result); |
14506 | } else if (T->isRealFloatingType()) { |
14507 | llvm::APFloat F(0.0); |
14508 | if (!EvaluateFloat(E, F, Info)) |
14509 | return false; |
14510 | Result = APValue(F); |
14511 | } else if (T->isAnyComplexType()) { |
14512 | ComplexValue C; |
14513 | if (!EvaluateComplex(E, C, Info)) |
14514 | return false; |
14515 | C.moveInto(Result); |
14516 | } else if (T->isFixedPointType()) { |
14517 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
14518 | } else if (T->isMemberPointerType()) { |
14519 | MemberPtr P; |
14520 | if (!EvaluateMemberPointer(E, P, Info)) |
14521 | return false; |
14522 | P.moveInto(Result); |
14523 | return true; |
14524 | } else if (T->isArrayType()) { |
14525 | LValue LV; |
14526 | APValue &Value = |
14527 | Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV); |
14528 | if (!EvaluateArray(E, LV, Value, Info)) |
14529 | return false; |
14530 | Result = Value; |
14531 | } else if (T->isRecordType()) { |
14532 | LValue LV; |
14533 | APValue &Value = |
14534 | Info.CurrentCall->createTemporary(E, T, ScopeKind::FullExpression, LV); |
14535 | if (!EvaluateRecord(E, LV, Value, Info)) |
14536 | return false; |
14537 | Result = Value; |
14538 | } else if (T->isVoidType()) { |
14539 | if (!Info.getLangOpts().CPlusPlus11) |
14540 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
14541 | << E->getType(); |
14542 | if (!EvaluateVoid(E, Info)) |
14543 | return false; |
14544 | } else if (T->isAtomicType()) { |
14545 | QualType Unqual = T.getAtomicUnqualifiedType(); |
14546 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
14547 | LValue LV; |
14548 | APValue &Value = Info.CurrentCall->createTemporary( |
14549 | E, Unqual, ScopeKind::FullExpression, LV); |
14550 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
14551 | return false; |
14552 | } else { |
14553 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
14554 | return false; |
14555 | } |
14556 | } else if (Info.getLangOpts().CPlusPlus11) { |
14557 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
14558 | return false; |
14559 | } else { |
14560 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
14561 | return false; |
14562 | } |
14563 | |
14564 | return true; |
14565 | } |
14566 | |
14567 | |
14568 | |
14569 | |
14570 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
14571 | const Expr *E, bool AllowNonLiteralTypes) { |
14572 | assert(!E->isValueDependent()); |
14573 | |
14574 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
14575 | return false; |
14576 | |
14577 | if (E->isPRValue()) { |
14578 | |
14579 | |
14580 | QualType T = E->getType(); |
14581 | if (T->isArrayType()) |
14582 | return EvaluateArray(E, This, Result, Info); |
14583 | else if (T->isRecordType()) |
14584 | return EvaluateRecord(E, This, Result, Info); |
14585 | else if (T->isAtomicType()) { |
14586 | QualType Unqual = T.getAtomicUnqualifiedType(); |
14587 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
14588 | return EvaluateAtomic(E, &This, Result, Info); |
14589 | } |
14590 | } |
14591 | |
14592 | |
14593 | return Evaluate(Result, Info, E); |
14594 | } |
14595 | |
14596 | |
14597 | |
14598 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
14599 | assert(!E->isValueDependent()); |
14600 | if (Info.EnableNewConstInterp) { |
14601 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Info, E, Result)) |
14602 | return false; |
14603 | } else { |
14604 | if (E->getType().isNull()) |
14605 | return false; |
14606 | |
14607 | if (!CheckLiteralType(Info, E)) |
14608 | return false; |
14609 | |
14610 | if (!::Evaluate(Result, Info, E)) |
14611 | return false; |
14612 | |
14613 | if (E->isGLValue()) { |
14614 | LValue LV; |
14615 | LV.setFrom(Info.Ctx, Result); |
14616 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
14617 | return false; |
14618 | } |
14619 | } |
14620 | |
14621 | |
14622 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
14623 | ConstantExprKind::Normal) && |
14624 | CheckMemoryLeaks(Info); |
14625 | } |
14626 | |
14627 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
14628 | const ASTContext &Ctx, bool &IsConst) { |
14629 | |
14630 | |
14631 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
14632 | Result.Val = APValue(APSInt(L->getValue(), |
14633 | L->getType()->isUnsignedIntegerType())); |
14634 | IsConst = true; |
14635 | return true; |
14636 | } |
14637 | |
14638 | |
14639 | |
14640 | if (Exp->getType().isNull()) { |
14641 | IsConst = false; |
14642 | return true; |
14643 | } |
14644 | |
14645 | |
14646 | |
14647 | if (Exp->isPRValue() && |
14648 | (Exp->getType()->isArrayType() || Exp->getType()->isRecordType()) && |
14649 | !Ctx.getLangOpts().CPlusPlus11) { |
14650 | IsConst = false; |
14651 | return true; |
14652 | } |
14653 | return false; |
14654 | } |
14655 | |
14656 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
14657 | Expr::SideEffectsKind SEK) { |
14658 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
14659 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
14660 | } |
14661 | |
14662 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
14663 | const ASTContext &Ctx, EvalInfo &Info) { |
14664 | assert(!E->isValueDependent()); |
14665 | bool IsConst; |
14666 | if (FastEvaluateAsRValue(E, Result, Ctx, IsConst)) |
14667 | return IsConst; |
14668 | |
14669 | return EvaluateAsRValue(Info, E, Result.Val); |
14670 | } |
14671 | |
14672 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
14673 | const ASTContext &Ctx, |
14674 | Expr::SideEffectsKind AllowSideEffects, |
14675 | EvalInfo &Info) { |
14676 | assert(!E->isValueDependent()); |
14677 | if (!E->getType()->isIntegralOrEnumerationType()) |
14678 | return false; |
14679 | |
14680 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) || |
14681 | !ExprResult.Val.isInt() || |
14682 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14683 | return false; |
14684 | |
14685 | return true; |
14686 | } |
14687 | |
14688 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
14689 | const ASTContext &Ctx, |
14690 | Expr::SideEffectsKind AllowSideEffects, |
14691 | EvalInfo &Info) { |
14692 | assert(!E->isValueDependent()); |
14693 | if (!E->getType()->isFixedPointType()) |
14694 | return false; |
14695 | |
14696 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info)) |
14697 | return false; |
14698 | |
14699 | if (!ExprResult.Val.isFixedPoint() || |
14700 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14701 | return false; |
14702 | |
14703 | return true; |
14704 | } |
14705 | |
14706 | |
14707 | |
14708 | |
14709 | |
14710 | |
14711 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
14712 | bool InConstantContext) const { |
14713 | assert(!isValueDependent() && |
14714 | "Expression evaluator can't be called on a dependent expression."); |
14715 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14716 | Info.InConstantContext = InConstantContext; |
14717 | return ::EvaluateAsRValue(this, Result, Ctx, Info); |
14718 | } |
14719 | |
14720 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
14721 | bool InConstantContext) const { |
14722 | assert(!isValueDependent() && |
14723 | "Expression evaluator can't be called on a dependent expression."); |
14724 | EvalResult Scratch; |
14725 | return EvaluateAsRValue(Scratch, Ctx, InConstantContext) && |
14726 | HandleConversionToBool(Scratch.Val, Result); |
14727 | } |
14728 | |
14729 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
14730 | SideEffectsKind AllowSideEffects, |
14731 | bool InConstantContext) const { |
14732 | assert(!isValueDependent() && |
14733 | "Expression evaluator can't be called on a dependent expression."); |
14734 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14735 | Info.InConstantContext = InConstantContext; |
14736 | return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info); |
14737 | } |
14738 | |
14739 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
14740 | SideEffectsKind AllowSideEffects, |
14741 | bool InConstantContext) const { |
14742 | assert(!isValueDependent() && |
14743 | "Expression evaluator can't be called on a dependent expression."); |
14744 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
14745 | Info.InConstantContext = InConstantContext; |
14746 | return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info); |
14747 | } |
14748 | |
14749 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
14750 | SideEffectsKind AllowSideEffects, |
14751 | bool InConstantContext) const { |
14752 | assert(!isValueDependent() && |
14753 | "Expression evaluator can't be called on a dependent expression."); |
14754 | |
14755 | if (!getType()->isRealFloatingType()) |
14756 | return false; |
14757 | |
14758 | EvalResult ExprResult; |
14759 | if (!EvaluateAsRValue(ExprResult, Ctx, InConstantContext) || |
14760 | !ExprResult.Val.isFloat() || |
14761 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
14762 | return false; |
14763 | |
14764 | Result = ExprResult.Val.getFloat(); |
14765 | return true; |
14766 | } |
14767 | |
14768 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
14769 | bool InConstantContext) const { |
14770 | assert(!isValueDependent() && |
14771 | "Expression evaluator can't be called on a dependent expression."); |
14772 | |
14773 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
14774 | Info.InConstantContext = InConstantContext; |
14775 | LValue LV; |
14776 | CheckedTemporaries CheckedTemps; |
14777 | if (!EvaluateLValue(this, LV, Info) || !Info.discardCleanups() || |
14778 | Result.HasSideEffects || |
14779 | !CheckLValueConstantExpression(Info, getExprLoc(), |
14780 | Ctx.getLValueReferenceType(getType()), LV, |
14781 | ConstantExprKind::Normal, CheckedTemps)) |
14782 | return false; |
14783 | |
14784 | LV.moveInto(Result.Val); |
14785 | return true; |
14786 | } |
14787 | |
14788 | static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base, |
14789 | APValue DestroyedValue, QualType Type, |
14790 | SourceLocation Loc, Expr::EvalStatus &EStatus, |
14791 | bool IsConstantDestruction) { |
14792 | EvalInfo Info(Ctx, EStatus, |
14793 | IsConstantDestruction ? EvalInfo::EM_ConstantExpression |
14794 | : EvalInfo::EM_ConstantFold); |
14795 | Info.setEvaluatingDecl(Base, DestroyedValue, |
14796 | EvalInfo::EvaluatingDeclKind::Dtor); |
14797 | Info.InConstantContext = IsConstantDestruction; |
14798 | |
14799 | LValue LVal; |
14800 | LVal.set(Base); |
14801 | |
14802 | if (!HandleDestruction(Info, Loc, Base, DestroyedValue, Type) || |
14803 | EStatus.HasSideEffects) |
14804 | return false; |
14805 | |
14806 | if (!Info.discardCleanups()) |
14807 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14808 | |
14809 | return true; |
14810 | } |
14811 | |
14812 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, |
14813 | ConstantExprKind Kind) const { |
14814 | assert(!isValueDependent() && |
14815 | "Expression evaluator can't be called on a dependent expression."); |
14816 | |
14817 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
14818 | EvalInfo Info(Ctx, Result, EM); |
14819 | Info.InConstantContext = true; |
14820 | |
14821 | |
14822 | QualType T = getType(); |
14823 | if (Kind == ConstantExprKind::ClassTemplateArgument) |
14824 | T.addConst(); |
14825 | |
14826 | |
14827 | |
14828 | |
14829 | MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true); |
14830 | APValue::LValueBase Base(&BaseMTE); |
14831 | |
14832 | Info.setEvaluatingDecl(Base, Result.Val); |
14833 | LValue LVal; |
14834 | LVal.set(Base); |
14835 | |
14836 | if (!::EvaluateInPlace(Result.Val, Info, LVal, this) || Result.HasSideEffects) |
14837 | return false; |
14838 | |
14839 | if (!Info.discardCleanups()) |
14840 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14841 | |
14842 | if (!CheckConstantExpression(Info, getExprLoc(), getStorageType(Ctx, this), |
14843 | Result.Val, Kind)) |
14844 | return false; |
14845 | if (!CheckMemoryLeaks(Info)) |
14846 | return false; |
14847 | |
14848 | |
14849 | |
14850 | if (Kind == ConstantExprKind::ClassTemplateArgument && |
14851 | (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result, |
14852 | true) || |
14853 | Result.HasSideEffects)) { |
14854 | |
14855 | |
14856 | return false; |
14857 | } |
14858 | |
14859 | return true; |
14860 | } |
14861 | |
14862 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
14863 | const VarDecl *VD, |
14864 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
14865 | bool IsConstantInitialization) const { |
14866 | assert(!isValueDependent() && |
14867 | "Expression evaluator can't be called on a dependent expression."); |
14868 | |
14869 | |
14870 | |
14871 | if (isPRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
14872 | !Ctx.getLangOpts().CPlusPlus11) |
14873 | return false; |
14874 | |
14875 | Expr::EvalStatus EStatus; |
14876 | EStatus.Diag = &Notes; |
14877 | |
14878 | EvalInfo Info(Ctx, EStatus, |
14879 | (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus11) |
14880 | ? EvalInfo::EM_ConstantExpression |
14881 | : EvalInfo::EM_ConstantFold); |
14882 | Info.setEvaluatingDecl(VD, Value); |
14883 | Info.InConstantContext = IsConstantInitialization; |
14884 | |
14885 | SourceLocation DeclLoc = VD->getLocation(); |
14886 | QualType DeclTy = VD->getType(); |
14887 | |
14888 | if (Info.EnableNewConstInterp) { |
14889 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
14890 | if (!InterpCtx.evaluateAsInitializer(Info, VD, Value)) |
14891 | return false; |
14892 | } else { |
14893 | LValue LVal; |
14894 | LVal.set(VD); |
14895 | |
14896 | if (!EvaluateInPlace(Value, Info, LVal, this, |
14897 | true) || |
14898 | EStatus.HasSideEffects) |
14899 | return false; |
14900 | |
14901 | |
14902 | |
14903 | Info.performLifetimeExtension(); |
14904 | |
14905 | if (!Info.discardCleanups()) |
14906 | llvm_unreachable("Unhandled cleanup; missing full expression marker?"); |
14907 | } |
14908 | return CheckConstantExpression(Info, DeclLoc, DeclTy, Value, |
14909 | ConstantExprKind::Normal) && |
14910 | CheckMemoryLeaks(Info); |
14911 | } |
14912 | |
14913 | bool VarDecl::evaluateDestruction( |
14914 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
14915 | Expr::EvalStatus EStatus; |
14916 | EStatus.Diag = &Notes; |
14917 | |
14918 | |
14919 | |
14920 | bool IsConstantDestruction = hasConstantInitialization(); |
14921 | |
14922 | |
14923 | |
14924 | |
14925 | APValue DestroyedValue; |
14926 | if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent()) |
14927 | DestroyedValue = *getEvaluatedValue(); |
14928 | else if (!getDefaultInitValue(getType(), DestroyedValue)) |
14929 | return false; |
14930 | |
14931 | if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue), |
14932 | getType(), getLocation(), EStatus, |
14933 | IsConstantDestruction) || |
14934 | EStatus.HasSideEffects) |
14935 | return false; |
14936 | |
14937 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
14938 | return true; |
14939 | } |
14940 | |
14941 | |
14942 | |
14943 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
14944 | assert(!isValueDependent() && |
14945 | "Expression evaluator can't be called on a dependent expression."); |
14946 | |
14947 | EvalResult Result; |
14948 | return EvaluateAsRValue(Result, Ctx, true) && |
14949 | !hasUnacceptableSideEffect(Result, SEK); |
14950 | } |
14951 | |
14952 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
14953 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
14954 | assert(!isValueDependent() && |
14955 | "Expression evaluator can't be called on a dependent expression."); |
14956 | |
14957 | EvalResult EVResult; |
14958 | EVResult.Diag = Diag; |
14959 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
14960 | Info.InConstantContext = true; |
14961 | |
14962 | bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info); |
14963 | (void)Result; |
14964 | assert(Result && "Could not evaluate expression"); |
14965 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
14966 | |
14967 | return EVResult.Val.getInt(); |
14968 | } |
14969 | |
14970 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
14971 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
14972 | assert(!isValueDependent() && |
14973 | "Expression evaluator can't be called on a dependent expression."); |
14974 | |
14975 | EvalResult EVResult; |
14976 | EVResult.Diag = Diag; |
14977 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
14978 | Info.InConstantContext = true; |
14979 | Info.CheckingForUndefinedBehavior = true; |
14980 | |
14981 | bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val); |
14982 | (void)Result; |
14983 | assert(Result && "Could not evaluate expression"); |
14984 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
14985 | |
14986 | return EVResult.Val.getInt(); |
14987 | } |
14988 | |
14989 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
14990 | assert(!isValueDependent() && |
14991 | "Expression evaluator can't be called on a dependent expression."); |
14992 | |
14993 | bool IsConst; |
14994 | EvalResult EVResult; |
14995 | if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) { |
14996 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
14997 | Info.CheckingForUndefinedBehavior = true; |
14998 | (void)::EvaluateAsRValue(Info, this, EVResult.Val); |
14999 | } |
15000 | } |
15001 | |
15002 | bool Expr::EvalResult::isGlobalLValue() const { |
15003 | assert(Val.isLValue()); |
15004 | return IsGlobalLValue(Val.getLValueBase()); |
15005 | } |
15006 | |
15007 | |
15008 | |
15009 | |
15010 | |
15011 | |
15012 | |
15013 | |
15014 | |
15015 | |
15016 | |
15017 | |
15018 | |
15019 | |
15020 | |
15021 | |
15022 | namespace { |
15023 | |
15024 | enum ICEKind { |
15025 | |
15026 | IK_ICE, |
15027 | |
15028 | |
15029 | |
15030 | IK_ICEIfUnevaluated, |
15031 | |
15032 | IK_NotICE |
15033 | }; |
15034 | |
15035 | struct ICEDiag { |
15036 | ICEKind Kind; |
15037 | SourceLocation Loc; |
15038 | |
15039 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
15040 | }; |
15041 | |
15042 | } |
15043 | |
15044 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
15045 | |
15046 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
15047 | |
15048 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
15049 | Expr::EvalResult EVResult; |
15050 | Expr::EvalStatus Status; |
15051 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
15052 | |
15053 | Info.InConstantContext = true; |
15054 | if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects || |
15055 | !EVResult.Val.isInt()) |
15056 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15057 | |
15058 | return NoDiag(); |
15059 | } |
15060 | |
15061 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
15062 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
15063 | if (!E->getType()->isIntegralOrEnumerationType()) |
15064 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15065 | |
15066 | switch (E->getStmtClass()) { |
15067 | #define ABSTRACT_STMT(Node) |
15068 | #define STMT(Node, Base) case Expr::Node##Class: |
15069 | #define EXPR(Node, Base) |
15070 | #include "clang/AST/StmtNodes.inc" |
15071 | case Expr::PredefinedExprClass: |
15072 | case Expr::FloatingLiteralClass: |
15073 | case Expr::ImaginaryLiteralClass: |
15074 | case Expr::StringLiteralClass: |
15075 | case Expr::ArraySubscriptExprClass: |
15076 | case Expr::MatrixSubscriptExprClass: |
15077 | case Expr::OMPArraySectionExprClass: |
15078 | case Expr::OMPArrayShapingExprClass: |
15079 | case Expr::OMPIteratorExprClass: |
15080 | case Expr::MemberExprClass: |
15081 | case Expr::CompoundAssignOperatorClass: |
15082 | case Expr::CompoundLiteralExprClass: |
15083 | case Expr::ExtVectorElementExprClass: |
15084 | case Expr::DesignatedInitExprClass: |
15085 | case Expr::ArrayInitLoopExprClass: |
15086 | case Expr::ArrayInitIndexExprClass: |
15087 | case Expr::NoInitExprClass: |
15088 | case Expr::DesignatedInitUpdateExprClass: |
15089 | case Expr::ImplicitValueInitExprClass: |
15090 | case Expr::ParenListExprClass: |
15091 | case Expr::VAArgExprClass: |
15092 | case Expr::AddrLabelExprClass: |
15093 | case Expr::StmtExprClass: |
15094 | case Expr::CXXMemberCallExprClass: |
15095 | case Expr::CUDAKernelCallExprClass: |
15096 | case Expr::CXXAddrspaceCastExprClass: |
15097 | case Expr::CXXDynamicCastExprClass: |
15098 | case Expr::CXXTypeidExprClass: |
15099 | case Expr::CXXUuidofExprClass: |
15100 | case Expr::MSPropertyRefExprClass: |
15101 | case Expr::MSPropertySubscriptExprClass: |
15102 | case Expr::CXXNullPtrLiteralExprClass: |
15103 | case Expr::UserDefinedLiteralClass: |
15104 | case Expr::CXXThisExprClass: |
15105 | case Expr::CXXThrowExprClass: |
15106 | case Expr::CXXNewExprClass: |
15107 | case Expr::CXXDeleteExprClass: |
15108 | case Expr::CXXPseudoDestructorExprClass: |
15109 | case Expr::UnresolvedLookupExprClass: |
15110 | case Expr::TypoExprClass: |
15111 | case Expr::RecoveryExprClass: |
15112 | case Expr::DependentScopeDeclRefExprClass: |
15113 | case Expr::CXXConstructExprClass: |
15114 | case Expr::CXXInheritedCtorInitExprClass: |
15115 | case Expr::CXXStdInitializerListExprClass: |
15116 | case Expr::CXXBindTemporaryExprClass: |
15117 | case Expr::ExprWithCleanupsClass: |
15118 | case Expr::CXXTemporaryObjectExprClass: |
15119 | case Expr::CXXUnresolvedConstructExprClass: |
15120 | case Expr::CXXDependentScopeMemberExprClass: |
15121 | case Expr::UnresolvedMemberExprClass: |
15122 | case Expr::ObjCStringLiteralClass: |
15123 | case Expr::ObjCBoxedExprClass: |
15124 | case Expr::ObjCArrayLiteralClass: |
15125 | case Expr::ObjCDictionaryLiteralClass: |
15126 | case Expr::ObjCEncodeExprClass: |
15127 | case Expr::ObjCMessageExprClass: |
15128 | case Expr::ObjCSelectorExprClass: |
15129 | case Expr::ObjCProtocolExprClass: |
15130 | case Expr::ObjCIvarRefExprClass: |
15131 | case Expr::ObjCPropertyRefExprClass: |
15132 | case Expr::ObjCSubscriptRefExprClass: |
15133 | case Expr::ObjCIsaExprClass: |
15134 | case Expr::ObjCAvailabilityCheckExprClass: |
15135 | case Expr::ShuffleVectorExprClass: |
15136 | case Expr::ConvertVectorExprClass: |
15137 | case Expr::BlockExprClass: |
15138 | case Expr::NoStmtClass: |
15139 | case Expr::OpaqueValueExprClass: |
15140 | case Expr::PackExpansionExprClass: |
15141 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
15142 | case Expr::FunctionParmPackExprClass: |
15143 | case Expr::AsTypeExprClass: |
15144 | case Expr::ObjCIndirectCopyRestoreExprClass: |
15145 | case Expr::MaterializeTemporaryExprClass: |
15146 | case Expr::PseudoObjectExprClass: |
15147 | case Expr::AtomicExprClass: |
15148 | case Expr::LambdaExprClass: |
15149 | case Expr::CXXFoldExprClass: |
15150 | case Expr::CoawaitExprClass: |
15151 | case Expr::DependentCoawaitExprClass: |
15152 | case Expr::CoyieldExprClass: |
15153 | case Expr::SYCLUniqueStableNameExprClass: |
15154 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15155 | |
15156 | case Expr::InitListExprClass: { |
15157 | |
15158 | |
15159 | |
15160 | |
15161 | if (E->isPRValue()) |
15162 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
15163 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
15164 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15165 | } |
15166 | |
15167 | case Expr::SizeOfPackExprClass: |
15168 | case Expr::GNUNullExprClass: |
15169 | case Expr::SourceLocExprClass: |
15170 | return NoDiag(); |
15171 | |
15172 | case Expr::SubstNonTypeTemplateParmExprClass: |
15173 | return |
15174 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
15175 | |
15176 | case Expr::ConstantExprClass: |
15177 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
15178 | |
15179 | case Expr::ParenExprClass: |
15180 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
15181 | case Expr::GenericSelectionExprClass: |
15182 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
15183 | case Expr::IntegerLiteralClass: |
15184 | case Expr::FixedPointLiteralClass: |
15185 | case Expr::CharacterLiteralClass: |
15186 | case Expr::ObjCBoolLiteralExprClass: |
15187 | case Expr::CXXBoolLiteralExprClass: |
15188 | case Expr::CXXScalarValueInitExprClass: |
15189 | case Expr::TypeTraitExprClass: |
15190 | case Expr::ConceptSpecializationExprClass: |
15191 | case Expr::RequiresExprClass: |
15192 | case Expr::ArrayTypeTraitExprClass: |
15193 | case Expr::ExpressionTraitExprClass: |
15194 | case Expr::CXXNoexceptExprClass: |
15195 | return NoDiag(); |
15196 | case Expr::CallExprClass: |
15197 | case Expr::CXXOperatorCallExprClass: { |
15198 | |
15199 | |
15200 | |
15201 | const CallExpr *CE = cast<CallExpr>(E); |
15202 | if (CE->getBuiltinCallee()) |
15203 | return CheckEvalInICE(E, Ctx); |
15204 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15205 | } |
15206 | case Expr::CXXRewrittenBinaryOperatorClass: |
15207 | return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(), |
15208 | Ctx); |
15209 | case Expr::DeclRefExprClass: { |
15210 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
15211 | if (isa<EnumConstantDecl>(D)) |
15212 | return NoDiag(); |
15213 | |
15214 | |
15215 | |
15216 | |
15217 | |
15218 | |
15219 | |
15220 | |
15221 | |
15222 | |
15223 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
15224 | if (VD && VD->isUsableInConstantExpressions(Ctx) && |
15225 | !VD->getType()->isReferenceType()) |
15226 | return NoDiag(); |
15227 | |
15228 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15229 | } |
15230 | case Expr::UnaryOperatorClass: { |
15231 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
15232 | switch (Exp->getOpcode()) { |
15233 | case UO_PostInc: |
15234 | case UO_PostDec: |
15235 | case UO_PreInc: |
15236 | case UO_PreDec: |
15237 | case UO_AddrOf: |
15238 | case UO_Deref: |
15239 | case UO_Coawait: |
15240 | |
15241 | |
15242 | |
15243 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15244 | case UO_Extension: |
15245 | case UO_LNot: |
15246 | case UO_Plus: |
15247 | case UO_Minus: |
15248 | case UO_Not: |
15249 | case UO_Real: |
15250 | case UO_Imag: |
15251 | return CheckICE(Exp->getSubExpr(), Ctx); |
15252 | } |
15253 | llvm_unreachable("invalid unary operator class"); |
15254 | } |
15255 | case Expr::OffsetOfExprClass: { |
15256 | |
15257 | |
15258 | |
15259 | |
15260 | |
15261 | |
15262 | return CheckEvalInICE(E, Ctx); |
15263 | } |
15264 | case Expr::UnaryExprOrTypeTraitExprClass: { |
15265 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
15266 | if ((Exp->getKind() == UETT_SizeOf) && |
15267 | Exp->getTypeOfArgument()->isVariableArrayType()) |
15268 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15269 | return NoDiag(); |
15270 | } |
15271 | case Expr::BinaryOperatorClass: { |
15272 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
15273 | switch (Exp->getOpcode()) { |
15274 | case BO_PtrMemD: |
15275 | case BO_PtrMemI: |
15276 | case BO_Assign: |
15277 | case BO_MulAssign: |
15278 | case BO_DivAssign: |
15279 | case BO_RemAssign: |
15280 | case BO_AddAssign: |
15281 | case BO_SubAssign: |
15282 | case BO_ShlAssign: |
15283 | case BO_ShrAssign: |
15284 | case BO_AndAssign: |
15285 | case BO_XorAssign: |
15286 | case BO_OrAssign: |
15287 | |
15288 | |
15289 | |
15290 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15291 | |
15292 | case BO_Mul: |
15293 | case BO_Div: |
15294 | case BO_Rem: |
15295 | case BO_Add: |
15296 | case BO_Sub: |
15297 | case BO_Shl: |
15298 | case BO_Shr: |
15299 | case BO_LT: |
15300 | case BO_GT: |
15301 | case BO_LE: |
15302 | case BO_GE: |
15303 | case BO_EQ: |
15304 | case BO_NE: |
15305 | case BO_And: |
15306 | case BO_Xor: |
15307 | case BO_Or: |
15308 | case BO_Comma: |
15309 | case BO_Cmp: { |
15310 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
15311 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
15312 | if (Exp->getOpcode() == BO_Div || |
15313 | Exp->getOpcode() == BO_Rem) { |
15314 | |
15315 | |
15316 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
15317 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
15318 | if (REval == 0) |
15319 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15320 | if (REval.isSigned() && REval.isAllOnesValue()) { |
15321 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
15322 | if (LEval.isMinSignedValue()) |
15323 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15324 | } |
15325 | } |
15326 | } |
15327 | if (Exp->getOpcode() == BO_Comma) { |
15328 | if (Ctx.getLangOpts().C99) { |
15329 | |
15330 | |
15331 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
15332 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
15333 | } else { |
15334 | |
15335 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15336 | } |
15337 | } |
15338 | return Worst(LHSResult, RHSResult); |
15339 | } |
15340 | case BO_LAnd: |
15341 | case BO_LOr: { |
15342 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
15343 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
15344 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
15345 | |
15346 | |
15347 | |
15348 | if ((Exp->getOpcode() == BO_LAnd) != |
15349 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
15350 | return RHSResult; |
15351 | return NoDiag(); |
15352 | } |
15353 | |
15354 | return Worst(LHSResult, RHSResult); |
15355 | } |
15356 | } |
15357 | llvm_unreachable("invalid binary operator kind"); |
15358 | } |
15359 | case Expr::ImplicitCastExprClass: |
15360 | case Expr::CStyleCastExprClass: |
15361 | case Expr::CXXFunctionalCastExprClass: |
15362 | case Expr::CXXStaticCastExprClass: |
15363 | case Expr::CXXReinterpretCastExprClass: |
15364 | case Expr::CXXConstCastExprClass: |
15365 | case Expr::ObjCBridgedCastExprClass: { |
15366 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
15367 | if (isa<ExplicitCastExpr>(E)) { |
15368 | if (const FloatingLiteral *FL |
15369 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
15370 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
15371 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
15372 | APSInt IgnoredVal(DestWidth, !DestSigned); |
15373 | bool Ignored; |
15374 | |
15375 | |
15376 | |
15377 | if (FL->getValue().convertToInteger(IgnoredVal, |
15378 | llvm::APFloat::rmTowardZero, |
15379 | &Ignored) & APFloat::opInvalidOp) |
15380 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15381 | return NoDiag(); |
15382 | } |
15383 | } |
15384 | switch (cast<CastExpr>(E)->getCastKind()) { |
15385 | case CK_LValueToRValue: |
15386 | case CK_AtomicToNonAtomic: |
15387 | case CK_NonAtomicToAtomic: |
15388 | case CK_NoOp: |
15389 | case CK_IntegralToBoolean: |
15390 | case CK_IntegralCast: |
15391 | return CheckICE(SubExpr, Ctx); |
15392 | default: |
15393 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15394 | } |
15395 | } |
15396 | case Expr::BinaryConditionalOperatorClass: { |
15397 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
15398 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
15399 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
15400 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
15401 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
15402 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
15403 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
15404 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
15405 | return FalseResult; |
15406 | } |
15407 | case Expr::ConditionalOperatorClass: { |
15408 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
15409 | |
15410 | |
15411 | |
15412 | |
15413 | if (const CallExpr *CallCE |
15414 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
15415 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
15416 | return CheckEvalInICE(E, Ctx); |
15417 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
15418 | if (CondResult.Kind == IK_NotICE) |
15419 | return CondResult; |
15420 | |
15421 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
15422 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
15423 | |
15424 | if (TrueResult.Kind == IK_NotICE) |
15425 | return TrueResult; |
15426 | if (FalseResult.Kind == IK_NotICE) |
15427 | return FalseResult; |
15428 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
15429 | return CondResult; |
15430 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
15431 | return NoDiag(); |
15432 | |
15433 | |
15434 | |
15435 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
15436 | return FalseResult; |
15437 | return TrueResult; |
15438 | } |
15439 | case Expr::CXXDefaultArgExprClass: |
15440 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
15441 | case Expr::CXXDefaultInitExprClass: |
15442 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
15443 | case Expr::ChooseExprClass: { |
15444 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
15445 | } |
15446 | case Expr::BuiltinBitCastExprClass: { |
15447 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
15448 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15449 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
15450 | } |
15451 | } |
15452 | |
15453 | llvm_unreachable("Invalid StmtClass!"); |
15454 | } |
15455 | |
15456 | |
15457 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
15458 | const Expr *E, |
15459 | llvm::APSInt *Value, |
15460 | SourceLocation *Loc) { |
15461 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
15462 | if (Loc) *Loc = E->getExprLoc(); |
15463 | return false; |
15464 | } |
15465 | |
15466 | APValue Result; |
15467 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
15468 | return false; |
15469 | |
15470 | if (!Result.isInt()) { |
15471 | if (Loc) *Loc = E->getExprLoc(); |
15472 | return false; |
15473 | } |
15474 | |
15475 | if (Value) *Value = Result.getInt(); |
15476 | return true; |
15477 | } |
15478 | |
15479 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
15480 | SourceLocation *Loc) const { |
15481 | assert(!isValueDependent() && |
15482 | "Expression evaluator can't be called on a dependent expression."); |
15483 | |
15484 | if (Ctx.getLangOpts().CPlusPlus11) |
15485 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
15486 | |
15487 | ICEDiag D = CheckICE(this, Ctx); |
15488 | if (D.Kind != IK_ICE) { |
15489 | if (Loc) *Loc = D.Loc; |
15490 | return false; |
15491 | } |
15492 | return true; |
15493 | } |
15494 | |
15495 | Optional<llvm::APSInt> Expr::getIntegerConstantExpr(const ASTContext &Ctx, |
15496 | SourceLocation *Loc, |
15497 | bool isEvaluated) const { |
15498 | assert(!isValueDependent() && |
15499 | "Expression evaluator can't be called on a dependent expression."); |
15500 | |
15501 | APSInt Value; |
15502 | |
15503 | if (Ctx.getLangOpts().CPlusPlus11) { |
15504 | if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc)) |
15505 | return Value; |
15506 | return None; |
15507 | } |
15508 | |
15509 | if (!isIntegerConstantExpr(Ctx, Loc)) |
15510 | return None; |
15511 | |
15512 | |
15513 | |
15514 | |
15515 | |
15516 | EvalResult ExprResult; |
15517 | Expr::EvalStatus Status; |
15518 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
15519 | Info.InConstantContext = true; |
15520 | |
15521 | if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) |
15522 | llvm_unreachable("ICE cannot be evaluated!"); |
15523 | |
15524 | return ExprResult.Val.getInt(); |
15525 | } |
15526 | |
15527 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
15528 | assert(!isValueDependent() && |
15529 | "Expression evaluator can't be called on a dependent expression."); |
15530 | |
15531 | return CheckICE(this, Ctx).Kind == IK_ICE; |
15532 | } |
15533 | |
15534 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
15535 | SourceLocation *Loc) const { |
15536 | assert(!isValueDependent() && |
15537 | "Expression evaluator can't be called on a dependent expression."); |
15538 | |
15539 | |
15540 | |
15541 | assert(Ctx.getLangOpts().CPlusPlus); |
15542 | |
15543 | |
15544 | Expr::EvalStatus Status; |
15545 | SmallVector<PartialDiagnosticAt, 8> Diags; |
15546 | Status.Diag = &Diags; |
15547 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
15548 | |
15549 | APValue Scratch; |
15550 | bool IsConstExpr = |
15551 | ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch) && |
15552 | |
15553 | |
15554 | Info.discardCleanups() && !Status.HasSideEffects; |
15555 | |
15556 | if (!Diags.empty()) { |
15557 | IsConstExpr = false; |
15558 | if (Loc) *Loc = Diags[0].first; |
15559 | } else if (!IsConstExpr) { |
15560 | |
15561 | if (Loc) *Loc = getExprLoc(); |
15562 | } |
15563 | |
15564 | return IsConstExpr; |
15565 | } |
15566 | |
15567 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
15568 | const FunctionDecl *Callee, |
15569 | ArrayRef<const Expr*> Args, |
15570 | const Expr *This) const { |
15571 | assert(!isValueDependent() && |
15572 | "Expression evaluator can't be called on a dependent expression."); |
15573 | |
15574 | Expr::EvalStatus Status; |
15575 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
15576 | Info.InConstantContext = true; |
15577 | |
15578 | LValue ThisVal; |
15579 | const LValue *ThisPtr = nullptr; |
15580 | if (This) { |
15581 | #ifndef NDEBUG |
15582 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
15583 | assert(MD && "Don't provide `this` for non-methods."); |
15584 | assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
15585 | #endif |
15586 | if (!This->isValueDependent() && |
15587 | EvaluateObjectArgument(Info, This, ThisVal) && |
15588 | !Info.EvalStatus.HasSideEffects) |
15589 | ThisPtr = &ThisVal; |
15590 | |
15591 | |
15592 | |
15593 | Info.EvalStatus.HasSideEffects = false; |
15594 | } |
15595 | |
15596 | CallRef Call = Info.CurrentCall->createCall(Callee); |
15597 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
15598 | I != E; ++I) { |
15599 | unsigned Idx = I - Args.begin(); |
15600 | if (Idx >= Callee->getNumParams()) |
15601 | break; |
15602 | const ParmVarDecl *PVD = Callee->getParamDecl(Idx); |
15603 | if ((*I)->isValueDependent() || |
15604 | !EvaluateCallArg(PVD, *I, Call, Info) || |
15605 | Info.EvalStatus.HasSideEffects) { |
15606 | |
15607 | if (APValue *Slot = Info.getParamSlot(Call, PVD)) |
15608 | *Slot = APValue(); |
15609 | } |
15610 | |
15611 | |
15612 | |
15613 | Info.EvalStatus.HasSideEffects = false; |
15614 | } |
15615 | |
15616 | |
15617 | |
15618 | Info.discardCleanups(); |
15619 | Info.EvalStatus.HasSideEffects = false; |
15620 | |
15621 | |
15622 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, Call); |
15623 | |
15624 | FullExpressionRAII Scope(Info); |
15625 | return Evaluate(Value, Info, this) && Scope.destroy() && |
15626 | !Info.EvalStatus.HasSideEffects; |
15627 | } |
15628 | |
15629 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
15630 | SmallVectorImpl< |
15631 | PartialDiagnosticAt> &Diags) { |
15632 | |
15633 | |
15634 | |
15635 | if (FD->isDependentContext()) |
15636 | return true; |
15637 | |
15638 | Expr::EvalStatus Status; |
15639 | Status.Diag = &Diags; |
15640 | |
15641 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
15642 | Info.InConstantContext = true; |
15643 | Info.CheckingPotentialConstantExpression = true; |
15644 | |
15645 | |
15646 | if (Info.EnableNewConstInterp) { |
15647 | Info.Ctx.getInterpContext().isPotentialConstantExpr(Info, FD); |
15648 | return Diags.empty(); |
15649 | } |
15650 | |
15651 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
15652 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
15653 | |
15654 | |
15655 | |
15656 | LValue This; |
15657 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
15658 | This.set({&VIE, Info.CurrentCall->Index}); |
15659 | |
15660 | ArrayRef<const Expr*> Args; |
15661 | |
15662 | APValue Scratch; |
15663 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
15664 | |
15665 | |
15666 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
15667 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
15668 | } else { |
15669 | SourceLocation Loc = FD->getLocation(); |
15670 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
15671 | Args, CallRef(), FD->getBody(), Info, Scratch, nullptr); |
15672 | } |
15673 | |
15674 | return Diags.empty(); |
15675 | } |
15676 | |
15677 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
15678 | const FunctionDecl *FD, |
15679 | SmallVectorImpl< |
15680 | PartialDiagnosticAt> &Diags) { |
15681 | assert(!E->isValueDependent() && |
15682 | "Expression evaluator can't be called on a dependent expression."); |
15683 | |
15684 | Expr::EvalStatus Status; |
15685 | Status.Diag = &Diags; |
15686 | |
15687 | EvalInfo Info(FD->getASTContext(), Status, |
15688 | EvalInfo::EM_ConstantExpressionUnevaluated); |
15689 | Info.InConstantContext = true; |
15690 | Info.CheckingPotentialConstantExpression = true; |
15691 | |
15692 | |
15693 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, CallRef()); |
15694 | |
15695 | APValue ResultScratch; |
15696 | Evaluate(ResultScratch, Info, E); |
15697 | return Diags.empty(); |
15698 | } |
15699 | |
15700 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
15701 | unsigned Type) const { |
15702 | if (!getType()->isPointerType()) |
15703 | return false; |
15704 | |
15705 | Expr::EvalStatus Status; |
15706 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
15707 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
15708 | } |
15709 | |
15710 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
15711 | EvalInfo &Info) { |
15712 | if (!E->getType()->hasPointerRepresentation() || !E->isPRValue()) |
15713 | return false; |
15714 | |
15715 | LValue String; |
15716 | |
15717 | if (!EvaluatePointer(E, String, Info)) |
15718 | return false; |
15719 | |
15720 | QualType CharTy = E->getType()->getPointeeType(); |
15721 | |
15722 | |
15723 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
15724 | String.getLValueBase().dyn_cast<const Expr *>())) { |
15725 | StringRef Str = S->getBytes(); |
15726 | int64_t Off = String.Offset.getQuantity(); |
15727 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
15728 | S->getCharByteWidth() == 1 && |
15729 | |
15730 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
15731 | Str = Str.substr(Off); |
15732 | |
15733 | StringRef::size_type Pos = Str.find(0); |
15734 | if (Pos != StringRef::npos) |
15735 | Str = Str.substr(0, Pos); |
15736 | |
15737 | Result = Str.size(); |
15738 | return true; |
15739 | } |
15740 | |
15741 | |
15742 | } |
15743 | |
15744 | |
15745 | for (uint64_t Strlen = 0; ; ++Strlen) { |
15746 | APValue Char; |
15747 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
15748 | !Char.isInt()) |
15749 | return false; |
15750 | if (!Char.getInt()) { |
15751 | Result = Strlen; |
15752 | return true; |
15753 | } |
15754 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
15755 | return false; |
15756 | } |
15757 | } |
15758 | |
15759 | bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const { |
15760 | Expr::EvalStatus Status; |
15761 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
15762 | return EvaluateBuiltinStrLen(this, Result, Info); |
15763 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | #ifndef LLVM_CLANG_AST_TYPE_H |
18 | #define LLVM_CLANG_AST_TYPE_H |
19 | |
20 | #include "clang/AST/DependenceFlags.h" |
21 | #include "clang/AST/NestedNameSpecifier.h" |
22 | #include "clang/AST/TemplateName.h" |
23 | #include "clang/Basic/AddressSpaces.h" |
24 | #include "clang/Basic/AttrKinds.h" |
25 | #include "clang/Basic/Diagnostic.h" |
26 | #include "clang/Basic/ExceptionSpecificationType.h" |
27 | #include "clang/Basic/LLVM.h" |
28 | #include "clang/Basic/Linkage.h" |
29 | #include "clang/Basic/PartialDiagnostic.h" |
30 | #include "clang/Basic/SourceLocation.h" |
31 | #include "clang/Basic/Specifiers.h" |
32 | #include "clang/Basic/Visibility.h" |
33 | #include "llvm/ADT/APInt.h" |
34 | #include "llvm/ADT/APSInt.h" |
35 | #include "llvm/ADT/ArrayRef.h" |
36 | #include "llvm/ADT/FoldingSet.h" |
37 | #include "llvm/ADT/None.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/Twine.h" |
43 | #include "llvm/ADT/iterator_range.h" |
44 | #include "llvm/Support/Casting.h" |
45 | #include "llvm/Support/Compiler.h" |
46 | #include "llvm/Support/ErrorHandling.h" |
47 | #include "llvm/Support/PointerLikeTypeTraits.h" |
48 | #include "llvm/Support/TrailingObjects.h" |
49 | #include "llvm/Support/type_traits.h" |
50 | #include <cassert> |
51 | #include <cstddef> |
52 | #include <cstdint> |
53 | #include <cstring> |
54 | #include <string> |
55 | #include <type_traits> |
56 | #include <utility> |
57 | |
58 | namespace clang { |
59 | |
60 | class ExtQuals; |
61 | class QualType; |
62 | class ConceptDecl; |
63 | class TagDecl; |
64 | class TemplateParameterList; |
65 | class Type; |
66 | |
67 | enum { |
68 | TypeAlignmentInBits = 4, |
69 | TypeAlignment = 1 << TypeAlignmentInBits |
70 | }; |
71 | |
72 | namespace serialization { |
73 | template <class T> class AbstractTypeReader; |
74 | template <class T> class AbstractTypeWriter; |
75 | } |
76 | |
77 | } |
78 | |
79 | namespace llvm { |
80 | |
81 | template <typename T> |
82 | struct PointerLikeTypeTraits; |
83 | template<> |
84 | struct PointerLikeTypeTraits< ::clang::Type*> { |
85 | static inline void *getAsVoidPointer(::clang::Type *P) { return P; } |
86 | |
87 | static inline ::clang::Type *getFromVoidPointer(void *P) { |
88 | return static_cast< ::clang::Type*>(P); |
89 | } |
90 | |
91 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
92 | }; |
93 | |
94 | template<> |
95 | struct PointerLikeTypeTraits< ::clang::ExtQuals*> { |
96 | static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } |
97 | |
98 | static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { |
99 | return static_cast< ::clang::ExtQuals*>(P); |
100 | } |
101 | |
102 | static constexpr int NumLowBitsAvailable = clang::TypeAlignmentInBits; |
103 | }; |
104 | |
105 | } |
106 | |
107 | namespace clang { |
108 | |
109 | class ASTContext; |
110 | template <typename> class CanQual; |
111 | class CXXRecordDecl; |
112 | class DeclContext; |
113 | class EnumDecl; |
114 | class Expr; |
115 | class ExtQualsTypeCommonBase; |
116 | class FunctionDecl; |
117 | class IdentifierInfo; |
118 | class NamedDecl; |
119 | class ObjCInterfaceDecl; |
120 | class ObjCProtocolDecl; |
121 | class ObjCTypeParamDecl; |
122 | struct PrintingPolicy; |
123 | class RecordDecl; |
124 | class Stmt; |
125 | class TagDecl; |
126 | class TemplateArgument; |
127 | class TemplateArgumentListInfo; |
128 | class TemplateArgumentLoc; |
129 | class TemplateTypeParmDecl; |
130 | class TypedefNameDecl; |
131 | class UnresolvedUsingTypenameDecl; |
132 | |
133 | using CanQualType = CanQual<Type>; |
134 | |
135 | |
136 | #define TYPE(Class, Base) class Class##Type; |
137 | #include "clang/AST/TypeNodes.inc" |
138 | |
139 | |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | class Qualifiers { |
146 | public: |
147 | enum TQ { |
148 | Const = 0x1, |
149 | Restrict = 0x2, |
150 | Volatile = 0x4, |
151 | CVRMask = Const | Volatile | Restrict |
152 | }; |
153 | |
154 | enum GC { |
155 | GCNone = 0, |
156 | Weak, |
157 | Strong |
158 | }; |
159 | |
160 | enum ObjCLifetime { |
161 | |
162 | OCL_None, |
163 | |
164 | |
165 | |
166 | OCL_ExplicitNone, |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | OCL_Strong, |
174 | |
175 | |
176 | OCL_Weak, |
177 | |
178 | |
179 | OCL_Autoreleasing |
180 | }; |
181 | |
182 | enum { |
183 | |
184 | |
185 | MaxAddressSpace = 0x7fffffu, |
186 | |
187 | |
188 | FastWidth = 3, |
189 | |
190 | |
191 | FastMask = (1 << FastWidth) - 1 |
192 | }; |
193 | |
194 | |
195 | |
196 | static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) { |
197 | |
198 | if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) { |
199 | Qualifiers Q; |
200 | Q.Mask = L.Mask & R.Mask; |
201 | L.Mask &= ~Q.Mask; |
202 | R.Mask &= ~Q.Mask; |
203 | return Q; |
204 | } |
205 | |
206 | Qualifiers Q; |
207 | unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers(); |
208 | Q.addCVRQualifiers(CommonCRV); |
209 | L.removeCVRQualifiers(CommonCRV); |
210 | R.removeCVRQualifiers(CommonCRV); |
211 | |
212 | if (L.getObjCGCAttr() == R.getObjCGCAttr()) { |
213 | Q.setObjCGCAttr(L.getObjCGCAttr()); |
214 | L.removeObjCGCAttr(); |
215 | R.removeObjCGCAttr(); |
216 | } |
217 | |
218 | if (L.getObjCLifetime() == R.getObjCLifetime()) { |
219 | Q.setObjCLifetime(L.getObjCLifetime()); |
220 | L.removeObjCLifetime(); |
221 | R.removeObjCLifetime(); |
222 | } |
223 | |
224 | if (L.getAddressSpace() == R.getAddressSpace()) { |
225 | Q.setAddressSpace(L.getAddressSpace()); |
226 | L.removeAddressSpace(); |
227 | R.removeAddressSpace(); |
228 | } |
229 | return Q; |
230 | } |
231 | |
232 | static Qualifiers fromFastMask(unsigned Mask) { |
233 | Qualifiers Qs; |
234 | Qs.addFastQualifiers(Mask); |
235 | return Qs; |
236 | } |
237 | |
238 | static Qualifiers fromCVRMask(unsigned CVR) { |
239 | Qualifiers Qs; |
240 | Qs.addCVRQualifiers(CVR); |
241 | return Qs; |
242 | } |
243 | |
244 | static Qualifiers fromCVRUMask(unsigned CVRU) { |
245 | Qualifiers Qs; |
246 | Qs.addCVRUQualifiers(CVRU); |
247 | return Qs; |
248 | } |
249 | |
250 | |
251 | static Qualifiers fromOpaqueValue(unsigned opaque) { |
252 | Qualifiers Qs; |
253 | Qs.Mask = opaque; |
254 | return Qs; |
255 | } |
256 | |
257 | |
258 | unsigned getAsOpaqueValue() const { |
259 | return Mask; |
260 | } |
261 | |
262 | bool hasConst() const { return Mask & Const; } |
263 | bool hasOnlyConst() const { return Mask == Const; } |
264 | void removeConst() { Mask &= ~Const; } |
265 | void addConst() { Mask |= Const; } |
266 | |
267 | bool hasVolatile() const { return Mask & Volatile; } |
268 | bool hasOnlyVolatile() const { return Mask == Volatile; } |
269 | void removeVolatile() { Mask &= ~Volatile; } |
270 | void addVolatile() { Mask |= Volatile; } |
271 | |
272 | bool hasRestrict() const { return Mask & Restrict; } |
273 | bool hasOnlyRestrict() const { return Mask == Restrict; } |
274 | void removeRestrict() { Mask &= ~Restrict; } |
275 | void addRestrict() { Mask |= Restrict; } |
276 | |
277 | bool hasCVRQualifiers() const { return getCVRQualifiers(); } |
278 | unsigned getCVRQualifiers() const { return Mask & CVRMask; } |
279 | unsigned getCVRUQualifiers() const { return Mask & (CVRMask | UMask); } |
280 | |
281 | void setCVRQualifiers(unsigned mask) { |
282 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
283 | Mask = (Mask & ~CVRMask) | mask; |
284 | } |
285 | void removeCVRQualifiers(unsigned mask) { |
286 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
287 | Mask &= ~mask; |
288 | } |
289 | void removeCVRQualifiers() { |
290 | removeCVRQualifiers(CVRMask); |
291 | } |
292 | void addCVRQualifiers(unsigned mask) { |
293 | assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); |
294 | Mask |= mask; |
295 | } |
296 | void addCVRUQualifiers(unsigned mask) { |
297 | assert(!(mask & ~CVRMask & ~UMask) && "bitmask contains non-CVRU bits"); |
298 | Mask |= mask; |
299 | } |
300 | |
301 | bool hasUnaligned() const { return Mask & UMask; } |
302 | void setUnaligned(bool flag) { |
303 | Mask = (Mask & ~UMask) | (flag ? UMask : 0); |
304 | } |
305 | void removeUnaligned() { Mask &= ~UMask; } |
306 | void addUnaligned() { Mask |= UMask; } |
307 | |
308 | bool hasObjCGCAttr() const { return Mask & GCAttrMask; } |
309 | GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } |
310 | void setObjCGCAttr(GC type) { |
311 | Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); |
312 | } |
313 | void removeObjCGCAttr() { setObjCGCAttr(GCNone); } |
314 | void addObjCGCAttr(GC type) { |
315 | assert(type); |
316 | setObjCGCAttr(type); |
317 | } |
318 | Qualifiers withoutObjCGCAttr() const { |
319 | Qualifiers qs = *this; |
320 | qs.removeObjCGCAttr(); |
321 | return qs; |
322 | } |
323 | Qualifiers withoutObjCLifetime() const { |
324 | Qualifiers qs = *this; |
325 | qs.removeObjCLifetime(); |
326 | return qs; |
327 | } |
328 | Qualifiers withoutAddressSpace() const { |
329 | Qualifiers qs = *this; |
330 | qs.removeAddressSpace(); |
331 | return qs; |
332 | } |
333 | |
334 | bool hasObjCLifetime() const { return Mask & LifetimeMask; } |
335 | ObjCLifetime getObjCLifetime() const { |
336 | return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift); |
337 | } |
338 | void setObjCLifetime(ObjCLifetime type) { |
339 | Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift); |
340 | } |
341 | void removeObjCLifetime() { setObjCLifetime(OCL_None); } |
342 | void addObjCLifetime(ObjCLifetime type) { |
343 | assert(type); |
344 | assert(!hasObjCLifetime()); |
345 | Mask |= (type << LifetimeShift); |
346 | } |
347 | |
348 | |
349 | bool hasNonTrivialObjCLifetime() const { |
350 | ObjCLifetime lifetime = getObjCLifetime(); |
351 | return (lifetime > OCL_ExplicitNone); |
352 | } |
353 | |
354 | |
355 | bool hasStrongOrWeakObjCLifetime() const { |
356 | ObjCLifetime lifetime = getObjCLifetime(); |
357 | return (lifetime == OCL_Strong || lifetime == OCL_Weak); |
358 | } |
359 | |
360 | bool hasAddressSpace() const { return Mask & AddressSpaceMask; } |
361 | LangAS getAddressSpace() const { |
362 | return static_cast<LangAS>(Mask >> AddressSpaceShift); |
363 | } |
364 | bool hasTargetSpecificAddressSpace() const { |
365 | return isTargetAddressSpace(getAddressSpace()); |
366 | } |
367 | |
368 | unsigned getAddressSpaceAttributePrintValue() const { |
369 | auto Addr = getAddressSpace(); |
370 | |
371 | |
372 | |
373 | assert(Addr == LangAS::Default || hasTargetSpecificAddressSpace()); |
374 | if (Addr != LangAS::Default) |
375 | return toTargetAddressSpace(Addr); |
376 | |
377 | |
378 | |
379 | return 0; |
380 | } |
381 | void setAddressSpace(LangAS space) { |
382 | assert((unsigned)space <= MaxAddressSpace); |
383 | Mask = (Mask & ~AddressSpaceMask) |
384 | | (((uint32_t) space) << AddressSpaceShift); |
385 | } |
386 | void removeAddressSpace() { setAddressSpace(LangAS::Default); } |
387 | void addAddressSpace(LangAS space) { |
388 | assert(space != LangAS::Default); |
389 | setAddressSpace(space); |
390 | } |
391 | |
392 | |
393 | |
394 | bool hasFastQualifiers() const { return getFastQualifiers(); } |
395 | unsigned getFastQualifiers() const { return Mask & FastMask; } |
396 | void setFastQualifiers(unsigned mask) { |
397 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
398 | Mask = (Mask & ~FastMask) | mask; |
399 | } |
400 | void removeFastQualifiers(unsigned mask) { |
401 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
402 | Mask &= ~mask; |
403 | } |
404 | void removeFastQualifiers() { |
405 | removeFastQualifiers(FastMask); |
406 | } |
407 | void addFastQualifiers(unsigned mask) { |
408 | assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); |
409 | Mask |= mask; |
410 | } |
411 | |
412 | |
413 | |
414 | bool hasNonFastQualifiers() const { return Mask & ~FastMask; } |
415 | Qualifiers getNonFastQualifiers() const { |
416 | Qualifiers Quals = *this; |
417 | Quals.setFastQualifiers(0); |
418 | return Quals; |
419 | } |
420 | |
421 | |
422 | bool hasQualifiers() const { return Mask; } |
423 | bool empty() const { return !Mask; } |
424 | |
425 | |
426 | void addQualifiers(Qualifiers Q) { |
427 | |
428 | |
429 | if (!(Q.Mask & ~CVRMask)) |
430 | Mask |= Q.Mask; |
431 | else { |
432 | Mask |= (Q.Mask & CVRMask); |
433 | if (Q.hasAddressSpace()) |
434 | addAddressSpace(Q.getAddressSpace()); |
435 | if (Q.hasObjCGCAttr()) |
436 | addObjCGCAttr(Q.getObjCGCAttr()); |
437 | if (Q.hasObjCLifetime()) |
438 | addObjCLifetime(Q.getObjCLifetime()); |
439 | } |
440 | } |
441 | |
442 | |
443 | void removeQualifiers(Qualifiers Q) { |
444 | |
445 | |
446 | if (!(Q.Mask & ~CVRMask)) |
447 | Mask &= ~Q.Mask; |
448 | else { |
449 | Mask &= ~(Q.Mask & CVRMask); |
450 | if (getObjCGCAttr() == Q.getObjCGCAttr()) |
451 | removeObjCGCAttr(); |
452 | if (getObjCLifetime() == Q.getObjCLifetime()) |
453 | removeObjCLifetime(); |
454 | if (getAddressSpace() == Q.getAddressSpace()) |
455 | removeAddressSpace(); |
456 | } |
457 | } |
458 | |
459 | |
460 | |
461 | void addConsistentQualifiers(Qualifiers qs) { |
462 | assert(getAddressSpace() == qs.getAddressSpace() || |
463 | !hasAddressSpace() || !qs.hasAddressSpace()); |
464 | assert(getObjCGCAttr() == qs.getObjCGCAttr() || |
465 | !hasObjCGCAttr() || !qs.hasObjCGCAttr()); |
466 | assert(getObjCLifetime() == qs.getObjCLifetime() || |
467 | !hasObjCLifetime() || !qs.hasObjCLifetime()); |
468 | Mask |= qs.Mask; |
469 | } |
470 | |
471 | |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | |
478 | static bool isAddressSpaceSupersetOf(LangAS A, LangAS B) { |
479 | |
480 | return A == B || |
481 | |
482 | |
483 | (A == LangAS::opencl_generic && B != LangAS::opencl_constant) || |
484 | |
485 | |
486 | |
487 | (A == LangAS::opencl_global && (B == LangAS::opencl_global_device || |
488 | B == LangAS::opencl_global_host)) || |
489 | (A == LangAS::sycl_global && (B == LangAS::sycl_global_device || |
490 | B == LangAS::sycl_global_host)) || |
491 | |
492 | ((isPtrSizeAddressSpace(A) || A == LangAS::Default) && |
493 | (isPtrSizeAddressSpace(B) || B == LangAS::Default)) || |
494 | |
495 | (A == LangAS::Default && |
496 | (B == LangAS::sycl_private || B == LangAS::sycl_local || |
497 | B == LangAS::sycl_global || B == LangAS::sycl_global_device || |
498 | B == LangAS::sycl_global_host)) || |
499 | |
500 | |
501 | (A == LangAS::Default && |
502 | (B == LangAS::cuda_constant || B == LangAS::cuda_device || |
503 | B == LangAS::cuda_shared)); |
504 | } |
505 | |
506 | |
507 | |
508 | bool isAddressSpaceSupersetOf(Qualifiers other) const { |
509 | return isAddressSpaceSupersetOf(getAddressSpace(), other.getAddressSpace()); |
510 | } |
511 | |
512 | |
513 | |
514 | |
515 | bool compatiblyIncludes(Qualifiers other) const { |
516 | return isAddressSpaceSupersetOf(other) && |
517 | |
518 | |
519 | (getObjCGCAttr() == other.getObjCGCAttr() || !hasObjCGCAttr() || |
520 | !other.hasObjCGCAttr()) && |
521 | |
522 | getObjCLifetime() == other.getObjCLifetime() && |
523 | |
524 | (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask)) && |
525 | |
526 | (!other.hasUnaligned() || hasUnaligned()); |
527 | } |
528 | |
529 | |
530 | |
531 | |
532 | |
533 | |
534 | |
535 | |
536 | bool compatiblyIncludesObjCLifetime(Qualifiers other) const { |
537 | if (getObjCLifetime() == other.getObjCLifetime()) |
538 | return true; |
539 | |
540 | if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak) |
541 | return false; |
542 | |
543 | if (getObjCLifetime() == OCL_None || other.getObjCLifetime() == OCL_None) |
544 | return true; |
545 | |
546 | return hasConst(); |
547 | } |
548 | |
549 | |
550 | |
551 | bool isStrictSupersetOf(Qualifiers Other) const; |
552 | |
553 | bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } |
554 | bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } |
555 | |
556 | explicit operator bool() const { return hasQualifiers(); } |
557 | |
558 | Qualifiers &operator+=(Qualifiers R) { |
559 | addQualifiers(R); |
560 | return *this; |
561 | } |
562 | |
563 | |
564 | |
565 | friend Qualifiers operator+(Qualifiers L, Qualifiers R) { |
566 | L += R; |
567 | return L; |
568 | } |
569 | |
570 | Qualifiers &operator-=(Qualifiers R) { |
571 | removeQualifiers(R); |
572 | return *this; |
573 | } |
574 | |
575 | |
576 | friend Qualifiers operator-(Qualifiers L, Qualifiers R) { |
577 | L -= R; |
578 | return L; |
579 | } |
580 | |
581 | std::string getAsString() const; |
582 | std::string getAsString(const PrintingPolicy &Policy) const; |
583 | |
584 | static std::string getAddrSpaceAsString(LangAS AS); |
585 | |
586 | bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const; |
587 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
588 | bool appendSpaceIfNonEmpty = false) const; |
589 | |
590 | void Profile(llvm::FoldingSetNodeID &ID) const { |
591 | ID.AddInteger(Mask); |
592 | } |
593 | |
594 | private: |
595 | |
596 | |
597 | uint32_t Mask = 0; |
598 | |
599 | static const uint32_t UMask = 0x8; |
600 | static const uint32_t UShift = 3; |
601 | static const uint32_t GCAttrMask = 0x30; |
602 | static const uint32_t GCAttrShift = 4; |
603 | static const uint32_t LifetimeMask = 0x1C0; |
604 | static const uint32_t LifetimeShift = 6; |
605 | static const uint32_t AddressSpaceMask = |
606 | ~(CVRMask | UMask | GCAttrMask | LifetimeMask); |
607 | static const uint32_t AddressSpaceShift = 9; |
608 | }; |
609 | |
610 | |
611 | |
612 | struct SplitQualType { |
613 | |
614 | const Type *Ty = nullptr; |
615 | |
616 | |
617 | Qualifiers Quals; |
618 | |
619 | SplitQualType() = default; |
620 | SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {} |
621 | |
622 | SplitQualType getSingleStepDesugaredType() const; |
623 | |
624 | |
625 | std::pair<const Type *,Qualifiers> asPair() const { |
626 | return std::pair<const Type *, Qualifiers>(Ty, Quals); |
627 | } |
628 | |
629 | friend bool operator==(SplitQualType a, SplitQualType b) { |
630 | return a.Ty == b.Ty && a.Quals == b.Quals; |
631 | } |
632 | friend bool operator!=(SplitQualType a, SplitQualType b) { |
633 | return a.Ty != b.Ty || a.Quals != b.Quals; |
634 | } |
635 | }; |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | enum class ObjCSubstitutionContext { |
643 | |
644 | Ordinary, |
645 | |
646 | |
647 | Result, |
648 | |
649 | |
650 | Parameter, |
651 | |
652 | |
653 | Property, |
654 | |
655 | |
656 | Superclass, |
657 | }; |
658 | |
659 | |
660 | |
661 | |
662 | |
663 | |
664 | |
665 | |
666 | |
667 | |
668 | |
669 | |
670 | |
671 | |
672 | |
673 | class QualType { |
674 | friend class QualifierCollector; |
675 | |
676 | |
677 | llvm::PointerIntPair<llvm::PointerUnion<const Type *, const ExtQuals *>, |
678 | Qualifiers::FastWidth> Value; |
679 | |
680 | const ExtQuals *getExtQualsUnsafe() const { |
681 | return Value.getPointer().get<const ExtQuals*>(); |
682 | } |
683 | |
684 | const Type *getTypePtrUnsafe() const { |
685 | return Value.getPointer().get<const Type*>(); |
686 | } |
687 | |
688 | const ExtQualsTypeCommonBase *getCommonPtr() const { |
689 | assert(!isNull() && "Cannot retrieve a NULL type pointer"); |
690 | auto CommonPtrVal = reinterpret_cast<uintptr_t>(Value.getOpaqueValue()); |
691 | CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1); |
692 | return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal); |
693 | } |
694 | |
695 | public: |
696 | QualType() = default; |
697 | QualType(const Type *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
698 | QualType(const ExtQuals *Ptr, unsigned Quals) : Value(Ptr, Quals) {} |
699 | |
700 | unsigned getLocalFastQualifiers() const { return Value.getInt(); } |
701 | void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } |
702 | |
703 | |
704 | |
705 | |
706 | |
707 | const Type *getTypePtr() const; |
708 | |
709 | const Type *getTypePtrOrNull() const; |
710 | |
711 | |
712 | const IdentifierInfo *getBaseTypeIdentifier() const; |
713 | |
714 | |
715 | |
716 | SplitQualType split() const; |
717 | |
718 | void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } |
719 | |
720 | static QualType getFromOpaquePtr(const void *Ptr) { |
721 | QualType T; |
722 | T.Value.setFromOpaqueValue(const_cast<void*>(Ptr)); |
723 | return T; |
724 | } |
725 | |
726 | const Type &operator*() const { |
727 | return *getTypePtr(); |
728 | } |
729 | |
730 | const Type *operator->() const { |
731 | return getTypePtr(); |
732 | } |
733 | |
734 | bool isCanonical() const; |
735 | bool isCanonicalAsParam() const; |
736 | |
737 | |
738 | bool isNull() const { |
739 | return Value.getPointer().isNull(); |
740 | } |
741 | |
742 | |
743 | |
744 | |
745 | bool isLocalConstQualified() const { |
746 | return (getLocalFastQualifiers() & Qualifiers::Const); |
747 | } |
748 | |
749 | |
750 | bool isConstQualified() const; |
751 | |
752 | |
753 | |
754 | |
755 | bool isLocalRestrictQualified() const { |
756 | return (getLocalFastQualifiers() & Qualifiers::Restrict); |
757 | } |
758 | |
759 | |
760 | bool isRestrictQualified() const; |
761 | |
762 | |
763 | |
764 | |
765 | bool isLocalVolatileQualified() const { |
766 | return (getLocalFastQualifiers() & Qualifiers::Volatile); |
767 | } |
768 | |
769 | |
770 | bool isVolatileQualified() const; |
771 | |
772 | |
773 | |
774 | |
775 | bool hasLocalQualifiers() const { |
776 | return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); |
777 | } |
778 | |
779 | |
780 | bool hasQualifiers() const; |
781 | |
782 | |
783 | |
784 | |
785 | bool hasLocalNonFastQualifiers() const { |
786 | return Value.getPointer().is<const ExtQuals*>(); |
787 | } |
788 | |
789 | |
790 | |
791 | |
792 | Qualifiers getLocalQualifiers() const; |
793 | |
794 | |
795 | Qualifiers getQualifiers() const; |
796 | |
797 | |
798 | |
799 | |
800 | unsigned getLocalCVRQualifiers() const { |
801 | return getLocalFastQualifiers(); |
802 | } |
803 | |
804 | |
805 | |
806 | unsigned getCVRQualifiers() const; |
807 | |
808 | bool isConstant(const ASTContext& Ctx) const { |
809 | return QualType::isConstant(*this, Ctx); |
810 | } |
811 | |
812 | |
813 | bool isPODType(const ASTContext &Context) const; |
814 | |
815 | |
816 | |
817 | bool isCXX98PODType(const ASTContext &Context) const; |
818 | |
819 | |
820 | |
821 | |
822 | |
823 | bool isCXX11PODType(const ASTContext &Context) const; |
824 | |
825 | |
826 | bool isTrivialType(const ASTContext &Context) const; |
827 | |
828 | |
829 | bool isTriviallyCopyableType(const ASTContext &Context) const; |
830 | |
831 | |
832 | |
833 | bool mayBeDynamicClass() const; |
834 | |
835 | |
836 | bool mayBeNotDynamicClass() const; |
837 | |
838 | |
839 | |
840 | |
841 | |
842 | void addConst() { |
843 | addFastQualifiers(Qualifiers::Const); |
844 | } |
845 | QualType withConst() const { |
846 | return withFastQualifiers(Qualifiers::Const); |
847 | } |
848 | |
849 | |
850 | void addVolatile() { |
851 | addFastQualifiers(Qualifiers::Volatile); |
852 | } |
853 | QualType withVolatile() const { |
854 | return withFastQualifiers(Qualifiers::Volatile); |
855 | } |
856 | |
857 | |
858 | void addRestrict() { |
859 | addFastQualifiers(Qualifiers::Restrict); |
860 | } |
861 | QualType withRestrict() const { |
862 | return withFastQualifiers(Qualifiers::Restrict); |
863 | } |
864 | |
865 | QualType withCVRQualifiers(unsigned CVR) const { |
866 | return withFastQualifiers(CVR); |
867 | } |
868 | |
869 | void addFastQualifiers(unsigned TQs) { |
870 | assert(!(TQs & ~Qualifiers::FastMask) |
871 | && "non-fast qualifier bits set in mask!"); |
872 | Value.setInt(Value.getInt() | TQs); |
873 | } |
874 | |
875 | void removeLocalConst(); |
876 | void removeLocalVolatile(); |
877 | void removeLocalRestrict(); |
878 | void removeLocalCVRQualifiers(unsigned Mask); |
879 | |
880 | void removeLocalFastQualifiers() { Value.setInt(0); } |
881 | void removeLocalFastQualifiers(unsigned Mask) { |
882 | assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); |
883 | Value.setInt(Value.getInt() & ~Mask); |
884 | } |
885 | |
886 | |
887 | |
888 | QualType withFastQualifiers(unsigned TQs) const { |
889 | QualType T = *this; |
890 | T.addFastQualifiers(TQs); |
891 | return T; |
892 | } |
893 | |
894 | |
895 | |
896 | QualType withExactLocalFastQualifiers(unsigned TQs) const { |
897 | return withoutLocalFastQualifiers().withFastQualifiers(TQs); |
898 | } |
899 | |
900 | |
901 | QualType withoutLocalFastQualifiers() const { |
902 | QualType T = *this; |
903 | T.removeLocalFastQualifiers(); |
904 | return T; |
905 | } |
906 | |
907 | QualType getCanonicalType() const; |
908 | |
909 | |
910 | |
911 | |
912 | QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } |
913 | |
914 | |
915 | |
916 | |
917 | |
918 | |
919 | |
920 | |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | |
928 | |
929 | |
930 | |
931 | |
932 | inline QualType getUnqualifiedType() const; |
933 | |
934 | |
935 | |
936 | |
937 | |
938 | |
939 | |
940 | |
941 | |
942 | |
943 | inline SplitQualType getSplitUnqualifiedType() const; |
944 | |
945 | |
946 | |
947 | bool isMoreQualifiedThan(QualType Other) const; |
948 | |
949 | |
950 | |
951 | bool isAtLeastAsQualifiedAs(QualType Other) const; |
952 | |
953 | QualType getNonReferenceType() const; |
954 | |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | QualType getNonLValueExprType(const ASTContext &Context) const; |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | QualType getNonPackExpansionType() const; |
970 | |
971 | |
972 | |
973 | |
974 | |
975 | |
976 | |
977 | |
978 | |
979 | QualType getDesugaredType(const ASTContext &Context) const { |
980 | return getDesugaredType(*this, Context); |
981 | } |
982 | |
983 | SplitQualType getSplitDesugaredType() const { |
984 | return getSplitDesugaredType(*this); |
985 | } |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | |
992 | QualType getSingleStepDesugaredType(const ASTContext &Context) const { |
993 | return getSingleStepDesugaredTypeImpl(*this, Context); |
994 | } |
995 | |
996 | |
997 | |
998 | QualType IgnoreParens() const { |
999 | if (isa<ParenType>(*this)) |
1000 | return QualType::IgnoreParens(*this); |
1001 | return *this; |
1002 | } |
1003 | |
1004 | |
1005 | friend bool operator==(const QualType &LHS, const QualType &RHS) { |
1006 | return LHS.Value == RHS.Value; |
1007 | } |
1008 | friend bool operator!=(const QualType &LHS, const QualType &RHS) { |
1009 | return LHS.Value != RHS.Value; |
1010 | } |
1011 | friend bool operator<(const QualType &LHS, const QualType &RHS) { |
1012 | return LHS.Value < RHS.Value; |
1013 | } |
1014 | |
1015 | static std::string getAsString(SplitQualType split, |
1016 | const PrintingPolicy &Policy) { |
1017 | return getAsString(split.Ty, split.Quals, Policy); |
1018 | } |
1019 | static std::string getAsString(const Type *ty, Qualifiers qs, |
1020 | const PrintingPolicy &Policy); |
1021 | |
1022 | std::string getAsString() const; |
1023 | std::string getAsString(const PrintingPolicy &Policy) const; |
1024 | |
1025 | void print(raw_ostream &OS, const PrintingPolicy &Policy, |
1026 | const Twine &PlaceHolder = Twine(), |
1027 | unsigned Indentation = 0) const; |
1028 | |
1029 | static void print(SplitQualType split, raw_ostream &OS, |
1030 | const PrintingPolicy &policy, const Twine &PlaceHolder, |
1031 | unsigned Indentation = 0) { |
1032 | return print(split.Ty, split.Quals, OS, policy, PlaceHolder, Indentation); |
1033 | } |
1034 | |
1035 | static void print(const Type *ty, Qualifiers qs, |
1036 | raw_ostream &OS, const PrintingPolicy &policy, |
1037 | const Twine &PlaceHolder, |
1038 | unsigned Indentation = 0); |
1039 | |
1040 | void getAsStringInternal(std::string &Str, |
1041 | const PrintingPolicy &Policy) const; |
1042 | |
1043 | static void getAsStringInternal(SplitQualType split, std::string &out, |
1044 | const PrintingPolicy &policy) { |
1045 | return getAsStringInternal(split.Ty, split.Quals, out, policy); |
1046 | } |
1047 | |
1048 | static void getAsStringInternal(const Type *ty, Qualifiers qs, |
1049 | std::string &out, |
1050 | const PrintingPolicy &policy); |
1051 | |
1052 | class StreamedQualTypeHelper { |
1053 | const QualType &T; |
1054 | const PrintingPolicy &Policy; |
1055 | const Twine &PlaceHolder; |
1056 | unsigned Indentation; |
1057 | |
1058 | public: |
1059 | StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy, |
1060 | const Twine &PlaceHolder, unsigned Indentation) |
1061 | : T(T), Policy(Policy), PlaceHolder(PlaceHolder), |
1062 | Indentation(Indentation) {} |
1063 | |
1064 | friend raw_ostream &operator<<(raw_ostream &OS, |
1065 | const StreamedQualTypeHelper &SQT) { |
1066 | SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder, SQT.Indentation); |
1067 | return OS; |
1068 | } |
1069 | }; |
1070 | |
1071 | StreamedQualTypeHelper stream(const PrintingPolicy &Policy, |
1072 | const Twine &PlaceHolder = Twine(), |
1073 | unsigned Indentation = 0) const { |
1074 | return StreamedQualTypeHelper(*this, Policy, PlaceHolder, Indentation); |
1075 | } |
1076 | |
1077 | void dump(const char *s) const; |
1078 | void dump() const; |
1079 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
1080 | |
1081 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1082 | ID.AddPointer(getAsOpaquePtr()); |
1083 | } |
1084 | |
1085 | |
1086 | inline bool hasAddressSpace() const; |
1087 | |
1088 | |
1089 | inline LangAS getAddressSpace() const; |
1090 | |
1091 | |
1092 | |
1093 | |
1094 | |
1095 | |
1096 | |
1097 | |
1098 | |
1099 | bool isAddressSpaceOverlapping(QualType T) const { |
1100 | Qualifiers Q = getQualifiers(); |
1101 | Qualifiers TQ = T.getQualifiers(); |
1102 | |
1103 | return Q.isAddressSpaceSupersetOf(TQ) || TQ.isAddressSpaceSupersetOf(Q); |
1104 | } |
1105 | |
1106 | |
1107 | inline Qualifiers::GC getObjCGCAttr() const; |
1108 | |
1109 | |
1110 | bool isObjCGCWeak() const { |
1111 | return getObjCGCAttr() == Qualifiers::Weak; |
1112 | } |
1113 | |
1114 | |
1115 | bool isObjCGCStrong() const { |
1116 | return getObjCGCAttr() == Qualifiers::Strong; |
1117 | } |
1118 | |
1119 | |
1120 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1121 | return getQualifiers().getObjCLifetime(); |
1122 | } |
1123 | |
1124 | bool hasNonTrivialObjCLifetime() const { |
1125 | return getQualifiers().hasNonTrivialObjCLifetime(); |
1126 | } |
1127 | |
1128 | bool hasStrongOrWeakObjCLifetime() const { |
1129 | return getQualifiers().hasStrongOrWeakObjCLifetime(); |
1130 | } |
1131 | |
1132 | |
1133 | bool isNonWeakInMRRWithObjCWeak(const ASTContext &Context) const; |
1134 | |
1135 | enum PrimitiveDefaultInitializeKind { |
1136 | |
1137 | |
1138 | |
1139 | PDIK_Trivial, |
1140 | |
1141 | |
1142 | |
1143 | PDIK_ARCStrong, |
1144 | |
1145 | |
1146 | |
1147 | PDIK_ARCWeak, |
1148 | |
1149 | |
1150 | PDIK_Struct |
1151 | }; |
1152 | |
1153 | |
1154 | |
1155 | |
1156 | |
1157 | |
1158 | PrimitiveDefaultInitializeKind |
1159 | isNonTrivialToPrimitiveDefaultInitialize() const; |
1160 | |
1161 | enum PrimitiveCopyKind { |
1162 | |
1163 | |
1164 | |
1165 | PCK_Trivial, |
1166 | |
1167 | |
1168 | |
1169 | |
1170 | PCK_VolatileTrivial, |
1171 | |
1172 | |
1173 | |
1174 | PCK_ARCStrong, |
1175 | |
1176 | |
1177 | |
1178 | PCK_ARCWeak, |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | PCK_Struct |
1187 | }; |
1188 | |
1189 | |
1190 | |
1191 | |
1192 | PrimitiveCopyKind isNonTrivialToPrimitiveCopy() const; |
1193 | |
1194 | |
1195 | |
1196 | |
1197 | |
1198 | |
1199 | |
1200 | PrimitiveCopyKind isNonTrivialToPrimitiveDestructiveMove() const; |
1201 | |
1202 | enum DestructionKind { |
1203 | DK_none, |
1204 | DK_cxx_destructor, |
1205 | DK_objc_strong_lifetime, |
1206 | DK_objc_weak_lifetime, |
1207 | DK_nontrivial_c_struct |
1208 | }; |
1209 | |
1210 | |
1211 | |
1212 | |
1213 | |
1214 | DestructionKind isDestructedType() const { |
1215 | return isDestructedTypeImpl(*this); |
1216 | } |
1217 | |
1218 | |
1219 | |
1220 | |
1221 | |
1222 | bool hasNonTrivialToPrimitiveDefaultInitializeCUnion() const; |
1223 | |
1224 | |
1225 | |
1226 | |
1227 | bool hasNonTrivialToPrimitiveDestructCUnion() const; |
1228 | |
1229 | |
1230 | |
1231 | |
1232 | bool hasNonTrivialToPrimitiveCopyCUnion() const; |
1233 | |
1234 | |
1235 | |
1236 | |
1237 | |
1238 | |
1239 | |
1240 | |
1241 | |
1242 | |
1243 | |
1244 | bool isCForbiddenLValueType() const; |
1245 | |
1246 | |
1247 | |
1248 | |
1249 | |
1250 | |
1251 | |
1252 | |
1253 | |
1254 | |
1255 | |
1256 | |
1257 | |
1258 | |
1259 | |
1260 | QualType substObjCTypeArgs(ASTContext &ctx, |
1261 | ArrayRef<QualType> typeArgs, |
1262 | ObjCSubstitutionContext context) const; |
1263 | |
1264 | |
1265 | |
1266 | |
1267 | |
1268 | |
1269 | |
1270 | |
1271 | |
1272 | |
1273 | |
1274 | |
1275 | |
1276 | |
1277 | |
1278 | |
1279 | |
1280 | |
1281 | |
1282 | |
1283 | |
1284 | QualType substObjCMemberType(QualType objectType, |
1285 | const DeclContext *dc, |
1286 | ObjCSubstitutionContext context) const; |
1287 | |
1288 | |
1289 | QualType stripObjCKindOfType(const ASTContext &ctx) const; |
1290 | |
1291 | |
1292 | QualType getAtomicUnqualifiedType() const; |
1293 | |
1294 | private: |
1295 | |
1296 | |
1297 | |
1298 | static bool isConstant(QualType T, const ASTContext& Ctx); |
1299 | static QualType getDesugaredType(QualType T, const ASTContext &Context); |
1300 | static SplitQualType getSplitDesugaredType(QualType T); |
1301 | static SplitQualType getSplitUnqualifiedTypeImpl(QualType type); |
1302 | static QualType getSingleStepDesugaredTypeImpl(QualType type, |
1303 | const ASTContext &C); |
1304 | static QualType IgnoreParens(QualType T); |
1305 | static DestructionKind isDestructedTypeImpl(QualType type); |
1306 | |
1307 | |
1308 | static bool hasNonTrivialToPrimitiveDefaultInitializeCUnion(const RecordDecl *RD); |
1309 | static bool hasNonTrivialToPrimitiveDestructCUnion(const RecordDecl *RD); |
1310 | static bool hasNonTrivialToPrimitiveCopyCUnion(const RecordDecl *RD); |
1311 | }; |
1312 | |
1313 | } |
1314 | |
1315 | namespace llvm { |
1316 | |
1317 | |
1318 | |
1319 | template<> struct simplify_type< ::clang::QualType> { |
1320 | using SimpleType = const ::clang::Type *; |
1321 | |
1322 | static SimpleType getSimplifiedValue(::clang::QualType Val) { |
1323 | return Val.getTypePtr(); |
1324 | } |
1325 | }; |
1326 | |
1327 | |
1328 | template<> |
1329 | struct PointerLikeTypeTraits<clang::QualType> { |
1330 | static inline void *getAsVoidPointer(clang::QualType P) { |
1331 | return P.getAsOpaquePtr(); |
1332 | } |
1333 | |
1334 | static inline clang::QualType getFromVoidPointer(void *P) { |
1335 | return clang::QualType::getFromOpaquePtr(P); |
1336 | } |
1337 | |
1338 | |
1339 | static constexpr int NumLowBitsAvailable = 0; |
1340 | }; |
1341 | |
1342 | } |
1343 | |
1344 | namespace clang { |
1345 | |
1346 | |
1347 | |
1348 | |
1349 | class ExtQualsTypeCommonBase { |
1350 | friend class ExtQuals; |
1351 | friend class QualType; |
1352 | friend class Type; |
1353 | |
1354 | |
1355 | |
1356 | |
1357 | |
1358 | |
1359 | const Type *const BaseType; |
1360 | |
1361 | |
1362 | QualType CanonicalType; |
1363 | |
1364 | ExtQualsTypeCommonBase(const Type *baseType, QualType canon) |
1365 | : BaseType(baseType), CanonicalType(canon) {} |
1366 | }; |
1367 | |
1368 | |
1369 | |
1370 | |
1371 | |
1372 | |
1373 | |
1374 | |
1375 | |
1376 | |
1377 | |
1378 | class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode { |
1379 | |
1380 | |
1381 | |
1382 | |
1383 | |
1384 | |
1385 | |
1386 | |
1387 | |
1388 | |
1389 | |
1390 | |
1391 | |
1392 | |
1393 | |
1394 | Qualifiers Quals; |
1395 | |
1396 | ExtQuals *this_() { return this; } |
1397 | |
1398 | public: |
1399 | ExtQuals(const Type *baseType, QualType canon, Qualifiers quals) |
1400 | : ExtQualsTypeCommonBase(baseType, |
1401 | canon.isNull() ? QualType(this_(), 0) : canon), |
1402 | Quals(quals) { |
1403 | assert(Quals.hasNonFastQualifiers() |
1404 | && "ExtQuals created with no fast qualifiers"); |
1405 | assert(!Quals.hasFastQualifiers() |
1406 | && "ExtQuals created with fast qualifiers"); |
1407 | } |
1408 | |
1409 | Qualifiers getQualifiers() const { return Quals; } |
1410 | |
1411 | bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } |
1412 | Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } |
1413 | |
1414 | bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); } |
1415 | Qualifiers::ObjCLifetime getObjCLifetime() const { |
1416 | return Quals.getObjCLifetime(); |
1417 | } |
1418 | |
1419 | bool hasAddressSpace() const { return Quals.hasAddressSpace(); } |
1420 | LangAS getAddressSpace() const { return Quals.getAddressSpace(); } |
1421 | |
1422 | const Type *getBaseType() const { return BaseType; } |
1423 | |
1424 | public: |
1425 | void Profile(llvm::FoldingSetNodeID &ID) const { |
1426 | Profile(ID, getBaseType(), Quals); |
1427 | } |
1428 | |
1429 | static void Profile(llvm::FoldingSetNodeID &ID, |
1430 | const Type *BaseType, |
1431 | Qualifiers Quals) { |
1432 | assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); |
1433 | ID.AddPointer(BaseType); |
1434 | Quals.Profile(ID); |
1435 | } |
1436 | }; |
1437 | |
1438 | |
1439 | |
1440 | |
1441 | enum RefQualifierKind { |
1442 | |
1443 | RQ_None = 0, |
1444 | |
1445 | |
1446 | RQ_LValue, |
1447 | |
1448 | |
1449 | RQ_RValue |
1450 | }; |
1451 | |
1452 | |
1453 | enum class AutoTypeKeyword { |
1454 | |
1455 | Auto, |
1456 | |
1457 | |
1458 | DecltypeAuto, |
1459 | |
1460 | |
1461 | GNUAutoType |
1462 | }; |
1463 | |
1464 | |
1465 | |
1466 | |
1467 | |
1468 | |
1469 | |
1470 | |
1471 | |
1472 | |
1473 | |
1474 | |
1475 | |
1476 | |
1477 | |
1478 | |
1479 | |
1480 | |
1481 | |
1482 | |
1483 | |
1484 | |
1485 | |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | class alignas(8) Type : public ExtQualsTypeCommonBase { |
1491 | public: |
1492 | enum TypeClass { |
1493 | #define TYPE(Class, Base) Class, |
1494 | #define LAST_TYPE(Class) TypeLast = Class |
1495 | #define ABSTRACT_TYPE(Class, Base) |
1496 | #include "clang/AST/TypeNodes.inc" |
1497 | }; |
1498 | |
1499 | private: |
1500 | |
1501 | class TypeBitfields { |
1502 | friend class Type; |
1503 | template <class T> friend class TypePropertyCache; |
1504 | |
1505 | |
1506 | unsigned TC : 8; |
1507 | |
1508 | |
1509 | unsigned Dependence : llvm::BitWidth<TypeDependence>; |
1510 | |
1511 | |
1512 | |
1513 | mutable unsigned CacheValid : 1; |
1514 | |
1515 | |
1516 | mutable unsigned CachedLinkage : 3; |
1517 | |
1518 | |
1519 | mutable unsigned CachedLocalOrUnnamed : 1; |
1520 | |
1521 | |
1522 | mutable unsigned FromAST : 1; |
1523 | |
1524 | bool isCacheValid() const { |
1525 | return CacheValid; |
1526 | } |
1527 | |
1528 | Linkage getLinkage() const { |
1529 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1530 | return static_cast<Linkage>(CachedLinkage); |
1531 | } |
1532 | |
1533 | bool hasLocalOrUnnamedType() const { |
1534 | assert(isCacheValid() && "getting linkage from invalid cache"); |
1535 | return CachedLocalOrUnnamed; |
1536 | } |
1537 | }; |
1538 | enum { NumTypeBits = 8 + llvm::BitWidth<TypeDependence> + 6 }; |
1539 | |
1540 | protected: |
1541 | |
1542 | |
1543 | |
1544 | class ArrayTypeBitfields { |
1545 | friend class ArrayType; |
1546 | |
1547 | unsigned : NumTypeBits; |
1548 | |
1549 | |
1550 | |
1551 | unsigned IndexTypeQuals : 3; |
1552 | |
1553 | |
1554 | |
1555 | |
1556 | unsigned SizeModifier : 3; |
1557 | }; |
1558 | |
1559 | class ConstantArrayTypeBitfields { |
1560 | friend class ConstantArrayType; |
1561 | |
1562 | unsigned : NumTypeBits + 3 + 3; |
1563 | |
1564 | |
1565 | unsigned HasStoredSizeExpr : 1; |
1566 | }; |
1567 | |
1568 | class BuiltinTypeBitfields { |
1569 | friend class BuiltinType; |
1570 | |
1571 | unsigned : NumTypeBits; |
1572 | |
1573 | |
1574 | unsigned Kind : 8; |
1575 | }; |
1576 | |
1577 | |
1578 | |
1579 | |
1580 | class FunctionTypeBitfields { |
1581 | friend class FunctionProtoType; |
1582 | friend class FunctionType; |
1583 | |
1584 | unsigned : NumTypeBits; |
1585 | |
1586 | |
1587 | |
1588 | unsigned ExtInfo : 13; |
1589 | |
1590 | |
1591 | |
1592 | |
1593 | unsigned RefQualifier : 2; |
1594 | |
1595 | |
1596 | |
1597 | |
1598 | |
1599 | |
1600 | |
1601 | unsigned FastTypeQuals : Qualifiers::FastWidth; |
1602 | |
1603 | unsigned HasExtQuals : 1; |
1604 | |
1605 | |
1606 | |
1607 | |
1608 | |
1609 | unsigned NumParams : 16; |
1610 | |
1611 | |
1612 | unsigned ExceptionSpecType : 4; |
1613 | |
1614 | |
1615 | unsigned HasExtParameterInfos : 1; |
1616 | |
1617 | |
1618 | unsigned Variadic : 1; |
1619 | |
1620 | |
1621 | unsigned HasTrailingReturn : 1; |
1622 | }; |
1623 | |
1624 | class ObjCObjectTypeBitfields { |
1625 | friend class ObjCObjectType; |
1626 | |
1627 | unsigned : NumTypeBits; |
1628 | |
1629 | |
1630 | unsigned NumTypeArgs : 7; |
1631 | |
1632 | |
1633 | unsigned NumProtocols : 6; |
1634 | |
1635 | |
1636 | unsigned IsKindOf : 1; |
1637 | }; |
1638 | |
1639 | class ReferenceTypeBitfields { |
1640 | friend class ReferenceType; |
1641 | |
1642 | unsigned : NumTypeBits; |
1643 | |
1644 | |
1645 | |
1646 | |
1647 | |
1648 | |
1649 | |
1650 | |
1651 | |
1652 | |
1653 | |
1654 | |
1655 | unsigned SpelledAsLValue : 1; |
1656 | |
1657 | |
1658 | |
1659 | unsigned InnerRef : 1; |
1660 | }; |
1661 | |
1662 | class TypeWithKeywordBitfields { |
1663 | friend class TypeWithKeyword; |
1664 | |
1665 | unsigned : NumTypeBits; |
1666 | |
1667 | |
1668 | unsigned Keyword : 8; |
1669 | }; |
1670 | |
1671 | enum { NumTypeWithKeywordBits = 8 }; |
1672 | |
1673 | class ElaboratedTypeBitfields { |
1674 | friend class ElaboratedType; |
1675 | |
1676 | unsigned : NumTypeBits; |
1677 | unsigned : NumTypeWithKeywordBits; |
1678 | |
1679 | |
1680 | unsigned HasOwnedTagDecl : 1; |
1681 | }; |
1682 | |
1683 | class VectorTypeBitfields { |
1684 | friend class VectorType; |
1685 | friend class DependentVectorType; |
1686 | |
1687 | unsigned : NumTypeBits; |
1688 | |
1689 | |
1690 | |
1691 | unsigned VecKind : 3; |
1692 | |
1693 | uint32_t NumElements; |
1694 | }; |
1695 | |
1696 | class AttributedTypeBitfields { |
1697 | friend class AttributedType; |
1698 | |
1699 | unsigned : NumTypeBits; |
1700 | |
1701 | |
1702 | unsigned AttrKind : 32 - NumTypeBits; |
1703 | }; |
1704 | |
1705 | class AutoTypeBitfields { |
1706 | friend class AutoType; |
1707 | |
1708 | unsigned : NumTypeBits; |
1709 | |
1710 | |
1711 | |
1712 | unsigned Keyword : 2; |
1713 | |
1714 | |
1715 | |
1716 | |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | unsigned NumArgs; |
1722 | }; |
1723 | |
1724 | class SubstTemplateTypeParmPackTypeBitfields { |
1725 | friend class SubstTemplateTypeParmPackType; |
1726 | |
1727 | unsigned : NumTypeBits; |
1728 | |
1729 | |
1730 | |
1731 | |
1732 | |
1733 | |
1734 | |
1735 | |
1736 | unsigned NumArgs; |
1737 | }; |
1738 | |
1739 | class TemplateSpecializationTypeBitfields { |
1740 | friend class TemplateSpecializationType; |
1741 | |
1742 | unsigned : NumTypeBits; |
1743 | |
1744 | |
1745 | unsigned TypeAlias : 1; |
1746 | |
1747 | |
1748 | |
1749 | |
1750 | |
1751 | |
1752 | |
1753 | |
1754 | unsigned NumArgs; |
1755 | }; |
1756 | |
1757 | class DependentTemplateSpecializationTypeBitfields { |
1758 | friend class DependentTemplateSpecializationType; |
1759 | |
1760 | unsigned : NumTypeBits; |
1761 | unsigned : NumTypeWithKeywordBits; |
1762 | |
1763 | |
1764 | |
1765 | |
1766 | |
1767 | |
1768 | |
1769 | |
1770 | unsigned NumArgs; |
1771 | }; |
1772 | |
1773 | class PackExpansionTypeBitfields { |
1774 | friend class PackExpansionType; |
1775 | |
1776 | unsigned : NumTypeBits; |
1777 | |
1778 | |
1779 | |
1780 | |
1781 | |
1782 | |
1783 | |
1784 | |
1785 | |
1786 | |
1787 | |
1788 | |
1789 | unsigned NumExpansions; |
1790 | }; |
1791 | |
1792 | union { |
1793 | TypeBitfields TypeBits; |
1794 | ArrayTypeBitfields ArrayTypeBits; |
1795 | ConstantArrayTypeBitfields ConstantArrayTypeBits; |
1796 | AttributedTypeBitfields AttributedTypeBits; |
1797 | AutoTypeBitfields AutoTypeBits; |
1798 | BuiltinTypeBitfields BuiltinTypeBits; |
1799 | FunctionTypeBitfields FunctionTypeBits; |
1800 | ObjCObjectTypeBitfields ObjCObjectTypeBits; |
1801 | ReferenceTypeBitfields ReferenceTypeBits; |
1802 | TypeWithKeywordBitfields TypeWithKeywordBits; |
1803 | ElaboratedTypeBitfields ElaboratedTypeBits; |
1804 | VectorTypeBitfields VectorTypeBits; |
1805 | SubstTemplateTypeParmPackTypeBitfields SubstTemplateTypeParmPackTypeBits; |
1806 | TemplateSpecializationTypeBitfields TemplateSpecializationTypeBits; |
1807 | DependentTemplateSpecializationTypeBitfields |
1808 | DependentTemplateSpecializationTypeBits; |
1809 | PackExpansionTypeBitfields PackExpansionTypeBits; |
1810 | }; |
1811 | |
1812 | private: |
1813 | template <class T> friend class TypePropertyCache; |
1814 | |
1815 | |
1816 | void setFromAST(bool V = true) const { |
1817 | TypeBits.FromAST = V; |
1818 | } |
1819 | |
1820 | protected: |
1821 | friend class ASTContext; |
1822 | |
1823 | Type(TypeClass tc, QualType canon, TypeDependence Dependence) |
1824 | : ExtQualsTypeCommonBase(this, |
1825 | canon.isNull() ? QualType(this_(), 0) : canon) { |
1826 | static_assert(sizeof(*this) <= 8 + sizeof(ExtQualsTypeCommonBase), |
1827 | "changing bitfields changed sizeof(Type)!"); |
1828 | static_assert(alignof(decltype(*this)) % sizeof(void *) == 0, |
1829 | "Insufficient alignment!"); |
1830 | TypeBits.TC = tc; |
1831 | TypeBits.Dependence = static_cast<unsigned>(Dependence); |
1832 | TypeBits.CacheValid = false; |
1833 | TypeBits.CachedLocalOrUnnamed = false; |
1834 | TypeBits.CachedLinkage = NoLinkage; |
1835 | TypeBits.FromAST = false; |
1836 | } |
1837 | |
1838 | |
1839 | Type *this_() { return this; } |
1840 | |
1841 | void setDependence(TypeDependence D) { |
1842 | TypeBits.Dependence = static_cast<unsigned>(D); |
1843 | } |
1844 | |
1845 | void addDependence(TypeDependence D) { setDependence(getDependence() | D); } |
1846 | |
1847 | public: |
1848 | friend class ASTReader; |
1849 | friend class ASTWriter; |
1850 | template <class T> friend class serialization::AbstractTypeReader; |
1851 | template <class T> friend class serialization::AbstractTypeWriter; |
1852 | |
1853 | Type(const Type &) = delete; |
1854 | Type(Type &&) = delete; |
1855 | Type &operator=(const Type &) = delete; |
1856 | Type &operator=(Type &&) = delete; |
1857 | |
1858 | TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } |
1859 | |
1860 | |
1861 | bool isFromAST() const { return TypeBits.FromAST; } |
1862 | |
1863 | |
1864 | |
1865 | |
1866 | |
1867 | |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | |
1878 | bool containsUnexpandedParameterPack() const { |
1879 | return getDependence() & TypeDependence::UnexpandedPack; |
1880 | } |
1881 | |
1882 | |
1883 | |
1884 | bool isCanonicalUnqualified() const { |
1885 | return CanonicalType == QualType(this, 0); |
1886 | } |
1887 | |
1888 | |
1889 | |
1890 | |
1891 | QualType getLocallyUnqualifiedSingleStepDesugaredType() const; |
1892 | |
1893 | |
1894 | |
1895 | |
1896 | |
1897 | |
1898 | |
1899 | bool isSizelessType() const; |
1900 | bool isSizelessBuiltinType() const; |
1901 | |
1902 | |
1903 | |
1904 | |
1905 | bool isVLSTBuiltinType() const; |
1906 | |
1907 | |
1908 | |
1909 | |
1910 | QualType getSveEltType(const ASTContext &Ctx) const; |
1911 | |
1912 | |
1913 | |
1914 | |
1915 | |
1916 | |
1917 | |
1918 | |
1919 | |
1920 | |
1921 | |
1922 | |
1923 | bool isIncompleteType(NamedDecl **Def = nullptr) const; |
1924 | |
1925 | |
1926 | |
1927 | bool isIncompleteOrObjectType() const { |
1928 | return !isFunctionType(); |
1929 | } |
1930 | |
1931 | |
1932 | bool isObjectType() const { |
1933 | |
1934 | |
1935 | |
1936 | return !isReferenceType() && !isFunctionType() && !isVoidType(); |
1937 | } |
1938 | |
1939 | |
1940 | |
1941 | bool isLiteralType(const ASTContext &Ctx) const; |
1942 | |
1943 | |
1944 | bool isStructuralType() const; |
1945 | |
1946 | |
1947 | |
1948 | bool isStandardLayoutType() const; |
1949 | |
1950 | |
1951 | |
1952 | |
1953 | |
1954 | bool isBuiltinType() const; |
1955 | |
1956 | |
1957 | bool isSpecificBuiltinType(unsigned K) const; |
1958 | |
1959 | |
1960 | |
1961 | |
1962 | bool isPlaceholderType() const; |
1963 | const BuiltinType *getAsPlaceholderType() const; |
1964 | |
1965 | |
1966 | bool isSpecificPlaceholderType(unsigned K) const; |
1967 | |
1968 | |
1969 | |
1970 | bool isNonOverloadPlaceholderType() const; |
1971 | |
1972 | |
1973 | |
1974 | bool isIntegerType() const; |
1975 | bool isEnumeralType() const; |
1976 | |
1977 | |
1978 | bool isScopedEnumeralType() const; |
1979 | bool isBooleanType() const; |
1980 | bool isCharType() const; |
1981 | bool isWideCharType() const; |
1982 | bool isChar8Type() const; |
1983 | bool isChar16Type() const; |
1984 | bool isChar32Type() const; |
1985 | bool isAnyCharacterType() const; |
1986 | bool isIntegralType(const ASTContext &Ctx) const; |
1987 | |
1988 | |
1989 | bool isIntegralOrEnumerationType() const; |
1990 | |
1991 | |
1992 | bool isIntegralOrUnscopedEnumerationType() const; |
1993 | bool isUnscopedEnumerationType() const; |
1994 | |
1995 | |
1996 | bool isRealFloatingType() const; |
1997 | |
1998 | |
1999 | bool isComplexType() const; |
2000 | bool isAnyComplexType() const; |
2001 | bool isFloatingType() const; |
2002 | bool isHalfType() const; |
2003 | bool isFloat16Type() const; |
2004 | bool isBFloat16Type() const; |
2005 | bool isFloat128Type() const; |
2006 | bool isRealType() const; |
2007 | bool isArithmeticType() const; |
2008 | bool isVoidType() const; |
2009 | bool isScalarType() const; |
2010 | bool isAggregateType() const; |
2011 | bool isFundamentalType() const; |
2012 | bool isCompoundType() const; |
2013 | |
2014 | |
2015 | |
2016 | bool isFunctionType() const; |
2017 | bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } |
2018 | bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } |
2019 | bool isPointerType() const; |
2020 | bool isAnyPointerType() const; |
2021 | bool isBlockPointerType() const; |
2022 | bool isVoidPointerType() const; |
2023 | bool isReferenceType() const; |
2024 | bool isLValueReferenceType() const; |
2025 | bool isRValueReferenceType() const; |
2026 | bool isObjectPointerType() const; |
2027 | bool isFunctionPointerType() const; |
2028 | bool isFunctionReferenceType() const; |
2029 | bool isMemberPointerType() const; |
2030 | bool isMemberFunctionPointerType() const; |
2031 | bool isMemberDataPointerType() const; |
2032 | bool isArrayType() const; |
2033 | bool isConstantArrayType() const; |
2034 | bool isIncompleteArrayType() const; |
2035 | bool isVariableArrayType() const; |
2036 | bool isDependentSizedArrayType() const; |
2037 | bool isRecordType() const; |
2038 | bool isClassType() const; |
2039 | bool isStructureType() const; |
2040 | bool isObjCBoxableRecordType() const; |
2041 | bool isInterfaceType() const; |
2042 | bool isStructureOrClassType() const; |
2043 | bool isUnionType() const; |
2044 | bool isComplexIntegerType() const; |
2045 | bool isVectorType() const; |
2046 | bool isExtVectorType() const; |
2047 | bool isMatrixType() const; |
2048 | bool isConstantMatrixType() const; |
2049 | bool isDependentAddressSpaceType() const; |
2050 | bool isObjCObjectPointerType() const; |
2051 | bool isObjCRetainableType() const; |
2052 | bool isObjCLifetimeType() const; |
2053 | bool isObjCIndirectLifetimeType() const; |
2054 | bool isObjCNSObjectType() const; |
2055 | bool isObjCIndependentClassType() const; |
2056 | |
2057 | |
2058 | bool isObjCObjectType() const; |
2059 | bool isObjCQualifiedInterfaceType() const; |
2060 | bool isObjCQualifiedIdType() const; |
2061 | bool isObjCQualifiedClassType() const; |
2062 | bool isObjCObjectOrInterfaceType() const; |
2063 | bool isObjCIdType() const; |
2064 | bool isDecltypeType() const; |
2065 | |
2066 | |
2067 | |
2068 | |
2069 | |
2070 | |
2071 | bool isObjCInertUnsafeUnretainedType() const { |
2072 | return hasAttr(attr::ObjCInertUnsafeUnretained); |
2073 | } |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | bool isObjCIdOrObjectKindOfType(const ASTContext &ctx, |
2083 | const ObjCObjectType *&bound) const; |
2084 | |
2085 | bool isObjCClassType() const; |
2086 | |
2087 | |
2088 | |
2089 | |
2090 | |
2091 | |
2092 | |
2093 | bool isObjCClassOrClassKindOfType() const; |
2094 | |
2095 | bool isBlockCompatibleObjCPointerType(ASTContext &ctx) const; |
2096 | bool isObjCSelType() const; |
2097 | bool isObjCBuiltinType() const; |
2098 | bool isObjCARCBridgableType() const; |
2099 | bool isCARCBridgableType() const; |
2100 | bool isTemplateTypeParmType() const; |
2101 | bool isNullPtrType() const; |
2102 | bool isNothrowT() const; |
2103 | bool isAlignValT() const; |
2104 | bool isStdByteType() const; |
2105 | bool isAtomicType() const; |
2106 | bool isUndeducedAutoType() const; |
2107 | |
2108 | bool isTypedefNameType() const; |
2109 | |
2110 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
2111 | bool is##Id##Type() const; |
2112 | #include "clang/Basic/OpenCLImageTypes.def" |
2113 | |
2114 | bool isImageType() const; |
2115 | |
2116 | bool isSamplerT() const; |
2117 | bool isEventT() const; |
2118 | bool isClkEventT() const; |
2119 | bool isQueueT() const; |
2120 | bool isReserveIDT() const; |
2121 | |
2122 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
2123 | bool is##Id##Type() const; |
2124 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2125 | |
2126 | bool isOCLIntelSubgroupAVCType() const; |
2127 | bool isOCLExtOpaqueType() const; |
2128 | |
2129 | bool isPipeType() const; |
2130 | bool isExtIntType() const; |
2131 | bool isOpenCLSpecificType() const; |
2132 | |
2133 | |
2134 | |
2135 | |
2136 | bool isObjCARCImplicitlyUnretainedType() const; |
2137 | |
2138 | |
2139 | bool isCUDADeviceBuiltinSurfaceType() const; |
2140 | |
2141 | bool isCUDADeviceBuiltinTextureType() const; |
2142 | |
2143 | |
2144 | Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const; |
2145 | |
2146 | enum ScalarTypeKind { |
2147 | STK_CPointer, |
2148 | STK_BlockPointer, |
2149 | STK_ObjCObjectPointer, |
2150 | STK_MemberPointer, |
2151 | STK_Bool, |
2152 | STK_Integral, |
2153 | STK_Floating, |
2154 | STK_IntegralComplex, |
2155 | STK_FloatingComplex, |
2156 | STK_FixedPoint |
2157 | }; |
2158 | |
2159 | |
2160 | ScalarTypeKind getScalarTypeKind() const; |
2161 | |
2162 | TypeDependence getDependence() const { |
2163 | return static_cast<TypeDependence>(TypeBits.Dependence); |
2164 | } |
2165 | |
2166 | |
2167 | bool containsErrors() const { |
2168 | return getDependence() & TypeDependence::Error; |
2169 | } |
2170 | |
2171 | |
2172 | |
2173 | bool isDependentType() const { |
2174 | return getDependence() & TypeDependence::Dependent; |
2175 | } |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | bool isInstantiationDependentType() const { |
2182 | return getDependence() & TypeDependence::Instantiation; |
2183 | } |
2184 | |
2185 | |
2186 | |
2187 | |
2188 | bool isUndeducedType() const; |
2189 | |
2190 | |
2191 | bool isVariablyModifiedType() const { |
2192 | return getDependence() & TypeDependence::VariablyModified; |
2193 | } |
2194 | |
2195 | |
2196 | |
2197 | bool hasSizedVLAType() const; |
2198 | |
2199 | |
2200 | bool hasUnnamedOrLocalType() const; |
2201 | |
2202 | bool isOverloadableType() const; |
2203 | |
2204 | |
2205 | bool isElaboratedTypeSpecifier() const; |
2206 | |
2207 | bool canDecayToPointerType() const; |
2208 | |
2209 | |
2210 | |
2211 | |
2212 | bool hasPointerRepresentation() const; |
2213 | |
2214 | |
2215 | |
2216 | bool hasObjCPointerRepresentation() const; |
2217 | |
2218 | |
2219 | |
2220 | bool hasIntegerRepresentation() const; |
2221 | |
2222 | |
2223 | |
2224 | bool hasSignedIntegerRepresentation() const; |
2225 | |
2226 | |
2227 | |
2228 | bool hasUnsignedIntegerRepresentation() const; |
2229 | |
2230 | |
2231 | |
2232 | bool hasFloatingRepresentation() const; |
2233 | |
2234 | |
2235 | |
2236 | |
2237 | const RecordType *getAsStructureType() const; |
2238 | |
2239 | const RecordType *getAsUnionType() const; |
2240 | const ComplexType *getAsComplexIntegerType() const; |
2241 | const ObjCObjectType *getAsObjCInterfaceType() const; |
2242 | |
2243 | |
2244 | |
2245 | const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; |
2246 | const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; |
2247 | const ObjCObjectPointerType *getAsObjCQualifiedClassType() const; |
2248 | const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | CXXRecordDecl *getAsCXXRecordDecl() const; |
2254 | |
2255 | |
2256 | RecordDecl *getAsRecordDecl() const; |
2257 | |
2258 | |
2259 | |
2260 | |
2261 | TagDecl *getAsTagDecl() const; |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | |
2267 | |
2268 | const CXXRecordDecl *getPointeeCXXRecordDecl() const; |
2269 | |
2270 | |
2271 | |
2272 | |
2273 | DeducedType *getContainedDeducedType() const; |
2274 | |
2275 | |
2276 | |
2277 | |
2278 | AutoType *getContainedAutoType() const { |
2279 | return dyn_cast_or_null<AutoType>(getContainedDeducedType()); |
2280 | } |
2281 | |
2282 | |
2283 | |
2284 | |
2285 | bool hasAutoForTrailingReturnType() const; |
2286 | |
2287 | |
2288 | |
2289 | |
2290 | |
2291 | |
2292 | |
2293 | template <typename T> const T *getAs() const; |
2294 | |
2295 | |
2296 | |
2297 | |
2298 | |
2299 | |
2300 | template <typename T> const T *getAsAdjusted() const; |
2301 | |
2302 | |
2303 | |
2304 | const ArrayType *getAsArrayTypeUnsafe() const; |
2305 | |
2306 | |
2307 | |
2308 | |
2309 | |
2310 | |
2311 | |
2312 | template <typename T> const T *castAs() const; |
2313 | |
2314 | |
2315 | |
2316 | const ArrayType *castAsArrayTypeUnsafe() const; |
2317 | |
2318 | |
2319 | |
2320 | bool hasAttr(attr::Kind AK) const; |
2321 | |
2322 | |
2323 | |
2324 | |
2325 | const Type *getBaseElementTypeUnsafe() const; |
2326 | |
2327 | |
2328 | |
2329 | |
2330 | const Type *getArrayElementTypeNoTypeQual() const; |
2331 | |
2332 | |
2333 | |
2334 | |
2335 | const Type *getPointeeOrArrayElementType() const; |
2336 | |
2337 | |
2338 | |
2339 | QualType getPointeeType() const; |
2340 | |
2341 | |
2342 | |
2343 | const Type *getUnqualifiedDesugaredType() const; |
2344 | |
2345 | |
2346 | bool isPromotableIntegerType() const; |
2347 | |
2348 | |
2349 | |
2350 | |
2351 | bool isSignedIntegerType() const; |
2352 | |
2353 | |
2354 | |
2355 | |
2356 | bool isUnsignedIntegerType() const; |
2357 | |
2358 | |
2359 | |
2360 | bool isSignedIntegerOrEnumerationType() const; |
2361 | |
2362 | |
2363 | |
2364 | bool isUnsignedIntegerOrEnumerationType() const; |
2365 | |
2366 | |
2367 | |
2368 | bool isFixedPointType() const; |
2369 | |
2370 | |
2371 | bool isFixedPointOrIntegerType() const; |
2372 | |
2373 | |
2374 | |
2375 | bool isSaturatedFixedPointType() const; |
2376 | |
2377 | |
2378 | |
2379 | bool isUnsaturatedFixedPointType() const; |
2380 | |
2381 | |
2382 | |
2383 | bool isSignedFixedPointType() const; |
2384 | |
2385 | |
2386 | |
2387 | bool isUnsignedFixedPointType() const; |
2388 | |
2389 | |
2390 | |
2391 | |
2392 | bool isConstantSizeType() const; |
2393 | |
2394 | |
2395 | |
2396 | bool isSpecifierType() const; |
2397 | |
2398 | |
2399 | Linkage getLinkage() const; |
2400 | |
2401 | |
2402 | Visibility getVisibility() const { |
2403 | return getLinkageAndVisibility().getVisibility(); |
2404 | } |
2405 | |
2406 | |
2407 | bool isVisibilityExplicit() const { |
2408 | return getLinkageAndVisibility().isVisibilityExplicit(); |
2409 | } |
2410 | |
2411 | |
2412 | LinkageInfo getLinkageAndVisibility() const; |
2413 | |
2414 | |
2415 | |
2416 | bool isLinkageValid() const; |
2417 | |
2418 | |
2419 | |
2420 | |
2421 | |
2422 | |
2423 | Optional<NullabilityKind> getNullability(const ASTContext &context) const; |
2424 | |
2425 | |
2426 | |
2427 | |
2428 | |
2429 | |
2430 | bool canHaveNullability(bool ResultIfUnknown = true) const; |
2431 | |
2432 | |
2433 | |
2434 | |
2435 | |
2436 | |
2437 | |
2438 | |
2439 | |
2440 | |
2441 | |
2442 | |
2443 | |
2444 | |
2445 | |
2446 | |
2447 | Optional<ArrayRef<QualType>> |
2448 | getObjCSubstitutions(const DeclContext *dc) const; |
2449 | |
2450 | |
2451 | |
2452 | bool acceptsObjCTypeParams() const; |
2453 | |
2454 | const char *getTypeClassName() const; |
2455 | |
2456 | QualType getCanonicalTypeInternal() const { |
2457 | return CanonicalType; |
2458 | } |
2459 | |
2460 | CanQualType getCanonicalTypeUnqualified() const; |
2461 | void dump() const; |
2462 | void dump(llvm::raw_ostream &OS, const ASTContext &Context) const; |
2463 | }; |
2464 | |
2465 | |
2466 | |
2467 | template <> const TypedefType *Type::getAs() const; |
2468 | |
2469 | |
2470 | |
2471 | |
2472 | template <> const TemplateSpecializationType *Type::getAs() const; |
2473 | |
2474 | |
2475 | |
2476 | template <> const AttributedType *Type::getAs() const; |
2477 | |
2478 | |
2479 | |
2480 | #define TYPE(Class, Base) |
2481 | #define LEAF_TYPE(Class) \ |
2482 | template <> inline const Class##Type *Type::getAs() const { \ |
2483 | return dyn_cast<Class##Type>(CanonicalType); \ |
2484 | } \ |
2485 | template <> inline const Class##Type *Type::castAs() const { \ |
2486 | return cast<Class##Type>(CanonicalType); \ |
2487 | } |
2488 | #include "clang/AST/TypeNodes.inc" |
2489 | |
2490 | |
2491 | |
2492 | class BuiltinType : public Type { |
2493 | public: |
2494 | enum Kind { |
2495 | |
2496 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) Id, |
2497 | #include "clang/Basic/OpenCLImageTypes.def" |
2498 | |
2499 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) Id, |
2500 | #include "clang/Basic/OpenCLExtensionTypes.def" |
2501 | |
2502 | #define SVE_TYPE(Name, Id, SingletonId) Id, |
2503 | #include "clang/Basic/AArch64SVEACLETypes.def" |
2504 | |
2505 | #define PPC_VECTOR_TYPE(Name, Id, Size) Id, |
2506 | #include "clang/Basic/PPCTypes.def" |
2507 | |
2508 | #define RVV_TYPE(Name, Id, SingletonId) Id, |
2509 | #include "clang/Basic/RISCVVTypes.def" |
2510 | |
2511 | #define BUILTIN_TYPE(Id, SingletonId) Id, |
2512 | #define LAST_BUILTIN_TYPE(Id) LastKind = Id |
2513 | #include "clang/AST/BuiltinTypes.def" |
2514 | }; |
2515 | |
2516 | private: |
2517 | friend class ASTContext; |
2518 | |
2519 | BuiltinType(Kind K) |
2520 | : Type(Builtin, QualType(), |
2521 | K == Dependent ? TypeDependence::DependentInstantiation |
2522 | : TypeDependence::None) { |
2523 | BuiltinTypeBits.Kind = K; |
2524 | } |
2525 | |
2526 | public: |
2527 | Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } |
2528 | StringRef getName(const PrintingPolicy &Policy) const; |
2529 | |
2530 | const char *getNameAsCString(const PrintingPolicy &Policy) const { |
2531 | |
2532 | StringRef str = getName(Policy); |
2533 | assert(!str.empty() && str.data()[str.size()] == '\0'); |
2534 | return str.data(); |
2535 | } |
2536 | |
2537 | bool isSugared() const { return false; } |
2538 | QualType desugar() const { return QualType(this, 0); } |
2539 | |
2540 | bool isInteger() const { |
2541 | return getKind() >= Bool && getKind() <= Int128; |
2542 | } |
2543 | |
2544 | bool isSignedInteger() const { |
2545 | return getKind() >= Char_S && getKind() <= Int128; |
2546 | } |
2547 | |
2548 | bool isUnsignedInteger() const { |
2549 | return getKind() >= Bool && getKind() <= UInt128; |
2550 | } |
2551 | |
2552 | bool isFloatingPoint() const { |
2553 | return getKind() >= Half && getKind() <= Float128; |
2554 | } |
2555 | |
2556 | |
2557 | static bool isPlaceholderTypeKind(Kind K) { |
2558 | return K >= Overload; |
2559 | } |
2560 | |
2561 | |
2562 | |
2563 | |
2564 | bool isPlaceholderType() const { |
2565 | return isPlaceholderTypeKind(getKind()); |
2566 | } |
2567 | |
2568 | |
2569 | |
2570 | |
2571 | |
2572 | |
2573 | |
2574 | |
2575 | |
2576 | |
2577 | bool isNonOverloadPlaceholderType() const { |
2578 | return getKind() > Overload; |
2579 | } |
2580 | |
2581 | static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } |
2582 | }; |
2583 | |
2584 | |
2585 | |
2586 | class ComplexType : public Type, public llvm::FoldingSetNode { |
2587 | friend class ASTContext; |
2588 | |
2589 | QualType ElementType; |
2590 | |
2591 | ComplexType(QualType Element, QualType CanonicalPtr) |
2592 | : Type(Complex, CanonicalPtr, Element->getDependence()), |
2593 | ElementType(Element) {} |
2594 | |
2595 | public: |
2596 | QualType getElementType() const { return ElementType; } |
2597 | |
2598 | bool isSugared() const { return false; } |
2599 | QualType desugar() const { return QualType(this, 0); } |
2600 | |
2601 | void Profile(llvm::FoldingSetNodeID &ID) { |
2602 | Profile(ID, getElementType()); |
2603 | } |
2604 | |
2605 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { |
2606 | ID.AddPointer(Element.getAsOpaquePtr()); |
2607 | } |
2608 | |
2609 | static bool classof(const Type *T) { return T->getTypeClass() == Complex; } |
2610 | }; |
2611 | |
2612 | |
2613 | class ParenType : public Type, public llvm::FoldingSetNode { |
2614 | friend class ASTContext; |
2615 | |
2616 | QualType Inner; |
2617 | |
2618 | ParenType(QualType InnerType, QualType CanonType) |
2619 | : Type(Paren, CanonType, InnerType->getDependence()), Inner(InnerType) {} |
2620 | |
2621 | public: |
2622 | QualType getInnerType() const { return Inner; } |
2623 | |
2624 | bool isSugared() const { return true; } |
2625 | QualType desugar() const { return getInnerType(); } |
2626 | |
2627 | void Profile(llvm::FoldingSetNodeID &ID) { |
2628 | Profile(ID, getInnerType()); |
2629 | } |
2630 | |
2631 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) { |
2632 | Inner.Profile(ID); |
2633 | } |
2634 | |
2635 | static bool classof(const Type *T) { return T->getTypeClass() == Paren; } |
2636 | }; |
2637 | |
2638 | |
2639 | class PointerType : public Type, public llvm::FoldingSetNode { |
2640 | friend class ASTContext; |
2641 | |
2642 | QualType PointeeType; |
2643 | |
2644 | PointerType(QualType Pointee, QualType CanonicalPtr) |
2645 | : Type(Pointer, CanonicalPtr, Pointee->getDependence()), |
2646 | PointeeType(Pointee) {} |
2647 | |
2648 | public: |
2649 | QualType getPointeeType() const { return PointeeType; } |
2650 | |
2651 | bool isSugared() const { return false; } |
2652 | QualType desugar() const { return QualType(this, 0); } |
2653 | |
2654 | void Profile(llvm::FoldingSetNodeID &ID) { |
2655 | Profile(ID, getPointeeType()); |
2656 | } |
2657 | |
2658 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2659 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2660 | } |
2661 | |
2662 | static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } |
2663 | }; |
2664 | |
2665 | |
2666 | |
2667 | |
2668 | class AdjustedType : public Type, public llvm::FoldingSetNode { |
2669 | QualType OriginalTy; |
2670 | QualType AdjustedTy; |
2671 | |
2672 | protected: |
2673 | friend class ASTContext; |
2674 | |
2675 | AdjustedType(TypeClass TC, QualType OriginalTy, QualType AdjustedTy, |
2676 | QualType CanonicalPtr) |
2677 | : Type(TC, CanonicalPtr, OriginalTy->getDependence()), |
2678 | OriginalTy(OriginalTy), AdjustedTy(AdjustedTy) {} |
2679 | |
2680 | public: |
2681 | QualType getOriginalType() const { return OriginalTy; } |
2682 | QualType getAdjustedType() const { return AdjustedTy; } |
2683 | |
2684 | bool isSugared() const { return true; } |
2685 | QualType desugar() const { return AdjustedTy; } |
2686 | |
2687 | void Profile(llvm::FoldingSetNodeID &ID) { |
2688 | Profile(ID, OriginalTy, AdjustedTy); |
2689 | } |
2690 | |
2691 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Orig, QualType New) { |
2692 | ID.AddPointer(Orig.getAsOpaquePtr()); |
2693 | ID.AddPointer(New.getAsOpaquePtr()); |
2694 | } |
2695 | |
2696 | static bool classof(const Type *T) { |
2697 | return T->getTypeClass() == Adjusted || T->getTypeClass() == Decayed; |
2698 | } |
2699 | }; |
2700 | |
2701 | |
2702 | class DecayedType : public AdjustedType { |
2703 | friend class ASTContext; |
2704 | |
2705 | inline |
2706 | DecayedType(QualType OriginalType, QualType Decayed, QualType Canonical); |
2707 | |
2708 | public: |
2709 | QualType getDecayedType() const { return getAdjustedType(); } |
2710 | |
2711 | inline QualType getPointeeType() const; |
2712 | |
2713 | static bool classof(const Type *T) { return T->getTypeClass() == Decayed; } |
2714 | }; |
2715 | |
2716 | |
2717 | |
2718 | |
2719 | class BlockPointerType : public Type, public llvm::FoldingSetNode { |
2720 | friend class ASTContext; |
2721 | |
2722 | |
2723 | QualType PointeeType; |
2724 | |
2725 | BlockPointerType(QualType Pointee, QualType CanonicalCls) |
2726 | : Type(BlockPointer, CanonicalCls, Pointee->getDependence()), |
2727 | PointeeType(Pointee) {} |
2728 | |
2729 | public: |
2730 | |
2731 | QualType getPointeeType() const { return PointeeType; } |
2732 | |
2733 | bool isSugared() const { return false; } |
2734 | QualType desugar() const { return QualType(this, 0); } |
2735 | |
2736 | void Profile(llvm::FoldingSetNodeID &ID) { |
2737 | Profile(ID, getPointeeType()); |
2738 | } |
2739 | |
2740 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { |
2741 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2742 | } |
2743 | |
2744 | static bool classof(const Type *T) { |
2745 | return T->getTypeClass() == BlockPointer; |
2746 | } |
2747 | }; |
2748 | |
2749 | |
2750 | class ReferenceType : public Type, public llvm::FoldingSetNode { |
2751 | QualType PointeeType; |
2752 | |
2753 | protected: |
2754 | ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, |
2755 | bool SpelledAsLValue) |
2756 | : Type(tc, CanonicalRef, Referencee->getDependence()), |
2757 | PointeeType(Referencee) { |
2758 | ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; |
2759 | ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); |
2760 | } |
2761 | |
2762 | public: |
2763 | bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } |
2764 | bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } |
2765 | |
2766 | QualType getPointeeTypeAsWritten() const { return PointeeType; } |
2767 | |
2768 | QualType getPointeeType() const { |
2769 | |
2770 | const ReferenceType *T = this; |
2771 | while (T->isInnerRef()) |
2772 | T = T->PointeeType->castAs<ReferenceType>(); |
2773 | return T->PointeeType; |
2774 | } |
2775 | |
2776 | void Profile(llvm::FoldingSetNodeID &ID) { |
2777 | Profile(ID, PointeeType, isSpelledAsLValue()); |
2778 | } |
2779 | |
2780 | static void Profile(llvm::FoldingSetNodeID &ID, |
2781 | QualType Referencee, |
2782 | bool SpelledAsLValue) { |
2783 | ID.AddPointer(Referencee.getAsOpaquePtr()); |
2784 | ID.AddBoolean(SpelledAsLValue); |
2785 | } |
2786 | |
2787 | static bool classof(const Type *T) { |
2788 | return T->getTypeClass() == LValueReference || |
2789 | T->getTypeClass() == RValueReference; |
2790 | } |
2791 | }; |
2792 | |
2793 | |
2794 | class LValueReferenceType : public ReferenceType { |
2795 | friend class ASTContext; |
2796 | |
2797 | LValueReferenceType(QualType Referencee, QualType CanonicalRef, |
2798 | bool SpelledAsLValue) |
2799 | : ReferenceType(LValueReference, Referencee, CanonicalRef, |
2800 | SpelledAsLValue) {} |
2801 | |
2802 | public: |
2803 | bool isSugared() const { return false; } |
2804 | QualType desugar() const { return QualType(this, 0); } |
2805 | |
2806 | static bool classof(const Type *T) { |
2807 | return T->getTypeClass() == LValueReference; |
2808 | } |
2809 | }; |
2810 | |
2811 | |
2812 | class RValueReferenceType : public ReferenceType { |
2813 | friend class ASTContext; |
2814 | |
2815 | RValueReferenceType(QualType Referencee, QualType CanonicalRef) |
2816 | : ReferenceType(RValueReference, Referencee, CanonicalRef, false) {} |
2817 | |
2818 | public: |
2819 | bool isSugared() const { return false; } |
2820 | QualType desugar() const { return QualType(this, 0); } |
2821 | |
2822 | static bool classof(const Type *T) { |
2823 | return T->getTypeClass() == RValueReference; |
2824 | } |
2825 | }; |
2826 | |
2827 | |
2828 | |
2829 | |
2830 | class MemberPointerType : public Type, public llvm::FoldingSetNode { |
2831 | friend class ASTContext; |
2832 | |
2833 | QualType PointeeType; |
2834 | |
2835 | |
2836 | |
2837 | const Type *Class; |
2838 | |
2839 | MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) |
2840 | : Type(MemberPointer, CanonicalPtr, |
2841 | (Cls->getDependence() & ~TypeDependence::VariablyModified) | |
2842 | Pointee->getDependence()), |
2843 | PointeeType(Pointee), Class(Cls) {} |
2844 | |
2845 | public: |
2846 | QualType getPointeeType() const { return PointeeType; } |
2847 | |
2848 | |
2849 | |
2850 | bool isMemberFunctionPointer() const { |
2851 | return PointeeType->isFunctionProtoType(); |
2852 | } |
2853 | |
2854 | |
2855 | |
2856 | bool isMemberDataPointer() const { |
2857 | return !PointeeType->isFunctionProtoType(); |
2858 | } |
2859 | |
2860 | const Type *getClass() const { return Class; } |
2861 | CXXRecordDecl *getMostRecentCXXRecordDecl() const; |
2862 | |
2863 | bool isSugared() const { return false; } |
2864 | QualType desugar() const { return QualType(this, 0); } |
2865 | |
2866 | void Profile(llvm::FoldingSetNodeID &ID) { |
2867 | Profile(ID, getPointeeType(), getClass()); |
2868 | } |
2869 | |
2870 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, |
2871 | const Type *Class) { |
2872 | ID.AddPointer(Pointee.getAsOpaquePtr()); |
2873 | ID.AddPointer(Class); |
2874 | } |
2875 | |
2876 | static bool classof(const Type *T) { |
2877 | return T->getTypeClass() == MemberPointer; |
2878 | } |
2879 | }; |
2880 | |
2881 | |
2882 | class ArrayType : public Type, public llvm::FoldingSetNode { |
2883 | public: |
2884 | |
2885 | |
2886 | |
2887 | |
2888 | enum ArraySizeModifier { |
2889 | Normal, Static, Star |
2890 | }; |
2891 | |
2892 | private: |
2893 | |
2894 | QualType ElementType; |
2895 | |
2896 | protected: |
2897 | friend class ASTContext; |
2898 | |
2899 | ArrayType(TypeClass tc, QualType et, QualType can, ArraySizeModifier sm, |
2900 | unsigned tq, const Expr *sz = nullptr); |
2901 | |
2902 | public: |
2903 | QualType getElementType() const { return ElementType; } |
2904 | |
2905 | ArraySizeModifier getSizeModifier() const { |
2906 | return ArraySizeModifier(ArrayTypeBits.SizeModifier); |
2907 | } |
2908 | |
2909 | Qualifiers getIndexTypeQualifiers() const { |
2910 | return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); |
2911 | } |
2912 | |
2913 | unsigned getIndexTypeCVRQualifiers() const { |
2914 | return ArrayTypeBits.IndexTypeQuals; |
2915 | } |
2916 | |
2917 | static bool classof(const Type *T) { |
2918 | return T->getTypeClass() == ConstantArray || |
2919 | T->getTypeClass() == VariableArray || |
2920 | T->getTypeClass() == IncompleteArray || |
2921 | T->getTypeClass() == DependentSizedArray; |
2922 | } |
2923 | }; |
2924 | |
2925 | |
2926 | |
2927 | |
2928 | class ConstantArrayType final |
2929 | : public ArrayType, |
2930 | private llvm::TrailingObjects<ConstantArrayType, const Expr *> { |
2931 | friend class ASTContext; |
2932 | friend TrailingObjects; |
2933 | |
2934 | llvm::APInt Size; |
2935 | |
2936 | ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, |
2937 | const Expr *sz, ArraySizeModifier sm, unsigned tq) |
2938 | : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) { |
2939 | ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr; |
2940 | if (ConstantArrayTypeBits.HasStoredSizeExpr) { |
2941 | assert(!can.isNull() && "canonical constant array should not have size"); |
2942 | *getTrailingObjects<const Expr*>() = sz; |
2943 | } |
2944 | } |
2945 | |
2946 | unsigned numTrailingObjects(OverloadToken<const Expr*>) const { |
2947 | return ConstantArrayTypeBits.HasStoredSizeExpr; |
2948 | } |
2949 | |
2950 | public: |
2951 | const llvm::APInt &getSize() const { return Size; } |
2952 | const Expr *getSizeExpr() const { |
2953 | return ConstantArrayTypeBits.HasStoredSizeExpr |
2954 | ? *getTrailingObjects<const Expr *>() |
2955 | : nullptr; |
2956 | } |
2957 | bool isSugared() const { return false; } |
2958 | QualType desugar() const { return QualType(this, 0); } |
2959 | |
2960 | |
2961 | |
2962 | static unsigned getNumAddressingBits(const ASTContext &Context, |
2963 | QualType ElementType, |
2964 | const llvm::APInt &NumElements); |
2965 | |
2966 | |
2967 | |
2968 | static unsigned getMaxSizeBits(const ASTContext &Context); |
2969 | |
2970 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
2971 | Profile(ID, Ctx, getElementType(), getSize(), getSizeExpr(), |
2972 | getSizeModifier(), getIndexTypeCVRQualifiers()); |
2973 | } |
2974 | |
2975 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx, |
2976 | QualType ET, const llvm::APInt &ArraySize, |
2977 | const Expr *SizeExpr, ArraySizeModifier SizeMod, |
2978 | unsigned TypeQuals); |
2979 | |
2980 | static bool classof(const Type *T) { |
2981 | return T->getTypeClass() == ConstantArray; |
2982 | } |
2983 | }; |
2984 | |
2985 | |
2986 | |
2987 | |
2988 | class IncompleteArrayType : public ArrayType { |
2989 | friend class ASTContext; |
2990 | |
2991 | IncompleteArrayType(QualType et, QualType can, |
2992 | ArraySizeModifier sm, unsigned tq) |
2993 | : ArrayType(IncompleteArray, et, can, sm, tq) {} |
2994 | |
2995 | public: |
2996 | friend class StmtIteratorBase; |
2997 | |
2998 | bool isSugared() const { return false; } |
2999 | QualType desugar() const { return QualType(this, 0); } |
3000 | |
3001 | static bool classof(const Type *T) { |
3002 | return T->getTypeClass() == IncompleteArray; |
3003 | } |
3004 | |
3005 | void Profile(llvm::FoldingSetNodeID &ID) { |
3006 | Profile(ID, getElementType(), getSizeModifier(), |
3007 | getIndexTypeCVRQualifiers()); |
3008 | } |
3009 | |
3010 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, |
3011 | ArraySizeModifier SizeMod, unsigned TypeQuals) { |
3012 | ID.AddPointer(ET.getAsOpaquePtr()); |
3013 | ID.AddInteger(SizeMod); |
3014 | ID.AddInteger(TypeQuals); |
3015 | } |
3016 | }; |
3017 | |
3018 | |
3019 | |
3020 | |
3021 | |
3022 | |
3023 | |
3024 | |
3025 | |
3026 | |
3027 | |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | class VariableArrayType : public ArrayType { |
3033 | friend class ASTContext; |
3034 | |
3035 | |
3036 | |
3037 | Stmt *SizeExpr; |
3038 | |
3039 | |
3040 | SourceRange Brackets; |
3041 | |
3042 | VariableArrayType(QualType et, QualType can, Expr *e, |
3043 | ArraySizeModifier sm, unsigned tq, |
3044 | SourceRange brackets) |
3045 | : ArrayType(VariableArray, et, can, sm, tq, e), |
3046 | SizeExpr((Stmt*) e), Brackets(brackets) {} |
3047 | |
3048 | public: |
3049 | friend class StmtIteratorBase; |
3050 | |
3051 | Expr *getSizeExpr() const { |
3052 | |
3053 | |
3054 | return (Expr*) SizeExpr; |
3055 | } |
3056 | |
3057 | SourceRange getBracketsRange() const { return Brackets; } |
3058 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3059 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3060 | |
3061 | bool isSugared() const { return false; } |
3062 | QualType desugar() const { return QualType(this, 0); } |
3063 | |
3064 | static bool classof(const Type *T) { |
3065 | return T->getTypeClass() == VariableArray; |
3066 | } |
3067 | |
3068 | void Profile(llvm::FoldingSetNodeID &ID) { |
3069 | llvm_unreachable("Cannot unique VariableArrayTypes."); |
3070 | } |
3071 | }; |
3072 | |
3073 | |
3074 | |
3075 | |
3076 | |
3077 | |
3078 | |
3079 | |
3080 | |
3081 | |
3082 | |
3083 | |
3084 | |
3085 | |
3086 | class DependentSizedArrayType : public ArrayType { |
3087 | friend class ASTContext; |
3088 | |
3089 | const ASTContext &Context; |
3090 | |
3091 | |
3092 | |
3093 | |
3094 | |
3095 | |
3096 | Stmt *SizeExpr; |
3097 | |
3098 | |
3099 | SourceRange Brackets; |
3100 | |
3101 | DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can, |
3102 | Expr *e, ArraySizeModifier sm, unsigned tq, |
3103 | SourceRange brackets); |
3104 | |
3105 | public: |
3106 | friend class StmtIteratorBase; |
3107 | |
3108 | Expr *getSizeExpr() const { |
3109 | |
3110 | |
3111 | return (Expr*) SizeExpr; |
3112 | } |
3113 | |
3114 | SourceRange getBracketsRange() const { return Brackets; } |
3115 | SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } |
3116 | SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } |
3117 | |
3118 | bool isSugared() const { return false; } |
3119 | QualType desugar() const { return QualType(this, 0); } |
3120 | |
3121 | static bool classof(const Type *T) { |
3122 | return T->getTypeClass() == DependentSizedArray; |
3123 | } |
3124 | |
3125 | void Profile(llvm::FoldingSetNodeID &ID) { |
3126 | Profile(ID, Context, getElementType(), |
3127 | getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); |
3128 | } |
3129 | |
3130 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3131 | QualType ET, ArraySizeModifier SizeMod, |
3132 | unsigned TypeQuals, Expr *E); |
3133 | }; |
3134 | |
3135 | |
3136 | |
3137 | |
3138 | |
3139 | |
3140 | |
3141 | |
3142 | |
3143 | |
3144 | |
3145 | |
3146 | class DependentAddressSpaceType : public Type, public llvm::FoldingSetNode { |
3147 | friend class ASTContext; |
3148 | |
3149 | const ASTContext &Context; |
3150 | Expr *AddrSpaceExpr; |
3151 | QualType PointeeType; |
3152 | SourceLocation loc; |
3153 | |
3154 | DependentAddressSpaceType(const ASTContext &Context, QualType PointeeType, |
3155 | QualType can, Expr *AddrSpaceExpr, |
3156 | SourceLocation loc); |
3157 | |
3158 | public: |
3159 | Expr *getAddrSpaceExpr() const { return AddrSpaceExpr; } |
3160 | QualType getPointeeType() const { return PointeeType; } |
3161 | SourceLocation getAttributeLoc() const { return loc; } |
3162 | |
3163 | bool isSugared() const { return false; } |
3164 | QualType desugar() const { return QualType(this, 0); } |
3165 | |
3166 | static bool classof(const Type *T) { |
3167 | return T->getTypeClass() == DependentAddressSpace; |
3168 | } |
3169 | |
3170 | void Profile(llvm::FoldingSetNodeID &ID) { |
3171 | Profile(ID, Context, getPointeeType(), getAddrSpaceExpr()); |
3172 | } |
3173 | |
3174 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3175 | QualType PointeeType, Expr *AddrSpaceExpr); |
3176 | }; |
3177 | |
3178 | |
3179 | |
3180 | |
3181 | |
3182 | |
3183 | |
3184 | |
3185 | |
3186 | |
3187 | |
3188 | class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { |
3189 | friend class ASTContext; |
3190 | |
3191 | const ASTContext &Context; |
3192 | Expr *SizeExpr; |
3193 | |
3194 | |
3195 | QualType ElementType; |
3196 | |
3197 | SourceLocation loc; |
3198 | |
3199 | DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType, |
3200 | QualType can, Expr *SizeExpr, SourceLocation loc); |
3201 | |
3202 | public: |
3203 | Expr *getSizeExpr() const { return SizeExpr; } |
3204 | QualType getElementType() const { return ElementType; } |
3205 | SourceLocation getAttributeLoc() const { return loc; } |
3206 | |
3207 | bool isSugared() const { return false; } |
3208 | QualType desugar() const { return QualType(this, 0); } |
3209 | |
3210 | static bool classof(const Type *T) { |
3211 | return T->getTypeClass() == DependentSizedExtVector; |
3212 | } |
3213 | |
3214 | void Profile(llvm::FoldingSetNodeID &ID) { |
3215 | Profile(ID, Context, getElementType(), getSizeExpr()); |
3216 | } |
3217 | |
3218 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3219 | QualType ElementType, Expr *SizeExpr); |
3220 | }; |
3221 | |
3222 | |
3223 | |
3224 | |
3225 | |
3226 | |
3227 | |
3228 | class VectorType : public Type, public llvm::FoldingSetNode { |
3229 | public: |
3230 | enum VectorKind { |
3231 | |
3232 | GenericVector, |
3233 | |
3234 | |
3235 | AltiVecVector, |
3236 | |
3237 | |
3238 | AltiVecPixel, |
3239 | |
3240 | |
3241 | AltiVecBool, |
3242 | |
3243 | |
3244 | NeonVector, |
3245 | |
3246 | |
3247 | NeonPolyVector, |
3248 | |
3249 | |
3250 | SveFixedLengthDataVector, |
3251 | |
3252 | |
3253 | SveFixedLengthPredicateVector |
3254 | }; |
3255 | |
3256 | protected: |
3257 | friend class ASTContext; |
3258 | |
3259 | |
3260 | QualType ElementType; |
3261 | |
3262 | VectorType(QualType vecType, unsigned nElements, QualType canonType, |
3263 | VectorKind vecKind); |
3264 | |
3265 | VectorType(TypeClass tc, QualType vecType, unsigned nElements, |
3266 | QualType canonType, VectorKind vecKind); |
3267 | |
3268 | public: |
3269 | QualType getElementType() const { return ElementType; } |
3270 | unsigned getNumElements() const { return VectorTypeBits.NumElements; } |
3271 | |
3272 | bool isSugared() const { return false; } |
3273 | QualType desugar() const { return QualType(this, 0); } |
3274 | |
3275 | VectorKind getVectorKind() const { |
3276 | return VectorKind(VectorTypeBits.VecKind); |
3277 | } |
3278 | |
3279 | void Profile(llvm::FoldingSetNodeID &ID) { |
3280 | Profile(ID, getElementType(), getNumElements(), |
3281 | getTypeClass(), getVectorKind()); |
3282 | } |
3283 | |
3284 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3285 | unsigned NumElements, TypeClass TypeClass, |
3286 | VectorKind VecKind) { |
3287 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3288 | ID.AddInteger(NumElements); |
3289 | ID.AddInteger(TypeClass); |
3290 | ID.AddInteger(VecKind); |
3291 | } |
3292 | |
3293 | static bool classof(const Type *T) { |
3294 | return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; |
3295 | } |
3296 | }; |
3297 | |
3298 | |
3299 | |
3300 | |
3301 | |
3302 | |
3303 | |
3304 | |
3305 | |
3306 | |
3307 | class DependentVectorType : public Type, public llvm::FoldingSetNode { |
3308 | friend class ASTContext; |
3309 | |
3310 | const ASTContext &Context; |
3311 | QualType ElementType; |
3312 | Expr *SizeExpr; |
3313 | SourceLocation Loc; |
3314 | |
3315 | DependentVectorType(const ASTContext &Context, QualType ElementType, |
3316 | QualType CanonType, Expr *SizeExpr, |
3317 | SourceLocation Loc, VectorType::VectorKind vecKind); |
3318 | |
3319 | public: |
3320 | Expr *getSizeExpr() const { return SizeExpr; } |
3321 | QualType getElementType() const { return ElementType; } |
3322 | SourceLocation getAttributeLoc() const { return Loc; } |
3323 | VectorType::VectorKind getVectorKind() const { |
3324 | return VectorType::VectorKind(VectorTypeBits.VecKind); |
3325 | } |
3326 | |
3327 | bool isSugared() const { return false; } |
3328 | QualType desugar() const { return QualType(this, 0); } |
3329 | |
3330 | static bool classof(const Type *T) { |
3331 | return T->getTypeClass() == DependentVector; |
3332 | } |
3333 | |
3334 | void Profile(llvm::FoldingSetNodeID &ID) { |
3335 | Profile(ID, Context, getElementType(), getSizeExpr(), getVectorKind()); |
3336 | } |
3337 | |
3338 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3339 | QualType ElementType, const Expr *SizeExpr, |
3340 | VectorType::VectorKind VecKind); |
3341 | }; |
3342 | |
3343 | |
3344 | |
3345 | |
3346 | |
3347 | |
3348 | |
3349 | class ExtVectorType : public VectorType { |
3350 | friend class ASTContext; |
3351 | |
3352 | ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) |
3353 | : VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {} |
3354 | |
3355 | public: |
3356 | static int getPointAccessorIdx(char c) { |
3357 | switch (c) { |
3358 | default: return -1; |
3359 | case 'x': case 'r': return 0; |
3360 | case 'y': case 'g': return 1; |
3361 | case 'z': case 'b': return 2; |
3362 | case 'w': case 'a': return 3; |
3363 | } |
3364 | } |
3365 | |
3366 | static int getNumericAccessorIdx(char c) { |
3367 | switch (c) { |
3368 | default: return -1; |
3369 | case '0': return 0; |
3370 | case '1': return 1; |
3371 | case '2': return 2; |
3372 | case '3': return 3; |
3373 | case '4': return 4; |
3374 | case '5': return 5; |
3375 | case '6': return 6; |
3376 | case '7': return 7; |
3377 | case '8': return 8; |
3378 | case '9': return 9; |
3379 | case 'A': |
3380 | case 'a': return 10; |
3381 | case 'B': |
3382 | case 'b': return 11; |
3383 | case 'C': |
3384 | case 'c': return 12; |
3385 | case 'D': |
3386 | case 'd': return 13; |
3387 | case 'E': |
3388 | case 'e': return 14; |
3389 | case 'F': |
3390 | case 'f': return 15; |
3391 | } |
3392 | } |
3393 | |
3394 | static int getAccessorIdx(char c, bool isNumericAccessor) { |
3395 | if (isNumericAccessor) |
3396 | return getNumericAccessorIdx(c); |
3397 | else |
3398 | return getPointAccessorIdx(c); |
3399 | } |
3400 | |
3401 | bool isAccessorWithinNumElements(char c, bool isNumericAccessor) const { |
3402 | if (int idx = getAccessorIdx(c, isNumericAccessor)+1) |
3403 | return unsigned(idx-1) < getNumElements(); |
3404 | return false; |
3405 | } |
3406 | |
3407 | bool isSugared() const { return false; } |
3408 | QualType desugar() const { return QualType(this, 0); } |
3409 | |
3410 | static bool classof(const Type *T) { |
3411 | return T->getTypeClass() == ExtVector; |
3412 | } |
3413 | }; |
3414 | |
3415 | |
3416 | |
3417 | |
3418 | class MatrixType : public Type, public llvm::FoldingSetNode { |
3419 | protected: |
3420 | friend class ASTContext; |
3421 | |
3422 | |
3423 | QualType ElementType; |
3424 | |
3425 | MatrixType(QualType ElementTy, QualType CanonElementTy); |
3426 | |
3427 | MatrixType(TypeClass TypeClass, QualType ElementTy, QualType CanonElementTy, |
3428 | const Expr *RowExpr = nullptr, const Expr *ColumnExpr = nullptr); |
3429 | |
3430 | public: |
3431 | |
3432 | QualType getElementType() const { return ElementType; } |
3433 | |
3434 | |
3435 | |
3436 | |
3437 | |
3438 | |
3439 | static bool isValidElementType(QualType T) { |
3440 | return T->isDependentType() || |
3441 | (T->isRealType() && !T->isBooleanType() && !T->isEnumeralType()); |
3442 | } |
3443 | |
3444 | bool isSugared() const { return false; } |
3445 | QualType desugar() const { return QualType(this, 0); } |
3446 | |
3447 | static bool classof(const Type *T) { |
3448 | return T->getTypeClass() == ConstantMatrix || |
3449 | T->getTypeClass() == DependentSizedMatrix; |
3450 | } |
3451 | }; |
3452 | |
3453 | |
3454 | class ConstantMatrixType final : public MatrixType { |
3455 | protected: |
3456 | friend class ASTContext; |
3457 | |
3458 | |
3459 | unsigned NumRows; |
3460 | unsigned NumColumns; |
3461 | |
3462 | static constexpr unsigned MaxElementsPerDimension = (1 << 20) - 1; |
3463 | |
3464 | ConstantMatrixType(QualType MatrixElementType, unsigned NRows, |
3465 | unsigned NColumns, QualType CanonElementType); |
3466 | |
3467 | ConstantMatrixType(TypeClass typeClass, QualType MatrixType, unsigned NRows, |
3468 | unsigned NColumns, QualType CanonElementType); |
3469 | |
3470 | public: |
3471 | |
3472 | unsigned getNumRows() const { return NumRows; } |
3473 | |
3474 | |
3475 | unsigned getNumColumns() const { return NumColumns; } |
3476 | |
3477 | |
3478 | unsigned getNumElementsFlattened() const { |
3479 | return getNumRows() * getNumColumns(); |
3480 | } |
3481 | |
3482 | |
3483 | static constexpr bool isDimensionValid(size_t NumElements) { |
3484 | return NumElements > 0 && NumElements <= MaxElementsPerDimension; |
3485 | } |
3486 | |
3487 | |
3488 | static constexpr unsigned getMaxElementsPerDimension() { |
3489 | return MaxElementsPerDimension; |
3490 | } |
3491 | |
3492 | void Profile(llvm::FoldingSetNodeID &ID) { |
3493 | Profile(ID, getElementType(), getNumRows(), getNumColumns(), |
3494 | getTypeClass()); |
3495 | } |
3496 | |
3497 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, |
3498 | unsigned NumRows, unsigned NumColumns, |
3499 | TypeClass TypeClass) { |
3500 | ID.AddPointer(ElementType.getAsOpaquePtr()); |
3501 | ID.AddInteger(NumRows); |
3502 | ID.AddInteger(NumColumns); |
3503 | ID.AddInteger(TypeClass); |
3504 | } |
3505 | |
3506 | static bool classof(const Type *T) { |
3507 | return T->getTypeClass() == ConstantMatrix; |
3508 | } |
3509 | }; |
3510 | |
3511 | |
3512 | |
3513 | class DependentSizedMatrixType final : public MatrixType { |
3514 | friend class ASTContext; |
3515 | |
3516 | const ASTContext &Context; |
3517 | Expr *RowExpr; |
3518 | Expr *ColumnExpr; |
3519 | |
3520 | SourceLocation loc; |
3521 | |
3522 | DependentSizedMatrixType(const ASTContext &Context, QualType ElementType, |
3523 | QualType CanonicalType, Expr *RowExpr, |
3524 | Expr *ColumnExpr, SourceLocation loc); |
3525 | |
3526 | public: |
3527 | Expr *getRowExpr() const { return RowExpr; } |
3528 | Expr *getColumnExpr() const { return ColumnExpr; } |
3529 | SourceLocation getAttributeLoc() const { return loc; } |
3530 | |
3531 | static bool classof(const Type *T) { |
3532 | return T->getTypeClass() == DependentSizedMatrix; |
3533 | } |
3534 | |
3535 | void Profile(llvm::FoldingSetNodeID &ID) { |
3536 | Profile(ID, Context, getElementType(), getRowExpr(), getColumnExpr()); |
3537 | } |
3538 | |
3539 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
3540 | QualType ElementType, Expr *RowExpr, Expr *ColumnExpr); |
3541 | }; |
3542 | |
3543 | |
3544 | |
3545 | class FunctionType : public Type { |
3546 | |
3547 | QualType ResultType; |
3548 | |
3549 | public: |
3550 | |
3551 | |
3552 | |
3553 | |
3554 | |
3555 | |
3556 | |
3557 | |
3558 | |
3559 | |
3560 | |
3561 | |
3562 | |
3563 | |
3564 | |
3565 | |
3566 | |
3567 | |
3568 | |
3569 | |
3570 | |
3571 | class ExtParameterInfo { |
3572 | enum { |
3573 | ABIMask = 0x0F, |
3574 | IsConsumed = 0x10, |
3575 | HasPassObjSize = 0x20, |
3576 | IsNoEscape = 0x40, |
3577 | }; |
3578 | unsigned char Data = 0; |
3579 | |
3580 | public: |
3581 | ExtParameterInfo() = default; |
3582 | |
3583 | |
3584 | ParameterABI getABI() const { return ParameterABI(Data & ABIMask); } |
3585 | ExtParameterInfo withABI(ParameterABI kind) const { |
3586 | ExtParameterInfo copy = *this; |
3587 | copy.Data = (copy.Data & ~ABIMask) | unsigned(kind); |
3588 | return copy; |
3589 | } |
3590 | |
3591 | |
3592 | |
3593 | bool isConsumed() const { return (Data & IsConsumed); } |
3594 | ExtParameterInfo withIsConsumed(bool consumed) const { |
3595 | ExtParameterInfo copy = *this; |
3596 | if (consumed) |
3597 | copy.Data |= IsConsumed; |
3598 | else |
3599 | copy.Data &= ~IsConsumed; |
3600 | return copy; |
3601 | } |
3602 | |
3603 | bool hasPassObjectSize() const { return Data & HasPassObjSize; } |
3604 | ExtParameterInfo withHasPassObjectSize() const { |
3605 | ExtParameterInfo Copy = *this; |
3606 | Copy.Data |= HasPassObjSize; |
3607 | return Copy; |
3608 | } |
3609 | |
3610 | bool isNoEscape() const { return Data & IsNoEscape; } |
3611 | ExtParameterInfo withIsNoEscape(bool NoEscape) const { |
3612 | ExtParameterInfo Copy = *this; |
3613 | if (NoEscape) |
3614 | Copy.Data |= IsNoEscape; |
3615 | else |
3616 | Copy.Data &= ~IsNoEscape; |
3617 | return Copy; |
3618 | } |
3619 | |
3620 | unsigned char getOpaqueValue() const { return Data; } |
3621 | static ExtParameterInfo getFromOpaqueValue(unsigned char data) { |
3622 | ExtParameterInfo result; |
3623 | result.Data = data; |
3624 | return result; |
3625 | } |
3626 | |
3627 | friend bool operator==(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3628 | return lhs.Data == rhs.Data; |
3629 | } |
3630 | |
3631 | friend bool operator!=(ExtParameterInfo lhs, ExtParameterInfo rhs) { |
3632 | return lhs.Data != rhs.Data; |
3633 | } |
3634 | }; |
3635 | |
3636 | |
3637 | |
3638 | |
3639 | |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | |
3646 | |
3647 | |
3648 | |
3649 | |
3650 | |
3651 | |
3652 | |
3653 | |
3654 | |
3655 | |
3656 | class ExtInfo { |
3657 | friend class FunctionType; |
3658 | |
3659 | |
3660 | |
3661 | |
3662 | |
3663 | |
3664 | |
3665 | |
3666 | |
3667 | enum { CallConvMask = 0x1F }; |
3668 | enum { NoReturnMask = 0x20 }; |
3669 | enum { ProducesResultMask = 0x40 }; |
3670 | enum { NoCallerSavedRegsMask = 0x80 }; |
3671 | enum { |
3672 | RegParmMask = 0x700, |
3673 | RegParmOffset = 8 |
3674 | }; |
3675 | enum { NoCfCheckMask = 0x800 }; |
3676 | enum { CmseNSCallMask = 0x1000 }; |
3677 | uint16_t Bits = CC_C; |
3678 | |
3679 | ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {} |
3680 | |
3681 | public: |
3682 | |
3683 | |
3684 | ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc, |
3685 | bool producesResult, bool noCallerSavedRegs, bool NoCfCheck, |
3686 | bool cmseNSCall) { |
3687 | assert((!hasRegParm || regParm < 7) && "Invalid regparm value"); |
3688 | Bits = ((unsigned)cc) | (noReturn ? NoReturnMask : 0) | |
3689 | (producesResult ? ProducesResultMask : 0) | |
3690 | (noCallerSavedRegs ? NoCallerSavedRegsMask : 0) | |
3691 | (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0) | |
3692 | (NoCfCheck ? NoCfCheckMask : 0) | |
3693 | (cmseNSCall ? CmseNSCallMask : 0); |
3694 | } |
3695 | |
3696 | |
3697 | |
3698 | ExtInfo() = default; |
3699 | |
3700 | |
3701 | |
3702 | ExtInfo(CallingConv CC) : Bits(CC) {} |
3703 | |
3704 | bool getNoReturn() const { return Bits & NoReturnMask; } |
3705 | bool getProducesResult() const { return Bits & ProducesResultMask; } |
3706 | bool getCmseNSCall() const { return Bits & CmseNSCallMask; } |
3707 | bool getNoCallerSavedRegs() const { return Bits & NoCallerSavedRegsMask; } |
3708 | bool getNoCfCheck() const { return Bits & NoCfCheckMask; } |
3709 | bool getHasRegParm() const { return ((Bits & RegParmMask) >> RegParmOffset) != 0; } |
3710 | |
3711 | unsigned getRegParm() const { |
3712 | unsigned RegParm = (Bits & RegParmMask) >> RegParmOffset; |
3713 | if (RegParm > 0) |
3714 | --RegParm; |
3715 | return RegParm; |
3716 | } |
3717 | |
3718 | CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } |
3719 | |
3720 | bool operator==(ExtInfo Other) const { |
3721 | return Bits == Other.Bits; |
3722 | } |
3723 | bool operator!=(ExtInfo Other) const { |
3724 | return Bits != Other.Bits; |
3725 | } |
3726 | |
3727 | |
3728 | |
3729 | |
3730 | ExtInfo withNoReturn(bool noReturn) const { |
3731 | if (noReturn) |
3732 | return ExtInfo(Bits | NoReturnMask); |
3733 | else |
3734 | return ExtInfo(Bits & ~NoReturnMask); |
3735 | } |
3736 | |
3737 | ExtInfo withProducesResult(bool producesResult) const { |
3738 | if (producesResult) |
3739 | return ExtInfo(Bits | ProducesResultMask); |
3740 | else |
3741 | return ExtInfo(Bits & ~ProducesResultMask); |
3742 | } |
3743 | |
3744 | ExtInfo withCmseNSCall(bool cmseNSCall) const { |
3745 | if (cmseNSCall) |
3746 | return ExtInfo(Bits | CmseNSCallMask); |
3747 | else |
3748 | return ExtInfo(Bits & ~CmseNSCallMask); |
3749 | } |
3750 | |
3751 | ExtInfo withNoCallerSavedRegs(bool noCallerSavedRegs) const { |
3752 | if (noCallerSavedRegs) |
3753 | return ExtInfo(Bits | NoCallerSavedRegsMask); |
3754 | else |
3755 | return ExtInfo(Bits & ~NoCallerSavedRegsMask); |
3756 | } |
3757 | |
3758 | ExtInfo withNoCfCheck(bool noCfCheck) const { |
3759 | if (noCfCheck) |
3760 | return ExtInfo(Bits | NoCfCheckMask); |
3761 | else |
3762 | return ExtInfo(Bits & ~NoCfCheckMask); |
3763 | } |
3764 | |
3765 | ExtInfo withRegParm(unsigned RegParm) const { |
3766 | assert(RegParm < 7 && "Invalid regparm value"); |
3767 | return ExtInfo((Bits & ~RegParmMask) | |
3768 | ((RegParm + 1) << RegParmOffset)); |
3769 | } |
3770 | |
3771 | ExtInfo withCallingConv(CallingConv cc) const { |
3772 | return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); |
3773 | } |
3774 | |
3775 | void Profile(llvm::FoldingSetNodeID &ID) const { |
3776 | ID.AddInteger(Bits); |
3777 | } |
3778 | }; |
3779 | |
3780 | |
3781 | |
3782 | |
3783 | struct ExceptionType { QualType Type; }; |
3784 | |
3785 | |
3786 | |
3787 | |
3788 | |
3789 | struct alignas(void *) FunctionTypeExtraBitfields { |
3790 | |
3791 | |
3792 | |
3793 | unsigned NumExceptionType; |
3794 | }; |
3795 | |
3796 | protected: |
3797 | FunctionType(TypeClass tc, QualType res, QualType Canonical, |
3798 | TypeDependence Dependence, ExtInfo Info) |
3799 | : Type(tc, Canonical, Dependence), ResultType(res) { |
3800 | FunctionTypeBits.ExtInfo = Info.Bits; |
3801 | } |
3802 | |
3803 | Qualifiers getFastTypeQuals() const { |
3804 | return Qualifiers::fromFastMask(FunctionTypeBits.FastTypeQuals); |
3805 | } |
3806 | |
3807 | public: |
3808 | QualType getReturnType() const { return ResultType; } |
3809 | |
3810 | bool getHasRegParm() const { return getExtInfo().getHasRegParm(); } |
3811 | unsigned getRegParmType() const { return getExtInfo().getRegParm(); } |
3812 | |
3813 | |
3814 | |
3815 | |
3816 | bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } |
3817 | |
3818 | bool getCmseNSCallAttr() const { return getExtInfo().getCmseNSCall(); } |
3819 | CallingConv getCallConv() const { return getExtInfo().getCC(); } |
3820 | ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } |
3821 | |
3822 | static_assert((~Qualifiers::FastMask & Qualifiers::CVRMask) == 0, |
3823 | "Const, volatile and restrict are assumed to be a subset of " |
3824 | "the fast qualifiers."); |
3825 | |
3826 | bool isConst() const { return getFastTypeQuals().hasConst(); } |
3827 | bool isVolatile() const { return getFastTypeQuals().hasVolatile(); } |
3828 | bool isRestrict() const { return getFastTypeQuals().hasRestrict(); } |
3829 | |
3830 | |
3831 | |
3832 | QualType getCallResultType(const ASTContext &Context) const { |
3833 | return getReturnType().getNonLValueExprType(Context); |
3834 | } |
3835 | |
3836 | static StringRef getNameForCallConv(CallingConv CC); |
3837 | |
3838 | static bool classof(const Type *T) { |
3839 | return T->getTypeClass() == FunctionNoProto || |
3840 | T->getTypeClass() == FunctionProto; |
3841 | } |
3842 | }; |
3843 | |
3844 | |
3845 | |
3846 | class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { |
3847 | friend class ASTContext; |
3848 | |
3849 | FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) |
3850 | : FunctionType(FunctionNoProto, Result, Canonical, |
3851 | Result->getDependence() & |
3852 | ~(TypeDependence::DependentInstantiation | |
3853 | TypeDependence::UnexpandedPack), |
3854 | Info) {} |
3855 | |
3856 | public: |
3857 | |
3858 | |
3859 | bool isSugared() const { return false; } |
3860 | QualType desugar() const { return QualType(this, 0); } |
3861 | |
3862 | void Profile(llvm::FoldingSetNodeID &ID) { |
3863 | Profile(ID, getReturnType(), getExtInfo()); |
3864 | } |
3865 | |
3866 | static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, |
3867 | ExtInfo Info) { |
3868 | Info.Profile(ID); |
3869 | ID.AddPointer(ResultType.getAsOpaquePtr()); |
3870 | } |
3871 | |
3872 | static bool classof(const Type *T) { |
3873 | return T->getTypeClass() == FunctionNoProto; |
3874 | } |
3875 | }; |
3876 | |
3877 | |
3878 | |
3879 | |
3880 | |
3881 | |
3882 | |
3883 | |
3884 | class FunctionProtoType final |
3885 | : public FunctionType, |
3886 | public llvm::FoldingSetNode, |
3887 | private llvm::TrailingObjects< |
3888 | FunctionProtoType, QualType, SourceLocation, |
3889 | FunctionType::FunctionTypeExtraBitfields, FunctionType::ExceptionType, |
3890 | Expr *, FunctionDecl *, FunctionType::ExtParameterInfo, Qualifiers> { |
3891 | friend class ASTContext; |
3892 | friend TrailingObjects; |
3893 | |
3894 | |
3895 | |
3896 | |
3897 | |
3898 | |
3899 | |
3900 | |
3901 | |
3902 | |
3903 | |
3904 | |
3905 | |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | |
3917 | |
3918 | |
3919 | |
3920 | |
3921 | |
3922 | |
3923 | |
3924 | |
3925 | |
3926 | |
3927 | |
3928 | |
3929 | |
3930 | |
3931 | |
3932 | |
3933 | |
3934 | |
3935 | |
3936 | public: |
3937 | |
3938 | |
3939 | |
3940 | |
3941 | struct ExceptionSpecInfo { |
3942 | |
3943 | ExceptionSpecificationType Type = EST_None; |
3944 | |
3945 | |
3946 | ArrayRef<QualType> Exceptions; |
3947 | |
3948 | |
3949 | Expr *NoexceptExpr = nullptr; |
3950 | |
3951 | |
3952 | |
3953 | FunctionDecl *SourceDecl = nullptr; |
3954 | |
3955 | |
3956 | |
3957 | FunctionDecl *SourceTemplate = nullptr; |
3958 | |
3959 | ExceptionSpecInfo() = default; |
3960 | |
3961 | ExceptionSpecInfo(ExceptionSpecificationType EST) : Type(EST) {} |
3962 | }; |
3963 | |
3964 | |
3965 | |
3966 | |
3967 | struct ExtProtoInfo { |
3968 | FunctionType::ExtInfo ExtInfo; |
3969 | bool Variadic : 1; |
3970 | bool HasTrailingReturn : 1; |
3971 | Qualifiers TypeQuals; |
3972 | RefQualifierKind RefQualifier = RQ_None; |
3973 | ExceptionSpecInfo ExceptionSpec; |
3974 | const ExtParameterInfo *ExtParameterInfos = nullptr; |
3975 | SourceLocation EllipsisLoc; |
3976 | |
3977 | ExtProtoInfo() : Variadic(false), HasTrailingReturn(false) {} |
3978 | |
3979 | ExtProtoInfo(CallingConv CC) |
3980 | : ExtInfo(CC), Variadic(false), HasTrailingReturn(false) {} |
3981 | |
3982 | ExtProtoInfo withExceptionSpec(const ExceptionSpecInfo &ESI) { |
3983 | ExtProtoInfo Result(*this); |
3984 | Result.ExceptionSpec = ESI; |
3985 | return Result; |
3986 | } |
3987 | }; |
3988 | |
3989 | private: |
3990 | unsigned numTrailingObjects(OverloadToken<QualType>) const { |
3991 | return getNumParams(); |
3992 | } |
3993 | |
3994 | unsigned numTrailingObjects(OverloadToken<SourceLocation>) const { |
3995 | return isVariadic(); |
3996 | } |
3997 | |
3998 | unsigned numTrailingObjects(OverloadToken<FunctionTypeExtraBitfields>) const { |
3999 | return hasExtraBitfields(); |
4000 | } |
4001 | |
4002 | unsigned numTrailingObjects(OverloadToken<ExceptionType>) const { |
4003 | return getExceptionSpecSize().NumExceptionType; |
4004 | } |
4005 | |
4006 | unsigned numTrailingObjects(OverloadToken<Expr *>) const { |
4007 | return getExceptionSpecSize().NumExprPtr; |
4008 | } |
4009 | |
4010 | unsigned numTrailingObjects(OverloadToken<FunctionDecl *>) const { |
4011 | return getExceptionSpecSize().NumFunctionDeclPtr; |
4012 | } |
4013 | |
4014 | unsigned numTrailingObjects(OverloadToken<ExtParameterInfo>) const { |
4015 | return hasExtParameterInfos() ? getNumParams() : 0; |
4016 | } |
4017 | |
4018 | |
4019 | |
4020 | static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray, |
4021 | unsigned numArgs) { |
4022 | for (unsigned Idx = 0; Idx < numArgs; ++Idx) |
4023 | if (ArgArray[Idx]->containsUnexpandedParameterPack()) |
4024 | return true; |
4025 | |
4026 | return false; |
4027 | } |
4028 | |
4029 | FunctionProtoType(QualType result, ArrayRef<QualType> params, |
4030 | QualType canonical, const ExtProtoInfo &epi); |
4031 | |
4032 | |
4033 | |
4034 | |
4035 | struct ExceptionSpecSizeHolder { |
4036 | unsigned NumExceptionType; |
4037 | unsigned NumExprPtr; |
4038 | unsigned NumFunctionDeclPtr; |
4039 | }; |
4040 | |
4041 | |
4042 | |
4043 | static ExceptionSpecSizeHolder |
4044 | getExceptionSpecSize(ExceptionSpecificationType EST, unsigned NumExceptions) { |
4045 | switch (EST) { |
4046 | case EST_None: |
4047 | case EST_DynamicNone: |
4048 | case EST_MSAny: |
4049 | case EST_BasicNoexcept: |
4050 | case EST_Unparsed: |
4051 | case EST_NoThrow: |
4052 | return {0, 0, 0}; |
4053 | |
4054 | case EST_Dynamic: |
4055 | return {NumExceptions, 0, 0}; |
4056 | |
4057 | case EST_DependentNoexcept: |
4058 | case EST_NoexceptFalse: |
4059 | case EST_NoexceptTrue: |
4060 | return {0, 1, 0}; |
4061 | |
4062 | case EST_Uninstantiated: |
4063 | return {0, 0, 2}; |
4064 | |
4065 | case EST_Unevaluated: |
4066 | return {0, 0, 1}; |
4067 | } |
4068 | llvm_unreachable("bad exception specification kind"); |
4069 | } |
4070 | |
4071 | |
4072 | |
4073 | ExceptionSpecSizeHolder getExceptionSpecSize() const { |
4074 | return getExceptionSpecSize(getExceptionSpecType(), getNumExceptions()); |
4075 | } |
4076 | |
4077 | |
4078 | static bool hasExtraBitfields(ExceptionSpecificationType EST) { |
4079 | |
4080 | |
4081 | return EST == EST_Dynamic; |
4082 | } |
4083 | |
4084 | |
4085 | bool hasExtraBitfields() const { |
4086 | return hasExtraBitfields(getExceptionSpecType()); |
4087 | } |
4088 | |
4089 | bool hasExtQualifiers() const { |
4090 | return FunctionTypeBits.HasExtQuals; |
4091 | } |
4092 | |
4093 | public: |
4094 | unsigned getNumParams() const { return FunctionTypeBits.NumParams; } |
4095 | |
4096 | QualType getParamType(unsigned i) const { |
4097 | assert(i < getNumParams() && "invalid parameter index"); |
4098 | return param_type_begin()[i]; |
4099 | } |
4100 | |
4101 | ArrayRef<QualType> getParamTypes() const { |
4102 | return llvm::makeArrayRef(param_type_begin(), param_type_end()); |
4103 | } |
4104 | |
4105 | ExtProtoInfo getExtProtoInfo() const { |
4106 | ExtProtoInfo EPI; |
4107 | EPI.ExtInfo = getExtInfo(); |
4108 | EPI.Variadic = isVariadic(); |
4109 | EPI.EllipsisLoc = getEllipsisLoc(); |
4110 | EPI.HasTrailingReturn = hasTrailingReturn(); |
4111 | EPI.ExceptionSpec = getExceptionSpecInfo(); |
4112 | EPI.TypeQuals = getMethodQuals(); |
4113 | EPI.RefQualifier = getRefQualifier(); |
4114 | EPI.ExtParameterInfos = getExtParameterInfosOrNull(); |
4115 | return EPI; |
4116 | } |
4117 | |
4118 | |
4119 | ExceptionSpecificationType getExceptionSpecType() const { |
4120 | return static_cast<ExceptionSpecificationType>( |
4121 | FunctionTypeBits.ExceptionSpecType); |
4122 | } |
4123 | |
4124 | |
4125 | bool hasExceptionSpec() const { return getExceptionSpecType() != EST_None; } |
4126 | |
4127 | |
4128 | bool hasDynamicExceptionSpec() const { |
4129 | return isDynamicExceptionSpec(getExceptionSpecType()); |
4130 | } |
4131 | |
4132 | |
4133 | bool hasNoexceptExceptionSpec() const { |
4134 | return isNoexceptExceptionSpec(getExceptionSpecType()); |
4135 | } |
4136 | |
4137 | |
4138 | bool hasDependentExceptionSpec() const; |
4139 | |
4140 | |
4141 | |
4142 | bool hasInstantiationDependentExceptionSpec() const; |
4143 | |
4144 | |
4145 | ExceptionSpecInfo getExceptionSpecInfo() const { |
4146 | ExceptionSpecInfo Result; |
4147 | Result.Type = getExceptionSpecType(); |
4148 | if (Result.Type == EST_Dynamic) { |
4149 | Result.Exceptions = exceptions(); |
4150 | } else if (isComputedNoexcept(Result.Type)) { |
4151 | Result.NoexceptExpr = getNoexceptExpr(); |
4152 | } else if (Result.Type == EST_Uninstantiated) { |
4153 | Result.SourceDecl = getExceptionSpecDecl(); |
4154 | Result.SourceTemplate = getExceptionSpecTemplate(); |
4155 | } else if (Result.Type == EST_Unevaluated) { |
4156 | Result.SourceDecl = getExceptionSpecDecl(); |
4157 | } |
4158 | return Result; |
4159 | } |
4160 | |
4161 | |
4162 | unsigned getNumExceptions() const { |
4163 | return getExceptionSpecType() == EST_Dynamic |
4164 | ? getTrailingObjects<FunctionTypeExtraBitfields>() |
4165 | ->NumExceptionType |
4166 | : 0; |
4167 | } |
4168 | |
4169 | |
4170 | QualType getExceptionType(unsigned i) const { |
4171 | assert(i < getNumExceptions() && "Invalid exception number!"); |
4172 | return exception_begin()[i]; |
4173 | } |
4174 | |
4175 | |
4176 | |
4177 | Expr *getNoexceptExpr() const { |
4178 | if (!isComputedNoexcept(getExceptionSpecType())) |
4179 | return nullptr; |
4180 | return *getTrailingObjects<Expr *>(); |
4181 | } |
4182 | |
4183 | |
4184 | |
4185 | |
4186 | |
4187 | FunctionDecl *getExceptionSpecDecl() const { |
4188 | if (getExceptionSpecType() != EST_Uninstantiated && |
4189 | getExceptionSpecType() != EST_Unevaluated) |
4190 | return nullptr; |
4191 | return getTrailingObjects<FunctionDecl *>()[0]; |
4192 | } |
4193 | |
4194 | |
4195 | |
4196 | |
4197 | |
4198 | FunctionDecl *getExceptionSpecTemplate() const { |
4199 | if (getExceptionSpecType() != EST_Uninstantiated) |
4200 | return nullptr; |
4201 | return getTrailingObjects<FunctionDecl *>()[1]; |
4202 | } |
4203 | |
4204 | |
4205 | |
4206 | CanThrowResult canThrow() const; |
4207 | |
4208 | |
4209 | |
4210 | |
4211 | bool isNothrow(bool ResultIfDependent = false) const { |
4212 | return ResultIfDependent ? canThrow() != CT_Can : canThrow() == CT_Cannot; |
4213 | } |
4214 | |
4215 | |
4216 | bool isVariadic() const { return FunctionTypeBits.Variadic; } |
4217 | |
4218 | SourceLocation getEllipsisLoc() const { |
4219 | return isVariadic() ? *getTrailingObjects<SourceLocation>() |
4220 | : SourceLocation(); |
4221 | } |
4222 | |
4223 | |
4224 | |
4225 | |
4226 | |
4227 | |
4228 | |
4229 | bool isTemplateVariadic() const; |
4230 | |
4231 | |
4232 | bool hasTrailingReturn() const { return FunctionTypeBits.HasTrailingReturn; } |
4233 | |
4234 | Qualifiers getMethodQuals() const { |
4235 | if (hasExtQualifiers()) |
4236 | return *getTrailingObjects<Qualifiers>(); |
4237 | else |
4238 | return getFastTypeQuals(); |
4239 | } |
4240 | |
4241 | |
4242 | RefQualifierKind getRefQualifier() const { |
4243 | return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier); |
4244 | } |
4245 | |
4246 | using param_type_iterator = const QualType *; |
4247 | using param_type_range = llvm::iterator_range<param_type_iterator>; |
4248 | |
4249 | param_type_range param_types() const { |
4250 | return param_type_range(param_type_begin(), param_type_end()); |
4251 | } |
4252 | |
4253 | param_type_iterator param_type_begin() const { |
4254 | return getTrailingObjects<QualType>(); |
4255 | } |
4256 | |
4257 | param_type_iterator param_type_end() const { |
4258 | return param_type_begin() + getNumParams(); |
4259 | } |
4260 | |
4261 | using exception_iterator = const QualType *; |
4262 | |
4263 | ArrayRef<QualType> exceptions() const { |
4264 | return llvm::makeArrayRef(exception_begin(), exception_end()); |
4265 | } |
4266 | |
4267 | exception_iterator exception_begin() const { |
4268 | return reinterpret_cast<exception_iterator>( |
4269 | getTrailingObjects<ExceptionType>()); |
4270 | } |
4271 | |
4272 | exception_iterator exception_end() const { |
4273 | return exception_begin() + getNumExceptions(); |
4274 | } |
4275 | |
4276 | |
4277 | |
4278 | bool hasExtParameterInfos() const { |
4279 | return FunctionTypeBits.HasExtParameterInfos; |
4280 | } |
4281 | |
4282 | ArrayRef<ExtParameterInfo> getExtParameterInfos() const { |
4283 | assert(hasExtParameterInfos()); |
4284 | return ArrayRef<ExtParameterInfo>(getTrailingObjects<ExtParameterInfo>(), |
4285 | getNumParams()); |
4286 | } |
4287 | |
4288 | |
4289 | |
4290 | |
4291 | const ExtParameterInfo *getExtParameterInfosOrNull() const { |
4292 | if (!hasExtParameterInfos()) |
4293 | return nullptr; |
4294 | return getTrailingObjects<ExtParameterInfo>(); |
4295 | } |
4296 | |
4297 | ExtParameterInfo getExtParameterInfo(unsigned I) const { |
4298 | assert(I < getNumParams() && "parameter index out of range"); |
4299 | if (hasExtParameterInfos()) |
4300 | return getTrailingObjects<ExtParameterInfo>()[I]; |
4301 | return ExtParameterInfo(); |
4302 | } |
4303 | |
4304 | ParameterABI getParameterABI(unsigned I) const { |
4305 | assert(I < getNumParams() && "parameter index out of range"); |
4306 | if (hasExtParameterInfos()) |
4307 | return getTrailingObjects<ExtParameterInfo>()[I].getABI(); |
4308 | return ParameterABI::Ordinary; |
4309 | } |
4310 | |
4311 | bool isParamConsumed(unsigned I) const { |
4312 | assert(I < getNumParams() && "parameter index out of range"); |
4313 | if (hasExtParameterInfos()) |
4314 | return getTrailingObjects<ExtParameterInfo>()[I].isConsumed(); |
4315 | return false; |
4316 | } |
4317 | |
4318 | bool isSugared() const { return false; } |
4319 | QualType desugar() const { return QualType(this, 0); } |
4320 | |
4321 | void printExceptionSpecification(raw_ostream &OS, |
4322 | const PrintingPolicy &Policy) const; |
4323 | |
4324 | static bool classof(const Type *T) { |
4325 | return T->getTypeClass() == FunctionProto; |
4326 | } |
4327 | |
4328 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx); |
4329 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, |
4330 | param_type_iterator ArgTys, unsigned NumArgs, |
4331 | const ExtProtoInfo &EPI, const ASTContext &Context, |
4332 | bool Canonical); |
4333 | }; |
4334 | |
4335 | |
4336 | |
4337 | |
4338 | |
4339 | |
4340 | class UnresolvedUsingType : public Type { |
4341 | friend class ASTContext; |
4342 | |
4343 | UnresolvedUsingTypenameDecl *Decl; |
4344 | |
4345 | UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) |
4346 | : Type(UnresolvedUsing, QualType(), |
4347 | TypeDependence::DependentInstantiation), |
4348 | Decl(const_cast<UnresolvedUsingTypenameDecl *>(D)) {} |
4349 | |
4350 | public: |
4351 | UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } |
4352 | |
4353 | bool isSugared() const { return false; } |
4354 | QualType desugar() const { return QualType(this, 0); } |
4355 | |
4356 | static bool classof(const Type *T) { |
4357 | return T->getTypeClass() == UnresolvedUsing; |
4358 | } |
4359 | |
4360 | void Profile(llvm::FoldingSetNodeID &ID) { |
4361 | return Profile(ID, Decl); |
4362 | } |
4363 | |
4364 | static void Profile(llvm::FoldingSetNodeID &ID, |
4365 | UnresolvedUsingTypenameDecl *D) { |
4366 | ID.AddPointer(D); |
4367 | } |
4368 | }; |
4369 | |
4370 | class TypedefType : public Type { |
4371 | TypedefNameDecl *Decl; |
4372 | |
4373 | private: |
4374 | friend class ASTContext; |
4375 | |
4376 | TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType underlying, |
4377 | QualType can); |
4378 | |
4379 | public: |
4380 | TypedefNameDecl *getDecl() const { return Decl; } |
4381 | |
4382 | bool isSugared() const { return true; } |
4383 | QualType desugar() const; |
4384 | |
4385 | static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } |
4386 | }; |
4387 | |
4388 | |
4389 | |
4390 | class MacroQualifiedType : public Type { |
4391 | friend class ASTContext; |
4392 | |
4393 | QualType UnderlyingTy; |
4394 | const IdentifierInfo *MacroII; |
4395 | |
4396 | MacroQualifiedType(QualType UnderlyingTy, QualType CanonTy, |
4397 | const IdentifierInfo *MacroII) |
4398 | : Type(MacroQualified, CanonTy, UnderlyingTy->getDependence()), |
4399 | UnderlyingTy(UnderlyingTy), MacroII(MacroII) { |
4400 | assert(isa<AttributedType>(UnderlyingTy) && |
4401 | "Expected a macro qualified type to only wrap attributed types."); |
4402 | } |
4403 | |
4404 | public: |
4405 | const IdentifierInfo *getMacroIdentifier() const { return MacroII; } |
4406 | QualType getUnderlyingType() const { return UnderlyingTy; } |
4407 | |
4408 | |
4409 | |
4410 | QualType getModifiedType() const; |
4411 | |
4412 | bool isSugared() const { return true; } |
4413 | QualType desugar() const; |
4414 | |
4415 | static bool classof(const Type *T) { |
4416 | return T->getTypeClass() == MacroQualified; |
4417 | } |
4418 | }; |
4419 | |
4420 | |
4421 | class TypeOfExprType : public Type { |
4422 | Expr *TOExpr; |
4423 | |
4424 | protected: |
4425 | friend class ASTContext; |
4426 | |
4427 | TypeOfExprType(Expr *E, QualType can = QualType()); |
4428 | |
4429 | public: |
4430 | Expr *getUnderlyingExpr() const { return TOExpr; } |
4431 | |
4432 | |
4433 | QualType desugar() const; |
4434 | |
4435 | |
4436 | bool isSugared() const; |
4437 | |
4438 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } |
4439 | }; |
4440 | |
4441 | |
4442 | |
4443 | |
4444 | |
4445 | |
4446 | |
4447 | class DependentTypeOfExprType |
4448 | : public TypeOfExprType, public llvm::FoldingSetNode { |
4449 | const ASTContext &Context; |
4450 | |
4451 | public: |
4452 | DependentTypeOfExprType(const ASTContext &Context, Expr *E) |
4453 | : TypeOfExprType(E), Context(Context) {} |
4454 | |
4455 | void Profile(llvm::FoldingSetNodeID &ID) { |
4456 | Profile(ID, Context, getUnderlyingExpr()); |
4457 | } |
4458 | |
4459 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4460 | Expr *E); |
4461 | }; |
4462 | |
4463 | |
4464 | class TypeOfType : public Type { |
4465 | friend class ASTContext; |
4466 | |
4467 | QualType TOType; |
4468 | |
4469 | TypeOfType(QualType T, QualType can) |
4470 | : Type(TypeOf, can, T->getDependence()), TOType(T) { |
4471 | assert(!isa<TypedefType>(can) && "Invalid canonical type"); |
4472 | } |
4473 | |
4474 | public: |
4475 | QualType getUnderlyingType() const { return TOType; } |
4476 | |
4477 | |
4478 | QualType desugar() const { return getUnderlyingType(); } |
4479 | |
4480 | |
4481 | bool isSugared() const { return true; } |
4482 | |
4483 | static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } |
4484 | }; |
4485 | |
4486 | |
4487 | class DecltypeType : public Type { |
4488 | Expr *E; |
4489 | QualType UnderlyingType; |
4490 | |
4491 | protected: |
4492 | friend class ASTContext; |
4493 | |
4494 | DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); |
4495 | |
4496 | public: |
4497 | Expr *getUnderlyingExpr() const { return E; } |
4498 | QualType getUnderlyingType() const { return UnderlyingType; } |
4499 | |
4500 | |
4501 | QualType desugar() const; |
4502 | |
4503 | |
4504 | bool isSugared() const; |
4505 | |
4506 | static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } |
4507 | }; |
4508 | |
4509 | |
4510 | |
4511 | |
4512 | |
4513 | |
4514 | |
4515 | class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { |
4516 | const ASTContext &Context; |
4517 | |
4518 | public: |
4519 | DependentDecltypeType(const ASTContext &Context, Expr *E); |
4520 | |
4521 | void Profile(llvm::FoldingSetNodeID &ID) { |
4522 | Profile(ID, Context, getUnderlyingExpr()); |
4523 | } |
4524 | |
4525 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
4526 | Expr *E); |
4527 | }; |
4528 | |
4529 | |
4530 | class UnaryTransformType : public Type { |
4531 | public: |
4532 | enum UTTKind { |
4533 | EnumUnderlyingType |
4534 | }; |
4535 | |
4536 | private: |
4537 | |
4538 | QualType BaseType; |
4539 | |
4540 | |
4541 | QualType UnderlyingType; |
4542 | |
4543 | UTTKind UKind; |
4544 | |
4545 | protected: |
4546 | friend class ASTContext; |
4547 | |
4548 | UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind, |
4549 | QualType CanonicalTy); |
4550 | |
4551 | public: |
4552 | bool isSugared() const { return !isDependentType(); } |
4553 | QualType desugar() const { return UnderlyingType; } |
4554 | |
4555 | QualType getUnderlyingType() const { return UnderlyingType; } |
4556 | QualType getBaseType() const { return BaseType; } |
4557 | |
4558 | UTTKind getUTTKind() const { return UKind; } |
4559 | |
4560 | static bool classof(const Type *T) { |
4561 | return T->getTypeClass() == UnaryTransform; |
4562 | } |
4563 | }; |
4564 | |
4565 | |
4566 | |
4567 | |
4568 | |
4569 | |
4570 | |
4571 | class DependentUnaryTransformType : public UnaryTransformType, |
4572 | public llvm::FoldingSetNode { |
4573 | public: |
4574 | DependentUnaryTransformType(const ASTContext &C, QualType BaseType, |
4575 | UTTKind UKind); |
4576 | |
4577 | void Profile(llvm::FoldingSetNodeID &ID) { |
4578 | Profile(ID, getBaseType(), getUTTKind()); |
4579 | } |
4580 | |
4581 | static void Profile(llvm::FoldingSetNodeID &ID, QualType BaseType, |
4582 | UTTKind UKind) { |
4583 | ID.AddPointer(BaseType.getAsOpaquePtr()); |
4584 | ID.AddInteger((unsigned)UKind); |
4585 | } |
4586 | }; |
4587 | |
4588 | class TagType : public Type { |
4589 | friend class ASTReader; |
4590 | template <class T> friend class serialization::AbstractTypeReader; |
4591 | |
4592 | |
4593 | |
4594 | TagDecl *decl; |
4595 | |
4596 | protected: |
4597 | TagType(TypeClass TC, const TagDecl *D, QualType can); |
4598 | |
4599 | public: |
4600 | TagDecl *getDecl() const; |
4601 | |
4602 | |
4603 | bool isBeingDefined() const; |
4604 | |
4605 | static bool classof(const Type *T) { |
4606 | return T->getTypeClass() == Enum || T->getTypeClass() == Record; |
4607 | } |
4608 | }; |
4609 | |
4610 | |
4611 | |
4612 | class RecordType : public TagType { |
4613 | protected: |
4614 | friend class ASTContext; |
4615 | |
4616 | explicit RecordType(const RecordDecl *D) |
4617 | : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4618 | explicit RecordType(TypeClass TC, RecordDecl *D) |
4619 | : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4620 | |
4621 | public: |
4622 | RecordDecl *getDecl() const { |
4623 | return reinterpret_cast<RecordDecl*>(TagType::getDecl()); |
4624 | } |
4625 | |
4626 | |
4627 | |
4628 | bool hasConstFields() const; |
4629 | |
4630 | bool isSugared() const { return false; } |
4631 | QualType desugar() const { return QualType(this, 0); } |
4632 | |
4633 | static bool classof(const Type *T) { return T->getTypeClass() == Record; } |
4634 | }; |
4635 | |
4636 | |
4637 | |
4638 | class EnumType : public TagType { |
4639 | friend class ASTContext; |
4640 | |
4641 | explicit EnumType(const EnumDecl *D) |
4642 | : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) {} |
4643 | |
4644 | public: |
4645 | EnumDecl *getDecl() const { |
4646 | return reinterpret_cast<EnumDecl*>(TagType::getDecl()); |
4647 | } |
4648 | |
4649 | bool isSugared() const { return false; } |
4650 | QualType desugar() const { return QualType(this, 0); } |
4651 | |
4652 | static bool classof(const Type *T) { return T->getTypeClass() == Enum; } |
4653 | }; |
4654 | |
4655 | |
4656 | |
4657 | |
4658 | |
4659 | |
4660 | |
4661 | |
4662 | |
4663 | |
4664 | |
4665 | |
4666 | |
4667 | class AttributedType : public Type, public llvm::FoldingSetNode { |
4668 | public: |
4669 | using Kind = attr::Kind; |
4670 | |
4671 | private: |
4672 | friend class ASTContext; |
4673 | |
4674 | QualType ModifiedType; |
4675 | QualType EquivalentType; |
4676 | |
4677 | AttributedType(QualType canon, attr::Kind attrKind, QualType modified, |
4678 | QualType equivalent) |
4679 | : Type(Attributed, canon, equivalent->getDependence()), |
4680 | ModifiedType(modified), EquivalentType(equivalent) { |
4681 | AttributedTypeBits.AttrKind = attrKind; |
4682 | } |
4683 | |
4684 | public: |
4685 | Kind getAttrKind() const { |
4686 | return static_cast<Kind>(AttributedTypeBits.AttrKind); |
4687 | } |
4688 | |
4689 | QualType getModifiedType() const { return ModifiedType; } |
4690 | QualType getEquivalentType() const { return EquivalentType; } |
4691 | |
4692 | bool isSugared() const { return true; } |
4693 | QualType desugar() const { return getEquivalentType(); } |
4694 | |
4695 | |
4696 | |
4697 | |
4698 | |
4699 | |
4700 | |
4701 | |
4702 | |
4703 | |
4704 | |
4705 | |
4706 | |
4707 | |
4708 | |
4709 | |
4710 | bool isQualifier() const; |
4711 | |
4712 | bool isMSTypeSpec() const; |
4713 | |
4714 | bool isCallingConv() const; |
4715 | |
4716 | llvm::Optional<NullabilityKind> getImmediateNullability() const; |
4717 | |
4718 | |
4719 | |
4720 | static Kind getNullabilityAttrKind(NullabilityKind kind) { |
4721 | switch (kind) { |
4722 | case NullabilityKind::NonNull: |
4723 | return attr::TypeNonNull; |
4724 | |
4725 | case NullabilityKind::Nullable: |
4726 | return attr::TypeNullable; |
4727 | |
4728 | case NullabilityKind::NullableResult: |
4729 | return attr::TypeNullableResult; |
4730 | |
4731 | case NullabilityKind::Unspecified: |
4732 | return attr::TypeNullUnspecified; |
4733 | } |
4734 | llvm_unreachable("Unknown nullability kind."); |
4735 | } |
4736 | |
4737 | |
4738 | |
4739 | |
4740 | |
4741 | |
4742 | |
4743 | |
4744 | |
4745 | |
4746 | static Optional<NullabilityKind> stripOuterNullability(QualType &T); |
4747 | |
4748 | void Profile(llvm::FoldingSetNodeID &ID) { |
4749 | Profile(ID, getAttrKind(), ModifiedType, EquivalentType); |
4750 | } |
4751 | |
4752 | static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind, |
4753 | QualType modified, QualType equivalent) { |
4754 | ID.AddInteger(attrKind); |
4755 | ID.AddPointer(modified.getAsOpaquePtr()); |
4756 | ID.AddPointer(equivalent.getAsOpaquePtr()); |
4757 | } |
4758 | |
4759 | static bool classof(const Type *T) { |
4760 | return T->getTypeClass() == Attributed; |
4761 | } |
4762 | }; |
4763 | |
4764 | class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4765 | friend class ASTContext; |
4766 | |
4767 | |
4768 | struct CanonicalTTPTInfo { |
4769 | unsigned Depth : 15; |
4770 | unsigned ParameterPack : 1; |
4771 | unsigned Index : 16; |
4772 | }; |
4773 | |
4774 | union { |
4775 | |
4776 | CanonicalTTPTInfo CanTTPTInfo; |
4777 | |
4778 | |
4779 | TemplateTypeParmDecl *TTPDecl; |
4780 | }; |
4781 | |
4782 | |
4783 | TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon) |
4784 | : Type(TemplateTypeParm, Canon, |
4785 | TypeDependence::DependentInstantiation | |
4786 | (Canon->getDependence() & TypeDependence::UnexpandedPack)), |
4787 | TTPDecl(TTPDecl) {} |
4788 | |
4789 | |
4790 | TemplateTypeParmType(unsigned D, unsigned I, bool PP) |
4791 | : Type(TemplateTypeParm, QualType(this, 0), |
4792 | TypeDependence::DependentInstantiation | |
4793 | (PP ? TypeDependence::UnexpandedPack : TypeDependence::None)) { |
4794 | CanTTPTInfo.Depth = D; |
4795 | CanTTPTInfo.Index = I; |
4796 | CanTTPTInfo.ParameterPack = PP; |
4797 | } |
4798 | |
4799 | const CanonicalTTPTInfo& getCanTTPTInfo() const { |
4800 | QualType Can = getCanonicalTypeInternal(); |
4801 | return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo; |
4802 | } |
4803 | |
4804 | public: |
4805 | unsigned getDepth() const { return getCanTTPTInfo().Depth; } |
4806 | unsigned getIndex() const { return getCanTTPTInfo().Index; } |
4807 | bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; } |
4808 | |
4809 | TemplateTypeParmDecl *getDecl() const { |
4810 | return isCanonicalUnqualified() ? nullptr : TTPDecl; |
4811 | } |
4812 | |
4813 | IdentifierInfo *getIdentifier() const; |
4814 | |
4815 | bool isSugared() const { return false; } |
4816 | QualType desugar() const { return QualType(this, 0); } |
4817 | |
4818 | void Profile(llvm::FoldingSetNodeID &ID) { |
4819 | Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl()); |
4820 | } |
4821 | |
4822 | static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, |
4823 | unsigned Index, bool ParameterPack, |
4824 | TemplateTypeParmDecl *TTPDecl) { |
4825 | ID.AddInteger(Depth); |
4826 | ID.AddInteger(Index); |
4827 | ID.AddBoolean(ParameterPack); |
4828 | ID.AddPointer(TTPDecl); |
4829 | } |
4830 | |
4831 | static bool classof(const Type *T) { |
4832 | return T->getTypeClass() == TemplateTypeParm; |
4833 | } |
4834 | }; |
4835 | |
4836 | |
4837 | |
4838 | |
4839 | |
4840 | |
4841 | |
4842 | |
4843 | class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { |
4844 | friend class ASTContext; |
4845 | |
4846 | |
4847 | const TemplateTypeParmType *Replaced; |
4848 | |
4849 | SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) |
4850 | : Type(SubstTemplateTypeParm, Canon, Canon->getDependence()), |
4851 | Replaced(Param) {} |
4852 | |
4853 | public: |
4854 | |
4855 | const TemplateTypeParmType *getReplacedParameter() const { |
4856 | return Replaced; |
4857 | } |
4858 | |
4859 | |
4860 | |
4861 | QualType getReplacementType() const { |
4862 | return getCanonicalTypeInternal(); |
4863 | } |
4864 | |
4865 | bool isSugared() const { return true; } |
4866 | QualType desugar() const { return getReplacementType(); } |
4867 | |
4868 | void Profile(llvm::FoldingSetNodeID &ID) { |
4869 | Profile(ID, getReplacedParameter(), getReplacementType()); |
4870 | } |
4871 | |
4872 | static void Profile(llvm::FoldingSetNodeID &ID, |
4873 | const TemplateTypeParmType *Replaced, |
4874 | QualType Replacement) { |
4875 | ID.AddPointer(Replaced); |
4876 | ID.AddPointer(Replacement.getAsOpaquePtr()); |
4877 | } |
4878 | |
4879 | static bool classof(const Type *T) { |
4880 | return T->getTypeClass() == SubstTemplateTypeParm; |
4881 | } |
4882 | }; |
4883 | |
4884 | |
4885 | |
4886 | |
4887 | |
4888 | |
4889 | |
4890 | |
4891 | |
4892 | |
4893 | |
4894 | |
4895 | |
4896 | class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode { |
4897 | friend class ASTContext; |
4898 | |
4899 | |
4900 | const TemplateTypeParmType *Replaced; |
4901 | |
4902 | |
4903 | |
4904 | const TemplateArgument *Arguments; |
4905 | |
4906 | SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param, |
4907 | QualType Canon, |
4908 | const TemplateArgument &ArgPack); |
4909 | |
4910 | public: |
4911 | IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); } |
4912 | |
4913 | |
4914 | const TemplateTypeParmType *getReplacedParameter() const { |
4915 | return Replaced; |
4916 | } |
4917 | |
4918 | unsigned getNumArgs() const { |
4919 | return SubstTemplateTypeParmPackTypeBits.NumArgs; |
4920 | } |
4921 | |
4922 | bool isSugared() const { return false; } |
4923 | QualType desugar() const { return QualType(this, 0); } |
4924 | |
4925 | TemplateArgument getArgumentPack() const; |
4926 | |
4927 | void Profile(llvm::FoldingSetNodeID &ID); |
4928 | static void Profile(llvm::FoldingSetNodeID &ID, |
4929 | const TemplateTypeParmType *Replaced, |
4930 | const TemplateArgument &ArgPack); |
4931 | |
4932 | static bool classof(const Type *T) { |
4933 | return T->getTypeClass() == SubstTemplateTypeParmPack; |
4934 | } |
4935 | }; |
4936 | |
4937 | |
4938 | |
4939 | |
4940 | |
4941 | |
4942 | |
4943 | |
4944 | |
4945 | class DeducedType : public Type { |
4946 | protected: |
4947 | DeducedType(TypeClass TC, QualType DeducedAsType, |
4948 | TypeDependence ExtraDependence) |
4949 | : Type(TC, |
4950 | |
4951 | DeducedAsType.isNull() ? QualType(this, 0) |
4952 | : DeducedAsType.getCanonicalType(), |
4953 | ExtraDependence | (DeducedAsType.isNull() |
4954 | ? TypeDependence::None |
4955 | : DeducedAsType->getDependence() & |
4956 | ~TypeDependence::VariablyModified)) {} |
4957 | |
4958 | public: |
4959 | bool isSugared() const { return !isCanonicalUnqualified(); } |
4960 | QualType desugar() const { return getCanonicalTypeInternal(); } |
4961 | |
4962 | |
4963 | |
4964 | QualType getDeducedType() const { |
4965 | return !isCanonicalUnqualified() ? getCanonicalTypeInternal() : QualType(); |
4966 | } |
4967 | bool isDeduced() const { |
4968 | return !isCanonicalUnqualified() || isDependentType(); |
4969 | } |
4970 | |
4971 | static bool classof(const Type *T) { |
4972 | return T->getTypeClass() == Auto || |
4973 | T->getTypeClass() == DeducedTemplateSpecialization; |
4974 | } |
4975 | }; |
4976 | |
4977 | |
4978 | |
4979 | class alignas(8) AutoType : public DeducedType, public llvm::FoldingSetNode { |
4980 | friend class ASTContext; |
4981 | |
4982 | ConceptDecl *TypeConstraintConcept; |
4983 | |
4984 | AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword, |
4985 | TypeDependence ExtraDependence, ConceptDecl *CD, |
4986 | ArrayRef<TemplateArgument> TypeConstraintArgs); |
4987 | |
4988 | const TemplateArgument *getArgBuffer() const { |
4989 | return reinterpret_cast<const TemplateArgument*>(this+1); |
4990 | } |
4991 | |
4992 | TemplateArgument *getArgBuffer() { |
4993 | return reinterpret_cast<TemplateArgument*>(this+1); |
4994 | } |
4995 | |
4996 | public: |
4997 | |
4998 | const TemplateArgument *getArgs() const { |
4999 | return getArgBuffer(); |
5000 | } |
5001 | |
5002 | |
5003 | unsigned getNumArgs() const { |
5004 | return AutoTypeBits.NumArgs; |
5005 | } |
5006 | |
5007 | const TemplateArgument &getArg(unsigned Idx) const; |
5008 | |
5009 | ArrayRef<TemplateArgument> getTypeConstraintArguments() const { |
5010 | return {getArgs(), getNumArgs()}; |
5011 | } |
5012 | |
5013 | ConceptDecl *getTypeConstraintConcept() const { |
5014 | return TypeConstraintConcept; |
5015 | } |
5016 | |
5017 | bool isConstrained() const { |
5018 | return TypeConstraintConcept != nullptr; |
5019 | } |
5020 | |
5021 | bool isDecltypeAuto() const { |
5022 | return getKeyword() == AutoTypeKeyword::DecltypeAuto; |
5023 | } |
5024 | |
5025 | AutoTypeKeyword getKeyword() const { |
5026 | return (AutoTypeKeyword)AutoTypeBits.Keyword; |
5027 | } |
5028 | |
5029 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5030 | Profile(ID, Context, getDeducedType(), getKeyword(), isDependentType(), |
5031 | getTypeConstraintConcept(), getTypeConstraintArguments()); |
5032 | } |
5033 | |
5034 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
5035 | QualType Deduced, AutoTypeKeyword Keyword, |
5036 | bool IsDependent, ConceptDecl *CD, |
5037 | ArrayRef<TemplateArgument> Arguments); |
5038 | |
5039 | static bool classof(const Type *T) { |
5040 | return T->getTypeClass() == Auto; |
5041 | } |
5042 | }; |
5043 | |
5044 | |
5045 | class DeducedTemplateSpecializationType : public DeducedType, |
5046 | public llvm::FoldingSetNode { |
5047 | friend class ASTContext; |
5048 | |
5049 | |
5050 | TemplateName Template; |
5051 | |
5052 | DeducedTemplateSpecializationType(TemplateName Template, |
5053 | QualType DeducedAsType, |
5054 | bool IsDeducedAsDependent) |
5055 | : DeducedType(DeducedTemplateSpecialization, DeducedAsType, |
5056 | toTypeDependence(Template.getDependence()) | |
5057 | (IsDeducedAsDependent |
5058 | ? TypeDependence::DependentInstantiation |
5059 | : TypeDependence::None)), |
5060 | Template(Template) {} |
5061 | |
5062 | public: |
5063 | |
5064 | TemplateName getTemplateName() const { return Template;} |
5065 | |
5066 | void Profile(llvm::FoldingSetNodeID &ID) { |
5067 | Profile(ID, getTemplateName(), getDeducedType(), isDependentType()); |
5068 | } |
5069 | |
5070 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Template, |
5071 | QualType Deduced, bool IsDependent) { |
5072 | Template.Profile(ID); |
5073 | ID.AddPointer(Deduced.getAsOpaquePtr()); |
5074 | ID.AddBoolean(IsDependent); |
5075 | } |
5076 | |
5077 | static bool classof(const Type *T) { |
5078 | return T->getTypeClass() == DeducedTemplateSpecialization; |
5079 | } |
5080 | }; |
5081 | |
5082 | |
5083 | |
5084 | |
5085 | |
5086 | |
5087 | |
5088 | |
5089 | |
5090 | |
5091 | |
5092 | |
5093 | |
5094 | |
5095 | |
5096 | |
5097 | |
5098 | |
5099 | |
5100 | |
5101 | |
5102 | class alignas(8) TemplateSpecializationType |
5103 | : public Type, |
5104 | public llvm::FoldingSetNode { |
5105 | friend class ASTContext; |
5106 | |
5107 | |
5108 | |
5109 | |
5110 | |
5111 | |
5112 | |
5113 | |
5114 | TemplateName Template; |
5115 | |
5116 | TemplateSpecializationType(TemplateName T, |
5117 | ArrayRef<TemplateArgument> Args, |
5118 | QualType Canon, |
5119 | QualType Aliased); |
5120 | |
5121 | public: |
5122 | |
5123 | |
5124 | |
5125 | |
5126 | |
5127 | |
5128 | |
5129 | |
5130 | |
5131 | |
5132 | |
5133 | static bool |
5134 | anyDependentTemplateArguments(ArrayRef<TemplateArgumentLoc> Args, |
5135 | ArrayRef<TemplateArgument> Converted); |
5136 | static bool |
5137 | anyDependentTemplateArguments(const TemplateArgumentListInfo &, |
5138 | ArrayRef<TemplateArgument> Converted); |
5139 | static bool anyInstantiationDependentTemplateArguments( |
5140 | ArrayRef<TemplateArgumentLoc> Args); |
5141 | |
5142 | |
5143 | |
5144 | bool isCurrentInstantiation() const { |
5145 | return isa<InjectedClassNameType>(getCanonicalTypeInternal()); |
5146 | } |
5147 | |
5148 | |
5149 | |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | |
5155 | |
5156 | |
5157 | |
5158 | |
5159 | |
5160 | |
5161 | |
5162 | |
5163 | bool isTypeAlias() const { return TemplateSpecializationTypeBits.TypeAlias; } |
5164 | |
5165 | |
5166 | |
5167 | QualType getAliasedType() const { |
5168 | assert(isTypeAlias() && "not a type alias template specialization"); |
5169 | return *reinterpret_cast<const QualType*>(end()); |
5170 | } |
5171 | |
5172 | using iterator = const TemplateArgument *; |
5173 | |
5174 | iterator begin() const { return getArgs(); } |
5175 | iterator end() const; |
5176 | |
5177 | |
5178 | TemplateName getTemplateName() const { return Template; } |
5179 | |
5180 | |
5181 | const TemplateArgument *getArgs() const { |
5182 | return reinterpret_cast<const TemplateArgument *>(this + 1); |
5183 | } |
5184 | |
5185 | |
5186 | unsigned getNumArgs() const { |
5187 | return TemplateSpecializationTypeBits.NumArgs; |
5188 | } |
5189 | |
5190 | |
5191 | |
5192 | const TemplateArgument &getArg(unsigned Idx) const; |
5193 | |
5194 | ArrayRef<TemplateArgument> template_arguments() const { |
5195 | return {getArgs(), getNumArgs()}; |
5196 | } |
5197 | |
5198 | bool isSugared() const { |
5199 | return !isDependentType() || isCurrentInstantiation() || isTypeAlias(); |
5200 | } |
5201 | |
5202 | QualType desugar() const { |
5203 | return isTypeAlias() ? getAliasedType() : getCanonicalTypeInternal(); |
5204 | } |
5205 | |
5206 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) { |
5207 | Profile(ID, Template, template_arguments(), Ctx); |
5208 | if (isTypeAlias()) |
5209 | getAliasedType().Profile(ID); |
5210 | } |
5211 | |
5212 | static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, |
5213 | ArrayRef<TemplateArgument> Args, |
5214 | const ASTContext &Context); |
5215 | |
5216 | static bool classof(const Type *T) { |
5217 | return T->getTypeClass() == TemplateSpecialization; |
5218 | } |
5219 | }; |
5220 | |
5221 | |
5222 | |
5223 | void printTemplateArgumentList(raw_ostream &OS, |
5224 | ArrayRef<TemplateArgument> Args, |
5225 | const PrintingPolicy &Policy, |
5226 | const TemplateParameterList *TPL = nullptr); |
5227 | |
5228 | void printTemplateArgumentList(raw_ostream &OS, |
5229 | ArrayRef<TemplateArgumentLoc> Args, |
5230 | const PrintingPolicy &Policy, |
5231 | const TemplateParameterList *TPL = nullptr); |
5232 | |
5233 | void printTemplateArgumentList(raw_ostream &OS, |
5234 | const TemplateArgumentListInfo &Args, |
5235 | const PrintingPolicy &Policy, |
5236 | const TemplateParameterList *TPL = nullptr); |
5237 | |
5238 | |
5239 | |
5240 | |
5241 | |
5242 | |
5243 | |
5244 | |
5245 | |
5246 | |
5247 | |
5248 | |
5249 | |
5250 | |
5251 | |
5252 | |
5253 | |
5254 | |
5255 | class InjectedClassNameType : public Type { |
5256 | friend class ASTContext; |
5257 | friend class ASTNodeImporter; |
5258 | friend class ASTReader; |
5259 | |
5260 | |
5261 | template <class T> friend class serialization::AbstractTypeReader; |
5262 | |
5263 | CXXRecordDecl *Decl; |
5264 | |
5265 | |
5266 | |
5267 | |
5268 | |
5269 | |
5270 | |
5271 | |
5272 | |
5273 | |
5274 | QualType InjectedType; |
5275 | |
5276 | InjectedClassNameType(CXXRecordDecl *D, QualType TST) |
5277 | : Type(InjectedClassName, QualType(), |
5278 | TypeDependence::DependentInstantiation), |
5279 | Decl(D), InjectedType(TST) { |
5280 | assert(isa<TemplateSpecializationType>(TST)); |
5281 | assert(!TST.hasQualifiers()); |
5282 | assert(TST->isDependentType()); |
5283 | } |
5284 | |
5285 | public: |
5286 | QualType getInjectedSpecializationType() const { return InjectedType; } |
5287 | |
5288 | const TemplateSpecializationType *getInjectedTST() const { |
5289 | return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); |
5290 | } |
5291 | |
5292 | TemplateName getTemplateName() const { |
5293 | return getInjectedTST()->getTemplateName(); |
5294 | } |
5295 | |
5296 | CXXRecordDecl *getDecl() const; |
5297 | |
5298 | bool isSugared() const { return false; } |
5299 | QualType desugar() const { return QualType(this, 0); } |
5300 | |
5301 | static bool classof(const Type *T) { |
5302 | return T->getTypeClass() == InjectedClassName; |
5303 | } |
5304 | }; |
5305 | |
5306 | |
5307 | enum TagTypeKind { |
5308 | |
5309 | TTK_Struct, |
5310 | |
5311 | |
5312 | TTK_Interface, |
5313 | |
5314 | |
5315 | TTK_Union, |
5316 | |
5317 | |
5318 | TTK_Class, |
5319 | |
5320 | |
5321 | TTK_Enum |
5322 | }; |
5323 | |
5324 | |
5325 | |
5326 | enum ElaboratedTypeKeyword { |
5327 | |
5328 | ETK_Struct, |
5329 | |
5330 | |
5331 | ETK_Interface, |
5332 | |
5333 | |
5334 | ETK_Union, |
5335 | |
5336 | |
5337 | ETK_Class, |
5338 | |
5339 | |
5340 | ETK_Enum, |
5341 | |
5342 | |
5343 | |
5344 | ETK_Typename, |
5345 | |
5346 | |
5347 | ETK_None |
5348 | }; |
5349 | |
5350 | |
5351 | |
5352 | |
5353 | |
5354 | class TypeWithKeyword : public Type { |
5355 | protected: |
5356 | TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, |
5357 | QualType Canonical, TypeDependence Dependence) |
5358 | : Type(tc, Canonical, Dependence) { |
5359 | TypeWithKeywordBits.Keyword = Keyword; |
5360 | } |
5361 | |
5362 | public: |
5363 | ElaboratedTypeKeyword getKeyword() const { |
5364 | return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); |
5365 | } |
5366 | |
5367 | |
5368 | static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); |
5369 | |
5370 | |
5371 | |
5372 | static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); |
5373 | |
5374 | |
5375 | static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); |
5376 | |
5377 | |
5378 | |
5379 | |
5380 | static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); |
5381 | |
5382 | static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); |
5383 | |
5384 | static StringRef getKeywordName(ElaboratedTypeKeyword Keyword); |
5385 | |
5386 | static StringRef getTagTypeKindName(TagTypeKind Kind) { |
5387 | return getKeywordName(getKeywordForTagTypeKind(Kind)); |
5388 | } |
5389 | |
5390 | class CannotCastToThisType {}; |
5391 | static CannotCastToThisType classof(const Type *); |
5392 | }; |
5393 | |
5394 | |
5395 | |
5396 | |
5397 | |
5398 | |
5399 | |
5400 | |
5401 | |
5402 | class ElaboratedType final |
5403 | : public TypeWithKeyword, |
5404 | public llvm::FoldingSetNode, |
5405 | private llvm::TrailingObjects<ElaboratedType, TagDecl *> { |
5406 | friend class ASTContext; |
5407 | friend TrailingObjects; |
5408 | |
5409 | |
5410 | NestedNameSpecifier *NNS; |
5411 | |
5412 | |
5413 | QualType NamedType; |
5414 | |
5415 | |
5416 | |
5417 | |
5418 | |
5419 | ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5420 | QualType NamedType, QualType CanonType, TagDecl *OwnedTagDecl) |
5421 | : TypeWithKeyword(Keyword, Elaborated, CanonType, |
5422 | |
5423 | |
5424 | |
5425 | |
5426 | NamedType->getDependence() | |
5427 | (NNS ? toSyntacticDependence( |
5428 | toTypeDependence(NNS->getDependence())) |
5429 | : TypeDependence::None)), |
5430 | NNS(NNS), NamedType(NamedType) { |
5431 | ElaboratedTypeBits.HasOwnedTagDecl = false; |
5432 | if (OwnedTagDecl) { |
5433 | ElaboratedTypeBits.HasOwnedTagDecl = true; |
5434 | *getTrailingObjects<TagDecl *>() = OwnedTagDecl; |
5435 | } |
5436 | assert(!(Keyword == ETK_None && NNS == nullptr) && |
5437 | "ElaboratedType cannot have elaborated type keyword " |
5438 | "and name qualifier both null."); |
5439 | } |
5440 | |
5441 | public: |
5442 | |
5443 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5444 | |
5445 | |
5446 | QualType getNamedType() const { return NamedType; } |
5447 | |
5448 | |
5449 | QualType desugar() const { return getNamedType(); } |
5450 | |
5451 | |
5452 | bool isSugared() const { return true; } |
5453 | |
5454 | |
5455 | |
5456 | TagDecl *getOwnedTagDecl() const { |
5457 | return ElaboratedTypeBits.HasOwnedTagDecl ? *getTrailingObjects<TagDecl *>() |
5458 | : nullptr; |
5459 | } |
5460 | |
5461 | void Profile(llvm::FoldingSetNodeID &ID) { |
5462 | Profile(ID, getKeyword(), NNS, NamedType, getOwnedTagDecl()); |
5463 | } |
5464 | |
5465 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5466 | NestedNameSpecifier *NNS, QualType NamedType, |
5467 | TagDecl *OwnedTagDecl) { |
5468 | ID.AddInteger(Keyword); |
5469 | ID.AddPointer(NNS); |
5470 | NamedType.Profile(ID); |
5471 | ID.AddPointer(OwnedTagDecl); |
5472 | } |
5473 | |
5474 | static bool classof(const Type *T) { return T->getTypeClass() == Elaborated; } |
5475 | }; |
5476 | |
5477 | |
5478 | |
5479 | |
5480 | |
5481 | |
5482 | |
5483 | |
5484 | |
5485 | |
5486 | |
5487 | |
5488 | |
5489 | class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { |
5490 | friend class ASTContext; |
5491 | |
5492 | |
5493 | NestedNameSpecifier *NNS; |
5494 | |
5495 | |
5496 | const IdentifierInfo *Name; |
5497 | |
5498 | DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, |
5499 | const IdentifierInfo *Name, QualType CanonType) |
5500 | : TypeWithKeyword(Keyword, DependentName, CanonType, |
5501 | TypeDependence::DependentInstantiation | |
5502 | toTypeDependence(NNS->getDependence())), |
5503 | NNS(NNS), Name(Name) {} |
5504 | |
5505 | public: |
5506 | |
5507 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5508 | |
5509 | |
5510 | |
5511 | |
5512 | |
5513 | |
5514 | const IdentifierInfo *getIdentifier() const { |
5515 | return Name; |
5516 | } |
5517 | |
5518 | bool isSugared() const { return false; } |
5519 | QualType desugar() const { return QualType(this, 0); } |
5520 | |
5521 | void Profile(llvm::FoldingSetNodeID &ID) { |
5522 | Profile(ID, getKeyword(), NNS, Name); |
5523 | } |
5524 | |
5525 | static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, |
5526 | NestedNameSpecifier *NNS, const IdentifierInfo *Name) { |
5527 | ID.AddInteger(Keyword); |
5528 | ID.AddPointer(NNS); |
5529 | ID.AddPointer(Name); |
5530 | } |
5531 | |
5532 | static bool classof(const Type *T) { |
5533 | return T->getTypeClass() == DependentName; |
5534 | } |
5535 | }; |
5536 | |
5537 | |
5538 | |
5539 | |
5540 | class alignas(8) DependentTemplateSpecializationType |
5541 | : public TypeWithKeyword, |
5542 | public llvm::FoldingSetNode { |
5543 | friend class ASTContext; |
5544 | |
5545 | |
5546 | NestedNameSpecifier *NNS; |
5547 | |
5548 | |
5549 | const IdentifierInfo *Name; |
5550 | |
5551 | DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, |
5552 | NestedNameSpecifier *NNS, |
5553 | const IdentifierInfo *Name, |
5554 | ArrayRef<TemplateArgument> Args, |
5555 | QualType Canon); |
5556 | |
5557 | const TemplateArgument *getArgBuffer() const { |
5558 | return reinterpret_cast<const TemplateArgument*>(this+1); |
5559 | } |
5560 | |
5561 | TemplateArgument *getArgBuffer() { |
5562 | return reinterpret_cast<TemplateArgument*>(this+1); |
5563 | } |
5564 | |
5565 | public: |
5566 | NestedNameSpecifier *getQualifier() const { return NNS; } |
5567 | const IdentifierInfo *getIdentifier() const { return Name; } |
5568 | |
5569 | |
5570 | const TemplateArgument *getArgs() const { |
5571 | return getArgBuffer(); |
5572 | } |
5573 | |
5574 | |
5575 | unsigned getNumArgs() const { |
5576 | return DependentTemplateSpecializationTypeBits.NumArgs; |
5577 | } |
5578 | |
5579 | const TemplateArgument &getArg(unsigned Idx) const; |
5580 | |
5581 | ArrayRef<TemplateArgument> template_arguments() const { |
5582 | return {getArgs(), getNumArgs()}; |
5583 | } |
5584 | |
5585 | using iterator = const TemplateArgument *; |
5586 | |
5587 | iterator begin() const { return getArgs(); } |
5588 | iterator end() const; |
5589 | |
5590 | bool isSugared() const { return false; } |
5591 | QualType desugar() const { return QualType(this, 0); } |
5592 | |
5593 | void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) { |
5594 | Profile(ID, Context, getKeyword(), NNS, Name, {getArgs(), getNumArgs()}); |
5595 | } |
5596 | |
5597 | static void Profile(llvm::FoldingSetNodeID &ID, |
5598 | const ASTContext &Context, |
5599 | ElaboratedTypeKeyword Keyword, |
5600 | NestedNameSpecifier *Qualifier, |
5601 | const IdentifierInfo *Name, |
5602 | ArrayRef<TemplateArgument> Args); |
5603 | |
5604 | static bool classof(const Type *T) { |
5605 | return T->getTypeClass() == DependentTemplateSpecialization; |
5606 | } |
5607 | }; |
5608 | |
5609 | |
5610 | |
5611 | |
5612 | |
5613 | |
5614 | |
5615 | |
5616 | |
5617 | |
5618 | |
5619 | |
5620 | |
5621 | |
5622 | |
5623 | |
5624 | |
5625 | |
5626 | |
5627 | |
5628 | |
5629 | |
5630 | |
5631 | class PackExpansionType : public Type, public llvm::FoldingSetNode { |
5632 | friend class ASTContext; |
5633 | |
5634 | |
5635 | QualType Pattern; |
5636 | |
5637 | PackExpansionType(QualType Pattern, QualType Canon, |
5638 | Optional<unsigned> NumExpansions) |
5639 | : Type(PackExpansion, Canon, |
5640 | (Pattern->getDependence() | TypeDependence::Dependent | |
5641 | TypeDependence::Instantiation) & |
5642 | ~TypeDependence::UnexpandedPack), |
5643 | Pattern(Pattern) { |
5644 | PackExpansionTypeBits.NumExpansions = |
5645 | NumExpansions ? *NumExpansions + 1 : 0; |
5646 | } |
5647 | |
5648 | public: |
5649 | |
5650 | |
5651 | |
5652 | QualType getPattern() const { return Pattern; } |
5653 | |
5654 | |
5655 | |
5656 | Optional<unsigned> getNumExpansions() const { |
5657 | if (PackExpansionTypeBits.NumExpansions) |
5658 | return PackExpansionTypeBits.NumExpansions - 1; |
5659 | return None; |
5660 | } |
5661 | |
5662 | bool isSugared() const { return false; } |
5663 | QualType desugar() const { return QualType(this, 0); } |
5664 | |
5665 | void Profile(llvm::FoldingSetNodeID &ID) { |
5666 | Profile(ID, getPattern(), getNumExpansions()); |
5667 | } |
5668 | |
5669 | static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern, |
5670 | Optional<unsigned> NumExpansions) { |
5671 | ID.AddPointer(Pattern.getAsOpaquePtr()); |
5672 | ID.AddBoolean(NumExpansions.hasValue()); |
5673 | if (NumExpansions) |
5674 | ID.AddInteger(*NumExpansions); |
5675 | } |
5676 | |
5677 | static bool classof(const Type *T) { |
5678 | return T->getTypeClass() == PackExpansion; |
5679 | } |
5680 | }; |
5681 | |
5682 | |
5683 | |
5684 | template <class T> |
5685 | class ObjCProtocolQualifiers { |
5686 | protected: |
5687 | ObjCProtocolQualifiers() = default; |
5688 | |
5689 | ObjCProtocolDecl * const *getProtocolStorage() const { |
5690 | return const_cast<ObjCProtocolQualifiers*>(this)->getProtocolStorage(); |
5691 | } |
5692 | |
5693 | ObjCProtocolDecl **getProtocolStorage() { |
5694 | return static_cast<T*>(this)->getProtocolStorageImpl(); |
5695 | } |
5696 | |
5697 | void setNumProtocols(unsigned N) { |
5698 | static_cast<T*>(this)->setNumProtocolsImpl(N); |
5699 | } |
5700 | |
5701 | void initialize(ArrayRef<ObjCProtocolDecl *> protocols) { |
5702 | setNumProtocols(protocols.size()); |
5703 | assert(getNumProtocols() == protocols.size() && |
5704 | "bitfield overflow in protocol count"); |
5705 | if (!protocols.empty()) |
5706 | memcpy(getProtocolStorage(), protocols.data(), |
5707 | protocols.size() * sizeof(ObjCProtocolDecl*)); |
5708 | } |
5709 | |
5710 | public: |
5711 | using qual_iterator = ObjCProtocolDecl * const *; |
5712 | using qual_range = llvm::iterator_range<qual_iterator>; |
5713 | |
5714 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
5715 | qual_iterator qual_begin() const { return getProtocolStorage(); } |
5716 | qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } |
5717 | |
5718 | bool qual_empty() const { return getNumProtocols() == 0; } |
5719 | |
5720 | |
5721 | |
5722 | unsigned getNumProtocols() const { |
5723 | return static_cast<const T*>(this)->getNumProtocolsImpl(); |
5724 | } |
5725 | |
5726 | |
5727 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
5728 | assert(I < getNumProtocols() && "Out-of-range protocol access"); |
5729 | return qual_begin()[I]; |
5730 | } |
5731 | |
5732 | |
5733 | ArrayRef<ObjCProtocolDecl *> getProtocols() const { |
5734 | return ArrayRef<ObjCProtocolDecl *>(qual_begin(), getNumProtocols()); |
5735 | } |
5736 | }; |
5737 | |
5738 | |
5739 | |
5740 | class ObjCTypeParamType : public Type, |
5741 | public ObjCProtocolQualifiers<ObjCTypeParamType>, |
5742 | public llvm::FoldingSetNode { |
5743 | friend class ASTContext; |
5744 | friend class ObjCProtocolQualifiers<ObjCTypeParamType>; |
5745 | |
5746 | |
5747 | unsigned NumProtocols : 6; |
5748 | |
5749 | ObjCTypeParamDecl *OTPDecl; |
5750 | |
5751 | |
5752 | |
5753 | |
5754 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5755 | |
5756 | |
5757 | |
5758 | unsigned getNumProtocolsImpl() const { |
5759 | return NumProtocols; |
5760 | } |
5761 | |
5762 | void setNumProtocolsImpl(unsigned N) { |
5763 | NumProtocols = N; |
5764 | } |
5765 | |
5766 | ObjCTypeParamType(const ObjCTypeParamDecl *D, |
5767 | QualType can, |
5768 | ArrayRef<ObjCProtocolDecl *> protocols); |
5769 | |
5770 | public: |
5771 | bool isSugared() const { return true; } |
5772 | QualType desugar() const { return getCanonicalTypeInternal(); } |
5773 | |
5774 | static bool classof(const Type *T) { |
5775 | return T->getTypeClass() == ObjCTypeParam; |
5776 | } |
5777 | |
5778 | void Profile(llvm::FoldingSetNodeID &ID); |
5779 | static void Profile(llvm::FoldingSetNodeID &ID, |
5780 | const ObjCTypeParamDecl *OTPDecl, |
5781 | QualType CanonicalType, |
5782 | ArrayRef<ObjCProtocolDecl *> protocols); |
5783 | |
5784 | ObjCTypeParamDecl *getDecl() const { return OTPDecl; } |
5785 | }; |
5786 | |
5787 | |
5788 | |
5789 | |
5790 | |
5791 | |
5792 | |
5793 | |
5794 | |
5795 | |
5796 | |
5797 | |
5798 | |
5799 | |
5800 | |
5801 | |
5802 | |
5803 | |
5804 | |
5805 | |
5806 | |
5807 | |
5808 | |
5809 | |
5810 | |
5811 | |
5812 | |
5813 | |
5814 | |
5815 | class ObjCObjectType : public Type, |
5816 | public ObjCProtocolQualifiers<ObjCObjectType> { |
5817 | friend class ObjCProtocolQualifiers<ObjCObjectType>; |
5818 | |
5819 | |
5820 | |
5821 | |
5822 | |
5823 | |
5824 | |
5825 | |
5826 | |
5827 | |
5828 | |
5829 | |
5830 | |
5831 | |
5832 | QualType BaseType; |
5833 | |
5834 | |
5835 | mutable llvm::PointerIntPair<const ObjCObjectType *, 1, bool> |
5836 | CachedSuperClassType; |
5837 | |
5838 | QualType *getTypeArgStorage(); |
5839 | const QualType *getTypeArgStorage() const { |
5840 | return const_cast<ObjCObjectType *>(this)->getTypeArgStorage(); |
5841 | } |
5842 | |
5843 | ObjCProtocolDecl **getProtocolStorageImpl(); |
5844 | |
5845 | |
5846 | unsigned getNumProtocolsImpl() const { |
5847 | return ObjCObjectTypeBits.NumProtocols; |
5848 | } |
5849 | void setNumProtocolsImpl(unsigned N) { |
5850 | ObjCObjectTypeBits.NumProtocols = N; |
5851 | } |
5852 | |
5853 | protected: |
5854 | enum Nonce_ObjCInterface { Nonce_ObjCInterface }; |
5855 | |
5856 | ObjCObjectType(QualType Canonical, QualType Base, |
5857 | ArrayRef<QualType> typeArgs, |
5858 | ArrayRef<ObjCProtocolDecl *> protocols, |
5859 | bool isKindOf); |
5860 | |
5861 | ObjCObjectType(enum Nonce_ObjCInterface) |
5862 | : Type(ObjCInterface, QualType(), TypeDependence::None), |
5863 | BaseType(QualType(this_(), 0)) { |
5864 | ObjCObjectTypeBits.NumProtocols = 0; |
5865 | ObjCObjectTypeBits.NumTypeArgs = 0; |
5866 | ObjCObjectTypeBits.IsKindOf = 0; |
5867 | } |
5868 | |
5869 | void computeSuperClassTypeSlow() const; |
5870 | |
5871 | public: |
5872 | |
5873 | |
5874 | |
5875 | |
5876 | |
5877 | |
5878 | QualType getBaseType() const { return BaseType; } |
5879 | |
5880 | bool isObjCId() const { |
5881 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); |
5882 | } |
5883 | |
5884 | bool isObjCClass() const { |
5885 | return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); |
5886 | } |
5887 | |
5888 | bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } |
5889 | bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } |
5890 | bool isObjCUnqualifiedIdOrClass() const { |
5891 | if (!qual_empty()) return false; |
5892 | if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) |
5893 | return T->getKind() == BuiltinType::ObjCId || |
5894 | T->getKind() == BuiltinType::ObjCClass; |
5895 | return false; |
5896 | } |
5897 | bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } |
5898 | bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } |
5899 | |
5900 | |
5901 | |
5902 | ObjCInterfaceDecl *getInterface() const; |
5903 | |
5904 | |
5905 | |
5906 | bool isSpecialized() const; |
5907 | |
5908 | |
5909 | bool isSpecializedAsWritten() const { |
5910 | return ObjCObjectTypeBits.NumTypeArgs > 0; |
5911 | } |
5912 | |
5913 | |
5914 | |
5915 | bool isUnspecialized() const { return !isSpecialized(); } |
5916 | |
5917 | |
5918 | |
5919 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
5920 | |
5921 | |
5922 | ArrayRef<QualType> getTypeArgs() const; |
5923 | |
5924 | |
5925 | |
5926 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
5927 | return llvm::makeArrayRef(getTypeArgStorage(), |
5928 | ObjCObjectTypeBits.NumTypeArgs); |
5929 | } |
5930 | |
5931 | |
5932 | bool isKindOfTypeAsWritten() const { return ObjCObjectTypeBits.IsKindOf; } |
5933 | |
5934 | |
5935 | bool isKindOfType() const; |
5936 | |
5937 | |
5938 | |
5939 | |
5940 | |
5941 | |
5942 | |
5943 | QualType getSuperClassType() const { |
5944 | if (!CachedSuperClassType.getInt()) |
5945 | computeSuperClassTypeSlow(); |
5946 | |
5947 | assert(CachedSuperClassType.getInt() && "Superclass not set?"); |
5948 | return QualType(CachedSuperClassType.getPointer(), 0); |
5949 | } |
5950 | |
5951 | |
5952 | |
5953 | QualType stripObjCKindOfTypeAndQuals(const ASTContext &ctx) const; |
5954 | |
5955 | bool isSugared() const { return false; } |
5956 | QualType desugar() const { return QualType(this, 0); } |
5957 | |
5958 | static bool classof(const Type *T) { |
5959 | return T->getTypeClass() == ObjCObject || |
5960 | T->getTypeClass() == ObjCInterface; |
5961 | } |
5962 | }; |
5963 | |
5964 | |
5965 | |
5966 | |
5967 | |
5968 | class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { |
5969 | friend class ASTContext; |
5970 | |
5971 | |
5972 | |
5973 | |
5974 | ObjCObjectTypeImpl(QualType Canonical, QualType Base, |
5975 | ArrayRef<QualType> typeArgs, |
5976 | ArrayRef<ObjCProtocolDecl *> protocols, |
5977 | bool isKindOf) |
5978 | : ObjCObjectType(Canonical, Base, typeArgs, protocols, isKindOf) {} |
5979 | |
5980 | public: |
5981 | void Profile(llvm::FoldingSetNodeID &ID); |
5982 | static void Profile(llvm::FoldingSetNodeID &ID, |
5983 | QualType Base, |
5984 | ArrayRef<QualType> typeArgs, |
5985 | ArrayRef<ObjCProtocolDecl *> protocols, |
5986 | bool isKindOf); |
5987 | }; |
5988 | |
5989 | inline QualType *ObjCObjectType::getTypeArgStorage() { |
5990 | return reinterpret_cast<QualType *>(static_cast<ObjCObjectTypeImpl*>(this)+1); |
5991 | } |
5992 | |
5993 | inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorageImpl() { |
5994 | return reinterpret_cast<ObjCProtocolDecl**>( |
5995 | getTypeArgStorage() + ObjCObjectTypeBits.NumTypeArgs); |
5996 | } |
5997 | |
5998 | inline ObjCProtocolDecl **ObjCTypeParamType::getProtocolStorageImpl() { |
5999 | return reinterpret_cast<ObjCProtocolDecl**>( |
6000 | static_cast<ObjCTypeParamType*>(this)+1); |
6001 | } |
6002 | |
6003 | |
6004 | |
6005 | |
6006 | |
6007 | |
6008 | |
6009 | |
6010 | |
6011 | |
6012 | |
6013 | |
6014 | |
6015 | class ObjCInterfaceType : public ObjCObjectType { |
6016 | friend class ASTContext; |
6017 | friend class ASTReader; |
6018 | friend class ObjCInterfaceDecl; |
6019 | template <class T> friend class serialization::AbstractTypeReader; |
6020 | |
6021 | mutable ObjCInterfaceDecl *Decl; |
6022 | |
6023 | ObjCInterfaceType(const ObjCInterfaceDecl *D) |
6024 | : ObjCObjectType(Nonce_ObjCInterface), |
6025 | Decl(const_cast<ObjCInterfaceDecl*>(D)) {} |
6026 | |
6027 | public: |
6028 | |
6029 | ObjCInterfaceDecl *getDecl() const { return Decl; } |
6030 | |
6031 | bool isSugared() const { return false; } |
6032 | QualType desugar() const { return QualType(this, 0); } |
6033 | |
6034 | static bool classof(const Type *T) { |
6035 | return T->getTypeClass() == ObjCInterface; |
6036 | } |
6037 | |
6038 | |
6039 | |
6040 | |
6041 | |
6042 | enum { |
6043 | qual_iterator, |
6044 | qual_begin, |
6045 | qual_end, |
6046 | getNumProtocols, |
6047 | getProtocol |
6048 | }; |
6049 | }; |
6050 | |
6051 | inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { |
6052 | QualType baseType = getBaseType(); |
6053 | while (const auto *ObjT = baseType->getAs<ObjCObjectType>()) { |
6054 | if (const auto *T = dyn_cast<ObjCInterfaceType>(ObjT)) |
6055 | return T->getDecl(); |
6056 | |
6057 | baseType = ObjT->getBaseType(); |
6058 | } |
6059 | |
6060 | return nullptr; |
6061 | } |
6062 | |
6063 | |
6064 | |
6065 | |
6066 | |
6067 | |
6068 | |
6069 | |
6070 | |
6071 | |
6072 | class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { |
6073 | friend class ASTContext; |
6074 | |
6075 | QualType PointeeType; |
6076 | |
6077 | ObjCObjectPointerType(QualType Canonical, QualType Pointee) |
6078 | : Type(ObjCObjectPointer, Canonical, Pointee->getDependence()), |
6079 | PointeeType(Pointee) {} |
6080 | |
6081 | public: |
6082 | |
6083 | |
6084 | QualType getPointeeType() const { return PointeeType; } |
6085 | |
6086 | |
6087 | |
6088 | |
6089 | |
6090 | |
6091 | |
6092 | |
6093 | |
6094 | |
6095 | |
6096 | |
6097 | |
6098 | |
6099 | |
6100 | |
6101 | |
6102 | |
6103 | |
6104 | |
6105 | |
6106 | |
6107 | |
6108 | |
6109 | const ObjCObjectType *getObjectType() const { |
6110 | return PointeeType->castAs<ObjCObjectType>(); |
6111 | } |
6112 | |
6113 | |
6114 | |
6115 | |
6116 | |
6117 | |
6118 | const ObjCInterfaceType *getInterfaceType() const; |
6119 | |
6120 | |
6121 | |
6122 | |
6123 | |
6124 | ObjCInterfaceDecl *getInterfaceDecl() const { |
6125 | return getObjectType()->getInterface(); |
6126 | } |
6127 | |
6128 | |
6129 | |
6130 | bool isObjCIdType() const { |
6131 | return getObjectType()->isObjCUnqualifiedId(); |
6132 | } |
6133 | |
6134 | |
6135 | |
6136 | bool isObjCClassType() const { |
6137 | return getObjectType()->isObjCUnqualifiedClass(); |
6138 | } |
6139 | |
6140 | |
6141 | bool isObjCIdOrClassType() const { |
6142 | return getObjectType()->isObjCUnqualifiedIdOrClass(); |
6143 | } |
6144 | |
6145 | |
6146 | |
6147 | bool isObjCQualifiedIdType() const { |
6148 | return getObjectType()->isObjCQualifiedId(); |
6149 | } |
6150 | |
6151 | |
6152 | |
6153 | bool isObjCQualifiedClassType() const { |
6154 | return getObjectType()->isObjCQualifiedClass(); |
6155 | } |
6156 | |
6157 | |
6158 | bool isKindOfType() const { return getObjectType()->isKindOfType(); } |
6159 | |
6160 | |
6161 | bool isSpecialized() const { return getObjectType()->isSpecialized(); } |
6162 | |
6163 | |
6164 | bool isSpecializedAsWritten() const { |
6165 | return getObjectType()->isSpecializedAsWritten(); |
6166 | } |
6167 | |
6168 | |
6169 | bool isUnspecialized() const { return getObjectType()->isUnspecialized(); } |
6170 | |
6171 | |
6172 | |
6173 | bool isUnspecializedAsWritten() const { return !isSpecializedAsWritten(); } |
6174 | |
6175 | |
6176 | ArrayRef<QualType> getTypeArgs() const { |
6177 | return getObjectType()->getTypeArgs(); |
6178 | } |
6179 | |
6180 | |
6181 | ArrayRef<QualType> getTypeArgsAsWritten() const { |
6182 | return getObjectType()->getTypeArgsAsWritten(); |
6183 | } |
6184 | |
6185 | |
6186 | |
6187 | |
6188 | using qual_iterator = ObjCObjectType::qual_iterator; |
6189 | using qual_range = llvm::iterator_range<qual_iterator>; |
6190 | |
6191 | qual_range quals() const { return qual_range(qual_begin(), qual_end()); } |
6192 | |
6193 | qual_iterator qual_begin() const { |
6194 | return getObjectType()->qual_begin(); |
6195 | } |
6196 | |
6197 | qual_iterator qual_end() const { |
6198 | return getObjectType()->qual_end(); |
6199 | } |
6200 | |
6201 | bool qual_empty() const { return getObjectType()->qual_empty(); } |
6202 | |
6203 | |
6204 | unsigned getNumProtocols() const { |
6205 | return getObjectType()->getNumProtocols(); |
6206 | } |
6207 | |
6208 | |
6209 | ObjCProtocolDecl *getProtocol(unsigned I) const { |
6210 | return getObjectType()->getProtocol(I); |
6211 | } |
6212 | |
6213 | bool isSugared() const { return false; } |
6214 | QualType desugar() const { return QualType(this, 0); } |
6215 | |
6216 | |
6217 | |
6218 | |
6219 | |
6220 | |
6221 | |
6222 | QualType getSuperClassType() const; |
6223 | |
6224 | |
6225 | |
6226 | const ObjCObjectPointerType *stripObjCKindOfTypeAndQuals( |
6227 | const ASTContext &ctx) const; |
6228 | |
6229 | void Profile(llvm::FoldingSetNodeID &ID) { |
6230 | Profile(ID, getPointeeType()); |
6231 | } |
6232 | |
6233 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6234 | ID.AddPointer(T.getAsOpaquePtr()); |
6235 | } |
6236 | |
6237 | static bool classof(const Type *T) { |
6238 | return T->getTypeClass() == ObjCObjectPointer; |
6239 | } |
6240 | }; |
6241 | |
6242 | class AtomicType : public Type, public llvm::FoldingSetNode { |
6243 | friend class ASTContext; |
6244 | |
6245 | QualType ValueType; |
6246 | |
6247 | AtomicType(QualType ValTy, QualType Canonical) |
6248 | : Type(Atomic, Canonical, ValTy->getDependence()), ValueType(ValTy) {} |
6249 | |
6250 | public: |
6251 | |
6252 | |
6253 | QualType getValueType() const { return ValueType; } |
6254 | |
6255 | bool isSugared() const { return false; } |
6256 | QualType desugar() const { return QualType(this, 0); } |
6257 | |
6258 | void Profile(llvm::FoldingSetNodeID &ID) { |
6259 | Profile(ID, getValueType()); |
6260 | } |
6261 | |
6262 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { |
6263 | ID.AddPointer(T.getAsOpaquePtr()); |
6264 | } |
6265 | |
6266 | static bool classof(const Type *T) { |
6267 | return T->getTypeClass() == Atomic; |
6268 | } |
6269 | }; |
6270 | |
6271 | |
6272 | class PipeType : public Type, public llvm::FoldingSetNode { |
6273 | friend class ASTContext; |
6274 | |
6275 | QualType ElementType; |
6276 | bool isRead; |
6277 | |
6278 | PipeType(QualType elemType, QualType CanonicalPtr, bool isRead) |
6279 | : Type(Pipe, CanonicalPtr, elemType->getDependence()), |
6280 | ElementType(elemType), isRead(isRead) {} |
6281 | |
6282 | public: |
6283 | QualType getElementType() const { return ElementType; } |
6284 | |
6285 | bool isSugared() const { return false; } |
6286 | |
6287 | QualType desugar() const { return QualType(this, 0); } |
6288 | |
6289 | void Profile(llvm::FoldingSetNodeID &ID) { |
6290 | Profile(ID, getElementType(), isReadOnly()); |
6291 | } |
6292 | |
6293 | static void Profile(llvm::FoldingSetNodeID &ID, QualType T, bool isRead) { |
6294 | ID.AddPointer(T.getAsOpaquePtr()); |
6295 | ID.AddBoolean(isRead); |
6296 | } |
6297 | |
6298 | static bool classof(const Type *T) { |
6299 | return T->getTypeClass() == Pipe; |
6300 | } |
6301 | |
6302 | bool isReadOnly() const { return isRead; } |
6303 | }; |
6304 | |
6305 | |
6306 | class ExtIntType final : public Type, public llvm::FoldingSetNode { |
6307 | friend class ASTContext; |
6308 | unsigned IsUnsigned : 1; |
6309 | unsigned NumBits : 24; |
6310 | |
6311 | protected: |
6312 | ExtIntType(bool isUnsigned, unsigned NumBits); |
6313 | |
6314 | public: |
6315 | bool isUnsigned() const { return IsUnsigned; } |
6316 | bool isSigned() const { return !IsUnsigned; } |
6317 | unsigned getNumBits() const { return NumBits; } |
6318 | |
6319 | bool isSugared() const { return false; } |
6320 | QualType desugar() const { return QualType(this, 0); } |
6321 | |
6322 | void Profile(llvm::FoldingSetNodeID &ID) { |
6323 | Profile(ID, isUnsigned(), getNumBits()); |
6324 | } |
6325 | |
6326 | static void Profile(llvm::FoldingSetNodeID &ID, bool IsUnsigned, |
6327 | unsigned NumBits) { |
6328 | ID.AddBoolean(IsUnsigned); |
6329 | ID.AddInteger(NumBits); |
6330 | } |
6331 | |
6332 | static bool classof(const Type *T) { return T->getTypeClass() == ExtInt; } |
6333 | }; |
6334 | |
6335 | class DependentExtIntType final : public Type, public llvm::FoldingSetNode { |
6336 | friend class ASTContext; |
6337 | const ASTContext &Context; |
6338 | llvm::PointerIntPair<Expr*, 1, bool> ExprAndUnsigned; |
6339 | |
6340 | protected: |
6341 | DependentExtIntType(const ASTContext &Context, bool IsUnsigned, |
6342 | Expr *NumBits); |
6343 | |
6344 | public: |
6345 | bool isUnsigned() const; |
6346 | bool isSigned() const { return !isUnsigned(); } |
6347 | Expr *getNumBitsExpr() const; |
6348 | |
6349 | bool isSugared() const { return false; } |
6350 | QualType desugar() const { return QualType(this, 0); } |
6351 | |
6352 | void Profile(llvm::FoldingSetNodeID &ID) { |
6353 | Profile(ID, Context, isUnsigned(), getNumBitsExpr()); |
6354 | } |
6355 | static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context, |
6356 | bool IsUnsigned, Expr *NumBitsExpr); |
6357 | |
6358 | static bool classof(const Type *T) { |
6359 | return T->getTypeClass() == DependentExtInt; |
6360 | } |
6361 | }; |
6362 | |
6363 | |
6364 | class QualifierCollector : public Qualifiers { |
6365 | public: |
6366 | QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {} |
6367 | |
6368 | |
6369 | |
6370 | |
6371 | const Type *strip(QualType type) { |
6372 | addFastQualifiers(type.getLocalFastQualifiers()); |
6373 | if (!type.hasLocalNonFastQualifiers()) |
6374 | return type.getTypePtrUnsafe(); |
6375 | |
6376 | const ExtQuals *extQuals = type.getExtQualsUnsafe(); |
6377 | addConsistentQualifiers(extQuals->getQualifiers()); |
6378 | return extQuals->getBaseType(); |
6379 | } |
6380 | |
6381 | |
6382 | QualType apply(const ASTContext &Context, QualType QT) const; |
6383 | |
6384 | |
6385 | QualType apply(const ASTContext &Context, const Type* T) const; |
6386 | }; |
6387 | |
6388 | |
6389 | |
6390 | |
6391 | |
6392 | |
6393 | |
6394 | |
6395 | class alignas(8) TypeSourceInfo { |
6396 | |
6397 | |
6398 | friend class ASTContext; |
6399 | |
6400 | QualType Ty; |
6401 | |
6402 | TypeSourceInfo(QualType ty) : Ty(ty) {} |
6403 | |
6404 | public: |
6405 | |
6406 | QualType getType() const { return Ty; } |
6407 | |
6408 | |
6409 | TypeLoc getTypeLoc() const; |
6410 | |
6411 | |
6412 | void overrideType(QualType T) { Ty = T; } |
6413 | }; |
6414 | |
6415 | |
6416 | |
6417 | inline SplitQualType SplitQualType::getSingleStepDesugaredType() const { |
6418 | SplitQualType desugar = |
6419 | Ty->getLocallyUnqualifiedSingleStepDesugaredType().split(); |
6420 | desugar.Quals.addConsistentQualifiers(Quals); |
6421 | return desugar; |
6422 | } |
6423 | |
6424 | inline const Type *QualType::getTypePtr() const { |
6425 | return getCommonPtr()->BaseType; |
6426 | } |
6427 | |
6428 | inline const Type *QualType::getTypePtrOrNull() const { |
6429 | return (isNull() ? nullptr : getCommonPtr()->BaseType); |
6430 | } |
6431 | |
6432 | inline SplitQualType QualType::split() const { |
6433 | if (!hasLocalNonFastQualifiers()) |
6434 | return SplitQualType(getTypePtrUnsafe(), |
6435 | Qualifiers::fromFastMask(getLocalFastQualifiers())); |
6436 | |
6437 | const ExtQuals *eq = getExtQualsUnsafe(); |
6438 | Qualifiers qs = eq->getQualifiers(); |
6439 | qs.addFastQualifiers(getLocalFastQualifiers()); |
6440 | return SplitQualType(eq->getBaseType(), qs); |
6441 | } |
6442 | |
6443 | inline Qualifiers QualType::getLocalQualifiers() const { |
6444 | Qualifiers Quals; |
6445 | if (hasLocalNonFastQualifiers()) |
6446 | Quals = getExtQualsUnsafe()->getQualifiers(); |
6447 | Quals.addFastQualifiers(getLocalFastQualifiers()); |
6448 | return Quals; |
6449 | } |
6450 | |
6451 | inline Qualifiers QualType::getQualifiers() const { |
6452 | Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers(); |
6453 | quals.addFastQualifiers(getLocalFastQualifiers()); |
6454 | return quals; |
6455 | } |
6456 | |
6457 | inline unsigned QualType::getCVRQualifiers() const { |
6458 | unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers(); |
6459 | cvr |= getLocalCVRQualifiers(); |
6460 | return cvr; |
6461 | } |
6462 | |
6463 | inline QualType QualType::getCanonicalType() const { |
6464 | QualType canon = getCommonPtr()->CanonicalType; |
6465 | return canon.withFastQualifiers(getLocalFastQualifiers()); |
6466 | } |
6467 | |
6468 | inline bool QualType::isCanonical() const { |
6469 | return getTypePtr()->isCanonicalUnqualified(); |
6470 | } |
6471 | |
6472 | inline bool QualType::isCanonicalAsParam() const { |
6473 | if (!isCanonical()) return false; |
6474 | if (hasLocalQualifiers()) return false; |
6475 | |
6476 | const Type *T = getTypePtr(); |
6477 | if (T->isVariablyModifiedType() && T->hasSizedVLAType()) |
6478 | return false; |
6479 | |
6480 | return !isa<FunctionType>(T) && !isa<ArrayType>(T); |
6481 | } |
6482 | |
6483 | inline bool QualType::isConstQualified() const { |
6484 | return isLocalConstQualified() || |
6485 | getCommonPtr()->CanonicalType.isLocalConstQualified(); |
6486 | } |
6487 | |
6488 | inline bool QualType::isRestrictQualified() const { |
6489 | return isLocalRestrictQualified() || |
6490 | getCommonPtr()->CanonicalType.isLocalRestrictQualified(); |
6491 | } |
6492 | |
6493 | |
6494 | inline bool QualType::isVolatileQualified() const { |
6495 | return isLocalVolatileQualified() || |
6496 | getCommonPtr()->CanonicalType.isLocalVolatileQualified(); |
6497 | } |
6498 | |
6499 | inline bool QualType::hasQualifiers() const { |
6500 | return hasLocalQualifiers() || |
6501 | getCommonPtr()->CanonicalType.hasLocalQualifiers(); |
6502 | } |
6503 | |
6504 | inline QualType QualType::getUnqualifiedType() const { |
6505 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6506 | return QualType(getTypePtr(), 0); |
6507 | |
6508 | return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0); |
6509 | } |
6510 | |
6511 | inline SplitQualType QualType::getSplitUnqualifiedType() const { |
6512 | if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers()) |
6513 | return split(); |
6514 | |
6515 | return getSplitUnqualifiedTypeImpl(*this); |
6516 | } |
6517 | |
6518 | inline void QualType::removeLocalConst() { |
6519 | removeLocalFastQualifiers(Qualifiers::Const); |
6520 | } |
6521 | |
6522 | inline void QualType::removeLocalRestrict() { |
6523 | removeLocalFastQualifiers(Qualifiers::Restrict); |
6524 | } |
6525 | |
6526 | inline void QualType::removeLocalVolatile() { |
6527 | removeLocalFastQualifiers(Qualifiers::Volatile); |
6528 | } |
6529 | |
6530 | inline void QualType::removeLocalCVRQualifiers(unsigned Mask) { |
6531 | assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); |
6532 | static_assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask, |
6533 | "Fast bits differ from CVR bits!"); |
6534 | |
6535 | |
6536 | removeLocalFastQualifiers(Mask); |
6537 | } |
6538 | |
6539 | |
6540 | inline bool QualType::hasAddressSpace() const { |
6541 | return getQualifiers().hasAddressSpace(); |
6542 | } |
6543 | |
6544 | |
6545 | inline LangAS QualType::getAddressSpace() const { |
6546 | return getQualifiers().getAddressSpace(); |
6547 | } |
6548 | |
6549 | |
6550 | inline Qualifiers::GC QualType::getObjCGCAttr() const { |
6551 | return getQualifiers().getObjCGCAttr(); |
6552 | } |
6553 | |
6554 | inline bool QualType::hasNonTrivialToPrimitiveDefaultInitializeCUnion() const { |
6555 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6556 | return hasNonTrivialToPrimitiveDefaultInitializeCUnion(RD); |
6557 | return false; |
6558 | } |
6559 | |
6560 | inline bool QualType::hasNonTrivialToPrimitiveDestructCUnion() const { |
6561 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6562 | return hasNonTrivialToPrimitiveDestructCUnion(RD); |
6563 | return false; |
6564 | } |
6565 | |
6566 | inline bool QualType::hasNonTrivialToPrimitiveCopyCUnion() const { |
6567 | if (auto *RD = getTypePtr()->getBaseElementTypeUnsafe()->getAsRecordDecl()) |
6568 | return hasNonTrivialToPrimitiveCopyCUnion(RD); |
6569 | return false; |
6570 | } |
6571 | |
6572 | inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { |
6573 | if (const auto *PT = t.getAs<PointerType>()) { |
6574 | if (const auto *FT = PT->getPointeeType()->getAs<FunctionType>()) |
6575 | return FT->getExtInfo(); |
6576 | } else if (const auto *FT = t.getAs<FunctionType>()) |
6577 | return FT->getExtInfo(); |
6578 | |
6579 | return FunctionType::ExtInfo(); |
6580 | } |
6581 | |
6582 | inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { |
6583 | return getFunctionExtInfo(*t); |
6584 | } |
6585 | |
6586 | |
6587 | |
6588 | |
6589 | |
6590 | |
6591 | inline bool QualType::isMoreQualifiedThan(QualType other) const { |
6592 | Qualifiers MyQuals = getQualifiers(); |
6593 | Qualifiers OtherQuals = other.getQualifiers(); |
6594 | return (MyQuals != OtherQuals && MyQuals.compatiblyIncludes(OtherQuals)); |
6595 | } |
6596 | |
6597 | |
6598 | |
6599 | |
6600 | |
6601 | inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const { |
6602 | Qualifiers OtherQuals = other.getQualifiers(); |
6603 | |
6604 | |
6605 | if (getUnqualifiedType()->isVoidType()) |
6606 | OtherQuals.removeUnaligned(); |
6607 | |
6608 | return getQualifiers().compatiblyIncludes(OtherQuals); |
6609 | } |
6610 | |
6611 | |
6612 | |
6613 | |
6614 | |
6615 | |
6616 | |
6617 | |
6618 | |
6619 | |
6620 | inline QualType QualType::getNonReferenceType() const { |
6621 | if (const auto *RefType = (*this)->getAs<ReferenceType>()) |
6622 | return RefType->getPointeeType(); |
6623 | else |
6624 | return *this; |
6625 | } |
6626 | |
6627 | inline bool QualType::isCForbiddenLValueType() const { |
6628 | return ((getTypePtr()->isVoidType() && !hasQualifiers()) || |
6629 | getTypePtr()->isFunctionType()); |
6630 | } |
6631 | |
6632 | |
6633 | |
6634 | |
6635 | inline bool Type::isFundamentalType() const { |
6636 | return isVoidType() || |
6637 | isNullPtrType() || |
6638 | |
6639 | |
6640 | (isArithmeticType() && !isEnumeralType()); |
6641 | } |
6642 | |
6643 | |
6644 | |
6645 | |
6646 | inline bool Type::isCompoundType() const { |
6647 | |
6648 | |
6649 | |
6650 | return isArrayType() || |
6651 | |
6652 | isFunctionType() || |
6653 | |
6654 | isPointerType() || |
6655 | |
6656 | isReferenceType() || |
6657 | |
6658 | isRecordType() || |
6659 | |
6660 | |
6661 | isUnionType() || |
6662 | |
6663 | isEnumeralType() || |
6664 | |
6665 | isMemberPointerType(); |
6666 | } |
6667 | |
6668 | inline bool Type::isFunctionType() const { |
6669 | return isa<FunctionType>(CanonicalType); |
| 16 | | Assuming field 'CanonicalType' is a 'FunctionType' | |
|
| 17 | | Returning the value 1, which participates in a condition later | |
|
6670 | } |
6671 | |
6672 | inline bool Type::isPointerType() const { |
6673 | return isa<PointerType>(CanonicalType); |
6674 | } |
6675 | |
6676 | inline bool Type::isAnyPointerType() const { |
6677 | return isPointerType() || isObjCObjectPointerType(); |
6678 | } |
6679 | |
6680 | inline bool Type::isBlockPointerType() const { |
6681 | return isa<BlockPointerType>(CanonicalType); |
6682 | } |
6683 | |
6684 | inline bool Type::isReferenceType() const { |
6685 | return isa<ReferenceType>(CanonicalType); |
6686 | } |
6687 | |
6688 | inline bool Type::isLValueReferenceType() const { |
6689 | return isa<LValueReferenceType>(CanonicalType); |
6690 | } |
6691 | |
6692 | inline bool Type::isRValueReferenceType() const { |
6693 | return isa<RValueReferenceType>(CanonicalType); |
6694 | } |
6695 | |
6696 | inline bool Type::isObjectPointerType() const { |
6697 | |
6698 | |
6699 | |
6700 | if (const auto *T = getAs<PointerType>()) |
6701 | return !T->getPointeeType()->isFunctionType(); |
6702 | else |
6703 | return false; |
6704 | } |
6705 | |
6706 | inline bool Type::isFunctionPointerType() const { |
6707 | if (const auto *T = getAs<PointerType>()) |
| 13 | | Assuming the object is a 'PointerType' | |
|
| |
6708 | return T->getPointeeType()->isFunctionType(); |
| 15 | | Calling 'Type::isFunctionType' | |
|
| 18 | | Returning from 'Type::isFunctionType' | |
|
| 19 | | Returning the value 1, which participates in a condition later | |
|
6709 | else |
6710 | return false; |
6711 | } |
6712 | |
6713 | inline bool Type::isFunctionReferenceType() const { |
6714 | if (const auto *T = getAs<ReferenceType>()) |
6715 | return T->getPointeeType()->isFunctionType(); |
6716 | else |
6717 | return false; |
6718 | } |
6719 | |
6720 | inline bool Type::isMemberPointerType() const { |
6721 | return isa<MemberPointerType>(CanonicalType); |
6722 | } |
6723 | |
6724 | inline bool Type::isMemberFunctionPointerType() const { |
6725 | if (const auto *T = getAs<MemberPointerType>()) |
6726 | return T->isMemberFunctionPointer(); |
6727 | else |
6728 | return false; |
6729 | } |
6730 | |
6731 | inline bool Type::isMemberDataPointerType() const { |
6732 | if (const auto *T = getAs<MemberPointerType>()) |
6733 | return T->isMemberDataPointer(); |
6734 | else |
6735 | return false; |
6736 | } |
6737 | |
6738 | inline bool Type::isArrayType() const { |
6739 | return isa<ArrayType>(CanonicalType); |
6740 | } |
6741 | |
6742 | inline bool Type::isConstantArrayType() const { |
6743 | return isa<ConstantArrayType>(CanonicalType); |
6744 | } |
6745 | |
6746 | inline bool Type::isIncompleteArrayType() const { |
6747 | return isa<IncompleteArrayType>(CanonicalType); |
6748 | } |
6749 | |
6750 | inline bool Type::isVariableArrayType() const { |
6751 | return isa<VariableArrayType>(CanonicalType); |
6752 | } |
6753 | |
6754 | inline bool Type::isDependentSizedArrayType() const { |
6755 | return isa<DependentSizedArrayType>(CanonicalType); |
6756 | } |
6757 | |
6758 | inline bool Type::isBuiltinType() const { |
6759 | return isa<BuiltinType>(CanonicalType); |
6760 | } |
6761 | |
6762 | inline bool Type::isRecordType() const { |
6763 | return isa<RecordType>(CanonicalType); |
6764 | } |
6765 | |
6766 | inline bool Type::isEnumeralType() const { |
6767 | return isa<EnumType>(CanonicalType); |
6768 | } |
6769 | |
6770 | inline bool Type::isAnyComplexType() const { |
6771 | return isa<ComplexType>(CanonicalType); |
6772 | } |
6773 | |
6774 | inline bool Type::isVectorType() const { |
6775 | return isa<VectorType>(CanonicalType); |
6776 | } |
6777 | |
6778 | inline bool Type::isExtVectorType() const { |
6779 | return isa<ExtVectorType>(CanonicalType); |
6780 | } |
6781 | |
6782 | inline bool Type::isMatrixType() const { |
6783 | return isa<MatrixType>(CanonicalType); |
6784 | } |
6785 | |
6786 | inline bool Type::isConstantMatrixType() const { |
6787 | return isa<ConstantMatrixType>(CanonicalType); |
6788 | } |
6789 | |
6790 | inline bool Type::isDependentAddressSpaceType() const { |
6791 | return isa<DependentAddressSpaceType>(CanonicalType); |
6792 | } |
6793 | |
6794 | inline bool Type::isObjCObjectPointerType() const { |
6795 | return isa<ObjCObjectPointerType>(CanonicalType); |
6796 | } |
6797 | |
6798 | inline bool Type::isObjCObjectType() const { |
6799 | return isa<ObjCObjectType>(CanonicalType); |
6800 | } |
6801 | |
6802 | inline bool Type::isObjCObjectOrInterfaceType() const { |
6803 | return isa<ObjCInterfaceType>(CanonicalType) || |
6804 | isa<ObjCObjectType>(CanonicalType); |
6805 | } |
6806 | |
6807 | inline bool Type::isAtomicType() const { |
6808 | return isa<AtomicType>(CanonicalType); |
6809 | } |
6810 | |
6811 | inline bool Type::isUndeducedAutoType() const { |
6812 | return isa<AutoType>(CanonicalType); |
6813 | } |
6814 | |
6815 | inline bool Type::isObjCQualifiedIdType() const { |
6816 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6817 | return OPT->isObjCQualifiedIdType(); |
6818 | return false; |
6819 | } |
6820 | |
6821 | inline bool Type::isObjCQualifiedClassType() const { |
6822 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6823 | return OPT->isObjCQualifiedClassType(); |
6824 | return false; |
6825 | } |
6826 | |
6827 | inline bool Type::isObjCIdType() const { |
6828 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6829 | return OPT->isObjCIdType(); |
6830 | return false; |
6831 | } |
6832 | |
6833 | inline bool Type::isObjCClassType() const { |
6834 | if (const auto *OPT = getAs<ObjCObjectPointerType>()) |
6835 | return OPT->isObjCClassType(); |
6836 | return false; |
6837 | } |
6838 | |
6839 | inline bool Type::isObjCSelType() const { |
6840 | if (const auto *OPT = getAs<PointerType>()) |
6841 | return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); |
6842 | return false; |
6843 | } |
6844 | |
6845 | inline bool Type::isObjCBuiltinType() const { |
6846 | return isObjCIdType() || isObjCClassType() || isObjCSelType(); |
6847 | } |
6848 | |
6849 | inline bool Type::isDecltypeType() const { |
6850 | return isa<DecltypeType>(this); |
6851 | } |
6852 | |
6853 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
6854 | inline bool Type::is##Id##Type() const { \ |
6855 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6856 | } |
6857 | #include "clang/Basic/OpenCLImageTypes.def" |
6858 | |
6859 | inline bool Type::isSamplerT() const { |
6860 | return isSpecificBuiltinType(BuiltinType::OCLSampler); |
6861 | } |
6862 | |
6863 | inline bool Type::isEventT() const { |
6864 | return isSpecificBuiltinType(BuiltinType::OCLEvent); |
6865 | } |
6866 | |
6867 | inline bool Type::isClkEventT() const { |
6868 | return isSpecificBuiltinType(BuiltinType::OCLClkEvent); |
6869 | } |
6870 | |
6871 | inline bool Type::isQueueT() const { |
6872 | return isSpecificBuiltinType(BuiltinType::OCLQueue); |
6873 | } |
6874 | |
6875 | inline bool Type::isReserveIDT() const { |
6876 | return isSpecificBuiltinType(BuiltinType::OCLReserveID); |
6877 | } |
6878 | |
6879 | inline bool Type::isImageType() const { |
6880 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) is##Id##Type() || |
6881 | return |
6882 | #include "clang/Basic/OpenCLImageTypes.def" |
6883 | false; |
6884 | } |
6885 | |
6886 | inline bool Type::isPipeType() const { |
6887 | return isa<PipeType>(CanonicalType); |
6888 | } |
6889 | |
6890 | inline bool Type::isExtIntType() const { |
6891 | return isa<ExtIntType>(CanonicalType); |
6892 | } |
6893 | |
6894 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
6895 | inline bool Type::is##Id##Type() const { \ |
6896 | return isSpecificBuiltinType(BuiltinType::Id); \ |
6897 | } |
6898 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6899 | |
6900 | inline bool Type::isOCLIntelSubgroupAVCType() const { |
6901 | #define INTEL_SUBGROUP_AVC_TYPE(ExtType, Id) \ |
6902 | isOCLIntelSubgroupAVC##Id##Type() || |
6903 | return |
6904 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6905 | false; |
6906 | } |
6907 | |
6908 | inline bool Type::isOCLExtOpaqueType() const { |
6909 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) is##Id##Type() || |
6910 | return |
6911 | #include "clang/Basic/OpenCLExtensionTypes.def" |
6912 | false; |
6913 | } |
6914 | |
6915 | inline bool Type::isOpenCLSpecificType() const { |
6916 | return isSamplerT() || isEventT() || isImageType() || isClkEventT() || |
6917 | isQueueT() || isReserveIDT() || isPipeType() || isOCLExtOpaqueType(); |
6918 | } |
6919 | |
6920 | inline bool Type::isTemplateTypeParmType() const { |
6921 | return isa<TemplateTypeParmType>(CanonicalType); |
6922 | } |
6923 | |
6924 | inline bool Type::isSpecificBuiltinType(unsigned K) const { |
6925 | if (const BuiltinType *BT = getAs<BuiltinType>()) { |
| 7 | | Assuming the object is not a 'BuiltinType' | |
|
| |
6926 | return BT->getKind() == static_cast<BuiltinType::Kind>(K); |
6927 | } |
6928 | return false; |
| 9 | | Returning zero, which participates in a condition later | |
|
6929 | } |
6930 | |
6931 | inline bool Type::isPlaceholderType() const { |
6932 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6933 | return BT->isPlaceholderType(); |
6934 | return false; |
6935 | } |
6936 | |
6937 | inline const BuiltinType *Type::getAsPlaceholderType() const { |
6938 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6939 | if (BT->isPlaceholderType()) |
6940 | return BT; |
6941 | return nullptr; |
6942 | } |
6943 | |
6944 | inline bool Type::isSpecificPlaceholderType(unsigned K) const { |
6945 | assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K)); |
6946 | return isSpecificBuiltinType(K); |
6947 | } |
6948 | |
6949 | inline bool Type::isNonOverloadPlaceholderType() const { |
6950 | if (const auto *BT = dyn_cast<BuiltinType>(this)) |
6951 | return BT->isNonOverloadPlaceholderType(); |
6952 | return false; |
6953 | } |
6954 | |
6955 | inline bool Type::isVoidType() const { |
6956 | return isSpecificBuiltinType(BuiltinType::Void); |
6957 | } |
6958 | |
6959 | inline bool Type::isHalfType() const { |
6960 | |
6961 | return isSpecificBuiltinType(BuiltinType::Half); |
6962 | } |
6963 | |
6964 | inline bool Type::isFloat16Type() const { |
6965 | return isSpecificBuiltinType(BuiltinType::Float16); |
6966 | } |
6967 | |
6968 | inline bool Type::isBFloat16Type() const { |
6969 | return isSpecificBuiltinType(BuiltinType::BFloat16); |
6970 | } |
6971 | |
6972 | inline bool Type::isFloat128Type() const { |
6973 | return isSpecificBuiltinType(BuiltinType::Float128); |
6974 | } |
6975 | |
6976 | inline bool Type::isNullPtrType() const { |
6977 | return isSpecificBuiltinType(BuiltinType::NullPtr); |
6978 | } |
6979 | |
6980 | bool IsEnumDeclComplete(EnumDecl *); |
6981 | bool IsEnumDeclScoped(EnumDecl *); |
6982 | |
6983 | inline bool Type::isIntegerType() const { |
6984 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
6985 | return BT->getKind() >= BuiltinType::Bool && |
6986 | BT->getKind() <= BuiltinType::Int128; |
6987 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) { |
6988 | |
6989 | |
6990 | return IsEnumDeclComplete(ET->getDecl()) && |
6991 | !IsEnumDeclScoped(ET->getDecl()); |
6992 | } |
6993 | return isExtIntType(); |
6994 | } |
6995 | |
6996 | inline bool Type::isFixedPointType() const { |
6997 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
6998 | return BT->getKind() >= BuiltinType::ShortAccum && |
6999 | BT->getKind() <= BuiltinType::SatULongFract; |
7000 | } |
7001 | return false; |
7002 | } |
7003 | |
7004 | inline bool Type::isFixedPointOrIntegerType() const { |
7005 | return isFixedPointType() || isIntegerType(); |
7006 | } |
7007 | |
7008 | inline bool Type::isSaturatedFixedPointType() const { |
7009 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7010 | return BT->getKind() >= BuiltinType::SatShortAccum && |
7011 | BT->getKind() <= BuiltinType::SatULongFract; |
7012 | } |
7013 | return false; |
7014 | } |
7015 | |
7016 | inline bool Type::isUnsaturatedFixedPointType() const { |
7017 | return isFixedPointType() && !isSaturatedFixedPointType(); |
7018 | } |
7019 | |
7020 | inline bool Type::isSignedFixedPointType() const { |
7021 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) { |
7022 | return ((BT->getKind() >= BuiltinType::ShortAccum && |
7023 | BT->getKind() <= BuiltinType::LongAccum) || |
7024 | (BT->getKind() >= BuiltinType::ShortFract && |
7025 | BT->getKind() <= BuiltinType::LongFract) || |
7026 | (BT->getKind() >= BuiltinType::SatShortAccum && |
7027 | BT->getKind() <= BuiltinType::SatLongAccum) || |
7028 | (BT->getKind() >= BuiltinType::SatShortFract && |
7029 | BT->getKind() <= BuiltinType::SatLongFract)); |
7030 | } |
7031 | return false; |
7032 | } |
7033 | |
7034 | inline bool Type::isUnsignedFixedPointType() const { |
7035 | return isFixedPointType() && !isSignedFixedPointType(); |
7036 | } |
7037 | |
7038 | inline bool Type::isScalarType() const { |
7039 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7040 | return BT->getKind() > BuiltinType::Void && |
7041 | BT->getKind() <= BuiltinType::NullPtr; |
7042 | if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) |
7043 | |
7044 | |
7045 | return IsEnumDeclComplete(ET->getDecl()); |
7046 | return isa<PointerType>(CanonicalType) || |
7047 | isa<BlockPointerType>(CanonicalType) || |
7048 | isa<MemberPointerType>(CanonicalType) || |
7049 | isa<ComplexType>(CanonicalType) || |
7050 | isa<ObjCObjectPointerType>(CanonicalType) || |
7051 | isExtIntType(); |
7052 | } |
7053 | |
7054 | inline bool Type::isIntegralOrEnumerationType() const { |
7055 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7056 | return BT->getKind() >= BuiltinType::Bool && |
7057 | BT->getKind() <= BuiltinType::Int128; |
7058 | |
7059 | |
7060 | |
7061 | if (const auto *ET = dyn_cast<EnumType>(CanonicalType)) |
7062 | return IsEnumDeclComplete(ET->getDecl()); |
7063 | |
7064 | return isExtIntType(); |
7065 | } |
7066 | |
7067 | inline bool Type::isBooleanType() const { |
7068 | if (const auto *BT = dyn_cast<BuiltinType>(CanonicalType)) |
7069 | return BT->getKind() == BuiltinType::Bool; |
7070 | return false; |
7071 | } |
7072 | |
7073 | inline bool Type::isUndeducedType() const { |
7074 | auto *DT = getContainedDeducedType(); |
7075 | return DT && !DT->isDeduced(); |
7076 | } |
7077 | |
7078 | |
7079 | |
7080 | inline bool Type::isOverloadableType() const { |
7081 | return isDependentType() || isRecordType() || isEnumeralType(); |
7082 | } |
7083 | |
7084 | |
7085 | inline bool Type::isTypedefNameType() const { |
7086 | if (getAs<TypedefType>()) |
7087 | return true; |
7088 | if (auto *TST = getAs<TemplateSpecializationType>()) |
7089 | return TST->isTypeAlias(); |
7090 | return false; |
7091 | } |
7092 | |
7093 | |
7094 | inline bool Type::canDecayToPointerType() const { |
7095 | return isFunctionType() || isArrayType(); |
7096 | } |
7097 | |
7098 | inline bool Type::hasPointerRepresentation() const { |
7099 | return (isPointerType() || isReferenceType() || isBlockPointerType() || |
7100 | isObjCObjectPointerType() || isNullPtrType()); |
7101 | } |
7102 | |
7103 | inline bool Type::hasObjCPointerRepresentation() const { |
7104 | return isObjCObjectPointerType(); |
7105 | } |
7106 | |
7107 | inline const Type *Type::getBaseElementTypeUnsafe() const { |
7108 | const Type *type = this; |
7109 | while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe()) |
7110 | type = arrayType->getElementType().getTypePtr(); |
7111 | return type; |
7112 | } |
7113 | |
7114 | inline const Type *Type::getPointeeOrArrayElementType() const { |
7115 | const Type *type = this; |
7116 | if (type->isAnyPointerType()) |
7117 | return type->getPointeeType().getTypePtr(); |
7118 | else if (type->isArrayType()) |
7119 | return type->getBaseElementTypeUnsafe(); |
7120 | return type; |
7121 | } |
7122 | |
7123 | |
7124 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7125 | LangAS AS) { |
7126 | PD.AddTaggedVal(static_cast<std::underlying_type_t<LangAS>>(AS), |
7127 | DiagnosticsEngine::ArgumentKind::ak_addrspace); |
7128 | return PD; |
7129 | } |
7130 | |
7131 | |
7132 | |
7133 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7134 | Qualifiers Q) { |
7135 | PD.AddTaggedVal(Q.getAsOpaqueValue(), |
7136 | DiagnosticsEngine::ArgumentKind::ak_qual); |
7137 | return PD; |
7138 | } |
7139 | |
7140 | |
7141 | |
7142 | inline const StreamingDiagnostic &operator<<(const StreamingDiagnostic &PD, |
7143 | QualType T) { |
7144 | PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), |
7145 | DiagnosticsEngine::ak_qualtype); |
7146 | return PD; |
7147 | } |
7148 | |
7149 | |
7150 | |
7151 | template <typename T> |
7152 | using TypeIsArrayType = |
7153 | std::integral_constant<bool, std::is_same<T, ArrayType>::value || |
7154 | std::is_base_of<ArrayType, T>::value>; |
7155 | |
7156 | |
7157 | template <typename T> const T *Type::getAs() const { |
7158 | static_assert(!TypeIsArrayType<T>::value, |
7159 | "ArrayType cannot be used with getAs!"); |
7160 | |
7161 | |
7162 | if (const auto *Ty = dyn_cast<T>(this)) |
7163 | return Ty; |
7164 | |
7165 | |
7166 | if (!isa<T>(CanonicalType)) |
7167 | return nullptr; |
7168 | |
7169 | |
7170 | |
7171 | return cast<T>(getUnqualifiedDesugaredType()); |
7172 | } |
7173 | |
7174 | template <typename T> const T *Type::getAsAdjusted() const { |
7175 | static_assert(!TypeIsArrayType<T>::value, "ArrayType cannot be used with getAsAdjusted!"); |
7176 | |
7177 | |
7178 | if (const auto *Ty = dyn_cast<T>(this)) |
7179 | return Ty; |
7180 | |
7181 | |
7182 | if (!isa<T>(CanonicalType)) |
7183 | return nullptr; |
7184 | |
7185 | |
7186 | |
7187 | const Type *Ty = this; |
7188 | while (Ty) { |
7189 | if (const auto *A = dyn_cast<AttributedType>(Ty)) |
7190 | Ty = A->getModifiedType().getTypePtr(); |
7191 | else if (const auto *E = dyn_cast<ElaboratedType>(Ty)) |
7192 | Ty = E->desugar().getTypePtr(); |
7193 | else if (const auto *P = dyn_cast<ParenType>(Ty)) |
7194 | Ty = P->desugar().getTypePtr(); |
7195 | else if (const auto *A = dyn_cast<AdjustedType>(Ty)) |
7196 | Ty = A->desugar().getTypePtr(); |
7197 | else if (const auto *M = dyn_cast<MacroQualifiedType>(Ty)) |
7198 | Ty = M->desugar().getTypePtr(); |
7199 | else |
7200 | break; |
7201 | } |
7202 | |
7203 | |
7204 | |
7205 | return dyn_cast<T>(Ty); |
7206 | } |
7207 | |
7208 | inline const ArrayType *Type::getAsArrayTypeUnsafe() const { |
7209 | |
7210 | if (const auto *arr = dyn_cast<ArrayType>(this)) |
7211 | return arr; |
7212 | |
7213 | |
7214 | if (!isa<ArrayType>(CanonicalType)) |
7215 | return nullptr; |
7216 | |
7217 | |
7218 | |
7219 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7220 | } |
7221 | |
7222 | template <typename T> const T *Type::castAs() const { |
7223 | static_assert(!TypeIsArrayType<T>::value, |
7224 | "ArrayType cannot be used with castAs!"); |
7225 | |
7226 | if (const auto *ty = dyn_cast<T>(this)) return ty; |
7227 | assert(isa<T>(CanonicalType)); |
7228 | return cast<T>(getUnqualifiedDesugaredType()); |
7229 | } |
7230 | |
7231 | inline const ArrayType *Type::castAsArrayTypeUnsafe() const { |
7232 | assert(isa<ArrayType>(CanonicalType)); |
7233 | if (const auto *arr = dyn_cast<ArrayType>(this)) return arr; |
7234 | return cast<ArrayType>(getUnqualifiedDesugaredType()); |
7235 | } |
7236 | |
7237 | DecayedType::DecayedType(QualType OriginalType, QualType DecayedPtr, |
7238 | QualType CanonicalPtr) |
7239 | : AdjustedType(Decayed, OriginalType, DecayedPtr, CanonicalPtr) { |
7240 | #ifndef NDEBUG |
7241 | QualType Adjusted = getAdjustedType(); |
7242 | (void)AttributedType::stripOuterNullability(Adjusted); |
7243 | assert(isa<PointerType>(Adjusted)); |
7244 | #endif |
7245 | } |
7246 | |
7247 | QualType DecayedType::getPointeeType() const { |
7248 | QualType Decayed = getDecayedType(); |
7249 | (void)AttributedType::stripOuterNullability(Decayed); |
7250 | return cast<PointerType>(Decayed)->getPointeeType(); |
7251 | } |
7252 | |
7253 | |
7254 | |
7255 | |
7256 | |
7257 | void FixedPointValueToString(SmallVectorImpl<char> &Str, llvm::APSInt Val, |
7258 | unsigned Scale); |
7259 | |
7260 | } |
7261 | |
7262 | #endif // LLVM_CLANG_AST_TYPE_H |