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 "clang/AST/APValue.h" |
36 | #include "clang/AST/ASTContext.h" |
37 | #include "clang/AST/ASTDiagnostic.h" |
38 | #include "clang/AST/ASTLambda.h" |
39 | #include "clang/AST/CharUnits.h" |
40 | #include "clang/AST/Expr.h" |
41 | #include "clang/AST/OSLog.h" |
42 | #include "clang/AST/RecordLayout.h" |
43 | #include "clang/AST/StmtVisitor.h" |
44 | #include "clang/AST/TypeLoc.h" |
45 | #include "clang/Basic/Builtins.h" |
46 | #include "clang/Basic/FixedPoint.h" |
47 | #include "clang/Basic/TargetInfo.h" |
48 | #include "llvm/Support/SaveAndRestore.h" |
49 | #include "llvm/Support/raw_ostream.h" |
50 | #include <cstring> |
51 | #include <functional> |
52 | |
53 | #define DEBUG_TYPE "exprconstant" |
54 | |
55 | using namespace clang; |
56 | using llvm::APSInt; |
57 | using llvm::APFloat; |
58 | |
59 | static bool IsGlobalLValue(APValue::LValueBase B); |
60 | |
61 | namespace { |
62 | struct LValue; |
63 | struct CallStackFrame; |
64 | struct EvalInfo; |
65 | |
66 | static QualType getType(APValue::LValueBase B) { |
67 | if (!B) return QualType(); |
68 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | for (auto *Redecl = cast<ValueDecl>(D->getMostRecentDecl()); Redecl; |
77 | Redecl = cast_or_null<ValueDecl>(Redecl->getPreviousDecl())) { |
78 | QualType T = Redecl->getType(); |
79 | if (!T->isIncompleteArrayType()) |
80 | return T; |
81 | } |
82 | return D->getType(); |
83 | } |
84 | |
85 | const Expr *Base = B.get<const Expr*>(); |
86 | |
87 | |
88 | |
89 | if (const MaterializeTemporaryExpr *MTE = |
90 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
91 | SmallVector<const Expr *, 2> CommaLHSs; |
92 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
93 | const Expr *Temp = MTE->GetTemporaryExpr(); |
94 | const Expr *Inner = Temp->skipRValueSubobjectAdjustments(CommaLHSs, |
95 | Adjustments); |
96 | |
97 | |
98 | if (!Adjustments.empty()) |
99 | return Inner->getType(); |
100 | } |
101 | |
102 | return Base->getType(); |
103 | } |
104 | |
105 | |
106 | |
107 | static |
108 | APValue::BaseOrMemberType getAsBaseOrMember(APValue::LValuePathEntry E) { |
109 | APValue::BaseOrMemberType Value; |
110 | Value.setFromOpaqueValue(E.BaseOrMember); |
111 | return Value; |
112 | } |
113 | |
114 | |
115 | |
116 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
117 | return dyn_cast<FieldDecl>(getAsBaseOrMember(E).getPointer()); |
118 | } |
119 | |
120 | |
121 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
122 | return dyn_cast<CXXRecordDecl>(getAsBaseOrMember(E).getPointer()); |
123 | } |
124 | |
125 | |
126 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
127 | return getAsBaseOrMember(E).getInt(); |
128 | } |
129 | |
130 | |
131 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
132 | const FunctionDecl *Callee = CE->getDirectCallee(); |
133 | return Callee ? Callee->getAttr<AllocSizeAttr>() : nullptr; |
134 | } |
135 | |
136 | |
137 | |
138 | |
139 | |
140 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
141 | if (!E->getType()->isPointerType()) |
142 | return nullptr; |
143 | |
144 | E = E->IgnoreParens(); |
145 | |
146 | |
147 | |
148 | if (const auto *FE = dyn_cast<FullExpr>(E)) |
149 | E = FE->getSubExpr()->IgnoreParens(); |
150 | |
151 | if (const auto *Cast = dyn_cast<CastExpr>(E)) |
152 | E = Cast->getSubExpr()->IgnoreParens(); |
153 | |
154 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
155 | return getAllocSizeAttr(CE) ? CE : nullptr; |
156 | return nullptr; |
157 | } |
158 | |
159 | |
160 | |
161 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
162 | const auto *E = Base.dyn_cast<const Expr *>(); |
163 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
164 | } |
165 | |
166 | |
167 | |
168 | |
169 | static const uint64_t AssumedSizeForUnsizedArray = |
170 | std::numeric_limits<uint64_t>::max() / 2; |
171 | |
172 | |
173 | |
174 | |
175 | |
176 | static unsigned |
177 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
178 | ArrayRef<APValue::LValuePathEntry> Path, |
179 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
180 | bool &FirstEntryIsUnsizedArray) { |
181 | |
182 | |
183 | (0) . __assert_fail ("!isBaseAnAllocSizeCall(Base) && \"Unsized arrays shouldn't appear here\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 184, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isBaseAnAllocSizeCall(Base) && |
184 | (0) . __assert_fail ("!isBaseAnAllocSizeCall(Base) && \"Unsized arrays shouldn't appear here\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 184, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unsized arrays shouldn't appear here"); |
185 | unsigned MostDerivedLength = 0; |
186 | Type = getType(Base); |
187 | |
188 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
189 | if (Type->isArrayType()) { |
190 | const ArrayType *AT = Ctx.getAsArrayType(Type); |
191 | Type = AT->getElementType(); |
192 | MostDerivedLength = I + 1; |
193 | IsArray = true; |
194 | |
195 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) { |
196 | ArraySize = CAT->getSize().getZExtValue(); |
197 | } else { |
198 | (0) . __assert_fail ("I == 0 && \"unexpected unsized array designator\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 198, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I == 0 && "unexpected unsized array designator"); |
199 | FirstEntryIsUnsizedArray = true; |
200 | ArraySize = AssumedSizeForUnsizedArray; |
201 | } |
202 | } else if (Type->isAnyComplexType()) { |
203 | const ComplexType *CT = Type->castAs<ComplexType>(); |
204 | Type = CT->getElementType(); |
205 | ArraySize = 2; |
206 | MostDerivedLength = I + 1; |
207 | IsArray = true; |
208 | } else if (const FieldDecl *FD = getAsField(Path[I])) { |
209 | Type = FD->getType(); |
210 | ArraySize = 0; |
211 | MostDerivedLength = I + 1; |
212 | IsArray = false; |
213 | } else { |
214 | |
215 | ArraySize = 0; |
216 | IsArray = false; |
217 | } |
218 | } |
219 | return MostDerivedLength; |
220 | } |
221 | |
222 | |
223 | enum CheckSubobjectKind { |
224 | CSK_Base, CSK_Derived, CSK_Field, CSK_ArrayToPointer, CSK_ArrayIndex, |
225 | CSK_This, CSK_Real, CSK_Imag |
226 | }; |
227 | |
228 | |
229 | struct SubobjectDesignator { |
230 | |
231 | |
232 | |
233 | unsigned Invalid : 1; |
234 | |
235 | |
236 | unsigned IsOnePastTheEnd : 1; |
237 | |
238 | |
239 | unsigned FirstEntryIsAnUnsizedArray : 1; |
240 | |
241 | |
242 | unsigned MostDerivedIsArrayElement : 1; |
243 | |
244 | |
245 | |
246 | unsigned MostDerivedPathLength : 28; |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | uint64_t MostDerivedArraySize; |
256 | |
257 | |
258 | QualType MostDerivedType; |
259 | |
260 | typedef APValue::LValuePathEntry PathEntry; |
261 | |
262 | |
263 | SmallVector<PathEntry, 8> Entries; |
264 | |
265 | SubobjectDesignator() : Invalid(true) {} |
266 | |
267 | explicit SubobjectDesignator(QualType T) |
268 | : Invalid(false), IsOnePastTheEnd(false), |
269 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
270 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
271 | MostDerivedType(T) {} |
272 | |
273 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
274 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
275 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
276 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
277 | (0) . __assert_fail ("V.isLValue() && \"Non-LValue used to make an LValue designator?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 277, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V.isLValue() && "Non-LValue used to make an LValue designator?"); |
278 | if (!Invalid) { |
279 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
280 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
281 | Entries.insert(Entries.end(), VEntries.begin(), VEntries.end()); |
282 | if (V.getLValueBase()) { |
283 | bool IsArray = false; |
284 | bool FirstIsUnsizedArray = false; |
285 | MostDerivedPathLength = findMostDerivedSubobject( |
286 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
287 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
288 | MostDerivedIsArrayElement = IsArray; |
289 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
290 | } |
291 | } |
292 | } |
293 | |
294 | void setInvalid() { |
295 | Invalid = true; |
296 | Entries.clear(); |
297 | } |
298 | |
299 | |
300 | |
301 | bool isMostDerivedAnUnsizedArray() const { |
302 | (0) . __assert_fail ("!Invalid && \"Calling this makes no sense on invalid designators\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 302, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Invalid && "Calling this makes no sense on invalid designators"); |
303 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
304 | } |
305 | |
306 | |
307 | |
308 | uint64_t getMostDerivedArraySize() const { |
309 | (0) . __assert_fail ("!isMostDerivedAnUnsizedArray() && \"Unsized array has no size\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 309, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size"); |
310 | return MostDerivedArraySize; |
311 | } |
312 | |
313 | |
314 | bool isOnePastTheEnd() const { |
315 | assert(!Invalid); |
316 | if (IsOnePastTheEnd) |
317 | return true; |
318 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
319 | Entries[MostDerivedPathLength - 1].ArrayIndex == MostDerivedArraySize) |
320 | return true; |
321 | return false; |
322 | } |
323 | |
324 | |
325 | |
326 | |
327 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
328 | if (Invalid || isMostDerivedAnUnsizedArray()) |
329 | return {0, 0}; |
330 | |
331 | |
332 | |
333 | |
334 | bool IsArray = MostDerivedPathLength == Entries.size() && |
335 | MostDerivedIsArrayElement; |
336 | uint64_t ArrayIndex = |
337 | IsArray ? Entries.back().ArrayIndex : (uint64_t)IsOnePastTheEnd; |
338 | uint64_t ArraySize = |
339 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
340 | return {ArrayIndex, ArraySize - ArrayIndex}; |
341 | } |
342 | |
343 | |
344 | bool isValidSubobject() const { |
345 | if (Invalid) |
346 | return false; |
347 | return !isOnePastTheEnd(); |
348 | } |
349 | |
350 | |
351 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
352 | |
353 | |
354 | QualType getType(ASTContext &Ctx) const { |
355 | (0) . __assert_fail ("!Invalid && \"invalid designator has no subobject type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 355, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Invalid && "invalid designator has no subobject type"); |
356 | return MostDerivedPathLength == Entries.size() |
357 | ? MostDerivedType |
358 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
359 | } |
360 | |
361 | |
362 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
363 | PathEntry Entry; |
364 | Entry.ArrayIndex = 0; |
365 | Entries.push_back(Entry); |
366 | |
367 | |
368 | MostDerivedType = CAT->getElementType(); |
369 | MostDerivedIsArrayElement = true; |
370 | MostDerivedArraySize = CAT->getSize().getZExtValue(); |
371 | MostDerivedPathLength = Entries.size(); |
372 | } |
373 | |
374 | |
375 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
376 | PathEntry Entry; |
377 | Entry.ArrayIndex = 0; |
378 | Entries.push_back(Entry); |
379 | |
380 | MostDerivedType = ElemTy; |
381 | MostDerivedIsArrayElement = true; |
382 | |
383 | |
384 | |
385 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
386 | MostDerivedPathLength = Entries.size(); |
387 | } |
388 | |
389 | |
390 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
391 | PathEntry Entry; |
392 | APValue::BaseOrMemberType Value(D, Virtual); |
393 | Entry.BaseOrMember = Value.getOpaqueValue(); |
394 | Entries.push_back(Entry); |
395 | |
396 | |
397 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
398 | MostDerivedType = FD->getType(); |
399 | MostDerivedIsArrayElement = false; |
400 | MostDerivedArraySize = 0; |
401 | MostDerivedPathLength = Entries.size(); |
402 | } |
403 | } |
404 | |
405 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
406 | PathEntry Entry; |
407 | Entry.ArrayIndex = Imag; |
408 | Entries.push_back(Entry); |
409 | |
410 | |
411 | |
412 | MostDerivedType = EltTy; |
413 | MostDerivedIsArrayElement = true; |
414 | MostDerivedArraySize = 2; |
415 | MostDerivedPathLength = Entries.size(); |
416 | } |
417 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
418 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
419 | const APSInt &N); |
420 | |
421 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
422 | if (Invalid || !N) return; |
423 | uint64_t TruncatedN = N.extOrTrunc(64).getZExtValue(); |
424 | if (isMostDerivedAnUnsizedArray()) { |
425 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
426 | |
427 | |
428 | |
429 | Entries.back().ArrayIndex += TruncatedN; |
430 | return; |
431 | } |
432 | |
433 | |
434 | |
435 | |
436 | bool IsArray = MostDerivedPathLength == Entries.size() && |
437 | MostDerivedIsArrayElement; |
438 | uint64_t ArrayIndex = |
439 | IsArray ? Entries.back().ArrayIndex : (uint64_t)IsOnePastTheEnd; |
440 | uint64_t ArraySize = |
441 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
442 | |
443 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
444 | |
445 | |
446 | N = N.extend(std::max<unsigned>(N.getBitWidth() + 1, 65)); |
447 | (llvm::APInt&)N += ArrayIndex; |
448 | (0) . __assert_fail ("N.ugt(ArraySize) && \"bounds check failed for in-bounds index\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 448, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index"); |
449 | diagnosePointerArithmetic(Info, E, N); |
450 | setInvalid(); |
451 | return; |
452 | } |
453 | |
454 | ArrayIndex += TruncatedN; |
455 | (0) . __assert_fail ("ArrayIndex <= ArraySize && \"bounds check succeeded for out-of-bounds index\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 456, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ArrayIndex <= ArraySize && |
456 | (0) . __assert_fail ("ArrayIndex <= ArraySize && \"bounds check succeeded for out-of-bounds index\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 456, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "bounds check succeeded for out-of-bounds index"); |
457 | |
458 | if (IsArray) |
459 | Entries.back().ArrayIndex = ArrayIndex; |
460 | else |
461 | IsOnePastTheEnd = (ArrayIndex != 0); |
462 | } |
463 | }; |
464 | |
465 | |
466 | struct CallStackFrame { |
467 | EvalInfo &Info; |
468 | |
469 | |
470 | CallStackFrame *Caller; |
471 | |
472 | |
473 | const FunctionDecl *Callee; |
474 | |
475 | |
476 | const LValue *This; |
477 | |
478 | |
479 | |
480 | APValue *Arguments; |
481 | |
482 | |
483 | |
484 | typedef std::pair<const void *, unsigned> MapKeyTy; |
485 | typedef std::map<MapKeyTy, APValue> MapTy; |
486 | |
487 | MapTy Temporaries; |
488 | |
489 | |
490 | SourceLocation CallLoc; |
491 | |
492 | |
493 | unsigned Index; |
494 | |
495 | |
496 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
497 | unsigned CurTempVersion = TempVersionStack.back(); |
498 | |
499 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
500 | |
501 | void pushTempVersion() { |
502 | TempVersionStack.push_back(++CurTempVersion); |
503 | } |
504 | |
505 | void popTempVersion() { |
506 | TempVersionStack.pop_back(); |
507 | } |
508 | |
509 | |
510 | |
511 | |
512 | |
513 | |
514 | |
515 | |
516 | |
517 | llvm::DenseMap<const VarDecl *, FieldDecl *> LambdaCaptureFields; |
518 | FieldDecl *LambdaThisCaptureField; |
519 | |
520 | CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
521 | const FunctionDecl *Callee, const LValue *This, |
522 | APValue *Arguments); |
523 | ~CallStackFrame(); |
524 | |
525 | |
526 | APValue *getTemporary(const void *Key, unsigned Version) { |
527 | MapKeyTy KV(Key, Version); |
528 | auto LB = Temporaries.lower_bound(KV); |
529 | if (LB != Temporaries.end() && LB->first == KV) |
530 | return &LB->second; |
531 | |
532 | |
533 | (0) . __assert_fail ("(LB == Temporaries.end() || LB->first.first != Key) && (LB == Temporaries.begin() || std..prev(LB)->first.first != Key) && \"Element with key 'Key' found in map\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 535, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((LB == Temporaries.end() || LB->first.first != Key) && |
534 | (0) . __assert_fail ("(LB == Temporaries.end() || LB->first.first != Key) && (LB == Temporaries.begin() || std..prev(LB)->first.first != Key) && \"Element with key 'Key' found in map\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 535, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (LB == Temporaries.begin() || std::prev(LB)->first.first != Key) && |
535 | (0) . __assert_fail ("(LB == Temporaries.end() || LB->first.first != Key) && (LB == Temporaries.begin() || std..prev(LB)->first.first != Key) && \"Element with key 'Key' found in map\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 535, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Element with key 'Key' found in map"); |
536 | return nullptr; |
537 | } |
538 | |
539 | |
540 | APValue *getCurrentTemporary(const void *Key) { |
541 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
542 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
543 | return &std::prev(UB)->second; |
544 | return nullptr; |
545 | } |
546 | |
547 | |
548 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
549 | auto UB = Temporaries.upper_bound(MapKeyTy(Key, UINT_MAX)); |
550 | if (UB != Temporaries.begin() && std::prev(UB)->first.first == Key) |
551 | return std::prev(UB)->first.second; |
552 | return 0; |
553 | } |
554 | |
555 | APValue &createTemporary(const void *Key, bool IsLifetimeExtended); |
556 | }; |
557 | |
558 | |
559 | class ThisOverrideRAII { |
560 | public: |
561 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
562 | : Frame(Frame), OldThis(Frame.This) { |
563 | if (Enable) |
564 | Frame.This = NewThis; |
565 | } |
566 | ~ThisOverrideRAII() { |
567 | Frame.This = OldThis; |
568 | } |
569 | private: |
570 | CallStackFrame &Frame; |
571 | const LValue *OldThis; |
572 | }; |
573 | |
574 | |
575 | |
576 | class OptionalDiagnostic { |
577 | PartialDiagnostic *Diag; |
578 | |
579 | public: |
580 | explicit OptionalDiagnostic(PartialDiagnostic *Diag = nullptr) |
581 | : Diag(Diag) {} |
582 | |
583 | template<typename T> |
584 | OptionalDiagnostic &operator<<(const T &v) { |
585 | if (Diag) |
586 | *Diag << v; |
587 | return *this; |
588 | } |
589 | |
590 | OptionalDiagnostic &operator<<(const APSInt &I) { |
591 | if (Diag) { |
592 | SmallVector<char, 32> Buffer; |
593 | I.toString(Buffer); |
594 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
595 | } |
596 | return *this; |
597 | } |
598 | |
599 | OptionalDiagnostic &operator<<(const APFloat &F) { |
600 | if (Diag) { |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | unsigned precision = |
608 | llvm::APFloat::semanticsPrecision(F.getSemantics()); |
609 | precision = (precision * 59 + 195) / 196; |
610 | SmallVector<char, 32> Buffer; |
611 | F.toString(Buffer, precision); |
612 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
613 | } |
614 | return *this; |
615 | } |
616 | |
617 | OptionalDiagnostic &operator<<(const APFixedPoint &FX) { |
618 | if (Diag) { |
619 | SmallVector<char, 32> Buffer; |
620 | FX.toString(Buffer); |
621 | *Diag << StringRef(Buffer.data(), Buffer.size()); |
622 | } |
623 | return *this; |
624 | } |
625 | }; |
626 | |
627 | |
628 | class Cleanup { |
629 | llvm::PointerIntPair<APValue*, 1, bool> Value; |
630 | |
631 | public: |
632 | Cleanup(APValue *Val, bool IsLifetimeExtended) |
633 | : Value(Val, IsLifetimeExtended) {} |
634 | |
635 | bool isLifetimeExtended() const { return Value.getInt(); } |
636 | void endLifetime() { |
637 | *Value.getPointer() = APValue(); |
638 | } |
639 | }; |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | |
650 | |
651 | |
652 | |
653 | |
654 | |
655 | struct EvalInfo { |
656 | ASTContext &Ctx; |
657 | |
658 | |
659 | Expr::EvalStatus &EvalStatus; |
660 | |
661 | |
662 | CallStackFrame *CurrentCall; |
663 | |
664 | |
665 | unsigned CallStackDepth; |
666 | |
667 | |
668 | unsigned NextCallIndex; |
669 | |
670 | |
671 | |
672 | |
673 | unsigned StepsLeft; |
674 | |
675 | |
676 | |
677 | CallStackFrame BottomFrame; |
678 | |
679 | |
680 | |
681 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
682 | |
683 | |
684 | |
685 | APValue::LValueBase EvaluatingDecl; |
686 | |
687 | |
688 | |
689 | APValue *EvaluatingDeclValue; |
690 | |
691 | |
692 | |
693 | typedef std::pair<APValue::LValueBase, std::pair<unsigned, unsigned>> |
694 | EvaluatingObject; |
695 | |
696 | |
697 | |
698 | llvm::DenseSet<EvaluatingObject> EvaluatingConstructors; |
699 | |
700 | struct EvaluatingConstructorRAII { |
701 | EvalInfo &EI; |
702 | EvaluatingObject Object; |
703 | bool DidInsert; |
704 | EvaluatingConstructorRAII(EvalInfo &EI, EvaluatingObject Object) |
705 | : EI(EI), Object(Object) { |
706 | DidInsert = EI.EvaluatingConstructors.insert(Object).second; |
707 | } |
708 | ~EvaluatingConstructorRAII() { |
709 | if (DidInsert) EI.EvaluatingConstructors.erase(Object); |
710 | } |
711 | }; |
712 | |
713 | bool isEvaluatingConstructor(APValue::LValueBase Decl, unsigned CallIndex, |
714 | unsigned Version) { |
715 | return EvaluatingConstructors.count( |
716 | EvaluatingObject(Decl, {CallIndex, Version})); |
717 | } |
718 | |
719 | |
720 | |
721 | uint64_t ArrayInitIndex = -1; |
722 | |
723 | |
724 | |
725 | bool HasActiveDiagnostic; |
726 | |
727 | |
728 | |
729 | bool HasFoldFailureDiagnostic; |
730 | |
731 | |
732 | bool IsSpeculativelyEvaluating; |
733 | |
734 | |
735 | |
736 | bool InConstantContext; |
737 | |
738 | enum EvaluationMode { |
739 | |
740 | |
741 | EM_ConstantExpression, |
742 | |
743 | |
744 | |
745 | |
746 | |
747 | EM_PotentialConstantExpression, |
748 | |
749 | |
750 | |
751 | EM_ConstantFold, |
752 | |
753 | |
754 | |
755 | |
756 | EM_EvaluateForOverflow, |
757 | |
758 | |
759 | |
760 | EM_IgnoreSideEffects, |
761 | |
762 | |
763 | |
764 | |
765 | |
766 | |
767 | EM_ConstantExpressionUnevaluated, |
768 | |
769 | |
770 | |
771 | |
772 | |
773 | |
774 | |
775 | |
776 | EM_PotentialConstantExpressionUnevaluated, |
777 | } EvalMode; |
778 | |
779 | |
780 | |
781 | bool checkingPotentialConstantExpression() const { |
782 | return EvalMode == EM_PotentialConstantExpression || |
783 | EvalMode == EM_PotentialConstantExpressionUnevaluated; |
784 | } |
785 | |
786 | |
787 | |
788 | |
789 | bool checkingForOverflow() { return EvalMode == EM_EvaluateForOverflow; } |
790 | |
791 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
792 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
793 | CallStackDepth(0), NextCallIndex(1), |
794 | StepsLeft(getLangOpts().ConstexprStepLimit), |
795 | BottomFrame(*this, SourceLocation(), nullptr, nullptr, nullptr), |
796 | EvaluatingDecl((const ValueDecl *)nullptr), |
797 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
798 | HasFoldFailureDiagnostic(false), IsSpeculativelyEvaluating(false), |
799 | InConstantContext(false), EvalMode(Mode) {} |
800 | |
801 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value) { |
802 | EvaluatingDecl = Base; |
803 | EvaluatingDeclValue = &Value; |
804 | EvaluatingConstructors.insert({Base, {0, 0}}); |
805 | } |
806 | |
807 | const LangOptions &getLangOpts() const { return Ctx.getLangOpts(); } |
808 | |
809 | bool CheckCallLimit(SourceLocation Loc) { |
810 | |
811 | |
812 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
813 | return false; |
814 | if (NextCallIndex == 0) { |
815 | |
816 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
817 | return false; |
818 | } |
819 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
820 | return true; |
821 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
822 | << getLangOpts().ConstexprCallDepth; |
823 | return false; |
824 | } |
825 | |
826 | CallStackFrame *getCallFrame(unsigned CallIndex) { |
827 | (0) . __assert_fail ("CallIndex && \"no call index in getCallFrame\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 827, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CallIndex && "no call index in getCallFrame"); |
828 | |
829 | |
830 | CallStackFrame *Frame = CurrentCall; |
831 | while (Frame->Index > CallIndex) |
832 | Frame = Frame->Caller; |
833 | return (Frame->Index == CallIndex) ? Frame : nullptr; |
834 | } |
835 | |
836 | bool nextStep(const Stmt *S) { |
837 | if (!StepsLeft) { |
838 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
839 | return false; |
840 | } |
841 | --StepsLeft; |
842 | return true; |
843 | } |
844 | |
845 | private: |
846 | |
847 | PartialDiagnostic &addDiag(SourceLocation Loc, diag::kind DiagId) { |
848 | PartialDiagnostic PD(DiagId, Ctx.getDiagAllocator()); |
849 | EvalStatus.Diag->push_back(std::make_pair(Loc, PD)); |
850 | return EvalStatus.Diag->back().second; |
851 | } |
852 | |
853 | |
854 | void addCallStack(unsigned Limit); |
855 | |
856 | private: |
857 | OptionalDiagnostic Diag(SourceLocation Loc, diag::kind DiagId, |
858 | unsigned , bool IsCCEDiag) { |
859 | |
860 | if (EvalStatus.Diag) { |
861 | |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | if (!EvalStatus.Diag->empty()) { |
868 | switch (EvalMode) { |
869 | case EM_ConstantFold: |
870 | case EM_IgnoreSideEffects: |
871 | case EM_EvaluateForOverflow: |
872 | if (!HasFoldFailureDiagnostic) |
873 | break; |
874 | |
875 | LLVM_FALLTHROUGH; |
876 | case EM_ConstantExpression: |
877 | case EM_PotentialConstantExpression: |
878 | case EM_ConstantExpressionUnevaluated: |
879 | case EM_PotentialConstantExpressionUnevaluated: |
880 | HasActiveDiagnostic = false; |
881 | return OptionalDiagnostic(); |
882 | } |
883 | } |
884 | |
885 | unsigned CallStackNotes = CallStackDepth - 1; |
886 | unsigned Limit = Ctx.getDiagnostics().getConstexprBacktraceLimit(); |
887 | if (Limit) |
888 | CallStackNotes = std::min(CallStackNotes, Limit + 1); |
889 | if (checkingPotentialConstantExpression()) |
890 | CallStackNotes = 0; |
891 | |
892 | HasActiveDiagnostic = true; |
893 | HasFoldFailureDiagnostic = !IsCCEDiag; |
894 | EvalStatus.Diag->clear(); |
895 | EvalStatus.Diag->reserve(1 + ExtraNotes + CallStackNotes); |
896 | addDiag(Loc, DiagId); |
897 | if (!checkingPotentialConstantExpression()) |
898 | addCallStack(Limit); |
899 | return OptionalDiagnostic(&(*EvalStatus.Diag)[0].second); |
900 | } |
901 | HasActiveDiagnostic = false; |
902 | return OptionalDiagnostic(); |
903 | } |
904 | public: |
905 | |
906 | OptionalDiagnostic |
907 | FFDiag(SourceLocation Loc, |
908 | diag::kind DiagId = diag::note_invalid_subexpr_in_const_expr, |
909 | unsigned = 0) { |
910 | return Diag(Loc, DiagId, ExtraNotes, false); |
911 | } |
912 | |
913 | OptionalDiagnostic FFDiag(const Expr *E, diag::kind DiagId |
914 | = diag::note_invalid_subexpr_in_const_expr, |
915 | unsigned = 0) { |
916 | if (EvalStatus.Diag) |
917 | return Diag(E->getExprLoc(), DiagId, ExtraNotes, ); |
918 | HasActiveDiagnostic = false; |
919 | return OptionalDiagnostic(); |
920 | } |
921 | |
922 | |
923 | |
924 | |
925 | |
926 | |
927 | OptionalDiagnostic CCEDiag(SourceLocation Loc, diag::kind DiagId |
928 | = diag::note_invalid_subexpr_in_const_expr, |
929 | unsigned = 0) { |
930 | |
931 | |
932 | if (!EvalStatus.Diag || !EvalStatus.Diag->empty()) { |
933 | HasActiveDiagnostic = false; |
934 | return OptionalDiagnostic(); |
935 | } |
936 | return Diag(Loc, DiagId, ExtraNotes, true); |
937 | } |
938 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind DiagId |
939 | = diag::note_invalid_subexpr_in_const_expr, |
940 | unsigned = 0) { |
941 | return CCEDiag(E->getExprLoc(), DiagId, ExtraNotes); |
942 | } |
943 | |
944 | OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId) { |
945 | if (!HasActiveDiagnostic) |
946 | return OptionalDiagnostic(); |
947 | return OptionalDiagnostic(&addDiag(Loc, DiagId)); |
948 | } |
949 | |
950 | |
951 | void addNotes(ArrayRef<PartialDiagnosticAt> Diags) { |
952 | if (HasActiveDiagnostic) { |
953 | EvalStatus.Diag->insert(EvalStatus.Diag->end(), |
954 | Diags.begin(), Diags.end()); |
955 | } |
956 | } |
957 | |
958 | |
959 | |
960 | bool keepEvaluatingAfterSideEffect() { |
961 | switch (EvalMode) { |
962 | case EM_PotentialConstantExpression: |
963 | case EM_PotentialConstantExpressionUnevaluated: |
964 | case EM_EvaluateForOverflow: |
965 | case EM_IgnoreSideEffects: |
966 | return true; |
967 | |
968 | case EM_ConstantExpression: |
969 | case EM_ConstantExpressionUnevaluated: |
970 | case EM_ConstantFold: |
971 | return false; |
972 | } |
973 | llvm_unreachable("Missed EvalMode case"); |
974 | } |
975 | |
976 | |
977 | |
978 | bool noteSideEffect() { |
979 | EvalStatus.HasSideEffects = true; |
980 | return keepEvaluatingAfterSideEffect(); |
981 | } |
982 | |
983 | |
984 | bool keepEvaluatingAfterUndefinedBehavior() { |
985 | switch (EvalMode) { |
986 | case EM_EvaluateForOverflow: |
987 | case EM_IgnoreSideEffects: |
988 | case EM_ConstantFold: |
989 | return true; |
990 | |
991 | case EM_PotentialConstantExpression: |
992 | case EM_PotentialConstantExpressionUnevaluated: |
993 | case EM_ConstantExpression: |
994 | case EM_ConstantExpressionUnevaluated: |
995 | return false; |
996 | } |
997 | llvm_unreachable("Missed EvalMode case"); |
998 | } |
999 | |
1000 | |
1001 | |
1002 | |
1003 | bool noteUndefinedBehavior() { |
1004 | EvalStatus.HasUndefinedBehavior = true; |
1005 | return keepEvaluatingAfterUndefinedBehavior(); |
1006 | } |
1007 | |
1008 | |
1009 | |
1010 | bool keepEvaluatingAfterFailure() { |
1011 | if (!StepsLeft) |
1012 | return false; |
1013 | |
1014 | switch (EvalMode) { |
1015 | case EM_PotentialConstantExpression: |
1016 | case EM_PotentialConstantExpressionUnevaluated: |
1017 | case EM_EvaluateForOverflow: |
1018 | return true; |
1019 | |
1020 | case EM_ConstantExpression: |
1021 | case EM_ConstantExpressionUnevaluated: |
1022 | case EM_ConstantFold: |
1023 | case EM_IgnoreSideEffects: |
1024 | return false; |
1025 | } |
1026 | llvm_unreachable("Missed EvalMode case"); |
1027 | } |
1028 | |
1029 | |
1030 | |
1031 | |
1032 | |
1033 | |
1034 | |
1035 | |
1036 | |
1037 | |
1038 | |
1039 | LLVM_NODISCARD bool noteFailure() { |
1040 | |
1041 | |
1042 | |
1043 | |
1044 | |
1045 | |
1046 | |
1047 | bool KeepGoing = keepEvaluatingAfterFailure(); |
1048 | EvalStatus.HasSideEffects |= KeepGoing; |
1049 | return KeepGoing; |
1050 | } |
1051 | |
1052 | class ArrayInitLoopIndex { |
1053 | EvalInfo &Info; |
1054 | uint64_t OuterIndex; |
1055 | |
1056 | public: |
1057 | ArrayInitLoopIndex(EvalInfo &Info) |
1058 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
1059 | Info.ArrayInitIndex = 0; |
1060 | } |
1061 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
1062 | |
1063 | operator uint64_t&() { return Info.ArrayInitIndex; } |
1064 | }; |
1065 | }; |
1066 | |
1067 | |
1068 | struct FoldConstant { |
1069 | EvalInfo &Info; |
1070 | bool Enabled; |
1071 | bool HadNoPriorDiags; |
1072 | EvalInfo::EvaluationMode OldMode; |
1073 | |
1074 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
1075 | : Info(Info), |
1076 | Enabled(Enabled), |
1077 | HadNoPriorDiags(Info.EvalStatus.Diag && |
1078 | Info.EvalStatus.Diag->empty() && |
1079 | !Info.EvalStatus.HasSideEffects), |
1080 | OldMode(Info.EvalMode) { |
1081 | if (Enabled && |
1082 | (Info.EvalMode == EvalInfo::EM_ConstantExpression || |
1083 | Info.EvalMode == EvalInfo::EM_ConstantExpressionUnevaluated)) |
1084 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
1085 | } |
1086 | void keepDiagnostics() { Enabled = false; } |
1087 | ~FoldConstant() { |
1088 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
1089 | !Info.EvalStatus.HasSideEffects) |
1090 | Info.EvalStatus.Diag->clear(); |
1091 | Info.EvalMode = OldMode; |
1092 | } |
1093 | }; |
1094 | |
1095 | |
1096 | |
1097 | struct IgnoreSideEffectsRAII { |
1098 | EvalInfo &Info; |
1099 | EvalInfo::EvaluationMode OldMode; |
1100 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
1101 | : Info(Info), OldMode(Info.EvalMode) { |
1102 | if (!Info.checkingPotentialConstantExpression()) |
1103 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
1104 | } |
1105 | |
1106 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
1107 | }; |
1108 | |
1109 | |
1110 | |
1111 | class SpeculativeEvaluationRAII { |
1112 | EvalInfo *Info = nullptr; |
1113 | Expr::EvalStatus OldStatus; |
1114 | bool OldIsSpeculativelyEvaluating; |
1115 | |
1116 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
1117 | Info = Other.Info; |
1118 | OldStatus = Other.OldStatus; |
1119 | OldIsSpeculativelyEvaluating = Other.OldIsSpeculativelyEvaluating; |
1120 | Other.Info = nullptr; |
1121 | } |
1122 | |
1123 | void maybeRestoreState() { |
1124 | if (!Info) |
1125 | return; |
1126 | |
1127 | Info->EvalStatus = OldStatus; |
1128 | Info->IsSpeculativelyEvaluating = OldIsSpeculativelyEvaluating; |
1129 | } |
1130 | |
1131 | public: |
1132 | SpeculativeEvaluationRAII() = default; |
1133 | |
1134 | SpeculativeEvaluationRAII( |
1135 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
1136 | : Info(&Info), OldStatus(Info.EvalStatus), |
1137 | OldIsSpeculativelyEvaluating(Info.IsSpeculativelyEvaluating) { |
1138 | Info.EvalStatus.Diag = NewDiag; |
1139 | Info.IsSpeculativelyEvaluating = true; |
1140 | } |
1141 | |
1142 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
1143 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
1144 | moveFromAndCancel(std::move(Other)); |
1145 | } |
1146 | |
1147 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
1148 | maybeRestoreState(); |
1149 | moveFromAndCancel(std::move(Other)); |
1150 | return *this; |
1151 | } |
1152 | |
1153 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
1154 | }; |
1155 | |
1156 | |
1157 | |
1158 | template<bool IsFullExpression> |
1159 | class ScopeRAII { |
1160 | EvalInfo &Info; |
1161 | unsigned OldStackSize; |
1162 | public: |
1163 | ScopeRAII(EvalInfo &Info) |
1164 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
1165 | |
1166 | |
1167 | Info.CurrentCall->pushTempVersion(); |
1168 | } |
1169 | ~ScopeRAII() { |
1170 | |
1171 | |
1172 | cleanup(Info, OldStackSize); |
1173 | Info.CurrentCall->popTempVersion(); |
1174 | } |
1175 | private: |
1176 | static void cleanup(EvalInfo &Info, unsigned OldStackSize) { |
1177 | unsigned NewEnd = OldStackSize; |
1178 | for (unsigned I = OldStackSize, N = Info.CleanupStack.size(); |
1179 | I != N; ++I) { |
1180 | if (IsFullExpression && Info.CleanupStack[I].isLifetimeExtended()) { |
1181 | |
1182 | |
1183 | std::swap(Info.CleanupStack[I], Info.CleanupStack[NewEnd]); |
1184 | ++NewEnd; |
1185 | } else { |
1186 | |
1187 | Info.CleanupStack[I].endLifetime(); |
1188 | } |
1189 | } |
1190 | Info.CleanupStack.erase(Info.CleanupStack.begin() + NewEnd, |
1191 | Info.CleanupStack.end()); |
1192 | } |
1193 | }; |
1194 | typedef ScopeRAII<false> BlockScopeRAII; |
1195 | typedef ScopeRAII<true> FullExpressionRAII; |
1196 | } |
1197 | |
1198 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
1199 | CheckSubobjectKind CSK) { |
1200 | if (Invalid) |
1201 | return false; |
1202 | if (isOnePastTheEnd()) { |
1203 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
1204 | << CSK; |
1205 | setInvalid(); |
1206 | return false; |
1207 | } |
1208 | |
1209 | |
1210 | |
1211 | return true; |
1212 | } |
1213 | |
1214 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
1215 | const Expr *E) { |
1216 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
1217 | |
1218 | |
1219 | } |
1220 | |
1221 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
1222 | const Expr *E, |
1223 | const APSInt &N) { |
1224 | |
1225 | |
1226 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
1227 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1228 | << N << 0 |
1229 | << static_cast<unsigned>(getMostDerivedArraySize()); |
1230 | else |
1231 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1232 | << N << 1; |
1233 | setInvalid(); |
1234 | } |
1235 | |
1236 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceLocation CallLoc, |
1237 | const FunctionDecl *Callee, const LValue *This, |
1238 | APValue *Arguments) |
1239 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
1240 | Arguments(Arguments), CallLoc(CallLoc), Index(Info.NextCallIndex++) { |
1241 | Info.CurrentCall = this; |
1242 | ++Info.CallStackDepth; |
1243 | } |
1244 | |
1245 | CallStackFrame::~CallStackFrame() { |
1246 | (0) . __assert_fail ("Info.CurrentCall == this && \"calls retired out of order\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1246, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Info.CurrentCall == this && "calls retired out of order"); |
1247 | --Info.CallStackDepth; |
1248 | Info.CurrentCall = Caller; |
1249 | } |
1250 | |
1251 | APValue &CallStackFrame::createTemporary(const void *Key, |
1252 | bool IsLifetimeExtended) { |
1253 | unsigned Version = Info.CurrentCall->getTempVersion(); |
1254 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
1255 | (0) . __assert_fail ("Result.isUninit() && \"temporary created multiple times\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1255, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result.isUninit() && "temporary created multiple times"); |
1256 | Info.CleanupStack.push_back(Cleanup(&Result, IsLifetimeExtended)); |
1257 | return Result; |
1258 | } |
1259 | |
1260 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out); |
1261 | |
1262 | void EvalInfo::addCallStack(unsigned Limit) { |
1263 | |
1264 | unsigned ActiveCalls = CallStackDepth - 1; |
1265 | unsigned SkipStart = ActiveCalls, SkipEnd = SkipStart; |
1266 | if (Limit && Limit < ActiveCalls) { |
1267 | SkipStart = Limit / 2 + Limit % 2; |
1268 | SkipEnd = ActiveCalls - Limit / 2; |
1269 | } |
1270 | |
1271 | |
1272 | unsigned CallIdx = 0; |
1273 | for (CallStackFrame *Frame = CurrentCall; Frame != &BottomFrame; |
1274 | Frame = Frame->Caller, ++CallIdx) { |
1275 | |
1276 | if (CallIdx >= SkipStart && CallIdx < SkipEnd) { |
1277 | if (CallIdx == SkipStart) { |
1278 | |
1279 | addDiag(Frame->CallLoc, diag::note_constexpr_calls_suppressed) |
1280 | << unsigned(ActiveCalls - Limit); |
1281 | } |
1282 | continue; |
1283 | } |
1284 | |
1285 | |
1286 | |
1287 | if (auto *CD = dyn_cast_or_null<CXXConstructorDecl>(Frame->Callee)) { |
1288 | if (CD->isInheritingConstructor()) { |
1289 | addDiag(Frame->CallLoc, diag::note_constexpr_inherited_ctor_call_here) |
1290 | << CD->getParent(); |
1291 | continue; |
1292 | } |
1293 | } |
1294 | |
1295 | SmallVector<char, 128> Buffer; |
1296 | llvm::raw_svector_ostream Out(Buffer); |
1297 | describeCall(Frame, Out); |
1298 | addDiag(Frame->CallLoc, diag::note_constexpr_call_here) << Out.str(); |
1299 | } |
1300 | } |
1301 | |
1302 | |
1303 | enum AccessKinds { |
1304 | AK_Read, |
1305 | AK_Assign, |
1306 | AK_Increment, |
1307 | AK_Decrement |
1308 | }; |
1309 | |
1310 | namespace { |
1311 | struct ComplexValue { |
1312 | private: |
1313 | bool IsInt; |
1314 | |
1315 | public: |
1316 | APSInt IntReal, IntImag; |
1317 | APFloat FloatReal, FloatImag; |
1318 | |
1319 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
1320 | |
1321 | void makeComplexFloat() { IsInt = false; } |
1322 | bool isComplexFloat() const { return !IsInt; } |
1323 | APFloat &getComplexFloatReal() { return FloatReal; } |
1324 | APFloat &getComplexFloatImag() { return FloatImag; } |
1325 | |
1326 | void makeComplexInt() { IsInt = true; } |
1327 | bool isComplexInt() const { return IsInt; } |
1328 | APSInt &getComplexIntReal() { return IntReal; } |
1329 | APSInt &getComplexIntImag() { return IntImag; } |
1330 | |
1331 | void moveInto(APValue &v) const { |
1332 | if (isComplexFloat()) |
1333 | v = APValue(FloatReal, FloatImag); |
1334 | else |
1335 | v = APValue(IntReal, IntImag); |
1336 | } |
1337 | void setFrom(const APValue &v) { |
1338 | assert(v.isComplexFloat() || v.isComplexInt()); |
1339 | if (v.isComplexFloat()) { |
1340 | makeComplexFloat(); |
1341 | FloatReal = v.getComplexFloatReal(); |
1342 | FloatImag = v.getComplexFloatImag(); |
1343 | } else { |
1344 | makeComplexInt(); |
1345 | IntReal = v.getComplexIntReal(); |
1346 | IntImag = v.getComplexIntImag(); |
1347 | } |
1348 | } |
1349 | }; |
1350 | |
1351 | struct LValue { |
1352 | APValue::LValueBase Base; |
1353 | CharUnits Offset; |
1354 | SubobjectDesignator Designator; |
1355 | bool IsNullPtr : 1; |
1356 | bool InvalidBase : 1; |
1357 | |
1358 | const APValue::LValueBase getLValueBase() const { return Base; } |
1359 | CharUnits &getLValueOffset() { return Offset; } |
1360 | const CharUnits &getLValueOffset() const { return Offset; } |
1361 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
1362 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
1363 | bool isNullPointer() const { return IsNullPtr;} |
1364 | |
1365 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
1366 | unsigned getLValueVersion() const { return Base.getVersion(); } |
1367 | |
1368 | void moveInto(APValue &V) const { |
1369 | if (Designator.Invalid) |
1370 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
1371 | else { |
1372 | (0) . __assert_fail ("!InvalidBase && \"APValues can't handle invalid LValue bases\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1372, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!InvalidBase && "APValues can't handle invalid LValue bases"); |
1373 | V = APValue(Base, Offset, Designator.Entries, |
1374 | Designator.IsOnePastTheEnd, IsNullPtr); |
1375 | } |
1376 | } |
1377 | void setFrom(ASTContext &Ctx, const APValue &V) { |
1378 | (0) . __assert_fail ("V.isLValue() && \"Setting LValue from a non-LValue?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1378, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V.isLValue() && "Setting LValue from a non-LValue?"); |
1379 | Base = V.getLValueBase(); |
1380 | Offset = V.getLValueOffset(); |
1381 | InvalidBase = false; |
1382 | Designator = SubobjectDesignator(Ctx, V); |
1383 | IsNullPtr = V.isNullPointer(); |
1384 | } |
1385 | |
1386 | void set(APValue::LValueBase B, bool BInvalid = false) { |
1387 | #ifndef NDEBUG |
1388 | |
1389 | if (BInvalid) { |
1390 | const auto *E = B.get<const Expr *>(); |
1391 | (0) . __assert_fail ("(isa(E) || tryUnwrapAllocSizeCall(E)) && \"Unexpected type of invalid base\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1392, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
1392 | (0) . __assert_fail ("(isa(E) || tryUnwrapAllocSizeCall(E)) && \"Unexpected type of invalid base\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1392, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unexpected type of invalid base"); |
1393 | } |
1394 | #endif |
1395 | |
1396 | Base = B; |
1397 | Offset = CharUnits::fromQuantity(0); |
1398 | InvalidBase = BInvalid; |
1399 | Designator = SubobjectDesignator(getType(B)); |
1400 | IsNullPtr = false; |
1401 | } |
1402 | |
1403 | void setNull(QualType PointerTy, uint64_t TargetVal) { |
1404 | Base = (Expr *)nullptr; |
1405 | Offset = CharUnits::fromQuantity(TargetVal); |
1406 | InvalidBase = false; |
1407 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
1408 | IsNullPtr = true; |
1409 | } |
1410 | |
1411 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
1412 | set(B, true); |
1413 | } |
1414 | |
1415 | private: |
1416 | |
1417 | |
1418 | template <typename GenDiagType> |
1419 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
1420 | if (Designator.Invalid) |
1421 | return false; |
1422 | if (IsNullPtr) { |
1423 | GenDiag(); |
1424 | Designator.setInvalid(); |
1425 | return false; |
1426 | } |
1427 | return true; |
1428 | } |
1429 | |
1430 | public: |
1431 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
1432 | CheckSubobjectKind CSK) { |
1433 | return checkNullPointerDiagnosingWith([&Info, E, CSK] { |
1434 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
1435 | }); |
1436 | } |
1437 | |
1438 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
1439 | AccessKinds AK) { |
1440 | return checkNullPointerDiagnosingWith([&Info, E, AK] { |
1441 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
1442 | }); |
1443 | } |
1444 | |
1445 | |
1446 | |
1447 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
1448 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
1449 | Designator.checkSubobject(Info, E, CSK); |
1450 | } |
1451 | |
1452 | void addDecl(EvalInfo &Info, const Expr *E, |
1453 | const Decl *D, bool Virtual = false) { |
1454 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
1455 | Designator.addDeclUnchecked(D, Virtual); |
1456 | } |
1457 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
1458 | if (!Designator.Entries.empty()) { |
1459 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
1460 | Designator.setInvalid(); |
1461 | return; |
1462 | } |
1463 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) { |
1464 | isPointerType() || getType(Base)->isArrayType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1464, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
1465 | Designator.FirstEntryIsAnUnsizedArray = true; |
1466 | Designator.addUnsizedArrayUnchecked(ElemTy); |
1467 | } |
1468 | } |
1469 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
1470 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
1471 | Designator.addArrayUnchecked(CAT); |
1472 | } |
1473 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
1474 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
1475 | Designator.addComplexUnchecked(EltTy, Imag); |
1476 | } |
1477 | void clearIsNullPointer() { |
1478 | IsNullPtr = false; |
1479 | } |
1480 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
1481 | const APSInt &Index, CharUnits ElementSize) { |
1482 | |
1483 | |
1484 | if (!Index) |
1485 | return; |
1486 | |
1487 | |
1488 | |
1489 | |
1490 | uint64_t Offset64 = Offset.getQuantity(); |
1491 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
1492 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
1493 | Offset = CharUnits::fromQuantity(Offset64 + ElemSize64 * Index64); |
1494 | |
1495 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
1496 | Designator.adjustIndex(Info, E, Index); |
1497 | clearIsNullPointer(); |
1498 | } |
1499 | void adjustOffset(CharUnits N) { |
1500 | Offset += N; |
1501 | if (N.getQuantity()) |
1502 | clearIsNullPointer(); |
1503 | } |
1504 | }; |
1505 | |
1506 | struct MemberPtr { |
1507 | MemberPtr() {} |
1508 | explicit MemberPtr(const ValueDecl *Decl) : |
1509 | DeclAndIsDerivedMember(Decl, false), Path() {} |
1510 | |
1511 | |
1512 | |
1513 | const ValueDecl *getDecl() const { |
1514 | return DeclAndIsDerivedMember.getPointer(); |
1515 | } |
1516 | |
1517 | bool isDerivedMember() const { |
1518 | return DeclAndIsDerivedMember.getInt(); |
1519 | } |
1520 | |
1521 | const CXXRecordDecl *getContainingRecord() const { |
1522 | return cast<CXXRecordDecl>( |
1523 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
1524 | } |
1525 | |
1526 | void moveInto(APValue &V) const { |
1527 | V = APValue(getDecl(), isDerivedMember(), Path); |
1528 | } |
1529 | void setFrom(const APValue &V) { |
1530 | assert(V.isMemberPointer()); |
1531 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
1532 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
1533 | Path.clear(); |
1534 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
1535 | Path.insert(Path.end(), P.begin(), P.end()); |
1536 | } |
1537 | |
1538 | |
1539 | |
1540 | |
1541 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
1542 | |
1543 | |
1544 | SmallVector<const CXXRecordDecl*, 4> Path; |
1545 | |
1546 | |
1547 | |
1548 | bool castBack(const CXXRecordDecl *Class) { |
1549 | assert(!Path.empty()); |
1550 | const CXXRecordDecl *Expected; |
1551 | if (Path.size() >= 2) |
1552 | Expected = Path[Path.size() - 2]; |
1553 | else |
1554 | Expected = getContainingRecord(); |
1555 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | |
1561 | |
1562 | return false; |
1563 | } |
1564 | Path.pop_back(); |
1565 | return true; |
1566 | } |
1567 | |
1568 | bool castToDerived(const CXXRecordDecl *Derived) { |
1569 | if (!getDecl()) |
1570 | return true; |
1571 | if (!isDerivedMember()) { |
1572 | Path.push_back(Derived); |
1573 | return true; |
1574 | } |
1575 | if (!castBack(Derived)) |
1576 | return false; |
1577 | if (Path.empty()) |
1578 | DeclAndIsDerivedMember.setInt(false); |
1579 | return true; |
1580 | } |
1581 | |
1582 | bool castToBase(const CXXRecordDecl *Base) { |
1583 | if (!getDecl()) |
1584 | return true; |
1585 | if (Path.empty()) |
1586 | DeclAndIsDerivedMember.setInt(true); |
1587 | if (isDerivedMember()) { |
1588 | Path.push_back(Base); |
1589 | return true; |
1590 | } |
1591 | return castBack(Base); |
1592 | } |
1593 | }; |
1594 | |
1595 | |
1596 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
1597 | if (!LHS.getDecl() || !RHS.getDecl()) |
1598 | return !LHS.getDecl() && !RHS.getDecl(); |
1599 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
1600 | return false; |
1601 | return LHS.Path == RHS.Path; |
1602 | } |
1603 | } |
1604 | |
1605 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
1606 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
1607 | const LValue &This, const Expr *E, |
1608 | bool AllowNonLiteralTypes = false); |
1609 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
1610 | bool InvalidBaseOK = false); |
1611 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
1612 | bool InvalidBaseOK = false); |
1613 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
1614 | EvalInfo &Info); |
1615 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
1616 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
1617 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
1618 | EvalInfo &Info); |
1619 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
1620 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
1621 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
1622 | EvalInfo &Info); |
1623 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
1624 | |
1625 | |
1626 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
1627 | EvalInfo &Info); |
1628 | |
1629 | |
1630 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
1631 | EvalInfo &Info); |
1632 | |
1633 | |
1634 | |
1635 | |
1636 | |
1637 | |
1638 | template <class KeyTy> |
1639 | static APValue &createTemporary(const KeyTy *Key, bool IsLifetimeExtended, |
1640 | LValue &LV, CallStackFrame &Frame) { |
1641 | LV.set({Key, Frame.Info.CurrentCall->Index, |
1642 | Frame.Info.CurrentCall->getTempVersion()}); |
1643 | return Frame.createTemporary(Key, IsLifetimeExtended); |
1644 | } |
1645 | |
1646 | |
1647 | |
1648 | static void negateAsSigned(APSInt &Int) { |
1649 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
1650 | Int = Int.extend(Int.getBitWidth() + 1); |
1651 | Int.setIsSigned(true); |
1652 | } |
1653 | Int = -Int; |
1654 | } |
1655 | |
1656 | |
1657 | static void describeCall(CallStackFrame *Frame, raw_ostream &Out) { |
1658 | unsigned ArgIndex = 0; |
1659 | bool IsMemberCall = isa<CXXMethodDecl>(Frame->Callee) && |
1660 | !isa<CXXConstructorDecl>(Frame->Callee) && |
1661 | cast<CXXMethodDecl>(Frame->Callee)->isInstance(); |
1662 | |
1663 | if (!IsMemberCall) |
1664 | Out << *Frame->Callee << '('; |
1665 | |
1666 | if (Frame->This && IsMemberCall) { |
1667 | APValue Val; |
1668 | Frame->This->moveInto(Val); |
1669 | Val.printPretty(Out, Frame->Info.Ctx, |
1670 | Frame->This->Designator.MostDerivedType); |
1671 | |
1672 | Out << "->" << *Frame->Callee << '('; |
1673 | IsMemberCall = false; |
1674 | } |
1675 | |
1676 | for (FunctionDecl::param_const_iterator I = Frame->Callee->param_begin(), |
1677 | E = Frame->Callee->param_end(); I != E; ++I, ++ArgIndex) { |
1678 | if (ArgIndex > (unsigned)IsMemberCall) |
1679 | Out << ", "; |
1680 | |
1681 | const ParmVarDecl *Param = *I; |
1682 | const APValue &Arg = Frame->Arguments[ArgIndex]; |
1683 | Arg.printPretty(Out, Frame->Info.Ctx, Param->getType()); |
1684 | |
1685 | if (ArgIndex == 0 && IsMemberCall) |
1686 | Out << "->" << *Frame->Callee << '('; |
1687 | } |
1688 | |
1689 | Out << ')'; |
1690 | } |
1691 | |
1692 | |
1693 | |
1694 | |
1695 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
1696 | APValue Scratch; |
1697 | if (!Evaluate(Scratch, Info, E)) |
1698 | |
1699 | return Info.noteSideEffect(); |
1700 | return true; |
1701 | } |
1702 | |
1703 | |
1704 | static bool IsStringLiteralCall(const CallExpr *E) { |
1705 | unsigned Builtin = E->getBuiltinCallee(); |
1706 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
1707 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString); |
1708 | } |
1709 | |
1710 | static bool IsGlobalLValue(APValue::LValueBase B) { |
1711 | |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | if (!B) return true; |
1717 | |
1718 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
1719 | |
1720 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
1721 | return VD->hasGlobalStorage(); |
1722 | |
1723 | return isa<FunctionDecl>(D); |
1724 | } |
1725 | |
1726 | const Expr *E = B.get<const Expr*>(); |
1727 | switch (E->getStmtClass()) { |
1728 | default: |
1729 | return false; |
1730 | case Expr::CompoundLiteralExprClass: { |
1731 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(E); |
1732 | return CLE->isFileScope() && CLE->isLValue(); |
1733 | } |
1734 | case Expr::MaterializeTemporaryExprClass: |
1735 | |
1736 | |
1737 | return cast<MaterializeTemporaryExpr>(E)->getStorageDuration() == SD_Static; |
1738 | |
1739 | case Expr::StringLiteralClass: |
1740 | case Expr::PredefinedExprClass: |
1741 | case Expr::ObjCStringLiteralClass: |
1742 | case Expr::ObjCEncodeExprClass: |
1743 | case Expr::CXXTypeidExprClass: |
1744 | case Expr::CXXUuidofExprClass: |
1745 | return true; |
1746 | case Expr::ObjCBoxedExprClass: |
1747 | return cast<ObjCBoxedExpr>(E)->isExpressibleAsConstantInitializer(); |
1748 | case Expr::CallExprClass: |
1749 | return IsStringLiteralCall(cast<CallExpr>(E)); |
1750 | |
1751 | case Expr::AddrLabelExprClass: |
1752 | return true; |
1753 | |
1754 | |
1755 | case Expr::BlockExprClass: |
1756 | return !cast<BlockExpr>(E)->getBlockDecl()->hasCaptures(); |
1757 | case Expr::ImplicitValueInitExprClass: |
1758 | |
1759 | |
1760 | |
1761 | |
1762 | |
1763 | |
1764 | return true; |
1765 | } |
1766 | } |
1767 | |
1768 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
1769 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
1770 | } |
1771 | |
1772 | static bool IsLiteralLValue(const LValue &Value) { |
1773 | if (Value.getLValueCallIndex()) |
1774 | return false; |
1775 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
1776 | return E && !isa<MaterializeTemporaryExpr>(E); |
1777 | } |
1778 | |
1779 | static bool IsWeakLValue(const LValue &Value) { |
1780 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
1781 | return Decl && Decl->isWeak(); |
1782 | } |
1783 | |
1784 | static bool isZeroSized(const LValue &Value) { |
1785 | const ValueDecl *Decl = GetLValueBaseDecl(Value); |
1786 | if (Decl && isa<VarDecl>(Decl)) { |
1787 | QualType Ty = Decl->getType(); |
1788 | if (Ty->isArrayType()) |
1789 | return Ty->isIncompleteType() || |
1790 | Decl->getASTContext().getTypeSize(Ty) == 0; |
1791 | } |
1792 | return false; |
1793 | } |
1794 | |
1795 | static bool HasSameBase(const LValue &A, const LValue &B) { |
1796 | if (!A.getLValueBase()) |
1797 | return !B.getLValueBase(); |
1798 | if (!B.getLValueBase()) |
1799 | return false; |
1800 | |
1801 | if (A.getLValueBase().getOpaqueValue() != |
1802 | B.getLValueBase().getOpaqueValue()) { |
1803 | const Decl *ADecl = GetLValueBaseDecl(A); |
1804 | if (!ADecl) |
1805 | return false; |
1806 | const Decl *BDecl = GetLValueBaseDecl(B); |
1807 | if (!BDecl || ADecl->getCanonicalDecl() != BDecl->getCanonicalDecl()) |
1808 | return false; |
1809 | } |
1810 | |
1811 | return IsGlobalLValue(A.getLValueBase()) || |
1812 | (A.getLValueCallIndex() == B.getLValueCallIndex() && |
1813 | A.getLValueVersion() == B.getLValueVersion()); |
1814 | } |
1815 | |
1816 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
1817 | (0) . __assert_fail ("Base && \"no location for a null lvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1817, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Base && "no location for a null lvalue"); |
1818 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
1819 | if (VD) |
1820 | Info.Note(VD->getLocation(), diag::note_declared_at); |
1821 | else |
1822 | Info.Note(Base.get<const Expr*>()->getExprLoc(), |
1823 | diag::note_constexpr_temporary_here); |
1824 | } |
1825 | |
1826 | |
1827 | |
1828 | |
1829 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
1830 | QualType Type, const LValue &LVal, |
1831 | Expr::ConstExprUsage Usage) { |
1832 | bool IsReferenceType = Type->isReferenceType(); |
1833 | |
1834 | APValue::LValueBase Base = LVal.getLValueBase(); |
1835 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
1836 | |
1837 | |
1838 | |
1839 | |
1840 | if (!IsGlobalLValue(Base)) { |
1841 | if (Info.getLangOpts().CPlusPlus11) { |
1842 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
1843 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
1844 | << IsReferenceType << !Designator.Entries.empty() |
1845 | << !!VD << VD; |
1846 | NoteLValueLocation(Info, Base); |
1847 | } else { |
1848 | Info.FFDiag(Loc); |
1849 | } |
1850 | |
1851 | return false; |
1852 | } |
1853 | (0) . __assert_fail ("(Info.checkingPotentialConstantExpression() || LVal.getLValueCallIndex() == 0) && \"have call index for global lvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1855, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((Info.checkingPotentialConstantExpression() || |
1854 | (0) . __assert_fail ("(Info.checkingPotentialConstantExpression() || LVal.getLValueCallIndex() == 0) && \"have call index for global lvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1855, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> LVal.getLValueCallIndex() == 0) && |
1855 | (0) . __assert_fail ("(Info.checkingPotentialConstantExpression() || LVal.getLValueCallIndex() == 0) && \"have call index for global lvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 1855, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "have call index for global lvalue"); |
1856 | |
1857 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) { |
1858 | if (const VarDecl *Var = dyn_cast<const VarDecl>(VD)) { |
1859 | |
1860 | if (Var->getTLSKind()) |
1861 | return false; |
1862 | |
1863 | |
1864 | if (Usage == Expr::EvaluateForCodeGen && Var->hasAttr<DLLImportAttr>()) |
1865 | return false; |
1866 | } |
1867 | if (const auto *FD = dyn_cast<const FunctionDecl>(VD)) { |
1868 | |
1869 | |
1870 | |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | |
1876 | |
1877 | |
1878 | if (Info.getLangOpts().CPlusPlus && Usage == Expr::EvaluateForCodeGen && |
1879 | FD->hasAttr<DLLImportAttr>()) |
1880 | return false; |
1881 | } |
1882 | } |
1883 | |
1884 | |
1885 | |
1886 | if (!IsReferenceType) |
1887 | return true; |
1888 | |
1889 | |
1890 | if (!Base) { |
1891 | |
1892 | Info.CCEDiag(Loc); |
1893 | return true; |
1894 | } |
1895 | |
1896 | |
1897 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
1898 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
1899 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
1900 | << !Designator.Entries.empty() << !!VD << VD; |
1901 | NoteLValueLocation(Info, Base); |
1902 | } |
1903 | |
1904 | return true; |
1905 | } |
1906 | |
1907 | |
1908 | |
1909 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
1910 | SourceLocation Loc, |
1911 | QualType Type, |
1912 | const APValue &Value, |
1913 | Expr::ConstExprUsage Usage) { |
1914 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
1915 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Member); |
1916 | if (!FD) |
1917 | return true; |
1918 | return Usage == Expr::EvaluateForMangling || FD->isVirtual() || |
1919 | !FD->hasAttr<DLLImportAttr>(); |
1920 | } |
1921 | |
1922 | |
1923 | |
1924 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
1925 | const LValue *This = nullptr) { |
1926 | if (!E->isRValue() || E->getType()->isLiteralType(Info.Ctx)) |
1927 | return true; |
1928 | |
1929 | |
1930 | |
1931 | |
1932 | |
1933 | |
1934 | |
1935 | |
1936 | |
1937 | |
1938 | |
1939 | |
1940 | |
1941 | |
1942 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
1943 | return true; |
1944 | |
1945 | |
1946 | if (Info.getLangOpts().CPlusPlus11) |
1947 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
1948 | << E->getType(); |
1949 | else |
1950 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
1951 | return false; |
1952 | } |
1953 | |
1954 | |
1955 | |
1956 | |
1957 | static bool |
1958 | CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, QualType Type, |
1959 | const APValue &Value, |
1960 | Expr::ConstExprUsage Usage = Expr::EvaluateForCodeGen) { |
1961 | if (Value.isUninit()) { |
1962 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
1963 | << true << Type; |
1964 | return false; |
1965 | } |
1966 | |
1967 | |
1968 | |
1969 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
1970 | Type = AT->getValueType(); |
1971 | |
1972 | |
1973 | |
1974 | |
1975 | if (Value.isArray()) { |
1976 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
1977 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
1978 | if (!CheckConstantExpression(Info, DiagLoc, EltTy, |
1979 | Value.getArrayInitializedElt(I), Usage)) |
1980 | return false; |
1981 | } |
1982 | if (!Value.hasArrayFiller()) |
1983 | return true; |
1984 | return CheckConstantExpression(Info, DiagLoc, EltTy, Value.getArrayFiller(), |
1985 | Usage); |
1986 | } |
1987 | if (Value.isUnion() && Value.getUnionField()) { |
1988 | return CheckConstantExpression(Info, DiagLoc, |
1989 | Value.getUnionField()->getType(), |
1990 | Value.getUnionValue(), Usage); |
1991 | } |
1992 | if (Value.isStruct()) { |
1993 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
1994 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD)) { |
1995 | unsigned BaseIndex = 0; |
1996 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
1997 | if (!CheckConstantExpression(Info, DiagLoc, BS.getType(), |
1998 | Value.getStructBase(BaseIndex), Usage)) |
1999 | return false; |
2000 | ++BaseIndex; |
2001 | } |
2002 | } |
2003 | for (const auto *I : RD->fields()) { |
2004 | if (I->isUnnamedBitfield()) |
2005 | continue; |
2006 | |
2007 | if (!CheckConstantExpression(Info, DiagLoc, I->getType(), |
2008 | Value.getStructField(I->getFieldIndex()), |
2009 | Usage)) |
2010 | return false; |
2011 | } |
2012 | } |
2013 | |
2014 | if (Value.isLValue()) { |
2015 | LValue LVal; |
2016 | LVal.setFrom(Info.Ctx, Value); |
2017 | return CheckLValueConstantExpression(Info, DiagLoc, Type, LVal, Usage); |
2018 | } |
2019 | |
2020 | if (Value.isMemberPointer()) |
2021 | return CheckMemberPointerConstantExpression(Info, DiagLoc, Type, Value, Usage); |
2022 | |
2023 | |
2024 | return true; |
2025 | } |
2026 | |
2027 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
2028 | |
2029 | |
2030 | if (!Value.getLValueBase()) { |
2031 | Result = !Value.getLValueOffset().isZero(); |
2032 | return true; |
2033 | } |
2034 | |
2035 | |
2036 | |
2037 | Result = true; |
2038 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
2039 | return !Decl || !Decl->isWeak(); |
2040 | } |
2041 | |
2042 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
2043 | switch (Val.getKind()) { |
2044 | case APValue::Uninitialized: |
2045 | return false; |
2046 | case APValue::Int: |
2047 | Result = Val.getInt().getBoolValue(); |
2048 | return true; |
2049 | case APValue::FixedPoint: |
2050 | Result = Val.getFixedPoint().getBoolValue(); |
2051 | return true; |
2052 | case APValue::Float: |
2053 | Result = !Val.getFloat().isZero(); |
2054 | return true; |
2055 | case APValue::ComplexInt: |
2056 | Result = Val.getComplexIntReal().getBoolValue() || |
2057 | Val.getComplexIntImag().getBoolValue(); |
2058 | return true; |
2059 | case APValue::ComplexFloat: |
2060 | Result = !Val.getComplexFloatReal().isZero() || |
2061 | !Val.getComplexFloatImag().isZero(); |
2062 | return true; |
2063 | case APValue::LValue: |
2064 | return EvalPointerValueAsBool(Val, Result); |
2065 | case APValue::MemberPointer: |
2066 | Result = Val.getMemberPointerDecl(); |
2067 | return true; |
2068 | case APValue::Vector: |
2069 | case APValue::Array: |
2070 | case APValue::Struct: |
2071 | case APValue::Union: |
2072 | case APValue::AddrLabelDiff: |
2073 | return false; |
2074 | } |
2075 | |
2076 | llvm_unreachable("unknown APValue kind"); |
2077 | } |
2078 | |
2079 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
2080 | EvalInfo &Info) { |
2081 | (0) . __assert_fail ("E->isRValue() && \"missing lvalue-to-rvalue conv in bool condition\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2081, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && "missing lvalue-to-rvalue conv in bool condition"); |
2082 | APValue Val; |
2083 | if (!Evaluate(Val, Info, E)) |
2084 | return false; |
2085 | return HandleConversionToBool(Val, Result); |
2086 | } |
2087 | |
2088 | template<typename T> |
2089 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
2090 | const T &SrcValue, QualType DestType) { |
2091 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
2092 | << SrcValue << DestType; |
2093 | return Info.noteUndefinedBehavior(); |
2094 | } |
2095 | |
2096 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
2097 | QualType SrcType, const APFloat &Value, |
2098 | QualType DestType, APSInt &Result) { |
2099 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2100 | |
2101 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
2102 | |
2103 | Result = APSInt(DestWidth, !DestSigned); |
2104 | bool ignored; |
2105 | if (Value.convertToInteger(Result, llvm::APFloat::rmTowardZero, &ignored) |
2106 | & APFloat::opInvalidOp) |
2107 | return HandleOverflow(Info, E, Value, DestType); |
2108 | return true; |
2109 | } |
2110 | |
2111 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
2112 | QualType SrcType, QualType DestType, |
2113 | APFloat &Result) { |
2114 | APFloat Value = Result; |
2115 | bool ignored; |
2116 | if (Result.convert(Info.Ctx.getFloatTypeSemantics(DestType), |
2117 | APFloat::rmNearestTiesToEven, &ignored) |
2118 | & APFloat::opOverflow) |
2119 | return HandleOverflow(Info, E, Value, DestType); |
2120 | return true; |
2121 | } |
2122 | |
2123 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
2124 | QualType DestType, QualType SrcType, |
2125 | const APSInt &Value) { |
2126 | unsigned DestWidth = Info.Ctx.getIntWidth(DestType); |
2127 | |
2128 | |
2129 | APSInt Result = Value.extOrTrunc(DestWidth); |
2130 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
2131 | if (DestType->isBooleanType()) |
2132 | Result = Value.getBoolValue(); |
2133 | return Result; |
2134 | } |
2135 | |
2136 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
2137 | QualType SrcType, const APSInt &Value, |
2138 | QualType DestType, APFloat &Result) { |
2139 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(DestType), 1); |
2140 | if (Result.convertFromAPInt(Value, Value.isSigned(), |
2141 | APFloat::rmNearestTiesToEven) |
2142 | & APFloat::opOverflow) |
2143 | return HandleOverflow(Info, E, Value, DestType); |
2144 | return true; |
2145 | } |
2146 | |
2147 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
2148 | APValue &Value, const FieldDecl *FD) { |
2149 | (0) . __assert_fail ("FD->isBitField() && \"truncateBitfieldValue on non-bitfield\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2149, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield"); |
2150 | |
2151 | if (!Value.isInt()) { |
2152 | |
2153 | |
2154 | |
2155 | (0) . __assert_fail ("Value.isLValue() && \"integral value neither int nor lvalue?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2155, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Value.isLValue() && "integral value neither int nor lvalue?"); |
2156 | Info.FFDiag(E); |
2157 | return false; |
2158 | } |
2159 | |
2160 | APSInt &Int = Value.getInt(); |
2161 | unsigned OldBitWidth = Int.getBitWidth(); |
2162 | unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx); |
2163 | if (NewBitWidth < OldBitWidth) |
2164 | Int = Int.trunc(NewBitWidth).extend(OldBitWidth); |
2165 | return true; |
2166 | } |
2167 | |
2168 | static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E, |
2169 | llvm::APInt &Res) { |
2170 | APValue SVal; |
2171 | if (!Evaluate(SVal, Info, E)) |
2172 | return false; |
2173 | if (SVal.isInt()) { |
2174 | Res = SVal.getInt(); |
2175 | return true; |
2176 | } |
2177 | if (SVal.isFloat()) { |
2178 | Res = SVal.getFloat().bitcastToAPInt(); |
2179 | return true; |
2180 | } |
2181 | if (SVal.isVector()) { |
2182 | QualType VecTy = E->getType(); |
2183 | unsigned VecSize = Info.Ctx.getTypeSize(VecTy); |
2184 | QualType EltTy = VecTy->castAs<VectorType>()->getElementType(); |
2185 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
2186 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
2187 | Res = llvm::APInt::getNullValue(VecSize); |
2188 | for (unsigned i = 0; i < SVal.getVectorLength(); i++) { |
2189 | APValue &Elt = SVal.getVectorElt(i); |
2190 | llvm::APInt EltAsInt; |
2191 | if (Elt.isInt()) { |
2192 | EltAsInt = Elt.getInt(); |
2193 | } else if (Elt.isFloat()) { |
2194 | EltAsInt = Elt.getFloat().bitcastToAPInt(); |
2195 | } else { |
2196 | |
2197 | |
2198 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2199 | return false; |
2200 | } |
2201 | unsigned BaseEltSize = EltAsInt.getBitWidth(); |
2202 | if (BigEndian) |
2203 | Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i*EltSize+BaseEltSize); |
2204 | else |
2205 | Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i*EltSize); |
2206 | } |
2207 | return true; |
2208 | } |
2209 | |
2210 | |
2211 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2212 | return false; |
2213 | } |
2214 | |
2215 | |
2216 | |
2217 | |
2218 | template<typename Operation> |
2219 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
2220 | const APSInt &LHS, const APSInt &RHS, |
2221 | unsigned BitWidth, Operation Op, |
2222 | APSInt &Result) { |
2223 | if (LHS.isUnsigned()) { |
2224 | Result = Op(LHS, RHS); |
2225 | return true; |
2226 | } |
2227 | |
2228 | APSInt Value(Op(LHS.extend(BitWidth), RHS.extend(BitWidth)), false); |
2229 | Result = Value.trunc(LHS.getBitWidth()); |
2230 | if (Result.extend(BitWidth) != Value) { |
2231 | if (Info.checkingForOverflow()) |
2232 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
2233 | diag::warn_integer_constant_overflow) |
2234 | << Result.toString(10) << E->getType(); |
2235 | else |
2236 | return HandleOverflow(Info, E, Value, E->getType()); |
2237 | } |
2238 | return true; |
2239 | } |
2240 | |
2241 | |
2242 | static bool handleIntIntBinOp(EvalInfo &Info, const Expr *E, const APSInt &LHS, |
2243 | BinaryOperatorKind Opcode, APSInt RHS, |
2244 | APSInt &Result) { |
2245 | switch (Opcode) { |
2246 | default: |
2247 | Info.FFDiag(E); |
2248 | return false; |
2249 | case BO_Mul: |
2250 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2251 | std::multiplies<APSInt>(), Result); |
2252 | case BO_Add: |
2253 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2254 | std::plus<APSInt>(), Result); |
2255 | case BO_Sub: |
2256 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2257 | std::minus<APSInt>(), Result); |
2258 | case BO_And: Result = LHS & RHS; return true; |
2259 | case BO_Xor: Result = LHS ^ RHS; return true; |
2260 | case BO_Or: Result = LHS | RHS; return true; |
2261 | case BO_Div: |
2262 | case BO_Rem: |
2263 | if (RHS == 0) { |
2264 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
2265 | return false; |
2266 | } |
2267 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2268 | |
2269 | |
2270 | if (RHS.isNegative() && RHS.isAllOnesValue() && |
2271 | LHS.isSigned() && LHS.isMinSignedValue()) |
2272 | return HandleOverflow(Info, E, -LHS.extend(LHS.getBitWidth() + 1), |
2273 | E->getType()); |
2274 | return true; |
2275 | case BO_Shl: { |
2276 | if (Info.getLangOpts().OpenCL) |
2277 | |
2278 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2279 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2280 | RHS.isUnsigned()); |
2281 | else if (RHS.isSigned() && RHS.isNegative()) { |
2282 | |
2283 | |
2284 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2285 | RHS = -RHS; |
2286 | goto shift_right; |
2287 | } |
2288 | shift_left: |
2289 | |
2290 | |
2291 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2292 | if (SA != RHS) { |
2293 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2294 | << RHS << E->getType() << LHS.getBitWidth(); |
2295 | } else if (LHS.isSigned()) { |
2296 | |
2297 | |
2298 | if (LHS.isNegative()) |
2299 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
2300 | else if (LHS.countLeadingZeros() < SA) |
2301 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
2302 | } |
2303 | Result = LHS << SA; |
2304 | return true; |
2305 | } |
2306 | case BO_Shr: { |
2307 | if (Info.getLangOpts().OpenCL) |
2308 | |
2309 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2310 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2311 | RHS.isUnsigned()); |
2312 | else if (RHS.isSigned() && RHS.isNegative()) { |
2313 | |
2314 | |
2315 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2316 | RHS = -RHS; |
2317 | goto shift_left; |
2318 | } |
2319 | shift_right: |
2320 | |
2321 | |
2322 | unsigned SA = (unsigned) RHS.getLimitedValue(LHS.getBitWidth()-1); |
2323 | if (SA != RHS) |
2324 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2325 | << RHS << E->getType() << LHS.getBitWidth(); |
2326 | Result = LHS >> SA; |
2327 | return true; |
2328 | } |
2329 | |
2330 | case BO_LT: Result = LHS < RHS; return true; |
2331 | case BO_GT: Result = LHS > RHS; return true; |
2332 | case BO_LE: Result = LHS <= RHS; return true; |
2333 | case BO_GE: Result = LHS >= RHS; return true; |
2334 | case BO_EQ: Result = LHS == RHS; return true; |
2335 | case BO_NE: Result = LHS != RHS; return true; |
2336 | case BO_Cmp: |
2337 | llvm_unreachable("BO_Cmp should be handled elsewhere"); |
2338 | } |
2339 | } |
2340 | |
2341 | |
2342 | static bool handleFloatFloatBinOp(EvalInfo &Info, const Expr *E, |
2343 | APFloat &LHS, BinaryOperatorKind Opcode, |
2344 | const APFloat &RHS) { |
2345 | switch (Opcode) { |
2346 | default: |
2347 | Info.FFDiag(E); |
2348 | return false; |
2349 | case BO_Mul: |
2350 | LHS.multiply(RHS, APFloat::rmNearestTiesToEven); |
2351 | break; |
2352 | case BO_Add: |
2353 | LHS.add(RHS, APFloat::rmNearestTiesToEven); |
2354 | break; |
2355 | case BO_Sub: |
2356 | LHS.subtract(RHS, APFloat::rmNearestTiesToEven); |
2357 | break; |
2358 | case BO_Div: |
2359 | LHS.divide(RHS, APFloat::rmNearestTiesToEven); |
2360 | break; |
2361 | } |
2362 | |
2363 | if (LHS.isInfinity() || LHS.isNaN()) { |
2364 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
2365 | return Info.noteUndefinedBehavior(); |
2366 | } |
2367 | return true; |
2368 | } |
2369 | |
2370 | |
2371 | |
2372 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
2373 | const RecordDecl *TruncatedType, |
2374 | unsigned TruncatedElements) { |
2375 | SubobjectDesignator &D = Result.Designator; |
2376 | |
2377 | |
2378 | if (TruncatedElements == D.Entries.size()) |
2379 | return true; |
2380 | (0) . __assert_fail ("TruncatedElements >= D.MostDerivedPathLength && \"not casting to a derived class\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2381, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TruncatedElements >= D.MostDerivedPathLength && |
2381 | (0) . __assert_fail ("TruncatedElements >= D.MostDerivedPathLength && \"not casting to a derived class\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2381, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "not casting to a derived class"); |
2382 | if (!Result.checkSubobject(Info, E, CSK_Derived)) |
2383 | return false; |
2384 | |
2385 | |
2386 | const RecordDecl *RD = TruncatedType; |
2387 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
2388 | if (RD->isInvalidDecl()) return false; |
2389 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
2390 | const CXXRecordDecl *Base = getAsBaseClass(D.Entries[I]); |
2391 | if (isVirtualBaseClass(D.Entries[I])) |
2392 | Result.Offset -= Layout.getVBaseClassOffset(Base); |
2393 | else |
2394 | Result.Offset -= Layout.getBaseClassOffset(Base); |
2395 | RD = Base; |
2396 | } |
2397 | D.Entries.resize(TruncatedElements); |
2398 | return true; |
2399 | } |
2400 | |
2401 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
2402 | const CXXRecordDecl *Derived, |
2403 | const CXXRecordDecl *Base, |
2404 | const ASTRecordLayout *RL = nullptr) { |
2405 | if (!RL) { |
2406 | if (Derived->isInvalidDecl()) return false; |
2407 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
2408 | } |
2409 | |
2410 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
2411 | Obj.addDecl(Info, E, Base, false); |
2412 | return true; |
2413 | } |
2414 | |
2415 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
2416 | const CXXRecordDecl *DerivedDecl, |
2417 | const CXXBaseSpecifier *Base) { |
2418 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
2419 | |
2420 | if (!Base->isVirtual()) |
2421 | return HandleLValueDirectBase(Info, E, Obj, DerivedDecl, BaseDecl); |
2422 | |
2423 | SubobjectDesignator &D = Obj.Designator; |
2424 | if (D.Invalid) |
2425 | return false; |
2426 | |
2427 | |
2428 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
2429 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
2430 | return false; |
2431 | |
2432 | |
2433 | if (DerivedDecl->isInvalidDecl()) return false; |
2434 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
2435 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(BaseDecl); |
2436 | Obj.addDecl(Info, E, BaseDecl, true); |
2437 | return true; |
2438 | } |
2439 | |
2440 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
2441 | QualType Type, LValue &Result) { |
2442 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
2443 | PathE = E->path_end(); |
2444 | PathI != PathE; ++PathI) { |
2445 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
2446 | *PathI)) |
2447 | return false; |
2448 | Type = (*PathI)->getType(); |
2449 | } |
2450 | return true; |
2451 | } |
2452 | |
2453 | |
2454 | |
2455 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
2456 | const FieldDecl *FD, |
2457 | const ASTRecordLayout *RL = nullptr) { |
2458 | if (!RL) { |
2459 | if (FD->getParent()->isInvalidDecl()) return false; |
2460 | RL = &Info.Ctx.getASTRecordLayout(FD->getParent()); |
2461 | } |
2462 | |
2463 | unsigned I = FD->getFieldIndex(); |
2464 | LVal.adjustOffset(Info.Ctx.toCharUnitsFromBits(RL->getFieldOffset(I))); |
2465 | LVal.addDecl(Info, E, FD); |
2466 | return true; |
2467 | } |
2468 | |
2469 | |
2470 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
2471 | LValue &LVal, |
2472 | const IndirectFieldDecl *IFD) { |
2473 | for (const auto *C : IFD->chain()) |
2474 | if (!HandleLValueMember(Info, E, LVal, cast<FieldDecl>(C))) |
2475 | return false; |
2476 | return true; |
2477 | } |
2478 | |
2479 | |
2480 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, |
2481 | QualType Type, CharUnits &Size) { |
2482 | |
2483 | |
2484 | if (Type->isVoidType() || Type->isFunctionType()) { |
2485 | Size = CharUnits::One(); |
2486 | return true; |
2487 | } |
2488 | |
2489 | if (Type->isDependentType()) { |
2490 | Info.FFDiag(Loc); |
2491 | return false; |
2492 | } |
2493 | |
2494 | if (!Type->isConstantSizeType()) { |
2495 | |
2496 | |
2497 | Info.FFDiag(Loc); |
2498 | return false; |
2499 | } |
2500 | |
2501 | Size = Info.Ctx.getTypeSizeInChars(Type); |
2502 | return true; |
2503 | } |
2504 | |
2505 | |
2506 | |
2507 | |
2508 | |
2509 | |
2510 | |
2511 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
2512 | LValue &LVal, QualType EltTy, |
2513 | APSInt Adjustment) { |
2514 | CharUnits SizeOfPointee; |
2515 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfPointee)) |
2516 | return false; |
2517 | |
2518 | LVal.adjustOffsetAndIndex(Info, E, Adjustment, SizeOfPointee); |
2519 | return true; |
2520 | } |
2521 | |
2522 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
2523 | LValue &LVal, QualType EltTy, |
2524 | int64_t Adjustment) { |
2525 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
2526 | APSInt::get(Adjustment)); |
2527 | } |
2528 | |
2529 | |
2530 | |
2531 | |
2532 | |
2533 | |
2534 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
2535 | LValue &LVal, QualType EltTy, |
2536 | bool Imag) { |
2537 | if (Imag) { |
2538 | CharUnits SizeOfComponent; |
2539 | if (!HandleSizeof(Info, E->getExprLoc(), EltTy, SizeOfComponent)) |
2540 | return false; |
2541 | LVal.Offset += SizeOfComponent; |
2542 | } |
2543 | LVal.addComplex(Info, E, EltTy, Imag); |
2544 | return true; |
2545 | } |
2546 | |
2547 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
2548 | QualType Type, const LValue &LVal, |
2549 | APValue &RVal); |
2550 | |
2551 | |
2552 | |
2553 | |
2554 | |
2555 | |
2556 | |
2557 | |
2558 | |
2559 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
2560 | const VarDecl *VD, CallStackFrame *Frame, |
2561 | APValue *&Result, const LValue *LVal) { |
2562 | |
2563 | |
2564 | |
2565 | if (const ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(VD)) { |
2566 | |
2567 | |
2568 | if (Info.checkingPotentialConstantExpression()) |
2569 | return false; |
2570 | if (!Frame || !Frame->Arguments) { |
2571 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2572 | return false; |
2573 | } |
2574 | Result = &Frame->Arguments[PVD->getFunctionScopeIndex()]; |
2575 | return true; |
2576 | } |
2577 | |
2578 | |
2579 | if (Frame) { |
2580 | Result = LVal ? Frame->getTemporary(VD, LVal->getLValueVersion()) |
2581 | : Frame->getCurrentTemporary(VD); |
2582 | if (!Result) { |
2583 | |
2584 | |
2585 | |
2586 | |
2587 | (0) . __assert_fail ("isLambdaCallOperator(Frame->Callee) && (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && \"missing value for local variable\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2589, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isLambdaCallOperator(Frame->Callee) && |
2588 | (0) . __assert_fail ("isLambdaCallOperator(Frame->Callee) && (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && \"missing value for local variable\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2589, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
2589 | (0) . __assert_fail ("isLambdaCallOperator(Frame->Callee) && (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && \"missing value for local variable\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2589, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "missing value for local variable"); |
2590 | if (Info.checkingPotentialConstantExpression()) |
2591 | return false; |
2592 | |
2593 | Info.FFDiag(E->getBeginLoc(), |
2594 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
2595 | << "captures not currently allowed"; |
2596 | return false; |
2597 | } |
2598 | return true; |
2599 | } |
2600 | |
2601 | |
2602 | const Expr *Init = VD->getAnyInitializer(VD); |
2603 | if (!Init || Init->isValueDependent()) { |
2604 | |
2605 | |
2606 | if (!Info.checkingPotentialConstantExpression()) |
2607 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2608 | return false; |
2609 | } |
2610 | |
2611 | |
2612 | |
2613 | if (Info.EvaluatingDecl.dyn_cast<const ValueDecl*>() == VD) { |
2614 | Result = Info.EvaluatingDeclValue; |
2615 | return true; |
2616 | } |
2617 | |
2618 | |
2619 | |
2620 | if (VD->isWeak()) { |
2621 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2622 | return false; |
2623 | } |
2624 | |
2625 | |
2626 | |
2627 | SmallVector<PartialDiagnosticAt, 8> Notes; |
2628 | if (!VD->evaluateValue(Notes)) { |
2629 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, |
2630 | Notes.size() + 1) << VD; |
2631 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2632 | Info.addNotes(Notes); |
2633 | return false; |
2634 | } else if (!VD->checkInitIsICE()) { |
2635 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, |
2636 | Notes.size() + 1) << VD; |
2637 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2638 | Info.addNotes(Notes); |
2639 | } |
2640 | |
2641 | Result = VD->getEvaluatedValue(); |
2642 | return true; |
2643 | } |
2644 | |
2645 | static bool IsConstNonVolatile(QualType T) { |
2646 | Qualifiers Quals = T.getQualifiers(); |
2647 | return Quals.hasConst() && !Quals.hasVolatile(); |
2648 | } |
2649 | |
2650 | |
2651 | |
2652 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
2653 | const CXXRecordDecl *Base) { |
2654 | Base = Base->getCanonicalDecl(); |
2655 | unsigned Index = 0; |
2656 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
2657 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
2658 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
2659 | return Index; |
2660 | } |
2661 | |
2662 | llvm_unreachable("base class missing from derived class's bases list"); |
2663 | } |
2664 | |
2665 | |
2666 | static APSInt (EvalInfo &Info, const Expr *Lit, |
2667 | uint64_t Index) { |
2668 | |
2669 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Lit)) { |
2670 | std::string Str; |
2671 | Info.Ctx.getObjCEncodingForType(ObjCEnc->getEncodedType(), Str); |
2672 | (0) . __assert_fail ("Index <= Str.size() && \"Index too large\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2672, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Index <= Str.size() && "Index too large"); |
2673 | return APSInt::getUnsigned(Str.c_str()[Index]); |
2674 | } |
2675 | |
2676 | if (auto PE = dyn_cast<PredefinedExpr>(Lit)) |
2677 | Lit = PE->getFunctionName(); |
2678 | const StringLiteral *S = cast<StringLiteral>(Lit); |
2679 | const ConstantArrayType *CAT = |
2680 | Info.Ctx.getAsConstantArrayType(S->getType()); |
2681 | (0) . __assert_fail ("CAT && \"string literal isn't an array\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2681, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CAT && "string literal isn't an array"); |
2682 | QualType CharType = CAT->getElementType(); |
2683 | (0) . __assert_fail ("CharType->isIntegerType() && \"unexpected character type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2683, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CharType->isIntegerType() && "unexpected character type"); |
2684 | |
2685 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
2686 | CharType->isUnsignedIntegerType()); |
2687 | if (Index < S->getLength()) |
2688 | Value = S->getCodeUnit(Index); |
2689 | return Value; |
2690 | } |
2691 | |
2692 | |
2693 | |
2694 | |
2695 | |
2696 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
2697 | APValue &Result) { |
2698 | const ConstantArrayType *CAT = |
2699 | Info.Ctx.getAsConstantArrayType(S->getType()); |
2700 | (0) . __assert_fail ("CAT && \"string literal isn't an array\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2700, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CAT && "string literal isn't an array"); |
2701 | QualType CharType = CAT->getElementType(); |
2702 | (0) . __assert_fail ("CharType->isIntegerType() && \"unexpected character type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2702, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CharType->isIntegerType() && "unexpected character type"); |
2703 | |
2704 | unsigned Elts = CAT->getSize().getZExtValue(); |
2705 | Result = APValue(APValue::UninitArray(), |
2706 | std::min(S->getLength(), Elts), Elts); |
2707 | APSInt Value(S->getCharByteWidth() * Info.Ctx.getCharWidth(), |
2708 | CharType->isUnsignedIntegerType()); |
2709 | if (Result.hasArrayFiller()) |
2710 | Result.getArrayFiller() = APValue(Value); |
2711 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
2712 | Value = S->getCodeUnit(I); |
2713 | Result.getArrayInitializedElt(I) = APValue(Value); |
2714 | } |
2715 | } |
2716 | |
2717 | |
2718 | static void expandArray(APValue &Array, unsigned Index) { |
2719 | unsigned Size = Array.getArraySize(); |
2720 | assert(Index < Size); |
2721 | |
2722 | |
2723 | unsigned OldElts = Array.getArrayInitializedElts(); |
2724 | unsigned NewElts = std::max(Index+1, OldElts * 2); |
2725 | NewElts = std::min(Size, std::max(NewElts, 8u)); |
2726 | |
2727 | |
2728 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
2729 | for (unsigned I = 0; I != OldElts; ++I) |
2730 | NewValue.getArrayInitializedElt(I).swap(Array.getArrayInitializedElt(I)); |
2731 | for (unsigned I = OldElts; I != NewElts; ++I) |
2732 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
2733 | if (NewValue.hasArrayFiller()) |
2734 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
2735 | Array.swap(NewValue); |
2736 | } |
2737 | |
2738 | |
2739 | |
2740 | |
2741 | |
2742 | |
2743 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
2744 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
2745 | if (!RD || (RD->isUnion() && !RD->field_empty())) |
2746 | return true; |
2747 | if (RD->isEmpty()) |
2748 | return false; |
2749 | |
2750 | for (auto *Field : RD->fields()) |
2751 | if (isReadByLvalueToRvalueConversion(Field->getType())) |
2752 | return true; |
2753 | |
2754 | for (auto &BaseSpec : RD->bases()) |
2755 | if (isReadByLvalueToRvalueConversion(BaseSpec.getType())) |
2756 | return true; |
2757 | |
2758 | return false; |
2759 | } |
2760 | |
2761 | |
2762 | |
2763 | static bool diagnoseUnreadableFields(EvalInfo &Info, const Expr *E, |
2764 | QualType T) { |
2765 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
2766 | if (!RD) |
2767 | return false; |
2768 | |
2769 | if (!RD->hasMutableFields()) |
2770 | return false; |
2771 | |
2772 | for (auto *Field : RD->fields()) { |
2773 | |
2774 | |
2775 | |
2776 | |
2777 | if (Field->isMutable() && |
2778 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
2779 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) << Field; |
2780 | Info.Note(Field->getLocation(), diag::note_declared_at); |
2781 | return true; |
2782 | } |
2783 | |
2784 | if (diagnoseUnreadableFields(Info, E, Field->getType())) |
2785 | return true; |
2786 | } |
2787 | |
2788 | for (auto &BaseSpec : RD->bases()) |
2789 | if (diagnoseUnreadableFields(Info, E, BaseSpec.getType())) |
2790 | return true; |
2791 | |
2792 | |
2793 | return false; |
2794 | } |
2795 | |
2796 | namespace { |
2797 | |
2798 | |
2799 | struct CompleteObject { |
2800 | |
2801 | APValue *Value; |
2802 | |
2803 | QualType Type; |
2804 | bool LifetimeStartedInEvaluation; |
2805 | |
2806 | CompleteObject() : Value(nullptr) {} |
2807 | CompleteObject(APValue *Value, QualType Type, |
2808 | bool LifetimeStartedInEvaluation) |
2809 | : Value(Value), Type(Type), |
2810 | LifetimeStartedInEvaluation(LifetimeStartedInEvaluation) { |
2811 | (0) . __assert_fail ("Value && \"missing value for complete object\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2811, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Value && "missing value for complete object"); |
2812 | } |
2813 | |
2814 | explicit operator bool() const { return Value; } |
2815 | }; |
2816 | } |
2817 | |
2818 | |
2819 | template<typename SubobjectHandler> |
2820 | typename SubobjectHandler::result_type |
2821 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
2822 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
2823 | if (Sub.Invalid) |
2824 | |
2825 | return handler.failed(); |
2826 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
2827 | if (Info.getLangOpts().CPlusPlus11) |
2828 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
2829 | ? diag::note_constexpr_access_past_end |
2830 | : diag::note_constexpr_access_unsized_array) |
2831 | << handler.AccessKind; |
2832 | else |
2833 | Info.FFDiag(E); |
2834 | return handler.failed(); |
2835 | } |
2836 | |
2837 | APValue *O = Obj.Value; |
2838 | QualType ObjType = Obj.Type; |
2839 | const FieldDecl *LastField = nullptr; |
2840 | const bool MayReadMutableMembers = |
2841 | Obj.LifetimeStartedInEvaluation && Info.getLangOpts().CPlusPlus14; |
2842 | |
2843 | |
2844 | for (unsigned I = 0, N = Sub.Entries.size(); ; ++I) { |
2845 | if (O->isUninit()) { |
2846 | if (!Info.checkingPotentialConstantExpression()) |
2847 | Info.FFDiag(E, diag::note_constexpr_access_uninit) << handler.AccessKind; |
2848 | return handler.failed(); |
2849 | } |
2850 | |
2851 | if (I == N) { |
2852 | |
2853 | |
2854 | |
2855 | |
2856 | if (ObjType->isRecordType() && handler.AccessKind == AK_Read && |
2857 | !MayReadMutableMembers && diagnoseUnreadableFields(Info, E, ObjType)) |
2858 | return handler.failed(); |
2859 | |
2860 | if (!handler.found(*O, ObjType)) |
2861 | return false; |
2862 | |
2863 | |
2864 | if (handler.AccessKind != AK_Read && |
2865 | LastField && LastField->isBitField() && |
2866 | !truncateBitfieldValue(Info, E, *O, LastField)) |
2867 | return false; |
2868 | |
2869 | return true; |
2870 | } |
2871 | |
2872 | LastField = nullptr; |
2873 | if (ObjType->isArrayType()) { |
2874 | |
2875 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(ObjType); |
2876 | (0) . __assert_fail ("CAT && \"vla in literal type?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2876, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CAT && "vla in literal type?"); |
2877 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
2878 | if (CAT->getSize().ule(Index)) { |
2879 | |
2880 | |
2881 | if (Info.getLangOpts().CPlusPlus11) |
2882 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
2883 | << handler.AccessKind; |
2884 | else |
2885 | Info.FFDiag(E); |
2886 | return handler.failed(); |
2887 | } |
2888 | |
2889 | ObjType = CAT->getElementType(); |
2890 | |
2891 | if (O->getArrayInitializedElts() > Index) |
2892 | O = &O->getArrayInitializedElt(Index); |
2893 | else if (handler.AccessKind != AK_Read) { |
2894 | expandArray(*O, Index); |
2895 | O = &O->getArrayInitializedElt(Index); |
2896 | } else |
2897 | O = &O->getArrayFiller(); |
2898 | } else if (ObjType->isAnyComplexType()) { |
2899 | |
2900 | uint64_t Index = Sub.Entries[I].ArrayIndex; |
2901 | if (Index > 1) { |
2902 | if (Info.getLangOpts().CPlusPlus11) |
2903 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
2904 | << handler.AccessKind; |
2905 | else |
2906 | Info.FFDiag(E); |
2907 | return handler.failed(); |
2908 | } |
2909 | |
2910 | bool WasConstQualified = ObjType.isConstQualified(); |
2911 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
2912 | if (WasConstQualified) |
2913 | ObjType.addConst(); |
2914 | |
2915 | (0) . __assert_fail ("I == N - 1 && \"extracting subobject of scalar?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2915, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I == N - 1 && "extracting subobject of scalar?"); |
2916 | if (O->isComplexInt()) { |
2917 | return handler.found(Index ? O->getComplexIntImag() |
2918 | : O->getComplexIntReal(), ObjType); |
2919 | } else { |
2920 | isComplexFloat()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 2920, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(O->isComplexFloat()); |
2921 | return handler.found(Index ? O->getComplexFloatImag() |
2922 | : O->getComplexFloatReal(), ObjType); |
2923 | } |
2924 | } else if (const FieldDecl *Field = getAsField(Sub.Entries[I])) { |
2925 | |
2926 | |
2927 | |
2928 | if (Field->isMutable() && handler.AccessKind == AK_Read && |
2929 | !MayReadMutableMembers) { |
2930 | Info.FFDiag(E, diag::note_constexpr_ltor_mutable, 1) |
2931 | << Field; |
2932 | Info.Note(Field->getLocation(), diag::note_declared_at); |
2933 | return handler.failed(); |
2934 | } |
2935 | |
2936 | |
2937 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
2938 | if (RD->isUnion()) { |
2939 | const FieldDecl *UnionField = O->getUnionField(); |
2940 | if (!UnionField || |
2941 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
2942 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
2943 | << handler.AccessKind << Field << !UnionField << UnionField; |
2944 | return handler.failed(); |
2945 | } |
2946 | O = &O->getUnionValue(); |
2947 | } else |
2948 | O = &O->getStructField(Field->getFieldIndex()); |
2949 | |
2950 | bool WasConstQualified = ObjType.isConstQualified(); |
2951 | ObjType = Field->getType(); |
2952 | if (WasConstQualified && !Field->isMutable()) |
2953 | ObjType.addConst(); |
2954 | |
2955 | if (ObjType.isVolatileQualified()) { |
2956 | if (Info.getLangOpts().CPlusPlus) { |
2957 | |
2958 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
2959 | << handler.AccessKind << 2 << Field; |
2960 | Info.Note(Field->getLocation(), diag::note_declared_at); |
2961 | } else { |
2962 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2963 | } |
2964 | return handler.failed(); |
2965 | } |
2966 | |
2967 | LastField = Field; |
2968 | } else { |
2969 | |
2970 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
2971 | const CXXRecordDecl *Base = getAsBaseClass(Sub.Entries[I]); |
2972 | O = &O->getStructBase(getBaseIndex(Derived, Base)); |
2973 | |
2974 | bool WasConstQualified = ObjType.isConstQualified(); |
2975 | ObjType = Info.Ctx.getRecordType(Base); |
2976 | if (WasConstQualified) |
2977 | ObjType.addConst(); |
2978 | } |
2979 | } |
2980 | } |
2981 | |
2982 | namespace { |
2983 | struct ExtractSubobjectHandler { |
2984 | EvalInfo &Info; |
2985 | APValue &Result; |
2986 | |
2987 | static const AccessKinds AccessKind = AK_Read; |
2988 | |
2989 | typedef bool result_type; |
2990 | bool failed() { return false; } |
2991 | bool found(APValue &Subobj, QualType SubobjType) { |
2992 | Result = Subobj; |
2993 | return true; |
2994 | } |
2995 | bool found(APSInt &Value, QualType SubobjType) { |
2996 | Result = APValue(Value); |
2997 | return true; |
2998 | } |
2999 | bool found(APFloat &Value, QualType SubobjType) { |
3000 | Result = APValue(Value); |
3001 | return true; |
3002 | } |
3003 | }; |
3004 | } |
3005 | |
3006 | const AccessKinds ExtractSubobjectHandler::AccessKind; |
3007 | |
3008 | |
3009 | static bool (EvalInfo &Info, const Expr *E, |
3010 | const CompleteObject &Obj, |
3011 | const SubobjectDesignator &Sub, |
3012 | APValue &Result) { |
3013 | ExtractSubobjectHandler Handler = { Info, Result }; |
3014 | return findSubobject(Info, E, Obj, Sub, Handler); |
3015 | } |
3016 | |
3017 | namespace { |
3018 | struct ModifySubobjectHandler { |
3019 | EvalInfo &Info; |
3020 | APValue &NewVal; |
3021 | const Expr *E; |
3022 | |
3023 | typedef bool result_type; |
3024 | static const AccessKinds AccessKind = AK_Assign; |
3025 | |
3026 | bool checkConst(QualType QT) { |
3027 | |
3028 | if (QT.isConstQualified()) { |
3029 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3030 | return false; |
3031 | } |
3032 | return true; |
3033 | } |
3034 | |
3035 | bool failed() { return false; } |
3036 | bool found(APValue &Subobj, QualType SubobjType) { |
3037 | if (!checkConst(SubobjType)) |
3038 | return false; |
3039 | |
3040 | Subobj.swap(NewVal); |
3041 | return true; |
3042 | } |
3043 | bool found(APSInt &Value, QualType SubobjType) { |
3044 | if (!checkConst(SubobjType)) |
3045 | return false; |
3046 | if (!NewVal.isInt()) { |
3047 | |
3048 | Info.FFDiag(E); |
3049 | return false; |
3050 | } |
3051 | Value = NewVal.getInt(); |
3052 | return true; |
3053 | } |
3054 | bool found(APFloat &Value, QualType SubobjType) { |
3055 | if (!checkConst(SubobjType)) |
3056 | return false; |
3057 | Value = NewVal.getFloat(); |
3058 | return true; |
3059 | } |
3060 | }; |
3061 | } |
3062 | |
3063 | const AccessKinds ModifySubobjectHandler::AccessKind; |
3064 | |
3065 | |
3066 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
3067 | const CompleteObject &Obj, |
3068 | const SubobjectDesignator &Sub, |
3069 | APValue &NewVal) { |
3070 | ModifySubobjectHandler Handler = { Info, NewVal, E }; |
3071 | return findSubobject(Info, E, Obj, Sub, Handler); |
3072 | } |
3073 | |
3074 | |
3075 | |
3076 | static unsigned FindDesignatorMismatch(QualType ObjType, |
3077 | const SubobjectDesignator &A, |
3078 | const SubobjectDesignator &B, |
3079 | bool &WasArrayIndex) { |
3080 | unsigned I = 0, N = std::min(A.Entries.size(), B.Entries.size()); |
3081 | for (; I != N; ++I) { |
3082 | if (!ObjType.isNull() && |
3083 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
3084 | |
3085 | if (A.Entries[I].ArrayIndex != B.Entries[I].ArrayIndex) { |
3086 | WasArrayIndex = true; |
3087 | return I; |
3088 | } |
3089 | if (ObjType->isAnyComplexType()) |
3090 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
3091 | else |
3092 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
3093 | } else { |
3094 | if (A.Entries[I].BaseOrMember != B.Entries[I].BaseOrMember) { |
3095 | WasArrayIndex = false; |
3096 | return I; |
3097 | } |
3098 | if (const FieldDecl *FD = getAsField(A.Entries[I])) |
3099 | |
3100 | ObjType = FD->getType(); |
3101 | else |
3102 | |
3103 | ObjType = QualType(); |
3104 | } |
3105 | } |
3106 | WasArrayIndex = false; |
3107 | return I; |
3108 | } |
3109 | |
3110 | |
3111 | |
3112 | static bool AreElementsOfSameArray(QualType ObjType, |
3113 | const SubobjectDesignator &A, |
3114 | const SubobjectDesignator &B) { |
3115 | if (A.Entries.size() != B.Entries.size()) |
3116 | return false; |
3117 | |
3118 | bool IsArray = A.MostDerivedIsArrayElement; |
3119 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
3120 | |
3121 | return false; |
3122 | |
3123 | |
3124 | |
3125 | |
3126 | bool WasArrayIndex; |
3127 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
3128 | return CommonLength >= A.Entries.size() - IsArray; |
3129 | } |
3130 | |
3131 | |
3132 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
3133 | AccessKinds AK, const LValue &LVal, |
3134 | QualType LValType) { |
3135 | if (!LVal.Base) { |
3136 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
3137 | return CompleteObject(); |
3138 | } |
3139 | |
3140 | CallStackFrame *Frame = nullptr; |
3141 | if (LVal.getLValueCallIndex()) { |
3142 | Frame = Info.getCallFrame(LVal.getLValueCallIndex()); |
3143 | if (!Frame) { |
3144 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
3145 | << AK << LVal.Base.is<const ValueDecl*>(); |
3146 | NoteLValueLocation(Info, LVal.Base); |
3147 | return CompleteObject(); |
3148 | } |
3149 | } |
3150 | |
3151 | |
3152 | |
3153 | |
3154 | |
3155 | if (LValType.isVolatileQualified()) { |
3156 | if (Info.getLangOpts().CPlusPlus) |
3157 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
3158 | << AK << LValType; |
3159 | else |
3160 | Info.FFDiag(E); |
3161 | return CompleteObject(); |
3162 | } |
3163 | |
3164 | |
3165 | APValue *BaseVal = nullptr; |
3166 | QualType BaseType = getType(LVal.Base); |
3167 | bool LifetimeStartedInEvaluation = Frame; |
3168 | |
3169 | if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl*>()) { |
3170 | |
3171 | |
3172 | |
3173 | |
3174 | |
3175 | |
3176 | |
3177 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
3178 | if (VD) { |
3179 | if (const VarDecl *VDef = VD->getDefinition(Info.Ctx)) |
3180 | VD = VDef; |
3181 | } |
3182 | if (!VD || VD->isInvalidDecl()) { |
3183 | Info.FFDiag(E); |
3184 | return CompleteObject(); |
3185 | } |
3186 | |
3187 | |
3188 | if (BaseType.isVolatileQualified()) { |
3189 | if (Info.getLangOpts().CPlusPlus) { |
3190 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3191 | << AK << 1 << VD; |
3192 | Info.Note(VD->getLocation(), diag::note_declared_at); |
3193 | } else { |
3194 | Info.FFDiag(E); |
3195 | } |
3196 | return CompleteObject(); |
3197 | } |
3198 | |
3199 | |
3200 | |
3201 | if (!Frame) { |
3202 | if (Info.getLangOpts().CPlusPlus14 && |
3203 | VD == Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()) { |
3204 | |
3205 | |
3206 | |
3207 | } else if (AK != AK_Read) { |
3208 | |
3209 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
3210 | return CompleteObject(); |
3211 | } else if (VD->isConstexpr()) { |
3212 | |
3213 | } else if (BaseType->isIntegralOrEnumerationType()) { |
3214 | |
3215 | if (!(BaseType.isConstQualified() || |
3216 | (Info.getLangOpts().OpenCL && |
3217 | BaseType.getAddressSpace() == LangAS::opencl_constant))) { |
3218 | if (Info.getLangOpts().CPlusPlus) { |
3219 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
3220 | Info.Note(VD->getLocation(), diag::note_declared_at); |
3221 | } else { |
3222 | Info.FFDiag(E); |
3223 | } |
3224 | return CompleteObject(); |
3225 | } |
3226 | } else if (BaseType->isFloatingType() && BaseType.isConstQualified()) { |
3227 | |
3228 | |
3229 | |
3230 | if (Info.getLangOpts().CPlusPlus11) { |
3231 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
3232 | Info.Note(VD->getLocation(), diag::note_declared_at); |
3233 | } else { |
3234 | Info.CCEDiag(E); |
3235 | } |
3236 | } else if (BaseType.isConstQualified() && VD->hasDefinition(Info.Ctx)) { |
3237 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_constexpr) << VD; |
3238 | |
3239 | } else { |
3240 | |
3241 | if (Info.checkingPotentialConstantExpression() && |
3242 | VD->getType().isConstQualified() && !VD->hasDefinition(Info.Ctx)) { |
3243 | |
3244 | |
3245 | } else if (Info.getLangOpts().CPlusPlus11) { |
3246 | Info.FFDiag(E, diag::note_constexpr_ltor_non_constexpr, 1) << VD; |
3247 | Info.Note(VD->getLocation(), diag::note_declared_at); |
3248 | } else { |
3249 | Info.FFDiag(E); |
3250 | } |
3251 | return CompleteObject(); |
3252 | } |
3253 | } |
3254 | |
3255 | if (!evaluateVarDeclInit(Info, E, VD, Frame, BaseVal, &LVal)) |
3256 | return CompleteObject(); |
3257 | } else { |
3258 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
3259 | |
3260 | if (!Frame) { |
3261 | if (const MaterializeTemporaryExpr *MTE = |
3262 | dyn_cast<MaterializeTemporaryExpr>(Base)) { |
3263 | (0) . __assert_fail ("MTE->getStorageDuration() == SD_Static && \"should have a frame for a non-global materialized temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3264, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MTE->getStorageDuration() == SD_Static && |
3264 | (0) . __assert_fail ("MTE->getStorageDuration() == SD_Static && \"should have a frame for a non-global materialized temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3264, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "should have a frame for a non-global materialized temporary"); |
3265 | |
3266 | |
3267 | |
3268 | |
3269 | |
3270 | |
3271 | |
3272 | |
3273 | |
3274 | |
3275 | |
3276 | |
3277 | |
3278 | |
3279 | |
3280 | const ValueDecl *VD = Info.EvaluatingDecl.dyn_cast<const ValueDecl*>(); |
3281 | const ValueDecl *ED = MTE->getExtendingDecl(); |
3282 | if (!(BaseType.isConstQualified() && |
3283 | BaseType->isIntegralOrEnumerationType()) && |
3284 | !(VD && VD->getCanonicalDecl() == ED->getCanonicalDecl())) { |
3285 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
3286 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
3287 | return CompleteObject(); |
3288 | } |
3289 | |
3290 | BaseVal = Info.Ctx.getMaterializedTemporaryValue(MTE, false); |
3291 | (0) . __assert_fail ("BaseVal && \"got reference to unevaluated temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3291, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseVal && "got reference to unevaluated temporary"); |
3292 | LifetimeStartedInEvaluation = true; |
3293 | } else { |
3294 | Info.FFDiag(E); |
3295 | return CompleteObject(); |
3296 | } |
3297 | } else { |
3298 | BaseVal = Frame->getTemporary(Base, LVal.Base.getVersion()); |
3299 | (0) . __assert_fail ("BaseVal && \"missing value for temporary\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3299, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseVal && "missing value for temporary"); |
3300 | } |
3301 | |
3302 | |
3303 | if (BaseType.isVolatileQualified()) { |
3304 | if (Info.getLangOpts().CPlusPlus) { |
3305 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3306 | << AK << 0; |
3307 | Info.Note(Base->getExprLoc(), diag::note_constexpr_temporary_here); |
3308 | } else { |
3309 | Info.FFDiag(E); |
3310 | } |
3311 | return CompleteObject(); |
3312 | } |
3313 | } |
3314 | |
3315 | |
3316 | |
3317 | |
3318 | if (Info.isEvaluatingConstructor(LVal.getLValueBase(), |
3319 | LVal.getLValueCallIndex(), |
3320 | LVal.getLValueVersion())) { |
3321 | BaseType = Info.Ctx.getCanonicalType(BaseType); |
3322 | BaseType.removeLocalConst(); |
3323 | LifetimeStartedInEvaluation = true; |
3324 | } |
3325 | |
3326 | |
3327 | |
3328 | |
3329 | |
3330 | |
3331 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
3332 | Info.EvalStatus.HasSideEffects) || |
3333 | (AK != AK_Read && Info.IsSpeculativelyEvaluating)) |
3334 | return CompleteObject(); |
3335 | |
3336 | return CompleteObject(BaseVal, BaseType, LifetimeStartedInEvaluation); |
3337 | } |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | |
3343 | |
3344 | |
3345 | |
3346 | |
3347 | |
3348 | |
3349 | |
3350 | static bool handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, |
3351 | QualType Type, |
3352 | const LValue &LVal, APValue &RVal) { |
3353 | if (LVal.Designator.Invalid) |
3354 | return false; |
3355 | |
3356 | |
3357 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
3358 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
3359 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Base)) { |
3360 | |
3361 | |
3362 | |
3363 | if (Type.isVolatileQualified()) { |
3364 | Info.FFDiag(Conv); |
3365 | return false; |
3366 | } |
3367 | APValue Lit; |
3368 | if (!Evaluate(Lit, Info, CLE->getInitializer())) |
3369 | return false; |
3370 | CompleteObject LitObj(&Lit, Base->getType(), false); |
3371 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal); |
3372 | } else if (isa<StringLiteral>(Base) || isa<PredefinedExpr>(Base)) { |
3373 | |
3374 | |
3375 | (0) . __assert_fail ("LVal.Designator.Entries.size() <= 1 && \"Can only read characters from string literals\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3376, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(LVal.Designator.Entries.size() <= 1 && |
3376 | (0) . __assert_fail ("LVal.Designator.Entries.size() <= 1 && \"Can only read characters from string literals\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3376, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can only read characters from string literals"); |
3377 | if (LVal.Designator.Entries.empty()) { |
3378 | |
3379 | |
3380 | |
3381 | Info.FFDiag(Conv); |
3382 | return false; |
3383 | } |
3384 | if (LVal.Designator.isOnePastTheEnd()) { |
3385 | if (Info.getLangOpts().CPlusPlus11) |
3386 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK_Read; |
3387 | else |
3388 | Info.FFDiag(Conv); |
3389 | return false; |
3390 | } |
3391 | uint64_t CharIndex = LVal.Designator.Entries[0].ArrayIndex; |
3392 | RVal = APValue(extractStringLiteralCharacter(Info, Base, CharIndex)); |
3393 | return true; |
3394 | } |
3395 | } |
3396 | |
3397 | CompleteObject Obj = findCompleteObject(Info, Conv, AK_Read, LVal, Type); |
3398 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal); |
3399 | } |
3400 | |
3401 | |
3402 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
3403 | QualType LValType, APValue &Val) { |
3404 | if (LVal.Designator.Invalid) |
3405 | return false; |
3406 | |
3407 | if (!Info.getLangOpts().CPlusPlus14) { |
3408 | Info.FFDiag(E); |
3409 | return false; |
3410 | } |
3411 | |
3412 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
3413 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
3414 | } |
3415 | |
3416 | namespace { |
3417 | struct CompoundAssignSubobjectHandler { |
3418 | EvalInfo &Info; |
3419 | const Expr *E; |
3420 | QualType PromotedLHSType; |
3421 | BinaryOperatorKind Opcode; |
3422 | const APValue &RHS; |
3423 | |
3424 | static const AccessKinds AccessKind = AK_Assign; |
3425 | |
3426 | typedef bool result_type; |
3427 | |
3428 | bool checkConst(QualType QT) { |
3429 | |
3430 | if (QT.isConstQualified()) { |
3431 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3432 | return false; |
3433 | } |
3434 | return true; |
3435 | } |
3436 | |
3437 | bool failed() { return false; } |
3438 | bool found(APValue &Subobj, QualType SubobjType) { |
3439 | switch (Subobj.getKind()) { |
3440 | case APValue::Int: |
3441 | return found(Subobj.getInt(), SubobjType); |
3442 | case APValue::Float: |
3443 | return found(Subobj.getFloat(), SubobjType); |
3444 | case APValue::ComplexInt: |
3445 | case APValue::ComplexFloat: |
3446 | |
3447 | Info.FFDiag(E); |
3448 | return false; |
3449 | case APValue::LValue: |
3450 | return foundPointer(Subobj, SubobjType); |
3451 | default: |
3452 | |
3453 | Info.FFDiag(E); |
3454 | return false; |
3455 | } |
3456 | } |
3457 | bool found(APSInt &Value, QualType SubobjType) { |
3458 | if (!checkConst(SubobjType)) |
3459 | return false; |
3460 | |
3461 | if (!SubobjType->isIntegerType()) { |
3462 | |
3463 | |
3464 | Info.FFDiag(E); |
3465 | return false; |
3466 | } |
3467 | |
3468 | if (RHS.isInt()) { |
3469 | APSInt LHS = |
3470 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
3471 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
3472 | return false; |
3473 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
3474 | return true; |
3475 | } else if (RHS.isFloat()) { |
3476 | APFloat FValue(0.0); |
3477 | return HandleIntToFloatCast(Info, E, SubobjType, Value, PromotedLHSType, |
3478 | FValue) && |
3479 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
3480 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
3481 | Value); |
3482 | } |
3483 | |
3484 | Info.FFDiag(E); |
3485 | return false; |
3486 | } |
3487 | bool found(APFloat &Value, QualType SubobjType) { |
3488 | return checkConst(SubobjType) && |
3489 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
3490 | Value) && |
3491 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
3492 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
3493 | } |
3494 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
3495 | if (!checkConst(SubobjType)) |
3496 | return false; |
3497 | |
3498 | QualType PointeeType; |
3499 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
3500 | PointeeType = PT->getPointeeType(); |
3501 | |
3502 | if (PointeeType.isNull() || !RHS.isInt() || |
3503 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
3504 | Info.FFDiag(E); |
3505 | return false; |
3506 | } |
3507 | |
3508 | APSInt Offset = RHS.getInt(); |
3509 | if (Opcode == BO_Sub) |
3510 | negateAsSigned(Offset); |
3511 | |
3512 | LValue LVal; |
3513 | LVal.setFrom(Info.Ctx, Subobj); |
3514 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
3515 | return false; |
3516 | LVal.moveInto(Subobj); |
3517 | return true; |
3518 | } |
3519 | }; |
3520 | } |
3521 | |
3522 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
3523 | |
3524 | |
3525 | static bool handleCompoundAssignment( |
3526 | EvalInfo &Info, const Expr *E, |
3527 | const LValue &LVal, QualType LValType, QualType PromotedLValType, |
3528 | BinaryOperatorKind Opcode, const APValue &RVal) { |
3529 | if (LVal.Designator.Invalid) |
3530 | return false; |
3531 | |
3532 | if (!Info.getLangOpts().CPlusPlus14) { |
3533 | Info.FFDiag(E); |
3534 | return false; |
3535 | } |
3536 | |
3537 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
3538 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
3539 | RVal }; |
3540 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
3541 | } |
3542 | |
3543 | namespace { |
3544 | struct IncDecSubobjectHandler { |
3545 | EvalInfo &Info; |
3546 | const UnaryOperator *E; |
3547 | AccessKinds AccessKind; |
3548 | APValue *Old; |
3549 | |
3550 | typedef bool result_type; |
3551 | |
3552 | bool checkConst(QualType QT) { |
3553 | |
3554 | if (QT.isConstQualified()) { |
3555 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3556 | return false; |
3557 | } |
3558 | return true; |
3559 | } |
3560 | |
3561 | bool failed() { return false; } |
3562 | bool found(APValue &Subobj, QualType SubobjType) { |
3563 | |
3564 | |
3565 | if (Old) { |
3566 | *Old = Subobj; |
3567 | Old = nullptr; |
3568 | } |
3569 | |
3570 | switch (Subobj.getKind()) { |
3571 | case APValue::Int: |
3572 | return found(Subobj.getInt(), SubobjType); |
3573 | case APValue::Float: |
3574 | return found(Subobj.getFloat(), SubobjType); |
3575 | case APValue::ComplexInt: |
3576 | return found(Subobj.getComplexIntReal(), |
3577 | SubobjType->castAs<ComplexType>()->getElementType() |
3578 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
3579 | case APValue::ComplexFloat: |
3580 | return found(Subobj.getComplexFloatReal(), |
3581 | SubobjType->castAs<ComplexType>()->getElementType() |
3582 | .withCVRQualifiers(SubobjType.getCVRQualifiers())); |
3583 | case APValue::LValue: |
3584 | return foundPointer(Subobj, SubobjType); |
3585 | default: |
3586 | |
3587 | Info.FFDiag(E); |
3588 | return false; |
3589 | } |
3590 | } |
3591 | bool found(APSInt &Value, QualType SubobjType) { |
3592 | if (!checkConst(SubobjType)) |
3593 | return false; |
3594 | |
3595 | if (!SubobjType->isIntegerType()) { |
3596 | |
3597 | |
3598 | Info.FFDiag(E); |
3599 | return false; |
3600 | } |
3601 | |
3602 | if (Old) *Old = APValue(Value); |
3603 | |
3604 | |
3605 | |
3606 | if (SubobjType->isBooleanType()) { |
3607 | if (AccessKind == AK_Increment) |
3608 | Value = 1; |
3609 | else |
3610 | Value = !Value; |
3611 | return true; |
3612 | } |
3613 | |
3614 | bool WasNegative = Value.isNegative(); |
3615 | if (AccessKind == AK_Increment) { |
3616 | ++Value; |
3617 | |
3618 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
3619 | APSInt ActualValue(Value, ); |
3620 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
3621 | } |
3622 | } else { |
3623 | --Value; |
3624 | |
3625 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
3626 | unsigned BitWidth = Value.getBitWidth(); |
3627 | APSInt ActualValue(Value.sext(BitWidth + 1), ); |
3628 | ActualValue.setBit(BitWidth); |
3629 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
3630 | } |
3631 | } |
3632 | return true; |
3633 | } |
3634 | bool found(APFloat &Value, QualType SubobjType) { |
3635 | if (!checkConst(SubobjType)) |
3636 | return false; |
3637 | |
3638 | if (Old) *Old = APValue(Value); |
3639 | |
3640 | APFloat One(Value.getSemantics(), 1); |
3641 | if (AccessKind == AK_Increment) |
3642 | Value.add(One, APFloat::rmNearestTiesToEven); |
3643 | else |
3644 | Value.subtract(One, APFloat::rmNearestTiesToEven); |
3645 | return true; |
3646 | } |
3647 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
3648 | if (!checkConst(SubobjType)) |
3649 | return false; |
3650 | |
3651 | QualType PointeeType; |
3652 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
3653 | PointeeType = PT->getPointeeType(); |
3654 | else { |
3655 | Info.FFDiag(E); |
3656 | return false; |
3657 | } |
3658 | |
3659 | LValue LVal; |
3660 | LVal.setFrom(Info.Ctx, Subobj); |
3661 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
3662 | AccessKind == AK_Increment ? 1 : -1)) |
3663 | return false; |
3664 | LVal.moveInto(Subobj); |
3665 | return true; |
3666 | } |
3667 | }; |
3668 | } |
3669 | |
3670 | |
3671 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
3672 | QualType LValType, bool IsIncrement, APValue *Old) { |
3673 | if (LVal.Designator.Invalid) |
3674 | return false; |
3675 | |
3676 | if (!Info.getLangOpts().CPlusPlus14) { |
3677 | Info.FFDiag(E); |
3678 | return false; |
3679 | } |
3680 | |
3681 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
3682 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
3683 | IncDecSubobjectHandler Handler = {Info, cast<UnaryOperator>(E), AK, Old}; |
3684 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
3685 | } |
3686 | |
3687 | |
3688 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
3689 | LValue &This) { |
3690 | if (Object->getType()->isPointerType()) |
3691 | return EvaluatePointer(Object, This, Info); |
3692 | |
3693 | if (Object->isGLValue()) |
3694 | return EvaluateLValue(Object, This, Info); |
3695 | |
3696 | if (Object->getType()->isLiteralType(Info.Ctx)) |
3697 | return EvaluateTemporary(Object, This, Info); |
3698 | |
3699 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
3700 | return false; |
3701 | } |
3702 | |
3703 | |
3704 | |
3705 | |
3706 | |
3707 | |
3708 | |
3709 | |
3710 | |
3711 | |
3712 | |
3713 | |
3714 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
3715 | QualType LVType, |
3716 | LValue &LV, |
3717 | const Expr *RHS, |
3718 | bool IncludeMember = true) { |
3719 | MemberPtr MemPtr; |
3720 | if (!EvaluateMemberPointer(RHS, MemPtr, Info)) |
3721 | return nullptr; |
3722 | |
3723 | |
3724 | |
3725 | if (!MemPtr.getDecl()) { |
3726 | |
3727 | Info.FFDiag(RHS); |
3728 | return nullptr; |
3729 | } |
3730 | |
3731 | if (MemPtr.isDerivedMember()) { |
3732 | |
3733 | |
3734 | |
3735 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
3736 | LV.Designator.Entries.size()) { |
3737 | Info.FFDiag(RHS); |
3738 | return nullptr; |
3739 | } |
3740 | unsigned PathLengthToMember = |
3741 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
3742 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
3743 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
3744 | LV.Designator.Entries[PathLengthToMember + I]); |
3745 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
3746 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
3747 | Info.FFDiag(RHS); |
3748 | return nullptr; |
3749 | } |
3750 | } |
3751 | |
3752 | |
3753 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
3754 | PathLengthToMember)) |
3755 | return nullptr; |
3756 | } else if (!MemPtr.Path.empty()) { |
3757 | |
3758 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
3759 | MemPtr.Path.size() + IncludeMember); |
3760 | |
3761 | |
3762 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
3763 | LVType = PT->getPointeeType(); |
3764 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
3765 | (0) . __assert_fail ("RD && \"member pointer access on non-class-type expression\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3765, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RD && "member pointer access on non-class-type expression"); |
3766 | |
3767 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
3768 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
3769 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, Base)) |
3770 | return nullptr; |
3771 | RD = Base; |
3772 | } |
3773 | |
3774 | if (!HandleLValueDirectBase(Info, RHS, LV, RD, |
3775 | MemPtr.getContainingRecord())) |
3776 | return nullptr; |
3777 | } |
3778 | |
3779 | |
3780 | if (IncludeMember) { |
3781 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(MemPtr.getDecl())) { |
3782 | if (!HandleLValueMember(Info, RHS, LV, FD)) |
3783 | return nullptr; |
3784 | } else if (const IndirectFieldDecl *IFD = |
3785 | dyn_cast<IndirectFieldDecl>(MemPtr.getDecl())) { |
3786 | if (!HandleLValueIndirectMember(Info, RHS, LV, IFD)) |
3787 | return nullptr; |
3788 | } else { |
3789 | llvm_unreachable("can't construct reference to bound member function"); |
3790 | } |
3791 | } |
3792 | |
3793 | return MemPtr.getDecl(); |
3794 | } |
3795 | |
3796 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
3797 | const BinaryOperator *BO, |
3798 | LValue &LV, |
3799 | bool IncludeMember = true) { |
3800 | getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 3800, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
3801 | |
3802 | if (!EvaluateObjectArgument(Info, BO->getLHS(), LV)) { |
3803 | if (Info.noteFailure()) { |
3804 | MemberPtr MemPtr; |
3805 | EvaluateMemberPointer(BO->getRHS(), MemPtr, Info); |
3806 | } |
3807 | return nullptr; |
3808 | } |
3809 | |
3810 | return HandleMemberPointerAccess(Info, BO->getLHS()->getType(), LV, |
3811 | BO->getRHS(), IncludeMember); |
3812 | } |
3813 | |
3814 | |
3815 | |
3816 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
3817 | LValue &Result) { |
3818 | SubobjectDesignator &D = Result.Designator; |
3819 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
3820 | return false; |
3821 | |
3822 | QualType TargetQT = E->getType(); |
3823 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
3824 | TargetQT = PT->getPointeeType(); |
3825 | |
3826 | |
3827 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
3828 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
3829 | << D.MostDerivedType << TargetQT; |
3830 | return false; |
3831 | } |
3832 | |
3833 | |
3834 | |
3835 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
3836 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
3837 | const CXXRecordDecl *FinalType; |
3838 | if (NewEntriesSize == D.MostDerivedPathLength) |
3839 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
3840 | else |
3841 | FinalType = getAsBaseClass(D.Entries[NewEntriesSize - 1]); |
3842 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
3843 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
3844 | << D.MostDerivedType << TargetQT; |
3845 | return false; |
3846 | } |
3847 | |
3848 | |
3849 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
3850 | } |
3851 | |
3852 | namespace { |
3853 | enum EvalStmtResult { |
3854 | |
3855 | ESR_Failed, |
3856 | |
3857 | ESR_Returned, |
3858 | |
3859 | ESR_Succeeded, |
3860 | |
3861 | ESR_Continue, |
3862 | |
3863 | ESR_Break, |
3864 | |
3865 | ESR_CaseNotFound |
3866 | }; |
3867 | } |
3868 | |
3869 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
3870 | |
3871 | if (!VD->hasLocalStorage()) |
3872 | return true; |
3873 | |
3874 | LValue Result; |
3875 | APValue &Val = createTemporary(VD, true, Result, *Info.CurrentCall); |
3876 | |
3877 | const Expr *InitE = VD->getInit(); |
3878 | if (!InitE) { |
3879 | Info.FFDiag(VD->getBeginLoc(), diag::note_constexpr_uninitialized) |
3880 | << false << VD->getType(); |
3881 | Val = APValue(); |
3882 | return false; |
3883 | } |
3884 | |
3885 | if (InitE->isValueDependent()) |
3886 | return false; |
3887 | |
3888 | if (!EvaluateInPlace(Val, Info, Result, InitE)) { |
3889 | |
3890 | |
3891 | Val = APValue(); |
3892 | return false; |
3893 | } |
3894 | |
3895 | return true; |
3896 | } |
3897 | |
3898 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
3899 | bool OK = true; |
3900 | |
3901 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
3902 | OK &= EvaluateVarDecl(Info, VD); |
3903 | |
3904 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D)) |
3905 | for (auto *BD : DD->bindings()) |
3906 | if (auto *VD = BD->getHoldingVar()) |
3907 | OK &= EvaluateDecl(Info, VD); |
3908 | |
3909 | return OK; |
3910 | } |
3911 | |
3912 | |
3913 | |
3914 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
3915 | const Expr *Cond, bool &Result) { |
3916 | FullExpressionRAII Scope(Info); |
3917 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
3918 | return false; |
3919 | return EvaluateAsBooleanCondition(Cond, Result, Info); |
3920 | } |
3921 | |
3922 | namespace { |
3923 | |
3924 | |
3925 | struct StmtResult { |
3926 | |
3927 | APValue &Value; |
3928 | |
3929 | const LValue *Slot; |
3930 | }; |
3931 | |
3932 | struct TempVersionRAII { |
3933 | CallStackFrame &Frame; |
3934 | |
3935 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
3936 | Frame.pushTempVersion(); |
3937 | } |
3938 | |
3939 | ~TempVersionRAII() { |
3940 | Frame.popTempVersion(); |
3941 | } |
3942 | }; |
3943 | |
3944 | } |
3945 | |
3946 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
3947 | const Stmt *S, |
3948 | const SwitchCase *SC = nullptr); |
3949 | |
3950 | |
3951 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
3952 | const Stmt *Body, |
3953 | const SwitchCase *Case = nullptr) { |
3954 | BlockScopeRAII Scope(Info); |
3955 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, Body, Case)) { |
3956 | case ESR_Break: |
3957 | return ESR_Succeeded; |
3958 | case ESR_Succeeded: |
3959 | case ESR_Continue: |
3960 | return ESR_Continue; |
3961 | case ESR_Failed: |
3962 | case ESR_Returned: |
3963 | case ESR_CaseNotFound: |
3964 | return ESR; |
3965 | } |
3966 | llvm_unreachable("Invalid EvalStmtResult!"); |
3967 | } |
3968 | |
3969 | |
3970 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
3971 | const SwitchStmt *SS) { |
3972 | BlockScopeRAII Scope(Info); |
3973 | |
3974 | |
3975 | APSInt Value; |
3976 | { |
3977 | FullExpressionRAII Scope(Info); |
3978 | if (const Stmt *Init = SS->getInit()) { |
3979 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
3980 | if (ESR != ESR_Succeeded) |
3981 | return ESR; |
3982 | } |
3983 | if (SS->getConditionVariable() && |
3984 | !EvaluateDecl(Info, SS->getConditionVariable())) |
3985 | return ESR_Failed; |
3986 | if (!EvaluateInteger(SS->getCond(), Value, Info)) |
3987 | return ESR_Failed; |
3988 | } |
3989 | |
3990 | |
3991 | |
3992 | const SwitchCase *Found = nullptr; |
3993 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
3994 | SC = SC->getNextSwitchCase()) { |
3995 | if (isa<DefaultStmt>(SC)) { |
3996 | Found = SC; |
3997 | continue; |
3998 | } |
3999 | |
4000 | const CaseStmt *CS = cast<CaseStmt>(SC); |
4001 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Info.Ctx); |
4002 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Info.Ctx) |
4003 | : LHS; |
4004 | if (LHS <= Value && Value <= RHS) { |
4005 | Found = SC; |
4006 | break; |
4007 | } |
4008 | } |
4009 | |
4010 | if (!Found) |
4011 | return ESR_Succeeded; |
4012 | |
4013 | |
4014 | switch (EvalStmtResult ESR = EvaluateStmt(Result, Info, SS->getBody(), Found)) { |
4015 | case ESR_Break: |
4016 | return ESR_Succeeded; |
4017 | case ESR_Succeeded: |
4018 | case ESR_Continue: |
4019 | case ESR_Failed: |
4020 | case ESR_Returned: |
4021 | return ESR; |
4022 | case ESR_CaseNotFound: |
4023 | |
4024 | |
4025 | Info.FFDiag(Found->getBeginLoc(), |
4026 | diag::note_constexpr_stmt_expr_unsupported); |
4027 | return ESR_Failed; |
4028 | } |
4029 | llvm_unreachable("Invalid EvalStmtResult!"); |
4030 | } |
4031 | |
4032 | |
4033 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
4034 | const Stmt *S, const SwitchCase *Case) { |
4035 | if (!Info.nextStep(S)) |
4036 | return ESR_Failed; |
4037 | |
4038 | |
4039 | |
4040 | if (Case) { |
4041 | |
4042 | |
4043 | |
4044 | |
4045 | switch (S->getStmtClass()) { |
4046 | case Stmt::CompoundStmtClass: |
4047 | |
4048 | |
4049 | |
4050 | case Stmt::LabelStmtClass: |
4051 | case Stmt::AttributedStmtClass: |
4052 | case Stmt::DoStmtClass: |
4053 | break; |
4054 | |
4055 | case Stmt::CaseStmtClass: |
4056 | case Stmt::DefaultStmtClass: |
4057 | if (Case == S) |
4058 | Case = nullptr; |
4059 | break; |
4060 | |
4061 | case Stmt::IfStmtClass: { |
4062 | |
4063 | |
4064 | const IfStmt *IS = cast<IfStmt>(S); |
4065 | |
4066 | |
4067 | |
4068 | BlockScopeRAII Scope(Info); |
4069 | |
4070 | EvalStmtResult ESR = EvaluateStmt(Result, Info, IS->getThen(), Case); |
4071 | if (ESR != ESR_CaseNotFound || !IS->getElse()) |
4072 | return ESR; |
4073 | return EvaluateStmt(Result, Info, IS->getElse(), Case); |
4074 | } |
4075 | |
4076 | case Stmt::WhileStmtClass: { |
4077 | EvalStmtResult ESR = |
4078 | EvaluateLoopBody(Result, Info, cast<WhileStmt>(S)->getBody(), Case); |
4079 | if (ESR != ESR_Continue) |
4080 | return ESR; |
4081 | break; |
4082 | } |
4083 | |
4084 | case Stmt::ForStmtClass: { |
4085 | const ForStmt *FS = cast<ForStmt>(S); |
4086 | EvalStmtResult ESR = |
4087 | EvaluateLoopBody(Result, Info, FS->getBody(), Case); |
4088 | if (ESR != ESR_Continue) |
4089 | return ESR; |
4090 | if (FS->getInc()) { |
4091 | FullExpressionRAII IncScope(Info); |
4092 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
4093 | return ESR_Failed; |
4094 | } |
4095 | break; |
4096 | } |
4097 | |
4098 | case Stmt::DeclStmtClass: |
4099 | |
4100 | |
4101 | default: |
4102 | return ESR_CaseNotFound; |
4103 | } |
4104 | } |
4105 | |
4106 | switch (S->getStmtClass()) { |
4107 | default: |
4108 | if (const Expr *E = dyn_cast<Expr>(S)) { |
4109 | |
4110 | |
4111 | FullExpressionRAII Scope(Info); |
4112 | if (!EvaluateIgnoredValue(Info, E)) |
4113 | return ESR_Failed; |
4114 | return ESR_Succeeded; |
4115 | } |
4116 | |
4117 | Info.FFDiag(S->getBeginLoc()); |
4118 | return ESR_Failed; |
4119 | |
4120 | case Stmt::NullStmtClass: |
4121 | return ESR_Succeeded; |
4122 | |
4123 | case Stmt::DeclStmtClass: { |
4124 | const DeclStmt *DS = cast<DeclStmt>(S); |
4125 | for (const auto *DclIt : DS->decls()) { |
4126 | |
4127 | |
4128 | |
4129 | FullExpressionRAII Scope(Info); |
4130 | if (!EvaluateDecl(Info, DclIt) && !Info.noteFailure()) |
4131 | return ESR_Failed; |
4132 | } |
4133 | return ESR_Succeeded; |
4134 | } |
4135 | |
4136 | case Stmt::ReturnStmtClass: { |
4137 | const Expr *RetExpr = cast<ReturnStmt>(S)->getRetValue(); |
4138 | FullExpressionRAII Scope(Info); |
4139 | if (RetExpr && |
4140 | !(Result.Slot |
4141 | ? EvaluateInPlace(Result.Value, Info, *Result.Slot, RetExpr) |
4142 | : Evaluate(Result.Value, Info, RetExpr))) |
4143 | return ESR_Failed; |
4144 | return ESR_Returned; |
4145 | } |
4146 | |
4147 | case Stmt::CompoundStmtClass: { |
4148 | BlockScopeRAII Scope(Info); |
4149 | |
4150 | const CompoundStmt *CS = cast<CompoundStmt>(S); |
4151 | for (const auto *BI : CS->body()) { |
4152 | EvalStmtResult ESR = EvaluateStmt(Result, Info, BI, Case); |
4153 | if (ESR == ESR_Succeeded) |
4154 | Case = nullptr; |
4155 | else if (ESR != ESR_CaseNotFound) |
4156 | return ESR; |
4157 | } |
4158 | return Case ? ESR_CaseNotFound : ESR_Succeeded; |
4159 | } |
4160 | |
4161 | case Stmt::IfStmtClass: { |
4162 | const IfStmt *IS = cast<IfStmt>(S); |
4163 | |
4164 | |
4165 | BlockScopeRAII Scope(Info); |
4166 | if (const Stmt *Init = IS->getInit()) { |
4167 | EvalStmtResult ESR = EvaluateStmt(Result, Info, Init); |
4168 | if (ESR != ESR_Succeeded) |
4169 | return ESR; |
4170 | } |
4171 | bool Cond; |
4172 | if (!EvaluateCond(Info, IS->getConditionVariable(), IS->getCond(), Cond)) |
4173 | return ESR_Failed; |
4174 | |
4175 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
4176 | EvalStmtResult ESR = EvaluateStmt(Result, Info, SubStmt); |
4177 | if (ESR != ESR_Succeeded) |
4178 | return ESR; |
4179 | } |
4180 | return ESR_Succeeded; |
4181 | } |
4182 | |
4183 | case Stmt::WhileStmtClass: { |
4184 | const WhileStmt *WS = cast<WhileStmt>(S); |
4185 | while (true) { |
4186 | BlockScopeRAII Scope(Info); |
4187 | bool Continue; |
4188 | if (!EvaluateCond(Info, WS->getConditionVariable(), WS->getCond(), |
4189 | Continue)) |
4190 | return ESR_Failed; |
4191 | if (!Continue) |
4192 | break; |
4193 | |
4194 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, WS->getBody()); |
4195 | if (ESR != ESR_Continue) |
4196 | return ESR; |
4197 | } |
4198 | return ESR_Succeeded; |
4199 | } |
4200 | |
4201 | case Stmt::DoStmtClass: { |
4202 | const DoStmt *DS = cast<DoStmt>(S); |
4203 | bool Continue; |
4204 | do { |
4205 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, DS->getBody(), Case); |
4206 | if (ESR != ESR_Continue) |
4207 | return ESR; |
4208 | Case = nullptr; |
4209 | |
4210 | FullExpressionRAII CondScope(Info); |
4211 | if (!EvaluateAsBooleanCondition(DS->getCond(), Continue, Info)) |
4212 | return ESR_Failed; |
4213 | } while (Continue); |
4214 | return ESR_Succeeded; |
4215 | } |
4216 | |
4217 | case Stmt::ForStmtClass: { |
4218 | const ForStmt *FS = cast<ForStmt>(S); |
4219 | BlockScopeRAII Scope(Info); |
4220 | if (FS->getInit()) { |
4221 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
4222 | if (ESR != ESR_Succeeded) |
4223 | return ESR; |
4224 | } |
4225 | while (true) { |
4226 | BlockScopeRAII Scope(Info); |
4227 | bool Continue = true; |
4228 | if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), |
4229 | FS->getCond(), Continue)) |
4230 | return ESR_Failed; |
4231 | if (!Continue) |
4232 | break; |
4233 | |
4234 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
4235 | if (ESR != ESR_Continue) |
4236 | return ESR; |
4237 | |
4238 | if (FS->getInc()) { |
4239 | FullExpressionRAII IncScope(Info); |
4240 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
4241 | return ESR_Failed; |
4242 | } |
4243 | } |
4244 | return ESR_Succeeded; |
4245 | } |
4246 | |
4247 | case Stmt::CXXForRangeStmtClass: { |
4248 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(S); |
4249 | BlockScopeRAII Scope(Info); |
4250 | |
4251 | |
4252 | if (FS->getInit()) { |
4253 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getInit()); |
4254 | if (ESR != ESR_Succeeded) |
4255 | return ESR; |
4256 | } |
4257 | |
4258 | |
4259 | EvalStmtResult ESR = EvaluateStmt(Result, Info, FS->getRangeStmt()); |
4260 | if (ESR != ESR_Succeeded) |
4261 | return ESR; |
4262 | |
4263 | |
4264 | ESR = EvaluateStmt(Result, Info, FS->getBeginStmt()); |
4265 | if (ESR != ESR_Succeeded) |
4266 | return ESR; |
4267 | ESR = EvaluateStmt(Result, Info, FS->getEndStmt()); |
4268 | if (ESR != ESR_Succeeded) |
4269 | return ESR; |
4270 | |
4271 | while (true) { |
4272 | |
4273 | { |
4274 | bool Continue = true; |
4275 | FullExpressionRAII CondExpr(Info); |
4276 | if (!EvaluateAsBooleanCondition(FS->getCond(), Continue, Info)) |
4277 | return ESR_Failed; |
4278 | if (!Continue) |
4279 | break; |
4280 | } |
4281 | |
4282 | |
4283 | BlockScopeRAII InnerScope(Info); |
4284 | ESR = EvaluateStmt(Result, Info, FS->getLoopVarStmt()); |
4285 | if (ESR != ESR_Succeeded) |
4286 | return ESR; |
4287 | |
4288 | |
4289 | ESR = EvaluateLoopBody(Result, Info, FS->getBody()); |
4290 | if (ESR != ESR_Continue) |
4291 | return ESR; |
4292 | |
4293 | |
4294 | if (!EvaluateIgnoredValue(Info, FS->getInc())) |
4295 | return ESR_Failed; |
4296 | } |
4297 | |
4298 | return ESR_Succeeded; |
4299 | } |
4300 | |
4301 | case Stmt::SwitchStmtClass: |
4302 | return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S)); |
4303 | |
4304 | case Stmt::ContinueStmtClass: |
4305 | return ESR_Continue; |
4306 | |
4307 | case Stmt::BreakStmtClass: |
4308 | return ESR_Break; |
4309 | |
4310 | case Stmt::LabelStmtClass: |
4311 | return EvaluateStmt(Result, Info, cast<LabelStmt>(S)->getSubStmt(), Case); |
4312 | |
4313 | case Stmt::AttributedStmtClass: |
4314 | |
4315 | |
4316 | return EvaluateStmt(Result, Info, cast<AttributedStmt>(S)->getSubStmt(), |
4317 | Case); |
4318 | |
4319 | case Stmt::CaseStmtClass: |
4320 | case Stmt::DefaultStmtClass: |
4321 | return EvaluateStmt(Result, Info, cast<SwitchCase>(S)->getSubStmt(), Case); |
4322 | case Stmt::CXXTryStmtClass: |
4323 | |
4324 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(S)->getTryBlock(), Case); |
4325 | } |
4326 | } |
4327 | |
4328 | |
4329 | |
4330 | |
4331 | |
4332 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
4333 | const CXXConstructorDecl *CD, |
4334 | bool IsValueInitialization) { |
4335 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
4336 | return false; |
4337 | |
4338 | |
4339 | |
4340 | |
4341 | if (!CD->isConstexpr() && !IsValueInitialization) { |
4342 | if (Info.getLangOpts().CPlusPlus11) { |
4343 | |
4344 | |
4345 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
4346 | << << << CD; |
4347 | Info.Note(CD->getLocation(), diag::note_declared_at); |
4348 | } else { |
4349 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
4350 | } |
4351 | } |
4352 | return true; |
4353 | } |
4354 | |
4355 | |
4356 | |
4357 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
4358 | const FunctionDecl *Declaration, |
4359 | const FunctionDecl *Definition, |
4360 | const Stmt *Body) { |
4361 | |
4362 | |
4363 | if (Info.checkingPotentialConstantExpression() && !Definition && |
4364 | Declaration->isConstexpr()) |
4365 | return false; |
4366 | |
4367 | |
4368 | |
4369 | |
4370 | if (Declaration->isInvalidDecl()) { |
4371 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
4372 | return false; |
4373 | } |
4374 | |
4375 | |
4376 | if (Definition && Definition->isConstexpr() && |
4377 | !Definition->isInvalidDecl() && Body) |
4378 | return true; |
4379 | |
4380 | if (Info.getLangOpts().CPlusPlus11) { |
4381 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
4382 | |
4383 | |
4384 | |
4385 | auto *CD = dyn_cast<CXXConstructorDecl>(DiagDecl); |
4386 | if (CD && CD->isInheritingConstructor()) { |
4387 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
4388 | if (!Inherited->isConstexpr()) |
4389 | DiagDecl = CD = Inherited; |
4390 | } |
4391 | |
4392 | |
4393 | |
4394 | |
4395 | if (CD && CD->isInheritingConstructor()) |
4396 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
4397 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
4398 | else |
4399 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
4400 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
4401 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
4402 | } else { |
4403 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
4404 | } |
4405 | return false; |
4406 | } |
4407 | |
4408 | |
4409 | |
4410 | static bool hasFields(const CXXRecordDecl *RD) { |
4411 | if (!RD || RD->isEmpty()) |
4412 | return false; |
4413 | for (auto *FD : RD->fields()) { |
4414 | if (FD->isUnnamedBitfield()) |
4415 | continue; |
4416 | return true; |
4417 | } |
4418 | for (auto &Base : RD->bases()) |
4419 | if (hasFields(Base.getType()->getAsCXXRecordDecl())) |
4420 | return true; |
4421 | return false; |
4422 | } |
4423 | |
4424 | namespace { |
4425 | typedef SmallVector<APValue, 8> ArgVector; |
4426 | } |
4427 | |
4428 | |
4429 | static bool EvaluateArgs(ArrayRef<const Expr*> Args, ArgVector &ArgValues, |
4430 | EvalInfo &Info) { |
4431 | bool Success = true; |
4432 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
4433 | I != E; ++I) { |
4434 | if (!Evaluate(ArgValues[I - Args.begin()], Info, *I)) { |
4435 | |
4436 | |
4437 | if (!Info.noteFailure()) |
4438 | return false; |
4439 | Success = false; |
4440 | } |
4441 | } |
4442 | return Success; |
4443 | } |
4444 | |
4445 | |
4446 | static bool HandleFunctionCall(SourceLocation CallLoc, |
4447 | const FunctionDecl *Callee, const LValue *This, |
4448 | ArrayRef<const Expr*> Args, const Stmt *Body, |
4449 | EvalInfo &Info, APValue &Result, |
4450 | const LValue *ResultSlot) { |
4451 | ArgVector ArgValues(Args.size()); |
4452 | if (!EvaluateArgs(Args, ArgValues, Info)) |
4453 | return false; |
4454 | |
4455 | if (!Info.CheckCallLimit(CallLoc)) |
4456 | return false; |
4457 | |
4458 | CallStackFrame Frame(Info, CallLoc, Callee, This, ArgValues.data()); |
4459 | |
4460 | |
4461 | |
4462 | |
4463 | |
4464 | |
4465 | |
4466 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Callee); |
4467 | if (MD && MD->isDefaulted() && |
4468 | (MD->getParent()->isUnion() || |
4469 | (MD->isTrivial() && hasFields(MD->getParent())))) { |
4470 | isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4471, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(This && |
4471 | isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4471, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
4472 | LValue RHS; |
4473 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
4474 | APValue RHSValue; |
4475 | if (!handleLValueToRValueConversion(Info, Args[0], Args[0]->getType(), |
4476 | RHS, RHSValue)) |
4477 | return false; |
4478 | if (!handleAssignment(Info, Args[0], *This, MD->getThisType(), |
4479 | RHSValue)) |
4480 | return false; |
4481 | This->moveInto(Result); |
4482 | return true; |
4483 | } else if (MD && isLambdaCallOperator(MD)) { |
4484 | |
4485 | |
4486 | |
4487 | |
4488 | |
4489 | |
4490 | if (!Info.checkingPotentialConstantExpression()) |
4491 | MD->getParent()->getCaptureFields(Frame.LambdaCaptureFields, |
4492 | Frame.LambdaThisCaptureField); |
4493 | } |
4494 | |
4495 | StmtResult Ret = {Result, ResultSlot}; |
4496 | EvalStmtResult ESR = EvaluateStmt(Ret, Info, Body); |
4497 | if (ESR == ESR_Succeeded) { |
4498 | if (Callee->getReturnType()->isVoidType()) |
4499 | return true; |
4500 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
4501 | } |
4502 | return ESR == ESR_Returned; |
4503 | } |
4504 | |
4505 | |
4506 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
4507 | APValue *ArgValues, |
4508 | const CXXConstructorDecl *Definition, |
4509 | EvalInfo &Info, APValue &Result) { |
4510 | SourceLocation CallLoc = E->getExprLoc(); |
4511 | if (!Info.CheckCallLimit(CallLoc)) |
4512 | return false; |
4513 | |
4514 | const CXXRecordDecl *RD = Definition->getParent(); |
4515 | if (RD->getNumVBases()) { |
4516 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
4517 | return false; |
4518 | } |
4519 | |
4520 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
4521 | Info, {This.getLValueBase(), |
4522 | {This.getLValueCallIndex(), This.getLValueVersion()}}); |
4523 | CallStackFrame Frame(Info, CallLoc, Definition, &This, ArgValues); |
4524 | |
4525 | |
4526 | |
4527 | APValue RetVal; |
4528 | StmtResult Ret = {RetVal, nullptr}; |
4529 | |
4530 | |
4531 | if (Definition->isDelegatingConstructor()) { |
4532 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
4533 | { |
4534 | FullExpressionRAII InitScope(Info); |
4535 | if (!EvaluateInPlace(Result, Info, This, (*I)->getInit())) |
4536 | return false; |
4537 | } |
4538 | return EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
4539 | } |
4540 | |
4541 | |
4542 | |
4543 | |
4544 | |
4545 | |
4546 | |
4547 | |
4548 | |
4549 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
4550 | (Definition->getParent()->isUnion() || |
4551 | (Definition->isTrivial() && hasFields(Definition->getParent())))) { |
4552 | LValue RHS; |
4553 | RHS.setFrom(Info.Ctx, ArgValues[0]); |
4554 | return handleLValueToRValueConversion( |
4555 | Info, E, Definition->getParamDecl(0)->getType().getNonReferenceType(), |
4556 | RHS, Result); |
4557 | } |
4558 | |
4559 | |
4560 | if (!RD->isUnion() && Result.isUninit()) |
4561 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
4562 | std::distance(RD->field_begin(), RD->field_end())); |
4563 | |
4564 | if (RD->isInvalidDecl()) return false; |
4565 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
4566 | |
4567 | |
4568 | BlockScopeRAII LifetimeExtendedScope(Info); |
4569 | |
4570 | bool Success = true; |
4571 | unsigned BasesSeen = 0; |
4572 | #ifndef NDEBUG |
4573 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
4574 | #endif |
4575 | for (const auto *I : Definition->inits()) { |
4576 | LValue Subobject = This; |
4577 | LValue SubobjectParent = This; |
4578 | APValue *Value = &Result; |
4579 | |
4580 | |
4581 | FieldDecl *FD = nullptr; |
4582 | if (I->isBaseInitializer()) { |
4583 | QualType BaseType(I->getBaseClass(), 0); |
4584 | #ifndef NDEBUG |
4585 | |
4586 | |
4587 | (0) . __assert_fail ("!BaseIt->isVirtual() && \"virtual base for literal type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4587, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!BaseIt->isVirtual() && "virtual base for literal type"); |
4588 | (0) . __assert_fail ("Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && \"base class initializers not in expected order\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4589, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
4589 | (0) . __assert_fail ("Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && \"base class initializers not in expected order\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4589, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "base class initializers not in expected order"); |
4590 | ++BaseIt; |
4591 | #endif |
4592 | if (!HandleLValueDirectBase(Info, I->getInit(), Subobject, RD, |
4593 | BaseType->getAsCXXRecordDecl(), &Layout)) |
4594 | return false; |
4595 | Value = &Result.getStructBase(BasesSeen++); |
4596 | } else if ((FD = I->getMember())) { |
4597 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD, &Layout)) |
4598 | return false; |
4599 | if (RD->isUnion()) { |
4600 | Result = APValue(FD); |
4601 | Value = &Result.getUnionValue(); |
4602 | } else { |
4603 | Value = &Result.getStructField(FD->getFieldIndex()); |
4604 | } |
4605 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
4606 | |
4607 | |
4608 | auto IndirectFieldChain = IFD->chain(); |
4609 | for (auto *C : IndirectFieldChain) { |
4610 | FD = cast<FieldDecl>(C); |
4611 | CXXRecordDecl *CD = cast<CXXRecordDecl>(FD->getParent()); |
4612 | |
4613 | |
4614 | |
4615 | |
4616 | |
4617 | if (Value->isUninit() || |
4618 | (Value->isUnion() && Value->getUnionField() != FD)) { |
4619 | if (CD->isUnion()) |
4620 | *Value = APValue(FD); |
4621 | else |
4622 | *Value = APValue(APValue::UninitStruct(), CD->getNumBases(), |
4623 | std::distance(CD->field_begin(), CD->field_end())); |
4624 | } |
4625 | |
4626 | |
4627 | if (C == IndirectFieldChain.back()) |
4628 | SubobjectParent = Subobject; |
4629 | if (!HandleLValueMember(Info, I->getInit(), Subobject, FD)) |
4630 | return false; |
4631 | if (CD->isUnion()) |
4632 | Value = &Value->getUnionValue(); |
4633 | else |
4634 | Value = &Value->getStructField(FD->getFieldIndex()); |
4635 | } |
4636 | } else { |
4637 | llvm_unreachable("unknown base initializer kind"); |
4638 | } |
4639 | |
4640 | |
4641 | |
4642 | |
4643 | const Expr *Init = I->getInit(); |
4644 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
4645 | isa<CXXDefaultInitExpr>(Init)); |
4646 | FullExpressionRAII InitScope(Info); |
4647 | if (!EvaluateInPlace(*Value, Info, Subobject, Init) || |
4648 | (FD && FD->isBitField() && |
4649 | !truncateBitfieldValue(Info, Init, *Value, FD))) { |
4650 | |
4651 | |
4652 | if (!Info.noteFailure()) |
4653 | return false; |
4654 | Success = false; |
4655 | } |
4656 | } |
4657 | |
4658 | return Success && |
4659 | EvaluateStmt(Ret, Info, Definition->getBody()) != ESR_Failed; |
4660 | } |
4661 | |
4662 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
4663 | ArrayRef<const Expr*> Args, |
4664 | const CXXConstructorDecl *Definition, |
4665 | EvalInfo &Info, APValue &Result) { |
4666 | ArgVector ArgValues(Args.size()); |
4667 | if (!EvaluateArgs(Args, ArgValues, Info)) |
4668 | return false; |
4669 | |
4670 | return HandleConstructorCall(E, This, ArgValues.data(), Definition, |
4671 | Info, Result); |
4672 | } |
4673 | |
4674 | |
4675 | |
4676 | |
4677 | namespace { |
4678 | |
4679 | template <class Derived> |
4680 | class ExprEvaluatorBase |
4681 | : public ConstStmtVisitor<Derived, bool> { |
4682 | private: |
4683 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
4684 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
4685 | return getDerived().Success(V, E); |
4686 | } |
4687 | bool DerivedZeroInitialization(const Expr *E) { |
4688 | return getDerived().ZeroInitialization(E); |
4689 | } |
4690 | |
4691 | |
4692 | |
4693 | |
4694 | template<typename ConditionalOperator> |
4695 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
4696 | assert(Info.checkingPotentialConstantExpression()); |
4697 | |
4698 | |
4699 | SmallVector<PartialDiagnosticAt, 8> Diag; |
4700 | { |
4701 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
4702 | StmtVisitorTy::Visit(E->getFalseExpr()); |
4703 | if (Diag.empty()) |
4704 | return; |
4705 | } |
4706 | |
4707 | { |
4708 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
4709 | Diag.clear(); |
4710 | StmtVisitorTy::Visit(E->getTrueExpr()); |
4711 | if (Diag.empty()) |
4712 | return; |
4713 | } |
4714 | |
4715 | Error(E, diag::note_constexpr_conditional_never_const); |
4716 | } |
4717 | |
4718 | |
4719 | template<typename ConditionalOperator> |
4720 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
4721 | bool BoolResult; |
4722 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
4723 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
4724 | CheckPotentialConstantConditional(E); |
4725 | return false; |
4726 | } |
4727 | if (Info.noteFailure()) { |
4728 | StmtVisitorTy::Visit(E->getTrueExpr()); |
4729 | StmtVisitorTy::Visit(E->getFalseExpr()); |
4730 | } |
4731 | return false; |
4732 | } |
4733 | |
4734 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
4735 | return StmtVisitorTy::Visit(EvalExpr); |
4736 | } |
4737 | |
4738 | protected: |
4739 | EvalInfo &Info; |
4740 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
4741 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
4742 | |
4743 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
4744 | return Info.CCEDiag(E, D); |
4745 | } |
4746 | |
4747 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
4748 | |
4749 | public: |
4750 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
4751 | |
4752 | EvalInfo &getEvalInfo() { return Info; } |
4753 | |
4754 | |
4755 | |
4756 | bool Error(const Expr *E, diag::kind D) { |
4757 | Info.FFDiag(E, D); |
4758 | return false; |
4759 | } |
4760 | bool Error(const Expr *E) { |
4761 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
4762 | } |
4763 | |
4764 | bool VisitStmt(const Stmt *) { |
4765 | llvm_unreachable("Expression evaluator should not be called on stmts"); |
4766 | } |
4767 | bool VisitExpr(const Expr *E) { |
4768 | return Error(E); |
4769 | } |
4770 | |
4771 | bool VisitConstantExpr(const ConstantExpr *E) |
4772 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
4773 | bool VisitParenExpr(const ParenExpr *E) |
4774 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
4775 | bool VisitUnaryExtension(const UnaryOperator *E) |
4776 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
4777 | bool VisitUnaryPlus(const UnaryOperator *E) |
4778 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
4779 | bool VisitChooseExpr(const ChooseExpr *E) |
4780 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
4781 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
4782 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
4783 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
4784 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
4785 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
4786 | TempVersionRAII RAII(*Info.CurrentCall); |
4787 | return StmtVisitorTy::Visit(E->getExpr()); |
4788 | } |
4789 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
4790 | TempVersionRAII RAII(*Info.CurrentCall); |
4791 | |
4792 | if (!E->getExpr()) |
4793 | return Error(E); |
4794 | return StmtVisitorTy::Visit(E->getExpr()); |
4795 | } |
4796 | |
4797 | |
4798 | bool VisitExprWithCleanups(const ExprWithCleanups *E) |
4799 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
4800 | |
4801 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
4802 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
4803 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
4804 | } |
4805 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
4806 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
4807 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
4808 | } |
4809 | |
4810 | bool VisitBinaryOperator(const BinaryOperator *E) { |
4811 | switch (E->getOpcode()) { |
4812 | default: |
4813 | return Error(E); |
4814 | |
4815 | case BO_Comma: |
4816 | VisitIgnoredValue(E->getLHS()); |
4817 | return StmtVisitorTy::Visit(E->getRHS()); |
4818 | |
4819 | case BO_PtrMemD: |
4820 | case BO_PtrMemI: { |
4821 | LValue Obj; |
4822 | if (!HandleMemberPointerAccess(Info, E, Obj)) |
4823 | return false; |
4824 | APValue Result; |
4825 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
4826 | return false; |
4827 | return DerivedSuccess(Result, E); |
4828 | } |
4829 | } |
4830 | } |
4831 | |
4832 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
4833 | |
4834 | |
4835 | if (!Evaluate(Info.CurrentCall->createTemporary(E->getOpaqueValue(), false), |
4836 | Info, E->getCommon())) |
4837 | return false; |
4838 | |
4839 | return HandleConditionalOperator(E); |
4840 | } |
4841 | |
4842 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
4843 | bool IsBcpCall = false; |
4844 | |
4845 | |
4846 | |
4847 | |
4848 | if (const CallExpr *CallCE = |
4849 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
4850 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
4851 | IsBcpCall = true; |
4852 | |
4853 | |
4854 | |
4855 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
4856 | return false; |
4857 | |
4858 | FoldConstant Fold(Info, IsBcpCall); |
4859 | if (!HandleConditionalOperator(E)) { |
4860 | Fold.keepDiagnostics(); |
4861 | return false; |
4862 | } |
4863 | |
4864 | return true; |
4865 | } |
4866 | |
4867 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
4868 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(E)) |
4869 | return DerivedSuccess(*Value, E); |
4870 | |
4871 | const Expr *Source = E->getSourceExpr(); |
4872 | if (!Source) |
4873 | return Error(E); |
4874 | if (Source == E) { |
4875 | (0) . __assert_fail ("0 && \"OpaqueValueExpr recursively refers to itself\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4875, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 && "OpaqueValueExpr recursively refers to itself"); |
4876 | return Error(E); |
4877 | } |
4878 | return StmtVisitorTy::Visit(Source); |
4879 | } |
4880 | |
4881 | bool VisitCallExpr(const CallExpr *E) { |
4882 | APValue Result; |
4883 | if (!handleCallExpr(E, Result, nullptr)) |
4884 | return false; |
4885 | return DerivedSuccess(Result, E); |
4886 | } |
4887 | |
4888 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
4889 | const LValue *ResultSlot) { |
4890 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
4891 | QualType CalleeType = Callee->getType(); |
4892 | |
4893 | const FunctionDecl *FD = nullptr; |
4894 | LValue *This = nullptr, ThisVal; |
4895 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
4896 | bool HasQualifier = false; |
4897 | |
4898 | |
4899 | if (CalleeType->isSpecificBuiltinType(BuiltinType::BoundMember)) { |
4900 | const ValueDecl *Member = nullptr; |
4901 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
4902 | |
4903 | if (!EvaluateObjectArgument(Info, ME->getBase(), ThisVal)) |
4904 | return false; |
4905 | Member = ME->getMemberDecl(); |
4906 | This = &ThisVal; |
4907 | HasQualifier = ME->hasQualifier(); |
4908 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
4909 | |
4910 | Member = HandleMemberPointerAccess(Info, BE, ThisVal, false); |
4911 | if (!Member) return false; |
4912 | This = &ThisVal; |
4913 | } else |
4914 | return Error(Callee); |
4915 | |
4916 | FD = dyn_cast<FunctionDecl>(Member); |
4917 | if (!FD) |
4918 | return Error(Callee); |
4919 | } else if (CalleeType->isFunctionPointerType()) { |
4920 | LValue Call; |
4921 | if (!EvaluatePointer(Callee, Call, Info)) |
4922 | return false; |
4923 | |
4924 | if (!Call.getLValueOffset().isZero()) |
4925 | return Error(Callee); |
4926 | FD = dyn_cast_or_null<FunctionDecl>( |
4927 | Call.getLValueBase().dyn_cast<const ValueDecl*>()); |
4928 | if (!FD) |
4929 | return Error(Callee); |
4930 | |
4931 | |
4932 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
4933 | CalleeType->getPointeeType(), FD->getType())) { |
4934 | return Error(E); |
4935 | } |
4936 | |
4937 | |
4938 | |
4939 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
4940 | if (MD && !MD->isStatic()) { |
4941 | |
4942 | |
4943 | |
4944 | if (Args.empty()) |
4945 | return Error(E); |
4946 | |
4947 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
4948 | return false; |
4949 | This = &ThisVal; |
4950 | Args = Args.slice(1); |
4951 | } else if (MD && MD->isLambdaStaticInvoker()) { |
4952 | |
4953 | |
4954 | |
4955 | |
4956 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
4957 | (0) . __assert_fail ("ClosureClass->captures_begin() == ClosureClass->captures_end() && \"Number of captures must be zero for conversion to function-ptr\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4959, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert( |
4958 | (0) . __assert_fail ("ClosureClass->captures_begin() == ClosureClass->captures_end() && \"Number of captures must be zero for conversion to function-ptr\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4959, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> ClosureClass->captures_begin() == ClosureClass->captures_end() && |
4959 | (0) . __assert_fail ("ClosureClass->captures_begin() == ClosureClass->captures_end() && \"Number of captures must be zero for conversion to function-ptr\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4959, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Number of captures must be zero for conversion to function-ptr"); |
4960 | |
4961 | const CXXMethodDecl *LambdaCallOp = |
4962 | ClosureClass->getLambdaCallOperator(); |
4963 | |
4964 | |
4965 | |
4966 | |
4967 | |
4968 | if (ClosureClass->isGenericLambda()) { |
4969 | (0) . __assert_fail ("MD->isFunctionTemplateSpecialization() && \"A generic lambda's static-invoker function must be a \" \"template specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4971, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD->isFunctionTemplateSpecialization() && |
4970 | (0) . __assert_fail ("MD->isFunctionTemplateSpecialization() && \"A generic lambda's static-invoker function must be a \" \"template specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4971, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "A generic lambda's static-invoker function must be a " |
4971 | (0) . __assert_fail ("MD->isFunctionTemplateSpecialization() && \"A generic lambda's static-invoker function must be a \" \"template specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4971, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "template specialization"); |
4972 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
4973 | FunctionTemplateDecl *CallOpTemplate = |
4974 | LambdaCallOp->getDescribedFunctionTemplate(); |
4975 | void *InsertPos = nullptr; |
4976 | FunctionDecl *CorrespondingCallOpSpecialization = |
4977 | CallOpTemplate->findSpecialization(TAL->asArray(), InsertPos); |
4978 | (0) . __assert_fail ("CorrespondingCallOpSpecialization && \"We must always have a function call operator specialization \" \"that corresponds to our static invoker specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4980, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CorrespondingCallOpSpecialization && |
4979 | (0) . __assert_fail ("CorrespondingCallOpSpecialization && \"We must always have a function call operator specialization \" \"that corresponds to our static invoker specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4980, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "We must always have a function call operator specialization " |
4980 | (0) . __assert_fail ("CorrespondingCallOpSpecialization && \"We must always have a function call operator specialization \" \"that corresponds to our static invoker specialization\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 4980, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "that corresponds to our static invoker specialization"); |
4981 | FD = cast<CXXMethodDecl>(CorrespondingCallOpSpecialization); |
4982 | } else |
4983 | FD = LambdaCallOp; |
4984 | } |
4985 | |
4986 | |
4987 | } else |
4988 | return Error(E); |
4989 | |
4990 | if (This && !This->checkSubobject(Info, E, CSK_This)) |
4991 | return false; |
4992 | |
4993 | |
4994 | |
4995 | if (This && !HasQualifier && |
4996 | isa<CXXMethodDecl>(FD) && cast<CXXMethodDecl>(FD)->isVirtual()) |
4997 | return Error(E, diag::note_constexpr_virtual_call); |
4998 | |
4999 | const FunctionDecl *Definition = nullptr; |
5000 | Stmt *Body = FD->getBody(Definition); |
5001 | |
5002 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
5003 | !HandleFunctionCall(E->getExprLoc(), Definition, This, Args, Body, Info, |
5004 | Result, ResultSlot)) |
5005 | return false; |
5006 | |
5007 | return true; |
5008 | } |
5009 | |
5010 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
5011 | return StmtVisitorTy::Visit(E->getInitializer()); |
5012 | } |
5013 | bool VisitInitListExpr(const InitListExpr *E) { |
5014 | if (E->getNumInits() == 0) |
5015 | return DerivedZeroInitialization(E); |
5016 | if (E->getNumInits() == 1) |
5017 | return StmtVisitorTy::Visit(E->getInit(0)); |
5018 | return Error(E); |
5019 | } |
5020 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
5021 | return DerivedZeroInitialization(E); |
5022 | } |
5023 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
5024 | return DerivedZeroInitialization(E); |
5025 | } |
5026 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
5027 | return DerivedZeroInitialization(E); |
5028 | } |
5029 | |
5030 | |
5031 | bool VisitMemberExpr(const MemberExpr *E) { |
5032 | (0) . __assert_fail ("!E->isArrow() && \"missing call to bound member function?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5032, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!E->isArrow() && "missing call to bound member function?"); |
5033 | |
5034 | APValue Val; |
5035 | if (!Evaluate(Val, Info, E->getBase())) |
5036 | return false; |
5037 | |
5038 | QualType BaseTy = E->getBase()->getType(); |
5039 | |
5040 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
5041 | if (!FD) return Error(E); |
5042 | (0) . __assert_fail ("!FD->getType()->isReferenceType() && \"prvalue reference?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5042, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!FD->getType()->isReferenceType() && "prvalue reference?"); |
5043 | (0) . __assert_fail ("BaseTy->castAs()->getDecl()->getCanonicalDecl() == FD->getParent()->getCanonicalDecl() && \"record / field mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5044, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
5044 | (0) . __assert_fail ("BaseTy->castAs()->getDecl()->getCanonicalDecl() == FD->getParent()->getCanonicalDecl() && \"record / field mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5044, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
5045 | |
5046 | CompleteObject Obj(&Val, BaseTy, true); |
5047 | SubobjectDesignator Designator(BaseTy); |
5048 | Designator.addDeclUnchecked(FD); |
5049 | |
5050 | APValue Result; |
5051 | return extractSubobject(Info, E, Obj, Designator, Result) && |
5052 | DerivedSuccess(Result, E); |
5053 | } |
5054 | |
5055 | bool VisitCastExpr(const CastExpr *E) { |
5056 | switch (E->getCastKind()) { |
5057 | default: |
5058 | break; |
5059 | |
5060 | case CK_AtomicToNonAtomic: { |
5061 | APValue AtomicVal; |
5062 | |
5063 | |
5064 | |
5065 | if (!Evaluate(AtomicVal, Info, E->getSubExpr())) |
5066 | return false; |
5067 | return DerivedSuccess(AtomicVal, E); |
5068 | } |
5069 | |
5070 | case CK_NoOp: |
5071 | case CK_UserDefinedConversion: |
5072 | return StmtVisitorTy::Visit(E->getSubExpr()); |
5073 | |
5074 | case CK_LValueToRValue: { |
5075 | LValue LVal; |
5076 | if (!EvaluateLValue(E->getSubExpr(), LVal, Info)) |
5077 | return false; |
5078 | APValue RVal; |
5079 | |
5080 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
5081 | LVal, RVal)) |
5082 | return false; |
5083 | return DerivedSuccess(RVal, E); |
5084 | } |
5085 | } |
5086 | |
5087 | return Error(E); |
5088 | } |
5089 | |
5090 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
5091 | return VisitUnaryPostIncDec(UO); |
5092 | } |
5093 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
5094 | return VisitUnaryPostIncDec(UO); |
5095 | } |
5096 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
5097 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
5098 | return Error(UO); |
5099 | |
5100 | LValue LVal; |
5101 | if (!EvaluateLValue(UO->getSubExpr(), LVal, Info)) |
5102 | return false; |
5103 | APValue RVal; |
5104 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
5105 | UO->isIncrementOp(), &RVal)) |
5106 | return false; |
5107 | return DerivedSuccess(RVal, UO); |
5108 | } |
5109 | |
5110 | bool VisitStmtExpr(const StmtExpr *E) { |
5111 | |
5112 | |
5113 | if (Info.checkingForOverflow()) |
5114 | return Error(E); |
5115 | |
5116 | BlockScopeRAII Scope(Info); |
5117 | const CompoundStmt *CS = E->getSubStmt(); |
5118 | if (CS->body_empty()) |
5119 | return true; |
5120 | |
5121 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
5122 | BE = CS->body_end(); |
5123 | ; ++BI) { |
5124 | if (BI + 1 == BE) { |
5125 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
5126 | if (!FinalExpr) { |
5127 | Info.FFDiag((*BI)->getBeginLoc(), |
5128 | diag::note_constexpr_stmt_expr_unsupported); |
5129 | return false; |
5130 | } |
5131 | return this->Visit(FinalExpr); |
5132 | } |
5133 | |
5134 | APValue ReturnValue; |
5135 | StmtResult Result = { ReturnValue, nullptr }; |
5136 | EvalStmtResult ESR = EvaluateStmt(Result, Info, *BI); |
5137 | if (ESR != ESR_Succeeded) { |
5138 | |
5139 | |
5140 | |
5141 | if (ESR != ESR_Failed) |
5142 | Info.FFDiag((*BI)->getBeginLoc(), |
5143 | diag::note_constexpr_stmt_expr_unsupported); |
5144 | return false; |
5145 | } |
5146 | } |
5147 | |
5148 | llvm_unreachable("Return from function from the loop above."); |
5149 | } |
5150 | |
5151 | |
5152 | void VisitIgnoredValue(const Expr *E) { |
5153 | EvaluateIgnoredValue(Info, E); |
5154 | } |
5155 | |
5156 | |
5157 | void VisitIgnoredBaseExpression(const Expr *E) { |
5158 | |
5159 | |
5160 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Info.Ctx)) |
5161 | return; |
5162 | VisitIgnoredValue(E); |
5163 | } |
5164 | }; |
5165 | |
5166 | } |
5167 | |
5168 | |
5169 | |
5170 | |
5171 | namespace { |
5172 | template<class Derived> |
5173 | class LValueExprEvaluatorBase |
5174 | : public ExprEvaluatorBase<Derived> { |
5175 | protected: |
5176 | LValue &Result; |
5177 | bool InvalidBaseOK; |
5178 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
5179 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
5180 | |
5181 | bool Success(APValue::LValueBase B) { |
5182 | Result.set(B); |
5183 | return true; |
5184 | } |
5185 | |
5186 | bool evaluatePointer(const Expr *E, LValue &Result) { |
5187 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
5188 | } |
5189 | |
5190 | public: |
5191 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
5192 | : ExprEvaluatorBaseTy(Info), Result(Result), |
5193 | InvalidBaseOK(InvalidBaseOK) {} |
5194 | |
5195 | bool Success(const APValue &V, const Expr *E) { |
5196 | Result.setFrom(this->Info.Ctx, V); |
5197 | return true; |
5198 | } |
5199 | |
5200 | bool VisitMemberExpr(const MemberExpr *E) { |
5201 | |
5202 | QualType BaseTy; |
5203 | bool EvalOK; |
5204 | if (E->isArrow()) { |
5205 | EvalOK = evaluatePointer(E->getBase(), Result); |
5206 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
5207 | } else if (E->getBase()->isRValue()) { |
5208 | getBase()->getType()->isRecordType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5208, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getBase()->getType()->isRecordType()); |
5209 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
5210 | BaseTy = E->getBase()->getType(); |
5211 | } else { |
5212 | EvalOK = this->Visit(E->getBase()); |
5213 | BaseTy = E->getBase()->getType(); |
5214 | } |
5215 | if (!EvalOK) { |
5216 | if (!InvalidBaseOK) |
5217 | return false; |
5218 | Result.setInvalid(E); |
5219 | return true; |
5220 | } |
5221 | |
5222 | const ValueDecl *MD = E->getMemberDecl(); |
5223 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl())) { |
5224 | (0) . __assert_fail ("BaseTy->getAs()->getDecl()->getCanonicalDecl() == FD->getParent()->getCanonicalDecl() && \"record / field mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5225, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseTy->getAs<RecordType>()->getDecl()->getCanonicalDecl() == |
5225 | (0) . __assert_fail ("BaseTy->getAs()->getDecl()->getCanonicalDecl() == FD->getParent()->getCanonicalDecl() && \"record / field mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5225, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> FD->getParent()->getCanonicalDecl() && "record / field mismatch"); |
5226 | (void)BaseTy; |
5227 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
5228 | return false; |
5229 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(MD)) { |
5230 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
5231 | return false; |
5232 | } else |
5233 | return this->Error(E); |
5234 | |
5235 | if (MD->getType()->isReferenceType()) { |
5236 | APValue RefValue; |
5237 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
5238 | RefValue)) |
5239 | return false; |
5240 | return Success(RefValue, E); |
5241 | } |
5242 | return true; |
5243 | } |
5244 | |
5245 | bool VisitBinaryOperator(const BinaryOperator *E) { |
5246 | switch (E->getOpcode()) { |
5247 | default: |
5248 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
5249 | |
5250 | case BO_PtrMemD: |
5251 | case BO_PtrMemI: |
5252 | return HandleMemberPointerAccess(this->Info, E, Result); |
5253 | } |
5254 | } |
5255 | |
5256 | bool VisitCastExpr(const CastExpr *E) { |
5257 | switch (E->getCastKind()) { |
5258 | default: |
5259 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
5260 | |
5261 | case CK_DerivedToBase: |
5262 | case CK_UncheckedDerivedToBase: |
5263 | if (!this->Visit(E->getSubExpr())) |
5264 | return false; |
5265 | |
5266 | |
5267 | |
5268 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
5269 | Result); |
5270 | } |
5271 | } |
5272 | }; |
5273 | } |
5274 | |
5275 | |
5276 | |
5277 | |
5278 | |
5279 | |
5280 | |
5281 | |
5282 | |
5283 | |
5284 | |
5285 | |
5286 | |
5287 | |
5288 | |
5289 | |
5290 | |
5291 | |
5292 | |
5293 | |
5294 | |
5295 | |
5296 | |
5297 | |
5298 | |
5299 | |
5300 | |
5301 | |
5302 | |
5303 | |
5304 | |
5305 | |
5306 | namespace { |
5307 | class LValueExprEvaluator |
5308 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
5309 | public: |
5310 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
5311 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
5312 | |
5313 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
5314 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
5315 | |
5316 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
5317 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
5318 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
5319 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
5320 | bool VisitMemberExpr(const MemberExpr *E); |
5321 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
5322 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
5323 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
5324 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
5325 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
5326 | bool VisitUnaryDeref(const UnaryOperator *E); |
5327 | bool VisitUnaryReal(const UnaryOperator *E); |
5328 | bool VisitUnaryImag(const UnaryOperator *E); |
5329 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
5330 | return VisitUnaryPreIncDec(UO); |
5331 | } |
5332 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
5333 | return VisitUnaryPreIncDec(UO); |
5334 | } |
5335 | bool VisitBinAssign(const BinaryOperator *BO); |
5336 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
5337 | |
5338 | bool VisitCastExpr(const CastExpr *E) { |
5339 | switch (E->getCastKind()) { |
5340 | default: |
5341 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
5342 | |
5343 | case CK_LValueBitCast: |
5344 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
5345 | if (!Visit(E->getSubExpr())) |
5346 | return false; |
5347 | Result.Designator.setInvalid(); |
5348 | return true; |
5349 | |
5350 | case CK_BaseToDerived: |
5351 | if (!Visit(E->getSubExpr())) |
5352 | return false; |
5353 | return HandleBaseToDerivedCast(Info, E, Result); |
5354 | } |
5355 | } |
5356 | }; |
5357 | } |
5358 | |
5359 | |
5360 | |
5361 | |
5362 | |
5363 | |
5364 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
5365 | bool InvalidBaseOK) { |
5366 | isGLValue() || E->getType()->isFunctionType() || E->getType()->isVoidType() || isa(E)", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5367, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isGLValue() || E->getType()->isFunctionType() || |
5367 | isGLValue() || E->getType()->isFunctionType() || E->getType()->isVoidType() || isa(E)", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5367, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E)); |
5368 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
5369 | } |
5370 | |
5371 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
5372 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(E->getDecl())) |
5373 | return Success(FD); |
5374 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
5375 | return VisitVarDecl(E, VD); |
5376 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(E->getDecl())) |
5377 | return Visit(BD->getBinding()); |
5378 | return Error(E); |
5379 | } |
5380 | |
5381 | |
5382 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
5383 | |
5384 | |
5385 | |
5386 | |
5387 | |
5388 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
5389 | isa<DeclRefExpr>(E) && |
5390 | cast<DeclRefExpr>(E)->refersToEnclosingVariableOrCapture()) { |
5391 | |
5392 | |
5393 | |
5394 | |
5395 | if (Info.checkingPotentialConstantExpression()) |
5396 | return false; |
5397 | |
5398 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
5399 | |
5400 | Result = *Info.CurrentCall->This; |
5401 | |
5402 | |
5403 | if (!HandleLValueMember(Info, E, Result, FD)) |
5404 | return false; |
5405 | |
5406 | |
5407 | if (FD->getType()->isReferenceType()) { |
5408 | APValue RVal; |
5409 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
5410 | RVal)) |
5411 | return false; |
5412 | Result.setFrom(Info.Ctx, RVal); |
5413 | } |
5414 | return true; |
5415 | } |
5416 | } |
5417 | CallStackFrame *Frame = nullptr; |
5418 | if (VD->hasLocalStorage() && Info.CurrentCall->Index > 1) { |
5419 | |
5420 | |
5421 | |
5422 | |
5423 | |
5424 | |
5425 | if (Info.CurrentCall->Callee && |
5426 | Info.CurrentCall->Callee->Equals(VD->getDeclContext())) { |
5427 | Frame = Info.CurrentCall; |
5428 | } |
5429 | } |
5430 | |
5431 | if (!VD->getType()->isReferenceType()) { |
5432 | if (Frame) { |
5433 | Result.set({VD, Frame->Index, |
5434 | Info.CurrentCall->getCurrentTemporaryVersion(VD)}); |
5435 | return true; |
5436 | } |
5437 | return Success(VD); |
5438 | } |
5439 | |
5440 | APValue *V; |
5441 | if (!evaluateVarDeclInit(Info, E, VD, Frame, V, nullptr)) |
5442 | return false; |
5443 | if (V->isUninit()) { |
5444 | if (!Info.checkingPotentialConstantExpression()) |
5445 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
5446 | return false; |
5447 | } |
5448 | return Success(*V, E); |
5449 | } |
5450 | |
5451 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
5452 | const MaterializeTemporaryExpr *E) { |
5453 | |
5454 | SmallVector<const Expr *, 2> CommaLHSs; |
5455 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
5456 | const Expr *Inner = E->GetTemporaryExpr()-> |
5457 | skipRValueSubobjectAdjustments(CommaLHSs, Adjustments); |
5458 | |
5459 | |
5460 | for (unsigned I = 0, N = CommaLHSs.size(); I != N; ++I) |
5461 | if (!EvaluateIgnoredValue(Info, CommaLHSs[I])) |
5462 | return false; |
5463 | |
5464 | |
5465 | |
5466 | |
5467 | APValue *Value; |
5468 | if (E->getStorageDuration() == SD_Static) { |
5469 | Value = Info.Ctx.getMaterializedTemporaryValue(E, true); |
5470 | *Value = APValue(); |
5471 | Result.set(E); |
5472 | } else { |
5473 | Value = &createTemporary(E, E->getStorageDuration() == SD_Automatic, Result, |
5474 | *Info.CurrentCall); |
5475 | } |
5476 | |
5477 | QualType Type = Inner->getType(); |
5478 | |
5479 | |
5480 | if (!EvaluateInPlace(*Value, Info, Result, Inner) || |
5481 | (E->getStorageDuration() == SD_Static && |
5482 | !CheckConstantExpression(Info, E->getExprLoc(), Type, *Value))) { |
5483 | *Value = APValue(); |
5484 | return false; |
5485 | } |
5486 | |
5487 | |
5488 | for (unsigned I = Adjustments.size(); I != 0; ) { |
5489 | --I; |
5490 | switch (Adjustments[I].Kind) { |
5491 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
5492 | if (!HandleLValueBasePath(Info, Adjustments[I].DerivedToBase.BasePath, |
5493 | Type, Result)) |
5494 | return false; |
5495 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
5496 | break; |
5497 | |
5498 | case SubobjectAdjustment::FieldAdjustment: |
5499 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
5500 | return false; |
5501 | Type = Adjustments[I].Field->getType(); |
5502 | break; |
5503 | |
5504 | case SubobjectAdjustment::MemberPointerAdjustment: |
5505 | if (!HandleMemberPointerAccess(this->Info, Type, Result, |
5506 | Adjustments[I].Ptr.RHS)) |
5507 | return false; |
5508 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
5509 | break; |
5510 | } |
5511 | } |
5512 | |
5513 | return true; |
5514 | } |
5515 | |
5516 | bool |
5517 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
5518 | (0) . __assert_fail ("(!Info.getLangOpts().CPlusPlus || E->isFileScope()) && \"lvalue compound literal in c++?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5519, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
5519 | (0) . __assert_fail ("(!Info.getLangOpts().CPlusPlus || E->isFileScope()) && \"lvalue compound literal in c++?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5519, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "lvalue compound literal in c++?"); |
5520 | |
5521 | |
5522 | return Success(E); |
5523 | } |
5524 | |
5525 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
5526 | if (!E->isPotentiallyEvaluated()) |
5527 | return Success(E); |
5528 | |
5529 | Info.FFDiag(E, diag::note_constexpr_typeid_polymorphic) |
5530 | << E->getExprOperand()->getType() |
5531 | << E->getExprOperand()->getSourceRange(); |
5532 | return false; |
5533 | } |
5534 | |
5535 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
5536 | return Success(E); |
5537 | } |
5538 | |
5539 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
5540 | |
5541 | if (const VarDecl *VD = dyn_cast<VarDecl>(E->getMemberDecl())) { |
5542 | VisitIgnoredBaseExpression(E->getBase()); |
5543 | return VisitVarDecl(E, VD); |
5544 | } |
5545 | |
5546 | |
5547 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getMemberDecl())) { |
5548 | if (MD->isStatic()) { |
5549 | VisitIgnoredBaseExpression(E->getBase()); |
5550 | return Success(MD); |
5551 | } |
5552 | } |
5553 | |
5554 | |
5555 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
5556 | } |
5557 | |
5558 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
5559 | |
5560 | if (E->getBase()->getType()->isVectorType()) |
5561 | return Error(E); |
5562 | |
5563 | bool Success = true; |
5564 | if (!evaluatePointer(E->getBase(), Result)) { |
5565 | if (!Info.noteFailure()) |
5566 | return false; |
5567 | Success = false; |
5568 | } |
5569 | |
5570 | APSInt Index; |
5571 | if (!EvaluateInteger(E->getIdx(), Index, Info)) |
5572 | return false; |
5573 | |
5574 | return Success && |
5575 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
5576 | } |
5577 | |
5578 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
5579 | return evaluatePointer(E->getSubExpr(), Result); |
5580 | } |
5581 | |
5582 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
5583 | if (!Visit(E->getSubExpr())) |
5584 | return false; |
5585 | |
5586 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
5587 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
5588 | return true; |
5589 | } |
5590 | |
5591 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
5592 | (0) . __assert_fail ("E->getSubExpr()->getType()->isAnyComplexType() && \"lvalue __imag__ on scalar?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5593, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getSubExpr()->getType()->isAnyComplexType() && |
5593 | (0) . __assert_fail ("E->getSubExpr()->getType()->isAnyComplexType() && \"lvalue __imag__ on scalar?\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5593, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "lvalue __imag__ on scalar?"); |
5594 | if (!Visit(E->getSubExpr())) |
5595 | return false; |
5596 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
5597 | return true; |
5598 | } |
5599 | |
5600 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
5601 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
5602 | return Error(UO); |
5603 | |
5604 | if (!this->Visit(UO->getSubExpr())) |
5605 | return false; |
5606 | |
5607 | return handleIncDec( |
5608 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
5609 | UO->isIncrementOp(), nullptr); |
5610 | } |
5611 | |
5612 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
5613 | const CompoundAssignOperator *CAO) { |
5614 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
5615 | return Error(CAO); |
5616 | |
5617 | APValue RHS; |
5618 | |
5619 | |
5620 | if (!this->Visit(CAO->getLHS())) { |
5621 | if (Info.noteFailure()) |
5622 | Evaluate(RHS, this->Info, CAO->getRHS()); |
5623 | return false; |
5624 | } |
5625 | |
5626 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) |
5627 | return false; |
5628 | |
5629 | return handleCompoundAssignment( |
5630 | this->Info, CAO, |
5631 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
5632 | CAO->getOpForCompoundAssignment(CAO->getOpcode()), RHS); |
5633 | } |
5634 | |
5635 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
5636 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
5637 | return Error(E); |
5638 | |
5639 | APValue NewVal; |
5640 | |
5641 | if (!this->Visit(E->getLHS())) { |
5642 | if (Info.noteFailure()) |
5643 | Evaluate(NewVal, this->Info, E->getRHS()); |
5644 | return false; |
5645 | } |
5646 | |
5647 | if (!Evaluate(NewVal, this->Info, E->getRHS())) |
5648 | return false; |
5649 | |
5650 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
5651 | NewVal); |
5652 | } |
5653 | |
5654 | |
5655 | |
5656 | |
5657 | |
5658 | |
5659 | |
5660 | |
5661 | |
5662 | |
5663 | |
5664 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
5665 | const CallExpr *Call, |
5666 | llvm::APInt &Result) { |
5667 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
5668 | |
5669 | getElemSizeParam().isValid()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5669, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
5670 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
5671 | unsigned BitsInSizeT = Ctx.getTypeSize(Ctx.getSizeType()); |
5672 | if (Call->getNumArgs() <= SizeArgNo) |
5673 | return false; |
5674 | |
5675 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
5676 | Expr::EvalResult ExprResult; |
5677 | if (!E->EvaluateAsInt(ExprResult, Ctx, Expr::SE_AllowSideEffects)) |
5678 | return false; |
5679 | Into = ExprResult.Val.getInt(); |
5680 | if (Into.isNegative() || !Into.isIntN(BitsInSizeT)) |
5681 | return false; |
5682 | Into = Into.zextOrSelf(BitsInSizeT); |
5683 | return true; |
5684 | }; |
5685 | |
5686 | APSInt SizeOfElem; |
5687 | if (!EvaluateAsSizeT(Call->getArg(SizeArgNo), SizeOfElem)) |
5688 | return false; |
5689 | |
5690 | if (!AllocSize->getNumElemsParam().isValid()) { |
5691 | Result = std::move(SizeOfElem); |
5692 | return true; |
5693 | } |
5694 | |
5695 | APSInt NumberOfElems; |
5696 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
5697 | if (!EvaluateAsSizeT(Call->getArg(NumArgNo), NumberOfElems)) |
5698 | return false; |
5699 | |
5700 | bool Overflow; |
5701 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(NumberOfElems, Overflow); |
5702 | if (Overflow) |
5703 | return false; |
5704 | |
5705 | Result = std::move(BytesAvailable); |
5706 | return true; |
5707 | } |
5708 | |
5709 | |
5710 | |
5711 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
5712 | const LValue &LVal, |
5713 | llvm::APInt &Result) { |
5714 | (0) . __assert_fail ("isBaseAnAllocSizeCall(LVal.getLValueBase()) && \"Can't get the size of a non alloc_size function\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5715, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
5715 | (0) . __assert_fail ("isBaseAnAllocSizeCall(LVal.getLValueBase()) && \"Can't get the size of a non alloc_size function\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5715, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Can't get the size of a non alloc_size function"); |
5716 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
5717 | const CallExpr *CE = tryUnwrapAllocSizeCall(Base); |
5718 | return getBytesReturnedByAllocSizeCall(Ctx, CE, Result); |
5719 | } |
5720 | |
5721 | |
5722 | |
5723 | |
5724 | |
5725 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
5726 | LValue &Result) { |
5727 | if (Base.isNull()) |
5728 | return false; |
5729 | |
5730 | |
5731 | |
5732 | |
5733 | |
5734 | |
5735 | const auto *VD = |
5736 | dyn_cast_or_null<VarDecl>(Base.dyn_cast<const ValueDecl *>()); |
5737 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
5738 | return false; |
5739 | |
5740 | const Expr *Init = VD->getAnyInitializer(); |
5741 | if (!Init) |
5742 | return false; |
5743 | |
5744 | const Expr *E = Init->IgnoreParens(); |
5745 | if (!tryUnwrapAllocSizeCall(E)) |
5746 | return false; |
5747 | |
5748 | |
5749 | |
5750 | Result.setInvalid(E); |
5751 | |
5752 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
5753 | Result.addUnsizedArray(Info, E, Pointee); |
5754 | return true; |
5755 | } |
5756 | |
5757 | namespace { |
5758 | class PointerExprEvaluator |
5759 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
5760 | LValue &Result; |
5761 | bool InvalidBaseOK; |
5762 | |
5763 | bool Success(const Expr *E) { |
5764 | Result.set(E); |
5765 | return true; |
5766 | } |
5767 | |
5768 | bool evaluateLValue(const Expr *E, LValue &Result) { |
5769 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
5770 | } |
5771 | |
5772 | bool evaluatePointer(const Expr *E, LValue &Result) { |
5773 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
5774 | } |
5775 | |
5776 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
5777 | public: |
5778 | |
5779 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
5780 | : ExprEvaluatorBaseTy(info), Result(Result), |
5781 | InvalidBaseOK(InvalidBaseOK) {} |
5782 | |
5783 | bool Success(const APValue &V, const Expr *E) { |
5784 | Result.setFrom(Info.Ctx, V); |
5785 | return true; |
5786 | } |
5787 | bool ZeroInitialization(const Expr *E) { |
5788 | auto TargetVal = Info.Ctx.getTargetNullPointerValue(E->getType()); |
5789 | Result.setNull(E->getType(), TargetVal); |
5790 | return true; |
5791 | } |
5792 | |
5793 | bool VisitBinaryOperator(const BinaryOperator *E); |
5794 | bool VisitCastExpr(const CastExpr* E); |
5795 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
5796 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
5797 | { return Success(E); } |
5798 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
5799 | if (E->isExpressibleAsConstantInitializer()) |
5800 | return Success(E); |
5801 | if (Info.noteFailure()) |
5802 | EvaluateIgnoredValue(Info, E->getSubExpr()); |
5803 | return Error(E); |
5804 | } |
5805 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
5806 | { return Success(E); } |
5807 | bool VisitCallExpr(const CallExpr *E); |
5808 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
5809 | bool VisitBlockExpr(const BlockExpr *E) { |
5810 | if (!E->getBlockDecl()->hasCaptures()) |
5811 | return Success(E); |
5812 | return Error(E); |
5813 | } |
5814 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
5815 | |
5816 | if (Info.checkingPotentialConstantExpression()) |
5817 | return false; |
5818 | if (!Info.CurrentCall->This) { |
5819 | if (Info.getLangOpts().CPlusPlus11) |
5820 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
5821 | else |
5822 | Info.FFDiag(E); |
5823 | return false; |
5824 | } |
5825 | Result = *Info.CurrentCall->This; |
5826 | |
5827 | |
5828 | |
5829 | |
5830 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
5831 | |
5832 | |
5833 | if (!HandleLValueMember(Info, E, Result, |
5834 | Info.CurrentCall->LambdaThisCaptureField)) |
5835 | return false; |
5836 | |
5837 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
5838 | ->isPointerType()) { |
5839 | APValue RVal; |
5840 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
5841 | RVal)) |
5842 | return false; |
5843 | |
5844 | Result.setFrom(Info.Ctx, RVal); |
5845 | } |
5846 | } |
5847 | return true; |
5848 | } |
5849 | |
5850 | |
5851 | }; |
5852 | } |
5853 | |
5854 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
5855 | bool InvalidBaseOK) { |
5856 | isRValue() && E->getType()->hasPointerRepresentation()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 5856, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
5857 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
5858 | } |
5859 | |
5860 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
5861 | if (E->getOpcode() != BO_Add && |
5862 | E->getOpcode() != BO_Sub) |
5863 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
5864 | |
5865 | const Expr *PExp = E->getLHS(); |
5866 | const Expr *IExp = E->getRHS(); |
5867 | if (IExp->getType()->isPointerType()) |
5868 | std::swap(PExp, IExp); |
5869 | |
5870 | bool EvalPtrOK = evaluatePointer(PExp, Result); |
5871 | if (!EvalPtrOK && !Info.noteFailure()) |
5872 | return false; |
5873 | |
5874 | llvm::APSInt Offset; |
5875 | if (!EvaluateInteger(IExp, Offset, Info) || !EvalPtrOK) |
5876 | return false; |
5877 | |
5878 | if (E->getOpcode() == BO_Sub) |
5879 | negateAsSigned(Offset); |
5880 | |
5881 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
5882 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
5883 | } |
5884 | |
5885 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
5886 | return evaluateLValue(E->getSubExpr(), Result); |
5887 | } |
5888 | |
5889 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
5890 | const Expr *SubExpr = E->getSubExpr(); |
5891 | |
5892 | switch (E->getCastKind()) { |
5893 | default: |
5894 | break; |
5895 | |
5896 | case CK_BitCast: |
5897 | case CK_CPointerToObjCPointerCast: |
5898 | case CK_BlockPointerToObjCPointerCast: |
5899 | case CK_AnyPointerToBlockPointerCast: |
5900 | case CK_AddressSpaceConversion: |
5901 | if (!Visit(SubExpr)) |
5902 | return false; |
5903 | |
5904 | |
5905 | |
5906 | if (!E->getType()->isVoidPointerType()) { |
5907 | Result.Designator.setInvalid(); |
5908 | if (SubExpr->getType()->isVoidPointerType()) |
5909 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
5910 | << 3 << SubExpr->getType(); |
5911 | else |
5912 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
5913 | } |
5914 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
5915 | ZeroInitialization(E); |
5916 | return true; |
5917 | |
5918 | case CK_DerivedToBase: |
5919 | case CK_UncheckedDerivedToBase: |
5920 | if (!evaluatePointer(E->getSubExpr(), Result)) |
5921 | return false; |
5922 | if (!Result.Base && Result.Offset.isZero()) |
5923 | return true; |
5924 | |
5925 | |
5926 | |
5927 | return HandleLValueBasePath(Info, E, E->getSubExpr()->getType()-> |
5928 | castAs<PointerType>()->getPointeeType(), |
5929 | Result); |
5930 | |
5931 | case CK_BaseToDerived: |
5932 | if (!Visit(E->getSubExpr())) |
5933 | return false; |
5934 | if (!Result.Base && Result.Offset.isZero()) |
5935 | return true; |
5936 | return HandleBaseToDerivedCast(Info, E, Result); |
5937 | |
5938 | case CK_NullToPointer: |
5939 | VisitIgnoredValue(E->getSubExpr()); |
5940 | return ZeroInitialization(E); |
5941 | |
5942 | case CK_IntegralToPointer: { |
5943 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
5944 | |
5945 | APValue Value; |
5946 | if (!EvaluateIntegerOrLValue(SubExpr, Value, Info)) |
5947 | break; |
5948 | |
5949 | if (Value.isInt()) { |
5950 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
5951 | uint64_t N = Value.getInt().extOrTrunc(Size).getZExtValue(); |
5952 | Result.Base = (Expr*)nullptr; |
5953 | Result.InvalidBase = false; |
5954 | Result.Offset = CharUnits::fromQuantity(N); |
5955 | Result.Designator.setInvalid(); |
5956 | Result.IsNullPtr = false; |
5957 | return true; |
5958 | } else { |
5959 | |
5960 | Result.setFrom(Info.Ctx, Value); |
5961 | return true; |
5962 | } |
5963 | } |
5964 | |
5965 | case CK_ArrayToPointerDecay: { |
5966 | if (SubExpr->isGLValue()) { |
5967 | if (!evaluateLValue(SubExpr, Result)) |
5968 | return false; |
5969 | } else { |
5970 | APValue &Value = createTemporary(SubExpr, false, Result, |
5971 | *Info.CurrentCall); |
5972 | if (!EvaluateInPlace(Value, Info, Result, SubExpr)) |
5973 | return false; |
5974 | } |
5975 | |
5976 | auto *AT = Info.Ctx.getAsArrayType(SubExpr->getType()); |
5977 | if (auto *CAT = dyn_cast<ConstantArrayType>(AT)) |
5978 | Result.addArray(Info, E, CAT); |
5979 | else |
5980 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
5981 | return true; |
5982 | } |
5983 | |
5984 | case CK_FunctionToPointerDecay: |
5985 | return evaluateLValue(SubExpr, Result); |
5986 | |
5987 | case CK_LValueToRValue: { |
5988 | LValue LVal; |
5989 | if (!evaluateLValue(E->getSubExpr(), LVal)) |
5990 | return false; |
5991 | |
5992 | APValue RVal; |
5993 | |
5994 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
5995 | LVal, RVal)) |
5996 | return InvalidBaseOK && |
5997 | evaluateLValueAsAllocSize(Info, LVal.Base, Result); |
5998 | return Success(RVal, E); |
5999 | } |
6000 | } |
6001 | |
6002 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
6003 | } |
6004 | |
6005 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
6006 | UnaryExprOrTypeTrait ExprKind) { |
6007 | |
6008 | |
6009 | |
6010 | if (const ReferenceType *Ref = T->getAs<ReferenceType>()) |
6011 | T = Ref->getPointeeType(); |
6012 | |
6013 | if (T.getQualifiers().hasUnaligned()) |
6014 | return CharUnits::One(); |
6015 | |
6016 | const bool AlignOfReturnsPreferred = |
6017 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
6018 | |
6019 | |
6020 | |
6021 | |
6022 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
6023 | return Info.Ctx.toCharUnitsFromBits( |
6024 | Info.Ctx.getPreferredTypeAlign(T.getTypePtr())); |
6025 | |
6026 | else if (ExprKind == UETT_AlignOf) |
6027 | return Info.Ctx.getTypeAlignInChars(T.getTypePtr()); |
6028 | else |
6029 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind"); |
6030 | } |
6031 | |
6032 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
6033 | UnaryExprOrTypeTrait ExprKind) { |
6034 | E = E->IgnoreParens(); |
6035 | |
6036 | |
6037 | |
6038 | |
6039 | |
6040 | |
6041 | |
6042 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
6043 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
6044 | ); |
6045 | |
6046 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
6047 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
6048 | ); |
6049 | |
6050 | return GetAlignOfType(Info, E->getType(), ExprKind); |
6051 | } |
6052 | |
6053 | |
6054 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
6055 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
6056 | return true; |
6057 | |
6058 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
6059 | return false; |
6060 | |
6061 | Result.setInvalid(E); |
6062 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
6063 | Result.addUnsizedArray(Info, E, PointeeTy); |
6064 | return true; |
6065 | } |
6066 | |
6067 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
6068 | if (IsStringLiteralCall(E)) |
6069 | return Success(E); |
6070 | |
6071 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
6072 | return VisitBuiltinCallExpr(E, BuiltinOp); |
6073 | |
6074 | return visitNonBuiltinCallExpr(E); |
6075 | } |
6076 | |
6077 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
6078 | unsigned BuiltinOp) { |
6079 | switch (BuiltinOp) { |
6080 | case Builtin::BI__builtin_addressof: |
6081 | return evaluateLValue(E->getArg(0), Result); |
6082 | case Builtin::BI__builtin_assume_aligned: { |
6083 | |
6084 | |
6085 | |
6086 | if (!evaluatePointer(E->getArg(0), Result)) |
6087 | return false; |
6088 | |
6089 | LValue OffsetResult(Result); |
6090 | APSInt Alignment; |
6091 | if (!EvaluateInteger(E->getArg(1), Alignment, Info)) |
6092 | return false; |
6093 | CharUnits Align = CharUnits::fromQuantity(Alignment.getZExtValue()); |
6094 | |
6095 | if (E->getNumArgs() > 2) { |
6096 | APSInt Offset; |
6097 | if (!EvaluateInteger(E->getArg(2), Offset, Info)) |
6098 | return false; |
6099 | |
6100 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
6101 | OffsetResult.Offset += CharUnits::fromQuantity(AdditionalOffset); |
6102 | } |
6103 | |
6104 | |
6105 | if (OffsetResult.Base) { |
6106 | CharUnits BaseAlignment; |
6107 | if (const ValueDecl *VD = |
6108 | OffsetResult.Base.dyn_cast<const ValueDecl*>()) { |
6109 | BaseAlignment = Info.Ctx.getDeclAlign(VD); |
6110 | } else { |
6111 | BaseAlignment = GetAlignOfExpr( |
6112 | Info, OffsetResult.Base.get<const Expr *>(), UETT_AlignOf); |
6113 | } |
6114 | |
6115 | if (BaseAlignment < Align) { |
6116 | Result.Designator.setInvalid(); |
6117 | |
6118 | CCEDiag(E->getArg(0), |
6119 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
6120 | << (unsigned)BaseAlignment.getQuantity() |
6121 | << (unsigned)Align.getQuantity(); |
6122 | return false; |
6123 | } |
6124 | } |
6125 | |
6126 | |
6127 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
6128 | Result.Designator.setInvalid(); |
6129 | |
6130 | (OffsetResult.Base |
6131 | ? CCEDiag(E->getArg(0), |
6132 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
6133 | : CCEDiag(E->getArg(0), |
6134 | diag::note_constexpr_baa_value_insufficient_alignment)) |
6135 | << (int)OffsetResult.Offset.getQuantity() |
6136 | << (unsigned)Align.getQuantity(); |
6137 | return false; |
6138 | } |
6139 | |
6140 | return true; |
6141 | } |
6142 | case Builtin::BI__builtin_launder: |
6143 | return evaluatePointer(E->getArg(0), Result); |
6144 | case Builtin::BIstrchr: |
6145 | case Builtin::BIwcschr: |
6146 | case Builtin::BImemchr: |
6147 | case Builtin::BIwmemchr: |
6148 | if (Info.getLangOpts().CPlusPlus11) |
6149 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
6150 | << << |
6151 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
6152 | else |
6153 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
6154 | LLVM_FALLTHROUGH; |
6155 | case Builtin::BI__builtin_strchr: |
6156 | case Builtin::BI__builtin_wcschr: |
6157 | case Builtin::BI__builtin_memchr: |
6158 | case Builtin::BI__builtin_char_memchr: |
6159 | case Builtin::BI__builtin_wmemchr: { |
6160 | if (!Visit(E->getArg(0))) |
6161 | return false; |
6162 | APSInt Desired; |
6163 | if (!EvaluateInteger(E->getArg(1), Desired, Info)) |
6164 | return false; |
6165 | uint64_t MaxLength = uint64_t(-1); |
6166 | if (BuiltinOp != Builtin::BIstrchr && |
6167 | BuiltinOp != Builtin::BIwcschr && |
6168 | BuiltinOp != Builtin::BI__builtin_strchr && |
6169 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
6170 | APSInt N; |
6171 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
6172 | return false; |
6173 | MaxLength = N.getExtValue(); |
6174 | } |
6175 | |
6176 | if (MaxLength == 0u) |
6177 | return ZeroInitialization(E); |
6178 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
6179 | Result.Designator.Invalid) |
6180 | return false; |
6181 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
6182 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
6183 | BuiltinOp == Builtin::BI__builtin_memchr; |
6184 | getArg(0)->getType()->getPointeeType())", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsRawByte || |
6185 | getArg(0)->getType()->getPointeeType())", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Info.Ctx.hasSameUnqualifiedType( |
6186 | getArg(0)->getType()->getPointeeType())", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CharTy, E->getArg(0)->getType()->getPointeeType())); |
6187 | |
6188 | if (IsRawByte && CharTy->isIncompleteType()) { |
6189 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
6190 | return false; |
6191 | } |
6192 | |
6193 | |
6194 | if (IsRawByte && Info.Ctx.getTypeSizeInChars(CharTy) != CharUnits::One()) |
6195 | return false; |
6196 | |
6197 | |
6198 | uint64_t DesiredVal; |
6199 | bool StopAtNull = false; |
6200 | switch (BuiltinOp) { |
6201 | case Builtin::BIstrchr: |
6202 | case Builtin::BI__builtin_strchr: |
6203 | |
6204 | |
6205 | if (!APSInt::isSameValue(HandleIntToIntCast(Info, E, CharTy, |
6206 | E->getArg(1)->getType(), |
6207 | Desired), |
6208 | Desired)) |
6209 | return ZeroInitialization(E); |
6210 | StopAtNull = true; |
6211 | LLVM_FALLTHROUGH; |
6212 | case Builtin::BImemchr: |
6213 | case Builtin::BI__builtin_memchr: |
6214 | case Builtin::BI__builtin_char_memchr: |
6215 | |
6216 | |
6217 | |
6218 | DesiredVal = Desired.trunc(Info.Ctx.getCharWidth()).getZExtValue(); |
6219 | break; |
6220 | |
6221 | case Builtin::BIwcschr: |
6222 | case Builtin::BI__builtin_wcschr: |
6223 | StopAtNull = true; |
6224 | LLVM_FALLTHROUGH; |
6225 | case Builtin::BIwmemchr: |
6226 | case Builtin::BI__builtin_wmemchr: |
6227 | |
6228 | DesiredVal = Desired.getZExtValue(); |
6229 | break; |
6230 | } |
6231 | |
6232 | for (; MaxLength; --MaxLength) { |
6233 | APValue Char; |
6234 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
6235 | !Char.isInt()) |
6236 | return false; |
6237 | if (Char.getInt().getZExtValue() == DesiredVal) |
6238 | return true; |
6239 | if (StopAtNull && !Char.getInt()) |
6240 | break; |
6241 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
6242 | return false; |
6243 | } |
6244 | |
6245 | return ZeroInitialization(E); |
6246 | } |
6247 | |
6248 | case Builtin::BImemcpy: |
6249 | case Builtin::BImemmove: |
6250 | case Builtin::BIwmemcpy: |
6251 | case Builtin::BIwmemmove: |
6252 | if (Info.getLangOpts().CPlusPlus11) |
6253 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
6254 | << << |
6255 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
6256 | else |
6257 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
6258 | LLVM_FALLTHROUGH; |
6259 | case Builtin::BI__builtin_memcpy: |
6260 | case Builtin::BI__builtin_memmove: |
6261 | case Builtin::BI__builtin_wmemcpy: |
6262 | case Builtin::BI__builtin_wmemmove: { |
6263 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
6264 | BuiltinOp == Builtin::BIwmemmove || |
6265 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
6266 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
6267 | bool Move = BuiltinOp == Builtin::BImemmove || |
6268 | BuiltinOp == Builtin::BIwmemmove || |
6269 | BuiltinOp == Builtin::BI__builtin_memmove || |
6270 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
6271 | |
6272 | |
6273 | if (!Visit(E->getArg(0))) |
6274 | return false; |
6275 | LValue Dest = Result; |
6276 | |
6277 | LValue Src; |
6278 | if (!EvaluatePointer(E->getArg(1), Src, Info)) |
6279 | return false; |
6280 | |
6281 | APSInt N; |
6282 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
6283 | return false; |
6284 | (0) . __assert_fail ("!N.isSigned() && \"memcpy and friends take an unsigned size\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6284, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!N.isSigned() && "memcpy and friends take an unsigned size"); |
6285 | |
6286 | |
6287 | |
6288 | if (!N) |
6289 | return true; |
6290 | |
6291 | |
6292 | |
6293 | |
6294 | if (!Src.Base || !Dest.Base) { |
6295 | APValue Val; |
6296 | (!Src.Base ? Src : Dest).moveInto(Val); |
6297 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
6298 | << Move << WChar << !!Src.Base |
6299 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
6300 | return false; |
6301 | } |
6302 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
6303 | return false; |
6304 | |
6305 | |
6306 | |
6307 | |
6308 | QualType T = Dest.Designator.getType(Info.Ctx); |
6309 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
6310 | if (!Info.Ctx.hasSameUnqualifiedType(T, SrcT)) { |
6311 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
6312 | return false; |
6313 | } |
6314 | if (T->isIncompleteType()) { |
6315 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
6316 | return false; |
6317 | } |
6318 | if (!T.isTriviallyCopyableType(Info.Ctx)) { |
6319 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
6320 | return false; |
6321 | } |
6322 | |
6323 | |
6324 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
6325 | if (!WChar) { |
6326 | uint64_t Remainder; |
6327 | llvm::APInt OrigN = N; |
6328 | llvm::APInt::udivrem(OrigN, TSize, N, Remainder); |
6329 | if (Remainder) { |
6330 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
6331 | << Move << WChar << 0 << T << OrigN.toString(10, ) |
6332 | << (unsigned)TSize; |
6333 | return false; |
6334 | } |
6335 | } |
6336 | |
6337 | |
6338 | |
6339 | |
6340 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
6341 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
6342 | if (N.ugt(RemainingSrcSize) || N.ugt(RemainingDestSize)) { |
6343 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
6344 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
6345 | << N.toString(10, ); |
6346 | return false; |
6347 | } |
6348 | uint64_t NElems = N.getZExtValue(); |
6349 | uint64_t NBytes = NElems * TSize; |
6350 | |
6351 | |
6352 | int Direction = 1; |
6353 | if (HasSameBase(Src, Dest)) { |
6354 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
6355 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
6356 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
6357 | |
6358 | if (!Move) { |
6359 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
6360 | return false; |
6361 | } |
6362 | |
6363 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
6364 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
6365 | return false; |
6366 | Direction = -1; |
6367 | } else if (!Move && SrcOffset >= DestOffset && |
6368 | SrcOffset - DestOffset < NBytes) { |
6369 | |
6370 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
6371 | return false; |
6372 | } |
6373 | } |
6374 | |
6375 | while (true) { |
6376 | APValue Val; |
6377 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
6378 | !handleAssignment(Info, E, Dest, T, Val)) |
6379 | return false; |
6380 | |
6381 | |
6382 | if (--NElems == 0) |
6383 | return true; |
6384 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
6385 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
6386 | return false; |
6387 | } |
6388 | } |
6389 | |
6390 | default: |
6391 | return visitNonBuiltinCallExpr(E); |
6392 | } |
6393 | } |
6394 | |
6395 | |
6396 | |
6397 | |
6398 | |
6399 | namespace { |
6400 | class MemberPointerExprEvaluator |
6401 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
6402 | MemberPtr &Result; |
6403 | |
6404 | bool Success(const ValueDecl *D) { |
6405 | Result = MemberPtr(D); |
6406 | return true; |
6407 | } |
6408 | public: |
6409 | |
6410 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
6411 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
6412 | |
6413 | bool Success(const APValue &V, const Expr *E) { |
6414 | Result.setFrom(V); |
6415 | return true; |
6416 | } |
6417 | bool ZeroInitialization(const Expr *E) { |
6418 | return Success((const ValueDecl*)nullptr); |
6419 | } |
6420 | |
6421 | bool VisitCastExpr(const CastExpr *E); |
6422 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
6423 | }; |
6424 | } |
6425 | |
6426 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
6427 | EvalInfo &Info) { |
6428 | isRValue() && E->getType()->isMemberPointerType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6428, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isMemberPointerType()); |
6429 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
6430 | } |
6431 | |
6432 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
6433 | switch (E->getCastKind()) { |
6434 | default: |
6435 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
6436 | |
6437 | case CK_NullToMemberPointer: |
6438 | VisitIgnoredValue(E->getSubExpr()); |
6439 | return ZeroInitialization(E); |
6440 | |
6441 | case CK_BaseToDerivedMemberPointer: { |
6442 | if (!Visit(E->getSubExpr())) |
6443 | return false; |
6444 | if (E->path_empty()) |
6445 | return true; |
6446 | |
6447 | |
6448 | |
6449 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
6450 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
6451 | PathI != PathE; ++PathI) { |
6452 | (0) . __assert_fail ("!(*PathI)->isVirtual() && \"memptr cast through vbase\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6452, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
6453 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
6454 | if (!Result.castToDerived(Derived)) |
6455 | return Error(E); |
6456 | } |
6457 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
6458 | if (!Result.castToDerived(FinalTy->getAsCXXRecordDecl())) |
6459 | return Error(E); |
6460 | return true; |
6461 | } |
6462 | |
6463 | case CK_DerivedToBaseMemberPointer: |
6464 | if (!Visit(E->getSubExpr())) |
6465 | return false; |
6466 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
6467 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
6468 | (0) . __assert_fail ("!(*PathI)->isVirtual() && \"memptr cast through vbase\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6468, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!(*PathI)->isVirtual() && "memptr cast through vbase"); |
6469 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
6470 | if (!Result.castToBase(Base)) |
6471 | return Error(E); |
6472 | } |
6473 | return true; |
6474 | } |
6475 | } |
6476 | |
6477 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
6478 | |
6479 | |
6480 | return Success(cast<DeclRefExpr>(E->getSubExpr())->getDecl()); |
6481 | } |
6482 | |
6483 | |
6484 | |
6485 | |
6486 | |
6487 | namespace { |
6488 | class RecordExprEvaluator |
6489 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
6490 | const LValue &This; |
6491 | APValue &Result; |
6492 | public: |
6493 | |
6494 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
6495 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
6496 | |
6497 | bool Success(const APValue &V, const Expr *E) { |
6498 | Result = V; |
6499 | return true; |
6500 | } |
6501 | bool ZeroInitialization(const Expr *E) { |
6502 | return ZeroInitialization(E, E->getType()); |
6503 | } |
6504 | bool ZeroInitialization(const Expr *E, QualType T); |
6505 | |
6506 | bool VisitCallExpr(const CallExpr *E) { |
6507 | return handleCallExpr(E, Result, &This); |
6508 | } |
6509 | bool VisitCastExpr(const CastExpr *E); |
6510 | bool VisitInitListExpr(const InitListExpr *E); |
6511 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
6512 | return VisitCXXConstructExpr(E, E->getType()); |
6513 | } |
6514 | bool VisitLambdaExpr(const LambdaExpr *E); |
6515 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
6516 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
6517 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
6518 | |
6519 | bool VisitBinCmp(const BinaryOperator *E); |
6520 | }; |
6521 | } |
6522 | |
6523 | |
6524 | |
6525 | |
6526 | |
6527 | |
6528 | |
6529 | |
6530 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
6531 | const RecordDecl *RD, |
6532 | const LValue &This, APValue &Result) { |
6533 | (0) . __assert_fail ("!RD->isUnion() && \"Expected non-union class type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6533, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!RD->isUnion() && "Expected non-union class type"); |
6534 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(RD); |
6535 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
6536 | std::distance(RD->field_begin(), RD->field_end())); |
6537 | |
6538 | if (RD->isInvalidDecl()) return false; |
6539 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6540 | |
6541 | if (CD) { |
6542 | unsigned Index = 0; |
6543 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
6544 | End = CD->bases_end(); I != End; ++I, ++Index) { |
6545 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
6546 | LValue Subobject = This; |
6547 | if (!HandleLValueDirectBase(Info, E, Subobject, CD, Base, &Layout)) |
6548 | return false; |
6549 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
6550 | Result.getStructBase(Index))) |
6551 | return false; |
6552 | } |
6553 | } |
6554 | |
6555 | for (const auto *I : RD->fields()) { |
6556 | |
6557 | if (I->getType()->isReferenceType()) |
6558 | continue; |
6559 | |
6560 | LValue Subobject = This; |
6561 | if (!HandleLValueMember(Info, E, Subobject, I, &Layout)) |
6562 | return false; |
6563 | |
6564 | ImplicitValueInitExpr VIE(I->getType()); |
6565 | if (!EvaluateInPlace( |
6566 | Result.getStructField(I->getFieldIndex()), Info, Subobject, &VIE)) |
6567 | return false; |
6568 | } |
6569 | |
6570 | return true; |
6571 | } |
6572 | |
6573 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
6574 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
6575 | if (RD->isInvalidDecl()) return false; |
6576 | if (RD->isUnion()) { |
6577 | |
6578 | |
6579 | RecordDecl::field_iterator I = RD->field_begin(); |
6580 | if (I == RD->field_end()) { |
6581 | Result = APValue((const FieldDecl*)nullptr); |
6582 | return true; |
6583 | } |
6584 | |
6585 | LValue Subobject = This; |
6586 | if (!HandleLValueMember(Info, E, Subobject, *I)) |
6587 | return false; |
6588 | Result = APValue(*I); |
6589 | ImplicitValueInitExpr VIE(I->getType()); |
6590 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
6591 | } |
6592 | |
6593 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->getNumVBases()) { |
6594 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
6595 | return false; |
6596 | } |
6597 | |
6598 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
6599 | } |
6600 | |
6601 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
6602 | switch (E->getCastKind()) { |
6603 | default: |
6604 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
6605 | |
6606 | case CK_ConstructorConversion: |
6607 | return Visit(E->getSubExpr()); |
6608 | |
6609 | case CK_DerivedToBase: |
6610 | case CK_UncheckedDerivedToBase: { |
6611 | APValue DerivedObject; |
6612 | if (!Evaluate(DerivedObject, Info, E->getSubExpr())) |
6613 | return false; |
6614 | if (!DerivedObject.isStruct()) |
6615 | return Error(E->getSubExpr()); |
6616 | |
6617 | |
6618 | APValue *Value = &DerivedObject; |
6619 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
6620 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
6621 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
6622 | (0) . __assert_fail ("!(*PathI)->isVirtual() && \"record rvalue with virtual base\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6622, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!(*PathI)->isVirtual() && "record rvalue with virtual base"); |
6623 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
6624 | Value = &Value->getStructBase(getBaseIndex(RD, Base)); |
6625 | RD = Base; |
6626 | } |
6627 | Result = *Value; |
6628 | return true; |
6629 | } |
6630 | } |
6631 | } |
6632 | |
6633 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
6634 | if (E->isTransparent()) |
6635 | return Visit(E->getInit(0)); |
6636 | |
6637 | const RecordDecl *RD = E->getType()->castAs<RecordType>()->getDecl(); |
6638 | if (RD->isInvalidDecl()) return false; |
6639 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6640 | |
6641 | if (RD->isUnion()) { |
6642 | const FieldDecl *Field = E->getInitializedFieldInUnion(); |
6643 | Result = APValue(Field); |
6644 | if (!Field) |
6645 | return true; |
6646 | |
6647 | |
6648 | |
6649 | |
6650 | |
6651 | |
6652 | ImplicitValueInitExpr VIE(Field->getType()); |
6653 | const Expr *InitExpr = E->getNumInits() ? E->getInit(0) : &VIE; |
6654 | |
6655 | LValue Subobject = This; |
6656 | if (!HandleLValueMember(Info, InitExpr, Subobject, Field, &Layout)) |
6657 | return false; |
6658 | |
6659 | |
6660 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
6661 | isa<CXXDefaultInitExpr>(InitExpr)); |
6662 | |
6663 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, InitExpr); |
6664 | } |
6665 | |
6666 | auto *CXXRD = dyn_cast<CXXRecordDecl>(RD); |
6667 | if (Result.isUninit()) |
6668 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
6669 | std::distance(RD->field_begin(), RD->field_end())); |
6670 | unsigned ElementNo = 0; |
6671 | bool Success = true; |
6672 | |
6673 | |
6674 | if (CXXRD) { |
6675 | for (const auto &Base : CXXRD->bases()) { |
6676 | (0) . __assert_fail ("ElementNo < E->getNumInits() && \"missing init for base class\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6676, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ElementNo < E->getNumInits() && "missing init for base class"); |
6677 | const Expr *Init = E->getInit(ElementNo); |
6678 | |
6679 | LValue Subobject = This; |
6680 | if (!HandleLValueBase(Info, Init, Subobject, CXXRD, &Base)) |
6681 | return false; |
6682 | |
6683 | APValue &FieldVal = Result.getStructBase(ElementNo); |
6684 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init)) { |
6685 | if (!Info.noteFailure()) |
6686 | return false; |
6687 | Success = false; |
6688 | } |
6689 | ++ElementNo; |
6690 | } |
6691 | } |
6692 | |
6693 | |
6694 | for (const auto *Field : RD->fields()) { |
6695 | |
6696 | |
6697 | if (Field->isUnnamedBitfield()) |
6698 | continue; |
6699 | |
6700 | LValue Subobject = This; |
6701 | |
6702 | bool HaveInit = ElementNo < E->getNumInits(); |
6703 | |
6704 | |
6705 | |
6706 | if (!HandleLValueMember(Info, HaveInit ? E->getInit(ElementNo) : E, |
6707 | Subobject, Field, &Layout)) |
6708 | return false; |
6709 | |
6710 | |
6711 | |
6712 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
6713 | const Expr *Init = HaveInit ? E->getInit(ElementNo++) : &VIE; |
6714 | |
6715 | |
6716 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
6717 | isa<CXXDefaultInitExpr>(Init)); |
6718 | |
6719 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
6720 | if (!EvaluateInPlace(FieldVal, Info, Subobject, Init) || |
6721 | (Field->isBitField() && !truncateBitfieldValue(Info, Init, |
6722 | FieldVal, Field))) { |
6723 | if (!Info.noteFailure()) |
6724 | return false; |
6725 | Success = false; |
6726 | } |
6727 | } |
6728 | |
6729 | return Success; |
6730 | } |
6731 | |
6732 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
6733 | QualType T) { |
6734 | |
6735 | |
6736 | const CXXConstructorDecl *FD = E->getConstructor(); |
6737 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
6738 | |
6739 | bool ZeroInit = E->requiresZeroInitialization(); |
6740 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
6741 | |
6742 | if (!Result.isUninit()) |
6743 | return true; |
6744 | |
6745 | |
6746 | |
6747 | |
6748 | |
6749 | |
6750 | |
6751 | |
6752 | |
6753 | return ZeroInitialization(E, T); |
6754 | } |
6755 | |
6756 | const FunctionDecl *Definition = nullptr; |
6757 | auto Body = FD->getBody(Definition); |
6758 | |
6759 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
6760 | return false; |
6761 | |
6762 | |
6763 | if (E->isElidable() && !ZeroInit) |
6764 | if (const MaterializeTemporaryExpr *ME |
6765 | = dyn_cast<MaterializeTemporaryExpr>(E->getArg(0))) |
6766 | return Visit(ME->GetTemporaryExpr()); |
6767 | |
6768 | if (ZeroInit && !ZeroInitialization(E, T)) |
6769 | return false; |
6770 | |
6771 | auto Args = llvm::makeArrayRef(E->getArgs(), E->getNumArgs()); |
6772 | return HandleConstructorCall(E, This, Args, |
6773 | cast<CXXConstructorDecl>(Definition), Info, |
6774 | Result); |
6775 | } |
6776 | |
6777 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
6778 | const CXXInheritedCtorInitExpr *E) { |
6779 | if (!Info.CurrentCall) { |
6780 | assert(Info.checkingPotentialConstantExpression()); |
6781 | return false; |
6782 | } |
6783 | |
6784 | const CXXConstructorDecl *FD = E->getConstructor(); |
6785 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
6786 | return false; |
6787 | |
6788 | const FunctionDecl *Definition = nullptr; |
6789 | auto Body = FD->getBody(Definition); |
6790 | |
6791 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
6792 | return false; |
6793 | |
6794 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
6795 | cast<CXXConstructorDecl>(Definition), Info, |
6796 | Result); |
6797 | } |
6798 | |
6799 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
6800 | const CXXStdInitializerListExpr *E) { |
6801 | const ConstantArrayType *ArrayType = |
6802 | Info.Ctx.getAsConstantArrayType(E->getSubExpr()->getType()); |
6803 | |
6804 | LValue Array; |
6805 | if (!EvaluateLValue(E->getSubExpr(), Array, Info)) |
6806 | return false; |
6807 | |
6808 | |
6809 | Array.addArray(Info, E, ArrayType); |
6810 | |
6811 | |
6812 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
6813 | RecordDecl::field_iterator Field = Record->field_begin(); |
6814 | if (Field == Record->field_end()) |
6815 | return Error(E); |
6816 | |
6817 | |
6818 | if (!Field->getType()->isPointerType() || |
6819 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
6820 | ArrayType->getElementType())) |
6821 | return Error(E); |
6822 | |
6823 | |
6824 | Result = APValue(APValue::UninitStruct(), 0, 2); |
6825 | Array.moveInto(Result.getStructField(0)); |
6826 | |
6827 | if (++Field == Record->field_end()) |
6828 | return Error(E); |
6829 | |
6830 | if (Field->getType()->isPointerType() && |
6831 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
6832 | ArrayType->getElementType())) { |
6833 | |
6834 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
6835 | ArrayType->getElementType(), |
6836 | ArrayType->getSize().getZExtValue())) |
6837 | return false; |
6838 | Array.moveInto(Result.getStructField(1)); |
6839 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
6840 | |
6841 | Result.getStructField(1) = APValue(APSInt(ArrayType->getSize())); |
6842 | else |
6843 | return Error(E); |
6844 | |
6845 | if (++Field != Record->field_end()) |
6846 | return Error(E); |
6847 | |
6848 | return true; |
6849 | } |
6850 | |
6851 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
6852 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
6853 | if (ClosureClass->isInvalidDecl()) return false; |
6854 | |
6855 | if (Info.checkingPotentialConstantExpression()) return true; |
6856 | |
6857 | const size_t NumFields = |
6858 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
6859 | |
6860 | (0) . __assert_fail ("NumFields == (size_t)std..distance(E->capture_init_begin(), E->capture_init_end()) && \"The number of lambda capture initializers should equal the number of \" \"fields within the closure type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6863, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
6861 | (0) . __assert_fail ("NumFields == (size_t)std..distance(E->capture_init_begin(), E->capture_init_end()) && \"The number of lambda capture initializers should equal the number of \" \"fields within the closure type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6863, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->capture_init_end()) && |
6862 | (0) . __assert_fail ("NumFields == (size_t)std..distance(E->capture_init_begin(), E->capture_init_end()) && \"The number of lambda capture initializers should equal the number of \" \"fields within the closure type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6863, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "The number of lambda capture initializers should equal the number of " |
6863 | (0) . __assert_fail ("NumFields == (size_t)std..distance(E->capture_init_begin(), E->capture_init_end()) && \"The number of lambda capture initializers should equal the number of \" \"fields within the closure type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6863, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "fields within the closure type"); |
6864 | |
6865 | Result = APValue(APValue::UninitStruct(), , NumFields); |
6866 | |
6867 | |
6868 | auto *CaptureInitIt = E->capture_init_begin(); |
6869 | const LambdaCapture *CaptureIt = ClosureClass->captures_begin(); |
6870 | bool Success = true; |
6871 | for (const auto *Field : ClosureClass->fields()) { |
6872 | capture_init_end()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6872, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CaptureInitIt != E->capture_init_end()); |
6873 | |
6874 | Expr *const CurFieldInit = *CaptureInitIt++; |
6875 | |
6876 | |
6877 | |
6878 | if (!CurFieldInit) |
6879 | return Error(E); |
6880 | |
6881 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
6882 | if (!EvaluateInPlace(FieldVal, Info, This, CurFieldInit)) { |
6883 | if (!Info.keepEvaluatingAfterFailure()) |
6884 | return false; |
6885 | Success = false; |
6886 | } |
6887 | ++CaptureIt; |
6888 | } |
6889 | return Success; |
6890 | } |
6891 | |
6892 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
6893 | APValue &Result, EvalInfo &Info) { |
6894 | (0) . __assert_fail ("E->isRValue() && E->getType()->isRecordType() && \"can't evaluate expression as a record rvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6895, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isRecordType() && |
6895 | (0) . __assert_fail ("E->isRValue() && E->getType()->isRecordType() && \"can't evaluate expression as a record rvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6895, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "can't evaluate expression as a record rvalue"); |
6896 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
6897 | } |
6898 | |
6899 | |
6900 | |
6901 | |
6902 | |
6903 | |
6904 | |
6905 | |
6906 | namespace { |
6907 | class TemporaryExprEvaluator |
6908 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
6909 | public: |
6910 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
6911 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
6912 | |
6913 | |
6914 | bool VisitConstructExpr(const Expr *E) { |
6915 | APValue &Value = createTemporary(E, false, Result, *Info.CurrentCall); |
6916 | return EvaluateInPlace(Value, Info, Result, E); |
6917 | } |
6918 | |
6919 | bool VisitCastExpr(const CastExpr *E) { |
6920 | switch (E->getCastKind()) { |
6921 | default: |
6922 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
6923 | |
6924 | case CK_ConstructorConversion: |
6925 | return VisitConstructExpr(E->getSubExpr()); |
6926 | } |
6927 | } |
6928 | bool VisitInitListExpr(const InitListExpr *E) { |
6929 | return VisitConstructExpr(E); |
6930 | } |
6931 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
6932 | return VisitConstructExpr(E); |
6933 | } |
6934 | bool VisitCallExpr(const CallExpr *E) { |
6935 | return VisitConstructExpr(E); |
6936 | } |
6937 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
6938 | return VisitConstructExpr(E); |
6939 | } |
6940 | bool VisitLambdaExpr(const LambdaExpr *E) { |
6941 | return VisitConstructExpr(E); |
6942 | } |
6943 | }; |
6944 | } |
6945 | |
6946 | |
6947 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
6948 | isRValue() && E->getType()->isRecordType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6948, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isRecordType()); |
6949 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
6950 | } |
6951 | |
6952 | |
6953 | |
6954 | |
6955 | |
6956 | namespace { |
6957 | class VectorExprEvaluator |
6958 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
6959 | APValue &Result; |
6960 | public: |
6961 | |
6962 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
6963 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
6964 | |
6965 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
6966 | getType()->castAs()->getNumElements()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6966, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
6967 | |
6968 | Result = APValue(V.data(), V.size()); |
6969 | return true; |
6970 | } |
6971 | bool Success(const APValue &V, const Expr *E) { |
6972 | assert(V.isVector()); |
6973 | Result = V; |
6974 | return true; |
6975 | } |
6976 | bool ZeroInitialization(const Expr *E); |
6977 | |
6978 | bool VisitUnaryReal(const UnaryOperator *E) |
6979 | { return Visit(E->getSubExpr()); } |
6980 | bool VisitCastExpr(const CastExpr* E); |
6981 | bool VisitInitListExpr(const InitListExpr *E); |
6982 | bool VisitUnaryImag(const UnaryOperator *E); |
6983 | |
6984 | |
6985 | |
6986 | }; |
6987 | } |
6988 | |
6989 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
6990 | (0) . __assert_fail ("E->isRValue() && E->getType()->isVectorType() &&\"not a vector rvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 6990, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isVectorType() &&"not a vector rvalue"); |
6991 | return VectorExprEvaluator(Info, Result).Visit(E); |
6992 | } |
6993 | |
6994 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
6995 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
6996 | unsigned NElts = VTy->getNumElements(); |
6997 | |
6998 | const Expr *SE = E->getSubExpr(); |
6999 | QualType SETy = SE->getType(); |
7000 | |
7001 | switch (E->getCastKind()) { |
7002 | case CK_VectorSplat: { |
7003 | APValue Val = APValue(); |
7004 | if (SETy->isIntegerType()) { |
7005 | APSInt IntResult; |
7006 | if (!EvaluateInteger(SE, IntResult, Info)) |
7007 | return false; |
7008 | Val = APValue(std::move(IntResult)); |
7009 | } else if (SETy->isRealFloatingType()) { |
7010 | APFloat FloatResult(0.0); |
7011 | if (!EvaluateFloat(SE, FloatResult, Info)) |
7012 | return false; |
7013 | Val = APValue(std::move(FloatResult)); |
7014 | } else { |
7015 | return Error(E); |
7016 | } |
7017 | |
7018 | |
7019 | SmallVector<APValue, 4> Elts(NElts, Val); |
7020 | return Success(Elts, E); |
7021 | } |
7022 | case CK_BitCast: { |
7023 | |
7024 | llvm::APInt SValInt; |
7025 | if (!EvalAndBitcastToAPInt(Info, SE, SValInt)) |
7026 | return false; |
7027 | |
7028 | QualType EltTy = VTy->getElementType(); |
7029 | unsigned EltSize = Info.Ctx.getTypeSize(EltTy); |
7030 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7031 | SmallVector<APValue, 4> Elts; |
7032 | if (EltTy->isRealFloatingType()) { |
7033 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy); |
7034 | unsigned FloatEltSize = EltSize; |
7035 | if (&Sem == &APFloat::x87DoubleExtended()) |
7036 | FloatEltSize = 80; |
7037 | for (unsigned i = 0; i < NElts; i++) { |
7038 | llvm::APInt Elt; |
7039 | if (BigEndian) |
7040 | Elt = SValInt.rotl(i*EltSize+FloatEltSize).trunc(FloatEltSize); |
7041 | else |
7042 | Elt = SValInt.rotr(i*EltSize).trunc(FloatEltSize); |
7043 | Elts.push_back(APValue(APFloat(Sem, Elt))); |
7044 | } |
7045 | } else if (EltTy->isIntegerType()) { |
7046 | for (unsigned i = 0; i < NElts; i++) { |
7047 | llvm::APInt Elt; |
7048 | if (BigEndian) |
7049 | Elt = SValInt.rotl(i*EltSize+EltSize).zextOrTrunc(EltSize); |
7050 | else |
7051 | Elt = SValInt.rotr(i*EltSize).zextOrTrunc(EltSize); |
7052 | Elts.push_back(APValue(APSInt(Elt, EltTy->isSignedIntegerType()))); |
7053 | } |
7054 | } else { |
7055 | return Error(E); |
7056 | } |
7057 | return Success(Elts, E); |
7058 | } |
7059 | default: |
7060 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
7061 | } |
7062 | } |
7063 | |
7064 | bool |
7065 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
7066 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
7067 | unsigned NumInits = E->getNumInits(); |
7068 | unsigned NumElements = VT->getNumElements(); |
7069 | |
7070 | QualType EltTy = VT->getElementType(); |
7071 | SmallVector<APValue, 4> Elements; |
7072 | |
7073 | |
7074 | |
7075 | |
7076 | |
7077 | unsigned CountInits = 0, CountElts = 0; |
7078 | while (CountElts < NumElements) { |
7079 | |
7080 | if (CountInits < NumInits |
7081 | && E->getInit(CountInits)->getType()->isVectorType()) { |
7082 | APValue v; |
7083 | if (!EvaluateVector(E->getInit(CountInits), v, Info)) |
7084 | return Error(E); |
7085 | unsigned vlen = v.getVectorLength(); |
7086 | for (unsigned j = 0; j < vlen; j++) |
7087 | Elements.push_back(v.getVectorElt(j)); |
7088 | CountElts += vlen; |
7089 | } else if (EltTy->isIntegerType()) { |
7090 | llvm::APSInt sInt(32); |
7091 | if (CountInits < NumInits) { |
7092 | if (!EvaluateInteger(E->getInit(CountInits), sInt, Info)) |
7093 | return false; |
7094 | } else |
7095 | sInt = Info.Ctx.MakeIntValue(0, EltTy); |
7096 | Elements.push_back(APValue(sInt)); |
7097 | CountElts++; |
7098 | } else { |
7099 | llvm::APFloat f(0.0); |
7100 | if (CountInits < NumInits) { |
7101 | if (!EvaluateFloat(E->getInit(CountInits), f, Info)) |
7102 | return false; |
7103 | } else |
7104 | f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy)); |
7105 | Elements.push_back(APValue(f)); |
7106 | CountElts++; |
7107 | } |
7108 | CountInits++; |
7109 | } |
7110 | return Success(Elements, E); |
7111 | } |
7112 | |
7113 | bool |
7114 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
7115 | const VectorType *VT = E->getType()->getAs<VectorType>(); |
7116 | QualType EltTy = VT->getElementType(); |
7117 | APValue ZeroElement; |
7118 | if (EltTy->isIntegerType()) |
7119 | ZeroElement = APValue(Info.Ctx.MakeIntValue(0, EltTy)); |
7120 | else |
7121 | ZeroElement = |
7122 | APValue(APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy))); |
7123 | |
7124 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
7125 | return Success(Elements, E); |
7126 | } |
7127 | |
7128 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
7129 | VisitIgnoredValue(E->getSubExpr()); |
7130 | return ZeroInitialization(E); |
7131 | } |
7132 | |
7133 | |
7134 | |
7135 | |
7136 | |
7137 | namespace { |
7138 | class ArrayExprEvaluator |
7139 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
7140 | const LValue &This; |
7141 | APValue &Result; |
7142 | public: |
7143 | |
7144 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
7145 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
7146 | |
7147 | bool Success(const APValue &V, const Expr *E) { |
7148 | (0) . __assert_fail ("V.isArray() && \"expected array\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7148, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V.isArray() && "expected array"); |
7149 | Result = V; |
7150 | return true; |
7151 | } |
7152 | |
7153 | bool ZeroInitialization(const Expr *E) { |
7154 | const ConstantArrayType *CAT = |
7155 | Info.Ctx.getAsConstantArrayType(E->getType()); |
7156 | if (!CAT) |
7157 | return Error(E); |
7158 | |
7159 | Result = APValue(APValue::UninitArray(), 0, |
7160 | CAT->getSize().getZExtValue()); |
7161 | if (!Result.hasArrayFiller()) return true; |
7162 | |
7163 | |
7164 | LValue Subobject = This; |
7165 | Subobject.addArray(Info, E, CAT); |
7166 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
7167 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
7168 | } |
7169 | |
7170 | bool VisitCallExpr(const CallExpr *E) { |
7171 | return handleCallExpr(E, Result, &This); |
7172 | } |
7173 | bool VisitInitListExpr(const InitListExpr *E); |
7174 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
7175 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
7176 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
7177 | const LValue &Subobject, |
7178 | APValue *Value, QualType Type); |
7179 | bool VisitStringLiteral(const StringLiteral *E) { |
7180 | expandStringLiteral(Info, E, Result); |
7181 | return true; |
7182 | } |
7183 | }; |
7184 | } |
7185 | |
7186 | static bool EvaluateArray(const Expr *E, const LValue &This, |
7187 | APValue &Result, EvalInfo &Info) { |
7188 | (0) . __assert_fail ("E->isRValue() && E->getType()->isArrayType() && \"not an array rvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7188, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isArrayType() && "not an array rvalue"); |
7189 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
7190 | } |
7191 | |
7192 | |
7193 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
7194 | |
7195 | |
7196 | if (isa<ImplicitValueInitExpr>(FillerExpr)) |
7197 | return false; |
7198 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(FillerExpr)) { |
7199 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
7200 | if (MaybeElementDependentArrayFiller(ILE->getInit(I))) |
7201 | return true; |
7202 | } |
7203 | return false; |
7204 | } |
7205 | return true; |
7206 | } |
7207 | |
7208 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
7209 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(E->getType()); |
7210 | if (!CAT) |
7211 | return Error(E); |
7212 | |
7213 | |
7214 | |
7215 | if (E->isStringLiteralInit()) |
7216 | return Visit(E->getInit(0)); |
7217 | |
7218 | bool Success = true; |
7219 | |
7220 | (0) . __assert_fail ("(!Result.isArray() || Result.getArrayInitializedElts() == 0) && \"zero-initialized array shouldn't have any initialized elts\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7221, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
7221 | (0) . __assert_fail ("(!Result.isArray() || Result.getArrayInitializedElts() == 0) && \"zero-initialized array shouldn't have any initialized elts\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7221, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "zero-initialized array shouldn't have any initialized elts"); |
7222 | APValue Filler; |
7223 | if (Result.isArray() && Result.hasArrayFiller()) |
7224 | Filler = Result.getArrayFiller(); |
7225 | |
7226 | unsigned NumEltsToInit = E->getNumInits(); |
7227 | unsigned NumElts = CAT->getSize().getZExtValue(); |
7228 | const Expr *FillerExpr = E->hasArrayFiller() ? E->getArrayFiller() : nullptr; |
7229 | |
7230 | |
7231 | |
7232 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr)) |
7233 | NumEltsToInit = NumElts; |
7234 | |
7235 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
7236 | << NumEltsToInit << ".\n"); |
7237 | |
7238 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
7239 | |
7240 | |
7241 | |
7242 | if (!Filler.isUninit()) { |
7243 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
7244 | Result.getArrayInitializedElt(I) = Filler; |
7245 | if (Result.hasArrayFiller()) |
7246 | Result.getArrayFiller() = Filler; |
7247 | } |
7248 | |
7249 | LValue Subobject = This; |
7250 | Subobject.addArray(Info, E, CAT); |
7251 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
7252 | const Expr *Init = |
7253 | Index < E->getNumInits() ? E->getInit(Index) : FillerExpr; |
7254 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
7255 | Info, Subobject, Init) || |
7256 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
7257 | CAT->getElementType(), 1)) { |
7258 | if (!Info.noteFailure()) |
7259 | return false; |
7260 | Success = false; |
7261 | } |
7262 | } |
7263 | |
7264 | if (!Result.hasArrayFiller()) |
7265 | return Success; |
7266 | |
7267 | |
7268 | |
7269 | (0) . __assert_fail ("FillerExpr && \"no array filler for incomplete init list\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7269, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FillerExpr && "no array filler for incomplete init list"); |
7270 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, |
7271 | FillerExpr) && Success; |
7272 | } |
7273 | |
7274 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
7275 | if (E->getCommonExpr() && |
7276 | !Evaluate(Info.CurrentCall->createTemporary(E->getCommonExpr(), false), |
7277 | Info, E->getCommonExpr()->getSourceExpr())) |
7278 | return false; |
7279 | |
7280 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
7281 | |
7282 | uint64_t Elements = CAT->getSize().getZExtValue(); |
7283 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
7284 | |
7285 | LValue Subobject = This; |
7286 | Subobject.addArray(Info, E, CAT); |
7287 | |
7288 | bool Success = true; |
7289 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
7290 | if (!EvaluateInPlace(Result.getArrayInitializedElt(Index), |
7291 | Info, Subobject, E->getSubExpr()) || |
7292 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
7293 | CAT->getElementType(), 1)) { |
7294 | if (!Info.noteFailure()) |
7295 | return false; |
7296 | Success = false; |
7297 | } |
7298 | } |
7299 | |
7300 | return Success; |
7301 | } |
7302 | |
7303 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
7304 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
7305 | } |
7306 | |
7307 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
7308 | const LValue &Subobject, |
7309 | APValue *Value, |
7310 | QualType Type) { |
7311 | bool HadZeroInit = !Value->isUninit(); |
7312 | |
7313 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) { |
7314 | unsigned N = CAT->getSize().getZExtValue(); |
7315 | |
7316 | |
7317 | APValue Filler = |
7318 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
7319 | : APValue(); |
7320 | |
7321 | *Value = APValue(APValue::UninitArray(), N, N); |
7322 | |
7323 | if (HadZeroInit) |
7324 | for (unsigned I = 0; I != N; ++I) |
7325 | Value->getArrayInitializedElt(I) = Filler; |
7326 | |
7327 | |
7328 | LValue ArrayElt = Subobject; |
7329 | ArrayElt.addArray(Info, E, CAT); |
7330 | for (unsigned I = 0; I != N; ++I) |
7331 | if (!VisitCXXConstructExpr(E, ArrayElt, &Value->getArrayInitializedElt(I), |
7332 | CAT->getElementType()) || |
7333 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
7334 | CAT->getElementType(), 1)) |
7335 | return false; |
7336 | |
7337 | return true; |
7338 | } |
7339 | |
7340 | if (!Type->isRecordType()) |
7341 | return Error(E); |
7342 | |
7343 | return RecordExprEvaluator(Info, Subobject, *Value) |
7344 | .VisitCXXConstructExpr(E, Type); |
7345 | } |
7346 | |
7347 | |
7348 | |
7349 | |
7350 | |
7351 | |
7352 | |
7353 | |
7354 | |
7355 | namespace { |
7356 | class IntExprEvaluator |
7357 | : public ExprEvaluatorBase<IntExprEvaluator> { |
7358 | APValue &Result; |
7359 | public: |
7360 | IntExprEvaluator(EvalInfo &info, APValue &result) |
7361 | : ExprEvaluatorBaseTy(info), Result(result) {} |
7362 | |
7363 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
7364 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7365, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getType()->isIntegralOrEnumerationType() && |
7365 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7365, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7366 | (0) . __assert_fail ("SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7367, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
7367 | (0) . __assert_fail ("SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7367, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7368 | (0) . __assert_fail ("SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7369, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
7369 | (0) . __assert_fail ("SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7369, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7370 | Result = APValue(SI); |
7371 | return true; |
7372 | } |
7373 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
7374 | return Success(SI, E, Result); |
7375 | } |
7376 | |
7377 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
7378 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7379, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getType()->isIntegralOrEnumerationType() && |
7379 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7379, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7380 | (0) . __assert_fail ("I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7381, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
7381 | (0) . __assert_fail ("I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7381, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7382 | Result = APValue(APSInt(I)); |
7383 | Result.getInt().setIsUnsigned( |
7384 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
7385 | return true; |
7386 | } |
7387 | bool Success(const llvm::APInt &I, const Expr *E) { |
7388 | return Success(I, E, Result); |
7389 | } |
7390 | |
7391 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
7392 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7393, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getType()->isIntegralOrEnumerationType() && |
7393 | (0) . __assert_fail ("E->getType()->isIntegralOrEnumerationType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7393, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7394 | Result = APValue(Info.Ctx.MakeIntValue(Value, E->getType())); |
7395 | return true; |
7396 | } |
7397 | bool Success(uint64_t Value, const Expr *E) { |
7398 | return Success(Value, E, Result); |
7399 | } |
7400 | |
7401 | bool Success(CharUnits Size, const Expr *E) { |
7402 | return Success(Size.getQuantity(), E); |
7403 | } |
7404 | |
7405 | bool Success(const APValue &V, const Expr *E) { |
7406 | if (V.isLValue() || V.isAddrLabelDiff()) { |
7407 | Result = V; |
7408 | return true; |
7409 | } |
7410 | return Success(V.getInt(), E); |
7411 | } |
7412 | |
7413 | bool ZeroInitialization(const Expr *E) { return Success(0, E); } |
7414 | |
7415 | |
7416 | |
7417 | |
7418 | |
7419 | bool VisitConstantExpr(const ConstantExpr *E); |
7420 | |
7421 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
7422 | return Success(E->getValue(), E); |
7423 | } |
7424 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
7425 | return Success(E->getValue(), E); |
7426 | } |
7427 | |
7428 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
7429 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
7430 | if (CheckReferencedDecl(E, E->getDecl())) |
7431 | return true; |
7432 | |
7433 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
7434 | } |
7435 | bool VisitMemberExpr(const MemberExpr *E) { |
7436 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
7437 | VisitIgnoredBaseExpression(E->getBase()); |
7438 | return true; |
7439 | } |
7440 | |
7441 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
7442 | } |
7443 | |
7444 | bool VisitCallExpr(const CallExpr *E); |
7445 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
7446 | bool VisitBinaryOperator(const BinaryOperator *E); |
7447 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
7448 | bool VisitUnaryOperator(const UnaryOperator *E); |
7449 | |
7450 | bool VisitCastExpr(const CastExpr* E); |
7451 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
7452 | |
7453 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
7454 | return Success(E->getValue(), E); |
7455 | } |
7456 | |
7457 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
7458 | return Success(E->getValue(), E); |
7459 | } |
7460 | |
7461 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
7462 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
7463 | |
7464 | |
7465 | Info.FFDiag(E); |
7466 | return false; |
7467 | } |
7468 | return Success(Info.ArrayInitIndex, E); |
7469 | } |
7470 | |
7471 | |
7472 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
7473 | return ZeroInitialization(E); |
7474 | } |
7475 | |
7476 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
7477 | return Success(E->getValue(), E); |
7478 | } |
7479 | |
7480 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
7481 | return Success(E->getValue(), E); |
7482 | } |
7483 | |
7484 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
7485 | return Success(E->getValue(), E); |
7486 | } |
7487 | |
7488 | bool VisitUnaryReal(const UnaryOperator *E); |
7489 | bool VisitUnaryImag(const UnaryOperator *E); |
7490 | |
7491 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
7492 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
7493 | |
7494 | |
7495 | }; |
7496 | |
7497 | class FixedPointExprEvaluator |
7498 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
7499 | APValue &Result; |
7500 | |
7501 | public: |
7502 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
7503 | : ExprEvaluatorBaseTy(info), Result(result) {} |
7504 | |
7505 | bool Success(const llvm::APInt &I, const Expr *E) { |
7506 | return Success( |
7507 | APFixedPoint(I, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
7508 | } |
7509 | |
7510 | bool Success(uint64_t Value, const Expr *E) { |
7511 | return Success( |
7512 | APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(E->getType())), E); |
7513 | } |
7514 | |
7515 | bool Success(const APValue &V, const Expr *E) { |
7516 | return Success(V.getFixedPoint(), E); |
7517 | } |
7518 | |
7519 | bool Success(const APFixedPoint &V, const Expr *E) { |
7520 | (0) . __assert_fail ("E->getType()->isFixedPointType() && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7520, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getType()->isFixedPointType() && "Invalid evaluation result."); |
7521 | (0) . __assert_fail ("V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7522, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
7522 | (0) . __assert_fail ("V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && \"Invalid evaluation result.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7522, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid evaluation result."); |
7523 | Result = APValue(V); |
7524 | return true; |
7525 | } |
7526 | |
7527 | |
7528 | |
7529 | |
7530 | |
7531 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
7532 | return Success(E->getValue(), E); |
7533 | } |
7534 | |
7535 | bool VisitCastExpr(const CastExpr *E); |
7536 | bool VisitUnaryOperator(const UnaryOperator *E); |
7537 | bool VisitBinaryOperator(const BinaryOperator *E); |
7538 | }; |
7539 | } |
7540 | |
7541 | |
7542 | |
7543 | |
7544 | |
7545 | |
7546 | |
7547 | |
7548 | |
7549 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
7550 | EvalInfo &Info) { |
7551 | isRValue() && E->getType()->isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7551, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isIntegralOrEnumerationType()); |
7552 | return IntExprEvaluator(Info, Result).Visit(E); |
7553 | } |
7554 | |
7555 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
7556 | APValue Val; |
7557 | if (!EvaluateIntegerOrLValue(E, Val, Info)) |
7558 | return false; |
7559 | if (!Val.isInt()) { |
7560 | |
7561 | |
7562 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
7563 | return false; |
7564 | } |
7565 | Result = Val.getInt(); |
7566 | return true; |
7567 | } |
7568 | |
7569 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
7570 | EvalInfo &Info) { |
7571 | if (E->getType()->isFixedPointType()) { |
7572 | APValue Val; |
7573 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
7574 | return false; |
7575 | if (!Val.isFixedPoint()) |
7576 | return false; |
7577 | |
7578 | Result = Val.getFixedPoint(); |
7579 | return true; |
7580 | } |
7581 | return false; |
7582 | } |
7583 | |
7584 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
7585 | EvalInfo &Info) { |
7586 | if (E->getType()->isIntegerType()) { |
7587 | auto FXSema = Info.Ctx.getFixedPointSemantics(E->getType()); |
7588 | APSInt Val; |
7589 | if (!EvaluateInteger(E, Val, Info)) |
7590 | return false; |
7591 | Result = APFixedPoint(Val, FXSema); |
7592 | return true; |
7593 | } else if (E->getType()->isFixedPointType()) { |
7594 | return EvaluateFixedPoint(E, Result, Info); |
7595 | } |
7596 | return false; |
7597 | } |
7598 | |
7599 | |
7600 | |
7601 | |
7602 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
7603 | |
7604 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) { |
7605 | |
7606 | bool SameSign = (ECD->getInitVal().isSigned() |
7607 | == E->getType()->isSignedIntegerOrEnumerationType()); |
7608 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
7609 | == Info.Ctx.getIntWidth(E->getType())); |
7610 | if (SameSign && SameWidth) |
7611 | return Success(ECD->getInitVal(), E); |
7612 | else { |
7613 | |
7614 | |
7615 | llvm::APSInt Val = ECD->getInitVal(); |
7616 | if (!SameSign) |
7617 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
7618 | if (!SameWidth) |
7619 | Val = Val.extOrTrunc(Info.Ctx.getIntWidth(E->getType())); |
7620 | return Success(Val, E); |
7621 | } |
7622 | } |
7623 | return false; |
7624 | } |
7625 | |
7626 | |
7627 | |
7628 | enum class GCCTypeClass { |
7629 | None = -1, |
7630 | Void = 0, |
7631 | Integer = 1, |
7632 | |
7633 | |
7634 | Enum = 3, |
7635 | Bool = 4, |
7636 | Pointer = 5, |
7637 | |
7638 | |
7639 | PointerToDataMember = 7, |
7640 | RealFloat = 8, |
7641 | Complex = 9, |
7642 | |
7643 | |
7644 | |
7645 | |
7646 | |
7647 | PointerToMemberFunction = 12, |
7648 | ClassOrStruct = 12, |
7649 | Union = 13, |
7650 | |
7651 | |
7652 | |
7653 | |
7654 | }; |
7655 | |
7656 | |
7657 | |
7658 | static GCCTypeClass |
7659 | EvaluateBuiltinClassifyType(QualType T, const LangOptions &LangOpts) { |
7660 | (0) . __assert_fail ("!T->isDependentType() && \"unexpected dependent type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7660, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!T->isDependentType() && "unexpected dependent type"); |
7661 | |
7662 | QualType CanTy = T.getCanonicalType(); |
7663 | const BuiltinType *BT = dyn_cast<BuiltinType>(CanTy); |
7664 | |
7665 | switch (CanTy->getTypeClass()) { |
7666 | #define TYPE(ID, BASE) |
7667 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
7668 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
7669 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
7670 | #include "clang/AST/TypeNodes.def" |
7671 | case Type::Auto: |
7672 | case Type::DeducedTemplateSpecialization: |
7673 | llvm_unreachable("unexpected non-canonical or dependent type"); |
7674 | |
7675 | case Type::Builtin: |
7676 | switch (BT->getKind()) { |
7677 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
7678 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
7679 | case BuiltinType::ID: return GCCTypeClass::Integer; |
7680 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
7681 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
7682 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
7683 | case BuiltinType::ID: break; |
7684 | #include "clang/AST/BuiltinTypes.def" |
7685 | case BuiltinType::Void: |
7686 | return GCCTypeClass::Void; |
7687 | |
7688 | case BuiltinType::Bool: |
7689 | return GCCTypeClass::Bool; |
7690 | |
7691 | case BuiltinType::Char_U: |
7692 | case BuiltinType::UChar: |
7693 | case BuiltinType::WChar_U: |
7694 | case BuiltinType::Char8: |
7695 | case BuiltinType::Char16: |
7696 | case BuiltinType::Char32: |
7697 | case BuiltinType::UShort: |
7698 | case BuiltinType::UInt: |
7699 | case BuiltinType::ULong: |
7700 | case BuiltinType::ULongLong: |
7701 | case BuiltinType::UInt128: |
7702 | return GCCTypeClass::Integer; |
7703 | |
7704 | case BuiltinType::UShortAccum: |
7705 | case BuiltinType::UAccum: |
7706 | case BuiltinType::ULongAccum: |
7707 | case BuiltinType::UShortFract: |
7708 | case BuiltinType::UFract: |
7709 | case BuiltinType::ULongFract: |
7710 | case BuiltinType::SatUShortAccum: |
7711 | case BuiltinType::SatUAccum: |
7712 | case BuiltinType::SatULongAccum: |
7713 | case BuiltinType::SatUShortFract: |
7714 | case BuiltinType::SatUFract: |
7715 | case BuiltinType::SatULongFract: |
7716 | return GCCTypeClass::None; |
7717 | |
7718 | case BuiltinType::NullPtr: |
7719 | |
7720 | case BuiltinType::ObjCId: |
7721 | case BuiltinType::ObjCClass: |
7722 | case BuiltinType::ObjCSel: |
7723 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
7724 | case BuiltinType::Id: |
7725 | #include "clang/Basic/OpenCLImageTypes.def" |
7726 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
7727 | case BuiltinType::Id: |
7728 | #include "clang/Basic/OpenCLExtensionTypes.def" |
7729 | case BuiltinType::OCLSampler: |
7730 | case BuiltinType::OCLEvent: |
7731 | case BuiltinType::OCLClkEvent: |
7732 | case BuiltinType::OCLQueue: |
7733 | case BuiltinType::OCLReserveID: |
7734 | return GCCTypeClass::None; |
7735 | |
7736 | case BuiltinType::Dependent: |
7737 | llvm_unreachable("unexpected dependent type"); |
7738 | }; |
7739 | llvm_unreachable("unexpected placeholder type"); |
7740 | |
7741 | case Type::Enum: |
7742 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
7743 | |
7744 | case Type::Pointer: |
7745 | case Type::ConstantArray: |
7746 | case Type::VariableArray: |
7747 | case Type::IncompleteArray: |
7748 | case Type::FunctionNoProto: |
7749 | case Type::FunctionProto: |
7750 | return GCCTypeClass::Pointer; |
7751 | |
7752 | case Type::MemberPointer: |
7753 | return CanTy->isMemberDataPointerType() |
7754 | ? GCCTypeClass::PointerToDataMember |
7755 | : GCCTypeClass::PointerToMemberFunction; |
7756 | |
7757 | case Type::Complex: |
7758 | return GCCTypeClass::Complex; |
7759 | |
7760 | case Type::Record: |
7761 | return CanTy->isUnionType() ? GCCTypeClass::Union |
7762 | : GCCTypeClass::ClassOrStruct; |
7763 | |
7764 | case Type::Atomic: |
7765 | |
7766 | return EvaluateBuiltinClassifyType( |
7767 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
7768 | |
7769 | case Type::BlockPointer: |
7770 | case Type::Vector: |
7771 | case Type::ExtVector: |
7772 | case Type::ObjCObject: |
7773 | case Type::ObjCInterface: |
7774 | case Type::ObjCObjectPointer: |
7775 | case Type::Pipe: |
7776 | |
7777 | |
7778 | return GCCTypeClass::None; |
7779 | |
7780 | case Type::LValueReference: |
7781 | case Type::RValueReference: |
7782 | llvm_unreachable("invalid type for expression"); |
7783 | } |
7784 | |
7785 | llvm_unreachable("unexpected type class"); |
7786 | } |
7787 | |
7788 | |
7789 | |
7790 | static GCCTypeClass |
7791 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
7792 | |
7793 | |
7794 | if (E->getNumArgs() == 0) |
7795 | return GCCTypeClass::None; |
7796 | |
7797 | |
7798 | |
7799 | |
7800 | return EvaluateBuiltinClassifyType(E->getArg(0)->getType(), LangOpts); |
7801 | } |
7802 | |
7803 | |
7804 | |
7805 | |
7806 | |
7807 | |
7808 | template<typename LValue> |
7809 | static bool EvaluateBuiltinConstantPForLValue(const LValue &LV) { |
7810 | const Expr *E = LV.getLValueBase().template dyn_cast<const Expr*>(); |
7811 | return E && isa<StringLiteral>(E) && LV.getLValueOffset().isZero(); |
7812 | } |
7813 | |
7814 | |
7815 | |
7816 | static bool EvaluateBuiltinConstantP(ASTContext &Ctx, const Expr *Arg) { |
7817 | QualType ArgType = Arg->getType(); |
7818 | |
7819 | |
7820 | |
7821 | |
7822 | |
7823 | |
7824 | |
7825 | |
7826 | |
7827 | |
7828 | |
7829 | |
7830 | |
7831 | |
7832 | if (ArgType->isIntegralOrEnumerationType()) { |
7833 | Expr::EvalResult Result; |
7834 | if (!Arg->EvaluateAsRValue(Result, Ctx) || Result.HasSideEffects) |
7835 | return false; |
7836 | |
7837 | APValue &V = Result.Val; |
7838 | if (V.getKind() == APValue::Int) |
7839 | return true; |
7840 | if (V.getKind() == APValue::LValue) |
7841 | return EvaluateBuiltinConstantPForLValue(V); |
7842 | } else if (ArgType->isFloatingType() || ArgType->isAnyComplexType()) { |
7843 | return Arg->isEvaluatable(Ctx); |
7844 | } else if (ArgType->isPointerType() || Arg->isGLValue()) { |
7845 | LValue LV; |
7846 | Expr::EvalStatus Status; |
7847 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
7848 | if ((Arg->isGLValue() ? EvaluateLValue(Arg, LV, Info) |
7849 | : EvaluatePointer(Arg, LV, Info)) && |
7850 | !Status.HasSideEffects) |
7851 | return EvaluateBuiltinConstantPForLValue(LV); |
7852 | } |
7853 | |
7854 | |
7855 | return false; |
7856 | } |
7857 | |
7858 | |
7859 | |
7860 | static QualType getObjectType(APValue::LValueBase B) { |
7861 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
7862 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
7863 | return VD->getType(); |
7864 | } else if (const Expr *E = B.get<const Expr*>()) { |
7865 | if (isa<CompoundLiteralExpr>(E)) |
7866 | return E->getType(); |
7867 | } |
7868 | |
7869 | return QualType(); |
7870 | } |
7871 | |
7872 | |
7873 | |
7874 | |
7875 | |
7876 | |
7877 | |
7878 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
7879 | isRValue() && E->getType()->hasPointerRepresentation()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7879, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->hasPointerRepresentation()); |
7880 | |
7881 | auto *NoParens = E->IgnoreParens(); |
7882 | auto *Cast = dyn_cast<CastExpr>(NoParens); |
7883 | if (Cast == nullptr) |
7884 | return NoParens; |
7885 | |
7886 | |
7887 | |
7888 | auto CastKind = Cast->getCastKind(); |
7889 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
7890 | CastKind != CK_AddressSpaceConversion) |
7891 | return NoParens; |
7892 | |
7893 | auto *SubExpr = Cast->getSubExpr(); |
7894 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isRValue()) |
7895 | return NoParens; |
7896 | return ignorePointerCastsAndParens(SubExpr); |
7897 | } |
7898 | |
7899 | |
7900 | |
7901 | |
7902 | |
7903 | |
7904 | |
7905 | |
7906 | |
7907 | |
7908 | |
7909 | |
7910 | |
7911 | |
7912 | |
7913 | |
7914 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
7915 | assert(!LVal.Designator.Invalid); |
7916 | |
7917 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
7918 | const RecordDecl *Parent = FD->getParent(); |
7919 | Invalid = Parent->isInvalidDecl(); |
7920 | if (Invalid || Parent->isUnion()) |
7921 | return true; |
7922 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(Parent); |
7923 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
7924 | }; |
7925 | |
7926 | auto &Base = LVal.getLValueBase(); |
7927 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Base.dyn_cast<const Expr *>())) { |
7928 | if (auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
7929 | bool Invalid; |
7930 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
7931 | return Invalid; |
7932 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(ME->getMemberDecl())) { |
7933 | for (auto *FD : IFD->chain()) { |
7934 | bool Invalid; |
7935 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(FD), Invalid)) |
7936 | return Invalid; |
7937 | } |
7938 | } |
7939 | } |
7940 | |
7941 | unsigned I = 0; |
7942 | QualType BaseType = getType(Base); |
7943 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
7944 | |
7945 | |
7946 | ++I; |
7947 | if (BaseType->isIncompleteArrayType()) |
7948 | BaseType = Ctx.getAsArrayType(BaseType)->getElementType(); |
7949 | else |
7950 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
7951 | } |
7952 | |
7953 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
7954 | const auto &Entry = LVal.Designator.Entries[I]; |
7955 | if (BaseType->isArrayType()) { |
7956 | |
7957 | |
7958 | if (I + 1 == E) |
7959 | return true; |
7960 | const auto *CAT = cast<ConstantArrayType>(Ctx.getAsArrayType(BaseType)); |
7961 | uint64_t Index = Entry.ArrayIndex; |
7962 | if (Index + 1 != CAT->getSize()) |
7963 | return false; |
7964 | BaseType = CAT->getElementType(); |
7965 | } else if (BaseType->isAnyComplexType()) { |
7966 | const auto *CT = BaseType->castAs<ComplexType>(); |
7967 | uint64_t Index = Entry.ArrayIndex; |
7968 | if (Index != 1) |
7969 | return false; |
7970 | BaseType = CT->getElementType(); |
7971 | } else if (auto *FD = getAsField(Entry)) { |
7972 | bool Invalid; |
7973 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
7974 | return Invalid; |
7975 | BaseType = FD->getType(); |
7976 | } else { |
7977 | (0) . __assert_fail ("getAsBaseClass(Entry) && \"Expecting cast to a base class\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 7977, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(getAsBaseClass(Entry) && "Expecting cast to a base class"); |
7978 | return false; |
7979 | } |
7980 | } |
7981 | return true; |
7982 | } |
7983 | |
7984 | |
7985 | |
7986 | |
7987 | |
7988 | static bool refersToCompleteObject(const LValue &LVal) { |
7989 | if (LVal.Designator.Invalid) |
7990 | return false; |
7991 | |
7992 | if (!LVal.Designator.Entries.empty()) |
7993 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
7994 | |
7995 | if (!LVal.InvalidBase) |
7996 | return true; |
7997 | |
7998 | |
7999 | |
8000 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
8001 | return !E || !isa<MemberExpr>(E); |
8002 | } |
8003 | |
8004 | |
8005 | |
8006 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
8007 | const SubobjectDesignator &Designator = LVal.Designator; |
8008 | |
8009 | |
8010 | |
8011 | |
8012 | |
8013 | |
8014 | |
8015 | |
8016 | |
8017 | |
8018 | |
8019 | return LVal.InvalidBase && |
8020 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
8021 | Designator.MostDerivedIsArrayElement && |
8022 | isDesignatorAtObjectEnd(Ctx, LVal); |
8023 | } |
8024 | |
8025 | |
8026 | |
8027 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
8028 | CharUnits &Result) { |
8029 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
8030 | if (Int.ugt(CharUnitsMax)) |
8031 | return false; |
8032 | Result = CharUnits::fromQuantity(Int.getZExtValue()); |
8033 | return true; |
8034 | } |
8035 | |
8036 | |
8037 | |
8038 | |
8039 | |
8040 | |
8041 | |
8042 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
8043 | unsigned Type, const LValue &LVal, |
8044 | CharUnits &EndOffset) { |
8045 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
8046 | |
8047 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
8048 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
8049 | return false; |
8050 | return HandleSizeof(Info, ExprLoc, Ty, Result); |
8051 | }; |
8052 | |
8053 | |
8054 | |
8055 | |
8056 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
8057 | |
8058 | if (Type == 3 && !DetermineForCompleteObject) |
8059 | return false; |
8060 | |
8061 | llvm::APInt APEndOffset; |
8062 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8063 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
8064 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
8065 | |
8066 | if (LVal.InvalidBase) |
8067 | return false; |
8068 | |
8069 | QualType BaseTy = getObjectType(LVal.getLValueBase()); |
8070 | return CheckedHandleSizeof(BaseTy, EndOffset); |
8071 | } |
8072 | |
8073 | |
8074 | const SubobjectDesignator &Designator = LVal.Designator; |
8075 | |
8076 | |
8077 | |
8078 | |
8079 | |
8080 | |
8081 | |
8082 | |
8083 | if (isUserWritingOffTheEnd(Info.Ctx, LVal)) { |
8084 | |
8085 | |
8086 | llvm::APInt APEndOffset; |
8087 | if (isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8088 | getBytesReturnedByAllocSizeCall(Info.Ctx, LVal, APEndOffset)) |
8089 | return convertUnsignedAPIntToCharUnits(APEndOffset, EndOffset); |
8090 | |
8091 | |
8092 | |
8093 | |
8094 | if (Type == 1) |
8095 | return false; |
8096 | } |
8097 | |
8098 | CharUnits BytesPerElem; |
8099 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
8100 | return false; |
8101 | |
8102 | |
8103 | |
8104 | |
8105 | int64_t ElemsRemaining; |
8106 | if (Designator.MostDerivedIsArrayElement && |
8107 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
8108 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
8109 | uint64_t ArrayIndex = Designator.Entries.back().ArrayIndex; |
8110 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
8111 | } else { |
8112 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
8113 | } |
8114 | |
8115 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
8116 | return true; |
8117 | } |
8118 | |
8119 | |
8120 | |
8121 | |
8122 | |
8123 | |
8124 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
8125 | EvalInfo &Info, uint64_t &Size) { |
8126 | |
8127 | LValue LVal; |
8128 | { |
8129 | |
8130 | |
8131 | |
8132 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
8133 | IgnoreSideEffectsRAII Fold(Info); |
8134 | |
8135 | if (E->isGLValue()) { |
8136 | |
8137 | |
8138 | APValue RVal; |
8139 | if (!EvaluateAsRValue(Info, E, RVal)) |
8140 | return false; |
8141 | LVal.setFrom(Info.Ctx, RVal); |
8142 | } else if (!EvaluatePointer(ignorePointerCastsAndParens(E), LVal, Info, |
8143 | )) |
8144 | return false; |
8145 | } |
8146 | |
8147 | |
8148 | |
8149 | if (LVal.getLValueOffset().isNegative()) { |
8150 | Size = 0; |
8151 | return true; |
8152 | } |
8153 | |
8154 | CharUnits EndOffset; |
8155 | if (!determineEndOffset(Info, E->getExprLoc(), Type, LVal, EndOffset)) |
8156 | return false; |
8157 | |
8158 | |
8159 | |
8160 | if (EndOffset <= LVal.getLValueOffset()) |
8161 | Size = 0; |
8162 | else |
8163 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
8164 | return true; |
8165 | } |
8166 | |
8167 | bool IntExprEvaluator::VisitConstantExpr(const ConstantExpr *E) { |
8168 | llvm::SaveAndRestore<bool> InConstantContext(Info.InConstantContext, true); |
8169 | return ExprEvaluatorBaseTy::VisitConstantExpr(E); |
8170 | } |
8171 | |
8172 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
8173 | if (unsigned BuiltinOp = E->getBuiltinCallee()) |
8174 | return VisitBuiltinCallExpr(E, BuiltinOp); |
8175 | |
8176 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8177 | } |
8178 | |
8179 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
8180 | unsigned BuiltinOp) { |
8181 | switch (unsigned BuiltinOp = E->getBuiltinCallee()) { |
8182 | default: |
8183 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8184 | |
8185 | case Builtin::BI__builtin_dynamic_object_size: |
8186 | case Builtin::BI__builtin_object_size: { |
8187 | |
8188 | unsigned Type = |
8189 | E->getArg(1)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
8190 | (0) . __assert_fail ("Type <= 3 && \"unexpected type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8190, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Type <= 3 && "unexpected type"); |
8191 | |
8192 | uint64_t Size; |
8193 | if (tryEvaluateBuiltinObjectSize(E->getArg(0), Type, Info, Size)) |
8194 | return Success(Size, E); |
8195 | |
8196 | if (E->getArg(0)->HasSideEffects(Info.Ctx)) |
8197 | return Success((Type & 2) ? 0 : -1, E); |
8198 | |
8199 | |
8200 | |
8201 | switch (Info.EvalMode) { |
8202 | case EvalInfo::EM_ConstantExpression: |
8203 | case EvalInfo::EM_PotentialConstantExpression: |
8204 | case EvalInfo::EM_ConstantFold: |
8205 | case EvalInfo::EM_EvaluateForOverflow: |
8206 | case EvalInfo::EM_IgnoreSideEffects: |
8207 | |
8208 | return Error(E); |
8209 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
8210 | case EvalInfo::EM_PotentialConstantExpressionUnevaluated: |
8211 | |
8212 | return Success((Type & 2) ? 0 : -1, E); |
8213 | } |
8214 | |
8215 | llvm_unreachable("unexpected EvalMode"); |
8216 | } |
8217 | |
8218 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
8219 | analyze_os_log::OSLogBufferLayout Layout; |
8220 | analyze_os_log::computeOSLogBufferLayout(Info.Ctx, E, Layout); |
8221 | return Success(Layout.size().getQuantity(), E); |
8222 | } |
8223 | |
8224 | case Builtin::BI__builtin_bswap16: |
8225 | case Builtin::BI__builtin_bswap32: |
8226 | case Builtin::BI__builtin_bswap64: { |
8227 | APSInt Val; |
8228 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8229 | return false; |
8230 | |
8231 | return Success(Val.byteSwap(), E); |
8232 | } |
8233 | |
8234 | case Builtin::BI__builtin_classify_type: |
8235 | return Success((int)EvaluateBuiltinClassifyType(E, Info.getLangOpts()), E); |
8236 | |
8237 | case Builtin::BI__builtin_clrsb: |
8238 | case Builtin::BI__builtin_clrsbl: |
8239 | case Builtin::BI__builtin_clrsbll: { |
8240 | APSInt Val; |
8241 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8242 | return false; |
8243 | |
8244 | return Success(Val.getBitWidth() - Val.getMinSignedBits(), E); |
8245 | } |
8246 | |
8247 | case Builtin::BI__builtin_clz: |
8248 | case Builtin::BI__builtin_clzl: |
8249 | case Builtin::BI__builtin_clzll: |
8250 | case Builtin::BI__builtin_clzs: { |
8251 | APSInt Val; |
8252 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8253 | return false; |
8254 | if (!Val) |
8255 | return Error(E); |
8256 | |
8257 | return Success(Val.countLeadingZeros(), E); |
8258 | } |
8259 | |
8260 | case Builtin::BI__builtin_constant_p: { |
8261 | auto Arg = E->getArg(0); |
8262 | if (EvaluateBuiltinConstantP(Info.Ctx, Arg)) |
8263 | return Success(true, E); |
8264 | auto ArgTy = Arg->IgnoreImplicit()->getType(); |
8265 | if (!Info.InConstantContext && !Arg->HasSideEffects(Info.Ctx) && |
8266 | !ArgTy->isAggregateType() && !ArgTy->isPointerType()) { |
8267 | |
8268 | |
8269 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
8270 | return false; |
8271 | } |
8272 | return Success(false, E); |
8273 | } |
8274 | |
8275 | case Builtin::BI__builtin_ctz: |
8276 | case Builtin::BI__builtin_ctzl: |
8277 | case Builtin::BI__builtin_ctzll: |
8278 | case Builtin::BI__builtin_ctzs: { |
8279 | APSInt Val; |
8280 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8281 | return false; |
8282 | if (!Val) |
8283 | return Error(E); |
8284 | |
8285 | return Success(Val.countTrailingZeros(), E); |
8286 | } |
8287 | |
8288 | case Builtin::BI__builtin_eh_return_data_regno: { |
8289 | int Operand = E->getArg(0)->EvaluateKnownConstInt(Info.Ctx).getZExtValue(); |
8290 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(Operand); |
8291 | return Success(Operand, E); |
8292 | } |
8293 | |
8294 | case Builtin::BI__builtin_expect: |
8295 | return Visit(E->getArg(0)); |
8296 | |
8297 | case Builtin::BI__builtin_ffs: |
8298 | case Builtin::BI__builtin_ffsl: |
8299 | case Builtin::BI__builtin_ffsll: { |
8300 | APSInt Val; |
8301 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8302 | return false; |
8303 | |
8304 | unsigned N = Val.countTrailingZeros(); |
8305 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
8306 | } |
8307 | |
8308 | case Builtin::BI__builtin_fpclassify: { |
8309 | APFloat Val(0.0); |
8310 | if (!EvaluateFloat(E->getArg(5), Val, Info)) |
8311 | return false; |
8312 | unsigned Arg; |
8313 | switch (Val.getCategory()) { |
8314 | case APFloat::fcNaN: Arg = 0; break; |
8315 | case APFloat::fcInfinity: Arg = 1; break; |
8316 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
8317 | case APFloat::fcZero: Arg = 4; break; |
8318 | } |
8319 | return Visit(E->getArg(Arg)); |
8320 | } |
8321 | |
8322 | case Builtin::BI__builtin_isinf_sign: { |
8323 | APFloat Val(0.0); |
8324 | return EvaluateFloat(E->getArg(0), Val, Info) && |
8325 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
8326 | } |
8327 | |
8328 | case Builtin::BI__builtin_isinf: { |
8329 | APFloat Val(0.0); |
8330 | return EvaluateFloat(E->getArg(0), Val, Info) && |
8331 | Success(Val.isInfinity() ? 1 : 0, E); |
8332 | } |
8333 | |
8334 | case Builtin::BI__builtin_isfinite: { |
8335 | APFloat Val(0.0); |
8336 | return EvaluateFloat(E->getArg(0), Val, Info) && |
8337 | Success(Val.isFinite() ? 1 : 0, E); |
8338 | } |
8339 | |
8340 | case Builtin::BI__builtin_isnan: { |
8341 | APFloat Val(0.0); |
8342 | return EvaluateFloat(E->getArg(0), Val, Info) && |
8343 | Success(Val.isNaN() ? 1 : 0, E); |
8344 | } |
8345 | |
8346 | case Builtin::BI__builtin_isnormal: { |
8347 | APFloat Val(0.0); |
8348 | return EvaluateFloat(E->getArg(0), Val, Info) && |
8349 | Success(Val.isNormal() ? 1 : 0, E); |
8350 | } |
8351 | |
8352 | case Builtin::BI__builtin_parity: |
8353 | case Builtin::BI__builtin_parityl: |
8354 | case Builtin::BI__builtin_parityll: { |
8355 | APSInt Val; |
8356 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8357 | return false; |
8358 | |
8359 | return Success(Val.countPopulation() % 2, E); |
8360 | } |
8361 | |
8362 | case Builtin::BI__builtin_popcount: |
8363 | case Builtin::BI__builtin_popcountl: |
8364 | case Builtin::BI__builtin_popcountll: { |
8365 | APSInt Val; |
8366 | if (!EvaluateInteger(E->getArg(0), Val, Info)) |
8367 | return false; |
8368 | |
8369 | return Success(Val.countPopulation(), E); |
8370 | } |
8371 | |
8372 | case Builtin::BIstrlen: |
8373 | case Builtin::BIwcslen: |
8374 | |
8375 | if (Info.getLangOpts().CPlusPlus11) |
8376 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
8377 | << << |
8378 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
8379 | else |
8380 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
8381 | LLVM_FALLTHROUGH; |
8382 | case Builtin::BI__builtin_strlen: |
8383 | case Builtin::BI__builtin_wcslen: { |
8384 | |
8385 | |
8386 | LValue String; |
8387 | if (!EvaluatePointer(E->getArg(0), String, Info)) |
8388 | return false; |
8389 | |
8390 | QualType CharTy = E->getArg(0)->getType()->getPointeeType(); |
8391 | |
8392 | |
8393 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
8394 | String.getLValueBase().dyn_cast<const Expr *>())) { |
8395 | |
8396 | |
8397 | StringRef Str = S->getBytes(); |
8398 | int64_t Off = String.Offset.getQuantity(); |
8399 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
8400 | S->getCharByteWidth() == 1 && |
8401 | |
8402 | Info.Ctx.hasSameUnqualifiedType(CharTy, Info.Ctx.CharTy)) { |
8403 | Str = Str.substr(Off); |
8404 | |
8405 | StringRef::size_type Pos = Str.find(0); |
8406 | if (Pos != StringRef::npos) |
8407 | Str = Str.substr(0, Pos); |
8408 | |
8409 | return Success(Str.size(), E); |
8410 | } |
8411 | |
8412 | |
8413 | } |
8414 | |
8415 | |
8416 | for (uint64_t Strlen = 0; ; ++Strlen) { |
8417 | APValue Char; |
8418 | if (!handleLValueToRValueConversion(Info, E, CharTy, String, Char) || |
8419 | !Char.isInt()) |
8420 | return false; |
8421 | if (!Char.getInt()) |
8422 | return Success(Strlen, E); |
8423 | if (!HandleLValueArrayAdjustment(Info, E, String, CharTy, 1)) |
8424 | return false; |
8425 | } |
8426 | } |
8427 | |
8428 | case Builtin::BIstrcmp: |
8429 | case Builtin::BIwcscmp: |
8430 | case Builtin::BIstrncmp: |
8431 | case Builtin::BIwcsncmp: |
8432 | case Builtin::BImemcmp: |
8433 | case Builtin::BIbcmp: |
8434 | case Builtin::BIwmemcmp: |
8435 | |
8436 | if (Info.getLangOpts().CPlusPlus11) |
8437 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
8438 | << << |
8439 | << (std::string("'") + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'"); |
8440 | else |
8441 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
8442 | LLVM_FALLTHROUGH; |
8443 | case Builtin::BI__builtin_strcmp: |
8444 | case Builtin::BI__builtin_wcscmp: |
8445 | case Builtin::BI__builtin_strncmp: |
8446 | case Builtin::BI__builtin_wcsncmp: |
8447 | case Builtin::BI__builtin_memcmp: |
8448 | case Builtin::BI__builtin_bcmp: |
8449 | case Builtin::BI__builtin_wmemcmp: { |
8450 | LValue String1, String2; |
8451 | if (!EvaluatePointer(E->getArg(0), String1, Info) || |
8452 | !EvaluatePointer(E->getArg(1), String2, Info)) |
8453 | return false; |
8454 | |
8455 | uint64_t MaxLength = uint64_t(-1); |
8456 | if (BuiltinOp != Builtin::BIstrcmp && |
8457 | BuiltinOp != Builtin::BIwcscmp && |
8458 | BuiltinOp != Builtin::BI__builtin_strcmp && |
8459 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
8460 | APSInt N; |
8461 | if (!EvaluateInteger(E->getArg(2), N, Info)) |
8462 | return false; |
8463 | MaxLength = N.getExtValue(); |
8464 | } |
8465 | |
8466 | |
8467 | if (MaxLength == 0u) |
8468 | return Success(0, E); |
8469 | |
8470 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
8471 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
8472 | String1.Designator.Invalid || String2.Designator.Invalid) |
8473 | return false; |
8474 | |
8475 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
8476 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
8477 | |
8478 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
8479 | BuiltinOp == Builtin::BIbcmp || |
8480 | BuiltinOp == Builtin::BI__builtin_memcmp || |
8481 | BuiltinOp == Builtin::BI__builtin_bcmp; |
8482 | |
8483 | getArg(0)->getType()->getPointeeType()) && Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8486, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsRawByte || |
8484 | getArg(0)->getType()->getPointeeType()) && Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8486, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> (Info.Ctx.hasSameUnqualifiedType( |
8485 | getArg(0)->getType()->getPointeeType()) && Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8486, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
8486 | getArg(0)->getType()->getPointeeType()) && Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8486, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
8487 | |
8488 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
8489 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
8490 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
8491 | Char1.isInt() && Char2.isInt(); |
8492 | }; |
8493 | const auto &AdvanceElems = [&] { |
8494 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
8495 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
8496 | }; |
8497 | |
8498 | if (IsRawByte) { |
8499 | uint64_t BytesRemaining = MaxLength; |
8500 | |
8501 | if (CharTy1->isIncompleteType()) { |
8502 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy1; |
8503 | return false; |
8504 | } |
8505 | if (CharTy2->isIncompleteType()) { |
8506 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy2; |
8507 | return false; |
8508 | } |
8509 | uint64_t CharTy1Width{Info.Ctx.getTypeSize(CharTy1)}; |
8510 | CharUnits CharTy1Size = Info.Ctx.toCharUnitsFromBits(CharTy1Width); |
8511 | |
8512 | if (CharTy1Size != Info.Ctx.getTypeSizeInChars(CharTy2)) |
8513 | return false; |
8514 | uint64_t BytesPerElement = CharTy1Size.getQuantity(); |
8515 | (0) . __assert_fail ("BytesRemaining && \"BytesRemaining should not be zero. the \" \"following loop considers at least one element\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8516, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BytesRemaining && "BytesRemaining should not be zero: the " |
8516 | (0) . __assert_fail ("BytesRemaining && \"BytesRemaining should not be zero. the \" \"following loop considers at least one element\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8516, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "following loop considers at least one element"); |
8517 | while (true) { |
8518 | APValue Char1, Char2; |
8519 | if (!ReadCurElems(Char1, Char2)) |
8520 | return false; |
8521 | |
8522 | |
8523 | |
8524 | |
8525 | |
8526 | APSInt Char1InMem = Char1.getInt().extOrTrunc(CharTy1Width); |
8527 | APSInt Char2InMem = Char2.getInt().extOrTrunc(CharTy1Width); |
8528 | if (Char1InMem.ne(Char2InMem)) { |
8529 | |
8530 | |
8531 | if (BytesPerElement == 1u) { |
8532 | |
8533 | return Success(Char1InMem.ult(Char2InMem) ? -1 : 1, E); |
8534 | } |
8535 | |
8536 | |
8537 | return false; |
8538 | } |
8539 | if (!AdvanceElems()) |
8540 | return false; |
8541 | if (BytesRemaining <= BytesPerElement) |
8542 | break; |
8543 | BytesRemaining -= BytesPerElement; |
8544 | } |
8545 | |
8546 | return Success(0, E); |
8547 | } |
8548 | |
8549 | bool StopAtNull = |
8550 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
8551 | BuiltinOp != Builtin::BIwmemcmp && |
8552 | BuiltinOp != Builtin::BI__builtin_memcmp && |
8553 | BuiltinOp != Builtin::BI__builtin_bcmp && |
8554 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
8555 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
8556 | BuiltinOp == Builtin::BIwcsncmp || |
8557 | BuiltinOp == Builtin::BIwmemcmp || |
8558 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
8559 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
8560 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
8561 | |
8562 | for (; MaxLength; --MaxLength) { |
8563 | APValue Char1, Char2; |
8564 | if (!ReadCurElems(Char1, Char2)) |
8565 | return false; |
8566 | if (Char1.getInt() != Char2.getInt()) { |
8567 | if (IsWide) |
8568 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
8569 | |
8570 | return Success(Char1.getInt().ult(Char2.getInt()) ? -1 : 1, E); |
8571 | } |
8572 | if (StopAtNull && !Char1.getInt()) |
8573 | return Success(0, E); |
8574 | assert(!(StopAtNull && !Char2.getInt())); |
8575 | if (!AdvanceElems()) |
8576 | return false; |
8577 | } |
8578 | |
8579 | return Success(0, E); |
8580 | } |
8581 | |
8582 | case Builtin::BI__atomic_always_lock_free: |
8583 | case Builtin::BI__atomic_is_lock_free: |
8584 | case Builtin::BI__c11_atomic_is_lock_free: { |
8585 | APSInt SizeVal; |
8586 | if (!EvaluateInteger(E->getArg(0), SizeVal, Info)) |
8587 | return false; |
8588 | |
8589 | |
8590 | |
8591 | |
8592 | |
8593 | |
8594 | |
8595 | |
8596 | |
8597 | |
8598 | |
8599 | CharUnits Size = CharUnits::fromQuantity(SizeVal.getZExtValue()); |
8600 | if (Size.isPowerOfTwo()) { |
8601 | |
8602 | unsigned InlineWidthBits = |
8603 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
8604 | if (Size <= Info.Ctx.toCharUnitsFromBits(InlineWidthBits)) { |
8605 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
8606 | Size == CharUnits::One() || |
8607 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
8608 | Expr::NPC_NeverValueDependent)) |
8609 | |
8610 | |
8611 | return Success(1, E); |
8612 | |
8613 | QualType PointeeType = E->getArg(1)->IgnoreImpCasts()->getType()-> |
8614 | castAs<PointerType>()->getPointeeType(); |
8615 | if (!PointeeType->isIncompleteType() && |
8616 | Info.Ctx.getTypeAlignInChars(PointeeType) >= Size) { |
8617 | |
8618 | return Success(1, E); |
8619 | } |
8620 | } |
8621 | } |
8622 | |
8623 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
8624 | Success(0, E) : Error(E); |
8625 | } |
8626 | case Builtin::BIomp_is_initial_device: |
8627 | |
8628 | return Success(Info.getLangOpts().OpenMPIsDevice ? 0 : 1, E); |
8629 | case Builtin::BI__builtin_add_overflow: |
8630 | case Builtin::BI__builtin_sub_overflow: |
8631 | case Builtin::BI__builtin_mul_overflow: |
8632 | case Builtin::BI__builtin_sadd_overflow: |
8633 | case Builtin::BI__builtin_uadd_overflow: |
8634 | case Builtin::BI__builtin_uaddl_overflow: |
8635 | case Builtin::BI__builtin_uaddll_overflow: |
8636 | case Builtin::BI__builtin_usub_overflow: |
8637 | case Builtin::BI__builtin_usubl_overflow: |
8638 | case Builtin::BI__builtin_usubll_overflow: |
8639 | case Builtin::BI__builtin_umul_overflow: |
8640 | case Builtin::BI__builtin_umull_overflow: |
8641 | case Builtin::BI__builtin_umulll_overflow: |
8642 | case Builtin::BI__builtin_saddl_overflow: |
8643 | case Builtin::BI__builtin_saddll_overflow: |
8644 | case Builtin::BI__builtin_ssub_overflow: |
8645 | case Builtin::BI__builtin_ssubl_overflow: |
8646 | case Builtin::BI__builtin_ssubll_overflow: |
8647 | case Builtin::BI__builtin_smul_overflow: |
8648 | case Builtin::BI__builtin_smull_overflow: |
8649 | case Builtin::BI__builtin_smulll_overflow: { |
8650 | LValue ResultLValue; |
8651 | APSInt LHS, RHS; |
8652 | |
8653 | QualType ResultType = E->getArg(2)->getType()->getPointeeType(); |
8654 | if (!EvaluateInteger(E->getArg(0), LHS, Info) || |
8655 | !EvaluateInteger(E->getArg(1), RHS, Info) || |
8656 | !EvaluatePointer(E->getArg(2), ResultLValue, Info)) |
8657 | return false; |
8658 | |
8659 | APSInt Result; |
8660 | bool DidOverflow = false; |
8661 | |
8662 | |
8663 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
8664 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
8665 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
8666 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
8667 | ResultType->isSignedIntegerOrEnumerationType(); |
8668 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
8669 | ResultType->isSignedIntegerOrEnumerationType(); |
8670 | uint64_t LHSSize = LHS.getBitWidth(); |
8671 | uint64_t RHSSize = RHS.getBitWidth(); |
8672 | uint64_t ResultSize = Info.Ctx.getTypeSize(ResultType); |
8673 | uint64_t MaxBits = std::max(std::max(LHSSize, RHSSize), ResultSize); |
8674 | |
8675 | |
8676 | |
8677 | |
8678 | |
8679 | if (IsSigned && !AllSigned) |
8680 | ++MaxBits; |
8681 | |
8682 | LHS = APSInt(IsSigned ? LHS.sextOrSelf(MaxBits) : LHS.zextOrSelf(MaxBits), |
8683 | !IsSigned); |
8684 | RHS = APSInt(IsSigned ? RHS.sextOrSelf(MaxBits) : RHS.zextOrSelf(MaxBits), |
8685 | !IsSigned); |
8686 | Result = APSInt(MaxBits, !IsSigned); |
8687 | } |
8688 | |
8689 | |
8690 | switch (BuiltinOp) { |
8691 | default: |
8692 | llvm_unreachable("Invalid value for BuiltinOp"); |
8693 | case Builtin::BI__builtin_add_overflow: |
8694 | case Builtin::BI__builtin_sadd_overflow: |
8695 | case Builtin::BI__builtin_saddl_overflow: |
8696 | case Builtin::BI__builtin_saddll_overflow: |
8697 | case Builtin::BI__builtin_uadd_overflow: |
8698 | case Builtin::BI__builtin_uaddl_overflow: |
8699 | case Builtin::BI__builtin_uaddll_overflow: |
8700 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, DidOverflow) |
8701 | : LHS.uadd_ov(RHS, DidOverflow); |
8702 | break; |
8703 | case Builtin::BI__builtin_sub_overflow: |
8704 | case Builtin::BI__builtin_ssub_overflow: |
8705 | case Builtin::BI__builtin_ssubl_overflow: |
8706 | case Builtin::BI__builtin_ssubll_overflow: |
8707 | case Builtin::BI__builtin_usub_overflow: |
8708 | case Builtin::BI__builtin_usubl_overflow: |
8709 | case Builtin::BI__builtin_usubll_overflow: |
8710 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, DidOverflow) |
8711 | : LHS.usub_ov(RHS, DidOverflow); |
8712 | break; |
8713 | case Builtin::BI__builtin_mul_overflow: |
8714 | case Builtin::BI__builtin_smul_overflow: |
8715 | case Builtin::BI__builtin_smull_overflow: |
8716 | case Builtin::BI__builtin_smulll_overflow: |
8717 | case Builtin::BI__builtin_umul_overflow: |
8718 | case Builtin::BI__builtin_umull_overflow: |
8719 | case Builtin::BI__builtin_umulll_overflow: |
8720 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, DidOverflow) |
8721 | : LHS.umul_ov(RHS, DidOverflow); |
8722 | break; |
8723 | } |
8724 | |
8725 | |
8726 | |
8727 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
8728 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
8729 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
8730 | |
8731 | |
8732 | |
8733 | |
8734 | |
8735 | APSInt Temp = Result.extOrTrunc(Info.Ctx.getTypeSize(ResultType)); |
8736 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
8737 | |
8738 | if (!APSInt::isSameValue(Temp, Result)) |
8739 | DidOverflow = true; |
8740 | Result = Temp; |
8741 | } |
8742 | |
8743 | APValue APV{Result}; |
8744 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
8745 | return false; |
8746 | return Success(DidOverflow, E); |
8747 | } |
8748 | } |
8749 | } |
8750 | |
8751 | |
8752 | |
8753 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
8754 | const LValue &LV) { |
8755 | |
8756 | |
8757 | if (!LV.getLValueBase()) |
8758 | return false; |
8759 | |
8760 | |
8761 | |
8762 | if (!LV.getLValueDesignator().Invalid && |
8763 | !LV.getLValueDesignator().isOnePastTheEnd()) |
8764 | return false; |
8765 | |
8766 | |
8767 | |
8768 | QualType Ty = getType(LV.getLValueBase()); |
8769 | if (Ty->isIncompleteType()) |
8770 | return true; |
8771 | |
8772 | |
8773 | |
8774 | auto Size = Ctx.getTypeSizeInChars(Ty); |
8775 | return LV.getLValueOffset() == Size; |
8776 | } |
8777 | |
8778 | namespace { |
8779 | |
8780 | |
8781 | |
8782 | |
8783 | |
8784 | |
8785 | class DataRecursiveIntBinOpEvaluator { |
8786 | struct EvalResult { |
8787 | APValue Val; |
8788 | bool Failed; |
8789 | |
8790 | EvalResult() : Failed(false) { } |
8791 | |
8792 | void swap(EvalResult &RHS) { |
8793 | Val.swap(RHS.Val); |
8794 | Failed = RHS.Failed; |
8795 | RHS.Failed = false; |
8796 | } |
8797 | }; |
8798 | |
8799 | struct Job { |
8800 | const Expr *E; |
8801 | EvalResult LHSResult; |
8802 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
8803 | |
8804 | Job() = default; |
8805 | Job(Job &&) = default; |
8806 | |
8807 | void startSpeculativeEval(EvalInfo &Info) { |
8808 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
8809 | } |
8810 | |
8811 | private: |
8812 | SpeculativeEvaluationRAII SpecEvalRAII; |
8813 | }; |
8814 | |
8815 | SmallVector<Job, 16> Queue; |
8816 | |
8817 | IntExprEvaluator &IntEval; |
8818 | EvalInfo &Info; |
8819 | APValue &FinalResult; |
8820 | |
8821 | public: |
8822 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
8823 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
8824 | |
8825 | |
8826 | |
8827 | |
8828 | |
8829 | static bool shouldEnqueue(const BinaryOperator *E) { |
8830 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
8831 | (E->isRValue() && E->getType()->isIntegralOrEnumerationType() && |
8832 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
8833 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
8834 | } |
8835 | |
8836 | bool Traverse(const BinaryOperator *E) { |
8837 | enqueue(E); |
8838 | EvalResult PrevResult; |
8839 | while (!Queue.empty()) |
8840 | process(PrevResult); |
8841 | |
8842 | if (PrevResult.Failed) return false; |
8843 | |
8844 | FinalResult.swap(PrevResult.Val); |
8845 | return true; |
8846 | } |
8847 | |
8848 | private: |
8849 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
8850 | return IntEval.Success(Value, E, Result); |
8851 | } |
8852 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
8853 | return IntEval.Success(Value, E, Result); |
8854 | } |
8855 | bool Error(const Expr *E) { |
8856 | return IntEval.Error(E); |
8857 | } |
8858 | bool Error(const Expr *E, diag::kind D) { |
8859 | return IntEval.Error(E, D); |
8860 | } |
8861 | |
8862 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
8863 | return Info.CCEDiag(E, D); |
8864 | } |
8865 | |
8866 | |
8867 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
8868 | bool &SuppressRHSDiags); |
8869 | |
8870 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
8871 | const BinaryOperator *E, APValue &Result); |
8872 | |
8873 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
8874 | Result.Failed = !Evaluate(Result.Val, Info, E); |
8875 | if (Result.Failed) |
8876 | Result.Val = APValue(); |
8877 | } |
8878 | |
8879 | void process(EvalResult &Result); |
8880 | |
8881 | void enqueue(const Expr *E) { |
8882 | E = E->IgnoreParens(); |
8883 | Queue.resize(Queue.size()+1); |
8884 | Queue.back().E = E; |
8885 | Queue.back().Kind = Job::AnyExprKind; |
8886 | } |
8887 | }; |
8888 | |
8889 | } |
8890 | |
8891 | bool DataRecursiveIntBinOpEvaluator:: |
8892 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
8893 | bool &SuppressRHSDiags) { |
8894 | if (E->getOpcode() == BO_Comma) { |
8895 | |
8896 | if (LHSResult.Failed) |
8897 | return Info.noteSideEffect(); |
8898 | return true; |
8899 | } |
8900 | |
8901 | if (E->isLogicalOp()) { |
8902 | bool LHSAsBool; |
8903 | if (!LHSResult.Failed && HandleConversionToBool(LHSResult.Val, LHSAsBool)) { |
8904 | |
8905 | |
8906 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
8907 | Success(LHSAsBool, E, LHSResult.Val); |
8908 | return false; |
8909 | } |
8910 | } else { |
8911 | LHSResult.Failed = true; |
8912 | |
8913 | |
8914 | |
8915 | if (!Info.noteSideEffect()) |
8916 | return false; |
8917 | |
8918 | |
8919 | |
8920 | |
8921 | SuppressRHSDiags = true; |
8922 | } |
8923 | |
8924 | return true; |
8925 | } |
8926 | |
8927 | getLHS()->getType()->isIntegralOrEnumerationType() && E->getRHS()->getType()->isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8928, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
8928 | getLHS()->getType()->isIntegralOrEnumerationType() && E->getRHS()->getType()->isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8928, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->getRHS()->getType()->isIntegralOrEnumerationType()); |
8929 | |
8930 | if (LHSResult.Failed && !Info.noteFailure()) |
8931 | return false; |
8932 | |
8933 | return true; |
8934 | } |
8935 | |
8936 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
8937 | bool IsSub) { |
8938 | |
8939 | |
8940 | |
8941 | (0) . __assert_fail ("!LVal.hasLValuePath() && \"have designator for integer lvalue\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8941, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!LVal.hasLValuePath() && "have designator for integer lvalue"); |
8942 | CharUnits &Offset = LVal.getLValueOffset(); |
8943 | uint64_t Offset64 = Offset.getQuantity(); |
8944 | uint64_t Index64 = Index.extOrTrunc(64).getZExtValue(); |
8945 | Offset = CharUnits::fromQuantity(IsSub ? Offset64 - Index64 |
8946 | : Offset64 + Index64); |
8947 | } |
8948 | |
8949 | bool DataRecursiveIntBinOpEvaluator:: |
8950 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
8951 | const BinaryOperator *E, APValue &Result) { |
8952 | if (E->getOpcode() == BO_Comma) { |
8953 | if (RHSResult.Failed) |
8954 | return false; |
8955 | Result = RHSResult.Val; |
8956 | return true; |
8957 | } |
8958 | |
8959 | if (E->isLogicalOp()) { |
8960 | bool lhsResult, rhsResult; |
8961 | bool LHSIsOK = HandleConversionToBool(LHSResult.Val, lhsResult); |
8962 | bool RHSIsOK = HandleConversionToBool(RHSResult.Val, rhsResult); |
8963 | |
8964 | if (LHSIsOK) { |
8965 | if (RHSIsOK) { |
8966 | if (E->getOpcode() == BO_LOr) |
8967 | return Success(lhsResult || rhsResult, E, Result); |
8968 | else |
8969 | return Success(lhsResult && rhsResult, E, Result); |
8970 | } |
8971 | } else { |
8972 | if (RHSIsOK) { |
8973 | |
8974 | |
8975 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
8976 | return Success(rhsResult, E, Result); |
8977 | } |
8978 | } |
8979 | |
8980 | return false; |
8981 | } |
8982 | |
8983 | getLHS()->getType()->isIntegralOrEnumerationType() && E->getRHS()->getType()->isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8984, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
8984 | getLHS()->getType()->isIntegralOrEnumerationType() && E->getRHS()->getType()->isIntegralOrEnumerationType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 8984, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->getRHS()->getType()->isIntegralOrEnumerationType()); |
8985 | |
8986 | if (LHSResult.Failed || RHSResult.Failed) |
8987 | return false; |
8988 | |
8989 | const APValue &LHSVal = LHSResult.Val; |
8990 | const APValue &RHSVal = RHSResult.Val; |
8991 | |
8992 | |
8993 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
8994 | Result = LHSVal; |
8995 | addOrSubLValueAsInteger(Result, RHSVal.getInt(), E->getOpcode() == BO_Sub); |
8996 | return true; |
8997 | } |
8998 | |
8999 | |
9000 | if (E->getOpcode() == BO_Add && |
9001 | RHSVal.isLValue() && LHSVal.isInt()) { |
9002 | Result = RHSVal; |
9003 | addOrSubLValueAsInteger(Result, LHSVal.getInt(), ); |
9004 | return true; |
9005 | } |
9006 | |
9007 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
9008 | |
9009 | if (!LHSVal.getLValueOffset().isZero() || |
9010 | !RHSVal.getLValueOffset().isZero()) |
9011 | return false; |
9012 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
9013 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
9014 | if (!LHSExpr || !RHSExpr) |
9015 | return false; |
9016 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
9017 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
9018 | if (!LHSAddrExpr || !RHSAddrExpr) |
9019 | return false; |
9020 | |
9021 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
9022 | RHSAddrExpr->getLabel()->getDeclContext()) |
9023 | return false; |
9024 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
9025 | return true; |
9026 | } |
9027 | |
9028 | |
9029 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
9030 | return Error(E); |
9031 | |
9032 | |
9033 | |
9034 | |
9035 | APSInt Value(Info.Ctx.getIntWidth(E->getType()), |
9036 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
9037 | if (!handleIntIntBinOp(Info, E, LHSVal.getInt(), E->getOpcode(), |
9038 | RHSVal.getInt(), Value)) |
9039 | return false; |
9040 | return Success(Value, E, Result); |
9041 | } |
9042 | |
9043 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
9044 | Job &job = Queue.back(); |
9045 | |
9046 | switch (job.Kind) { |
9047 | case Job::AnyExprKind: { |
9048 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(job.E)) { |
9049 | if (shouldEnqueue(Bop)) { |
9050 | job.Kind = Job::BinOpKind; |
9051 | enqueue(Bop->getLHS()); |
9052 | return; |
9053 | } |
9054 | } |
9055 | |
9056 | EvaluateExpr(job.E, Result); |
9057 | Queue.pop_back(); |
9058 | return; |
9059 | } |
9060 | |
9061 | case Job::BinOpKind: { |
9062 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
9063 | bool SuppressRHSDiags = false; |
9064 | if (!VisitBinOpLHSOnly(Result, Bop, SuppressRHSDiags)) { |
9065 | Queue.pop_back(); |
9066 | return; |
9067 | } |
9068 | if (SuppressRHSDiags) |
9069 | job.startSpeculativeEval(Info); |
9070 | job.LHSResult.swap(Result); |
9071 | job.Kind = Job::BinOpVisitedLHSKind; |
9072 | enqueue(Bop->getRHS()); |
9073 | return; |
9074 | } |
9075 | |
9076 | case Job::BinOpVisitedLHSKind: { |
9077 | const BinaryOperator *Bop = cast<BinaryOperator>(job.E); |
9078 | EvalResult RHS; |
9079 | RHS.swap(Result); |
9080 | Result.Failed = !VisitBinOp(job.LHSResult, RHS, Bop, Result.Val); |
9081 | Queue.pop_back(); |
9082 | return; |
9083 | } |
9084 | } |
9085 | |
9086 | llvm_unreachable("Invalid Job::Kind!"); |
9087 | } |
9088 | |
9089 | namespace { |
9090 | |
9091 | |
9092 | class DelayedNoteFailureRAII { |
9093 | EvalInfo &Info; |
9094 | bool NoteFailure; |
9095 | |
9096 | public: |
9097 | DelayedNoteFailureRAII(EvalInfo &Info, bool NoteFailure = true) |
9098 | : Info(Info), NoteFailure(NoteFailure) {} |
9099 | ~DelayedNoteFailureRAII() { |
9100 | if (NoteFailure) { |
9101 | bool ContinueAfterFailure = Info.noteFailure(); |
9102 | (void)ContinueAfterFailure; |
9103 | (0) . __assert_fail ("ContinueAfterFailure && \"Shouldn't have kept evaluating on failure.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9104, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ContinueAfterFailure && |
9104 | (0) . __assert_fail ("ContinueAfterFailure && \"Shouldn't have kept evaluating on failure.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9104, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Shouldn't have kept evaluating on failure."); |
9105 | } |
9106 | } |
9107 | }; |
9108 | } |
9109 | |
9110 | template <class SuccessCB, class AfterCB> |
9111 | static bool |
9112 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
9113 | SuccessCB &&Success, AfterCB &&DoAfter) { |
9114 | (0) . __assert_fail ("E->isComparisonOp() && \"expected comparison operator\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9114, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isComparisonOp() && "expected comparison operator"); |
9115 | (0) . __assert_fail ("(E->getOpcode() == BO_Cmp || E->getType()->isIntegralOrEnumerationType()) && \"unsupported binary expression evaluation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9117, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((E->getOpcode() == BO_Cmp || |
9116 | (0) . __assert_fail ("(E->getOpcode() == BO_Cmp || E->getType()->isIntegralOrEnumerationType()) && \"unsupported binary expression evaluation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9117, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> E->getType()->isIntegralOrEnumerationType()) && |
9117 | (0) . __assert_fail ("(E->getOpcode() == BO_Cmp || E->getType()->isIntegralOrEnumerationType()) && \"unsupported binary expression evaluation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9117, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "unsupported binary expression evaluation"); |
9118 | auto Error = [&](const Expr *E) { |
9119 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9120 | return false; |
9121 | }; |
9122 | |
9123 | using CCR = ComparisonCategoryResult; |
9124 | bool IsRelational = E->isRelationalOp(); |
9125 | bool IsEquality = E->isEqualityOp(); |
9126 | if (E->getOpcode() == BO_Cmp) { |
9127 | const ComparisonCategoryInfo &CmpInfo = |
9128 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
9129 | IsRelational = CmpInfo.isOrdered(); |
9130 | IsEquality = CmpInfo.isEquality(); |
9131 | } |
9132 | |
9133 | QualType LHSTy = E->getLHS()->getType(); |
9134 | QualType RHSTy = E->getRHS()->getType(); |
9135 | |
9136 | if (LHSTy->isIntegralOrEnumerationType() && |
9137 | RHSTy->isIntegralOrEnumerationType()) { |
9138 | APSInt LHS, RHS; |
9139 | bool LHSOK = EvaluateInteger(E->getLHS(), LHS, Info); |
9140 | if (!LHSOK && !Info.noteFailure()) |
9141 | return false; |
9142 | if (!EvaluateInteger(E->getRHS(), RHS, Info) || !LHSOK) |
9143 | return false; |
9144 | if (LHS < RHS) |
9145 | return Success(CCR::Less, E); |
9146 | if (LHS > RHS) |
9147 | return Success(CCR::Greater, E); |
9148 | return Success(CCR::Equal, E); |
9149 | } |
9150 | |
9151 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
9152 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHSTy)); |
9153 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHSTy)); |
9154 | |
9155 | bool LHSOK = EvaluateFixedPointOrInteger(E->getLHS(), LHSFX, Info); |
9156 | if (!LHSOK && !Info.noteFailure()) |
9157 | return false; |
9158 | if (!EvaluateFixedPointOrInteger(E->getRHS(), RHSFX, Info) || !LHSOK) |
9159 | return false; |
9160 | if (LHSFX < RHSFX) |
9161 | return Success(CCR::Less, E); |
9162 | if (LHSFX > RHSFX) |
9163 | return Success(CCR::Greater, E); |
9164 | return Success(CCR::Equal, E); |
9165 | } |
9166 | |
9167 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
9168 | ComplexValue LHS, RHS; |
9169 | bool LHSOK; |
9170 | if (E->isAssignmentOp()) { |
9171 | LValue LV; |
9172 | EvaluateLValue(E->getLHS(), LV, Info); |
9173 | LHSOK = false; |
9174 | } else if (LHSTy->isRealFloatingType()) { |
9175 | LHSOK = EvaluateFloat(E->getLHS(), LHS.FloatReal, Info); |
9176 | if (LHSOK) { |
9177 | LHS.makeComplexFloat(); |
9178 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
9179 | } |
9180 | } else { |
9181 | LHSOK = EvaluateComplex(E->getLHS(), LHS, Info); |
9182 | } |
9183 | if (!LHSOK && !Info.noteFailure()) |
9184 | return false; |
9185 | |
9186 | if (E->getRHS()->getType()->isRealFloatingType()) { |
9187 | if (!EvaluateFloat(E->getRHS(), RHS.FloatReal, Info) || !LHSOK) |
9188 | return false; |
9189 | RHS.makeComplexFloat(); |
9190 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
9191 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
9192 | return false; |
9193 | |
9194 | if (LHS.isComplexFloat()) { |
9195 | APFloat::cmpResult CR_r = |
9196 | LHS.getComplexFloatReal().compare(RHS.getComplexFloatReal()); |
9197 | APFloat::cmpResult CR_i = |
9198 | LHS.getComplexFloatImag().compare(RHS.getComplexFloatImag()); |
9199 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
9200 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
9201 | } else { |
9202 | (0) . __assert_fail ("IsEquality && \"invalid complex comparison\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9202, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsEquality && "invalid complex comparison"); |
9203 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
9204 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
9205 | return Success(IsEqual ? CCR::Equal : CCR::Nonequal, E); |
9206 | } |
9207 | } |
9208 | |
9209 | if (LHSTy->isRealFloatingType() && |
9210 | RHSTy->isRealFloatingType()) { |
9211 | APFloat RHS(0.0), LHS(0.0); |
9212 | |
9213 | bool LHSOK = EvaluateFloat(E->getRHS(), RHS, Info); |
9214 | if (!LHSOK && !Info.noteFailure()) |
9215 | return false; |
9216 | |
9217 | if (!EvaluateFloat(E->getLHS(), LHS, Info) || !LHSOK) |
9218 | return false; |
9219 | |
9220 | (0) . __assert_fail ("E->isComparisonOp() && \"Invalid binary operator!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9220, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isComparisonOp() && "Invalid binary operator!"); |
9221 | auto GetCmpRes = [&]() { |
9222 | switch (LHS.compare(RHS)) { |
9223 | case APFloat::cmpEqual: |
9224 | return CCR::Equal; |
9225 | case APFloat::cmpLessThan: |
9226 | return CCR::Less; |
9227 | case APFloat::cmpGreaterThan: |
9228 | return CCR::Greater; |
9229 | case APFloat::cmpUnordered: |
9230 | return CCR::Unordered; |
9231 | } |
9232 | llvm_unreachable("Unrecognised APFloat::cmpResult enum"); |
9233 | }; |
9234 | return Success(GetCmpRes(), E); |
9235 | } |
9236 | |
9237 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
9238 | LValue LHSValue, RHSValue; |
9239 | |
9240 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
9241 | if (!LHSOK && !Info.noteFailure()) |
9242 | return false; |
9243 | |
9244 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
9245 | return false; |
9246 | |
9247 | |
9248 | |
9249 | if (!HasSameBase(LHSValue, RHSValue)) { |
9250 | |
9251 | |
9252 | if (!IsEquality) |
9253 | return Error(E); |
9254 | |
9255 | |
9256 | |
9257 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
9258 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
9259 | return Error(E); |
9260 | |
9261 | |
9262 | |
9263 | |
9264 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
9265 | LHSValue.Base && RHSValue.Base) |
9266 | return Error(E); |
9267 | |
9268 | |
9269 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
9270 | return Error(E); |
9271 | |
9272 | |
9273 | if ((LHSValue.Base && LHSValue.Offset.isZero() && |
9274 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) || |
9275 | (RHSValue.Base && RHSValue.Offset.isZero() && |
9276 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue))) |
9277 | return Error(E); |
9278 | |
9279 | |
9280 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
9281 | (LHSValue.Base && isZeroSized(RHSValue))) |
9282 | return Error(E); |
9283 | return Success(CCR::Nonequal, E); |
9284 | } |
9285 | |
9286 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
9287 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
9288 | |
9289 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
9290 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
9291 | |
9292 | |
9293 | |
9294 | |
9295 | |
9296 | |
9297 | |
9298 | |
9299 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
9300 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
9301 | |
9302 | |
9303 | |
9304 | |
9305 | |
9306 | |
9307 | |
9308 | |
9309 | |
9310 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
9311 | bool WasArrayIndex; |
9312 | unsigned Mismatch = FindDesignatorMismatch( |
9313 | getType(LHSValue.Base), LHSDesignator, RHSDesignator, WasArrayIndex); |
9314 | |
9315 | |
9316 | |
9317 | |
9318 | |
9319 | |
9320 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
9321 | Mismatch < RHSDesignator.Entries.size()) { |
9322 | const FieldDecl *LF = getAsField(LHSDesignator.Entries[Mismatch]); |
9323 | const FieldDecl *RF = getAsField(RHSDesignator.Entries[Mismatch]); |
9324 | if (!LF && !RF) |
9325 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
9326 | else if (!LF) |
9327 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
9328 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
9329 | << RF->getParent() << RF; |
9330 | else if (!RF) |
9331 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
9332 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
9333 | << LF->getParent() << LF; |
9334 | else if (!LF->getParent()->isUnion() && |
9335 | LF->getAccess() != RF->getAccess()) |
9336 | Info.CCEDiag(E, |
9337 | diag::note_constexpr_pointer_comparison_differing_access) |
9338 | << LF << LF->getAccess() << RF << RF->getAccess() |
9339 | << LF->getParent(); |
9340 | } |
9341 | } |
9342 | |
9343 | |
9344 | |
9345 | unsigned PtrSize = Info.Ctx.getTypeSize(LHSTy); |
9346 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
9347 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
9348 | (0) . __assert_fail ("PtrSize <= 64 && \"Unexpected pointer width\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9348, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PtrSize <= 64 && "Unexpected pointer width"); |
9349 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
9350 | CompareLHS &= Mask; |
9351 | CompareRHS &= Mask; |
9352 | |
9353 | |
9354 | |
9355 | |
9356 | if (!LHSValue.Base.isNull() && IsRelational) { |
9357 | QualType BaseTy = getType(LHSValue.Base); |
9358 | if (BaseTy->isIncompleteType()) |
9359 | return Error(E); |
9360 | CharUnits Size = Info.Ctx.getTypeSizeInChars(BaseTy); |
9361 | uint64_t OffsetLimit = Size.getQuantity(); |
9362 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
9363 | return Error(E); |
9364 | } |
9365 | |
9366 | if (CompareLHS < CompareRHS) |
9367 | return Success(CCR::Less, E); |
9368 | if (CompareLHS > CompareRHS) |
9369 | return Success(CCR::Greater, E); |
9370 | return Success(CCR::Equal, E); |
9371 | } |
9372 | |
9373 | if (LHSTy->isMemberPointerType()) { |
9374 | (0) . __assert_fail ("IsEquality && \"unexpected member pointer operation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9374, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsEquality && "unexpected member pointer operation"); |
9375 | (0) . __assert_fail ("RHSTy->isMemberPointerType() && \"invalid comparison\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9375, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RHSTy->isMemberPointerType() && "invalid comparison"); |
9376 | |
9377 | MemberPtr LHSValue, RHSValue; |
9378 | |
9379 | bool LHSOK = EvaluateMemberPointer(E->getLHS(), LHSValue, Info); |
9380 | if (!LHSOK && !Info.noteFailure()) |
9381 | return false; |
9382 | |
9383 | if (!EvaluateMemberPointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
9384 | return false; |
9385 | |
9386 | |
9387 | |
9388 | |
9389 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
9390 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
9391 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
9392 | } |
9393 | |
9394 | |
9395 | |
9396 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
9397 | if (MD->isVirtual()) |
9398 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
9399 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
9400 | if (MD->isVirtual()) |
9401 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
9402 | |
9403 | |
9404 | |
9405 | |
9406 | |
9407 | bool Equal = LHSValue == RHSValue; |
9408 | return Success(Equal ? CCR::Equal : CCR::Nonequal, E); |
9409 | } |
9410 | |
9411 | if (LHSTy->isNullPtrType()) { |
9412 | (0) . __assert_fail ("E->isComparisonOp() && \"unexpected nullptr operation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9412, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isComparisonOp() && "unexpected nullptr operation"); |
9413 | (0) . __assert_fail ("RHSTy->isNullPtrType() && \"missing pointer conversion\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9413, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RHSTy->isNullPtrType() && "missing pointer conversion"); |
9414 | |
9415 | |
9416 | |
9417 | return Success(CCR::Equal, E); |
9418 | } |
9419 | |
9420 | return DoAfter(); |
9421 | } |
9422 | |
9423 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
9424 | if (!CheckLiteralType(Info, E)) |
9425 | return false; |
9426 | |
9427 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
9428 | const BinaryOperator *E) { |
9429 | |
9430 | |
9431 | const ComparisonCategoryInfo &CmpInfo = |
9432 | Info.Ctx.CompCategories.getInfoForType(E->getType()); |
9433 | const VarDecl *VD = |
9434 | CmpInfo.getValueInfo(CmpInfo.makeWeakResult(ResKind))->VD; |
9435 | |
9436 | LValue LV; |
9437 | LV.set(VD); |
9438 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
9439 | return false; |
9440 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
9441 | }; |
9442 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
9443 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
9444 | }); |
9445 | } |
9446 | |
9447 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
9448 | |
9449 | |
9450 | if (!Info.keepEvaluatingAfterFailure() && E->isAssignmentOp()) |
9451 | return Error(E); |
9452 | |
9453 | DelayedNoteFailureRAII MaybeNoteFailureLater(Info, E->isAssignmentOp()); |
9454 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
9455 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
9456 | |
9457 | (0) . __assert_fail ("(!E->getLHS()->getType()->isIntegralOrEnumerationType() || !E->getRHS()->getType()->isIntegralOrEnumerationType()) && \"DataRecursiveIntBinOpEvaluator should have handled integral types\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9459, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
9458 | (0) . __assert_fail ("(!E->getLHS()->getType()->isIntegralOrEnumerationType() || !E->getRHS()->getType()->isIntegralOrEnumerationType()) && \"DataRecursiveIntBinOpEvaluator should have handled integral types\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9459, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
9459 | (0) . __assert_fail ("(!E->getLHS()->getType()->isIntegralOrEnumerationType() || !E->getRHS()->getType()->isIntegralOrEnumerationType()) && \"DataRecursiveIntBinOpEvaluator should have handled integral types\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9459, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "DataRecursiveIntBinOpEvaluator should have handled integral types"); |
9460 | |
9461 | if (E->isComparisonOp()) { |
9462 | |
9463 | |
9464 | auto OnSuccess = [&](ComparisonCategoryResult ResKind, |
9465 | const BinaryOperator *E) { |
9466 | using CCR = ComparisonCategoryResult; |
9467 | bool IsEqual = ResKind == CCR::Equal, |
9468 | IsLess = ResKind == CCR::Less, |
9469 | IsGreater = ResKind == CCR::Greater; |
9470 | auto Op = E->getOpcode(); |
9471 | switch (Op) { |
9472 | default: |
9473 | llvm_unreachable("unsupported binary operator"); |
9474 | case BO_EQ: |
9475 | case BO_NE: |
9476 | return Success(IsEqual == (Op == BO_EQ), E); |
9477 | case BO_LT: return Success(IsLess, E); |
9478 | case BO_GT: return Success(IsGreater, E); |
9479 | case BO_LE: return Success(IsEqual || IsLess, E); |
9480 | case BO_GE: return Success(IsEqual || IsGreater, E); |
9481 | } |
9482 | }; |
9483 | return EvaluateComparisonBinaryOperator(Info, E, OnSuccess, [&]() { |
9484 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
9485 | }); |
9486 | } |
9487 | |
9488 | QualType LHSTy = E->getLHS()->getType(); |
9489 | QualType RHSTy = E->getRHS()->getType(); |
9490 | |
9491 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
9492 | E->getOpcode() == BO_Sub) { |
9493 | LValue LHSValue, RHSValue; |
9494 | |
9495 | bool LHSOK = EvaluatePointer(E->getLHS(), LHSValue, Info); |
9496 | if (!LHSOK && !Info.noteFailure()) |
9497 | return false; |
9498 | |
9499 | if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK) |
9500 | return false; |
9501 | |
9502 | |
9503 | |
9504 | if (!HasSameBase(LHSValue, RHSValue)) { |
9505 | |
9506 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
9507 | return Error(E); |
9508 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
9509 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
9510 | if (!LHSExpr || !RHSExpr) |
9511 | return Error(E); |
9512 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(LHSExpr); |
9513 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(RHSExpr); |
9514 | if (!LHSAddrExpr || !RHSAddrExpr) |
9515 | return Error(E); |
9516 | |
9517 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
9518 | RHSAddrExpr->getLabel()->getDeclContext()) |
9519 | return Error(E); |
9520 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
9521 | } |
9522 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
9523 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
9524 | |
9525 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
9526 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
9527 | |
9528 | |
9529 | |
9530 | |
9531 | |
9532 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
9533 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
9534 | RHSDesignator)) |
9535 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
9536 | |
9537 | QualType Type = E->getLHS()->getType(); |
9538 | QualType ElementType = Type->getAs<PointerType>()->getPointeeType(); |
9539 | |
9540 | CharUnits ElementSize; |
9541 | if (!HandleSizeof(Info, E->getExprLoc(), ElementType, ElementSize)) |
9542 | return false; |
9543 | |
9544 | |
9545 | |
9546 | |
9547 | if (ElementSize.isZero()) { |
9548 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
9549 | << ElementType; |
9550 | return false; |
9551 | } |
9552 | |
9553 | |
9554 | |
9555 | |
9556 | |
9557 | |
9558 | |
9559 | |
9560 | |
9561 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
9562 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
9563 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
9564 | false); |
9565 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
9566 | APSInt Result = TrueResult.trunc(Info.Ctx.getIntWidth(E->getType())); |
9567 | |
9568 | if (Result.extend(65) != TrueResult && |
9569 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
9570 | return false; |
9571 | return Success(Result, E); |
9572 | } |
9573 | |
9574 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
9575 | } |
9576 | |
9577 | |
9578 | |
9579 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
9580 | const UnaryExprOrTypeTraitExpr *E) { |
9581 | switch(E->getKind()) { |
9582 | case UETT_PreferredAlignOf: |
9583 | case UETT_AlignOf: { |
9584 | if (E->isArgumentType()) |
9585 | return Success(GetAlignOfType(Info, E->getArgumentType(), E->getKind()), |
9586 | E); |
9587 | else |
9588 | return Success(GetAlignOfExpr(Info, E->getArgumentExpr(), E->getKind()), |
9589 | E); |
9590 | } |
9591 | |
9592 | case UETT_VecStep: { |
9593 | QualType Ty = E->getTypeOfArgument(); |
9594 | |
9595 | if (Ty->isVectorType()) { |
9596 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
9597 | |
9598 | |
9599 | |
9600 | if (n == 3) |
9601 | n = 4; |
9602 | |
9603 | return Success(n, E); |
9604 | } else |
9605 | return Success(1, E); |
9606 | } |
9607 | |
9608 | case UETT_SizeOf: { |
9609 | QualType SrcTy = E->getTypeOfArgument(); |
9610 | |
9611 | |
9612 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
9613 | SrcTy = Ref->getPointeeType(); |
9614 | |
9615 | CharUnits Sizeof; |
9616 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof)) |
9617 | return false; |
9618 | return Success(Sizeof, E); |
9619 | } |
9620 | case UETT_OpenMPRequiredSimdAlign: |
9621 | isArgumentType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9621, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isArgumentType()); |
9622 | return Success( |
9623 | Info.Ctx.toCharUnitsFromBits( |
9624 | Info.Ctx.getOpenMPDefaultSimdAlign(E->getArgumentType())) |
9625 | .getQuantity(), |
9626 | E); |
9627 | } |
9628 | |
9629 | llvm_unreachable("unknown expr/type trait"); |
9630 | } |
9631 | |
9632 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
9633 | CharUnits Result; |
9634 | unsigned n = OOE->getNumComponents(); |
9635 | if (n == 0) |
9636 | return Error(OOE); |
9637 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
9638 | for (unsigned i = 0; i != n; ++i) { |
9639 | OffsetOfNode ON = OOE->getComponent(i); |
9640 | switch (ON.getKind()) { |
9641 | case OffsetOfNode::Array: { |
9642 | const Expr *Idx = OOE->getIndexExpr(ON.getArrayExprIndex()); |
9643 | APSInt IdxResult; |
9644 | if (!EvaluateInteger(Idx, IdxResult, Info)) |
9645 | return false; |
9646 | const ArrayType *AT = Info.Ctx.getAsArrayType(CurrentType); |
9647 | if (!AT) |
9648 | return Error(OOE); |
9649 | CurrentType = AT->getElementType(); |
9650 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(CurrentType); |
9651 | Result += IdxResult.getSExtValue() * ElementSize; |
9652 | break; |
9653 | } |
9654 | |
9655 | case OffsetOfNode::Field: { |
9656 | FieldDecl *MemberDecl = ON.getField(); |
9657 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
9658 | if (!RT) |
9659 | return Error(OOE); |
9660 | RecordDecl *RD = RT->getDecl(); |
9661 | if (RD->isInvalidDecl()) return false; |
9662 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
9663 | unsigned i = MemberDecl->getFieldIndex(); |
9664 | (0) . __assert_fail ("i < RL.getFieldCount() && \"offsetof field in wrong type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9664, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(i < RL.getFieldCount() && "offsetof field in wrong type"); |
9665 | Result += Info.Ctx.toCharUnitsFromBits(RL.getFieldOffset(i)); |
9666 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
9667 | break; |
9668 | } |
9669 | |
9670 | case OffsetOfNode::Identifier: |
9671 | llvm_unreachable("dependent __builtin_offsetof"); |
9672 | |
9673 | case OffsetOfNode::Base: { |
9674 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
9675 | if (BaseSpec->isVirtual()) |
9676 | return Error(OOE); |
9677 | |
9678 | |
9679 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
9680 | if (!RT) |
9681 | return Error(OOE); |
9682 | RecordDecl *RD = RT->getDecl(); |
9683 | if (RD->isInvalidDecl()) return false; |
9684 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(RD); |
9685 | |
9686 | |
9687 | CurrentType = BaseSpec->getType(); |
9688 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
9689 | if (!BaseRT) |
9690 | return Error(OOE); |
9691 | |
9692 | |
9693 | Result += RL.getBaseClassOffset(cast<CXXRecordDecl>(BaseRT->getDecl())); |
9694 | break; |
9695 | } |
9696 | } |
9697 | } |
9698 | return Success(Result, OOE); |
9699 | } |
9700 | |
9701 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
9702 | switch (E->getOpcode()) { |
9703 | default: |
9704 | |
9705 | |
9706 | return Error(E); |
9707 | case UO_Extension: |
9708 | |
9709 | |
9710 | return Visit(E->getSubExpr()); |
9711 | case UO_Plus: |
9712 | |
9713 | return Visit(E->getSubExpr()); |
9714 | case UO_Minus: { |
9715 | if (!Visit(E->getSubExpr())) |
9716 | return false; |
9717 | if (!Result.isInt()) return Error(E); |
9718 | const APSInt &Value = Result.getInt(); |
9719 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() && |
9720 | !HandleOverflow(Info, E, -Value.extend(Value.getBitWidth() + 1), |
9721 | E->getType())) |
9722 | return false; |
9723 | return Success(-Value, E); |
9724 | } |
9725 | case UO_Not: { |
9726 | if (!Visit(E->getSubExpr())) |
9727 | return false; |
9728 | if (!Result.isInt()) return Error(E); |
9729 | return Success(~Result.getInt(), E); |
9730 | } |
9731 | case UO_LNot: { |
9732 | bool bres; |
9733 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
9734 | return false; |
9735 | return Success(!bres, E); |
9736 | } |
9737 | } |
9738 | } |
9739 | |
9740 | |
9741 | |
9742 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9743 | const Expr *SubExpr = E->getSubExpr(); |
9744 | QualType DestType = E->getType(); |
9745 | QualType SrcType = SubExpr->getType(); |
9746 | |
9747 | switch (E->getCastKind()) { |
9748 | case CK_BaseToDerived: |
9749 | case CK_DerivedToBase: |
9750 | case CK_UncheckedDerivedToBase: |
9751 | case CK_Dynamic: |
9752 | case CK_ToUnion: |
9753 | case CK_ArrayToPointerDecay: |
9754 | case CK_FunctionToPointerDecay: |
9755 | case CK_NullToPointer: |
9756 | case CK_NullToMemberPointer: |
9757 | case CK_BaseToDerivedMemberPointer: |
9758 | case CK_DerivedToBaseMemberPointer: |
9759 | case CK_ReinterpretMemberPointer: |
9760 | case CK_ConstructorConversion: |
9761 | case CK_IntegralToPointer: |
9762 | case CK_ToVoid: |
9763 | case CK_VectorSplat: |
9764 | case CK_IntegralToFloating: |
9765 | case CK_FloatingCast: |
9766 | case CK_CPointerToObjCPointerCast: |
9767 | case CK_BlockPointerToObjCPointerCast: |
9768 | case CK_AnyPointerToBlockPointerCast: |
9769 | case CK_ObjCObjectLValueCast: |
9770 | case CK_FloatingRealToComplex: |
9771 | case CK_FloatingComplexToReal: |
9772 | case CK_FloatingComplexCast: |
9773 | case CK_FloatingComplexToIntegralComplex: |
9774 | case CK_IntegralRealToComplex: |
9775 | case CK_IntegralComplexCast: |
9776 | case CK_IntegralComplexToFloatingComplex: |
9777 | case CK_BuiltinFnToFnPtr: |
9778 | case CK_ZeroToOCLOpaqueType: |
9779 | case CK_NonAtomicToAtomic: |
9780 | case CK_AddressSpaceConversion: |
9781 | case CK_IntToOCLSampler: |
9782 | case CK_FixedPointCast: |
9783 | case CK_IntegralToFixedPoint: |
9784 | llvm_unreachable("invalid cast kind for integral value"); |
9785 | |
9786 | case CK_BitCast: |
9787 | case CK_Dependent: |
9788 | case CK_LValueBitCast: |
9789 | case CK_ARCProduceObject: |
9790 | case CK_ARCConsumeObject: |
9791 | case CK_ARCReclaimReturnedObject: |
9792 | case CK_ARCExtendBlockObject: |
9793 | case CK_CopyAndAutoreleaseBlockObject: |
9794 | return Error(E); |
9795 | |
9796 | case CK_UserDefinedConversion: |
9797 | case CK_LValueToRValue: |
9798 | case CK_AtomicToNonAtomic: |
9799 | case CK_NoOp: |
9800 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9801 | |
9802 | case CK_MemberPointerToBoolean: |
9803 | case CK_PointerToBoolean: |
9804 | case CK_IntegralToBoolean: |
9805 | case CK_FloatingToBoolean: |
9806 | case CK_BooleanToSignedIntegral: |
9807 | case CK_FloatingComplexToBoolean: |
9808 | case CK_IntegralComplexToBoolean: { |
9809 | bool BoolResult; |
9810 | if (!EvaluateAsBooleanCondition(SubExpr, BoolResult, Info)) |
9811 | return false; |
9812 | uint64_t IntResult = BoolResult; |
9813 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
9814 | IntResult = (uint64_t)-1; |
9815 | return Success(IntResult, E); |
9816 | } |
9817 | |
9818 | case CK_FixedPointToIntegral: { |
9819 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SrcType)); |
9820 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
9821 | return false; |
9822 | bool Overflowed; |
9823 | llvm::APSInt Result = Src.convertToInt( |
9824 | Info.Ctx.getIntWidth(DestType), |
9825 | DestType->isSignedIntegerOrEnumerationType(), &Overflowed); |
9826 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
9827 | return false; |
9828 | return Success(Result, E); |
9829 | } |
9830 | |
9831 | case CK_FixedPointToBoolean: { |
9832 | |
9833 | APValue Val; |
9834 | if (!Evaluate(Val, Info, SubExpr)) |
9835 | return false; |
9836 | return Success(Val.getFixedPoint().getBoolValue(), E); |
9837 | } |
9838 | |
9839 | case CK_IntegralCast: { |
9840 | if (!Visit(SubExpr)) |
9841 | return false; |
9842 | |
9843 | if (!Result.isInt()) { |
9844 | |
9845 | |
9846 | |
9847 | |
9848 | |
9849 | if (Result.isAddrLabelDiff()) |
9850 | return Info.Ctx.getTypeSize(DestType) <= Info.Ctx.getTypeSize(SrcType); |
9851 | |
9852 | return Info.Ctx.getTypeSize(DestType) == Info.Ctx.getTypeSize(SrcType); |
9853 | } |
9854 | |
9855 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
9856 | Result.getInt()), E); |
9857 | } |
9858 | |
9859 | case CK_PointerToIntegral: { |
9860 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 2; |
9861 | |
9862 | LValue LV; |
9863 | if (!EvaluatePointer(SubExpr, LV, Info)) |
9864 | return false; |
9865 | |
9866 | if (LV.getLValueBase()) { |
9867 | |
9868 | |
9869 | |
9870 | |
9871 | if (Info.Ctx.getTypeSize(DestType) != Info.Ctx.getTypeSize(SrcType)) |
9872 | return Error(E); |
9873 | |
9874 | LV.Designator.setInvalid(); |
9875 | LV.moveInto(Result); |
9876 | return true; |
9877 | } |
9878 | |
9879 | APSInt AsInt; |
9880 | APValue V; |
9881 | LV.moveInto(V); |
9882 | if (!V.toIntegralConstant(AsInt, SrcType, Info.Ctx)) |
9883 | llvm_unreachable("Can't cast this!"); |
9884 | |
9885 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
9886 | } |
9887 | |
9888 | case CK_IntegralComplexToReal: { |
9889 | ComplexValue C; |
9890 | if (!EvaluateComplex(SubExpr, C, Info)) |
9891 | return false; |
9892 | return Success(C.getComplexIntReal(), E); |
9893 | } |
9894 | |
9895 | case CK_FloatingToIntegral: { |
9896 | APFloat F(0.0); |
9897 | if (!EvaluateFloat(SubExpr, F, Info)) |
9898 | return false; |
9899 | |
9900 | APSInt Value; |
9901 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
9902 | return false; |
9903 | return Success(Value, E); |
9904 | } |
9905 | } |
9906 | |
9907 | llvm_unreachable("unknown cast resulting in integral value"); |
9908 | } |
9909 | |
9910 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
9911 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
9912 | ComplexValue LV; |
9913 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
9914 | return false; |
9915 | if (!LV.isComplexInt()) |
9916 | return Error(E); |
9917 | return Success(LV.getComplexIntReal(), E); |
9918 | } |
9919 | |
9920 | return Visit(E->getSubExpr()); |
9921 | } |
9922 | |
9923 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
9924 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
9925 | ComplexValue LV; |
9926 | if (!EvaluateComplex(E->getSubExpr(), LV, Info)) |
9927 | return false; |
9928 | if (!LV.isComplexInt()) |
9929 | return Error(E); |
9930 | return Success(LV.getComplexIntImag(), E); |
9931 | } |
9932 | |
9933 | VisitIgnoredValue(E->getSubExpr()); |
9934 | return Success(0, E); |
9935 | } |
9936 | |
9937 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
9938 | return Success(E->getPackLength(), E); |
9939 | } |
9940 | |
9941 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
9942 | return Success(E->getValue(), E); |
9943 | } |
9944 | |
9945 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
9946 | switch (E->getOpcode()) { |
9947 | default: |
9948 | |
9949 | return Error(E); |
9950 | case UO_Plus: |
9951 | |
9952 | return Visit(E->getSubExpr()); |
9953 | case UO_Minus: { |
9954 | if (!Visit(E->getSubExpr())) return false; |
9955 | if (!Result.isFixedPoint()) |
9956 | return Error(E); |
9957 | bool Overflowed; |
9958 | APFixedPoint Negated = Result.getFixedPoint().negate(&Overflowed); |
9959 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
9960 | return false; |
9961 | return Success(Negated, E); |
9962 | } |
9963 | case UO_LNot: { |
9964 | bool bres; |
9965 | if (!EvaluateAsBooleanCondition(E->getSubExpr(), bres, Info)) |
9966 | return false; |
9967 | return Success(!bres, E); |
9968 | } |
9969 | } |
9970 | } |
9971 | |
9972 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9973 | const Expr *SubExpr = E->getSubExpr(); |
9974 | QualType DestType = E->getType(); |
9975 | (0) . __assert_fail ("DestType->isFixedPointType() && \"Expected destination type to be a fixed point type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9976, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DestType->isFixedPointType() && |
9976 | (0) . __assert_fail ("DestType->isFixedPointType() && \"Expected destination type to be a fixed point type\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 9976, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expected destination type to be a fixed point type"); |
9977 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(DestType); |
9978 | |
9979 | switch (E->getCastKind()) { |
9980 | case CK_FixedPointCast: { |
9981 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(SubExpr->getType())); |
9982 | if (!EvaluateFixedPoint(SubExpr, Src, Info)) |
9983 | return false; |
9984 | bool Overflowed; |
9985 | APFixedPoint Result = Src.convert(DestFXSema, &Overflowed); |
9986 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
9987 | return false; |
9988 | return Success(Result, E); |
9989 | } |
9990 | case CK_IntegralToFixedPoint: { |
9991 | APSInt Src; |
9992 | if (!EvaluateInteger(SubExpr, Src, Info)) |
9993 | return false; |
9994 | |
9995 | bool Overflowed; |
9996 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
9997 | Src, Info.Ctx.getFixedPointSemantics(DestType), &Overflowed); |
9998 | |
9999 | if (Overflowed && !HandleOverflow(Info, E, IntResult, DestType)) |
10000 | return false; |
10001 | |
10002 | return Success(IntResult, E); |
10003 | } |
10004 | case CK_NoOp: |
10005 | case CK_LValueToRValue: |
10006 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10007 | default: |
10008 | return Error(E); |
10009 | } |
10010 | } |
10011 | |
10012 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10013 | const Expr *LHS = E->getLHS(); |
10014 | const Expr *RHS = E->getRHS(); |
10015 | FixedPointSemantics ResultFXSema = |
10016 | Info.Ctx.getFixedPointSemantics(E->getType()); |
10017 | |
10018 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(LHS->getType())); |
10019 | if (!EvaluateFixedPointOrInteger(LHS, LHSFX, Info)) |
10020 | return false; |
10021 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(RHS->getType())); |
10022 | if (!EvaluateFixedPointOrInteger(RHS, RHSFX, Info)) |
10023 | return false; |
10024 | |
10025 | switch (E->getOpcode()) { |
10026 | case BO_Add: { |
10027 | bool AddOverflow, ConversionOverflow; |
10028 | APFixedPoint Result = LHSFX.add(RHSFX, &AddOverflow) |
10029 | .convert(ResultFXSema, &ConversionOverflow); |
10030 | if ((AddOverflow || ConversionOverflow) && |
10031 | !HandleOverflow(Info, E, Result, E->getType())) |
10032 | return false; |
10033 | return Success(Result, E); |
10034 | } |
10035 | default: |
10036 | return false; |
10037 | } |
10038 | llvm_unreachable("Should've exited before this"); |
10039 | } |
10040 | |
10041 | |
10042 | |
10043 | |
10044 | |
10045 | namespace { |
10046 | class FloatExprEvaluator |
10047 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
10048 | APFloat &Result; |
10049 | public: |
10050 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
10051 | : ExprEvaluatorBaseTy(info), Result(result) {} |
10052 | |
10053 | bool Success(const APValue &V, const Expr *e) { |
10054 | Result = V.getFloat(); |
10055 | return true; |
10056 | } |
10057 | |
10058 | bool ZeroInitialization(const Expr *E) { |
10059 | Result = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(E->getType())); |
10060 | return true; |
10061 | } |
10062 | |
10063 | bool VisitCallExpr(const CallExpr *E); |
10064 | |
10065 | bool VisitUnaryOperator(const UnaryOperator *E); |
10066 | bool VisitBinaryOperator(const BinaryOperator *E); |
10067 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
10068 | bool VisitCastExpr(const CastExpr *E); |
10069 | |
10070 | bool VisitUnaryReal(const UnaryOperator *E); |
10071 | bool VisitUnaryImag(const UnaryOperator *E); |
10072 | |
10073 | |
10074 | }; |
10075 | } |
10076 | |
10077 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
10078 | isRValue() && E->getType()->isRealFloatingType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10078, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isRealFloatingType()); |
10079 | return FloatExprEvaluator(Info, Result).Visit(E); |
10080 | } |
10081 | |
10082 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
10083 | QualType ResultTy, |
10084 | const Expr *Arg, |
10085 | bool SNaN, |
10086 | llvm::APFloat &Result) { |
10087 | const StringLiteral *S = dyn_cast<StringLiteral>(Arg->IgnoreParenCasts()); |
10088 | if (!S) return false; |
10089 | |
10090 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(ResultTy); |
10091 | |
10092 | llvm::APInt fill; |
10093 | |
10094 | |
10095 | if (S->getString().empty()) |
10096 | fill = llvm::APInt(32, 0); |
10097 | else if (S->getString().getAsInteger(0, fill)) |
10098 | return false; |
10099 | |
10100 | if (Context.getTargetInfo().isNan2008()) { |
10101 | if (SNaN) |
10102 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
10103 | else |
10104 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
10105 | } else { |
10106 | |
10107 | |
10108 | |
10109 | |
10110 | |
10111 | if (SNaN) |
10112 | Result = llvm::APFloat::getQNaN(Sem, false, &fill); |
10113 | else |
10114 | Result = llvm::APFloat::getSNaN(Sem, false, &fill); |
10115 | } |
10116 | |
10117 | return true; |
10118 | } |
10119 | |
10120 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
10121 | switch (E->getBuiltinCallee()) { |
10122 | default: |
10123 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
10124 | |
10125 | case Builtin::BI__builtin_huge_val: |
10126 | case Builtin::BI__builtin_huge_valf: |
10127 | case Builtin::BI__builtin_huge_vall: |
10128 | case Builtin::BI__builtin_huge_valf128: |
10129 | case Builtin::BI__builtin_inf: |
10130 | case Builtin::BI__builtin_inff: |
10131 | case Builtin::BI__builtin_infl: |
10132 | case Builtin::BI__builtin_inff128: { |
10133 | const llvm::fltSemantics &Sem = |
10134 | Info.Ctx.getFloatTypeSemantics(E->getType()); |
10135 | Result = llvm::APFloat::getInf(Sem); |
10136 | return true; |
10137 | } |
10138 | |
10139 | case Builtin::BI__builtin_nans: |
10140 | case Builtin::BI__builtin_nansf: |
10141 | case Builtin::BI__builtin_nansl: |
10142 | case Builtin::BI__builtin_nansf128: |
10143 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
10144 | true, Result)) |
10145 | return Error(E); |
10146 | return true; |
10147 | |
10148 | case Builtin::BI__builtin_nan: |
10149 | case Builtin::BI__builtin_nanf: |
10150 | case Builtin::BI__builtin_nanl: |
10151 | case Builtin::BI__builtin_nanf128: |
10152 | |
10153 | |
10154 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(0), |
10155 | false, Result)) |
10156 | return Error(E); |
10157 | return true; |
10158 | |
10159 | case Builtin::BI__builtin_fabs: |
10160 | case Builtin::BI__builtin_fabsf: |
10161 | case Builtin::BI__builtin_fabsl: |
10162 | case Builtin::BI__builtin_fabsf128: |
10163 | if (!EvaluateFloat(E->getArg(0), Result, Info)) |
10164 | return false; |
10165 | |
10166 | if (Result.isNegative()) |
10167 | Result.changeSign(); |
10168 | return true; |
10169 | |
10170 | |
10171 | |
10172 | |
10173 | |
10174 | case Builtin::BI__builtin_copysign: |
10175 | case Builtin::BI__builtin_copysignf: |
10176 | case Builtin::BI__builtin_copysignl: |
10177 | case Builtin::BI__builtin_copysignf128: { |
10178 | APFloat RHS(0.); |
10179 | if (!EvaluateFloat(E->getArg(0), Result, Info) || |
10180 | !EvaluateFloat(E->getArg(1), RHS, Info)) |
10181 | return false; |
10182 | Result.copySign(RHS); |
10183 | return true; |
10184 | } |
10185 | } |
10186 | } |
10187 | |
10188 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
10189 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
10190 | ComplexValue CV; |
10191 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
10192 | return false; |
10193 | Result = CV.FloatReal; |
10194 | return true; |
10195 | } |
10196 | |
10197 | return Visit(E->getSubExpr()); |
10198 | } |
10199 | |
10200 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
10201 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
10202 | ComplexValue CV; |
10203 | if (!EvaluateComplex(E->getSubExpr(), CV, Info)) |
10204 | return false; |
10205 | Result = CV.FloatImag; |
10206 | return true; |
10207 | } |
10208 | |
10209 | VisitIgnoredValue(E->getSubExpr()); |
10210 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(E->getType()); |
10211 | Result = llvm::APFloat::getZero(Sem); |
10212 | return true; |
10213 | } |
10214 | |
10215 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
10216 | switch (E->getOpcode()) { |
10217 | default: return Error(E); |
10218 | case UO_Plus: |
10219 | return EvaluateFloat(E->getSubExpr(), Result, Info); |
10220 | case UO_Minus: |
10221 | if (!EvaluateFloat(E->getSubExpr(), Result, Info)) |
10222 | return false; |
10223 | Result.changeSign(); |
10224 | return true; |
10225 | } |
10226 | } |
10227 | |
10228 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10229 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
10230 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10231 | |
10232 | APFloat RHS(0.0); |
10233 | bool LHSOK = EvaluateFloat(E->getLHS(), Result, Info); |
10234 | if (!LHSOK && !Info.noteFailure()) |
10235 | return false; |
10236 | return EvaluateFloat(E->getRHS(), RHS, Info) && LHSOK && |
10237 | handleFloatFloatBinOp(Info, E, Result, E->getOpcode(), RHS); |
10238 | } |
10239 | |
10240 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
10241 | Result = E->getValue(); |
10242 | return true; |
10243 | } |
10244 | |
10245 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10246 | const Expr* SubExpr = E->getSubExpr(); |
10247 | |
10248 | switch (E->getCastKind()) { |
10249 | default: |
10250 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10251 | |
10252 | case CK_IntegralToFloating: { |
10253 | APSInt IntResult; |
10254 | return EvaluateInteger(SubExpr, IntResult, Info) && |
10255 | HandleIntToFloatCast(Info, E, SubExpr->getType(), IntResult, |
10256 | E->getType(), Result); |
10257 | } |
10258 | |
10259 | case CK_FloatingCast: { |
10260 | if (!Visit(SubExpr)) |
10261 | return false; |
10262 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
10263 | Result); |
10264 | } |
10265 | |
10266 | case CK_FloatingComplexToReal: { |
10267 | ComplexValue V; |
10268 | if (!EvaluateComplex(SubExpr, V, Info)) |
10269 | return false; |
10270 | Result = V.getComplexFloatReal(); |
10271 | return true; |
10272 | } |
10273 | } |
10274 | } |
10275 | |
10276 | |
10277 | |
10278 | |
10279 | |
10280 | namespace { |
10281 | class ComplexExprEvaluator |
10282 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
10283 | ComplexValue &Result; |
10284 | |
10285 | public: |
10286 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
10287 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
10288 | |
10289 | bool Success(const APValue &V, const Expr *e) { |
10290 | Result.setFrom(V); |
10291 | return true; |
10292 | } |
10293 | |
10294 | bool ZeroInitialization(const Expr *E); |
10295 | |
10296 | |
10297 | |
10298 | |
10299 | |
10300 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
10301 | bool VisitCastExpr(const CastExpr *E); |
10302 | bool VisitBinaryOperator(const BinaryOperator *E); |
10303 | bool VisitUnaryOperator(const UnaryOperator *E); |
10304 | bool VisitInitListExpr(const InitListExpr *E); |
10305 | }; |
10306 | } |
10307 | |
10308 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
10309 | EvalInfo &Info) { |
10310 | isRValue() && E->getType()->isAnyComplexType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10310, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isAnyComplexType()); |
10311 | return ComplexExprEvaluator(Info, Result).Visit(E); |
10312 | } |
10313 | |
10314 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
10315 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
10316 | if (ElemTy->isRealFloatingType()) { |
10317 | Result.makeComplexFloat(); |
10318 | APFloat Zero = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(ElemTy)); |
10319 | Result.FloatReal = Zero; |
10320 | Result.FloatImag = Zero; |
10321 | } else { |
10322 | Result.makeComplexInt(); |
10323 | APSInt Zero = Info.Ctx.MakeIntValue(0, ElemTy); |
10324 | Result.IntReal = Zero; |
10325 | Result.IntImag = Zero; |
10326 | } |
10327 | return true; |
10328 | } |
10329 | |
10330 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
10331 | const Expr* SubExpr = E->getSubExpr(); |
10332 | |
10333 | if (SubExpr->getType()->isRealFloatingType()) { |
10334 | Result.makeComplexFloat(); |
10335 | APFloat &Imag = Result.FloatImag; |
10336 | if (!EvaluateFloat(SubExpr, Imag, Info)) |
10337 | return false; |
10338 | |
10339 | Result.FloatReal = APFloat(Imag.getSemantics()); |
10340 | return true; |
10341 | } else { |
10342 | (0) . __assert_fail ("SubExpr->getType()->isIntegerType() && \"Unexpected imaginary literal.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10343, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(SubExpr->getType()->isIntegerType() && |
10343 | (0) . __assert_fail ("SubExpr->getType()->isIntegerType() && \"Unexpected imaginary literal.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10343, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unexpected imaginary literal."); |
10344 | |
10345 | Result.makeComplexInt(); |
10346 | APSInt &Imag = Result.IntImag; |
10347 | if (!EvaluateInteger(SubExpr, Imag, Info)) |
10348 | return false; |
10349 | |
10350 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
10351 | return true; |
10352 | } |
10353 | } |
10354 | |
10355 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10356 | |
10357 | switch (E->getCastKind()) { |
10358 | case CK_BitCast: |
10359 | case CK_BaseToDerived: |
10360 | case CK_DerivedToBase: |
10361 | case CK_UncheckedDerivedToBase: |
10362 | case CK_Dynamic: |
10363 | case CK_ToUnion: |
10364 | case CK_ArrayToPointerDecay: |
10365 | case CK_FunctionToPointerDecay: |
10366 | case CK_NullToPointer: |
10367 | case CK_NullToMemberPointer: |
10368 | case CK_BaseToDerivedMemberPointer: |
10369 | case CK_DerivedToBaseMemberPointer: |
10370 | case CK_MemberPointerToBoolean: |
10371 | case CK_ReinterpretMemberPointer: |
10372 | case CK_ConstructorConversion: |
10373 | case CK_IntegralToPointer: |
10374 | case CK_PointerToIntegral: |
10375 | case CK_PointerToBoolean: |
10376 | case CK_ToVoid: |
10377 | case CK_VectorSplat: |
10378 | case CK_IntegralCast: |
10379 | case CK_BooleanToSignedIntegral: |
10380 | case CK_IntegralToBoolean: |
10381 | case CK_IntegralToFloating: |
10382 | case CK_FloatingToIntegral: |
10383 | case CK_FloatingToBoolean: |
10384 | case CK_FloatingCast: |
10385 | case CK_CPointerToObjCPointerCast: |
10386 | case CK_BlockPointerToObjCPointerCast: |
10387 | case CK_AnyPointerToBlockPointerCast: |
10388 | case CK_ObjCObjectLValueCast: |
10389 | case CK_FloatingComplexToReal: |
10390 | case CK_FloatingComplexToBoolean: |
10391 | case CK_IntegralComplexToReal: |
10392 | case CK_IntegralComplexToBoolean: |
10393 | case CK_ARCProduceObject: |
10394 | case CK_ARCConsumeObject: |
10395 | case CK_ARCReclaimReturnedObject: |
10396 | case CK_ARCExtendBlockObject: |
10397 | case CK_CopyAndAutoreleaseBlockObject: |
10398 | case CK_BuiltinFnToFnPtr: |
10399 | case CK_ZeroToOCLOpaqueType: |
10400 | case CK_NonAtomicToAtomic: |
10401 | case CK_AddressSpaceConversion: |
10402 | case CK_IntToOCLSampler: |
10403 | case CK_FixedPointCast: |
10404 | case CK_FixedPointToBoolean: |
10405 | case CK_FixedPointToIntegral: |
10406 | case CK_IntegralToFixedPoint: |
10407 | llvm_unreachable("invalid cast kind for complex value"); |
10408 | |
10409 | case CK_LValueToRValue: |
10410 | case CK_AtomicToNonAtomic: |
10411 | case CK_NoOp: |
10412 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10413 | |
10414 | case CK_Dependent: |
10415 | case CK_LValueBitCast: |
10416 | case CK_UserDefinedConversion: |
10417 | return Error(E); |
10418 | |
10419 | case CK_FloatingRealToComplex: { |
10420 | APFloat &Real = Result.FloatReal; |
10421 | if (!EvaluateFloat(E->getSubExpr(), Real, Info)) |
10422 | return false; |
10423 | |
10424 | Result.makeComplexFloat(); |
10425 | Result.FloatImag = APFloat(Real.getSemantics()); |
10426 | return true; |
10427 | } |
10428 | |
10429 | case CK_FloatingComplexCast: { |
10430 | if (!Visit(E->getSubExpr())) |
10431 | return false; |
10432 | |
10433 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
10434 | QualType From |
10435 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
10436 | |
10437 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
10438 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
10439 | } |
10440 | |
10441 | case CK_FloatingComplexToIntegralComplex: { |
10442 | if (!Visit(E->getSubExpr())) |
10443 | return false; |
10444 | |
10445 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
10446 | QualType From |
10447 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
10448 | Result.makeComplexInt(); |
10449 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
10450 | To, Result.IntReal) && |
10451 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
10452 | To, Result.IntImag); |
10453 | } |
10454 | |
10455 | case CK_IntegralRealToComplex: { |
10456 | APSInt &Real = Result.IntReal; |
10457 | if (!EvaluateInteger(E->getSubExpr(), Real, Info)) |
10458 | return false; |
10459 | |
10460 | Result.makeComplexInt(); |
10461 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
10462 | return true; |
10463 | } |
10464 | |
10465 | case CK_IntegralComplexCast: { |
10466 | if (!Visit(E->getSubExpr())) |
10467 | return false; |
10468 | |
10469 | QualType To = E->getType()->getAs<ComplexType>()->getElementType(); |
10470 | QualType From |
10471 | = E->getSubExpr()->getType()->getAs<ComplexType>()->getElementType(); |
10472 | |
10473 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
10474 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
10475 | return true; |
10476 | } |
10477 | |
10478 | case CK_IntegralComplexToFloatingComplex: { |
10479 | if (!Visit(E->getSubExpr())) |
10480 | return false; |
10481 | |
10482 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
10483 | QualType From |
10484 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
10485 | Result.makeComplexFloat(); |
10486 | return HandleIntToFloatCast(Info, E, From, Result.IntReal, |
10487 | To, Result.FloatReal) && |
10488 | HandleIntToFloatCast(Info, E, From, Result.IntImag, |
10489 | To, Result.FloatImag); |
10490 | } |
10491 | } |
10492 | |
10493 | llvm_unreachable("unknown cast resulting in complex value"); |
10494 | } |
10495 | |
10496 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10497 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
10498 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10499 | |
10500 | |
10501 | |
10502 | bool LHSReal = false, RHSReal = false; |
10503 | |
10504 | bool LHSOK; |
10505 | if (E->getLHS()->getType()->isRealFloatingType()) { |
10506 | LHSReal = true; |
10507 | APFloat &Real = Result.FloatReal; |
10508 | LHSOK = EvaluateFloat(E->getLHS(), Real, Info); |
10509 | if (LHSOK) { |
10510 | Result.makeComplexFloat(); |
10511 | Result.FloatImag = APFloat(Real.getSemantics()); |
10512 | } |
10513 | } else { |
10514 | LHSOK = Visit(E->getLHS()); |
10515 | } |
10516 | if (!LHSOK && !Info.noteFailure()) |
10517 | return false; |
10518 | |
10519 | ComplexValue RHS; |
10520 | if (E->getRHS()->getType()->isRealFloatingType()) { |
10521 | RHSReal = true; |
10522 | APFloat &Real = RHS.FloatReal; |
10523 | if (!EvaluateFloat(E->getRHS(), Real, Info) || !LHSOK) |
10524 | return false; |
10525 | RHS.makeComplexFloat(); |
10526 | RHS.FloatImag = APFloat(Real.getSemantics()); |
10527 | } else if (!EvaluateComplex(E->getRHS(), RHS, Info) || !LHSOK) |
10528 | return false; |
10529 | |
10530 | (0) . __assert_fail ("!(LHSReal && RHSReal) && \"Cannot have both operands of a complex operation be real.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10531, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!(LHSReal && RHSReal) && |
10531 | (0) . __assert_fail ("!(LHSReal && RHSReal) && \"Cannot have both operands of a complex operation be real.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10531, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot have both operands of a complex operation be real."); |
10532 | switch (E->getOpcode()) { |
10533 | default: return Error(E); |
10534 | case BO_Add: |
10535 | if (Result.isComplexFloat()) { |
10536 | Result.getComplexFloatReal().add(RHS.getComplexFloatReal(), |
10537 | APFloat::rmNearestTiesToEven); |
10538 | if (LHSReal) |
10539 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
10540 | else if (!RHSReal) |
10541 | Result.getComplexFloatImag().add(RHS.getComplexFloatImag(), |
10542 | APFloat::rmNearestTiesToEven); |
10543 | } else { |
10544 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
10545 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
10546 | } |
10547 | break; |
10548 | case BO_Sub: |
10549 | if (Result.isComplexFloat()) { |
10550 | Result.getComplexFloatReal().subtract(RHS.getComplexFloatReal(), |
10551 | APFloat::rmNearestTiesToEven); |
10552 | if (LHSReal) { |
10553 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
10554 | Result.getComplexFloatImag().changeSign(); |
10555 | } else if (!RHSReal) { |
10556 | Result.getComplexFloatImag().subtract(RHS.getComplexFloatImag(), |
10557 | APFloat::rmNearestTiesToEven); |
10558 | } |
10559 | } else { |
10560 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
10561 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
10562 | } |
10563 | break; |
10564 | case BO_Mul: |
10565 | if (Result.isComplexFloat()) { |
10566 | |
10567 | |
10568 | |
10569 | |
10570 | ComplexValue LHS = Result; |
10571 | APFloat &A = LHS.getComplexFloatReal(); |
10572 | APFloat &B = LHS.getComplexFloatImag(); |
10573 | APFloat &C = RHS.getComplexFloatReal(); |
10574 | APFloat &D = RHS.getComplexFloatImag(); |
10575 | APFloat &ResR = Result.getComplexFloatReal(); |
10576 | APFloat &ResI = Result.getComplexFloatImag(); |
10577 | if (LHSReal) { |
10578 | (0) . __assert_fail ("!RHSReal && \"Cannot have two real operands for a complex op!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!RHSReal && "Cannot have two real operands for a complex op!"); |
10579 | ResR = A * C; |
10580 | ResI = A * D; |
10581 | } else if (RHSReal) { |
10582 | ResR = C * A; |
10583 | ResI = C * B; |
10584 | } else { |
10585 | |
10586 | |
10587 | APFloat AC = A * C; |
10588 | APFloat BD = B * D; |
10589 | APFloat AD = A * D; |
10590 | APFloat BC = B * C; |
10591 | ResR = AC - BD; |
10592 | ResI = AD + BC; |
10593 | if (ResR.isNaN() && ResI.isNaN()) { |
10594 | bool Recalc = false; |
10595 | if (A.isInfinity() || B.isInfinity()) { |
10596 | A = APFloat::copySign( |
10597 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
10598 | B = APFloat::copySign( |
10599 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
10600 | if (C.isNaN()) |
10601 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
10602 | if (D.isNaN()) |
10603 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
10604 | Recalc = true; |
10605 | } |
10606 | if (C.isInfinity() || D.isInfinity()) { |
10607 | C = APFloat::copySign( |
10608 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
10609 | D = APFloat::copySign( |
10610 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
10611 | if (A.isNaN()) |
10612 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
10613 | if (B.isNaN()) |
10614 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
10615 | Recalc = true; |
10616 | } |
10617 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
10618 | AD.isInfinity() || BC.isInfinity())) { |
10619 | if (A.isNaN()) |
10620 | A = APFloat::copySign(APFloat(A.getSemantics()), A); |
10621 | if (B.isNaN()) |
10622 | B = APFloat::copySign(APFloat(B.getSemantics()), B); |
10623 | if (C.isNaN()) |
10624 | C = APFloat::copySign(APFloat(C.getSemantics()), C); |
10625 | if (D.isNaN()) |
10626 | D = APFloat::copySign(APFloat(D.getSemantics()), D); |
10627 | Recalc = true; |
10628 | } |
10629 | if (Recalc) { |
10630 | ResR = APFloat::getInf(A.getSemantics()) * (A * C - B * D); |
10631 | ResI = APFloat::getInf(A.getSemantics()) * (A * D + B * C); |
10632 | } |
10633 | } |
10634 | } |
10635 | } else { |
10636 | ComplexValue LHS = Result; |
10637 | Result.getComplexIntReal() = |
10638 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
10639 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
10640 | Result.getComplexIntImag() = |
10641 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
10642 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
10643 | } |
10644 | break; |
10645 | case BO_Div: |
10646 | if (Result.isComplexFloat()) { |
10647 | |
10648 | |
10649 | |
10650 | |
10651 | ComplexValue LHS = Result; |
10652 | APFloat &A = LHS.getComplexFloatReal(); |
10653 | APFloat &B = LHS.getComplexFloatImag(); |
10654 | APFloat &C = RHS.getComplexFloatReal(); |
10655 | APFloat &D = RHS.getComplexFloatImag(); |
10656 | APFloat &ResR = Result.getComplexFloatReal(); |
10657 | APFloat &ResI = Result.getComplexFloatImag(); |
10658 | if (RHSReal) { |
10659 | ResR = A / C; |
10660 | ResI = B / C; |
10661 | } else { |
10662 | if (LHSReal) { |
10663 | |
10664 | B = APFloat::getZero(A.getSemantics()); |
10665 | } |
10666 | int DenomLogB = 0; |
10667 | APFloat MaxCD = maxnum(abs(C), abs(D)); |
10668 | if (MaxCD.isFinite()) { |
10669 | DenomLogB = ilogb(MaxCD); |
10670 | C = scalbn(C, -DenomLogB, APFloat::rmNearestTiesToEven); |
10671 | D = scalbn(D, -DenomLogB, APFloat::rmNearestTiesToEven); |
10672 | } |
10673 | APFloat Denom = C * C + D * D; |
10674 | ResR = scalbn((A * C + B * D) / Denom, -DenomLogB, |
10675 | APFloat::rmNearestTiesToEven); |
10676 | ResI = scalbn((B * C - A * D) / Denom, -DenomLogB, |
10677 | APFloat::rmNearestTiesToEven); |
10678 | if (ResR.isNaN() && ResI.isNaN()) { |
10679 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
10680 | ResR = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * A; |
10681 | ResI = APFloat::getInf(ResR.getSemantics(), C.isNegative()) * B; |
10682 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
10683 | D.isFinite()) { |
10684 | A = APFloat::copySign( |
10685 | APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), A); |
10686 | B = APFloat::copySign( |
10687 | APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), B); |
10688 | ResR = APFloat::getInf(ResR.getSemantics()) * (A * C + B * D); |
10689 | ResI = APFloat::getInf(ResI.getSemantics()) * (B * C - A * D); |
10690 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
10691 | C = APFloat::copySign( |
10692 | APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), C); |
10693 | D = APFloat::copySign( |
10694 | APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), D); |
10695 | ResR = APFloat::getZero(ResR.getSemantics()) * (A * C + B * D); |
10696 | ResI = APFloat::getZero(ResI.getSemantics()) * (B * C - A * D); |
10697 | } |
10698 | } |
10699 | } |
10700 | } else { |
10701 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
10702 | return Error(E, diag::note_expr_divide_by_zero); |
10703 | |
10704 | ComplexValue LHS = Result; |
10705 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
10706 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
10707 | Result.getComplexIntReal() = |
10708 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
10709 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
10710 | Result.getComplexIntImag() = |
10711 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
10712 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
10713 | } |
10714 | break; |
10715 | } |
10716 | |
10717 | return true; |
10718 | } |
10719 | |
10720 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
10721 | |
10722 | if (!Visit(E->getSubExpr())) |
10723 | return false; |
10724 | |
10725 | switch (E->getOpcode()) { |
10726 | default: |
10727 | return Error(E); |
10728 | case UO_Extension: |
10729 | return true; |
10730 | case UO_Plus: |
10731 | |
10732 | return true; |
10733 | case UO_Minus: |
10734 | if (Result.isComplexFloat()) { |
10735 | Result.getComplexFloatReal().changeSign(); |
10736 | Result.getComplexFloatImag().changeSign(); |
10737 | } |
10738 | else { |
10739 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
10740 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
10741 | } |
10742 | return true; |
10743 | case UO_Not: |
10744 | if (Result.isComplexFloat()) |
10745 | Result.getComplexFloatImag().changeSign(); |
10746 | else |
10747 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
10748 | return true; |
10749 | } |
10750 | } |
10751 | |
10752 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10753 | if (E->getNumInits() == 2) { |
10754 | if (E->getType()->isComplexType()) { |
10755 | Result.makeComplexFloat(); |
10756 | if (!EvaluateFloat(E->getInit(0), Result.FloatReal, Info)) |
10757 | return false; |
10758 | if (!EvaluateFloat(E->getInit(1), Result.FloatImag, Info)) |
10759 | return false; |
10760 | } else { |
10761 | Result.makeComplexInt(); |
10762 | if (!EvaluateInteger(E->getInit(0), Result.IntReal, Info)) |
10763 | return false; |
10764 | if (!EvaluateInteger(E->getInit(1), Result.IntImag, Info)) |
10765 | return false; |
10766 | } |
10767 | return true; |
10768 | } |
10769 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
10770 | } |
10771 | |
10772 | |
10773 | |
10774 | |
10775 | |
10776 | |
10777 | namespace { |
10778 | class AtomicExprEvaluator : |
10779 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
10780 | const LValue *This; |
10781 | APValue &Result; |
10782 | public: |
10783 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
10784 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
10785 | |
10786 | bool Success(const APValue &V, const Expr *E) { |
10787 | Result = V; |
10788 | return true; |
10789 | } |
10790 | |
10791 | bool ZeroInitialization(const Expr *E) { |
10792 | ImplicitValueInitExpr VIE( |
10793 | E->getType()->castAs<AtomicType>()->getValueType()); |
10794 | |
10795 | |
10796 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
10797 | : Evaluate(Result, Info, &VIE); |
10798 | } |
10799 | |
10800 | bool VisitCastExpr(const CastExpr *E) { |
10801 | switch (E->getCastKind()) { |
10802 | default: |
10803 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10804 | case CK_NonAtomicToAtomic: |
10805 | return This ? EvaluateInPlace(Result, Info, *This, E->getSubExpr()) |
10806 | : Evaluate(Result, Info, E->getSubExpr()); |
10807 | } |
10808 | } |
10809 | }; |
10810 | } |
10811 | |
10812 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
10813 | EvalInfo &Info) { |
10814 | isRValue() && E->getType()->isAtomicType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10814, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isAtomicType()); |
10815 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
10816 | } |
10817 | |
10818 | |
10819 | |
10820 | |
10821 | |
10822 | |
10823 | namespace { |
10824 | class VoidExprEvaluator |
10825 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
10826 | public: |
10827 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
10828 | |
10829 | bool Success(const APValue &V, const Expr *e) { return true; } |
10830 | |
10831 | bool ZeroInitialization(const Expr *E) { return true; } |
10832 | |
10833 | bool VisitCastExpr(const CastExpr *E) { |
10834 | switch (E->getCastKind()) { |
10835 | default: |
10836 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10837 | case CK_ToVoid: |
10838 | VisitIgnoredValue(E->getSubExpr()); |
10839 | return true; |
10840 | } |
10841 | } |
10842 | |
10843 | bool VisitCallExpr(const CallExpr *E) { |
10844 | switch (E->getBuiltinCallee()) { |
10845 | default: |
10846 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
10847 | case Builtin::BI__assume: |
10848 | case Builtin::BI__builtin_assume: |
10849 | |
10850 | return true; |
10851 | } |
10852 | } |
10853 | }; |
10854 | } |
10855 | |
10856 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
10857 | isRValue() && E->getType()->isVoidType()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10857, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(E->isRValue() && E->getType()->isVoidType()); |
10858 | return VoidExprEvaluator(Info).Visit(E); |
10859 | } |
10860 | |
10861 | |
10862 | |
10863 | |
10864 | |
10865 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
10866 | |
10867 | |
10868 | QualType T = E->getType(); |
10869 | if (E->isGLValue() || T->isFunctionType()) { |
10870 | LValue LV; |
10871 | if (!EvaluateLValue(E, LV, Info)) |
10872 | return false; |
10873 | LV.moveInto(Result); |
10874 | } else if (T->isVectorType()) { |
10875 | if (!EvaluateVector(E, Result, Info)) |
10876 | return false; |
10877 | } else if (T->isIntegralOrEnumerationType()) { |
10878 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
10879 | return false; |
10880 | } else if (T->hasPointerRepresentation()) { |
10881 | LValue LV; |
10882 | if (!EvaluatePointer(E, LV, Info)) |
10883 | return false; |
10884 | LV.moveInto(Result); |
10885 | } else if (T->isRealFloatingType()) { |
10886 | llvm::APFloat F(0.0); |
10887 | if (!EvaluateFloat(E, F, Info)) |
10888 | return false; |
10889 | Result = APValue(F); |
10890 | } else if (T->isAnyComplexType()) { |
10891 | ComplexValue C; |
10892 | if (!EvaluateComplex(E, C, Info)) |
10893 | return false; |
10894 | C.moveInto(Result); |
10895 | } else if (T->isFixedPointType()) { |
10896 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
10897 | } else if (T->isMemberPointerType()) { |
10898 | MemberPtr P; |
10899 | if (!EvaluateMemberPointer(E, P, Info)) |
10900 | return false; |
10901 | P.moveInto(Result); |
10902 | return true; |
10903 | } else if (T->isArrayType()) { |
10904 | LValue LV; |
10905 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
10906 | if (!EvaluateArray(E, LV, Value, Info)) |
10907 | return false; |
10908 | Result = Value; |
10909 | } else if (T->isRecordType()) { |
10910 | LValue LV; |
10911 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
10912 | if (!EvaluateRecord(E, LV, Value, Info)) |
10913 | return false; |
10914 | Result = Value; |
10915 | } else if (T->isVoidType()) { |
10916 | if (!Info.getLangOpts().CPlusPlus11) |
10917 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
10918 | << E->getType(); |
10919 | if (!EvaluateVoid(E, Info)) |
10920 | return false; |
10921 | } else if (T->isAtomicType()) { |
10922 | QualType Unqual = T.getAtomicUnqualifiedType(); |
10923 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
10924 | LValue LV; |
10925 | APValue &Value = createTemporary(E, false, LV, *Info.CurrentCall); |
10926 | if (!EvaluateAtomic(E, &LV, Value, Info)) |
10927 | return false; |
10928 | } else { |
10929 | if (!EvaluateAtomic(E, nullptr, Result, Info)) |
10930 | return false; |
10931 | } |
10932 | } else if (Info.getLangOpts().CPlusPlus11) { |
10933 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
10934 | return false; |
10935 | } else { |
10936 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
10937 | return false; |
10938 | } |
10939 | |
10940 | return true; |
10941 | } |
10942 | |
10943 | |
10944 | |
10945 | |
10946 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
10947 | const Expr *E, bool AllowNonLiteralTypes) { |
10948 | isValueDependent()", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 10948, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!E->isValueDependent()); |
10949 | |
10950 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, &This)) |
10951 | return false; |
10952 | |
10953 | if (E->isRValue()) { |
10954 | |
10955 | |
10956 | QualType T = E->getType(); |
10957 | if (T->isArrayType()) |
10958 | return EvaluateArray(E, This, Result, Info); |
10959 | else if (T->isRecordType()) |
10960 | return EvaluateRecord(E, This, Result, Info); |
10961 | else if (T->isAtomicType()) { |
10962 | QualType Unqual = T.getAtomicUnqualifiedType(); |
10963 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
10964 | return EvaluateAtomic(E, &This, Result, Info); |
10965 | } |
10966 | } |
10967 | |
10968 | |
10969 | return Evaluate(Result, Info, E); |
10970 | } |
10971 | |
10972 | |
10973 | |
10974 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
10975 | if (E->getType().isNull()) |
10976 | return false; |
10977 | |
10978 | if (!CheckLiteralType(Info, E)) |
10979 | return false; |
10980 | |
10981 | if (!::Evaluate(Result, Info, E)) |
10982 | return false; |
10983 | |
10984 | if (E->isGLValue()) { |
10985 | LValue LV; |
10986 | LV.setFrom(Info.Ctx, Result); |
10987 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
10988 | return false; |
10989 | } |
10990 | |
10991 | |
10992 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result); |
10993 | } |
10994 | |
10995 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
10996 | const ASTContext &Ctx, bool &IsConst) { |
10997 | |
10998 | |
10999 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Exp)) { |
11000 | Result.Val = APValue(APSInt(L->getValue(), |
11001 | L->getType()->isUnsignedIntegerType())); |
11002 | IsConst = true; |
11003 | return true; |
11004 | } |
11005 | |
11006 | |
11007 | |
11008 | if (Exp->getType().isNull()) { |
11009 | IsConst = false; |
11010 | return true; |
11011 | } |
11012 | |
11013 | |
11014 | |
11015 | if (Exp->isRValue() && (Exp->getType()->isArrayType() || |
11016 | Exp->getType()->isRecordType()) && |
11017 | !Ctx.getLangOpts().CPlusPlus11) { |
11018 | IsConst = false; |
11019 | return true; |
11020 | } |
11021 | return false; |
11022 | } |
11023 | |
11024 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
11025 | Expr::SideEffectsKind SEK) { |
11026 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
11027 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
11028 | } |
11029 | |
11030 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
11031 | const ASTContext &Ctx, EvalInfo &Info) { |
11032 | bool IsConst; |
11033 | if (FastEvaluateAsRValue(E, Result, Ctx, IsConst)) |
11034 | return IsConst; |
11035 | |
11036 | return EvaluateAsRValue(Info, E, Result.Val); |
11037 | } |
11038 | |
11039 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
11040 | const ASTContext &Ctx, |
11041 | Expr::SideEffectsKind AllowSideEffects, |
11042 | EvalInfo &Info) { |
11043 | if (!E->getType()->isIntegralOrEnumerationType()) |
11044 | return false; |
11045 | |
11046 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info) || |
11047 | !ExprResult.Val.isInt() || |
11048 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
11049 | return false; |
11050 | |
11051 | return true; |
11052 | } |
11053 | |
11054 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
11055 | const ASTContext &Ctx, |
11056 | Expr::SideEffectsKind AllowSideEffects, |
11057 | EvalInfo &Info) { |
11058 | if (!E->getType()->isFixedPointType()) |
11059 | return false; |
11060 | |
11061 | if (!::EvaluateAsRValue(E, ExprResult, Ctx, Info)) |
11062 | return false; |
11063 | |
11064 | if (!ExprResult.Val.isFixedPoint() || |
11065 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
11066 | return false; |
11067 | |
11068 | return true; |
11069 | } |
11070 | |
11071 | |
11072 | |
11073 | |
11074 | |
11075 | |
11076 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
11077 | bool InConstantContext) const { |
11078 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
11079 | Info.InConstantContext = InConstantContext; |
11080 | return ::EvaluateAsRValue(this, Result, Ctx, Info); |
11081 | } |
11082 | |
11083 | bool Expr::EvaluateAsBooleanCondition(bool &Result, |
11084 | const ASTContext &Ctx) const { |
11085 | EvalResult Scratch; |
11086 | return EvaluateAsRValue(Scratch, Ctx) && |
11087 | HandleConversionToBool(Scratch.Val, Result); |
11088 | } |
11089 | |
11090 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
11091 | SideEffectsKind AllowSideEffects) const { |
11092 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
11093 | return ::EvaluateAsInt(this, Result, Ctx, AllowSideEffects, Info); |
11094 | } |
11095 | |
11096 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
11097 | SideEffectsKind AllowSideEffects) const { |
11098 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
11099 | return ::EvaluateAsFixedPoint(this, Result, Ctx, AllowSideEffects, Info); |
11100 | } |
11101 | |
11102 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
11103 | SideEffectsKind AllowSideEffects) const { |
11104 | if (!getType()->isRealFloatingType()) |
11105 | return false; |
11106 | |
11107 | EvalResult ExprResult; |
11108 | if (!EvaluateAsRValue(ExprResult, Ctx) || !ExprResult.Val.isFloat() || |
11109 | hasUnacceptableSideEffect(ExprResult, AllowSideEffects)) |
11110 | return false; |
11111 | |
11112 | Result = ExprResult.Val.getFloat(); |
11113 | return true; |
11114 | } |
11115 | |
11116 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx) const { |
11117 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
11118 | |
11119 | LValue LV; |
11120 | if (!EvaluateLValue(this, LV, Info) || Result.HasSideEffects || |
11121 | !CheckLValueConstantExpression(Info, getExprLoc(), |
11122 | Ctx.getLValueReferenceType(getType()), LV, |
11123 | Expr::EvaluateForCodeGen)) |
11124 | return false; |
11125 | |
11126 | LV.moveInto(Result.Val); |
11127 | return true; |
11128 | } |
11129 | |
11130 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, ConstExprUsage Usage, |
11131 | const ASTContext &Ctx) const { |
11132 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
11133 | EvalInfo Info(Ctx, Result, EM); |
11134 | Info.InConstantContext = true; |
11135 | if (!::Evaluate(Result.Val, Info, this)) |
11136 | return false; |
11137 | |
11138 | return CheckConstantExpression(Info, getExprLoc(), getType(), Result.Val, |
11139 | Usage); |
11140 | } |
11141 | |
11142 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
11143 | const VarDecl *VD, |
11144 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
11145 | |
11146 | |
11147 | if (isRValue() && (getType()->isArrayType() || getType()->isRecordType()) && |
11148 | !Ctx.getLangOpts().CPlusPlus11) |
11149 | return false; |
11150 | |
11151 | Expr::EvalStatus EStatus; |
11152 | EStatus.Diag = &Notes; |
11153 | |
11154 | EvalInfo InitInfo(Ctx, EStatus, VD->isConstexpr() |
11155 | ? EvalInfo::EM_ConstantExpression |
11156 | : EvalInfo::EM_ConstantFold); |
11157 | InitInfo.setEvaluatingDecl(VD, Value); |
11158 | InitInfo.InConstantContext = true; |
11159 | |
11160 | LValue LVal; |
11161 | LVal.set(VD); |
11162 | |
11163 | |
11164 | |
11165 | |
11166 | |
11167 | if (Ctx.getLangOpts().CPlusPlus && !VD->hasLocalStorage() && |
11168 | !VD->getType()->isReferenceType()) { |
11169 | ImplicitValueInitExpr VIE(VD->getType()); |
11170 | if (!EvaluateInPlace(Value, InitInfo, LVal, &VIE, |
11171 | )) |
11172 | return false; |
11173 | } |
11174 | |
11175 | if (!EvaluateInPlace(Value, InitInfo, LVal, this, |
11176 | ) || |
11177 | EStatus.HasSideEffects) |
11178 | return false; |
11179 | |
11180 | return CheckConstantExpression(InitInfo, VD->getLocation(), VD->getType(), |
11181 | Value); |
11182 | } |
11183 | |
11184 | |
11185 | |
11186 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
11187 | EvalResult Result; |
11188 | return EvaluateAsRValue(Result, Ctx, true) && |
11189 | !hasUnacceptableSideEffect(Result, SEK); |
11190 | } |
11191 | |
11192 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
11193 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
11194 | EvalResult EVResult; |
11195 | EVResult.Diag = Diag; |
11196 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
11197 | Info.InConstantContext = true; |
11198 | |
11199 | bool Result = ::EvaluateAsRValue(this, EVResult, Ctx, Info); |
11200 | (void)Result; |
11201 | (0) . __assert_fail ("Result && \"Could not evaluate expression\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11201, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result && "Could not evaluate expression"); |
11202 | (0) . __assert_fail ("EVResult.Val.isInt() && \"Expression did not evaluate to integer\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11202, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
11203 | |
11204 | return EVResult.Val.getInt(); |
11205 | } |
11206 | |
11207 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
11208 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
11209 | EvalResult EVResult; |
11210 | EVResult.Diag = Diag; |
11211 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_EvaluateForOverflow); |
11212 | Info.InConstantContext = true; |
11213 | |
11214 | bool Result = ::EvaluateAsRValue(Info, this, EVResult.Val); |
11215 | (void)Result; |
11216 | (0) . __assert_fail ("Result && \"Could not evaluate expression\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11216, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result && "Could not evaluate expression"); |
11217 | (0) . __assert_fail ("EVResult.Val.isInt() && \"Expression did not evaluate to integer\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11217, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EVResult.Val.isInt() && "Expression did not evaluate to integer"); |
11218 | |
11219 | return EVResult.Val.getInt(); |
11220 | } |
11221 | |
11222 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
11223 | bool IsConst; |
11224 | EvalResult EVResult; |
11225 | if (!FastEvaluateAsRValue(this, EVResult, Ctx, IsConst)) { |
11226 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_EvaluateForOverflow); |
11227 | (void)::EvaluateAsRValue(Info, this, EVResult.Val); |
11228 | } |
11229 | } |
11230 | |
11231 | bool Expr::EvalResult::isGlobalLValue() const { |
11232 | assert(Val.isLValue()); |
11233 | return IsGlobalLValue(Val.getLValueBase()); |
11234 | } |
11235 | |
11236 | |
11237 | |
11238 | |
11239 | |
11240 | |
11241 | |
11242 | |
11243 | |
11244 | |
11245 | |
11246 | |
11247 | |
11248 | |
11249 | |
11250 | |
11251 | |
11252 | namespace { |
11253 | |
11254 | enum ICEKind { |
11255 | |
11256 | IK_ICE, |
11257 | |
11258 | |
11259 | |
11260 | IK_ICEIfUnevaluated, |
11261 | |
11262 | IK_NotICE |
11263 | }; |
11264 | |
11265 | struct ICEDiag { |
11266 | ICEKind Kind; |
11267 | SourceLocation Loc; |
11268 | |
11269 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
11270 | }; |
11271 | |
11272 | } |
11273 | |
11274 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
11275 | |
11276 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
11277 | |
11278 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
11279 | Expr::EvalResult EVResult; |
11280 | Expr::EvalStatus Status; |
11281 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
11282 | |
11283 | Info.InConstantContext = true; |
11284 | if (!::EvaluateAsRValue(E, EVResult, Ctx, Info) || EVResult.HasSideEffects || |
11285 | !EVResult.Val.isInt()) |
11286 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11287 | |
11288 | return NoDiag(); |
11289 | } |
11290 | |
11291 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
11292 | (0) . __assert_fail ("!E->isValueDependent() && \"Should not see value dependent exprs!\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11292, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
11293 | if (!E->getType()->isIntegralOrEnumerationType()) |
11294 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11295 | |
11296 | switch (E->getStmtClass()) { |
11297 | #define ABSTRACT_STMT(Node) |
11298 | #define STMT(Node, Base) case Expr::Node##Class: |
11299 | #define EXPR(Node, Base) |
11300 | #include "clang/AST/StmtNodes.inc" |
11301 | case Expr::PredefinedExprClass: |
11302 | case Expr::FloatingLiteralClass: |
11303 | case Expr::ImaginaryLiteralClass: |
11304 | case Expr::StringLiteralClass: |
11305 | case Expr::ArraySubscriptExprClass: |
11306 | case Expr::OMPArraySectionExprClass: |
11307 | case Expr::MemberExprClass: |
11308 | case Expr::CompoundAssignOperatorClass: |
11309 | case Expr::CompoundLiteralExprClass: |
11310 | case Expr::ExtVectorElementExprClass: |
11311 | case Expr::DesignatedInitExprClass: |
11312 | case Expr::ArrayInitLoopExprClass: |
11313 | case Expr::ArrayInitIndexExprClass: |
11314 | case Expr::NoInitExprClass: |
11315 | case Expr::DesignatedInitUpdateExprClass: |
11316 | case Expr::ImplicitValueInitExprClass: |
11317 | case Expr::ParenListExprClass: |
11318 | case Expr::VAArgExprClass: |
11319 | case Expr::AddrLabelExprClass: |
11320 | case Expr::StmtExprClass: |
11321 | case Expr::CXXMemberCallExprClass: |
11322 | case Expr::CUDAKernelCallExprClass: |
11323 | case Expr::CXXDynamicCastExprClass: |
11324 | case Expr::CXXTypeidExprClass: |
11325 | case Expr::CXXUuidofExprClass: |
11326 | case Expr::MSPropertyRefExprClass: |
11327 | case Expr::MSPropertySubscriptExprClass: |
11328 | case Expr::CXXNullPtrLiteralExprClass: |
11329 | case Expr::UserDefinedLiteralClass: |
11330 | case Expr::CXXThisExprClass: |
11331 | case Expr::CXXThrowExprClass: |
11332 | case Expr::CXXNewExprClass: |
11333 | case Expr::CXXDeleteExprClass: |
11334 | case Expr::CXXPseudoDestructorExprClass: |
11335 | case Expr::UnresolvedLookupExprClass: |
11336 | case Expr::TypoExprClass: |
11337 | case Expr::DependentScopeDeclRefExprClass: |
11338 | case Expr::CXXConstructExprClass: |
11339 | case Expr::CXXInheritedCtorInitExprClass: |
11340 | case Expr::CXXStdInitializerListExprClass: |
11341 | case Expr::CXXBindTemporaryExprClass: |
11342 | case Expr::ExprWithCleanupsClass: |
11343 | case Expr::CXXTemporaryObjectExprClass: |
11344 | case Expr::CXXUnresolvedConstructExprClass: |
11345 | case Expr::CXXDependentScopeMemberExprClass: |
11346 | case Expr::UnresolvedMemberExprClass: |
11347 | case Expr::ObjCStringLiteralClass: |
11348 | case Expr::ObjCBoxedExprClass: |
11349 | case Expr::ObjCArrayLiteralClass: |
11350 | case Expr::ObjCDictionaryLiteralClass: |
11351 | case Expr::ObjCEncodeExprClass: |
11352 | case Expr::ObjCMessageExprClass: |
11353 | case Expr::ObjCSelectorExprClass: |
11354 | case Expr::ObjCProtocolExprClass: |
11355 | case Expr::ObjCIvarRefExprClass: |
11356 | case Expr::ObjCPropertyRefExprClass: |
11357 | case Expr::ObjCSubscriptRefExprClass: |
11358 | case Expr::ObjCIsaExprClass: |
11359 | case Expr::ObjCAvailabilityCheckExprClass: |
11360 | case Expr::ShuffleVectorExprClass: |
11361 | case Expr::ConvertVectorExprClass: |
11362 | case Expr::BlockExprClass: |
11363 | case Expr::NoStmtClass: |
11364 | case Expr::OpaqueValueExprClass: |
11365 | case Expr::PackExpansionExprClass: |
11366 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
11367 | case Expr::FunctionParmPackExprClass: |
11368 | case Expr::AsTypeExprClass: |
11369 | case Expr::ObjCIndirectCopyRestoreExprClass: |
11370 | case Expr::MaterializeTemporaryExprClass: |
11371 | case Expr::PseudoObjectExprClass: |
11372 | case Expr::AtomicExprClass: |
11373 | case Expr::LambdaExprClass: |
11374 | case Expr::CXXFoldExprClass: |
11375 | case Expr::CoawaitExprClass: |
11376 | case Expr::DependentCoawaitExprClass: |
11377 | case Expr::CoyieldExprClass: |
11378 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11379 | |
11380 | case Expr::InitListExprClass: { |
11381 | |
11382 | |
11383 | |
11384 | |
11385 | if (E->isRValue()) |
11386 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
11387 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
11388 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11389 | } |
11390 | |
11391 | case Expr::SizeOfPackExprClass: |
11392 | case Expr::GNUNullExprClass: |
11393 | |
11394 | return NoDiag(); |
11395 | |
11396 | case Expr::SubstNonTypeTemplateParmExprClass: |
11397 | return |
11398 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
11399 | |
11400 | case Expr::ConstantExprClass: |
11401 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
11402 | |
11403 | case Expr::ParenExprClass: |
11404 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
11405 | case Expr::GenericSelectionExprClass: |
11406 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
11407 | case Expr::IntegerLiteralClass: |
11408 | case Expr::FixedPointLiteralClass: |
11409 | case Expr::CharacterLiteralClass: |
11410 | case Expr::ObjCBoolLiteralExprClass: |
11411 | case Expr::CXXBoolLiteralExprClass: |
11412 | case Expr::CXXScalarValueInitExprClass: |
11413 | case Expr::TypeTraitExprClass: |
11414 | case Expr::ArrayTypeTraitExprClass: |
11415 | case Expr::ExpressionTraitExprClass: |
11416 | case Expr::CXXNoexceptExprClass: |
11417 | return NoDiag(); |
11418 | case Expr::CallExprClass: |
11419 | case Expr::CXXOperatorCallExprClass: { |
11420 | |
11421 | |
11422 | |
11423 | const CallExpr *CE = cast<CallExpr>(E); |
11424 | if (CE->getBuiltinCallee()) |
11425 | return CheckEvalInICE(E, Ctx); |
11426 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11427 | } |
11428 | case Expr::DeclRefExprClass: { |
11429 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
11430 | return NoDiag(); |
11431 | const ValueDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
11432 | if (Ctx.getLangOpts().CPlusPlus && |
11433 | D && IsConstNonVolatile(D->getType())) { |
11434 | |
11435 | |
11436 | |
11437 | if (isa<ParmVarDecl>(D)) |
11438 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
11439 | |
11440 | |
11441 | |
11442 | |
11443 | if (const VarDecl *Dcl = dyn_cast<VarDecl>(D)) { |
11444 | if (!Dcl->getType()->isIntegralOrEnumerationType()) |
11445 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
11446 | |
11447 | const VarDecl *VD; |
11448 | |
11449 | |
11450 | if (Dcl->getAnyInitializer(VD) && VD->checkInitIsICE()) |
11451 | return NoDiag(); |
11452 | else |
11453 | return ICEDiag(IK_NotICE, cast<DeclRefExpr>(E)->getLocation()); |
11454 | } |
11455 | } |
11456 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11457 | } |
11458 | case Expr::UnaryOperatorClass: { |
11459 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
11460 | switch (Exp->getOpcode()) { |
11461 | case UO_PostInc: |
11462 | case UO_PostDec: |
11463 | case UO_PreInc: |
11464 | case UO_PreDec: |
11465 | case UO_AddrOf: |
11466 | case UO_Deref: |
11467 | case UO_Coawait: |
11468 | |
11469 | |
11470 | |
11471 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11472 | case UO_Extension: |
11473 | case UO_LNot: |
11474 | case UO_Plus: |
11475 | case UO_Minus: |
11476 | case UO_Not: |
11477 | case UO_Real: |
11478 | case UO_Imag: |
11479 | return CheckICE(Exp->getSubExpr(), Ctx); |
11480 | } |
11481 | llvm_unreachable("invalid unary operator class"); |
11482 | } |
11483 | case Expr::OffsetOfExprClass: { |
11484 | |
11485 | |
11486 | |
11487 | |
11488 | |
11489 | |
11490 | return CheckEvalInICE(E, Ctx); |
11491 | } |
11492 | case Expr::UnaryExprOrTypeTraitExprClass: { |
11493 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
11494 | if ((Exp->getKind() == UETT_SizeOf) && |
11495 | Exp->getTypeOfArgument()->isVariableArrayType()) |
11496 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11497 | return NoDiag(); |
11498 | } |
11499 | case Expr::BinaryOperatorClass: { |
11500 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
11501 | switch (Exp->getOpcode()) { |
11502 | case BO_PtrMemD: |
11503 | case BO_PtrMemI: |
11504 | case BO_Assign: |
11505 | case BO_MulAssign: |
11506 | case BO_DivAssign: |
11507 | case BO_RemAssign: |
11508 | case BO_AddAssign: |
11509 | case BO_SubAssign: |
11510 | case BO_ShlAssign: |
11511 | case BO_ShrAssign: |
11512 | case BO_AndAssign: |
11513 | case BO_XorAssign: |
11514 | case BO_OrAssign: |
11515 | |
11516 | |
11517 | |
11518 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11519 | |
11520 | case BO_Mul: |
11521 | case BO_Div: |
11522 | case BO_Rem: |
11523 | case BO_Add: |
11524 | case BO_Sub: |
11525 | case BO_Shl: |
11526 | case BO_Shr: |
11527 | case BO_LT: |
11528 | case BO_GT: |
11529 | case BO_LE: |
11530 | case BO_GE: |
11531 | case BO_EQ: |
11532 | case BO_NE: |
11533 | case BO_And: |
11534 | case BO_Xor: |
11535 | case BO_Or: |
11536 | case BO_Comma: |
11537 | case BO_Cmp: { |
11538 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
11539 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
11540 | if (Exp->getOpcode() == BO_Div || |
11541 | Exp->getOpcode() == BO_Rem) { |
11542 | |
11543 | |
11544 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
11545 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
11546 | if (REval == 0) |
11547 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
11548 | if (REval.isSigned() && REval.isAllOnesValue()) { |
11549 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
11550 | if (LEval.isMinSignedValue()) |
11551 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
11552 | } |
11553 | } |
11554 | } |
11555 | if (Exp->getOpcode() == BO_Comma) { |
11556 | if (Ctx.getLangOpts().C99) { |
11557 | |
11558 | |
11559 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
11560 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
11561 | } else { |
11562 | |
11563 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11564 | } |
11565 | } |
11566 | return Worst(LHSResult, RHSResult); |
11567 | } |
11568 | case BO_LAnd: |
11569 | case BO_LOr: { |
11570 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
11571 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
11572 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
11573 | |
11574 | |
11575 | |
11576 | if ((Exp->getOpcode() == BO_LAnd) != |
11577 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
11578 | return RHSResult; |
11579 | return NoDiag(); |
11580 | } |
11581 | |
11582 | return Worst(LHSResult, RHSResult); |
11583 | } |
11584 | } |
11585 | llvm_unreachable("invalid binary operator kind"); |
11586 | } |
11587 | case Expr::ImplicitCastExprClass: |
11588 | case Expr::CStyleCastExprClass: |
11589 | case Expr::CXXFunctionalCastExprClass: |
11590 | case Expr::CXXStaticCastExprClass: |
11591 | case Expr::CXXReinterpretCastExprClass: |
11592 | case Expr::CXXConstCastExprClass: |
11593 | case Expr::ObjCBridgedCastExprClass: { |
11594 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
11595 | if (isa<ExplicitCastExpr>(E)) { |
11596 | if (const FloatingLiteral *FL |
11597 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
11598 | unsigned DestWidth = Ctx.getIntWidth(E->getType()); |
11599 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
11600 | APSInt IgnoredVal(DestWidth, !DestSigned); |
11601 | bool Ignored; |
11602 | |
11603 | |
11604 | |
11605 | if (FL->getValue().convertToInteger(IgnoredVal, |
11606 | llvm::APFloat::rmTowardZero, |
11607 | &Ignored) & APFloat::opInvalidOp) |
11608 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11609 | return NoDiag(); |
11610 | } |
11611 | } |
11612 | switch (cast<CastExpr>(E)->getCastKind()) { |
11613 | case CK_LValueToRValue: |
11614 | case CK_AtomicToNonAtomic: |
11615 | case CK_NonAtomicToAtomic: |
11616 | case CK_NoOp: |
11617 | case CK_IntegralToBoolean: |
11618 | case CK_IntegralCast: |
11619 | return CheckICE(SubExpr, Ctx); |
11620 | default: |
11621 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
11622 | } |
11623 | } |
11624 | case Expr::BinaryConditionalOperatorClass: { |
11625 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
11626 | ICEDiag CommonResult = CheckICE(Exp->getCommon(), Ctx); |
11627 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
11628 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
11629 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
11630 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
11631 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
11632 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
11633 | return FalseResult; |
11634 | } |
11635 | case Expr::ConditionalOperatorClass: { |
11636 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
11637 | |
11638 | |
11639 | |
11640 | |
11641 | if (const CallExpr *CallCE |
11642 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
11643 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
11644 | return CheckEvalInICE(E, Ctx); |
11645 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
11646 | if (CondResult.Kind == IK_NotICE) |
11647 | return CondResult; |
11648 | |
11649 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
11650 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
11651 | |
11652 | if (TrueResult.Kind == IK_NotICE) |
11653 | return TrueResult; |
11654 | if (FalseResult.Kind == IK_NotICE) |
11655 | return FalseResult; |
11656 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
11657 | return CondResult; |
11658 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
11659 | return NoDiag(); |
11660 | |
11661 | |
11662 | |
11663 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
11664 | return FalseResult; |
11665 | return TrueResult; |
11666 | } |
11667 | case Expr::CXXDefaultArgExprClass: |
11668 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
11669 | case Expr::CXXDefaultInitExprClass: |
11670 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
11671 | case Expr::ChooseExprClass: { |
11672 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
11673 | } |
11674 | } |
11675 | |
11676 | llvm_unreachable("Invalid StmtClass!"); |
11677 | } |
11678 | |
11679 | |
11680 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
11681 | const Expr *E, |
11682 | llvm::APSInt *Value, |
11683 | SourceLocation *Loc) { |
11684 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
11685 | if (Loc) *Loc = E->getExprLoc(); |
11686 | return false; |
11687 | } |
11688 | |
11689 | APValue Result; |
11690 | if (!E->isCXX11ConstantExpr(Ctx, &Result, Loc)) |
11691 | return false; |
11692 | |
11693 | if (!Result.isInt()) { |
11694 | if (Loc) *Loc = E->getExprLoc(); |
11695 | return false; |
11696 | } |
11697 | |
11698 | if (Value) *Value = Result.getInt(); |
11699 | return true; |
11700 | } |
11701 | |
11702 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
11703 | SourceLocation *Loc) const { |
11704 | if (Ctx.getLangOpts().CPlusPlus11) |
11705 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, nullptr, Loc); |
11706 | |
11707 | ICEDiag D = CheckICE(this, Ctx); |
11708 | if (D.Kind != IK_ICE) { |
11709 | if (Loc) *Loc = D.Loc; |
11710 | return false; |
11711 | } |
11712 | return true; |
11713 | } |
11714 | |
11715 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Value, const ASTContext &Ctx, |
11716 | SourceLocation *Loc, bool isEvaluated) const { |
11717 | if (Ctx.getLangOpts().CPlusPlus11) |
11718 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, this, &Value, Loc); |
11719 | |
11720 | if (!isIntegerConstantExpr(Ctx, Loc)) |
11721 | return false; |
11722 | |
11723 | |
11724 | |
11725 | |
11726 | |
11727 | EvalResult ExprResult; |
11728 | Expr::EvalStatus Status; |
11729 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
11730 | Info.InConstantContext = true; |
11731 | |
11732 | if (!::EvaluateAsInt(this, ExprResult, Ctx, SE_AllowSideEffects, Info)) |
11733 | llvm_unreachable("ICE cannot be evaluated!"); |
11734 | |
11735 | Value = ExprResult.Val.getInt(); |
11736 | return true; |
11737 | } |
11738 | |
11739 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
11740 | return CheckICE(this, Ctx).Kind == IK_ICE; |
11741 | } |
11742 | |
11743 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
11744 | SourceLocation *Loc) const { |
11745 | |
11746 | |
11747 | assert(Ctx.getLangOpts().CPlusPlus); |
11748 | |
11749 | |
11750 | Expr::EvalStatus Status; |
11751 | SmallVector<PartialDiagnosticAt, 8> Diags; |
11752 | Status.Diag = &Diags; |
11753 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
11754 | |
11755 | APValue Scratch; |
11756 | bool IsConstExpr = ::EvaluateAsRValue(Info, this, Result ? *Result : Scratch); |
11757 | |
11758 | if (!Diags.empty()) { |
11759 | IsConstExpr = false; |
11760 | if (Loc) *Loc = Diags[0].first; |
11761 | } else if (!IsConstExpr) { |
11762 | |
11763 | if (Loc) *Loc = getExprLoc(); |
11764 | } |
11765 | |
11766 | return IsConstExpr; |
11767 | } |
11768 | |
11769 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
11770 | const FunctionDecl *Callee, |
11771 | ArrayRef<const Expr*> Args, |
11772 | const Expr *This) const { |
11773 | Expr::EvalStatus Status; |
11774 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
11775 | Info.InConstantContext = true; |
11776 | |
11777 | LValue ThisVal; |
11778 | const LValue *ThisPtr = nullptr; |
11779 | if (This) { |
11780 | #ifndef NDEBUG |
11781 | auto *MD = dyn_cast<CXXMethodDecl>(Callee); |
11782 | (0) . __assert_fail ("MD && \"Don't provide `this` for non-methods.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11782, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD && "Don't provide `this` for non-methods."); |
11783 | (0) . __assert_fail ("!MD->isStatic() && \"Don't provide `this` for static methods.\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11783, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!MD->isStatic() && "Don't provide `this` for static methods."); |
11784 | #endif |
11785 | if (EvaluateObjectArgument(Info, This, ThisVal)) |
11786 | ThisPtr = &ThisVal; |
11787 | if (Info.EvalStatus.HasSideEffects) |
11788 | return false; |
11789 | } |
11790 | |
11791 | ArgVector ArgValues(Args.size()); |
11792 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
11793 | I != E; ++I) { |
11794 | if ((*I)->isValueDependent() || |
11795 | !Evaluate(ArgValues[I - Args.begin()], Info, *I)) |
11796 | |
11797 | ArgValues[I - Args.begin()] = APValue(); |
11798 | if (Info.EvalStatus.HasSideEffects) |
11799 | return false; |
11800 | } |
11801 | |
11802 | |
11803 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, |
11804 | ArgValues.data()); |
11805 | return Evaluate(Value, Info, this) && !Info.EvalStatus.HasSideEffects; |
11806 | } |
11807 | |
11808 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
11809 | SmallVectorImpl< |
11810 | PartialDiagnosticAt> &Diags) { |
11811 | |
11812 | |
11813 | |
11814 | if (FD->isDependentContext()) |
11815 | return true; |
11816 | |
11817 | Expr::EvalStatus Status; |
11818 | Status.Diag = &Diags; |
11819 | |
11820 | EvalInfo Info(FD->getASTContext(), Status, |
11821 | EvalInfo::EM_PotentialConstantExpression); |
11822 | Info.InConstantContext = true; |
11823 | |
11824 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
11825 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
11826 | |
11827 | |
11828 | |
11829 | LValue This; |
11830 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
11831 | This.set({&VIE, Info.CurrentCall->Index}); |
11832 | |
11833 | ArrayRef<const Expr*> Args; |
11834 | |
11835 | APValue Scratch; |
11836 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) { |
11837 | |
11838 | |
11839 | Info.setEvaluatingDecl(This.getLValueBase(), Scratch); |
11840 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
11841 | } else { |
11842 | SourceLocation Loc = FD->getLocation(); |
11843 | HandleFunctionCall(Loc, FD, (MD && MD->isInstance()) ? &This : nullptr, |
11844 | Args, FD->getBody(), Info, Scratch, nullptr); |
11845 | } |
11846 | |
11847 | return Diags.empty(); |
11848 | } |
11849 | |
11850 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
11851 | const FunctionDecl *FD, |
11852 | SmallVectorImpl< |
11853 | PartialDiagnosticAt> &Diags) { |
11854 | Expr::EvalStatus Status; |
11855 | Status.Diag = &Diags; |
11856 | |
11857 | EvalInfo Info(FD->getASTContext(), Status, |
11858 | EvalInfo::EM_PotentialConstantExpressionUnevaluated); |
11859 | Info.InConstantContext = true; |
11860 | |
11861 | |
11862 | ArrayRef<const Expr*> Args; |
11863 | ArgVector ArgValues(0); |
11864 | bool Success = EvaluateArgs(Args, ArgValues, Info); |
11865 | (void)Success; |
11866 | (0) . __assert_fail ("Success && \"Failed to set up arguments for potential constant evaluation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11867, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Success && |
11867 | (0) . __assert_fail ("Success && \"Failed to set up arguments for potential constant evaluation\"", "/home/seafit/code_projects/clang_source/clang/lib/AST/ExprConstant.cpp", 11867, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Failed to set up arguments for potential constant evaluation"); |
11868 | CallStackFrame Frame(Info, SourceLocation(), FD, nullptr, ArgValues.data()); |
11869 | |
11870 | APValue ResultScratch; |
11871 | Evaluate(ResultScratch, Info, E); |
11872 | return Diags.empty(); |
11873 | } |
11874 | |
11875 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
11876 | unsigned Type) const { |
11877 | if (!getType()->isPointerType()) |
11878 | return false; |
11879 | |
11880 | Expr::EvalStatus Status; |
11881 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
11882 | return tryEvaluateBuiltinObjectSize(this, Type, Info, Result); |
11883 | } |
11884 | |