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 "InterpState.h"
11#include "Pointer.h"
12#include "Record.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SetVector.h"
15#include <iterator>
16
17namespace clang {
18namespace interp {
19
20static void DiagnoseUninitializedSubobject(InterpState &S, SourceLocation Loc,
21 const FieldDecl *SubObjDecl) {
22 assert(SubObjDecl && "Subobject declaration does not exist");
23 S.FFDiag(Loc, DiagId: diag::note_constexpr_uninitialized)
24 << /*(name)*/ 1 << SubObjDecl;
25 S.Note(Loc: SubObjDecl->getLocation(),
26 DiagId: diag::note_constexpr_subobject_declared_here);
27}
28
29static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
30 PtrView BasePtr, const Record *R,
31 bool IsCompleteClass = true);
32
33static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc,
34 PtrView BasePtr) {
35 const Descriptor *BaseDesc = BasePtr.getFieldDesc();
36 assert(BaseDesc->isArray());
37
38 size_t NumElems = BaseDesc->getNumElems();
39 if (NumElems == 0)
40 return true;
41
42 bool Result = true;
43
44 if (BaseDesc->isPrimitiveArray()) {
45 if (BasePtr.allElementsInitialized())
46 return true;
47 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: BasePtr.getField());
48 return false;
49 }
50 const Descriptor *ElemDesc = BaseDesc->ElemDesc;
51
52 if (ElemDesc->isRecord()) {
53 const Record *R = ElemDesc->ElemRecord;
54 for (size_t I = 0; I != NumElems; ++I) {
55 PtrView ElemPtr = BasePtr.atIndex(Idx: I).narrow();
56 Result &= CheckFieldsInitialized(S, Loc, BasePtr: ElemPtr, R);
57 }
58 } else {
59 assert(ElemDesc->isArray());
60 for (size_t I = 0; I != NumElems; ++I) {
61 PtrView ElemPtr = BasePtr.atIndex(Idx: I).narrow();
62 Result &= CheckArrayInitialized(S, Loc, BasePtr: ElemPtr);
63 }
64 }
65
66 return Result;
67}
68
69static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
70 PtrView BasePtr, const Record *R,
71 bool IsCompleteClass) {
72 assert(R);
73 bool Result = true;
74 // Check all fields of this record are initialized.
75 for (const Record::Field &F : R->fields()) {
76 PtrView FieldPtr = BasePtr.atField(Offset: F.Offset);
77
78 // Don't check inactive union members.
79 if (R->isUnion() && !FieldPtr.isActive())
80 continue;
81
82 QualType FieldType = F.Decl->getType();
83 const Descriptor *FieldDesc = FieldPtr.getFieldDesc();
84
85 if (FieldDesc->isRecord()) {
86 Result &= CheckFieldsInitialized(S, Loc, BasePtr: FieldPtr, R: FieldPtr.getRecord());
87 } else if (FieldType->isIncompleteArrayType()) {
88 // Nothing to do here.
89 } else if (F.Decl->isUnnamedBitField()) {
90 // Nothing do do here.
91 } else if (FieldDesc->isArray()) {
92 Result &= CheckArrayInitialized(S, Loc, BasePtr: FieldPtr);
93 } else if (!FieldPtr.isInitialized()) {
94 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: F.Decl);
95 Result = false;
96 }
97 }
98
99 auto diagnoseBase = [&](const Record::Base &B, unsigned Index) -> bool {
100 const Descriptor *Desc = BasePtr.getDeclDesc();
101 if (const auto *CD = dyn_cast_if_present<CXXRecordDecl>(Val: R->getDecl())) {
102 const auto &BS = *std::next(x: CD->bases_begin(), n: Index);
103 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
104 S.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
105 << B.Desc->getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
106 } else {
107 S.FFDiag(Loc: Desc->getLocation(), DiagId: diag::note_constexpr_uninitialized_base)
108 << B.Desc->getType();
109 }
110 return false;
111 };
112
113 // Check Fields in all bases.
114 for (auto [I, B] : llvm::enumerate(First: R->bases())) {
115 PtrView P = BasePtr.atField(Offset: B.Offset);
116 if (!P.isInitialized())
117 return diagnoseBase(B, I);
118 Result &= CheckFieldsInitialized(S, Loc, BasePtr: P, R: B.R, /*IsCompleteClass=*/false);
119 }
120
121 // And virtual bases.
122 if (IsCompleteClass) {
123 for (auto [I, B] : llvm::enumerate(First: R->virtual_bases())) {
124 PtrView P = BasePtr.atField(Offset: B.Offset);
125 if (!P.isInitialized())
126 return diagnoseBase(B, I);
127 Result &=
128 CheckFieldsInitialized(S, Loc, BasePtr: P, R: B.R, /*IsCompleteClass=*/false);
129 }
130 }
131
132 return Result;
133}
134
135bool EvaluationResult::checkFullyInitialized(InterpState &S,
136 const Pointer &Ptr) const {
137 assert(Source);
138 assert(empty());
139
140 if (Ptr.isZero())
141 return true;
142 if (!Ptr.isBlockPointer())
143 return true;
144
145 // We can't inspect dead pointers at all. Return true here so we can
146 // diagnose them later.
147 if (!Ptr.isLive())
148 return true;
149
150 SourceLocation InitLoc;
151 if (const auto *D = dyn_cast<const Decl *>(Val: Source))
152 InitLoc = cast<VarDecl>(Val: D)->getAnyInitializer()->getExprLoc();
153 else if (const auto *E = dyn_cast<const Expr *>(Val: Source))
154 InitLoc = E->getExprLoc();
155
156 if (const Record *R = Ptr.getRecord())
157 return CheckFieldsInitialized(S, Loc: InitLoc, BasePtr: Ptr.view(), R);
158
159 if (isa_and_nonnull<ConstantArrayType>(Val: Ptr.getType()->getAsArrayTypeUnsafe()))
160 return CheckArrayInitialized(S, Loc: InitLoc, BasePtr: Ptr.view());
161
162 return true;
163}
164
165static bool isOrHasPtr(const Descriptor *D) {
166 if ((D->isPrimitive() || D->isPrimitiveArray()) && D->getPrimType() == PT_Ptr)
167 return true;
168
169 if (D->ElemRecord)
170 return D->ElemRecord->hasPtrField();
171 return false;
172}
173
174static void collectBlocks(PtrView Ptr, llvm::SetVector<const Block *> &Blocks,
175 bool IsCompleteClass = true) {
176 auto isUsefulPtr = [](const Pointer &P) -> bool {
177 return P.isLive() && P.isBlockPointer() && !P.isZero() && !P.isDummy() &&
178 P.isDereferencable() && !P.isUnknownSizeArray() && !P.isOnePastEnd();
179 };
180
181 if (!Ptr.isLive() || Ptr.isZero() || Ptr.isDummy() ||
182 Ptr.isUnknownSizeArray() || Ptr.isOnePastEnd())
183 return;
184
185 Blocks.insert(X: Ptr.Pointee);
186
187 const Descriptor *Desc = Ptr.getFieldDesc();
188 if (!Desc)
189 return;
190
191 if (const Record *R = Desc->ElemRecord) {
192 if (!R->hasPtrField())
193 return;
194
195 for (const Record::Base &B : R->bases()) {
196 if (!B.R->hasPtrField())
197 continue;
198 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
199 collectBlocks(Ptr: BasePtr, Blocks, /*IsCompleteClass=*/false);
200 }
201
202 for (const Record::Field &F : R->fields()) {
203 if (!isOrHasPtr(D: F.Desc))
204 continue;
205 PtrView FieldPtr = Ptr.atField(Offset: F.Offset);
206 collectBlocks(Ptr: FieldPtr, Blocks);
207 }
208
209 if (IsCompleteClass) {
210 for (const Record::Base &B : R->virtual_bases()) {
211 if (!B.R->hasPtrField())
212 continue;
213 PtrView BasePtr = Ptr.atField(Offset: B.Offset);
214 collectBlocks(Ptr: BasePtr, Blocks, /*IsCompleteClass=*/false);
215 }
216 }
217
218 return;
219 }
220
221 if (Desc->isPrimitive() && Desc->getPrimType() == PT_Ptr) {
222 Pointer Pointee = Ptr.deref<Pointer>();
223 if (isUsefulPtr(Pointee) && !Blocks.contains(key: Pointee.block()))
224 collectBlocks(Ptr: Pointee.view(), Blocks);
225
226 return;
227 }
228
229 if (Desc->isPrimitiveArray() && Desc->getPrimType() == PT_Ptr) {
230 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
231 Pointer ElemPointee = Ptr.elem<Pointer>(I);
232 if (isUsefulPtr(ElemPointee) && !Blocks.contains(key: ElemPointee.block()))
233 collectBlocks(Ptr: ElemPointee.view(), Blocks);
234 }
235 return;
236 }
237
238 if (Desc->isCompositeArray() && isOrHasPtr(D: Desc->ElemDesc)) {
239 for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
240 PtrView ElemPtr = Ptr.atIndex(Idx: I).narrow();
241 collectBlocks(Ptr: ElemPtr, Blocks);
242 }
243 }
244}
245
246bool EvaluationResult::checkDynamicAllocations(InterpState &S,
247 const Context &Ctx,
248 const Pointer &Ptr,
249 SourceInfo Info) {
250 if (!Ptr.isBlockPointer())
251 return true;
252 // Collect all blocks that this pointer (transitively) points to and
253 // return false if any of them is a dynamic block.
254 llvm::SetVector<const Block *> Blocks;
255
256 collectBlocks(Ptr: Ptr.view(), Blocks);
257
258 for (const Block *B : Blocks) {
259 if (B->isDynamic()) {
260 assert(B->getDescriptor());
261 assert(B->getDescriptor()->asExpr());
262
263 bool IsSubobj = !Ptr.isRoot() || Ptr.isArrayElement();
264 S.FFDiag(SI: Info, DiagId: diag::note_constexpr_dynamic_alloc)
265 << Ptr.getType()->isReferenceType() << IsSubobj;
266 S.Note(Loc: B->getDescriptor()->asExpr()->getExprLoc(),
267 DiagId: diag::note_constexpr_dynamic_alloc_here);
268 return false;
269 }
270 }
271
272 return true;
273}
274
275} // namespace interp
276} // namespace clang
277