Joos1W Compiler Framework
All Classes Functions Typedefs Pages
HierarchyChecker.h
1 #pragma once
2 
3 #include <map>
4 #include <set>
5 #include <vector>
6 
7 #include "ast/AST.h"
8 #include "ast/AstNode.h"
9 #include "diagnostics/Diagnostics.h"
10 
11 namespace semantic {
12 
14 public:
15  HierarchyChecker(diagnostics::DiagnosticEngine& diag) : diag{diag} {}
16 
17  void Check(ast::LinkingUnit const* lu) {
18  lu_ = lu;
19  checkInheritance();
20  }
21 
22  bool isSubtype(ast::Type const* sub, ast::Type const* super);
23 
24  // @brief Check if a class is a subclass of another class
25  bool isSuperClass(ast::ClassDecl const* super, ast::ClassDecl const* sub);
26 
27  // @brief Check if an interface is a subperinterface of another class/interface
28  bool isSuperInterface(ast::InterfaceDecl const* super, ast::Decl const* sub);
29 
30  bool isInheritedSet(ast::Decl const* decl) {
31  return methodInheritanceMap_.find(decl) != methodInheritanceMap_.end();
32  }
33 
34  auto& getInheritedMethods(ast::Decl const* decl) {
35  assert(isInheritedSet(decl));
36  std::vector<int> x;
37  return methodInheritanceMap_.at(decl);
38  }
39 
40  auto& getInheritedMembers(ast::Decl const* decl) {
41  // assert(memberInheritancesMap_.find(decl) != memberInheritancesMap_.end());
42  return memberInheritancesMap_[decl];
43  }
44 
45  void setInheritedMethods(
46  ast::Decl const* decl,
47  std::pmr::vector<ast::MethodDecl const*>& inheritedMethods) {
48  assert(inheritanceMap_.find(decl) != inheritanceMap_.end());
49  methodInheritanceMap_.insert({decl, inheritedMethods});
50  }
51 
52 private:
53  diagnostics::DiagnosticEngine& diag;
54  ast::LinkingUnit const* lu_;
55  std::pmr::map<ast::Decl const*, std::pmr::set<ast::Decl const*>>
56  inheritanceMap_;
57  std::pmr::map<ast::Decl const*, std::pmr::vector<ast::MethodDecl const*>>
58  methodInheritanceMap_;
59  std::pmr::map<ast::Decl const*, std::pmr::set<ast::TypedDecl const*>>
60  memberInheritancesMap_;
61  void checkInheritance();
62 
63  // Check functions for method
64  void checkClassConstructors(ast::ClassDecl const* classDecl);
65  void checkClassMethod(
66  ast::ClassDecl const* classDecl,
67  std::pmr::vector<ast::MethodDecl const*>& inheritedMethods);
68  void checkInterfaceMethod(
69  ast::InterfaceDecl const* interfaceDecl,
70  std::pmr::vector<ast::MethodDecl const*>& inheritedMethods);
71 
72  // Check method inheritance
73  void checkMethodInheritance();
74  void checkMethodInheritanceHelper(ast::Decl const* node,
75  std::pmr::set<ast::Decl const*>& visited);
76 
77  void setInheritedMembersHelper(ast::Decl const* node, ast::Decl const* parent);
78 };
79 
80 } // namespace semantic