1//===----- EvaluationResult.cpp - Result class for the VM ------*- C++ -*-===//
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#include "EvaluationResult.h"
10#include "../ExprConstShared.h"
11#include "InterpState.h"
12#include "Pointer.h"
13#include "Record.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/ExprCXX.h"
17#include "clang/AST/ExprObjC.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallPtrSet.h"
20#include <iterator>
21
22namespace clang {
23namespace interp {
24
25QualType EvaluationResult::getStorageType() const {
26 if (const auto *E = Source.asExpr()) {
27 if (E->isPRValue())
28 return E->getType();
29
30 return Ctx.getASTContext().getLValueReferenceType(T: E->getType());
31 }
32
33 if (const auto *D = Source.asValueDecl())
34 return D->getType();
35 return QualType();
36}
37
38static void DiagnoseUninitializedSubobject(InterpState &S, SourceLocation Loc,
39 const FieldDecl *SubObjDecl) {
40 assert(SubObjDecl && "Subobject declaration does not exist");
41 S.FFDiag(Loc, DiagId: diag::note_constexpr_uninitialized)
42 << /*(name)*/ 1 << SubObjDecl;
43 S.Note(Loc: SubObjDecl->getLocation(),
44 DiagId: diag::note_constexpr_subobject_declared_here);
45}
46
47static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
48 PtrView BasePtr, const Record *R,
49 bool IsCompleteClass = true);
50
51static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc,
52 PtrView BasePtr) {
53 const Descriptor *BaseDesc = BasePtr.getFieldDesc();
54 assert(BaseDesc->isArray());
55
56 size_t NumElems = BaseDesc->getNumElems();
57 if (NumElems == 0)
58 return true;
59
60 bool Result = true;
61
62 if (BaseDesc->isPrimitiveArray()) {
63 if (BasePtr.allElementsInitialized())
64 return true;
65 if (const FieldDecl *F = BasePtr.getField())
66 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: F);
67 return false;
68 }
69 const Descriptor *ElemDesc = BaseDesc->ElemDesc;
70
71 if (ElemDesc->isRecord()) {
72 const Record *R = ElemDesc->ElemRecord;
73 for (size_t I = 0; I != NumElems; ++I) {
74 PtrView ElemPtr = BasePtr.atIndex(Idx: I).narrow();
75 Result &= CheckFieldsInitialized(S, Loc, BasePtr: ElemPtr, R);
76 }
77 } else if (ElemDesc->isArray()) {
78 for (size_t I = 0; I != NumElems; ++I) {
79 PtrView ElemPtr = BasePtr.atIndex(Idx: I).narrow();
80 Result &= CheckArrayInitialized(S, Loc, BasePtr: ElemPtr);
81 }
82 }
83
84 return Result;
85}
86
87static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
88 PtrView BasePtr, const Record *R,
89 bool IsCompleteClass) {
90 assert(R);
91 bool Result = true;
92 // Check all fields of this record are initialized.
93 for (const Record::Field &F : R->fields()) {
94 PtrView FieldPtr = BasePtr.atField(Offset: F.Offset);
95
96 // Don't check inactive union members.
97 if (R->isUnion() && !FieldPtr.isActive())
98 continue;
99
100 QualType FieldType = F.Decl->getType();
101 const Descriptor *FieldDesc = FieldPtr.getFieldDesc();
102
103 if (FieldDesc->isRecord()) {
104 Result &= CheckFieldsInitialized(S, Loc, BasePtr: FieldPtr, R: FieldPtr.getRecord());
105 } else if (FieldType->isIncompleteArrayType()) {
106 // Nothing to do here.
107 } else if (F.Decl->isUnnamedBitField()) {
108 // Nothing do do here.
109 } else if (FieldDesc->isArray()) {
110 Result &= CheckArrayInitialized(S, Loc, BasePtr: FieldPtr);
111 } else if (!FieldPtr.isInitialized()) {
112 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: F.Decl);
113 Result = false;
114 }
115 }
116
117 auto diagnoseBase = [&](const Record::Base &B, unsigned Index) -> bool {
118 const Descriptor *Desc = BasePtr.getDeclDesc();
119 if (const auto *CD = dyn_cast_if_present<CXXRecordDecl>(Val: R->getDecl())) {
120 const auto &BS = *std::next(x: CD->bases_begin(), n: Index);
121 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
122 S.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
123 << B.Desc->getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
124 } else {
125 S.FFDiag(Loc: Desc->getLocation(), DiagId: diag::note_constexpr_uninitialized_base)
126 << B.Desc->getType();
127 }
128 return false;
129 };
130
131 // Check Fields in all bases.
132 for (auto [I, B] : llvm::enumerate(First: R->bases())) {
133 PtrView P = BasePtr.atField(Offset: B.Offset);
134 if (!P.isInitialized())
135 return diagnoseBase(B, I);
136 Result &= CheckFieldsInitialized(S, Loc, BasePtr: P, R: B.R, /*IsCompleteClass=*/false);
137 }
138
139 // And virtual bases.
140 if (IsCompleteClass) {
141 for (auto [I, B] : llvm::enumerate(First: R->virtual_bases())) {
142 PtrView P = BasePtr.atField(Offset: B.Offset);
143 if (!P.isInitialized())
144 return diagnoseBase(B, I);
145 Result &=
146 CheckFieldsInitialized(S, Loc, BasePtr: P, R: B.R, /*IsCompleteClass=*/false);
147 }
148 }
149
150 return Result;
151}
152
153bool EvaluationResult::checkFullyInitialized(InterpState &S,
154 const Pointer &Ptr) const {
155 assert(Source);
156 assert(empty());
157
158 if (Ptr.isZero())
159 return true;
160 if (!Ptr.isBlockPointer())
161 return true;
162
163 // We can't inspect dead pointers at all. Return true here so we can
164 // diagnose them later.
165 if (!Ptr.isLive())
166 return true;
167
168 SourceLocation InitLoc;
169 if (const auto *D = Source.asDecl())
170 InitLoc = cast<VarDecl>(Val: D)->getAnyInitializer()->getExprLoc();
171 else if (const auto *E = Source.asExpr())
172 InitLoc = E->getExprLoc();
173
174 if (const Record *R = Ptr.getRecord())
175 return CheckFieldsInitialized(S, Loc: InitLoc, BasePtr: Ptr.view(), R);
176
177 if (isa_and_nonnull<ConstantArrayType>(Val: Ptr.getType()->getAsArrayTypeUnsafe()))
178 return CheckArrayInitialized(S, Loc: InitLoc, BasePtr: Ptr.view());
179
180 return true;
181}
182
183static bool isOrHasPtr(const Descriptor *D) {
184 if ((D->isPrimitive() || D->isPrimitiveArray()) && D->getPrimType() == PT_Ptr)
185 return true;
186
187 if (D->ElemRecord)
188 return D->ElemRecord->hasPtrField();
189 return false;
190}
191
192static void collectBlocks(PtrView Ptr,
193 llvm::SmallPtrSet<const Block *, 4> &Blocks,
194 bool IsCompleteClass = true) {
195 auto isUsefulPtr = [](const Pointer &P) -> bool {
196 return P.isLive() && P.isBlockPointer() && !P.isZero() && !P.isDummy() &&
197 P.isDereferencable() && !P.isUnknownSizeArray() && !P.isOnePastEnd();
198 };
199
200 if (!Ptr.isLive() || Ptr.isZero() || Ptr.isUnknownSizeArray() ||
201 Ptr.isOnePastEnd())
202 return;
203
204 Blocks.insert(Ptr: Ptr.Pointee);
205
206 const Descriptor *Desc = Ptr.getFieldDesc();
207 if (!Desc)
208 return;
209
210 if (const Record *R = Desc->ElemRecord) {
211 if (!R->hasPtrField())
212 return;
213
214 for (const Record::Base &B : R->bases()) {
215 if (!B.R->hasPtrField())
216 continue;
217 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
218 collectBlocks(Ptr: BasePtr, Blocks, /*IsCompleteClass=*/false);
219 }
220
221 for (const Record::Field &F : R->fields()) {
222 if (!isOrHasPtr(D: F.Desc))
223 continue;
224 PtrView FieldPtr = Ptr.atField(Offset: F.Offset);
225 collectBlocks(Ptr: FieldPtr, Blocks);
226 }
227
228 if (IsCompleteClass) {
229 for (const Record::Base &B : R->virtual_bases()) {
230 if (!B.R->hasPtrField())
231 continue;
232 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
233 collectBlocks(Ptr: BasePtr, Blocks, /*IsCompleteClass=*/false);
234 }
235 }
236
237 return;
238 }
239
240 if (Desc->isPrimitive() && Desc->getPrimType() == PT_Ptr) {
241 Pointer Pointee = Ptr.deref<Pointer>();
242 if (isUsefulPtr(Pointee) && !Blocks.contains(Ptr: Pointee.block()))
243 collectBlocks(Ptr: Pointee.view(), Blocks);
244
245 return;
246 }
247
248 if (Desc->isPrimitiveArray() && Desc->getPrimType() == PT_Ptr) {
249 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
250 Pointer ElemPointee = Ptr.elem<Pointer>(I);
251 if (isUsefulPtr(ElemPointee) && !Blocks.contains(Ptr: ElemPointee.block()))
252 collectBlocks(Ptr: ElemPointee.view(), Blocks);
253 }
254 return;
255 }
256
257 if (Desc->isCompositeArray() && isOrHasPtr(D: Desc->ElemDesc)) {
258 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
259 PtrView ElemPtr = Ptr.atIndex(Idx: I).narrow();
260 collectBlocks(Ptr: ElemPtr, Blocks);
261 }
262 }
263}
264
265bool EvaluationResult::checkDynamicAllocations(InterpState &S,
266 const Pointer &Ptr,
267 SourceInfo Info) const {
268 if (!Ptr.isBlockPointer())
269 return true;
270
271 // Collect all blocks that this pointer (transitively) points to and
272 // return false if any of them is a dynamic block.
273 llvm::SmallPtrSet<const Block *, 4> Blocks;
274
275 collectBlocks(Ptr: Ptr.view(), Blocks);
276
277 for (const Block *B : Blocks) {
278 if (B->isDynamic()) {
279 assert(B->getDescriptor());
280 assert(B->getDescriptor()->asExpr());
281
282 bool IsSubobj = !Ptr.isRoot() || Ptr.isArrayElement();
283 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_dynamic_alloc)
284 << Ptr.getType()->isReferenceType() << IsSubobj;
285 S.Note(Loc: B->getDescriptor()->asExpr()->getExprLoc(),
286 DiagId: diag::note_constexpr_dynamic_alloc_here);
287 return false;
288 }
289 }
290
291 return true;
292}
293
294static bool isGlobalLValue(const Pointer &Ptr) {
295 if (Ptr.isDynamic())
296 return true;
297 if (Ptr.isTypeidPointer())
298 return true;
299
300 return ::isGlobalLValue(D: Ptr.getRootValueDecl(), E: Ptr.getRootExpr());
301}
302
303/// Check if the given function pointer can be returned from an evaluation.
304static bool checkFunctionPtr(InterpState &S, const Pointer &Ptr,
305 QualType PtrType, SourceInfo Info,
306 ConstantExprKind ConstexprKind) {
307 assert(Ptr.isFunctionPointer());
308 const FunctionPointer &FuncPtr = Ptr.asFunctionPointer();
309
310 if (!FuncPtr.Func)
311 return true;
312
313 const FunctionDecl *FD = FuncPtr.Func->getDecl();
314 // E.g. ObjC block pointers.
315 if (!FD)
316 return true;
317 if (FD->isImmediateFunction()) {
318 S.FFDiag(SI: Info, DiagId: diag::note_consteval_address_accessible)
319 << !PtrType->isAnyPointerType();
320 S.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
321 return false;
322 }
323
324 // __declspec(dllimport) must be handled very carefully:
325 // We must never initialize an expression with the thunk in C++.
326 // Doing otherwise would allow the same id-expression to yield
327 // different addresses for the same function in different translation
328 // units. However, this means that we must dynamically initialize the
329 // expression with the contents of the import address table at runtime.
330 //
331 // The C language has no notion of ODR; furthermore, it has no notion of
332 // dynamic initialization. This means that we are permitted to
333 // perform initialization with the address of the thunk.
334 if (S.getLangOpts().CPlusPlus && !isForManglingOnly(Kind: ConstexprKind) &&
335 FD->hasAttr<DLLImportAttr>())
336 // FIXME: Diagnostic!
337 return false;
338 return true;
339}
340
341static bool lvalFields(InterpState &S, const ASTContext &Ctx, PtrView Ptr,
342 QualType PtrType, SourceInfo Info,
343 ConstantExprKind ConstexprKind,
344 llvm::SmallPtrSet<const Block *, 4> &CheckedBlocks);
345static bool lval(InterpState &S, const ASTContext &Ctx, const Pointer &Ptr,
346 QualType PtrType, SourceInfo Info,
347 ConstantExprKind ConstexprKind,
348 llvm::SmallPtrSet<const Block *, 4> &CheckedBlocks) {
349 if (Ptr.isFunctionPointer())
350 return checkFunctionPtr(S, Ptr, PtrType, Info, ConstexprKind);
351
352 if (!Ptr.isBlockPointer() && !Ptr.isOpaquePointer())
353 return true;
354
355 const Expr *BaseE = Ptr.getRootExpr();
356 const ValueDecl *BaseVD = Ptr.getRootValueDecl();
357 assert(BaseE || BaseVD);
358 bool IsReferenceType = PtrType->isReferenceType();
359 bool IsSubObj = !Ptr.isRoot() || (Ptr.inArray() && !Ptr.isArrayRoot());
360
361 if (!isGlobalLValue(Ptr)) {
362 if (S.getLangOpts().CPlusPlus11) {
363 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
364 << IsReferenceType << IsSubObj << !!BaseVD << BaseVD;
365 const VarDecl *VarD = dyn_cast_if_present<VarDecl>(Val: BaseVD);
366 if (VarD && VarD->isConstexpr()) {
367 // Non-static local constexpr variables have unintuitive semantics:
368 // constexpr int a = 1;
369 // constexpr const int *p = &a;
370 // ... is invalid because the address of 'a' is not constant. Suggest
371 // adding a 'static' in this case.
372 S.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
373 << VarD
374 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
375 } else {
376 if (BaseVD)
377 S.Note(Loc: BaseVD->getLocation(), DiagId: diag::note_declared_at);
378 else if (BaseE)
379 S.Note(Loc: BaseE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
380 }
381 } else {
382 S.FFDiag(SI: Info);
383 }
384 return false;
385 }
386
387 if (const auto *VD = dyn_cast_if_present<VarDecl>(Val: BaseVD)) {
388 // We might allow creating pointers into invalid records, but do not allow
389 // returning them from an evaluation.
390 if (const RecordDecl *TypeRD = VD->getType()->getAsRecordDecl();
391 TypeRD && TypeRD->isInvalidDecl())
392 return false;
393 // Check if this is a thread-local variable.
394 if (VD->getTLSKind()) {
395 // FIXME: Diagnostic!
396 return false;
397 }
398
399 // A dllimport variable never acts like a constant, unless we're
400 // evaluating a value for use only in name mangling, and unless it's a
401 // static local. For the latter case, we'd still need to evaluate the
402 // constant expression in case we're inside a (inlined) function.
403 if (!isForManglingOnly(Kind: ConstexprKind) && VD->hasAttr<DLLImportAttr>() &&
404 !VD->isStaticLocal())
405 return false;
406
407 // Address of a managed variable is never a constant expression.
408 if (S.getLangOpts().CUDA && VD->hasAttr<HIPManagedAttr>())
409 return false;
410
411 // In CUDA/HIP device compilation, only device side variables have
412 // constant addresses.
413 if (S.getLangOpts().CUDA && S.getLangOpts().CUDAIsDevice &&
414 Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
415 if ((!VD->hasAttr<CUDADeviceAttr>() && !VD->hasAttr<CUDAConstantAttr>() &&
416 !VD->getType()->isCUDADeviceBuiltinSurfaceType() &&
417 !VD->getType()->isCUDADeviceBuiltinTextureType()))
418 return false;
419 }
420
421 return true;
422 }
423
424 if (const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(Val: BaseE)) {
425 QualType TempType = Ptr.getType();
426
427 if (TempType.isDestructedType()) {
428 S.FFDiag(Loc: MTE->getExprLoc(),
429 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
430 << TempType;
431 return false;
432 }
433
434 if (Ptr.getFieldDesc()->isPrimitive() &&
435 Ptr.getFieldDesc()->getPrimType() == PT_Ptr) {
436 // Recurse!
437 Pointer Pointee = Ptr.deref<Pointer>();
438 if (!Pointee.isBlockPointer() ||
439 CheckedBlocks.insert(Ptr: Pointee.block()).second) {
440 if (!lval(S, Ctx, Ptr: Pointee, PtrType: Pointee.getType(),
441 Info: Ptr.getDeclDesc()->getLoc(), ConstexprKind, CheckedBlocks))
442 return false;
443 }
444 } else {
445 if (!lvalFields(S, Ctx, Ptr: Ptr.view(), PtrType: TempType, Info, ConstexprKind,
446 CheckedBlocks))
447 return false;
448 }
449 }
450
451 return true;
452}
453
454static bool lvalFields(InterpState &S, const ASTContext &Ctx, PtrView Ptr,
455 QualType PtrType, SourceInfo Info,
456 ConstantExprKind ConstexprKind,
457 llvm::SmallPtrSet<const Block *, 4> &CheckedBlocks) {
458 assert(Ptr.isLive());
459 const Descriptor *FieldDesc = Ptr.getFieldDesc();
460 if (const Record *R = Ptr.getRecord()) {
461 if (!R->hasPtrField())
462 return true;
463
464 for (const Record::Base &B : R->bases()) {
465 if (!B.R->hasPtrField())
466 continue;
467
468 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
469 if (!lvalFields(S, Ctx, Ptr: BasePtr, PtrType: B.Desc->getType(), Info, ConstexprKind,
470 CheckedBlocks))
471 return false;
472 }
473
474 for (const Record::Field &F : R->fields()) {
475 PtrView FieldPtr = Ptr.atField(Offset: F.Offset);
476 if (!isOrHasPtr(D: F.Desc))
477 continue;
478
479 if (F.Desc->isPrimitive() && F.Desc->getPrimType() == PT_Ptr) {
480 Pointer Pointee = FieldPtr.deref<Pointer>();
481 if (!Pointee.isBlockPointer() ||
482 CheckedBlocks.insert(Ptr: Pointee.block()).second) {
483 QualType FieldType = F.Decl->getType();
484 if (!lval(S, Ctx, Ptr: Pointee, PtrType: FieldType, Info, ConstexprKind,
485 CheckedBlocks))
486 return false;
487 }
488 } else {
489 if (!lvalFields(S, Ctx, Ptr: FieldPtr, PtrType: F.Decl->getType(), Info,
490 ConstexprKind, CheckedBlocks))
491 return false;
492 }
493 }
494
495 for (const Record::Base &B : R->virtual_bases()) {
496 if (!B.R->hasPtrField())
497 continue;
498 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
499 if (!lvalFields(S, Ctx, Ptr: BasePtr, PtrType: B.Desc->getType(), Info, ConstexprKind,
500 CheckedBlocks))
501 return false;
502 }
503 return true;
504 }
505
506 if (FieldDesc->isPrimitiveArray() && FieldDesc->getPrimType() == PT_Ptr) {
507 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I) {
508 Pointer Pointee = Ptr.elem<Pointer>(I);
509
510 if (!Pointee.isBlockPointer() ||
511 CheckedBlocks.insert(Ptr: Pointee.block()).second) {
512 if (!lval(S, Ctx, Ptr: Pointee, PtrType: FieldDesc->getElemQualType(), Info,
513 ConstexprKind, CheckedBlocks))
514 return false;
515 }
516 }
517 return true;
518 }
519
520 if (FieldDesc->isCompositeArray()) {
521 if (FieldDesc->ElemRecord && !FieldDesc->ElemRecord->hasPtrField())
522 return true;
523
524 for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I) {
525 PtrView Elem = Ptr.atIndex(Idx: I).narrow();
526 if (!lvalFields(S, Ctx, Ptr: Elem, PtrType: FieldDesc->getElemQualType(), Info,
527 ConstexprKind, CheckedBlocks))
528 return false;
529 }
530 return true;
531 }
532 if (FieldDesc->isPrimitive() && FieldDesc->getPrimType() == PT_MemberPtr) {
533 MemberPointer MP = Ptr.deref<MemberPointer>();
534 if (!EvaluationResult::checkMemberPointer(S, MemberPtr: MP, Info, ConstexprKind))
535 return false;
536 }
537
538 return true;
539}
540
541/// Toplevel accessor to check all lvalue fields.
542bool EvaluationResult::checkLValueFields(InterpState &S, const Pointer &Ptr,
543 SourceInfo Info,
544 ConstantExprKind ConstexprKind) const {
545 if (!Ptr.isBlockPointer())
546 return true;
547
548 QualType SourceType = getStorageType();
549 llvm::SmallPtrSet<const Block *, 4> CheckedBlocks;
550
551 return lvalFields(S, Ctx: Ctx.getASTContext(), Ptr: Ptr.view(), PtrType: SourceType, Info,
552 ConstexprKind, CheckedBlocks);
553}
554
555bool EvaluationResult::checkLValue(InterpState &S, const Pointer &Ptr,
556 SourceInfo Info,
557 ConstantExprKind ConstexprKind) const {
558 QualType SourceType = getStorageType();
559 if (Ptr.isFunctionPointer())
560 return checkFunctionPtr(S, Ptr, PtrType: SourceType, Info, ConstexprKind);
561
562 if (Ptr.isZero())
563 return true;
564
565 bool IsReferenceType = SourceType->isReferenceType();
566 if (Ptr.isTypeidPointer()) {
567 if (isTemplateArgument(Kind: ConstexprKind)) {
568 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_invalid_template_arg)
569 << IsReferenceType << /*IsSubObj=*/false << /*InvalidBaseKind=*/0;
570 return false;
571 }
572 return true;
573 }
574
575 if (Ptr.isStringPointer()) {
576 // Additional restrictions apply in a template argument. We only enforce the
577 // C++20 restrictions here; additional syntactic and semantic restrictions
578 // are applied elsewhere.
579 if (isTemplateArgument(Kind: ConstexprKind)) {
580 bool IsSubObj = Ptr.asStringPointer().Decayed || Ptr.getIndex() != 0;
581 int InvalidBaseKind = -1;
582 StringRef Ident;
583 const Expr *BaseE = Ptr.asStringPointer().Base;
584 if (isa_and_nonnull<StringLiteral>(Val: BaseE))
585 InvalidBaseKind = 1;
586 else if (const auto *PE = dyn_cast_if_present<PredefinedExpr>(Val: BaseE)) {
587 InvalidBaseKind = 3;
588 Ident = PE->getIdentKindName();
589 }
590
591 if (InvalidBaseKind != -1) {
592 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_invalid_template_arg)
593 << IsReferenceType << IsSubObj << InvalidBaseKind << Ident;
594 return false;
595 }
596 }
597 return true;
598 }
599
600 if (!Ptr.isBlockPointer() && !Ptr.isOpaquePointer())
601 return true;
602
603 // Additional restrictions apply in a template argument. We only enforce the
604 // C++20 restrictions here; additional syntactic and semantic restrictions
605 // are applied elsewhere.
606 if (isTemplateArgument(Kind: ConstexprKind)) {
607 const Expr *BaseE = Ptr.getRootExpr();
608 const ValueDecl *BaseVD = Ptr.getRootValueDecl();
609 assert(BaseE || BaseVD);
610 if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
611 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD)) {
612 bool IsSubObj = !Ptr.isRoot() || (Ptr.inArray() && !Ptr.isArrayRoot());
613 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_invalid_template_arg)
614 << IsReferenceType << IsSubObj << 2;
615 return false;
616 }
617 }
618
619 llvm::SmallPtrSet<const Block *, 4> CheckedBlocks;
620 if (!lval(S, Ctx: Ctx.getASTContext(), Ptr, PtrType: SourceType, Info, ConstexprKind,
621 CheckedBlocks)) {
622 return false;
623 }
624
625 return true;
626}
627
628bool EvaluationResult::checkMemberPointer(InterpState &S,
629 const MemberPointer &MemberPtr,
630 SourceInfo Info,
631 ConstantExprKind ConstexprKind) {
632 const CXXMethodDecl *MD = MemberPtr.getMemberFunction();
633 if (!MD)
634 return true;
635
636 if (MD->isImmediateFunction()) {
637 S.FFDiag(SI: Info, DiagId: diag::note_consteval_address_accessible)
638 << /*pointer=*/false;
639 S.Note(Loc: MD->getLocation(), DiagId: diag::note_declared_at);
640 return false;
641 }
642
643 if (isForManglingOnly(Kind: ConstexprKind) || MD->isVirtual() ||
644 !MD->hasAttr<DLLImportAttr>()) {
645 return true;
646 }
647 return false;
648}
649
650bool EvaluationResult::checkFunctionPointer(
651 InterpState &S, const Pointer &Ptr, SourceInfo Info,
652 ConstantExprKind ConstexprKind) const {
653 return checkFunctionPtr(S, Ptr, PtrType: getStorageType(), Info, ConstexprKind);
654}
655
656} // namespace interp
657} // namespace clang
658