xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/AST/Interp/Context.h (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
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 /// Wrapper around interpreter termination results.
38 enum class InterpResult {
39   /// Interpreter successfully computed a value.
40   Success,
41   /// Interpreter encountered an error and quit.
42   Fail,
43   /// Interpreter encountered an unimplemented feature, AST fallback.
44   Bail,
45 };
46 
47 /// Holds all information required to evaluate constexpr code in a module.
48 class Context {
49 public:
50   /// Initialises the constexpr VM.
51   Context(ASTContext &Ctx);
52 
53   /// Cleans up the constexpr VM.
54   ~Context();
55 
56   /// Checks if a function is a potential constant expression.
57   InterpResult isPotentialConstantExpr(State &Parent,
58                                        const FunctionDecl *FnDecl);
59 
60   /// Evaluates a toplevel expression as an rvalue.
61   InterpResult evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
62 
63   /// Evaluates a toplevel initializer.
64   InterpResult evaluateAsInitializer(State &Parent, const VarDecl *VD,
65                                      APValue &Result);
66 
67   /// Returns the AST context.
68   ASTContext &getASTContext() const { return Ctx; }
69   /// Returns the language options.
70   const LangOptions &getLangOpts() const;
71   /// Returns the interpreter stack.
72   InterpStack &getStack() { return Stk; }
73   /// Returns CHAR_BIT.
74   unsigned getCharBit() const;
75 
76   /// Classifies an expression.
77   llvm::Optional<PrimType> classify(QualType T);
78 
79 private:
80   /// Runs a function.
81   InterpResult Run(State &Parent, Function *Func, APValue &Result);
82 
83   /// Checks a result fromt the interpreter.
84   InterpResult Check(State &Parent, llvm::Expected<bool> &&R);
85 
86 private:
87   /// Current compilation context.
88   ASTContext &Ctx;
89   /// Flag to indicate if the use of the interpreter is mandatory.
90   bool ForceInterp;
91   /// Interpreter stack, shared across invocations.
92   InterpStack Stk;
93   /// Constexpr program.
94   std::unique_ptr<Program> P;
95 };
96 
97 } // namespace interp
98 } // namespace clang
99 
100 #endif
101