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 "Context.h"
11#include "InterpState.h"
12#include "Record.h"
13#include "clang/AST/ExprCXX.h"
14
15namespace clang {
16namespace interp {
17
18APValue EvaluationResult::toAPValue() const {
19 assert(!empty());
20 switch (Kind) {
21 case LValue:
22 // Either a pointer or a function pointer.
23 if (const auto *P = std::get_if<Pointer>(ptr: &Value))
24 return P->toAPValue();
25 else if (const auto *FP = std::get_if<FunctionPointer>(ptr: &Value))
26 return FP->toAPValue();
27 else
28 llvm_unreachable("Unhandled LValue type");
29 break;
30 case RValue:
31 return std::get<APValue>(v: Value);
32 case Valid:
33 return APValue();
34 default:
35 llvm_unreachable("Unhandled result kind?");
36 }
37}
38
39std::optional<APValue> EvaluationResult::toRValue() const {
40 if (Kind == RValue)
41 return toAPValue();
42
43 assert(Kind == LValue);
44
45 // We have a pointer and want an RValue.
46 if (const auto *P = std::get_if<Pointer>(ptr: &Value))
47 return P->toRValue(Ctx: *Ctx);
48 else if (const auto *FP = std::get_if<FunctionPointer>(ptr: &Value)) // Nope
49 return FP->toAPValue();
50 llvm_unreachable("Unhandled lvalue kind");
51}
52
53static void DiagnoseUninitializedSubobject(InterpState &S, SourceLocation Loc,
54 const FieldDecl *SubObjDecl) {
55 assert(SubObjDecl && "Subobject declaration does not exist");
56 S.FFDiag(Loc, diag::note_constexpr_uninitialized)
57 << /*(name)*/ 1 << SubObjDecl;
58 S.Note(SubObjDecl->getLocation(),
59 diag::note_constexpr_subobject_declared_here);
60}
61
62static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
63 const Pointer &BasePtr, const Record *R);
64
65static bool CheckArrayInitialized(InterpState &S, SourceLocation Loc,
66 const Pointer &BasePtr,
67 const ConstantArrayType *CAT) {
68 bool Result = true;
69 size_t NumElems = CAT->getZExtSize();
70 QualType ElemType = CAT->getElementType();
71
72 if (ElemType->isRecordType()) {
73 const Record *R = BasePtr.getElemRecord();
74 for (size_t I = 0; I != NumElems; ++I) {
75 Pointer ElemPtr = BasePtr.atIndex(Idx: I).narrow();
76 Result &= CheckFieldsInitialized(S, Loc, BasePtr: ElemPtr, R);
77 }
78 } else if (const auto *ElemCAT = dyn_cast<ConstantArrayType>(ElemType)) {
79 for (size_t I = 0; I != NumElems; ++I) {
80 Pointer ElemPtr = BasePtr.atIndex(Idx: I).narrow();
81 Result &= CheckArrayInitialized(S, Loc, ElemPtr, ElemCAT);
82 }
83 } else {
84 for (size_t I = 0; I != NumElems; ++I) {
85 if (!BasePtr.atIndex(Idx: I).isInitialized()) {
86 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: BasePtr.getField());
87 Result = false;
88 }
89 }
90 }
91
92 return Result;
93}
94
95static bool CheckFieldsInitialized(InterpState &S, SourceLocation Loc,
96 const Pointer &BasePtr, const Record *R) {
97 assert(R);
98 bool Result = true;
99 // Check all fields of this record are initialized.
100 for (const Record::Field &F : R->fields()) {
101 Pointer FieldPtr = BasePtr.atField(Off: F.Offset);
102 QualType FieldType = F.Decl->getType();
103
104 if (FieldType->isRecordType()) {
105 Result &= CheckFieldsInitialized(S, Loc, BasePtr: FieldPtr, R: FieldPtr.getRecord());
106 } else if (FieldType->isIncompleteArrayType()) {
107 // Nothing to do here.
108 } else if (F.Decl->isUnnamedBitField()) {
109 // Nothing do do here.
110 } else if (FieldType->isArrayType()) {
111 const auto *CAT =
112 cast<ConstantArrayType>(FieldType->getAsArrayTypeUnsafe());
113 Result &= CheckArrayInitialized(S, Loc, FieldPtr, CAT);
114 } else if (!FieldPtr.isInitialized()) {
115 DiagnoseUninitializedSubobject(S, Loc, SubObjDecl: F.Decl);
116 Result = false;
117 }
118 }
119
120 // Check Fields in all bases
121 for (const Record::Base &B : R->bases()) {
122 Pointer P = BasePtr.atField(Off: B.Offset);
123 if (!P.isInitialized()) {
124 S.FFDiag(BasePtr.getDeclDesc()->asDecl()->getLocation(),
125 diag::note_constexpr_uninitialized_base)
126 << B.Desc->getType();
127 return false;
128 }
129 Result &= CheckFieldsInitialized(S, Loc, BasePtr: P, R: B.R);
130 }
131
132 // TODO: Virtual bases
133
134 return Result;
135}
136
137bool EvaluationResult::checkFullyInitialized(InterpState &S,
138 const Pointer &Ptr) const {
139 assert(Source);
140 assert(empty());
141
142 // Our Source must be a VarDecl.
143 const Decl *SourceDecl = Source.dyn_cast<const Decl *>();
144 assert(SourceDecl);
145 const auto *VD = cast<VarDecl>(Val: SourceDecl);
146 assert(VD->getType()->isRecordType() || VD->getType()->isArrayType());
147 SourceLocation InitLoc = VD->getAnyInitializer()->getExprLoc();
148
149 assert(!Ptr.isZero());
150
151 if (const Record *R = Ptr.getRecord())
152 return CheckFieldsInitialized(S, Loc: InitLoc, BasePtr: Ptr, R);
153 const auto *CAT =
154 cast<ConstantArrayType>(Ptr.getType()->getAsArrayTypeUnsafe());
155 return CheckArrayInitialized(S, InitLoc, Ptr, CAT);
156}
157
158void EvaluationResult::dump() const {
159 assert(Ctx);
160 auto &OS = llvm::errs();
161 const ASTContext &ASTCtx = Ctx->getASTContext();
162
163 switch (Kind) {
164 case Empty:
165 OS << "Empty\n";
166 break;
167 case RValue:
168 OS << "RValue: ";
169 std::get<APValue>(v: Value).dump(OS, Context: ASTCtx);
170 break;
171 case LValue: {
172 assert(Source);
173 QualType SourceType;
174 if (const auto *D = Source.dyn_cast<const Decl *>()) {
175 if (const auto *VD = dyn_cast<ValueDecl>(Val: D))
176 SourceType = VD->getType();
177 } else if (const auto *E = Source.dyn_cast<const Expr *>()) {
178 SourceType = E->getType();
179 }
180
181 OS << "LValue: ";
182 if (const auto *P = std::get_if<Pointer>(ptr: &Value))
183 P->toAPValue().printPretty(OS, Ctx: ASTCtx, Ty: SourceType);
184 else if (const auto *FP = std::get_if<FunctionPointer>(ptr: &Value)) // Nope
185 FP->toAPValue().printPretty(OS, Ctx: ASTCtx, Ty: SourceType);
186 OS << "\n";
187 break;
188 }
189 case Invalid:
190 OS << "Invalid\n";
191 break;
192 case Valid:
193 OS << "Valid\n";
194 break;
195 }
196}
197
198} // namespace interp
199} // namespace clang
200

source code of clang/lib/AST/Interp/EvaluationResult.cpp