/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 | 2 | Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {} |
26 | | |
27 | 2 | Context::~Context() {} |
28 | | |
29 | 2 | bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) { |
30 | 2 | Function *Func = P->getFunction(FD); |
31 | 2 | if (!Func) { |
32 | 2 | if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) { |
33 | 2 | Func = *R; |
34 | 2 | } 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 | 2 | } |
41 | | |
42 | 2 | if (!Func->isConstexpr()) |
43 | 1 | return false; |
44 | | |
45 | 1 | APValue Dummy; |
46 | 1 | return Run(Parent, Func, Dummy); |
47 | 2 | } |
48 | | |
49 | 13 | bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) { |
50 | 13 | ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result); |
51 | 13 | return Check(Parent, C.interpretExpr(E)); |
52 | 13 | } |
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 | 66 | llvm::Optional<PrimType> Context::classify(QualType T) { |
63 | 66 | if (T->isReferenceType() || T->isPointerType()) { |
64 | 0 | return PT_Ptr; |
65 | 0 | } |
66 | | |
67 | 66 | if (T->isBooleanType()) |
68 | 5 | return PT_Bool; |
69 | | |
70 | 61 | if (T->isSignedIntegerOrEnumerationType()) { |
71 | 60 | switch (Ctx.getIntWidth(T)) { |
72 | 0 | case 64: |
73 | 0 | return PT_Sint64; |
74 | 60 | case 32: |
75 | 60 | 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 | 60 | } |
83 | 60 | } |
84 | | |
85 | 1 | 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 | | |
100 | 1 | if (T->isNullPtrType()) |
101 | 0 | return PT_Ptr; |
102 | | |
103 | 1 | if (auto *AT = dyn_cast<AtomicType>(T)) |
104 | 0 | return classify(AT->getValueType()); |
105 | | |
106 | 1 | return {}; |
107 | 1 | } |
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 | 13 | bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) { |
123 | 13 | if (Flag) |
124 | 13 | 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 | 13 | } |