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