clang 17.0.0git
Context.cpp
Go to the documentation of this file.
1//===--- Context.cpp - 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#include "Context.h"
10#include "ByteCodeEmitter.h"
11#include "ByteCodeExprGen.h"
12#include "ByteCodeStmtGen.h"
13#include "EvalEmitter.h"
14#include "Interp.h"
15#include "InterpFrame.h"
16#include "InterpStack.h"
17#include "PrimType.h"
18#include "Program.h"
19#include "clang/AST/Expr.h"
21
22using namespace clang;
23using namespace clang::interp;
24
25Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
26
28
30 assert(Stk.empty());
31 Function *Func = P->getFunction(FD);
32 if (!Func || !Func->hasBody()) {
33 if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) {
34 Func = *R;
35 } else {
36 handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) {
37 Parent.FFDiag(Err.getRange().getBegin(),
38 diag::err_experimental_clang_interp_failed)
39 << Err.getRange();
40 });
41 return false;
42 }
43 }
44
45 APValue DummyResult;
46 if (!Run(Parent, Func, DummyResult)) {
47 return false;
48 }
49
50 return Func->isConstexpr();
51}
52
54 assert(Stk.empty());
56 if (Check(Parent, C.interpretExpr(E))) {
57 assert(Stk.empty());
58 return true;
59 }
60
61 Stk.clear();
62 return false;
63}
64
66 APValue &Result) {
67 assert(Stk.empty());
69 if (Check(Parent, C.interpretDecl(VD))) {
70 assert(Stk.empty());
71 return true;
72 }
73
74 Stk.clear();
75 return false;
76}
77
78const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
79
80std::optional<PrimType> Context::classify(QualType T) const {
81 if (T->isReferenceType() || T->isPointerType()) {
82 return PT_Ptr;
83 }
84
85 if (T->isBooleanType())
86 return PT_Bool;
87
89 switch (Ctx.getIntWidth(T)) {
90 case 64:
91 return PT_Sint64;
92 case 32:
93 return PT_Sint32;
94 case 16:
95 return PT_Sint16;
96 case 8:
97 return PT_Sint8;
98 default:
99 return {};
100 }
101 }
102
104 switch (Ctx.getIntWidth(T)) {
105 case 64:
106 return PT_Uint64;
107 case 32:
108 return PT_Uint32;
109 case 16:
110 return PT_Uint16;
111 case 8:
112 return PT_Uint8;
113 default:
114 return {};
115 }
116 }
117
118 if (T->isNullPtrType())
119 return PT_Ptr;
120
121 if (T->isFloatingType())
122 return PT_Float;
123
124 if (auto *AT = dyn_cast<AtomicType>(T))
125 return classify(AT->getValueType());
126
127 return {};
128}
129
130unsigned Context::getCharBit() const {
131 return Ctx.getTargetInfo().getCharWidth();
132}
133
134bool Context::Run(State &Parent, Function *Func, APValue &Result) {
135 InterpState State(Parent, *P, Stk, *this);
136 State.Current = new InterpFrame(State, Func, /*Caller=*/nullptr, {});
137 if (Interpret(State, Result))
138 return true;
139 Stk.clear();
140 return false;
141}
142
143bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) {
144 if (Flag)
145 return *Flag;
146 handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) {
147 Parent.FFDiag(Err.getRange().getBegin(),
148 diag::err_experimental_clang_interp_failed)
149 << Err.getRange();
150 });
151 return false;
152}
NodeId Parent
Definition: ASTDiff.cpp:191
StringRef P
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
unsigned getIntWidth(QualType T) const
const LangOptions & getLangOpts() const
Definition: ASTContext.h:762
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:744
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1917
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:82
A (possibly-)qualified type.
Definition: Type.h:736
unsigned getCharWidth() const
Definition: TargetInfo.h:464
bool isBooleanType() const
Definition: Type.h:7334
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:2063
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:2113
bool isPointerType() const
Definition: Type.h:6910
bool isReferenceType() const
Definition: Type.h:6922
bool isFloatingType() const
Definition: Type.cpp:2145
bool isNullPtrType() const
Definition: Type.h:7243
Represents a variable declaration or definition.
Definition: Decl.h:913
Compilation context for expressions.
Compilation context for statements.
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:78
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:27
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:25
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:130
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:65
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:29
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:53
std::optional< PrimType > classify(QualType T) const
Classifies an expression.
Definition: Context.cpp:80
Bytecode function.
Definition: Function.h:74
bool hasBody() const
Definition: Function.h:146
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:130
Frame storing local variables.
Definition: InterpFrame.h:29
void clear()
Clears the stack without calling any destructors.
Definition: InterpStack.cpp:20
Interpreter context.
Definition: InterpState.h:34
The program contains and links the bytecode for all functions.
Definition: Program.h:40
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
Defines the clang::TargetInfo interface.
bool Interpret(InterpState &S, APValue &Result)
Interpreter entry point.
Definition: Interp.cpp:495
@ C
Languages that the frontend can parse and compile.
@ Result
The result type of a method or function.
Error thrown by the compiler.