Joos1W Compiler Framework
All Classes Functions Typedefs Pages
ExprStaticChecker.h
1 #pragma once
2 
3 #include "ast/AstNode.h"
4 #include "ast/DeclContext.h"
5 #include "ast/ExprEvaluator.h"
6 #include "diagnostics/Diagnostics.h"
7 #include "semantic/HierarchyChecker.h"
8 #include "semantic/NameResolver.h"
9 
10 namespace semantic {
11 
13  const ast::Decl* const decl;
14  const ast::Type* const type;
15  const bool isValue;
16  // NOTE: After an operation, the temp value is not an instance variable
17  // anymore because it is a new (local) value
18  const bool isInstanceVar;
19 };
20 
22  bool isStaticContext;
23  bool isInstFieldInitializer;
24  ast::ClassDecl const* currentClass;
25  ast::ScopeID const* fieldScope;
26  ExprStaticCheckerState()
27  : isStaticContext{false},
28  isInstFieldInitializer{false},
29  currentClass{nullptr},
30  fieldScope{nullptr} {}
31 };
32 
34  using ETy = ExprStaticCheckerData;
35 
36 public:
37  ExprStaticChecker(diagnostics::DiagnosticEngine& diag,
38  semantic::NameResolver& NR, semantic::HierarchyChecker &HC)
39  : diag{diag}, NR{NR}, state{}, HC{HC}, loc_{} {}
40 
41 public:
42  void Evaluate(ast::Expr* expr, ExprStaticCheckerState state);
43 
44 private: // Overriden methods
45  using Type = ast::Type;
46  using BinaryOp = ast::exprnode::BinaryOp;
47  using UnaryOp = ast::exprnode::UnaryOp;
48  using DotOp = ast::exprnode::MemberAccess;
53  using CastOp = ast::exprnode::Cast;
54  using ExprValue = ast::exprnode::ExprValue;
55 
56  ETy mapValue(ExprValue& node) const override;
57  ETy evalBinaryOp(BinaryOp& op, ETy lhs, ETy rhs) const override;
58  ETy evalUnaryOp(UnaryOp& op, ETy rhs) const override;
59  ETy evalMemberAccess(DotOp& op, ETy lhs, ETy field) const override;
60  ETy evalMethodCall(MethodOp& op, ETy method,
61  const op_array& args) const override;
62  ETy evalNewObject(NewOp& op, ETy object, const op_array& args) const override;
63  ETy evalNewArray(NewArrayOp& op, ETy type, ETy size) const override;
64  ETy evalArrayAccess(ArrayAccessOp& op, ETy array, ETy index) const override;
65  ETy evalCast(CastOp& op, ETy type, ETy value) const override;
66 
67 private:
68  void checkInstanceVar(ETy var, bool checkInitOrder = true) const;
69  void isAccessible(ETy lhs, ETy var) const;
70 
71 private:
72  diagnostics::DiagnosticEngine& diag;
73  semantic::NameResolver& NR;
75  HierarchyChecker &HC;
76  SourceRange loc_;
77 };
78 
79 } // namespace semantic