ByteCodeStmtGen.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===--- ByteCodeStmtGen.h - Code generator for expressions -----*- 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. // Defines the constexpr bytecode compiler.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_AST_INTERP_BYTECODESTMTGEN_H
  13. #define LLVM_CLANG_AST_INTERP_BYTECODESTMTGEN_H
  14. #include "ByteCodeEmitter.h"
  15. #include "ByteCodeExprGen.h"
  16. #include "EvalEmitter.h"
  17. #include "Pointer.h"
  18. #include "PrimType.h"
  19. #include "Record.h"
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/Expr.h"
  22. #include "clang/AST/StmtVisitor.h"
  23. #include "llvm/ADT/Optional.h"
  24. namespace clang {
  25. namespace interp {
  26. template <class Emitter> class LoopScope;
  27. template <class Emitter> class SwitchScope;
  28. template <class Emitter> class LabelScope;
  29. /// Compilation context for statements.
  30. template <class Emitter>
  31. class ByteCodeStmtGen : public ByteCodeExprGen<Emitter> {
  32. using LabelTy = typename Emitter::LabelTy;
  33. using AddrTy = typename Emitter::AddrTy;
  34. using OptLabelTy = llvm::Optional<LabelTy>;
  35. using CaseMap = llvm::DenseMap<const SwitchCase *, LabelTy>;
  36. public:
  37. template<typename... Tys>
  38. ByteCodeStmtGen(Tys&&... Args)
  39. : ByteCodeExprGen<Emitter>(std::forward<Tys>(Args)...) {}
  40. protected:
  41. bool visitFunc(const FunctionDecl *F) override;
  42. private:
  43. friend class LabelScope<Emitter>;
  44. friend class LoopScope<Emitter>;
  45. friend class SwitchScope<Emitter>;
  46. // Statement visitors.
  47. bool visitStmt(const Stmt *S);
  48. bool visitCompoundStmt(const CompoundStmt *S);
  49. bool visitDeclStmt(const DeclStmt *DS);
  50. bool visitReturnStmt(const ReturnStmt *RS);
  51. bool visitIfStmt(const IfStmt *IS);
  52. /// Compiles a variable declaration.
  53. bool visitVarDecl(const VarDecl *VD);
  54. private:
  55. /// Type of the expression returned by the function.
  56. llvm::Optional<PrimType> ReturnType;
  57. /// Switch case mapping.
  58. CaseMap CaseLabels;
  59. /// Point to break to.
  60. OptLabelTy BreakLabel;
  61. /// Point to continue to.
  62. OptLabelTy ContinueLabel;
  63. /// Default case label.
  64. OptLabelTy DefaultLabel;
  65. };
  66. extern template class ByteCodeExprGen<EvalEmitter>;
  67. } // namespace interp
  68. } // namespace clang
  69. #endif