tan  0.0.1
type_check.h
1 #ifndef __TAN_ANALYSIS_TYPE_CHECK_H__
2 #define __TAN_ANALYSIS_TYPE_CHECK_H__
3 #include "base.h"
4 #include "analysis/analysis_action.h"
5 #include "common/dependency_graph.h"
6 #include "ast/package.h"
7 
8 namespace tanlang {
9 
10 class Decl;
11 class Expr;
12 class Type;
13 class Program;
14 class TokenizedSourceFile;
15 class ASTBase;
16 
17 class TypeCheck : public SemanticAnalysisAction<TypeCheck, Package *, void> {
18 public:
19  void run_impl(Package *cu);
20 
21 private:
22  /**
23  * \brief Check whether it's legal to implicitly convert from type `from` to type `to`
24  * See TYPE_CASTING.md for specifications.
25  * \param from Source type.
26  * \param to Destination type.
27  */
28  static bool CanImplicitlyConvert(Type *from, Type *to);
29 
30  /**
31  * \brief Find out which one of the two input types of a binary operation should operands promote to.
32  * See TYPE_CASTING.md for specifications.
33  * \return Guaranteed to be one of `t1` and `t2`, or nullptr if cannot find a legal promotion.
34  */
35  static Type *ImplicitTypePromote(Type *t1, Type *t2);
36 
37  Cast *create_implicit_conversion(Expr *from, Type *to);
38 
39  /**
40  * \brief Find the type that operands of a BOP should promote to, and add a Cast node to the AST.
41  * Raise an error if can't find a valid type promotion.
42  * \note This could modify the lhs or rhs of `bop`, make sure to update the references to any of them after calling.
43  * \return The promoted type.
44  */
45  Type *auto_promote_bop_operand_types(BinaryOperator *bop);
46 
47  FunctionDecl *search_function_callee(FunctionCall *p);
48 
49  /**
50  * \brief Resolve a type reference.
51  * \return Non-null
52  */
53  Type *resolve_type_ref(Type *p, ASTBase *node);
54 
55  /**
56  * \brief Resolve a type.
57  * \return Non-null
58  */
59  Type *resolve_type(Type *p, ASTBase *node);
60 
61 public:
62  // DECLARE_AST_VISITOR_IMPL(Program);
63  DECLARE_AST_VISITOR_IMPL(Identifier);
64  DECLARE_AST_VISITOR_IMPL(Parenthesis);
65  DECLARE_AST_VISITOR_IMPL(If);
66  DECLARE_AST_VISITOR_IMPL(VarDecl);
67  DECLARE_AST_VISITOR_IMPL(ArgDecl);
68  DECLARE_AST_VISITOR_IMPL(Return);
69  DECLARE_AST_VISITOR_IMPL(CompoundStmt);
70  DECLARE_AST_VISITOR_IMPL(BinaryOrUnary);
71  DECLARE_AST_VISITOR_IMPL(BinaryOperator);
72  DECLARE_AST_VISITOR_IMPL(UnaryOperator);
73  DECLARE_AST_VISITOR_IMPL(Cast);
74  DECLARE_AST_VISITOR_IMPL(Assignment);
75  DECLARE_AST_VISITOR_IMPL(FunctionCall);
76  DECLARE_AST_VISITOR_IMPL(FunctionDecl);
77  DECLARE_AST_VISITOR_IMPL(Import);
78  DECLARE_AST_VISITOR_IMPL(Intrinsic);
79  DECLARE_AST_VISITOR_IMPL(ArrayLiteral);
80  DECLARE_AST_VISITOR_IMPL(CharLiteral);
81  DECLARE_AST_VISITOR_IMPL(BoolLiteral);
82  DECLARE_AST_VISITOR_IMPL(IntegerLiteral);
83  DECLARE_AST_VISITOR_IMPL(FloatLiteral);
84  DECLARE_AST_VISITOR_IMPL(StringLiteral);
85  DECLARE_AST_VISITOR_IMPL(MemberAccess);
86  DECLARE_AST_VISITOR_IMPL(StructDecl);
87  DECLARE_AST_VISITOR_IMPL(Loop);
88  DECLARE_AST_VISITOR_IMPL(BreakContinue);
89 
90 private:
91  void analyze_func_decl_prototype(ASTBase *_p);
92 
93  void analyze_func_body(ASTBase *_p);
94 
95  void analyze_function_call(FunctionCall *p, bool include_intrinsics);
96 
97  void analyze_intrinsic_func_call(Intrinsic *p, FunctionCall *func_call);
98 
99  void analyze_member_func_call(MemberAccess *p, Expr *lhs, FunctionCall *rhs);
100 
101  void analyze_bracket_access(MemberAccess *p, Expr *lhs, Expr *rhs);
102 
103  void analyze_member_access_member_variable(MemberAccess *p, Expr *lhs, Expr *rhs);
104 };
105 
106 } // namespace tanlang
107 
108 #endif // __TAN_ANALYSIS_TYPE_CHECK_H__
Represent if-[else] or if-elif-[else] statements.
Definition: stmt.h:144
A generic representation of Intrinsic variables/functions.
Definition: intrinsic.h:55
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...
Definition: type.h:22