1//===--- Context.h - Context for the constexpr 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// Defines the constexpr execution context.
10//
11// The execution context manages cached bytecode and the global context.
12// It invokes the compiler and interpreter, propagating errors.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_AST_INTERP_CONTEXT_H
17#define LLVM_CLANG_AST_INTERP_CONTEXT_H
18
19#include "InterpStack.h"
20#include "clang/AST/ASTContext.h"
21
22namespace clang {
23class LangOptions;
24class FunctionDecl;
25class VarDecl;
26class APValue;
27class BlockExpr;
28
29namespace interp {
30class Function;
31class Program;
32class State;
33enum PrimType : uint8_t;
34
35struct ParamOffset {
36 unsigned Offset;
37 bool IsPtr;
38};
39
40struct FuncParam {
41 unsigned Index;
42 bool IsPtr;
43};
44
45class EvalIDScope;
46/// Holds all information required to evaluate constexpr code in a module.
47class Context final {
48public:
49 /// Initialises the constexpr VM.
50 explicit Context(ASTContext &Ctx);
51
52 /// Cleans up the constexpr VM.
53 ~Context();
54
55 /// Checks if a function is a potential constant expression.
56 bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FD);
57 void isPotentialConstantExprUnevaluated(State &Parent, const Expr *E,
58 const FunctionDecl *FD);
59
60 /// Evaluates a toplevel expression as an rvalue.
61 bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
62
63 /// Like evaluateAsRvalue(), but does no implicit lvalue-to-rvalue conversion.
64 bool evaluate(State &Parent, const Expr *E, APValue &Result,
65 ConstantExprKind Kind);
66
67 /// Evaluates a toplevel initializer.
68 bool evaluateAsInitializer(State &Parent, const VarDecl *VD, const Expr *Init,
69 APValue &Result);
70
71 /// Evaluates the destruction of a variable.
72 bool evaluateDestruction(State &Parent, const VarDecl *VD, APValue Value);
73
74 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
75 const Expr *PtrExpr, APValue &Result);
76 bool evaluateCharRange(State &Parent, const Expr *SizeExpr,
77 const Expr *PtrExpr, std::string &Result);
78
79 /// Evaluate \param E and if it can be evaluated to a null-terminated string,
80 /// copy the result into \param Result.
81 bool evaluateString(State &Parent, const Expr *E, std::string &Result);
82
83 /// Evalute \param E and if it can be evaluated to a string literal,
84 /// run strlen() on it.
85 std::optional<uint64_t> evaluateStrlen(State &Parent, const Expr *E);
86
87 /// If \param E evaluates to a pointer the number of accessible bytes
88 /// past the pointer is estimated in \param Result as if evaluated by
89 /// the builtin function __builtin_object_size. This is a best effort
90 /// approximation, when Kind & 2 == 0 the object size is less
91 /// than or equal to the estimated size, when Kind & 2 == 1 the
92 /// true value is greater than or equal to the estimated size.
93 /// When Kind & 1 == 1 only bytes belonging to the same subobject
94 /// as the one referred to by E are considered, when Kind & 1 == 0
95 /// bytes belonging to the same storage (stack, heap allocation,
96 /// global variable) are considered.
97 std::optional<uint64_t> tryEvaluateObjectSize(State &Parent, const Expr *E,
98 unsigned Kind);
99
100 std::optional<bool> evaluateWithSubstitution(State &Parent,
101 const FunctionDecl *Callee,
102 ArrayRef<const Expr *> Args,
103 const Expr *This,
104 const Expr *Condition);
105
106 /// Returns the AST context.
107 ASTContext &getASTContext() const { return Ctx; }
108 /// Returns the language options.
109 const LangOptions &getLangOpts() const;
110 /// Returns CHAR_BIT.
111 unsigned getCharBit() const;
112 /// Return the floating-point semantics for T.
113 const llvm::fltSemantics &getFloatSemantics(QualType T) const;
114 /// Return the size of T in bits.
115 uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
116
117 /// Classifies a type.
118 OptPrimType classify(QualType T) const;
119
120 /// Classifies an expression.
121 OptPrimType classify(const Expr *E) const {
122 assert(E);
123 if (E->isGLValue())
124 return PT_Ptr;
125
126 return classify(T: E->getType());
127 }
128
129 bool canClassify(QualType T) const {
130 T = T.getCanonicalType();
131 if (const auto *BT = dyn_cast<BuiltinType>(Val&: T)) {
132 if (BT->isInteger() || BT->isFloatingPoint())
133 return true;
134 if (BT->getKind() == BuiltinType::NullPtr ||
135 BT->getKind() == BuiltinType::BoundMember)
136 return true;
137 }
138 if (T->isPointerOrReferenceType())
139 return true;
140
141 if (T->isArrayType() || T->isRecordType() || T->isAnyComplexType() ||
142 T->isVectorType())
143 return false;
144
145 if (T->isEnumeralType())
146 return true;
147
148 return classify(T) != std::nullopt;
149 }
150 bool canClassify(const Expr *E) const {
151 if (E->isGLValue())
152 return true;
153 return canClassify(T: E->getType());
154 }
155
156 const CXXMethodDecl *
157 getOverridingFunction(const CXXRecordDecl *DynamicDecl,
158 const CXXRecordDecl *StaticDecl,
159 const CXXMethodDecl *InitialFunction) const;
160
161 const Function *getOrCreateFunction(const FunctionDecl *FuncDecl);
162 const Function *getOrCreateObjCBlock(const BlockExpr *E);
163
164 /// Returns whether we should create a global variable for the
165 /// given ValueDecl.
166 static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
167 if (const auto *V = dyn_cast<VarDecl>(Val: VD))
168 return V->hasGlobalStorage() || V->isConstexpr();
169
170 return false;
171 }
172
173 /// Returns the program. This is only needed for unittests.
174 Program &getProgram() const { return *P; }
175
176 unsigned collectBaseOffset(const RecordDecl *BaseDecl,
177 const RecordDecl *DerivedDecl) const;
178
179 const Record *getRecord(const RecordDecl *D) const;
180
181 unsigned getEvalID() const { return EvalID; }
182
183 /// Unevaluated builtins don't get their arguments put on the stack
184 /// automatically. They instead operate on the AST of their Call
185 /// Expression.
186 /// Similar information is available via ASTContext::BuiltinInfo,
187 /// but that is not correct for our use cases.
188 static bool isUnevaluatedBuiltin(unsigned ID);
189
190private:
191 friend class EvalIDScope;
192 /// Runs a function.
193 bool Run(State &Parent, const Function *Func);
194
195 template <typename ResultT>
196 bool evaluateStringRepr(State &Parent, const Expr *SizeExpr,
197 const Expr *PtrExpr, ResultT &Result);
198
199 /// Current compilation context.
200 ASTContext &Ctx;
201 /// Interpreter stack, shared across invocations.
202 InterpStack Stk;
203 /// Constexpr program.
204 std::unique_ptr<Program> P;
205 /// ID identifying an evaluation.
206 unsigned EvalID = 0;
207 /// Cached widths (in bits) of common types, for a faster classify().
208 unsigned ShortWidth;
209 unsigned IntWidth;
210 unsigned LongWidth;
211 unsigned LongLongWidth;
212};
213
214class EvalIDScope {
215public:
216 EvalIDScope(Context &Ctx) : Ctx(Ctx), OldID(Ctx.EvalID) { ++Ctx.EvalID; }
217 ~EvalIDScope() { Ctx.EvalID = OldID; }
218 EvalIDScope(const EvalIDScope &) = delete;
219 EvalIDScope &operator=(const EvalIDScope &) = delete;
220
221private:
222 Context &Ctx;
223 const unsigned OldID;
224};
225
226} // namespace interp
227} // namespace clang
228
229#endif
230