sammine-lang
Loading...
Searching...
No Matches
LinearTypeChecker.h
Go to the documentation of this file.
1#pragma once
2#include "ast/Ast.h"
3#include "ast/ASTProperties.h"
4#include "util/Utilities.h"
5#include <string>
6#include <unordered_map>
7#include <vector>
8
15
16namespace sammine_lang::AST {
17
18enum class VarState { Unconsumed, Consumed };
19
20struct VarInfo {
21 VarState state;
22 sammine_util::Location def_location;
23 sammine_util::Location consume_location;
24 std::string name;
25 // Recursive: per-field/element tracking for wrapper types with linear innards
26 std::unordered_map<std::string, VarInfo> children;
27};
28
29using LinearVarMap = std::unordered_map<std::string, VarInfo>;
30
32 std::vector<LinearVarMap> scope_stack;
33 int loop_depth = 0;
34
35 // Scope management
36 void push_scope();
37 void pop_scope_and_check(sammine_util::Location loc);
38
39 // Variable tracking
40 void register_linear(const std::string &name, sammine_util::Location loc);
41 void consume(VarInfo *info, sammine_util::Location loc);
42 VarInfo *find_linear(const std::string &name);
43
44 // Get merged view of all scopes for snapshotting
45 LinearVarMap snapshot() const;
46 void restore(const LinearVarMap &snap);
47
48 // Branch analysis
49 void check_branch_consistency(const LinearVarMap &before,
50 const std::vector<LinearVarMap> &branches,
52
53 // Recursive dispatch
54 void check_program(ProgramAST *ast);
55 void check_func(FuncDefAST *ast);
56 void check_block(BlockAST *ast);
57 void check_stmt(ExprAST *stmt);
58 void check_var_def(VarDefAST *ast);
59 void check_binary(BinaryExprAST *ast);
60 void check_call(CallExprAST *ast);
61 void check_free(FreeExprAST *ast);
62 void check_return(ReturnExprAST *ast);
63 void check_if(IfExprAST *ast);
64 void check_while(WhileExprAST *ast);
65 void check_case(CaseExprAST *ast);
66 void check_deref(DerefExprAST *ast);
67 void check_struct_literal(StructLiteralExprAST *ast);
68 void check_array_literal(ArrayLiteralExprAST *ast);
69 void check_tuple_literal(TupleLiteralExprAST *ast);
70
71 // Recursive inner-linear tracking for wrapper types
72 void register_inner_linear(VarInfo &parent, const Type &t,
74 void check_children_consumed(const VarInfo &info);
75 VarInfo *find_child(const std::string &var_name,
76 const std::string &field_name);
77
78 const ASTProperties *props_ = nullptr;
79
80public:
81 void check(ProgramAST *program, const ASTProperties &props);
82};
83
84} // namespace sammine_lang::AST
Defined the AST Node classes (ProgramAST, StructDefAST, FuncDefAST) and a visitor interface for trave...
Holds classes and functionalities for dealing with Error handling, source locations caching & indexin...
Definition ASTProperties.h:40
An AST to simulate a { } code block.
Definition Ast.h:317
Definition Ast.h:548
Definition Ast.h:798
Definition Ast.h:302
Definition Ast.h:667
Definition Ast.h:326
Definition Ast.h:598
Definition LinearTypeChecker.h:31
Definition Ast.h:197
A variable definition: "var x = expression;" or "let (a, b) = expr;".
Definition Ast.h:420
Definition Utilities.h:70
Definition Utilities.h:130
Definition Types.h:139
Definition LinearTypeChecker.h:20