tan  0.0.1
compiler_action.h
1 #ifndef __TAN_COMMON_COMPILER_ACTION_H__
2 #define __TAN_COMMON_COMPILER_ACTION_H__
3 
4 #include "common/ast_visitor.h"
5 
6 namespace tanlang {
7 
8 template <class T, class U>
9 concept SameHelper = std::is_same_v<T, U>;
10 
11 /**
12  * \brief Some compilers don't have std::same_as
13  */
14 template <class T, class U>
15 concept same_as = SameHelper<T, U> && SameHelper<U, T>;
16 
17 template <typename C, typename Input, typename Output>
18 concept HasImpl = requires(C c, Input input) {
19  { c.run_impl(input) } -> same_as<Output>;
20 };
21 
22 template <typename Derived, typename Input, typename Output> class CompilerAction : public ASTVisitor<Derived> {
23 public:
25 
26  virtual ~CompilerAction() = default;
27 
28  Output run(Input input) {
29  static_assert(HasImpl<Derived, Input, Output>);
30 
31  init(input);
32  return ((Derived *)this)->run_impl(input);
33  }
34 
35 protected:
36  virtual void init(Input){};
37 };
38 
39 } // namespace tanlang
40 
41 #endif // __TAN_COMMON_COMPILER_ACTION_H__