Context.cpp 3.6 KB

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