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