Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/AST/Interp/Context.h
Line
Count
Source (jump to first uncovered line)
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
21
namespace clang {
22
class ASTContext;
23
class LangOptions;
24
class FunctionDecl;
25
class VarDecl;
26
class APValue;
27
28
namespace interp {
29
class Function;
30
class Program;
31
class State;
32
enum PrimType : unsigned;
33
34
struct ParamOffset {
35
  unsigned Offset;
36
  bool IsPtr;
37
};
38
39
/// Holds all information required to evaluate constexpr code in a module.
40
class Context final {
41
public:
42
  /// Initialises the constexpr VM.
43
  Context(ASTContext &Ctx);
44
45
  /// Cleans up the constexpr VM.
46
  ~Context();
47
48
  /// Checks if a function is a potential constant expression.
49
  bool isPotentialConstantExpr(State &Parent, const FunctionDecl *FnDecl);
50
51
  /// Evaluates a toplevel expression as an rvalue.
52
  bool evaluateAsRValue(State &Parent, const Expr *E, APValue &Result);
53
54
  /// Evaluates a toplevel initializer.
55
  bool evaluateAsInitializer(State &Parent, const VarDecl *VD, APValue &Result);
56
57
  /// Returns the AST context.
58
0
  ASTContext &getASTContext() const { return Ctx; }
59
  /// Returns the language options.
60
  const LangOptions &getLangOpts() const;
61
  /// Returns the interpreter stack.
62
0
  InterpStack &getStack() { return Stk; }
63
  /// Returns CHAR_BIT.
64
  unsigned getCharBit() const;
65
  /// Return the floating-point semantics for T.
66
  const llvm::fltSemantics &getFloatSemantics(QualType T) const;
67
  /// Return the size of T in bits.
68
0
  uint32_t getBitWidth(QualType T) const { return Ctx.getIntWidth(T); }
69
70
  /// Classifies an expression.
71
  std::optional<PrimType> classify(QualType T) const;
72
73
  const CXXMethodDecl *
74
  getOverridingFunction(const CXXRecordDecl *DynamicDecl,
75
                        const CXXRecordDecl *StaticDecl,
76
                        const CXXMethodDecl *InitialFunction) const;
77
78
  const Function *getOrCreateFunction(const FunctionDecl *FD);
79
80
  /// Returns whether we should create a global variable for the
81
  /// given ValueDecl.
82
0
  static bool shouldBeGloballyIndexed(const ValueDecl *VD) {
83
0
    if (const auto *V = dyn_cast<VarDecl>(VD))
84
0
      return V->hasGlobalStorage() || V->isConstexpr();
85
86
0
    return false;
87
0
  }
88
89
  /// Returns the program. This is only needed for unittests.
90
0
  Program &getProgram() const { return *P.get(); }
91
92
private:
93
  /// Runs a function.
94
  bool Run(State &Parent, const Function *Func, APValue &Result);
95
96
  /// Checks a result from the interpreter.
97
  bool Check(State &Parent, llvm::Expected<bool> &&R);
98
99
  /// Current compilation context.
100
  ASTContext &Ctx;
101
  /// Interpreter stack, shared across invocations.
102
  InterpStack Stk;
103
  /// Constexpr program.
104
  std::unique_ptr<Program> P;
105
};
106
107
} // namespace interp
108
} // namespace clang
109
110
#endif