clang  10.0.0git
Context.h
Go to the documentation of this file.
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 "Context.h"
20 #include "InterpStack.h"
21 #include "clang/AST/APValue.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 
24 namespace clang {
25 class ASTContext;
26 class LangOptions;
27 class Stmt;
28 class FunctionDecl;
29 class VarDecl;
30 
31 namespace interp {
32 class Function;
33 class Program;
34 class State;
35 enum PrimType : unsigned;
36 
37 /// Holds all information required to evaluate constexpr code in a module.
38 class Context {
39 public:
40  /// Initialises the constexpr VM.
41  Context(ASTContext &Ctx);
42 
43  /// Cleans up the constexpr VM.
44  ~Context();
45 
46  /// Checks if a function is a potential constant expression.
47  bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
48 
49  /// Evaluates a toplevel expression as an rvalue.
50  bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
51 
52  /// Evaluates a toplevel initializer.
53  bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
54 
55  /// Returns the AST context.
56  ASTContext &getASTContext() const { return Ctx; }
57  /// Returns the language options.
58  const LangOptions &getLangOpts() const;
59  /// Returns the interpreter stack.
60  InterpStack &getStack() { return Stk; }
61  /// Returns CHAR_BIT.
62  unsigned getCharBit() const;
63 
64  /// Classifies an expression.
66 
67 private:
68  /// Runs a function.
69  bool Run(State &Parent, Function *Func, APValue &Result);
70 
71  /// Checks a result fromt the interpreter.
72  bool Check(State &Parent, llvm::Expected<bool> &&R);
73 
74 private:
75  /// Current compilation context.
76  ASTContext &Ctx;
77  /// Interpreter stack, shared across invocations.
78  InterpStack Stk;
79  /// Constexpr program.
80  std::unique_ptr<Program> P;
81 };
82 
83 } // namespace interp
84 } // namespace clang
85 
86 #endif
Represents a function declaration or definition.
Definition: Decl.h:1783
Context(ASTContext &Ctx)
Initialises the constexpr VM.
Definition: Context.cpp:24
A (possibly-)qualified type.
Definition: Type.h:654
constexpr XRayInstrMask Function
Definition: XRayInstr.h:38
Represents a variable declaration or definition.
Definition: Decl.h:820
bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result)
Evaluates a toplevel initializer.
Definition: Context.cpp:53
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:38
bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result)
Evaluates a toplevel expression as an rvalue.
Definition: Context.cpp:48
llvm::Optional< PrimType > classify(QualType T)
Classifies an expression.
Definition: Context.cpp:61
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
LineState State
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:53
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:27
bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl)
Checks if a function is a potential constant expression.
Definition: Context.cpp:28
unsigned getCharBit() const
Returns CHAR_BIT.
Definition: Context.cpp:108
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:22
NodeId Parent
Definition: ASTDiff.cpp:191
This represents one expression.
Definition: Expr.h:108
ASTContext & getASTContext() const
Returns the AST context.
Definition: Context.h:56
Bytecode function.
Definition: Function.h:59
~Context()
Cleans up the constexpr VM.
Definition: Context.cpp:26
Dataflow Directional Tag Classes.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat]...
Definition: APValue.h:115
Interface for the VM to interact with the AST walker&#39;s context.
Definition: State.h:55
const LangOptions & getLangOpts() const
Returns the language options.
Definition: Context.cpp:59
InterpStack & getStack()
Returns the interpreter stack.
Definition: Context.h:60