18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/SmallPtrSet.h"
25QualType EvaluationResult::getStorageType()
const {
26 if (
const auto *E = Source.asExpr()) {
30 return Ctx.getASTContext().getLValueReferenceType(E->getType());
33 if (
const auto *D = Source.asValueDecl())
40 assert(SubObjDecl &&
"Subobject declaration does not exist");
41 S.
FFDiag(Loc, diag::note_constexpr_uninitialized)
44 diag::note_constexpr_subobject_declared_here);
48 PtrView BasePtr,
const Record *R,
49 bool IsCompleteClass =
true);
73 for (
size_t I = 0; I != NumElems; ++I) {
77 }
else if (ElemDesc->
isArray()) {
78 for (
size_t I = 0; I != NumElems; ++I) {
89 bool IsCompleteClass) {
93 for (
const Record::Field &F : R->fields()) {
97 if (R->isUnion() && !FieldPtr.
isActive())
100 QualType FieldType = F.Decl->getType();
107 }
else if (F.Decl->isUnnamedBitField()) {
109 }
else if (FieldDesc->
isArray()) {
117 auto diagnoseBase = [&](
const Record::Base &B,
unsigned Index) ->
bool {
119 if (
const auto *CD = dyn_cast_if_present<CXXRecordDecl>(R->getDecl())) {
120 const auto &BS = *std::next(CD->bases_begin(), Index);
122 S.
FFDiag(TypeBeginLoc, diag::note_constexpr_uninitialized_base)
123 << B.Desc->getType() <<
SourceRange(TypeBeginLoc, BS.getEndLoc());
126 << B.Desc->getType();
132 for (
auto [I, B] : llvm::enumerate(R->bases())) {
135 return diagnoseBase(B, I);
140 if (IsCompleteClass) {
141 for (
auto [I, B] : llvm::enumerate(R->virtual_bases())) {
144 return diagnoseBase(B, I);
160 if (!Ptr.isBlockPointer())
169 if (
const auto *D = Source.asDecl())
170 InitLoc =
cast<VarDecl>(D)->getAnyInitializer()->getExprLoc();
171 else if (
const auto *E = Source.asExpr())
172 InitLoc = E->getExprLoc();
174 if (
const Record *R = Ptr.getRecord())
177 if (isa_and_nonnull<ConstantArrayType>(Ptr.getType()->getAsArrayTypeUnsafe()))
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();
200 if (!Ptr.isLive() || Ptr.isZero() || Ptr.isUnknownSizeArray() ||
204 Blocks.insert(Ptr.Pointee);
211 if (!R->hasPtrField())
214 for (
const Record::Base &B : R->bases()) {
215 if (!B.R->hasPtrField())
217 PtrView BasePtr = Ptr.atField(B.Offset);
221 for (
const Record::Field &F : R->fields()) {
224 PtrView FieldPtr = Ptr.atField(F.Offset);
228 if (IsCompleteClass) {
229 for (
const Record::Base &B : R->virtual_bases()) {
230 if (!B.R->hasPtrField())
232 PtrView BasePtr = Ptr.atField(B.Offset);
242 if (isUsefulPtr(Pointee) && !Blocks.contains(Pointee.
block()))
249 for (
unsigned I = 0; I != Desc->
getNumElems(); ++I) {
251 if (isUsefulPtr(ElemPointee) && !Blocks.contains(ElemPointee.
block()))
258 for (
unsigned I = 0; I != Desc->
getNumElems(); ++I) {
259 PtrView ElemPtr = Ptr.atIndex(I).narrow();
268 if (!Ptr.isBlockPointer())
277 for (
const Block *B : Blocks) {
278 if (B->isDynamic()) {
279 assert(B->getDescriptor());
280 assert(B->getDescriptor()->asExpr());
282 bool IsSubobj = !Ptr.isRoot() || Ptr.isArrayElement();
283 S.
FFDiag(Info, diag::note_constexpr_dynamic_alloc)
284 << Ptr.getType()->isReferenceType() << IsSubobj;
285 S.
Note(B->getDescriptor()->asExpr()->getExprLoc(),
286 diag::note_constexpr_dynamic_alloc_here);
297 if (Ptr.isTypeidPointer())
300 return ::isGlobalLValue(Ptr.getRootValueDecl(), Ptr.getRootExpr());
307 assert(Ptr.isFunctionPointer());
318 S.
FFDiag(Info, diag::note_consteval_address_accessible)
349 if (Ptr.isFunctionPointer())
352 if (!Ptr.isBlockPointer() && !Ptr.isOpaquePointer())
355 const Expr *BaseE = Ptr.getRootExpr();
356 const ValueDecl *BaseVD = Ptr.getRootValueDecl();
357 assert(BaseE || BaseVD);
359 bool IsSubObj = !Ptr.isRoot() || (Ptr.inArray() && !Ptr.isArrayRoot());
363 S.
FFDiag(Info, diag::note_constexpr_non_global, 1)
364 << IsReferenceType << IsSubObj << !!BaseVD << BaseVD;
365 const VarDecl *VarD = dyn_cast_if_present<VarDecl>(BaseVD);
377 S.
Note(BaseVD->getLocation(), diag::note_declared_at);
387 if (
const auto *VD = dyn_cast_if_present<VarDecl>(BaseVD)) {
390 if (
const RecordDecl *TypeRD = VD->getType()->getAsRecordDecl();
394 if (VD->getTLSKind()) {
404 !VD->isStaticLocal())
408 if (S.
getLangOpts().CUDA && VD->hasAttr<HIPManagedAttr>())
415 if ((!VD->hasAttr<CUDADeviceAttr>() && !VD->hasAttr<CUDAConstantAttr>() &&
416 !VD->getType()->isCUDADeviceBuiltinSurfaceType() &&
417 !VD->getType()->isCUDADeviceBuiltinTextureType()))
424 if (
const auto *MTE = dyn_cast_if_present<MaterializeTemporaryExpr>(BaseE)) {
428 S.
FFDiag(MTE->getExprLoc(),
429 diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
434 if (Ptr.getFieldDesc()->isPrimitive() &&
435 Ptr.getFieldDesc()->getPrimType() ==
PT_Ptr) {
439 CheckedBlocks.insert(Pointee.
block()).second) {
441 Ptr.getDeclDesc()->getLoc(), ConstexprKind, CheckedBlocks))
445 if (!
lvalFields(S, Ctx, Ptr.view(), TempType, Info, ConstexprKind,
458 assert(Ptr.isLive());
459 const Descriptor *FieldDesc = Ptr.getFieldDesc();
460 if (
const Record *R = Ptr.getRecord()) {
461 if (!R->hasPtrField())
464 for (
const Record::Base &B : R->bases()) {
465 if (!B.R->hasPtrField())
468 PtrView BasePtr = Ptr.atField(B.Offset);
474 for (
const Record::Field &F : R->fields()) {
475 PtrView FieldPtr = Ptr.atField(F.Offset);
479 if (F.Desc->isPrimitive() && F.Desc->getPrimType() ==
PT_Ptr) {
482 CheckedBlocks.insert(Pointee.
block()).second) {
483 QualType FieldType = F.Decl->getType();
484 if (!
lval(S, Ctx, Pointee, FieldType, Info, ConstexprKind,
490 ConstexprKind, CheckedBlocks))
495 for (
const Record::Base &B : R->virtual_bases()) {
496 if (!B.R->hasPtrField())
498 PtrView BasePtr = Ptr.atField(B.Offset);
507 for (
unsigned I = 0; I != FieldDesc->
getNumElems(); ++I) {
511 CheckedBlocks.insert(Pointee.
block()).second) {
513 ConstexprKind, CheckedBlocks))
524 for (
unsigned I = 0; I != FieldDesc->
getNumElems(); ++I) {
525 PtrView Elem = Ptr.atIndex(I).narrow();
527 ConstexprKind, CheckedBlocks))
545 if (!Ptr.isBlockPointer())
548 QualType SourceType = getStorageType();
552 ConstexprKind, CheckedBlocks);
558 QualType SourceType = getStorageType();
559 if (Ptr.isFunctionPointer())
566 if (Ptr.isTypeidPointer()) {
568 S.
FFDiag(Info, diag::note_constexpr_invalid_template_arg)
569 << IsReferenceType <<
false << 0;
575 if (Ptr.isStringPointer()) {
580 bool IsSubObj = Ptr.asStringPointer().Decayed || Ptr.getIndex() != 0;
581 int InvalidBaseKind = -1;
583 const Expr *BaseE = Ptr.asStringPointer().Base;
584 if (isa_and_nonnull<StringLiteral>(BaseE))
586 else if (
const auto *PE = dyn_cast_if_present<PredefinedExpr>(BaseE)) {
588 Ident = PE->getIdentKindName();
591 if (InvalidBaseKind != -1) {
592 S.
FFDiag(Info, diag::note_constexpr_invalid_template_arg)
593 << IsReferenceType << IsSubObj << InvalidBaseKind << Ident;
600 if (!Ptr.isBlockPointer() && !Ptr.isOpaquePointer())
607 const Expr *BaseE = Ptr.getRootExpr();
608 const ValueDecl *BaseVD = Ptr.getRootValueDecl();
609 assert(BaseE || BaseVD);
610 if (isa_and_nonnull<MaterializeTemporaryExpr>(BaseE) ||
611 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(BaseVD)) {
612 bool IsSubObj = !Ptr.isRoot() || (Ptr.inArray() && !Ptr.isArrayRoot());
613 S.
FFDiag(Info, diag::note_constexpr_invalid_template_arg)
614 << IsReferenceType << IsSubObj << 2;
637 S.
FFDiag(Info, diag::note_consteval_address_accessible)
644 !MD->
hasAttr<DLLImportAttr>()) {
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
bool isTemplateArgument(ConstantExprKind Kind)
bool isForManglingOnly(ConstantExprKind Kind)
Determines whether the given kind of constant expression is only ever used for name mangling.
llvm::MachO::Record Record
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
struct clang::ASTContext::CUDAConstantEvalContext CUDAConstantEvalCtx
Represents a static or instance method of a struct/union/class.
bool isInvalidDecl() const
SourceLocation getLocation() const
SourceLocation getBeginLoc() const LLVM_READONLY
This represents one expression.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a member of a struct/union/class.
static FixItHint CreateInsertion(SourceLocation InsertionLoc, StringRef Code, bool BeforePreviousInsertions=false)
Create a code modification hint that inserts the given code string at a specific location.
Represents a function declaration or definition.
bool isImmediateFunction() const
A (possibly-)qualified type.
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
Represents a struct/union/class.
Encodes a location in the source.
A trivial tuple used to represent a source range.
bool isIncompleteArrayType() const
bool isReferenceType() const
bool isAnyPointerType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
A memory block, either on the stack or in the heap.
static bool checkMemberPointer(InterpState &S, const MemberPointer &MemberPtr, SourceInfo Info, ConstantExprKind ConstexprKind)
Check if the given member pointer can be returned from an evaluation.
bool checkFunctionPointer(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check if the given function pointer can be returned from an evaluation.
bool checkLValue(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check the given pointer as an lvalue, i.e.
bool checkDynamicAllocations(InterpState &S, const Pointer &Ptr, SourceInfo Info) const
Check that none of the blocks the given pointer (transitively) points to are dynamically allocated.
bool checkFullyInitialized(InterpState &S, const Pointer &Ptr) const
Check that all subobjects of the given pointer have been initialized.
bool checkLValueFields(InterpState &S, const Pointer &Ptr, SourceInfo Info, ConstantExprKind ConstexprKind) const
Check all fields of the given pointer.
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
A pointer to a memory block, live or dead.
QualType getType() const
Returns the type of the innermost field.
bool isBlockPointer() const
const Block * block() const
Structure/Class descriptor.
bool hasPtrField() const
If this record (or any of its bases) contains a field of type PT_Ptr.
Describes the statement/declaration an opcode was generated from.
OptionalDiagnostic Note(SourceLocation Loc, diag::kind DiagId)
Add a note to a prior diagnostic.
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Diagnose that the evaluation could not be folded (FF => FoldFailure)
ASTContext & getASTContext() const
const LangOptions & getLangOpts() const
static bool lvalFields(InterpState &S, const ASTContext &Ctx, PtrView Ptr, QualType PtrType, SourceInfo Info, ConstantExprKind ConstexprKind, llvm::SmallPtrSet< const Block *, 4 > &CheckedBlocks)
static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc, PtrView BasePtr)
static bool isOrHasPtr(const Descriptor *D)
static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc, PtrView BasePtr, const Record *R, bool IsCompleteClass=true)
static bool isGlobalLValue(const Pointer &Ptr)
static void DiagnoseUninitializedSubobject(InterpState &S, SourceLocation Loc, const FieldDecl *SubObjDecl)
static bool lval(InterpState &S, const ASTContext &Ctx, const Pointer &Ptr, QualType PtrType, SourceInfo Info, ConstantExprKind ConstexprKind, llvm::SmallPtrSet< const Block *, 4 > &CheckedBlocks)
static bool checkFunctionPtr(InterpState &S, const Pointer &Ptr, QualType PtrType, SourceInfo Info, ConstantExprKind ConstexprKind)
Check if the given function pointer can be returned from an evaluation.
static void collectBlocks(PtrView Ptr, llvm::SmallPtrSet< const Block *, 4 > &Blocks, bool IsCompleteClass=true)
Top level wrappers for InstallAPI frontend operations.
Expr::ConstantExprKind ConstantExprKind
@ Result
The result type of a method or function.
U cast(CodeGen::Address addr)
bool NoWrongSidedVars
Do not allow wrong-sided variables in constant expressions.
Describes a memory block created by an allocation site.
unsigned getNumElems() const
Returns the number of elements stored in the block.
SourceLocation getLocation() const
bool isPrimitive() const
Checks if the descriptor is of a primitive.
QualType getElemQualType() const
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
const Descriptor *const ElemDesc
Descriptor of the array element.
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
PrimType getPrimType() const
bool isRecord() const
Checks if the descriptor is of a record.
const Record *const ElemRecord
Pointer to the record, if block contains records.
bool isArray() const
Checks if the descriptor is of an array.
const Descriptor * getDeclDesc() const
bool allElementsInitialized() const
PtrView atField(unsigned Offset) const
const Record * getRecord() const
const Descriptor * getFieldDesc() const
const FieldDecl * getField() const
PtrView atIndex(unsigned Idx) const
bool isInitialized() const