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