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 
13 #ifndef LLVM_CLANG_AST_INTERP_BYTECODESTMTGEN_H
14 #define LLVM_CLANG_AST_INTERP_BYTECODESTMTGEN_H
15 
16 #include "ByteCodeEmitter.h"
17 #include "ByteCodeExprGen.h"
18 #include "EvalEmitter.h"
19 #include "Pointer.h"
20 #include "PrimType.h"
21 #include "Record.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/Expr.h"
24 #include "clang/AST/StmtVisitor.h"
25 
26 namespace clang {
27 namespace interp {
28 
29 template <class Emitter> class LoopScope;
30 template <class Emitter> class SwitchScope;
31 template <class Emitter> class LabelScope;
32 
33 /// Compilation context for statements.
34 template <class Emitter>
35 class ByteCodeStmtGen final : public ByteCodeExprGen<Emitter> {
36   using LabelTy = typename Emitter::LabelTy;
37   using AddrTy = typename Emitter::AddrTy;
38   using OptLabelTy = std::optional<LabelTy>;
39   using CaseMap = llvm::DenseMap<const SwitchCase *, LabelTy>;
40 
41 public:
42   template<typename... Tys>
43   ByteCodeStmtGen(Tys&&... Args)
44       : ByteCodeExprGen<Emitter>(std::forward<Tys>(Args)...) {}
45 
46 protected:
47   bool visitFunc(const FunctionDecl *F) override;
48 
49 private:
50   friend class LabelScope<Emitter>;
51   friend class LoopScope<Emitter>;
52   friend class SwitchScope<Emitter>;
53 
54   // Statement visitors.
55   bool visitStmt(const Stmt *S);
56   bool visitCompoundStmt(const CompoundStmt *S);
57   bool visitLoopBody(const Stmt *S);
58   bool visitDeclStmt(const DeclStmt *DS);
59   bool visitReturnStmt(const ReturnStmt *RS);
60   bool visitIfStmt(const IfStmt *IS);
61   bool visitWhileStmt(const WhileStmt *S);
62   bool visitDoStmt(const DoStmt *S);
63   bool visitForStmt(const ForStmt *S);
64   bool visitCXXForRangeStmt(const CXXForRangeStmt *S);
65   bool visitBreakStmt(const BreakStmt *S);
66   bool visitContinueStmt(const ContinueStmt *S);
67   bool visitSwitchStmt(const SwitchStmt *S);
68   bool visitCaseStmt(const CaseStmt *S);
69   bool visitDefaultStmt(const DefaultStmt *S);
70 
71   /// Type of the expression returned by the function.
72   std::optional<PrimType> ReturnType;
73 
74   /// Switch case mapping.
75   CaseMap CaseLabels;
76 
77   /// Point to break to.
78   OptLabelTy BreakLabel;
79   /// Point to continue to.
80   OptLabelTy ContinueLabel;
81   /// Default case label.
82   OptLabelTy DefaultLabel;
83 };
84 
85 extern template class ByteCodeExprGen<EvalEmitter>;
86 
87 } // namespace interp
88 } // namespace clang
89 
90 #endif
91