clang 10.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"
20
21 using namespace clang;
22 using namespace clang::interp;
23
24 Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
25
26 Context::~Context() {}
27
28 bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
29 Function *Func = P->getFunction(FD);
30 if (!Func) {
31 if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) {
32 Func = *R;
33 } else {
34 handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) {
35 Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
36 });
37 return false;
38 }
39 }
40
41 if (!Func->isConstexpr())
42 return false;
43
44 APValue Dummy;
45 return Run(Parent, Func, Dummy);
46 }
47
48 bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
49 ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
50 return Check(Parent, C.interpretExpr(E));
51 }
52
53 bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
54 APValue &Result) {
55 ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
56 return Check(Parent, C.interpretDecl(VD));
57 }
58
59 const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
60
61 llvm::Optional<PrimType> Context::classify(QualType T) {
62 if (T->isReferenceType() || T->isPointerType()) {
63 return PT_Ptr;
64 }
65
66 if (T->isBooleanType())
67 return PT_Bool;
68
69 if (T->isSignedIntegerOrEnumerationType()) {
70 switch (Ctx.getIntWidth(T)) {
71 case 64:
72 return PT_Sint64;
73 case 32:
74 return PT_Sint32;
75 case 16:
76 return PT_Sint16;
77 case 8:
78 return PT_Sint8;
79 default:
80 return {};
81 }
82 }
83
84 if (T->isUnsignedIntegerOrEnumerationType()) {
85 switch (Ctx.getIntWidth(T)) {
86 case 64:
87 return PT_Uint64;
88 case 32:
89 return PT_Uint32;
90 case 16:
91 return PT_Uint16;
92 case 8:
93 return PT_Uint8;
94 default:
95 return {};
96 }
97 }
98
99 if (T->isNullPtrType())
100 return PT_Ptr;
101
102 if (auto *AT = dyn_cast<AtomicType>(T))
103 return classify(AT->getValueType());
104
105 return {};
106 }
107
108 unsigned Context::getCharBit() const {
109 return Ctx.getTargetInfo().getCharWidth();
110 }
111
112 bool Context::Run(State &Parent, Function *Func, APValue &Result) {
113 InterpState State(Parent, *P, Stk, *this);
114 State.Current = new InterpFrame(State, Func, nullptr, {}, {});
115 if (Interpret(State, Result))
116 return true;
117 Stk.clear();
118 return false;
119 }
120
121 bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) {
122 if (Flag)
123 return *Flag;
124 handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) {
125 Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
126 });
127 return false;
128 }
Interp.h
clang::interp::InterpStack::clear
void clear()
Clears the stack without calling any destructors.
Definition: InterpStack.cpp:20
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::interp::Context::Context
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:24
ByteCodeStmtGen.h
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
clang::Type::isUnsignedIntegerOrEnumerationType
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition: Type.cpp:1984
clang::interp::InterpState::Current
InterpFrame * Current
The current frame.
Definition: InterpState.h:104
clang::interp::PT_Sint64
Definition: PrimType.h:34
clang::interp::InterpFrame
Frame storing local variables.
Definition: InterpFrame.h:29
P
StringRef P
Definition: ASTMatchersInternal.cpp:485
clang::ASTContext::getTargetInfo
const TargetInfo & getTargetInfo() const
Definition: ASTContext.h:707
clang::TargetInfo::getCharWidth
unsigned getCharWidth() const
Definition: TargetInfo.h:383
ByteCodeEmitter.h
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::interp::PT_Sint16
Definition: PrimType.h:30
clang::interp::Context::evaluateAsInitializer
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:53
clang::interp::Context::evaluateAsRValue
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:48
clang::interp::Context::classify
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:61
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
llvm::Optional< PrimType >
State
LineState State
Definition: UnwrappedLineFormatter.cpp:932
clang::interp::ByteCodeStmtGen
Compilation context for statements.
Definition: ByteCodeStmtGen.h:40
clang::interp::Interpret
bool Interpret(InterpState &S, APValue &Result)
Interpreter entry point.
Definition: Interp.cpp:401
clang::interp::PT_Uint8
Definition: PrimType.h:29
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6516
PrimType.h
clang::LangOptions
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
clang::interp::PT_Bool
Definition: PrimType.h:36
Context.h
llvm::Expected
Definition: LLVM.h:38
clang::interp::Context::isPotentialConstantExpr
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:28
clang::interp::Context::getCharBit
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:108
Parent
NodeId Parent
Definition: ASTDiff.cpp:191
clang::Expr
This represents one expression.
Definition: Expr.h:108
clang::Type::isNullPtrType
bool isNullPtrType() const
Definition: Type.h:6802
clang::interp::State::FFDiag
OptionalDiagnostic FFDiag(SourceLocation Loc, diag::kind DiagId=diag::note_invalid_subexpr_in_const_expr, unsigned ExtraNotes=0)
Definition: State.cpp:20
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:43
Program.h
clang::interp::PT_Sint32
Definition: PrimType.h:32
clang::interp::PT_Uint16
Definition: PrimType.h:31
clang::interp::Function
Bytecode function.
Definition: Function.h:59
InterpStack.h
clang::Type::isSignedIntegerOrEnumerationType
bool isSignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is signed or an enumeration types whose underlying ty...
Definition: Type.cpp:1944
clang::interp::Context::~Context
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:26
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::interp::PT_Ptr
Definition: PrimType.h:37
clang::Type::isBooleanType
bool isBooleanType() const
Definition: Type.h:6894
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::PT_Sint8
Definition: PrimType.h:28
clang::ASTContext::getIntWidth
unsigned getIntWidth(QualType T) const
Definition: ASTContext.cpp:9465
clang::interp::ByteCodeGenError
Error thrown by the compiler.
Definition: ByteCodeGenError.h:21
clang::APValue
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:115
clang::interp::Function::isConstexpr
bool isConstexpr() const
Checks if the function is valid to call in constexpr.
Definition: Function.h:103
InterpFrame.h
clang::interp::ByteCodeExprGen
Compilation context for expressions.
Definition: ByteCodeExprGen.h:41
clang::interp::State
Interface for the VM to interact with the AST walker&#39;s context.
Definition: State.h:55
clang::interp
Definition: ASTContext.h:146
Expr.h
clang::interp::Context::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:59
ByteCodeExprGen.h
clang::Type::isPointerType
bool isPointerType() const
Definition: Type.h:6504
AttributeLangSupport::C
Definition: SemaDeclAttr.cpp:45
clang::interp::PT_Uint64
Definition: PrimType.h:35
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:724
clang::interp::PT_Uint32
Definition: PrimType.h:33
EvalEmitter.h