Context.cpp 3.9 KB

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