Context.cpp 3.3 KB

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