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->getSize().getZExtValue(); |
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->getSize().getZExtValue(); |
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) << E->getType() << E->getSourceRange(); |
2782 | return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType()); |
2783 | } |
2784 | return true; |
2785 | } |
2786 | |
2787 | /// Perform the given binary integer operation. |
2788 | static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E, |
2789 | const APSInt &LHS, BinaryOperatorKind Opcode, |
2790 | APSInt RHS, APSInt &Result) { |
2791 | bool HandleOverflowResult = true; |
2792 | switch (Opcode) { |
2793 | default: |
2794 | Info.FFDiag(E); |
2795 | return false; |
2796 | case BO_Mul: |
2797 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() * 2, |
2798 | std::multiplies<APSInt>(), Result); |
2799 | case BO_Add: |
2800 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2801 | std::plus<APSInt>(), Result); |
2802 | case BO_Sub: |
2803 | return CheckedIntArithmetic(Info, E, LHS, RHS, LHS.getBitWidth() + 1, |
2804 | std::minus<APSInt>(), Result); |
2805 | case BO_And: Result = LHS & RHS; return true; |
2806 | case BO_Xor: Result = LHS ^ RHS; return true; |
2807 | case BO_Or: Result = LHS | RHS; return true; |
2808 | case BO_Div: |
2809 | case BO_Rem: |
2810 | if (RHS == 0) { |
2811 | Info.FFDiag(E, diag::note_expr_divide_by_zero) |
2812 | << E->getRHS()->getSourceRange(); |
2813 | return false; |
2814 | } |
2815 | // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports |
2816 | // this operation and gives the two's complement result. |
2817 | if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() && |
2818 | LHS.isMinSignedValue()) |
2819 | HandleOverflowResult = HandleOverflow( |
2820 | Info, E, -LHS.extend(width: LHS.getBitWidth() + 1), E->getType()); |
2821 | Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS); |
2822 | return HandleOverflowResult; |
2823 | case BO_Shl: { |
2824 | if (Info.getLangOpts().OpenCL) |
2825 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
2826 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2827 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2828 | RHS.isUnsigned()); |
2829 | else if (RHS.isSigned() && RHS.isNegative()) { |
2830 | // During constant-folding, a negative shift is an opposite shift. Such |
2831 | // a shift is not a constant expression. |
2832 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2833 | RHS = -RHS; |
2834 | goto shift_right; |
2835 | } |
2836 | shift_left: |
2837 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of |
2838 | // the shifted type. |
2839 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
2840 | if (SA != RHS) { |
2841 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2842 | << RHS << E->getType() << LHS.getBitWidth(); |
2843 | } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) { |
2844 | // C++11 [expr.shift]p2: A signed left shift must have a non-negative |
2845 | // operand, and must not overflow the corresponding unsigned type. |
2846 | // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to |
2847 | // E1 x 2^E2 module 2^N. |
2848 | if (LHS.isNegative()) |
2849 | Info.CCEDiag(E, diag::note_constexpr_lshift_of_negative) << LHS; |
2850 | else if (LHS.countl_zero() < SA) |
2851 | Info.CCEDiag(E, diag::note_constexpr_lshift_discards); |
2852 | } |
2853 | Result = LHS << SA; |
2854 | return true; |
2855 | } |
2856 | case BO_Shr: { |
2857 | if (Info.getLangOpts().OpenCL) |
2858 | // OpenCL 6.3j: shift values are effectively % word size of LHS. |
2859 | RHS &= APSInt(llvm::APInt(RHS.getBitWidth(), |
2860 | static_cast<uint64_t>(LHS.getBitWidth() - 1)), |
2861 | RHS.isUnsigned()); |
2862 | else if (RHS.isSigned() && RHS.isNegative()) { |
2863 | // During constant-folding, a negative shift is an opposite shift. Such a |
2864 | // shift is not a constant expression. |
2865 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHS; |
2866 | RHS = -RHS; |
2867 | goto shift_left; |
2868 | } |
2869 | shift_right: |
2870 | // C++11 [expr.shift]p1: Shift width must be less than the bit width of the |
2871 | // shifted type. |
2872 | unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1); |
2873 | if (SA != RHS) |
2874 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
2875 | << RHS << E->getType() << LHS.getBitWidth(); |
2876 | Result = LHS >> SA; |
2877 | return true; |
2878 | } |
2879 | |
2880 | case BO_LT: Result = LHS < RHS; return true; |
2881 | case BO_GT: Result = LHS > RHS; return true; |
2882 | case BO_LE: Result = LHS <= RHS; return true; |
2883 | case BO_GE: Result = LHS >= RHS; return true; |
2884 | case BO_EQ: Result = LHS == RHS; return true; |
2885 | case BO_NE: Result = LHS != RHS; return true; |
2886 | case BO_Cmp: |
2887 | llvm_unreachable("BO_Cmp should be handled elsewhere" ); |
2888 | } |
2889 | } |
2890 | |
2891 | /// Perform the given binary floating-point operation, in-place, on LHS. |
2892 | static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E, |
2893 | APFloat &LHS, BinaryOperatorKind Opcode, |
2894 | const APFloat &RHS) { |
2895 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
2896 | APFloat::opStatus St; |
2897 | switch (Opcode) { |
2898 | default: |
2899 | Info.FFDiag(E); |
2900 | return false; |
2901 | case BO_Mul: |
2902 | St = LHS.multiply(RHS, RM); |
2903 | break; |
2904 | case BO_Add: |
2905 | St = LHS.add(RHS, RM); |
2906 | break; |
2907 | case BO_Sub: |
2908 | St = LHS.subtract(RHS, RM); |
2909 | break; |
2910 | case BO_Div: |
2911 | // [expr.mul]p4: |
2912 | // If the second operand of / or % is zero the behavior is undefined. |
2913 | if (RHS.isZero()) |
2914 | Info.CCEDiag(E, diag::note_expr_divide_by_zero); |
2915 | St = LHS.divide(RHS, RM); |
2916 | break; |
2917 | } |
2918 | |
2919 | // [expr.pre]p4: |
2920 | // If during the evaluation of an expression, the result is not |
2921 | // mathematically defined [...], the behavior is undefined. |
2922 | // FIXME: C++ rules require us to not conform to IEEE 754 here. |
2923 | if (LHS.isNaN()) { |
2924 | Info.CCEDiag(E, diag::note_constexpr_float_arithmetic) << LHS.isNaN(); |
2925 | return Info.noteUndefinedBehavior(); |
2926 | } |
2927 | |
2928 | return checkFloatingPointResult(Info, E, St); |
2929 | } |
2930 | |
2931 | static bool handleLogicalOpForVector(const APInt &LHSValue, |
2932 | BinaryOperatorKind Opcode, |
2933 | const APInt &RHSValue, APInt &Result) { |
2934 | bool LHS = (LHSValue != 0); |
2935 | bool RHS = (RHSValue != 0); |
2936 | |
2937 | if (Opcode == BO_LAnd) |
2938 | Result = LHS && RHS; |
2939 | else |
2940 | Result = LHS || RHS; |
2941 | return true; |
2942 | } |
2943 | static bool handleLogicalOpForVector(const APFloat &LHSValue, |
2944 | BinaryOperatorKind Opcode, |
2945 | const APFloat &RHSValue, APInt &Result) { |
2946 | bool LHS = !LHSValue.isZero(); |
2947 | bool RHS = !RHSValue.isZero(); |
2948 | |
2949 | if (Opcode == BO_LAnd) |
2950 | Result = LHS && RHS; |
2951 | else |
2952 | Result = LHS || RHS; |
2953 | return true; |
2954 | } |
2955 | |
2956 | static bool handleLogicalOpForVector(const APValue &LHSValue, |
2957 | BinaryOperatorKind Opcode, |
2958 | const APValue &RHSValue, APInt &Result) { |
2959 | // The result is always an int type, however operands match the first. |
2960 | if (LHSValue.getKind() == APValue::Int) |
2961 | return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode, |
2962 | RHSValue: RHSValue.getInt(), Result); |
2963 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options" ); |
2964 | return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode, |
2965 | RHSValue: RHSValue.getFloat(), Result); |
2966 | } |
2967 | |
2968 | template <typename APTy> |
2969 | static bool |
2970 | handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode, |
2971 | const APTy &RHSValue, APInt &Result) { |
2972 | switch (Opcode) { |
2973 | default: |
2974 | llvm_unreachable("unsupported binary operator" ); |
2975 | case BO_EQ: |
2976 | Result = (LHSValue == RHSValue); |
2977 | break; |
2978 | case BO_NE: |
2979 | Result = (LHSValue != RHSValue); |
2980 | break; |
2981 | case BO_LT: |
2982 | Result = (LHSValue < RHSValue); |
2983 | break; |
2984 | case BO_GT: |
2985 | Result = (LHSValue > RHSValue); |
2986 | break; |
2987 | case BO_LE: |
2988 | Result = (LHSValue <= RHSValue); |
2989 | break; |
2990 | case BO_GE: |
2991 | Result = (LHSValue >= RHSValue); |
2992 | break; |
2993 | } |
2994 | |
2995 | // The boolean operations on these vector types use an instruction that |
2996 | // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1 |
2997 | // to -1 to make sure that we produce the correct value. |
2998 | Result.negate(); |
2999 | |
3000 | return true; |
3001 | } |
3002 | |
3003 | static bool handleCompareOpForVector(const APValue &LHSValue, |
3004 | BinaryOperatorKind Opcode, |
3005 | const APValue &RHSValue, APInt &Result) { |
3006 | // The result is always an int type, however operands match the first. |
3007 | if (LHSValue.getKind() == APValue::Int) |
3008 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode, |
3009 | RHSValue: RHSValue.getInt(), Result); |
3010 | assert(LHSValue.getKind() == APValue::Float && "Should be no other options" ); |
3011 | return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode, |
3012 | RHSValue: RHSValue.getFloat(), Result); |
3013 | } |
3014 | |
3015 | // Perform binary operations for vector types, in place on the LHS. |
3016 | static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E, |
3017 | BinaryOperatorKind Opcode, |
3018 | APValue &LHSValue, |
3019 | const APValue &RHSValue) { |
3020 | assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI && |
3021 | "Operation not supported on vector types" ); |
3022 | |
3023 | const auto *VT = E->getType()->castAs<VectorType>(); |
3024 | unsigned NumElements = VT->getNumElements(); |
3025 | QualType EltTy = VT->getElementType(); |
3026 | |
3027 | // In the cases (typically C as I've observed) where we aren't evaluating |
3028 | // constexpr but are checking for cases where the LHS isn't yet evaluatable, |
3029 | // just give up. |
3030 | if (!LHSValue.isVector()) { |
3031 | assert(LHSValue.isLValue() && |
3032 | "A vector result that isn't a vector OR uncalculated LValue" ); |
3033 | Info.FFDiag(E); |
3034 | return false; |
3035 | } |
3036 | |
3037 | assert(LHSValue.getVectorLength() == NumElements && |
3038 | RHSValue.getVectorLength() == NumElements && "Different vector sizes" ); |
3039 | |
3040 | SmallVector<APValue, 4> ResultElements; |
3041 | |
3042 | for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) { |
3043 | APValue LHSElt = LHSValue.getVectorElt(I: EltNum); |
3044 | APValue RHSElt = RHSValue.getVectorElt(I: EltNum); |
3045 | |
3046 | if (EltTy->isIntegerType()) { |
3047 | APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy), |
3048 | EltTy->isUnsignedIntegerType()}; |
3049 | bool Success = true; |
3050 | |
3051 | if (BinaryOperator::isLogicalOp(Opc: Opcode)) |
3052 | Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3053 | else if (BinaryOperator::isComparisonOp(Opc: Opcode)) |
3054 | Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult); |
3055 | else |
3056 | Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode, |
3057 | RHS: RHSElt.getInt(), Result&: EltResult); |
3058 | |
3059 | if (!Success) { |
3060 | Info.FFDiag(E); |
3061 | return false; |
3062 | } |
3063 | ResultElements.emplace_back(Args&: EltResult); |
3064 | |
3065 | } else if (EltTy->isFloatingType()) { |
3066 | assert(LHSElt.getKind() == APValue::Float && |
3067 | RHSElt.getKind() == APValue::Float && |
3068 | "Mismatched LHS/RHS/Result Type" ); |
3069 | APFloat LHSFloat = LHSElt.getFloat(); |
3070 | |
3071 | if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode, |
3072 | RHS: RHSElt.getFloat())) { |
3073 | Info.FFDiag(E); |
3074 | return false; |
3075 | } |
3076 | |
3077 | ResultElements.emplace_back(Args&: LHSFloat); |
3078 | } |
3079 | } |
3080 | |
3081 | LHSValue = APValue(ResultElements.data(), ResultElements.size()); |
3082 | return true; |
3083 | } |
3084 | |
3085 | /// Cast an lvalue referring to a base subobject to a derived class, by |
3086 | /// truncating the lvalue's path to the given length. |
3087 | static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3088 | const RecordDecl *TruncatedType, |
3089 | unsigned TruncatedElements) { |
3090 | SubobjectDesignator &D = Result.Designator; |
3091 | |
3092 | // Check we actually point to a derived class object. |
3093 | if (TruncatedElements == D.Entries.size()) |
3094 | return true; |
3095 | assert(TruncatedElements >= D.MostDerivedPathLength && |
3096 | "not casting to a derived class" ); |
3097 | if (!Result.checkSubobject(Info, E, CSK: CSK_Derived)) |
3098 | return false; |
3099 | |
3100 | // Truncate the path to the subobject, and remove any derived-to-base offsets. |
3101 | const RecordDecl *RD = TruncatedType; |
3102 | for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) { |
3103 | if (RD->isInvalidDecl()) return false; |
3104 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
3105 | const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]); |
3106 | if (isVirtualBaseClass(E: D.Entries[I])) |
3107 | Result.Offset -= Layout.getVBaseClassOffset(VBase: Base); |
3108 | else |
3109 | Result.Offset -= Layout.getBaseClassOffset(Base); |
3110 | RD = Base; |
3111 | } |
3112 | D.Entries.resize(N: TruncatedElements); |
3113 | return true; |
3114 | } |
3115 | |
3116 | static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3117 | const CXXRecordDecl *Derived, |
3118 | const CXXRecordDecl *Base, |
3119 | const ASTRecordLayout *RL = nullptr) { |
3120 | if (!RL) { |
3121 | if (Derived->isInvalidDecl()) return false; |
3122 | RL = &Info.Ctx.getASTRecordLayout(Derived); |
3123 | } |
3124 | |
3125 | Obj.getLValueOffset() += RL->getBaseClassOffset(Base); |
3126 | Obj.addDecl(Info, E, Base, /*Virtual*/ false); |
3127 | return true; |
3128 | } |
3129 | |
3130 | static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj, |
3131 | const CXXRecordDecl *DerivedDecl, |
3132 | const CXXBaseSpecifier *Base) { |
3133 | const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl(); |
3134 | |
3135 | if (!Base->isVirtual()) |
3136 | return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl); |
3137 | |
3138 | SubobjectDesignator &D = Obj.Designator; |
3139 | if (D.Invalid) |
3140 | return false; |
3141 | |
3142 | // Extract most-derived object and corresponding type. |
3143 | DerivedDecl = D.MostDerivedType->getAsCXXRecordDecl(); |
3144 | if (!CastToDerivedClass(Info, E, Obj, DerivedDecl, D.MostDerivedPathLength)) |
3145 | return false; |
3146 | |
3147 | // Find the virtual base class. |
3148 | if (DerivedDecl->isInvalidDecl()) return false; |
3149 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(DerivedDecl); |
3150 | Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl); |
3151 | Obj.addDecl(Info, E, BaseDecl, /*Virtual*/ true); |
3152 | return true; |
3153 | } |
3154 | |
3155 | static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E, |
3156 | QualType Type, LValue &Result) { |
3157 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
3158 | PathE = E->path_end(); |
3159 | PathI != PathE; ++PathI) { |
3160 | if (!HandleLValueBase(Info, E, Result, Type->getAsCXXRecordDecl(), |
3161 | *PathI)) |
3162 | return false; |
3163 | Type = (*PathI)->getType(); |
3164 | } |
3165 | return true; |
3166 | } |
3167 | |
3168 | /// Cast an lvalue referring to a derived class to a known base subobject. |
3169 | static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result, |
3170 | const CXXRecordDecl *DerivedRD, |
3171 | const CXXRecordDecl *BaseRD) { |
3172 | CXXBasePaths Paths(/*FindAmbiguities=*/false, |
3173 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
3174 | if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths)) |
3175 | llvm_unreachable("Class must be derived from the passed in base class!" ); |
3176 | |
3177 | for (CXXBasePathElement &Elem : Paths.front()) |
3178 | if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base)) |
3179 | return false; |
3180 | return true; |
3181 | } |
3182 | |
3183 | /// Update LVal to refer to the given field, which must be a member of the type |
3184 | /// currently described by LVal. |
3185 | static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal, |
3186 | const FieldDecl *FD, |
3187 | const ASTRecordLayout *RL = nullptr) { |
3188 | if (!RL) { |
3189 | if (FD->getParent()->isInvalidDecl()) return false; |
3190 | RL = &Info.Ctx.getASTRecordLayout(D: FD->getParent()); |
3191 | } |
3192 | |
3193 | unsigned I = FD->getFieldIndex(); |
3194 | LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I))); |
3195 | LVal.addDecl(Info, E, FD); |
3196 | return true; |
3197 | } |
3198 | |
3199 | /// Update LVal to refer to the given indirect field. |
3200 | static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E, |
3201 | LValue &LVal, |
3202 | const IndirectFieldDecl *IFD) { |
3203 | for (const auto *C : IFD->chain()) |
3204 | if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C))) |
3205 | return false; |
3206 | return true; |
3207 | } |
3208 | |
3209 | enum class SizeOfType { |
3210 | SizeOf, |
3211 | DataSizeOf, |
3212 | }; |
3213 | |
3214 | /// Get the size of the given type in char units. |
3215 | static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type, |
3216 | CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) { |
3217 | // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc |
3218 | // extension. |
3219 | if (Type->isVoidType() || Type->isFunctionType()) { |
3220 | Size = CharUnits::One(); |
3221 | return true; |
3222 | } |
3223 | |
3224 | if (Type->isDependentType()) { |
3225 | Info.FFDiag(Loc); |
3226 | return false; |
3227 | } |
3228 | |
3229 | if (!Type->isConstantSizeType()) { |
3230 | // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2. |
3231 | // FIXME: Better diagnostic. |
3232 | Info.FFDiag(Loc); |
3233 | return false; |
3234 | } |
3235 | |
3236 | if (SOT == SizeOfType::SizeOf) |
3237 | Size = Info.Ctx.getTypeSizeInChars(T: Type); |
3238 | else |
3239 | Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width; |
3240 | return true; |
3241 | } |
3242 | |
3243 | /// Update a pointer value to model pointer arithmetic. |
3244 | /// \param Info - Information about the ongoing evaluation. |
3245 | /// \param E - The expression being evaluated, for diagnostic purposes. |
3246 | /// \param LVal - The pointer value to be updated. |
3247 | /// \param EltTy - The pointee type represented by LVal. |
3248 | /// \param Adjustment - The adjustment, in objects of type EltTy, to add. |
3249 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3250 | LValue &LVal, QualType EltTy, |
3251 | APSInt Adjustment) { |
3252 | CharUnits SizeOfPointee; |
3253 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee)) |
3254 | return false; |
3255 | |
3256 | LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee); |
3257 | return true; |
3258 | } |
3259 | |
3260 | static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E, |
3261 | LValue &LVal, QualType EltTy, |
3262 | int64_t Adjustment) { |
3263 | return HandleLValueArrayAdjustment(Info, E, LVal, EltTy, |
3264 | Adjustment: APSInt::get(X: Adjustment)); |
3265 | } |
3266 | |
3267 | /// Update an lvalue to refer to a component of a complex number. |
3268 | /// \param Info - Information about the ongoing evaluation. |
3269 | /// \param LVal - The lvalue to be updated. |
3270 | /// \param EltTy - The complex number's component type. |
3271 | /// \param Imag - False for the real component, true for the imaginary. |
3272 | static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E, |
3273 | LValue &LVal, QualType EltTy, |
3274 | bool Imag) { |
3275 | if (Imag) { |
3276 | CharUnits SizeOfComponent; |
3277 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent)) |
3278 | return false; |
3279 | LVal.Offset += SizeOfComponent; |
3280 | } |
3281 | LVal.addComplex(Info, E, EltTy, Imag); |
3282 | return true; |
3283 | } |
3284 | |
3285 | /// Try to evaluate the initializer for a variable declaration. |
3286 | /// |
3287 | /// \param Info Information about the ongoing evaluation. |
3288 | /// \param E An expression to be used when printing diagnostics. |
3289 | /// \param VD The variable whose initializer should be obtained. |
3290 | /// \param Version The version of the variable within the frame. |
3291 | /// \param Frame The frame in which the variable was created. Must be null |
3292 | /// if this variable is not local to the evaluation. |
3293 | /// \param Result Filled in with a pointer to the value of the variable. |
3294 | static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E, |
3295 | const VarDecl *VD, CallStackFrame *Frame, |
3296 | unsigned Version, APValue *&Result) { |
3297 | APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version); |
3298 | |
3299 | // If this is a local variable, dig out its value. |
3300 | if (Frame) { |
3301 | Result = Frame->getTemporary(Key: VD, Version); |
3302 | if (Result) |
3303 | return true; |
3304 | |
3305 | if (!isa<ParmVarDecl>(Val: VD)) { |
3306 | // Assume variables referenced within a lambda's call operator that were |
3307 | // not declared within the call operator are captures and during checking |
3308 | // of a potential constant expression, assume they are unknown constant |
3309 | // expressions. |
3310 | assert(isLambdaCallOperator(Frame->Callee) && |
3311 | (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) && |
3312 | "missing value for local variable" ); |
3313 | if (Info.checkingPotentialConstantExpression()) |
3314 | return false; |
3315 | // FIXME: This diagnostic is bogus; we do support captures. Is this code |
3316 | // still reachable at all? |
3317 | Info.FFDiag(E->getBeginLoc(), |
3318 | diag::note_unimplemented_constexpr_lambda_feature_ast) |
3319 | << "captures not currently allowed" ; |
3320 | return false; |
3321 | } |
3322 | } |
3323 | |
3324 | // If we're currently evaluating the initializer of this declaration, use that |
3325 | // in-flight value. |
3326 | if (Info.EvaluatingDecl == Base) { |
3327 | Result = Info.EvaluatingDeclValue; |
3328 | return true; |
3329 | } |
3330 | |
3331 | if (isa<ParmVarDecl>(Val: VD)) { |
3332 | // Assume parameters of a potential constant expression are usable in |
3333 | // constant expressions. |
3334 | if (!Info.checkingPotentialConstantExpression() || |
3335 | !Info.CurrentCall->Callee || |
3336 | !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) { |
3337 | if (Info.getLangOpts().CPlusPlus11) { |
3338 | Info.FFDiag(E, diag::note_constexpr_function_param_value_unknown) |
3339 | << VD; |
3340 | NoteLValueLocation(Info, Base); |
3341 | } else { |
3342 | Info.FFDiag(E); |
3343 | } |
3344 | } |
3345 | return false; |
3346 | } |
3347 | |
3348 | if (E->isValueDependent()) |
3349 | return false; |
3350 | |
3351 | // Dig out the initializer, and use the declaration which it's attached to. |
3352 | // FIXME: We should eventually check whether the variable has a reachable |
3353 | // initializing declaration. |
3354 | const Expr *Init = VD->getAnyInitializer(D&: VD); |
3355 | if (!Init) { |
3356 | // Don't diagnose during potential constant expression checking; an |
3357 | // initializer might be added later. |
3358 | if (!Info.checkingPotentialConstantExpression()) { |
3359 | Info.FFDiag(E, diag::note_constexpr_var_init_unknown, 1) |
3360 | << VD; |
3361 | NoteLValueLocation(Info, Base); |
3362 | } |
3363 | return false; |
3364 | } |
3365 | |
3366 | if (Init->isValueDependent()) { |
3367 | // The DeclRefExpr is not value-dependent, but the variable it refers to |
3368 | // has a value-dependent initializer. This should only happen in |
3369 | // constant-folding cases, where the variable is not actually of a suitable |
3370 | // type for use in a constant expression (otherwise the DeclRefExpr would |
3371 | // have been value-dependent too), so diagnose that. |
3372 | assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx)); |
3373 | if (!Info.checkingPotentialConstantExpression()) { |
3374 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
3375 | ? diag::note_constexpr_ltor_non_constexpr |
3376 | : diag::note_constexpr_ltor_non_integral, 1) |
3377 | << VD << VD->getType(); |
3378 | NoteLValueLocation(Info, Base); |
3379 | } |
3380 | return false; |
3381 | } |
3382 | |
3383 | // Check that we can fold the initializer. In C++, we will have already done |
3384 | // this in the cases where it matters for conformance. |
3385 | if (!VD->evaluateValue()) { |
3386 | Info.FFDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3387 | NoteLValueLocation(Info, Base); |
3388 | return false; |
3389 | } |
3390 | |
3391 | // Check that the variable is actually usable in constant expressions. For a |
3392 | // const integral variable or a reference, we might have a non-constant |
3393 | // initializer that we can nonetheless evaluate the initializer for. Such |
3394 | // variables are not usable in constant expressions. In C++98, the |
3395 | // initializer also syntactically needs to be an ICE. |
3396 | // |
3397 | // FIXME: We don't diagnose cases that aren't potentially usable in constant |
3398 | // expressions here; doing so would regress diagnostics for things like |
3399 | // reading from a volatile constexpr variable. |
3400 | if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() && |
3401 | VD->mightBeUsableInConstantExpressions(C: Info.Ctx)) || |
3402 | ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) && |
3403 | !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) { |
3404 | Info.CCEDiag(E, diag::note_constexpr_var_init_non_constant, 1) << VD; |
3405 | NoteLValueLocation(Info, Base); |
3406 | } |
3407 | |
3408 | // Never use the initializer of a weak variable, not even for constant |
3409 | // folding. We can't be sure that this is the definition that will be used. |
3410 | if (VD->isWeak()) { |
3411 | Info.FFDiag(E, diag::note_constexpr_var_init_weak) << VD; |
3412 | NoteLValueLocation(Info, Base); |
3413 | return false; |
3414 | } |
3415 | |
3416 | Result = VD->getEvaluatedValue(); |
3417 | return true; |
3418 | } |
3419 | |
3420 | /// Get the base index of the given base class within an APValue representing |
3421 | /// the given derived class. |
3422 | static unsigned getBaseIndex(const CXXRecordDecl *Derived, |
3423 | const CXXRecordDecl *Base) { |
3424 | Base = Base->getCanonicalDecl(); |
3425 | unsigned Index = 0; |
3426 | for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(), |
3427 | E = Derived->bases_end(); I != E; ++I, ++Index) { |
3428 | if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base) |
3429 | return Index; |
3430 | } |
3431 | |
3432 | llvm_unreachable("base class missing from derived class's bases list" ); |
3433 | } |
3434 | |
3435 | /// Extract the value of a character from a string literal. |
3436 | static APSInt (EvalInfo &Info, const Expr *Lit, |
3437 | uint64_t Index) { |
3438 | assert(!isa<SourceLocExpr>(Lit) && |
3439 | "SourceLocExpr should have already been converted to a StringLiteral" ); |
3440 | |
3441 | // FIXME: Support MakeStringConstant |
3442 | if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) { |
3443 | std::string Str; |
3444 | Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str); |
3445 | assert(Index <= Str.size() && "Index too large" ); |
3446 | return APSInt::getUnsigned(X: Str.c_str()[Index]); |
3447 | } |
3448 | |
3449 | if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit)) |
3450 | Lit = PE->getFunctionName(); |
3451 | const StringLiteral *S = cast<StringLiteral>(Val: Lit); |
3452 | const ConstantArrayType *CAT = |
3453 | Info.Ctx.getAsConstantArrayType(T: S->getType()); |
3454 | assert(CAT && "string literal isn't an array" ); |
3455 | QualType CharType = CAT->getElementType(); |
3456 | assert(CharType->isIntegerType() && "unexpected character type" ); |
3457 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3458 | CharType->isUnsignedIntegerType()); |
3459 | if (Index < S->getLength()) |
3460 | Value = S->getCodeUnit(i: Index); |
3461 | return Value; |
3462 | } |
3463 | |
3464 | // Expand a string literal into an array of characters. |
3465 | // |
3466 | // FIXME: This is inefficient; we should probably introduce something similar |
3467 | // to the LLVM ConstantDataArray to make this cheaper. |
3468 | static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S, |
3469 | APValue &Result, |
3470 | QualType AllocType = QualType()) { |
3471 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
3472 | T: AllocType.isNull() ? S->getType() : AllocType); |
3473 | assert(CAT && "string literal isn't an array" ); |
3474 | QualType CharType = CAT->getElementType(); |
3475 | assert(CharType->isIntegerType() && "unexpected character type" ); |
3476 | |
3477 | unsigned Elts = CAT->getSize().getZExtValue(); |
3478 | Result = APValue(APValue::UninitArray(), |
3479 | std::min(a: S->getLength(), b: Elts), Elts); |
3480 | APSInt Value(Info.Ctx.getTypeSize(T: CharType), |
3481 | CharType->isUnsignedIntegerType()); |
3482 | if (Result.hasArrayFiller()) |
3483 | Result.getArrayFiller() = APValue(Value); |
3484 | for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) { |
3485 | Value = S->getCodeUnit(i: I); |
3486 | Result.getArrayInitializedElt(I) = APValue(Value); |
3487 | } |
3488 | } |
3489 | |
3490 | // Expand an array so that it has more than Index filled elements. |
3491 | static void expandArray(APValue &Array, unsigned Index) { |
3492 | unsigned Size = Array.getArraySize(); |
3493 | assert(Index < Size); |
3494 | |
3495 | // Always at least double the number of elements for which we store a value. |
3496 | unsigned OldElts = Array.getArrayInitializedElts(); |
3497 | unsigned NewElts = std::max(a: Index+1, b: OldElts * 2); |
3498 | NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u)); |
3499 | |
3500 | // Copy the data across. |
3501 | APValue NewValue(APValue::UninitArray(), NewElts, Size); |
3502 | for (unsigned I = 0; I != OldElts; ++I) |
3503 | NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I)); |
3504 | for (unsigned I = OldElts; I != NewElts; ++I) |
3505 | NewValue.getArrayInitializedElt(I) = Array.getArrayFiller(); |
3506 | if (NewValue.hasArrayFiller()) |
3507 | NewValue.getArrayFiller() = Array.getArrayFiller(); |
3508 | Array.swap(RHS&: NewValue); |
3509 | } |
3510 | |
3511 | /// Determine whether a type would actually be read by an lvalue-to-rvalue |
3512 | /// conversion. If it's of class type, we may assume that the copy operation |
3513 | /// is trivial. Note that this is never true for a union type with fields |
3514 | /// (because the copy always "reads" the active member) and always true for |
3515 | /// a non-class type. |
3516 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD); |
3517 | static bool isReadByLvalueToRvalueConversion(QualType T) { |
3518 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3519 | return !RD || isReadByLvalueToRvalueConversion(RD); |
3520 | } |
3521 | static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) { |
3522 | // FIXME: A trivial copy of a union copies the object representation, even if |
3523 | // the union is empty. |
3524 | if (RD->isUnion()) |
3525 | return !RD->field_empty(); |
3526 | if (RD->isEmpty()) |
3527 | return false; |
3528 | |
3529 | for (auto *Field : RD->fields()) |
3530 | if (!Field->isUnnamedBitfield() && |
3531 | isReadByLvalueToRvalueConversion(Field->getType())) |
3532 | return true; |
3533 | |
3534 | for (auto &BaseSpec : RD->bases()) |
3535 | if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType())) |
3536 | return true; |
3537 | |
3538 | return false; |
3539 | } |
3540 | |
3541 | /// Diagnose an attempt to read from any unreadable field within the specified |
3542 | /// type, which might be a class type. |
3543 | static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK, |
3544 | QualType T) { |
3545 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
3546 | if (!RD) |
3547 | return false; |
3548 | |
3549 | if (!RD->hasMutableFields()) |
3550 | return false; |
3551 | |
3552 | for (auto *Field : RD->fields()) { |
3553 | // If we're actually going to read this field in some way, then it can't |
3554 | // be mutable. If we're in a union, then assigning to a mutable field |
3555 | // (even an empty one) can change the active member, so that's not OK. |
3556 | // FIXME: Add core issue number for the union case. |
3557 | if (Field->isMutable() && |
3558 | (RD->isUnion() || isReadByLvalueToRvalueConversion(Field->getType()))) { |
3559 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) << AK << Field; |
3560 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3561 | return true; |
3562 | } |
3563 | |
3564 | if (diagnoseMutableFields(Info, E, AK, Field->getType())) |
3565 | return true; |
3566 | } |
3567 | |
3568 | for (auto &BaseSpec : RD->bases()) |
3569 | if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType())) |
3570 | return true; |
3571 | |
3572 | // All mutable fields were empty, and thus not actually read. |
3573 | return false; |
3574 | } |
3575 | |
3576 | static bool lifetimeStartedInEvaluation(EvalInfo &Info, |
3577 | APValue::LValueBase Base, |
3578 | bool MutableSubobject = false) { |
3579 | // A temporary or transient heap allocation we created. |
3580 | if (Base.getCallIndex() || Base.is<DynamicAllocLValue>()) |
3581 | return true; |
3582 | |
3583 | switch (Info.IsEvaluatingDecl) { |
3584 | case EvalInfo::EvaluatingDeclKind::None: |
3585 | return false; |
3586 | |
3587 | case EvalInfo::EvaluatingDeclKind::Ctor: |
3588 | // The variable whose initializer we're evaluating. |
3589 | if (Info.EvaluatingDecl == Base) |
3590 | return true; |
3591 | |
3592 | // A temporary lifetime-extended by the variable whose initializer we're |
3593 | // evaluating. |
3594 | if (auto *BaseE = Base.dyn_cast<const Expr *>()) |
3595 | if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE)) |
3596 | return Info.EvaluatingDecl == BaseMTE->getExtendingDecl(); |
3597 | return false; |
3598 | |
3599 | case EvalInfo::EvaluatingDeclKind::Dtor: |
3600 | // C++2a [expr.const]p6: |
3601 | // [during constant destruction] the lifetime of a and its non-mutable |
3602 | // subobjects (but not its mutable subobjects) [are] considered to start |
3603 | // within e. |
3604 | if (MutableSubobject || Base != Info.EvaluatingDecl) |
3605 | return false; |
3606 | // FIXME: We can meaningfully extend this to cover non-const objects, but |
3607 | // we will need special handling: we should be able to access only |
3608 | // subobjects of such objects that are themselves declared const. |
3609 | QualType T = getType(B: Base); |
3610 | return T.isConstQualified() || T->isReferenceType(); |
3611 | } |
3612 | |
3613 | llvm_unreachable("unknown evaluating decl kind" ); |
3614 | } |
3615 | |
3616 | static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT, |
3617 | SourceLocation CallLoc = {}) { |
3618 | return Info.CheckArraySize( |
3619 | Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc, |
3620 | BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getSize().getZExtValue(), |
3621 | /*Diag=*/true); |
3622 | } |
3623 | |
3624 | namespace { |
3625 | /// A handle to a complete object (an object that is not a subobject of |
3626 | /// another object). |
3627 | struct CompleteObject { |
3628 | /// The identity of the object. |
3629 | APValue::LValueBase Base; |
3630 | /// The value of the complete object. |
3631 | APValue *Value; |
3632 | /// The type of the complete object. |
3633 | QualType Type; |
3634 | |
3635 | CompleteObject() : Value(nullptr) {} |
3636 | CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type) |
3637 | : Base(Base), Value(Value), Type(Type) {} |
3638 | |
3639 | bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const { |
3640 | // If this isn't a "real" access (eg, if it's just accessing the type |
3641 | // info), allow it. We assume the type doesn't change dynamically for |
3642 | // subobjects of constexpr objects (even though we'd hit UB here if it |
3643 | // did). FIXME: Is this right? |
3644 | if (!isAnyAccess(AK)) |
3645 | return true; |
3646 | |
3647 | // In C++14 onwards, it is permitted to read a mutable member whose |
3648 | // lifetime began within the evaluation. |
3649 | // FIXME: Should we also allow this in C++11? |
3650 | if (!Info.getLangOpts().CPlusPlus14) |
3651 | return false; |
3652 | return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true); |
3653 | } |
3654 | |
3655 | explicit operator bool() const { return !Type.isNull(); } |
3656 | }; |
3657 | } // end anonymous namespace |
3658 | |
3659 | static QualType getSubobjectType(QualType ObjType, QualType SubobjType, |
3660 | bool IsMutable = false) { |
3661 | // C++ [basic.type.qualifier]p1: |
3662 | // - A const object is an object of type const T or a non-mutable subobject |
3663 | // of a const object. |
3664 | if (ObjType.isConstQualified() && !IsMutable) |
3665 | SubobjType.addConst(); |
3666 | // - A volatile object is an object of type const T or a subobject of a |
3667 | // volatile object. |
3668 | if (ObjType.isVolatileQualified()) |
3669 | SubobjType.addVolatile(); |
3670 | return SubobjType; |
3671 | } |
3672 | |
3673 | /// Find the designated sub-object of an rvalue. |
3674 | template<typename SubobjectHandler> |
3675 | typename SubobjectHandler::result_type |
3676 | findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj, |
3677 | const SubobjectDesignator &Sub, SubobjectHandler &handler) { |
3678 | if (Sub.Invalid) |
3679 | // A diagnostic will have already been produced. |
3680 | return handler.failed(); |
3681 | if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) { |
3682 | if (Info.getLangOpts().CPlusPlus11) |
3683 | Info.FFDiag(E, Sub.isOnePastTheEnd() |
3684 | ? diag::note_constexpr_access_past_end |
3685 | : diag::note_constexpr_access_unsized_array) |
3686 | << handler.AccessKind; |
3687 | else |
3688 | Info.FFDiag(E); |
3689 | return handler.failed(); |
3690 | } |
3691 | |
3692 | APValue *O = Obj.Value; |
3693 | QualType ObjType = Obj.Type; |
3694 | const FieldDecl *LastField = nullptr; |
3695 | const FieldDecl *VolatileField = nullptr; |
3696 | |
3697 | // Walk the designator's path to find the subobject. |
3698 | for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) { |
3699 | // Reading an indeterminate value is undefined, but assigning over one is OK. |
3700 | if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) || |
3701 | (O->isIndeterminate() && |
3702 | !isValidIndeterminateAccess(handler.AccessKind))) { |
3703 | if (!Info.checkingPotentialConstantExpression()) |
3704 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
3705 | << handler.AccessKind << O->isIndeterminate() |
3706 | << E->getSourceRange(); |
3707 | return handler.failed(); |
3708 | } |
3709 | |
3710 | // C++ [class.ctor]p5, C++ [class.dtor]p5: |
3711 | // const and volatile semantics are not applied on an object under |
3712 | // {con,de}struction. |
3713 | if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) && |
3714 | ObjType->isRecordType() && |
3715 | Info.isEvaluatingCtorDtor( |
3716 | Base: Obj.Base, |
3717 | Path: llvm::ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) != |
3718 | ConstructionPhase::None) { |
3719 | ObjType = Info.Ctx.getCanonicalType(T: ObjType); |
3720 | ObjType.removeLocalConst(); |
3721 | ObjType.removeLocalVolatile(); |
3722 | } |
3723 | |
3724 | // If this is our last pass, check that the final object type is OK. |
3725 | if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) { |
3726 | // Accesses to volatile objects are prohibited. |
3727 | if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) { |
3728 | if (Info.getLangOpts().CPlusPlus) { |
3729 | int DiagKind; |
3730 | SourceLocation Loc; |
3731 | const NamedDecl *Decl = nullptr; |
3732 | if (VolatileField) { |
3733 | DiagKind = 2; |
3734 | Loc = VolatileField->getLocation(); |
3735 | Decl = VolatileField; |
3736 | } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) { |
3737 | DiagKind = 1; |
3738 | Loc = VD->getLocation(); |
3739 | Decl = VD; |
3740 | } else { |
3741 | DiagKind = 0; |
3742 | if (auto *E = Obj.Base.dyn_cast<const Expr *>()) |
3743 | Loc = E->getExprLoc(); |
3744 | } |
3745 | Info.FFDiag(E, diag::note_constexpr_access_volatile_obj, 1) |
3746 | << handler.AccessKind << DiagKind << Decl; |
3747 | Info.Note(Loc, diag::note_constexpr_volatile_here) << DiagKind; |
3748 | } else { |
3749 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
3750 | } |
3751 | return handler.failed(); |
3752 | } |
3753 | |
3754 | // If we are reading an object of class type, there may still be more |
3755 | // things we need to check: if there are any mutable subobjects, we |
3756 | // cannot perform this read. (This only happens when performing a trivial |
3757 | // copy or assignment.) |
3758 | if (ObjType->isRecordType() && |
3759 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) && |
3760 | diagnoseMutableFields(Info, E, handler.AccessKind, ObjType)) |
3761 | return handler.failed(); |
3762 | } |
3763 | |
3764 | if (I == N) { |
3765 | if (!handler.found(*O, ObjType)) |
3766 | return false; |
3767 | |
3768 | // If we modified a bit-field, truncate it to the right width. |
3769 | if (isModification(handler.AccessKind) && |
3770 | LastField && LastField->isBitField() && |
3771 | !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField)) |
3772 | return false; |
3773 | |
3774 | return true; |
3775 | } |
3776 | |
3777 | LastField = nullptr; |
3778 | if (ObjType->isArrayType()) { |
3779 | // Next subobject is an array element. |
3780 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: ObjType); |
3781 | assert(CAT && "vla in literal type?" ); |
3782 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3783 | if (CAT->getSize().ule(RHS: Index)) { |
3784 | // Note, it should not be possible to form a pointer with a valid |
3785 | // designator which points more than one past the end of the array. |
3786 | if (Info.getLangOpts().CPlusPlus11) |
3787 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3788 | << handler.AccessKind; |
3789 | else |
3790 | Info.FFDiag(E); |
3791 | return handler.failed(); |
3792 | } |
3793 | |
3794 | ObjType = CAT->getElementType(); |
3795 | |
3796 | if (O->getArrayInitializedElts() > Index) |
3797 | O = &O->getArrayInitializedElt(I: Index); |
3798 | else if (!isRead(handler.AccessKind)) { |
3799 | if (!CheckArraySize(Info, CAT, CallLoc: E->getExprLoc())) |
3800 | return handler.failed(); |
3801 | |
3802 | expandArray(Array&: *O, Index); |
3803 | O = &O->getArrayInitializedElt(I: Index); |
3804 | } else |
3805 | O = &O->getArrayFiller(); |
3806 | } else if (ObjType->isAnyComplexType()) { |
3807 | // Next subobject is a complex number. |
3808 | uint64_t Index = Sub.Entries[I].getAsArrayIndex(); |
3809 | if (Index > 1) { |
3810 | if (Info.getLangOpts().CPlusPlus11) |
3811 | Info.FFDiag(E, diag::note_constexpr_access_past_end) |
3812 | << handler.AccessKind; |
3813 | else |
3814 | Info.FFDiag(E); |
3815 | return handler.failed(); |
3816 | } |
3817 | |
3818 | ObjType = getSubobjectType( |
3819 | ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType()); |
3820 | |
3821 | assert(I == N - 1 && "extracting subobject of scalar?" ); |
3822 | if (O->isComplexInt()) { |
3823 | return handler.found(Index ? O->getComplexIntImag() |
3824 | : O->getComplexIntReal(), ObjType); |
3825 | } else { |
3826 | assert(O->isComplexFloat()); |
3827 | return handler.found(Index ? O->getComplexFloatImag() |
3828 | : O->getComplexFloatReal(), ObjType); |
3829 | } |
3830 | } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) { |
3831 | if (Field->isMutable() && |
3832 | !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) { |
3833 | Info.FFDiag(E, diag::note_constexpr_access_mutable, 1) |
3834 | << handler.AccessKind << Field; |
3835 | Info.Note(Field->getLocation(), diag::note_declared_at); |
3836 | return handler.failed(); |
3837 | } |
3838 | |
3839 | // Next subobject is a class, struct or union field. |
3840 | RecordDecl *RD = ObjType->castAs<RecordType>()->getDecl(); |
3841 | if (RD->isUnion()) { |
3842 | const FieldDecl *UnionField = O->getUnionField(); |
3843 | if (!UnionField || |
3844 | UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) { |
3845 | if (I == N - 1 && handler.AccessKind == AK_Construct) { |
3846 | // Placement new onto an inactive union member makes it active. |
3847 | O->setUnion(Field, Value: APValue()); |
3848 | } else { |
3849 | // FIXME: If O->getUnionValue() is absent, report that there's no |
3850 | // active union member rather than reporting the prior active union |
3851 | // member. We'll need to fix nullptr_t to not use APValue() as its |
3852 | // representation first. |
3853 | Info.FFDiag(E, diag::note_constexpr_access_inactive_union_member) |
3854 | << handler.AccessKind << Field << !UnionField << UnionField; |
3855 | return handler.failed(); |
3856 | } |
3857 | } |
3858 | O = &O->getUnionValue(); |
3859 | } else |
3860 | O = &O->getStructField(i: Field->getFieldIndex()); |
3861 | |
3862 | ObjType = getSubobjectType(ObjType, Field->getType(), Field->isMutable()); |
3863 | LastField = Field; |
3864 | if (Field->getType().isVolatileQualified()) |
3865 | VolatileField = Field; |
3866 | } else { |
3867 | // Next subobject is a base class. |
3868 | const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl(); |
3869 | const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]); |
3870 | O = &O->getStructBase(i: getBaseIndex(Derived, Base)); |
3871 | |
3872 | ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getRecordType(Base)); |
3873 | } |
3874 | } |
3875 | } |
3876 | |
3877 | namespace { |
3878 | struct ExtractSubobjectHandler { |
3879 | EvalInfo &Info; |
3880 | const Expr *E; |
3881 | APValue &Result; |
3882 | const AccessKinds AccessKind; |
3883 | |
3884 | typedef bool result_type; |
3885 | bool failed() { return false; } |
3886 | bool found(APValue &Subobj, QualType SubobjType) { |
3887 | Result = Subobj; |
3888 | if (AccessKind == AK_ReadObjectRepresentation) |
3889 | return true; |
3890 | return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result); |
3891 | } |
3892 | bool found(APSInt &Value, QualType SubobjType) { |
3893 | Result = APValue(Value); |
3894 | return true; |
3895 | } |
3896 | bool found(APFloat &Value, QualType SubobjType) { |
3897 | Result = APValue(Value); |
3898 | return true; |
3899 | } |
3900 | }; |
3901 | } // end anonymous namespace |
3902 | |
3903 | /// Extract the designated sub-object of an rvalue. |
3904 | static bool (EvalInfo &Info, const Expr *E, |
3905 | const CompleteObject &Obj, |
3906 | const SubobjectDesignator &Sub, APValue &Result, |
3907 | AccessKinds AK = AK_Read) { |
3908 | assert(AK == AK_Read || AK == AK_ReadObjectRepresentation); |
3909 | ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK}; |
3910 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
3911 | } |
3912 | |
3913 | namespace { |
3914 | struct ModifySubobjectHandler { |
3915 | EvalInfo &Info; |
3916 | APValue &NewVal; |
3917 | const Expr *E; |
3918 | |
3919 | typedef bool result_type; |
3920 | static const AccessKinds AccessKind = AK_Assign; |
3921 | |
3922 | bool checkConst(QualType QT) { |
3923 | // Assigning to a const object has undefined behavior. |
3924 | if (QT.isConstQualified()) { |
3925 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
3926 | return false; |
3927 | } |
3928 | return true; |
3929 | } |
3930 | |
3931 | bool failed() { return false; } |
3932 | bool found(APValue &Subobj, QualType SubobjType) { |
3933 | if (!checkConst(QT: SubobjType)) |
3934 | return false; |
3935 | // We've been given ownership of NewVal, so just swap it in. |
3936 | Subobj.swap(RHS&: NewVal); |
3937 | return true; |
3938 | } |
3939 | bool found(APSInt &Value, QualType SubobjType) { |
3940 | if (!checkConst(QT: SubobjType)) |
3941 | return false; |
3942 | if (!NewVal.isInt()) { |
3943 | // Maybe trying to write a cast pointer value into a complex? |
3944 | Info.FFDiag(E); |
3945 | return false; |
3946 | } |
3947 | Value = NewVal.getInt(); |
3948 | return true; |
3949 | } |
3950 | bool found(APFloat &Value, QualType SubobjType) { |
3951 | if (!checkConst(QT: SubobjType)) |
3952 | return false; |
3953 | Value = NewVal.getFloat(); |
3954 | return true; |
3955 | } |
3956 | }; |
3957 | } // end anonymous namespace |
3958 | |
3959 | const AccessKinds ModifySubobjectHandler::AccessKind; |
3960 | |
3961 | /// Update the designated sub-object of an rvalue to the given value. |
3962 | static bool modifySubobject(EvalInfo &Info, const Expr *E, |
3963 | const CompleteObject &Obj, |
3964 | const SubobjectDesignator &Sub, |
3965 | APValue &NewVal) { |
3966 | ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E }; |
3967 | return findSubobject(Info, E, Obj, Sub, handler&: Handler); |
3968 | } |
3969 | |
3970 | /// Find the position where two subobject designators diverge, or equivalently |
3971 | /// the length of the common initial subsequence. |
3972 | static unsigned FindDesignatorMismatch(QualType ObjType, |
3973 | const SubobjectDesignator &A, |
3974 | const SubobjectDesignator &B, |
3975 | bool &WasArrayIndex) { |
3976 | unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size()); |
3977 | for (/**/; I != N; ++I) { |
3978 | if (!ObjType.isNull() && |
3979 | (ObjType->isArrayType() || ObjType->isAnyComplexType())) { |
3980 | // Next subobject is an array element. |
3981 | if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) { |
3982 | WasArrayIndex = true; |
3983 | return I; |
3984 | } |
3985 | if (ObjType->isAnyComplexType()) |
3986 | ObjType = ObjType->castAs<ComplexType>()->getElementType(); |
3987 | else |
3988 | ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType(); |
3989 | } else { |
3990 | if (A.Entries[I].getAsBaseOrMember() != |
3991 | B.Entries[I].getAsBaseOrMember()) { |
3992 | WasArrayIndex = false; |
3993 | return I; |
3994 | } |
3995 | if (const FieldDecl *FD = getAsField(E: A.Entries[I])) |
3996 | // Next subobject is a field. |
3997 | ObjType = FD->getType(); |
3998 | else |
3999 | // Next subobject is a base class. |
4000 | ObjType = QualType(); |
4001 | } |
4002 | } |
4003 | WasArrayIndex = false; |
4004 | return I; |
4005 | } |
4006 | |
4007 | /// Determine whether the given subobject designators refer to elements of the |
4008 | /// same array object. |
4009 | static bool AreElementsOfSameArray(QualType ObjType, |
4010 | const SubobjectDesignator &A, |
4011 | const SubobjectDesignator &B) { |
4012 | if (A.Entries.size() != B.Entries.size()) |
4013 | return false; |
4014 | |
4015 | bool IsArray = A.MostDerivedIsArrayElement; |
4016 | if (IsArray && A.MostDerivedPathLength != A.Entries.size()) |
4017 | // A is a subobject of the array element. |
4018 | return false; |
4019 | |
4020 | // If A (and B) designates an array element, the last entry will be the array |
4021 | // index. That doesn't have to match. Otherwise, we're in the 'implicit array |
4022 | // of length 1' case, and the entire path must match. |
4023 | bool WasArrayIndex; |
4024 | unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex); |
4025 | return CommonLength >= A.Entries.size() - IsArray; |
4026 | } |
4027 | |
4028 | /// Find the complete object to which an LValue refers. |
4029 | static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E, |
4030 | AccessKinds AK, const LValue &LVal, |
4031 | QualType LValType) { |
4032 | if (LVal.InvalidBase) { |
4033 | Info.FFDiag(E); |
4034 | return CompleteObject(); |
4035 | } |
4036 | |
4037 | if (!LVal.Base) { |
4038 | Info.FFDiag(E, diag::note_constexpr_access_null) << AK; |
4039 | return CompleteObject(); |
4040 | } |
4041 | |
4042 | CallStackFrame *Frame = nullptr; |
4043 | unsigned Depth = 0; |
4044 | if (LVal.getLValueCallIndex()) { |
4045 | std::tie(args&: Frame, args&: Depth) = |
4046 | Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex()); |
4047 | if (!Frame) { |
4048 | Info.FFDiag(E, diag::note_constexpr_lifetime_ended, 1) |
4049 | << AK << LVal.Base.is<const ValueDecl*>(); |
4050 | NoteLValueLocation(Info, Base: LVal.Base); |
4051 | return CompleteObject(); |
4052 | } |
4053 | } |
4054 | |
4055 | bool IsAccess = isAnyAccess(AK); |
4056 | |
4057 | // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type |
4058 | // is not a constant expression (even if the object is non-volatile). We also |
4059 | // apply this rule to C++98, in order to conform to the expected 'volatile' |
4060 | // semantics. |
4061 | if (isFormalAccess(AK) && LValType.isVolatileQualified()) { |
4062 | if (Info.getLangOpts().CPlusPlus) |
4063 | Info.FFDiag(E, diag::note_constexpr_access_volatile_type) |
4064 | << AK << LValType; |
4065 | else |
4066 | Info.FFDiag(E); |
4067 | return CompleteObject(); |
4068 | } |
4069 | |
4070 | // Compute value storage location and type of base object. |
4071 | APValue *BaseVal = nullptr; |
4072 | QualType BaseType = getType(B: LVal.Base); |
4073 | |
4074 | if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl && |
4075 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4076 | // This is the object whose initializer we're evaluating, so its lifetime |
4077 | // started in the current evaluation. |
4078 | BaseVal = Info.EvaluatingDeclValue; |
4079 | } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) { |
4080 | // Allow reading from a GUID declaration. |
4081 | if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) { |
4082 | if (isModification(AK)) { |
4083 | // All the remaining cases do not permit modification of the object. |
4084 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4085 | return CompleteObject(); |
4086 | } |
4087 | APValue &V = GD->getAsAPValue(); |
4088 | if (V.isAbsent()) { |
4089 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
4090 | << GD->getType(); |
4091 | return CompleteObject(); |
4092 | } |
4093 | return CompleteObject(LVal.Base, &V, GD->getType()); |
4094 | } |
4095 | |
4096 | // Allow reading the APValue from an UnnamedGlobalConstantDecl. |
4097 | if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) { |
4098 | if (isModification(AK)) { |
4099 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4100 | return CompleteObject(); |
4101 | } |
4102 | return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()), |
4103 | GCD->getType()); |
4104 | } |
4105 | |
4106 | // Allow reading from template parameter objects. |
4107 | if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) { |
4108 | if (isModification(AK)) { |
4109 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4110 | return CompleteObject(); |
4111 | } |
4112 | return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()), |
4113 | TPO->getType()); |
4114 | } |
4115 | |
4116 | // In C++98, const, non-volatile integers initialized with ICEs are ICEs. |
4117 | // In C++11, constexpr, non-volatile variables initialized with constant |
4118 | // expressions are constant expressions too. Inside constexpr functions, |
4119 | // parameters are constant expressions even if they're non-const. |
4120 | // In C++1y, objects local to a constant expression (those with a Frame) are |
4121 | // both readable and writable inside constant expressions. |
4122 | // In C, such things can also be folded, although they are not ICEs. |
4123 | const VarDecl *VD = dyn_cast<VarDecl>(Val: D); |
4124 | if (VD) { |
4125 | if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx)) |
4126 | VD = VDef; |
4127 | } |
4128 | if (!VD || VD->isInvalidDecl()) { |
4129 | Info.FFDiag(E); |
4130 | return CompleteObject(); |
4131 | } |
4132 | |
4133 | bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx); |
4134 | |
4135 | // Unless we're looking at a local variable or argument in a constexpr call, |
4136 | // the variable we're reading must be const. |
4137 | if (!Frame) { |
4138 | if (IsAccess && isa<ParmVarDecl>(Val: VD)) { |
4139 | // Access of a parameter that's not associated with a frame isn't going |
4140 | // to work out, but we can leave it to evaluateVarDeclInit to provide a |
4141 | // suitable diagnostic. |
4142 | } else if (Info.getLangOpts().CPlusPlus14 && |
4143 | lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4144 | // OK, we can read and modify an object if we're in the process of |
4145 | // evaluating its initializer, because its lifetime began in this |
4146 | // evaluation. |
4147 | } else if (isModification(AK)) { |
4148 | // All the remaining cases do not permit modification of the object. |
4149 | Info.FFDiag(E, diag::note_constexpr_modify_global); |
4150 | return CompleteObject(); |
4151 | } else if (VD->isConstexpr()) { |
4152 | // OK, we can read this variable. |
4153 | } else if (BaseType->isIntegralOrEnumerationType()) { |
4154 | if (!IsConstant) { |
4155 | if (!IsAccess) |
4156 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4157 | if (Info.getLangOpts().CPlusPlus) { |
4158 | Info.FFDiag(E, diag::note_constexpr_ltor_non_const_int, 1) << VD; |
4159 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4160 | } else { |
4161 | Info.FFDiag(E); |
4162 | } |
4163 | return CompleteObject(); |
4164 | } |
4165 | } else if (!IsAccess) { |
4166 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4167 | } else if (IsConstant && Info.checkingPotentialConstantExpression() && |
4168 | BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) { |
4169 | // This variable might end up being constexpr. Don't diagnose it yet. |
4170 | } else if (IsConstant) { |
4171 | // Keep evaluating to see what we can do. In particular, we support |
4172 | // folding of const floating-point types, in order to make static const |
4173 | // data members of such types (supported as an extension) more useful. |
4174 | if (Info.getLangOpts().CPlusPlus) { |
4175 | Info.CCEDiag(E, Info.getLangOpts().CPlusPlus11 |
4176 | ? diag::note_constexpr_ltor_non_constexpr |
4177 | : diag::note_constexpr_ltor_non_integral, 1) |
4178 | << VD << BaseType; |
4179 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4180 | } else { |
4181 | Info.CCEDiag(E); |
4182 | } |
4183 | } else { |
4184 | // Never allow reading a non-const value. |
4185 | if (Info.getLangOpts().CPlusPlus) { |
4186 | Info.FFDiag(E, Info.getLangOpts().CPlusPlus11 |
4187 | ? diag::note_constexpr_ltor_non_constexpr |
4188 | : diag::note_constexpr_ltor_non_integral, 1) |
4189 | << VD << BaseType; |
4190 | Info.Note(VD->getLocation(), diag::note_declared_at); |
4191 | } else { |
4192 | Info.FFDiag(E); |
4193 | } |
4194 | return CompleteObject(); |
4195 | } |
4196 | } |
4197 | |
4198 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(), Result&: BaseVal)) |
4199 | return CompleteObject(); |
4200 | } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) { |
4201 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
4202 | if (!Alloc) { |
4203 | Info.FFDiag(E, diag::note_constexpr_access_deleted_object) << AK; |
4204 | return CompleteObject(); |
4205 | } |
4206 | return CompleteObject(LVal.Base, &(*Alloc)->Value, |
4207 | LVal.Base.getDynamicAllocType()); |
4208 | } else { |
4209 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4210 | |
4211 | if (!Frame) { |
4212 | if (const MaterializeTemporaryExpr *MTE = |
4213 | dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) { |
4214 | assert(MTE->getStorageDuration() == SD_Static && |
4215 | "should have a frame for a non-global materialized temporary" ); |
4216 | |
4217 | // C++20 [expr.const]p4: [DR2126] |
4218 | // An object or reference is usable in constant expressions if it is |
4219 | // - a temporary object of non-volatile const-qualified literal type |
4220 | // whose lifetime is extended to that of a variable that is usable |
4221 | // in constant expressions |
4222 | // |
4223 | // C++20 [expr.const]p5: |
4224 | // an lvalue-to-rvalue conversion [is not allowed unless it applies to] |
4225 | // - a non-volatile glvalue that refers to an object that is usable |
4226 | // in constant expressions, or |
4227 | // - a non-volatile glvalue of literal type that refers to a |
4228 | // non-volatile object whose lifetime began within the evaluation |
4229 | // of E; |
4230 | // |
4231 | // C++11 misses the 'began within the evaluation of e' check and |
4232 | // instead allows all temporaries, including things like: |
4233 | // int &&r = 1; |
4234 | // int x = ++r; |
4235 | // constexpr int k = r; |
4236 | // Therefore we use the C++14-onwards rules in C++11 too. |
4237 | // |
4238 | // Note that temporaries whose lifetimes began while evaluating a |
4239 | // variable's constructor are not usable while evaluating the |
4240 | // corresponding destructor, not even if they're of const-qualified |
4241 | // types. |
4242 | if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) && |
4243 | !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) { |
4244 | if (!IsAccess) |
4245 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4246 | Info.FFDiag(E, diag::note_constexpr_access_static_temporary, 1) << AK; |
4247 | Info.Note(MTE->getExprLoc(), diag::note_constexpr_temporary_here); |
4248 | return CompleteObject(); |
4249 | } |
4250 | |
4251 | BaseVal = MTE->getOrCreateValue(MayCreate: false); |
4252 | assert(BaseVal && "got reference to unevaluated temporary" ); |
4253 | } else { |
4254 | if (!IsAccess) |
4255 | return CompleteObject(LVal.getLValueBase(), nullptr, BaseType); |
4256 | APValue Val; |
4257 | LVal.moveInto(V&: Val); |
4258 | Info.FFDiag(E, diag::note_constexpr_access_unreadable_object) |
4259 | << AK |
4260 | << Val.getAsString(Info.Ctx, |
4261 | Info.Ctx.getLValueReferenceType(LValType)); |
4262 | NoteLValueLocation(Info, Base: LVal.Base); |
4263 | return CompleteObject(); |
4264 | } |
4265 | } else { |
4266 | BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion()); |
4267 | assert(BaseVal && "missing value for temporary" ); |
4268 | } |
4269 | } |
4270 | |
4271 | // In C++14, we can't safely access any mutable state when we might be |
4272 | // evaluating after an unmodeled side effect. Parameters are modeled as state |
4273 | // in the caller, but aren't visible once the call returns, so they can be |
4274 | // modified in a speculatively-evaluated call. |
4275 | // |
4276 | // FIXME: Not all local state is mutable. Allow local constant subobjects |
4277 | // to be read here (but take care with 'mutable' fields). |
4278 | unsigned VisibleDepth = Depth; |
4279 | if (llvm::isa_and_nonnull<ParmVarDecl>( |
4280 | Val: LVal.Base.dyn_cast<const ValueDecl *>())) |
4281 | ++VisibleDepth; |
4282 | if ((Frame && Info.getLangOpts().CPlusPlus14 && |
4283 | Info.EvalStatus.HasSideEffects) || |
4284 | (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth)) |
4285 | return CompleteObject(); |
4286 | |
4287 | return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType); |
4288 | } |
4289 | |
4290 | /// Perform an lvalue-to-rvalue conversion on the given glvalue. This |
4291 | /// can also be used for 'lvalue-to-lvalue' conversions for looking up the |
4292 | /// glvalue referred to by an entity of reference type. |
4293 | /// |
4294 | /// \param Info - Information about the ongoing evaluation. |
4295 | /// \param Conv - The expression for which we are performing the conversion. |
4296 | /// Used for diagnostics. |
4297 | /// \param Type - The type of the glvalue (before stripping cv-qualifiers in the |
4298 | /// case of a non-class type). |
4299 | /// \param LVal - The glvalue on which we are attempting to perform this action. |
4300 | /// \param RVal - The produced value will be placed here. |
4301 | /// \param WantObjectRepresentation - If true, we're looking for the object |
4302 | /// representation rather than the value, and in particular, |
4303 | /// there is no requirement that the result be fully initialized. |
4304 | static bool |
4305 | handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type, |
4306 | const LValue &LVal, APValue &RVal, |
4307 | bool WantObjectRepresentation = false) { |
4308 | if (LVal.Designator.Invalid) |
4309 | return false; |
4310 | |
4311 | // Check for special cases where there is no existing APValue to look at. |
4312 | const Expr *Base = LVal.Base.dyn_cast<const Expr*>(); |
4313 | |
4314 | AccessKinds AK = |
4315 | WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read; |
4316 | |
4317 | if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) { |
4318 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(Val: Base)) { |
4319 | // In C99, a CompoundLiteralExpr is an lvalue, and we defer evaluating the |
4320 | // initializer until now for such expressions. Such an expression can't be |
4321 | // an ICE in C, so this only matters for fold. |
4322 | if (Type.isVolatileQualified()) { |
4323 | Info.FFDiag(E: Conv); |
4324 | return false; |
4325 | } |
4326 | |
4327 | APValue Lit; |
4328 | if (!Evaluate(Result&: Lit, Info, E: CLE->getInitializer())) |
4329 | return false; |
4330 | |
4331 | // According to GCC info page: |
4332 | // |
4333 | // 6.28 Compound Literals |
4334 | // |
4335 | // As an optimization, G++ sometimes gives array compound literals longer |
4336 | // lifetimes: when the array either appears outside a function or has a |
4337 | // const-qualified type. If foo and its initializer had elements of type |
4338 | // char *const rather than char *, or if foo were a global variable, the |
4339 | // array would have static storage duration. But it is probably safest |
4340 | // just to avoid the use of array compound literals in C++ code. |
4341 | // |
4342 | // Obey that rule by checking constness for converted array types. |
4343 | |
4344 | QualType CLETy = CLE->getType(); |
4345 | if (CLETy->isArrayType() && !Type->isArrayType()) { |
4346 | if (!CLETy.isConstant(Ctx: Info.Ctx)) { |
4347 | Info.FFDiag(E: Conv); |
4348 | Info.Note(CLE->getExprLoc(), diag::note_declared_at); |
4349 | return false; |
4350 | } |
4351 | } |
4352 | |
4353 | CompleteObject LitObj(LVal.Base, &Lit, Base->getType()); |
4354 | return extractSubobject(Info, Conv, LitObj, LVal.Designator, RVal, AK); |
4355 | } else if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) { |
4356 | // Special-case character extraction so we don't have to construct an |
4357 | // APValue for the whole string. |
4358 | assert(LVal.Designator.Entries.size() <= 1 && |
4359 | "Can only read characters from string literals" ); |
4360 | if (LVal.Designator.Entries.empty()) { |
4361 | // Fail for now for LValue to RValue conversion of an array. |
4362 | // (This shouldn't show up in C/C++, but it could be triggered by a |
4363 | // weird EvaluateAsRValue call from a tool.) |
4364 | Info.FFDiag(E: Conv); |
4365 | return false; |
4366 | } |
4367 | if (LVal.Designator.isOnePastTheEnd()) { |
4368 | if (Info.getLangOpts().CPlusPlus11) |
4369 | Info.FFDiag(Conv, diag::note_constexpr_access_past_end) << AK; |
4370 | else |
4371 | Info.FFDiag(E: Conv); |
4372 | return false; |
4373 | } |
4374 | uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex(); |
4375 | RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex)); |
4376 | return true; |
4377 | } |
4378 | } |
4379 | |
4380 | CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type); |
4381 | return Obj && extractSubobject(Info, Conv, Obj, LVal.Designator, RVal, AK); |
4382 | } |
4383 | |
4384 | /// Perform an assignment of Val to LVal. Takes ownership of Val. |
4385 | static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4386 | QualType LValType, APValue &Val) { |
4387 | if (LVal.Designator.Invalid) |
4388 | return false; |
4389 | |
4390 | if (!Info.getLangOpts().CPlusPlus14) { |
4391 | Info.FFDiag(E); |
4392 | return false; |
4393 | } |
4394 | |
4395 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType); |
4396 | return Obj && modifySubobject(Info, E, Obj, LVal.Designator, Val); |
4397 | } |
4398 | |
4399 | namespace { |
4400 | struct CompoundAssignSubobjectHandler { |
4401 | EvalInfo &Info; |
4402 | const CompoundAssignOperator *E; |
4403 | QualType PromotedLHSType; |
4404 | BinaryOperatorKind Opcode; |
4405 | const APValue &RHS; |
4406 | |
4407 | static const AccessKinds AccessKind = AK_Assign; |
4408 | |
4409 | typedef bool result_type; |
4410 | |
4411 | bool checkConst(QualType QT) { |
4412 | // Assigning to a const object has undefined behavior. |
4413 | if (QT.isConstQualified()) { |
4414 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4415 | return false; |
4416 | } |
4417 | return true; |
4418 | } |
4419 | |
4420 | bool failed() { return false; } |
4421 | bool found(APValue &Subobj, QualType SubobjType) { |
4422 | switch (Subobj.getKind()) { |
4423 | case APValue::Int: |
4424 | return found(Value&: Subobj.getInt(), SubobjType); |
4425 | case APValue::Float: |
4426 | return found(Value&: Subobj.getFloat(), SubobjType); |
4427 | case APValue::ComplexInt: |
4428 | case APValue::ComplexFloat: |
4429 | // FIXME: Implement complex compound assignment. |
4430 | Info.FFDiag(E); |
4431 | return false; |
4432 | case APValue::LValue: |
4433 | return foundPointer(Subobj, SubobjType); |
4434 | case APValue::Vector: |
4435 | return foundVector(Value&: Subobj, SubobjType); |
4436 | case APValue::Indeterminate: |
4437 | Info.FFDiag(E, diag::note_constexpr_access_uninit) |
4438 | << /*read of=*/0 << /*uninitialized object=*/1 |
4439 | << E->getLHS()->getSourceRange(); |
4440 | return false; |
4441 | default: |
4442 | // FIXME: can this happen? |
4443 | Info.FFDiag(E); |
4444 | return false; |
4445 | } |
4446 | } |
4447 | |
4448 | bool foundVector(APValue &Value, QualType SubobjType) { |
4449 | if (!checkConst(QT: SubobjType)) |
4450 | return false; |
4451 | |
4452 | if (!SubobjType->isVectorType()) { |
4453 | Info.FFDiag(E); |
4454 | return false; |
4455 | } |
4456 | return handleVectorVectorBinOp(Info, E, Opcode, Value, RHS); |
4457 | } |
4458 | |
4459 | bool found(APSInt &Value, QualType SubobjType) { |
4460 | if (!checkConst(QT: SubobjType)) |
4461 | return false; |
4462 | |
4463 | if (!SubobjType->isIntegerType()) { |
4464 | // We don't support compound assignment on integer-cast-to-pointer |
4465 | // values. |
4466 | Info.FFDiag(E); |
4467 | return false; |
4468 | } |
4469 | |
4470 | if (RHS.isInt()) { |
4471 | APSInt LHS = |
4472 | HandleIntToIntCast(Info, E, PromotedLHSType, SubobjType, Value); |
4473 | if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS.getInt(), LHS)) |
4474 | return false; |
4475 | Value = HandleIntToIntCast(Info, E, SubobjType, PromotedLHSType, LHS); |
4476 | return true; |
4477 | } else if (RHS.isFloat()) { |
4478 | const FPOptions FPO = E->getFPFeaturesInEffect( |
4479 | Info.Ctx.getLangOpts()); |
4480 | APFloat FValue(0.0); |
4481 | return HandleIntToFloatCast(Info, E, FPO, SubobjType, Value, |
4482 | PromotedLHSType, FValue) && |
4483 | handleFloatFloatBinOp(Info, E, FValue, Opcode, RHS.getFloat()) && |
4484 | HandleFloatToIntCast(Info, E, PromotedLHSType, FValue, SubobjType, |
4485 | Value); |
4486 | } |
4487 | |
4488 | Info.FFDiag(E); |
4489 | return false; |
4490 | } |
4491 | bool found(APFloat &Value, QualType SubobjType) { |
4492 | return checkConst(SubobjType) && |
4493 | HandleFloatToFloatCast(Info, E, SubobjType, PromotedLHSType, |
4494 | Value) && |
4495 | handleFloatFloatBinOp(Info, E, Value, Opcode, RHS.getFloat()) && |
4496 | HandleFloatToFloatCast(Info, E, PromotedLHSType, SubobjType, Value); |
4497 | } |
4498 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4499 | if (!checkConst(QT: SubobjType)) |
4500 | return false; |
4501 | |
4502 | QualType PointeeType; |
4503 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4504 | PointeeType = PT->getPointeeType(); |
4505 | |
4506 | if (PointeeType.isNull() || !RHS.isInt() || |
4507 | (Opcode != BO_Add && Opcode != BO_Sub)) { |
4508 | Info.FFDiag(E); |
4509 | return false; |
4510 | } |
4511 | |
4512 | APSInt Offset = RHS.getInt(); |
4513 | if (Opcode == BO_Sub) |
4514 | negateAsSigned(Int&: Offset); |
4515 | |
4516 | LValue LVal; |
4517 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4518 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, Offset)) |
4519 | return false; |
4520 | LVal.moveInto(V&: Subobj); |
4521 | return true; |
4522 | } |
4523 | }; |
4524 | } // end anonymous namespace |
4525 | |
4526 | const AccessKinds CompoundAssignSubobjectHandler::AccessKind; |
4527 | |
4528 | /// Perform a compound assignment of LVal <op>= RVal. |
4529 | static bool handleCompoundAssignment(EvalInfo &Info, |
4530 | const CompoundAssignOperator *E, |
4531 | const LValue &LVal, QualType LValType, |
4532 | QualType PromotedLValType, |
4533 | BinaryOperatorKind Opcode, |
4534 | const APValue &RVal) { |
4535 | if (LVal.Designator.Invalid) |
4536 | return false; |
4537 | |
4538 | if (!Info.getLangOpts().CPlusPlus14) { |
4539 | Info.FFDiag(E); |
4540 | return false; |
4541 | } |
4542 | |
4543 | CompleteObject Obj = findCompleteObject(Info, E, AK_Assign, LVal, LValType); |
4544 | CompoundAssignSubobjectHandler Handler = { Info, E, PromotedLValType, Opcode, |
4545 | RVal }; |
4546 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4547 | } |
4548 | |
4549 | namespace { |
4550 | struct IncDecSubobjectHandler { |
4551 | EvalInfo &Info; |
4552 | const UnaryOperator *E; |
4553 | AccessKinds AccessKind; |
4554 | APValue *Old; |
4555 | |
4556 | typedef bool result_type; |
4557 | |
4558 | bool checkConst(QualType QT) { |
4559 | // Assigning to a const object has undefined behavior. |
4560 | if (QT.isConstQualified()) { |
4561 | Info.FFDiag(E, diag::note_constexpr_modify_const_type) << QT; |
4562 | return false; |
4563 | } |
4564 | return true; |
4565 | } |
4566 | |
4567 | bool failed() { return false; } |
4568 | bool found(APValue &Subobj, QualType SubobjType) { |
4569 | // Stash the old value. Also clear Old, so we don't clobber it later |
4570 | // if we're post-incrementing a complex. |
4571 | if (Old) { |
4572 | *Old = Subobj; |
4573 | Old = nullptr; |
4574 | } |
4575 | |
4576 | switch (Subobj.getKind()) { |
4577 | case APValue::Int: |
4578 | return found(Value&: Subobj.getInt(), SubobjType); |
4579 | case APValue::Float: |
4580 | return found(Value&: Subobj.getFloat(), SubobjType); |
4581 | case APValue::ComplexInt: |
4582 | return found(Value&: Subobj.getComplexIntReal(), |
4583 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4584 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4585 | case APValue::ComplexFloat: |
4586 | return found(Value&: Subobj.getComplexFloatReal(), |
4587 | SubobjType: SubobjType->castAs<ComplexType>()->getElementType() |
4588 | .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers())); |
4589 | case APValue::LValue: |
4590 | return foundPointer(Subobj, SubobjType); |
4591 | default: |
4592 | // FIXME: can this happen? |
4593 | Info.FFDiag(E); |
4594 | return false; |
4595 | } |
4596 | } |
4597 | bool found(APSInt &Value, QualType SubobjType) { |
4598 | if (!checkConst(QT: SubobjType)) |
4599 | return false; |
4600 | |
4601 | if (!SubobjType->isIntegerType()) { |
4602 | // We don't support increment / decrement on integer-cast-to-pointer |
4603 | // values. |
4604 | Info.FFDiag(E); |
4605 | return false; |
4606 | } |
4607 | |
4608 | if (Old) *Old = APValue(Value); |
4609 | |
4610 | // bool arithmetic promotes to int, and the conversion back to bool |
4611 | // doesn't reduce mod 2^n, so special-case it. |
4612 | if (SubobjType->isBooleanType()) { |
4613 | if (AccessKind == AK_Increment) |
4614 | Value = 1; |
4615 | else |
4616 | Value = !Value; |
4617 | return true; |
4618 | } |
4619 | |
4620 | bool WasNegative = Value.isNegative(); |
4621 | if (AccessKind == AK_Increment) { |
4622 | ++Value; |
4623 | |
4624 | if (!WasNegative && Value.isNegative() && E->canOverflow()) { |
4625 | APSInt ActualValue(Value, /*IsUnsigned*/true); |
4626 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4627 | } |
4628 | } else { |
4629 | --Value; |
4630 | |
4631 | if (WasNegative && !Value.isNegative() && E->canOverflow()) { |
4632 | unsigned BitWidth = Value.getBitWidth(); |
4633 | APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false); |
4634 | ActualValue.setBit(BitWidth); |
4635 | return HandleOverflow(Info, E, ActualValue, SubobjType); |
4636 | } |
4637 | } |
4638 | return true; |
4639 | } |
4640 | bool found(APFloat &Value, QualType SubobjType) { |
4641 | if (!checkConst(QT: SubobjType)) |
4642 | return false; |
4643 | |
4644 | if (Old) *Old = APValue(Value); |
4645 | |
4646 | APFloat One(Value.getSemantics(), 1); |
4647 | llvm::RoundingMode RM = getActiveRoundingMode(Info, E); |
4648 | APFloat::opStatus St; |
4649 | if (AccessKind == AK_Increment) |
4650 | St = Value.add(RHS: One, RM); |
4651 | else |
4652 | St = Value.subtract(RHS: One, RM); |
4653 | return checkFloatingPointResult(Info, E, St); |
4654 | } |
4655 | bool foundPointer(APValue &Subobj, QualType SubobjType) { |
4656 | if (!checkConst(QT: SubobjType)) |
4657 | return false; |
4658 | |
4659 | QualType PointeeType; |
4660 | if (const PointerType *PT = SubobjType->getAs<PointerType>()) |
4661 | PointeeType = PT->getPointeeType(); |
4662 | else { |
4663 | Info.FFDiag(E); |
4664 | return false; |
4665 | } |
4666 | |
4667 | LValue LVal; |
4668 | LVal.setFrom(Ctx&: Info.Ctx, V: Subobj); |
4669 | if (!HandleLValueArrayAdjustment(Info, E, LVal, PointeeType, |
4670 | AccessKind == AK_Increment ? 1 : -1)) |
4671 | return false; |
4672 | LVal.moveInto(V&: Subobj); |
4673 | return true; |
4674 | } |
4675 | }; |
4676 | } // end anonymous namespace |
4677 | |
4678 | /// Perform an increment or decrement on LVal. |
4679 | static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal, |
4680 | QualType LValType, bool IsIncrement, APValue *Old) { |
4681 | if (LVal.Designator.Invalid) |
4682 | return false; |
4683 | |
4684 | if (!Info.getLangOpts().CPlusPlus14) { |
4685 | Info.FFDiag(E); |
4686 | return false; |
4687 | } |
4688 | |
4689 | AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement; |
4690 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType); |
4691 | IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old}; |
4692 | return Obj && findSubobject(Info, E, Obj, LVal.Designator, Handler); |
4693 | } |
4694 | |
4695 | /// Build an lvalue for the object argument of a member function call. |
4696 | static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object, |
4697 | LValue &This) { |
4698 | if (Object->getType()->isPointerType() && Object->isPRValue()) |
4699 | return EvaluatePointer(E: Object, Result&: This, Info); |
4700 | |
4701 | if (Object->isGLValue()) |
4702 | return EvaluateLValue(E: Object, Result&: This, Info); |
4703 | |
4704 | if (Object->getType()->isLiteralType(Ctx: Info.Ctx)) |
4705 | return EvaluateTemporary(E: Object, Result&: This, Info); |
4706 | |
4707 | if (Object->getType()->isRecordType() && Object->isPRValue()) |
4708 | return EvaluateTemporary(E: Object, Result&: This, Info); |
4709 | |
4710 | Info.FFDiag(Object, diag::note_constexpr_nonliteral) << Object->getType(); |
4711 | return false; |
4712 | } |
4713 | |
4714 | /// HandleMemberPointerAccess - Evaluate a member access operation and build an |
4715 | /// lvalue referring to the result. |
4716 | /// |
4717 | /// \param Info - Information about the ongoing evaluation. |
4718 | /// \param LV - An lvalue referring to the base of the member pointer. |
4719 | /// \param RHS - The member pointer expression. |
4720 | /// \param IncludeMember - Specifies whether the member itself is included in |
4721 | /// the resulting LValue subobject designator. This is not possible when |
4722 | /// creating a bound member function. |
4723 | /// \return The field or method declaration to which the member pointer refers, |
4724 | /// or 0 if evaluation fails. |
4725 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4726 | QualType LVType, |
4727 | LValue &LV, |
4728 | const Expr *RHS, |
4729 | bool IncludeMember = true) { |
4730 | MemberPtr MemPtr; |
4731 | if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info)) |
4732 | return nullptr; |
4733 | |
4734 | // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to |
4735 | // member value, the behavior is undefined. |
4736 | if (!MemPtr.getDecl()) { |
4737 | // FIXME: Specific diagnostic. |
4738 | Info.FFDiag(E: RHS); |
4739 | return nullptr; |
4740 | } |
4741 | |
4742 | if (MemPtr.isDerivedMember()) { |
4743 | // This is a member of some derived class. Truncate LV appropriately. |
4744 | // The end of the derived-to-base path for the base object must match the |
4745 | // derived-to-base path for the member pointer. |
4746 | if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() > |
4747 | LV.Designator.Entries.size()) { |
4748 | Info.FFDiag(E: RHS); |
4749 | return nullptr; |
4750 | } |
4751 | unsigned PathLengthToMember = |
4752 | LV.Designator.Entries.size() - MemPtr.Path.size(); |
4753 | for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) { |
4754 | const CXXRecordDecl *LVDecl = getAsBaseClass( |
4755 | LV.Designator.Entries[PathLengthToMember + I]); |
4756 | const CXXRecordDecl *MPDecl = MemPtr.Path[I]; |
4757 | if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) { |
4758 | Info.FFDiag(E: RHS); |
4759 | return nullptr; |
4760 | } |
4761 | } |
4762 | |
4763 | // Truncate the lvalue to the appropriate derived class. |
4764 | if (!CastToDerivedClass(Info, RHS, LV, MemPtr.getContainingRecord(), |
4765 | PathLengthToMember)) |
4766 | return nullptr; |
4767 | } else if (!MemPtr.Path.empty()) { |
4768 | // Extend the LValue path with the member pointer's path. |
4769 | LV.Designator.Entries.reserve(LV.Designator.Entries.size() + |
4770 | MemPtr.Path.size() + IncludeMember); |
4771 | |
4772 | // Walk down to the appropriate base class. |
4773 | if (const PointerType *PT = LVType->getAs<PointerType>()) |
4774 | LVType = PT->getPointeeType(); |
4775 | const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl(); |
4776 | assert(RD && "member pointer access on non-class-type expression" ); |
4777 | // The first class in the path is that of the lvalue. |
4778 | for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) { |
4779 | const CXXRecordDecl *Base = MemPtr.Path[N - I - 1]; |
4780 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base)) |
4781 | return nullptr; |
4782 | RD = Base; |
4783 | } |
4784 | // Finally cast to the class containing the member. |
4785 | if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, |
4786 | Base: MemPtr.getContainingRecord())) |
4787 | return nullptr; |
4788 | } |
4789 | |
4790 | // Add the member. Note that we cannot build bound member functions here. |
4791 | if (IncludeMember) { |
4792 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) { |
4793 | if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD)) |
4794 | return nullptr; |
4795 | } else if (const IndirectFieldDecl *IFD = |
4796 | dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) { |
4797 | if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD)) |
4798 | return nullptr; |
4799 | } else { |
4800 | llvm_unreachable("can't construct reference to bound member function" ); |
4801 | } |
4802 | } |
4803 | |
4804 | return MemPtr.getDecl(); |
4805 | } |
4806 | |
4807 | static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info, |
4808 | const BinaryOperator *BO, |
4809 | LValue &LV, |
4810 | bool IncludeMember = true) { |
4811 | assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI); |
4812 | |
4813 | if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) { |
4814 | if (Info.noteFailure()) { |
4815 | MemberPtr MemPtr; |
4816 | EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info); |
4817 | } |
4818 | return nullptr; |
4819 | } |
4820 | |
4821 | return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV, |
4822 | RHS: BO->getRHS(), IncludeMember); |
4823 | } |
4824 | |
4825 | /// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on |
4826 | /// the provided lvalue, which currently refers to the base object. |
4827 | static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E, |
4828 | LValue &Result) { |
4829 | SubobjectDesignator &D = Result.Designator; |
4830 | if (D.Invalid || !Result.checkNullPointer(Info, E, CSK_Derived)) |
4831 | return false; |
4832 | |
4833 | QualType TargetQT = E->getType(); |
4834 | if (const PointerType *PT = TargetQT->getAs<PointerType>()) |
4835 | TargetQT = PT->getPointeeType(); |
4836 | |
4837 | // Check this cast lands within the final derived-to-base subobject path. |
4838 | if (D.MostDerivedPathLength + E->path_size() > D.Entries.size()) { |
4839 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4840 | << D.MostDerivedType << TargetQT; |
4841 | return false; |
4842 | } |
4843 | |
4844 | // Check the type of the final cast. We don't need to check the path, |
4845 | // since a cast can only be formed if the path is unique. |
4846 | unsigned NewEntriesSize = D.Entries.size() - E->path_size(); |
4847 | const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl(); |
4848 | const CXXRecordDecl *FinalType; |
4849 | if (NewEntriesSize == D.MostDerivedPathLength) |
4850 | FinalType = D.MostDerivedType->getAsCXXRecordDecl(); |
4851 | else |
4852 | FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]); |
4853 | if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl()) { |
4854 | Info.CCEDiag(E, diag::note_constexpr_invalid_downcast) |
4855 | << D.MostDerivedType << TargetQT; |
4856 | return false; |
4857 | } |
4858 | |
4859 | // Truncate the lvalue to the appropriate derived class. |
4860 | return CastToDerivedClass(Info, E, Result, TargetType, NewEntriesSize); |
4861 | } |
4862 | |
4863 | /// Get the value to use for a default-initialized object of type T. |
4864 | /// Return false if it encounters something invalid. |
4865 | static bool handleDefaultInitValue(QualType T, APValue &Result) { |
4866 | bool Success = true; |
4867 | |
4868 | // If there is already a value present don't overwrite it. |
4869 | if (!Result.isAbsent()) |
4870 | return true; |
4871 | |
4872 | if (auto *RD = T->getAsCXXRecordDecl()) { |
4873 | if (RD->isInvalidDecl()) { |
4874 | Result = APValue(); |
4875 | return false; |
4876 | } |
4877 | if (RD->isUnion()) { |
4878 | Result = APValue((const FieldDecl *)nullptr); |
4879 | return true; |
4880 | } |
4881 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
4882 | std::distance(RD->field_begin(), RD->field_end())); |
4883 | |
4884 | unsigned Index = 0; |
4885 | for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(), |
4886 | End = RD->bases_end(); |
4887 | I != End; ++I, ++Index) |
4888 | Success &= |
4889 | handleDefaultInitValue(T: I->getType(), Result&: Result.getStructBase(i: Index)); |
4890 | |
4891 | for (const auto *I : RD->fields()) { |
4892 | if (I->isUnnamedBitfield()) |
4893 | continue; |
4894 | Success &= handleDefaultInitValue( |
4895 | I->getType(), Result.getStructField(I->getFieldIndex())); |
4896 | } |
4897 | return Success; |
4898 | } |
4899 | |
4900 | if (auto *AT = |
4901 | dyn_cast_or_null<ConstantArrayType>(T->getAsArrayTypeUnsafe())) { |
4902 | Result = APValue(APValue::UninitArray(), 0, AT->getSize().getZExtValue()); |
4903 | if (Result.hasArrayFiller()) |
4904 | Success &= |
4905 | handleDefaultInitValue(AT->getElementType(), Result.getArrayFiller()); |
4906 | |
4907 | return Success; |
4908 | } |
4909 | |
4910 | Result = APValue::IndeterminateValue(); |
4911 | return true; |
4912 | } |
4913 | |
4914 | namespace { |
4915 | enum EvalStmtResult { |
4916 | /// Evaluation failed. |
4917 | ESR_Failed, |
4918 | /// Hit a 'return' statement. |
4919 | ESR_Returned, |
4920 | /// Evaluation succeeded. |
4921 | ESR_Succeeded, |
4922 | /// Hit a 'continue' statement. |
4923 | ESR_Continue, |
4924 | /// Hit a 'break' statement. |
4925 | ESR_Break, |
4926 | /// Still scanning for 'case' or 'default' statement. |
4927 | ESR_CaseNotFound |
4928 | }; |
4929 | } |
4930 | |
4931 | static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) { |
4932 | if (VD->isInvalidDecl()) |
4933 | return false; |
4934 | // We don't need to evaluate the initializer for a static local. |
4935 | if (!VD->hasLocalStorage()) |
4936 | return true; |
4937 | |
4938 | LValue Result; |
4939 | APValue &Val = Info.CurrentCall->createTemporary(VD, VD->getType(), |
4940 | ScopeKind::Block, Result); |
4941 | |
4942 | const Expr *InitE = VD->getInit(); |
4943 | if (!InitE) { |
4944 | if (VD->getType()->isDependentType()) |
4945 | return Info.noteSideEffect(); |
4946 | return handleDefaultInitValue(VD->getType(), Val); |
4947 | } |
4948 | if (InitE->isValueDependent()) |
4949 | return false; |
4950 | |
4951 | if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) { |
4952 | // Wipe out any partially-computed value, to allow tracking that this |
4953 | // evaluation failed. |
4954 | Val = APValue(); |
4955 | return false; |
4956 | } |
4957 | |
4958 | return true; |
4959 | } |
4960 | |
4961 | static bool EvaluateDecl(EvalInfo &Info, const Decl *D) { |
4962 | bool OK = true; |
4963 | |
4964 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
4965 | OK &= EvaluateVarDecl(Info, VD); |
4966 | |
4967 | if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D)) |
4968 | for (auto *BD : DD->bindings()) |
4969 | if (auto *VD = BD->getHoldingVar()) |
4970 | OK &= EvaluateDecl(Info, VD); |
4971 | |
4972 | return OK; |
4973 | } |
4974 | |
4975 | static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) { |
4976 | assert(E->isValueDependent()); |
4977 | if (Info.noteSideEffect()) |
4978 | return true; |
4979 | assert(E->containsErrors() && "valid value-dependent expression should never " |
4980 | "reach invalid code path." ); |
4981 | return false; |
4982 | } |
4983 | |
4984 | /// Evaluate a condition (either a variable declaration or an expression). |
4985 | static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl, |
4986 | const Expr *Cond, bool &Result) { |
4987 | if (Cond->isValueDependent()) |
4988 | return false; |
4989 | FullExpressionRAII Scope(Info); |
4990 | if (CondDecl && !EvaluateDecl(Info, CondDecl)) |
4991 | return false; |
4992 | if (!EvaluateAsBooleanCondition(E: Cond, Result, Info)) |
4993 | return false; |
4994 | return Scope.destroy(); |
4995 | } |
4996 | |
4997 | namespace { |
4998 | /// A location where the result (returned value) of evaluating a |
4999 | /// statement should be stored. |
5000 | struct StmtResult { |
5001 | /// The APValue that should be filled in with the returned value. |
5002 | APValue &Value; |
5003 | /// The location containing the result, if any (used to support RVO). |
5004 | const LValue *Slot; |
5005 | }; |
5006 | |
5007 | struct TempVersionRAII { |
5008 | CallStackFrame &Frame; |
5009 | |
5010 | TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) { |
5011 | Frame.pushTempVersion(); |
5012 | } |
5013 | |
5014 | ~TempVersionRAII() { |
5015 | Frame.popTempVersion(); |
5016 | } |
5017 | }; |
5018 | |
5019 | } |
5020 | |
5021 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5022 | const Stmt *S, |
5023 | const SwitchCase *SC = nullptr); |
5024 | |
5025 | /// Evaluate the body of a loop, and translate the result as appropriate. |
5026 | static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info, |
5027 | const Stmt *Body, |
5028 | const SwitchCase *Case = nullptr) { |
5029 | BlockScopeRAII Scope(Info); |
5030 | |
5031 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case); |
5032 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5033 | ESR = ESR_Failed; |
5034 | |
5035 | switch (ESR) { |
5036 | case ESR_Break: |
5037 | return ESR_Succeeded; |
5038 | case ESR_Succeeded: |
5039 | case ESR_Continue: |
5040 | return ESR_Continue; |
5041 | case ESR_Failed: |
5042 | case ESR_Returned: |
5043 | case ESR_CaseNotFound: |
5044 | return ESR; |
5045 | } |
5046 | llvm_unreachable("Invalid EvalStmtResult!" ); |
5047 | } |
5048 | |
5049 | /// Evaluate a switch statement. |
5050 | static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info, |
5051 | const SwitchStmt *SS) { |
5052 | BlockScopeRAII Scope(Info); |
5053 | |
5054 | // Evaluate the switch condition. |
5055 | APSInt Value; |
5056 | { |
5057 | if (const Stmt *Init = SS->getInit()) { |
5058 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5059 | if (ESR != ESR_Succeeded) { |
5060 | if (ESR != ESR_Failed && !Scope.destroy()) |
5061 | ESR = ESR_Failed; |
5062 | return ESR; |
5063 | } |
5064 | } |
5065 | |
5066 | FullExpressionRAII CondScope(Info); |
5067 | if (SS->getConditionVariable() && |
5068 | !EvaluateDecl(Info, SS->getConditionVariable())) |
5069 | return ESR_Failed; |
5070 | if (SS->getCond()->isValueDependent()) { |
5071 | // We don't know what the value is, and which branch should jump to. |
5072 | EvaluateDependentExpr(E: SS->getCond(), Info); |
5073 | return ESR_Failed; |
5074 | } |
5075 | if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info)) |
5076 | return ESR_Failed; |
5077 | |
5078 | if (!CondScope.destroy()) |
5079 | return ESR_Failed; |
5080 | } |
5081 | |
5082 | // Find the switch case corresponding to the value of the condition. |
5083 | // FIXME: Cache this lookup. |
5084 | const SwitchCase *Found = nullptr; |
5085 | for (const SwitchCase *SC = SS->getSwitchCaseList(); SC; |
5086 | SC = SC->getNextSwitchCase()) { |
5087 | if (isa<DefaultStmt>(Val: SC)) { |
5088 | Found = SC; |
5089 | continue; |
5090 | } |
5091 | |
5092 | const CaseStmt *CS = cast<CaseStmt>(Val: SC); |
5093 | APSInt LHS = CS->getLHS()->EvaluateKnownConstInt(Ctx: Info.Ctx); |
5094 | APSInt RHS = CS->getRHS() ? CS->getRHS()->EvaluateKnownConstInt(Ctx: Info.Ctx) |
5095 | : LHS; |
5096 | if (LHS <= Value && Value <= RHS) { |
5097 | Found = SC; |
5098 | break; |
5099 | } |
5100 | } |
5101 | |
5102 | if (!Found) |
5103 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5104 | |
5105 | // Search the switch body for the switch case and evaluate it from there. |
5106 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found); |
5107 | if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy()) |
5108 | return ESR_Failed; |
5109 | |
5110 | switch (ESR) { |
5111 | case ESR_Break: |
5112 | return ESR_Succeeded; |
5113 | case ESR_Succeeded: |
5114 | case ESR_Continue: |
5115 | case ESR_Failed: |
5116 | case ESR_Returned: |
5117 | return ESR; |
5118 | case ESR_CaseNotFound: |
5119 | // This can only happen if the switch case is nested within a statement |
5120 | // expression. We have no intention of supporting that. |
5121 | Info.FFDiag(Found->getBeginLoc(), |
5122 | diag::note_constexpr_stmt_expr_unsupported); |
5123 | return ESR_Failed; |
5124 | } |
5125 | llvm_unreachable("Invalid EvalStmtResult!" ); |
5126 | } |
5127 | |
5128 | static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) { |
5129 | // An expression E is a core constant expression unless the evaluation of E |
5130 | // would evaluate one of the following: [C++23] - a control flow that passes |
5131 | // through a declaration of a variable with static or thread storage duration |
5132 | // unless that variable is usable in constant expressions. |
5133 | if (VD->isLocalVarDecl() && VD->isStaticLocal() && |
5134 | !VD->isUsableInConstantExpressions(C: Info.Ctx)) { |
5135 | Info.CCEDiag(VD->getLocation(), diag::note_constexpr_static_local) |
5136 | << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD; |
5137 | return false; |
5138 | } |
5139 | return true; |
5140 | } |
5141 | |
5142 | // Evaluate a statement. |
5143 | static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, |
5144 | const Stmt *S, const SwitchCase *Case) { |
5145 | if (!Info.nextStep(S)) |
5146 | return ESR_Failed; |
5147 | |
5148 | // If we're hunting down a 'case' or 'default' label, recurse through |
5149 | // substatements until we hit the label. |
5150 | if (Case) { |
5151 | switch (S->getStmtClass()) { |
5152 | case Stmt::CompoundStmtClass: |
5153 | // FIXME: Precompute which substatement of a compound statement we |
5154 | // would jump to, and go straight there rather than performing a |
5155 | // linear scan each time. |
5156 | case Stmt::LabelStmtClass: |
5157 | case Stmt::AttributedStmtClass: |
5158 | case Stmt::DoStmtClass: |
5159 | break; |
5160 | |
5161 | case Stmt::CaseStmtClass: |
5162 | case Stmt::DefaultStmtClass: |
5163 | if (Case == S) |
5164 | Case = nullptr; |
5165 | break; |
5166 | |
5167 | case Stmt::IfStmtClass: { |
5168 | // FIXME: Precompute which side of an 'if' we would jump to, and go |
5169 | // straight there rather than scanning both sides. |
5170 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5171 | |
5172 | // Wrap the evaluation in a block scope, in case it's a DeclStmt |
5173 | // preceded by our switch label. |
5174 | BlockScopeRAII Scope(Info); |
5175 | |
5176 | // Step into the init statement in case it brings an (uninitialized) |
5177 | // variable into scope. |
5178 | if (const Stmt *Init = IS->getInit()) { |
5179 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5180 | if (ESR != ESR_CaseNotFound) { |
5181 | assert(ESR != ESR_Succeeded); |
5182 | return ESR; |
5183 | } |
5184 | } |
5185 | |
5186 | // Condition variable must be initialized if it exists. |
5187 | // FIXME: We can skip evaluating the body if there's a condition |
5188 | // variable, as there can't be any case labels within it. |
5189 | // (The same is true for 'for' statements.) |
5190 | |
5191 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case); |
5192 | if (ESR == ESR_Failed) |
5193 | return ESR; |
5194 | if (ESR != ESR_CaseNotFound) |
5195 | return Scope.destroy() ? ESR : ESR_Failed; |
5196 | if (!IS->getElse()) |
5197 | return ESR_CaseNotFound; |
5198 | |
5199 | ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case); |
5200 | if (ESR == ESR_Failed) |
5201 | return ESR; |
5202 | if (ESR != ESR_CaseNotFound) |
5203 | return Scope.destroy() ? ESR : ESR_Failed; |
5204 | return ESR_CaseNotFound; |
5205 | } |
5206 | |
5207 | case Stmt::WhileStmtClass: { |
5208 | EvalStmtResult ESR = |
5209 | EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case); |
5210 | if (ESR != ESR_Continue) |
5211 | return ESR; |
5212 | break; |
5213 | } |
5214 | |
5215 | case Stmt::ForStmtClass: { |
5216 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5217 | BlockScopeRAII Scope(Info); |
5218 | |
5219 | // Step into the init statement in case it brings an (uninitialized) |
5220 | // variable into scope. |
5221 | if (const Stmt *Init = FS->getInit()) { |
5222 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case); |
5223 | if (ESR != ESR_CaseNotFound) { |
5224 | assert(ESR != ESR_Succeeded); |
5225 | return ESR; |
5226 | } |
5227 | } |
5228 | |
5229 | EvalStmtResult ESR = |
5230 | EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case); |
5231 | if (ESR != ESR_Continue) |
5232 | return ESR; |
5233 | if (const auto *Inc = FS->getInc()) { |
5234 | if (Inc->isValueDependent()) { |
5235 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5236 | return ESR_Failed; |
5237 | } else { |
5238 | FullExpressionRAII IncScope(Info); |
5239 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5240 | return ESR_Failed; |
5241 | } |
5242 | } |
5243 | break; |
5244 | } |
5245 | |
5246 | case Stmt::DeclStmtClass: { |
5247 | // Start the lifetime of any uninitialized variables we encounter. They |
5248 | // might be used by the selected branch of the switch. |
5249 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5250 | for (const auto *D : DS->decls()) { |
5251 | if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
5252 | if (!CheckLocalVariableDeclaration(Info, VD)) |
5253 | return ESR_Failed; |
5254 | if (VD->hasLocalStorage() && !VD->getInit()) |
5255 | if (!EvaluateVarDecl(Info, VD)) |
5256 | return ESR_Failed; |
5257 | // FIXME: If the variable has initialization that can't be jumped |
5258 | // over, bail out of any immediately-surrounding compound-statement |
5259 | // too. There can't be any case labels here. |
5260 | } |
5261 | } |
5262 | return ESR_CaseNotFound; |
5263 | } |
5264 | |
5265 | default: |
5266 | return ESR_CaseNotFound; |
5267 | } |
5268 | } |
5269 | |
5270 | switch (S->getStmtClass()) { |
5271 | default: |
5272 | if (const Expr *E = dyn_cast<Expr>(Val: S)) { |
5273 | if (E->isValueDependent()) { |
5274 | if (!EvaluateDependentExpr(E, Info)) |
5275 | return ESR_Failed; |
5276 | } else { |
5277 | // Don't bother evaluating beyond an expression-statement which couldn't |
5278 | // be evaluated. |
5279 | // FIXME: Do we need the FullExpressionRAII object here? |
5280 | // VisitExprWithCleanups should create one when necessary. |
5281 | FullExpressionRAII Scope(Info); |
5282 | if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy()) |
5283 | return ESR_Failed; |
5284 | } |
5285 | return ESR_Succeeded; |
5286 | } |
5287 | |
5288 | Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange(); |
5289 | return ESR_Failed; |
5290 | |
5291 | case Stmt::NullStmtClass: |
5292 | return ESR_Succeeded; |
5293 | |
5294 | case Stmt::DeclStmtClass: { |
5295 | const DeclStmt *DS = cast<DeclStmt>(Val: S); |
5296 | for (const auto *D : DS->decls()) { |
5297 | const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D); |
5298 | if (VD && !CheckLocalVariableDeclaration(Info, VD)) |
5299 | return ESR_Failed; |
5300 | // Each declaration initialization is its own full-expression. |
5301 | FullExpressionRAII Scope(Info); |
5302 | if (!EvaluateDecl(Info, D) && !Info.noteFailure()) |
5303 | return ESR_Failed; |
5304 | if (!Scope.destroy()) |
5305 | return ESR_Failed; |
5306 | } |
5307 | return ESR_Succeeded; |
5308 | } |
5309 | |
5310 | case Stmt::ReturnStmtClass: { |
5311 | const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue(); |
5312 | FullExpressionRAII Scope(Info); |
5313 | if (RetExpr && RetExpr->isValueDependent()) { |
5314 | EvaluateDependentExpr(E: RetExpr, Info); |
5315 | // We know we returned, but we don't know what the value is. |
5316 | return ESR_Failed; |
5317 | } |
5318 | if (RetExpr && |
5319 | !(Result.Slot |
5320 | ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr) |
5321 | : Evaluate(Result&: Result.Value, Info, E: RetExpr))) |
5322 | return ESR_Failed; |
5323 | return Scope.destroy() ? ESR_Returned : ESR_Failed; |
5324 | } |
5325 | |
5326 | case Stmt::CompoundStmtClass: { |
5327 | BlockScopeRAII Scope(Info); |
5328 | |
5329 | const CompoundStmt *CS = cast<CompoundStmt>(Val: S); |
5330 | for (const auto *BI : CS->body()) { |
5331 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case); |
5332 | if (ESR == ESR_Succeeded) |
5333 | Case = nullptr; |
5334 | else if (ESR != ESR_CaseNotFound) { |
5335 | if (ESR != ESR_Failed && !Scope.destroy()) |
5336 | return ESR_Failed; |
5337 | return ESR; |
5338 | } |
5339 | } |
5340 | if (Case) |
5341 | return ESR_CaseNotFound; |
5342 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5343 | } |
5344 | |
5345 | case Stmt::IfStmtClass: { |
5346 | const IfStmt *IS = cast<IfStmt>(Val: S); |
5347 | |
5348 | // Evaluate the condition, as either a var decl or as an expression. |
5349 | BlockScopeRAII Scope(Info); |
5350 | if (const Stmt *Init = IS->getInit()) { |
5351 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init); |
5352 | if (ESR != ESR_Succeeded) { |
5353 | if (ESR != ESR_Failed && !Scope.destroy()) |
5354 | return ESR_Failed; |
5355 | return ESR; |
5356 | } |
5357 | } |
5358 | bool Cond; |
5359 | if (IS->isConsteval()) { |
5360 | Cond = IS->isNonNegatedConsteval(); |
5361 | // If we are not in a constant context, if consteval should not evaluate |
5362 | // to true. |
5363 | if (!Info.InConstantContext) |
5364 | Cond = !Cond; |
5365 | } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(), |
5366 | Result&: Cond)) |
5367 | return ESR_Failed; |
5368 | |
5369 | if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) { |
5370 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt); |
5371 | if (ESR != ESR_Succeeded) { |
5372 | if (ESR != ESR_Failed && !Scope.destroy()) |
5373 | return ESR_Failed; |
5374 | return ESR; |
5375 | } |
5376 | } |
5377 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5378 | } |
5379 | |
5380 | case Stmt::WhileStmtClass: { |
5381 | const WhileStmt *WS = cast<WhileStmt>(Val: S); |
5382 | while (true) { |
5383 | BlockScopeRAII Scope(Info); |
5384 | bool Continue; |
5385 | if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(), |
5386 | Result&: Continue)) |
5387 | return ESR_Failed; |
5388 | if (!Continue) |
5389 | break; |
5390 | |
5391 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody()); |
5392 | if (ESR != ESR_Continue) { |
5393 | if (ESR != ESR_Failed && !Scope.destroy()) |
5394 | return ESR_Failed; |
5395 | return ESR; |
5396 | } |
5397 | if (!Scope.destroy()) |
5398 | return ESR_Failed; |
5399 | } |
5400 | return ESR_Succeeded; |
5401 | } |
5402 | |
5403 | case Stmt::DoStmtClass: { |
5404 | const DoStmt *DS = cast<DoStmt>(Val: S); |
5405 | bool Continue; |
5406 | do { |
5407 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case); |
5408 | if (ESR != ESR_Continue) |
5409 | return ESR; |
5410 | Case = nullptr; |
5411 | |
5412 | if (DS->getCond()->isValueDependent()) { |
5413 | EvaluateDependentExpr(E: DS->getCond(), Info); |
5414 | // Bailout as we don't know whether to keep going or terminate the loop. |
5415 | return ESR_Failed; |
5416 | } |
5417 | FullExpressionRAII CondScope(Info); |
5418 | if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) || |
5419 | !CondScope.destroy()) |
5420 | return ESR_Failed; |
5421 | } while (Continue); |
5422 | return ESR_Succeeded; |
5423 | } |
5424 | |
5425 | case Stmt::ForStmtClass: { |
5426 | const ForStmt *FS = cast<ForStmt>(Val: S); |
5427 | BlockScopeRAII ForScope(Info); |
5428 | if (FS->getInit()) { |
5429 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5430 | if (ESR != ESR_Succeeded) { |
5431 | if (ESR != ESR_Failed && !ForScope.destroy()) |
5432 | return ESR_Failed; |
5433 | return ESR; |
5434 | } |
5435 | } |
5436 | while (true) { |
5437 | BlockScopeRAII IterScope(Info); |
5438 | bool Continue = true; |
5439 | if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(), |
5440 | Cond: FS->getCond(), Result&: Continue)) |
5441 | return ESR_Failed; |
5442 | if (!Continue) |
5443 | break; |
5444 | |
5445 | EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5446 | if (ESR != ESR_Continue) { |
5447 | if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy())) |
5448 | return ESR_Failed; |
5449 | return ESR; |
5450 | } |
5451 | |
5452 | if (const auto *Inc = FS->getInc()) { |
5453 | if (Inc->isValueDependent()) { |
5454 | if (!EvaluateDependentExpr(E: Inc, Info)) |
5455 | return ESR_Failed; |
5456 | } else { |
5457 | FullExpressionRAII IncScope(Info); |
5458 | if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy()) |
5459 | return ESR_Failed; |
5460 | } |
5461 | } |
5462 | |
5463 | if (!IterScope.destroy()) |
5464 | return ESR_Failed; |
5465 | } |
5466 | return ForScope.destroy() ? ESR_Succeeded : ESR_Failed; |
5467 | } |
5468 | |
5469 | case Stmt::CXXForRangeStmtClass: { |
5470 | const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S); |
5471 | BlockScopeRAII Scope(Info); |
5472 | |
5473 | // Evaluate the init-statement if present. |
5474 | if (FS->getInit()) { |
5475 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit()); |
5476 | if (ESR != ESR_Succeeded) { |
5477 | if (ESR != ESR_Failed && !Scope.destroy()) |
5478 | return ESR_Failed; |
5479 | return ESR; |
5480 | } |
5481 | } |
5482 | |
5483 | // Initialize the __range variable. |
5484 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt()); |
5485 | if (ESR != ESR_Succeeded) { |
5486 | if (ESR != ESR_Failed && !Scope.destroy()) |
5487 | return ESR_Failed; |
5488 | return ESR; |
5489 | } |
5490 | |
5491 | // In error-recovery cases it's possible to get here even if we failed to |
5492 | // synthesize the __begin and __end variables. |
5493 | if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond()) |
5494 | return ESR_Failed; |
5495 | |
5496 | // Create the __begin and __end iterators. |
5497 | ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt()); |
5498 | if (ESR != ESR_Succeeded) { |
5499 | if (ESR != ESR_Failed && !Scope.destroy()) |
5500 | return ESR_Failed; |
5501 | return ESR; |
5502 | } |
5503 | ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt()); |
5504 | if (ESR != ESR_Succeeded) { |
5505 | if (ESR != ESR_Failed && !Scope.destroy()) |
5506 | return ESR_Failed; |
5507 | return ESR; |
5508 | } |
5509 | |
5510 | while (true) { |
5511 | // Condition: __begin != __end. |
5512 | { |
5513 | if (FS->getCond()->isValueDependent()) { |
5514 | EvaluateDependentExpr(E: FS->getCond(), Info); |
5515 | // We don't know whether to keep going or terminate the loop. |
5516 | return ESR_Failed; |
5517 | } |
5518 | bool Continue = true; |
5519 | FullExpressionRAII CondExpr(Info); |
5520 | if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info)) |
5521 | return ESR_Failed; |
5522 | if (!Continue) |
5523 | break; |
5524 | } |
5525 | |
5526 | // User's variable declaration, initialized by *__begin. |
5527 | BlockScopeRAII InnerScope(Info); |
5528 | ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt()); |
5529 | if (ESR != ESR_Succeeded) { |
5530 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5531 | return ESR_Failed; |
5532 | return ESR; |
5533 | } |
5534 | |
5535 | // Loop body. |
5536 | ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody()); |
5537 | if (ESR != ESR_Continue) { |
5538 | if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy())) |
5539 | return ESR_Failed; |
5540 | return ESR; |
5541 | } |
5542 | if (FS->getInc()->isValueDependent()) { |
5543 | if (!EvaluateDependentExpr(E: FS->getInc(), Info)) |
5544 | return ESR_Failed; |
5545 | } else { |
5546 | // Increment: ++__begin |
5547 | if (!EvaluateIgnoredValue(Info, E: FS->getInc())) |
5548 | return ESR_Failed; |
5549 | } |
5550 | |
5551 | if (!InnerScope.destroy()) |
5552 | return ESR_Failed; |
5553 | } |
5554 | |
5555 | return Scope.destroy() ? ESR_Succeeded : ESR_Failed; |
5556 | } |
5557 | |
5558 | case Stmt::SwitchStmtClass: |
5559 | return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S)); |
5560 | |
5561 | case Stmt::ContinueStmtClass: |
5562 | return ESR_Continue; |
5563 | |
5564 | case Stmt::BreakStmtClass: |
5565 | return ESR_Break; |
5566 | |
5567 | case Stmt::LabelStmtClass: |
5568 | return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case); |
5569 | |
5570 | case Stmt::AttributedStmtClass: { |
5571 | const auto *AS = cast<AttributedStmt>(Val: S); |
5572 | const auto *SS = AS->getSubStmt(); |
5573 | MSConstexprContextRAII ConstexprContext( |
5574 | *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(AS->getAttrs()) && |
5575 | isa<ReturnStmt>(SS)); |
5576 | return EvaluateStmt(Result, Info, S: SS, Case); |
5577 | } |
5578 | |
5579 | case Stmt::CaseStmtClass: |
5580 | case Stmt::DefaultStmtClass: |
5581 | return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case); |
5582 | case Stmt::CXXTryStmtClass: |
5583 | // Evaluate try blocks by evaluating all sub statements. |
5584 | return EvaluateStmt(Result, Info, cast<CXXTryStmt>(Val: S)->getTryBlock(), Case); |
5585 | } |
5586 | } |
5587 | |
5588 | /// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial |
5589 | /// default constructor. If so, we'll fold it whether or not it's marked as |
5590 | /// constexpr. If it is marked as constexpr, we will never implicitly define it, |
5591 | /// so we need special handling. |
5592 | static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc, |
5593 | const CXXConstructorDecl *CD, |
5594 | bool IsValueInitialization) { |
5595 | if (!CD->isTrivial() || !CD->isDefaultConstructor()) |
5596 | return false; |
5597 | |
5598 | // Value-initialization does not call a trivial default constructor, so such a |
5599 | // call is a core constant expression whether or not the constructor is |
5600 | // constexpr. |
5601 | if (!CD->isConstexpr() && !IsValueInitialization) { |
5602 | if (Info.getLangOpts().CPlusPlus11) { |
5603 | // FIXME: If DiagDecl is an implicitly-declared special member function, |
5604 | // we should be much more explicit about why it's not constexpr. |
5605 | Info.CCEDiag(Loc, diag::note_constexpr_invalid_function, 1) |
5606 | << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD; |
5607 | Info.Note(CD->getLocation(), diag::note_declared_at); |
5608 | } else { |
5609 | Info.CCEDiag(Loc, diag::note_invalid_subexpr_in_const_expr); |
5610 | } |
5611 | } |
5612 | return true; |
5613 | } |
5614 | |
5615 | /// CheckConstexprFunction - Check that a function can be called in a constant |
5616 | /// expression. |
5617 | static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc, |
5618 | const FunctionDecl *Declaration, |
5619 | const FunctionDecl *Definition, |
5620 | const Stmt *Body) { |
5621 | // Potential constant expressions can contain calls to declared, but not yet |
5622 | // defined, constexpr functions. |
5623 | if (Info.checkingPotentialConstantExpression() && !Definition && |
5624 | Declaration->isConstexpr()) |
5625 | return false; |
5626 | |
5627 | // Bail out if the function declaration itself is invalid. We will |
5628 | // have produced a relevant diagnostic while parsing it, so just |
5629 | // note the problematic sub-expression. |
5630 | if (Declaration->isInvalidDecl()) { |
5631 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5632 | return false; |
5633 | } |
5634 | |
5635 | // DR1872: An instantiated virtual constexpr function can't be called in a |
5636 | // constant expression (prior to C++20). We can still constant-fold such a |
5637 | // call. |
5638 | if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Declaration) && |
5639 | cast<CXXMethodDecl>(Declaration)->isVirtual()) |
5640 | Info.CCEDiag(CallLoc, diag::note_constexpr_virtual_call); |
5641 | |
5642 | if (Definition && Definition->isInvalidDecl()) { |
5643 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5644 | return false; |
5645 | } |
5646 | |
5647 | // Can we evaluate this function call? |
5648 | if (Definition && Body && |
5649 | (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr && |
5650 | Definition->hasAttr<MSConstexprAttr>()))) |
5651 | return true; |
5652 | |
5653 | if (Info.getLangOpts().CPlusPlus11) { |
5654 | const FunctionDecl *DiagDecl = Definition ? Definition : Declaration; |
5655 | |
5656 | // If this function is not constexpr because it is an inherited |
5657 | // non-constexpr constructor, diagnose that directly. |
5658 | auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl); |
5659 | if (CD && CD->isInheritingConstructor()) { |
5660 | auto *Inherited = CD->getInheritedConstructor().getConstructor(); |
5661 | if (!Inherited->isConstexpr()) |
5662 | DiagDecl = CD = Inherited; |
5663 | } |
5664 | |
5665 | // FIXME: If DiagDecl is an implicitly-declared special member function |
5666 | // or an inheriting constructor, we should be much more explicit about why |
5667 | // it's not constexpr. |
5668 | if (CD && CD->isInheritingConstructor()) |
5669 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_inhctor, 1) |
5670 | << CD->getInheritedConstructor().getConstructor()->getParent(); |
5671 | else |
5672 | Info.FFDiag(CallLoc, diag::note_constexpr_invalid_function, 1) |
5673 | << DiagDecl->isConstexpr() << (bool)CD << DiagDecl; |
5674 | Info.Note(DiagDecl->getLocation(), diag::note_declared_at); |
5675 | } else { |
5676 | Info.FFDiag(CallLoc, diag::note_invalid_subexpr_in_const_expr); |
5677 | } |
5678 | return false; |
5679 | } |
5680 | |
5681 | namespace { |
5682 | struct CheckDynamicTypeHandler { |
5683 | AccessKinds AccessKind; |
5684 | typedef bool result_type; |
5685 | bool failed() { return false; } |
5686 | bool found(APValue &Subobj, QualType SubobjType) { return true; } |
5687 | bool found(APSInt &Value, QualType SubobjType) { return true; } |
5688 | bool found(APFloat &Value, QualType SubobjType) { return true; } |
5689 | }; |
5690 | } // end anonymous namespace |
5691 | |
5692 | /// Check that we can access the notional vptr of an object / determine its |
5693 | /// dynamic type. |
5694 | static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This, |
5695 | AccessKinds AK, bool Polymorphic) { |
5696 | if (This.Designator.Invalid) |
5697 | return false; |
5698 | |
5699 | CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType()); |
5700 | |
5701 | if (!Obj) |
5702 | return false; |
5703 | |
5704 | if (!Obj.Value) { |
5705 | // The object is not usable in constant expressions, so we can't inspect |
5706 | // its value to see if it's in-lifetime or what the active union members |
5707 | // are. We can still check for a one-past-the-end lvalue. |
5708 | if (This.Designator.isOnePastTheEnd() || |
5709 | This.Designator.isMostDerivedAnUnsizedArray()) { |
5710 | Info.FFDiag(E, This.Designator.isOnePastTheEnd() |
5711 | ? diag::note_constexpr_access_past_end |
5712 | : diag::note_constexpr_access_unsized_array) |
5713 | << AK; |
5714 | return false; |
5715 | } else if (Polymorphic) { |
5716 | // Conservatively refuse to perform a polymorphic operation if we would |
5717 | // not be able to read a notional 'vptr' value. |
5718 | APValue Val; |
5719 | This.moveInto(V&: Val); |
5720 | QualType StarThisType = |
5721 | Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Info.Ctx)); |
5722 | Info.FFDiag(E, diag::note_constexpr_polymorphic_unknown_dynamic_type) |
5723 | << AK << Val.getAsString(Info.Ctx, StarThisType); |
5724 | return false; |
5725 | } |
5726 | return true; |
5727 | } |
5728 | |
5729 | CheckDynamicTypeHandler Handler{.AccessKind: AK}; |
5730 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
5731 | } |
5732 | |
5733 | /// Check that the pointee of the 'this' pointer in a member function call is |
5734 | /// either within its lifetime or in its period of construction or destruction. |
5735 | static bool |
5736 | checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E, |
5737 | const LValue &This, |
5738 | const CXXMethodDecl *NamedMember) { |
5739 | return checkDynamicType( |
5740 | Info, E, This, |
5741 | AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false); |
5742 | } |
5743 | |
5744 | struct DynamicType { |
5745 | /// The dynamic class type of the object. |
5746 | const CXXRecordDecl *Type; |
5747 | /// The corresponding path length in the lvalue. |
5748 | unsigned PathLength; |
5749 | }; |
5750 | |
5751 | static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator, |
5752 | unsigned PathLength) { |
5753 | assert(PathLength >= Designator.MostDerivedPathLength && PathLength <= |
5754 | Designator.Entries.size() && "invalid path length" ); |
5755 | return (PathLength == Designator.MostDerivedPathLength) |
5756 | ? Designator.MostDerivedType->getAsCXXRecordDecl() |
5757 | : getAsBaseClass(E: Designator.Entries[PathLength - 1]); |
5758 | } |
5759 | |
5760 | /// Determine the dynamic type of an object. |
5761 | static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info, |
5762 | const Expr *E, |
5763 | LValue &This, |
5764 | AccessKinds AK) { |
5765 | // If we don't have an lvalue denoting an object of class type, there is no |
5766 | // meaningful dynamic type. (We consider objects of non-class type to have no |
5767 | // dynamic type.) |
5768 | if (!checkDynamicType(Info, E, This, AK, Polymorphic: true)) |
5769 | return std::nullopt; |
5770 | |
5771 | // Refuse to compute a dynamic type in the presence of virtual bases. This |
5772 | // shouldn't happen other than in constant-folding situations, since literal |
5773 | // types can't have virtual bases. |
5774 | // |
5775 | // Note that consumers of DynamicType assume that the type has no virtual |
5776 | // bases, and will need modifications if this restriction is relaxed. |
5777 | const CXXRecordDecl *Class = |
5778 | This.Designator.MostDerivedType->getAsCXXRecordDecl(); |
5779 | if (!Class || Class->getNumVBases()) { |
5780 | Info.FFDiag(E); |
5781 | return std::nullopt; |
5782 | } |
5783 | |
5784 | // FIXME: For very deep class hierarchies, it might be beneficial to use a |
5785 | // binary search here instead. But the overwhelmingly common case is that |
5786 | // we're not in the middle of a constructor, so it probably doesn't matter |
5787 | // in practice. |
5788 | ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries; |
5789 | for (unsigned PathLength = This.Designator.MostDerivedPathLength; |
5790 | PathLength <= Path.size(); ++PathLength) { |
5791 | switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(), |
5792 | Path: Path.slice(N: 0, M: PathLength))) { |
5793 | case ConstructionPhase::Bases: |
5794 | case ConstructionPhase::DestroyingBases: |
5795 | // We're constructing or destroying a base class. This is not the dynamic |
5796 | // type. |
5797 | break; |
5798 | |
5799 | case ConstructionPhase::None: |
5800 | case ConstructionPhase::AfterBases: |
5801 | case ConstructionPhase::AfterFields: |
5802 | case ConstructionPhase::Destroying: |
5803 | // We've finished constructing the base classes and not yet started |
5804 | // destroying them again, so this is the dynamic type. |
5805 | return DynamicType{getBaseClassType(This.Designator, PathLength), |
5806 | PathLength}; |
5807 | } |
5808 | } |
5809 | |
5810 | // CWG issue 1517: we're constructing a base class of the object described by |
5811 | // 'This', so that object has not yet begun its period of construction and |
5812 | // any polymorphic operation on it results in undefined behavior. |
5813 | Info.FFDiag(E); |
5814 | return std::nullopt; |
5815 | } |
5816 | |
5817 | /// Perform virtual dispatch. |
5818 | static const CXXMethodDecl *HandleVirtualDispatch( |
5819 | EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found, |
5820 | llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) { |
5821 | std::optional<DynamicType> DynType = ComputeDynamicType( |
5822 | Info, E, This, |
5823 | AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall); |
5824 | if (!DynType) |
5825 | return nullptr; |
5826 | |
5827 | // Find the final overrider. It must be declared in one of the classes on the |
5828 | // path from the dynamic type to the static type. |
5829 | // FIXME: If we ever allow literal types to have virtual base classes, that |
5830 | // won't be true. |
5831 | const CXXMethodDecl *Callee = Found; |
5832 | unsigned PathLength = DynType->PathLength; |
5833 | for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) { |
5834 | const CXXRecordDecl *Class = getBaseClassType(This.Designator, PathLength); |
5835 | const CXXMethodDecl *Overrider = |
5836 | Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false); |
5837 | if (Overrider) { |
5838 | Callee = Overrider; |
5839 | break; |
5840 | } |
5841 | } |
5842 | |
5843 | // C++2a [class.abstract]p6: |
5844 | // the effect of making a virtual call to a pure virtual function [...] is |
5845 | // undefined |
5846 | if (Callee->isPureVirtual()) { |
5847 | Info.FFDiag(E, diag::note_constexpr_pure_virtual_call, 1) << Callee; |
5848 | Info.Note(Callee->getLocation(), diag::note_declared_at); |
5849 | return nullptr; |
5850 | } |
5851 | |
5852 | // If necessary, walk the rest of the path to determine the sequence of |
5853 | // covariant adjustment steps to apply. |
5854 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(), |
5855 | T2: Found->getReturnType())) { |
5856 | CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType()); |
5857 | for (unsigned CovariantPathLength = PathLength + 1; |
5858 | CovariantPathLength != This.Designator.Entries.size(); |
5859 | ++CovariantPathLength) { |
5860 | const CXXRecordDecl *NextClass = |
5861 | getBaseClassType(This.Designator, CovariantPathLength); |
5862 | const CXXMethodDecl *Next = |
5863 | Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false); |
5864 | if (Next && !Info.Ctx.hasSameUnqualifiedType( |
5865 | T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back())) |
5866 | CovariantAdjustmentPath.push_back(Elt: Next->getReturnType()); |
5867 | } |
5868 | if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(), |
5869 | T2: CovariantAdjustmentPath.back())) |
5870 | CovariantAdjustmentPath.push_back(Elt: Found->getReturnType()); |
5871 | } |
5872 | |
5873 | // Perform 'this' adjustment. |
5874 | if (!CastToDerivedClass(Info, E, This, Callee->getParent(), PathLength)) |
5875 | return nullptr; |
5876 | |
5877 | return Callee; |
5878 | } |
5879 | |
5880 | /// Perform the adjustment from a value returned by a virtual function to |
5881 | /// a value of the statically expected type, which may be a pointer or |
5882 | /// reference to a base class of the returned type. |
5883 | static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E, |
5884 | APValue &Result, |
5885 | ArrayRef<QualType> Path) { |
5886 | assert(Result.isLValue() && |
5887 | "unexpected kind of APValue for covariant return" ); |
5888 | if (Result.isNullPointer()) |
5889 | return true; |
5890 | |
5891 | LValue LVal; |
5892 | LVal.setFrom(Ctx&: Info.Ctx, V: Result); |
5893 | |
5894 | const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl(); |
5895 | for (unsigned I = 1; I != Path.size(); ++I) { |
5896 | const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl(); |
5897 | assert(OldClass && NewClass && "unexpected kind of covariant return" ); |
5898 | if (OldClass != NewClass && |
5899 | !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass)) |
5900 | return false; |
5901 | OldClass = NewClass; |
5902 | } |
5903 | |
5904 | LVal.moveInto(V&: Result); |
5905 | return true; |
5906 | } |
5907 | |
5908 | /// Determine whether \p Base, which is known to be a direct base class of |
5909 | /// \p Derived, is a public base class. |
5910 | static bool isBaseClassPublic(const CXXRecordDecl *Derived, |
5911 | const CXXRecordDecl *Base) { |
5912 | for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) { |
5913 | auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl(); |
5914 | if (BaseClass && declaresSameEntity(BaseClass, Base)) |
5915 | return BaseSpec.getAccessSpecifier() == AS_public; |
5916 | } |
5917 | llvm_unreachable("Base is not a direct base of Derived" ); |
5918 | } |
5919 | |
5920 | /// Apply the given dynamic cast operation on the provided lvalue. |
5921 | /// |
5922 | /// This implements the hard case of dynamic_cast, requiring a "runtime check" |
5923 | /// to find a suitable target subobject. |
5924 | static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E, |
5925 | LValue &Ptr) { |
5926 | // We can't do anything with a non-symbolic pointer value. |
5927 | SubobjectDesignator &D = Ptr.Designator; |
5928 | if (D.Invalid) |
5929 | return false; |
5930 | |
5931 | // C++ [expr.dynamic.cast]p6: |
5932 | // If v is a null pointer value, the result is a null pointer value. |
5933 | if (Ptr.isNullPointer() && !E->isGLValue()) |
5934 | return true; |
5935 | |
5936 | // For all the other cases, we need the pointer to point to an object within |
5937 | // its lifetime / period of construction / destruction, and we need to know |
5938 | // its dynamic type. |
5939 | std::optional<DynamicType> DynType = |
5940 | ComputeDynamicType(Info, E, Ptr, AK_DynamicCast); |
5941 | if (!DynType) |
5942 | return false; |
5943 | |
5944 | // C++ [expr.dynamic.cast]p7: |
5945 | // If T is "pointer to cv void", then the result is a pointer to the most |
5946 | // derived object |
5947 | if (E->getType()->isVoidPointerType()) |
5948 | return CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength); |
5949 | |
5950 | const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl(); |
5951 | assert(C && "dynamic_cast target is not void pointer nor class" ); |
5952 | CanQualType CQT = Info.Ctx.getCanonicalType(T: Info.Ctx.getRecordType(C)); |
5953 | |
5954 | auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) { |
5955 | // C++ [expr.dynamic.cast]p9: |
5956 | if (!E->isGLValue()) { |
5957 | // The value of a failed cast to pointer type is the null pointer value |
5958 | // of the required result type. |
5959 | Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
5960 | return true; |
5961 | } |
5962 | |
5963 | // A failed cast to reference type throws [...] std::bad_cast. |
5964 | unsigned DiagKind; |
5965 | if (!Paths && (declaresSameEntity(DynType->Type, C) || |
5966 | DynType->Type->isDerivedFrom(Base: C))) |
5967 | DiagKind = 0; |
5968 | else if (!Paths || Paths->begin() == Paths->end()) |
5969 | DiagKind = 1; |
5970 | else if (Paths->isAmbiguous(BaseType: CQT)) |
5971 | DiagKind = 2; |
5972 | else { |
5973 | assert(Paths->front().Access != AS_public && "why did the cast fail?" ); |
5974 | DiagKind = 3; |
5975 | } |
5976 | Info.FFDiag(E, diag::note_constexpr_dynamic_cast_to_reference_failed) |
5977 | << DiagKind << Ptr.Designator.getType(Info.Ctx) |
5978 | << Info.Ctx.getRecordType(DynType->Type) |
5979 | << E->getType().getUnqualifiedType(); |
5980 | return false; |
5981 | }; |
5982 | |
5983 | // Runtime check, phase 1: |
5984 | // Walk from the base subobject towards the derived object looking for the |
5985 | // target type. |
5986 | for (int PathLength = Ptr.Designator.Entries.size(); |
5987 | PathLength >= (int)DynType->PathLength; --PathLength) { |
5988 | const CXXRecordDecl *Class = getBaseClassType(Ptr.Designator, PathLength); |
5989 | if (declaresSameEntity(Class, C)) |
5990 | return CastToDerivedClass(Info, E, Ptr, Class, PathLength); |
5991 | // We can only walk across public inheritance edges. |
5992 | if (PathLength > (int)DynType->PathLength && |
5993 | !isBaseClassPublic(getBaseClassType(Ptr.Designator, PathLength - 1), |
5994 | Class)) |
5995 | return RuntimeCheckFailed(nullptr); |
5996 | } |
5997 | |
5998 | // Runtime check, phase 2: |
5999 | // Search the dynamic type for an unambiguous public base of type C. |
6000 | CXXBasePaths Paths(/*FindAmbiguities=*/true, |
6001 | /*RecordPaths=*/true, /*DetectVirtual=*/false); |
6002 | if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) && |
6003 | Paths.front().Access == AS_public) { |
6004 | // Downcast to the dynamic type... |
6005 | if (!CastToDerivedClass(Info, E, Ptr, DynType->Type, DynType->PathLength)) |
6006 | return false; |
6007 | // ... then upcast to the chosen base class subobject. |
6008 | for (CXXBasePathElement &Elem : Paths.front()) |
6009 | if (!HandleLValueBase(Info, E, Ptr, Elem.Class, Elem.Base)) |
6010 | return false; |
6011 | return true; |
6012 | } |
6013 | |
6014 | // Otherwise, the runtime check fails. |
6015 | return RuntimeCheckFailed(&Paths); |
6016 | } |
6017 | |
6018 | namespace { |
6019 | struct StartLifetimeOfUnionMemberHandler { |
6020 | EvalInfo &Info; |
6021 | const Expr *LHSExpr; |
6022 | const FieldDecl *Field; |
6023 | bool DuringInit; |
6024 | bool Failed = false; |
6025 | static const AccessKinds AccessKind = AK_Assign; |
6026 | |
6027 | typedef bool result_type; |
6028 | bool failed() { return Failed; } |
6029 | bool found(APValue &Subobj, QualType SubobjType) { |
6030 | // We are supposed to perform no initialization but begin the lifetime of |
6031 | // the object. We interpret that as meaning to do what default |
6032 | // initialization of the object would do if all constructors involved were |
6033 | // trivial: |
6034 | // * All base, non-variant member, and array element subobjects' lifetimes |
6035 | // begin |
6036 | // * No variant members' lifetimes begin |
6037 | // * All scalar subobjects whose lifetimes begin have indeterminate values |
6038 | assert(SubobjType->isUnionType()); |
6039 | if (declaresSameEntity(Subobj.getUnionField(), Field)) { |
6040 | // This union member is already active. If it's also in-lifetime, there's |
6041 | // nothing to do. |
6042 | if (Subobj.getUnionValue().hasValue()) |
6043 | return true; |
6044 | } else if (DuringInit) { |
6045 | // We're currently in the process of initializing a different union |
6046 | // member. If we carried on, that initialization would attempt to |
6047 | // store to an inactive union member, resulting in undefined behavior. |
6048 | Info.FFDiag(LHSExpr, |
6049 | diag::note_constexpr_union_member_change_during_init); |
6050 | return false; |
6051 | } |
6052 | APValue Result; |
6053 | Failed = !handleDefaultInitValue(Field->getType(), Result); |
6054 | Subobj.setUnion(Field, Value: Result); |
6055 | return true; |
6056 | } |
6057 | bool found(APSInt &Value, QualType SubobjType) { |
6058 | llvm_unreachable("wrong value kind for union object" ); |
6059 | } |
6060 | bool found(APFloat &Value, QualType SubobjType) { |
6061 | llvm_unreachable("wrong value kind for union object" ); |
6062 | } |
6063 | }; |
6064 | } // end anonymous namespace |
6065 | |
6066 | const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind; |
6067 | |
6068 | /// Handle a builtin simple-assignment or a call to a trivial assignment |
6069 | /// operator whose left-hand side might involve a union member access. If it |
6070 | /// does, implicitly start the lifetime of any accessed union elements per |
6071 | /// C++20 [class.union]5. |
6072 | static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info, |
6073 | const Expr *LHSExpr, |
6074 | const LValue &LHS) { |
6075 | if (LHS.InvalidBase || LHS.Designator.Invalid) |
6076 | return false; |
6077 | |
6078 | llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths; |
6079 | // C++ [class.union]p5: |
6080 | // define the set S(E) of subexpressions of E as follows: |
6081 | unsigned PathLength = LHS.Designator.Entries.size(); |
6082 | for (const Expr *E = LHSExpr; E != nullptr;) { |
6083 | // -- If E is of the form A.B, S(E) contains the elements of S(A)... |
6084 | if (auto *ME = dyn_cast<MemberExpr>(Val: E)) { |
6085 | auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl()); |
6086 | // Note that we can't implicitly start the lifetime of a reference, |
6087 | // so we don't need to proceed any further if we reach one. |
6088 | if (!FD || FD->getType()->isReferenceType()) |
6089 | break; |
6090 | |
6091 | // ... and also contains A.B if B names a union member ... |
6092 | if (FD->getParent()->isUnion()) { |
6093 | // ... of a non-class, non-array type, or of a class type with a |
6094 | // trivial default constructor that is not deleted, or an array of |
6095 | // such types. |
6096 | auto *RD = |
6097 | FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
6098 | if (!RD || RD->hasTrivialDefaultConstructor()) |
6099 | UnionPathLengths.push_back(Elt: {PathLength - 1, FD}); |
6100 | } |
6101 | |
6102 | E = ME->getBase(); |
6103 | --PathLength; |
6104 | assert(declaresSameEntity(FD, |
6105 | LHS.Designator.Entries[PathLength] |
6106 | .getAsBaseOrMember().getPointer())); |
6107 | |
6108 | // -- If E is of the form A[B] and is interpreted as a built-in array |
6109 | // subscripting operator, S(E) is [S(the array operand, if any)]. |
6110 | } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) { |
6111 | // Step over an ArrayToPointerDecay implicit cast. |
6112 | auto *Base = ASE->getBase()->IgnoreImplicit(); |
6113 | if (!Base->getType()->isArrayType()) |
6114 | break; |
6115 | |
6116 | E = Base; |
6117 | --PathLength; |
6118 | |
6119 | } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) { |
6120 | // Step over a derived-to-base conversion. |
6121 | E = ICE->getSubExpr(); |
6122 | if (ICE->getCastKind() == CK_NoOp) |
6123 | continue; |
6124 | if (ICE->getCastKind() != CK_DerivedToBase && |
6125 | ICE->getCastKind() != CK_UncheckedDerivedToBase) |
6126 | break; |
6127 | // Walk path backwards as we walk up from the base to the derived class. |
6128 | for (const CXXBaseSpecifier *Elt : llvm::reverse(ICE->path())) { |
6129 | if (Elt->isVirtual()) { |
6130 | // A class with virtual base classes never has a trivial default |
6131 | // constructor, so S(E) is empty in this case. |
6132 | E = nullptr; |
6133 | break; |
6134 | } |
6135 | |
6136 | --PathLength; |
6137 | assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(), |
6138 | LHS.Designator.Entries[PathLength] |
6139 | .getAsBaseOrMember().getPointer())); |
6140 | } |
6141 | |
6142 | // -- Otherwise, S(E) is empty. |
6143 | } else { |
6144 | break; |
6145 | } |
6146 | } |
6147 | |
6148 | // Common case: no unions' lifetimes are started. |
6149 | if (UnionPathLengths.empty()) |
6150 | return true; |
6151 | |
6152 | // if modification of X [would access an inactive union member], an object |
6153 | // of the type of X is implicitly created |
6154 | CompleteObject Obj = |
6155 | findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType()); |
6156 | if (!Obj) |
6157 | return false; |
6158 | for (std::pair<unsigned, const FieldDecl *> LengthAndField : |
6159 | llvm::reverse(C&: UnionPathLengths)) { |
6160 | // Form a designator for the union object. |
6161 | SubobjectDesignator D = LHS.Designator; |
6162 | D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first); |
6163 | |
6164 | bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) == |
6165 | ConstructionPhase::AfterBases; |
6166 | StartLifetimeOfUnionMemberHandler StartLifetime{ |
6167 | .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit}; |
6168 | if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime)) |
6169 | return false; |
6170 | } |
6171 | |
6172 | return true; |
6173 | } |
6174 | |
6175 | static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg, |
6176 | CallRef Call, EvalInfo &Info, |
6177 | bool NonNull = false) { |
6178 | LValue LV; |
6179 | // Create the parameter slot and register its destruction. For a vararg |
6180 | // argument, create a temporary. |
6181 | // FIXME: For calling conventions that destroy parameters in the callee, |
6182 | // should we consider performing destruction when the function returns |
6183 | // instead? |
6184 | APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV) |
6185 | : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(), |
6186 | Scope: ScopeKind::Call, LV); |
6187 | if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg)) |
6188 | return false; |
6189 | |
6190 | // Passing a null pointer to an __attribute__((nonnull)) parameter results in |
6191 | // undefined behavior, so is non-constant. |
6192 | if (NonNull && V.isLValue() && V.isNullPointer()) { |
6193 | Info.CCEDiag(Arg, diag::note_non_null_attribute_failed); |
6194 | return false; |
6195 | } |
6196 | |
6197 | return true; |
6198 | } |
6199 | |
6200 | /// Evaluate the arguments to a function call. |
6201 | static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call, |
6202 | EvalInfo &Info, const FunctionDecl *Callee, |
6203 | bool RightToLeft = false) { |
6204 | bool Success = true; |
6205 | llvm::SmallBitVector ForbiddenNullArgs; |
6206 | if (Callee->hasAttr<NonNullAttr>()) { |
6207 | ForbiddenNullArgs.resize(N: Args.size()); |
6208 | for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) { |
6209 | if (!Attr->args_size()) { |
6210 | ForbiddenNullArgs.set(); |
6211 | break; |
6212 | } else |
6213 | for (auto Idx : Attr->args()) { |
6214 | unsigned ASTIdx = Idx.getASTIndex(); |
6215 | if (ASTIdx >= Args.size()) |
6216 | continue; |
6217 | ForbiddenNullArgs[ASTIdx] = true; |
6218 | } |
6219 | } |
6220 | } |
6221 | for (unsigned I = 0; I < Args.size(); I++) { |
6222 | unsigned Idx = RightToLeft ? Args.size() - I - 1 : I; |
6223 | const ParmVarDecl *PVD = |
6224 | Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr; |
6225 | bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx]; |
6226 | if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull)) { |
6227 | // If we're checking for a potential constant expression, evaluate all |
6228 | // initializers even if some of them fail. |
6229 | if (!Info.noteFailure()) |
6230 | return false; |
6231 | Success = false; |
6232 | } |
6233 | } |
6234 | return Success; |
6235 | } |
6236 | |
6237 | /// Perform a trivial copy from Param, which is the parameter of a copy or move |
6238 | /// constructor or assignment operator. |
6239 | static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param, |
6240 | const Expr *E, APValue &Result, |
6241 | bool CopyObjectRepresentation) { |
6242 | // Find the reference argument. |
6243 | CallStackFrame *Frame = Info.CurrentCall; |
6244 | APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param); |
6245 | if (!RefValue) { |
6246 | Info.FFDiag(E); |
6247 | return false; |
6248 | } |
6249 | |
6250 | // Copy out the contents of the RHS object. |
6251 | LValue RefLValue; |
6252 | RefLValue.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
6253 | return handleLValueToRValueConversion( |
6254 | Info, E, Param->getType().getNonReferenceType(), RefLValue, Result, |
6255 | CopyObjectRepresentation); |
6256 | } |
6257 | |
6258 | /// Evaluate a function call. |
6259 | static bool HandleFunctionCall(SourceLocation CallLoc, |
6260 | const FunctionDecl *Callee, const LValue *This, |
6261 | const Expr *E, ArrayRef<const Expr *> Args, |
6262 | CallRef Call, const Stmt *Body, EvalInfo &Info, |
6263 | APValue &Result, const LValue *ResultSlot) { |
6264 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6265 | return false; |
6266 | |
6267 | CallStackFrame Frame(Info, E->getSourceRange(), Callee, This, E, Call); |
6268 | |
6269 | // For a trivial copy or move assignment, perform an APValue copy. This is |
6270 | // essential for unions, where the operations performed by the assignment |
6271 | // operator cannot be represented as statements. |
6272 | // |
6273 | // Skip this for non-union classes with no fields; in that case, the defaulted |
6274 | // copy/move does not actually read the object. |
6275 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
6276 | if (MD && MD->isDefaulted() && |
6277 | (MD->getParent()->isUnion() || |
6278 | (MD->isTrivial() && |
6279 | isReadByLvalueToRvalueConversion(RD: MD->getParent())))) { |
6280 | assert(This && |
6281 | (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())); |
6282 | APValue RHSValue; |
6283 | if (!handleTrivialCopy(Info, MD->getParamDecl(0), Args[0], RHSValue, |
6284 | MD->getParent()->isUnion())) |
6285 | return false; |
6286 | if (!handleAssignment(Info, E: Args[0], LVal: *This, LValType: MD->getThisType(), |
6287 | Val&: RHSValue)) |
6288 | return false; |
6289 | This->moveInto(V&: Result); |
6290 | return true; |
6291 | } else if (MD && isLambdaCallOperator(MD)) { |
6292 | // We're in a lambda; determine the lambda capture field maps unless we're |
6293 | // just constexpr checking a lambda's call operator. constexpr checking is |
6294 | // done before the captures have been added to the closure object (unless |
6295 | // we're inferring constexpr-ness), so we don't have access to them in this |
6296 | // case. But since we don't need the captures to constexpr check, we can |
6297 | // just ignore them. |
6298 | if (!Info.checkingPotentialConstantExpression()) |
6299 | MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields, |
6300 | ThisCapture&: Frame.LambdaThisCaptureField); |
6301 | } |
6302 | |
6303 | StmtResult Ret = {.Value: Result, .Slot: ResultSlot}; |
6304 | EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body); |
6305 | if (ESR == ESR_Succeeded) { |
6306 | if (Callee->getReturnType()->isVoidType()) |
6307 | return true; |
6308 | Info.FFDiag(Callee->getEndLoc(), diag::note_constexpr_no_return); |
6309 | } |
6310 | return ESR == ESR_Returned; |
6311 | } |
6312 | |
6313 | /// Evaluate a constructor call. |
6314 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6315 | CallRef Call, |
6316 | const CXXConstructorDecl *Definition, |
6317 | EvalInfo &Info, APValue &Result) { |
6318 | SourceLocation CallLoc = E->getExprLoc(); |
6319 | if (!Info.CheckCallLimit(Loc: CallLoc)) |
6320 | return false; |
6321 | |
6322 | const CXXRecordDecl *RD = Definition->getParent(); |
6323 | if (RD->getNumVBases()) { |
6324 | Info.FFDiag(CallLoc, diag::note_constexpr_virtual_base) << RD; |
6325 | return false; |
6326 | } |
6327 | |
6328 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
6329 | Info, |
6330 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
6331 | RD->getNumBases()); |
6332 | CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call); |
6333 | |
6334 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
6335 | // wasteful. |
6336 | APValue RetVal; |
6337 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
6338 | |
6339 | // If it's a delegating constructor, delegate. |
6340 | if (Definition->isDelegatingConstructor()) { |
6341 | CXXConstructorDecl::init_const_iterator I = Definition->init_begin(); |
6342 | if ((*I)->getInit()->isValueDependent()) { |
6343 | if (!EvaluateDependentExpr(E: (*I)->getInit(), Info)) |
6344 | return false; |
6345 | } else { |
6346 | FullExpressionRAII InitScope(Info); |
6347 | if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) || |
6348 | !InitScope.destroy()) |
6349 | return false; |
6350 | } |
6351 | return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed; |
6352 | } |
6353 | |
6354 | // For a trivial copy or move constructor, perform an APValue copy. This is |
6355 | // essential for unions (or classes with anonymous union members), where the |
6356 | // operations performed by the constructor cannot be represented by |
6357 | // ctor-initializers. |
6358 | // |
6359 | // Skip this for empty non-union classes; we should not perform an |
6360 | // lvalue-to-rvalue conversion on them because their copy constructor does not |
6361 | // actually read them. |
6362 | if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() && |
6363 | (Definition->getParent()->isUnion() || |
6364 | (Definition->isTrivial() && |
6365 | isReadByLvalueToRvalueConversion(Definition->getParent())))) { |
6366 | return handleTrivialCopy(Info, Definition->getParamDecl(0), E, Result, |
6367 | Definition->getParent()->isUnion()); |
6368 | } |
6369 | |
6370 | // Reserve space for the struct members. |
6371 | if (!Result.hasValue()) { |
6372 | if (!RD->isUnion()) |
6373 | Result = APValue(APValue::UninitStruct(), RD->getNumBases(), |
6374 | std::distance(RD->field_begin(), RD->field_end())); |
6375 | else |
6376 | // A union starts with no active member. |
6377 | Result = APValue((const FieldDecl*)nullptr); |
6378 | } |
6379 | |
6380 | if (RD->isInvalidDecl()) return false; |
6381 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6382 | |
6383 | // A scope for temporaries lifetime-extended by reference members. |
6384 | BlockScopeRAII LifetimeExtendedScope(Info); |
6385 | |
6386 | bool Success = true; |
6387 | unsigned BasesSeen = 0; |
6388 | #ifndef NDEBUG |
6389 | CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin(); |
6390 | #endif |
6391 | CXXRecordDecl::field_iterator FieldIt = RD->field_begin(); |
6392 | auto SkipToField = [&](FieldDecl *FD, bool Indirect) { |
6393 | // We might be initializing the same field again if this is an indirect |
6394 | // field initialization. |
6395 | if (FieldIt == RD->field_end() || |
6396 | FieldIt->getFieldIndex() > FD->getFieldIndex()) { |
6397 | assert(Indirect && "fields out of order?" ); |
6398 | return; |
6399 | } |
6400 | |
6401 | // Default-initialize any fields with no explicit initializer. |
6402 | for (; !declaresSameEntity(*FieldIt, FD); ++FieldIt) { |
6403 | assert(FieldIt != RD->field_end() && "missing field?" ); |
6404 | if (!FieldIt->isUnnamedBitfield()) |
6405 | Success &= handleDefaultInitValue( |
6406 | FieldIt->getType(), |
6407 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6408 | } |
6409 | ++FieldIt; |
6410 | }; |
6411 | for (const auto *I : Definition->inits()) { |
6412 | LValue Subobject = This; |
6413 | LValue SubobjectParent = This; |
6414 | APValue *Value = &Result; |
6415 | |
6416 | // Determine the subobject to initialize. |
6417 | FieldDecl *FD = nullptr; |
6418 | if (I->isBaseInitializer()) { |
6419 | QualType BaseType(I->getBaseClass(), 0); |
6420 | #ifndef NDEBUG |
6421 | // Non-virtual base classes are initialized in the order in the class |
6422 | // definition. We have already checked for virtual base classes. |
6423 | assert(!BaseIt->isVirtual() && "virtual base for literal type" ); |
6424 | assert(Info.Ctx.hasSameType(BaseIt->getType(), BaseType) && |
6425 | "base class initializers not in expected order" ); |
6426 | ++BaseIt; |
6427 | #endif |
6428 | if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD, |
6429 | Base: BaseType->getAsCXXRecordDecl(), RL: &Layout)) |
6430 | return false; |
6431 | Value = &Result.getStructBase(i: BasesSeen++); |
6432 | } else if ((FD = I->getMember())) { |
6433 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout)) |
6434 | return false; |
6435 | if (RD->isUnion()) { |
6436 | Result = APValue(FD); |
6437 | Value = &Result.getUnionValue(); |
6438 | } else { |
6439 | SkipToField(FD, false); |
6440 | Value = &Result.getStructField(i: FD->getFieldIndex()); |
6441 | } |
6442 | } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) { |
6443 | // Walk the indirect field decl's chain to find the object to initialize, |
6444 | // and make sure we've initialized every step along it. |
6445 | auto IndirectFieldChain = IFD->chain(); |
6446 | for (auto *C : IndirectFieldChain) { |
6447 | FD = cast<FieldDecl>(Val: C); |
6448 | CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent()); |
6449 | // Switch the union field if it differs. This happens if we had |
6450 | // preceding zero-initialization, and we're now initializing a union |
6451 | // subobject other than the first. |
6452 | // FIXME: In this case, the values of the other subobjects are |
6453 | // specified, since zero-initialization sets all padding bits to zero. |
6454 | if (!Value->hasValue() || |
6455 | (Value->isUnion() && Value->getUnionField() != FD)) { |
6456 | if (CD->isUnion()) |
6457 | *Value = APValue(FD); |
6458 | else |
6459 | // FIXME: This immediately starts the lifetime of all members of |
6460 | // an anonymous struct. It would be preferable to strictly start |
6461 | // member lifetime in initialization order. |
6462 | Success &= |
6463 | handleDefaultInitValue(T: Info.Ctx.getRecordType(CD), Result&: *Value); |
6464 | } |
6465 | // Store Subobject as its parent before updating it for the last element |
6466 | // in the chain. |
6467 | if (C == IndirectFieldChain.back()) |
6468 | SubobjectParent = Subobject; |
6469 | if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD)) |
6470 | return false; |
6471 | if (CD->isUnion()) |
6472 | Value = &Value->getUnionValue(); |
6473 | else { |
6474 | if (C == IndirectFieldChain.front() && !RD->isUnion()) |
6475 | SkipToField(FD, true); |
6476 | Value = &Value->getStructField(i: FD->getFieldIndex()); |
6477 | } |
6478 | } |
6479 | } else { |
6480 | llvm_unreachable("unknown base initializer kind" ); |
6481 | } |
6482 | |
6483 | // Need to override This for implicit field initializers as in this case |
6484 | // This refers to innermost anonymous struct/union containing initializer, |
6485 | // not to currently constructed class. |
6486 | const Expr *Init = I->getInit(); |
6487 | if (Init->isValueDependent()) { |
6488 | if (!EvaluateDependentExpr(E: Init, Info)) |
6489 | return false; |
6490 | } else { |
6491 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent, |
6492 | isa<CXXDefaultInitExpr>(Val: Init)); |
6493 | FullExpressionRAII InitScope(Info); |
6494 | if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) || |
6495 | (FD && FD->isBitField() && |
6496 | !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) { |
6497 | // If we're checking for a potential constant expression, evaluate all |
6498 | // initializers even if some of them fail. |
6499 | if (!Info.noteFailure()) |
6500 | return false; |
6501 | Success = false; |
6502 | } |
6503 | } |
6504 | |
6505 | // This is the point at which the dynamic type of the object becomes this |
6506 | // class type. |
6507 | if (I->isBaseInitializer() && BasesSeen == RD->getNumBases()) |
6508 | EvalObj.finishedConstructingBases(); |
6509 | } |
6510 | |
6511 | // Default-initialize any remaining fields. |
6512 | if (!RD->isUnion()) { |
6513 | for (; FieldIt != RD->field_end(); ++FieldIt) { |
6514 | if (!FieldIt->isUnnamedBitfield()) |
6515 | Success &= handleDefaultInitValue( |
6516 | FieldIt->getType(), |
6517 | Result.getStructField(i: FieldIt->getFieldIndex())); |
6518 | } |
6519 | } |
6520 | |
6521 | EvalObj.finishedConstructingFields(); |
6522 | |
6523 | return Success && |
6524 | EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed && |
6525 | LifetimeExtendedScope.destroy(); |
6526 | } |
6527 | |
6528 | static bool HandleConstructorCall(const Expr *E, const LValue &This, |
6529 | ArrayRef<const Expr*> Args, |
6530 | const CXXConstructorDecl *Definition, |
6531 | EvalInfo &Info, APValue &Result) { |
6532 | CallScopeRAII CallScope(Info); |
6533 | CallRef Call = Info.CurrentCall->createCall(Definition); |
6534 | if (!EvaluateArgs(Args, Call, Info, Definition)) |
6535 | return false; |
6536 | |
6537 | return HandleConstructorCall(E, This, Call, Definition, Info, Result) && |
6538 | CallScope.destroy(); |
6539 | } |
6540 | |
6541 | static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange, |
6542 | const LValue &This, APValue &Value, |
6543 | QualType T) { |
6544 | // Objects can only be destroyed while they're within their lifetimes. |
6545 | // FIXME: We have no representation for whether an object of type nullptr_t |
6546 | // is in its lifetime; it usually doesn't matter. Perhaps we should model it |
6547 | // as indeterminate instead? |
6548 | if (Value.isAbsent() && !T->isNullPtrType()) { |
6549 | APValue Printable; |
6550 | This.moveInto(V&: Printable); |
6551 | Info.FFDiag(CallRange.getBegin(), |
6552 | diag::note_constexpr_destroy_out_of_lifetime) |
6553 | << Printable.getAsString(Info.Ctx, Info.Ctx.getLValueReferenceType(T)); |
6554 | return false; |
6555 | } |
6556 | |
6557 | // Invent an expression for location purposes. |
6558 | // FIXME: We shouldn't need to do this. |
6559 | OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue); |
6560 | |
6561 | // For arrays, destroy elements right-to-left. |
6562 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) { |
6563 | uint64_t Size = CAT->getSize().getZExtValue(); |
6564 | QualType ElemT = CAT->getElementType(); |
6565 | |
6566 | if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin())) |
6567 | return false; |
6568 | |
6569 | LValue ElemLV = This; |
6570 | ElemLV.addArray(Info, &LocE, CAT); |
6571 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, Size)) |
6572 | return false; |
6573 | |
6574 | // Ensure that we have actual array elements available to destroy; the |
6575 | // destructors might mutate the value, so we can't run them on the array |
6576 | // filler. |
6577 | if (Size && Size > Value.getArrayInitializedElts()) |
6578 | expandArray(Array&: Value, Index: Value.getArraySize() - 1); |
6579 | |
6580 | for (; Size != 0; --Size) { |
6581 | APValue &Elem = Value.getArrayInitializedElt(I: Size - 1); |
6582 | if (!HandleLValueArrayAdjustment(Info, &LocE, ElemLV, ElemT, -1) || |
6583 | !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT)) |
6584 | return false; |
6585 | } |
6586 | |
6587 | // End the lifetime of this array now. |
6588 | Value = APValue(); |
6589 | return true; |
6590 | } |
6591 | |
6592 | const CXXRecordDecl *RD = T->getAsCXXRecordDecl(); |
6593 | if (!RD) { |
6594 | if (T.isDestructedType()) { |
6595 | Info.FFDiag(CallRange.getBegin(), |
6596 | diag::note_constexpr_unsupported_destruction) |
6597 | << T; |
6598 | return false; |
6599 | } |
6600 | |
6601 | Value = APValue(); |
6602 | return true; |
6603 | } |
6604 | |
6605 | if (RD->getNumVBases()) { |
6606 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_virtual_base) << RD; |
6607 | return false; |
6608 | } |
6609 | |
6610 | const CXXDestructorDecl *DD = RD->getDestructor(); |
6611 | if (!DD && !RD->hasTrivialDestructor()) { |
6612 | Info.FFDiag(Loc: CallRange.getBegin()); |
6613 | return false; |
6614 | } |
6615 | |
6616 | if (!DD || DD->isTrivial() || |
6617 | (RD->isAnonymousStructOrUnion() && RD->isUnion())) { |
6618 | // A trivial destructor just ends the lifetime of the object. Check for |
6619 | // this case before checking for a body, because we might not bother |
6620 | // building a body for a trivial destructor. Note that it doesn't matter |
6621 | // whether the destructor is constexpr in this case; all trivial |
6622 | // destructors are constexpr. |
6623 | // |
6624 | // If an anonymous union would be destroyed, some enclosing destructor must |
6625 | // have been explicitly defined, and the anonymous union destruction should |
6626 | // have no effect. |
6627 | Value = APValue(); |
6628 | return true; |
6629 | } |
6630 | |
6631 | if (!Info.CheckCallLimit(Loc: CallRange.getBegin())) |
6632 | return false; |
6633 | |
6634 | const FunctionDecl *Definition = nullptr; |
6635 | const Stmt *Body = DD->getBody(Definition); |
6636 | |
6637 | if (!CheckConstexprFunction(Info, CallRange.getBegin(), DD, Definition, Body)) |
6638 | return false; |
6639 | |
6640 | CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr, |
6641 | CallRef()); |
6642 | |
6643 | // We're now in the period of destruction of this object. |
6644 | unsigned BasesLeft = RD->getNumBases(); |
6645 | EvalInfo::EvaluatingDestructorRAII EvalObj( |
6646 | Info, |
6647 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}); |
6648 | if (!EvalObj.DidInsert) { |
6649 | // C++2a [class.dtor]p19: |
6650 | // the behavior is undefined if the destructor is invoked for an object |
6651 | // whose lifetime has ended |
6652 | // (Note that formally the lifetime ends when the period of destruction |
6653 | // begins, even though certain uses of the object remain valid until the |
6654 | // period of destruction ends.) |
6655 | Info.FFDiag(CallRange.getBegin(), diag::note_constexpr_double_destroy); |
6656 | return false; |
6657 | } |
6658 | |
6659 | // FIXME: Creating an APValue just to hold a nonexistent return value is |
6660 | // wasteful. |
6661 | APValue RetVal; |
6662 | StmtResult Ret = {.Value: RetVal, .Slot: nullptr}; |
6663 | if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed) |
6664 | return false; |
6665 | |
6666 | // A union destructor does not implicitly destroy its members. |
6667 | if (RD->isUnion()) |
6668 | return true; |
6669 | |
6670 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(RD); |
6671 | |
6672 | // We don't have a good way to iterate fields in reverse, so collect all the |
6673 | // fields first and then walk them backwards. |
6674 | SmallVector<FieldDecl*, 16> Fields(RD->fields()); |
6675 | for (const FieldDecl *FD : llvm::reverse(Fields)) { |
6676 | if (FD->isUnnamedBitfield()) |
6677 | continue; |
6678 | |
6679 | LValue Subobject = This; |
6680 | if (!HandleLValueMember(Info, &LocE, Subobject, FD, &Layout)) |
6681 | return false; |
6682 | |
6683 | APValue *SubobjectValue = &Value.getStructField(FD->getFieldIndex()); |
6684 | if (!HandleDestructionImpl(Info, CallRange, Subobject, *SubobjectValue, |
6685 | FD->getType())) |
6686 | return false; |
6687 | } |
6688 | |
6689 | if (BasesLeft != 0) |
6690 | EvalObj.startedDestroyingBases(); |
6691 | |
6692 | // Destroy base classes in reverse order. |
6693 | for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) { |
6694 | --BasesLeft; |
6695 | |
6696 | QualType BaseType = Base.getType(); |
6697 | LValue Subobject = This; |
6698 | if (!HandleLValueDirectBase(Info, &LocE, Subobject, RD, |
6699 | BaseType->getAsCXXRecordDecl(), &Layout)) |
6700 | return false; |
6701 | |
6702 | APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft); |
6703 | if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue, |
6704 | T: BaseType)) |
6705 | return false; |
6706 | } |
6707 | assert(BasesLeft == 0 && "NumBases was wrong?" ); |
6708 | |
6709 | // The period of destruction ends now. The object is gone. |
6710 | Value = APValue(); |
6711 | return true; |
6712 | } |
6713 | |
6714 | namespace { |
6715 | struct DestroyObjectHandler { |
6716 | EvalInfo &Info; |
6717 | const Expr *E; |
6718 | const LValue &This; |
6719 | const AccessKinds AccessKind; |
6720 | |
6721 | typedef bool result_type; |
6722 | bool failed() { return false; } |
6723 | bool found(APValue &Subobj, QualType SubobjType) { |
6724 | return HandleDestructionImpl(Info, E->getSourceRange(), This, Subobj, |
6725 | SubobjType); |
6726 | } |
6727 | bool found(APSInt &Value, QualType SubobjType) { |
6728 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6729 | return false; |
6730 | } |
6731 | bool found(APFloat &Value, QualType SubobjType) { |
6732 | Info.FFDiag(E, diag::note_constexpr_destroy_complex_elem); |
6733 | return false; |
6734 | } |
6735 | }; |
6736 | } |
6737 | |
6738 | /// Perform a destructor or pseudo-destructor call on the given object, which |
6739 | /// might in general not be a complete object. |
6740 | static bool HandleDestruction(EvalInfo &Info, const Expr *E, |
6741 | const LValue &This, QualType ThisType) { |
6742 | CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType); |
6743 | DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy}; |
6744 | return Obj && findSubobject(Info, E, Obj, This.Designator, Handler); |
6745 | } |
6746 | |
6747 | /// Destroy and end the lifetime of the given complete object. |
6748 | static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc, |
6749 | APValue::LValueBase LVBase, APValue &Value, |
6750 | QualType T) { |
6751 | // If we've had an unmodeled side-effect, we can't rely on mutable state |
6752 | // (such as the object we're about to destroy) being correct. |
6753 | if (Info.EvalStatus.HasSideEffects) |
6754 | return false; |
6755 | |
6756 | LValue LV; |
6757 | LV.set(B: {LVBase}); |
6758 | return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T); |
6759 | } |
6760 | |
6761 | /// Perform a call to 'operator new' or to `__builtin_operator_new'. |
6762 | static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E, |
6763 | LValue &Result) { |
6764 | if (Info.checkingPotentialConstantExpression() || |
6765 | Info.SpeculativeEvaluationDepth) |
6766 | return false; |
6767 | |
6768 | // This is permitted only within a call to std::allocator<T>::allocate. |
6769 | auto Caller = Info.getStdAllocatorCaller(FnName: "allocate" ); |
6770 | if (!Caller) { |
6771 | Info.FFDiag(E->getExprLoc(), Info.getLangOpts().CPlusPlus20 |
6772 | ? diag::note_constexpr_new_untyped |
6773 | : diag::note_constexpr_new); |
6774 | return false; |
6775 | } |
6776 | |
6777 | QualType ElemType = Caller.ElemType; |
6778 | if (ElemType->isIncompleteType() || ElemType->isFunctionType()) { |
6779 | Info.FFDiag(E->getExprLoc(), |
6780 | diag::note_constexpr_new_not_complete_object_type) |
6781 | << (ElemType->isIncompleteType() ? 0 : 1) << ElemType; |
6782 | return false; |
6783 | } |
6784 | |
6785 | APSInt ByteSize; |
6786 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info)) |
6787 | return false; |
6788 | bool IsNothrow = false; |
6789 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) { |
6790 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
6791 | IsNothrow |= E->getType()->isNothrowT(); |
6792 | } |
6793 | |
6794 | CharUnits ElemSize; |
6795 | if (!HandleSizeof(Info, E->getExprLoc(), ElemType, ElemSize)) |
6796 | return false; |
6797 | APInt Size, Remainder; |
6798 | APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity()); |
6799 | APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder); |
6800 | if (Remainder != 0) { |
6801 | // This likely indicates a bug in the implementation of 'std::allocator'. |
6802 | Info.FFDiag(E->getExprLoc(), diag::note_constexpr_operator_new_bad_size) |
6803 | << ByteSize << APSInt(ElemSizeAP, true) << ElemType; |
6804 | return false; |
6805 | } |
6806 | |
6807 | if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(), |
6808 | ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) { |
6809 | if (IsNothrow) { |
6810 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
6811 | return true; |
6812 | } |
6813 | return false; |
6814 | } |
6815 | |
6816 | QualType AllocType = Info.Ctx.getConstantArrayType( |
6817 | EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
6818 | APValue *Val = Info.createHeapAlloc(E, AllocType, Result); |
6819 | *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue()); |
6820 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: AllocType)); |
6821 | return true; |
6822 | } |
6823 | |
6824 | static bool hasVirtualDestructor(QualType T) { |
6825 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6826 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6827 | return DD->isVirtual(); |
6828 | return false; |
6829 | } |
6830 | |
6831 | static const FunctionDecl *getVirtualOperatorDelete(QualType T) { |
6832 | if (CXXRecordDecl *RD = T->getAsCXXRecordDecl()) |
6833 | if (CXXDestructorDecl *DD = RD->getDestructor()) |
6834 | return DD->isVirtual() ? DD->getOperatorDelete() : nullptr; |
6835 | return nullptr; |
6836 | } |
6837 | |
6838 | /// Check that the given object is a suitable pointer to a heap allocation that |
6839 | /// still exists and is of the right kind for the purpose of a deletion. |
6840 | /// |
6841 | /// On success, returns the heap allocation to deallocate. On failure, produces |
6842 | /// a diagnostic and returns std::nullopt. |
6843 | static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E, |
6844 | const LValue &Pointer, |
6845 | DynAlloc::Kind DeallocKind) { |
6846 | auto PointerAsString = [&] { |
6847 | return Pointer.toString(Info.Ctx, Info.Ctx.VoidPtrTy); |
6848 | }; |
6849 | |
6850 | DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>(); |
6851 | if (!DA) { |
6852 | Info.FFDiag(E, diag::note_constexpr_delete_not_heap_alloc) |
6853 | << PointerAsString(); |
6854 | if (Pointer.Base) |
6855 | NoteLValueLocation(Info, Base: Pointer.Base); |
6856 | return std::nullopt; |
6857 | } |
6858 | |
6859 | std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA); |
6860 | if (!Alloc) { |
6861 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
6862 | return std::nullopt; |
6863 | } |
6864 | |
6865 | if (DeallocKind != (*Alloc)->getKind()) { |
6866 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
6867 | Info.FFDiag(E, diag::note_constexpr_new_delete_mismatch) |
6868 | << DeallocKind << (*Alloc)->getKind() << AllocType; |
6869 | NoteLValueLocation(Info, Base: Pointer.Base); |
6870 | return std::nullopt; |
6871 | } |
6872 | |
6873 | bool Subobject = false; |
6874 | if (DeallocKind == DynAlloc::New) { |
6875 | Subobject = Pointer.Designator.MostDerivedPathLength != 0 || |
6876 | Pointer.Designator.isOnePastTheEnd(); |
6877 | } else { |
6878 | Subobject = Pointer.Designator.Entries.size() != 1 || |
6879 | Pointer.Designator.Entries[0].getAsArrayIndex() != 0; |
6880 | } |
6881 | if (Subobject) { |
6882 | Info.FFDiag(E, diag::note_constexpr_delete_subobject) |
6883 | << PointerAsString() << Pointer.Designator.isOnePastTheEnd(); |
6884 | return std::nullopt; |
6885 | } |
6886 | |
6887 | return Alloc; |
6888 | } |
6889 | |
6890 | // Perform a call to 'operator delete' or '__builtin_operator_delete'. |
6891 | bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) { |
6892 | if (Info.checkingPotentialConstantExpression() || |
6893 | Info.SpeculativeEvaluationDepth) |
6894 | return false; |
6895 | |
6896 | // This is permitted only within a call to std::allocator<T>::deallocate. |
6897 | if (!Info.getStdAllocatorCaller(FnName: "deallocate" )) { |
6898 | Info.FFDiag(E->getExprLoc()); |
6899 | return true; |
6900 | } |
6901 | |
6902 | LValue Pointer; |
6903 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info)) |
6904 | return false; |
6905 | for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) |
6906 | EvaluateIgnoredValue(Info, E: E->getArg(Arg: I)); |
6907 | |
6908 | if (Pointer.Designator.Invalid) |
6909 | return false; |
6910 | |
6911 | // Deleting a null pointer would have no effect, but it's not permitted by |
6912 | // std::allocator<T>::deallocate's contract. |
6913 | if (Pointer.isNullPointer()) { |
6914 | Info.CCEDiag(E->getExprLoc(), diag::note_constexpr_deallocate_null); |
6915 | return true; |
6916 | } |
6917 | |
6918 | if (!CheckDeleteKind(Info, E, Pointer, DynAlloc::StdAllocator)) |
6919 | return false; |
6920 | |
6921 | Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>()); |
6922 | return true; |
6923 | } |
6924 | |
6925 | //===----------------------------------------------------------------------===// |
6926 | // Generic Evaluation |
6927 | //===----------------------------------------------------------------------===// |
6928 | namespace { |
6929 | |
6930 | class BitCastBuffer { |
6931 | // FIXME: We're going to need bit-level granularity when we support |
6932 | // bit-fields. |
6933 | // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but |
6934 | // we don't support a host or target where that is the case. Still, we should |
6935 | // use a more generic type in case we ever do. |
6936 | SmallVector<std::optional<unsigned char>, 32> Bytes; |
6937 | |
6938 | static_assert(std::numeric_limits<unsigned char>::digits >= 8, |
6939 | "Need at least 8 bit unsigned char" ); |
6940 | |
6941 | bool TargetIsLittleEndian; |
6942 | |
6943 | public: |
6944 | BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian) |
6945 | : Bytes(Width.getQuantity()), |
6946 | TargetIsLittleEndian(TargetIsLittleEndian) {} |
6947 | |
6948 | [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width, |
6949 | SmallVectorImpl<unsigned char> &Output) const { |
6950 | for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) { |
6951 | // If a byte of an integer is uninitialized, then the whole integer is |
6952 | // uninitialized. |
6953 | if (!Bytes[I.getQuantity()]) |
6954 | return false; |
6955 | Output.push_back(Elt: *Bytes[I.getQuantity()]); |
6956 | } |
6957 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6958 | std::reverse(first: Output.begin(), last: Output.end()); |
6959 | return true; |
6960 | } |
6961 | |
6962 | void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) { |
6963 | if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian) |
6964 | std::reverse(first: Input.begin(), last: Input.end()); |
6965 | |
6966 | size_t Index = 0; |
6967 | for (unsigned char Byte : Input) { |
6968 | assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?" ); |
6969 | Bytes[Offset.getQuantity() + Index] = Byte; |
6970 | ++Index; |
6971 | } |
6972 | } |
6973 | |
6974 | size_t size() { return Bytes.size(); } |
6975 | }; |
6976 | |
6977 | /// Traverse an APValue to produce an BitCastBuffer, emulating how the current |
6978 | /// target would represent the value at runtime. |
6979 | class APValueToBufferConverter { |
6980 | EvalInfo &Info; |
6981 | BitCastBuffer Buffer; |
6982 | const CastExpr *BCE; |
6983 | |
6984 | APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth, |
6985 | const CastExpr *BCE) |
6986 | : Info(Info), |
6987 | Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()), |
6988 | BCE(BCE) {} |
6989 | |
6990 | bool visit(const APValue &Val, QualType Ty) { |
6991 | return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0)); |
6992 | } |
6993 | |
6994 | // Write out Val with type Ty into Buffer starting at Offset. |
6995 | bool visit(const APValue &Val, QualType Ty, CharUnits Offset) { |
6996 | assert((size_t)Offset.getQuantity() <= Buffer.size()); |
6997 | |
6998 | // As a special case, nullptr_t has an indeterminate value. |
6999 | if (Ty->isNullPtrType()) |
7000 | return true; |
7001 | |
7002 | // Dig through Src to find the byte at SrcOffset. |
7003 | switch (Val.getKind()) { |
7004 | case APValue::Indeterminate: |
7005 | case APValue::None: |
7006 | return true; |
7007 | |
7008 | case APValue::Int: |
7009 | return visitInt(Val: Val.getInt(), Ty, Offset); |
7010 | case APValue::Float: |
7011 | return visitFloat(Val: Val.getFloat(), Ty, Offset); |
7012 | case APValue::Array: |
7013 | return visitArray(Val, Ty, Offset); |
7014 | case APValue::Struct: |
7015 | return visitRecord(Val, Ty, Offset); |
7016 | case APValue::Vector: |
7017 | return visitVector(Val, Ty, Offset); |
7018 | |
7019 | case APValue::ComplexInt: |
7020 | case APValue::ComplexFloat: |
7021 | case APValue::FixedPoint: |
7022 | // FIXME: We should support these. |
7023 | |
7024 | case APValue::Union: |
7025 | case APValue::MemberPointer: |
7026 | case APValue::AddrLabelDiff: { |
7027 | Info.FFDiag(BCE->getBeginLoc(), |
7028 | diag::note_constexpr_bit_cast_unsupported_type) |
7029 | << Ty; |
7030 | return false; |
7031 | } |
7032 | |
7033 | case APValue::LValue: |
7034 | llvm_unreachable("LValue subobject in bit_cast?" ); |
7035 | } |
7036 | llvm_unreachable("Unhandled APValue::ValueKind" ); |
7037 | } |
7038 | |
7039 | bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) { |
7040 | const RecordDecl *RD = Ty->getAsRecordDecl(); |
7041 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7042 | |
7043 | // Visit the base classes. |
7044 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) { |
7045 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7046 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7047 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7048 | |
7049 | if (!visitRecord(Val: Val.getStructBase(i: I), Ty: BS.getType(), |
7050 | Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset)) |
7051 | return false; |
7052 | } |
7053 | } |
7054 | |
7055 | // Visit the fields. |
7056 | unsigned FieldIdx = 0; |
7057 | for (FieldDecl *FD : RD->fields()) { |
7058 | if (FD->isBitField()) { |
7059 | Info.FFDiag(BCE->getBeginLoc(), |
7060 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7061 | return false; |
7062 | } |
7063 | |
7064 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx); |
7065 | |
7066 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 && |
7067 | "only bit-fields can have sub-char alignment" ); |
7068 | CharUnits FieldOffset = |
7069 | Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset; |
7070 | QualType FieldTy = FD->getType(); |
7071 | if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset)) |
7072 | return false; |
7073 | ++FieldIdx; |
7074 | } |
7075 | |
7076 | return true; |
7077 | } |
7078 | |
7079 | bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) { |
7080 | const auto *CAT = |
7081 | dyn_cast_or_null<ConstantArrayType>(Ty->getAsArrayTypeUnsafe()); |
7082 | if (!CAT) |
7083 | return false; |
7084 | |
7085 | CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(CAT->getElementType()); |
7086 | unsigned NumInitializedElts = Val.getArrayInitializedElts(); |
7087 | unsigned ArraySize = Val.getArraySize(); |
7088 | // First, initialize the initialized elements. |
7089 | for (unsigned I = 0; I != NumInitializedElts; ++I) { |
7090 | const APValue &SubObj = Val.getArrayInitializedElt(I); |
7091 | if (!visit(SubObj, CAT->getElementType(), Offset + I * ElemWidth)) |
7092 | return false; |
7093 | } |
7094 | |
7095 | // Next, initialize the rest of the array using the filler. |
7096 | if (Val.hasArrayFiller()) { |
7097 | const APValue &Filler = Val.getArrayFiller(); |
7098 | for (unsigned I = NumInitializedElts; I != ArraySize; ++I) { |
7099 | if (!visit(Filler, CAT->getElementType(), Offset + I * ElemWidth)) |
7100 | return false; |
7101 | } |
7102 | } |
7103 | |
7104 | return true; |
7105 | } |
7106 | |
7107 | bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) { |
7108 | const VectorType *VTy = Ty->castAs<VectorType>(); |
7109 | QualType EltTy = VTy->getElementType(); |
7110 | unsigned NElts = VTy->getNumElements(); |
7111 | unsigned EltSize = |
7112 | VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(T: EltTy); |
7113 | |
7114 | if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) { |
7115 | // The vector's size in bits is not a multiple of the target's byte size, |
7116 | // so its layout is unspecified. For now, we'll simply treat these cases |
7117 | // as unsupported (this should only be possible with OpenCL bool vectors |
7118 | // whose element count isn't a multiple of the byte size). |
7119 | Info.FFDiag(BCE->getBeginLoc(), |
7120 | diag::note_constexpr_bit_cast_invalid_vector) |
7121 | << Ty.getCanonicalType() << EltSize << NElts |
7122 | << Info.Ctx.getCharWidth(); |
7123 | return false; |
7124 | } |
7125 | |
7126 | if (EltTy->isRealFloatingType() && &Info.Ctx.getFloatTypeSemantics(T: EltTy) == |
7127 | &APFloat::x87DoubleExtended()) { |
7128 | // The layout for x86_fp80 vectors seems to be handled very inconsistently |
7129 | // by both clang and LLVM, so for now we won't allow bit_casts involving |
7130 | // it in a constexpr context. |
7131 | Info.FFDiag(BCE->getBeginLoc(), |
7132 | diag::note_constexpr_bit_cast_unsupported_type) |
7133 | << EltTy; |
7134 | return false; |
7135 | } |
7136 | |
7137 | if (VTy->isExtVectorBoolType()) { |
7138 | // Special handling for OpenCL bool vectors: |
7139 | // Since these vectors are stored as packed bits, but we can't write |
7140 | // individual bits to the BitCastBuffer, we'll buffer all of the elements |
7141 | // together into an appropriately sized APInt and write them all out at |
7142 | // once. Because we don't accept vectors where NElts * EltSize isn't a |
7143 | // multiple of the char size, there will be no padding space, so we don't |
7144 | // have to worry about writing data which should have been left |
7145 | // uninitialized. |
7146 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7147 | |
7148 | llvm::APInt Res = llvm::APInt::getZero(numBits: NElts); |
7149 | for (unsigned I = 0; I < NElts; ++I) { |
7150 | const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt(); |
7151 | assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 && |
7152 | "bool vector element must be 1-bit unsigned integer!" ); |
7153 | |
7154 | Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I); |
7155 | } |
7156 | |
7157 | SmallVector<uint8_t, 8> Bytes(NElts / 8); |
7158 | llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8); |
7159 | Buffer.writeObject(Offset, Input&: Bytes); |
7160 | } else { |
7161 | // Iterate over each of the elements and write them out to the buffer at |
7162 | // the appropriate offset. |
7163 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7164 | for (unsigned I = 0; I < NElts; ++I) { |
7165 | if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars)) |
7166 | return false; |
7167 | } |
7168 | } |
7169 | |
7170 | return true; |
7171 | } |
7172 | |
7173 | bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) { |
7174 | APSInt AdjustedVal = Val; |
7175 | unsigned Width = AdjustedVal.getBitWidth(); |
7176 | if (Ty->isBooleanType()) { |
7177 | Width = Info.Ctx.getTypeSize(T: Ty); |
7178 | AdjustedVal = AdjustedVal.extend(width: Width); |
7179 | } |
7180 | |
7181 | SmallVector<uint8_t, 8> Bytes(Width / 8); |
7182 | llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8); |
7183 | Buffer.writeObject(Offset, Input&: Bytes); |
7184 | return true; |
7185 | } |
7186 | |
7187 | bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) { |
7188 | APSInt AsInt(Val.bitcastToAPInt()); |
7189 | return visitInt(Val: AsInt, Ty, Offset); |
7190 | } |
7191 | |
7192 | public: |
7193 | static std::optional<BitCastBuffer> |
7194 | convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) { |
7195 | CharUnits DstSize = Info.Ctx.getTypeSizeInChars(BCE->getType()); |
7196 | APValueToBufferConverter Converter(Info, DstSize, BCE); |
7197 | if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType())) |
7198 | return std::nullopt; |
7199 | return Converter.Buffer; |
7200 | } |
7201 | }; |
7202 | |
7203 | /// Write an BitCastBuffer into an APValue. |
7204 | class BufferToAPValueConverter { |
7205 | EvalInfo &Info; |
7206 | const BitCastBuffer &Buffer; |
7207 | const CastExpr *BCE; |
7208 | |
7209 | BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer, |
7210 | const CastExpr *BCE) |
7211 | : Info(Info), Buffer(Buffer), BCE(BCE) {} |
7212 | |
7213 | // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast |
7214 | // with an invalid type, so anything left is a deficiency on our part (FIXME). |
7215 | // Ideally this will be unreachable. |
7216 | std::nullopt_t unsupportedType(QualType Ty) { |
7217 | Info.FFDiag(BCE->getBeginLoc(), |
7218 | diag::note_constexpr_bit_cast_unsupported_type) |
7219 | << Ty; |
7220 | return std::nullopt; |
7221 | } |
7222 | |
7223 | std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) { |
7224 | Info.FFDiag(BCE->getBeginLoc(), |
7225 | diag::note_constexpr_bit_cast_unrepresentable_value) |
7226 | << Ty << toString(Val, /*Radix=*/10); |
7227 | return std::nullopt; |
7228 | } |
7229 | |
7230 | std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset, |
7231 | const EnumType *EnumSugar = nullptr) { |
7232 | if (T->isNullPtrType()) { |
7233 | uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0)); |
7234 | return APValue((Expr *)nullptr, |
7235 | /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue), |
7236 | APValue::NoLValuePath{}, /*IsNullPtr=*/true); |
7237 | } |
7238 | |
7239 | CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T); |
7240 | |
7241 | // Work around floating point types that contain unused padding bytes. This |
7242 | // is really just `long double` on x86, which is the only fundamental type |
7243 | // with padding bytes. |
7244 | if (T->isRealFloatingType()) { |
7245 | const llvm::fltSemantics &Semantics = |
7246 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7247 | unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics); |
7248 | assert(NumBits % 8 == 0); |
7249 | CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8); |
7250 | if (NumBytes != SizeOf) |
7251 | SizeOf = NumBytes; |
7252 | } |
7253 | |
7254 | SmallVector<uint8_t, 8> Bytes; |
7255 | if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) { |
7256 | // If this is std::byte or unsigned char, then its okay to store an |
7257 | // indeterminate value. |
7258 | bool IsStdByte = EnumSugar && EnumSugar->isStdByteType(); |
7259 | bool IsUChar = |
7260 | !EnumSugar && (T->isSpecificBuiltinType(BuiltinType::UChar) || |
7261 | T->isSpecificBuiltinType(BuiltinType::Char_U)); |
7262 | if (!IsStdByte && !IsUChar) { |
7263 | QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0); |
7264 | Info.FFDiag(BCE->getExprLoc(), |
7265 | diag::note_constexpr_bit_cast_indet_dest) |
7266 | << DisplayType << Info.Ctx.getLangOpts().CharIsSigned; |
7267 | return std::nullopt; |
7268 | } |
7269 | |
7270 | return APValue::IndeterminateValue(); |
7271 | } |
7272 | |
7273 | APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true); |
7274 | llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7275 | |
7276 | if (T->isIntegralOrEnumerationType()) { |
7277 | Val.setIsSigned(T->isSignedIntegerOrEnumerationType()); |
7278 | |
7279 | unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0)); |
7280 | if (IntWidth != Val.getBitWidth()) { |
7281 | APSInt Truncated = Val.trunc(width: IntWidth); |
7282 | if (Truncated.extend(width: Val.getBitWidth()) != Val) |
7283 | return unrepresentableValue(Ty: QualType(T, 0), Val); |
7284 | Val = Truncated; |
7285 | } |
7286 | |
7287 | return APValue(Val); |
7288 | } |
7289 | |
7290 | if (T->isRealFloatingType()) { |
7291 | const llvm::fltSemantics &Semantics = |
7292 | Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0)); |
7293 | return APValue(APFloat(Semantics, Val)); |
7294 | } |
7295 | |
7296 | return unsupportedType(Ty: QualType(T, 0)); |
7297 | } |
7298 | |
7299 | std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) { |
7300 | const RecordDecl *RD = RTy->getAsRecordDecl(); |
7301 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
7302 | |
7303 | unsigned NumBases = 0; |
7304 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) |
7305 | NumBases = CXXRD->getNumBases(); |
7306 | |
7307 | APValue ResultVal(APValue::UninitStruct(), NumBases, |
7308 | std::distance(RD->field_begin(), RD->field_end())); |
7309 | |
7310 | // Visit the base classes. |
7311 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) { |
7312 | for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) { |
7313 | const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I]; |
7314 | CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl(); |
7315 | if (BaseDecl->isEmpty() || |
7316 | Info.Ctx.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero()) |
7317 | continue; |
7318 | |
7319 | std::optional<APValue> SubObj = visitType( |
7320 | BS.getType(), Layout.getBaseClassOffset(BaseDecl) + Offset); |
7321 | if (!SubObj) |
7322 | return std::nullopt; |
7323 | ResultVal.getStructBase(i: I) = *SubObj; |
7324 | } |
7325 | } |
7326 | |
7327 | // Visit the fields. |
7328 | unsigned FieldIdx = 0; |
7329 | for (FieldDecl *FD : RD->fields()) { |
7330 | // FIXME: We don't currently support bit-fields. A lot of the logic for |
7331 | // this is in CodeGen, so we need to factor it around. |
7332 | if (FD->isBitField()) { |
7333 | Info.FFDiag(BCE->getBeginLoc(), |
7334 | diag::note_constexpr_bit_cast_unsupported_bitfield); |
7335 | return std::nullopt; |
7336 | } |
7337 | |
7338 | uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldIdx); |
7339 | assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0); |
7340 | |
7341 | CharUnits FieldOffset = |
7342 | CharUnits::fromQuantity(FieldOffsetBits / Info.Ctx.getCharWidth()) + |
7343 | Offset; |
7344 | QualType FieldTy = FD->getType(); |
7345 | std::optional<APValue> SubObj = visitType(FieldTy, FieldOffset); |
7346 | if (!SubObj) |
7347 | return std::nullopt; |
7348 | ResultVal.getStructField(FieldIdx) = *SubObj; |
7349 | ++FieldIdx; |
7350 | } |
7351 | |
7352 | return ResultVal; |
7353 | } |
7354 | |
7355 | std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) { |
7356 | QualType RepresentationType = Ty->getDecl()->getIntegerType(); |
7357 | assert(!RepresentationType.isNull() && |
7358 | "enum forward decl should be caught by Sema" ); |
7359 | const auto *AsBuiltin = |
7360 | RepresentationType.getCanonicalType()->castAs<BuiltinType>(); |
7361 | // Recurse into the underlying type. Treat std::byte transparently as |
7362 | // unsigned char. |
7363 | return visit(AsBuiltin, Offset, /*EnumTy=*/Ty); |
7364 | } |
7365 | |
7366 | std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) { |
7367 | size_t Size = Ty->getSize().getLimitedValue(); |
7368 | CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(Ty->getElementType()); |
7369 | |
7370 | APValue ArrayValue(APValue::UninitArray(), Size, Size); |
7371 | for (size_t I = 0; I != Size; ++I) { |
7372 | std::optional<APValue> ElementValue = |
7373 | visitType(Ty->getElementType(), Offset + I * ElementWidth); |
7374 | if (!ElementValue) |
7375 | return std::nullopt; |
7376 | ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue); |
7377 | } |
7378 | |
7379 | return ArrayValue; |
7380 | } |
7381 | |
7382 | std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) { |
7383 | QualType EltTy = VTy->getElementType(); |
7384 | unsigned NElts = VTy->getNumElements(); |
7385 | unsigned EltSize = |
7386 | VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(T: EltTy); |
7387 | |
7388 | if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) { |
7389 | // The vector's size in bits is not a multiple of the target's byte size, |
7390 | // so its layout is unspecified. For now, we'll simply treat these cases |
7391 | // as unsupported (this should only be possible with OpenCL bool vectors |
7392 | // whose element count isn't a multiple of the byte size). |
7393 | Info.FFDiag(BCE->getBeginLoc(), |
7394 | diag::note_constexpr_bit_cast_invalid_vector) |
7395 | << QualType(VTy, 0) << EltSize << NElts << Info.Ctx.getCharWidth(); |
7396 | return std::nullopt; |
7397 | } |
7398 | |
7399 | if (EltTy->isRealFloatingType() && &Info.Ctx.getFloatTypeSemantics(T: EltTy) == |
7400 | &APFloat::x87DoubleExtended()) { |
7401 | // The layout for x86_fp80 vectors seems to be handled very inconsistently |
7402 | // by both clang and LLVM, so for now we won't allow bit_casts involving |
7403 | // it in a constexpr context. |
7404 | Info.FFDiag(BCE->getBeginLoc(), |
7405 | diag::note_constexpr_bit_cast_unsupported_type) |
7406 | << EltTy; |
7407 | return std::nullopt; |
7408 | } |
7409 | |
7410 | SmallVector<APValue, 4> Elts; |
7411 | Elts.reserve(N: NElts); |
7412 | if (VTy->isExtVectorBoolType()) { |
7413 | // Special handling for OpenCL bool vectors: |
7414 | // Since these vectors are stored as packed bits, but we can't read |
7415 | // individual bits from the BitCastBuffer, we'll buffer all of the |
7416 | // elements together into an appropriately sized APInt and write them all |
7417 | // out at once. Because we don't accept vectors where NElts * EltSize |
7418 | // isn't a multiple of the char size, there will be no padding space, so |
7419 | // we don't have to worry about reading any padding data which didn't |
7420 | // actually need to be accessed. |
7421 | bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian(); |
7422 | |
7423 | SmallVector<uint8_t, 8> Bytes; |
7424 | Bytes.reserve(N: NElts / 8); |
7425 | if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes)) |
7426 | return std::nullopt; |
7427 | |
7428 | APSInt SValInt(NElts, true); |
7429 | llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size()); |
7430 | |
7431 | for (unsigned I = 0; I < NElts; ++I) { |
7432 | llvm::APInt Elt = |
7433 | SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize); |
7434 | Elts.emplace_back( |
7435 | APSInt(std::move(Elt), !EltTy->isSignedIntegerType())); |
7436 | } |
7437 | } else { |
7438 | // Iterate over each of the elements and read them from the buffer at |
7439 | // the appropriate offset. |
7440 | CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy); |
7441 | for (unsigned I = 0; I < NElts; ++I) { |
7442 | std::optional<APValue> EltValue = |
7443 | visitType(EltTy, Offset + I * EltSizeChars); |
7444 | if (!EltValue) |
7445 | return std::nullopt; |
7446 | Elts.push_back(std::move(*EltValue)); |
7447 | } |
7448 | } |
7449 | |
7450 | return APValue(Elts.data(), Elts.size()); |
7451 | } |
7452 | |
7453 | std::optional<APValue> visit(const Type *Ty, CharUnits Offset) { |
7454 | return unsupportedType(Ty: QualType(Ty, 0)); |
7455 | } |
7456 | |
7457 | std::optional<APValue> visitType(QualType Ty, CharUnits Offset) { |
7458 | QualType Can = Ty.getCanonicalType(); |
7459 | |
7460 | switch (Can->getTypeClass()) { |
7461 | #define TYPE(Class, Base) \ |
7462 | case Type::Class: \ |
7463 | return visit(cast<Class##Type>(Can.getTypePtr()), Offset); |
7464 | #define ABSTRACT_TYPE(Class, Base) |
7465 | #define NON_CANONICAL_TYPE(Class, Base) \ |
7466 | case Type::Class: \ |
7467 | llvm_unreachable("non-canonical type should be impossible!"); |
7468 | #define DEPENDENT_TYPE(Class, Base) \ |
7469 | case Type::Class: \ |
7470 | llvm_unreachable( \ |
7471 | "dependent types aren't supported in the constant evaluator!"); |
7472 | #define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \ |
7473 | case Type::Class: \ |
7474 | llvm_unreachable("either dependent or not canonical!"); |
7475 | #include "clang/AST/TypeNodes.inc" |
7476 | } |
7477 | llvm_unreachable("Unhandled Type::TypeClass" ); |
7478 | } |
7479 | |
7480 | public: |
7481 | // Pull out a full value of type DstType. |
7482 | static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer, |
7483 | const CastExpr *BCE) { |
7484 | BufferToAPValueConverter Converter(Info, Buffer, BCE); |
7485 | return Converter.visitType(BCE->getType(), CharUnits::fromQuantity(Quantity: 0)); |
7486 | } |
7487 | }; |
7488 | |
7489 | static bool checkBitCastConstexprEligibilityType(SourceLocation Loc, |
7490 | QualType Ty, EvalInfo *Info, |
7491 | const ASTContext &Ctx, |
7492 | bool CheckingDest) { |
7493 | Ty = Ty.getCanonicalType(); |
7494 | |
7495 | auto diag = [&](int Reason) { |
7496 | if (Info) |
7497 | Info->FFDiag(Loc, diag::note_constexpr_bit_cast_invalid_type) |
7498 | << CheckingDest << (Reason == 4) << Reason; |
7499 | return false; |
7500 | }; |
7501 | auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) { |
7502 | if (Info) |
7503 | Info->Note(NoteLoc, diag::note_constexpr_bit_cast_invalid_subtype) |
7504 | << NoteTy << Construct << Ty; |
7505 | return false; |
7506 | }; |
7507 | |
7508 | if (Ty->isUnionType()) |
7509 | return diag(0); |
7510 | if (Ty->isPointerType()) |
7511 | return diag(1); |
7512 | if (Ty->isMemberPointerType()) |
7513 | return diag(2); |
7514 | if (Ty.isVolatileQualified()) |
7515 | return diag(3); |
7516 | |
7517 | if (RecordDecl *Record = Ty->getAsRecordDecl()) { |
7518 | if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Record)) { |
7519 | for (CXXBaseSpecifier &BS : CXXRD->bases()) |
7520 | if (!checkBitCastConstexprEligibilityType(Loc, BS.getType(), Info, Ctx, |
7521 | CheckingDest)) |
7522 | return note(1, BS.getType(), BS.getBeginLoc()); |
7523 | } |
7524 | for (FieldDecl *FD : Record->fields()) { |
7525 | if (FD->getType()->isReferenceType()) |
7526 | return diag(4); |
7527 | if (!checkBitCastConstexprEligibilityType(Loc, FD->getType(), Info, Ctx, |
7528 | CheckingDest)) |
7529 | return note(0, FD->getType(), FD->getBeginLoc()); |
7530 | } |
7531 | } |
7532 | |
7533 | if (Ty->isArrayType() && |
7534 | !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty), |
7535 | Info, Ctx, CheckingDest)) |
7536 | return false; |
7537 | |
7538 | return true; |
7539 | } |
7540 | |
7541 | static bool checkBitCastConstexprEligibility(EvalInfo *Info, |
7542 | const ASTContext &Ctx, |
7543 | const CastExpr *BCE) { |
7544 | bool DestOK = checkBitCastConstexprEligibilityType( |
7545 | BCE->getBeginLoc(), BCE->getType(), Info, Ctx, true); |
7546 | bool SourceOK = DestOK && checkBitCastConstexprEligibilityType( |
7547 | BCE->getBeginLoc(), |
7548 | BCE->getSubExpr()->getType(), Info, Ctx, false); |
7549 | return SourceOK; |
7550 | } |
7551 | |
7552 | static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7553 | const APValue &SourceRValue, |
7554 | const CastExpr *BCE) { |
7555 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7556 | "no host or target supports non 8-bit chars" ); |
7557 | |
7558 | if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE)) |
7559 | return false; |
7560 | |
7561 | // Read out SourceValue into a char buffer. |
7562 | std::optional<BitCastBuffer> Buffer = |
7563 | APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE); |
7564 | if (!Buffer) |
7565 | return false; |
7566 | |
7567 | // Write out the buffer into a new APValue. |
7568 | std::optional<APValue> MaybeDestValue = |
7569 | BufferToAPValueConverter::convert(Info, *Buffer, BCE); |
7570 | if (!MaybeDestValue) |
7571 | return false; |
7572 | |
7573 | DestValue = std::move(*MaybeDestValue); |
7574 | return true; |
7575 | } |
7576 | |
7577 | static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue, |
7578 | APValue &SourceValue, |
7579 | const CastExpr *BCE) { |
7580 | assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 && |
7581 | "no host or target supports non 8-bit chars" ); |
7582 | assert(SourceValue.isLValue() && |
7583 | "LValueToRValueBitcast requires an lvalue operand!" ); |
7584 | |
7585 | LValue SourceLValue; |
7586 | APValue SourceRValue; |
7587 | SourceLValue.setFrom(Ctx&: Info.Ctx, V: SourceValue); |
7588 | if (!handleLValueToRValueConversion( |
7589 | Info, BCE, BCE->getSubExpr()->getType().withConst(), SourceLValue, |
7590 | SourceRValue, /*WantObjectRepresentation=*/true)) |
7591 | return false; |
7592 | |
7593 | return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE); |
7594 | } |
7595 | |
7596 | template <class Derived> |
7597 | class ExprEvaluatorBase |
7598 | : public ConstStmtVisitor<Derived, bool> { |
7599 | private: |
7600 | Derived &getDerived() { return static_cast<Derived&>(*this); } |
7601 | bool DerivedSuccess(const APValue &V, const Expr *E) { |
7602 | return getDerived().Success(V, E); |
7603 | } |
7604 | bool DerivedZeroInitialization(const Expr *E) { |
7605 | return getDerived().ZeroInitialization(E); |
7606 | } |
7607 | |
7608 | // Check whether a conditional operator with a non-constant condition is a |
7609 | // potential constant expression. If neither arm is a potential constant |
7610 | // expression, then the conditional operator is not either. |
7611 | template<typename ConditionalOperator> |
7612 | void CheckPotentialConstantConditional(const ConditionalOperator *E) { |
7613 | assert(Info.checkingPotentialConstantExpression()); |
7614 | |
7615 | // Speculatively evaluate both arms. |
7616 | SmallVector<PartialDiagnosticAt, 8> Diag; |
7617 | { |
7618 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7619 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7620 | if (Diag.empty()) |
7621 | return; |
7622 | } |
7623 | |
7624 | { |
7625 | SpeculativeEvaluationRAII Speculate(Info, &Diag); |
7626 | Diag.clear(); |
7627 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7628 | if (Diag.empty()) |
7629 | return; |
7630 | } |
7631 | |
7632 | Error(E, diag::note_constexpr_conditional_never_const); |
7633 | } |
7634 | |
7635 | |
7636 | template<typename ConditionalOperator> |
7637 | bool HandleConditionalOperator(const ConditionalOperator *E) { |
7638 | bool BoolResult; |
7639 | if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) { |
7640 | if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) { |
7641 | CheckPotentialConstantConditional(E); |
7642 | return false; |
7643 | } |
7644 | if (Info.noteFailure()) { |
7645 | StmtVisitorTy::Visit(E->getTrueExpr()); |
7646 | StmtVisitorTy::Visit(E->getFalseExpr()); |
7647 | } |
7648 | return false; |
7649 | } |
7650 | |
7651 | Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr(); |
7652 | return StmtVisitorTy::Visit(EvalExpr); |
7653 | } |
7654 | |
7655 | protected: |
7656 | EvalInfo &Info; |
7657 | typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy; |
7658 | typedef ExprEvaluatorBase ExprEvaluatorBaseTy; |
7659 | |
7660 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
7661 | return Info.CCEDiag(E, DiagId: D); |
7662 | } |
7663 | |
7664 | bool ZeroInitialization(const Expr *E) { return Error(E); } |
7665 | |
7666 | bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) { |
7667 | unsigned BuiltinOp = E->getBuiltinCallee(); |
7668 | return BuiltinOp != 0 && |
7669 | Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp); |
7670 | } |
7671 | |
7672 | public: |
7673 | ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {} |
7674 | |
7675 | EvalInfo &getEvalInfo() { return Info; } |
7676 | |
7677 | /// Report an evaluation error. This should only be called when an error is |
7678 | /// first discovered. When propagating an error, just return false. |
7679 | bool Error(const Expr *E, diag::kind D) { |
7680 | Info.FFDiag(E, DiagId: D) << E->getSourceRange(); |
7681 | return false; |
7682 | } |
7683 | bool Error(const Expr *E) { |
7684 | return Error(E, diag::note_invalid_subexpr_in_const_expr); |
7685 | } |
7686 | |
7687 | bool VisitStmt(const Stmt *) { |
7688 | llvm_unreachable("Expression evaluator should not be called on stmts" ); |
7689 | } |
7690 | bool VisitExpr(const Expr *E) { |
7691 | return Error(E); |
7692 | } |
7693 | |
7694 | bool VisitPredefinedExpr(const PredefinedExpr *E) { |
7695 | return StmtVisitorTy::Visit(E->getFunctionName()); |
7696 | } |
7697 | bool VisitConstantExpr(const ConstantExpr *E) { |
7698 | if (E->hasAPValueResult()) |
7699 | return DerivedSuccess(V: E->getAPValueResult(), E); |
7700 | |
7701 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7702 | } |
7703 | |
7704 | bool VisitParenExpr(const ParenExpr *E) |
7705 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7706 | bool VisitUnaryExtension(const UnaryOperator *E) |
7707 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7708 | bool VisitUnaryPlus(const UnaryOperator *E) |
7709 | { return StmtVisitorTy::Visit(E->getSubExpr()); } |
7710 | bool VisitChooseExpr(const ChooseExpr *E) |
7711 | { return StmtVisitorTy::Visit(E->getChosenSubExpr()); } |
7712 | bool VisitGenericSelectionExpr(const GenericSelectionExpr *E) |
7713 | { return StmtVisitorTy::Visit(E->getResultExpr()); } |
7714 | bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E) |
7715 | { return StmtVisitorTy::Visit(E->getReplacement()); } |
7716 | bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) { |
7717 | TempVersionRAII RAII(*Info.CurrentCall); |
7718 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7719 | return StmtVisitorTy::Visit(E->getExpr()); |
7720 | } |
7721 | bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) { |
7722 | TempVersionRAII RAII(*Info.CurrentCall); |
7723 | // The initializer may not have been parsed yet, or might be erroneous. |
7724 | if (!E->getExpr()) |
7725 | return Error(E); |
7726 | SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope); |
7727 | return StmtVisitorTy::Visit(E->getExpr()); |
7728 | } |
7729 | |
7730 | bool VisitExprWithCleanups(const ExprWithCleanups *E) { |
7731 | FullExpressionRAII Scope(Info); |
7732 | return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy(); |
7733 | } |
7734 | |
7735 | // Temporaries are registered when created, so we don't care about |
7736 | // CXXBindTemporaryExpr. |
7737 | bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
7738 | return StmtVisitorTy::Visit(E->getSubExpr()); |
7739 | } |
7740 | |
7741 | bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) { |
7742 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 0; |
7743 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7744 | } |
7745 | bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) { |
7746 | if (!Info.Ctx.getLangOpts().CPlusPlus20) |
7747 | CCEDiag(E, diag::note_constexpr_invalid_cast) << 1; |
7748 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7749 | } |
7750 | bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) { |
7751 | return static_cast<Derived*>(this)->VisitCastExpr(E); |
7752 | } |
7753 | |
7754 | bool VisitBinaryOperator(const BinaryOperator *E) { |
7755 | switch (E->getOpcode()) { |
7756 | default: |
7757 | return Error(E); |
7758 | |
7759 | case BO_Comma: |
7760 | VisitIgnoredValue(E: E->getLHS()); |
7761 | return StmtVisitorTy::Visit(E->getRHS()); |
7762 | |
7763 | case BO_PtrMemD: |
7764 | case BO_PtrMemI: { |
7765 | LValue Obj; |
7766 | if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj)) |
7767 | return false; |
7768 | APValue Result; |
7769 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Obj, Result)) |
7770 | return false; |
7771 | return DerivedSuccess(V: Result, E); |
7772 | } |
7773 | } |
7774 | } |
7775 | |
7776 | bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) { |
7777 | return StmtVisitorTy::Visit(E->getSemanticForm()); |
7778 | } |
7779 | |
7780 | bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) { |
7781 | // Evaluate and cache the common expression. We treat it as a temporary, |
7782 | // even though it's not quite the same thing. |
7783 | LValue CommonLV; |
7784 | if (!Evaluate(Result&: Info.CurrentCall->createTemporary( |
7785 | E->getOpaqueValue(), |
7786 | getStorageType(Info.Ctx, E->getOpaqueValue()), |
7787 | ScopeKind::FullExpression, CommonLV), |
7788 | Info, E: E->getCommon())) |
7789 | return false; |
7790 | |
7791 | return HandleConditionalOperator(E); |
7792 | } |
7793 | |
7794 | bool VisitConditionalOperator(const ConditionalOperator *E) { |
7795 | bool IsBcpCall = false; |
7796 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
7797 | // the result is a constant expression if it can be folded without |
7798 | // side-effects. This is an important GNU extension. See GCC PR38377 |
7799 | // for discussion. |
7800 | if (const CallExpr *CallCE = |
7801 | dyn_cast<CallExpr>(E->getCond()->IgnoreParenCasts())) |
7802 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
7803 | IsBcpCall = true; |
7804 | |
7805 | // Always assume __builtin_constant_p(...) ? ... : ... is a potential |
7806 | // constant expression; we can't check whether it's potentially foldable. |
7807 | // FIXME: We should instead treat __builtin_constant_p as non-constant if |
7808 | // it would return 'false' in this mode. |
7809 | if (Info.checkingPotentialConstantExpression() && IsBcpCall) |
7810 | return false; |
7811 | |
7812 | FoldConstant Fold(Info, IsBcpCall); |
7813 | if (!HandleConditionalOperator(E)) { |
7814 | Fold.keepDiagnostics(); |
7815 | return false; |
7816 | } |
7817 | |
7818 | return true; |
7819 | } |
7820 | |
7821 | bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) { |
7822 | if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E); |
7823 | Value && !Value->isAbsent()) |
7824 | return DerivedSuccess(V: *Value, E); |
7825 | |
7826 | const Expr *Source = E->getSourceExpr(); |
7827 | if (!Source) |
7828 | return Error(E); |
7829 | if (Source == E) { |
7830 | assert(0 && "OpaqueValueExpr recursively refers to itself" ); |
7831 | return Error(E); |
7832 | } |
7833 | return StmtVisitorTy::Visit(Source); |
7834 | } |
7835 | |
7836 | bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) { |
7837 | for (const Expr *SemE : E->semantics()) { |
7838 | if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) { |
7839 | // FIXME: We can't handle the case where an OpaqueValueExpr is also the |
7840 | // result expression: there could be two different LValues that would |
7841 | // refer to the same object in that case, and we can't model that. |
7842 | if (SemE == E->getResultExpr()) |
7843 | return Error(E); |
7844 | |
7845 | // Unique OVEs get evaluated if and when we encounter them when |
7846 | // emitting the rest of the semantic form, rather than eagerly. |
7847 | if (OVE->isUnique()) |
7848 | continue; |
7849 | |
7850 | LValue LV; |
7851 | if (!Evaluate(Info.CurrentCall->createTemporary( |
7852 | OVE, getStorageType(Info.Ctx, OVE), |
7853 | ScopeKind::FullExpression, LV), |
7854 | Info, OVE->getSourceExpr())) |
7855 | return false; |
7856 | } else if (SemE == E->getResultExpr()) { |
7857 | if (!StmtVisitorTy::Visit(SemE)) |
7858 | return false; |
7859 | } else { |
7860 | if (!EvaluateIgnoredValue(Info, E: SemE)) |
7861 | return false; |
7862 | } |
7863 | } |
7864 | return true; |
7865 | } |
7866 | |
7867 | bool VisitCallExpr(const CallExpr *E) { |
7868 | APValue Result; |
7869 | if (!handleCallExpr(E, Result, ResultSlot: nullptr)) |
7870 | return false; |
7871 | return DerivedSuccess(V: Result, E); |
7872 | } |
7873 | |
7874 | bool handleCallExpr(const CallExpr *E, APValue &Result, |
7875 | const LValue *ResultSlot) { |
7876 | CallScopeRAII CallScope(Info); |
7877 | |
7878 | const Expr *Callee = E->getCallee()->IgnoreParens(); |
7879 | QualType CalleeType = Callee->getType(); |
7880 | |
7881 | const FunctionDecl *FD = nullptr; |
7882 | LValue *This = nullptr, ThisVal; |
7883 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
7884 | bool HasQualifier = false; |
7885 | |
7886 | CallRef Call; |
7887 | |
7888 | // Extract function decl and 'this' pointer from the callee. |
7889 | if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) { |
7890 | const CXXMethodDecl *Member = nullptr; |
7891 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Callee)) { |
7892 | // Explicit bound member calls, such as x.f() or p->g(); |
7893 | if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ThisVal)) |
7894 | return false; |
7895 | Member = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
7896 | if (!Member) |
7897 | return Error(Callee); |
7898 | This = &ThisVal; |
7899 | HasQualifier = ME->hasQualifier(); |
7900 | } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Callee)) { |
7901 | // Indirect bound member calls ('.*' or '->*'). |
7902 | const ValueDecl *D = |
7903 | HandleMemberPointerAccess(Info, BO: BE, LV&: ThisVal, IncludeMember: false); |
7904 | if (!D) |
7905 | return false; |
7906 | Member = dyn_cast<CXXMethodDecl>(D); |
7907 | if (!Member) |
7908 | return Error(Callee); |
7909 | This = &ThisVal; |
7910 | } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Callee)) { |
7911 | if (!Info.getLangOpts().CPlusPlus20) |
7912 | Info.CCEDiag(PDE, diag::note_constexpr_pseudo_destructor); |
7913 | return EvaluateObjectArgument(Info, PDE->getBase(), ThisVal) && |
7914 | HandleDestruction(Info, PDE, ThisVal, PDE->getDestroyedType()); |
7915 | } else |
7916 | return Error(Callee); |
7917 | FD = Member; |
7918 | } else if (CalleeType->isFunctionPointerType()) { |
7919 | LValue CalleeLV; |
7920 | if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info)) |
7921 | return false; |
7922 | |
7923 | if (!CalleeLV.getLValueOffset().isZero()) |
7924 | return Error(Callee); |
7925 | if (CalleeLV.isNullPointer()) { |
7926 | Info.FFDiag(Callee, diag::note_constexpr_null_callee) |
7927 | << const_cast<Expr *>(Callee); |
7928 | return false; |
7929 | } |
7930 | FD = dyn_cast_or_null<FunctionDecl>( |
7931 | CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>()); |
7932 | if (!FD) |
7933 | return Error(Callee); |
7934 | // Don't call function pointers which have been cast to some other type. |
7935 | // Per DR (no number yet), the caller and callee can differ in noexcept. |
7936 | if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec( |
7937 | T: CalleeType->getPointeeType(), U: FD->getType())) { |
7938 | return Error(E); |
7939 | } |
7940 | |
7941 | // For an (overloaded) assignment expression, evaluate the RHS before the |
7942 | // LHS. |
7943 | auto *OCE = dyn_cast<CXXOperatorCallExpr>(E); |
7944 | if (OCE && OCE->isAssignmentOp()) { |
7945 | assert(Args.size() == 2 && "wrong number of arguments in assignment" ); |
7946 | Call = Info.CurrentCall->createCall(Callee: FD); |
7947 | bool HasThis = false; |
7948 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
7949 | HasThis = MD->isImplicitObjectMemberFunction(); |
7950 | if (!EvaluateArgs(HasThis ? Args.slice(1) : Args, Call, Info, FD, |
7951 | /*RightToLeft=*/true)) |
7952 | return false; |
7953 | } |
7954 | |
7955 | // Overloaded operator calls to member functions are represented as normal |
7956 | // calls with '*this' as the first argument. |
7957 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
7958 | if (MD && |
7959 | (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) { |
7960 | // FIXME: When selecting an implicit conversion for an overloaded |
7961 | // operator delete, we sometimes try to evaluate calls to conversion |
7962 | // operators without a 'this' parameter! |
7963 | if (Args.empty()) |
7964 | return Error(E); |
7965 | |
7966 | if (!EvaluateObjectArgument(Info, Args[0], ThisVal)) |
7967 | return false; |
7968 | |
7969 | // If we are calling a static operator, the 'this' argument needs to be |
7970 | // ignored after being evaluated. |
7971 | if (MD->isInstance()) |
7972 | This = &ThisVal; |
7973 | |
7974 | // If this is syntactically a simple assignment using a trivial |
7975 | // assignment operator, start the lifetimes of union members as needed, |
7976 | // per C++20 [class.union]5. |
7977 | if (Info.getLangOpts().CPlusPlus20 && OCE && |
7978 | OCE->getOperator() == OO_Equal && MD->isTrivial() && |
7979 | !MaybeHandleUnionActiveMemberChange(Info, Args[0], ThisVal)) |
7980 | return false; |
7981 | |
7982 | Args = Args.slice(1); |
7983 | } else if (MD && MD->isLambdaStaticInvoker()) { |
7984 | // Map the static invoker for the lambda back to the call operator. |
7985 | // Conveniently, we don't have to slice out the 'this' argument (as is |
7986 | // being done for the non-static case), since a static member function |
7987 | // doesn't have an implicit argument passed in. |
7988 | const CXXRecordDecl *ClosureClass = MD->getParent(); |
7989 | assert( |
7990 | ClosureClass->captures_begin() == ClosureClass->captures_end() && |
7991 | "Number of captures must be zero for conversion to function-ptr" ); |
7992 | |
7993 | const CXXMethodDecl *LambdaCallOp = |
7994 | ClosureClass->getLambdaCallOperator(); |
7995 | |
7996 | // Set 'FD', the function that will be called below, to the call |
7997 | // operator. If the closure object represents a generic lambda, find |
7998 | // the corresponding specialization of the call operator. |
7999 | |
8000 | if (ClosureClass->isGenericLambda()) { |
8001 | assert(MD->isFunctionTemplateSpecialization() && |
8002 | "A generic lambda's static-invoker function must be a " |
8003 | "template specialization" ); |
8004 | const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs(); |
8005 | FunctionTemplateDecl *CallOpTemplate = |
8006 | LambdaCallOp->getDescribedFunctionTemplate(); |
8007 | void *InsertPos = nullptr; |
8008 | FunctionDecl *CorrespondingCallOpSpecialization = |
8009 | CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos); |
8010 | assert(CorrespondingCallOpSpecialization && |
8011 | "We must always have a function call operator specialization " |
8012 | "that corresponds to our static invoker specialization" ); |
8013 | assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization)); |
8014 | FD = CorrespondingCallOpSpecialization; |
8015 | } else |
8016 | FD = LambdaCallOp; |
8017 | } else if (FD->isReplaceableGlobalAllocationFunction()) { |
8018 | if (FD->getDeclName().getCXXOverloadedOperator() == OO_New || |
8019 | FD->getDeclName().getCXXOverloadedOperator() == OO_Array_New) { |
8020 | LValue Ptr; |
8021 | if (!HandleOperatorNewCall(Info, E, Result&: Ptr)) |
8022 | return false; |
8023 | Ptr.moveInto(V&: Result); |
8024 | return CallScope.destroy(); |
8025 | } else { |
8026 | return HandleOperatorDeleteCall(Info, E) && CallScope.destroy(); |
8027 | } |
8028 | } |
8029 | } else |
8030 | return Error(E); |
8031 | |
8032 | // Evaluate the arguments now if we've not already done so. |
8033 | if (!Call) { |
8034 | Call = Info.CurrentCall->createCall(Callee: FD); |
8035 | if (!EvaluateArgs(Args, Call, Info, FD)) |
8036 | return false; |
8037 | } |
8038 | |
8039 | SmallVector<QualType, 4> CovariantAdjustmentPath; |
8040 | if (This) { |
8041 | auto *NamedMember = dyn_cast<CXXMethodDecl>(FD); |
8042 | if (NamedMember && NamedMember->isVirtual() && !HasQualifier) { |
8043 | // Perform virtual dispatch, if necessary. |
8044 | FD = HandleVirtualDispatch(Info, E, *This, NamedMember, |
8045 | CovariantAdjustmentPath); |
8046 | if (!FD) |
8047 | return false; |
8048 | } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) { |
8049 | // Check that the 'this' pointer points to an object of the right type. |
8050 | // FIXME: If this is an assignment operator call, we may need to change |
8051 | // the active union member before we check this. |
8052 | if (!checkNonVirtualMemberCallThisPointer(Info, E, *This, NamedMember)) |
8053 | return false; |
8054 | } |
8055 | } |
8056 | |
8057 | // Destructor calls are different enough that they have their own codepath. |
8058 | if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) { |
8059 | assert(This && "no 'this' pointer for destructor call" ); |
8060 | return HandleDestruction(Info, E, *This, |
8061 | Info.Ctx.getRecordType(Decl: DD->getParent())) && |
8062 | CallScope.destroy(); |
8063 | } |
8064 | |
8065 | const FunctionDecl *Definition = nullptr; |
8066 | Stmt *Body = FD->getBody(Definition); |
8067 | |
8068 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body) || |
8069 | !HandleFunctionCall(E->getExprLoc(), Definition, This, E, Args, Call, |
8070 | Body, Info, Result, ResultSlot)) |
8071 | return false; |
8072 | |
8073 | if (!CovariantAdjustmentPath.empty() && |
8074 | !HandleCovariantReturnAdjustment(Info, E, Result, |
8075 | CovariantAdjustmentPath)) |
8076 | return false; |
8077 | |
8078 | return CallScope.destroy(); |
8079 | } |
8080 | |
8081 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8082 | return StmtVisitorTy::Visit(E->getInitializer()); |
8083 | } |
8084 | bool VisitInitListExpr(const InitListExpr *E) { |
8085 | if (E->getNumInits() == 0) |
8086 | return DerivedZeroInitialization(E); |
8087 | if (E->getNumInits() == 1) |
8088 | return StmtVisitorTy::Visit(E->getInit(Init: 0)); |
8089 | return Error(E); |
8090 | } |
8091 | bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) { |
8092 | return DerivedZeroInitialization(E); |
8093 | } |
8094 | bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) { |
8095 | return DerivedZeroInitialization(E); |
8096 | } |
8097 | bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) { |
8098 | return DerivedZeroInitialization(E); |
8099 | } |
8100 | |
8101 | /// A member expression where the object is a prvalue is itself a prvalue. |
8102 | bool VisitMemberExpr(const MemberExpr *E) { |
8103 | assert(!Info.Ctx.getLangOpts().CPlusPlus11 && |
8104 | "missing temporary materialization conversion" ); |
8105 | assert(!E->isArrow() && "missing call to bound member function?" ); |
8106 | |
8107 | APValue Val; |
8108 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8109 | return false; |
8110 | |
8111 | QualType BaseTy = E->getBase()->getType(); |
8112 | |
8113 | const FieldDecl *FD = dyn_cast<FieldDecl>(E->getMemberDecl()); |
8114 | if (!FD) return Error(E); |
8115 | assert(!FD->getType()->isReferenceType() && "prvalue reference?" ); |
8116 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8117 | FD->getParent()->getCanonicalDecl() && "record / field mismatch" ); |
8118 | |
8119 | // Note: there is no lvalue base here. But this case should only ever |
8120 | // happen in C or in C++98, where we cannot be evaluating a constexpr |
8121 | // constructor, which is the only case the base matters. |
8122 | CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy); |
8123 | SubobjectDesignator Designator(BaseTy); |
8124 | Designator.addDeclUnchecked(FD); |
8125 | |
8126 | APValue Result; |
8127 | return extractSubobject(Info, E, Obj, Designator, Result) && |
8128 | DerivedSuccess(V: Result, E); |
8129 | } |
8130 | |
8131 | bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) { |
8132 | APValue Val; |
8133 | if (!Evaluate(Result&: Val, Info, E: E->getBase())) |
8134 | return false; |
8135 | |
8136 | if (Val.isVector()) { |
8137 | SmallVector<uint32_t, 4> Indices; |
8138 | E->getEncodedElementAccess(Elts&: Indices); |
8139 | if (Indices.size() == 1) { |
8140 | // Return scalar. |
8141 | return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E); |
8142 | } else { |
8143 | // Construct new APValue vector. |
8144 | SmallVector<APValue, 4> Elts; |
8145 | for (unsigned I = 0; I < Indices.size(); ++I) { |
8146 | Elts.push_back(Elt: Val.getVectorElt(I: Indices[I])); |
8147 | } |
8148 | APValue VecResult(Elts.data(), Indices.size()); |
8149 | return DerivedSuccess(V: VecResult, E); |
8150 | } |
8151 | } |
8152 | |
8153 | return false; |
8154 | } |
8155 | |
8156 | bool VisitCastExpr(const CastExpr *E) { |
8157 | switch (E->getCastKind()) { |
8158 | default: |
8159 | break; |
8160 | |
8161 | case CK_AtomicToNonAtomic: { |
8162 | APValue AtomicVal; |
8163 | // This does not need to be done in place even for class/array types: |
8164 | // atomic-to-non-atomic conversion implies copying the object |
8165 | // representation. |
8166 | if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr())) |
8167 | return false; |
8168 | return DerivedSuccess(V: AtomicVal, E); |
8169 | } |
8170 | |
8171 | case CK_NoOp: |
8172 | case CK_UserDefinedConversion: |
8173 | return StmtVisitorTy::Visit(E->getSubExpr()); |
8174 | |
8175 | case CK_LValueToRValue: { |
8176 | LValue LVal; |
8177 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info)) |
8178 | return false; |
8179 | APValue RVal; |
8180 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
8181 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
8182 | LVal, RVal)) |
8183 | return false; |
8184 | return DerivedSuccess(V: RVal, E); |
8185 | } |
8186 | case CK_LValueToRValueBitCast: { |
8187 | APValue DestValue, SourceValue; |
8188 | if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr())) |
8189 | return false; |
8190 | if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E)) |
8191 | return false; |
8192 | return DerivedSuccess(V: DestValue, E); |
8193 | } |
8194 | |
8195 | case CK_AddressSpaceConversion: { |
8196 | APValue Value; |
8197 | if (!Evaluate(Result&: Value, Info, E: E->getSubExpr())) |
8198 | return false; |
8199 | return DerivedSuccess(V: Value, E); |
8200 | } |
8201 | } |
8202 | |
8203 | return Error(E); |
8204 | } |
8205 | |
8206 | bool VisitUnaryPostInc(const UnaryOperator *UO) { |
8207 | return VisitUnaryPostIncDec(UO); |
8208 | } |
8209 | bool VisitUnaryPostDec(const UnaryOperator *UO) { |
8210 | return VisitUnaryPostIncDec(UO); |
8211 | } |
8212 | bool VisitUnaryPostIncDec(const UnaryOperator *UO) { |
8213 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8214 | return Error(UO); |
8215 | |
8216 | LValue LVal; |
8217 | if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info)) |
8218 | return false; |
8219 | APValue RVal; |
8220 | if (!handleIncDec(this->Info, UO, LVal, UO->getSubExpr()->getType(), |
8221 | UO->isIncrementOp(), &RVal)) |
8222 | return false; |
8223 | return DerivedSuccess(V: RVal, E: UO); |
8224 | } |
8225 | |
8226 | bool VisitStmtExpr(const StmtExpr *E) { |
8227 | // We will have checked the full-expressions inside the statement expression |
8228 | // when they were completed, and don't need to check them again now. |
8229 | llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior, |
8230 | false); |
8231 | |
8232 | const CompoundStmt *CS = E->getSubStmt(); |
8233 | if (CS->body_empty()) |
8234 | return true; |
8235 | |
8236 | BlockScopeRAII Scope(Info); |
8237 | for (CompoundStmt::const_body_iterator BI = CS->body_begin(), |
8238 | BE = CS->body_end(); |
8239 | /**/; ++BI) { |
8240 | if (BI + 1 == BE) { |
8241 | const Expr *FinalExpr = dyn_cast<Expr>(*BI); |
8242 | if (!FinalExpr) { |
8243 | Info.FFDiag((*BI)->getBeginLoc(), |
8244 | diag::note_constexpr_stmt_expr_unsupported); |
8245 | return false; |
8246 | } |
8247 | return this->Visit(FinalExpr) && Scope.destroy(); |
8248 | } |
8249 | |
8250 | APValue ReturnValue; |
8251 | StmtResult Result = { .Value: ReturnValue, .Slot: nullptr }; |
8252 | EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI); |
8253 | if (ESR != ESR_Succeeded) { |
8254 | // FIXME: If the statement-expression terminated due to 'return', |
8255 | // 'break', or 'continue', it would be nice to propagate that to |
8256 | // the outer statement evaluation rather than bailing out. |
8257 | if (ESR != ESR_Failed) |
8258 | Info.FFDiag((*BI)->getBeginLoc(), |
8259 | diag::note_constexpr_stmt_expr_unsupported); |
8260 | return false; |
8261 | } |
8262 | } |
8263 | |
8264 | llvm_unreachable("Return from function from the loop above." ); |
8265 | } |
8266 | |
8267 | bool VisitPackIndexingExpr(const PackIndexingExpr *E) { |
8268 | return StmtVisitorTy::Visit(E->getSelectedExpr()); |
8269 | } |
8270 | |
8271 | /// Visit a value which is evaluated, but whose value is ignored. |
8272 | void VisitIgnoredValue(const Expr *E) { |
8273 | EvaluateIgnoredValue(Info, E); |
8274 | } |
8275 | |
8276 | /// Potentially visit a MemberExpr's base expression. |
8277 | void VisitIgnoredBaseExpression(const Expr *E) { |
8278 | // While MSVC doesn't evaluate the base expression, it does diagnose the |
8279 | // presence of side-effecting behavior. |
8280 | if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx)) |
8281 | return; |
8282 | VisitIgnoredValue(E); |
8283 | } |
8284 | }; |
8285 | |
8286 | } // namespace |
8287 | |
8288 | //===----------------------------------------------------------------------===// |
8289 | // Common base class for lvalue and temporary evaluation. |
8290 | //===----------------------------------------------------------------------===// |
8291 | namespace { |
8292 | template<class Derived> |
8293 | class LValueExprEvaluatorBase |
8294 | : public ExprEvaluatorBase<Derived> { |
8295 | protected: |
8296 | LValue &Result; |
8297 | bool InvalidBaseOK; |
8298 | typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy; |
8299 | typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy; |
8300 | |
8301 | bool Success(APValue::LValueBase B) { |
8302 | Result.set(B); |
8303 | return true; |
8304 | } |
8305 | |
8306 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8307 | return EvaluatePointer(E, Result, this->Info, InvalidBaseOK); |
8308 | } |
8309 | |
8310 | public: |
8311 | LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) |
8312 | : ExprEvaluatorBaseTy(Info), Result(Result), |
8313 | InvalidBaseOK(InvalidBaseOK) {} |
8314 | |
8315 | bool Success(const APValue &V, const Expr *E) { |
8316 | Result.setFrom(Ctx&: this->Info.Ctx, V); |
8317 | return true; |
8318 | } |
8319 | |
8320 | bool VisitMemberExpr(const MemberExpr *E) { |
8321 | // Handle non-static data members. |
8322 | QualType BaseTy; |
8323 | bool EvalOK; |
8324 | if (E->isArrow()) { |
8325 | EvalOK = evaluatePointer(E: E->getBase(), Result); |
8326 | BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType(); |
8327 | } else if (E->getBase()->isPRValue()) { |
8328 | assert(E->getBase()->getType()->isRecordType()); |
8329 | EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info); |
8330 | BaseTy = E->getBase()->getType(); |
8331 | } else { |
8332 | EvalOK = this->Visit(E->getBase()); |
8333 | BaseTy = E->getBase()->getType(); |
8334 | } |
8335 | if (!EvalOK) { |
8336 | if (!InvalidBaseOK) |
8337 | return false; |
8338 | Result.setInvalid(E); |
8339 | return true; |
8340 | } |
8341 | |
8342 | const ValueDecl *MD = E->getMemberDecl(); |
8343 | if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) { |
8344 | assert(BaseTy->castAs<RecordType>()->getDecl()->getCanonicalDecl() == |
8345 | FD->getParent()->getCanonicalDecl() && "record / field mismatch" ); |
8346 | (void)BaseTy; |
8347 | if (!HandleLValueMember(this->Info, E, Result, FD)) |
8348 | return false; |
8349 | } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) { |
8350 | if (!HandleLValueIndirectMember(this->Info, E, Result, IFD)) |
8351 | return false; |
8352 | } else |
8353 | return this->Error(E); |
8354 | |
8355 | if (MD->getType()->isReferenceType()) { |
8356 | APValue RefValue; |
8357 | if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result, |
8358 | RefValue)) |
8359 | return false; |
8360 | return Success(RefValue, E); |
8361 | } |
8362 | return true; |
8363 | } |
8364 | |
8365 | bool VisitBinaryOperator(const BinaryOperator *E) { |
8366 | switch (E->getOpcode()) { |
8367 | default: |
8368 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
8369 | |
8370 | case BO_PtrMemD: |
8371 | case BO_PtrMemI: |
8372 | return HandleMemberPointerAccess(this->Info, E, Result); |
8373 | } |
8374 | } |
8375 | |
8376 | bool VisitCastExpr(const CastExpr *E) { |
8377 | switch (E->getCastKind()) { |
8378 | default: |
8379 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
8380 | |
8381 | case CK_DerivedToBase: |
8382 | case CK_UncheckedDerivedToBase: |
8383 | if (!this->Visit(E->getSubExpr())) |
8384 | return false; |
8385 | |
8386 | // Now figure out the necessary offset to add to the base LV to get from |
8387 | // the derived class to the base class. |
8388 | return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(), |
8389 | Result); |
8390 | } |
8391 | } |
8392 | }; |
8393 | } |
8394 | |
8395 | //===----------------------------------------------------------------------===// |
8396 | // LValue Evaluation |
8397 | // |
8398 | // This is used for evaluating lvalues (in C and C++), xvalues (in C++11), |
8399 | // function designators (in C), decl references to void objects (in C), and |
8400 | // temporaries (if building with -Wno-address-of-temporary). |
8401 | // |
8402 | // LValue evaluation produces values comprising a base expression of one of the |
8403 | // following types: |
8404 | // - Declarations |
8405 | // * VarDecl |
8406 | // * FunctionDecl |
8407 | // - Literals |
8408 | // * CompoundLiteralExpr in C (and in global scope in C++) |
8409 | // * StringLiteral |
8410 | // * PredefinedExpr |
8411 | // * ObjCStringLiteralExpr |
8412 | // * ObjCEncodeExpr |
8413 | // * AddrLabelExpr |
8414 | // * BlockExpr |
8415 | // * CallExpr for a MakeStringConstant builtin |
8416 | // - typeid(T) expressions, as TypeInfoLValues |
8417 | // - Locals and temporaries |
8418 | // * MaterializeTemporaryExpr |
8419 | // * Any Expr, with a CallIndex indicating the function in which the temporary |
8420 | // was evaluated, for cases where the MaterializeTemporaryExpr is missing |
8421 | // from the AST (FIXME). |
8422 | // * A MaterializeTemporaryExpr that has static storage duration, with no |
8423 | // CallIndex, for a lifetime-extended temporary. |
8424 | // * The ConstantExpr that is currently being evaluated during evaluation of an |
8425 | // immediate invocation. |
8426 | // plus an offset in bytes. |
8427 | //===----------------------------------------------------------------------===// |
8428 | namespace { |
8429 | class LValueExprEvaluator |
8430 | : public LValueExprEvaluatorBase<LValueExprEvaluator> { |
8431 | public: |
8432 | LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) : |
8433 | LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {} |
8434 | |
8435 | bool VisitVarDecl(const Expr *E, const VarDecl *VD); |
8436 | bool VisitUnaryPreIncDec(const UnaryOperator *UO); |
8437 | |
8438 | bool VisitCallExpr(const CallExpr *E); |
8439 | bool VisitDeclRefExpr(const DeclRefExpr *E); |
8440 | bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(E); } |
8441 | bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E); |
8442 | bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E); |
8443 | bool VisitMemberExpr(const MemberExpr *E); |
8444 | bool VisitStringLiteral(const StringLiteral *E) { return Success(E); } |
8445 | bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(E); } |
8446 | bool VisitCXXTypeidExpr(const CXXTypeidExpr *E); |
8447 | bool VisitCXXUuidofExpr(const CXXUuidofExpr *E); |
8448 | bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E); |
8449 | bool VisitUnaryDeref(const UnaryOperator *E); |
8450 | bool VisitUnaryReal(const UnaryOperator *E); |
8451 | bool VisitUnaryImag(const UnaryOperator *E); |
8452 | bool VisitUnaryPreInc(const UnaryOperator *UO) { |
8453 | return VisitUnaryPreIncDec(UO); |
8454 | } |
8455 | bool VisitUnaryPreDec(const UnaryOperator *UO) { |
8456 | return VisitUnaryPreIncDec(UO); |
8457 | } |
8458 | bool VisitBinAssign(const BinaryOperator *BO); |
8459 | bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO); |
8460 | |
8461 | bool VisitCastExpr(const CastExpr *E) { |
8462 | switch (E->getCastKind()) { |
8463 | default: |
8464 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
8465 | |
8466 | case CK_LValueBitCast: |
8467 | this->CCEDiag(E, diag::note_constexpr_invalid_cast) |
8468 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
8469 | if (!Visit(E->getSubExpr())) |
8470 | return false; |
8471 | Result.Designator.setInvalid(); |
8472 | return true; |
8473 | |
8474 | case CK_BaseToDerived: |
8475 | if (!Visit(E->getSubExpr())) |
8476 | return false; |
8477 | return HandleBaseToDerivedCast(Info, E, Result); |
8478 | |
8479 | case CK_Dynamic: |
8480 | if (!Visit(E->getSubExpr())) |
8481 | return false; |
8482 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
8483 | } |
8484 | } |
8485 | }; |
8486 | } // end anonymous namespace |
8487 | |
8488 | /// Evaluate an expression as an lvalue. This can be legitimately called on |
8489 | /// expressions which are not glvalues, in three cases: |
8490 | /// * function designators in C, and |
8491 | /// * "extern void" objects |
8492 | /// * @selector() expressions in Objective-C |
8493 | static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info, |
8494 | bool InvalidBaseOK) { |
8495 | assert(!E->isValueDependent()); |
8496 | assert(E->isGLValue() || E->getType()->isFunctionType() || |
8497 | E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens())); |
8498 | return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
8499 | } |
8500 | |
8501 | bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) { |
8502 | const NamedDecl *D = E->getDecl(); |
8503 | if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl, |
8504 | UnnamedGlobalConstantDecl>(Val: D)) |
8505 | return Success(B: cast<ValueDecl>(Val: D)); |
8506 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
8507 | return VisitVarDecl(E, VD); |
8508 | if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D)) |
8509 | return Visit(BD->getBinding()); |
8510 | return Error(E); |
8511 | } |
8512 | |
8513 | |
8514 | bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) { |
8515 | |
8516 | // If we are within a lambda's call operator, check whether the 'VD' referred |
8517 | // to within 'E' actually represents a lambda-capture that maps to a |
8518 | // data-member/field within the closure object, and if so, evaluate to the |
8519 | // field or what the field refers to. |
8520 | if (Info.CurrentCall && isLambdaCallOperator(Info.CurrentCall->Callee) && |
8521 | isa<DeclRefExpr>(Val: E) && |
8522 | cast<DeclRefExpr>(Val: E)->refersToEnclosingVariableOrCapture()) { |
8523 | // We don't always have a complete capture-map when checking or inferring if |
8524 | // the function call operator meets the requirements of a constexpr function |
8525 | // - but we don't need to evaluate the captures to determine constexprness |
8526 | // (dcl.constexpr C++17). |
8527 | if (Info.checkingPotentialConstantExpression()) |
8528 | return false; |
8529 | |
8530 | if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) { |
8531 | const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee); |
8532 | |
8533 | // Static lambda function call operators can't have captures. We already |
8534 | // diagnosed this, so bail out here. |
8535 | if (MD->isStatic()) { |
8536 | assert(Info.CurrentCall->This == nullptr && |
8537 | "This should not be set for a static call operator" ); |
8538 | return false; |
8539 | } |
8540 | |
8541 | // Start with 'Result' referring to the complete closure object... |
8542 | if (MD->isExplicitObjectMemberFunction()) { |
8543 | APValue *RefValue = |
8544 | Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: MD->getParamDecl(0)); |
8545 | Result.setFrom(Ctx&: Info.Ctx, V: *RefValue); |
8546 | } else |
8547 | Result = *Info.CurrentCall->This; |
8548 | |
8549 | // ... then update it to refer to the field of the closure object |
8550 | // that represents the capture. |
8551 | if (!HandleLValueMember(Info, E, Result, FD)) |
8552 | return false; |
8553 | // And if the field is of reference type, update 'Result' to refer to what |
8554 | // the field refers to. |
8555 | if (FD->getType()->isReferenceType()) { |
8556 | APValue RVal; |
8557 | if (!handleLValueToRValueConversion(Info, E, FD->getType(), Result, |
8558 | RVal)) |
8559 | return false; |
8560 | Result.setFrom(Ctx&: Info.Ctx, V: RVal); |
8561 | } |
8562 | return true; |
8563 | } |
8564 | } |
8565 | |
8566 | CallStackFrame *Frame = nullptr; |
8567 | unsigned Version = 0; |
8568 | if (VD->hasLocalStorage()) { |
8569 | // Only if a local variable was declared in the function currently being |
8570 | // evaluated, do we expect to be able to find its value in the current |
8571 | // frame. (Otherwise it was likely declared in an enclosing context and |
8572 | // could either have a valid evaluatable value (for e.g. a constexpr |
8573 | // variable) or be ill-formed (and trigger an appropriate evaluation |
8574 | // diagnostic)). |
8575 | CallStackFrame *CurrFrame = Info.CurrentCall; |
8576 | if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) { |
8577 | // Function parameters are stored in some caller's frame. (Usually the |
8578 | // immediate caller, but for an inherited constructor they may be more |
8579 | // distant.) |
8580 | if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) { |
8581 | if (CurrFrame->Arguments) { |
8582 | VD = CurrFrame->Arguments.getOrigParam(PVD); |
8583 | Frame = |
8584 | Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first; |
8585 | Version = CurrFrame->Arguments.Version; |
8586 | } |
8587 | } else { |
8588 | Frame = CurrFrame; |
8589 | Version = CurrFrame->getCurrentTemporaryVersion(Key: VD); |
8590 | } |
8591 | } |
8592 | } |
8593 | |
8594 | if (!VD->getType()->isReferenceType()) { |
8595 | if (Frame) { |
8596 | Result.set({VD, Frame->Index, Version}); |
8597 | return true; |
8598 | } |
8599 | return Success(VD); |
8600 | } |
8601 | |
8602 | if (!Info.getLangOpts().CPlusPlus11) { |
8603 | Info.CCEDiag(E, diag::note_constexpr_ltor_non_integral, 1) |
8604 | << VD << VD->getType(); |
8605 | Info.Note(VD->getLocation(), diag::note_declared_at); |
8606 | } |
8607 | |
8608 | APValue *V; |
8609 | if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V)) |
8610 | return false; |
8611 | if (!V->hasValue()) { |
8612 | // FIXME: Is it possible for V to be indeterminate here? If so, we should |
8613 | // adjust the diagnostic to say that. |
8614 | if (!Info.checkingPotentialConstantExpression()) |
8615 | Info.FFDiag(E, diag::note_constexpr_use_uninit_reference); |
8616 | return false; |
8617 | } |
8618 | return Success(V: *V, E); |
8619 | } |
8620 | |
8621 | bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) { |
8622 | if (!IsConstantEvaluatedBuiltinCall(E)) |
8623 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8624 | |
8625 | switch (E->getBuiltinCallee()) { |
8626 | default: |
8627 | return false; |
8628 | case Builtin::BIas_const: |
8629 | case Builtin::BIforward: |
8630 | case Builtin::BIforward_like: |
8631 | case Builtin::BImove: |
8632 | case Builtin::BImove_if_noexcept: |
8633 | if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr()) |
8634 | return Visit(E->getArg(Arg: 0)); |
8635 | break; |
8636 | } |
8637 | |
8638 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
8639 | } |
8640 | |
8641 | bool LValueExprEvaluator::VisitMaterializeTemporaryExpr( |
8642 | const MaterializeTemporaryExpr *E) { |
8643 | // Walk through the expression to find the materialized temporary itself. |
8644 | SmallVector<const Expr *, 2> CommaLHSs; |
8645 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
8646 | const Expr *Inner = |
8647 | E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments); |
8648 | |
8649 | // If we passed any comma operators, evaluate their LHSs. |
8650 | for (const Expr *E : CommaLHSs) |
8651 | if (!EvaluateIgnoredValue(Info, E)) |
8652 | return false; |
8653 | |
8654 | // A materialized temporary with static storage duration can appear within the |
8655 | // result of a constant expression evaluation, so we need to preserve its |
8656 | // value for use outside this evaluation. |
8657 | APValue *Value; |
8658 | if (E->getStorageDuration() == SD_Static) { |
8659 | if (Info.EvalMode == EvalInfo::EM_ConstantFold) |
8660 | return false; |
8661 | // FIXME: What about SD_Thread? |
8662 | Value = E->getOrCreateValue(MayCreate: true); |
8663 | *Value = APValue(); |
8664 | Result.set(E); |
8665 | } else { |
8666 | Value = &Info.CurrentCall->createTemporary( |
8667 | Key: E, T: Inner->getType(), |
8668 | Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression |
8669 | : ScopeKind::Block, |
8670 | LV&: Result); |
8671 | } |
8672 | |
8673 | QualType Type = Inner->getType(); |
8674 | |
8675 | // Materialize the temporary itself. |
8676 | if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) { |
8677 | *Value = APValue(); |
8678 | return false; |
8679 | } |
8680 | |
8681 | // Adjust our lvalue to refer to the desired subobject. |
8682 | for (unsigned I = Adjustments.size(); I != 0; /**/) { |
8683 | --I; |
8684 | switch (Adjustments[I].Kind) { |
8685 | case SubobjectAdjustment::DerivedToBaseAdjustment: |
8686 | if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath, |
8687 | Type, Result)) |
8688 | return false; |
8689 | Type = Adjustments[I].DerivedToBase.BasePath->getType(); |
8690 | break; |
8691 | |
8692 | case SubobjectAdjustment::FieldAdjustment: |
8693 | if (!HandleLValueMember(Info, E, Result, Adjustments[I].Field)) |
8694 | return false; |
8695 | Type = Adjustments[I].Field->getType(); |
8696 | break; |
8697 | |
8698 | case SubobjectAdjustment::MemberPointerAdjustment: |
8699 | if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result, |
8700 | RHS: Adjustments[I].Ptr.RHS)) |
8701 | return false; |
8702 | Type = Adjustments[I].Ptr.MPT->getPointeeType(); |
8703 | break; |
8704 | } |
8705 | } |
8706 | |
8707 | return true; |
8708 | } |
8709 | |
8710 | bool |
8711 | LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) { |
8712 | assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) && |
8713 | "lvalue compound literal in c++?" ); |
8714 | // Defer visiting the literal until the lvalue-to-rvalue conversion. We can |
8715 | // only see this when folding in C, so there's no standard to follow here. |
8716 | return Success(E); |
8717 | } |
8718 | |
8719 | bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) { |
8720 | TypeInfoLValue TypeInfo; |
8721 | |
8722 | if (!E->isPotentiallyEvaluated()) { |
8723 | if (E->isTypeOperand()) |
8724 | TypeInfo = TypeInfoLValue(E->getTypeOperand(Context&: Info.Ctx).getTypePtr()); |
8725 | else |
8726 | TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr()); |
8727 | } else { |
8728 | if (!Info.Ctx.getLangOpts().CPlusPlus20) { |
8729 | Info.CCEDiag(E, diag::note_constexpr_typeid_polymorphic) |
8730 | << E->getExprOperand()->getType() |
8731 | << E->getExprOperand()->getSourceRange(); |
8732 | } |
8733 | |
8734 | if (!Visit(E->getExprOperand())) |
8735 | return false; |
8736 | |
8737 | std::optional<DynamicType> DynType = |
8738 | ComputeDynamicType(Info, E, Result, AK_TypeId); |
8739 | if (!DynType) |
8740 | return false; |
8741 | |
8742 | TypeInfo = |
8743 | TypeInfoLValue(Info.Ctx.getRecordType(DynType->Type).getTypePtr()); |
8744 | } |
8745 | |
8746 | return Success(APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType())); |
8747 | } |
8748 | |
8749 | bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) { |
8750 | return Success(E->getGuidDecl()); |
8751 | } |
8752 | |
8753 | bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) { |
8754 | // Handle static data members. |
8755 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) { |
8756 | VisitIgnoredBaseExpression(E: E->getBase()); |
8757 | return VisitVarDecl(E, VD); |
8758 | } |
8759 | |
8760 | // Handle static member functions. |
8761 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) { |
8762 | if (MD->isStatic()) { |
8763 | VisitIgnoredBaseExpression(E: E->getBase()); |
8764 | return Success(MD); |
8765 | } |
8766 | } |
8767 | |
8768 | // Handle non-static data members. |
8769 | return LValueExprEvaluatorBaseTy::VisitMemberExpr(E); |
8770 | } |
8771 | |
8772 | bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) { |
8773 | // FIXME: Deal with vectors as array subscript bases. |
8774 | if (E->getBase()->getType()->isVectorType() || |
8775 | E->getBase()->getType()->isSveVLSBuiltinType()) |
8776 | return Error(E); |
8777 | |
8778 | APSInt Index; |
8779 | bool Success = true; |
8780 | |
8781 | // C++17's rules require us to evaluate the LHS first, regardless of which |
8782 | // side is the base. |
8783 | for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) { |
8784 | if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result) |
8785 | : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) { |
8786 | if (!Info.noteFailure()) |
8787 | return false; |
8788 | Success = false; |
8789 | } |
8790 | } |
8791 | |
8792 | return Success && |
8793 | HandleLValueArrayAdjustment(Info, E, Result, E->getType(), Index); |
8794 | } |
8795 | |
8796 | bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) { |
8797 | return evaluatePointer(E: E->getSubExpr(), Result); |
8798 | } |
8799 | |
8800 | bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
8801 | if (!Visit(E->getSubExpr())) |
8802 | return false; |
8803 | // __real is a no-op on scalar lvalues. |
8804 | if (E->getSubExpr()->getType()->isAnyComplexType()) |
8805 | HandleLValueComplexElement(Info, E, Result, E->getType(), false); |
8806 | return true; |
8807 | } |
8808 | |
8809 | bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
8810 | assert(E->getSubExpr()->getType()->isAnyComplexType() && |
8811 | "lvalue __imag__ on scalar?" ); |
8812 | if (!Visit(E->getSubExpr())) |
8813 | return false; |
8814 | HandleLValueComplexElement(Info, E, Result, E->getType(), true); |
8815 | return true; |
8816 | } |
8817 | |
8818 | bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) { |
8819 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8820 | return Error(UO); |
8821 | |
8822 | if (!this->Visit(UO->getSubExpr())) |
8823 | return false; |
8824 | |
8825 | return handleIncDec( |
8826 | this->Info, UO, Result, UO->getSubExpr()->getType(), |
8827 | UO->isIncrementOp(), nullptr); |
8828 | } |
8829 | |
8830 | bool LValueExprEvaluator::VisitCompoundAssignOperator( |
8831 | const CompoundAssignOperator *CAO) { |
8832 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8833 | return Error(CAO); |
8834 | |
8835 | bool Success = true; |
8836 | |
8837 | // C++17 onwards require that we evaluate the RHS first. |
8838 | APValue RHS; |
8839 | if (!Evaluate(RHS, this->Info, CAO->getRHS())) { |
8840 | if (!Info.noteFailure()) |
8841 | return false; |
8842 | Success = false; |
8843 | } |
8844 | |
8845 | // The overall lvalue result is the result of evaluating the LHS. |
8846 | if (!this->Visit(CAO->getLHS()) || !Success) |
8847 | return false; |
8848 | |
8849 | return handleCompoundAssignment( |
8850 | this->Info, CAO, |
8851 | Result, CAO->getLHS()->getType(), CAO->getComputationLHSType(), |
8852 | CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RHS); |
8853 | } |
8854 | |
8855 | bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) { |
8856 | if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure()) |
8857 | return Error(E); |
8858 | |
8859 | bool Success = true; |
8860 | |
8861 | // C++17 onwards require that we evaluate the RHS first. |
8862 | APValue NewVal; |
8863 | if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) { |
8864 | if (!Info.noteFailure()) |
8865 | return false; |
8866 | Success = false; |
8867 | } |
8868 | |
8869 | if (!this->Visit(E->getLHS()) || !Success) |
8870 | return false; |
8871 | |
8872 | if (Info.getLangOpts().CPlusPlus20 && |
8873 | !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result)) |
8874 | return false; |
8875 | |
8876 | return handleAssignment(this->Info, E, Result, E->getLHS()->getType(), |
8877 | NewVal); |
8878 | } |
8879 | |
8880 | //===----------------------------------------------------------------------===// |
8881 | // Pointer Evaluation |
8882 | //===----------------------------------------------------------------------===// |
8883 | |
8884 | /// Attempts to compute the number of bytes available at the pointer |
8885 | /// returned by a function with the alloc_size attribute. Returns true if we |
8886 | /// were successful. Places an unsigned number into `Result`. |
8887 | /// |
8888 | /// This expects the given CallExpr to be a call to a function with an |
8889 | /// alloc_size attribute. |
8890 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8891 | const CallExpr *Call, |
8892 | llvm::APInt &Result) { |
8893 | const AllocSizeAttr *AllocSize = getAllocSizeAttr(Call); |
8894 | |
8895 | assert(AllocSize && AllocSize->getElemSizeParam().isValid()); |
8896 | unsigned SizeArgNo = AllocSize->getElemSizeParam().getASTIndex(); |
8897 | unsigned BitsInSizeT = Ctx.getTypeSize(T: Ctx.getSizeType()); |
8898 | if (Call->getNumArgs() <= SizeArgNo) |
8899 | return false; |
8900 | |
8901 | auto EvaluateAsSizeT = [&](const Expr *E, APSInt &Into) { |
8902 | Expr::EvalResult ExprResult; |
8903 | if (!E->EvaluateAsInt(Result&: ExprResult, Ctx, AllowSideEffects: Expr::SE_AllowSideEffects)) |
8904 | return false; |
8905 | Into = ExprResult.Val.getInt(); |
8906 | if (Into.isNegative() || !Into.isIntN(N: BitsInSizeT)) |
8907 | return false; |
8908 | Into = Into.zext(width: BitsInSizeT); |
8909 | return true; |
8910 | }; |
8911 | |
8912 | APSInt SizeOfElem; |
8913 | if (!EvaluateAsSizeT(Call->getArg(Arg: SizeArgNo), SizeOfElem)) |
8914 | return false; |
8915 | |
8916 | if (!AllocSize->getNumElemsParam().isValid()) { |
8917 | Result = std::move(SizeOfElem); |
8918 | return true; |
8919 | } |
8920 | |
8921 | APSInt NumberOfElems; |
8922 | unsigned NumArgNo = AllocSize->getNumElemsParam().getASTIndex(); |
8923 | if (!EvaluateAsSizeT(Call->getArg(Arg: NumArgNo), NumberOfElems)) |
8924 | return false; |
8925 | |
8926 | bool Overflow; |
8927 | llvm::APInt BytesAvailable = SizeOfElem.umul_ov(RHS: NumberOfElems, Overflow); |
8928 | if (Overflow) |
8929 | return false; |
8930 | |
8931 | Result = std::move(BytesAvailable); |
8932 | return true; |
8933 | } |
8934 | |
8935 | /// Convenience function. LVal's base must be a call to an alloc_size |
8936 | /// function. |
8937 | static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx, |
8938 | const LValue &LVal, |
8939 | llvm::APInt &Result) { |
8940 | assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) && |
8941 | "Can't get the size of a non alloc_size function" ); |
8942 | const auto *Base = LVal.getLValueBase().get<const Expr *>(); |
8943 | const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base); |
8944 | return getBytesReturnedByAllocSizeCall(Ctx, Call: CE, Result); |
8945 | } |
8946 | |
8947 | /// Attempts to evaluate the given LValueBase as the result of a call to |
8948 | /// a function with the alloc_size attribute. If it was possible to do so, this |
8949 | /// function will return true, make Result's Base point to said function call, |
8950 | /// and mark Result's Base as invalid. |
8951 | static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base, |
8952 | LValue &Result) { |
8953 | if (Base.isNull()) |
8954 | return false; |
8955 | |
8956 | // Because we do no form of static analysis, we only support const variables. |
8957 | // |
8958 | // Additionally, we can't support parameters, nor can we support static |
8959 | // variables (in the latter case, use-before-assign isn't UB; in the former, |
8960 | // we have no clue what they'll be assigned to). |
8961 | const auto *VD = |
8962 | dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>()); |
8963 | if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified()) |
8964 | return false; |
8965 | |
8966 | const Expr *Init = VD->getAnyInitializer(); |
8967 | if (!Init || Init->getType().isNull()) |
8968 | return false; |
8969 | |
8970 | const Expr *E = Init->IgnoreParens(); |
8971 | if (!tryUnwrapAllocSizeCall(E)) |
8972 | return false; |
8973 | |
8974 | // Store E instead of E unwrapped so that the type of the LValue's base is |
8975 | // what the user wanted. |
8976 | Result.setInvalid(B: E); |
8977 | |
8978 | QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType(); |
8979 | Result.addUnsizedArray(Info, E, ElemTy: Pointee); |
8980 | return true; |
8981 | } |
8982 | |
8983 | namespace { |
8984 | class PointerExprEvaluator |
8985 | : public ExprEvaluatorBase<PointerExprEvaluator> { |
8986 | LValue &Result; |
8987 | bool InvalidBaseOK; |
8988 | |
8989 | bool Success(const Expr *E) { |
8990 | Result.set(B: E); |
8991 | return true; |
8992 | } |
8993 | |
8994 | bool evaluateLValue(const Expr *E, LValue &Result) { |
8995 | return EvaluateLValue(E, Result, Info, InvalidBaseOK); |
8996 | } |
8997 | |
8998 | bool evaluatePointer(const Expr *E, LValue &Result) { |
8999 | return EvaluatePointer(E, Result, Info, InvalidBaseOK); |
9000 | } |
9001 | |
9002 | bool visitNonBuiltinCallExpr(const CallExpr *E); |
9003 | public: |
9004 | |
9005 | PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK) |
9006 | : ExprEvaluatorBaseTy(info), Result(Result), |
9007 | InvalidBaseOK(InvalidBaseOK) {} |
9008 | |
9009 | bool Success(const APValue &V, const Expr *E) { |
9010 | Result.setFrom(Ctx&: Info.Ctx, V); |
9011 | return true; |
9012 | } |
9013 | bool ZeroInitialization(const Expr *E) { |
9014 | Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType()); |
9015 | return true; |
9016 | } |
9017 | |
9018 | bool VisitBinaryOperator(const BinaryOperator *E); |
9019 | bool VisitCastExpr(const CastExpr* E); |
9020 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
9021 | bool VisitObjCStringLiteral(const ObjCStringLiteral *E) |
9022 | { return Success(E); } |
9023 | bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) { |
9024 | if (E->isExpressibleAsConstantInitializer()) |
9025 | return Success(E); |
9026 | if (Info.noteFailure()) |
9027 | EvaluateIgnoredValue(Info, E: E->getSubExpr()); |
9028 | return Error(E); |
9029 | } |
9030 | bool VisitAddrLabelExpr(const AddrLabelExpr *E) |
9031 | { return Success(E); } |
9032 | bool VisitCallExpr(const CallExpr *E); |
9033 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
9034 | bool VisitBlockExpr(const BlockExpr *E) { |
9035 | if (!E->getBlockDecl()->hasCaptures()) |
9036 | return Success(E); |
9037 | return Error(E); |
9038 | } |
9039 | bool VisitCXXThisExpr(const CXXThisExpr *E) { |
9040 | // Can't look at 'this' when checking a potential constant expression. |
9041 | if (Info.checkingPotentialConstantExpression()) |
9042 | return false; |
9043 | if (!Info.CurrentCall->This) { |
9044 | if (Info.getLangOpts().CPlusPlus11) |
9045 | Info.FFDiag(E, diag::note_constexpr_this) << E->isImplicit(); |
9046 | else |
9047 | Info.FFDiag(E); |
9048 | return false; |
9049 | } |
9050 | Result = *Info.CurrentCall->This; |
9051 | |
9052 | if (isLambdaCallOperator(Info.CurrentCall->Callee)) { |
9053 | // Ensure we actually have captured 'this'. If something was wrong with |
9054 | // 'this' capture, the error would have been previously reported. |
9055 | // Otherwise we can be inside of a default initialization of an object |
9056 | // declared by lambda's body, so no need to return false. |
9057 | if (!Info.CurrentCall->LambdaThisCaptureField) |
9058 | return true; |
9059 | |
9060 | // If we have captured 'this', the 'this' expression refers |
9061 | // to the enclosing '*this' object (either by value or reference) which is |
9062 | // either copied into the closure object's field that represents the |
9063 | // '*this' or refers to '*this'. |
9064 | // Update 'Result' to refer to the data member/field of the closure object |
9065 | // that represents the '*this' capture. |
9066 | if (!HandleLValueMember(Info, E, Result, |
9067 | Info.CurrentCall->LambdaThisCaptureField)) |
9068 | return false; |
9069 | // If we captured '*this' by reference, replace the field with its referent. |
9070 | if (Info.CurrentCall->LambdaThisCaptureField->getType() |
9071 | ->isPointerType()) { |
9072 | APValue RVal; |
9073 | if (!handleLValueToRValueConversion(Info, E, E->getType(), Result, |
9074 | RVal)) |
9075 | return false; |
9076 | |
9077 | Result.setFrom(Ctx&: Info.Ctx, V: RVal); |
9078 | } |
9079 | } |
9080 | return true; |
9081 | } |
9082 | |
9083 | bool VisitCXXNewExpr(const CXXNewExpr *E); |
9084 | |
9085 | bool VisitSourceLocExpr(const SourceLocExpr *E) { |
9086 | assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?" ); |
9087 | APValue LValResult = E->EvaluateInContext( |
9088 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
9089 | Result.setFrom(Ctx&: Info.Ctx, V: LValResult); |
9090 | return true; |
9091 | } |
9092 | |
9093 | bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) { |
9094 | std::string ResultStr = E->ComputeName(Context&: Info.Ctx); |
9095 | |
9096 | QualType CharTy = Info.Ctx.CharTy.withConst(); |
9097 | APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()), |
9098 | ResultStr.size() + 1); |
9099 | QualType ArrayTy = Info.Ctx.getConstantArrayType( |
9100 | EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9101 | |
9102 | StringLiteral *SL = |
9103 | StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary, |
9104 | /*Pascal*/ false, Ty: ArrayTy, Loc: E->getLocation()); |
9105 | |
9106 | evaluateLValue(SL, Result); |
9107 | Result.addArray(Info, E, cast<ConstantArrayType>(Val&: ArrayTy)); |
9108 | return true; |
9109 | } |
9110 | |
9111 | // FIXME: Missing: @protocol, @selector |
9112 | }; |
9113 | } // end anonymous namespace |
9114 | |
9115 | static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info, |
9116 | bool InvalidBaseOK) { |
9117 | assert(!E->isValueDependent()); |
9118 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
9119 | return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(E); |
9120 | } |
9121 | |
9122 | bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
9123 | if (E->getOpcode() != BO_Add && |
9124 | E->getOpcode() != BO_Sub) |
9125 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
9126 | |
9127 | const Expr *PExp = E->getLHS(); |
9128 | const Expr *IExp = E->getRHS(); |
9129 | if (IExp->getType()->isPointerType()) |
9130 | std::swap(a&: PExp, b&: IExp); |
9131 | |
9132 | bool EvalPtrOK = evaluatePointer(E: PExp, Result); |
9133 | if (!EvalPtrOK && !Info.noteFailure()) |
9134 | return false; |
9135 | |
9136 | llvm::APSInt Offset; |
9137 | if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK) |
9138 | return false; |
9139 | |
9140 | if (E->getOpcode() == BO_Sub) |
9141 | negateAsSigned(Int&: Offset); |
9142 | |
9143 | QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType(); |
9144 | return HandleLValueArrayAdjustment(Info, E, Result, Pointee, Offset); |
9145 | } |
9146 | |
9147 | bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
9148 | return evaluateLValue(E: E->getSubExpr(), Result); |
9149 | } |
9150 | |
9151 | // Is the provided decl 'std::source_location::current'? |
9152 | static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) { |
9153 | if (!FD) |
9154 | return false; |
9155 | const IdentifierInfo *FnII = FD->getIdentifier(); |
9156 | if (!FnII || !FnII->isStr(Str: "current" )) |
9157 | return false; |
9158 | |
9159 | const auto *RD = dyn_cast<RecordDecl>(FD->getParent()); |
9160 | if (!RD) |
9161 | return false; |
9162 | |
9163 | const IdentifierInfo *ClassII = RD->getIdentifier(); |
9164 | return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location" ); |
9165 | } |
9166 | |
9167 | bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
9168 | const Expr *SubExpr = E->getSubExpr(); |
9169 | |
9170 | switch (E->getCastKind()) { |
9171 | default: |
9172 | break; |
9173 | case CK_BitCast: |
9174 | case CK_CPointerToObjCPointerCast: |
9175 | case CK_BlockPointerToObjCPointerCast: |
9176 | case CK_AnyPointerToBlockPointerCast: |
9177 | case CK_AddressSpaceConversion: |
9178 | if (!Visit(SubExpr)) |
9179 | return false; |
9180 | // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are |
9181 | // permitted in constant expressions in C++11. Bitcasts from cv void* are |
9182 | // also static_casts, but we disallow them as a resolution to DR1312. |
9183 | if (!E->getType()->isVoidPointerType()) { |
9184 | // In some circumstances, we permit casting from void* to cv1 T*, when the |
9185 | // actual pointee object is actually a cv2 T. |
9186 | bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid && |
9187 | !Result.IsNullPtr; |
9188 | bool VoidPtrCastMaybeOK = |
9189 | HasValidResult && |
9190 | Info.Ctx.hasSameUnqualifiedType(T1: Result.Designator.getType(Info.Ctx), |
9191 | T2: E->getType()->getPointeeType()); |
9192 | // 1. We'll allow it in std::allocator::allocate, and anything which that |
9193 | // calls. |
9194 | // 2. HACK 2022-03-28: Work around an issue with libstdc++'s |
9195 | // <source_location> header. Fixed in GCC 12 and later (2022-04-??). |
9196 | // We'll allow it in the body of std::source_location::current. GCC's |
9197 | // implementation had a parameter of type `void*`, and casts from |
9198 | // that back to `const __impl*` in its body. |
9199 | if (VoidPtrCastMaybeOK && |
9200 | (Info.getStdAllocatorCaller(FnName: "allocate" ) || |
9201 | IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) || |
9202 | Info.getLangOpts().CPlusPlus26)) { |
9203 | // Permitted. |
9204 | } else { |
9205 | if (SubExpr->getType()->isVoidPointerType()) { |
9206 | if (HasValidResult) |
9207 | CCEDiag(E, diag::note_constexpr_invalid_void_star_cast) |
9208 | << SubExpr->getType() << Info.getLangOpts().CPlusPlus26 |
9209 | << Result.Designator.getType(Info.Ctx).getCanonicalType() |
9210 | << E->getType()->getPointeeType(); |
9211 | else |
9212 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9213 | << 3 << SubExpr->getType(); |
9214 | } else |
9215 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9216 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
9217 | Result.Designator.setInvalid(); |
9218 | } |
9219 | } |
9220 | if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr) |
9221 | ZeroInitialization(E); |
9222 | return true; |
9223 | |
9224 | case CK_DerivedToBase: |
9225 | case CK_UncheckedDerivedToBase: |
9226 | if (!evaluatePointer(E: E->getSubExpr(), Result)) |
9227 | return false; |
9228 | if (!Result.Base && Result.Offset.isZero()) |
9229 | return true; |
9230 | |
9231 | // Now figure out the necessary offset to add to the base LV to get from |
9232 | // the derived class to the base class. |
9233 | return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()-> |
9234 | castAs<PointerType>()->getPointeeType(), |
9235 | Result); |
9236 | |
9237 | case CK_BaseToDerived: |
9238 | if (!Visit(E->getSubExpr())) |
9239 | return false; |
9240 | if (!Result.Base && Result.Offset.isZero()) |
9241 | return true; |
9242 | return HandleBaseToDerivedCast(Info, E, Result); |
9243 | |
9244 | case CK_Dynamic: |
9245 | if (!Visit(E->getSubExpr())) |
9246 | return false; |
9247 | return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result); |
9248 | |
9249 | case CK_NullToPointer: |
9250 | VisitIgnoredValue(E: E->getSubExpr()); |
9251 | return ZeroInitialization(E); |
9252 | |
9253 | case CK_IntegralToPointer: { |
9254 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
9255 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
9256 | |
9257 | APValue Value; |
9258 | if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info)) |
9259 | break; |
9260 | |
9261 | if (Value.isInt()) { |
9262 | unsigned Size = Info.Ctx.getTypeSize(E->getType()); |
9263 | uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue(); |
9264 | Result.Base = (Expr*)nullptr; |
9265 | Result.InvalidBase = false; |
9266 | Result.Offset = CharUnits::fromQuantity(Quantity: N); |
9267 | Result.Designator.setInvalid(); |
9268 | Result.IsNullPtr = false; |
9269 | return true; |
9270 | } else { |
9271 | // Cast is of an lvalue, no need to change value. |
9272 | Result.setFrom(Ctx&: Info.Ctx, V: Value); |
9273 | return true; |
9274 | } |
9275 | } |
9276 | |
9277 | case CK_ArrayToPointerDecay: { |
9278 | if (SubExpr->isGLValue()) { |
9279 | if (!evaluateLValue(E: SubExpr, Result)) |
9280 | return false; |
9281 | } else { |
9282 | APValue &Value = Info.CurrentCall->createTemporary( |
9283 | Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
9284 | if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr)) |
9285 | return false; |
9286 | } |
9287 | // The result is a pointer to the first element of the array. |
9288 | auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType()); |
9289 | if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) |
9290 | Result.addArray(Info, E, CAT); |
9291 | else |
9292 | Result.addUnsizedArray(Info, E, AT->getElementType()); |
9293 | return true; |
9294 | } |
9295 | |
9296 | case CK_FunctionToPointerDecay: |
9297 | return evaluateLValue(E: SubExpr, Result); |
9298 | |
9299 | case CK_LValueToRValue: { |
9300 | LValue LVal; |
9301 | if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal)) |
9302 | return false; |
9303 | |
9304 | APValue RVal; |
9305 | // Note, we use the subexpression's type in order to retain cv-qualifiers. |
9306 | if (!handleLValueToRValueConversion(Info, E, E->getSubExpr()->getType(), |
9307 | LVal, RVal)) |
9308 | return InvalidBaseOK && |
9309 | evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result); |
9310 | return Success(RVal, E); |
9311 | } |
9312 | } |
9313 | |
9314 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
9315 | } |
9316 | |
9317 | static CharUnits GetAlignOfType(EvalInfo &Info, QualType T, |
9318 | UnaryExprOrTypeTrait ExprKind) { |
9319 | // C++ [expr.alignof]p3: |
9320 | // When alignof is applied to a reference type, the result is the |
9321 | // alignment of the referenced type. |
9322 | T = T.getNonReferenceType(); |
9323 | |
9324 | if (T.getQualifiers().hasUnaligned()) |
9325 | return CharUnits::One(); |
9326 | |
9327 | const bool AlignOfReturnsPreferred = |
9328 | Info.Ctx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7; |
9329 | |
9330 | // __alignof is defined to return the preferred alignment. |
9331 | // Before 8, clang returned the preferred alignment for alignof and _Alignof |
9332 | // as well. |
9333 | if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred) |
9334 | return Info.Ctx.toCharUnitsFromBits( |
9335 | BitSize: Info.Ctx.getPreferredTypeAlign(T: T.getTypePtr())); |
9336 | // alignof and _Alignof are defined to return the ABI alignment. |
9337 | else if (ExprKind == UETT_AlignOf) |
9338 | return Info.Ctx.getTypeAlignInChars(T: T.getTypePtr()); |
9339 | else |
9340 | llvm_unreachable("GetAlignOfType on a non-alignment ExprKind" ); |
9341 | } |
9342 | |
9343 | static CharUnits GetAlignOfExpr(EvalInfo &Info, const Expr *E, |
9344 | UnaryExprOrTypeTrait ExprKind) { |
9345 | E = E->IgnoreParens(); |
9346 | |
9347 | // The kinds of expressions that we have special-case logic here for |
9348 | // should be kept up to date with the special checks for those |
9349 | // expressions in Sema. |
9350 | |
9351 | // alignof decl is always accepted, even if it doesn't make sense: we default |
9352 | // to 1 in those cases. |
9353 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E)) |
9354 | return Info.Ctx.getDeclAlign(DRE->getDecl(), |
9355 | /*RefAsPointee*/true); |
9356 | |
9357 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E)) |
9358 | return Info.Ctx.getDeclAlign(ME->getMemberDecl(), |
9359 | /*RefAsPointee*/true); |
9360 | |
9361 | return GetAlignOfType(Info, T: E->getType(), ExprKind); |
9362 | } |
9363 | |
9364 | static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) { |
9365 | if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>()) |
9366 | return Info.Ctx.getDeclAlign(VD); |
9367 | if (const auto *E = Value.Base.dyn_cast<const Expr *>()) |
9368 | return GetAlignOfExpr(Info, E, ExprKind: UETT_AlignOf); |
9369 | return GetAlignOfType(Info, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf); |
9370 | } |
9371 | |
9372 | /// Evaluate the value of the alignment argument to __builtin_align_{up,down}, |
9373 | /// __builtin_is_aligned and __builtin_assume_aligned. |
9374 | static bool getAlignmentArgument(const Expr *E, QualType ForType, |
9375 | EvalInfo &Info, APSInt &Alignment) { |
9376 | if (!EvaluateInteger(E, Result&: Alignment, Info)) |
9377 | return false; |
9378 | if (Alignment < 0 || !Alignment.isPowerOf2()) { |
9379 | Info.FFDiag(E, diag::note_constexpr_invalid_alignment) << Alignment; |
9380 | return false; |
9381 | } |
9382 | unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType); |
9383 | APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1)); |
9384 | if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) { |
9385 | Info.FFDiag(E, diag::note_constexpr_alignment_too_big) |
9386 | << MaxValue << ForType << Alignment; |
9387 | return false; |
9388 | } |
9389 | // Ensure both alignment and source value have the same bit width so that we |
9390 | // don't assert when computing the resulting value. |
9391 | APSInt ExtAlignment = |
9392 | APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true); |
9393 | assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 && |
9394 | "Alignment should not be changed by ext/trunc" ); |
9395 | Alignment = ExtAlignment; |
9396 | assert(Alignment.getBitWidth() == SrcWidth); |
9397 | return true; |
9398 | } |
9399 | |
9400 | // To be clear: this happily visits unsupported builtins. Better name welcomed. |
9401 | bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) { |
9402 | if (ExprEvaluatorBaseTy::VisitCallExpr(E)) |
9403 | return true; |
9404 | |
9405 | if (!(InvalidBaseOK && getAllocSizeAttr(E))) |
9406 | return false; |
9407 | |
9408 | Result.setInvalid(E); |
9409 | QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType(); |
9410 | Result.addUnsizedArray(Info, E, PointeeTy); |
9411 | return true; |
9412 | } |
9413 | |
9414 | bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) { |
9415 | if (!IsConstantEvaluatedBuiltinCall(E)) |
9416 | return visitNonBuiltinCallExpr(E); |
9417 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
9418 | } |
9419 | |
9420 | // Determine if T is a character type for which we guarantee that |
9421 | // sizeof(T) == 1. |
9422 | static bool isOneByteCharacterType(QualType T) { |
9423 | return T->isCharType() || T->isChar8Type(); |
9424 | } |
9425 | |
9426 | bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
9427 | unsigned BuiltinOp) { |
9428 | if (IsNoOpCall(E)) |
9429 | return Success(E); |
9430 | |
9431 | switch (BuiltinOp) { |
9432 | case Builtin::BIaddressof: |
9433 | case Builtin::BI__addressof: |
9434 | case Builtin::BI__builtin_addressof: |
9435 | return evaluateLValue(E: E->getArg(Arg: 0), Result); |
9436 | case Builtin::BI__builtin_assume_aligned: { |
9437 | // We need to be very careful here because: if the pointer does not have the |
9438 | // asserted alignment, then the behavior is undefined, and undefined |
9439 | // behavior is non-constant. |
9440 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
9441 | return false; |
9442 | |
9443 | LValue OffsetResult(Result); |
9444 | APSInt Alignment; |
9445 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
9446 | Alignment)) |
9447 | return false; |
9448 | CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue()); |
9449 | |
9450 | if (E->getNumArgs() > 2) { |
9451 | APSInt Offset; |
9452 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info)) |
9453 | return false; |
9454 | |
9455 | int64_t AdditionalOffset = -Offset.getZExtValue(); |
9456 | OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset); |
9457 | } |
9458 | |
9459 | // If there is a base object, then it must have the correct alignment. |
9460 | if (OffsetResult.Base) { |
9461 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult); |
9462 | |
9463 | if (BaseAlignment < Align) { |
9464 | Result.Designator.setInvalid(); |
9465 | // FIXME: Add support to Diagnostic for long / long long. |
9466 | CCEDiag(E->getArg(0), |
9467 | diag::note_constexpr_baa_insufficient_alignment) << 0 |
9468 | << (unsigned)BaseAlignment.getQuantity() |
9469 | << (unsigned)Align.getQuantity(); |
9470 | return false; |
9471 | } |
9472 | } |
9473 | |
9474 | // The offset must also have the correct alignment. |
9475 | if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) { |
9476 | Result.Designator.setInvalid(); |
9477 | |
9478 | (OffsetResult.Base |
9479 | ? CCEDiag(E->getArg(0), |
9480 | diag::note_constexpr_baa_insufficient_alignment) << 1 |
9481 | : CCEDiag(E->getArg(0), |
9482 | diag::note_constexpr_baa_value_insufficient_alignment)) |
9483 | << (int)OffsetResult.Offset.getQuantity() |
9484 | << (unsigned)Align.getQuantity(); |
9485 | return false; |
9486 | } |
9487 | |
9488 | return true; |
9489 | } |
9490 | case Builtin::BI__builtin_align_up: |
9491 | case Builtin::BI__builtin_align_down: { |
9492 | if (!evaluatePointer(E: E->getArg(Arg: 0), Result)) |
9493 | return false; |
9494 | APSInt Alignment; |
9495 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info, |
9496 | Alignment)) |
9497 | return false; |
9498 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result); |
9499 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset); |
9500 | // For align_up/align_down, we can return the same value if the alignment |
9501 | // is known to be greater or equal to the requested value. |
9502 | if (PtrAlign.getQuantity() >= Alignment) |
9503 | return true; |
9504 | |
9505 | // The alignment could be greater than the minimum at run-time, so we cannot |
9506 | // infer much about the resulting pointer value. One case is possible: |
9507 | // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we |
9508 | // can infer the correct index if the requested alignment is smaller than |
9509 | // the base alignment so we can perform the computation on the offset. |
9510 | if (BaseAlignment.getQuantity() >= Alignment) { |
9511 | assert(Alignment.getBitWidth() <= 64 && |
9512 | "Cannot handle > 64-bit address-space" ); |
9513 | uint64_t Alignment64 = Alignment.getZExtValue(); |
9514 | CharUnits NewOffset = CharUnits::fromQuantity( |
9515 | BuiltinOp == Builtin::BI__builtin_align_down |
9516 | ? llvm::alignDown(Result.Offset.getQuantity(), Alignment64) |
9517 | : llvm::alignTo(Result.Offset.getQuantity(), Alignment64)); |
9518 | Result.adjustOffset(N: NewOffset - Result.Offset); |
9519 | // TODO: diagnose out-of-bounds values/only allow for arrays? |
9520 | return true; |
9521 | } |
9522 | // Otherwise, we cannot constant-evaluate the result. |
9523 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_adjust) |
9524 | << Alignment; |
9525 | return false; |
9526 | } |
9527 | case Builtin::BI__builtin_operator_new: |
9528 | return HandleOperatorNewCall(Info, E, Result); |
9529 | case Builtin::BI__builtin_launder: |
9530 | return evaluatePointer(E: E->getArg(Arg: 0), Result); |
9531 | case Builtin::BIstrchr: |
9532 | case Builtin::BIwcschr: |
9533 | case Builtin::BImemchr: |
9534 | case Builtin::BIwmemchr: |
9535 | if (Info.getLangOpts().CPlusPlus11) |
9536 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9537 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
9538 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
9539 | else |
9540 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9541 | [[fallthrough]]; |
9542 | case Builtin::BI__builtin_strchr: |
9543 | case Builtin::BI__builtin_wcschr: |
9544 | case Builtin::BI__builtin_memchr: |
9545 | case Builtin::BI__builtin_char_memchr: |
9546 | case Builtin::BI__builtin_wmemchr: { |
9547 | if (!Visit(E->getArg(Arg: 0))) |
9548 | return false; |
9549 | APSInt Desired; |
9550 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info)) |
9551 | return false; |
9552 | uint64_t MaxLength = uint64_t(-1); |
9553 | if (BuiltinOp != Builtin::BIstrchr && |
9554 | BuiltinOp != Builtin::BIwcschr && |
9555 | BuiltinOp != Builtin::BI__builtin_strchr && |
9556 | BuiltinOp != Builtin::BI__builtin_wcschr) { |
9557 | APSInt N; |
9558 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
9559 | return false; |
9560 | MaxLength = N.getZExtValue(); |
9561 | } |
9562 | // We cannot find the value if there are no candidates to match against. |
9563 | if (MaxLength == 0u) |
9564 | return ZeroInitialization(E); |
9565 | if (!Result.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
9566 | Result.Designator.Invalid) |
9567 | return false; |
9568 | QualType CharTy = Result.Designator.getType(Info.Ctx); |
9569 | bool IsRawByte = BuiltinOp == Builtin::BImemchr || |
9570 | BuiltinOp == Builtin::BI__builtin_memchr; |
9571 | assert(IsRawByte || |
9572 | Info.Ctx.hasSameUnqualifiedType( |
9573 | CharTy, E->getArg(0)->getType()->getPointeeType())); |
9574 | // Pointers to const void may point to objects of incomplete type. |
9575 | if (IsRawByte && CharTy->isIncompleteType()) { |
9576 | Info.FFDiag(E, diag::note_constexpr_ltor_incomplete_type) << CharTy; |
9577 | return false; |
9578 | } |
9579 | // Give up on byte-oriented matching against multibyte elements. |
9580 | // FIXME: We can compare the bytes in the correct order. |
9581 | if (IsRawByte && !isOneByteCharacterType(T: CharTy)) { |
9582 | Info.FFDiag(E, diag::note_constexpr_memchr_unsupported) |
9583 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str() |
9584 | << CharTy; |
9585 | return false; |
9586 | } |
9587 | // Figure out what value we're actually looking for (after converting to |
9588 | // the corresponding unsigned type if necessary). |
9589 | uint64_t DesiredVal; |
9590 | bool StopAtNull = false; |
9591 | switch (BuiltinOp) { |
9592 | case Builtin::BIstrchr: |
9593 | case Builtin::BI__builtin_strchr: |
9594 | // strchr compares directly to the passed integer, and therefore |
9595 | // always fails if given an int that is not a char. |
9596 | if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, CharTy, |
9597 | E->getArg(Arg: 1)->getType(), |
9598 | Desired), |
9599 | I2: Desired)) |
9600 | return ZeroInitialization(E); |
9601 | StopAtNull = true; |
9602 | [[fallthrough]]; |
9603 | case Builtin::BImemchr: |
9604 | case Builtin::BI__builtin_memchr: |
9605 | case Builtin::BI__builtin_char_memchr: |
9606 | // memchr compares by converting both sides to unsigned char. That's also |
9607 | // correct for strchr if we get this far (to cope with plain char being |
9608 | // unsigned in the strchr case). |
9609 | DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue(); |
9610 | break; |
9611 | |
9612 | case Builtin::BIwcschr: |
9613 | case Builtin::BI__builtin_wcschr: |
9614 | StopAtNull = true; |
9615 | [[fallthrough]]; |
9616 | case Builtin::BIwmemchr: |
9617 | case Builtin::BI__builtin_wmemchr: |
9618 | // wcschr and wmemchr are given a wchar_t to look for. Just use it. |
9619 | DesiredVal = Desired.getZExtValue(); |
9620 | break; |
9621 | } |
9622 | |
9623 | for (; MaxLength; --MaxLength) { |
9624 | APValue Char; |
9625 | if (!handleLValueToRValueConversion(Info, E, CharTy, Result, Char) || |
9626 | !Char.isInt()) |
9627 | return false; |
9628 | if (Char.getInt().getZExtValue() == DesiredVal) |
9629 | return true; |
9630 | if (StopAtNull && !Char.getInt()) |
9631 | break; |
9632 | if (!HandleLValueArrayAdjustment(Info, E, Result, CharTy, 1)) |
9633 | return false; |
9634 | } |
9635 | // Not found: return nullptr. |
9636 | return ZeroInitialization(E); |
9637 | } |
9638 | |
9639 | case Builtin::BImemcpy: |
9640 | case Builtin::BImemmove: |
9641 | case Builtin::BIwmemcpy: |
9642 | case Builtin::BIwmemmove: |
9643 | if (Info.getLangOpts().CPlusPlus11) |
9644 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
9645 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
9646 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
9647 | else |
9648 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
9649 | [[fallthrough]]; |
9650 | case Builtin::BI__builtin_memcpy: |
9651 | case Builtin::BI__builtin_memmove: |
9652 | case Builtin::BI__builtin_wmemcpy: |
9653 | case Builtin::BI__builtin_wmemmove: { |
9654 | bool WChar = BuiltinOp == Builtin::BIwmemcpy || |
9655 | BuiltinOp == Builtin::BIwmemmove || |
9656 | BuiltinOp == Builtin::BI__builtin_wmemcpy || |
9657 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9658 | bool Move = BuiltinOp == Builtin::BImemmove || |
9659 | BuiltinOp == Builtin::BIwmemmove || |
9660 | BuiltinOp == Builtin::BI__builtin_memmove || |
9661 | BuiltinOp == Builtin::BI__builtin_wmemmove; |
9662 | |
9663 | // The result of mem* is the first argument. |
9664 | if (!Visit(E->getArg(Arg: 0))) |
9665 | return false; |
9666 | LValue Dest = Result; |
9667 | |
9668 | LValue Src; |
9669 | if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info)) |
9670 | return false; |
9671 | |
9672 | APSInt N; |
9673 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
9674 | return false; |
9675 | assert(!N.isSigned() && "memcpy and friends take an unsigned size" ); |
9676 | |
9677 | // If the size is zero, we treat this as always being a valid no-op. |
9678 | // (Even if one of the src and dest pointers is null.) |
9679 | if (!N) |
9680 | return true; |
9681 | |
9682 | // Otherwise, if either of the operands is null, we can't proceed. Don't |
9683 | // try to determine the type of the copied objects, because there aren't |
9684 | // any. |
9685 | if (!Src.Base || !Dest.Base) { |
9686 | APValue Val; |
9687 | (!Src.Base ? Src : Dest).moveInto(V&: Val); |
9688 | Info.FFDiag(E, diag::note_constexpr_memcpy_null) |
9689 | << Move << WChar << !!Src.Base |
9690 | << Val.getAsString(Info.Ctx, E->getArg(0)->getType()); |
9691 | return false; |
9692 | } |
9693 | if (Src.Designator.Invalid || Dest.Designator.Invalid) |
9694 | return false; |
9695 | |
9696 | // We require that Src and Dest are both pointers to arrays of |
9697 | // trivially-copyable type. (For the wide version, the designator will be |
9698 | // invalid if the designated object is not a wchar_t.) |
9699 | QualType T = Dest.Designator.getType(Info.Ctx); |
9700 | QualType SrcT = Src.Designator.getType(Info.Ctx); |
9701 | if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) { |
9702 | // FIXME: Consider using our bit_cast implementation to support this. |
9703 | Info.FFDiag(E, diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T; |
9704 | return false; |
9705 | } |
9706 | if (T->isIncompleteType()) { |
9707 | Info.FFDiag(E, diag::note_constexpr_memcpy_incomplete_type) << Move << T; |
9708 | return false; |
9709 | } |
9710 | if (!T.isTriviallyCopyableType(Context: Info.Ctx)) { |
9711 | Info.FFDiag(E, diag::note_constexpr_memcpy_nontrivial) << Move << T; |
9712 | return false; |
9713 | } |
9714 | |
9715 | // Figure out how many T's we're copying. |
9716 | uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity(); |
9717 | if (TSize == 0) |
9718 | return false; |
9719 | if (!WChar) { |
9720 | uint64_t Remainder; |
9721 | llvm::APInt OrigN = N; |
9722 | llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder); |
9723 | if (Remainder) { |
9724 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9725 | << Move << WChar << 0 << T << toString(OrigN, 10, /*Signed*/false) |
9726 | << (unsigned)TSize; |
9727 | return false; |
9728 | } |
9729 | } |
9730 | |
9731 | // Check that the copying will remain within the arrays, just so that we |
9732 | // can give a more meaningful diagnostic. This implicitly also checks that |
9733 | // N fits into 64 bits. |
9734 | uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second; |
9735 | uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second; |
9736 | if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) { |
9737 | Info.FFDiag(E, diag::note_constexpr_memcpy_unsupported) |
9738 | << Move << WChar << (N.ugt(RemainingSrcSize) ? 1 : 2) << T |
9739 | << toString(N, 10, /*Signed*/false); |
9740 | return false; |
9741 | } |
9742 | uint64_t NElems = N.getZExtValue(); |
9743 | uint64_t NBytes = NElems * TSize; |
9744 | |
9745 | // Check for overlap. |
9746 | int Direction = 1; |
9747 | if (HasSameBase(A: Src, B: Dest)) { |
9748 | uint64_t SrcOffset = Src.getLValueOffset().getQuantity(); |
9749 | uint64_t DestOffset = Dest.getLValueOffset().getQuantity(); |
9750 | if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) { |
9751 | // Dest is inside the source region. |
9752 | if (!Move) { |
9753 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9754 | return false; |
9755 | } |
9756 | // For memmove and friends, copy backwards. |
9757 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, NElems - 1) || |
9758 | !HandleLValueArrayAdjustment(Info, E, Dest, T, NElems - 1)) |
9759 | return false; |
9760 | Direction = -1; |
9761 | } else if (!Move && SrcOffset >= DestOffset && |
9762 | SrcOffset - DestOffset < NBytes) { |
9763 | // Src is inside the destination region for memcpy: invalid. |
9764 | Info.FFDiag(E, diag::note_constexpr_memcpy_overlap) << WChar; |
9765 | return false; |
9766 | } |
9767 | } |
9768 | |
9769 | while (true) { |
9770 | APValue Val; |
9771 | // FIXME: Set WantObjectRepresentation to true if we're copying a |
9772 | // char-like type? |
9773 | if (!handleLValueToRValueConversion(Info, E, T, Src, Val) || |
9774 | !handleAssignment(Info, E, Dest, T, Val)) |
9775 | return false; |
9776 | // Do not iterate past the last element; if we're copying backwards, that |
9777 | // might take us off the start of the array. |
9778 | if (--NElems == 0) |
9779 | return true; |
9780 | if (!HandleLValueArrayAdjustment(Info, E, Src, T, Direction) || |
9781 | !HandleLValueArrayAdjustment(Info, E, Dest, T, Direction)) |
9782 | return false; |
9783 | } |
9784 | } |
9785 | |
9786 | default: |
9787 | return false; |
9788 | } |
9789 | } |
9790 | |
9791 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
9792 | APValue &Result, const InitListExpr *ILE, |
9793 | QualType AllocType); |
9794 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
9795 | APValue &Result, |
9796 | const CXXConstructExpr *CCE, |
9797 | QualType AllocType); |
9798 | |
9799 | bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) { |
9800 | if (!Info.getLangOpts().CPlusPlus20) |
9801 | Info.CCEDiag(E, diag::note_constexpr_new); |
9802 | |
9803 | // We cannot speculatively evaluate a delete expression. |
9804 | if (Info.SpeculativeEvaluationDepth) |
9805 | return false; |
9806 | |
9807 | FunctionDecl *OperatorNew = E->getOperatorNew(); |
9808 | |
9809 | bool IsNothrow = false; |
9810 | bool IsPlacement = false; |
9811 | if (OperatorNew->isReservedGlobalPlacementOperator() && |
9812 | Info.CurrentCall->isStdFunction() && !E->isArray()) { |
9813 | // FIXME Support array placement new. |
9814 | assert(E->getNumPlacementArgs() == 1); |
9815 | if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info)) |
9816 | return false; |
9817 | if (Result.Designator.Invalid) |
9818 | return false; |
9819 | IsPlacement = true; |
9820 | } else if (!OperatorNew->isReplaceableGlobalAllocationFunction()) { |
9821 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
9822 | << isa<CXXMethodDecl>(OperatorNew) << OperatorNew; |
9823 | return false; |
9824 | } else if (E->getNumPlacementArgs()) { |
9825 | // The only new-placement list we support is of the form (std::nothrow). |
9826 | // |
9827 | // FIXME: There is no restriction on this, but it's not clear that any |
9828 | // other form makes any sense. We get here for cases such as: |
9829 | // |
9830 | // new (std::align_val_t{N}) X(int) |
9831 | // |
9832 | // (which should presumably be valid only if N is a multiple of |
9833 | // alignof(int), and in any case can't be deallocated unless N is |
9834 | // alignof(X) and X has new-extended alignment). |
9835 | if (E->getNumPlacementArgs() != 1 || |
9836 | !E->getPlacementArg(0)->getType()->isNothrowT()) |
9837 | return Error(E, diag::note_constexpr_new_placement); |
9838 | |
9839 | LValue Nothrow; |
9840 | if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info)) |
9841 | return false; |
9842 | IsNothrow = true; |
9843 | } |
9844 | |
9845 | const Expr *Init = E->getInitializer(); |
9846 | const InitListExpr *ResizedArrayILE = nullptr; |
9847 | const CXXConstructExpr *ResizedArrayCCE = nullptr; |
9848 | bool ValueInit = false; |
9849 | |
9850 | QualType AllocType = E->getAllocatedType(); |
9851 | if (std::optional<const Expr *> ArraySize = E->getArraySize()) { |
9852 | const Expr *Stripped = *ArraySize; |
9853 | for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped); |
9854 | Stripped = ICE->getSubExpr()) |
9855 | if (ICE->getCastKind() != CK_NoOp && |
9856 | ICE->getCastKind() != CK_IntegralCast) |
9857 | break; |
9858 | |
9859 | llvm::APSInt ArrayBound; |
9860 | if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info)) |
9861 | return false; |
9862 | |
9863 | // C++ [expr.new]p9: |
9864 | // The expression is erroneous if: |
9865 | // -- [...] its value before converting to size_t [or] applying the |
9866 | // second standard conversion sequence is less than zero |
9867 | if (ArrayBound.isSigned() && ArrayBound.isNegative()) { |
9868 | if (IsNothrow) |
9869 | return ZeroInitialization(E); |
9870 | |
9871 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_negative) |
9872 | << ArrayBound << (*ArraySize)->getSourceRange(); |
9873 | return false; |
9874 | } |
9875 | |
9876 | // -- its value is such that the size of the allocated object would |
9877 | // exceed the implementation-defined limit |
9878 | if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(), |
9879 | BitWidth: ConstantArrayType::getNumAddressingBits( |
9880 | Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound), |
9881 | ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) { |
9882 | if (IsNothrow) |
9883 | return ZeroInitialization(E); |
9884 | return false; |
9885 | } |
9886 | |
9887 | // -- the new-initializer is a braced-init-list and the number of |
9888 | // array elements for which initializers are provided [...] |
9889 | // exceeds the number of elements to initialize |
9890 | if (!Init) { |
9891 | // No initialization is performed. |
9892 | } else if (isa<CXXScalarValueInitExpr>(Val: Init) || |
9893 | isa<ImplicitValueInitExpr>(Val: Init)) { |
9894 | ValueInit = true; |
9895 | } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) { |
9896 | ResizedArrayCCE = CCE; |
9897 | } else { |
9898 | auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType()); |
9899 | assert(CAT && "unexpected type for array initializer" ); |
9900 | |
9901 | unsigned Bits = |
9902 | std::max(a: CAT->getSize().getBitWidth(), b: ArrayBound.getBitWidth()); |
9903 | llvm::APInt InitBound = CAT->getSize().zext(width: Bits); |
9904 | llvm::APInt AllocBound = ArrayBound.zext(width: Bits); |
9905 | if (InitBound.ugt(RHS: AllocBound)) { |
9906 | if (IsNothrow) |
9907 | return ZeroInitialization(E); |
9908 | |
9909 | Info.FFDiag(*ArraySize, diag::note_constexpr_new_too_small) |
9910 | << toString(AllocBound, 10, /*Signed=*/false) |
9911 | << toString(InitBound, 10, /*Signed=*/false) |
9912 | << (*ArraySize)->getSourceRange(); |
9913 | return false; |
9914 | } |
9915 | |
9916 | // If the sizes differ, we must have an initializer list, and we need |
9917 | // special handling for this case when we initialize. |
9918 | if (InitBound != AllocBound) |
9919 | ResizedArrayILE = cast<InitListExpr>(Val: Init); |
9920 | } |
9921 | |
9922 | AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr, |
9923 | ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0); |
9924 | } else { |
9925 | assert(!AllocType->isArrayType() && |
9926 | "array allocation with non-array new" ); |
9927 | } |
9928 | |
9929 | APValue *Val; |
9930 | if (IsPlacement) { |
9931 | AccessKinds AK = AK_Construct; |
9932 | struct FindObjectHandler { |
9933 | EvalInfo &Info; |
9934 | const Expr *E; |
9935 | QualType AllocType; |
9936 | const AccessKinds AccessKind; |
9937 | APValue *Value; |
9938 | |
9939 | typedef bool result_type; |
9940 | bool failed() { return false; } |
9941 | bool found(APValue &Subobj, QualType SubobjType) { |
9942 | // FIXME: Reject the cases where [basic.life]p8 would not permit the |
9943 | // old name of the object to be used to name the new object. |
9944 | if (!Info.Ctx.hasSameUnqualifiedType(SubobjType, AllocType)) { |
9945 | Info.FFDiag(E, diag::note_constexpr_placement_new_wrong_type) << |
9946 | SubobjType << AllocType; |
9947 | return false; |
9948 | } |
9949 | Value = &Subobj; |
9950 | return true; |
9951 | } |
9952 | bool found(APSInt &Value, QualType SubobjType) { |
9953 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9954 | return false; |
9955 | } |
9956 | bool found(APFloat &Value, QualType SubobjType) { |
9957 | Info.FFDiag(E, diag::note_constexpr_construct_complex_elem); |
9958 | return false; |
9959 | } |
9960 | } Handler = {Info, E, AllocType, AK, nullptr}; |
9961 | |
9962 | CompleteObject Obj = findCompleteObject(Info, E, AK, Result, AllocType); |
9963 | if (!Obj || !findSubobject(Info, E, Obj, Result.Designator, Handler)) |
9964 | return false; |
9965 | |
9966 | Val = Handler.Value; |
9967 | |
9968 | // [basic.life]p1: |
9969 | // The lifetime of an object o of type T ends when [...] the storage |
9970 | // which the object occupies is [...] reused by an object that is not |
9971 | // nested within o (6.6.2). |
9972 | *Val = APValue(); |
9973 | } else { |
9974 | // Perform the allocation and obtain a pointer to the resulting object. |
9975 | Val = Info.createHeapAlloc(E, AllocType, Result); |
9976 | if (!Val) |
9977 | return false; |
9978 | } |
9979 | |
9980 | if (ValueInit) { |
9981 | ImplicitValueInitExpr VIE(AllocType); |
9982 | if (!EvaluateInPlace(*Val, Info, Result, &VIE)) |
9983 | return false; |
9984 | } else if (ResizedArrayILE) { |
9985 | if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE, |
9986 | AllocType)) |
9987 | return false; |
9988 | } else if (ResizedArrayCCE) { |
9989 | if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE, |
9990 | AllocType)) |
9991 | return false; |
9992 | } else if (Init) { |
9993 | if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init)) |
9994 | return false; |
9995 | } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) { |
9996 | return false; |
9997 | } |
9998 | |
9999 | // Array new returns a pointer to the first element, not a pointer to the |
10000 | // array. |
10001 | if (auto *AT = AllocType->getAsArrayTypeUnsafe()) |
10002 | Result.addArray(Info, E, CAT: cast<ConstantArrayType>(AT)); |
10003 | |
10004 | return true; |
10005 | } |
10006 | //===----------------------------------------------------------------------===// |
10007 | // Member Pointer Evaluation |
10008 | //===----------------------------------------------------------------------===// |
10009 | |
10010 | namespace { |
10011 | class MemberPointerExprEvaluator |
10012 | : public ExprEvaluatorBase<MemberPointerExprEvaluator> { |
10013 | MemberPtr &Result; |
10014 | |
10015 | bool Success(const ValueDecl *D) { |
10016 | Result = MemberPtr(D); |
10017 | return true; |
10018 | } |
10019 | public: |
10020 | |
10021 | MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result) |
10022 | : ExprEvaluatorBaseTy(Info), Result(Result) {} |
10023 | |
10024 | bool Success(const APValue &V, const Expr *E) { |
10025 | Result.setFrom(V); |
10026 | return true; |
10027 | } |
10028 | bool ZeroInitialization(const Expr *E) { |
10029 | return Success(D: (const ValueDecl*)nullptr); |
10030 | } |
10031 | |
10032 | bool VisitCastExpr(const CastExpr *E); |
10033 | bool VisitUnaryAddrOf(const UnaryOperator *E); |
10034 | }; |
10035 | } // end anonymous namespace |
10036 | |
10037 | static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result, |
10038 | EvalInfo &Info) { |
10039 | assert(!E->isValueDependent()); |
10040 | assert(E->isPRValue() && E->getType()->isMemberPointerType()); |
10041 | return MemberPointerExprEvaluator(Info, Result).Visit(E); |
10042 | } |
10043 | |
10044 | bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10045 | switch (E->getCastKind()) { |
10046 | default: |
10047 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10048 | |
10049 | case CK_NullToMemberPointer: |
10050 | VisitIgnoredValue(E: E->getSubExpr()); |
10051 | return ZeroInitialization(E); |
10052 | |
10053 | case CK_BaseToDerivedMemberPointer: { |
10054 | if (!Visit(E->getSubExpr())) |
10055 | return false; |
10056 | if (E->path_empty()) |
10057 | return true; |
10058 | // Base-to-derived member pointer casts store the path in derived-to-base |
10059 | // order, so iterate backwards. The CXXBaseSpecifier also provides us with |
10060 | // the wrong end of the derived->base arc, so stagger the path by one class. |
10061 | typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter; |
10062 | for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin()); |
10063 | PathI != PathE; ++PathI) { |
10064 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase" ); |
10065 | const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl(); |
10066 | if (!Result.castToDerived(Derived)) |
10067 | return Error(E); |
10068 | } |
10069 | const Type *FinalTy = E->getType()->castAs<MemberPointerType>()->getClass(); |
10070 | if (!Result.castToDerived(Derived: FinalTy->getAsCXXRecordDecl())) |
10071 | return Error(E); |
10072 | return true; |
10073 | } |
10074 | |
10075 | case CK_DerivedToBaseMemberPointer: |
10076 | if (!Visit(E->getSubExpr())) |
10077 | return false; |
10078 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10079 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10080 | assert(!(*PathI)->isVirtual() && "memptr cast through vbase" ); |
10081 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10082 | if (!Result.castToBase(Base)) |
10083 | return Error(E); |
10084 | } |
10085 | return true; |
10086 | } |
10087 | } |
10088 | |
10089 | bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) { |
10090 | // C++11 [expr.unary.op]p3 has very strict rules on how the address of a |
10091 | // member can be formed. |
10092 | return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl()); |
10093 | } |
10094 | |
10095 | //===----------------------------------------------------------------------===// |
10096 | // Record Evaluation |
10097 | //===----------------------------------------------------------------------===// |
10098 | |
10099 | namespace { |
10100 | class RecordExprEvaluator |
10101 | : public ExprEvaluatorBase<RecordExprEvaluator> { |
10102 | const LValue &This; |
10103 | APValue &Result; |
10104 | public: |
10105 | |
10106 | RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result) |
10107 | : ExprEvaluatorBaseTy(info), This(This), Result(Result) {} |
10108 | |
10109 | bool Success(const APValue &V, const Expr *E) { |
10110 | Result = V; |
10111 | return true; |
10112 | } |
10113 | bool ZeroInitialization(const Expr *E) { |
10114 | return ZeroInitialization(E, T: E->getType()); |
10115 | } |
10116 | bool ZeroInitialization(const Expr *E, QualType T); |
10117 | |
10118 | bool VisitCallExpr(const CallExpr *E) { |
10119 | return handleCallExpr(E, Result, ResultSlot: &This); |
10120 | } |
10121 | bool VisitCastExpr(const CastExpr *E); |
10122 | bool VisitInitListExpr(const InitListExpr *E); |
10123 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10124 | return VisitCXXConstructExpr(E, E->getType()); |
10125 | } |
10126 | bool VisitLambdaExpr(const LambdaExpr *E); |
10127 | bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E); |
10128 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T); |
10129 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E); |
10130 | bool VisitBinCmp(const BinaryOperator *E); |
10131 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
10132 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
10133 | ArrayRef<Expr *> Args); |
10134 | }; |
10135 | } |
10136 | |
10137 | /// Perform zero-initialization on an object of non-union class type. |
10138 | /// C++11 [dcl.init]p5: |
10139 | /// To zero-initialize an object or reference of type T means: |
10140 | /// [...] |
10141 | /// -- if T is a (possibly cv-qualified) non-union class type, |
10142 | /// each non-static data member and each base-class subobject is |
10143 | /// zero-initialized |
10144 | static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E, |
10145 | const RecordDecl *RD, |
10146 | const LValue &This, APValue &Result) { |
10147 | assert(!RD->isUnion() && "Expected non-union class type" ); |
10148 | const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD); |
10149 | Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0, |
10150 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10151 | |
10152 | if (RD->isInvalidDecl()) return false; |
10153 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10154 | |
10155 | if (CD) { |
10156 | unsigned Index = 0; |
10157 | for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(), |
10158 | End = CD->bases_end(); I != End; ++I, ++Index) { |
10159 | const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl(); |
10160 | LValue Subobject = This; |
10161 | if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout)) |
10162 | return false; |
10163 | if (!HandleClassZeroInitialization(Info, E, Base, Subobject, |
10164 | Result.getStructBase(i: Index))) |
10165 | return false; |
10166 | } |
10167 | } |
10168 | |
10169 | for (const auto *I : RD->fields()) { |
10170 | // -- if T is a reference type, no initialization is performed. |
10171 | if (I->isUnnamedBitfield() || I->getType()->isReferenceType()) |
10172 | continue; |
10173 | |
10174 | LValue Subobject = This; |
10175 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout)) |
10176 | return false; |
10177 | |
10178 | ImplicitValueInitExpr VIE(I->getType()); |
10179 | if (!EvaluateInPlace( |
10180 | Result.getStructField(i: I->getFieldIndex()), Info, Subobject, &VIE)) |
10181 | return false; |
10182 | } |
10183 | |
10184 | return true; |
10185 | } |
10186 | |
10187 | bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) { |
10188 | const RecordDecl *RD = T->castAs<RecordType>()->getDecl(); |
10189 | if (RD->isInvalidDecl()) return false; |
10190 | if (RD->isUnion()) { |
10191 | // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the |
10192 | // object's first non-static named data member is zero-initialized |
10193 | RecordDecl::field_iterator I = RD->field_begin(); |
10194 | while (I != RD->field_end() && (*I)->isUnnamedBitfield()) |
10195 | ++I; |
10196 | if (I == RD->field_end()) { |
10197 | Result = APValue((const FieldDecl*)nullptr); |
10198 | return true; |
10199 | } |
10200 | |
10201 | LValue Subobject = This; |
10202 | if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I)) |
10203 | return false; |
10204 | Result = APValue(*I); |
10205 | ImplicitValueInitExpr VIE(I->getType()); |
10206 | return EvaluateInPlace(Result.getUnionValue(), Info, Subobject, &VIE); |
10207 | } |
10208 | |
10209 | if (isa<CXXRecordDecl>(Val: RD) && cast<CXXRecordDecl>(Val: RD)->getNumVBases()) { |
10210 | Info.FFDiag(E, diag::note_constexpr_virtual_base) << RD; |
10211 | return false; |
10212 | } |
10213 | |
10214 | return HandleClassZeroInitialization(Info, E, RD, This, Result); |
10215 | } |
10216 | |
10217 | bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10218 | switch (E->getCastKind()) { |
10219 | default: |
10220 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10221 | |
10222 | case CK_ConstructorConversion: |
10223 | return Visit(E->getSubExpr()); |
10224 | |
10225 | case CK_DerivedToBase: |
10226 | case CK_UncheckedDerivedToBase: { |
10227 | APValue DerivedObject; |
10228 | if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr())) |
10229 | return false; |
10230 | if (!DerivedObject.isStruct()) |
10231 | return Error(E: E->getSubExpr()); |
10232 | |
10233 | // Derived-to-base rvalue conversion: just slice off the derived part. |
10234 | APValue *Value = &DerivedObject; |
10235 | const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl(); |
10236 | for (CastExpr::path_const_iterator PathI = E->path_begin(), |
10237 | PathE = E->path_end(); PathI != PathE; ++PathI) { |
10238 | assert(!(*PathI)->isVirtual() && "record rvalue with virtual base" ); |
10239 | const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl(); |
10240 | Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base)); |
10241 | RD = Base; |
10242 | } |
10243 | Result = *Value; |
10244 | return true; |
10245 | } |
10246 | } |
10247 | } |
10248 | |
10249 | bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10250 | if (E->isTransparent()) |
10251 | return Visit(E->getInit(Init: 0)); |
10252 | return VisitCXXParenListOrInitListExpr(E, E->inits()); |
10253 | } |
10254 | |
10255 | bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr( |
10256 | const Expr *ExprToVisit, ArrayRef<Expr *> Args) { |
10257 | const RecordDecl *RD = |
10258 | ExprToVisit->getType()->castAs<RecordType>()->getDecl(); |
10259 | if (RD->isInvalidDecl()) return false; |
10260 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD); |
10261 | auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD); |
10262 | |
10263 | EvalInfo::EvaluatingConstructorRAII EvalObj( |
10264 | Info, |
10265 | ObjectUnderConstruction{This.getLValueBase(), This.Designator.Entries}, |
10266 | CXXRD && CXXRD->getNumBases()); |
10267 | |
10268 | if (RD->isUnion()) { |
10269 | const FieldDecl *Field; |
10270 | if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) { |
10271 | Field = ILE->getInitializedFieldInUnion(); |
10272 | } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) { |
10273 | Field = PLIE->getInitializedFieldInUnion(); |
10274 | } else { |
10275 | llvm_unreachable( |
10276 | "Expression is neither an init list nor a C++ paren list" ); |
10277 | } |
10278 | |
10279 | Result = APValue(Field); |
10280 | if (!Field) |
10281 | return true; |
10282 | |
10283 | // If the initializer list for a union does not contain any elements, the |
10284 | // first element of the union is value-initialized. |
10285 | // FIXME: The element should be initialized from an initializer list. |
10286 | // Is this difference ever observable for initializer lists which |
10287 | // we don't build? |
10288 | ImplicitValueInitExpr VIE(Field->getType()); |
10289 | const Expr *InitExpr = Args.empty() ? &VIE : Args[0]; |
10290 | |
10291 | LValue Subobject = This; |
10292 | if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout)) |
10293 | return false; |
10294 | |
10295 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10296 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10297 | isa<CXXDefaultInitExpr>(Val: InitExpr)); |
10298 | |
10299 | if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) { |
10300 | if (Field->isBitField()) |
10301 | return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(), |
10302 | FD: Field); |
10303 | return true; |
10304 | } |
10305 | |
10306 | return false; |
10307 | } |
10308 | |
10309 | if (!Result.hasValue()) |
10310 | Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0, |
10311 | std::distance(first: RD->field_begin(), last: RD->field_end())); |
10312 | unsigned ElementNo = 0; |
10313 | bool Success = true; |
10314 | |
10315 | // Initialize base classes. |
10316 | if (CXXRD && CXXRD->getNumBases()) { |
10317 | for (const auto &Base : CXXRD->bases()) { |
10318 | assert(ElementNo < Args.size() && "missing init for base class" ); |
10319 | const Expr *Init = Args[ElementNo]; |
10320 | |
10321 | LValue Subobject = This; |
10322 | if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base)) |
10323 | return false; |
10324 | |
10325 | APValue &FieldVal = Result.getStructBase(i: ElementNo); |
10326 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) { |
10327 | if (!Info.noteFailure()) |
10328 | return false; |
10329 | Success = false; |
10330 | } |
10331 | ++ElementNo; |
10332 | } |
10333 | |
10334 | EvalObj.finishedConstructingBases(); |
10335 | } |
10336 | |
10337 | // Initialize members. |
10338 | for (const auto *Field : RD->fields()) { |
10339 | // Anonymous bit-fields are not considered members of the class for |
10340 | // purposes of aggregate initialization. |
10341 | if (Field->isUnnamedBitfield()) |
10342 | continue; |
10343 | |
10344 | LValue Subobject = This; |
10345 | |
10346 | bool HaveInit = ElementNo < Args.size(); |
10347 | |
10348 | // FIXME: Diagnostics here should point to the end of the initializer |
10349 | // list, not the start. |
10350 | if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit, |
10351 | LVal&: Subobject, FD: Field, RL: &Layout)) |
10352 | return false; |
10353 | |
10354 | // Perform an implicit value-initialization for members beyond the end of |
10355 | // the initializer list. |
10356 | ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType()); |
10357 | const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE; |
10358 | |
10359 | if (Field->getType()->isIncompleteArrayType()) { |
10360 | if (auto *CAT = Info.Ctx.getAsConstantArrayType(Init->getType())) { |
10361 | if (!CAT->getSize().isZero()) { |
10362 | // Bail out for now. This might sort of "work", but the rest of the |
10363 | // code isn't really prepared to handle it. |
10364 | Info.FFDiag(Init, diag::note_constexpr_unsupported_flexible_array); |
10365 | return false; |
10366 | } |
10367 | } |
10368 | } |
10369 | |
10370 | // Temporarily override This, in case there's a CXXDefaultInitExpr in here. |
10371 | ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This, |
10372 | isa<CXXDefaultInitExpr>(Val: Init)); |
10373 | |
10374 | APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex()); |
10375 | if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) || |
10376 | (Field->isBitField() && !truncateBitfieldValue(Info, E: Init, |
10377 | Value&: FieldVal, FD: Field))) { |
10378 | if (!Info.noteFailure()) |
10379 | return false; |
10380 | Success = false; |
10381 | } |
10382 | } |
10383 | |
10384 | EvalObj.finishedConstructingFields(); |
10385 | |
10386 | return Success; |
10387 | } |
10388 | |
10389 | bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
10390 | QualType T) { |
10391 | // Note that E's type is not necessarily the type of our class here; we might |
10392 | // be initializing an array element instead. |
10393 | const CXXConstructorDecl *FD = E->getConstructor(); |
10394 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false; |
10395 | |
10396 | bool ZeroInit = E->requiresZeroInitialization(); |
10397 | if (CheckTrivialDefaultConstructor(Info, E->getExprLoc(), FD, ZeroInit)) { |
10398 | // If we've already performed zero-initialization, we're already done. |
10399 | if (Result.hasValue()) |
10400 | return true; |
10401 | |
10402 | if (ZeroInit) |
10403 | return ZeroInitialization(E, T); |
10404 | |
10405 | return handleDefaultInitValue(T, Result); |
10406 | } |
10407 | |
10408 | const FunctionDecl *Definition = nullptr; |
10409 | auto Body = FD->getBody(Definition); |
10410 | |
10411 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10412 | return false; |
10413 | |
10414 | // Avoid materializing a temporary for an elidable copy/move constructor. |
10415 | if (E->isElidable() && !ZeroInit) { |
10416 | // FIXME: This only handles the simplest case, where the source object |
10417 | // is passed directly as the first argument to the constructor. |
10418 | // This should also handle stepping though implicit casts and |
10419 | // and conversion sequences which involve two steps, with a |
10420 | // conversion operator followed by a converting constructor. |
10421 | const Expr *SrcObj = E->getArg(Arg: 0); |
10422 | assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent())); |
10423 | assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType())); |
10424 | if (const MaterializeTemporaryExpr *ME = |
10425 | dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj)) |
10426 | return Visit(ME->getSubExpr()); |
10427 | } |
10428 | |
10429 | if (ZeroInit && !ZeroInitialization(E, T)) |
10430 | return false; |
10431 | |
10432 | auto Args = llvm::ArrayRef(E->getArgs(), E->getNumArgs()); |
10433 | return HandleConstructorCall(E, This, Args, |
10434 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10435 | Result); |
10436 | } |
10437 | |
10438 | bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr( |
10439 | const CXXInheritedCtorInitExpr *E) { |
10440 | if (!Info.CurrentCall) { |
10441 | assert(Info.checkingPotentialConstantExpression()); |
10442 | return false; |
10443 | } |
10444 | |
10445 | const CXXConstructorDecl *FD = E->getConstructor(); |
10446 | if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) |
10447 | return false; |
10448 | |
10449 | const FunctionDecl *Definition = nullptr; |
10450 | auto Body = FD->getBody(Definition); |
10451 | |
10452 | if (!CheckConstexprFunction(Info, E->getExprLoc(), FD, Definition, Body)) |
10453 | return false; |
10454 | |
10455 | return HandleConstructorCall(E, This, Info.CurrentCall->Arguments, |
10456 | cast<CXXConstructorDecl>(Val: Definition), Info, |
10457 | Result); |
10458 | } |
10459 | |
10460 | bool RecordExprEvaluator::VisitCXXStdInitializerListExpr( |
10461 | const CXXStdInitializerListExpr *E) { |
10462 | const ConstantArrayType *ArrayType = |
10463 | Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType()); |
10464 | |
10465 | LValue Array; |
10466 | if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info)) |
10467 | return false; |
10468 | |
10469 | assert(ArrayType && "unexpected type for array initializer" ); |
10470 | |
10471 | // Get a pointer to the first element of the array. |
10472 | Array.addArray(Info, E, ArrayType); |
10473 | |
10474 | auto InvalidType = [&] { |
10475 | Info.FFDiag(E, diag::note_constexpr_unsupported_layout) |
10476 | << E->getType(); |
10477 | return false; |
10478 | }; |
10479 | |
10480 | // FIXME: Perform the checks on the field types in SemaInit. |
10481 | RecordDecl *Record = E->getType()->castAs<RecordType>()->getDecl(); |
10482 | RecordDecl::field_iterator Field = Record->field_begin(); |
10483 | if (Field == Record->field_end()) |
10484 | return InvalidType(); |
10485 | |
10486 | // Start pointer. |
10487 | if (!Field->getType()->isPointerType() || |
10488 | !Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10489 | ArrayType->getElementType())) |
10490 | return InvalidType(); |
10491 | |
10492 | // FIXME: What if the initializer_list type has base classes, etc? |
10493 | Result = APValue(APValue::UninitStruct(), 0, 2); |
10494 | Array.moveInto(V&: Result.getStructField(i: 0)); |
10495 | |
10496 | if (++Field == Record->field_end()) |
10497 | return InvalidType(); |
10498 | |
10499 | if (Field->getType()->isPointerType() && |
10500 | Info.Ctx.hasSameType(Field->getType()->getPointeeType(), |
10501 | ArrayType->getElementType())) { |
10502 | // End pointer. |
10503 | if (!HandleLValueArrayAdjustment(Info, E, Array, |
10504 | ArrayType->getElementType(), |
10505 | ArrayType->getSize().getZExtValue())) |
10506 | return false; |
10507 | Array.moveInto(V&: Result.getStructField(i: 1)); |
10508 | } else if (Info.Ctx.hasSameType(Field->getType(), Info.Ctx.getSizeType())) |
10509 | // Length. |
10510 | Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize())); |
10511 | else |
10512 | return InvalidType(); |
10513 | |
10514 | if (++Field != Record->field_end()) |
10515 | return InvalidType(); |
10516 | |
10517 | return true; |
10518 | } |
10519 | |
10520 | bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) { |
10521 | const CXXRecordDecl *ClosureClass = E->getLambdaClass(); |
10522 | if (ClosureClass->isInvalidDecl()) |
10523 | return false; |
10524 | |
10525 | const size_t NumFields = |
10526 | std::distance(ClosureClass->field_begin(), ClosureClass->field_end()); |
10527 | |
10528 | assert(NumFields == (size_t)std::distance(E->capture_init_begin(), |
10529 | E->capture_init_end()) && |
10530 | "The number of lambda capture initializers should equal the number of " |
10531 | "fields within the closure type" ); |
10532 | |
10533 | Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields); |
10534 | // Iterate through all the lambda's closure object's fields and initialize |
10535 | // them. |
10536 | auto *CaptureInitIt = E->capture_init_begin(); |
10537 | bool Success = true; |
10538 | const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(ClosureClass); |
10539 | for (const auto *Field : ClosureClass->fields()) { |
10540 | assert(CaptureInitIt != E->capture_init_end()); |
10541 | // Get the initializer for this field |
10542 | Expr *const CurFieldInit = *CaptureInitIt++; |
10543 | |
10544 | // If there is no initializer, either this is a VLA or an error has |
10545 | // occurred. |
10546 | if (!CurFieldInit) |
10547 | return Error(E); |
10548 | |
10549 | LValue Subobject = This; |
10550 | |
10551 | if (!HandleLValueMember(Info, E, Subobject, Field, &Layout)) |
10552 | return false; |
10553 | |
10554 | APValue &FieldVal = Result.getStructField(Field->getFieldIndex()); |
10555 | if (!EvaluateInPlace(FieldVal, Info, Subobject, CurFieldInit)) { |
10556 | if (!Info.keepEvaluatingAfterFailure()) |
10557 | return false; |
10558 | Success = false; |
10559 | } |
10560 | } |
10561 | return Success; |
10562 | } |
10563 | |
10564 | static bool EvaluateRecord(const Expr *E, const LValue &This, |
10565 | APValue &Result, EvalInfo &Info) { |
10566 | assert(!E->isValueDependent()); |
10567 | assert(E->isPRValue() && E->getType()->isRecordType() && |
10568 | "can't evaluate expression as a record rvalue" ); |
10569 | return RecordExprEvaluator(Info, This, Result).Visit(E); |
10570 | } |
10571 | |
10572 | //===----------------------------------------------------------------------===// |
10573 | // Temporary Evaluation |
10574 | // |
10575 | // Temporaries are represented in the AST as rvalues, but generally behave like |
10576 | // lvalues. The full-object of which the temporary is a subobject is implicitly |
10577 | // materialized so that a reference can bind to it. |
10578 | //===----------------------------------------------------------------------===// |
10579 | namespace { |
10580 | class TemporaryExprEvaluator |
10581 | : public LValueExprEvaluatorBase<TemporaryExprEvaluator> { |
10582 | public: |
10583 | TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) : |
10584 | LValueExprEvaluatorBaseTy(Info, Result, false) {} |
10585 | |
10586 | /// Visit an expression which constructs the value of this temporary. |
10587 | bool VisitConstructExpr(const Expr *E) { |
10588 | APValue &Value = Info.CurrentCall->createTemporary( |
10589 | Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result); |
10590 | return EvaluateInPlace(Result&: Value, Info, This: Result, E); |
10591 | } |
10592 | |
10593 | bool VisitCastExpr(const CastExpr *E) { |
10594 | switch (E->getCastKind()) { |
10595 | default: |
10596 | return LValueExprEvaluatorBaseTy::VisitCastExpr(E); |
10597 | |
10598 | case CK_ConstructorConversion: |
10599 | return VisitConstructExpr(E: E->getSubExpr()); |
10600 | } |
10601 | } |
10602 | bool VisitInitListExpr(const InitListExpr *E) { |
10603 | return VisitConstructExpr(E); |
10604 | } |
10605 | bool VisitCXXConstructExpr(const CXXConstructExpr *E) { |
10606 | return VisitConstructExpr(E); |
10607 | } |
10608 | bool VisitCallExpr(const CallExpr *E) { |
10609 | return VisitConstructExpr(E); |
10610 | } |
10611 | bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) { |
10612 | return VisitConstructExpr(E); |
10613 | } |
10614 | bool VisitLambdaExpr(const LambdaExpr *E) { |
10615 | return VisitConstructExpr(E); |
10616 | } |
10617 | }; |
10618 | } // end anonymous namespace |
10619 | |
10620 | /// Evaluate an expression of record type as a temporary. |
10621 | static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) { |
10622 | assert(!E->isValueDependent()); |
10623 | assert(E->isPRValue() && E->getType()->isRecordType()); |
10624 | return TemporaryExprEvaluator(Info, Result).Visit(E); |
10625 | } |
10626 | |
10627 | //===----------------------------------------------------------------------===// |
10628 | // Vector Evaluation |
10629 | //===----------------------------------------------------------------------===// |
10630 | |
10631 | namespace { |
10632 | class VectorExprEvaluator |
10633 | : public ExprEvaluatorBase<VectorExprEvaluator> { |
10634 | APValue &Result; |
10635 | public: |
10636 | |
10637 | VectorExprEvaluator(EvalInfo &info, APValue &Result) |
10638 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
10639 | |
10640 | bool Success(ArrayRef<APValue> V, const Expr *E) { |
10641 | assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements()); |
10642 | // FIXME: remove this APValue copy. |
10643 | Result = APValue(V.data(), V.size()); |
10644 | return true; |
10645 | } |
10646 | bool Success(const APValue &V, const Expr *E) { |
10647 | assert(V.isVector()); |
10648 | Result = V; |
10649 | return true; |
10650 | } |
10651 | bool ZeroInitialization(const Expr *E); |
10652 | |
10653 | bool VisitUnaryReal(const UnaryOperator *E) |
10654 | { return Visit(E->getSubExpr()); } |
10655 | bool VisitCastExpr(const CastExpr* E); |
10656 | bool VisitInitListExpr(const InitListExpr *E); |
10657 | bool VisitUnaryImag(const UnaryOperator *E); |
10658 | bool VisitBinaryOperator(const BinaryOperator *E); |
10659 | bool VisitUnaryOperator(const UnaryOperator *E); |
10660 | // FIXME: Missing: conditional operator (for GNU |
10661 | // conditional select), shufflevector, ExtVectorElementExpr |
10662 | }; |
10663 | } // end anonymous namespace |
10664 | |
10665 | static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) { |
10666 | assert(E->isPRValue() && E->getType()->isVectorType() && |
10667 | "not a vector prvalue" ); |
10668 | return VectorExprEvaluator(Info, Result).Visit(E); |
10669 | } |
10670 | |
10671 | bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) { |
10672 | const VectorType *VTy = E->getType()->castAs<VectorType>(); |
10673 | unsigned NElts = VTy->getNumElements(); |
10674 | |
10675 | const Expr *SE = E->getSubExpr(); |
10676 | QualType SETy = SE->getType(); |
10677 | |
10678 | switch (E->getCastKind()) { |
10679 | case CK_VectorSplat: { |
10680 | APValue Val = APValue(); |
10681 | if (SETy->isIntegerType()) { |
10682 | APSInt IntResult; |
10683 | if (!EvaluateInteger(E: SE, Result&: IntResult, Info)) |
10684 | return false; |
10685 | Val = APValue(std::move(IntResult)); |
10686 | } else if (SETy->isRealFloatingType()) { |
10687 | APFloat FloatResult(0.0); |
10688 | if (!EvaluateFloat(E: SE, Result&: FloatResult, Info)) |
10689 | return false; |
10690 | Val = APValue(std::move(FloatResult)); |
10691 | } else { |
10692 | return Error(E); |
10693 | } |
10694 | |
10695 | // Splat and create vector APValue. |
10696 | SmallVector<APValue, 4> Elts(NElts, Val); |
10697 | return Success(Elts, E); |
10698 | } |
10699 | case CK_BitCast: { |
10700 | APValue SVal; |
10701 | if (!Evaluate(Result&: SVal, Info, E: SE)) |
10702 | return false; |
10703 | |
10704 | if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) { |
10705 | // Give up if the input isn't an int, float, or vector. For example, we |
10706 | // reject "(v4i16)(intptr_t)&a". |
10707 | Info.FFDiag(E, diag::note_constexpr_invalid_cast) |
10708 | << 2 << Info.Ctx.getLangOpts().CPlusPlus; |
10709 | return false; |
10710 | } |
10711 | |
10712 | if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E)) |
10713 | return false; |
10714 | |
10715 | return true; |
10716 | } |
10717 | default: |
10718 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
10719 | } |
10720 | } |
10721 | |
10722 | bool |
10723 | VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
10724 | const VectorType *VT = E->getType()->castAs<VectorType>(); |
10725 | unsigned NumInits = E->getNumInits(); |
10726 | unsigned NumElements = VT->getNumElements(); |
10727 | |
10728 | QualType EltTy = VT->getElementType(); |
10729 | SmallVector<APValue, 4> Elements; |
10730 | |
10731 | // The number of initializers can be less than the number of |
10732 | // vector elements. For OpenCL, this can be due to nested vector |
10733 | // initialization. For GCC compatibility, missing trailing elements |
10734 | // should be initialized with zeroes. |
10735 | unsigned CountInits = 0, CountElts = 0; |
10736 | while (CountElts < NumElements) { |
10737 | // Handle nested vector initialization. |
10738 | if (CountInits < NumInits |
10739 | && E->getInit(Init: CountInits)->getType()->isVectorType()) { |
10740 | APValue v; |
10741 | if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info)) |
10742 | return Error(E); |
10743 | unsigned vlen = v.getVectorLength(); |
10744 | for (unsigned j = 0; j < vlen; j++) |
10745 | Elements.push_back(Elt: v.getVectorElt(I: j)); |
10746 | CountElts += vlen; |
10747 | } else if (EltTy->isIntegerType()) { |
10748 | llvm::APSInt sInt(32); |
10749 | if (CountInits < NumInits) { |
10750 | if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info)) |
10751 | return false; |
10752 | } else // trailing integer zero. |
10753 | sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy); |
10754 | Elements.push_back(Elt: APValue(sInt)); |
10755 | CountElts++; |
10756 | } else { |
10757 | llvm::APFloat f(0.0); |
10758 | if (CountInits < NumInits) { |
10759 | if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info)) |
10760 | return false; |
10761 | } else // trailing float zero. |
10762 | f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)); |
10763 | Elements.push_back(Elt: APValue(f)); |
10764 | CountElts++; |
10765 | } |
10766 | CountInits++; |
10767 | } |
10768 | return Success(Elements, E); |
10769 | } |
10770 | |
10771 | bool |
10772 | VectorExprEvaluator::ZeroInitialization(const Expr *E) { |
10773 | const auto *VT = E->getType()->castAs<VectorType>(); |
10774 | QualType EltTy = VT->getElementType(); |
10775 | APValue ZeroElement; |
10776 | if (EltTy->isIntegerType()) |
10777 | ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy)); |
10778 | else |
10779 | ZeroElement = |
10780 | APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy))); |
10781 | |
10782 | SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement); |
10783 | return Success(V: Elements, E); |
10784 | } |
10785 | |
10786 | bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
10787 | VisitIgnoredValue(E: E->getSubExpr()); |
10788 | return ZeroInitialization(E); |
10789 | } |
10790 | |
10791 | bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
10792 | BinaryOperatorKind Op = E->getOpcode(); |
10793 | assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp && |
10794 | "Operation not supported on vector types" ); |
10795 | |
10796 | if (Op == BO_Comma) |
10797 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
10798 | |
10799 | Expr *LHS = E->getLHS(); |
10800 | Expr *RHS = E->getRHS(); |
10801 | |
10802 | assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() && |
10803 | "Must both be vector types" ); |
10804 | // Checking JUST the types are the same would be fine, except shifts don't |
10805 | // need to have their types be the same (since you always shift by an int). |
10806 | assert(LHS->getType()->castAs<VectorType>()->getNumElements() == |
10807 | E->getType()->castAs<VectorType>()->getNumElements() && |
10808 | RHS->getType()->castAs<VectorType>()->getNumElements() == |
10809 | E->getType()->castAs<VectorType>()->getNumElements() && |
10810 | "All operands must be the same size." ); |
10811 | |
10812 | APValue LHSValue; |
10813 | APValue RHSValue; |
10814 | bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS); |
10815 | if (!LHSOK && !Info.noteFailure()) |
10816 | return false; |
10817 | if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK) |
10818 | return false; |
10819 | |
10820 | if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue)) |
10821 | return false; |
10822 | |
10823 | return Success(LHSValue, E); |
10824 | } |
10825 | |
10826 | static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx, |
10827 | QualType ResultTy, |
10828 | UnaryOperatorKind Op, |
10829 | APValue Elt) { |
10830 | switch (Op) { |
10831 | case UO_Plus: |
10832 | // Nothing to do here. |
10833 | return Elt; |
10834 | case UO_Minus: |
10835 | if (Elt.getKind() == APValue::Int) { |
10836 | Elt.getInt().negate(); |
10837 | } else { |
10838 | assert(Elt.getKind() == APValue::Float && |
10839 | "Vector can only be int or float type" ); |
10840 | Elt.getFloat().changeSign(); |
10841 | } |
10842 | return Elt; |
10843 | case UO_Not: |
10844 | // This is only valid for integral types anyway, so we don't have to handle |
10845 | // float here. |
10846 | assert(Elt.getKind() == APValue::Int && |
10847 | "Vector operator ~ can only be int" ); |
10848 | Elt.getInt().flipAllBits(); |
10849 | return Elt; |
10850 | case UO_LNot: { |
10851 | if (Elt.getKind() == APValue::Int) { |
10852 | Elt.getInt() = !Elt.getInt(); |
10853 | // operator ! on vectors returns -1 for 'truth', so negate it. |
10854 | Elt.getInt().negate(); |
10855 | return Elt; |
10856 | } |
10857 | assert(Elt.getKind() == APValue::Float && |
10858 | "Vector can only be int or float type" ); |
10859 | // Float types result in an int of the same size, but -1 for true, or 0 for |
10860 | // false. |
10861 | APSInt EltResult{Ctx.getIntWidth(T: ResultTy), |
10862 | ResultTy->isUnsignedIntegerType()}; |
10863 | if (Elt.getFloat().isZero()) |
10864 | EltResult.setAllBits(); |
10865 | else |
10866 | EltResult.clearAllBits(); |
10867 | |
10868 | return APValue{EltResult}; |
10869 | } |
10870 | default: |
10871 | // FIXME: Implement the rest of the unary operators. |
10872 | return std::nullopt; |
10873 | } |
10874 | } |
10875 | |
10876 | bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
10877 | Expr *SubExpr = E->getSubExpr(); |
10878 | const auto *VD = SubExpr->getType()->castAs<VectorType>(); |
10879 | // This result element type differs in the case of negating a floating point |
10880 | // vector, since the result type is the a vector of the equivilant sized |
10881 | // integer. |
10882 | const QualType ResultEltTy = VD->getElementType(); |
10883 | UnaryOperatorKind Op = E->getOpcode(); |
10884 | |
10885 | APValue SubExprValue; |
10886 | if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr)) |
10887 | return false; |
10888 | |
10889 | // FIXME: This vector evaluator someday needs to be changed to be LValue |
10890 | // aware/keep LValue information around, rather than dealing with just vector |
10891 | // types directly. Until then, we cannot handle cases where the operand to |
10892 | // these unary operators is an LValue. The only case I've been able to see |
10893 | // cause this is operator++ assigning to a member expression (only valid in |
10894 | // altivec compilations) in C mode, so this shouldn't limit us too much. |
10895 | if (SubExprValue.isLValue()) |
10896 | return false; |
10897 | |
10898 | assert(SubExprValue.getVectorLength() == VD->getNumElements() && |
10899 | "Vector length doesn't match type?" ); |
10900 | |
10901 | SmallVector<APValue, 4> ResultElements; |
10902 | for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) { |
10903 | std::optional<APValue> Elt = handleVectorUnaryOperator( |
10904 | Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum)); |
10905 | if (!Elt) |
10906 | return false; |
10907 | ResultElements.push_back(Elt: *Elt); |
10908 | } |
10909 | return Success(APValue(ResultElements.data(), ResultElements.size()), E); |
10910 | } |
10911 | |
10912 | //===----------------------------------------------------------------------===// |
10913 | // Array Evaluation |
10914 | //===----------------------------------------------------------------------===// |
10915 | |
10916 | namespace { |
10917 | class ArrayExprEvaluator |
10918 | : public ExprEvaluatorBase<ArrayExprEvaluator> { |
10919 | const LValue &This; |
10920 | APValue &Result; |
10921 | public: |
10922 | |
10923 | ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result) |
10924 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
10925 | |
10926 | bool Success(const APValue &V, const Expr *E) { |
10927 | assert(V.isArray() && "expected array" ); |
10928 | Result = V; |
10929 | return true; |
10930 | } |
10931 | |
10932 | bool ZeroInitialization(const Expr *E) { |
10933 | const ConstantArrayType *CAT = |
10934 | Info.Ctx.getAsConstantArrayType(T: E->getType()); |
10935 | if (!CAT) { |
10936 | if (E->getType()->isIncompleteArrayType()) { |
10937 | // We can be asked to zero-initialize a flexible array member; this |
10938 | // is represented as an ImplicitValueInitExpr of incomplete array |
10939 | // type. In this case, the array has zero elements. |
10940 | Result = APValue(APValue::UninitArray(), 0, 0); |
10941 | return true; |
10942 | } |
10943 | // FIXME: We could handle VLAs here. |
10944 | return Error(E); |
10945 | } |
10946 | |
10947 | Result = APValue(APValue::UninitArray(), 0, |
10948 | CAT->getSize().getZExtValue()); |
10949 | if (!Result.hasArrayFiller()) |
10950 | return true; |
10951 | |
10952 | // Zero-initialize all elements. |
10953 | LValue Subobject = This; |
10954 | Subobject.addArray(Info, E, CAT); |
10955 | ImplicitValueInitExpr VIE(CAT->getElementType()); |
10956 | return EvaluateInPlace(Result.getArrayFiller(), Info, Subobject, &VIE); |
10957 | } |
10958 | |
10959 | bool VisitCallExpr(const CallExpr *E) { |
10960 | return handleCallExpr(E, Result, ResultSlot: &This); |
10961 | } |
10962 | bool VisitInitListExpr(const InitListExpr *E, |
10963 | QualType AllocType = QualType()); |
10964 | bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E); |
10965 | bool VisitCXXConstructExpr(const CXXConstructExpr *E); |
10966 | bool VisitCXXConstructExpr(const CXXConstructExpr *E, |
10967 | const LValue &Subobject, |
10968 | APValue *Value, QualType Type); |
10969 | bool VisitStringLiteral(const StringLiteral *E, |
10970 | QualType AllocType = QualType()) { |
10971 | expandStringLiteral(Info, S: E, Result, AllocType); |
10972 | return true; |
10973 | } |
10974 | bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E); |
10975 | bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit, |
10976 | ArrayRef<Expr *> Args, |
10977 | const Expr *ArrayFiller, |
10978 | QualType AllocType = QualType()); |
10979 | }; |
10980 | } // end anonymous namespace |
10981 | |
10982 | static bool EvaluateArray(const Expr *E, const LValue &This, |
10983 | APValue &Result, EvalInfo &Info) { |
10984 | assert(!E->isValueDependent()); |
10985 | assert(E->isPRValue() && E->getType()->isArrayType() && |
10986 | "not an array prvalue" ); |
10987 | return ArrayExprEvaluator(Info, This, Result).Visit(E); |
10988 | } |
10989 | |
10990 | static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This, |
10991 | APValue &Result, const InitListExpr *ILE, |
10992 | QualType AllocType) { |
10993 | assert(!ILE->isValueDependent()); |
10994 | assert(ILE->isPRValue() && ILE->getType()->isArrayType() && |
10995 | "not an array prvalue" ); |
10996 | return ArrayExprEvaluator(Info, This, Result) |
10997 | .VisitInitListExpr(E: ILE, AllocType); |
10998 | } |
10999 | |
11000 | static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This, |
11001 | APValue &Result, |
11002 | const CXXConstructExpr *CCE, |
11003 | QualType AllocType) { |
11004 | assert(!CCE->isValueDependent()); |
11005 | assert(CCE->isPRValue() && CCE->getType()->isArrayType() && |
11006 | "not an array prvalue" ); |
11007 | return ArrayExprEvaluator(Info, This, Result) |
11008 | .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType); |
11009 | } |
11010 | |
11011 | // Return true iff the given array filler may depend on the element index. |
11012 | static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) { |
11013 | // For now, just allow non-class value-initialization and initialization |
11014 | // lists comprised of them. |
11015 | if (isa<ImplicitValueInitExpr>(Val: FillerExpr)) |
11016 | return false; |
11017 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) { |
11018 | for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) { |
11019 | if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I))) |
11020 | return true; |
11021 | } |
11022 | |
11023 | if (ILE->hasArrayFiller() && |
11024 | MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller())) |
11025 | return true; |
11026 | |
11027 | return false; |
11028 | } |
11029 | return true; |
11030 | } |
11031 | |
11032 | bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E, |
11033 | QualType AllocType) { |
11034 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11035 | T: AllocType.isNull() ? E->getType() : AllocType); |
11036 | if (!CAT) |
11037 | return Error(E); |
11038 | |
11039 | // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...] |
11040 | // an appropriately-typed string literal enclosed in braces. |
11041 | if (E->isStringLiteralInit()) { |
11042 | auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts()); |
11043 | // FIXME: Support ObjCEncodeExpr here once we support it in |
11044 | // ArrayExprEvaluator generally. |
11045 | if (!SL) |
11046 | return Error(E); |
11047 | return VisitStringLiteral(E: SL, AllocType); |
11048 | } |
11049 | // Any other transparent list init will need proper handling of the |
11050 | // AllocType; we can't just recurse to the inner initializer. |
11051 | assert(!E->isTransparent() && |
11052 | "transparent array list initialization is not string literal init?" ); |
11053 | |
11054 | return VisitCXXParenListOrInitListExpr(E, E->inits(), E->getArrayFiller(), |
11055 | AllocType); |
11056 | } |
11057 | |
11058 | bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr( |
11059 | const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller, |
11060 | QualType AllocType) { |
11061 | const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType( |
11062 | T: AllocType.isNull() ? ExprToVisit->getType() : AllocType); |
11063 | |
11064 | bool Success = true; |
11065 | |
11066 | assert((!Result.isArray() || Result.getArrayInitializedElts() == 0) && |
11067 | "zero-initialized array shouldn't have any initialized elts" ); |
11068 | APValue Filler; |
11069 | if (Result.isArray() && Result.hasArrayFiller()) |
11070 | Filler = Result.getArrayFiller(); |
11071 | |
11072 | unsigned NumEltsToInit = Args.size(); |
11073 | unsigned NumElts = CAT->getSize().getZExtValue(); |
11074 | |
11075 | // If the initializer might depend on the array index, run it for each |
11076 | // array element. |
11077 | if (NumEltsToInit != NumElts && MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) |
11078 | NumEltsToInit = NumElts; |
11079 | |
11080 | LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: " |
11081 | << NumEltsToInit << ".\n" ); |
11082 | |
11083 | Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts); |
11084 | |
11085 | // If the array was previously zero-initialized, preserve the |
11086 | // zero-initialized values. |
11087 | if (Filler.hasValue()) { |
11088 | for (unsigned I = 0, E = Result.getArrayInitializedElts(); I != E; ++I) |
11089 | Result.getArrayInitializedElt(I) = Filler; |
11090 | if (Result.hasArrayFiller()) |
11091 | Result.getArrayFiller() = Filler; |
11092 | } |
11093 | |
11094 | LValue Subobject = This; |
11095 | Subobject.addArray(Info, E: ExprToVisit, CAT); |
11096 | for (unsigned Index = 0; Index != NumEltsToInit; ++Index) { |
11097 | const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller; |
11098 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index), |
11099 | Info, This: Subobject, E: Init) || |
11100 | !HandleLValueArrayAdjustment(Info, Init, Subobject, |
11101 | CAT->getElementType(), 1)) { |
11102 | if (!Info.noteFailure()) |
11103 | return false; |
11104 | Success = false; |
11105 | } |
11106 | } |
11107 | |
11108 | if (!Result.hasArrayFiller()) |
11109 | return Success; |
11110 | |
11111 | // If we get here, we have a trivial filler, which we can just evaluate |
11112 | // once and splat over the rest of the array elements. |
11113 | assert(ArrayFiller && "no array filler for incomplete init list" ); |
11114 | return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, |
11115 | E: ArrayFiller) && |
11116 | Success; |
11117 | } |
11118 | |
11119 | bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) { |
11120 | LValue CommonLV; |
11121 | if (E->getCommonExpr() && |
11122 | !Evaluate(Result&: Info.CurrentCall->createTemporary( |
11123 | Key: E->getCommonExpr(), |
11124 | T: getStorageType(Info.Ctx, E->getCommonExpr()), |
11125 | Scope: ScopeKind::FullExpression, LV&: CommonLV), |
11126 | Info, E: E->getCommonExpr()->getSourceExpr())) |
11127 | return false; |
11128 | |
11129 | auto *CAT = cast<ConstantArrayType>(E->getType()->castAsArrayTypeUnsafe()); |
11130 | |
11131 | uint64_t Elements = CAT->getSize().getZExtValue(); |
11132 | Result = APValue(APValue::UninitArray(), Elements, Elements); |
11133 | |
11134 | LValue Subobject = This; |
11135 | Subobject.addArray(Info, E, CAT: CAT); |
11136 | |
11137 | bool Success = true; |
11138 | for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) { |
11139 | // C++ [class.temporary]/5 |
11140 | // There are four contexts in which temporaries are destroyed at a different |
11141 | // point than the end of the full-expression. [...] The second context is |
11142 | // when a copy constructor is called to copy an element of an array while |
11143 | // the entire array is copied [...]. In either case, if the constructor has |
11144 | // one or more default arguments, the destruction of every temporary created |
11145 | // in a default argument is sequenced before the construction of the next |
11146 | // array element, if any. |
11147 | FullExpressionRAII Scope(Info); |
11148 | |
11149 | if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index), |
11150 | Info, This: Subobject, E: E->getSubExpr()) || |
11151 | !HandleLValueArrayAdjustment(Info, E, Subobject, |
11152 | CAT->getElementType(), 1)) { |
11153 | if (!Info.noteFailure()) |
11154 | return false; |
11155 | Success = false; |
11156 | } |
11157 | |
11158 | // Make sure we run the destructors too. |
11159 | Scope.destroy(); |
11160 | } |
11161 | |
11162 | return Success; |
11163 | } |
11164 | |
11165 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) { |
11166 | return VisitCXXConstructExpr(E, This, &Result, E->getType()); |
11167 | } |
11168 | |
11169 | bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E, |
11170 | const LValue &Subobject, |
11171 | APValue *Value, |
11172 | QualType Type) { |
11173 | bool HadZeroInit = Value->hasValue(); |
11174 | |
11175 | if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) { |
11176 | unsigned FinalSize = CAT->getSize().getZExtValue(); |
11177 | |
11178 | // Preserve the array filler if we had prior zero-initialization. |
11179 | APValue Filler = |
11180 | HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller() |
11181 | : APValue(); |
11182 | |
11183 | *Value = APValue(APValue::UninitArray(), 0, FinalSize); |
11184 | if (FinalSize == 0) |
11185 | return true; |
11186 | |
11187 | bool HasTrivialConstructor = CheckTrivialDefaultConstructor( |
11188 | Info, E->getExprLoc(), E->getConstructor(), |
11189 | E->requiresZeroInitialization()); |
11190 | LValue ArrayElt = Subobject; |
11191 | ArrayElt.addArray(Info, E, CAT); |
11192 | // We do the whole initialization in two passes, first for just one element, |
11193 | // then for the whole array. It's possible we may find out we can't do const |
11194 | // init in the first pass, in which case we avoid allocating a potentially |
11195 | // large array. We don't do more passes because expanding array requires |
11196 | // copying the data, which is wasteful. |
11197 | for (const unsigned N : {1u, FinalSize}) { |
11198 | unsigned OldElts = Value->getArrayInitializedElts(); |
11199 | if (OldElts == N) |
11200 | break; |
11201 | |
11202 | // Expand the array to appropriate size. |
11203 | APValue NewValue(APValue::UninitArray(), N, FinalSize); |
11204 | for (unsigned I = 0; I < OldElts; ++I) |
11205 | NewValue.getArrayInitializedElt(I).swap( |
11206 | RHS&: Value->getArrayInitializedElt(I)); |
11207 | Value->swap(RHS&: NewValue); |
11208 | |
11209 | if (HadZeroInit) |
11210 | for (unsigned I = OldElts; I < N; ++I) |
11211 | Value->getArrayInitializedElt(I) = Filler; |
11212 | |
11213 | if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) { |
11214 | // If we have a trivial constructor, only evaluate it once and copy |
11215 | // the result into all the array elements. |
11216 | APValue &FirstResult = Value->getArrayInitializedElt(I: 0); |
11217 | for (unsigned I = OldElts; I < FinalSize; ++I) |
11218 | Value->getArrayInitializedElt(I) = FirstResult; |
11219 | } else { |
11220 | for (unsigned I = OldElts; I < N; ++I) { |
11221 | if (!VisitCXXConstructExpr(E, ArrayElt, |
11222 | &Value->getArrayInitializedElt(I), |
11223 | CAT->getElementType()) || |
11224 | !HandleLValueArrayAdjustment(Info, E, ArrayElt, |
11225 | CAT->getElementType(), 1)) |
11226 | return false; |
11227 | // When checking for const initilization any diagnostic is considered |
11228 | // an error. |
11229 | if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() && |
11230 | !Info.keepEvaluatingAfterFailure()) |
11231 | return false; |
11232 | } |
11233 | } |
11234 | } |
11235 | |
11236 | return true; |
11237 | } |
11238 | |
11239 | if (!Type->isRecordType()) |
11240 | return Error(E); |
11241 | |
11242 | return RecordExprEvaluator(Info, Subobject, *Value) |
11243 | .VisitCXXConstructExpr(E, T: Type); |
11244 | } |
11245 | |
11246 | bool ArrayExprEvaluator::VisitCXXParenListInitExpr( |
11247 | const CXXParenListInitExpr *E) { |
11248 | assert(dyn_cast<ConstantArrayType>(E->getType()) && |
11249 | "Expression result is not a constant array type" ); |
11250 | |
11251 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs(), |
11252 | E->getArrayFiller()); |
11253 | } |
11254 | |
11255 | //===----------------------------------------------------------------------===// |
11256 | // Integer Evaluation |
11257 | // |
11258 | // As a GNU extension, we support casting pointers to sufficiently-wide integer |
11259 | // types and back in constant folding. Integer values are thus represented |
11260 | // either as an integer-valued APValue, or as an lvalue-valued APValue. |
11261 | //===----------------------------------------------------------------------===// |
11262 | |
11263 | namespace { |
11264 | class IntExprEvaluator |
11265 | : public ExprEvaluatorBase<IntExprEvaluator> { |
11266 | APValue &Result; |
11267 | public: |
11268 | IntExprEvaluator(EvalInfo &info, APValue &result) |
11269 | : ExprEvaluatorBaseTy(info), Result(result) {} |
11270 | |
11271 | bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) { |
11272 | assert(E->getType()->isIntegralOrEnumerationType() && |
11273 | "Invalid evaluation result." ); |
11274 | assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() && |
11275 | "Invalid evaluation result." ); |
11276 | assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11277 | "Invalid evaluation result." ); |
11278 | Result = APValue(SI); |
11279 | return true; |
11280 | } |
11281 | bool Success(const llvm::APSInt &SI, const Expr *E) { |
11282 | return Success(SI, E, Result); |
11283 | } |
11284 | |
11285 | bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) { |
11286 | assert(E->getType()->isIntegralOrEnumerationType() && |
11287 | "Invalid evaluation result." ); |
11288 | assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11289 | "Invalid evaluation result." ); |
11290 | Result = APValue(APSInt(I)); |
11291 | Result.getInt().setIsUnsigned( |
11292 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
11293 | return true; |
11294 | } |
11295 | bool Success(const llvm::APInt &I, const Expr *E) { |
11296 | return Success(I, E, Result); |
11297 | } |
11298 | |
11299 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
11300 | assert(E->getType()->isIntegralOrEnumerationType() && |
11301 | "Invalid evaluation result." ); |
11302 | Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType())); |
11303 | return true; |
11304 | } |
11305 | bool Success(uint64_t Value, const Expr *E) { |
11306 | return Success(Value, E, Result); |
11307 | } |
11308 | |
11309 | bool Success(CharUnits Size, const Expr *E) { |
11310 | return Success(Value: Size.getQuantity(), E); |
11311 | } |
11312 | |
11313 | bool Success(const APValue &V, const Expr *E) { |
11314 | if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate()) { |
11315 | Result = V; |
11316 | return true; |
11317 | } |
11318 | return Success(SI: V.getInt(), E); |
11319 | } |
11320 | |
11321 | bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); } |
11322 | |
11323 | //===--------------------------------------------------------------------===// |
11324 | // Visitor Methods |
11325 | //===--------------------------------------------------------------------===// |
11326 | |
11327 | bool VisitIntegerLiteral(const IntegerLiteral *E) { |
11328 | return Success(E->getValue(), E); |
11329 | } |
11330 | bool VisitCharacterLiteral(const CharacterLiteral *E) { |
11331 | return Success(E->getValue(), E); |
11332 | } |
11333 | |
11334 | bool CheckReferencedDecl(const Expr *E, const Decl *D); |
11335 | bool VisitDeclRefExpr(const DeclRefExpr *E) { |
11336 | if (CheckReferencedDecl(E, E->getDecl())) |
11337 | return true; |
11338 | |
11339 | return ExprEvaluatorBaseTy::VisitDeclRefExpr(E); |
11340 | } |
11341 | bool VisitMemberExpr(const MemberExpr *E) { |
11342 | if (CheckReferencedDecl(E, E->getMemberDecl())) { |
11343 | VisitIgnoredBaseExpression(E: E->getBase()); |
11344 | return true; |
11345 | } |
11346 | |
11347 | return ExprEvaluatorBaseTy::VisitMemberExpr(E); |
11348 | } |
11349 | |
11350 | bool VisitCallExpr(const CallExpr *E); |
11351 | bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp); |
11352 | bool VisitBinaryOperator(const BinaryOperator *E); |
11353 | bool VisitOffsetOfExpr(const OffsetOfExpr *E); |
11354 | bool VisitUnaryOperator(const UnaryOperator *E); |
11355 | |
11356 | bool VisitCastExpr(const CastExpr* E); |
11357 | bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E); |
11358 | |
11359 | bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) { |
11360 | return Success(E->getValue(), E); |
11361 | } |
11362 | |
11363 | bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) { |
11364 | return Success(E->getValue(), E); |
11365 | } |
11366 | |
11367 | bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) { |
11368 | if (Info.ArrayInitIndex == uint64_t(-1)) { |
11369 | // We were asked to evaluate this subexpression independent of the |
11370 | // enclosing ArrayInitLoopExpr. We can't do that. |
11371 | Info.FFDiag(E); |
11372 | return false; |
11373 | } |
11374 | return Success(Info.ArrayInitIndex, E); |
11375 | } |
11376 | |
11377 | // Note, GNU defines __null as an integer, not a pointer. |
11378 | bool VisitGNUNullExpr(const GNUNullExpr *E) { |
11379 | return ZeroInitialization(E); |
11380 | } |
11381 | |
11382 | bool VisitTypeTraitExpr(const TypeTraitExpr *E) { |
11383 | return Success(E->getValue(), E); |
11384 | } |
11385 | |
11386 | bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) { |
11387 | return Success(E->getValue(), E); |
11388 | } |
11389 | |
11390 | bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) { |
11391 | return Success(E->getValue(), E); |
11392 | } |
11393 | |
11394 | bool VisitUnaryReal(const UnaryOperator *E); |
11395 | bool VisitUnaryImag(const UnaryOperator *E); |
11396 | |
11397 | bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E); |
11398 | bool VisitSizeOfPackExpr(const SizeOfPackExpr *E); |
11399 | bool VisitSourceLocExpr(const SourceLocExpr *E); |
11400 | bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E); |
11401 | bool VisitRequiresExpr(const RequiresExpr *E); |
11402 | // FIXME: Missing: array subscript of vector, member of vector |
11403 | }; |
11404 | |
11405 | class FixedPointExprEvaluator |
11406 | : public ExprEvaluatorBase<FixedPointExprEvaluator> { |
11407 | APValue &Result; |
11408 | |
11409 | public: |
11410 | FixedPointExprEvaluator(EvalInfo &info, APValue &result) |
11411 | : ExprEvaluatorBaseTy(info), Result(result) {} |
11412 | |
11413 | bool Success(const llvm::APInt &I, const Expr *E) { |
11414 | return Success( |
11415 | V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
11416 | } |
11417 | |
11418 | bool Success(uint64_t Value, const Expr *E) { |
11419 | return Success( |
11420 | V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E); |
11421 | } |
11422 | |
11423 | bool Success(const APValue &V, const Expr *E) { |
11424 | return Success(V: V.getFixedPoint(), E); |
11425 | } |
11426 | |
11427 | bool Success(const APFixedPoint &V, const Expr *E) { |
11428 | assert(E->getType()->isFixedPointType() && "Invalid evaluation result." ); |
11429 | assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) && |
11430 | "Invalid evaluation result." ); |
11431 | Result = APValue(V); |
11432 | return true; |
11433 | } |
11434 | |
11435 | bool ZeroInitialization(const Expr *E) { |
11436 | return Success(Value: 0, E); |
11437 | } |
11438 | |
11439 | //===--------------------------------------------------------------------===// |
11440 | // Visitor Methods |
11441 | //===--------------------------------------------------------------------===// |
11442 | |
11443 | bool VisitFixedPointLiteral(const FixedPointLiteral *E) { |
11444 | return Success(E->getValue(), E); |
11445 | } |
11446 | |
11447 | bool VisitCastExpr(const CastExpr *E); |
11448 | bool VisitUnaryOperator(const UnaryOperator *E); |
11449 | bool VisitBinaryOperator(const BinaryOperator *E); |
11450 | }; |
11451 | } // end anonymous namespace |
11452 | |
11453 | /// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and |
11454 | /// produce either the integer value or a pointer. |
11455 | /// |
11456 | /// GCC has a heinous extension which folds casts between pointer types and |
11457 | /// pointer-sized integral types. We support this by allowing the evaluation of |
11458 | /// an integer rvalue to produce a pointer (represented as an lvalue) instead. |
11459 | /// Some simple arithmetic on such values is supported (they are treated much |
11460 | /// like char*). |
11461 | static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result, |
11462 | EvalInfo &Info) { |
11463 | assert(!E->isValueDependent()); |
11464 | assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType()); |
11465 | return IntExprEvaluator(Info, Result).Visit(E); |
11466 | } |
11467 | |
11468 | static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) { |
11469 | assert(!E->isValueDependent()); |
11470 | APValue Val; |
11471 | if (!EvaluateIntegerOrLValue(E, Result&: Val, Info)) |
11472 | return false; |
11473 | if (!Val.isInt()) { |
11474 | // FIXME: It would be better to produce the diagnostic for casting |
11475 | // a pointer to an integer. |
11476 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
11477 | return false; |
11478 | } |
11479 | Result = Val.getInt(); |
11480 | return true; |
11481 | } |
11482 | |
11483 | bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) { |
11484 | APValue Evaluated = E->EvaluateInContext( |
11485 | Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr()); |
11486 | return Success(Evaluated, E); |
11487 | } |
11488 | |
11489 | static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result, |
11490 | EvalInfo &Info) { |
11491 | assert(!E->isValueDependent()); |
11492 | if (E->getType()->isFixedPointType()) { |
11493 | APValue Val; |
11494 | if (!FixedPointExprEvaluator(Info, Val).Visit(E)) |
11495 | return false; |
11496 | if (!Val.isFixedPoint()) |
11497 | return false; |
11498 | |
11499 | Result = Val.getFixedPoint(); |
11500 | return true; |
11501 | } |
11502 | return false; |
11503 | } |
11504 | |
11505 | static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result, |
11506 | EvalInfo &Info) { |
11507 | assert(!E->isValueDependent()); |
11508 | if (E->getType()->isIntegerType()) { |
11509 | auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
11510 | APSInt Val; |
11511 | if (!EvaluateInteger(E, Result&: Val, Info)) |
11512 | return false; |
11513 | Result = APFixedPoint(Val, FXSema); |
11514 | return true; |
11515 | } else if (E->getType()->isFixedPointType()) { |
11516 | return EvaluateFixedPoint(E, Result, Info); |
11517 | } |
11518 | return false; |
11519 | } |
11520 | |
11521 | /// Check whether the given declaration can be directly converted to an integral |
11522 | /// rvalue. If not, no diagnostic is produced; there are other things we can |
11523 | /// try. |
11524 | bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) { |
11525 | // Enums are integer constant exprs. |
11526 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) { |
11527 | // Check for signedness/width mismatches between E type and ECD value. |
11528 | bool SameSign = (ECD->getInitVal().isSigned() |
11529 | == E->getType()->isSignedIntegerOrEnumerationType()); |
11530 | bool SameWidth = (ECD->getInitVal().getBitWidth() |
11531 | == Info.Ctx.getIntWidth(T: E->getType())); |
11532 | if (SameSign && SameWidth) |
11533 | return Success(SI: ECD->getInitVal(), E); |
11534 | else { |
11535 | // Get rid of mismatch (otherwise Success assertions will fail) |
11536 | // by computing a new value matching the type of E. |
11537 | llvm::APSInt Val = ECD->getInitVal(); |
11538 | if (!SameSign) |
11539 | Val.setIsSigned(!ECD->getInitVal().isSigned()); |
11540 | if (!SameWidth) |
11541 | Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
11542 | return Success(SI: Val, E); |
11543 | } |
11544 | } |
11545 | return false; |
11546 | } |
11547 | |
11548 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
11549 | /// as GCC. |
11550 | GCCTypeClass EvaluateBuiltinClassifyType(QualType T, |
11551 | const LangOptions &LangOpts) { |
11552 | assert(!T->isDependentType() && "unexpected dependent type" ); |
11553 | |
11554 | QualType CanTy = T.getCanonicalType(); |
11555 | |
11556 | switch (CanTy->getTypeClass()) { |
11557 | #define TYPE(ID, BASE) |
11558 | #define DEPENDENT_TYPE(ID, BASE) case Type::ID: |
11559 | #define NON_CANONICAL_TYPE(ID, BASE) case Type::ID: |
11560 | #define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID: |
11561 | #include "clang/AST/TypeNodes.inc" |
11562 | case Type::Auto: |
11563 | case Type::DeducedTemplateSpecialization: |
11564 | llvm_unreachable("unexpected non-canonical or dependent type" ); |
11565 | |
11566 | case Type::Builtin: |
11567 | switch (cast<BuiltinType>(CanTy)->getKind()) { |
11568 | #define BUILTIN_TYPE(ID, SINGLETON_ID) |
11569 | #define SIGNED_TYPE(ID, SINGLETON_ID) \ |
11570 | case BuiltinType::ID: return GCCTypeClass::Integer; |
11571 | #define FLOATING_TYPE(ID, SINGLETON_ID) \ |
11572 | case BuiltinType::ID: return GCCTypeClass::RealFloat; |
11573 | #define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \ |
11574 | case BuiltinType::ID: break; |
11575 | #include "clang/AST/BuiltinTypes.def" |
11576 | case BuiltinType::Void: |
11577 | return GCCTypeClass::Void; |
11578 | |
11579 | case BuiltinType::Bool: |
11580 | return GCCTypeClass::Bool; |
11581 | |
11582 | case BuiltinType::Char_U: |
11583 | case BuiltinType::UChar: |
11584 | case BuiltinType::WChar_U: |
11585 | case BuiltinType::Char8: |
11586 | case BuiltinType::Char16: |
11587 | case BuiltinType::Char32: |
11588 | case BuiltinType::UShort: |
11589 | case BuiltinType::UInt: |
11590 | case BuiltinType::ULong: |
11591 | case BuiltinType::ULongLong: |
11592 | case BuiltinType::UInt128: |
11593 | return GCCTypeClass::Integer; |
11594 | |
11595 | case BuiltinType::UShortAccum: |
11596 | case BuiltinType::UAccum: |
11597 | case BuiltinType::ULongAccum: |
11598 | case BuiltinType::UShortFract: |
11599 | case BuiltinType::UFract: |
11600 | case BuiltinType::ULongFract: |
11601 | case BuiltinType::SatUShortAccum: |
11602 | case BuiltinType::SatUAccum: |
11603 | case BuiltinType::SatULongAccum: |
11604 | case BuiltinType::SatUShortFract: |
11605 | case BuiltinType::SatUFract: |
11606 | case BuiltinType::SatULongFract: |
11607 | return GCCTypeClass::None; |
11608 | |
11609 | case BuiltinType::NullPtr: |
11610 | |
11611 | case BuiltinType::ObjCId: |
11612 | case BuiltinType::ObjCClass: |
11613 | case BuiltinType::ObjCSel: |
11614 | #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ |
11615 | case BuiltinType::Id: |
11616 | #include "clang/Basic/OpenCLImageTypes.def" |
11617 | #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ |
11618 | case BuiltinType::Id: |
11619 | #include "clang/Basic/OpenCLExtensionTypes.def" |
11620 | case BuiltinType::OCLSampler: |
11621 | case BuiltinType::OCLEvent: |
11622 | case BuiltinType::OCLClkEvent: |
11623 | case BuiltinType::OCLQueue: |
11624 | case BuiltinType::OCLReserveID: |
11625 | #define SVE_TYPE(Name, Id, SingletonId) \ |
11626 | case BuiltinType::Id: |
11627 | #include "clang/Basic/AArch64SVEACLETypes.def" |
11628 | #define PPC_VECTOR_TYPE(Name, Id, Size) \ |
11629 | case BuiltinType::Id: |
11630 | #include "clang/Basic/PPCTypes.def" |
11631 | #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11632 | #include "clang/Basic/RISCVVTypes.def" |
11633 | #define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id: |
11634 | #include "clang/Basic/WebAssemblyReferenceTypes.def" |
11635 | return GCCTypeClass::None; |
11636 | |
11637 | case BuiltinType::Dependent: |
11638 | llvm_unreachable("unexpected dependent type" ); |
11639 | }; |
11640 | llvm_unreachable("unexpected placeholder type" ); |
11641 | |
11642 | case Type::Enum: |
11643 | return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer; |
11644 | |
11645 | case Type::Pointer: |
11646 | case Type::ConstantArray: |
11647 | case Type::VariableArray: |
11648 | case Type::IncompleteArray: |
11649 | case Type::FunctionNoProto: |
11650 | case Type::FunctionProto: |
11651 | return GCCTypeClass::Pointer; |
11652 | |
11653 | case Type::MemberPointer: |
11654 | return CanTy->isMemberDataPointerType() |
11655 | ? GCCTypeClass::PointerToDataMember |
11656 | : GCCTypeClass::PointerToMemberFunction; |
11657 | |
11658 | case Type::Complex: |
11659 | return GCCTypeClass::Complex; |
11660 | |
11661 | case Type::Record: |
11662 | return CanTy->isUnionType() ? GCCTypeClass::Union |
11663 | : GCCTypeClass::ClassOrStruct; |
11664 | |
11665 | case Type::Atomic: |
11666 | // GCC classifies _Atomic T the same as T. |
11667 | return EvaluateBuiltinClassifyType( |
11668 | CanTy->castAs<AtomicType>()->getValueType(), LangOpts); |
11669 | |
11670 | case Type::Vector: |
11671 | case Type::ExtVector: |
11672 | return GCCTypeClass::Vector; |
11673 | |
11674 | case Type::BlockPointer: |
11675 | case Type::ConstantMatrix: |
11676 | case Type::ObjCObject: |
11677 | case Type::ObjCInterface: |
11678 | case Type::ObjCObjectPointer: |
11679 | case Type::Pipe: |
11680 | // Classify all other types that don't fit into the regular |
11681 | // classification the same way. |
11682 | return GCCTypeClass::None; |
11683 | |
11684 | case Type::BitInt: |
11685 | return GCCTypeClass::BitInt; |
11686 | |
11687 | case Type::LValueReference: |
11688 | case Type::RValueReference: |
11689 | llvm_unreachable("invalid type for expression" ); |
11690 | } |
11691 | |
11692 | llvm_unreachable("unexpected type class" ); |
11693 | } |
11694 | |
11695 | /// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way |
11696 | /// as GCC. |
11697 | static GCCTypeClass |
11698 | EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) { |
11699 | // If no argument was supplied, default to None. This isn't |
11700 | // ideal, however it is what gcc does. |
11701 | if (E->getNumArgs() == 0) |
11702 | return GCCTypeClass::None; |
11703 | |
11704 | // FIXME: Bizarrely, GCC treats a call with more than one argument as not |
11705 | // being an ICE, but still folds it to a constant using the type of the first |
11706 | // argument. |
11707 | return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts); |
11708 | } |
11709 | |
11710 | /// EvaluateBuiltinConstantPForLValue - Determine the result of |
11711 | /// __builtin_constant_p when applied to the given pointer. |
11712 | /// |
11713 | /// A pointer is only "constant" if it is null (or a pointer cast to integer) |
11714 | /// or it points to the first character of a string literal. |
11715 | static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) { |
11716 | APValue::LValueBase Base = LV.getLValueBase(); |
11717 | if (Base.isNull()) { |
11718 | // A null base is acceptable. |
11719 | return true; |
11720 | } else if (const Expr *E = Base.dyn_cast<const Expr *>()) { |
11721 | if (!isa<StringLiteral>(Val: E)) |
11722 | return false; |
11723 | return LV.getLValueOffset().isZero(); |
11724 | } else if (Base.is<TypeInfoLValue>()) { |
11725 | // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to |
11726 | // evaluate to true. |
11727 | return true; |
11728 | } else { |
11729 | // Any other base is not constant enough for GCC. |
11730 | return false; |
11731 | } |
11732 | } |
11733 | |
11734 | /// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to |
11735 | /// GCC as we can manage. |
11736 | static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) { |
11737 | // This evaluation is not permitted to have side-effects, so evaluate it in |
11738 | // a speculative evaluation context. |
11739 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
11740 | |
11741 | // Constant-folding is always enabled for the operand of __builtin_constant_p |
11742 | // (even when the enclosing evaluation context otherwise requires a strict |
11743 | // language-specific constant expression). |
11744 | FoldConstant Fold(Info, true); |
11745 | |
11746 | QualType ArgType = Arg->getType(); |
11747 | |
11748 | // __builtin_constant_p always has one operand. The rules which gcc follows |
11749 | // are not precisely documented, but are as follows: |
11750 | // |
11751 | // - If the operand is of integral, floating, complex or enumeration type, |
11752 | // and can be folded to a known value of that type, it returns 1. |
11753 | // - If the operand can be folded to a pointer to the first character |
11754 | // of a string literal (or such a pointer cast to an integral type) |
11755 | // or to a null pointer or an integer cast to a pointer, it returns 1. |
11756 | // |
11757 | // Otherwise, it returns 0. |
11758 | // |
11759 | // FIXME: GCC also intends to return 1 for literals of aggregate types, but |
11760 | // its support for this did not work prior to GCC 9 and is not yet well |
11761 | // understood. |
11762 | if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() || |
11763 | ArgType->isAnyComplexType() || ArgType->isPointerType() || |
11764 | ArgType->isNullPtrType()) { |
11765 | APValue V; |
11766 | if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) { |
11767 | Fold.keepDiagnostics(); |
11768 | return false; |
11769 | } |
11770 | |
11771 | // For a pointer (possibly cast to integer), there are special rules. |
11772 | if (V.getKind() == APValue::LValue) |
11773 | return EvaluateBuiltinConstantPForLValue(LV: V); |
11774 | |
11775 | // Otherwise, any constant value is good enough. |
11776 | return V.hasValue(); |
11777 | } |
11778 | |
11779 | // Anything else isn't considered to be sufficiently constant. |
11780 | return false; |
11781 | } |
11782 | |
11783 | /// Retrieves the "underlying object type" of the given expression, |
11784 | /// as used by __builtin_object_size. |
11785 | static QualType getObjectType(APValue::LValueBase B) { |
11786 | if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) { |
11787 | if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D)) |
11788 | return VD->getType(); |
11789 | } else if (const Expr *E = B.dyn_cast<const Expr*>()) { |
11790 | if (isa<CompoundLiteralExpr>(Val: E)) |
11791 | return E->getType(); |
11792 | } else if (B.is<TypeInfoLValue>()) { |
11793 | return B.getTypeInfoType(); |
11794 | } else if (B.is<DynamicAllocLValue>()) { |
11795 | return B.getDynamicAllocType(); |
11796 | } |
11797 | |
11798 | return QualType(); |
11799 | } |
11800 | |
11801 | /// A more selective version of E->IgnoreParenCasts for |
11802 | /// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only |
11803 | /// to change the type of E. |
11804 | /// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo` |
11805 | /// |
11806 | /// Always returns an RValue with a pointer representation. |
11807 | static const Expr *ignorePointerCastsAndParens(const Expr *E) { |
11808 | assert(E->isPRValue() && E->getType()->hasPointerRepresentation()); |
11809 | |
11810 | auto *NoParens = E->IgnoreParens(); |
11811 | auto *Cast = dyn_cast<CastExpr>(Val: NoParens); |
11812 | if (Cast == nullptr) |
11813 | return NoParens; |
11814 | |
11815 | // We only conservatively allow a few kinds of casts, because this code is |
11816 | // inherently a simple solution that seeks to support the common case. |
11817 | auto CastKind = Cast->getCastKind(); |
11818 | if (CastKind != CK_NoOp && CastKind != CK_BitCast && |
11819 | CastKind != CK_AddressSpaceConversion) |
11820 | return NoParens; |
11821 | |
11822 | auto *SubExpr = Cast->getSubExpr(); |
11823 | if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue()) |
11824 | return NoParens; |
11825 | return ignorePointerCastsAndParens(E: SubExpr); |
11826 | } |
11827 | |
11828 | /// Checks to see if the given LValue's Designator is at the end of the LValue's |
11829 | /// record layout. e.g. |
11830 | /// struct { struct { int a, b; } fst, snd; } obj; |
11831 | /// obj.fst // no |
11832 | /// obj.snd // yes |
11833 | /// obj.fst.a // no |
11834 | /// obj.fst.b // no |
11835 | /// obj.snd.a // no |
11836 | /// obj.snd.b // yes |
11837 | /// |
11838 | /// Please note: this function is specialized for how __builtin_object_size |
11839 | /// views "objects". |
11840 | /// |
11841 | /// If this encounters an invalid RecordDecl or otherwise cannot determine the |
11842 | /// correct result, it will always return true. |
11843 | static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) { |
11844 | assert(!LVal.Designator.Invalid); |
11845 | |
11846 | auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD, bool &Invalid) { |
11847 | const RecordDecl *Parent = FD->getParent(); |
11848 | Invalid = Parent->isInvalidDecl(); |
11849 | if (Invalid || Parent->isUnion()) |
11850 | return true; |
11851 | const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent); |
11852 | return FD->getFieldIndex() + 1 == Layout.getFieldCount(); |
11853 | }; |
11854 | |
11855 | auto &Base = LVal.getLValueBase(); |
11856 | if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) { |
11857 | if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) { |
11858 | bool Invalid; |
11859 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11860 | return Invalid; |
11861 | } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) { |
11862 | for (auto *FD : IFD->chain()) { |
11863 | bool Invalid; |
11864 | if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD), Invalid)) |
11865 | return Invalid; |
11866 | } |
11867 | } |
11868 | } |
11869 | |
11870 | unsigned I = 0; |
11871 | QualType BaseType = getType(B: Base); |
11872 | if (LVal.Designator.FirstEntryIsAnUnsizedArray) { |
11873 | // If we don't know the array bound, conservatively assume we're looking at |
11874 | // the final array element. |
11875 | ++I; |
11876 | if (BaseType->isIncompleteArrayType()) |
11877 | BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType(); |
11878 | else |
11879 | BaseType = BaseType->castAs<PointerType>()->getPointeeType(); |
11880 | } |
11881 | |
11882 | for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) { |
11883 | const auto &Entry = LVal.Designator.Entries[I]; |
11884 | if (BaseType->isArrayType()) { |
11885 | // Because __builtin_object_size treats arrays as objects, we can ignore |
11886 | // the index iff this is the last array in the Designator. |
11887 | if (I + 1 == E) |
11888 | return true; |
11889 | const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType)); |
11890 | uint64_t Index = Entry.getAsArrayIndex(); |
11891 | if (Index + 1 != CAT->getSize()) |
11892 | return false; |
11893 | BaseType = CAT->getElementType(); |
11894 | } else if (BaseType->isAnyComplexType()) { |
11895 | const auto *CT = BaseType->castAs<ComplexType>(); |
11896 | uint64_t Index = Entry.getAsArrayIndex(); |
11897 | if (Index != 1) |
11898 | return false; |
11899 | BaseType = CT->getElementType(); |
11900 | } else if (auto *FD = getAsField(Entry)) { |
11901 | bool Invalid; |
11902 | if (!IsLastOrInvalidFieldDecl(FD, Invalid)) |
11903 | return Invalid; |
11904 | BaseType = FD->getType(); |
11905 | } else { |
11906 | assert(getAsBaseClass(Entry) && "Expecting cast to a base class" ); |
11907 | return false; |
11908 | } |
11909 | } |
11910 | return true; |
11911 | } |
11912 | |
11913 | /// Tests to see if the LValue has a user-specified designator (that isn't |
11914 | /// necessarily valid). Note that this always returns 'true' if the LValue has |
11915 | /// an unsized array as its first designator entry, because there's currently no |
11916 | /// way to tell if the user typed *foo or foo[0]. |
11917 | static bool refersToCompleteObject(const LValue &LVal) { |
11918 | if (LVal.Designator.Invalid) |
11919 | return false; |
11920 | |
11921 | if (!LVal.Designator.Entries.empty()) |
11922 | return LVal.Designator.isMostDerivedAnUnsizedArray(); |
11923 | |
11924 | if (!LVal.InvalidBase) |
11925 | return true; |
11926 | |
11927 | // If `E` is a MemberExpr, then the first part of the designator is hiding in |
11928 | // the LValueBase. |
11929 | const auto *E = LVal.Base.dyn_cast<const Expr *>(); |
11930 | return !E || !isa<MemberExpr>(Val: E); |
11931 | } |
11932 | |
11933 | /// Attempts to detect a user writing into a piece of memory that's impossible |
11934 | /// to figure out the size of by just using types. |
11935 | static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) { |
11936 | const SubobjectDesignator &Designator = LVal.Designator; |
11937 | // Notes: |
11938 | // - Users can only write off of the end when we have an invalid base. Invalid |
11939 | // bases imply we don't know where the memory came from. |
11940 | // - We used to be a bit more aggressive here; we'd only be conservative if |
11941 | // the array at the end was flexible, or if it had 0 or 1 elements. This |
11942 | // broke some common standard library extensions (PR30346), but was |
11943 | // otherwise seemingly fine. It may be useful to reintroduce this behavior |
11944 | // with some sort of list. OTOH, it seems that GCC is always |
11945 | // conservative with the last element in structs (if it's an array), so our |
11946 | // current behavior is more compatible than an explicit list approach would |
11947 | // be. |
11948 | auto isFlexibleArrayMember = [&] { |
11949 | using FAMKind = LangOptions::StrictFlexArraysLevelKind; |
11950 | FAMKind StrictFlexArraysLevel = |
11951 | Ctx.getLangOpts().getStrictFlexArraysLevel(); |
11952 | |
11953 | if (Designator.isMostDerivedAnUnsizedArray()) |
11954 | return true; |
11955 | |
11956 | if (StrictFlexArraysLevel == FAMKind::Default) |
11957 | return true; |
11958 | |
11959 | if (Designator.getMostDerivedArraySize() == 0 && |
11960 | StrictFlexArraysLevel != FAMKind::IncompleteOnly) |
11961 | return true; |
11962 | |
11963 | if (Designator.getMostDerivedArraySize() == 1 && |
11964 | StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete) |
11965 | return true; |
11966 | |
11967 | return false; |
11968 | }; |
11969 | |
11970 | return LVal.InvalidBase && |
11971 | Designator.Entries.size() == Designator.MostDerivedPathLength && |
11972 | Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() && |
11973 | isDesignatorAtObjectEnd(Ctx, LVal); |
11974 | } |
11975 | |
11976 | /// Converts the given APInt to CharUnits, assuming the APInt is unsigned. |
11977 | /// Fails if the conversion would cause loss of precision. |
11978 | static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int, |
11979 | CharUnits &Result) { |
11980 | auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max(); |
11981 | if (Int.ugt(RHS: CharUnitsMax)) |
11982 | return false; |
11983 | Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue()); |
11984 | return true; |
11985 | } |
11986 | |
11987 | /// If we're evaluating the object size of an instance of a struct that |
11988 | /// contains a flexible array member, add the size of the initializer. |
11989 | static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T, |
11990 | const LValue &LV, CharUnits &Size) { |
11991 | if (!T.isNull() && T->isStructureType() && |
11992 | T->getAsStructureType()->getDecl()->hasFlexibleArrayMember()) |
11993 | if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>()) |
11994 | if (const auto *VD = dyn_cast<VarDecl>(Val: V)) |
11995 | if (VD->hasInit()) |
11996 | Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx); |
11997 | } |
11998 | |
11999 | /// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will |
12000 | /// determine how many bytes exist from the beginning of the object to either |
12001 | /// the end of the current subobject, or the end of the object itself, depending |
12002 | /// on what the LValue looks like + the value of Type. |
12003 | /// |
12004 | /// If this returns false, the value of Result is undefined. |
12005 | static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc, |
12006 | unsigned Type, const LValue &LVal, |
12007 | CharUnits &EndOffset) { |
12008 | bool DetermineForCompleteObject = refersToCompleteObject(LVal); |
12009 | |
12010 | auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) { |
12011 | if (Ty.isNull() || Ty->isIncompleteType() || Ty->isFunctionType()) |
12012 | return false; |
12013 | return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result); |
12014 | }; |
12015 | |
12016 | // We want to evaluate the size of the entire object. This is a valid fallback |
12017 | // for when Type=1 and the designator is invalid, because we're asked for an |
12018 | // upper-bound. |
12019 | if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) { |
12020 | // Type=3 wants a lower bound, so we can't fall back to this. |
12021 | if (Type == 3 && !DetermineForCompleteObject) |
12022 | return false; |
12023 | |
12024 | llvm::APInt APEndOffset; |
12025 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12026 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12027 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12028 | |
12029 | if (LVal.InvalidBase) |
12030 | return false; |
12031 | |
12032 | QualType BaseTy = getObjectType(B: LVal.getLValueBase()); |
12033 | const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset); |
12034 | addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset); |
12035 | return Ret; |
12036 | } |
12037 | |
12038 | // We want to evaluate the size of a subobject. |
12039 | const SubobjectDesignator &Designator = LVal.Designator; |
12040 | |
12041 | // The following is a moderately common idiom in C: |
12042 | // |
12043 | // struct Foo { int a; char c[1]; }; |
12044 | // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar)); |
12045 | // strcpy(&F->c[0], Bar); |
12046 | // |
12047 | // In order to not break too much legacy code, we need to support it. |
12048 | if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) { |
12049 | // If we can resolve this to an alloc_size call, we can hand that back, |
12050 | // because we know for certain how many bytes there are to write to. |
12051 | llvm::APInt APEndOffset; |
12052 | if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) && |
12053 | getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset)) |
12054 | return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset); |
12055 | |
12056 | // If we cannot determine the size of the initial allocation, then we can't |
12057 | // given an accurate upper-bound. However, we are still able to give |
12058 | // conservative lower-bounds for Type=3. |
12059 | if (Type == 1) |
12060 | return false; |
12061 | } |
12062 | |
12063 | CharUnits BytesPerElem; |
12064 | if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem)) |
12065 | return false; |
12066 | |
12067 | // According to the GCC documentation, we want the size of the subobject |
12068 | // denoted by the pointer. But that's not quite right -- what we actually |
12069 | // want is the size of the immediately-enclosing array, if there is one. |
12070 | int64_t ElemsRemaining; |
12071 | if (Designator.MostDerivedIsArrayElement && |
12072 | Designator.Entries.size() == Designator.MostDerivedPathLength) { |
12073 | uint64_t ArraySize = Designator.getMostDerivedArraySize(); |
12074 | uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex(); |
12075 | ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex; |
12076 | } else { |
12077 | ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1; |
12078 | } |
12079 | |
12080 | EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining; |
12081 | return true; |
12082 | } |
12083 | |
12084 | /// Tries to evaluate the __builtin_object_size for @p E. If successful, |
12085 | /// returns true and stores the result in @p Size. |
12086 | /// |
12087 | /// If @p WasError is non-null, this will report whether the failure to evaluate |
12088 | /// is to be treated as an Error in IntExprEvaluator. |
12089 | static bool tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, |
12090 | EvalInfo &Info, uint64_t &Size) { |
12091 | // Determine the denoted object. |
12092 | LValue LVal; |
12093 | { |
12094 | // The operand of __builtin_object_size is never evaluated for side-effects. |
12095 | // If there are any, but we can determine the pointed-to object anyway, then |
12096 | // ignore the side-effects. |
12097 | SpeculativeEvaluationRAII SpeculativeEval(Info); |
12098 | IgnoreSideEffectsRAII Fold(Info); |
12099 | |
12100 | if (E->isGLValue()) { |
12101 | // It's possible for us to be given GLValues if we're called via |
12102 | // Expr::tryEvaluateObjectSize. |
12103 | APValue RVal; |
12104 | if (!EvaluateAsRValue(Info, E, Result&: RVal)) |
12105 | return false; |
12106 | LVal.setFrom(Ctx&: Info.Ctx, V: RVal); |
12107 | } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info, |
12108 | /*InvalidBaseOK=*/true)) |
12109 | return false; |
12110 | } |
12111 | |
12112 | // If we point to before the start of the object, there are no accessible |
12113 | // bytes. |
12114 | if (LVal.getLValueOffset().isNegative()) { |
12115 | Size = 0; |
12116 | return true; |
12117 | } |
12118 | |
12119 | CharUnits EndOffset; |
12120 | if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset)) |
12121 | return false; |
12122 | |
12123 | // If we've fallen outside of the end offset, just pretend there's nothing to |
12124 | // write to/read from. |
12125 | if (EndOffset <= LVal.getLValueOffset()) |
12126 | Size = 0; |
12127 | else |
12128 | Size = (EndOffset - LVal.getLValueOffset()).getQuantity(); |
12129 | return true; |
12130 | } |
12131 | |
12132 | bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { |
12133 | if (!IsConstantEvaluatedBuiltinCall(E)) |
12134 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
12135 | return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee()); |
12136 | } |
12137 | |
12138 | static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info, |
12139 | APValue &Val, APSInt &Alignment) { |
12140 | QualType SrcTy = E->getArg(Arg: 0)->getType(); |
12141 | if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment)) |
12142 | return false; |
12143 | // Even though we are evaluating integer expressions we could get a pointer |
12144 | // argument for the __builtin_is_aligned() case. |
12145 | if (SrcTy->isPointerType()) { |
12146 | LValue Ptr; |
12147 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info)) |
12148 | return false; |
12149 | Ptr.moveInto(V&: Val); |
12150 | } else if (!SrcTy->isIntegralOrEnumerationType()) { |
12151 | Info.FFDiag(E: E->getArg(Arg: 0)); |
12152 | return false; |
12153 | } else { |
12154 | APSInt SrcInt; |
12155 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info)) |
12156 | return false; |
12157 | assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() && |
12158 | "Bit widths must be the same" ); |
12159 | Val = APValue(SrcInt); |
12160 | } |
12161 | assert(Val.hasValue()); |
12162 | return true; |
12163 | } |
12164 | |
12165 | bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E, |
12166 | unsigned BuiltinOp) { |
12167 | switch (BuiltinOp) { |
12168 | default: |
12169 | return false; |
12170 | |
12171 | case Builtin::BI__builtin_dynamic_object_size: |
12172 | case Builtin::BI__builtin_object_size: { |
12173 | // The type was checked when we built the expression. |
12174 | unsigned Type = |
12175 | E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
12176 | assert(Type <= 3 && "unexpected type" ); |
12177 | |
12178 | uint64_t Size; |
12179 | if (tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, Size)) |
12180 | return Success(Size, E); |
12181 | |
12182 | if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx)) |
12183 | return Success((Type & 2) ? 0 : -1, E); |
12184 | |
12185 | // Expression had no side effects, but we couldn't statically determine the |
12186 | // size of the referenced object. |
12187 | switch (Info.EvalMode) { |
12188 | case EvalInfo::EM_ConstantExpression: |
12189 | case EvalInfo::EM_ConstantFold: |
12190 | case EvalInfo::EM_IgnoreSideEffects: |
12191 | // Leave it to IR generation. |
12192 | return Error(E); |
12193 | case EvalInfo::EM_ConstantExpressionUnevaluated: |
12194 | // Reduce it to a constant now. |
12195 | return Success((Type & 2) ? 0 : -1, E); |
12196 | } |
12197 | |
12198 | llvm_unreachable("unexpected EvalMode" ); |
12199 | } |
12200 | |
12201 | case Builtin::BI__builtin_os_log_format_buffer_size: { |
12202 | analyze_os_log::OSLogBufferLayout Layout; |
12203 | analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout); |
12204 | return Success(Layout.size().getQuantity(), E); |
12205 | } |
12206 | |
12207 | case Builtin::BI__builtin_is_aligned: { |
12208 | APValue Src; |
12209 | APSInt Alignment; |
12210 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12211 | return false; |
12212 | if (Src.isLValue()) { |
12213 | // If we evaluated a pointer, check the minimum known alignment. |
12214 | LValue Ptr; |
12215 | Ptr.setFrom(Ctx&: Info.Ctx, V: Src); |
12216 | CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr); |
12217 | CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset); |
12218 | // We can return true if the known alignment at the computed offset is |
12219 | // greater than the requested alignment. |
12220 | assert(PtrAlign.isPowerOfTwo()); |
12221 | assert(Alignment.isPowerOf2()); |
12222 | if (PtrAlign.getQuantity() >= Alignment) |
12223 | return Success(1, E); |
12224 | // If the alignment is not known to be sufficient, some cases could still |
12225 | // be aligned at run time. However, if the requested alignment is less or |
12226 | // equal to the base alignment and the offset is not aligned, we know that |
12227 | // the run-time value can never be aligned. |
12228 | if (BaseAlignment.getQuantity() >= Alignment && |
12229 | PtrAlign.getQuantity() < Alignment) |
12230 | return Success(0, E); |
12231 | // Otherwise we can't infer whether the value is sufficiently aligned. |
12232 | // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N) |
12233 | // in cases where we can't fully evaluate the pointer. |
12234 | Info.FFDiag(E->getArg(0), diag::note_constexpr_alignment_compute) |
12235 | << Alignment; |
12236 | return false; |
12237 | } |
12238 | assert(Src.isInt()); |
12239 | return Success((Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E); |
12240 | } |
12241 | case Builtin::BI__builtin_align_up: { |
12242 | APValue Src; |
12243 | APSInt Alignment; |
12244 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12245 | return false; |
12246 | if (!Src.isInt()) |
12247 | return Error(E); |
12248 | APSInt AlignedVal = |
12249 | APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1), |
12250 | Src.getInt().isUnsigned()); |
12251 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
12252 | return Success(AlignedVal, E); |
12253 | } |
12254 | case Builtin::BI__builtin_align_down: { |
12255 | APValue Src; |
12256 | APSInt Alignment; |
12257 | if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment)) |
12258 | return false; |
12259 | if (!Src.isInt()) |
12260 | return Error(E); |
12261 | APSInt AlignedVal = |
12262 | APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned()); |
12263 | assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth()); |
12264 | return Success(AlignedVal, E); |
12265 | } |
12266 | |
12267 | case Builtin::BI__builtin_bitreverse8: |
12268 | case Builtin::BI__builtin_bitreverse16: |
12269 | case Builtin::BI__builtin_bitreverse32: |
12270 | case Builtin::BI__builtin_bitreverse64: { |
12271 | APSInt Val; |
12272 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12273 | return false; |
12274 | |
12275 | return Success(Val.reverseBits(), E); |
12276 | } |
12277 | |
12278 | case Builtin::BI__builtin_bswap16: |
12279 | case Builtin::BI__builtin_bswap32: |
12280 | case Builtin::BI__builtin_bswap64: { |
12281 | APSInt Val; |
12282 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12283 | return false; |
12284 | |
12285 | return Success(Val.byteSwap(), E); |
12286 | } |
12287 | |
12288 | case Builtin::BI__builtin_classify_type: |
12289 | return Success((int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E); |
12290 | |
12291 | case Builtin::BI__builtin_clrsb: |
12292 | case Builtin::BI__builtin_clrsbl: |
12293 | case Builtin::BI__builtin_clrsbll: { |
12294 | APSInt Val; |
12295 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12296 | return false; |
12297 | |
12298 | return Success(Val.getBitWidth() - Val.getSignificantBits(), E); |
12299 | } |
12300 | |
12301 | case Builtin::BI__builtin_clz: |
12302 | case Builtin::BI__builtin_clzl: |
12303 | case Builtin::BI__builtin_clzll: |
12304 | case Builtin::BI__builtin_clzs: |
12305 | case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes |
12306 | case Builtin::BI__lzcnt: |
12307 | case Builtin::BI__lzcnt64: { |
12308 | APSInt Val; |
12309 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12310 | return false; |
12311 | |
12312 | // When the argument is 0, the result of GCC builtins is undefined, whereas |
12313 | // for Microsoft intrinsics, the result is the bit-width of the argument. |
12314 | bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 && |
12315 | BuiltinOp != Builtin::BI__lzcnt && |
12316 | BuiltinOp != Builtin::BI__lzcnt64; |
12317 | |
12318 | if (ZeroIsUndefined && !Val) |
12319 | return Error(E); |
12320 | |
12321 | return Success(Val.countl_zero(), E); |
12322 | } |
12323 | |
12324 | case Builtin::BI__builtin_constant_p: { |
12325 | const Expr *Arg = E->getArg(Arg: 0); |
12326 | if (EvaluateBuiltinConstantP(Info, Arg)) |
12327 | return Success(true, E); |
12328 | if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) { |
12329 | // Outside a constant context, eagerly evaluate to false in the presence |
12330 | // of side-effects in order to avoid -Wunsequenced false-positives in |
12331 | // a branch on __builtin_constant_p(expr). |
12332 | return Success(false, E); |
12333 | } |
12334 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12335 | return false; |
12336 | } |
12337 | |
12338 | case Builtin::BI__builtin_is_constant_evaluated: { |
12339 | const auto *Callee = Info.CurrentCall->getCallee(); |
12340 | if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression && |
12341 | (Info.CallStackDepth == 1 || |
12342 | (Info.CallStackDepth == 2 && Callee->isInStdNamespace() && |
12343 | Callee->getIdentifier() && |
12344 | Callee->getIdentifier()->isStr("is_constant_evaluated" )))) { |
12345 | // FIXME: Find a better way to avoid duplicated diagnostics. |
12346 | if (Info.EvalStatus.Diag) |
12347 | Info.report((Info.CallStackDepth == 1) |
12348 | ? E->getExprLoc() |
12349 | : Info.CurrentCall->getCallRange().getBegin(), |
12350 | diag::warn_is_constant_evaluated_always_true_constexpr) |
12351 | << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated" |
12352 | : "std::is_constant_evaluated" ); |
12353 | } |
12354 | |
12355 | return Success(Info.InConstantContext, E); |
12356 | } |
12357 | |
12358 | case Builtin::BI__builtin_ctz: |
12359 | case Builtin::BI__builtin_ctzl: |
12360 | case Builtin::BI__builtin_ctzll: |
12361 | case Builtin::BI__builtin_ctzs: { |
12362 | APSInt Val; |
12363 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12364 | return false; |
12365 | if (!Val) |
12366 | return Error(E); |
12367 | |
12368 | return Success(Val.countr_zero(), E); |
12369 | } |
12370 | |
12371 | case Builtin::BI__builtin_eh_return_data_regno: { |
12372 | int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue(); |
12373 | Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand); |
12374 | return Success(Operand, E); |
12375 | } |
12376 | |
12377 | case Builtin::BI__builtin_expect: |
12378 | case Builtin::BI__builtin_expect_with_probability: |
12379 | return Visit(E->getArg(Arg: 0)); |
12380 | |
12381 | case Builtin::BI__builtin_ffs: |
12382 | case Builtin::BI__builtin_ffsl: |
12383 | case Builtin::BI__builtin_ffsll: { |
12384 | APSInt Val; |
12385 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12386 | return false; |
12387 | |
12388 | unsigned N = Val.countr_zero(); |
12389 | return Success(N == Val.getBitWidth() ? 0 : N + 1, E); |
12390 | } |
12391 | |
12392 | case Builtin::BI__builtin_fpclassify: { |
12393 | APFloat Val(0.0); |
12394 | if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info)) |
12395 | return false; |
12396 | unsigned Arg; |
12397 | switch (Val.getCategory()) { |
12398 | case APFloat::fcNaN: Arg = 0; break; |
12399 | case APFloat::fcInfinity: Arg = 1; break; |
12400 | case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break; |
12401 | case APFloat::fcZero: Arg = 4; break; |
12402 | } |
12403 | return Visit(E->getArg(Arg)); |
12404 | } |
12405 | |
12406 | case Builtin::BI__builtin_isinf_sign: { |
12407 | APFloat Val(0.0); |
12408 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12409 | Success(Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E); |
12410 | } |
12411 | |
12412 | case Builtin::BI__builtin_isinf: { |
12413 | APFloat Val(0.0); |
12414 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12415 | Success(Val.isInfinity() ? 1 : 0, E); |
12416 | } |
12417 | |
12418 | case Builtin::BI__builtin_isfinite: { |
12419 | APFloat Val(0.0); |
12420 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12421 | Success(Val.isFinite() ? 1 : 0, E); |
12422 | } |
12423 | |
12424 | case Builtin::BI__builtin_isnan: { |
12425 | APFloat Val(0.0); |
12426 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12427 | Success(Val.isNaN() ? 1 : 0, E); |
12428 | } |
12429 | |
12430 | case Builtin::BI__builtin_isnormal: { |
12431 | APFloat Val(0.0); |
12432 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12433 | Success(Val.isNormal() ? 1 : 0, E); |
12434 | } |
12435 | |
12436 | case Builtin::BI__builtin_issubnormal: { |
12437 | APFloat Val(0.0); |
12438 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12439 | Success(Val.isDenormal() ? 1 : 0, E); |
12440 | } |
12441 | |
12442 | case Builtin::BI__builtin_iszero: { |
12443 | APFloat Val(0.0); |
12444 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12445 | Success(Val.isZero() ? 1 : 0, E); |
12446 | } |
12447 | |
12448 | case Builtin::BI__builtin_issignaling: { |
12449 | APFloat Val(0.0); |
12450 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12451 | Success(Val.isSignaling() ? 1 : 0, E); |
12452 | } |
12453 | |
12454 | case Builtin::BI__builtin_isfpclass: { |
12455 | APSInt MaskVal; |
12456 | if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info)) |
12457 | return false; |
12458 | unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue()); |
12459 | APFloat Val(0.0); |
12460 | return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) && |
12461 | Success((Val.classify() & Test) ? 1 : 0, E); |
12462 | } |
12463 | |
12464 | case Builtin::BI__builtin_parity: |
12465 | case Builtin::BI__builtin_parityl: |
12466 | case Builtin::BI__builtin_parityll: { |
12467 | APSInt Val; |
12468 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12469 | return false; |
12470 | |
12471 | return Success(Val.popcount() % 2, E); |
12472 | } |
12473 | |
12474 | case Builtin::BI__builtin_popcount: |
12475 | case Builtin::BI__builtin_popcountl: |
12476 | case Builtin::BI__builtin_popcountll: |
12477 | case Builtin::BI__popcnt16: // Microsoft variants of popcount |
12478 | case Builtin::BI__popcnt: |
12479 | case Builtin::BI__popcnt64: { |
12480 | APSInt Val; |
12481 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) |
12482 | return false; |
12483 | |
12484 | return Success(Val.popcount(), E); |
12485 | } |
12486 | |
12487 | case Builtin::BI__builtin_rotateleft8: |
12488 | case Builtin::BI__builtin_rotateleft16: |
12489 | case Builtin::BI__builtin_rotateleft32: |
12490 | case Builtin::BI__builtin_rotateleft64: |
12491 | case Builtin::BI_rotl8: // Microsoft variants of rotate right |
12492 | case Builtin::BI_rotl16: |
12493 | case Builtin::BI_rotl: |
12494 | case Builtin::BI_lrotl: |
12495 | case Builtin::BI_rotl64: { |
12496 | APSInt Val, Amt; |
12497 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
12498 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
12499 | return false; |
12500 | |
12501 | return Success(Val.rotl(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
12502 | } |
12503 | |
12504 | case Builtin::BI__builtin_rotateright8: |
12505 | case Builtin::BI__builtin_rotateright16: |
12506 | case Builtin::BI__builtin_rotateright32: |
12507 | case Builtin::BI__builtin_rotateright64: |
12508 | case Builtin::BI_rotr8: // Microsoft variants of rotate right |
12509 | case Builtin::BI_rotr16: |
12510 | case Builtin::BI_rotr: |
12511 | case Builtin::BI_lrotr: |
12512 | case Builtin::BI_rotr64: { |
12513 | APSInt Val, Amt; |
12514 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) || |
12515 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amt, Info)) |
12516 | return false; |
12517 | |
12518 | return Success(Val.rotr(rotateAmt: Amt.urem(RHS: Val.getBitWidth())), E); |
12519 | } |
12520 | |
12521 | case Builtin::BIstrlen: |
12522 | case Builtin::BIwcslen: |
12523 | // A call to strlen is not a constant expression. |
12524 | if (Info.getLangOpts().CPlusPlus11) |
12525 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
12526 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
12527 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
12528 | else |
12529 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12530 | [[fallthrough]]; |
12531 | case Builtin::BI__builtin_strlen: |
12532 | case Builtin::BI__builtin_wcslen: { |
12533 | // As an extension, we support __builtin_strlen() as a constant expression, |
12534 | // and support folding strlen() to a constant. |
12535 | uint64_t StrLen; |
12536 | if (EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Result&: StrLen, Info)) |
12537 | return Success(StrLen, E); |
12538 | return false; |
12539 | } |
12540 | |
12541 | case Builtin::BIstrcmp: |
12542 | case Builtin::BIwcscmp: |
12543 | case Builtin::BIstrncmp: |
12544 | case Builtin::BIwcsncmp: |
12545 | case Builtin::BImemcmp: |
12546 | case Builtin::BIbcmp: |
12547 | case Builtin::BIwmemcmp: |
12548 | // A call to strlen is not a constant expression. |
12549 | if (Info.getLangOpts().CPlusPlus11) |
12550 | Info.CCEDiag(E, diag::note_constexpr_invalid_function) |
12551 | << /*isConstexpr*/ 0 << /*isConstructor*/ 0 |
12552 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str(); |
12553 | else |
12554 | Info.CCEDiag(E, diag::note_invalid_subexpr_in_const_expr); |
12555 | [[fallthrough]]; |
12556 | case Builtin::BI__builtin_strcmp: |
12557 | case Builtin::BI__builtin_wcscmp: |
12558 | case Builtin::BI__builtin_strncmp: |
12559 | case Builtin::BI__builtin_wcsncmp: |
12560 | case Builtin::BI__builtin_memcmp: |
12561 | case Builtin::BI__builtin_bcmp: |
12562 | case Builtin::BI__builtin_wmemcmp: { |
12563 | LValue String1, String2; |
12564 | if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) || |
12565 | !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info)) |
12566 | return false; |
12567 | |
12568 | uint64_t MaxLength = uint64_t(-1); |
12569 | if (BuiltinOp != Builtin::BIstrcmp && |
12570 | BuiltinOp != Builtin::BIwcscmp && |
12571 | BuiltinOp != Builtin::BI__builtin_strcmp && |
12572 | BuiltinOp != Builtin::BI__builtin_wcscmp) { |
12573 | APSInt N; |
12574 | if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info)) |
12575 | return false; |
12576 | MaxLength = N.getZExtValue(); |
12577 | } |
12578 | |
12579 | // Empty substrings compare equal by definition. |
12580 | if (MaxLength == 0u) |
12581 | return Success(0, E); |
12582 | |
12583 | if (!String1.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
12584 | !String2.checkNullPointerForFoldAccess(Info, E, AK_Read) || |
12585 | String1.Designator.Invalid || String2.Designator.Invalid) |
12586 | return false; |
12587 | |
12588 | QualType CharTy1 = String1.Designator.getType(Info.Ctx); |
12589 | QualType CharTy2 = String2.Designator.getType(Info.Ctx); |
12590 | |
12591 | bool IsRawByte = BuiltinOp == Builtin::BImemcmp || |
12592 | BuiltinOp == Builtin::BIbcmp || |
12593 | BuiltinOp == Builtin::BI__builtin_memcmp || |
12594 | BuiltinOp == Builtin::BI__builtin_bcmp; |
12595 | |
12596 | assert(IsRawByte || |
12597 | (Info.Ctx.hasSameUnqualifiedType( |
12598 | CharTy1, E->getArg(0)->getType()->getPointeeType()) && |
12599 | Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2))); |
12600 | |
12601 | // For memcmp, allow comparing any arrays of '[[un]signed] char' or |
12602 | // 'char8_t', but no other types. |
12603 | if (IsRawByte && |
12604 | !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) { |
12605 | // FIXME: Consider using our bit_cast implementation to support this. |
12606 | Info.FFDiag(E, diag::note_constexpr_memcmp_unsupported) |
12607 | << ("'" + Info.Ctx.BuiltinInfo.getName(BuiltinOp) + "'" ).str() |
12608 | << CharTy1 << CharTy2; |
12609 | return false; |
12610 | } |
12611 | |
12612 | const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) { |
12613 | return handleLValueToRValueConversion(Info, E, CharTy1, String1, Char1) && |
12614 | handleLValueToRValueConversion(Info, E, CharTy2, String2, Char2) && |
12615 | Char1.isInt() && Char2.isInt(); |
12616 | }; |
12617 | const auto &AdvanceElems = [&] { |
12618 | return HandleLValueArrayAdjustment(Info, E, String1, CharTy1, 1) && |
12619 | HandleLValueArrayAdjustment(Info, E, String2, CharTy2, 1); |
12620 | }; |
12621 | |
12622 | bool StopAtNull = |
12623 | (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp && |
12624 | BuiltinOp != Builtin::BIwmemcmp && |
12625 | BuiltinOp != Builtin::BI__builtin_memcmp && |
12626 | BuiltinOp != Builtin::BI__builtin_bcmp && |
12627 | BuiltinOp != Builtin::BI__builtin_wmemcmp); |
12628 | bool IsWide = BuiltinOp == Builtin::BIwcscmp || |
12629 | BuiltinOp == Builtin::BIwcsncmp || |
12630 | BuiltinOp == Builtin::BIwmemcmp || |
12631 | BuiltinOp == Builtin::BI__builtin_wcscmp || |
12632 | BuiltinOp == Builtin::BI__builtin_wcsncmp || |
12633 | BuiltinOp == Builtin::BI__builtin_wmemcmp; |
12634 | |
12635 | for (; MaxLength; --MaxLength) { |
12636 | APValue Char1, Char2; |
12637 | if (!ReadCurElems(Char1, Char2)) |
12638 | return false; |
12639 | if (Char1.getInt().ne(RHS: Char2.getInt())) { |
12640 | if (IsWide) // wmemcmp compares with wchar_t signedness. |
12641 | return Success(Char1.getInt() < Char2.getInt() ? -1 : 1, E); |
12642 | // memcmp always compares unsigned chars. |
12643 | return Success(Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E); |
12644 | } |
12645 | if (StopAtNull && !Char1.getInt()) |
12646 | return Success(0, E); |
12647 | assert(!(StopAtNull && !Char2.getInt())); |
12648 | if (!AdvanceElems()) |
12649 | return false; |
12650 | } |
12651 | // We hit the strncmp / memcmp limit. |
12652 | return Success(0, E); |
12653 | } |
12654 | |
12655 | case Builtin::BI__atomic_always_lock_free: |
12656 | case Builtin::BI__atomic_is_lock_free: |
12657 | case Builtin::BI__c11_atomic_is_lock_free: { |
12658 | APSInt SizeVal; |
12659 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info)) |
12660 | return false; |
12661 | |
12662 | // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power |
12663 | // of two less than or equal to the maximum inline atomic width, we know it |
12664 | // is lock-free. If the size isn't a power of two, or greater than the |
12665 | // maximum alignment where we promote atomics, we know it is not lock-free |
12666 | // (at least not in the sense of atomic_is_lock_free). Otherwise, |
12667 | // the answer can only be determined at runtime; for example, 16-byte |
12668 | // atomics have lock-free implementations on some, but not all, |
12669 | // x86-64 processors. |
12670 | |
12671 | // Check power-of-two. |
12672 | CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue()); |
12673 | if (Size.isPowerOfTwo()) { |
12674 | // Check against inlining width. |
12675 | unsigned InlineWidthBits = |
12676 | Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth(); |
12677 | if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) { |
12678 | if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free || |
12679 | Size == CharUnits::One() || |
12680 | E->getArg(1)->isNullPointerConstant(Info.Ctx, |
12681 | Expr::NPC_NeverValueDependent)) |
12682 | // OK, we will inline appropriately-aligned operations of this size, |
12683 | // and _Atomic(T) is appropriately-aligned. |
12684 | return Success(1, E); |
12685 | |
12686 | QualType PointeeType = E->getArg(Arg: 1)->IgnoreImpCasts()->getType()-> |
12687 | castAs<PointerType>()->getPointeeType(); |
12688 | if (!PointeeType->isIncompleteType() && |
12689 | Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) { |
12690 | // OK, we will inline operations on this object. |
12691 | return Success(1, E); |
12692 | } |
12693 | } |
12694 | } |
12695 | |
12696 | return BuiltinOp == Builtin::BI__atomic_always_lock_free ? |
12697 | Success(0, E) : Error(E); |
12698 | } |
12699 | case Builtin::BI__builtin_add_overflow: |
12700 | case Builtin::BI__builtin_sub_overflow: |
12701 | case Builtin::BI__builtin_mul_overflow: |
12702 | case Builtin::BI__builtin_sadd_overflow: |
12703 | case Builtin::BI__builtin_uadd_overflow: |
12704 | case Builtin::BI__builtin_uaddl_overflow: |
12705 | case Builtin::BI__builtin_uaddll_overflow: |
12706 | case Builtin::BI__builtin_usub_overflow: |
12707 | case Builtin::BI__builtin_usubl_overflow: |
12708 | case Builtin::BI__builtin_usubll_overflow: |
12709 | case Builtin::BI__builtin_umul_overflow: |
12710 | case Builtin::BI__builtin_umull_overflow: |
12711 | case Builtin::BI__builtin_umulll_overflow: |
12712 | case Builtin::BI__builtin_saddl_overflow: |
12713 | case Builtin::BI__builtin_saddll_overflow: |
12714 | case Builtin::BI__builtin_ssub_overflow: |
12715 | case Builtin::BI__builtin_ssubl_overflow: |
12716 | case Builtin::BI__builtin_ssubll_overflow: |
12717 | case Builtin::BI__builtin_smul_overflow: |
12718 | case Builtin::BI__builtin_smull_overflow: |
12719 | case Builtin::BI__builtin_smulll_overflow: { |
12720 | LValue ResultLValue; |
12721 | APSInt LHS, RHS; |
12722 | |
12723 | QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType(); |
12724 | if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) || |
12725 | !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) || |
12726 | !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info)) |
12727 | return false; |
12728 | |
12729 | APSInt Result; |
12730 | bool DidOverflow = false; |
12731 | |
12732 | // If the types don't have to match, enlarge all 3 to the largest of them. |
12733 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12734 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12735 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12736 | bool IsSigned = LHS.isSigned() || RHS.isSigned() || |
12737 | ResultType->isSignedIntegerOrEnumerationType(); |
12738 | bool AllSigned = LHS.isSigned() && RHS.isSigned() && |
12739 | ResultType->isSignedIntegerOrEnumerationType(); |
12740 | uint64_t LHSSize = LHS.getBitWidth(); |
12741 | uint64_t RHSSize = RHS.getBitWidth(); |
12742 | uint64_t ResultSize = Info.Ctx.getTypeSize(T: ResultType); |
12743 | uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize); |
12744 | |
12745 | // Add an additional bit if the signedness isn't uniformly agreed to. We |
12746 | // could do this ONLY if there is a signed and an unsigned that both have |
12747 | // MaxBits, but the code to check that is pretty nasty. The issue will be |
12748 | // caught in the shrink-to-result later anyway. |
12749 | if (IsSigned && !AllSigned) |
12750 | ++MaxBits; |
12751 | |
12752 | LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned); |
12753 | RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned); |
12754 | Result = APSInt(MaxBits, !IsSigned); |
12755 | } |
12756 | |
12757 | // Find largest int. |
12758 | switch (BuiltinOp) { |
12759 | default: |
12760 | llvm_unreachable("Invalid value for BuiltinOp" ); |
12761 | case Builtin::BI__builtin_add_overflow: |
12762 | case Builtin::BI__builtin_sadd_overflow: |
12763 | case Builtin::BI__builtin_saddl_overflow: |
12764 | case Builtin::BI__builtin_saddll_overflow: |
12765 | case Builtin::BI__builtin_uadd_overflow: |
12766 | case Builtin::BI__builtin_uaddl_overflow: |
12767 | case Builtin::BI__builtin_uaddll_overflow: |
12768 | Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow) |
12769 | : LHS.uadd_ov(RHS, Overflow&: DidOverflow); |
12770 | break; |
12771 | case Builtin::BI__builtin_sub_overflow: |
12772 | case Builtin::BI__builtin_ssub_overflow: |
12773 | case Builtin::BI__builtin_ssubl_overflow: |
12774 | case Builtin::BI__builtin_ssubll_overflow: |
12775 | case Builtin::BI__builtin_usub_overflow: |
12776 | case Builtin::BI__builtin_usubl_overflow: |
12777 | case Builtin::BI__builtin_usubll_overflow: |
12778 | Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow) |
12779 | : LHS.usub_ov(RHS, Overflow&: DidOverflow); |
12780 | break; |
12781 | case Builtin::BI__builtin_mul_overflow: |
12782 | case Builtin::BI__builtin_smul_overflow: |
12783 | case Builtin::BI__builtin_smull_overflow: |
12784 | case Builtin::BI__builtin_smulll_overflow: |
12785 | case Builtin::BI__builtin_umul_overflow: |
12786 | case Builtin::BI__builtin_umull_overflow: |
12787 | case Builtin::BI__builtin_umulll_overflow: |
12788 | Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow) |
12789 | : LHS.umul_ov(RHS, Overflow&: DidOverflow); |
12790 | break; |
12791 | } |
12792 | |
12793 | // In the case where multiple sizes are allowed, truncate and see if |
12794 | // the values are the same. |
12795 | if (BuiltinOp == Builtin::BI__builtin_add_overflow || |
12796 | BuiltinOp == Builtin::BI__builtin_sub_overflow || |
12797 | BuiltinOp == Builtin::BI__builtin_mul_overflow) { |
12798 | // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead, |
12799 | // since it will give us the behavior of a TruncOrSelf in the case where |
12800 | // its parameter <= its size. We previously set Result to be at least the |
12801 | // type-size of the result, so getTypeSize(ResultType) <= Result.BitWidth |
12802 | // will work exactly like TruncOrSelf. |
12803 | APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getTypeSize(T: ResultType)); |
12804 | Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType()); |
12805 | |
12806 | if (!APSInt::isSameValue(I1: Temp, I2: Result)) |
12807 | DidOverflow = true; |
12808 | Result = Temp; |
12809 | } |
12810 | |
12811 | APValue APV{Result}; |
12812 | if (!handleAssignment(Info, E, ResultLValue, ResultType, APV)) |
12813 | return false; |
12814 | return Success(DidOverflow, E); |
12815 | } |
12816 | } |
12817 | } |
12818 | |
12819 | /// Determine whether this is a pointer past the end of the complete |
12820 | /// object referred to by the lvalue. |
12821 | static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx, |
12822 | const LValue &LV) { |
12823 | // A null pointer can be viewed as being "past the end" but we don't |
12824 | // choose to look at it that way here. |
12825 | if (!LV.getLValueBase()) |
12826 | return false; |
12827 | |
12828 | // If the designator is valid and refers to a subobject, we're not pointing |
12829 | // past the end. |
12830 | if (!LV.getLValueDesignator().Invalid && |
12831 | !LV.getLValueDesignator().isOnePastTheEnd()) |
12832 | return false; |
12833 | |
12834 | // A pointer to an incomplete type might be past-the-end if the type's size is |
12835 | // zero. We cannot tell because the type is incomplete. |
12836 | QualType Ty = getType(B: LV.getLValueBase()); |
12837 | if (Ty->isIncompleteType()) |
12838 | return true; |
12839 | |
12840 | // We're a past-the-end pointer if we point to the byte after the object, |
12841 | // no matter what our type or path is. |
12842 | auto Size = Ctx.getTypeSizeInChars(T: Ty); |
12843 | return LV.getLValueOffset() == Size; |
12844 | } |
12845 | |
12846 | namespace { |
12847 | |
12848 | /// Data recursive integer evaluator of certain binary operators. |
12849 | /// |
12850 | /// We use a data recursive algorithm for binary operators so that we are able |
12851 | /// to handle extreme cases of chained binary operators without causing stack |
12852 | /// overflow. |
12853 | class DataRecursiveIntBinOpEvaluator { |
12854 | struct EvalResult { |
12855 | APValue Val; |
12856 | bool Failed = false; |
12857 | |
12858 | EvalResult() = default; |
12859 | |
12860 | void swap(EvalResult &RHS) { |
12861 | Val.swap(RHS&: RHS.Val); |
12862 | Failed = RHS.Failed; |
12863 | RHS.Failed = false; |
12864 | } |
12865 | }; |
12866 | |
12867 | struct Job { |
12868 | const Expr *E; |
12869 | EvalResult LHSResult; // meaningful only for binary operator expression. |
12870 | enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind; |
12871 | |
12872 | Job() = default; |
12873 | Job(Job &&) = default; |
12874 | |
12875 | void startSpeculativeEval(EvalInfo &Info) { |
12876 | SpecEvalRAII = SpeculativeEvaluationRAII(Info); |
12877 | } |
12878 | |
12879 | private: |
12880 | SpeculativeEvaluationRAII SpecEvalRAII; |
12881 | }; |
12882 | |
12883 | SmallVector<Job, 16> Queue; |
12884 | |
12885 | IntExprEvaluator &IntEval; |
12886 | EvalInfo &Info; |
12887 | APValue &FinalResult; |
12888 | |
12889 | public: |
12890 | DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result) |
12891 | : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { } |
12892 | |
12893 | /// True if \param E is a binary operator that we are going to handle |
12894 | /// data recursively. |
12895 | /// We handle binary operators that are comma, logical, or that have operands |
12896 | /// with integral or enumeration type. |
12897 | static bool shouldEnqueue(const BinaryOperator *E) { |
12898 | return E->getOpcode() == BO_Comma || E->isLogicalOp() || |
12899 | (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() && |
12900 | E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12901 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12902 | } |
12903 | |
12904 | bool Traverse(const BinaryOperator *E) { |
12905 | enqueue(E); |
12906 | EvalResult PrevResult; |
12907 | while (!Queue.empty()) |
12908 | process(Result&: PrevResult); |
12909 | |
12910 | if (PrevResult.Failed) return false; |
12911 | |
12912 | FinalResult.swap(RHS&: PrevResult.Val); |
12913 | return true; |
12914 | } |
12915 | |
12916 | private: |
12917 | bool Success(uint64_t Value, const Expr *E, APValue &Result) { |
12918 | return IntEval.Success(Value, E, Result); |
12919 | } |
12920 | bool Success(const APSInt &Value, const Expr *E, APValue &Result) { |
12921 | return IntEval.Success(SI: Value, E, Result); |
12922 | } |
12923 | bool Error(const Expr *E) { |
12924 | return IntEval.Error(E); |
12925 | } |
12926 | bool Error(const Expr *E, diag::kind D) { |
12927 | return IntEval.Error(E, D); |
12928 | } |
12929 | |
12930 | OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) { |
12931 | return Info.CCEDiag(E, DiagId: D); |
12932 | } |
12933 | |
12934 | // Returns true if visiting the RHS is necessary, false otherwise. |
12935 | bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12936 | bool &SuppressRHSDiags); |
12937 | |
12938 | bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
12939 | const BinaryOperator *E, APValue &Result); |
12940 | |
12941 | void EvaluateExpr(const Expr *E, EvalResult &Result) { |
12942 | Result.Failed = !Evaluate(Result&: Result.Val, Info, E); |
12943 | if (Result.Failed) |
12944 | Result.Val = APValue(); |
12945 | } |
12946 | |
12947 | void process(EvalResult &Result); |
12948 | |
12949 | void enqueue(const Expr *E) { |
12950 | E = E->IgnoreParens(); |
12951 | Queue.resize(N: Queue.size()+1); |
12952 | Queue.back().E = E; |
12953 | Queue.back().Kind = Job::AnyExprKind; |
12954 | } |
12955 | }; |
12956 | |
12957 | } |
12958 | |
12959 | bool DataRecursiveIntBinOpEvaluator:: |
12960 | VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E, |
12961 | bool &SuppressRHSDiags) { |
12962 | if (E->getOpcode() == BO_Comma) { |
12963 | // Ignore LHS but note if we could not evaluate it. |
12964 | if (LHSResult.Failed) |
12965 | return Info.noteSideEffect(); |
12966 | return true; |
12967 | } |
12968 | |
12969 | if (E->isLogicalOp()) { |
12970 | bool LHSAsBool; |
12971 | if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) { |
12972 | // We were able to evaluate the LHS, see if we can get away with not |
12973 | // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 |
12974 | if (LHSAsBool == (E->getOpcode() == BO_LOr)) { |
12975 | Success(LHSAsBool, E, LHSResult.Val); |
12976 | return false; // Ignore RHS |
12977 | } |
12978 | } else { |
12979 | LHSResult.Failed = true; |
12980 | |
12981 | // Since we weren't able to evaluate the left hand side, it |
12982 | // might have had side effects. |
12983 | if (!Info.noteSideEffect()) |
12984 | return false; |
12985 | |
12986 | // We can't evaluate the LHS; however, sometimes the result |
12987 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
12988 | // Don't ignore RHS and suppress diagnostics from this arm. |
12989 | SuppressRHSDiags = true; |
12990 | } |
12991 | |
12992 | return true; |
12993 | } |
12994 | |
12995 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
12996 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
12997 | |
12998 | if (LHSResult.Failed && !Info.noteFailure()) |
12999 | return false; // Ignore RHS; |
13000 | |
13001 | return true; |
13002 | } |
13003 | |
13004 | static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index, |
13005 | bool IsSub) { |
13006 | // Compute the new offset in the appropriate width, wrapping at 64 bits. |
13007 | // FIXME: When compiling for a 32-bit target, we should use 32-bit |
13008 | // offsets. |
13009 | assert(!LVal.hasLValuePath() && "have designator for integer lvalue" ); |
13010 | CharUnits &Offset = LVal.getLValueOffset(); |
13011 | uint64_t Offset64 = Offset.getQuantity(); |
13012 | uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue(); |
13013 | Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64 |
13014 | : Offset64 + Index64); |
13015 | } |
13016 | |
13017 | bool DataRecursiveIntBinOpEvaluator:: |
13018 | VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult, |
13019 | const BinaryOperator *E, APValue &Result) { |
13020 | if (E->getOpcode() == BO_Comma) { |
13021 | if (RHSResult.Failed) |
13022 | return false; |
13023 | Result = RHSResult.Val; |
13024 | return true; |
13025 | } |
13026 | |
13027 | if (E->isLogicalOp()) { |
13028 | bool lhsResult, rhsResult; |
13029 | bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult); |
13030 | bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult); |
13031 | |
13032 | if (LHSIsOK) { |
13033 | if (RHSIsOK) { |
13034 | if (E->getOpcode() == BO_LOr) |
13035 | return Success(lhsResult || rhsResult, E, Result); |
13036 | else |
13037 | return Success(lhsResult && rhsResult, E, Result); |
13038 | } |
13039 | } else { |
13040 | if (RHSIsOK) { |
13041 | // We can't evaluate the LHS; however, sometimes the result |
13042 | // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. |
13043 | if (rhsResult == (E->getOpcode() == BO_LOr)) |
13044 | return Success(rhsResult, E, Result); |
13045 | } |
13046 | } |
13047 | |
13048 | return false; |
13049 | } |
13050 | |
13051 | assert(E->getLHS()->getType()->isIntegralOrEnumerationType() && |
13052 | E->getRHS()->getType()->isIntegralOrEnumerationType()); |
13053 | |
13054 | if (LHSResult.Failed || RHSResult.Failed) |
13055 | return false; |
13056 | |
13057 | const APValue &LHSVal = LHSResult.Val; |
13058 | const APValue &RHSVal = RHSResult.Val; |
13059 | |
13060 | // Handle cases like (unsigned long)&a + 4. |
13061 | if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) { |
13062 | Result = LHSVal; |
13063 | addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub); |
13064 | return true; |
13065 | } |
13066 | |
13067 | // Handle cases like 4 + (unsigned long)&a |
13068 | if (E->getOpcode() == BO_Add && |
13069 | RHSVal.isLValue() && LHSVal.isInt()) { |
13070 | Result = RHSVal; |
13071 | addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false); |
13072 | return true; |
13073 | } |
13074 | |
13075 | if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) { |
13076 | // Handle (intptr_t)&&A - (intptr_t)&&B. |
13077 | if (!LHSVal.getLValueOffset().isZero() || |
13078 | !RHSVal.getLValueOffset().isZero()) |
13079 | return false; |
13080 | const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>(); |
13081 | const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>(); |
13082 | if (!LHSExpr || !RHSExpr) |
13083 | return false; |
13084 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
13085 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
13086 | if (!LHSAddrExpr || !RHSAddrExpr) |
13087 | return false; |
13088 | // Make sure both labels come from the same function. |
13089 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
13090 | RHSAddrExpr->getLabel()->getDeclContext()) |
13091 | return false; |
13092 | Result = APValue(LHSAddrExpr, RHSAddrExpr); |
13093 | return true; |
13094 | } |
13095 | |
13096 | // All the remaining cases expect both operands to be an integer |
13097 | if (!LHSVal.isInt() || !RHSVal.isInt()) |
13098 | return Error(E); |
13099 | |
13100 | // Set up the width and signedness manually, in case it can't be deduced |
13101 | // from the operation we're performing. |
13102 | // FIXME: Don't do this in the cases where we can deduce it. |
13103 | APSInt Value(Info.Ctx.getIntWidth(T: E->getType()), |
13104 | E->getType()->isUnsignedIntegerOrEnumerationType()); |
13105 | if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(), |
13106 | RHS: RHSVal.getInt(), Result&: Value)) |
13107 | return false; |
13108 | return Success(Value, E, Result); |
13109 | } |
13110 | |
13111 | void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) { |
13112 | Job &job = Queue.back(); |
13113 | |
13114 | switch (job.Kind) { |
13115 | case Job::AnyExprKind: { |
13116 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) { |
13117 | if (shouldEnqueue(E: Bop)) { |
13118 | job.Kind = Job::BinOpKind; |
13119 | enqueue(E: Bop->getLHS()); |
13120 | return; |
13121 | } |
13122 | } |
13123 | |
13124 | EvaluateExpr(E: job.E, Result); |
13125 | Queue.pop_back(); |
13126 | return; |
13127 | } |
13128 | |
13129 | case Job::BinOpKind: { |
13130 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
13131 | bool SuppressRHSDiags = false; |
13132 | if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) { |
13133 | Queue.pop_back(); |
13134 | return; |
13135 | } |
13136 | if (SuppressRHSDiags) |
13137 | job.startSpeculativeEval(Info); |
13138 | job.LHSResult.swap(RHS&: Result); |
13139 | job.Kind = Job::BinOpVisitedLHSKind; |
13140 | enqueue(E: Bop->getRHS()); |
13141 | return; |
13142 | } |
13143 | |
13144 | case Job::BinOpVisitedLHSKind: { |
13145 | const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E); |
13146 | EvalResult RHS; |
13147 | RHS.swap(RHS&: Result); |
13148 | Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val); |
13149 | Queue.pop_back(); |
13150 | return; |
13151 | } |
13152 | } |
13153 | |
13154 | llvm_unreachable("Invalid Job::Kind!" ); |
13155 | } |
13156 | |
13157 | namespace { |
13158 | enum class CmpResult { |
13159 | Unequal, |
13160 | Less, |
13161 | Equal, |
13162 | Greater, |
13163 | Unordered, |
13164 | }; |
13165 | } |
13166 | |
13167 | template <class SuccessCB, class AfterCB> |
13168 | static bool |
13169 | EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E, |
13170 | SuccessCB &&Success, AfterCB &&DoAfter) { |
13171 | assert(!E->isValueDependent()); |
13172 | assert(E->isComparisonOp() && "expected comparison operator" ); |
13173 | assert((E->getOpcode() == BO_Cmp || |
13174 | E->getType()->isIntegralOrEnumerationType()) && |
13175 | "unsupported binary expression evaluation" ); |
13176 | auto Error = [&](const Expr *E) { |
13177 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
13178 | return false; |
13179 | }; |
13180 | |
13181 | bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp; |
13182 | bool IsEquality = E->isEqualityOp(); |
13183 | |
13184 | QualType LHSTy = E->getLHS()->getType(); |
13185 | QualType RHSTy = E->getRHS()->getType(); |
13186 | |
13187 | if (LHSTy->isIntegralOrEnumerationType() && |
13188 | RHSTy->isIntegralOrEnumerationType()) { |
13189 | APSInt LHS, RHS; |
13190 | bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info); |
13191 | if (!LHSOK && !Info.noteFailure()) |
13192 | return false; |
13193 | if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
13194 | return false; |
13195 | if (LHS < RHS) |
13196 | return Success(CmpResult::Less, E); |
13197 | if (LHS > RHS) |
13198 | return Success(CmpResult::Greater, E); |
13199 | return Success(CmpResult::Equal, E); |
13200 | } |
13201 | |
13202 | if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) { |
13203 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy)); |
13204 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy)); |
13205 | |
13206 | bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info); |
13207 | if (!LHSOK && !Info.noteFailure()) |
13208 | return false; |
13209 | if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK) |
13210 | return false; |
13211 | if (LHSFX < RHSFX) |
13212 | return Success(CmpResult::Less, E); |
13213 | if (LHSFX > RHSFX) |
13214 | return Success(CmpResult::Greater, E); |
13215 | return Success(CmpResult::Equal, E); |
13216 | } |
13217 | |
13218 | if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) { |
13219 | ComplexValue LHS, RHS; |
13220 | bool LHSOK; |
13221 | if (E->isAssignmentOp()) { |
13222 | LValue LV; |
13223 | EvaluateLValue(E: E->getLHS(), Result&: LV, Info); |
13224 | LHSOK = false; |
13225 | } else if (LHSTy->isRealFloatingType()) { |
13226 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info); |
13227 | if (LHSOK) { |
13228 | LHS.makeComplexFloat(); |
13229 | LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics()); |
13230 | } |
13231 | } else { |
13232 | LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info); |
13233 | } |
13234 | if (!LHSOK && !Info.noteFailure()) |
13235 | return false; |
13236 | |
13237 | if (E->getRHS()->getType()->isRealFloatingType()) { |
13238 | if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK) |
13239 | return false; |
13240 | RHS.makeComplexFloat(); |
13241 | RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics()); |
13242 | } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK) |
13243 | return false; |
13244 | |
13245 | if (LHS.isComplexFloat()) { |
13246 | APFloat::cmpResult CR_r = |
13247 | LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal()); |
13248 | APFloat::cmpResult CR_i = |
13249 | LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag()); |
13250 | bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual; |
13251 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
13252 | } else { |
13253 | assert(IsEquality && "invalid complex comparison" ); |
13254 | bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() && |
13255 | LHS.getComplexIntImag() == RHS.getComplexIntImag(); |
13256 | return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E); |
13257 | } |
13258 | } |
13259 | |
13260 | if (LHSTy->isRealFloatingType() && |
13261 | RHSTy->isRealFloatingType()) { |
13262 | APFloat RHS(0.0), LHS(0.0); |
13263 | |
13264 | bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info); |
13265 | if (!LHSOK && !Info.noteFailure()) |
13266 | return false; |
13267 | |
13268 | if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK) |
13269 | return false; |
13270 | |
13271 | assert(E->isComparisonOp() && "Invalid binary operator!" ); |
13272 | llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS); |
13273 | if (!Info.InConstantContext && |
13274 | APFloatCmpResult == APFloat::cmpUnordered && |
13275 | E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) { |
13276 | // Note: Compares may raise invalid in some cases involving NaN or sNaN. |
13277 | Info.FFDiag(E, diag::note_constexpr_float_arithmetic_strict); |
13278 | return false; |
13279 | } |
13280 | auto GetCmpRes = [&]() { |
13281 | switch (APFloatCmpResult) { |
13282 | case APFloat::cmpEqual: |
13283 | return CmpResult::Equal; |
13284 | case APFloat::cmpLessThan: |
13285 | return CmpResult::Less; |
13286 | case APFloat::cmpGreaterThan: |
13287 | return CmpResult::Greater; |
13288 | case APFloat::cmpUnordered: |
13289 | return CmpResult::Unordered; |
13290 | } |
13291 | llvm_unreachable("Unrecognised APFloat::cmpResult enum" ); |
13292 | }; |
13293 | return Success(GetCmpRes(), E); |
13294 | } |
13295 | |
13296 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
13297 | LValue LHSValue, RHSValue; |
13298 | |
13299 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
13300 | if (!LHSOK && !Info.noteFailure()) |
13301 | return false; |
13302 | |
13303 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13304 | return false; |
13305 | |
13306 | // Reject differing bases from the normal codepath; we special-case |
13307 | // comparisons to null. |
13308 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
13309 | auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) { |
13310 | std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType()); |
13311 | std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType()); |
13312 | Info.FFDiag(E, DiagID) |
13313 | << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS); |
13314 | return false; |
13315 | }; |
13316 | // Inequalities and subtractions between unrelated pointers have |
13317 | // unspecified or undefined behavior. |
13318 | if (!IsEquality) |
13319 | return DiagComparison( |
13320 | diag::note_constexpr_pointer_comparison_unspecified); |
13321 | // A constant address may compare equal to the address of a symbol. |
13322 | // The one exception is that address of an object cannot compare equal |
13323 | // to a null pointer constant. |
13324 | // TODO: Should we restrict this to actual null pointers, and exclude the |
13325 | // case of zero cast to pointer type? |
13326 | if ((!LHSValue.Base && !LHSValue.Offset.isZero()) || |
13327 | (!RHSValue.Base && !RHSValue.Offset.isZero())) |
13328 | return DiagComparison(diag::note_constexpr_pointer_constant_comparison, |
13329 | !RHSValue.Base); |
13330 | // It's implementation-defined whether distinct literals will have |
13331 | // distinct addresses. In clang, the result of such a comparison is |
13332 | // unspecified, so it is not a constant expression. However, we do know |
13333 | // that the address of a literal will be non-null. |
13334 | if ((IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue)) && |
13335 | LHSValue.Base && RHSValue.Base) |
13336 | return DiagComparison(diag::note_constexpr_literal_comparison); |
13337 | // We can't tell whether weak symbols will end up pointing to the same |
13338 | // object. |
13339 | if (IsWeakLValue(LHSValue) || IsWeakLValue(RHSValue)) |
13340 | return DiagComparison(diag::note_constexpr_pointer_weak_comparison, |
13341 | !IsWeakLValue(LHSValue)); |
13342 | // We can't compare the address of the start of one object with the |
13343 | // past-the-end address of another object, per C++ DR1652. |
13344 | if (LHSValue.Base && LHSValue.Offset.isZero() && |
13345 | isOnePastTheEndOfCompleteObject(Info.Ctx, RHSValue)) |
13346 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
13347 | true); |
13348 | if (RHSValue.Base && RHSValue.Offset.isZero() && |
13349 | isOnePastTheEndOfCompleteObject(Info.Ctx, LHSValue)) |
13350 | return DiagComparison(diag::note_constexpr_pointer_comparison_past_end, |
13351 | false); |
13352 | // We can't tell whether an object is at the same address as another |
13353 | // zero sized object. |
13354 | if ((RHSValue.Base && isZeroSized(LHSValue)) || |
13355 | (LHSValue.Base && isZeroSized(RHSValue))) |
13356 | return DiagComparison( |
13357 | diag::note_constexpr_pointer_comparison_zero_sized); |
13358 | return Success(CmpResult::Unequal, E); |
13359 | } |
13360 | |
13361 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
13362 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
13363 | |
13364 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
13365 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
13366 | |
13367 | // C++11 [expr.rel]p3: |
13368 | // Pointers to void (after pointer conversions) can be compared, with a |
13369 | // result defined as follows: If both pointers represent the same |
13370 | // address or are both the null pointer value, the result is true if the |
13371 | // operator is <= or >= and false otherwise; otherwise the result is |
13372 | // unspecified. |
13373 | // We interpret this as applying to pointers to *cv* void. |
13374 | if (LHSTy->isVoidPointerType() && LHSOffset != RHSOffset && IsRelational) |
13375 | Info.CCEDiag(E, diag::note_constexpr_void_comparison); |
13376 | |
13377 | // C++11 [expr.rel]p2: |
13378 | // - If two pointers point to non-static data members of the same object, |
13379 | // or to subobjects or array elements fo such members, recursively, the |
13380 | // pointer to the later declared member compares greater provided the |
13381 | // two members have the same access control and provided their class is |
13382 | // not a union. |
13383 | // [...] |
13384 | // - Otherwise pointer comparisons are unspecified. |
13385 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) { |
13386 | bool WasArrayIndex; |
13387 | unsigned Mismatch = FindDesignatorMismatch( |
13388 | ObjType: getType(B: LHSValue.Base), A: LHSDesignator, B: RHSDesignator, WasArrayIndex); |
13389 | // At the point where the designators diverge, the comparison has a |
13390 | // specified value if: |
13391 | // - we are comparing array indices |
13392 | // - we are comparing fields of a union, or fields with the same access |
13393 | // Otherwise, the result is unspecified and thus the comparison is not a |
13394 | // constant expression. |
13395 | if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() && |
13396 | Mismatch < RHSDesignator.Entries.size()) { |
13397 | const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]); |
13398 | const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]); |
13399 | if (!LF && !RF) |
13400 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_classes); |
13401 | else if (!LF) |
13402 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
13403 | << getAsBaseClass(LHSDesignator.Entries[Mismatch]) |
13404 | << RF->getParent() << RF; |
13405 | else if (!RF) |
13406 | Info.CCEDiag(E, diag::note_constexpr_pointer_comparison_base_field) |
13407 | << getAsBaseClass(RHSDesignator.Entries[Mismatch]) |
13408 | << LF->getParent() << LF; |
13409 | else if (!LF->getParent()->isUnion() && |
13410 | LF->getAccess() != RF->getAccess()) |
13411 | Info.CCEDiag(E, |
13412 | diag::note_constexpr_pointer_comparison_differing_access) |
13413 | << LF << LF->getAccess() << RF << RF->getAccess() |
13414 | << LF->getParent(); |
13415 | } |
13416 | } |
13417 | |
13418 | // The comparison here must be unsigned, and performed with the same |
13419 | // width as the pointer. |
13420 | unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy); |
13421 | uint64_t CompareLHS = LHSOffset.getQuantity(); |
13422 | uint64_t CompareRHS = RHSOffset.getQuantity(); |
13423 | assert(PtrSize <= 64 && "Unexpected pointer width" ); |
13424 | uint64_t Mask = ~0ULL >> (64 - PtrSize); |
13425 | CompareLHS &= Mask; |
13426 | CompareRHS &= Mask; |
13427 | |
13428 | // If there is a base and this is a relational operator, we can only |
13429 | // compare pointers within the object in question; otherwise, the result |
13430 | // depends on where the object is located in memory. |
13431 | if (!LHSValue.Base.isNull() && IsRelational) { |
13432 | QualType BaseTy = getType(B: LHSValue.Base); |
13433 | if (BaseTy->isIncompleteType()) |
13434 | return Error(E); |
13435 | CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy); |
13436 | uint64_t OffsetLimit = Size.getQuantity(); |
13437 | if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit) |
13438 | return Error(E); |
13439 | } |
13440 | |
13441 | if (CompareLHS < CompareRHS) |
13442 | return Success(CmpResult::Less, E); |
13443 | if (CompareLHS > CompareRHS) |
13444 | return Success(CmpResult::Greater, E); |
13445 | return Success(CmpResult::Equal, E); |
13446 | } |
13447 | |
13448 | if (LHSTy->isMemberPointerType()) { |
13449 | assert(IsEquality && "unexpected member pointer operation" ); |
13450 | assert(RHSTy->isMemberPointerType() && "invalid comparison" ); |
13451 | |
13452 | MemberPtr LHSValue, RHSValue; |
13453 | |
13454 | bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info); |
13455 | if (!LHSOK && !Info.noteFailure()) |
13456 | return false; |
13457 | |
13458 | if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13459 | return false; |
13460 | |
13461 | // If either operand is a pointer to a weak function, the comparison is not |
13462 | // constant. |
13463 | if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) { |
13464 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
13465 | << LHSValue.getDecl(); |
13466 | return false; |
13467 | } |
13468 | if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) { |
13469 | Info.FFDiag(E, diag::note_constexpr_mem_pointer_weak_comparison) |
13470 | << RHSValue.getDecl(); |
13471 | return false; |
13472 | } |
13473 | |
13474 | // C++11 [expr.eq]p2: |
13475 | // If both operands are null, they compare equal. Otherwise if only one is |
13476 | // null, they compare unequal. |
13477 | if (!LHSValue.getDecl() || !RHSValue.getDecl()) { |
13478 | bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl(); |
13479 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
13480 | } |
13481 | |
13482 | // Otherwise if either is a pointer to a virtual member function, the |
13483 | // result is unspecified. |
13484 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(LHSValue.getDecl())) |
13485 | if (MD->isVirtual()) |
13486 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
13487 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(RHSValue.getDecl())) |
13488 | if (MD->isVirtual()) |
13489 | Info.CCEDiag(E, diag::note_constexpr_compare_virtual_mem_ptr) << MD; |
13490 | |
13491 | // Otherwise they compare equal if and only if they would refer to the |
13492 | // same member of the same most derived object or the same subobject if |
13493 | // they were dereferenced with a hypothetical object of the associated |
13494 | // class type. |
13495 | bool Equal = LHSValue == RHSValue; |
13496 | return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E); |
13497 | } |
13498 | |
13499 | if (LHSTy->isNullPtrType()) { |
13500 | assert(E->isComparisonOp() && "unexpected nullptr operation" ); |
13501 | assert(RHSTy->isNullPtrType() && "missing pointer conversion" ); |
13502 | // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t |
13503 | // are compared, the result is true of the operator is <=, >= or ==, and |
13504 | // false otherwise. |
13505 | LValue Res; |
13506 | if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) || |
13507 | !EvaluatePointer(E: E->getRHS(), Result&: Res, Info)) |
13508 | return false; |
13509 | return Success(CmpResult::Equal, E); |
13510 | } |
13511 | |
13512 | return DoAfter(); |
13513 | } |
13514 | |
13515 | bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) { |
13516 | if (!CheckLiteralType(Info, E)) |
13517 | return false; |
13518 | |
13519 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
13520 | ComparisonCategoryResult CCR; |
13521 | switch (CR) { |
13522 | case CmpResult::Unequal: |
13523 | llvm_unreachable("should never produce Unequal for three-way comparison" ); |
13524 | case CmpResult::Less: |
13525 | CCR = ComparisonCategoryResult::Less; |
13526 | break; |
13527 | case CmpResult::Equal: |
13528 | CCR = ComparisonCategoryResult::Equal; |
13529 | break; |
13530 | case CmpResult::Greater: |
13531 | CCR = ComparisonCategoryResult::Greater; |
13532 | break; |
13533 | case CmpResult::Unordered: |
13534 | CCR = ComparisonCategoryResult::Unordered; |
13535 | break; |
13536 | } |
13537 | // Evaluation succeeded. Lookup the information for the comparison category |
13538 | // type and fetch the VarDecl for the result. |
13539 | const ComparisonCategoryInfo &CmpInfo = |
13540 | Info.Ctx.CompCategories.getInfoForType(Ty: E->getType()); |
13541 | const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD; |
13542 | // Check and evaluate the result as a constant expression. |
13543 | LValue LV; |
13544 | LV.set(VD); |
13545 | if (!handleLValueToRValueConversion(Info, E, E->getType(), LV, Result)) |
13546 | return false; |
13547 | return CheckConstantExpression(Info, E->getExprLoc(), E->getType(), Result, |
13548 | ConstantExprKind::Normal); |
13549 | }; |
13550 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
13551 | return ExprEvaluatorBaseTy::VisitBinCmp(E); |
13552 | }); |
13553 | } |
13554 | |
13555 | bool RecordExprEvaluator::VisitCXXParenListInitExpr( |
13556 | const CXXParenListInitExpr *E) { |
13557 | return VisitCXXParenListOrInitListExpr(E, E->getInitExprs()); |
13558 | } |
13559 | |
13560 | bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
13561 | // We don't support assignment in C. C++ assignments don't get here because |
13562 | // assignment is an lvalue in C++. |
13563 | if (E->isAssignmentOp()) { |
13564 | Error(E); |
13565 | if (!Info.noteFailure()) |
13566 | return false; |
13567 | } |
13568 | |
13569 | if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E)) |
13570 | return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E); |
13571 | |
13572 | assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() || |
13573 | !E->getRHS()->getType()->isIntegralOrEnumerationType()) && |
13574 | "DataRecursiveIntBinOpEvaluator should have handled integral types" ); |
13575 | |
13576 | if (E->isComparisonOp()) { |
13577 | // Evaluate builtin binary comparisons by evaluating them as three-way |
13578 | // comparisons and then translating the result. |
13579 | auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) { |
13580 | assert((CR != CmpResult::Unequal || E->isEqualityOp()) && |
13581 | "should only produce Unequal for equality comparisons" ); |
13582 | bool IsEqual = CR == CmpResult::Equal, |
13583 | IsLess = CR == CmpResult::Less, |
13584 | IsGreater = CR == CmpResult::Greater; |
13585 | auto Op = E->getOpcode(); |
13586 | switch (Op) { |
13587 | default: |
13588 | llvm_unreachable("unsupported binary operator" ); |
13589 | case BO_EQ: |
13590 | case BO_NE: |
13591 | return Success(IsEqual == (Op == BO_EQ), E); |
13592 | case BO_LT: |
13593 | return Success(IsLess, E); |
13594 | case BO_GT: |
13595 | return Success(IsGreater, E); |
13596 | case BO_LE: |
13597 | return Success(IsEqual || IsLess, E); |
13598 | case BO_GE: |
13599 | return Success(IsEqual || IsGreater, E); |
13600 | } |
13601 | }; |
13602 | return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() { |
13603 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13604 | }); |
13605 | } |
13606 | |
13607 | QualType LHSTy = E->getLHS()->getType(); |
13608 | QualType RHSTy = E->getRHS()->getType(); |
13609 | |
13610 | if (LHSTy->isPointerType() && RHSTy->isPointerType() && |
13611 | E->getOpcode() == BO_Sub) { |
13612 | LValue LHSValue, RHSValue; |
13613 | |
13614 | bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info); |
13615 | if (!LHSOK && !Info.noteFailure()) |
13616 | return false; |
13617 | |
13618 | if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK) |
13619 | return false; |
13620 | |
13621 | // Reject differing bases from the normal codepath; we special-case |
13622 | // comparisons to null. |
13623 | if (!HasSameBase(A: LHSValue, B: RHSValue)) { |
13624 | // Handle &&A - &&B. |
13625 | if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero()) |
13626 | return Error(E); |
13627 | const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>(); |
13628 | const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>(); |
13629 | if (!LHSExpr || !RHSExpr) |
13630 | return Error(E); |
13631 | const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr); |
13632 | const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr); |
13633 | if (!LHSAddrExpr || !RHSAddrExpr) |
13634 | return Error(E); |
13635 | // Make sure both labels come from the same function. |
13636 | if (LHSAddrExpr->getLabel()->getDeclContext() != |
13637 | RHSAddrExpr->getLabel()->getDeclContext()) |
13638 | return Error(E); |
13639 | return Success(APValue(LHSAddrExpr, RHSAddrExpr), E); |
13640 | } |
13641 | const CharUnits &LHSOffset = LHSValue.getLValueOffset(); |
13642 | const CharUnits &RHSOffset = RHSValue.getLValueOffset(); |
13643 | |
13644 | SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator(); |
13645 | SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator(); |
13646 | |
13647 | // C++11 [expr.add]p6: |
13648 | // Unless both pointers point to elements of the same array object, or |
13649 | // one past the last element of the array object, the behavior is |
13650 | // undefined. |
13651 | if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && |
13652 | !AreElementsOfSameArray(getType(LHSValue.Base), LHSDesignator, |
13653 | RHSDesignator)) |
13654 | Info.CCEDiag(E, diag::note_constexpr_pointer_subtraction_not_same_array); |
13655 | |
13656 | QualType Type = E->getLHS()->getType(); |
13657 | QualType ElementType = Type->castAs<PointerType>()->getPointeeType(); |
13658 | |
13659 | CharUnits ElementSize; |
13660 | if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize)) |
13661 | return false; |
13662 | |
13663 | // As an extension, a type may have zero size (empty struct or union in |
13664 | // C, array of zero length). Pointer subtraction in such cases has |
13665 | // undefined behavior, so is not constant. |
13666 | if (ElementSize.isZero()) { |
13667 | Info.FFDiag(E, diag::note_constexpr_pointer_subtraction_zero_size) |
13668 | << ElementType; |
13669 | return false; |
13670 | } |
13671 | |
13672 | // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime, |
13673 | // and produce incorrect results when it overflows. Such behavior |
13674 | // appears to be non-conforming, but is common, so perhaps we should |
13675 | // assume the standard intended for such cases to be undefined behavior |
13676 | // and check for them. |
13677 | |
13678 | // Compute (LHSOffset - RHSOffset) / Size carefully, checking for |
13679 | // overflow in the final conversion to ptrdiff_t. |
13680 | APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false); |
13681 | APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false); |
13682 | APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true), |
13683 | false); |
13684 | APSInt TrueResult = (LHS - RHS) / ElemSize; |
13685 | APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType())); |
13686 | |
13687 | if (Result.extend(width: 65) != TrueResult && |
13688 | !HandleOverflow(Info, E, TrueResult, E->getType())) |
13689 | return false; |
13690 | return Success(Result, E); |
13691 | } |
13692 | |
13693 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
13694 | } |
13695 | |
13696 | /// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with |
13697 | /// a result as the expression's type. |
13698 | bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr( |
13699 | const UnaryExprOrTypeTraitExpr *E) { |
13700 | switch(E->getKind()) { |
13701 | case UETT_PreferredAlignOf: |
13702 | case UETT_AlignOf: { |
13703 | if (E->isArgumentType()) |
13704 | return Success(GetAlignOfType(Info, T: E->getArgumentType(), ExprKind: E->getKind()), |
13705 | E); |
13706 | else |
13707 | return Success(GetAlignOfExpr(Info, E: E->getArgumentExpr(), ExprKind: E->getKind()), |
13708 | E); |
13709 | } |
13710 | |
13711 | case UETT_VecStep: { |
13712 | QualType Ty = E->getTypeOfArgument(); |
13713 | |
13714 | if (Ty->isVectorType()) { |
13715 | unsigned n = Ty->castAs<VectorType>()->getNumElements(); |
13716 | |
13717 | // The vec_step built-in functions that take a 3-component |
13718 | // vector return 4. (OpenCL 1.1 spec 6.11.12) |
13719 | if (n == 3) |
13720 | n = 4; |
13721 | |
13722 | return Success(n, E); |
13723 | } else |
13724 | return Success(1, E); |
13725 | } |
13726 | |
13727 | case UETT_DataSizeOf: |
13728 | case UETT_SizeOf: { |
13729 | QualType SrcTy = E->getTypeOfArgument(); |
13730 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
13731 | // the result is the size of the referenced type." |
13732 | if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>()) |
13733 | SrcTy = Ref->getPointeeType(); |
13734 | |
13735 | CharUnits Sizeof; |
13736 | if (!HandleSizeof(Info, E->getExprLoc(), SrcTy, Sizeof, |
13737 | E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf |
13738 | : SizeOfType::SizeOf)) { |
13739 | return false; |
13740 | } |
13741 | return Success(Sizeof, E); |
13742 | } |
13743 | case UETT_OpenMPRequiredSimdAlign: |
13744 | assert(E->isArgumentType()); |
13745 | return Success( |
13746 | Info.Ctx.toCharUnitsFromBits( |
13747 | BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType())) |
13748 | .getQuantity(), |
13749 | E); |
13750 | case UETT_VectorElements: { |
13751 | QualType Ty = E->getTypeOfArgument(); |
13752 | // If the vector has a fixed size, we can determine the number of elements |
13753 | // at compile time. |
13754 | if (Ty->isVectorType()) |
13755 | return Success(Ty->castAs<VectorType>()->getNumElements(), E); |
13756 | |
13757 | assert(Ty->isSizelessVectorType()); |
13758 | if (Info.InConstantContext) |
13759 | Info.CCEDiag(E, diag::note_constexpr_non_const_vectorelements) |
13760 | << E->getSourceRange(); |
13761 | |
13762 | return false; |
13763 | } |
13764 | } |
13765 | |
13766 | llvm_unreachable("unknown expr/type trait" ); |
13767 | } |
13768 | |
13769 | bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) { |
13770 | CharUnits Result; |
13771 | unsigned n = OOE->getNumComponents(); |
13772 | if (n == 0) |
13773 | return Error(OOE); |
13774 | QualType CurrentType = OOE->getTypeSourceInfo()->getType(); |
13775 | for (unsigned i = 0; i != n; ++i) { |
13776 | OffsetOfNode ON = OOE->getComponent(Idx: i); |
13777 | switch (ON.getKind()) { |
13778 | case OffsetOfNode::Array: { |
13779 | const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex()); |
13780 | APSInt IdxResult; |
13781 | if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info)) |
13782 | return false; |
13783 | const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType); |
13784 | if (!AT) |
13785 | return Error(OOE); |
13786 | CurrentType = AT->getElementType(); |
13787 | CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType); |
13788 | Result += IdxResult.getSExtValue() * ElementSize; |
13789 | break; |
13790 | } |
13791 | |
13792 | case OffsetOfNode::Field: { |
13793 | FieldDecl *MemberDecl = ON.getField(); |
13794 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13795 | if (!RT) |
13796 | return Error(OOE); |
13797 | RecordDecl *RD = RT->getDecl(); |
13798 | if (RD->isInvalidDecl()) return false; |
13799 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
13800 | unsigned i = MemberDecl->getFieldIndex(); |
13801 | assert(i < RL.getFieldCount() && "offsetof field in wrong type" ); |
13802 | Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i)); |
13803 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
13804 | break; |
13805 | } |
13806 | |
13807 | case OffsetOfNode::Identifier: |
13808 | llvm_unreachable("dependent __builtin_offsetof" ); |
13809 | |
13810 | case OffsetOfNode::Base: { |
13811 | CXXBaseSpecifier *BaseSpec = ON.getBase(); |
13812 | if (BaseSpec->isVirtual()) |
13813 | return Error(OOE); |
13814 | |
13815 | // Find the layout of the class whose base we are looking into. |
13816 | const RecordType *RT = CurrentType->getAs<RecordType>(); |
13817 | if (!RT) |
13818 | return Error(OOE); |
13819 | RecordDecl *RD = RT->getDecl(); |
13820 | if (RD->isInvalidDecl()) return false; |
13821 | const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD); |
13822 | |
13823 | // Find the base class itself. |
13824 | CurrentType = BaseSpec->getType(); |
13825 | const RecordType *BaseRT = CurrentType->getAs<RecordType>(); |
13826 | if (!BaseRT) |
13827 | return Error(OOE); |
13828 | |
13829 | // Add the offset to the base. |
13830 | Result += RL.getBaseClassOffset(Base: cast<CXXRecordDecl>(Val: BaseRT->getDecl())); |
13831 | break; |
13832 | } |
13833 | } |
13834 | } |
13835 | return Success(Result, OOE); |
13836 | } |
13837 | |
13838 | bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
13839 | switch (E->getOpcode()) { |
13840 | default: |
13841 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
13842 | // See C99 6.6p3. |
13843 | return Error(E); |
13844 | case UO_Extension: |
13845 | // FIXME: Should extension allow i-c-e extension expressions in its scope? |
13846 | // If so, we could clear the diagnostic ID. |
13847 | return Visit(E->getSubExpr()); |
13848 | case UO_Plus: |
13849 | // The result is just the value. |
13850 | return Visit(E->getSubExpr()); |
13851 | case UO_Minus: { |
13852 | if (!Visit(E->getSubExpr())) |
13853 | return false; |
13854 | if (!Result.isInt()) return Error(E); |
13855 | const APSInt &Value = Result.getInt(); |
13856 | if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow()) { |
13857 | if (Info.checkingForUndefinedBehavior()) |
13858 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
13859 | diag::warn_integer_constant_overflow) |
13860 | << toString(Value, 10) << E->getType() << E->getSourceRange(); |
13861 | |
13862 | if (!HandleOverflow(Info, E, -Value.extend(width: Value.getBitWidth() + 1), |
13863 | E->getType())) |
13864 | return false; |
13865 | } |
13866 | return Success(-Value, E); |
13867 | } |
13868 | case UO_Not: { |
13869 | if (!Visit(E->getSubExpr())) |
13870 | return false; |
13871 | if (!Result.isInt()) return Error(E); |
13872 | return Success(~Result.getInt(), E); |
13873 | } |
13874 | case UO_LNot: { |
13875 | bool bres; |
13876 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
13877 | return false; |
13878 | return Success(!bres, E); |
13879 | } |
13880 | } |
13881 | } |
13882 | |
13883 | /// HandleCast - This is used to evaluate implicit or explicit casts where the |
13884 | /// result type is integer. |
13885 | bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) { |
13886 | const Expr *SubExpr = E->getSubExpr(); |
13887 | QualType DestType = E->getType(); |
13888 | QualType SrcType = SubExpr->getType(); |
13889 | |
13890 | switch (E->getCastKind()) { |
13891 | case CK_BaseToDerived: |
13892 | case CK_DerivedToBase: |
13893 | case CK_UncheckedDerivedToBase: |
13894 | case CK_Dynamic: |
13895 | case CK_ToUnion: |
13896 | case CK_ArrayToPointerDecay: |
13897 | case CK_FunctionToPointerDecay: |
13898 | case CK_NullToPointer: |
13899 | case CK_NullToMemberPointer: |
13900 | case CK_BaseToDerivedMemberPointer: |
13901 | case CK_DerivedToBaseMemberPointer: |
13902 | case CK_ReinterpretMemberPointer: |
13903 | case CK_ConstructorConversion: |
13904 | case CK_IntegralToPointer: |
13905 | case CK_ToVoid: |
13906 | case CK_VectorSplat: |
13907 | case CK_IntegralToFloating: |
13908 | case CK_FloatingCast: |
13909 | case CK_CPointerToObjCPointerCast: |
13910 | case CK_BlockPointerToObjCPointerCast: |
13911 | case CK_AnyPointerToBlockPointerCast: |
13912 | case CK_ObjCObjectLValueCast: |
13913 | case CK_FloatingRealToComplex: |
13914 | case CK_FloatingComplexToReal: |
13915 | case CK_FloatingComplexCast: |
13916 | case CK_FloatingComplexToIntegralComplex: |
13917 | case CK_IntegralRealToComplex: |
13918 | case CK_IntegralComplexCast: |
13919 | case CK_IntegralComplexToFloatingComplex: |
13920 | case CK_BuiltinFnToFnPtr: |
13921 | case CK_ZeroToOCLOpaqueType: |
13922 | case CK_NonAtomicToAtomic: |
13923 | case CK_AddressSpaceConversion: |
13924 | case CK_IntToOCLSampler: |
13925 | case CK_FloatingToFixedPoint: |
13926 | case CK_FixedPointToFloating: |
13927 | case CK_FixedPointCast: |
13928 | case CK_IntegralToFixedPoint: |
13929 | case CK_MatrixCast: |
13930 | llvm_unreachable("invalid cast kind for integral value" ); |
13931 | |
13932 | case CK_BitCast: |
13933 | case CK_Dependent: |
13934 | case CK_LValueBitCast: |
13935 | case CK_ARCProduceObject: |
13936 | case CK_ARCConsumeObject: |
13937 | case CK_ARCReclaimReturnedObject: |
13938 | case CK_ARCExtendBlockObject: |
13939 | case CK_CopyAndAutoreleaseBlockObject: |
13940 | return Error(E); |
13941 | |
13942 | case CK_UserDefinedConversion: |
13943 | case CK_LValueToRValue: |
13944 | case CK_AtomicToNonAtomic: |
13945 | case CK_NoOp: |
13946 | case CK_LValueToRValueBitCast: |
13947 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
13948 | |
13949 | case CK_MemberPointerToBoolean: |
13950 | case CK_PointerToBoolean: |
13951 | case CK_IntegralToBoolean: |
13952 | case CK_FloatingToBoolean: |
13953 | case CK_BooleanToSignedIntegral: |
13954 | case CK_FloatingComplexToBoolean: |
13955 | case CK_IntegralComplexToBoolean: { |
13956 | bool BoolResult; |
13957 | if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info)) |
13958 | return false; |
13959 | uint64_t IntResult = BoolResult; |
13960 | if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral) |
13961 | IntResult = (uint64_t)-1; |
13962 | return Success(IntResult, E); |
13963 | } |
13964 | |
13965 | case CK_FixedPointToIntegral: { |
13966 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType)); |
13967 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
13968 | return false; |
13969 | bool Overflowed; |
13970 | llvm::APSInt Result = Src.convertToInt( |
13971 | DstWidth: Info.Ctx.getIntWidth(T: DestType), |
13972 | DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed); |
13973 | if (Overflowed && !HandleOverflow(Info, E, Result, DestType)) |
13974 | return false; |
13975 | return Success(Result, E); |
13976 | } |
13977 | |
13978 | case CK_FixedPointToBoolean: { |
13979 | // Unsigned padding does not affect this. |
13980 | APValue Val; |
13981 | if (!Evaluate(Result&: Val, Info, E: SubExpr)) |
13982 | return false; |
13983 | return Success(Val.getFixedPoint().getBoolValue(), E); |
13984 | } |
13985 | |
13986 | case CK_IntegralCast: { |
13987 | if (!Visit(SubExpr)) |
13988 | return false; |
13989 | |
13990 | if (!Result.isInt()) { |
13991 | // Allow casts of address-of-label differences if they are no-ops |
13992 | // or narrowing. (The narrowing case isn't actually guaranteed to |
13993 | // be constant-evaluatable except in some narrow cases which are hard |
13994 | // to detect here. We let it through on the assumption the user knows |
13995 | // what they are doing.) |
13996 | if (Result.isAddrLabelDiff()) |
13997 | return Info.Ctx.getTypeSize(T: DestType) <= Info.Ctx.getTypeSize(T: SrcType); |
13998 | // Only allow casts of lvalues if they are lossless. |
13999 | return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType); |
14000 | } |
14001 | |
14002 | if (Info.Ctx.getLangOpts().CPlusPlus && Info.InConstantContext && |
14003 | Info.EvalMode == EvalInfo::EM_ConstantExpression && |
14004 | DestType->isEnumeralType()) { |
14005 | |
14006 | bool ConstexprVar = true; |
14007 | |
14008 | // We know if we are here that we are in a context that we might require |
14009 | // a constant expression or a context that requires a constant |
14010 | // value. But if we are initializing a value we don't know if it is a |
14011 | // constexpr variable or not. We can check the EvaluatingDecl to determine |
14012 | // if it constexpr or not. If not then we don't want to emit a diagnostic. |
14013 | if (const auto *VD = dyn_cast_or_null<VarDecl>( |
14014 | Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>())) |
14015 | ConstexprVar = VD->isConstexpr(); |
14016 | |
14017 | const EnumType *ET = dyn_cast<EnumType>(Val: DestType.getCanonicalType()); |
14018 | const EnumDecl *ED = ET->getDecl(); |
14019 | // Check that the value is within the range of the enumeration values. |
14020 | // |
14021 | // This corressponds to [expr.static.cast]p10 which says: |
14022 | // A value of integral or enumeration type can be explicitly converted |
14023 | // to a complete enumeration type ... If the enumeration type does not |
14024 | // have a fixed underlying type, the value is unchanged if the original |
14025 | // value is within the range of the enumeration values ([dcl.enum]), and |
14026 | // otherwise, the behavior is undefined. |
14027 | // |
14028 | // This was resolved as part of DR2338 which has CD5 status. |
14029 | if (!ED->isFixed()) { |
14030 | llvm::APInt Min; |
14031 | llvm::APInt Max; |
14032 | |
14033 | ED->getValueRange(Max, Min); |
14034 | --Max; |
14035 | |
14036 | if (ED->getNumNegativeBits() && ConstexprVar && |
14037 | (Max.slt(Result.getInt().getSExtValue()) || |
14038 | Min.sgt(Result.getInt().getSExtValue()))) |
14039 | Info.Ctx.getDiagnostics().Report( |
14040 | E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range) |
14041 | << llvm::toString(Result.getInt(), 10) << Min.getSExtValue() |
14042 | << Max.getSExtValue() << ED; |
14043 | else if (!ED->getNumNegativeBits() && ConstexprVar && |
14044 | Max.ult(Result.getInt().getZExtValue())) |
14045 | Info.Ctx.getDiagnostics().Report( |
14046 | E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range) |
14047 | << llvm::toString(Result.getInt(), 10) << Min.getZExtValue() |
14048 | << Max.getZExtValue() << ED; |
14049 | } |
14050 | } |
14051 | |
14052 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, |
14053 | Result.getInt()), E); |
14054 | } |
14055 | |
14056 | case CK_PointerToIntegral: { |
14057 | CCEDiag(E, diag::note_constexpr_invalid_cast) |
14058 | << 2 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange(); |
14059 | |
14060 | LValue LV; |
14061 | if (!EvaluatePointer(E: SubExpr, Result&: LV, Info)) |
14062 | return false; |
14063 | |
14064 | if (LV.getLValueBase()) { |
14065 | // Only allow based lvalue casts if they are lossless. |
14066 | // FIXME: Allow a larger integer size than the pointer size, and allow |
14067 | // narrowing back down to pointer width in subsequent integral casts. |
14068 | // FIXME: Check integer type's active bits, not its type size. |
14069 | if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType)) |
14070 | return Error(E); |
14071 | |
14072 | LV.Designator.setInvalid(); |
14073 | LV.moveInto(V&: Result); |
14074 | return true; |
14075 | } |
14076 | |
14077 | APSInt AsInt; |
14078 | APValue V; |
14079 | LV.moveInto(V); |
14080 | if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx)) |
14081 | llvm_unreachable("Can't cast this!" ); |
14082 | |
14083 | return Success(HandleIntToIntCast(Info, E, DestType, SrcType, AsInt), E); |
14084 | } |
14085 | |
14086 | case CK_IntegralComplexToReal: { |
14087 | ComplexValue C; |
14088 | if (!EvaluateComplex(E: SubExpr, Res&: C, Info)) |
14089 | return false; |
14090 | return Success(C.getComplexIntReal(), E); |
14091 | } |
14092 | |
14093 | case CK_FloatingToIntegral: { |
14094 | APFloat F(0.0); |
14095 | if (!EvaluateFloat(E: SubExpr, Result&: F, Info)) |
14096 | return false; |
14097 | |
14098 | APSInt Value; |
14099 | if (!HandleFloatToIntCast(Info, E, SrcType, F, DestType, Value)) |
14100 | return false; |
14101 | return Success(Value, E); |
14102 | } |
14103 | } |
14104 | |
14105 | llvm_unreachable("unknown cast resulting in integral value" ); |
14106 | } |
14107 | |
14108 | bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
14109 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14110 | ComplexValue LV; |
14111 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
14112 | return false; |
14113 | if (!LV.isComplexInt()) |
14114 | return Error(E); |
14115 | return Success(LV.getComplexIntReal(), E); |
14116 | } |
14117 | |
14118 | return Visit(E->getSubExpr()); |
14119 | } |
14120 | |
14121 | bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
14122 | if (E->getSubExpr()->getType()->isComplexIntegerType()) { |
14123 | ComplexValue LV; |
14124 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info)) |
14125 | return false; |
14126 | if (!LV.isComplexInt()) |
14127 | return Error(E); |
14128 | return Success(LV.getComplexIntImag(), E); |
14129 | } |
14130 | |
14131 | VisitIgnoredValue(E: E->getSubExpr()); |
14132 | return Success(0, E); |
14133 | } |
14134 | |
14135 | bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) { |
14136 | return Success(E->getPackLength(), E); |
14137 | } |
14138 | |
14139 | bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) { |
14140 | return Success(E->getValue(), E); |
14141 | } |
14142 | |
14143 | bool IntExprEvaluator::VisitConceptSpecializationExpr( |
14144 | const ConceptSpecializationExpr *E) { |
14145 | return Success(E->isSatisfied(), E); |
14146 | } |
14147 | |
14148 | bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) { |
14149 | return Success(E->isSatisfied(), E); |
14150 | } |
14151 | |
14152 | bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14153 | switch (E->getOpcode()) { |
14154 | default: |
14155 | // Invalid unary operators |
14156 | return Error(E); |
14157 | case UO_Plus: |
14158 | // The result is just the value. |
14159 | return Visit(E->getSubExpr()); |
14160 | case UO_Minus: { |
14161 | if (!Visit(E->getSubExpr())) return false; |
14162 | if (!Result.isFixedPoint()) |
14163 | return Error(E); |
14164 | bool Overflowed; |
14165 | APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed); |
14166 | if (Overflowed && !HandleOverflow(Info, E, Negated, E->getType())) |
14167 | return false; |
14168 | return Success(Negated, E); |
14169 | } |
14170 | case UO_LNot: { |
14171 | bool bres; |
14172 | if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info)) |
14173 | return false; |
14174 | return Success(!bres, E); |
14175 | } |
14176 | } |
14177 | } |
14178 | |
14179 | bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14180 | const Expr *SubExpr = E->getSubExpr(); |
14181 | QualType DestType = E->getType(); |
14182 | assert(DestType->isFixedPointType() && |
14183 | "Expected destination type to be a fixed point type" ); |
14184 | auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType); |
14185 | |
14186 | switch (E->getCastKind()) { |
14187 | case CK_FixedPointCast: { |
14188 | APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
14189 | if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info)) |
14190 | return false; |
14191 | bool Overflowed; |
14192 | APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed); |
14193 | if (Overflowed) { |
14194 | if (Info.checkingForUndefinedBehavior()) |
14195 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14196 | diag::warn_fixedpoint_constant_overflow) |
14197 | << Result.toString() << E->getType(); |
14198 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14199 | return false; |
14200 | } |
14201 | return Success(Result, E); |
14202 | } |
14203 | case CK_IntegralToFixedPoint: { |
14204 | APSInt Src; |
14205 | if (!EvaluateInteger(E: SubExpr, Result&: Src, Info)) |
14206 | return false; |
14207 | |
14208 | bool Overflowed; |
14209 | APFixedPoint IntResult = APFixedPoint::getFromIntValue( |
14210 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
14211 | |
14212 | if (Overflowed) { |
14213 | if (Info.checkingForUndefinedBehavior()) |
14214 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14215 | diag::warn_fixedpoint_constant_overflow) |
14216 | << IntResult.toString() << E->getType(); |
14217 | if (!HandleOverflow(Info, E, IntResult, E->getType())) |
14218 | return false; |
14219 | } |
14220 | |
14221 | return Success(IntResult, E); |
14222 | } |
14223 | case CK_FloatingToFixedPoint: { |
14224 | APFloat Src(0.0); |
14225 | if (!EvaluateFloat(E: SubExpr, Result&: Src, Info)) |
14226 | return false; |
14227 | |
14228 | bool Overflowed; |
14229 | APFixedPoint Result = APFixedPoint::getFromFloatValue( |
14230 | Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed); |
14231 | |
14232 | if (Overflowed) { |
14233 | if (Info.checkingForUndefinedBehavior()) |
14234 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14235 | diag::warn_fixedpoint_constant_overflow) |
14236 | << Result.toString() << E->getType(); |
14237 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14238 | return false; |
14239 | } |
14240 | |
14241 | return Success(Result, E); |
14242 | } |
14243 | case CK_NoOp: |
14244 | case CK_LValueToRValue: |
14245 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14246 | default: |
14247 | return Error(E); |
14248 | } |
14249 | } |
14250 | |
14251 | bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14252 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14253 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14254 | |
14255 | const Expr *LHS = E->getLHS(); |
14256 | const Expr *RHS = E->getRHS(); |
14257 | FixedPointSemantics ResultFXSema = |
14258 | Info.Ctx.getFixedPointSemantics(Ty: E->getType()); |
14259 | |
14260 | APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType())); |
14261 | if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info)) |
14262 | return false; |
14263 | APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType())); |
14264 | if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info)) |
14265 | return false; |
14266 | |
14267 | bool OpOverflow = false, ConversionOverflow = false; |
14268 | APFixedPoint Result(LHSFX.getSemantics()); |
14269 | switch (E->getOpcode()) { |
14270 | case BO_Add: { |
14271 | Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow) |
14272 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14273 | break; |
14274 | } |
14275 | case BO_Sub: { |
14276 | Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow) |
14277 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14278 | break; |
14279 | } |
14280 | case BO_Mul: { |
14281 | Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow) |
14282 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14283 | break; |
14284 | } |
14285 | case BO_Div: { |
14286 | if (RHSFX.getValue() == 0) { |
14287 | Info.FFDiag(E, diag::note_expr_divide_by_zero); |
14288 | return false; |
14289 | } |
14290 | Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow) |
14291 | .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow); |
14292 | break; |
14293 | } |
14294 | case BO_Shl: |
14295 | case BO_Shr: { |
14296 | FixedPointSemantics LHSSema = LHSFX.getSemantics(); |
14297 | llvm::APSInt RHSVal = RHSFX.getValue(); |
14298 | |
14299 | unsigned ShiftBW = |
14300 | LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding(); |
14301 | unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1); |
14302 | // Embedded-C 4.1.6.2.2: |
14303 | // The right operand must be nonnegative and less than the total number |
14304 | // of (nonpadding) bits of the fixed-point operand ... |
14305 | if (RHSVal.isNegative()) |
14306 | Info.CCEDiag(E, diag::note_constexpr_negative_shift) << RHSVal; |
14307 | else if (Amt != RHSVal) |
14308 | Info.CCEDiag(E, diag::note_constexpr_large_shift) |
14309 | << RHSVal << E->getType() << ShiftBW; |
14310 | |
14311 | if (E->getOpcode() == BO_Shl) |
14312 | Result = LHSFX.shl(Amt, Overflow: &OpOverflow); |
14313 | else |
14314 | Result = LHSFX.shr(Amt, Overflow: &OpOverflow); |
14315 | break; |
14316 | } |
14317 | default: |
14318 | return false; |
14319 | } |
14320 | if (OpOverflow || ConversionOverflow) { |
14321 | if (Info.checkingForUndefinedBehavior()) |
14322 | Info.Ctx.getDiagnostics().Report(E->getExprLoc(), |
14323 | diag::warn_fixedpoint_constant_overflow) |
14324 | << Result.toString() << E->getType(); |
14325 | if (!HandleOverflow(Info, E, Result, E->getType())) |
14326 | return false; |
14327 | } |
14328 | return Success(Result, E); |
14329 | } |
14330 | |
14331 | //===----------------------------------------------------------------------===// |
14332 | // Float Evaluation |
14333 | //===----------------------------------------------------------------------===// |
14334 | |
14335 | namespace { |
14336 | class FloatExprEvaluator |
14337 | : public ExprEvaluatorBase<FloatExprEvaluator> { |
14338 | APFloat &Result; |
14339 | public: |
14340 | FloatExprEvaluator(EvalInfo &info, APFloat &result) |
14341 | : ExprEvaluatorBaseTy(info), Result(result) {} |
14342 | |
14343 | bool Success(const APValue &V, const Expr *e) { |
14344 | Result = V.getFloat(); |
14345 | return true; |
14346 | } |
14347 | |
14348 | bool ZeroInitialization(const Expr *E) { |
14349 | Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
14350 | return true; |
14351 | } |
14352 | |
14353 | bool VisitCallExpr(const CallExpr *E); |
14354 | |
14355 | bool VisitUnaryOperator(const UnaryOperator *E); |
14356 | bool VisitBinaryOperator(const BinaryOperator *E); |
14357 | bool VisitFloatingLiteral(const FloatingLiteral *E); |
14358 | bool VisitCastExpr(const CastExpr *E); |
14359 | |
14360 | bool VisitUnaryReal(const UnaryOperator *E); |
14361 | bool VisitUnaryImag(const UnaryOperator *E); |
14362 | |
14363 | // FIXME: Missing: array subscript of vector, member of vector |
14364 | }; |
14365 | } // end anonymous namespace |
14366 | |
14367 | static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) { |
14368 | assert(!E->isValueDependent()); |
14369 | assert(E->isPRValue() && E->getType()->isRealFloatingType()); |
14370 | return FloatExprEvaluator(Info, Result).Visit(E); |
14371 | } |
14372 | |
14373 | static bool TryEvaluateBuiltinNaN(const ASTContext &Context, |
14374 | QualType ResultTy, |
14375 | const Expr *Arg, |
14376 | bool SNaN, |
14377 | llvm::APFloat &Result) { |
14378 | const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts()); |
14379 | if (!S) return false; |
14380 | |
14381 | const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy); |
14382 | |
14383 | llvm::APInt fill; |
14384 | |
14385 | // Treat empty strings as if they were zero. |
14386 | if (S->getString().empty()) |
14387 | fill = llvm::APInt(32, 0); |
14388 | else if (S->getString().getAsInteger(Radix: 0, Result&: fill)) |
14389 | return false; |
14390 | |
14391 | if (Context.getTargetInfo().isNan2008()) { |
14392 | if (SNaN) |
14393 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
14394 | else |
14395 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
14396 | } else { |
14397 | // Prior to IEEE 754-2008, architectures were allowed to choose whether |
14398 | // the first bit of their significand was set for qNaN or sNaN. MIPS chose |
14399 | // a different encoding to what became a standard in 2008, and for pre- |
14400 | // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as |
14401 | // sNaN. This is now known as "legacy NaN" encoding. |
14402 | if (SNaN) |
14403 | Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill); |
14404 | else |
14405 | Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill); |
14406 | } |
14407 | |
14408 | return true; |
14409 | } |
14410 | |
14411 | bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) { |
14412 | if (!IsConstantEvaluatedBuiltinCall(E)) |
14413 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
14414 | |
14415 | switch (E->getBuiltinCallee()) { |
14416 | default: |
14417 | return false; |
14418 | |
14419 | case Builtin::BI__builtin_huge_val: |
14420 | case Builtin::BI__builtin_huge_valf: |
14421 | case Builtin::BI__builtin_huge_vall: |
14422 | case Builtin::BI__builtin_huge_valf16: |
14423 | case Builtin::BI__builtin_huge_valf128: |
14424 | case Builtin::BI__builtin_inf: |
14425 | case Builtin::BI__builtin_inff: |
14426 | case Builtin::BI__builtin_infl: |
14427 | case Builtin::BI__builtin_inff16: |
14428 | case Builtin::BI__builtin_inff128: { |
14429 | const llvm::fltSemantics &Sem = |
14430 | Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
14431 | Result = llvm::APFloat::getInf(Sem); |
14432 | return true; |
14433 | } |
14434 | |
14435 | case Builtin::BI__builtin_nans: |
14436 | case Builtin::BI__builtin_nansf: |
14437 | case Builtin::BI__builtin_nansl: |
14438 | case Builtin::BI__builtin_nansf16: |
14439 | case Builtin::BI__builtin_nansf128: |
14440 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
14441 | true, Result)) |
14442 | return Error(E); |
14443 | return true; |
14444 | |
14445 | case Builtin::BI__builtin_nan: |
14446 | case Builtin::BI__builtin_nanf: |
14447 | case Builtin::BI__builtin_nanl: |
14448 | case Builtin::BI__builtin_nanf16: |
14449 | case Builtin::BI__builtin_nanf128: |
14450 | // If this is __builtin_nan() turn this into a nan, otherwise we |
14451 | // can't constant fold it. |
14452 | if (!TryEvaluateBuiltinNaN(Info.Ctx, E->getType(), E->getArg(Arg: 0), |
14453 | false, Result)) |
14454 | return Error(E); |
14455 | return true; |
14456 | |
14457 | case Builtin::BI__builtin_fabs: |
14458 | case Builtin::BI__builtin_fabsf: |
14459 | case Builtin::BI__builtin_fabsl: |
14460 | case Builtin::BI__builtin_fabsf128: |
14461 | // The C standard says "fabs raises no floating-point exceptions, |
14462 | // even if x is a signaling NaN. The returned value is independent of |
14463 | // the current rounding direction mode." Therefore constant folding can |
14464 | // proceed without regard to the floating point settings. |
14465 | // Reference, WG14 N2478 F.10.4.3 |
14466 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info)) |
14467 | return false; |
14468 | |
14469 | if (Result.isNegative()) |
14470 | Result.changeSign(); |
14471 | return true; |
14472 | |
14473 | case Builtin::BI__arithmetic_fence: |
14474 | return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info); |
14475 | |
14476 | // FIXME: Builtin::BI__builtin_powi |
14477 | // FIXME: Builtin::BI__builtin_powif |
14478 | // FIXME: Builtin::BI__builtin_powil |
14479 | |
14480 | case Builtin::BI__builtin_copysign: |
14481 | case Builtin::BI__builtin_copysignf: |
14482 | case Builtin::BI__builtin_copysignl: |
14483 | case Builtin::BI__builtin_copysignf128: { |
14484 | APFloat RHS(0.); |
14485 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14486 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14487 | return false; |
14488 | Result.copySign(RHS); |
14489 | return true; |
14490 | } |
14491 | |
14492 | case Builtin::BI__builtin_fmax: |
14493 | case Builtin::BI__builtin_fmaxf: |
14494 | case Builtin::BI__builtin_fmaxl: |
14495 | case Builtin::BI__builtin_fmaxf16: |
14496 | case Builtin::BI__builtin_fmaxf128: { |
14497 | // TODO: Handle sNaN. |
14498 | APFloat RHS(0.); |
14499 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14500 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14501 | return false; |
14502 | // When comparing zeroes, return +0.0 if one of the zeroes is positive. |
14503 | if (Result.isZero() && RHS.isZero() && Result.isNegative()) |
14504 | Result = RHS; |
14505 | else if (Result.isNaN() || RHS > Result) |
14506 | Result = RHS; |
14507 | return true; |
14508 | } |
14509 | |
14510 | case Builtin::BI__builtin_fmin: |
14511 | case Builtin::BI__builtin_fminf: |
14512 | case Builtin::BI__builtin_fminl: |
14513 | case Builtin::BI__builtin_fminf16: |
14514 | case Builtin::BI__builtin_fminf128: { |
14515 | // TODO: Handle sNaN. |
14516 | APFloat RHS(0.); |
14517 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) || |
14518 | !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info)) |
14519 | return false; |
14520 | // When comparing zeroes, return -0.0 if one of the zeroes is negative. |
14521 | if (Result.isZero() && RHS.isZero() && RHS.isNegative()) |
14522 | Result = RHS; |
14523 | else if (Result.isNaN() || RHS < Result) |
14524 | Result = RHS; |
14525 | return true; |
14526 | } |
14527 | } |
14528 | } |
14529 | |
14530 | bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) { |
14531 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14532 | ComplexValue CV; |
14533 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
14534 | return false; |
14535 | Result = CV.FloatReal; |
14536 | return true; |
14537 | } |
14538 | |
14539 | return Visit(E->getSubExpr()); |
14540 | } |
14541 | |
14542 | bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) { |
14543 | if (E->getSubExpr()->getType()->isAnyComplexType()) { |
14544 | ComplexValue CV; |
14545 | if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info)) |
14546 | return false; |
14547 | Result = CV.FloatImag; |
14548 | return true; |
14549 | } |
14550 | |
14551 | VisitIgnoredValue(E: E->getSubExpr()); |
14552 | const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType()); |
14553 | Result = llvm::APFloat::getZero(Sem); |
14554 | return true; |
14555 | } |
14556 | |
14557 | bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
14558 | switch (E->getOpcode()) { |
14559 | default: return Error(E); |
14560 | case UO_Plus: |
14561 | return EvaluateFloat(E: E->getSubExpr(), Result, Info); |
14562 | case UO_Minus: |
14563 | // In C standard, WG14 N2478 F.3 p4 |
14564 | // "the unary - raises no floating point exceptions, |
14565 | // even if the operand is signalling." |
14566 | if (!EvaluateFloat(E: E->getSubExpr(), Result, Info)) |
14567 | return false; |
14568 | Result.changeSign(); |
14569 | return true; |
14570 | } |
14571 | } |
14572 | |
14573 | bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14574 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14575 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14576 | |
14577 | APFloat RHS(0.0); |
14578 | bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info); |
14579 | if (!LHSOK && !Info.noteFailure()) |
14580 | return false; |
14581 | return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK && |
14582 | handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS); |
14583 | } |
14584 | |
14585 | bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) { |
14586 | Result = E->getValue(); |
14587 | return true; |
14588 | } |
14589 | |
14590 | bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14591 | const Expr* SubExpr = E->getSubExpr(); |
14592 | |
14593 | switch (E->getCastKind()) { |
14594 | default: |
14595 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14596 | |
14597 | case CK_IntegralToFloating: { |
14598 | APSInt IntResult; |
14599 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14600 | LO: Info.Ctx.getLangOpts()); |
14601 | return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) && |
14602 | HandleIntToFloatCast(Info, E, FPO, SubExpr->getType(), |
14603 | IntResult, E->getType(), Result); |
14604 | } |
14605 | |
14606 | case CK_FixedPointToFloating: { |
14607 | APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType())); |
14608 | if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info)) |
14609 | return false; |
14610 | Result = |
14611 | FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType())); |
14612 | return true; |
14613 | } |
14614 | |
14615 | case CK_FloatingCast: { |
14616 | if (!Visit(SubExpr)) |
14617 | return false; |
14618 | return HandleFloatToFloatCast(Info, E, SubExpr->getType(), E->getType(), |
14619 | Result); |
14620 | } |
14621 | |
14622 | case CK_FloatingComplexToReal: { |
14623 | ComplexValue V; |
14624 | if (!EvaluateComplex(E: SubExpr, Res&: V, Info)) |
14625 | return false; |
14626 | Result = V.getComplexFloatReal(); |
14627 | return true; |
14628 | } |
14629 | } |
14630 | } |
14631 | |
14632 | //===----------------------------------------------------------------------===// |
14633 | // Complex Evaluation (for float and integer) |
14634 | //===----------------------------------------------------------------------===// |
14635 | |
14636 | namespace { |
14637 | class ComplexExprEvaluator |
14638 | : public ExprEvaluatorBase<ComplexExprEvaluator> { |
14639 | ComplexValue &Result; |
14640 | |
14641 | public: |
14642 | ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result) |
14643 | : ExprEvaluatorBaseTy(info), Result(Result) {} |
14644 | |
14645 | bool Success(const APValue &V, const Expr *e) { |
14646 | Result.setFrom(V); |
14647 | return true; |
14648 | } |
14649 | |
14650 | bool ZeroInitialization(const Expr *E); |
14651 | |
14652 | //===--------------------------------------------------------------------===// |
14653 | // Visitor Methods |
14654 | //===--------------------------------------------------------------------===// |
14655 | |
14656 | bool VisitImaginaryLiteral(const ImaginaryLiteral *E); |
14657 | bool VisitCastExpr(const CastExpr *E); |
14658 | bool VisitBinaryOperator(const BinaryOperator *E); |
14659 | bool VisitUnaryOperator(const UnaryOperator *E); |
14660 | bool VisitInitListExpr(const InitListExpr *E); |
14661 | bool VisitCallExpr(const CallExpr *E); |
14662 | }; |
14663 | } // end anonymous namespace |
14664 | |
14665 | static bool EvaluateComplex(const Expr *E, ComplexValue &Result, |
14666 | EvalInfo &Info) { |
14667 | assert(!E->isValueDependent()); |
14668 | assert(E->isPRValue() && E->getType()->isAnyComplexType()); |
14669 | return ComplexExprEvaluator(Info, Result).Visit(E); |
14670 | } |
14671 | |
14672 | bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) { |
14673 | QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType(); |
14674 | if (ElemTy->isRealFloatingType()) { |
14675 | Result.makeComplexFloat(); |
14676 | APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy)); |
14677 | Result.FloatReal = Zero; |
14678 | Result.FloatImag = Zero; |
14679 | } else { |
14680 | Result.makeComplexInt(); |
14681 | APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy); |
14682 | Result.IntReal = Zero; |
14683 | Result.IntImag = Zero; |
14684 | } |
14685 | return true; |
14686 | } |
14687 | |
14688 | bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) { |
14689 | const Expr* SubExpr = E->getSubExpr(); |
14690 | |
14691 | if (SubExpr->getType()->isRealFloatingType()) { |
14692 | Result.makeComplexFloat(); |
14693 | APFloat &Imag = Result.FloatImag; |
14694 | if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info)) |
14695 | return false; |
14696 | |
14697 | Result.FloatReal = APFloat(Imag.getSemantics()); |
14698 | return true; |
14699 | } else { |
14700 | assert(SubExpr->getType()->isIntegerType() && |
14701 | "Unexpected imaginary literal." ); |
14702 | |
14703 | Result.makeComplexInt(); |
14704 | APSInt &Imag = Result.IntImag; |
14705 | if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info)) |
14706 | return false; |
14707 | |
14708 | Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned()); |
14709 | return true; |
14710 | } |
14711 | } |
14712 | |
14713 | bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) { |
14714 | |
14715 | switch (E->getCastKind()) { |
14716 | case CK_BitCast: |
14717 | case CK_BaseToDerived: |
14718 | case CK_DerivedToBase: |
14719 | case CK_UncheckedDerivedToBase: |
14720 | case CK_Dynamic: |
14721 | case CK_ToUnion: |
14722 | case CK_ArrayToPointerDecay: |
14723 | case CK_FunctionToPointerDecay: |
14724 | case CK_NullToPointer: |
14725 | case CK_NullToMemberPointer: |
14726 | case CK_BaseToDerivedMemberPointer: |
14727 | case CK_DerivedToBaseMemberPointer: |
14728 | case CK_MemberPointerToBoolean: |
14729 | case CK_ReinterpretMemberPointer: |
14730 | case CK_ConstructorConversion: |
14731 | case CK_IntegralToPointer: |
14732 | case CK_PointerToIntegral: |
14733 | case CK_PointerToBoolean: |
14734 | case CK_ToVoid: |
14735 | case CK_VectorSplat: |
14736 | case CK_IntegralCast: |
14737 | case CK_BooleanToSignedIntegral: |
14738 | case CK_IntegralToBoolean: |
14739 | case CK_IntegralToFloating: |
14740 | case CK_FloatingToIntegral: |
14741 | case CK_FloatingToBoolean: |
14742 | case CK_FloatingCast: |
14743 | case CK_CPointerToObjCPointerCast: |
14744 | case CK_BlockPointerToObjCPointerCast: |
14745 | case CK_AnyPointerToBlockPointerCast: |
14746 | case CK_ObjCObjectLValueCast: |
14747 | case CK_FloatingComplexToReal: |
14748 | case CK_FloatingComplexToBoolean: |
14749 | case CK_IntegralComplexToReal: |
14750 | case CK_IntegralComplexToBoolean: |
14751 | case CK_ARCProduceObject: |
14752 | case CK_ARCConsumeObject: |
14753 | case CK_ARCReclaimReturnedObject: |
14754 | case CK_ARCExtendBlockObject: |
14755 | case CK_CopyAndAutoreleaseBlockObject: |
14756 | case CK_BuiltinFnToFnPtr: |
14757 | case CK_ZeroToOCLOpaqueType: |
14758 | case CK_NonAtomicToAtomic: |
14759 | case CK_AddressSpaceConversion: |
14760 | case CK_IntToOCLSampler: |
14761 | case CK_FloatingToFixedPoint: |
14762 | case CK_FixedPointToFloating: |
14763 | case CK_FixedPointCast: |
14764 | case CK_FixedPointToBoolean: |
14765 | case CK_FixedPointToIntegral: |
14766 | case CK_IntegralToFixedPoint: |
14767 | case CK_MatrixCast: |
14768 | llvm_unreachable("invalid cast kind for complex value" ); |
14769 | |
14770 | case CK_LValueToRValue: |
14771 | case CK_AtomicToNonAtomic: |
14772 | case CK_NoOp: |
14773 | case CK_LValueToRValueBitCast: |
14774 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
14775 | |
14776 | case CK_Dependent: |
14777 | case CK_LValueBitCast: |
14778 | case CK_UserDefinedConversion: |
14779 | return Error(E); |
14780 | |
14781 | case CK_FloatingRealToComplex: { |
14782 | APFloat &Real = Result.FloatReal; |
14783 | if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info)) |
14784 | return false; |
14785 | |
14786 | Result.makeComplexFloat(); |
14787 | Result.FloatImag = APFloat(Real.getSemantics()); |
14788 | return true; |
14789 | } |
14790 | |
14791 | case CK_FloatingComplexCast: { |
14792 | if (!Visit(E->getSubExpr())) |
14793 | return false; |
14794 | |
14795 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14796 | QualType From |
14797 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14798 | |
14799 | return HandleFloatToFloatCast(Info, E, From, To, Result.FloatReal) && |
14800 | HandleFloatToFloatCast(Info, E, From, To, Result.FloatImag); |
14801 | } |
14802 | |
14803 | case CK_FloatingComplexToIntegralComplex: { |
14804 | if (!Visit(E->getSubExpr())) |
14805 | return false; |
14806 | |
14807 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14808 | QualType From |
14809 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14810 | Result.makeComplexInt(); |
14811 | return HandleFloatToIntCast(Info, E, From, Result.FloatReal, |
14812 | To, Result.IntReal) && |
14813 | HandleFloatToIntCast(Info, E, From, Result.FloatImag, |
14814 | To, Result.IntImag); |
14815 | } |
14816 | |
14817 | case CK_IntegralRealToComplex: { |
14818 | APSInt &Real = Result.IntReal; |
14819 | if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info)) |
14820 | return false; |
14821 | |
14822 | Result.makeComplexInt(); |
14823 | Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned()); |
14824 | return true; |
14825 | } |
14826 | |
14827 | case CK_IntegralComplexCast: { |
14828 | if (!Visit(E->getSubExpr())) |
14829 | return false; |
14830 | |
14831 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14832 | QualType From |
14833 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14834 | |
14835 | Result.IntReal = HandleIntToIntCast(Info, E, To, From, Result.IntReal); |
14836 | Result.IntImag = HandleIntToIntCast(Info, E, To, From, Result.IntImag); |
14837 | return true; |
14838 | } |
14839 | |
14840 | case CK_IntegralComplexToFloatingComplex: { |
14841 | if (!Visit(E->getSubExpr())) |
14842 | return false; |
14843 | |
14844 | const FPOptions FPO = E->getFPFeaturesInEffect( |
14845 | LO: Info.Ctx.getLangOpts()); |
14846 | QualType To = E->getType()->castAs<ComplexType>()->getElementType(); |
14847 | QualType From |
14848 | = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType(); |
14849 | Result.makeComplexFloat(); |
14850 | return HandleIntToFloatCast(Info, E, FPO, From, Result.IntReal, |
14851 | To, Result.FloatReal) && |
14852 | HandleIntToFloatCast(Info, E, FPO, From, Result.IntImag, |
14853 | To, Result.FloatImag); |
14854 | } |
14855 | } |
14856 | |
14857 | llvm_unreachable("unknown cast resulting in complex value" ); |
14858 | } |
14859 | |
14860 | bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { |
14861 | if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma) |
14862 | return ExprEvaluatorBaseTy::VisitBinaryOperator(E); |
14863 | |
14864 | // Track whether the LHS or RHS is real at the type system level. When this is |
14865 | // the case we can simplify our evaluation strategy. |
14866 | bool LHSReal = false, RHSReal = false; |
14867 | |
14868 | bool LHSOK; |
14869 | if (E->getLHS()->getType()->isRealFloatingType()) { |
14870 | LHSReal = true; |
14871 | APFloat &Real = Result.FloatReal; |
14872 | LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info); |
14873 | if (LHSOK) { |
14874 | Result.makeComplexFloat(); |
14875 | Result.FloatImag = APFloat(Real.getSemantics()); |
14876 | } |
14877 | } else { |
14878 | LHSOK = Visit(E->getLHS()); |
14879 | } |
14880 | if (!LHSOK && !Info.noteFailure()) |
14881 | return false; |
14882 | |
14883 | ComplexValue RHS; |
14884 | if (E->getRHS()->getType()->isRealFloatingType()) { |
14885 | RHSReal = true; |
14886 | APFloat &Real = RHS.FloatReal; |
14887 | if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK) |
14888 | return false; |
14889 | RHS.makeComplexFloat(); |
14890 | RHS.FloatImag = APFloat(Real.getSemantics()); |
14891 | } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK) |
14892 | return false; |
14893 | |
14894 | assert(!(LHSReal && RHSReal) && |
14895 | "Cannot have both operands of a complex operation be real." ); |
14896 | switch (E->getOpcode()) { |
14897 | default: return Error(E); |
14898 | case BO_Add: |
14899 | if (Result.isComplexFloat()) { |
14900 | Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(), |
14901 | RM: APFloat::rmNearestTiesToEven); |
14902 | if (LHSReal) |
14903 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14904 | else if (!RHSReal) |
14905 | Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(), |
14906 | RM: APFloat::rmNearestTiesToEven); |
14907 | } else { |
14908 | Result.getComplexIntReal() += RHS.getComplexIntReal(); |
14909 | Result.getComplexIntImag() += RHS.getComplexIntImag(); |
14910 | } |
14911 | break; |
14912 | case BO_Sub: |
14913 | if (Result.isComplexFloat()) { |
14914 | Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(), |
14915 | RM: APFloat::rmNearestTiesToEven); |
14916 | if (LHSReal) { |
14917 | Result.getComplexFloatImag() = RHS.getComplexFloatImag(); |
14918 | Result.getComplexFloatImag().changeSign(); |
14919 | } else if (!RHSReal) { |
14920 | Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(), |
14921 | RM: APFloat::rmNearestTiesToEven); |
14922 | } |
14923 | } else { |
14924 | Result.getComplexIntReal() -= RHS.getComplexIntReal(); |
14925 | Result.getComplexIntImag() -= RHS.getComplexIntImag(); |
14926 | } |
14927 | break; |
14928 | case BO_Mul: |
14929 | if (Result.isComplexFloat()) { |
14930 | // This is an implementation of complex multiplication according to the |
14931 | // constraints laid out in C11 Annex G. The implementation uses the |
14932 | // following naming scheme: |
14933 | // (a + ib) * (c + id) |
14934 | ComplexValue LHS = Result; |
14935 | APFloat &A = LHS.getComplexFloatReal(); |
14936 | APFloat &B = LHS.getComplexFloatImag(); |
14937 | APFloat &C = RHS.getComplexFloatReal(); |
14938 | APFloat &D = RHS.getComplexFloatImag(); |
14939 | APFloat &ResR = Result.getComplexFloatReal(); |
14940 | APFloat &ResI = Result.getComplexFloatImag(); |
14941 | if (LHSReal) { |
14942 | assert(!RHSReal && "Cannot have two real operands for a complex op!" ); |
14943 | ResR = A * C; |
14944 | ResI = A * D; |
14945 | } else if (RHSReal) { |
14946 | ResR = C * A; |
14947 | ResI = C * B; |
14948 | } else { |
14949 | // In the fully general case, we need to handle NaNs and infinities |
14950 | // robustly. |
14951 | APFloat AC = A * C; |
14952 | APFloat BD = B * D; |
14953 | APFloat AD = A * D; |
14954 | APFloat BC = B * C; |
14955 | ResR = AC - BD; |
14956 | ResI = AD + BC; |
14957 | if (ResR.isNaN() && ResI.isNaN()) { |
14958 | bool Recalc = false; |
14959 | if (A.isInfinity() || B.isInfinity()) { |
14960 | A = APFloat::copySign( |
14961 | Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), Sign: A); |
14962 | B = APFloat::copySign( |
14963 | Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), Sign: B); |
14964 | if (C.isNaN()) |
14965 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
14966 | if (D.isNaN()) |
14967 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
14968 | Recalc = true; |
14969 | } |
14970 | if (C.isInfinity() || D.isInfinity()) { |
14971 | C = APFloat::copySign( |
14972 | Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), Sign: C); |
14973 | D = APFloat::copySign( |
14974 | Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), Sign: D); |
14975 | if (A.isNaN()) |
14976 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
14977 | if (B.isNaN()) |
14978 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
14979 | Recalc = true; |
14980 | } |
14981 | if (!Recalc && (AC.isInfinity() || BD.isInfinity() || |
14982 | AD.isInfinity() || BC.isInfinity())) { |
14983 | if (A.isNaN()) |
14984 | A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A); |
14985 | if (B.isNaN()) |
14986 | B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B); |
14987 | if (C.isNaN()) |
14988 | C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C); |
14989 | if (D.isNaN()) |
14990 | D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D); |
14991 | Recalc = true; |
14992 | } |
14993 | if (Recalc) { |
14994 | ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D); |
14995 | ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C); |
14996 | } |
14997 | } |
14998 | } |
14999 | } else { |
15000 | ComplexValue LHS = Result; |
15001 | Result.getComplexIntReal() = |
15002 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() - |
15003 | LHS.getComplexIntImag() * RHS.getComplexIntImag()); |
15004 | Result.getComplexIntImag() = |
15005 | (LHS.getComplexIntReal() * RHS.getComplexIntImag() + |
15006 | LHS.getComplexIntImag() * RHS.getComplexIntReal()); |
15007 | } |
15008 | break; |
15009 | case BO_Div: |
15010 | if (Result.isComplexFloat()) { |
15011 | // This is an implementation of complex division according to the |
15012 | // constraints laid out in C11 Annex G. The implementation uses the |
15013 | // following naming scheme: |
15014 | // (a + ib) / (c + id) |
15015 | ComplexValue LHS = Result; |
15016 | APFloat &A = LHS.getComplexFloatReal(); |
15017 | APFloat &B = LHS.getComplexFloatImag(); |
15018 | APFloat &C = RHS.getComplexFloatReal(); |
15019 | APFloat &D = RHS.getComplexFloatImag(); |
15020 | APFloat &ResR = Result.getComplexFloatReal(); |
15021 | APFloat &ResI = Result.getComplexFloatImag(); |
15022 | if (RHSReal) { |
15023 | ResR = A / C; |
15024 | ResI = B / C; |
15025 | } else { |
15026 | if (LHSReal) { |
15027 | // No real optimizations we can do here, stub out with zero. |
15028 | B = APFloat::getZero(Sem: A.getSemantics()); |
15029 | } |
15030 | int DenomLogB = 0; |
15031 | APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D)); |
15032 | if (MaxCD.isFinite()) { |
15033 | DenomLogB = ilogb(Arg: MaxCD); |
15034 | C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
15035 | D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven); |
15036 | } |
15037 | APFloat Denom = C * C + D * D; |
15038 | ResR = scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, |
15039 | RM: APFloat::rmNearestTiesToEven); |
15040 | ResI = scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, |
15041 | RM: APFloat::rmNearestTiesToEven); |
15042 | if (ResR.isNaN() && ResI.isNaN()) { |
15043 | if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) { |
15044 | ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A; |
15045 | ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B; |
15046 | } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() && |
15047 | D.isFinite()) { |
15048 | A = APFloat::copySign( |
15049 | Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0), Sign: A); |
15050 | B = APFloat::copySign( |
15051 | Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0), Sign: B); |
15052 | ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D); |
15053 | ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D); |
15054 | } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) { |
15055 | C = APFloat::copySign( |
15056 | Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0), Sign: C); |
15057 | D = APFloat::copySign( |
15058 | Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0), Sign: D); |
15059 | ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D); |
15060 | ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D); |
15061 | } |
15062 | } |
15063 | } |
15064 | } else { |
15065 | if (RHS.getComplexIntReal() == 0 && RHS.getComplexIntImag() == 0) |
15066 | return Error(E, diag::note_expr_divide_by_zero); |
15067 | |
15068 | ComplexValue LHS = Result; |
15069 | APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() + |
15070 | RHS.getComplexIntImag() * RHS.getComplexIntImag(); |
15071 | Result.getComplexIntReal() = |
15072 | (LHS.getComplexIntReal() * RHS.getComplexIntReal() + |
15073 | LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den; |
15074 | Result.getComplexIntImag() = |
15075 | (LHS.getComplexIntImag() * RHS.getComplexIntReal() - |
15076 | LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den; |
15077 | } |
15078 | break; |
15079 | } |
15080 | |
15081 | return true; |
15082 | } |
15083 | |
15084 | bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) { |
15085 | // Get the operand value into 'Result'. |
15086 | if (!Visit(E->getSubExpr())) |
15087 | return false; |
15088 | |
15089 | switch (E->getOpcode()) { |
15090 | default: |
15091 | return Error(E); |
15092 | case UO_Extension: |
15093 | return true; |
15094 | case UO_Plus: |
15095 | // The result is always just the subexpr. |
15096 | return true; |
15097 | case UO_Minus: |
15098 | if (Result.isComplexFloat()) { |
15099 | Result.getComplexFloatReal().changeSign(); |
15100 | Result.getComplexFloatImag().changeSign(); |
15101 | } |
15102 | else { |
15103 | Result.getComplexIntReal() = -Result.getComplexIntReal(); |
15104 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
15105 | } |
15106 | return true; |
15107 | case UO_Not: |
15108 | if (Result.isComplexFloat()) |
15109 | Result.getComplexFloatImag().changeSign(); |
15110 | else |
15111 | Result.getComplexIntImag() = -Result.getComplexIntImag(); |
15112 | return true; |
15113 | } |
15114 | } |
15115 | |
15116 | bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) { |
15117 | if (E->getNumInits() == 2) { |
15118 | if (E->getType()->isComplexType()) { |
15119 | Result.makeComplexFloat(); |
15120 | if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info)) |
15121 | return false; |
15122 | if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info)) |
15123 | return false; |
15124 | } else { |
15125 | Result.makeComplexInt(); |
15126 | if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info)) |
15127 | return false; |
15128 | if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info)) |
15129 | return false; |
15130 | } |
15131 | return true; |
15132 | } |
15133 | return ExprEvaluatorBaseTy::VisitInitListExpr(E); |
15134 | } |
15135 | |
15136 | bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) { |
15137 | if (!IsConstantEvaluatedBuiltinCall(E)) |
15138 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
15139 | |
15140 | switch (E->getBuiltinCallee()) { |
15141 | case Builtin::BI__builtin_complex: |
15142 | Result.makeComplexFloat(); |
15143 | if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info)) |
15144 | return false; |
15145 | if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info)) |
15146 | return false; |
15147 | return true; |
15148 | |
15149 | default: |
15150 | return false; |
15151 | } |
15152 | } |
15153 | |
15154 | //===----------------------------------------------------------------------===// |
15155 | // Atomic expression evaluation, essentially just handling the NonAtomicToAtomic |
15156 | // implicit conversion. |
15157 | //===----------------------------------------------------------------------===// |
15158 | |
15159 | namespace { |
15160 | class AtomicExprEvaluator : |
15161 | public ExprEvaluatorBase<AtomicExprEvaluator> { |
15162 | const LValue *This; |
15163 | APValue &Result; |
15164 | public: |
15165 | AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result) |
15166 | : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {} |
15167 | |
15168 | bool Success(const APValue &V, const Expr *E) { |
15169 | Result = V; |
15170 | return true; |
15171 | } |
15172 | |
15173 | bool ZeroInitialization(const Expr *E) { |
15174 | ImplicitValueInitExpr VIE( |
15175 | E->getType()->castAs<AtomicType>()->getValueType()); |
15176 | // For atomic-qualified class (and array) types in C++, initialize the |
15177 | // _Atomic-wrapped subobject directly, in-place. |
15178 | return This ? EvaluateInPlace(Result, Info, *This, &VIE) |
15179 | : Evaluate(Result, Info, &VIE); |
15180 | } |
15181 | |
15182 | bool VisitCastExpr(const CastExpr *E) { |
15183 | switch (E->getCastKind()) { |
15184 | default: |
15185 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15186 | case CK_NullToPointer: |
15187 | VisitIgnoredValue(E: E->getSubExpr()); |
15188 | return ZeroInitialization(E); |
15189 | case CK_NonAtomicToAtomic: |
15190 | return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr()) |
15191 | : Evaluate(Result, Info, E: E->getSubExpr()); |
15192 | } |
15193 | } |
15194 | }; |
15195 | } // end anonymous namespace |
15196 | |
15197 | static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result, |
15198 | EvalInfo &Info) { |
15199 | assert(!E->isValueDependent()); |
15200 | assert(E->isPRValue() && E->getType()->isAtomicType()); |
15201 | return AtomicExprEvaluator(Info, This, Result).Visit(E); |
15202 | } |
15203 | |
15204 | //===----------------------------------------------------------------------===// |
15205 | // Void expression evaluation, primarily for a cast to void on the LHS of a |
15206 | // comma operator |
15207 | //===----------------------------------------------------------------------===// |
15208 | |
15209 | namespace { |
15210 | class VoidExprEvaluator |
15211 | : public ExprEvaluatorBase<VoidExprEvaluator> { |
15212 | public: |
15213 | VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {} |
15214 | |
15215 | bool Success(const APValue &V, const Expr *e) { return true; } |
15216 | |
15217 | bool ZeroInitialization(const Expr *E) { return true; } |
15218 | |
15219 | bool VisitCastExpr(const CastExpr *E) { |
15220 | switch (E->getCastKind()) { |
15221 | default: |
15222 | return ExprEvaluatorBaseTy::VisitCastExpr(E); |
15223 | case CK_ToVoid: |
15224 | VisitIgnoredValue(E: E->getSubExpr()); |
15225 | return true; |
15226 | } |
15227 | } |
15228 | |
15229 | bool VisitCallExpr(const CallExpr *E) { |
15230 | if (!IsConstantEvaluatedBuiltinCall(E)) |
15231 | return ExprEvaluatorBaseTy::VisitCallExpr(E); |
15232 | |
15233 | switch (E->getBuiltinCallee()) { |
15234 | case Builtin::BI__assume: |
15235 | case Builtin::BI__builtin_assume: |
15236 | // The argument is not evaluated! |
15237 | return true; |
15238 | |
15239 | case Builtin::BI__builtin_operator_delete: |
15240 | return HandleOperatorDeleteCall(Info, E); |
15241 | |
15242 | default: |
15243 | return false; |
15244 | } |
15245 | } |
15246 | |
15247 | bool VisitCXXDeleteExpr(const CXXDeleteExpr *E); |
15248 | }; |
15249 | } // end anonymous namespace |
15250 | |
15251 | bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) { |
15252 | // We cannot speculatively evaluate a delete expression. |
15253 | if (Info.SpeculativeEvaluationDepth) |
15254 | return false; |
15255 | |
15256 | FunctionDecl *OperatorDelete = E->getOperatorDelete(); |
15257 | if (!OperatorDelete->isReplaceableGlobalAllocationFunction()) { |
15258 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
15259 | << isa<CXXMethodDecl>(OperatorDelete) << OperatorDelete; |
15260 | return false; |
15261 | } |
15262 | |
15263 | const Expr *Arg = E->getArgument(); |
15264 | |
15265 | LValue Pointer; |
15266 | if (!EvaluatePointer(E: Arg, Result&: Pointer, Info)) |
15267 | return false; |
15268 | if (Pointer.Designator.Invalid) |
15269 | return false; |
15270 | |
15271 | // Deleting a null pointer has no effect. |
15272 | if (Pointer.isNullPointer()) { |
15273 | // This is the only case where we need to produce an extension warning: |
15274 | // the only other way we can succeed is if we find a dynamic allocation, |
15275 | // and we will have warned when we allocated it in that case. |
15276 | if (!Info.getLangOpts().CPlusPlus20) |
15277 | Info.CCEDiag(E, diag::note_constexpr_new); |
15278 | return true; |
15279 | } |
15280 | |
15281 | std::optional<DynAlloc *> Alloc = CheckDeleteKind( |
15282 | Info, E, Pointer, E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New); |
15283 | if (!Alloc) |
15284 | return false; |
15285 | QualType AllocType = Pointer.Base.getDynamicAllocType(); |
15286 | |
15287 | // For the non-array case, the designator must be empty if the static type |
15288 | // does not have a virtual destructor. |
15289 | if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 && |
15290 | !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) { |
15291 | Info.FFDiag(E, diag::note_constexpr_delete_base_nonvirt_dtor) |
15292 | << Arg->getType()->getPointeeType() << AllocType; |
15293 | return false; |
15294 | } |
15295 | |
15296 | // For a class type with a virtual destructor, the selected operator delete |
15297 | // is the one looked up when building the destructor. |
15298 | if (!E->isArrayForm() && !E->isGlobalDelete()) { |
15299 | const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType); |
15300 | if (VirtualDelete && |
15301 | !VirtualDelete->isReplaceableGlobalAllocationFunction()) { |
15302 | Info.FFDiag(E, diag::note_constexpr_new_non_replaceable) |
15303 | << isa<CXXMethodDecl>(VirtualDelete) << VirtualDelete; |
15304 | return false; |
15305 | } |
15306 | } |
15307 | |
15308 | if (!HandleDestruction(Info, E->getExprLoc(), Pointer.getLValueBase(), |
15309 | (*Alloc)->Value, AllocType)) |
15310 | return false; |
15311 | |
15312 | if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) { |
15313 | // The element was already erased. This means the destructor call also |
15314 | // deleted the object. |
15315 | // FIXME: This probably results in undefined behavior before we get this |
15316 | // far, and should be diagnosed elsewhere first. |
15317 | Info.FFDiag(E, diag::note_constexpr_double_delete); |
15318 | return false; |
15319 | } |
15320 | |
15321 | return true; |
15322 | } |
15323 | |
15324 | static bool EvaluateVoid(const Expr *E, EvalInfo &Info) { |
15325 | assert(!E->isValueDependent()); |
15326 | assert(E->isPRValue() && E->getType()->isVoidType()); |
15327 | return VoidExprEvaluator(Info).Visit(E); |
15328 | } |
15329 | |
15330 | //===----------------------------------------------------------------------===// |
15331 | // Top level Expr::EvaluateAsRValue method. |
15332 | //===----------------------------------------------------------------------===// |
15333 | |
15334 | static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) { |
15335 | assert(!E->isValueDependent()); |
15336 | // In C, function designators are not lvalues, but we evaluate them as if they |
15337 | // are. |
15338 | QualType T = E->getType(); |
15339 | if (E->isGLValue() || T->isFunctionType()) { |
15340 | LValue LV; |
15341 | if (!EvaluateLValue(E, Result&: LV, Info)) |
15342 | return false; |
15343 | LV.moveInto(V&: Result); |
15344 | } else if (T->isVectorType()) { |
15345 | if (!EvaluateVector(E, Result, Info)) |
15346 | return false; |
15347 | } else if (T->isIntegralOrEnumerationType()) { |
15348 | if (!IntExprEvaluator(Info, Result).Visit(E)) |
15349 | return false; |
15350 | } else if (T->hasPointerRepresentation()) { |
15351 | LValue LV; |
15352 | if (!EvaluatePointer(E, Result&: LV, Info)) |
15353 | return false; |
15354 | LV.moveInto(V&: Result); |
15355 | } else if (T->isRealFloatingType()) { |
15356 | llvm::APFloat F(0.0); |
15357 | if (!EvaluateFloat(E, Result&: F, Info)) |
15358 | return false; |
15359 | Result = APValue(F); |
15360 | } else if (T->isAnyComplexType()) { |
15361 | ComplexValue C; |
15362 | if (!EvaluateComplex(E, Result&: C, Info)) |
15363 | return false; |
15364 | C.moveInto(v&: Result); |
15365 | } else if (T->isFixedPointType()) { |
15366 | if (!FixedPointExprEvaluator(Info, Result).Visit(E)) return false; |
15367 | } else if (T->isMemberPointerType()) { |
15368 | MemberPtr P; |
15369 | if (!EvaluateMemberPointer(E, Result&: P, Info)) |
15370 | return false; |
15371 | P.moveInto(V&: Result); |
15372 | return true; |
15373 | } else if (T->isArrayType()) { |
15374 | LValue LV; |
15375 | APValue &Value = |
15376 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
15377 | if (!EvaluateArray(E, This: LV, Result&: Value, Info)) |
15378 | return false; |
15379 | Result = Value; |
15380 | } else if (T->isRecordType()) { |
15381 | LValue LV; |
15382 | APValue &Value = |
15383 | Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV); |
15384 | if (!EvaluateRecord(E, This: LV, Result&: Value, Info)) |
15385 | return false; |
15386 | Result = Value; |
15387 | } else if (T->isVoidType()) { |
15388 | if (!Info.getLangOpts().CPlusPlus11) |
15389 | Info.CCEDiag(E, diag::note_constexpr_nonliteral) |
15390 | << E->getType(); |
15391 | if (!EvaluateVoid(E, Info)) |
15392 | return false; |
15393 | } else if (T->isAtomicType()) { |
15394 | QualType Unqual = T.getAtomicUnqualifiedType(); |
15395 | if (Unqual->isArrayType() || Unqual->isRecordType()) { |
15396 | LValue LV; |
15397 | APValue &Value = Info.CurrentCall->createTemporary( |
15398 | Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV); |
15399 | if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info)) |
15400 | return false; |
15401 | Result = Value; |
15402 | } else { |
15403 | if (!EvaluateAtomic(E, This: nullptr, Result, Info)) |
15404 | return false; |
15405 | } |
15406 | } else if (Info.getLangOpts().CPlusPlus11) { |
15407 | Info.FFDiag(E, diag::note_constexpr_nonliteral) << E->getType(); |
15408 | return false; |
15409 | } else { |
15410 | Info.FFDiag(E, diag::note_invalid_subexpr_in_const_expr); |
15411 | return false; |
15412 | } |
15413 | |
15414 | return true; |
15415 | } |
15416 | |
15417 | /// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some |
15418 | /// cases, the in-place evaluation is essential, since later initializers for |
15419 | /// an object can indirectly refer to subobjects which were initialized earlier. |
15420 | static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This, |
15421 | const Expr *E, bool AllowNonLiteralTypes) { |
15422 | assert(!E->isValueDependent()); |
15423 | |
15424 | if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This)) |
15425 | return false; |
15426 | |
15427 | if (E->isPRValue()) { |
15428 | // Evaluate arrays and record types in-place, so that later initializers can |
15429 | // refer to earlier-initialized members of the object. |
15430 | QualType T = E->getType(); |
15431 | if (T->isArrayType()) |
15432 | return EvaluateArray(E, This, Result, Info); |
15433 | else if (T->isRecordType()) |
15434 | return EvaluateRecord(E, This, Result, Info); |
15435 | else if (T->isAtomicType()) { |
15436 | QualType Unqual = T.getAtomicUnqualifiedType(); |
15437 | if (Unqual->isArrayType() || Unqual->isRecordType()) |
15438 | return EvaluateAtomic(E, This: &This, Result, Info); |
15439 | } |
15440 | } |
15441 | |
15442 | // For any other type, in-place evaluation is unimportant. |
15443 | return Evaluate(Result, Info, E); |
15444 | } |
15445 | |
15446 | /// EvaluateAsRValue - Try to evaluate this expression, performing an implicit |
15447 | /// lvalue-to-rvalue cast if it is an lvalue. |
15448 | static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) { |
15449 | assert(!E->isValueDependent()); |
15450 | |
15451 | if (E->getType().isNull()) |
15452 | return false; |
15453 | |
15454 | if (!CheckLiteralType(Info, E)) |
15455 | return false; |
15456 | |
15457 | if (Info.EnableNewConstInterp) { |
15458 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result)) |
15459 | return false; |
15460 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
15461 | Kind: ConstantExprKind::Normal); |
15462 | } |
15463 | |
15464 | if (!::Evaluate(Result, Info, E)) |
15465 | return false; |
15466 | |
15467 | // Implicit lvalue-to-rvalue cast. |
15468 | if (E->isGLValue()) { |
15469 | LValue LV; |
15470 | LV.setFrom(Ctx&: Info.Ctx, V: Result); |
15471 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result)) |
15472 | return false; |
15473 | } |
15474 | |
15475 | // Check this core constant expression is a constant expression. |
15476 | return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result, |
15477 | Kind: ConstantExprKind::Normal) && |
15478 | CheckMemoryLeaks(Info); |
15479 | } |
15480 | |
15481 | static bool FastEvaluateAsRValue(const Expr *Exp, Expr::EvalResult &Result, |
15482 | const ASTContext &Ctx, bool &IsConst) { |
15483 | // Fast-path evaluations of integer literals, since we sometimes see files |
15484 | // containing vast quantities of these. |
15485 | if (const IntegerLiteral *L = dyn_cast<IntegerLiteral>(Val: Exp)) { |
15486 | Result.Val = APValue(APSInt(L->getValue(), |
15487 | L->getType()->isUnsignedIntegerType())); |
15488 | IsConst = true; |
15489 | return true; |
15490 | } |
15491 | |
15492 | if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) { |
15493 | Result.Val = APValue(APSInt(APInt(1, L->getValue()))); |
15494 | IsConst = true; |
15495 | return true; |
15496 | } |
15497 | |
15498 | if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) { |
15499 | if (CE->hasAPValueResult()) { |
15500 | Result.Val = CE->getAPValueResult(); |
15501 | IsConst = true; |
15502 | return true; |
15503 | } |
15504 | |
15505 | // The SubExpr is usually just an IntegerLiteral. |
15506 | return FastEvaluateAsRValue(CE->getSubExpr(), Result, Ctx, IsConst); |
15507 | } |
15508 | |
15509 | // This case should be rare, but we need to check it before we check on |
15510 | // the type below. |
15511 | if (Exp->getType().isNull()) { |
15512 | IsConst = false; |
15513 | return true; |
15514 | } |
15515 | |
15516 | return false; |
15517 | } |
15518 | |
15519 | static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result, |
15520 | Expr::SideEffectsKind SEK) { |
15521 | return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) || |
15522 | (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior); |
15523 | } |
15524 | |
15525 | static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result, |
15526 | const ASTContext &Ctx, EvalInfo &Info) { |
15527 | assert(!E->isValueDependent()); |
15528 | bool IsConst; |
15529 | if (FastEvaluateAsRValue(Exp: E, Result, Ctx, IsConst)) |
15530 | return IsConst; |
15531 | |
15532 | return EvaluateAsRValue(Info, E, Result&: Result.Val); |
15533 | } |
15534 | |
15535 | static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult, |
15536 | const ASTContext &Ctx, |
15537 | Expr::SideEffectsKind AllowSideEffects, |
15538 | EvalInfo &Info) { |
15539 | assert(!E->isValueDependent()); |
15540 | if (!E->getType()->isIntegralOrEnumerationType()) |
15541 | return false; |
15542 | |
15543 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) || |
15544 | !ExprResult.Val.isInt() || |
15545 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15546 | return false; |
15547 | |
15548 | return true; |
15549 | } |
15550 | |
15551 | static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult, |
15552 | const ASTContext &Ctx, |
15553 | Expr::SideEffectsKind AllowSideEffects, |
15554 | EvalInfo &Info) { |
15555 | assert(!E->isValueDependent()); |
15556 | if (!E->getType()->isFixedPointType()) |
15557 | return false; |
15558 | |
15559 | if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info)) |
15560 | return false; |
15561 | |
15562 | if (!ExprResult.Val.isFixedPoint() || |
15563 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15564 | return false; |
15565 | |
15566 | return true; |
15567 | } |
15568 | |
15569 | /// EvaluateAsRValue - Return true if this is a constant which we can fold using |
15570 | /// any crazy technique (that has nothing to do with language standards) that |
15571 | /// we want to. If this function returns true, it returns the folded constant |
15572 | /// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion |
15573 | /// will be applied to the result. |
15574 | bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx, |
15575 | bool InConstantContext) const { |
15576 | assert(!isValueDependent() && |
15577 | "Expression evaluator can't be called on a dependent expression." ); |
15578 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue" ); |
15579 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15580 | Info.InConstantContext = InConstantContext; |
15581 | return ::EvaluateAsRValue(E: this, Result, Ctx, Info); |
15582 | } |
15583 | |
15584 | bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx, |
15585 | bool InConstantContext) const { |
15586 | assert(!isValueDependent() && |
15587 | "Expression evaluator can't be called on a dependent expression." ); |
15588 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition" ); |
15589 | EvalResult Scratch; |
15590 | return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) && |
15591 | HandleConversionToBool(Val: Scratch.Val, Result); |
15592 | } |
15593 | |
15594 | bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, |
15595 | SideEffectsKind AllowSideEffects, |
15596 | bool InConstantContext) const { |
15597 | assert(!isValueDependent() && |
15598 | "Expression evaluator can't be called on a dependent expression." ); |
15599 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt" ); |
15600 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15601 | Info.InConstantContext = InConstantContext; |
15602 | return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
15603 | } |
15604 | |
15605 | bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx, |
15606 | SideEffectsKind AllowSideEffects, |
15607 | bool InConstantContext) const { |
15608 | assert(!isValueDependent() && |
15609 | "Expression evaluator can't be called on a dependent expression." ); |
15610 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint" ); |
15611 | EvalInfo Info(Ctx, Result, EvalInfo::EM_IgnoreSideEffects); |
15612 | Info.InConstantContext = InConstantContext; |
15613 | return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info); |
15614 | } |
15615 | |
15616 | bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx, |
15617 | SideEffectsKind AllowSideEffects, |
15618 | bool InConstantContext) const { |
15619 | assert(!isValueDependent() && |
15620 | "Expression evaluator can't be called on a dependent expression." ); |
15621 | |
15622 | if (!getType()->isRealFloatingType()) |
15623 | return false; |
15624 | |
15625 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat" ); |
15626 | EvalResult ExprResult; |
15627 | if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) || |
15628 | !ExprResult.Val.isFloat() || |
15629 | hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects)) |
15630 | return false; |
15631 | |
15632 | Result = ExprResult.Val.getFloat(); |
15633 | return true; |
15634 | } |
15635 | |
15636 | bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx, |
15637 | bool InConstantContext) const { |
15638 | assert(!isValueDependent() && |
15639 | "Expression evaluator can't be called on a dependent expression." ); |
15640 | |
15641 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue" ); |
15642 | EvalInfo Info(Ctx, Result, EvalInfo::EM_ConstantFold); |
15643 | Info.InConstantContext = InConstantContext; |
15644 | LValue LV; |
15645 | CheckedTemporaries CheckedTemps; |
15646 | if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() || |
15647 | Result.HasSideEffects || |
15648 | !CheckLValueConstantExpression(Info, Loc: getExprLoc(), |
15649 | Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV, |
15650 | Kind: ConstantExprKind::Normal, CheckedTemps)) |
15651 | return false; |
15652 | |
15653 | LV.moveInto(V&: Result.Val); |
15654 | return true; |
15655 | } |
15656 | |
15657 | static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base, |
15658 | APValue DestroyedValue, QualType Type, |
15659 | SourceLocation Loc, Expr::EvalStatus &EStatus, |
15660 | bool IsConstantDestruction) { |
15661 | EvalInfo Info(Ctx, EStatus, |
15662 | IsConstantDestruction ? EvalInfo::EM_ConstantExpression |
15663 | : EvalInfo::EM_ConstantFold); |
15664 | Info.setEvaluatingDecl(Base, Value&: DestroyedValue, |
15665 | EDK: EvalInfo::EvaluatingDeclKind::Dtor); |
15666 | Info.InConstantContext = IsConstantDestruction; |
15667 | |
15668 | LValue LVal; |
15669 | LVal.set(B: Base); |
15670 | |
15671 | if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) || |
15672 | EStatus.HasSideEffects) |
15673 | return false; |
15674 | |
15675 | if (!Info.discardCleanups()) |
15676 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15677 | |
15678 | return true; |
15679 | } |
15680 | |
15681 | bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx, |
15682 | ConstantExprKind Kind) const { |
15683 | assert(!isValueDependent() && |
15684 | "Expression evaluator can't be called on a dependent expression." ); |
15685 | bool IsConst; |
15686 | if (FastEvaluateAsRValue(Exp: this, Result, Ctx, IsConst) && Result.Val.hasValue()) |
15687 | return true; |
15688 | |
15689 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr" ); |
15690 | EvalInfo::EvaluationMode EM = EvalInfo::EM_ConstantExpression; |
15691 | EvalInfo Info(Ctx, Result, EM); |
15692 | Info.InConstantContext = true; |
15693 | |
15694 | if (Info.EnableNewConstInterp) { |
15695 | if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val)) |
15696 | return false; |
15697 | return CheckConstantExpression(Info, DiagLoc: getExprLoc(), |
15698 | Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind); |
15699 | } |
15700 | |
15701 | // The type of the object we're initializing is 'const T' for a class NTTP. |
15702 | QualType T = getType(); |
15703 | if (Kind == ConstantExprKind::ClassTemplateArgument) |
15704 | T.addConst(); |
15705 | |
15706 | // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to |
15707 | // represent the result of the evaluation. CheckConstantExpression ensures |
15708 | // this doesn't escape. |
15709 | MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true); |
15710 | APValue::LValueBase Base(&BaseMTE); |
15711 | Info.setEvaluatingDecl(Base, Value&: Result.Val); |
15712 | |
15713 | if (Info.EnableNewConstInterp) { |
15714 | if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E: this, Result&: Result.Val)) |
15715 | return false; |
15716 | } else { |
15717 | LValue LVal; |
15718 | LVal.set(B: Base); |
15719 | // C++23 [intro.execution]/p5 |
15720 | // A full-expression is [...] a constant-expression |
15721 | // So we need to make sure temporary objects are destroyed after having |
15722 | // evaluating the expression (per C++23 [class.temporary]/p4). |
15723 | FullExpressionRAII Scope(Info); |
15724 | if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) || |
15725 | Result.HasSideEffects || !Scope.destroy()) |
15726 | return false; |
15727 | |
15728 | if (!Info.discardCleanups()) |
15729 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15730 | } |
15731 | |
15732 | if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this), |
15733 | Value: Result.Val, Kind)) |
15734 | return false; |
15735 | if (!CheckMemoryLeaks(Info)) |
15736 | return false; |
15737 | |
15738 | // If this is a class template argument, it's required to have constant |
15739 | // destruction too. |
15740 | if (Kind == ConstantExprKind::ClassTemplateArgument && |
15741 | (!EvaluateDestruction(Ctx, Base, Result.Val, T, getBeginLoc(), Result, |
15742 | true) || |
15743 | Result.HasSideEffects)) { |
15744 | // FIXME: Prefix a note to indicate that the problem is lack of constant |
15745 | // destruction. |
15746 | return false; |
15747 | } |
15748 | |
15749 | return true; |
15750 | } |
15751 | |
15752 | bool Expr::EvaluateAsInitializer(APValue &Value, const ASTContext &Ctx, |
15753 | const VarDecl *VD, |
15754 | SmallVectorImpl<PartialDiagnosticAt> &Notes, |
15755 | bool IsConstantInitialization) const { |
15756 | assert(!isValueDependent() && |
15757 | "Expression evaluator can't be called on a dependent expression." ); |
15758 | |
15759 | llvm::TimeTraceScope TimeScope("EvaluateAsInitializer" , [&] { |
15760 | std::string Name; |
15761 | llvm::raw_string_ostream OS(Name); |
15762 | VD->printQualifiedName(OS); |
15763 | return Name; |
15764 | }); |
15765 | |
15766 | Expr::EvalStatus EStatus; |
15767 | EStatus.Diag = &Notes; |
15768 | |
15769 | EvalInfo Info(Ctx, EStatus, |
15770 | (IsConstantInitialization && Ctx.getLangOpts().CPlusPlus) |
15771 | ? EvalInfo::EM_ConstantExpression |
15772 | : EvalInfo::EM_ConstantFold); |
15773 | Info.setEvaluatingDecl(VD, Value); |
15774 | Info.InConstantContext = IsConstantInitialization; |
15775 | |
15776 | SourceLocation DeclLoc = VD->getLocation(); |
15777 | QualType DeclTy = VD->getType(); |
15778 | |
15779 | if (Info.EnableNewConstInterp) { |
15780 | auto &InterpCtx = const_cast<ASTContext &>(Ctx).getInterpContext(); |
15781 | if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Result&: Value)) |
15782 | return false; |
15783 | |
15784 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
15785 | Kind: ConstantExprKind::Normal); |
15786 | } else { |
15787 | LValue LVal; |
15788 | LVal.set(VD); |
15789 | |
15790 | { |
15791 | // C++23 [intro.execution]/p5 |
15792 | // A full-expression is ... an init-declarator ([dcl.decl]) or a |
15793 | // mem-initializer. |
15794 | // So we need to make sure temporary objects are destroyed after having |
15795 | // evaluated the expression (per C++23 [class.temporary]/p4). |
15796 | // |
15797 | // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the |
15798 | // serialization code calls ParmVarDecl::getDefaultArg() which strips the |
15799 | // outermost FullExpr, such as ExprWithCleanups. |
15800 | FullExpressionRAII Scope(Info); |
15801 | if (!EvaluateInPlace(Result&: Value, Info, This: LVal, E: this, |
15802 | /*AllowNonLiteralTypes=*/true) || |
15803 | EStatus.HasSideEffects) |
15804 | return false; |
15805 | } |
15806 | |
15807 | // At this point, any lifetime-extended temporaries are completely |
15808 | // initialized. |
15809 | Info.performLifetimeExtension(); |
15810 | |
15811 | if (!Info.discardCleanups()) |
15812 | llvm_unreachable("Unhandled cleanup; missing full expression marker?" ); |
15813 | } |
15814 | |
15815 | return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value, |
15816 | Kind: ConstantExprKind::Normal) && |
15817 | CheckMemoryLeaks(Info); |
15818 | } |
15819 | |
15820 | bool VarDecl::evaluateDestruction( |
15821 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
15822 | Expr::EvalStatus EStatus; |
15823 | EStatus.Diag = &Notes; |
15824 | |
15825 | // Only treat the destruction as constant destruction if we formally have |
15826 | // constant initialization (or are usable in a constant expression). |
15827 | bool IsConstantDestruction = hasConstantInitialization(); |
15828 | |
15829 | // Make a copy of the value for the destructor to mutate, if we know it. |
15830 | // Otherwise, treat the value as default-initialized; if the destructor works |
15831 | // anyway, then the destruction is constant (and must be essentially empty). |
15832 | APValue DestroyedValue; |
15833 | if (getEvaluatedValue() && !getEvaluatedValue()->isAbsent()) |
15834 | DestroyedValue = *getEvaluatedValue(); |
15835 | else if (!handleDefaultInitValue(getType(), DestroyedValue)) |
15836 | return false; |
15837 | |
15838 | if (!EvaluateDestruction(getASTContext(), this, std::move(DestroyedValue), |
15839 | getType(), getLocation(), EStatus, |
15840 | IsConstantDestruction) || |
15841 | EStatus.HasSideEffects) |
15842 | return false; |
15843 | |
15844 | ensureEvaluatedStmt()->HasConstantDestruction = true; |
15845 | return true; |
15846 | } |
15847 | |
15848 | /// isEvaluatable - Call EvaluateAsRValue to see if this expression can be |
15849 | /// constant folded, but discard the result. |
15850 | bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const { |
15851 | assert(!isValueDependent() && |
15852 | "Expression evaluator can't be called on a dependent expression." ); |
15853 | |
15854 | EvalResult Result; |
15855 | return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) && |
15856 | !hasUnacceptableSideEffect(Result, SEK); |
15857 | } |
15858 | |
15859 | APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx, |
15860 | SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
15861 | assert(!isValueDependent() && |
15862 | "Expression evaluator can't be called on a dependent expression." ); |
15863 | |
15864 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt" ); |
15865 | EvalResult EVResult; |
15866 | EVResult.Diag = Diag; |
15867 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
15868 | Info.InConstantContext = true; |
15869 | |
15870 | bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info); |
15871 | (void)Result; |
15872 | assert(Result && "Could not evaluate expression" ); |
15873 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer" ); |
15874 | |
15875 | return EVResult.Val.getInt(); |
15876 | } |
15877 | |
15878 | APSInt Expr::EvaluateKnownConstIntCheckOverflow( |
15879 | const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const { |
15880 | assert(!isValueDependent() && |
15881 | "Expression evaluator can't be called on a dependent expression." ); |
15882 | |
15883 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow" ); |
15884 | EvalResult EVResult; |
15885 | EVResult.Diag = Diag; |
15886 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
15887 | Info.InConstantContext = true; |
15888 | Info.CheckingForUndefinedBehavior = true; |
15889 | |
15890 | bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
15891 | (void)Result; |
15892 | assert(Result && "Could not evaluate expression" ); |
15893 | assert(EVResult.Val.isInt() && "Expression did not evaluate to integer" ); |
15894 | |
15895 | return EVResult.Val.getInt(); |
15896 | } |
15897 | |
15898 | void Expr::EvaluateForOverflow(const ASTContext &Ctx) const { |
15899 | assert(!isValueDependent() && |
15900 | "Expression evaluator can't be called on a dependent expression." ); |
15901 | |
15902 | ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow" ); |
15903 | bool IsConst; |
15904 | EvalResult EVResult; |
15905 | if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult, Ctx, IsConst)) { |
15906 | EvalInfo Info(Ctx, EVResult, EvalInfo::EM_IgnoreSideEffects); |
15907 | Info.CheckingForUndefinedBehavior = true; |
15908 | (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val); |
15909 | } |
15910 | } |
15911 | |
15912 | bool Expr::EvalResult::isGlobalLValue() const { |
15913 | assert(Val.isLValue()); |
15914 | return IsGlobalLValue(B: Val.getLValueBase()); |
15915 | } |
15916 | |
15917 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
15918 | /// an integer constant expression. |
15919 | |
15920 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
15921 | /// comma, etc |
15922 | |
15923 | // CheckICE - This function does the fundamental ICE checking: the returned |
15924 | // ICEDiag contains an ICEKind indicating whether the expression is an ICE, |
15925 | // and a (possibly null) SourceLocation indicating the location of the problem. |
15926 | // |
15927 | // Note that to reduce code duplication, this helper does no evaluation |
15928 | // itself; the caller checks whether the expression is evaluatable, and |
15929 | // in the rare cases where CheckICE actually cares about the evaluated |
15930 | // value, it calls into Evaluate. |
15931 | |
15932 | namespace { |
15933 | |
15934 | enum ICEKind { |
15935 | /// This expression is an ICE. |
15936 | IK_ICE, |
15937 | /// This expression is not an ICE, but if it isn't evaluated, it's |
15938 | /// a legal subexpression for an ICE. This return value is used to handle |
15939 | /// the comma operator in C99 mode, and non-constant subexpressions. |
15940 | IK_ICEIfUnevaluated, |
15941 | /// This expression is not an ICE, and is not a legal subexpression for one. |
15942 | IK_NotICE |
15943 | }; |
15944 | |
15945 | struct ICEDiag { |
15946 | ICEKind Kind; |
15947 | SourceLocation Loc; |
15948 | |
15949 | ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {} |
15950 | }; |
15951 | |
15952 | } |
15953 | |
15954 | static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); } |
15955 | |
15956 | static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; } |
15957 | |
15958 | static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) { |
15959 | Expr::EvalResult EVResult; |
15960 | Expr::EvalStatus Status; |
15961 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
15962 | |
15963 | Info.InConstantContext = true; |
15964 | if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects || |
15965 | !EVResult.Val.isInt()) |
15966 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15967 | |
15968 | return NoDiag(); |
15969 | } |
15970 | |
15971 | static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) { |
15972 | assert(!E->isValueDependent() && "Should not see value dependent exprs!" ); |
15973 | if (!E->getType()->isIntegralOrEnumerationType()) |
15974 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
15975 | |
15976 | switch (E->getStmtClass()) { |
15977 | #define ABSTRACT_STMT(Node) |
15978 | #define STMT(Node, Base) case Expr::Node##Class: |
15979 | #define EXPR(Node, Base) |
15980 | #include "clang/AST/StmtNodes.inc" |
15981 | case Expr::PredefinedExprClass: |
15982 | case Expr::FloatingLiteralClass: |
15983 | case Expr::ImaginaryLiteralClass: |
15984 | case Expr::StringLiteralClass: |
15985 | case Expr::ArraySubscriptExprClass: |
15986 | case Expr::MatrixSubscriptExprClass: |
15987 | case Expr::OMPArraySectionExprClass: |
15988 | case Expr::OMPArrayShapingExprClass: |
15989 | case Expr::OMPIteratorExprClass: |
15990 | case Expr::MemberExprClass: |
15991 | case Expr::CompoundAssignOperatorClass: |
15992 | case Expr::CompoundLiteralExprClass: |
15993 | case Expr::ExtVectorElementExprClass: |
15994 | case Expr::DesignatedInitExprClass: |
15995 | case Expr::ArrayInitLoopExprClass: |
15996 | case Expr::ArrayInitIndexExprClass: |
15997 | case Expr::NoInitExprClass: |
15998 | case Expr::DesignatedInitUpdateExprClass: |
15999 | case Expr::ImplicitValueInitExprClass: |
16000 | case Expr::ParenListExprClass: |
16001 | case Expr::VAArgExprClass: |
16002 | case Expr::AddrLabelExprClass: |
16003 | case Expr::StmtExprClass: |
16004 | case Expr::CXXMemberCallExprClass: |
16005 | case Expr::CUDAKernelCallExprClass: |
16006 | case Expr::CXXAddrspaceCastExprClass: |
16007 | case Expr::CXXDynamicCastExprClass: |
16008 | case Expr::CXXTypeidExprClass: |
16009 | case Expr::CXXUuidofExprClass: |
16010 | case Expr::MSPropertyRefExprClass: |
16011 | case Expr::MSPropertySubscriptExprClass: |
16012 | case Expr::CXXNullPtrLiteralExprClass: |
16013 | case Expr::UserDefinedLiteralClass: |
16014 | case Expr::CXXThisExprClass: |
16015 | case Expr::CXXThrowExprClass: |
16016 | case Expr::CXXNewExprClass: |
16017 | case Expr::CXXDeleteExprClass: |
16018 | case Expr::CXXPseudoDestructorExprClass: |
16019 | case Expr::UnresolvedLookupExprClass: |
16020 | case Expr::TypoExprClass: |
16021 | case Expr::RecoveryExprClass: |
16022 | case Expr::DependentScopeDeclRefExprClass: |
16023 | case Expr::CXXConstructExprClass: |
16024 | case Expr::CXXInheritedCtorInitExprClass: |
16025 | case Expr::CXXStdInitializerListExprClass: |
16026 | case Expr::CXXBindTemporaryExprClass: |
16027 | case Expr::ExprWithCleanupsClass: |
16028 | case Expr::CXXTemporaryObjectExprClass: |
16029 | case Expr::CXXUnresolvedConstructExprClass: |
16030 | case Expr::CXXDependentScopeMemberExprClass: |
16031 | case Expr::UnresolvedMemberExprClass: |
16032 | case Expr::ObjCStringLiteralClass: |
16033 | case Expr::ObjCBoxedExprClass: |
16034 | case Expr::ObjCArrayLiteralClass: |
16035 | case Expr::ObjCDictionaryLiteralClass: |
16036 | case Expr::ObjCEncodeExprClass: |
16037 | case Expr::ObjCMessageExprClass: |
16038 | case Expr::ObjCSelectorExprClass: |
16039 | case Expr::ObjCProtocolExprClass: |
16040 | case Expr::ObjCIvarRefExprClass: |
16041 | case Expr::ObjCPropertyRefExprClass: |
16042 | case Expr::ObjCSubscriptRefExprClass: |
16043 | case Expr::ObjCIsaExprClass: |
16044 | case Expr::ObjCAvailabilityCheckExprClass: |
16045 | case Expr::ShuffleVectorExprClass: |
16046 | case Expr::ConvertVectorExprClass: |
16047 | case Expr::BlockExprClass: |
16048 | case Expr::NoStmtClass: |
16049 | case Expr::OpaqueValueExprClass: |
16050 | case Expr::PackExpansionExprClass: |
16051 | case Expr::SubstNonTypeTemplateParmPackExprClass: |
16052 | case Expr::FunctionParmPackExprClass: |
16053 | case Expr::AsTypeExprClass: |
16054 | case Expr::ObjCIndirectCopyRestoreExprClass: |
16055 | case Expr::MaterializeTemporaryExprClass: |
16056 | case Expr::PseudoObjectExprClass: |
16057 | case Expr::AtomicExprClass: |
16058 | case Expr::LambdaExprClass: |
16059 | case Expr::CXXFoldExprClass: |
16060 | case Expr::CoawaitExprClass: |
16061 | case Expr::DependentCoawaitExprClass: |
16062 | case Expr::CoyieldExprClass: |
16063 | case Expr::SYCLUniqueStableNameExprClass: |
16064 | case Expr::CXXParenListInitExprClass: |
16065 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16066 | |
16067 | case Expr::InitListExprClass: { |
16068 | // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the |
16069 | // form "T x = { a };" is equivalent to "T x = a;". |
16070 | // Unless we're initializing a reference, T is a scalar as it is known to be |
16071 | // of integral or enumeration type. |
16072 | if (E->isPRValue()) |
16073 | if (cast<InitListExpr>(E)->getNumInits() == 1) |
16074 | return CheckICE(cast<InitListExpr>(E)->getInit(0), Ctx); |
16075 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16076 | } |
16077 | |
16078 | case Expr::SizeOfPackExprClass: |
16079 | case Expr::GNUNullExprClass: |
16080 | case Expr::SourceLocExprClass: |
16081 | return NoDiag(); |
16082 | |
16083 | case Expr::PackIndexingExprClass: |
16084 | return CheckICE(cast<PackIndexingExpr>(E)->getSelectedExpr(), Ctx); |
16085 | |
16086 | case Expr::SubstNonTypeTemplateParmExprClass: |
16087 | return |
16088 | CheckICE(cast<SubstNonTypeTemplateParmExpr>(E)->getReplacement(), Ctx); |
16089 | |
16090 | case Expr::ConstantExprClass: |
16091 | return CheckICE(cast<ConstantExpr>(E)->getSubExpr(), Ctx); |
16092 | |
16093 | case Expr::ParenExprClass: |
16094 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
16095 | case Expr::GenericSelectionExprClass: |
16096 | return CheckICE(cast<GenericSelectionExpr>(E)->getResultExpr(), Ctx); |
16097 | case Expr::IntegerLiteralClass: |
16098 | case Expr::FixedPointLiteralClass: |
16099 | case Expr::CharacterLiteralClass: |
16100 | case Expr::ObjCBoolLiteralExprClass: |
16101 | case Expr::CXXBoolLiteralExprClass: |
16102 | case Expr::CXXScalarValueInitExprClass: |
16103 | case Expr::TypeTraitExprClass: |
16104 | case Expr::ConceptSpecializationExprClass: |
16105 | case Expr::RequiresExprClass: |
16106 | case Expr::ArrayTypeTraitExprClass: |
16107 | case Expr::ExpressionTraitExprClass: |
16108 | case Expr::CXXNoexceptExprClass: |
16109 | return NoDiag(); |
16110 | case Expr::CallExprClass: |
16111 | case Expr::CXXOperatorCallExprClass: { |
16112 | // C99 6.6/3 allows function calls within unevaluated subexpressions of |
16113 | // constant expressions, but they can never be ICEs because an ICE cannot |
16114 | // contain an operand of (pointer to) function type. |
16115 | const CallExpr *CE = cast<CallExpr>(E); |
16116 | if (CE->getBuiltinCallee()) |
16117 | return CheckEvalInICE(E, Ctx); |
16118 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16119 | } |
16120 | case Expr::CXXRewrittenBinaryOperatorClass: |
16121 | return CheckICE(cast<CXXRewrittenBinaryOperator>(E)->getSemanticForm(), |
16122 | Ctx); |
16123 | case Expr::DeclRefExprClass: { |
16124 | const NamedDecl *D = cast<DeclRefExpr>(E)->getDecl(); |
16125 | if (isa<EnumConstantDecl>(D)) |
16126 | return NoDiag(); |
16127 | |
16128 | // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified |
16129 | // integer variables in constant expressions: |
16130 | // |
16131 | // C++ 7.1.5.1p2 |
16132 | // A variable of non-volatile const-qualified integral or enumeration |
16133 | // type initialized by an ICE can be used in ICEs. |
16134 | // |
16135 | // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In |
16136 | // that mode, use of reference variables should not be allowed. |
16137 | const VarDecl *VD = dyn_cast<VarDecl>(D); |
16138 | if (VD && VD->isUsableInConstantExpressions(C: Ctx) && |
16139 | !VD->getType()->isReferenceType()) |
16140 | return NoDiag(); |
16141 | |
16142 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16143 | } |
16144 | case Expr::UnaryOperatorClass: { |
16145 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
16146 | switch (Exp->getOpcode()) { |
16147 | case UO_PostInc: |
16148 | case UO_PostDec: |
16149 | case UO_PreInc: |
16150 | case UO_PreDec: |
16151 | case UO_AddrOf: |
16152 | case UO_Deref: |
16153 | case UO_Coawait: |
16154 | // C99 6.6/3 allows increment and decrement within unevaluated |
16155 | // subexpressions of constant expressions, but they can never be ICEs |
16156 | // because an ICE cannot contain an lvalue operand. |
16157 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16158 | case UO_Extension: |
16159 | case UO_LNot: |
16160 | case UO_Plus: |
16161 | case UO_Minus: |
16162 | case UO_Not: |
16163 | case UO_Real: |
16164 | case UO_Imag: |
16165 | return CheckICE(E: Exp->getSubExpr(), Ctx); |
16166 | } |
16167 | llvm_unreachable("invalid unary operator class" ); |
16168 | } |
16169 | case Expr::OffsetOfExprClass: { |
16170 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
16171 | // EvaluateAsRValue matches the proposed gcc behavior for cases like |
16172 | // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect |
16173 | // compliance: we should warn earlier for offsetof expressions with |
16174 | // array subscripts that aren't ICEs, and if the array subscripts |
16175 | // are ICEs, the value of the offsetof must be an integer constant. |
16176 | return CheckEvalInICE(E, Ctx); |
16177 | } |
16178 | case Expr::UnaryExprOrTypeTraitExprClass: { |
16179 | const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(E); |
16180 | if ((Exp->getKind() == UETT_SizeOf) && |
16181 | Exp->getTypeOfArgument()->isVariableArrayType()) |
16182 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16183 | return NoDiag(); |
16184 | } |
16185 | case Expr::BinaryOperatorClass: { |
16186 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
16187 | switch (Exp->getOpcode()) { |
16188 | case BO_PtrMemD: |
16189 | case BO_PtrMemI: |
16190 | case BO_Assign: |
16191 | case BO_MulAssign: |
16192 | case BO_DivAssign: |
16193 | case BO_RemAssign: |
16194 | case BO_AddAssign: |
16195 | case BO_SubAssign: |
16196 | case BO_ShlAssign: |
16197 | case BO_ShrAssign: |
16198 | case BO_AndAssign: |
16199 | case BO_XorAssign: |
16200 | case BO_OrAssign: |
16201 | // C99 6.6/3 allows assignments within unevaluated subexpressions of |
16202 | // constant expressions, but they can never be ICEs because an ICE cannot |
16203 | // contain an lvalue operand. |
16204 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16205 | |
16206 | case BO_Mul: |
16207 | case BO_Div: |
16208 | case BO_Rem: |
16209 | case BO_Add: |
16210 | case BO_Sub: |
16211 | case BO_Shl: |
16212 | case BO_Shr: |
16213 | case BO_LT: |
16214 | case BO_GT: |
16215 | case BO_LE: |
16216 | case BO_GE: |
16217 | case BO_EQ: |
16218 | case BO_NE: |
16219 | case BO_And: |
16220 | case BO_Xor: |
16221 | case BO_Or: |
16222 | case BO_Comma: |
16223 | case BO_Cmp: { |
16224 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
16225 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
16226 | if (Exp->getOpcode() == BO_Div || |
16227 | Exp->getOpcode() == BO_Rem) { |
16228 | // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure |
16229 | // we don't evaluate one. |
16230 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) { |
16231 | llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx); |
16232 | if (REval == 0) |
16233 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16234 | if (REval.isSigned() && REval.isAllOnes()) { |
16235 | llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx); |
16236 | if (LEval.isMinSignedValue()) |
16237 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16238 | } |
16239 | } |
16240 | } |
16241 | if (Exp->getOpcode() == BO_Comma) { |
16242 | if (Ctx.getLangOpts().C99) { |
16243 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
16244 | // if it isn't evaluated. |
16245 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) |
16246 | return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc()); |
16247 | } else { |
16248 | // In both C89 and C++, commas in ICEs are illegal. |
16249 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16250 | } |
16251 | } |
16252 | return Worst(A: LHSResult, B: RHSResult); |
16253 | } |
16254 | case BO_LAnd: |
16255 | case BO_LOr: { |
16256 | ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx); |
16257 | ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx); |
16258 | if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) { |
16259 | // Rare case where the RHS has a comma "side-effect"; we need |
16260 | // to actually check the condition to see whether the side |
16261 | // with the comma is evaluated. |
16262 | if ((Exp->getOpcode() == BO_LAnd) != |
16263 | (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0)) |
16264 | return RHSResult; |
16265 | return NoDiag(); |
16266 | } |
16267 | |
16268 | return Worst(A: LHSResult, B: RHSResult); |
16269 | } |
16270 | } |
16271 | llvm_unreachable("invalid binary operator kind" ); |
16272 | } |
16273 | case Expr::ImplicitCastExprClass: |
16274 | case Expr::CStyleCastExprClass: |
16275 | case Expr::CXXFunctionalCastExprClass: |
16276 | case Expr::CXXStaticCastExprClass: |
16277 | case Expr::CXXReinterpretCastExprClass: |
16278 | case Expr::CXXConstCastExprClass: |
16279 | case Expr::ObjCBridgedCastExprClass: { |
16280 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
16281 | if (isa<ExplicitCastExpr>(E)) { |
16282 | if (const FloatingLiteral *FL |
16283 | = dyn_cast<FloatingLiteral>(SubExpr->IgnoreParenImpCasts())) { |
16284 | unsigned DestWidth = Ctx.getIntWidth(T: E->getType()); |
16285 | bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType(); |
16286 | APSInt IgnoredVal(DestWidth, !DestSigned); |
16287 | bool Ignored; |
16288 | // If the value does not fit in the destination type, the behavior is |
16289 | // undefined, so we are not required to treat it as a constant |
16290 | // expression. |
16291 | if (FL->getValue().convertToInteger(Result&: IgnoredVal, |
16292 | RM: llvm::APFloat::rmTowardZero, |
16293 | IsExact: &Ignored) & APFloat::opInvalidOp) |
16294 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16295 | return NoDiag(); |
16296 | } |
16297 | } |
16298 | switch (cast<CastExpr>(E)->getCastKind()) { |
16299 | case CK_LValueToRValue: |
16300 | case CK_AtomicToNonAtomic: |
16301 | case CK_NonAtomicToAtomic: |
16302 | case CK_NoOp: |
16303 | case CK_IntegralToBoolean: |
16304 | case CK_IntegralCast: |
16305 | return CheckICE(E: SubExpr, Ctx); |
16306 | default: |
16307 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16308 | } |
16309 | } |
16310 | case Expr::BinaryConditionalOperatorClass: { |
16311 | const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(E); |
16312 | ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx); |
16313 | if (CommonResult.Kind == IK_NotICE) return CommonResult; |
16314 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
16315 | if (FalseResult.Kind == IK_NotICE) return FalseResult; |
16316 | if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult; |
16317 | if (FalseResult.Kind == IK_ICEIfUnevaluated && |
16318 | Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag(); |
16319 | return FalseResult; |
16320 | } |
16321 | case Expr::ConditionalOperatorClass: { |
16322 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
16323 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
16324 | // then only the true side is actually considered in an integer constant |
16325 | // expression, and it is fully evaluated. This is an important GNU |
16326 | // extension. See GCC PR38377 for discussion. |
16327 | if (const CallExpr *CallCE |
16328 | = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
16329 | if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) |
16330 | return CheckEvalInICE(E, Ctx); |
16331 | ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx); |
16332 | if (CondResult.Kind == IK_NotICE) |
16333 | return CondResult; |
16334 | |
16335 | ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx); |
16336 | ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx); |
16337 | |
16338 | if (TrueResult.Kind == IK_NotICE) |
16339 | return TrueResult; |
16340 | if (FalseResult.Kind == IK_NotICE) |
16341 | return FalseResult; |
16342 | if (CondResult.Kind == IK_ICEIfUnevaluated) |
16343 | return CondResult; |
16344 | if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE) |
16345 | return NoDiag(); |
16346 | // Rare case where the diagnostics depend on which side is evaluated |
16347 | // Note that if we get here, CondResult is 0, and at least one of |
16348 | // TrueResult and FalseResult is non-zero. |
16349 | if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0) |
16350 | return FalseResult; |
16351 | return TrueResult; |
16352 | } |
16353 | case Expr::CXXDefaultArgExprClass: |
16354 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
16355 | case Expr::CXXDefaultInitExprClass: |
16356 | return CheckICE(cast<CXXDefaultInitExpr>(E)->getExpr(), Ctx); |
16357 | case Expr::ChooseExprClass: { |
16358 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(), Ctx); |
16359 | } |
16360 | case Expr::BuiltinBitCastExprClass: { |
16361 | if (!checkBitCastConstexprEligibility(nullptr, Ctx, cast<CastExpr>(E))) |
16362 | return ICEDiag(IK_NotICE, E->getBeginLoc()); |
16363 | return CheckICE(cast<CastExpr>(E)->getSubExpr(), Ctx); |
16364 | } |
16365 | } |
16366 | |
16367 | llvm_unreachable("Invalid StmtClass!" ); |
16368 | } |
16369 | |
16370 | /// Evaluate an expression as a C++11 integral constant expression. |
16371 | static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, |
16372 | const Expr *E, |
16373 | llvm::APSInt *Value, |
16374 | SourceLocation *Loc) { |
16375 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
16376 | if (Loc) *Loc = E->getExprLoc(); |
16377 | return false; |
16378 | } |
16379 | |
16380 | APValue Result; |
16381 | if (!E->isCXX11ConstantExpr(Ctx, Result: &Result, Loc)) |
16382 | return false; |
16383 | |
16384 | if (!Result.isInt()) { |
16385 | if (Loc) *Loc = E->getExprLoc(); |
16386 | return false; |
16387 | } |
16388 | |
16389 | if (Value) *Value = Result.getInt(); |
16390 | return true; |
16391 | } |
16392 | |
16393 | bool Expr::isIntegerConstantExpr(const ASTContext &Ctx, |
16394 | SourceLocation *Loc) const { |
16395 | assert(!isValueDependent() && |
16396 | "Expression evaluator can't be called on a dependent expression." ); |
16397 | |
16398 | ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr" ); |
16399 | |
16400 | if (Ctx.getLangOpts().CPlusPlus11) |
16401 | return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr, Loc); |
16402 | |
16403 | ICEDiag D = CheckICE(E: this, Ctx); |
16404 | if (D.Kind != IK_ICE) { |
16405 | if (Loc) *Loc = D.Loc; |
16406 | return false; |
16407 | } |
16408 | return true; |
16409 | } |
16410 | |
16411 | std::optional<llvm::APSInt> |
16412 | Expr::getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc) const { |
16413 | if (isValueDependent()) { |
16414 | // Expression evaluator can't succeed on a dependent expression. |
16415 | return std::nullopt; |
16416 | } |
16417 | |
16418 | APSInt Value; |
16419 | |
16420 | if (Ctx.getLangOpts().CPlusPlus11) { |
16421 | if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value, Loc)) |
16422 | return Value; |
16423 | return std::nullopt; |
16424 | } |
16425 | |
16426 | if (!isIntegerConstantExpr(Ctx, Loc)) |
16427 | return std::nullopt; |
16428 | |
16429 | // The only possible side-effects here are due to UB discovered in the |
16430 | // evaluation (for instance, INT_MAX + 1). In such a case, we are still |
16431 | // required to treat the expression as an ICE, so we produce the folded |
16432 | // value. |
16433 | EvalResult ExprResult; |
16434 | Expr::EvalStatus Status; |
16435 | EvalInfo Info(Ctx, Status, EvalInfo::EM_IgnoreSideEffects); |
16436 | Info.InConstantContext = true; |
16437 | |
16438 | if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info)) |
16439 | llvm_unreachable("ICE cannot be evaluated!" ); |
16440 | |
16441 | return ExprResult.Val.getInt(); |
16442 | } |
16443 | |
16444 | bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const { |
16445 | assert(!isValueDependent() && |
16446 | "Expression evaluator can't be called on a dependent expression." ); |
16447 | |
16448 | return CheckICE(E: this, Ctx).Kind == IK_ICE; |
16449 | } |
16450 | |
16451 | bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result, |
16452 | SourceLocation *Loc) const { |
16453 | assert(!isValueDependent() && |
16454 | "Expression evaluator can't be called on a dependent expression." ); |
16455 | |
16456 | // We support this checking in C++98 mode in order to diagnose compatibility |
16457 | // issues. |
16458 | assert(Ctx.getLangOpts().CPlusPlus); |
16459 | |
16460 | // Build evaluation settings. |
16461 | Expr::EvalStatus Status; |
16462 | SmallVector<PartialDiagnosticAt, 8> Diags; |
16463 | Status.Diag = &Diags; |
16464 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
16465 | |
16466 | APValue Scratch; |
16467 | bool IsConstExpr = |
16468 | ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) && |
16469 | // FIXME: We don't produce a diagnostic for this, but the callers that |
16470 | // call us on arbitrary full-expressions should generally not care. |
16471 | Info.discardCleanups() && !Status.HasSideEffects; |
16472 | |
16473 | if (!Diags.empty()) { |
16474 | IsConstExpr = false; |
16475 | if (Loc) *Loc = Diags[0].first; |
16476 | } else if (!IsConstExpr) { |
16477 | // FIXME: This shouldn't happen. |
16478 | if (Loc) *Loc = getExprLoc(); |
16479 | } |
16480 | |
16481 | return IsConstExpr; |
16482 | } |
16483 | |
16484 | bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx, |
16485 | const FunctionDecl *Callee, |
16486 | ArrayRef<const Expr*> Args, |
16487 | const Expr *This) const { |
16488 | assert(!isValueDependent() && |
16489 | "Expression evaluator can't be called on a dependent expression." ); |
16490 | |
16491 | llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution" , [&] { |
16492 | std::string Name; |
16493 | llvm::raw_string_ostream OS(Name); |
16494 | Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(), |
16495 | /*Qualified=*/true); |
16496 | return Name; |
16497 | }); |
16498 | |
16499 | Expr::EvalStatus Status; |
16500 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpressionUnevaluated); |
16501 | Info.InConstantContext = true; |
16502 | |
16503 | LValue ThisVal; |
16504 | const LValue *ThisPtr = nullptr; |
16505 | if (This) { |
16506 | #ifndef NDEBUG |
16507 | auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee); |
16508 | assert(MD && "Don't provide `this` for non-methods." ); |
16509 | assert(MD->isImplicitObjectMemberFunction() && |
16510 | "Don't provide `this` for methods without an implicit object." ); |
16511 | #endif |
16512 | if (!This->isValueDependent() && |
16513 | EvaluateObjectArgument(Info, Object: This, This&: ThisVal) && |
16514 | !Info.EvalStatus.HasSideEffects) |
16515 | ThisPtr = &ThisVal; |
16516 | |
16517 | // Ignore any side-effects from a failed evaluation. This is safe because |
16518 | // they can't interfere with any other argument evaluation. |
16519 | Info.EvalStatus.HasSideEffects = false; |
16520 | } |
16521 | |
16522 | CallRef Call = Info.CurrentCall->createCall(Callee); |
16523 | for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end(); |
16524 | I != E; ++I) { |
16525 | unsigned Idx = I - Args.begin(); |
16526 | if (Idx >= Callee->getNumParams()) |
16527 | break; |
16528 | const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx); |
16529 | if ((*I)->isValueDependent() || |
16530 | !EvaluateCallArg(PVD, Arg: *I, Call, Info) || |
16531 | Info.EvalStatus.HasSideEffects) { |
16532 | // If evaluation fails, throw away the argument entirely. |
16533 | if (APValue *Slot = Info.getParamSlot(Call, PVD)) |
16534 | *Slot = APValue(); |
16535 | } |
16536 | |
16537 | // Ignore any side-effects from a failed evaluation. This is safe because |
16538 | // they can't interfere with any other argument evaluation. |
16539 | Info.EvalStatus.HasSideEffects = false; |
16540 | } |
16541 | |
16542 | // Parameter cleanups happen in the caller and are not part of this |
16543 | // evaluation. |
16544 | Info.discardCleanups(); |
16545 | Info.EvalStatus.HasSideEffects = false; |
16546 | |
16547 | // Build fake call to Callee. |
16548 | CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This, |
16549 | Call); |
16550 | // FIXME: Missing ExprWithCleanups in enable_if conditions? |
16551 | FullExpressionRAII Scope(Info); |
16552 | return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() && |
16553 | !Info.EvalStatus.HasSideEffects; |
16554 | } |
16555 | |
16556 | bool Expr::isPotentialConstantExpr(const FunctionDecl *FD, |
16557 | SmallVectorImpl< |
16558 | PartialDiagnosticAt> &Diags) { |
16559 | // FIXME: It would be useful to check constexpr function templates, but at the |
16560 | // moment the constant expression evaluator cannot cope with the non-rigorous |
16561 | // ASTs which we build for dependent expressions. |
16562 | if (FD->isDependentContext()) |
16563 | return true; |
16564 | |
16565 | llvm::TimeTraceScope TimeScope("isPotentialConstantExpr" , [&] { |
16566 | std::string Name; |
16567 | llvm::raw_string_ostream OS(Name); |
16568 | FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(), |
16569 | /*Qualified=*/true); |
16570 | return Name; |
16571 | }); |
16572 | |
16573 | Expr::EvalStatus Status; |
16574 | Status.Diag = &Diags; |
16575 | |
16576 | EvalInfo Info(FD->getASTContext(), Status, EvalInfo::EM_ConstantExpression); |
16577 | Info.InConstantContext = true; |
16578 | Info.CheckingPotentialConstantExpression = true; |
16579 | |
16580 | // The constexpr VM attempts to compile all methods to bytecode here. |
16581 | if (Info.EnableNewConstInterp) { |
16582 | Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FnDecl: FD); |
16583 | return Diags.empty(); |
16584 | } |
16585 | |
16586 | const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD); |
16587 | const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr; |
16588 | |
16589 | // Fabricate an arbitrary expression on the stack and pretend that it |
16590 | // is a temporary being used as the 'this' pointer. |
16591 | LValue This; |
16592 | ImplicitValueInitExpr VIE(RD ? Info.Ctx.getRecordType(RD) : Info.Ctx.IntTy); |
16593 | This.set({&VIE, Info.CurrentCall->Index}); |
16594 | |
16595 | ArrayRef<const Expr*> Args; |
16596 | |
16597 | APValue Scratch; |
16598 | if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) { |
16599 | // Evaluate the call as a constant initializer, to allow the construction |
16600 | // of objects of non-literal types. |
16601 | Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch); |
16602 | HandleConstructorCall(&VIE, This, Args, CD, Info, Scratch); |
16603 | } else { |
16604 | SourceLocation Loc = FD->getLocation(); |
16605 | HandleFunctionCall( |
16606 | Loc, FD, (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr, |
16607 | &VIE, Args, CallRef(), FD->getBody(), Info, Scratch, |
16608 | /*ResultSlot=*/nullptr); |
16609 | } |
16610 | |
16611 | return Diags.empty(); |
16612 | } |
16613 | |
16614 | bool Expr::isPotentialConstantExprUnevaluated(Expr *E, |
16615 | const FunctionDecl *FD, |
16616 | SmallVectorImpl< |
16617 | PartialDiagnosticAt> &Diags) { |
16618 | assert(!E->isValueDependent() && |
16619 | "Expression evaluator can't be called on a dependent expression." ); |
16620 | |
16621 | Expr::EvalStatus Status; |
16622 | Status.Diag = &Diags; |
16623 | |
16624 | EvalInfo Info(FD->getASTContext(), Status, |
16625 | EvalInfo::EM_ConstantExpressionUnevaluated); |
16626 | Info.InConstantContext = true; |
16627 | Info.CheckingPotentialConstantExpression = true; |
16628 | |
16629 | // Fabricate a call stack frame to give the arguments a plausible cover story. |
16630 | CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr, |
16631 | /*CallExpr=*/nullptr, CallRef()); |
16632 | |
16633 | APValue ResultScratch; |
16634 | Evaluate(Result&: ResultScratch, Info, E); |
16635 | return Diags.empty(); |
16636 | } |
16637 | |
16638 | bool Expr::tryEvaluateObjectSize(uint64_t &Result, ASTContext &Ctx, |
16639 | unsigned Type) const { |
16640 | if (!getType()->isPointerType()) |
16641 | return false; |
16642 | |
16643 | Expr::EvalStatus Status; |
16644 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
16645 | return tryEvaluateBuiltinObjectSize(E: this, Type, Info, Size&: Result); |
16646 | } |
16647 | |
16648 | static bool EvaluateBuiltinStrLen(const Expr *E, uint64_t &Result, |
16649 | EvalInfo &Info) { |
16650 | if (!E->getType()->hasPointerRepresentation() || !E->isPRValue()) |
16651 | return false; |
16652 | |
16653 | LValue String; |
16654 | |
16655 | if (!EvaluatePointer(E, Result&: String, Info)) |
16656 | return false; |
16657 | |
16658 | QualType CharTy = E->getType()->getPointeeType(); |
16659 | |
16660 | // Fast path: if it's a string literal, search the string value. |
16661 | if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>( |
16662 | Val: String.getLValueBase().dyn_cast<const Expr *>())) { |
16663 | StringRef Str = S->getBytes(); |
16664 | int64_t Off = String.Offset.getQuantity(); |
16665 | if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() && |
16666 | S->getCharByteWidth() == 1 && |
16667 | // FIXME: Add fast-path for wchar_t too. |
16668 | Info.Ctx.hasSameUnqualifiedType(T1: CharTy, T2: Info.Ctx.CharTy)) { |
16669 | Str = Str.substr(Start: Off); |
16670 | |
16671 | StringRef::size_type Pos = Str.find(C: 0); |
16672 | if (Pos != StringRef::npos) |
16673 | Str = Str.substr(Start: 0, N: Pos); |
16674 | |
16675 | Result = Str.size(); |
16676 | return true; |
16677 | } |
16678 | |
16679 | // Fall through to slow path. |
16680 | } |
16681 | |
16682 | // Slow path: scan the bytes of the string looking for the terminating 0. |
16683 | for (uint64_t Strlen = 0; /**/; ++Strlen) { |
16684 | APValue Char; |
16685 | if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) || |
16686 | !Char.isInt()) |
16687 | return false; |
16688 | if (!Char.getInt()) { |
16689 | Result = Strlen; |
16690 | return true; |
16691 | } |
16692 | if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
16693 | return false; |
16694 | } |
16695 | } |
16696 | |
16697 | bool Expr::EvaluateCharRangeAsString(std::string &Result, |
16698 | const Expr *SizeExpression, |
16699 | const Expr *PtrExpression, ASTContext &Ctx, |
16700 | EvalResult &Status) const { |
16701 | LValue String; |
16702 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantExpression); |
16703 | Info.InConstantContext = true; |
16704 | |
16705 | FullExpressionRAII Scope(Info); |
16706 | APSInt SizeValue; |
16707 | if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info)) |
16708 | return false; |
16709 | |
16710 | int64_t Size = SizeValue.getExtValue(); |
16711 | |
16712 | if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info)) |
16713 | return false; |
16714 | |
16715 | QualType CharTy = PtrExpression->getType()->getPointeeType(); |
16716 | for (int64_t I = 0; I < Size; ++I) { |
16717 | APValue Char; |
16718 | if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String, |
16719 | RVal&: Char)) |
16720 | return false; |
16721 | |
16722 | APSInt C = Char.getInt(); |
16723 | Result.push_back(c: static_cast<char>(C.getExtValue())); |
16724 | if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1)) |
16725 | return false; |
16726 | } |
16727 | if (!Scope.destroy()) |
16728 | return false; |
16729 | |
16730 | if (!CheckMemoryLeaks(Info)) |
16731 | return false; |
16732 | |
16733 | return true; |
16734 | } |
16735 | |
16736 | bool Expr::tryEvaluateStrLen(uint64_t &Result, ASTContext &Ctx) const { |
16737 | Expr::EvalStatus Status; |
16738 | EvalInfo Info(Ctx, Status, EvalInfo::EM_ConstantFold); |
16739 | return EvaluateBuiltinStrLen(E: this, Result, Info); |
16740 | } |
16741 | |