ByteCodeStmtGen.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. class QualType;
  26. namespace interp {
  27. class Function;
  28. class State;
  29. template <class Emitter> class LoopScope;
  30. template <class Emitter> class SwitchScope;
  31. template <class Emitter> class LabelScope;
  32. /// Compilation context for statements.
  33. template <class Emitter>
  34. class ByteCodeStmtGen : public ByteCodeExprGen<Emitter> {
  35. using LabelTy = typename Emitter::LabelTy;
  36. using AddrTy = typename Emitter::AddrTy;
  37. using OptLabelTy = llvm::Optional<LabelTy>;
  38. using CaseMap = llvm::DenseMap<const SwitchCase *, LabelTy>;
  39. public:
  40. template<typename... Tys>
  41. ByteCodeStmtGen(Tys&&... Args)
  42. : ByteCodeExprGen<Emitter>(std::forward<Tys>(Args)...) {}
  43. protected:
  44. bool visitFunc(const FunctionDecl *F) override;
  45. private:
  46. friend class LabelScope<Emitter>;
  47. friend class LoopScope<Emitter>;
  48. friend class SwitchScope<Emitter>;
  49. // Statement visitors.
  50. bool visitStmt(const Stmt *S);
  51. bool visitCompoundStmt(const CompoundStmt *S);
  52. bool visitDeclStmt(const DeclStmt *DS);
  53. bool visitReturnStmt(const ReturnStmt *RS);
  54. bool visitIfStmt(const IfStmt *IS);
  55. /// Compiles a variable declaration.
  56. bool visitVarDecl(const VarDecl *VD);
  57. private:
  58. /// Type of the expression returned by the function.
  59. llvm::Optional<PrimType> ReturnType;
  60. /// Switch case mapping.
  61. CaseMap CaseLabels;
  62. /// Point to break to.
  63. OptLabelTy BreakLabel;
  64. /// Point to continue to.
  65. OptLabelTy ContinueLabel;
  66. /// Default case label.
  67. OptLabelTy DefaultLabel;
  68. };
  69. extern template class ByteCodeExprGen<EvalEmitter>;
  70. } // namespace interp
  71. } // namespace clang
  72. #endif