Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Interp/Context.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "clang/Basic/TargetInfo.h"
21
22
using namespace clang;
23
using namespace clang::interp;
24
25
1
Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
26
27
1
Context::~Context() {}
28
29
1
bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
30
1
  Function *Func = P->getFunction(FD);
31
1
  if (!Func) {
32
1
    if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) {
33
1
      Func = *R;
34
0
    } else {
35
0
      handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) {
36
0
        Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
37
0
      });
38
0
      return false;
39
0
    }
40
1
  }
41
1
42
1
  if (!Func->isConstexpr())
43
0
    return false;
44
1
45
1
  APValue Dummy;
46
1
  return Run(Parent, Func, Dummy);
47
1
}
48
49
7
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
50
7
  ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
51
7
  return Check(Parent, C.interpretExpr(E));
52
7
}
53
54
bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
55
0
                                    APValue &Result) {
56
0
  ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
57
0
  return Check(Parent, C.interpretDecl(VD));
58
0
}
59
60
0
const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
61
62
42
llvm::Optional<PrimType> Context::classify(QualType T) {
63
42
  if (T->isReferenceType() || T->isPointerType()) {
64
0
    return PT_Ptr;
65
0
  }
66
42
67
42
  if (T->isBooleanType())
68
5
    return PT_Bool;
69
37
70
37
  if (T->isSignedIntegerOrEnumerationType()) {
71
37
    switch (Ctx.getIntWidth(T)) {
72
0
    case 64:
73
0
      return PT_Sint64;
74
37
    case 32:
75
37
      return PT_Sint32;
76
0
    case 16:
77
0
      return PT_Sint16;
78
0
    case 8:
79
0
      return PT_Sint8;
80
0
    default:
81
0
      return {};
82
0
    }
83
0
  }
84
0
85
0
  if (T->isUnsignedIntegerOrEnumerationType()) {
86
0
    switch (Ctx.getIntWidth(T)) {
87
0
    case 64:
88
0
      return PT_Uint64;
89
0
    case 32:
90
0
      return PT_Uint32;
91
0
    case 16:
92
0
      return PT_Uint16;
93
0
    case 8:
94
0
      return PT_Uint8;
95
0
    default:
96
0
      return {};
97
0
    }
98
0
  }
99
0
100
0
  if (T->isNullPtrType())
101
0
    return PT_Ptr;
102
0
103
0
  if (auto *AT = dyn_cast<AtomicType>(T))
104
0
    return classify(AT->getValueType());
105
0
106
0
  return {};
107
0
}
108
109
0
unsigned Context::getCharBit() const {
110
0
  return Ctx.getTargetInfo().getCharWidth();
111
0
}
112
113
1
bool Context::Run(State &Parent, Function *Func, APValue &Result) {
114
1
  InterpState State(Parent, *P, Stk, *this);
115
1
  State.Current = new InterpFrame(State, Func, nullptr, {}, {});
116
1
  if (Interpret(State, Result))
117
0
    return true;
118
1
  Stk.clear();
119
1
  return false;
120
1
}
121
122
7
bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) {
123
7
  if (Flag)
124
7
    return *Flag;
125
0
  handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) {
126
0
    Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
127
0
  });
128
0
  return false;
129
0
}