1 | //===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements the Expr constant evaluator. |
10 | // |
11 | // Constant expression evaluation produces four main results: |
12 | // |
13 | // * A success/failure flag indicating whether constant folding was successful. |
14 | // This is the 'bool' return value used by most of the code in this file. A |
15 | // 'false' return value indicates that constant folding has failed, and any |
16 | // appropriate diagnostic has already been produced. |
17 | // |
18 | // * An evaluated result, valid only if constant folding has not failed. |
19 | // |
20 | // * A flag indicating if evaluation encountered (unevaluated) side-effects. |
21 | // These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1), |
22 | // where it is possible to determine the evaluated result regardless. |
23 | // |
24 | // * A set of notes indicating why the evaluation was not a constant expression |
25 | // (under the C++11 / C++1y rules only, at the moment), or, if folding failed |
26 | // too, why the expression could not be folded. |
27 | // |
28 | // If we are checking for a potential constant expression, failure to constant |
29 | // fold a potential constant sub-expression will be indicated by a 'false' |
30 | // return value (the expression could not be folded) and no diagnostic (the |
31 | // expression is not necessarily non-constant). |
32 | // |
33 | //===----------------------------------------------------------------------===// |
34 | |
35 | #include "ExprConstShared.h" |
36 | #include "Interp/Context.h" |
37 | #include "Interp/Frame.h" |
38 | #include "Interp/State.h" |
39 | #include "clang/AST/APValue.h" |
40 | #include "clang/AST/ASTContext.h" |
41 | #include "clang/AST/ASTDiagnostic.h" |
42 | #include "clang/AST/ASTLambda.h" |
43 | #include "clang/AST/Attr.h" |
44 | #include "clang/AST/CXXInheritance.h" |
45 | #include "clang/AST/CharUnits.h" |
46 | #include "clang/AST/CurrentSourceLocExprScope.h" |
47 | #include "clang/AST/Expr.h" |
48 | #include "clang/AST/OSLog.h" |
49 | #include "clang/AST/OptionalDiagnostic.h" |
50 | #include "clang/AST/RecordLayout.h" |
51 | #include "clang/AST/StmtVisitor.h" |
52 | #include "clang/AST/TypeLoc.h" |
53 | #include "clang/Basic/Builtins.h" |
54 | #include "clang/Basic/DiagnosticSema.h" |
55 | #include "clang/Basic/TargetInfo.h" |
56 | #include "llvm/ADT/APFixedPoint.h" |
57 | #include "llvm/ADT/SmallBitVector.h" |
58 | #include "llvm/ADT/StringExtras.h" |
59 | #include "llvm/Support/Debug.h" |
60 | #include "llvm/Support/SaveAndRestore.h" |
61 | #include "llvm/Support/TimeProfiler.h" |
62 | #include "llvm/Support/raw_ostream.h" |
63 | #include <cstring> |
64 | #include <functional> |
65 | #include <optional> |
66 | |
67 | #define DEBUG_TYPE "exprconstant" |
68 | |
69 | using namespace clang; |
70 | using llvm::APFixedPoint; |
71 | using llvm::APInt; |
72 | using llvm::APSInt; |
73 | using llvm::APFloat; |
74 | using llvm::FixedPointSemantics; |
75 | |
76 | namespace { |
77 | struct LValue; |
78 | class CallStackFrame; |
79 | class EvalInfo; |
80 | |
81 | using SourceLocExprScopeGuard = |
82 | CurrentSourceLocExprScope::SourceLocExprScopeGuard; |
83 | |
84 | static QualType getType(APValue::LValueBase B) { |
85 | return B.getType(); |
86 | } |
87 | |
88 | /// Get an LValue path entry, which is known to not be an array index, as a |
89 | /// field declaration. |
90 | static const FieldDecl *getAsField(APValue::LValuePathEntry E) { |
91 | return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer()); |
92 | } |
93 | /// Get an LValue path entry, which is known to not be an array index, as a |
94 | /// base class declaration. |
95 | static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) { |
96 | return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer()); |
97 | } |
98 | /// Determine whether this LValue path entry for a base class names a virtual |
99 | /// base class. |
100 | static bool isVirtualBaseClass(APValue::LValuePathEntry E) { |
101 | return E.getAsBaseOrMember().getInt(); |
102 | } |
103 | |
104 | /// Given an expression, determine the type used to store the result of |
105 | /// evaluating that expression. |
106 | static QualType getStorageType(const ASTContext &Ctx, const Expr *E) { |
107 | if (E->isPRValue()) |
108 | return E->getType(); |
109 | return Ctx.getLValueReferenceType(T: E->getType()); |
110 | } |
111 | |
112 | /// Given a CallExpr, try to get the alloc_size attribute. May return null. |
113 | static const AllocSizeAttr *getAllocSizeAttr(const CallExpr *CE) { |
114 | if (const FunctionDecl *DirectCallee = CE->getDirectCallee()) |
115 | return DirectCallee->getAttr<AllocSizeAttr>(); |
116 | if (const Decl *IndirectCallee = CE->getCalleeDecl()) |
117 | return IndirectCallee->getAttr<AllocSizeAttr>(); |
118 | return nullptr; |
119 | } |
120 | |
121 | /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr. |
122 | /// This will look through a single cast. |
123 | /// |
124 | /// Returns null if we couldn't unwrap a function with alloc_size. |
125 | static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) { |
126 | if (!E->getType()->isPointerType()) |
127 | return nullptr; |
128 | |
129 | E = E->IgnoreParens(); |
130 | // If we're doing a variable assignment from e.g. malloc(N), there will |
131 | // probably be a cast of some kind. In exotic cases, we might also see a |
132 | // top-level ExprWithCleanups. Ignore them either way. |
133 | if (const auto *FE = dyn_cast<FullExpr>(Val: E)) |
134 | E = FE->getSubExpr()->IgnoreParens(); |
135 | |
136 | if (const auto *Cast = dyn_cast<CastExpr>(Val: E)) |
137 | E = Cast->getSubExpr()->IgnoreParens(); |
138 | |
139 | if (const auto *CE = dyn_cast<CallExpr>(E)) |
140 | return getAllocSizeAttr(CE) ? CE : nullptr; |
141 | return nullptr; |
142 | } |
143 | |
144 | /// Determines whether or not the given Base contains a call to a function |
145 | /// with the alloc_size attribute. |
146 | static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) { |
147 | const auto *E = Base.dyn_cast<const Expr *>(); |
148 | return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E); |
149 | } |
150 | |
151 | /// Determines whether the given kind of constant expression is only ever |
152 | /// used for name mangling. If so, it's permitted to reference things that we |
153 | /// can't generate code for (in particular, dllimported functions). |
154 | static bool isForManglingOnly(ConstantExprKind Kind) { |
155 | switch (Kind) { |
156 | case ConstantExprKind::Normal: |
157 | case ConstantExprKind::ClassTemplateArgument: |
158 | case ConstantExprKind::ImmediateInvocation: |
159 | // Note that non-type template arguments of class type are emitted as |
160 | // template parameter objects. |
161 | return false; |
162 | |
163 | case ConstantExprKind::NonClassTemplateArgument: |
164 | return true; |
165 | } |
166 | llvm_unreachable("unknown ConstantExprKind" ); |
167 | } |
168 | |
169 | static bool isTemplateArgument(ConstantExprKind Kind) { |
170 | switch (Kind) { |
171 | case ConstantExprKind::Normal: |
172 | case ConstantExprKind::ImmediateInvocation: |
173 | return false; |
174 | |
175 | case ConstantExprKind::ClassTemplateArgument: |
176 | case ConstantExprKind::NonClassTemplateArgument: |
177 | return true; |
178 | } |
179 | llvm_unreachable("unknown ConstantExprKind" ); |
180 | } |
181 | |
182 | /// The bound to claim that an array of unknown bound has. |
183 | /// The value in MostDerivedArraySize is undefined in this case. So, set it |
184 | /// to an arbitrary value that's likely to loudly break things if it's used. |
185 | static const uint64_t AssumedSizeForUnsizedArray = |
186 | std::numeric_limits<uint64_t>::max() / 2; |
187 | |
188 | /// Determines if an LValue with the given LValueBase will have an unsized |
189 | /// array in its designator. |
190 | /// Find the path length and type of the most-derived subobject in the given |
191 | /// path, and find the size of the containing array, if any. |
192 | static unsigned |
193 | findMostDerivedSubobject(ASTContext &Ctx, APValue::LValueBase Base, |
194 | ArrayRef<APValue::LValuePathEntry> Path, |
195 | uint64_t &ArraySize, QualType &Type, bool &IsArray, |
196 | bool &FirstEntryIsUnsizedArray) { |
197 | // This only accepts LValueBases from APValues, and APValues don't support |
198 | // arrays that lack size info. |
199 | assert(!isBaseAnAllocSizeCall(Base) && |
200 | "Unsized arrays shouldn't appear here" ); |
201 | unsigned MostDerivedLength = 0; |
202 | Type = getType(B: Base); |
203 | |
204 | for (unsigned I = 0, N = Path.size(); I != N; ++I) { |
205 | if (Type->isArrayType()) { |
206 | const ArrayType *AT = Ctx.getAsArrayType(T: Type); |
207 | Type = AT->getElementType(); |
208 | MostDerivedLength = I + 1; |
209 | IsArray = true; |
210 | |
211 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) { |
212 | ArraySize = CAT->getZExtSize(); |
213 | } else { |
214 | assert(I == 0 && "unexpected unsized array designator" ); |
215 | FirstEntryIsUnsizedArray = true; |
216 | ArraySize = AssumedSizeForUnsizedArray; |
217 | } |
218 | } else if (Type->isAnyComplexType()) { |
219 | const ComplexType *CT = Type->castAs<ComplexType>(); |
220 | Type = CT->getElementType(); |
221 | ArraySize = 2; |
222 | MostDerivedLength = I + 1; |
223 | IsArray = true; |
224 | } else if (const FieldDecl *FD = getAsField(E: Path[I])) { |
225 | Type = FD->getType(); |
226 | ArraySize = 0; |
227 | MostDerivedLength = I + 1; |
228 | IsArray = false; |
229 | } else { |
230 | // Path[I] describes a base class. |
231 | ArraySize = 0; |
232 | IsArray = false; |
233 | } |
234 | } |
235 | return MostDerivedLength; |
236 | } |
237 | |
238 | /// A path from a glvalue to a subobject of that glvalue. |
239 | struct SubobjectDesignator { |
240 | /// True if the subobject was named in a manner not supported by C++11. Such |
241 | /// lvalues can still be folded, but they are not core constant expressions |
242 | /// and we cannot perform lvalue-to-rvalue conversions on them. |
243 | LLVM_PREFERRED_TYPE(bool) |
244 | unsigned Invalid : 1; |
245 | |
246 | /// Is this a pointer one past the end of an object? |
247 | LLVM_PREFERRED_TYPE(bool) |
248 | unsigned IsOnePastTheEnd : 1; |
249 | |
250 | /// Indicator of whether the first entry is an unsized array. |
251 | LLVM_PREFERRED_TYPE(bool) |
252 | unsigned FirstEntryIsAnUnsizedArray : 1; |
253 | |
254 | /// Indicator of whether the most-derived object is an array element. |
255 | LLVM_PREFERRED_TYPE(bool) |
256 | unsigned MostDerivedIsArrayElement : 1; |
257 | |
258 | /// The length of the path to the most-derived object of which this is a |
259 | /// subobject. |
260 | unsigned MostDerivedPathLength : 28; |
261 | |
262 | /// The size of the array of which the most-derived object is an element. |
263 | /// This will always be 0 if the most-derived object is not an array |
264 | /// element. 0 is not an indicator of whether or not the most-derived object |
265 | /// is an array, however, because 0-length arrays are allowed. |
266 | /// |
267 | /// If the current array is an unsized array, the value of this is |
268 | /// undefined. |
269 | uint64_t MostDerivedArraySize; |
270 | |
271 | /// The type of the most derived object referred to by this address. |
272 | QualType MostDerivedType; |
273 | |
274 | typedef APValue::LValuePathEntry PathEntry; |
275 | |
276 | /// The entries on the path from the glvalue to the designated subobject. |
277 | SmallVector<PathEntry, 8> Entries; |
278 | |
279 | SubobjectDesignator() : Invalid(true) {} |
280 | |
281 | explicit SubobjectDesignator(QualType T) |
282 | : Invalid(false), IsOnePastTheEnd(false), |
283 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
284 | MostDerivedPathLength(0), MostDerivedArraySize(0), |
285 | MostDerivedType(T) {} |
286 | |
287 | SubobjectDesignator(ASTContext &Ctx, const APValue &V) |
288 | : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false), |
289 | FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false), |
290 | MostDerivedPathLength(0), MostDerivedArraySize(0) { |
291 | assert(V.isLValue() && "Non-LValue used to make an LValue designator?" ); |
292 | if (!Invalid) { |
293 | IsOnePastTheEnd = V.isLValueOnePastTheEnd(); |
294 | ArrayRef<PathEntry> VEntries = V.getLValuePath(); |
295 | Entries.insert(I: Entries.end(), From: VEntries.begin(), To: VEntries.end()); |
296 | if (V.getLValueBase()) { |
297 | bool IsArray = false; |
298 | bool FirstIsUnsizedArray = false; |
299 | MostDerivedPathLength = findMostDerivedSubobject( |
300 | Ctx, V.getLValueBase(), V.getLValuePath(), MostDerivedArraySize, |
301 | MostDerivedType, IsArray, FirstIsUnsizedArray); |
302 | MostDerivedIsArrayElement = IsArray; |
303 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
304 | } |
305 | } |
306 | } |
307 | |
308 | void truncate(ASTContext &Ctx, APValue::LValueBase Base, |
309 | unsigned NewLength) { |
310 | if (Invalid) |
311 | return; |
312 | |
313 | assert(Base && "cannot truncate path for null pointer" ); |
314 | assert(NewLength <= Entries.size() && "not a truncation" ); |
315 | |
316 | if (NewLength == Entries.size()) |
317 | return; |
318 | Entries.resize(N: NewLength); |
319 | |
320 | bool IsArray = false; |
321 | bool FirstIsUnsizedArray = false; |
322 | MostDerivedPathLength = findMostDerivedSubobject( |
323 | Ctx, Base, Entries, MostDerivedArraySize, MostDerivedType, IsArray, |
324 | FirstIsUnsizedArray); |
325 | MostDerivedIsArrayElement = IsArray; |
326 | FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray; |
327 | } |
328 | |
329 | void setInvalid() { |
330 | Invalid = true; |
331 | Entries.clear(); |
332 | } |
333 | |
334 | /// Determine whether the most derived subobject is an array without a |
335 | /// known bound. |
336 | bool isMostDerivedAnUnsizedArray() const { |
337 | assert(!Invalid && "Calling this makes no sense on invalid designators" ); |
338 | return Entries.size() == 1 && FirstEntryIsAnUnsizedArray; |
339 | } |
340 | |
341 | /// Determine what the most derived array's size is. Results in an assertion |
342 | /// failure if the most derived array lacks a size. |
343 | uint64_t getMostDerivedArraySize() const { |
344 | assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size" ); |
345 | return MostDerivedArraySize; |
346 | } |
347 | |
348 | /// Determine whether this is a one-past-the-end pointer. |
349 | bool isOnePastTheEnd() const { |
350 | assert(!Invalid); |
351 | if (IsOnePastTheEnd) |
352 | return true; |
353 | if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement && |
354 | Entries[MostDerivedPathLength - 1].getAsArrayIndex() == |
355 | MostDerivedArraySize) |
356 | return true; |
357 | return false; |
358 | } |
359 | |
360 | /// Get the range of valid index adjustments in the form |
361 | /// {maximum value that can be subtracted from this pointer, |
362 | /// maximum value that can be added to this pointer} |
363 | std::pair<uint64_t, uint64_t> validIndexAdjustments() { |
364 | if (Invalid || isMostDerivedAnUnsizedArray()) |
365 | return {0, 0}; |
366 | |
367 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
368 | // nonarray object behaves the same as a pointer to the first element of |
369 | // an array of length one with the type of the object as its element type. |
370 | bool IsArray = MostDerivedPathLength == Entries.size() && |
371 | MostDerivedIsArrayElement; |
372 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
373 | : (uint64_t)IsOnePastTheEnd; |
374 | uint64_t ArraySize = |
375 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
376 | return {ArrayIndex, ArraySize - ArrayIndex}; |
377 | } |
378 | |
379 | /// Check that this refers to a valid subobject. |
380 | bool isValidSubobject() const { |
381 | if (Invalid) |
382 | return false; |
383 | return !isOnePastTheEnd(); |
384 | } |
385 | /// Check that this refers to a valid subobject, and if not, produce a |
386 | /// relevant diagnostic and set the designator as invalid. |
387 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK); |
388 | |
389 | /// Get the type of the designated object. |
390 | QualType getType(ASTContext &Ctx) const { |
391 | assert(!Invalid && "invalid designator has no subobject type" ); |
392 | return MostDerivedPathLength == Entries.size() |
393 | ? MostDerivedType |
394 | : Ctx.getRecordType(getAsBaseClass(Entries.back())); |
395 | } |
396 | |
397 | /// Update this designator to refer to the first element within this array. |
398 | void addArrayUnchecked(const ConstantArrayType *CAT) { |
399 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0)); |
400 | |
401 | // This is a most-derived object. |
402 | MostDerivedType = CAT->getElementType(); |
403 | MostDerivedIsArrayElement = true; |
404 | MostDerivedArraySize = CAT->getZExtSize(); |
405 | MostDerivedPathLength = Entries.size(); |
406 | } |
407 | /// Update this designator to refer to the first element within the array of |
408 | /// elements of type T. This is an array of unknown size. |
409 | void addUnsizedArrayUnchecked(QualType ElemTy) { |
410 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0)); |
411 | |
412 | MostDerivedType = ElemTy; |
413 | MostDerivedIsArrayElement = true; |
414 | // The value in MostDerivedArraySize is undefined in this case. So, set it |
415 | // to an arbitrary value that's likely to loudly break things if it's |
416 | // used. |
417 | MostDerivedArraySize = AssumedSizeForUnsizedArray; |
418 | MostDerivedPathLength = Entries.size(); |
419 | } |
420 | /// Update this designator to refer to the given base or member of this |
421 | /// object. |
422 | void addDeclUnchecked(const Decl *D, bool Virtual = false) { |
423 | Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual)); |
424 | |
425 | // If this isn't a base class, it's a new most-derived object. |
426 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) { |
427 | MostDerivedType = FD->getType(); |
428 | MostDerivedIsArrayElement = false; |
429 | MostDerivedArraySize = 0; |
430 | MostDerivedPathLength = Entries.size(); |
431 | } |
432 | } |
433 | /// Update this designator to refer to the given complex component. |
434 | void addComplexUnchecked(QualType EltTy, bool Imag) { |
435 | Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag)); |
436 | |
437 | // This is technically a most-derived object, though in practice this |
438 | // is unlikely to matter. |
439 | MostDerivedType = EltTy; |
440 | MostDerivedIsArrayElement = true; |
441 | MostDerivedArraySize = 2; |
442 | MostDerivedPathLength = Entries.size(); |
443 | } |
444 | void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E); |
445 | void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E, |
446 | const APSInt &N); |
447 | /// Add N to the address of this subobject. |
448 | void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N) { |
449 | if (Invalid || !N) return; |
450 | uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue(); |
451 | if (isMostDerivedAnUnsizedArray()) { |
452 | diagnoseUnsizedArrayPointerArithmetic(Info, E); |
453 | // Can't verify -- trust that the user is doing the right thing (or if |
454 | // not, trust that the caller will catch the bad behavior). |
455 | // FIXME: Should we reject if this overflows, at least? |
456 | Entries.back() = PathEntry::ArrayIndex( |
457 | Index: Entries.back().getAsArrayIndex() + TruncatedN); |
458 | return; |
459 | } |
460 | |
461 | // [expr.add]p4: For the purposes of these operators, a pointer to a |
462 | // nonarray object behaves the same as a pointer to the first element of |
463 | // an array of length one with the type of the object as its element type. |
464 | bool IsArray = MostDerivedPathLength == Entries.size() && |
465 | MostDerivedIsArrayElement; |
466 | uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex() |
467 | : (uint64_t)IsOnePastTheEnd; |
468 | uint64_t ArraySize = |
469 | IsArray ? getMostDerivedArraySize() : (uint64_t)1; |
470 | |
471 | if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) { |
472 | // Calculate the actual index in a wide enough type, so we can include |
473 | // it in the note. |
474 | N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65)); |
475 | (llvm::APInt&)N += ArrayIndex; |
476 | assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index" ); |
477 | diagnosePointerArithmetic(Info, E, N); |
478 | setInvalid(); |
479 | return; |
480 | } |
481 | |
482 | ArrayIndex += TruncatedN; |
483 | assert(ArrayIndex <= ArraySize && |
484 | "bounds check succeeded for out-of-bounds index" ); |
485 | |
486 | if (IsArray) |
487 | Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex); |
488 | else |
489 | IsOnePastTheEnd = (ArrayIndex != 0); |
490 | } |
491 | }; |
492 | |
493 | /// A scope at the end of which an object can need to be destroyed. |
494 | enum class ScopeKind { |
495 | Block, |
496 | FullExpression, |
497 | Call |
498 | }; |
499 | |
500 | /// A reference to a particular call and its arguments. |
501 | struct CallRef { |
502 | CallRef() : OrigCallee(), CallIndex(0), Version() {} |
503 | CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version) |
504 | : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {} |
505 | |
506 | explicit operator bool() const { return OrigCallee; } |
507 | |
508 | /// Get the parameter that the caller initialized, corresponding to the |
509 | /// given parameter in the callee. |
510 | const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const { |
511 | return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex()) |
512 | : PVD; |
513 | } |
514 | |
515 | /// The callee at the point where the arguments were evaluated. This might |
516 | /// be different from the actual callee (a different redeclaration, or a |
517 | /// virtual override), but this function's parameters are the ones that |
518 | /// appear in the parameter map. |
519 | const FunctionDecl *OrigCallee; |
520 | /// The call index of the frame that holds the argument values. |
521 | unsigned CallIndex; |
522 | /// The version of the parameters corresponding to this call. |
523 | unsigned Version; |
524 | }; |
525 | |
526 | /// A stack frame in the constexpr call stack. |
527 | class CallStackFrame : public interp::Frame { |
528 | public: |
529 | EvalInfo &Info; |
530 | |
531 | /// Parent - The caller of this stack frame. |
532 | CallStackFrame *Caller; |
533 | |
534 | /// Callee - The function which was called. |
535 | const FunctionDecl *Callee; |
536 | |
537 | /// This - The binding for the this pointer in this call, if any. |
538 | const LValue *This; |
539 | |
540 | /// CallExpr - The syntactical structure of member function calls |
541 | const Expr *CallExpr; |
542 | |
543 | /// Information on how to find the arguments to this call. Our arguments |
544 | /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a |
545 | /// key and this value as the version. |
546 | CallRef Arguments; |
547 | |
548 | /// Source location information about the default argument or default |
549 | /// initializer expression we're evaluating, if any. |
550 | CurrentSourceLocExprScope CurSourceLocExprScope; |
551 | |
552 | // Note that we intentionally use std::map here so that references to |
553 | // values are stable. |
554 | typedef std::pair<const void *, unsigned> MapKeyTy; |
555 | typedef std::map<MapKeyTy, APValue> MapTy; |
556 | /// Temporaries - Temporary lvalues materialized within this stack frame. |
557 | MapTy Temporaries; |
558 | |
559 | /// CallRange - The source range of the call expression for this call. |
560 | SourceRange CallRange; |
561 | |
562 | /// Index - The call index of this call. |
563 | unsigned Index; |
564 | |
565 | /// The stack of integers for tracking version numbers for temporaries. |
566 | SmallVector<unsigned, 2> TempVersionStack = {1}; |
567 | unsigned CurTempVersion = TempVersionStack.back(); |
568 | |
569 | unsigned getTempVersion() const { return TempVersionStack.back(); } |
570 | |
571 | void pushTempVersion() { |
572 | TempVersionStack.push_back(Elt: ++CurTempVersion); |
573 | } |
574 | |
575 | void popTempVersion() { |
576 | TempVersionStack.pop_back(); |
577 | } |
578 | |
579 | CallRef createCall(const FunctionDecl *Callee) { |
580 | return {Callee, Index, ++CurTempVersion}; |
581 | } |
582 | |
583 | // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact |
584 | // on the overall stack usage of deeply-recursing constexpr evaluations. |
585 | // (We should cache this map rather than recomputing it repeatedly.) |
586 | // But let's try this and see how it goes; we can look into caching the map |
587 | // as a later change. |
588 | |
589 | /// LambdaCaptureFields - Mapping from captured variables/this to |
590 | /// corresponding data members in the closure class. |
591 | llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields; |
592 | FieldDecl *LambdaThisCaptureField = nullptr; |
593 | |
594 | CallStackFrame(EvalInfo &Info, SourceRange CallRange, |
595 | const FunctionDecl *Callee, const LValue *This, |
596 | const Expr *CallExpr, CallRef Arguments); |
597 | ~CallStackFrame(); |
598 | |
599 | // Return the temporary for Key whose version number is Version. |
600 | APValue *getTemporary(const void *Key, unsigned Version) { |
601 | MapKeyTy KV(Key, Version); |
602 | auto LB = Temporaries.lower_bound(x: KV); |
603 | if (LB != Temporaries.end() && LB->first == KV) |
604 | return &LB->second; |
605 | return nullptr; |
606 | } |
607 | |
608 | // Return the current temporary for Key in the map. |
609 | APValue *getCurrentTemporary(const void *Key) { |
610 | auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX)); |
611 | if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key) |
612 | return &std::prev(x: UB)->second; |
613 | return nullptr; |
614 | } |
615 | |
616 | // Return the version number of the current temporary for Key. |
617 | unsigned getCurrentTemporaryVersion(const void *Key) const { |
618 | auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX)); |
619 | if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key) |
620 | return std::prev(x: UB)->first.second; |
621 | return 0; |
622 | } |
623 | |
624 | /// Allocate storage for an object of type T in this stack frame. |
625 | /// Populates LV with a handle to the created object. Key identifies |
626 | /// the temporary within the stack frame, and must not be reused without |
627 | /// bumping the temporary version number. |
628 | template<typename KeyT> |
629 | APValue &createTemporary(const KeyT *Key, QualType T, |
630 | ScopeKind Scope, LValue &LV); |
631 | |
632 | /// Allocate storage for a parameter of a function call made in this frame. |
633 | APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV); |
634 | |
635 | void describe(llvm::raw_ostream &OS) const override; |
636 | |
637 | Frame *getCaller() const override { return Caller; } |
638 | SourceRange getCallRange() const override { return CallRange; } |
639 | const FunctionDecl *getCallee() const override { return Callee; } |
640 | |
641 | bool isStdFunction() const { |
642 | for (const DeclContext *DC = Callee; DC; DC = DC->getParent()) |
643 | if (DC->isStdNamespace()) |
644 | return true; |
645 | return false; |
646 | } |
647 | |
648 | /// Whether we're in a context where [[msvc::constexpr]] evaluation is |
649 | /// permitted. See MSConstexprDocs for description of permitted contexts. |
650 | bool CanEvalMSConstexpr = false; |
651 | |
652 | private: |
653 | APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T, |
654 | ScopeKind Scope); |
655 | }; |
656 | |
657 | /// Temporarily override 'this'. |
658 | class ThisOverrideRAII { |
659 | public: |
660 | ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable) |
661 | : Frame(Frame), OldThis(Frame.This) { |
662 | if (Enable) |
663 | Frame.This = NewThis; |
664 | } |
665 | ~ThisOverrideRAII() { |
666 | Frame.This = OldThis; |
667 | } |
668 | private: |
669 | CallStackFrame &Frame; |
670 | const LValue *OldThis; |
671 | }; |
672 | |
673 | // A shorthand time trace scope struct, prints source range, for example |
674 | // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}} |
675 | class ExprTimeTraceScope { |
676 | public: |
677 | ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name) |
678 | : TimeScope(Name, [E, &Ctx] { |
679 | return E->getSourceRange().printToString(Ctx.getSourceManager()); |
680 | }) {} |
681 | |
682 | private: |
683 | llvm::TimeTraceScope TimeScope; |
684 | }; |
685 | |
686 | /// RAII object used to change the current ability of |
687 | /// [[msvc::constexpr]] evaulation. |
688 | struct { |
689 | CallStackFrame &; |
690 | bool ; |
691 | explicit (CallStackFrame &Frame, bool Value) |
692 | : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) { |
693 | Frame.CanEvalMSConstexpr = Value; |
694 | } |
695 | |
696 | () { Frame.CanEvalMSConstexpr = OldValue; } |
697 | }; |
698 | } |
699 | |
700 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
701 | const LValue &This, QualType ThisType); |
702 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
703 | APValue::LValueBase LVBase, APValue &Value, |
704 | QualType T); |
705 | |
706 | namespace { |
707 | /// A cleanup, and a flag indicating whether it is lifetime-extended. |
708 | class Cleanup { |
709 | llvm::PointerIntPair<APValue*, 2, ScopeKind> Value; |
710 | APValue::LValueBase Base; |
711 | QualType T; |
712 | |
713 | public: |
714 | Cleanup(APValue *Val, APValue::LValueBase Base, QualType T, |
715 | ScopeKind Scope) |
716 | : Value(Val, Scope), Base(Base), T(T) {} |
717 | |
718 | /// Determine whether this cleanup should be performed at the end of the |
719 | /// given kind of scope. |
720 | bool isDestroyedAtEndOf(ScopeKind K) const { |
721 | return (int)Value.getInt() >= (int)K; |
722 | } |
723 | bool endLifetime(EvalInfo &Info, bool RunDestructors) { |
724 | if (RunDestructors) { |
725 | SourceLocation Loc; |
726 | if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>()) |
727 | Loc = VD->getLocation(); |
728 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
729 | Loc = E->getExprLoc(); |
730 | return HandleDestruction(Info, Loc, Base, *Value.getPointer(), T); |
731 | } |
732 | *Value.getPointer() = APValue(); |
733 | return true; |
734 | } |
735 | |
736 | bool hasSideEffect() { |
737 | return T.isDestructedType(); |
738 | } |
739 | }; |
740 | |
741 | /// A reference to an object whose construction we are currently evaluating. |
742 | struct ObjectUnderConstruction { |
743 | APValue::LValueBase Base; |
744 | ArrayRef<APValue::LValuePathEntry> Path; |
745 | friend bool operator==(const ObjectUnderConstruction &LHS, |
746 | const ObjectUnderConstruction &RHS) { |
747 | return LHS.Base == RHS.Base && LHS.Path == RHS.Path; |
748 | } |
749 | friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) { |
750 | return llvm::hash_combine(args: Obj.Base, args: Obj.Path); |
751 | } |
752 | }; |
753 | enum class ConstructionPhase { |
754 | None, |
755 | Bases, |
756 | AfterBases, |
757 | AfterFields, |
758 | Destroying, |
759 | DestroyingBases |
760 | }; |
761 | } |
762 | |
763 | namespace llvm { |
764 | template<> struct DenseMapInfo<ObjectUnderConstruction> { |
765 | using Base = DenseMapInfo<APValue::LValueBase>; |
766 | static ObjectUnderConstruction getEmptyKey() { |
767 | return {.Base: Base::getEmptyKey(), .Path: {}}; } |
768 | static ObjectUnderConstruction getTombstoneKey() { |
769 | return {.Base: Base::getTombstoneKey(), .Path: {}}; |
770 | } |
771 | static unsigned getHashValue(const ObjectUnderConstruction &Object) { |
772 | return hash_value(Obj: Object); |
773 | } |
774 | static bool isEqual(const ObjectUnderConstruction &LHS, |
775 | const ObjectUnderConstruction &RHS) { |
776 | return LHS == RHS; |
777 | } |
778 | }; |
779 | } |
780 | |
781 | namespace { |
782 | /// A dynamically-allocated heap object. |
783 | struct DynAlloc { |
784 | /// The value of this heap-allocated object. |
785 | APValue Value; |
786 | /// The allocating expression; used for diagnostics. Either a CXXNewExpr |
787 | /// or a CallExpr (the latter is for direct calls to operator new inside |
788 | /// std::allocator<T>::allocate). |
789 | const Expr *AllocExpr = nullptr; |
790 | |
791 | enum Kind { |
792 | New, |
793 | ArrayNew, |
794 | StdAllocator |
795 | }; |
796 | |
797 | /// Get the kind of the allocation. This must match between allocation |
798 | /// and deallocation. |
799 | Kind getKind() const { |
800 | if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr)) |
801 | return NE->isArray() ? ArrayNew : New; |
802 | assert(isa<CallExpr>(AllocExpr)); |
803 | return StdAllocator; |
804 | } |
805 | }; |
806 | |
807 | struct DynAllocOrder { |
808 | bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const { |
809 | return L.getIndex() < R.getIndex(); |
810 | } |
811 | }; |
812 | |
813 | /// EvalInfo - This is a private struct used by the evaluator to capture |
814 | /// information about a subexpression as it is folded. It retains information |
815 | /// about the AST context, but also maintains information about the folded |
816 | /// expression. |
817 | /// |
818 | /// If an expression could be evaluated, it is still possible it is not a C |
819 | /// "integer constant expression" or constant expression. If not, this struct |
820 | /// captures information about how and why not. |
821 | /// |
822 | /// One bit of information passed *into* the request for constant folding |
823 | /// indicates whether the subexpression is "evaluated" or not according to C |
824 | /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can |
825 | /// evaluate the expression regardless of what the RHS is, but C only allows |
826 | /// certain things in certain situations. |
827 | class EvalInfo : public interp::State { |
828 | public: |
829 | ASTContext &Ctx; |
830 | |
831 | /// EvalStatus - Contains information about the evaluation. |
832 | Expr::EvalStatus &EvalStatus; |
833 | |
834 | /// CurrentCall - The top of the constexpr call stack. |
835 | CallStackFrame *CurrentCall; |
836 | |
837 | /// CallStackDepth - The number of calls in the call stack right now. |
838 | unsigned CallStackDepth; |
839 | |
840 | /// NextCallIndex - The next call index to assign. |
841 | unsigned NextCallIndex; |
842 | |
843 | /// StepsLeft - The remaining number of evaluation steps we're permitted |
844 | /// to perform. This is essentially a limit for the number of statements |
845 | /// we will evaluate. |
846 | unsigned StepsLeft; |
847 | |
848 | /// Enable the experimental new constant interpreter. If an expression is |
849 | /// not supported by the interpreter, an error is triggered. |
850 | bool EnableNewConstInterp; |
851 | |
852 | /// BottomFrame - The frame in which evaluation started. This must be |
853 | /// initialized after CurrentCall and CallStackDepth. |
854 | CallStackFrame BottomFrame; |
855 | |
856 | /// A stack of values whose lifetimes end at the end of some surrounding |
857 | /// evaluation frame. |
858 | llvm::SmallVector<Cleanup, 16> CleanupStack; |
859 | |
860 | /// EvaluatingDecl - This is the declaration whose initializer is being |
861 | /// evaluated, if any. |
862 | APValue::LValueBase EvaluatingDecl; |
863 | |
864 | enum class EvaluatingDeclKind { |
865 | None, |
866 | /// We're evaluating the construction of EvaluatingDecl. |
867 | Ctor, |
868 | /// We're evaluating the destruction of EvaluatingDecl. |
869 | Dtor, |
870 | }; |
871 | EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None; |
872 | |
873 | /// EvaluatingDeclValue - This is the value being constructed for the |
874 | /// declaration whose initializer is being evaluated, if any. |
875 | APValue *EvaluatingDeclValue; |
876 | |
877 | /// Set of objects that are currently being constructed. |
878 | llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase> |
879 | ObjectsUnderConstruction; |
880 | |
881 | /// Current heap allocations, along with the location where each was |
882 | /// allocated. We use std::map here because we need stable addresses |
883 | /// for the stored APValues. |
884 | std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs; |
885 | |
886 | /// The number of heap allocations performed so far in this evaluation. |
887 | unsigned NumHeapAllocs = 0; |
888 | |
889 | struct EvaluatingConstructorRAII { |
890 | EvalInfo &EI; |
891 | ObjectUnderConstruction Object; |
892 | bool DidInsert; |
893 | EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object, |
894 | bool HasBases) |
895 | : EI(EI), Object(Object) { |
896 | DidInsert = |
897 | EI.ObjectsUnderConstruction |
898 | .insert(KV: {Object, HasBases ? ConstructionPhase::Bases |
899 | : ConstructionPhase::AfterBases}) |
900 | .second; |
901 | } |
902 | void finishedConstructingBases() { |
903 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases; |
904 | } |
905 | void finishedConstructingFields() { |
906 | EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields; |
907 | } |
908 | ~EvaluatingConstructorRAII() { |
909 | if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object); |
910 | } |
911 | }; |
912 | |
913 | struct EvaluatingDestructorRAII { |
914 | EvalInfo &EI; |
915 | ObjectUnderConstruction Object; |
916 | bool DidInsert; |
917 | EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object) |
918 | : EI(EI), Object(Object) { |
919 | DidInsert = EI.ObjectsUnderConstruction |
920 | .insert(KV: {Object, ConstructionPhase::Destroying}) |
921 | .second; |
922 | } |
923 | void startedDestroyingBases() { |
924 | EI.ObjectsUnderConstruction[Object] = |
925 | ConstructionPhase::DestroyingBases; |
926 | } |
927 | ~EvaluatingDestructorRAII() { |
928 | if (DidInsert) |
929 | EI.ObjectsUnderConstruction.erase(Val: Object); |
930 | } |
931 | }; |
932 | |
933 | ConstructionPhase |
934 | isEvaluatingCtorDtor(APValue::LValueBase Base, |
935 | ArrayRef<APValue::LValuePathEntry> Path) { |
936 | return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path}); |
937 | } |
938 | |
939 | /// If we're currently speculatively evaluating, the outermost call stack |
940 | /// depth at which we can mutate state, otherwise 0. |
941 | unsigned SpeculativeEvaluationDepth = 0; |
942 | |
943 | /// The current array initialization index, if we're performing array |
944 | /// initialization. |
945 | uint64_t ArrayInitIndex = -1; |
946 | |
947 | /// HasActiveDiagnostic - Was the previous diagnostic stored? If so, further |
948 | /// notes attached to it will also be stored, otherwise they will not be. |
949 | bool HasActiveDiagnostic; |
950 | |
951 | /// Have we emitted a diagnostic explaining why we couldn't constant |
952 | /// fold (not just why it's not strictly a constant expression)? |
953 | bool HasFoldFailureDiagnostic; |
954 | |
955 | /// Whether we're checking that an expression is a potential constant |
956 | /// expression. If so, do not fail on constructs that could become constant |
957 | /// later on (such as a use of an undefined global). |
958 | bool CheckingPotentialConstantExpression = false; |
959 | |
960 | /// Whether we're checking for an expression that has undefined behavior. |
961 | /// If so, we will produce warnings if we encounter an operation that is |
962 | /// always undefined. |
963 | /// |
964 | /// Note that we still need to evaluate the expression normally when this |
965 | /// is set; this is used when evaluating ICEs in C. |
966 | bool CheckingForUndefinedBehavior = false; |
967 | |
968 | enum EvaluationMode { |
969 | /// Evaluate as a constant expression. Stop if we find that the expression |
970 | /// is not a constant expression. |
971 | EM_ConstantExpression, |
972 | |
973 | /// Evaluate as a constant expression. Stop if we find that the expression |
974 | /// is not a constant expression. Some expressions can be retried in the |
975 | /// optimizer if we don't constant fold them here, but in an unevaluated |
976 | /// context we try to fold them immediately since the optimizer never |
977 | /// gets a chance to look at it. |
978 | EM_ConstantExpressionUnevaluated, |
979 | |
980 | /// Fold the expression to a constant. Stop if we hit a side-effect that |
981 | /// we can't model. |
982 | EM_ConstantFold, |
983 | |
984 | /// Evaluate in any way we know how. Don't worry about side-effects that |
985 | /// can't be modeled. |
986 | EM_IgnoreSideEffects, |
987 | } EvalMode; |
988 | |
989 | /// Are we checking whether the expression is a potential constant |
990 | /// expression? |
991 | bool checkingPotentialConstantExpression() const override { |
992 | return CheckingPotentialConstantExpression; |
993 | } |
994 | |
995 | /// Are we checking an expression for overflow? |
996 | // FIXME: We should check for any kind of undefined or suspicious behavior |
997 | // in such constructs, not just overflow. |
998 | bool checkingForUndefinedBehavior() const override { |
999 | return CheckingForUndefinedBehavior; |
1000 | } |
1001 | |
1002 | EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode) |
1003 | : Ctx(const_cast<ASTContext &>(C)), EvalStatus(S), CurrentCall(nullptr), |
1004 | CallStackDepth(0), NextCallIndex(1), |
1005 | StepsLeft(C.getLangOpts().ConstexprStepLimit), |
1006 | EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp), |
1007 | BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr, |
1008 | /*This=*/nullptr, |
1009 | /*CallExpr=*/nullptr, CallRef()), |
1010 | EvaluatingDecl((const ValueDecl *)nullptr), |
1011 | EvaluatingDeclValue(nullptr), HasActiveDiagnostic(false), |
1012 | HasFoldFailureDiagnostic(false), EvalMode(Mode) {} |
1013 | |
1014 | ~EvalInfo() { |
1015 | discardCleanups(); |
1016 | } |
1017 | |
1018 | ASTContext &getCtx() const override { return Ctx; } |
1019 | |
1020 | void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value, |
1021 | EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) { |
1022 | EvaluatingDecl = Base; |
1023 | IsEvaluatingDecl = EDK; |
1024 | EvaluatingDeclValue = &Value; |
1025 | } |
1026 | |
1027 | bool CheckCallLimit(SourceLocation Loc) { |
1028 | // Don't perform any constexpr calls (other than the call we're checking) |
1029 | // when checking a potential constant expression. |
1030 | if (checkingPotentialConstantExpression() && CallStackDepth > 1) |
1031 | return false; |
1032 | if (NextCallIndex == 0) { |
1033 | // NextCallIndex has wrapped around. |
1034 | FFDiag(Loc, diag::note_constexpr_call_limit_exceeded); |
1035 | return false; |
1036 | } |
1037 | if (CallStackDepth <= getLangOpts().ConstexprCallDepth) |
1038 | return true; |
1039 | FFDiag(Loc, diag::note_constexpr_depth_limit_exceeded) |
1040 | << getLangOpts().ConstexprCallDepth; |
1041 | return false; |
1042 | } |
1043 | |
1044 | bool CheckArraySize(SourceLocation Loc, unsigned BitWidth, |
1045 | uint64_t ElemCount, bool Diag) { |
1046 | // FIXME: GH63562 |
1047 | // APValue stores array extents as unsigned, |
1048 | // so anything that is greater that unsigned would overflow when |
1049 | // constructing the array, we catch this here. |
1050 | if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) || |
1051 | ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) { |
1052 | if (Diag) |
1053 | FFDiag(Loc, diag::note_constexpr_new_too_large) << ElemCount; |
1054 | return false; |
1055 | } |
1056 | |
1057 | // FIXME: GH63562 |
1058 | // Arrays allocate an APValue per element. |
1059 | // We use the number of constexpr steps as a proxy for the maximum size |
1060 | // of arrays to avoid exhausting the system resources, as initialization |
1061 | // of each element is likely to take some number of steps anyway. |
1062 | uint64_t Limit = Ctx.getLangOpts().ConstexprStepLimit; |
1063 | if (ElemCount > Limit) { |
1064 | if (Diag) |
1065 | FFDiag(Loc, diag::note_constexpr_new_exceeds_limits) |
1066 | << ElemCount << Limit; |
1067 | return false; |
1068 | } |
1069 | return true; |
1070 | } |
1071 | |
1072 | std::pair<CallStackFrame *, unsigned> |
1073 | getCallFrameAndDepth(unsigned CallIndex) { |
1074 | assert(CallIndex && "no call index in getCallFrameAndDepth" ); |
1075 | // We will eventually hit BottomFrame, which has Index 1, so Frame can't |
1076 | // be null in this loop. |
1077 | unsigned Depth = CallStackDepth; |
1078 | CallStackFrame *Frame = CurrentCall; |
1079 | while (Frame->Index > CallIndex) { |
1080 | Frame = Frame->Caller; |
1081 | --Depth; |
1082 | } |
1083 | if (Frame->Index == CallIndex) |
1084 | return {Frame, Depth}; |
1085 | return {nullptr, 0}; |
1086 | } |
1087 | |
1088 | bool nextStep(const Stmt *S) { |
1089 | if (!StepsLeft) { |
1090 | FFDiag(S->getBeginLoc(), diag::note_constexpr_step_limit_exceeded); |
1091 | return false; |
1092 | } |
1093 | --StepsLeft; |
1094 | return true; |
1095 | } |
1096 | |
1097 | APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV); |
1098 | |
1099 | std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) { |
1100 | std::optional<DynAlloc *> Result; |
1101 | auto It = HeapAllocs.find(x: DA); |
1102 | if (It != HeapAllocs.end()) |
1103 | Result = &It->second; |
1104 | return Result; |
1105 | } |
1106 | |
1107 | /// Get the allocated storage for the given parameter of the given call. |
1108 | APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) { |
1109 | CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first; |
1110 | return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version) |
1111 | : nullptr; |
1112 | } |
1113 | |
1114 | /// Information about a stack frame for std::allocator<T>::[de]allocate. |
1115 | struct StdAllocatorCaller { |
1116 | unsigned FrameIndex; |
1117 | QualType ElemType; |
1118 | explicit operator bool() const { return FrameIndex != 0; }; |
1119 | }; |
1120 | |
1121 | StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const { |
1122 | for (const CallStackFrame *Call = CurrentCall; Call != &BottomFrame; |
1123 | Call = Call->Caller) { |
1124 | const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee); |
1125 | if (!MD) |
1126 | continue; |
1127 | const IdentifierInfo *FnII = MD->getIdentifier(); |
1128 | if (!FnII || !FnII->isStr(Str: FnName)) |
1129 | continue; |
1130 | |
1131 | const auto *CTSD = |
1132 | dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent()); |
1133 | if (!CTSD) |
1134 | continue; |
1135 | |
1136 | const IdentifierInfo *ClassII = CTSD->getIdentifier(); |
1137 | const TemplateArgumentList &TAL = CTSD->getTemplateArgs(); |
1138 | if (CTSD->isInStdNamespace() && ClassII && |
1139 | ClassII->isStr(Str: "allocator" ) && TAL.size() >= 1 && |
1140 | TAL[0].getKind() == TemplateArgument::Type) |
1141 | return {Call->Index, TAL[0].getAsType()}; |
1142 | } |
1143 | |
1144 | return {}; |
1145 | } |
1146 | |
1147 | void performLifetimeExtension() { |
1148 | // Disable the cleanups for lifetime-extended temporaries. |
1149 | llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) { |
1150 | return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression); |
1151 | }); |
1152 | } |
1153 | |
1154 | /// Throw away any remaining cleanups at the end of evaluation. If any |
1155 | /// cleanups would have had a side-effect, note that as an unmodeled |
1156 | /// side-effect and return false. Otherwise, return true. |
1157 | bool discardCleanups() { |
1158 | for (Cleanup &C : CleanupStack) { |
1159 | if (C.hasSideEffect() && !noteSideEffect()) { |
1160 | CleanupStack.clear(); |
1161 | return false; |
1162 | } |
1163 | } |
1164 | CleanupStack.clear(); |
1165 | return true; |
1166 | } |
1167 | |
1168 | private: |
1169 | interp::Frame *getCurrentFrame() override { return CurrentCall; } |
1170 | const interp::Frame *getBottomFrame() const override { return &BottomFrame; } |
1171 | |
1172 | bool hasActiveDiagnostic() override { return HasActiveDiagnostic; } |
1173 | void setActiveDiagnostic(bool Flag) override { HasActiveDiagnostic = Flag; } |
1174 | |
1175 | void setFoldFailureDiagnostic(bool Flag) override { |
1176 | HasFoldFailureDiagnostic = Flag; |
1177 | } |
1178 | |
1179 | Expr::EvalStatus &getEvalStatus() const override { return EvalStatus; } |
1180 | |
1181 | // If we have a prior diagnostic, it will be noting that the expression |
1182 | // isn't a constant expression. This diagnostic is more important, |
1183 | // unless we require this evaluation to produce a constant expression. |
1184 | // |
1185 | // FIXME: We might want to show both diagnostics to the user in |
1186 | // EM_ConstantFold mode. |
1187 | bool hasPriorDiagnostic() override { |
1188 | if (!EvalStatus.Diag->empty()) { |
1189 | switch (EvalMode) { |
1190 | case EM_ConstantFold: |
1191 | case EM_IgnoreSideEffects: |
1192 | if (!HasFoldFailureDiagnostic) |
1193 | break; |
1194 | // We've already failed to fold something. Keep that diagnostic. |
1195 | [[fallthrough]]; |
1196 | case EM_ConstantExpression: |
1197 | case EM_ConstantExpressionUnevaluated: |
1198 | setActiveDiagnostic(false); |
1199 | return true; |
1200 | } |
1201 | } |
1202 | return false; |
1203 | } |
1204 | |
1205 | unsigned getCallStackDepth() override { return CallStackDepth; } |
1206 | |
1207 | public: |
1208 | /// Should we continue evaluation after encountering a side-effect that we |
1209 | /// couldn't model? |
1210 | bool keepEvaluatingAfterSideEffect() { |
1211 | switch (EvalMode) { |
1212 | case EM_IgnoreSideEffects: |
1213 | return true; |
1214 | |
1215 | case EM_ConstantExpression: |
1216 | case EM_ConstantExpressionUnevaluated: |
1217 | case EM_ConstantFold: |
1218 | // By default, assume any side effect might be valid in some other |
1219 | // evaluation of this expression from a different context. |
1220 | return checkingPotentialConstantExpression() || |
1221 | checkingForUndefinedBehavior(); |
1222 | } |
1223 | llvm_unreachable("Missed EvalMode case" ); |
1224 | } |
1225 | |
1226 | /// Note that we have had a side-effect, and determine whether we should |
1227 | /// keep evaluating. |
1228 | bool noteSideEffect() { |
1229 | EvalStatus.HasSideEffects = true; |
1230 | return keepEvaluatingAfterSideEffect(); |
1231 | } |
1232 | |
1233 | /// Should we continue evaluation after encountering undefined behavior? |
1234 | bool keepEvaluatingAfterUndefinedBehavior() { |
1235 | switch (EvalMode) { |
1236 | case EM_IgnoreSideEffects: |
1237 | case EM_ConstantFold: |
1238 | return true; |
1239 | |
1240 | case EM_ConstantExpression: |
1241 | case EM_ConstantExpressionUnevaluated: |
1242 | return checkingForUndefinedBehavior(); |
1243 | } |
1244 | llvm_unreachable("Missed EvalMode case" ); |
1245 | } |
1246 | |
1247 | /// Note that we hit something that was technically undefined behavior, but |
1248 | /// that we can evaluate past it (such as signed overflow or floating-point |
1249 | /// division by zero.) |
1250 | bool noteUndefinedBehavior() override { |
1251 | EvalStatus.HasUndefinedBehavior = true; |
1252 | return keepEvaluatingAfterUndefinedBehavior(); |
1253 | } |
1254 | |
1255 | /// Should we continue evaluation as much as possible after encountering a |
1256 | /// construct which can't be reduced to a value? |
1257 | bool keepEvaluatingAfterFailure() const override { |
1258 | if (!StepsLeft) |
1259 | return false; |
1260 | |
1261 | switch (EvalMode) { |
1262 | case EM_ConstantExpression: |
1263 | case EM_ConstantExpressionUnevaluated: |
1264 | case EM_ConstantFold: |
1265 | case EM_IgnoreSideEffects: |
1266 | return checkingPotentialConstantExpression() || |
1267 | checkingForUndefinedBehavior(); |
1268 | } |
1269 | llvm_unreachable("Missed EvalMode case" ); |
1270 | } |
1271 | |
1272 | /// Notes that we failed to evaluate an expression that other expressions |
1273 | /// directly depend on, and determine if we should keep evaluating. This |
1274 | /// should only be called if we actually intend to keep evaluating. |
1275 | /// |
1276 | /// Call noteSideEffect() instead if we may be able to ignore the value that |
1277 | /// we failed to evaluate, e.g. if we failed to evaluate Foo() in: |
1278 | /// |
1279 | /// (Foo(), 1) // use noteSideEffect |
1280 | /// (Foo() || true) // use noteSideEffect |
1281 | /// Foo() + 1 // use noteFailure |
1282 | [[nodiscard]] bool noteFailure() { |
1283 | // Failure when evaluating some expression often means there is some |
1284 | // subexpression whose evaluation was skipped. Therefore, (because we |
1285 | // don't track whether we skipped an expression when unwinding after an |
1286 | // evaluation failure) every evaluation failure that bubbles up from a |
1287 | // subexpression implies that a side-effect has potentially happened. We |
1288 | // skip setting the HasSideEffects flag to true until we decide to |
1289 | // continue evaluating after that point, which happens here. |
1290 | bool KeepGoing = keepEvaluatingAfterFailure(); |
1291 | EvalStatus.HasSideEffects |= KeepGoing; |
1292 | return KeepGoing; |
1293 | } |
1294 | |
1295 | class ArrayInitLoopIndex { |
1296 | EvalInfo &Info; |
1297 | uint64_t OuterIndex; |
1298 | |
1299 | public: |
1300 | ArrayInitLoopIndex(EvalInfo &Info) |
1301 | : Info(Info), OuterIndex(Info.ArrayInitIndex) { |
1302 | Info.ArrayInitIndex = 0; |
1303 | } |
1304 | ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; } |
1305 | |
1306 | operator uint64_t&() { return Info.ArrayInitIndex; } |
1307 | }; |
1308 | }; |
1309 | |
1310 | /// Object used to treat all foldable expressions as constant expressions. |
1311 | struct FoldConstant { |
1312 | EvalInfo &Info; |
1313 | bool Enabled; |
1314 | bool HadNoPriorDiags; |
1315 | EvalInfo::EvaluationMode OldMode; |
1316 | |
1317 | explicit FoldConstant(EvalInfo &Info, bool Enabled) |
1318 | : Info(Info), |
1319 | Enabled(Enabled), |
1320 | HadNoPriorDiags(Info.EvalStatus.Diag && |
1321 | Info.EvalStatus.Diag->empty() && |
1322 | !Info.EvalStatus.HasSideEffects), |
1323 | OldMode(Info.EvalMode) { |
1324 | if (Enabled) |
1325 | Info.EvalMode = EvalInfo::EM_ConstantFold; |
1326 | } |
1327 | void keepDiagnostics() { Enabled = false; } |
1328 | ~FoldConstant() { |
1329 | if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() && |
1330 | !Info.EvalStatus.HasSideEffects) |
1331 | Info.EvalStatus.Diag->clear(); |
1332 | Info.EvalMode = OldMode; |
1333 | } |
1334 | }; |
1335 | |
1336 | /// RAII object used to set the current evaluation mode to ignore |
1337 | /// side-effects. |
1338 | struct IgnoreSideEffectsRAII { |
1339 | EvalInfo &Info; |
1340 | EvalInfo::EvaluationMode OldMode; |
1341 | explicit IgnoreSideEffectsRAII(EvalInfo &Info) |
1342 | : Info(Info), OldMode(Info.EvalMode) { |
1343 | Info.EvalMode = EvalInfo::EM_IgnoreSideEffects; |
1344 | } |
1345 | |
1346 | ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; } |
1347 | }; |
1348 | |
1349 | /// RAII object used to optionally suppress diagnostics and side-effects from |
1350 | /// a speculative evaluation. |
1351 | class SpeculativeEvaluationRAII { |
1352 | EvalInfo *Info = nullptr; |
1353 | Expr::EvalStatus OldStatus; |
1354 | unsigned OldSpeculativeEvaluationDepth = 0; |
1355 | |
1356 | void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) { |
1357 | Info = Other.Info; |
1358 | OldStatus = Other.OldStatus; |
1359 | OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth; |
1360 | Other.Info = nullptr; |
1361 | } |
1362 | |
1363 | void maybeRestoreState() { |
1364 | if (!Info) |
1365 | return; |
1366 | |
1367 | Info->EvalStatus = OldStatus; |
1368 | Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth; |
1369 | } |
1370 | |
1371 | public: |
1372 | SpeculativeEvaluationRAII() = default; |
1373 | |
1374 | SpeculativeEvaluationRAII( |
1375 | EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr) |
1376 | : Info(&Info), OldStatus(Info.EvalStatus), |
1377 | OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) { |
1378 | Info.EvalStatus.Diag = NewDiag; |
1379 | Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1; |
1380 | } |
1381 | |
1382 | SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete; |
1383 | SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) { |
1384 | moveFromAndCancel(Other: std::move(Other)); |
1385 | } |
1386 | |
1387 | SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) { |
1388 | maybeRestoreState(); |
1389 | moveFromAndCancel(Other: std::move(Other)); |
1390 | return *this; |
1391 | } |
1392 | |
1393 | ~SpeculativeEvaluationRAII() { maybeRestoreState(); } |
1394 | }; |
1395 | |
1396 | /// RAII object wrapping a full-expression or block scope, and handling |
1397 | /// the ending of the lifetime of temporaries created within it. |
1398 | template<ScopeKind Kind> |
1399 | class ScopeRAII { |
1400 | EvalInfo &Info; |
1401 | unsigned OldStackSize; |
1402 | public: |
1403 | ScopeRAII(EvalInfo &Info) |
1404 | : Info(Info), OldStackSize(Info.CleanupStack.size()) { |
1405 | // Push a new temporary version. This is needed to distinguish between |
1406 | // temporaries created in different iterations of a loop. |
1407 | Info.CurrentCall->pushTempVersion(); |
1408 | } |
1409 | bool destroy(bool RunDestructors = true) { |
1410 | bool OK = cleanup(Info, RunDestructors, OldStackSize); |
1411 | OldStackSize = -1U; |
1412 | return OK; |
1413 | } |
1414 | ~ScopeRAII() { |
1415 | if (OldStackSize != -1U) |
1416 | destroy(RunDestructors: false); |
1417 | // Body moved to a static method to encourage the compiler to inline away |
1418 | // instances of this class. |
1419 | Info.CurrentCall->popTempVersion(); |
1420 | } |
1421 | private: |
1422 | static bool cleanup(EvalInfo &Info, bool RunDestructors, |
1423 | unsigned OldStackSize) { |
1424 | assert(OldStackSize <= Info.CleanupStack.size() && |
1425 | "running cleanups out of order?" ); |
1426 | |
1427 | // Run all cleanups for a block scope, and non-lifetime-extended cleanups |
1428 | // for a full-expression scope. |
1429 | bool Success = true; |
1430 | for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) { |
1431 | if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) { |
1432 | if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) { |
1433 | Success = false; |
1434 | break; |
1435 | } |
1436 | } |
1437 | } |
1438 | |
1439 | // Compact any retained cleanups. |
1440 | auto NewEnd = Info.CleanupStack.begin() + OldStackSize; |
1441 | if (Kind != ScopeKind::Block) |
1442 | NewEnd = |
1443 | std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) { |
1444 | return C.isDestroyedAtEndOf(K: Kind); |
1445 | }); |
1446 | Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end()); |
1447 | return Success; |
1448 | } |
1449 | }; |
1450 | typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII; |
1451 | typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII; |
1452 | typedef ScopeRAII<ScopeKind::Call> CallScopeRAII; |
1453 | } |
1454 | |
1455 | bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E, |
1456 | CheckSubobjectKind CSK) { |
1457 | if (Invalid) |
1458 | return false; |
1459 | if (isOnePastTheEnd()) { |
1460 | Info.CCEDiag(E, diag::note_constexpr_past_end_subobject) |
1461 | << CSK; |
1462 | setInvalid(); |
1463 | return false; |
1464 | } |
1465 | // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there |
1466 | // must actually be at least one array element; even a VLA cannot have a |
1467 | // bound of zero. And if our index is nonzero, we already had a CCEDiag. |
1468 | return true; |
1469 | } |
1470 | |
1471 | void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, |
1472 | const Expr *E) { |
1473 | Info.CCEDiag(E, diag::note_constexpr_unsized_array_indexed); |
1474 | // Do not set the designator as invalid: we can represent this situation, |
1475 | // and correct handling of __builtin_object_size requires us to do so. |
1476 | } |
1477 | |
1478 | void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info, |
1479 | const Expr *E, |
1480 | const APSInt &N) { |
1481 | // If we're complaining, we must be able to statically determine the size of |
1482 | // the most derived array. |
1483 | if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement) |
1484 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1485 | << N << /*array*/ 0 |
1486 | << static_cast<unsigned>(getMostDerivedArraySize()); |
1487 | else |
1488 | Info.CCEDiag(E, diag::note_constexpr_array_index) |
1489 | << N << /*non-array*/ 1; |
1490 | setInvalid(); |
1491 | } |
1492 | |
1493 | CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange, |
1494 | const FunctionDecl *Callee, const LValue *This, |
1495 | const Expr *CallExpr, CallRef Call) |
1496 | : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This), |
1497 | CallExpr(CallExpr), Arguments(Call), CallRange(CallRange), |
1498 | Index(Info.NextCallIndex++) { |
1499 | Info.CurrentCall = this; |
1500 | ++Info.CallStackDepth; |
1501 | } |
1502 | |
1503 | CallStackFrame::~CallStackFrame() { |
1504 | assert(Info.CurrentCall == this && "calls retired out of order" ); |
1505 | --Info.CallStackDepth; |
1506 | Info.CurrentCall = Caller; |
1507 | } |
1508 | |
1509 | static bool isRead(AccessKinds AK) { |
1510 | return AK == AK_Read || AK == AK_ReadObjectRepresentation; |
1511 | } |
1512 | |
1513 | static bool isModification(AccessKinds AK) { |
1514 | switch (AK) { |
1515 | case AK_Read: |
1516 | case AK_ReadObjectRepresentation: |
1517 | case AK_MemberCall: |
1518 | case AK_DynamicCast: |
1519 | case AK_TypeId: |
1520 | return false; |
1521 | case AK_Assign: |
1522 | case AK_Increment: |
1523 | case AK_Decrement: |
1524 | case AK_Construct: |
1525 | case AK_Destroy: |
1526 | return true; |
1527 | } |
1528 | llvm_unreachable("unknown access kind" ); |
1529 | } |
1530 | |
1531 | static bool isAnyAccess(AccessKinds AK) { |
1532 | return isRead(AK) || isModification(AK); |
1533 | } |
1534 | |
1535 | /// Is this an access per the C++ definition? |
1536 | static bool isFormalAccess(AccessKinds AK) { |
1537 | return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy; |
1538 | } |
1539 | |
1540 | /// Is this kind of axcess valid on an indeterminate object value? |
1541 | static bool isValidIndeterminateAccess(AccessKinds AK) { |
1542 | switch (AK) { |
1543 | case AK_Read: |
1544 | case AK_Increment: |
1545 | case AK_Decrement: |
1546 | // These need the object's value. |
1547 | return false; |
1548 | |
1549 | case AK_ReadObjectRepresentation: |
1550 | case AK_Assign: |
1551 | case AK_Construct: |
1552 | case AK_Destroy: |
1553 | // Construction and destruction don't need the value. |
1554 | return true; |
1555 | |
1556 | case AK_MemberCall: |
1557 | case AK_DynamicCast: |
1558 | case AK_TypeId: |
1559 | // These aren't really meaningful on scalars. |
1560 | return true; |
1561 | } |
1562 | llvm_unreachable("unknown access kind" ); |
1563 | } |
1564 | |
1565 | namespace { |
1566 | struct ComplexValue { |
1567 | private: |
1568 | bool IsInt; |
1569 | |
1570 | public: |
1571 | APSInt IntReal, IntImag; |
1572 | APFloat FloatReal, FloatImag; |
1573 | |
1574 | ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {} |
1575 | |
1576 | void makeComplexFloat() { IsInt = false; } |
1577 | bool isComplexFloat() const { return !IsInt; } |
1578 | APFloat &getComplexFloatReal() { return FloatReal; } |
1579 | APFloat &getComplexFloatImag() { return FloatImag; } |
1580 | |
1581 | void makeComplexInt() { IsInt = true; } |
1582 | bool isComplexInt() const { return IsInt; } |
1583 | APSInt &getComplexIntReal() { return IntReal; } |
1584 | APSInt &getComplexIntImag() { return IntImag; } |
1585 | |
1586 | void moveInto(APValue &v) const { |
1587 | if (isComplexFloat()) |
1588 | v = APValue(FloatReal, FloatImag); |
1589 | else |
1590 | v = APValue(IntReal, IntImag); |
1591 | } |
1592 | void setFrom(const APValue &v) { |
1593 | assert(v.isComplexFloat() || v.isComplexInt()); |
1594 | if (v.isComplexFloat()) { |
1595 | makeComplexFloat(); |
1596 | FloatReal = v.getComplexFloatReal(); |
1597 | FloatImag = v.getComplexFloatImag(); |
1598 | } else { |
1599 | makeComplexInt(); |
1600 | IntReal = v.getComplexIntReal(); |
1601 | IntImag = v.getComplexIntImag(); |
1602 | } |
1603 | } |
1604 | }; |
1605 | |
1606 | struct LValue { |
1607 | APValue::LValueBase Base; |
1608 | CharUnits Offset; |
1609 | SubobjectDesignator Designator; |
1610 | bool IsNullPtr : 1; |
1611 | bool InvalidBase : 1; |
1612 | |
1613 | const APValue::LValueBase getLValueBase() const { return Base; } |
1614 | CharUnits &getLValueOffset() { return Offset; } |
1615 | const CharUnits &getLValueOffset() const { return Offset; } |
1616 | SubobjectDesignator &getLValueDesignator() { return Designator; } |
1617 | const SubobjectDesignator &getLValueDesignator() const { return Designator;} |
1618 | bool isNullPointer() const { return IsNullPtr;} |
1619 | |
1620 | unsigned getLValueCallIndex() const { return Base.getCallIndex(); } |
1621 | unsigned getLValueVersion() const { return Base.getVersion(); } |
1622 | |
1623 | void moveInto(APValue &V) const { |
1624 | if (Designator.Invalid) |
1625 | V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr); |
1626 | else { |
1627 | assert(!InvalidBase && "APValues can't handle invalid LValue bases" ); |
1628 | V = APValue(Base, Offset, Designator.Entries, |
1629 | Designator.IsOnePastTheEnd, IsNullPtr); |
1630 | } |
1631 | } |
1632 | void setFrom(ASTContext &Ctx, const APValue &V) { |
1633 | assert(V.isLValue() && "Setting LValue from a non-LValue?" ); |
1634 | Base = V.getLValueBase(); |
1635 | Offset = V.getLValueOffset(); |
1636 | InvalidBase = false; |
1637 | Designator = SubobjectDesignator(Ctx, V); |
1638 | IsNullPtr = V.isNullPointer(); |
1639 | } |
1640 | |
1641 | void set(APValue::LValueBase B, bool BInvalid = false) { |
1642 | #ifndef NDEBUG |
1643 | // We only allow a few types of invalid bases. Enforce that here. |
1644 | if (BInvalid) { |
1645 | const auto *E = B.get<const Expr *>(); |
1646 | assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) && |
1647 | "Unexpected type of invalid base" ); |
1648 | } |
1649 | #endif |
1650 | |
1651 | Base = B; |
1652 | Offset = CharUnits::fromQuantity(Quantity: 0); |
1653 | InvalidBase = BInvalid; |
1654 | Designator = SubobjectDesignator(getType(B)); |
1655 | IsNullPtr = false; |
1656 | } |
1657 | |
1658 | void setNull(ASTContext &Ctx, QualType PointerTy) { |
1659 | Base = (const ValueDecl *)nullptr; |
1660 | Offset = |
1661 | CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy)); |
1662 | InvalidBase = false; |
1663 | Designator = SubobjectDesignator(PointerTy->getPointeeType()); |
1664 | IsNullPtr = true; |
1665 | } |
1666 | |
1667 | void setInvalid(APValue::LValueBase B, unsigned I = 0) { |
1668 | set(B, BInvalid: true); |
1669 | } |
1670 | |
1671 | std::string toString(ASTContext &Ctx, QualType T) const { |
1672 | APValue Printable; |
1673 | moveInto(V&: Printable); |
1674 | return Printable.getAsString(Ctx, Ty: T); |
1675 | } |
1676 | |
1677 | private: |
1678 | // Check that this LValue is not based on a null pointer. If it is, produce |
1679 | // a diagnostic and mark the designator as invalid. |
1680 | template <typename GenDiagType> |
1681 | bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) { |
1682 | if (Designator.Invalid) |
1683 | return false; |
1684 | if (IsNullPtr) { |
1685 | GenDiag(); |
1686 | Designator.setInvalid(); |
1687 | return false; |
1688 | } |
1689 | return true; |
1690 | } |
1691 | |
1692 | public: |
1693 | bool checkNullPointer(EvalInfo &Info, const Expr *E, |
1694 | CheckSubobjectKind CSK) { |
1695 | return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] { |
1696 | Info.CCEDiag(E, diag::note_constexpr_null_subobject) << CSK; |
1697 | }); |
1698 | } |
1699 | |
1700 | bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E, |
1701 | AccessKinds AK) { |
1702 | return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] { |
1703 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
1704 | }); |
1705 | } |
1706 | |
1707 | // Check this LValue refers to an object. If not, set the designator to be |
1708 | // invalid and emit a diagnostic. |
1709 | bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) { |
1710 | return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) && |
1711 | Designator.checkSubobject(Info, E, CSK); |
1712 | } |
1713 | |
1714 | void addDecl(EvalInfo &Info, const Expr *E, |
1715 | const Decl *D, bool Virtual = false) { |
1716 | if (checkSubobject(Info, E, isa<FieldDecl>(D) ? CSK_Field : CSK_Base)) |
1717 | Designator.addDeclUnchecked(D, Virtual); |
1718 | } |
1719 | void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) { |
1720 | if (!Designator.Entries.empty()) { |
1721 | Info.CCEDiag(E, diag::note_constexpr_unsupported_unsized_array); |
1722 | Designator.setInvalid(); |
1723 | return; |
1724 | } |
1725 | if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) { |
1726 | assert(getType(Base)->isPointerType() || getType(Base)->isArrayType()); |
1727 | Designator.FirstEntryIsAnUnsizedArray = true; |
1728 | Designator.addUnsizedArrayUnchecked(ElemTy); |
1729 | } |
1730 | } |
1731 | void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) { |
1732 | if (checkSubobject(Info, E, CSK_ArrayToPointer)) |
1733 | Designator.addArrayUnchecked(CAT); |
1734 | } |
1735 | void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) { |
1736 | if (checkSubobject(Info, E, Imag ? CSK_Imag : CSK_Real)) |
1737 | Designator.addComplexUnchecked(EltTy, Imag); |
1738 | } |
1739 | void clearIsNullPointer() { |
1740 | IsNullPtr = false; |
1741 | } |
1742 | void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E, |
1743 | const APSInt &Index, CharUnits ElementSize) { |
1744 | // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB, |
1745 | // but we're not required to diagnose it and it's valid in C++.) |
1746 | if (!Index) |
1747 | return; |
1748 | |
1749 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
1750 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
1751 | // offsets. |
1752 | uint64_t Offset64 = Offset.getQuantity(); |
1753 | uint64_t ElemSize64 = ElementSize.getQuantity(); |
1754 | uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue(); |
1755 | Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64); |
1756 | |
1757 | if (checkNullPointer(Info, E, CSK_ArrayIndex)) |
1758 | Designator.adjustIndex(Info, E, Index); |
1759 | clearIsNullPointer(); |
1760 | } |
1761 | void adjustOffset(CharUnits N) { |
1762 | Offset += N; |
1763 | if (N.getQuantity()) |
1764 | clearIsNullPointer(); |
1765 | } |
1766 | }; |
1767 | |
1768 | struct MemberPtr { |
1769 | MemberPtr() {} |
1770 | explicit MemberPtr(const ValueDecl *Decl) |
1771 | : DeclAndIsDerivedMember(Decl, false) {} |
1772 | |
1773 | /// The member or (direct or indirect) field referred to by this member |
1774 | /// pointer, or 0 if this is a null member pointer. |
1775 | const ValueDecl *getDecl() const { |
1776 | return DeclAndIsDerivedMember.getPointer(); |
1777 | } |
1778 | /// Is this actually a member of some type derived from the relevant class? |
1779 | bool isDerivedMember() const { |
1780 | return DeclAndIsDerivedMember.getInt(); |
1781 | } |
1782 | /// Get the class which the declaration actually lives in. |
1783 | const CXXRecordDecl *getContainingRecord() const { |
1784 | return cast<CXXRecordDecl>( |
1785 | DeclAndIsDerivedMember.getPointer()->getDeclContext()); |
1786 | } |
1787 | |
1788 | void moveInto(APValue &V) const { |
1789 | V = APValue(getDecl(), isDerivedMember(), Path); |
1790 | } |
1791 | void setFrom(const APValue &V) { |
1792 | assert(V.isMemberPointer()); |
1793 | DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl()); |
1794 | DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember()); |
1795 | Path.clear(); |
1796 | ArrayRef<const CXXRecordDecl*> P = V.getMemberPointerPath(); |
1797 | Path.insert(I: Path.end(), From: P.begin(), To: P.end()); |
1798 | } |
1799 | |
1800 | /// DeclAndIsDerivedMember - The member declaration, and a flag indicating |
1801 | /// whether the member is a member of some class derived from the class type |
1802 | /// of the member pointer. |
1803 | llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember; |
1804 | /// Path - The path of base/derived classes from the member declaration's |
1805 | /// class (exclusive) to the class type of the member pointer (inclusive). |
1806 | SmallVector<const CXXRecordDecl*, 4> Path; |
1807 | |
1808 | /// Perform a cast towards the class of the Decl (either up or down the |
1809 | /// hierarchy). |
1810 | bool castBack(const CXXRecordDecl *Class) { |
1811 | assert(!Path.empty()); |
1812 | const CXXRecordDecl *Expected; |
1813 | if (Path.size() >= 2) |
1814 | Expected = Path[Path.size() - 2]; |
1815 | else |
1816 | Expected = getContainingRecord(); |
1817 | if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) { |
1818 | // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*), |
1819 | // if B does not contain the original member and is not a base or |
1820 | // derived class of the class containing the original member, the result |
1821 | // of the cast is undefined. |
1822 | // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to |
1823 | // (D::*). We consider that to be a language defect. |
1824 | return false; |
1825 | } |
1826 | Path.pop_back(); |
1827 | return true; |
1828 | } |
1829 | /// Perform a base-to-derived member pointer cast. |
1830 | bool castToDerived(const CXXRecordDecl *Derived) { |
1831 | if (!getDecl()) |
1832 | return true; |
1833 | if (!isDerivedMember()) { |
1834 | Path.push_back(Elt: Derived); |
1835 | return true; |
1836 | } |
1837 | if (!castBack(Class: Derived)) |
1838 | return false; |
1839 | if (Path.empty()) |
1840 | DeclAndIsDerivedMember.setInt(false); |
1841 | return true; |
1842 | } |
1843 | /// Perform a derived-to-base member pointer cast. |
1844 | bool castToBase(const CXXRecordDecl *Base) { |
1845 | if (!getDecl()) |
1846 | return true; |
1847 | if (Path.empty()) |
1848 | DeclAndIsDerivedMember.setInt(true); |
1849 | if (isDerivedMember()) { |
1850 | Path.push_back(Elt: Base); |
1851 | return true; |
1852 | } |
1853 | return castBack(Class: Base); |
1854 | } |
1855 | }; |
1856 | |
1857 | /// Compare two member pointers, which are assumed to be of the same type. |
1858 | static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) { |
1859 | if (!LHS.getDecl() || !RHS.getDecl()) |
1860 | return !LHS.getDecl() && !RHS.getDecl(); |
1861 | if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl()) |
1862 | return false; |
1863 | return LHS.Path == RHS.Path; |
1864 | } |
1865 | } |
1866 | |
1867 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E); |
1868 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, |
1869 | const LValue &This, const Expr *E, |
1870 | bool AllowNonLiteralTypes = false); |
1871 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
1872 | bool InvalidBaseOK = false); |
1873 | static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info, |
1874 | bool InvalidBaseOK = false); |
1875 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
1876 | EvalInfo &Info); |
1877 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info); |
1878 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info); |
1879 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
1880 | EvalInfo &Info); |
1881 | static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info); |
1882 | static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info); |
1883 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
1884 | EvalInfo &Info); |
1885 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result); |
1886 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
1887 | EvalInfo &Info); |
1888 | |
1889 | /// Evaluate an integer or fixed point expression into an APResult. |
1890 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
1891 | EvalInfo &Info); |
1892 | |
1893 | /// Evaluate only a fixed point expression into an APResult. |
1894 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
1895 | EvalInfo &Info); |
1896 | |
1897 | //===----------------------------------------------------------------------===// |
1898 | // Misc utilities |
1899 | //===----------------------------------------------------------------------===// |
1900 | |
1901 | /// Negate an APSInt in place, converting it to a signed form if necessary, and |
1902 | /// preserving its value (by extending by up to one bit as needed). |
1903 | static void negateAsSigned(APSInt &Int) { |
1904 | if (Int.isUnsigned() || Int.isMinSignedValue()) { |
1905 | Int = Int.extend(width: Int.getBitWidth() + 1); |
1906 | Int.setIsSigned(true); |
1907 | } |
1908 | Int = -Int; |
1909 | } |
1910 | |
1911 | template<typename KeyT> |
1912 | APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T, |
1913 | ScopeKind Scope, LValue &LV) { |
1914 | unsigned Version = getTempVersion(); |
1915 | APValue::LValueBase Base(Key, Index, Version); |
1916 | LV.set(B: Base); |
1917 | return createLocal(Base, Key, T, Scope); |
1918 | } |
1919 | |
1920 | /// Allocate storage for a parameter of a function call made in this frame. |
1921 | APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD, |
1922 | LValue &LV) { |
1923 | assert(Args.CallIndex == Index && "creating parameter in wrong frame" ); |
1924 | APValue::LValueBase Base(PVD, Index, Args.Version); |
1925 | LV.set(B: Base); |
1926 | // We always destroy parameters at the end of the call, even if we'd allow |
1927 | // them to live to the end of the full-expression at runtime, in order to |
1928 | // give portable results and match other compilers. |
1929 | return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call); |
1930 | } |
1931 | |
1932 | APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key, |
1933 | QualType T, ScopeKind Scope) { |
1934 | assert(Base.getCallIndex() == Index && "lvalue for wrong frame" ); |
1935 | unsigned Version = Base.getVersion(); |
1936 | APValue &Result = Temporaries[MapKeyTy(Key, Version)]; |
1937 | assert(Result.isAbsent() && "local created multiple times" ); |
1938 | |
1939 | // If we're creating a local immediately in the operand of a speculative |
1940 | // evaluation, don't register a cleanup to be run outside the speculative |
1941 | // evaluation context, since we won't actually be able to initialize this |
1942 | // object. |
1943 | if (Index <= Info.SpeculativeEvaluationDepth) { |
1944 | if (T.isDestructedType()) |
1945 | Info.noteSideEffect(); |
1946 | } else { |
1947 | Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope)); |
1948 | } |
1949 | return Result; |
1950 | } |
1951 | |
1952 | APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) { |
1953 | if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) { |
1954 | FFDiag(E, diag::note_constexpr_heap_alloc_limit_exceeded); |
1955 | return nullptr; |
1956 | } |
1957 | |
1958 | DynamicAllocLValue DA(NumHeapAllocs++); |
1959 | LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T)); |
1960 | auto Result = HeapAllocs.emplace(args: std::piecewise_construct, |
1961 | args: std::forward_as_tuple(args&: DA), args: std::tuple<>()); |
1962 | assert(Result.second && "reused a heap alloc index?" ); |
1963 | Result.first->second.AllocExpr = E; |
1964 | return &Result.first->second.Value; |
1965 | } |
1966 | |
1967 | /// Produce a string describing the given constexpr call. |
1968 | void CallStackFrame::describe(raw_ostream &Out) const { |
1969 | unsigned ArgIndex = 0; |
1970 | bool IsMemberCall = |
1971 | isa<CXXMethodDecl>(Val: Callee) && !isa<CXXConstructorDecl>(Val: Callee) && |
1972 | cast<CXXMethodDecl>(Val: Callee)->isImplicitObjectMemberFunction(); |
1973 | |
1974 | if (!IsMemberCall) |
1975 | Callee->getNameForDiagnostic(OS&: Out, Policy: Info.Ctx.getPrintingPolicy(), |
1976 | /*Qualified=*/false); |
1977 | |
1978 | if (This && IsMemberCall) { |
1979 | if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) { |
1980 | const Expr *Object = MCE->getImplicitObjectArgument(); |
1981 | Object->printPretty(Out, /*Helper=*/nullptr, Info.Ctx.getPrintingPolicy(), |
1982 | /*Indentation=*/0); |
1983 | if (Object->getType()->isPointerType()) |
1984 | Out << "->" ; |
1985 | else |
1986 | Out << "." ; |
1987 | } else if (const auto *OCE = |
1988 | dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) { |
1989 | OCE->getArg(0)->printPretty(Out, /*Helper=*/nullptr, |
1990 | Info.Ctx.getPrintingPolicy(), |
1991 | /*Indentation=*/0); |
1992 | Out << "." ; |
1993 | } else { |
1994 | APValue Val; |
1995 | This->moveInto(V&: Val); |
1996 | Val.printPretty( |
1997 | Out, Info.Ctx, |
1998 | Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType)); |
1999 | Out << "." ; |
2000 | } |
2001 | Callee->getNameForDiagnostic(OS&: Out, Policy: Info.Ctx.getPrintingPolicy(), |
2002 | /*Qualified=*/false); |
2003 | IsMemberCall = false; |
2004 | } |
2005 | |
2006 | Out << '('; |
2007 | |
2008 | for (FunctionDecl::param_const_iterator I = Callee->param_begin(), |
2009 | E = Callee->param_end(); I != E; ++I, ++ArgIndex) { |
2010 | if (ArgIndex > (unsigned)IsMemberCall) |
2011 | Out << ", " ; |
2012 | |
2013 | const ParmVarDecl *Param = *I; |
2014 | APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param); |
2015 | if (V) |
2016 | V->printPretty(Out, Info.Ctx, Param->getType()); |
2017 | else |
2018 | Out << "<...>" ; |
2019 | |
2020 | if (ArgIndex == 0 && IsMemberCall) |
2021 | Out << "->" << *Callee << '('; |
2022 | } |
2023 | |
2024 | Out << ')'; |
2025 | } |
2026 | |
2027 | /// Evaluate an expression to see if it had side-effects, and discard its |
2028 | /// result. |
2029 | /// \return \c true if the caller should keep evaluating. |
2030 | static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) { |
2031 | assert(!E->isValueDependent()); |
2032 | APValue Scratch; |
2033 | if (!Evaluate(Result&: Scratch, Info, E)) |
2034 | // We don't need the value, but we might have skipped a side effect here. |
2035 | return Info.noteSideEffect(); |
2036 | return true; |
2037 | } |
2038 | |
2039 | /// Should this call expression be treated as a no-op? |
2040 | static bool IsNoOpCall(const CallExpr *E) { |
2041 | unsigned Builtin = E->getBuiltinCallee(); |
2042 | return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString || |
2043 | Builtin == Builtin::BI__builtin___NSStringMakeConstantString || |
2044 | Builtin == Builtin::BI__builtin_function_start); |
2045 | } |
2046 | |
2047 | static bool IsGlobalLValue(APValue::LValueBase B) { |
2048 | // C++11 [expr.const]p3 An address constant expression is a prvalue core |
2049 | // constant expression of pointer type that evaluates to... |
2050 | |
2051 | // ... a null pointer value, or a prvalue core constant expression of type |
2052 | // std::nullptr_t. |
2053 | if (!B) |
2054 | return true; |
2055 | |
2056 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
2057 | // ... the address of an object with static storage duration, |
2058 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
2059 | return VD->hasGlobalStorage(); |
2060 | if (isa<TemplateParamObjectDecl>(Val: D)) |
2061 | return true; |
2062 | // ... the address of a function, |
2063 | // ... the address of a GUID [MS extension], |
2064 | // ... the address of an unnamed global constant |
2065 | return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D); |
2066 | } |
2067 | |
2068 | if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>()) |
2069 | return true; |
2070 | |
2071 | const Expr *E = B.get<const Expr*>(); |
2072 | switch (E->getStmtClass()) { |
2073 | default: |
2074 | return false; |
2075 | case Expr::CompoundLiteralExprClass: { |
2076 | const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E); |
2077 | return CLE->isFileScope() && CLE->isLValue(); |
2078 | } |
2079 | case Expr::MaterializeTemporaryExprClass: |
2080 | // A materialized temporary might have been lifetime-extended to static |
2081 | // storage duration. |
2082 | return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static; |
2083 | // A string literal has static storage duration. |
2084 | case Expr::StringLiteralClass: |
2085 | case Expr::PredefinedExprClass: |
2086 | case Expr::ObjCStringLiteralClass: |
2087 | case Expr::ObjCEncodeExprClass: |
2088 | return true; |
2089 | case Expr::ObjCBoxedExprClass: |
2090 | return cast<ObjCBoxedExpr>(Val: E)->isExpressibleAsConstantInitializer(); |
2091 | case Expr::CallExprClass: |
2092 | return IsNoOpCall(E: cast<CallExpr>(Val: E)); |
2093 | // For GCC compatibility, &&label has static storage duration. |
2094 | case Expr::AddrLabelExprClass: |
2095 | return true; |
2096 | // A Block literal expression may be used as the initialization value for |
2097 | // Block variables at global or local static scope. |
2098 | case Expr::BlockExprClass: |
2099 | return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures(); |
2100 | // The APValue generated from a __builtin_source_location will be emitted as a |
2101 | // literal. |
2102 | case Expr::SourceLocExprClass: |
2103 | return true; |
2104 | case Expr::ImplicitValueInitExprClass: |
2105 | // FIXME: |
2106 | // We can never form an lvalue with an implicit value initialization as its |
2107 | // base through expression evaluation, so these only appear in one case: the |
2108 | // implicit variable declaration we invent when checking whether a constexpr |
2109 | // constructor can produce a constant expression. We must assume that such |
2110 | // an expression might be a global lvalue. |
2111 | return true; |
2112 | } |
2113 | } |
2114 | |
2115 | static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) { |
2116 | return LVal.Base.dyn_cast<const ValueDecl*>(); |
2117 | } |
2118 | |
2119 | static bool IsLiteralLValue(const LValue &Value) { |
2120 | if (Value.getLValueCallIndex()) |
2121 | return false; |
2122 | const Expr *E = Value.Base.dyn_cast<const Expr*>(); |
2123 | return E && !isa<MaterializeTemporaryExpr>(Val: E); |
2124 | } |
2125 | |
2126 | static bool IsWeakLValue(const LValue &Value) { |
2127 | const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value); |
2128 | return Decl && Decl->isWeak(); |
2129 | } |
2130 | |
2131 | static bool isZeroSized(const LValue &Value) { |
2132 | const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value); |
2133 | if (Decl && isa<VarDecl>(Val: Decl)) { |
2134 | QualType Ty = Decl->getType(); |
2135 | if (Ty->isArrayType()) |
2136 | return Ty->isIncompleteType() || |
2137 | Decl->getASTContext().getTypeSize(Ty) == 0; |
2138 | } |
2139 | return false; |
2140 | } |
2141 | |
2142 | static bool HasSameBase(const LValue &A, const LValue &B) { |
2143 | if (!A.getLValueBase()) |
2144 | return !B.getLValueBase(); |
2145 | if (!B.getLValueBase()) |
2146 | return false; |
2147 | |
2148 | if (A.getLValueBase().getOpaqueValue() != |
2149 | B.getLValueBase().getOpaqueValue()) |
2150 | return false; |
2151 | |
2152 | return A.getLValueCallIndex() == B.getLValueCallIndex() && |
2153 | A.getLValueVersion() == B.getLValueVersion(); |
2154 | } |
2155 | |
2156 | static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) { |
2157 | assert(Base && "no location for a null lvalue" ); |
2158 | const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>(); |
2159 | |
2160 | // For a parameter, find the corresponding call stack frame (if it still |
2161 | // exists), and point at the parameter of the function definition we actually |
2162 | // invoked. |
2163 | if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) { |
2164 | unsigned Idx = PVD->getFunctionScopeIndex(); |
2165 | for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) { |
2166 | if (F->Arguments.CallIndex == Base.getCallIndex() && |
2167 | F->Arguments.Version == Base.getVersion() && F->Callee && |
2168 | Idx < F->Callee->getNumParams()) { |
2169 | VD = F->Callee->getParamDecl(i: Idx); |
2170 | break; |
2171 | } |
2172 | } |
2173 | } |
2174 | |
2175 | if (VD) |
2176 | Info.Note(VD->getLocation(), diag::note_declared_at); |
2177 | else if (const Expr *E = Base.dyn_cast<const Expr*>()) |
2178 | Info.Note(E->getExprLoc(), diag::note_constexpr_temporary_here); |
2179 | else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) { |
2180 | // FIXME: Produce a note for dangling pointers too. |
2181 | if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA)) |
2182 | Info.Note((*Alloc)->AllocExpr->getExprLoc(), |
2183 | diag::note_constexpr_dynamic_alloc_here); |
2184 | } |
2185 | |
2186 | // We have no information to show for a typeid(T) object. |
2187 | } |
2188 | |
2189 | enum class CheckEvaluationResultKind { |
2190 | ConstantExpression, |
2191 | FullyInitialized, |
2192 | }; |
2193 | |
2194 | /// Materialized temporaries that we've already checked to determine if they're |
2195 | /// initializsed by a constant expression. |
2196 | using CheckedTemporaries = |
2197 | llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>; |
2198 | |
2199 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2200 | EvalInfo &Info, SourceLocation DiagLoc, |
2201 | QualType Type, const APValue &Value, |
2202 | ConstantExprKind Kind, |
2203 | const FieldDecl *SubobjectDecl, |
2204 | CheckedTemporaries &CheckedTemps); |
2205 | |
2206 | /// Check that this reference or pointer core constant expression is a valid |
2207 | /// value for an address or reference constant expression. Return true if we |
2208 | /// can fold this expression, whether or not it's a constant expression. |
2209 | static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc, |
2210 | QualType Type, const LValue &LVal, |
2211 | ConstantExprKind Kind, |
2212 | CheckedTemporaries &CheckedTemps) { |
2213 | bool IsReferenceType = Type->isReferenceType(); |
2214 | |
2215 | APValue::LValueBase Base = LVal.getLValueBase(); |
2216 | const SubobjectDesignator &Designator = LVal.getLValueDesignator(); |
2217 | |
2218 | const Expr *BaseE = Base.dyn_cast<const Expr *>(); |
2219 | const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>(); |
2220 | |
2221 | // Additional restrictions apply in a template argument. We only enforce the |
2222 | // C++20 restrictions here; additional syntactic and semantic restrictions |
2223 | // are applied elsewhere. |
2224 | if (isTemplateArgument(Kind)) { |
2225 | int InvalidBaseKind = -1; |
2226 | StringRef Ident; |
2227 | if (Base.is<TypeInfoLValue>()) |
2228 | InvalidBaseKind = 0; |
2229 | else if (isa_and_nonnull<StringLiteral>(Val: BaseE)) |
2230 | InvalidBaseKind = 1; |
2231 | else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) || |
2232 | isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD)) |
2233 | InvalidBaseKind = 2; |
2234 | else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) { |
2235 | InvalidBaseKind = 3; |
2236 | Ident = PE->getIdentKindName(); |
2237 | } |
2238 | |
2239 | if (InvalidBaseKind != -1) { |
2240 | Info.FFDiag(Loc, diag::note_constexpr_invalid_template_arg) |
2241 | << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind |
2242 | << Ident; |
2243 | return false; |
2244 | } |
2245 | } |
2246 | |
2247 | if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD); |
2248 | FD && FD->isImmediateFunction()) { |
2249 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) |
2250 | << !Type->isAnyPointerType(); |
2251 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2252 | return false; |
2253 | } |
2254 | |
2255 | // Check that the object is a global. Note that the fake 'this' object we |
2256 | // manufacture when checking potential constant expressions is conservatively |
2257 | // assumed to be global here. |
2258 | if (!IsGlobalLValue(B: Base)) { |
2259 | if (Info.getLangOpts().CPlusPlus11) { |
2260 | Info.FFDiag(Loc, diag::note_constexpr_non_global, 1) |
2261 | << IsReferenceType << !Designator.Entries.empty() << !!BaseVD |
2262 | << BaseVD; |
2263 | auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD); |
2264 | if (VarD && VarD->isConstexpr()) { |
2265 | // Non-static local constexpr variables have unintuitive semantics: |
2266 | // constexpr int a = 1; |
2267 | // constexpr const int *p = &a; |
2268 | // ... is invalid because the address of 'a' is not constant. Suggest |
2269 | // adding a 'static' in this case. |
2270 | Info.Note(VarD->getLocation(), diag::note_constexpr_not_static) |
2271 | << VarD |
2272 | << FixItHint::CreateInsertion(VarD->getBeginLoc(), "static " ); |
2273 | } else { |
2274 | NoteLValueLocation(Info, Base); |
2275 | } |
2276 | } else { |
2277 | Info.FFDiag(Loc); |
2278 | } |
2279 | // Don't allow references to temporaries to escape. |
2280 | return false; |
2281 | } |
2282 | assert((Info.checkingPotentialConstantExpression() || |
2283 | LVal.getLValueCallIndex() == 0) && |
2284 | "have call index for global lvalue" ); |
2285 | |
2286 | if (Base.is<DynamicAllocLValue>()) { |
2287 | Info.FFDiag(Loc, diag::note_constexpr_dynamic_alloc) |
2288 | << IsReferenceType << !Designator.Entries.empty(); |
2289 | NoteLValueLocation(Info, Base); |
2290 | return false; |
2291 | } |
2292 | |
2293 | if (BaseVD) { |
2294 | if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) { |
2295 | // Check if this is a thread-local variable. |
2296 | if (Var->getTLSKind()) |
2297 | // FIXME: Diagnostic! |
2298 | return false; |
2299 | |
2300 | // A dllimport variable never acts like a constant, unless we're |
2301 | // evaluating a value for use only in name mangling. |
2302 | if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>()) |
2303 | // FIXME: Diagnostic! |
2304 | return false; |
2305 | |
2306 | // In CUDA/HIP device compilation, only device side variables have |
2307 | // constant addresses. |
2308 | if (Info.getCtx().getLangOpts().CUDA && |
2309 | Info.getCtx().getLangOpts().CUDAIsDevice && |
2310 | Info.getCtx().CUDAConstantEvalCtx.NoWrongSidedVars) { |
2311 | if ((!Var->hasAttr<CUDADeviceAttr>() && |
2312 | !Var->hasAttr<CUDAConstantAttr>() && |
2313 | !Var->getType()->isCUDADeviceBuiltinSurfaceType() && |
2314 | !Var->getType()->isCUDADeviceBuiltinTextureType()) || |
2315 | Var->hasAttr<HIPManagedAttr>()) |
2316 | return false; |
2317 | } |
2318 | } |
2319 | if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) { |
2320 | // __declspec(dllimport) must be handled very carefully: |
2321 | // We must never initialize an expression with the thunk in C++. |
2322 | // Doing otherwise would allow the same id-expression to yield |
2323 | // different addresses for the same function in different translation |
2324 | // units. However, this means that we must dynamically initialize the |
2325 | // expression with the contents of the import address table at runtime. |
2326 | // |
2327 | // The C language has no notion of ODR; furthermore, it has no notion of |
2328 | // dynamic initialization. This means that we are permitted to |
2329 | // perform initialization with the address of the thunk. |
2330 | if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) && |
2331 | FD->hasAttr<DLLImportAttr>()) |
2332 | // FIXME: Diagnostic! |
2333 | return false; |
2334 | } |
2335 | } else if (const auto *MTE = |
2336 | dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) { |
2337 | if (CheckedTemps.insert(Ptr: MTE).second) { |
2338 | QualType TempType = getType(B: Base); |
2339 | if (TempType.isDestructedType()) { |
2340 | Info.FFDiag(MTE->getExprLoc(), |
2341 | diag::note_constexpr_unsupported_temporary_nontrivial_dtor) |
2342 | << TempType; |
2343 | return false; |
2344 | } |
2345 | |
2346 | APValue *V = MTE->getOrCreateValue(MayCreate: false); |
2347 | assert(V && "evasluation result refers to uninitialised temporary" ); |
2348 | if (!CheckEvaluationResult(CheckEvaluationResultKind::ConstantExpression, |
2349 | Info, MTE->getExprLoc(), TempType, *V, Kind, |
2350 | /*SubobjectDecl=*/nullptr, CheckedTemps)) |
2351 | return false; |
2352 | } |
2353 | } |
2354 | |
2355 | // Allow address constant expressions to be past-the-end pointers. This is |
2356 | // an extension: the standard requires them to point to an object. |
2357 | if (!IsReferenceType) |
2358 | return true; |
2359 | |
2360 | // A reference constant expression must refer to an object. |
2361 | if (!Base) { |
2362 | // FIXME: diagnostic |
2363 | Info.CCEDiag(Loc); |
2364 | return true; |
2365 | } |
2366 | |
2367 | // Does this refer one past the end of some object? |
2368 | if (!Designator.Invalid && Designator.isOnePastTheEnd()) { |
2369 | Info.FFDiag(Loc, diag::note_constexpr_past_end, 1) |
2370 | << !Designator.Entries.empty() << !!BaseVD << BaseVD; |
2371 | NoteLValueLocation(Info, Base); |
2372 | } |
2373 | |
2374 | return true; |
2375 | } |
2376 | |
2377 | /// Member pointers are constant expressions unless they point to a |
2378 | /// non-virtual dllimport member function. |
2379 | static bool CheckMemberPointerConstantExpression(EvalInfo &Info, |
2380 | SourceLocation Loc, |
2381 | QualType Type, |
2382 | const APValue &Value, |
2383 | ConstantExprKind Kind) { |
2384 | const ValueDecl *Member = Value.getMemberPointerDecl(); |
2385 | const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member); |
2386 | if (!FD) |
2387 | return true; |
2388 | if (FD->isImmediateFunction()) { |
2389 | Info.FFDiag(Loc, diag::note_consteval_address_accessible) << /*pointer*/ 0; |
2390 | Info.Note(FD->getLocation(), diag::note_declared_at); |
2391 | return false; |
2392 | } |
2393 | return isForManglingOnly(Kind) || FD->isVirtual() || |
2394 | !FD->hasAttr<DLLImportAttr>(); |
2395 | } |
2396 | |
2397 | /// Check that this core constant expression is of literal type, and if not, |
2398 | /// produce an appropriate diagnostic. |
2399 | static bool CheckLiteralType(EvalInfo &Info, const Expr *E, |
2400 | const LValue *This = nullptr) { |
2401 | if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx)) |
2402 | return true; |
2403 | |
2404 | // C++1y: A constant initializer for an object o [...] may also invoke |
2405 | // constexpr constructors for o and its subobjects even if those objects |
2406 | // are of non-literal class types. |
2407 | // |
2408 | // C++11 missed this detail for aggregates, so classes like this: |
2409 | // struct foo_t { union { int i; volatile int j; } u; }; |
2410 | // are not (obviously) initializable like so: |
2411 | // __attribute__((__require_constant_initialization__)) |
2412 | // static const foo_t x = {{0}}; |
2413 | // because "i" is a subobject with non-literal initialization (due to the |
2414 | // volatile member of the union). See: |
2415 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677 |
2416 | // Therefore, we use the C++1y behavior. |
2417 | if (This && Info.EvaluatingDecl == This->getLValueBase()) |
2418 | return true; |
2419 | |
2420 | // Prvalue constant expressions must be of literal types. |
2421 | if (Info.getLangOpts().CPlusPlus11) |
2422 | Info.FFDiag(E, diag::note_constexpr_nonliteral) |
2423 | << E->getType(); |
2424 | else |
2425 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
2426 | return false; |
2427 | } |
2428 | |
2429 | static bool CheckEvaluationResult(CheckEvaluationResultKind CERK, |
2430 | EvalInfo &Info, SourceLocation DiagLoc, |
2431 | QualType Type, const APValue &Value, |
2432 | ConstantExprKind Kind, |
2433 | const FieldDecl *SubobjectDecl, |
2434 | CheckedTemporaries &CheckedTemps) { |
2435 | if (!Value.hasValue()) { |
2436 | if (SubobjectDecl) { |
2437 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2438 | << /*(name)*/ 1 << SubobjectDecl; |
2439 | Info.Note(SubobjectDecl->getLocation(), |
2440 | diag::note_constexpr_subobject_declared_here); |
2441 | } else { |
2442 | Info.FFDiag(DiagLoc, diag::note_constexpr_uninitialized) |
2443 | << /*of type*/ 0 << Type; |
2444 | } |
2445 | return false; |
2446 | } |
2447 | |
2448 | // We allow _Atomic(T) to be initialized from anything that T can be |
2449 | // initialized from. |
2450 | if (const AtomicType *AT = Type->getAs<AtomicType>()) |
2451 | Type = AT->getValueType(); |
2452 | |
2453 | // Core issue 1454: For a literal constant expression of array or class type, |
2454 | // each subobject of its value shall have been initialized by a constant |
2455 | // expression. |
2456 | if (Value.isArray()) { |
2457 | QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType(); |
2458 | for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) { |
2459 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy, |
2460 | Value: Value.getArrayInitializedElt(I), Kind, |
2461 | SubobjectDecl, CheckedTemps)) |
2462 | return false; |
2463 | } |
2464 | if (!Value.hasArrayFiller()) |
2465 | return true; |
2466 | return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy, |
2467 | Value: Value.getArrayFiller(), Kind, SubobjectDecl, |
2468 | CheckedTemps); |
2469 | } |
2470 | if (Value.isUnion() && Value.getUnionField()) { |
2471 | return CheckEvaluationResult( |
2472 | CERK, Info, DiagLoc, Value.getUnionField()->getType(), |
2473 | Value.getUnionValue(), Kind, Value.getUnionField(), CheckedTemps); |
2474 | } |
2475 | if (Value.isStruct()) { |
2476 | RecordDecl *RD = Type->castAs<RecordType>()->getDecl(); |
2477 | if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
2478 | unsigned BaseIndex = 0; |
2479 | for (const CXXBaseSpecifier &BS : CD->bases()) { |
2480 | const APValue &BaseValue = Value.getStructBase(i: BaseIndex); |
2481 | if (!BaseValue.hasValue()) { |
2482 | SourceLocation TypeBeginLoc = BS.getBaseTypeLoc(); |
2483 | Info.FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base) |
2484 | << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc()); |
2485 | return false; |
2486 | } |
2487 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue, |
2488 | Kind, /*SubobjectDecl=*/nullptr, |
2489 | CheckedTemps)) |
2490 | return false; |
2491 | ++BaseIndex; |
2492 | } |
2493 | } |
2494 | for (const auto *I : RD->fields()) { |
2495 | if (I->isUnnamedBitField()) |
2496 | continue; |
2497 | |
2498 | if (!CheckEvaluationResult(CERK, Info, DiagLoc, I->getType(), |
2499 | Value.getStructField(i: I->getFieldIndex()), Kind, |
2500 | I, CheckedTemps)) |
2501 | return false; |
2502 | } |
2503 | } |
2504 | |
2505 | if (Value.isLValue() && |
2506 | CERK == CheckEvaluationResultKind::ConstantExpression) { |
2507 | LValue LVal; |
2508 | LVal.setFrom(Ctx&: Info.Ctx, V: Value); |
2509 | return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind, |
2510 | CheckedTemps); |
2511 | } |
2512 | |
2513 | if (Value.isMemberPointer() && |
2514 | CERK == CheckEvaluationResultKind::ConstantExpression) |
2515 | return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind); |
2516 | |
2517 | // Everything else is fine. |
2518 | return true; |
2519 | } |
2520 | |
2521 | /// Check that this core constant expression value is a valid value for a |
2522 | /// constant expression. If not, report an appropriate diagnostic. Does not |
2523 | /// check that the expression is of literal type. |
2524 | static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc, |
2525 | QualType Type, const APValue &Value, |
2526 | ConstantExprKind Kind) { |
2527 | // Nothing to check for a constant expression of type 'cv void'. |
2528 | if (Type->isVoidType()) |
2529 | return true; |
2530 | |
2531 | CheckedTemporaries CheckedTemps; |
2532 | return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression, |
2533 | Info, DiagLoc, Type, Value, Kind, |
2534 | /*SubobjectDecl=*/nullptr, CheckedTemps); |
2535 | } |
2536 | |
2537 | /// Check that this evaluated value is fully-initialized and can be loaded by |
2538 | /// an lvalue-to-rvalue conversion. |
2539 | static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc, |
2540 | QualType Type, const APValue &Value) { |
2541 | CheckedTemporaries CheckedTemps; |
2542 | return CheckEvaluationResult( |
2543 | CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value, |
2544 | Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps); |
2545 | } |
2546 | |
2547 | /// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless |
2548 | /// "the allocated storage is deallocated within the evaluation". |
2549 | static bool CheckMemoryLeaks(EvalInfo &Info) { |
2550 | if (!Info.HeapAllocs.empty()) { |
2551 | // We can still fold to a constant despite a compile-time memory leak, |
2552 | // so long as the heap allocation isn't referenced in the result (we check |
2553 | // that in CheckConstantExpression). |
2554 | Info.CCEDiag(Info.HeapAllocs.begin()->second.AllocExpr, |
2555 | diag::note_constexpr_memory_leak) |
2556 | << unsigned(Info.HeapAllocs.size() - 1); |
2557 | } |
2558 | return true; |
2559 | } |
2560 | |
2561 | static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) { |
2562 | // A null base expression indicates a null pointer. These are always |
2563 | // evaluatable, and they are false unless the offset is zero. |
2564 | if (!Value.getLValueBase()) { |
2565 | // TODO: Should a non-null pointer with an offset of zero evaluate to true? |
2566 | Result = !Value.getLValueOffset().isZero(); |
2567 | return true; |
2568 | } |
2569 | |
2570 | // We have a non-null base. These are generally known to be true, but if it's |
2571 | // a weak declaration it can be null at runtime. |
2572 | Result = true; |
2573 | const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>(); |
2574 | return !Decl || !Decl->isWeak(); |
2575 | } |
2576 | |
2577 | static bool HandleConversionToBool(const APValue &Val, bool &Result) { |
2578 | // TODO: This function should produce notes if it fails. |
2579 | switch (Val.getKind()) { |
2580 | case APValue::None: |
2581 | case APValue::Indeterminate: |
2582 | return false; |
2583 | case APValue::Int: |
2584 | Result = Val.getInt().getBoolValue(); |
2585 | return true; |
2586 | case APValue::FixedPoint: |
2587 | Result = Val.getFixedPoint().getBoolValue(); |
2588 | return true; |
2589 | case APValue::Float: |
2590 | Result = !Val.getFloat().isZero(); |
2591 | return true; |
2592 | case APValue::ComplexInt: |
2593 | Result = Val.getComplexIntReal().getBoolValue() || |
2594 | Val.getComplexIntImag().getBoolValue(); |
2595 | return true; |
2596 | case APValue::ComplexFloat: |
2597 | Result = !Val.getComplexFloatReal().isZero() || |
2598 | !Val.getComplexFloatImag().isZero(); |
2599 | return true; |
2600 | case APValue::LValue: |
2601 | return EvalPointerValueAsBool(Value: Val, Result); |
2602 | case APValue::MemberPointer: |
2603 | if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) { |
2604 | return false; |
2605 | } |
2606 | Result = Val.getMemberPointerDecl(); |
2607 | return true; |
2608 | case APValue::Vector: |
2609 | case APValue::Array: |
2610 | case APValue::Struct: |
2611 | case APValue::Union: |
2612 | case APValue::AddrLabelDiff: |
2613 | return false; |
2614 | } |
2615 | |
2616 | llvm_unreachable("unknown APValue kind" ); |
2617 | } |
2618 | |
2619 | static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result, |
2620 | EvalInfo &Info) { |
2621 | assert(!E->isValueDependent()); |
2622 | assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition" ); |
2623 | APValue Val; |
2624 | if (!Evaluate(Result&: Val, Info, E)) |
2625 | return false; |
2626 | return HandleConversionToBool(Val, Result); |
2627 | } |
2628 | |
2629 | template<typename T> |
2630 | static bool HandleOverflow(EvalInfo &Info, const Expr *E, |
2631 | const T &SrcValue, QualType DestType) { |
2632 | Info.CCEDiag(E, diag::note_constexpr_overflow) |
2633 | << SrcValue << DestType; |
2634 | return Info.noteUndefinedBehavior(); |
2635 | } |
2636 | |
2637 | static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E, |
2638 | QualType SrcType, const APFloat &Value, |
2639 | QualType DestType, APSInt &Result) { |
2640 | unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType); |
2641 | // Determine whether we are converting to unsigned or signed. |
2642 | bool DestSigned = DestType->isSignedIntegerOrEnumerationType(); |
2643 | |
2644 | Result = APSInt(DestWidth, !DestSigned); |
2645 | bool ignored; |
2646 | if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored) |
2647 | & APFloat::opInvalidOp) |
2648 | return HandleOverflow(Info, E, SrcValue: Value, DestType); |
2649 | return true; |
2650 | } |
2651 | |
2652 | /// Get rounding mode to use in evaluation of the specified expression. |
2653 | /// |
2654 | /// If rounding mode is unknown at compile time, still try to evaluate the |
2655 | /// expression. If the result is exact, it does not depend on rounding mode. |
2656 | /// So return "tonearest" mode instead of "dynamic". |
2657 | static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) { |
2658 | llvm::RoundingMode RM = |
2659 | E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).getRoundingMode(); |
2660 | if (RM == llvm::RoundingMode::Dynamic) |
2661 | RM = llvm::RoundingMode::NearestTiesToEven; |
2662 | return RM; |
2663 | } |
2664 | |
2665 | /// Check if the given evaluation result is allowed for constant evaluation. |
2666 | static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E, |
2667 | APFloat::opStatus St) { |
2668 | // In a constant context, assume that any dynamic rounding mode or FP |
2669 | // exception state matches the default floating-point environment. |
2670 | if (Info.InConstantContext) |
2671 | return true; |
2672 | |
2673 | FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()); |
2674 | if ((St & APFloat::opInexact) && |
2675 | FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) { |
2676 | // Inexact result means that it depends on rounding mode. If the requested |
2677 | // mode is dynamic, the evaluation cannot be made in compile time. |
2678 | Info.FFDiag(E, diag::note_constexpr_dynamic_rounding); |
2679 | return false; |
2680 | } |
2681 | |
2682 | if ((St != APFloat::opOK) && |
2683 | (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic || |
2684 | FPO.getExceptionMode() != LangOptions::FPE_Ignore || |
2685 | FPO.getAllowFEnvAccess())) { |
2686 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
2687 | return false; |
2688 | } |
2689 | |
2690 | if ((St & APFloat::opStatus::opInvalidOp) && |
2691 | FPO.getExceptionMode() != LangOptions::FPE_Ignore) { |
2692 | // There is no usefully definable result. |
2693 | Info.FFDiag(E); |
2694 | return false; |
2695 | } |
2696 | |
2697 | // FIXME: if: |
2698 | // - evaluation triggered other FP exception, and |
2699 | // - exception mode is not "ignore", and |
2700 | // - the expression being evaluated is not a part of global variable |
2701 | // initializer, |
2702 | // the evaluation probably need to be rejected. |
2703 | return true; |
2704 | } |
2705 | |
2706 | static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E, |
2707 | QualType SrcType, QualType DestType, |
2708 | APFloat &Result) { |
2709 | assert(isa<CastExpr>(E) || isa<CompoundAssignOperator>(E)); |
2710 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2711 | APFloat::opStatus St; |
2712 | APFloat Value = Result; |
2713 | bool ignored; |
2714 | St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored); |
2715 | return checkFloatingPointResult(Info, E, St); |
2716 | } |
2717 | |
2718 | static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, |
2719 | QualType DestType, QualType SrcType, |
2720 | const APSInt &Value) { |
2721 | unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType); |
2722 | // Figure out if this is a truncate, extend or noop cast. |
2723 | // If the input is signed, do a sign extend, noop, or truncate. |
2724 | APSInt Result = Value.extOrTrunc(width: DestWidth); |
2725 | Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); |
2726 | if (DestType->isBooleanType()) |
2727 | Result = Value.getBoolValue(); |
2728 | return Result; |
2729 | } |
2730 | |
2731 | static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E, |
2732 | const FPOptions FPO, |
2733 | QualType SrcType, const APSInt &Value, |
2734 | QualType DestType, APFloat &Result) { |
2735 | Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1); |
2736 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2737 | APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM); |
2738 | return checkFloatingPointResult(Info, E, St); |
2739 | } |
2740 | |
2741 | static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E, |
2742 | APValue &Value, const FieldDecl *FD) { |
2743 | assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield" ); |
2744 | |
2745 | if (!Value.isInt()) { |
2746 | // Trying to store a pointer-cast-to-integer into a bitfield. |
2747 | // FIXME: In this case, we should provide the diagnostic for casting |
2748 | // a pointer to an integer. |
2749 | assert(Value.isLValue() && "integral value neither int nor lvalue?" ); |
2750 | Info.FFDiag(E); |
2751 | return false; |
2752 | } |
2753 | |
2754 | APSInt &Int = Value.getInt(); |
2755 | unsigned OldBitWidth = Int.getBitWidth(); |
2756 | unsigned NewBitWidth = FD->getBitWidthValue(Ctx: Info.Ctx); |
2757 | if (NewBitWidth < OldBitWidth) |
2758 | Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth); |
2759 | return true; |
2760 | } |
2761 | |
2762 | /// Perform the given integer operation, which is known to need at most BitWidth |
2763 | /// bits, and check for overflow in the original type (if that type was not an |
2764 | /// unsigned type). |
2765 | template<typename Operation> |
2766 | static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E, |
2767 | const APSInt &LHS, const APSInt &RHS, |
2768 | unsigned BitWidth, Operation Op, |
2769 | APSInt &Result) { |
2770 | if (LHS.isUnsigned()) { |
2771 | Result = Op(LHS, RHS); |
2772 | return true; |
2773 | } |
2774 | |
2775 | APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false); |
2776 | Result = Value.trunc(width: LHS.getBitWidth()); |
2777 | if (Result.extend(width: BitWidth) != Value) { |
2778 | if (Info.checkingForUndefinedBehavior()) |
2779 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
2780 | diag::warn_integer_constant_overflow) |
2781 | << toString(Result, 10, Result.isSigned(), /*formatAsCLiteral=*/false, |
2782 | /*UpperCase=*/true, /*InsertSeparators=*/true) |
2783 | << E->getType() << E->getSourceRange(); |
2784 | return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType()); |
2785 | } |
2786 | return true; |
2787 | } |
2788 | |
2789 | /// Perform the given binary integer operation. |
2790 | static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E, |
2791 | const APSInt &LHS, BinaryOperatorKind Opcode, |
2792 | APSInt RHS, APSInt &Result) { |
2793 | bool HandleOverflowResult = true; |
2794 | switch (Opcode) { |
2795 | default: |
2796 | Info.FFDiag(E); |
2797 | return false; |
2798 | case BO_Mul: |
2799 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2800 | std::multiplies<APSInt>(), Result); |
2801 | case BO_Add: |
2802 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2803 | std::plus<APSInt>(), Result); |
2804 | case BO_Sub: |
2805 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2806 | std::minus<APSInt>(), Result); |
2807 | case BO_And: Result = LHS & RHS; return true; |
2808 | case BO_Xor: Result = LHS ^ RHS; return true; |
2809 | case BO_Or: Result = LHS | RHS; return true; |
2810 | case BO_Div: |
2811 | case BO_Rem: |
2812 | if (RHS == 0) { |
2813 | Info.FFDiag(E, diag::note_expr_divide_by_zero) |
2814 | << E->getRHS()->getSourceRange(); |
2815 | return false; |
2816 | } |
2817 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
2818 | // this operation and gives the two's complement result. |
2819 | if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() && |
2820 | LHS.isMinSignedValue()) |
2821 | HandleOverflowResult = HandleOverflow( |
2822 | Info, E, -LHS.extend(width: LHS.getBitWidth() + 1), E->getType()); |
2823 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2824 | return HandleOverflowResult; |
2825 | case BO_Shl: { |
2826 | if (Info.getLangOpts().OpenCL) |
2827 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
2828 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2829 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2830 | RHS.isUnsigned()); |
2831 | else if (RHS.isSigned() && RHS.isNegative()) { |
2832 | // During constant-folding, a negative shift is an opposite shift. Such |
2833 | // a shift is not a constant expression. |
2834 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2835 | RHS = -RHS; |
2836 | goto shift_right; |
2837 | } |
2838 | shift_left: |
2839 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
2840 | // the shifted type. |
2841 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
2842 | if (SA != RHS) { |
2843 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2844 | << RHS << E->getType() << LHS.getBitWidth(); |
2845 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) { |
2846 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
2847 | // operand, and must not overflow the corresponding unsigned type. |
2848 | // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to |
2849 | // E1 x 2^E2 module 2^N. |
2850 | if (LHS.isNegative()) |
2851 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
2852 | else if (LHS.countl_zero() < SA) |
2853 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
2854 | } |
2855 | Result = LHS << SA; |
2856 | return true; |
2857 | } |
2858 | case BO_Shr: { |
2859 | if (Info.getLangOpts().OpenCL) |
2860 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
2861 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2862 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2863 | RHS.isUnsigned()); |
2864 | else if (RHS.isSigned() && RHS.isNegative()) { |
2865 | // During constant-folding, a negative shift is an opposite shift. Such a |
2866 | // shift is not a constant expression. |
2867 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2868 | RHS = -RHS; |
2869 | goto shift_left; |
2870 | } |
2871 | shift_right: |
2872 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
2873 | // shifted type. |
2874 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
2875 | if (SA != RHS) |
2876 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2877 | << RHS << E->getType() << LHS.getBitWidth(); |
2878 | Result = LHS >> SA; |
2879 | return true; |
2880 | } |
2881 | |
2882 | case BO_LT: Result = LHS < RHS; return true; |
2883 | case BO_GT: Result = LHS > RHS; return true; |
2884 | case BO_LE: Result = LHS <= RHS; return true; |
2885 | case BO_GE: Result = LHS >= RHS; return true; |
2886 | case BO_EQ: Result = LHS == RHS; return true; |
2887 | case BO_NE: Result = LHS != RHS; return true; |
2888 | case BO_Cmp: |
2889 | llvm_unreachable("BO_Cmp should be handled elsewhere" ); |
2890 | } |
2891 | } |
2892 | |
2893 | /// Perform the given binary floating-point operation, in-place, on LHS. |
2894 | static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E, |
2895 | APFloat &LHS, BinaryOperatorKind Opcode, |
2896 | const APFloat &RHS) { |
2897 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2898 | APFloat::opStatus St; |
2899 | switch (Opcode) { |
2900 | default: |
2901 | Info.FFDiag(E); |
2902 | return false; |
2903 | case BO_Mul: |
2904 | St = LHS.multiply(RHS, RM); |
2905 | break; |
2906 | case BO_Add: |
2907 | St = LHS.add(RHS, RM); |
2908 | break; |
2909 | case BO_Sub: |
2910 | St = LHS.subtract(RHS, RM); |
2911 | break; |
2912 | case BO_Div: |
2913 | // [expr.mul]p4: |
2914 | // If the second operand of / or % is zero the behavior is undefined. |
2915 | if (RHS.isZero()) |
2916 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
2917 | St = LHS.divide(RHS, RM); |
2918 | break; |
2919 | } |
2920 | |
2921 | // [expr.pre]p4: |
2922 | // If during the evaluation of an expression, the result is not |
2923 | // mathematically defined [...], the behavior is undefined. |
2924 | // FIXME: C++ rules require us to not conform to IEEE 754 here. |
2925 | if (LHS.isNaN()) { |
2926 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
2927 | return Info.noteUndefinedBehavior(); |
2928 | } |
2929 | |
2930 | return checkFloatingPointResult(Info, E, St); |
2931 | } |
2932 | |
2933 | static bool handleLogicalOpForVector(const APInt &LHSValue, |
2934 | BinaryOperatorKind Opcode, |
2935 | const APInt &RHSValue, APInt &Result) { |
2936 | bool LHS = (LHSValue != 0); |
2937 | bool RHS = (RHSValue != 0); |
2938 | |
2939 | if (Opcode == BO_LAnd) |
2940 | Result = LHS && RHS; |
2941 | else |
2942 | Result = LHS || RHS; |
2943 | return true; |
2944 | } |
2945 | static bool handleLogicalOpForVector(const APFloat &LHSValue, |
2946 | BinaryOperatorKind Opcode, |
2947 | const APFloat &RHSValue, APInt &Result) { |
2948 | bool LHS = !LHSValue.isZero(); |
2949 | bool RHS = !RHSValue.isZero(); |
2950 | |
2951 | if (Opcode == BO_LAnd) |
2952 | Result = LHS && RHS; |
2953 | else |
2954 | Result = LHS || RHS; |
2955 | return true; |
2956 | } |
2957 | |
2958 | static bool handleLogicalOpForVector(const APValue &LHSValue, |
2959 | BinaryOperatorKind Opcode, |
2960 | const APValue &RHSValue, APInt &Result) { |
2961 | // The result is always an int type, however operands match the first. |
2962 | if (LHSValue.getKind() == APValue::Int) |
2963 | return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode, |
2964 | RHSValue: RHSValue.getInt(), Result); |
2965 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options" ); |
2966 | return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode, |
2967 | RHSValue: RHSValue.getFloat(), Result); |
2968 | } |
2969 | |
2970 | template <typename APTy> |
2971 | static bool |
2972 | handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode, |
2973 | const APTy &RHSValue, APInt &Result) { |
2974 | switch (Opcode) { |
2975 | default: |
2976 | llvm_unreachable("unsupported binary operator" ); |
2977 | case BO_EQ: |
2978 | Result = (LHSValue == RHSValue); |
2979 | break; |
2980 | case BO_NE: |
2981 | Result = (LHSValue != RHSValue); |
2982 | break; |
2983 | case BO_LT: |
2984 | Result = (LHSValue < RHSValue); |
2985 | break; |
2986 | case BO_GT: |
2987 | Result = (LHSValue > RHSValue); |
2988 | break; |
2989 | case BO_LE: |
2990 | Result = (LHSValue <= RHSValue); |
2991 | break; |
2992 | case BO_GE: |
2993 | Result = (LHSValue >= RHSValue); |
2994 | break; |
2995 | } |
2996 | |
2997 | // The boolean operations on these vector types use an instruction that |
2998 | // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1 |
2999 | // to -1 to make sure that we produce the correct value. |
3000 | Result.negate(); |
3001 | |
3002 | return true; |
3003 | } |
3004 | |
3005 | static bool handleCompareOpForVector(const APValue &LHSValue, |
3006 | BinaryOperatorKind Opcode, |
3007 | const APValue &RHSValue, APInt &Result) { |
3008 | // The result is always an int type, however operands match the first. |
3009 | if (LHSValue.getKind() == APValue::Int) |
3010 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode, |
3011 | RHSValue: RHSValue.getInt(), Result); |
3012 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options" ); |
3013 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode, |
3014 | RHSValue: RHSValue.getFloat(), Result); |
3015 | } |
3016 | |
3017 | // Perform binary operations for vector types, in place on the LHS. |
3018 | static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E, |
3019 | BinaryOperatorKind Opcode, |
3020 | APValue &LHSValue, |
3021 | const APValue &RHSValue) { |
3022 | assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI && |
3023 | "Operation not supported on vector types" ); |
3024 | |
3025 | const auto *VT = E->getType()->castAs<VectorType>(); |
3026 | unsigned NumElements = VT->getNumElements(); |
3027 | QualType EltTy = VT->getElementType(); |
3028 | |
3029 | // In the cases (typically C as I've observed) where we aren't evaluating |
3030 | // constexpr but are checking for cases where the LHS isn't yet evaluatable, |
3031 | // just give up. |
3032 | if (!LHSValue.isVector()) { |
3033 | assert(LHSValue.isLValue() && |
3034 | "A vector result that isn't a vector OR uncalculated LValue" ); |
3035 | Info.FFDiag(E); |
3036 | return false; |
3037 | } |
3038 | |
3039 | assert(LHSValue.getVectorLength() == NumElements && |
3040 | RHSValue.getVectorLength() == NumElements && "Different vector sizes" ); |
3041 | |
3042 | SmallVector<APValue, 4> ResultElements; |
3043 | |
3044 | for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) { |
3045 | APValue LHSElt = LHSValue.getVectorElt(I: EltNum); |
3046 | APValue RHSElt = RHSValue.getVectorElt(I: EltNum); |
3047 | |
3048 | if (EltTy->isIntegerType()) { |
3049 | APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy), |
3050 | EltTy->isUnsignedIntegerType()}; |
3051 | bool Success = true; |
3052 | |
3053 | if (BinaryOperator::isLogicalOp(Opc: Opcode)) |
3054 | Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3055 | else if (BinaryOperator::isComparisonOp(Opc: Opcode)) |
3056 | Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3057 | else |
3058 | Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode, |
3059 | RHS: RHSElt.getInt(), Result&: EltResult); |
3060 | |
3061 | if (!Success) { |
3062 | Info.FFDiag(E); |
3063 | return false; |
3064 | } |
3065 | ResultElements.emplace_back(Args&: EltResult); |
3066 | |
3067 | } else if (EltTy->isFloatingType()) { |
3068 | assert(LHSElt.getKind() == APValue::Float && |
3069 | RHSElt.getKind() == APValue::Float && |
3070 | "Mismatched LHS/RHS/Result Type" ); |
3071 | APFloat LHSFloat = LHSElt.getFloat(); |
3072 | |
3073 | if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode, |
3074 | RHS: RHSElt.getFloat())) { |
3075 | Info.FFDiag(E); |
3076 | return false; |
3077 | } |
3078 | |
3079 | ResultElements.emplace_back(Args&: LHSFloat); |
3080 | } |
3081 | } |
3082 | |
3083 | LHSValue = APValue(ResultElements.data(), ResultElements.size()); |
3084 | return true; |
3085 | } |
3086 | |
3087 | /// Cast an lvalue referring to a base subobject to a derived class, by |
3088 | /// truncating the lvalue's path to the given length. |
3089 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3090 | const RecordDecl *TruncatedType, |
3091 | unsigned TruncatedElements) { |
3092 | SubobjectDesignator &D = Result.Designator; |
3093 | |
3094 | // Check we actually point to a derived class object. |
3095 | if (TruncatedElements == D.Entries.size()) |
3096 | return true; |
3097 | assert(TruncatedElements >= D.MostDerivedPathLength && |
3098 | "not casting to a derived class" ); |
3099 | if (!Result.checkSubobject(Info, E, CSK: CSK_Derived)) |
3100 | return false; |
3101 | |
3102 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
3103 | const RecordDecl *RD = TruncatedType; |
3104 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
3105 | if (RD->isInvalidDecl()) return false; |
3106 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
3107 | const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]); |
3108 | if (isVirtualBaseClass(E: D.Entries[I])) |
3109 | Result.Offset -= Layout.getVBaseClassOffset(VBase: Base); |
3110 | else |
3111 | Result.Offset -= Layout.getBaseClassOffset(Base); |
3112 | RD = Base; |
3113 | } |
3114 | D.Entries.resize(N: TruncatedElements); |
3115 | return true; |
3116 | } |
3117 | |
3118 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3119 | const CXXRecordDecl *Derived, |
3120 | const CXXRecordDecl *Base, |
3121 | const ASTRecordLayout *RL = nullptr) { |
3122 | if (!RL) { |
3123 | if (Derived->isInvalidDecl()) return false; |
3124 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
3125 | } |
3126 | |
3127 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
3128 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
3129 | return true; |
3130 | } |
3131 | |
3132 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3133 | const CXXRecordDecl *DerivedDecl, |
3134 | const CXXBaseSpecifier *Base) { |
3135 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
3136 | |
3137 | if (!Base->isVirtual()) |
3138 | return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl); |
3139 | |
3140 | SubobjectDesignator &D = Obj.Designator; |
3141 | if (D.Invalid) |
3142 | return false; |
3143 | |
3144 | // Extract most-derived object and corresponding type. |
3145 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
3146 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
3147 | return false; |
3148 | |
3149 | // Find the virtual base class. |
3150 | if (DerivedDecl->isInvalidDecl()) return false; |
3151 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
3152 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl); |
3153 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
3154 | return true; |
3155 | } |
3156 | |
3157 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
3158 | QualType Type, LValue &Result) { |
3159 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
3160 | PathE = E->path_end(); |
3161 | PathI != PathE; ++PathI) { |
3162 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
3163 | *PathI)) |
3164 | return false; |
3165 | Type = (*PathI)->getType(); |
3166 | } |
3167 | return true; |
3168 | } |
3169 | |
3170 | /// Cast an lvalue referring to a derived class to a known base subobject. |
3171 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3172 | const CXXRecordDecl *DerivedRD, |
3173 | const CXXRecordDecl *BaseRD) { |
3174 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
3175 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
3176 | if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths)) |
3177 | llvm_unreachable("Class must be derived from the passed in base class!" ); |
3178 | |
3179 | for (CXXBasePathElement &Elem : Paths.front()) |
3180 | if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base)) |
3181 | return false; |
3182 | return true; |
3183 | } |
3184 | |
3185 | /// Update LVal to refer to the given field, which must be a member of the type |
3186 | /// currently described by LVal. |
3187 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
3188 | const FieldDecl *FD, |
3189 | const ASTRecordLayout *RL = nullptr) { |
3190 | if (!RL) { |
3191 | if (FD->getParent()->isInvalidDecl()) return false; |
3192 | RL = &Info.Ctx.getASTRecordLayout(D: FD->getParent()); |
3193 | } |
3194 | |
3195 | unsigned I = FD->getFieldIndex(); |
3196 | LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I))); |
3197 | LVal.addDecl(Info, E, FD); |
3198 | return true; |
3199 | } |
3200 | |
3201 | /// Update LVal to refer to the given indirect field. |
3202 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
3203 | LValue &LVal, |
3204 | const IndirectFieldDecl *IFD) { |
3205 | for (const auto *C : IFD->chain()) |
3206 | if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C))) |
3207 | return false; |
3208 | return true; |
3209 | } |
3210 | |
3211 | enum class SizeOfType { |
3212 | SizeOf, |
3213 | DataSizeOf, |
3214 | }; |
3215 | |
3216 | /// Get the size of the given type in char units. |
3217 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type, |
3218 | CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) { |
3219 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
3220 | // extension. |
3221 | if (Type->isVoidType() || Type->isFunctionType()) { |
3222 | Size = CharUnits::One(); |
3223 | return true; |
3224 | } |
3225 | |
3226 | if (Type->isDependentType()) { |
3227 | Info.FFDiag(Loc); |
3228 | return false; |
3229 | } |
3230 | |
3231 | if (!Type->isConstantSizeType()) { |
3232 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
3233 | // FIXME: Better diagnostic. |
3234 | Info.FFDiag(Loc); |
3235 | return false; |
3236 | } |
3237 | |
3238 | if (SOT == SizeOfType::SizeOf) |
3239 | Size = Info.Ctx.getTypeSizeInChars(T: Type); |
3240 | else |
3241 | Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width; |
3242 | return true; |
3243 | } |
3244 | |
3245 | /// Update a pointer value to model pointer arithmetic. |
3246 | /// \param Info - Information about the ongoing evaluation. |
3247 | /// \param E - The expression being evaluated, for diagnostic purposes. |
3248 | /// \param LVal - The pointer value to be updated. |
3249 | /// \param EltTy - The pointee type represented by LVal. |
3250 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
3251 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3252 | LValue &LVal, QualType EltTy, |
3253 | APSInt Adjustment) { |
3254 | CharUnits SizeOfPointee; |
3255 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee)) |
3256 | return false; |
3257 | |
3258 | LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee); |
3259 | return true; |
3260 | } |
3261 | |
3262 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3263 | LValue &LVal, QualType EltTy, |
3264 | int64_t Adjustment) { |
3265 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
3266 | Adjustment: APSInt::get(X: Adjustment)); |
3267 | } |
3268 | |
3269 | /// Update an lvalue to refer to a component of a complex number. |
3270 | /// \param Info - Information about the ongoing evaluation. |
3271 | /// \param LVal - The lvalue to be updated. |
3272 | /// \param EltTy - The complex number's component type. |
3273 | /// \param Imag - False for the real component, true for the imaginary. |
3274 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
3275 | LValue &LVal, QualType EltTy, |
3276 | bool Imag) { |
3277 | if (Imag) { |
3278 | CharUnits SizeOfComponent; |
3279 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent)) |
3280 | return false; |
3281 | LVal.Offset += SizeOfComponent; |
3282 | } |
3283 | LVal.addComplex(Info, E, EltTy, Imag); |
3284 | return true; |
3285 | } |
3286 | |
3287 | /// Try to evaluate the initializer for a variable declaration. |
3288 | /// |
3289 | /// \param Info Information about the ongoing evaluation. |
3290 | /// \param E An expression to be used when printing diagnostics. |
3291 | /// \param VD The variable whose initializer should be obtained. |
3292 | /// \param Version The version of the variable within the frame. |
3293 | /// \param Frame The frame in which the variable was created. Must be null |
3294 | /// if this variable is not local to the evaluation. |
3295 | /// \param Result Filled in with a pointer to the value of the variable. |
3296 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
3297 | const VarDecl *VD, CallStackFrame *Frame, |
3298 | unsigned Version, APValue *&Result) { |
3299 | APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version); |
3300 | |
3301 | // If this is a local variable, dig out its value. |
3302 | if (Frame) { |
3303 | Result = Frame->getTemporary(Key: VD, Version); |
3304 | if (Result) |
3305 | return true; |
3306 | |
3307 | if (!isa<ParmVarDecl>(Val: VD)) { |
3308 | // Assume variables referenced within a lambda's call operator that were |
3309 | // not declared within the call operator are captures and during checking |
3310 | // of a potential constant expression, assume they are unknown constant |
3311 | // expressions. |
3312 | assert(isLambdaCallOperator(Frame->Callee) && |
3313 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
3314 | "missing value for local variable" ); |
3315 | if (Info.checkingPotentialConstantExpression()) |
3316 | return false; |
3317 | // FIXME: This diagnostic is bogus; we do support captures. Is this code |
3318 | // still reachable at all? |
3319 | Info.FFDiag(E->getBeginLoc(), |
3320 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
3321 | << "captures not currently allowed" ; |
3322 | return false; |
3323 | } |
3324 | } |
3325 | |
3326 | // If we're currently evaluating the initializer of this declaration, use that |
3327 | // in-flight value. |
3328 | if (Info.EvaluatingDecl == Base) { |
3329 | Result = Info.EvaluatingDeclValue; |
3330 | return true; |
3331 | } |
3332 | |
3333 | if (isa<ParmVarDecl>(Val: VD)) { |
3334 | // Assume parameters of a potential constant expression are usable in |
3335 | // constant expressions. |
3336 | if (!Info.checkingPotentialConstantExpression() || |
3337 | !Info.CurrentCall->Callee || |
3338 | !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) { |
3339 | if (Info.getLangOpts().CPlusPlus11) { |
3340 | Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown) |
3341 | << VD; |
3342 | NoteLValueLocation(Info, Base); |
3343 | } else { |
3344 | Info.FFDiag(E); |
3345 | } |
3346 | } |
3347 | return false; |
3348 | } |
3349 | |
3350 | if (E->isValueDependent()) |
3351 | return false; |
3352 | |
3353 | // Dig out the initializer, and use the declaration which it's attached to. |
3354 | // FIXME: We should eventually check whether the variable has a reachable |
3355 | // initializing declaration. |
3356 | const Expr *Init = VD->getAnyInitializer(D&: VD); |
3357 | if (!Init) { |
3358 | // Don't diagnose during potential constant expression checking; an |
3359 | // initializer might be added later. |
3360 | if (!Info.checkingPotentialConstantExpression()) { |
3361 | Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) |
3362 | << VD; |
3363 | NoteLValueLocation(Info, Base); |
3364 | } |
3365 | return false; |
3366 | } |
3367 | |
3368 | if (Init->isValueDependent()) { |
3369 | // The DeclRefExpr is not value-dependent, but the variable it refers to |
3370 | // has a value-dependent initializer. This should only happen in |
3371 | // constant-folding cases, where the variable is not actually of a suitable |
3372 | // type for use in a constant expression (otherwise the DeclRefExpr would |
3373 | // have been value-dependent too), so diagnose that. |
3374 | assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx)); |
3375 | if (!Info.checkingPotentialConstantExpression()) { |
3376 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
3377 | ? diag::note_constexpr_ltor_non_constexpr |
3378 | : diag::note_constexpr_ltor_non_integral, 1) |
3379 | << VD << VD->getType(); |
3380 | NoteLValueLocation(Info, Base); |
3381 | } |
3382 | return false; |
3383 | } |
3384 | |
3385 | // Check that we can fold the initializer. In C++, we will have already done |
3386 | // this in the cases where it matters for conformance. |
3387 | if (!VD->evaluateValue()) { |
3388 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3389 | NoteLValueLocation(Info, Base); |
3390 | return false; |
3391 | } |
3392 | |
3393 | // Check that the variable is actually usable in constant expressions. For a |
3394 | // const integral variable or a reference, we might have a non-constant |
3395 | // initializer that we can nonetheless evaluate the initializer for. Such |
3396 | // variables are not usable in constant expressions. In C++98, the |
3397 | // initializer also syntactically needs to be an ICE. |
3398 | // |
3399 | // FIXME: We don't diagnose cases that aren't potentially usable in constant |
3400 | // expressions here; doing so would regress diagnostics for things like |
3401 | // reading from a volatile constexpr variable. |
3402 | if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() && |
3403 | VD->mightBeUsableInConstantExpressions(C: Info.Ctx)) || |
3404 | ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) && |
3405 | !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) { |
3406 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3407 | NoteLValueLocation(Info, Base); |
3408 | } |
3409 | |
3410 | // Never use the initializer of a weak variable, not even for constant |
3411 | // folding. We can't be sure that this is the definition that will be used. |
3412 | if (VD->isWeak()) { |
3413 | Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD; |
3414 | NoteLValueLocation(Info, Base); |
3415 | return false; |
3416 | } |
3417 | |
3418 | Result = VD->getEvaluatedValue(); |
3419 | return true; |
3420 | } |
3421 | |
3422 | /// Get the base index of the given base class within an APValue representing |
3423 | /// the given derived class. |
3424 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
3425 | const CXXRecordDecl *Base) { |
3426 | Base = Base->getCanonicalDecl(); |
3427 | unsigned Index = 0; |
3428 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
3429 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
3430 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
3431 | return Index; |
3432 | } |
3433 | |
3434 | llvm_unreachable("base class missing from derived class's bases list" ); |
3435 | } |
3436 | |
3437 | /// Extract the value of a character from a string literal. |
3438 | static APSInt (EvalInfo &Info, const Expr *Lit, |
3439 | uint64_t Index) { |
3440 | assert(!isa<SourceLocExpr>(Lit) && |
3441 | "SourceLocExpr should have already been converted to a StringLiteral" ); |
3442 | |
3443 | // FIXME: Support MakeStringConstant |
3444 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) { |
3445 | std::string Str; |
3446 | Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str); |
3447 | assert(Index <= Str.size() && "Index too large" ); |
3448 | return APSInt::getUnsigned(X: Str.c_str()[Index]); |
3449 | } |
3450 | |
3451 | if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit)) |
3452 | Lit = PE->getFunctionName(); |
3453 | const StringLiteral *S = cast<StringLiteral>(Val: Lit); |
3454 | const ConstantArrayType *CAT = |
3455 | Info.Ctx.getAsConstantArrayType(T: S->getType()); |
3456 | assert(CAT && "string literal isn't an array" ); |
3457 | QualType CharType = CAT->getElementType(); |
3458 | assert(CharType->isIntegerType() && "unexpected character type" ); |
3459 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3460 | CharType->isUnsignedIntegerType()); |
3461 | if (Index < S->getLength()) |
3462 | Value = S->getCodeUnit(i: Index); |
3463 | return Value; |
3464 | } |
3465 | |
3466 | // Expand a string literal into an array of characters. |
3467 | // |
3468 | // FIXME: This is inefficient; we should probably introduce something similar |
3469 | // to the LLVM ConstantDataArray to make this cheaper. |
3470 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
3471 | APValue &Result, |
3472 | QualType AllocType = QualType()) { |
3473 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
3474 | T: AllocType.isNull() ? S->getType() : AllocType); |
3475 | assert(CAT && "string literal isn't an array" ); |
3476 | QualType CharType = CAT->getElementType(); |
3477 | assert(CharType->isIntegerType() && "unexpected character type" ); |
3478 | |
3479 | unsigned Elts = CAT->getZExtSize(); |
3480 | Result = APValue(APValue::UninitArray(), |
3481 | std::min(a: S->getLength(), b: Elts), Elts); |
3482 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3483 | CharType->isUnsignedIntegerType()); |
3484 | if (Result.hasArrayFiller()) |
3485 | Result.getArrayFiller() = APValue(Value); |
3486 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
3487 | Value = S->getCodeUnit(i: I); |
3488 | Result.getArrayInitializedElt(I) = APValue(Value); |
3489 | } |
3490 | } |
3491 | |
3492 | // Expand an array so that it has more than Index filled elements. |
3493 | static void expandArray(APValue &Array, unsigned Index) { |
3494 | unsigned Size = Array.getArraySize(); |
3495 | assert(Index < Size); |
3496 | |
3497 | // Always at least double the number of elements for which we store a value. |
3498 | unsigned OldElts = Array.getArrayInitializedElts(); |
3499 | unsigned NewElts = std::max(a: Index+1, b: OldElts * 2); |
3500 | NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u)); |
3501 | |
3502 | // Copy the data across. |
3503 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
3504 | for (unsigned I = 0; I != OldElts; ++I) |
3505 | NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I)); |
3506 | for (unsigned I = OldElts; I != NewElts; ++I) |
3507 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
3508 | if (NewValue.hasArrayFiller()) |
3509 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
3510 | Array.swap(RHS&: NewValue); |
3511 | } |
3512 | |
3513 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
3514 | /// conversion. If it's of class type, we may assume that the copy operation |
3515 | /// is trivial. Note that this is never true for a union type with fields |
3516 | /// (because the copy always "reads" the active member) and always true for |
3517 | /// a non-class type. |
3518 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD); |
3519 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
3520 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3521 | return !RD || isReadByLvalueToRvalueConversion(RD); |
3522 | } |
3523 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) { |
3524 | // FIXME: A trivial copy of a union copies the object representation, even if |
3525 | // the union is empty. |
3526 | if (RD->isUnion()) |
3527 | return !RD->field_empty(); |
3528 | if (RD->isEmpty()) |
3529 | return false; |
3530 | |
3531 | for (auto *Field : RD->fields()) |
3532 | if (!Field->isUnnamedBitField() && |
3533 | isReadByLvalueToRvalueConversion(Field->getType())) |
3534 | return true; |
3535 | |
3536 | for (auto &BaseSpec : RD->bases()) |
3537 | if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType())) |
3538 | return true; |
3539 | |
3540 | return false; |
3541 | } |
3542 | |
3543 | /// Diagnose an attempt to read from any unreadable field within the specified |
3544 | /// type, which might be a class type. |
3545 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
3546 | QualType T) { |
3547 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3548 | if (!RD) |
3549 | return false; |
3550 | |
3551 | if (!RD->hasMutableFields()) |
3552 | return false; |
3553 | |
3554 | for (auto *Field : RD->fields()) { |
3555 | // If we're actually going to read this field in some way, then it can't |
3556 | // be mutable. If we're in a union, then assigning to a mutable field |
3557 | // (even an empty one) can change the active member, so that's not OK. |
3558 | // FIXME: Add core issue number for the union case. |
3559 | if (Field->isMutable() && |
3560 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
3561 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
3562 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3563 | return true; |
3564 | } |
3565 | |
3566 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
3567 | return true; |
3568 | } |
3569 | |
3570 | for (auto &BaseSpec : RD->bases()) |
3571 | if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType())) |
3572 | return true; |
3573 | |
3574 | // All mutable fields were empty, and thus not actually read. |
3575 | return false; |
3576 | } |
3577 | |
3578 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
3579 | APValue::LValueBase Base, |
3580 | bool MutableSubobject = false) { |
3581 | // A temporary or transient heap allocation we created. |
3582 | if (Base.getCallIndex() || Base.is<DynamicAllocLValue>()) |
3583 | return true; |
3584 | |
3585 | switch (Info.IsEvaluatingDecl) { |
3586 | case EvalInfo::EvaluatingDeclKind::None: |
3587 | return false; |
3588 | |
3589 | case EvalInfo::EvaluatingDeclKind::Ctor: |
3590 | // The variable whose initializer we're evaluating. |
3591 | if (Info.EvaluatingDecl == Base) |
3592 | return true; |
3593 | |
3594 | // A temporary lifetime-extended by the variable whose initializer we're |
3595 | // evaluating. |
3596 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
3597 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE)) |
3598 | return Info.EvaluatingDecl == BaseMTE->getExtendingDecl(); |
3599 | return false; |
3600 | |
3601 | case EvalInfo::EvaluatingDeclKind::Dtor: |
3602 | // C++2a [expr.const]p6: |
3603 | // [during constant destruction] the lifetime of a and its non-mutable |
3604 | // subobjects (but not its mutable subobjects) [are] considered to start |
3605 | // within e. |
3606 | if (MutableSubobject || Base != Info.EvaluatingDecl) |
3607 | return false; |
3608 | // FIXME: We can meaningfully extend this to cover non-const objects, but |
3609 | // we will need special handling: we should be able to access only |
3610 | // subobjects of such objects that are themselves declared const. |
3611 | QualType T = getType(B: Base); |
3612 | return T.isConstQualified() || T->isReferenceType(); |
3613 | } |
3614 | |
3615 | llvm_unreachable("unknown evaluating decl kind" ); |
3616 | } |
3617 | |
3618 | static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT, |
3619 | SourceLocation CallLoc = {}) { |
3620 | return Info.CheckArraySize( |
3621 | Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc, |
3622 | BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(), |
3623 | /*Diag=*/true); |
3624 | } |
3625 | |
3626 | namespace { |
3627 | /// A handle to a complete object (an object that is not a subobject of |
3628 | /// another object). |
3629 | struct CompleteObject { |
3630 | /// The identity of the object. |
3631 | APValue::LValueBase Base; |
3632 | /// The value of the complete object. |
3633 | APValue *Value; |
3634 | /// The type of the complete object. |
3635 | QualType Type; |
3636 | |
3637 | CompleteObject() : Value(nullptr) {} |
3638 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
3639 | : Base(Base), Value(Value), Type(Type) {} |
3640 | |
3641 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
3642 | // If this isn't a "real" access (eg, if it's just accessing the type |
3643 | // info), allow it. We assume the type doesn't change dynamically for |
3644 | // subobjects of constexpr objects (even though we'd hit UB here if it |
3645 | // did). FIXME: Is this right? |
3646 | if (!isAnyAccess(AK)) |
3647 | return true; |
3648 | |
3649 | // In C++14 onwards, it is permitted to read a mutable member whose |
3650 | // lifetime began within the evaluation. |
3651 | // FIXME: Should we also allow this in C++11? |
3652 | if (!Info.getLangOpts().CPlusPlus14) |
3653 | return false; |
3654 | return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true); |
3655 | } |
3656 | |
3657 | explicit operator bool() const { return !Type.isNull(); } |
3658 | }; |
3659 | } // end anonymous namespace |
3660 | |
3661 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
3662 | bool IsMutable = false) { |
3663 | // C++ [basic.type.qualifier]p1: |
3664 | // - A const object is an object of type const T or a non-mutable subobject |
3665 | // of a const object. |
3666 | if (ObjType.isConstQualified() && !IsMutable) |
3667 | SubobjType.addConst(); |
3668 | // - A volatile object is an object of type const T or a subobject of a |
3669 | // volatile object. |
3670 | if (ObjType.isVolatileQualified()) |
3671 | SubobjType.addVolatile(); |
3672 | return SubobjType; |
3673 | } |
3674 | |
3675 | /// Find the designated sub-object of an rvalue. |
3676 | template<typename SubobjectHandler> |
3677 | typename SubobjectHandler::result_type |
3678 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
3679 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
3680 | if (Sub.Invalid) |
3681 | // A diagnostic will have already been produced. |
3682 | return handler.failed(); |
3683 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
3684 | if (Info.getLangOpts().CPlusPlus11) |
3685 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
3686 | ? diag::note_constexpr_access_past_end |
3687 | : diag::note_constexpr_access_unsized_array) |
3688 | << handler.AccessKind; |
3689 | else |
3690 | Info.FFDiag(E); |
3691 | return handler.failed(); |
3692 | } |
3693 | |
3694 | APValue *O = Obj.Value; |
3695 | QualType ObjType = Obj.Type; |
3696 | const FieldDecl *LastField = nullptr; |
3697 | const FieldDecl *VolatileField = nullptr; |
3698 | |
3699 | // Walk the designator's path to find the subobject. |
3700 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
3701 | // Reading an indeterminate value is undefined, but assigning over one is OK. |
3702 | if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) || |
3703 | (O->isIndeterminate() && |
3704 | !isValidIndeterminateAccess(handler.AccessKind))) { |
3705 | if (!Info.checkingPotentialConstantExpression()) |
3706 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
3707 | << handler.AccessKind << O->isIndeterminate() |
3708 | << E->getSourceRange(); |
3709 | return handler.failed(); |
3710 | } |
3711 | |
3712 | // C++ [class.ctor]p5, C++ [class.dtor]p5: |
3713 | // const and volatile semantics are not applied on an object under |
3714 | // {con,de}struction. |
3715 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
3716 | ObjType->isRecordType() && |
3717 | Info.isEvaluatingCtorDtor( |
3718 | Base: Obj.Base, |
3719 | Path: llvm::ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) != |
3720 | ConstructionPhase::None) { |
3721 | ObjType = Info.Ctx.getCanonicalType(T: ObjType); |
3722 | ObjType.removeLocalConst(); |
3723 | ObjType.removeLocalVolatile(); |
3724 | } |
3725 | |
3726 | // If this is our last pass, check that the final object type is OK. |
3727 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
3728 | // Accesses to volatile objects are prohibited. |
3729 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
3730 | if (Info.getLangOpts().CPlusPlus) { |
3731 | int DiagKind; |
3732 | SourceLocation Loc; |
3733 | const NamedDecl *Decl = nullptr; |
3734 | if (VolatileField) { |
3735 | DiagKind = 2; |
3736 | Loc = VolatileField->getLocation(); |
3737 | Decl = VolatileField; |
3738 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
3739 | DiagKind = 1; |
3740 | Loc = VD->getLocation(); |
3741 | Decl = VD; |
3742 | } else { |
3743 | DiagKind = 0; |
3744 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
3745 | Loc = E->getExprLoc(); |
3746 | } |
3747 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3748 | << handler.AccessKind << DiagKind << Decl; |
3749 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
3750 | } else { |
3751 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
3752 | } |
3753 | return handler.failed(); |
3754 | } |
3755 | |
3756 | // If we are reading an object of class type, there may still be more |
3757 | // things we need to check: if there are any mutable subobjects, we |
3758 | // cannot perform this read. (This only happens when performing a trivial |
3759 | // copy or assignment.) |
3760 | if (ObjType->isRecordType() && |
3761 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) && |
3762 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
3763 | return handler.failed(); |
3764 | } |
3765 | |
3766 | if (I == N) { |
3767 | if (!handler.found(*O, ObjType)) |
3768 | return false; |
3769 | |
3770 | // If we modified a bit-field, truncate it to the right width. |
3771 | if (isModification(handler.AccessKind) && |
3772 | LastField && LastField->isBitField() && |
3773 | !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField)) |
3774 | return false; |
3775 | |
3776 | return true; |
3777 | } |
3778 | |
3779 | LastField = nullptr; |
3780 | if (ObjType->isArrayType()) { |
3781 | // Next subobject is an array element. |
3782 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: ObjType); |
3783 | assert(CAT && "vla in literal type?" ); |
3784 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3785 | if (CAT->getSize().ule(RHS: Index)) { |
3786 | // Note, it should not be possible to form a pointer with a valid |
3787 | // designator which points more than one past the end of the array. |
3788 | if (Info.getLangOpts().CPlusPlus11) |
3789 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3790 | << handler.AccessKind; |
3791 | else |
3792 | Info.FFDiag(E); |
3793 | return handler.failed(); |
3794 | } |
3795 | |
3796 | ObjType = CAT->getElementType(); |
3797 | |
3798 | if (O->getArrayInitializedElts() > Index) |
3799 | O = &O->getArrayInitializedElt(I: Index); |
3800 | else if (!isRead(handler.AccessKind)) { |
3801 | if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc())) |
3802 | return handler.failed(); |
3803 | |
3804 | expandArray(Array&: *O, Index); |
3805 | O = &O->getArrayInitializedElt(I: Index); |
3806 | } else |
3807 | O = &O->getArrayFiller(); |
3808 | } else if (ObjType->isAnyComplexType()) { |
3809 | // Next subobject is a complex number. |
3810 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3811 | if (Index > 1) { |
3812 | if (Info.getLangOpts().CPlusPlus11) |
3813 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3814 | << handler.AccessKind; |
3815 | else |
3816 | Info.FFDiag(E); |
3817 | return handler.failed(); |
3818 | } |
3819 | |
3820 | ObjType = getSubobjectType( |
3821 | ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType()); |
3822 | |
3823 | assert(I == N - 1 && "extracting subobject of scalar?" ); |
3824 | if (O->isComplexInt()) { |
3825 | return handler.found(Index ? O->getComplexIntImag() |
3826 | : O->getComplexIntReal(), ObjType); |
3827 | } else { |
3828 | assert(O->isComplexFloat()); |
3829 | return handler.found(Index ? O->getComplexFloatImag() |
3830 | : O->getComplexFloatReal(), ObjType); |
3831 | } |
3832 | } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) { |
3833 | if (Field->isMutable() && |
3834 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) { |
3835 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
3836 | << handler.AccessKind << Field; |
3837 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3838 | return handler.failed(); |
3839 | } |
3840 | |
3841 | // Next subobject is a class, struct or union field. |
3842 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
3843 | if (RD->isUnion()) { |
3844 | const FieldDecl *UnionField = O->getUnionField(); |
3845 | if (!UnionField || |
3846 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
3847 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
3848 | // Placement new onto an inactive union member makes it active. |
3849 | O->setUnion(Field, Value: APValue()); |
3850 | } else { |
3851 | // FIXME: If O->getUnionValue() is absent, report that there's no |
3852 | // active union member rather than reporting the prior active union |
3853 | // member. We'll need to fix nullptr_t to not use APValue() as its |
3854 | // representation first. |
3855 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
3856 | << handler.AccessKind << Field << !UnionField << UnionField; |
3857 | return handler.failed(); |
3858 | } |
3859 | } |
3860 | O = &O->getUnionValue(); |
3861 | } else |
3862 | O = &O->getStructField(i: Field->getFieldIndex()); |
3863 | |
3864 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
3865 | LastField = Field; |
3866 | if (Field->getType().isVolatileQualified()) |
3867 | VolatileField = Field; |
3868 | } else { |
3869 | // Next subobject is a base class. |
3870 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
3871 | const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]); |
3872 | O = &O->getStructBase(i: getBaseIndex(Derived, Base)); |
3873 | |
3874 | ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getRecordType(Base)); |
3875 | } |
3876 | } |
3877 | } |
3878 | |
3879 | namespace { |
3880 | struct ExtractSubobjectHandler { |
3881 | EvalInfo &Info; |
3882 | const Expr *E; |
3883 | APValue &Result; |
3884 | const AccessKinds AccessKind; |
3885 | |
3886 | typedef bool result_type; |
3887 | bool failed() { return false; } |
3888 | bool found(APValue &Subobj, QualType SubobjType) { |
3889 | Result = Subobj; |
3890 | if (AccessKind == AK_ReadObjectRepresentation) |
3891 | return true; |
3892 | return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result); |
3893 | } |
3894 | bool found(APSInt &Value, QualType SubobjType) { |
3895 | Result = APValue(Value); |
3896 | return true; |
3897 | } |
3898 | bool found(APFloat &Value, QualType SubobjType) { |
3899 | Result = APValue(Value); |
3900 | return true; |
3901 | } |
3902 | }; |
3903 | } // end anonymous namespace |
3904 | |
3905 | /// Extract the designated sub-object of an rvalue. |
3906 | static bool (EvalInfo &Info, const Expr *E, |
3907 | const CompleteObject &Obj, |
3908 | const SubobjectDesignator &Sub, APValue &Result, |
3909 | AccessKinds AK = AK_Read) { |
3910 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
3911 | ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK}; |
3912 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
3913 | } |
3914 | |
3915 | namespace { |
3916 | struct ModifySubobjectHandler { |
3917 | EvalInfo &Info; |
3918 | APValue &NewVal; |
3919 | const Expr *E; |
3920 | |
3921 | typedef bool result_type; |
3922 | static const AccessKinds AccessKind = AK_Assign; |
3923 | |
3924 | bool checkConst(QualType QT) { |
3925 | // Assigning to a const object has undefined behavior. |
3926 | if (QT.isConstQualified()) { |
3927 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3928 | return false; |
3929 | } |
3930 | return true; |
3931 | } |
3932 | |
3933 | bool failed() { return false; } |
3934 | bool found(APValue &Subobj, QualType SubobjType) { |
3935 | if (!checkConst(QT: SubobjType)) |
3936 | return false; |
3937 | // We've been given ownership of NewVal, so just swap it in. |
3938 | Subobj.swap(RHS&: NewVal); |
3939 | return true; |
3940 | } |
3941 | bool found(APSInt &Value, QualType SubobjType) { |
3942 | if (!checkConst(QT: SubobjType)) |
3943 | return false; |
3944 | if (!NewVal.isInt()) { |
3945 | // Maybe trying to write a cast pointer value into a complex? |
3946 | Info.FFDiag(E); |
3947 | return false; |
3948 | } |
3949 | Value = NewVal.getInt(); |
3950 | return true; |
3951 | } |
3952 | bool found(APFloat &Value, QualType SubobjType) { |
3953 | if (!checkConst(QT: SubobjType)) |
3954 | return false; |
3955 | Value = NewVal.getFloat(); |
3956 | return true; |
3957 | } |
3958 | }; |
3959 | } // end anonymous namespace |
3960 | |
3961 | const AccessKinds ModifySubobjectHandler::AccessKind; |
3962 | |
3963 | /// Update the designated sub-object of an rvalue to the given value. |
3964 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
3965 | const CompleteObject &Obj, |
3966 | const SubobjectDesignator &Sub, |
3967 | APValue &NewVal) { |
3968 | ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E }; |
3969 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
3970 | } |
3971 | |
3972 | /// Find the position where two subobject designators diverge, or equivalently |
3973 | /// the length of the common initial subsequence. |
3974 | static unsigned FindDesignatorMismatch(QualType ObjType, |
3975 | const SubobjectDesignator &A, |
3976 | const SubobjectDesignator &B, |
3977 | bool &WasArrayIndex) { |
3978 | unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size()); |
3979 | for (/**/; I != N; ++I) { |
3980 | if (!ObjType.isNull() && |
3981 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
3982 | // Next subobject is an array element. |
3983 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
3984 | WasArrayIndex = true; |
3985 | return I; |
3986 | } |
3987 | if (ObjType->isAnyComplexType()) |
3988 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
3989 | else |
3990 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
3991 | } else { |
3992 | if (A.Entries[I].getAsBaseOrMember() != |
3993 | B.Entries[I].getAsBaseOrMember()) { |
3994 | WasArrayIndex = false; |
3995 | return I; |
3996 | } |
3997 | if (const FieldDecl *FD = getAsField(E: A.Entries[I])) |
3998 | // Next subobject is a field. |
3999 | ObjType = FD->getType(); |
4000 | else |
4001 | // Next subobject is a base class. |
4002 | ObjType = QualType(); |
4003 | } |
4004 | } |
4005 | WasArrayIndex = false; |
4006 | return I; |
4007 | } |
4008 | |
4009 | /// Determine whether the given subobject designators refer to elements of the |
4010 | /// same array object. |
4011 | static bool AreElementsOfSameArray(QualType ObjType, |
4012 | const SubobjectDesignator &A, |
4013 | const SubobjectDesignator &B) { |
4014 | if (A.Entries.size() != B.Entries.size()) |
4015 | return false; |
4016 | |
4017 | bool IsArray = A.MostDerivedIsArrayElement; |
4018 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
4019 | // A is a subobject of the array element. |
4020 | return false; |
4021 | |
4022 | // If A (and B) designates an array element, the last entry will be the array |
4023 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
4024 | // of length 1' case, and the entire path must match. |
4025 | bool WasArrayIndex; |
4026 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
4027 | return CommonLength >= A.Entries.size() - IsArray; |
4028 | } |
4029 | |
4030 | /// Find the complete object to which an LValue refers. |
4031 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
4032 | AccessKinds AK, const LValue &LVal, |
4033 | QualType LValType) { |
4034 | if (LVal.InvalidBase) { |
4035 | Info.FFDiag(E); |
4036 | return CompleteObject(); |
4037 | } |
4038 | |
4039 | if (!LVal.Base) { |
4040 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
4041 | return CompleteObject(); |
4042 | } |
4043 | |
4044 | CallStackFrame *Frame = nullptr; |
4045 | unsigned Depth = 0; |
4046 | if (LVal.getLValueCallIndex()) { |
4047 | std::tie(args&: Frame, args&: Depth) = |
4048 | Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex()); |
4049 | if (!Frame) { |
4050 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
4051 | << AK << LVal.Base.is<const ValueDecl*>(); |
4052 | NoteLValueLocation(Info, Base: LVal.Base); |
4053 | return CompleteObject(); |
4054 | } |
4055 | } |
4056 | |
4057 | bool IsAccess = isAnyAccess(AK); |
4058 | |
4059 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
4060 | // is not a constant expression (even if the object is non-volatile). We also |
4061 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
4062 | // semantics. |
4063 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
4064 | if (Info.getLangOpts().CPlusPlus) |
4065 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
4066 | << AK << LValType; |
4067 | else |
4068 | Info.FFDiag(E); |
4069 | return CompleteObject(); |
4070 | } |
4071 | |
4072 | // Compute value storage location and type of base object. |
4073 | APValue *BaseVal = nullptr; |
4074 | QualType BaseType = getType(B: LVal.Base); |
4075 | |
4076 | if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl && |
4077 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4078 | // This is the object whose initializer we're evaluating, so its lifetime |
4079 | // started in the current evaluation. |
4080 | BaseVal = Info.EvaluatingDeclValue; |
4081 | } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) { |
4082 | // Allow reading from a GUID declaration. |
4083 | if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) { |
4084 | if (isModification(AK)) { |
4085 | // All the remaining cases do not permit modification of the object. |
4086 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4087 | return CompleteObject(); |
4088 | } |
4089 | APValue &V = GD->getAsAPValue(); |
4090 | if (V.isAbsent()) { |
4091 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
4092 | << GD->getType(); |
4093 | return CompleteObject(); |
4094 | } |
4095 | return CompleteObject(LVal.Base, &V, GD->getType()); |
4096 | } |
4097 | |
4098 | // Allow reading the APValue from an UnnamedGlobalConstantDecl. |
4099 | if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) { |
4100 | if (isModification(AK)) { |
4101 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4102 | return CompleteObject(); |
4103 | } |
4104 | return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()), |
4105 | GCD->getType()); |
4106 | } |
4107 | |
4108 | // Allow reading from template parameter objects. |
4109 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) { |
4110 | if (isModification(AK)) { |
4111 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4112 | return CompleteObject(); |
4113 | } |
4114 | return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()), |
4115 | TPO->getType()); |
4116 | } |
4117 | |
4118 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
4119 | // In C++11, constexpr, non-volatile variables initialized with constant |
4120 | // expressions are constant expressions too. Inside constexpr functions, |
4121 | // parameters are constant expressions even if they're non-const. |
4122 | // In C++1y, objects local to a constant expression (those with a Frame) are |
4123 | // both readable and writable inside constant expressions. |
4124 | // In C, such things can also be folded, although they are not ICEs. |
4125 | const VarDecl *VD = dyn_cast<VarDecl>(Val: D); |
4126 | if (VD) { |
4127 | if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx)) |
4128 | VD = VDef; |
4129 | } |
4130 | if (!VD || VD->isInvalidDecl()) { |
4131 | Info.FFDiag(E); |
4132 | return CompleteObject(); |
4133 | } |
4134 | |
4135 | bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx); |
4136 | bool ConstexprVar = false; |
4137 | if (const auto *VD = dyn_cast_if_present<VarDecl>( |
4138 | Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>())) |
4139 | ConstexprVar = VD->isConstexpr(); |
4140 | |
4141 | // Unless we're looking at a local variable or argument in a constexpr call, |
4142 | // the variable we're reading must be const. |
4143 | if (!Frame) { |
4144 | if (IsAccess && isa<ParmVarDecl>(Val: VD)) { |
4145 | // Access of a parameter that's not associated with a frame isn't going |
4146 | // to work out, but we can leave it to evaluateVarDeclInit to provide a |
4147 | // suitable diagnostic. |
4148 | } else if (Info.getLangOpts().CPlusPlus14 && |
4149 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4150 | // OK, we can read and modify an object if we're in the process of |
4151 | // evaluating its initializer, because its lifetime began in this |
4152 | // evaluation. |
4153 | } else if (isModification(AK)) { |
4154 | // All the remaining cases do not permit modification of the object. |
4155 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4156 | return CompleteObject(); |
4157 | } else if (VD->isConstexpr()) { |
4158 | // OK, we can read this variable. |
4159 | } else if (Info.getLangOpts().C23 && ConstexprVar) { |
4160 | Info.FFDiag(E); |
4161 | return CompleteObject(); |
4162 | } else if (BaseType->isIntegralOrEnumerationType()) { |
4163 | if (!IsConstant) { |
4164 | if (!IsAccess) |
4165 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4166 | if (Info.getLangOpts().CPlusPlus) { |
4167 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
4168 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4169 | } else { |
4170 | Info.FFDiag(E); |
4171 | } |
4172 | return CompleteObject(); |
4173 | } |
4174 | } else if (!IsAccess) { |
4175 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4176 | } else if (IsConstant && Info.checkingPotentialConstantExpression() && |
4177 | BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) { |
4178 | // This variable might end up being constexpr. Don't diagnose it yet. |
4179 | } else if (IsConstant) { |
4180 | // Keep evaluating to see what we can do. In particular, we support |
4181 | // folding of const floating-point types, in order to make static const |
4182 | // data members of such types (supported as an extension) more useful. |
4183 | if (Info.getLangOpts().CPlusPlus) { |
4184 | Info.CCEDiag(E, Info.getLangOpts().CPlusPlus11 |
4185 | ? diag::note_constexpr_ltor_non_constexpr |
4186 | : diag::note_constexpr_ltor_non_integral, 1) |
4187 | << VD << BaseType; |
4188 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4189 | } else { |
4190 | Info.CCEDiag(E); |
4191 | } |
4192 | } else { |
4193 | // Never allow reading a non-const value. |
4194 | if (Info.getLangOpts().CPlusPlus) { |
4195 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
4196 | ? diag::note_constexpr_ltor_non_constexpr |
4197 | : diag::note_constexpr_ltor_non_integral, 1) |
4198 | << VD << BaseType; |
4199 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4200 | } else { |
4201 | Info.FFDiag(E); |
4202 | } |
4203 | return CompleteObject(); |
4204 | } |
4205 | } |
4206 | |
4207 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(), Result&: BaseVal)) |
4208 | return CompleteObject(); |
4209 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
4210 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
4211 | if (!Alloc) { |
4212 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
4213 | return CompleteObject(); |
4214 | } |
4215 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
4216 | LVal.Base.getDynamicAllocType()); |
4217 | } else { |
4218 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4219 | |
4220 | if (!Frame) { |
4221 | if (const MaterializeTemporaryExpr *MTE = |
4222 | dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) { |
4223 | assert(MTE->getStorageDuration() == SD_Static && |
4224 | "should have a frame for a non-global materialized temporary" ); |
4225 | |
4226 | // C++20 [expr.const]p4: [DR2126] |
4227 | // An object or reference is usable in constant expressions if it is |
4228 | // - a temporary object of non-volatile const-qualified literal type |
4229 | // whose lifetime is extended to that of a variable that is usable |
4230 | // in constant expressions |
4231 | // |
4232 | // C++20 [expr.const]p5: |
4233 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
4234 | // - a non-volatile glvalue that refers to an object that is usable |
4235 | // in constant expressions, or |
4236 | // - a non-volatile glvalue of literal type that refers to a |
4237 | // non-volatile object whose lifetime began within the evaluation |
4238 | // of E; |
4239 | // |
4240 | // C++11 misses the 'began within the evaluation of e' check and |
4241 | // instead allows all temporaries, including things like: |
4242 | // int &&r = 1; |
4243 | // int x = ++r; |
4244 | // constexpr int k = r; |
4245 | // Therefore we use the C++14-onwards rules in C++11 too. |
4246 | // |
4247 | // Note that temporaries whose lifetimes began while evaluating a |
4248 | // variable's constructor are not usable while evaluating the |
4249 | // corresponding destructor, not even if they're of const-qualified |
4250 | // types. |
4251 | if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) && |
4252 | !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4253 | if (!IsAccess) |
4254 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4255 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
4256 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
4257 | return CompleteObject(); |
4258 | } |
4259 | |
4260 | BaseVal = MTE->getOrCreateValue(MayCreate: false); |
4261 | assert(BaseVal && "got reference to unevaluated temporary" ); |
4262 | } else { |
4263 | if (!IsAccess) |
4264 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4265 | APValue Val; |
4266 | LVal.moveInto(V&: Val); |
4267 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
4268 | << AK |
4269 | << Val.getAsString(Info.Ctx, |
4270 | Info.Ctx.getLValueReferenceType(LValType)); |
4271 | NoteLValueLocation(Info, Base: LVal.Base); |
4272 | return CompleteObject(); |
4273 | } |
4274 | } else { |
4275 | BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion()); |
4276 | assert(BaseVal && "missing value for temporary" ); |
4277 | } |
4278 | } |
4279 | |
4280 | // In C++14, we can't safely access any mutable state when we might be |
4281 | // evaluating after an unmodeled side effect. Parameters are modeled as state |
4282 | // in the caller, but aren't visible once the call returns, so they can be |
4283 | // modified in a speculatively-evaluated call. |
4284 | // |
4285 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
4286 | // to be read here (but take care with 'mutable' fields). |
4287 | unsigned VisibleDepth = Depth; |
4288 | if (llvm::isa_and_nonnull<ParmVarDecl>( |
4289 | Val: LVal.Base.dyn_cast<const ValueDecl *>())) |
4290 | ++VisibleDepth; |
4291 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
4292 | Info.EvalStatus.HasSideEffects) || |
4293 | (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth)) |
4294 | return CompleteObject(); |
4295 | |
4296 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
4297 | } |
4298 | |
4299 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
4300 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
4301 | /// glvalue referred to by an entity of reference type. |
4302 | /// |
4303 | /// \param Info - Information about the ongoing evaluation. |
4304 | /// \param Conv - The expression for which we are performing the conversion. |
4305 | /// Used for diagnostics. |
4306 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
4307 | /// case of a non-class type). |
4308 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
4309 | /// \param RVal - The produced value will be placed here. |
4310 | /// \param WantObjectRepresentation - If true, we're looking for the object |
4311 | /// representation rather than the value, and in particular, |
4312 | /// there is no requirement that the result be fully initialized. |
4313 | static bool |
4314 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
4315 | const LValue &LVal, APValue &RVal, |
4316 | bool WantObjectRepresentation = false) { |
4317 | if (LVal.Designator.Invalid) |
4318 | return false; |
4319 | |
4320 | // Check for special cases where there is no existing APValue to look at. |
4321 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4322 | |
4323 | AccessKinds AK = |
4324 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
4325 | |
4326 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
4327 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Val: Base)) { |
4328 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
4329 | // initializer until now for such expressions. Such an expression can't be |
4330 | // an ICE in C, so this only matters for fold. |
4331 | if (Type.isVolatileQualified()) { |
4332 | Info.FFDiag(E: Conv); |
4333 | return false; |
4334 | } |
4335 | |
4336 | APValue Lit; |
4337 | if (!Evaluate(Result&: Lit, Info, E: CLE->getInitializer())) |
4338 | return false; |
4339 | |
4340 | // According to GCC info page: |
4341 | // |
4342 | // 6.28 Compound Literals |
4343 | // |
4344 | // As an optimization, G++ sometimes gives array compound literals longer |
4345 | // lifetimes: when the array either appears outside a function or has a |
4346 | // const-qualified type. If foo and its initializer had elements of type |
4347 | // char *const rather than char *, or if foo were a global variable, the |
4348 | // array would have static storage duration. But it is probably safest |
4349 | // just to avoid the use of array compound literals in C++ code. |
4350 | // |
4351 | // Obey that rule by checking constness for converted array types. |
4352 | |
4353 | QualType CLETy = CLE->getType(); |
4354 | if (CLETy->isArrayType() && !Type->isArrayType()) { |
4355 | if (!CLETy.isConstant(Ctx: Info.Ctx)) { |
4356 | Info.FFDiag(E: Conv); |
4357 | Info.Note(CLE->getExprLoc(), diag::note_declared_at); |
4358 | return false; |
4359 | } |
4360 | } |
4361 | |
4362 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
4363 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
4364 | } else if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) { |
4365 | // Special-case character extraction so we don't have to construct an |
4366 | // APValue for the whole string. |
4367 | assert(LVal.Designator.Entries.size() <= 1 && |
4368 | "Can only read characters from string literals" ); |
4369 | if (LVal.Designator.Entries.empty()) { |
4370 | // Fail for now for LValue to RValue conversion of an array. |
4371 | // (This shouldn't show up in C/C++, but it could be triggered by a |
4372 | // weird EvaluateAsRValue call from a tool.) |
4373 | Info.FFDiag(E: Conv); |
4374 | return false; |
4375 | } |
4376 | if (LVal.Designator.isOnePastTheEnd()) { |
4377 | if (Info.getLangOpts().CPlusPlus11) |
4378 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
4379 | else |
4380 | Info.FFDiag(E: Conv); |
4381 | return false; |
4382 | } |
4383 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
4384 | RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex)); |
4385 | return true; |
4386 | } |
4387 | } |
4388 | |
4389 | CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type); |
4390 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
4391 | } |
4392 | |
4393 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
4394 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4395 | QualType LValType, APValue &Val) { |
4396 | if (LVal.Designator.Invalid) |
4397 | return false; |
4398 | |
4399 | if (!Info.getLangOpts().CPlusPlus14) { |
4400 | Info.FFDiag(E); |
4401 | return false; |
4402 | } |
4403 | |
4404 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType); |
4405 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
4406 | } |
4407 | |
4408 | namespace { |
4409 | struct CompoundAssignSubobjectHandler { |
4410 | EvalInfo &Info; |
4411 | const CompoundAssignOperator *E; |
4412 | QualType PromotedLHSType; |
4413 | BinaryOperatorKind Opcode; |
4414 | const APValue &RHS; |
4415 | |
4416 | static const AccessKinds AccessKind = AK_Assign; |
4417 | |
4418 | typedef bool result_type; |
4419 | |
4420 | bool checkConst(QualType QT) { |
4421 | // Assigning to a const object has undefined behavior. |
4422 | if (QT.isConstQualified()) { |
4423 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4424 | return false; |
4425 | } |
4426 | return true; |
4427 | } |
4428 | |
4429 | bool failed() { return false; } |
4430 | bool found(APValue &Subobj, QualType SubobjType) { |
4431 | switch (Subobj.getKind()) { |
4432 | case APValue::Int: |
4433 | return found(Value&: Subobj.getInt(), SubobjType); |
4434 | case APValue::Float: |
4435 | return found(Value&: Subobj.getFloat(), SubobjType); |
4436 | case APValue::ComplexInt: |
4437 | case APValue::ComplexFloat: |
4438 | // FIXME: Implement complex compound assignment. |
4439 | Info.FFDiag(E); |
4440 | return false; |
4441 | case APValue::LValue: |
4442 | return foundPointer(Subobj, SubobjType); |
4443 | case APValue::Vector: |
4444 | return foundVector(Value&: Subobj, SubobjType); |
4445 | case APValue::Indeterminate: |
4446 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
4447 | << /*read of=*/0 << /*uninitialized object=*/1 |
4448 | << E->getLHS()->getSourceRange(); |
4449 | return false; |
4450 | default: |
4451 | // FIXME: can this happen? |
4452 | Info.FFDiag(E); |
4453 | return false; |
4454 | } |
4455 | } |
4456 | |
4457 | bool foundVector(APValue &Value, QualType SubobjType) { |
4458 | if (!checkConst(QT: SubobjType)) |
4459 | return false; |
4460 | |
4461 | if (!SubobjType->isVectorType()) { |
4462 | Info.FFDiag(E); |
4463 | return false; |
4464 | } |
4465 | return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS); |
4466 | } |
4467 | |
4468 | bool found(APSInt &Value, QualType SubobjType) { |
4469 | if (!checkConst(QT: SubobjType)) |
4470 | return false; |
4471 | |
4472 | if (!SubobjType->isIntegerType()) { |
4473 | // We don't support compound assignment on integer-cast-to-pointer |
4474 | // values. |
4475 | Info.FFDiag(E); |
4476 | return false; |
4477 | } |
4478 | |
4479 | if (RHS.isInt()) { |
4480 | APSInt LHS = |
4481 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
4482 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
4483 | return false; |
4484 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
4485 | return true; |
4486 | } else if (RHS.isFloat()) { |
4487 | const FPOptions FPO = E->getFPFeaturesInEffect( |
4488 | Info.Ctx.getLangOpts()); |
4489 | APFloat FValue(0.0); |
4490 | return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value, |
4491 | PromotedLHSType, FValue) && |
4492 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
4493 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
4494 | Value); |
4495 | } |
4496 | |
4497 | Info.FFDiag(E); |
4498 | return false; |
4499 | } |
4500 | bool found(APFloat &Value, QualType SubobjType) { |
4501 | return checkConst(SubobjType) && |
4502 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
4503 | Value) && |
4504 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
4505 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
4506 | } |
4507 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4508 | if (!checkConst(QT: SubobjType)) |
4509 | return false; |
4510 | |
4511 | QualType PointeeType; |
4512 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4513 | PointeeType = PT->getPointeeType(); |
4514 | |
4515 | if (PointeeType.isNull() || !RHS.isInt() || |
4516 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
4517 | Info.FFDiag(E); |
4518 | return false; |
4519 | } |
4520 | |
4521 | APSInt Offset = RHS.getInt(); |
4522 | if (Opcode == BO_Sub) |
4523 | negateAsSigned(Int&: Offset); |
4524 | |
4525 | LValue LVal; |
4526 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4527 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
4528 | return false; |
4529 | LVal.moveInto(V&: Subobj); |
4530 | return true; |
4531 | } |
4532 | }; |
4533 | } // end anonymous namespace |
4534 | |
4535 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
4536 | |
4537 | /// Perform a compound assignment of LVal <op>= RVal. |
4538 | static bool handleCompoundAssignment(EvalInfo &Info, |
4539 | const CompoundAssignOperator *E, |
4540 | const LValue &LVal, QualType LValType, |
4541 | QualType PromotedLValType, |
4542 | BinaryOperatorKind Opcode, |
4543 | const APValue &RVal) { |
4544 | if (LVal.Designator.Invalid) |
4545 | return false; |
4546 | |
4547 | if (!Info.getLangOpts().CPlusPlus14) { |
4548 | Info.FFDiag(E); |
4549 | return false; |
4550 | } |
4551 | |
4552 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4553 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
4554 | RVal }; |
4555 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4556 | } |
4557 | |
4558 | namespace { |
4559 | struct IncDecSubobjectHandler { |
4560 | EvalInfo &Info; |
4561 | const UnaryOperator *E; |
4562 | AccessKinds AccessKind; |
4563 | APValue *Old; |
4564 | |
4565 | typedef bool result_type; |
4566 | |
4567 | bool checkConst(QualType QT) { |
4568 | // Assigning to a const object has undefined behavior. |
4569 | if (QT.isConstQualified()) { |
4570 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4571 | return false; |
4572 | } |
4573 | return true; |
4574 | } |
4575 | |
4576 | bool failed() { return false; } |
4577 | bool found(APValue &Subobj, QualType SubobjType) { |
4578 | // Stash the old value. Also clear Old, so we don't clobber it later |
4579 | // if we're post-incrementing a complex. |
4580 | if (Old) { |
4581 | *Old = Subobj; |
4582 | Old = nullptr; |
4583 | } |
4584 | |
4585 | switch (Subobj.getKind()) { |
4586 | case APValue::Int: |
4587 | return found(Value&: Subobj.getInt(), SubobjType); |
4588 | case APValue::Float: |
4589 | return found(Value&: Subobj.getFloat(), SubobjType); |
4590 | case APValue::ComplexInt: |
4591 | return found(Value&: Subobj.getComplexIntReal(), |
4592 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4593 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4594 | case APValue::ComplexFloat: |
4595 | return found(Value&: Subobj.getComplexFloatReal(), |
4596 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4597 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4598 | case APValue::LValue: |
4599 | return foundPointer(Subobj, SubobjType); |
4600 | default: |
4601 | // FIXME: can this happen? |
4602 | Info.FFDiag(E); |
4603 | return false; |
4604 | } |
4605 | } |
4606 | bool found(APSInt &Value, QualType SubobjType) { |
4607 | if (!checkConst(QT: SubobjType)) |
4608 | return false; |
4609 | |
4610 | if (!SubobjType->isIntegerType()) { |
4611 | // We don't support increment / decrement on integer-cast-to-pointer |
4612 | // values. |
4613 | Info.FFDiag(E); |
4614 | return false; |
4615 | } |
4616 | |
4617 | if (Old) *Old = APValue(Value); |
4618 | |
4619 | // bool arithmetic promotes to int, and the conversion back to bool |
4620 | // doesn't reduce mod 2^n, so special-case it. |
4621 | if (SubobjType->isBooleanType()) { |
4622 | if (AccessKind == AK_Increment) |
4623 | Value = 1; |
4624 | else |
4625 | Value = !Value; |
4626 | return true; |
4627 | } |
4628 | |
4629 | bool WasNegative = Value.isNegative(); |
4630 | if (AccessKind == AK_Increment) { |
4631 | ++Value; |
4632 | |
4633 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
4634 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
4635 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4636 | } |
4637 | } else { |
4638 | --Value; |
4639 | |
4640 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
4641 | unsigned BitWidth = Value.getBitWidth(); |
4642 | APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false); |
4643 | ActualValue.setBit(BitWidth); |
4644 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4645 | } |
4646 | } |
4647 | return true; |
4648 | } |
4649 | bool found(APFloat &Value, QualType SubobjType) { |
4650 | if (!checkConst(QT: SubobjType)) |
4651 | return false; |
4652 | |
4653 | if (Old) *Old = APValue(Value); |
4654 | |
4655 | APFloat One(Value.getSemantics(), 1); |
4656 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
4657 | APFloat::opStatus St; |
4658 | if (AccessKind == AK_Increment) |
4659 | St = Value.add(RHS: One, RM); |
4660 | else |
4661 | St = Value.subtract(RHS: One, RM); |
4662 | return checkFloatingPointResult(Info, E, St); |
4663 | } |
4664 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4665 | if (!checkConst(QT: SubobjType)) |
4666 | return false; |
4667 | |
4668 | QualType PointeeType; |
4669 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4670 | PointeeType = PT->getPointeeType(); |
4671 | else { |
4672 | Info.FFDiag(E); |
4673 | return false; |
4674 | } |
4675 | |
4676 | LValue LVal; |
4677 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4678 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
4679 | AccessKind == AK_Increment ? 1 : -1)) |
4680 | return false; |
4681 | LVal.moveInto(V&: Subobj); |
4682 | return true; |
4683 | } |
4684 | }; |
4685 | } // end anonymous namespace |
4686 | |
4687 | /// Perform an increment or decrement on LVal. |
4688 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4689 | QualType LValType, bool IsIncrement, APValue *Old) { |
4690 | if (LVal.Designator.Invalid) |
4691 | return false; |
4692 | |
4693 | if (!Info.getLangOpts().CPlusPlus14) { |
4694 | Info.FFDiag(E); |
4695 | return false; |
4696 | } |
4697 | |
4698 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
4699 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
4700 | IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old}; |
4701 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4702 | } |
4703 | |
4704 | /// Build an lvalue for the object argument of a member function call. |
4705 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
4706 | LValue &This) { |
4707 | if (Object->getType()->isPointerType() && Object->isPRValue()) |
4708 | return EvaluatePointer(E: Object, Result&: This, Info); |
4709 | |
4710 | if (Object->isGLValue()) |
4711 | return EvaluateLValue(E: Object, Result&: This, Info); |
4712 | |
4713 | if (Object->getType()->isLiteralType(Ctx: Info.Ctx)) |
4714 | return EvaluateTemporary(E: Object, Result&: This, Info); |
4715 | |
4716 | if (Object->getType()->isRecordType() && Object->isPRValue()) |
4717 | return EvaluateTemporary(E: Object, Result&: This, Info); |
4718 | |
4719 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
4720 | return false; |
4721 | } |
4722 | |
4723 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
4724 | /// lvalue referring to the result. |
4725 | /// |
4726 | /// \param Info - Information about the ongoing evaluation. |
4727 | /// \param LV - An lvalue referring to the base of the member pointer. |
4728 | /// \param RHS - The member pointer expression. |
4729 | /// \param IncludeMember - Specifies whether the member itself is included in |
4730 | /// the resulting LValue subobject designator. This is not possible when |
4731 | /// creating a bound member function. |
4732 | /// \return The field or method declaration to which the member pointer refers, |
4733 | /// or 0 if evaluation fails. |
4734 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4735 | QualType LVType, |
4736 | LValue &LV, |
4737 | const Expr *RHS, |
4738 | bool IncludeMember = true) { |
4739 | MemberPtr MemPtr; |
4740 | if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info)) |
4741 | return nullptr; |
4742 | |
4743 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
4744 | // member value, the behavior is undefined. |
4745 | if (!MemPtr.getDecl()) { |
4746 | // FIXME: Specific diagnostic. |
4747 | Info.FFDiag(E: RHS); |
4748 | return nullptr; |
4749 | } |
4750 | |
4751 | if (MemPtr.isDerivedMember()) { |
4752 | // This is a member of some derived class. Truncate LV appropriately. |
4753 | // The end of the derived-to-base path for the base object must match the |
4754 | // derived-to-base path for the member pointer. |
4755 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
4756 | LV.Designator.Entries.size()) { |
4757 | Info.FFDiag(E: RHS); |
4758 | return nullptr; |
4759 | } |
4760 | unsigned PathLengthToMember = |
4761 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
4762 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
4763 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
4764 | LV.Designator.Entries[PathLengthToMember + I]); |
4765 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
4766 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
4767 | Info.FFDiag(E: RHS); |
4768 | return nullptr; |
4769 | } |
4770 | } |
4771 | |
4772 | // Truncate the lvalue to the appropriate derived class. |
4773 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
4774 | PathLengthToMember)) |
4775 | return nullptr; |
4776 | } else if (!MemPtr.Path.empty()) { |
4777 | // Extend the LValue path with the member pointer's path. |
4778 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
4779 | MemPtr.Path.size() + IncludeMember); |
4780 | |
4781 | // Walk down to the appropriate base class. |
4782 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
4783 | LVType = PT->getPointeeType(); |
4784 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
4785 | assert(RD && "member pointer access on non-class-type expression" ); |
4786 | // The first class in the path is that of the lvalue. |
4787 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
4788 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
4789 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base)) |
4790 | return nullptr; |
4791 | RD = Base; |
4792 | } |
4793 | // Finally cast to the class containing the member. |
4794 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, |
4795 | Base: MemPtr.getContainingRecord())) |
4796 | return nullptr; |
4797 | } |
4798 | |
4799 | // Add the member. Note that we cannot build bound member functions here. |
4800 | if (IncludeMember) { |
4801 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) { |
4802 | if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD)) |
4803 | return nullptr; |
4804 | } else if (const IndirectFieldDecl *IFD = |
4805 | dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) { |
4806 | if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD)) |
4807 | return nullptr; |
4808 | } else { |
4809 | llvm_unreachable("can't construct reference to bound member function" ); |
4810 | } |
4811 | } |
4812 | |
4813 | return MemPtr.getDecl(); |
4814 | } |
4815 | |
4816 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4817 | const BinaryOperator *BO, |
4818 | LValue &LV, |
4819 | bool IncludeMember = true) { |
4820 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
4821 | |
4822 | if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) { |
4823 | if (Info.noteFailure()) { |
4824 | MemberPtr MemPtr; |
4825 | EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info); |
4826 | } |
4827 | return nullptr; |
4828 | } |
4829 | |
4830 | return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV, |
4831 | RHS: BO->getRHS(), IncludeMember); |
4832 | } |
4833 | |
4834 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
4835 | /// the provided lvalue, which currently refers to the base object. |
4836 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
4837 | LValue &Result) { |
4838 | SubobjectDesignator &D = Result.Designator; |
4839 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
4840 | return false; |
4841 | |
4842 | QualType TargetQT = E->getType(); |
4843 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
4844 | TargetQT = PT->getPointeeType(); |
4845 | |
4846 | // Check this cast lands within the final derived-to-base subobject path. |
4847 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
4848 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4849 | << D.MostDerivedType << TargetQT; |
4850 | return false; |
4851 | } |
4852 | |
4853 | // Check the type of the final cast. We don't need to check the path, |
4854 | // since a cast can only be formed if the path is unique. |
4855 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
4856 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
4857 | const CXXRecordDecl *FinalType; |
4858 | if (NewEntriesSize == D.MostDerivedPathLength) |
4859 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
4860 | else |
4861 | FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]); |
4862 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
4863 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4864 | << D.MostDerivedType << TargetQT; |
4865 | return false; |
4866 | } |
4867 | |
4868 | // Truncate the lvalue to the appropriate derived class. |
4869 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
4870 | } |
4871 | |
4872 | /// Get the value to use for a default-initialized object of type T. |
4873 | /// Return false if it encounters something invalid. |
4874 | static bool handleDefaultInitValue(QualType T, APValue &Result) { |
4875 | bool Success = true; |
4876 | |
4877 | // If there is already a value present don't overwrite it. |
4878 | if (!Result.isAbsent()) |
4879 | return true; |
4880 | |
4881 | if (auto *RD = T->getAsCXXRecordDecl()) { |
4882 | if (RD->isInvalidDecl()) { |
4883 | Result = APValue(); |
4884 | return false; |
4885 | } |
4886 | if (RD->isUnion()) { |
4887 | Result = APValue((const FieldDecl *)nullptr); |
4888 | return true; |
4889 | } |
4890 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
4891 | std::distance(RD->field_begin(), RD->field_end())); |
4892 | |
4893 | unsigned Index = 0; |
4894 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
4895 | End = RD->bases_end(); |
4896 | I != End; ++I, ++Index) |
4897 | Success &= |
4898 | handleDefaultInitValue(T: I->getType(), Result&: Result.getStructBase(i: Index)); |
4899 | |
4900 | for (const auto *I : RD->fields()) { |
4901 | if (I->isUnnamedBitField()) |
4902 | continue; |
4903 | Success &= handleDefaultInitValue( |
4904 | I->getType(), Result.getStructField(I->getFieldIndex())); |
4905 | } |
4906 | return Success; |
4907 | } |
4908 | |
4909 | if (auto *AT = |
4910 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
4911 | Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize()); |
4912 | if (Result.hasArrayFiller()) |
4913 | Success &= |
4914 | handleDefaultInitValue(AT->getElementType(), Result.getArrayFiller()); |
4915 | |
4916 | return Success; |
4917 | } |
4918 | |
4919 | Result = APValue::IndeterminateValue(); |
4920 | return true; |
4921 | } |
4922 | |
4923 | namespace { |
4924 | enum EvalStmtResult { |
4925 | /// Evaluation failed. |
4926 | ESR_Failed, |
4927 | /// Hit a 'return' statement. |
4928 | ESR_Returned, |
4929 | /// Evaluation succeeded. |
4930 | ESR_Succeeded, |
4931 | /// Hit a 'continue' statement. |
4932 | ESR_Continue, |
4933 | /// Hit a 'break' statement. |
4934 | ESR_Break, |
4935 | /// Still scanning for 'case' or 'default' statement. |
4936 | ESR_CaseNotFound |
4937 | }; |
4938 | } |
4939 | |
4940 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
4941 | if (VD->isInvalidDecl()) |
4942 | return false; |
4943 | // We don't need to evaluate the initializer for a static local. |
4944 | if (!VD->hasLocalStorage()) |
4945 | return true; |
4946 | |
4947 | LValue Result; |
4948 | APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(), |
4949 | ScopeKind::Block, Result); |
4950 | |
4951 | const Expr *InitE = VD->getInit(); |
4952 | if (!InitE) { |
4953 | if (VD->getType()->isDependentType()) |
4954 | return Info.noteSideEffect(); |
4955 | return handleDefaultInitValue(VD->getType(), Val); |
4956 | } |
4957 | if (InitE->isValueDependent()) |
4958 | return false; |
4959 | |
4960 | if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) { |
4961 | // Wipe out any partially-computed value, to allow tracking that this |
4962 | // evaluation failed. |
4963 | Val = APValue(); |
4964 | return false; |
4965 | } |
4966 | |
4967 | return true; |
4968 | } |
4969 | |
4970 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
4971 | bool OK = true; |
4972 | |
4973 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
4974 | OK &= EvaluateVarDecl(Info, VD); |
4975 | |
4976 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D)) |
4977 | for (auto *BD : DD->bindings()) |
4978 | if (auto *VD = BD->getHoldingVar()) |
4979 | OK &= EvaluateDecl(Info, VD); |
4980 | |
4981 | return OK; |
4982 | } |
4983 | |
4984 | static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) { |
4985 | assert(E->isValueDependent()); |
4986 | if (Info.noteSideEffect()) |
4987 | return true; |
4988 | assert(E->containsErrors() && "valid value-dependent expression should never " |
4989 | "reach invalid code path." ); |
4990 | return false; |
4991 | } |
4992 | |
4993 | /// Evaluate a condition (either a variable declaration or an expression). |
4994 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
4995 | const Expr *Cond, bool &Result) { |
4996 | if (Cond->isValueDependent()) |
4997 | return false; |
4998 | FullExpressionRAII Scope(Info); |
4999 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
5000 | return false; |
5001 | if (!EvaluateAsBooleanCondition(E: Cond, Result, Info)) |
5002 | return false; |
5003 | return Scope.destroy(); |
5004 | } |
5005 | |
5006 | namespace { |
5007 | /// A location where the result (returned value) of evaluating a |
5008 | /// statement should be stored. |
5009 | struct StmtResult { |
5010 | /// The APValue that should be filled in with the returned value. |
5011 | APValue &Value; |
5012 | /// The location containing the result, if any (used to support RVO). |
5013 | const LValue *Slot; |
5014 | }; |
5015 | |
5016 | struct TempVersionRAII { |
5017 | CallStackFrame &Frame; |
5018 | |
5019 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
5020 | Frame.pushTempVersion(); |
5021 | } |
5022 | |
5023 | ~TempVersionRAII() { |
5024 | Frame.popTempVersion(); |
5025 | } |
5026 | }; |
5027 | |
5028 | } |
5029 | |
5030 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5031 | const Stmt *S, |
5032 | const SwitchCase *SC = nullptr); |
5033 | |
5034 | /// Evaluate the body of a loop, and translate the result as appropriate. |
5035 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
5036 | const Stmt *Body, |
5037 | const SwitchCase *Case = nullptr) { |
5038 | BlockScopeRAII Scope(Info); |
5039 | |
5040 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case); |
5041 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5042 | ESR = ESR_Failed; |
5043 | |
5044 | switch (ESR) { |
5045 | case ESR_Break: |
5046 | return ESR_Succeeded; |
5047 | case ESR_Succeeded: |
5048 | case ESR_Continue: |
5049 | return ESR_Continue; |
5050 | case ESR_Failed: |
5051 | case ESR_Returned: |
5052 | case ESR_CaseNotFound: |
5053 | return ESR; |
5054 | } |
5055 | llvm_unreachable("Invalid EvalStmtResult!" ); |
5056 | } |
5057 | |
5058 | /// Evaluate a switch statement. |
5059 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
5060 | const SwitchStmt *SS) { |
5061 | BlockScopeRAII Scope(Info); |
5062 | |
5063 | // Evaluate the switch condition. |
5064 | APSInt Value; |
5065 | { |
5066 | if (const Stmt *Init = SS->getInit()) { |
5067 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5068 | if (ESR != ESR_Succeeded) { |
5069 | if (ESR != ESR_Failed && !Scope.destroy()) |
5070 | ESR = ESR_Failed; |
5071 | return ESR; |
5072 | } |
5073 | } |
5074 | |
5075 | FullExpressionRAII CondScope(Info); |
5076 | if (SS->getConditionVariable() && |
5077 | !EvaluateDecl(Info, SS->getConditionVariable())) |
5078 | return ESR_Failed; |
5079 | if (SS->getCond()->isValueDependent()) { |
5080 | // We don't know what the value is, and which branch should jump to. |
5081 | EvaluateDependentExpr(E: SS->getCond(), Info); |
5082 | return ESR_Failed; |
5083 | } |
5084 | if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info)) |
5085 | return ESR_Failed; |
5086 | |
5087 | if (!CondScope.destroy()) |
5088 | return ESR_Failed; |
5089 | } |
5090 | |
5091 | // Find the switch case corresponding to the value of the condition. |
5092 | // FIXME: Cache this lookup. |
5093 | const SwitchCase *Found = nullptr; |
5094 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
5095 | SC = SC->getNextSwitchCase()) { |
5096 | if (isa<DefaultStmt>(Val: SC)) { |
5097 | Found = SC; |
5098 | continue; |
5099 | } |
5100 | |
5101 | const CaseStmt *CS = cast<CaseStmt>(Val: SC); |
5102 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Ctx: Info.Ctx); |
5103 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Ctx: Info.Ctx) |
5104 | : LHS; |
5105 | if (LHS <= Value && Value <= RHS) { |
5106 | Found = SC; |
5107 | break; |
5108 | } |
5109 | } |
5110 | |
5111 | if (!Found) |
5112 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5113 | |
5114 | // Search the switch body for the switch case and evaluate it from there. |
5115 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found); |
5116 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5117 | return ESR_Failed; |
5118 | |
5119 | switch (ESR) { |
5120 | case ESR_Break: |
5121 | return ESR_Succeeded; |
5122 | case ESR_Succeeded: |
5123 | case ESR_Continue: |
5124 | case ESR_Failed: |
5125 | case ESR_Returned: |
5126 | return ESR; |
5127 | case ESR_CaseNotFound: |
5128 | // This can only happen if the switch case is nested within a statement |
5129 | // expression. We have no intention of supporting that. |
5130 | Info.FFDiag(Found->getBeginLoc(), |
5131 | diag::note_constexpr_stmt_expr_unsupported); |
5132 | return ESR_Failed; |
5133 | } |
5134 | llvm_unreachable("Invalid EvalStmtResult!" ); |
5135 | } |
5136 | |
5137 | static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) { |
5138 | // An expression E is a core constant expression unless the evaluation of E |
5139 | // would evaluate one of the following: [C++23] - a control flow that passes |
5140 | // through a declaration of a variable with static or thread storage duration |
5141 | // unless that variable is usable in constant expressions. |
5142 | if (VD->isLocalVarDecl() && VD->isStaticLocal() && |
5143 | !VD->isUsableInConstantExpressions(C: Info.Ctx)) { |
5144 | Info.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local) |
5145 | << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD; |
5146 | return false; |
5147 | } |
5148 | return true; |
5149 | } |
5150 | |
5151 | // Evaluate a statement. |
5152 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5153 | const Stmt *S, const SwitchCase *Case) { |
5154 | if (!Info.nextStep(S)) |
5155 | return ESR_Failed; |
5156 | |
5157 | // If we're hunting down a 'case' or 'default' label, recurse through |
5158 | // substatements until we hit the label. |
5159 | if (Case) { |
5160 | switch (S->getStmtClass()) { |
5161 | case Stmt::CompoundStmtClass: |
5162 | // FIXME: Precompute which substatement of a compound statement we |
5163 | // would jump to, and go straight there rather than performing a |
5164 | // linear scan each time. |
5165 | case Stmt::LabelStmtClass: |
5166 | case Stmt::AttributedStmtClass: |
5167 | case Stmt::DoStmtClass: |
5168 | break; |
5169 | |
5170 | case Stmt::CaseStmtClass: |
5171 | case Stmt::DefaultStmtClass: |
5172 | if (Case == S) |
5173 | Case = nullptr; |
5174 | break; |
5175 | |
5176 | case Stmt::IfStmtClass: { |
5177 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
5178 | // straight there rather than scanning both sides. |
5179 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5180 | |
5181 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
5182 | // preceded by our switch label. |
5183 | BlockScopeRAII Scope(Info); |
5184 | |
5185 | // Step into the init statement in case it brings an (uninitialized) |
5186 | // variable into scope. |
5187 | if (const Stmt *Init = IS->getInit()) { |
5188 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5189 | if (ESR != ESR_CaseNotFound) { |
5190 | assert(ESR != ESR_Succeeded); |
5191 | return ESR; |
5192 | } |
5193 | } |
5194 | |
5195 | // Condition variable must be initialized if it exists. |
5196 | // FIXME: We can skip evaluating the body if there's a condition |
5197 | // variable, as there can't be any case labels within it. |
5198 | // (The same is true for 'for' statements.) |
5199 | |
5200 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case); |
5201 | if (ESR == ESR_Failed) |
5202 | return ESR; |
5203 | if (ESR != ESR_CaseNotFound) |
5204 | return Scope.destroy() ? ESR : ESR_Failed; |
5205 | if (!IS->getElse()) |
5206 | return ESR_CaseNotFound; |
5207 | |
5208 | ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case); |
5209 | if (ESR == ESR_Failed) |
5210 | return ESR; |
5211 | if (ESR != ESR_CaseNotFound) |
5212 | return Scope.destroy() ? ESR : ESR_Failed; |
5213 | return ESR_CaseNotFound; |
5214 | } |
5215 | |
5216 | case Stmt::WhileStmtClass: { |
5217 | EvalStmtResult ESR = |
5218 | EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case); |
5219 | if (ESR != ESR_Continue) |
5220 | return ESR; |
5221 | break; |
5222 | } |
5223 | |
5224 | case Stmt::ForStmtClass: { |
5225 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5226 | BlockScopeRAII Scope(Info); |
5227 | |
5228 | // Step into the init statement in case it brings an (uninitialized) |
5229 | // variable into scope. |
5230 | if (const Stmt *Init = FS->getInit()) { |
5231 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5232 | if (ESR != ESR_CaseNotFound) { |
5233 | assert(ESR != ESR_Succeeded); |
5234 | return ESR; |
5235 | } |
5236 | } |
5237 | |
5238 | EvalStmtResult ESR = |
5239 | EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case); |
5240 | if (ESR != ESR_Continue) |
5241 | return ESR; |
5242 | if (const auto *Inc = FS->getInc()) { |
5243 | if (Inc->isValueDependent()) { |
5244 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5245 | return ESR_Failed; |
5246 | } else { |
5247 | FullExpressionRAII IncScope(Info); |
5248 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5249 | return ESR_Failed; |
5250 | } |
5251 | } |
5252 | break; |
5253 | } |
5254 | |
5255 | case Stmt::DeclStmtClass: { |
5256 | // Start the lifetime of any uninitialized variables we encounter. They |
5257 | // might be used by the selected branch of the switch. |
5258 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5259 | for (const auto *D : DS->decls()) { |
5260 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
5261 | if (!CheckLocalVariableDeclaration(Info, VD)) |
5262 | return ESR_Failed; |
5263 | if (VD->hasLocalStorage() && !VD->getInit()) |
5264 | if (!EvaluateVarDecl(Info, VD)) |
5265 | return ESR_Failed; |
5266 | // FIXME: If the variable has initialization that can't be jumped |
5267 | // over, bail out of any immediately-surrounding compound-statement |
5268 | // too. There can't be any case labels here. |
5269 | } |
5270 | } |
5271 | return ESR_CaseNotFound; |
5272 | } |
5273 | |
5274 | default: |
5275 | return ESR_CaseNotFound; |
5276 | } |
5277 | } |
5278 | |
5279 | switch (S->getStmtClass()) { |
5280 | default: |
5281 | if (const Expr *E = dyn_cast<Expr>(Val: S)) { |
5282 | if (E->isValueDependent()) { |
5283 | if (!EvaluateDependentExpr(E, Info)) |
5284 | return ESR_Failed; |
5285 | } else { |
5286 | // Don't bother evaluating beyond an expression-statement which couldn't |
5287 | // be evaluated. |
5288 | // FIXME: Do we need the FullExpressionRAII object here? |
5289 | // VisitExprWithCleanups should create one when necessary. |
5290 | FullExpressionRAII Scope(Info); |
5291 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
5292 | return ESR_Failed; |
5293 | } |
5294 | return ESR_Succeeded; |
5295 | } |
5296 | |
5297 | Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange(); |
5298 | return ESR_Failed; |
5299 | |
5300 | case Stmt::NullStmtClass: |
5301 | return ESR_Succeeded; |
5302 | |
5303 | case Stmt::DeclStmtClass: { |
5304 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5305 | for (const auto *D : DS->decls()) { |
5306 | const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D); |
5307 | if (VD && !CheckLocalVariableDeclaration(Info, VD)) |
5308 | return ESR_Failed; |
5309 | // Each declaration initialization is its own full-expression. |
5310 | FullExpressionRAII Scope(Info); |
5311 | if (!EvaluateDecl(Info, D) && !Info.noteFailure()) |
5312 | return ESR_Failed; |
5313 | if (!Scope.destroy()) |
5314 | return ESR_Failed; |
5315 | } |
5316 | return ESR_Succeeded; |
5317 | } |
5318 | |
5319 | case Stmt::ReturnStmtClass: { |
5320 | const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue(); |
5321 | FullExpressionRAII Scope(Info); |
5322 | if (RetExpr && RetExpr->isValueDependent()) { |
5323 | EvaluateDependentExpr(E: RetExpr, Info); |
5324 | // We know we returned, but we don't know what the value is. |
5325 | return ESR_Failed; |
5326 | } |
5327 | if (RetExpr && |
5328 | !(Result.Slot |
5329 | ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr) |
5330 | : Evaluate(Result&: Result.Value, Info, E: RetExpr))) |
5331 | return ESR_Failed; |
5332 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
5333 | } |
5334 | |
5335 | case Stmt::CompoundStmtClass: { |
5336 | BlockScopeRAII Scope(Info); |
5337 | |
5338 | const CompoundStmt *CS = cast<CompoundStmt>(Val: S); |
5339 | for (const auto *BI : CS->body()) { |
5340 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case); |
5341 | if (ESR == ESR_Succeeded) |
5342 | Case = nullptr; |
5343 | else if (ESR != ESR_CaseNotFound) { |
5344 | if (ESR != ESR_Failed && !Scope.destroy()) |
5345 | return ESR_Failed; |
5346 | return ESR; |
5347 | } |
5348 | } |
5349 | if (Case) |
5350 | return ESR_CaseNotFound; |
5351 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5352 | } |
5353 | |
5354 | case Stmt::IfStmtClass: { |
5355 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5356 | |
5357 | // Evaluate the condition, as either a var decl or as an expression. |
5358 | BlockScopeRAII Scope(Info); |
5359 | if (const Stmt *Init = IS->getInit()) { |
5360 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5361 | if (ESR != ESR_Succeeded) { |
5362 | if (ESR != ESR_Failed && !Scope.destroy()) |
5363 | return ESR_Failed; |
5364 | return ESR; |
5365 | } |
5366 | } |
5367 | bool Cond; |
5368 | if (IS->isConsteval()) { |
5369 | Cond = IS->isNonNegatedConsteval(); |
5370 | // If we are not in a constant context, if consteval should not evaluate |
5371 | // to true. |
5372 | if (!Info.InConstantContext) |
5373 | Cond = !Cond; |
5374 | } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(), |
5375 | Result&: Cond)) |
5376 | return ESR_Failed; |
5377 | |
5378 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
5379 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt); |
5380 | if (ESR != ESR_Succeeded) { |
5381 | if (ESR != ESR_Failed && !Scope.destroy()) |
5382 | return ESR_Failed; |
5383 | return ESR; |
5384 | } |
5385 | } |
5386 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5387 | } |
5388 | |
5389 | case Stmt::WhileStmtClass: { |
5390 | const WhileStmt *WS = cast<WhileStmt>(Val: S); |
5391 | while (true) { |
5392 | BlockScopeRAII Scope(Info); |
5393 | bool Continue; |
5394 | if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(), |
5395 | Result&: Continue)) |
5396 | return ESR_Failed; |
5397 | if (!Continue) |
5398 | break; |
5399 | |
5400 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody()); |
5401 | if (ESR != ESR_Continue) { |
5402 | if (ESR != ESR_Failed && !Scope.destroy()) |
5403 | return ESR_Failed; |
5404 | return ESR; |
5405 | } |
5406 | if (!Scope.destroy()) |
5407 | return ESR_Failed; |
5408 | } |
5409 | return ESR_Succeeded; |
5410 | } |
5411 | |
5412 | case Stmt::DoStmtClass: { |
5413 | const DoStmt *DS = cast<DoStmt>(Val: S); |
5414 | bool Continue; |
5415 | do { |
5416 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case); |
5417 | if (ESR != ESR_Continue) |
5418 | return ESR; |
5419 | Case = nullptr; |
5420 | |
5421 | if (DS->getCond()->isValueDependent()) { |
5422 | EvaluateDependentExpr(E: DS->getCond(), Info); |
5423 | // Bailout as we don't know whether to keep going or terminate the loop. |
5424 | return ESR_Failed; |
5425 | } |
5426 | FullExpressionRAII CondScope(Info); |
5427 | if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) || |
5428 | !CondScope.destroy()) |
5429 | return ESR_Failed; |
5430 | } while (Continue); |
5431 | return ESR_Succeeded; |
5432 | } |
5433 | |
5434 | case Stmt::ForStmtClass: { |
5435 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5436 | BlockScopeRAII ForScope(Info); |
5437 | if (FS->getInit()) { |
5438 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5439 | if (ESR != ESR_Succeeded) { |
5440 | if (ESR != ESR_Failed && !ForScope.destroy()) |
5441 | return ESR_Failed; |
5442 | return ESR; |
5443 | } |
5444 | } |
5445 | while (true) { |
5446 | BlockScopeRAII IterScope(Info); |
5447 | bool Continue = true; |
5448 | if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(), |
5449 | Cond: FS->getCond(), Result&: Continue)) |
5450 | return ESR_Failed; |
5451 | if (!Continue) |
5452 | break; |
5453 | |
5454 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5455 | if (ESR != ESR_Continue) { |
5456 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
5457 | return ESR_Failed; |
5458 | return ESR; |
5459 | } |
5460 | |
5461 | if (const auto *Inc = FS->getInc()) { |
5462 | if (Inc->isValueDependent()) { |
5463 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5464 | return ESR_Failed; |
5465 | } else { |
5466 | FullExpressionRAII IncScope(Info); |
5467 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5468 | return ESR_Failed; |
5469 | } |
5470 | } |
5471 | |
5472 | if (!IterScope.destroy()) |
5473 | return ESR_Failed; |
5474 | } |
5475 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
5476 | } |
5477 | |
5478 | case Stmt::CXXForRangeStmtClass: { |
5479 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S); |
5480 | BlockScopeRAII Scope(Info); |
5481 | |
5482 | // Evaluate the init-statement if present. |
5483 | if (FS->getInit()) { |
5484 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5485 | if (ESR != ESR_Succeeded) { |
5486 | if (ESR != ESR_Failed && !Scope.destroy()) |
5487 | return ESR_Failed; |
5488 | return ESR; |
5489 | } |
5490 | } |
5491 | |
5492 | // Initialize the __range variable. |
5493 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt()); |
5494 | if (ESR != ESR_Succeeded) { |
5495 | if (ESR != ESR_Failed && !Scope.destroy()) |
5496 | return ESR_Failed; |
5497 | return ESR; |
5498 | } |
5499 | |
5500 | // In error-recovery cases it's possible to get here even if we failed to |
5501 | // synthesize the __begin and __end variables. |
5502 | if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond()) |
5503 | return ESR_Failed; |
5504 | |
5505 | // Create the __begin and __end iterators. |
5506 | ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt()); |
5507 | if (ESR != ESR_Succeeded) { |
5508 | if (ESR != ESR_Failed && !Scope.destroy()) |
5509 | return ESR_Failed; |
5510 | return ESR; |
5511 | } |
5512 | ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt()); |
5513 | if (ESR != ESR_Succeeded) { |
5514 | if (ESR != ESR_Failed && !Scope.destroy()) |
5515 | return ESR_Failed; |
5516 | return ESR; |
5517 | } |
5518 | |
5519 | while (true) { |
5520 | // Condition: __begin != __end. |
5521 | { |
5522 | if (FS->getCond()->isValueDependent()) { |
5523 | EvaluateDependentExpr(E: FS->getCond(), Info); |
5524 | // We don't know whether to keep going or terminate the loop. |
5525 | return ESR_Failed; |
5526 | } |
5527 | bool Continue = true; |
5528 | FullExpressionRAII CondExpr(Info); |
5529 | if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info)) |
5530 | return ESR_Failed; |
5531 | if (!Continue) |
5532 | break; |
5533 | } |
5534 | |
5535 | // User's variable declaration, initialized by *__begin. |
5536 | BlockScopeRAII InnerScope(Info); |
5537 | ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt()); |
5538 | if (ESR != ESR_Succeeded) { |
5539 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5540 | return ESR_Failed; |
5541 | return ESR; |
5542 | } |
5543 | |
5544 | // Loop body. |
5545 | ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5546 | if (ESR != ESR_Continue) { |
5547 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5548 | return ESR_Failed; |
5549 | return ESR; |
5550 | } |
5551 | if (FS->getInc()->isValueDependent()) { |
5552 | if (!EvaluateDependentExpr(E: FS->getInc(), Info)) |
5553 | return ESR_Failed; |
5554 | } else { |
5555 | // Increment: ++__begin |
5556 | if (!EvaluateIgnoredValue(Info, E: FS->getInc())) |
5557 | return ESR_Failed; |
5558 | } |
5559 | |
5560 | if (!InnerScope.destroy()) |
5561 | return ESR_Failed; |
5562 | } |
5563 | |
5564 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5565 | } |
5566 | |
5567 | case Stmt::SwitchStmtClass: |
5568 | return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S)); |
5569 | |
5570 | case Stmt::ContinueStmtClass: |
5571 | return ESR_Continue; |
5572 | |
5573 | case Stmt::BreakStmtClass: |
5574 | return ESR_Break; |
5575 | |
5576 | case Stmt::LabelStmtClass: |
5577 | return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case); |
5578 | |
5579 | case Stmt::AttributedStmtClass: { |
5580 | const auto *AS = cast<AttributedStmt>(Val: S); |
5581 | const auto *SS = AS->getSubStmt(); |
5582 | MSConstexprContextRAII ConstexprContext( |
5583 | *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(AS->getAttrs()) && |
5584 | isa<ReturnStmt>(SS)); |
5585 | |
5586 | auto LO = Info.getCtx().getLangOpts(); |
5587 | if (LO.CXXAssumptions && !LO.MSVCCompat) { |
5588 | for (auto *Attr : AS->getAttrs()) { |
5589 | auto *AA = dyn_cast<CXXAssumeAttr>(Attr); |
5590 | if (!AA) |
5591 | continue; |
5592 | |
5593 | auto *Assumption = AA->getAssumption(); |
5594 | if (Assumption->isValueDependent()) |
5595 | return ESR_Failed; |
5596 | |
5597 | if (Assumption->HasSideEffects(Info.getCtx())) |
5598 | continue; |
5599 | |
5600 | bool Value; |
5601 | if (!EvaluateAsBooleanCondition(Assumption, Value, Info)) |
5602 | return ESR_Failed; |
5603 | if (!Value) { |
5604 | Info.CCEDiag(Assumption->getExprLoc(), |
5605 | diag::note_constexpr_assumption_failed); |
5606 | return ESR_Failed; |
5607 | } |
5608 | } |
5609 | } |
5610 | |
5611 | return EvaluateStmt(Result, Info, S: SS, Case); |
5612 | } |
5613 | |
5614 | case Stmt::CaseStmtClass: |
5615 | case Stmt::DefaultStmtClass: |
5616 | return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case); |
5617 | case Stmt::CXXTryStmtClass: |
5618 | // Evaluate try blocks by evaluating all sub statements. |
5619 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(Val: S)->getTryBlock(), Case); |
5620 | } |
5621 | } |
5622 | |
5623 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
5624 | /// default constructor. If so, we'll fold it whether or not it's marked as |
5625 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
5626 | /// so we need special handling. |
5627 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
5628 | const CXXConstructorDecl *CD, |
5629 | bool IsValueInitialization) { |
5630 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
5631 | return false; |
5632 | |
5633 | // Value-initialization does not call a trivial default constructor, so such a |
5634 | // call is a core constant expression whether or not the constructor is |
5635 | // constexpr. |
5636 | if (!CD->isConstexpr() && !IsValueInitialization) { |
5637 | if (Info.getLangOpts().CPlusPlus11) { |
5638 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
5639 | // we should be much more explicit about why it's not constexpr. |
5640 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
5641 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
5642 | Info.Note(CD->getLocation(), diag::note_declared_at); |
5643 | } else { |
5644 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
5645 | } |
5646 | } |
5647 | return true; |
5648 | } |
5649 | |
5650 | /// CheckConstexprFunction - Check that a function can be called in a constant |
5651 | /// expression. |
5652 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
5653 | const FunctionDecl *Declaration, |
5654 | const FunctionDecl *Definition, |
5655 | const Stmt *Body) { |
5656 | // Potential constant expressions can contain calls to declared, but not yet |
5657 | // defined, constexpr functions. |
5658 | if (Info.checkingPotentialConstantExpression() && !Definition && |
5659 | Declaration->isConstexpr()) |
5660 | return false; |
5661 | |
5662 | // Bail out if the function declaration itself is invalid. We will |
5663 | // have produced a relevant diagnostic while parsing it, so just |
5664 | // note the problematic sub-expression. |
5665 | if (Declaration->isInvalidDecl()) { |
5666 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5667 | return false; |
5668 | } |
5669 | |
5670 | // DR1872: An instantiated virtual constexpr function can't be called in a |
5671 | // constant expression (prior to C++20). We can still constant-fold such a |
5672 | // call. |
5673 | if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) && |
5674 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
5675 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
5676 | |
5677 | if (Definition && Definition->isInvalidDecl()) { |
5678 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5679 | return false; |
5680 | } |
5681 | |
5682 | // Can we evaluate this function call? |
5683 | if (Definition && Body && |
5684 | (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr && |
5685 | Definition->hasAttr<MSConstexprAttr>()))) |
5686 | return true; |
5687 | |
5688 | if (Info.getLangOpts().CPlusPlus11) { |
5689 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
5690 | |
5691 | // If this function is not constexpr because it is an inherited |
5692 | // non-constexpr constructor, diagnose that directly. |
5693 | auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl); |
5694 | if (CD && CD->isInheritingConstructor()) { |
5695 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
5696 | if (!Inherited->isConstexpr()) |
5697 | DiagDecl = CD = Inherited; |
5698 | } |
5699 | |
5700 | // FIXME: If DiagDecl is an implicitly-declared special member function |
5701 | // or an inheriting constructor, we should be much more explicit about why |
5702 | // it's not constexpr. |
5703 | if (CD && CD->isInheritingConstructor()) |
5704 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
5705 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
5706 | else |
5707 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
5708 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
5709 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
5710 | } else { |
5711 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5712 | } |
5713 | return false; |
5714 | } |
5715 | |
5716 | namespace { |
5717 | struct CheckDynamicTypeHandler { |
5718 | AccessKinds AccessKind; |
5719 | typedef bool result_type; |
5720 | bool failed() { return false; } |
5721 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
5722 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
5723 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
5724 | }; |
5725 | } // end anonymous namespace |
5726 | |
5727 | /// Check that we can access the notional vptr of an object / determine its |
5728 | /// dynamic type. |
5729 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
5730 | AccessKinds AK, bool Polymorphic) { |
5731 | if (This.Designator.Invalid) |
5732 | return false; |
5733 | |
5734 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType()); |
5735 | |
5736 | if (!Obj) |
5737 | return false; |
5738 | |
5739 | if (!Obj.Value) { |
5740 | // The object is not usable in constant expressions, so we can't inspect |
5741 | // its value to see if it's in-lifetime or what the active union members |
5742 | // are. We can still check for a one-past-the-end lvalue. |
5743 | if (This.Designator.isOnePastTheEnd() || |
5744 | This.Designator.isMostDerivedAnUnsizedArray()) { |
5745 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
5746 | ? diag::note_constexpr_access_past_end |
5747 | : diag::note_constexpr_access_unsized_array) |
5748 | << AK; |
5749 | return false; |
5750 | } else if (Polymorphic) { |
5751 | // Conservatively refuse to perform a polymorphic operation if we would |
5752 | // not be able to read a notional 'vptr' value. |
5753 | APValue Val; |
5754 | This.moveInto(V&: Val); |
5755 | QualType StarThisType = |
5756 | Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Info.Ctx)); |
5757 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
5758 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
5759 | return false; |
5760 | } |
5761 | return true; |
5762 | } |
5763 | |
5764 | CheckDynamicTypeHandler Handler{.AccessKind: AK}; |
5765 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
5766 | } |
5767 | |
5768 | /// Check that the pointee of the 'this' pointer in a member function call is |
5769 | /// either within its lifetime or in its period of construction or destruction. |
5770 | static bool |
5771 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
5772 | const LValue &This, |
5773 | const CXXMethodDecl *NamedMember) { |
5774 | return checkDynamicType( |
5775 | Info, E, This, |
5776 | AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false); |
5777 | } |
5778 | |
5779 | struct DynamicType { |
5780 | /// The dynamic class type of the object. |
5781 | const CXXRecordDecl *Type; |
5782 | /// The corresponding path length in the lvalue. |
5783 | unsigned PathLength; |
5784 | }; |
5785 | |
5786 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
5787 | unsigned PathLength) { |
5788 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
5789 | Designator.Entries.size() && "invalid path length" ); |
5790 | return (PathLength == Designator.MostDerivedPathLength) |
5791 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
5792 | : getAsBaseClass(E: Designator.Entries[PathLength - 1]); |
5793 | } |
5794 | |
5795 | /// Determine the dynamic type of an object. |
5796 | static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info, |
5797 | const Expr *E, |
5798 | LValue &This, |
5799 | AccessKinds AK) { |
5800 | // If we don't have an lvalue denoting an object of class type, there is no |
5801 | // meaningful dynamic type. (We consider objects of non-class type to have no |
5802 | // dynamic type.) |
5803 | if (!checkDynamicType(Info, E, This, AK, Polymorphic: true)) |
5804 | return std::nullopt; |
5805 | |
5806 | // Refuse to compute a dynamic type in the presence of virtual bases. This |
5807 | // shouldn't happen other than in constant-folding situations, since literal |
5808 | // types can't have virtual bases. |
5809 | // |
5810 | // Note that consumers of DynamicType assume that the type has no virtual |
5811 | // bases, and will need modifications if this restriction is relaxed. |
5812 | const CXXRecordDecl *Class = |
5813 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
5814 | if (!Class || Class->getNumVBases()) { |
5815 | Info.FFDiag(E); |
5816 | return std::nullopt; |
5817 | } |
5818 | |
5819 | // FIXME: For very deep class hierarchies, it might be beneficial to use a |
5820 | // binary search here instead. But the overwhelmingly common case is that |
5821 | // we're not in the middle of a constructor, so it probably doesn't matter |
5822 | // in practice. |
5823 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
5824 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
5825 | PathLength <= Path.size(); ++PathLength) { |
5826 | switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(), |
5827 | Path: Path.slice(N: 0, M: PathLength))) { |
5828 | case ConstructionPhase::Bases: |
5829 | case ConstructionPhase::DestroyingBases: |
5830 | // We're constructing or destroying a base class. This is not the dynamic |
5831 | // type. |
5832 | break; |
5833 | |
5834 | case ConstructionPhase::None: |
5835 | case ConstructionPhase::AfterBases: |
5836 | case ConstructionPhase::AfterFields: |
5837 | case ConstructionPhase::Destroying: |
5838 | // We've finished constructing the base classes and not yet started |
5839 | // destroying them again, so this is the dynamic type. |
5840 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
5841 | PathLength}; |
5842 | } |
5843 | } |
5844 | |
5845 | // CWG issue 1517: we're constructing a base class of the object described by |
5846 | // 'This', so that object has not yet begun its period of construction and |
5847 | // any polymorphic operation on it results in undefined behavior. |
5848 | Info.FFDiag(E); |
5849 | return std::nullopt; |
5850 | } |
5851 | |
5852 | /// Perform virtual dispatch. |
5853 | static const CXXMethodDecl *HandleVirtualDispatch( |
5854 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
5855 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
5856 | std::optional<DynamicType> DynType = ComputeDynamicType( |
5857 | Info, E, This, |
5858 | AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall); |
5859 | if (!DynType) |
5860 | return nullptr; |
5861 | |
5862 | // Find the final overrider. It must be declared in one of the classes on the |
5863 | // path from the dynamic type to the static type. |
5864 | // FIXME: If we ever allow literal types to have virtual base classes, that |
5865 | // won't be true. |
5866 | const CXXMethodDecl *Callee = Found; |
5867 | unsigned PathLength = DynType->PathLength; |
5868 | for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
5869 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
5870 | const CXXMethodDecl *Overrider = |
5871 | Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false); |
5872 | if (Overrider) { |
5873 | Callee = Overrider; |
5874 | break; |
5875 | } |
5876 | } |
5877 | |
5878 | // C++2a [class.abstract]p6: |
5879 | // the effect of making a virtual call to a pure virtual function [...] is |
5880 | // undefined |
5881 | if (Callee->isPureVirtual()) { |
5882 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
5883 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
5884 | return nullptr; |
5885 | } |
5886 | |
5887 | // If necessary, walk the rest of the path to determine the sequence of |
5888 | // covariant adjustment steps to apply. |
5889 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(), |
5890 | T2: Found->getReturnType())) { |
5891 | CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType()); |
5892 | for (unsigned CovariantPathLength = PathLength + 1; |
5893 | CovariantPathLength != This.Designator.Entries.size(); |
5894 | ++CovariantPathLength) { |
5895 | const CXXRecordDecl *NextClass = |
5896 | getBaseClassType(This.Designator, CovariantPathLength); |
5897 | const CXXMethodDecl *Next = |
5898 | Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false); |
5899 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
5900 | T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back())) |
5901 | CovariantAdjustmentPath.push_back(Elt: Next->getReturnType()); |
5902 | } |
5903 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(), |
5904 | T2: CovariantAdjustmentPath.back())) |
5905 | CovariantAdjustmentPath.push_back(Elt: Found->getReturnType()); |
5906 | } |
5907 | |
5908 | // Perform 'this' adjustment. |
5909 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
5910 | return nullptr; |
5911 | |
5912 | return Callee; |
5913 | } |
5914 | |
5915 | /// Perform the adjustment from a value returned by a virtual function to |
5916 | /// a value of the statically expected type, which may be a pointer or |
5917 | /// reference to a base class of the returned type. |
5918 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
5919 | APValue &Result, |
5920 | ArrayRef<QualType> Path) { |
5921 | assert(Result.isLValue() && |
5922 | "unexpected kind of APValue for covariant return" ); |
5923 | if (Result.isNullPointer()) |
5924 | return true; |
5925 | |
5926 | LValue LVal; |
5927 | LVal.setFrom(Ctx&: Info.Ctx, V: Result); |
5928 | |
5929 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
5930 | for (unsigned I = 1; I != Path.size(); ++I) { |
5931 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
5932 | assert(OldClass && NewClass && "unexpected kind of covariant return" ); |
5933 | if (OldClass != NewClass && |
5934 | !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass)) |
5935 | return false; |
5936 | OldClass = NewClass; |
5937 | } |
5938 | |
5939 | LVal.moveInto(V&: Result); |
5940 | return true; |
5941 | } |
5942 | |
5943 | /// Determine whether \p Base, which is known to be a direct base class of |
5944 | /// \p Derived, is a public base class. |
5945 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
5946 | const CXXRecordDecl *Base) { |
5947 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
5948 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
5949 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
5950 | return BaseSpec.getAccessSpecifier() == AS_public; |
5951 | } |
5952 | llvm_unreachable("Base is not a direct base of Derived" ); |
5953 | } |
5954 | |
5955 | /// Apply the given dynamic cast operation on the provided lvalue. |
5956 | /// |
5957 | /// This implements the hard case of dynamic_cast, requiring a "runtime check" |
5958 | /// to find a suitable target subobject. |
5959 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
5960 | LValue &Ptr) { |
5961 | // We can't do anything with a non-symbolic pointer value. |
5962 | SubobjectDesignator &D = Ptr.Designator; |
5963 | if (D.Invalid) |
5964 | return false; |
5965 | |
5966 | // C++ [expr.dynamic.cast]p6: |
5967 | // If v is a null pointer value, the result is a null pointer value. |
5968 | if (Ptr.isNullPointer() && !E->isGLValue()) |
5969 | return true; |
5970 | |
5971 | // For all the other cases, we need the pointer to point to an object within |
5972 | // its lifetime / period of construction / destruction, and we need to know |
5973 | // its dynamic type. |
5974 | std::optional<DynamicType> DynType = |
5975 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
5976 | if (!DynType) |
5977 | return false; |
5978 | |
5979 | // C++ [expr.dynamic.cast]p7: |
5980 | // If T is "pointer to cv void", then the result is a pointer to the most |
5981 | // derived object |
5982 | if (E->getType()->isVoidPointerType()) |
5983 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
5984 | |
5985 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
5986 | assert(C && "dynamic_cast target is not void pointer nor class" ); |
5987 | CanQualType CQT = Info.Ctx.getCanonicalType(T: Info.Ctx.getRecordType(C)); |
5988 | |
5989 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
5990 | // C++ [expr.dynamic.cast]p9: |
5991 | if (!E->isGLValue()) { |
5992 | // The value of a failed cast to pointer type is the null pointer value |
5993 | // of the required result type. |
5994 | Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
5995 | return true; |
5996 | } |
5997 | |
5998 | // A failed cast to reference type throws [...] std::bad_cast. |
5999 | unsigned DiagKind; |
6000 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
6001 | DynType->Type->isDerivedFrom(Base: C))) |
6002 | DiagKind = 0; |
6003 | else if (!Paths || Paths->begin() == Paths->end()) |
6004 | DiagKind = 1; |
6005 | else if (Paths->isAmbiguous(BaseType: CQT)) |
6006 | DiagKind = 2; |
6007 | else { |
6008 | assert(Paths->front().Access != AS_public && "why did the cast fail?" ); |
6009 | DiagKind = 3; |
6010 | } |
6011 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
6012 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
6013 | << Info.Ctx.getRecordType(DynType->Type) |
6014 | << E->getType().getUnqualifiedType(); |
6015 | return false; |
6016 | }; |
6017 | |
6018 | // Runtime check, phase 1: |
6019 | // Walk from the base subobject towards the derived object looking for the |
6020 | // target type. |
6021 | for (int PathLength = Ptr.Designator.Entries.size(); |
6022 | PathLength >= (int)DynType->PathLength; --PathLength) { |
6023 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
6024 | if (declaresSameEntity(Class, C)) |
6025 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
6026 | // We can only walk across public inheritance edges. |
6027 | if (PathLength > (int)DynType->PathLength && |
6028 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
6029 | Class)) |
6030 | return RuntimeCheckFailed(nullptr); |
6031 | } |
6032 | |
6033 | // Runtime check, phase 2: |
6034 | // Search the dynamic type for an unambiguous public base of type C. |
6035 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
6036 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
6037 | if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) && |
6038 | Paths.front().Access == AS_public) { |
6039 | // Downcast to the dynamic type... |
6040 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
6041 | return false; |
6042 | // ... then upcast to the chosen base class subobject. |
6043 | for (CXXBasePathElement &Elem : Paths.front()) |
6044 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
6045 | return false; |
6046 | return true; |
6047 | } |
6048 | |
6049 | // Otherwise, the runtime check fails. |
6050 | return RuntimeCheckFailed(&Paths); |
6051 | } |
6052 | |
6053 | namespace { |
6054 | struct StartLifetimeOfUnionMemberHandler { |
6055 | EvalInfo &Info; |
6056 | const Expr *LHSExpr; |
6057 | const FieldDecl *Field; |
6058 | bool DuringInit; |
6059 | bool Failed = false; |
6060 | static const AccessKinds AccessKind = AK_Assign; |
6061 | |
6062 | typedef bool result_type; |
6063 | bool failed() { return Failed; } |
6064 | bool found(APValue &Subobj, QualType SubobjType) { |
6065 | // We are supposed to perform no initialization but begin the lifetime of |
6066 | // the object. We interpret that as meaning to do what default |
6067 | // initialization of the object would do if all constructors involved were |
6068 | // trivial: |
6069 | // * All base, non-variant member, and array element subobjects' lifetimes |
6070 | // begin |
6071 | // * No variant members' lifetimes begin |
6072 | // * All scalar subobjects whose lifetimes begin have indeterminate values |
6073 | assert(SubobjType->isUnionType()); |
6074 | if (declaresSameEntity(Subobj.getUnionField(), Field)) { |
6075 | // This union member is already active. If it's also in-lifetime, there's |
6076 | // nothing to do. |
6077 | if (Subobj.getUnionValue().hasValue()) |
6078 | return true; |
6079 | } else if (DuringInit) { |
6080 | // We're currently in the process of initializing a different union |
6081 | // member. If we carried on, that initialization would attempt to |
6082 | // store to an inactive union member, resulting in undefined behavior. |
6083 | Info.FFDiag(LHSExpr, |
6084 | diag::note_constexpr_union_member_change_during_init); |
6085 | return false; |
6086 | } |
6087 | APValue Result; |
6088 | Failed = !handleDefaultInitValue(Field->getType(), Result); |
6089 | Subobj.setUnion(Field, Value: Result); |
6090 | return true; |
6091 | } |
6092 | bool found(APSInt &Value, QualType SubobjType) { |
6093 | llvm_unreachable("wrong value kind for union object" ); |
6094 | } |
6095 | bool found(APFloat &Value, QualType SubobjType) { |
6096 | llvm_unreachable("wrong value kind for union object" ); |
6097 | } |
6098 | }; |
6099 | } // end anonymous namespace |
6100 | |
6101 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
6102 | |
6103 | /// Handle a builtin simple-assignment or a call to a trivial assignment |
6104 | /// operator whose left-hand side might involve a union member access. If it |
6105 | /// does, implicitly start the lifetime of any accessed union elements per |
6106 | /// C++20 [class.union]5. |
6107 | static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info, |
6108 | const Expr *LHSExpr, |
6109 | const LValue &LHS) { |
6110 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
6111 | return false; |
6112 | |
6113 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
6114 | // C++ [class.union]p5: |
6115 | // define the set S(E) of subexpressions of E as follows: |
6116 | unsigned PathLength = LHS.Designator.Entries.size(); |
6117 | for (const Expr *E = LHSExpr; E != nullptr;) { |
6118 | // -- If E is of the form A.B, S(E) contains the elements of S(A)... |
6119 | if (auto *ME = dyn_cast<MemberExpr>(Val: E)) { |
6120 | auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl()); |
6121 | // Note that we can't implicitly start the lifetime of a reference, |
6122 | // so we don't need to proceed any further if we reach one. |
6123 | if (!FD || FD->getType()->isReferenceType()) |
6124 | break; |
6125 | |
6126 | // ... and also contains A.B if B names a union member ... |
6127 | if (FD->getParent()->isUnion()) { |
6128 | // ... of a non-class, non-array type, or of a class type with a |
6129 | // trivial default constructor that is not deleted, or an array of |
6130 | // such types. |
6131 | auto *RD = |
6132 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
6133 | if (!RD || RD->hasTrivialDefaultConstructor()) |
6134 | UnionPathLengths.push_back(Elt: {PathLength - 1, FD}); |
6135 | } |
6136 | |
6137 | E = ME->getBase(); |
6138 | --PathLength; |
6139 | assert(declaresSameEntity(FD, |
6140 | LHS.Designator.Entries[PathLength] |
6141 | .getAsBaseOrMember().getPointer())); |
6142 | |
6143 | // -- If E is of the form A[B] and is interpreted as a built-in array |
6144 | // subscripting operator, S(E) is [S(the array operand, if any)]. |
6145 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
6146 | // Step over an ArrayToPointerDecay implicit cast. |
6147 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
6148 | if (!Base->getType()->isArrayType()) |
6149 | break; |
6150 | |
6151 | E = Base; |
6152 | --PathLength; |
6153 | |
6154 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) { |
6155 | // Step over a derived-to-base conversion. |
6156 | E = ICE->getSubExpr(); |
6157 | if (ICE->getCastKind() == CK_NoOp) |
6158 | continue; |
6159 | if (ICE->getCastKind() != CK_DerivedToBase && |
6160 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
6161 | break; |
6162 | // Walk path backwards as we walk up from the base to the derived class. |
6163 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
6164 | if (Elt->isVirtual()) { |
6165 | // A class with virtual base classes never has a trivial default |
6166 | // constructor, so S(E) is empty in this case. |
6167 | E = nullptr; |
6168 | break; |
6169 | } |
6170 | |
6171 | --PathLength; |
6172 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
6173 | LHS.Designator.Entries[PathLength] |
6174 | .getAsBaseOrMember().getPointer())); |
6175 | } |
6176 | |
6177 | // -- Otherwise, S(E) is empty. |
6178 | } else { |
6179 | break; |
6180 | } |
6181 | } |
6182 | |
6183 | // Common case: no unions' lifetimes are started. |
6184 | if (UnionPathLengths.empty()) |
6185 | return true; |
6186 | |
6187 | // if modification of X [would access an inactive union member], an object |
6188 | // of the type of X is implicitly created |
6189 | CompleteObject Obj = |
6190 | findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType()); |
6191 | if (!Obj) |
6192 | return false; |
6193 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
6194 | llvm::reverse(C&: UnionPathLengths)) { |
6195 | // Form a designator for the union object. |
6196 | SubobjectDesignator D = LHS.Designator; |
6197 | D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first); |
6198 | |
6199 | bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) == |
6200 | ConstructionPhase::AfterBases; |
6201 | StartLifetimeOfUnionMemberHandler StartLifetime{ |
6202 | .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit}; |
6203 | if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime)) |
6204 | return false; |
6205 | } |
6206 | |
6207 | return true; |
6208 | } |
6209 | |
6210 | static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg, |
6211 | CallRef Call, EvalInfo &Info, |
6212 | bool NonNull = false) { |
6213 | LValue LV; |
6214 | // Create the parameter slot and register its destruction. For a vararg |
6215 | // argument, create a temporary. |
6216 | // FIXME: For calling conventions that destroy parameters in the callee, |
6217 | // should we consider performing destruction when the function returns |
6218 | // instead? |
6219 | APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV) |
6220 | : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(), |
6221 | Scope: ScopeKind::Call, LV); |
6222 | if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg)) |
6223 | return false; |
6224 | |
6225 | // Passing a null pointer to an __attribute__((nonnull)) parameter results in |
6226 | // undefined behavior, so is non-constant. |
6227 | if (NonNull && V.isLValue() && V.isNullPointer()) { |
6228 | Info.CCEDiag(Arg, diag::note_non_null_attribute_failed); |
6229 | return false; |
6230 | } |
6231 | |
6232 | return true; |
6233 | } |
6234 | |
6235 | /// Evaluate the arguments to a function call. |
6236 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call, |
6237 | EvalInfo &Info, const FunctionDecl *Callee, |
6238 | bool RightToLeft = false) { |
6239 | bool Success = true; |
6240 | llvm::SmallBitVector ForbiddenNullArgs; |
6241 | if (Callee->hasAttr<NonNullAttr>()) { |
6242 | ForbiddenNullArgs.resize(N: Args.size()); |
6243 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
6244 | if (!Attr->args_size()) { |
6245 | ForbiddenNullArgs.set(); |
6246 | break; |
6247 | } else |
6248 | for (auto Idx : Attr->args()) { |
6249 | unsigned ASTIdx = Idx.getASTIndex(); |
6250 | if (ASTIdx >= Args.size()) |
6251 | continue; |
6252 | ForbiddenNullArgs[ASTIdx] = true; |
6253 | } |
6254 | } |
6255 | } |
6256 | for (unsigned I = 0; I < Args.size(); I++) { |
6257 | unsigned Idx = RightToLeft ? Args.size() - I - 1 : I; |
6258 | const ParmVarDecl *PVD = |
6259 | Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr; |
6260 | bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx]; |
6261 | if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull)) { |
6262 | // If we're checking for a potential constant expression, evaluate all |
6263 | // initializers even if some of them fail. |
6264 | if (!Info.noteFailure()) |
6265 | return false; |
6266 | Success = false; |
6267 | } |
6268 | } |
6269 | return Success; |
6270 | } |
6271 | |
6272 | /// Perform a trivial copy from Param, which is the parameter of a copy or move |
6273 | /// constructor or assignment operator. |
6274 | static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param, |
6275 | const Expr *E, APValue &Result, |
6276 | bool CopyObjectRepresentation) { |
6277 | // Find the reference argument. |
6278 | CallStackFrame *Frame = Info.CurrentCall; |
6279 | APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param); |
6280 | if (!RefValue) { |
6281 | Info.FFDiag(E); |
6282 | return false; |
6283 | } |
6284 | |
6285 | // Copy out the contents of the RHS object. |
6286 | LValue RefLValue; |
6287 | RefLValue.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
6288 | return handleLValueToRValueConversion( |
6289 | Info, E, Param->getType().getNonReferenceType(), RefLValue, Result, |
6290 | CopyObjectRepresentation); |
6291 | } |
6292 | |
6293 | /// Evaluate a function call. |
6294 | static bool HandleFunctionCall(SourceLocation CallLoc, |
6295 | const FunctionDecl *Callee, const LValue *This, |
6296 | const Expr *E, ArrayRef<const Expr *> Args, |
6297 | CallRef Call, const Stmt *Body, EvalInfo &Info, |
6298 | APValue &Result, const LValue *ResultSlot) { |
6299 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6300 | return false; |
6301 | |
6302 | CallStackFrame Frame(Info, E->getSourceRange(), Callee, This, E, Call); |
6303 | |
6304 | // For a trivial copy or move assignment, perform an APValue copy. This is |
6305 | // essential for unions, where the operations performed by the assignment |
6306 | // operator cannot be represented as statements. |
6307 | // |
6308 | // Skip this for non-union classes with no fields; in that case, the defaulted |
6309 | // copy/move does not actually read the object. |
6310 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
6311 | if (MD && MD->isDefaulted() && |
6312 | (MD->getParent()->isUnion() || |
6313 | (MD->isTrivial() && |
6314 | isReadByLvalueToRvalueConversion(RD: MD->getParent())))) { |
6315 | assert(This && |
6316 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
6317 | APValue RHSValue; |
6318 | if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue, |
6319 | MD->getParent()->isUnion())) |
6320 | return false; |
6321 | if (!handleAssignment(Info, E: Args[0], LVal: *This, LValType: MD->getThisType(), |
6322 | Val&: RHSValue)) |
6323 | return false; |
6324 | This->moveInto(V&: Result); |
6325 | return true; |
6326 | } else if (MD && isLambdaCallOperator(MD)) { |
6327 | // We're in a lambda; determine the lambda capture field maps unless we're |
6328 | // just constexpr checking a lambda's call operator. constexpr checking is |
6329 | // done before the captures have been added to the closure object (unless |
6330 | // we're inferring constexpr-ness), so we don't have access to them in this |
6331 | // case. But since we don't need the captures to constexpr check, we can |
6332 | // just ignore them. |
6333 | if (!Info.checkingPotentialConstantExpression()) |
6334 | MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields, |
6335 | ThisCapture&: Frame.LambdaThisCaptureField); |
6336 | } |
6337 | |
6338 | StmtResult Ret = {.Value: Result, .Slot: ResultSlot}; |
6339 | EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body); |
6340 | if (ESR == ESR_Succeeded) { |
6341 | if (Callee->getReturnType()->isVoidType()) |
6342 | return true; |
6343 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
6344 | } |
6345 | return ESR == ESR_Returned; |
6346 | } |
6347 | |
6348 | /// Evaluate a constructor call. |
6349 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6350 | CallRef Call, |
6351 | const CXXConstructorDecl *Definition, |
6352 | EvalInfo &Info, APValue &Result) { |
6353 | SourceLocation CallLoc = E->getExprLoc(); |
6354 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6355 | return false; |
6356 | |
6357 | const CXXRecordDecl *RD = Definition->getParent(); |
6358 | if (RD->getNumVBases()) { |
6359 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6360 | return false; |
6361 | } |
6362 | |
6363 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
6364 | Info, |
6365 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
6366 | RD->getNumBases()); |
6367 | CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call); |
6368 | |
6369 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
6370 | // wasteful. |
6371 | APValue RetVal; |
6372 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
6373 | |
6374 | // If it's a delegating constructor, delegate. |
6375 | if (Definition->isDelegatingConstructor()) { |
6376 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
6377 | if ((*I)->getInit()->isValueDependent()) { |
6378 | if (!EvaluateDependentExpr(E: (*I)->getInit(), Info)) |
6379 | return false; |
6380 | } else { |
6381 | FullExpressionRAII InitScope(Info); |
6382 | if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) || |
6383 | !InitScope.destroy()) |
6384 | return false; |
6385 | } |
6386 | return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed; |
6387 | } |
6388 | |
6389 | // For a trivial copy or move constructor, perform an APValue copy. This is |
6390 | // essential for unions (or classes with anonymous union members), where the |
6391 | // operations performed by the constructor cannot be represented by |
6392 | // ctor-initializers. |
6393 | // |
6394 | // Skip this for empty non-union classes; we should not perform an |
6395 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
6396 | // actually read them. |
6397 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
6398 | (Definition->getParent()->isUnion() || |
6399 | (Definition->isTrivial() && |
6400 | isReadByLvalueToRvalueConversion(Definition->getParent())))) { |
6401 | return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result, |
6402 | Definition->getParent()->isUnion()); |
6403 | } |
6404 | |
6405 | // Reserve space for the struct members. |
6406 | if (!Result.hasValue()) { |
6407 | if (!RD->isUnion()) |
6408 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
6409 | std::distance(RD->field_begin(), RD->field_end())); |
6410 | else |
6411 | // A union starts with no active member. |
6412 | Result = APValue((const FieldDecl*)nullptr); |
6413 | } |
6414 | |
6415 | if (RD->isInvalidDecl()) return false; |
6416 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6417 | |
6418 | // A scope for temporaries lifetime-extended by reference members. |
6419 | BlockScopeRAII LifetimeExtendedScope(Info); |
6420 | |
6421 | bool Success = true; |
6422 | unsigned BasesSeen = 0; |
6423 | #ifndef NDEBUG |
6424 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
6425 | #endif |
6426 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
6427 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
6428 | // We might be initializing the same field again if this is an indirect |
6429 | // field initialization. |
6430 | if (FieldIt == RD->field_end() || |
6431 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
6432 | assert(Indirect && "fields out of order?" ); |
6433 | return; |
6434 | } |
6435 | |
6436 | // Default-initialize any fields with no explicit initializer. |
6437 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
6438 | assert(FieldIt != RD->field_end() && "missing field?" ); |
6439 | if (!FieldIt->isUnnamedBitField()) |
6440 | Success &= handleDefaultInitValue( |
6441 | FieldIt->getType(), |
6442 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6443 | } |
6444 | ++FieldIt; |
6445 | }; |
6446 | for (const auto *I : Definition->inits()) { |
6447 | LValue Subobject = This; |
6448 | LValue SubobjectParent = This; |
6449 | APValue *Value = &Result; |
6450 | |
6451 | // Determine the subobject to initialize. |
6452 | FieldDecl *FD = nullptr; |
6453 | if (I->isBaseInitializer()) { |
6454 | QualType BaseType(I->getBaseClass(), 0); |
6455 | #ifndef NDEBUG |
6456 | // Non-virtual base classes are initialized in the order in the class |
6457 | // definition. We have already checked for virtual base classes. |
6458 | assert(!BaseIt->isVirtual() && "virtual base for literal type" ); |
6459 | assert(Info.Ctx.hasSameUnqualifiedType(BaseIt->getType(), BaseType) && |
6460 | "base class initializers not in expected order" ); |
6461 | ++BaseIt; |
6462 | #endif |
6463 | if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD, |
6464 | Base: BaseType->getAsCXXRecordDecl(), RL: &Layout)) |
6465 | return false; |
6466 | Value = &Result.getStructBase(i: BasesSeen++); |
6467 | } else if ((FD = I->getMember())) { |
6468 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout)) |
6469 | return false; |
6470 | if (RD->isUnion()) { |
6471 | Result = APValue(FD); |
6472 | Value = &Result.getUnionValue(); |
6473 | } else { |
6474 | SkipToField(FD, false); |
6475 | Value = &Result.getStructField(i: FD->getFieldIndex()); |
6476 | } |
6477 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
6478 | // Walk the indirect field decl's chain to find the object to initialize, |
6479 | // and make sure we've initialized every step along it. |
6480 | auto IndirectFieldChain = IFD->chain(); |
6481 | for (auto *C : IndirectFieldChain) { |
6482 | FD = cast<FieldDecl>(Val: C); |
6483 | CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent()); |
6484 | // Switch the union field if it differs. This happens if we had |
6485 | // preceding zero-initialization, and we're now initializing a union |
6486 | // subobject other than the first. |
6487 | // FIXME: In this case, the values of the other subobjects are |
6488 | // specified, since zero-initialization sets all padding bits to zero. |
6489 | if (!Value->hasValue() || |
6490 | (Value->isUnion() && Value->getUnionField() != FD)) { |
6491 | if (CD->isUnion()) |
6492 | *Value = APValue(FD); |
6493 | else |
6494 | // FIXME: This immediately starts the lifetime of all members of |
6495 | // an anonymous struct. It would be preferable to strictly start |
6496 | // member lifetime in initialization order. |
6497 | Success &= |
6498 | handleDefaultInitValue(T: Info.Ctx.getRecordType(CD), Result&: *Value); |
6499 | } |
6500 | // Store Subobject as its parent before updating it for the last element |
6501 | // in the chain. |
6502 | if (C == IndirectFieldChain.back()) |
6503 | SubobjectParent = Subobject; |
6504 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD)) |
6505 | return false; |
6506 | if (CD->isUnion()) |
6507 | Value = &Value->getUnionValue(); |
6508 | else { |
6509 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
6510 | SkipToField(FD, true); |
6511 | Value = &Value->getStructField(i: FD->getFieldIndex()); |
6512 | } |
6513 | } |
6514 | } else { |
6515 | llvm_unreachable("unknown base initializer kind" ); |
6516 | } |
6517 | |
6518 | // Need to override This for implicit field initializers as in this case |
6519 | // This refers to innermost anonymous struct/union containing initializer, |
6520 | // not to currently constructed class. |
6521 | const Expr *Init = I->getInit(); |
6522 | if (Init->isValueDependent()) { |
6523 | if (!EvaluateDependentExpr(E: Init, Info)) |
6524 | return false; |
6525 | } else { |
6526 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
6527 | isa<CXXDefaultInitExpr>(Val: Init)); |
6528 | FullExpressionRAII InitScope(Info); |
6529 | if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) || |
6530 | (FD && FD->isBitField() && |
6531 | !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) { |
6532 | // If we're checking for a potential constant expression, evaluate all |
6533 | // initializers even if some of them fail. |
6534 | if (!Info.noteFailure()) |
6535 | return false; |
6536 | Success = false; |
6537 | } |
6538 | } |
6539 | |
6540 | // This is the point at which the dynamic type of the object becomes this |
6541 | // class type. |
6542 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
6543 | EvalObj.finishedConstructingBases(); |
6544 | } |
6545 | |
6546 | // Default-initialize any remaining fields. |
6547 | if (!RD->isUnion()) { |
6548 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
6549 | if (!FieldIt->isUnnamedBitField()) |
6550 | Success &= handleDefaultInitValue( |
6551 | FieldIt->getType(), |
6552 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6553 | } |
6554 | } |
6555 | |
6556 | EvalObj.finishedConstructingFields(); |
6557 | |
6558 | return Success && |
6559 | EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed && |
6560 | LifetimeExtendedScope.destroy(); |
6561 | } |
6562 | |
6563 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6564 | ArrayRef<const Expr*> Args, |
6565 | const CXXConstructorDecl *Definition, |
6566 | EvalInfo &Info, APValue &Result) { |
6567 | CallScopeRAII CallScope(Info); |
6568 | CallRef Call = Info.CurrentCall->createCall(Definition); |
6569 | if (!EvaluateArgs(Args, Call, Info, Definition)) |
6570 | return false; |
6571 | |
6572 | return HandleConstructorCall(E, This, Call, Definition, Info, Result) && |
6573 | CallScope.destroy(); |
6574 | } |
6575 | |
6576 | static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange, |
6577 | const LValue &This, APValue &Value, |
6578 | QualType T) { |
6579 | // Objects can only be destroyed while they're within their lifetimes. |
6580 | // FIXME: We have no representation for whether an object of type nullptr_t |
6581 | // is in its lifetime; it usually doesn't matter. Perhaps we should model it |
6582 | // as indeterminate instead? |
6583 | if (Value.isAbsent() && !T->isNullPtrType()) { |
6584 | APValue Printable; |
6585 | This.moveInto(V&: Printable); |
6586 | Info.FFDiag(CallRange.getBegin(), |
6587 | diag::note_constexpr_destroy_out_of_lifetime) |
6588 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
6589 | return false; |
6590 | } |
6591 | |
6592 | // Invent an expression for location purposes. |
6593 | // FIXME: We shouldn't need to do this. |
6594 | OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue); |
6595 | |
6596 | // For arrays, destroy elements right-to-left. |
6597 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
6598 | uint64_t Size = CAT->getZExtSize(); |
6599 | QualType ElemT = CAT->getElementType(); |
6600 | |
6601 | if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin())) |
6602 | return false; |
6603 | |
6604 | LValue ElemLV = This; |
6605 | ElemLV.addArray(Info, &LocE, CAT); |
6606 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
6607 | return false; |
6608 | |
6609 | // Ensure that we have actual array elements available to destroy; the |
6610 | // destructors might mutate the value, so we can't run them on the array |
6611 | // filler. |
6612 | if (Size && Size > Value.getArrayInitializedElts()) |
6613 | expandArray(Array&: Value, Index: Value.getArraySize() - 1); |
6614 | |
6615 | for (; Size != 0; --Size) { |
6616 | APValue &Elem = Value.getArrayInitializedElt(I: Size - 1); |
6617 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
6618 | !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT)) |
6619 | return false; |
6620 | } |
6621 | |
6622 | // End the lifetime of this array now. |
6623 | Value = APValue(); |
6624 | return true; |
6625 | } |
6626 | |
6627 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
6628 | if (!RD) { |
6629 | if (T.isDestructedType()) { |
6630 | Info.FFDiag(CallRange.getBegin(), |
6631 | diag::note_constexpr_unsupported_destruction) |
6632 | << T; |
6633 | return false; |
6634 | } |
6635 | |
6636 | Value = APValue(); |
6637 | return true; |
6638 | } |
6639 | |
6640 | if (RD->getNumVBases()) { |
6641 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_virtual_base) << RD; |
6642 | return false; |
6643 | } |
6644 | |
6645 | const CXXDestructorDecl *DD = RD->getDestructor(); |
6646 | if (!DD && !RD->hasTrivialDestructor()) { |
6647 | Info.FFDiag(Loc: CallRange.getBegin()); |
6648 | return false; |
6649 | } |
6650 | |
6651 | if (!DD || DD->isTrivial() || |
6652 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
6653 | // A trivial destructor just ends the lifetime of the object. Check for |
6654 | // this case before checking for a body, because we might not bother |
6655 | // building a body for a trivial destructor. Note that it doesn't matter |
6656 | // whether the destructor is constexpr in this case; all trivial |
6657 | // destructors are constexpr. |
6658 | // |
6659 | // If an anonymous union would be destroyed, some enclosing destructor must |
6660 | // have been explicitly defined, and the anonymous union destruction should |
6661 | // have no effect. |
6662 | Value = APValue(); |
6663 | return true; |
6664 | } |
6665 | |
6666 | if (!Info.CheckCallLimit(Loc: CallRange.getBegin())) |
6667 | return false; |
6668 | |
6669 | const FunctionDecl *Definition = nullptr; |
6670 | const Stmt *Body = DD->getBody(Definition); |
6671 | |
6672 | if (!CheckConstexprFunction(Info, CallRange.getBegin(), DD, Definition, Body)) |
6673 | return false; |
6674 | |
6675 | CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr, |
6676 | CallRef()); |
6677 | |
6678 | // We're now in the period of destruction of this object. |
6679 | unsigned BasesLeft = RD->getNumBases(); |
6680 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
6681 | Info, |
6682 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
6683 | if (!EvalObj.DidInsert) { |
6684 | // C++2a [class.dtor]p19: |
6685 | // the behavior is undefined if the destructor is invoked for an object |
6686 | // whose lifetime has ended |
6687 | // (Note that formally the lifetime ends when the period of destruction |
6688 | // begins, even though certain uses of the object remain valid until the |
6689 | // period of destruction ends.) |
6690 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_double_destroy); |
6691 | return false; |
6692 | } |
6693 | |
6694 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
6695 | // wasteful. |
6696 | APValue RetVal; |
6697 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
6698 | if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed) |
6699 | return false; |
6700 | |
6701 | // A union destructor does not implicitly destroy its members. |
6702 | if (RD->isUnion()) |
6703 | return true; |
6704 | |
6705 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6706 | |
6707 | // We don't have a good way to iterate fields in reverse, so collect all the |
6708 | // fields first and then walk them backwards. |
6709 | SmallVector<FieldDecl*, 16> Fields(RD->fields()); |
6710 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
6711 | if (FD->isUnnamedBitField()) |
6712 | continue; |
6713 | |
6714 | LValue Subobject = This; |
6715 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
6716 | return false; |
6717 | |
6718 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
6719 | if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue, |
6720 | FD->getType())) |
6721 | return false; |
6722 | } |
6723 | |
6724 | if (BasesLeft != 0) |
6725 | EvalObj.startedDestroyingBases(); |
6726 | |
6727 | // Destroy base classes in reverse order. |
6728 | for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) { |
6729 | --BasesLeft; |
6730 | |
6731 | QualType BaseType = Base.getType(); |
6732 | LValue Subobject = This; |
6733 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
6734 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6735 | return false; |
6736 | |
6737 | APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft); |
6738 | if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue, |
6739 | T: BaseType)) |
6740 | return false; |
6741 | } |
6742 | assert(BasesLeft == 0 && "NumBases was wrong?" ); |
6743 | |
6744 | // The period of destruction ends now. The object is gone. |
6745 | Value = APValue(); |
6746 | return true; |
6747 | } |
6748 | |
6749 | namespace { |
6750 | struct DestroyObjectHandler { |
6751 | EvalInfo &Info; |
6752 | const Expr *E; |
6753 | const LValue &This; |
6754 | const AccessKinds AccessKind; |
6755 | |
6756 | typedef bool result_type; |
6757 | bool failed() { return false; } |
6758 | bool found(APValue &Subobj, QualType SubobjType) { |
6759 | return HandleDestructionImpl(Info, E->getSourceRange(), This, Subobj, |
6760 | SubobjType); |
6761 | } |
6762 | bool found(APSInt &Value, QualType SubobjType) { |
6763 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6764 | return false; |
6765 | } |
6766 | bool found(APFloat &Value, QualType SubobjType) { |
6767 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6768 | return false; |
6769 | } |
6770 | }; |
6771 | } |
6772 | |
6773 | /// Perform a destructor or pseudo-destructor call on the given object, which |
6774 | /// might in general not be a complete object. |
6775 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
6776 | const LValue &This, QualType ThisType) { |
6777 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType); |
6778 | DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy}; |
6779 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
6780 | } |
6781 | |
6782 | /// Destroy and end the lifetime of the given complete object. |
6783 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
6784 | APValue::LValueBase LVBase, APValue &Value, |
6785 | QualType T) { |
6786 | // If we've had an unmodeled side-effect, we can't rely on mutable state |
6787 | // (such as the object we're about to destroy) being correct. |
6788 | if (Info.EvalStatus.HasSideEffects) |
6789 | return false; |
6790 | |
6791 | LValue LV; |
6792 | LV.set(B: {LVBase}); |
6793 | return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T); |
6794 | } |
6795 | |
6796 | /// Perform a call to 'operator new' or to `__builtin_operator_new'. |
6797 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
6798 | LValue &Result) { |
6799 | if (Info.checkingPotentialConstantExpression() || |
6800 | Info.SpeculativeEvaluationDepth) |
6801 | return false; |
6802 | |
6803 | // This is permitted only within a call to std::allocator<T>::allocate. |
6804 | auto Caller = Info.getStdAllocatorCaller(FnName: "allocate" ); |
6805 | if (!Caller) { |
6806 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus20 |
6807 | ? diag::note_constexpr_new_untyped |
6808 | : diag::note_constexpr_new); |
6809 | return false; |
6810 | } |
6811 | |
6812 | QualType ElemType = Caller.ElemType; |
6813 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
6814 | Info.FFDiag(E->getExprLoc(), |
6815 | diag::note_constexpr_new_not_complete_object_type) |
6816 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
6817 | return false; |
6818 | } |
6819 | |
6820 | APSInt ByteSize; |
6821 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info)) |
6822 | return false; |
6823 | bool IsNothrow = false; |
6824 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
6825 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
6826 | IsNothrow |= E->getType()->isNothrowT(); |
6827 | } |
6828 | |
6829 | CharUnits ElemSize; |
6830 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
6831 | return false; |
6832 | APInt Size, Remainder; |
6833 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
6834 | APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder); |
6835 | if (Remainder != 0) { |
6836 | // This likely indicates a bug in the implementation of 'std::allocator'. |
6837 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
6838 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
6839 | return false; |
6840 | } |
6841 | |
6842 | if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(), |
6843 | ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) { |
6844 | if (IsNothrow) { |
6845 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
6846 | return true; |
6847 | } |
6848 | return false; |
6849 | } |
6850 | |
6851 | QualType AllocType = Info.Ctx.getConstantArrayType( |
6852 | EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
6853 | APValue *Val = Info.createHeapAlloc(E, AllocType, Result); |
6854 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
6855 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: AllocType)); |
6856 | return true; |
6857 | } |
6858 | |
6859 | static bool hasVirtualDestructor(QualType T) { |
6860 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6861 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6862 | return DD->isVirtual(); |
6863 | return false; |
6864 | } |
6865 | |
6866 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
6867 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6868 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6869 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
6870 | return nullptr; |
6871 | } |
6872 | |
6873 | /// Check that the given object is a suitable pointer to a heap allocation that |
6874 | /// still exists and is of the right kind for the purpose of a deletion. |
6875 | /// |
6876 | /// On success, returns the heap allocation to deallocate. On failure, produces |
6877 | /// a diagnostic and returns std::nullopt. |
6878 | static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
6879 | const LValue &Pointer, |
6880 | DynAlloc::Kind DeallocKind) { |
6881 | auto PointerAsString = [&] { |
6882 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
6883 | }; |
6884 | |
6885 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
6886 | if (!DA) { |
6887 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
6888 | << PointerAsString(); |
6889 | if (Pointer.Base) |
6890 | NoteLValueLocation(Info, Base: Pointer.Base); |
6891 | return std::nullopt; |
6892 | } |
6893 | |
6894 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
6895 | if (!Alloc) { |
6896 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
6897 | return std::nullopt; |
6898 | } |
6899 | |
6900 | if (DeallocKind != (*Alloc)->getKind()) { |
6901 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
6902 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
6903 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
6904 | NoteLValueLocation(Info, Base: Pointer.Base); |
6905 | return std::nullopt; |
6906 | } |
6907 | |
6908 | bool Subobject = false; |
6909 | if (DeallocKind == DynAlloc::New) { |
6910 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
6911 | Pointer.Designator.isOnePastTheEnd(); |
6912 | } else { |
6913 | Subobject = Pointer.Designator.Entries.size() != 1 || |
6914 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
6915 | } |
6916 | if (Subobject) { |
6917 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
6918 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
6919 | return std::nullopt; |
6920 | } |
6921 | |
6922 | return Alloc; |
6923 | } |
6924 | |
6925 | // Perform a call to 'operator delete' or '__builtin_operator_delete'. |
6926 | bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
6927 | if (Info.checkingPotentialConstantExpression() || |
6928 | Info.SpeculativeEvaluationDepth) |
6929 | return false; |
6930 | |
6931 | // This is permitted only within a call to std::allocator<T>::deallocate. |
6932 | if (!Info.getStdAllocatorCaller(FnName: "deallocate" )) { |
6933 | Info.FFDiag(E->getExprLoc()); |
6934 | return true; |
6935 | } |
6936 | |
6937 | LValue Pointer; |
6938 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info)) |
6939 | return false; |
6940 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
6941 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
6942 | |
6943 | if (Pointer.Designator.Invalid) |
6944 | return false; |
6945 | |
6946 | // Deleting a null pointer would have no effect, but it's not permitted by |
6947 | // std::allocator<T>::deallocate's contract. |
6948 | if (Pointer.isNullPointer()) { |
6949 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null); |
6950 | return true; |
6951 | } |
6952 | |
6953 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
6954 | return false; |
6955 | |
6956 | Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>()); |
6957 | return true; |
6958 | } |
6959 | |
6960 | //===----------------------------------------------------------------------===// |
6961 | // Generic Evaluation |
6962 | //===----------------------------------------------------------------------===// |
6963 | namespace { |
6964 | |
6965 | class BitCastBuffer { |
6966 | // FIXME: We're going to need bit-level granularity when we support |
6967 | // bit-fields. |
6968 | // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but |
6969 | // we don't support a host or target where that is the case. Still, we should |
6970 | // use a more generic type in case we ever do. |
6971 | SmallVector<std::optional<unsigned char>, 32> Bytes; |
6972 | |
6973 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
6974 | "Need at least 8 bit unsigned char" ); |
6975 | |
6976 | bool TargetIsLittleEndian; |
6977 | |
6978 | public: |
6979 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
6980 | : Bytes(Width.getQuantity()), |
6981 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
6982 | |
6983 | [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width, |
6984 | SmallVectorImpl<unsigned char> &Output) const { |
6985 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
6986 | // If a byte of an integer is uninitialized, then the whole integer is |
6987 | // uninitialized. |
6988 | if (!Bytes[I.getQuantity()]) |
6989 | return false; |
6990 | Output.push_back(Elt: *Bytes[I.getQuantity()]); |
6991 | } |
6992 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6993 | std::reverse(first: Output.begin(), last: Output.end()); |
6994 | return true; |
6995 | } |
6996 | |
6997 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
6998 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6999 | std::reverse(first: Input.begin(), last: Input.end()); |
7000 | |
7001 | size_t Index = 0; |
7002 | for (unsigned char Byte : Input) { |
7003 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?" ); |
7004 | Bytes[Offset.getQuantity() + Index] = Byte; |
7005 | ++Index; |
7006 | } |
7007 | } |
7008 | |
7009 | size_t size() { return Bytes.size(); } |
7010 | }; |
7011 | |
7012 | /// Traverse an APValue to produce an BitCastBuffer, emulating how the current |
7013 | /// target would represent the value at runtime. |
7014 | class APValueToBufferConverter { |
7015 | EvalInfo &Info; |
7016 | BitCastBuffer Buffer; |
7017 | const CastExpr *BCE; |
7018 | |
7019 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
7020 | const CastExpr *BCE) |
7021 | : Info(Info), |
7022 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
7023 | BCE(BCE) {} |
7024 | |
7025 | bool visit(const APValue &Val, QualType Ty) { |
7026 | return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0)); |
7027 | } |
7028 | |
7029 | // Write out Val with type Ty into Buffer starting at Offset. |
7030 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
7031 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
7032 | |
7033 | // As a special case, nullptr_t has an indeterminate value. |
7034 | if (Ty->isNullPtrType()) |
7035 | return true; |
7036 | |
7037 | // Dig through Src to find the byte at SrcOffset. |
7038 | switch (Val.getKind()) { |
7039 | case APValue::Indeterminate: |
7040 | case APValue::None: |
7041 | return true; |
7042 | |
7043 | case APValue::Int: |
7044 | return visitInt(Val: Val.getInt(), Ty, Offset); |
7045 | case APValue::Float: |
7046 | return visitFloat(Val: Val.getFloat(), Ty, Offset); |
7047 | case APValue::Array: |
7048 | return visitArray(Val, Ty, Offset); |
7049 | case APValue::Struct: |
7050 | return visitRecord(Val, Ty, Offset); |
7051 | case APValue::Vector: |
7052 | return visitVector(Val, Ty, Offset); |
7053 | |
7054 | case APValue::ComplexInt: |
7055 | case APValue::ComplexFloat: |
7056 | case APValue::FixedPoint: |
7057 | // FIXME: We should support these. |
7058 | |
7059 | case APValue::Union: |
7060 | case APValue::MemberPointer: |
7061 | case APValue::AddrLabelDiff: { |
7062 | Info.FFDiag(BCE->getBeginLoc(), |
7063 | diag::note_constexpr_bit_cast_unsupported_type) |
7064 | << Ty; |
7065 | return false; |
7066 | } |
7067 | |
7068 | case APValue::LValue: |
7069 | llvm_unreachable("LValue subobject in bit_cast?" ); |
7070 | } |
7071 | llvm_unreachable("Unhandled APValue::ValueKind" ); |
7072 | } |
7073 | |
7074 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
7075 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
7076 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7077 | |
7078 | // Visit the base classes. |
7079 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
7080 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7081 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7082 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7083 | |
7084 | if (!visitRecord(Val: Val.getStructBase(i: I), Ty: BS.getType(), |
7085 | Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset)) |
7086 | return false; |
7087 | } |
7088 | } |
7089 | |
7090 | // Visit the fields. |
7091 | unsigned FieldIdx = 0; |
7092 | for (FieldDecl *FD : RD->fields()) { |
7093 | if (FD->isBitField()) { |
7094 | Info.FFDiag(BCE->getBeginLoc(), |
7095 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7096 | return false; |
7097 | } |
7098 | |
7099 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx); |
7100 | |
7101 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
7102 | "only bit-fields can have sub-char alignment" ); |
7103 | CharUnits FieldOffset = |
7104 | Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset; |
7105 | QualType FieldTy = FD->getType(); |
7106 | if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset)) |
7107 | return false; |
7108 | ++FieldIdx; |
7109 | } |
7110 | |
7111 | return true; |
7112 | } |
7113 | |
7114 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
7115 | const auto *CAT = |
7116 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
7117 | if (!CAT) |
7118 | return false; |
7119 | |
7120 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
7121 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
7122 | unsigned ArraySize = Val.getArraySize(); |
7123 | // First, initialize the initialized elements. |
7124 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
7125 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
7126 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
7127 | return false; |
7128 | } |
7129 | |
7130 | // Next, initialize the rest of the array using the filler. |
7131 | if (Val.hasArrayFiller()) { |
7132 | const APValue &Filler = Val.getArrayFiller(); |
7133 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
7134 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
7135 | return false; |
7136 | } |
7137 | } |
7138 | |
7139 | return true; |
7140 | } |
7141 | |
7142 | bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) { |
7143 | const VectorType *VTy = Ty->castAs<VectorType>(); |
7144 | QualType EltTy = VTy->getElementType(); |
7145 | unsigned NElts = VTy->getNumElements(); |
7146 | unsigned EltSize = |
7147 | VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(T: EltTy); |
7148 | |
7149 | if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) { |
7150 | // The vector's size in bits is not a multiple of the target's byte size, |
7151 | // so its layout is unspecified. For now, we'll simply treat these cases |
7152 | // as unsupported (this should only be possible with OpenCL bool vectors |
7153 | // whose element count isn't a multiple of the byte size). |
7154 | Info.FFDiag(BCE->getBeginLoc(), |
7155 | diag::note_constexpr_bit_cast_invalid_vector) |
7156 | << Ty.getCanonicalType() << EltSize << NElts |
7157 | << Info.Ctx.getCharWidth(); |
7158 | return false; |
7159 | } |
7160 | |
7161 | if (EltTy->isRealFloatingType() && &Info.Ctx.getFloatTypeSemantics(T: EltTy) == |
7162 | &APFloat::x87DoubleExtended()) { |
7163 | // The layout for x86_fp80 vectors seems to be handled very inconsistently |
7164 | // by both clang and LLVM, so for now we won't allow bit_casts involving |
7165 | // it in a constexpr context. |
7166 | Info.FFDiag(BCE->getBeginLoc(), |
7167 | diag::note_constexpr_bit_cast_unsupported_type) |
7168 | << EltTy; |
7169 | return false; |
7170 | } |
7171 | |
7172 | if (VTy->isExtVectorBoolType()) { |
7173 | // Special handling for OpenCL bool vectors: |
7174 | // Since these vectors are stored as packed bits, but we can't write |
7175 | // individual bits to the BitCastBuffer, we'll buffer all of the elements |
7176 | // together into an appropriately sized APInt and write them all out at |
7177 | // once. Because we don't accept vectors where NElts * EltSize isn't a |
7178 | // multiple of the char size, there will be no padding space, so we don't |
7179 | // have to worry about writing data which should have been left |
7180 | // uninitialized. |
7181 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7182 | |
7183 | llvm::APInt Res = llvm::APInt::getZero(numBits: NElts); |
7184 | for (unsigned I = 0; I < NElts; ++I) { |
7185 | const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt(); |
7186 | assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 && |
7187 | "bool vector element must be 1-bit unsigned integer!" ); |
7188 | |
7189 | Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I); |
7190 | } |
7191 | |
7192 | SmallVector<uint8_t, 8> Bytes(NElts / 8); |
7193 | llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8); |
7194 | Buffer.writeObject(Offset, Input&: Bytes); |
7195 | } else { |
7196 | // Iterate over each of the elements and write them out to the buffer at |
7197 | // the appropriate offset. |
7198 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7199 | for (unsigned I = 0; I < NElts; ++I) { |
7200 | if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars)) |
7201 | return false; |
7202 | } |
7203 | } |
7204 | |
7205 | return true; |
7206 | } |
7207 | |
7208 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
7209 | APSInt AdjustedVal = Val; |
7210 | unsigned Width = AdjustedVal.getBitWidth(); |
7211 | if (Ty->isBooleanType()) { |
7212 | Width = Info.Ctx.getTypeSize(T: Ty); |
7213 | AdjustedVal = AdjustedVal.extend(width: Width); |
7214 | } |
7215 | |
7216 | SmallVector<uint8_t, 8> Bytes(Width / 8); |
7217 | llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8); |
7218 | Buffer.writeObject(Offset, Input&: Bytes); |
7219 | return true; |
7220 | } |
7221 | |
7222 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
7223 | APSInt AsInt(Val.bitcastToAPInt()); |
7224 | return visitInt(Val: AsInt, Ty, Offset); |
7225 | } |
7226 | |
7227 | public: |
7228 | static std::optional<BitCastBuffer> |
7229 | convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) { |
7230 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
7231 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
7232 | if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType())) |
7233 | return std::nullopt; |
7234 | return Converter.Buffer; |
7235 | } |
7236 | }; |
7237 | |
7238 | /// Write an BitCastBuffer into an APValue. |
7239 | class BufferToAPValueConverter { |
7240 | EvalInfo &Info; |
7241 | const BitCastBuffer &Buffer; |
7242 | const CastExpr *BCE; |
7243 | |
7244 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
7245 | const CastExpr *BCE) |
7246 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
7247 | |
7248 | // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast |
7249 | // with an invalid type, so anything left is a deficiency on our part (FIXME). |
7250 | // Ideally this will be unreachable. |
7251 | std::nullopt_t unsupportedType(QualType Ty) { |
7252 | Info.FFDiag(BCE->getBeginLoc(), |
7253 | diag::note_constexpr_bit_cast_unsupported_type) |
7254 | << Ty; |
7255 | return std::nullopt; |
7256 | } |
7257 | |
7258 | std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) { |
7259 | Info.FFDiag(BCE->getBeginLoc(), |
7260 | diag::note_constexpr_bit_cast_unrepresentable_value) |
7261 | << Ty << toString(Val, /*Radix=*/10); |
7262 | return std::nullopt; |
7263 | } |
7264 | |
7265 | std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
7266 | const EnumType *EnumSugar = nullptr) { |
7267 | if (T->isNullPtrType()) { |
7268 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0)); |
7269 | return APValue((Expr *)nullptr, |
7270 | /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue), |
7271 | APValue::NoLValuePath{}, /*IsNullPtr=*/true); |
7272 | } |
7273 | |
7274 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
7275 | |
7276 | // Work around floating point types that contain unused padding bytes. This |
7277 | // is really just `long double` on x86, which is the only fundamental type |
7278 | // with padding bytes. |
7279 | if (T->isRealFloatingType()) { |
7280 | const llvm::fltSemantics &Semantics = |
7281 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7282 | unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics); |
7283 | assert(NumBits % 8 == 0); |
7284 | CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8); |
7285 | if (NumBytes != SizeOf) |
7286 | SizeOf = NumBytes; |
7287 | } |
7288 | |
7289 | SmallVector<uint8_t, 8> Bytes; |
7290 | if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) { |
7291 | // If this is std::byte or unsigned char, then its okay to store an |
7292 | // indeterminate value. |
7293 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
7294 | bool IsUChar = |
7295 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
7296 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
7297 | if (!IsStdByte && !IsUChar) { |
7298 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
7299 | Info.FFDiag(BCE->getExprLoc(), |
7300 | diag::note_constexpr_bit_cast_indet_dest) |
7301 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
7302 | return std::nullopt; |
7303 | } |
7304 | |
7305 | return APValue::IndeterminateValue(); |
7306 | } |
7307 | |
7308 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
7309 | llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7310 | |
7311 | if (T->isIntegralOrEnumerationType()) { |
7312 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
7313 | |
7314 | unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0)); |
7315 | if (IntWidth != Val.getBitWidth()) { |
7316 | APSInt Truncated = Val.trunc(width: IntWidth); |
7317 | if (Truncated.extend(width: Val.getBitWidth()) != Val) |
7318 | return unrepresentableValue(Ty: QualType(T, 0), Val); |
7319 | Val = Truncated; |
7320 | } |
7321 | |
7322 | return APValue(Val); |
7323 | } |
7324 | |
7325 | if (T->isRealFloatingType()) { |
7326 | const llvm::fltSemantics &Semantics = |
7327 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7328 | return APValue(APFloat(Semantics, Val)); |
7329 | } |
7330 | |
7331 | return unsupportedType(Ty: QualType(T, 0)); |
7332 | } |
7333 | |
7334 | std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
7335 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
7336 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7337 | |
7338 | unsigned NumBases = 0; |
7339 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
7340 | NumBases = CXXRD->getNumBases(); |
7341 | |
7342 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
7343 | std::distance(RD->field_begin(), RD->field_end())); |
7344 | |
7345 | // Visit the base classes. |
7346 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
7347 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7348 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7349 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7350 | |
7351 | std::optional<APValue> SubObj = visitType( |
7352 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
7353 | if (!SubObj) |
7354 | return std::nullopt; |
7355 | ResultVal.getStructBase(i: I) = *SubObj; |
7356 | } |
7357 | } |
7358 | |
7359 | // Visit the fields. |
7360 | unsigned FieldIdx = 0; |
7361 | for (FieldDecl *FD : RD->fields()) { |
7362 | // FIXME: We don't currently support bit-fields. A lot of the logic for |
7363 | // this is in CodeGen, so we need to factor it around. |
7364 | if (FD->isBitField()) { |
7365 | Info.FFDiag(BCE->getBeginLoc(), |
7366 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7367 | return std::nullopt; |
7368 | } |
7369 | |
7370 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
7371 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
7372 | |
7373 | CharUnits FieldOffset = |
7374 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
7375 | Offset; |
7376 | QualType FieldTy = FD->getType(); |
7377 | std::optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
7378 | if (!SubObj) |
7379 | return std::nullopt; |
7380 | ResultVal.getStructField(FieldIdx) = *SubObj; |
7381 | ++FieldIdx; |
7382 | } |
7383 | |
7384 | return ResultVal; |
7385 | } |
7386 | |
7387 | std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
7388 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
7389 | assert(!RepresentationType.isNull() && |
7390 | "enum forward decl should be caught by Sema" ); |
7391 | const auto *AsBuiltin = |
7392 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
7393 | // Recurse into the underlying type. Treat std::byte transparently as |
7394 | // unsigned char. |
7395 | return visit(AsBuiltin, Offset, /*EnumTy=*/Ty); |
7396 | } |
7397 | |
7398 | std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
7399 | size_t Size = Ty->getLimitedSize(); |
7400 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
7401 | |
7402 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
7403 | for (size_t I = 0; I != Size; ++I) { |
7404 | std::optional<APValue> ElementValue = |
7405 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7406 | if (!ElementValue) |
7407 | return std::nullopt; |
7408 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
7409 | } |
7410 | |
7411 | return ArrayValue; |
7412 | } |
7413 | |
7414 | std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) { |
7415 | QualType EltTy = VTy->getElementType(); |
7416 | unsigned NElts = VTy->getNumElements(); |
7417 | unsigned EltSize = |
7418 | VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(T: EltTy); |
7419 | |
7420 | if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) { |
7421 | // The vector's size in bits is not a multiple of the target's byte size, |
7422 | // so its layout is unspecified. For now, we'll simply treat these cases |
7423 | // as unsupported (this should only be possible with OpenCL bool vectors |
7424 | // whose element count isn't a multiple of the byte size). |
7425 | Info.FFDiag(BCE->getBeginLoc(), |
7426 | diag::note_constexpr_bit_cast_invalid_vector) |
7427 | << QualType(VTy, 0) << EltSize << NElts << Info.Ctx.getCharWidth(); |
7428 | return std::nullopt; |
7429 | } |
7430 | |
7431 | if (EltTy->isRealFloatingType() && &Info.Ctx.getFloatTypeSemantics(T: EltTy) == |
7432 | &APFloat::x87DoubleExtended()) { |
7433 | // The layout for x86_fp80 vectors seems to be handled very inconsistently |
7434 | // by both clang and LLVM, so for now we won't allow bit_casts involving |
7435 | // it in a constexpr context. |
7436 | Info.FFDiag(BCE->getBeginLoc(), |
7437 | diag::note_constexpr_bit_cast_unsupported_type) |
7438 | << EltTy; |
7439 | return std::nullopt; |
7440 | } |
7441 | |
7442 | SmallVector<APValue, 4> Elts; |
7443 | Elts.reserve(N: NElts); |
7444 | if (VTy->isExtVectorBoolType()) { |
7445 | // Special handling for OpenCL bool vectors: |
7446 | // Since these vectors are stored as packed bits, but we can't read |
7447 | // individual bits from the BitCastBuffer, we'll buffer all of the |
7448 | // elements together into an appropriately sized APInt and write them all |
7449 | // out at once. Because we don't accept vectors where NElts * EltSize |
7450 | // isn't a multiple of the char size, there will be no padding space, so |
7451 | // we don't have to worry about reading any padding data which didn't |
7452 | // actually need to be accessed. |
7453 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7454 | |
7455 | SmallVector<uint8_t, 8> Bytes; |
7456 | Bytes.reserve(N: NElts / 8); |
7457 | if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes)) |
7458 | return std::nullopt; |
7459 | |
7460 | APSInt SValInt(NElts, true); |
7461 | llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7462 | |
7463 | for (unsigned I = 0; I < NElts; ++I) { |
7464 | llvm::APInt Elt = |
7465 | SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize); |
7466 | Elts.emplace_back( |
7467 | APSInt(std::move(Elt), !EltTy->isSignedIntegerType())); |
7468 | } |
7469 | } else { |
7470 | // Iterate over each of the elements and read them from the buffer at |
7471 | // the appropriate offset. |
7472 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7473 | for (unsigned I = 0; I < NElts; ++I) { |
7474 | std::optional<APValue> EltValue = |
7475 | visitType(EltTy, Offset + I * EltSizeChars); |
7476 | if (!EltValue) |
7477 | return std::nullopt; |
7478 | Elts.push_back(std::move(*EltValue)); |
7479 | } |
7480 | } |
7481 | |
7482 | return APValue(Elts.data(), Elts.size()); |
7483 | } |
7484 | |
7485 | std::optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
7486 | return unsupportedType(Ty: QualType(Ty, 0)); |
7487 | } |
7488 | |
7489 | std::optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
7490 | QualType Can = Ty.getCanonicalType(); |
7491 | |
7492 | switch (Can->getTypeClass()) { |
7493 | #define TYPE(Class, Base) \ |
7494 | case Type::Class: \ |
7495 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
7496 | #define ABSTRACT_TYPE(Class, Base) |
7497 | #define NON_CANONICAL_TYPE(Class, Base) \ |
7498 | case Type::Class: \ |
7499 | llvm_unreachable("non-canonical type should be impossible!"); |
7500 | #define DEPENDENT_TYPE(Class, Base) \ |
7501 | case Type::Class: \ |
7502 | llvm_unreachable( \ |
7503 | "dependent types aren't supported in the constant evaluator!"); |
7504 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
7505 | case Type::Class: \ |
7506 | llvm_unreachable("either dependent or not canonical!"); |
7507 | #include "clang/AST/TypeNodes.inc" |
7508 | } |
7509 | llvm_unreachable("Unhandled Type::TypeClass" ); |
7510 | } |
7511 | |
7512 | public: |
7513 | // Pull out a full value of type DstType. |
7514 | static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
7515 | const CastExpr *BCE) { |
7516 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
7517 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(Quantity: 0)); |
7518 | } |
7519 | }; |
7520 | |
7521 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
7522 | QualType Ty, EvalInfo *Info, |
7523 | const ASTContext &Ctx, |
7524 | bool CheckingDest) { |
7525 | Ty = Ty.getCanonicalType(); |
7526 | |
7527 | auto diag = [&](int Reason) { |
7528 | if (Info) |
7529 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
7530 | << CheckingDest << (Reason == 4) << Reason; |
7531 | return false; |
7532 | }; |
7533 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
7534 | if (Info) |
7535 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
7536 | << NoteTy << Construct << Ty; |
7537 | return false; |
7538 | }; |
7539 | |
7540 | if (Ty->isUnionType()) |
7541 | return diag(0); |
7542 | if (Ty->isPointerType()) |
7543 | return diag(1); |
7544 | if (Ty->isMemberPointerType()) |
7545 | return diag(2); |
7546 | if (Ty.isVolatileQualified()) |
7547 | return diag(3); |
7548 | |
7549 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
7550 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
7551 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
7552 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
7553 | CheckingDest)) |
7554 | return note(1, BS.getType(), BS.getBeginLoc()); |
7555 | } |
7556 | for (FieldDecl *FD : Record->fields()) { |
7557 | if (FD->getType()->isReferenceType()) |
7558 | return diag(4); |
7559 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
7560 | CheckingDest)) |
7561 | return note(0, FD->getType(), FD->getBeginLoc()); |
7562 | } |
7563 | } |
7564 | |
7565 | if (Ty->isArrayType() && |
7566 | !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty), |
7567 | Info, Ctx, CheckingDest)) |
7568 | return false; |
7569 | |
7570 | return true; |
7571 | } |
7572 | |
7573 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
7574 | const ASTContext &Ctx, |
7575 | const CastExpr *BCE) { |
7576 | bool DestOK = checkBitCastConstexprEligibilityType( |
7577 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
7578 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
7579 | BCE->getBeginLoc(), |
7580 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
7581 | return SourceOK; |
7582 | } |
7583 | |
7584 | static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7585 | const APValue &SourceRValue, |
7586 | const CastExpr *BCE) { |
7587 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7588 | "no host or target supports non 8-bit chars" ); |
7589 | |
7590 | if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE)) |
7591 | return false; |
7592 | |
7593 | // Read out SourceValue into a char buffer. |
7594 | std::optional<BitCastBuffer> Buffer = |
7595 | APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE); |
7596 | if (!Buffer) |
7597 | return false; |
7598 | |
7599 | // Write out the buffer into a new APValue. |
7600 | std::optional<APValue> MaybeDestValue = |
7601 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
7602 | if (!MaybeDestValue) |
7603 | return false; |
7604 | |
7605 | DestValue = std::move(*MaybeDestValue); |
7606 | return true; |
7607 | } |
7608 | |
7609 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7610 | APValue &SourceValue, |
7611 | const CastExpr *BCE) { |
7612 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7613 | "no host or target supports non 8-bit chars" ); |
7614 | assert(SourceValue.isLValue() && |
7615 | "LValueToRValueBitcast requires an lvalue operand!" ); |
7616 | |
7617 | LValue SourceLValue; |
7618 | APValue SourceRValue; |
7619 | SourceLValue.setFrom(Ctx&: Info.Ctx, V: SourceValue); |
7620 | if (!handleLValueToRValueConversion( |
7621 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
7622 | SourceRValue, /*WantObjectRepresentation=*/true)) |
7623 | return false; |
7624 | |
7625 | return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE); |
7626 | } |
7627 | |
7628 | template <class Derived> |
7629 | class ExprEvaluatorBase |
7630 | : public ConstStmtVisitor<Derived, bool> { |
7631 | private: |
7632 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
7633 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
7634 | return getDerived().Success(V, E); |
7635 | } |
7636 | bool DerivedZeroInitialization(const Expr *E) { |
7637 | return getDerived().ZeroInitialization(E); |
7638 | } |
7639 | |
7640 | // Check whether a conditional operator with a non-constant condition is a |
7641 | // potential constant expression. If neither arm is a potential constant |
7642 | // expression, then the conditional operator is not either. |
7643 | template<typename ConditionalOperator> |
7644 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
7645 | assert(Info.checkingPotentialConstantExpression()); |
7646 | |
7647 | // Speculatively evaluate both arms. |
7648 | SmallVector<PartialDiagnosticAt, 8> Diag; |
7649 | { |
7650 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7651 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7652 | if (Diag.empty()) |
7653 | return; |
7654 | } |
7655 | |
7656 | { |
7657 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7658 | Diag.clear(); |
7659 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7660 | if (Diag.empty()) |
7661 | return; |
7662 | } |
7663 | |
7664 | Error(E, diag::note_constexpr_conditional_never_const); |
7665 | } |
7666 | |
7667 | |
7668 | template<typename ConditionalOperator> |
7669 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
7670 | bool BoolResult; |
7671 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
7672 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
7673 | CheckPotentialConstantConditional(E); |
7674 | return false; |
7675 | } |
7676 | if (Info.noteFailure()) { |
7677 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7678 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7679 | } |
7680 | return false; |
7681 | } |
7682 | |
7683 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
7684 | return StmtVisitorTy::Visit(EvalExpr); |
7685 | } |
7686 | |
7687 | protected: |
7688 | EvalInfo &Info; |
7689 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
7690 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
7691 | |
7692 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
7693 | return Info.CCEDiag(E, DiagId: D); |
7694 | } |
7695 | |
7696 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
7697 | |
7698 | bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) { |
7699 | unsigned BuiltinOp = E->getBuiltinCallee(); |
7700 | return BuiltinOp != 0 && |
7701 | Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp); |
7702 | } |
7703 | |
7704 | public: |
7705 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
7706 | |
7707 | EvalInfo &getEvalInfo() { return Info; } |
7708 | |
7709 | /// Report an evaluation error. This should only be called when an error is |
7710 | /// first discovered. When propagating an error, just return false. |
7711 | bool Error(const Expr *E, diag::kind D) { |
7712 | Info.FFDiag(E, DiagId: D) << E->getSourceRange(); |
7713 | return false; |
7714 | } |
7715 | bool Error(const Expr *E) { |
7716 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
7717 | } |
7718 | |
7719 | bool VisitStmt(const Stmt *) { |
7720 | llvm_unreachable("Expression evaluator should not be called on stmts" ); |
7721 | } |
7722 | bool VisitExpr(const Expr *E) { |
7723 | return Error(E); |
7724 | } |
7725 | |
7726 | bool VisitPredefinedExpr(const PredefinedExpr *E) { |
7727 | return StmtVisitorTy::Visit(E->getFunctionName()); |
7728 | } |
7729 | bool VisitConstantExpr(const ConstantExpr *E) { |
7730 | if (E->hasAPValueResult()) |
7731 | return DerivedSuccess(V: E->getAPValueResult(), E); |
7732 | |
7733 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7734 | } |
7735 | |
7736 | bool VisitParenExpr(const ParenExpr *E) |
7737 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7738 | bool VisitUnaryExtension(const UnaryOperator *E) |
7739 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7740 | bool VisitUnaryPlus(const UnaryOperator *E) |
7741 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7742 | bool VisitChooseExpr(const ChooseExpr *E) |
7743 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
7744 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
7745 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
7746 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
7747 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
7748 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
7749 | TempVersionRAII RAII(*Info.CurrentCall); |
7750 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7751 | return StmtVisitorTy::Visit(E->getExpr()); |
7752 | } |
7753 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
7754 | TempVersionRAII RAII(*Info.CurrentCall); |
7755 | // The initializer may not have been parsed yet, or might be erroneous. |
7756 | if (!E->getExpr()) |
7757 | return Error(E); |
7758 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7759 | return StmtVisitorTy::Visit(E->getExpr()); |
7760 | } |
7761 | |
7762 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
7763 | FullExpressionRAII Scope(Info); |
7764 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
7765 | } |
7766 | |
7767 | // Temporaries are registered when created, so we don't care about |
7768 | // CXXBindTemporaryExpr. |
7769 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
7770 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7771 | } |
7772 | |
7773 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
7774 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
7775 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7776 | } |
7777 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
7778 | if (!Info.Ctx.getLangOpts().CPlusPlus20) |
7779 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
7780 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7781 | } |
7782 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
7783 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7784 | } |
7785 | |
7786 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7787 | switch (E->getOpcode()) { |
7788 | default: |
7789 | return Error(E); |
7790 | |
7791 | case BO_Comma: |
7792 | VisitIgnoredValue(E: E->getLHS()); |
7793 | return StmtVisitorTy::Visit(E->getRHS()); |
7794 | |
7795 | case BO_PtrMemD: |
7796 | case BO_PtrMemI: { |
7797 | LValue Obj; |
7798 | if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj)) |
7799 | return false; |
7800 | APValue Result; |
7801 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
7802 | return false; |
7803 | return DerivedSuccess(V: Result, E); |
7804 | } |
7805 | } |
7806 | } |
7807 | |
7808 | bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) { |
7809 | return StmtVisitorTy::Visit(E->getSemanticForm()); |
7810 | } |
7811 | |
7812 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
7813 | // Evaluate and cache the common expression. We treat it as a temporary, |
7814 | // even though it's not quite the same thing. |
7815 | LValue CommonLV; |
7816 | if (!Evaluate(Result&: Info.CurrentCall->createTemporary( |
7817 | E->getOpaqueValue(), |
7818 | getStorageType(Info.Ctx, E->getOpaqueValue()), |
7819 | ScopeKind::FullExpression, CommonLV), |
7820 | Info, E: E->getCommon())) |
7821 | return false; |
7822 | |
7823 | return HandleConditionalOperator(E); |
7824 | } |
7825 | |
7826 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
7827 | bool IsBcpCall = false; |
7828 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
7829 | // the result is a constant expression if it can be folded without |
7830 | // side-effects. This is an important GNU extension. See GCC PR38377 |
7831 | // for discussion. |
7832 | if (const CallExpr *CallCE = |
7833 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
7834 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
7835 | IsBcpCall = true; |
7836 | |
7837 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
7838 | // constant expression; we can't check whether it's potentially foldable. |
7839 | // FIXME: We should instead treat __builtin_constant_p as non-constant if |
7840 | // it would return 'false' in this mode. |
7841 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
7842 | return false; |
7843 | |
7844 | FoldConstant Fold(Info, IsBcpCall); |
7845 | if (!HandleConditionalOperator(E)) { |
7846 | Fold.keepDiagnostics(); |
7847 | return false; |
7848 | } |
7849 | |
7850 | return true; |
7851 | } |
7852 | |
7853 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
7854 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E); |
7855 | Value && !Value->isAbsent()) |
7856 | return DerivedSuccess(V: *Value, E); |
7857 | |
7858 | const Expr *Source = E->getSourceExpr(); |
7859 | if (!Source) |
7860 | return Error(E); |
7861 | if (Source == E) { |
7862 | assert(0 && "OpaqueValueExpr recursively refers to itself" ); |
7863 | return Error(E); |
7864 | } |
7865 | return StmtVisitorTy::Visit(Source); |
7866 | } |
7867 | |
7868 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
7869 | for (const Expr *SemE : E->semantics()) { |
7870 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { |
7871 | // FIXME: We can't handle the case where an OpaqueValueExpr is also the |
7872 | // result expression: there could be two different LValues that would |
7873 | // refer to the same object in that case, and we can't model that. |
7874 | if (SemE == E->getResultExpr()) |
7875 | return Error(E); |
7876 | |
7877 | // Unique OVEs get evaluated if and when we encounter them when |
7878 | // emitting the rest of the semantic form, rather than eagerly. |
7879 | if (OVE->isUnique()) |
7880 | continue; |
7881 | |
7882 | LValue LV; |
7883 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7884 | OVE, getStorageType(Info.Ctx, OVE), |
7885 | ScopeKind::FullExpression, LV), |
7886 | Info, OVE->getSourceExpr())) |
7887 | return false; |
7888 | } else if (SemE == E->getResultExpr()) { |
7889 | if (!StmtVisitorTy::Visit(SemE)) |
7890 | return false; |
7891 | } else { |
7892 | if (!EvaluateIgnoredValue(Info, E: SemE)) |
7893 | return false; |
7894 | } |
7895 | } |
7896 | return true; |
7897 | } |
7898 | |
7899 | bool VisitCallExpr(const CallExpr *E) { |
7900 | APValue Result; |
7901 | if (!handleCallExpr(E, Result, ResultSlot: nullptr)) |
7902 | return false; |
7903 | return DerivedSuccess(V: Result, E); |
7904 | } |
7905 | |
7906 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
7907 | const LValue *ResultSlot) { |
7908 | CallScopeRAII CallScope(Info); |
7909 | |
7910 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
7911 | QualType CalleeType = Callee->getType(); |
7912 | |
7913 | const FunctionDecl *FD = nullptr; |
7914 | LValue *This = nullptr, ThisVal; |
7915 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
7916 | bool HasQualifier = false; |
7917 | |
7918 | CallRef Call; |
7919 | |
7920 | // Extract function decl and 'this' pointer from the callee. |
7921 | if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) { |
7922 | const CXXMethodDecl *Member = nullptr; |
7923 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
7924 | // Explicit bound member calls, such as x.f() or p->g(); |
7925 | if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ThisVal)) |
7926 | return false; |
7927 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
7928 | if (!Member) |
7929 | return Error(Callee); |
7930 | This = &ThisVal; |
7931 | HasQualifier = ME->hasQualifier(); |
7932 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
7933 | // Indirect bound member calls ('.*' or '->*'). |
7934 | const ValueDecl *D = |
7935 | HandleMemberPointerAccess(Info, BO: BE, LV&: ThisVal, IncludeMember: false); |
7936 | if (!D) |
7937 | return false; |
7938 | Member = dyn_cast<CXXMethodDecl>(D); |
7939 | if (!Member) |
7940 | return Error(Callee); |
7941 | This = &ThisVal; |
7942 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
7943 | if (!Info.getLangOpts().CPlusPlus20) |
7944 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
7945 | return EvaluateObjectArgument(Info, PDE->getBase(), ThisVal) && |
7946 | HandleDestruction(Info, PDE, ThisVal, PDE->getDestroyedType()); |
7947 | } else |
7948 | return Error(Callee); |
7949 | FD = Member; |
7950 | } else if (CalleeType->isFunctionPointerType()) { |
7951 | LValue CalleeLV; |
7952 | if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info)) |
7953 | return false; |
7954 | |
7955 | if (!CalleeLV.getLValueOffset().isZero()) |
7956 | return Error(Callee); |
7957 | if (CalleeLV.isNullPointer()) { |
7958 | Info.FFDiag(Callee, diag::note_constexpr_null_callee) |
7959 | << const_cast<Expr *>(Callee); |
7960 | return false; |
7961 | } |
7962 | FD = dyn_cast_or_null<FunctionDecl>( |
7963 | CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>()); |
7964 | if (!FD) |
7965 | return Error(Callee); |
7966 | // Don't call function pointers which have been cast to some other type. |
7967 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
7968 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
7969 | T: CalleeType->getPointeeType(), U: FD->getType())) { |
7970 | return Error(E); |
7971 | } |
7972 | |
7973 | // For an (overloaded) assignment expression, evaluate the RHS before the |
7974 | // LHS. |
7975 | auto *OCE = dyn_cast<CXXOperatorCallExpr>(E); |
7976 | if (OCE && OCE->isAssignmentOp()) { |
7977 | assert(Args.size() == 2 && "wrong number of arguments in assignment" ); |
7978 | Call = Info.CurrentCall->createCall(Callee: FD); |
7979 | bool HasThis = false; |
7980 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
7981 | HasThis = MD->isImplicitObjectMemberFunction(); |
7982 | if (!EvaluateArgs(HasThis ? Args.slice(1) : Args, Call, Info, FD, |
7983 | /*RightToLeft=*/true)) |
7984 | return false; |
7985 | } |
7986 | |
7987 | // Overloaded operator calls to member functions are represented as normal |
7988 | // calls with '*this' as the first argument. |
7989 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
7990 | if (MD && |
7991 | (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) { |
7992 | // FIXME: When selecting an implicit conversion for an overloaded |
7993 | // operator delete, we sometimes try to evaluate calls to conversion |
7994 | // operators without a 'this' parameter! |
7995 | if (Args.empty()) |
7996 | return Error(E); |
7997 | |
7998 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
7999 | return false; |
8000 | |
8001 | // If we are calling a static operator, the 'this' argument needs to be |
8002 | // ignored after being evaluated. |
8003 | if (MD->isInstance()) |
8004 | This = &ThisVal; |
8005 | |
8006 | // If this is syntactically a simple assignment using a trivial |
8007 | // assignment operator, start the lifetimes of union members as needed, |
8008 | // per C++20 [class.union]5. |
8009 | if (Info.getLangOpts().CPlusPlus20 && OCE && |
8010 | OCE->getOperator() == OO_Equal && MD->isTrivial() && |
8011 | !MaybeHandleUnionActiveMemberChange(Info, Args[0], ThisVal)) |
8012 | return false; |
8013 | |
8014 | Args = Args.slice(1); |
8015 | } else if (MD && MD->isLambdaStaticInvoker()) { |
8016 | // Map the static invoker for the lambda back to the call operator. |
8017 | // Conveniently, we don't have to slice out the 'this' argument (as is |
8018 | // being done for the non-static case), since a static member function |
8019 | // doesn't have an implicit argument passed in. |
8020 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
8021 | assert( |
8022 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
8023 | "Number of captures must be zero for conversion to function-ptr" ); |
8024 | |
8025 | const CXXMethodDecl *LambdaCallOp = |
8026 | ClosureClass->getLambdaCallOperator(); |
8027 | |
8028 | // Set 'FD', the function that will be called below, to the call |
8029 | // operator. If the closure object represents a generic lambda, find |
8030 | // the corresponding specialization of the call operator. |
8031 | |
8032 | if (ClosureClass->isGenericLambda()) { |
8033 | assert(MD->isFunctionTemplateSpecialization() && |
8034 | "A generic lambda's static-invoker function must be a " |
8035 | "template specialization" ); |
8036 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
8037 | FunctionTemplateDecl *CallOpTemplate = |
8038 | LambdaCallOp->getDescribedFunctionTemplate(); |
8039 | void *InsertPos = nullptr; |
8040 | FunctionDecl *CorrespondingCallOpSpecialization = |
8041 | CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos); |
8042 | assert(CorrespondingCallOpSpecialization && |
8043 | "We must always have a function call operator specialization " |
8044 | "that corresponds to our static invoker specialization" ); |
8045 | assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization)); |
8046 | FD = CorrespondingCallOpSpecialization; |
8047 | } else |
8048 | FD = LambdaCallOp; |
8049 | } else if (FD->isReplaceableGlobalAllocationFunction()) { |
8050 | if (FD->getDeclName().getCXXOverloadedOperator() == OO_New || |
8051 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New) { |
8052 | LValue Ptr; |
8053 | if (!HandleOperatorNewCall(Info, E, Result&: Ptr)) |
8054 | return false; |
8055 | Ptr.moveInto(V&: Result); |
8056 | return CallScope.destroy(); |
8057 | } else { |
8058 | return HandleOperatorDeleteCall(Info, E) && CallScope.destroy(); |
8059 | } |
8060 | } |
8061 | } else |
8062 | return Error(E); |
8063 | |
8064 | // Evaluate the arguments now if we've not already done so. |
8065 | if (!Call) { |
8066 | Call = Info.CurrentCall->createCall(Callee: FD); |
8067 | if (!EvaluateArgs(Args, Call, Info, FD)) |
8068 | return false; |
8069 | } |
8070 | |
8071 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
8072 | if (This) { |
8073 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
8074 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
8075 | // Perform virtual dispatch, if necessary. |
8076 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
8077 | CovariantAdjustmentPath); |
8078 | if (!FD) |
8079 | return false; |
8080 | } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) { |
8081 | // Check that the 'this' pointer points to an object of the right type. |
8082 | // FIXME: If this is an assignment operator call, we may need to change |
8083 | // the active union member before we check this. |
8084 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
8085 | return false; |
8086 | } |
8087 | } |
8088 | |
8089 | // Destructor calls are different enough that they have their own codepath. |
8090 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
8091 | assert(This && "no 'this' pointer for destructor call" ); |
8092 | return HandleDestruction(Info, E, *This, |
8093 | Info.Ctx.getRecordType(Decl: DD->getParent())) && |
8094 | CallScope.destroy(); |
8095 | } |
8096 | |
8097 | const FunctionDecl *Definition = nullptr; |
8098 | Stmt *Body = FD->getBody(Definition); |
8099 | |
8100 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
8101 | !HandleFunctionCall(E->getExprLoc(), Definition, This, E, Args, Call, |
8102 | Body, Info, Result, ResultSlot)) |
8103 | return false; |
8104 | |
8105 | if (!CovariantAdjustmentPath.empty() && |
8106 | !HandleCovariantReturnAdjustment(Info, E, Result, |
8107 | CovariantAdjustmentPath)) |
8108 | return false; |
8109 | |
8110 | return CallScope.destroy(); |
8111 | } |
8112 | |
8113 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8114 | return StmtVisitorTy::Visit(E->getInitializer()); |
8115 | } |
8116 | bool VisitInitListExpr(const InitListExpr *E) { |
8117 | if (E->getNumInits() == 0) |
8118 | return DerivedZeroInitialization(E); |
8119 | if (E->getNumInits() == 1) |
8120 | return StmtVisitorTy::Visit(E->getInit(Init: 0)); |
8121 | return Error(E); |
8122 | } |
8123 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
8124 | return DerivedZeroInitialization(E); |
8125 | } |
8126 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
8127 | return DerivedZeroInitialization(E); |
8128 | } |
8129 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
8130 | return DerivedZeroInitialization(E); |
8131 | } |
8132 | |
8133 | /// A member expression where the object is a prvalue is itself a prvalue. |
8134 | bool VisitMemberExpr(const MemberExpr *E) { |
8135 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
8136 | "missing temporary materialization conversion" ); |
8137 | assert(!E->isArrow() && "missing call to bound member function?" ); |
8138 | |
8139 | APValue Val; |
8140 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8141 | return false; |
8142 | |
8143 | QualType BaseTy = E->getBase()->getType(); |
8144 | |
8145 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
8146 | if (!FD) return Error(E); |
8147 | assert(!FD->getType()->isReferenceType() && "prvalue reference?" ); |
8148 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8149 | FD->getParent()->getCanonicalDecl() && "record / field mismatch" ); |
8150 | |
8151 | // Note: there is no lvalue base here. But this case should only ever |
8152 | // happen in C or in C++98, where we cannot be evaluating a constexpr |
8153 | // constructor, which is the only case the base matters. |
8154 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
8155 | SubobjectDesignator Designator(BaseTy); |
8156 | Designator.addDeclUnchecked(FD); |
8157 | |
8158 | APValue Result; |
8159 | return extractSubobject(Info, E, Obj, Designator, Result) && |
8160 | DerivedSuccess(V: Result, E); |
8161 | } |
8162 | |
8163 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
8164 | APValue Val; |
8165 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8166 | return false; |
8167 | |
8168 | if (Val.isVector()) { |
8169 | SmallVector<uint32_t, 4> Indices; |
8170 | E->getEncodedElementAccess(Elts&: Indices); |
8171 | if (Indices.size() == 1) { |
8172 | // Return scalar. |
8173 | return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E); |
8174 | } else { |
8175 | // Construct new APValue vector. |
8176 | SmallVector<APValue, 4> Elts; |
8177 | for (unsigned I = 0; I < Indices.size(); ++I) { |
8178 | Elts.push_back(Elt: Val.getVectorElt(I: Indices[I])); |
8179 | } |
8180 | APValue VecResult(Elts.data(), Indices.size()); |
8181 | return DerivedSuccess(V: VecResult, E); |
8182 | } |
8183 | } |
8184 | |
8185 | return false; |
8186 | } |
8187 | |
8188 | bool VisitCastExpr(const CastExpr *E) { |
8189 | switch (E->getCastKind()) { |
8190 | default: |
8191 | break; |
8192 | |
8193 | case CK_AtomicToNonAtomic: { |
8194 | APValue AtomicVal; |
8195 | // This does not need to be done in place even for class/array types: |
8196 | // atomic-to-non-atomic conversion implies copying the object |
8197 | // representation. |
8198 | if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr())) |
8199 | return false; |
8200 | return DerivedSuccess(V: AtomicVal, E); |
8201 | } |
8202 | |
8203 | case CK_NoOp: |
8204 | case CK_UserDefinedConversion: |
8205 | return StmtVisitorTy::Visit(E->getSubExpr()); |
8206 | |
8207 | case CK_LValueToRValue: { |
8208 | LValue LVal; |
8209 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info)) |
8210 | return false; |
8211 | APValue RVal; |
8212 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
8213 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
8214 | LVal, RVal)) |
8215 | return false; |
8216 | return DerivedSuccess(V: RVal, E); |
8217 | } |
8218 | case CK_LValueToRValueBitCast: { |
8219 | APValue DestValue, SourceValue; |
8220 | if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr())) |
8221 | return false; |
8222 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E)) |
8223 | return false; |
8224 | return DerivedSuccess(V: DestValue, E); |
8225 | } |
8226 | |
8227 | case CK_AddressSpaceConversion: { |
8228 | APValue Value; |
8229 | if (!Evaluate(Result&: Value, Info, E: E->getSubExpr())) |
8230 | return false; |
8231 | return DerivedSuccess(V: Value, E); |
8232 | } |
8233 | } |
8234 | |
8235 | return Error(E); |
8236 | } |
8237 | |
8238 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
8239 | return VisitUnaryPostIncDec(UO); |
8240 | } |
8241 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
8242 | return VisitUnaryPostIncDec(UO); |
8243 | } |
8244 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
8245 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8246 | return Error(UO); |
8247 | |
8248 | LValue LVal; |
8249 | if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info)) |
8250 | return false; |
8251 | APValue RVal; |
8252 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
8253 | UO->isIncrementOp(), &RVal)) |
8254 | return false; |
8255 | return DerivedSuccess(V: RVal, E: UO); |
8256 | } |
8257 | |
8258 | bool VisitStmtExpr(const StmtExpr *E) { |
8259 | // We will have checked the full-expressions inside the statement expression |
8260 | // when they were completed, and don't need to check them again now. |
8261 | llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior, |
8262 | false); |
8263 | |
8264 | const CompoundStmt *CS = E->getSubStmt(); |
8265 | if (CS->body_empty()) |
8266 | return true; |
8267 | |
8268 | BlockScopeRAII Scope(Info); |
8269 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
8270 | BE = CS->body_end(); |
8271 | /**/; ++BI) { |
8272 | if (BI + 1 == BE) { |
8273 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
8274 | if (!FinalExpr) { |
8275 | Info.FFDiag((*BI)->getBeginLoc(), |
8276 | diag::note_constexpr_stmt_expr_unsupported); |
8277 | return false; |
8278 | } |
8279 | return this->Visit(FinalExpr) && Scope.destroy(); |
8280 | } |
8281 | |
8282 | APValue ReturnValue; |
8283 | StmtResult Result = { .Value: ReturnValue, .Slot: nullptr }; |
8284 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI); |
8285 | if (ESR != ESR_Succeeded) { |
8286 | // FIXME: If the statement-expression terminated due to 'return', |
8287 | // 'break', or 'continue', it would be nice to propagate that to |
8288 | // the outer statement evaluation rather than bailing out. |
8289 | if (ESR != ESR_Failed) |
8290 | Info.FFDiag((*BI)->getBeginLoc(), |
8291 | diag::note_constexpr_stmt_expr_unsupported); |
8292 | return false; |
8293 | } |
8294 | } |
8295 | |
8296 | llvm_unreachable("Return from function from the loop above." ); |
8297 | } |
8298 | |
8299 | bool VisitPackIndexingExpr(const PackIndexingExpr *E) { |
8300 | return StmtVisitorTy::Visit(E->getSelectedExpr()); |
8301 | } |
8302 | |
8303 | /// Visit a value which is evaluated, but whose value is ignored. |
8304 | void VisitIgnoredValue(const Expr *E) { |
8305 | EvaluateIgnoredValue(Info, E); |
8306 | } |
8307 | |
8308 | /// Potentially visit a MemberExpr's base expression. |
8309 | void VisitIgnoredBaseExpression(const Expr *E) { |
8310 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
8311 | // presence of side-effecting behavior. |
8312 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx)) |
8313 | return; |
8314 | VisitIgnoredValue(E); |
8315 | } |
8316 | }; |
8317 | |
8318 | } // namespace |
8319 | |
8320 | //===----------------------------------------------------------------------===// |
8321 | // Common base class for lvalue and temporary evaluation. |
8322 | //===----------------------------------------------------------------------===// |
8323 | namespace { |
8324 | template<class Derived> |
8325 | class LValueExprEvaluatorBase |
8326 | : public ExprEvaluatorBase<Derived> { |
8327 | protected: |
8328 | LValue &Result; |
8329 | bool InvalidBaseOK; |
8330 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
8331 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
8332 | |
8333 | bool Success(APValue::LValueBase B) { |
8334 | Result.set(B); |
8335 | return true; |
8336 | } |
8337 | |
8338 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8339 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
8340 | } |
8341 | |
8342 | public: |
8343 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
8344 | : ExprEvaluatorBaseTy(Info), Result(Result), |
8345 | InvalidBaseOK(InvalidBaseOK) {} |
8346 | |
8347 | bool Success(const APValue &V, const Expr *E) { |
8348 | Result.setFrom(Ctx&: this->Info.Ctx, V); |
8349 | return true; |
8350 | } |
8351 | |
8352 | bool VisitMemberExpr(const MemberExpr *E) { |
8353 | // Handle non-static data members. |
8354 | QualType BaseTy; |
8355 | bool EvalOK; |
8356 | if (E->isArrow()) { |
8357 | EvalOK = evaluatePointer(E: E->getBase(), Result); |
8358 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
8359 | } else if (E->getBase()->isPRValue()) { |
8360 | assert(E->getBase()->getType()->isRecordType()); |
8361 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
8362 | BaseTy = E->getBase()->getType(); |
8363 | } else { |
8364 | EvalOK = this->Visit(E->getBase()); |
8365 | BaseTy = E->getBase()->getType(); |
8366 | } |
8367 | if (!EvalOK) { |
8368 | if (!InvalidBaseOK) |
8369 | return false; |
8370 | Result.setInvalid(E); |
8371 | return true; |
8372 | } |
8373 | |
8374 | const ValueDecl *MD = E->getMemberDecl(); |
8375 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) { |
8376 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8377 | FD->getParent()->getCanonicalDecl() && "record / field mismatch" ); |
8378 | (void)BaseTy; |
8379 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
8380 | return false; |
8381 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) { |
8382 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
8383 | return false; |
8384 | } else |
8385 | return this->Error(E); |
8386 | |
8387 | if (MD->getType()->isReferenceType()) { |
8388 | APValue RefValue; |
8389 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
8390 | RefValue)) |
8391 | return false; |
8392 | return Success(RefValue, E); |
8393 | } |
8394 | return true; |
8395 | } |
8396 | |
8397 | bool VisitBinaryOperator(const BinaryOperator *E) { |
8398 | switch (E->getOpcode()) { |
8399 | default: |
8400 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
8401 | |
8402 | case BO_PtrMemD: |
8403 | case BO_PtrMemI: |
8404 | return HandleMemberPointerAccess(this->Info, E, Result); |
8405 | } |
8406 | } |
8407 | |
8408 | bool VisitCastExpr(const CastExpr *E) { |
8409 | switch (E->getCastKind()) { |
8410 | default: |
8411 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8412 | |
8413 | case CK_DerivedToBase: |
8414 | case CK_UncheckedDerivedToBase: |
8415 | if (!this->Visit(E->getSubExpr())) |
8416 | return false; |
8417 | |
8418 | // Now figure out the necessary offset to add to the base LV to get from |
8419 | // the derived class to the base class. |
8420 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
8421 | Result); |
8422 | } |
8423 | } |
8424 | }; |
8425 | } |
8426 | |
8427 | //===----------------------------------------------------------------------===// |
8428 | // LValue Evaluation |
8429 | // |
8430 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
8431 | // function designators (in C), decl references to void objects (in C), and |
8432 | // temporaries (if building with -Wno-address-of-temporary). |
8433 | // |
8434 | // LValue evaluation produces values comprising a base expression of one of the |
8435 | // following types: |
8436 | // - Declarations |
8437 | // * VarDecl |
8438 | // * FunctionDecl |
8439 | // - Literals |
8440 | // * CompoundLiteralExpr in C (and in global scope in C++) |
8441 | // * StringLiteral |
8442 | // * PredefinedExpr |
8443 | // * ObjCStringLiteralExpr |
8444 | // * ObjCEncodeExpr |
8445 | // * AddrLabelExpr |
8446 | // * BlockExpr |
8447 | // * CallExpr for a MakeStringConstant builtin |
8448 | // - typeid(T) expressions, as TypeInfoLValues |
8449 | // - Locals and temporaries |
8450 | // * MaterializeTemporaryExpr |
8451 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
8452 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
8453 | // from the AST (FIXME). |
8454 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
8455 | // CallIndex, for a lifetime-extended temporary. |
8456 | // * The ConstantExpr that is currently being evaluated during evaluation of an |
8457 | // immediate invocation. |
8458 | // plus an offset in bytes. |
8459 | //===----------------------------------------------------------------------===// |
8460 | namespace { |
8461 | class LValueExprEvaluator |
8462 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
8463 | public: |
8464 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
8465 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
8466 | |
8467 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
8468 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
8469 | |
8470 | bool VisitCallExpr(const CallExpr *E); |
8471 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
8472 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
8473 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
8474 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
8475 | bool VisitMemberExpr(const MemberExpr *E); |
8476 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
8477 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
8478 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
8479 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
8480 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
8481 | bool VisitUnaryDeref(const UnaryOperator *E); |
8482 | bool VisitUnaryReal(const UnaryOperator *E); |
8483 | bool VisitUnaryImag(const UnaryOperator *E); |
8484 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
8485 | return VisitUnaryPreIncDec(UO); |
8486 | } |
8487 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
8488 | return VisitUnaryPreIncDec(UO); |
8489 | } |
8490 | bool VisitBinAssign(const BinaryOperator *BO); |
8491 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
8492 | |
8493 | bool VisitCastExpr(const CastExpr *E) { |
8494 | switch (E->getCastKind()) { |
8495 | default: |
8496 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
8497 | |
8498 | case CK_LValueBitCast: |
8499 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) |
8500 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
8501 | if (!Visit(E->getSubExpr())) |
8502 | return false; |
8503 | Result.Designator.setInvalid(); |
8504 | return true; |
8505 | |
8506 | case CK_BaseToDerived: |
8507 | if (!Visit(E->getSubExpr())) |
8508 | return false; |
8509 | return HandleBaseToDerivedCast(Info, E, Result); |
8510 | |
8511 | case CK_Dynamic: |
8512 | if (!Visit(E->getSubExpr())) |
8513 | return false; |
8514 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
8515 | } |
8516 | } |
8517 | }; |
8518 | } // end anonymous namespace |
8519 | |
8520 | /// Get an lvalue to a field of a lambda's closure type. |
8521 | static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result, |
8522 | const CXXMethodDecl *MD, const FieldDecl *FD, |
8523 | bool LValueToRValueConversion) { |
8524 | // Static lambda function call operators can't have captures. We already |
8525 | // diagnosed this, so bail out here. |
8526 | if (MD->isStatic()) { |
8527 | assert(Info.CurrentCall->This == nullptr && |
8528 | "This should not be set for a static call operator" ); |
8529 | return false; |
8530 | } |
8531 | |
8532 | // Start with 'Result' referring to the complete closure object... |
8533 | if (MD->isExplicitObjectMemberFunction()) { |
8534 | // Self may be passed by reference or by value. |
8535 | const ParmVarDecl *Self = MD->getParamDecl(0); |
8536 | if (Self->getType()->isReferenceType()) { |
8537 | APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self); |
8538 | Result.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
8539 | } else { |
8540 | const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self); |
8541 | CallStackFrame *Frame = |
8542 | Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex) |
8543 | .first; |
8544 | unsigned Version = Info.CurrentCall->Arguments.Version; |
8545 | Result.set({VD, Frame->Index, Version}); |
8546 | } |
8547 | } else |
8548 | Result = *Info.CurrentCall->This; |
8549 | |
8550 | // ... then update it to refer to the field of the closure object |
8551 | // that represents the capture. |
8552 | if (!HandleLValueMember(Info, E, LVal&: Result, FD)) |
8553 | return false; |
8554 | |
8555 | // And if the field is of reference type (or if we captured '*this' by |
8556 | // reference), update 'Result' to refer to what |
8557 | // the field refers to. |
8558 | if (LValueToRValueConversion) { |
8559 | APValue RVal; |
8560 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, RVal)) |
8561 | return false; |
8562 | Result.setFrom(Ctx&: Info.Ctx, V: RVal); |
8563 | } |
8564 | return true; |
8565 | } |
8566 | |
8567 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
8568 | /// expressions which are not glvalues, in three cases: |
8569 | /// * function designators in C, and |
8570 | /// * "extern void" objects |
8571 | /// * @selector() expressions in Objective-C |
8572 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
8573 | bool InvalidBaseOK) { |
8574 | assert(!E->isValueDependent()); |
8575 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
8576 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens())); |
8577 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8578 | } |
8579 | |
8580 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
8581 | const NamedDecl *D = E->getDecl(); |
8582 | if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl, |
8583 | UnnamedGlobalConstantDecl>(Val: D)) |
8584 | return Success(B: cast<ValueDecl>(Val: D)); |
8585 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
8586 | return VisitVarDecl(E, VD); |
8587 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D)) |
8588 | return Visit(BD->getBinding()); |
8589 | return Error(E); |
8590 | } |
8591 | |
8592 | |
8593 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
8594 | |
8595 | // If we are within a lambda's call operator, check whether the 'VD' referred |
8596 | // to within 'E' actually represents a lambda-capture that maps to a |
8597 | // data-member/field within the closure object, and if so, evaluate to the |
8598 | // field or what the field refers to. |
8599 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
8600 | isa<DeclRefExpr>(Val: E) && |
8601 | cast<DeclRefExpr>(Val: E)->refersToEnclosingVariableOrCapture()) { |
8602 | // We don't always have a complete capture-map when checking or inferring if |
8603 | // the function call operator meets the requirements of a constexpr function |
8604 | // - but we don't need to evaluate the captures to determine constexprness |
8605 | // (dcl.constexpr C++17). |
8606 | if (Info.checkingPotentialConstantExpression()) |
8607 | return false; |
8608 | |
8609 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
8610 | const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee); |
8611 | return HandleLambdaCapture(Info, E, Result, MD, FD, |
8612 | FD->getType()->isReferenceType()); |
8613 | } |
8614 | } |
8615 | |
8616 | CallStackFrame *Frame = nullptr; |
8617 | unsigned Version = 0; |
8618 | if (VD->hasLocalStorage()) { |
8619 | // Only if a local variable was declared in the function currently being |
8620 | // evaluated, do we expect to be able to find its value in the current |
8621 | // frame. (Otherwise it was likely declared in an enclosing context and |
8622 | // could either have a valid evaluatable value (for e.g. a constexpr |
8623 | // variable) or be ill-formed (and trigger an appropriate evaluation |
8624 | // diagnostic)). |
8625 | CallStackFrame *CurrFrame = Info.CurrentCall; |
8626 | if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) { |
8627 | // Function parameters are stored in some caller's frame. (Usually the |
8628 | // immediate caller, but for an inherited constructor they may be more |
8629 | // distant.) |
8630 | if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) { |
8631 | if (CurrFrame->Arguments) { |
8632 | VD = CurrFrame->Arguments.getOrigParam(PVD); |
8633 | Frame = |
8634 | Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first; |
8635 | Version = CurrFrame->Arguments.Version; |
8636 | } |
8637 | } else { |
8638 | Frame = CurrFrame; |
8639 | Version = CurrFrame->getCurrentTemporaryVersion(Key: VD); |
8640 | } |
8641 | } |
8642 | } |
8643 | |
8644 | if (!VD->getType()->isReferenceType()) { |
8645 | if (Frame) { |
8646 | Result.set({VD, Frame->Index, Version}); |
8647 | return true; |
8648 | } |
8649 | return Success(VD); |
8650 | } |
8651 | |
8652 | if (!Info.getLangOpts().CPlusPlus11) { |
8653 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1) |
8654 | << VD << VD->getType(); |
8655 | Info.Note(VD->getLocation(), diag::note_declared_at); |
8656 | } |
8657 | |
8658 | APValue *V; |
8659 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V)) |
8660 | return false; |
8661 | if (!V->hasValue()) { |
8662 | // FIXME: Is it possible for V to be indeterminate here? If so, we should |
8663 | // adjust the diagnostic to say that. |
8664 | if (!Info.checkingPotentialConstantExpression()) |
8665 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
8666 | return false; |
8667 | } |
8668 | return Success(V: *V, E); |
8669 | } |
8670 | |
8671 | bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) { |
8672 | if (!IsConstantEvaluatedBuiltinCall(E)) |
8673 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8674 | |
8675 | switch (E->getBuiltinCallee()) { |
8676 | default: |
8677 | return false; |
8678 | case Builtin::BIas_const: |
8679 | case Builtin::BIforward: |
8680 | case Builtin::BIforward_like: |
8681 | case Builtin::BImove: |
8682 | case Builtin::BImove_if_noexcept: |
8683 | if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr()) |
8684 | return Visit(E->getArg(Arg: 0)); |
8685 | break; |
8686 | } |
8687 | |
8688 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8689 | } |
8690 | |
8691 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
8692 | const MaterializeTemporaryExpr *E) { |
8693 | // Walk through the expression to find the materialized temporary itself. |
8694 | SmallVector<const Expr *, 2> CommaLHSs; |
8695 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
8696 | const Expr *Inner = |
8697 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
8698 | |
8699 | // If we passed any comma operators, evaluate their LHSs. |
8700 | for (const Expr *E : CommaLHSs) |
8701 | if (!EvaluateIgnoredValue(Info, E)) |
8702 | return false; |
8703 | |
8704 | // A materialized temporary with static storage duration can appear within the |
8705 | // result of a constant expression evaluation, so we need to preserve its |
8706 | // value for use outside this evaluation. |
8707 | APValue *Value; |
8708 | if (E->getStorageDuration() == SD_Static) { |
8709 | if (Info.EvalMode == EvalInfo::EM_ConstantFold) |
8710 | return false; |
8711 | // FIXME: What about SD_Thread? |
8712 | Value = E->getOrCreateValue(MayCreate: true); |
8713 | *Value = APValue(); |
8714 | Result.set(E); |
8715 | } else { |
8716 | Value = &Info.CurrentCall->createTemporary( |
8717 | Key: E, T: Inner->getType(), |
8718 | Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression |
8719 | : ScopeKind::Block, |
8720 | LV&: Result); |
8721 | } |
8722 | |
8723 | QualType Type = Inner->getType(); |
8724 | |
8725 | // Materialize the temporary itself. |
8726 | if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) { |
8727 | *Value = APValue(); |
8728 | return false; |
8729 | } |
8730 | |
8731 | // Adjust our lvalue to refer to the desired subobject. |
8732 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
8733 | --I; |
8734 | switch (Adjustments[I].Kind) { |
8735 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
8736 | if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath, |
8737 | Type, Result)) |
8738 | return false; |
8739 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
8740 | break; |
8741 | |
8742 | case SubobjectAdjustment::FieldAdjustment: |
8743 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
8744 | return false; |
8745 | Type = Adjustments[I].Field->getType(); |
8746 | break; |
8747 | |
8748 | case SubobjectAdjustment::MemberPointerAdjustment: |
8749 | if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result, |
8750 | RHS: Adjustments[I].Ptr.RHS)) |
8751 | return false; |
8752 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
8753 | break; |
8754 | } |
8755 | } |
8756 | |
8757 | return true; |
8758 | } |
8759 | |
8760 | bool |
8761 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8762 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
8763 | "lvalue compound literal in c++?" ); |
8764 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
8765 | // only see this when folding in C, so there's no standard to follow here. |
8766 | return Success(E); |
8767 | } |
8768 | |
8769 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
8770 | TypeInfoLValue TypeInfo; |
8771 | |
8772 | if (!E->isPotentiallyEvaluated()) { |
8773 | if (E->isTypeOperand()) |
8774 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Context&: Info.Ctx).getTypePtr()); |
8775 | else |
8776 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
8777 | } else { |
8778 | if (!Info.Ctx.getLangOpts().CPlusPlus20) { |
8779 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
8780 | << E->getExprOperand()->getType() |
8781 | << E->getExprOperand()->getSourceRange(); |
8782 | } |
8783 | |
8784 | if (!Visit(E->getExprOperand())) |
8785 | return false; |
8786 | |
8787 | std::optional<DynamicType> DynType = |
8788 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
8789 | if (!DynType) |
8790 | return false; |
8791 | |
8792 | TypeInfo = |
8793 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
8794 | } |
8795 | |
8796 | return Success(APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType())); |
8797 | } |
8798 | |
8799 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
8800 | return Success(E->getGuidDecl()); |
8801 | } |
8802 | |
8803 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
8804 | // Handle static data members. |
8805 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) { |
8806 | VisitIgnoredBaseExpression(E: E->getBase()); |
8807 | return VisitVarDecl(E, VD); |
8808 | } |
8809 | |
8810 | // Handle static member functions. |
8811 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) { |
8812 | if (MD->isStatic()) { |
8813 | VisitIgnoredBaseExpression(E: E->getBase()); |
8814 | return Success(MD); |
8815 | } |
8816 | } |
8817 | |
8818 | // Handle non-static data members. |
8819 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
8820 | } |
8821 | |
8822 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
8823 | // FIXME: Deal with vectors as array subscript bases. |
8824 | if (E->getBase()->getType()->isVectorType() || |
8825 | E->getBase()->getType()->isSveVLSBuiltinType()) |
8826 | return Error(E); |
8827 | |
8828 | APSInt Index; |
8829 | bool Success = true; |
8830 | |
8831 | // C++17's rules require us to evaluate the LHS first, regardless of which |
8832 | // side is the base. |
8833 | for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) { |
8834 | if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result) |
8835 | : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) { |
8836 | if (!Info.noteFailure()) |
8837 | return false; |
8838 | Success = false; |
8839 | } |
8840 | } |
8841 | |
8842 | return Success && |
8843 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
8844 | } |
8845 | |
8846 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
8847 | return evaluatePointer(E: E->getSubExpr(), Result); |
8848 | } |
8849 | |
8850 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
8851 | if (!Visit(E->getSubExpr())) |
8852 | return false; |
8853 | // __real is a no-op on scalar lvalues. |
8854 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
8855 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
8856 | return true; |
8857 | } |
8858 | |
8859 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
8860 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
8861 | "lvalue __imag__ on scalar?" ); |
8862 | if (!Visit(E->getSubExpr())) |
8863 | return false; |
8864 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
8865 | return true; |
8866 | } |
8867 | |
8868 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
8869 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8870 | return Error(UO); |
8871 | |
8872 | if (!this->Visit(UO->getSubExpr())) |
8873 | return false; |
8874 | |
8875 | return handleIncDec( |
8876 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
8877 | UO->isIncrementOp(), nullptr); |
8878 | } |
8879 | |
8880 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
8881 | const CompoundAssignOperator *CAO) { |
8882 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8883 | return Error(CAO); |
8884 | |
8885 | bool Success = true; |
8886 | |
8887 | // C++17 onwards require that we evaluate the RHS first. |
8888 | APValue RHS; |
8889 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) { |
8890 | if (!Info.noteFailure()) |
8891 | return false; |
8892 | Success = false; |
8893 | } |
8894 | |
8895 | // The overall lvalue result is the result of evaluating the LHS. |
8896 | if (!this->Visit(CAO->getLHS()) || !Success) |
8897 | return false; |
8898 | |
8899 | return handleCompoundAssignment( |
8900 | this->Info, CAO, |
8901 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
8902 | CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RHS); |
8903 | } |
8904 | |
8905 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
8906 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8907 | return Error(E); |
8908 | |
8909 | bool Success = true; |
8910 | |
8911 | // C++17 onwards require that we evaluate the RHS first. |
8912 | APValue NewVal; |
8913 | if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) { |
8914 | if (!Info.noteFailure()) |
8915 | return false; |
8916 | Success = false; |
8917 | } |
8918 | |
8919 | if (!this->Visit(E->getLHS()) || !Success) |
8920 | return false; |
8921 | |
8922 | if (Info.getLangOpts().CPlusPlus20 && |
8923 | !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result)) |
8924 | return false; |
8925 | |
8926 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
8927 | NewVal); |
8928 | } |
8929 | |
8930 | //===----------------------------------------------------------------------===// |
8931 | // Pointer Evaluation |
8932 | //===----------------------------------------------------------------------===// |
8933 | |
8934 | /// Attempts to compute the number of bytes available at the pointer |
8935 | /// returned by a function with the alloc_size attribute. Returns true if we |
8936 | /// were successful. Places an unsigned number into `Result`. |
8937 | /// |
8938 | /// This expects the given CallExpr to be a call to a function with an |
8939 | /// alloc_size attribute. |
8940 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8941 | const CallExpr *Call, |
8942 | llvm::APInt &Result) { |
8943 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
8944 | |
8945 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
8946 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
8947 | unsigned BitsInSizeT = Ctx.getTypeSize(T: Ctx.getSizeType()); |
8948 | if (Call->getNumArgs() <= SizeArgNo) |
8949 | return false; |
8950 | |
8951 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
8952 | Expr::EvalResult ExprResult; |
8953 | if (!E->EvaluateAsInt(Result&: ExprResult, Ctx, AllowSideEffects: Expr::SE_AllowSideEffects)) |
8954 | return false; |
8955 | Into = ExprResult.Val.getInt(); |
8956 | if (Into.isNegative() || !Into.isIntN(N: BitsInSizeT)) |
8957 | return false; |
8958 | Into = Into.zext(width: BitsInSizeT); |
8959 | return true; |
8960 | }; |
8961 | |
8962 | APSInt SizeOfElem; |
8963 | if (!EvaluateAsSizeT(Call->getArg(Arg: SizeArgNo), SizeOfElem)) |
8964 | return false; |
8965 | |
8966 | if (!AllocSize->getNumElemsParam().isValid()) { |
8967 | Result = std::move(SizeOfElem); |
8968 | return true; |
8969 | } |
8970 | |
8971 | APSInt NumberOfElems; |
8972 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
8973 | if (!EvaluateAsSizeT(Call->getArg(Arg: NumArgNo), NumberOfElems)) |
8974 | return false; |
8975 | |
8976 | bool Overflow; |
8977 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(RHS: NumberOfElems, Overflow); |
8978 | if (Overflow) |
8979 | return false; |
8980 | |
8981 | Result = std::move(BytesAvailable); |
8982 | return true; |
8983 | } |
8984 | |
8985 | /// Convenience function. LVal's base must be a call to an alloc_size |
8986 | /// function. |
8987 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8988 | const LValue &LVal, |
8989 | llvm::APInt &Result) { |
8990 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8991 | "Can't get the size of a non alloc_size function" ); |
8992 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
8993 | const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base); |
8994 | return getBytesReturnedByAllocSizeCall(Ctx, Call: CE, Result); |
8995 | } |
8996 | |
8997 | /// Attempts to evaluate the given LValueBase as the result of a call to |
8998 | /// a function with the alloc_size attribute. If it was possible to do so, this |
8999 | /// function will return true, make Result's Base point to said function call, |
9000 | /// and mark Result's Base as invalid. |
9001 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
9002 | LValue &Result) { |
9003 | if (Base.isNull()) |
9004 | return false; |
9005 | |
9006 | // Because we do no form of static analysis, we only support const variables. |
9007 | // |
9008 | // Additionally, we can't support parameters, nor can we support static |
9009 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
9010 | // we have no clue what they'll be assigned to). |
9011 | const auto *VD = |
9012 | dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>()); |
9013 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
9014 | return false; |
9015 | |
9016 | const Expr *Init = VD->getAnyInitializer(); |
9017 | if (!Init || Init->getType().isNull()) |
9018 | return false; |
9019 | |
9020 | const Expr *E = Init->IgnoreParens(); |
9021 | if (!tryUnwrapAllocSizeCall(E)) |
9022 | return false; |
9023 | |
9024 | // Store E instead of E unwrapped so that the type of the LValue's base is |
9025 | // what the user wanted. |
9026 | Result.setInvalid(B: E); |
9027 | |
9028 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
9029 | Result.addUnsizedArray(Info, E, ElemTy: Pointee); |
9030 | return true; |
9031 | } |
9032 | |
9033 | namespace { |
9034 | class PointerExprEvaluator |
9035 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
9036 | LValue &Result; |
9037 | bool InvalidBaseOK; |
9038 | |
9039 | bool Success(const Expr *E) { |
9040 | Result.set(B: E); |
9041 | return true; |
9042 | } |
9043 | |
9044 | bool evaluateLValue(const Expr *E, LValue &Result) { |
9045 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
9046 | } |
9047 | |
9048 | bool evaluatePointer(const Expr *E, LValue &Result) { |
9049 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
9050 | } |
9051 | |
9052 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
9053 | public: |
9054 | |
9055 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
9056 | : ExprEvaluatorBaseTy(info), Result(Result), |
9057 | InvalidBaseOK(InvalidBaseOK) {} |
9058 | |
9059 | bool Success(const APValue &V, const Expr *E) { |
9060 | Result.setFrom(Ctx&: Info.Ctx, V); |
9061 | return true; |
9062 | } |
9063 | bool ZeroInitialization(const Expr *E) { |
9064 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
9065 | return true; |
9066 | } |
9067 | |
9068 | bool VisitBinaryOperator(const BinaryOperator *E); |
9069 | bool VisitCastExpr(const CastExpr* E); |
9070 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
9071 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
9072 | { return Success(E); } |
9073 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
9074 | if (E->isExpressibleAsConstantInitializer()) |
9075 | return Success(E); |
9076 | if (Info.noteFailure()) |
9077 | EvaluateIgnoredValue(Info, E: E->getSubExpr()); |
9078 | return Error(E); |
9079 | } |
9080 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
9081 | { return Success(E); } |
9082 | bool VisitCallExpr(const CallExpr *E); |
9083 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
9084 | bool VisitBlockExpr(const BlockExpr *E) { |
9085 | if (!E->getBlockDecl()->hasCaptures()) |
9086 | return Success(E); |
9087 | return Error(E); |
9088 | } |
9089 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
9090 | auto DiagnoseInvalidUseOfThis = [&] { |
9091 | if (Info.getLangOpts().CPlusPlus11) |
9092 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
9093 | else |
9094 | Info.FFDiag(E); |
9095 | }; |
9096 | |
9097 | // Can't look at 'this' when checking a potential constant expression. |
9098 | if (Info.checkingPotentialConstantExpression()) |
9099 | return false; |
9100 | |
9101 | bool IsExplicitLambda = |
9102 | isLambdaCallWithExplicitObjectParameter(Info.CurrentCall->Callee); |
9103 | if (!IsExplicitLambda) { |
9104 | if (!Info.CurrentCall->This) { |
9105 | DiagnoseInvalidUseOfThis(); |
9106 | return false; |
9107 | } |
9108 | |
9109 | Result = *Info.CurrentCall->This; |
9110 | } |
9111 | |
9112 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
9113 | // Ensure we actually have captured 'this'. If something was wrong with |
9114 | // 'this' capture, the error would have been previously reported. |
9115 | // Otherwise we can be inside of a default initialization of an object |
9116 | // declared by lambda's body, so no need to return false. |
9117 | if (!Info.CurrentCall->LambdaThisCaptureField) { |
9118 | if (IsExplicitLambda && !Info.CurrentCall->This) { |
9119 | DiagnoseInvalidUseOfThis(); |
9120 | return false; |
9121 | } |
9122 | |
9123 | return true; |
9124 | } |
9125 | |
9126 | const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee); |
9127 | return HandleLambdaCapture( |
9128 | Info, E, Result, MD, Info.CurrentCall->LambdaThisCaptureField, |
9129 | Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType()); |
9130 | } |
9131 | return true; |
9132 | } |
9133 | |
9134 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
9135 | |
9136 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
9137 | assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?" ); |
9138 | APValue LValResult = E->EvaluateInContext( |
9139 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
9140 | Result.setFrom(Ctx&: Info.Ctx, V: LValResult); |
9141 | return true; |
9142 | } |
9143 | |
9144 | bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) { |
9145 | std::string ResultStr = E->ComputeName(Context&: Info.Ctx); |
9146 | |
9147 | QualType CharTy = Info.Ctx.CharTy.withConst(); |
9148 | APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()), |
9149 | ResultStr.size() + 1); |
9150 | QualType ArrayTy = Info.Ctx.getConstantArrayType( |
9151 | EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9152 | |
9153 | StringLiteral *SL = |
9154 | StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary, |
9155 | /*Pascal*/ false, Ty: ArrayTy, Loc: E->getLocation()); |
9156 | |
9157 | evaluateLValue(SL, Result); |
9158 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: ArrayTy)); |
9159 | return true; |
9160 | } |
9161 | |
9162 | // FIXME: Missing: @protocol, @selector |
9163 | }; |
9164 | } // end anonymous namespace |
9165 | |
9166 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
9167 | bool InvalidBaseOK) { |
9168 | assert(!E->isValueDependent()); |
9169 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
9170 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
9171 | } |
9172 | |
9173 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
9174 | if (E->getOpcode() != BO_Add && |
9175 | E->getOpcode() != BO_Sub) |
9176 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
9177 | |
9178 | const Expr *PExp = E->getLHS(); |
9179 | const Expr *IExp = E->getRHS(); |
9180 | if (IExp->getType()->isPointerType()) |
9181 | std::swap(a&: PExp, b&: IExp); |
9182 | |
9183 | bool EvalPtrOK = evaluatePointer(E: PExp, Result); |
9184 | if (!EvalPtrOK && !Info.noteFailure()) |
9185 | return false; |
9186 | |
9187 | llvm::APSInt Offset; |
9188 | if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK) |
9189 | return false; |
9190 | |
9191 | if (E->getOpcode() == BO_Sub) |
9192 | negateAsSigned(Int&: Offset); |
9193 | |
9194 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
9195 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
9196 | } |
9197 | |
9198 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
9199 | return evaluateLValue(E: E->getSubExpr(), Result); |
9200 | } |
9201 | |
9202 | // Is the provided decl 'std::source_location::current'? |
9203 | static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) { |
9204 | if (!FD) |
9205 | return false; |
9206 | const IdentifierInfo *FnII = FD->getIdentifier(); |
9207 | if (!FnII || !FnII->isStr(Str: "current" )) |
9208 | return false; |
9209 | |
9210 | const auto *RD = dyn_cast<RecordDecl>(FD->getParent()); |
9211 | if (!RD) |
9212 | return false; |
9213 | |
9214 | const IdentifierInfo *ClassII = RD->getIdentifier(); |
9215 | return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location" ); |
9216 | } |
9217 | |
9218 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9219 | const Expr *SubExpr = E->getSubExpr(); |
9220 | |
9221 | switch (E->getCastKind()) { |
9222 | default: |
9223 | break; |
9224 | case CK_BitCast: |
9225 | case CK_CPointerToObjCPointerCast: |
9226 | case CK_BlockPointerToObjCPointerCast: |
9227 | case CK_AnyPointerToBlockPointerCast: |
9228 | case CK_AddressSpaceConversion: |
9229 | if (!Visit(SubExpr)) |
9230 | return false; |
9231 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
9232 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
9233 | // also static_casts, but we disallow them as a resolution to DR1312. |
9234 | if (!E->getType()->isVoidPointerType()) { |
9235 | // In some circumstances, we permit casting from void* to cv1 T*, when the |
9236 | // actual pointee object is actually a cv2 T. |
9237 | bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid && |
9238 | !Result.IsNullPtr; |
9239 | bool VoidPtrCastMaybeOK = |
9240 | HasValidResult && |
9241 | Info.Ctx.hasSameUnqualifiedType(T1: Result.Designator.getType(Info.Ctx), |
9242 | T2: E->getType()->getPointeeType()); |
9243 | // 1. We'll allow it in std::allocator::allocate, and anything which that |
9244 | // calls. |
9245 | // 2. HACK 2022-03-28: Work around an issue with libstdc++'s |
9246 | // <source_location> header. Fixed in GCC 12 and later (2022-04-??). |
9247 | // We'll allow it in the body of std::source_location::current. GCC's |
9248 | // implementation had a parameter of type `void*`, and casts from |
9249 | // that back to `const __impl*` in its body. |
9250 | if (VoidPtrCastMaybeOK && |
9251 | (Info.getStdAllocatorCaller(FnName: "allocate" ) || |
9252 | IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) || |
9253 | Info.getLangOpts().CPlusPlus26)) { |
9254 | // Permitted. |
9255 | } else { |
9256 | if (SubExpr->getType()->isVoidPointerType() && |
9257 | Info.getLangOpts().CPlusPlus) { |
9258 | if (HasValidResult) |
9259 | CCEDiag(E, diag::note_constexpr_invalid_void_star_cast) |
9260 | << SubExpr->getType() << Info.getLangOpts().CPlusPlus26 |
9261 | << Result.Designator.getType(Info.Ctx).getCanonicalType() |
9262 | << E->getType()->getPointeeType(); |
9263 | else |
9264 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9265 | << 3 << SubExpr->getType(); |
9266 | } else |
9267 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9268 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
9269 | Result.Designator.setInvalid(); |
9270 | } |
9271 | } |
9272 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
9273 | ZeroInitialization(E); |
9274 | return true; |
9275 | |
9276 | case CK_DerivedToBase: |
9277 | case CK_UncheckedDerivedToBase: |
9278 | if (!evaluatePointer(E: E->getSubExpr(), Result)) |
9279 | return false; |
9280 | if (!Result.Base && Result.Offset.isZero()) |
9281 | return true; |
9282 | |
9283 | // Now figure out the necessary offset to add to the base LV to get from |
9284 | // the derived class to the base class. |
9285 | return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()-> |
9286 | castAs<PointerType>()->getPointeeType(), |
9287 | Result); |
9288 | |
9289 | case CK_BaseToDerived: |
9290 | if (!Visit(E->getSubExpr())) |
9291 | return false; |
9292 | if (!Result.Base && Result.Offset.isZero()) |
9293 | return true; |
9294 | return HandleBaseToDerivedCast(Info, E, Result); |
9295 | |
9296 | case CK_Dynamic: |
9297 | if (!Visit(E->getSubExpr())) |
9298 | return false; |
9299 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
9300 | |
9301 | case CK_NullToPointer: |
9302 | VisitIgnoredValue(E: E->getSubExpr()); |
9303 | return ZeroInitialization(E); |
9304 | |
9305 | case CK_IntegralToPointer: { |
9306 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9307 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
9308 | |
9309 | APValue Value; |
9310 | if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info)) |
9311 | break; |
9312 | |
9313 | if (Value.isInt()) { |
9314 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
9315 | uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue(); |
9316 | Result.Base = (Expr*)nullptr; |
9317 | Result.InvalidBase = false; |
9318 | Result.Offset = CharUnits::fromQuantity(Quantity: N); |
9319 | Result.Designator.setInvalid(); |
9320 | Result.IsNullPtr = false; |
9321 | return true; |
9322 | } else { |
9323 | // Cast is of an lvalue, no need to change value. |
9324 | Result.setFrom(Ctx&: Info.Ctx, V: Value); |
9325 | return true; |
9326 | } |
9327 | } |
9328 | |
9329 | case CK_ArrayToPointerDecay: { |
9330 | if (SubExpr->isGLValue()) { |
9331 | if (!evaluateLValue(E: SubExpr, Result)) |
9332 | return false; |
9333 | } else { |
9334 | APValue &Value = Info.CurrentCall->createTemporary( |
9335 | Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
9336 | if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr)) |
9337 | return false; |
9338 | } |
9339 | // The result is a pointer to the first element of the array. |
9340 | auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType()); |
9341 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
9342 | Result.addArray(Info, E, CAT); |
9343 | else |
9344 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
9345 | return true; |
9346 | } |
9347 | |
9348 | case CK_FunctionToPointerDecay: |
9349 | return evaluateLValue(E: SubExpr, Result); |
9350 | |
9351 | case CK_LValueToRValue: { |
9352 | LValue LVal; |
9353 | if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal)) |
9354 | return false; |
9355 | |
9356 | APValue RVal; |
9357 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
9358 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
9359 | LVal, RVal)) |
9360 | return InvalidBaseOK && |
9361 | evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result); |
9362 | return Success(RVal, E); |
9363 | } |
9364 | } |
9365 | |
9366 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9367 | } |
9368 | |
9369 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
9370 | UnaryExprOrTypeTrait ExprKind) { |
9371 | // C++ [expr.alignof]p3: |
9372 | // When alignof is applied to a reference type, the result is the |
9373 | // alignment of the referenced type. |
9374 | T = T.getNonReferenceType(); |
9375 | |
9376 | if (T.getQualifiers().hasUnaligned()) |
9377 | return CharUnits::One(); |
9378 | |
9379 | const bool AlignOfReturnsPreferred = |
9380 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
9381 | |
9382 | // __alignof is defined to return the preferred alignment. |
9383 | // Before 8, clang returned the preferred alignment for alignof and _Alignof |
9384 | // as well. |
9385 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
9386 | return Info.Ctx.toCharUnitsFromBits( |
9387 | BitSize: Info.Ctx.getPreferredTypeAlign(T: T.getTypePtr())); |
9388 | // alignof and _Alignof are defined to return the ABI alignment. |
9389 | else if (ExprKind == UETT_AlignOf) |
9390 | return Info.Ctx.getTypeAlignInChars(T: T.getTypePtr()); |
9391 | else |
9392 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind" ); |
9393 | } |
9394 | |
9395 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
9396 | UnaryExprOrTypeTrait ExprKind) { |
9397 | E = E->IgnoreParens(); |
9398 | |
9399 | // The kinds of expressions that we have special-case logic here for |
9400 | // should be kept up to date with the special checks for those |
9401 | // expressions in Sema. |
9402 | |
9403 | // alignof decl is always accepted, even if it doesn't make sense: we default |
9404 | // to 1 in those cases. |
9405 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E)) |
9406 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
9407 | /*RefAsPointee*/true); |
9408 | |
9409 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E)) |
9410 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
9411 | /*RefAsPointee*/true); |
9412 | |
9413 | return GetAlignOfType(Info, T: E->getType(), ExprKind); |
9414 | } |
9415 | |
9416 | static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) { |
9417 | if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>()) |
9418 | return Info.Ctx.getDeclAlign(VD); |
9419 | if (const auto *E = Value.Base.dyn_cast<const Expr *>()) |
9420 | return GetAlignOfExpr(Info, E, ExprKind: UETT_AlignOf); |
9421 | return GetAlignOfType(Info, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf); |
9422 | } |
9423 | |
9424 | /// Evaluate the value of the alignment argument to __builtin_align_{up,down}, |
9425 | /// __builtin_is_aligned and __builtin_assume_aligned. |
9426 | static bool getAlignmentArgument(const Expr *E, QualType ForType, |
9427 | EvalInfo &Info, APSInt &Alignment) { |
9428 | if (!EvaluateInteger(E, Result&: Alignment, Info)) |
9429 | return false; |
9430 | if (Alignment < 0 || !Alignment.isPowerOf2()) { |
9431 | Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment; |
9432 | return false; |
9433 | } |
9434 | unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType); |
9435 | APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1)); |
9436 | if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) { |
9437 | Info.FFDiag(E, diag::note_constexpr_alignment_too_big) |
9438 | << MaxValue << ForType << Alignment; |
9439 | return false; |
9440 | } |
9441 | // Ensure both alignment and source value have the same bit width so that we |
9442 | // don't assert when computing the resulting value. |
9443 | APSInt ExtAlignment = |
9444 | APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true); |
9445 | assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 && |
9446 | "Alignment should not be changed by ext/trunc" ); |
9447 | Alignment = ExtAlignment; |
9448 | assert(Alignment.getBitWidth() == SrcWidth); |
9449 | return true; |
9450 | } |
9451 | |
9452 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
9453 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
9454 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
9455 | return true; |
9456 | |
9457 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
9458 | return false; |
9459 | |
9460 | Result.setInvalid(E); |
9461 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
9462 | Result.addUnsizedArray(Info, E, PointeeTy); |
9463 | return true; |
9464 | } |
9465 | |
9466 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
9467 | if (!IsConstantEvaluatedBuiltinCall(E)) |
9468 | return visitNonBuiltinCallExpr(E); |
9469 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
9470 | } |
9471 | |
9472 | // Determine if T is a character type for which we guarantee that |
9473 | // sizeof(T) == 1. |
9474 | static bool isOneByteCharacterType(QualType T) { |
9475 | return T->isCharType() || T->isChar8Type(); |
9476 | } |
9477 | |
9478 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
9479 | unsigned BuiltinOp) { |
9480 | if (IsNoOpCall(E)) |
9481 | return Success(E); |
9482 | |
9483 | switch (BuiltinOp) { |
9484 | case Builtin::BIaddressof: |
9485 | case Builtin::BI__addressof: |
9486 | case Builtin::BI__builtin_addressof: |
9487 | return evaluateLValue(E: E->getArg(Arg: 0), Result); |
9488 | case Builtin::BI__builtin_assume_aligned: { |
9489 | // We need to be very careful here because: if the pointer does not have the |
9490 | // asserted alignment, then the behavior is undefined, and undefined |
9491 | // behavior is non-constant. |
9492 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
9493 | return false; |
9494 | |
9495 | LValue OffsetResult(Result); |
9496 | APSInt Alignment; |
9497 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
9498 | Alignment)) |
9499 | return false; |
9500 | CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue()); |
9501 | |
9502 | if (E->getNumArgs() > 2) { |
9503 | APSInt Offset; |
9504 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info)) |
9505 | return false; |
9506 | |
9507 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
9508 | OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset); |
9509 | } |
9510 | |
9511 | // If there is a base object, then it must have the correct alignment. |
9512 | if (OffsetResult.Base) { |
9513 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult); |
9514 | |
9515 | if (BaseAlignment < Align) { |
9516 | Result.Designator.setInvalid(); |
9517 | // FIXME: Add support to Diagnostic for long / long long. |
9518 | CCEDiag(E->getArg(0), |
9519 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
9520 | << (unsigned)BaseAlignment.getQuantity() |
9521 | << (unsigned)Align.getQuantity(); |
9522 | return false; |
9523 | } |
9524 | } |
9525 | |
9526 | // The offset must also have the correct alignment. |
9527 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
9528 | Result.Designator.setInvalid(); |
9529 | |
9530 | (OffsetResult.Base |
9531 | ? CCEDiag(E->getArg(0), |
9532 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
9533 | : CCEDiag(E->getArg(0), |
9534 | diag::note_constexpr_baa_value_insufficient_alignment)) |
9535 | << (int)OffsetResult.Offset.getQuantity() |
9536 | << (unsigned)Align.getQuantity(); |
9537 | return false; |
9538 | } |
9539 | |
9540 | return true; |
9541 | } |
9542 | case Builtin::BI__builtin_align_up: |
9543 | case Builtin::BI__builtin_align_down: { |
9544 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
9545 | return false; |
9546 | APSInt Alignment; |
9547 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
9548 | Alignment)) |
9549 | return false; |
9550 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result); |
9551 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset); |
9552 | // For align_up/align_down, we can return the same value if the alignment |
9553 | // is known to be greater or equal to the requested value. |
9554 | if (PtrAlign.getQuantity() >= Alignment) |
9555 | return true; |
9556 | |
9557 | // The alignment could be greater than the minimum at run-time, so we cannot |
9558 | // infer much about the resulting pointer value. One case is possible: |
9559 | // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we |
9560 | // can infer the correct index if the requested alignment is smaller than |
9561 | // the base alignment so we can perform the computation on the offset. |
9562 | if (BaseAlignment.getQuantity() >= Alignment) { |
9563 | assert(Alignment.getBitWidth() <= 64 && |
9564 | "Cannot handle > 64-bit address-space" ); |
9565 | uint64_t Alignment64 = Alignment.getZExtValue(); |
9566 | CharUnits NewOffset = CharUnits::fromQuantity( |
9567 | BuiltinOp == Builtin::BI__builtin_align_down |
9568 | ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64) |
9569 | : llvm::alignTo(Result.Offset.getQuantity(), Alignment64)); |
9570 | Result.adjustOffset(N: NewOffset - Result.Offset); |
9571 | // TODO: diagnose out-of-bounds values/only allow for arrays? |
9572 | return true; |
9573 | } |
9574 | // Otherwise, we cannot constant-evaluate the result. |
9575 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust) |
9576 | << Alignment; |
9577 | return false; |
9578 | } |
9579 | case Builtin::BI__builtin_operator_new: |
9580 | return HandleOperatorNewCall(Info, E, Result); |
9581 | case Builtin::BI__builtin_launder: |
9582 | return evaluatePointer(E: E->getArg(Arg: 0), Result); |
9583 | case Builtin::BIstrchr: |
9584 | case Builtin::BIwcschr: |
9585 | case Builtin::BImemchr: |
9586 | case Builtin::BIwmemchr: |
9587 | if (Info.getLangOpts().CPlusPlus11) |
9588 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9589 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
9590 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
9591 | else |
9592 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9593 | [[fallthrough]]; |
9594 | case Builtin::BI__builtin_strchr: |
9595 | case Builtin::BI__builtin_wcschr: |
9596 | case Builtin::BI__builtin_memchr: |
9597 | case Builtin::BI__builtin_char_memchr: |
9598 | case Builtin::BI__builtin_wmemchr: { |
9599 | if (!Visit(E->getArg(Arg: 0))) |
9600 | return false; |
9601 | APSInt Desired; |
9602 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info)) |
9603 | return false; |
9604 | uint64_t MaxLength = uint64_t(-1); |
9605 | if (BuiltinOp != Builtin::BIstrchr && |
9606 | BuiltinOp != Builtin::BIwcschr && |
9607 | BuiltinOp != Builtin::BI__builtin_strchr && |
9608 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
9609 | APSInt N; |
9610 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
9611 | return false; |
9612 | MaxLength = N.getZExtValue(); |
9613 | } |
9614 | // We cannot find the value if there are no candidates to match against. |
9615 | if (MaxLength == 0u) |
9616 | return ZeroInitialization(E); |
9617 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
9618 | Result.Designator.Invalid) |
9619 | return false; |
9620 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
9621 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
9622 | BuiltinOp == Builtin::BI__builtin_memchr; |
9623 | assert(IsRawByte || |
9624 | Info.Ctx.hasSameUnqualifiedType( |
9625 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
9626 | // Pointers to const void may point to objects of incomplete type. |
9627 | if (IsRawByte && CharTy->isIncompleteType()) { |
9628 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
9629 | return false; |
9630 | } |
9631 | // Give up on byte-oriented matching against multibyte elements. |
9632 | // FIXME: We can compare the bytes in the correct order. |
9633 | if (IsRawByte && !isOneByteCharacterType(T: CharTy)) { |
9634 | Info.FFDiag(E, diag::note_constexpr_memchr_unsupported) |
9635 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str() |
9636 | << CharTy; |
9637 | return false; |
9638 | } |
9639 | // Figure out what value we're actually looking for (after converting to |
9640 | // the corresponding unsigned type if necessary). |
9641 | uint64_t DesiredVal; |
9642 | bool StopAtNull = false; |
9643 | switch (BuiltinOp) { |
9644 | case Builtin::BIstrchr: |
9645 | case Builtin::BI__builtin_strchr: |
9646 | // strchr compares directly to the passed integer, and therefore |
9647 | // always fails if given an int that is not a char. |
9648 | if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, CharTy, |
9649 | E->getArg(Arg: 1)->getType(), |
9650 | Desired), |
9651 | I2: Desired)) |
9652 | return ZeroInitialization(E); |
9653 | StopAtNull = true; |
9654 | [[fallthrough]]; |
9655 | case Builtin::BImemchr: |
9656 | case Builtin::BI__builtin_memchr: |
9657 | case Builtin::BI__builtin_char_memchr: |
9658 | // memchr compares by converting both sides to unsigned char. That's also |
9659 | // correct for strchr if we get this far (to cope with plain char being |
9660 | // unsigned in the strchr case). |
9661 | DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue(); |
9662 | break; |
9663 | |
9664 | case Builtin::BIwcschr: |
9665 | case Builtin::BI__builtin_wcschr: |
9666 | StopAtNull = true; |
9667 | [[fallthrough]]; |
9668 | case Builtin::BIwmemchr: |
9669 | case Builtin::BI__builtin_wmemchr: |
9670 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
9671 | DesiredVal = Desired.getZExtValue(); |
9672 | break; |
9673 | } |
9674 | |
9675 | for (; MaxLength; --MaxLength) { |
9676 | APValue Char; |
9677 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
9678 | !Char.isInt()) |
9679 | return false; |
9680 | if (Char.getInt().getZExtValue() == DesiredVal) |
9681 | return true; |
9682 | if (StopAtNull && !Char.getInt()) |
9683 | break; |
9684 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
9685 | return false; |
9686 | } |
9687 | // Not found: return nullptr. |
9688 | return ZeroInitialization(E); |
9689 | } |
9690 | |
9691 | case Builtin::BImemcpy: |
9692 | case Builtin::BImemmove: |
9693 | case Builtin::BIwmemcpy: |
9694 | case Builtin::BIwmemmove: |
9695 | if (Info.getLangOpts().CPlusPlus11) |
9696 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9697 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
9698 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
9699 | else |
9700 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9701 | [[fallthrough]]; |
9702 | case Builtin::BI__builtin_memcpy: |
9703 | case Builtin::BI__builtin_memmove: |
9704 | case Builtin::BI__builtin_wmemcpy: |
9705 | case Builtin::BI__builtin_wmemmove: { |
9706 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
9707 | BuiltinOp == Builtin::BIwmemmove || |
9708 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
9709 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9710 | bool Move = BuiltinOp == Builtin::BImemmove || |
9711 | BuiltinOp == Builtin::BIwmemmove || |
9712 | BuiltinOp == Builtin::BI__builtin_memmove || |
9713 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9714 | |
9715 | // The result of mem* is the first argument. |
9716 | if (!Visit(E->getArg(Arg: 0))) |
9717 | return false; |
9718 | LValue Dest = Result; |
9719 | |
9720 | LValue Src; |
9721 | if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info)) |
9722 | return false; |
9723 | |
9724 | APSInt N; |
9725 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
9726 | return false; |
9727 | assert(!N.isSigned() && "memcpy and friends take an unsigned size" ); |
9728 | |
9729 | // If the size is zero, we treat this as always being a valid no-op. |
9730 | // (Even if one of the src and dest pointers is null.) |
9731 | if (!N) |
9732 | return true; |
9733 | |
9734 | // Otherwise, if either of the operands is null, we can't proceed. Don't |
9735 | // try to determine the type of the copied objects, because there aren't |
9736 | // any. |
9737 | if (!Src.Base || !Dest.Base) { |
9738 | APValue Val; |
9739 | (!Src.Base ? Src : Dest).moveInto(V&: Val); |
9740 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
9741 | << Move << WChar << !!Src.Base |
9742 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
9743 | return false; |
9744 | } |
9745 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
9746 | return false; |
9747 | |
9748 | // We require that Src and Dest are both pointers to arrays of |
9749 | // trivially-copyable type. (For the wide version, the designator will be |
9750 | // invalid if the designated object is not a wchar_t.) |
9751 | QualType T = Dest.Designator.getType(Info.Ctx); |
9752 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
9753 | if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) { |
9754 | // FIXME: Consider using our bit_cast implementation to support this. |
9755 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
9756 | return false; |
9757 | } |
9758 | if (T->isIncompleteType()) { |
9759 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
9760 | return false; |
9761 | } |
9762 | if (!T.isTriviallyCopyableType(Context: Info.Ctx)) { |
9763 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
9764 | return false; |
9765 | } |
9766 | |
9767 | // Figure out how many T's we're copying. |
9768 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
9769 | if (TSize == 0) |
9770 | return false; |
9771 | if (!WChar) { |
9772 | uint64_t Remainder; |
9773 | llvm::APInt OrigN = N; |
9774 | llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder); |
9775 | if (Remainder) { |
9776 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9777 | << Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false) |
9778 | << (unsigned)TSize; |
9779 | return false; |
9780 | } |
9781 | } |
9782 | |
9783 | // Check that the copying will remain within the arrays, just so that we |
9784 | // can give a more meaningful diagnostic. This implicitly also checks that |
9785 | // N fits into 64 bits. |
9786 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
9787 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
9788 | if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) { |
9789 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9790 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
9791 | << toString(N, 10, /*Signed*/false); |
9792 | return false; |
9793 | } |
9794 | uint64_t NElems = N.getZExtValue(); |
9795 | uint64_t NBytes = NElems * TSize; |
9796 | |
9797 | // Check for overlap. |
9798 | int Direction = 1; |
9799 | if (HasSameBase(A: Src, B: Dest)) { |
9800 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
9801 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
9802 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
9803 | // Dest is inside the source region. |
9804 | if (!Move) { |
9805 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9806 | return false; |
9807 | } |
9808 | // For memmove and friends, copy backwards. |
9809 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
9810 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
9811 | return false; |
9812 | Direction = -1; |
9813 | } else if (!Move && SrcOffset >= DestOffset && |
9814 | SrcOffset - DestOffset < NBytes) { |
9815 | // Src is inside the destination region for memcpy: invalid. |
9816 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9817 | return false; |
9818 | } |
9819 | } |
9820 | |
9821 | while (true) { |
9822 | APValue Val; |
9823 | // FIXME: Set WantObjectRepresentation to true if we're copying a |
9824 | // char-like type? |
9825 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
9826 | !handleAssignment(Info, E, Dest, T, Val)) |
9827 | return false; |
9828 | // Do not iterate past the last element; if we're copying backwards, that |
9829 | // might take us off the start of the array. |
9830 | if (--NElems == 0) |
9831 | return true; |
9832 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
9833 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
9834 | return false; |
9835 | } |
9836 | } |
9837 | |
9838 | default: |
9839 | return false; |
9840 | } |
9841 | } |
9842 | |
9843 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
9844 | APValue &Result, const InitListExpr *ILE, |
9845 | QualType AllocType); |
9846 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
9847 | APValue &Result, |
9848 | const CXXConstructExpr *CCE, |
9849 | QualType AllocType); |
9850 | |
9851 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
9852 | if (!Info.getLangOpts().CPlusPlus20) |
9853 | Info.CCEDiag(E, diag::note_constexpr_new); |
9854 | |
9855 | // We cannot speculatively evaluate a delete expression. |
9856 | if (Info.SpeculativeEvaluationDepth) |
9857 | return false; |
9858 | |
9859 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
9860 | |
9861 | bool IsNothrow = false; |
9862 | bool IsPlacement = false; |
9863 | if (OperatorNew->isReservedGlobalPlacementOperator() && |
9864 | Info.CurrentCall->isStdFunction() && !E->isArray()) { |
9865 | // FIXME Support array placement new. |
9866 | assert(E->getNumPlacementArgs() == 1); |
9867 | if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info)) |
9868 | return false; |
9869 | if (Result.Designator.Invalid) |
9870 | return false; |
9871 | IsPlacement = true; |
9872 | } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) { |
9873 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
9874 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
9875 | return false; |
9876 | } else if (E->getNumPlacementArgs()) { |
9877 | // The only new-placement list we support is of the form (std::nothrow). |
9878 | // |
9879 | // FIXME: There is no restriction on this, but it's not clear that any |
9880 | // other form makes any sense. We get here for cases such as: |
9881 | // |
9882 | // new (std::align_val_t{N}) X(int) |
9883 | // |
9884 | // (which should presumably be valid only if N is a multiple of |
9885 | // alignof(int), and in any case can't be deallocated unless N is |
9886 | // alignof(X) and X has new-extended alignment). |
9887 | if (E->getNumPlacementArgs() != 1 || |
9888 | !E->getPlacementArg(0)->getType()->isNothrowT()) |
9889 | return Error(E, diag::note_constexpr_new_placement); |
9890 | |
9891 | LValue Nothrow; |
9892 | if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info)) |
9893 | return false; |
9894 | IsNothrow = true; |
9895 | } |
9896 | |
9897 | const Expr *Init = E->getInitializer(); |
9898 | const InitListExpr *ResizedArrayILE = nullptr; |
9899 | const CXXConstructExpr *ResizedArrayCCE = nullptr; |
9900 | bool ValueInit = false; |
9901 | |
9902 | QualType AllocType = E->getAllocatedType(); |
9903 | if (std::optional<const Expr *> ArraySize = E->getArraySize()) { |
9904 | const Expr *Stripped = *ArraySize; |
9905 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped); |
9906 | Stripped = ICE->getSubExpr()) |
9907 | if (ICE->getCastKind() != CK_NoOp && |
9908 | ICE->getCastKind() != CK_IntegralCast) |
9909 | break; |
9910 | |
9911 | llvm::APSInt ArrayBound; |
9912 | if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info)) |
9913 | return false; |
9914 | |
9915 | // C++ [expr.new]p9: |
9916 | // The expression is erroneous if: |
9917 | // -- [...] its value before converting to size_t [or] applying the |
9918 | // second standard conversion sequence is less than zero |
9919 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
9920 | if (IsNothrow) |
9921 | return ZeroInitialization(E); |
9922 | |
9923 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
9924 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9925 | return false; |
9926 | } |
9927 | |
9928 | // -- its value is such that the size of the allocated object would |
9929 | // exceed the implementation-defined limit |
9930 | if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(), |
9931 | BitWidth: ConstantArrayType::getNumAddressingBits( |
9932 | Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound), |
9933 | ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) { |
9934 | if (IsNothrow) |
9935 | return ZeroInitialization(E); |
9936 | return false; |
9937 | } |
9938 | |
9939 | // -- the new-initializer is a braced-init-list and the number of |
9940 | // array elements for which initializers are provided [...] |
9941 | // exceeds the number of elements to initialize |
9942 | if (!Init) { |
9943 | // No initialization is performed. |
9944 | } else if (isa<CXXScalarValueInitExpr>(Val: Init) || |
9945 | isa<ImplicitValueInitExpr>(Val: Init)) { |
9946 | ValueInit = true; |
9947 | } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) { |
9948 | ResizedArrayCCE = CCE; |
9949 | } else { |
9950 | auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType()); |
9951 | assert(CAT && "unexpected type for array initializer" ); |
9952 | |
9953 | unsigned Bits = |
9954 | std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth()); |
9955 | llvm::APInt InitBound = CAT->getSize().zext(width: Bits); |
9956 | llvm::APInt AllocBound = ArrayBound.zext(width: Bits); |
9957 | if (InitBound.ugt(RHS: AllocBound)) { |
9958 | if (IsNothrow) |
9959 | return ZeroInitialization(E); |
9960 | |
9961 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
9962 | << toString(AllocBound, 10, /*Signed=*/false) |
9963 | << toString(InitBound, 10, /*Signed=*/false) |
9964 | << (*ArraySize)->getSourceRange(); |
9965 | return false; |
9966 | } |
9967 | |
9968 | // If the sizes differ, we must have an initializer list, and we need |
9969 | // special handling for this case when we initialize. |
9970 | if (InitBound != AllocBound) |
9971 | ResizedArrayILE = cast<InitListExpr>(Val: Init); |
9972 | } |
9973 | |
9974 | AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr, |
9975 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9976 | } else { |
9977 | assert(!AllocType->isArrayType() && |
9978 | "array allocation with non-array new" ); |
9979 | } |
9980 | |
9981 | APValue *Val; |
9982 | if (IsPlacement) { |
9983 | AccessKinds AK = AK_Construct; |
9984 | struct FindObjectHandler { |
9985 | EvalInfo &Info; |
9986 | const Expr *E; |
9987 | QualType AllocType; |
9988 | const AccessKinds AccessKind; |
9989 | APValue *Value; |
9990 | |
9991 | typedef bool result_type; |
9992 | bool failed() { return false; } |
9993 | bool found(APValue &Subobj, QualType SubobjType) { |
9994 | // FIXME: Reject the cases where [basic.life]p8 would not permit the |
9995 | // old name of the object to be used to name the new object. |
9996 | if (!Info.Ctx.hasSameUnqualifiedType(SubobjType, AllocType)) { |
9997 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) << |
9998 | SubobjType << AllocType; |
9999 | return false; |
10000 | } |
10001 | Value = &Subobj; |
10002 | return true; |
10003 | } |
10004 | bool found(APSInt &Value, QualType SubobjType) { |
10005 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
10006 | return false; |
10007 | } |
10008 | bool found(APFloat &Value, QualType SubobjType) { |
10009 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
10010 | return false; |
10011 | } |
10012 | } Handler = {Info, E, AllocType, AK, nullptr}; |
10013 | |
10014 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
10015 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
10016 | return false; |
10017 | |
10018 | Val = Handler.Value; |
10019 | |
10020 | // [basic.life]p1: |
10021 | // The lifetime of an object o of type T ends when [...] the storage |
10022 | // which the object occupies is [...] reused by an object that is not |
10023 | // nested within o (6.6.2). |
10024 | *Val = APValue(); |
10025 | } else { |
10026 | // Perform the allocation and obtain a pointer to the resulting object. |
10027 | Val = Info.createHeapAlloc(E, AllocType, Result); |
10028 | if (!Val) |
10029 | return false; |
10030 | } |
10031 | |
10032 | if (ValueInit) { |
10033 | ImplicitValueInitExpr VIE(AllocType); |
10034 | if (!EvaluateInPlace(*Val, Info, Result, &VIE)) |
10035 | return false; |
10036 | } else if (ResizedArrayILE) { |
10037 | if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE, |
10038 | AllocType)) |
10039 | return false; |
10040 | } else if (ResizedArrayCCE) { |
10041 | if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE, |
10042 | AllocType)) |
10043 | return false; |
10044 | } else if (Init) { |
10045 | if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init)) |
10046 | return false; |
10047 | } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) { |
10048 | return false; |
10049 | } |
10050 | |
10051 | // Array new returns a pointer to the first element, not a pointer to the |
10052 | // array. |
10053 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
10054 | Result.addArray(Info, E, CAT: cast<ConstantArrayType>(AT)); |
10055 | |
10056 | return true; |
10057 | } |
10058 | //===----------------------------------------------------------------------===// |
10059 | // Member Pointer Evaluation |
10060 | //===----------------------------------------------------------------------===// |
10061 | |
10062 | namespace { |
10063 | class MemberPointerExprEvaluator |
10064 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
10065 | MemberPtr &Result; |
10066 | |
10067 | bool Success(const ValueDecl *D) { |
10068 | Result = MemberPtr(D); |
10069 | return true; |
10070 | } |
10071 | public: |
10072 | |
10073 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
10074 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
10075 | |
10076 | bool Success(const APValue &V, const Expr *E) { |
10077 | Result.setFrom(V); |
10078 | return true; |
10079 | } |
10080 | bool ZeroInitialization(const Expr *E) { |
10081 | return Success(D: (const ValueDecl*)nullptr); |
10082 | } |
10083 | |
10084 | bool VisitCastExpr(const CastExpr *E); |
10085 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
10086 | }; |
10087 | } // end anonymous namespace |
10088 | |
10089 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
10090 | EvalInfo &Info) { |
10091 | assert(!E->isValueDependent()); |
10092 | assert(E->isPRValue() && E->getType()->isMemberPointerType()); |
10093 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
10094 | } |
10095 | |
10096 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10097 | switch (E->getCastKind()) { |
10098 | default: |
10099 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10100 | |
10101 | case CK_NullToMemberPointer: |
10102 | VisitIgnoredValue(E: E->getSubExpr()); |
10103 | return ZeroInitialization(E); |
10104 | |
10105 | case CK_BaseToDerivedMemberPointer: { |
10106 | if (!Visit(E->getSubExpr())) |
10107 | return false; |
10108 | if (E->path_empty()) |
10109 | return true; |
10110 | // Base-to-derived member pointer casts store the path in derived-to-base |
10111 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
10112 | // the wrong end of the derived->base arc, so stagger the path by one class. |
10113 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
10114 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
10115 | PathI != PathE; ++PathI) { |
10116 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase" ); |
10117 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
10118 | if (!Result.castToDerived(Derived)) |
10119 | return Error(E); |
10120 | } |
10121 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
10122 | if (!Result.castToDerived(Derived: FinalTy->getAsCXXRecordDecl())) |
10123 | return Error(E); |
10124 | return true; |
10125 | } |
10126 | |
10127 | case CK_DerivedToBaseMemberPointer: |
10128 | if (!Visit(E->getSubExpr())) |
10129 | return false; |
10130 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10131 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10132 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase" ); |
10133 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10134 | if (!Result.castToBase(Base)) |
10135 | return Error(E); |
10136 | } |
10137 | return true; |
10138 | } |
10139 | } |
10140 | |
10141 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
10142 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
10143 | // member can be formed. |
10144 | return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl()); |
10145 | } |
10146 | |
10147 | //===----------------------------------------------------------------------===// |
10148 | // Record Evaluation |
10149 | //===----------------------------------------------------------------------===// |
10150 | |
10151 | namespace { |
10152 | class RecordExprEvaluator |
10153 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
10154 | const LValue &This; |
10155 | APValue &Result; |
10156 | public: |
10157 | |
10158 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
10159 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
10160 | |
10161 | bool Success(const APValue &V, const Expr *E) { |
10162 | Result = V; |
10163 | return true; |
10164 | } |
10165 | bool ZeroInitialization(const Expr *E) { |
10166 | return ZeroInitialization(E, T: E->getType()); |
10167 | } |
10168 | bool ZeroInitialization(const Expr *E, QualType T); |
10169 | |
10170 | bool VisitCallExpr(const CallExpr *E) { |
10171 | return handleCallExpr(E, Result, ResultSlot: &This); |
10172 | } |
10173 | bool VisitCastExpr(const CastExpr *E); |
10174 | bool VisitInitListExpr(const InitListExpr *E); |
10175 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10176 | return VisitCXXConstructExpr(E, E->getType()); |
10177 | } |
10178 | bool VisitLambdaExpr(const LambdaExpr *E); |
10179 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
10180 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
10181 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
10182 | bool VisitBinCmp(const BinaryOperator *E); |
10183 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
10184 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
10185 | ArrayRef<Expr *> Args); |
10186 | }; |
10187 | } |
10188 | |
10189 | /// Perform zero-initialization on an object of non-union class type. |
10190 | /// C++11 [dcl.init]p5: |
10191 | /// To zero-initialize an object or reference of type T means: |
10192 | /// [...] |
10193 | /// -- if T is a (possibly cv-qualified) non-union class type, |
10194 | /// each non-static data member and each base-class subobject is |
10195 | /// zero-initialized |
10196 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
10197 | const RecordDecl *RD, |
10198 | const LValue &This, APValue &Result) { |
10199 | assert(!RD->isUnion() && "Expected non-union class type" ); |
10200 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD); |
10201 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
10202 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10203 | |
10204 | if (RD->isInvalidDecl()) return false; |
10205 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10206 | |
10207 | if (CD) { |
10208 | unsigned Index = 0; |
10209 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
10210 | End = CD->bases_end(); I != End; ++I, ++Index) { |
10211 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
10212 | LValue Subobject = This; |
10213 | if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout)) |
10214 | return false; |
10215 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
10216 | Result.getStructBase(i: Index))) |
10217 | return false; |
10218 | } |
10219 | } |
10220 | |
10221 | for (const auto *I : RD->fields()) { |
10222 | // -- if T is a reference type, no initialization is performed. |
10223 | if (I->isUnnamedBitField() || I->getType()->isReferenceType()) |
10224 | continue; |
10225 | |
10226 | LValue Subobject = This; |
10227 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout)) |
10228 | return false; |
10229 | |
10230 | ImplicitValueInitExpr VIE(I->getType()); |
10231 | if (!EvaluateInPlace( |
10232 | Result.getStructField(i: I->getFieldIndex()), Info, Subobject, &VIE)) |
10233 | return false; |
10234 | } |
10235 | |
10236 | return true; |
10237 | } |
10238 | |
10239 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
10240 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
10241 | if (RD->isInvalidDecl()) return false; |
10242 | if (RD->isUnion()) { |
10243 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
10244 | // object's first non-static named data member is zero-initialized |
10245 | RecordDecl::field_iterator I = RD->field_begin(); |
10246 | while (I != RD->field_end() && (*I)->isUnnamedBitField()) |
10247 | ++I; |
10248 | if (I == RD->field_end()) { |
10249 | Result = APValue((const FieldDecl*)nullptr); |
10250 | return true; |
10251 | } |
10252 | |
10253 | LValue Subobject = This; |
10254 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I)) |
10255 | return false; |
10256 | Result = APValue(*I); |
10257 | ImplicitValueInitExpr VIE(I->getType()); |
10258 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
10259 | } |
10260 | |
10261 | if (isa<CXXRecordDecl>(Val: RD) && cast<CXXRecordDecl>(Val: RD)->getNumVBases()) { |
10262 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
10263 | return false; |
10264 | } |
10265 | |
10266 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
10267 | } |
10268 | |
10269 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10270 | switch (E->getCastKind()) { |
10271 | default: |
10272 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10273 | |
10274 | case CK_ConstructorConversion: |
10275 | return Visit(E->getSubExpr()); |
10276 | |
10277 | case CK_DerivedToBase: |
10278 | case CK_UncheckedDerivedToBase: { |
10279 | APValue DerivedObject; |
10280 | if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr())) |
10281 | return false; |
10282 | if (!DerivedObject.isStruct()) |
10283 | return Error(E: E->getSubExpr()); |
10284 | |
10285 | // Derived-to-base rvalue conversion: just slice off the derived part. |
10286 | APValue *Value = &DerivedObject; |
10287 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
10288 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10289 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10290 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base" ); |
10291 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10292 | Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base)); |
10293 | RD = Base; |
10294 | } |
10295 | Result = *Value; |
10296 | return true; |
10297 | } |
10298 | } |
10299 | } |
10300 | |
10301 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10302 | if (E->isTransparent()) |
10303 | return Visit(E->getInit(Init: 0)); |
10304 | return VisitCXXParenListOrInitListExpr(E, E->inits()); |
10305 | } |
10306 | |
10307 | bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr( |
10308 | const Expr *ExprToVisit, ArrayRef<Expr *> Args) { |
10309 | const RecordDecl *RD = |
10310 | ExprToVisit->getType()->castAs<RecordType>()->getDecl(); |
10311 | if (RD->isInvalidDecl()) return false; |
10312 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10313 | auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD); |
10314 | |
10315 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
10316 | Info, |
10317 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
10318 | CXXRD && CXXRD->getNumBases()); |
10319 | |
10320 | if (RD->isUnion()) { |
10321 | const FieldDecl *Field; |
10322 | if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) { |
10323 | Field = ILE->getInitializedFieldInUnion(); |
10324 | } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) { |
10325 | Field = PLIE->getInitializedFieldInUnion(); |
10326 | } else { |
10327 | llvm_unreachable( |
10328 | "Expression is neither an init list nor a C++ paren list" ); |
10329 | } |
10330 | |
10331 | Result = APValue(Field); |
10332 | if (!Field) |
10333 | return true; |
10334 | |
10335 | // If the initializer list for a union does not contain any elements, the |
10336 | // first element of the union is value-initialized. |
10337 | // FIXME: The element should be initialized from an initializer list. |
10338 | // Is this difference ever observable for initializer lists which |
10339 | // we don't build? |
10340 | ImplicitValueInitExpr VIE(Field->getType()); |
10341 | const Expr *InitExpr = Args.empty() ? &VIE : Args[0]; |
10342 | |
10343 | LValue Subobject = This; |
10344 | if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout)) |
10345 | return false; |
10346 | |
10347 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10348 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10349 | isa<CXXDefaultInitExpr>(Val: InitExpr)); |
10350 | |
10351 | if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) { |
10352 | if (Field->isBitField()) |
10353 | return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(), |
10354 | FD: Field); |
10355 | return true; |
10356 | } |
10357 | |
10358 | return false; |
10359 | } |
10360 | |
10361 | if (!Result.hasValue()) |
10362 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
10363 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10364 | unsigned ElementNo = 0; |
10365 | bool Success = true; |
10366 | |
10367 | // Initialize base classes. |
10368 | if (CXXRD && CXXRD->getNumBases()) { |
10369 | for (const auto &Base : CXXRD->bases()) { |
10370 | assert(ElementNo < Args.size() && "missing init for base class" ); |
10371 | const Expr *Init = Args[ElementNo]; |
10372 | |
10373 | LValue Subobject = This; |
10374 | if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base)) |
10375 | return false; |
10376 | |
10377 | APValue &FieldVal = Result.getStructBase(i: ElementNo); |
10378 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) { |
10379 | if (!Info.noteFailure()) |
10380 | return false; |
10381 | Success = false; |
10382 | } |
10383 | ++ElementNo; |
10384 | } |
10385 | |
10386 | EvalObj.finishedConstructingBases(); |
10387 | } |
10388 | |
10389 | // Initialize members. |
10390 | for (const auto *Field : RD->fields()) { |
10391 | // Anonymous bit-fields are not considered members of the class for |
10392 | // purposes of aggregate initialization. |
10393 | if (Field->isUnnamedBitField()) |
10394 | continue; |
10395 | |
10396 | LValue Subobject = This; |
10397 | |
10398 | bool HaveInit = ElementNo < Args.size(); |
10399 | |
10400 | // FIXME: Diagnostics here should point to the end of the initializer |
10401 | // list, not the start. |
10402 | if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit, |
10403 | LVal&: Subobject, FD: Field, RL: &Layout)) |
10404 | return false; |
10405 | |
10406 | // Perform an implicit value-initialization for members beyond the end of |
10407 | // the initializer list. |
10408 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
10409 | const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE; |
10410 | |
10411 | if (Field->getType()->isIncompleteArrayType()) { |
10412 | if (auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType())) { |
10413 | if (!CAT->isZeroSize()) { |
10414 | // Bail out for now. This might sort of "work", but the rest of the |
10415 | // code isn't really prepared to handle it. |
10416 | Info.FFDiag(Init, diag::note_constexpr_unsupported_flexible_array); |
10417 | return false; |
10418 | } |
10419 | } |
10420 | } |
10421 | |
10422 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10423 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10424 | isa<CXXDefaultInitExpr>(Val: Init)); |
10425 | |
10426 | APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex()); |
10427 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) || |
10428 | (Field->isBitField() && !truncateBitfieldValue(Info, E: Init, |
10429 | Value&: FieldVal, FD: Field))) { |
10430 | if (!Info.noteFailure()) |
10431 | return false; |
10432 | Success = false; |
10433 | } |
10434 | } |
10435 | |
10436 | EvalObj.finishedConstructingFields(); |
10437 | |
10438 | return Success; |
10439 | } |
10440 | |
10441 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
10442 | QualType T) { |
10443 | // Note that E's type is not necessarily the type of our class here; we might |
10444 | // be initializing an array element instead. |
10445 | const CXXConstructorDecl *FD = E->getConstructor(); |
10446 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
10447 | |
10448 | bool ZeroInit = E->requiresZeroInitialization(); |
10449 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
10450 | // If we've already performed zero-initialization, we're already done. |
10451 | if (Result.hasValue()) |
10452 | return true; |
10453 | |
10454 | if (ZeroInit) |
10455 | return ZeroInitialization(E, T); |
10456 | |
10457 | return handleDefaultInitValue(T, Result); |
10458 | } |
10459 | |
10460 | const FunctionDecl *Definition = nullptr; |
10461 | auto Body = FD->getBody(Definition); |
10462 | |
10463 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10464 | return false; |
10465 | |
10466 | // Avoid materializing a temporary for an elidable copy/move constructor. |
10467 | if (E->isElidable() && !ZeroInit) { |
10468 | // FIXME: This only handles the simplest case, where the source object |
10469 | // is passed directly as the first argument to the constructor. |
10470 | // This should also handle stepping though implicit casts and |
10471 | // and conversion sequences which involve two steps, with a |
10472 | // conversion operator followed by a converting constructor. |
10473 | const Expr *SrcObj = E->getArg(Arg: 0); |
10474 | assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent())); |
10475 | assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType())); |
10476 | if (const MaterializeTemporaryExpr *ME = |
10477 | dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) |
10478 | return Visit(ME->getSubExpr()); |
10479 | } |
10480 | |
10481 | if (ZeroInit && !ZeroInitialization(E, T)) |
10482 | return false; |
10483 | |
10484 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
10485 | return HandleConstructorCall(E, This, Args, |
10486 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10487 | Result); |
10488 | } |
10489 | |
10490 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
10491 | const CXXInheritedCtorInitExpr *E) { |
10492 | if (!Info.CurrentCall) { |
10493 | assert(Info.checkingPotentialConstantExpression()); |
10494 | return false; |
10495 | } |
10496 | |
10497 | const CXXConstructorDecl *FD = E->getConstructor(); |
10498 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
10499 | return false; |
10500 | |
10501 | const FunctionDecl *Definition = nullptr; |
10502 | auto Body = FD->getBody(Definition); |
10503 | |
10504 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10505 | return false; |
10506 | |
10507 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
10508 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10509 | Result); |
10510 | } |
10511 | |
10512 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
10513 | const CXXStdInitializerListExpr *E) { |
10514 | const ConstantArrayType *ArrayType = |
10515 | Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType()); |
10516 | |
10517 | LValue Array; |
10518 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info)) |
10519 | return false; |
10520 | |
10521 | assert(ArrayType && "unexpected type for array initializer" ); |
10522 | |
10523 | // Get a pointer to the first element of the array. |
10524 | Array.addArray(Info, E, ArrayType); |
10525 | |
10526 | auto InvalidType = [&] { |
10527 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
10528 | << E->getType(); |
10529 | return false; |
10530 | }; |
10531 | |
10532 | // FIXME: Perform the checks on the field types in SemaInit. |
10533 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
10534 | RecordDecl::field_iterator Field = Record->field_begin(); |
10535 | if (Field == Record->field_end()) |
10536 | return InvalidType(); |
10537 | |
10538 | // Start pointer. |
10539 | if (!Field->getType()->isPointerType() || |
10540 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10541 | ArrayType->getElementType())) |
10542 | return InvalidType(); |
10543 | |
10544 | // FIXME: What if the initializer_list type has base classes, etc? |
10545 | Result = APValue(APValue::UninitStruct(), 0, 2); |
10546 | Array.moveInto(V&: Result.getStructField(i: 0)); |
10547 | |
10548 | if (++Field == Record->field_end()) |
10549 | return InvalidType(); |
10550 | |
10551 | if (Field->getType()->isPointerType() && |
10552 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10553 | ArrayType->getElementType())) { |
10554 | // End pointer. |
10555 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
10556 | ArrayType->getElementType(), |
10557 | ArrayType->getZExtSize())) |
10558 | return false; |
10559 | Array.moveInto(V&: Result.getStructField(i: 1)); |
10560 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
10561 | // Length. |
10562 | Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize())); |
10563 | else |
10564 | return InvalidType(); |
10565 | |
10566 | if (++Field != Record->field_end()) |
10567 | return InvalidType(); |
10568 | |
10569 | return true; |
10570 | } |
10571 | |
10572 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
10573 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
10574 | if (ClosureClass->isInvalidDecl()) |
10575 | return false; |
10576 | |
10577 | const size_t NumFields = |
10578 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
10579 | |
10580 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
10581 | E->capture_init_end()) && |
10582 | "The number of lambda capture initializers should equal the number of " |
10583 | "fields within the closure type" ); |
10584 | |
10585 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
10586 | // Iterate through all the lambda's closure object's fields and initialize |
10587 | // them. |
10588 | auto *CaptureInitIt = E->capture_init_begin(); |
10589 | bool Success = true; |
10590 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass); |
10591 | for (const auto *Field : ClosureClass->fields()) { |
10592 | assert(CaptureInitIt != E->capture_init_end()); |
10593 | // Get the initializer for this field |
10594 | Expr *const CurFieldInit = *CaptureInitIt++; |
10595 | |
10596 | // If there is no initializer, either this is a VLA or an error has |
10597 | // occurred. |
10598 | if (!CurFieldInit) |
10599 | return Error(E); |
10600 | |
10601 | LValue Subobject = This; |
10602 | |
10603 | if (!HandleLValueMember(Info, E, Subobject, Field, &Layout)) |
10604 | return false; |
10605 | |
10606 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
10607 | if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) { |
10608 | if (!Info.keepEvaluatingAfterFailure()) |
10609 | return false; |
10610 | Success = false; |
10611 | } |
10612 | } |
10613 | return Success; |
10614 | } |
10615 | |
10616 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
10617 | APValue &Result, EvalInfo &Info) { |
10618 | assert(!E->isValueDependent()); |
10619 | assert(E->isPRValue() && E->getType()->isRecordType() && |
10620 | "can't evaluate expression as a record rvalue" ); |
10621 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
10622 | } |
10623 | |
10624 | //===----------------------------------------------------------------------===// |
10625 | // Temporary Evaluation |
10626 | // |
10627 | // Temporaries are represented in the AST as rvalues, but generally behave like |
10628 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
10629 | // materialized so that a reference can bind to it. |
10630 | //===----------------------------------------------------------------------===// |
10631 | namespace { |
10632 | class TemporaryExprEvaluator |
10633 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
10634 | public: |
10635 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
10636 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
10637 | |
10638 | /// Visit an expression which constructs the value of this temporary. |
10639 | bool VisitConstructExpr(const Expr *E) { |
10640 | APValue &Value = Info.CurrentCall->createTemporary( |
10641 | Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
10642 | return EvaluateInPlace(Result&: Value, Info, This: Result, E); |
10643 | } |
10644 | |
10645 | bool VisitCastExpr(const CastExpr *E) { |
10646 | switch (E->getCastKind()) { |
10647 | default: |
10648 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
10649 | |
10650 | case CK_ConstructorConversion: |
10651 | return VisitConstructExpr(E: E->getSubExpr()); |
10652 | } |
10653 | } |
10654 | bool VisitInitListExpr(const InitListExpr *E) { |
10655 | return VisitConstructExpr(E); |
10656 | } |
10657 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10658 | return VisitConstructExpr(E); |
10659 | } |
10660 | bool VisitCallExpr(const CallExpr *E) { |
10661 | return VisitConstructExpr(E); |
10662 | } |
10663 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
10664 | return VisitConstructExpr(E); |
10665 | } |
10666 | bool VisitLambdaExpr(const LambdaExpr *E) { |
10667 | return VisitConstructExpr(E); |
10668 | } |
10669 | }; |
10670 | } // end anonymous namespace |
10671 | |
10672 | /// Evaluate an expression of record type as a temporary. |
10673 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
10674 | assert(!E->isValueDependent()); |
10675 | assert(E->isPRValue() && E->getType()->isRecordType()); |
10676 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
10677 | } |
10678 | |
10679 | //===----------------------------------------------------------------------===// |
10680 | // Vector Evaluation |
10681 | //===----------------------------------------------------------------------===// |
10682 | |
10683 | namespace { |
10684 | class VectorExprEvaluator |
10685 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
10686 | APValue &Result; |
10687 | public: |
10688 | |
10689 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
10690 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
10691 | |
10692 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
10693 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
10694 | // FIXME: remove this APValue copy. |
10695 | Result = APValue(V.data(), V.size()); |
10696 | return true; |
10697 | } |
10698 | bool Success(const APValue &V, const Expr *E) { |
10699 | assert(V.isVector()); |
10700 | Result = V; |
10701 | return true; |
10702 | } |
10703 | bool ZeroInitialization(const Expr *E); |
10704 | |
10705 | bool VisitUnaryReal(const UnaryOperator *E) |
10706 | { return Visit(E->getSubExpr()); } |
10707 | bool VisitCastExpr(const CastExpr* E); |
10708 | bool VisitInitListExpr(const InitListExpr *E); |
10709 | bool VisitUnaryImag(const UnaryOperator *E); |
10710 | bool VisitBinaryOperator(const BinaryOperator *E); |
10711 | bool VisitUnaryOperator(const UnaryOperator *E); |
10712 | // FIXME: Missing: conditional operator (for GNU |
10713 | // conditional select), shufflevector, ExtVectorElementExpr |
10714 | }; |
10715 | } // end anonymous namespace |
10716 | |
10717 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
10718 | assert(E->isPRValue() && E->getType()->isVectorType() && |
10719 | "not a vector prvalue" ); |
10720 | return VectorExprEvaluator(Info, Result).Visit(E); |
10721 | } |
10722 | |
10723 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10724 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
10725 | unsigned NElts = VTy->getNumElements(); |
10726 | |
10727 | const Expr *SE = E->getSubExpr(); |
10728 | QualType SETy = SE->getType(); |
10729 | |
10730 | switch (E->getCastKind()) { |
10731 | case CK_VectorSplat: { |
10732 | APValue Val = APValue(); |
10733 | if (SETy->isIntegerType()) { |
10734 | APSInt IntResult; |
10735 | if (!EvaluateInteger(E: SE, Result&: IntResult, Info)) |
10736 | return false; |
10737 | Val = APValue(std::move(IntResult)); |
10738 | } else if (SETy->isRealFloatingType()) { |
10739 | APFloat FloatResult(0.0); |
10740 | if (!EvaluateFloat(E: SE, Result&: FloatResult, Info)) |
10741 | return false; |
10742 | Val = APValue(std::move(FloatResult)); |
10743 | } else { |
10744 | return Error(E); |
10745 | } |
10746 | |
10747 | // Splat and create vector APValue. |
10748 | SmallVector<APValue, 4> Elts(NElts, Val); |
10749 | return Success(Elts, E); |
10750 | } |
10751 | case CK_BitCast: { |
10752 | APValue SVal; |
10753 | if (!Evaluate(Result&: SVal, Info, E: SE)) |
10754 | return false; |
10755 | |
10756 | if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) { |
10757 | // Give up if the input isn't an int, float, or vector. For example, we |
10758 | // reject "(v4i16)(intptr_t)&a". |
10759 | Info.FFDiag(E, diag::note_constexpr_invalid_cast) |
10760 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
10761 | return false; |
10762 | } |
10763 | |
10764 | if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E)) |
10765 | return false; |
10766 | |
10767 | return true; |
10768 | } |
10769 | default: |
10770 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10771 | } |
10772 | } |
10773 | |
10774 | bool |
10775 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10776 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
10777 | unsigned NumInits = E->getNumInits(); |
10778 | unsigned NumElements = VT->getNumElements(); |
10779 | |
10780 | QualType EltTy = VT->getElementType(); |
10781 | SmallVector<APValue, 4> Elements; |
10782 | |
10783 | // The number of initializers can be less than the number of |
10784 | // vector elements. For OpenCL, this can be due to nested vector |
10785 | // initialization. For GCC compatibility, missing trailing elements |
10786 | // should be initialized with zeroes. |
10787 | unsigned CountInits = 0, CountElts = 0; |
10788 | while (CountElts < NumElements) { |
10789 | // Handle nested vector initialization. |
10790 | if (CountInits < NumInits |
10791 | && E->getInit(Init: CountInits)->getType()->isVectorType()) { |
10792 | APValue v; |
10793 | if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info)) |
10794 | return Error(E); |
10795 | unsigned vlen = v.getVectorLength(); |
10796 | for (unsigned j = 0; j < vlen; j++) |
10797 | Elements.push_back(Elt: v.getVectorElt(I: j)); |
10798 | CountElts += vlen; |
10799 | } else if (EltTy->isIntegerType()) { |
10800 | llvm::APSInt sInt(32); |
10801 | if (CountInits < NumInits) { |
10802 | if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info)) |
10803 | return false; |
10804 | } else // trailing integer zero. |
10805 | sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy); |
10806 | Elements.push_back(Elt: APValue(sInt)); |
10807 | CountElts++; |
10808 | } else { |
10809 | llvm::APFloat f(0.0); |
10810 | if (CountInits < NumInits) { |
10811 | if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info)) |
10812 | return false; |
10813 | } else // trailing float zero. |
10814 | f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)); |
10815 | Elements.push_back(Elt: APValue(f)); |
10816 | CountElts++; |
10817 | } |
10818 | CountInits++; |
10819 | } |
10820 | return Success(Elements, E); |
10821 | } |
10822 | |
10823 | bool |
10824 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
10825 | const auto *VT = E->getType()->castAs<VectorType>(); |
10826 | QualType EltTy = VT->getElementType(); |
10827 | APValue ZeroElement; |
10828 | if (EltTy->isIntegerType()) |
10829 | ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy)); |
10830 | else |
10831 | ZeroElement = |
10832 | APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy))); |
10833 | |
10834 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
10835 | return Success(V: Elements, E); |
10836 | } |
10837 | |
10838 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
10839 | VisitIgnoredValue(E: E->getSubExpr()); |
10840 | return ZeroInitialization(E); |
10841 | } |
10842 | |
10843 | bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10844 | BinaryOperatorKind Op = E->getOpcode(); |
10845 | assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp && |
10846 | "Operation not supported on vector types" ); |
10847 | |
10848 | if (Op == BO_Comma) |
10849 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10850 | |
10851 | Expr *LHS = E->getLHS(); |
10852 | Expr *RHS = E->getRHS(); |
10853 | |
10854 | assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() && |
10855 | "Must both be vector types" ); |
10856 | // Checking JUST the types are the same would be fine, except shifts don't |
10857 | // need to have their types be the same (since you always shift by an int). |
10858 | assert(LHS->getType()->castAs<VectorType>()->getNumElements() == |
10859 | E->getType()->castAs<VectorType>()->getNumElements() && |
10860 | RHS->getType()->castAs<VectorType>()->getNumElements() == |
10861 | E->getType()->castAs<VectorType>()->getNumElements() && |
10862 | "All operands must be the same size." ); |
10863 | |
10864 | APValue LHSValue; |
10865 | APValue RHSValue; |
10866 | bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS); |
10867 | if (!LHSOK && !Info.noteFailure()) |
10868 | return false; |
10869 | if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK) |
10870 | return false; |
10871 | |
10872 | if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue)) |
10873 | return false; |
10874 | |
10875 | return Success(LHSValue, E); |
10876 | } |
10877 | |
10878 | static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx, |
10879 | QualType ResultTy, |
10880 | UnaryOperatorKind Op, |
10881 | APValue Elt) { |
10882 | switch (Op) { |
10883 | case UO_Plus: |
10884 | // Nothing to do here. |
10885 | return Elt; |
10886 | case UO_Minus: |
10887 | if (Elt.getKind() == APValue::Int) { |
10888 | Elt.getInt().negate(); |
10889 | } else { |
10890 | assert(Elt.getKind() == APValue::Float && |
10891 | "Vector can only be int or float type" ); |
10892 | Elt.getFloat().changeSign(); |
10893 | } |
10894 | return Elt; |
10895 | case UO_Not: |
10896 | // This is only valid for integral types anyway, so we don't have to handle |
10897 | // float here. |
10898 | assert(Elt.getKind() == APValue::Int && |
10899 | "Vector operator ~ can only be int" ); |
10900 | Elt.getInt().flipAllBits(); |
10901 | return Elt; |
10902 | case UO_LNot: { |
10903 | if (Elt.getKind() == APValue::Int) { |
10904 | Elt.getInt() = !Elt.getInt(); |
10905 | // operator ! on vectors returns -1 for 'truth', so negate it. |
10906 | Elt.getInt().negate(); |
10907 | return Elt; |
10908 | } |
10909 | assert(Elt.getKind() == APValue::Float && |
10910 | "Vector can only be int or float type" ); |
10911 | // Float types result in an int of the same size, but -1 for true, or 0 for |
10912 | // false. |
10913 | APSInt EltResult{Ctx.getIntWidth(T: ResultTy), |
10914 | ResultTy->isUnsignedIntegerType()}; |
10915 | if (Elt.getFloat().isZero()) |
10916 | EltResult.setAllBits(); |
10917 | else |
10918 | EltResult.clearAllBits(); |
10919 | |
10920 | return APValue{EltResult}; |
10921 | } |
10922 | default: |
10923 | // FIXME: Implement the rest of the unary operators. |
10924 | return std::nullopt; |
10925 | } |
10926 | } |
10927 | |
10928 | bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
10929 | Expr *SubExpr = E->getSubExpr(); |
10930 | const auto *VD = SubExpr->getType()->castAs<VectorType>(); |
10931 | // This result element type differs in the case of negating a floating point |
10932 | // vector, since the result type is the a vector of the equivilant sized |
10933 | // integer. |
10934 | const QualType ResultEltTy = VD->getElementType(); |
10935 | UnaryOperatorKind Op = E->getOpcode(); |
10936 | |
10937 | APValue SubExprValue; |
10938 | if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr)) |
10939 | return false; |
10940 | |
10941 | // FIXME: This vector evaluator someday needs to be changed to be LValue |
10942 | // aware/keep LValue information around, rather than dealing with just vector |
10943 | // types directly. Until then, we cannot handle cases where the operand to |
10944 | // these unary operators is an LValue. The only case I've been able to see |
10945 | // cause this is operator++ assigning to a member expression (only valid in |
10946 | // altivec compilations) in C mode, so this shouldn't limit us too much. |
10947 | if (SubExprValue.isLValue()) |
10948 | return false; |
10949 | |
10950 | assert(SubExprValue.getVectorLength() == VD->getNumElements() && |
10951 | "Vector length doesn't match type?" ); |
10952 | |
10953 | SmallVector<APValue, 4> ResultElements; |
10954 | for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) { |
10955 | std::optional<APValue> Elt = handleVectorUnaryOperator( |
10956 | Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum)); |
10957 | if (!Elt) |
10958 | return false; |
10959 | ResultElements.push_back(Elt: *Elt); |
10960 | } |
10961 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
10962 | } |
10963 | |
10964 | //===----------------------------------------------------------------------===// |
10965 | // Array Evaluation |
10966 | //===----------------------------------------------------------------------===// |
10967 | |
10968 | namespace { |
10969 | class ArrayExprEvaluator |
10970 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
10971 | const LValue &This; |
10972 | APValue &Result; |
10973 | public: |
10974 | |
10975 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
10976 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
10977 | |
10978 | bool Success(const APValue &V, const Expr *E) { |
10979 | assert(V.isArray() && "expected array" ); |
10980 | Result = V; |
10981 | return true; |
10982 | } |
10983 | |
10984 | bool ZeroInitialization(const Expr *E) { |
10985 | const ConstantArrayType *CAT = |
10986 | Info.Ctx.getAsConstantArrayType(T: E->getType()); |
10987 | if (!CAT) { |
10988 | if (E->getType()->isIncompleteArrayType()) { |
10989 | // We can be asked to zero-initialize a flexible array member; this |
10990 | // is represented as an ImplicitValueInitExpr of incomplete array |
10991 | // type. In this case, the array has zero elements. |
10992 | Result = APValue(APValue::UninitArray(), 0, 0); |
10993 | return true; |
10994 | } |
10995 | // FIXME: We could handle VLAs here. |
10996 | return Error(E); |
10997 | } |
10998 | |
10999 | Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize()); |
11000 | if (!Result.hasArrayFiller()) |
11001 | return true; |
11002 | |
11003 | // Zero-initialize all elements. |
11004 | LValue Subobject = This; |
11005 | Subobject.addArray(Info, E, CAT); |
11006 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
11007 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
11008 | } |
11009 | |
11010 | bool VisitCallExpr(const CallExpr *E) { |
11011 | return handleCallExpr(E, Result, ResultSlot: &This); |
11012 | } |
11013 | bool VisitInitListExpr(const InitListExpr *E, |
11014 | QualType AllocType = QualType()); |
11015 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
11016 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
11017 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
11018 | const LValue &Subobject, |
11019 | APValue *Value, QualType Type); |
11020 | bool VisitStringLiteral(const StringLiteral *E, |
11021 | QualType AllocType = QualType()) { |
11022 | expandStringLiteral(Info, S: E, Result, AllocType); |
11023 | return true; |
11024 | } |
11025 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
11026 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
11027 | ArrayRef<Expr *> Args, |
11028 | const Expr *ArrayFiller, |
11029 | QualType AllocType = QualType()); |
11030 | }; |
11031 | } // end anonymous namespace |
11032 | |
11033 | static bool EvaluateArray(const Expr *E, const LValue &This, |
11034 | APValue &Result, EvalInfo &Info) { |
11035 | assert(!E->isValueDependent()); |
11036 | assert(E->isPRValue() && E->getType()->isArrayType() && |
11037 | "not an array prvalue" ); |
11038 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
11039 | } |
11040 | |
11041 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
11042 | APValue &Result, const InitListExpr *ILE, |
11043 | QualType AllocType) { |
11044 | assert(!ILE->isValueDependent()); |
11045 | assert(ILE->isPRValue() && ILE->getType()->isArrayType() && |
11046 | "not an array prvalue" ); |
11047 | return ArrayExprEvaluator(Info, This, Result) |
11048 | .VisitInitListExpr(E: ILE, AllocType); |
11049 | } |
11050 | |
11051 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
11052 | APValue &Result, |
11053 | const CXXConstructExpr *CCE, |
11054 | QualType AllocType) { |
11055 | assert(!CCE->isValueDependent()); |
11056 | assert(CCE->isPRValue() && CCE->getType()->isArrayType() && |
11057 | "not an array prvalue" ); |
11058 | return ArrayExprEvaluator(Info, This, Result) |
11059 | .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType); |
11060 | } |
11061 | |
11062 | // Return true iff the given array filler may depend on the element index. |
11063 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
11064 | // For now, just allow non-class value-initialization and initialization |
11065 | // lists comprised of them. |
11066 | if (isa<ImplicitValueInitExpr>(Val: FillerExpr)) |
11067 | return false; |
11068 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) { |
11069 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
11070 | if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I))) |
11071 | return true; |
11072 | } |
11073 | |
11074 | if (ILE->hasArrayFiller() && |
11075 | MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller())) |
11076 | return true; |
11077 | |
11078 | return false; |
11079 | } |
11080 | return true; |
11081 | } |
11082 | |
11083 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
11084 | QualType AllocType) { |
11085 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11086 | T: AllocType.isNull() ? E->getType() : AllocType); |
11087 | if (!CAT) |
11088 | return Error(E); |
11089 | |
11090 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
11091 | // an appropriately-typed string literal enclosed in braces. |
11092 | if (E->isStringLiteralInit()) { |
11093 | auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts()); |
11094 | // FIXME: Support ObjCEncodeExpr here once we support it in |
11095 | // ArrayExprEvaluator generally. |
11096 | if (!SL) |
11097 | return Error(E); |
11098 | return VisitStringLiteral(E: SL, AllocType); |
11099 | } |
11100 | // Any other transparent list init will need proper handling of the |
11101 | // AllocType; we can't just recurse to the inner initializer. |
11102 | assert(!E->isTransparent() && |
11103 | "transparent array list initialization is not string literal init?" ); |
11104 | |
11105 | return VisitCXXParenListOrInitListExpr(E, E->inits(), E->getArrayFiller(), |
11106 | AllocType); |
11107 | } |
11108 | |
11109 | bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr( |
11110 | const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller, |
11111 | QualType AllocType) { |
11112 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11113 | T: AllocType.isNull() ? ExprToVisit->getType() : AllocType); |
11114 | |
11115 | bool Success = true; |
11116 | |
11117 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
11118 | "zero-initialized array shouldn't have any initialized elts" ); |
11119 | APValue Filler; |
11120 | if (Result.isArray() && Result.hasArrayFiller()) |
11121 | Filler = Result.getArrayFiller(); |
11122 | |
11123 | unsigned NumEltsToInit = Args.size(); |
11124 | unsigned NumElts = CAT->getZExtSize(); |
11125 | |
11126 | // If the initializer might depend on the array index, run it for each |
11127 | // array element. |
11128 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) |
11129 | NumEltsToInit = NumElts; |
11130 | |
11131 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
11132 | << NumEltsToInit << ".\n" ); |
11133 | |
11134 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
11135 | |
11136 | // If the array was previously zero-initialized, preserve the |
11137 | // zero-initialized values. |
11138 | if (Filler.hasValue()) { |
11139 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
11140 | Result.getArrayInitializedElt(I) = Filler; |
11141 | if (Result.hasArrayFiller()) |
11142 | Result.getArrayFiller() = Filler; |
11143 | } |
11144 | |
11145 | LValue Subobject = This; |
11146 | Subobject.addArray(Info, E: ExprToVisit, CAT); |
11147 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
11148 | const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller; |
11149 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index), |
11150 | Info, This: Subobject, E: Init) || |
11151 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
11152 | CAT->getElementType(), 1)) { |
11153 | if (!Info.noteFailure()) |
11154 | return false; |
11155 | Success = false; |
11156 | } |
11157 | } |
11158 | |
11159 | if (!Result.hasArrayFiller()) |
11160 | return Success; |
11161 | |
11162 | // If we get here, we have a trivial filler, which we can just evaluate |
11163 | // once and splat over the rest of the array elements. |
11164 | assert(ArrayFiller && "no array filler for incomplete init list" ); |
11165 | return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, |
11166 | E: ArrayFiller) && |
11167 | Success; |
11168 | } |
11169 | |
11170 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
11171 | LValue CommonLV; |
11172 | if (E->getCommonExpr() && |
11173 | !Evaluate(Result&: Info.CurrentCall->createTemporary( |
11174 | Key: E->getCommonExpr(), |
11175 | T: getStorageType(Info.Ctx, E->getCommonExpr()), |
11176 | Scope: ScopeKind::FullExpression, LV&: CommonLV), |
11177 | Info, E: E->getCommonExpr()->getSourceExpr())) |
11178 | return false; |
11179 | |
11180 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
11181 | |
11182 | uint64_t Elements = CAT->getZExtSize(); |
11183 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
11184 | |
11185 | LValue Subobject = This; |
11186 | Subobject.addArray(Info, E, CAT: CAT); |
11187 | |
11188 | bool Success = true; |
11189 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
11190 | // C++ [class.temporary]/5 |
11191 | // There are four contexts in which temporaries are destroyed at a different |
11192 | // point than the end of the full-expression. [...] The second context is |
11193 | // when a copy constructor is called to copy an element of an array while |
11194 | // the entire array is copied [...]. In either case, if the constructor has |
11195 | // one or more default arguments, the destruction of every temporary created |
11196 | // in a default argument is sequenced before the construction of the next |
11197 | // array element, if any. |
11198 | FullExpressionRAII Scope(Info); |
11199 | |
11200 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index), |
11201 | Info, This: Subobject, E: E->getSubExpr()) || |
11202 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
11203 | CAT->getElementType(), 1)) { |
11204 | if (!Info.noteFailure()) |
11205 | return false; |
11206 | Success = false; |
11207 | } |
11208 | |
11209 | // Make sure we run the destructors too. |
11210 | Scope.destroy(); |
11211 | } |
11212 | |
11213 | return Success; |
11214 | } |
11215 | |
11216 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
11217 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
11218 | } |
11219 | |
11220 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
11221 | const LValue &Subobject, |
11222 | APValue *Value, |
11223 | QualType Type) { |
11224 | bool HadZeroInit = Value->hasValue(); |
11225 | |
11226 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) { |
11227 | unsigned FinalSize = CAT->getZExtSize(); |
11228 | |
11229 | // Preserve the array filler if we had prior zero-initialization. |
11230 | APValue Filler = |
11231 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
11232 | : APValue(); |
11233 | |
11234 | *Value = APValue(APValue::UninitArray(), 0, FinalSize); |
11235 | if (FinalSize == 0) |
11236 | return true; |
11237 | |
11238 | bool HasTrivialConstructor = CheckTrivialDefaultConstructor( |
11239 | Info, E->getExprLoc(), E->getConstructor(), |
11240 | E->requiresZeroInitialization()); |
11241 | LValue ArrayElt = Subobject; |
11242 | ArrayElt.addArray(Info, E, CAT); |
11243 | // We do the whole initialization in two passes, first for just one element, |
11244 | // then for the whole array. It's possible we may find out we can't do const |
11245 | // init in the first pass, in which case we avoid allocating a potentially |
11246 | // large array. We don't do more passes because expanding array requires |
11247 | // copying the data, which is wasteful. |
11248 | for (const unsigned N : {1u, FinalSize}) { |
11249 | unsigned OldElts = Value->getArrayInitializedElts(); |
11250 | if (OldElts == N) |
11251 | break; |
11252 | |
11253 | // Expand the array to appropriate size. |
11254 | APValue NewValue(APValue::UninitArray(), N, FinalSize); |
11255 | for (unsigned I = 0; I < OldElts; ++I) |
11256 | NewValue.getArrayInitializedElt(I).swap( |
11257 | RHS&: Value->getArrayInitializedElt(I)); |
11258 | Value->swap(RHS&: NewValue); |
11259 | |
11260 | if (HadZeroInit) |
11261 | for (unsigned I = OldElts; I < N; ++I) |
11262 | Value->getArrayInitializedElt(I) = Filler; |
11263 | |
11264 | if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) { |
11265 | // If we have a trivial constructor, only evaluate it once and copy |
11266 | // the result into all the array elements. |
11267 | APValue &FirstResult = Value->getArrayInitializedElt(I: 0); |
11268 | for (unsigned I = OldElts; I < FinalSize; ++I) |
11269 | Value->getArrayInitializedElt(I) = FirstResult; |
11270 | } else { |
11271 | for (unsigned I = OldElts; I < N; ++I) { |
11272 | if (!VisitCXXConstructExpr(E, ArrayElt, |
11273 | &Value->getArrayInitializedElt(I), |
11274 | CAT->getElementType()) || |
11275 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
11276 | CAT->getElementType(), 1)) |
11277 | return false; |
11278 | // When checking for const initilization any diagnostic is considered |
11279 | // an error. |
11280 | if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() && |
11281 | !Info.keepEvaluatingAfterFailure()) |
11282 | return false; |
11283 | } |
11284 | } |
11285 | } |
11286 | |
11287 | return true; |
11288 | } |
11289 | |
11290 | if (!Type->isRecordType()) |
11291 | return Error(E); |
11292 | |
11293 | return RecordExprEvaluator(Info, Subobject, *Value) |
11294 | .VisitCXXConstructExpr(E, T: Type); |
11295 | } |
11296 | |
11297 | bool ArrayExprEvaluator::VisitCXXParenListInitExpr( |
11298 | const CXXParenListInitExpr *E) { |
11299 | assert(dyn_cast<ConstantArrayType>(E->getType()) && |
11300 | "Expression result is not a constant array type" ); |
11301 | |
11302 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs(), |
11303 | E->getArrayFiller()); |
11304 | } |
11305 | |
11306 | //===----------------------------------------------------------------------===// |
11307 | // Integer Evaluation |
11308 | // |
11309 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
11310 | // types and back in constant folding. Integer values are thus represented |
11311 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
11312 | //===----------------------------------------------------------------------===// |
11313 | |
11314 | namespace { |
11315 | class IntExprEvaluator |
11316 | : public ExprEvaluatorBase<IntExprEvaluator> { |
11317 | APValue &Result; |
11318 | public: |
11319 | IntExprEvaluator(EvalInfo &info, APValue &result) |
11320 | : ExprEvaluatorBaseTy(info), Result(result) {} |
11321 | |
11322 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
11323 | assert(E->getType()->isIntegralOrEnumerationType() && |
11324 | "Invalid evaluation result." ); |
11325 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
11326 | "Invalid evaluation result." ); |
11327 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11328 | "Invalid evaluation result." ); |
11329 | Result = APValue(SI); |
11330 | return true; |
11331 | } |
11332 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
11333 | return Success(SI, E, Result); |
11334 | } |
11335 | |
11336 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
11337 | assert(E->getType()->isIntegralOrEnumerationType() && |
11338 | "Invalid evaluation result." ); |
11339 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11340 | "Invalid evaluation result." ); |
11341 | Result = APValue(APSInt(I)); |
11342 | Result.getInt().setIsUnsigned( |
11343 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
11344 | return true; |
11345 | } |
11346 | bool Success(const llvm::APInt &I, const Expr *E) { |
11347 | return Success(I, E, Result); |
11348 | } |
11349 | |
11350 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
11351 | assert(E->getType()->isIntegralOrEnumerationType() && |
11352 | "Invalid evaluation result." ); |
11353 | Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType())); |
11354 | return true; |
11355 | } |
11356 | bool Success(uint64_t Value, const Expr *E) { |
11357 | return Success(Value, E, Result); |
11358 | } |
11359 | |
11360 | bool Success(CharUnits Size, const Expr *E) { |
11361 | return Success(Value: Size.getQuantity(), E); |
11362 | } |
11363 | |
11364 | bool Success(const APValue &V, const Expr *E) { |
11365 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate()) { |
11366 | Result = V; |
11367 | return true; |
11368 | } |
11369 | return Success(SI: V.getInt(), E); |
11370 | } |
11371 | |
11372 | bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); } |
11373 | |
11374 | //===--------------------------------------------------------------------===// |
11375 | // Visitor Methods |
11376 | //===--------------------------------------------------------------------===// |
11377 | |
11378 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
11379 | return Success(E->getValue(), E); |
11380 | } |
11381 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
11382 | return Success(E->getValue(), E); |
11383 | } |
11384 | |
11385 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
11386 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
11387 | if (CheckReferencedDecl(E, E->getDecl())) |
11388 | return true; |
11389 | |
11390 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
11391 | } |
11392 | bool VisitMemberExpr(const MemberExpr *E) { |
11393 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
11394 | VisitIgnoredBaseExpression(E: E->getBase()); |
11395 | return true; |
11396 | } |
11397 | |
11398 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
11399 | } |
11400 | |
11401 | bool VisitCallExpr(const CallExpr *E); |
11402 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
11403 | bool VisitBinaryOperator(const BinaryOperator *E); |
11404 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
11405 | bool VisitUnaryOperator(const UnaryOperator *E); |
11406 | |
11407 | bool VisitCastExpr(const CastExpr* E); |
11408 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
11409 | |
11410 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
11411 | return Success(E->getValue(), E); |
11412 | } |
11413 | |
11414 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
11415 | return Success(E->getValue(), E); |
11416 | } |
11417 | |
11418 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
11419 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
11420 | // We were asked to evaluate this subexpression independent of the |
11421 | // enclosing ArrayInitLoopExpr. We can't do that. |
11422 | Info.FFDiag(E); |
11423 | return false; |
11424 | } |
11425 | return Success(Info.ArrayInitIndex, E); |
11426 | } |
11427 | |
11428 | // Note, GNU defines __null as an integer, not a pointer. |
11429 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
11430 | return ZeroInitialization(E); |
11431 | } |
11432 | |
11433 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
11434 | return Success(E->getValue(), E); |
11435 | } |
11436 | |
11437 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
11438 | return Success(E->getValue(), E); |
11439 | } |
11440 | |
11441 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
11442 | return Success(E->getValue(), E); |
11443 | } |
11444 | |
11445 | bool VisitUnaryReal(const UnaryOperator *E); |
11446 | bool VisitUnaryImag(const UnaryOperator *E); |
11447 | |
11448 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
11449 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
11450 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
11451 | bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E); |
11452 | bool VisitRequiresExpr(const RequiresExpr *E); |
11453 | // FIXME: Missing: array subscript of vector, member of vector |
11454 | }; |
11455 | |
11456 | class FixedPointExprEvaluator |
11457 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
11458 | APValue &Result; |
11459 | |
11460 | public: |
11461 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
11462 | : ExprEvaluatorBaseTy(info), Result(result) {} |
11463 | |
11464 | bool Success(const llvm::APInt &I, const Expr *E) { |
11465 | return Success( |
11466 | V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
11467 | } |
11468 | |
11469 | bool Success(uint64_t Value, const Expr *E) { |
11470 | return Success( |
11471 | V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
11472 | } |
11473 | |
11474 | bool Success(const APValue &V, const Expr *E) { |
11475 | return Success(V: V.getFixedPoint(), E); |
11476 | } |
11477 | |
11478 | bool Success(const APFixedPoint &V, const Expr *E) { |
11479 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result." ); |
11480 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11481 | "Invalid evaluation result." ); |
11482 | Result = APValue(V); |
11483 | return true; |
11484 | } |
11485 | |
11486 | bool ZeroInitialization(const Expr *E) { |
11487 | return Success(Value: 0, E); |
11488 | } |
11489 | |
11490 | //===--------------------------------------------------------------------===// |
11491 | // Visitor Methods |
11492 | //===--------------------------------------------------------------------===// |
11493 | |
11494 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
11495 | return Success(E->getValue(), E); |
11496 | } |
11497 | |
11498 | bool VisitCastExpr(const CastExpr *E); |
11499 | bool VisitUnaryOperator(const UnaryOperator *E); |
11500 | bool VisitBinaryOperator(const BinaryOperator *E); |
11501 | }; |
11502 | } // end anonymous namespace |
11503 | |
11504 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
11505 | /// produce either the integer value or a pointer. |
11506 | /// |
11507 | /// GCC has a heinous extension which folds casts between pointer types and |
11508 | /// pointer-sized integral types. We support this by allowing the evaluation of |
11509 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
11510 | /// Some simple arithmetic on such values is supported (they are treated much |
11511 | /// like char*). |
11512 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
11513 | EvalInfo &Info) { |
11514 | assert(!E->isValueDependent()); |
11515 | assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType()); |
11516 | return IntExprEvaluator(Info, Result).Visit(E); |
11517 | } |
11518 | |
11519 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
11520 | assert(!E->isValueDependent()); |
11521 | APValue Val; |
11522 | if (!EvaluateIntegerOrLValue(E, Result&: Val, Info)) |
11523 | return false; |
11524 | if (!Val.isInt()) { |
11525 | // FIXME: It would be better to produce the diagnostic for casting |
11526 | // a pointer to an integer. |
11527 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11528 | return false; |
11529 | } |
11530 | Result = Val.getInt(); |
11531 | return true; |
11532 | } |
11533 | |
11534 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
11535 | APValue Evaluated = E->EvaluateInContext( |
11536 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
11537 | return Success(Evaluated, E); |
11538 | } |
11539 | |
11540 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
11541 | EvalInfo &Info) { |
11542 | assert(!E->isValueDependent()); |
11543 | if (E->getType()->isFixedPointType()) { |
11544 | APValue Val; |
11545 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
11546 | return false; |
11547 | if (!Val.isFixedPoint()) |
11548 | return false; |
11549 | |
11550 | Result = Val.getFixedPoint(); |
11551 | return true; |
11552 | } |
11553 | return false; |
11554 | } |
11555 | |
11556 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
11557 | EvalInfo &Info) { |
11558 | assert(!E->isValueDependent()); |
11559 | if (E->getType()->isIntegerType()) { |
11560 | auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
11561 | APSInt Val; |
11562 | if (!EvaluateInteger(E, Result&: Val, Info)) |
11563 | return false; |
11564 | Result = APFixedPoint(Val, FXSema); |
11565 | return true; |
11566 | } else if (E->getType()->isFixedPointType()) { |
11567 | return EvaluateFixedPoint(E, Result, Info); |
11568 | } |
11569 | return false; |
11570 | } |
11571 | |
11572 | /// Check whether the given declaration can be directly converted to an integral |
11573 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
11574 | /// try. |
11575 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
11576 | // Enums are integer constant exprs. |
11577 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) { |
11578 | // Check for signedness/width mismatches between E type and ECD value. |
11579 | bool SameSign = (ECD->getInitVal().isSigned() |
11580 | == E->getType()->isSignedIntegerOrEnumerationType()); |
11581 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
11582 | == Info.Ctx.getIntWidth(T: E->getType())); |
11583 | if (SameSign && SameWidth) |
11584 | return Success(SI: ECD->getInitVal(), E); |
11585 | else { |
11586 | // Get rid of mismatch (otherwise Success assertions will fail) |
11587 | // by computing a new value matching the type of E. |
11588 | llvm::APSInt Val = ECD->getInitVal(); |
11589 | if (!SameSign) |
11590 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
11591 | if (!SameWidth) |
11592 | Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
11593 | return Success(SI: Val, E); |
11594 | } |
11595 | } |
11596 | return false; |
11597 | } |
11598 | |
11599 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
11600 | /// as GCC. |
11601 | GCCTypeClass EvaluateBuiltinClassifyType(QualType T, |
11602 | const LangOptions &LangOpts) { |
11603 | assert(!T->isDependentType() && "unexpected dependent type" ); |
11604 | |
11605 | QualType CanTy = T.getCanonicalType(); |
11606 | |
11607 | switch (CanTy->getTypeClass()) { |
11608 | #define TYPE(ID, BASE) |
11609 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
11610 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
11611 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
11612 | #include "clang/AST/TypeNodes.inc" |
11613 | case Type::Auto: |
11614 | case Type::DeducedTemplateSpecialization: |
11615 | llvm_unreachable("unexpected non-canonical or dependent type" ); |
11616 | |
11617 | case Type::Builtin: |
11618 | switch (cast<BuiltinType>(CanTy)->getKind()) { |
11619 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
11620 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
11621 | case BuiltinType::ID: return GCCTypeClass::Integer; |
11622 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
11623 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
11624 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
11625 | case BuiltinType::ID: break; |
11626 | #include "clang/AST/BuiltinTypes.def" |
11627 | case BuiltinType::Void: |
11628 | return GCCTypeClass::Void; |
11629 | |
11630 | case BuiltinType::Bool: |
11631 | return GCCTypeClass::Bool; |
11632 | |
11633 | case BuiltinType::Char_U: |
11634 | case BuiltinType::UChar: |
11635 | case BuiltinType::WChar_U: |
11636 | case BuiltinType::Char8: |
11637 | case BuiltinType::Char16: |
11638 | case BuiltinType::Char32: |
11639 | case BuiltinType::UShort: |
11640 | case BuiltinType::UInt: |
11641 | case BuiltinType::ULong: |
11642 | case BuiltinType::ULongLong: |
11643 | case BuiltinType::UInt128: |
11644 | return GCCTypeClass::Integer; |
11645 | |
11646 | case BuiltinType::UShortAccum: |
11647 | case BuiltinType::UAccum: |
11648 | case BuiltinType::ULongAccum: |
11649 | case BuiltinType::UShortFract: |
11650 | case BuiltinType::UFract: |
11651 | case BuiltinType::ULongFract: |
11652 | case BuiltinType::SatUShortAccum: |
11653 | case BuiltinType::SatUAccum: |
11654 | case BuiltinType::SatULongAccum: |
11655 | case BuiltinType::SatUShortFract: |
11656 | case BuiltinType::SatUFract: |
11657 | case BuiltinType::SatULongFract: |
11658 | return GCCTypeClass::None; |
11659 | |
11660 | case BuiltinType::NullPtr: |
11661 | |
11662 | case BuiltinType::ObjCId: |
11663 | case BuiltinType::ObjCClass: |
11664 | case BuiltinType::ObjCSel: |
11665 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
11666 | case BuiltinType::Id: |
11667 | #include "clang/Basic/OpenCLImageTypes.def" |
11668 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
11669 | case BuiltinType::Id: |
11670 | #include "clang/Basic/OpenCLExtensionTypes.def" |
11671 | case BuiltinType::OCLSampler: |
11672 | case BuiltinType::OCLEvent: |
11673 | case BuiltinType::OCLClkEvent: |
11674 | case BuiltinType::OCLQueue: |
11675 | case BuiltinType::OCLReserveID: |
11676 | #define SVE_TYPE(Name, Id, SingletonId) \ |
11677 | case BuiltinType::Id: |
11678 | #include "clang/Basic/AArch64SVEACLETypes.def" |
11679 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
11680 | case BuiltinType::Id: |
11681 | #include "clang/Basic/PPCTypes.def" |
11682 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11683 | #include "clang/Basic/RISCVVTypes.def" |
11684 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11685 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
11686 | return GCCTypeClass::None; |
11687 | |
11688 | case BuiltinType::Dependent: |
11689 | llvm_unreachable("unexpected dependent type" ); |
11690 | }; |
11691 | llvm_unreachable("unexpected placeholder type" ); |
11692 | |
11693 | case Type::Enum: |
11694 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
11695 | |
11696 | case Type::Pointer: |
11697 | case Type::ConstantArray: |
11698 | case Type::VariableArray: |
11699 | case Type::IncompleteArray: |
11700 | case Type::FunctionNoProto: |
11701 | case Type::FunctionProto: |
11702 | case Type::ArrayParameter: |
11703 | return GCCTypeClass::Pointer; |
11704 | |
11705 | case Type::MemberPointer: |
11706 | return CanTy->isMemberDataPointerType() |
11707 | ? GCCTypeClass::PointerToDataMember |
11708 | : GCCTypeClass::PointerToMemberFunction; |
11709 | |
11710 | case Type::Complex: |
11711 | return GCCTypeClass::Complex; |
11712 | |
11713 | case Type::Record: |
11714 | return CanTy->isUnionType() ? GCCTypeClass::Union |
11715 | : GCCTypeClass::ClassOrStruct; |
11716 | |
11717 | case Type::Atomic: |
11718 | // GCC classifies _Atomic T the same as T. |
11719 | return EvaluateBuiltinClassifyType( |
11720 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
11721 | |
11722 | case Type::Vector: |
11723 | case Type::ExtVector: |
11724 | return GCCTypeClass::Vector; |
11725 | |
11726 | case Type::BlockPointer: |
11727 | case Type::ConstantMatrix: |
11728 | case Type::ObjCObject: |
11729 | case Type::ObjCInterface: |
11730 | case Type::ObjCObjectPointer: |
11731 | case Type::Pipe: |
11732 | // Classify all other types that don't fit into the regular |
11733 | // classification the same way. |
11734 | return GCCTypeClass::None; |
11735 | |
11736 | case Type::BitInt: |
11737 | return GCCTypeClass::BitInt; |
11738 | |
11739 | case Type::LValueReference: |
11740 | case Type::RValueReference: |
11741 | llvm_unreachable("invalid type for expression" ); |
11742 | } |
11743 | |
11744 | llvm_unreachable("unexpected type class" ); |
11745 | } |
11746 | |
11747 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
11748 | /// as GCC. |
11749 | static GCCTypeClass |
11750 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
11751 | // If no argument was supplied, default to None. This isn't |
11752 | // ideal, however it is what gcc does. |
11753 | if (E->getNumArgs() == 0) |
11754 | return GCCTypeClass::None; |
11755 | |
11756 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
11757 | // being an ICE, but still folds it to a constant using the type of the first |
11758 | // argument. |
11759 | return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts); |
11760 | } |
11761 | |
11762 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
11763 | /// __builtin_constant_p when applied to the given pointer. |
11764 | /// |
11765 | /// A pointer is only "constant" if it is null (or a pointer cast to integer) |
11766 | /// or it points to the first character of a string literal. |
11767 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
11768 | APValue::LValueBase Base = LV.getLValueBase(); |
11769 | if (Base.isNull()) { |
11770 | // A null base is acceptable. |
11771 | return true; |
11772 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
11773 | if (!isa<StringLiteral>(Val: E)) |
11774 | return false; |
11775 | return LV.getLValueOffset().isZero(); |
11776 | } else if (Base.is<TypeInfoLValue>()) { |
11777 | // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to |
11778 | // evaluate to true. |
11779 | return true; |
11780 | } else { |
11781 | // Any other base is not constant enough for GCC. |
11782 | return false; |
11783 | } |
11784 | } |
11785 | |
11786 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
11787 | /// GCC as we can manage. |
11788 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
11789 | // This evaluation is not permitted to have side-effects, so evaluate it in |
11790 | // a speculative evaluation context. |
11791 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11792 | |
11793 | // Constant-folding is always enabled for the operand of __builtin_constant_p |
11794 | // (even when the enclosing evaluation context otherwise requires a strict |
11795 | // language-specific constant expression). |
11796 | FoldConstant Fold(Info, true); |
11797 | |
11798 | QualType ArgType = Arg->getType(); |
11799 | |
11800 | // __builtin_constant_p always has one operand. The rules which gcc follows |
11801 | // are not precisely documented, but are as follows: |
11802 | // |
11803 | // - If the operand is of integral, floating, complex or enumeration type, |
11804 | // and can be folded to a known value of that type, it returns 1. |
11805 | // - If the operand can be folded to a pointer to the first character |
11806 | // of a string literal (or such a pointer cast to an integral type) |
11807 | // or to a null pointer or an integer cast to a pointer, it returns 1. |
11808 | // |
11809 | // Otherwise, it returns 0. |
11810 | // |
11811 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
11812 | // its support for this did not work prior to GCC 9 and is not yet well |
11813 | // understood. |
11814 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
11815 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
11816 | ArgType->isNullPtrType()) { |
11817 | APValue V; |
11818 | if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) { |
11819 | Fold.keepDiagnostics(); |
11820 | return false; |
11821 | } |
11822 | |
11823 | // For a pointer (possibly cast to integer), there are special rules. |
11824 | if (V.getKind() == APValue::LValue) |
11825 | return EvaluateBuiltinConstantPForLValue(LV: V); |
11826 | |
11827 | // Otherwise, any constant value is good enough. |
11828 | return V.hasValue(); |
11829 | } |
11830 | |
11831 | // Anything else isn't considered to be sufficiently constant. |
11832 | return false; |
11833 | } |
11834 | |
11835 | /// Retrieves the "underlying object type" of the given expression, |
11836 | /// as used by __builtin_object_size. |
11837 | static QualType getObjectType(APValue::LValueBase B) { |
11838 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
11839 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
11840 | return VD->getType(); |
11841 | } else if (const Expr *E = B.dyn_cast<const Expr*>()) { |
11842 | if (isa<CompoundLiteralExpr>(Val: E)) |
11843 | return E->getType(); |
11844 | } else if (B.is<TypeInfoLValue>()) { |
11845 | return B.getTypeInfoType(); |
11846 | } else if (B.is<DynamicAllocLValue>()) { |
11847 | return B.getDynamicAllocType(); |
11848 | } |
11849 | |
11850 | return QualType(); |
11851 | } |
11852 | |
11853 | /// A more selective version of E->IgnoreParenCasts for |
11854 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
11855 | /// to change the type of E. |
11856 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
11857 | /// |
11858 | /// Always returns an RValue with a pointer representation. |
11859 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
11860 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
11861 | |
11862 | const Expr *NoParens = E->IgnoreParens(); |
11863 | const auto *Cast = dyn_cast<CastExpr>(Val: NoParens); |
11864 | if (Cast == nullptr) |
11865 | return NoParens; |
11866 | |
11867 | // We only conservatively allow a few kinds of casts, because this code is |
11868 | // inherently a simple solution that seeks to support the common case. |
11869 | auto CastKind = Cast->getCastKind(); |
11870 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
11871 | CastKind != CK_AddressSpaceConversion) |
11872 | return NoParens; |
11873 | |
11874 | const auto *SubExpr = Cast->getSubExpr(); |
11875 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue()) |
11876 | return NoParens; |
11877 | return ignorePointerCastsAndParens(E: SubExpr); |
11878 | } |
11879 | |
11880 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
11881 | /// record layout. e.g. |
11882 | /// struct { struct { int a, b; } fst, snd; } obj; |
11883 | /// obj.fst // no |
11884 | /// obj.snd // yes |
11885 | /// obj.fst.a // no |
11886 | /// obj.fst.b // no |
11887 | /// obj.snd.a // no |
11888 | /// obj.snd.b // yes |
11889 | /// |
11890 | /// Please note: this function is specialized for how __builtin_object_size |
11891 | /// views "objects". |
11892 | /// |
11893 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
11894 | /// correct result, it will always return true. |
11895 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
11896 | assert(!LVal.Designator.Invalid); |
11897 | |
11898 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
11899 | const RecordDecl *Parent = FD->getParent(); |
11900 | Invalid = Parent->isInvalidDecl(); |
11901 | if (Invalid || Parent->isUnion()) |
11902 | return true; |
11903 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent); |
11904 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
11905 | }; |
11906 | |
11907 | auto &Base = LVal.getLValueBase(); |
11908 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) { |
11909 | if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) { |
11910 | bool Invalid; |
11911 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11912 | return Invalid; |
11913 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) { |
11914 | for (auto *FD : IFD->chain()) { |
11915 | bool Invalid; |
11916 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD), Invalid)) |
11917 | return Invalid; |
11918 | } |
11919 | } |
11920 | } |
11921 | |
11922 | unsigned I = 0; |
11923 | QualType BaseType = getType(B: Base); |
11924 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
11925 | // If we don't know the array bound, conservatively assume we're looking at |
11926 | // the final array element. |
11927 | ++I; |
11928 | if (BaseType->isIncompleteArrayType()) |
11929 | BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType(); |
11930 | else |
11931 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
11932 | } |
11933 | |
11934 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
11935 | const auto &Entry = LVal.Designator.Entries[I]; |
11936 | if (BaseType->isArrayType()) { |
11937 | // Because __builtin_object_size treats arrays as objects, we can ignore |
11938 | // the index iff this is the last array in the Designator. |
11939 | if (I + 1 == E) |
11940 | return true; |
11941 | const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType)); |
11942 | uint64_t Index = Entry.getAsArrayIndex(); |
11943 | if (Index + 1 != CAT->getZExtSize()) |
11944 | return false; |
11945 | BaseType = CAT->getElementType(); |
11946 | } else if (BaseType->isAnyComplexType()) { |
11947 | const auto *CT = BaseType->castAs<ComplexType>(); |
11948 | uint64_t Index = Entry.getAsArrayIndex(); |
11949 | if (Index != 1) |
11950 | return false; |
11951 | BaseType = CT->getElementType(); |
11952 | } else if (auto *FD = getAsField(Entry)) { |
11953 | bool Invalid; |
11954 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11955 | return Invalid; |
11956 | BaseType = FD->getType(); |
11957 | } else { |
11958 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class" ); |
11959 | return false; |
11960 | } |
11961 | } |
11962 | return true; |
11963 | } |
11964 | |
11965 | /// Tests to see if the LValue has a user-specified designator (that isn't |
11966 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
11967 | /// an unsized array as its first designator entry, because there's currently no |
11968 | /// way to tell if the user typed *foo or foo[0]. |
11969 | static bool refersToCompleteObject(const LValue &LVal) { |
11970 | if (LVal.Designator.Invalid) |
11971 | return false; |
11972 | |
11973 | if (!LVal.Designator.Entries.empty()) |
11974 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
11975 | |
11976 | if (!LVal.InvalidBase) |
11977 | return true; |
11978 | |
11979 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
11980 | // the LValueBase. |
11981 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
11982 | return !E || !isa<MemberExpr>(Val: E); |
11983 | } |
11984 | |
11985 | /// Attempts to detect a user writing into a piece of memory that's impossible |
11986 | /// to figure out the size of by just using types. |
11987 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
11988 | const SubobjectDesignator &Designator = LVal.Designator; |
11989 | // Notes: |
11990 | // - Users can only write off of the end when we have an invalid base. Invalid |
11991 | // bases imply we don't know where the memory came from. |
11992 | // - We used to be a bit more aggressive here; we'd only be conservative if |
11993 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
11994 | // broke some common standard library extensions (PR30346), but was |
11995 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
11996 | // with some sort of list. OTOH, it seems that GCC is always |
11997 | // conservative with the last element in structs (if it's an array), so our |
11998 | // current behavior is more compatible than an explicit list approach would |
11999 | // be. |
12000 | auto isFlexibleArrayMember = [&] { |
12001 | using FAMKind = LangOptions::StrictFlexArraysLevelKind; |
12002 | FAMKind StrictFlexArraysLevel = |
12003 | Ctx.getLangOpts().getStrictFlexArraysLevel(); |
12004 | |
12005 | if (Designator.isMostDerivedAnUnsizedArray()) |
12006 | return true; |
12007 | |
12008 | if (StrictFlexArraysLevel == FAMKind::Default) |
12009 | return true; |
12010 | |
12011 | if (Designator.getMostDerivedArraySize() == 0 && |
12012 | StrictFlexArraysLevel != FAMKind::IncompleteOnly) |
12013 | return true; |
12014 | |
12015 | if (Designator.getMostDerivedArraySize() == 1 && |
12016 | StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete) |
12017 | return true; |
12018 | |
12019 | return false; |
12020 | }; |
12021 | |
12022 | return LVal.InvalidBase && |
12023 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
12024 | Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() && |
12025 | isDesignatorAtObjectEnd(Ctx, LVal); |
12026 | } |
12027 | |
12028 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
12029 | /// Fails if the conversion would cause loss of precision. |
12030 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
12031 | CharUnits &Result) { |
12032 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
12033 | if (Int.ugt(RHS: CharUnitsMax)) |
12034 | return false; |
12035 | Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue()); |
12036 | return true; |
12037 | } |
12038 | |
12039 | /// If we're evaluating the object size of an instance of a struct that |
12040 | /// contains a flexible array member, add the size of the initializer. |
12041 | static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T, |
12042 | const LValue &LV, CharUnits &Size) { |
12043 | if (!T.isNull() && T->isStructureType() && |
12044 | T->getAsStructureType()->getDecl()->hasFlexibleArrayMember()) |
12045 | if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>()) |
12046 | if (const auto *VD = dyn_cast<VarDecl>(Val: V)) |
12047 | if (VD->hasInit()) |
12048 | Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx); |
12049 | } |
12050 | |
12051 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
12052 | /// determine how many bytes exist from the beginning of the object to either |
12053 | /// the end of the current subobject, or the end of the object itself, depending |
12054 | /// on what the LValue looks like + the value of Type. |
12055 | /// |
12056 | /// If this returns false, the value of Result is undefined. |
12057 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
12058 | unsigned Type, const LValue &LVal, |
12059 | CharUnits &EndOffset) { |
12060 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
12061 | |
12062 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
12063 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
12064 | return false; |
12065 | return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result); |
12066 | }; |
12067 | |
12068 | // We want to evaluate the size of the entire object. This is a valid fallback |
12069 | // for when Type=1 and the designator is invalid, because we're asked for an |
12070 | // upper-bound. |
12071 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
12072 | // Type=3 wants a lower bound, so we can't fall back to this. |
12073 | if (Type == 3 && !DetermineForCompleteObject) |
12074 | return false; |
12075 | |
12076 | llvm::APInt APEndOffset; |
12077 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12078 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12079 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12080 | |
12081 | if (LVal.InvalidBase) |
12082 | return false; |
12083 | |
12084 | QualType BaseTy = getObjectType(B: LVal.getLValueBase()); |
12085 | const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset); |
12086 | addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset); |
12087 | return Ret; |
12088 | } |
12089 | |
12090 | // We want to evaluate the size of a subobject. |
12091 | const SubobjectDesignator &Designator = LVal.Designator; |
12092 | |
12093 | // The following is a moderately common idiom in C: |
12094 | // |
12095 | // struct Foo { int a; char c[1]; }; |
12096 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
12097 | // strcpy(&F->c[0], Bar); |
12098 | // |
12099 | // In order to not break too much legacy code, we need to support it. |
12100 | if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) { |
12101 | // If we can resolve this to an alloc_size call, we can hand that back, |
12102 | // because we know for certain how many bytes there are to write to. |
12103 | llvm::APInt APEndOffset; |
12104 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12105 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12106 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12107 | |
12108 | // If we cannot determine the size of the initial allocation, then we can't |
12109 | // given an accurate upper-bound. However, we are still able to give |
12110 | // conservative lower-bounds for Type=3. |
12111 | if (Type == 1) |
12112 | return false; |
12113 | } |
12114 | |
12115 | CharUnits BytesPerElem; |
12116 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
12117 | return false; |
12118 | |
12119 | // According to the GCC documentation, we want the size of the subobject |
12120 | // denoted by the pointer. But that's not quite right -- what we actually |
12121 | // want is the size of the immediately-enclosing array, if there is one. |
12122 | int64_t ElemsRemaining; |
12123 | if (Designator.MostDerivedIsArrayElement && |
12124 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
12125 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
12126 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
12127 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
12128 | } else { |
12129 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
12130 | } |
12131 | |
12132 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
12133 | return true; |
12134 | } |
12135 | |
12136 | /// Tries to evaluate the __builtin_object_size for @p E. If successful, |
12137 | /// returns true and stores the result in @p Size. |
12138 | /// |
12139 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
12140 | /// is to be treated as an Error in IntExprEvaluator. |
12141 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
12142 | EvalInfo &Info, uint64_t &Size) { |
12143 | // Determine the denoted object. |
12144 | LValue LVal; |
12145 | { |
12146 | // The operand of __builtin_object_size is never evaluated for side-effects. |
12147 | // If there are any, but we can determine the pointed-to object anyway, then |
12148 | // ignore the side-effects. |
12149 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
12150 | IgnoreSideEffectsRAII Fold(Info); |
12151 | |
12152 | if (E->isGLValue()) { |
12153 | // It's possible for us to be given GLValues if we're called via |
12154 | // Expr::tryEvaluateObjectSize. |
12155 | APValue RVal; |
12156 | if (!EvaluateAsRValue(Info, E, Result&: RVal)) |
12157 | return false; |
12158 | LVal.setFrom(Ctx&: Info.Ctx, V: RVal); |
12159 | } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info, |
12160 | /*InvalidBaseOK=*/true)) |
12161 | return false; |
12162 | } |
12163 | |
12164 | // If we point to before the start of the object, there are no accessible |
12165 | // bytes. |
12166 | if (LVal.getLValueOffset().isNegative()) { |
12167 | Size = 0; |
12168 | return true; |
12169 | } |
12170 | |
12171 | CharUnits EndOffset; |
12172 | if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset)) |
12173 | return false; |
12174 | |
12175 | // If we've fallen outside of the end offset, just pretend there's nothing to |
12176 | // write to/read from. |
12177 | if (EndOffset <= LVal.getLValueOffset()) |
12178 | Size = 0; |
12179 | else |
12180 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
12181 | return true; |
12182 | } |
12183 | |
12184 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
12185 | if (!IsConstantEvaluatedBuiltinCall(E)) |
12186 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
12187 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
12188 | } |
12189 | |
12190 | static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info, |
12191 | APValue &Val, APSInt &Alignment) { |
12192 | QualType SrcTy = E->getArg(Arg: 0)->getType(); |
12193 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment)) |
12194 | return false; |
12195 | // Even though we are evaluating integer expressions we could get a pointer |
12196 | // argument for the __builtin_is_aligned() case. |
12197 | if (SrcTy->isPointerType()) { |
12198 | LValue Ptr; |
12199 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info)) |
12200 | return false; |
12201 | Ptr.moveInto(V&: Val); |
12202 | } else if (!SrcTy->isIntegralOrEnumerationType()) { |
12203 | Info.FFDiag(E: E->getArg(Arg: 0)); |
12204 | return false; |
12205 | } else { |
12206 | APSInt SrcInt; |
12207 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info)) |
12208 | return false; |
12209 | assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() && |
12210 | "Bit widths must be the same" ); |
12211 | Val = APValue(SrcInt); |
12212 | } |
12213 | assert(Val.hasValue()); |
12214 | return true; |
12215 | } |
12216 | |
12217 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
12218 | unsigned BuiltinOp) { |
12219 | switch (BuiltinOp) { |
12220 | default: |
12221 | return false; |
12222 | |
12223 | case Builtin::BI__builtin_dynamic_object_size: |
12224 | case Builtin::BI__builtin_object_size: { |
12225 | // The type was checked when we built the expression. |
12226 | unsigned Type = |
12227 | E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
12228 | assert(Type <= 3 && "unexpected type" ); |
12229 | |
12230 | uint64_t Size; |
12231 | if (tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, Size)) |
12232 | return Success(Size, E); |
12233 | |
12234 | if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx)) |
12235 | return Success((Type & 2) ? 0 : -1, E); |
12236 | |
12237 | // Expression had no side effects, but we couldn't statically determine the |
12238 | // size of the referenced object. |
12239 | switch (Info.EvalMode) { |
12240 | case EvalInfo::EM_ConstantExpression: |
12241 | case EvalInfo::EM_ConstantFold: |
12242 | case EvalInfo::EM_IgnoreSideEffects: |
12243 | // Leave it to IR generation. |
12244 | return Error(E); |
12245 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
12246 | // Reduce it to a constant now. |
12247 | return Success((Type & 2) ? 0 : -1, E); |
12248 | } |
12249 | |
12250 | llvm_unreachable("unexpected EvalMode" ); |
12251 | } |
12252 | |
12253 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
12254 | analyze_os_log::OSLogBufferLayout Layout; |
12255 | analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout); |
12256 | return Success(Layout.size().getQuantity(), E); |
12257 | } |
12258 | |
12259 | case Builtin::BI__builtin_is_aligned: { |
12260 | APValue Src; |
12261 | APSInt Alignment; |
12262 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12263 | return false; |
12264 | if (Src.isLValue()) { |
12265 | // If we evaluated a pointer, check the minimum known alignment. |
12266 | LValue Ptr; |
12267 | Ptr.setFrom(Ctx&: Info.Ctx, V: Src); |
12268 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr); |
12269 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset); |
12270 | // We can return true if the known alignment at the computed offset is |
12271 | // greater than the requested alignment. |
12272 | assert(PtrAlign.isPowerOfTwo()); |
12273 | assert(Alignment.isPowerOf2()); |
12274 | if (PtrAlign.getQuantity() >= Alignment) |
12275 | return Success(1, E); |
12276 | // If the alignment is not known to be sufficient, some cases could still |
12277 | // be aligned at run time. However, if the requested alignment is less or |
12278 | // equal to the base alignment and the offset is not aligned, we know that |
12279 | // the run-time value can never be aligned. |
12280 | if (BaseAlignment.getQuantity() >= Alignment && |
12281 | PtrAlign.getQuantity() < Alignment) |
12282 | return Success(0, E); |
12283 | // Otherwise we can't infer whether the value is sufficiently aligned. |
12284 | // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N) |
12285 | // in cases where we can't fully evaluate the pointer. |
12286 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute) |
12287 | << Alignment; |
12288 | return false; |
12289 | } |
12290 | assert(Src.isInt()); |
12291 | return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E); |
12292 | } |
12293 | case Builtin::BI__builtin_align_up: { |
12294 | APValue Src; |
12295 | APSInt Alignment; |
12296 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12297 | return false; |
12298 | if (!Src.isInt()) |
12299 | return Error(E); |
12300 | APSInt AlignedVal = |
12301 | APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1), |
12302 | Src.getInt().isUnsigned()); |
12303 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
12304 | return Success(AlignedVal, E); |
12305 | } |
12306 | case Builtin::BI__builtin_align_down: { |
12307 | APValue Src; |
12308 | APSInt Alignment; |
12309 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12310 | return false; |
12311 | if (!Src.isInt()) |
12312 | return Error(E); |
12313 | APSInt AlignedVal = |
12314 | APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned()); |
12315 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
12316 | return Success(AlignedVal, E); |
12317 | } |
12318 | |
12319 | case Builtin::BI__builtin_bitreverse8: |
12320 | case Builtin::BI__builtin_bitreverse16: |
12321 | case Builtin::BI__builtin_bitreverse32: |
12322 | case Builtin::BI__builtin_bitreverse64: { |
12323 | APSInt Val; |
12324 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12325 | return false; |
12326 | |
12327 | return Success(Val.reverseBits(), E); |
12328 | } |
12329 | |
12330 | case Builtin::BI__builtin_bswap16: |
12331 | case Builtin::BI__builtin_bswap32: |
12332 | case Builtin::BI__builtin_bswap64: { |
12333 | APSInt Val; |
12334 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12335 | return false; |
12336 | |
12337 | return Success(Val.byteSwap(), E); |
12338 | } |
12339 | |
12340 | case Builtin::BI__builtin_classify_type: |
12341 | return Success((int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E); |
12342 | |
12343 | case Builtin::BI__builtin_clrsb: |
12344 | case Builtin::BI__builtin_clrsbl: |
12345 | case Builtin::BI__builtin_clrsbll: { |
12346 | APSInt Val; |
12347 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12348 | return false; |
12349 | |
12350 | return Success(Val.getBitWidth() - Val.getSignificantBits(), E); |
12351 | } |
12352 | |
12353 | case Builtin::BI__builtin_clz: |
12354 | case Builtin::BI__builtin_clzl: |
12355 | case Builtin::BI__builtin_clzll: |
12356 | case Builtin::BI__builtin_clzs: |
12357 | case Builtin::BI__builtin_clzg: |
12358 | case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes |
12359 | case Builtin::BI__lzcnt: |
12360 | case Builtin::BI__lzcnt64: { |
12361 | APSInt Val; |
12362 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12363 | return false; |
12364 | |
12365 | std::optional<APSInt> Fallback; |
12366 | if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) { |
12367 | APSInt FallbackTemp; |
12368 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info)) |
12369 | return false; |
12370 | Fallback = FallbackTemp; |
12371 | } |
12372 | |
12373 | if (!Val) { |
12374 | if (Fallback) |
12375 | return Success(*Fallback, E); |
12376 | |
12377 | // When the argument is 0, the result of GCC builtins is undefined, |
12378 | // whereas for Microsoft intrinsics, the result is the bit-width of the |
12379 | // argument. |
12380 | bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 && |
12381 | BuiltinOp != Builtin::BI__lzcnt && |
12382 | BuiltinOp != Builtin::BI__lzcnt64; |
12383 | |
12384 | if (ZeroIsUndefined) |
12385 | return Error(E); |
12386 | } |
12387 | |
12388 | return Success(Val.countl_zero(), E); |
12389 | } |
12390 | |
12391 | case Builtin::BI__builtin_constant_p: { |
12392 | const Expr *Arg = E->getArg(Arg: 0); |
12393 | if (EvaluateBuiltinConstantP(Info, Arg)) |
12394 | return Success(true, E); |
12395 | if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) { |
12396 | // Outside a constant context, eagerly evaluate to false in the presence |
12397 | // of side-effects in order to avoid -Wunsequenced false-positives in |
12398 | // a branch on __builtin_constant_p(expr). |
12399 | return Success(false, E); |
12400 | } |
12401 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12402 | return false; |
12403 | } |
12404 | |
12405 | case Builtin::BI__builtin_is_constant_evaluated: { |
12406 | const auto *Callee = Info.CurrentCall->getCallee(); |
12407 | if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression && |
12408 | (Info.CallStackDepth == 1 || |
12409 | (Info.CallStackDepth == 2 && Callee->isInStdNamespace() && |
12410 | Callee->getIdentifier() && |
12411 | Callee->getIdentifier()->isStr("is_constant_evaluated" )))) { |
12412 | // FIXME: Find a better way to avoid duplicated diagnostics. |
12413 | if (Info.EvalStatus.Diag) |
12414 | Info.report((Info.CallStackDepth == 1) |
12415 | ? E->getExprLoc() |
12416 | : Info.CurrentCall->getCallRange().getBegin(), |
12417 | diag::warn_is_constant_evaluated_always_true_constexpr) |
12418 | << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated" |
12419 | : "std::is_constant_evaluated" ); |
12420 | } |
12421 | |
12422 | return Success(Info.InConstantContext, E); |
12423 | } |
12424 | |
12425 | case Builtin::BI__builtin_ctz: |
12426 | case Builtin::BI__builtin_ctzl: |
12427 | case Builtin::BI__builtin_ctzll: |
12428 | case Builtin::BI__builtin_ctzs: |
12429 | case Builtin::BI__builtin_ctzg: { |
12430 | APSInt Val; |
12431 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12432 | return false; |
12433 | |
12434 | std::optional<APSInt> Fallback; |
12435 | if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) { |
12436 | APSInt FallbackTemp; |
12437 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info)) |
12438 | return false; |
12439 | Fallback = FallbackTemp; |
12440 | } |
12441 | |
12442 | if (!Val) { |
12443 | if (Fallback) |
12444 | return Success(*Fallback, E); |
12445 | |
12446 | return Error(E); |
12447 | } |
12448 | |
12449 | return Success(Val.countr_zero(), E); |
12450 | } |
12451 | |
12452 | case Builtin::BI__builtin_eh_return_data_regno: { |
12453 | int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
12454 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand); |
12455 | return Success(Operand, E); |
12456 | } |
12457 | |
12458 | case Builtin::BI__builtin_expect: |
12459 | case Builtin::BI__builtin_expect_with_probability: |
12460 | return Visit(E->getArg(Arg: 0)); |
12461 | |
12462 | case Builtin::BI__builtin_ffs: |
12463 | case Builtin::BI__builtin_ffsl: |
12464 | case Builtin::BI__builtin_ffsll: { |
12465 | APSInt Val; |
12466 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12467 | return false; |
12468 | |
12469 | unsigned N = Val.countr_zero(); |
12470 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
12471 | } |
12472 | |
12473 | case Builtin::BI__builtin_fpclassify: { |
12474 | APFloat Val(0.0); |
12475 | if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info)) |
12476 | return false; |
12477 | unsigned Arg; |
12478 | switch (Val.getCategory()) { |
12479 | case APFloat::fcNaN: Arg = 0; break; |
12480 | case APFloat::fcInfinity: Arg = 1; break; |
12481 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
12482 | case APFloat::fcZero: Arg = 4; break; |
12483 | } |
12484 | return Visit(E->getArg(Arg)); |
12485 | } |
12486 | |
12487 | case Builtin::BI__builtin_isinf_sign: { |
12488 | APFloat Val(0.0); |
12489 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12490 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
12491 | } |
12492 | |
12493 | case Builtin::BI__builtin_isinf: { |
12494 | APFloat Val(0.0); |
12495 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12496 | Success(Val.isInfinity() ? 1 : 0, E); |
12497 | } |
12498 | |
12499 | case Builtin::BI__builtin_isfinite: { |
12500 | APFloat Val(0.0); |
12501 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12502 | Success(Val.isFinite() ? 1 : 0, E); |
12503 | } |
12504 | |
12505 | case Builtin::BI__builtin_isnan: { |
12506 | APFloat Val(0.0); |
12507 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12508 | Success(Val.isNaN() ? 1 : 0, E); |
12509 | } |
12510 | |
12511 | case Builtin::BI__builtin_isnormal: { |
12512 | APFloat Val(0.0); |
12513 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12514 | Success(Val.isNormal() ? 1 : 0, E); |
12515 | } |
12516 | |
12517 | case Builtin::BI__builtin_issubnormal: { |
12518 | APFloat Val(0.0); |
12519 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12520 | Success(Val.isDenormal() ? 1 : 0, E); |
12521 | } |
12522 | |
12523 | case Builtin::BI__builtin_iszero: { |
12524 | APFloat Val(0.0); |
12525 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12526 | Success(Val.isZero() ? 1 : 0, E); |
12527 | } |
12528 | |
12529 | case Builtin::BI__builtin_issignaling: { |
12530 | APFloat Val(0.0); |
12531 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12532 | Success(Val.isSignaling() ? 1 : 0, E); |
12533 | } |
12534 | |
12535 | case Builtin::BI__builtin_isfpclass: { |
12536 | APSInt MaskVal; |
12537 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info)) |
12538 | return false; |
12539 | unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue()); |
12540 | APFloat Val(0.0); |
12541 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12542 | Success((Val.classify() & Test) ? 1 : 0, E); |
12543 | } |
12544 | |
12545 | case Builtin::BI__builtin_parity: |
12546 | case Builtin::BI__builtin_parityl: |
12547 | case Builtin::BI__builtin_parityll: { |
12548 | APSInt Val; |
12549 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12550 | return false; |
12551 | |
12552 | return Success(Val.popcount() % 2, E); |
12553 | } |
12554 | |
12555 | case Builtin::BI__builtin_popcount: |
12556 | case Builtin::BI__builtin_popcountl: |
12557 | case Builtin::BI__builtin_popcountll: |
12558 | case Builtin::BI__builtin_popcountg: |
12559 | case Builtin::BI__popcnt16: // Microsoft variants of popcount |
12560 | case Builtin::BI__popcnt: |
12561 | case Builtin::BI__popcnt64: { |
12562 | APSInt Val; |
12563 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12564 | return false; |
12565 | |
12566 | return Success(Val.popcount(), E); |
12567 | } |
12568 | |
12569 | case Builtin::BI__builtin_rotateleft8: |
12570 | case Builtin::BI__builtin_rotateleft16: |
12571 | case Builtin::BI__builtin_rotateleft32: |
12572 | case Builtin::BI__builtin_rotateleft64: |
12573 | case Builtin::BI_rotl8: // Microsoft variants of rotate right |
12574 | case Builtin::BI_rotl16: |
12575 | case Builtin::BI_rotl: |
12576 | case Builtin::BI_lrotl: |
12577 | case Builtin::BI_rotl64: { |
12578 | APSInt Val, Amt; |
12579 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
12580 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
12581 | return false; |
12582 | |
12583 | return Success(Val.rotl(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
12584 | } |
12585 | |
12586 | case Builtin::BI__builtin_rotateright8: |
12587 | case Builtin::BI__builtin_rotateright16: |
12588 | case Builtin::BI__builtin_rotateright32: |
12589 | case Builtin::BI__builtin_rotateright64: |
12590 | case Builtin::BI_rotr8: // Microsoft variants of rotate right |
12591 | case Builtin::BI_rotr16: |
12592 | case Builtin::BI_rotr: |
12593 | case Builtin::BI_lrotr: |
12594 | case Builtin::BI_rotr64: { |
12595 | APSInt Val, Amt; |
12596 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
12597 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
12598 | return false; |
12599 | |
12600 | return Success(Val.rotr(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
12601 | } |
12602 | |
12603 | case Builtin::BIstrlen: |
12604 | case Builtin::BIwcslen: |
12605 | // A call to strlen is not a constant expression. |
12606 | if (Info.getLangOpts().CPlusPlus11) |
12607 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
12608 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
12609 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
12610 | else |
12611 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12612 | [[fallthrough]]; |
12613 | case Builtin::BI__builtin_strlen: |
12614 | case Builtin::BI__builtin_wcslen: { |
12615 | // As an extension, we support __builtin_strlen() as a constant expression, |
12616 | // and support folding strlen() to a constant. |
12617 | uint64_t StrLen; |
12618 | if (EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Result&: StrLen, Info)) |
12619 | return Success(StrLen, E); |
12620 | return false; |
12621 | } |
12622 | |
12623 | case Builtin::BIstrcmp: |
12624 | case Builtin::BIwcscmp: |
12625 | case Builtin::BIstrncmp: |
12626 | case Builtin::BIwcsncmp: |
12627 | case Builtin::BImemcmp: |
12628 | case Builtin::BIbcmp: |
12629 | case Builtin::BIwmemcmp: |
12630 | // A call to strlen is not a constant expression. |
12631 | if (Info.getLangOpts().CPlusPlus11) |
12632 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
12633 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
12634 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
12635 | else |
12636 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12637 | [[fallthrough]]; |
12638 | case Builtin::BI__builtin_strcmp: |
12639 | case Builtin::BI__builtin_wcscmp: |
12640 | case Builtin::BI__builtin_strncmp: |
12641 | case Builtin::BI__builtin_wcsncmp: |
12642 | case Builtin::BI__builtin_memcmp: |
12643 | case Builtin::BI__builtin_bcmp: |
12644 | case Builtin::BI__builtin_wmemcmp: { |
12645 | LValue String1, String2; |
12646 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) || |
12647 | !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info)) |
12648 | return false; |
12649 | |
12650 | uint64_t MaxLength = uint64_t(-1); |
12651 | if (BuiltinOp != Builtin::BIstrcmp && |
12652 | BuiltinOp != Builtin::BIwcscmp && |
12653 | BuiltinOp != Builtin::BI__builtin_strcmp && |
12654 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
12655 | APSInt N; |
12656 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
12657 | return false; |
12658 | MaxLength = N.getZExtValue(); |
12659 | } |
12660 | |
12661 | // Empty substrings compare equal by definition. |
12662 | if (MaxLength == 0u) |
12663 | return Success(0, E); |
12664 | |
12665 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
12666 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
12667 | String1.Designator.Invalid || String2.Designator.Invalid) |
12668 | return false; |
12669 | |
12670 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
12671 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
12672 | |
12673 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
12674 | BuiltinOp == Builtin::BIbcmp || |
12675 | BuiltinOp == Builtin::BI__builtin_memcmp || |
12676 | BuiltinOp == Builtin::BI__builtin_bcmp; |
12677 | |
12678 | assert(IsRawByte || |
12679 | (Info.Ctx.hasSameUnqualifiedType( |
12680 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
12681 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
12682 | |
12683 | // For memcmp, allow comparing any arrays of '[[un]signed] char' or |
12684 | // 'char8_t', but no other types. |
12685 | if (IsRawByte && |
12686 | !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) { |
12687 | // FIXME: Consider using our bit_cast implementation to support this. |
12688 | Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported) |
12689 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str() |
12690 | << CharTy1 << CharTy2; |
12691 | return false; |
12692 | } |
12693 | |
12694 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
12695 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
12696 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
12697 | Char1.isInt() && Char2.isInt(); |
12698 | }; |
12699 | const auto &AdvanceElems = [&] { |
12700 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
12701 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
12702 | }; |
12703 | |
12704 | bool StopAtNull = |
12705 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
12706 | BuiltinOp != Builtin::BIwmemcmp && |
12707 | BuiltinOp != Builtin::BI__builtin_memcmp && |
12708 | BuiltinOp != Builtin::BI__builtin_bcmp && |
12709 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
12710 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
12711 | BuiltinOp == Builtin::BIwcsncmp || |
12712 | BuiltinOp == Builtin::BIwmemcmp || |
12713 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
12714 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
12715 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
12716 | |
12717 | for (; MaxLength; --MaxLength) { |
12718 | APValue Char1, Char2; |
12719 | if (!ReadCurElems(Char1, Char2)) |
12720 | return false; |
12721 | if (Char1.getInt().ne(RHS: Char2.getInt())) { |
12722 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
12723 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
12724 | // memcmp always compares unsigned chars. |
12725 | return Success(Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E); |
12726 | } |
12727 | if (StopAtNull && !Char1.getInt()) |
12728 | return Success(0, E); |
12729 | assert(!(StopAtNull && !Char2.getInt())); |
12730 | if (!AdvanceElems()) |
12731 | return false; |
12732 | } |
12733 | // We hit the strncmp / memcmp limit. |
12734 | return Success(0, E); |
12735 | } |
12736 | |
12737 | case Builtin::BI__atomic_always_lock_free: |
12738 | case Builtin::BI__atomic_is_lock_free: |
12739 | case Builtin::BI__c11_atomic_is_lock_free: { |
12740 | APSInt SizeVal; |
12741 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info)) |
12742 | return false; |
12743 | |
12744 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
12745 | // of two less than or equal to the maximum inline atomic width, we know it |
12746 | // is lock-free. If the size isn't a power of two, or greater than the |
12747 | // maximum alignment where we promote atomics, we know it is not lock-free |
12748 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
12749 | // the answer can only be determined at runtime; for example, 16-byte |
12750 | // atomics have lock-free implementations on some, but not all, |
12751 | // x86-64 processors. |
12752 | |
12753 | // Check power-of-two. |
12754 | CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue()); |
12755 | if (Size.isPowerOfTwo()) { |
12756 | // Check against inlining width. |
12757 | unsigned InlineWidthBits = |
12758 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
12759 | if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) { |
12760 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
12761 | Size == CharUnits::One() || |
12762 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
12763 | Expr::NPC_NeverValueDependent)) |
12764 | // OK, we will inline appropriately-aligned operations of this size, |
12765 | // and _Atomic(T) is appropriately-aligned. |
12766 | return Success(1, E); |
12767 | |
12768 | QualType PointeeType = E->getArg(Arg: 1)->IgnoreImpCasts()->getType()-> |
12769 | castAs<PointerType>()->getPointeeType(); |
12770 | if (!PointeeType->isIncompleteType() && |
12771 | Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) { |
12772 | // OK, we will inline operations on this object. |
12773 | return Success(1, E); |
12774 | } |
12775 | } |
12776 | } |
12777 | |
12778 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
12779 | Success(0, E) : Error(E); |
12780 | } |
12781 | case Builtin::BI__builtin_addcb: |
12782 | case Builtin::BI__builtin_addcs: |
12783 | case Builtin::BI__builtin_addc: |
12784 | case Builtin::BI__builtin_addcl: |
12785 | case Builtin::BI__builtin_addcll: |
12786 | case Builtin::BI__builtin_subcb: |
12787 | case Builtin::BI__builtin_subcs: |
12788 | case Builtin::BI__builtin_subc: |
12789 | case Builtin::BI__builtin_subcl: |
12790 | case Builtin::BI__builtin_subcll: { |
12791 | LValue CarryOutLValue; |
12792 | APSInt LHS, RHS, CarryIn, CarryOut, Result; |
12793 | QualType ResultType = E->getArg(Arg: 0)->getType(); |
12794 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
12795 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) || |
12796 | !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) || |
12797 | !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info)) |
12798 | return false; |
12799 | // Copy the number of bits and sign. |
12800 | Result = LHS; |
12801 | CarryOut = LHS; |
12802 | |
12803 | bool FirstOverflowed = false; |
12804 | bool SecondOverflowed = false; |
12805 | switch (BuiltinOp) { |
12806 | default: |
12807 | llvm_unreachable("Invalid value for BuiltinOp" ); |
12808 | case Builtin::BI__builtin_addcb: |
12809 | case Builtin::BI__builtin_addcs: |
12810 | case Builtin::BI__builtin_addc: |
12811 | case Builtin::BI__builtin_addcl: |
12812 | case Builtin::BI__builtin_addcll: |
12813 | Result = |
12814 | LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed); |
12815 | break; |
12816 | case Builtin::BI__builtin_subcb: |
12817 | case Builtin::BI__builtin_subcs: |
12818 | case Builtin::BI__builtin_subc: |
12819 | case Builtin::BI__builtin_subcl: |
12820 | case Builtin::BI__builtin_subcll: |
12821 | Result = |
12822 | LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed); |
12823 | break; |
12824 | } |
12825 | |
12826 | // It is possible for both overflows to happen but CGBuiltin uses an OR so |
12827 | // this is consistent. |
12828 | CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed); |
12829 | APValue APV{CarryOut}; |
12830 | if (!handleAssignment(Info, E, CarryOutLValue, ResultType, APV)) |
12831 | return false; |
12832 | return Success(Result, E); |
12833 | } |
12834 | case Builtin::BI__builtin_add_overflow: |
12835 | case Builtin::BI__builtin_sub_overflow: |
12836 | case Builtin::BI__builtin_mul_overflow: |
12837 | case Builtin::BI__builtin_sadd_overflow: |
12838 | case Builtin::BI__builtin_uadd_overflow: |
12839 | case Builtin::BI__builtin_uaddl_overflow: |
12840 | case Builtin::BI__builtin_uaddll_overflow: |
12841 | case Builtin::BI__builtin_usub_overflow: |
12842 | case Builtin::BI__builtin_usubl_overflow: |
12843 | case Builtin::BI__builtin_usubll_overflow: |
12844 | case Builtin::BI__builtin_umul_overflow: |
12845 | case Builtin::BI__builtin_umull_overflow: |
12846 | case Builtin::BI__builtin_umulll_overflow: |
12847 | case Builtin::BI__builtin_saddl_overflow: |
12848 | case Builtin::BI__builtin_saddll_overflow: |
12849 | case Builtin::BI__builtin_ssub_overflow: |
12850 | case Builtin::BI__builtin_ssubl_overflow: |
12851 | case Builtin::BI__builtin_ssubll_overflow: |
12852 | case Builtin::BI__builtin_smul_overflow: |
12853 | case Builtin::BI__builtin_smull_overflow: |
12854 | case Builtin::BI__builtin_smulll_overflow: { |
12855 | LValue ResultLValue; |
12856 | APSInt LHS, RHS; |
12857 | |
12858 | QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType(); |
12859 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
12860 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) || |
12861 | !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info)) |
12862 | return false; |
12863 | |
12864 | APSInt Result; |
12865 | bool DidOverflow = false; |
12866 | |
12867 | // If the types don't have to match, enlarge all 3 to the largest of them. |
12868 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12869 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12870 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12871 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
12872 | ResultType->isSignedIntegerOrEnumerationType(); |
12873 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
12874 | ResultType->isSignedIntegerOrEnumerationType(); |
12875 | uint64_t LHSSize = LHS.getBitWidth(); |
12876 | uint64_t RHSSize = RHS.getBitWidth(); |
12877 | uint64_t ResultSize = Info.Ctx.getTypeSize(T: ResultType); |
12878 | uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize); |
12879 | |
12880 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
12881 | // could do this ONLY if there is a signed and an unsigned that both have |
12882 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
12883 | // caught in the shrink-to-result later anyway. |
12884 | if (IsSigned && !AllSigned) |
12885 | ++MaxBits; |
12886 | |
12887 | LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned); |
12888 | RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned); |
12889 | Result = APSInt(MaxBits, !IsSigned); |
12890 | } |
12891 | |
12892 | // Find largest int. |
12893 | switch (BuiltinOp) { |
12894 | default: |
12895 | llvm_unreachable("Invalid value for BuiltinOp" ); |
12896 | case Builtin::BI__builtin_add_overflow: |
12897 | case Builtin::BI__builtin_sadd_overflow: |
12898 | case Builtin::BI__builtin_saddl_overflow: |
12899 | case Builtin::BI__builtin_saddll_overflow: |
12900 | case Builtin::BI__builtin_uadd_overflow: |
12901 | case Builtin::BI__builtin_uaddl_overflow: |
12902 | case Builtin::BI__builtin_uaddll_overflow: |
12903 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow) |
12904 | : LHS.uadd_ov(RHS, Overflow&: DidOverflow); |
12905 | break; |
12906 | case Builtin::BI__builtin_sub_overflow: |
12907 | case Builtin::BI__builtin_ssub_overflow: |
12908 | case Builtin::BI__builtin_ssubl_overflow: |
12909 | case Builtin::BI__builtin_ssubll_overflow: |
12910 | case Builtin::BI__builtin_usub_overflow: |
12911 | case Builtin::BI__builtin_usubl_overflow: |
12912 | case Builtin::BI__builtin_usubll_overflow: |
12913 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow) |
12914 | : LHS.usub_ov(RHS, Overflow&: DidOverflow); |
12915 | break; |
12916 | case Builtin::BI__builtin_mul_overflow: |
12917 | case Builtin::BI__builtin_smul_overflow: |
12918 | case Builtin::BI__builtin_smull_overflow: |
12919 | case Builtin::BI__builtin_smulll_overflow: |
12920 | case Builtin::BI__builtin_umul_overflow: |
12921 | case Builtin::BI__builtin_umull_overflow: |
12922 | case Builtin::BI__builtin_umulll_overflow: |
12923 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow) |
12924 | : LHS.umul_ov(RHS, Overflow&: DidOverflow); |
12925 | break; |
12926 | } |
12927 | |
12928 | // In the case where multiple sizes are allowed, truncate and see if |
12929 | // the values are the same. |
12930 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12931 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12932 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12933 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
12934 | // since it will give us the behavior of a TruncOrSelf in the case where |
12935 | // its parameter <= its size. We previously set Result to be at least the |
12936 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
12937 | // will work exactly like TruncOrSelf. |
12938 | APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getTypeSize(T: ResultType)); |
12939 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
12940 | |
12941 | if (!APSInt::isSameValue(I1: Temp, I2: Result)) |
12942 | DidOverflow = true; |
12943 | Result = Temp; |
12944 | } |
12945 | |
12946 | APValue APV{Result}; |
12947 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
12948 | return false; |
12949 | return Success(DidOverflow, E); |
12950 | } |
12951 | } |
12952 | } |
12953 | |
12954 | /// Determine whether this is a pointer past the end of the complete |
12955 | /// object referred to by the lvalue. |
12956 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
12957 | const LValue &LV) { |
12958 | // A null pointer can be viewed as being "past the end" but we don't |
12959 | // choose to look at it that way here. |
12960 | if (!LV.getLValueBase()) |
12961 | return false; |
12962 | |
12963 | // If the designator is valid and refers to a subobject, we're not pointing |
12964 | // past the end. |
12965 | if (!LV.getLValueDesignator().Invalid && |
12966 | !LV.getLValueDesignator().isOnePastTheEnd()) |
12967 | return false; |
12968 | |
12969 | // A pointer to an incomplete type might be past-the-end if the type's size is |
12970 | // zero. We cannot tell because the type is incomplete. |
12971 | QualType Ty = getType(B: LV.getLValueBase()); |
12972 | if (Ty->isIncompleteType()) |
12973 | return true; |
12974 | |
12975 | // Can't be past the end of an invalid object. |
12976 | if (LV.getLValueDesignator().Invalid) |
12977 | return false; |
12978 | |
12979 | // We're a past-the-end pointer if we point to the byte after the object, |
12980 | // no matter what our type or path is. |
12981 | auto Size = Ctx.getTypeSizeInChars(T: Ty); |
12982 | return LV.getLValueOffset() == Size; |
12983 | } |
12984 | |
12985 | namespace { |
12986 | |
12987 | /// Data recursive integer evaluator of certain binary operators. |
12988 | /// |
12989 | /// We use a data recursive algorithm for binary operators so that we are able |
12990 | /// to handle extreme cases of chained binary operators without causing stack |
12991 | /// overflow. |
12992 | class DataRecursiveIntBinOpEvaluator { |
12993 | struct EvalResult { |
12994 | APValue Val; |
12995 | bool Failed = false; |
12996 | |
12997 | EvalResult() = default; |
12998 | |
12999 | void swap(EvalResult &RHS) { |
13000 | Val.swap(RHS&: RHS.Val); |
13001 | Failed = RHS.Failed; |
13002 | RHS.Failed = false; |
13003 | } |
13004 | }; |
13005 | |
13006 | struct Job { |
13007 | const Expr *E; |
13008 | EvalResult LHSResult; // meaningful only for binary operator expression. |
13009 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
13010 | |
13011 | Job() = default; |
13012 | Job(Job &&) = default; |
13013 | |
13014 | void startSpeculativeEval(EvalInfo &Info) { |
13015 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
13016 | } |
13017 | |
13018 | private: |
13019 | SpeculativeEvaluationRAII SpecEvalRAII; |
13020 | }; |
13021 | |
13022 | SmallVector<Job, 16> Queue; |
13023 | |
13024 | IntExprEvaluator &IntEval; |
13025 | EvalInfo &Info; |
13026 | APValue &FinalResult; |
13027 | |
13028 | public: |
13029 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
13030 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
13031 | |
13032 | /// True if \param E is a binary operator that we are going to handle |
13033 | /// data recursively. |
13034 | /// We handle binary operators that are comma, logical, or that have operands |
13035 | /// with integral or enumeration type. |
13036 | static bool shouldEnqueue(const BinaryOperator *E) { |
13037 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
13038 | (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() && |
13039 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
13040 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
13041 | } |
13042 | |
13043 | bool Traverse(const BinaryOperator *E) { |
13044 | enqueue(E); |
13045 | EvalResult PrevResult; |
13046 | while (!Queue.empty()) |
13047 | process(Result&: PrevResult); |
13048 | |
13049 | if (PrevResult.Failed) return false; |
13050 | |
13051 | FinalResult.swap(RHS&: PrevResult.Val); |
13052 | return true; |
13053 | } |
13054 | |
13055 | private: |
13056 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
13057 | return IntEval.Success(Value, E, Result); |
13058 | } |
13059 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
13060 | return IntEval.Success(SI: Value, E, Result); |
13061 | } |
13062 | bool Error(const Expr *E) { |
13063 | return IntEval.Error(E); |
13064 | } |
13065 | bool Error(const Expr *E, diag::kind D) { |
13066 | return IntEval.Error(E, D); |
13067 | } |
13068 | |
13069 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
13070 | return Info.CCEDiag(E, DiagId: D); |
13071 | } |
13072 | |
13073 | // Returns true if visiting the RHS is necessary, false otherwise. |
13074 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
13075 | bool &SuppressRHSDiags); |
13076 | |
13077 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
13078 | const BinaryOperator *E, APValue &Result); |
13079 | |
13080 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
13081 | Result.Failed = !Evaluate(Result&: Result.Val, Info, E); |
13082 | if (Result.Failed) |
13083 | Result.Val = APValue(); |
13084 | } |
13085 | |
13086 | void process(EvalResult &Result); |
13087 | |
13088 | void enqueue(const Expr *E) { |
13089 | E = E->IgnoreParens(); |
13090 | Queue.resize(N: Queue.size()+1); |
13091 | Queue.back().E = E; |
13092 | Queue.back().Kind = Job::AnyExprKind; |
13093 | } |
13094 | }; |
13095 | |
13096 | } |
13097 | |
13098 | bool DataRecursiveIntBinOpEvaluator:: |
13099 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
13100 | bool &SuppressRHSDiags) { |
13101 | if (E->getOpcode() == BO_Comma) { |
13102 | // Ignore LHS but note if we could not evaluate it. |
13103 | if (LHSResult.Failed) |
13104 | return Info.noteSideEffect(); |
13105 | return true; |
13106 | } |
13107 | |
13108 | if (E->isLogicalOp()) { |
13109 | bool LHSAsBool; |
13110 | if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) { |
13111 | // We were able to evaluate the LHS, see if we can get away with not |
13112 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
13113 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
13114 | Success(LHSAsBool, E, LHSResult.Val); |
13115 | return false; // Ignore RHS |
13116 | } |
13117 | } else { |
13118 | LHSResult.Failed = true; |
13119 | |
13120 | // Since we weren't able to evaluate the left hand side, it |
13121 | // might have had side effects. |
13122 | if (!Info.noteSideEffect()) |
13123 | return false; |
13124 | |
13125 | // We can't evaluate the LHS; however, sometimes the result |
13126 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
13127 | // Don't ignore RHS and suppress diagnostics from this arm. |
13128 | SuppressRHSDiags = true; |
13129 | } |
13130 | |
13131 | return true; |
13132 | } |
13133 | |
13134 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
13135 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
13136 | |
13137 | if (LHSResult.Failed && !Info.noteFailure()) |
13138 | return false; // Ignore RHS; |
13139 | |
13140 | return true; |
13141 | } |
13142 | |
13143 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
13144 | bool IsSub) { |
13145 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
13146 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
13147 | // offsets. |
13148 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue" ); |
13149 | CharUnits &Offset = LVal.getLValueOffset(); |
13150 | uint64_t Offset64 = Offset.getQuantity(); |
13151 | uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue(); |
13152 | Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64 |
13153 | : Offset64 + Index64); |
13154 | } |
13155 | |
13156 | bool DataRecursiveIntBinOpEvaluator:: |
13157 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
13158 | const BinaryOperator *E, APValue &Result) { |
13159 | if (E->getOpcode() == BO_Comma) { |
13160 | if (RHSResult.Failed) |
13161 | return false; |
13162 | Result = RHSResult.Val; |
13163 | return true; |
13164 | } |
13165 | |
13166 | if (E->isLogicalOp()) { |
13167 | bool lhsResult, rhsResult; |
13168 | bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult); |
13169 | bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult); |
13170 | |
13171 | if (LHSIsOK) { |
13172 | if (RHSIsOK) { |
13173 | if (E->getOpcode() == BO_LOr) |
13174 | return Success(lhsResult || rhsResult, E, Result); |
13175 | else |
13176 | return Success(lhsResult && rhsResult, E, Result); |
13177 | } |
13178 | } else { |
13179 | if (RHSIsOK) { |
13180 | // We can't evaluate the LHS; however, sometimes the result |
13181 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
13182 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
13183 | return Success(rhsResult, E, Result); |
13184 | } |
13185 | } |
13186 | |
13187 | return false; |
13188 | } |
13189 | |
13190 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
13191 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
13192 | |
13193 | if (LHSResult.Failed || RHSResult.Failed) |
13194 | return false; |
13195 | |
13196 | const APValue &LHSVal = LHSResult.Val; |
13197 | const APValue &RHSVal = RHSResult.Val; |
13198 | |
13199 | // Handle cases like (unsigned long)&a + 4. |
13200 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
13201 | Result = LHSVal; |
13202 | addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub); |
13203 | return true; |
13204 | } |
13205 | |
13206 | // Handle cases like 4 + (unsigned long)&a |
13207 | if (E->getOpcode() == BO_Add && |
13208 | RHSVal.isLValue() && LHSVal.isInt()) { |
13209 | Result = RHSVal; |
13210 | addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false); |
13211 | return true; |
13212 | } |
13213 | |
13214 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
13215 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
13216 | if (!LHSVal.getLValueOffset().isZero() || |
13217 | !RHSVal.getLValueOffset().isZero()) |
13218 | return false; |
13219 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
13220 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
13221 | if (!LHSExpr || !RHSExpr) |
13222 | return false; |
13223 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
13224 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
13225 | if (!LHSAddrExpr || !RHSAddrExpr) |
13226 | return false; |
13227 | // Make sure both labels come from the same function. |
13228 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
13229 | RHSAddrExpr->getLabel()->getDeclContext()) |
13230 | return false; |
13231 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
13232 | return true; |
13233 | } |
13234 | |
13235 | // All the remaining cases expect both operands to be an integer |
13236 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
13237 | return Error(E); |
13238 | |
13239 | // Set up the width and signedness manually, in case it can't be deduced |
13240 | // from the operation we're performing. |
13241 | // FIXME: Don't do this in the cases where we can deduce it. |
13242 | APSInt Value(Info.Ctx.getIntWidth(T: E->getType()), |
13243 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
13244 | if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(), |
13245 | RHS: RHSVal.getInt(), Result&: Value)) |
13246 | return false; |
13247 | return Success(Value, E, Result); |
13248 | } |
13249 | |
13250 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
13251 | Job &job = Queue.back(); |
13252 | |
13253 | switch (job.Kind) { |
13254 | case Job::AnyExprKind: { |
13255 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) { |
13256 | if (shouldEnqueue(E: Bop)) { |
13257 | job.Kind = Job::BinOpKind; |
13258 | enqueue(E: Bop->getLHS()); |
13259 | return; |
13260 | } |
13261 | } |
13262 | |
13263 | EvaluateExpr(E: job.E, Result); |
13264 | Queue.pop_back(); |
13265 | return; |
13266 | } |
13267 | |
13268 | case Job::BinOpKind: { |
13269 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
13270 | bool SuppressRHSDiags = false; |
13271 | if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) { |
13272 | Queue.pop_back(); |
13273 | return; |
13274 | } |
13275 | if (SuppressRHSDiags) |
13276 | job.startSpeculativeEval(Info); |
13277 | job.LHSResult.swap(RHS&: Result); |
13278 | job.Kind = Job::BinOpVisitedLHSKind; |
13279 | enqueue(E: Bop->getRHS()); |
13280 | return; |
13281 | } |
13282 | |
13283 | case Job::BinOpVisitedLHSKind: { |
13284 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
13285 | EvalResult RHS; |
13286 | RHS.swap(RHS&: Result); |
13287 | Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val); |
13288 | Queue.pop_back(); |
13289 | return; |
13290 | } |
13291 | } |
13292 | |
13293 | llvm_unreachable("Invalid Job::Kind!" ); |
13294 | } |
13295 | |
13296 | namespace { |
13297 | enum class CmpResult { |
13298 | Unequal, |
13299 | Less, |
13300 | Equal, |
13301 | Greater, |
13302 | Unordered, |
13303 | }; |
13304 | } |
13305 | |
13306 | template <class SuccessCB, class AfterCB> |
13307 | static bool |
13308 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
13309 | SuccessCB &&Success, AfterCB &&DoAfter) { |
13310 | assert(!E->isValueDependent()); |
13311 | assert(E->isComparisonOp() && "expected comparison operator" ); |
13312 | assert((E->getOpcode() == BO_Cmp || |
13313 | E->getType()->isIntegralOrEnumerationType()) && |
13314 | "unsupported binary expression evaluation" ); |
13315 | auto Error = [&](const Expr *E) { |
13316 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
13317 | return false; |
13318 | }; |
13319 | |
13320 | bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp; |
13321 | bool IsEquality = E->isEqualityOp(); |
13322 | |
13323 | QualType LHSTy = E->getLHS()->getType(); |
13324 | QualType RHSTy = E->getRHS()->getType(); |
13325 | |
13326 | if (LHSTy->isIntegralOrEnumerationType() && |
13327 | RHSTy->isIntegralOrEnumerationType()) { |
13328 | APSInt LHS, RHS; |
13329 | bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info); |
13330 | if (!LHSOK && !Info.noteFailure()) |
13331 | return false; |
13332 | if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
13333 | return false; |
13334 | if (LHS < RHS) |
13335 | return Success(CmpResult::Less, E); |
13336 | if (LHS > RHS) |
13337 | return Success(CmpResult::Greater, E); |
13338 | return Success(CmpResult::Equal, E); |
13339 | } |
13340 | |
13341 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
13342 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy)); |
13343 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy)); |
13344 | |
13345 | bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info); |
13346 | if (!LHSOK && !Info.noteFailure()) |
13347 | return false; |
13348 | if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK) |
13349 | return false; |
13350 | if (LHSFX < RHSFX) |
13351 | return Success(CmpResult::Less, E); |
13352 | if (LHSFX > RHSFX) |
13353 | return Success(CmpResult::Greater, E); |
13354 | return Success(CmpResult::Equal, E); |
13355 | } |
13356 | |
13357 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
13358 | ComplexValue LHS, RHS; |
13359 | bool LHSOK; |
13360 | if (E->isAssignmentOp()) { |
13361 | LValue LV; |
13362 | EvaluateLValue(E: E->getLHS(), Result&: LV, Info); |
13363 | LHSOK = false; |
13364 | } else if (LHSTy->isRealFloatingType()) { |
13365 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info); |
13366 | if (LHSOK) { |
13367 | LHS.makeComplexFloat(); |
13368 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
13369 | } |
13370 | } else { |
13371 | LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info); |
13372 | } |
13373 | if (!LHSOK && !Info.noteFailure()) |
13374 | return false; |
13375 | |
13376 | if (E->getRHS()->getType()->isRealFloatingType()) { |
13377 | if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK) |
13378 | return false; |
13379 | RHS.makeComplexFloat(); |
13380 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
13381 | } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK) |
13382 | return false; |
13383 | |
13384 | if (LHS.isComplexFloat()) { |
13385 | APFloat::cmpResult CR_r = |
13386 | LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal()); |
13387 | APFloat::cmpResult CR_i = |
13388 | LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag()); |
13389 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
13390 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
13391 | } else { |
13392 | assert(IsEquality && "invalid complex comparison" ); |
13393 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
13394 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
13395 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
13396 | } |
13397 | } |
13398 | |
13399 | if (LHSTy->isRealFloatingType() && |
13400 | RHSTy->isRealFloatingType()) { |
13401 | APFloat RHS(0.0), LHS(0.0); |
13402 | |
13403 | bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info); |
13404 | if (!LHSOK && !Info.noteFailure()) |
13405 | return false; |
13406 | |
13407 | if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK) |
13408 | return false; |
13409 | |
13410 | assert(E->isComparisonOp() && "Invalid binary operator!" ); |
13411 | llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS); |
13412 | if (!Info.InConstantContext && |
13413 | APFloatCmpResult == APFloat::cmpUnordered && |
13414 | E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) { |
13415 | // Note: Compares may raise invalid in some cases involving NaN or sNaN. |
13416 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
13417 | return false; |
13418 | } |
13419 | auto GetCmpRes = [&]() { |
13420 | switch (APFloatCmpResult) { |
13421 | case APFloat::cmpEqual: |
13422 | return CmpResult::Equal; |
13423 | case APFloat::cmpLessThan: |
13424 | return CmpResult::Less; |
13425 | case APFloat::cmpGreaterThan: |
13426 | return CmpResult::Greater; |
13427 | case APFloat::cmpUnordered: |
13428 | return CmpResult::Unordered; |
13429 | } |
13430 | llvm_unreachable("Unrecognised APFloat::cmpResult enum" ); |
13431 | }; |
13432 | return Success(GetCmpRes(), E); |
13433 | } |
13434 | |
13435 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
13436 | LValue LHSValue, RHSValue; |
13437 | |
13438 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
13439 | if (!LHSOK && !Info.noteFailure()) |
13440 | return false; |
13441 | |
13442 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13443 | return false; |
13444 | |
13445 | // Reject differing bases from the normal codepath; we special-case |
13446 | // comparisons to null. |
13447 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
13448 | auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) { |
13449 | std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType()); |
13450 | std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType()); |
13451 | Info.FFDiag(E, DiagID) |
13452 | << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS); |
13453 | return false; |
13454 | }; |
13455 | // Inequalities and subtractions between unrelated pointers have |
13456 | // unspecified or undefined behavior. |
13457 | if (!IsEquality) |
13458 | return DiagComparison( |
13459 | diag::note_constexpr_pointer_comparison_unspecified); |
13460 | // A constant address may compare equal to the address of a symbol. |
13461 | // The one exception is that address of an object cannot compare equal |
13462 | // to a null pointer constant. |
13463 | // TODO: Should we restrict this to actual null pointers, and exclude the |
13464 | // case of zero cast to pointer type? |
13465 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
13466 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
13467 | return DiagComparison(diag::note_constexpr_pointer_constant_comparison, |
13468 | !RHSValue.Base); |
13469 | // It's implementation-defined whether distinct literals will have |
13470 | // distinct addresses. In clang, the result of such a comparison is |
13471 | // unspecified, so it is not a constant expression. However, we do know |
13472 | // that the address of a literal will be non-null. |
13473 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
13474 | LHSValue.Base && RHSValue.Base) |
13475 | return DiagComparison(diag::note_constexpr_literal_comparison); |
13476 | // We can't tell whether weak symbols will end up pointing to the same |
13477 | // object. |
13478 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
13479 | return DiagComparison(diag::note_constexpr_pointer_weak_comparison, |
13480 | !IsWeakLValue(LHSValue)); |
13481 | // We can't compare the address of the start of one object with the |
13482 | // past-the-end address of another object, per C++ DR1652. |
13483 | if (LHSValue.Base && LHSValue.Offset.isZero() && |
13484 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) |
13485 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
13486 | true); |
13487 | if (RHSValue.Base && RHSValue.Offset.isZero() && |
13488 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue)) |
13489 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
13490 | false); |
13491 | // We can't tell whether an object is at the same address as another |
13492 | // zero sized object. |
13493 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
13494 | (LHSValue.Base && isZeroSized(RHSValue))) |
13495 | return DiagComparison( |
13496 | diag::note_constexpr_pointer_comparison_zero_sized); |
13497 | return Success(CmpResult::Unequal, E); |
13498 | } |
13499 | |
13500 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
13501 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
13502 | |
13503 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
13504 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
13505 | |
13506 | // C++11 [expr.rel]p3: |
13507 | // Pointers to void (after pointer conversions) can be compared, with a |
13508 | // result defined as follows: If both pointers represent the same |
13509 | // address or are both the null pointer value, the result is true if the |
13510 | // operator is <= or >= and false otherwise; otherwise the result is |
13511 | // unspecified. |
13512 | // We interpret this as applying to pointers to *cv* void. |
13513 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
13514 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
13515 | |
13516 | // C++11 [expr.rel]p2: |
13517 | // - If two pointers point to non-static data members of the same object, |
13518 | // or to subobjects or array elements fo such members, recursively, the |
13519 | // pointer to the later declared member compares greater provided the |
13520 | // two members have the same access control and provided their class is |
13521 | // not a union. |
13522 | // [...] |
13523 | // - Otherwise pointer comparisons are unspecified. |
13524 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
13525 | bool WasArrayIndex; |
13526 | unsigned Mismatch = FindDesignatorMismatch( |
13527 | ObjType: getType(B: LHSValue.Base), A: LHSDesignator, B: RHSDesignator, WasArrayIndex); |
13528 | // At the point where the designators diverge, the comparison has a |
13529 | // specified value if: |
13530 | // - we are comparing array indices |
13531 | // - we are comparing fields of a union, or fields with the same access |
13532 | // Otherwise, the result is unspecified and thus the comparison is not a |
13533 | // constant expression. |
13534 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
13535 | Mismatch < RHSDesignator.Entries.size()) { |
13536 | const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]); |
13537 | const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]); |
13538 | if (!LF && !RF) |
13539 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
13540 | else if (!LF) |
13541 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
13542 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
13543 | << RF->getParent() << RF; |
13544 | else if (!RF) |
13545 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
13546 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
13547 | << LF->getParent() << LF; |
13548 | else if (!LF->getParent()->isUnion() && |
13549 | LF->getAccess() != RF->getAccess()) |
13550 | Info.CCEDiag(E, |
13551 | diag::note_constexpr_pointer_comparison_differing_access) |
13552 | << LF << LF->getAccess() << RF << RF->getAccess() |
13553 | << LF->getParent(); |
13554 | } |
13555 | } |
13556 | |
13557 | // The comparison here must be unsigned, and performed with the same |
13558 | // width as the pointer. |
13559 | unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy); |
13560 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
13561 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
13562 | assert(PtrSize <= 64 && "Unexpected pointer width" ); |
13563 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
13564 | CompareLHS &= Mask; |
13565 | CompareRHS &= Mask; |
13566 | |
13567 | // If there is a base and this is a relational operator, we can only |
13568 | // compare pointers within the object in question; otherwise, the result |
13569 | // depends on where the object is located in memory. |
13570 | if (!LHSValue.Base.isNull() && IsRelational) { |
13571 | QualType BaseTy = getType(B: LHSValue.Base); |
13572 | if (BaseTy->isIncompleteType()) |
13573 | return Error(E); |
13574 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy); |
13575 | uint64_t OffsetLimit = Size.getQuantity(); |
13576 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
13577 | return Error(E); |
13578 | } |
13579 | |
13580 | if (CompareLHS < CompareRHS) |
13581 | return Success(CmpResult::Less, E); |
13582 | if (CompareLHS > CompareRHS) |
13583 | return Success(CmpResult::Greater, E); |
13584 | return Success(CmpResult::Equal, E); |
13585 | } |
13586 | |
13587 | if (LHSTy->isMemberPointerType()) { |
13588 | assert(IsEquality && "unexpected member pointer operation" ); |
13589 | assert(RHSTy->isMemberPointerType() && "invalid comparison" ); |
13590 | |
13591 | MemberPtr LHSValue, RHSValue; |
13592 | |
13593 | bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info); |
13594 | if (!LHSOK && !Info.noteFailure()) |
13595 | return false; |
13596 | |
13597 | if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13598 | return false; |
13599 | |
13600 | // If either operand is a pointer to a weak function, the comparison is not |
13601 | // constant. |
13602 | if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) { |
13603 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
13604 | << LHSValue.getDecl(); |
13605 | return false; |
13606 | } |
13607 | if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) { |
13608 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
13609 | << RHSValue.getDecl(); |
13610 | return false; |
13611 | } |
13612 | |
13613 | // C++11 [expr.eq]p2: |
13614 | // If both operands are null, they compare equal. Otherwise if only one is |
13615 | // null, they compare unequal. |
13616 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
13617 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
13618 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
13619 | } |
13620 | |
13621 | // Otherwise if either is a pointer to a virtual member function, the |
13622 | // result is unspecified. |
13623 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
13624 | if (MD->isVirtual()) |
13625 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
13626 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
13627 | if (MD->isVirtual()) |
13628 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
13629 | |
13630 | // Otherwise they compare equal if and only if they would refer to the |
13631 | // same member of the same most derived object or the same subobject if |
13632 | // they were dereferenced with a hypothetical object of the associated |
13633 | // class type. |
13634 | bool Equal = LHSValue == RHSValue; |
13635 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
13636 | } |
13637 | |
13638 | if (LHSTy->isNullPtrType()) { |
13639 | assert(E->isComparisonOp() && "unexpected nullptr operation" ); |
13640 | assert(RHSTy->isNullPtrType() && "missing pointer conversion" ); |
13641 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
13642 | // are compared, the result is true of the operator is <=, >= or ==, and |
13643 | // false otherwise. |
13644 | LValue Res; |
13645 | if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) || |
13646 | !EvaluatePointer(E: E->getRHS(), Result&: Res, Info)) |
13647 | return false; |
13648 | return Success(CmpResult::Equal, E); |
13649 | } |
13650 | |
13651 | return DoAfter(); |
13652 | } |
13653 | |
13654 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
13655 | if (!CheckLiteralType(Info, E)) |
13656 | return false; |
13657 | |
13658 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
13659 | ComparisonCategoryResult CCR; |
13660 | switch (CR) { |
13661 | case CmpResult::Unequal: |
13662 | llvm_unreachable("should never produce Unequal for three-way comparison" ); |
13663 | case CmpResult::Less: |
13664 | CCR = ComparisonCategoryResult::Less; |
13665 | break; |
13666 | case CmpResult::Equal: |
13667 | CCR = ComparisonCategoryResult::Equal; |
13668 | break; |
13669 | case CmpResult::Greater: |
13670 | CCR = ComparisonCategoryResult::Greater; |
13671 | break; |
13672 | case CmpResult::Unordered: |
13673 | CCR = ComparisonCategoryResult::Unordered; |
13674 | break; |
13675 | } |
13676 | // Evaluation succeeded. Lookup the information for the comparison category |
13677 | // type and fetch the VarDecl for the result. |
13678 | const ComparisonCategoryInfo &CmpInfo = |
13679 | Info.Ctx.CompCategories.getInfoForType(Ty: E->getType()); |
13680 | const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD; |
13681 | // Check and evaluate the result as a constant expression. |
13682 | LValue LV; |
13683 | LV.set(VD); |
13684 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
13685 | return false; |
13686 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
13687 | ConstantExprKind::Normal); |
13688 | }; |
13689 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
13690 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
13691 | }); |
13692 | } |
13693 | |
13694 | bool RecordExprEvaluator::VisitCXXParenListInitExpr( |
13695 | const CXXParenListInitExpr *E) { |
13696 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs()); |
13697 | } |
13698 | |
13699 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13700 | // We don't support assignment in C. C++ assignments don't get here because |
13701 | // assignment is an lvalue in C++. |
13702 | if (E->isAssignmentOp()) { |
13703 | Error(E); |
13704 | if (!Info.noteFailure()) |
13705 | return false; |
13706 | } |
13707 | |
13708 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
13709 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
13710 | |
13711 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
13712 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
13713 | "DataRecursiveIntBinOpEvaluator should have handled integral types" ); |
13714 | |
13715 | if (E->isComparisonOp()) { |
13716 | // Evaluate builtin binary comparisons by evaluating them as three-way |
13717 | // comparisons and then translating the result. |
13718 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
13719 | assert((CR != CmpResult::Unequal || E->isEqualityOp()) && |
13720 | "should only produce Unequal for equality comparisons" ); |
13721 | bool IsEqual = CR == CmpResult::Equal, |
13722 | IsLess = CR == CmpResult::Less, |
13723 | IsGreater = CR == CmpResult::Greater; |
13724 | auto Op = E->getOpcode(); |
13725 | switch (Op) { |
13726 | default: |
13727 | llvm_unreachable("unsupported binary operator" ); |
13728 | case BO_EQ: |
13729 | case BO_NE: |
13730 | return Success(IsEqual == (Op == BO_EQ), E); |
13731 | case BO_LT: |
13732 | return Success(IsLess, E); |
13733 | case BO_GT: |
13734 | return Success(IsGreater, E); |
13735 | case BO_LE: |
13736 | return Success(IsEqual || IsLess, E); |
13737 | case BO_GE: |
13738 | return Success(IsEqual || IsGreater, E); |
13739 | } |
13740 | }; |
13741 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
13742 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13743 | }); |
13744 | } |
13745 | |
13746 | QualType LHSTy = E->getLHS()->getType(); |
13747 | QualType RHSTy = E->getRHS()->getType(); |
13748 | |
13749 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
13750 | E->getOpcode() == BO_Sub) { |
13751 | LValue LHSValue, RHSValue; |
13752 | |
13753 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
13754 | if (!LHSOK && !Info.noteFailure()) |
13755 | return false; |
13756 | |
13757 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13758 | return false; |
13759 | |
13760 | // Reject differing bases from the normal codepath; we special-case |
13761 | // comparisons to null. |
13762 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
13763 | // Handle &&A - &&B. |
13764 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
13765 | return Error(E); |
13766 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
13767 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
13768 | if (!LHSExpr || !RHSExpr) |
13769 | return Error(E); |
13770 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
13771 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
13772 | if (!LHSAddrExpr || !RHSAddrExpr) |
13773 | return Error(E); |
13774 | // Make sure both labels come from the same function. |
13775 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
13776 | RHSAddrExpr->getLabel()->getDeclContext()) |
13777 | return Error(E); |
13778 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
13779 | } |
13780 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
13781 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
13782 | |
13783 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
13784 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
13785 | |
13786 | // C++11 [expr.add]p6: |
13787 | // Unless both pointers point to elements of the same array object, or |
13788 | // one past the last element of the array object, the behavior is |
13789 | // undefined. |
13790 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
13791 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
13792 | RHSDesignator)) |
13793 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
13794 | |
13795 | QualType Type = E->getLHS()->getType(); |
13796 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
13797 | |
13798 | CharUnits ElementSize; |
13799 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize)) |
13800 | return false; |
13801 | |
13802 | // As an extension, a type may have zero size (empty struct or union in |
13803 | // C, array of zero length). Pointer subtraction in such cases has |
13804 | // undefined behavior, so is not constant. |
13805 | if (ElementSize.isZero()) { |
13806 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
13807 | << ElementType; |
13808 | return false; |
13809 | } |
13810 | |
13811 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
13812 | // and produce incorrect results when it overflows. Such behavior |
13813 | // appears to be non-conforming, but is common, so perhaps we should |
13814 | // assume the standard intended for such cases to be undefined behavior |
13815 | // and check for them. |
13816 | |
13817 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
13818 | // overflow in the final conversion to ptrdiff_t. |
13819 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
13820 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
13821 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
13822 | false); |
13823 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
13824 | APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
13825 | |
13826 | if (Result.extend(width: 65) != TrueResult && |
13827 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
13828 | return false; |
13829 | return Success(Result, E); |
13830 | } |
13831 | |
13832 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13833 | } |
13834 | |
13835 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
13836 | /// a result as the expression's type. |
13837 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
13838 | const UnaryExprOrTypeTraitExpr *E) { |
13839 | switch(E->getKind()) { |
13840 | case UETT_PreferredAlignOf: |
13841 | case UETT_AlignOf: { |
13842 | if (E->isArgumentType()) |
13843 | return Success(GetAlignOfType(Info, T: E->getArgumentType(), ExprKind: E->getKind()), |
13844 | E); |
13845 | else |
13846 | return Success(GetAlignOfExpr(Info, E: E->getArgumentExpr(), ExprKind: E->getKind()), |
13847 | E); |
13848 | } |
13849 | |
13850 | case UETT_VecStep: { |
13851 | QualType Ty = E->getTypeOfArgument(); |
13852 | |
13853 | if (Ty->isVectorType()) { |
13854 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
13855 | |
13856 | // The vec_step built-in functions that take a 3-component |
13857 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
13858 | if (n == 3) |
13859 | n = 4; |
13860 | |
13861 | return Success(n, E); |
13862 | } else |
13863 | return Success(1, E); |
13864 | } |
13865 | |
13866 | case UETT_DataSizeOf: |
13867 | case UETT_SizeOf: { |
13868 | QualType SrcTy = E->getTypeOfArgument(); |
13869 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
13870 | // the result is the size of the referenced type." |
13871 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
13872 | SrcTy = Ref->getPointeeType(); |
13873 | |
13874 | CharUnits Sizeof; |
13875 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof, |
13876 | E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf |
13877 | : SizeOfType::SizeOf)) { |
13878 | return false; |
13879 | } |
13880 | return Success(Sizeof, E); |
13881 | } |
13882 | case UETT_OpenMPRequiredSimdAlign: |
13883 | assert(E->isArgumentType()); |
13884 | return Success( |
13885 | Info.Ctx.toCharUnitsFromBits( |
13886 | BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType())) |
13887 | .getQuantity(), |
13888 | E); |
13889 | case UETT_VectorElements: { |
13890 | QualType Ty = E->getTypeOfArgument(); |
13891 | // If the vector has a fixed size, we can determine the number of elements |
13892 | // at compile time. |
13893 | if (Ty->isVectorType()) |
13894 | return Success(Ty->castAs<VectorType>()->getNumElements(), E); |
13895 | |
13896 | assert(Ty->isSizelessVectorType()); |
13897 | if (Info.InConstantContext) |
13898 | Info.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) |
13899 | << E->getSourceRange(); |
13900 | |
13901 | return false; |
13902 | } |
13903 | } |
13904 | |
13905 | llvm_unreachable("unknown expr/type trait" ); |
13906 | } |
13907 | |
13908 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
13909 | CharUnits Result; |
13910 | unsigned n = OOE->getNumComponents(); |
13911 | if (n == 0) |
13912 | return Error(OOE); |
13913 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
13914 | for (unsigned i = 0; i != n; ++i) { |
13915 | OffsetOfNode ON = OOE->getComponent(Idx: i); |
13916 | switch (ON.getKind()) { |
13917 | case OffsetOfNode::Array: { |
13918 | const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex()); |
13919 | APSInt IdxResult; |
13920 | if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info)) |
13921 | return false; |
13922 | const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType); |
13923 | if (!AT) |
13924 | return Error(OOE); |
13925 | CurrentType = AT->getElementType(); |
13926 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType); |
13927 | Result += IdxResult.getSExtValue() * ElementSize; |
13928 | break; |
13929 | } |
13930 | |
13931 | case OffsetOfNode::Field: { |
13932 | FieldDecl *MemberDecl = ON.getField(); |
13933 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13934 | if (!RT) |
13935 | return Error(OOE); |
13936 | RecordDecl *RD = RT->getDecl(); |
13937 | if (RD->isInvalidDecl()) return false; |
13938 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
13939 | unsigned i = MemberDecl->getFieldIndex(); |
13940 | assert(i < RL.getFieldCount() && "offsetof field in wrong type" ); |
13941 | Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i)); |
13942 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
13943 | break; |
13944 | } |
13945 | |
13946 | case OffsetOfNode::Identifier: |
13947 | llvm_unreachable("dependent __builtin_offsetof" ); |
13948 | |
13949 | case OffsetOfNode::Base: { |
13950 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
13951 | if (BaseSpec->isVirtual()) |
13952 | return Error(OOE); |
13953 | |
13954 | // Find the layout of the class whose base we are looking into. |
13955 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13956 | if (!RT) |
13957 | return Error(OOE); |
13958 | RecordDecl *RD = RT->getDecl(); |
13959 | if (RD->isInvalidDecl()) return false; |
13960 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
13961 | |
13962 | // Find the base class itself. |
13963 | CurrentType = BaseSpec->getType(); |
13964 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
13965 | if (!BaseRT) |
13966 | return Error(OOE); |
13967 | |
13968 | // Add the offset to the base. |
13969 | Result += RL.getBaseClassOffset(Base: cast<CXXRecordDecl>(Val: BaseRT->getDecl())); |
13970 | break; |
13971 | } |
13972 | } |
13973 | } |
13974 | return Success(Result, OOE); |
13975 | } |
13976 | |
13977 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13978 | switch (E->getOpcode()) { |
13979 | default: |
13980 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
13981 | // See C99 6.6p3. |
13982 | return Error(E); |
13983 | case UO_Extension: |
13984 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
13985 | // If so, we could clear the diagnostic ID. |
13986 | return Visit(E->getSubExpr()); |
13987 | case UO_Plus: |
13988 | // The result is just the value. |
13989 | return Visit(E->getSubExpr()); |
13990 | case UO_Minus: { |
13991 | if (!Visit(E->getSubExpr())) |
13992 | return false; |
13993 | if (!Result.isInt()) return Error(E); |
13994 | const APSInt &Value = Result.getInt(); |
13995 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) { |
13996 | if (Info.checkingForUndefinedBehavior()) |
13997 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13998 | diag::warn_integer_constant_overflow) |
13999 | << toString(Value, 10, Value.isSigned(), /*formatAsCLiteral=*/false, |
14000 | /*UpperCase=*/true, /*InsertSeparators=*/true) |
14001 | << E->getType() << E->getSourceRange(); |
14002 | |
14003 | if (!HandleOverflow(Info, E, -Value.extend(width: Value.getBitWidth() + 1), |
14004 | E->getType())) |
14005 | return false; |
14006 | } |
14007 | return Success(-Value, E); |
14008 | } |
14009 | case UO_Not: { |
14010 | if (!Visit(E->getSubExpr())) |
14011 | return false; |
14012 | if (!Result.isInt()) return Error(E); |
14013 | return Success(~Result.getInt(), E); |
14014 | } |
14015 | case UO_LNot: { |
14016 | bool bres; |
14017 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
14018 | return false; |
14019 | return Success(!bres, E); |
14020 | } |
14021 | } |
14022 | } |
14023 | |
14024 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
14025 | /// result type is integer. |
14026 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14027 | const Expr *SubExpr = E->getSubExpr(); |
14028 | QualType DestType = E->getType(); |
14029 | QualType SrcType = SubExpr->getType(); |
14030 | |
14031 | switch (E->getCastKind()) { |
14032 | case CK_BaseToDerived: |
14033 | case CK_DerivedToBase: |
14034 | case CK_UncheckedDerivedToBase: |
14035 | case CK_Dynamic: |
14036 | case CK_ToUnion: |
14037 | case CK_ArrayToPointerDecay: |
14038 | case CK_FunctionToPointerDecay: |
14039 | case CK_NullToPointer: |
14040 | case CK_NullToMemberPointer: |
14041 | case CK_BaseToDerivedMemberPointer: |
14042 | case CK_DerivedToBaseMemberPointer: |
14043 | case CK_ReinterpretMemberPointer: |
14044 | case CK_ConstructorConversion: |
14045 | case CK_IntegralToPointer: |
14046 | case CK_ToVoid: |
14047 | case CK_VectorSplat: |
14048 | case CK_IntegralToFloating: |
14049 | case CK_FloatingCast: |
14050 | case CK_CPointerToObjCPointerCast: |
14051 | case CK_BlockPointerToObjCPointerCast: |
14052 | case CK_AnyPointerToBlockPointerCast: |
14053 | case CK_ObjCObjectLValueCast: |
14054 | case CK_FloatingRealToComplex: |
14055 | case CK_FloatingComplexToReal: |
14056 | case CK_FloatingComplexCast: |
14057 | case CK_FloatingComplexToIntegralComplex: |
14058 | case CK_IntegralRealToComplex: |
14059 | case CK_IntegralComplexCast: |
14060 | case CK_IntegralComplexToFloatingComplex: |
14061 | case CK_BuiltinFnToFnPtr: |
14062 | case CK_ZeroToOCLOpaqueType: |
14063 | case CK_NonAtomicToAtomic: |
14064 | case CK_AddressSpaceConversion: |
14065 | case CK_IntToOCLSampler: |
14066 | case CK_FloatingToFixedPoint: |
14067 | case CK_FixedPointToFloating: |
14068 | case CK_FixedPointCast: |
14069 | case CK_IntegralToFixedPoint: |
14070 | case CK_MatrixCast: |
14071 | case CK_HLSLVectorTruncation: |
14072 | llvm_unreachable("invalid cast kind for integral value" ); |
14073 | |
14074 | case CK_BitCast: |
14075 | case CK_Dependent: |
14076 | case CK_LValueBitCast: |
14077 | case CK_ARCProduceObject: |
14078 | case CK_ARCConsumeObject: |
14079 | case CK_ARCReclaimReturnedObject: |
14080 | case CK_ARCExtendBlockObject: |
14081 | case CK_CopyAndAutoreleaseBlockObject: |
14082 | return Error(E); |
14083 | |
14084 | case CK_UserDefinedConversion: |
14085 | case CK_LValueToRValue: |
14086 | case CK_AtomicToNonAtomic: |
14087 | case CK_NoOp: |
14088 | case CK_LValueToRValueBitCast: |
14089 | case CK_HLSLArrayRValue: |
14090 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14091 | |
14092 | case CK_MemberPointerToBoolean: |
14093 | case CK_PointerToBoolean: |
14094 | case CK_IntegralToBoolean: |
14095 | case CK_FloatingToBoolean: |
14096 | case CK_BooleanToSignedIntegral: |
14097 | case CK_FloatingComplexToBoolean: |
14098 | case CK_IntegralComplexToBoolean: { |
14099 | bool BoolResult; |
14100 | if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info)) |
14101 | return false; |
14102 | uint64_t IntResult = BoolResult; |
14103 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
14104 | IntResult = (uint64_t)-1; |
14105 | return Success(IntResult, E); |
14106 | } |
14107 | |
14108 | case CK_FixedPointToIntegral: { |
14109 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType)); |
14110 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
14111 | return false; |
14112 | bool Overflowed; |
14113 | llvm::APSInt Result = Src.convertToInt( |
14114 | DstWidth: Info.Ctx.getIntWidth(T: DestType), |
14115 | DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed); |
14116 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
14117 | return false; |
14118 | return Success(Result, E); |
14119 | } |
14120 | |
14121 | case CK_FixedPointToBoolean: { |
14122 | // Unsigned padding does not affect this. |
14123 | APValue Val; |
14124 | if (!Evaluate(Result&: Val, Info, E: SubExpr)) |
14125 | return false; |
14126 | return Success(Val.getFixedPoint().getBoolValue(), E); |
14127 | } |
14128 | |
14129 | case CK_IntegralCast: { |
14130 | if (!Visit(SubExpr)) |
14131 | return false; |
14132 | |
14133 | if (!Result.isInt()) { |
14134 | // Allow casts of address-of-label differences if they are no-ops |
14135 | // or narrowing. (The narrowing case isn't actually guaranteed to |
14136 | // be constant-evaluatable except in some narrow cases which are hard |
14137 | // to detect here. We let it through on the assumption the user knows |
14138 | // what they are doing.) |
14139 | if (Result.isAddrLabelDiff()) |
14140 | return Info.Ctx.getTypeSize(T: DestType) <= Info.Ctx.getTypeSize(T: SrcType); |
14141 | // Only allow casts of lvalues if they are lossless. |
14142 | return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType); |
14143 | } |
14144 | |
14145 | if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext && |
14146 | Info.EvalMode == EvalInfo::EM_ConstantExpression && |
14147 | DestType->isEnumeralType()) { |
14148 | |
14149 | bool ConstexprVar = true; |
14150 | |
14151 | // We know if we are here that we are in a context that we might require |
14152 | // a constant expression or a context that requires a constant |
14153 | // value. But if we are initializing a value we don't know if it is a |
14154 | // constexpr variable or not. We can check the EvaluatingDecl to determine |
14155 | // if it constexpr or not. If not then we don't want to emit a diagnostic. |
14156 | if (const auto *VD = dyn_cast_or_null<VarDecl>( |
14157 | Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>())) |
14158 | ConstexprVar = VD->isConstexpr(); |
14159 | |
14160 | const EnumType *ET = dyn_cast<EnumType>(Val: DestType.getCanonicalType()); |
14161 | const EnumDecl *ED = ET->getDecl(); |
14162 | // Check that the value is within the range of the enumeration values. |
14163 | // |
14164 | // This corressponds to [expr.static.cast]p10 which says: |
14165 | // A value of integral or enumeration type can be explicitly converted |
14166 | // to a complete enumeration type ... If the enumeration type does not |
14167 | // have a fixed underlying type, the value is unchanged if the original |
14168 | // value is within the range of the enumeration values ([dcl.enum]), and |
14169 | // otherwise, the behavior is undefined. |
14170 | // |
14171 | // This was resolved as part of DR2338 which has CD5 status. |
14172 | if (!ED->isFixed()) { |
14173 | llvm::APInt Min; |
14174 | llvm::APInt Max; |
14175 | |
14176 | ED->getValueRange(Max, Min); |
14177 | --Max; |
14178 | |
14179 | if (ED->getNumNegativeBits() && ConstexprVar && |
14180 | (Max.slt(Result.getInt().getSExtValue()) || |
14181 | Min.sgt(Result.getInt().getSExtValue()))) |
14182 | Info.Ctx.getDiagnostics().Report( |
14183 | E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range) |
14184 | << llvm::toString(Result.getInt(), 10) << Min.getSExtValue() |
14185 | << Max.getSExtValue() << ED; |
14186 | else if (!ED->getNumNegativeBits() && ConstexprVar && |
14187 | Max.ult(Result.getInt().getZExtValue())) |
14188 | Info.Ctx.getDiagnostics().Report( |
14189 | E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range) |
14190 | << llvm::toString(Result.getInt(), 10) << Min.getZExtValue() |
14191 | << Max.getZExtValue() << ED; |
14192 | } |
14193 | } |
14194 | |
14195 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
14196 | Result.getInt()), E); |
14197 | } |
14198 | |
14199 | case CK_PointerToIntegral: { |
14200 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
14201 | << 2 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange(); |
14202 | |
14203 | LValue LV; |
14204 | if (!EvaluatePointer(E: SubExpr, Result&: LV, Info)) |
14205 | return false; |
14206 | |
14207 | if (LV.getLValueBase()) { |
14208 | // Only allow based lvalue casts if they are lossless. |
14209 | // FIXME: Allow a larger integer size than the pointer size, and allow |
14210 | // narrowing back down to pointer width in subsequent integral casts. |
14211 | // FIXME: Check integer type's active bits, not its type size. |
14212 | if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType)) |
14213 | return Error(E); |
14214 | |
14215 | LV.Designator.setInvalid(); |
14216 | LV.moveInto(V&: Result); |
14217 | return true; |
14218 | } |
14219 | |
14220 | APSInt AsInt; |
14221 | APValue V; |
14222 | LV.moveInto(V); |
14223 | if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx)) |
14224 | llvm_unreachable("Can't cast this!" ); |
14225 | |
14226 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
14227 | } |
14228 | |
14229 | case CK_IntegralComplexToReal: { |
14230 | ComplexValue C; |
14231 | if (!EvaluateComplex(E: SubExpr, Res&: C, Info)) |
14232 | return false; |
14233 | return Success(C.getComplexIntReal(), E); |
14234 | } |
14235 | |
14236 | case CK_FloatingToIntegral: { |
14237 | APFloat F(0.0); |
14238 | if (!EvaluateFloat(E: SubExpr, Result&: F, Info)) |
14239 | return false; |
14240 | |
14241 | APSInt Value; |
14242 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
14243 | return false; |
14244 | return Success(Value, E); |
14245 | } |
14246 | } |
14247 | |
14248 | llvm_unreachable("unknown cast resulting in integral value" ); |
14249 | } |
14250 | |
14251 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
14252 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14253 | ComplexValue LV; |
14254 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
14255 | return false; |
14256 | if (!LV.isComplexInt()) |
14257 | return Error(E); |
14258 | return Success(LV.getComplexIntReal(), E); |
14259 | } |
14260 | |
14261 | return Visit(E->getSubExpr()); |
14262 | } |
14263 | |
14264 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
14265 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
14266 | ComplexValue LV; |
14267 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
14268 | return false; |
14269 | if (!LV.isComplexInt()) |
14270 | return Error(E); |
14271 | return Success(LV.getComplexIntImag(), E); |
14272 | } |
14273 | |
14274 | VisitIgnoredValue(E: E->getSubExpr()); |
14275 | return Success(0, E); |
14276 | } |
14277 | |
14278 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
14279 | return Success(E->getPackLength(), E); |
14280 | } |
14281 | |
14282 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
14283 | return Success(E->getValue(), E); |
14284 | } |
14285 | |
14286 | bool IntExprEvaluator::VisitConceptSpecializationExpr( |
14287 | const ConceptSpecializationExpr *E) { |
14288 | return Success(E->isSatisfied(), E); |
14289 | } |
14290 | |
14291 | bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) { |
14292 | return Success(E->isSatisfied(), E); |
14293 | } |
14294 | |
14295 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14296 | switch (E->getOpcode()) { |
14297 | default: |
14298 | // Invalid unary operators |
14299 | return Error(E); |
14300 | case UO_Plus: |
14301 | // The result is just the value. |
14302 | return Visit(E->getSubExpr()); |
14303 | case UO_Minus: { |
14304 | if (!Visit(E->getSubExpr())) return false; |
14305 | if (!Result.isFixedPoint()) |
14306 | return Error(E); |
14307 | bool Overflowed; |
14308 | APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed); |
14309 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
14310 | return false; |
14311 | return Success(Negated, E); |
14312 | } |
14313 | case UO_LNot: { |
14314 | bool bres; |
14315 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
14316 | return false; |
14317 | return Success(!bres, E); |
14318 | } |
14319 | } |
14320 | } |
14321 | |
14322 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14323 | const Expr *SubExpr = E->getSubExpr(); |
14324 | QualType DestType = E->getType(); |
14325 | assert(DestType->isFixedPointType() && |
14326 | "Expected destination type to be a fixed point type" ); |
14327 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType); |
14328 | |
14329 | switch (E->getCastKind()) { |
14330 | case CK_FixedPointCast: { |
14331 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
14332 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
14333 | return false; |
14334 | bool Overflowed; |
14335 | APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed); |
14336 | if (Overflowed) { |
14337 | if (Info.checkingForUndefinedBehavior()) |
14338 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14339 | diag::warn_fixedpoint_constant_overflow) |
14340 | << Result.toString() << E->getType(); |
14341 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14342 | return false; |
14343 | } |
14344 | return Success(Result, E); |
14345 | } |
14346 | case CK_IntegralToFixedPoint: { |
14347 | APSInt Src; |
14348 | if (!EvaluateInteger(E: SubExpr, Result&: Src, Info)) |
14349 | return false; |
14350 | |
14351 | bool Overflowed; |
14352 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
14353 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
14354 | |
14355 | if (Overflowed) { |
14356 | if (Info.checkingForUndefinedBehavior()) |
14357 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14358 | diag::warn_fixedpoint_constant_overflow) |
14359 | << IntResult.toString() << E->getType(); |
14360 | if (!HandleOverflow(Info, E, IntResult, E->getType())) |
14361 | return false; |
14362 | } |
14363 | |
14364 | return Success(IntResult, E); |
14365 | } |
14366 | case CK_FloatingToFixedPoint: { |
14367 | APFloat Src(0.0); |
14368 | if (!EvaluateFloat(E: SubExpr, Result&: Src, Info)) |
14369 | return false; |
14370 | |
14371 | bool Overflowed; |
14372 | APFixedPoint Result = APFixedPoint::getFromFloatValue( |
14373 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
14374 | |
14375 | if (Overflowed) { |
14376 | if (Info.checkingForUndefinedBehavior()) |
14377 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14378 | diag::warn_fixedpoint_constant_overflow) |
14379 | << Result.toString() << E->getType(); |
14380 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14381 | return false; |
14382 | } |
14383 | |
14384 | return Success(Result, E); |
14385 | } |
14386 | case CK_NoOp: |
14387 | case CK_LValueToRValue: |
14388 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14389 | default: |
14390 | return Error(E); |
14391 | } |
14392 | } |
14393 | |
14394 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14395 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14396 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14397 | |
14398 | const Expr *LHS = E->getLHS(); |
14399 | const Expr *RHS = E->getRHS(); |
14400 | FixedPointSemantics ResultFXSema = |
14401 | Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
14402 | |
14403 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType())); |
14404 | if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info)) |
14405 | return false; |
14406 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType())); |
14407 | if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info)) |
14408 | return false; |
14409 | |
14410 | bool OpOverflow = false, ConversionOverflow = false; |
14411 | APFixedPoint Result(LHSFX.getSemantics()); |
14412 | switch (E->getOpcode()) { |
14413 | case BO_Add: { |
14414 | Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow) |
14415 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14416 | break; |
14417 | } |
14418 | case BO_Sub: { |
14419 | Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow) |
14420 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14421 | break; |
14422 | } |
14423 | case BO_Mul: { |
14424 | Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow) |
14425 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14426 | break; |
14427 | } |
14428 | case BO_Div: { |
14429 | if (RHSFX.getValue() == 0) { |
14430 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
14431 | return false; |
14432 | } |
14433 | Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow) |
14434 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14435 | break; |
14436 | } |
14437 | case BO_Shl: |
14438 | case BO_Shr: { |
14439 | FixedPointSemantics LHSSema = LHSFX.getSemantics(); |
14440 | llvm::APSInt RHSVal = RHSFX.getValue(); |
14441 | |
14442 | unsigned ShiftBW = |
14443 | LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding(); |
14444 | unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1); |
14445 | // Embedded-C 4.1.6.2.2: |
14446 | // The right operand must be nonnegative and less than the total number |
14447 | // of (nonpadding) bits of the fixed-point operand ... |
14448 | if (RHSVal.isNegative()) |
14449 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal; |
14450 | else if (Amt != RHSVal) |
14451 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
14452 | << RHSVal << E->getType() << ShiftBW; |
14453 | |
14454 | if (E->getOpcode() == BO_Shl) |
14455 | Result = LHSFX.shl(Amt, Overflow: &OpOverflow); |
14456 | else |
14457 | Result = LHSFX.shr(Amt, Overflow: &OpOverflow); |
14458 | break; |
14459 | } |
14460 | default: |
14461 | return false; |
14462 | } |
14463 | if (OpOverflow || ConversionOverflow) { |
14464 | if (Info.checkingForUndefinedBehavior()) |
14465 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14466 | diag::warn_fixedpoint_constant_overflow) |
14467 | << Result.toString() << E->getType(); |
14468 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14469 | return false; |
14470 | } |
14471 | return Success(Result, E); |
14472 | } |
14473 | |
14474 | //===----------------------------------------------------------------------===// |
14475 | // Float Evaluation |
14476 | //===----------------------------------------------------------------------===// |
14477 | |
14478 | namespace { |
14479 | class FloatExprEvaluator |
14480 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
14481 | APFloat &Result; |
14482 | public: |
14483 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
14484 | : ExprEvaluatorBaseTy(info), Result(result) {} |
14485 | |
14486 | bool Success(const APValue &V, const Expr *e) { |
14487 | Result = V.getFloat(); |
14488 | return true; |
14489 | } |
14490 | |
14491 | bool ZeroInitialization(const Expr *E) { |
14492 | Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
14493 | return true; |
14494 | } |
14495 | |
14496 | bool VisitCallExpr(const CallExpr *E); |
14497 | |
14498 | bool VisitUnaryOperator(const UnaryOperator *E); |
14499 | bool VisitBinaryOperator(const BinaryOperator *E); |
14500 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
14501 | bool VisitCastExpr(const CastExpr *E); |
14502 | |
14503 | bool VisitUnaryReal(const UnaryOperator *E); |
14504 | bool VisitUnaryImag(const UnaryOperator *E); |
14505 | |
14506 | // FIXME: Missing: array subscript of vector, member of vector |
14507 | }; |
14508 | } // end anonymous namespace |
14509 | |
14510 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
14511 | assert(!E->isValueDependent()); |
14512 | assert(E->isPRValue() && E->getType()->isRealFloatingType()); |
14513 | return FloatExprEvaluator(Info, Result).Visit(E); |
14514 | } |
14515 | |
14516 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
14517 | QualType ResultTy, |
14518 | const Expr *Arg, |
14519 | bool SNaN, |
14520 | llvm::APFloat &Result) { |
14521 | const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts()); |
14522 | if (!S) return false; |
14523 | |
14524 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy); |
14525 | |
14526 | llvm::APInt fill; |
14527 | |
14528 | // Treat empty strings as if they were zero. |
14529 | if (S->getString().empty()) |
14530 | fill = llvm::APInt(32, 0); |
14531 | else if (S->getString().getAsInteger(Radix: 0, Result&: fill)) |
14532 | return false; |
14533 | |
14534 | if (Context.getTargetInfo().isNan2008()) { |
14535 | if (SNaN) |
14536 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
14537 | else |
14538 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
14539 | } else { |
14540 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
14541 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
14542 | // a different encoding to what became a standard in 2008, and for pre- |
14543 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
14544 | // sNaN. This is now known as "legacy NaN" encoding. |
14545 | if (SNaN) |
14546 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
14547 | else |
14548 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
14549 | } |
14550 | |
14551 | return true; |
14552 | } |
14553 | |
14554 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
14555 | if (!IsConstantEvaluatedBuiltinCall(E)) |
14556 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14557 | |
14558 | switch (E->getBuiltinCallee()) { |
14559 | default: |
14560 | return false; |
14561 | |
14562 | case Builtin::BI__builtin_huge_val: |
14563 | case Builtin::BI__builtin_huge_valf: |
14564 | case Builtin::BI__builtin_huge_vall: |
14565 | case Builtin::BI__builtin_huge_valf16: |
14566 | case Builtin::BI__builtin_huge_valf128: |
14567 | case Builtin::BI__builtin_inf: |
14568 | case Builtin::BI__builtin_inff: |
14569 | case Builtin::BI__builtin_infl: |
14570 | case Builtin::BI__builtin_inff16: |
14571 | case Builtin::BI__builtin_inff128: { |
14572 | const llvm::fltSemantics &Sem = |
14573 | Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
14574 | Result = llvm::APFloat::getInf(Sem); |
14575 | return true; |
14576 | } |
14577 | |
14578 | case Builtin::BI__builtin_nans: |
14579 | case Builtin::BI__builtin_nansf: |
14580 | case Builtin::BI__builtin_nansl: |
14581 | case Builtin::BI__builtin_nansf16: |
14582 | case Builtin::BI__builtin_nansf128: |
14583 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
14584 | true, Result)) |
14585 | return Error(E); |
14586 | return true; |
14587 | |
14588 | case Builtin::BI__builtin_nan: |
14589 | case Builtin::BI__builtin_nanf: |
14590 | case Builtin::BI__builtin_nanl: |
14591 | case Builtin::BI__builtin_nanf16: |
14592 | case Builtin::BI__builtin_nanf128: |
14593 | // If this is __builtin_nan() turn this into a nan, otherwise we |
14594 | // can't constant fold it. |
14595 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
14596 | false, Result)) |
14597 | return Error(E); |
14598 | return true; |
14599 | |
14600 | case Builtin::BI__builtin_fabs: |
14601 | case Builtin::BI__builtin_fabsf: |
14602 | case Builtin::BI__builtin_fabsl: |
14603 | case Builtin::BI__builtin_fabsf128: |
14604 | // The C standard says "fabs raises no floating-point exceptions, |
14605 | // even if x is a signaling NaN. The returned value is independent of |
14606 | // the current rounding direction mode." Therefore constant folding can |
14607 | // proceed without regard to the floating point settings. |
14608 | // Reference, WG14 N2478 F.10.4.3 |
14609 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info)) |
14610 | return false; |
14611 | |
14612 | if (Result.isNegative()) |
14613 | Result.changeSign(); |
14614 | return true; |
14615 | |
14616 | case Builtin::BI__arithmetic_fence: |
14617 | return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info); |
14618 | |
14619 | // FIXME: Builtin::BI__builtin_powi |
14620 | // FIXME: Builtin::BI__builtin_powif |
14621 | // FIXME: Builtin::BI__builtin_powil |
14622 | |
14623 | case Builtin::BI__builtin_copysign: |
14624 | case Builtin::BI__builtin_copysignf: |
14625 | case Builtin::BI__builtin_copysignl: |
14626 | case Builtin::BI__builtin_copysignf128: { |
14627 | APFloat RHS(0.); |
14628 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14629 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14630 | return false; |
14631 | Result.copySign(RHS); |
14632 | return true; |
14633 | } |
14634 | |
14635 | case Builtin::BI__builtin_fmax: |
14636 | case Builtin::BI__builtin_fmaxf: |
14637 | case Builtin::BI__builtin_fmaxl: |
14638 | case Builtin::BI__builtin_fmaxf16: |
14639 | case Builtin::BI__builtin_fmaxf128: { |
14640 | // TODO: Handle sNaN. |
14641 | APFloat RHS(0.); |
14642 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14643 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14644 | return false; |
14645 | // When comparing zeroes, return +0.0 if one of the zeroes is positive. |
14646 | if (Result.isZero() && RHS.isZero() && Result.isNegative()) |
14647 | Result = RHS; |
14648 | else if (Result.isNaN() || RHS > Result) |
14649 | Result = RHS; |
14650 | return true; |
14651 | } |
14652 | |
14653 | case Builtin::BI__builtin_fmin: |
14654 | case Builtin::BI__builtin_fminf: |
14655 | case Builtin::BI__builtin_fminl: |
14656 | case Builtin::BI__builtin_fminf16: |
14657 | case Builtin::BI__builtin_fminf128: { |
14658 | // TODO: Handle sNaN. |
14659 | APFloat RHS(0.); |
14660 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14661 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14662 | return false; |
14663 | // When comparing zeroes, return -0.0 if one of the zeroes is negative. |
14664 | if (Result.isZero() && RHS.isZero() && RHS.isNegative()) |
14665 | Result = RHS; |
14666 | else if (Result.isNaN() || RHS < Result) |
14667 | Result = RHS; |
14668 | return true; |
14669 | } |
14670 | } |
14671 | } |
14672 | |
14673 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
14674 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14675 | ComplexValue CV; |
14676 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
14677 | return false; |
14678 | Result = CV.FloatReal; |
14679 | return true; |
14680 | } |
14681 | |
14682 | return Visit(E->getSubExpr()); |
14683 | } |
14684 | |
14685 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
14686 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14687 | ComplexValue CV; |
14688 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
14689 | return false; |
14690 | Result = CV.FloatImag; |
14691 | return true; |
14692 | } |
14693 | |
14694 | VisitIgnoredValue(E: E->getSubExpr()); |
14695 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
14696 | Result = llvm::APFloat::getZero(Sem); |
14697 | return true; |
14698 | } |
14699 | |
14700 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14701 | switch (E->getOpcode()) { |
14702 | default: return Error(E); |
14703 | case UO_Plus: |
14704 | return EvaluateFloat(E: E->getSubExpr(), Result, Info); |
14705 | case UO_Minus: |
14706 | // In C standard, WG14 N2478 F.3 p4 |
14707 | // "the unary - raises no floating point exceptions, |
14708 | // even if the operand is signalling." |
14709 | if (!EvaluateFloat(E: E->getSubExpr(), Result, Info)) |
14710 | return false; |
14711 | Result.changeSign(); |
14712 | return true; |
14713 | } |
14714 | } |
14715 | |
14716 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14717 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14718 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14719 | |
14720 | APFloat RHS(0.0); |
14721 | bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info); |
14722 | if (!LHSOK && !Info.noteFailure()) |
14723 | return false; |
14724 | return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK && |
14725 | handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS); |
14726 | } |
14727 | |
14728 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
14729 | Result = E->getValue(); |
14730 | return true; |
14731 | } |
14732 | |
14733 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14734 | const Expr* SubExpr = E->getSubExpr(); |
14735 | |
14736 | switch (E->getCastKind()) { |
14737 | default: |
14738 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14739 | |
14740 | case CK_IntegralToFloating: { |
14741 | APSInt IntResult; |
14742 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14743 | LO: Info.Ctx.getLangOpts()); |
14744 | return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) && |
14745 | HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(), |
14746 | IntResult, E->getType(), Result); |
14747 | } |
14748 | |
14749 | case CK_FixedPointToFloating: { |
14750 | APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
14751 | if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info)) |
14752 | return false; |
14753 | Result = |
14754 | FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
14755 | return true; |
14756 | } |
14757 | |
14758 | case CK_FloatingCast: { |
14759 | if (!Visit(SubExpr)) |
14760 | return false; |
14761 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
14762 | Result); |
14763 | } |
14764 | |
14765 | case CK_FloatingComplexToReal: { |
14766 | ComplexValue V; |
14767 | if (!EvaluateComplex(E: SubExpr, Res&: V, Info)) |
14768 | return false; |
14769 | Result = V.getComplexFloatReal(); |
14770 | return true; |
14771 | } |
14772 | } |
14773 | } |
14774 | |
14775 | //===----------------------------------------------------------------------===// |
14776 | // Complex Evaluation (for float and integer) |
14777 | //===----------------------------------------------------------------------===// |
14778 | |
14779 | namespace { |
14780 | class ComplexExprEvaluator |
14781 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
14782 | ComplexValue &Result; |
14783 | |
14784 | public: |
14785 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
14786 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
14787 | |
14788 | bool Success(const APValue &V, const Expr *e) { |
14789 | Result.setFrom(V); |
14790 | return true; |
14791 | } |
14792 | |
14793 | bool ZeroInitialization(const Expr *E); |
14794 | |
14795 | //===--------------------------------------------------------------------===// |
14796 | // Visitor Methods |
14797 | //===--------------------------------------------------------------------===// |
14798 | |
14799 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
14800 | bool VisitCastExpr(const CastExpr *E); |
14801 | bool VisitBinaryOperator(const BinaryOperator *E); |
14802 | bool VisitUnaryOperator(const UnaryOperator *E); |
14803 | bool VisitInitListExpr(const InitListExpr *E); |
14804 | bool VisitCallExpr(const CallExpr *E); |
14805 | }; |
14806 | } // end anonymous namespace |
14807 | |
14808 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
14809 | EvalInfo &Info) { |
14810 | assert(!E->isValueDependent()); |
14811 | assert(E->isPRValue() && E->getType()->isAnyComplexType()); |
14812 | return ComplexExprEvaluator(Info, Result).Visit(E); |
14813 | } |
14814 | |
14815 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
14816 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
14817 | if (ElemTy->isRealFloatingType()) { |
14818 | Result.makeComplexFloat(); |
14819 | APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy)); |
14820 | Result.FloatReal = Zero; |
14821 | Result.FloatImag = Zero; |
14822 | } else { |
14823 | Result.makeComplexInt(); |
14824 | APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy); |
14825 | Result.IntReal = Zero; |
14826 | Result.IntImag = Zero; |
14827 | } |
14828 | return true; |
14829 | } |
14830 | |
14831 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
14832 | const Expr* SubExpr = E->getSubExpr(); |
14833 | |
14834 | if (SubExpr->getType()->isRealFloatingType()) { |
14835 | Result.makeComplexFloat(); |
14836 | APFloat &Imag = Result.FloatImag; |
14837 | if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info)) |
14838 | return false; |
14839 | |
14840 | Result.FloatReal = APFloat(Imag.getSemantics()); |
14841 | return true; |
14842 | } else { |
14843 | assert(SubExpr->getType()->isIntegerType() && |
14844 | "Unexpected imaginary literal." ); |
14845 | |
14846 | Result.makeComplexInt(); |
14847 | APSInt &Imag = Result.IntImag; |
14848 | if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info)) |
14849 | return false; |
14850 | |
14851 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
14852 | return true; |
14853 | } |
14854 | } |
14855 | |
14856 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14857 | |
14858 | switch (E->getCastKind()) { |
14859 | case CK_BitCast: |
14860 | case CK_BaseToDerived: |
14861 | case CK_DerivedToBase: |
14862 | case CK_UncheckedDerivedToBase: |
14863 | case CK_Dynamic: |
14864 | case CK_ToUnion: |
14865 | case CK_ArrayToPointerDecay: |
14866 | case CK_FunctionToPointerDecay: |
14867 | case CK_NullToPointer: |
14868 | case CK_NullToMemberPointer: |
14869 | case CK_BaseToDerivedMemberPointer: |
14870 | case CK_DerivedToBaseMemberPointer: |
14871 | case CK_MemberPointerToBoolean: |
14872 | case CK_ReinterpretMemberPointer: |
14873 | case CK_ConstructorConversion: |
14874 | case CK_IntegralToPointer: |
14875 | case CK_PointerToIntegral: |
14876 | case CK_PointerToBoolean: |
14877 | case CK_ToVoid: |
14878 | case CK_VectorSplat: |
14879 | case CK_IntegralCast: |
14880 | case CK_BooleanToSignedIntegral: |
14881 | case CK_IntegralToBoolean: |
14882 | case CK_IntegralToFloating: |
14883 | case CK_FloatingToIntegral: |
14884 | case CK_FloatingToBoolean: |
14885 | case CK_FloatingCast: |
14886 | case CK_CPointerToObjCPointerCast: |
14887 | case CK_BlockPointerToObjCPointerCast: |
14888 | case CK_AnyPointerToBlockPointerCast: |
14889 | case CK_ObjCObjectLValueCast: |
14890 | case CK_FloatingComplexToReal: |
14891 | case CK_FloatingComplexToBoolean: |
14892 | case CK_IntegralComplexToReal: |
14893 | case CK_IntegralComplexToBoolean: |
14894 | case CK_ARCProduceObject: |
14895 | case CK_ARCConsumeObject: |
14896 | case CK_ARCReclaimReturnedObject: |
14897 | case CK_ARCExtendBlockObject: |
14898 | case CK_CopyAndAutoreleaseBlockObject: |
14899 | case CK_BuiltinFnToFnPtr: |
14900 | case CK_ZeroToOCLOpaqueType: |
14901 | case CK_NonAtomicToAtomic: |
14902 | case CK_AddressSpaceConversion: |
14903 | case CK_IntToOCLSampler: |
14904 | case CK_FloatingToFixedPoint: |
14905 | case CK_FixedPointToFloating: |
14906 | case CK_FixedPointCast: |
14907 | case CK_FixedPointToBoolean: |
14908 | case CK_FixedPointToIntegral: |
14909 | case CK_IntegralToFixedPoint: |
14910 | case CK_MatrixCast: |
14911 | case CK_HLSLVectorTruncation: |
14912 | llvm_unreachable("invalid cast kind for complex value" ); |
14913 | |
14914 | case CK_LValueToRValue: |
14915 | case CK_AtomicToNonAtomic: |
14916 | case CK_NoOp: |
14917 | case CK_LValueToRValueBitCast: |
14918 | case CK_HLSLArrayRValue: |
14919 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14920 | |
14921 | case CK_Dependent: |
14922 | case CK_LValueBitCast: |
14923 | case CK_UserDefinedConversion: |
14924 | return Error(E); |
14925 | |
14926 | case CK_FloatingRealToComplex: { |
14927 | APFloat &Real = Result.FloatReal; |
14928 | if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info)) |
14929 | return false; |
14930 | |
14931 | Result.makeComplexFloat(); |
14932 | Result.FloatImag = APFloat(Real.getSemantics()); |
14933 | return true; |
14934 | } |
14935 | |
14936 | case CK_FloatingComplexCast: { |
14937 | if (!Visit(E->getSubExpr())) |
14938 | return false; |
14939 | |
14940 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14941 | QualType From |
14942 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14943 | |
14944 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
14945 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
14946 | } |
14947 | |
14948 | case CK_FloatingComplexToIntegralComplex: { |
14949 | if (!Visit(E->getSubExpr())) |
14950 | return false; |
14951 | |
14952 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14953 | QualType From |
14954 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14955 | Result.makeComplexInt(); |
14956 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
14957 | To, Result.IntReal) && |
14958 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
14959 | To, Result.IntImag); |
14960 | } |
14961 | |
14962 | case CK_IntegralRealToComplex: { |
14963 | APSInt &Real = Result.IntReal; |
14964 | if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info)) |
14965 | return false; |
14966 | |
14967 | Result.makeComplexInt(); |
14968 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
14969 | return true; |
14970 | } |
14971 | |
14972 | case CK_IntegralComplexCast: { |
14973 | if (!Visit(E->getSubExpr())) |
14974 | return false; |
14975 | |
14976 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14977 | QualType From |
14978 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14979 | |
14980 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
14981 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
14982 | return true; |
14983 | } |
14984 | |
14985 | case CK_IntegralComplexToFloatingComplex: { |
14986 | if (!Visit(E->getSubExpr())) |
14987 | return false; |
14988 | |
14989 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14990 | LO: Info.Ctx.getLangOpts()); |
14991 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14992 | QualType From |
14993 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14994 | Result.makeComplexFloat(); |
14995 | return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal, |
14996 | To, Result.FloatReal) && |
14997 | HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag, |
14998 | To, Result.FloatImag); |
14999 | } |
15000 | } |
15001 | |
15002 | llvm_unreachable("unknown cast resulting in complex value" ); |
15003 | } |
15004 | |
15005 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
15006 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
15007 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
15008 | |
15009 | // Track whether the LHS or RHS is real at the type system level. When this is |
15010 | // the case we can simplify our evaluation strategy. |
15011 | bool LHSReal = false, RHSReal = false; |
15012 | |
15013 | bool LHSOK; |
15014 | if (E->getLHS()->getType()->isRealFloatingType()) { |
15015 | LHSReal = true; |
15016 | APFloat &Real = Result.FloatReal; |
15017 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info); |
15018 | if (LHSOK) { |
15019 | Result.makeComplexFloat(); |
15020 | Result.FloatImag = APFloat(Real.getSemantics()); |
15021 | } |
15022 | } else { |
15023 | LHSOK = Visit(E->getLHS()); |
15024 | } |
15025 | if (!LHSOK && !Info.noteFailure()) |
15026 | return false; |
15027 | |
15028 | ComplexValue RHS; |
15029 | if (E->getRHS()->getType()->isRealFloatingType()) { |
15030 | RHSReal = true; |
15031 | APFloat &Real = RHS.FloatReal; |
15032 | if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK) |
15033 | return false; |
15034 | RHS.makeComplexFloat(); |
15035 | RHS.FloatImag = APFloat(Real.getSemantics()); |
15036 | } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
15037 | return false; |
15038 | |
15039 | assert(!(LHSReal && RHSReal) && |
15040 | "Cannot have both operands of a complex operation be real." ); |
15041 | switch (E->getOpcode()) { |
15042 | default: return Error(E); |
15043 | case BO_Add: |
15044 | if (Result.isComplexFloat()) { |
15045 | Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(), |
15046 | RM: APFloat::rmNearestTiesToEven); |
15047 | if (LHSReal) |
15048 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
15049 | else if (!RHSReal) |
15050 | Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(), |
15051 | RM: APFloat::rmNearestTiesToEven); |
15052 | } else { |
15053 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
15054 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
15055 | } |
15056 | break; |
15057 | case BO_Sub: |
15058 | if (Result.isComplexFloat()) { |
15059 | Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(), |
15060 | RM: APFloat::rmNearestTiesToEven); |
15061 | if (LHSReal) { |
15062 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
15063 | Result.getComplexFloatImag().changeSign(); |
15064 | } else if (!RHSReal) { |
15065 | Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(), |
15066 | RM: APFloat::rmNearestTiesToEven); |
15067 | } |
15068 | } else { |
15069 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
15070 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
15071 | } |
15072 | break; |
15073 | case BO_Mul: |
15074 | if (Result.isComplexFloat()) { |
15075 | // This is an implementation of complex multiplication according to the |
15076 | // constraints laid out in C11 Annex G. The implementation uses the |
15077 | // following naming scheme: |
15078 | // (a + ib) * (c + id) |
15079 | ComplexValue LHS = Result; |
15080 | APFloat &A = LHS.getComplexFloatReal(); |
15081 | APFloat &B = LHS.getComplexFloatImag(); |
15082 | APFloat &C = RHS.getComplexFloatReal(); |
15083 | APFloat &D = RHS.getComplexFloatImag(); |
15084 | APFloat &ResR = Result.getComplexFloatReal(); |
15085 | APFloat &ResI = Result.getComplexFloatImag(); |
15086 | if (LHSReal) { |
15087 | assert(!RHSReal && "Cannot have two real operands for a complex op!" ); |
15088 | ResR = A * C; |
15089 | ResI = A * D; |
15090 | } else if (RHSReal) { |
15091 | ResR = C * A; |
15092 | ResI = C * B; |
15093 | } else { |
15094 | // In the fully general case, we need to handle NaNs and infinities |
15095 | // robustly. |
15096 | APFloat AC = A * C; |
15097 | APFloat BD = B * D; |
15098 | APFloat AD = A * D; |
15099 | APFloat BC = B * C; |
15100 | ResR = AC - BD; |
15101 | ResI = AD + BC; |
15102 | if (ResR.isNaN() && ResI.isNaN()) { |
15103 | bool Recalc = false; |
15104 | if (A.isInfinity() || B.isInfinity()) { |
15105 | A = APFloat::copySign( |
15106 | Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), Sign: A); |
15107 | B = APFloat::copySign( |
15108 | Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), Sign: B); |
15109 | if (C.isNaN()) |
15110 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
15111 | if (D.isNaN()) |
15112 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
15113 | Recalc = true; |
15114 | } |
15115 | if (C.isInfinity() || D.isInfinity()) { |
15116 | C = APFloat::copySign( |
15117 | Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), Sign: C); |
15118 | D = APFloat::copySign( |
15119 | Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), Sign: D); |
15120 | if (A.isNaN()) |
15121 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
15122 | if (B.isNaN()) |
15123 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
15124 | Recalc = true; |
15125 | } |
15126 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
15127 | AD.isInfinity() || BC.isInfinity())) { |
15128 | if (A.isNaN()) |
15129 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
15130 | if (B.isNaN()) |
15131 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
15132 | if (C.isNaN()) |
15133 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
15134 | if (D.isNaN()) |
15135 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
15136 | Recalc = true; |
15137 | } |
15138 | if (Recalc) { |
15139 | ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D); |
15140 | ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C); |
15141 | } |
15142 | } |
15143 | } |
15144 | } else { |
15145 | ComplexValue LHS = Result; |
15146 | Result.getComplexIntReal() = |
15147 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
15148 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
15149 | Result.getComplexIntImag() = |
15150 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
15151 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
15152 | } |
15153 | break; |
15154 | case BO_Div: |
15155 | if (Result.isComplexFloat()) { |
15156 | // This is an implementation of complex division according to the |
15157 | // constraints laid out in C11 Annex G. The implementation uses the |
15158 | // following naming scheme: |
15159 | // (a + ib) / (c + id) |
15160 | ComplexValue LHS = Result; |
15161 | APFloat &A = LHS.getComplexFloatReal(); |
15162 | APFloat &B = LHS.getComplexFloatImag(); |
15163 | APFloat &C = RHS.getComplexFloatReal(); |
15164 | APFloat &D = RHS.getComplexFloatImag(); |
15165 | APFloat &ResR = Result.getComplexFloatReal(); |
15166 | APFloat &ResI = Result.getComplexFloatImag(); |
15167 | if (RHSReal) { |
15168 | ResR = A / C; |
15169 | ResI = B / C; |
15170 | } else { |
15171 | if (LHSReal) { |
15172 | // No real optimizations we can do here, stub out with zero. |
15173 | B = APFloat::getZero(Sem: A.getSemantics()); |
15174 | } |
15175 | int DenomLogB = 0; |
15176 | APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D)); |
15177 | if (MaxCD.isFinite()) { |
15178 | DenomLogB = ilogb(Arg: MaxCD); |
15179 | C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
15180 | D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
15181 | } |
15182 | APFloat Denom = C * C + D * D; |
15183 | ResR = scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, |
15184 | RM: APFloat::rmNearestTiesToEven); |
15185 | ResI = scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, |
15186 | RM: APFloat::rmNearestTiesToEven); |
15187 | if (ResR.isNaN() && ResI.isNaN()) { |
15188 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
15189 | ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A; |
15190 | ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B; |
15191 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
15192 | D.isFinite()) { |
15193 | A = APFloat::copySign( |
15194 | Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), Sign: A); |
15195 | B = APFloat::copySign( |
15196 | Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), Sign: B); |
15197 | ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D); |
15198 | ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D); |
15199 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
15200 | C = APFloat::copySign( |
15201 | Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), Sign: C); |
15202 | D = APFloat::copySign( |
15203 | Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), Sign: D); |
15204 | ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D); |
15205 | ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D); |
15206 | } |
15207 | } |
15208 | } |
15209 | } else { |
15210 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
15211 | return Error(E, diag::note_expr_divide_by_zero); |
15212 | |
15213 | ComplexValue LHS = Result; |
15214 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
15215 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
15216 | Result.getComplexIntReal() = |
15217 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
15218 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
15219 | Result.getComplexIntImag() = |
15220 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
15221 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
15222 | } |
15223 | break; |
15224 | } |
15225 | |
15226 | return true; |
15227 | } |
15228 | |
15229 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
15230 | // Get the operand value into 'Result'. |
15231 | if (!Visit(E->getSubExpr())) |
15232 | return false; |
15233 | |
15234 | switch (E->getOpcode()) { |
15235 | default: |
15236 | return Error(E); |
15237 | case UO_Extension: |
15238 | return true; |
15239 | case UO_Plus: |
15240 | // The result is always just the subexpr. |
15241 | return true; |
15242 | case UO_Minus: |
15243 | if (Result.isComplexFloat()) { |
15244 | Result.getComplexFloatReal().changeSign(); |
15245 | Result.getComplexFloatImag().changeSign(); |
15246 | } |
15247 | else { |
15248 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
15249 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
15250 | } |
15251 | return true; |
15252 | case UO_Not: |
15253 | if (Result.isComplexFloat()) |
15254 | Result.getComplexFloatImag().changeSign(); |
15255 | else |
15256 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
15257 | return true; |
15258 | } |
15259 | } |
15260 | |
15261 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
15262 | if (E->getNumInits() == 2) { |
15263 | if (E->getType()->isComplexType()) { |
15264 | Result.makeComplexFloat(); |
15265 | if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info)) |
15266 | return false; |
15267 | if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info)) |
15268 | return false; |
15269 | } else { |
15270 | Result.makeComplexInt(); |
15271 | if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info)) |
15272 | return false; |
15273 | if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info)) |
15274 | return false; |
15275 | } |
15276 | return true; |
15277 | } |
15278 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
15279 | } |
15280 | |
15281 | bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) { |
15282 | if (!IsConstantEvaluatedBuiltinCall(E)) |
15283 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
15284 | |
15285 | switch (E->getBuiltinCallee()) { |
15286 | case Builtin::BI__builtin_complex: |
15287 | Result.makeComplexFloat(); |
15288 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info)) |
15289 | return false; |
15290 | if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info)) |
15291 | return false; |
15292 | return true; |
15293 | |
15294 | default: |
15295 | return false; |
15296 | } |
15297 | } |
15298 | |
15299 | //===----------------------------------------------------------------------===// |
15300 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
15301 | // implicit conversion. |
15302 | //===----------------------------------------------------------------------===// |
15303 | |
15304 | namespace { |
15305 | class AtomicExprEvaluator : |
15306 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
15307 | const LValue *This; |
15308 | APValue &Result; |
15309 | public: |
15310 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
15311 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
15312 | |
15313 | bool Success(const APValue &V, const Expr *E) { |
15314 | Result = V; |
15315 | return true; |
15316 | } |
15317 | |
15318 | bool ZeroInitialization(const Expr *E) { |
15319 | ImplicitValueInitExpr VIE( |
15320 | E->getType()->castAs<AtomicType>()->getValueType()); |
15321 | // For atomic-qualified class (and array) types in C++, initialize the |
15322 | // _Atomic-wrapped subobject directly, in-place. |
15323 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
15324 | : Evaluate(Result, Info, &VIE); |
15325 | } |
15326 | |
15327 | bool VisitCastExpr(const CastExpr *E) { |
15328 | switch (E->getCastKind()) { |
15329 | default: |
15330 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15331 | case CK_NullToPointer: |
15332 | VisitIgnoredValue(E: E->getSubExpr()); |
15333 | return ZeroInitialization(E); |
15334 | case CK_NonAtomicToAtomic: |
15335 | return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr()) |
15336 | : Evaluate(Result, Info, E: E->getSubExpr()); |
15337 | } |
15338 | } |
15339 | }; |
15340 | } // end anonymous namespace |
15341 | |
15342 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
15343 | EvalInfo &Info) { |
15344 | assert(!E->isValueDependent()); |
15345 | assert(E->isPRValue() && E->getType()->isAtomicType()); |
15346 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
15347 | } |
15348 | |
15349 | //===----------------------------------------------------------------------===// |
15350 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
15351 | // comma operator |
15352 | //===----------------------------------------------------------------------===// |
15353 | |
15354 | namespace { |
15355 | class VoidExprEvaluator |
15356 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
15357 | public: |
15358 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
15359 | |
15360 | bool Success(const APValue &V, const Expr *e) { return true; } |
15361 | |
15362 | bool ZeroInitialization(const Expr *E) { return true; } |
15363 | |
15364 | bool VisitCastExpr(const CastExpr *E) { |
15365 | switch (E->getCastKind()) { |
15366 | default: |
15367 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15368 | case CK_ToVoid: |
15369 | VisitIgnoredValue(E: E->getSubExpr()); |
15370 | return true; |
15371 | } |
15372 | } |
15373 | |
15374 | bool VisitCallExpr(const CallExpr *E) { |
15375 | if (!IsConstantEvaluatedBuiltinCall(E)) |
15376 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
15377 | |
15378 | switch (E->getBuiltinCallee()) { |
15379 | case Builtin::BI__assume: |
15380 | case Builtin::BI__builtin_assume: |
15381 | // The argument is not evaluated! |
15382 | return true; |
15383 | |
15384 | case Builtin::BI__builtin_operator_delete: |
15385 | return HandleOperatorDeleteCall(Info, E); |
15386 | |
15387 | default: |
15388 | return false; |
15389 | } |
15390 | } |
15391 | |
15392 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
15393 | }; |
15394 | } // end anonymous namespace |
15395 | |
15396 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
15397 | // We cannot speculatively evaluate a delete expression. |
15398 | if (Info.SpeculativeEvaluationDepth) |
15399 | return false; |
15400 | |
15401 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
15402 | if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) { |
15403 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
15404 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
15405 | return false; |
15406 | } |
15407 | |
15408 | const Expr *Arg = E->getArgument(); |
15409 | |
15410 | LValue Pointer; |
15411 | if (!EvaluatePointer(E: Arg, Result&: Pointer, Info)) |
15412 | return false; |
15413 | if (Pointer.Designator.Invalid) |
15414 | return false; |
15415 | |
15416 | // Deleting a null pointer has no effect. |
15417 | if (Pointer.isNullPointer()) { |
15418 | // This is the only case where we need to produce an extension warning: |
15419 | // the only other way we can succeed is if we find a dynamic allocation, |
15420 | // and we will have warned when we allocated it in that case. |
15421 | if (!Info.getLangOpts().CPlusPlus20) |
15422 | Info.CCEDiag(E, diag::note_constexpr_new); |
15423 | return true; |
15424 | } |
15425 | |
15426 | std::optional<DynAlloc *> Alloc = CheckDeleteKind( |
15427 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
15428 | if (!Alloc) |
15429 | return false; |
15430 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
15431 | |
15432 | // For the non-array case, the designator must be empty if the static type |
15433 | // does not have a virtual destructor. |
15434 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
15435 | !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) { |
15436 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
15437 | << Arg->getType()->getPointeeType() << AllocType; |
15438 | return false; |
15439 | } |
15440 | |
15441 | // For a class type with a virtual destructor, the selected operator delete |
15442 | // is the one looked up when building the destructor. |
15443 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
15444 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType); |
15445 | if (VirtualDelete && |
15446 | !VirtualDelete->isReplaceableGlobalAllocationFunction()) { |
15447 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
15448 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
15449 | return false; |
15450 | } |
15451 | } |
15452 | |
15453 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
15454 | (*Alloc)->Value, AllocType)) |
15455 | return false; |
15456 | |
15457 | if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
15458 | // The element was already erased. This means the destructor call also |
15459 | // deleted the object. |
15460 | // FIXME: This probably results in undefined behavior before we get this |
15461 | // far, and should be diagnosed elsewhere first. |
15462 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
15463 | return false; |
15464 | } |
15465 | |
15466 | return true; |
15467 | } |
15468 | |
15469 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
15470 | assert(!E->isValueDependent()); |
15471 | assert(E->isPRValue() && E->getType()->isVoidType()); |
15472 | return VoidExprEvaluator(Info).Visit(E); |
15473 | } |
15474 | |
15475 | //===----------------------------------------------------------------------===// |
15476 | // Top level Expr::EvaluateAsRValue method. |
15477 | //===----------------------------------------------------------------------===// |
15478 | |
15479 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
15480 | assert(!E->isValueDependent()); |
15481 | // In C, function designators are not lvalues, but we evaluate them as if they |
15482 | // are. |
15483 | QualType T = E->getType(); |
15484 | if (E->isGLValue() || T->isFunctionType()) { |
15485 | LValue LV; |
15486 | if (!EvaluateLValue(E, Result&: LV, Info)) |
15487 | return false; |
15488 | LV.moveInto(V&: Result); |
15489 | } else if (T->isVectorType()) { |
15490 | if (!EvaluateVector(E, Result, Info)) |
15491 | return false; |
15492 | } else if (T->isIntegralOrEnumerationType()) { |
15493 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
15494 | return false; |
15495 | } else if (T->hasPointerRepresentation()) { |
15496 | LValue LV; |
15497 | if (!EvaluatePointer(E, Result&: LV, Info)) |
15498 | return false; |
15499 | LV.moveInto(V&: Result); |
15500 | } else if (T->isRealFloatingType()) { |
15501 | llvm::APFloat F(0.0); |
15502 | if (!EvaluateFloat(E, Result&: F, Info)) |
15503 | return false; |
15504 | Result = APValue(F); |
15505 | } else if (T->isAnyComplexType()) { |
15506 | ComplexValue C; |
15507 | if (!EvaluateComplex(E, Result&: C, Info)) |
15508 | return false; |
15509 | C.moveInto(v&: Result); |
15510 | } else if (T->isFixedPointType()) { |
15511 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
15512 | } else if (T->isMemberPointerType()) { |
15513 | MemberPtr P; |
15514 | if (!EvaluateMemberPointer(E, Result&: P, Info)) |
15515 | return false; |
15516 | P.moveInto(V&: Result); |
15517 | return true; |
15518 | } else if (T->isArrayType()) { |
15519 | LValue LV; |
15520 | APValue &Value = |
15521 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
15522 | if (!EvaluateArray(E, This: LV, Result&: Value, Info)) |
15523 | return false; |
15524 | Result = Value; |
15525 | } else if (T->isRecordType()) { |
15526 | LValue LV; |
15527 | APValue &Value = |
15528 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
15529 | if (!EvaluateRecord(E, This: LV, Result&: Value, Info)) |
15530 | return false; |
15531 | Result = Value; |
15532 | } else if (T->isVoidType()) { |
15533 | if (!Info.getLangOpts().CPlusPlus11) |
15534 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
15535 | << E->getType(); |
15536 | if (!EvaluateVoid(E, Info)) |
15537 | return false; |
15538 | } else if (T->isAtomicType()) { |
15539 | QualType Unqual = T.getAtomicUnqualifiedType(); |
15540 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
15541 | LValue LV; |
15542 | APValue &Value = Info.CurrentCall->createTemporary( |
15543 | Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV); |
15544 | if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info)) |
15545 | return false; |
15546 | Result = Value; |
15547 | } else { |
15548 | if (!EvaluateAtomic(E, This: nullptr, Result, Info)) |
15549 | return false; |
15550 | } |
15551 | } else if (Info.getLangOpts().CPlusPlus11) { |
15552 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
15553 | return false; |
15554 | } else { |
15555 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
15556 | return false; |
15557 | } |
15558 | |
15559 | return true; |
15560 | } |
15561 | |
15562 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
15563 | /// cases, the in-place evaluation is essential, since later initializers for |
15564 | /// an object can indirectly refer to subobjects which were initialized earlier. |
15565 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
15566 | const Expr *E, bool AllowNonLiteralTypes) { |
15567 | assert(!E->isValueDependent()); |
15568 | |
15569 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This)) |
15570 | return false; |
15571 | |
15572 | if (E->isPRValue()) { |
15573 | // Evaluate arrays and record types in-place, so that later initializers can |
15574 | // refer to earlier-initialized members of the object. |
15575 | QualType T = E->getType(); |
15576 | if (T->isArrayType()) |
15577 | return EvaluateArray(E, This, Result, Info); |
15578 | else if (T->isRecordType()) |
15579 | return EvaluateRecord(E, This, Result, Info); |
15580 | else if (T->isAtomicType()) { |
15581 | QualType Unqual = T.getAtomicUnqualifiedType(); |
15582 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
15583 | return EvaluateAtomic(E, This: &This, Result, Info); |
15584 | } |
15585 | } |
15586 | |
15587 | // For any other type, in-place evaluation is unimportant. |
15588 | return Evaluate(Result, Info, E); |
15589 | } |
15590 | |
15591 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
15592 | /// lvalue-to-rvalue cast if it is an lvalue. |
15593 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
15594 | assert(!E->isValueDependent()); |
15595 | |
15596 | if (E->getType().isNull()) |
15597 | return false; |
15598 | |
15599 | if (!CheckLiteralType(Info, E)) |
15600 | return false; |
15601 | |
15602 | if (Info.EnableNewConstInterp) { |
15603 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result)) |
15604 | return false; |
15605 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
15606 | Kind: ConstantExprKind::Normal); |
15607 | } |
15608 | |
15609 | if (!::Evaluate(Result, Info, E)) |
15610 | return false; |
15611 | |
15612 | // Implicit lvalue-to-rvalue cast. |
15613 | if (E->isGLValue()) { |
15614 | LValue LV; |
15615 | LV.setFrom(Ctx&: Info.Ctx, V: Result); |
15616 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result)) |
15617 | return false; |
15618 | } |
15619 | |
15620 | // Check this core constant expression is a constant expression. |
15621 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
15622 | Kind: ConstantExprKind::Normal) && |
15623 | CheckMemoryLeaks(Info); |
15624 | } |
15625 | |
15626 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
15627 | const ASTContext &Ctx, bool &IsConst) { |
15628 | // Fast-path evaluations of integer literals, since we sometimes see files |
15629 | // containing vast quantities of these. |
15630 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Val: Exp)) { |
15631 | Result.Val = APValue(APSInt(L->getValue(), |
15632 | L->getType()->isUnsignedIntegerType())); |
15633 | IsConst = true; |
15634 | return true; |
15635 | } |
15636 | |
15637 | if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) { |
15638 | Result.Val = APValue(APSInt(APInt(1, L->getValue()))); |
15639 | IsConst = true; |
15640 | return true; |
15641 | } |
15642 | |
15643 | if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) { |
15644 | if (CE->hasAPValueResult()) { |
15645 | Result.Val = CE->getAPValueResult(); |
15646 | IsConst = true; |
15647 | return true; |
15648 | } |
15649 | |
15650 | // The SubExpr is usually just an IntegerLiteral. |
15651 | return FastEvaluateAsRValue(CE->getSubExpr(), Result, Ctx, IsConst); |
15652 | } |
15653 | |
15654 | // This case should be rare, but we need to check it before we check on |
15655 | // the type below. |
15656 | if (Exp->getType().isNull()) { |
15657 | IsConst = false; |
15658 | return true; |
15659 | } |
15660 | |
15661 | return false; |
15662 | } |
15663 | |
15664 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
15665 | Expr::SideEffectsKind SEK) { |
15666 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
15667 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
15668 | } |
15669 | |
15670 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
15671 | const ASTContext &Ctx, EvalInfo &Info) { |
15672 | assert(!E->isValueDependent()); |
15673 | bool IsConst; |
15674 | if (FastEvaluateAsRValue(Exp: E, Result, Ctx, IsConst)) |
15675 | return IsConst; |
15676 | |
15677 | return EvaluateAsRValue(Info, E, Result&: Result.Val); |
15678 | } |
15679 | |
15680 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
15681 | const ASTContext &Ctx, |
15682 | Expr::SideEffectsKind AllowSideEffects, |
15683 | EvalInfo &Info) { |
15684 | assert(!E->isValueDependent()); |
15685 | if (!E->getType()->isIntegralOrEnumerationType()) |
15686 | return false; |
15687 | |
15688 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) || |
15689 | !ExprResult.Val.isInt() || |
15690 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15691 | return false; |
15692 | |
15693 | return true; |
15694 | } |
15695 | |
15696 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
15697 | const ASTContext &Ctx, |
15698 | Expr::SideEffectsKind AllowSideEffects, |
15699 | EvalInfo &Info) { |
15700 | assert(!E->isValueDependent()); |
15701 | if (!E->getType()->isFixedPointType()) |
15702 | return false; |
15703 | |
15704 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info)) |
15705 | return false; |
15706 | |
15707 | if (!ExprResult.Val.isFixedPoint() || |
15708 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15709 | return false; |
15710 | |
15711 | return true; |
15712 | } |
15713 | |
15714 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
15715 | /// any crazy technique (that has nothing to do with language standards) that |
15716 | /// we want to. If this function returns true, it returns the folded constant |
15717 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
15718 | /// will be applied to the result. |
15719 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
15720 | bool InConstantContext) const { |
15721 | assert(!isValueDependent() && |
15722 | "Expression evaluator can't be called on a dependent expression." ); |
15723 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue" ); |
15724 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15725 | Info.InConstantContext = InConstantContext; |
15726 | return ::EvaluateAsRValue(E: this, Result, Ctx, Info); |
15727 | } |
15728 | |
15729 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
15730 | bool InConstantContext) const { |
15731 | assert(!isValueDependent() && |
15732 | "Expression evaluator can't be called on a dependent expression." ); |
15733 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition" ); |
15734 | EvalResult Scratch; |
15735 | return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) && |
15736 | HandleConversionToBool(Val: Scratch.Val, Result); |
15737 | } |
15738 | |
15739 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
15740 | SideEffectsKind AllowSideEffects, |
15741 | bool InConstantContext) const { |
15742 | assert(!isValueDependent() && |
15743 | "Expression evaluator can't be called on a dependent expression." ); |
15744 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt" ); |
15745 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15746 | Info.InConstantContext = InConstantContext; |
15747 | return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
15748 | } |
15749 | |
15750 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
15751 | SideEffectsKind AllowSideEffects, |
15752 | bool InConstantContext) const { |
15753 | assert(!isValueDependent() && |
15754 | "Expression evaluator can't be called on a dependent expression." ); |
15755 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint" ); |
15756 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15757 | Info.InConstantContext = InConstantContext; |
15758 | return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
15759 | } |
15760 | |
15761 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
15762 | SideEffectsKind AllowSideEffects, |
15763 | bool InConstantContext) const { |
15764 | assert(!isValueDependent() && |
15765 | "Expression evaluator can't be called on a dependent expression." ); |
15766 | |
15767 | if (!getType()->isRealFloatingType()) |
15768 | return false; |
15769 | |
15770 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat" ); |
15771 | EvalResult ExprResult; |
15772 | if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) || |
15773 | !ExprResult.Val.isFloat() || |
15774 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15775 | return false; |
15776 | |
15777 | Result = ExprResult.Val.getFloat(); |
15778 | return true; |
15779 | } |
15780 | |
15781 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
15782 | bool InConstantContext) const { |
15783 | assert(!isValueDependent() && |
15784 | "Expression evaluator can't be called on a dependent expression." ); |
15785 | |
15786 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue" ); |
15787 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
15788 | Info.InConstantContext = InConstantContext; |
15789 | LValue LV; |
15790 | CheckedTemporaries CheckedTemps; |
15791 | if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() || |
15792 | Result.HasSideEffects || |
15793 | !CheckLValueConstantExpression(Info, Loc: getExprLoc(), |
15794 | Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV, |
15795 | Kind: ConstantExprKind::Normal, CheckedTemps)) |
15796 | return false; |
15797 | |
15798 | LV.moveInto(V&: Result.Val); |
15799 | return true; |
15800 | } |
15801 | |
15802 | static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base, |
15803 | APValue DestroyedValue, QualType Type, |
15804 | SourceLocation Loc, Expr::EvalStatus &EStatus, |
15805 | bool IsConstantDestruction) { |
15806 | EvalInfo Info(Ctx, EStatus, |
15807 | IsConstantDestruction ? EvalInfo::EM_ConstantExpression |
15808 | : EvalInfo::EM_ConstantFold); |
15809 | Info.setEvaluatingDecl(Base, Value&: DestroyedValue, |
15810 | EDK: EvalInfo::EvaluatingDeclKind::Dtor); |
15811 | Info.InConstantContext = IsConstantDestruction; |
15812 | |
15813 | LValue LVal; |
15814 | LVal.set(B: Base); |
15815 | |
15816 | if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) || |
15817 | EStatus.HasSideEffects) |
15818 | return false; |
15819 | |
15820 | if (!Info.discardCleanups()) |
15821 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15822 | |
15823 | return true; |
15824 | } |
15825 | |
15826 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, |
15827 | ConstantExprKind Kind) const { |
15828 | assert(!isValueDependent() && |
15829 | "Expression evaluator can't be called on a dependent expression." ); |
15830 | bool IsConst; |
15831 | if (FastEvaluateAsRValue(Exp: this, Result, Ctx, IsConst) && Result.Val.hasValue()) |
15832 | return true; |
15833 | |
15834 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr" ); |
15835 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
15836 | EvalInfo Info(Ctx, Result, EM); |
15837 | Info.InConstantContext = true; |
15838 | |
15839 | if (Info.EnableNewConstInterp) { |
15840 | if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val)) |
15841 | return false; |
15842 | return CheckConstantExpression(Info, DiagLoc: getExprLoc(), |
15843 | Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind); |
15844 | } |
15845 | |
15846 | // The type of the object we're initializing is 'const T' for a class NTTP. |
15847 | QualType T = getType(); |
15848 | if (Kind == ConstantExprKind::ClassTemplateArgument) |
15849 | T.addConst(); |
15850 | |
15851 | // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to |
15852 | // represent the result of the evaluation. CheckConstantExpression ensures |
15853 | // this doesn't escape. |
15854 | MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true); |
15855 | APValue::LValueBase Base(&BaseMTE); |
15856 | Info.setEvaluatingDecl(Base, Value&: Result.Val); |
15857 | |
15858 | if (Info.EnableNewConstInterp) { |
15859 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E: this, Result&: Result.Val)) |
15860 | return false; |
15861 | } else { |
15862 | LValue LVal; |
15863 | LVal.set(B: Base); |
15864 | // C++23 [intro.execution]/p5 |
15865 | // A full-expression is [...] a constant-expression |
15866 | // So we need to make sure temporary objects are destroyed after having |
15867 | // evaluating the expression (per C++23 [class.temporary]/p4). |
15868 | FullExpressionRAII Scope(Info); |
15869 | if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) || |
15870 | Result.HasSideEffects || !Scope.destroy()) |
15871 | return false; |
15872 | |
15873 | if (!Info.discardCleanups()) |
15874 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15875 | } |
15876 | |
15877 | if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this), |
15878 | Value: Result.Val, Kind)) |
15879 | return false; |
15880 | if (!CheckMemoryLeaks(Info)) |
15881 | return false; |
15882 | |
15883 | // If this is a class template argument, it's required to have constant |
15884 | // destruction too. |
15885 | if (Kind == ConstantExprKind::ClassTemplateArgument && |
15886 | (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result, |
15887 | true) || |
15888 | Result.HasSideEffects)) { |
15889 | // FIXME: Prefix a note to indicate that the problem is lack of constant |
15890 | // destruction. |
15891 | return false; |
15892 | } |
15893 | |
15894 | return true; |
15895 | } |
15896 | |
15897 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
15898 | const VarDecl *VD, |
15899 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
15900 | bool IsConstantInitialization) const { |
15901 | assert(!isValueDependent() && |
15902 | "Expression evaluator can't be called on a dependent expression." ); |
15903 | |
15904 | llvm::TimeTraceScope TimeScope("EvaluateAsInitializer" , [&] { |
15905 | std::string Name; |
15906 | llvm::raw_string_ostream OS(Name); |
15907 | VD->printQualifiedName(OS); |
15908 | return Name; |
15909 | }); |
15910 | |
15911 | Expr::EvalStatus EStatus; |
15912 | EStatus.Diag = &Notes; |
15913 | |
15914 | EvalInfo Info(Ctx, EStatus, |
15915 | (IsConstantInitialization && |
15916 | (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23)) |
15917 | ? EvalInfo::EM_ConstantExpression |
15918 | : EvalInfo::EM_ConstantFold); |
15919 | Info.setEvaluatingDecl(VD, Value); |
15920 | Info.InConstantContext = IsConstantInitialization; |
15921 | |
15922 | SourceLocation DeclLoc = VD->getLocation(); |
15923 | QualType DeclTy = VD->getType(); |
15924 | |
15925 | if (Info.EnableNewConstInterp) { |
15926 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
15927 | if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Result&: Value)) |
15928 | return false; |
15929 | |
15930 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
15931 | Kind: ConstantExprKind::Normal); |
15932 | } else { |
15933 | LValue LVal; |
15934 | LVal.set(VD); |
15935 | |
15936 | { |
15937 | // C++23 [intro.execution]/p5 |
15938 | // A full-expression is ... an init-declarator ([dcl.decl]) or a |
15939 | // mem-initializer. |
15940 | // So we need to make sure temporary objects are destroyed after having |
15941 | // evaluated the expression (per C++23 [class.temporary]/p4). |
15942 | // |
15943 | // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the |
15944 | // serialization code calls ParmVarDecl::getDefaultArg() which strips the |
15945 | // outermost FullExpr, such as ExprWithCleanups. |
15946 | FullExpressionRAII Scope(Info); |
15947 | if (!EvaluateInPlace(Result&: Value, Info, This: LVal, E: this, |
15948 | /*AllowNonLiteralTypes=*/true) || |
15949 | EStatus.HasSideEffects) |
15950 | return false; |
15951 | } |
15952 | |
15953 | // At this point, any lifetime-extended temporaries are completely |
15954 | // initialized. |
15955 | Info.performLifetimeExtension(); |
15956 | |
15957 | if (!Info.discardCleanups()) |
15958 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15959 | } |
15960 | |
15961 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
15962 | Kind: ConstantExprKind::Normal) && |
15963 | CheckMemoryLeaks(Info); |
15964 | } |
15965 | |
15966 | bool VarDecl::evaluateDestruction( |
15967 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
15968 | Expr::EvalStatus EStatus; |
15969 | EStatus.Diag = &Notes; |
15970 | |
15971 | // Only treat the destruction as constant destruction if we formally have |
15972 | // constant initialization (or are usable in a constant expression). |
15973 | bool IsConstantDestruction = hasConstantInitialization(); |
15974 | |
15975 | // Make a copy of the value for the destructor to mutate, if we know it. |
15976 | // Otherwise, treat the value as default-initialized; if the destructor works |
15977 | // anyway, then the destruction is constant (and must be essentially empty). |
15978 | APValue DestroyedValue; |
15979 | if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent()) |
15980 | DestroyedValue = *getEvaluatedValue(); |
15981 | else if (!handleDefaultInitValue(getType(), DestroyedValue)) |
15982 | return false; |
15983 | |
15984 | if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue), |
15985 | getType(), getLocation(), EStatus, |
15986 | IsConstantDestruction) || |
15987 | EStatus.HasSideEffects) |
15988 | return false; |
15989 | |
15990 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
15991 | return true; |
15992 | } |
15993 | |
15994 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
15995 | /// constant folded, but discard the result. |
15996 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
15997 | assert(!isValueDependent() && |
15998 | "Expression evaluator can't be called on a dependent expression." ); |
15999 | |
16000 | EvalResult Result; |
16001 | return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) && |
16002 | !hasUnacceptableSideEffect(Result, SEK); |
16003 | } |
16004 | |
16005 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
16006 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
16007 | assert(!isValueDependent() && |
16008 | "Expression evaluator can't be called on a dependent expression." ); |
16009 | |
16010 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt" ); |
16011 | EvalResult EVResult; |
16012 | EVResult.Diag = Diag; |
16013 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
16014 | Info.InConstantContext = true; |
16015 | |
16016 | bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info); |
16017 | (void)Result; |
16018 | assert(Result && "Could not evaluate expression" ); |
16019 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer" ); |
16020 | |
16021 | return EVResult.Val.getInt(); |
16022 | } |
16023 | |
16024 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
16025 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
16026 | assert(!isValueDependent() && |
16027 | "Expression evaluator can't be called on a dependent expression." ); |
16028 | |
16029 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow" ); |
16030 | EvalResult EVResult; |
16031 | EVResult.Diag = Diag; |
16032 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
16033 | Info.InConstantContext = true; |
16034 | Info.CheckingForUndefinedBehavior = true; |
16035 | |
16036 | bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
16037 | (void)Result; |
16038 | assert(Result && "Could not evaluate expression" ); |
16039 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer" ); |
16040 | |
16041 | return EVResult.Val.getInt(); |
16042 | } |
16043 | |
16044 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
16045 | assert(!isValueDependent() && |
16046 | "Expression evaluator can't be called on a dependent expression." ); |
16047 | |
16048 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow" ); |
16049 | bool IsConst; |
16050 | EvalResult EVResult; |
16051 | if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult, Ctx, IsConst)) { |
16052 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
16053 | Info.CheckingForUndefinedBehavior = true; |
16054 | (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
16055 | } |
16056 | } |
16057 | |
16058 | bool Expr::EvalResult::isGlobalLValue() const { |
16059 | assert(Val.isLValue()); |
16060 | return IsGlobalLValue(B: Val.getLValueBase()); |
16061 | } |
16062 | |
16063 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
16064 | /// an integer constant expression. |
16065 | |
16066 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
16067 | /// comma, etc |
16068 | |
16069 | // CheckICE - This function does the fundamental ICE checking: the returned |
16070 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
16071 | // and a (possibly null) SourceLocation indicating the location of the problem. |
16072 | // |
16073 | // Note that to reduce code duplication, this helper does no evaluation |
16074 | // itself; the caller checks whether the expression is evaluatable, and |
16075 | // in the rare cases where CheckICE actually cares about the evaluated |
16076 | // value, it calls into Evaluate. |
16077 | |
16078 | namespace { |
16079 | |
16080 | enum ICEKind { |
16081 | /// This expression is an ICE. |
16082 | IK_ICE, |
16083 | /// This expression is not an ICE, but if it isn't evaluated, it's |
16084 | /// a legal subexpression for an ICE. This return value is used to handle |
16085 | /// the comma operator in C99 mode, and non-constant subexpressions. |
16086 | IK_ICEIfUnevaluated, |
16087 | /// This expression is not an ICE, and is not a legal subexpression for one. |
16088 | IK_NotICE |
16089 | }; |
16090 | |
16091 | struct ICEDiag { |
16092 | ICEKind Kind; |
16093 | SourceLocation Loc; |
16094 | |
16095 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
16096 | }; |
16097 | |
16098 | } |
16099 | |
16100 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
16101 | |
16102 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
16103 | |
16104 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
16105 | Expr::EvalResult EVResult; |
16106 | Expr::EvalStatus Status; |
16107 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
16108 | |
16109 | Info.InConstantContext = true; |
16110 | if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects || |
16111 | !EVResult.Val.isInt()) |
16112 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16113 | |
16114 | return NoDiag(); |
16115 | } |
16116 | |
16117 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
16118 | assert(!E->isValueDependent() && "Should not see value dependent exprs!" ); |
16119 | if (!E->getType()->isIntegralOrEnumerationType()) |
16120 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16121 | |
16122 | switch (E->getStmtClass()) { |
16123 | #define ABSTRACT_STMT(Node) |
16124 | #define STMT(Node, Base) case Expr::Node##Class: |
16125 | #define EXPR(Node, Base) |
16126 | #include "clang/AST/StmtNodes.inc" |
16127 | case Expr::PredefinedExprClass: |
16128 | case Expr::FloatingLiteralClass: |
16129 | case Expr::ImaginaryLiteralClass: |
16130 | case Expr::StringLiteralClass: |
16131 | case Expr::ArraySubscriptExprClass: |
16132 | case Expr::MatrixSubscriptExprClass: |
16133 | case Expr::OMPArraySectionExprClass: |
16134 | case Expr::OMPArrayShapingExprClass: |
16135 | case Expr::OMPIteratorExprClass: |
16136 | case Expr::MemberExprClass: |
16137 | case Expr::CompoundAssignOperatorClass: |
16138 | case Expr::CompoundLiteralExprClass: |
16139 | case Expr::ExtVectorElementExprClass: |
16140 | case Expr::DesignatedInitExprClass: |
16141 | case Expr::ArrayInitLoopExprClass: |
16142 | case Expr::ArrayInitIndexExprClass: |
16143 | case Expr::NoInitExprClass: |
16144 | case Expr::DesignatedInitUpdateExprClass: |
16145 | case Expr::ImplicitValueInitExprClass: |
16146 | case Expr::ParenListExprClass: |
16147 | case Expr::VAArgExprClass: |
16148 | case Expr::AddrLabelExprClass: |
16149 | case Expr::StmtExprClass: |
16150 | case Expr::CXXMemberCallExprClass: |
16151 | case Expr::CUDAKernelCallExprClass: |
16152 | case Expr::CXXAddrspaceCastExprClass: |
16153 | case Expr::CXXDynamicCastExprClass: |
16154 | case Expr::CXXTypeidExprClass: |
16155 | case Expr::CXXUuidofExprClass: |
16156 | case Expr::MSPropertyRefExprClass: |
16157 | case Expr::MSPropertySubscriptExprClass: |
16158 | case Expr::CXXNullPtrLiteralExprClass: |
16159 | case Expr::UserDefinedLiteralClass: |
16160 | case Expr::CXXThisExprClass: |
16161 | case Expr::CXXThrowExprClass: |
16162 | case Expr::CXXNewExprClass: |
16163 | case Expr::CXXDeleteExprClass: |
16164 | case Expr::CXXPseudoDestructorExprClass: |
16165 | case Expr::UnresolvedLookupExprClass: |
16166 | case Expr::TypoExprClass: |
16167 | case Expr::RecoveryExprClass: |
16168 | case Expr::DependentScopeDeclRefExprClass: |
16169 | case Expr::CXXConstructExprClass: |
16170 | case Expr::CXXInheritedCtorInitExprClass: |
16171 | case Expr::CXXStdInitializerListExprClass: |
16172 | case Expr::CXXBindTemporaryExprClass: |
16173 | case Expr::ExprWithCleanupsClass: |
16174 | case Expr::CXXTemporaryObjectExprClass: |
16175 | case Expr::CXXUnresolvedConstructExprClass: |
16176 | case Expr::CXXDependentScopeMemberExprClass: |
16177 | case Expr::UnresolvedMemberExprClass: |
16178 | case Expr::ObjCStringLiteralClass: |
16179 | case Expr::ObjCBoxedExprClass: |
16180 | case Expr::ObjCArrayLiteralClass: |
16181 | case Expr::ObjCDictionaryLiteralClass: |
16182 | case Expr::ObjCEncodeExprClass: |
16183 | case Expr::ObjCMessageExprClass: |
16184 | case Expr::ObjCSelectorExprClass: |
16185 | case Expr::ObjCProtocolExprClass: |
16186 | case Expr::ObjCIvarRefExprClass: |
16187 | case Expr::ObjCPropertyRefExprClass: |
16188 | case Expr::ObjCSubscriptRefExprClass: |
16189 | case Expr::ObjCIsaExprClass: |
16190 | case Expr::ObjCAvailabilityCheckExprClass: |
16191 | case Expr::ShuffleVectorExprClass: |
16192 | case Expr::ConvertVectorExprClass: |
16193 | case Expr::BlockExprClass: |
16194 | case Expr::NoStmtClass: |
16195 | case Expr::OpaqueValueExprClass: |
16196 | case Expr::PackExpansionExprClass: |
16197 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
16198 | case Expr::FunctionParmPackExprClass: |
16199 | case Expr::AsTypeExprClass: |
16200 | case Expr::ObjCIndirectCopyRestoreExprClass: |
16201 | case Expr::MaterializeTemporaryExprClass: |
16202 | case Expr::PseudoObjectExprClass: |
16203 | case Expr::AtomicExprClass: |
16204 | case Expr::LambdaExprClass: |
16205 | case Expr::CXXFoldExprClass: |
16206 | case Expr::CoawaitExprClass: |
16207 | case Expr::DependentCoawaitExprClass: |
16208 | case Expr::CoyieldExprClass: |
16209 | case Expr::SYCLUniqueStableNameExprClass: |
16210 | case Expr::CXXParenListInitExprClass: |
16211 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16212 | |
16213 | case Expr::InitListExprClass: { |
16214 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
16215 | // form "T x = { a };" is equivalent to "T x = a;". |
16216 | // Unless we're initializing a reference, T is a scalar as it is known to be |
16217 | // of integral or enumeration type. |
16218 | if (E->isPRValue()) |
16219 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
16220 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
16221 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16222 | } |
16223 | |
16224 | case Expr::SizeOfPackExprClass: |
16225 | case Expr::GNUNullExprClass: |
16226 | case Expr::SourceLocExprClass: |
16227 | return NoDiag(); |
16228 | |
16229 | case Expr::PackIndexingExprClass: |
16230 | return CheckICE(cast<PackIndexingExpr>(E)->getSelectedExpr(), Ctx); |
16231 | |
16232 | case Expr::SubstNonTypeTemplateParmExprClass: |
16233 | return |
16234 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
16235 | |
16236 | case Expr::ConstantExprClass: |
16237 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
16238 | |
16239 | case Expr::ParenExprClass: |
16240 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
16241 | case Expr::GenericSelectionExprClass: |
16242 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
16243 | case Expr::IntegerLiteralClass: |
16244 | case Expr::FixedPointLiteralClass: |
16245 | case Expr::CharacterLiteralClass: |
16246 | case Expr::ObjCBoolLiteralExprClass: |
16247 | case Expr::CXXBoolLiteralExprClass: |
16248 | case Expr::CXXScalarValueInitExprClass: |
16249 | case Expr::TypeTraitExprClass: |
16250 | case Expr::ConceptSpecializationExprClass: |
16251 | case Expr::RequiresExprClass: |
16252 | case Expr::ArrayTypeTraitExprClass: |
16253 | case Expr::ExpressionTraitExprClass: |
16254 | case Expr::CXXNoexceptExprClass: |
16255 | return NoDiag(); |
16256 | case Expr::CallExprClass: |
16257 | case Expr::CXXOperatorCallExprClass: { |
16258 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
16259 | // constant expressions, but they can never be ICEs because an ICE cannot |
16260 | // contain an operand of (pointer to) function type. |
16261 | const CallExpr *CE = cast<CallExpr>(E); |
16262 | if (CE->getBuiltinCallee()) |
16263 | return CheckEvalInICE(E, Ctx); |
16264 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16265 | } |
16266 | case Expr::CXXRewrittenBinaryOperatorClass: |
16267 | return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(), |
16268 | Ctx); |
16269 | case Expr::DeclRefExprClass: { |
16270 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
16271 | if (isa<EnumConstantDecl>(D)) |
16272 | return NoDiag(); |
16273 | |
16274 | // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified |
16275 | // integer variables in constant expressions: |
16276 | // |
16277 | // C++ 7.1.5.1p2 |
16278 | // A variable of non-volatile const-qualified integral or enumeration |
16279 | // type initialized by an ICE can be used in ICEs. |
16280 | // |
16281 | // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In |
16282 | // that mode, use of reference variables should not be allowed. |
16283 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
16284 | if (VD && VD->isUsableInConstantExpressions(C: Ctx) && |
16285 | !VD->getType()->isReferenceType()) |
16286 | return NoDiag(); |
16287 | |
16288 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16289 | } |
16290 | case Expr::UnaryOperatorClass: { |
16291 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
16292 | switch (Exp->getOpcode()) { |
16293 | case UO_PostInc: |
16294 | case UO_PostDec: |
16295 | case UO_PreInc: |
16296 | case UO_PreDec: |
16297 | case UO_AddrOf: |
16298 | case UO_Deref: |
16299 | case UO_Coawait: |
16300 | // C99 6.6/3 allows increment and decrement within unevaluated |
16301 | // subexpressions of constant expressions, but they can never be ICEs |
16302 | // because an ICE cannot contain an lvalue operand. |
16303 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16304 | case UO_Extension: |
16305 | case UO_LNot: |
16306 | case UO_Plus: |
16307 | case UO_Minus: |
16308 | case UO_Not: |
16309 | case UO_Real: |
16310 | case UO_Imag: |
16311 | return CheckICE(E: Exp->getSubExpr(), Ctx); |
16312 | } |
16313 | llvm_unreachable("invalid unary operator class" ); |
16314 | } |
16315 | case Expr::OffsetOfExprClass: { |
16316 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
16317 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
16318 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
16319 | // compliance: we should warn earlier for offsetof expressions with |
16320 | // array subscripts that aren't ICEs, and if the array subscripts |
16321 | // are ICEs, the value of the offsetof must be an integer constant. |
16322 | return CheckEvalInICE(E, Ctx); |
16323 | } |
16324 | case Expr::UnaryExprOrTypeTraitExprClass: { |
16325 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
16326 | if ((Exp->getKind() == UETT_SizeOf) && |
16327 | Exp->getTypeOfArgument()->isVariableArrayType()) |
16328 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16329 | return NoDiag(); |
16330 | } |
16331 | case Expr::BinaryOperatorClass: { |
16332 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
16333 | switch (Exp->getOpcode()) { |
16334 | case BO_PtrMemD: |
16335 | case BO_PtrMemI: |
16336 | case BO_Assign: |
16337 | case BO_MulAssign: |
16338 | case BO_DivAssign: |
16339 | case BO_RemAssign: |
16340 | case BO_AddAssign: |
16341 | case BO_SubAssign: |
16342 | case BO_ShlAssign: |
16343 | case BO_ShrAssign: |
16344 | case BO_AndAssign: |
16345 | case BO_XorAssign: |
16346 | case BO_OrAssign: |
16347 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
16348 | // constant expressions, but they can never be ICEs because an ICE cannot |
16349 | // contain an lvalue operand. |
16350 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16351 | |
16352 | case BO_Mul: |
16353 | case BO_Div: |
16354 | case BO_Rem: |
16355 | case BO_Add: |
16356 | case BO_Sub: |
16357 | case BO_Shl: |
16358 | case BO_Shr: |
16359 | case BO_LT: |
16360 | case BO_GT: |
16361 | case BO_LE: |
16362 | case BO_GE: |
16363 | case BO_EQ: |
16364 | case BO_NE: |
16365 | case BO_And: |
16366 | case BO_Xor: |
16367 | case BO_Or: |
16368 | case BO_Comma: |
16369 | case BO_Cmp: { |
16370 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
16371 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
16372 | if (Exp->getOpcode() == BO_Div || |
16373 | Exp->getOpcode() == BO_Rem) { |
16374 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
16375 | // we don't evaluate one. |
16376 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
16377 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
16378 | if (REval == 0) |
16379 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16380 | if (REval.isSigned() && REval.isAllOnes()) { |
16381 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
16382 | if (LEval.isMinSignedValue()) |
16383 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16384 | } |
16385 | } |
16386 | } |
16387 | if (Exp->getOpcode() == BO_Comma) { |
16388 | if (Ctx.getLangOpts().C99) { |
16389 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
16390 | // if it isn't evaluated. |
16391 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
16392 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16393 | } else { |
16394 | // In both C89 and C++, commas in ICEs are illegal. |
16395 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16396 | } |
16397 | } |
16398 | return Worst(A: LHSResult, B: RHSResult); |
16399 | } |
16400 | case BO_LAnd: |
16401 | case BO_LOr: { |
16402 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
16403 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
16404 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
16405 | // Rare case where the RHS has a comma "side-effect"; we need |
16406 | // to actually check the condition to see whether the side |
16407 | // with the comma is evaluated. |
16408 | if ((Exp->getOpcode() == BO_LAnd) != |
16409 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
16410 | return RHSResult; |
16411 | return NoDiag(); |
16412 | } |
16413 | |
16414 | return Worst(A: LHSResult, B: RHSResult); |
16415 | } |
16416 | } |
16417 | llvm_unreachable("invalid binary operator kind" ); |
16418 | } |
16419 | case Expr::ImplicitCastExprClass: |
16420 | case Expr::CStyleCastExprClass: |
16421 | case Expr::CXXFunctionalCastExprClass: |
16422 | case Expr::CXXStaticCastExprClass: |
16423 | case Expr::CXXReinterpretCastExprClass: |
16424 | case Expr::CXXConstCastExprClass: |
16425 | case Expr::ObjCBridgedCastExprClass: { |
16426 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
16427 | if (isa<ExplicitCastExpr>(E)) { |
16428 | if (const FloatingLiteral *FL |
16429 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
16430 | unsigned DestWidth = Ctx.getIntWidth(T: E->getType()); |
16431 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
16432 | APSInt IgnoredVal(DestWidth, !DestSigned); |
16433 | bool Ignored; |
16434 | // If the value does not fit in the destination type, the behavior is |
16435 | // undefined, so we are not required to treat it as a constant |
16436 | // expression. |
16437 | if (FL->getValue().convertToInteger(Result&: IgnoredVal, |
16438 | RM: llvm::APFloat::rmTowardZero, |
16439 | IsExact: &Ignored) & APFloat::opInvalidOp) |
16440 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16441 | return NoDiag(); |
16442 | } |
16443 | } |
16444 | switch (cast<CastExpr>(E)->getCastKind()) { |
16445 | case CK_LValueToRValue: |
16446 | case CK_AtomicToNonAtomic: |
16447 | case CK_NonAtomicToAtomic: |
16448 | case CK_NoOp: |
16449 | case CK_IntegralToBoolean: |
16450 | case CK_IntegralCast: |
16451 | return CheckICE(E: SubExpr, Ctx); |
16452 | default: |
16453 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16454 | } |
16455 | } |
16456 | case Expr::BinaryConditionalOperatorClass: { |
16457 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
16458 | ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx); |
16459 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
16460 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
16461 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
16462 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
16463 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
16464 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
16465 | return FalseResult; |
16466 | } |
16467 | case Expr::ConditionalOperatorClass: { |
16468 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
16469 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
16470 | // then only the true side is actually considered in an integer constant |
16471 | // expression, and it is fully evaluated. This is an important GNU |
16472 | // extension. See GCC PR38377 for discussion. |
16473 | if (const CallExpr *CallCE |
16474 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
16475 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
16476 | return CheckEvalInICE(E, Ctx); |
16477 | ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx); |
16478 | if (CondResult.Kind == IK_NotICE) |
16479 | return CondResult; |
16480 | |
16481 | ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx); |
16482 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
16483 | |
16484 | if (TrueResult.Kind == IK_NotICE) |
16485 | return TrueResult; |
16486 | if (FalseResult.Kind == IK_NotICE) |
16487 | return FalseResult; |
16488 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
16489 | return CondResult; |
16490 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
16491 | return NoDiag(); |
16492 | // Rare case where the diagnostics depend on which side is evaluated |
16493 | // Note that if we get here, CondResult is 0, and at least one of |
16494 | // TrueResult and FalseResult is non-zero. |
16495 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
16496 | return FalseResult; |
16497 | return TrueResult; |
16498 | } |
16499 | case Expr::CXXDefaultArgExprClass: |
16500 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
16501 | case Expr::CXXDefaultInitExprClass: |
16502 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
16503 | case Expr::ChooseExprClass: { |
16504 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
16505 | } |
16506 | case Expr::BuiltinBitCastExprClass: { |
16507 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
16508 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16509 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
16510 | } |
16511 | } |
16512 | |
16513 | llvm_unreachable("Invalid StmtClass!" ); |
16514 | } |
16515 | |
16516 | /// Evaluate an expression as a C++11 integral constant expression. |
16517 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
16518 | const Expr *E, |
16519 | llvm::APSInt *Value, |
16520 | SourceLocation *Loc) { |
16521 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
16522 | if (Loc) *Loc = E->getExprLoc(); |
16523 | return false; |
16524 | } |
16525 | |
16526 | APValue Result; |
16527 | if (!E->isCXX11ConstantExpr(Ctx, Result: &Result, Loc)) |
16528 | return false; |
16529 | |
16530 | if (!Result.isInt()) { |
16531 | if (Loc) *Loc = E->getExprLoc(); |
16532 | return false; |
16533 | } |
16534 | |
16535 | if (Value) *Value = Result.getInt(); |
16536 | return true; |
16537 | } |
16538 | |
16539 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
16540 | SourceLocation *Loc) const { |
16541 | assert(!isValueDependent() && |
16542 | "Expression evaluator can't be called on a dependent expression." ); |
16543 | |
16544 | ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr" ); |
16545 | |
16546 | if (Ctx.getLangOpts().CPlusPlus11) |
16547 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr, Loc); |
16548 | |
16549 | ICEDiag D = CheckICE(E: this, Ctx); |
16550 | if (D.Kind != IK_ICE) { |
16551 | if (Loc) *Loc = D.Loc; |
16552 | return false; |
16553 | } |
16554 | return true; |
16555 | } |
16556 | |
16557 | std::optional<llvm::APSInt> |
16558 | Expr::getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc) const { |
16559 | if (isValueDependent()) { |
16560 | // Expression evaluator can't succeed on a dependent expression. |
16561 | return std::nullopt; |
16562 | } |
16563 | |
16564 | APSInt Value; |
16565 | |
16566 | if (Ctx.getLangOpts().CPlusPlus11) { |
16567 | if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value, Loc)) |
16568 | return Value; |
16569 | return std::nullopt; |
16570 | } |
16571 | |
16572 | if (!isIntegerConstantExpr(Ctx, Loc)) |
16573 | return std::nullopt; |
16574 | |
16575 | // The only possible side-effects here are due to UB discovered in the |
16576 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
16577 | // required to treat the expression as an ICE, so we produce the folded |
16578 | // value. |
16579 | EvalResult ExprResult; |
16580 | Expr::EvalStatus Status; |
16581 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
16582 | Info.InConstantContext = true; |
16583 | |
16584 | if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info)) |
16585 | llvm_unreachable("ICE cannot be evaluated!" ); |
16586 | |
16587 | return ExprResult.Val.getInt(); |
16588 | } |
16589 | |
16590 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
16591 | assert(!isValueDependent() && |
16592 | "Expression evaluator can't be called on a dependent expression." ); |
16593 | |
16594 | return CheckICE(E: this, Ctx).Kind == IK_ICE; |
16595 | } |
16596 | |
16597 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
16598 | SourceLocation *Loc) const { |
16599 | assert(!isValueDependent() && |
16600 | "Expression evaluator can't be called on a dependent expression." ); |
16601 | |
16602 | // We support this checking in C++98 mode in order to diagnose compatibility |
16603 | // issues. |
16604 | assert(Ctx.getLangOpts().CPlusPlus); |
16605 | |
16606 | // Build evaluation settings. |
16607 | Expr::EvalStatus Status; |
16608 | SmallVector<PartialDiagnosticAt, 8> Diags; |
16609 | Status.Diag = &Diags; |
16610 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
16611 | |
16612 | APValue Scratch; |
16613 | bool IsConstExpr = |
16614 | ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) && |
16615 | // FIXME: We don't produce a diagnostic for this, but the callers that |
16616 | // call us on arbitrary full-expressions should generally not care. |
16617 | Info.discardCleanups() && !Status.HasSideEffects; |
16618 | |
16619 | if (!Diags.empty()) { |
16620 | IsConstExpr = false; |
16621 | if (Loc) *Loc = Diags[0].first; |
16622 | } else if (!IsConstExpr) { |
16623 | // FIXME: This shouldn't happen. |
16624 | if (Loc) *Loc = getExprLoc(); |
16625 | } |
16626 | |
16627 | return IsConstExpr; |
16628 | } |
16629 | |
16630 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
16631 | const FunctionDecl *Callee, |
16632 | ArrayRef<const Expr*> Args, |
16633 | const Expr *This) const { |
16634 | assert(!isValueDependent() && |
16635 | "Expression evaluator can't be called on a dependent expression." ); |
16636 | |
16637 | llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution" , [&] { |
16638 | std::string Name; |
16639 | llvm::raw_string_ostream OS(Name); |
16640 | Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(), |
16641 | /*Qualified=*/true); |
16642 | return Name; |
16643 | }); |
16644 | |
16645 | Expr::EvalStatus Status; |
16646 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
16647 | Info.InConstantContext = true; |
16648 | |
16649 | LValue ThisVal; |
16650 | const LValue *ThisPtr = nullptr; |
16651 | if (This) { |
16652 | #ifndef NDEBUG |
16653 | auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
16654 | assert(MD && "Don't provide `this` for non-methods." ); |
16655 | assert(MD->isImplicitObjectMemberFunction() && |
16656 | "Don't provide `this` for methods without an implicit object." ); |
16657 | #endif |
16658 | if (!This->isValueDependent() && |
16659 | EvaluateObjectArgument(Info, Object: This, This&: ThisVal) && |
16660 | !Info.EvalStatus.HasSideEffects) |
16661 | ThisPtr = &ThisVal; |
16662 | |
16663 | // Ignore any side-effects from a failed evaluation. This is safe because |
16664 | // they can't interfere with any other argument evaluation. |
16665 | Info.EvalStatus.HasSideEffects = false; |
16666 | } |
16667 | |
16668 | CallRef Call = Info.CurrentCall->createCall(Callee); |
16669 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
16670 | I != E; ++I) { |
16671 | unsigned Idx = I - Args.begin(); |
16672 | if (Idx >= Callee->getNumParams()) |
16673 | break; |
16674 | const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx); |
16675 | if ((*I)->isValueDependent() || |
16676 | !EvaluateCallArg(PVD, Arg: *I, Call, Info) || |
16677 | Info.EvalStatus.HasSideEffects) { |
16678 | // If evaluation fails, throw away the argument entirely. |
16679 | if (APValue *Slot = Info.getParamSlot(Call, PVD)) |
16680 | *Slot = APValue(); |
16681 | } |
16682 | |
16683 | // Ignore any side-effects from a failed evaluation. This is safe because |
16684 | // they can't interfere with any other argument evaluation. |
16685 | Info.EvalStatus.HasSideEffects = false; |
16686 | } |
16687 | |
16688 | // Parameter cleanups happen in the caller and are not part of this |
16689 | // evaluation. |
16690 | Info.discardCleanups(); |
16691 | Info.EvalStatus.HasSideEffects = false; |
16692 | |
16693 | // Build fake call to Callee. |
16694 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This, |
16695 | Call); |
16696 | // FIXME: Missing ExprWithCleanups in enable_if conditions? |
16697 | FullExpressionRAII Scope(Info); |
16698 | return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() && |
16699 | !Info.EvalStatus.HasSideEffects; |
16700 | } |
16701 | |
16702 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
16703 | SmallVectorImpl< |
16704 | PartialDiagnosticAt> &Diags) { |
16705 | // FIXME: It would be useful to check constexpr function templates, but at the |
16706 | // moment the constant expression evaluator cannot cope with the non-rigorous |
16707 | // ASTs which we build for dependent expressions. |
16708 | if (FD->isDependentContext()) |
16709 | return true; |
16710 | |
16711 | llvm::TimeTraceScope TimeScope("isPotentialConstantExpr" , [&] { |
16712 | std::string Name; |
16713 | llvm::raw_string_ostream OS(Name); |
16714 | FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(), |
16715 | /*Qualified=*/true); |
16716 | return Name; |
16717 | }); |
16718 | |
16719 | Expr::EvalStatus Status; |
16720 | Status.Diag = &Diags; |
16721 | |
16722 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
16723 | Info.InConstantContext = true; |
16724 | Info.CheckingPotentialConstantExpression = true; |
16725 | |
16726 | // The constexpr VM attempts to compile all methods to bytecode here. |
16727 | if (Info.EnableNewConstInterp) { |
16728 | Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FnDecl: FD); |
16729 | return Diags.empty(); |
16730 | } |
16731 | |
16732 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD); |
16733 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
16734 | |
16735 | // Fabricate an arbitrary expression on the stack and pretend that it |
16736 | // is a temporary being used as the 'this' pointer. |
16737 | LValue This; |
16738 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
16739 | This.set({&VIE, Info.CurrentCall->Index}); |
16740 | |
16741 | ArrayRef<const Expr*> Args; |
16742 | |
16743 | APValue Scratch; |
16744 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) { |
16745 | // Evaluate the call as a constant initializer, to allow the construction |
16746 | // of objects of non-literal types. |
16747 | Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch); |
16748 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
16749 | } else { |
16750 | SourceLocation Loc = FD->getLocation(); |
16751 | HandleFunctionCall( |
16752 | Loc, FD, (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr, |
16753 | &VIE, Args, CallRef(), FD->getBody(), Info, Scratch, |
16754 | /*ResultSlot=*/nullptr); |
16755 | } |
16756 | |
16757 | return Diags.empty(); |
16758 | } |
16759 | |
16760 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
16761 | const FunctionDecl *FD, |
16762 | SmallVectorImpl< |
16763 | PartialDiagnosticAt> &Diags) { |
16764 | assert(!E->isValueDependent() && |
16765 | "Expression evaluator can't be called on a dependent expression." ); |
16766 | |
16767 | Expr::EvalStatus Status; |
16768 | Status.Diag = &Diags; |
16769 | |
16770 | EvalInfo Info(FD->getASTContext(), Status, |
16771 | EvalInfo::EM_ConstantExpressionUnevaluated); |
16772 | Info.InConstantContext = true; |
16773 | Info.CheckingPotentialConstantExpression = true; |
16774 | |
16775 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
16776 | CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr, |
16777 | /*CallExpr=*/nullptr, CallRef()); |
16778 | |
16779 | APValue ResultScratch; |
16780 | Evaluate(Result&: ResultScratch, Info, E); |
16781 | return Diags.empty(); |
16782 | } |
16783 | |
16784 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
16785 | unsigned Type) const { |
16786 | if (!getType()->isPointerType()) |
16787 | return false; |
16788 | |
16789 | Expr::EvalStatus Status; |
16790 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
16791 | return tryEvaluateBuiltinObjectSize(E: this, Type, Info, Size&: Result); |
16792 | } |
16793 | |
16794 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
16795 | EvalInfo &Info) { |
16796 | if (!E->getType()->hasPointerRepresentation() || !E->isPRValue()) |
16797 | return false; |
16798 | |
16799 | LValue String; |
16800 | |
16801 | if (!EvaluatePointer(E, Result&: String, Info)) |
16802 | return false; |
16803 | |
16804 | QualType CharTy = E->getType()->getPointeeType(); |
16805 | |
16806 | // Fast path: if it's a string literal, search the string value. |
16807 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
16808 | Val: String.getLValueBase().dyn_cast<const Expr *>())) { |
16809 | StringRef Str = S->getBytes(); |
16810 | int64_t Off = String.Offset.getQuantity(); |
16811 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
16812 | S->getCharByteWidth() == 1 && |
16813 | // FIXME: Add fast-path for wchar_t too. |
16814 | Info.Ctx.hasSameUnqualifiedType(T1: CharTy, T2: Info.Ctx.CharTy)) { |
16815 | Str = Str.substr(Start: Off); |
16816 | |
16817 | StringRef::size_type Pos = Str.find(C: 0); |
16818 | if (Pos != StringRef::npos) |
16819 | Str = Str.substr(Start: 0, N: Pos); |
16820 | |
16821 | Result = Str.size(); |
16822 | return true; |
16823 | } |
16824 | |
16825 | // Fall through to slow path. |
16826 | } |
16827 | |
16828 | // Slow path: scan the bytes of the string looking for the terminating 0. |
16829 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
16830 | APValue Char; |
16831 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) || |
16832 | !Char.isInt()) |
16833 | return false; |
16834 | if (!Char.getInt()) { |
16835 | Result = Strlen; |
16836 | return true; |
16837 | } |
16838 | if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
16839 | return false; |
16840 | } |
16841 | } |
16842 | |
16843 | bool Expr::EvaluateCharRangeAsString(std::string &Result, |
16844 | const Expr *SizeExpression, |
16845 | const Expr *PtrExpression, ASTContext &Ctx, |
16846 | EvalResult &Status) const { |
16847 | LValue String; |
16848 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
16849 | Info.InConstantContext = true; |
16850 | |
16851 | FullExpressionRAII Scope(Info); |
16852 | APSInt SizeValue; |
16853 | if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info)) |
16854 | return false; |
16855 | |
16856 | uint64_t Size = SizeValue.getZExtValue(); |
16857 | |
16858 | if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info)) |
16859 | return false; |
16860 | |
16861 | QualType CharTy = PtrExpression->getType()->getPointeeType(); |
16862 | for (uint64_t I = 0; I < Size; ++I) { |
16863 | APValue Char; |
16864 | if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String, |
16865 | RVal&: Char)) |
16866 | return false; |
16867 | |
16868 | APSInt C = Char.getInt(); |
16869 | Result.push_back(c: static_cast<char>(C.getExtValue())); |
16870 | if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
16871 | return false; |
16872 | } |
16873 | if (!Scope.destroy()) |
16874 | return false; |
16875 | |
16876 | if (!CheckMemoryLeaks(Info)) |
16877 | return false; |
16878 | |
16879 | return true; |
16880 | } |
16881 | |
16882 | bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const { |
16883 | Expr::EvalStatus Status; |
16884 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
16885 | return EvaluateBuiltinStrLen(E: this, Result, Info); |
16886 | } |
16887 | |